标签:
1 #include<stdio.h> 2 #include<algorithm> 3 using namespace std; 4 const int N = 50001; 5 long long a[N]; 6 int main(){ 7 int n, i; 8 long long ma_ed, ans, sum = 0; 9 scanf("%d", &n); 10 for(i = 1; i <= n; ++i){ 11 scanf("%I64d", &a[i]); 12 sum += a[i]; 13 } 14 ma_ed = a[1]; 15 ans = max(0LL, a[1]); 16 for(i = 2; i <= n; ++i){//1~n求最大子段和 17 if(ma_ed > 0) 18 ma_ed += a[i]; 19 else 20 ma_ed = a[i]; 21 ans = max(ma_ed, ans); 22 } 23 long long mi_ed = a[1], s = a[1]; 24 //最大字段和首尾相接情况 25 for(i = 2; i <= n; ++i){ 26 if(mi_ed < 0) 27 mi_ed += a[i]; 28 else 29 mi_ed = a[i]; 30 s = min(mi_ed, s); 31 } 32 //首尾相接时,答案为序列数的总和 与 其中数相加最小的和(负值) 之差 33 printf("%I64d\n", max(ans, sum - s)); 34 return 0; 35 }
标签:
原文地址:http://www.cnblogs.com/GraceSkyer/p/5929399.html