Pagini recente » Cod sursa (job #672161) | Cod sursa (job #532995) | Cod sursa (job #1852958) | Monitorul de evaluare | Cod sursa (job #2299582)
#include <fstream>
#include <iostream>
#include <algorithm>
#include <limits.h>
using namespace std;
ifstream fin ("oo.in");
ofstream fout ("oo.out");
int n,i,sol,v[100050],best[100050];
int main(){
fin>>n;
for(i=1;i<=n;i++)
fin>>v[i];
best[1]=v[1];
best[2]=v[1]+v[2];
for(i=3;i<n;i++)
best[i]=max(best[i-1],best[i-3]+v[i]+v[i-1]);
sol=best[n-1];
best[1]=0;
best[2]=v[2];
best[3]=v[2]+v[3];
for(i=4;i<=n;i++)
best[i]=max(best[i-1],best[i-3]+v[i]+v[i-1]);
sol=max(sol,best[n]);
best[1]=v[n];
best[2]=v[1]+v[n];
best[3]=v[1]+v[n];
for(i=4;i<n-1;i++)
best[i]=max(best[i-1],best[i-3]+v[i]+v[i-1]);
sol=max(sol,best[n]);
fout<<sol;
return 0;
}