304 North Cardinal St.
Dorchester Center, MA 02124

# Reign CodeChef Solution

## Reign CodeChef Solution in C++14

``````#include <bits/stdc++.h>

#pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#pragma GCC optimize("Ofast")//Comment optimisations for interactive problems (use endl)
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")

using namespace std;

struct PairHash {inline std::size_t operator()(const std::pair<int, int> &v) const { return v.first * 31 + v.second; }};

// speed
#define Code ios_base::sync_with_stdio(false);
#define By ios::sync_with_stdio(0);
#define Sumfi cout.tie(NULL);

// alias
using ll = long long;
using ld = long double;
using ull = unsigned long long;

// constants
const ld PI = 3.14159265358979323846;  /* pi */
const ll INF = 1e18;
const ld EPS = 1e-9;
const ll MAX_N = 1010101;
const ll mod = 1e9 + 7;

// typedef
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef array<ll,3> all3;
typedef array<ll,5> all5;
typedef vector<all3> vall3;
typedef vector<all5> vall5;
typedef vector<ld> vld;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<int> vi;
typedef deque<ll> dqll;
typedef deque<pll> dqpll;
typedef pair<string, string> pss;
typedef vector<pss> vpss;
typedef vector<string> vs;
typedef vector<vs> vvs;
typedef unordered_set<ll> usll;
typedef unordered_set<pll, PairHash> uspll;
typedef unordered_map<ll, ll> umll;
typedef unordered_map<pll, ll, PairHash> umpll;

// macros
#define rep(i,m,n) for(ll i=m;i<n;i++)
#define rrep(i,m,n) for(ll i=n;i>=m;i--)
#define all(a) begin(a), end(a)
#define rall(a) rbegin(a), rend(a)
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
#define INF(a) memset(a,0x3f3f3f3f3f3f3f3fLL,sizeof(a))
#define ASCEND(a) iota(all(a),0)
#define sz(x) ll((x).size())
#define BIT(a,i) (a & (1ll<<i))
#define BITSHIFT(a,i,n) (((a<<i) & ((1ll<<n) - 1)) | (a>>(n-i)))
#define pyes cout<<"YES\n";
#define pno cout<<"NO\n";
#define endl "\n"
#define pneg1 cout<<"-1\n";
#define ppossible cout<<"Possible\n";
#define pimpossible cout<<"Impossible\n";
#define TC(x) cout<<"Case #"<<x<<": ";
#define X first
#define Y second

// utility functions
template <typename T>
void print(T &&t)  { cout << t << "\n"; }
template<typename T>
void printv(vector<T>v){ll n=v.size();rep(i,0,n){cout<<v[i];if(i+1!=n)cout<<' ';}cout<<endl;}
template<typename T>
void printvln(vector<T>v){ll n=v.size();rep(i,0,n)cout<<v[i]<<endl;}
void fileIO(string in = "input.txt", string out = "output.txt") {freopen(in.c_str(),"r",stdin); freopen(out.c_str(),"w",stdout);}
template<typename T>
template<typename T, typename U>
template<typename T, typename U>

struct Combination {
vll fac, inv;
ll n, MOD;

ll modpow(ll n, ll x, ll MOD = mod) { if(!x) return 1; ll res = modpow(n,x>>1,MOD); res = (res * res) % MOD; if(x&1) res = (res * n) % MOD; return res; }

Combination(ll _n, ll MOD = mod): n(_n + 1), MOD(MOD) {
inv = fac = vll(n,1);
rep(i,1,n) fac[i] = fac[i-1] * i % MOD;
inv[n - 1] = modpow(fac[n - 1], MOD - 2, MOD);
rrep(i,1,n - 2) inv[i] = inv[i + 1] * (i + 1) % MOD;
}

ll fact(ll n) {return fac[n];}
ll nCr(ll n, ll r) {
if(n < r or n < 0 or r < 0) return 0;
return fac[n] * inv[r] % MOD * inv[n-r] % MOD;
}
};

struct Matrix {
ll r,c;
vvll matrix;
Matrix(ll r, ll c, ll v = 0): r(r), c(c), matrix(vvll(r,vll(c,v))) {}

Matrix operator*(const Matrix& B) const {
Matrix res(r, B.c);
rep(i,0,r) rep(j,0,B.c) rep(k,0,B.r) {
res.matrix[i][j] = (res.matrix[i][j] + matrix[i][k] * B.matrix[k][j] % mod) % mod;
}
return res;
}

Matrix copy() {
Matrix copy(r,c);
copy.matrix = matrix;
return copy;
}

Matrix pow(ll n) {
assert(r == c);
Matrix res(r,r);
Matrix now = copy();
rep(i,0,r) res.matrix[i][i] = 1;
while(n) {
if(n & 1) res = res * now;
now = now * now;
n /= 2;
}
return res;
}
};

// geometry data structures
template <typename T>
struct Point {
T y,x;
Point(T y, T x) : y(y), x(x) {}
Point(pair<T,T> p) : y(p.first), x(p.second) {}
Point() {}
void input() {cin>>y>>x;}
friend ostream& operator<<(ostream& os, const Point<T>& p) { os<<p.y<<' '<<p.x<<'\n'; return os;}
Point<T> operator+(Point<T>& p) {return Point<T>(y + p.y, x + p.x);}
Point<T> operator-(Point<T>& p) {return Point<T>(y - p.y, x - p.x);}
Point<T> operator*(ll n) {return Point<T>(y*n,x*n); }
Point<T> operator/(ll n) {return Point<T>(y/n,x/n); }
bool operator<(const Point &other) const {if (x == other.x) return y < other.y;return x < other.x;}
Point<T> rotate(Point<T> center, ld angle) {
ld si = sin(angle * PI / 180.), co = cos(angle * PI / 180.);
ld y = this->y - center.y;
ld x = this->x - center.x;

return Point<T>(y * co - x * si + center.y, y * si + x * co + center.x);
}
ld distance(Point<T> other) {
T dy = abs(this->y - other.y);
T dx = abs(this->x - other.x);
return sqrt(dy * dy + dx * dx);
}

T norm() { return x * x + y * y; }
};

template<typename T>
struct Line {
Point<T> A, B;
Line(Point<T> A, Point<T> B) : A(A), B(B) {}
Line() {}

void input() {
A = Point<T>();
B = Point<T>();
A.input();
B.input();
}

T ccw(Point<T> &a, Point<T> &b, Point<T> &c) {
T res = a.x * b.y + b.x * c.y + c.x * a.y;
res -= (a.x * c.y + b.x * a.y + c.x * b.y);
return res;
}

bool isIntersect(Line<T> o) {
T p1p2 = ccw(A,B,o.A) * ccw(A,B,o.B);
T p3p4 = ccw(o.A,o.B,A) * ccw(o.A,o.B,B);
if (p1p2 == 0 && p3p4 == 0) {
pair<T,T> p1(A.y, A.x), p2(B.y,B.x), p3(o.A.y, o.A.x), p4(o.B.y, o.B.x);
if (p1 > p2) swap(p2, p1);
if (p3 > p4) swap(p3, p4);
return p3 <= p2 && p1 <= p4;
}
return p1p2 <= 0 && p3p4 <= 0;
}

pair<bool,Point<ld>> intersection(Line<T> o) {
if(!this->intersection(o)) return {false, {}};
ld det = 1. * (o.B.y-o.A.y)*(B.x-A.x) - 1.*(o.B.x-o.A.x)*(B.y-A.y);
ld t = ((o.B.x-o.A.x)*(A.y-o.A.y) - (o.B.y-o.A.y)*(A.x-o.A.x)) / det;
return {true, {A.y + 1. * t * (B.y - A.y), B.x + 1. * t * (B.x - A.x)}};
}

//@formula for : y = ax + b
//@return {a,b};
pair<ld, ld> formula() {
T y1 = A.y, y2 = B.y;
T x1 = A.x, x2 = B.x;
if(y1 == y2) return {1e9, 0};
if(x1 == x2) return {0, 1e9};
ld a = 1. * (y2 - y1) / (x2 - x1);
ld b = -x1 * a + y1;
return {a, b};
}
};

template<typename T>
struct Circle {
Point<T> center;
Circle() {}

void input() {
center = Point<T>();
center.input();
}

bool circumference(Point<T> p) {
return (center.x - p.x) * (center.x - p.x) + (center.y - p.y) * (center.y - p.y) == radius * radius;
}

bool intersect(Circle<T> c) {
T d = (center.x - c.center.x) * (center.x - c.center.x) + (center.y - c.center.y) * (center.y - c.center.y);
}

bool include(Circle<T> c) {
T d = (center.x - c.center.x) * (center.x - c.center.x) + (center.y - c.center.y) * (center.y - c.center.y);
}
};

ll __gcd(ll x, ll y) { return !y ? x : __gcd(y, x % y); }
all3 __exgcd(ll x, ll y) { if(!y) return {x,1,0}; auto [g,x1,y1] = __exgcd(y, x % y); return {g, y1, x1 - (x/y) * y1}; }
ll __lcm(ll x, ll y) { return x / __gcd(x,y) * y; }
ll modpow(ll n, ll x, ll MOD = mod) { n%=MOD; if(!x) return 1; ll res = modpow(n,x>>1,MOD); res = (res * res) % MOD; if(x&1) res = (res * n) % MOD; return res; }

ll solve(vll A, ll k) {
ll res = -INF;
vll dp(sz(A) + 1 + k + 1, -INF), dpp(sz(A) + 1 + k + 1, -INF), dppp(sz(A) + 1 + k + 1, -INF);
rrep(i,0,sz(A) - 1) {
dp[i] = max(A[i], dp[i+1] + A[i]);
dpp[i] = max(dp[i], dpp[i + 1]);
dppp[i] = max(dppp[i + 1] + A[i], dpp[i + k + 1] + A[i]);
res = max(res, dppp[i]);
}
return res;
}
int main() {
Code By Sumfi
cout.precision(12);
ll tc = 1;
cin>>tc;
rep(i,1,tc+1) {
ll n,k;
cin>>n>>k;
vll A(n);
print(solve(A,k));
}
return 0;
}``````

## Reign CodeChef Solution in PYTH 3

``````# cook your dish here
t = int(input())
for _ in range(t):
n,k = map(int,input().split())
L = list(map(int,input().split()))
M = [L[0]]
for i in range(1,n):
M.append(max(0,M[-1])+L[i])
N = [L[-1]]
for i in range(n-2,-1,-1):
N.append(max(0,N[-1])+L[i])
M1 = [M[0]]
m = M[0]
for i in range(1,n):
m = max(m,M[i])
M1.append(m)
N1 = [N[0]]
m = N[0]
for i in range(1,n):
m = max(m,N[i])
N1.append(m)
N1.reverse()
i = 0
j = k+1
ans = M1[i] + N1[j]
while j < n:
ans = max(ans,M1[i]+N1[j])
i += 1
j += 1
print(ans)``````

## Reign CodeChef Solution in C

``````#include <stdio.h>
#include <stdlib.h>
long long int maxi(long long int aa,long long int bb)
{
if(aa<bb)
return bb;
return aa;
}
int main()
{
int testc=0;
long long int lr[100001],rl[100001],arr[100001],sum=0,max=-2000000001;
int n,i,k,j;
scanf("%d",&testc);
while(testc>0)
{
scanf("%d%d",&n,&k);
for(i=0;i<n;i++)
scanf("%lld",&arr[i]);
sum=0;
max=-2000000001;
for(i=0;i<n-k-1;i++)
{
if(sum+arr[i]>0)
{
if(max<sum+arr[i])
{
max=sum+arr[i];}
sum=sum+arr[i];
lr[i]=max;
}
else
{
sum=0;
max=maxi(max,arr[i]);
lr[i]=max;
}
}
sum=0;
max=-2000000001;
for(j=n-1;j>k;j--)
{
if(sum+arr[j]>0)
{
if(max<sum+arr[j])
{
max=sum+arr[j];}
sum=sum+arr[j];
rl[j]=max;
}
else
{
sum=0;
max=maxi(max,arr[j]);
rl[j]=max;
}
}
max=-2000000001;
for(i=0;i<n-k-1;i++)
{
max=maxi(max,lr[i]+rl[i+k+1]);
}
printf("%lld\n",max);
testc--;
}
return 0;
}
``````

## Reign CodeChef Solution in JAVA

``````
import java.io.*;
import java.math.*;
import java.util.*;

class Reign
{
public static void main(String[] args)throws Exception
{
new Solver().solve();
}
}

//*   Success is not final, failure is not fatal: it is the courage to continue that counts.

class Solver {

void solve() throws Exception
{
int t = sc.nextInt();
while(t-->0){
int n =sc.nextInt();
int k = sc.nextInt();
long[] arr = sc.getLongArray(n);
long sum = 0;
long max = Long.MIN_VALUE;
long[][] dp = new long[2][n];
for(int i =0;i<n;i++){
sum+=arr[i];
if(sum<arr[i]){
sum= arr[i];
}
max = Math.max(sum,max);
dp[0][i] = max;
}

sum = 0;
max = Long.MIN_VALUE;
for(int i = n-1;i>=0;i--){
sum+=arr[i];
if(sum<arr[i]){
sum= arr[i];
}
max = Math.max(sum,max);
dp[1][i] = max;
}
// for(long x : dp[0]){
//     sc.print(x+" ");
// }
// sc.println();
// for(long x : dp[1]){
//     sc.print(x+" ");
// }
// sc.println();
long ans = Long.MIN_VALUE;
for(int i =k+1;i<n;i++){
ans = Math.max(dp[1][i]+dp[0][i-k-1],ans);
}
sc.println(ans);
}

sc.flush();
}

final Helper sc;
final int MAXN = 1000_006;
final long MOD = (long) 1e9 + 7;

Solver() {
sc = new Helper(MOD, MAXN);
sc.initIO(System.in, System.out);
}

}

class Helper {
final long MOD;
final int MAXN;
final Random rnd;

public Helper(long mod, int maxn) {
MOD = mod;
MAXN = maxn;
rnd = new Random();
}

public static int[] sieve;
public static ArrayList<Integer> primes;

public void setSieve() {
primes = new ArrayList<>();
sieve = new int[MAXN];
int i, j;
for (i = 2; i*i < MAXN; ++i)
if (sieve[i] == 0) {
for (j = i*i; j < MAXN; j += i) {
sieve[j] = i;
}
}
}

public static long[] factorial;

public void setFactorial() {
factorial = new long[MAXN];
factorial[0] = 1;
for (int i = 1; i < MAXN; ++i) factorial[i] = factorial[i - 1] * i % MOD;
}

public long getFactorial(int n) {
if (factorial == null) setFactorial();
return factorial[n];
}

public long ncr(int n, int r) {
if (r > n) return 0;
if (factorial == null) setFactorial();
long numerator = factorial[n];
long denominator = factorial[r] * factorial[n - r] % MOD;
return numerator * pow(denominator, MOD - 2, MOD) % MOD;
}

public long[] getLongArray(int size) throws Exception {
long[] ar = new long[size];
for (int i = 0; i < size; ++i) ar[i] = nextLong();
return ar;
}

public int[] getIntArray(int size) throws Exception {
int[] ar = new int[size];
for (int i = 0; i < size; ++i) ar[i] = nextInt();
return ar;
}

public long gcd(long a, long b) {
return b == 0 ? a : gcd(b, a % b);
}

public int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}

public long max(long[] ar) {
long ret = ar[0];
for (long itr : ar) ret = Math.max(ret, itr);
return ret;
}

public int max(int[] ar) {
int ret = ar[0];
for (int itr : ar) ret = Math.max(ret, itr);
return ret;
}

public long min(long[] ar) {
long ret = ar[0];
for (long itr : ar) ret = Math.min(ret, itr);
return ret;
}

public int min(int[] ar) {
int ret = ar[0];
for (int itr : ar) ret = Math.min(ret, itr);
return ret;
}

public long sum(long[] ar) {
long sum = 0;
for (long itr : ar) sum += itr;
return sum;
}

public long sum(int[] ar) {
long sum = 0;
for (int itr : ar) sum += itr;
return sum;
}

public void shuffle(int[] ar) {
int r;
for (int i = 0; i < ar.length; ++i) {
r = rnd.nextInt(ar.length);
if (r != i) {
ar[i] ^= ar[r];
ar[r] ^= ar[i];
ar[i] ^= ar[r];
}
}
}

public void shuffle(long[] ar) {
int r;
for (int i = 0; i < ar.length; ++i) {
r = rnd.nextInt(ar.length);
if (r != i) {
ar[i] ^= ar[r];
ar[r] ^= ar[i];
ar[i] ^= ar[r];
}
}
}

public long pow(long base, long exp, long MOD) {
base %= MOD;
long ret = 1;
while (exp > 0) {
if ((exp & 1) == 1) ret = ret * base % MOD;
base = base * base % MOD;
exp >>= 1;
}
return ret;
}

static byte[] buf = new byte[1000_006];
static int index, total;
static InputStream in;
static BufferedWriter bw;

public void initIO(InputStream is, OutputStream os) {
try {
in = is;
bw = new BufferedWriter(new OutputStreamWriter(os));
} catch (Exception e) {
}
}

public void initIO(String inputFile, String outputFile) {
try {
in = new FileInputStream(inputFile);
bw = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(outputFile)));
} catch (Exception e) {
}
}

private int scan() throws Exception {
if (index >= total) {
index = 0;
if (total <= 0)
return -1;
}
return buf[index++];
}

public String nextLine() throws Exception {
int c;
for (c = scan(); c <= 32; c = scan()) ;
StringBuilder sb = new StringBuilder();
for (; c >= 32; c = scan())
sb.append((char) c);
return sb.toString();
}

public String next() throws Exception {
int c;
for (c = scan(); c <= 32; c = scan()) ;
StringBuilder sb = new StringBuilder();
for (; c > 32; c = scan())
sb.append((char) c);
return sb.toString();
}

