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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define ll long long
ll wt[101], pft[101];
ll n;
ll dp[101][101];
ll solve(ll st_index, ll wt_left) {

  if (st_index == n)
    return 0;

  if (dp[st_index][wt_left] != -1)
    return dp[st_index][wt_left];

  ll ans = solve(st_index + 1, wt_left);

  if (wt[st_index] <= wt_left) {
    ans = max(ans, pft[st_index] + solve(st_index + 1, wt_left - wt[st_index]));
  }

  return dp[st_index][wt_left] = ans;
}

int main() {

  ll w;
  cin >> n >> w;
  memset(wt, 0, sizeof wt);
  memset(pft, 0, sizeof pft);
  memset(dp, -1, sizeof dp);

  for (ll i = 0; i < n; i++) {
    cin >> wt[i] >> pft[i];
  }

  ll ans = solve(0, w);
  cout << ans;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define ll long long
ll wt[101], pft[101];
ll n;
ll dp[110][100001];
ll solve(ll st_index, ll wt_left) {

  if (st_index == n)
    return 0;

  if (dp[st_index][wt_left] != -1)
    return dp[st_index][wt_left];

  ll ans = solve(st_index + 1, wt_left);

  if (wt[st_index] <= wt_left) {
    ans = max(ans, pft[st_index] + solve(st_index + 1, wt_left - wt[st_index]));
  }

  return dp[st_index][wt_left] = ans;
}

int main() {

  ll w;
  cin >> n >> w;
  memset(wt, 0, sizeof wt);
  memset(pft, 0, sizeof pft);
  memset(dp, -1, sizeof dp);

  for (ll i = 0; i < n; i++) {
    cin >> wt[i] >> pft[i];
  }

  ll ans = solve(0, w);
  cout << ans;

  return 0;
}
Problem p03163 (C++)

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

#define int long long

#define PB push_back
#define MP make_pair
#define S second
#define F first
// #define D cout<<"*\n";
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define mod 1000000007
const double pi = 3.14159265358979323846;

int dp[2][100000004];

int knapsack(int value[], int weight[], int n, int W) {
  memset(dp, 0, sizeof(dp));

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

  if (n & 1)
    return dp[1][W];
  return dp[0][W];
}

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

  while (t--) {
    int n, W;
    cin >> n >> W;

    int value[n], weight[n];
    for (int i = 0; i < n; i++) {
      cin >> weight[i] >> value[i];
    }
    cout << knapsack(value, weight, n, W);
  }
  return 0;
}


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

#define int long long

#define PB push_back
#define MP make_pair
#define S second
#define F first
// #define D cout<<"*\n";
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define mod 1000000007
const double pi = 3.14159265358979323846;

int dp[2][1000004];

int knapsack(int value[], int weight[], int n, int W) {
  memset(dp, 0, sizeof(dp));

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

  if (n & 1)
    return dp[1][W];
  return dp[0][W];
}

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

  while (t--) {
    int n, W;
    cin >> n >> W;

    int value[n], weight[n];
    for (int i = 0; i < n; i++) {
      cin >> weight[i] >> value[i];
    }
    cout << knapsack(value, weight, n, W);
  }
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int n;
int max_weight;
int w[101];
long long int v[101];
long long int dp[101][100001];
long long int ans = 0;
int main() {
  cin >> n >> max_weight;
  for (int i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
  }

  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= max_weight; j++) {
      dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
      if (j + w[i] <= max_weight)
        dp[i + 1][j + w[i]] = max(dp[i + 1][j + w[i]], dp[i][j] + v[i]);
    }
  }
  for (int i = 0; i <= max_weight; i++) {
    if (dp[n][i] >= ans)
      ans = dp[n][i];
  }
  cout << ans << endl;
}

Fixed submission:
#include <iostream>
using namespace std;
int n;
int max_weight;
int w[101];
long long int v[101];
long long int dp[101][100001];
long long int ans = 0;
int main() {
  cin >> n >> max_weight;
  for (int i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= max_weight; j++) {
      dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
      if (j + w[i] <= max_weight)
        dp[i + 1][j + w[i]] = max(dp[i + 1][j + w[i]], dp[i][j] + v[i]);
    }
  }
  for (int i = 0; i <= max_weight; i++) {
    if (dp[n][i] >= ans)
      ans = dp[n][i];
  }
  cout << ans << endl;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <vector>
// #include <bits/stdc++.h>
#define lson l, mid, rt << 1

#define rson mid + 1, r, rt << 1 | 1

const int N = 1e5 + 100;
using namespace std;

typedef long long ll;
const int inf = 0x3f3f3f3f;

ll dp[200], a[200];
int main() {
  int n, W;
  scanf("%d%d", &n, &W);
  ll w, v;
  for (int i = 1; i <= n; i++) {
    scanf("%lld%lld", &w, &v);
    for (int j = W; j >= w; j--)
      dp[j] = max(dp[j], dp[j - w] + v);
  }
  printf("%lld\n", dp[W]);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <vector>
// #include <bits/stdc++.h>
#define lson l, mid, rt << 1

#define rson mid + 1, r, rt << 1 | 1

const int N = 1e5 + 100;
using namespace std;

typedef long long ll;
const int inf = 0x3f3f3f3f;

ll dp[N], a[200];
int main() {
  int n, W;
  scanf("%d%d", &n, &W);
  ll w, v;
  for (int i = 1; i <= n; i++) {
    scanf("%lld%lld", &w, &v);
    for (int j = W; j >= w; j--)
      dp[j] = max(dp[j], dp[j - w] + v);
  }
  printf("%lld\n", dp[W]);
  return 0;
}
Problem p03163 (C++)

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

using namespace std;

#define ll long long

ll v[105], weight[105], dp[105][10005], n, CAP;

ll KnapSack(int i, ll w) {
  if (i == n) {
    return 0;
  }
  if (dp[i][w] != -1)
    return dp[i][w];
  ll ans1 = 0, ans2 = 0;
  if (w + weight[i] <= CAP) {
    ans1 = KnapSack(i + 1, w + weight[i]) + v[i];
  }
  ans2 = KnapSack(i + 1, w);

  return dp[i][w] = max(ans1, ans2);
}

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

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

  ll ans = KnapSack(0, 0);

  cout << ans << endl;

  return 0;
}


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

using namespace std;

#define ll long long

ll v[105], weight[105], dp[105][100005], n, CAP;

ll KnapSack(int i, ll w) {
  if (i == n) {
    return 0;
  }
  if (dp[i][w] != -1)
    return dp[i][w];
  ll ans1 = 0, ans2 = 0;
  if (w + weight[i] <= CAP) {
    ans1 = KnapSack(i + 1, w + weight[i]) + v[i];
  }
  ans2 = KnapSack(i + 1, w);

  return dp[i][w] = max(ans1, ans2);
}

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

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

  ll ans = KnapSack(0, 0);

  cout << ans << endl;

  return 0;
}
Problem p03163 (C++)

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

using namespace std;

#define ll long long

ll n, CAP, w[105], v[105], dp[105][105];

ll knapsack(ll item, ll weight) {
  if (item > n) {
    return 0;
  }

  if (dp[item][weight] != -1)
    return dp[item][weight];

  ll ans1 = 0;

  if (weight + w[item] <= CAP) {
    ans1 = knapsack(item + 1, weight + w[item]) + v[item];
  }

  ll ans2 = knapsack(item + 1, weight);

  return dp[item][weight] = max(ans1, ans2);
}

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

  cin >> n >> CAP;

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

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

  ll ans = knapsack(1, 0);

  cout << ans << endl;

  return 0;
}


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

using namespace std;

#define ll long long

ll n, CAP, w[105], v[105], dp[105][100005];

ll knapsack(ll item, ll weight) {
  if (item > n) {
    return 0;
  }

  if (dp[item][weight] != -1)
    return dp[item][weight];

  ll ans1 = 0;

  if (weight + w[item] <= CAP) {
    ans1 = knapsack(item + 1, weight + w[item]) + v[item];
  }

  ll ans2 = knapsack(item + 1, weight);

  return dp[item][weight] = max(ans1, ans2);
}

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

  cin >> n >> CAP;

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

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

  ll ans = knapsack(1, 0);

  cout << ans << endl;

  return 0;
}
Problem p03163 (C++)

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

#define int long long int

const int INF = 1e9 + 5;

#undef int
int main() {
#define int long long int
  int n, w;
  cin >> n >> w;

  vector<int> weight(n, 0);
  vector<int> values(n, 0);
  int dp[n + 5][w + 5];
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i < n; i++) {
    cin >> weight[i];
    cin >> values[i];
  }

  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 (j >= weight[i - 1]) {
        dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight[i]] + values[i - 1]);
      }

      else
        dp[i][j] = dp[i - 1][j];
    }
  }

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

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

#define int long long int

const int INF = 1e9 + 5;

#undef int
int main() {
#define int long long int
  int n, w;
  cin >> n >> w;

  vector<int> weight(n, 0);
  vector<int> values(n, 0);
  int dp[n + 5][w + 5];
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i < n; i++) {
    cin >> weight[i];
    cin >> values[i];
  }

  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 (j >= weight[i - 1]) {
        dp[i][j] =
            max(dp[i - 1][j], dp[i - 1][j - weight[i - 1]] + values[i - 1]);
      }

      else
        dp[i][j] = dp[i - 1][j];
    }
  }

  cout << dp[n][w];
}
Problem p03163 (C++)

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

#define MAXN 120
#define MAXW 100001

long long N, W;
long long w[MAXN], v[MAXN];

long long dp[MAXN][MAXW];

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

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

#define MAXN 120
#define MAXW 100001

long long N, W;
long long w[MAXN], v[MAXN];

long long dp[MAXN][MAXW];

int main() {
  cin >> N >> W;
  for (int i = 0; i < N; i++) {
    cin >> w[i] >> v[i];
  }
  for (int i = w[0]; i <= W; i++) {
    dp[0][i] = v[0];
  }
  for (int i = 1; i < N; i++) {
    for (int j = 0; j <= W; j++) {
      if (w[i] > j) {
        dp[i][j] = dp[i - 1][j];
      } else {
        dp[i][j] = max(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 <bits/stdc++.h>
using namespace std;
#define ll long long
#define FASTIO                                                                 \
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(nullptr)
#define INF (1LL << 62) - 1
#define F first
#define S second
#define pb push_back

ll dp[102][100002];

ll dosomething(ll w, ll n, vector<ll> &wt, vector<ll> &val, ll i) {
  if (i >= n || w <= 0)
    return 0;
  if (dp[i][w] != -1)
    return dp[i][w];

  if (wt[i] > w)
    dp[i][w] = dosomething(w, n, wt, val, i + 1);
  else
    dp[i][w] = max(dosomething(w, n, wt, val, i + 1),
                   val[i] + dosomething(w - wt[i], n, wt, val, i + 1));

  return dp[i][w];
}

int main() {
  FASTIO;
  for (ll i = 0; i < 1002; i++)
    for (ll j = 0; j < 1002; j++)
      dp[i][j] = -1;
  ll n, w;
  cin >> n >> w;
  vector<ll> wt(n), val(n);
  for (ll i = 0; i < n; i++)
    cin >> wt[i] >> val[i];

  cout << dosomething(w, n, wt, val, 0) << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define FASTIO                                                                 \
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(nullptr)
#define INF (1LL << 62) - 1
#define F first
#define S second
#define pb push_back

ll dp[102][100002];

ll dosomething(ll w, ll n, vector<ll> &wt, vector<ll> &val, ll i) {
  if (i >= n || w <= 0)
    return 0;
  if (dp[i][w] != -1)
    return dp[i][w];

  if (wt[i] > w)
    dp[i][w] = dosomething(w, n, wt, val, i + 1);
  else
    dp[i][w] = max(dosomething(w, n, wt, val, i + 1),
                   val[i] + dosomething(w - wt[i], n, wt, val, i + 1));

  return dp[i][w];
}

int main() {
  FASTIO;
  for (ll i = 0; i < 102; i++)
    for (ll j = 0; j < 100002; j++)
      dp[i][j] = -1;
  ll n, w;
  cin >> n >> w;
  vector<ll> wt(n), val(n);
  for (ll i = 0; i < n; i++)
    cin >> wt[i] >> val[i];

  cout << dosomething(w, n, wt, val, 0) << "\n";
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <cstdio>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long li;
typedef unsigned long long uli;
typedef pair<int, int> pii;
typedef long double ld;

#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define fr first
#define sc second
#define pb push_back
#define forn(i, n) for (int i = 0; i < int(n); ++i)
#define fore(i, l, r) for (int i = int(l); i < int(r); ++i)
#define forb(i, n) for (int i = int(n) - 1; i >= 0; --i)
// #define x first
// #define y second

const int INF = 1e9;
const li INF64 = 4e18;
const int M = 100;
const int N = 100 * 1000 + 100;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;
const double PI = 3.14159265359;

li gcd(li a, li b) { return a == 0 ? b : gcd(b % a, a); }

li dp[M][N];

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

  int n, m;
  cin >> n >> m;

  vector<int> w(n), v(n);
  forn(i, n) { cin >> w[i] >> v[i]; }

  fore(i, 1, n + 1) {
    fore(j, 1, m + 1) {
      dp[i][j] = dp[i - 1][j];
      if (j - w[i - 1] >= 0) {
        dp[i][j] = max(dp[i][j], dp[i - 1][j - w[i - 1]] + v[i - 1]);
      }
    }
  }

  cout << dp[n][m] << '\n';
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <cstdio>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long li;
typedef unsigned long long uli;
typedef pair<int, int> pii;
typedef long double ld;

#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define fr first
#define sc second
#define pb push_back
#define forn(i, n) for (int i = 0; i < int(n); ++i)
#define fore(i, l, r) for (int i = int(l); i < int(r); ++i)
#define forb(i, n) for (int i = int(n) - 1; i >= 0; --i)
// #define x first
// #define y second

const int INF = 1e9;
const li INF64 = 4e18;
const int M = 100 + 10;
const int N = 100 * 1000 + 100;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;
const double PI = 3.14159265359;

li gcd(li a, li b) { return a == 0 ? b : gcd(b % a, a); }

li dp[M][N];

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

  int n, m;
  cin >> n >> m;

  vector<int> w(n), v(n);
  forn(i, n) { cin >> w[i] >> v[i]; }

  fore(i, 1, n + 1) {
    fore(j, 1, m + 1) {
      dp[i][j] = dp[i - 1][j];
      if (j - w[i - 1] >= 0) {
        dp[i][j] = max(dp[i][j], dp[i - 1][j - w[i - 1]] + v[i - 1]);
      }
    }
  }

  cout << dp[n][m] << '\n';
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 1; i <= (n); i++)
using ll = long long;
typedef pair<int, int> P;
const int INF = 1e9;
const int MOD = 1000000007;
int main() {
  ll N, W;
  cin >> N >> W;
  vector<ll> w(N + 1), v(N + 1);
  rep(i, N) cin >> w[i] >> v[i];
  vector<vector<ll>> dat(N + 1, vector<ll>(W + 1));
  rep(i, N) {
    rep(j, W + 1) {
      if (w[i] <= j) {
        dat[i][j] = max(dat[i - 1][j], dat[i - 1][j - w[i]] + v[i]);
      } else
        dat[i][j] = dat[i - 1][j];
    }
  }
  cout << dat[N][W];
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 1; i <= (n); i++)
using ll = long long;
typedef pair<int, int> P;
const int INF = 1e9;
const int MOD = 1000000007;
int main() {
  ll N, W;
  cin >> N >> W;
  vector<ll> w(N + 1), v(N + 1);
  rep(i, N) cin >> w[i] >> v[i];
  vector<vector<ll>> dat(N + 1, vector<ll>(W + 1));
  rep(i, N) {
    rep(j, W) {
      if (w[i] <= j) {
        dat[i][j] = max(dat[i - 1][j], dat[i - 1][j - w[i]] + v[i]);
      } else
        dat[i][j] = dat[i - 1][j];
    }
  }
  cout << dat[N][W];
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define INP (a << 50)
using namespace std;
int main() {
  int a, b, c, i, j;
  cin >> a >> b;
  int s[a + 5], ss[a + 5];
  long long int sss[a + 5][a + 5];
  for (i = 1; i <= a; i++) {
    cin >> s[i] >> ss[i];
  }
  memset(sss, 0, sizeof(sss));
  for (i = 1; i <= a; i++) {
    for (j = 1; j <= b; j++) {
      if (s[i] <= j) {
        sss[i][j] = max(sss[i - 1][j], sss[i - 1][j - s[i]] + ss[i]);
      } else
        sss[i][j] = sss[i - 1][j];
    }
  }
  cout << sss[a][b] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define INP (a << 50)
using namespace std;
int main() {
  int a, b, c, i, j;
  cin >> a >> b;
  int s[a + 5], ss[b + 5];
  long long int sss[a + 5][b + 5];
  for (i = 1; i <= a; i++) {
    cin >> s[i] >> ss[i];
  }
  memset(sss, 0, sizeof(sss));
  for (i = 1; i <= a; i++) {
    for (j = 1; j <= b; j++) {
      if (s[i] <= j) {
        sss[i][j] = max(sss[i - 1][j], sss[i - 1][j - s[i]] + ss[i]);
      } else
        sss[i][j] = sss[i - 1][j];
    }
  }
  cout << sss[a][b] << endl;
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll a[201], b[201];
ll n, w;
ll dp[201][201];
ll rec(ll idx, ll weight) {
  if (weight > w)
    return -1e9;
  if (weight == w)
    return 0;
  if (idx == n)
    return 0;
  if (dp[idx][weight] != -1)
    return dp[idx][weight];
  ll op1 = rec(idx + 1, weight + a[idx]) + b[idx];
  ll op2 = rec(idx + 1, weight);
  return dp[idx][weight] = max(op1, op2);
}

int main() {
  cin >> n >> w;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }
  memset(dp, -1, sizeof(dp));
  cout << rec(0, 0);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll a[201], b[201];
ll n, w;
ll dp[201][200001];
ll rec(ll idx, ll weight) {
  if (weight > w)
    return -1e9;
  if (weight == w)
    return 0;
  if (idx == n)
    return 0;
  if (dp[idx][weight] != -1)
    return dp[idx][weight];
  ll op1 = rec(idx + 1, weight + a[idx]) + b[idx];
  ll op2 = rec(idx + 1, weight);
  return dp[idx][weight] = max(op1, op2);
}

int main() {
  cin >> n >> w;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }
  memset(dp, -1, sizeof(dp));
  cout << rec(0, 0);
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
/* in the name of allah */
#include <bits/stdc++.h>
using namespace std;

const int N = 100, W = 1e5 + 5;
long long n, w, a[N], v[N], dp[N][W];

void readInput() {
  cin >> n >> w;
  for (int i = 1; i <= n; i++)
    cin >> a[i] >> v[i];
}

void solve() {
  for (int i = 1; i <= n; i++)
    for (int j = 0; j <= w; j++) {
      dp[i][j] = dp[i - 1][j];
      if (j >= a[i] && dp[i][j] < dp[i - 1][j - a[i]] + v[i])
        dp[i][j] = dp[i - 1][j - a[i]] + v[i];
    }
  cout << dp[n][w] << endl;
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  readInput(), solve();
  return 0;
}


Fixed submission:
/* in the name of allah */
#include <bits/stdc++.h>
using namespace std;

const int N = 100 + 5, W = 1e5 + 5;
long long n, w, a[N], v[N], dp[N][W];

void readInput() {
  cin >> n >> w;
  for (int i = 1; i <= n; i++)
    cin >> a[i] >> v[i];
}

void solve() {
  for (int i = 1; i <= n; i++)
    for (int j = 0; j <= w; j++) {
      dp[i][j] = dp[i - 1][j];
      if (j >= a[i] && dp[i][j] < dp[i - 1][j - a[i]] + v[i])
        dp[i][j] = dp[i - 1][j - a[i]] + v[i];
    }
  cout << dp[n][w] << endl;
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  readInput(), solve();
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(x, y, z) for (int x = y; x <= z; x++)
#define MSET(x, y) memset(x, y, sizeof(x))
#define M 105
using namespace std;
int n, m, w[M], v[M];
long long dp[10005];
int main() {
  while (~scanf("%d %d", &n, &m)) {
    REP(i, 1, n) scanf("%d %d", &w[i], &v[i]);
    MSET(dp, -1);
    dp[0] = 0;
    REP(i, 1, n)
    for (int j = m; j >= w[i]; j--) if (dp[j - w[i]] != -1) dp[j] =
        max(dp[j], dp[j - w[i]] + v[i]);
    printf("%lld\n", *max_element(dp, dp + m + 1));
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(x, y, z) for (int x = y; x <= z; x++)
#define MSET(x, y) memset(x, y, sizeof(x))
#define M 105
using namespace std;
int n, m, w[M], v[M];
long long dp[100005];
int main() {
  while (~scanf("%d %d", &n, &m)) {
    REP(i, 1, n) scanf("%d %d", &w[i], &v[i]);
    MSET(dp, -1);
    dp[0] = 0;
    REP(i, 1, n)
    for (int j = m; j >= w[i]; j--) if (dp[j - w[i]] != -1) dp[j] =
        max(dp[j], dp[j - w[i]] + v[i]);
    printf("%lld\n", *max_element(dp, dp + m + 1));
  }

  return 0;
}
Problem p03163 (C++)

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

#define reps(i, s, n) for (int(i) = (s); (i) < (n); (i)++)
#define rep(i, n) reps(i, 0, n)
using namespace std;
using ll = long long;

int main() {
  int N, W;
  cin >> N >> W;
  vector<int> w(N + 1, 0), v(N + 1, 0);
  rep(i, N) cin >> w[i + 1] >> v[i + 1];
  // dp[101][100000];
  ll dp[101][1000];

  rep(i, N + 1) {
    rep(j, W + 1) { dp[i][j] = 0; }
  }

  reps(i, 1, N + 1) {
    rep(j, W + 1) {
      if (j - w[i] >= 0)
        dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]);
      else
        dp[i][j] = dp[i - 1][j];
    }
  }

  // rep(i,N+1){
  //     rep(j,W+1){
  //         cout << dp[i][j] <<" ";
  //     }
  //     cout << endl;
  // }
  cout << dp[N][W] << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <vector>

#define reps(i, s, n) for (int(i) = (s); (i) < (n); (i)++)
#define rep(i, n) reps(i, 0, n)
using namespace std;
using ll = long long;

int main() {
  int N, W;
  cin >> N >> W;
  vector<int> w(N + 1, 0), v(N + 1, 0);
  rep(i, N) cin >> w[i + 1] >> v[i + 1];
  ll dp[101][100010];
  // ll dp[101][1000];

  rep(i, N + 1) {
    rep(j, W + 1) { dp[i][j] = 0; }
  }

  reps(i, 1, N + 1) {
    rep(j, W + 1) {
      if (j - w[i] >= 0)
        dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]);
      else
        dp[i][j] = dp[i - 1][j];
    }
  }

  // rep(i,N+1){
  //     rep(j,W+1){
  //         cout << dp[i][j] <<" ";
  //     }
  //     cout << endl;
  // }
  cout << dp[N][W] << endl;

  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <climits>
#include <cstring>
#include <iostream>
#define ll long long
using namespace std;
ll n, w;
ll weight[105], value[105];
ll dp[105][105];
ll solve(ll i, ll j) {
  if (i == n + 1)
    return 0;
  if (j == 0)
    return 0;
  if (dp[i][j] != -1)
    return dp[i][j];
  if (j - weight[i] < 0)
    return dp[i][j] = solve(i + 1, j);
  else
    return dp[i][j] =
               max(value[i] + solve(i + 1, j - weight[i]), solve(i + 1, j));
}
int main() {
  cin >> n >> w;
  for (ll i = 1; i <= n; i++) {
    cin >> weight[i] >> value[i];
  }
  memset(dp, -1, sizeof(dp));
  cout << max(solve(1, w - weight[1]), solve(1, w));
  return 0;
}


Fixed submission:
#include <climits>
#include <cstring>
#include <iostream>
#define ll long long
using namespace std;
ll n, w;
ll weight[100005], value[100005];
ll dp[105][100005];
ll solve(ll i, ll j) {
  if (i == n + 1)
    return 0;
  if (j == 0)
    return 0;
  if (dp[i][j] != -1)
    return dp[i][j];
  if (j - weight[i] < 0)
    return dp[i][j] = solve(i + 1, j);
  else
    return dp[i][j] =
               max(value[i] + solve(i + 1, j - weight[i]), solve(i + 1, j));
}
int main() {
  cin >> n >> w;
  for (ll i = 1; i <= n; i++) {
    cin >> weight[i] >> value[i];
  }
  memset(dp, -1, sizeof(dp));
  cout << max(solve(1, w - weight[1]), solve(1, w));
  return 0;
}
Problem p03163 (C++)

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

using namespace std;

typedef long long int ll;

#define all(x) x.begin(), x.end()

const ll mod = 1e9 + 7;
const ll INF = 1e9;
const ll MAXN = 1e9;

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

  vector<vector<ll>> dp(n + 1);
  for (int i = 0; i <= n; i++) {
    dp[i].resize(W);
  }

  for (int i = 1; i <= n; i++) {
    for (int j = W; j >= 0; j--) {
      dp[i][j] =
          max(dp[i - 1][j], (j - w[i] >= 0 ? dp[i - 1][j - w[i]] + v[i] : 0));
    }
  }

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

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

typedef long long int ll;

#define all(x) x.begin(), x.end()

const ll mod = 1e9 + 7;
const ll INF = 1e9;
const ll MAXN = 1e9;

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

  vector<vector<ll>> dp(n + 1);
  for (int i = 0; i <= n; i++) {
    dp[i].resize(W + 1, 0);
  }

  for (int i = 1; i <= n; i++) {
    for (int j = W; j >= 0; j--) {
      dp[i][j] =
          max(dp[i - 1][j], (j - w[i] >= 0 ? dp[i - 1][j - w[i]] + v[i] : 0));
    }
  }

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

  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define double long double
#define endl "\n"
const int INF = (int)1e9 + 9;
const int maxn = (int)1e6 + 6;
// const mod = (int) 1e9 + 7;

using namespace std;

signed main() {
  int N, W;
  cin >> N >> W;

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

  dp[0][w[0]] = v[0];

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

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

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define double long double
#define endl "\n"
const int INF = (int)1e9 + 9;
const int maxn = (int)1e6 + 6;
// const mod = (int) 1e9 + 7;

using namespace std;

signed main() {
  int N, W;
  cin >> N >> W;

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

  dp[0][w[0]] = v[0];

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

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

  cout << ans << endl;

  return 0;
}
Problem p03163 (C++)

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

using namespace std;

int N, W, w[100], v[100];
long long dp[109][10009];

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

  for (int j = 0; j <= W; ++j)
    dp[0][j] = 0;

  for (int i = 0; i < N; ++i) {
    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 <bits/stdc++.h>

using namespace std;

int N, W, w[100], v[100];
long long dp[109][100009];

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

  for (int j = 0; j <= W; ++j)
    dp[0][j] = 0;

  for (int i = 0; i < N; ++i) {
    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 <bits/stdc++.h>

using namespace std;

const int maxn = 1e2 + 10;
const int maxm = 1e5 + 10;

typedef long long ll;

int w[maxn], c[maxn];
ll dp[maxn];

void solve(int n, int m) {
  for (int i = 1; i <= n; i++)
    for (int j = m; j >= w[i]; j--)
      dp[j] = max(dp[j], dp[j - w[i]] + (ll)c[i]);
}

int main(void) {
  int n, m;
  cin >> n >> m;

  for (int i = 1; i <= n; i++)
    cin >> w[i] >> c[i];

  solve(n, m);

  cout << dp[m] << "\n";
}

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

using namespace std;

const int maxn = 1e2 + 10;
const int maxm = 1e5 + 10;

typedef long long ll;

int w[maxn], c[maxn];
ll dp[maxm];

void solve(int n, int m) {
  for (int i = 1; i <= n; i++)
    for (int j = m; j >= w[i]; j--)
      dp[j] = max(dp[j], dp[j - w[i]] + (ll)c[i]);
}

int main(void) {
  int n, m;
  cin >> n >> m;

  for (int i = 1; i <= n; i++)
    cin >> w[i] >> c[i];

  solve(n, m);

  cout << dp[m] << "\n";
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define frst first
#define sec second
#define fast cin.tie(0), ios_base ::sync_with_stdio(0)
#define db double
#define mod 1000000007
void online_judge() {
#ifndef ONLINE_JUDGE
  freopen("Input.txt", "r", stdin);
  freopen("Output.txt", "w", stdout);
#endif
}

ll dp[101][100001];
ll solve1(ll wt[], ll p[], ll n, ll w) {
  for (int i = 0; i <= n; ++i) {
    for (int j = 0; j <= w; ++j) {
      if (i == 0 || j == 0) {
        dp[i][j] == 0;
        continue;
      }
      dp[i][j] = dp[i - 1][j];
      if (j >= wt[i - 1]) {
        dp[i][j] = max(dp[i][j], dp[i - 1][j - wt[i - 1]] + p[i - 1]);
      }
    }
  }
  ////cout << dp[n][w] << endl;
  return dp[n][w];
}

void go() {
  ll n, w;
  cin >> n >> w;
  ll wt[n], p[n];
  for (int i = 0; i < n; ++i)
    cin >> wt[i] >> p[i];
  // memset(dp , -1 , sizeof dp);
  cout << solve1(wt, p, n, w);
}

int main() {
  fast;
  online_judge();
  // cout << 2332;
  go();
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define frst first
#define sec second
#define fast cin.tie(0), ios_base ::sync_with_stdio(0)
#define db double
#define mod 1000000007
void online_judge() {
#ifndef ONLINE_JUDGE
  freopen("Input.txt", "r", stdin);
  freopen("Output.txt", "w", stdout);
#endif
}

ll dp[101][100001];
ll solve1(ll wt[], ll p[], ll n, ll w) {
  for (int i = 0; i <= n; ++i) {
    for (int j = 0; j <= w; ++j) {
      if (i == 0 || j == 0) {
        dp[i][j] == 0;
        continue;
      }
      dp[i][j] = dp[i - 1][j];
      if (j >= wt[i - 1]) {
        dp[i][j] = max(dp[i][j], dp[i - 1][j - wt[i - 1]] + p[i - 1]);
      }
    }
  }
  ////cout << dp[n][w] << endl;
  return dp[n][w];
}

void go() {
  ll n, w;
  cin >> n >> w;
  ll wt[n], p[n];
  for (int i = 0; i < n; ++i)
    cin >> wt[i] >> p[i];
  // memset(dp , -1 , sizeof dp);
  cout << solve1(wt, p, n, w);
}

int main() {
  fast;
  ///	online_judge();
  // cout << 2332;
  go();
}
Problem p03163 (C++)

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

using namespace std;

int main(int argc, const char *argv[]) {
  long long n, m;
  cin >> n >> m;
  long long arr[n][2];
  for (long long i = 0; i < n; i++) {
    cin >> arr[i][0] >> arr[i][1];
  }
  long long dp[n + 1][m + 1];
  for (long long j = 0; j < m + 1; j++) {
    for (long long i = 0; i < n + 1; i++) {
      dp[i][j] = 0;
    }
  }

  for (long long i = 1; i < n + 1; i++) {
    for (long long j = 0; j < m + 1; j++) {
      if (arr[i - 1][0] > j) {
        dp[i][j] = dp[i - 1][j];
      } else {
        dp[i][j] =
            max(dp[i - 1][j], arr[i - 1][1] + dp[i - 1][j - arr[i - 1][0]]);
      }
    }
  }

  cout << arr[n][m];
  return 0;
}


Fixed submission:
#include <iostream>

using namespace std;

int main(int argc, const char *argv[]) {
  long long n, m;
  cin >> n >> m;
  long long arr[n][2];
  for (long long i = 0; i < n; i++) {
    cin >> arr[i][0] >> arr[i][1];
  }
  long long dp[n + 1][m + 1];
  for (long long j = 0; j < m + 1; j++) {
    for (long long i = 0; i < n + 1; i++) {
      dp[i][j] = 0;
    }
  }

  for (long long i = 1; i < n + 1; i++) {
    for (long long j = 0; j < m + 1; j++) {
      if (arr[i - 1][0] > j) {
        dp[i][j] = dp[i - 1][j];
      } else {
        dp[i][j] =
            max(dp[i - 1][j], arr[i - 1][1] + dp[i - 1][j - arr[i - 1][0]]);
      }
    }
  }

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

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

int main() {
  long long int n, W1, w[100001] = {0}, j, temp, temp2, max1;
  cin >> n >> W1;
  for (int i = 0; i < W1; i++) {
    cin >> temp >> temp2;
    j = W1;
    while (j - temp >= 0) {
      if (w[j - temp]) {
        w[j] = max(w[j], w[j - temp] + temp2);
      }
      j--;
    }
    if (w[temp] < temp2) {
      w[temp] = temp2;
    }
  }
  max1 = w[0];
  for (int i = 1; i <= W1; i++) {
    if (max1 < w[i]) {
      max1 = w[i];
    }
  }
  cout << max1;
  return 0;
}

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

int main() {
  long long int n, W1, w[100001] = {0}, j, temp, temp2, max1;
  cin >> n >> W1;
  for (int i = 0; i < n; i++) {
    cin >> temp >> temp2;
    j = W1;
    while (j - temp >= 0) {
      if (w[j - temp]) {
        w[j] = max(w[j], w[j - temp] + temp2);
      }
      j--;
    }
    if (w[temp] < temp2) {
      w[temp] = temp2;
    }
  }
  max1 = w[0];
  for (int i = 1; i <= W1; i++) {
    if (max1 < w[i]) {
      max1 = w[i];
    }
  }
  cout << max1;
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long int res[103], i, j, a, b, n, x;
int main() {
  cin >> n >> x;
  for (i = 0; i < n; i++) {
    scanf("%lld%lld", &a, &b);
    for (j = x - a; j >= 0; j--)
      res[j + a] = max(res[j + a], res[j] + b);
  }
  cout << res[x];
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long int res[100001], i, j, a, b, n, x;
int main() {
  cin >> n >> x;
  for (i = 0; i < n; i++) {
    scanf("%lld%lld", &a, &b);
    for (j = x - a; j >= 0; j--)
      res[j + a] = max(res[j + a], res[j] + b);
  }
  cout << res[x];
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define w(t)                                                                   \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define pb push_back
#define mk make_pair
#define ascSort(v) sort(v.begin(), v.end())
#define descSort(v) sort(v.begin(), v.end(), greater<int>())
#define ff first
#define ss second
#define pi pair<int, int>
#define vi vector<int>
#define umapi unordered_map<int, int>

const int m = 998244353;
using namespace std;

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

int32_t main() {

  FIO();

  int n, mx;
  cin >> n >> mx;
  int w[n], v[n], dp[mx + 1];
  for (int i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
  }
  for (int i = 0; i <= mx; i++) {
    dp[i] = -1;
  }
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = mx; j >= 0; j--) {
      if (dp[j] != -1 && j + w[i] <= mx)
        dp[j + w[i]] = max(dp[j + w[i]], dp[j] + v[i]);
    }
  }

  int ret = dp[0];
  for (int i = 0; i <= mx; i++) {
    ret = max(ret, dp[i]);
  }

  cout << ret << "\n";

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define w(t)                                                                   \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define pb push_back
#define mk make_pair
#define ascSort(v) sort(v.begin(), v.end())
#define descSort(v) sort(v.begin(), v.end(), greater<int>())
#define ff first
#define ss second
#define pi pair<int, int>
#define vi vector<int>
#define umapi unordered_map<int, int>

const int m = 998244353;
using namespace std;

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

int32_t main() {

  // FIO();

  int n, mx;
  cin >> n >> mx;
  int w[n], v[n], dp[mx + 1];
  for (int i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
  }
  for (int i = 0; i <= mx; i++) {
    dp[i] = -1;
  }
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = mx; j >= 0; j--) {
      if (dp[j] != -1 && j + w[i] <= mx)
        dp[j + w[i]] = max(dp[j + w[i]], dp[j] + v[i]);
    }
  }

  int ret = dp[0];
  for (int i = 0; i <= mx; i++) {
    ret = max(ret, dp[i]);
  }

  cout << ret << "\n";

  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define debug(x)                                                               \
  cout << '[' << #x << " is: " << x << "] "                                    \
       << "\n";
#define sor(v) sort(v.begin(), v.end())
#define rsor(v) sort(v.rbegin(), v.rend())
#define rev(v) reverse(v.begin(), v.end())
#define print(v)                                                               \
  for (auto f : v)                                                             \
    cout << f << " ";
#define printpair(v)                                                           \
  for (auto f : v)                                                             \
    cout << f.first << " " << f.second << endl;
#define int long long
vector<int> wt, v;
vector<vector<int>> dp(1e2, vector<int>(1e5, -1));
int n, w;
int calc(int idx, int c) {

  if (idx == 0 || c == 0)
    return dp[idx][c] = 0;
  if (dp[idx][c] != -1)
    return dp[idx][c];
  int firstc = -1, secondc = -1;
  if (c < wt[idx])
    return dp[idx][c] = calc(idx - 1, c);
  else {
    firstc = v[idx] + calc(idx - 1, c - wt[idx]);
    secondc = calc(idx - 1, c);
  }
  return dp[idx][c] = max(firstc, secondc);
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> w;
  v.resize(n + 1), wt.resize(n + 1);
  for (int x = 1; x <= n; x++)
    cin >> wt[x] >> v[x];
  cout << calc(n, w);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define debug(x)                                                               \
  cout << '[' << #x << " is: " << x << "] "                                    \
       << "\n";
#define sor(v) sort(v.begin(), v.end())
#define rsor(v) sort(v.rbegin(), v.rend())
#define rev(v) reverse(v.begin(), v.end())
#define print(v)                                                               \
  for (auto f : v)                                                             \
    cout << f << " ";
#define printpair(v)                                                           \
  for (auto f : v)                                                             \
    cout << f.first << " " << f.second << endl;
#define int long long
vector<int> wt, v;
vector<vector<int>> dp(1e2 + 1, vector<int>(1e5 + 1, -1));
int n, w;
int calc(int idx, int c) {

  if (idx == 0 || c == 0)
    return dp[idx][c] = 0;
  if (dp[idx][c] != -1)
    return dp[idx][c];
  int firstc = -1, secondc = -1;
  if (c < wt[idx])
    return dp[idx][c] = calc(idx - 1, c);
  else {
    firstc = v[idx] + calc(idx - 1, c - wt[idx]);
    secondc = calc(idx - 1, c);
  }
  return dp[idx][c] = max(firstc, secondc);
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> w;
  v.resize(n + 1), wt.resize(n + 1);
  for (int x = 1; x <= n; x++)
    cin >> wt[x] >> v[x];
  cout << calc(n, w);

  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define debug(x)                                                               \
  cout << '[' << #x << " is: " << x << "] "                                    \
       << "\n";
#define sor(v) sort(v.begin(), v.end())
#define rsor(v) sort(v.rbegin(), v.rend())
#define rev(v) reverse(v.begin(), v.end())
#define print(v)                                                               \
  for (auto f : v)                                                             \
    cout << f << " ";
#define printpair(v)                                                           \
  for (auto f : v)                                                             \
    cout << f.first << " " << f.second << endl;
#define int long long
vector<int> wt, v;
vector<vector<int>> dp(1e2, vector<int>(1e5, -1));
int n, w;
int calc(int idx, int c) {
  int res;
  if (idx == 0 || c == 0)
    return dp[idx][c] = 0;
  if (dp[idx][c] != -1)
    return dp[idx][c];
  int firstc = -1, secondc = -1;
  if (c < wt[idx])
    res = calc(idx - 1, c);
  else {
    firstc = v[idx] + calc(idx - 1, c - wt[idx]);
    secondc = calc(idx - 1, c);
    res = max(firstc, secondc);
  }
  return dp[idx][c] = res;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> w;
  v.resize(n + 1), wt.resize(n + 1);
  for (int x = 1; x <= n; x++)
    cin >> wt[x] >> v[x];
  cout << calc(n, w);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define debug(x)                                                               \
  cout << '[' << #x << " is: " << x << "] "                                    \
       << "\n";
#define sor(v) sort(v.begin(), v.end())
#define rsor(v) sort(v.rbegin(), v.rend())
#define rev(v) reverse(v.begin(), v.end())
#define print(v)                                                               \
  for (auto f : v)                                                             \
    cout << f << " ";
#define printpair(v)                                                           \
  for (auto f : v)                                                             \
    cout << f.first << " " << f.second << endl;
#define int long long
vector<int> wt, v;
vector<vector<int>> dp(1e2 + 1, vector<int>(1e5 + 1, -1));
int n, w;
int calc(int idx, int c) {
  int res;
  if (idx == 0 || c == 0)
    return dp[idx][c] = 0;
  if (dp[idx][c] != -1)
    return dp[idx][c];
  int firstc = -1, secondc = -1;
  if (c < wt[idx])
    res = calc(idx - 1, c);
  else {
    firstc = v[idx] + calc(idx - 1, c - wt[idx]);
    secondc = calc(idx - 1, c);
    res = max(firstc, secondc);
  }
  return dp[idx][c] = res;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> w;
  v.resize(n + 1), wt.resize(n + 1);
  for (int x = 1; x <= n; x++)
    cin >> wt[x] >> v[x];
  cout << calc(n, w);

  return 0;
}
Problem p03163 (C++)

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

const int MAXN = 1e2 + 5;
const int MAXNN = 1e5 + 7;
const int INF = 1e9 + 7;
#define ll long long
#define pb push_back
#define endl '\n'
#define fast_io                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define F first
#define S second

ll poww(ll a, ll b, ll md) {
  return (!b ? 1
             : (b & 1 ? a * poww(a * a % md, b / 2, md) % md
                      : poww(a * a % md, b / 2, md) % md));
}

ll MOD(ll a) { return ((a % INF) + INF) % INF; }

ll inv(ll a) { return poww(a, INF - 2, INF); }

ll w[MAXN], val[MAXN], dp[MAXN][MAXN];

int main() {
  fast_io;
  // cout << fixed << setprecision(15);

  int n, cap;
  cin >> n >> cap;
  for (int i = 1; i <= n; ++i)
    cin >> w[i] >> val[i];

  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= cap; ++j) {
      dp[i][j] = dp[i - 1][j];
      if (j >= w[i])
        dp[i][j] = max(dp[i][j], dp[i - 1][j - w[i]] + val[i]);
    }
  }
  cout << dp[n][cap];
}


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

const int MAXN = 1e2 + 5;
const int MAXNN = 1e5 + 7;
const int INF = 1e9 + 7;
#define ll long long
#define pb push_back
#define endl '\n'
#define fast_io                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define F first
#define S second

ll poww(ll a, ll b, ll md) {
  return (!b ? 1
             : (b & 1 ? a * poww(a * a % md, b / 2, md) % md
                      : poww(a * a % md, b / 2, md) % md));
}

ll MOD(ll a) { return ((a % INF) + INF) % INF; }

ll inv(ll a) { return poww(a, INF - 2, INF); }

ll w[MAXN], val[MAXN], dp[MAXN][MAXNN];

int main() {
  fast_io;
  // cout << fixed << setprecision(15);

  int n, cap;
  cin >> n >> cap;
  for (int i = 1; i <= n; ++i)
    cin >> w[i] >> val[i];

  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= cap; ++j) {
      dp[i][j] = dp[i - 1][j];
      if (j >= w[i])
        dp[i][j] = max(dp[i][j], dp[i - 1][j - w[i]] + val[i]);
    }
  }
  cout << dp[n][cap];
}