Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- int main()
- {
- int n;
- cin >> n;
- vector <int> v(n);
- for(int i=0; i<n; i++)
- cin >> v[i];
- int maxSum = 0, a = 0, b = 0, s = 0;
- while(b < n)
- {
- s += v[b];
- if(maxSum < s)
- maxSum = s;
- while (a <= b && s < 0)
- {
- s -= v[a];
- a++;
- }
- b++;
- }
- while(a < b)
- {
- s -= v[a];
- if(maxSum < s)
- maxSum = s;
- a++;
- }
- cout << maxSum << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment