Cod sursa(job #3142034)

Utilizator not_anduAndu Scheusan not_andu Data 18 iulie 2023 14:38:29
Problema Oo Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.02 kb
#include <bits/stdc++.h>
#pragma GCC optimize("O3")

using namespace std;

typedef long long ll;

#define VMAX 100003

int n;
short v[VMAX];
int sum_1 = 0, sum_2 = 0, sum_3 = 0;

int operatie(int index){

    int d[VMAX];

    memset(d, 0, sizeof d);

    for(int i = index + 1; i < index + n - 1; ++i){
        
        if(i - 3 < 0){
            d[i] = max(d[i - 1], d[0] + v[i - 1] + v[i]);
        }
        else{
            d[i] = max(d[i - 1], d[i - 3] + v[i - 1] + v[i]);
        }

    }

    return d[index + n - 2];
}

ll maxim(int a, int b, int c){

    return max(a, max(b, c));

}

void solve(){

    cin >> n;
    
    for(int i = 1; i <= n; ++i){

        cin >> v[i];

    }

    v[n + 1] = v[1];

    sum_1 = operatie(1);
    sum_2 = operatie(2);
    sum_3 = operatie(3);

    cout << maxim(sum_1, sum_2, sum_3) << '\n';

}

int main(){
    
    ios_base::sync_with_stdio(false);

    freopen("oo.in", "r", stdin);
    freopen("oo.out", "w", stdout);

    cin.tie(nullptr);
    cout.tie(nullptr);

    solve();

    return 0;
}