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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define pb push_back
#define csl                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)
#define reps(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, b) for (int i = 0; i < int(b); i++)
#define first ff
#define second ss
#define size sz
typedef long long ll;
typedef vector<long long> vll;
typedef vector<pair<int, int>> vii;
typedef vector<int> vi;
typedef pair<ll, ll> pll;
const int INF = 1e9 + 5;
int main() {
  csl;
  int n;
  cin >> n;
  vector<int> dp(3, 0);
  for (int d = 0; d < n; d++) {
    vector<int> q(3);
    vector<int> inp(3);
    rep(j, 3) { cin >> inp[j]; }

    rep(i, n) {
      rep(j, n) {
        if (i != j) {
          q[j] = max(q[j], dp[i] + inp[j]);
        }
      }
    }
    dp = q;
  }

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


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define pb push_back
#define csl                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)
#define reps(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, b) for (int i = 0; i < int(b); i++)
#define first ff
#define second ss
#define size sz
typedef long long ll;
typedef vector<long long> vll;
typedef vector<pair<int, int>> vii;
typedef vector<int> vi;
typedef pair<ll, ll> pll;
const int INF = 1e9 + 5;
int main() {
  csl;
  int n;
  cin >> n;
  vector<int> dp(3, 0);
  for (int d = 0; d < n; d++) {
    vector<int> q(3);
    vector<int> inp(3);
    rep(j, 3) { cin >> inp[j]; }

    rep(i, 3) {
      rep(j, 3) {
        if (i != j) {
          q[j] = max(q[j], dp[i] + inp[j]);
        }
      }
    }
    dp = q;
  }

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <math.h>
#include <string>
using namespace std;

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

/*int dp[100001];
int main(){
int N,K;
cin>>N>>K;
int h[100000];
for(int i=1;i<N+1;i++){
cin>>h[i];
}
dp[0]=0;
dp[1]=0;
for(int i=2;i<N+1;i++){
dp[i]=99999999;
if(i<=K){
for(int j=1;j<i;j++){
dp[i]=min(dp[i],dp[j]+abs(h[i]-h[j]));
}
}
if(i>K){
for(int j=i-K;j<i;j++){
dp[i]=min(dp[i],dp[j]+abs(h[i]-h[j]));
}
}
}
cout<<dp[N]<<endl;*/

/*int dp[200000];
int main(){
int N,C;
cin>>N>>C;
int h[200000];
for(int i=1;i<N+1;i++){
cin>>h[i];
}
dp[1]=0;
dp[2]=(h[2]-h[1])*(h[2]-h[1])+C;
for(int i=3;i<N+1;i++){
dp[i]=99999999;
for(int j=1;j<i;j++){
dp[i]=min(dp[i],dp[j]+(h[i]-h[j])*(h[i]-h[j])+C);
}
}
cout<<dp[N]<<endl;*/


Fixed submission:
#include <algorithm>
#include <iostream>
#include <math.h>
#include <string>
using namespace std;

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

/*int dp[100001];
int main(){
int N,K;
cin>>N>>K;
int h[100000];
for(int i=1;i<N+1;i++){
cin>>h[i];
}
dp[0]=0;
dp[1]=0;
for(int i=2;i<N+1;i++){
dp[i]=99999999;
if(i<=K){
for(int j=1;j<i;j++){
dp[i]=min(dp[i],dp[j]+abs(h[i]-h[j]));
}
}
if(i>K){
for(int j=i-K;j<i;j++){
dp[i]=min(dp[i],dp[j]+abs(h[i]-h[j]));
}
}
}
cout<<dp[N]<<endl;*/

/*int dp[200000];
int main(){
int N,C;
cin>>N>>C;
int h[200000];
for(int i=1;i<N+1;i++){
cin>>h[i];
}
dp[1]=0;
dp[2]=(h[2]-h[1])*(h[2]-h[1])+C;
for(int i=3;i<N+1;i++){
dp[i]=99999999;
for(int j=1;j<i;j++){
dp[i]=min(dp[i],dp[j]+(h[i]-h[j])*(h[i]-h[j])+C);
}
}
cout<<dp[N]<<endl;*/
Problem p03162 (C++)

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

int const N = 1e5 + 5, M = 1e9 + 7, OO = 0x3f3f3f3f;

int n;
int arr[3][N];

int mem[N][4];
int solve(int i, int last) {
  if (i == n)
    return 0;
  if (mem[i][last] = !-1)
    return mem[i][last];
  int op1 = (last != 1) ? solve(i + 1, 1) + arr[0][i] : 0;
  int op2 = (last != 2) ? solve(i + 1, 2) + arr[1][i] : 0;
  int op3 = (last != 3) ? solve(i + 1, 3) + arr[2][i] : 0;
  return mem[i][last] = max(op1, max(op2, op3));
}

int main() {
  //    freopen("input.txt","rt",stdin);
  //    freopen("output.txt","wt",stdout);
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d %d %d", arr[0] + i, arr[1] + i, arr[2] + i);
  memset(mem, -1, sizeof mem);
  printf("%d\n", solve(0, 0));

  return 0;
}


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

int const N = 1e5 + 5, M = 1e9 + 7, OO = 0x3f3f3f3f;

int n;
int arr[3][N];

int mem[N][4];
int solve(int i, int last) {
  if (i == n)
    return 0;
  if (mem[i][last] != -1)
    return mem[i][last];
  int op1 = (last != 1) ? solve(i + 1, 1) + arr[0][i] : 0;
  int op2 = (last != 2) ? solve(i + 1, 2) + arr[1][i] : 0;
  int op3 = (last != 3) ? solve(i + 1, 3) + arr[2][i] : 0;
  return mem[i][last] = max(op1, max(op2, op3));
}

int main() {
  //    freopen("input.txt","rt",stdin);
  //    freopen("output.txt","wt",stdout);
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d %d %d", arr[0] + i, arr[1] + i, arr[2] + i);
  memset(mem, -1, sizeof mem);
  printf("%d\n", solve(0, 0));

  return 0;
}
Problem p03162 (C++)

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

int main() {
  int n;
  cin >> n;
  vector<int> dp(3);
  // dp[0] = max({dp[0],dp[1],dp[2]});
  for (int day = 0; day < n; day++) {
    vector<int> new_dp(3);
    vector<int> c(3);
    for (int i = 0; i < 3; ++i) {
      scanf("%d", &c[i]);
    }
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        if (j != i) {
          new_dp[j] = max(new_dp[j], dp[i] + c[j]);
        }
      }
    }
    dp = new_dp;
  }

  cout << dp[n - 1];

  return 0;
}

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

int main() {
  int n;
  cin >> n;
  vector<int> dp(3);
  // dp[0] = max({dp[0],dp[1],dp[2]});
  for (int day = 0; day < n; day++) {
    vector<int> new_dp(3);
    vector<int> c(3);
    for (int i = 0; i < 3; ++i) {
      scanf("%d", &c[i]);
    }
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        if (j != i) {
          new_dp[j] = max(new_dp[j], dp[i] + c[j]);
        }
      }
    }
    dp = new_dp;
  }

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

  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> a(N);
  vector<int> b(N);
  vector<int> c(N);
  for (int i = 0; i < N; i++) {
    int A, B, C;
    cin >> A >> B >> C;
    a.at(i) = A;
    b.at(i) = B;
    c.at(i) = C;
  }
  vector<vector<int>> dp(N + 1, vector<int>(3, 0));
  for (int i = 0; i <= N; i++) {
    dp[i][0] = a.at(i - 1) + max(dp[i - 1][1], dp[i - 1][2]);
    dp[i][1] = b.at(i - 1) + max(dp[i - 1][0], dp[i - 1][2]);
    dp[i][2] = c.at(i - 1) + max(dp[i - 1][0], dp[i - 1][1]);
  }
  int ans = max(dp[N][0], max(dp[N][1], dp[N][2]));
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
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++) {
    int A, B, C;
    cin >> A >> B >> C;
    a.at(i) = A;
    b.at(i) = B;
    c.at(i) = C;
  }
  vector<vector<int>> dp(N + 1, vector<int>(3, 0));
  for (int i = 1; i <= N; i++) {
    dp[i][0] = a.at(i - 1) + max(dp[i - 1][1], dp[i - 1][2]);
    dp[i][1] = b.at(i - 1) + max(dp[i - 1][0], dp[i - 1][2]);
    dp[i][2] = c.at(i - 1) + max(dp[i - 1][0], dp[i - 1][1]);
  }
  int ans = max(dp[N][0], max(dp[N][1], dp[N][2]));
  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(NULL);                                                               \
  cout.tie(NULL);
#define pb push_back
#define ll long long
#define mp make_pair
#define MAXN 1e6
const int mod = 1e9 + 7;

void solve() {
  int n, x;
  cin >> n;
  vector<int> dp(3);
  for (int i = 0; i < n; i++) {
    vector<int> new_dp(3, 0);
    vector<int> c(3);
    for (int j = 0; j < 3; j++)
      cin >> c[i];

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

int main() { fastio solve(); }


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define pb push_back
#define ll long long
#define mp make_pair
#define MAXN 1e6
const int mod = 1e9 + 7;

void solve() {
  int n, x;
  cin >> n;
  vector<int> dp(3);
  for (int i = 0; i < n; i++) {
    vector<int> new_dp(3, 0);
    vector<int> c(3);
    for (int j = 0; j < 3; j++)
      cin >> c[j];

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

int main() { fastio solve(); }
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define sanyes                                                                 \
  ios_base::sync_with_stdio(0), cout.tie(0);                                   \
  cin.tie(0);
#define llong long long
#define pb push_back
#define bpc __builtin_popcount

const llong mxn = 1e5 + 7;
const llong mod = 998244353;
const llong inf = 1e18 + 9;

using namespace std;

llong n, l, dp[1001][3], a[mxn], b[mxn], c[mxn], mx = -inf;

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


Fixed submission:
#include <bits/stdc++.h>
#define sanyes                                                                 \
  ios_base::sync_with_stdio(0), cout.tie(0);                                   \
  cin.tie(0);
#define llong long long
#define pb push_back
#define bpc __builtin_popcount

const llong mxn = 1e5 + 7;
const llong mod = 998244353;
const llong inf = 1e18 + 9;

using namespace std;

llong n, l, dp[mxn][3], a[mxn], b[mxn], c[mxn], mx = -inf;

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

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

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

  int dp[10010][3];

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


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

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

  int dp[100010][3];

  for (int i = 0; i < N; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    if (i == 0) {
      dp[i][0] = a;
      dp[i][1] = b;
      dp[i][2] = c;
    } else {
      dp[i][0] = a + max({dp[i - 1][1], dp[i - 1][2]});
      dp[i][1] = b + max({dp[i - 1][0], dp[i - 1][2]});
      dp[i][2] = c + max({dp[i - 1][0], dp[i - 1][1]});
    }
  }
  cout << max({dp[N - 1][0], dp[N - 1][1], dp[N - 1][2]});
}
Problem p03162 (C++)

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

int dp[10005][3] = {0};

int ans(int *a, int *b, int *c, int n) {
  dp[1][0] = a[1];
  dp[1][1] = b[1];
  dp[1][2] = c[1];
  for (int i = 2; 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]);
  }
  return max(dp[n][0], max(dp[n][1], dp[n][2]));
}

int main() {
  int n;
  cin >> n;
  int a[n + 1], b[n + 1], c[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }
  cout << ans(a, b, c, n) << "\n";
}

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

int dp[100005][3] = {0};

int ans(int *a, int *b, int *c, int n) {
  dp[1][0] = a[1];
  dp[1][1] = b[1];
  dp[1][2] = c[1];
  for (int i = 2; 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]);
  }
  return max(dp[n][0], max(dp[n][1], dp[n][2]));
}

int main() {
  int n;
  cin >> n;
  int a[n + 1], b[n + 1], c[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }
  cout << ans(a, b, c, n) << "\n";
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define _USE_MATH_DEFINES
#include <math.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define NIL = -1;

ll gcd(long long a, long long b) {
  if (a < b)
    swap(a, b);
  if (b == 0)
    return a;

  return gcd(b, a % b);
}

ll lcm(ll a, ll b) { a / gcd(a, b) * b; }
const ll mod = 1e9 + 7;

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

const ll INF = 1e9;
/////////////////////////////////////////////////////////////////////////////////////////

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

  vector<vector<ll>> dp(n + 1, vector<ll>(n + 1, 0));
  dp[0][0] = 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][0] + a[i], dp[i][2] + c[i]);
    dp[i + 1][2] = max(dp[i][0] + a[i], dp[i][1] + b[i]);
  }

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

  cout << ans << endl;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define _USE_MATH_DEFINES
#include <math.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define NIL = -1;

ll gcd(long long a, long long b) {
  if (a < b)
    swap(a, b);
  if (b == 0)
    return a;

  return gcd(b, a % b);
}

ll lcm(ll a, ll b) { a / gcd(a, b) * b; }
const ll mod = 1e9 + 7;

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

const ll INF = 1e9;
/////////////////////////////////////////////////////////////////////////////////////////

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

  vector<vector<ll>> dp(n + 1, vector<ll>(3, 0));
  dp[0][0] = 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][0] + a[i], dp[i][2] + c[i]);
    dp[i + 1][2] = max(dp[i][0] + a[i], dp[i][1] + b[i]);
  }

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

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

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

#define ll long long
#define FASTIO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define ii pair<ll, ll>
#define all(x) std::begin(x), std::end(x)

ll const MAX = 100005;
ll const INF = 1e9;
ll const mod = 1000000007;

ll dp[MAX][3];
ll a[MAX][3];
ll n;
int k;

ll cost(int ndx, int ac) {
  if (ndx >= n)
    return 0;

  ll &ret = dp[ndx][ac];
  if (ret != -1)
    return ret;

  return max(a[ndx][(ac + 1) % 3] + cost(ndx + 1, (ac + 1) % 3),
             a[ndx][(ac + 2) % 3] + cost(ndx + 1, (ac + 2) % 3));
}

int main() {
  // freopen ( "portals.in" , "r" , stdin ) ;
  FASTIO;

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

  ll ans = 0;
  for (int i = 0; i < 3; i++)
    ans = max(ans, cost(0, i));
  cout << ans;
}


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

#define ll long long
#define FASTIO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define ii pair<ll, ll>
#define all(x) std::begin(x), std::end(x)

ll const MAX = 100005;
ll const INF = 1e9;
ll const mod = 1000000007;

ll dp[MAX][3];
ll a[MAX][3];
ll n;
int k;

ll cost(int ndx, int ac) {
  if (ndx >= n)
    return 0;

  ll &ret = dp[ndx][ac];
  if (ret != -1)
    return ret;

  return ret = max(a[ndx][(ac + 1) % 3] + cost(ndx + 1, (ac + 1) % 3),
                   a[ndx][(ac + 2) % 3] + cost(ndx + 1, (ac + 2) % 3));
}

int main() {
  // freopen ( "portals.in" , "r" , stdin ) ;
  FASTIO;

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

  ll ans = 0;
  for (int i = 0; i < 3; i++)
    ans = max(ans, cost(0, i));
  cout << ans;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define endl "\n"
const int MAX = 100005;
const long long mod = 1.0e9 + 7;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  ll n, k, i, j;
  cin >> n;
  ll a[n + 1], b[n + 1], c[n];
  vector<pair<ll, ll>> v[n];
  for (i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }
  ll dp[n + 1][3];
  memset(dp, 0, sizeof(dp));
  dp[1][0] = a[0];
  dp[1][1] = b[0];
  dp[1][2] = c[0];
  for (i = 2; i <= n; i++) {
    dp[i][0] = a[i - 1] + max(dp[i - 1][1], dp[i - 1][2]);
    dp[i][1] = b[i - 1] + max(dp[i - 1][0], dp[i - 1][2]);
    dp[i][2] = c[i - 1] + max(dp[i - 1][1], dp[i - 1][0]);
  }
  cout << max(dp[n][2], max(dp[n][1], dp[n][0])) << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define endl "\n"
const int MAX = 100005;
const long long mod = 1.0e9 + 7;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll n, k, i, j;
  cin >> n;
  ll a[n + 1], b[n + 1], c[n];
  vector<pair<ll, ll>> v[n];
  for (i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }
  ll dp[n + 1][3];
  memset(dp, 0, sizeof(dp));
  dp[1][0] = a[0];
  dp[1][1] = b[0];
  dp[1][2] = c[0];
  for (i = 2; i <= n; i++) {
    dp[i][0] = a[i - 1] + max(dp[i - 1][1], dp[i - 1][2]);
    dp[i][1] = b[i - 1] + max(dp[i - 1][0], dp[i - 1][2]);
    dp[i][2] = c[i - 1] + max(dp[i - 1][1], dp[i - 1][0]);
  }
  cout << max(dp[n][2], max(dp[n][1], dp[n][0])) << endl;
  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
const int INF = 1e9;
const int MOD = 1e9 + 7;
const long long LINF = 1e18;
#define dump(x) cout << 'x' << ' = ' << (x) << ` `;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOREACH(x, a) for (auto &(x) : (a))
typedef long long ll;
using namespace std;
int main(int argc, char const *argv[]) {
  int n;
  cin >> n;
  vector<vector<ll>> a(n + 1);
  for (int i = 1; i <= n; ++i)
    REP(j, 3) {
      int A;
      cin >> A;
      a[i].push_back(A);
    }
  ll dp[n + 10][n + 10];
  REP(i, n + 10) REP(j, n) dp[i][j] = 0;
  dp[0][0] = dp[0][1] = dp[0][2] = 0;
  for (int i = 1; i <= n; ++i)
    REP(j, 3) REP(k, 3) {
      if (k == j)
        continue;
      dp[i][j] = max(a[i][j] + dp[i - 1][k], dp[i][j]);
    }
  ll ans = 0;
  REP(i, 3) ans = max(ans, dp[n][i]);
  cout << ans << std::endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
const int INF = 1e9;
const int MOD = 1e9 + 7;
const long long LINF = 1e18;
#define dump(x) cout << 'x' << ' = ' << (x) << ` `;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOREACH(x, a) for (auto &(x) : (a))
typedef long long ll;
using namespace std;
int main(int argc, char const *argv[]) {
  int n;
  cin >> n;
  vector<vector<ll>> a(n + 1);
  for (int i = 1; i <= n; ++i)
    REP(j, 3) {
      int A;
      cin >> A;
      a[i].push_back(A);
    }
  ll dp[n + 10][3];
  REP(i, n + 10) REP(j, 3) dp[i][j] = 0;
  dp[0][0] = dp[0][1] = dp[0][2] = 0;
  for (int i = 1; i <= n; ++i)
    REP(j, 3) REP(k, 3) {
      if (k == j)
        continue;
      dp[i][j] = max(a[i][j] + dp[i - 1][k], dp[i][j]);
    }
  ll ans = 0;
  REP(i, 3) ans = max(ans, dp[n][i]);
  cout << ans << std::endl;
  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int mat[3][10001];
int main() {
#ifdef EVAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int N;
  cin >> N;
  int a[N];
  int b[N];
  int c[N];
  for (int i = 0; i < N; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }
  mat[0][0] = a[0];
  mat[1][0] = b[0];
  mat[2][0] = c[0];
  for (int i = 0; i < N - 1; i++) {
    for (int j = 0; j < 3; j++) {
      if (j == 0) {
        mat[1][i + 1] = max(mat[1][i + 1], mat[j][i] + b[i + 1]);
        mat[2][i + 1] = max(mat[2][i + 1], mat[j][i] + c[i + 1]);
      }
      if (j == 1) {
        mat[0][i + 1] = max(mat[0][i + 1], mat[j][i] + a[i + 1]);
        mat[2][i + 1] = max(mat[2][i + 1], mat[j][i] + c[i + 1]);
      }
      if (j == 2) {
        mat[0][i + 1] = max(mat[0][i + 1], mat[j][i] + a[i + 1]);
        mat[1][i + 1] = max(mat[1][i + 1], mat[j][i] + b[i + 1]);
      }
    }
  }
  cout << max(mat[2][N - 1], max(mat[1][N - 1], mat[0][N - 1]));
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int mat[3][100001];
int main() {
#ifdef EVAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int N;
  cin >> N;
  int a[N];
  int b[N];
  int c[N];
  for (int i = 0; i < N; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }
  mat[0][0] = a[0];
  mat[1][0] = b[0];
  mat[2][0] = c[0];
  for (int i = 0; i < N - 1; i++) {
    for (int j = 0; j < 3; j++) {
      if (j == 0) {
        mat[1][i + 1] = max(mat[1][i + 1], mat[j][i] + b[i + 1]);
        mat[2][i + 1] = max(mat[2][i + 1], mat[j][i] + c[i + 1]);
      }
      if (j == 1) {
        mat[0][i + 1] = max(mat[0][i + 1], mat[j][i] + a[i + 1]);
        mat[2][i + 1] = max(mat[2][i + 1], mat[j][i] + c[i + 1]);
      }
      if (j == 2) {
        mat[0][i + 1] = max(mat[0][i + 1], mat[j][i] + a[i + 1]);
        mat[1][i + 1] = max(mat[1][i + 1], mat[j][i] + b[i + 1]);
      }
    }
  }
  cout << max(mat[2][N - 1], max(mat[1][N - 1], mat[0][N - 1]));
}
Problem p03162 (C++)

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

#ifndef ONLINE_JUDGE
const bool DEBUG = true;
#else
const bool DEBUG = false;
#endif

using ll = long long;
using ull = unsigned long long;

#define F first
#define S second
#define all(x) begin(x), end(x)

template <class T> void read_vector(vector<T> &arr) {
  const int size = arr.size();
  for (int i = 0; i < size; ++i)
    cin >> arr[i];
}

// template <class T>
// vector<T> create_vector(int n) {
//	vector<T> v(n);
//	for (int i = 0; i < size; ++i) cin >> v[i];
//	return v;
// }

void exec() {
  int n;
  cin >> n;

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

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

  sort(all(dp[n - 1]));
  cout << dp[n - 1].back() << endl;
}

int main() {
  exec();

  return 0;
}

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

#ifndef ONLINE_JUDGE
const bool DEBUG = true;
#else
const bool DEBUG = false;
#endif

using ll = long long;
using ull = unsigned long long;

#define F first
#define S second
#define all(x) begin(x), end(x)

template <class T> void read_vector(vector<T> &arr) {
  const int size = arr.size();
  for (int i = 0; i < size; ++i)
    cin >> arr[i];
}

// template <class T>
// vector<T> create_vector(int n) {
//	vector<T> v(n);
//	for (int i = 0; i < size; ++i) cin >> v[i];
//	return v;
// }

void exec() {
  int n;
  cin >> n;

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

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

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

int main() {
  exec();

  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int dp[11000][3];
int main() {
  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];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      if (i == 0) {
        if (j == 0)
          dp[i][j] = a[i];
        else if (j == 1)
          dp[i][j] = b[i];
        else if (j == 2)
          dp[i][j] = c[i];
      } else {
        for (int k = 0; k < 3; k++) {
          if (j == k)
            continue;
          if (j == 0)
            dp[i][j] = max(dp[i][j], dp[i - 1][k] + a[i]);
          else if (j == 1)
            dp[i][j] = max(dp[i][j], dp[i - 1][k] + b[i]);
          else if (j == 2)
            dp[i][j] = max(dp[i][j], dp[i - 1][k] + c[i]);
        }
      }
    }
  }

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int dp[110000][3];
int main() {
  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];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      if (i == 0) {
        if (j == 0)
          dp[i][j] = a[i];
        else if (j == 1)
          dp[i][j] = b[i];
        else if (j == 2)
          dp[i][j] = c[i];
      } else {
        for (int k = 0; k < 3; k++) {
          if (j == k)
            continue;
          if (j == 0)
            dp[i][j] = max(dp[i][j], dp[i - 1][k] + a[i]);
          else if (j == 1)
            dp[i][j] = max(dp[i][j], dp[i - 1][k] + b[i]);
          else if (j == 2)
            dp[i][j] = max(dp[i][j], dp[i - 1][k] + c[i]);
        }
      }
    }
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int dp[2][3];
int main() {
  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];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      if (i == 0) {
        if (j == 0)
          dp[i % 2][j] = a[i];
        else if (j == 1)
          dp[i % 2][j] = b[i];
        else if (j == 2)
          dp[i % 2][j] = c[i];
      } else {
        for (int k = 0; k < 3; k++) {
          if (j == k)
            continue;
          if (j == 0)
            dp[i % 2][j] = max(dp[i % 2][j], dp[(i - 1) % 2][k] + a[i]);
          else if (j == 1)
            dp[i % 2][j] = max(dp[i % 2][j], dp[(i - 1) % 2][k] + b[i]);
          else if (j == 2)
            dp[i % 2][j] = max(dp[i % 2][j], dp[(i - 1) % 2][k] + c[i]);
        }
      }
    }
  }

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int dp[2][3];
int main() {
  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];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      if (i == 0) {
        if (j == 0)
          dp[i % 2][j] = a[i];
        else if (j == 1)
          dp[i % 2][j] = b[i];
        else if (j == 2)
          dp[i % 2][j] = c[i];
      } else {
        for (int k = 0; k < 3; k++) {
          if (j == k)
            continue;
          if (j == 0)
            dp[i % 2][j] = max(dp[i % 2][j], dp[(i - 1) % 2][k] + a[i]);
          else if (j == 1)
            dp[i % 2][j] = max(dp[i % 2][j], dp[(i - 1) % 2][k] + b[i]);
          else if (j == 2)
            dp[i % 2][j] = max(dp[i % 2][j], dp[(i - 1) % 2][k] + c[i]);
        }
      }
    }
  }

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

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

using namespace std;

int main() {

  int dp[3][10001];
  int act[3][10001];

  int N;
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> act[0][i] >> act[1][i] >> act[2][i];
  }

  dp[0][1] = act[0][1];
  dp[1][1] = act[1][1];
  dp[2][1] = act[2][1];

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

  int ret = 0;
  for (int i = 0; i < 3; i++) {
    ret = ret > dp[i][N] ? ret : dp[i][N];
  }

  cout << ret << "\n";

  return 0;
}


Fixed submission:
#include <iostream>
#include <math.h>
#include <vector>

using namespace std;

int main() {

  int dp[3][100001];
  int act[3][100001];

  int N;
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> act[0][i] >> act[1][i] >> act[2][i];
  }

  dp[0][1] = act[0][1];
  dp[1][1] = act[1][1];
  dp[2][1] = act[2][1];

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

  int ret = 0;
  for (int i = 0; i < 3; i++) {
    ret = ret > dp[i][N] ? ret : dp[i][N];
  }

  cout << ret << "\n";

  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long long int lli;
typedef long double ld;
const int oo = 1e9;
// const int mod = 1e9+9;
/*ll gcd(ll x, ll y){return (y ? gcd(y, x%y) : x); }
*ll lcm(ll x, ll y){return x*(y/gcd(x,y));}
*void exEuclid(int a, int b){
        if(b==0){x = 1; y=0; d=a; return ;}
        exEuclid(b, a%b);
        int x1=y;
        int y1=x-(a/b)*y;
        x=x1; 100000
        y=y1;
}
* */
//===========================================================//
int n;
const int nx = 1e5 + 5;
int ac[nx][3];
int dp[3][nx];
int go(int a, int i) {
  if (i == n)
    return 0;
  int &ans = dp[a][i];

  if (a == 0) {
    ans = max(ans, go(1, i + 1) + ac[i][0]);
    ans = max(ans, go(2, i + 1) + ac[i][0]);
  }
  if (a == 1) {
    ans = max(ans, go(0, i + 1) + ac[i][1]);
    ans = max(ans, go(2, i + 1) + ac[i][1]);
  }
  if (a == 2) {
    ans = max(ans, go(0, i + 1) + ac[i][2]);
    ans = max(ans, go(1, i + 1) + ac[i][2]);
  }

  return ans;
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> ac[i][0] >> ac[i][1] >> ac[i][2];
  memset(dp, -1, sizeof dp);
  cout << max(go(0, 0), max(go(1, 0), go(2, 0))) << "\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long long int lli;
typedef long double ld;
const int oo = 1e9;
// const int mod = 1e9+9;
/*ll gcd(ll x, ll y){return (y ? gcd(y, x%y) : x); }
*ll lcm(ll x, ll y){return x*(y/gcd(x,y));}
*void exEuclid(int a, int b){
        if(b==0){x = 1; y=0; d=a; return ;}
        exEuclid(b, a%b);
        int x1=y;
        int y1=x-(a/b)*y;
        x=x1; 100000
        y=y1;
}
* */
//===========================================================//
int n;
const int nx = 1e5 + 5;
int ac[nx][3];
int dp[3][nx];
int go(int a, int i) {
  if (i == n)
    return 0;
  if (dp[a][i] != -1)
    return dp[a][i];
  int &ans = dp[a][i];

  if (a == 0) {
    ans = max(ans, go(1, i + 1) + ac[i][0]);
    ans = max(ans, go(2, i + 1) + ac[i][0]);
  }
  if (a == 1) {
    ans = max(ans, go(0, i + 1) + ac[i][1]);
    ans = max(ans, go(2, i + 1) + ac[i][1]);
  }
  if (a == 2) {
    ans = max(ans, go(0, i + 1) + ac[i][2]);
    ans = max(ans, go(1, i + 1) + ac[i][2]);
  }

  return ans;
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> ac[i][0] >> ac[i][1] >> ac[i][2];
  memset(dp, -1, sizeof dp);
  cout << max(go(0, 0), max(go(1, 0), go(2, 0))) << "\n";
  return 0;
}
Problem p03162 (C++)

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

typedef long long ll;

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

int main(void) {
  int n;
  cin >> n;
  ll happy_arr[10][3];
  ll dp_arr[10][3];
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < 3; ++j) {
      cin >> happy_arr[i][j];
    }
  }
  for (int i = 0; i < 3; ++i) {
    dp_arr[0][i] = 0;
  }

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

  ll max = 0;
  for (int i = 0; i < 3; ++i) {
    chmax(max, dp_arr[n][i]);
  }

  cout << max << endl;

  return 0;
}

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

typedef long long ll;

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

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

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

  ll max = 0;
  for (int i = 0; i < 3; ++i) {
    chmax(max, dp_arr[n][i]);
  }

  cout << max << 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++)
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;

int main() {
  int a[10][3];
  int dp[10][3];
  int N;
  cin >> N;
  rep(i, N) rep(j, 3) cin >> a[i][j];
  rep(i, N + 1) rep(j, 3) dp[i][j] = 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] + a[i][k]);
      }
    }
  }
  int res = 0;
  rep(i, 3) res = max(res, dp[N][i]);
  cout << res << endl;

  return 0;
}

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

int main() {
  int a[100010][3];
  int dp[100010][3];
  int N;
  cin >> N;
  rep(i, N) rep(j, 3) cin >> a[i][j];
  rep(i, N + 1) rep(j, 3) dp[i][j] = 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] + a[i][k]);
      }
    }
  }
  int res = 0;
  rep(i, 3) res = max(res, dp[N][i]);
  cout << res << endl;

  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < (b); i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int inf = 1 << 20;
const int mod = 1e9 + 7;

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

int dp[10005][5];
int main() {
  int n;
  cin >> n;
  vector<int> a(n), b(n), c(n);
  rep(i, 0, n) cin >> a[i] >> b[i] >> c[i];
  dp[0][0] = a[0], dp[0][1] = b[0], dp[0][2] = c[0];

  rep(i, 0, n - 1) {
    chmax(dp[i + 1][0], dp[i][1] + a[i + 1]);
    chmax(dp[i + 1][0], dp[i][2] + a[i + 1]);
    chmax(dp[i + 1][1], dp[i][0] + b[i + 1]);
    chmax(dp[i + 1][1], dp[i][2] + b[i + 1]);
    chmax(dp[i + 1][2], dp[i][0] + c[i + 1]);
    chmax(dp[i + 1][2], dp[i][1] + c[i + 1]);
  }
  cout << max({dp[n - 1][0], dp[n - 1][1], dp[n - 1][2]}) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < (b); i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int inf = 1 << 20;
const int mod = 1e9 + 7;

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

int dp[100005][5];
int main() {
  int n;
  cin >> n;
  vector<int> a(n), b(n), c(n);
  rep(i, 0, n) cin >> a[i] >> b[i] >> c[i];
  dp[0][0] = a[0], dp[0][1] = b[0], dp[0][2] = c[0];

  rep(i, 0, n - 1) {
    chmax(dp[i + 1][0], dp[i][1] + a[i + 1]);
    chmax(dp[i + 1][0], dp[i][2] + a[i + 1]);
    chmax(dp[i + 1][1], dp[i][0] + b[i + 1]);
    chmax(dp[i + 1][1], dp[i][2] + b[i + 1]);
    chmax(dp[i + 1][2], dp[i][0] + c[i + 1]);
    chmax(dp[i + 1][2], dp[i][1] + c[i + 1]);
  }
  cout << max({dp[n - 1][0], dp[n - 1][1], dp[n - 1][2]}) << endl;
  return 0;
}
Problem p03162 (C++)

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

using namespace std;

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)
#define unless(p) if (!(p))
#define until(p) while (!(p))

using ll = long long;
using P = std::tuple<int, int>;

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

int N;
int A[100100][3];
int dp[100100][3];

int rec(int d, int p) {
  if (d == N) {
    return 0;
  }

  int res = 0;
  for (int n = 0; n < 3; ++n) {
    if (d > 0 && n == p) {
      continue;
    }

    res = std::max(res, A[d][n] + rec(d + 1, n));
  }

  return dp[d][p] = res;
}

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

  std::cin >> N;

  for (int i = 0; i < N; ++i) {
    std::cin >> A[i][0] >> A[i][1] >> A[i][2];
  }

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

  int res = rec(0, 0);
  std::cout << res << std::endl;
}


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

using namespace std;

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)
#define unless(p) if (!(p))
#define until(p) while (!(p))

using ll = long long;
using P = std::tuple<int, int>;

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

int N;
int A[100100][3];
int dp[100100][3];

int rec(int d, int p) {
  if (d == N) {
    return 0;
  }

  if (dp[d][p] != -1) {
    return dp[d][p];
  }

  int res = 0;
  for (int n = 0; n < 3; ++n) {
    if (d > 0 && n == p) {
      continue;
    }

    res = std::max(res, A[d][n] + rec(d + 1, n));
  }

  return dp[d][p] = res;
}

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

  std::cin >> N;

  for (int i = 0; i < N; ++i) {
    std::cin >> A[i][0] >> A[i][1] >> A[i][2];
  }

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

  int res = rec(0, 0);
  std::cout << res << std::endl;
}
Problem p03162 (C++)

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

ll INF = 1e+18;
int iINF = 1e9;
int MAXI = 1e8 + 1;
using vec = vector<int>;

int main() {
  int N;
  cin >> N;
  vector<vector<int>> arr(N);
  rep(i, 0, N) {
    int a, b, c;
    cin >> a >> b >> c;
    arr[i].push_back(a);
    arr[i].push_back(b);
    arr[i].push_back(c);
  }
  vector<vector<int>> dp(N, vec(3, 0));
  dp[0][0] = arr[0][0];
  dp[0][1] = arr[0][1];
  dp[0][2] = arr[0][2];

  rep(i, 0, N - 1) {
    rep(j, 0, 3) {
      rep(k, 0, 3) {
        if (j == k)
          continue;
        if (dp[j + 1][k] < dp[i][j] + arr[i + 1][k])
          dp[i + 1][k] = dp[i][j] + arr[i + 1][k];
      }
    }
  }

  int ans = 0;
  rep(i, 0, 3) {
    if (dp[N - 1][i] > ans)
      ans = dp[N - 1][i];
  }

  cout << ans << endl;
}

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

ll INF = 1e+18;
int iINF = 1e9;
int MAXI = 1e8 + 1;
using vec = vector<int>;

int main() {
  int N;
  cin >> N;
  vector<vector<int>> arr(N);
  rep(i, 0, N) {
    int a, b, c;
    cin >> a >> b >> c;
    arr[i].push_back(a);
    arr[i].push_back(b);
    arr[i].push_back(c);
  }
  vector<vector<int>> dp(N, vec(3, 0));
  dp[0][0] = arr[0][0];
  dp[0][1] = arr[0][1];
  dp[0][2] = arr[0][2];

  rep(i, 0, N - 1) {
    rep(j, 0, 3) {
      rep(k, 0, 3) {
        if (j == k)
          continue;
        if (dp[i + 1][k] < dp[i][j] + arr[i + 1][k])
          dp[i + 1][k] = dp[i][j] + arr[i + 1][k];
      }
    }
  }

  int ans = 0;
  rep(i, 0, 3) {
    if (dp[N - 1][i] > ans)
      ans = dp[N - 1][i];
  }

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

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

using namespace std;

bool **v;
int **dp;
int *a, *b, *c;
int solve(int n, const int &max, char last) {
  if (n == max)
    return 0;

  if (v[n][last])
    return dp[n][last];

  int score_a = last == 'a' ? 0 : solve(n + 1, max, 'a') + a[n];
  int score_b = last == 'b' ? 0 : solve(n + 1, max, 'b') + b[n];
  int score_c = last == 'c' ? 0 : solve(n + 1, max, 'c') + c[n];

  dp[n][last] = std::max({score_a, score_b, score_c});
  v[n][last] = 1;

  return dp[n][last];
}

int main() {
  int N;
  cin >> N;
  v = new bool *[N];
  a = new int[N];
  b = new int[N];
  c = new int[N];
  dp = new int *[N];

  for (int i = 0; i < N; i++) {
    v[i] = new bool[3];
    dp[i] = new int[3];
    cin >> a[i];
    cin >> b[i];
    cin >> c[i];
  }

  fill(&dp[0][0], &dp[0][0] + sizeof(dp), 0);
  fill(&v[0][0], &v[0][0] + sizeof(v), 0);

  cout << solve(0, N, 0) << endl;
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"

using namespace std;

bool **v;
int **dp;
int *a, *b, *c;
int solve(int n, const int &max, char last) {
  if (n == max)
    return 0;

  if (v[n][last])
    return dp[n][last];

  int score_a = last == 'a' ? 0 : solve(n + 1, max, 'a') + a[n];
  int score_b = last == 'b' ? 0 : solve(n + 1, max, 'b') + b[n];
  int score_c = last == 'c' ? 0 : solve(n + 1, max, 'c') + c[n];

  dp[n][last] = std::max({score_a, score_b, score_c});
  v[n][last] = 1;

  return dp[n][last];
}

int main() {
  int N;
  cin >> N;
  v = new bool *[N];
  a = new int[N];
  b = new int[N];
  c = new int[N];
  dp = new int *[N];

  for (int i = 0; i < N; i++) {
    v[i] = new bool[3];
    dp[i] = new int[3];
    cin >> a[i];
    cin >> b[i];
    cin >> c[i];
  }

  // fill(&dp[0][0], &dp[0][0] + sizeof(dp), 0);
  // fill(&v[0][0], &v[0][0] + sizeof(v), 0);

  cout << solve(0, N, 0) << endl;
  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
ll n;
ll func(ll a[][4]) {
  ll h[n + 1][4];
  h[1][1] = a[1][1];
  h[1][2] = a[1][2];
  h[1][3] = a[1][3];
  for (ll i = 2; i <= n; i++) {
    h[i][1] = a[i][1] + max(h[i - 1][2], h[i - 1][3]);
    h[i][2] = a[i][2] + max(h[i - 1][3], h[i - 1][1]);
    h[i][3] = a[i][3] + max(h[i - 1][1], h[i - 1][2]);
  }
  cout << max(h[n][1], max(h[n][2], h[n][3]));
}
int32_t main() {
  IOS;
  cin >> n;
  ll a[n + 1][4];
  for (ll i = 1; i <= n; i++) {
    cin >> a[i][1] >> a[i][2] >> a[i][3];
  }
  func(a);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
ll n;
void func(ll a[][4]) {
  ll h[n + 1][4];
  h[1][1] = a[1][1];
  h[1][2] = a[1][2];
  h[1][3] = a[1][3];
  for (ll i = 2; i <= n; i++) {
    h[i][1] = a[i][1] + max(h[i - 1][2], h[i - 1][3]);
    h[i][2] = a[i][2] + max(h[i - 1][3], h[i - 1][1]);
    h[i][3] = a[i][3] + max(h[i - 1][1], h[i - 1][2]);
  }
  cout << max(h[n][1], max(h[n][2], h[n][3]));
}
int32_t main() {
  IOS;
  cin >> n;
  ll a[n + 1][4];
  for (ll i = 1; i <= n; i++) {
    cin >> a[i][1] >> a[i][2] >> a[i][3];
  }
  func(a);
}