| # | Time | Username | Problem | Language | Result | Execution time | Memory |
|---|---|---|---|---|---|---|---|
| 1351878 | viettrung | Candies (JOI18_candies) | C++20 | 1 ms | 344 KiB |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<ll, ll>
#define pb push_back
#define f(i, a, b) for(int i = a; i <= b; i++)
#define fi first
#define se second
#define mll map<ll, ll>
#define sll set<ll>
const ll du = 1e9 + 7;
const ll ars = 2e5 + 5;
int n;
ll a[ars];
priority_queue<pll> q;
ll ans = 0;
bool vs[ars];
int L[ars], R[ars];
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define task "tenshi"
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];
L[i] = i - 1;
R[i] = i + 1;
q.push({a[i], i});
}
int T = (n + 1) / 2;
for(int t = 1; t <= T; t++){
auto[val, i] = q.top();
q.pop();
if(vs[i]){
t--;
continue;
}
ans += val;
cout << ans << '\n';
int l = L[i], r = R[i];
if(l >= 1) vs[l] = true;
if(r <= n) vs[r] = true;
int nl = (l >= 1 ? L[l] : 0);
int nr = (r <= n ? R[r] : n + 1);
if(l >= 1 && r <= n){
a[i] = a[l] + a[r] - a[i];
q.push({a[i], i});
}
L[i] = nl;
R[i] = nr;
if(nl >= 1) R[nl] = i;
if(nr <= n) L[nr] = i;
}
}
Compilation message (stderr)
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
