Problem p03162 (C++)

Buggy submission (status: Runtime Error):
//  [amitdu6ey]
// g++ -std=c++11 -o2 -Wall filename.cpp -o filename
#include <bits/stdc++.h>
#define hell 1000000009
#define bug1(x) cout << "$ " << x << " $" << endl
#define bug2(x) cout << "% " << x << " %" << endl
#define bug3(x) cout << "# " << x << " #" << endl
#define ll long long
#define pb push_back
#define mp make_pair
#define tr(z) for (auto it = z.begin(); it != z.end(); it++)
#define rloop(i, a, b) for (long long i = a; i >= b; i--)
#define loop(i, a, b) for (long long i = a; i < b; i++)
#define vbool vector<bool>
#define vvbool vector<vector<bool>>
#define vchar vector<char>
#define vi vector<int>
#define vvi vector<vector<int>>
#define pll pair<long long, long long>
#define vll vector<long long>
#define vvl vector<vector<long long>>
#define ninf numeric_limits<long long>::min()
#define pinf numeric_limits<long long>::max()
#define comp(a, b) (abs(a - b) < 1e-9) // to compare doubles
using namespace std;

ll a[10009][3];
ll dp[10009][3];
ll n;

ll f(ll x, ll l) {
  if (dp[l][x] != 0)
    return dp[l][x];
  else if (l >= n)
    return 0;
  else if (x == 0)
    return dp[l][x] = a[l][x] + max(f(1, l + 1), f(2, l + 1));
  else if (x == 1)
    return dp[l][x] = a[l][x] + max(f(0, l + 1), f(2, l + 1));
  else if (x == 2)
    return dp[l][x] = a[l][x] + max(f(0, l + 1), f(1, l + 1));
}

void solve() {
  cin >> n;
  loop(i, 0, n) {
    loop(j, 0, 3) { cin >> a[i][j]; }
  }
  loop(i, 0, 10009) {
    loop(j, 0, 3) { dp[i][j] = 0; }
  }
  cout << max({f(0, 0), f(1, 0), f(2, 0)}) << "\n";
  return;
}

int main() {
  ios_base::sync_with_stdio(0);
  cout.tie(0);
  cin.tie(0);
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  int test_cases = 1;
  // cin>>test_cases;
  while (test_cases--) {
    solve();
  }
  return 0;
}


Fixed submission:
//  [amitdu6ey]
// g++ -std=c++11 -o2 -Wall filename.cpp -o filename
#include <bits/stdc++.h>
#define hell 1000000009
#define bug1(x) cout << "$ " << x << " $" << endl
#define bug2(x) cout << "% " << x << " %" << endl
#define bug3(x) cout << "# " << x << " #" << endl
#define ll long long
#define pb push_back
#define mp make_pair
#define tr(z) for (auto it = z.begin(); it != z.end(); it++)
#define rloop(i, a, b) for (long long i = a; i >= b; i--)
#define loop(i, a, b) for (long long i = a; i < b; i++)
#define vbool vector<bool>
#define vvbool vector<vector<bool>>
#define vchar vector<char>
#define vi vector<int>
#define vvi vector<vector<int>>
#define pll pair<long long, long long>
#define vll vector<long long>
#define vvl vector<vector<long long>>
#define ninf numeric_limits<long long>::min()
#define pinf numeric_limits<long long>::max()
#define comp(a, b) (abs(a - b) < 1e-9) // to compare doubles
using namespace std;

ll a[100009][3];
ll dp[100009][3];
ll n;

ll f(ll x, ll l) {
  if (dp[l][x] != 0)
    return dp[l][x];
  else if (l >= n)
    return 0;
  else if (x == 0)
    return dp[l][x] = a[l][x] + max(f(1, l + 1), f(2, l + 1));
  else if (x == 1)
    return dp[l][x] = a[l][x] + max(f(0, l + 1), f(2, l + 1));
  else if (x == 2)
    return dp[l][x] = a[l][x] + max(f(0, l + 1), f(1, l + 1));
}

void solve() {
  cin >> n;
  loop(i, 0, n) {
    loop(j, 0, 3) { cin >> a[i][j]; }
  }
  loop(i, 0, 10009) {
    loop(j, 0, 3) { dp[i][j] = 0; }
  }
  cout << max({f(0, 0), f(1, 0), f(2, 0)}) << "\n";
  return;
}

int main() {
  ios_base::sync_with_stdio(0);
  cout.tie(0);
  cin.tie(0);
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  int test_cases = 1;
  // cin>>test_cases;
  while (test_cases--) {
    solve();
  }
  return 0;
}