제출 #1359309

#제출 시각아이디문제언어결과실행 시간메모리
1359309iamhereforfunCandies (JOI18_candies)C++20
0 / 100
1 ms580 KiB
// Starcraft 2 enjoyer //

#include <bits/stdc++.h>

// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")

using namespace std;

#define LSOne(X) ((X) & -(X))
#define int long long

const int N = 5e5 + 5;
const int M = 9e4 + 5;
const int B = 18;
const long long K = 2;
const int LG = 60;
const long long INF = 1e18 + 5;
const int C = 26;
const int MOD = 1e9 + 7;

int n, l[N], r[N];
long long a[N], cur;
bool del[N];
priority_queue<pair<int, int>> pq;

inline void solve()
{
    cin >> n;
    for (int x = 1; x <= n; x++)
    {
        cin >> a[x];
        pq.push({a[x], x});
    }
    for(int x = 0; x <= n + 1; x++)
    {
        l[x] = x - 1;
        r[x] = x + 1;
    }
    l[0] = 0;
    r[n + 1] = n + 1;
    cur = 0;
    for (int x = 1; x <= (n + 1) / 2; x++)
    {
        while (!pq.empty() && del[pq.top().second])
            pq.pop();
        auto [val, id] = pq.top();
        // cout << val << " " << id << "\n";
        pq.pop();
        cur += val;
        cout << cur << "\n";
        int ll = l[id], rr = r[id];
        del[ll] = 1;
        del[rr] = 1;
        a[id] = a[l[id]] + a[r[id]] - a[id];
        pq.push({a[id], id});
        l[id] = l[ll];
        r[id] = r[rr];
        r[l[id]] = id;
        l[r[id]] = id;
    }
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    // cin >> t;
    for (int x = 1; x <= t; x++)
    {
        solve();
    }
    return 0;
}
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…