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

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

#define INF 1e9
#define MAX 1005
#define xx first
#define yy second
#define pb push_back
#define mp make_pair
#define ull long long
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define nl '\n'
#define zai << ' ' <<
#define all(a) a.begin(), a.end()
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;

int a[MAX], b[MAX], c[MAX], n;
int dp[MAX][5];
bool vis[MAX][5];
// 0-A
// 1-B
// 2-C
int solve(int i, int last) {
  if (i == n)
    return 0;
  if (vis[i][last])
    return dp[i][last];
  vis[i][last] = 1;
  int ans = 0;
  for (int j = 0; j < 3; j++) {
    if (j != last) {
      if (j == 0) {
        ans = max(ans, a[i] + solve(i + 1, j));
      }
      if (j == 1) {
        ans = max(ans, b[i] + solve(i + 1, j));
      }
      if (j == 2) {
        ans = max(ans, c[i] + solve(i + 1, j));
      }
    }
  }
  return dp[i][last] = ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }
  cout << solve(0, -1);
}

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

#define INF 1e9
#define MAX 100005
#define xx first
#define yy second
#define pb push_back
#define mp make_pair
#define ull long long
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define nl '\n'
#define zai << ' ' <<
#define all(a) a.begin(), a.end()
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;

int a[MAX], b[MAX], c[MAX], n;
int dp[MAX][5];
bool vis[MAX][5];
// 0-A
// 1-B
// 2-C
int solve(int i, int last) {
  if (i == n)
    return 0;
  if (vis[i][last])
    return dp[i][last];
  vis[i][last] = 1;
  int ans = 0;
  for (int j = 0; j < 3; j++) {
    if (j != last) {
      if (j == 0) {
        ans = max(ans, a[i] + solve(i + 1, j));
      }
      if (j == 1) {
        ans = max(ans, b[i] + solve(i + 1, j));
      }
      if (j == 2) {
        ans = max(ans, c[i] + solve(i + 1, j));
      }
    }
  }
  return dp[i][last] = ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }
  cout << solve(0, -1);
}
Problem p03162 (C++)

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

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

  vector<vector<int>> h(n, vector<int>(3));
  rep(i, n) {
    rep(j, 3) { cin >> h.at(i).at(j); }
  }
  vector<vector<int>> dp(n, vector<int>(3, 0));

  rep(i, n) {

    if (i == 0) {
      rep(l, n) { dp.at(0).at(l) = h.at(0).at(l); }
      continue;
    }

    rep(j, 3) {
      rep(k, 3) {

        if (k != j) {
          dp.at(i).at(j) =
              max(dp.at(i).at(j), dp.at(i - 1).at(k) + h.at(i).at(j));
        }
      }
    }
  }

  int ans = 0;
  rep(i, 3) { ans = max(ans, dp.at(n - 1).at(i)); }

  cout << ans << endl;
}

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

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

  vector<vector<int>> h(n, vector<int>(3));
  rep(i, n) {
    rep(j, 3) { cin >> h.at(i).at(j); }
  }
  vector<vector<int>> dp(n, vector<int>(3, 0));

  rep(i, n) {

    if (i == 0) {
      rep(l, 3) { dp.at(0).at(l) = h.at(0).at(l); }
      continue;
    }

    rep(j, 3) {
      rep(k, 3) {

        if (k != j) {
          dp.at(i).at(j) =
              max(dp.at(i).at(j), dp.at(i - 1).at(k) + h.at(i).at(j));
        }
      }
    }
  }

  int ans = 0;
  rep(i, 3) { ans = max(ans, dp.at(n - 1).at(i)); }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define For(i, m, n) for (int i = m; i < n; i++)
#define FOR(i, n) for (int i = 0; i < n; i++)
#define ll long long
#define ull unsigned long long
#define lli long long int
#define pb push_back
ll mob = 1000000007;
using namespace std;
ll dp[11000][3];
int main()

{
  int n;
  std::cin >> n;
  int a[n][3];
  FOR(i, n) {
    FOR(j, 3) { cin >> a[i][j]; }
  }

  FOR(i, n) {
    FOR(j, 3) {
      if (i == 0) {
        dp[i][j] = a[i][j];

      } else {
        FOR(k, 3) {
          if (j == k)
            continue;
          dp[i][j] = max(dp[i][j], dp[i - 1][k] + a[i][j]);
        }
      }
    }
  }
  ll u = 0;
  if (u < dp[n - 1][0])
    u = dp[n - 1][0];
  if (u < dp[n - 1][1])
    u = dp[n - 1][1];
  if (u < dp[n - 1][2])
    u = dp[n - 1][2];

  cout << u;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define For(i, m, n) for (int i = m; i < n; i++)
#define FOR(i, n) for (int i = 0; i < n; i++)
#define ll long long
#define ull unsigned long long
#define lli long long int
#define pb push_back
ll mob = 1000000007;
using namespace std;
ll dp[1000000][3];
int main()

{
  int n;
  std::cin >> n;
  int a[n][3];
  FOR(i, n) {
    FOR(j, 3) { cin >> a[i][j]; }
  }

  FOR(i, n) {
    FOR(j, 3) {
      if (i == 0) {
        dp[i][j] = a[i][j];

      } else {
        FOR(k, 3) {
          if (j == k)
            continue;
          dp[i][j] = max(dp[i][j], dp[i - 1][k] + a[i][j]);
        }
      }
    }
  }
  ll u = 0;
  if (u < dp[n - 1][0])
    u = dp[n - 1][0];
  if (u < dp[n - 1][1])
    u = dp[n - 1][1];
  if (u < dp[n - 1][2])
    u = dp[n - 1][2];

  cout << u;
  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 Pair = pair<int, int>;
using Graph = vector<vector<int>>;
#define MOD 1000000007
#define INF 1000000007
#define SEP " "

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using Pair = pair<int, int>;
using Graph = vector<vector<int>>;
#define MOD 1000000007
#define INF 1000000007
#define SEP " "

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

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

using namespace std;

typedef long long int lli;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  vector<int> a(10000), b(100000), c(100000);
  vector<vector<int>> dp(3, vector<int>(100000, 0));

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

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

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

  return 0;
}


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

using namespace std;

typedef long long int lli;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  vector<int> a(100000), b(100000), c(100000);
  vector<vector<int>> dp(3, vector<int>(100000, 0));

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

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

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

  return 0;
}
Problem p03162 (C++)

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

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

const long INF = -(1L << 60);

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

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      scanf("%d", &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 res = 0;
  for (int i = 0; i < 3; i++) {
    chmax(res, dp[n][i]);
  }
  printf("%ld", res);
}


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

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

const long INF = -(1L << 60);

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

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      scanf("%d", &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 res = 0;
  for (int i = 0; i < 3; i++) {
    chmax(res, dp[n][i]);
  }
  printf("%ld", res);
}
Problem p03162 (C++)

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

using namespace std;

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

  vector<vector<long long>> v(n + 1, vector<long long>(n + 1)),
      dp(n + 1, vector<long long>(n + 1));

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> v[i][j];
    }
  }
  long long ans = INT_MIN;
  for (int i = 0; i < 3; i++) {
    dp[0][i] = v[0][i];
    if (dp[0][i] > ans)
      ans = dp[0][i];
  }

  for (int i = 1; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      if (!j)
        dp[i][j] = max(dp[i - 1][j + 1], dp[i - 1][j + 2]) + v[i][j];
      else if (j == 1)
        dp[i][j] = max(dp[i - 1][j - 1], dp[i - 1][j + 1]) + v[i][j];
      else
        dp[i][j] = max(dp[i - 1][j - 1], dp[i - 1][j - 2]) + v[i][j];

      if (dp[i][j] > ans)
        ans = dp[i][j];
    }
  }
  cout << ans << endl;

  return 0;
}


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

using namespace std;

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

  vector<vector<long long>> v(n + 1, vector<long long>(4)),
      dp(n + 1, vector<long long>(4));

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> v[i][j];
    }
  }
  long long ans = INT_MIN;
  for (int i = 0; i < 3; i++) {
    dp[0][i] = v[0][i];
    if (dp[0][i] > ans)
      ans = dp[0][i];
  }

  for (int i = 1; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      if (!j)
        dp[i][j] = max(dp[i - 1][j + 1], dp[i - 1][j + 2]) + v[i][j];
      else if (j == 1)
        dp[i][j] = max(dp[i - 1][j - 1], dp[i - 1][j + 1]) + v[i][j];
      else
        dp[i][j] = max(dp[i - 1][j - 1], dp[i - 1][j - 2]) + v[i][j];

      if (dp[i][j] > ans)
        ans = dp[i][j];
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p03162 (C++)

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

#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define ALL(x) x.begin(), x.end()

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

const int mod = 1e9 + 7;
const ll INF = 1e9;
const int MAX = 1e5;

// int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

struct edge {
  int to, cost;
}; // 辺
// vector<edge> graph[MAX]; // 隣接リスト
// bool visit[MAX]; // 訪問の有無

int n;           // 変数
int act[MAX][3]; // i日目の活動がxのとき得る幸福度
int dp[MAX][3]; // i日目に活動xをしたときの幸福度の総和の最大値

int rec(int i, int to) {
  if (dp[i][to] != 0)
    return dp[i][to];
  if (i == 0)
    return dp[i][to] = act[i][to];
  int r = 0;
  rep(ye, 3) {
    if (ye == to)
      continue;
    r = max(r, rec(i - 1, ye) + act[i][to]);
  }
  return r;
}

int main() {
  // input
  cin >> n;
  rep(i, n) {
    rep(j, 3) { cin >> act[i][j]; }
  }
  // solve
  int ans = 0;
  rep(to, 3) { ans = max(ans, rec(n - 1, to)); }
  // output
  cout << ans << "\n";
}

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

#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define ALL(x) x.begin(), x.end()

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

const int mod = 1e9 + 7;
const ll INF = 1e9;
const int MAX = 1e5;

// int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

struct edge {
  int to, cost;
}; // 辺
// vector<edge> graph[MAX]; // 隣接リスト
// bool visit[MAX]; // 訪問の有無

int n;           // 変数
int act[MAX][3]; // i日目の活動がxのとき得る幸福度
int dp[MAX][3]; // i日目に活動xをしたときの幸福度の総和の最大値

int rec(int i, int to) {
  if (dp[i][to] != 0)
    return dp[i][to];
  if (i == 0)
    return dp[i][to] = act[i][to];
  int r = 0;
  rep(ye, 3) {
    if (ye == to)
      continue;
    r = max(r, rec(i - 1, ye) + act[i][to]);
  }
  return dp[i][to] = r;
}

int main() {
  // input
  cin >> n;
  rep(i, n) {
    rep(j, 3) { cin >> act[i][j]; }
  }
  // solve
  int ans = 0;
  rep(to, 3) { ans = max(ans, rec(n - 1, to)); }
  // output
  cout << ans << "\n";
}
Problem p03162 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
int a[100001][3], dp[100001][3];
int n;
int ans = 0;

int solve(int i, int j) {
  if (i == n)
    return 0;
  if (j == 0) {
    return dp[i][j] = max(solve(i + 1, 1) + a[i][1], solve(i + 1, 2) + a[i][2]);
  } else if (j == 1) {
    return dp[i][j] = max(solve(i + 1, 0) + a[i][0], solve(i + 1, 2) + a[i][2]);
  } else if (j == 2) {
    return dp[i][j] = max(solve(i + 1, 0) + a[i][0], solve(i + 1, 1) + a[i][1]);
  }
  return dp[i][j];
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> a[i][j];
      dp[i][j] = -1;
    }
  }
  int ans = max(solve(0, 0), solve(0, 1));
  ans = max(ans, solve(0, 2));
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
int a[100001][3], dp[100001][3];
int n;
int ans = 0;

int solve(int i, int j) {
  if (i == n)
    return 0;
  if (dp[i][j] != -1)
    return dp[i][j];
  if (j == 0) {
    return dp[i][j] = max(solve(i + 1, 1) + a[i][1], solve(i + 1, 2) + a[i][2]);
  } else if (j == 1) {
    return dp[i][j] = max(solve(i + 1, 0) + a[i][0], solve(i + 1, 2) + a[i][2]);
  } else if (j == 2) {
    return dp[i][j] = max(solve(i + 1, 0) + a[i][0], solve(i + 1, 1) + a[i][1]);
  }
  return dp[i][j];
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> a[i][j];
      dp[i][j] = -1;
    }
  }
  int ans = max(solve(0, 0), solve(0, 1));
  ans = max(ans, solve(0, 2));
  cout << ans << endl;

  return 0;
}
Problem p03162 (C++)

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

int dp[2][100000];

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

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

  for (int i = 2; i <= n; i++) {
    for (int j = 0; j < 3; j++) {
      dp[j][i] += max(dp[(j + 1) % 3][i - 1], dp[(j + 2) % 3][i - 1]);
    }
  }

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

  return 0;
}

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

const int N = 1e5 + 10;

int dp[3][N];

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

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

  for (int i = 2; i <= n; i++) {
    for (int j = 0; j < 3; j++) {
      dp[j][i] += max(dp[(j + 1) % 3][i - 1], dp[(j + 2) % 3][i - 1]);
    }
  }

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

  return 0;
}
Problem p03162 (C++)

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

int N;
int a[100000], b[10000], c[100000];
int dp[10000][3];

signed main() {
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> a[i] >> b[i] >> c[i];

  dp[0][0] = a[0];
  dp[0][1] = b[0];
  dp[0][2] = c[0];

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

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


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

int N;
int a[100000], b[100000], c[1000000];
int dp[100000][3];

signed main() {
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> a[i] >> b[i] >> c[i];

  dp[0][0] = a[0];
  dp[0][1] = b[0];
  dp[0][2] = c[0];

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

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

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <ccomplex>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <set>
#include <sstream>
#include <tuple>
#include <unordered_map>
#include <vector>

using namespace std;

typedef long long ll;

#define rep(i, s, e) for (ll i = s; i < e; i++)
#define reft0(i, j) setfill('0') << setw(i) << j

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 main() {
  ll N;
  vector<ll> a;
  vector<ll> b;
  vector<ll> c;

  cin >> N;
  rep(i, 0, N) {
    ll tempA, tempB, tempC;
    cin >> tempA >> tempB >> tempC;
    a.push_back(tempA);
    b.push_back(tempB);
    c.push_back(tempC);
  }

  ll ansA[100003];
  ll ansB[100003];
  ll ansC[100003];
  rep(i, 0, 100010) {
    ansA[i] = 0;
    ansB[i] = 0;
    ansC[i] = 0;
  }
  ansA[0] = a[0];
  ansB[0] = b[0];
  ansC[0] = c[0];
  rep(idx, 1, N) {
    chMax(ansA[idx], max(ansB[idx - 1], ansC[idx - 1]) + a[idx]);
    chMax(ansB[idx], max(ansA[idx - 1], ansC[idx - 1]) + b[idx]);
    chMax(ansC[idx], max(ansA[idx - 1], ansB[idx - 1]) + c[idx]);
  }

  cout << max(ansA[N - 1], max(ansB[N - 1], ansC[N - 1])) << endl;

  return 0;
}


Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <ccomplex>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <set>
#include <sstream>
#include <tuple>
#include <unordered_map>
#include <vector>

using namespace std;

typedef long long ll;

#define rep(i, s, e) for (ll i = s; i < e; i++)
#define reft0(i, j) setfill('0') << setw(i) << j

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 main() {
  ll N;
  vector<ll> a;
  vector<ll> b;
  vector<ll> c;

  cin >> N;
  rep(i, 0, N) {
    ll tempA, tempB, tempC;
    cin >> tempA >> tempB >> tempC;
    a.push_back(tempA);
    b.push_back(tempB);
    c.push_back(tempC);
  }

  ll ansA[100003];
  ll ansB[100003];
  ll ansC[100003];
  rep(i, 0, 100003) {
    ansA[i] = 0;
    ansB[i] = 0;
    ansC[i] = 0;
  }
  ansA[0] = a[0];
  ansB[0] = b[0];
  ansC[0] = c[0];
  rep(idx, 1, N) {
    chMax(ansA[idx], max(ansB[idx - 1], ansC[idx - 1]) + a[idx]);
    chMax(ansB[idx], max(ansA[idx - 1], ansC[idx - 1]) + b[idx]);
    chMax(ansC[idx], max(ansA[idx - 1], ansB[idx - 1]) + c[idx]);
  }

  cout << max(ansA[N - 1], max(ansB[N - 1], ansC[N - 1])) << 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];
  int x = n;
  while (n--)
    cin >> arr[n - x - 1][0] >> arr[n - x - 1][1] >> arr[n - x - 1][2];
  int dp[n][3];
  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++) {
    int a, b, c;
    a = arr[i][0] + max(dp[i - 1][1], dp[i - 1][2]);
    b = arr[i][1] + max(dp[i - 1][0], dp[i - 1][2]);
    c = arr[i][2] + max(dp[i - 1][0], dp[i - 1][1]);
    dp[i][0] = a;
    dp[i][1] = b;
    dp[i][2] = c;
  }
  cout << max(dp[n - 1][2], max(dp[n - 1][0], dp[n - 1][1]));
}

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

int main() {
  int n;
  cin >> n;
  int arr[n][3];
  int x = n;
  while (x--)
    cin >> arr[n - x - 1][0] >> arr[n - x - 1][1] >> arr[n - x - 1][2];
  int dp[n][3];
  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++) {
    int a, b, c;
    a = arr[i][0] + max(dp[i - 1][1], dp[i - 1][2]);
    b = arr[i][1] + max(dp[i - 1][0], dp[i - 1][2]);
    c = arr[i][2] + max(dp[i - 1][0], dp[i - 1][1]);
    dp[i][0] = a;
    dp[i][1] = b;
    dp[i][2] = c;
  }
  cout << max(dp[n - 1][2], max(dp[n - 1][0], dp[n - 1][1]));
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll INF = 1LL << 60;
#define rep1(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, k, n) for (ll i = k; i < (n); i++)
using namespace std;
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 N;
ll a[10010][3];

// DP テーブル
ll dp[100010][3];

int main() {
  int N;
  cin >> N;
  rep1(i, N) rep1(j, 3) cin >> a[i][j];

  // ループ
  rep1(i, N) {
    rep1(j, 3) {
      rep1(k, 3) {
        if (j == k)
          continue;
        chmax(dp[i + 1][k], dp[i][j] + a[i][k]);
      }
    }
  }
  ll ans = 0;
  rep1(i, 3) chmax(ans, dp[N][i]);
  // 答え
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll INF = 1LL << 60;
#define rep1(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, k, n) for (ll i = k; i < (n); i++)
using namespace std;
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 N;
ll a[100010][3];

// DP テーブル
ll dp[100010][3];

int main() {
  int N;
  cin >> N;
  rep1(i, N) rep1(j, 3) cin >> a[i][j];

  // ループ
  rep1(i, N) {
    rep1(j, 3) {
      rep1(k, 3) {
        if (j == k)
          continue;
        chmax(dp[i + 1][k], dp[i][j] + a[i][k]);
      }
    }
  }
  ll ans = 0;
  rep1(i, 3) chmax(ans, dp[N][i]);
  // 答え
  cout << ans << endl;
}
Problem p03162 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> dp(100001, vector<int>(4, -1));
int dfs(vector<int> swim, vector<int> bugs, vector<int> homework, int index,
        int prev) {

  if (index == swim.size()) {
    return 0;
  }
  if (dp[index][prev] != -1)
    return dp[index][prev];
  int happiness = 0;
  if (prev != 1) {
    happiness =
        max(happiness, dfs(swim, bugs, homework, index + 1, 1) + swim[index]);
  }
  if (prev != 2) {
    happiness =
        max(happiness, dfs(swim, bugs, homework, index + 1, 2) + bugs[index]);
  }
  if (prev != 3) {
    happiness = max(happiness,
                    homework[index] + dfs(swim, bugs, homework, index + 1, 3));
  }

  dp[index][prev] = happiness;
  return happiness;
}

int main() {

  int n;
  cin >> n;
  vector<int> swim(n), bugs(n), homework(n);

  for (int i = 0; i < n; i++) {
    cin >> swim[i] >> bugs[i] >> homework[i];
  }

  cout << dfs(swim, bugs, homework, 0, 0);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> dp(100001, vector<int>(4, -1));
int dfs(vector<int> &swim, vector<int> &bugs, vector<int> &homework, int index,
        int prev) {

  if (index == swim.size()) {
    return 0;
  }
  if (dp[index][prev] != -1)
    return dp[index][prev];
  int happiness = 0;
  if (prev != 1) {
    happiness =
        max(happiness, dfs(swim, bugs, homework, index + 1, 1) + swim[index]);
  }
  if (prev != 2) {
    happiness =
        max(happiness, dfs(swim, bugs, homework, index + 1, 2) + bugs[index]);
  }
  if (prev != 3) {
    happiness = max(happiness,
                    homework[index] + dfs(swim, bugs, homework, index + 1, 3));
  }

  dp[index][prev] = happiness;
  return happiness;
}

int main() {

  int n;
  cin >> n;
  vector<int> swim(n), bugs(n), homework(n);

  for (int i = 0; i < n; i++) {
    cin >> swim[i] >> bugs[i] >> homework[i];
  }

  cout << dfs(swim, bugs, homework, 0, 0);
}
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 rep1(i, n) for (int i = 1; i < n + 1; i++)
#define sort(A) sort(A.begin(), A.end())
#define reverse(A) reverse(A.begin(), A.end());
typedef long long ll;

const ll MOD = 1000000007;

int main() {
  int n;
  cin >> n;
  vector<int> a(n + 1), b(n + 1), c(n + 1);
  rep1(i, n) cin >> a[i] >> b[i] >> c[i];
  int dp[3][100010] = {};
  dp[1][1] = a[1];
  dp[2][1] = b[1];
  dp[3][1] = c[1];
  for (int i = 2; i <= n; i++) {
    int way1;
    int way2;
    way1 = dp[2][i - 1] + a[i];
    way2 = dp[3][i - 1] + a[i];
    dp[1][i] = max(way1, way2);
    way1 = dp[1][i - 1] + b[i];
    way2 = dp[3][i - 1] + b[i];
    dp[2][i] = max(way1, way2);
    way1 = dp[1][i - 1] + c[i];
    way2 = dp[2][i - 1] + c[i];
    dp[3][i] = max(way1, way2);
  }
  int ans = 0;
  rep1(i, 3) ans = max(ans, dp[i][n]);
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define sort(A) sort(A.begin(), A.end())
#define reverse(A) reverse(A.begin(), A.end());
typedef long long ll;

const ll MOD = 1000000007;

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define endl "\n"
const int MOD = 1e9 + 7;
#ifndef HOME
#define cerr                                                                   \
  if (0)                                                                       \
  cerr
#endif
const int MaxN = 1e5 + 5;
int dp[MaxN][3];
int a[MaxN][3];
int n;
int solve(int i, int type) {
  if (i == n) {
    return 0;
  }
  int &ans = dp[i][type];
  if (ans != -1) {
    return ans;
  }
  int res = 0;
  if (i == 0) {
    res = max(res, a[i][0] + solve(i + 1, 1));
    res = max(res, a[i][0] + solve(i + 1, 2));
    res = max(res, a[i][1] + solve(i + 1, 2));
    res = max(res, a[i][1] + solve(i + 1, 0));
    res = max(res, a[i][2] + solve(i + 1, 1));
    res = max(res, a[i][2] + solve(i + 1, 0));
  } else {
    for (int j = 0; j < n; j++) {
      if (type != j) {
        res = max(res, a[i][type] + solve(i + 1, j));
      }
    }
  }
  return ans = res;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i][0] >> a[i][1] >> a[i][2];
  }
  memset(dp, -1, sizeof(dp));
  cout << solve(0, 0);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define endl "\n"
const int MOD = 1e9 + 7;
#ifndef HOME
#define cerr                                                                   \
  if (0)                                                                       \
  cerr
#endif
const int MaxN = 1e5 + 5;
int dp[MaxN][3];
int a[MaxN][3];
int n;
int solve(int i, int type) {
  if (i == n) {
    return 0;
  }
  int &ans = dp[i][type];
  if (ans != -1) {
    return ans;
  }
  int res = 0;
  if (i == 0) {
    res = max(res, a[i][0] + solve(i + 1, 1));
    res = max(res, a[i][0] + solve(i + 1, 2));
    res = max(res, a[i][1] + solve(i + 1, 2));
    res = max(res, a[i][1] + solve(i + 1, 0));
    res = max(res, a[i][2] + solve(i + 1, 1));
    res = max(res, a[i][2] + solve(i + 1, 0));
  } else {
    for (int j = 0; j < 3; j++) {
      if (type != j) {
        res = max(res, a[i][type] + solve(i + 1, j));
      }
    }
  }
  return ans = res;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i][0] >> a[i][1] >> a[i][2];
  }
  memset(dp, -1, sizeof(dp));
  cout << solve(0, 0);
  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
const int maxn = 1e4;
int a[maxn][maxn];
int dp[maxn][maxn];

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

    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]));
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
const int maxn = 1e5;
int a[maxn][3];
int dp[maxn][3];

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

    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]));
  return 0;
}
Problem p03162 (C++)

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

using namespace std;

#define endl "\n"
#define pb(s) push_back(s)
#define mp(a, b) make_pair(a, b)
#define f first
#define se second
#define ALL(v) v.begin(), v.end()
#define ALLA(arr, sz) arr, arr + sz
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr, sz) sort(ALLA(arr, sz))
#define REVERSEA(arr, sz) reverse(ALLA(arr, sz))
#define PI 3.14159265358979323846264338327950L
#define MOD 1000000007

typedef long long ll;

void solve() {
  int N;
  cin >> N;
  vector<int> dp(3);
  for (int i = 1; i <= N; ++i) {
    vector<int> c(3);
    vector<int> dp2(3, 0);
    for (int i = 0; i < N; ++i)
      cin >> c[i];
    for (int i = 0; i < 3; ++i) {
      for (int j = 0; j < 3; ++j) {
        if (i != j) {
          dp2[j] = max(dp2[j], dp[i] + c[j]);
        }
      }
    }
    dp = dp2;
  }
  cout << max(dp[0], max(dp[1], dp[2])) << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  // int t;
  // cin>>t;
  // while(t--)
  solve();
  return 0;
}


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

using namespace std;

#define endl "\n"
#define pb(s) push_back(s)
#define mp(a, b) make_pair(a, b)
#define f first
#define se second
#define ALL(v) v.begin(), v.end()
#define ALLA(arr, sz) arr, arr + sz
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr, sz) sort(ALLA(arr, sz))
#define REVERSEA(arr, sz) reverse(ALLA(arr, sz))
#define PI 3.14159265358979323846264338327950L
#define MOD 1000000007

typedef long long ll;

void solve() {
  int N;
  cin >> N;
  vector<int> dp(3);
  for (int i = 1; i <= N; ++i) {
    vector<int> c(3);
    vector<int> dp2(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) {
          dp2[j] = max(dp2[j], dp[i] + c[j]);
        }
      }
    }
    dp = dp2;
  }
  cout << max(dp[0], max(dp[1], dp[2])) << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  // int t;
  // cin>>t;
  // while(t--)
  solve();
  return 0;
}
Problem p03162 (C++)

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

long long t, n;
long long dp[100000][3];
long long c[4];
const int INF = 1e9 + 5;
int main() {
  cin >> n;

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= 3; j++) {
      long long x;
      cin >> x;
      for (int u = 1; u <= 3; u++) {
        if (u != j) {
          dp[i][j] = max(dp[i][j], dp[i - 1][u] + x);
        }
      }
    }
  }
  cout << max(dp[n][1], max(dp[n][2], dp[n][3]));
  /*for (int i=0; i<n; i++) {
      for (int j=1; j<=3; j++) {
          cout << dp[i][j] << " ";
      }
      cout << '\n';
  }*/
}

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

long long t, n;
long long dp[200000][3];
long long c[4];
const int INF = 1e9 + 5;
int main() {
  cin >> n;

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= 3; j++) {
      long long x;
      cin >> x;
      for (int u = 1; u <= 3; u++) {
        if (u != j) {
          dp[i][j] = max(dp[i][j], dp[i - 1][u] + x);
        }
      }
    }
  }
  cout << max(dp[n][1], max(dp[n][2], dp[n][3]));
  /*for (int i=0; i<n; i++) {
      for (int j=1; j<=3; j++) {
          cout << dp[i][j] << " ";
      }
      cout << '\n';
  }*/
}
Problem p03162 (C++)

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

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

  int n;
  cin >> n;

  vector<vector<long long>> dp(n, vector<long long>(3, 0));
  for (int i = 0; i < n; i++)
    cin >> dp[i][0] >> dp[i][1] >> dp[i][2];

  long long mx = INT_MIN;

  for (int i = 1; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      mx = INT_MIN;
      for (int k = 0; k < 3; k++) {
        if (k != j)
          mx = max(mx, dp[i - 1][k]);
      }
      dp[i][j] += mx;
    }
  }

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


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

int main() {

  int n;
  cin >> n;

  vector<vector<long long>> dp(n, vector<long long>(3, 0));
  for (int i = 0; i < n; i++)
    cin >> dp[i][0] >> dp[i][1] >> dp[i][2];

  long long mx = INT_MIN;

  for (int i = 1; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      mx = INT_MIN;
      for (int k = 0; k < 3; k++) {
        if (k != j)
          mx = max(mx, dp[i - 1][k]);
      }
      dp[i][j] += mx;
    }
  }

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

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

long long mmax(long long x, long long y) {
  if (x > y)
    return x;
  else
    return y;
}

int main() {
  int n;
  cin >> n;
  Graph abc(n);
  rep(i, n) {
    int a, b, c;
    cin >> a >> b >> c;
    abc[i].push_back(a);
    abc[i].push_back(b);
    abc[i].push_back(c);
  }

  long long dp[1000][3];
  rep(i, 1000) rep(j, 3) dp[i][j] = 0;
  dp[0][0] = abc[0][0];
  dp[0][1] = abc[0][1];
  dp[0][2] = abc[0][2];
  rep(i, n - 1) {
    rep(j, 3) {
      long long x = dp[i][(j + 1) % 3];
      long long y = dp[i][(j + 2) % 3];
      dp[i + 1][j] = mmax(x, y) + abc[i + 1][j];
    }
  }
  // rep(i, n) rep(j, 3)
  //	cout << i << j << ":" << dp[i][j] << endl;

  long long ans = 0;
  rep(i, 3) ans = mmax(ans, dp[n - 1][i]);
  cout << ans << endl;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

long long mmax(long long x, long long y) {
  if (x > y)
    return x;
  else
    return y;
}

int main() {
  int n;
  cin >> n;
  Graph abc(n);
  rep(i, n) {
    int a, b, c;
    cin >> a >> b >> c;
    abc[i].push_back(a);
    abc[i].push_back(b);
    abc[i].push_back(c);
  }

  long long dp[100009][3];
  rep(i, 100009) rep(j, 3) dp[i][j] = 0;
  dp[0][0] = abc[0][0];
  dp[0][1] = abc[0][1];
  dp[0][2] = abc[0][2];
  rep(i, n - 1) {
    rep(j, 3) {
      long long x = dp[i][(j + 1) % 3];
      long long y = dp[i][(j + 2) % 3];
      dp[i + 1][j] = mmax(x, y) + abc[i + 1][j];
    }
  }
  // rep(i, n) rep(j, 3)
  //	cout << i << j << ":" << dp[i][j] << endl;

  long long ans = 0;
  rep(i, 3) ans = mmax(ans, dp[n - 1][i]);
  cout << ans << endl;
}
Problem p03162 (C++)

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

using ll = long long;
using P = pair<int, int>;

const int INF = 1e9;
const int MOD = 1e9 + 7;

ll dp[10010][3];

int main() {
  int N;
  cin >> N;
  ll a[N][3];
  for (int i = 0; i < N; i++)
    for (int j = 0; j < 3; j++)
      cin >> a[i][j];

  for (int i = 0; i < 3; i++)
    dp[0][i] = a[0][i];
  for (int i = 1; i < N; i++) {
    dp[i][0] = a[i][0] + max(dp[i - 1][1], dp[i - 1][2]);
    dp[i][1] = a[i][1] + max(dp[i - 1][0], dp[i - 1][2]);
    dp[i][2] = a[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])) << endl;
  return 0;
}


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

using ll = long long;
using P = pair<int, int>;

const int INF = 1e9;
const int MOD = 1e9 + 7;

ll dp[100010][3];

int main() {
  int N;
  cin >> N;
  ll a[N][3];
  for (int i = 0; i < N; i++)
    for (int j = 0; j < 3; j++)
      cin >> a[i][j];

  for (int i = 0; i < 3; i++)
    dp[0][i] = a[0][i];
  for (int i = 1; i < N; i++) {
    dp[i][0] = a[i][0] + max(dp[i - 1][1], dp[i - 1][2]);
    dp[i][1] = a[i][1] + max(dp[i - 1][0], dp[i - 1][2]);
    dp[i][2] = a[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])) << endl;
  return 0;
}
Problem p03162 (C++)

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

#define ll long long
#define P pair<int, int>
#define F first
#define S second

const ll mod = 1000000007LL;

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

void solve() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }

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

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

int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
  freopen("practice/input.txt", "r", stdin);
  freopen("practice/output.txt", "w", stdout);
#endif

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int T = 1;
  while (T--) {
    solve();
  }

  return 0;
}

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

#define ll long long
#define P pair<int, int>
#define F first
#define S second

const ll mod = 1000000007LL;

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

void solve() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }

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

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

int main(int argc, char const *argv[]) {
  /*#ifndef ONLINE_JUDGE
    freopen("practice/input.txt", "r", stdin);
    freopen("practice/output.txt", "w", stdout);
  #endif*/

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int T = 1;
  while (T--) {
    solve();
  }

  return 0;
}
Problem p03162 (C++)

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

#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define int long long int
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 5;

int32_t main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  IOS int n;
  cin >> n;
  vector<int> dp(3);
  for (int d = 0; d < n; d++) {
    vector<int> ndp(3, 0);
    vector<int> c(3);
    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)
          ndp[j] = max(ndp[j], c[j] + dp[i]);
      }
    }
    dp = ndp;
  }
  cout << max({dp[0], dp[1], dp[2]});
  return 0;
}


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

#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define int long long int
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 5;

int32_t main() {

  IOS int n;
  cin >> n;
  vector<int> dp(3);
  for (int d = 0; d < n; d++) {
    vector<int> ndp(3, 0);
    vector<int> c(3);
    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)
          ndp[j] = max(ndp[j], c[j] + dp[i]);
      }
    }
    dp = ndp;
  }
  cout << max({dp[0], dp[1], dp[2]});
  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
// define debug for debugging
#define DEBUG
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define MOD 1000000007
#define MAX 100000 + 100
#define max3(a, b, c) max(a, max(b, c))
typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
int n, cevap = -1;
int arr[MAX][3], dp[MAX][3][3];
void dfs(int a, int b, int c) {
  if (a == -1) {
    dfs(0, 0, 1);
    dfs(0, 1, 2);
    dfs(0, 2, 0);
    cevap = max3(dp[0][0][1], dp[0][1][2], dp[0][2][0]);
  } else if (dp[a][b][c] == 0) {
    int mn = 0;
    if (a + 1 < n) {
      if (c != 0) {
        dfs(a + 1, c, 0);
        mn = dp[a + 1][c][0];
      }
      if (c != 1) {
        dfs(a + 1, c, 1);
        mn = max(mn, dp[a + 1][c][1]);
      }
      if (c != 2) {
        dfs(a + 1, c, 2);
        mn = max(mn, dp[a + 1][c][2]);
      }
    } else
      mn = 0;
    dp[a][b][c] = mn + arr[a][c];
    // cout << a << ' ' <<  b << ' ' << c << ' ' << dp[a][b][c] << ' ' << mn <<
    // ' ' << arr[a][c] << '\n';
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifdef DEBUG
  freopen("i.txt", "r", stdin);
  freopen("o.txt", "w", stdout);
#endif
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> arr[i][0] >> arr[i][1] >> arr[i][2];
  }
  dfs(-1, -1, -1);
  cout << cevap;
  return 0;
}


Fixed submission:
// define debug for debugging
// #define DEBUG
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define MOD 1000000007
#define MAX 100000 + 100
#define max3(a, b, c) max(a, max(b, c))
typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
int n, cevap = -1;
int arr[MAX][3], dp[MAX][3][3];
void dfs(int a, int b, int c) {
  if (a == -1) {
    dfs(0, 0, 1);
    dfs(0, 1, 2);
    dfs(0, 2, 0);
    cevap = max3(dp[0][0][1], dp[0][1][2], dp[0][2][0]);
  } else if (dp[a][b][c] == 0) {
    int mn = 0;
    if (a + 1 < n) {
      if (c != 0) {
        dfs(a + 1, c, 0);
        mn = dp[a + 1][c][0];
      }
      if (c != 1) {
        dfs(a + 1, c, 1);
        mn = max(mn, dp[a + 1][c][1]);
      }
      if (c != 2) {
        dfs(a + 1, c, 2);
        mn = max(mn, dp[a + 1][c][2]);
      }
    } else
      mn = 0;
    dp[a][b][c] = mn + arr[a][c];
    // cout << a << ' ' <<  b << ' ' << c << ' ' << dp[a][b][c] << ' ' << mn <<
    // ' ' << arr[a][c] << '\n';
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifdef DEBUG
  freopen("i.txt", "r", stdin);
  freopen("o.txt", "w", stdout);
#endif
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> arr[i][0] >> arr[i][1] >> arr[i][2];
  }
  dfs(-1, -1, -1);
  cout << cevap;
  return 0;
}
Problem p03162 (C++)

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

int n, dp[300005][5], arr[300005][5];

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < 3; j++)
      scanf("%d", arr[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) {
          dp[i + 1][k] = max(dp[i + 1][k], dp[i][j] + arr[i][k]);
        }
      }
    }
  }
  int res = max(dp[n][0], dp[n][1]);
  res = max(res, dp[n][2]);
  printf("%d\n", res);

  return 0;
}

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

int n, dp[300005][5], arr[300005][5];

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < 3; j++)
      scanf("%d", &arr[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) {
          dp[i + 1][k] = max(dp[i + 1][k], dp[i][j] + arr[i][k]);
        }
      }
    }
  }
  int res = max(dp[n][0], dp[n][1]);
  res = max(res, dp[n][2]);
  printf("%d\n", res);

  return 0;
}
Problem p03162 (C++)

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

using ll = long long int;
vector<vector<ll>> v;
ll n;
ll DP[100000][3];

ll backtrack(ll pos, char choice) {

  if (pos == n)
    return 0LL;

  vector<ll> b;

  for (ll i = 0; i < 3; i++) {

    char ch = (char)(i + 'a');

    if (choice != ch && DP[pos][i] != -1) {
      b.push_back(DP[pos][i]);
    }
  }

  if (b.size() == 2)
    return max(b[0], b[1]);

  ll answer = 0;

  for (ll i = 0; i < 3; i++) {

    char ch = (char)(i + 'a');

    if (choice != ch) {
      answer = max(answer, v[pos][ch - 'a'] + backtrack(pos + 1, ch));
    }
  }
  return answer;
}

int main() {

  cin >> n;

  for (ll i = 0; i < n; i++) {

    ll a, b, c;
    cin >> a >> b >> c;
    v.push_back({a, b, c});
  }

  memset(DP, -1, sizeof(DP));
  cout << backtrack(0, '.');

  return 0;
}

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

using ll = long long int;
vector<vector<ll>> v;
ll n;
ll DP[100000][3];

ll backtrack(ll pos, char choice) {

  if (pos == n)
    return 0LL;

  vector<ll> b;

  for (ll i = 0; i < 3; i++) {

    char ch = (char)(i + 'a');

    if (choice != ch && DP[pos][i] != -1) {
      b.push_back(DP[pos][i]);
    }
  }

  if (b.size() == 2)
    return max(b[0], b[1]);

  ll answer = 0;

  for (ll i = 0; i < 3; i++) {

    char ch = (char)(i + 'a');

    if (choice != ch) {
      DP[pos][i] = v[pos][ch - 'a'] + backtrack(pos + 1, ch);
      answer = max(answer, DP[pos][i]);
    }
  }
  return answer;
}

int main() {

  cin >> n;

  for (ll i = 0; i < n; i++) {

    ll a, b, c;
    cin >> a >> b >> c;
    v.push_back({a, b, c});
  }

  memset(DP, -1, sizeof(DP));
  cout << backtrack(0, '.');

  return 0;
}