public int nextInt() throws Exception {
int c, val = 0;
for (c = scan(); c <= 32; c = scan()) ;
boolean neg = c == '-';
if (c == '-' || c == '+')
c = scan();
for (; c >= '0' && c <= '9'; c = scan())
val = (val << 3) + (val << 1) + (c & 15);
return neg ? -val : val;
}

public long nextLong() throws Exception {
int c;
long val = 0;
for (c = scan(); c <= 32; c = scan()) ;
boolean neg = c == '-';
if (c == '-' || c == '+')
c = scan();
for (; c >= '0' && c <= '9'; c = scan())
val = (val << 3) + (val << 1) + (c & 15);
return neg ? -val : val;
}

public void print(Object a) throws Exception {
bw.write(a.toString());
}

public void printsp(Object a) throws Exception {
print(a);
print(" ");
}

public void println() throws Exception {
bw.write("\n");
}

public void printArray(int[] arr) throws Exception{
for(int i = 0;i<arr.length;i++){
print(arr[i]+ " ");
}
println();
}

public void println(Object a) throws Exception {
print(a);
println();
}

public void flush() throws Exception {
bw.flush();
}
}``````

## Reign CodeChef Solution in PYPY 3

``````def kanade(a):
ans = c = float('-inf')
p = []
for i in a:
c = max(i, c + i)
ans = max(ans, c)
p += ans,
return p

t = int(raw_input())
while t:
t-=1
n, k = map(int, raw_input().split())
s = map(int, raw_input().split())
ans = float('-inf')
for i in xrange(n):
if i + k + 1 < n:
ans = max(ans, a[i] + b[i+k+1])
print ans
``````

## Reign CodeChef Solution in PYTH

``````def kanade(a):
ans = c = float('-inf')
p = []
for i in a:
c = max(i, c + i)
ans = max(ans, c)
p += ans,
return p

t = int(raw_input())
while t:
t-=1
n, k = map(int, raw_input().split())
s = map(int, raw_input().split())
ans = float('-inf')
for i in xrange(n):
if i + k + 1 < n:
ans = max(ans, a[i] + b[i+k+1])
print ans
``````

## Reign CodeChef Solution in C#

``````using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace reign_modified
{
class Program
{
static void Main(string[] args)
{
labl:
if (string.IsNullOrEmpty(inp))
goto labl;

int t = Convert.ToInt32(inp);
Int64[] res = new Int64[t];

for (int zz = 0; zz < t; zz++)
{
int n, q;
lab1:
if (string.IsNullOrEmpty(s1))
goto lab1;
string[] str = s1.Split(' ');
n = Convert.ToInt32(str[0]);
q = Convert.ToInt32(str[1]);
Int64[] arr = new Int64[n];
Int64 maximum = -1000000000000;
if (n > 0)
{
lab2:
if (string.IsNullOrEmpty(s2))
goto lab2;
string[] str2 = s2.Split(' ');
int i, j;
for (i = 0; i < n; i++)
arr[i] = Convert.ToInt64(str2[i]);

Int64 max_so_far = arr[0];
Int64 max_ending_here = arr[0];
Int64[] sum1 = new Int64[n];
Int64[] sum2 = new Int64[n];
sum1[0] = arr[0];
for (i = 1; i < n; i++)
{
if (max_ending_here < 0)
max_ending_here = arr[i];
else
max_ending_here += arr[i];

if (max_ending_here >= max_so_far)
max_so_far = max_ending_here;

sum1[i] = max_so_far;
}
max_so_far = arr[n - 1];
max_ending_here = arr[n - 1];
sum2[n - 1] = arr[n - 1];
for (i = n-2; i >=0; i--)
{
if (max_ending_here < 0)
max_ending_here = arr[i];
else
max_ending_here += arr[i];

if (max_ending_here >= max_so_far)
max_so_far = max_ending_here;

sum2[i] = max_so_far;
}
//Console.WriteLine("max : " + maximum);
for (i = 0; i < n; i++)
{
j = i + q + 1;
if (j < n)
{
maximum = Math.Max(maximum, sum1[i] + sum2[j]);
}
else break;
}
}
res[zz] = maximum;
}
for (int zz = 0; zz < t; zz++)
Console.WriteLine(res[zz]);
}
}
}``````
##### Reign CodeChef Solution Review:

In our experience, we suggest you solve this Reign CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Reign CodeChef Solution.

Find on CodeChef

##### Conclusion:

I hope this Reign CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Programming Language in a business context; there are no prerequisites.

Keep Learning!