#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
using ll = long long;
using ii = pair<int, int>;
using iii = tuple<int, int, int>;
using ld = long double;
int N;
ll ans, A[200005];
multiset<pair<ll, ll> > s, s2;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N;
N += 2;
A[1] = -(ll)1e16;
for (int i = 2; i <= N - 1; i++) {
cin >> A[i];
}
A[N] = -(ll)1e16;
for (int i = 1; i <= N; i++) {
s2.emplace(A[i], i);
s.emplace(i, A[i]);
}
for (int j = 1; j < (N + 1) / 2; j++) {
vector<set<pair<ll, ll> >::iterator> to_erase, to_erase2;
auto [x, idx] = *s2.rbegin();
ans += x;
auto it = s.find(mp(idx, x));
x = -x;
to_erase.pb(it);
to_erase2.pb(--s2.end());
++it;
if (it != s.end()) {
to_erase.pb(it);
to_erase2.pb(s2.find(mp(it->second, it->first)));
x += it->second;
}
--it;
if (it != s.begin()) {
--it;
to_erase.pb(it);
to_erase2.pb(s2.find(mp(it->second, it->first)));
x += it->second;
++it;
}
for (auto it_2 : to_erase) {
s.erase(it_2);
}
for (auto it_2 : to_erase2) {
s2.erase(it_2);
}
s.emplace(idx, x);
s2.emplace(x, idx);
cout << ans << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
600 KB |
Output is correct |
2 |
Correct |
2 ms |
604 KB |
Output is correct |
3 |
Correct |
2 ms |
608 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
2 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
732 KB |
Output is correct |
8 |
Correct |
2 ms |
604 KB |
Output is correct |
9 |
Correct |
2 ms |
736 KB |
Output is correct |
10 |
Correct |
2 ms |
856 KB |
Output is correct |
11 |
Correct |
3 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
612 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
3 ms |
764 KB |
Output is correct |
15 |
Correct |
2 ms |
728 KB |
Output is correct |
16 |
Correct |
2 ms |
600 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
2 ms |
604 KB |
Output is correct |
19 |
Correct |
2 ms |
604 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
600 KB |
Output is correct |
2 |
Correct |
2 ms |
604 KB |
Output is correct |
3 |
Correct |
2 ms |
608 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
2 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
732 KB |
Output is correct |
8 |
Correct |
2 ms |
604 KB |
Output is correct |
9 |
Correct |
2 ms |
736 KB |
Output is correct |
10 |
Correct |
2 ms |
856 KB |
Output is correct |
11 |
Correct |
3 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
612 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
3 ms |
764 KB |
Output is correct |
15 |
Correct |
2 ms |
728 KB |
Output is correct |
16 |
Correct |
2 ms |
600 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
2 ms |
604 KB |
Output is correct |
19 |
Correct |
2 ms |
604 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
21 |
Correct |
453 ms |
30364 KB |
Output is correct |
22 |
Correct |
419 ms |
30660 KB |
Output is correct |
23 |
Correct |
453 ms |
30312 KB |
Output is correct |
24 |
Correct |
181 ms |
30204 KB |
Output is correct |
25 |
Correct |
197 ms |
30232 KB |
Output is correct |
26 |
Correct |
180 ms |
30208 KB |
Output is correct |
27 |
Correct |
189 ms |
30336 KB |
Output is correct |
28 |
Correct |
207 ms |
30452 KB |
Output is correct |
29 |
Correct |
211 ms |
30424 KB |
Output is correct |
30 |
Correct |
204 ms |
30356 KB |
Output is correct |
31 |
Correct |
219 ms |
30376 KB |
Output is correct |
32 |
Correct |
237 ms |
30816 KB |
Output is correct |
33 |
Correct |
309 ms |
30252 KB |
Output is correct |
34 |
Correct |
294 ms |
30340 KB |
Output is correct |
35 |
Correct |
315 ms |
30208 KB |
Output is correct |
36 |
Correct |
392 ms |
30272 KB |
Output is correct |
37 |
Correct |
440 ms |
30356 KB |
Output is correct |
38 |
Correct |
424 ms |
30396 KB |
Output is correct |
39 |
Correct |
210 ms |
30096 KB |
Output is correct |
40 |
Correct |
188 ms |
30296 KB |
Output is correct |
41 |
Correct |
183 ms |
30292 KB |
Output is correct |
42 |
Correct |
211 ms |
30664 KB |
Output is correct |
43 |
Correct |
211 ms |
30416 KB |
Output is correct |
44 |
Correct |
196 ms |
30544 KB |
Output is correct |
45 |
Correct |
270 ms |
30256 KB |
Output is correct |
46 |
Correct |
243 ms |
30272 KB |
Output is correct |
47 |
Correct |
223 ms |
30280 KB |
Output is correct |
48 |
Correct |
294 ms |
30032 KB |
Output is correct |
49 |
Correct |
283 ms |
30252 KB |
Output is correct |
50 |
Correct |
301 ms |
30224 KB |
Output is correct |