#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main() {
ios::sync_with_stdio(0); cin.tie(0);
int n, ans = 0;
cin >> n;
vector<int> a(n);
set<int> e;
set<pair<int, int>> st;
for (int i = 0; i < n; i++) {
cin >> a[i];
e.insert(i);
st.insert({a[i], i});
}
for (int it = 1; it <= (n + 1) / 2; it++) {
auto [val, id] = *st.rbegin();
ans += val;
if (e.size() > 2) {
if (id == *e.begin()) {
st.erase({a[id], id});
e.erase(id);
int lb = *e.begin();
st.erase({a[lb], lb});
e.erase(lb);
} else if (id == *e.rbegin()) {
st.erase({a[id], id});
e.erase(id);
int lb = *e.rbegin();
st.erase({a[lb], lb});
e.erase(lb);
} else {
auto it = --e.lower_bound(id);
int l = a[*it], ll = *it;
it = e.upper_bound(id);
int r = a[*it], rr = *it;
st.erase({l, ll}); e.erase(ll);
st.erase({r, rr}); e.erase(rr);
st.erase({a[id], id});
a[id] = l + r - a[id];
st.insert({a[id], id});
}
}
cout << ans << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
608 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
680 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
728 KB |
Output is correct |
18 |
Correct |
2 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
608 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
724 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
680 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
728 KB |
Output is correct |
18 |
Correct |
2 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
279 ms |
27220 KB |
Output is correct |
22 |
Correct |
261 ms |
27220 KB |
Output is correct |
23 |
Correct |
266 ms |
27216 KB |
Output is correct |
24 |
Correct |
130 ms |
27056 KB |
Output is correct |
25 |
Correct |
134 ms |
26960 KB |
Output is correct |
26 |
Correct |
135 ms |
27064 KB |
Output is correct |
27 |
Correct |
147 ms |
27212 KB |
Output is correct |
28 |
Correct |
142 ms |
27220 KB |
Output is correct |
29 |
Correct |
146 ms |
27220 KB |
Output is correct |
30 |
Correct |
165 ms |
27348 KB |
Output is correct |
31 |
Correct |
151 ms |
27212 KB |
Output is correct |
32 |
Correct |
142 ms |
27216 KB |
Output is correct |
33 |
Correct |
185 ms |
26960 KB |
Output is correct |
34 |
Correct |
189 ms |
26960 KB |
Output is correct |
35 |
Correct |
187 ms |
26960 KB |
Output is correct |
36 |
Correct |
280 ms |
27220 KB |
Output is correct |
37 |
Correct |
264 ms |
27112 KB |
Output is correct |
38 |
Correct |
257 ms |
27216 KB |
Output is correct |
39 |
Correct |
132 ms |
27108 KB |
Output is correct |
40 |
Correct |
135 ms |
26964 KB |
Output is correct |
41 |
Correct |
167 ms |
26964 KB |
Output is correct |
42 |
Correct |
147 ms |
27216 KB |
Output is correct |
43 |
Correct |
147 ms |
27476 KB |
Output is correct |
44 |
Correct |
145 ms |
27212 KB |
Output is correct |
45 |
Correct |
156 ms |
27216 KB |
Output is correct |
46 |
Correct |
154 ms |
27264 KB |
Output is correct |
47 |
Correct |
153 ms |
27336 KB |
Output is correct |
48 |
Correct |
203 ms |
26904 KB |
Output is correct |
49 |
Correct |
186 ms |
26960 KB |
Output is correct |
50 |
Correct |
187 ms |
27040 KB |
Output is correct |