#include<bits/stdc++.h>
#define rep(a,b,c) for(ll a=b;a<=c;++a)
#define ll long long
#define ff first
#define ss second
#define mp make_pair
using namespace std;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll N=2e5+5,inf=1e18;
ll Ceil(ll a,ll b){return (a+b-1)/b;}
ll n,A[N],pre[N],nxt[N],cur;
priority_queue<pll> pq;
bitset<N> exist;
vector<ll> ans;
void Delete(ll i){
nxt[pre[i]]=nxt[i];
pre[nxt[i]]=pre[i];
exist[i]=0;
}
signed main(){
ios::sync_with_stdio(0);cin.tie(0);
cin>>n;
rep(i,1,n) cin>>A[i],pq.push(mp(A[i],i));
rep(i,1,n) pre[i]=i-1,nxt[i]=i+1,exist[i]=1;
nxt[n]=0;
while(ans.size()<Ceil(n,2)){
ll v=pq.top().ff,i=pq.top().ss;pq.pop();
if(!exist[i]) continue;
cur+=v;
if(pre[i]&&nxt[i]){
A[i]=A[nxt[i]]+A[pre[i]]-A[i];
pq.push(mp(A[i],i));
Delete(pre[i]);
Delete(nxt[i]);
}
ans.push_back(cur);
}
for(ll i:ans) cout<<i<<'\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |