#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mp make_pair
#define fi first
#define sc second
int n;
ll a[200005];
multiset<pair<ll,int> >M;
multiset<pair<int,ll> >rM;
ll ans[200005];
int main(){
cin >> n;
for(int i=1;i<=n;i++){
cin >> a[i];
M.insert(mp(-a[i],i));
rM.insert(mp(i,-a[i]));
}
M.insert(mp(1e18,0)); M.insert(mp(1e18,n+1));
rM.insert(mp(0,1e18)); rM.insert(mp(n+1,1e18));
ans[0] = 0;
for(int num=1;num<=(n+1)/2;num++){
pair<ll,int> p = *M.begin();
ans[num] = ans[num-1] - p.first;
cout << ans[num] << endl;
multiset<pair<int,ll> >::iterator it = rM.lower_bound(mp(p.sc,-1e18));
it--;
pair<int,ll>a = *it;
M.erase(M.find(mp(a.sc,a.fi)));
rM.erase(it++);
pair<int,ll>b = *it;
M.erase(M.find(mp(b.sc,b.fi)));
rM.erase(it++);
pair<int,ll>c = *it;
M.erase(M.find(mp(c.sc,c.fi)));
rM.erase(it++);
rM.insert(mp(b.fi,a.sc+c.sc-b.sc));
M.insert(mp(a.sc+c.sc-b.sc,b.fi));
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
632 KB |
Output is correct |
2 |
Correct |
7 ms |
748 KB |
Output is correct |
3 |
Correct |
6 ms |
748 KB |
Output is correct |
4 |
Correct |
6 ms |
792 KB |
Output is correct |
5 |
Correct |
6 ms |
792 KB |
Output is correct |
6 |
Correct |
6 ms |
792 KB |
Output is correct |
7 |
Correct |
6 ms |
840 KB |
Output is correct |
8 |
Correct |
6 ms |
844 KB |
Output is correct |
9 |
Correct |
6 ms |
844 KB |
Output is correct |
10 |
Correct |
6 ms |
852 KB |
Output is correct |
11 |
Correct |
6 ms |
856 KB |
Output is correct |
12 |
Correct |
6 ms |
856 KB |
Output is correct |
13 |
Correct |
6 ms |
864 KB |
Output is correct |
14 |
Correct |
6 ms |
868 KB |
Output is correct |
15 |
Correct |
7 ms |
872 KB |
Output is correct |
16 |
Correct |
6 ms |
876 KB |
Output is correct |
17 |
Correct |
6 ms |
880 KB |
Output is correct |
18 |
Correct |
6 ms |
1020 KB |
Output is correct |
19 |
Correct |
6 ms |
1020 KB |
Output is correct |
20 |
Correct |
7 ms |
1020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
632 KB |
Output is correct |
2 |
Correct |
7 ms |
748 KB |
Output is correct |
3 |
Correct |
6 ms |
748 KB |
Output is correct |
4 |
Correct |
6 ms |
792 KB |
Output is correct |
5 |
Correct |
6 ms |
792 KB |
Output is correct |
6 |
Correct |
6 ms |
792 KB |
Output is correct |
7 |
Correct |
6 ms |
840 KB |
Output is correct |
8 |
Correct |
6 ms |
844 KB |
Output is correct |
9 |
Correct |
6 ms |
844 KB |
Output is correct |
10 |
Correct |
6 ms |
852 KB |
Output is correct |
11 |
Correct |
6 ms |
856 KB |
Output is correct |
12 |
Correct |
6 ms |
856 KB |
Output is correct |
13 |
Correct |
6 ms |
864 KB |
Output is correct |
14 |
Correct |
6 ms |
868 KB |
Output is correct |
15 |
Correct |
7 ms |
872 KB |
Output is correct |
16 |
Correct |
6 ms |
876 KB |
Output is correct |
17 |
Correct |
6 ms |
880 KB |
Output is correct |
18 |
Correct |
6 ms |
1020 KB |
Output is correct |
19 |
Correct |
6 ms |
1020 KB |
Output is correct |
20 |
Correct |
7 ms |
1020 KB |
Output is correct |
21 |
Correct |
988 ms |
31564 KB |
Output is correct |
22 |
Correct |
998 ms |
33476 KB |
Output is correct |
23 |
Correct |
937 ms |
35436 KB |
Output is correct |
24 |
Correct |
539 ms |
37220 KB |
Output is correct |
25 |
Correct |
559 ms |
38832 KB |
Output is correct |
26 |
Correct |
600 ms |
40880 KB |
Output is correct |
27 |
Correct |
586 ms |
42640 KB |
Output is correct |
28 |
Correct |
644 ms |
44708 KB |
Output is correct |
29 |
Correct |
550 ms |
46520 KB |
Output is correct |
30 |
Correct |
592 ms |
48568 KB |
Output is correct |
31 |
Correct |
633 ms |
50308 KB |
Output is correct |
32 |
Correct |
576 ms |
52356 KB |
Output is correct |
33 |
Correct |
764 ms |
54040 KB |
Output is correct |
34 |
Correct |
768 ms |
55860 KB |
Output is correct |
35 |
Correct |
775 ms |
57536 KB |
Output is correct |
36 |
Correct |
1028 ms |
59616 KB |
Output is correct |
37 |
Correct |
1018 ms |
61284 KB |
Output is correct |
38 |
Correct |
1032 ms |
63172 KB |
Output is correct |
39 |
Correct |
564 ms |
65124 KB |
Output is correct |
40 |
Correct |
549 ms |
66772 KB |
Output is correct |
41 |
Correct |
582 ms |
68712 KB |
Output is correct |
42 |
Correct |
565 ms |
70432 KB |
Output is correct |
43 |
Correct |
554 ms |
72404 KB |
Output is correct |
44 |
Correct |
565 ms |
74392 KB |
Output is correct |
45 |
Correct |
580 ms |
76380 KB |
Output is correct |
46 |
Correct |
608 ms |
78220 KB |
Output is correct |
47 |
Correct |
588 ms |
80288 KB |
Output is correct |
48 |
Correct |
797 ms |
81960 KB |
Output is correct |
49 |
Correct |
782 ms |
83812 KB |
Output is correct |
50 |
Correct |
784 ms |
85544 KB |
Output is correct |