Problem p03162 (C++)

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

#define ll long long
#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf (int)(1e18)
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(x)                                                                   \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)
#define rep(i, j, n) for (int i = j; i < n; i++)
#define all(p) p.begin(), p.end()
#define ub(a, b) upper_bound(all(a), b)
#define lb(a, b) lower_bound(all(a), b)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

void run() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}
int32_t main() {
  run();
  int n;
  cin >> n;
  vector<vector<int>> a(n, vector<int>(3, 0));
  rep(i, 0, n) { rep(j, 0, 3) cin >> a[i][j]; }
  vector<vector<int>> dp(n, vector<int>(3, 0));
  rep(i, 0, 3) dp[0][i] = a[0][i];
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      for (int k = 0; k < 3; k++) {
        if (k == j) {
          continue;
        }
        dp[i][j] = max(a[i][j] + dp[i - 1][k], dp[i][j]);
      }
    }
  }
  int ans = max(dp[n - 1][0], max(dp[n - 1][1], dp[n - 1][2]));
  cout << ans << endl;
  return 0;
}

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

#define ll long long
#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf (int)(1e18)
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(x)                                                                   \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)
#define rep(i, j, n) for (int i = j; i < n; i++)
#define all(p) p.begin(), p.end()
#define ub(a, b) upper_bound(all(a), b)
#define lb(a, b) lower_bound(all(a), b)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

void run() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
int32_t main() {
  run();
  int n;
  cin >> n;
  vector<vector<int>> a(n, vector<int>(3, 0));
  rep(i, 0, n) { rep(j, 0, 3) cin >> a[i][j]; }
  vector<vector<int>> dp(n, vector<int>(3, 0));
  rep(i, 0, 3) dp[0][i] = a[0][i];
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      for (int k = 0; k < 3; k++) {
        if (k == j) {
          continue;
        }
        dp[i][j] = max(a[i][j] + dp[i - 1][k], dp[i][j]);
      }
    }
  }
  int ans = max(dp[n - 1][0], max(dp[n - 1][1], dp[n - 1][2]));
  cout << ans << endl;
  return 0;
}