CCCPaste

Untitled

#include 

using namespace std;

int n;
int mat[100005][3];
int dp[100005][3];
int vacation(int i, int prev_activity) {
    if(i == n) {
        return 0;
    }
    if(dp[i][prev_activity] != -1) {
        return dp[i][prev_activity];
    }
    int result = 0;
    if(prev_activity == 0) {
        result = max(result, vacation(i + 1, 1) + mat[i][prev_activity]);
        result = max(result, vacation(i + 1, 2) + mat[i][prev_activity]);
    }
    else if(prev_activity == 1) {
        result = max(result, vacation(i + 1, 0) + mat[i][prev_activity]);
        result = max(result, vacation(i + 1, 2) + mat[i][prev_activity]);
    }
    else {
        result = max(result, vacation(i + 1, 0) + mat[i][prev_activity]);
        result = max(result, vacation(i + 1, 1) + mat[i][prev_activity]);
    }
    return dp[i][prev_activity] = result;
}
int main()
{
    cin >> n;
    for(int i = 0; i < n; i++) {
        cin >> mat[i][0] >> mat[i][1] >> mat[i][2];
        dp[i][0] = -1;
        dp[i][1] = -1;
        dp[i][2] = -1;
    }
    int result = max(vacation(0, 0), max(vacation(0, 1), vacation(0, 2)));
    cout << result << endl;
}