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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define MOD 1000000007
#define PI 3.1415926535897932
#define rep(i, n) for (int i = 0; i < n; i++)
#define repe(i, j, n) for (int i = j; i < n; i++)
#define repi(i, n) for (int i = 0; i <= n; i++)
#define repie(i, j, n) for (int i = j; i <= n; i++)
#define all(x) x.begin(), x.end()
#define println() cout << endl
#define P pair<int, int>
#define fi first
#define se second

typedef long long ll;

long long modinv(long long a, long long m) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

void solve1() {
  int n;
  cin >> n;
  vector<int> a(n), b(n), c(n);
  rep(i, n) { cin >> a[i] >> b[i] >> c[i]; }

  int dp[n][3];
  dp[0][0] = a[0];
  dp[0][1] = b[0];
  dp[0][2] = c[0];

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

  int ans = max({dp[n][0], dp[n][1], dp[n][2]});

  cout << ans << endl;
}

int main() { solve1(); }


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define MOD 1000000007
#define PI 3.1415926535897932
#define rep(i, n) for (int i = 0; i < n; i++)
#define repe(i, j, n) for (int i = j; i < n; i++)
#define repi(i, n) for (int i = 0; i <= n; i++)
#define repie(i, j, n) for (int i = j; i <= n; i++)
#define all(x) x.begin(), x.end()
#define println() cout << endl
#define P pair<int, int>
#define fi first
#define se second

typedef long long ll;

long long modinv(long long a, long long m) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

void solve1() {
  int n;
  cin >> n;
  vector<int> a(n), b(n), c(n);
  rep(i, n) { cin >> a[i] >> b[i] >> c[i]; }

  int dp[n + 1][3];
  dp[0][0] = a[0];
  dp[0][1] = b[0];
  dp[0][2] = c[0];

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

  int ans = max({dp[n][0], dp[n][1], dp[n][2]});

  cout << ans << endl;
}

int main() { solve1(); }
Problem p03162 (C++)

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

#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPN(i, n) for (ll i = 0; i < n; i++, cout << "\n")
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define mod1 1000000007
#define ll long long
#define clr0(arr) memset(arr, 0, sizeof(arr))
#define clrval(arr, val) memset(arr, val, sizeof(arr))
using namespace std;

ll dp[10010][3];

ll memo(ll arr[][3], ll n, ll prev) {

  if (n <= 0)
    return 0;

  if (prev != -1 && dp[n - 1][prev] != -1)
    return dp[n - 1][prev];

  ll tempAns = INT_MIN;

  REP(i, 3) {
    if (i == prev)
      continue;

    ll val = arr[n - 1][i] + memo(arr, n - 1, i);
    tempAns = max(tempAns, val);
  }

  if (prev != -1)
    dp[n - 1][prev] = tempAns;
  return tempAns;
}

void solve() {

  ll n;
  cin >> n;

  ll arr[n][3];
  REP(i, n)
  cin >> arr[i][0] >> arr[i][1] >> arr[i][2];

  clrval(dp, -1);

  cout << memo(arr, n, -1) << "\n";
  // cout<<rec(arr,n,-1)<<"\n";
}

int main() {
  // int t;
  // cin>>t;

  // while(t--){
  solve();
  // }

  return 0;
}


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

#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPN(i, n) for (ll i = 0; i < n; i++, cout << "\n")
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define mod1 1000000007
#define ll long long
#define clr0(arr) memset(arr, 0, sizeof(arr))
#define clrval(arr, val) memset(arr, val, sizeof(arr))
using namespace std;

ll dp[100100][3];

ll memo(ll arr[][3], ll n, ll prev) {

  if (n <= 0)
    return 0;

  if (prev != -1 && dp[n - 1][prev] != -1)
    return dp[n - 1][prev];

  ll tempAns = INT_MIN;

  REP(i, 3) {
    if (i == prev)
      continue;

    ll val = arr[n - 1][i] + memo(arr, n - 1, i);
    tempAns = max(tempAns, val);
  }

  if (prev != -1)
    dp[n - 1][prev] = tempAns;
  return tempAns;
}

void solve() {

  ll n;
  cin >> n;

  ll arr[n][3];
  REP(i, n)
  cin >> arr[i][0] >> arr[i][1] >> arr[i][2];

  clrval(dp, -1);

  cout << memo(arr, n, -1) << "\n";
  // cout<<rec(arr,n,-1)<<"\n";
}

int main() {
  // int t;
  // cin>>t;

  // while(t--){
  solve();
  // }

  return 0;
}
Problem p03162 (C++)

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

int INF = 1001001001;

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 main() {
  int n;
  cin >> n;
  vector<vector<int>> a(n, vector<int>(3));
  rep(i, n) rep(j, 3) cin >> a[i][j];
  vector<vector<int>> dp(n + 1, vector<int>(3));
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < 3; j++) {
      for (int k = 0; k < 3; k++) {
        if (j == k)
          continue;
        chmax(dp[i][j], dp[i - 1][k] + a[i][j]);
      }
    }
  }
  int ans = 0;
  rep(i, 3) chmax(ans, dp[n][i]);
  cout << ans << endl;
  return 0;
}

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

int INF = 1001001001;

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 main() {
  int n;
  cin >> n;
  vector<vector<int>> a(n, vector<int>(3));
  rep(i, n) rep(j, 3) cin >> a[i][j];
  vector<vector<int>> dp(n + 1, vector<int>(3));
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < 3; j++) {
      for (int k = 0; k < 3; k++) {
        if (j == k)
          continue;
        chmax(dp[i][j], dp[i - 1][k] + a[i - 1][j]);
      }
    }
  }
  int ans = 0;
  rep(i, 3) chmax(ans, dp[n][i]);
  cout << ans << endl;
  return 0;
}
Problem p03162 (C++)

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

int main() {
  ll int n;
  cin >> n;
  int a[n][3];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++)
      cin >> a[i][j];
  }
  ll int dp[n][n];
  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][1] + a[i][0], dp[i - 1][2] + a[i][0]);
    dp[i][1] = max(dp[i - 1][0] + a[i][1], dp[i - 1][2] + a[i][1]);
    dp[i][2] = max(dp[i - 1][0] + a[i][2], 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 <iostream>
#define ll long long
using namespace std;

int main() {
  ll int n;
  cin >> n;
  int a[n][3];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++)
      cin >> a[i][j];
  }
  ll int dp[n][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][1] + a[i][0], dp[i - 1][2] + a[i][0]);
    dp[i][1] = max(dp[i - 1][0] + a[i][1], dp[i - 1][2] + a[i][1]);
    dp[i][2] = max(dp[i - 1][0] + a[i][2], 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: Runtime Error):
#include <cmath>
#include <iostream>
using namespace std;

int abc[10001][3];
int dp[10001][3];
int n;

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> abc[i][0] >> abc[i][1] >> abc[i][2];
  }
  // dp[i][j]:=前日にjを選ぶ時にi日目までに得られる幸福度の最大値
  // i日目に...
  for (int i = 0; i <= n; i++) {
    // 前日にjを選んでいたとき...
    for (int j = 0; j < 3; j++) {
      // kを選ぶ
      for (int k = 0; k < 3; k++) {
        if (k == j)
          continue;
        dp[i + 1][k] = max(dp[i][j] + abc[i + 1][k], dp[i + 1][k]);
      }
    }
  }
  cout << max(max(dp[n][0], dp[n][1]), dp[n][2]) << endl;
  return 0;
}

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

int abc[100001][3];
int dp[100001][3];
int n;

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> abc[i][0] >> abc[i][1] >> abc[i][2];
  }
  // dp[i][j]:=前日にjを選ぶ時にi日目までに得られる幸福度の最大値
  // i日目に...
  for (int i = 0; i <= n; i++) {
    // 前日にjを選んでいたとき...
    for (int j = 0; j < 3; j++) {
      // kを選ぶ
      for (int k = 0; k < 3; k++) {
        if (k == j)
          continue;
        dp[i + 1][k] = max(dp[i][j] + abc[i + 1][k], dp[i + 1][k]);
      }
    }
  }
  cout << max(max(dp[n][0], dp[n][1]), dp[n][2]) << endl;
  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <vector>
#define ll long long
#define pii pair<int, int>
#define pll pair<long long, long long>
#define plxy pair<long long, pll>
#define INT_MAX 1e18
#define Keep_calm_You_are_still_learning                                       \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
using namespace std;
using namespace std;
const int mod = 1e9 + 7;
const int N = 1e3 + 5;
ll ans[N][3];
int main() {
  ll n, a[N], b[N], c[N];
  cin >> n;
  for (ll i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }
  ans[0][0] = a[0];
  ans[0][1] = b[0];
  ans[0][2] = c[0];
  for (ll i = 1; i < n; i++) {
    ans[i][0] = a[i] + max(ans[i - 1][1], ans[i - 1][2]);
    ans[i][1] = b[i] + max(ans[i - 1][0], ans[i - 1][2]);
    ans[i][2] = c[i] + max(ans[i - 1][0], ans[i - 1][1]);
  }
  cout << max(ans[n - 1][0], max(ans[n - 1][1], ans[n - 1][2]));
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <vector>
#define ll long long
#define pii pair<int, int>
#define pll pair<long long, long long>
#define plxy pair<long long, pll>
#define INT_MAX 1e18
#define Keep_calm_You_are_still_learning                                       \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
using namespace std;
using namespace std;
const int mod = 1e9 + 7;
const int N = 1e5 + 5;
ll ans[N][3];
int main() {
  ll n, a[N], b[N], c[N];
  cin >> n;
  for (ll i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }
  ans[0][0] = a[0];
  ans[0][1] = b[0];
  ans[0][2] = c[0];
  for (ll i = 1; i < n; i++) {
    ans[i][0] = a[i] + max(ans[i - 1][1], ans[i - 1][2]);
    ans[i][1] = b[i] + max(ans[i - 1][0], ans[i - 1][2]);
    ans[i][2] = c[i] + max(ans[i - 1][0], ans[i - 1][1]);
  }
  cout << max(ans[n - 1][0], max(ans[n - 1][1], ans[n - 1][2]));
  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define int long long
#define N 100011

using namespace std;
const int INF = -(int)1e17;

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

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> arr[i][0] >> arr[i][1] >> arr[i][2];
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int res = INF;
      if (i == 0)
        res = arr[i][j];
      else {
        if (j == 0)
          res = max(opt[i - 1][1], opt[i - 1][2]);
        else if (j == 1)
          res = max(opt[i - 1][0], opt[i - 1][2]);
        else
          res = max(opt[i - 1][0], opt[i - 1][1]);
        res += arr[i][j];
      }
      opt[i][j] = res;
    }
  }
  int ans = INF;
  for (int i = 0; i < 3; i++)
    ans = max(ans, opt[n - 1][i]);
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define N 100011

using namespace std;
const int INF = -(int)1e17;

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

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> arr[i][0] >> arr[i][1] >> arr[i][2];
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      int res = INF;
      if (i == 0)
        res = arr[i][j];
      else {
        if (j == 0)
          res = max(opt[i - 1][1], opt[i - 1][2]);
        else if (j == 1)
          res = max(opt[i - 1][0], opt[i - 1][2]);
        else
          res = max(opt[i - 1][0], opt[i - 1][1]);
        res += arr[i][j];
      }
      opt[i][j] = res;
    }
  }
  int ans = INF;
  for (int i = 0; i < 3; i++)
    ans = max(ans, opt[n - 1][i]);
  cout << ans << endl;
  return 0;
}
Problem p03162 (C++)

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

