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

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

int main() {
  int n;
  cin >> n;
  vector<vector<int>> abc(n, vector<int>(3));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      cin >> abc[i][j];
  vector<vector<int>> dp(n + 1, vector<int>(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][j] = max(dp[i + 1][j], dp[i][k] + abc[i][j]);
      }
    }
  }
  cout << *max_element(dp[n].begin(), dp[n].end()) << endl;
  return 0;
}

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

int main() {
  int n;
  cin >> n;
  vector<vector<int>> abc(n, vector<int>(3));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < 3; j++)
      cin >> abc[i][j];
  vector<vector<int>> dp(n + 1, vector<int>(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][j] = max(dp[i + 1][j], dp[i][k] + abc[i][j]);
      }
    }
  }
  cout << *max_element(dp[n].begin(), dp[n].end()) << endl;
  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
/*
 ╔═══╦═══╦═══╗
 ║╔═╗║╔══╩╗╔╗║
 ║║░║║╚══╗║║║║
 ║║░║║╔══╝║║║║
 ║╚═╝║╚══╦╝╚╝║
 ╚══╗╠═══╩═══╝
 ░░░╚╝░░░░░░░░
 */
/*
 *
 *
 *
 */
#include <bits/stdc++.h>
#define ll long long
#define MAX 10000
using namespace std;

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

  return 0;
}


Fixed submission:
/*
 ╔═══╦═══╦═══╗
 ║╔═╗║╔══╩╗╔╗║
 ║║░║║╚══╗║║║║
 ║║░║║╔══╝║║║║
 ║╚═╝║╚══╦╝╚╝║
 ╚══╗╠═══╩═══╝
 ░░░╚╝░░░░░░░░
 */
/*
 *
 *
 *
 */
#include <bits/stdc++.h>
#define ll long long
#define MAX 100000
using namespace std;

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

  return 0;
}
Problem p03162 (C++)

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

using namespace std;

int solve(vector<array<int, 3>> &v_abc, vector<vector<int>> memo, int pre = -1,
          int cur = 0) {
  int n = v_abc.size();
  if (cur == n) {
    return 0;
  }

  if (memo[cur][pre + 1] == -1) {
    int ans = 0;
    for (int i = 0; i < 3; ++i) {
      if (i != pre) {
        ans = max(ans, solve(v_abc, memo, i, cur + 1) + v_abc[cur][i]);
      }
    }

    memo[cur][pre + 1] = ans;
  }

  return memo[cur][pre + 1];
}

int main(int argc, const char *argv[]) {
  int n;
  cin >> n;

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

  vector<vector<int>> memo(n, vector<int>(4, -1));

  cout << solve(v_abc, memo) << '\n';
  return 0;
}

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

using namespace std;

int solve(vector<array<int, 3>> &v_abc, vector<vector<int>> &memo, int pre = -1,
          int cur = 0) {
  int n = v_abc.size();
  if (cur == n) {
    return 0;
  }

  if (memo[cur][pre + 1] == -1) {
    int ans = 0;
    for (int i = 0; i < 3; ++i) {
      if (i != pre) {
        ans = max(ans, solve(v_abc, memo, i, cur + 1) + v_abc[cur][i]);
      }
    }

    memo[cur][pre + 1] = ans;
  }

  return memo[cur][pre + 1];
}

int main(int argc, const char *argv[]) {
  int n;
  cin >> n;

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

  vector<vector<int>> memo(n, vector<int>(4, -1));

  cout << solve(v_abc, memo) << '\n';
  return 0;
}
Problem p03162 (C++)

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

using namespace std;

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

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

using namespace std;

int main() {
  int n, i;
  scanf("%d", &n);
  vector<vector<int>> a(n, vector<int>(3, 0));
  vector<vector<int>> dp(n, vector<int>(3, 0));
  for (i = 0; i < n; i++) {
    scanf("%d %d %d", &a[i][0], &a[i][1], &a[i][2]);
  }
  dp[n - 1][0] = a[n - 1][0];
  dp[n - 1][1] = a[n - 1][1];
  dp[n - 1][2] = a[n - 1][2];
  for (i = n - 2; i >= 0; i--) {
    dp[i][0] = a[i][0] + max(dp[i + 1][1], dp[i + 1][2]);
    dp[i][1] = a[i][1] + max(dp[i + 1][0], dp[i + 1][2]);
    dp[i][2] = a[i][2] + max(dp[i + 1][0], dp[i + 1][1]);
  }
  printf("%d", max({dp[0][0], dp[0][1], dp[0][2]}));
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
/*
    Auther: ghoshashis545 Ashis Ghosh
    College: jalpaiguri Govt Enggineering College
    Date:12/06/2020
*/
#include <algorithm>
#include <bits/stdc++.h>
#include <string>
using namespace std;
#define ll long long
#define int ll
#define ld long double
#define ff first
#define ss string
#define se second
#define alt(v) v.begin(), v.end()
#define pb emplace_back
#define mp make_pair
#define fab(i, a, b) for (ll i = (a); i < (b); i++)
#define fba(i, a, b) for (ll i = (b); i >= (a); i--)
// ll mod=1000000007;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

const int N = 1e5 + 5;
int cache[N][3];
int a[N][3];
int n;
int dp(int idx, int f) {
  if (idx == n)
    return 0;
  int &ans = cache[idx][f];
  if (ans != -1)
    return ans;
  ans = 0;
  for (int i = 0; i < 3; i++) {
    if (i == f)
      continue;
    ans = max(ans, dp(idx + 1, i));
  }
  ans += a[idx][f];
  return ans;
}
bool test = 0;
void solve() {
  cin >> n;
  memset(cache, -1, sizeof(cache));
  for (int i = 0; i < n; i++)
    cin >> a[i][0] >> a[i][1] >> a[i][2];
  int ans = 0;
  for (int i = 0; i < 3; i++)
    ans = max(ans, dp(0, i));
  cout << ans;
}

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

Fixed submission:
/*
    Auther: ghoshashis545 Ashis Ghosh
    College: jalpaiguri Govt Enggineering College
    Date:12/06/2020
*/
#include <algorithm>
#include <bits/stdc++.h>
#include <string>
using namespace std;
#define ll long long
#define int ll
#define ld long double
#define ff first
#define ss string
#define se second
#define alt(v) v.begin(), v.end()
#define pb emplace_back
#define mp make_pair
#define fab(i, a, b) for (ll i = (a); i < (b); i++)
#define fba(i, a, b) for (ll i = (b); i >= (a); i--)
// ll mod=1000000007;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

const int N = 1e5 + 5;
int cache[N][3];
int a[N][3];
int n;
int dp(int idx, int f) {
  if (idx == n)
    return 0;
  int &ans = cache[idx][f];
  if (ans != -1)
    return ans;
  ans = 0;
  for (int i = 0; i < 3; i++) {
    if (i == f)
      continue;
    ans = max(ans, dp(idx + 1, i));
  }
  ans += a[idx][f];
  return ans;
}
bool test = 0;
void solve() {
  cin >> n;
  memset(cache, -1, sizeof(cache));
  for (int i = 0; i < n; i++)
    cin >> a[i][0] >> a[i][1] >> a[i][2];
  int ans = 0;
  for (int i = 0; i < 3; i++)
    ans = max(ans, dp(0, i));
  cout << ans;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin.exceptions(cin.failbit);
  //     #ifndef ONLINE_JUDGE
  // 	freopen("input.txt", "r", stdin);
  // 	freopen("output.txt", "w", stdout);
  // 	#endif
  int t = 1;
  if (test)
    cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
Problem p03162 (C++)

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

using namespace std;

int a[10020], b[10020], c[10020];
int da[10020], db[10020], dc[10020];

int resiA(int i, int n);
int resiB(int i, int n);
int resiC(int i, int n);

int resiA(int i, int n) {
  if (i == n - 1) {
    return a[i];
  }

  if (da[i] == 0) {

    da[i] = max(resiB(i + 1, n) + a[i], resiC(i + 1, n) + a[i]);
  }

  return da[i];
}

int resiB(int i, int n) {
  if (i == n - 1) {
    return b[i];
  }

  if (db[i] == 0) {

    db[i] = max(resiA(i + 1, n) + b[i], resiC(i + 1, n) + b[i]);
  }

  return db[i];
}

int resiC(int i, int n) {
  if (i == n - 1) {
    return c[i];
  }

  if (dc[i] == 0) {

    dc[i] = max(resiA(i + 1, n) + c[i], resiB(i + 1, n) + c[i]);
  }

  return dc[i];
}

int main() {
  int n;

  cin >> n;

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

  int rez = max(max(resiB(0, n), resiC(0, n)), resiA(0, n));

  cout << rez;

  return 0;
}

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

using namespace std;

int a[100200], b[100020], c[100020];
int da[100200], db[100020], dc[100020];

int resiA(int i, int n);
int resiB(int i, int n);
int resiC(int i, int n);

int resiA(int i, int n) {
  if (i == n - 1) {
    return a[i];
  }

  if (da[i] == 0) {

    da[i] = max(resiB(i + 1, n) + a[i], resiC(i + 1, n) + a[i]);
  }

  return da[i];
}

int resiB(int i, int n) {
  if (i == n - 1) {
    return b[i];
  }

  if (db[i] == 0) {

    db[i] = max(resiA(i + 1, n) + b[i], resiC(i + 1, n) + b[i]);
  }

  return db[i];
}

int resiC(int i, int n) {
  if (i == n - 1) {
    return c[i];
  }

  if (dc[i] == 0) {

    dc[i] = max(resiA(i + 1, n) + c[i], resiB(i + 1, n) + c[i]);
  }

  return dc[i];
}

int main() {
  int n;

  cin >> n;

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

  int rez = max(max(resiB(0, n), resiC(0, n)), resiA(0, n));

  cout << rez;

  return 0;
}
Problem p03162 (C++)

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

const int N = 1e5 + 5;

int a[5][N];
int dp[5][N];
int n;

int solve(int idx, int prev) {

  if (idx == n + 1) {
    return 0;
  }

  int &ans = dp[prev][idx];

  if (ans != -1) {
    return ans;
  }

  ans = 0;
  for (int i = 1; i <= 3; i++) {
    if (i == prev) {
      continue;
    }
    ans = max(ans, a[i][idx] + solve(idx + 1, i));
  }
  return ans;
}

int32_t main() {

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

  cin >> n;

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

  int ans = solve(1, 0);
  cout << ans << "\n";
  return 0;
}


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

const int N = 1e5 + 5;

int a[5][N];
int dp[5][N];
int n;

int solve(int idx, int prev) {

  if (idx == n + 1) {
    return 0;
  }

  int &ans = dp[prev][idx];

  if (ans != -1) {
    return ans;
  }

  ans = 0;
  for (int i = 1; i <= 3; i++) {
    if (i == prev) {
      continue;
    }
    ans = max(ans, a[i][idx] + solve(idx + 1, i));
  }
  return ans;
}

int32_t main() {

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

  cin >> n;

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

  int ans = solve(1, 0);
  cout << ans << "\n";
  return 0;
}
Problem p03162 (C++)

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

using namespace std;
#define int long long int

const int maxn = 1e5 + 5;
const int inf = 1e10;
vector<vector<int>> dp(maxn, vector<int>(3, inf));
vector<int> a(maxn), b(maxn), c(maxn);
int n;

int32_t main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }

  dp[0][0] = a[0];
  dp[0][1] = b[0];
  dp[0][2] = c[0];
  // cout<<dp[0][0]<<" "<<dp[0][1]<<" "<<dp[0][2]<<endl;
  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<<dp[i][0]<<" "<<dp[i][1]<<" "<<dp[i][2]<<endl;
  }

  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;
