#include <bits/stdc++.h>
#define IO_OP std::ios::sync_with_stdio(0); std::cin.tie(0);
#define F first
#define S second
#define V vector
#define PB push_back
#define MP make_pair
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << "Line(" << __LINE__ << ") -> " << #x << " is " << x << endl
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;
const int INF = 1e9 + 7;
signed main()
{
IO_OP;
int n;
cin >> n;
V<ll> a(n);
set<int> alive;
priority_queue<pair<ll, int>> pq;
for(int i = 0; i < n; i++) {
cin >> a[i];
alive.insert(i);
pq.push({a[i], i});
}
ll ans = 0;
for(int i = 0; i < (n + 1) / 2; i++) {
auto p = pq.top(); pq.pop();
if(alive.count(p.S) == 0) {
i--;
continue;
}
auto it = alive.find(p.S);
ans += p.F;
if(it != alive.begin() && next(it) != alive.end()) {
int l = *prev(it), r = *next(it);
alive.erase(l), alive.erase(r);
a[p.S] = a[l] + a[r] - a[p.S];
pq.push({a[p.S], p.S});
} else {
if(it != alive.begin()) alive.erase(prev(it));
if(next(it) != alive.end()) alive.erase(next(it));
alive.erase(it);
}
cout << ans << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
620 KB |
Output is correct |
2 |
Correct |
2 ms |
492 KB |
Output is correct |
3 |
Correct |
2 ms |
492 KB |
Output is correct |
4 |
Correct |
2 ms |
492 KB |
Output is correct |
5 |
Correct |
2 ms |
492 KB |
Output is correct |
6 |
Correct |
2 ms |
492 KB |
Output is correct |
7 |
Correct |
2 ms |
492 KB |
Output is correct |
8 |
Correct |
2 ms |
492 KB |
Output is correct |
9 |
Correct |
2 ms |
492 KB |
Output is correct |
10 |
Correct |
2 ms |
492 KB |
Output is correct |
11 |
Correct |
2 ms |
492 KB |
Output is correct |
12 |
Correct |
2 ms |
492 KB |
Output is correct |
13 |
Correct |
2 ms |
492 KB |
Output is correct |
14 |
Correct |
2 ms |
492 KB |
Output is correct |
15 |
Correct |
2 ms |
492 KB |
Output is correct |
16 |
Correct |
2 ms |
492 KB |
Output is correct |
17 |
Correct |
2 ms |
492 KB |
Output is correct |
18 |
Correct |
2 ms |
492 KB |
Output is correct |
19 |
Correct |
2 ms |
492 KB |
Output is correct |
20 |
Correct |
2 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
620 KB |
Output is correct |
2 |
Correct |
2 ms |
492 KB |
Output is correct |
3 |
Correct |
2 ms |
492 KB |
Output is correct |
4 |
Correct |
2 ms |
492 KB |
Output is correct |
5 |
Correct |
2 ms |
492 KB |
Output is correct |
6 |
Correct |
2 ms |
492 KB |
Output is correct |
7 |
Correct |
2 ms |
492 KB |
Output is correct |
8 |
Correct |
2 ms |
492 KB |
Output is correct |
9 |
Correct |
2 ms |
492 KB |
Output is correct |
10 |
Correct |
2 ms |
492 KB |
Output is correct |
11 |
Correct |
2 ms |
492 KB |
Output is correct |
12 |
Correct |
2 ms |
492 KB |
Output is correct |
13 |
Correct |
2 ms |
492 KB |
Output is correct |
14 |
Correct |
2 ms |
492 KB |
Output is correct |
15 |
Correct |
2 ms |
492 KB |
Output is correct |
16 |
Correct |
2 ms |
492 KB |
Output is correct |
17 |
Correct |
2 ms |
492 KB |
Output is correct |
18 |
Correct |
2 ms |
492 KB |
Output is correct |
19 |
Correct |
2 ms |
492 KB |
Output is correct |
20 |
Correct |
2 ms |
492 KB |
Output is correct |
21 |
Correct |
399 ms |
17872 KB |
Output is correct |
22 |
Correct |
399 ms |
17912 KB |
Output is correct |
23 |
Correct |
375 ms |
18132 KB |
Output is correct |
24 |
Correct |
212 ms |
17956 KB |
Output is correct |
25 |
Correct |
237 ms |
17748 KB |
Output is correct |
26 |
Correct |
206 ms |
17768 KB |
Output is correct |
27 |
Correct |
228 ms |
17876 KB |
Output is correct |
28 |
Correct |
219 ms |
17876 KB |
Output is correct |
29 |
Correct |
222 ms |
18308 KB |
Output is correct |
30 |
Correct |
225 ms |
17876 KB |
Output is correct |
31 |
Correct |
230 ms |
18020 KB |
Output is correct |
32 |
Correct |
232 ms |
17876 KB |
Output is correct |
33 |
Correct |
283 ms |
17620 KB |
Output is correct |
34 |
Correct |
275 ms |
17736 KB |
Output is correct |
35 |
Correct |
286 ms |
17716 KB |
Output is correct |
36 |
Correct |
377 ms |
18004 KB |
Output is correct |
37 |
Correct |
387 ms |
18004 KB |
Output is correct |
38 |
Correct |
385 ms |
18004 KB |
Output is correct |
39 |
Correct |
203 ms |
17748 KB |
Output is correct |
40 |
Correct |
209 ms |
17748 KB |
Output is correct |
41 |
Correct |
213 ms |
17748 KB |
Output is correct |
42 |
Correct |
217 ms |
18004 KB |
Output is correct |
43 |
Correct |
218 ms |
18004 KB |
Output is correct |
44 |
Correct |
213 ms |
17876 KB |
Output is correct |
45 |
Correct |
219 ms |
17876 KB |
Output is correct |
46 |
Correct |
233 ms |
17876 KB |
Output is correct |
47 |
Correct |
228 ms |
18004 KB |
Output is correct |
48 |
Correct |
273 ms |
17620 KB |
Output is correct |
49 |
Correct |
282 ms |
17876 KB |
Output is correct |
50 |
Correct |
276 ms |
17620 KB |
Output is correct |