prithivMLmods/Coder-Stat huggingface dataset:
Problem p03162 (C++)

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

#define ll unsigned long long
#define INF 1234567890
#define N (int)(1e5 + 5)
#define pi acos(-1)

void solve() {
  int dp[4];
  int n;
  cin >> n;
  for (int i = 1; i <= 3; ++i)
    cin >> dp[i];
  for (int i = 2; i <= n; ++i) {
    int cost[4], new_dp[4];
    for (int j = 1; j <= 3; ++j)
      cin >> cost[j], new_dp[j] = -INF;
    for (int j = 1; j <= 3; ++j)
      for (int k = 1; k <= 3; ++k)
        if (j != k)
          new_dp[j] = max(new_dp[j], cost[j] + dp[i]);
    for (int j = 1; j <= 3; ++j)
      dp[j] = new_dp[j];
  }
  cout << max(dp[1], max(dp[2], dp[3]));
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  solve();
  return 0;
}


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

#define ll unsigned long long
#define INF 1234567890
#define N (int)(1e5 + 5)
#define pi acos(-1)

void solve() {
  int dp[4];
  int n;
  cin >> n;
  for (int i = 1; i <= 3; ++i)
    cin >> dp[i];
  for (int i = 2; i <= n; ++i) {
    int cost[4], new_dp[4];
    for (int j = 1; j <= 3; ++j)
      cin >> cost[j], new_dp[j] = -INF;
    for (int j = 1; j <= 3; ++j)
      for (int k = 1; k <= 3; ++k)
        if (j != k)
          new_dp[j] = max(new_dp[j], cost[j] + dp[k]);
    for (int j = 1; j <= 3; ++j)
      dp[j] = new_dp[j];
  }
  cout << max(dp[1], max(dp[2], dp[3]));
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  solve();
  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int main() {
  int n;
  vector<vector<int>> a(n, vector<int>(3)), dp(n, vector<int>(3, INT_MIN));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < 3; j++)
      cin >> a[i][j];
  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++)
    for (int j = 0; j < 3; j++)
      for (int k = 0; k < 3; k++)
        if (j != k)
          dp[i][j] = max(dp[i][j], dp[i - 1][k] + a[i][j]);

  cout << max(dp[n - 1][0], max(dp[n - 1][1], dp[n - 1][2]));
}


Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<vector<int>> a(n, vector<int>(3)), dp(n, vector<int>(3, INT_MIN));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < 3; j++)
      cin >> a[i][j];
  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++)
    for (int j = 0; j < 3; j++)
      for (int k = 0; k < 3; k++)
        if (j != k)
          dp[i][j] = max(dp[i][j], dp[i - 1][k] + a[i][j]);

  cout << max(dp[n - 1][0], max(dp[n - 1][1], dp[n - 1][2]));
}
Problem p03162 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define all(v) v.begin(), v.end()
#define mp make_pair
#define ff first
#define ss second
#define MAXN 100005
#define endl "\n"
using namespace std;
ll n, a, b, c;
vector<int> v[3];
ll dp[MAXN][4];
ll rec(int i, int p) {
  if (i == n)
    return 0;
  ll ans = 0;
  for (int h = 0; h < 3; h++) {
    if (h != p) {
      ans = max(ans, rec(i + 1, h) + v[h][i]);
    }
  }
  // cout<<i<<" "<<p<<" "<<ans<<endl;
  return dp[i][p] = ans;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (ll i = 0; i < n; i++) {
    cin >> a >> b >> c;
    v[0].pb(a);
    v[1].pb(b);
    v[2].pb(c);
  }
  memset(dp, -1, sizeof(dp));
  ll ans = max(rec(0, 0), rec(0, 1));
  cout << ans;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define all(v) v.begin(), v.end()
#define mp make_pair
#define ff first
#define ss second
#define MAXN 100005
#define endl "\n"
using namespace std;
ll n, a, b, c;
vector<int> v[3];
ll dp[MAXN][4];
ll rec(int i, int p) {
  if (i == n)
    return 0;
  if (dp[i][p] != -1)
    return dp[i][p];
  ll ans = 0;
  for (int h = 0; h < 3; h++) {
    if (h != p) {
      ans = max(ans, rec(i + 1, h) + v[h][i]);
    }
  }
  // cout<<i<<" "<<p<<" "<<ans<<endl;
  return dp[i][p] = ans;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (ll i = 0; i < n; i++) {
    cin >> a >> b >> c;
    v[0].pb(a);
    v[1].pb(b);
    v[2].pb(c);
  }
  memset(dp, -1, sizeof(dp));
  ll ans = max(rec(0, 0), rec(0, 1));
  cout << ans;

  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int t = 1;
  // cin >> t;
  while (t--) {

    int n;
    cin >> n;
    vector<vector<int>> arr(n, vector<int>(3));
    long long temp;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 3; j++) {
        cin >> temp;
        arr[i][j] = temp;
      }
    }
    long long dp[10001][3];
    for (int i = 0; i < n; i++) {
      dp[0][i] = arr[0][i];
    }
    int k;
    temp = 0;
    for (int i = 1; i < n; i++) {
      for (int j = 0; j < 3; j++) {
        k = i - 1;
        temp = 0;
        for (int p = 0; p < 3; p++) {
          if (p != j && dp[k][p] > temp)
            temp = dp[k][p];
        }
        dp[i][j] = arr[i][j] + temp;
      }
    }
    long long ans = 0;
    for (int i = 0; i < 3; i++) {
      if (dp[n - 1][i] > ans)
        ans = dp[n - 1][i];
    }
    cout << ans << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int t = 1;
  // cin >> t;
  while (t--) {

    int n;
    cin >> n;
    vector<vector<int>> arr(n, vector<int>(3));
    long long temp;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 3; j++) {
        cin >> temp;
        arr[i][j] = temp;
      }
    }
    long long dp[100001][3];
    for (int i = 0; i < n; i++) {
      dp[0][i] = arr[0][i];
    }
    int k;
    temp = 0;
    for (int i = 1; i < n; i++) {
      for (int j = 0; j < 3; j++) {
        k = i - 1;
        temp = 0;
        for (int p = 0; p < 3; p++) {
          if (p != j && dp[k][p] > temp)
            temp = dp[k][p];
        }
        dp[i][j] = arr[i][j] + temp;
      }
    }
    long long ans = 0;
    for (int i = 0; i < 3; i++) {
      if (dp[n - 1][i] > ans)
        ans = dp[n - 1][i];
    }
    cout << ans << endl;
  }
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long LL;
typedef pair<LL, LL> P;
const LL mod = 1e9 + 7;
const LL LINF = 1LL << 62;
const int INF = 1000000000;

int main() {
  int N;
  cin >> N;
  vector<int> a(N), b(N), c(N);
  for (int i = 0; i < N; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }
  vector<vector<int>> dp(N, vector<int>(3, 0));
  for (int i = 0; i < N; i++) {
    dp[i + 1][0] = a[i] + max(dp[i][1], dp[i][2]);
    dp[i + 1][1] = b[i] + max(dp[i][0], dp[i][2]);
    dp[i + 1][2] = c[i] + max(dp[i][0], dp[i][1]);
  }
  cout << max({dp[N][0], dp[N][1], dp[N][2]}) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long LL;
typedef pair<LL, LL> P;
const LL mod = 1e9 + 7;
const LL LINF = 1LL << 62;
const int INF = 1000000000;

int main() {
  int N;
  cin >> N;
  vector<int> a(N), b(N), c(N);
  for (int i = 0; i < N; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }
  vector<vector<int>> dp(N + 1, vector<int>(3, 0));
  for (int i = 0; i < N; i++) {
    dp[i + 1][0] = a[i] + max(dp[i][1], dp[i][2]);
    dp[i + 1][1] = b[i] + max(dp[i][0], dp[i][2]);
    dp[i + 1][2] = c[i] + max(dp[i][0], dp[i][1]);
  }
  cout << max({dp[N][0], dp[N][1], dp[N][2]}) << endl;
  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
struct iofix {
  ios_base::Init i;
  iofix() {
    cin.sync_with_stdio(0);
    cin.tie(0);
    cout.sync_with_stdio(0);
    cout.tie(0);
    cout.precision(10);
    cout << fixed;
  }
} iofix;

void solve() {
  int N;
  cin >> N;
  vector<int64_t> dp(3, 0);
  for (int i = 0; i < N; ++i) {
    vector<int64_t> cost(3);
    for (int c = 0; c < N; ++c)
      cin >> cost[c];
    vector<int64_t> new_dp(3, 0);
    for (int j = 0; j < 3; ++j) {
      for (int k = 0; k < 3; ++k) {
        if (j != k)
          new_dp[k] = max(new_dp[k], dp[j] + cost[k]);
      }
    }
    dp = new_dp;
  }
  cout << max({dp[0], dp[1], dp[2]}) << "\n";
}

int main() {

  solve();

#ifndef Time
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
struct iofix {
  ios_base::Init i;
  iofix() {
    cin.sync_with_stdio(0);
    cin.tie(0);
    cout.sync_with_stdio(0);
    cout.tie(0);
    cout.precision(10);
    cout << fixed;
  }
} iofix;

void solve() {
  int N;
  cin >> N;
  vector<int64_t> dp(3, 0);
  for (int i = 0; i < N; ++i) {
    vector<int64_t> cost(3);
    for (int c = 0; c < 3; ++c)
      cin >> cost[c];
    vector<int64_t> new_dp(3, 0);
    for (int j = 0; j < 3; ++j) {
      for (int k = 0; k < 3; ++k) {
        if (j != k)
          new_dp[k] = max(new_dp[k], dp[j] + cost[k]);
      }
    }
    dp = new_dp;
  }
  cout << max({dp[0], dp[1], dp[2]}) << "\n";
}

int main() {

  solve();

#ifndef Time
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n) for (int i = 1; i <= (int)(n); i++)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define repi(i, n, m) for (int i = (int)(n); i < (int)(m); i++)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<vector<int>> abc(100010, vector<int>(3));
  reps(i, n) cin >> abc.at(i).at(0) >> abc.at(i).at(1) >> abc.at(i).at(2);

  vector<vector<int>> dp(
      10010,
      vector<int>(3,
                  0)); // dp[i][j]->i日目までの幸福度の総和のmax,i日目の活動はj
  rep(i, n) {
    rep(j, 3) {
      rep(k, 3) {
        if (k == j)
          continue;
        dp.at(i + 1).at(k) =
            max(dp.at(i + 1).at(k), dp.at(i).at(j) + abc.at(i + 1).at(k));
      }
    }
  }

  int ans = -1;
  rep(i, 3) ans = max(ans, dp.at(n).at(i));
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n) for (int i = 1; i <= (int)(n); i++)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define repi(i, n, m) for (int i = (int)(n); i < (int)(m); i++)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<vector<int>> abc(100010, vector<int>(3));
  reps(i, n) cin >> abc.at(i).at(0) >> abc.at(i).at(1) >> abc.at(i).at(2);

  vector<vector<int>> dp(
      100010,
      vector<int>(3,
                  0)); // dp[i][j]->i日目までの幸福度の総和のmax,i日目の活動はj
  rep(i, n) {
    rep(j, 3) {
      rep(k, 3) {
        if (k == j)
          continue;
        dp.at(i + 1).at(k) =
            max(dp.at(i + 1).at(k), dp.at(i).at(j) + abc.at(i + 1).at(k));
      }
    }
  }

  int ans = -1;
  rep(i, 3) ans = max(ans, dp.at(n).at(i));
  cout << ans << endl;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define pi pair<ll, ll>
#define pii pair<ll, pi>
#define pb push_back
#define mk make_pair
const int inf = 1e9;
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  int a[n][3]; // on the ith day if he did j the work
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> a[i][j];
    }
  }
  int dp[n][3];
  for (int i = 0; i < n; i++) {
    for (int k = 0; k < 3; k++) {
      dp[i][k] = 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] = max(dp[i - 1][1], dp[i - 1][2]) + a[i][0];
    dp[i][1] = max(dp[i - 1][0], dp[i - 1][2]) + a[i][1];
    dp[i][2] = max(dp[i - 1][1], dp[i - 1][0]) + a[i][2];
  }
  cout << max(dp[n - 1][0], max(dp[n - 1][1], dp[n - 1][2])) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define pi pair<ll, ll>
#define pii pair<ll, pi>
#define pb push_back
#define mk make_pair
const int inf = 1e9;
int main() {
  // #ifndef ONLINE_JUDGE
  // 	freopen("input.txt", "r", stdin);
  // 	freopen("output.txt", "w", stdout);
  // #endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  int a[n][3]; // on the ith day if he did j the work
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> a[i][j];
    }
  }
  int dp[n][3];
  for (int i = 0; i < n; i++) {
    for (int k = 0; k < 3; k++) {
      dp[i][k] = 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] = max(dp[i - 1][1], dp[i - 1][2]) + a[i][0];
    dp[i][1] = max(dp[i - 1][0], dp[i - 1][2]) + a[i][1];
    dp[i][2] = max(dp[i - 1][1], dp[i - 1][0]) + a[i][2];
  }
  cout << max(dp[n - 1][0], max(dp[n - 1][1], dp[n - 1][2])) << endl;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const int INT_INF = 2147483647;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
typedef pair<int, int> P;

int dp[100005][3];

int main() {
  int n;
  cin >> n;
  int h[n + 1][3];
  rep(i, n) cin >> h[i + 1][0] >> h[i + 1][1] >> h[i + 1][2];
  for (int d = 1; d <= n; d++) {
    rep(i, 3) {
      rep(j, 3) {
        if (i == j)
          continue;
        chmax(dp[d][i], dp[d - 1][j] + h[d][i]);
      }
    }
  }
  int ans = 0;
  rep(i, n) chmax(ans, dp[n][i]);
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const int INT_INF = 2147483647;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
typedef pair<int, int> P;

int dp[100005][3];

int main() {
  int n;
  cin >> n;
  int h[n + 1][3];
  rep(i, n) cin >> h[i + 1][0] >> h[i + 1][1] >> h[i + 1][2];
  for (int d = 1; d <= n; d++) {
    rep(i, 3) {
      rep(j, 3) {
        if (i == j)
          continue;
        chmax(dp[d][i], dp[d - 1][j] + h[d][i]);
      }
    }
  }
  int ans = 0;
  rep(i, 3) chmax(ans, dp[n][i]);
  cout << ans << endl;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <math.h>
#include <vector>
typedef long long ll;
// max ll constant  = LLONG_MAX

using namespace std;

vector<vector<int>> A(100000, vector<int>(3));
vector<vector<int>> dp(10000, vector<int>(3, -1));

int idx, N;
int f, s, ans = -1;

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i][0] >> A[i][1] >> A[i][2];
  }
  for (int j = 0; j < N; j++) {
    f = s = -1;
    for (int n = 0; n < 3; n++) {
      if (A[j][n] > f) {
        f = A[j][n];
        idx = n;
      }
    }
    if (j == 0) {
      for (int x = 0; x < 3; x++) {
        if (A[j][x] == f) {
          dp[j][x] = f;
        }
      }
    } else {
      for (int n = 0; n < 3; n++) {
        for (int r = 0; r < 3; r++) {
          if (r == n || dp[j - 1][r] == -1) {
            continue;
          } else {
            dp[j][n] = max(dp[j][n], A[j][n] + dp[j - 1][r]);
          }
        }
      }
    }
  }
  // for(int u = 0; u < N; u++){
  //   cout << dp[u][0] << " " << dp[u][1] << " " << dp[u][2] << endl;
  // }
  for (int l = 0; l < 3; l++) {
    if (dp[N - 1][l] > ans) {
      ans = dp[N - 1][l];
    }
  }
  cout << ans;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <math.h>
#include <vector>
typedef long long ll;
// max ll constant  = LLONG_MAX

using namespace std;

vector<vector<int>> A(100000, vector<int>(3));
vector<vector<int>> dp(100000, vector<int>(3, -1));

int idx, N;
int f, s, ans = -1;

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i][0] >> A[i][1] >> A[i][2];
  }
  for (int j = 0; j < N; j++) {
    f = s = -1;
    for (int n = 0; n < 3; n++) {
      if (A[j][n] > f) {
        f = A[j][n];
        idx = n;
      }
    }
    if (j == 0) {
      for (int x = 0; x < 3; x++) {
        if (A[j][x] == f) {
          dp[j][x] = f;
        }
      }
    } else {
      for (int n = 0; n < 3; n++) {
        for (int r = 0; r < 3; r++) {
          if (r == n || dp[j - 1][r] == -1) {
            continue;
          } else {
            dp[j][n] = max(dp[j][n], A[j][n] + dp[j - 1][r]);
          }
        }
      }
    }
  }
  // for(int u = 0; u < N; u++){
  //   cout << dp[u][0] << " " << dp[u][1] << " " << dp[u][2] << endl;
  // }
  for (int l = 0; l < 3; l++) {
    if (dp[N - 1][l] > ans) {
      ans = dp[N - 1][l];
    }
  }
  cout << ans;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
/// dp solution
#include <bits/stdc++.h>
using namespace std;
const int N = 10007;
long long dp[N][3];
int main() {
  int n, A[N], B[N], C[N];
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> A[i] >> B[i] >> C[i];
  dp[0][0] = A[0], dp[0][1] = B[0], dp[0][2] = C[0];
  for (int i = 1; i < n; ++i) {
    dp[i][0] = A[i] + max(dp[i - 1][1], dp[i - 1][2]);
    dp[i][1] = B[i] + max(dp[i - 1][0], dp[i - 1][2]);
    dp[i][2] = C[i] + max(dp[i - 1][0], dp[i - 1][1]);
  }
  cout << max(dp[n - 1][0], max(dp[n - 1][1], dp[n - 1][2]));
}


Fixed submission:
/// dp solution
#include <bits/stdc++.h>
using namespace std;
const int N = 100007;
long long dp[N][3];
int main() {
  int n, A[N], B[N], C[N];
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> A[i] >> B[i] >> C[i];
  dp[0][0] = A[0], dp[0][1] = B[0], dp[0][2] = C[0];
  for (int i = 1; i < n; ++i) {
    dp[i][0] = A[i] + max(dp[i - 1][1], dp[i - 1][2]);
    dp[i][1] = B[i] + max(dp[i - 1][0], dp[i - 1][2]);
    dp[i][2] = C[i] + max(dp[i - 1][0], dp[i - 1][1]);
  }
  cout << max(dp[n - 1][0], max(dp[n - 1][1], dp[n - 1][2]));
}
Problem p03162 (C++)

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

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int hpns[10010][3]; // 各幸福度
int dp[10010][3]; // dp[i][j]  => 前日jを選んだうえで達成できる最大値

int N, a, b, c;

int main() {
  cin >> N;

  for (int i = 1; i <= N; i++) {
    cin >> a >> b >> c;
    hpns[i][0] = a;
    hpns[i][1] = b;
    hpns[i][2] = c;
    dp[i][0] = -1;
    dp[i][1] = -1;
    dp[i][2] = -1;
  }

  dp[0][0] = 0;
  dp[0][1] = 0;
  dp[0][2] = 0;
  for (int i = 1; i <= N; i++) {
    for (int j = 0; j < 3; j++) {
      for (int k = 0; k < 3; k++) {
        if (j == k)
          continue;
        chmax(dp[i][j], dp[i - 1][k] + hpns[i][j]);
      }
    }
  }

  cout << max(dp[N][0], max(dp[N][1], dp[N][2])) << endl;
  return 0;
}


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

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int hpns[100010][3]; // 各幸福度
int dp[100010][3]; // dp[i][j]  => 前日jを選んだうえで達成できる最大値

int N, a, b, c;

int main() {
  cin >> N;

  for (int i = 1; i <= N; i++) {
    cin >> a >> b >> c;
    hpns[i][0] = a;
    hpns[i][1] = b;
    hpns[i][2] = c;
    dp[i][0] = -1;
    dp[i][1] = -1;
    dp[i][2] = -1;
  }

  dp[0][0] = 0;
  dp[0][1] = 0;
  dp[0][2] = 0;
  for (int i = 1; i <= N; i++) {
    for (int j = 0; j < 3; j++) {
      for (int k = 0; k < 3; k++) {
        if (j == k)
          continue;
        chmax(dp[i][j], dp[i - 1][k] + hpns[i][j]);
      }
    }
  }

  cout << max(dp[N][0], max(dp[N][1], dp[N][2])) << endl;
  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
long long n, f[4][100004], a[4][100005];
int main() {
  cin >> n;
  for (long long i = 1; i <= n; ++i) {
    cin >> a[i][1] >> a[i][2] >> a[i][3];
  }
  f[1][1] = a[1][1];
  f[1][2] = a[1][2];
  f[1][3] = a[1][3];
  for (long long i = 2; i <= n; ++i) {
    for (long long j = 1; j <= 3; ++j) {
      if (j == 1) {
        f[i][j] = max(f[i - 1][2], f[i - 1][3]) + a[i][j];
      } else if (j == 2) {
        f[i][j] = max(f[i - 1][1], f[i - 1][3]) + a[i][j];
      } else if (j == 3) {
        f[i][j] = max(f[i - 1][1], f[i - 1][2]) + a[i][j];
      }
    }
  }
  cout << max(f[n][1], max(f[n][2], f[n][3]));
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
long long n, f[100005][4], a[100005][4];
int main() {
  cin >> n;
  for (long long i = 1; i <= n; ++i) {
    cin >> a[i][1] >> a[i][2] >> a[i][3];
  }
  f[1][1] = a[1][1];
  f[1][2] = a[1][2];
  f[1][3] = a[1][3];
  for (long long i = 2; i <= n; ++i) {
    for (long long j = 1; j <= 3; ++j) {
      if (j == 1) {
        f[i][j] = max(f[i - 1][2], f[i - 1][3]) + a[i][j];
      } else if (j == 2) {
        f[i][j] = max(f[i - 1][1], f[i - 1][3]) + a[i][j];
      } else if (j == 3) {
        f[i][j] = max(f[i - 1][1], f[i - 1][2]) + a[i][j];
      }
    }
  }
  cout << max(f[n][1], max(f[n][2], f[n][3]));
  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <cstdlib>
#include <iostream>
using namespace std;

int n;
long long a[1010];
long long b[1010];
long long c[1010];
long long dp[10010][3];

int main() {
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> a[i] >> b[i] >> c[i];
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= 2; ++j) {
      dp[i][j] = 0;
    }
  }

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= 2; ++j) {
      if (j == 0) {
        dp[i][j] = max(dp[i - 1][1], dp[i - 1][2]) + a[i];
      } else if (j == 1) {
        dp[i][j] = max(dp[i - 1][2], dp[i - 1][0]) + b[i];
      } else if (j == 2) {
        dp[i][j] = max(dp[i - 1][0], dp[i - 1][1]) + c[i];
      }
    }
  }

  cout << max(max(dp[n - 1][0], dp[n - 1][1]), dp[n - 1][2]) << endl;
  return 0;
}


Fixed submission:
#include <cstdlib>
#include <iostream>
using namespace std;

int n;
long long a[100010];
long long b[100010];
long long c[100010];
long long dp[100010][3];

int main() {
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> a[i] >> b[i] >> c[i];
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= 2; ++j) {
      dp[i][j] = 0;
    }
  }

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= 2; ++j) {
      if (j == 0) {
        dp[i][j] = max(dp[i - 1][1], dp[i - 1][2]) + a[i];
      } else if (j == 1) {
        dp[i][j] = max(dp[i - 1][2], dp[i - 1][0]) + b[i];
      } else if (j == 2) {
        dp[i][j] = max(dp[i - 1][0], dp[i - 1][1]) + c[i];
      }
    }
  }

  cout << max(max(dp[n - 1][0], dp[n - 1][1]), dp[n - 1][2]) << endl;
  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
#define fin for (i = 0; i < n; i++)
#define vec vector<ll>
#define pb push_back
#define vecit vector<ll>::iterator
#define vecp vector<pair<ll, ll>>
#define vecpit vector<pair<ll, ll>>::iterator
#define mapp map<ll, ll>
#define mapr map<ll, pair<ll, ll>>
#define maprit map<ll, pair<ll, ll>>::iterator
#define mapit map<ll, ll>::iterator
#define mod1 1000000007
#define INF 1000000000000
using namespace std;
int main() {
  ll n;
  cin >> n;
  ll i, k, j, l;
  ll map1[n][4];
  map1[0][1] = 0;
  map1[0][2] = 0;
  map1[0][3] = 0;

  for (i = 1; i <= n; i++) {
    cin >> j >> k >> l;
    map1[i][1] = j + max(map1[i - 1][2], map1[i - 1][3]);
    map1[i][2] = k + max(map1[i - 1][1], map1[i - 1][3]);
    map1[i][3] = l + max(map1[i - 1][2], map1[i - 1][1]);
  }
  cout << max(max(map1[i - 1][1], map1[i - 1][2]), map1[i - 1][3]);
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define fin for (i = 0; i < n; i++)
#define vec vector<ll>
#define pb push_back
#define vecit vector<ll>::iterator
#define vecp vector<pair<ll, ll>>
#define vecpit vector<pair<ll, ll>>::iterator
#define mapp map<ll, ll>
#define mapr map<ll, pair<ll, ll>>
#define maprit map<ll, pair<ll, ll>>::iterator
#define mapit map<ll, ll>::iterator
#define mod1 1000000007
#define INF 1000000000000
using namespace std;
int main() {
  ll n;
  cin >> n;
  ll i, k, j, l;
  ll map1[n + 1][4];
  map1[0][1] = 0;
  map1[0][2] = 0;
  map1[0][3] = 0;

  for (i = 1; i <= n; i++) {
    cin >> j >> k >> l;
    map1[i][1] = j + max(map1[i - 1][2], map1[i - 1][3]);
    map1[i][2] = k + max(map1[i - 1][1], map1[i - 1][3]);
    map1[i][3] = l + max(map1[i - 1][2], map1[i - 1][1]);
  }
  cout << max(max(map1[i - 1][1], map1[i - 1][2]), map1[i - 1][3]);
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int dp[10001][3];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < 10001; i++) {
    for (int j = 0; j < 3; j++) {
      dp[i][j] = 0;
    }
  }
  vector<int> a(n);
  vector<int> b(n);
  vector<int> c(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    cin >> b[i];
    cin >> c[i];
  }
  dp[0][0] = a[0];
  dp[0][1] = b[0];
  dp[0][2] = c[0];

  for (int i = 1; i < n; i++) {
    dp[i][0] = max(dp[i - 1][1], dp[i - 1][2]) + a[i];
    dp[i][1] = max(dp[i - 1][0], dp[i - 1][2]) + b[i];
    dp[i][2] = max(dp[i - 1][0], dp[i - 1][1]) + c[i];
  }
  cout << max(dp[n - 1][0], max(dp[n - 1][1], dp[n - 1][2])) << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int dp[100001][3];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < 10001; i++) {
    for (int j = 0; j < 3; j++) {
      dp[i][j] = 0;
    }
  }
  vector<int> a(n);
  vector<int> b(n);
  vector<int> c(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    cin >> b[i];
    cin >> c[i];
  }
  dp[0][0] = a[0];
  dp[0][1] = b[0];
  dp[0][2] = c[0];

  for (int i = 1; i < n; i++) {
    dp[i][0] = max(dp[i - 1][1], dp[i - 1][2]) + a[i];
    dp[i][1] = max(dp[i - 1][0], dp[i - 1][2]) + b[i];
    dp[i][2] = max(dp[i - 1][0], dp[i - 1][1]) + c[i];
  }
  cout << max(dp[n - 1][0], max(dp[n - 1][1], dp[n - 1][2])) << "\n";
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int main() {
  ll n;
  cin >> n;
  ll a[n][3];
  ll i, j;
  for (i = 0; i < n; i++) {

    for (j = 0; j < 3; j++) {

      cin >> a[i][j];
    }
  }
  ll dp[n][n];
  dp[0][0] = a[0][0];
  dp[0][1] = a[0][1];
  dp[0][2] = a[0][2];
  for (i = 1; i < n; i++) {

    dp[i][0] = max(dp[i - 1][1], dp[i - 1][2]) + a[i][0];
    dp[i][1] = max(dp[i - 1][0], dp[i - 1][2]) + a[i][1];
    dp[i][2] = max(dp[i - 1][1], dp[i - 1][0]) + a[i][2];
  }
  cout << max(dp[n - 1][0], max(dp[n - 1][1], dp[n - 1][2]));
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int main() {
  ll n;
  cin >> n;
  ll a[n][3];
  ll i, j;
  for (i = 0; i < n; i++) {

    for (j = 0; j < 3; j++) {

      cin >> a[i][j];
    }
  }
  ll dp[n][3];
  dp[0][0] = a[0][0];
  dp[0][1] = a[0][1];
  dp[0][2] = a[0][2];
  for (i = 1; i < n; i++) {

    dp[i][0] = max(dp[i - 1][1], dp[i - 1][2]) + a[i][0];
    dp[i][1] = max(dp[i - 1][0], dp[i - 1][2]) + a[i][1];
    dp[i][2] = max(dp[i - 1][1], dp[i - 1][0]) + a[i][2];
  }
  cout << max(dp[n - 1][0], max(dp[n - 1][1], dp[n - 1][2]));
}
Problem p03162 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1e5 + 5;
int n;
int mem[MAXN][4];
int a[MAXN], b[MAXN], c[MAXN];
int solve(int i, int prev) {

  if (i == n)
    return 0;

  int &ret = mem[i][prev];

  int c1 = -1e9, c2 = -1e9, c3 = -1e9;

  if (prev != 1)
    c1 = solve(i + 1, 1) + a[i];
  if (prev != 2)
    c2 = solve(i + 1, 2) + b[i];
  if (prev != 3)
    c3 = solve(i + 1, 3) + c[i];

  ret = max(c1, max(c2, c3));

  return ret;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i] >> b[i] >> c[i];
  memset(mem, -1, sizeof(mem));
  cout << solve(0, 0);
}

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

using namespace std;

const int MAXN = 1e5 + 5;
int n;
int mem[MAXN][4];
int a[MAXN], b[MAXN], c[MAXN];
int solve(int i, int prev) {

  if (i == n)
    return 0;

  int &ret = mem[i][prev];
  if (ret != -1)
    return ret;

  int c1 = -1e9, c2 = -1e9, c3 = -1e9;

  if (prev != 1)
    c1 = solve(i + 1, 1) + a[i];
  if (prev != 2)
    c2 = solve(i + 1, 2) + b[i];
  if (prev != 3)
    c3 = solve(i + 1, 3) + c[i];

  ret = max(c1, max(c2, c3));

  return ret;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i] >> b[i] >> c[i];
  memset(mem, -1, sizeof(mem));
  cout << solve(0, 0);
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n;
  cin >> n;
  int k[3][10001];
  int dp[3][10001] = {};
  for (int i = 0; i < n; i++) {
    cin >> k[0][i + 1] >> k[1][i + 1] >> k[2][i + 1];
  }
  for (int i = 0; i < n; i++) {
    dp[0][i + 1] = max(dp[1][i], dp[2][i]) + k[0][i + 1];
    dp[1][i + 1] = max(dp[0][i], dp[2][i]) + k[1][i + 1];
    dp[2][i + 1] = max(dp[1][i], dp[0][i]) + k[2][i + 1];
  }
  cout << max(max(dp[0][n], dp[1][n]), dp[2][n]) << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n;
  cin >> n;
  int k[3][100001];
  int dp[3][100001] = {};
  for (int i = 0; i < n; i++) {
    cin >> k[0][i + 1] >> k[1][i + 1] >> k[2][i + 1];
  }
  for (int i = 0; i < n; i++) {
    dp[0][i + 1] = max(dp[1][i], dp[2][i]) + k[0][i + 1];
    dp[1][i + 1] = max(dp[0][i], dp[2][i]) + k[1][i + 1];
    dp[2][i + 1] = max(dp[1][i], dp[0][i]) + k[2][i + 1];
  }
  cout << max(max(dp[0][n], dp[1][n]), dp[2][n]) << endl;
  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;

const ll INF = 1e9 + 7;

#define rep(i, n) for (int i = 0; i < (n); i++)

vector<vector<ll>> memo(10000, vector<ll>(3, -1));
ll N;

int dp(vector<vector<int>> &ABC, ll i, ll j) {
  if (i == 0)
    return 0;

  if (memo[i][j] != -1)
    return memo[i][j];
  return memo[i][j] = max(dp(ABC, i - 1, (j + 1) % 3) + ABC[i][j],
                          dp(ABC, i - 1, (j + 2) % 3) + ABC[i][j]);
}

int main() {
  cin >> N;
  vector<vector<int>> ABC(N + 1, vector<int>(3));
  ABC[0][0] = 0, ABC[0][1] = 0, ABC[0][2] = 0;
  for (int i = 1; i <= N; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> ABC[i][j];
    }
  }
  cout << max(max(dp(ABC, N, 0), dp(ABC, N, 1)), dp(ABC, N, 2)) << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;

const ll INF = 1e9 + 7;

#define rep(i, n) for (int i = 0; i < (n); i++)

vector<vector<ll>> memo(100007, vector<ll>(3, -1));
ll N;

int dp(vector<vector<int>> &ABC, ll i, ll j) {
  if (i == 0)
    return 0;

  if (memo[i][j] != -1)
    return memo[i][j];
  return memo[i][j] = max(dp(ABC, i - 1, (j + 1) % 3) + ABC[i][j],
                          dp(ABC, i - 1, (j + 2) % 3) + ABC[i][j]);
}

int main() {
  cin >> N;
  vector<vector<int>> ABC(N + 1, vector<int>(3));
  ABC[0][0] = 0, ABC[0][1] = 0, ABC[0][2] = 0;
  for (int i = 1; i <= N; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> ABC[i][j];
    }
  }
  cout << max(max(dp(ABC, N, 0), dp(ABC, N, 1)), dp(ABC, N, 2)) << endl;
  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <cstring>
#include <iostream>
#include <string>

using namespace std;

int dp[10001][3];

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  int n;
  cin >> n;
  memset(dp, 0, sizeof(dp));

  int a[n][3];

  for (int i = 0; i < n; i++)
    for (int j = 0; j < 3; j++)
      cin >> a[i][j];

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; ++j) {
      for (int k = 0; k < 3; ++k) {
        if (j == k)
          continue;
        chmax(dp[i + 1][k], dp[i][j] + a[i][k]);
      }
    }
  }

  // for (int i=0; i<=n; i++) {
  //     cout << dp[i][0] << " " << dp[i][1] << " " << dp[i][2] << endl;
  // }

  int max = 0;
  chmax(max, dp[n][0]);
  chmax(max, dp[n][1]);
  chmax(max, dp[n][2]);
  cout << max << endl;
}

Fixed submission:
#include <cstring>
#include <iostream>
#include <string>

using namespace std;

int dp[100001][3];

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  int n;
  cin >> n;
  memset(dp, 0, sizeof(dp));

  int a[n][3];

  for (int i = 0; i < n; i++)
    for (int j = 0; j < 3; j++)
      cin >> a[i][j];

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; ++j) {
      for (int k = 0; k < 3; ++k) {
        if (j == k)
          continue;
        chmax(dp[i + 1][k], dp[i][j] + a[i][k]);
      }
    }
  }

  // for (int i=0; i<=n; i++) {
  //     cout << dp[i][0] << " " << dp[i][1] << " " << dp[i][2] << endl;
  // }

  int max = 0;
  chmax(max, dp[n][0]);
  chmax(max, dp[n][1]);
  chmax(max, dp[n][2]);
  cout << max << endl;
}
Problem p03162 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define IO                                                                     \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define int long long
using namespace std;