#define int long long int

const int maxn = 1e5 + 5;
const int inf = 1e10;
vector<vector<int>> dp(maxn, vector<int>(3, inf));
vector<int> a(maxn), b(maxn), c(maxn);
int n;

int32_t main() {
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  // #endif
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }

  dp[0][0] = a[0];
  dp[0][1] = b[0];
  dp[0][2] = c[0];
  // cout<<dp[0][0]<<" "<<dp[0][1]<<" "<<dp[0][2]<<endl;
  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<<dp[i][0]<<" "<<dp[i][1]<<" "<<dp[i][2]<<endl;
  }

  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 <algorithm>
#include <assert.h>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iosfwd>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define lli long long int
#define lower(a) transform(a.begin(), a.end(), a.begin(), ::tolower);

using namespace std;

const int N = 3e5 + 500;
const long long mod = 1e9 + 7;
const long long INF = 1LL << 57;
const int M = 1e6 + 500;

lli dp[N][3];

void solve() {
  lli n;
  cin >> n;
  lli a[n][3];
  for (int i = 1; i <= n; i++)
    cin >> a[i][0] >> a[i][1] >> a[i][2];
  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];
  }
  cout << max(dp[n][0], max(dp[n][1], dp[n][2])) << endl;
  return;
}
/*
10
6
10 3 2 5 7 8
*/
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  lli t;
  t = 1;
  //    cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iosfwd>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define lli long long int
#define lower(a) transform(a.begin(), a.end(), a.begin(), ::tolower);

using namespace std;

const int N = 3e5 + 500;
const long long mod = 1e9 + 7;
const long long INF = 1LL << 57;
const int M = 1e6 + 500;

lli dp[N][3];

void solve() {
  lli n;
  cin >> n;
  lli a[n + 1][3];
  for (int i = 1; i <= n; i++)
    cin >> a[i][0] >> a[i][1] >> a[i][2];
  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];
  }
  cout << max(dp[n][0], max(dp[n][1], dp[n][2])) << endl;
  return;
}
/*
10
6
10 3 2 5 7 8
*/
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  lli t;
  t = 1;
  //    cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}
Problem p03162 (C++)

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

using namespace std;

int main() {
  int n;
  int h[10005][3];
  int dp[10005][3];

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> h[i][0] >> h[i][1] >> h[i][2];
  }
  memset(dp, INT_MIN, sizeof(dp));
  dp[0][0] = h[0][0];
  dp[0][1] = h[0][1];
  dp[0][2] = h[0][2];

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


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

using namespace std;

int main() {
  int n;
  int h[100100][3];
  int dp[100100][3];

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> h[i][0] >> h[i][1] >> h[i][2];
  }
  memset(dp, INT_MIN, sizeof(dp));
  dp[0][0] = h[0][0];
  dp[0][1] = h[0][1];
  dp[0][2] = h[0][2];

  for (int day = 1; day < n; day++) {
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        if (i != j) {
          dp[day][j] = max(dp[day - 1][i] + h[day][j], dp[day][j]);
        }
      }
    }
  }
  cout << max({dp[n - 1][0], dp[n - 1][1], dp[n - 1][2]});
}
Problem p03162 (C++)

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

#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"
#define int long long
#define all(v) v.begin(), v.end()
#define sz(v) (int)v.size()
#define pii pair<int, int>
#define fi first
#define se second
#define forn(i, a, b) for (int i = a; i < b; i++)
#define deb(x) cout << #x << ' ' << x << endl
#define clock                                                                  \
  cerr << 1000 * (double)clock() / (double)CLOCKS_PER_SEC << "ms" << endl;

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

const int N = 1e5 + 5;
int a[N][3], n;
int cache[N][3];

int dp(int idx, int prev) {
  if (idx >= n)
    return 0;
  if (cache[idx][prev] != -1)
    return cache[idx][prev];
  int ans = 0;
  forn(i, 0, 3) {
    if (i != prev)
      ans = max(ans, a[idx][i] + dp(idx + 1, i));
  }
  cache[idx][prev] = ans;
  return ans;
}

int32_t main() {
  IOS INPUT();
  cin >> n;
  memset(cache, -1, sizeof(cache));
  forn(i, 0, n) { forn(j, 0, 3) cin >> a[i][j]; }
  int ans = 0;
  forn(i, 0, 3) { ans = max(ans, a[0][i] + dp(1, i)); }
  cout << ans << endl;
  return 0;
}

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

#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"
#define int long long
#define all(v) v.begin(), v.end()
#define sz(v) (int)v.size()
#define pii pair<int, int>
#define fi first
#define se second
#define forn(i, a, b) for (int i = a; i < b; i++)
#define deb(x) cout << #x << ' ' << x << endl
#define clock                                                                  \
  cerr << 1000 * (double)clock() / (double)CLOCKS_PER_SEC << "ms" << endl;

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

const int N = 1e5 + 5;
int a[N][3], n;
int cache[N][3];

int dp(int idx, int prev) {
  if (idx >= n)
    return 0;
  if (cache[idx][prev] != -1)
    return cache[idx][prev];
  int ans = 0;
  forn(i, 0, 3) {
    if (i != prev)
      ans = max(ans, a[idx][i] + dp(idx + 1, i));
  }
  cache[idx][prev] = ans;
  return ans;
}

int32_t main() {
  IOS // INPUT();
          cin >>
      n;
  memset(cache, -1, sizeof(cache));
  forn(i, 0, n) { forn(j, 0, 3) cin >> a[i][j]; }
  int ans = 0;
  forn(i, 0, 3) { ans = max(ans, a[0][i] + dp(1, 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 int

int n, a[100001], b[100001], c[100001];
ll dp[3][100001];

ll call(int i, int key) {
  if (dp[key][i])
    return dp[key][i];
  if (i == n - 1) {
    if (key == 0)
      return dp[key][i] = max(b[i], c[i]);
    else if (key == 1)
      return dp[key][i] = max(a[i], c[i]);
    else
      return dp[key][i] = max(b[i], a[i]);
  }
  if (key == 0)
    return dp[key][i] = max(b[i] + call(i + 1, 1), c[i] + call(i + 1, 2));
  else if (key == 1)
    return dp[key][i] = max(a[i] + call(i + 1, 0), c[i] + call(i + 1, 2));
  else
    return dp[key][i] = max(a[i] + call(i + 1, 0), b[i] + call(i + 1, 1));
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i] >> b[i] >> c[i];
  cout << max(a[0] + call(1, 0), max(b[0] + call(1, 1), c[0] + call(1, 2)))
       << endl;
  return 0;
}


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

#define ll long long int

int n, a[100001], b[100001], c[100001];
ll dp[3][100001];

ll call(int i, int key) {
  if (dp[key][i])
    return dp[key][i];
  if (i > n - 1)
    return 0;
  if (i == n - 1) {
    if (key == 0)
      return dp[key][i] = max(b[i], c[i]);
    else if (key == 1)
      return dp[key][i] = max(a[i], c[i]);
    else
      return dp[key][i] = max(b[i], a[i]);
  }
  if (key == 0)
    return dp[key][i] = max(b[i] + call(i + 1, 1), c[i] + call(i + 1, 2));
  else if (key == 1)
    return dp[key][i] = max(a[i] + call(i + 1, 0), c[i] + call(i + 1, 2));
  else
    return dp[key][i] = max(a[i] + call(i + 1, 0), b[i] + call(i + 1, 1));
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i] >> b[i] >> c[i];
  cout << max(a[0] + call(1, 0), max(b[0] + call(1, 1), c[0] + call(1, 2)))
       << endl;
  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):

#include <algorithm>
#include <iostream>

using namespace std;

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

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> a[i][j];
    }
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < 3; j++) {
      for (int k = 0; k < 3; k++) {
        if (j == k) {
          continue;
        }
        dp[i + 1][k] = max(dp[i + 1][k], dp[i][j] + a[i][k]);
      }
    }
  }
  long long Ans = 0;
  for (int i = 0; i < 3; i++) {
    Ans = max(Ans, dp[N][i]);
  }
  cout << Ans << endl;
}

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

long long dp[100100][3];
long long a[100100][3];

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> a[i][j];
    }
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < 3; j++) {
      for (int k = 0; k < 3; k++) {
        if (j == k) {
          continue;
        }
        dp[i + 1][k] = max(dp[i + 1][k], dp[i][j] + a[i][k]);
      }
    }
  }
  long long Ans = 0;
  for (int i = 0; i < 3; i++) {
    Ans = max(Ans, dp[N][i]);
  }
  cout << Ans << endl;
}
Problem p03162 (C++)

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

using namespace std;
const int MAXN = 1e5;
const int INF = 1e9 - 7;
typedef long long ll;
ll dp[MAXN][4];
int n;
vector<ll> a, b, c;

ll happy(ll N, ll K) {
  if (N == 0)
    return 0;
  if (dp[N][K] != -1)
    return dp[N][K];
  if (K == 3)
    return dp[N][K] = max(happy(N - 1, 2) + b[N], happy(N - 1, 1) + a[N]);
  if (K == 2)
    return dp[N][K] = max(happy(N - 1, 1) + a[N], happy(N - 1, 3) + c[N]);
  if (K == 1)
    return dp[N][K] = max(happy(N - 1, 2) + b[N], happy(N - 1, 3) + c[N]);
  if (K == 0)
    return dp[N][K] = max(happy(N - 1, 1) + a[N],
                          max(happy(N - 1, 2) + b[N], happy(N - 1, 3) + c[N]));
}

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

  a = vector<ll>(n + 1);
  b = vector<ll>(n + 1);
  c = vector<ll>(n + 1);

  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= 3; j++)
      dp[i][j] = -1;
  a[0] = INF;
  b[0] = INF;
  c[0] = INF;
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }
  cout << happy(n, 0) << "\n";

  return 0;
}


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

using namespace std;
const int MAXN = 1e5;
const int INF = 1e9 - 7;
typedef long long ll;
ll dp[MAXN + 2][4];
int n;
vector<ll> a, b, c;

ll happy(ll N, ll K) {
  if (N == 0)
    return 0;
  if (dp[N][K] != -1)
    return dp[N][K];
  if (K == 3)
    return dp[N][K] = max(happy(N - 1, 2) + b[N], happy(N - 1, 1) + a[N]);
  if (K == 2)
    return dp[N][K] = max(happy(N - 1, 1) + a[N], happy(N - 1, 3) + c[N]);
  if (K == 1)
    return dp[N][K] = max(happy(N - 1, 2) + b[N], happy(N - 1, 3) + c[N]);
  if (K == 0)
    return dp[N][K] = max(happy(N - 1, 1) + a[N],
                          max(happy(N - 1, 2) + b[N], happy(N - 1, 3) + c[N]));
}

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

  a = vector<ll>(n + 1);
  b = vector<ll>(n + 1);
  c = vector<ll>(n + 1);

  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= 3; j++)
      dp[i][j] = -1;
  a[0] = INF;
  b[0] = INF;
  c[0] = INF;
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }
  cout << happy(n, 0) << "\n";

  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Time Limit Exceeded):
// #include <bits/stdc++.h>
#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> VI;
typedef vector<VI> VVI; // 例: VVI dp(10, vector<int>(10, INF);
typedef priority_queue<int, vector<int>, less<int>> QUE_int;
const int INF =
    2 * pow(10, 9) + 1; //+1しないとREになるかも(out of rangeになるんかな?? )

vector<int> dx = {-1, 0, 1, 0, -1, -1, 1, 1};
vector<int> dy = {0, -1, 0, 1, -1, 1, -1, 1};

int main(void) {
  int N;
  cin >> N;

  vector<int> a(N), b(N), c(N);
  for (int i = 0; i < N; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }
  VVI act(N, vector<int>(3));
  for (int i = 0; i < N; i++) {
    act[i][0] = a[i];
    act[i][1] = b[i];
    act[i][2] = c[i];
  }

  VVI dp(N + 1, vector<int>(3 + 1, -1)); // dp[i][j]:i日目にjをした時の最大幸福値
  dp[0][0] = act[0][0];
  dp[0][1] = act[0][1];
  dp[0][2] = act[0][2];
  for (int i = 1; i < N; i++) {
    for (int j = 0; j < N; j++) {
      int act0 = j % 3;
      int act1 = (j + 1) % 3; //
      int act2 = (j + 2) % 3;
      dp[i][act0] = max(dp[i - 1][act1], dp[i - 1][act2]) + act[i][act0];
    }
  }

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


Fixed submission:
// #include <bits/stdc++.h>
#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> VI;
typedef vector<VI> VVI; // 例: VVI dp(10, vector<int>(10, INF);
typedef priority_queue<int, vector<int>, less<int>> QUE_int;
const int INF =
    2 * pow(10, 9) + 1; //+1しないとREになるかも(out of rangeになるんかな?? )

vector<int> dx = {-1, 0, 1, 0, -1, -1, 1, 1};
vector<int> dy = {0, -1, 0, 1, -1, 1, -1, 1};

int main(void) {
  int N;
  cin >> N;

  vector<int> a(N), b(N), c(N);
  for (int i = 0; i < N; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }
  VVI act(N, vector<int>(3));
  for (int i = 0; i < N; i++) {
    act[i][0] = a[i];
    act[i][1] = b[i];
    act[i][2] = c[i];
  }

  VVI dp(N + 1, vector<int>(3 + 1, -1)); // dp[i][j]:i日目にjをした時の最大幸福値
  dp[0][0] = act[0][0];
  dp[0][1] = act[0][1];
  dp[0][2] = act[0][2];
  for (int i = 1; i < N; i++) {
    for (int j = 0; j < 3; j++) {
      int act0 = j % 3;
      int act1 = (j + 1) % 3; //
      int act2 = (j + 2) % 3;
      dp[i][act0] = max(dp[i - 1][act1], dp[i - 1][act2]) + act[i][act0];
    }
  }

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

Buggy submission (status: Runtime Error):
//
//  main.cpp
//  C - Vacation
//
//  Created by Hashizo on 2019/06/12.
//

#include <algorithm>
#include <iostream>
#include <regex>
#include <string>
#include <vector>

using namespace std;

#undef INT_MAX
#undef INT_MIN

#define INT_MAX 2147483647
#define INT_MIN (-INT_MAX - 1)

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())
#define REPLACE(s, f, t) regex_replace(s, regex(f), t)

int main(int argc, const char *argv[]) {
  int n;
  cin >> n;

  int activity[4][3];
  int dp[4 + 1][3];

  REP(i, n) {
    REP(j, 3) { cin >> activity[i][j]; }
  }

  REP(j, 3) { dp[0][j] = 0; }

  REP(i, n) {
    dp[i + 1][0] = activity[i][0] + max(dp[i][1], dp[i][2]);
    dp[i + 1][1] = activity[i][1] + max(dp[i][0], dp[i][2]);
    dp[i + 1][2] = activity[i][2] + max(dp[i][0], dp[i][1]);
    //		dp[i+1][0] = dp[i][0] + max(activity[i][1], activity[i][2]);
    //		dp[i+1][1] = dp[i][1] + max(activity[i][0], activity[i][2]);
    //		dp[i+1][2] = dp[i][2] + max(activity[i][0], activity[i][1]);
  }

  int res = 0;
  REP(j, 3) { res = max(dp[n][j], res); }

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


Fixed submission:
//
//  main.cpp
//  C - Vacation
//
//  Created by Hashizo on 2019/06/12.
//

#include <algorithm>
#include <iostream>
#include <regex>
#include <string>
#include <vector>

using namespace std;

#undef INT_MAX
#undef INT_MIN

#define INT_MAX 2147483647
#define INT_MIN (-INT_MAX - 1)

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())
#define REPLACE(s, f, t) regex_replace(s, regex(f), t)

int main(int argc, const char *argv[]) {
  int n;
  cin >> n;

  int activity[n][3];
  int dp[n + 1][3];

  REP(i, n) {
    REP(j, 3) { cin >> activity[i][j]; }
  }

  REP(j, 3) { dp[0][j] = 0; }

  REP(i, n) {
    dp[i + 1][0] = activity[i][0] + max(dp[i][1], dp[i][2]);
    dp[i + 1][1] = activity[i][1] + max(dp[i][0], dp[i][2]);
    dp[i + 1][2] = activity[i][2] + max(dp[i][0], dp[i][1]);
    //		dp[i+1][0] = dp[i][0] + max(activity[i][1], activity[i][2]);
    //		dp[i+1][1] = dp[i][1] + max(activity[i][0], activity[i][2]);
    //		dp[i+1][2] = dp[i][2] + max(activity[i][0], activity[i][1]);
  }

  int res = 0;
  REP(j, 3) { res = max(dp[n][j], res); }

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

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<vector<int>> dp(n, vector<int>(3));
  for (int &i : dp.at(0)) {
    cin >> i;
  }
  for (int i = 1; i < n; ++i) {
    for (int &i : dp.at(i)) {
      cin >> i;
    }
    dp.at(i).at(0) += max(dp.at(i - 1).at(1), dp.at(i - 1).at(2));
    dp.at(i).at(1) += max(dp.at(i - 1).at(0), dp.at(i - 1).at(2));
    dp.at(i).at(2) += max(dp.at(i - 1).at(0), dp.at(i - 1).at(1));
  }
  cout << *max_element(dp.back().begin(), dp.back().end());
  return 0;
}

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<vector<int>> dp(n, vector<int>(3));
  for (int &i : dp.at(0)) {
    cin >> i;
  }
  for (int i = 1; i < n; ++i) {
    for (int &j : dp.at(i)) {
      cin >> j;
    }
    dp.at(i).at(0) += max(dp.at(i - 1).at(1), dp.at(i - 1).at(2));
    dp.at(i).at(1) += max(dp.at(i - 1).at(0), dp.at(i - 1).at(2));
    dp.at(i).at(2) += max(dp.at(i - 1).at(0), dp.at(i - 1).at(1));
  }
  cout << *max_element(dp.back().begin(), dp.back().end());
  return 0;
}
Problem p03162 (C++)

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

#define F first
#define S second

int dx[] = {0, 0, 1, -1, -1, -1, 1, 1};
int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};
const double EPS = 1e-8;
const double PI = acos(-1.0);
#define inf INT_MAX
ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }

struct hap {
  int a;
  int b;
  int c;
};
int a[100005][3]; // dont ;
int n;
int dp[100005][4];

int sol(int ind, int l) {
  if (ind >= n)
    return 0;

  int &res = dp[ind][l];
  for (int i = 0; i < 3; i++) {
    if (l != i) {
      res = max(res, sol(ind + 1, i) + a[ind][i]);
    }
  }

  return res;
}

int main() {

  cin >> n;

  for (int i = 0; i < n; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    a[i][0] = x;
    a[i][1] = y;
    a[i][2] = z;
  }
  memset(dp, -1, sizeof(dp));

  int ans = sol(0, 3);
  cout << ans << endl;

  return 0;
}


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

#define F first
#define S second

int dx[] = {0, 0, 1, -1, -1, -1, 1, 1};
int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};
const double EPS = 1e-8;
const double PI = acos(-1.0);
#define inf INT_MAX
ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }

struct hap {
  int a;
  int b;
  int c;
};
int a[100005][3]; // dont ;
int n;
int dp[100005][4];

int sol(int ind, int l) {
  if (ind >= n)
    return 0;

  int &res = dp[ind][l];
  if (res != -1)
    return res;
  for (int i = 0; i < 3; i++) {
    if (l != i) {
      res = max(res, sol(ind + 1, i) + a[ind][i]);
    }
  }

  return res;
}

int main() {

  cin >> n;

  for (int i = 0; i < n; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    a[i][0] = x;
    a[i][1] = y;
    a[i][2] = z;
  }
  memset(dp, -1, sizeof(dp));

  int ans = sol(0, 3);
  cout << ans << endl;

  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define ll long long
#define pb push_back
#define F first
#define S second
#define ld long double
#define vi vector<ll>
const int INF = 1e9;
const int N = 100005;

vi dp(3);
void solve() {
  ll n;
  cin >> n;
  for (ll day = 0; day < n; day++) {
    vi dp1(3);
    vi a(3);
    for (ll i = 0; i < 3; i++)
      cin >> a[i];
    for (ll i = 0; i < 3; i++) {
      for (ll j = 0; j < 3; j++) {
        if (i != j) {
          dp1[j] = max(dp1[j], dp[i] + a[j]);
        }
      }
    }
    dp = dp1;
  }

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

int main() {

  FASTIO;

#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif

  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define ll long long
#define pb push_back
#define F first
#define S second
#define ld long double
#define vi vector<ll>
const int INF = 1e9;
const int N = 100005;

vi dp(3);
void solve() {
  ll n;
  cin >> n;
  for (ll day = 0; day < n; day++) {
    vi dp1(3);
    vi a(3);
    for (ll i = 0; i < 3; i++)
      cin >> a[i];
    for (ll i = 0; i < 3; i++) {
      for (ll j = 0; j < 3; j++) {
        if (i != j) {
          dp1[j] = max(dp1[j], dp[i] + a[j]);
        }
      }
    }
    dp = dp1;
  }

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

int main() {

  FASTIO;
  /*
  #ifndef ONLINE_JUDGE
          // for getting input from input.txt
          freopen("input.txt", "r", stdin);
          // for writing output to output.txt
          freopen("output.txt", "w", stdout);
  #endif
  */
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
Problem p03162 (C++)

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

typedef vector<int> vi;
typedef pair<int, int> pi;
typedef long long ll;
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define REP(i, a, b) for (int i = a; i <= b; i++)

vector<vector<ll>> dp;

int main() {

  ll n, a, b, c;

  cin >> n;

  dp.assign(n + 5, vector<ll>(3, 0));

  for (ll i = 0; i < n; i++) {
    dp[0][i] = 0;
    dp[1][i] = 0;
    dp[2][i] = 0;
  }

  cin >> a >> b >> c;

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

  n--;

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

    cin >> a >> b >> c;

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

  ll maxi = 0;

  maxi = max(maxi, dp[0][n]);
  maxi = max(maxi, dp[1][n]);
  maxi = max(maxi, dp[2][n]);

  cout << maxi << endl;

  return 0;
}

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

typedef vector<int> vi;
typedef pair<int, int> pi;
typedef long long ll;
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define REP(i, a, b) for (int i = a; i <= b; i++)

vector<vector<ll>> dp;

int main() {

  ll n, a, b, c;

  cin >> n;

  dp.assign(3, vector<ll>(n + 1, 0));

  cin >> a >> b >> c;

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

  n--;

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

    cin >> a >> b >> c;

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

  ll maxi = 0;

  maxi = max(maxi, dp[0][n]);
  maxi = max(maxi, dp[1][n]);
  maxi = max(maxi, dp[2][n]);

  cout << maxi << endl;

  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
#define fastio()                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define loop(i, a, b) for (ll i = a; i < b; i++)
#define loope(i, a, b) for (ll i = a; i <= b; i++)
#define test()                                                                 \
  ull t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define puu pair<ull, ull>
#define f first
#define s second
#define pb push_back
#define mkp make_pair
#define ins insert
#define Vint vector<int>
#define Vll vector<ll>
#define Vull vector<ull>
#define nl cout << endl
#define sp cout << " "
#define MOD 1000000007
#define all(x) x.begin(), x.end()

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fastio();
  ll n;
  cin >> n;
  ll v[n][3];
  loop(i, 0, n) { loop(j, 0, 3) cin >> v[i][j]; }

  vector<vector<ll>> dp(n, vector<ll>(3, 0));
  dp[0][0] = v[0][0];
  dp[0][1] = v[0][1];
  dp[0][2] = v[0][2];
  loop(i, 1, n) {
    loop(j, 0, 3) dp[i][j] =
        max(dp[i - 1][(j + 1) % 3], dp[i - 1][(j + 2) % 3]) + v[i][j];
  }

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
#define fastio()                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define loop(i, a, b) for (ll i = a; i < b; i++)
#define loope(i, a, b) for (ll i = a; i <= b; i++)
#define test()                                                                 \
  ull t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define puu pair<ull, ull>
#define f first
#define s second
#define pb push_back
#define mkp make_pair
#define ins insert
#define Vint vector<int>
#define Vll vector<ll>
#define Vull vector<ull>
#define nl cout << endl
#define sp cout << " "
#define MOD 1000000007
#define all(x) x.begin(), x.end()

int main() {
  // #ifndef ONLINE_JUDGE
  //       freopen("input.txt", "r", stdin);
  //       freopen("output.txt", "w", stdout);
  //   #endif
  //   fastio();
  ll n;
  cin >> n;
  ll v[n][3];
  loop(i, 0, n) { loop(j, 0, 3) cin >> v[i][j]; }

  vector<vector<ll>> dp(n, vector<ll>(3, 0));
  dp[0][0] = v[0][0];
  dp[0][1] = v[0][1];
  dp[0][2] = v[0][2];
  loop(i, 1, n) {
    loop(j, 0, 3) dp[i][j] =
        max(dp[i - 1][(j + 1) % 3], dp[i - 1][(j + 2) % 3]) + v[i][j];
  }

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

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

int dp[1000][3];

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

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

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


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

int dp[100007][3];

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

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

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define vi vector<int>
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define pb push_back
#define pp pair<int, int>
#define rep(i, l, r) for (int i = l; i < r; i++)
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt.txt", "r", stdin);
  freopen("output.txt.txt", "w", stdout);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vi a(n), b(n), c(n);
  rep(i, 0, n) cin >> a[i] >> b[i] >> c[i];
  int dp[n][3];
  rep(i, 0, n) {
    if (i == 0) {
      dp[i][0] = a[i], dp[i][1] = b[i], dp[i][2] = c[i];
    } else {
      dp[i][0] = max(dp[i - 1][1] + a[i], dp[i - 1][2] + a[i]);
      dp[i][1] = max(dp[i - 1][0] + b[i], dp[i - 1][2] + b[i]);
      dp[i][2] = max(dp[i - 1][0] + c[i], dp[i - 1][1] + c[i]);
    }
  }
  cout << max(dp[n - 1][0], max(dp[n - 1][1], dp[n - 1][2]));
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define vi vector<int>
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define pb push_back
#define pp pair<int, int>
#define rep(i, l, r) for (int i = l; i < r; i++)
int main() {
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt.txt","r",stdin);
  // freopen("output.txt.txt","w",stdout);
  // #endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vi a(n), b(n), c(n);
  rep(i, 0, n) cin >> a[i] >> b[i] >> c[i];
  int dp[n][3];
  rep(i, 0, n) {
    if (i == 0) {
      dp[i][0] = a[i], dp[i][1] = b[i], dp[i][2] = c[i];
    } else {
      dp[i][0] = max(dp[i - 1][1] + a[i], dp[i - 1][2] + a[i]);
      dp[i][1] = max(dp[i - 1][0] + b[i], dp[i - 1][2] + b[i]);
      dp[i][2] = max(dp[i - 1][0] + c[i], dp[i - 1][1] + c[i]);
    }
  }
  cout << max(dp[n - 1][0], max(dp[n - 1][1], dp[n - 1][2]));
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<vector<int>> vvi;
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define rep(i, a, b) for (int i = a; i <= b; i++)

const int INF = INT_MAX;

int fact[9];

int main() {

  ios::sync_with_stdio(0);
  cin.tie(0);

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

  int n;
  cin >> n;
  int **dp = new int *[3];
  rep(i, 0, 2) dp[i] = new int[n + 1];
  dp[0][0] = 0;
  dp[1][0] = 0;
  dp[2][0] = 0;

  rep(day, 1, n) {
    rep(i, 0, 2) {
      int x;
      cin >> x;
      if (i == 0)
        dp[0][day] = x + max(dp[1][day - 1], dp[2][day - 1]);
      else if (i == 1)
        dp[1][day] = x + max(dp[0][day - 1], dp[2][day - 1]);
      else
        dp[2][day] = x + max(dp[0][day - 1], dp[1][day - 1]);
    }
  }

  int ans = dp[0][n];
  rep(i, 1, 2) ans = max(ans, dp[i][n]);
  cout << ans;

  rep(i, 0, 2) delete[] dp[i];
  delete[] dp;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<vector<int>> vvi;
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define rep(i, a, b) for (int i = a; i <= b; i++)

const int INF = INT_MAX;

int fact[9];

int main() {

  ios::sync_with_stdio(0);
  cin.tie(0);

  int n;
  cin >> n;
  int **dp = new int *[3];
  rep(i, 0, 2) dp[i] = new int[n + 1];
  dp[0][0] = 0;
  dp[1][0] = 0;
  dp[2][0] = 0;

  rep(day, 1, n) {
    rep(i, 0, 2) {
      int x;
      cin >> x;
      if (i == 0)
        dp[0][day] = x + max(dp[1][day - 1], dp[2][day - 1]);
      else if (i == 1)
        dp[1][day] = x + max(dp[0][day - 1], dp[2][day - 1]);
      else
        dp[2][day] = x + max(dp[0][day - 1], dp[1][day - 1]);
    }
  }

  int ans = dp[0][n];
  rep(i, 1, 2) ans = max(ans, dp[i][n]);
  cout << ans;

  rep(i, 0, 2) delete[] dp[i];
  delete[] dp;

  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 REP0(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REP1(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)

typedef long long LL;
typedef pair<int, int> pii;

const int INTINF = 1e9;
const LL LLINF = 1e18;
const int dg5 = 1e5;

// dp[j][i] : i 日目に j を選ぶとしたときの最大幸福度
LL dp[2][dg5 + 1];
LL abc[2][dg5 + 1];

void solve() {
  int N;
  cin >> N;
  REP0(i, N) { cin >> abc[0][i] >> abc[1][i] >> abc[2][i]; }

  REP0(i, 3) { dp[i][0] = abc[i][0]; }

  REP1(i, N - 1) {
    REP0(j, 3) {
      dp[j][i] = max(dp[(j + 1) % 3][i - 1] + abc[j][i],
                     dp[(j + 2) % 3][i - 1] + abc[j][i]);
    }
  }

  LL ans = 0;
  REP0(i, 3) { ans = max(ans, dp[i][N - 1]); }

  cout << ans << endl;
}

int main(int argc, char const *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(10);
  solve();
  return 0;
}

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

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP0(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REP1(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)

typedef long long LL;
typedef pair<int, int> pii;

const int INTINF = 1e9;
const LL LLINF = 1e18;
const int dg5 = 1e5;

// dp[j][i] : i 日目に j を選ぶとしたときの最大幸福度
LL dp[3][dg5 + 1];
LL abc[3][dg5 + 1];

void solve() {
  int N;
  cin >> N;
  REP0(i, N) { cin >> abc[0][i] >> abc[1][i] >> abc[2][i]; }

  REP0(i, 3) { dp[i][0] = abc[i][0]; }

  REP1(i, N - 1) {
    REP0(j, 3) {
      dp[j][i] = max(dp[(j + 1) % 3][i - 1] + abc[j][i],
                     dp[(j + 2) % 3][i - 1] + abc[j][i]);
    }
  }

  LL ans = 0;
  REP0(i, 3) { ans = max(ans, dp[i][N - 1]); }

  cout << ans << endl;
}

int main(int argc, char const *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(10);
  solve();
  return 0;
}