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;
const int Z = 200'002;
const ll oo = 1e18;
/*
Take maximum value (say A[i]) for K = 1, then we have that we either
take A[i] or both A[i-1] and A[i+1]. So add a virtual element weighing
A[i-1] - A[i] + A[i+1] replacing these three elements. Now solve
inductively for (K - 1).
*/
int N, L[Z], R[Z];
ll A[Z], sum;
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> N;
priority_queue<array<ll, 2>> pq;
for(int i = 1; i <= N; i++) {
cin >> A[i];
L[i] = i - 1;
R[i] = i + 1;
pq.push({A[i], i});
}
A[0] = A[N+1] = oo;
R[N+1] = N+1;
R[0] = 1, L[N+1] = N;
for(int k = 1; k<=(N+1)/2; k++) {
while(A[pq.top()[1]] == oo) pq.pop();
auto [v, i] = pq.top(); pq.pop();
bool bad = !L[i] || R[i] > N;
cout << (sum += v) << '\n';
if(A[L[i]] != oo) v -= A[L[i]], A[L[i]] = oo;
if(A[R[i]] != oo) v -= A[R[i]], A[R[i]] = oo;
pq.push({A[i] = -v, i});
R[L[i] = L[L[i]]] = i;
L[R[i] = R[R[i]]] = i;
if(bad) A[i] = oo;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |