#include <bits/stdc++.h>
#include <cassert>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef pair<short, short> pss;
#define MAX 201010
#define MAXS 2200
#define INF 1'000'000'010'000'000'000
#define bb ' '
#define ln '\n'
#define Ln '\n'
#define TC 1
#define MOD 1000000007
ll A[MAX];
ll S[MAX];
int chk[MAX];
ll xs[MAX];
signed main() {
ios::sync_with_stdio(false), cin.tie(0);
int N;
cin >> N;
int i;
priority_queue<pair<ll, pii>> pq;
set<int> s;
//for (i = 1; i <= N; i++) A[i] = rand();
for (i = 1; i <= N; i++) cin >> A[i];
for (i = 1; i <= N; i++) pq.emplace(A[i], pii(i, i)), S[i] = S[i - 1] + A[i], s.insert(i);
xs[1] = A[1];
xs[2] = A[2];
for (i = 3; i <= N; i += 2) xs[i] = xs[i - 2] + A[i];
for (i = 4; i <= N; i += 2) xs[i] = xs[i - 2] + A[i];
s.insert(0);
s.insert(N + 1);
ll x = 0;
ll sum = 0;
for (i = 1; i <= (N + 1) / 2; i++) {
int l, r;
l = pq.top().second.first;
r = pq.top().second.second;
ll v = pq.top().first;
pq.pop();
if (chk[l - 1] || chk[r + 1]) {
i--;
continue;
}
if (chk[l] || chk[r]) {
i--;
continue;
};
sum += v;
cout << sum << ln;
chk[l] = chk[r] = 1;
s.erase(l);
s.erase(r);
if (l == 1 || r == N) continue;
if (l - 2 >= 1 && chk[l - 2]) {
auto it = s.lower_bound(l - 2);
it--;
l = *it + 1;
}
if (r + 2 <= N && chk[r + 2]) {
auto it = s.lower_bound(r + 2);
r = *it - 1;
}
while (1) {
auto it = s.lower_bound(l);
if (it == s.end()) break;
if (*it <= r) {
s.erase(it);
continue;
}
break;
}
l--;
r++;
if (l <= 0) continue;
if (r > N) continue;
assert(!((l ^ r) & 1));
ll nv;
if (l > 1) nv = xs[r] - xs[l - 2] - (xs[r - 1] - xs[l - 1]);
else nv = xs[r] - (xs[r - 1] - xs[l - 1]);
pq.emplace(nv, pii(l, r));
}
}
Compilation message
candies.cpp: In function 'int main()':
candies.cpp:40:5: warning: unused variable 'x' [-Wunused-variable]
40 | ll x = 0;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2652 KB |
Output is correct |
2 |
Correct |
2 ms |
2648 KB |
Output is correct |
3 |
Correct |
2 ms |
2648 KB |
Output is correct |
4 |
Correct |
2 ms |
2652 KB |
Output is correct |
5 |
Correct |
3 ms |
2652 KB |
Output is correct |
6 |
Correct |
2 ms |
2652 KB |
Output is correct |
7 |
Correct |
2 ms |
2652 KB |
Output is correct |
8 |
Correct |
2 ms |
2652 KB |
Output is correct |
9 |
Correct |
2 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
2 ms |
2900 KB |
Output is correct |
12 |
Correct |
2 ms |
2652 KB |
Output is correct |
13 |
Correct |
2 ms |
2652 KB |
Output is correct |
14 |
Correct |
2 ms |
2652 KB |
Output is correct |
15 |
Correct |
2 ms |
2652 KB |
Output is correct |
16 |
Correct |
2 ms |
2652 KB |
Output is correct |
17 |
Correct |
2 ms |
2652 KB |
Output is correct |
18 |
Correct |
2 ms |
2652 KB |
Output is correct |
19 |
Correct |
2 ms |
2608 KB |
Output is correct |
20 |
Correct |
3 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2652 KB |
Output is correct |
2 |
Correct |
2 ms |
2648 KB |
Output is correct |
3 |
Correct |
2 ms |
2648 KB |
Output is correct |
4 |
Correct |
2 ms |
2652 KB |
Output is correct |
5 |
Correct |
3 ms |
2652 KB |
Output is correct |
6 |
Correct |
2 ms |
2652 KB |
Output is correct |
7 |
Correct |
2 ms |
2652 KB |
Output is correct |
8 |
Correct |
2 ms |
2652 KB |
Output is correct |
9 |
Correct |
2 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
2 ms |
2900 KB |
Output is correct |
12 |
Correct |
2 ms |
2652 KB |
Output is correct |
13 |
Correct |
2 ms |
2652 KB |
Output is correct |
14 |
Correct |
2 ms |
2652 KB |
Output is correct |
15 |
Correct |
2 ms |
2652 KB |
Output is correct |
16 |
Correct |
2 ms |
2652 KB |
Output is correct |
17 |
Correct |
2 ms |
2652 KB |
Output is correct |
18 |
Correct |
2 ms |
2652 KB |
Output is correct |
19 |
Correct |
2 ms |
2608 KB |
Output is correct |
20 |
Correct |
3 ms |
2652 KB |
Output is correct |
21 |
Correct |
185 ms |
19940 KB |
Output is correct |
22 |
Correct |
189 ms |
19900 KB |
Output is correct |
23 |
Correct |
185 ms |
19800 KB |
Output is correct |
24 |
Correct |
132 ms |
19836 KB |
Output is correct |
25 |
Correct |
131 ms |
19892 KB |
Output is correct |
26 |
Correct |
131 ms |
19948 KB |
Output is correct |
27 |
Correct |
140 ms |
20376 KB |
Output is correct |
28 |
Correct |
141 ms |
20668 KB |
Output is correct |
29 |
Correct |
141 ms |
19888 KB |
Output is correct |
30 |
Correct |
144 ms |
20472 KB |
Output is correct |
31 |
Correct |
144 ms |
19816 KB |
Output is correct |
32 |
Correct |
146 ms |
19888 KB |
Output is correct |
33 |
Correct |
155 ms |
19756 KB |
Output is correct |
34 |
Correct |
159 ms |
19740 KB |
Output is correct |
35 |
Correct |
155 ms |
19828 KB |
Output is correct |
36 |
Correct |
187 ms |
19936 KB |
Output is correct |
37 |
Correct |
186 ms |
20404 KB |
Output is correct |
38 |
Correct |
190 ms |
19892 KB |
Output is correct |
39 |
Correct |
129 ms |
20880 KB |
Output is correct |
40 |
Correct |
131 ms |
19756 KB |
Output is correct |
41 |
Correct |
129 ms |
20148 KB |
Output is correct |
42 |
Correct |
144 ms |
19740 KB |
Output is correct |
43 |
Correct |
142 ms |
19796 KB |
Output is correct |
44 |
Correct |
139 ms |
20120 KB |
Output is correct |
45 |
Correct |
144 ms |
20148 KB |
Output is correct |
46 |
Correct |
148 ms |
19896 KB |
Output is correct |
47 |
Correct |
144 ms |
20288 KB |
Output is correct |
48 |
Correct |
165 ms |
20680 KB |
Output is correct |
49 |
Correct |
159 ms |
19896 KB |
Output is correct |
50 |
Correct |
159 ms |
19780 KB |
Output is correct |