Problem p03162 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define int long long
#define pb push_back
#define setbits(x) __builtin_popcountll(x)
#define endl "\n"
#define ll long long
int decimalsum(int x) {
  int sum = 0;
  while (x) {
    sum += x % 10;
    x = x / 10;
  }
  return sum;
}

int rec(vector<vector<int>> dp, int n, vector<vector<int>> a, int x, int p) {
  if (x == n)
    return 0;
  if (dp[x][p] != -1)
    return dp[x][p];
  return dp[x][p] = max(rec(dp, n, a, x + 1, (p + 1) % 3) + a[x][(p + 1) % 3],
                        rec(dp, n, a, x + 1, (p + 2) % 3) + a[x][(p + 2) % 3]);
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  //                int php[4000] , cdc[8000], cdm[8000];
  //                for(int j=0;j<5;j++){
  // for(int i=0;i<2000;i++)php[i/10]=1;
  // for(int i=0;i<2000;i++)php[i/10]=1;
  // for(int i=0;i<2000;i++)php[i/10]=1;
  // for(int i=0;i<2000;i++)php[i/10]=1;
  // for(int i=0;i<2000;i++)php[i/10]=1;
  // for(int i=0;i<2000;i++)php[i/10]=1;
  //
  // int t;
  // cin>>t;
  // while(t--)
  {
    // solve();

    int n;
    cin >> n;

    vector<vector<int>> dp(n, vector<int>(3, -1));
    vector<vector<int>> a(n, vector<int>(3, -1));
    for (int i = 0; i < n; i++) {
      cin >> a[i][0] >> a[i][1] >> a[i][2];
    }
    int ans = 0;
    ans = max(rec(dp, n, a, 1, 0) + a[0][0], rec(dp, n, a, 1, 1) + a[0][1]);
    ans = max(ans, rec(dp, n, a, 1, 2) + a[0][2]);
    cout << ans << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define int long long
#define pb push_back
#define setbits(x) __builtin_popcountll(x)
#define endl "\n"
#define ll long long
int decimalsum(int x) {
  int sum = 0;
  while (x) {
    sum += x % 10;
    x = x / 10;
  }
  return sum;
}

int rec(vector<vector<int>> dp, int n, vector<vector<int>> a, int x, int p) {
  if (x == n)
    return 0;
  if (dp[x][p] != -1)
    return dp[x][p];
  return dp[x][p] = max(rec(dp, n, a, x + 1, (p + 1) % 3) + a[x][(p + 1) % 3],
                        rec(dp, n, a, x + 1, (p + 2) % 3) + a[x][(p + 2) % 3]);
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  //                int php[4000] , cdc[8000], cdm[8000];
  //                for(int j=0;j<5;j++){
  // for(int i=0;i<2000;i++)php[i/10]=1;
  // for(int i=0;i<2000;i++)php[i/10]=1;
  // for(int i=0;i<2000;i++)php[i/10]=1;
  // for(int i=0;i<2000;i++)php[i/10]=1;
  // for(int i=0;i<2000;i++)php[i/10]=1;
  // for(int i=0;i<2000;i++)php[i/10]=1;
  //
  // int t;
  // cin>>t;
  // while(t--)
  {
    // solve();

    int n;
    cin >> n;

    vector<vector<int>> dp(n, vector<int>(3, -1));
    vector<vector<int>> a(n, vector<int>(3, -1));
    for (int i = 0; i < n; i++) {
      cin >> a[i][0] >> a[i][1] >> a[i][2];
    }
    int ans = 0;
    dp[0][0] = a[0][0];
    dp[0][1] = a[0][1];
    dp[0][2] = a[0][2];
    for (int i = 1; i < n; i++) {
      dp[i][0] = a[i][0] + max(dp[i - 1][1], dp[i - 1][2]);
      dp[i][1] = a[i][1] + max(dp[i - 1][0], dp[i - 1][2]);
      dp[i][2] = a[i][2] + max(dp[i - 1][1], dp[i - 1][0]);
    }
    ans = max(dp[n - 1][0], max(dp[n - 1][1], dp[n - 1][2]));
    cout << ans << endl;
  }
  return 0;
}