#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main()
{
cin.tie(0)->sync_with_stdio(0);
int n;
cin >> n;
vector<int> a(n + 2), l(n + 2), r(n + 2);
priority_queue<pair<int, int>> q;
for(int i = 1; i <= n; i++) {
cin >> a[i];
l[i] = i - 1, r[i] = i + 1;
q.emplace(a[i], i);
}
l[n + 1] = n;
r[0] = 1;
const int oo = 1e18;
a[0] = a[n + 1] = -oo;
int ans = 0;
vector<bool> used(n + 2);
for(int t = 1; t <= (n + 1) / 2; t++) {
while(used[q.top().second]) {
q.pop();
}
auto [add, i] = q.top();
q.pop();
ans += add;
a[i] = a[l[i]] + a[r[i]] - a[i];
q.emplace(a[i], i);
used[l[i]] = used[r[i]] = 1;
l[i] = l[l[i]];
r[l[i]] = i;
r[i] = r[r[i]];
l[r[i]] = i;
cout << ans << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
464 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
464 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
424 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
464 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
464 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
464 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
424 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
464 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
106 ms |
11668 KB |
Output is correct |
22 |
Correct |
90 ms |
11720 KB |
Output is correct |
23 |
Correct |
97 ms |
11676 KB |
Output is correct |
24 |
Correct |
91 ms |
11492 KB |
Output is correct |
25 |
Correct |
73 ms |
11512 KB |
Output is correct |
26 |
Correct |
66 ms |
11512 KB |
Output is correct |
27 |
Correct |
88 ms |
11788 KB |
Output is correct |
28 |
Correct |
98 ms |
11624 KB |
Output is correct |
29 |
Correct |
85 ms |
11784 KB |
Output is correct |
30 |
Correct |
97 ms |
11708 KB |
Output is correct |
31 |
Correct |
76 ms |
11732 KB |
Output is correct |
32 |
Correct |
84 ms |
11684 KB |
Output is correct |
33 |
Correct |
97 ms |
11512 KB |
Output is correct |
34 |
Correct |
87 ms |
11464 KB |
Output is correct |
35 |
Correct |
102 ms |
11516 KB |
Output is correct |
36 |
Correct |
98 ms |
11588 KB |
Output is correct |
37 |
Correct |
96 ms |
11592 KB |
Output is correct |
38 |
Correct |
98 ms |
11604 KB |
Output is correct |
39 |
Correct |
73 ms |
11564 KB |
Output is correct |
40 |
Correct |
73 ms |
11476 KB |
Output is correct |
41 |
Correct |
82 ms |
11572 KB |
Output is correct |
42 |
Correct |
82 ms |
11676 KB |
Output is correct |
43 |
Correct |
83 ms |
11664 KB |
Output is correct |
44 |
Correct |
82 ms |
11672 KB |
Output is correct |
45 |
Correct |
90 ms |
11680 KB |
Output is correct |
46 |
Correct |
87 ms |
11764 KB |
Output is correct |
47 |
Correct |
75 ms |
11700 KB |
Output is correct |
48 |
Correct |
82 ms |
11448 KB |
Output is correct |
49 |
Correct |
91 ms |
11468 KB |
Output is correct |
50 |
Correct |
89 ms |
11464 KB |
Output is correct |