#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 summ(int l,int r){
long long res=0;
for(int i=l;i<=r;i+=2){
res+=a[i];
}
return res;
}
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];
w[i]={i-1,i+1,i,i,a[i]};
}
w[0].r=1;
w[n+1].l=n;
for(int i=1;i<=n/2+n%2;i++){
curr=0; maxs=-inf;
while(true){
curr=w[curr].r;
if(curr==n+1)break;
if(w[curr].res>maxs){
maxs=w[curr].res; best=curr;
}
}
sum+=maxs;
if(w[best].l==0){
erasev(w[best].r);
erasev(best);
}else if(w[best].r==n+1){
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);
erasev(w[best].l);
erasev(w[best].r);
}
cout<<sum<<"\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2396 KB |
Output is correct |
2 |
Correct |
4 ms |
2396 KB |
Output is correct |
3 |
Correct |
4 ms |
2396 KB |
Output is correct |
4 |
Correct |
4 ms |
2396 KB |
Output is correct |
5 |
Correct |
4 ms |
2396 KB |
Output is correct |
6 |
Correct |
4 ms |
2396 KB |
Output is correct |
7 |
Correct |
4 ms |
2396 KB |
Output is correct |
8 |
Correct |
4 ms |
2396 KB |
Output is correct |
9 |
Correct |
4 ms |
2396 KB |
Output is correct |
10 |
Correct |
4 ms |
2396 KB |
Output is correct |
11 |
Correct |
4 ms |
2396 KB |
Output is correct |
12 |
Correct |
4 ms |
2396 KB |
Output is correct |
13 |
Correct |
4 ms |
2392 KB |
Output is correct |
14 |
Correct |
4 ms |
2596 KB |
Output is correct |
15 |
Correct |
4 ms |
2396 KB |
Output is correct |
16 |
Correct |
4 ms |
2392 KB |
Output is correct |
17 |
Correct |
4 ms |
2396 KB |
Output is correct |
18 |
Correct |
4 ms |
2396 KB |
Output is correct |
19 |
Correct |
4 ms |
2396 KB |
Output is correct |
20 |
Correct |
4 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2396 KB |
Output is correct |
2 |
Correct |
4 ms |
2396 KB |
Output is correct |
3 |
Correct |
4 ms |
2396 KB |
Output is correct |
4 |
Correct |
4 ms |
2396 KB |
Output is correct |
5 |
Correct |
4 ms |
2396 KB |
Output is correct |
6 |
Correct |
4 ms |
2396 KB |
Output is correct |
7 |
Correct |
4 ms |
2396 KB |
Output is correct |
8 |
Correct |
4 ms |
2396 KB |
Output is correct |
9 |
Correct |
4 ms |
2396 KB |
Output is correct |
10 |
Correct |
4 ms |
2396 KB |
Output is correct |
11 |
Correct |
4 ms |
2396 KB |
Output is correct |
12 |
Correct |
4 ms |
2396 KB |
Output is correct |
13 |
Correct |
4 ms |
2392 KB |
Output is correct |
14 |
Correct |
4 ms |
2596 KB |
Output is correct |
15 |
Correct |
4 ms |
2396 KB |
Output is correct |
16 |
Correct |
4 ms |
2392 KB |
Output is correct |
17 |
Correct |
4 ms |
2396 KB |
Output is correct |
18 |
Correct |
4 ms |
2396 KB |
Output is correct |
19 |
Correct |
4 ms |
2396 KB |
Output is correct |
20 |
Correct |
4 ms |
2396 KB |
Output is correct |
21 |
Execution timed out |
5053 ms |
13116 KB |
Time limit exceeded |
22 |
Halted |
0 ms |
0 KB |
- |