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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int t = 1;
  // cin >> t;
  while (t--) {

    int n;
    cin >> n;
    long long w;
    cin >> w;
    long long *val = new long long[n];
    long long *we = new long long[n];

    for (int i = 0; i < n; i++) {
      cin >> we[i];
      cin >> val[i];
    }
    long long dp[10001][10001];
    for (int i = 0; i < n; i++) {
      dp[0][i] = 0;
    }
    for (int i = 0; i < n; i++) {
      dp[i][0] = 0;
    }
    int k;

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int t = 1;
  // cin >> t;
  while (t--) {

    int n;
    cin >> n;
    long long w;
    cin >> w;
    long long *val = new long long[n];
    long long *we = new long long[n];

    for (int i = 0; i < n; i++) {
      cin >> we[i];
      cin >> val[i];
    }
    long long dp[101][100001];
    for (int i = 0; i < n; i++) {
      dp[0][i] = 0;
    }
    for (int i = 0; i < n; i++) {
      dp[i][0] = 0;
    }
    int k;

    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= w; j++) {
        if (we[i - 1] <= j) {
          dp[i][j] = max(val[i - 1] + dp[i - 1][j - we[i - 1]], dp[i - 1][j]);
        } else
          dp[i][j] = dp[i - 1][j];
      }
    }
    cout << dp[n][w];
  }
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
// atcoder knapsack 1
#include <bits/stdc++.h>
#define N_ 101000
using namespace std;
long long D[N_];

int main() {
  int a, b, n, W;
  cin >> n >> W;
  for (int i = 1; i <= n; i++) {
    cin >> a >> b;
    for (int j = W; j >= 0; j--)
      D[j + a] = max(D[j + a], D[j] + b);
  }
  long long r = 0;
  for (int i = 0; i <= W; i++)
    r = max(r, D[i]);
  cout << r << endl;
  return 0;
}


Fixed submission:
// atcoder knapsack 1
#include <bits/stdc++.h>
#define N_ 101000000
using namespace std;
long long D[N_];

int main() {
  int a, b, n, W;
  cin >> n >> W;
  for (int i = 1; i <= n; i++) {
    cin >> a >> b;
    for (int j = W; j >= 0; j--)
      D[j + a] = max(D[j + a], D[j] + b);
  }
  long long r = 0;
  for (int i = 0; i <= W; i++)
    r = max(r, D[i]);
  cout << r << endl;
  return 0;
}
Problem p03163 (C++)

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

#define fi first
#define se second

using namespace std;

typedef pair<int, int> ii;

int n, w, v[105], we[105];
long long dp[100005][105];

long long recur(int cap, int i) {
  if (i > n)
    return 0;
  if (cap < we[i])
    return recur(cap, i + 1);
  else
    return max(v[i] + recur(cap - we[i], i + 1), recur(cap, i + 1));
}
int main() {
  cin >> n >> w;
  for (int i = 1; i <= n; ++i)
    cin >> we[i] >> v[i];
  cout << recur(w, 1);
  return 0;
}

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

#define fi first
#define se second

using namespace std;

typedef pair<int, int> ii;

int n, w, v[105], we[105];
long long dp[100005][105];

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

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
pair<long long int, long long int> p[105];
long long int dp[100005];
int main() {
  long long int n, w;
  cin >> n >> w;
  for (long long int i = 0; i < n; i++) {
    long long int w1, v1;
    cin >> w1 >> v1;
    p[i] = make_pair(w1, v1);
  }
  for (long long int i = 0; i < n; i++) {
    for (long long int j = w; j >= 0; j--) {
      if ((j - p[i].first == 0 || dp[j - p[i].first] != 0) &&
          j - p[i].first >= 0) {
        dp[j] = max(dp[j], p[i].second + dp[j - p[i].first]);
      }
    }
    /*for(long long int j=0;j<12;j++) {
            cout <<dp[j]<<" ";
    }
    cout <<endl;*/
  }
  /*for(long long int j=w;j>=0;j--) {
          for(long long int k=0;k<n;k++) {
                  if(j-p[k].first!=p[k].first && dp[j-p[k].first]!=0 &&
  dp[p[k].first]!=0) dp[j]=max(dp[j],dp[p[k].first]+dp[j-p[k].first]);
          }
  }*/
  long long int maxi = 0;
  for (long long int i = 0; i <= w; i++) {
    maxi = max(maxi, dp[i]);
  }
  cout << maxi;
  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;
pair<long long int, long long int> p[105];
long long int dp[100005];
int main() {
  long long int n, w;
  cin >> n >> w;
  for (long long int i = 0; i < n; i++) {
    long long int w1, v1;
    cin >> w1 >> v1;
    p[i] = make_pair(w1, v1);
  }
  for (long long int i = 0; i < n; i++) {
    for (long long int j = w; j >= 0; j--) {
      if (j - p[i].first >= 0 &&
          (j - p[i].first == 0 || dp[j - p[i].first] != 0)) {
        dp[j] = max(dp[j], p[i].second + dp[j - p[i].first]);
      }
    }
    /*for(long long int j=0;j<12;j++) {
            cout <<dp[j]<<" ";
    }
    cout <<endl;*/
  }
  /*for(long long int j=w;j>=0;j--) {
          for(long long int k=0;k<n;k++) {
                  if(j-p[k].first!=p[k].first && dp[j-p[k].first]!=0 &&
  dp[p[k].first]!=0) dp[j]=max(dp[j],dp[p[k].first]+dp[j-p[k].first]);
          }
  }*/
  long long int maxi = 0;
  for (long long int i = 0; i <= w; i++) {
    maxi = max(maxi, dp[i]);
  }
  cout << maxi;
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
// #define Fast ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define int long long
typedef vector<int> Vl;
typedef pair<int, int> pii;
typedef vector<pii> Vll;
#define S second
#define F first
#define mp make_pair
#define ms(x, y) memset(x, y, sizeof(x))
#define pb push_back
#define sl(n) scanf("%lld", &n)
#define pl(n) printf("%lld", n)
#define sz(a) (int)(a).size()
const int mod = 2e19;
int dp[105][10005];
int recur(int w[], int val[], int i, int n, int W) {
  if (W < 0)
    return -mod;
  if (i == n)
    return 0;
  if (dp[i][W] != 0)
    return dp[i][W];
  int take = val[i] + recur(w, val, i + 1, n, W - w[i]);
  int leav = recur(w, val, i + 1, n, W);
  return dp[i][W] = max(take, leav);
}

signed main() {
  ms(dp, 0);
  int n, W;
  sl(n);
  sl(W);
  int w[n], val[n];
  for (int i = 0; i < n; i++) {
    sl(w[i]);
    sl(val[i]);
  }
  cout << recur(w, val, 0, n, W) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
// #define Fast ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define int long long
typedef vector<int> Vl;
typedef pair<int, int> pii;
typedef vector<pii> Vll;
#define S second
#define F first
#define mp make_pair
#define ms(x, y) memset(x, y, sizeof(x))
#define pb push_back
#define sl(n) scanf("%lld", &n)
#define pl(n) printf("%lld", n)
#define sz(a) (int)(a).size()
const int mod = 2e19;
int dp[105][100005];
int recur(int w[], int val[], int i, int n, int W) {
  if (W < 0)
    return -mod;
  if (i == n)
    return 0;
  if (dp[i][W] != 0)
    return dp[i][W];
  int take = val[i] + recur(w, val, i + 1, n, W - w[i]);
  int leav = recur(w, val, i + 1, n, W);
  return dp[i][W] = max(take, leav);
}

signed main() {
  ms(dp, 0);
  int n, W;
  sl(n);
  sl(W);
  int w[n], val[n];
  for (int i = 0; i < n; i++) {
    sl(w[i]);
    sl(val[i]);
  }
  cout << recur(w, val, 0, n, W) << endl;
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <utility>
#include <vector>

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
using namespace std;
typedef pair<int, int> P;
typedef long long int ll;
typedef pair<ll, ll> LLP;
int dx[4] = {1, 0, 0, -1};
int dy[4] = {0, 1, -1, 0};
#define INF 1000000007
#define MOD 1000000007
#define MAX_N 105
#define MAX_W 100005

ll dp[MAX_N][MAX_W]; // 0:海,1:山,2:宿題,dp[i][j] = iをj日目に行う時の幸福度
ll weight[MAX_N];
ll value[MAX_N];

int main() {

  ll n, weight_maxi;
  cin >> n >> weight_maxi;
  REP(i, n) { cin >> weight[i] >> value[i]; }

  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= weight_maxi; j++) {

      if (j < weight[i]) {
        dp[i][j] = dp[i - 1][j];
      } else {
        dp[i][j] =
            max(dp[i - 1][j], dp[i - 1][j - weight[i - 1]] + value[i - 1]);
      }
    }
  }
  cout << dp[n][weight_maxi] << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <utility>
#include <vector>

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
using namespace std;
typedef pair<int, int> P;
typedef long long int ll;
typedef pair<ll, ll> LLP;
int dx[4] = {1, 0, 0, -1};
int dy[4] = {0, 1, -1, 0};
#define INF 1000000007
#define MOD 1000000007
#define MAX_N 105
#define MAX_W 100005

ll dp[MAX_N][MAX_W]; // 0:海,1:山,2:宿題,dp[i][j] = iをj日目に行う時の幸福度
ll weight[MAX_N];
ll value[MAX_N];

int main() {

  ll n, weight_maxi;
  cin >> n >> weight_maxi;
  REP(i, n) { cin >> weight[i] >> value[i]; }

  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= weight_maxi; j++) {

      if (j < weight[i - 1]) {
        dp[i][j] = dp[i - 1][j];
      } else {
        dp[i][j] =
            max(dp[i - 1][j], dp[i - 1][j - weight[i - 1]] + value[i - 1]);
      }
    }
  }
  cout << dp[n][weight_maxi] << endl;

  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Time Limit Exceeded):
/**
 *   author: Shubham Srivastava
 *   created: 26 January 2020  00:24:22
 **/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define MOD 1000000007
#define INF 0x7fffffff
#define sorti(v) sort(v.begin(), v.end())
#define sortd(v) sort(v.rbegin(), v.rend())
typedef pair<ll, ll> pll;
typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef vector<pii> vii;
ll parent[2000005];

ll helper(vector<ll> weight, vector<ll> value, ll w, ll i, ll n,
          vector<vector<ll>> &dp) {
  if (w <= 0 || i == n)
    return 0;
  if (dp[i][w] != -1)
    return dp[i][w];
  if (weight[i] > w)
    return dp[i][w] = helper(weight, value, w, i + 1, n, dp);
  return dp[i][w] =
             max(value[i] + helper(weight, value, w - weight[i], i + 1, n, dp),
                 helper(weight, value, w, i + 1, n, dp));
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, w;
  cin >> n >> w;
  vector<vector<ll>> dp(n + 1, vector<ll>(w + 1, -1));
  vector<ll> weight(n), value(n);
  for (ll i = 0; i < n; i++) {
    cin >> weight[i] >> value[i];
  }
  cout << helper(weight, value, w, 0, n, dp);
  return 0;
}

Fixed submission:
/**
 *   author: Shubham Srivastava
 *   created: 26 January 2020  00:24:22
 **/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define MOD 1000000007
#define INF 0x7fffffff
#define sorti(v) sort(v.begin(), v.end())
#define sortd(v) sort(v.rbegin(), v.rend())
typedef pair<ll, ll> pll;
typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef vector<pii> vii;
ll parent[2000005];

ll helper(vector<ll> &weight, vector<ll> &value, ll w, ll i, ll n,
          vector<vector<ll>> &dp) {
  if (w <= 0 || i == n)
    return 0;
  if (dp[i][w] != -1)
    return dp[i][w];
  if (weight[i] > w)
    return dp[i][w] = helper(weight, value, w, i + 1, n, dp);
  return dp[i][w] =
             max(value[i] + helper(weight, value, w - weight[i], i + 1, n, dp),
                 helper(weight, value, w, i + 1, n, dp));
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, w;
  cin >> n >> w;
  vector<vector<ll>> dp(n + 1, vector<ll>(w + 1, -1));
  vector<ll> weight(n), value(n);
  for (ll i = 0; i < n; i++) {
    cin >> weight[i] >> value[i];
  }
  cout << helper(weight, value, w, 0, n, dp);
  return 0;
}
Problem p03163 (C++)

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

using namespace std;

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

  long long dp[101][101];

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

  for (int i = 1; i <= n; i++) {

    int wi;
    long long vi;
    cin >> wi >> vi;

    for (int j = 1; j <= w; j++) {
      if (j < wi) {
        dp[i][j] = dp[i - 1][j];
        continue;
      }
      dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - wi] + vi);
    }
  }

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

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

using namespace std;

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

  long long dp[101][100001];

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

  for (int i = 1; i <= n; i++) {

    int wi;
    long long vi;
    cin >> wi >> vi;

    for (int j = 1; j <= w; j++) {
      if (j < wi) {
        dp[i][j] = dp[i - 1][j];
        continue;
      }
      dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - wi] + vi);
    }
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#define lld long long int

//-4%3=-1
lld n, w;
lld wt[101], val[101], dp[101][101];

lld fun(lld i, lld w) {
  if (i <= 0 || w <= 0)
    return 0;
  if (dp[i][w] == -1) {
    if (wt[i] <= w)
      return dp[i][w] = max(val[i] + fun(i - 1, w - wt[i]), fun(i - 1, w));
    else
      return dp[i][w] = fun(i - 1, w);
  } else
    return dp[i][w];
}

int main() {
  // your code goes here
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  lld t;
  // cin>>t;
  t = 1;
  while (t--) {

    cin >> n >> w;
    for (int i = 1; i <= n; i++)
      cin >> wt[i] >> val[i];
    memset(dp, -1, sizeof(dp));
    cout << fun(n, w);
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#define lld long long int

//-4%3=-1
lld n, w;
lld wt[101], val[101], dp[101][100001];

lld fun(lld i, lld w) {
  if (i <= 0 || w <= 0)
    return 0;
  if (dp[i][w] == -1) {
    if (wt[i] <= w)
      return dp[i][w] = max(val[i] + fun(i - 1, w - wt[i]), fun(i - 1, w));
    else
      return dp[i][w] = fun(i - 1, w);
  } else
    return dp[i][w];
}

int main() {
  // your code goes here
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  lld t;
  // cin>>t;
  t = 1;
  while (t--) {

    cin >> n >> w;
    for (int i = 1; i <= n; i++)
      cin >> wt[i] >> val[i];
    memset(dp, -1, sizeof(dp));
    cout << fun(n, w);
  }
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define maxn 100
#define maxk 10001
using namespace std;
typedef long long LL;

LL ans;
int n, k;
int w[maxn];
int v[maxn];
LL dp[maxk];

int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> w[i] >> v[i];
  memset(dp, -1, sizeof(dp));
  dp[0] = 0;
  for (int i = 0; i < n; i++)
    for (int j = k - w[i]; j >= 0; j--)
      if (dp[j] != -1)
        if (dp[j + w[i]] == -1 || dp[j] + v[i] > dp[j + w[i]])
          dp[j + w[i]] = dp[j] + v[i];
  for (int i = 0; i <= k; i++)
    ans = max(ans, dp[i]);
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define maxn 100
#define maxk 100001
using namespace std;
typedef long long LL;

LL ans;
int n, k;
int w[maxn];
int v[maxn];
LL dp[maxk];

int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> w[i] >> v[i];
  memset(dp, -1, sizeof(dp));
  dp[0] = 0;
  for (int i = 0; i < n; i++)
    for (int j = k - w[i]; j >= 0; j--)
      if (dp[j] != -1)
        if (dp[j + w[i]] == -1 || dp[j] + v[i] > dp[j + w[i]])
          dp[j + w[i]] = dp[j] + v[i];
  for (int i = 0; i <= k; i++)
    ans = max(ans, dp[i]);
  cout << ans << endl;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#define ll long long
#define rep(i, a, b) for (int i = a; i <= b; i++)
void read(int &x) {
  x = 0;
  int flag = 1;
  char ch = ' ';
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      flag = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  x *= flag;
}
void read(ll &x) {
  x = 0;
  int flag = 1;
  char ch = ' ';
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      flag = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  x *= flag;
}
using namespace std;
const int maxn = 110;
int n, w, weight[maxn], value[maxn];
ll dp[10010];
int main() {
  read(n), read(w);
  rep(i, 1, n) read(weight[i]), read(value[i]);
  rep(i, 1, n) for (int j = w; j >= weight[i]; j--) dp[j] =
      max(dp[j], dp[j - weight[i]] + value[i]);
  printf("%lld\n", dp[w]);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#define ll long long
#define rep(i, a, b) for (int i = a; i <= b; i++)
void read(int &x) {
  x = 0;
  int flag = 1;
  char ch = ' ';
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      flag = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  x *= flag;
}
void read(ll &x) {
  x = 0;
  int flag = 1;
  char ch = ' ';
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      flag = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  x *= flag;
}
using namespace std;
const int maxn = 110;
int n, w, weight[maxn], value[maxn];
ll dp[100010];
int main() {
  read(n), read(w);
  rep(i, 1, n) read(weight[i]), read(value[i]);
  rep(i, 1, n) for (int j = w; j >= weight[i]; j--) dp[j] =
      max(dp[j], dp[j - weight[i]] + value[i]);
  printf("%lld\n", dp[w]);
  return 0;
}
Problem p03163 (C++)

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

long long t[100][100000];

using namespace std;

void Knapsack(vector<long long> wt, vector<long long> val, long long w,
              long long n) {
  if (w == 0 || n == 0) {
    return;
  }

  for (int i = 1; i < n + 1; i++) {
    for (int j = 1; j < w + 1; j++) {
      if (wt[i - 1] <= j) {
        t[i][j] = max(val[i - 1] + t[i - 1][j - wt[i - 1]], t[i - 1][j]);
      }

      else {
        t[i][j] = t[i - 1][j];
      }
    }
  }
}

int main() {
  int n, w;

  cin >> n;
  cin >> w;

  // 	memset(t, -1, sizeof(t));

  vector<long long> wt(n);
  vector<long long> val(n);

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

  for (int i = 0; i < n + 1; i++) {
    for (int j = 0; j < w + 1; j++) {
      if (i == 0 || j == 0) {
        t[i][j] = 0;
      }
    }
  }

  Knapsack(wt, val, w, n);

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

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

long long t[101][1000001];

using namespace std;

void Knapsack(vector<long long> wt, vector<long long> val, long long w,
              long long n) {
  if (w == 0 || n == 0) {
    return;
  }

  for (int i = 1; i < n + 1; i++) {
    for (int j = 1; j < w + 1; j++) {
      if (wt[i - 1] <= j) {
        t[i][j] = max(val[i - 1] + t[i - 1][j - wt[i - 1]], t[i - 1][j]);
      }

      else {
        t[i][j] = t[i - 1][j];
      }
    }
  }
}

int main() {
  int n, w;

  cin >> n;
  cin >> w;

  // 	memset(t, -1, sizeof(t));

  vector<long long> wt(n);
  vector<long long> val(n);

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

  for (int i = 0; i < n + 1; i++) {
    for (int j = 0; j < w + 1; j++) {
      if (i == 0 || j == 0) {
        t[i][j] = 0;
      }
    }
  }

  Knapsack(wt, val, w, n);

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

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

using namespace std;
#define sz(x) (int)(x).size()
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define ll long long
const int N = 3e5 + 5, MOD = 1e9 + 7;

int n, s;
int main() {
#ifndef ONLINE_JUDGE
  freopen("tst.txt", "r", stdin);
#endif

  scanf("%d %d", &n, &s);

  std::vector<vector<ll>> dp(n, vector<ll>(s + 1));
  for (int i = 0; i < n; i++) {
    int w, x;
    scanf("%d %d", &w, &x);
    if (i)
      for (int j = 0; j <= s; j++) {
        if (j - w >= 0)
          dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w] + x);
        else
          dp[i][j] = dp[i - 1][j];
      }
    else
      for (int j = w; j <= s; j++)
        dp[i][j] = x;
  }

  printf("%lld\n", dp[n - 1][s]);
}


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

using namespace std;
#define sz(x) (int)(x).size()
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define ll long long
const int N = 3e5 + 5, MOD = 1e9 + 7;

int n, s;
int main() {

  scanf("%d %d", &n, &s);

  std::vector<vector<ll>> dp(n, vector<ll>(s + 1));
  for (int i = 0; i < n; i++) {
    int w, x;
    scanf("%d %d", &w, &x);
    if (i)
      for (int j = 0; j <= s; j++) {
        if (j - w >= 0)
          dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w] + x);
        else
          dp[i][j] = dp[i - 1][j];
      }
    else
      for (int j = w; j <= s; j++)
        dp[i][j] = x;
  }

  printf("%lld\n", dp[n - 1][s]);
}
Problem p03163 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define all(s) s.begin(), s.end()
#define sz(s) (int)s.size()
#define clr(v, d) memset(v, d, sizeof(v))
#define fi first
#define se second
#define pp push_back
#define pb pop_back
#define pi 3.141592654
#define MOD 1000000007
#define cons1 1e12
#define cons2 1e6
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, ll> pa;
typedef unsigned long long int ull;
typedef double long dl;
const int N = 1e5 + 9, oo = -1e9;
int n, v[N][2];
ll ans[N];
ll sol(int i, int w) {
  if (i == n)
    return 0;
  ll x = sol(i + 1, w);
  if (w >= v[i][0])
    x = max(x, sol(i + 1, w - v[i][0]) + v[i][1]);
  return x;
}
void solve() {
  int w;
  cin >> n >> w;
  rep(i, 0, n) cin >> v[i][0] >> v[i][1];
  cout << sol(0, w) << endl;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  // cin >> t;
  while (t--)
    solve();

#ifndef ONLINE_JUDGE
  -system("pause");
#endif
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define all(s) s.begin(), s.end()
#define sz(s) (int)s.size()
#define clr(v, d) memset(v, d, sizeof(v))
#define fi first
#define se second
#define pp push_back
#define pb pop_back
#define pi 3.141592654
#define MOD 1000000007
#define cons1 1e12
#define cons2 1e6
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, ll> pa;
typedef unsigned long long int ull;
typedef double long dl;
const int N = 1e5 + 9, oo = -1e9;
ll dp[109][N], n, v[109][109];
ll sol(int i, int k) {
  if (n - i && !dp[i][k]) {
    if (k >= v[i][0])
      dp[i][k] = v[i][1] + sol(i + 1, k - v[i][0]);
    dp[i][k] = max(dp[i][k], sol(i + 1, k));
  }

  return dp[i][k];
}
void solve() {
  int w;
  cin >> n >> w;
  rep(i, 0, n) cin >> v[i][0] >> v[i][1];
  cout << sol(0, w) << endl;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  // cin >> t;
  while (t--)
    solve();

#ifndef ONLINE_JUDGE
  -system("pause");
#endif
}
Problem p03163 (C++)

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

using namespace std;

ifstream fin("jocul.in");
ofstream fout("jocul.out");

long long n, g;
long long v[105], w[105];
long long dp[100005];

int main() {
  long long i, sum = 0, answer = -1;

  cin >> n >> g;

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

  dp[0] = 0;

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

  for (i = 0; i <= sum; i++) {
    if (dp[i] > answer && i <= g)
      answer = dp[i];
  }

  cout << answer;

  return 0;
}


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

using namespace std;

ifstream fin("jocul.in");
ofstream fout("jocul.out");

long long n, g;
long long v[105], w[105];
long long dp[10000005];

int main() {
  long long i, sum = 0, answer = -1;

  cin >> n >> g;

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

  dp[0] = 0;

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

  for (i = 0; i <= sum; i++) {
    if (dp[i] > answer && i <= g)
      answer = dp[i];
  }

  cout << answer;

  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 100;
const int CAP = 1e5 + 10;
int n, cap;
int v[N], w[N];
ll dp[N][CAP];
ll run(int idx, int cur) {
  if (dp[idx][cur] != -1)
    return dp[idx][cur];
  ll res = 0;
  if (idx == n or cur == 0)
    res = 0;
  else if (w[idx] > cur)
    res = run(idx + 1, cur);
  else
    res = max(run(idx + 1, cur), run(idx + 1, cur - w[idx]) + v[idx]);
  return dp[idx][cur] = res;
}
ll run() {
  for (int i = 1; i <= n; ++i) {
    for (int k = 1; k <= cap; ++k) {
      if (w[i - 1] > k)
        dp[i][k] = dp[i - 1][k];
      else
        dp[i][k] = max(dp[i - 1][k], dp[i - 1][k - w[i - 1]] + v[i - 1]);
    }
  }
  return dp[n][cap];
}
int main() {
  scanf("%d %d", &n, &cap);
  for (int i = 0; i < n; ++i)
    scanf("%d %d", &w[i], &v[i]);
  // memset(dp, -1, sizeof dp);
  ll ans = run();
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 100 + 10;
const int CAP = 1e5 + 10;
int n, cap;
int v[N], w[N];
ll dp[N][CAP];
ll run(int idx, int cur) {
  if (dp[idx][cur] != -1)
    return dp[idx][cur];
  ll res = 0;
  if (idx == n or cur == 0)
    res = 0;
  else if (w[idx] > cur)
    res = run(idx + 1, cur);
  else
    res = max(run(idx + 1, cur), run(idx + 1, cur - w[idx]) + v[idx]);
  return dp[idx][cur] = res;
}
ll run() {
  for (int i = 1; i <= n; ++i) {
    for (int k = 1; k <= cap; ++k) {
      if (w[i - 1] > k)
        dp[i][k] = dp[i - 1][k];
      else
        dp[i][k] = max(dp[i - 1][k], dp[i - 1][k - w[i - 1]] + v[i - 1]);
    }
  }
  return dp[n][cap];
}
int main() {
  scanf("%d %d", &n, &cap);
  for (int i = 0; i < n; ++i)
    scanf("%d %d", &w[i], &v[i]);
  // memset(dp, -1, sizeof dp);
  ll ans = run();
  printf("%lld\n", ans);
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long wt[110], val[110], v[110][110];
  int n;
  long long w;
  cin >> n >> w;
  for (int i = 1; i <= n; i++)
    cin >> wt[i] >> val[i];

  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= w; j++)
      v[i][j] = 0;

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= w; j++) {
      if (wt[i] > j)
        v[i][j] = v[i - 1][j];
      else
        v[i][j] = max(v[i - 1][j], val[i] + v[i - 1][j - wt[i]]);
    }
  }
  cout << v[n][w];
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long wt[110], val[110], v[110][110000];
  int n;
  long long w;
  cin >> n >> w;
  for (int i = 1; i <= n; i++)
    cin >> wt[i] >> val[i];

  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= w; j++)
      v[i][j] = 0;

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= w; j++) {
      if (wt[i] > j)
        v[i][j] = v[i - 1][j];
      else
        v[i][j] = max(v[i - 1][j], val[i] + v[i - 1][j - wt[i]]);
    }
  }
  cout << v[n][w];
  return 0;
}
Problem p03163 (C++)

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

using namespace std;

#define pb push_back
#define eps 1e-9
#define PI acos(-1.0)
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<ii> vii;
const int INF = 1e9 + 5;

bool prime(ll x) {
  if (x == 2)
    return true;
  for (int i = 2; i * i <= x; i++)
    if (x % i == 0)
      return false;
  return true;
}
void self_max(ll &a, ll b) { a = max(a, b); }
bool cmp(int a, int b) { return a > b; }
int a[int(1e5 + 5)];
int main() {
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);

  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n, w;
  cin >> n >> w;
  std::vector<ll> dp(w + 5);
  for (int item = 0; item < n; ++item) {
    int wt, val;
    cin >> wt >> val;
    for (int wt_alrdy = w - wt; wt_alrdy >= 0; --wt_alrdy) {
      self_max(dp[wt_alrdy + wt], dp[wt_alrdy] + val);
    }
  }
  ll ans = 0;
  for (int i = 0; i <= w; ++i) {
    self_max(ans, dp[i]);
  }
  cout << ans;
  return 0;
}


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

using namespace std;

#define pb push_back
#define eps 1e-9
#define PI acos(-1.0)
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<ii> vii;
const int INF = 1e9 + 5;

bool prime(ll x) {
  if (x == 2)
    return true;
  for (int i = 2; i * i <= x; i++)
    if (x % i == 0)
      return false;
  return true;
}
void self_max(ll &a, ll b) { a = max(a, b); }
bool cmp(int a, int b) { return a > b; }
int a[int(1e5 + 5)];
int main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n, w;
  cin >> n >> w;
  std::vector<ll> dp(w + 5);
  for (int item = 0; item < n; ++item) {
    int wt, val;
    cin >> wt >> val;
    for (int wt_alrdy = w - wt; wt_alrdy >= 0; --wt_alrdy) {
      self_max(dp[wt_alrdy + wt], dp[wt_alrdy] + val);
    }
  }
  ll ans = 0;
  for (int i = 0; i <= w; ++i) {
    self_max(ans, dp[i]);
  }
  cout << ans;
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
long long a[10 + 100][2], b[10 + 100][110] = {0};
long long sol(int n, int i, int j) {
  if (i == n - 1) {
    long long m = -8;
    for (int t = 0; t < 3; t++) {
      if (t != j)
        m = max(m, a[i][t]);
    }
    return m;
  }
  long long m = -8, s = 0;
  for (int t = 0; t < 3; t++) {
    if (t != j) {
      if (m < a[i][t])
        s = t;
      m = max(m, a[i][t]);
    }
  }
  if (b[i][s] == -1)
    b[i][s] = m + sol(n, i + 1, s);
  return b[i][s];
}
int main() {

  int n, w;
  cin >> n >> w;
  for (int i = 1; i <= n; i++)
    for (int j = 0; j < 2; j++) {
      cin >> a[i][j];
    }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= w; j++) {
      if (j - a[i][0] > -1) {
        b[i][j] = max(a[i][1] + b[i - 1][j - a[i][0]], b[i - 1][j]);
      } else
        b[i][j] = b[i - 1][j];
    }
  }
  cout << b[n][w] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
