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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define mx 105
int n, W;
int weight[mx], value[mx];

#define ll long long

ll dp[mx][mx];
ll func(int i, int w) {
  if (i >= n + 1)
    return 0;
  if (dp[i][w] != -1)
    return dp[i][w];

  ll p1 = 0, p2 = 0;
  if (w + weight[i] <= W) {
    p1 = value[i] + func(i + 1, w + weight[i]);
  }
  p2 = func(i + 1, w);

  dp[i][w] = max(p1, p2);
  return dp[i][w];
}
int main() {
  memset(dp, -1, sizeof dp);
  scanf("%d %d\n", &n, &W);

  for (int i = 1; i <= n; i++)
    scanf("%d %d", &weight[i], &value[i]);

  ll ans = func(1, 0);
  printf("%lld\n", ans);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define mx 105
int n, W;
int weight[mx], value[mx];

#define ll long long

ll dp[mx][100000 + mx];
ll func(int i, int w) {
  if (i >= n + 1)
    return 0;
  if (dp[i][w] != -1)
    return dp[i][w];

  ll p1 = 0, p2 = 0;
  if (w + weight[i] <= W) {
    p1 = value[i] + func(i + 1, w + weight[i]);
  }
  p2 = func(i + 1, w);

  dp[i][w] = max(p1, p2);
  return dp[i][w];
}
int main() {
  memset(dp, -1, sizeof dp);
  scanf("%d %d\n", &n, &W);

  for (int i = 1; i <= n; i++)
    scanf("%d %d", &weight[i], &value[i]);

  ll ans = func(1, 0);
  printf("%lld\n", ans);
}
Problem p03163 (C++)

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

long long v[MAXN], dp[MAXN][100010];
int p[MAXN], ct = 1;
map<long long, int> m;

int hsh(long long p) {
  if (m[p] == 0)
    return m[p] = ct++;
  else
    return m[p];
}

long long res(int i, int n, int w) {
  if (i == n)
    return 0;
  if (dp[i][hsh(w)] != -1)
    return dp[i][hsh(w)];
  long long n_pega = res(i + 1, n, w), pega = 0;
  if (w >= p[i]) {
    pega = v[i] + res(i + 1, n, w - p[i]);
  }
  return dp[i][hsh(w)] = max(pega, n_pega);
}

int main() {
  int n, w;
  cin >> n >> w;
  memset(dp, -1, sizeof(dp));
  for (int i = 0; i < n; ++i) {
    cin >> p[i] >> v[i];
  }
  cout << res(0, n, w) << endl;
  // for(auto i : m){
  // 	cout << i.first << ' ' << i.second << endl;
  // }
  // cout << endl;
}

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

long long v[MAXN], dp[MAXN][100010];
int p[MAXN], ct = 1;
unordered_map<long long, int> m;

int hsh(long long p) {
  if (m[p] == 0)
    return m[p] = ct++;
  else
    return m[p];
}

long long res(int i, int n, int w) {
  if (i == n)
    return 0;
  if (dp[i][hsh(w)] != -1)
    return dp[i][hsh(w)];
  long long n_pega = res(i + 1, n, w), pega = 0;
  if (w >= p[i]) {
    pega = v[i] + res(i + 1, n, w - p[i]);
  }
  return dp[i][hsh(w)] = max(pega, n_pega);
}

int main() {
  int n, w;
  cin >> n >> w;
  memset(dp, -1, sizeof(dp));
  for (int i = 0; i < n; ++i) {
    cin >> p[i] >> v[i];
  }
  cout << res(0, n, w) << endl;
  // for(auto i : m){
  // 	cout << i.first << ' ' << i.second << endl;
  // }
  // cout << endl;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
#define MAX_N 100
#define MAX_W 100000
int N, W;
long long dp[MAX_N][MAX_W + 1];

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

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

Fixed submission:
#include <iostream>
using namespace std;
#define MAX_N 100
#define MAX_W 100000
int N, W;
long long dp[MAX_N + 1][MAX_W + 1];

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

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

Buggy submission (status: Runtime Error):
#pragma GCC optimize("O3")
#pragma GCC target("avx")
// #include<bits/stdc++.h>
#include <algorithm>
#include <cstdio>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

const int cm = 1 << 11;
char cn[cm], *ci = cn, ct;
inline int getint() {
  int A = 0;
  while ((ct = *ci++) >= '0')
    A = A * 10 + ct - '0';
  return A;
}

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

  fread(cn, 1, cm, stdin);
  int N = getint(), W = getint();

  ll dp[100001] = {};
  rep(i, N) {
    int w = getint();
    int v = getint();
    for (int j = 0; j <= W - w; j--) {
      chmax(dp[j], dp[j + w] + v);
    }
  }

  printf("%lld", dp[0]);

  Would you please return 0;
}

Fixed submission:
#pragma GCC optimize("O3")
#pragma GCC target("avx")
// #include<bits/stdc++.h>
#include <algorithm>
#include <cstdio>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

const int cm = 1 << 11;
char cn[cm], *ci = cn, ct;
inline int getint() {
  int A = 0;
  while ((ct = *ci++) >= '0')
    A = A * 10 + ct - '0';
  return A;
}

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

  fread(cn, 1, cm, stdin);
  int N = getint(), W = getint();

  ll dp[100001] = {};
  rep(i, N) {
    int w = getint();
    int v = getint();
    rep(j, W - w + 1) { chmax(dp[j], dp[j + w] + v); }
  }

  printf("%lld", dp[0]);

  Would you please return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <fstream>
#include <iostream>
#include <math.h>
#include <set>
#include <string>
#include <vector>
using namespace ::std;
#define int long long
vector<int> v;
signed main() {
  int n, k;

  cin >> n >> k;
  v.resize(k + 1, -1);
  v[0] = 0;
  int f = 0;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    int w;
    int q;
    cin >> w >> q;
    for (int j = f; j >= 0; j--) {
      if (v[j] == -1)
        continue;
      if (j + w > q)
        continue;
      v[w + j] = max(v[w + j], v[j] + q);
      f = max(f, w + j);
      ans = max(ans, v[w + j]);
    }
  }
  cout << ans;
}
//   aaaddda

Fixed submission:
#include <algorithm>
#include <fstream>
#include <iostream>
#include <math.h>
#include <set>
#include <string>
#include <vector>
using namespace ::std;
#define int long long
vector<int> v;
signed main() {
  int n, k;

  cin >> n >> k;
  v.resize(k + 1, -1);
  v[0] = 0;
  int f = 0;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    int w;
    int q;
    cin >> w >> q;
    for (int j = f; j >= 0; j--) {
      if (v[j] == -1)
        continue;
      if (j + w > k)
        continue;
      v[w + j] = max(v[w + j], v[j] + q);
      f = max(f, w + j);
      ans = max(ans, v[w + j]);
    }
  }
  cout << ans;
}
//   aaaddda
Problem p03163 (C++)

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

long long int n, W, i, j, weight[1005], val[1005], mat[100][100005];

int main() {
  cin >> n >> W;
  for (i = 0; i < n; i++) {
    cin >> weight[i] >> val[i];
  }
  for (i = 0; i <= W; i++)
    mat[0][i] = 0;
  for (i = 0; i <= n; i++)
    mat[i][0] = 0;

  for (i = 1; i <= n; i++) {
    for (j = 1; j <= W; j++) {
      if (weight[i - 1] <= j) {
        mat[i][j] =
            max(mat[i - 1][j], val[i - 1] + mat[i - 1][j - weight[i - 1]]);
      } else {
        mat[i][j] = mat[i - 1][j];
      }
    }
  }
  cout << mat[n][W] << endl;
}

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

long long int n, W, i, j, weight[1005], val[1005], mat[105][100005];

int main() {
  cin >> n >> W;
  for (i = 0; i < n; i++) {
    cin >> weight[i] >> val[i];
  }
  for (i = 0; i <= W; i++)
    mat[0][i] = 0;
  for (i = 0; i <= n; i++)
    mat[i][0] = 0;

  for (i = 1; i <= n; i++) {
    for (j = 1; j <= W; j++) {
      if (weight[i - 1] <= j) {
        mat[i][j] =
            max(mat[i - 1][j], val[i - 1] + mat[i - 1][j - weight[i - 1]]);
      } else {
        mat[i][j] = mat[i - 1][j];
      }
    }
  }
  cout << mat[n][W] << endl;
}
Problem p03163 (C++)

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

using namespace std;
struct wow {
  int x, y;
} v[105];
long long din[10005], n, val, maxim, j, i;
int main() {
  cin >> n >> val;
  for (i = 1; i <= n; i++) {
    cin >> v[i].x >> v[i].y;
  }
  for (i = 1; i <= val; i++) {
    din[i] = -1;
  }
  for (i = 1; i <= n; i++) {
    for (j = val; j >= v[i].x; j--) {
      if (din[j - v[i].x] != -1) {
        din[j] = max(din[j], din[j - v[i].x] + v[i].y);
      }
    }
  }
  for (j = val; j >= 1; j--) {
    maxim = max(maxim, din[j]);
  }
  cout << maxim;
  return 0;
}


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

using namespace std;
struct wow {
  int x, y;
} v[105];
long long din[100005], n, val, maxim, j, i;
int main() {
  cin >> n >> val;
  for (i = 1; i <= n; i++) {
    cin >> v[i].x >> v[i].y;
  }
  for (i = 1; i <= val; i++) {
    din[i] = -1;
  }
  for (i = 1; i <= n; i++) {
    for (j = val; j >= v[i].x; j--) {
      if (din[j - v[i].x] != -1) {
        din[j] = max(din[j], din[j - v[i].x] + v[i].y);
      }
    }
  }
  for (j = val; j >= 1; j--) {
    maxim = max(maxim, din[j]);
  }
  cout << maxim;
  return 0;
}
Problem p03163 (C++)

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

using namespace std;

int w[110], v[110];
long long int dp[110][100345];

int N, W;
int main() {
  cin >> N >> W;
  for (int i = 0; i < N; i++)
    for (int j = 0; j <= W; j++)
      dp[i][j] = -1;
  for (int i = 0; i < N; i++)
    cin >> w[i] >> v[i];
  for (int i = 0; i < N; i++)
    dp[i][0] = 0;
  for (int i = 0; i < N; i++) {
    for (int we = 0; we <= W; we++) {
      if (dp[i][we - w[i]] >= 0 && we >= w[i]) {
        dp[i + 1][we] = max(dp[i][we - w[i]] + v[i], dp[i][we]);
      } else
        dp[i + 1][we] = dp[i][we];
    }
  }
  long long int ans = 0;
  for (int i = 0; i <= W; i++)
    ans = max(ans, dp[N][i]);
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

int w[110], v[110];
long long int dp[110][100345];

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define MOD 1000000007
#define len(x) x.size()
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define all(v) v.begin(), v.end()
#define f(i, a, n) for (int i = a; i < n; i++)
using namespace std;
typedef vector<int> vi;
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
typedef vector<vll> vvll;
typedef vector<string> vs;

int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  // int t; cin>>t; while(t--) {
  int n, w;
  cin >> n >> w;
  vll dp(w + 1, 0);
  f(items, 0, n) {
    int weight, value;
    cin >> weight >> value;
    for (int weight_already = w - weight; weight_already >= 0; weight_already--)
      dp[weight_already + weight] =
          max(dp[weight_already + weight], dp[weight_already] + value);
  }
  ll ans = 0;
  f(i, 0, w + 1) ans = max(ans, dp[i]);
  cout << ans;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define MOD 1000000007
#define len(x) x.size()
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define all(v) v.begin(), v.end()
#define f(i, a, n) for (int i = a; i < n; i++)
using namespace std;
typedef vector<int> vi;
typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
typedef vector<vll> vvll;
typedef vector<string> vs;

int main() {

  /*#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin) ;
  freopen("output.txt", "w", stdout) ;
  #endif*/
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  // int t; cin>>t; while(t--) {
  int n, w;
  cin >> n >> w;
  vll dp(w + 1, 0);
  f(items, 0, n) {
    int weight, value;
    cin >> weight >> value;
    for (int weight_already = w - weight; weight_already >= 0; weight_already--)
      dp[weight_already + weight] =
          max(dp[weight_already + weight], dp[weight_already] + value);
  }
  ll ans = 0;
  f(i, 0, w + 1) ans = max(ans, dp[i]);
  cout << ans;

  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
const int maxn = 13010;
typedef long long ll;
ll n, W, w[maxn], v[maxn], f[maxn];
int main() {
  cin >> n >> W;
  for (int i = 1; i <= n; i++)
    cin >> w[i] >> v[i];
  for (int i = 1; i <= n; i++)
    for (ll l = W; l >= w[i]; l--)
      if (f[l - w[i]] + v[i] > f[l])
        f[l] = f[l - w[i]] + v[i];
  cout << f[W];
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
const int maxn = 1e5 + 5;
typedef long long ll;
ll n, W, w[maxn], v[maxn], f[maxn];
int main() {
  cin >> n >> W;
  for (int i = 1; i <= n; i++)
    cin >> w[i] >> v[i];
  for (int i = 1; i <= n; i++)
    for (ll l = W; l >= w[i]; l--)
      if (f[l - w[i]] + v[i] > f[l])
        f[l] = f[l - w[i]] + v[i];
  cout << f[W];
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll dp[100][100002];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll n, w;
  cin >> n >> w;
  for (int i = 1; i <= w; i++)
    dp[0][i] = -1ll;
  dp[0][0] = 0ll;
  for (ll i = 1; i <= n; i++) {
    ll hz, zh;
    cin >> hz >> zh;
    for (ll j = 1; j <= w; j++) {
      dp[i][j] = dp[i - 1][j];
      if (j - hz >= 0)
        dp[i][j] = max(dp[i][j], dp[i - 1][j - hz] + zh);
    }
  }
  ll ax = 0ll;
  for (ll i = 1; i <= w; i++)
    ax = max(dp[n][i], ax);
  cout << ax;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll dp[102][100002];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll n, w;
  cin >> n >> w;
  for (int i = 1; i <= w; i++)
    dp[0][i] = -1ll;
  dp[0][0] = 0ll;
  for (ll i = 1; i <= n; i++) {
    ll hz, zh;
    cin >> hz >> zh;
    for (ll j = 1; j <= w; j++) {
      dp[i][j] = dp[i - 1][j];
      if (j - hz >= 0)
        dp[i][j] = max(dp[i][j], dp[i - 1][j - hz] + zh);
    }
  }
  ll ax = 0ll;
  for (ll i = 1; i <= w; i++)
    ax = max(dp[n][i], ax);
  cout << ax;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
vector<int> weight;
vector<int> value;
long long ans[101][100000];
int main() {
  int N, W, w, v;
  cin >> N >> W;
  weight.resize(N + 1, 0);
  value.resize(N + 1, 0);
  for (int i = 0; i < N; ++i) {
    cin >> w >> v;
    weight[i] = w;
    value[i] = v;
  }
  for (int i = 0; i <= N; ++i) {
    for (int j = 0; j <= W; ++j) {
      if (i == 0 || j == 0) {
        ans[i][j] = 0;
      } else if (weight[i - 1] <= j) {
        ans[i][j] =
            max(value[i - 1] + ans[i - 1][j - weight[i - 1]], ans[i - 1][j]);
      } else {
        ans[i][j] = ans[i - 1][j];
      }
    }
  }
  cout << ans[N][W] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
vector<int> weight;
vector<int> value;
long long ans[101][100001];
int main() {
  int N, W, w, v;
  cin >> N >> W;
  weight.resize(N + 1, 0);
  value.resize(N + 1, 0);
  for (int i = 0; i < N; ++i) {
    cin >> w >> v;
    weight[i] = w;
    value[i] = v;
  }
  for (int i = 0; i <= N; ++i) {
    for (int j = 0; j <= W; ++j) {
      if (i == 0 || j == 0) {
        ans[i][j] = 0;
      } else if (weight[i - 1] <= j) {
        ans[i][j] =
            max(value[i - 1] + ans[i - 1][j - weight[i - 1]], ans[i - 1][j]);
      } else {
        ans[i][j] = ans[i - 1][j];
      }
    }
  }
  cout << ans[N][W] << endl;
  return 0;
}
Problem p03163 (C++)

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

#define endl '\n'

using namespace std;

const int MAXN = 103, MAXW = 1e5 + 3;
long long cost[MAXN], dp[MAXN][MAXN];
int weight[MAXN];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, w;
  cin >> n >> w;
  for (int i = 1; i <= n; ++i) {
    cin >> weight[i] >> cost[i];
  }
  for (int i = 1; i <= n; ++i) {
    for (int i2 = 1; i2 <= w; ++i2) {
      if (weight[i] > i2) {
        dp[i][i2] = dp[i - 1][i2];
      } else {
        dp[i][i2] = max(cost[i] + dp[i - 1][i2 - weight[i]], dp[i - 1][i2]);
      }
    }
  }
  cout << dp[n][w] << endl;
  return 0;
}

Fixed submission:
#include <iostream>

#define endl '\n'

using namespace std;

const int MAXN = 103, MAXW = 1e5 + 3;
long long cost[MAXN], dp[MAXN][MAXW];
int weight[MAXN];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, w;
  cin >> n >> w;
  for (int i = 1; i <= n; ++i) {
    cin >> weight[i] >> cost[i];
  }
  for (int i = 1; i <= n; ++i) {
    for (int i2 = 1; i2 <= w; ++i2) {
      if (weight[i] > i2) {
        dp[i][i2] = dp[i - 1][i2];
      } else {
        dp[i][i2] = max(cost[i] + dp[i - 1][i2 - weight[i]], dp[i - 1][i2]);
      }
    }
  }
  cout << dp[n][w] << endl;
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long unsigned int ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define ALL(x) (x).begin(), (x).end()
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

ll dp[101][10001] = {};
int main() {
  int N, W;
  cin >> N;
  cin >> W;
  int w[N], v[N];
  REP(i, N) {
    cin >> w[i];
    cin >> v[i];
  }
  REPS(i, N) {
    REP(j, W + 1) {
      if (j - w[i - 1] >= 0) {
        chmax(dp[i][j], dp[i - 1][j - w[i - 1]] + v[i - 1]);
      }
      chmax(dp[i][j], dp[i - 1][j]);
    }
  }
  ll res = 0;
  REP(i, W + 1) { chmax(res, dp[N][i]); }
  cout << res;
}


Fixed submission:
#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long unsigned int ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define ALL(x) (x).begin(), (x).end()
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

ll dp[101][100001] = {};
int main() {
  int N, W;
  cin >> N;
  cin >> W;
  int w[N], v[N];
  REP(i, N) {
    cin >> w[i];
    cin >> v[i];
  }
  REPS(i, N) {
    REP(j, W + 1) {
      if (j - w[i - 1] >= 0) {
        chmax(dp[i][j], dp[i - 1][j - w[i - 1]] + v[i - 1]);
      }
      chmax(dp[i][j], dp[i - 1][j]);
    }
  }
  ll res = 0;
  REP(i, W + 1) { chmax(res, dp[N][i]); }
  cout << res;
}
Problem p03163 (C++)

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

#define MAX_W 100010
#define MAX 100

long long dp[MAX][MAX_W];
long long weight[MAX], value[MAX];

int main() {
  for (int i = 0; i < MAX; i++) {
    for (int j = 0; j < MAX_W; j++) {
      dp[i][j] = 0;
    }
  }
  int n, w;
  cin >> n >> w;
  for (int i = 0; i < n; i++) {
    cin >> weight[i] >> value[i];
  }

  for (int i = 0; i < n; i++) {
    for (int W = 0; W <= w; W++) {
      if (W - weight[i] >= 0) {
        dp[i + 1][W] = max(dp[i + 1][W], dp[i][W - weight[i]] + value[i]);
      }

      dp[i + 1][W] = max(dp[i + 1][W], dp[i][W]);
    }
  }

  // for(int i=0;i<=n;i++){
  //	for(int j=0;j<=w;j++)printf("%3d ",dp[i][j]);
  //	cout<<endl;
  // }

  cout << dp[n][w] << endl;

  return 0;
}


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

#define MAX_W 100010
#define MAX 110

long long dp[MAX][MAX_W];
long long weight[MAX], value[MAX];

int main() {
  for (int i = 0; i < MAX; i++) {
    for (int j = 0; j < MAX_W; j++) {
      dp[i][j] = 0;
    }
  }
  int n, w;
  cin >> n >> w;
  for (int i = 0; i < n; i++) {
    cin >> weight[i] >> value[i];
  }

  for (int i = 0; i < n; i++) {
    for (int W = 0; W <= w; W++) {
      if (W - weight[i] >= 0) {
        dp[i + 1][W] = max(dp[i + 1][W], dp[i][W - weight[i]] + value[i]);
      }

      dp[i + 1][W] = max(dp[i + 1][W], dp[i][W]);
    }
  }

  // for(int i=0;i<=n;i++){
  //	for(int j=0;j<=w;j++)printf("%3d ",dp[i][j]);
  //	cout<<endl;
  // }

  cout << dp[n][w] << endl;

  return 0;
}
Problem p03163 (C++)

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

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;
}

typedef long long ll;
const long long INF = 1LL << 60;

long long dp[10010][110];

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

  vector<int> h(n), v(n);
  for (int i = 0; i < n; i++) {
    cin >> h[i] >> v[i];
  }

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

  for (int i = 1; i <= w; i++) {
    for (int j = 1; j <= n; j++) {
      if (i - h.at(j - 1) >= 0)
        chmax(dp[i][j], (ll)(dp[i - h.at(j - 1)][j - 1] + v.at(j - 1)));
      chmax(dp[i][j], dp[i][j - 1]);
    }
  }

  cout << dp[w][n] << endl;

  return 0;
}

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

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;
}

typedef long long ll;
const long long INF = 1LL << 60;

ll dp[100100][110];

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

  vector<int> h(n), v(n);
  for (int i = 0; i < n; i++) {
    cin >> h[i] >> v[i];
  }

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

  for (int i = 1; i <= w; i++) {
    for (int j = 1; j <= n; j++) {
      if (i - h.at(j - 1) >= 0)
        chmax(dp[i][j], (ll)(dp[i - h.at(j - 1)][j - 1] + v.at(j - 1)));
      chmax(dp[i][j], dp[i][j - 1]);
    }
  }

  cout << dp[w][n] << endl;

  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <stack>
using namespace std;
typedef long long ll;
typedef long double lxd;
#define INF 1000000007
#define mem(dp, a) memset(dp, a, sizeof dp)
#define flash                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define repb(i, a, b) for (ll i = a; i >= b; i--)
#define f(i, n) for (ll i = 0; i < n; i++)
#define fo(i, x, n) for (ll i = x; i <= n; i++)
#define pb(x) push_back(x)
#define tr(c, it) for (((c).begin())it = (c).begin(); it != (c).end(); it++)
#define test                                                                   \
  ll t;                                                                        \
  cin >> t;
#define fls fflush(stdout);
ll fn(ll w[], ll v[], ll sw, ll idx) {
  if (idx < 0)
    return 0;
  if (sw < w[idx])
    return fn(w, v, sw, idx - 1);
  else
    return max(v[idx] + fn(w, v, sw - w[idx], idx - 1), fn(w, v, sw, idx - 1));
}
int main() {
  ll n, sw;
  cin >> n >> sw;
  ll w[n], v[n];
  for (ll i = 0; i < n; i++)
    cin >> w[i] >> v[i];
  ll ans = fn(w, v, sw, n - 1);
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <stack>
using namespace std;
typedef long long ll;
typedef long double lxd;
#define INF 1000000007
#define mem(dp, a) memset(dp, a, sizeof dp)
#define flash                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define repb(i, a, b) for (ll i = a; i >= b; i--)
#define f(i, n) for (ll i = 0; i < n; i++)
#define fo(i, x, n) for (ll i = x; i <= n; i++)
#define pb(x) push_back(x)
#define tr(c, it) for (((c).begin())it = (c).begin(); it != (c).end(); it++)
#define test                                                                   \
  ll t;                                                                        \
  cin >> t;
#define fls fflush(stdout);
ll fn(ll w[], ll v[], ll sw, ll idx) {
  if (idx < 0)
    return 0;
  if (sw < w[idx])
    return fn(w, v, sw, idx - 1);
  else
    return max(v[idx] + fn(w, v, sw - w[idx], idx - 1), fn(w, v, sw, idx - 1));
}
int main() {
  ll n, sw;
  cin >> n >> sw;
  ll w[n], v[n];
  for (ll i = 0; i < n; i++)
    cin >> w[i] >> v[i];
  ll dp[n + 1][sw + 1];
  memset(dp, 0, sizeof(dp));
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= sw; j++) {
      if (w[i - 1] <= j)
        dp[i][j] = max(v[i - 1] + dp[i - 1][j - w[i - 1]], dp[i - 1][j]);
      else
        dp[i][j] = dp[i - 1][j];
    }
  }

  ll mx = dp[n][sw];
  cout << mx << endl;
  // ll ans=fn(w,v,sw,n-1);
  // cout<<ans<<endl;
  /* for(ll i=0;i<=n;i++)
    {
        for(ll j=0;j<=sw;j++)
        {
            cout<<dp[i][j];
        }cout<<endl;
    }*/
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long int
#define mp make_pair
#define S second
#define F first
ll mod = 1e9 + 7;
#define input_from_file freopen("input.txt", "r", stdin);
int main() {
  input_from_file;
  // int t; cin>>t;
  // while(t--){
  ll n, sum;
  cin >> n >> sum;
  vector<vector<ll>> vec(n, vector<ll>(2, 0));
  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j < 2; j++)
      cin >> vec[i][j];
  }
  vector<vector<ll>> dp(n + 1, vector<ll>(sum + 1, 0));
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= sum; j++) {
      if (j < vec[i - 1][0])
        dp[i][j] = dp[i - 1][j];
      else {
        dp[i][j] =
            max(dp[i - 1][j], dp[i - 1][j - vec[i - 1][0]] + vec[i - 1][1]);
      }
    }
  }
  // for(ll i=0;i<=n;i++){
  //   for(ll j=0;j<=sum;j++)  cout<<dp[i][j]<<" ";cout<<endl;}
  cout << dp[n][sum];
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long int
#define mp make_pair
#define S second
#define F first
ll mod = 1e9 + 7;
#define input_from_file freopen("input.txt", "r", stdin);
int main() {
  // input_from_file;
  // int t; cin>>t;
  // while(t--){
  ll n, sum;
  cin >> n >> sum;
  vector<vector<ll>> vec(n, vector<ll>(2, 0));
  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j < 2; j++)
      cin >> vec[i][j];
  }
  vector<vector<ll>> dp(n + 1, vector<ll>(sum + 1, 0));
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= sum; j++) {
      if (j < vec[i - 1][0])
        dp[i][j] = dp[i - 1][j];
      else {
        dp[i][j] =
            max(dp[i - 1][j], dp[i - 1][j - vec[i - 1][0]] + vec[i - 1][1]);
      }
    }
  }
  // for(ll i=0;i<=n;i++){
  //   for(ll j=0;j<=sum;j++)  cout<<dp[i][j]<<" ";cout<<endl;}
  cout << dp[n][sum];
  return 0;
}
Problem p03163 (C++)

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

struct item {
  int v;
  int w;
} a[101];

long long f[10001];

int main() {
  int N, W;
  scanf("%d%d", &N, &W);
  for (int i = 1; i <= N; ++i)
    scanf("%d%d", &a[i].w, &a[i].v);
  for (int i = 1; i <= W; ++i) {
    if (a[1].w <= i)
      f[i] = a[1].v;
  }
  for (int i = 2; i <= N; ++i) {
    for (int j = W; j >= 0; --j) {
      if (j >= a[i].w)
        f[j] = max(f[j], f[j - a[i].w] + a[i].v);
    }
  }
  cout << f[W] << endl;
  return 0;
}

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

struct item {
  int v;
  int w;
} a[101];

long long f[100001];

int main() {
  int N, W;
  scanf("%d%d", &N, &W);
  for (int i = 1; i <= N; ++i)
    scanf("%d%d", &a[i].w, &a[i].v);
  for (int i = 1; i <= W; ++i) {
    if (a[1].w <= i)
      f[i] = a[1].v;
  }
  for (int i = 2; i <= N; ++i) {
    for (int j = W; j >= 0; --j) {
      if (j >= a[i].w)
        f[j] = max(f[j], f[j - a[i].w] + a[i].v);
    }
  }
  cout << f[W] << endl;
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
// define DEBUG for debug
// #define DEBUG

#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define MAX 100000
#define MOD 1000000007
typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
ll n, w;
pll arr[100];
ll dp[100][MAX];
ll dfs(ll i, ll wei) {
  if (dp[i][wei] == 0) {
    if (i <= n) {
      if (wei + arr[i].F <= w)
        dp[i][wei] = dfs(i + 1, wei + arr[i].F) + arr[i].S;
      dp[i][wei] = max(dp[i][wei], dfs(i + 1, wei));
    } else
      return 0;
  }
  return dp[i][wei];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifdef DEBUG
  freopen("i.txt", "r", stdin);
  freopen("o.txt", "w", stdout);
#endif
  cin >> n >> w;
  for (ll i = 0; i < n; i++)
    cin >> arr[i].F >> arr[i].S;
  cout << dfs(0, 0);
  return 0;
}

Fixed submission:
// define DEBUG for debug
// #define DEBUG

#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define MAX 100000
#define MOD 1000000007
typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
ll n, w;
pll arr[100 + 100];
ll dp[100 + 100][MAX + 100];
ll dfs(ll i, ll wei) {
  if (dp[i][wei] == 0) {
    if (i <= n) {
      if (wei + arr[i].F <= w)
        dp[i][wei] = dfs(i + 1, wei + arr[i].F) + arr[i].S;
      dp[i][wei] = max(dp[i][wei], dfs(i + 1, wei));
    } else
      return 0;
  }
  return dp[i][wei];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifdef DEBUG
  freopen("i.txt", "r", stdin);
  freopen("o.txt", "w", stdout);
#endif
  cin >> n >> w;
  for (ll i = 0; i < n; i++)
    cin >> arr[i].F >> arr[i].S;
  cout << dfs(0, 0);
  return 0;
}
Problem p03163 (C++)

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

using namespace std;

long long w[100], v[100], MaxSum[100][100000];
int N;
long long W;

int main() {
  scanf("%d %llu", &N, &W);
  for (int i = 1; i <= N; i++) {
    scanf("%llu %llu", &w[i], &v[i]);
  }
  for (int i = 1; i <= N; i++) {
    for (int j = 0; j <= W; j++) {
      MaxSum[i][j] = MaxSum[i - 1][j];
      if (j >= w[i]) {
        MaxSum[i][j] = max(MaxSum[i][j], MaxSum[i - 1][j - w[i]] + v[i]);
      }
    }
  }
  printf("%llu", MaxSum[N][W]);
}

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

using namespace std;

long long w[200], v[200], MaxSum[200][200000];
int N;
long long W;

int main() {
  scanf("%d %llu", &N, &W);
  for (int i = 1; i <= N; i++) {
    scanf("%llu %llu", &w[i], &v[i]);
  }
  for (int i = 1; i <= N; i++) {
    for (int j = 0; j <= W; j++) {
      MaxSum[i][j] = MaxSum[i - 1][j];
      if (j >= w[i]) {
        MaxSum[i][j] = max(MaxSum[i][j], MaxSum[i - 1][j - w[i]] + v[i]);
      }
    }
  }
  printf("%llu", MaxSum[N][W]);
}
Problem p03163 (C++)

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

using namespace std;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  freopen("test.inp", "r", stdin);
  long long int n, i, j;
  long long int w;
  cin >> n >> w;
  long long int m[n + 10], v[n + 10];
  long long int f[n + 10][w + 10];
  for (i = 1; i <= n; i++) {
    cin >> m[i] >> v[i];
  }
  for (j = 0; j <= w; j++) {
    f[0][j] = 0;
  }
  for (i = 1; i <= n; i++) {
    f[i][0] = 0;
  }
  for (i = 1; i <= n; i++) {
    for (j = 0; j <= w; j++) {
      if (m[i] > j) {
        f[i][j] = f[i - 1][j];
      } else {
        f[i][j] = max((f[i - 1][j - m[i]] + v[i]), (f[i - 1][j]));
      }
    }
  }
  cout << f[n][w];
  return 0;
}


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

using namespace std;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // freopen("test.inp","r",stdin);
  long long int n, i, j;
  long long int w;
  cin >> n >> w;
  long long int m[n + 10], v[n + 10];
  long long int f[n + 10][w + 10];
  for (i = 1; i <= n; i++) {
    cin >> m[i] >> v[i];
  }
  for (j = 0; j <= w; j++) {
    f[0][j] = 0;
  }
  for (i = 1; i <= n; i++) {
    f[i][0] = 0;
  }
  for (i = 1; i <= n; i++) {
    for (j = 0; j <= w; j++) {
      if (m[i] > j) {
        f[i][j] = f[i - 1][j];
      } else {
        f[i][j] = max((f[i - 1][j - m[i]] + v[i]), (f[i - 1][j]));
      }
    }
  }
  cout << f[n][w];
  return 0;
}
Problem p03163 (C++)

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

long long KnapSack(int W, vector<int> wt, vector<int> val, int n) {
  if (n == 0 || W == 0)
    return 0;
  if (wt[n - 1] > W)
    return KnapSack(W, wt, val, n - 1);
  return max(val[n - 1] + KnapSack(W - wt[n - 1], wt, val, n - 1),
             KnapSack(W, wt, val, n - 1));
}

int main() {
  int n, W;
  cin >> n >> W;
  vector<int> wt(n), val(n);
  for (int i = 0; i < n; i++)
    cin >> wt[i] >> val[i];
  cout << KnapSack(W, wt, val, n);
}

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

long long KnapSack(int W, vector<int> wt, vector<int> val, int n) {
  vector<vector<long>> dp(n + 1, vector<long>(W + 1));
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= W; j++) {
      if (i == 0 || j == 0)
        dp[i][j] = 0;
      else if (wt[i - 1] <= j)
        dp[i][j] = max(val[i - 1] + dp[i - 1][j - wt[i - 1]], dp[i - 1][j]);
      else
        dp[i][j] = dp[i - 1][j];
    }
  }
  return dp[n][W];
}

int main() {
  int n, W;
  cin >> n >> W;
  vector<int> wt(n), val(n);
  for (int i = 0; i < n; i++)
    cin >> wt[i] >> val[i];
  cout << KnapSack(W, wt, val, n);
}
Problem p03163 (C++)

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

using LL = long long int;
using uLL = unsigned long long int;
using uint = unsigned int;
using ld = long double;

const int W = 200007;
const int N = 207;

LL w[N], v[N];
LL dp[N];
LL czy[N];

int main() {
  cin.tie(NULL);
  ios_base::sync_with_stdio(0);
  int n, WW;
  cin >> n >> WW;
  for (int i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
  }
  czy[0] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = WW; j >= w[i]; j--) {
      dp[j] = max(dp[j], czy[j - w[i]] * (dp[j - w[i]] + v[i]));
      czy[j] = max(czy[j - w[i]], czy[j]);
    }
  }
  LL res = 0;
  for (int i = 1; i <= WW; i++) {
    res = max(dp[i], res);
  }
  cout << res << '\n';

  return 0;
}

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

using LL = long long int;
using uLL = unsigned long long int;
using uint = unsigned int;
using ld = long double;

const int W = 200007;
const int N = 207;

LL w[N], v[N];
LL dp[W];
LL czy[W];

int main() {
  cin.tie(NULL);
  ios_base::sync_with_stdio(0);
  int n, WW;
  cin >> n >> WW;
  for (int i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
  }
  czy[0] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = WW; j >= w[i]; j--) {
      dp[j] = max(dp[j], czy[j - w[i]] * (dp[j - w[i]] + v[i]));
      czy[j] = max(czy[j - w[i]], czy[j]);
    }
  }
  LL res = 0;
  for (int i = 1; i <= WW; i++) {
    res = max(dp[i], res);
  }
  cout << res << '\n';

  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
// nani?
#include <bits/stdc++.h>
#define pi acos(-1);
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define f(i, a, b) for (int i = a; i < b; i++)
#define sor(a) sort(a.begin(), a.end())
#define rsor(a) sort(a.rbegin(), a.rend())
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)
// #define inf 1LL<<62
typedef long long ll;
typedef double ld;
using namespace std;
const ll inf = 1e9;
const ll MOD = 1e9 + 7;
// ac cmtr;
const ll MAX_N = 1e5;
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}

template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif
//---------------------------

ll dp[100][100005];
ll peso[100005];
ll val[100005];

int main() {
  fastio;
  ll n, w;
  cin >> n >> w;
  f(i, 0, n) {
    ll a, b;
    cin >> a >> b;
    peso[i + 1] = a;
    val[i + 1] = b;
  }
  f(i, 1, n + 1) {
    f(j, 1, w + 1) {
      if (j >= peso[i])
        dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - peso[i]] + val[i]);
      else
        dp[i][j] = dp[i - 1][j];
    }
  }
  cout << dp[n][w] << endl;

  return 0;
}


Fixed submission:
// nani?
#include <bits/stdc++.h>
#define pi acos(-1);
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define f(i, a, b) for (int i = a; i < b; i++)
#define sor(a) sort(a.begin(), a.end())
#define rsor(a) sort(a.rbegin(), a.rend())
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)
// #define inf 1LL<<62
typedef long long ll;
typedef double ld;
using namespace std;
const ll inf = 1e9;
const ll MOD = 1e9 + 7;
// ac cmtr;
const ll MAX_N = 1e5;
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}

template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif
//---------------------------

ll dp[105][100005];
ll peso[100005];
ll val[100005];

int main() {
  fastio;
  ll n, w;
  cin >> n >> w;
  f(i, 0, n) {
    ll a, b;
    cin >> a >> b;
    peso[i + 1] = a;
    val[i + 1] = b;
  }
  f(i, 1, n + 1) {
    f(j, 1, w + 1) {
      if (j >= peso[i])
        dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - peso[i]] + val[i]);
      else
        dp[i][j] = dp[i - 1][j];
    }
  }
  cout << dp[n][w] << endl;

  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fastio()                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define pb push_back
#define show(x) cout << (#x) << " : " << x << endl;
#define ll long long
#define ld long double

#define mp make_pair
#define ff first
#define ss second
#define pii pair<ll, ll>
#define sq(x) ((x) * (x))
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
const ll MOD = 1000 * 1000 * 1000 + 7;
using namespace std;

ll dp[100][100005];
ll N, W;

ll seno(ll idx, ll w, ll C[][2]) {

  ll Q, R;

  if (idx == N) {
    return 0;
  }

  if (dp[idx][w] != INT_MAX)
    return dp[idx][w];

  if (w + C[idx][0] <= W)
    Q = seno(idx + 1, w + C[idx][0], C) + C[idx][1];
  else
    Q = 0;

  R = seno(idx + 1, w, C);

  return dp[idx][w] = max(Q, R);
}

int main() {

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

  cin >> N >> W;

  ll A[N][2];

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

  for (int i = 0; i < 100; i++) {
    for (int j = 0; j <= 100004; j++)
      dp[i][j] = INT_MAX;
  }

  ll x = seno(0, 0, A);

  cout << x;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define fastio()                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define pb push_back
#define show(x) cout << (#x) << " : " << x << endl;
#define ll long long
#define ld long double

#define mp make_pair
#define ff first
#define ss second
#define pii pair<ll, ll>
#define sq(x) ((x) * (x))
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
const ll MOD = 1000 * 1000 * 1000 + 7;
using namespace std;

ll dp[100][100005];
ll N, W;

ll seno(ll idx, ll w, ll C[][2]) {

  ll Q, R;

  if (idx == N) {
    return 0;
  }

  if (dp[idx][w] != INT_MAX)
    return dp[idx][w];

  if (w + C[idx][0] <= W)
    Q = seno(idx + 1, w + C[idx][0], C) + C[idx][1];
  else
    Q = 0;

  R = seno(idx + 1, w, C);

  return dp[idx][w] = max(Q, R);
}

int main() {

  fastio();

  cin >> N >> W;

  ll A[N][2];

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

  for (int i = 0; i < 100; i++) {
    for (int j = 0; j <= 100004; j++)
      dp[i][j] = INT_MAX;
  }

  ll x = seno(0, 0, A);

  cout << x;

  return 0;
}
Problem p03163 (C++)

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

#define int long long
#define ll long long
#define f1(i, a, b) for (i = a; i < b; i++)
#define f2(i, a, b) for (i = a; i >= b; i--)
#define endl '\n'
#define pb push_back
#define gp " "
#define ff first
#define ss second
#define mp make_pair

const int mod = 1000000007;

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("/home/akmittal/Desktop/Competitive Programming/in.txt", "r", stdin);
  freopen("/home/akmittal/Desktop/Competitive Programming/out.txt", "w",
          stdout);
#endif
  int n, w;
  cin >> n >> w;
  vector<int> dp(w + 1, 0ll);
  int i;
  f1(i, 0, n) {
    int weight, value;
    cin >> weight >> value;
    int j;
    for (int j = w - weight; j >= 0; j--) {
      dp[j + weight] = max(dp[j + weight], dp[j] + value);
    }
    // f1(j,0,w+1)
    // cout<<dp[j]<<gp;
    // cout<<endl;
  }
  int ans = 0ll;
  f1(i, 0, w + 1) { ans = max(ans, dp[i]); }

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

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

#define int long long
#define ll long long
#define f1(i, a, b) for (i = a; i < b; i++)
#define f2(i, a, b) for (i = a; i >= b; i--)
#define endl '\n'
#define pb push_back
#define gp " "
#define ff first
#define ss second
#define mp make_pair

const int mod = 1000000007;

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // #ifndef ONLINE_JUDGE
  // 	freopen("/home/akmittal/Desktop/Competitive
  // Programming/in.txt","r",stdin); 	freopen("/home/akmittal/Desktop/Competitive
  // Programming/out.txt","w",stdout); #endif
  int n, w;
  cin >> n >> w;
  vector<int> dp(w + 1, 0ll);
  int i;
  f1(i, 0, n) {
    int weight, value;
    cin >> weight >> value;
    int j;
    for (int j = w - weight; j >= 0; j--) {
      dp[j + weight] = max(dp[j + weight], dp[j] + value);
    }
    // f1(j,0,w+1)
    // cout<<dp[j]<<gp;
    // cout<<endl;
  }
  int ans = 0ll;
  f1(i, 0, w + 1) { ans = max(ans, dp[i]); }

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

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

using namespace std;

int n, W;
vector<long long> w(101), v(101);

long long tb[100][100001];

long long dp(int i, int weight) {
  if (weight > W)
    return -1000000000000LL;
  if (i >= n) {
    return 0;
  }

  if (tb[i][weight] != -1)
    return tb[i][weight];

  long long total = 0;
  for (int j = i; j <= n; ++j) {
    total = max(total, dp(j + 1, weight + w[j]) + v[j]);
  }

  return tb[i][weight] = total;
}

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

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

  cin >> n >> W;

  for (int i = 0; i < n; ++i) {
    cin >> w[i] >> v[i];
  }

  cout << dp(0, 0) << endl;

  return 0;
}

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

using namespace std;

int n, W;
vector<long long> w(101), v(101);

long long tb[100][100001];

long long dp(int i, int weight) {
  if (weight > W)
    return -1000000000000LL;
  if (i >= n) {
    return 0;
  }

  if (tb[i][weight] != -1)
    return tb[i][weight];

  return tb[i][weight] =
             max(dp(i + 1, weight), dp(i + 1, weight + w[i]) + v[i]);
}

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

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

  cin >> n >> W;

  for (int i = 0; i < n; ++i) {
    cin >> w[i] >> v[i];
  }

  cout << dp(0, 0) << endl;

  return 0;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
long long n, c, value;
long long f[101][100001];
long long v[101], w[101];
int main() {
  scanf("%lld%lld", &n, &c);
  for (int i = 1; i <= n; i++) {
    scanf("%lld%lld", &w[i], &v[i]);
    value += v[i];
  }
  for (int j = 1; j <= value; j++)
    f[0][j] = (long long)10e11 + 1;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= value; j++) {
      f[i][j] = f[i - 1][j];
      if (j >= v[i])
        f[i][j] = min(f[i][j], f[i - 1][j - v[i]] + w[i]);
    }
  for (int i = value; i >= 1; i--) {
    if (f[n][i] <= c) {
      printf("%d", i);
      return 255;
    }
  }
}

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