제출 #1174907

#제출 시각아이디문제언어결과실행 시간메모리
1174907browntoadCandies (JOI18_candies)C++20
100 / 100
168 ms19448 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template<typename T>
using pbds_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define ll long long
#define int ll
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define RREP1(i, n) for (int i = (n); i >= 1; i--)
#define REP1(i, n) FOR(i, 1, n+1)
#define pii pair<int, int>
#define ppi pair<pii, int>
#define pip pair<int, pii>
#define f first
#define s second
#define pb push_back
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) (int)((x).size())
#define endl '\n'
#define IOS() ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)

const ll maxn = 5e5+5;
const ll mod = 1e9+7;
const ll inf = (1ll<<60);
const int iinf = 1e9+5;

ll pw(ll x, ll p, ll m){
    ll ret = 1;
    while(p > 0){
        if (p & 1){
            ret *= x;
            ret %= m;
        }
        x *= x;
        x %= m;
        p >>= 1;
    }
    return ret;
}
ll inv(ll x, ll m){
    return pw(x, m-2, m);
}

vector<int> solve(vector<int> &vc){
    int n = SZ(vc);

    vector<int> vals(n), non(n);
    set<int> ids;
    priority_queue<pii> pq; // val, id
    REP(i, n){
        ids.insert(i);
        vals[i] = vc[i];
        pq.push({vc[i], i});
    }

    vector<int> ans;
    int cur = 0;
    while(SZ(ids)){
        auto [v, id] = pq.top(); pq.pop();
        if (non[id]) continue;
        cur += v;
        ans.pb(cur);
        auto it = ids.find(id);
        if (it != ids.begin() && it != prev(ids.end())){
            int lid = (*prev(it)), rid = (*next(it));
            non[lid] = non[rid] = 1;
            ids.erase(lid); ids.erase(rid);
            vals[id] = vals[lid] + vals[rid] - vals[id];
            pq.push({vals[id], id});
        }
        else{
            non[id] = 1;
            if (it != ids.begin()){
                int lid = (*prev(it));
                non[lid] = 1;
                ids.erase(lid);
            }
            if (it != prev(ids.end())){
                int rid = (*next(it));
                non[rid] = 1;
                ids.erase(rid);
            }
            ids.erase(id);
        }
    }
    return ans;
}
signed main(){
    IOS();
    int n; cin>>n;
    vector<int> vc(n);
    REP(i, n) cin>>vc[i];
    vector<int> ans = solve(vc);

    for (auto x:ans) cout<<x<<endl;
}
/*
3 1
2 1 3
2 4 4
2 1 1
2
2

*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...