Pagini recente » Cod sursa (job #1127611) | Cod sursa (job #2542220) | Cod sursa (job #2512839) | Cod sursa (job #1201671) | Cod sursa (job #2046535)
#include <stdio.h>
#define MAXN 100002
//#define MAXN 9
#define max(a,b) ((a) > (b) ? (a) : (b))
int N, a[MAXN-1], best[MAXN], res;
void read_data(void)
{
int i;
scanf("%d\n", &N);
for(i = 1; i <= N; i++)
scanf("%d ", &a[i]);
}
//7
//3 9 4 8 6 50 2
int main(void)
{
freopen("oo.in", "rt", stdin);
freopen("oo.out", "wt", stdout);
int i;
scanf("%d\n", &N);
for(i = 1; i <= N; i++)
scanf("%d ", &a[i]);
// iau 1 si 2
best[2] = best[3] = best[4] = a[1]+a[2];
for(i = 5; i < N; i++)
{
best[i]=best[i-1];
if( best[i]<a[i]+a[i-1]+best[i-3])
best[i] = a[i]+a[i-1]+best[i-3];
}
res = best[N-1];
// nu iau 1 si 2
best[0] = best[1] = best[2] = 0;
for(i = 3; i <= N; i++)
{
best[i]=best[i-1];
if( best[i]<a[i]+a[i-1]+best[i-3])
best[i] = a[i]+a[i-1]+best[i-3];
}
res = max(res, best[N]);
// iau 1 si N
best[1] = best[2] = best[3] = a[1]+a[N];
for(i = 4; i < N-1; i++)
{
best[i]=best[i-1];
if( best[i]<a[i]+a[i-1]+best[i-3])
best[i] = a[i]+a[i-1]+best[i-3];
}
res = max(res, best[N-2]);
printf("%d\n", res);
return 0;
}