Problem p03162 (C++)

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

#ifdef DEBUG
#define PRINT(x)                                                               \
  cout << "func " << __func__ << ": line " << __LINE__ << ": " << #x << " = "  \
       << (x) << endl;
#define PRINTA(a, first, last)                                                 \
  cout << "func " << __func__ << ": line " << __LINE__ << ": " << #a << "["    \
       << (first) << ", " << (last) << ")" << endl;                            \
  for (int i = (first); i < (last); ++i) {                                     \
    cout << #a << "[" << i << "] = " << (a)[i] << endl;                        \
  }
#else
#define PRINT(x)
#define PRINTA(a, first, last)
#endif

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define MOD 1000000007

using namespace std;

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

template <class T, class U> void amax(T &x, U y) {
  if (x < y)
    x = y;
}
template <class T, class U> void amin(T &x, U y) {
  if (x > y)
    x = y;
}

int main(void) {
  constexpr int N_MAX = 1000;
  int N;
  int a[3][N_MAX];
  cin >> N;
  REP(i, N) REP(j, 3) cin >> a[j][i];

  int dp[3][N_MAX];
  REP(i, 3) dp[i][0] = a[i][0];

  FOR(i, 1, N) {
    REP(j, 3) {
      dp[j][i] = 0;
      REP(k, 3) {
        if (k != j) {
          amax(dp[j][i], dp[k][i - 1] + a[j][i]);
        }
      }
      // printf("dp[%d][%d] = %d\n", j, i, dp[j][i]);
    }
  }
  int result = 0;
  REP(i, 3) amax(result, dp[i][N - 1]);
  cout << result << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#ifdef DEBUG
#define PRINT(x)                                                               \
  cout << "func " << __func__ << ": line " << __LINE__ << ": " << #x << " = "  \
       << (x) << endl;
#define PRINTA(a, first, last)                                                 \
  cout << "func " << __func__ << ": line " << __LINE__ << ": " << #a << "["    \
       << (first) << ", " << (last) << ")" << endl;                            \
  for (int i = (first); i < (last); ++i) {                                     \
    cout << #a << "[" << i << "] = " << (a)[i] << endl;                        \
  }
#else
#define PRINT(x)
#define PRINTA(a, first, last)
#endif

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define MOD 1000000007

using namespace std;

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

template <class T, class U> void amax(T &x, U y) {
  if (x < y)
    x = y;
}
template <class T, class U> void amin(T &x, U y) {
  if (x > y)
    x = y;
}

int main(void) {
  constexpr int N_MAX = 100000;
  int N;
  int a[3][N_MAX];
  cin >> N;
  REP(i, N) REP(j, 3) cin >> a[j][i];

  int dp[3][N_MAX];
  REP(i, 3) dp[i][0] = a[i][0];

  FOR(i, 1, N) {
    REP(j, 3) {
      dp[j][i] = 0;
      REP(k, 3) {
        if (k != j) {
          amax(dp[j][i], dp[k][i - 1] + a[j][i]);
        }
      }
      // printf("dp[%d][%d] = %d\n", j, i, dp[j][i]);
    }
  }
  int result = 0;
  REP(i, 3) amax(result, dp[i][N - 1]);
  cout << result << endl;
  return 0;
}