Cod sursa(job #2349817)

Utilizator IoanaDraganescuIoana Draganescu IoanaDraganescu Data 20 februarie 2019 19:00:01
Problema Oo Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <iostream>
#include <fstream>

using namespace std;

ifstream fin("oo.in");
ofstream fout("oo.out");

int v[100005], d[100005];

int main()
{
    int n;
    fin >> n;
    for (int i = 1; i <= n; i++)
        fin >> v[i];
    int max1 = 0, max2 = 0, max3 = 0;
    d[1] = v[1] + v[n]; d[2] = v[1] + v[n]; d[3] = v[1] + v[n];
    for (int i = 4; i <= n - 2; i++)
        d[i] = max(d[i - 1], d[i - 3] + v[i] + v[i - 1]);
    max1 = d[n - 2];
    d[2] = v[2] + v[1]; d[3] = v[2] + v[1]; d[4] = v[2] + v[1];
    for (int i = 5; i <= n - 1; i++)
        d[i] = max(d[i - 1], d[i - 3] + v[i] + v[i - 1]);
    max2 = d[n - 1];
    d[3] = v[2] + v[3]; d[4] = v[2] + v[3]; d[5] = v[2] + v[3];
    for (int i = 6; i <= n; i++)
        d[i] = max(d[i - 1], d[i - 3] + v[i] + v[i - 1]);
    max3 = d[n];
    fout << max(max1, max(max2, max3)) << '\n';
    return 0;
}