This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
using namespace std;
#define int long long
const int MAXN = 1e6 + 10;
const int MOD = 998244353;
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x, int y) {
int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
int bm(int b, int p) {
if(p==0) return 1 % MOD;
int r = bm(b, p >> 1);
if(p&1) return (((r*r) % MOD) * b) % MOD;
return (r*r) % MOD;
}
int inv(int b) {
return bm(b, MOD-2);
}
int fastlog(int x) {
return (x == 0 ? -1 : 64 - __builtin_clzll(x) - 1);
}
void printcase(int i) { cout << "Case #" << i << ": "; }
struct node {
int rc,lc,val;
};
vector<node>vt;
void solve(int tc){
int n;
cin>>n;
int k=(n+1)/2;
int a[n+1];
for(int i=1;i<=n;i++)cin>>a[i];
vt.resize(n+2);
multiset<pair<int,int>>ms;
for(int i=1;i<=n;i++){
vt[i].lc=(i==1?-1:i-1);
vt[i].rc=(i==n?-1:i+1);
vt[i].val=a[i];
ms.insert({vt[i].val,i});
}
int ans[k+1];
ans[0]=0;
for(int i=1;i<=k;i++){
int idx=(*ms.rbegin()).second;
//cout<<"Move "<<i<<": "<<idx<<"\n";
ans[i]=ans[i-1]+(*ms.rbegin()).first;
ms.erase(*ms.rbegin());
int lb=-1,rb=-1;
if(vt[idx].lc!=-1 && vt[vt[idx].lc].lc!=-1) lb=vt[vt[idx].lc].lc;
if(vt[idx].rc!=-1 && vt[vt[idx].rc].rc!=-1) rb=vt[vt[idx].rc].rc;
//cout<<vt[idx].lc<<" "<<vt[idx].rc<<" "<<vt[idx].val<<"\n";
if(vt[idx].lc!=-1) {
ms.erase({vt[vt[idx].lc].val, vt[idx].lc});
vt[vt[idx].lc].rc=-1;
}
if(vt[idx].rc!=-1) {
ms.erase({vt[vt[idx].rc].val, vt[idx].rc});
vt[vt[idx].rc].lc=-1;
}
if(lb!=-1) {
vt[lb].rc=-1;
}
if(rb!=-1) {
vt[rb].lc=-1;
}
if(vt[idx].lc==-1 || vt[idx].rc==-1) continue;
int nw=0;
if(vt[idx].lc!=-1) nw+=vt[vt[idx].lc].val;
if(vt[idx].rc!=-1) nw+=vt[vt[idx].rc].val;
nw-=vt[idx].val;
if(lb!=-1) {
vt[lb].rc=idx;
}
if(rb!=-1) {
vt[rb].lc=idx;
}
vt[idx].lc=lb;
vt[idx].rc=rb;
vt[idx].val=nw;
ms.insert({vt[idx].val,idx});
}
for(int i=1; i<=k; i++) cout << ans[i] << "\n";
}
int32_t main() {
ios::sync_with_stdio(0); cin.tie(0);
int t = 1; //cin >> t;
for(int i=1; i<=t; i++) solve(i);
}
// 勿忘初衷
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |