# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1153182 | mingga | Candies (JOI18_candies) | C++20 | 99 ms | 10076 KiB |
#include "bits/stdc++.h"
using namespace std;
#define ln "\n"
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define int long long
const int mod = 1e9 + 7;
const int inf = 2e18;
const int N = 2e5 + 7;
int n, a[N], l[N], r[N];
bool del[N];
signed main() {
cin.tie(0) -> sync_with_stdio(0);
#define task ""
if(fopen(task ".INP", "r")) {
freopen(task ".INP", "r", stdin);
freopen(task ".OUT", "w", stdout);
}
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
priority_queue<pair<int, int>> q;
for(int i = 1; i <= n; i++) {
l[i] = i - 1;
r[i] = i + 1;
q.push({a[i], i});
}
int cur = 0;
a[0] = a[n + 1] = -inf;
for(int i = 1; i <= (n + 1) / 2;) {
auto x = q.top(); q.pop();
int val = x.fi, id = x.se;
if(del[id]) continue;
cur += val;
cout << cur << ln;
int lef = l[id], rig = r[id];
del[lef] = 1, del[rig] = 1;
int new_val = a[lef] + a[rig] - a[id];
r[id] = r[rig];
l[r[rig]] = id;
l[id] = l[lef];
r[l[lef]] = id;
a[id] = new_val;
q.push({new_val, id});
i++;
}
cerr << "\nTime: " << clock() * 1000 / CLOCKS_PER_SEC;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |