#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
int n;
cin>>n;
vector<int> t(n+2);
set<int> unused;
vector<ll> cost(n+2);
set<pair<ll, int>> cost_lst;
for(int i=1;i<=n;++i) {
cin>>t[i];
unused.insert(i);
cost[i]=t[i];
cost_lst.insert({cost[i], i});
}
cost[0]=-(1LL<<60);
cost[n+1]=-(1LL<<60);
unused.insert(0);
unused.insert(n+1);
ll ans=0;
for(int j=0;j<(n+1)/2;++j) {
auto best=(--cost_lst.end())->second;
auto best_cost=(--cost_lst.end())->first;
ans+=best_cost;
unused.erase(best);
cost_lst.erase({best_cost, best});
cost[best]=-cost[best];
auto it=unused.lower_bound(best);
if(it!=unused.end()) {
cost[best]+=cost[*it];
cost_lst.erase({cost[*it], *it});
unused.erase(it);
}
it=unused.lower_bound(best);
if(it!=unused.begin()) {
it--;
cost[best]+=cost[*it];
cost_lst.erase({cost[*it], *it});
unused.erase(it);
}
unused.insert(best);
cost_lst.insert({cost[best], best});
cout<<ans<<"\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
504 KB |
Output is correct |
2 |
Correct |
6 ms |
612 KB |
Output is correct |
3 |
Correct |
6 ms |
812 KB |
Output is correct |
4 |
Correct |
6 ms |
836 KB |
Output is correct |
5 |
Correct |
5 ms |
976 KB |
Output is correct |
6 |
Correct |
6 ms |
996 KB |
Output is correct |
7 |
Correct |
6 ms |
1076 KB |
Output is correct |
8 |
Correct |
5 ms |
1096 KB |
Output is correct |
9 |
Correct |
7 ms |
1132 KB |
Output is correct |
10 |
Correct |
5 ms |
1152 KB |
Output is correct |
11 |
Correct |
7 ms |
1172 KB |
Output is correct |
12 |
Correct |
6 ms |
1192 KB |
Output is correct |
13 |
Correct |
6 ms |
1212 KB |
Output is correct |
14 |
Correct |
7 ms |
1232 KB |
Output is correct |
15 |
Correct |
6 ms |
1252 KB |
Output is correct |
16 |
Correct |
6 ms |
1272 KB |
Output is correct |
17 |
Correct |
5 ms |
1292 KB |
Output is correct |
18 |
Correct |
5 ms |
1312 KB |
Output is correct |
19 |
Correct |
6 ms |
1332 KB |
Output is correct |
20 |
Correct |
5 ms |
1348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
504 KB |
Output is correct |
2 |
Correct |
6 ms |
612 KB |
Output is correct |
3 |
Correct |
6 ms |
812 KB |
Output is correct |
4 |
Correct |
6 ms |
836 KB |
Output is correct |
5 |
Correct |
5 ms |
976 KB |
Output is correct |
6 |
Correct |
6 ms |
996 KB |
Output is correct |
7 |
Correct |
6 ms |
1076 KB |
Output is correct |
8 |
Correct |
5 ms |
1096 KB |
Output is correct |
9 |
Correct |
7 ms |
1132 KB |
Output is correct |
10 |
Correct |
5 ms |
1152 KB |
Output is correct |
11 |
Correct |
7 ms |
1172 KB |
Output is correct |
12 |
Correct |
6 ms |
1192 KB |
Output is correct |
13 |
Correct |
6 ms |
1212 KB |
Output is correct |
14 |
Correct |
7 ms |
1232 KB |
Output is correct |
15 |
Correct |
6 ms |
1252 KB |
Output is correct |
16 |
Correct |
6 ms |
1272 KB |
Output is correct |
17 |
Correct |
5 ms |
1292 KB |
Output is correct |
18 |
Correct |
5 ms |
1312 KB |
Output is correct |
19 |
Correct |
6 ms |
1332 KB |
Output is correct |
20 |
Correct |
5 ms |
1348 KB |
Output is correct |
21 |
Correct |
1145 ms |
28816 KB |
Output is correct |
22 |
Correct |
1122 ms |
30876 KB |
Output is correct |
23 |
Correct |
1118 ms |
32772 KB |
Output is correct |
24 |
Correct |
480 ms |
34396 KB |
Output is correct |
25 |
Correct |
514 ms |
36164 KB |
Output is correct |
26 |
Correct |
514 ms |
38136 KB |
Output is correct |
27 |
Correct |
469 ms |
40024 KB |
Output is correct |
28 |
Correct |
499 ms |
42048 KB |
Output is correct |
29 |
Correct |
455 ms |
43892 KB |
Output is correct |
30 |
Correct |
500 ms |
45996 KB |
Output is correct |
31 |
Correct |
463 ms |
47844 KB |
Output is correct |
32 |
Correct |
463 ms |
49780 KB |
Output is correct |
33 |
Correct |
677 ms |
51616 KB |
Output is correct |
34 |
Correct |
615 ms |
53196 KB |
Output is correct |
35 |
Correct |
616 ms |
55236 KB |
Output is correct |
36 |
Correct |
1046 ms |
57024 KB |
Output is correct |
37 |
Correct |
988 ms |
59016 KB |
Output is correct |
38 |
Correct |
1034 ms |
60908 KB |
Output is correct |
39 |
Correct |
487 ms |
62668 KB |
Output is correct |
40 |
Correct |
501 ms |
64448 KB |
Output is correct |
41 |
Correct |
495 ms |
66132 KB |
Output is correct |
42 |
Correct |
517 ms |
68256 KB |
Output is correct |
43 |
Correct |
431 ms |
70212 KB |
Output is correct |
44 |
Correct |
535 ms |
71968 KB |
Output is correct |
45 |
Correct |
557 ms |
73904 KB |
Output is correct |
46 |
Correct |
544 ms |
75976 KB |
Output is correct |
47 |
Correct |
485 ms |
78052 KB |
Output is correct |
48 |
Correct |
697 ms |
79672 KB |
Output is correct |
49 |
Correct |
645 ms |
81368 KB |
Output is correct |
50 |
Correct |
650 ms |
83276 KB |
Output is correct |