#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pli = pair<ll, int>;
#define ff first
#define ss second
int L[200005], R[200005];
ll A[200005], ans;
bool used[200005];
priority_queue<pli> pq;
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int N; cin >> N;
for (int i = 1; i <= N; ++i) cin >> A[i], pq.emplace(A[i], i);
iota(L + 1, L + N + 1, 0);
iota(R + 1, R + N + 1, 2);
for (int i = 0; i < (N + 1 >> 1); ++i) {
while (used[pq.top().ss]) pq.pop();
int x = pq.top().ss; pq.pop();
used[L[x]] = used[R[x]] = 1;
ans += A[x];
A[x] = A[L[x]] + A[R[x]] - A[x];
L[x] = L[L[x]];
R[L[x]] = x;
R[x] = R[R[x]];
L[R[x]] = x;
cout << ans << '\n';
if (L[x] && R[x]) pq.emplace(A[x], x);
}
return 0;
}
Compilation message
candies.cpp: In function 'int main()':
candies.cpp:20:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
for (int i = 0; i < (N + 1 >> 1); ++i) {
~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
512 KB |
Output is correct |
2 |
Correct |
3 ms |
512 KB |
Output is correct |
3 |
Correct |
2 ms |
512 KB |
Output is correct |
4 |
Correct |
3 ms |
512 KB |
Output is correct |
5 |
Incorrect |
3 ms |
512 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
512 KB |
Output is correct |
2 |
Correct |
3 ms |
512 KB |
Output is correct |
3 |
Correct |
2 ms |
512 KB |
Output is correct |
4 |
Correct |
3 ms |
512 KB |
Output is correct |
5 |
Incorrect |
3 ms |
512 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |