#include<bits/stdc++.h>
using namespace std;
long long inf=1e15;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n;
cin>>n;
vector<long long>all(n);
set<long long>allind;
set<pair<long long,long long>>st;
for(long long i=0;i<n;i++){
cin>>all[i];
st.insert(make_pair(all[i],i));
allind.insert(i);
}
long long res=0;
while((long long)allind.size()>0){
auto x=*st.rbegin();
st.erase(x);
res+=x.first;
cout<<res<<"\n";
if((long long)allind.size()==1){
break;
}
if((*allind.begin())==x.second){
allind.erase(*allind.begin());
st.erase(make_pair(all[*allind.begin()],*allind.begin()));
allind.erase(*allind.begin());
continue;
}
if((*allind.rbegin())==x.second){
allind.erase(*allind.rbegin());
st.erase(make_pair(all[*allind.rbegin()],*allind.rbegin()));
allind.erase(*allind.rbegin());
continue;
}
allind.erase(x.second);
auto y=allind.lower_bound(x.second);
long long nx=*y;
y--;
long long ps=*y;
st.erase(make_pair(all[nx],nx));
st.erase(make_pair(all[ps],ps));
allind.erase(nx);
allind.erase(ps);
all[x.second]=all[nx]+all[ps]-all[x.second];
st.insert(make_pair(all[x.second],x.second));
allind.insert(x.second);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
600 KB |
Output is correct |
2 |
Correct |
2 ms |
604 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
2 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
2 ms |
600 KB |
Output is correct |
9 |
Correct |
2 ms |
600 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
616 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
2 ms |
600 KB |
Output is correct |
18 |
Correct |
2 ms |
604 KB |
Output is correct |
19 |
Correct |
2 ms |
604 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
600 KB |
Output is correct |
2 |
Correct |
2 ms |
604 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
2 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
2 ms |
600 KB |
Output is correct |
9 |
Correct |
2 ms |
600 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
616 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
2 ms |
600 KB |
Output is correct |
18 |
Correct |
2 ms |
604 KB |
Output is correct |
19 |
Correct |
2 ms |
604 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
21 |
Correct |
406 ms |
25356 KB |
Output is correct |
22 |
Correct |
432 ms |
27244 KB |
Output is correct |
23 |
Correct |
435 ms |
27208 KB |
Output is correct |
24 |
Correct |
195 ms |
26964 KB |
Output is correct |
25 |
Correct |
224 ms |
27220 KB |
Output is correct |
26 |
Correct |
197 ms |
26964 KB |
Output is correct |
27 |
Correct |
210 ms |
27156 KB |
Output is correct |
28 |
Correct |
204 ms |
27220 KB |
Output is correct |
29 |
Correct |
207 ms |
27220 KB |
Output is correct |
30 |
Correct |
221 ms |
27220 KB |
Output is correct |
31 |
Correct |
226 ms |
27288 KB |
Output is correct |
32 |
Correct |
230 ms |
27372 KB |
Output is correct |
33 |
Correct |
310 ms |
27296 KB |
Output is correct |
34 |
Correct |
300 ms |
26964 KB |
Output is correct |
35 |
Correct |
299 ms |
26964 KB |
Output is correct |
36 |
Correct |
491 ms |
27172 KB |
Output is correct |
37 |
Correct |
427 ms |
27340 KB |
Output is correct |
38 |
Correct |
404 ms |
27200 KB |
Output is correct |
39 |
Correct |
205 ms |
27156 KB |
Output is correct |
40 |
Correct |
185 ms |
27168 KB |
Output is correct |
41 |
Correct |
204 ms |
27216 KB |
Output is correct |
42 |
Correct |
199 ms |
27372 KB |
Output is correct |
43 |
Correct |
202 ms |
27220 KB |
Output is correct |
44 |
Correct |
204 ms |
27324 KB |
Output is correct |
45 |
Correct |
222 ms |
27304 KB |
Output is correct |
46 |
Correct |
224 ms |
27220 KB |
Output is correct |
47 |
Correct |
218 ms |
27220 KB |
Output is correct |
48 |
Correct |
283 ms |
26964 KB |
Output is correct |
49 |
Correct |
290 ms |
27120 KB |
Output is correct |
50 |
Correct |
288 ms |
27148 KB |
Output is correct |