#include<bits/stdc++.h>
#define MAXN 500007
using namespace std;
const long long inf=1e17;
struct node{
int l,r;
int from,to;
long long res;
};
int n,best;
long long a[MAXN],sum,maxs,curr;
bool used[MAXN];
long long even[MAXN],odd[MAXN];
node w[MAXN];
void erasev(int x){
w[w[x].l].r=w[x].r;
w[w[x].r].l=w[x].l;
}
long long pref[MAXN];
long long summ(int l,int r){
if(l%2==0){
return pref[r]-pref[l-2];
}else{
if(l==1)return pref[r];
else return pref[r]-pref[l-2];
}
}
set< pair<long long,int> > s;
pair<long long,int> z;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
pref[i]=a[i];
w[i]={i-1,i+1,i,i,a[i]};
s.insert({w[i].res,i});
}
for(int i=2;i<=n;i++){
pref[i]+=pref[i-2];
}
w[0].r=1;
w[n+1].l=n;
for(int i=1;i<=n/2+n%2;i++){
z=*s.rbegin();
maxs=z.first;
best=z.second;
s.erase(z);
sum+=maxs;
if(w[best].l==0){
s.erase({w[w[best].r].res,w[best].r});
erasev(w[best].r);
erasev(best);
}else if(w[best].r==n+1){
s.erase({w[w[best].l].res,w[best].l});
erasev(w[best].l);
erasev(best);
}else{
w[best].from=w[w[best].l].from;
w[best].to=w[w[best].r].to;
w[best].res=summ(w[best].from,w[best].to)-summ(w[best].from+1,w[best].to-1);
s.erase({w[w[best].l].res,w[best].l});
erasev(w[best].l);
s.erase({w[w[best].r].res,w[best].r});
erasev(w[best].r);
s.insert({w[best].res,best});
}
cout<<sum<<"\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4696 KB |
Output is correct |
2 |
Correct |
2 ms |
4700 KB |
Output is correct |
3 |
Correct |
2 ms |
4700 KB |
Output is correct |
4 |
Correct |
2 ms |
4700 KB |
Output is correct |
5 |
Correct |
2 ms |
4700 KB |
Output is correct |
6 |
Correct |
2 ms |
4700 KB |
Output is correct |
7 |
Correct |
2 ms |
4700 KB |
Output is correct |
8 |
Correct |
2 ms |
4700 KB |
Output is correct |
9 |
Correct |
2 ms |
4700 KB |
Output is correct |
10 |
Correct |
2 ms |
4700 KB |
Output is correct |
11 |
Correct |
2 ms |
4700 KB |
Output is correct |
12 |
Correct |
2 ms |
4580 KB |
Output is correct |
13 |
Correct |
2 ms |
4700 KB |
Output is correct |
14 |
Correct |
2 ms |
4700 KB |
Output is correct |
15 |
Correct |
2 ms |
4700 KB |
Output is correct |
16 |
Correct |
2 ms |
4700 KB |
Output is correct |
17 |
Correct |
2 ms |
4700 KB |
Output is correct |
18 |
Correct |
2 ms |
4700 KB |
Output is correct |
19 |
Correct |
2 ms |
4700 KB |
Output is correct |
20 |
Correct |
2 ms |
4700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4696 KB |
Output is correct |
2 |
Correct |
2 ms |
4700 KB |
Output is correct |
3 |
Correct |
2 ms |
4700 KB |
Output is correct |
4 |
Correct |
2 ms |
4700 KB |
Output is correct |
5 |
Correct |
2 ms |
4700 KB |
Output is correct |
6 |
Correct |
2 ms |
4700 KB |
Output is correct |
7 |
Correct |
2 ms |
4700 KB |
Output is correct |
8 |
Correct |
2 ms |
4700 KB |
Output is correct |
9 |
Correct |
2 ms |
4700 KB |
Output is correct |
10 |
Correct |
2 ms |
4700 KB |
Output is correct |
11 |
Correct |
2 ms |
4700 KB |
Output is correct |
12 |
Correct |
2 ms |
4580 KB |
Output is correct |
13 |
Correct |
2 ms |
4700 KB |
Output is correct |
14 |
Correct |
2 ms |
4700 KB |
Output is correct |
15 |
Correct |
2 ms |
4700 KB |
Output is correct |
16 |
Correct |
2 ms |
4700 KB |
Output is correct |
17 |
Correct |
2 ms |
4700 KB |
Output is correct |
18 |
Correct |
2 ms |
4700 KB |
Output is correct |
19 |
Correct |
2 ms |
4700 KB |
Output is correct |
20 |
Correct |
2 ms |
4700 KB |
Output is correct |
21 |
Correct |
287 ms |
30732 KB |
Output is correct |
22 |
Correct |
212 ms |
30800 KB |
Output is correct |
23 |
Correct |
216 ms |
30840 KB |
Output is correct |
24 |
Correct |
101 ms |
30532 KB |
Output is correct |
25 |
Correct |
101 ms |
30548 KB |
Output is correct |
26 |
Correct |
98 ms |
30544 KB |
Output is correct |
27 |
Correct |
115 ms |
30800 KB |
Output is correct |
28 |
Correct |
112 ms |
30884 KB |
Output is correct |
29 |
Correct |
117 ms |
30856 KB |
Output is correct |
30 |
Correct |
125 ms |
30804 KB |
Output is correct |
31 |
Correct |
122 ms |
30804 KB |
Output is correct |
32 |
Correct |
129 ms |
30804 KB |
Output is correct |
33 |
Correct |
172 ms |
30544 KB |
Output is correct |
34 |
Correct |
175 ms |
30620 KB |
Output is correct |
35 |
Correct |
171 ms |
30544 KB |
Output is correct |
36 |
Correct |
245 ms |
30716 KB |
Output is correct |
37 |
Correct |
216 ms |
30912 KB |
Output is correct |
38 |
Correct |
268 ms |
30896 KB |
Output is correct |
39 |
Correct |
102 ms |
30544 KB |
Output is correct |
40 |
Correct |
101 ms |
30692 KB |
Output is correct |
41 |
Correct |
109 ms |
30548 KB |
Output is correct |
42 |
Correct |
133 ms |
30924 KB |
Output is correct |
43 |
Correct |
111 ms |
30840 KB |
Output is correct |
44 |
Correct |
117 ms |
30804 KB |
Output is correct |
45 |
Correct |
125 ms |
30800 KB |
Output is correct |
46 |
Correct |
118 ms |
30720 KB |
Output is correct |
47 |
Correct |
123 ms |
30800 KB |
Output is correct |
48 |
Correct |
170 ms |
30688 KB |
Output is correct |
49 |
Correct |
173 ms |
30544 KB |
Output is correct |
50 |
Correct |
177 ms |
30720 KB |
Output is correct |