#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 n,A[N],pre[N],nxt[N],ans;
ll Ceil(ll a,ll b){return (a+b-1)/b;}
priority_queue<pll> pq;
bitset<N> exist;
void Delete(ll i){
if(!i) return ;
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;
rep(o,1,Ceil(n,2)){
while(!exist[pq.top().ss]) pq.pop();
ll v=pq.top().ff,i=pq.top().ss;pq.pop();
ans+=v;
if(pre[i]&&nxt[i]){
A[i]=A[pre[i]]+A[nxt[i]]-A[i];
pq.push(mp(A[i],i));
}else Delete(i);
Delete(pre[i]);
Delete(nxt[i]);
cout<<ans<<'\n';
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |