Submission #654964

#TimeUsernameProblemLanguageResultExecution timeMemory
654964onlk97Candies (JOI18_candies)C++14
100 / 100
210 ms21184 KiB
#include <bits/stdc++.h> #define int long long using namespace std; signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n; cin>>n; long long a[n+2]; a[0]=-1e18; a[n+1]=-1e18; for (int i=1; i<=n; i++) cin>>a[i]; long long l[n+2],r[n+2]; for (int i=1; i<=n; i++){ l[i]=i-1; r[i]=i+1; } l[0]=0; r[n+1]=n+1; bool used[n+2]; for (int i=0; i<=n+1; i++) used[i]=0; set <pair <long long,int>,greater <pair <long long,int> > > s; for (int i=1; i<=n; i++) s.insert(make_pair(a[i],i)); long long ans=0; for (int iters=1; iters<=(n+1)/2; iters++){ die:; pair <long long,int> tp=*s.begin(); s.erase(s.begin()); if (used[tp.second]) goto die; ans+=tp.first; cout<<ans<<'\n'; int ll=l[tp.second],rr=r[tp.second]; a[tp.second]=a[ll]+a[rr]-a[tp.second]; l[tp.second]=l[ll]; r[tp.second]=r[rr]; used[ll]=1; used[rr]=1; r[l[tp.second]]=tp.second; l[r[tp.second]]=tp.second; if (ll>=1&&rr<=n) s.insert(make_pair(a[tp.second],tp.second)); } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...