标签:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 37035 | Accepted: 11551 |
Description
Input
Output
Sample Input
1 10 1 -1 2 2 3 -3 4 -4 5 -5
Sample Output
13
Hint
Source
const int maxn = 50010; int num[maxn]; int le[maxn], ri[maxn]; int main() { int T; scanf("%d", &T); while (T --) { memset(le, 0, sizeof(le)); memset(ri, 0, sizeof(ri)); int n; scanf("%d", &n); for (int i = 1; i <= n; i ++) scanf("%d", &num[i]); le[1] = num[1]; ri[n] = num[n]; int cur_sum = num[1] < 0 ? 0 : num[1]; for (int i = 2; i <= n; i ++) { cur_sum += num[i]; le[i] = max(le[i-1], cur_sum); if (cur_sum < 0) cur_sum = 0; } cur_sum = num[n] < 0 ? 0 : num[n]; for (int i = n - 1; i >= 1; i --) { cur_sum += num[i]; ri[i] = max(ri[i+1], cur_sum); if (cur_sum < 0) cur_sum = 0; } int ans = -0x3fffffff; for (int i = 2; i <= n; i ++) { ans = max(ans, le[i-1] + ri[i]); } printf("%d\n", ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/LinKArftc/p/4961932.html