Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("avx,avx2,fma")

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef vector<pii> vpii;
typedef vector<pil> vpil;
typedef vector<pli> vpli;
typedef set<int> si;
typedef set<ll> sll;
typedef map<int, int> mii;
typedef map<ll, ll> mll;

const double pi = acos(-1.0);
const long long INF = 2e18;

#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define sz size()
#define in insert
#define endl "\n"

#define sqr(a) ((a) * (a))
#define jor(a) !(a & 1) // jor means EVEN number
#define bjor(a) (a & 1) // bjor means ODD number

#define all(a) a.begin(), a.end()
#define mem(arr, b) memset(arr, b, sizeof(arr)) // memset only for -1 and 0
// fill(a, a+n, 5) //fill for any value

#define for0(i, b) for (int i = 0; i < (b); i++)
#define for1(i, b) for (int i = 1; i <= (b); i++)
#define forab(i, a, b) for (int i = (a); i <= (b); i++)
#define forba(i, b, a) for (int i = (b); i >= (a); i--)
#define rep(i, a, b, c) for (int i = (a); i != (b); i += (c))

#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define fileRead freopen("input.txt", "r", stdin);
#define fileWrite freopen("output.txt", "w", stdout);

void print(int a[], int n) { // for testing or debugging
  for (int i = 0; i < n; i++)
    cout << a[i] << " ";
  cout << endl;
}

//-----------------------------------------------------------------------

int const mx = 10005;
int a[mx], b[mx], c[mx], N;

int dpA[mx], dpB[mx], dpC[mx];

int fun(char ch, int n) {
  /// base case
  if (n == 0) {
    if (ch == 'a')
      return max(b[0], c[0]);
    if (ch == 'b')
      return max(a[0], c[0]);
    if (ch == 'c')
      return max(a[0], b[0]);
  }

  /// memoization
  if (ch == 'a' and dpA[n] != -1)
    return dpA[n];
  if (ch == 'b' and dpB[n] != -1)
    return dpB[n];
  if (ch == 'c' and dpC[n] != -1)
    return dpC[n];

  if (ch == 'a') {
    dpA[n] = a[n] + max(fun('b', n - 1), fun('c', n - 1));
    return dpA[n];
  }
  if (ch == 'b') {
    dpB[n] = b[n] + max(fun('a', n - 1), fun('c', n - 1));
    return dpB[n];
  }
  if (ch == 'c') {
    dpC[n] = c[n] + max(fun('a', n - 1), fun('b', n - 1));
    return dpC[n];
  }
}

int main() {
  IOS;
  // fileRead;
  int tc = 1;
  // cin>>tc;
  while (tc--) {
    mem(dpA, -1), mem(dpB, -1), mem(dpC, -1);

    cin >> N;
    for0(i, N) cin >> a[i] >> b[i] >> c[i];

    cout << max({fun('a', N - 1), fun('b', N - 1), fun('c', N - 1)});
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("avx,avx2,fma")

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef vector<pii> vpii;
typedef vector<pil> vpil;
typedef vector<pli> vpli;
typedef set<int> si;
typedef set<ll> sll;
typedef map<int, int> mii;
typedef map<ll, ll> mll;

const double pi = acos(-1.0);
const long long INF = 2e18;

#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define sz size()
#define in insert
#define endl "\n"

#define sqr(a) ((a) * (a))
#define jor(a) !(a & 1) // jor means EVEN number
#define bjor(a) (a & 1) // bjor means ODD number

#define all(a) a.begin(), a.end()
#define mem(arr, b) memset(arr, b, sizeof(arr)) // memset only for -1 and 0
// fill(a, a+n, 5) //fill for any value

#define for0(i, b) for (int i = 0; i < (b); i++)
#define for1(i, b) for (int i = 1; i <= (b); i++)
#define forab(i, a, b) for (int i = (a); i <= (b); i++)
#define forba(i, b, a) for (int i = (b); i >= (a); i--)
#define rep(i, a, b, c) for (int i = (a); i != (b); i += (c))

#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define fileRead freopen("input.txt", "r", stdin);
#define fileWrite freopen("output.txt", "w", stdout);

void print(int a[], int n) { // for testing or debugging
  for (int i = 0; i < n; i++)
    cout << a[i] << " ";
  cout << endl;
}

//-----------------------------------------------------------------------

int const mx = 100005;
int a[mx], b[mx], c[mx], N;

int dpA[mx], dpB[mx], dpC[mx];

int fun(char ch, int n) {
  /// base case
  if (n == 0) {
    if (ch == 'a')
      return max(b[0], c[0]);
    if (ch == 'b')
      return max(a[0], c[0]);
    if (ch == 'c')
      return max(a[0], b[0]);
  }

  /// memoization
  if (ch == 'a' and dpA[n] != -1)
    return dpA[n];
  if (ch == 'b' and dpB[n] != -1)
    return dpB[n];
  if (ch == 'c' and dpC[n] != -1)
    return dpC[n];

  if (ch == 'a') {
    dpA[n] = a[n] + max(fun('b', n - 1), fun('c', n - 1));
    return dpA[n];
  }
  if (ch == 'b') {
    dpB[n] = b[n] + max(fun('a', n - 1), fun('c', n - 1));
    return dpB[n];
  }
  if (ch == 'c') {
    dpC[n] = c[n] + max(fun('a', n - 1), fun('b', n - 1));
    return dpC[n];
  }
}

int main() {
  IOS;
  // fileRead;
  int tc = 1;
  // cin>>tc;
  while (tc--) {
    mem(dpA, -1), mem(dpB, -1), mem(dpC, -1);

    cin >> N;
    for0(i, N) cin >> a[i] >> b[i] >> c[i];

    cout << max({fun('a', N - 1), fun('b', N - 1), fun('c', N - 1)});
  }
}