First of all, if you change the "circle" of the title to "array", I believe everyone will do it, as follows
int sum=0,maxn=0;//sum更新最大值
for(int i = 1;i <= n;i++){
scanf("%d",&a);
if(sum < 0)sum = 0;//当sum小于零时,不如啥都不选
sum += a;
maxn = max(maxn,sum);
}
printf("%d\n",maxn);
But this question is a circle .
If it is said to perform a series of operations on the array*2, the code is more complicated, but if you change your thinking, if you choose one, it must cross the boundary ( 1 11 ornnn ). As shown in the figure,
if the blue part is the largest continuous value, thenthe red part must be the smallest continuous segment! !
So the final answer is max{maxn, cnt-minn}.
Ac Code
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+10,INF=1e9,mod=INF+7;
int n,cnt,sum,minn=0,a,maxn,sum2;
int main()
{
scanf("%d",&n);
for(int i = 1;i <= n;i++){
scanf("%d",&a);
if(sum2 < 0)sum2 = 0;
if(sum > 0)sum = 0;
sum += a;
sum2 += a;
cnt += a;
minn = min(minn,sum);
maxn = max(maxn,sum2);
}
printf("%d\n",max(cnt-minn,maxn));
return 0;
}