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

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

using namespace std;

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

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

using namespace std;

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

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

int main() {
  int cost[10000][3], N;
  int result[10000][3];

  std::cin >> N;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < 3; j++) {
      std::cin >> cost[i][j];
    }
  }
  for (int i = 0; i < 3; i++)
    result[0][i] = cost[0][i];

  for (int i = 1; i < N; i++) {
    for (int j = 0; j < 3; j++) {
      result[i][0] = std::max(result[i - 1][1], result[i - 1][2]) + cost[i][0];
      result[i][1] = std::max(result[i - 1][0], result[i - 1][2]) + cost[i][1];
      result[i][2] = std::max(result[i - 1][0], result[i - 1][1]) + cost[i][2];
    }
  }
  std::cout << std::max(std::max(result[N - 1][0], result[N - 1][1]),
                        result[N - 1][2]);
  return 0;
}


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

int main() {
  int cost[100000][3], N;
  long long result[100000][3];

  std::cin >> N;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < 3; j++) {
      std::cin >> cost[i][j];
    }
  }
  for (int i = 0; i < 3; i++)
    result[0][i] = cost[0][i];

  for (int i = 1; i < N; i++) {
    for (int j = 0; j < 3; j++) {
      result[i][0] = std::max(result[i - 1][1], result[i - 1][2]) + cost[i][0];
      result[i][1] = std::max(result[i - 1][0], result[i - 1][2]) + cost[i][1];
      result[i][2] = std::max(result[i - 1][0], result[i - 1][1]) + cost[i][2];
    }
  }
  std::cout << std::max(std::max(result[N - 1][0], result[N - 1][1]),
                        result[N - 1][2]);
  return 0;
}
Problem p03162 (C++)

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

typedef long long ll;
using namespace std;

int main() {

  int n, k;
  cin >> n >> k;
  vector<vector<int>> happ(n, vector<int>(k));
  int INF = 1 << 30;

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

  // dp[i][j] es la mayor felicidad del dia i con la actividad anterior j
  vector<vector<int>> dp(n, vector<int>(k, 0));

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

  for (int day = 1; day < n; ++day) {
    for (int i = 0; i < k; ++i) {
      for (int j = 0; j < k; ++j) {
        if (i != j) {
          dp[day][j] = max(dp[day][j], dp[day - 1][i] + happ[day][j]);
          // cout << "day " << day << " i " << i << " j " << j << " dp "  <<
          // dp[day][j] << endl;
        }
      }
    }
  }

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

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

  return 0;
}


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

typedef long long ll;
using namespace std;

int main() {

  int n, k;
  cin >> n; // >> k;
  k = 3;
  vector<vector<int>> happ(n, vector<int>(k));
  int INF = 1 << 30;

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

  // dp[i][j] es la mayor felicidad del dia i con la actividad anterior j
  vector<vector<int>> dp(n, vector<int>(k, 0));

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

  for (int day = 1; day < n; ++day) {
    for (int i = 0; i < k; ++i) {
      for (int j = 0; j < k; ++j) {
        if (i != j) {
          dp[day][j] = max(dp[day][j], dp[day - 1][i] + happ[day][j]);
          // cout << "day " << day << " i " << i << " j " << j << " dp "  <<
          // dp[day][j] << endl;
        }
      }
    }
  }

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

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

  return 0;
}
Problem p03162 (C++)

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

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

int h[10010][3], dp[100100][3];

int main() {
  int n;
  cin >> n;
  rep(i, n) rep(j, 3) cin >> h[i][j];
  rep(i, n) rep(j, 3) rep(k, 3) {
    if (j == k)
      continue;
    chmax(dp[i + 1][k], dp[i][j] + h[i][k]);
  }
  // rep(i, n + 1)
  // {
  //     rep(j, 3) cout << dp[i][j] << " ";
  //     cout << endl;
  // }
  int ans = 0;
  rep(i, 3) ans = max(ans, dp[n][i]);
  cout << ans << endl;
  return 0;
}

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

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

int h[100100][3], dp[100100][3];

int main() {
  int n;
  cin >> n;
  rep(i, n) rep(j, 3) cin >> h[i][j];
  rep(i, n) rep(j, 3) rep(k, 3) {
    if (j == k)
      continue;
    chmax(dp[i + 1][k], dp[i][j] + h[i][k]);
  }
  // rep(i, n + 1)
  // {
  //     rep(j, 3) cout << dp[i][j] << " ";
  //     cout << endl;
  // }
  int ans = 0;
  rep(i, 3) 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;
#define ll long long
#define pll pair<ll, ll>
#define ff first
#define ss second
#define se set<ll>
#define inf 10000000000000

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifdef APNA_IO
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ll n;
  cin >> n;
  ll h[n][3];

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

  pll d[3];
  d[0].ff = 1;
  d[0].ss = 2;
  d[1].ff = 0;
  d[1].ss = 2;
  d[2].ss = 1;
  d[2].ff = 0;

  for (int i = 1; i < n; i++) {
    for (int j = 0; j < n; j++) {
      h[i][j] += max(h[i - 1][d[j].ss], h[i - 1][d[j].ff]);
    }
  }

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<ll, ll>
#define ff first
#define ss second
#define se set<ll>
#define inf 10000000000000

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifdef APNA_IO
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ll n;
  cin >> n;
  ll h[n][3];

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

  pll d[3];
  d[0].ff = 1;
  d[0].ss = 2;
  d[1].ff = 0;
  d[1].ss = 2;
  d[2].ss = 1;
  d[2].ff = 0;

  for (int i = 1; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      h[i][j] += max(h[i - 1][d[j].ss], h[i - 1][d[j].ff]);
    }
  }

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

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

