Pagini recente » Cod sursa (job #149140) | Cod sursa (job #3163149) | Cod sursa (job #2082917) | Cod sursa (job #3162833) | Cod sursa (job #2046536)
#include <iostream>
#include <cstdio>
#define MAXN 100001
using namespace std;
FILE *fin=fopen("oo.in","r"),*fout=fopen("oo.out","w");
int n;
int poz(int i){
if(i%n==0) return n;
else return i%n;
}
int maxim(int a,int b,int c){
if(a>b and a>c) return 1;
else if(b>a and b>c) return 2;
return 3;
}
int main(){
int v[MAXN],smax=0;
fscanf(fin,"%d",&n);
for(int i=1;i<=n;i++){
fscanf(fin,"%d",&v[i]);
}
for(int i=1;i<=n;i++){
int a,b,c;
a=v[i]+v[poz(i+1)]-v[poz(i-1)]-v[poz(i+2)];
b=v[poz(i+1)]+v[poz(i+2)]-v[i]-v[poz(i+3)];
c=v[poz(i+2)]+v[poz(i+3)]-v[poz(i+1)]-v[poz(i+4)];
if(v[i]==-1 and v[poz(i+1)]!=-1 and v[poz(i+2)]!=-1 and v[poz(i+3)]!=-1){
if(b>c){
smax+=v[poz(i+1)]+v[poz(i+2)];
v[i]=-1,v[poz(i+1)]=-1,v[poz(i+2)]=-1,v[poz(i+3)]=-1;
i=poz(i+3);
}
else{
smax+=v[poz(i+2)]+v[poz(i+3)];
v[poz(i+1)]=-1,v[poz(i+2)]=-1,v[poz(i+3)]=-1,v[poz(i+4)]=-1;
i=poz(i+4);
}
}
else if(v[poz(i+3)]==-1 and v[i]!=-1 and v[poz(i+1)]!=-1 and v[poz(i+2)]!=-1){
if(a>b){
smax+=v[i]+v[poz(i+1)];
v[i]=-1,v[poz(i+1)]=-1,v[poz(i-1)]=-1,v[poz(i+2)]=-1;
i=poz(i+2);
}
else{
smax+=v[poz(i+1)]+v[poz(i+2)];
v[poz(i+1)]=-1,v[poz(i+2)]=-1,v[i]=-1,v[poz(i+3)]=-1;
i=poz(i+3);
}
}
else if(v[i]!=-1 and v[poz(i+1)]!=-1 and v[poz(i+2)]!=-1 and v[poz(i+3)]!=-1){
if(maxim(a,b,c)==1){
smax+=v[i]+v[poz(i+1)];
v[i]=-1,v[poz(i+1)]=-1,v[poz(i-1)]=-1,v[poz(i+2)]=-1;
i=poz(i+2);
}
else if(maxim(a,b,c)==2){
smax+=v[poz(i+1)]+v[poz(i+2)];
v[i]=-1,v[poz(i+1)]=-1,v[poz(i+2)]=-1,v[poz(i+3)]=-1;
i=poz(i+3);
}
else{
smax+=v[poz(i+2)]+v[poz(i+3)];
v[poz(i+1)]=-1,v[poz(i+2)]=-1,v[poz(i+3)]=-1,v[poz(i+4)]=-1;
i=poz(i+4);
}
}
}
fprintf(fout,"%d",smax);
return 0;
}