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 long long
#define FastIO ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORR(i,a,b) for(int i=a;i>=b;i--)
#define pb push_back
#define ALL(v) v.begin(),v.end()
#define lg 31
#define mxn 100005
set<pair<int,int>> mx, st;
void erase(pair<int,int> p){
mx.erase(mx.find({p.second,p.first}));
st.erase(st.find(p));
}
int f(int cur){
if (st.size()==0) return 0;
if (st.size()==1){
cout<<(*st.begin()).second+cur<<"\n";
return (*st.begin()).second;
}
auto it=--mx.end();
pair<int,int> tmp=*it;
auto it2=st.find({tmp.second,tmp.first});
int res=tmp.first;
if (it2==st.begin()){
erase(*(it2++));
} else if (it2==--st.end()){
erase(*(it2--));
} else{
int x=((*(--it2)).second)-((*(++it2)).second)+((*(++it2)).second);
erase(*(it2--));
erase(*(it2--));
mx.insert({x,tmp.second});
st.insert({tmp.second,x});
}
erase(*it2);
cout<<res+cur<<"\n";
res+=f(res+cur);
return res;
}
signed main(){
FastIO
int tc=1;
// cin >> tc;
while (tc--){
int n;
cin>>n;
FOR(i,1,n){
int t;cin>>t;
st.insert({i,t});
mx.insert({t,i});
}
f(0);
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |