Cod sursa(job #547593)

Utilizator dadudadUdrea Dragos dadudad Data 6 martie 2011 15:10:10
Problema Oo Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.05 kb
#include <stdio.h>
#define INPUT "oo.in"
#define OUTPUT "oo.out"
#define MAXN 100001
 int N;
int v[MAXN];
 
int main()
{
freopen(INPUT, "r", stdin);
freopen(OUTPUT, "w", stdout);
scanf("%d", &N);
int i;
for(i = 1; i <= N; ++i) scanf("%d", &v[i]);
if(N == 2)
{
printf("%d\n", v[1] + v[2]);
}
else if(N == 3)
{
int max = v[1] + v[2];
if(max < v[2] + v[3]) max = v[2] + v[3];
printf("%d\n", max);
}
else
{
int max1 = v[1] + v[2], max2 = 0, max3 = 0, max, MAX = max1;
for(i = 3; i < N; ++i)
{
max = max3 + v[i] + v[i-1];
if(max1 > max) max = max1;
if(max > MAX) MAX = max;
max3 = max2; max2 = max1; max1 = max;
}
 
max1 = v[2] + v[3]; max2 = 0; max3 = 0;
for(i = 4; i <= N; ++i)
{
max = max3 + v[i] + v[i-1];
if(max1 > max) max = max1;
if(max > MAX) MAX = max;
max3 = max2; max2 = max1; max1 = max;
}
 
max1 = v[N] + v[1], max2 = 0, max3 = 0;
for(i = 2; i < N-1; ++i)
{
max = max3 + v[i] + v[i-1];
if(max1 > max) max = max1;
if(max > MAX) MAX = max;
max3 = max2; max2 = max1; max1 = max;
}
printf("%d\n", MAX);
}
return 0;
}