#include <bits/stdc++.h>
using namespace std;
#define TRACE(x) cerr << #x << " :: " << x << endl
#define _ << " " <<
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(),(x).end()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for (int i=(a);i>=(b);--i)
using ii=pair<long long,int>;
const int mxN = 2e5+5;
int N;
multiset<ii> ms;
map<int,ii> mp;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N;
FOR(i,1,N){
int A; cin >> A;
ms.insert(ii(A,i));
mp[i] = ii(A,1);
}
long long ans = 0;
FOR(_i,1,(N+1)/2){
//~ cout << _i << ":: "; for (auto& x : ms) { cout << x.first << ' '; } cout << endl;
auto i = prev(ms.end());
ans += i->first;
int idx = i->second;
auto y = mp.find(idx);
ii nw = ii(-y->second.first, -y->second.second);
if (y != mp.begin()) {
auto x = prev(y);
ms.erase(ii(x->second.first,x->first));
nw.first += x->second.first;
nw.second += x->second.second;
mp.erase(x);
}
if (next(y) != mp.end()) {
auto z = next(y);
ms.erase(ii(z->second.first,z->first));
nw.first += z->second.first;
nw.second += z->second.second;
mp.erase(z);
}
ms.erase(i);
mp.erase(y);
if (nw.second > 0) ms.insert(ii(nw.first, idx));
mp[idx] = nw;
//~ cout << _i << ":: "; for (auto& x : mp) { cout << x.second.first << ' '; } cout << endl;
cout << ans << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
640 KB |
Output is correct |
2 |
Correct |
2 ms |
640 KB |
Output is correct |
3 |
Correct |
3 ms |
640 KB |
Output is correct |
4 |
Correct |
2 ms |
640 KB |
Output is correct |
5 |
Correct |
2 ms |
640 KB |
Output is correct |
6 |
Correct |
3 ms |
640 KB |
Output is correct |
7 |
Correct |
2 ms |
640 KB |
Output is correct |
8 |
Correct |
2 ms |
640 KB |
Output is correct |
9 |
Correct |
2 ms |
640 KB |
Output is correct |
10 |
Correct |
2 ms |
640 KB |
Output is correct |
11 |
Correct |
3 ms |
640 KB |
Output is correct |
12 |
Correct |
3 ms |
640 KB |
Output is correct |
13 |
Correct |
2 ms |
640 KB |
Output is correct |
14 |
Correct |
3 ms |
640 KB |
Output is correct |
15 |
Correct |
2 ms |
640 KB |
Output is correct |
16 |
Correct |
2 ms |
640 KB |
Output is correct |
17 |
Correct |
2 ms |
640 KB |
Output is correct |
18 |
Correct |
3 ms |
640 KB |
Output is correct |
19 |
Correct |
2 ms |
640 KB |
Output is correct |
20 |
Correct |
2 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
640 KB |
Output is correct |
2 |
Correct |
2 ms |
640 KB |
Output is correct |
3 |
Correct |
3 ms |
640 KB |
Output is correct |
4 |
Correct |
2 ms |
640 KB |
Output is correct |
5 |
Correct |
2 ms |
640 KB |
Output is correct |
6 |
Correct |
3 ms |
640 KB |
Output is correct |
7 |
Correct |
2 ms |
640 KB |
Output is correct |
8 |
Correct |
2 ms |
640 KB |
Output is correct |
9 |
Correct |
2 ms |
640 KB |
Output is correct |
10 |
Correct |
2 ms |
640 KB |
Output is correct |
11 |
Correct |
3 ms |
640 KB |
Output is correct |
12 |
Correct |
3 ms |
640 KB |
Output is correct |
13 |
Correct |
2 ms |
640 KB |
Output is correct |
14 |
Correct |
3 ms |
640 KB |
Output is correct |
15 |
Correct |
2 ms |
640 KB |
Output is correct |
16 |
Correct |
2 ms |
640 KB |
Output is correct |
17 |
Correct |
2 ms |
640 KB |
Output is correct |
18 |
Correct |
3 ms |
640 KB |
Output is correct |
19 |
Correct |
2 ms |
640 KB |
Output is correct |
20 |
Correct |
2 ms |
640 KB |
Output is correct |
21 |
Correct |
587 ms |
28984 KB |
Output is correct |
22 |
Correct |
608 ms |
28832 KB |
Output is correct |
23 |
Correct |
602 ms |
28964 KB |
Output is correct |
24 |
Correct |
283 ms |
28664 KB |
Output is correct |
25 |
Correct |
262 ms |
28672 KB |
Output is correct |
26 |
Correct |
249 ms |
28664 KB |
Output is correct |
27 |
Correct |
268 ms |
28908 KB |
Output is correct |
28 |
Correct |
300 ms |
28792 KB |
Output is correct |
29 |
Correct |
280 ms |
29048 KB |
Output is correct |
30 |
Correct |
300 ms |
28920 KB |
Output is correct |
31 |
Correct |
284 ms |
28792 KB |
Output is correct |
32 |
Correct |
281 ms |
28792 KB |
Output is correct |
33 |
Correct |
396 ms |
28664 KB |
Output is correct |
34 |
Correct |
386 ms |
28600 KB |
Output is correct |
35 |
Correct |
388 ms |
28744 KB |
Output is correct |
36 |
Correct |
650 ms |
28952 KB |
Output is correct |
37 |
Correct |
556 ms |
28920 KB |
Output is correct |
38 |
Correct |
564 ms |
28980 KB |
Output is correct |
39 |
Correct |
255 ms |
28948 KB |
Output is correct |
40 |
Correct |
256 ms |
28664 KB |
Output is correct |
41 |
Correct |
265 ms |
28792 KB |
Output is correct |
42 |
Correct |
268 ms |
28924 KB |
Output is correct |
43 |
Correct |
292 ms |
28904 KB |
Output is correct |
44 |
Correct |
266 ms |
28792 KB |
Output is correct |
45 |
Correct |
289 ms |
28920 KB |
Output is correct |
46 |
Correct |
284 ms |
28928 KB |
Output is correct |
47 |
Correct |
303 ms |
28792 KB |
Output is correct |
48 |
Correct |
437 ms |
28536 KB |
Output is correct |
49 |
Correct |
408 ms |
28792 KB |
Output is correct |
50 |
Correct |
420 ms |
28748 KB |
Output is correct |