#include <bits/stdc++.h>
#define int long long
using namespace std;
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0);
int n;
cin>>n;
long long a[n+2];
a[0]=-1e18;
a[n+1]=-1e18;
for (int i=1; i<=n; i++) cin>>a[i];
long long l[n+2],r[n+2];
for (int i=1; i<=n; i++){
l[i]=i-1;
r[i]=i+1;
}
l[0]=0; r[n+1]=n+1;
bool used[n+2];
for (int i=0; i<=n+1; i++) used[i]=0;
set <pair <long long,int>,greater <pair <long long,int> > > s;
for (int i=1; i<=n; i++) s.insert(make_pair(a[i],i));
long long ans=0;
for (int iters=1; iters<=(n+1)/2; iters++){
die:;
pair <long long,int> tp=*s.begin(); s.erase(s.begin());
if (used[tp.second]) goto die;
ans+=tp.first;
cout<<ans<<'\n';
int ll=l[tp.second],rr=r[tp.second];
a[tp.second]=a[ll]+a[rr]-a[tp.second];
l[tp.second]=l[ll];
r[tp.second]=r[rr];
used[ll]=1;
used[rr]=1;
r[l[tp.second]]=tp.second;
l[r[tp.second]]=tp.second;
if (ll>=1&&rr<=n) s.insert(make_pair(a[tp.second],tp.second));
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 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 |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 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 |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
464 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 |
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 |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 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 |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
464 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
201 ms |
21104 KB |
Output is correct |
22 |
Correct |
210 ms |
21172 KB |
Output is correct |
23 |
Correct |
194 ms |
21052 KB |
Output is correct |
24 |
Correct |
100 ms |
20968 KB |
Output is correct |
25 |
Correct |
123 ms |
20960 KB |
Output is correct |
26 |
Correct |
97 ms |
20920 KB |
Output is correct |
27 |
Correct |
111 ms |
21064 KB |
Output is correct |
28 |
Correct |
113 ms |
21120 KB |
Output is correct |
29 |
Correct |
113 ms |
21056 KB |
Output is correct |
30 |
Correct |
114 ms |
21140 KB |
Output is correct |
31 |
Correct |
121 ms |
21144 KB |
Output is correct |
32 |
Correct |
114 ms |
21080 KB |
Output is correct |
33 |
Correct |
156 ms |
20944 KB |
Output is correct |
34 |
Correct |
154 ms |
20976 KB |
Output is correct |
35 |
Correct |
148 ms |
20976 KB |
Output is correct |
36 |
Correct |
195 ms |
21104 KB |
Output is correct |
37 |
Correct |
191 ms |
21184 KB |
Output is correct |
38 |
Correct |
198 ms |
21088 KB |
Output is correct |
39 |
Correct |
102 ms |
20940 KB |
Output is correct |
40 |
Correct |
99 ms |
20932 KB |
Output is correct |
41 |
Correct |
98 ms |
20940 KB |
Output is correct |
42 |
Correct |
117 ms |
21084 KB |
Output is correct |
43 |
Correct |
128 ms |
21148 KB |
Output is correct |
44 |
Correct |
127 ms |
21060 KB |
Output is correct |
45 |
Correct |
117 ms |
21140 KB |
Output is correct |
46 |
Correct |
129 ms |
21064 KB |
Output is correct |
47 |
Correct |
120 ms |
21120 KB |
Output is correct |
48 |
Correct |
133 ms |
20936 KB |
Output is correct |
49 |
Correct |
146 ms |
20864 KB |
Output is correct |
50 |
Correct |
157 ms |
20928 KB |
Output is correct |