Advertisement
Spidey2182

Remove from ends (C++)

Jun 7th, 2023
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.35 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.     ios::sync_with_stdio(false);
  7.     cin.tie(0);
  8.  
  9.     int n;
  10.     cin >> n;
  11.     vector<int> a(n);
  12.     for (int i = 0; i < n; i++)
  13.         cin >> a[i];
  14.  
  15.     int64_t ans = INT_MIN, sum = 0;
  16.     for (int x : a) {
  17.         sum += x;
  18.         ans = max(ans, sum);
  19.         if (sum < 0)
  20.             sum = 0;
  21.     }
  22.  
  23.     cout << ans;
  24.     return 0;
  25. }
  26.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement