Submission #967009

#TimeUsernameProblemLanguageResultExecution timeMemory
96700912345678Candies (JOI18_candies)C++17
100 / 100
85 ms14276 KiB
#include <bits/stdc++.h>

using namespace std;

#define ll long long

const int nx=2e5+5;

ll n, a[nx], used[nx], l[nx], r[nx], res;
priority_queue<pair<ll, ll>> pq;

void erase(int u)
{
    used[u]=1;
    if (l[u]!=0) r[l[u]]=r[u];
    if (r[u]!=n+1) l[r[u]]=l[u];
}

int main()
{
    cin.tie(NULL)->sync_with_stdio(false);
    cin>>n;
    for (int i=1; i<=n; i++) cin>>a[i], pq.push({a[i], i}), l[i]=i-1, r[i]=i+1;
    for (int i=1; i<=(n+1)/2; i++)
    {
        while (!pq.empty()&&used[pq.top().second]) pq.pop();
        if (pq.empty()) break;
        auto [cur, u]=pq.top();
        //cout<<cur<<' '<<u<<'\n';
        pq.pop();
        res+=cur;
        if (l[u]!=0&&r[u]!=n+1) pq.push({a[l[u]]+a[r[u]]-cur, u}), a[u]=a[l[u]]+a[r[u]]-cur;
        else erase(u);
        if (l[u]!=0) erase(l[u]);
        if (r[u]!=n+1) erase(r[u]);
        cout<<res<<'\n';
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...