#include <bits/stdc++.h>
#define int long long
using namespace std;
signed main(void) {
ios_base::sync_with_stdio(false);
cin.tie(0);
int nbElem;
cin >> nbElem;
vector<int> val(nbElem);
for (auto &v : val)
cin >> v;
set<pair<int, int>> choices, revChoices;
for (int iEl = 0; iEl < nbElem; ++iEl) {
choices.emplace(val[iEl], iEl);
revChoices.emplace(iEl, val[iEl]);
}
choices.emplace(-1e18, -1);
choices.emplace(-1e18, nbElem);
revChoices.emplace(-1, -1e18);
revChoices.emplace(nbElem, -1e18);
int curRet = 0;
for (int prend = 1; prend <= (nbElem + 1) / 2; ++prend) {
auto [gain, pos] = *choices.rbegin();
curRet += gain;
cout << curRet << '\n';
if (prend + 1 > (nbElem + 1) / 2)
break;
choices.erase({gain, pos});
revChoices.erase({pos, gain});
auto it = revChoices.lower_bound({pos, -(int)1e18});
it--;
int gainNext = -gain;
gainNext += it->second;
choices.erase({it->second, it->first});
revChoices.erase(it++);
gainNext += it->second;
choices.erase({it->second, it->first});
revChoices.erase(it);
choices.emplace(gainNext, pos);
revChoices.emplace(pos, gainNext);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
588 KB |
Output is correct |
2 |
Correct |
2 ms |
588 KB |
Output is correct |
3 |
Correct |
2 ms |
588 KB |
Output is correct |
4 |
Correct |
2 ms |
588 KB |
Output is correct |
5 |
Correct |
2 ms |
588 KB |
Output is correct |
6 |
Correct |
3 ms |
588 KB |
Output is correct |
7 |
Correct |
2 ms |
588 KB |
Output is correct |
8 |
Correct |
2 ms |
588 KB |
Output is correct |
9 |
Correct |
2 ms |
588 KB |
Output is correct |
10 |
Correct |
2 ms |
588 KB |
Output is correct |
11 |
Correct |
3 ms |
588 KB |
Output is correct |
12 |
Correct |
3 ms |
588 KB |
Output is correct |
13 |
Correct |
2 ms |
588 KB |
Output is correct |
14 |
Correct |
2 ms |
588 KB |
Output is correct |
15 |
Correct |
3 ms |
588 KB |
Output is correct |
16 |
Correct |
3 ms |
588 KB |
Output is correct |
17 |
Correct |
2 ms |
588 KB |
Output is correct |
18 |
Correct |
3 ms |
588 KB |
Output is correct |
19 |
Correct |
2 ms |
588 KB |
Output is correct |
20 |
Correct |
2 ms |
588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
588 KB |
Output is correct |
2 |
Correct |
2 ms |
588 KB |
Output is correct |
3 |
Correct |
2 ms |
588 KB |
Output is correct |
4 |
Correct |
2 ms |
588 KB |
Output is correct |
5 |
Correct |
2 ms |
588 KB |
Output is correct |
6 |
Correct |
3 ms |
588 KB |
Output is correct |
7 |
Correct |
2 ms |
588 KB |
Output is correct |
8 |
Correct |
2 ms |
588 KB |
Output is correct |
9 |
Correct |
2 ms |
588 KB |
Output is correct |
10 |
Correct |
2 ms |
588 KB |
Output is correct |
11 |
Correct |
3 ms |
588 KB |
Output is correct |
12 |
Correct |
3 ms |
588 KB |
Output is correct |
13 |
Correct |
2 ms |
588 KB |
Output is correct |
14 |
Correct |
2 ms |
588 KB |
Output is correct |
15 |
Correct |
3 ms |
588 KB |
Output is correct |
16 |
Correct |
3 ms |
588 KB |
Output is correct |
17 |
Correct |
2 ms |
588 KB |
Output is correct |
18 |
Correct |
3 ms |
588 KB |
Output is correct |
19 |
Correct |
2 ms |
588 KB |
Output is correct |
20 |
Correct |
2 ms |
588 KB |
Output is correct |
21 |
Correct |
607 ms |
28472 KB |
Output is correct |
22 |
Correct |
629 ms |
28480 KB |
Output is correct |
23 |
Correct |
666 ms |
28356 KB |
Output is correct |
24 |
Correct |
274 ms |
28364 KB |
Output is correct |
25 |
Correct |
305 ms |
28404 KB |
Output is correct |
26 |
Correct |
271 ms |
28444 KB |
Output is correct |
27 |
Correct |
307 ms |
28400 KB |
Output is correct |
28 |
Correct |
285 ms |
28352 KB |
Output is correct |
29 |
Correct |
294 ms |
28292 KB |
Output is correct |
30 |
Correct |
319 ms |
28324 KB |
Output is correct |
31 |
Correct |
306 ms |
28468 KB |
Output is correct |
32 |
Correct |
304 ms |
28412 KB |
Output is correct |
33 |
Correct |
480 ms |
28316 KB |
Output is correct |
34 |
Correct |
417 ms |
28352 KB |
Output is correct |
35 |
Correct |
433 ms |
28356 KB |
Output is correct |
36 |
Correct |
637 ms |
28344 KB |
Output is correct |
37 |
Correct |
628 ms |
28420 KB |
Output is correct |
38 |
Correct |
693 ms |
28356 KB |
Output is correct |
39 |
Correct |
321 ms |
28284 KB |
Output is correct |
40 |
Correct |
272 ms |
28372 KB |
Output is correct |
41 |
Correct |
279 ms |
28360 KB |
Output is correct |
42 |
Correct |
284 ms |
28368 KB |
Output is correct |
43 |
Correct |
289 ms |
28348 KB |
Output is correct |
44 |
Correct |
284 ms |
28376 KB |
Output is correct |
45 |
Correct |
296 ms |
28444 KB |
Output is correct |
46 |
Correct |
299 ms |
28388 KB |
Output is correct |
47 |
Correct |
296 ms |
28356 KB |
Output is correct |
48 |
Correct |
447 ms |
28500 KB |
Output is correct |
49 |
Correct |
432 ms |
28420 KB |
Output is correct |
50 |
Correct |
435 ms |
28356 KB |
Output is correct |