This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |