Pagini recente » Cod sursa (job #1155119) | Cod sursa (job #1137366) | Cod sursa (job #192424) | Cod sursa (job #302067) | Cod sursa (job #41163)
Cod sursa(job #41163)
#include <stdio.h>
#define input "oo.in"
#define output "oo.out"
#define nmax 100010
long min,i,pz,cnt,n,a[nmax],max[nmax],cnt1,cnt2,cnt3;
int main()
{
FILE *fin,*fout;
fin=fopen(input,"r");
fout=fopen(output,"w");
fscanf(fin,"%ld",&n);
for (i=1;i<=n;i++) fscanf(fin,"%ld",&a[i]);
a[n+1]=a[1];
max[2]=a[1]+a[2];
max[3]=a[2]+a[3];
max[4]=a[3]+a[4];
cnt1=max[2];
for (i=5;i<n;i++)
{
max[i]=a[i]+a[i-1]+cnt1;
if (max[i-2]>cnt1) cnt1=max[i-2];
}
if (cnt1<max[i-1]) cnt1=max[i-1];
if (cnt1<max[i]) cnt1=max[i];
max[3]=a[2]+a[3];
max[4]=a[3]+a[4];
max[5]=a[4]+a[5];
cnt2=max[3];
for (i=6;i<=n;i++)
{
max[i]=a[i]+a[i-1]+cnt2;
if (max[i-2]>cnt1) cnt2=max[i-2];
}
if (cnt2<max[i-1]) cnt2=max[i-1];
if (cnt2<max[i]) cnt2=max[i];
max[4]=a[3]+a[4];
max[5]=a[4]+a[5];
max[6]=a[5]+a[6];
cnt3=max[4];
for (i=7;i<=n+1;i++)
{
max[i]=a[i]+a[i-1]+cnt3;
if (max[i-2]>cnt1) cnt3=max[i-2];
}
if (cnt3<max[i-1]) cnt3=max[i-1];
if (cnt3<max[i]) cnt3=max[i];
if ((cnt1>=cnt2) && (cnt1>=cnt3)) cnt=cnt1;
else
if ((cnt2>=cnt1) && (cnt2>=cnt3)) cnt=cnt2;
else cnt=cnt3;
fprintf(fout,"%ld",cnt);
fclose(fin);
fclose(fout);
return 0;
}