分析:当现在拥有的钱超过欠的钱时,立即回头还钱
Code:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int main() {
int n;
while(~scanf("%d", &n)) {
int h = 0, benefit = 0, debt = 0, ans = n;
for(int i = 1; i <= n; i++) {
int x;
scanf("%d", &x);
if(x > 0) benefit += x;
else {
if(debt == 0) h = i;
debt -= x;
}
if(benefit >= debt && debt > 0) {
ans += 2*(i-h);
benefit -= debt;
debt = 0;
}
}
printf("%d\n", ans);
}
return 0;
}