#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define MAXN (1000005)
int main(){
ios_base::sync_with_stdio(false);cin.tie(0);
ll N;
cin>>N;
ll arr[N + 2];
bool used[N + 2];
ll l[N + 2];
ll r[N + 2];
priority_queue<pair<ll,ll> > pq;
memset(used,0,sizeof(used));
for(ll i = 1;i <= N;i++){
cin>>arr[i];
l[i] = i - 1;
r[i] = i + 1;
pq.push(make_pair(arr[i],i));
}
arr[0] = -1e15;
arr[N + 1] = -1e15;
l[0] = 0;
r[0] = 1;
l[N + 1] = N;
r[N + 1] = N + 1;
ll sum = 0;
for(ll j = 0;j < (N + 1) / 2;j++){
while(!pq.empty() && used[pq.top().second] == 1){
pq.pop();
}
ll value = pq.top().first;
ll i = pq.top().second;
pq.pop();
sum += value;
cout<<sum<<'\n';
arr[i] = arr[l[i]] + arr[r[i]] - arr[i];
used[l[i]] = 1;
used[r[i]] = 1;
pq.push(make_pair(arr[i],i));
l[i] = l[l[i]];
r[l[i]] = i;
r[i] = r[r[i]];
l[r[i]] = i;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
412 KB |
Output is correct |
6 |
Correct |
1 ms |
464 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
456 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
456 KB |
Output is correct |
11 |
Correct |
2 ms |
444 KB |
Output is correct |
12 |
Correct |
1 ms |
464 KB |
Output is correct |
13 |
Correct |
1 ms |
456 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
456 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
416 KB |
Output is correct |
20 |
Correct |
1 ms |
464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
412 KB |
Output is correct |
6 |
Correct |
1 ms |
464 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
456 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
456 KB |
Output is correct |
11 |
Correct |
2 ms |
444 KB |
Output is correct |
12 |
Correct |
1 ms |
464 KB |
Output is correct |
13 |
Correct |
1 ms |
456 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
456 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
416 KB |
Output is correct |
20 |
Correct |
1 ms |
464 KB |
Output is correct |
21 |
Correct |
96 ms |
11848 KB |
Output is correct |
22 |
Correct |
110 ms |
11956 KB |
Output is correct |
23 |
Correct |
97 ms |
11940 KB |
Output is correct |
24 |
Correct |
79 ms |
11668 KB |
Output is correct |
25 |
Correct |
76 ms |
11724 KB |
Output is correct |
26 |
Correct |
71 ms |
11728 KB |
Output is correct |
27 |
Correct |
87 ms |
11884 KB |
Output is correct |
28 |
Correct |
84 ms |
11856 KB |
Output is correct |
29 |
Correct |
82 ms |
11872 KB |
Output is correct |
30 |
Correct |
92 ms |
11836 KB |
Output is correct |
31 |
Correct |
82 ms |
11808 KB |
Output is correct |
32 |
Correct |
84 ms |
11872 KB |
Output is correct |
33 |
Correct |
105 ms |
11680 KB |
Output is correct |
34 |
Correct |
103 ms |
11580 KB |
Output is correct |
35 |
Correct |
100 ms |
11676 KB |
Output is correct |
36 |
Correct |
106 ms |
11868 KB |
Output is correct |
37 |
Correct |
105 ms |
11804 KB |
Output is correct |
38 |
Correct |
97 ms |
11832 KB |
Output is correct |
39 |
Correct |
72 ms |
11696 KB |
Output is correct |
40 |
Correct |
77 ms |
11748 KB |
Output is correct |
41 |
Correct |
73 ms |
11828 KB |
Output is correct |
42 |
Correct |
88 ms |
11820 KB |
Output is correct |
43 |
Correct |
80 ms |
11832 KB |
Output is correct |
44 |
Correct |
85 ms |
11832 KB |
Output is correct |
45 |
Correct |
96 ms |
11824 KB |
Output is correct |
46 |
Correct |
82 ms |
11852 KB |
Output is correct |
47 |
Correct |
86 ms |
11832 KB |
Output is correct |
48 |
Correct |
87 ms |
11580 KB |
Output is correct |
49 |
Correct |
86 ms |
11660 KB |
Output is correct |
50 |
Correct |
95 ms |
11628 KB |
Output is correct |