http://www.51nod.com/onlineJudge/questionCode.html#problemId=1050¬iceId=13385
参考:http://blog.csdn.net/acdreamers/article/details/38760805
#include<cstdio> #include<algorithm> using namespace std; typedef long long ll; int n; ll a[1000010]; ll work() { ll temp=0,m=0; for(int i=0;i<n;i++) { temp+=a[i]; if(temp<0) temp=0; if(m<temp) m=temp; } return m; } int main() { int t; ll sum,sum1,sum2; scanf("%d",&t); while(t--) { sum=0; scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%lld",&a[i]); sum+=a[i]; } sum1=work(); for(int i=0;i<n;i++) a[i]=-a[i]; sum2=work(); // printf("%lld %lld %lld ",sum,sum1,sum2); sum=max(sum+sum2,sum1); printf("%lld ",sum); } return 0; }