#define MAX 20000000
#define FOR(i, m, n) for (int i = (m); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
#define all(v) v.begin(), v.end()

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;

int main() {

  int n, x, m[3][MAX];
  ll dp[3][MAX];
  cin >> n;

  REP(i, 3 * n) {
    cin >> x;
    m[i % 3][i / 3] = x;
  }

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

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

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

  return 0;
}

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

#define MAX 2000000
#define FOR(i, m, n) for (int i = (m); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
#define all(v) v.begin(), v.end()

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;

int main() {

  int n, x, m[3][MAX];
  ll dp[3][MAX];
  cin >> n;

  REP(i, 3 * n) {
    cin >> x;
    m[i % 3][i / 3] = x;
  }

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

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

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

  return 0;
}
Problem p03162 (C++)

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

#define INF INT_MAX
#define MOD 1000000007
#define REP(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()

typedef long long ll;
typedef std::pair<int, int> pii;

using namespace std;

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

  int n;
  cin >> n;
  int abc[10000][3];
  int dp[10000][3];
  REP(i, n) REP(j, 3) cin >> abc[i][j];
  REP(i, 3) dp[0][i] = abc[0][i];
  for (int i = 1; i < n; i++) {
    REP(j, 3) {
      int max_v = 0;
      REP(k, 3) {
        if (j == k)
          continue;
        max_v = max(dp[i - 1][k] + abc[i][j], max_v);
      }
      dp[i][j] = max_v;
    }
  }
  int ans = 0;
  REP(i, 3) if (ans < dp[n - 1][i]) ans = dp[n - 1][i];
  cout << ans << endl;
  return 0;
}

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

#define INF INT_MAX
#define MOD 1000000007
#define REP(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()

typedef long long ll;
typedef std::pair<int, int> pii;

using namespace std;

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

  int n;
  cin >> n;
  int abc[100000][3];
  int dp[100000][3];
  REP(i, n) REP(j, 3) cin >> abc[i][j];
  REP(i, 3) dp[0][i] = abc[0][i];
  for (int i = 1; i < n; i++) {
    REP(j, 3) {
      int max_v = 0;
      REP(k, 3) {
        if (j == k)
          continue;
        max_v = max(dp[i - 1][k] + abc[i][j], max_v);
      }
      dp[i][j] = max_v;
    }
  }
  int ans = 0;
  REP(i, 3) if (ans < dp[n - 1][i]) ans = dp[n - 1][i];
  cout << ans << endl;
  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define FAST_IO                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
typedef long long ll;
/**
 * Uncomment the #define below if problem contains multiple test cases
 */
// #define MULTIPLE_TESTS 1

vector<vector<int>> cache;
int recursive(int cur, int prev, const vector<vector<int>> &cost) {
  if (cur == cost.size()) { // all done
    return 0;
  }
  if (prev != -1 && cache[cur][prev] != -1) {
    return cache[cur][prev];
  }
  int points = 0;
  // consider only ones with different prev
  for (int j = 0; j < 3; ++j) {
    if (j != prev) {
      points =
          max(points, recursive(cur + 1, j, cost) + cost[cur][j]); // prev = j
    }
  }
  return cache[cur][prev] = points;
}

void solve() {
  // write your code here
  int n;
  cin >> n;
  vector<vector<int>> cost(n, vector<int>(3)); // n*3 cost matrix
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < 3; ++j) {
      cin >> cost[i][j];
    }
  }
  cache.assign(n, vector<int>(3, -1));
  int ans = recursive(0, -1, cost);
  cout << ans << endl;
}

int main() {
  FAST_IO;
  int t = 1;
#ifdef MULTIPLE_TESTS
  cin >> t;
#endif

  while (t--) {
    solve();
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FAST_IO                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
typedef long long ll;
/**
 * Uncomment the #define below if problem contains multiple test cases
 */
// #define MULTIPLE_TESTS 1

vector<vector<int>> cache;
int recursive(int cur, int prev, const vector<vector<int>> &cost) {
  if (cur == cost.size()) { // all done
    return 0;
  }
  if (prev != -1 && cache[cur][prev] != -1) {
    return cache[cur][prev];
  }
  int points = 0;
  // consider only ones with different prev
  for (int j = 0; j < 3; ++j) {
    if (j != prev) {
      points =
          max(points, recursive(cur + 1, j, cost) + cost[cur][j]); // prev = j
    }
  }
  if (prev == -1) {
    return points;
  }
  return cache[cur][prev] = points;
}

void solve() {
  // write your code here
  int n;
  cin >> n;
  vector<vector<int>> cost(n, vector<int>(3)); // n*3 cost matrix
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < 3; ++j) {
      cin >> cost[i][j];
    }
  }
  cache.assign(n, vector<int>(3, -1));
  int ans = recursive(0, -1, cost);
  cout << ans << endl;
}

int main() {
  FAST_IO;
  int t = 1;
#ifdef MULTIPLE_TESTS
  cin >> t;
#endif

  while (t--) {
    solve();
  }

  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>
#define pb push_back
#define mp make_pair
#define ll long long
#define N 2 * 10e5
#define precise cout << fixed << setprecision(10)
#pragma GCC optimize("O0")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("-ffloat-store")
#define rep(i, a, b) for (int i = a; i < b; i++)
#define min3(a, b, c) min(a, min(b, c))
using namespace std;
void get(ll arr[], ll n) {
  for (ll i = 0; i < n; i++)
    cin >> arr[i];
}
#define max4(a, b, c, d) max(a, max(b, max(c, d)))
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll t;
  t = 1;
  // cin>>t;
  while (t--) {
    ll n;
    cin >> n;
    ll a[n + 1][3];
    ll dp[n + 1][n + 1];
    rep(i, 1, n + 1) { rep(j, 0, 3) cin >> a[i][j]; }
    dp[1][0] = a[1][0];
    dp[1][1] = a[1][1];
    dp[1][2] = a[1][2];
    for (ll i = 2; i <= n; i++) {
      for (ll j = 0; j < 3; j++) {
        if (j == 0)
          dp[i][j] = max(dp[i - 1][1], dp[i - 1][2]) + a[i][j];
        else if (j == 2)
          dp[i][j] = max(dp[i - 1][0], dp[i - 1][1]) + a[i][j];
        else if (j == 1)
          dp[i][j] = max(dp[i - 1][2], dp[i - 1][0]) + a[i][j];
      }
    }
    ll ans = 0;
    ans = max(ans, dp[n][0]);
    ans = max(ans, dp[n][1]);
    ans = max(ans, dp[n][2]);
    cout << ans;
  }
  // cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC <<
  // "ms\n";
}


Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
#define pb push_back
#define mp make_pair
#define ll long long
#define N 2 * 10e5
#define precise cout << fixed << setprecision(10)
#pragma GCC optimize("O0")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("-ffloat-store")
#define rep(i, a, b) for (int i = a; i < b; i++)
#define min3(a, b, c) min(a, min(b, c))
using namespace std;
void get(ll arr[], ll n) {
  for (ll i = 0; i < n; i++)
    cin >> arr[i];
}
#define max4(a, b, c, d) max(a, max(b, max(c, d)))
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll t;
  t = 1;
  // cin>>t;
  while (t--) {
    ll n;
    cin >> n;
    ll a[n + 1][3];
    ll dp[n + 1][3];
    rep(i, 1, n + 1) { rep(j, 0, 3) cin >> a[i][j]; }
    dp[1][0] = a[1][0];
    dp[1][1] = a[1][1];
    dp[1][2] = a[1][2];
    for (ll i = 2; i <= n; i++) {
      for (ll j = 0; j < 3; j++) {
        if (j == 0)
          dp[i][j] = max(dp[i - 1][1], dp[i - 1][2]) + a[i][j];
        else if (j == 2)
          dp[i][j] = max(dp[i - 1][0], dp[i - 1][1]) + a[i][j];
        else if (j == 1)
          dp[i][j] = max(dp[i - 1][2], dp[i - 1][0]) + a[i][j];
      }
    }
    ll ans = 0;
    ans = max(ans, dp[n][0]);
    ans = max(ans, dp[n][1]);
    ans = max(ans, dp[n][2]);
    cout << ans;
  }
  // cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC <<
  // "ms\n";
}
Problem p03162 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll ne(ll ar[], ll br[], ll cr[], ll n, int x, ll p) {
  if (p < n - 1) {
    if (x == 1)
      return max(br[p + 1] + ne(ar, br, cr, n, 2, p + 1),
                 cr[p + 1] + ne(ar, br, cr, n, 3, p + 1));
    else if (x == 2)
      return max(ar[p + 1] + ne(ar, br, cr, n, 1, p + 1),
                 cr[p + 1] + ne(ar, br, cr, n, 3, p + 1));
    else if (x == 3)
      return max(ar[p + 1] + ne(ar, br, cr, n, 1, p + 1),
                 br[p + 1] + ne(ar, br, cr, n, 2, p + 1));
  } else if (p == n - 1) {
    return 0;
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll n;
  cin >> n;
  ll ar[n], br[n], cr[n];
  for (ll i = 0; i < n; i++) {
    cin >> ar[i] >> br[i] >> cr[i];
  }
  cout << max(
      max(ar[0] + ne(ar, br, cr, n, 1, 0), br[0] + ne(ar, br, cr, n, 2, 0)),
      cr[0] + ne(ar, br, cr, n, 3, 0));
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll ne(ll ar[], ll br[], ll cr[], ll n, int x, ll p) {
  if (p < n - 1) {
    if (x == 1)
      return max(br[p + 1] + ne(ar, br, cr, n, 2, p + 1),
                 cr[p + 1] + ne(ar, br, cr, n, 3, p + 1));
    else if (x == 2)
      return max(ar[p + 1] + ne(ar, br, cr, n, 1, p + 1),
                 cr[p + 1] + ne(ar, br, cr, n, 3, p + 1));
    else if (x == 3)
      return max(ar[p + 1] + ne(ar, br, cr, n, 1, p + 1),
                 br[p + 1] + ne(ar, br, cr, n, 2, p + 1));
  } else if (p == n - 1) {
    return 0;
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll n;
  cin >> n;
  ll ar[n], br[n], cr[n];
  for (ll i = 0; i < n; i++) {
    cin >> ar[i] >> br[i] >> cr[i];
  }
  ll dr[n][3];
  dr[n - 1][0] = ar[n - 1];
  dr[n - 1][1] = br[n - 1];
  dr[n - 1][2] = cr[n - 1];
  for (ll i = n - 2; i >= 0; i--) {
    dr[i][0] = ar[i] + max(dr[i + 1][1], dr[i + 1][2]);
    dr[i][1] = br[i] + max(dr[i + 1][0], dr[i + 1][2]);
    dr[i][2] = cr[i] + max(dr[i + 1][1], dr[i + 1][0]);
  }
  cout << max(max(dr[0][0], dr[0][1]), dr[0][2]);
  return 0;
}
Problem p03162 (C++)

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

lli getmax(vector<vector<int>> &h, int i, int prev, int n,
           vector<vector<int>> &cache) {
  if (i >= n) {
    return 0;
  }

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

  lli maxi = 0;
  for (int j = 0; j < 3; j++) {
    if (j != prev) {
      maxi = max(maxi, h[i][j] + getmax(h, i + 1, j, n, cache));
    }
  }

  cache[i][prev] = maxi;
  return cache[i][prev];
}

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

  lli res = getmax(h, 0, -1, n, cache);
  cout << res << endl;
}

int main() { solve(); }


Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
#include <stdio.h>
#define lli long long int
using namespace std;

lli getmax(vector<vector<int>> &h, int i, int prev, int n,
           vector<vector<int>> &cache) {
  if (i >= n) {
    return 0;
  }

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

  lli maxi = 0;
  for (int j = 0; j < 3; j++) {
    if (j != prev) {
      maxi = max(maxi, h[i][j] + getmax(h, i + 1, j, n, cache));
    }
  }

  if (i != 0) {
    cache[i][prev] = maxi;
    return cache[i][prev];
  }

  return maxi;
}

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

  lli res = getmax(h, 0, -1, n, cache);
  cout << res << endl;
}

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define PB push_back
#define PPB pop_back
#define MP make_pair
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define test(x) cout << (x) << " "
#define d1(x) cout << (x) << endl
#define d2(x, y) cout << (x) << " " << (y) << endl
#define d3(x, y, z) cout << (x) << " " << (y) << " " << (z) << endl
#define loop(i, start, end) for (ll i = start; i < end; i++)
#define rloop(j, start, end) for (ll j = start - 1; j >= end; j--)

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

  ll n;
  cin >> n;

  ll a[n][3], dp[n + 1][3], maxi = INT_MIN, cnt = 0;

  loop(i, 0, n) cin >> a[i][0] >> a[i][1] >> a[i][2];

  memset(dp, 0, sizeof(dp));
  dp[0][0] = a[0][0];
  dp[0][1] = a[0][1];
  dp[0][2] = a[0][2];

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

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define PB push_back
#define PPB pop_back
#define MP make_pair
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define test(x) cout << (x) << " "
#define d1(x) cout << (x) << endl
#define d2(x, y) cout << (x) << " " << (y) << endl
#define d3(x, y, z) cout << (x) << " " << (y) << " " << (z) << endl
#define loop(i, start, end) for (ll i = start; i < end; i++)
#define rloop(j, start, end) for (ll j = start - 1; j >= end; j--)

int main() {
  IOS;

  ll n;
  cin >> n;

  ll a[n][3], dp[n + 1][3], maxi = INT_MIN, cnt = 0;

  loop(i, 0, n) cin >> a[i][0] >> a[i][1] >> a[i][2];

  memset(dp, 0, sizeof(dp));
  dp[0][0] = a[0][0];
  dp[0][1] = a[0][1];
  dp[0][2] = a[0][2];

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

  d1(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;
#define eb emplace_back
#define pb push_back
#define pii pair<int64_t, int64_t>
#define mp make_pair
#define whole(v) begin(v), end(v)
#define print64_t(c, n)                                                        \
  for (int64_t i = 0; i < n; i++)                                              \
    cout << c[i] << " ";
#define MOD 1000000007
#define INF 0x3f3f3f3f
int64_t dp[100005][3];
vector<int64_t> a, b, c;
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  std::ios::sync_with_stdio(false);
  cin.tie(0);
  int64_t n;
  cin >> n;
  a.reserve(n), b.reserve(n), c.reserve(n);
  for (int64_t i = 0; i < n; i++) {
    int64_t x, y, z;
    cin >> x >> y >> z;
    a.eb(x), b.eb(y), c.eb(z);
  }
  dp[0][0] = a[0];
  dp[0][1] = b[0];
  dp[0][2] = c[0];
  for (int64_t i = 1; i < n; i++) {
    dp[i][0] = max(dp[i - 1][1], dp[i - 1][2]) + a[i];
    dp[i][1] = max(dp[i - 1][2], dp[i - 1][0]) + b[i];
    dp[i][2] = max(dp[i - 1][1], dp[i - 1][0]) + c[i];
  }
  cout << max(dp[n - 1][0], max(dp[n - 1][1], dp[n - 1][2]));
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define eb emplace_back
#define pb push_back
#define pii pair<int64_t, int64_t>
#define mp make_pair
#define whole(v) begin(v), end(v)
#define print64_t(c, n)                                                        \
  for (int64_t i = 0; i < n; i++)                                              \
    cout << c[i] << " ";
#define MOD 1000000007
#define INF 0x3f3f3f3f
int64_t dp[100005][3];
vector<int64_t> a, b, c;
int main() {

  std::ios::sync_with_stdio(false);
  cin.tie(0);
  int64_t n;
  cin >> n;
  a.reserve(n), b.reserve(n), c.reserve(n);
  for (int64_t i = 0; i < n; i++) {
    int64_t x, y, z;
    cin >> x >> y >> z;
    a.eb(x), b.eb(y), c.eb(z);
  }
  dp[0][0] = a[0];
  dp[0][1] = b[0];
  dp[0][2] = c[0];
  for (int64_t i = 1; i < n; i++) {
    dp[i][0] = max(dp[i - 1][1], dp[i - 1][2]) + a[i];
    dp[i][1] = max(dp[i - 1][2], dp[i - 1][0]) + b[i];
    dp[i][2] = max(dp[i - 1][1], dp[i - 1][0]) + c[i];
  }
  cout << max(dp[n - 1][0], max(dp[n - 1][1], dp[n - 1][2]));
  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):

// Author Name - Mohammad Shehar Yaar Tausif
#include <bits/stdc++.h>
using namespace std;

typedef long long int lld;

int main() {
  long arr[1000][3], n, i, j, DP[1000][3];
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> arr[i][0] >> arr[i][1] >> arr[i][2];
  }
  DP[0][0] = arr[0][0];
  DP[0][1] = arr[0][1];
  DP[0][2] = arr[0][2];

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


Fixed submission:

// Author Name - Mohammad Shehar Yaar Tausif
#include <bits/stdc++.h>
using namespace std;

typedef long long int lld;

int main() {
  long arr[100000][3], n, i, j, DP[100000][3];
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> arr[i][0] >> arr[i][1] >> arr[i][2];
  }
  DP[0][0] = arr[0][0];
  DP[0][1] = arr[0][1];
  DP[0][2] = arr[0][2];

  for (i = 1; i < n; i++) {
    DP[i][0] = max(arr[i][0] + DP[i - 1][1], arr[i][0] + DP[i - 1][2]);
    DP[i][1] = max(arr[i][1] + DP[i - 1][0], arr[i][1] + DP[i - 1][2]);
    DP[i][2] = max(arr[i][2] + DP[i - 1][1], arr[i][2] + DP[i - 1][0]);
  }
  cout << max(DP[n - 1][0], max(DP[n - 1][1], DP[n - 1][2])) << endl;
  return 0;
}
Problem p03162 (C++)

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

#define vi vector<int>
#define vii vector<vi>
#define pi pair<int, int>
#define vpi vector<pi>
#define S second
#define F first
#define pb push_back
#define inf 1000000
#define Test                                                                   \
  int t, i = 0;                                                                \
  cin >> t;                                                                    \
  while (i < t)
#define MAXN 100005
#define M 10005

signed main() {
  int a[M], b[M], c[M];
  Test {
    cin >> a[i] >> b[i] >> c[i];
    i += 1;
  }
  for (i = 1; i < t; i++) {
    a[i] = max(a[i] + b[i - 1], a[i] + c[i - 1]);
    b[i] = max(a[i - 1] + b[i], c[i - 1] + b[i]);
    c[i] = max(a[i - 1] + c[i], b[i - 1] + c[i]);
  }
  cout << max({a[t - 1], b[t - 1], c[t - 1]}) << "\n";
}

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

#define vi vector<int>
#define vii vector<vi>
#define pi pair<int, int>
#define vpi vector<pi>
#define S second
#define F first
#define pb push_back
#define inf 1000000
#define Test                                                                   \
  int t, i = 0;                                                                \
  cin >> t;                                                                    \
  while (i < t)
#define MAXN 100005
#define M 100005

signed main() {
  int a[M], b[M], c[M];
  Test {
    cin >> a[i] >> b[i] >> c[i];
    i += 1;
  }
  for (i = 1; i < t; i++) {
    a[i] = max(a[i] + b[i - 1], a[i] + c[i - 1]);
    b[i] = max(a[i - 1] + b[i], c[i - 1] + b[i]);
    c[i] = max(a[i - 1] + c[i], b[i - 1] + c[i]);
  }
  cout << max({a[t - 1], b[t - 1], c[t - 1]}) << "\n";
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int w[101], v[101];
long long f[101][10001];
int main() {
  int n, w1, i, j;
  scanf("%d %d", &n, &w1);
  for (i = 1; i <= n; i++)
    scanf("%d %d", &w[i], &v[i]);
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= w1; j++) {
      if (j < w[i])
        f[i][j] = f[i - 1][j];

      else
        f[i][j] = max(f[i - 1][j], f[i - 1][j - w[i]] + v[i]);
    }
  }
  printf("%lld", f[n][w1]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int w[101], v[101];
long long f[101][100001];
int main() {
  int n, w1, i, j;
  scanf("%d %d", &n, &w1);
  for (i = 1; i <= n; i++)
    scanf("%d %d", &w[i], &v[i]);
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= w1; j++) {
      if (j < w[i])
        f[i][j] = f[i - 1][j];

      else
        f[i][j] = max(f[i - 1][j], f[i - 1][j - w[i]] + v[i]);
    }
  }
  printf("%lld", f[n][w1]);
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <cmath>
#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

int main() {
  int n;
  cin >> n;
  int W;
  cin >> W;
  vector<int> w(n);
  vector<ll> v(n);
  rep(i, n) cin >> w[i] >> v[i];
  // solve
  vector<vector<ll>> dp(n + 100, vector<ll>(1e6));
  rep(i, n) {
    for (int j = 0; j <= W; j++) {
      if (j + w[i] <= W) {
        dp[i + 1][j + w[i]] = max(dp[i + 1][j + w[i]], dp[i][j] + v[i]);
      }
      dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
    }
  }
  cout << dp[n][W] << endl;
}


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

int main() {
  int n;
  cin >> n;
  int W;
  cin >> W;
  vector<int> w(n);
  vector<ll> v(n);
  rep(i, n) cin >> w[i] >> v[i];
  // solve
  vector<vector<ll>> dp(1000, vector<ll>(1e5 + 10));
  rep(i, n) {
    for (int j = 0; j <= W; j++) {
      if (j + w[i] <= W) {
        dp[i + 1][j + w[i]] = max(dp[i + 1][j + w[i]], dp[i][j] + v[i]);
      }
      dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
    }
  }
  cout << dp[n][W] << endl;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
using namespace std;
const int MAXN = 105;
typedef long long ll;
ll v[MAXN], w[MAXN];
ll dp[MAXN] = {0};
int main() {
  int N, M;
  ll maxx = 0;
  cin >> N >> M;
  for (int i = 0; i < N; ++i) {
    cin >> v[i] >> w[i];
  }
  for (int i = 0; i < N; ++i) {
    for (int j = M; j >= v[i]; --j) {
      dp[j] = max(dp[j], dp[j - v[i]] + w[i]);
      maxx = max(maxx, dp[j]);
    }
    //		for(int j=0;j<=M;++j)
    //			cout<<dp[j]<<" ";
    //		cout<<endl;
  }
  cout << maxx << endl;
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
using namespace std;
const int MAXN = 1e5 + 5;
typedef long long ll;
ll v[MAXN], w[MAXN];
ll dp[MAXN] = {0};
int main() {
  int N, M;
  ll maxx = 0;
  cin >> N >> M;
  for (int i = 0; i < N; ++i) {
    cin >> v[i] >> w[i];
  }
  for (int i = 0; i < N; ++i) {
    for (int j = M; j >= v[i]; --j) {
      dp[j] = max(dp[j], dp[j - v[i]] + w[i]);
      maxx = max(maxx, dp[j]);
    }
    //		for(int j=0;j<=M;++j)
    //			cout<<dp[j]<<" ";
    //		cout<<endl;
  }
  cout << maxx << endl;
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <iostream>
#include <unistd.h>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

int n, W;

// DPtable
void show(vector<vector<ll>> &dp, int i) {
  printf("\n");
  printf("   |");
  rep(i, W + 1) printf("%3d", i);
  printf("\n");
  printf("---+");
  rep(i, 10) printf("---");
  printf("\n");
  rep(i, n + 1) {
    printf("%3d|", i);
    rep(j, W + 1) { printf("%3lld", dp[i][j]); }
    printf("\n");
  }
  sleep(i);
}

int main() {
  cin >> n;
  cin >> W;
  vector<int> w(n);
  vector<ll> v(n);
  rep(i, n) cin >> w[i] >> v[i];
  // solve
  vector<vector<ll>> dp(1000, vector<ll>(1e5 + 10));
  rep(i, n) {
    for (int j = 0; j <= W; j++) {
      if (w[i] <= j) {
        dp[i + 1][j] = max(dp[i][j - w[i]] + v[i], dp[i][j]);
      } else {
        dp[i + 1][j] = dp[i][j];
      }
      show(dp, 1);
    }
  }
  cout << dp[n][W] << endl;
}


Fixed submission:
#include <cmath>
#include <iostream>
#include <unistd.h>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

int n, W;

// DPtable
void show(vector<vector<ll>> &dp, int i) {
  printf("\n");
  printf("   |");
  rep(i, W + 1) printf("%3d", i);
  printf("\n");
  printf("---+");
  rep(i, 10) printf("---");
  printf("\n");
  rep(i, n + 1) {
    printf("%3d|", i);
    rep(j, W + 1) { printf("%3lld", dp[i][j]); }
    printf("\n");
  }
  sleep(i);
}

int main() {
  cin >> n;
  cin >> W;
  vector<int> w(n);
  vector<ll> v(n);
  rep(i, n) cin >> w[i] >> v[i];
  // solve
  vector<vector<ll>> dp(1000, vector<ll>(1e5 + 10));
  rep(i, n) {
    for (int j = 0; j <= W; j++) {
      if (w[i] <= j) {
        dp[i + 1][j] = max(dp[i][j - w[i]] + v[i], dp[i][j]);
      } else {
        dp[i + 1][j] = dp[i][j];
      }
      // show(dp, 1);
    }
  }
  cout << dp[n][W] << endl;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long n, W, dp[201][200001];
pair<long long, long long> v[201];
int main() {
  cin >> n >> W;
  for (int i = 1; i <= n; i++)
    cin >> v[i].first >> v[i].second;
  sort(v + 1, v + 1 + n);
  for (int i = 1; i <= W; i++)
    dp[i][0] = dp[0][i] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= W; j++) {
      if (j < v[i].first)
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      else
        dp[i][j] = max(max(dp[i - 1][j], dp[i][j - 1]),
                       dp[i - 1][j - v[i].first] + v[i].second);
    }
  }
  cout << dp[n][W];
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long n, W, dp[201][200001];
pair<long long, long long> v[201];
int main() {
  cin >> n >> W;
  for (int i = 1; i <= n; i++)
    cin >> v[i].first >> v[i].second;
  sort(v + 1, v + 1 + n);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= W; j++) {
      if (j < v[i].first)
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      else
        dp[i][j] = max(max(dp[i - 1][j], dp[i][j - 1]),
                       dp[i - 1][j - v[i].first] + v[i].second);
    }
  }
  cout << dp[n][W];
}
Problem p03163 (C++)

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

using namespace std;
long long int N, W;
long long int v[108], w[108];

long long int dp[108][10800];

// 大きいほうを返す関数
long long int mx(long long int a, long long int b) {
  if (a > b) {
    return a;
  } else {
    return b;
  }
}

int main() {
  cin >> N >> W;
  for (long long int i = 0; i < N; i++) {
    cin >> w[i] >> v[i];
  }

  for (long long int i = 0; i < N; i++) {
    for (long long int j = 0; j <= W; j++) {
      if (i == 0) {
        if (j >= w[0]) {
          dp[i][j] = v[i];
        } else {
          dp[i][j] = 0;
        }
      } else if (j - w[i] < 0) {
        dp[i][j] = dp[i - 1][j];
      } else {
        dp[i][j] = mx(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]);
      }
    }
  }
  cout << dp[N - 1][W] << endl;
}

Fixed submission:
#include <iostream>

using namespace std;
long long int N, W;
long long int v[108], w[108];

long long int dp[108][108000];

// 大きいほうを返す関数
long long int mx(long long int a, long long int b) {
  if (a > b) {
    return a;
  } else {
    return b;
  }
}

int main() {
  cin >> N >> W;
  for (long long int i = 0; i < N; i++) {
    cin >> w[i] >> v[i];
  }

  for (long long int i = 0; i < N; i++) {
    for (long long int j = 0; j <= W; j++) {
      if (i == 0) {
        if (j >= w[0]) {
          dp[i][j] = v[i];
        } else {
          dp[i][j] = 0;
        }
      } else if (j - w[i] < 0) {
        dp[i][j] = dp[i - 1][j];
      } else {
        dp[i][j] = mx(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]);
      }
    }
  }
  cout << dp[N - 1][W] << endl;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string.h>

using namespace std;
// Sol 3A
// forward dp
// enumerating items from beginning (z = 0..N)
// inner loop is looping
// "weights >= the current item weight"
// in descending order
long long dp[10005][105];
long long solve(int N, int W, int *v, int *w) {
  // items are 0 indexed, 0..N-1'th items
  // dp[a][b] = solution for a knapsack of weight a,
  // only can use items until and including b'th item
  // long long dp[W + 1][N + 1];
  // memset(dp, 0, sizeof(long long) * (W + 1) * (N + 1));

  for (int z = 0; z < N; ++z) {
    for (int q = W - w[z]; q >= 0; --q) {
      dp[q + w[z]][z] = max(dp[q + w[z]][z], v[z] + dp[q][z]);
    }
    for (int q = 0; q < W + 1; ++q) {
      dp[q][z + 1] = dp[q][z];
    }
  }

  // for (int q = 0; q < W + 1; ++q) {
  //   for (int z = 0; z < N; ++z) {
  //     cout << dp[q][z] << ' ';
  //   }
  //   cout << '\n';
  // }
  return dp[W][N - 1];
}

int main() {
  int N, W;
  cin >> N >> W;
  int v[N], w[N];
  for (int q = 0; q < N; ++q)
    cin >> w[q] >> v[q];

  long long ans = solve(N, W, v, w);
  cout << ans << '\n';
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <string.h>

using namespace std;
// Sol 3A
// forward dp
// enumerating items from beginning (z = 0..N)
// inner loop is looping
// "weights >= the current item weight"
// in descending order
long long dp[100005][105];
long long solve(int N, int W, int *v, int *w) {
  // items are 0 indexed, 0..N-1'th items
  // dp[a][b] = solution for a knapsack of weight a,
  // only can use items until and including b'th item
  // long long dp[W + 1][N + 1];
  // memset(dp, 0, sizeof(long long) * (W + 1) * (N + 1));

  for (int z = 0; z < N; ++z) {
    for (int q = W - w[z]; q >= 0; --q) {
      dp[q + w[z]][z] = max(dp[q + w[z]][z], v[z] + dp[q][z]);
    }
    for (int q = 0; q < W + 1; ++q) {
      dp[q][z + 1] = dp[q][z];
    }
  }

  // for (int q = 0; q < W + 1; ++q) {
  //   for (int z = 0; z < N; ++z) {
  //     cout << dp[q][z] << ' ';
  //   }
  //   cout << '\n';
  // }
  return dp[W][N - 1];
}

int main() {
  int N, W;
  cin >> N >> W;
  int v[N], w[N];
  for (int q = 0; q < N; ++q)
    cin >> w[q] >> v[q];

  long long ans = solve(N, W, v, w);
  cout << ans << '\n';
}
Problem p03163 (C++)

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

using namespace std;

typedef long long ll;
int n, W;
vector<ll> w, v;

ll dp[100][(int)1e5 + 1];

int main() {
  int t = 1;
  // cin >> t ;

  while (t--) {
    for (int i = 0; i < 100; i++)
      for (int j = 0; j <= 1e5; j++)
        dp[i][j] = 0;
    cin >> n >> W;
    w.resize(n);
    v.resize(n);
    for (int i = 0; i < n; i++)
      cin >> w[i] >> v[i];

    // cout<<fun(0,W) << endl ;

    for (int i = n - 1; i >= 0; i--)
      for (int c = 0; c <= W;
           c++) // ? best value in pathars i, i+1, ... n with capacity c.
      {
        if (i + 1 < n)
          dp[i][c] = dp[i + 1][c];

        if (c >= w[i]) {
          dp[i][c] = max(dp[i][c], v[i] + dp[i + 1][c - w[i]]);
        }
      }

    cout << dp[0][W] << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
int n, W;
vector<ll> w, v;

ll dp[100][(int)1e5 + 1];

int main() {
  int t = 1;
  // cin >> t ;

  while (t--) {
    for (int i = 0; i < 100; i++)
      for (int j = 0; j <= 1e5; j++)
        dp[i][j] = 0;
    cin >> n >> W;
    w.resize(n);
    v.resize(n);
    for (int i = 0; i < n; i++)
      cin >> w[i] >> v[i];

    // cout<<fun(0,W) << endl ;

    for (int i = n - 1; i >= 0; i--)
      for (int c = 0; c <= W;
           c++) // ? best value in pathars i, i+1, ... n with capacity c.
      {
        if (i + 1 < n)
          dp[i][c] = dp[i + 1][c];

        if (c >= w[i]) {
          if (i + 1 < n)
            dp[i][c] = max(dp[i][c], v[i] + dp[i + 1][c - w[i]]);
          else
            dp[i][c] = v[i];
        }
      }

    cout << dp[0][W] << endl;
  }
  return 0;
}
Problem p03163 (C++)

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

long long dp[100][100005], vi;

int main() {
  int n, w, wi;
  cin >> n >> w;
  ;
  for (int i = 0; i < w; i++)
    dp[0][i] = 0;
  for (int i = 1; i <= n; i++) {
    cin >> wi >> vi;
    for (int p = 0; p <= w; p++) {
      if (wi <= p)
        dp[i][p] = max(dp[i - 1][p], vi + dp[i - 1][p - wi]);
      else
        dp[i][p] = dp[i - 1][p];
    }
  }
  cout << dp[n][w];

  return 0;
}

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

long long dp[105][100005], vi;

int main() {
  int n, w, wi;
  cin >> n >> w;
  ;
  for (int i = 0; i < w; i++)
    dp[0][i] = 0;
  for (int i = 1; i <= n; i++) {
    cin >> wi >> vi;
    for (int p = 0; p <= w; p++) {
      if (wi <= p)
        dp[i][p] = max(dp[i - 1][p], vi + dp[i - 1][p - wi]);
      else
        dp[i][p] = dp[i - 1][p];
    }
  }
  cout << dp[n][w];

  return 0;
}