Submission #198787

#TimeUsernameProblemLanguageResultExecution timeMemory
198787red1108Candies (JOI18_candies)C++17
100 / 100
468 ms17688 KiB
#include<bits/stdc++.h>
#include<ext/rope>
using namespace std;
using namespace __gnu_cxx;
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false);cin.tie(0)
typedef long long ll;
typedef pair<ll,ll> pll;
const ll INF = 2e16;
const int inf = 2e9;
ll in[200010],ans;
int n,l[200010],r[200010];
set<pll> SET;
int main(){
	fastio;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>in[i];
		SET.insert(pll(-in[i],i));
		l[i]=i-1;r[i]=i+1;
	}
	for(int j=1;j<=(n+1)/2;j++){
		pll t = pll(SET.begin()->fi, SET.begin()->se);
		SET.erase(t);
		ans -= t.fi;
		int i = t.se;
		if(l[i]!=0&&r[i]!=n+1) in[i] = max(in[r[i]] + in[l[i]] - in[i],-INF);
		else in[i] = -INF;
		SET.insert(pll(-in[i], i));
		SET.erase(pll(-in[l[i]],l[i]));
		SET.erase(pll(-in[r[i]],r[i]));
		l[i] = l[l[i]];
		r[i] = r[r[i]];
		r[l[i]] = i;
		l[r[i]] = i;
		cout<<ans<<"\n";
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...