#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
struct Pt {
int l, r; ll sum;
bool operator <(const Pt &other) const {
if (sum != other.sum) return sum > other.sum;
return l < other.l;
}
};
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.setf(ios::fixed); cout.precision(20);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int n;
cin >> n;
vector <int> a(n);
for (int i = 0; i < n; ++i) cin >> a[i];
vector <ll> pref(n + 1);
for (int i = 0; i < n; ++i) pref[i + 1] = pref[i] + a[i];
auto getSum = [&] (int l, int r) {
return pref[r + 1] - pref[l];
};
set <Pt> q;
for (int i = 0; i < n; ++i) q.insert({i, i, a[i]});
vector <int> mL(n), mR(n);
for (int i = 0; i < n; ++i) {
mL[i] = i;
mR[i] = i;
}
map <pair <int, int>, ll> sum;
for (int i = 0; i < n; ++i) sum[{i, i}] = a[i];
ll ans = 0;
for (int i = 1; i <= (n + 1) / 2; ++i) {
auto cur = *q.begin();
q.erase(q.begin());
ans += cur.sum;
cout << ans << '\n';
int cl = cur.l, cr = cur.r;
mR[cl] = mL[cr] = -1;
ll cs = -cur.sum;
int x = cur.r + 1;
if (x < n) {
int tl = x;
int tr = mR[tl];
if (tr != -1) {
ll ts = sum[{tl, tr}];
q.erase({tl, tr, ts});
cr = tr;
cs += ts;
mR[tl] = mL[tr] = -1;
}
}
x = cur.l - 1;
if (x >= 0) {
int tr = x;
int tl = mL[tr];
if (tl != -1) {
ll ts = sum[{tl, tr}];
q.erase({tl, tr, ts});
cl = tl;
cs += ts;
mR[tl] = mL[tr] = -1;
}
}
if (cl != cur.l && cr != cur.r) {
mR[cl] = cr;
mL[cr] = cl;
sum[{cl, cr}] = cs;
q.insert({cl, cr, cs});
}
}
}
Compilation message
candies.cpp: In function 'int main()':
candies.cpp:28:10: warning: variable 'getSum' set but not used [-Wunused-but-set-variable]
auto getSum = [&] (int l, int r) {
^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
760 KB |
Output is correct |
2 |
Correct |
4 ms |
632 KB |
Output is correct |
3 |
Correct |
4 ms |
632 KB |
Output is correct |
4 |
Correct |
4 ms |
632 KB |
Output is correct |
5 |
Correct |
4 ms |
632 KB |
Output is correct |
6 |
Correct |
4 ms |
632 KB |
Output is correct |
7 |
Correct |
4 ms |
632 KB |
Output is correct |
8 |
Correct |
4 ms |
632 KB |
Output is correct |
9 |
Correct |
4 ms |
632 KB |
Output is correct |
10 |
Correct |
4 ms |
632 KB |
Output is correct |
11 |
Correct |
2 ms |
760 KB |
Output is correct |
12 |
Correct |
4 ms |
632 KB |
Output is correct |
13 |
Correct |
4 ms |
632 KB |
Output is correct |
14 |
Correct |
4 ms |
632 KB |
Output is correct |
15 |
Correct |
5 ms |
632 KB |
Output is correct |
16 |
Correct |
4 ms |
636 KB |
Output is correct |
17 |
Correct |
5 ms |
632 KB |
Output is correct |
18 |
Correct |
4 ms |
632 KB |
Output is correct |
19 |
Correct |
4 ms |
632 KB |
Output is correct |
20 |
Correct |
4 ms |
632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
760 KB |
Output is correct |
2 |
Correct |
4 ms |
632 KB |
Output is correct |
3 |
Correct |
4 ms |
632 KB |
Output is correct |
4 |
Correct |
4 ms |
632 KB |
Output is correct |
5 |
Correct |
4 ms |
632 KB |
Output is correct |
6 |
Correct |
4 ms |
632 KB |
Output is correct |
7 |
Correct |
4 ms |
632 KB |
Output is correct |
8 |
Correct |
4 ms |
632 KB |
Output is correct |
9 |
Correct |
4 ms |
632 KB |
Output is correct |
10 |
Correct |
4 ms |
632 KB |
Output is correct |
11 |
Correct |
2 ms |
760 KB |
Output is correct |
12 |
Correct |
4 ms |
632 KB |
Output is correct |
13 |
Correct |
4 ms |
632 KB |
Output is correct |
14 |
Correct |
4 ms |
632 KB |
Output is correct |
15 |
Correct |
5 ms |
632 KB |
Output is correct |
16 |
Correct |
4 ms |
636 KB |
Output is correct |
17 |
Correct |
5 ms |
632 KB |
Output is correct |
18 |
Correct |
4 ms |
632 KB |
Output is correct |
19 |
Correct |
4 ms |
632 KB |
Output is correct |
20 |
Correct |
4 ms |
632 KB |
Output is correct |
21 |
Correct |
625 ms |
32696 KB |
Output is correct |
22 |
Correct |
605 ms |
32892 KB |
Output is correct |
23 |
Correct |
614 ms |
32792 KB |
Output is correct |
24 |
Correct |
347 ms |
32708 KB |
Output is correct |
25 |
Correct |
342 ms |
32660 KB |
Output is correct |
26 |
Correct |
371 ms |
32620 KB |
Output is correct |
27 |
Correct |
351 ms |
32760 KB |
Output is correct |
28 |
Correct |
349 ms |
32760 KB |
Output is correct |
29 |
Correct |
347 ms |
32804 KB |
Output is correct |
30 |
Correct |
368 ms |
32848 KB |
Output is correct |
31 |
Correct |
360 ms |
32760 KB |
Output is correct |
32 |
Correct |
365 ms |
32700 KB |
Output is correct |
33 |
Correct |
477 ms |
32620 KB |
Output is correct |
34 |
Correct |
476 ms |
32632 KB |
Output is correct |
35 |
Correct |
471 ms |
32604 KB |
Output is correct |
36 |
Correct |
580 ms |
32852 KB |
Output is correct |
37 |
Correct |
599 ms |
32964 KB |
Output is correct |
38 |
Correct |
595 ms |
32728 KB |
Output is correct |
39 |
Correct |
354 ms |
32732 KB |
Output is correct |
40 |
Correct |
355 ms |
32708 KB |
Output is correct |
41 |
Correct |
362 ms |
32592 KB |
Output is correct |
42 |
Correct |
353 ms |
32732 KB |
Output is correct |
43 |
Correct |
369 ms |
32784 KB |
Output is correct |
44 |
Correct |
377 ms |
32824 KB |
Output is correct |
45 |
Correct |
388 ms |
32792 KB |
Output is correct |
46 |
Correct |
405 ms |
32788 KB |
Output is correct |
47 |
Correct |
405 ms |
32752 KB |
Output is correct |
48 |
Correct |
524 ms |
32632 KB |
Output is correct |
49 |
Correct |
494 ms |
32616 KB |
Output is correct |
50 |
Correct |
543 ms |
32496 KB |
Output is correct |