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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <ctype.h>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <vector>

#define ll long long

using namespace std;

void solve() {
  ll n, w;
  cin >> n >> w;

  vector<ll> dp(10005, -1);
  dp[0] = 0;

  ll x, v;
  for (ll i = 0; i < n; i++) {
    cin >> x >> v;

    for (ll j = w; j >= 0; j--)
      if (dp[j] >= 0 && j + x <= w &&
          (dp[j + x] == -1 || dp[j + x] < dp[j] + v))
        dp[j + x] = dp[j] + v;
  }

  ll ans = 0;
  for (ll i = 0; i <= w; i++)
    if (dp[i] >= 0)
      ans = max(ans, dp[i]);
  cout << ans << endl;
}

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

  ll t = 1;
  // cin>>t;

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

  return 0;
}


Fixed submission:
#include <algorithm>
#include <ctype.h>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <vector>

#define ll long long

using namespace std;

void solve() {
  ll n, w;
  cin >> n >> w;

  vector<ll> dp(100005, -1);
  dp[0] = 0;

  ll x, v;
  for (ll i = 0; i < n; i++) {
    cin >> x >> v;

    for (ll j = w; j >= 0; j--)
      if (dp[j] >= 0 && j + x <= w &&
          (dp[j + x] == -1 || dp[j + x] < dp[j] + v))
        dp[j + x] = dp[j] + v;
  }

  ll ans = 0;
  for (ll i = 0; i <= w; i++)
    if (dp[i] >= 0)
      ans = max(ans, dp[i]);
  cout << ans << endl;
}

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

  ll t = 1;
  // cin>>t;

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

  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
///"Bismillahir Rahmanir Raheem"///
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef vector<pii> vii;
typedef vector<pil> vil;
typedef vector<pli> vli;

#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define sz size()
#define all(a) a.begin(), a.end()
#define mem(a, b) memset(a, b, sizeof(a))
#define f0(i, b) for (int i = 0; i < (b); i++)
#define f1(i, b) for (int i = 1; i <= (b); i++)
#define f2(i, a, b) for (int i = (a); i <= (b); i++)
#define fr(i, b, a) for (int i = (b); i >= (a); i--)
#define rep(i, a, b, c) for (int i = (a); i != (b); i += (c))

int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy8[] = {1, -1, 1, -1, 0, 0, -1, 1};

const double PI = acos(-1.0);
const double EPS = 1e-6;
const int MOD = (int)1e9 + 7;
const int maxn = (int)2e5 + 5;
const int LOGN = 20;
const int N = 110;
const int W = 100010;
long long int dp[N][W];
int main() {
  int n, w;
  cin >> n >> w;
  // vector<int> dp(n+1,0);
  vector<int> pf(n);
  vector<int> wt(n);

  f1(i, n) { cin >> wt[i] >> pf[i]; }

  f1(i, n) {
    f1(j, w) {
      if (j - wt[i] >= 0)
        dp[i][j] = max(dp[i - 1][j], (dp[i - 1][j - wt[i]] + pf[i]));
      else {
        dp[i][j] = dp[i - 1][j];
      }
    }
  }
  cout << dp[n][w] << endl;
  /*for(int i = 0; i<=n; i++){
      for(int j =0;j<=bw;j++){
          cout<<dp[i][j]<<" ";
      }
      cout<<endl;
  }*/
  return 0;
}


Fixed submission:
///"Bismillahir Rahmanir Raheem"///
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef vector<pii> vii;
typedef vector<pil> vil;
typedef vector<pli> vli;

#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define sz size()
#define all(a) a.begin(), a.end()
#define mem(a, b) memset(a, b, sizeof(a))
#define f0(i, b) for (int i = 0; i < (b); i++)
#define f1(i, b) for (int i = 1; i <= (b); i++)
#define f2(i, a, b) for (int i = (a); i <= (b); i++)
#define fr(i, b, a) for (int i = (b); i >= (a); i--)
#define rep(i, a, b, c) for (int i = (a); i != (b); i += (c))

int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy8[] = {1, -1, 1, -1, 0, 0, -1, 1};

const double PI = acos(-1.0);
const double EPS = 1e-6;
const int MOD = (int)1e9 + 7;
const int maxn = (int)2e5 + 5;
const int LOGN = 20;
const int N = 110;
const int W = 100010;
long long int dp[N][W];
int main() {
  int n, w;
  cin >> n >> w;
  // vector<int> dp(n+1,0);
  vector<int> pf(N);
  vector<int> wt(N);

  f1(i, n) { cin >> wt[i] >> pf[i]; }

  f1(i, n) {
    f1(j, w) {
      if (j - wt[i] >= 0)
        dp[i][j] = max(dp[i - 1][j], (dp[i - 1][j - wt[i]] + pf[i]));
      else {
        dp[i][j] = dp[i - 1][j];
      }
    }
  }
  cout << dp[n][w] << endl;
  /*for(int i = 0; i<=n; i++){
      for(int j =0;j<=bw;j++){
          cout<<dp[i][j]<<" ";
      }
      cout<<endl;
  }*/
  return 0;
}
Problem p03163 (C++)

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

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

  ll wt[N];
  ll val[N];

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

  ll dp[W + 1][N + 1];

  for (ll w = 0; w <= W; w++) {
    for (ll i = 0; i <= N; i++) {
      if (w == 0 || i == 0) {
        dp[w][i] = 0;
      }

      else {

        if (wt[i - 1] <= w) {
          dp[w][i] = max(dp[w][i - 1], dp[w - wt[i]][i - 1] + val[i - 1]);
        }

        else {
          dp[w][i] = dp[w][i - 1];
        }
      }
    }
  }
  cout << dp[W][N] << endl;
}


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

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

  ll wt[N];
  ll val[N];

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

  ll dp[W + 1][N + 1];

  for (ll w = 0; w <= W; w++) {
    for (ll i = 0; i <= N; i++) {
      if (w == 0 || i == 0) {
        dp[w][i] = 0;
      }

      else {

        if (wt[i - 1] <= w) {
          dp[w][i] = max(dp[w][i - 1], dp[w - wt[i - 1]][i - 1] + val[i - 1]);
        }

        else {
          dp[w][i] = dp[w][i - 1];
        }
      }
    }
  }
  cout << dp[W][N] << endl;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
#define MOD 1000000007
#define MAX 10000000
using namespace std;

int main() {
  ll n, W, w, v;
  cin >> n >> W;
  vector<pair<ll, ll>> knap;
  for (int i = 0; i < n; i++) {
    cin >> w >> v;
    knap.push_back({w, v});
  }
  ll k[n + 1][W + 1];
  for (int i = 0; i <= n; i++) {
    for (int s = 0; s <= W; s++) {
      if (i == 0 || s == 0) {
        k[i][s] = 0;
      } else if (knap[i - 1].first <= s) {
        k[i][s] =
            max(knap[i].second + k[i - 1][s - knap[i].first], k[i - 1][s]);
      } else {
        k[i][s] = k[i - 1][s];
      }
    }
  }
  cout << k[n][W] << endl;
  return 0;
}

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

int main() {
  ll n, W, w, v;
  cin >> n >> W;
  vector<pair<ll, ll>> knap;
  for (int i = 0; i < n; i++) {
    cin >> w >> v;
    knap.push_back({w, v});
  }
  ll k[n + 1][W + 1];
  for (int i = 0; i <= n; i++) {
    for (int s = 0; s <= W; s++) {
      if (i == 0 || s == 0) {
        k[i][s] = 0;
      } else if (knap[i - 1].first <= s) {
        k[i][s] = max(knap[i - 1].second + k[i - 1][s - knap[i - 1].first],
                      k[i - 1][s]);
      } else {
        k[i][s] = k[i - 1][s];
      }
    }
  }
  cout << k[n][W] << endl;
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
// #include <iostream>
#include <queue>
#include <stdio.h>
#include <vector>
// #include <algorithm>
#define pb push_back
#define boost                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define f first
#define s second
// #define cerr if(0)cerr
using namespace std;
typedef long long LL;
typedef pair<int, LL> PILL;

const int N = 110;
const LL I = 1e18;
int n, W;
PILL T[N];
LL DP[N];

LL maxi(LL x, LL y) {
  if (y > x)
    return y;
  return x;
}

int main() {
  // boost;
  scanf("%d%d", &n, &W);
  for (int i = 1; i <= n; i++) {
    scanf("%d%lld", &T[i].f, &T[i].s);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = W; j >= T[i].f; j--) {
      DP[j] = maxi(DP[j], DP[j - T[i].f] + T[i].s);
    }
  }
  printf("%lld", DP[W]);

  return 0;
}


Fixed submission:
// #include <iostream>
#include <queue>
#include <stdio.h>
#include <vector>
// #include <algorithm>
#define pb push_back
#define boost                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define f first
#define s second
// #define cerr if(0)cerr
using namespace std;
typedef long long LL;
typedef pair<int, LL> PILL;

const int N = 110;
const LL I = 1e18;
int n, W;
PILL T[N];
LL DP[100010];

LL maxi(LL x, LL y) {
  if (y > x)
    return y;
  return x;
}

int main() {
  // boost;
  scanf("%d%d", &n, &W);
  for (int i = 1; i <= n; i++) {
    scanf("%d%lld", &T[i].f, &T[i].s);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = W; j >= T[i].f; j--) {
      DP[j] = maxi(DP[j], DP[j - T[i].f] + T[i].s);
    }
  }
  printf("%lld", DP[W]);

  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#define min(a, b) ((a < b) ? a : b)
#define max(a, b) ((a > b) ? a : b)
#define ll long long int
#define BIG 1e9
using namespace std;

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

Fixed submission:
#include <iostream>
#define min(a, b) ((a < b) ? a : b)
#define max(a, b) ((a > b) ? a : b)
#define ll long long int
#define BIG 1e9
using namespace std;

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pes first
#define val second
#define ll long long

using namespace std;

int const MMM = 105;
int n, m;
pair<ll, ll> arr[MMM];
ll dp[MMM];

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

  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    cin >> arr[i].first >> arr[i].second;
  for (int i = 1; i <= n; i++) {
    for (int j = m; j >= arr[i].pes; j--) {
      dp[j] = max(dp[j], dp[j - arr[i].pes] + arr[i].val);
    }
  }
  cout << dp[m];

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define pes first
#define val second
#define ll long long

using namespace std;

int const MMM = 105;
int n, m;
pair<ll, ll> arr[MMM];
ll dp[100099];

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

  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    cin >> arr[i].first >> arr[i].second;
  for (int i = 1; i <= n; i++) {
    for (int j = m; j >= arr[i].pes; j--) {
      dp[j] = max(dp[j], dp[j - arr[i].pes] + arr[i].val);
    }
  }
  cout << dp[m];

  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int n, w, value[105] = {}, weight[105] = {};
long long dp[101][100002];
long long findSolution(int pos, int W) {
  if (pos < 0 || W < 0) {
    return 0;
  }
  if (weight[pos] > W) {
    long long x = findSolution(pos - 1, W);
    // cout << "phase 1: "<<x <<" "<< pos << " " << W << endl;
    // cout << x << endl;
    return x;
  } else {
    long long x = value[pos] + findSolution(pos - 1, W - weight[pos]);
    // cout << "phase 2: "<< x << " "<< pos << " " << W << endl;
    long long y = findSolution(pos - 1, W);
    // cout << "phase 1: "<<y << " "<< pos << " " << W << endl;
    // cout << x << " " << y << endl;
    return dp[pos][W] = max(x, y);
  }
  // cout << pos << " " << W << endl;
}
int main() {
  scanf("%d %d", &n, &w);
  for (int i = 0; i < n; i++) {
    scanf("%d %d", &weight[i], &value[i]);
  }
  long long Max = findSolution(n, w);
  printf("%lld\n", Max);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, w, value[105] = {}, weight[105] = {};
long long dp[101][100002];
long long findSolution(int pos, int W) {
  if (pos < 0 || W < 0) {
    return 0;
  }
  if (dp[pos][W] != 0) {
    return dp[pos][W];
  }
  if (weight[pos] > W) {
    long long x = findSolution(pos - 1, W);
    // cout << "phase 1: "<<x <<" "<< pos << " " << W << endl;
    // cout << x << endl;
    return x;
  } else {
    long long x = value[pos] + findSolution(pos - 1, W - weight[pos]);
    // cout << "phase 2: "<< x << " "<< pos << " " << W << endl;
    long long y = findSolution(pos - 1, W);
    // cout << "phase 1: "<<y << " "<< pos << " " << W << endl;
    // cout << x << " " << y << endl;
    return dp[pos][W] = max(x, y);
  }
  // cout << pos << " " << W << endl;
}
int main() {
  scanf("%d %d", &n, &w);
  for (int i = 0; i < n; i++) {
    scanf("%d %d", &weight[i], &value[i]);
  }
  long long Max = findSolution(n, w);
  printf("%lld\n", Max);
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
// in progress
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define fr(i, a, b) for (int i = (int)(a); i <= (int)(b); i++)
#define forn(i, n) for (int i = 0; i < (int)(n); i++)
#define rfr(i, a, b) for (int i = (int)(a); i >= (int)(b); i--)
#define PI 3.14159265
ll n, w;
ll a[1005][2], rec[105][100010];
ll recur(ll ind, ll cw) {
  if (ind >= n)
    return 0;
  if (rec[ind + 1][cw] == -1)
    rec[ind + 1][cw] = recur(ind + 1, cw);
  if (rec[ind + 1][cw + a[ind][0]] == -1)
    rec[ind + 1][cw + a[ind][0]] = recur(ind + 1, cw + a[ind][0]);
  if (cw + a[ind][0] <= w)
    return max(rec[ind + 1][cw + a[ind][0]] + a[ind][1], rec[ind + 1][cw]);
  else
    return rec[ind + 1][cw];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // freopen("a.in", "r", stdin);
  // freopen("a.out", "w", stdout);
  cin >> n >> w;
  memset(rec, -1, sizeof(rec));
  for (int i = 0; i < n; i++)
    cin >> a[i][0] >> a[i][1];
  cout << recur(0, 0) << "\n";
  return 0;
}


Fixed submission:
// in progress
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define fr(i, a, b) for (int i = (int)(a); i <= (int)(b); i++)
#define forn(i, n) for (int i = 0; i < (int)(n); i++)
#define rfr(i, a, b) for (int i = (int)(a); i >= (int)(b); i--)
#define PI 3.14159265
ll n, w;
ll a[1005][2], rec[105][100010];
ll recur(ll ind, ll cw) {
  if (ind >= n)
    return 0;
  if (rec[ind + 1][cw] == -1)
    rec[ind + 1][cw] = recur(ind + 1, cw);
  if (cw + a[ind][0] <= w && rec[ind + 1][cw + a[ind][0]] == -1)
    rec[ind + 1][cw + a[ind][0]] = recur(ind + 1, cw + a[ind][0]);
  if (cw + a[ind][0] <= w)
    return max(rec[ind + 1][cw + a[ind][0]] + a[ind][1], rec[ind + 1][cw]);
  else
    return rec[ind + 1][cw];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // freopen("a.in", "r", stdin);
  // freopen("a.out", "w", stdout);
  cin >> n >> w;
  memset(rec, -1, sizeof(rec));
  for (int i = 0; i < n; i++)
    cin >> a[i][0] >> a[i][1];
  cout << recur(0, 0) << "\n";
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int main() {
  int n, lim;
  long long w[105], v[105], dp[105][1005];
  cin >> n >> lim;
  for (int i = 1; i <= n; i++) {
    cin >> w[i] >> v[i];
  }
  for (int i = 1; i <= lim; i++) {
    if (i < w[1])
      dp[1][i] = 0;
    else
      dp[1][i] = v[1];
  }
  for (int i = 2; i <= n; i++) {
    for (int j = 1; j <= lim; j++) {
      if (j < w[i])
        dp[i][j] = dp[i - 1][j];
      else
        dp[i][j] = max(dp[i - 1][j - w[i]] + v[i], dp[i - 1][j]);
    }
  }
  cout << dp[n][lim];
}

Fixed submission:
#include <iostream>
using namespace std;
int main() {
  int n, lim;
  long long w[105], v[105], dp[105][100005];
  cin >> n >> lim;
  for (int i = 1; i <= n; i++) {
    cin >> w[i] >> v[i];
  }
  for (int i = 1; i <= lim; i++) {
    if (i < w[1])
      dp[1][i] = 0;
    else
      dp[1][i] = v[1];
  }
  for (int i = 2; i <= n; i++) {
    for (int j = 1; j <= lim; j++) {
      if (j < w[i])
        dp[i][j] = dp[i - 1][j];
      else
        dp[i][j] = max(dp[i - 1][j - w[i]] + v[i], dp[i - 1][j]);
    }
  }
  cout << dp[n][lim];
}
Problem p03163 (C++)

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

using namespace std;

struct item {
  long val;
  long weight;
};

int compare(item left_item, item right_item) {
  return left_item.weight <= right_item.weight;
}

int main() {

  int N, W;

  cin >> N >> W;

  vector<vector<long long>> DP(W + 1, vector<long long>(N, 0));

  item temp = {0, 0};
  vector<item> items_arr(N, temp);

  // vector<int> Weights(N,0);
  // vector<int> Value(N,0);

  for (int k = 0; k < N; k++) {

    cin >> items_arr[k].weight >> items_arr[k].val;
  }

  // init
  sort(items_arr.begin(), items_arr.end(), compare);

  // for(auto x:items_arr)cout<<x.weight<<endl;
  for (int i = 1; i <= W; i++) {
    if (i >= items_arr[0].weight) {
      DP[i][0] = items_arr[0].val;
    }
  }

  for (int i = 1; i <= W; i++) {
    for (int j = 1; j < N; j++) {
      if (i >= items_arr[j].weight) {
        DP[i][j] = max(items_arr[j].val + DP[i - items_arr[j].weight][j - 1],
                       DP[i][j - 1]);
        // if(i==3&&j==2)cout<<items_arr[j].val+DP[i-items_arr[j].weight][j-1]<<"
        // "<<DP[i][j-1]<<endl;
      } else {
        DP[i][j] = DP[i][j - 1];
      }
      // cout<<i<<" "<<j<<" "<<DP[i][j]<<endl;
    }
  }

  cout << DP[W][N - 1] << endl;

  // system("pause");
  return 0;
}


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

using namespace std;

struct item {
  long val;
  long weight;
};

int compare(item left_item, item right_item) {
  return left_item.weight <= right_item.weight;
}

int main() {

  int N, W;

  cin >> N >> W;

  vector<vector<long long>> DP(W + 1, vector<long long>(N, 0));

  item temp = {0, 0};
  vector<item> items_arr(N, temp);

  // vector<int> Weights(N,0);
  // vector<int> Value(N,0);

  for (int k = 0; k < N; k++) {

    cin >> items_arr[k].weight >> items_arr[k].val;
  }

  // init
  // sort(items_arr.begin(),items_arr.end(),compare);

  // for(auto x:items_arr)cout<<x.weight<<endl;
  for (int i = 1; i <= W; i++) {
    if (i >= items_arr[0].weight) {
      DP[i][0] = items_arr[0].val;
    }
  }

  for (int i = 1; i <= W; i++) {
    for (int j = 1; j < N; j++) {
      if (i >= items_arr[j].weight) {
        DP[i][j] = max(items_arr[j].val + DP[i - items_arr[j].weight][j - 1],
                       DP[i][j - 1]);
        // if(i==3&&j==2)cout<<items_arr[j].val+DP[i-items_arr[j].weight][j-1]<<"
        // "<<DP[i][j-1]<<endl;
      } else {
        DP[i][j] = DP[i][j - 1];
      }
      // cout<<i<<" "<<j<<" "<<DP[i][j]<<endl;
    }
  }

  cout << DP[W][N - 1] << endl;

  // system("pause");
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
using namespace std;
typedef long long int ll;
static const int MAX_N = 100;
static const int MAX_W = 10000;

int N, W;
int w[MAX_N], v[MAX_N];
ll memo[MAX_N + 1][MAX_W + 1];

int main() {
  scanf("%d %d", &N, &W);
  for (int i = 0; i < N; i++)
    scanf("%d %d", &w[i], &v[i]);
  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= W; j++) {
      if (j < w[i])
        memo[i + 1][j] = memo[i][j];
      else
        memo[i + 1][j] = max(memo[i][j], memo[i][j - w[i]] + v[i]);
    }
  }
  printf("%lld\n", memo[N][W]);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
using namespace std;
typedef long long int ll;
static const int MAX_N = 100;
static const int MAX_W = 100000;

int N, W;
int w[MAX_N], v[MAX_N];
ll memo[MAX_N + 1][MAX_W + 1];

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define F first
#define S second
#define pb emplace_back
#define pi pair<int, int>
using namespace std;
const int maxn = 1e2 + 4;

int n, w;
int W[maxn], V[maxn];
ll dp[maxn][10004];

int main() {
  cin >> n >> w;
  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 (W[i] <= j) {
        dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - W[i]] + V[i] * 1ll);
      } else {
        dp[i][j] = dp[i - 1][j];
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i <= w; ++i) {
    ans = max(ans, dp[n - 1][i]);
  }
  cout << ans << '\n';
}

Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define F first
#define S second
#define pb emplace_back
#define pi pair<int, int>
using namespace std;
const int maxn = 1e2 + 4;

int n, w;
int W[maxn], V[maxn];
ll dp[maxn][100004];

int main() {
  cin >> n >> w;
  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 (W[i] <= j) {
        dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - W[i]] + V[i] * 1ll);
      } else {
        dp[i][j] = dp[i - 1][j];
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i <= w; ++i) {
    ans = max(ans, dp[n - 1][i]);
  }
  cout << ans << '\n';
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
struct node {
  ll val, wt;
};
int cmp(node a, node b) { return a.wt < b.wt; }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif // ONLINE_JUDGE
  ll n, w;
  cin >> n >> w;
  node arr[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> arr[i].wt >> arr[i].val;
  sort(arr + 1, arr + n, cmp);
  ll dp[n + 1][w + 1];
  memset(dp, 0, sizeof(dp));
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= w; j++) {
      if (j - arr[i].wt >= 0) {
        dp[i][j] = max((arr[i].val + dp[i - 1][j - arr[i].wt]),
                       max(dp[i - 1][j], dp[i][j - 1]));
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }
  cout << dp[n][w];
  // code goes here
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
struct node {
  ll val, wt;
};
int cmp(node a, node b) { return a.wt < b.wt; }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n, w;
  cin >> n >> w;
  node arr[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> arr[i].wt >> arr[i].val;
  sort(arr + 1, arr + n, cmp);
  ll dp[n + 1][w + 1];
  memset(dp, 0, sizeof(dp));
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= w; j++) {
      if (j - arr[i].wt >= 0) {
        dp[i][j] = max((arr[i].val + dp[i - 1][j - arr[i].wt]),
                       max(dp[i - 1][j], dp[i][j - 1]));
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }
  cout << dp[n][w];
  // code goes here
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
int dp[123][123];
int N, W, price[123], wt[123];
int32_t main() {
  cin >> N >> W;
  for (int i = 0; i < N; i++) {
    cin >> wt[i] >> price[i];
  }

  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= W; j++) {
      if (wt[i - 1] <= j) {
        dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - wt[i - 1]] + price[i - 1]);
      } else
        dp[i][j] = dp[i - 1][j];
    }
  }

  cout << dp[N][W] << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
int dp[123][123456];
int N, W, price[123], wt[123];
int32_t main() {
  cin >> N >> W;
  for (int i = 0; i < N; i++) {
    cin >> wt[i] >> price[i];
  }

  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= W; j++) {
      if (wt[i - 1] <= j) {
        dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - wt[i - 1]] + price[i - 1]);
      } else
        dp[i][j] = dp[i - 1][j];
    }
  }

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

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
const int INF = 1e9 + 5;

void max_self(ll &a, ll b) { a = max(a, b); }

int main() {
  ll N, W;
  cin >> N >> W;
  vector<ll> dp(W + 1);
  // dp[i] the maximum total value with weight exactly
  for (ll item = 0; item < N; item++) {
    ll weight, value;
    cin >> weight >> value;
    for (int weight_already = W; weight_already >= 0; weight_already--) {
      max_self(dp[weight_already + weight], dp[weight_already] + value);
    }
  }
  ll answer = 0;
  for (ll i = 0; i <= W; i++) {
    max_self(answer, dp[i]);
  }
  cout << answer;

  return 0;
}


Fixed submission:

#include <bits/stdc++.h>
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
const int INF = 1e9 + 5;

void max_self(ll &a, ll b) { a = max(a, b); }

int main() {
  ll N, W;
  cin >> N >> W;
  vector<ll> dp(W + 1);
  // dp[i] the maximum total value with weight exactly
  for (ll item = 0; item < N; item++) {
    ll weight, value;
    cin >> weight >> value;
    for (int weight_already = W - weight; weight_already >= 0;
         weight_already--) {
      max_self(dp[weight_already + weight], dp[weight_already] + value);
    }
  }
  ll answer = 0;
  for (ll i = 0; i <= W; i++) {
    max_self(answer, dp[i]);
  }
  cout << answer;

  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
/*
tarun360
IIT Indore
*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ll_s long
#define mod 1000000007
#define forn(i, start, lim) for (ll i = start; i < lim; i++)
#define forn_d(i, start, lim) for (ll i = start; i >= lim; i--)
#define forn_s(i, start, lim) for (ll_s i = start; i < lim; i++)
#define forn_d_s(i, start, lim) for (ll_s i = start; i >= lim; i--)
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define debug1(a) cout << a << endl
#define debug2(a, b) cout << a << " " << b << endl
#define debug3(a, b, c) cout << a << " " << b << " " << c << endl
#define debug4(a) cout << "chu " << a << endl

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll n, W;
  cin >> n >> W;
  forn(i, 1, n + 1) cin >> w[i] >> v[i];
  forn(i, 1, n + 1) {
    forn(j, 1, W + 1) {
      if (j - w[i] < 0)
        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][W] << endl;
  return 0;
}


Fixed submission:
/*
tarun360
IIT Indore
*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ll_s long
#define mod 1000000007
#define forn(i, start, lim) for (ll i = start; i < lim; i++)
#define forn_d(i, start, lim) for (ll i = start; i >= lim; i--)
#define forn_s(i, start, lim) for (ll_s i = start; i < lim; i++)
#define forn_d_s(i, start, lim) for (ll_s i = start; i >= lim; i--)
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define debug1(a) cout << a << endl
#define debug2(a, b) cout << a << " " << b << endl
#define debug3(a, b, c) cout << a << " " << b << " " << c << endl
#define debug4(a) cout << "chu " << a << endl

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll n, W;
  cin >> n >> W;
  forn(i, 1, n + 1) cin >> w[i] >> v[i];
  forn(i, 1, n + 1) {
    forn(j, 1, W + 1) {
      if (j - w[i] < 0)
        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][W] << endl;
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cstdlib>
#include <ext/pb_ds/assoc_container.hpp>
#include <iostream>
#include <map>
#include <math.h>
#include <random>
#include <set>
#include <stdio.h>
#include <vector>

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;

typedef long long ll;

const ll INF = 1e18, MOD = 1e9 + 7;
const int INT_MAX = 1e9;

ll n, W;

ll w[100], v[100], d[100], mx;

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

  for (int i = 0; i < n; i++) {
    scanf("%lld%lld", &w[i], &v[i]);
  }

  ll sum = 0;

  for (int i = 0; i < n; i++)
    for (int j = W; j >= 0; j--) {
      if (j + w[i] <= W) {
        d[j + w[i]] = max(d[j + w[i]], d[j] + v[i]);
        mx = max(mx, d[j + w[i]]);
      }
    }

  cout << mx;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cstdlib>
#include <ext/pb_ds/assoc_container.hpp>
#include <iostream>
#include <map>
#include <math.h>
#include <random>
#include <set>
#include <stdio.h>
#include <vector>

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;

typedef long long ll;

const ll INF = 1e18, MOD = 1e9 + 7;
const int INT_MAX = 1e9;

ll n, W;

ll w[100], v[100], d[1000000], mx;

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

  for (int i = 0; i < n; i++) {
    scanf("%lld%lld", &w[i], &v[i]);
  }

  ll sum = 0;

  for (int i = 0; i < n; i++)
    for (int j = W; j >= 0; j--) {
      if (j + w[i] <= W) {
        d[j + w[i]] = max(d[j + w[i]], d[j] + v[i]);
        mx = max(mx, d[j + w[i]]);
      }
    }

  cout << mx;
}
Problem p03163 (C++)

Buggy submission (status: Time Limit Exceeded):
// By Zank100
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define mod 1000000007
#define din(t)                                                                 \
  long long t;                                                                 \
  cin >> t;
#define in(t) cin >> t;
ll k(ll W, ll w[], ll v[], ll i) {
  if (W == 0 || i == -1)
    return 0;
  if (w[i] > W) {
    return k(W, w, v, i - 1);
  } else {
    return max(v[i] + k(W - w[i], w, v, i - 1), k(W, w, v, i - 1));
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  din(n);
  din(W);
  ll w[n], v[n];
  for (ll i = 0; i < n; i++) {
    in(w[i]);
    in(v[i]);
  }
  ll ans = k(W, w, v, n - 1);
  cout << ans << endl;
  return 0;
}


Fixed submission:
// By Zank100
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define mod 1000000007
#define din(t)                                                                 \
  long long t;                                                                 \
  cin >> t;
#define in(t) cin >> t;
ll k(ll W, ll w[], ll v[], ll i) {
  if (W == 0 || i == -1)
    return 0;
  if (w[i] > W) {
    return k(W, w, v, i - 1);
  } else {
    return max(v[i] + k(W - w[i], w, v, i - 1), k(W, w, v, i - 1));
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  din(n);
  din(W);
  ll w[n], v[n];
  for (ll i = 0; i < n; i++) {
    in(w[i]);
    in(v[i]);
  }
  ll k[n + 1][W + 1];
  for (ll i = 0; i <= n; i++) {
    for (ll j = 0; j <= W; j++) {
      if (i == 0 || j == 0) {
        k[i][j] = 0;
      } else if (w[i - 1] > j) {
        k[i][j] = k[i - 1][j];
      } else {
        k[i][j] = max(v[i - 1] + k[i - 1][j - w[i - 1]], k[i - 1][j]);
      }
    }
  }
  ll ans = k[n][W];
  cout << ans << endl;
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fly                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(nullptr);                                                            \
  cout.tie(nullptr)
#define ll long long
const ll mod = 1e9 + 7;

using namespace std;
ll dp[100010][101] = {0};
int main(int argc, char const *argv[]) {
  fly;
  ll n, w;
  cin >> n >> w;
  vector<ll> weigth(n);
  vector<ll> cst(n);
  for (int i = 1; i <= n; i++) {
    cin >> weigth[i] >> cst[i];
  }
  for (ll i = 0; i <= w; i++) {
    for (ll j = 0; j <= n; j++) {
      dp[i][j] = 0;
      if (i == 0 || j == 0)
        continue;
      dp[i][j] = max(dp[i][j], dp[i][j - 1]);
      if (i >= weigth[j]) {
        dp[i][j] = max(dp[i][j], dp[i - weigth[j]][j - 1] + cst[j]);
      }
    }
  }
  ll ans = 0;
  for (ll i = 0; i <= w; i++) {
    ans = max(ans, dp[i][n]);
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define fly                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(nullptr);                                                            \
  cout.tie(nullptr)
#define ll long long
const ll mod = 1e9 + 7;

using namespace std;
ll dp[100010][101] = {0};
int main(int argc, char const *argv[]) {
  fly;
  ll n, w;
  cin >> n >> w;
  vector<ll> weigth(n + 1);
  vector<ll> cst(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> weigth[i] >> cst[i];
  }
  for (ll i = 0; i <= w; i++) {
    for (ll j = 0; j <= n; j++) {
      dp[i][j] = 0;
      if (i == 0 || j == 0)
        continue;
      dp[i][j] = max(dp[i][j], dp[i][j - 1]);
      if (i >= weigth[j]) {
        dp[i][j] = max(dp[i][j], dp[i - weigth[j]][j - 1] + cst[j]);
      }
    }
  }
  ll ans = 0;
  for (ll i = 0; i <= w; i++) {
    ans = max(ans, dp[i][n]);
  }
  cout << ans << endl;

  return 0;
}
Problem p03163 (C++)

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

#define ll long long
using namespace std;

const ll mx = 10e5 + 9;
ll n, k, weight;
ll w[mx], v[mx];
ll dp[101][mx];

ll calculation(ll item, ll need) {
  if (dp[item][weight] != -1)
    return dp[item][weight];

  if (item == n)
    return 0;

  ll t1, t2;
  t1 = t2 = 0;
  if (need - w[item] >= 0)
    t1 = v[item] + calculation(item + 1, need - w[item]);
  t2 = calculation(item + 1, need);

  return dp[item][need] = max(t1, t2);
}

void solve() {
  cin >> n >> weight;
  for (ll i = 0; i < n; ++i)
    cin >> w[i] >> v[i];

  for (ll i = 0; i < 101; ++i)
    for (ll j = 0; j < mx; ++j)
      dp[i][j] = -1;

  cout << calculation(0, weight) << "\n";
}

int main() // Md. Inzamam-ul Haque
{
  solve();
}


Fixed submission:
/*
 ALLAH is Almighty....
*/
#include <bits/stdc++.h>

#define ll long long
using namespace std;

const ll mx = 10e5 + 9;
ll n, k, weight;
ll w[mx], v[mx];
ll dp[101][mx];

ll calculation(ll item, ll need) {
  if (dp[item][need] != -1)
    return dp[item][need];

  if (item == n)
    return 0;

  ll t1, t2;
  t1 = t2 = 0;
  if (need - w[item] >= 0)
    t1 = v[item] + calculation(item + 1, need - w[item]);
  t2 = calculation(item + 1, need);

  return dp[item][need] = max(t1, t2);
}

void solve() {
  cin >> n >> weight;
  for (ll i = 0; i < n; ++i)
    cin >> w[i] >> v[i];

  for (ll i = 0; i < 101; ++i)
    for (ll j = 0; j < mx; ++j)
      dp[i][j] = -1;

  cout << calculation(0, weight) << "\n";
}

int main() // Md. Inzamam-ul Haque
{
  solve();
}
Problem p03163 (C++)

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

int n, w;

long long arr[10005];

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

  std::cin >> n >> w;
  for (int i = 0; i < n; i++) {
    long long a, b;
    std::cin >> a >> b;
    for (int j = w; j > a - 1; j--)
      arr[j] = std::max(arr[j], arr[j - a] + b);
  }
  std::cout << *std::max_element(&arr[0], &arr[w + 1]) << '\n';
}

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

int n, w;

long long arr[100005];

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

  std::cin >> n >> w;
  for (int i = 0; i < n; i++) {
    long long a, b;
    std::cin >> a >> b;
    for (int j = w; j > a - 1; j--)
      arr[j] = std::max(arr[j], arr[j - a] + b);
  }
  std::cout << *std::max_element(&arr[0], &arr[w + 1]) << '\n';
}
Problem p03163 (C++)

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

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

int n, w;

long long ans[N];

int main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // freopen("in.in" , "r" , stdin) ;

  cin >> n >> w;

  for (int i = 1; i < N; i++) {
    ans[i] = -1e9;
  }

  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    for (int j = w; j >= 0; j--) {
      ans[j + a] = max(ans[j + a], ans[j] + b);
    }
  }
  long long sol = 0;
  for (int i = 0; i <= w; i++) {
    sol = max(sol, ans[i]);
  }
  cout << sol;
  return 0;
}

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

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

int n, w;

long long ans[N];

int main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // freopen("in.in" , "r" , stdin) ;

  cin >> n >> w;

  for (int i = 1; i < N; i++) {
    ans[i] = -1e9;
  }

  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    for (int j = w - a; j >= 0; j--) {
      ans[j + a] = max(ans[j + a], ans[j] + b);
    }
  }
  long long sol = 0;
  for (int i = 0; i <= w; i++) {
    sol = max(sol, ans[i]);
  }
  cout << sol;
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#define ll long long
#define max(a, b) (a > b ? a : b)
#define maxn 10050
using namespace std;
ll dp[maxn];
int main() {
  int n, W;
  scanf("%d%d", &n, &W);
  for (int i = 1; i <= n; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    for (int j = W; j >= a; j--)
      dp[j] = max(dp[j - a] + b, dp[j]);
  }
  ll ans = 0;
  for (int j = 0; j <= W; j++)
    if (dp[j] > ans)
      ans = dp[j];
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <cstdio>
#define ll long long
#define max(a, b) (a > b ? a : b)
#define maxn 100050
using namespace std;
ll dp[maxn];
int main() {
  int n, W;
  scanf("%d%d", &n, &W);
  for (int i = 1; i <= n; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    for (int j = W; j >= a; j--)
      dp[j] = max(dp[j - a] + b, dp[j]);
  }
  ll ans = 0;
  for (int j = 0; j <= W; j++)
    if (dp[j] > ans)
      ans = dp[j];
  printf("%lld\n", ans);
  return 0;
}
Problem p03163 (C++)

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

typedef long long ll;

int v[101], w[101];
ll dp[102][10002];

int main() {
  int N, W;
  cin >> N >> W;
  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 (w[i] > j)
        dp[i + 1][j] = dp[i][j];
      else if (w[i] <= j) {
        dp[i + 1][j] = max(dp[i][j - w[i]] + v[i], dp[i][j]);
      }
    }
  }
  /*
  for ( int i = 0; i < N; i++ ) {
    for ( int j = 0; j <= W; j++ ) {
      cout << dp[i][j] << " ";
    }
    cout << endl;
  }
  */
  cout << dp[N][W] << endl;
  return 0;
}


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

typedef long long ll;

int v[101], w[101];
ll dp[102][100002];

int main() {
  int N, W;
  cin >> N >> W;
  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 (w[i] > j)
        dp[i + 1][j] = dp[i][j];
      else if (w[i] <= j) {
        dp[i + 1][j] = max(dp[i][j - w[i]] + v[i], dp[i][j]);
      }
    }
  }
  /*
  for ( int i = 0; i < N; i++ ) {
    for ( int j = 0; j <= W; j++ ) {
      cout << dp[i][j] << " ";
    }
    cout << endl;
  }
  */
  cout << dp[N][W] << endl;
  return 0;
}
Problem p03163 (C++)

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

int N;
vector<int> v(101), w(101);
vector<vector<ll>> dp(101, vector<ll>(101, -1));

// i番目以降の品物から重さの和がj以下なるように選んだときの、
// 取りうる価値の総和の最大値を返す関数
// i=品物 j=容量
ll rec(int i, int j) {
  if (dp[i][j] != -1) {
    return dp[i][j];
  }

  ll res;
  if (i == N) {
    // 品物がもう残っていないときは、価値の和の最大値は0で確定
    res = 0;
  } else if (j < w[i]) {
    // 残りの容量が足りず品物iを入れられないので、入れないパターンだけ処理
    // i+1 以降の品物のみを使ったときの最大値をそのままこの場合の最大値にする
    res = rec(i + 1, j);
  } else {
    // 品物iを入れるか入れないか選べるので、両方試して価値の和が大きい方を選ぶ
    res = max(rec(i + 1, j), rec(i + 1, j - w[i]) + v[i]);
  }

  dp[i][j] = res;
  return res;
}

int main() {
  // 入力
  int W;
  cin >> N >> W;
  rep(i, N) cin >> w[i] >> v[i];

  // 答え
  cout << rec(0, W) << endl;
}


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

int N;
vector<int> v(101), w(101);
vector<vector<ll>> dp(101, vector<ll>(1000001, -1));

// i番目以降の品物から重さの和がj以下なるように選んだときの、
// 取りうる価値の総和の最大値を返す関数
// i=品物 j=容量
ll rec(int i, int j) {
  if (dp[i][j] != -1) {
    return dp[i][j];
  }

  ll res;
  if (i == N) {
    // 品物がもう残っていないときは、価値の和の最大値は0で確定
    res = 0;
  } else if (j < w[i]) {
    // 残りの容量が足りず品物iを入れられないので、入れないパターンだけ処理
    // i+1 以降の品物のみを使ったときの最大値をそのままこの場合の最大値にする
    res = rec(i + 1, j);
  } else {
    // 品物iを入れるか入れないか選べるので、両方試して価値の和が大きい方を選ぶ
    res = max(rec(i + 1, j), rec(i + 1, j - w[i]) + v[i]);
  }

  dp[i][j] = res;
  return res;
}

int main() {
  // 入力
  int W;
  cin >> N >> W;
  rep(i, N) cin >> w[i] >> v[i];

  // 答え
  cout << rec(0, W) << endl;
}
Problem p03163 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define fr(i, j, p) for (int i = j; i < p; i++)
#define frr(i, j, p) for (int i = j; i >= p; i--)
#define PI 3.14159265358979323846
#define pb push_back
#define pbk pop_back()
#define mkp make_pair
#define print(m) std::cout << std::fixed << std::setprecision(11) << m << endl;
#define ff first
#define ss second
#define mod 1000000007
#define removeDuplicates(a) a.resize(unique(all(a)) - a.begin())

lli knap(lli tot, lli w[], lli v[], lli n) {
  if (n == 0 || tot == 0)
    return 0;
  if (w[n - 1] > tot)
    return knap(tot, w, v, n - 1);
  else
    return max(v[n - 1] + knap(tot - w[n - 1], w, v, n - 1),
               knap(tot, w, v, n - 1));
}
int main() {
  lli n, tot;
  cin >> n >> tot;
  lli w[n], v[n];
  fr(i, 0, n) cin >> w[i] >> v[i];
  cout << knap(tot, w, v, n);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define fr(i, j, p) for (int i = j; i < p; i++)
#define frr(i, j, p) for (int i = j; i >= p; i--)
#define PI 3.14159265358979323846
#define pb push_back
#define pbk pop_back()
#define mkp make_pair
#define print(m) std::cout << std::fixed << std::setprecision(11) << m << endl;
#define ff first
#define ss second
#define mod 1000000007
#define removeDuplicates(a) a.resize(unique(all(a)) - a.begin())

lli knap(lli tot, lli w[], lli v[], lli n) {
  lli k[n + 1][tot + 1];
  fr(i, 0, n + 1) {
    fr(j, 0, tot + 1) {
      if (i == 0 || j == 0)
        k[i][j] = 0;
      else if (w[i - 1] <= j)
        k[i][j] = max(v[i - 1] + k[i - 1][j - w[i - 1]], k[i - 1][j]);
      else
        k[i][j] = k[i - 1][j];
    }
  }
  return k[n][tot];
}
int main() {
  lli n, tot;
  cin >> n >> tot;
  lli w[n], v[n];
  fr(i, 0, n) cin >> w[i] >> v[i];
  cout << knap(tot, w, v, n);
  return 0;
}
Problem p03163 (C++)

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

signed main() {
  int n, w;
  cin >> n >> w;
  vector<int> x(n, 0), v(n, 0);
  for (int i = 1; i <= n; i++)
    cin >> x[i] >> v[i];
  vector<vector<long long>> f(n + 1, vector<long long>(w * 2 + 5, 0));
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < x[i]; j++)
      f[i][j] = f[i - 1][j];
    for (int j = x[i]; j <= w; j++) {
      f[i][j] = max(f[i - 1][j], f[i - 1][j - x[i]] + v[i]);
    }
  }
  cout << f[n][w];
  return 0;
}


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

signed main() {
  int n, w;
  cin >> n >> w;
  vector<int> x(n + 1, 0), v(n + 1, 0);
  for (int i = 1; i <= n; i++)
    cin >> x[i] >> v[i];
  vector<vector<long long>> f(n + 1, vector<long long>(w * 2 + 5, 0));
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < x[i]; j++)
      f[i][j] = f[i - 1][j];
    for (int j = x[i]; j <= w; j++) {
      f[i][j] = max(f[i - 1][j], f[i - 1][j - x[i]] + v[i]);
    }
  }
  cout << f[n][w];
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <fstream>
#include <iostream>
long long n, S, w[1000], v[1000], F[100][100000];
using namespace std;
int main() {
  // freopen("KNAPSACK1.INP","r",stdin);
  cin >> n >> S;
  for (long long i = 1; i <= n; i++)
    cin >> w[i] >> v[i];
  for (long long i = 1; i <= n; i++)
    for (long long j = 1; j <= S; j++)
      if (w[i] <= j)
        F[i][j] = max(F[i - 1][j - w[i]] + v[i], F[i - 1][j]);
      else
        F[i][j] = F[i - 1][j];
  cout << F[n][S];
  return 0;
}


Fixed submission:
#include <fstream>
#include <iostream>
long long n, S, w[1000], v[1000], F[101][100001];
using namespace std;
int main() {
  // freopen("KNAPSACK1.INP","r",stdin);
  cin >> n >> S;
  for (long long i = 1; i <= n; i++)
    cin >> w[i] >> v[i];
  for (long long i = 1; i <= n; i++)
    for (long long j = 1; j <= S; j++)
      if (w[i] <= j)
        F[i][j] = max(F[i - 1][j - w[i]] + v[i], F[i - 1][j]);
      else
        F[i][j] = F[i - 1][j];
  cout << F[n][S];
  return 0;
}