Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
#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;
}
# cook your dish here
# 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))
#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;
}
/* 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{
//BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
InputReader in=new InputReader(System.in);
PrintWriter out=new PrintWriter(System.out);
int t=in.readInt();
for(int t1=0;t1<t;t1++){
int n=in.readInt();
int[] a=new int[n];
for(int i=0;i<n;i++)
a[i]=in.readInt();
int start=in.readInt()-1;
int end=in.readInt()-1;
ArrayList<Integer> left=new ArrayList<Integer>();
ArrayList<Integer> right=new ArrayList<Integer>();
for(int i=start;i<end;i++){
left.add(a[i]);
}
for(int i=end;;i=(i+1)%n){
if(i==start)break;
right.add(a[i]);
}
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();
}
static final class InputReader{
private final InputStream stream;
private final byte[] buf=new byte[1024];
private int curChar;
private int numChars;
public InputReader(InputStream stream){this.stream=stream;}
private int read()throws IOException{
if(curChar>=numChars){
curChar=0;
numChars=stream.read(buf);
if(numChars<=0)
return -1;
}
return buf[curChar++];
}
public final int readInt()throws IOException{return (int)readLong();}
public final long readLong()throws IOException{
int c=read();
while(isSpaceChar(c)){
c=read();
if(c==-1) throw new IOException();
}
boolean negative=false;
if(c=='-'){
negative=true;
c=read();
}
long res=0;
do{
if(c<'0'||c>'9')throw new InputMismatchException();
res*=10;
res+=(c-'0');
c=read();
}while(!isSpaceChar(c));
return negative?(-res):(res);
}
public final int[] readIntBrray(int size)throws IOException{
int[] arr=new int[size];
for(int i=0;i<size;i++)arr[i]=readInt();
return arr;
}
public final String readString()throws IOException{
int c=read();
while(isSpaceChar(c))c=read();
StringBuilder res=new StringBuilder();
do{
res.append((char)c);
c=read();
}while(!isSpaceChar(c));
return res.toString();
}
public final String readLine()throws IOException{
int c=read();
while(isSpaceChar(c))c=read();
StringBuilder res=new StringBuilder();
do{
res.append((char)c);
c=read();
}while(!isSpaceChar(c));
return res.toString();
}
private boolean isSpaceChar(int c){
return c==' '||c=='\n'||c=='\r'||c=='\t'||c==-1;
}
}
}
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
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;
}
static void solve(StreamReader reader, StreamWriter writer)
{
var t = reader.ReadInt();
while (t-- > 0)
{
int n = reader.ReadInt();
var r = new int[n];
for (int i = 0; i < n; ++i)
r[i] = reader.ReadInt();
var start = reader.ReadInt() - 1;
var end = reader.ReadInt() - 1;
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
))
{
using (var reader = new StreamReader(
#if CODECHIEF_LOCAL
"input.txt"
#else
Console.OpenStandardInput()
#endif
))
{
#if CODECHIEF_LOCAL
var stopw = new Stopwatch();
stopw.Start();
#endif
solve(reader, writer);
#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
{
public static string ReadString(this StreamReader reader)
{
return reader.ReadToken();
}
public static int ReadInt(this StreamReader reader)
{
return int.Parse(reader.ReadToken(), CultureInfo.InvariantCulture);
}
public static long ReadLong(this StreamReader reader)
{
return long.Parse(reader.ReadToken(), CultureInfo.InvariantCulture);
}
public static double ReadDouble(this StreamReader reader)
{
return double.Parse(reader.ReadToken(), CultureInfo.InvariantCulture);
}
public static decimal ReadDecimal(this StreamReader reader)
{
return decimal.Parse(reader.ReadToken(), CultureInfo.InvariantCulture);
}
static Queue<string> buffer = new Queue<string>(100);
static string ReadToken(this StreamReader reader)
{
while (buffer.Count == 0)
{
reader.ReadLine().Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries).ToList().ForEach((t) =>
{
buffer.Enqueue(t);
});
} return buffer.Dequeue();
}
}
#endregion
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.
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!