-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrange-sum.cpp
36 lines (29 loc) · 918 Bytes
/
range-sum.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
Given an array of n integers (different from zero)
Fin de maximum range sum
A = {4, -2, 40}
The max range sum of A is (0, n-1) itself.
*/
#include <iostream>
#include <vector>
using namespace std;
// If all numbers if non-negative, the max range sum is sum of all elements of A.
// If there's a negative, it can be in range if the adjancents values ...positive + negative > 0
int main() {
int a, b, s;
// the number case tests
cin >> b;
for(int j = 1; j <= b; ++j) {
// the num of elements
cin >> s;
int ans = 0, sum = 0;
for(int i = 1; i <= s; ++i) {
cin >> a;
sum += a;
ans = max(ans, sum); // storage the maximum sum
sum = max(0, sum); // if sum decrease, then it's no belong more to maximum range sum
}
cout << ans << endl;
}
return 0;
}