#include <bits/stdc++.h>
using namespace std;
#define ar array
#define sz(v) int(std::size(v))
using i64 = long long;
const int N = 200000;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
cin >> N;
vector<i64> A(N);
for (auto &x : A) cin >> x;
priority_queue<pair<i64, int>> paths;
vector<int> L(N), R(N), done(N);
for (int i = 0; i < N; i++) {
L[i] = i - 1, R[i] = i + 1;
paths.push({A[i], i});
}
int take = (N + 1) / 2;
i64 total_cost = 0;
for (int rep = 0; rep < take; rep++) {
while (sz(paths) && done[paths.top().second]) paths.pop();
assert(sz(paths));
auto [cost, i] = paths.top(); paths.pop();
total_cost += cost;
cout << total_cost << '\n';
A[i] = (L[i] < 0 ? 0 : A[L[i]]) + (R[i] >= N ? 0 : A[R[i]]) - A[i];
if (L[i] >= 0) {
done[L[i]] = 1;
int u = L[L[i]];
if (u >= 0) R[u] = i;
L[i] = u;
}
if (R[i] < N) {
done[R[i]] = 1;
int u = R[R[i]];
if (u < N) L[u] = i;
R[i] = u;
}
paths.push({A[i], i});
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |