#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int mxN = 2e5 + 5, mod = 1e9 + 7, LOG = 20;
int n;
pair <int, int> cons[mxN];
ll a[mxN];
int main() {
ios :: sync_with_stdio(false), cin.tie(nullptr);
cin >> n;
set <pair <ll, int> > st;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
cons[i] = {i - 1, i + 1};
st.insert({a[i], i});
}
ll ans = 0;
for (int i = 1; i <= (n + 1) / 2; ++i) {
auto x = *(--st.end());
ans += x.first;
st.erase(x);
int lb = cons[x.second].first, rb = cons[x.second].second;
if (lb) st.erase({a[lb], lb});
if (rb <= n) st.erase({a[rb], rb});
if (lb && rb && rb <= n) {
a[x.second] = a[lb] + a[rb] - a[x.second];
st.insert({a[x.second], x.second});
cons[x.second] = {cons[lb].first, cons[rb].second};
cons[cons[lb].first].second = x.second;
cons[cons[rb].second].first = x.second;
}
else {
cons[cons[lb].first].second = cons[rb].second;
cons[cons[rb].second].first = cons[lb].first;
}
cons[0] = {0, 0}; cons[n + 1] = {0, 0};
cout << ans << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
2 ms |
460 KB |
Output is correct |
4 |
Correct |
2 ms |
460 KB |
Output is correct |
5 |
Correct |
2 ms |
460 KB |
Output is correct |
6 |
Correct |
2 ms |
460 KB |
Output is correct |
7 |
Correct |
2 ms |
460 KB |
Output is correct |
8 |
Correct |
2 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
460 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 |
460 KB |
Output is correct |
18 |
Correct |
2 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
2 ms |
460 KB |
Output is correct |
4 |
Correct |
2 ms |
460 KB |
Output is correct |
5 |
Correct |
2 ms |
460 KB |
Output is correct |
6 |
Correct |
2 ms |
460 KB |
Output is correct |
7 |
Correct |
2 ms |
460 KB |
Output is correct |
8 |
Correct |
2 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
460 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 |
460 KB |
Output is correct |
18 |
Correct |
2 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
21 |
Correct |
304 ms |
19336 KB |
Output is correct |
22 |
Correct |
301 ms |
19308 KB |
Output is correct |
23 |
Correct |
282 ms |
19268 KB |
Output is correct |
24 |
Correct |
130 ms |
19140 KB |
Output is correct |
25 |
Correct |
137 ms |
19224 KB |
Output is correct |
26 |
Correct |
140 ms |
19168 KB |
Output is correct |
27 |
Correct |
132 ms |
19364 KB |
Output is correct |
28 |
Correct |
130 ms |
19320 KB |
Output is correct |
29 |
Correct |
133 ms |
19400 KB |
Output is correct |
30 |
Correct |
137 ms |
19396 KB |
Output is correct |
31 |
Correct |
136 ms |
19280 KB |
Output is correct |
32 |
Correct |
135 ms |
19304 KB |
Output is correct |
33 |
Correct |
214 ms |
19140 KB |
Output is correct |
34 |
Correct |
210 ms |
19140 KB |
Output is correct |
35 |
Correct |
217 ms |
19220 KB |
Output is correct |
36 |
Correct |
288 ms |
19284 KB |
Output is correct |
37 |
Correct |
298 ms |
19304 KB |
Output is correct |
38 |
Correct |
303 ms |
19304 KB |
Output is correct |
39 |
Correct |
123 ms |
19168 KB |
Output is correct |
40 |
Correct |
120 ms |
19132 KB |
Output is correct |
41 |
Correct |
122 ms |
19100 KB |
Output is correct |
42 |
Correct |
131 ms |
19312 KB |
Output is correct |
43 |
Correct |
126 ms |
19396 KB |
Output is correct |
44 |
Correct |
140 ms |
19392 KB |
Output is correct |
45 |
Correct |
141 ms |
19396 KB |
Output is correct |
46 |
Correct |
149 ms |
19348 KB |
Output is correct |
47 |
Correct |
139 ms |
19384 KB |
Output is correct |
48 |
Correct |
215 ms |
19140 KB |
Output is correct |
49 |
Correct |
213 ms |
19180 KB |
Output is correct |
50 |
Correct |
209 ms |
19132 KB |
Output is correct |