#include <iostream>
#include <set>
using namespace std;
long long int a[200005];
int main() {
long long int n, res = 0;
cin >> n;
set<int> s;
set<pair<long long int, int>> t;
for (int i = 0; i < n; i++) {
cin >> a[i];
s.insert(i);
t.insert({a[i], i});
}
for (int i = 1; i <= (n + 1) / 2; i++) {
long long int h = (*t.rbegin()).first, k = (*t.rbegin()).second;
res += h;
if (s.size() > 2) {
if (k == *s.begin()) {
t.erase({a[k], k}); s.erase(k);
int l = *s.begin();
t.erase({a[l], l}); s.erase(l);
}
else if (k == *s.rbegin()) {
t.erase({a[k], k}); s.erase(k);
int l = *s.rbegin();
t.erase({a[l], l}); s.erase(l);
}
else {
int l = *prev(s.lower_bound(k)), r = *s.upper_bound(k);
t.erase({a[l], l}); t.erase({a[r], r});
s.erase(l); s.erase(r);
t.erase({a[k], k});
a[k] = a[l] + a[r] - a[k];
t.insert({a[k], k});
}
}
cout << res << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
696 KB |
Output is correct |
2 |
Correct |
3 ms |
600 KB |
Output is correct |
3 |
Correct |
2 ms |
604 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 |
600 KB |
Output is correct |
7 |
Correct |
2 ms |
600 KB |
Output is correct |
8 |
Correct |
2 ms |
604 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
3 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
856 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
708 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
2 ms |
600 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
696 KB |
Output is correct |
2 |
Correct |
3 ms |
600 KB |
Output is correct |
3 |
Correct |
2 ms |
604 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 |
600 KB |
Output is correct |
7 |
Correct |
2 ms |
600 KB |
Output is correct |
8 |
Correct |
2 ms |
604 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
3 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
856 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
708 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
2 ms |
600 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 |
399 ms |
27104 KB |
Output is correct |
22 |
Correct |
376 ms |
26964 KB |
Output is correct |
23 |
Correct |
382 ms |
27152 KB |
Output is correct |
24 |
Correct |
208 ms |
26716 KB |
Output is correct |
25 |
Correct |
198 ms |
26704 KB |
Output is correct |
26 |
Correct |
246 ms |
26704 KB |
Output is correct |
27 |
Correct |
221 ms |
27060 KB |
Output is correct |
28 |
Correct |
240 ms |
26964 KB |
Output is correct |
29 |
Correct |
233 ms |
26960 KB |
Output is correct |
30 |
Correct |
289 ms |
27140 KB |
Output is correct |
31 |
Correct |
268 ms |
26936 KB |
Output is correct |
32 |
Correct |
239 ms |
27124 KB |
Output is correct |
33 |
Correct |
355 ms |
26964 KB |
Output is correct |
34 |
Correct |
333 ms |
26856 KB |
Output is correct |
35 |
Correct |
313 ms |
26852 KB |
Output is correct |
36 |
Correct |
493 ms |
26988 KB |
Output is correct |
37 |
Correct |
387 ms |
26984 KB |
Output is correct |
38 |
Correct |
384 ms |
27064 KB |
Output is correct |
39 |
Correct |
210 ms |
26704 KB |
Output is correct |
40 |
Correct |
212 ms |
26892 KB |
Output is correct |
41 |
Correct |
203 ms |
26852 KB |
Output is correct |
42 |
Correct |
224 ms |
26960 KB |
Output is correct |
43 |
Correct |
264 ms |
27216 KB |
Output is correct |
44 |
Correct |
216 ms |
26964 KB |
Output is correct |
45 |
Correct |
240 ms |
27028 KB |
Output is correct |
46 |
Correct |
219 ms |
26964 KB |
Output is correct |
47 |
Correct |
237 ms |
27120 KB |
Output is correct |
48 |
Correct |
271 ms |
26704 KB |
Output is correct |
49 |
Correct |
307 ms |
26680 KB |
Output is correct |
50 |
Correct |
270 ms |
26704 KB |
Output is correct |