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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define fo(i, n) for (int i = 0; i < n; i++)
#define rfo(i, n) for (int i = n - 1; i >= 0; i--)
#define Fo(i, q, n) for (int i = q; i < n; i++)
#define rFo(i, q, n) for (int i = n - 1; i >= q; i--)
#define fO(i, n, k) for (int i = 0; i < n; i += k)
#define FO(i, q, n, k) for (int i = q; i < n; i += k)
#define zero(a, n) memset(a, 0, sizeof(a[0]) * n)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define endl "\n"
#define Endl "\n"
#define trace(x) cerr << #x << ": " << x << " " << endl;
using namespace std;
const int MOD = 1000000007;
void print() { cout << endl; }
template <typename T, typename... Types> void print(T var1, Types... var2) {
  cout << var1 << " ";
  print(var2...);
}
// ceil of a/b
template <typename T> T fceil(T a, T b) { return (T)(a + b - 1) / b; }
// const int N=2e5;
// int arr[N+1];

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

/*NOTE: Take MOD after sorting and not before sorting!*/


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define fo(i, n) for (int i = 0; i < n; i++)
#define rfo(i, n) for (int i = n - 1; i >= 0; i--)
#define Fo(i, q, n) for (int i = q; i < n; i++)
#define rFo(i, q, n) for (int i = n - 1; i >= q; i--)
#define fO(i, n, k) for (int i = 0; i < n; i += k)
#define FO(i, q, n, k) for (int i = q; i < n; i += k)
#define zero(a, n) memset(a, 0, sizeof(a[0]) * n)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define endl "\n"
#define Endl "\n"
#define trace(x) cerr << #x << ": " << x << " " << endl;
using namespace std;
const int MOD = 1000000007;
void print() { cout << endl; }
template <typename T, typename... Types> void print(T var1, Types... var2) {
  cout << var1 << " ";
  print(var2...);
}
// ceil of a/b
template <typename T> T fceil(T a, T b) { return (T)(a + b - 1) / b; }
// const int N=2e5;
// int arr[N+1];

int main() {
  int n;
  cin >> n;
  vector<int> a(n, 0);
  vector<int> b(n, 0);
  vector<int> c(n, 0);
  fo(i, n) { cin >> a[i] >> b[i] >> c[i]; }

  vector<vector<int>> dp(n, vector<int>(3, 0));
  dp[0][0] = a[0];
  dp[0][1] = b[0];
  dp[0][2] = c[0];
  fo(i, n) {
    if (i == 0) {
      continue;
    }
    dp[i][0] = max(dp[i - 1][1], dp[i - 1][2]) + a[i];
    dp[i][1] = max(dp[i - 1][0], dp[i - 1][2]) + b[i];
    dp[i][2] = max(dp[i - 1][1], dp[i - 1][0]) + c[i];
  }
  cout << max({dp[n - 1][0], dp[n - 1][1], dp[n - 1][2]}) << endl;
  return 0;
}

/*NOTE: Take MOD after sorting and not before sorting!*/
Problem p03162 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define all(x) x.begin(), x.end()
#define MOD 1000000007
#define vi vector<int>
#define pii pair<int, int>
#define INF (int)1e9
#define pb push_back
#define fastIO                                                                 \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);

vector<vi> arr;
int n;
int cache[100010][3];

int dpSol(int i, int ch) {
  if (i == n)
    return 0;

  int &res = cache[i][ch];

  if (ch == 0)
    return res = max(arr[i][1] + dpSol(i + 1, 1), arr[i][2] + dpSol(i + 1, 2));
  if (ch == 1)
    return res = max(arr[i][0] + dpSol(i + 1, 0), arr[i][2] + dpSol(i + 1, 2));
  return res = max(arr[i][0] + dpSol(i + 1, 0), arr[i][1] + dpSol(i + 1, 1));
}

int main() {
  fastIO cin >> n;

  memset(cache, -1, sizeof(cache));

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

  cout << max({dpSol(0, 0), dpSol(0, 1), dpSol(0, 2)});

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define all(x) x.begin(), x.end()
#define MOD 1000000007
#define vi vector<int>
#define pii pair<int, int>
#define INF (int)1e9
#define pb push_back
#define fastIO                                                                 \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);

vector<vi> arr;
int n;
int cache[100010][3];

int dpSol(int i, int ch) {
  if (i == n)
    return 0;

  int &res = cache[i][ch];
  if (res != -1)
    return res;

  if (ch == 0)
    return res = max(arr[i][1] + dpSol(i + 1, 1), arr[i][2] + dpSol(i + 1, 2));
  if (ch == 1)
    return res = max(arr[i][0] + dpSol(i + 1, 0), arr[i][2] + dpSol(i + 1, 2));
  return res = max(arr[i][0] + dpSol(i + 1, 0), arr[i][1] + dpSol(i + 1, 1));
}

int main() {
  fastIO cin >> n;

  memset(cache, -1, sizeof(cache));

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

  cout << max({dpSol(0, 0), dpSol(0, 1), dpSol(0, 2)});

  return 0;
}
Problem p03162 (C++)

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

int main() {
  int n;
  cin >> n;
  vector<vector<ll>> a(n, vector<ll>(n));
  for (int i = 0; i < n; i++) {
    cin >> a[i][0];
    cin >> a[i][1];
    cin >> a[i][2];
  }
  ll dp[100010][3];

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      for (int k = 0; k < 3; k++) {
        if (j == k)
          continue;
        dp[i + 1][k] = max(dp[i + 1][k], dp[i][j] + a[i][k]);
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i < 3; i++)
    ans = max(ans, dp[n][i]);
  cout << ans << endl;
  return 0;
}

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

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

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      for (int k = 0; k < 3; k++) {
        if (j == k)
          continue;
        dp[i + 1][k] = max(dp[i + 1][k], dp[i][j] + a[i][k]);
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i < 3; i++)
    ans = max(ans, dp[n][i]);
  cout << ans << 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 dp[n][n];
  for (int i = 0; i < n; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    if (i == 0) {
      dp[i][0] = a;
      dp[i][1] = b;
      dp[i][2] = c;
    } else {
      dp[i][0] = max(dp[i - 1][1], dp[i - 1][2]) + a;
      dp[i][1] = max(dp[i - 1][0], dp[i - 1][2]) + b;
      dp[i][2] = max(dp[i - 1][0], dp[i - 1][1]) + c;
    }
  }
  cout << max(max(dp[n - 1][0], dp[n - 1][1]), dp[n - 1][2]) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  long long int dp[112345][3];
  // int dp[n][n];
  for (int i = 0; i < n; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    if (i == 0) {
      dp[i][0] = a;
      dp[i][1] = b;
      dp[i][2] = c;
    } else {
      dp[i][0] = max(dp[i - 1][1], dp[i - 1][2]) + a;
      dp[i][1] = max(dp[i - 1][0], dp[i - 1][2]) + b;
      dp[i][2] = max(dp[i - 1][0], dp[i - 1][1]) + c;
    }
  }
  cout << max(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;
#define pb push_back
#define loop(a, b, inc) for (int i = a; i <= b; i += inc)
typedef pair<int, int> pii;
typedef pair<int, string> ps;
typedef unsigned long long int llu;
typedef long long int ll;
typedef unsigned u;
const int MOD = 1e7 + 9, MAXN = 1e5 + 100;

int dp[MAXN][5], tab[MAXN][5];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  loop(1, n, 1) {
    for (int j = 1; j <= 3; ++j)
      cin >> tab[i][j];
  }
  for (int j = 1; j <= n; ++j) {
    dp[n][j] = tab[n][j];
  }
  for (int i = n - 1; i >= 0; --i) {
    for (int j = 1; j <= 3; ++j) {
      for (int k = 1; k <= 3; ++k) {
        if (k != j)
          dp[i][j] = max(dp[i][j], dp[i + 1][k] + tab[i][j]);
      }
    }
    // cout << dp[i][1] <<" "<< dp[i][2] << " " << dp[i][3] << endl;
  }
  cout << max(max(dp[0][1], dp[0][2]), dp[0][3]) << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define loop(a, b, inc) for (int i = a; i <= b; i += inc)
typedef pair<int, int> pii;
typedef pair<int, string> ps;
typedef unsigned long long int llu;
typedef long long int ll;
typedef unsigned u;
const int MOD = 1e7 + 9, MAXN = 1e6 + 100;

int dp[MAXN][5], tab[MAXN][5];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  loop(1, n, 1) {
    for (int j = 1; j <= 3; ++j)
      cin >> tab[i][j];
  }
  for (int j = 1; j <= n; ++j) {
    dp[n][j] = tab[n][j];
  }
  for (int i = n - 1; i >= 0; --i) {
    for (int j = 1; j <= 3; ++j) {
      for (int k = 1; k <= 3; ++k) {
        if (k != j)
          dp[i][j] = max(dp[i][j], dp[i + 1][k] + tab[i][j]);
      }
    }
    // cout << dp[i][1] <<" "<< dp[i][2] << " " << dp[i][3] << endl;
  }
  cout << max(max(dp[0][1], dp[0][2]), dp[0][3]) << endl;

  return 0;
}
Problem p03162 (C++)

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

using namespace std;

const int Nmax = 1e5 + 5;
int N, S, Max, p;
int a[Nmax][3], dp[Nmax][3];

int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; ++i) {
    scanf("%d%d%d", a[i][0], a[i][1], a[i][2]);
    for (int j = 0; j < 3; ++j) {
      for (int k = 0; k < 3; ++k)
        if (j != k)
          dp[i][j] = max(dp[i - 1][k], dp[i][j]);
      dp[i][j] += a[i][j];
    }
  }
  printf("%d", max({dp[N][0], dp[N][1], dp[N][2]}));
  /// cout << dp[N][0] << " " << dp[N][1] << " " << dp[N][2];
  return 0;
}


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

using namespace std;

const int Nmax = 1e5 + 5;
int N, S, Max, p;
int a[Nmax][3], dp[Nmax][3];

int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; ++i) {
    scanf("%d%d%d", &a[i][0], &a[i][1], &a[i][2]);
    for (int j = 0; j < 3; ++j) {
      for (int k = 0; k < 3; ++k)
        if (j != k)
          dp[i][j] = max(dp[i - 1][k], dp[i][j]);
      dp[i][j] += a[i][j];
    }
  }
  printf("%d", max({dp[N][0], dp[N][1], dp[N][2]}));
  /// cout << dp[N][0] << " " << dp[N][1] << " " << dp[N][2];
  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define PI acos(-1)
#define mx 1000
int fx[] = {0, 0, -1, 1};
int fy[] = {1, -1, 0, 0};
int a[mx], b[mx], c[mx];
int dp[mx][3];
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  int n, i, j, res = 0;
  cin >> n;
  for (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 (i = 1; i <= n; i++) {
    dp[i][0] = max(dp[i - 1][1], dp[i - 1][2]) + a[i];
    dp[i][1] = max(dp[i - 1][0], dp[i - 1][2]) + b[i];
    dp[i][2] = max(dp[i - 1][0], dp[i - 1][1]) + c[i];
  }
  res = max(max(dp[n - 1][0], dp[n - 1][1]), dp[n - 1][2]);
  cout << res << endl;

  return 0;
}
/*
3
10 40 70
20 50 80
30 60 90
*/


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define PI acos(-1)
#define mx 100005
int fx[] = {0, 0, -1, 1};
int fy[] = {1, -1, 0, 0};
int a[mx], b[mx], c[mx];
int dp[mx][3];
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  int n, i, j, res = 0;
  cin >> n;
  for (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 (i = 1; i <= n; i++) {
    dp[i][0] = max(dp[i - 1][1], dp[i - 1][2]) + a[i];
    dp[i][1] = max(dp[i - 1][0], dp[i - 1][2]) + b[i];
    dp[i][2] = max(dp[i - 1][0], dp[i - 1][1]) + c[i];
  }
  res = max(max(dp[n - 1][0], dp[n - 1][1]), dp[n - 1][2]);
  cout << res << endl;

  return 0;
}
/*
3
10 40 70
20 50 80
30 60 90
*/
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> dp(3, 0);

  for (int day = 0; day < n; day++) {
    vector<int> c(3, 0);
    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 < n; j++) {
        if (i != j) {
          new_dp[j] = max(new_dp[j], dp[i] + c[j]);
        }
      }
    }
    dp = new_dp;
  }

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

  return 0;
};

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> dp(3, 0);

  for (int day = 0; day < n; day++) {
    vector<int> c(3, 0);
    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]);
        }
      }
    }
    dp = new_dp;
  }

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

  return 0;
};
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
// Utkarsh-raj

#include <bits/stdc++.h>

#define ll long long int
#define vc vector
#define mod 100000000007
#define mp make_pair
#define pb push_back
#define nl cout << endl;
#define ff first
#define ss second

using namespace std;

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

  ifstream myFile("task.in");
  if (!myFile.fail()) {
    assert(freopen("task.in", "r", stdin));
  }

  int n;
  cin >> n;

  ll arr[n][n];

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

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

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

  return 0;
}


Fixed submission:
// Utkarsh-raj

#include <bits/stdc++.h>

#define ll long long int
#define vc vector
#define mod 100000000007
#define mp make_pair
#define pb push_back
#define nl cout << endl;
#define ff first
#define ss second

using namespace std;

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

  ifstream myFile("task.in");
  if (!myFile.fail()) {
    assert(freopen("task.in", "r", stdin));
  }

  int n;
  cin >> n;

  ll arr[n][3];

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

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

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

  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define rep(i, b) FOR(i, 0, b)
#define INF mugen
#define dump(x) cerr << #x << "=" << x << endl
#define all(a) (a).begin(), (a).end()
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<vii> viii;
typedef pair<int, int> P;
template <class T> void chmin(T &a, T const &b) {
  if (b < a)
    a = b;
}
template <typename T> string to_string(const T &n) {
  ostringstream stm;
  stm << n;
  return stm.str();
}
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
using ll = long long;
const ll mod = LLONG_MAX;

ll N, ans;
ll dp[11234][2][2][2]; // i番目の日の最大の幸福  i-1日目に使ったかどうか
ll a[112345], b[112345], c[112345];
signed main() {
  cin >> N;
  rep(i, N) { cin >> a[i] >> b[i] >> c[i]; }

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

  ans = max({dp[N][0][0][1], dp[N][0][1][0], dp[N][1][0][0]});
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define rep(i, b) FOR(i, 0, b)
#define INF mugen
#define dump(x) cerr << #x << "=" << x << endl
#define all(a) (a).begin(), (a).end()
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<vii> viii;
typedef pair<int, int> P;
template <class T> void chmin(T &a, T const &b) {
  if (b < a)
    a = b;
}
template <typename T> string to_string(const T &n) {
  ostringstream stm;
  stm << n;
  return stm.str();
}
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
using ll = long long;
const ll mod = LLONG_MAX;

ll N, ans;
ll dp[112345][2][2][2]; // i番目の日の最大の幸福  i-1日目に使ったかどうか
ll a[112345], b[112345], c[112345];
signed main() {
  cin >> N;
  rep(i, N) { cin >> a[i] >> b[i] >> c[i]; }

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

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

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

long long cache[10005][3];

long long vacate(vector<long long> *v, int n, int i, int activity) {
  if (i >= n) {
    return 0;
  }

  if (activity != -1 && cache[i][activity] != -1) {
    return cache[i][activity];
  }

  long long points = 0;
  for (int m = 0; m < 3; m++) {
    if (activity != m) {
      points = max(points, v[i][m] + vacate(v, n, i + 1, m));
    }
  }

  cache[i][activity] = points;
  return points;
}

int main() {
  int n;
  cin >> n;
  vector<long long> v[n];

  int a, b, c;
  for (int i = 0; i < n; i++) {
    cin >> a >> b >> c;
    v[i].push_back(a);
    v[i].push_back(b);
    v[i].push_back(c);
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      cache[i][j] = -1;
    }
  }

  long long points = vacate(v, n, 0, -1);
  cout << points << endl;
}


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

long long cache[100005][3];

long long vacate(vector<long long> *v, int n, int i, int activity) {
  if (i >= n) {
    return 0;
  }

  if (activity != -1 && cache[i][activity] != -1) {
    return cache[i][activity];
  }

  long long points = 0;
  for (int m = 0; m < 3; m++) {
    if (activity != m) {
      points = max(points, v[i][m] + vacate(v, n, i + 1, m));
    }
  }

  cache[i][activity] = points;
  return points;
}

int main() {
  int n;
  cin >> n;
  vector<long long> v[n];

  int a, b, c;
  for (int i = 0; i < n; i++) {
    cin >> a >> b >> c;
    v[i].push_back(a);
    v[i].push_back(b);
    v[i].push_back(c);
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      cache[i][j] = -1;
    }
  }

  long long points = vacate(v, n, 0, -1);
  cout << points << endl;
}
Problem p03162 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <stack>
#include <string>
#include <type_traits>

using namespace std;

using ll = long long;

typedef pair<int, int> P;

#define rep(i, N) for (long long i = 0; i < (long long)N; ++i)

static int mod = 1000000007;
static int large = 1000000000;

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

int N;

int a[100001];
int b[100001];
int c[100001];

int dp[100001][3];

inline int change(int x, int i) {
  if (x == 0)
    return a[i - 1];
  else if (x == 1)
    return b[i - 1];
  else if (x == 2)
    return c[i - 1];
  return -1;
}

// i日目,jで遊ぶ
int f(int i, int j) {
  if (dp[i][j] != -1)
    dp[i][j];
  if (i == 0)
    return dp[0][j] = 0;
  int sec = 0;
  rep(k, 3) {
    if (k != j) {
      sec = max(sec, f(i - 1, k) + change(j, i));
    }
  }
  return dp[i][j] = sec;
}

int main() {
  cin >> N;
  rep(i, N) { cin >> a[i] >> b[i] >> c[i]; }
  rep(i, 100001) {
    rep(j, 3) { dp[i][j] = -1; }
  }
  cout << max(f(N, 0), max(f(N, 1), f(N, 2))) << endl;
  return 0;
}


Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <stack>
#include <string>
#include <type_traits>

using namespace std;

using ll = long long;

typedef pair<int, int> P;

#define rep(i, N) for (long long i = 0; i < (long long)N; ++i)

static int mod = 1000000007;
static int large = 1000000000;

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

int N;

int a[100001];
int b[100001];
int c[100001];

int dp[100001][3];

inline int change(int x, int i) {
  if (x == 0)
    return a[i - 1];
  else if (x == 1)
    return b[i - 1];
  else if (x == 2)
    return c[i - 1];
  return -1;
}

// i日目,jで遊ぶ
int f(int i, int j) {
  if (dp[i][j] != -1)
    return dp[i][j];
  if (i == 0)
    return dp[0][j] = 0;
  int sec = 0;
  rep(k, 3) {
    if (k != j) {
      sec = max(sec, f(i - 1, k) + change(j, i));
    }
  }
  return dp[i][j] = sec;
}

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int n;
int a[100005][4];
int dp[100005][4];
// int mx=INT_MIN;
int solve(int i, int f) {
  if (i == n)
    return 0;
  if (dp[i][f] != -1)
    return dp[i][f];
  int mx = INT_MIN;
  if (f == 0) {
    mx = max(mx, a[i][1] + solve(i + 1, 1));
    mx = max(mx, a[i][2] + solve(i + 1, 2));
    mx = max(mx, a[i][3] + solve(i + 1, 3));
  } else if (f == 1) {
    mx = max(mx, a[i][2] + solve(i + 1, 2));
    mx = max(mx, a[i][3] + solve(i + 1, 3));
  } else if (f == 2) {
    mx = max(mx, a[i][1] + solve(i + 1, 1));
    mx = max(mx, a[i][3] + solve(i + 1, 3));
  } else {
    mx = max(mx, a[i][1] + solve(i + 1, 1));
    mx = max(mx, a[i][2] + solve(i + 1, 2));
  }
  return dp[n][f] = mx;
}

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n;
int a[100005][4];
int dp[100005][4];
// int mx=INT_MIN;
int solve(int i, int f) {
  if (i == n)
    return 0;
  if (dp[i][f] != -1)
    return dp[i][f];
  int mx = INT_MIN;
  if (f == 0) {
    mx = max(mx, a[i][1] + solve(i + 1, 1));
    mx = max(mx, a[i][2] + solve(i + 1, 2));
    mx = max(mx, a[i][3] + solve(i + 1, 3));
  } else if (f == 1) {
    mx = max(mx, a[i][2] + solve(i + 1, 2));
    mx = max(mx, a[i][3] + solve(i + 1, 3));
  } else if (f == 2) {
    mx = max(mx, a[i][1] + solve(i + 1, 1));
    mx = max(mx, a[i][3] + solve(i + 1, 3));
  } else {
    mx = max(mx, a[i][1] + solve(i + 1, 1));
    mx = max(mx, a[i][2] + solve(i + 1, 2));
  }
  return dp[i][f] = mx;
}

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

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

#define f first
#define s second
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define debug(x) cout << #x << " " << x << "\n"

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;

const ll N = 1e5 + 3;

ll n, k, a[N][4], res[N][4];

ll _max(ll x, ll y, ll z) { return max(max(x, y), z); }

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

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

  for (ll i = 1; i <= n; ++i) {
    for (ll j = 1; j <= n; ++j) {
      for (ll k = 1; k <= 3; ++k) {
        if (k == j)
          continue;
        res[i][j] = max(res[i][j], res[i - 1][k] + a[i][j]);
      }
    }
  }
  cout << _max(res[n][1], res[n][2], res[n][3]);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

#define f first
#define s second
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define debug(x) cout << #x << " " << x << "\n"

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;

const ll N = 1e5 + 3;

ll n, k, a[N][4], res[N][4];

ll _max(ll x, ll y, ll z) { return max(max(x, y), z); }

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

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

  for (ll i = 1; i <= n; ++i) {
    for (ll j = 1; j <= 3; ++j) {
      for (ll k = 1; k <= 3; ++k) {
        if (k == j)
          continue;
        res[i][j] = max(res[i][j], res[i - 1][k] + a[i][j]);
      }
    }
  }
  cout << _max(res[n][1], res[n][2], res[n][3]);

  return 0;
}
Problem p03162 (C++)

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

#define int long long

#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);

#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vp vector<pii>
#define vs vector<string>
#define all(v) v.begin(), v.end()

#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define ff first
#define ss second
#define sz(x) ((int)(x).size())
#define rep(i, l, r) for (int i = (l); i < (r); ++i)

#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) ((a) * (b)) / gcd((a), (b))

#define pi 3.14159265358979323846 // 20 decimals
#define MOD 1000000007

int32_t main() {

  IOS
#ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  vi dp(3, 0);
  vi new_dp(3, 0);
  vi happy(3, 0);
  for (int vac = 0; vac < n; vac++) {
    for (int i = 0; i < 3; i++)
      cin >> happy[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] + happy[j]);
        }
      }
    }
    dp = new_dp;
  }

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

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

#define int long long

#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);

#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vp vector<pii>
#define vs vector<string>
#define all(v) v.begin(), v.end()

#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define ff first
#define ss second
#define sz(x) ((int)(x).size())
#define rep(i, l, r) for (int i = (l); i < (r); ++i)

#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) ((a) * (b)) / gcd((a), (b))

#define pi 3.14159265358979323846 // 20 decimals
#define MOD 1000000007

int32_t main() {

  IOS

      int n;
  cin >> n;
  vi dp(3, 0);
  vi new_dp(3, 0);
  vi happy(3, 0);
  for (int vac = 0; vac < n; vac++) {
    for (int i = 0; i < 3; i++)
      cin >> happy[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] + happy[j]);
        }
      }
    }
    dp = new_dp;
  }

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

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

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

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

int main() {
  int N;
  cin >> N;
  int val[N][3];
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> val[i][j];
    }
  }
  long long int dp[N][3];
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < 3; j++) {
      dp[i][j] = -100000;
    }
  }
  for (int i = 0; i < 3; i++) {
    dp[0][i] = val[0][i];
  }
  for (int i = 1; i < N; i++) {
    for (int j = 0; j < 3; j++) {
      dp[i][j] = max(dp[i - 1][(j + 1) % 3] + val[i][j],
                     dp[i - 1][(j + 2) % 3] + val[i][j]);
    }
  }
  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;

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

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

  for (int i = 0; i < 3; i++)
    dp[0][i] = a[0][i];

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

  int ans = 0;

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

  cout << ans << endl;
}


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

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

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

  for (int i = 0; i < 3; i++)
    dp[0][i] = a[0][i];

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

  int ans = 0;

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

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

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

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  ll n, i;
  cin >> n;
  ll a[n], b[n], c[n];
  for (i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }
  ll dp[n][3];
  dp[0][0] = a[0];
  dp[0][1] = b[0];
  dp[0][2] = c[0];

  for (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<<dp[i][0]<<" "<<dp[i][1]<<" "<<dp[i][2]<<endl;
  }
  cout << max({dp[n - 1][0], dp[n - 1][1], dp[n - 1][2]}) << endl;
}

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

int main() {

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

  for (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<<dp[i][0]<<" "<<dp[i][1]<<" "<<dp[i][2]<<endl;
  }
  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;
int a[10001][4], f[10001][4] = {0};
int main() {
  int i, j, k, n;
  scanf("%d", &n);
  for (i = 1; i <= n; i++)
    for (j = 1; j <= 3; j++)
      scanf("%d", &a[i][j]);
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= 3; j++) {
      for (k = 1; k <= 3; k++) {
        if (k == j)
          continue;
        f[i][j] = max(f[i][j], f[i - 1][k] + a[i][j]);
      }
    }
  }
  printf("%d", max(f[n][1], max(f[n][2], f[n][3])));
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int a[100001][4], f[100001][4] = {0};
int main() {
  int i, j, k, n;
  scanf("%d", &n);
  for (i = 1; i <= n; i++)
    for (j = 1; j <= 3; j++)
      scanf("%d", &a[i][j]);
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= 3; j++) {
      for (k = 1; k <= 3; k++) {
        if (k == j)
          continue;
        f[i][j] = max(f[i][j], f[i - 1][k] + a[i][j]);
      }
    }
  }
  printf("%d", max(f[n][1], max(f[n][2], f[n][3])));
  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define MOD ((int)(1e9 + 7))
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define int long long
#define float long double

int ans = 0;

int32_t main() {
  IOS int n;
  cin >> n;
  vector<vector<int>> A(3, vector<int>(n + 1)), dp(3, vector<int>(n + 1));

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

  for (int i = 0; i < 3; i++) {
    dp[i][1] = A[i][1];
  }

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MOD ((int)(1e9 + 7))
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define int long long
#define float long double

int ans = 0;

int32_t main() {
  IOS int n;
  cin >> n;
  vector<vector<int>> A(3, vector<int>(n + 1)), dp(3, vector<int>(n + 1));

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

  for (int i = 0; i < 3; i++) {
    dp[i][1] = A[i][1];
  }

  for (int i = 1; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      dp[j][i + 1] = max(dp[(j + 1) % 3][i] + A[j][i + 1],
                         dp[(j + 2) % 3][i] + A[j][i + 1]);
    }
  }
  cout << max({dp[0][n], dp[1][n], dp[2][n]}) << endl;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
// C - Vacation
#include <iostream>
#include <vector>
#define ll long long

using namespace std;

ll n;

// vector<ll> dp(3, vector<ll>(1000005, -1));
ll dp[3][1000005];

ll cost(vector<vector<ll>> &matrix, ll selected, ll day) {
  if (dp[day][selected] != -1)
    return dp[day][selected];
  if (day == matrix.size())
    return matrix[day][selected];
  dp[day][selected] =
      max(cost(matrix, (selected + 1) % 3, day + 1) + matrix[day][selected],
          cost(matrix, (selected + 2) % 3, day + 1) + matrix[day][selected]);
  return dp[day][selected];
}

int main() {
  cin >> n;
  vector<vector<ll>> matrix(n, vector<ll>(3, -1));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      dp[i][j] = -1;
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> matrix[i][j];
    }
  }
  cout << max(max(cost(matrix, 0, 0), cost(matrix, 1, 0)), cost(matrix, 2, 0));
}

Fixed submission:
// C - Vacation
#include <iostream>
#include <vector>
#define ll long long

using namespace std;

ll n;

// vector<ll> dp(3, vector<ll>(1000005, -1));
ll dp[1000005][3];

ll cost(vector<vector<ll>> &matrix, ll selected, ll day) {
  if (dp[day][selected] != -1)
    return dp[day][selected];
  if (day == matrix.size())
    return matrix[day][selected];
  dp[day][selected] =
      max(cost(matrix, (selected + 1) % 3, day + 1) + matrix[day][selected],
          cost(matrix, (selected + 2) % 3, day + 1) + matrix[day][selected]);
  return dp[day][selected];
}

int main() {
  cin >> n;
  vector<vector<ll>> matrix(n, vector<ll>(3, -1));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      dp[i][j] = -1;
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> matrix[i][j];
    }
  }
  cout << max(max(cost(matrix, 0, 0), cost(matrix, 1, 0)), cost(matrix, 2, 0));
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):

#include <iostream>
#include <vector>
using namespace std;

#include <algorithm>
#define str(a) to_string(a)
#define int long long
#define pb push_back

