Submission #875353

#TimeUsernameProblemLanguageResultExecution timeMemory
8753531075508020060209tcCandies (JOI18_candies)C++14
100 / 100
506 ms38032 KiB
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n;
int ar[500005];
int ps[2][500005];
set<pair<int,pair<int,int>>>stv[2];
set<pair<pair<int,int>,int>>st[2];
signed main()
{
    cin>>n;
    ar[0]=-1e16;ar[n+1]=-1e16;
    for(int i=1;i<=n;i++){
        cin>>ar[i];
    }
    ps[0][0]=ar[0];
    for(int i=1;i<=n+1;i++){
        ps[0][i]=ps[0][i-1];
        ps[1][i]=ps[1][i-1];
        ps[i%2][i]+=ar[i];
    }
    int ans=0;
    for(int i=0;i<=n+1;i++){
        stv[i%2].insert({ar[i],{i,i}});
        st[i%2].insert({{i,i},ar[i]});
    }
    for(int ttt=1;ttt<=(n+1)/2;ttt++){
        int v0=(*prev(stv[0].end())).first;
        int v1=(*prev(stv[1].end())).first;

        if(v0>v1){
            ans+=v0;
            int ogl=(*prev(stv[0].end())).second.first;
            int ogr=(*prev(stv[0].end())).second.second;
            stv[0].erase({v0,{ogl,ogr}});
            st[0].erase({{ogl,ogr},v0});
            int l;int r;
            auto it=st[1].lower_bound({{ogl,-1},-1000});
            int a;int b;int c;//{ {l,r},v }
            a=(*it).first.first;
            b=(*it).first.second;
            c=(*it).second;
            r=b;
            it--;
            st[1].erase({{a,b},c});
            stv[1].erase({c,{a,b}});
            a=(*it).first.first;
            b=(*it).first.second;
            c=(*it).second;
            l=a;
            st[1].erase({{a,b},c});
            stv[1].erase({c,{a,b}});
            int v=(ps[1][r]-ps[1][l-1])-(ps[0][r]-ps[0][l-1]);
            st[1].insert({{l,r},v});
            stv[1].insert({v,{l,r}});
        }else{
            //continue;
            ans+=v1;
            int ogl=(*prev(stv[1].end())).second.first;
            int ogr=(*prev(stv[1].end())).second.second;
            stv[1].erase({v1,{ogl,ogr}});
            st[1].erase({{ogl,ogr},v1});

            int l;int r;
            auto it=st[0].upper_bound({{ogl,-1},-100 });
            int a;int b;int c;//{ {l,r},v }
            a=(*it).first.first;
            b=(*it).first.second;
            c=(*it).second;
            r=b;

            it--;

            st[0].erase({{a,b},c});
            stv[0].erase({c,{a,b}});
            a=(*it).first.first;
            b=(*it).first.second;
            c=(*it).second;
            l=a;

            st[0].erase({{a,b},c});
            stv[0].erase({c,{a,b}});
            //cout<<l<<" "<<r<<"\n";
            int v=(ps[0][r]-ps[0][l-1])-(ps[1][r]-ps[1][l-1]);
           // cout<<"hehe";
            st[0].insert({{l,r},v});
            stv[0].insert({v,{l,r}});

        }

       cout<<ans<<"\n";

    }


}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...