이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define x first
#define y second
ll N;
ll A[200005];
ll L[200005];
ll R[200005];
ll Del[200005];
int main(){
cin.tie(0) -> sync_with_stdio(false);
cin >> N;
priority_queue<pair<ll, ll>> PQ;
for(int i = 1; i <= N; i++){
cin >> A[i];
PQ.push({A[i], i});
L[i] = i - 1, R[i] = i + 1;
}
ll ans = 0;
for(int i = 1; i <= (N + 1) / 2; i++){
while(Del[PQ.top().y]) PQ.pop();
pair<ll, ll> T = PQ.top(); PQ.pop();
ans += T.x;
cout << ans << '\n';
Del[L[T.y]] = Del[R[T.y]] = 1;
A[T.y] = A[L[T.y]] + A[R[T.y]] - A[T.y];
L[T.y] = L[L[T.y]], R[T.y] = R[R[T.y]];
R[L[T.y]] = T.y, L[R[T.y]] = T.y;
PQ.push({A[T.y], T.y});
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |