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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define all(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
// ll gcd(ll a, ll b){return b?gcd(b,a%b):a;}
// ll lcm(ll x, ll y) {return x / gcd(x, y) * y;}
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const ll mod = 1e9 + 7;
const ll inf = 1LL << 60;
const long double pi = 3.14159265358979323846;

// ****************************************CODE***************************************//

ll n, a[100100][3];
ll dp[100100][3];

int main() {
  cin >> n;
  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;
        dp[i + 1][k] = max(dp[i + 1][i], dp[i][j] + a[i][k]);
      }
    }
  }

  ll res = 0;
  for (int i = 0; i < 3; i++) {
    res = max(res, dp[n][i]);
  }

  cout << res << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define all(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
// ll gcd(ll a, ll b){return b?gcd(b,a%b):a;}
// ll lcm(ll x, ll y) {return x / gcd(x, y) * y;}
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const ll mod = 1e9 + 7;
const ll inf = 1LL << 60;
const long double pi = 3.14159265358979323846;

// ****************************************CODE***************************************//

ll n, a[100100][3];
ll dp[100100][3];

int main() {
  cin >> n;
  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;
        dp[i + 1][k] = max(dp[i + 1][k], dp[i][j] + a[i][k]);
      }
    }
  }

  ll res = 0;
  for (int i = 0; i < 3; i++) {
    res = max(res, dp[n][i]);
  }

  cout << res << endl;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define ll long long
#define pll pair<ll, ll>
#define vll vector<pll>
#define vl vector<ll>
#define f first
#define s second
#define N 50000
#define pb push_back
#define mp make_pair
#define MAP map<ll, ll>
#define INF 1000000000
#define mod 1000000007
#define foo(i, a, b) for (ll i = (a); i <= (b); i++)
#define rep(i, a, b) for (ll i = (a); i >= (b); i--)
#define sl set<ll>
#define sll set<pll>
#define ub upper_bound
#define lb lower_bound
using namespace std;
ll a[100000];
ll dp[1000][3];
int main() {
  fastio;
  ll n, a, b, c;
  vl A, B, C;
  cin >> n;
  foo(i, 0, n - 1) {
    cin >> a >> b >> c;
    A.pb(a);
    B.pb(b);
    C.pb(c);
  }
  foo(i, 1, n) {
    dp[i][0] = max(dp[i - 1][1], dp[i - 1][2]) + A[i - 1];
    dp[i][1] = max(dp[i - 1][0], dp[i - 1][2]) + B[i - 1];
    dp[i][2] = max(dp[i - 1][0], dp[i - 1][1]) + C[i - 1];
  }
  cout << max(dp[n][0], max(dp[n][1], dp[n][2]));
}

Fixed submission:
#include <bits/stdc++.h>
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define ll long long
#define pll pair<ll, ll>
#define vll vector<pll>
#define vl vector<ll>
#define f first
#define s second
#define N 50000
#define pb push_back
#define mp make_pair
#define MAP map<ll, ll>
#define INF 1000000000
#define mod 1000000007
#define foo(i, a, b) for (ll i = (a); i <= (b); i++)
#define rep(i, a, b) for (ll i = (a); i >= (b); i--)
#define sl set<ll>
#define sll set<pll>
#define ub upper_bound
#define lb lower_bound
using namespace std;
ll a[100000];
ll dp[100001][3];
int main() {
  fastio;
  ll n, a, b, c;
  vl A, B, C;
  cin >> n;
  foo(i, 0, n - 1) {
    cin >> a >> b >> c;
    A.pb(a);
    B.pb(b);
    C.pb(c);
  }
  foo(i, 1, n) {
    dp[i][0] = max(dp[i - 1][1], dp[i - 1][2]) + A[i - 1];
    dp[i][1] = max(dp[i - 1][0], dp[i - 1][2]) + B[i - 1];
    dp[i][2] = max(dp[i - 1][0], dp[i - 1][1]) + C[i - 1];
  }
  cout << max(dp[n][0], max(dp[n][1], dp[n][2]));
}
Problem p03162 (C++)

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

#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define sz(x) int((x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rp(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep(i, x, n) for (int i = (x); i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)

#define TRACE(x...)
#define PRINT(x...)
#define WATCH(x) TRACE(cout << #x << " = " << x << endl)

using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> vll;
typedef vector<vll> vvll;

const int MAXN = 10010;
ll dp[MAXN][3];
ll mat[MAXN][3];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  rp(i, n) rp(j, 3) cin >> mat[i][j];

  rp(j, 3) dp[0][j] = mat[0][j];
  rep(i, 1, n) {
    rp(j, 3) {
      dp[i][j] =
          max(dp[i - 1][(j + 1) % 3], dp[i - 1][(j + 2) % 3]) + mat[i][j];
    }
  }

  ll resp = 0;
  rp(j, 3) resp = max(dp[n - 1][j], resp);
  cout << resp << '\n';

  return 0;
}

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

#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define sz(x) int((x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rp(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep(i, x, n) for (int i = (x); i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)

#define TRACE(x...)
#define PRINT(x...)
#define WATCH(x) TRACE(cout << #x << " = " << x << endl)

using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> vll;
typedef vector<vll> vvll;

const int MAXN = 100010;
ll dp[MAXN][3];
ll mat[MAXN][3];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  rp(i, n) rp(j, 3) cin >> mat[i][j];

  rp(j, 3) dp[0][j] = mat[0][j];
  rep(i, 1, n) {
    rp(j, 3) {
      dp[i][j] =
          max(dp[i - 1][(j + 1) % 3], dp[i - 1][(j + 2) % 3]) + mat[i][j];
    }
  }

  ll resp = 0;
  rp(j, 3) resp = max(dp[n - 1][j], resp);
  cout << resp << '\n';

  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;

#define db(x) cerr << #x << " == " << x << endl
#define all(container) container.begin(), container.end()
#define mp(i, j) make_pair(i, j)
#define pb push_back

typedef pair<int, int> pii;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<pii> vii;

template <typename T> void print_vector_debug(const T &t) {
  cout << "[DEBUG] VECTOR:";
  for (auto i = t.cbegin(); i != t.cend(); ++i) {
    if ((i + 1) != t.cend()) {
      cout << *i << " ";
    } else {
      cout << *i << endl;
    }
  }
}

#define MAXN 100005

int N;
int vals[MAXN][3];
int vis[MAXN][4], dp[MAXN][4];

int solve(int idx, int last) {
  if (idx == N) {
    return 0;
  }

  if (vis[idx][last]) {
    return dp[idx][last];
  }

  int res = 0;

  for (int i = 0; i < 3; i++) {
    if (i != last) {
      res = max(res, solve(idx + 1, i) + vals[idx][i]);
    }
  }

  dp[idx][last] = res;
  return res;
}

int main() {
  scanf("%d", &N);

  for (int i = 0; i < N; i++) {
    scanf("%d%d%d", &vals[i][0], &vals[i][1], &vals[i][2]);
  }

  printf("%d\n", solve(0, 3));

  return 0;
}


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

#define db(x) cerr << #x << " == " << x << endl
#define all(container) container.begin(), container.end()
#define mp(i, j) make_pair(i, j)
#define pb push_back

typedef pair<int, int> pii;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<pii> vii;

template <typename T> void print_vector_debug(const T &t) {
  cout << "[DEBUG] VECTOR:";
  for (auto i = t.cbegin(); i != t.cend(); ++i) {
    if ((i + 1) != t.cend()) {
      cout << *i << " ";
    } else {
      cout << *i << endl;
    }
  }
}

#define MAXN 100005

int N;
int vals[MAXN][3];
int vis[MAXN][4], dp[MAXN][4];

int solve(int idx, int last) {
  if (idx == N) {
    return 0;
  }

  if (vis[idx][last]) {
    return dp[idx][last];
  }
  vis[idx][last] = 1;

  int res = 0;

  for (int i = 0; i < 3; i++) {
    if (i != last) {
      res = max(res, solve(idx + 1, i) + vals[idx][i]);
    }
  }

  dp[idx][last] = res;
  return res;
}

int main() {
  scanf("%d", &N);

  for (int i = 0; i < N; i++) {
    scanf("%d%d%d", &vals[i][0], &vals[i][1], &vals[i][2]);
  }

  printf("%d\n", solve(0, 3));

  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define fi first
#define sec second
#define MOD 1000000007
#define pb push_back
#define test cout << "debuged\n";
#define INF 10000000000
#define si 1000000
#define debug(x) cout << x << "\n";

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output", "w", stdout);
#endif
  ll n;
  cin >> n;
  vector<ll> a(n + 1), b(n + 1), c(n + 1);
  for (ll i = 1; i <= n; i++)
    cin >> a[i] >> b[i] >> c[i];
  ll dp[n + 1][3];
  dp[1][0] = a[1];
  dp[1][1] = b[1];
  dp[1][2] = c[1];
  for (ll i = 2; 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][0], max(dp[n][1], dp[n][2]));
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define fi first
#define sec second
#define MOD 1000000007
#define pb push_back
#define test cout << "debuged\n";
#define INF 10000000000
#define si 1000000
#define debug(x) cout << x << "\n";

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n;
  cin >> n;
  vector<ll> a(n + 1), b(n + 1), c(n + 1);
  for (ll i = 1; i <= n; i++)
    cin >> a[i] >> b[i] >> c[i];
  ll dp[n + 1][3];
  dp[1][0] = a[1];
  dp[1][1] = b[1];
  dp[1][2] = c[1];
  for (ll i = 2; 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][0], max(dp[n][1], dp[n][2]));
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, n) for (int i = 1; i <= n; ++i)
typedef long long ll;
const int inf = 100100100;
const ll INF = 1LL << 60;
const int MOD = (int)1e9 + 7;

// aとbのうち大きい値をaとして採用する
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  int N_MAX = 11000;
  int n;
  cin >> n;
  vector<int> a(N_MAX);
  vector<int> b(N_MAX);
  vector<int> c(N_MAX);
  rep(i, n) cin >> a[i] >> b[i] >> c[i];
  vector<vector<ll>> dp(N_MAX, vector<ll>(3, 0));

  rep(i, n) {
    chmax(dp[i + 1][0], dp[i][1] + a[i]);
    chmax(dp[i + 1][0], dp[i][2] + a[i]);
    chmax(dp[i + 1][1], dp[i][0] + b[i]);
    chmax(dp[i + 1][1], dp[i][2] + b[i]);
    chmax(dp[i + 1][2], dp[i][0] + c[i]);
    chmax(dp[i + 1][2], dp[i][1] + c[i]);
  }

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


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, n) for (int i = 1; i <= n; ++i)
typedef long long ll;
const int inf = 100100100;
const ll INF = 1LL << 60;
const int MOD = (int)1e9 + 7;

// aとbのうち大きい値をaとして採用する
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  int N_MAX = 110000;
  int n;
  cin >> n;
  vector<int> a(N_MAX);
  vector<int> b(N_MAX);
  vector<int> c(N_MAX);
  rep(i, n) cin >> a[i] >> b[i] >> c[i];
  vector<vector<ll>> dp(N_MAX, vector<ll>(3, 0));

  rep(i, n) {
    chmax(dp[i + 1][0], dp[i][1] + a[i]);
    chmax(dp[i + 1][0], dp[i][2] + a[i]);
    chmax(dp[i + 1][1], dp[i][0] + b[i]);
    chmax(dp[i + 1][1], dp[i][2] + b[i]);
    chmax(dp[i + 1][2], dp[i][0] + c[i]);
    chmax(dp[i + 1][2], dp[i][1] + c[i]);
  }

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

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

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


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

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

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

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> A, B, C;
  vector<long long> Ma, Mb, Mc;

  for (int i = 0; i < N; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    A.push_back(a);
    B.push_back(b);
    C.push_back(c);
  }

  Ma.push_back(A[0]);
  Mb.push_back(B[0]);
  Mc.push_back(C[0]);
  for (int i = 1; i < N; i++) {
    Ma[i] = max(Mb[i - 1] + A[i], Mc[i - 1] + A[i]);
    Mb[i] = max(Ma[i - 1] + B[i], Mc[i - 1] + B[i]);
    Mc[i] = max(Ma[i - 1] + C[i], Mb[i - 1] + C[i]);
  }

  cout << max(Ma[N - 1], max(Mb[N - 1], Mc[N - 1]));

  return 0;
}


Fixed submission:
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> A, B, C;
  vector<long long> Ma, Mb, Mc;

  for (int i = 0; i < N; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    A.push_back(a);
    B.push_back(b);
    C.push_back(c);
  }

  Ma.push_back(A[0]);
  Mb.push_back(B[0]);
  Mc.push_back(C[0]);
  for (int i = 1; i < N; i++) {
    Ma.push_back(max(Mb[i - 1] + A[i], Mc[i - 1] + A[i]));
    Mb.push_back(max(Ma[i - 1] + B[i], Mc[i - 1] + B[i]));
    Mc.push_back(max(Ma[i - 1] + C[i], Mb[i - 1] + C[i]));
  }

  cout << max(Ma[N - 1], max(Mb[N - 1], Mc[N - 1]));

  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <math.h>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>

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

#define fio ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define mod 1000000007
#define ll long long
#define pb emplace_back
#define mp make_pair
#define sf scanf
#define pf printf
#define F first
#define S second

//------------------------------------------------------------------------------
using namespace std;

ll int n, dp[100010][4], a[100010][4] = {0};

ll int vacation(ll int day, ll int last_activity) {
  if (dp[day][last_activity] != -1)
    return dp[day][last_activity];
  if (day >= n)
    return 0;

  ll int happy = -999999999999;
  for (int i = 0; i < 3; i++) {
    if (i != last_activity)
      happy = max(happy, a[day][i] + vacation(day + 1, i));
  }

  return happy;
}

int main() {
  fio;
  memset(dp, -1, sizeof(dp));
  ll int x;
  sf("%lld", &n);

  for (ll int i = 0; i < n; i++)
    for (int j = 0; j < 3; j++)
      sf("%lld", &a[i][j]);

  x = vacation(0, 5);

  pf("%lld\n", x);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <math.h>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>

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

#define fio ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define mod 1000000007
#define ll long long
#define pb emplace_back
#define mp make_pair
#define sf scanf
#define pf printf
#define F first
#define S second

//------------------------------------------------------------------------------
using namespace std;

ll int n, dp[100010][4], a[100010][4] = {0};

ll int vacation(ll int day, ll int last_activity) {
  if (dp[day][last_activity] != -1)
    return dp[day][last_activity];
  if (day >= n)
    return 0;

  ll int happy = -999999999999;
  for (int i = 0; i < 3; i++) {
    if (i != last_activity)
      happy = max(happy, a[day][i] + vacation(day + 1, i));
  }

  return dp[day][last_activity] = happy;
}

int main() {
  fio;
  memset(dp, -1, sizeof(dp));
  ll int x;
  sf("%lld", &n);

  for (ll int i = 0; i < n; i++)
    for (int j = 0; j < 3; j++)
      sf("%lld", &a[i][j]);

  x = vacation(0, 5);

  pf("%lld\n", x);

  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int func(int n, int m, int A[][3], int dp[][3]) {
  if (n < 0)
    return 0;
  if (n == 0)
    return 0;
  int maxi = 0;
  for (int i = 0; i <= 2; i++) {
    if (i != m) {
      int x = A[n - 1][i] + func(n - 1, i, A, dp);
      if (x > maxi)
        maxi = x;
    }
  }
  return dp[n][m] = maxi;
}
int main() {
  int n;
  cin >> n;
  int A[n][3];
  for (int i = 0; i < n; i++)
    cin >> A[i][0] >> A[i][1] >> A[i][2];
  int dp[n + 1][3];
  for (int i = 0; i <= n; i++)
    for (int j = 0; j < 3; j++)
      dp[i][j] = -1;
  dp[n][0] = max(A[n - 1][1] + func(n - 1, 1, A, dp),
                 A[n - 1][2] + func(n - 1, 2, A, dp));
  dp[n][1] = max(A[n - 1][0] + func(n - 1, 0, A, dp),
                 A[n - 1][2] + func(n - 1, 2, A, dp));
  dp[n][2] = max(A[n - 1][0] + func(n - 1, 0, A, dp),
                 A[n - 1][1] + func(n - 1, 1, A, dp));
  cout << max({dp[n][0], dp[n][1], dp[n][2]}) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int func(int n, int m, int A[][3], int dp[][3]) {
  if (n < 0)
    return 0;
  if (n == 0)
    return 0;
  if (dp[n][m] != -1)
    return dp[n][m];
  int maxi = 0;
  for (int i = 0; i <= 2; i++) {
    if (i != m) {
      int x = A[n - 1][i] + func(n - 1, i, A, dp);
      if (x > maxi)
        maxi = x;
    }
  }
  return dp[n][m] = maxi;
}
int main() {
  int n;
  cin >> n;
  int A[n][3];
  for (int i = 0; i < n; i++)
    cin >> A[i][0] >> A[i][1] >> A[i][2];
  int dp[n + 1][3];
  for (int i = 0; i <= n; i++)
    for (int j = 0; j < 3; j++)
      dp[i][j] = -1;
  dp[n][0] = max(A[n - 1][1] + func(n - 1, 1, A, dp),
                 A[n - 1][2] + func(n - 1, 2, A, dp));
  dp[n][1] = max(A[n - 1][0] + func(n - 1, 0, A, dp),
                 A[n - 1][2] + func(n - 1, 2, A, dp));
  dp[n][2] = max(A[n - 1][0] + func(n - 1, 0, A, dp),
                 A[n - 1][1] + func(n - 1, 1, A, dp));
  cout << max({dp[n][0], dp[n][1], dp[n][2]}) << endl;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long a[100001], b[100001], c[100001], dp[1001][1001], n;
long long cal(long long idx, long long v) {
  // cout<<dp[idx][v]<<' '<<idx<<endl;
  if (idx > n)
    return 0;
  if (dp[idx][v] != -1)
    return dp[idx][v];
  else
    dp[idx][v] = 0;
  if (v == 0)
    dp[idx][v] = max(cal(idx + 1, 1) + a[idx],
                     max(cal(idx + 1, 2) + b[idx], cal(idx + 1, 3) + c[idx]));
  if (v == 1)
    dp[idx][v] =
        max(cal(idx + 1, v + 1) + b[idx], cal(idx + 1, v + 2) + c[idx]);
  if (v == 2)
    dp[idx][v] =
        max(cal(idx + 1, v - 1) + a[idx], cal(idx + 1, v + 1) + c[idx]);
  if (v == 3)
    dp[idx][v] =
        max(cal(idx + 1, v - 2) + a[idx], cal(idx + 1, v - 1) + b[idx]);
  return dp[idx][v];
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i] >> b[i] >> c[i];
  for (int i = 1; i <= n; i++)
    for (int j = 0; j <= 3; j++)
      dp[i][j] = -1;
  cout << cal(1, 0);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long a[100001], b[100001], c[100001], dp[100001][3], n;
long long cal(long long idx, long long v) {
  // cout<<dp[idx][v]<<' '<<idx<<endl;
  if (idx > n)
    return 0;
  if (dp[idx][v] != -1)
    return dp[idx][v];
  else
    dp[idx][v] = 0;
  if (v == 0)
    dp[idx][v] = max(cal(idx + 1, 1) + a[idx],
                     max(cal(idx + 1, 2) + b[idx], cal(idx + 1, 3) + c[idx]));
  if (v == 1)
    dp[idx][v] =
        max(cal(idx + 1, v + 1) + b[idx], cal(idx + 1, v + 2) + c[idx]);
  if (v == 2)
    dp[idx][v] =
        max(cal(idx + 1, v - 1) + a[idx], cal(idx + 1, v + 1) + c[idx]);
  if (v == 3)
    dp[idx][v] =
        max(cal(idx + 1, v - 2) + a[idx], cal(idx + 1, v - 1) + b[idx]);
  return dp[idx][v];
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i] >> b[i] >> c[i];
  for (int i = 1; i <= n; i++)
    for (int j = 0; j <= 3; j++)
      dp[i][j] = -1;
  cout << cal(1, 0);
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define MOD 1000000007
#define PI 3.14159265358979323846
#define INF 1e9 + 5
using namespace std;

int solve() {
  int n;
  cin >> n;

  vector<int> dp(3);
  for (int day = 0; day < n; day++) {
    vector<int> new_dp(3);
    vector<int> a(3);
    for (int i = 0; i < 3; i++) {
      cin >> a[i];
    }

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (i != j) {
          new_dp[j] = max(new_dp[j], dp[i] + a[j]);
        }
      }
    }
    dp = new_dp;
  }

  return max({dp[0], dp[1], dp[2]});
}

int main() {
  std::ios_base::sync_with_stdio(0);
  std::cin.tie(0);
  /*
  int t;
  cin >> t;

  while (t--)
  */
  cout << solve() << "\n";

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define MOD 1000000007
#define PI 3.14159265358979323846
#define INF 1e9 + 5
using namespace std;

int solve() {
  int n;
  cin >> n;

  vector<int> dp(3);
  for (int day = 0; day < n; day++) {
    vector<int> new_dp(3);
    vector<int> a(3);
    for (int i = 0; i < 3; i++) {
      cin >> a[i];
    }

    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        if (i != j) {
          new_dp[j] = max(new_dp[j], dp[i] + a[j]);
        }
      }
    }
    dp = new_dp;
  }

  return max({dp[0], dp[1], dp[2]});
}

int main() {
  std::ios_base::sync_with_stdio(0);
  std::cin.tie(0);
  /*
  int t;
  cin >> t;

  while (t--)
  */
  cout << solve() << "\n";

  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;

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 main() {
  int n;
  cin >> n;
  vector<vector<int>> data(n, vector<int>(3));
  REP(i, n) {
    REP(j, 3) { cin >> data[i][j]; }
  }
  vector<vector<ll>> dp(100010, vector<ll>(3));

  REP(i, n) {
    REP(j, 3) {
      REP(k, 3) {
        if (j != k)
          chmax(dp[i + 1][k], dp[i][j] + data[i][k]);
      }
    }
  }
  ll ans = 0;
  REP(i, n) ans = max(ans, dp[n][i]);
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;

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 main() {
  int n;
  cin >> n;
  vector<vector<int>> data(n, vector<int>(3));
  REP(i, n) {
    REP(j, 3) { cin >> data[i][j]; }
  }
  vector<vector<ll>> dp(100010, vector<ll>(3));

  REP(i, n) {
    REP(j, 3) {
      REP(k, 3) {
        if (j != k)
          chmax(dp[i + 1][k], dp[i][j] + data[i][k]);
      }
    }
  }
  ll ans = 0;
  REP(i, 3) ans = max(ans, dp[n][i]);
  cout << ans << endl;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
// Author:: Pham Ngoc Minh- Chuyen Thai Binh
#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define ll long long
#define fl float
#define db double
#define ii pair<ll, ll>
#define pii pair<ii, ii>
#define ford(i, a, b) for (int i = a; i <= b; i++)
#define foru(i, a, b) for (int i = a; i >= b; i--)
#define forv(i, a, b) for (int i = a; i < b; i++)
#define oo 10000000000001
#define ooo -1000000000001
#define bost                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);
int const maxn = 1e4 + 5;
int n, dp[5][maxn], a, b, c;
int main() {
  bost cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a >> b >> c;
    dp[1][i] = max(dp[2][i - 1], dp[3][i - 1]) + a;
    dp[2][i] = max(dp[1][i - 1], dp[3][i - 1]) + b;
    dp[3][i] = max(dp[1][i - 1], dp[2][i - 1]) + c;
  }
  cout << max(dp[1][n], max(dp[2][n], dp[3][n]));
  return 0;
}
// I love Chuyen Thai Binh Verry much ! Now, you alaways in my code of me <3
// Chuyen Thai Binh La Nha :>>


Fixed submission:
// Author:: Pham Ngoc Minh- Chuyen Thai Binh
#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define ll long long
#define fl float
#define db double
#define ii pair<ll, ll>
#define pii pair<ii, ii>
#define ford(i, a, b) for (int i = a; i <= b; i++)
#define foru(i, a, b) for (int i = a; i >= b; i--)
#define forv(i, a, b) for (int i = a; i < b; i++)
#define oo 10000000000001
#define ooo -1000000000001
#define bost                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);
int const maxn = 1e5 + 5;
int n, dp[5][maxn], a, b, c;
int main() {
  bost cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a >> b >> c;
    dp[1][i] = max(dp[2][i - 1], dp[3][i - 1]) + a;
    dp[2][i] = max(dp[1][i - 1], dp[3][i - 1]) + b;
    dp[3][i] = max(dp[1][i - 1], dp[2][i - 1]) + c;
  }
  cout << max(dp[1][n], max(dp[2][n], dp[3][n]));
  return 0;
}
// I love Chuyen Thai Binh Verry much ! Now, you alaways in my code of me <3
// Chuyen Thai Binh La Nha :>>
Problem p03162 (Python)

Buggy submission (status: Time Limit Exceeded):
import sys

sys.setrecursionlimit(10**6)

n = int(input())
abc = [list(map(int, input().split())) for _ in range(n)]


def recur(i, last_index, v):
    if i == n:
        return v
    res = -1
    for j in range(3):
        if j == last_index:
            continue
        res = max(res, recur(i + 1, j, v + abc[i][j]))
    return res


print(recur(0, -1, 0))


Fixed submission:
import sys

sys.setrecursionlimit(10**6)

n = int(input())
abc = [list(map(int, input().split())) for _ in range(n)]

dp = [[0, 0, 0] for _ in range(n)]
dp[0] = abc[0]
for i in range(1, n):
    dp[i][0] = abc[i][0] + max(dp[i - 1][1], dp[i - 1][2])
    dp[i][1] = abc[i][1] + max(dp[i - 1][0], dp[i - 1][2])
    dp[i][2] = abc[i][2] + max(dp[i - 1][0], dp[i - 1][1])
print(max(dp[-1]))
Problem p03162 (Python)

Buggy submission (status: Time Limit Exceeded):
import numpy as np

N = int(input())

values = np.array([list(map(int, input().split(" "))) for _ in range(N)])
dp = np.zeros(shape=(N, 3), dtype=int)

dp[0] = values[0]

for i in range(1, N):
    dp[i][0] = np.max(values[i][0] + dp[i - 1][1:3])
    dp[i][1] = np.max(values[i][1] + dp[i - 1][0::2])
    dp[i][2] = np.max(values[i][2] + dp[i - 1][:2])

print(np.max(dp[N - 1]))


Fixed submission:
import numpy as np

N = int(input())

values = np.array([list(map(int, input().split(" "))) for _ in range(N)])
dp = np.zeros(shape=(N, 3), dtype=int)

dp[0] = values[0]

for i in range(1, N):
    prev = dp[i - 1]
    dp[i][0] = values[i][0] + max(prev[1], prev[2])
    dp[i][1] = values[i][1] + max(prev[0], prev[2])
    dp[i][2] = values[i][2] + max(prev[0], prev[1])

print(np.max(dp[N - 1]))
Problem p03162 (Python)

Buggy submission (status: Runtime Error):
import sys

sys.setrecursionlimit(10**7)

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(readline())
ABC = [[int(x) for x in readline().split()] for _ in range(N)]

dp = [[0] * 3 for _ in range(10 * 5 + 10)]

for i in range(N):
    for j in range(3):
        for k in range(3):
            if j != k:
                dp[i + 1][j] = max(dp[i + 1][j], dp[i][k] + ABC[i][k])

ans = max(dp[N])
print(ans)


Fixed submission:
import sys

sys.setrecursionlimit(10**7)

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(readline())
ABC = [[int(x) for x in readline().split()] for _ in range(N)]

dp = [[0] * 3 for _ in range(10**5 + 10)]

for i in range(N):
    for j in range(3):
        for k in range(3):
            if j != k:
                dp[i + 1][j] = max(dp[i + 1][j], dp[i][k] + ABC[i][k])

ans = max(dp[N])
print(ans)
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <vector>
using namespace std;
const int C = 150001;

int n, k, a, b, c, dp[2][C];

int abs(int a) { return (a > 0) ? (a) : (-a); }

int main() {
  int j;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d %d %d", &a, &b, &c);
    dp[0][i] = max(dp[1][i - 1], dp[2][i - 1]) + a;
    dp[1][i] = max(dp[0][i - 1], dp[2][i - 1]) + b;
    dp[2][i] = max(dp[1][i - 1], dp[0][i - 1]) + c;
  }
  printf("%d\n", max(dp[2][n], max(dp[1][n], dp[0][n])));

  return 0;
}

Fixed submission:
#include <stdio.h>
#include <vector>
using namespace std;
const int C = 150001;

int n, k, a, b, c, dp[3][C];

int abs(int a) { return (a > 0) ? (a) : (-a); }

int main() {
  int j;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d %d %d", &a, &b, &c);
    dp[0][i] = max(dp[1][i - 1], dp[2][i - 1]) + a;
    dp[1][i] = max(dp[0][i - 1], dp[2][i - 1]) + b;
    dp[2][i] = max(dp[1][i - 1], dp[0][i - 1]) + c;
  }
  printf("%d\n", max(dp[2][n], max(dp[1][n], dp[0][n])));

  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(void) {
  int N;
  cin >> N;
  ll dp[11000][2][2][2];
  for (int i = 0; i < 11000; i++)
    for (int j = 0; j < 2; j++)
      for (int k = 0; k < 2; k++)
        for (int l = 0; l < 2; l++)
          dp[i][j][k][l] = 0;

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

  for (int i = 1; i < N; i++) {
    dp[i + 1][1][0][0] = max(dp[i][0][1][0], dp[i][0][0][1]) + a[i + 1];
    dp[i + 1][0][1][0] = max(dp[i][1][0][0], dp[i][0][0][1]) + b[i + 1];
    dp[i + 1][0][0][1] = max(dp[i][1][0][0], dp[i][0][1][0]) + c[i + 1];
  }

  ll ans = max(dp[N][1][0][0], dp[N][0][1][0]);
  ans = max(ans, dp[N][0][0][1]);

  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(void) {
  int N;
  cin >> N;
  ll dp[110000][2][2][2];
  for (int i = 0; i < 110000; i++)
    for (int j = 0; j < 2; j++)
      for (int k = 0; k < 2; k++)
        for (int l = 0; l < 2; l++)
          dp[i][j][k][l] = 0;

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

  for (int i = 1; i < N; i++) {
    dp[i + 1][1][0][0] = max(dp[i][0][1][0], dp[i][0][0][1]) + a[i + 1];
    dp[i + 1][0][1][0] = max(dp[i][1][0][0], dp[i][0][0][1]) + b[i + 1];
    dp[i + 1][0][0][1] = max(dp[i][1][0][0], dp[i][0][1][0]) + c[i + 1];
  }

  ll ans = max(dp[N][1][0][0], dp[N][0][1][0]);
  ans = max(ans, dp[N][0][0][1]);

  cout << ans << endl;
  return 0;
}
Problem p03162 (C++)

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

using namespace std;

typedef long long ll;

ll dp[100005][3];

int main(int argc, char *argv[]) {
  int N, a[10005] = {0}, b[10005] = {0}, c[10005] = {0};

  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 = 0; i < N; i++) {
    dp[i + 1][0] = max(dp[i][1] + a[i + 1], dp[i][2] + a[i + 1]);
    dp[i + 1][1] = max(dp[i][0] + b[i + 1], dp[i][2] + b[i + 1]);
    dp[i + 1][2] = max(dp[i][0] + c[i + 1], dp[i][1] + c[i + 1]);
  }

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

  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

typedef long long ll;

ll dp[100005][3];

int main(int argc, char *argv[]) {
  int N, a[100005] = {0}, b[100005] = {0}, c[100005] = {0};

  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 = 0; i < N; i++) {
    dp[i + 1][0] = max(dp[i][1] + a[i + 1], dp[i][2] + a[i + 1]);
    dp[i + 1][1] = max(dp[i][0] + b[i + 1], dp[i][2] + b[i + 1]);
    dp[i + 1][2] = max(dp[i][0] + c[i + 1], dp[i][1] + c[i + 1]);
  }

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

  return 0;
}
Problem p03162 (C++)

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

using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vvl = vector<vl>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vb = vector<bool>;
using vs = vector<string>;
const ll oo = 0x3f3f3f3f3f3f3f3fLL;
const ld eps = 1e-9;
#define sz(c) ll((c).size())
#define all(c) begin(c), end(c)
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define xx first
#define yy second
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORD(i, a, b) for (ll i = ll(b) - 1; i >= (a); i--)
#define TR(X)                                                                  \
  ({                                                                           \
    if (1)                                                                     \
      cerr << "TR: " << (#X) << " = " << (X) << endl;                          \
  })

ll activities[10005][3] = {0};
ll scoreAfterDay[10005][3] = {0};

int main() {
  cin.sync_with_stdio(0);

  ll n;
  cin >> n;

  FOR(i, 1, n + 1) {
    ll a, b, c;
    cin >> a >> b >> c;
    activities[i][0] = a;
    activities[i][1] = b;
    activities[i][2] = c;
  }

  FOR(i, 1, n + 1) {
    FOR(j, 0, 3) {
      ll max = 0;
      FOR(k, 0, 3) {
        if (k != j) {
          ll tmp = scoreAfterDay[i - 1][k] + activities[i][j];
          if (tmp > max)
            max = tmp;
        }
      }
      scoreAfterDay[i][j] = max;
    }
  }
  cout << max(max(scoreAfterDay[n][0], scoreAfterDay[n][1]),
              scoreAfterDay[n][2]);
}

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

using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vvl = vector<vl>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vb = vector<bool>;
using vs = vector<string>;
const ll oo = 0x3f3f3f3f3f3f3f3fLL;
const ld eps = 1e-9;
#define sz(c) ll((c).size())
#define all(c) begin(c), end(c)
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define xx first
#define yy second
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORD(i, a, b) for (ll i = ll(b) - 1; i >= (a); i--)
#define TR(X)                                                                  \
  ({                                                                           \
    if (1)                                                                     \
      cerr << "TR: " << (#X) << " = " << (X) << endl;                          \
  })

ll activities[100005][3] = {0};
ll scoreAfterDay[100005][3] = {0};

int main() {
  cin.sync_with_stdio(0);

  ll n;
  cin >> n;

  FOR(i, 1, n + 1) {
    ll a, b, c;
    cin >> a >> b >> c;
    activities[i][0] = a;
    activities[i][1] = b;
    activities[i][2] = c;
  }

  FOR(i, 1, n + 1) {
    FOR(j, 0, 3) {
      ll max = 0;
      FOR(k, 0, 3) {
        if (k != j) {
          ll tmp = scoreAfterDay[i - 1][k] + activities[i][j];
          if (tmp > max)
            max = tmp;
        }
      }
      scoreAfterDay[i][j] = max;
    }
  }
  cout << max(max(scoreAfterDay[n][0], scoreAfterDay[n][1]),
              scoreAfterDay[n][2]);
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
#define MOD 1000000007

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

const ll INF = 1LL << 60;

int main() {
  ll n, a[10000000][3];
  cin >> n;
  FOR(i, n) {
    FOR(j, 3) { cin >> a[i][j]; }
  }
  ll dp[10000000][3] = {0};
  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]);
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i < 3; i++) {
    chmax(ans, dp[n][i]);
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
#define MOD 1000000007

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

const ll INF = 1LL << 60;

int main() {
  ll n, a[10000000][3];
  cin >> n;
  FOR(i, n) {
    FOR(j, 3) { cin >> a[i][j]; }
  }
  ll dp[1000000][3] = {0};
  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]);
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i < 3; i++) {
    chmax(ans, dp[n][i]);
  }
  cout << ans << endl;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define Fast_IO                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);

int32_t main() {
  Fast_IO
#ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n;
  cin >> n;

  int in[3];
  int ou[3];
  vector<int> dp(3, 0);
  for (int ii = 0; ii < n; ii++) {
    vector<int> dpo(3, 0);
    for (int j = 0; j < 3; j++) {
      cin >> in[j];
    }
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        if (i != j) {
          dpo[j] = max(dpo[j], dp[i] + in[j]);
        }
      }
    }
    dp = dpo;
  }
  cout << max({dp[0], dp[1], dp[2]});
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define Fast_IO                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);

int32_t main() {
  Fast_IO
#ifdef __WIN32
      freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n;
  cin >> n;

  int in[3];
  int ou[3];
  vector<int> dp(3, 0);
  for (int ii = 0; ii < n; ii++) {
    vector<int> dpo(3, 0);
    for (int j = 0; j < 3; j++) {
      cin >> in[j];
    }
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        if (i != j) {
          dpo[j] = max(dpo[j], dp[i] + in[j]);
        }
      }
    }
    dp = dpo;
  }
  cout << max({dp[0], dp[1], dp[2]});
}
Problem p03162 (C++)

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

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using pint = pair<int, int>;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const int MOD = 1000000007;
const long long INF = 1LL << 60;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

ll abc[10010][3]{};
ll dp[10010][3]{};

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; ++i)
    for (int j = 0; j < 3; ++j)
      cin >> abc[i][j];
  for (int a = 0; a < N; ++a) {
    for (int b = 0; b < 3; ++b) {
      for (int c = 0; c < 3; ++c) {
        if (b == c)
          continue;
        chmax(dp[a + 1][c], dp[a][b] + abc[a][c]);
      }
    }
  }
  ll res = 0;
  for (int i = 0; i < 3; ++i)
    chmax(res, dp[N][i]);
  cout << res << endl;
  return 0;
}

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

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using pint = pair<int, int>;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const int MOD = 1000000007;
const long long INF = 1LL << 60;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

ll abc[100100][3]{};
ll dp[100100][3]{};

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; ++i)
    for (int j = 0; j < 3; ++j)
      cin >> abc[i][j];
  for (int a = 0; a < N; ++a) {
    for (int b = 0; b < 3; ++b) {
      for (int c = 0; c < 3; ++c) {
        if (b == c)
          continue;
        chmax(dp[a + 1][c], dp[a][b] + abc[a][c]);
      }
    }
  }
  ll res = 0;
  for (int i = 0; i < 3; ++i)
    chmax(res, dp[N][i]);
  cout << res << endl;
  return 0;
}
Problem p03162 (Python)

Buggy submission (status: Runtime Error):
# dp[i日目][i-1日目にどれを選んだか] = i日目までの最大の幸福度
n = int(input())
a, b, c = [0 for _ in range(n)], [0 for _ in range(n)], [0 for _ in range(n)]
for i in range(n):
    a[i], b[i], c[i] = map(int, input().split())

dp = [[-1 for _ in range(3)] for _ in range(n + 1)]
for i in range(3):
    dp[0][i] = 0


def rec(i, choice):
    if dp[i][choice] > 0:
        return dp[i][choice]
    if i == n:
        return 0
    ret = 0  # ループじゃなくてifでやってるのでそれぞれ確実に大きい値が入る（ので更新する必要がない）
    if choice == 0:
        ret = a[i] + max(rec(i + 1, 1), rec(i + 1, 2))
    elif choice == 1:
        ret = b[i] + max(rec(i + 1, 0), rec(i + 1, 2))
    elif choice == 2:
        ret = c[i] + max(rec(i + 1, 0), rec(i + 1, 1))
    dp[i][choice] = ret
    return ret


print(max(rec(0, 0), rec(0, 1), rec(0, 2)))


Fixed submission:
# dp[i日目][i-1日目にどれを選んだか] = i日目までの最大の幸福度
import sys

sys.setrecursionlimit(10**8)

n = int(input())
a, b, c = [0 for _ in range(n)], [0 for _ in range(n)], [0 for _ in range(n)]
for i in range(n):
    a[i], b[i], c[i] = map(int, input().split())

dp = [[-1 for _ in range(3)] for _ in range(n + 1)]
for i in range(3):
    dp[0][i] = 0


def rec(i, choice):
    if dp[i][choice] > 0:
        return dp[i][choice]
    if i == n:
        return 0
    ret = 0  # ループじゃなくてifでやってるのでそれぞれ確実に大きい値が入る（ので更新する必要がない）
    if choice == 0:
        ret = a[i] + max(rec(i + 1, 1), rec(i + 1, 2))
    elif choice == 1:
        ret = b[i] + max(rec(i + 1, 0), rec(i + 1, 2))
    elif choice == 2:
        ret = c[i] + max(rec(i + 1, 0), rec(i + 1, 1))
    dp[i][choice] = ret
    return ret


print(max(rec(0, 0), rec(0, 1), rec(0, 2)))
Problem p03162 (C++)

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

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

int N;
long long a[10010][3];
long long dp[10010][3];

int main() {
  int N;
  cin >> N;
  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]);
      }
    }
  }

  long long res = 0;
  for (int j = 0; j < 3; ++j)
    chmax(res, dp[N][j]);
  cout << res << endl;

  return 0;
}

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

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

int N;
long long a[100010][3];
long long dp[100010][3];

int main() {
  int N;
  cin >> N;
  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]);
      }
    }
  }

  long long res = 0;
  for (int j = 0; j < 3; ++j)
    chmax(res, dp[N][j]);
  cout << res << endl;

  return 0;
}