Submission #1364063

#TimeUsernameProblemLanguageResultExecution timeMemory
1364063AmirAli_H1Candies (JOI18_candies)C++20
100 / 100
63 ms9132 KiB
#include <bits/stdc++.h>
using namespace std;

typedef		long long int			ll;
typedef		long double				ld;
typedef		pair<int, int>			pii;
typedef		pair<ll, ll>			pll;
typedef		pair<ld, ld>			pld;
typedef		complex<ld>				cld;

#define		all(x)					(x).begin(),(x).end()
#define		len(x)					((ll) (x).size())
#define		F						first
#define		S						second
#define		X						real()
#define		Y						imag()
#define		pb						push_back
#define		sep						' '
#define		endl					'\n'
#define		Mp						make_pair
#define		kill(x)					cout << x << '\n', exit(0)
#define		set_dec(x)				cout << fixed << setprecision(x);
#define		file_io(x,y)			freopen(x, "r", stdin); freopen(y, "w", stdout);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int maxn = (1 << 20) + 4;

int n; ll A[maxn];
int L[maxn], R[maxn];
priority_queue<pll> qu;
int mark[maxn];

void solve() {
	cin >> n;
	for (int i = 0; i < n; i++) cin >> A[i];
	for (int i = 0; i < n; i++) {
		L[i] = i - 1; R[i] = i + 1;
		qu.push(Mp(A[i], i)); mark[i] = 1;
	}
	ll ans = 0;
	for (int T = 0; T < (n + 1) / 2; T++) {
		while (!qu.empty()) {
			auto f = qu.top();
			if (!mark[f.S]) qu.pop();
			else break;
		}
		auto f = qu.top(); qu.pop();
		ans += f.F; ll Rx = -f.F;
		int j = f.S, tx = 0;
		if (L[j] != -1) {
			Rx += A[L[j]]; tx++;
			mark[L[j]] = 0;
			if (L[L[j]] != -1) R[L[L[j]]] = j;
			L[j] = L[L[j]];
		}
		if (R[j] != n) {
			Rx += A[R[j]]; tx++;
			mark[R[j]] = 0;
			if (R[R[j]] != n) L[R[R[j]]] = j;
			R[j] = R[R[j]];
		}
		if (tx == 2) {
			A[j] = Rx; qu.push(Mp(A[j], j));
		}
		else {
			mark[j] = 0;
			if (L[j] != -1) R[L[j]] = n;
			if (R[j] != n) L[R[j]] = -1;
		}
		cout << ans << endl;
	}
}

int main() {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

	int T = 1;
	while (T--) {
		solve();
	}

	return 0;
}
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...