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 int64_t
#define pb push_back
using pii=pair<int,int>;
const int lim=2e5+100;
int mod=1e9+7;
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#ifdef Local
freopen(".in","r",stdin);freopen(".out","w",stdout);
#endif
int n;
cin>>n;
int a[n],b[n];
for(int i=0;i<n;i++){cin>>a[i];b[i]=a[i];}
priority_queue<pii>pq;
for(int i=0;i<n;i++){
pq.push({a[i],i});
}
int cnt=0;
int pref[n];
pref[0]=a[0];
if(n)pref[1]=a[1];
for(int i=2;i<n;i++){
pref[i]=pref[i-2]+a[i];
}
auto f=[&](int l,int r){
return pref[r]-(1<l?pref[l-2]:int(0));
};
set<pii>st;
int ans=0;
for(int i=1;i<=(n+1)/2;i++){
while(pq.size()){
auto[val,ind]=pq.top();
pq.pop();
if(val!=b[ind])continue;
auto p=st.lower_bound({ind,ind}),q=(p==st.begin())?st.end():prev(p);
int l=ind,r=ind;
ans+=val;
if(p!=st.end()){
r=p->second;
st.erase(p);
}
if(q!=st.end()){
if(q->first<=ind&&ind<=q->second)continue;
l=q->first;
st.erase(q);
}
st.insert({l,r});
if(l){
int k=f(l,r)-((l+1<n)?f(l+1,r+1<n?r+1:r-1):0);
b[l-1]=a[l-1]-k;
pq.push({b[l-1],l-1});
}
if(r+1<n){
int k=f(l,r)-((0<=r-1)?f(0<=l-1?l-1:l+1,r-1):0);
b[r+1]=a[r+1]-k;
pq.push({b[r+1],r+1});
}
break;
}
cout<<ans<<"\n";
}
}
Compilation message (stderr)
candies.cpp: In function 'int main()':
candies.cpp:25:9: warning: unused variable 'cnt' [-Wunused-variable]
25 | int cnt=0;
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |