# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
202254 | quocnguyen1012 | Candies (JOI18_candies) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 5;
const ll linf = 1e18;
int N;
ll a[maxn]; int prf[maxn], nxt[maxn];
priority_queue<pair<ll, int>> pq;
bool del(int idx)
{
prf[nxt[idx]] = prf[idx];
nxt[prf[idx]] = nxt[idx];
}
signed main(void)
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen("A.INP", "r")){
freopen("A.INP", "r", stdin);
freopen("A.OUT", "w", stdout);
}
cin >> N;
for (int i = 1; i <= N; ++i){
cin >> a[i];
prf[i] = i - 1;
nxt[i] = i + 1;
pq.push(mp(a[i], i));
}
ll res = 0;
for (int i = 1; i <= (N + 1) / 2; ++i){
while (pq.size() && a[pq.top().se] != pq.top().fi) pq.pop();
auto now = pq.top(); pq.pop();
res += now.fi;
cout << res << '\n';
int pos = now.se;
a[pos] = max(-inf, a[prf[pos]] + a[nxt[pos]] - a[pos]);
int L = prf[pos], R = nxt[pos];
del(L); del(R);
a[L] = a[R] = -inf;
pq.push(mp(a[pos], pos));
}
}