Untitled

 avatar
unknown
c_cpp
4 years ago
913 B
2
Indexable
#include <iostream>
using namespace std;
const int maxn = 100005;
int n;
int niza[maxn][3];
int memo[maxn][4];
int f(int x, int previous_activity) {
    if(x == n) {
        return 0;
    }
    if(memo[x][previous_activity] != -1) {
        return memo[x][previous_activity];
    }
    int najgolem = -2e9;
    for(int j = 0; j < 3; j++) {
        if(j != previous_activity) {
            najgolem = max(najgolem, f(x + 1, j) + niza[x][j]);
        }
    }
    memo[x][previous_activity] = najgolem;
    return memo[x][previous_activity];
}
int main()
{
    cin >> n;
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < 3; j++) {
            cin >> niza[i][j];
            memo[i][j] = -1;
        }
    }
    int najgolem = 0;
    najgolem = max(najgolem, f(0, 0));
    najgolem = max(najgolem, f(0, 1));
    najgolem = max(najgolem, f(0, 2));
    cout << najgolem << endl;
    
    return 0;
}

// 2504
Editor is loading...