Hackerrank 101 Hack Jan 2016 Max-Sum-Subarray

,

Max-Sum-Subarray

#include <iostream>
#include <vector>
#define repeat(i,n) for (int i = 0; (i) < (n); ++(i))
using namespace std;
int main() {
    int n; cin >> n;
    vector<int> a(n); repeat (i,n) cin >> a[i];
    int ans = 0;
    int prv = 0;
    repeat (i,n) {
        if (a[i] == 0) {
            prv = 0;
        } else {
            prv = a[i] + max(prv, 0);
            ans = max(ans, prv);
        }
    }
    cout << ans << endl;
    return 0;
}