#include<bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pb push_back
#define mp make_pair
using namespace std;
ll n,a[200005],ans;
set<pair<ll,ll> >st;
set<ll>ind;
set<pair<ll,ll> >::iterator it;
set<ll>::iterator itt;
int main(){
ios::sync_with_stdio(false);
cin >> n;
for(int i=1; i<=n; i++){
cin >> a[i];
ind.insert(i);
st.insert(mp(a[i] , i));
}
a[0] = a[n + 1] = -99999999999999;
ind.insert(0);
ind.insert(n + 1);
for(int i=1; i<=(n + 1) / 2; i++){
it = st.end();
it--;
ll val = (*it).f , cur = (*it).s;
st.erase(it);
ans += val;
ind.erase(ind.find(cur));
itt = ind.upper_bound(cur);
a[cur] = -a[cur];
if(itt != ind.end()){
a[cur] += a[(*itt)];
st.erase(mp(a[(*itt)] , (*itt)));
ind.erase(itt);
}
itt = ind.upper_bound(cur);
if(itt != ind.begin()){
itt--;
a[cur] += a[(*itt)];
st.erase(mp(a[(*itt)] , (*itt)));
ind.erase(itt);
}
st.insert(mp(a[cur] , cur));
ind.insert(cur);
cout << ans << endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
632 KB |
Output is correct |
2 |
Correct |
9 ms |
632 KB |
Output is correct |
3 |
Correct |
9 ms |
636 KB |
Output is correct |
4 |
Correct |
9 ms |
632 KB |
Output is correct |
5 |
Correct |
9 ms |
632 KB |
Output is correct |
6 |
Correct |
9 ms |
632 KB |
Output is correct |
7 |
Correct |
9 ms |
632 KB |
Output is correct |
8 |
Correct |
9 ms |
632 KB |
Output is correct |
9 |
Correct |
8 ms |
632 KB |
Output is correct |
10 |
Correct |
9 ms |
632 KB |
Output is correct |
11 |
Correct |
9 ms |
632 KB |
Output is correct |
12 |
Correct |
9 ms |
632 KB |
Output is correct |
13 |
Correct |
9 ms |
632 KB |
Output is correct |
14 |
Correct |
9 ms |
636 KB |
Output is correct |
15 |
Correct |
9 ms |
632 KB |
Output is correct |
16 |
Correct |
9 ms |
632 KB |
Output is correct |
17 |
Correct |
9 ms |
632 KB |
Output is correct |
18 |
Correct |
9 ms |
632 KB |
Output is correct |
19 |
Correct |
8 ms |
632 KB |
Output is correct |
20 |
Correct |
8 ms |
632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
632 KB |
Output is correct |
2 |
Correct |
9 ms |
632 KB |
Output is correct |
3 |
Correct |
9 ms |
636 KB |
Output is correct |
4 |
Correct |
9 ms |
632 KB |
Output is correct |
5 |
Correct |
9 ms |
632 KB |
Output is correct |
6 |
Correct |
9 ms |
632 KB |
Output is correct |
7 |
Correct |
9 ms |
632 KB |
Output is correct |
8 |
Correct |
9 ms |
632 KB |
Output is correct |
9 |
Correct |
8 ms |
632 KB |
Output is correct |
10 |
Correct |
9 ms |
632 KB |
Output is correct |
11 |
Correct |
9 ms |
632 KB |
Output is correct |
12 |
Correct |
9 ms |
632 KB |
Output is correct |
13 |
Correct |
9 ms |
632 KB |
Output is correct |
14 |
Correct |
9 ms |
636 KB |
Output is correct |
15 |
Correct |
9 ms |
632 KB |
Output is correct |
16 |
Correct |
9 ms |
632 KB |
Output is correct |
17 |
Correct |
9 ms |
632 KB |
Output is correct |
18 |
Correct |
9 ms |
632 KB |
Output is correct |
19 |
Correct |
8 ms |
632 KB |
Output is correct |
20 |
Correct |
8 ms |
632 KB |
Output is correct |
21 |
Correct |
905 ms |
27392 KB |
Output is correct |
22 |
Correct |
915 ms |
27416 KB |
Output is correct |
23 |
Correct |
902 ms |
27384 KB |
Output is correct |
24 |
Correct |
514 ms |
27128 KB |
Output is correct |
25 |
Correct |
499 ms |
27128 KB |
Output is correct |
26 |
Correct |
509 ms |
27220 KB |
Output is correct |
27 |
Correct |
516 ms |
27384 KB |
Output is correct |
28 |
Correct |
523 ms |
27512 KB |
Output is correct |
29 |
Correct |
489 ms |
27384 KB |
Output is correct |
30 |
Correct |
527 ms |
27384 KB |
Output is correct |
31 |
Correct |
520 ms |
27384 KB |
Output is correct |
32 |
Correct |
521 ms |
27384 KB |
Output is correct |
33 |
Correct |
682 ms |
27256 KB |
Output is correct |
34 |
Correct |
708 ms |
27212 KB |
Output is correct |
35 |
Correct |
674 ms |
27256 KB |
Output is correct |
36 |
Correct |
933 ms |
27260 KB |
Output is correct |
37 |
Correct |
933 ms |
27492 KB |
Output is correct |
38 |
Correct |
889 ms |
27256 KB |
Output is correct |
39 |
Correct |
510 ms |
27260 KB |
Output is correct |
40 |
Correct |
499 ms |
27256 KB |
Output is correct |
41 |
Correct |
497 ms |
27256 KB |
Output is correct |
42 |
Correct |
519 ms |
27208 KB |
Output is correct |
43 |
Correct |
508 ms |
27256 KB |
Output is correct |
44 |
Correct |
491 ms |
27384 KB |
Output is correct |
45 |
Correct |
531 ms |
27256 KB |
Output is correct |
46 |
Correct |
507 ms |
27256 KB |
Output is correct |
47 |
Correct |
536 ms |
27384 KB |
Output is correct |
48 |
Correct |
695 ms |
27132 KB |
Output is correct |
49 |
Correct |
680 ms |
27256 KB |
Output is correct |
50 |
Correct |
699 ms |
27124 KB |
Output is correct |