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>
using namespace std;
using ll = long long;
#define Z 200'002
int N, K, L[Z], R[Z], r[Z];
ll A[Z], sum;
priority_queue<array<ll, 2>> pq;
int main() {
cin >> N;
for(int i = 1; i <= N; i++) {
cin >> A[i];
L[i] = i - 1, R[i] = i + 1;
pq.push({A[i], i});
}
R[N+1] = N+1, L[N+1] = N;
R[0] = r[0] = r[N+1] = 1;
for(int k = 1; k*2 <= N+1; k++) {
while(r[pq.top()[1]]) pq.pop();
auto [v, i] = pq.top(); pq.pop();
cout << (sum += v) << '\n';
r[i] = r[L[i]] || r[R[i]];
if(!r[L[i]]) v -= A[L[i]], r[L[i]] = 1, R[L[i] = L[L[i]]] = i;
if(!r[R[i]]) v -= A[R[i]], r[R[i]] = 1, L[R[i] = R[R[i]]] = i;
if(!r[i]) pq.push({A[i] = -v, i});
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |