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

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

using namespace std;
long n, a[10000][10000], f[10000][10000];
int main() {
  cin >> n;
  for (long i = 1; i <= n; i++) {
    for (long j = 1; j <= 3; j++)
      cin >> a[i][j];
  }
  f[0][1] = 0;
  f[0][2] = 0;
  f[0][3] = 0;
  for (long i = 1; i <= n; i++) {
    for (long j = 1; j <= 3; j++) {
      if (j == 1) {
        f[i][j] = max(a[i][j] + f[i - 1][j + 1], a[i][j] + f[i - 1][j + 2]);
      }
      if (j == 2) {
        f[i][j] = max(a[i][j] + f[i - 1][j - 1], a[i][j] + f[i - 1][j + 1]);
      }
      if (j == 3) {
        f[i][j] = max(a[i][j] + f[i - 1][j - 1], a[i][j] + f[i - 1][j - 2]);
      }
    }
  }
  cout << max(f[n][1], max(f[n][2], f[n][3]));
  return 0;
}


Fixed submission:
#include <iostream>

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

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

using namespace std;

int other[3][2] = {{1, 2}, {0, 2}, {1, 0}};

int getDp(int data[][3], int i, int choice, int dp[][3]) {
  if (dp[i][choice] != -1) {
    return dp[i][choice];
  }

  for (int j = 0; j < 2; j++) {
    dp[i][choice] =
        max(data[i][choice] + getDp(data, i - 1, other[choice][j], dp),
            dp[i][choice]);
  }
  return dp[i][choice];
}

int main() {
  int N;
  cin >> N;
  int data[N][3];
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> data[i][j];
    }
  }
  int dp[N][3];
  for (int i = 0; i < 3; i++) {
    dp[0][i] = data[0][i];
  }

  for (int i = 1; i < N; i++) {
    for (int j = 0; j < 3; j++) {
      dp[i][j] = -1;
    }
  }
  for (int i = 0; i < 3; i++) {
    getDp(data, N - 1, i, dp);
  }
  cout << max(dp[N - 1][0], max(dp[N - 1][1], dp[N - 1][2]));
  return 2;
}

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

using namespace std;

int other[3][2] = {{1, 2}, {0, 2}, {1, 0}};

int getDp(int data[][3], int i, int choice, int dp[][3]) {
  if (dp[i][choice] != -1) {
    return dp[i][choice];
  }

  for (int j = 0; j < 2; j++) {
    dp[i][choice] =
        max(data[i][choice] + getDp(data, i - 1, other[choice][j], dp),
            dp[i][choice]);
  }
  return dp[i][choice];
}

int main() {
  int N;
  cin >> N;
  int data[N][3];
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> data[i][j];
    }
  }
  int dp[N][3];
  for (int i = 0; i < 3; i++) {
    dp[0][i] = data[0][i];
  }

  for (int i = 1; i < N; i++) {
    for (int j = 0; j < 3; j++) {
      dp[i][j] = -1;
    }
  }
  for (int i = 0; i < 3; i++) {
    getDp(data, N - 1, i, dp);
  }
  cout << max(dp[N - 1][0], max(dp[N - 1][1], dp[N - 1][2]));
  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int long long
typedef long double ld;
#define pb push_back
#define f(i, a, b) for (int i = a; i < b; i++)
#define fd(i, a, b) for (int i = a - 1; i >= b; i--)
#define pf push_front
#define fi first
#define se second
#define INF 1e12
const int mod = 1e9 + 7;
int cnt = 0;
signed main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("INPUT.txt", "r", stdin);
  // for writing output to output.txt
  freopen("OUTPUT.txt", "w", stdout);
#endif

  // -------------------------------------Code starts
  // here---------------------------------------------------------------------
  int t = 1;
  // cin>>t;
  while (t--) {
    int n;
    cin >> n;
    int a[n][3];
    f(i, 0, n) { cin >> a[i][0] >> a[i][1] >> a[i][2]; }
    int dp[n][3];
    f(i, 0, n) f(j, 0, 3) dp[i][j] = 0;
    dp[0][0] = a[0][0];
    dp[0][1] = a[0][1];
    dp[0][2] = a[0][2];
    f(i, 1, n) {
      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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int long long
typedef long double ld;
#define pb push_back
#define f(i, a, b) for (int i = a; i < b; i++)
#define fd(i, a, b) for (int i = a - 1; i >= b; i--)
#define pf push_front
#define fi first
#define se second
#define INF 1e12
const int mod = 1e9 + 7;
int cnt = 0;
signed main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  /* #ifndef ONLINE_JUDGE
   // for getting input from input.txt
   freopen("INPUT.txt", "r", stdin);
   // for writing output to output.txt
   freopen("OUTPUT.txt", "w", stdout);
   #endif*/

  // -------------------------------------Code starts
  // here---------------------------------------------------------------------
  int t = 1;
  // cin>>t;
  while (t--) {
    int n;
    cin >> n;
    int a[n][3];
    f(i, 0, n) { cin >> a[i][0] >> a[i][1] >> a[i][2]; }
    int dp[n][3];
    f(i, 0, n) f(j, 0, 3) dp[i][j] = 0;
    dp[0][0] = a[0][0];
    dp[0][1] = a[0][1];
    dp[0][2] = a[0][2];
    f(i, 1, n) {
      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;
using ll = long long;
// using P = pair<int, int>;

ll dp[10005][3];

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

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

ll dp[100005][3];

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

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

using namespace std;

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

int add(int a, int b)

{
  a %= mo;
  b %= mo;
  return (a + b) % mo;
}

int mul(int a, int b) {
  a %= mo;
  b %= mo;
  return (a * b) % mo;
}

int32_t main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  // let's begin
  long long n;
  cin >> n;
  vector<long long> dp(3);
  for (int days = 0; days < n; days++) {
    vector<long long> curr_dp(3, 0);
    vector<long long> cost(3);
    for (int i = 0; i < 3; i++) {
      cin >> cost[i];
    }
    for (int i = 0; i < 3; ++i) {
      for (int j = 0; j < 3; ++j) {
        if (i != j) {
          curr_dp[j] = max(curr_dp[j], dp[i] + cost[j]);
        }
      }
    }
    dp = curr_dp;
  }
  cout << max(dp[0], max(dp[1], dp[2])) << "\n";
  return 0;
}

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

using namespace std;

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

int add(int a, int b)

{
  a %= mo;
  b %= mo;
  return (a + b) % mo;
}

int mul(int a, int b) {
  a %= mo;
  b %= mo;
  return (a * b) % mo;
}

int32_t main() {
  /* #ifndef ONLINE_JUDGE
   freopen("input.txt", "r", stdin);
   freopen("output.txt", "w", stdout);
   #endif*/
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  // let's begin
  long long n;
  cin >> n;
  vector<long long> dp(3);
  for (int days = 0; days < n; days++) {
    vector<long long> curr_dp(3, 0);
    vector<long long> cost(3);
    for (int i = 0; i < 3; i++) {
      cin >> cost[i];
    }
    for (int i = 0; i < 3; ++i) {
      for (int j = 0; j < 3; ++j) {
        if (i != j) {
          curr_dp[j] = max(curr_dp[j], dp[i] + cost[j]);
        }
      }
    }
    dp = curr_dp;
  }
  cout << max(dp[0], max(dp[1], dp[2])) << "\n";
  return 0;
}
Problem p03162 (C++)

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

typedef long long ll;
typedef vector<int> vec;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef pair<int, int> par;
typedef vector<par> vpar;

#define forn(i, n) for (ll i = 0; i < n; i++)
#define wez(n)                                                                 \
  ll(n);                                                                       \
  cin >> (n);
#define setmax(a, b) (a) = max((a), (b))
#define setmin(a, b) (a) = min((a), (b))
#define pb push_back
#define mp make_pair
#define outa(a, e) forn(i, e) cout << a[i] << (i == e - 1 ? '\n' : ' ')
#define oute(n) cout << n << "\n"

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  /* #ifndef ONLINE_JUDGE
   freopen("input.txt", "r", stdin);
   freopen("output.txt", "w", stdout);
   #endif*/
  wez(n);
  vector<ll> dp(3, 0);
  forn(i, n) {
    vector<ll> new_dp(3, 0);
    vector<int> cost(3);
    forn(j, 3) cin >> cost[j];
    forn(j, 3) {
      forn(k, 3) {
        if (j != k) {
          new_dp[j] = max(new_dp[j], dp[i] + cost[j]);
        }
      }
    }
    dp = new_dp;
  }
  cout << max(dp[0], max(dp[1], dp[2]));
  return 0;
}


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

typedef long long ll;
typedef vector<int> vec;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef pair<int, int> par;
typedef vector<par> vpar;

#define forn(i, n) for (ll i = 0; i < n; i++)
#define wez(n)                                                                 \
  ll(n);                                                                       \
  cin >> (n);
#define setmax(a, b) (a) = max((a), (b))
#define setmin(a, b) (a) = min((a), (b))
#define pb push_back
#define mp make_pair
#define outa(a, e) forn(i, e) cout << a[i] << (i == e - 1 ? '\n' : ' ')
#define oute(n) cout << n << "\n"

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  /* #ifndef ONLINE_JUDGE
   freopen("input.txt", "r", stdin);
   freopen("output.txt", "w", stdout);
   #endif*/
  wez(n);
  vector<ll> dp(3, 0);
  forn(i, n) {
    vector<ll> new_dp(3, 0);
    vector<int> cost(3);
    forn(j, 3) cin >> cost[j];
    forn(j, 3) {
      forn(k, 3) {
        if (j != k) {
          new_dp[k] = max(new_dp[k], dp[j] + cost[k]);
        }
      }
    }
    dp = new_dp;
  }
  cout << max(dp[0], max(dp[1], dp[2]));
  return 0;
}
Problem p03162 (C++)

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

const int MAXN = 1e5 + 10;

int vis[MAXN][3], dp[MAXN][3], N, A[3][MAXN];

int solve(int pos, int last) {

  if (pos == N + 1)
    return 0;

  if (vis[pos][last])
    return dp[pos][last];

  int best = 0;

  for (int i = 0; i < 3; i++) {
    if (i != last)
      best = max(best, A[i][pos] + solve(pos + 1, i));
  }

  return dp[pos][last] = best;
}

int main() {

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

  printf("%d\n", max(solve(1, 0), max(solve(1, 1), solve(1, 2))));

  return 0;
}

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

const int MAXN = 1e5 + 10;

int vis[MAXN][3], dp[MAXN][3], N, A[3][MAXN];

int solve(int pos, int last) {

  if (pos == N + 1)
    return 0;

  if (vis[pos][last])
    return dp[pos][last];
  vis[pos][last] = 1;

  int best = 0;

  for (int i = 0; i < 3; i++) {
    if (i != last)
      best = max(best, A[i][pos] + solve(pos + 1, i));
  }

  return dp[pos][last] = best;
}

int main() {

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

  printf("%d\n", max(solve(1, 0), max(solve(1, 1), solve(1, 2))));

  return 0;
}
Problem p03162 (C++)

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

const int mx = 1e5 + 123;
int dp[mx][4], n;
int a[mx][4];

int solve(int i, int f1) {
  if (i > n)
    return 0;
  if (dp[i][f1] != -1)
    return dp[i][f1];

  int ret = 0;
  for (int j = 1; j <= n; j++) {
    if (j != f1) {
      ret = max(ret, a[i][j] + solve(i + 1, j));
    }
  }

  return dp[i][f1] = ret;
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i][1] >> a[i][2] >> a[i][3];

  memset(dp, -1, sizeof(dp));
  cout << solve(0, 0) << endl;
}

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

const int mx = 1e5 + 123;
int dp[mx][4], n;
int a[mx][4];

int solve(int i, int f1) {
  if (i > n)
    return 0;
  if (dp[i][f1] != -1)
    return dp[i][f1];

  int ret = 0;
  for (int j = 1; j <= 3; j++) {
    if (j != f1) {
      ret = max(ret, a[i][j] + solve(i + 1, j));
    }
  }

  return dp[i][f1] = ret;
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i][1] >> a[i][2] >> a[i][3];

  memset(dp, -1, sizeof(dp));
  cout << solve(0, 0) << endl;
}
Problem p03162 (C++)

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t, y = 1;
  // cin>>t;
  t = 1;
  while (t > 0) {
    t--;
    ll n, i, j, k, flag = 1, x = 0;
    cin >> n;
    ll a = 0, b = 0, c = 0;
    ll dp[(ll)1e5][3];
    for (i = 1; i <= n; i++) {
      cin >> a >> b >> c;
      dp[i][1] = max(dp[i - 1][2] + a, dp[i - 1][3] + a);
      dp[i][2] = max(dp[i - 1][1] + b, dp[i - 1][3] + b);
      dp[i][3] = max(dp[i - 1][2] + c, dp[i - 1][1] + c);
    }
    x = max(dp[n][1], dp[n][2]);
    x = max(x, dp[n][3]);
    cout << x;
  }
  return 0;
}

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t, y = 1;
  // cin>>t;
  t = 1;
  while (t > 0) {
    t--;
    ll n, i, j, k, flag = 1, x = 0;
    cin >> n;
    ll a = 0, b = 0, c = 0;
    ll dp[(ll)1e5 + 1][3];
    for (i = 1; i <= n; i++) {
      cin >> a >> b >> c;
      dp[i][1] = max(dp[i - 1][2] + a, dp[i - 1][3] + a);
      dp[i][2] = max(dp[i - 1][1] + b, dp[i - 1][3] + b);
      dp[i][3] = max(dp[i - 1][2] + c, dp[i - 1][1] + c);
    }
    x = max(dp[n][1], dp[n][2]);
    x = max(x, dp[n][3]);
    cout << x;
  }
  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++) // n回繰り返す
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using graph = vector<vector<int>>;
const ll mod = pow(10, 9) + 7;
const int INF = 1e9;
const ll LINF = 1e18;
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;
}
int dp[11000][3];
int a[11000], b[11000], c[11000];
int main() {
  int N;
  cin >> N;
  rep(i, N) { cin >> a[i] >> b[i] >> c[i]; }
  rep(i, N) {
    rep(j, 3) { dp[i][j] = 0; }
  }
  dp[0][0] += a[0], dp[0][1] += b[0];
  dp[0][2] += c[0];
  rep(i, N) {
    if (i != 0) {
      chmax(dp[i][0], max(dp[i - 1][1] + a[i], dp[i - 1][2] + a[i]));
      chmax(dp[i][1], max(dp[i - 1][0] + b[i], dp[i - 1][2] + b[i]));
      chmax(dp[i][2], max(dp[i - 1][1] + c[i], dp[i - 1][0] + c[i]));
    }
  }

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

  ans = max(ans, (ll)dp[N - 1][2]);
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++) // n回繰り返す
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using graph = vector<vector<int>>;
const ll mod = pow(10, 9) + 7;
const int INF = 1e9;
const ll LINF = 1e18;
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;
}
int dp[110000][3];
int a[110000], b[110000], c[110000];
int main() {
  int N;
  cin >> N;
  rep(i, N) { cin >> a[i] >> b[i] >> c[i]; }
  rep(i, N) {
    rep(j, 3) { dp[i][j] = 0; }
  }
  dp[0][0] += a[0], dp[0][1] += b[0];
  dp[0][2] += c[0];
  rep(i, N) {
    if (i != 0) {
      chmax(dp[i][0], max(dp[i - 1][1] + a[i], dp[i - 1][2] + a[i]));
      chmax(dp[i][1], max(dp[i - 1][0] + b[i], dp[i - 1][2] + b[i]));
      chmax(dp[i][2], max(dp[i - 1][1] + c[i], dp[i - 1][0] + c[i]));
    }
  }

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

  ans = max(ans, (ll)dp[N - 1][2]);
  cout << ans << endl;
}
Problem p03162 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <fstream>
using namespace std;
#define rep(i, a, b) for (ll i = (a); i < (b); i++)
#define rep2(r, aa, bb) for (ll r = (aa)-1; r >= (bb); r--)
#define be(x) (x.begin(), x.end())
#define pb(t) push_back(t)
#define ll long long
int n;
int dp[100005][4];
vector<vector<int>> A;
int minicost(int v, int pre) {
  if (v >= n)
    return 0;
  if (dp[v][pre] != -1)
    return dp[v][pre];
  int maxi = 0;
  if (pre != 0) {
    maxi = max(maxi, A[v][0] + minicost(v + 1, 0));
  }
  if (pre != 1) {
    maxi = max(maxi, A[v][1] + minicost(v + 1, 1));
  }
  if (pre != 2) {
    maxi = max(maxi, A[v][2] + minicost(v + 1, 2));
  }
  return maxi;
}
int main() {
  cin >> n;
  A.resize(n);
  memset(dp, -1, sizeof(dp));
  rep(i, 0, n) {
    A[i].resize(3);
    cin >> A[i][0] >> A[i][1] >> A[i][2];
  }
  cout << minicost(0, 3);
}

Fixed submission:
#include <bits/stdc++.h>
#include <fstream>
using namespace std;
#define rep(i, a, b) for (ll i = (a); i < (b); i++)
#define rep2(r, aa, bb) for (ll r = (aa)-1; r >= (bb); r--)
#define be(x) (x.begin(), x.end())
#define pb(t) push_back(t)
#define ll long long
int n;
int dp[100005][4];
vector<vector<int>> A;
int minicost(int v, int pre) {
  if (v >= n)
    return 0;
  if (dp[v][pre] != -1)
    return dp[v][pre];
  int maxi = 0;
  if (pre != 0) {
    maxi = max(maxi, A[v][0] + minicost(v + 1, 0));
  }
  if (pre != 1) {
    maxi = max(maxi, A[v][1] + minicost(v + 1, 1));
  }
  if (pre != 2) {
    maxi = max(maxi, A[v][2] + minicost(v + 1, 2));
  }
  return dp[v][pre] = maxi;
}
int main() {
  cin >> n;
  A.resize(n);
  memset(dp, -1, sizeof(dp));
  rep(i, 0, n) {
    A[i].resize(3);
    cin >> A[i][0] >> A[i][1] >> A[i][2];
  }
  cout << minicost(0, 3);
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
using namespace std;
int arr[10010][10010];
int dp[10010][10010];
signed main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> arr[i][0] >> arr[i][1] >> arr[i][2];
  }
  for (int i = 0; i < 3; i++) {
    dp[0][i] = arr[0][i];
  }
  for (int i = 1; i < n; i++) {
    dp[i][0] = max(dp[i - 1][1] + arr[i][0], dp[i - 1][2] + arr[i][0]);
    dp[i][1] = max(dp[i - 1][0] + arr[i][1], dp[i - 1][2] + arr[i][1]);
    dp[i][2] = max(dp[i - 1][0] + arr[i][2], dp[i - 1][1] + arr[i][2]);
  }
  cout << max(max(dp[n - 1][0], dp[n - 1][1]), dp[n - 1][2]) << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
using namespace std;
int arr[100100][4];
int dp[100100][4];
signed main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> arr[i][0] >> arr[i][1] >> arr[i][2];
  }
  for (int i = 0; i < 3; i++) {
    dp[0][i] = arr[0][i];
  }
  for (int i = 1; i < n; i++) {
    dp[i][0] = max(dp[i - 1][1] + arr[i][0], dp[i - 1][2] + arr[i][0]);
    dp[i][1] = max(dp[i - 1][0] + arr[i][1], dp[i - 1][2] + arr[i][1]);
    dp[i][2] = max(dp[i - 1][0] + arr[i][2], dp[i - 1][1] + arr[i][2]);
  }
  cout << max(max(dp[n - 1][0], dp[n - 1][1]), dp[n - 1][2]) << "\n";
}
Problem p03162 (C++)

Buggy submission (status: Time Limit Exceeded):
// shan61916
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define endl "\n"
typedef long long ll;
typedef unsigned long long ull;
typedef double dll;
ll n;
ll a[100020][3];
ll dp[100020][4];
ll recurse(ll idx, ll type) {
  if (idx == n)
    return 0;
  ll &ans = dp[idx][type];
  ans = 0;
  if (type == 0) {
    return ans = max({a[idx][0] + recurse(idx + 1, 1),
                      a[idx][1] + recurse(idx + 1, 2),
                      a[idx][2] + recurse(idx + 1, 3)});
  }
  if (type == 1) {
    return ans = max({a[idx][1] + recurse(idx + 1, 2),
                      a[idx][2] + recurse(idx + 1, 3)});
  }
  if (type == 2) {
    return ans = max({a[idx][0] + recurse(idx + 1, 1),
                      a[idx][2] + recurse(idx + 1, 3)});
  } else {
    return ans = max({a[idx][0] + recurse(idx + 1, 1),
                      a[idx][1] + recurse(idx + 1, 2)});
  }
}
int main() {
  IOS
#ifdef SHAN
      freopen("input.txt", "r", stdin);
#endif
  cin >> n;
  memset(dp, -1, sizeof(dp));
  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j < 3; j++)
      cin >> a[i][j];
  }
  ll ans = recurse(0, 0);
  cout << ans << endl;
  return 0;
} // good night.

Fixed submission:
// shan61916
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define endl "\n"
typedef long long ll;
typedef unsigned long long ull;
typedef double dll;
ll n;
ll a[100020][3];
ll dp[100020][4];
ll recurse(ll idx, ll type) {
  if (idx == n)
    return 0;
  ll &ans = dp[idx][type];
  if (ans != -1)
    return ans;
  ans = 0;
  if (type == 0) {
    return ans = max({a[idx][0] + recurse(idx + 1, 1),
                      a[idx][1] + recurse(idx + 1, 2),
                      a[idx][2] + recurse(idx + 1, 3)});
  }
  if (type == 1) {
    return ans = max({a[idx][1] + recurse(idx + 1, 2),
                      a[idx][2] + recurse(idx + 1, 3)});
  }
  if (type == 2) {
    return ans = max({a[idx][0] + recurse(idx + 1, 1),
                      a[idx][2] + recurse(idx + 1, 3)});
  } else {
    return ans = max({a[idx][0] + recurse(idx + 1, 1),
                      a[idx][1] + recurse(idx + 1, 2)});
  }
}
int main() {
  IOS
#ifdef SHAN
      freopen("input.txt", "r", stdin);
#endif
  cin >> n;
  memset(dp, -1, sizeof(dp));
  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j < 3; j++)
      cin >> a[i][j];
  }
  ll ans = recurse(0, 0);
  cout << ans << endl;
  return 0;
} // good night.
Problem p03162 (C++)

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

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef set<int> si;
typedef map<int, int> mii;
typedef map<ll, ll> mll;
typedef map<string, int> msi;
typedef set<ll> sl;
typedef set<char> sc;
typedef pair<int, int> pii;

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
const int INF = 1 << 29;
#define MOD (ll)(1e9 + 7)
#define eps 1e-6
#define pb push_back
#define mp make_pair
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define F first
#define S second

int main() {
  ios::sync_with_stdio(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

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


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

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef set<int> si;
typedef map<int, int> mii;
typedef map<ll, ll> mll;
typedef map<string, int> msi;
typedef set<ll> sl;
typedef set<char> sc;
typedef pair<int, int> pii;

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
const int INF = 1 << 29;
#define MOD (ll)(1e9 + 7)
#define eps 1e-6
#define pb push_back
#define mp make_pair
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define F first
#define S second

int main() {
  ios::sync_with_stdio(0);

  int n;
  cin >> n;
  int a[n][3];
  REP(i, n) { cin >> a[i][0] >> a[i][1] >> a[i][2]; }
  int dp[n + 5][3];
  dp[0][0] = 0;
  dp[0][1] = 0;
  dp[0][2] = 0;
  dp[1][0] = a[0][0];
  dp[1][1] = a[0][1];
  dp[1][2] = a[0][2];
  for (int i = 2; i <= n; ++i) {
    dp[i][0] = a[i - 1][0] + max(dp[i - 1][1], dp[i - 1][2]);
    dp[i][1] = a[i - 1][1] + max(dp[i - 1][0], dp[i - 1][2]);
    dp[i][2] = a[i - 1][2] + max(dp[i - 1][0], dp[i - 1][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;
#define ll long long

ll a[10005], b[10005], c[10005], n;
ll dp[10005][4];

ll solve(ll day, ll activity) {
  if (day == n + 1) {
    return 0;
  }
  if (dp[day][activity] != -1) {
    return dp[day][activity];
  }
  if (activity == 1) {
    return dp[day][activity] =
               a[day] + max(solve(day + 1, 2), solve(day + 1, 3));
  } else if (activity == 2) {
    return dp[day][activity] =
               b[day] + max(solve(day + 1, 1), solve(day + 1, 3));
  } else if (activity == 3) {
    return dp[day][activity] =
               c[day] + max(solve(day + 1, 1), solve(day + 1, 2));
  }
}

int main() {

  cin >> n;

  for (ll i = 1; i <= n; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }
  memset(dp, -1, sizeof(dp));
  cout << max(max(solve(1, 1), solve(1, 2)), solve(1, 3));

  return 0;
}

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

ll a[100005], b[100005], c[100005], n;
ll dp[100005][4];

ll solve(ll day, ll activity) {
  if (day == n + 1) {
    return 0;
  }
  if (dp[day][activity] != -1) {
    return dp[day][activity];
  }
  if (activity == 1) {
    return dp[day][activity] =
               a[day] + max(solve(day + 1, 2), solve(day + 1, 3));
  } else if (activity == 2) {
    return dp[day][activity] =
               b[day] + max(solve(day + 1, 1), solve(day + 1, 3));
  } else if (activity == 3) {
    return dp[day][activity] =
               c[day] + max(solve(day + 1, 1), solve(day + 1, 2));
  }
}

int main() {

  cin >> n;

  for (ll i = 1; i <= n; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }
  memset(dp, -1, sizeof(dp));
  cout << max(max(solve(1, 1), solve(1, 2)), solve(1, 3));

  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define FAST_IO                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define mod 1000000007
#define f(i, n) for (int i = 0; i < n; i++)
#define INF 1e9 + 5
#define LINF 1e18L + 5;
#define pb push_back
#define mk make_pair
using namespace std;
/************************BELIAL**********DEVIL*********BELIAL***********DEVIL*************************/
int main() {

  int n;
  cin >> n;

  // vector<int> arr(n);
  vector<int> dp(3, 0); // maintain ans of prev state

  for (int days = 0; days < n; days++) {
    vector<int> c(3); // for a,b,c
    vector<int> new_dp(3, 0);
    for (int i = 0; i < 3; i++)
      cin >> c[i];

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 3; j++) {
        if (i != j) {
          new_dp[j] = max(new_dp[j], dp[i] + c[j]); //  ans of cur state
        }
      }
    }

    dp = new_dp;
  }

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

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define FAST_IO                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define mod 1000000007
#define f(i, n) for (int i = 0; i < n; i++)
#define INF 1e9 + 5
#define LINF 1e18L + 5;
#define pb push_back
#define mk make_pair
using namespace std;
/************************BELIAL**********DEVIL*********BELIAL***********DEVIL*************************/
int main() {

  int n;
  cin >> n;

  // vector<int> arr(n);
  vector<int> dp(3, 0); // maintain ans of prev state

  for (int days = 0; days < n; days++) {
    vector<int> c(3); // for a,b,c
    vector<int> new_dp(3, 0);
    for (int i = 0; i < 3; i++)
      cin >> c[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] + c[j]); //  ans of cur state
        }
      }
    }

    dp = new_dp;
  }

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

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

int main() {
  int n;
  cin >> n;
  int dpa[10000], dpb[10000], dpc[10000];
  dpa[0] = dpb[0] = dpc[0] = 0;
  for (int i = 1; i <= n; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    dpa[i] = a + max(dpb[i - 1], dpc[i - 1]);
    dpb[i] = b + max(dpc[i - 1], dpa[i - 1]);
    dpc[i] = c + max(dpa[i - 1], dpb[i - 1]);
  }
  cout << max({dpa[n], dpb[n], dpc[n]}) << endl;
}


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

int main() {
  int n;
  cin >> n;
  int dpa[100001], dpb[100001], dpc[100001];
  dpa[0] = dpb[0] = dpc[0] = 0;
  for (int i = 1; i <= n; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    dpa[i] = a + max(dpb[i - 1], dpc[i - 1]);
    dpb[i] = b + max(dpc[i - 1], dpa[i - 1]);
    dpc[i] = c + max(dpa[i - 1], dpb[i - 1]);
  }
  cout << max({dpa[n], dpb[n], dpc[n]}) << endl;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 1e18 + 10;
const ll N = 1e3 + 10;
ll n, x, y, z, pr, mx;
vector<ll> v[N];
ll dp[N][3];
ll func(ll ind, ll pr) {
  ll sum = 0;
  if (ind == n)
    return 0;
  if (dp[ind][pr] != -1)
    return dp[ind][pr];
  if (pr == 0) {
    sum += max(v[ind][1] + func(ind + 1, 1), v[ind][2] + func(ind + 1, 2));
  } else if (pr == 1) {
    sum += max(v[ind][0] + func(ind + 1, 0), v[ind][2] + func(ind + 1, 2));
  } else {
    sum += max(v[ind][0] + func(ind + 1, 0), v[ind][1] + func(ind + 1, 1));
  }
  dp[ind][pr] = sum;
  return sum;
}
void solve() {
  cin >> n;
  memset(dp, -1, sizeof dp);
  for (ll i = 0; i < n; i++) {
    cin >> x >> y >> z;
    // cout << x << " " << y << " " << z << endl;
    v[i].push_back(x);
    v[i].push_back(y);
    v[i].push_back(z);
  }
  for (ll i = 0; i < 3; i++) {
    mx = max(mx, func(1, i) + v[0][i]);
  }
  cout << mx << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll t;
  t = 1;
  // cin >> t ;
  while (t--)
    solve();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 1e18 + 10;
const ll N = 1e5 + 10;
ll n, x, y, z, pr, mx;
vector<ll> v[N];
ll dp[N][3];
ll func(ll ind, ll pr) {
  ll sum = 0;
  if (ind == n)
    return 0;
  if (dp[ind][pr] != -1)
    return dp[ind][pr];
  if (pr == 0) {
    sum += max(v[ind][1] + func(ind + 1, 1), v[ind][2] + func(ind + 1, 2));
  } else if (pr == 1) {
    sum += max(v[ind][0] + func(ind + 1, 0), v[ind][2] + func(ind + 1, 2));
  } else {
    sum += max(v[ind][0] + func(ind + 1, 0), v[ind][1] + func(ind + 1, 1));
  }
  dp[ind][pr] = sum;
  return sum;
}
void solve() {
  cin >> n;
  memset(dp, -1, sizeof dp);
  for (ll i = 0; i < n; i++) {
    cin >> x >> y >> z;
    // cout << x << " " << y << " " << z << endl;
    v[i].push_back(x);
    v[i].push_back(y);
    v[i].push_back(z);
  }
  for (ll i = 0; i < 3; i++) {
    mx = max(mx, func(1, i) + v[0][i]);
  }
  cout << mx << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll t;
  t = 1;
  // cin >> t ;
  while (t--)
    solve();
  return 0;
}
Problem p03162 (C++)

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

#define int long long int

const int INF = 1e9 + 5;

#undef int
int main() {

  int n, k;
  scanf("%d", &n);
  vector<int> dp(3, 0);
  vector<int> arr(3, 0);
  dp[0] = 0;

  for (int i = 0; i < n; i++) {
    vector<int> new_dp(3, 0);
    for (int j = 0; j < 3; j++)
      scanf("%d", &arr[j]);

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

    dp = new_dp;
  }

  printf("%d", max(max(dp[0], dp[1]), dp[2]));
}

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

#define int long long int

const int INF = 1e9 + 5;

#undef int
int main() {

  int n, k;
  scanf("%d", &n);
  vector<int> dp(3, 0);
  vector<int> arr(3, 0);
  dp[0] = 0;

  for (int i = 0; i < n; i++) {
    vector<int> new_dp(3, 0);
    for (int j = 0; j < 3; j++)
      scanf("%d", &arr[j]);

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

    dp = new_dp;
  }

  printf("%d", max(max(dp[0], dp[1]), dp[2]));
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll unsigned long long
map<pair<int, int>, int> m;
int ans;
int solve(vector<vector<int>> &mat, int day, int item) {
  if (day >= mat.size()) {
    return 0;
  }
  if (m.find({day, item}) != m.end())
    return m[{day, item}];
  int temp = 0;
  for (int j = 0; j < 3; j++) {
    if (j != item)
      temp = max(temp, mat[day][item] + solve(mat, day + 1, j));
  }
  return m[{day, item}] = temp;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("D:/CP/input.txt", "r", stdin);
  freopen("D:/CP/output.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  vector<vector<int>> mat(n, vector<int>(3));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> mat[i][j];
    }
  }
  ans = 0;
  for (int i = 0; i < 3; i++) {
    ans = max(ans, solve(mat, 0, i));
  }
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll unsigned long long
map<pair<int, int>, int> m;
int ans;
int solve(vector<vector<int>> &mat, int day, int item) {
  if (day >= mat.size()) {
    return 0;
  }
  if (m.find({day, item}) != m.end())
    return m[{day, item}];
  int temp = 0;
  for (int j = 0; j < 3; j++) {
    if (j != item)
      temp = max(temp, mat[day][item] + solve(mat, day + 1, j));
  }
  return m[{day, item}] = temp;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  vector<vector<int>> mat(n, vector<int>(3));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> mat[i][j];
    }
  }
  ans = 0;
  for (int i = 0; i < 3; i++) {
    ans = max(ans, solve(mat, 0, i));
  }
  cout << ans;
  return 0;
}
Problem p03162 (C++)

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

#define mod 1000000007
#define pll pair<long long, long long>
#define pdd pair<long double, long double>
#define mp make_pair
#define pb push_back
priority_queue<pll, vector<pll>, greater<pll>> pq;
#define ll long long

int main() {
  int n;
  cin >> n;
  vector<ll> a(n + 1), b(n + 1), c(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }
  vector<vector<ll>> dp(4, vector<ll>(n + 1, 0));
  for (int i = 1; i <= n; i++) {
    // cout<<"sgfd";
    dp[i][1] = a[i] + max(dp[i - 1][2], dp[i - 1][3]);
    dp[i][2] = b[i] + max(dp[i - 1][1], dp[i - 1][3]);
    dp[i][3] = c[i] + max(dp[i - 1][2], dp[i - 1][1]);
  }
  cout << max(dp[n][1], max(dp[n][2], dp[n][3]));
  return 0;
}


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

#define mod 1000000007
#define pll pair<long long, long long>
#define pdd pair<long double, long double>
#define mp make_pair
#define pb push_back
priority_queue<pll, vector<pll>, greater<pll>> pq;
#define ll long long

int main() {
  int n;
  cin >> n;
  vector<ll> a(n + 1), b(n + 1), c(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }
  vector<vector<ll>> dp(n + 1, vector<ll>(4, 0));
  for (int i = 1; i <= n; i++) {
    // cout<<"sgfd";
    dp[i][1] = a[i] + max(dp[i - 1][2], dp[i - 1][3]);
    dp[i][2] = b[i] + max(dp[i - 1][1], dp[i - 1][3]);
    dp[i][3] = c[i] + max(dp[i - 1][2], dp[i - 1][1]);
  }
  cout << max(dp[n][1], max(dp[n][2], dp[n][3]));
  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

#define loopi(x, y) for (int i = x; i < (y); i++)
#define loopj(x, y) for (int j = x; j < (y); j++)

#define nl cout << "\n";
#define ll long long int
using namespace std;
#define mod 1000000007

int main() {

  ios ::sync_with_stdio(false);
  cin.tie(nullptr);
  // ios_base :: sync_with_stdio(false);
  // cin.tie(NULL)

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  int n;
  cin >> n;
  int a, b, c;
  int dp[n][3];
  cin >> dp[0][0] >> dp[0][1] >> dp[0][2];
  loopi(1, n) {
    cin >> a >> b >> c;

    dp[i][0] = a + max(dp[i - 1][1], dp[i - 1][2]);
    dp[i][1] = b + max(dp[i - 1][0], dp[i - 1][2]);
    dp[i][2] = c + max(dp[i - 1][0], dp[i - 1][1]);
  }

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

  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

#define loopi(x, y) for (int i = x; i < (y); i++)
#define loopj(x, y) for (int j = x; j < (y); j++)

#define nl cout << "\n";
#define ll long long int
using namespace std;
#define mod 1000000007

int main() {

  ios ::sync_with_stdio(false);
  cin.tie(nullptr);
  // ios_base :: sync_with_stdio(false);
  // cin.tie(NULL)

  // #ifndef ONLINE_JUDGE
  // 	freopen("input.txt", "r", stdin);
  // 	freopen("output.txt", "w", stdout);
  // #endif

  int n;
  cin >> n;
  int a, b, c;
  int dp[n][3];
  cin >> dp[0][0] >> dp[0][1] >> dp[0][2];
  loopi(1, n) {
    cin >> a >> b >> c;

    dp[i][0] = a + max(dp[i - 1][1], dp[i - 1][2]);
    dp[i][1] = b + max(dp[i - 1][0], dp[i - 1][2]);
    dp[i][2] = c + max(dp[i - 1][0], dp[i - 1][1]);
  }

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

  return 0;
}
Problem p03162 (C++)

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

const long long MOD = 10e9 + 7;

using namespace std;

typedef long long ll;

long long f(long long i) {
  long long n = i;
  int mul = 1;
  while (i / 10 != 0) {
    int i1 = i % 10;
    int i2 = (i / 10) % 10;
    if (i1 == i2) {
      n = n - i1 * mul;
    }
    i = i / 10;
    mul *= 10;
  }
  return n;
}

int main() {
  // int mi=1000000000;
  // cout<<mi<<endl;
  int N, a, b, c;
  vector<int> va, vb, vc, aa, ab, ac;
  cin >> N;

  for (int i = 0; i < N; i++) {
    cin >> a >> b >> c;
    va.push_back(a);
    vc.push_back(c);
    vb.push_back(b);
  }
  aa.push_back(va[0]);
  ab.push_back(vb[0]);
  ac.push_back(vc[0]);
  for (int i = 1; i < N; i++) {
    aa[i] = max(ab[i - 1], ac[i - 1]) + va[i];
    ab[i] = max(aa[i - 1], ac[i - 1]) + vb[i];
    ac[i] = max(ab[i - 1], aa[i - 1]) + vc[i];
  }
  cout << max(max(aa[N - 1], ab[N - 1]), ac[N - 1]) << endl;
}

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

const long long MOD = 10e9 + 7;

using namespace std;

typedef long long ll;

long long f(long long i) {
  long long n = i;
  int mul = 1;
  while (i / 10 != 0) {
    int i1 = i % 10;
    int i2 = (i / 10) % 10;
    if (i1 == i2) {
      n = n - i1 * mul;
    }
    i = i / 10;
    mul *= 10;
  }
  return n;
}

int main() {
  // int mi=1000000000;
  // cout<<mi<<endl;
  int N, a, b, c;
  vector<int> va, vb, vc, aa, ab, ac;
  cin >> N;

  for (int i = 0; i < N; i++) {
    cin >> a >> b >> c;
    va.push_back(a);
    vc.push_back(c);
    vb.push_back(b);
  }
  aa.push_back(va[0]);
  ab.push_back(vb[0]);
  ac.push_back(vc[0]);
  for (int i = 1; i < N; i++) {
    aa.push_back(max(ab[i - 1], ac[i - 1]) + va[i]);
    ab.push_back(max(aa[i - 1], ac[i - 1]) + vb[i]);
    ac.push_back(max(ab[i - 1], aa[i - 1]) + vc[i]);
  }
  cout << max(max(aa[N - 1], ab[N - 1]), ac[N - 1]) << endl;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, x, n) for (int i = x; i < (n); i++)
#define all(n) begin(n), end(n)
struct cww {
  cww() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} star;
const long long INF = numeric_limits<long long>::max();
int a[11000], b[11000], c[11000];
long long dp[110000][3]; // dp[i][j]:=
                         // 最後にjをした時のi日目までの幸福度の最大値
int main() {
  int n;
  cin >> n;
  rep(i, n) { cin >> a[i] >> b[i] >> c[i]; }
  dp[0][0] = 0;
  dp[0][1] = 0;
  dp[0][2] = 0;
  rep(i, n) {
    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][1] + b[i], dp[i][0] + a[i]);
  }
  long long ans = -100000000000;
  rep(i, 3) { ans = max(ans, dp[n][i]); }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, x, n) for (int i = x; i < (n); i++)
#define all(n) begin(n), end(n)
struct cww {
  cww() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} star;
const long long INF = numeric_limits<long long>::max();
int a[110000], b[110000], c[110000];
long long dp[110000][3]; // dp[i][j]:=
                         // 最後にjをした時のi日目までの幸福度の最大値
int main() {
  int n;
  cin >> n;
  rep(i, n) { cin >> a[i] >> b[i] >> c[i]; }
  dp[0][0] = 0;
  dp[0][1] = 0;
  dp[0][2] = 0;
  rep(i, n) {
    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][1] + b[i], dp[i][0] + a[i]);
  }
  long long ans = -100000000000;
  rep(i, 3) { ans = max(ans, dp[n][i]); }
  cout << ans << endl;
  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
struct edge {
  int to;
  ll cap;
  int rev;
};
int main() {
  ll N;
  cin >> N;
  vector<ll> a(N + 5), b(N + 5), c(N + 5);
  vector<vector<ll>> dp(N + 5, vector<ll>(N + 5));
  rep(i, N) cin >> a[i] >> b[i] >> c[i];
  dp[0][0] = a[0];
  dp[0][1] = b[0];
  dp[0][2] = c[0];
  if (N <= 1) {
    cout << max(max(dp[0][0], dp[0][1]), dp[0][2]) << endl;
  } else {
    REP(i, 1, N) {
      dp[i][0] = max(dp[i - 1][1] + a[i], dp[i - 1][2] + a[i]);
      dp[i][1] = max(dp[i - 1][2] + b[i], dp[i - 1][0] + b[i]);
      dp[i][2] = max(dp[i - 1][0] + c[i], 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 <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
struct edge {
  int to;
  ll cap;
  int rev;
};
int main() {
  ll N;
  cin >> N;
  vector<ll> a(N + 5), b(N + 5), c(N + 5);
  vector<vector<ll>> dp(N + 5, vector<ll>(3));
  rep(i, N) cin >> a[i] >> b[i] >> c[i];
  dp[0][0] = a[0];
  dp[0][1] = b[0];
  dp[0][2] = c[0];
  if (N <= 1) {
    cout << max(max(dp[0][0], dp[0][1]), dp[0][2]) << endl;
  } else {
    REP(i, 1, N) {
      dp[i][0] = max(dp[i - 1][1] + a[i], dp[i - 1][2] + a[i]);
      dp[i][1] = max(dp[i - 1][2] + b[i], dp[i - 1][0] + b[i]);
      dp[i][2] = max(dp[i - 1][0] + c[i], 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>
using namespace std;
int main() {
  int n;
  cin >> n;
  int arr[n][3], dp[n][n];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++)
      cin >> arr[i][j];
  }
  dp[0][0] = arr[0][0];
  dp[0][1] = arr[0][1];
  dp[0][2] = arr[0][2];
  for (int i = 1; i < n; i++) {
    dp[i][0] = arr[i][0] + max(dp[i - 1][1], dp[i - 1][2]);
    dp[i][1] = arr[i][1] + max(dp[i - 1][0], dp[i - 1][2]);
    dp[i][2] = arr[i][2] + 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:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int arr[n][3], dp[n][3];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++)
      cin >> arr[i][j];
  }
  dp[0][0] = arr[0][0];
  dp[0][1] = arr[0][1];
  dp[0][2] = arr[0][2];
  for (int i = 1; i < n; i++) {
    dp[i][0] = arr[i][0] + max(dp[i - 1][1], dp[i - 1][2]);
    dp[i][1] = arr[i][1] + max(dp[i - 1][0], dp[i - 1][2]);
    dp[i][2] = arr[i][2] + max(dp[i - 1][0], dp[i - 1][1]);
  }
  cout << max(dp[n - 1][0], max(dp[n - 1][1], dp[n - 1][2]));
}