Submission #914047

#TimeUsernameProblemLanguageResultExecution timeMemory
914047amirhoseinfar1385Candies (JOI18_candies)C++17
0 / 100
2 ms600 KiB
#include<bits/stdc++.h> using namespace std; long long inf=1e15; int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n; cin>>n; vector<int>all(n); set<int>allind; set<pair<int,int>>st; for(long long i=0;i<n;i++){ cin>>all[i]; st.insert(make_pair(all[i],i)); allind.insert(i); } long long res=0; while((int)allind.size()>0){ auto x=*st.rbegin(); st.erase(x); res+=x.first; cout<<res<<"\n"; if((int)allind.size()==1){ break; } if((*allind.begin())==x.second){ allind.erase(*allind.begin()); st.erase(make_pair(all[*allind.begin()],*allind.begin())); allind.erase(*allind.begin()); continue; } if((*allind.rbegin())==x.second){ allind.erase(*allind.rbegin()); st.erase(make_pair(all[*allind.rbegin()],*allind.rbegin())); allind.erase(*allind.rbegin()); continue; } allind.erase(x.second); auto y=allind.lower_bound(x.second); int nx=*y; y--; int ps=*y; st.erase(make_pair(all[nx],nx)); st.erase(make_pair(all[ps],ps)); allind.erase(nx); allind.erase(ps); all[x.second]=all[nx]+all[ps]-all[x.second]; st.insert(make_pair(all[x.second],x.second)); allind.insert(x.second); } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...