#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<ll, ll>;
const int maxn = 2e5 + 5;
string to_string(string s) {
return '"' + s + '"';
}
string to_string(const char* s) {
return to_string((string) s);
}
string to_string(bool b) {
return (b ? "true" : "false");
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
int N;
ll a[maxn];
struct node {
ll val;
node *l, *r;
node () {
val = 0;
l = r = NULL;
}
};
using llp = pair<ll, node*>;
multiset<ii> Q;
int le[maxn], ri[maxn];
int main(int argc, char const *argv[])
{
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
cin >> N;
for(int i = 1; i <= N; i++) {
cin >> a[i];
le[i] = i-1, ri[i] = i+1;
Q.insert(ii(a[i], i));
}
a[0] = -1e18, a[N+1] = -1e18;
/*for(int i = 1; i <= N; i++) {
node *now = new node();
now -> val = a[i];
now -> l = prev;
if(prev) now -> l -> r = now;
Q.insert(llp(a[i], now));
prev = now;
}*/
ll ans = 0;
for(int i = 1; i <= (N - 1) / 2 + 1; i++) {
// take best
int best = (int)Q.rbegin() -> second;
Q.erase(--Q.end());
// update answer
// ans += best -> val;
ans += a[best];
// create virtual elem
// node *now = new node();
// now -> val = -best -> val;
a[best] = -a[best];
int l = le[best];
int r = ri[best];
if(true) {
Q.erase(ii(a[l], l));
le[best] = le[l];
a[best] += a[l];
}
if(true) {
Q.erase(ii(a[r], r));
ri[best] = ri[r];
a[best] += a[r];
}
if(le[best]) {
ri[le[best]] = best;
}
if(ri[best] <= N) {
le[ri[best]] = best;
}
/*if(best -> l) {
Q.erase(llp(best -> l -> val, best -> l));
now -> val += best -> l -> val;
now -> l = best -> l -> l;
// delete(best -> l);
} else {
flag = 0;
}
if(best -> r) {
Q.erase(llp(best -> r -> val, best -> r));
now -> val += best -> r -> val;
now -> r = best -> r -> r;
// delete(best -> r);
} else {
flag = 0;
}
if(now -> l) now -> l -> r = now;
if(now -> r) now -> r -> l = now;*/
Q.insert(ii(a[best], best));
cout << ans << endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
492 KB |
Output is correct |
2 |
Correct |
5 ms |
492 KB |
Output is correct |
3 |
Correct |
5 ms |
492 KB |
Output is correct |
4 |
Correct |
5 ms |
748 KB |
Output is correct |
5 |
Correct |
5 ms |
492 KB |
Output is correct |
6 |
Correct |
5 ms |
492 KB |
Output is correct |
7 |
Correct |
5 ms |
492 KB |
Output is correct |
8 |
Correct |
6 ms |
492 KB |
Output is correct |
9 |
Correct |
5 ms |
492 KB |
Output is correct |
10 |
Correct |
4 ms |
492 KB |
Output is correct |
11 |
Correct |
5 ms |
492 KB |
Output is correct |
12 |
Correct |
5 ms |
492 KB |
Output is correct |
13 |
Correct |
7 ms |
492 KB |
Output is correct |
14 |
Correct |
115 ms |
492 KB |
Output is correct |
15 |
Correct |
5 ms |
492 KB |
Output is correct |
16 |
Correct |
5 ms |
492 KB |
Output is correct |
17 |
Correct |
5 ms |
492 KB |
Output is correct |
18 |
Correct |
79 ms |
620 KB |
Output is correct |
19 |
Correct |
4 ms |
492 KB |
Output is correct |
20 |
Correct |
5 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
492 KB |
Output is correct |
2 |
Correct |
5 ms |
492 KB |
Output is correct |
3 |
Correct |
5 ms |
492 KB |
Output is correct |
4 |
Correct |
5 ms |
748 KB |
Output is correct |
5 |
Correct |
5 ms |
492 KB |
Output is correct |
6 |
Correct |
5 ms |
492 KB |
Output is correct |
7 |
Correct |
5 ms |
492 KB |
Output is correct |
8 |
Correct |
6 ms |
492 KB |
Output is correct |
9 |
Correct |
5 ms |
492 KB |
Output is correct |
10 |
Correct |
4 ms |
492 KB |
Output is correct |
11 |
Correct |
5 ms |
492 KB |
Output is correct |
12 |
Correct |
5 ms |
492 KB |
Output is correct |
13 |
Correct |
7 ms |
492 KB |
Output is correct |
14 |
Correct |
115 ms |
492 KB |
Output is correct |
15 |
Correct |
5 ms |
492 KB |
Output is correct |
16 |
Correct |
5 ms |
492 KB |
Output is correct |
17 |
Correct |
5 ms |
492 KB |
Output is correct |
18 |
Correct |
79 ms |
620 KB |
Output is correct |
19 |
Correct |
4 ms |
492 KB |
Output is correct |
20 |
Correct |
5 ms |
492 KB |
Output is correct |
21 |
Correct |
785 ms |
19456 KB |
Output is correct |
22 |
Correct |
755 ms |
19476 KB |
Output is correct |
23 |
Correct |
713 ms |
19432 KB |
Output is correct |
24 |
Correct |
469 ms |
19404 KB |
Output is correct |
25 |
Correct |
467 ms |
19320 KB |
Output is correct |
26 |
Correct |
450 ms |
19388 KB |
Output is correct |
27 |
Correct |
464 ms |
19568 KB |
Output is correct |
28 |
Correct |
481 ms |
19564 KB |
Output is correct |
29 |
Correct |
489 ms |
19532 KB |
Output is correct |
30 |
Correct |
560 ms |
19548 KB |
Output is correct |
31 |
Correct |
479 ms |
19492 KB |
Output is correct |
32 |
Correct |
511 ms |
19464 KB |
Output is correct |
33 |
Correct |
639 ms |
19228 KB |
Output is correct |
34 |
Correct |
610 ms |
19180 KB |
Output is correct |
35 |
Correct |
617 ms |
19312 KB |
Output is correct |
36 |
Correct |
766 ms |
19436 KB |
Output is correct |
37 |
Correct |
782 ms |
19416 KB |
Output is correct |
38 |
Correct |
773 ms |
19364 KB |
Output is correct |
39 |
Correct |
485 ms |
19308 KB |
Output is correct |
40 |
Correct |
450 ms |
19436 KB |
Output is correct |
41 |
Correct |
454 ms |
19308 KB |
Output is correct |
42 |
Correct |
466 ms |
19404 KB |
Output is correct |
43 |
Correct |
471 ms |
19564 KB |
Output is correct |
44 |
Correct |
470 ms |
19564 KB |
Output is correct |
45 |
Correct |
484 ms |
19564 KB |
Output is correct |
46 |
Correct |
473 ms |
19436 KB |
Output is correct |
47 |
Correct |
474 ms |
19564 KB |
Output is correct |
48 |
Correct |
570 ms |
19368 KB |
Output is correct |
49 |
Correct |
569 ms |
19224 KB |
Output is correct |
50 |
Correct |
579 ms |
19428 KB |
Output is correct |