#include <bits/stdc++.h>
using namespace std;
const int N = 100000;
long long n,a[N*3 + 10],nxt[N*3 + 10],off[N*3+10];
int main() {
scanf("%lld",&n);
for(int i = 1;i<=n;i++){
off[i] = 0;
scanf("%lld",&a[i]);
nxt[i] = i + 1;
}
int idx = 1;
while(idx<=n){
if(nxt[idx] <= n && a[nxt[idx]] <= 0 && a[idx] <= 0){
a[idx] += a[nxt[idx]];
nxt[idx] = nxt[nxt[idx]];
}else{
idx = nxt[idx];
}
}
vector<long long> res;
int tmp = 1;
while(tmp <= n){
res.push_back(a[tmp]);
// printf("%d ",a[tmp]);
tmp = nxt[tmp];
}
tmp = res.size();
for(int i = res.size() - 1; i>=0;i--){
if(res[i] == 0){
tmp = i;
}else{
break;
}
}
idx = tmp - 1;
while(idx > 0){
if(idx - 1 >= 0 && res[idx] < 0){
res[idx-1] += res[idx];
off[idx] = 1;
}
idx--;
}
for(int i = 0;i<tmp;i++){
if(off[i] == 0)
printf("%lld ",res[i]);
}
return 0;
}
Copy