int32_t main() {
  int n;
  cin >> n;
  vector<vector<int>> vec(n, vector<int>(n, 0));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++)
      cin >> vec[i][j];
  }
  int dp[n][3];
  dp[0][0] = vec[0][0];
  dp[0][1] = vec[0][1];
  dp[0][2] = vec[0][2];
  for (int i = 1; i < n; i++) {
    dp[i][0] = vec[i][0] + max(dp[i - 1][1], dp[i - 1][2]);
    dp[i][1] = vec[i][1] + max(dp[i - 1][0], dp[i - 1][2]);
    dp[i][2] = vec[i][2] + max(dp[i - 1][1], dp[i - 1][0]);
  }
  cout << max(max(dp[n - 1][0], dp[n - 1][1]), dp[n - 1][2]) << endl;
}


Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

#include <algorithm>
#define str(a) to_string(a)
#define int long long
#define pb push_back

int32_t main() {
  int n;
  cin >> n;
  int vec[n][3];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++)
      cin >> vec[i][j];
  }
  int dp[n][3];
  dp[0][0] = vec[0][0];
  dp[0][1] = vec[0][1];
  dp[0][2] = vec[0][2];
  for (int i = 1; i < n; i++) {
    dp[i][0] = vec[i][0] + max(dp[i - 1][1], dp[i - 1][2]);
    dp[i][1] = vec[i][1] + max(dp[i - 1][0], dp[i - 1][2]);
    dp[i][2] = vec[i][2] + max(dp[i - 1][1], dp[i - 1][0]);
  }
  cout << max(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>
#include <cstdio>
typedef long long int ll;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR_IN(i, a, b) for (int i = (a); i < (b); i++)
#define BETWEEN(x, a, b) ((x) >= (a) && (x) <= (b))

using namespace std;

/*
N 10^5
a,b,c 10^4

N
a1 b1 c1
...
aN bN cN
 */

int N;
ll dp[100000][3];
int h[3];

int main() {
  cin >> N;

  REP(i, N) {
    REP(j, 3)
    cin >> h[j];

    REP(j, 3) {
      REP(k, 3) {
        if (k == j)
          continue;
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][k] + h[j]);
      }
    }
  }

  ll ans = 0;
  REP(j, 3)
  ans = max(ans, dp[N][j]);

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <cstdio>
typedef long long int ll;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR_IN(i, a, b) for (int i = (a); i < (b); i++)
#define BETWEEN(x, a, b) ((x) >= (a) && (x) <= (b))

using namespace std;

/*
N 10^5
a,b,c 10^4

N
a1 b1 c1
...
aN bN cN
 */

int N;
ll dp[100001][3];
int h[3];

int main() {
  cin >> N;

  REP(i, N) {
    REP(j, 3)
    cin >> h[j];

    REP(j, 3) {
      REP(k, 3) {
        if (k == j)
          continue;
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][k] + h[j]);
      }
    }
  }

  ll ans = 0;
  REP(j, 3)
  ans = max(ans, dp[N][j]);

  cout << ans << endl;

  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define endl "\n"
#define mod 1000000007
using namespace std;

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

  ll dp[10001][3];
  ll arr[10001][3];

  for (ll i = 0; i < n; i++) {
    ll x, y, z;
    cin >> x >> y >> z;

    arr[i][0] = x;
    arr[i][1] = y;
    arr[i][2] = z;
  }

  for (ll i = 0; i < n; i++) {
    if (i == 0) {
      dp[i][0] = arr[i][0];
      dp[i][1] = arr[i][1];
      dp[i][2] = arr[i][2];
      continue;
    }
    dp[i][0] = max(arr[i][0] + dp[i - 1][1], arr[i][0] + dp[i - 1][2]);
    dp[i][1] = max(arr[i][1] + dp[i - 1][0], arr[i][1] + dp[i - 1][2]);
    dp[i][2] = max(arr[i][2] + dp[i - 1][0], arr[i][2] + dp[i - 1][1]);
  }

  cout << *max_element(dp[n - 1], dp[n - 1] + 3) << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define endl "\n"
#define mod 1000000007
using namespace std;

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

  ll dp[100001][3];
  ll arr[100001][3];

  for (ll i = 0; i < n; i++) {
    ll x, y, z;
    cin >> x >> y >> z;

    arr[i][0] = x;
    arr[i][1] = y;
    arr[i][2] = z;
  }

  for (ll i = 0; i < n; i++) {
    if (i == 0) {
      dp[i][0] = arr[i][0];
      dp[i][1] = arr[i][1];
      dp[i][2] = arr[i][2];
      continue;
    }
    dp[i][0] = max(arr[i][0] + dp[i - 1][1], arr[i][0] + dp[i - 1][2]);
    dp[i][1] = max(arr[i][1] + dp[i - 1][0], arr[i][1] + dp[i - 1][2]);
    dp[i][2] = max(arr[i][2] + dp[i - 1][0], arr[i][2] + dp[i - 1][1]);
  }

  cout << *max_element(dp[n - 1], dp[n - 1] + 3) << endl;

  return 0;
}
Problem p03162 (C++)

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

#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define rep(i, n) FOR(i, 0, n - 1)
typedef long long ll;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You
// use the default template now. You can remove this line by using your custom
// template)
int main() {
  long long N;
  cin >> N;

  ll act[N][3];
  rep(i, N) {
    rep(j, 3) { cin >> act[i][j]; }
  }

  vector<vector<ll>> dp(N, vector<ll>(3));

  rep(i, N) {
    rep(j, 3) {
      rep(k, 3) {
        if (j == k)
          continue;
        chmax(dp[i + 1][k], dp[i][j] + act[i][k]);
      }
    }
  }

  ll res = 0;
  rep(j, 3) chmax(res, dp[N][j]);
  cout << res << endl;
}


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

#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define rep(i, n) FOR(i, 0, n - 1)
typedef long long ll;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You
// use the default template now. You can remove this line by using your custom
// template)
int main() {
  long long N;
  cin >> N;

  ll act[N][3];
  rep(i, N) {
    rep(j, 3) { cin >> act[i][j]; }
  }

  vector<vector<ll>> dp(N + 10, vector<ll>(3));

  rep(i, N) {
    rep(j, 3) {
      rep(k, 3) {
        if (j == k)
          continue;
        chmax(dp[i + 1][k], dp[i][j] + act[i][k]);
      }
    }
  }

  ll res = 0;
  rep(j, 3) chmax(res, dp[N][j]);
  cout << res << endl;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define FAST_INPUT                                                             \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define ll long long

using namespace std;

void solve() {
  int n;
  cin >> n;
  vector<vector<int>> nums(n, vector<int>(3));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> nums[i][j];
    }
  }

  vector<vector<int>> dp(n, vector<int>(3, 0));
  for (int i = 0; i < 3; i++) {
    dp[0][i] = nums[0][i];
  }

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

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

int main() {
  FAST_INPUT;

  solve();

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define FAST_INPUT                                                             \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define ll long long

using namespace std;

void solve() {
  int n;
  cin >> n;
  vector<vector<int>> nums(n, vector<int>(3));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> nums[i][j];
    }
  }

  vector<vector<int>> dp(n, vector<int>(3, 0));
  for (int i = 0; i < 3; i++) {
    dp[0][i] = nums[0][i];
  }

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

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

int main() {
  FAST_INPUT;

  solve();

  return 0;
}
Problem p03162 (C++)

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

int dp[100000 + 1][3];

struct vacation {
  int a, b, c;
};

int solve(vector<vacation> v, int i, int n, int j) {

  if (i > n)
    return 0;

  if (dp[i][j] != -1)
    return dp[i][j];

  int sol = 0;

  if (j == 0)
    dp[i][j] =
        max(v[i].b + solve(v, i + 1, n, 1), v[i].c + solve(v, i + 1, n, 2));
  else if (j == 1)
    dp[i][j] =
        max(v[i].a + solve(v, i + 1, n, 0), v[i].c + solve(v, i + 1, n, 2));
  else
    dp[i][j] =
        max(v[i].b + solve(v, i + 1, n, 1), v[i].a + solve(v, i + 1, n, 0));

  return dp[i][j];
}

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

  vector<vacation> v(n + 1);
  int x, y, z;

  for (int i = 1; i <= n; i++) {

    cin >> v[i].a;
    cin >> v[i].b;
    cin >> v[i].c;
  }

  for (int i = 0; i <= n; i++)
    for (int j = 0; j < 3; j++)
      dp[i][j] = -1;

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

  int A = v[1].a + solve(v, 2, n, 0);

  int B = v[1].b + solve(v, 2, n, 1);

  int C = v[1].c + solve(v, 2, n, 2);

  cout << max(A, max(B, C)) << endl;

  return 0;
}

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

int dp[100000 + 1][3];

struct vacation {
  int a, b, c;
};

int solve(vector<vacation> &v, int i, int n, int j) {

  if (i > n)
    return 0;

  if (dp[i][j] != -1)
    return dp[i][j];

  int sol = 0;

  if (j == 0)
    dp[i][j] =
        max(v[i].b + solve(v, i + 1, n, 1), v[i].c + solve(v, i + 1, n, 2));
  else if (j == 1)
    dp[i][j] =
        max(v[i].a + solve(v, i + 1, n, 0), v[i].c + solve(v, i + 1, n, 2));
  else
    dp[i][j] =
        max(v[i].b + solve(v, i + 1, n, 1), v[i].a + solve(v, i + 1, n, 0));

  return dp[i][j];
}

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

  vector<vacation> v(n + 1);
  int x, y, z;

  for (int i = 1; i <= n; i++) {

    cin >> v[i].a;
    cin >> v[i].b;
    cin >> v[i].c;
  }

  for (int i = 0; i <= n; i++)
    for (int j = 0; j < 3; j++)
      dp[i][j] = -1;

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

  int A = v[1].a + solve(v, 2, n, 0);

  int B = v[1].b + solve(v, 2, n, 1);

  int C = v[1].c + solve(v, 2, n, 2);

  cout << max(A, max(B, C)) << endl;

  return 0;
}