long long a[1000 + 100][2], b[10 + 100][100000 + 100] = {0};
long long sol(int n, int i, int j) {
  if (i == n - 1) {
    long long m = -8;
    for (int t = 0; t < 3; t++) {
      if (t != j)
        m = max(m, a[i][t]);
    }
    return m;
  }
  long long m = -8, s = 0;
  for (int t = 0; t < 3; t++) {
    if (t != j) {
      if (m < a[i][t])
        s = t;
      m = max(m, a[i][t]);
    }
  }
  if (b[i][s] == -1)
    b[i][s] = m + sol(n, i + 1, s);
  return b[i][s];
}
int main() {

  int n, w;
  cin >> n >> w;
  for (int i = 1; i <= n; i++)
    for (int j = 0; j < 2; j++) {
      cin >> a[i][j];
    }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= w; j++) {
      if (j - a[i][0] > -1) {
        b[i][j] = max(a[i][1] + b[i - 1][j - a[i][0]], b[i - 1][j]);
      } else
        b[i][j] = b[i - 1][j];
    }
  }
  cout << b[n][w] << endl;
  return 0;
}
Problem p03163 (C++)

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

int n, w;
ll dp[105][105];
ll V[105], W[105];

ll solve(int i, ll p) {
  if (i == n || p == 0)
    return 0;

  if (dp[i][p] != -1)
    return dp[i][p];

  if (W[i] > p)
    return dp[i][p] = solve(i + 1, p);

  return dp[i][p] = max(V[i] + solve(i + 1, p - W[i]), solve(i + 1, p));
}

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

  memset(dp, -1, sizeof dp);

  for (int i = 0; i < n; i++)
    cin >> W[i] >> V[i];

  cout << solve(0, w) << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#define ll long long
using namespace std;

int n, w;
ll dp[105][1000000];
ll V[105], W[105];

ll solve(int i, ll p) {
  if (i == n || p == 0)
    return 0;

  if (dp[i][p] != -1)
    return dp[i][p];

  if (W[i] > p)
    return dp[i][p] = solve(i + 1, p);

  return dp[i][p] = max(V[i] + solve(i + 1, p - W[i]), solve(i + 1, p));
}

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

  memset(dp, -1, sizeof dp);

  for (int i = 0; i < n; i++)
    cin >> W[i] >> V[i];

  cout << solve(0, w) << endl;

  return 0;
}
Problem p03163 (C++)

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

using namespace std;

#define pii pair<int, int>
#define ll long long
#define s second
#define f first

ll n, W, val[105], wt[105], dp[105][105];

int main() {
  cin >> n >> W;
  for (int i = 0; i < n; ++i) {
    cin >> wt[i] >> val[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 (wt[i - 1] > j) {
        dp[i][j] = dp[i - 1][j];
      } else {
        dp[i][j] = max(val[i - 1] + dp[i - 1][j - wt[i - 1]], dp[i - 1][j]);
      }
    }
  }
  cout << dp[n][W];
  return 0;
}


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

using namespace std;

#define pii pair<int, int>
#define ll long long
#define s second
#define f first

ll n, W, val[105], wt[105], dp[105][100005];

int main() {
  cin >> n >> W;
  for (int i = 0; i < n; ++i) {
    cin >> wt[i] >> val[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 (wt[i - 1] > j) {
        dp[i][j] = dp[i - 1][j];
      } else {
        dp[i][j] = max(val[i - 1] + dp[i - 1][j - wt[i - 1]], dp[i - 1][j]);
      }
    }
  }
  cout << dp[n][W];
  return 0;
}
Problem p03163 (C++)

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

using namespace std;
typedef long long ll;
#define rep(i, s, e) for (int(i) = (s); (i) <= (e); ++(i))
#define all(x) x.begin(), x.end()

int main() {
  int N, W;
  cin >> N >> W;
  vector<int> w(N + 1);
  vector<ll> v(N + 1);
  rep(i, 1, N) cin >> w[i] >> v[i];

  vector<vector<ll>> dp(N + 1, vector<ll>(5001001, 0));
  rep(i, 1, N) rep(j, 0, W) {
    if (j < w[i])
      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;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
#define rep(i, s, e) for (int(i) = (s); (i) <= (e); ++(i))
#define all(x) x.begin(), x.end()

int main() {
  int N, W;
  cin >> N >> W;
  vector<int> w(N + 1);
  vector<ll> v(N + 1);
  rep(i, 1, N) cin >> w[i] >> v[i];

  vector<vector<ll>> dp(N + 1, vector<ll>(500000, 0));
  rep(i, 1, N) rep(j, 0, W) {
    if (j < w[i])
      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;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
using namespace std;
using LL = long long;

int N, W, w, v;
LL dp[105];

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

Fixed submission:
#include <algorithm>
#include <cstdio>
using namespace std;
using LL = long long;

int N, W, w, v;
LL dp[100005];

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int maxx = 1e6 + 10;
struct node {
  int w;
  int v;
} in[105];
long long dp[105] = {0};
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &in[i].w, &in[i].v);
  }
  for (int j = 0; j < n; j++) {
    for (int i = m; i >= 0; i--) {
      if (in[j].w <= i) {
        dp[i] = max(dp[i], dp[i - in[j].w] + in[j].v);
      }
    }
  }
  printf("%lld", dp[m]);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxx = 1e6 + 10;
struct node {
  int w;
  int v;
} in[105];
long long dp[maxx] = {0};
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &in[i].w, &in[i].v);
  }
  for (int j = 0; j < n; j++) {
    for (int i = m; i >= 0; i--) {
      if (in[j].w <= i) {
        dp[i] = max(dp[i], dp[i - in[j].w] + in[j].v);
      }
    }
  }
  printf("%lld", dp[m]);
}
Problem p03163 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define IO                                                                     \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define int long long
using namespace std;
const int MX = 1e5 + 5;
int dp[MX][105];
vector<int> v, w;
int n, W;

int solve(int weight, int ind) {
  if (weight > W)
    return -1e17;
  if (ind == n + 1)
    return 0;
  if (dp[weight][ind] != -1)
    return dp[weight][ind];
  long long ans = -1e17;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, solve(weight + w[ind], ind + 1) + v[ind]);
    ans = max(ans, solve(weight, ind + 1));
  }
  return dp[weight][ind] = ans;
}

int32_t main() {
  memset(dp, -1, sizeof dp);
  cin >> n >> W;
  v.resize(n + 1);
  w.resize(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> w[i] >> v[i];
  cout << solve(0, 1);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define IO                                                                     \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define int long long
using namespace std;
const int MX = 1e5 + 5;
int dp[MX][105];
vector<int> v, w;
int n, W;

int solve(int weight, int ind) {
  if (weight > W)
    return -1e17;
  if (ind == n + 1)
    return 0;
  if (dp[weight][ind] != -1)
    return dp[weight][ind];
  long long ans = -1e17;
  ans = max(ans, solve(weight + w[ind], ind + 1) + v[ind]);
  ans = max(ans, solve(weight, ind + 1));
  return dp[weight][ind] = ans;
}

int32_t main() {
  memset(dp, -1, sizeof dp);
  cin >> n >> W;
  v.resize(n + 1);
  w.resize(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> w[i] >> v[i];
  cout << solve(0, 1);
  return 0;
}
Problem p03163 (C++)

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

using namespace std;

int n, W;
int w[110], v[110];
long long dp[10100];

int main() {
  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 = W; j >= w[i]; j--) {
      dp[j] = max(dp[j], dp[j - w[i]] + 1ll * v[i]);
    }
  }

  long long ans = 0;
  for (int i = 0; i <= W; i++)
    ans = max(ans, dp[i]);
  cout << ans << '\n';
}


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

using namespace std;

int n, W;
int w[110], v[110];
long long dp[110000];

int main() {
  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 = W; j >= w[i]; j--) {
      dp[j] = max(dp[j], dp[j - w[i]] + 1ll * v[i]);
    }
  }

  long long ans = 0;
  for (int i = 0; i <= W; i++)
    ans = max(ans, dp[i]);
  cout << ans << '\n';
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define f(i, a, b) for (int i = (a); i < (b); i++)
#define frr(i, a, b) for (ll i = (a - 1); i >= (b); i--)
#define vi vector<int>
#define de deque<int>
#define del deque<ll>
#define vl vector<ll>
#define pb push_back
#define ppb pop_back()
#define pf push_front
#define ppf pop_front()
#define mpp map<ll, ll>
#define rev(a) reverse(a.begin(), a.end())
#define srt(a) sort(a.begin(), a.end())
#define ln(a) a.length()
#define sz(a) a.size()
#define endl "\n"
int M = 1000000007;
ll n;
ll dp[105][105];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  cin >> n;
  ll mw;
  cin >> mw;
  ll w[n], v[n];
  f(i, 0, n) { cin >> w[i] >> v[i]; }
  f(i, 0, n) { dp[i][0] = 0; }
  f(j, 0, mw + 1) {
    if (j >= w[0]) {
      dp[0][j] = v[0];
    } else {
      dp[0][j] = 0;
    }
  }
  f(i, 1, n) {
    f(j, 1, mw + 1) {
      if (j >= w[i]) {
        dp[i][j] = max(dp[i - 1][j], v[i] + dp[i - 1][j - w[i]]);
      } else {
        dp[i][j] = dp[i - 1][j];
      }
    }
  }
  cout << dp[n - 1][mw] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define f(i, a, b) for (int i = (a); i < (b); i++)
#define frr(i, a, b) for (ll i = (a - 1); i >= (b); i--)
#define vi vector<int>
#define de deque<int>
#define del deque<ll>
#define vl vector<ll>
#define pb push_back
#define ppb pop_back()
#define pf push_front
#define ppf pop_front()
#define mpp map<ll, ll>
#define rev(a) reverse(a.begin(), a.end())
#define srt(a) sort(a.begin(), a.end())
#define ln(a) a.length()
#define sz(a) a.size()
#define endl "\n"
int M = 1000000007;
ll n;
ll dp[105][100005];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  cin >> n;
  ll mw;
  cin >> mw;
  ll w[n], v[n];
  f(i, 0, n) { cin >> w[i] >> v[i]; }
  f(i, 0, n) { dp[i][0] = 0; }
  f(j, 0, mw + 1) {
    if (j >= w[0]) {
      dp[0][j] = v[0];
    } else {
      dp[0][j] = 0;
    }
  }
  f(i, 1, n) {
    f(j, 1, mw + 1) {
      if (j >= w[i]) {
        dp[i][j] = max(dp[i - 1][j], v[i] + dp[i - 1][j - w[i]]);
      } else {
        dp[i][j] = dp[i - 1][j];
      }
    }
  }
  cout << dp[n - 1][mw] << endl;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const ll INF = 1LL << 60;
const int MAX = 100100;

// input
int N;
ll W, weight[110], value[110];

// dp table
ll dp[110][MAX] = {0};

int main() {

  // input
  cin >> N >> W;
  for (int i = 0; i < N; i++)
    cin >> weight[i] >> value[i];

  // loop
  for (int i = 0; i < N; i++) {
    for (int sum_w = 0; sum_w <= W; sum_w++) {

      // choose i-th item
      chmax(dp[i + 1][sum_w], dp[i][sum_w - weight[i]] + value[i]);

      // don't choose i-th item
      chmax(dp[i + 1][sum_w], dp[i][sum_w]);
    }
  }

  // output
  cout << dp[N][W] << '\n';
}


Fixed submission:
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const ll INF = 1LL << 60;
const int MAX = 100100;

// input
int N;
ll W, weight[110], value[110];

// dp table
ll dp[110][MAX] = {0};

int main() {

  // input
  cin >> N >> W;
  for (int i = 0; i < N; i++)
    cin >> weight[i] >> value[i];

  // loop
  for (int i = 0; i < N; i++) {
    for (int sum_w = 0; sum_w <= W; sum_w++) {

      // choose i-th item
      if (sum_w - weight[i] >= 0) {
        chmax(dp[i + 1][sum_w], dp[i][sum_w - weight[i]] + value[i]);
      }

      // don't choose i-th item
      chmax(dp[i + 1][sum_w], dp[i][sum_w]);
    }
  }

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

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

int main() {

  int N, W;
  cin >> N >> W;
  ll dp[101][100001] = {0};

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

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

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <deque>
#include <iterator>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, s, n) for (long long i = (s); i(n); i++)
#define ll long long

int main() {

  int N, W;
  cin >> N >> W;
  ll dp[101][100001] = {0};

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

  //  rep(i,N){
  //    rep(j,W){
  //      cout << dp[i][j] <<" ";
  //    }
  //    cout<<endl;
  //  }
  ll ans = 0;
  rep(i, W + 1) ans = max(ans, dp[N][i]);
  cout << ans << endl;
}
Problem p03163 (C++)

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

long long n, val[100005], w, v, k, mxw, ans;
bool dp[100005];
int main() {
  dp[0] = true;
  scanf("%lld%lld", &n, &mxw);
  for (int i = 1; i <= n; i++) {
    scanf("%lld%lld", &w, &v);
    for (int j = mxw; j >= 0; j--) {
      if (dp[j]) {
        dp[j + w] = true;
        val[j + w] = max(val[j + w], val[j] + v);
      }
    }
  }
  ans = 0;
  for (int i = mxw; i >= 1; i--) {
    ans = max(ans, val[i]);
  }
  printf("%lld\n", ans);
  return 0;
}

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

long long n, val[100005], w, v, k, mxw, ans;
bool dp[100005];
int main() {
  dp[0] = true;
  scanf("%lld%lld", &n, &mxw);
  for (int i = 1; i <= n; i++) {
    scanf("%lld%lld", &w, &v);
    for (int j = mxw; j >= 0; j--) {
      if (dp[j] == true && j + w <= mxw) {
        dp[j + w] = true;
        val[j + w] = max(val[j + w], val[j] + v);
      }
    }
  }
  ans = 0;
  for (int i = mxw; i >= 1; i--) {
    ans = max(ans, val[i]);
  }
  printf("%lld\n", ans);
  return 0;
}