Cod sursa(job #880952)

Utilizator MagnvsDaniel Constantin Anghel Magnvs Data 17 februarie 2013 15:52:49
Problema Oo Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.33 kb
#include <stdio.h>
 
int v[100001],t[100000],s[100001],u[99999];
 
int main()
{
    int n,i,max;
    freopen("oo.in","r",stdin);
    freopen("oo.out","w",stdout);
    scanf("%d",&n);
    for (i=1;i<n+1;i++) scanf("%d",&v[i]);
 
    s[3]=v[2]+v[3];
    s[4]=v[3]+v[4];
    s[5]=v[4]+v[5];
    for (i=6;i<n+1;i++)
    {
        s[i]=s[i-3]+v[i-1]+v[i];
        if (s[i-4]+v[i]+v[i-1]>s[i]) s[i]=s[i-4]+v[i]+v[i-1];
        if (s[i-5]+v[i]+v[i-1]>s[i]) s[i]=s[i-5]+v[i]+v[i-1];
    }
    max=s[n];
    if (s[n-1]>max) max=s[n-1];
    if (s[n-2]>max) max=s[n-2];
 
    t[2]=v[1]+v[2];
    t[5]=t[2]+v[5]+v[4];
    t[6]=t[2]+v[5]+v[6];
    t[7]=t[2]+v[7]+v[6];
    for (i=8;i<n;i++)
    {
        t[i]=t[i-3]+v[i-1]+v[i];
        if (t[i-4]+v[i]+v[i-1]>t[i]) t[i]=t[i-4]+v[i]+v[i-1];
        if (t[i-5]+v[i]+v[i-1]>t[i]) t[i]=t[i-5]+v[i]+v[i-1];
    }
    if (t[n-1]>max) max=t[n-1];
    if (t[n-2]>max) max=t[n-2];
    if (t[n-3]>max) max=t[n-3];
 
    u[1]=v[n]+v[1];
    u[4]=u[1]+v[3]+v[4];
    u[5]=u[1]+v[5]+v[4];
    u[6]=u[1]+v[5]+v[6];
    for (i=7;i<n-1;i++)
    {
        u[i]=u[i-3]+v[i-1]+v[i];
        if (u[i-4]+v[i]+v[i-1]>u[i]) u[i]=u[i-4]+v[i]+v[i-1];
        if (u[i-5]+v[i]+v[i-1]>u[i]) u[i]=u[i-5]+v[i]+v[i-1];
    }
    if (u[n-2]>max) max=u[n-2];
    if (u[n-3]>max) max=u[n-3];
    if (u[n-4]>max) max=u[n-4];
    printf("%d",max);
    return 0;
}