Submission #914806

#TimeUsernameProblemLanguageResultExecution timeMemory
914806a_l_i_r_e_z_aCandies (JOI18_candies)C++17
100 / 100
218 ms19536 KiB
// In the name of God
// Hope is last to die

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;

#define pb push_back
#define S second
#define F first
#define mp make_pair
#define smax(x, y) (x) = max((x), (y))
#define smin(x, y) (x) = min((x), (y))
#define all(x) (x).begin(), (x).end()
#define len(x) ((int)(x).size())

const int maxn = 2e5 + 5; 
const int inf = 1e9 + 7;
int n, prv[maxn], nxt[maxn];
ll val[maxn];
set<pll> st;

int32_t main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    cin >> n;
    for(int i = 1; i <= n; i++){
        int x; cin >> x;
        st.insert(mp(x, i));
        val[i] = x;
        prv[i] = i - 1;
        nxt[i] = i + 1;
    }
    ll ans = 0;
    while(st.size()){
        ll v = (*st.rbegin()).F, i = (*st.rbegin()).S;
        ans += v;
        cout << ans << '\n';
        st.erase(mp(v, i));
        prv[nxt[i]] = prv[i];
        nxt[prv[i]] = nxt[i]; 
        if(st.size() == 0) break;
        if(nxt[i] == n + 1){
            st.erase(mp(val[prv[i]], prv[i]));
            nxt[prv[prv[i]]] = nxt[i];
        }
        else if(prv[i] == 0){
            st.erase(mp(val[nxt[i]], nxt[i]));
            prv[nxt[nxt[i]]] = prv[i];
        }
        else{
            st.erase(mp(val[prv[i]], prv[i]));
            st.erase(mp(val[nxt[i]], nxt[i]));
            nxt[prv[prv[i]]] = i;
            prv[nxt[nxt[i]]] = i;
            val[i] = val[prv[i]] + val[nxt[i]] - val[i];
            nxt[i] = nxt[nxt[i]];
            prv[i] = prv[prv[i]];
            st.insert(mp(val[i], i));
        }
    }

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...