Source Code
//fold
#include <bits/stdc++.h>
#define endl '\n'
#define mp make_pair
#define tostr(x) static_cast<ostringstream&>((ostringstream()<<dec<<x)).str()
#define rep(i,begin,end) for(auto i = begin;i < end;i++)
#define repr(i,begin,end) for(auto i = begin-1;i >= end;i--)
#define pb push_back
#define sz(a) ((int)(a).size())
#define fi first
#define se second
#define abs(a) ((a) < (0) ? (-1)*(a) : (a))
#define SQ(a) ((a)*(a))
#define eqd(a,b) (abs(a-b)<1e-9)
#define X real()
#define Y imag()
using namespace std;
typedef long long ll;
typedef long double ld;
template <typename t> t in(t q){cin >> q;return q;}
template <typename T> ostream& operator<<(ostream& os, const vector<T>& v){os << "[";for (int i = 0; i < sz(v); ++i) { os << v[i]; if (i != sz(v) - 1) os << ",";}os << "]";return os;}
template <typename T, typename S>ostream& operator<<(ostream& os, const map<T, S>& v){for (auto it : v)os << "(" << it.first << ":" << it.second << ")";return os;}
template <typename T, typename S>ostream& operator<<(ostream& os, const pair<T, S>& v){os << "(" << v.first << "," << v.second << ")";return os;}
const long double PI = acosl(-1);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng64(chrono::steady_clock::now().time_since_epoch().count());
inline int rand(int l,int r){return uniform_int_distribution<int>(l, r)(rng);}
inline ll rand(ll l,ll r){return uniform_int_distribution<ll>(l, r)(rng64);}
//endfold
#define  N  (200'005)
#define MOD (1000000000ll + 7ll)
#define OO (1050000000)
#define OOL (1100000000000000000)

//global

vector<ll> solve_pos(vector<ll> v){
	stack<ll> s;
	for(int i = 0; i < sz(v)-1; i++){
		while(!s.empty() && s.top() > v[i]){
			v[sz(v)-1] += s.top();
			s.pop();
		}
		s.push(v[i]);
	}
	s.push(v.back());
	vector<ll> res;
	while(!s.empty()){
		res.push_back(s.top()); s.pop();
	}
	reverse(res.begin(), res.end());
	while(sz(res) > 1 && res.back() == 0) res.pop_back();
	return res;
}

int main(){
	//fold
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cout << setprecision(10);
	//endfold
	int n;
	cin >> n;
	vector<ll> v;
	ll ea = n;
	rep(i,0,n){
		v.push_back(in(1ll));
		if(v[i] == 0){
			ea = min(1ll*i, ea);
		}
	}
	vector<ll> sm;
	set<ll> r;
	ll tot = 0;
	rep(i,0,n){
		if(v[i] < 0){
			sm.push_back(i);
			tot += v[i];
			r.insert(i);
		}
	}
	if(sz(sm) == 0){
		vector<ll> res = solve_pos(v);
		for(auto e: res) cout << e << " ";
		return 0;
	}
	vector<ll> w;
	ll del = 0;
	rep(i,0,n){
		if(i < min(sm[0],ea)){
			del += v[i];
		}else if(r.count(i)){
			continue;
		}else if(!(i == ea && ea < sm[0])){
			w.push_back(v[i]);
		}
	}
	if(sz(w) > 0){
		w.back() += del;
		vector<ll> res = solve_pos(w);
		/*if(sz(res) == 1 && res[0] == 0){
			cout << tot << endl;
			return 0;
		}*/
		cout << tot << " ";
		for(auto e: res) cout << e << " ";
		return 0;
	}
	if(del == 0){
		cout << tot << " ";
		return 0;
	}
	rep(i,0,n){
		if(i < min(sm[0],ea)){
			w.push_back(v[i]);
		}
	}
	vector<ll> res = solve_pos(w);
	res[0] += tot;
	for(auto e: res) cout << e << " ";
	return 0;
}
Copy
a Adhami
GNU G++17
0 ms
772 KB
Wrong Answer