分两种情况考虑,最长连续和,可能是总和-最小连续和,也可能是最长连续和
#include <iostream>
#include <cstdio>
using namespace std;
int A[200108];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
int sum=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&A[i]);
sum+=A[i];
}
int maxlen=0;
int sum1=0;
for(int i=1;i<=n;i++)
{
sum1+=A[i];
if(sum1>0)
{
sum1=0;
}
if(sum1<maxlen)
{
maxlen=sum1;
}
}
sum-=maxlen;
sum1=0;
int aa=0;
for(int i=1;i<=n;i++)
{
sum1+=A[i];
if(sum1<0)
{
sum1=0;
}
if(sum1>aa)
{
aa=sum1;
}
}
sum=sum>aa?sum:aa;
printf("%d\n",sum);
}
return 0;
}