// Why am I so dumb? :c
// chrono::system_clock::now().time_since_epoch().count()
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<ll, ll> pll;
template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int MAXN = (int)2e5 + 5;
const ll INF = (ll)1e18;
ll arr[MAXN], ans[MAXN];
int n;
pll rev(pll x) {
return mp(x.se, x.fi);
}
void solve() {
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%lld", &arr[i]);
}
set<pll> S, S2;
for (int i = 0; i < n; ++i) {
S.insert(mp(arr[i], i));
S2.insert(mp(i, arr[i]));
}
for (int i = 1; i <= (n + 1) / 2; ++i) {
ans[i] = ans[i - 1] + S.rbegin() -> fi;
auto it = S2.lower_bound(rev(*S.rbegin()));
if (it == --S2.end()) {
S.erase(rev(*S2.rbegin()));
S2.erase(--S2.end());
if (!S2.empty()) {
S.erase(rev(*S2.rbegin()));
S2.erase(--S2.end());
}
}
else if (it == S2.begin()) {
S.erase(rev(*S2.begin()));
S2.erase(S2.begin());
if (!S2.empty()) {
S.erase(rev(*S2.begin()));
S2.erase(S2.begin());
}
}
else {
--it;
pll A = *it++;
pll B = *it++;
pll C = *it++;
ll nw = A.se + C.se - B.se;
S.erase(rev(A));
S2.erase(A);
S.erase(rev(B));
S2.erase(B);
S.erase(rev(C));
S2.erase(C);
S.insert(mp(nw, A.fi));
S2.insert(mp(A.fi, nw));
}
}
for (int i = 1; i <= (n + 1) / 2; ++i) {
printf("%lld\n", ans[i]);
}
}
int main() {
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int tt = 1;
while (tt--) {
solve();
}
return 0;
}
Compilation message
candies.cpp: In function 'void solve()':
candies.cpp:38:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
candies.cpp:41:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld", &arr[i]);
~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
632 KB |
Output is correct |
2 |
Correct |
5 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 |
5 ms |
632 KB |
Output is correct |
7 |
Correct |
4 ms |
632 KB |
Output is correct |
8 |
Correct |
5 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 |
5 ms |
636 KB |
Output is correct |
12 |
Correct |
5 ms |
632 KB |
Output is correct |
13 |
Correct |
5 ms |
632 KB |
Output is correct |
14 |
Correct |
4 ms |
632 KB |
Output is correct |
15 |
Correct |
4 ms |
632 KB |
Output is correct |
16 |
Correct |
6 ms |
632 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 |
632 KB |
Output is correct |
2 |
Correct |
5 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 |
5 ms |
632 KB |
Output is correct |
7 |
Correct |
4 ms |
632 KB |
Output is correct |
8 |
Correct |
5 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 |
5 ms |
636 KB |
Output is correct |
12 |
Correct |
5 ms |
632 KB |
Output is correct |
13 |
Correct |
5 ms |
632 KB |
Output is correct |
14 |
Correct |
4 ms |
632 KB |
Output is correct |
15 |
Correct |
4 ms |
632 KB |
Output is correct |
16 |
Correct |
6 ms |
632 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 |
747 ms |
31156 KB |
Output is correct |
22 |
Correct |
763 ms |
31136 KB |
Output is correct |
23 |
Correct |
777 ms |
31156 KB |
Output is correct |
24 |
Correct |
371 ms |
30968 KB |
Output is correct |
25 |
Correct |
390 ms |
30968 KB |
Output is correct |
26 |
Correct |
374 ms |
30972 KB |
Output is correct |
27 |
Correct |
393 ms |
31244 KB |
Output is correct |
28 |
Correct |
413 ms |
31220 KB |
Output is correct |
29 |
Correct |
399 ms |
31344 KB |
Output is correct |
30 |
Correct |
428 ms |
31224 KB |
Output is correct |
31 |
Correct |
419 ms |
31188 KB |
Output is correct |
32 |
Correct |
416 ms |
31224 KB |
Output is correct |
33 |
Correct |
549 ms |
30964 KB |
Output is correct |
34 |
Correct |
546 ms |
31096 KB |
Output is correct |
35 |
Correct |
565 ms |
31020 KB |
Output is correct |
36 |
Correct |
756 ms |
31236 KB |
Output is correct |
37 |
Correct |
825 ms |
31284 KB |
Output is correct |
38 |
Correct |
729 ms |
31256 KB |
Output is correct |
39 |
Correct |
358 ms |
30968 KB |
Output is correct |
40 |
Correct |
357 ms |
30948 KB |
Output is correct |
41 |
Correct |
371 ms |
31024 KB |
Output is correct |
42 |
Correct |
381 ms |
31244 KB |
Output is correct |
43 |
Correct |
387 ms |
31180 KB |
Output is correct |
44 |
Correct |
383 ms |
31188 KB |
Output is correct |
45 |
Correct |
402 ms |
31352 KB |
Output is correct |
46 |
Correct |
393 ms |
31116 KB |
Output is correct |
47 |
Correct |
398 ms |
31216 KB |
Output is correct |
48 |
Correct |
536 ms |
30980 KB |
Output is correct |
49 |
Correct |
533 ms |
31100 KB |
Output is correct |
50 |
Correct |
553 ms |
31096 KB |
Output is correct |