Submission #1256491

#TimeUsernameProblemLanguageResultExecution timeMemory
1256491namhhCandies (JOI18_candies)C++20
100 / 100
73 ms11460 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
#define fi first
#define se second
const int N = 2e5+5;
int n,a[N],l[N],r[N],check[N];
priority_queue<pii>pq;
//ga qua phai doc sol huhu :<
signed main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n;
	for(int i = 1; i <= n; i++) cin >> a[i];
	a[0] = -1e18;
	a[n+1] = -1e18;
	for(int i = 1; i <= n; i++){
		l[i] = i-1;
		r[i] = i+1;
		pq.push({a[i],i});
	}
	int ans = 0;
	for(int i = 1; i <= (n+1)/2; i++){
		while(pq.size() && check[pq.top().se]) pq.pop();
		int val = pq.top().fi;
		int idx = pq.top().se;
		pq.pop();
		ans += val;
		a[idx] = a[l[idx]]+a[r[idx]]-a[idx];
		pq.push({a[idx],idx});
		cout << ans << "\n";
		check[l[idx]] = 1;
		check[r[idx]] = 1;
		l[idx] = l[l[idx]];
		r[idx] = r[r[idx]];
		l[r[idx]] = idx;
		r[l[idx]] = idx;
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...