int dp[100005][4];
vector<vector<int>> v;
int n;

int solve(int days, int prev) {
  if (days > n)
    return 0;
  if (dp[days][prev] != -1)
    return dp[days][prev];
  int ans = INT_MIN;
  for (int i = 1; i <= 3; i++) {
    if (i == prev)
      continue;
    ans = max(ans, solve(days + 1, i) + v[days][i]);
  }
  return ans;
}

int32_t main() {
  memset(dp, -1, sizeof dp);
  cin >> n;
  v.resize(n + 1);
  for (int i = 1; i <= n; i++) {
    v[i].resize(4);
    for (int j = 1; j <= 3; j++)
      cin >> v[i][j];
  }
  cout << solve(1, 0);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define IO                                                                     \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define int long long
using namespace std;

int dp[100005][4];
vector<vector<int>> v;
int n;

int solve(int days, int prev) {
  if (days > n)
    return 0;
  if (dp[days][prev] != -1)
    return dp[days][prev];
  int ans = INT_MIN;
  for (int i = 1; i <= 3; i++) {
    if (i == prev)
      continue;
    ans = max(ans, solve(days + 1, i) + v[days][i]);
  }
  return dp[days][prev] = ans;
}

int32_t main() {
  memset(dp, -1, sizeof dp);
  cin >> n;
  v.resize(n + 1);
  for (int i = 1; i <= n; i++) {
    v[i].resize(4);
    for (int j = 1; j <= 3; j++)
      cin >> v[i][j];
  }
  cout << solve(1, 0);

  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define IO                                                                     \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define int long long
using namespace std;
const int MX = 1e5 + 5;
int dp[MX][4];

vector<vector<int>> v;
int n;

int solve(int days, int prev) {
  if (days == n + 1)
    return 0;
  if (dp[days][prev] != -1)
    return dp[days][prev];
  long long ans = -1e17;
  for (int i = 1; i <= 3; i++) {
    if (i != prev) {
      ans = max(ans, solve(days + 1, i) + v[days][i]);
    }
  }
  return dp[prev][prev] = ans;
}

int32_t main() {
  memset(dp, -1, sizeof dp);
  cin >> n;
  v.resize(n + 1);
  for (int i = 1; i <= n; i++) {
    v[i].resize(4);
    for (int j = 1; j <= 3; j++)
      cin >> v[i][j];
  }
  cout << solve(1, 0);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define IO                                                                     \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define int long long
using namespace std;
const int MX = 1e5 + 5;
int dp[MX][4];

vector<vector<int>> v;
int n;

int solve(int days, int prev) {
  if (days == n + 1)
    return 0;
  if (dp[days][prev] != -1)
    return dp[days][prev];
  long long ans = -1e17;
  for (int i = 1; i <= 3; i++) {
    if (i != prev) {
      ans = max(ans, solve(days + 1, i) + v[days][i]);
    }
  }
  return dp[days][prev] = ans;
}

int32_t main() {
  memset(dp, -1, sizeof dp);
  cin >> n;
  v.resize(n + 1);
  for (int i = 1; i <= n; i++) {
    v[i].resize(4);
    for (int j = 1; j <= 3; j++)
      cin >> v[i][j];
  }
  cout << solve(1, 0);

  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define rrep(i, n) for (int i = (n - 1); i >= 0; i--)
#define all(v) v.begin(), v.end()
#define sz(v) (int)((v).size())
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

vi a(10000), b(10000), c(10000);
vvi dp(100000, vi(3, 0));

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> a[i] >> b[i] >> c[i];
  dp[0][0] = a[0];
  dp[0][1] = b[0];
  dp[0][2] = c[0];
  for (int i = 1; i < N; i++) {
    dp[i][0] = max(dp[i - 1][1], dp[i - 1][2]) + a[i];
    dp[i][1] = max(dp[i - 1][0], dp[i - 1][2]) + b[i];
    dp[i][2] = max(dp[i - 1][0], dp[i - 1][1]) + c[i];
  }
  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>
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define rrep(i, n) for (int i = (n - 1); i >= 0; i--)
#define all(v) v.begin(), v.end()
#define sz(v) (int)((v).size())
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int N_MAX = 100000;
vi a(N_MAX), b(N_MAX), c(N_MAX);
vvi dp(N_MAX, vi(3, 0));

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> a[i] >> b[i] >> c[i];
  dp[0][0] = a[0];
  dp[0][1] = b[0];
  dp[0][2] = c[0];
  for (int i = 1; i < N; i++) {
    dp[i][0] = max(dp[i - 1][1], dp[i - 1][2]) + a[i];
    dp[i][1] = max(dp[i - 1][0], dp[i - 1][2]) + b[i];
    dp[i][2] = max(dp[i - 1][0], dp[i - 1][1]) + c[i];
  }
  int ans = max(dp[N - 1][0], max(dp[N - 1][1], dp[N - 1][2]));
  cout << ans << endl;
  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define INF (1e10)
#define mp make_pair
#define pb push_back
#define A 0
#define B 1
#define C 2
using namespace std;
typedef long long int lo;
lo dp[10000005][3], arr[10000005][3], n;
lo f(lo i, lo c) {
  if (i == n)
    return dp[i][c] = arr[i][c];
  if (dp[i][c] != 0)
    return dp[i][c];
  if (c == A) {
    return dp[i][c] = max(f(i + 1, B) + arr[i][c], f(i + 1, C) + arr[i][c]);
  } else if (c == B) {
    return dp[i][c] = max(f(i + 1, A) + arr[i][c], f(i + 1, C) + arr[i][c]);
  } else {
    return dp[i][c] = max(f(i + 1, A) + arr[i][c], f(i + 1, B) + arr[i][c]);
  }
}
int main() {
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  cin >> n;
  for (lo i = 1; i <= n; i++) {
    lo a, b, c;
    cin >> a >> b >> c;
    arr[i][A] = a;
    arr[i][B] = b;
    arr[i][C] = c;
  }
  cout << max(f(1, A), max(f(1, B), f(1, C)));
}


Fixed submission:
#include <bits/stdc++.h>
#define INF (1e10)
#define mp make_pair
#define pb push_back
#define A 0
#define B 1
#define C 2
using namespace std;
typedef long long int lo;
lo dp[10000005][3], arr[10000005][3], n;
lo f(lo i, lo c) {
  if (i == n)
    return dp[i][c] = arr[i][c];
  if (dp[i][c] != 0)
    return dp[i][c];
  if (c == A) {
    return dp[i][c] = max(f(i + 1, B) + arr[i][c], f(i + 1, C) + arr[i][c]);
  } else if (c == B) {
    return dp[i][c] = max(f(i + 1, A) + arr[i][c], f(i + 1, C) + arr[i][c]);
  } else {
    return dp[i][c] = max(f(i + 1, A) + arr[i][c], f(i + 1, B) + arr[i][c]);
  }
}
int main() {
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  cin >> n;
  for (lo i = 1; i <= n; i++) {
    lo a, b, c;
    cin >> a >> b >> c;
    arr[i][A] = a;
    arr[i][B] = b;
    arr[i][C] = c;
  }
  cout << max(f(1, A), max(f(1, B), f(1, C)));
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const ll mod = 1e9 + 7;
#define rep(i, a, b) for (ll i = a; i < b; i++)

void swap(ll &a, ll &b) {
  ll t = a;
  a = b;
  b = t;
}

ll min(ll a, ll b) {
  if (a < b)
    return a;
  return b;
}

ll max(ll a, ll b) {
  if (a > b)
    return a;
  return b;
}

ll pow(ll a, ll b) {
  a %= mod;
  ll ans = 1;
  while (b > 0) {
    if (b & 1)
      ans = ans * a % mod;
    a = a * a % mod;
    b /= 2;
  }
  return ans;
}

int main() {

  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  /// 1 2 3 4  ....
  ll n;
  cin >> n;
  ll a[n][3];
  for (int i = 0; i < n; i++) {
    cin >> a[i][0] >> a[i][1] >> a[i][2];
  }
  ll dp[3][3];
  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] = max(dp[i - 1][1] + a[i][0], dp[i - 1][2] + a[i][0]);
    dp[i][1] = max(dp[i - 1][0] + a[i][1], dp[i - 1][2] + a[i][1]);
    dp[i][2] = max(dp[i - 1][0] + a[i][2], dp[i - 1][1] + a[i][2]);
  }
  cout << max(dp[n - 1][0], max(dp[n - 1][1], dp[n - 1][2]));
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const ll mod = 1e9 + 7;
#define rep(i, a, b) for (ll i = a; i < b; i++)

void swap(ll &a, ll &b) {
  ll t = a;
  a = b;
  b = t;
}

ll min(ll a, ll b) {
  if (a < b)
    return a;
  return b;
}

ll max(ll a, ll b) {
  if (a > b)
    return a;
  return b;
}

ll pow(ll a, ll b) {
  a %= mod;
  ll ans = 1;
  while (b > 0) {
    if (b & 1)
      ans = ans * a % mod;
    a = a * a % mod;
    b /= 2;
  }
  return ans;
}

int main() {

  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  /// 1 2 3 4  ....
  ll n;
  cin >> n;
  ll a[n][3];
  for (int i = 0; i < n; i++) {
    cin >> a[i][0] >> a[i][1] >> a[i][2];
  }
  ll dp[n][3];
  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] = max(dp[i - 1][1] + a[i][0], dp[i - 1][2] + a[i][0]);
    dp[i][1] = max(dp[i - 1][0] + a[i][1], dp[i - 1][2] + a[i][1]);
    dp[i][2] = max(dp[i - 1][0] + a[i][2], dp[i - 1][1] + a[i][2]);
  }
  cout << max(dp[n - 1][0], max(dp[n - 1][1], dp[n - 1][2]));
}
Problem p03162 (C++)

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

const int INF = 2000000000;
const int N_MAX = 10010;

int main() {
  int n;
  cin >> n;
  vector<int> A(N_MAX), B(N_MAX), C(N_MAX);
  for (int i = 0; i < n; i++) {
    cin >> A[i] >> B[i] >> C[i];
  }

  // dp[i][j]: i日目にjをやった時の幸福度
  // j: 0 => A, 1 => B, 2 => C
  int dp[N_MAX][3] = {};

  for (int i = 0; i < n; i++) {
    dp[i + 1][0] = max(dp[i][1] + B[i], dp[i][2] + C[i]);
    dp[i + 1][1] = max(dp[i][0] + A[i], dp[i][2] + C[i]);
    dp[i + 1][2] = max(dp[i][0] + A[i], dp[i][1] + B[i]);
  }

  int ans;
  ans = max({dp[n][0], dp[n][1], dp[n][2]});

  cout << ans << endl;

  return 0;
}


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

const int INF = 2000000000;
const int N_MAX = 100100;

int main() {
  int n;
  cin >> n;
  vector<int> A(N_MAX), B(N_MAX), C(N_MAX);
  for (int i = 0; i < n; i++) {
    cin >> A[i] >> B[i] >> C[i];
  }

  // dp[i][j]: i日目にjをやった時の幸福度
  // j: 0 => A, 1 => B, 2 => C
  int dp[N_MAX][3] = {};

  for (int i = 0; i < n; i++) {
    dp[i + 1][0] = max(dp[i][1] + B[i], dp[i][2] + C[i]);
    dp[i + 1][1] = max(dp[i][0] + A[i], dp[i][2] + C[i]);
    dp[i + 1][2] = max(dp[i][0] + A[i], dp[i][1] + B[i]);
  }

  int ans;
  ans = max({dp[n][0], dp[n][1], dp[n][2]});

  cout << ans << endl;

  return 0;
}