using namespace std;
int memo[100100][5], n, a[100050], b[100050], c[100050];
// 0 swim
// 1 Catch bugs
// 2 homework
int dp(int i, int last) {
  if (i > n)
    return 0;
  int Res1 = -2e9, Res2 = -2e9, Res3 = -2e9;
  if (last == 0) {
    Res1 = dp(i + 1, 1) + b[i];
    Res2 = dp(i + 1, 2) + c[i];
    Res3 = max(Res1, Res2);
  } else if (last == 1) {
    Res1 = dp(i + 1, 0) + a[i];
    Res2 = dp(i + 1, 2) + c[i];
    Res3 = max(Res1, Res2);
  } else {

    Res1 = dp(i + 1, 0) + a[i];
    Res2 = dp(i + 1, 1) + b[i];
    Res3 = max(Res1, Res2);
  }

  return memo[i][last] = Res3;
}

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

  memset(memo, -1, sizeof(memo));
  cout << max(max(dp(1, 1), dp(1, 2)), dp(1, 0));

  return 0;
}


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

using namespace std;
int memo[100100][5], n, a[100050], b[100050], c[100050];
// 0 swim
// 1 Catch bugs
// 2 homework
int dp(int i, int last) {
  if (i > n)
    return 0;
  int Res1 = -2e9, Res2 = -2e9, Res3 = -2e9;

  if (memo[i][last] + 1)
    return memo[i][last];

  if (last == 0) {
    Res1 = dp(i + 1, 1) + b[i];
    Res2 = dp(i + 1, 2) + c[i];
    Res3 = max(Res1, Res2);
  } else if (last == 1) {
    Res1 = dp(i + 1, 0) + a[i];
    Res2 = dp(i + 1, 2) + c[i];
    Res3 = max(Res1, Res2);
  } else {

    Res1 = dp(i + 1, 0) + a[i];
    Res2 = dp(i + 1, 1) + b[i];
    Res3 = max(Res1, Res2);
  }

  return memo[i][last] = Res3;
}

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

  memset(memo, -1, sizeof(memo));
  cout << max(max(dp(1, 1), dp(1, 2)), dp(1, 0));

  return 0;
}
Problem p03162 (C++)

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

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

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

  int n;
  cin >> n;
  int a[n], b[n], c[n], dp1[n], dp2[n], dp3[n];

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

  dp1[0] = a[0];
  dp2[0] = b[0];
  dp3[0] = c[0];

  for (int i = 1; i < n; i++) {
    dp1[i] = max(dp2[i - 1] + a[i], dp3[i - 1] + a[i]);
    dp2[i] = max(dp1[i - 1] + b[i], dp3[i - 1] + b[i]);
    dp3[i] = max(dp1[i - 1] + c[i], dp2[i - 1] + c[i]);
  }
  int ans = max(max(dp1[n - 1], dp2[n - 1]), dp3[n - 1]);
  cout << ans << endl;
}


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

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

  int n;
  cin >> n;
  int a[n], b[n], c[n], dp1[n], dp2[n], dp3[n];

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

  dp1[0] = a[0];
  dp2[0] = b[0];
  dp3[0] = c[0];

  for (int i = 1; i < n; i++) {
    dp1[i] = max(dp2[i - 1] + a[i], dp3[i - 1] + a[i]);
    dp2[i] = max(dp1[i - 1] + b[i], dp3[i - 1] + b[i]);
    dp3[i] = max(dp1[i - 1] + c[i], dp2[i - 1] + c[i]);
  }
  int ans = max(max(dp1[n - 1], dp2[n - 1]), dp3[n - 1]);
  cout << ans << endl;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
//        THIS IS MY NINJA WAY
#include <algorithm>
#include <bits/stdc++.h>
#include <ctime>
#include <vector>

#define str string
#define dbl double
#define ll long long
#define vl vector<ll>
#define vs vector<str>
#define pll pair<ll, ll>
#define vll vector<pll>
#define sl set<ll>
#define pb push_back
#define mp make_pair
#define ub upper_bound
#define lb lower_bound
#define ff first
#define ss second
#define fast                                                                   \
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);                   \
  srand(time(NULL));
#define fr(i, a, b) for (long long i = a; i < b; i++)
#define nfr(i, a, b) for (long long i = a; i <= b; i++)
#define psl pair<str, ll>
#define pls pair<ll, str>
#define pss pair<str, str>
#define ALL(a) a.begin(), a.end()
#define stl stack<ll>
#define coml complex<ll>
#define INF 0x3f3f3f3f3f3f3f3f
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
const ll MOD = 1e9 + 7;

using namespace std;
int main() {
  fast
      /*#ifndef ONLINE_JUDGE
              freopen("input.txt", "r" , stdin);
              freopen("output.txt", "w" , stdout);
      #endif*/

      ll n;
  cin >> n;
  vector<vl> v(n, vl(3));
  fr(i, 0, n) { fr(j, 0, 3) cin >> v[i][j]; }
  vector<vl> dp(n, vl(3, 0));
  dp[0] = v[0];
  fr(i, 0, n) {
    fr(j, 0, 3) {
      fr(k, 0, 3) {
        if (j != k)
          dp[i][k] = max(dp[i][k], dp[i - 1][j] + v[i][k]);
      }
    }
  }
  cout << *max_element(ALL(dp[n - 1]));
  return 0;
}

Fixed submission:
//        THIS IS MY NINJA WAY
#include <algorithm>
#include <bits/stdc++.h>
#include <ctime>
#include <vector>

#define str string
#define dbl double
#define ll long long
#define vl vector<ll>
#define vs vector<str>
#define pll pair<ll, ll>
#define vll vector<pll>
#define sl set<ll>
#define pb push_back
#define mp make_pair
#define ub upper_bound
#define lb lower_bound
#define ff first
#define ss second
#define fast                                                                   \
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);                   \
  srand(time(NULL));
#define fr(i, a, b) for (long long i = a; i < b; i++)
#define nfr(i, a, b) for (long long i = a; i <= b; i++)
#define psl pair<str, ll>
#define pls pair<ll, str>
#define pss pair<str, str>
#define ALL(a) a.begin(), a.end()
#define stl stack<ll>
#define coml complex<ll>
#define INF 0x3f3f3f3f3f3f3f3f
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
const ll MOD = 1e9 + 7;

using namespace std;
int main() {
  fast
      /*#ifndef ONLINE_JUDGE
              freopen("input.txt", "r" , stdin);
              freopen("output.txt", "w" , stdout);
      #endif*/

      ll n;
  cin >> n;
  vector<vl> v(n, vl(3));
  fr(i, 0, n) { fr(j, 0, 3) cin >> v[i][j]; }
  vector<vl> dp(n, vl(3, 0));
  dp[0] = v[0];
  fr(i, 1, n) {
    fr(j, 0, 3) {
      fr(k, 0, 3) {
        if (j != k)
          dp[i][k] = max(dp[i][k], dp[i - 1][j] + v[i][k]);
      }
    }
  }
  cout << *max_element(ALL(dp[n - 1]));
  return 0;
}
Problem p03162 (C++)

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // #ifndef ONLINE_JUDGE
  //     freopen("input.txt", "r", stdin);
  //     freopen("output.txt", "w", stdout);
  // #endif
  int n;
  cin >> n;
  int a[n][3];
  for (int i = 0; i < n; i++) {
    cin >> a[i][0] >> a[i][1] >> a[i][2];
  }
  // for(auto x:a){
  // 	cout<< x[0] << " "<< x[1] << " "<< x[2] << "\n";
  // }
  int dp[n][3];
  for (int i = 0; i < n; ++i) {
    dp[i][0] = 0;
    dp[i][1] = 0;
    dp[i][2] = 0;
  }
  dp[0][0] = a[0][0];
  dp[0][1] = a[0][1];
  dp[0][2] = a[0][2];
  // for(auto x:dp){
  // 	cout<< x[0] << " "<< x[1] << " "<< x[2] << "\n";
  // }
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      if (j == 0) {
        dp[i][j] = max(dp[i - 1][1], dp[i - 1][2]) + a[i][0];
      } else if (j == 1) {
        dp[i][j] = max(dp[i - 1][0], dp[i - 1][2]) + a[i][1];
      } else {
        dp[i][j] = max(dp[i - 1][0], dp[i - 1][1]) + a[i][2];
      }
    }
  }
  // for(auto x:dp){
  // 	cout<< x[0] << " "<< x[1] << " "<< x[2] << "\n";
  // }
  int ans = max({dp[n - 1][0], dp[n - 1][1], dp[n - 1][2]});
  cout << ans;
  return ans;
}


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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // #ifndef ONLINE_JUDGE
  //     freopen("input.txt", "r", stdin);
  //     freopen("output.txt", "w", stdout);
  // #endif
  int n;
  cin >> n;
  int a[n][3];
  for (int i = 0; i < n; i++) {
    cin >> a[i][0] >> a[i][1] >> a[i][2];
  }
  // for(auto x:a){
  // 	cout<< x[0] << " "<< x[1] << " "<< x[2] << "\n";
  // }
  int dp[n][3];
  for (int i = 0; i < n; ++i) {
    dp[i][0] = 0;
    dp[i][1] = 0;
    dp[i][2] = 0;
  }
  dp[0][0] = a[0][0];
  dp[0][1] = a[0][1];
  dp[0][2] = a[0][2];
  // for(auto x:dp){
  // 	cout<< x[0] << " "<< x[1] << " "<< x[2] << "\n";
  // }
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      if (j == 0) {
        dp[i][j] = max(dp[i - 1][1], dp[i - 1][2]) + a[i][0];
      } else if (j == 1) {
        dp[i][j] = max(dp[i - 1][0], dp[i - 1][2]) + a[i][1];
      } else {
        dp[i][j] = max(dp[i - 1][0], dp[i - 1][1]) + a[i][2];
      }
    }
  }
  // for(auto x:dp){
  // 	cout<< x[0] << " "<< x[1] << " "<< x[2] << "\n";
  // }
  int ans = max({dp[n - 1][0], dp[n - 1][1], dp[n - 1][2]});
  cout << ans;
  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
// In the name of ALLAH the most merciful the most beneficial

#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <math.h>
#define sc(x) scanf("%d", &x)
#define ll long long
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define FORd(i, a, b) for (int i = a; i > b; i--)
#define ff first
#define ss second
#define sz(x) x.size()
#define pb push_back

using namespace std;

typedef pair<int, int> pii;
typedef vector<pii> vii;
const int con = 1e5 + 8;
static const int INF = 0x3f3f3f3f;
static const int MOD = 998244353;

int main() {

  int N;
  sc(N);
  int arr[9][3];
  FOR(i, 0, N)
  sc(arr[i][0]), sc(arr[i][1]), sc(arr[i][2]);

  int ind[3];
  ind[0] = ind[1] = ind[2] = -1;
  FOR(i, 1, N) {

    FOR(j, 0, 3) {

      if (j == 0) {

        arr[i][j] += max(arr[i - 1][j + 1], arr[i - 1][j + 2]);
      } else if (j == 1) {

        arr[i][j] += max({arr[i - 1][j - 1], arr[i - 1][j + 1]});
      } else {
        arr[i][j] += max(arr[i - 1][j - 1], arr[i - 1][j - 2]);
      }
    }
  }
  N--;
  cout << max({arr[N][0], arr[N][1], arr[N][2]});
}


Fixed submission:
// In the name of ALLAH the most merciful the most beneficial

#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <math.h>
#define sc(x) scanf("%d", &x)
#define ll long long
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define FORd(i, a, b) for (int i = a; i > b; i--)
#define ff first
#define ss second
#define sz(x) x.size()
#define pb push_back

using namespace std;

typedef pair<int, int> pii;
typedef vector<pii> vii;
const int con = 1e5 + 8;
static const int INF = 0x3f3f3f3f;
static const int MOD = 998244353;

int main() {

  int N;
  sc(N);
  int arr[con][3];
  FOR(i, 0, N)
  sc(arr[i][0]), sc(arr[i][1]), sc(arr[i][2]);

  int ind[3];
  ind[0] = ind[1] = ind[2] = -1;
  FOR(i, 1, N) {

    FOR(j, 0, 3) {

      if (j == 0) {

        arr[i][j] += max(arr[i - 1][j + 1], arr[i - 1][j + 2]);
      } else if (j == 1) {

        arr[i][j] += max({arr[i - 1][j - 1], arr[i - 1][j + 1]});
      } else {
        arr[i][j] += max(arr[i - 1][j - 1], arr[i - 1][j - 2]);
      }
    }
  }
  N--;
  cout << max({arr[N][0], arr[N][1], arr[N][2]});
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
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;
}
const ll INF = 1LL << 60;

int n;
vector<vector<ll>> dp(10005, vector<ll>(3, 0));

int main() {
  cin >> n;
  vector<vector<ll>> a(n, vector<ll>(3));
  rep(i, n) rep(j, 3) cin >> a[i][j];
  rep(i, n) {
    rep(j, 3) {
      rep(k, 3) {
        if (j == k)
          continue;
        chmax(dp[i + 1][k], dp[i][j] + a[i][k]);
      }
    }
  }
  ll res = 0;
  rep(i, 3) chmax(res, dp[n][i]);
  cout << res << "\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
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;
}
const ll INF = 1LL << 60;

int n;
// vector<vector<ll>> dp(10005,vector<ll>(3,0));
long long dp[100010][3];
int main() {
  cin >> n;
  vector<vector<ll>> a(n, vector<ll>(3));
  rep(i, n) rep(j, 3) cin >> a[i][j];
  rep(i, n) {
    rep(j, 3) {
      rep(k, 3) {
        if (j == k)
          continue;
        chmax(dp[i + 1][k], dp[i][j] + a[i][k]);
      }
    }
  }
  ll res = 0;
  rep(i, 3) chmax(res, dp[n][i]);
  cout << res << "\n";
  return 0;
}
Problem p03162 (C++)

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

using namespace std;
int arr[10005][3];
int ans[10005][3];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> arr[i][j];
    }
  }
  for (int i = 0; i < 3; i++) {
    ans[0][i] = arr[0][i];
  }
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      int Max = 0;
      for (int k = 0; k < 3; k++) {
        if (k != j)
          Max = max(Max, arr[i][j] + ans[i - 1][k]);
      }
      ans[i][j] = Max;
    }
  }
  int Max = 0;
  Max = max(ans[n - 1][1], max(ans[n - 1][0], ans[n - 1][2]));
  cout << Max;
  return 0;
}


Fixed submission:
#include <iostream>

using namespace std;
int arr[1000005][3];
int ans[1000005][3];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> arr[i][j];
    }
  }
  for (int i = 0; i < 3; i++) {
    ans[0][i] = arr[0][i];
  }
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      int Max = 0;
      for (int k = 0; k < 3; k++) {
        if (k != j)
          Max = max(Max, arr[i][j] + ans[i - 1][k]);
      }
      ans[i][j] = Max;
    }
  }
  int Max = 0;
  Max = max(ans[n - 1][1], max(ans[n - 1][0], ans[n - 1][2]));
  cout << Max;
  return 0;
}
Problem p03162 (C++)

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

using namespace std;
#define ll long long
#define mod 1000000007

vector<vector<int>> v;
int dp[100001][3];

int f(int i, int j) {
  if (i == v.size())
    return 0;

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

  int ans = INT_MIN;
  for (int k = 0; k < 3; k++) {
    if (k != j) {
      ans = max(ans, v[i][k] + f(i + 1, k));
    }
  }
  return ans;
}

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

  memset(dp, -1, sizeof(dp));
  int n;
  cin >> n;

  v.resize(n);
  for (auto &i : v) {
    i.resize(3);
  }

  for (int i = 0; i < n; i++) {
    cin >> v[i][0] >> v[i][1] >> v[i][2];
  }
  cout << max(f(0, 0), max(f(0, 1), f(0, 2))) << "\n";
}


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

using namespace std;
#define ll long long
#define mod 1000000007

vector<vector<int>> v;
int dp[100001][3];

int f(int i, int j) {
  if (i == v.size())
    return 0;

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

  int ans = INT_MIN;
  for (int k = 0; k < 3; k++) {
    if (k != j) {
      ans = max(ans, v[i][k] + f(i + 1, k));
    }
  }
  dp[i][j] = ans;
  return ans;
}

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

  memset(dp, -1, sizeof(dp));
  int n;
  cin >> n;

  v.resize(n);
  for (auto &i : v) {
    i.resize(3);
  }

  for (int i = 0; i < n; i++) {
    cin >> v[i][0] >> v[i][1] >> v[i][2];
  }
  cout << max(f(0, 0), max(f(0, 1), f(0, 2))) << "\n";
}
Problem p03162 (C++)

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

#define ll long long int
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);

#define mp make_pair
#define pb push_back
#define ld long double
#define f first
#define s second
const ll N = (1e6);
const ll mod = (1e9 + 7);

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

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

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

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


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

#define ll long long int
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);

#define mp make_pair
#define pb push_back
#define ld long double
#define f first
#define s second
const ll N = (1e6);
const ll mod = (1e9 + 7);

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

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

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

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
using namespace std;
#define int long long
#define double long double
#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define mod 1000000007
using namespace __gnu_pbds;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    new_data_set;
bool isprime(int n) {
  if (n == 1)
    return true;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}
int lcm(int a, int b) { return (a * b) / __gcd(a, b); }
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
unsigned int factorial(unsigned int n) {
  if (n == 0)
    return 1;
  return n * factorial(n - 1);
}
const int inf = 1e17 + 7;
// const int N=2e5+5;
bool visited[100005] = {false};
signed main() {
  FAST int n;
  cin >> n;
  vector<int> ans(3, 0);
  for (int i = 0; i < n; i++) {
    vector<int> dp(3, 0);
    vector<int> b(3);
    for (int i = 0; i < 3; i++)
      cin >> b[i];
    for (int j = 0; j < 3; j++) {
      for (int k = 0; k < 3; k++) {
        if (j != k) {
          dp[k] = max(dp[k], ans[i] + b[j]);
        }
      }
    }
    ans = dp;
  }
  cout << max({ans[0], ans[1], ans[2]});
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
using namespace std;
#define int long long
#define double long double
#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define mod 1000000007
using namespace __gnu_pbds;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    new_data_set;
bool isprime(int n) {
  if (n == 1)
    return true;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}
int lcm(int a, int b) { return (a * b) / __gcd(a, b); }
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
unsigned int factorial(unsigned int n) {
  if (n == 0)
    return 1;
  return n * factorial(n - 1);
}
const int inf = 1e17 + 7;
// const int N=2e5+5;
bool visited[100005] = {false};
signed main() {
  FAST int n;
  cin >> n;
  vector<int> ans(3, 0);
  for (int i = 0; i < n; i++) {
    vector<int> dp(3, 0);
    vector<int> b(3);
    for (int i = 0; i < 3; i++)
      cin >> b[i];
    for (int j = 0; j < 3; j++) {
      for (int k = 0; k < 3; k++) {
        if (j != k) {
          dp[k] = max(dp[k], ans[j] + b[k]);
        }
      }
    }
    ans = dp;
  }
  cout << max({ans[0], ans[1], ans[2]});
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
template <class T> bool chmax(T &a, const T &b) {
  if (b > a) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 2LL << 60;
using ll = long long;
using P = pair<string, ll>;

int main(int argc, char const *argv[]) {
  ll n, h[3][10010], dp[3][10010];
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> h[j][i];
      dp[j][i] = 0;
    }
  }
  dp[0][0] = h[0][0];
  dp[1][0] = h[1][0];
  dp[2][0] = h[2][0];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      for (int k = 0; k < 3; k++) {
        if (j == k)
          continue;
        chmax(dp[j][i], dp[k][i - 1] + h[j][i]);
      }
    }
  }
  cout << max(max(dp[0][n - 1], dp[1][n - 1]), dp[2][n - 1]) << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
template <class T> bool chmax(T &a, const T &b) {
  if (b > a) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 2LL << 60;
using ll = long long;
using P = pair<string, ll>;

int main(int argc, char const *argv[]) {
  ll n, h[3][100100], dp[3][100100];
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> h[j][i];
      dp[j][i] = 0;
    }
  }
  dp[0][0] = h[0][0];
  dp[1][0] = h[1][0];
  dp[2][0] = h[2][0];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      for (int k = 0; k < 3; k++) {
        if (j == k)
          continue;
        chmax(dp[j][i], dp[k][i - 1] + h[j][i]);
      }
    }
  }
  cout << max(max(dp[0][n - 1], dp[1][n - 1]), dp[2][n - 1]) << endl;

  return 0;
}
Problem p03162 (C++)

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

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

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

  long long res = 0;
  for (int i = 0; i < n; i++)
    res = max(res, dp[n][i]);

  cout << res << endl;
  return 0;
}

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

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

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

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

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> b(n);
  vector<int> c(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    cin >> b[i];
    cin >> c[i];
  }
  int dp[n + 1]
        [3]; // i-1日目にjを選んだ時のdp[i][j]はi日目までの幸福度の総和の最大値
  dp[0][0] = 0;
  dp[0][1] = 0;
  dp[0][2] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      dp[i + 1][0] = max(dp[i][1] + b[i], dp[i][2] + c[i]);
      dp[i + 1][1] = max(dp[i][2] + c[i], dp[i][0] + a[i]);
      dp[i + 1][2] = max(dp[i][0] + a[i], dp[i][1] + b[i]);
      //      cout << dp[i+1][0] << " " << dp[i+1][1] << " " << dp[i+1][2] <<
      //      endl;
    }
  }
  cout << max(max(dp[n][0], dp[n][1]), dp[n][2]) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> b(n);
  vector<int> c(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    cin >> b[i];
    cin >> c[i];
  }
  int dp[n + 1]
        [3]; // i-1日目にjを選んだ時のdp[i][j]はi日目までの幸福度の総和の最大値
  dp[0][0] = 0;
  dp[0][1] = 0;
  dp[0][2] = 0;
  for (int i = 0; i < n; i++) {
    dp[i + 1][0] = max(dp[i][1] + b[i], dp[i][2] + c[i]);
    dp[i + 1][1] = max(dp[i][2] + c[i], dp[i][0] + a[i]);
    dp[i + 1][2] = max(dp[i][0] + a[i], dp[i][1] + b[i]);
    //      cout << dp[i+1][0] << " " << dp[i+1][1] << " " << dp[i+1][2] <<
    //      endl;
  }
  cout << max(max(dp[n][0], dp[n][1]), dp[n][2]) << endl;
  return 0;
}
Problem p03162 (C++)

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

using namespace std;

#define int long long

const int N = 1e5 + 100;
const int INF = 1e15;
int dp[N][4], h[N][4], n, k;

int sol(int idx, int p) {
  if (idx == n + 1)
    return 0;
  int &ans = dp[idx][p];
  if (ans != -1)
    return ans;
  for (int i = 1; i <= 3; i++) {
    if (i != p) {
      ans = max(ans, sol(idx + 1, i) + h[idx][i]);
    }
  }
  return ans;
}

void solve() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> h[i][1] >> h[i][2] >> h[i][3];
  memset(dp, -1, sizeof(dp));
  cout << sol(1, 0) << "\n";
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}

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

using namespace std;

#define int long long

const int N = 1e5 + 100;
const int INF = 1e15;
int dp[N][4], h[N][4], n, k;

int sol(int idx, int p) {
  if (idx == n + 1)
    return 0;
  int &ans = dp[idx][p];
  if (ans != -1)
    return ans;
  for (int i = 1; i <= 3; i++) {
    if (i != p) {
      ans = max(ans, sol(idx + 1, i) + h[idx][i]);
    }
  }
  return ans;
}

void solve() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> h[i][1] >> h[i][2] >> h[i][3];
  memset(dp, -1, sizeof(dp));
  cout << sol(1, 0) << "\n";
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}
Problem p03162 (C++)

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

vector<long long> a, b, c;
vector<vector<long long>> happy;

long long dp(int day, int prev) {
  if (day < 0) {
    return 0;
  }
  // if (happy[day][prev] != 0) {
  //     return happy[day][prev];
  // }
  if (prev == 0) {
    happy[day][prev] = max(dp(day - 1, 1) + b[day], dp(day - 1, 2) + c[day]);
  } else if (prev == 1) {
    happy[day][prev] = max(dp(day - 1, 0) + a[day], dp(day - 1, 2) + c[day]);
  } else if (prev == 2) {
    happy[day][prev] = max(dp(day - 1, 0) + a[day], dp(day - 1, 1) + b[day]);
  } else {
    return max({dp(day - 1, 0) + a[day], dp(day - 1, 1) + b[day],
                dp(day - 1, 2) + c[day]});
  }
  // cout << day << " day : " << happy[day][prev] << endl;
  return happy[day][prev];
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    a.push_back(x);
    b.push_back(y);
    c.push_back(z);
    vector<long long> m;
    m.push_back(0);
    m.push_back(0);
    m.push_back(0);
    happy.push_back(m);
  }
  cout << dp(n - 1, -1) << endl;
}


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

vector<long long> a, b, c;
vector<vector<long long>> happy;

long long dp(int day, int prev) {
  if (day < 0) {
    return 0;
  }
  if (prev >= 0 && happy[day][prev] != 0) {
    return happy[day][prev];
  }
  if (prev == 0) {
    happy[day][prev] = max(dp(day - 1, 1) + b[day], dp(day - 1, 2) + c[day]);
  } else if (prev == 1) {
    happy[day][prev] = max(dp(day - 1, 0) + a[day], dp(day - 1, 2) + c[day]);
  } else if (prev == 2) {
    happy[day][prev] = max(dp(day - 1, 0) + a[day], dp(day - 1, 1) + b[day]);
  } else {
    return max({dp(day - 1, 0) + a[day], dp(day - 1, 1) + b[day],
                dp(day - 1, 2) + c[day]});
  }
  // cout << day << " day : " << happy[day][prev] << endl;
  return happy[day][prev];
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    a.push_back(x);
    b.push_back(y);
    c.push_back(z);
    vector<long long> m;
    m.push_back(0);
    m.push_back(0);
    m.push_back(0);
    happy.push_back(m);
  }
  cout << dp(n - 1, -1) << endl;
}
Problem p03162 (C++)

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

int cost[10010][3];
long long happy[100010][3];

long long dp(int day, int prev) {
  if (day < 0) {
    return 0;
  }
  if (prev >= 0 && happy[day][prev] != 0) {
    return happy[day][prev];
  }
  if (prev == 0) {
    happy[day][prev] =
        max(dp(day - 1, 1) + cost[day][1], dp(day - 1, 2) + cost[day][2]);
  } else if (prev == 1) {
    happy[day][prev] =
        max(dp(day - 1, 0) + cost[day][0], dp(day - 1, 2) + cost[day][2]);
  } else if (prev == 2) {
    happy[day][prev] =
        max(dp(day - 1, 0) + cost[day][0], dp(day - 1, 1) + cost[day][1]);
  } else {
    return max({dp(day - 1, 0) + cost[day][0], dp(day - 1, 1) + cost[day][1],
                dp(day - 1, 2) + cost[day][2]});
  }
  // cout << day << " day : " << happy[day][prev] << endl;
  return happy[day][prev];
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    cost[i][0] = x;
    cost[i][1] = y;
    cost[i][2] = z;
  }
  cout << dp(n - 1, -1) << endl;
}


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

int cost[100010][3];
long long happy[100010][3];

long long dp(int day, int prev) {
  if (day < 0) {
    return 0;
  }
  if (prev >= 0 && happy[day][prev] != 0) {
    return happy[day][prev];
  }
  if (prev == 0) {
    happy[day][prev] =
        max(dp(day - 1, 1) + cost[day][1], dp(day - 1, 2) + cost[day][2]);
  } else if (prev == 1) {
    happy[day][prev] =
        max(dp(day - 1, 0) + cost[day][0], dp(day - 1, 2) + cost[day][2]);
  } else if (prev == 2) {
    happy[day][prev] =
        max(dp(day - 1, 0) + cost[day][0], dp(day - 1, 1) + cost[day][1]);
  } else {
    return max({dp(day - 1, 0) + cost[day][0], dp(day - 1, 1) + cost[day][1],
                dp(day - 1, 2) + cost[day][2]});
  }
  // cout << day << " day : " << happy[day][prev] << endl;
  return happy[day][prev];
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    cost[i][0] = x;
    cost[i][1] = y;
    cost[i][2] = z;
  }
  cout << dp(n - 1, -1) << endl;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
long double PI = 3.141592653589793238463;
const int N = 1e5 + 5;
const int MOD = 1e9 + 7;

ll a[100005][5];
ll b[100005];
ll c[100005];
ll dp[100005][5];
int n, k;

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

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
long double PI = 3.141592653589793238463;
const int N = 1e5 + 5;
const int MOD = 1e9 + 7;

ll a[100005][5];
ll b[100005];
ll c[100005];
ll dp[100005][5];
int n, k;

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

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

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

const long long INF = (1 << 30);

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

  int a[100000][3];
  rep(i, n) {
    rep(j, 3) { cin >> a[i][j]; }
  }

  long long dp[100100][3] = {}; // 幸福度を0で初期化
  rep(i, 3) { dp[0][i] = a[0][i]; }

  rep(i, n) {
    rep(j, 3) {
      rep(k, 3) {
        if (j == k)
          continue;
        dp[i + 1][j] = max(dp[i][k] + a[i + 1][j], dp[i + 1][j]);
      }
    }
  }

  long long ans = 0;
  rep(i, n) { ans = max(ans, dp[n - 1][i]); }

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

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

const long long INF = (1 << 30);

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

  int a[100000][3];
  rep(i, n) {
    rep(j, 3) { cin >> a[i][j]; }
  }

  long long dp[100100][3] = {}; // 幸福度を0で初期化
  rep(i, 3) { dp[0][i] = a[0][i]; }

  rep(i, n) {
    rep(j, 3) {
      rep(k, 3) {
        if (j == k)
          continue;
        dp[i + 1][j] = max(dp[i][k] + a[i + 1][j], dp[i + 1][j]);
      }
    }
  }

  long long ans = 0;
  rep(i, 3) { ans = max(ans, dp[n - 1][i]); }

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

Buggy submission (status: Time Limit Exceeded):

#include <bits/stdc++.h>

#define ll long long int
#define f(i, n) for (ll i = 0; i < n; i++)
#define fn(i, n) for (ll i = 1; i < n; i++)
#define F first
#define pb push_back
#define S second
using namespace std;
ll dp[100005][4];
ll n;
ll mn = INT_MAX;
ll sol(ll a[][3], ll i, ll k) {

  if (i == n - 1) {
    if (k == 0)
      return dp[i][k] = max(a[i][1], a[i][2]);
    if (k == 1)
      return dp[i][k] = max(a[i][0], a[i][2]);
    if (k == 2)
      return dp[i][k] = max(a[i][1], a[i][0]);

    return dp[i][k] = max(a[i][0], max(a[i][1], a[i][2]));
  }

  dp[i][0] = a[i][0] + sol(a, i + 1, 0);
  dp[i][1] = a[i][1] + sol(a, i + 1, 1);
  dp[i][2] = a[i][2] + sol(a, i + 1, 2);

  if (k == 0) {

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

  if (k == 1) {

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

  if (k == 2) {

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

  return dp[i][k] =
             max(a[i][1] + sol(a, i + 1, 1),
                 max(a[i][0] + sol(a, i + 1, 0), a[i][2] + sol(a, i + 1, 2)));
}

int main() {

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

  // ll n;
  cin >> n;
  ll a[n][3];
  bool l[3];
  f(i, 3) l[i] = 0;
  f(i, n) { cin >> a[i][0] >> a[i][1] >> a[i][2]; }
  // f(i,10) dp[i] = 100;
  memset(dp, -1, sizeof(dp));
  sol(a, 0, 3);
  cout << dp[0][3] << endl;

  return 0;
}


Fixed submission:

#include <bits/stdc++.h>

#define ll long long int
#define f(i, n) for (ll i = 0; i < n; i++)
#define fn(i, n) for (ll i = 1; i < n; i++)
#define F first
#define pb push_back
#define S second
using namespace std;
ll dp[100005][4];
ll n;
ll mn = INT_MAX;
ll sol(ll a[][3], ll i, ll k) {

  if (i == n - 1) {
    if (k == 0)
      return dp[i][k] = max(a[i][1], a[i][2]);
    if (k == 1)
      return dp[i][k] = max(a[i][0], a[i][2]);
    if (k == 2)
      return dp[i][k] = max(a[i][1], a[i][0]);

    return dp[i][k] = max(a[i][0], max(a[i][1], a[i][2]));
  }
  if (dp[i][0] == -1 || dp[i][1] == -1 || dp[i][2] == -1) {
    dp[i][0] = a[i][0] + sol(a, i + 1, 0);
    dp[i][1] = a[i][1] + sol(a, i + 1, 1);
    dp[i][2] = a[i][2] + sol(a, i + 1, 2);
  }

  if (k == 0) {

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

  if (k == 1) {

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

  if (k == 2) {

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

  return dp[i][k] =
             max(a[i][1] + sol(a, i + 1, 1),
                 max(a[i][0] + sol(a, i + 1, 0), a[i][2] + sol(a, i + 1, 2)));
}

int main() {

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

  // ll n;
  cin >> n;
  ll a[n][3];
  bool l[3];
  f(i, 3) l[i] = 0;
  f(i, n) { cin >> a[i][0] >> a[i][1] >> a[i][2]; }
  // f(i,10) dp[i] = 100;
  memset(dp, -1, sizeof(dp));
  sol(a, 0, 3);
  cout << dp[0][3] << endl;

  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll const mod = 1000000007;
ll const md = 998244353;
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ifstream in("txt.in");
ofstream out("txt.out");

#define mp make_pair
#define pb push_back
#define fi first
#define se second

int f[100005][5];
int dp[5][100005];

int main() {
  cout << fixed << setprecision(12);
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout.tie(0);

  ll n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= 3; ++j) {
      cin >> f[i][j];
    }
  }
  dp[1][1] = f[1][1];
  dp[1][2] = f[1][2];
  dp[1][3] = f[1][3];
  for (int i = 2; i <= n; ++i) {
    for (int j = 1; j <= 3; ++j) {
      if (j == 1)
        dp[i][j] = max(dp[i - 1][2], dp[i - 1][3]) + f[i][j];
      if (j == 2)
        dp[i][j] = max(dp[i - 1][1], dp[i - 1][3]) + f[i][j];
      if (j == 3)
        dp[i][j] = max(dp[i - 1][2], dp[i - 1][1]) + f[i][j];
    }
  }
  cout << max({dp[n][1], dp[n][2], dp[n][3]});

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll const mod = 1000000007;
ll const md = 998244353;
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ifstream in("txt.in");
ofstream out("txt.out");

#define mp make_pair
#define pb push_back
#define fi first
#define se second

int f[100005][5];
int dp[100005][5];

int main() {
  cout << fixed << setprecision(12);
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout.tie(0);

  ll n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= 3; ++j) {
      cin >> f[i][j];
    }
  }
  dp[1][1] = f[1][1];
  dp[1][2] = f[1][2];
  dp[1][3] = f[1][3];
  for (int i = 2; i <= n; ++i) {
    for (int j = 1; j <= 3; ++j) {
      if (j == 1)
        dp[i][j] = max(dp[i - 1][2], dp[i - 1][3]) + f[i][j];
      if (j == 2)
        dp[i][j] = max(dp[i - 1][1], dp[i - 1][3]) + f[i][j];
      if (j == 3)
        dp[i][j] = max(dp[i - 1][2], dp[i - 1][1]) + f[i][j];
    }
  }
  cout << max({dp[n][1], dp[n][2], dp[n][3]});

  return 0;
}