304 North Cardinal St.
Dorchester Center, MA 02124

# Chef and Circle Run CodeChef Solution

## Chef and Circle Run CodeChef Solution in C++14

``````#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
int main() {
ll t,n,s,e,c,a,kc,ka,i,k,lc,la;
cin>>t;
while(t-->0){
cin>>n;
vector<ll> v(n);
for(i=0;i<n;i++){
cin>>v[i];
}
cin>>s>>e;
s--;
e--;
//clock
kc=0;
c=0;
i=s;
lc=0;
while(i!=e){
c=c+v[i];
kc=kc+v[i];
if(kc<0)
kc=0;
lc=max(lc,kc);
i++;
}
kc=lc;
//anticlock
ka=0;
a=0;
i=s;
la=0;
while(i!=e){
i=(i-1+n)%n;
a=a+v[i];
ka=ka+v[i];
if(ka<0)
ka=0;
la=max(la,ka);
}
ka=la;
kc=c-kc;
if(kc>0)
kc=0;
ka=a-ka;
if(ka>0)
ka=0;
k=a+2*kc;
k=min(k,c+2*ka);
cout<<k<<endl;

}

return 0;
}``````

## Chef and Circle Run CodeChef Solution in PYTH 3

``````# cook your dish here
T = int(input())

for i in range(T):
N = int(input())

lst = [int(x) for x in input().strip().split()]
prefix_sum = []
for j in lst:
if len(prefix_sum) > 0: prefix_sum.append(prefix_sum[-1] + j)
else: prefix_sum.append(j)

U, V = [int(x) - 1 for x in input().strip().split()]

j = U
temp = 0
sum_1 = 0
max_sub_1 = 0
while True:
sum_1 += lst[j]
temp += lst[j]

if temp < 0: temp = 0
max_sub_1 = max(max_sub_1, temp)

if (j + 1) % N == V: break
j += 1
j %= N

j = U
sum_2 = 0
max_sub_2 = 0
temp = 0
j -= 1
if j < 0: j = N - 1
while True:
sum_2 += lst[j]
temp += lst[j]

if temp < 0: temp = 0
max_sub_2 = max(max_sub_2, temp)

if j == V: break
j -= 1
j %= N

ans1 = sum_1 + 2 * (sum_2 - max_sub_2)
ans2 = sum_2 + 2 * (sum_1 - max_sub_1)
print(min(ans1, ans2))``````

## Chef and Circle Run CodeChef Solution in C

``````#include<stdio.h>
long long int A[400002];
int main()
{
long long int i,start,end,curr_max,maxsofar1,maxsofar2,s,tc,n,minmin1,minmin2,sum1,sum2;

scanf("%lli",&tc);
while(tc--)
{

scanf("%lli",&n);
for(i=1;i<=n;i++)
scanf("%lli",&A[i]);
scanf("%lli%lli",&start,&end);
curr_max=maxsofar1=sum1=0;
for(i=start;i<end;i++)
{
curr_max=curr_max+A[i];
if(curr_max<0)
{
curr_max=0;

}
if(curr_max>maxsofar1)
{
maxsofar1=curr_max;

}

sum1=sum1+A[i];
}

for(i=n+1;i<=n+start;i++)
{
A[i]=A[i-n];
}
curr_max=maxsofar2=sum2=0;
for(i=end;i<n+start;i++)
{
curr_max=curr_max+A[i];
if(curr_max<0)
{
curr_max=0;

}
if(curr_max>maxsofar2)
{
maxsofar2=curr_max;

}

sum2=sum2+A[i];
}

minmin1=sum1+2*(sum2-maxsofar2);
minmin2=sum2+2*(sum1-maxsofar1);
if(minmin1>minmin2)
printf("%lli\n",minmin2);
else
printf("%lli\n",minmin1);
}
return 0;
}  ``````

## Chef and Circle Run CodeChef Solution in JAVA

``````/* package codechef; // don't place package name! */

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

public class Main{
public static int mod=786433;
public static void main (String[] args) throws java.lang.Exception{
PrintWriter out=new PrintWriter(System.out);
for(int t1=0;t1<t;t1++){
int[] a=new int[n];
for(int i=0;i<n;i++)
ArrayList<Integer> left=new ArrayList<Integer>();
ArrayList<Integer> right=new ArrayList<Integer>();
for(int i=start;i<end;i++){
}
for(int i=end;;i=(i+1)%n){
if(i==start)break;
}
long suml=0,sumr=0,maxl=0,sumsofar=0;
for(int i=0;i<left.size();i++){
suml+=left.get(i);
sumsofar=Math.max(sumsofar+left.get(i),left.get(i));
maxl=Math.max(maxl,sumsofar);
}
long maxr=0;sumsofar=0;
for(int i=0;i<right.size();i++){
sumr+=right.get(i);
sumsofar=Math.max(sumsofar+right.get(i),right.get(i));
maxr=Math.max(maxr,sumsofar);
}
out.println(Math.min(suml+2*(sumr-maxr),sumr+2*(suml-maxl)));
}
out.close();
}
private final InputStream stream;
private final byte[] buf=new byte[1024];
private int curChar;
private int numChars;
if(curChar>=numChars){
curChar=0;
if(numChars<=0)
return -1;
}
return buf[curChar++];
}
while(isSpaceChar(c)){
if(c==-1) throw new IOException();
}
boolean negative=false;
if(c=='-'){
negative=true;
}
long res=0;
do{
if(c<'0'||c>'9')throw new InputMismatchException();
res*=10;
res+=(c-'0');
}while(!isSpaceChar(c));
return negative?(-res):(res);
}
public final int[] readIntBrray(int size)throws IOException{
int[] arr=new int[size];
return arr;
}
StringBuilder res=new StringBuilder();
do{
res.append((char)c);
}while(!isSpaceChar(c));
return res.toString();
}
StringBuilder res=new StringBuilder();
do{
res.append((char)c);
}while(!isSpaceChar(c));
return res.toString();
}
private boolean isSpaceChar(int c){
return c==' '||c=='\n'||c=='\r'||c=='\t'||c==-1;
}
}
}  ``````

## Chef and Circle Run CodeChef Solution in PYTH

``````for t in xrange(int(raw_input())):
n = int(raw_input())
a = map(int, raw_input().split())
start, end = map(int, raw_input().split())
start -= 1
end -= 1
b1 = [0]
b2 = [0]
b3 = [0]
b4 = [0]

temp = start
while (temp != end):
b1.append(a[temp] + b1[-1])
temp = (temp + 1) % n

temp = (start)%n
while (temp != end):
b2.append(a[temp-1] + b2[-1])
temp = (temp - 1) % n

start, end = end, start

temp = start
while (temp != end):
b3.append(a[temp] + b3[-1])
temp = (temp + 1) % n

temp = (start)%n
while (temp != end):
b4.append(a[temp-1] + b4[-1])
temp = (temp - 1) % n

x1 = b1[1:]
x2 = [0]
for e in b2[1:]:
x2.append(min(x2[-1], e))
x2 = x2[1:]
x3 = [0]
for e in b3[1:]:
x3.append(min(x3[-1], e))
x3 = x3[1:][::-1]
m = min(0, min(x2), min(x3))
for i in range(len(x2)-1):
m = min(m, x2[i] + x3[i+1])
ans = x1[-1] + 2*m

x1 = b2[1:]
x2 = [0]
for e in b1[1:]:
x2.append(min(x2[-1], e))
x2 = x2[1:]
x3 = [0]
for e in b4[1:]:
x3.append(min(x3[-1], e))
x3 = x3[1:][::-1]
m = min(0, min(x2), min(x3))
for i in range(len(x2)-1):
m = min(m, x2[i] + x3[i+1])
ans = min(ans, x1[-1] + 2*m)

print ans``````

## Chef and Circle Run CodeChef Solution in C#

``````using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;

class Program
{
static long maxGap(int[] r, int n, int start, int end)
{
long partial = 0;
long res = 0;
long curMin = 0;
for (int i = start; i != end; i = (i + 1) % n)
{
partial += r[i];
if (res < partial - curMin)
res = partial - curMin;
curMin = Math.Min(curMin, partial);
}

return res;
}

{
while (t-- > 0)
{
var r = new int[n];
for (int i = 0; i < n; ++i)
long sum = 0;
long between = 0;
for (int i = 0; i < n; ++i)
sum += r[i];
for (int i = start; i < end; ++i)
between += r[i];

long res = long.MaxValue;

var temp = maxGap(r, n, start, end);

res = sum - between + 2 * (between - temp);

temp = maxGap(r, n, end, start);

res = Math.Min(res, between + 2 * (sum - between - temp));

writer.WriteLine(res);
}
}

#region launch
static void Main(string[] args)
{
using (var writer = new FormattedStreamWriter(
#if CODECHIEF_LOCAL
"output.txt"
#else
Console.OpenStandardOutput()
#endif
))
{
#if CODECHIEF_LOCAL
"input.txt"
#else
Console.OpenStandardInput()
#endif
))
{
#if CODECHIEF_LOCAL
var stopw = new Stopwatch();
stopw.Start();
#endif
#if CODECHIEF_LOCAL
stopw.Stop();
writer.WriteLine("> {0}ms", stopw.ElapsedMilliseconds);
#endif
}
}
}
#endregion
}
#region helpers
class FormattedStreamWriter : StreamWriter
{
public FormattedStreamWriter(Stream stream) : base(stream) { }
public FormattedStreamWriter(string filePath) : base(filePath) { }
public override IFormatProvider FormatProvider
{
get
{
return CultureInfo.InvariantCulture;
}
}
}
static class IOExtensions
{
{
}
{
}
{
}
{
}
{
}

static Queue<string> buffer = new Queue<string>(100);
{
while (buffer.Count == 0)
{
{
buffer.Enqueue(t);
});
} return buffer.Dequeue();
}
}
#endregion``````
##### Chef and Circle Run CodeChef Solution Review:

In our experience, we suggest you solve this Chef and Circle Run 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 Chef and Circle Run CodeChef Solution.

Find on CodeChef

##### Conclusion:

I hope this Chef and Circle Run 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!