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

Buggy submission (status: Runtime Error):
#include <stdio.h>
const int W = 1e5 + 10;
long long int max(long long int a, long long int b) { return a > b ? a : b; }
int main() {
  int n, w, p, v;
  long long int a[W], ans = 0;
  scanf("%d%d", &n, &w);
  for (int i = 0; i <= w; i++)
    a[i] = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &p, &v);
    for (int j = w; j >= p; j--)
      a[i] = max(a[i], a[i - p] + v);
  }
  for (int j = w; j >= 0; j--)
    ans = max(ans, a[j]);
  printf("%lld\n", ans);
}

Fixed submission:
#include <stdio.h>
const int W = 1e5 + 10;
long long int max(long long int a, long long int b) { return a > b ? a : b; }
int main() {
  int n, w, p, v;
  long long int a[W], ans = 0;
  scanf("%d%d", &n, &w);
  for (int i = 0; i <= w; i++)
    a[i] = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &p, &v);
    for (int j = w; j >= p; j--)
      a[j] = max(a[j], a[j - p] + v);
  }
  for (int j = w; j >= 0; j--)
    ans = max(ans, a[j]);
  printf("%lld\n", ans);
}
Problem p03163 (C++)

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

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

  vector<ll> w(n), v(n);
  rep(i, n) cin >> w[i] >> v[i];

  vector<vector<ll>> dp(100010, vector<ll>(101));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= W; ++j) {
      if (j - w[i] >= 0) {
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j - w[i]] + v[i]);
      }
      dp[i + 1][j] = max(dp[i][j], dp[i + 1][j]);
    }
  }
  cout << dp[n][W] << endl;
}

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

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

  vector<ll> w(n), v(n);
  rep(i, n) cin >> w[i] >> v[i];

  vector<vector<ll>> dp(110, vector<ll>(100100));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= W; ++j) {
      if (j - w[i] >= 0) {
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j - w[i]] + v[i]);
      }
      dp[i + 1][j] = max(dp[i][j], dp[i + 1][j]);
    }
  }
  cout << dp[n][W] << endl;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
long long w[N], v[N];
int n, W;
long long f[N];

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
long long w[N], v[N];
int n, W;
long long f[100010];

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <vector>

using namespace std;

long long f[10010];

int main() {
  int n, w;
  scanf("%d%d", &n, &w);
  // f[i][j]= f[i-1][j] / f[i-1][j-w[i]]+v[i];
  for (int i = 0; i < n; ++i) {
    int ww, vv;
    scanf("%d%d", &ww, &vv);
    for (int j = w; j >= ww; --j)
      if (f[j - ww] + vv > f[j])
        f[j] = f[j - ww] + vv;
  }
  printf("%lld\n", f[w]);
}


Fixed submission:
#include <algorithm>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <vector>

using namespace std;

long long f[100010];

int main() {
  int n, w;
  scanf("%d%d", &n, &w);
  // f[i][j]= f[i-1][j] / f[i-1][j-w[i]]+v[i];
  for (int i = 0; i < n; ++i) {
    int ww, vv;
    scanf("%d%d", &ww, &vv);
    for (int j = w; j >= ww; --j)
      if (f[j - ww] + vv > f[j])
        f[j] = f[j - ww] + vv;
  }
  printf("%lld\n", f[w]);
}
Problem p03163 (C++)

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

using namespace std;

int main() {
  long N, W;
  cin >> N >> W;
  vector<long> v(N);
  vector<long> w(N);

  // dp[i][j] : 商品iまで、重さjまでの価値最大値
  vector<vector<long>> dp(N, vector<long>(W + 1, 0));

  for (int i = 0; i < N; i++) {
    cin >> w.at(i) >> v.at(i);
  }

  for (int j = 0; j < W + 1; j++) {
    dp[0][j] = (j < w[0]) ? 0 : v[0];
  }

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

      cerr << "i, j, dp.i.j: " << i << ", " << j << ", " << dp[i][j] << endl;
    }
  }

  cout << dp[N - 1][W] << endl;
}

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

using namespace std;

int main() {
  long N, W;
  cin >> N >> W;
  vector<long> v(N);
  vector<long> w(N);

  // dp[i][j] : 商品iまで、重さjまでの価値最大値
  vector<vector<long>> dp(N, vector<long>(W + 1, 0));

  for (int i = 0; i < N; i++) {
    cin >> w.at(i) >> v.at(i);
  }

  for (int j = 0; j < W + 1; j++) {
    dp[0][j] = (j < w[0]) ? 0 : v[0];
  }

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

  cout << dp[N - 1][W] << endl;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define FIO ios_base::sync_with_stdio(false); // cin.tie(NULL); cout.tie(NULL)
#define mp make_pair
#define pb push_back
#define fs first
#define sc second

using namespace std;
typedef long long ll;
typedef long double ld;

/* BIEN CUC BO BEGIN*/
ll t, n, A[3][102], w;

ll DD[100002], ans;
/* BIEN CUC BO END*/

int main() {
  FIO;
  cin >> n >> w;
  for (int i = 1; i <= n; ++i) {
    cin >> A[1][i] >> A[2][i];
    for (int j = w; j >= 0; --j) {
      DD[j + A[1][i]] = max(DD[j + A[1][i]], DD[j] + A[2][i]);
    }
  }

  for (int i = 0; i <= w; ++i) {
    ans = max(ans, DD[i]);
  }

  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define FIO ios_base::sync_with_stdio(false); // cin.tie(NULL); cout.tie(NULL)
#define mp make_pair
#define pb push_back
#define fs first
#define sc second

using namespace std;
typedef long long ll;
typedef long double ld;

/* BIEN CUC BO BEGIN*/
ll t, n, A[3][102], w;

ll DD[200012], ans;
/* BIEN CUC BO END*/

int main() {
  FIO;
  cin >> n >> w;
  for (int i = 1; i <= n; ++i) {
    cin >> A[1][i] >> A[2][i];
    for (int j = w; j >= 0; --j) {
      DD[j + A[1][i]] = max(DD[j + A[1][i]], DD[j] + A[2][i]);
    }
  }

  for (int i = 0; i <= w; ++i) {
    ans = max(ans, DD[i]);
  }

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

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

const long long mod = 998244353;
const int N = 1e3 + 5;

#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define ll long long
#define fr(i, l, m) for (int i = l; i < m; i++)
#define vii vector<int>
#define vpr vector<pair<ll, ll>>
ll pwr(ll x, ll y);
bool isprime(int n);

int a[2][N];
ll dp[N][105];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n, w;
  cin >> n >> w;
  fr(i, 1, n + 1) { cin >> a[0][i] >> a[1][i]; }

  fr(i, 0, w + 1) {
    fr(j, 0, n + 1) {
      if (i == 0 || j == 0) {
        dp[i][j] = 0;
      } else {
        if (a[0][j] <= i) {
          dp[i][j] = max((ll)a[1][j] + dp[i - a[0][j]][j - 1], dp[i][j - 1]);
        } else {
          dp[i][j] = dp[i][j - 1];
        }
      }
    }
  }

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

  return 0;
}
ll pwr(ll x, ll y) {
  ll ans = 1;
  x = x % mod;
  while (y > 0) {
    if (y & 1)
      ans = (x * (ans % mod)) % mod;
    x = (x * x) % mod;
    y = y / 2;
  }
  return ans % mod;
}

bool isprime(int n) {
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;
  if (n % 2 == 0 || n % 3 == 0)
    return false;

  for (int i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0)
      return false;

  return true;
}


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

const long long mod = 998244353;
const int N = 1e5 + 5;

#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define ll long long
#define fr(i, l, m) for (int i = l; i < m; i++)
#define vii vector<int>
#define vpr vector<pair<ll, ll>>
ll pwr(ll x, ll y);
bool isprime(int n);

int a[2][N];
ll dp[N][105];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n, w;
  cin >> n >> w;
  fr(i, 1, n + 1) { cin >> a[0][i] >> a[1][i]; }

  fr(i, 0, w + 1) {
    fr(j, 0, n + 1) {
      if (i == 0 || j == 0) {
        dp[i][j] = 0;
      } else {
        if (a[0][j] <= i) {
          dp[i][j] = max((ll)a[1][j] + dp[i - a[0][j]][j - 1], dp[i][j - 1]);
        } else {
          dp[i][j] = dp[i][j - 1];
        }
      }
    }
  }

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

  return 0;
}
ll pwr(ll x, ll y) {
  ll ans = 1;
  x = x % mod;
  while (y > 0) {
    if (y & 1)
      ans = (x * (ans % mod)) % mod;
    x = (x * x) % mod;
    y = y / 2;
  }
  return ans % mod;
}

bool isprime(int n) {
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;
  if (n % 2 == 0 || n % 3 == 0)
    return false;

  for (int i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0)
      return false;

  return true;
}
Problem p03163 (C++)

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

int main() {
#define int long long int
  int n, w;
  cin >> n >> w;
  vector<pair<int, int>> v;
  int dp[10000][10000];
  v.push_back({0, 0});
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    v.push_back(make_pair(a, b));
  }
  for (int i = 0; i <= n; i++)
    dp[i][0] = 0;
  for (int i = 0; i <= w; i++)
    dp[0][i] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= w; j++) {
      if (v[i].first > j)
        dp[i][j] = dp[i - 1][j];
      else
        dp[i][j] = max(dp[i - 1][j], v[i].second + dp[i - 1][j - v[i].first]);
    }
  }
  cout << dp[n][w] << endl;
  return 0;
}


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

int main() {
#define int long long int
  int n, w;
  cin >> n >> w;
  vector<pair<int, int>> v;
  int dp[103][100003];
  v.push_back({0, 0});
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    v.push_back(make_pair(a, b));
  }
  for (int i = 0; i <= n; i++)
    dp[i][0] = 0;
  for (int i = 0; i <= w; i++)
    dp[0][i] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= w; j++) {
      if (v[i].first > j)
        dp[i][j] = dp[i - 1][j];
      else
        dp[i][j] = max(dp[i - 1][j], v[i].second + dp[i - 1][j - v[i].first]);
    }
  }
  cout << dp[n][w] << endl;
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define N 100005
#define s second
#define f first
#define ll long long
using namespace std;
pair<int, ll> a[101];
ll dp[101][N];
int main() {
  int n, w, j, i;
  cin >> n >> w;
  for (i = 1; i <= n; ++i)
    cin >> a[i].f >> a[i].s;
  for (i = 1; i <= n + 1; ++i) {
    for (j = 1; j <= w + 1; ++j) {
      if (a[i - 1].f <= j)
        dp[i][j] = max(dp[i - 1][j - a[i - 1].f] + a[i - 1].s, dp[i - 1][j]);
      else
        dp[i][j] = dp[i - 1][j];
    }
  }
  cout << dp[n + 1][w];
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define N 100005
#define s second
#define f first
#define ll long long
using namespace std;
pair<int, ll> a[105];
ll dp[105][N];
int main() {
  int n, w, j, i;
  cin >> n >> w;
  for (i = 1; i <= n; ++i)
    cin >> a[i].f >> a[i].s;
  for (i = 1; i <= n + 1; ++i) {
    for (j = 1; j <= w + 1; ++j) {
      if (a[i - 1].f <= j)
        dp[i][j] = max(dp[i - 1][j - a[i - 1].f] + a[i - 1].s, dp[i - 1][j]);
      else
        dp[i][j] = dp[i - 1][j];
    }
  }
  cout << dp[n + 1][w];
  return 0;
}
Problem p03163 (C++)

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

using namespace std;
int dp[101][100001];
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int n, capacity;

  cin >> n >> capacity;
  vector<int> wt(n);
  vector<int> val(n);

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

  for (int i = 0; i <= capacity; i++) {
    for (int j = 0; j <= n; j++) {
      if (i == 0 or j == 0)
        dp[i][j] = 0;
      else if (i >= wt[j - 1])
        dp[i][j] = max(val[j - 1] + dp[i - wt[j - 1]][j - 1], dp[i][j - 1]);
      else
        dp[i][j] = dp[i][j - 1];
    }
  }

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

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

using namespace std;
int dp[100001][101];
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int n, capacity;

  cin >> n >> capacity;
  vector<int> wt(n);
  vector<int> val(n);

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

  for (int i = 0; i <= capacity; i++) {
    for (int j = 0; j <= n; j++) {
      if (i == 0 or j == 0)
        dp[i][j] = 0;
      else if (i >= wt[j - 1])
        dp[i][j] = max(val[j - 1] + dp[i - wt[j - 1]][j - 1], dp[i][j - 1]);
      else
        dp[i][j] = dp[i][j - 1];
    }
  }

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define endl "\n"
const int MOD = 1e9 + 7;
#ifndef HOME
#define cerr                                                                   \
  if (0)                                                                       \
  cerr
#endif
const int MaxW = 1e5 + 5;
int n, weight;
int dp[105][MaxW];
int w[105];
int v[105];
int solve(int i, int can) {

  if (can > weight) {
    return -1e18;
  }
  if (i == n) {
    return 0;
  }

  int &ans = dp[i][can];
  if (ans != -1) {
    return ans;
  }
  // don't take current item or take current item
  return max(solve(i + 1, can), v[i] + solve(i + 1, can + w[i]));
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> weight;
  for (int i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
  }
  memset(dp, -1, sizeof(dp));
  cout << solve(0, 0) << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define endl "\n"
const int MOD = 1e9 + 7;
#ifndef HOME
#define cerr                                                                   \
  if (0)                                                                       \
  cerr
#endif
const int MaxW = 1e5 + 5;
int n, weight;
int dp[105][MaxW];
int w[105];
int v[105];
int solve(int i, int can) {

  if (can > weight) {
    return -1e18;
  }
  if (i == n) {
    return 0;
  }

  int &ans = dp[i][can];
  if (ans != -1) {
    return ans;
  }
  // don't take current item or take current item
  return ans = max(solve(i + 1, can), v[i] + solve(i + 1, can + w[i]));
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> weight;
  for (int i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
  }
  memset(dp, -1, sizeof(dp));
  cout << solve(0, 0) << endl;

  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define maxx 100100
long long dp[100][maxx], peso[maxx], valor[maxx];

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

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

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define maxx 100100
long long dp[200][maxx], peso[200], valor[200];

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

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

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

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

int main() {
  long long N, W;
  vector<long long> w(101), v(101);
  vector<vector<long long>> dp(101, vector<long long>(10001, 0));
  cin >> N >> W;
  for (int i = 1; i <= N; i++) {
    cin >> w[i];
    cin >> v[i];
  }
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= W; j++) {
      if (j < w[i]) {
        dp[i][j] = dp[i - 1][j];
      } else {
        dp[i][j] = max(dp[i - 1][j], v[i] + dp[i - 1][j - w[i]]);
      }
    }
  }

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

  return 0;
}


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

int main() {
  long long N, W;
  vector<long long> w(101), v(101);
  vector<vector<long long>> dp(101, vector<long long>(100001, 0));
  cin >> N >> W;
  for (int i = 1; i <= N; i++) {
    cin >> w[i];
    cin >> v[i];
  }
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= W; j++) {
      if (j < w[i]) {
        dp[i][j] = dp[i - 1][j];
      } else {
        dp[i][j] = max(dp[i - 1][j], v[i] + dp[i - 1][j - w[i]]);
      }
    }
  }

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

  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define sanyes                                                                 \
  ios_base::sync_with_stdio(0), cout.tie(0);                                   \
  cin.tie(0);
#define llong long long
#define pb push_back
#define bpc __builtin_popcount

const llong mxn = 1e5 + 7;
const llong mod = 998244353;
const llong inf = 1e18 + 9;

using namespace std;

llong n, k, dp[101][10001], w[mxn], v[mxn], c[mxn], mx = -inf;

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


Fixed submission:
#include <bits/stdc++.h>
#define sanyes                                                                 \
  ios_base::sync_with_stdio(0), cout.tie(0);                                   \
  cin.tie(0);
#define llong long long
#define pb push_back
#define bpc __builtin_popcount

const llong mxn = 1e5 + 7;
const llong mod = 998244353;
const llong inf = 1e18 + 9;

using namespace std;

llong n, k, dp[101][mxn], w[mxn], v[mxn], mx = -inf;

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

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

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

  int wt[n], val[n];

  for (int i = 0; i < n; ++i) {
    cin >> wt[i] >> val[i];
  }
  long long dp[1009][100009];
  for (int i = 0; i < n + 1; ++i) {
    for (int j = 0; j < W + 1; ++j) {
      if (i == 0 || j == 0) {
        dp[i][j] = 0;
        continue;
      }

      if (wt[i - 1] <= j) {
        dp[i][j] = max(val[i - 1] + dp[i - 1][j - wt[i - 1]], dp[i - 1][j]);
      } else {
        dp[i][j] = dp[i - 1][j];
      }
    }
  }
  cout << dp[n][W];
}


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

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

  int wt[n], val[n];

  for (int i = 0; i < n; ++i) {
    cin >> wt[i] >> val[i];
  }
  long long dp[109][100009];
  for (int i = 0; i < n + 1; ++i) {
    for (int j = 0; j < W + 1; ++j) {
      if (i == 0 || j == 0) {
        dp[i][j] = 0;
        continue;
      }

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

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

#define ll long long

using namespace std;

vector<ll> v;
vector<ll> w;

ll knapsack(ll x, ll n, vector<vector<ll>> dp) {

  if (x == 0 || n < 0)
    return 0;

  if (dp[n][x] > 0) {
    return dp[n][x];
  }
  if (w[n] > x) {
    dp[n][x] = knapsack(x, n - 1, dp);
  } else {
    dp[n][x] =
        max(knapsack(x - w[n], n - 1, dp) + v[n], knapsack(x, n - 1, dp));
  }
  return dp[n][x];
}

int main() {
  ll n, x;
  cin >> n >> x;
  v.assign(n, 0);
  w.assign(n, 0);
  vector<vector<ll>> dp;
  for (int i = 0; i < n; i++) {
    vector<ll> row(x + 1);
    dp.push_back(row);
  }

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

  cout << knapsack(x, n - 1, dp) << "\n";
}

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

#define ll long long

using namespace std;

vector<ll> v;
vector<ll> w;

ll knapsack(ll x, ll n, vector<vector<ll>> &dp) {

  if (x == 0 || n < 0)
    return 0;

  if (dp[n][x] > 0) {
    return dp[n][x];
  }
  if (w[n] > x) {
    dp[n][x] = knapsack(x, n - 1, dp);
  } else {
    dp[n][x] =
        max(knapsack(x - w[n], n - 1, dp) + v[n], knapsack(x, n - 1, dp));
  }
  return dp[n][x];
}

int main() {
  ll n, x;
  cin >> n >> x;
  v.assign(n, 0);
  w.assign(n, 0);
  vector<vector<ll>> dp;
  for (int i = 0; i < n; i++) {
    vector<ll> row(x + 1);
    dp.push_back(row);
  }

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

  cout << knapsack(x, n - 1, dp) << "\n";
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N = 200;
int main() {
  ios_base::sync_with_stdio(0);
  cout.tie(0);
  cin.tie(0);
  ll n, cap, item, maxis = 0, dp[N], i, j, weight;
  cin >> n >> cap;
  for (i = 0; i <= cap; ++i)
    dp[i] = 0;
  for (i = 0; i < n; ++i) {
    cin >> weight >> item;
    for (j = cap; j >= weight; --j)
      dp[j] = max(dp[j - weight] + item, dp[j]);
  }
  for (i = 0; i <= cap; ++i)
    maxis = max(maxis, dp[i]);
  cout << maxis;
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N = 150000;
int main() {
  ios_base::sync_with_stdio(0);
  cout.tie(0);
  cin.tie(0);
  ll n, cap, item, maxis = 0, dp[N], i, j, weight;
  cin >> n >> cap;
  for (i = 0; i <= cap; ++i)
    dp[i] = 0;
  for (i = 0; i < n; ++i) {
    cin >> weight >> item;
    for (j = cap; j >= weight; --j)
      dp[j] = max(dp[j - weight] + item, dp[j]);
  }
  for (i = 0; i <= cap; ++i)
    maxis = max(maxis, dp[i]);
  cout << maxis;
}
Problem p03163 (C++)

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

using namespace std;

long int dp[101][12345];
long int A[101][2];
int main() {
  int n, w;

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

  for (int i = 0; i < n; i++)
    scanf("%ld %ld", &A[i][0], &A[i][1]);

  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= w; j++) {
      if (i == 0 || j == 0)
        dp[i][j] = 0;
      else if (j < A[i - 1][0])
        dp[i][j] = dp[i - 1][j];
      else {
        dp[i][j] = max(A[i - 1][1] + dp[i - 1][j - A[i - 1][0]], dp[i - 1][j]);
      }
    }
  }
  printf("%ld\n", dp[n][w]);

  return 0;
}

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

using namespace std;

long int dp[1000][123456];
long int A[1000][2];
int main() {
  int n, w;

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

  for (int i = 0; i < n; i++)
    scanf("%ld %ld", &A[i][0], &A[i][1]);

  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= w; j++) {
      if (i == 0 || j == 0)
        dp[i][j] = 0;
      else if (j < A[i - 1][0])
        dp[i][j] = dp[i - 1][j];
      else {
        dp[i][j] = max(A[i - 1][1] + dp[i - 1][j - A[i - 1][0]], dp[i - 1][j]);
      }
    }
  }
  printf("%ld\n", dp[n][w]);

  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
int n, W;
int w[100], v[100];
ll dp[101][100001];
int main() {
  cin >> n >> W;
  for (int i = 0; i < n; i++)
    cin >> w[i] >> v[i];
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= W; j++) {
      if (j < w[i])
        dp[i + 1][j] = dp[i][j];
      else
        dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]);
    }
  }
  cout << dp[n][W] << "\n";

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
int n, W;
int w[100], v[100];
ll dp[101][100001];
int main() {
  cin >> n >> W;
  for (int i = 0; i < n; i++)
    cin >> w[i] >> v[i];
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= W; j++) {
      if (j < w[i])
        dp[i + 1][j] = dp[i][j];
      else
        dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]);
    }
  }
  cout << dp[n][W] << "\n";

  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define mkp make_pair
#define int long long
#define fori(j, n) for (int i = j; i < n; i++)
#define forb(n, j) for (int i = n - 1; i >= 0; i--)
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"
#define PI 3.141592653589793
#define M 1000000007
using namespace std;
int fast(int a, int b) {
  int ans = 1;
  a = a % M;
  if (a == 0)
    return 0;
  while (b > 0) {
    if (b & 1) {
      ans = (ans * a) % M;
    }
    b >>= 1;
    a = (a * a) % M;
  }
}
int N;
int v[105];
int w[105];
int dp[110][110];
int cal(int i, int W) {
  if (W < 0)
    return INT_MIN;
  if (i < 0 || w == 0)
    return 0;
  if (dp[i][W] != -1)
    return dp[i][W];
  int notinclude = cal(i - 1, W);
  int include = v[i] + cal(i - 1, W - w[i]);
  return dp[i][W] = max(include, notinclude);
}
int32_t main() {
  IOS;
  int W;
  memset(dp, -1, sizeof(dp));
  cin >> N >> W;
  fori(0, N) { cin >> w[i] >> v[i]; }
  cout << cal(N - 1, W);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define mkp make_pair
#define int long long
#define fori(j, n) for (int i = j; i < n; i++)
#define forb(n, j) for (int i = n - 1; i >= 0; i--)
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"
#define PI 3.141592653589793
#define M 1000000007
using namespace std;
int fast(int a, int b) {
  int ans = 1;
  a = a % M;
  if (a == 0)
    return 0;
  while (b > 0) {
    if (b & 1) {
      ans = (ans * a) % M;
    }
    b >>= 1;
    a = (a * a) % M;
  }
}
int N;
int v[105];
int w[105];
int dp[110][100001];
int cal(int i, int W) {
  if (W < 0)
    return INT_MIN;
  if (i < 0 || w == 0)
    return 0;
  if (dp[i][W] != -1)
    return dp[i][W];
  int notinclude = cal(i - 1, W);
  int include = v[i] + cal(i - 1, W - w[i]);
  return dp[i][W] = max(include, notinclude);
}
int32_t main() {
  IOS;
  int W;
  memset(dp, -1, sizeof(dp));
  cin >> N >> W;
  fori(0, N) { cin >> w[i] >> v[i]; }
  cout << cal(N - 1, W);
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define endl "\n"
const int MAX = 100005;
const long long mod = 1.0e9 + 7;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  ll n, w, i, j, k;
  cin >> n >> w;
  vector<pair<ll, ll>> v;
  for (i = 0; i < n; i++) {
    cin >> j >> k;
    v.push_back(make_pair(j, k));
  }
  sort(v.begin(), v.end());
  ll dp[n + 1][w + 1];
  memset(dp, 0, sizeof(dp));
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= w; j++) {
      if (j >= v[i - 1].first)
        dp[i][j] =
            max(dp[i - 1][j], v[i - 1].second + dp[i - 1][j - v[i - 1].first]);
      else
        dp[i][j] = dp[i - 1][j];
    }
  }
  // cout<<dp[w]<<endl;
  cout << dp[n][w] << endl;
  // cout<<ans<<endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define endl "\n"
const int MAX = 100005;
const long long mod = 1.0e9 + 7;

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

  ll n, w, i, j, k;
  cin >> n >> w;
  vector<pair<ll, ll>> v;
  for (i = 0; i < n; i++) {
    cin >> j >> k;
    v.push_back(make_pair(j, k));
  }
  sort(v.begin(), v.end());
  ll dp[n + 1][w + 1];
  memset(dp, 0, sizeof(dp));
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= w; j++) {
      if (j >= v[i - 1].first)
        dp[i][j] =
            max(dp[i - 1][j], v[i - 1].second + dp[i - 1][j - v[i - 1].first]);
      else
        dp[i][j] = dp[i - 1][j];
    }
  }
  // cout<<dp[w]<<endl;
  cout << dp[n][w] << endl;
  // cout<<ans<<endl;
  return 0;
}
Problem p03163 (C++)

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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef double db;

#define mp make_pair
#define pb push_back
#define pt(i) printf("%lld\n", (ll)i)
#define mp make_pair
#define ff first
#define ss second
#define pi acos(-1.0)

ll in() {
  ll a;
  scanf("%lld", &a);
  return a;
}
db din() {
  db a;
  scanf("%lf", &a);
  return a;
}
ll bigmod(ll b, ll p, ll md) {
  if (p == 0)
    return 1;
  if (p % 2 == 1) {
    return ((b % md) * bigmod(b, p - 1, md)) % md;
  } else {
    ll y = bigmod(b, p / 2, md);
    return (y * y) % md;
  }
}
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}

long long getRandom(long long a, long long b) {
  long long ret = (long long)rand() * (long long)rand();
  ret %= (b - a + 1);
  ret += a;
  return ret;
}

ll dp[105][100005], n, w, wi[105], vvi[105];

ll solve(ll pos, ll wei) {
  if (wei > w)
    return -1 * INT_MAX;
  if (dp[pos][wei] != -1)
    return dp[pos][wei];
  return dp[pos][wei] =
             max(vvi[pos] + solve(pos + 1, wei + wi[pos]), solve(pos + 1, wei));
}

int main() {
  //	freopen("in.c","r",stdin);
  //	freopen("out.c","w",stdout);
  n = in(), w = in();
  for (int i = 0; i < n; i++)
    wi[i] = in(), vvi[i] = in();
  memset(dp, -1, sizeof dp);
  pt(solve(0, 0));
}

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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef double db;

#define mp make_pair
#define pb push_back
#define pt(i) printf("%lld\n", (ll)i)
#define mp make_pair
#define ff first
#define ss second
#define pi acos(-1.0)

ll in() {
  ll a;
  scanf("%lld", &a);
  return a;
}
db din() {
  db a;
  scanf("%lf", &a);
  return a;
}
ll bigmod(ll b, ll p, ll md) {
  if (p == 0)
    return 1;
  if (p % 2 == 1) {
    return ((b % md) * bigmod(b, p - 1, md)) % md;
  } else {
    ll y = bigmod(b, p / 2, md);
    return (y * y) % md;
  }
}
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}

long long getRandom(long long a, long long b) {
  long long ret = (long long)rand() * (long long)rand();
  ret %= (b - a + 1);
  ret += a;
  return ret;
}

ll dp[105][100005], n, w, wi[105], vvi[105];

ll solve(ll pos, ll wei) {
  if (wei > w)
    return -1 * INT_MAX;
  if (pos == n)
    return 0;
  if (dp[pos][wei] != -1)
    return dp[pos][wei];
  return dp[pos][wei] =
             max(vvi[pos] + solve(pos + 1, wei + wi[pos]), solve(pos + 1, wei));
}

int main() {
  //	freopen("in.c","r",stdin);
  //	freopen("out.c","w",stdout);
  n = in(), w = in();
  for (int i = 0; i < n; i++)
    wi[i] = in(), vvi[i] = in();
  memset(dp, -1, sizeof dp);
  pt(solve(0, 0));
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>
using namespace std;

#define ll long long
#define endl '\n'
const ll mod = 1000000007;
ll dp[101][101];
pair<int, int> p[101];
int x, w;
ll f(int i, int w) {
  if (w == 0 || i == x)
    return 0;
  else if (p[i].first > w)
    return dp[i][w] = f(i + 1, w);
  else if (dp[i][w] != -1)
    return dp[i][w];
  return dp[i][w] = max(p[i].second + f(i + 1, w - p[i].first), f(i + 1, w));
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> x >> w;
  for (int i = 0; i < x; i++)
    cin >> p[i].first >> p[i].second;
  memset(dp, -1, sizeof(dp));
  cout << f(0, w) << endl;
  /*for (int i = 0; i < x; i++) {
          for (int j = 0; j <= w; j++) {
                  cout << dp[i][j] << " ";
          }
          cout << endl;
  }*/
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>
using namespace std;

#define ll long long
#define endl '\n'
const ll mod = 1000000007;
ll dp[101][(int)1e5 + 1];
pair<int, int> p[101];
int x, w;
ll f(int i, int w) {
  if (w == 0 || i == x)
    return 0;
  else if (p[i].first > w)
    return dp[i][w] = f(i + 1, w);
  else if (dp[i][w] != -1)
    return dp[i][w];
  return dp[i][w] = max(p[i].second + f(i + 1, w - p[i].first), f(i + 1, w));
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> x >> w;
  for (int i = 0; i < x; i++)
    cin >> p[i].first >> p[i].second;
  memset(dp, -1, sizeof(dp));
  cout << f(0, w) << endl;
  /*for (int i = 0; i < x; i++) {
          for (int j = 0; j <= w; j++) {
                  cout << dp[i][j] << " ";
          }
          cout << endl;
  }*/
  return 0;
}
Problem p03163 (C++)

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

using namespace std;

long long int dp[1000][1000] = {};

long long int knapsack(long long int wt[], long long int val[], long long int w,
                       long long int i) {
  if (w < 0) {
    return INT_MIN;
  } else if (i == -1) {
    return 0;
  } else if (dp[w][i] != 0) {
    return dp[w][i];
  } else {
    dp[w][i] = max(knapsack(wt, val, w - wt[i], i - 1) + val[i],
                   knapsack(wt, val, w, i - 1));
    return dp[w][i];
  }
}

int main() {
  long long int n, i, w;
  cin >> n >> w;
  long long int wt[n], val[n];
  for (i = 0; i < n; ++i) {
    cin >> wt[i] >> val[i];
  }
  cout << knapsack(wt, val, w, n - 1) << endl;
  return 0;
}

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

using namespace std;

long long int dp[100001][101] = {};

long long int knapsack(long long int wt[], long long int val[], long long int w,
                       long long int i) {
  if (w < 0) {
    return INT_MIN;
  } else if (i == -1) {
    return 0;
  } else if (dp[w][i] != 0) {
    return dp[w][i];
  } else {
    dp[w][i] = max(knapsack(wt, val, w - wt[i], i - 1) + val[i],
                   knapsack(wt, val, w, i - 1));
    return dp[w][i];
  }
}

int main() {
  long long int n, i, w;
  cin >> n >> w;
  long long int wt[n], val[n];
  for (i = 0; i < n; ++i) {
    cin >> wt[i] >> val[i];
  }
  cout << knapsack(wt, val, w, n - 1) << endl;
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm> // sort
#include <iostream>
#include <map>     // pair
#include <numeric> // accumulate(begin,end,初期値)
#include <string>
#include <vector>

typedef long long ll;
using namespace std;

#define FALSE printf("false\n");
#define TRUE printf("true\n");
#define all(x) (x).begin(), (x).end()
#define print(x) cout << x << endl
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define SUM(vec) accumulate(all(vec), 0)

const ll INF = 1e9 + 7;

// 各桁の和
template <typename T> T digit(T num) {
  T sum = 0;
  while (num) {
    sum += num % 10;
    num /= 10;
  }
  return sum;
}
template <typename T> T gcd(T a, T b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
template <typename T> T lcm(T a, T b) {
  T g = gcd(a, b);
  return a / g * b;
}
template <typename T> T power(T a, T b) {
  T tmp = 1;
  rep(i, b) { tmp *= a; }
  return tmp;
}

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

  ll dp[100][100100];

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

Fixed submission:
#include <algorithm> // sort
#include <iostream>
#include <map>     // pair
#include <numeric> // accumulate(begin,end,初期値)
#include <string>
#include <vector>

typedef long long ll;
using namespace std;

#define FALSE printf("false\n");
#define TRUE printf("true\n");
#define all(x) (x).begin(), (x).end()
#define print(x) cout << x << endl
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define SUM(vec) accumulate(all(vec), 0)

const ll INF = 1e9 + 7;

// 各桁の和
template <typename T> T digit(T num) {
  T sum = 0;
  while (num) {
    sum += num % 10;
    num /= 10;
  }
  return sum;
}
template <typename T> T gcd(T a, T b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
template <typename T> T lcm(T a, T b) {
  T g = gcd(a, b);
  return a / g * b;
}
template <typename T> T power(T a, T b) {
  T tmp = 1;
  rep(i, b) { tmp *= a; }
  return tmp;
}

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

  ll dp[110][100100];

  rep(i, n) {
    rep(j, W + 1) {
      if (j >= w[i]) {
        dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]);
      } else {
        dp[i + 1][j] = dp[i][j];
      }
    }
  }
  print(dp[n][W]);
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
#include <vector>
#define rep(i, x, n) for (int i = x; i < n; i++)
using namespace std;
const long long INF = 1LL << 60;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int N;
long long wv[110][2];
long long dp[110][100010];

int main() {
  int N, W;
  cin >> N >> W;
  rep(i, 0, N) {
    rep(r, 0, 2) { cin >> wv[i][r]; }
  }

  rep(i, 0, N) {
    rep(sum, 0, W + 1) {
      if (sum - wv[sum][0] >= 0) {
        chmax(dp[i + 1][sum], dp[i][sum - wv[i][0]] + wv[i][1]);
      }
      chmax(dp[i + 1][sum], dp[i][sum]);
    }
  }

  cout << dp[N][W] << endl;
}


Fixed submission:
#include <iostream>
#include <string>
#include <vector>
#define rep(i, x, n) for (int i = x; i < n; i++)
using namespace std;
const long long INF = 1LL << 60;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int N;
long long wv[110][2];
long long dp[110][100010];

int main() {
  int N, W;
  cin >> N >> W;
  rep(i, 0, N) {
    rep(r, 0, 2) { cin >> wv[i][r]; }
  }

  rep(i, 0, N) {
    rep(sum, 0, W + 1) {
      if (sum - wv[i][0] >= 0) {
        chmax(dp[i + 1][sum], dp[i][sum - wv[i][0]] + wv[i][1]);
      }
      chmax(dp[i + 1][sum], dp[i][sum]);
    }
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long int dp[100][100001] = {0};

int main() {
  int N, W;
  cin >> N >> W;
  long long int w[2][100];

  for (int i = 0; i < N; i++)
    cin >> w[0][i] >> w[1][i];

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

      dp[i + 1][sum] = max(dp[i + 1][sum], dp[i][sum]);
      //    cout << "dp[" << i+1 << "][" << sum << "]:" << dp[i+1][sum] << endl;
    }
  }

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long int dp[101][100001] = {0};

int main() {
  int N, W;
  cin >> N >> W;
  long long int w[2][100];

  for (int i = 0; i < N; i++)
    cin >> w[0][i] >> w[1][i];

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

      dp[i + 1][sum] = max(dp[i + 1][sum], dp[i][sum]);
      //    cout << "dp[" << i+1 << "][" << sum << "]:" << dp[i+1][sum] << endl;
    }
  }

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

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

#define ll long long
#define INF 100000000000
int n;
ll we, k;

ll dp[102][100003], w[100003], v[100003];

ll solve(int i, ll j) {
  if (i < 0) {
    return 0;
  }
  if (j - w[i] < 0)
    return dp[i][j] = max(dp[i][j], solve(i - 1, j));
  return dp[i][j] = max(solve(i - 1, j - w[i]) + v[i], solve(i - 1, j));
}

int main() {
  int t = 1, tn = 1;
  //    scanf("%d",&t);
  while (t--) {
    scanf("%d%lld", &n, &we);
    for (int i = 0; i < n; i++)
      for (int j = 0; j <= we; j++)
        dp[i][j] = -1;
    for (int i = 0; i < n; i++)
      scanf("%lld %lld", &w[i], &v[i]);
    printf("%lld", solve(n - 1, we));
  }
  return 0;
}


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

#define ll long long
#define INF 100000000000
int n;
ll we, k;

ll dp[102][100003], w[100003], v[100003];

ll solve(int i, ll j) {
  if (i < 0) {
    return 0;
  }
  if (dp[i][j] != -1)
    return dp[i][j];
  if (j - w[i] < 0)
    return dp[i][j] = max(dp[i][j], solve(i - 1, j));
  return dp[i][j] = max(solve(i - 1, j - w[i]) + v[i], solve(i - 1, j));
}

int main() {
  int t = 1, tn = 1;
  //    scanf("%d",&t);
  while (t--) {
    scanf("%d%lld", &n, &we);
    for (int i = 0; i < n; i++)
      for (int j = 0; j <= we; j++)
        dp[i][j] = -1;
    for (int i = 0; i < n; i++)
      scanf("%lld %lld", &w[i], &v[i]);
    printf("%lld", solve(n - 1, we));
  }
  return 0;
}
Problem p03163 (C++)

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

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

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

signed main() {
  int N, W;
  cin >> N >> W;
  vector<vector<int>> dp(100 + 10, vector<int>(100000 + 100, 0));
  int w[N], v[N];
  for (int i = 0; i < N; i++)
    cin >> w[i] >> v[i];
  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= W; j++) {
      if (j + w[i] <= W) {
        dp[i + 1][j + w[i]] = max(dp[i][j] + v[i], dp[i + 1][j + w[i]]);
      }
      dp[i + 1][j] = max(dp[i][j], dp[i + 1][j]);
    }
  }
  cout << dp[N][W] << endl;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;
long long int n, x, dp[1001][101], c, d, d1;

int main() {
  cin >> n >> x;
  int val[n + 1], wei[n + 1];
  for (int i = 0; i < n; i++)
    cin >> wei[i] >> val[i];
  for (int i = 1; i <= n; i++)
    dp[i][0] = 0;
  for (int i = 0; i <= x; i++)
    dp[0][i] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= x; j++) {
      if (wei[i - 1] <= j) {
        d1 = val[i - 1], c = dp[i - 1][j], d = dp[i - 1][j - wei[i - 1]];
        dp[i][j] = max(c, d + d1);
      } else
        dp[i][j] = dp[i - 1][j];
      // cout<<dp[i][j]<<" ";
    }
    // cout<<"\n";
  }
  cout << dp[n][x];
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;
long long int n, x, dp[101][100001], c, d, d1;

int main() {
  cin >> n >> x;
  int val[n + 1], wei[n + 1];
  for (int i = 0; i < n; i++)
    cin >> wei[i] >> val[i];
  for (int i = 1; i <= n; i++)
    dp[i][0] = 0;
  for (int i = 0; i <= x; i++)
    dp[0][i] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= x; j++) {
      if (wei[i - 1] <= j) {
        d1 = val[i - 1], c = dp[i - 1][j], d = dp[i - 1][j - wei[i - 1]];
        dp[i][j] = max(c, d + d1);
      } else
        dp[i][j] = dp[i - 1][j];
      // cout<<dp[i][j]<<" ";
    }
    // cout<<"\n";
  }
  cout << dp[n][x];
}
Problem p03163 (C++)

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

const int x = 101, y = 10001;
long long dp[x][y];
long long c[x], w[x], n, mx_w;

long long solve(int idx, int acc_w) {
  if (idx == n)
    return 0;
  if (dp[idx][acc_w] != -1)
    return dp[idx][acc_w];

  if (w[idx] + acc_w <= mx_w)
    return dp[idx][acc_w] = max(solve(idx + 1, acc_w),
                                c[idx] + solve(idx + 1, acc_w + w[idx]));
  else
    return dp[idx][acc_w] = solve(idx + 1, acc_w);
}
int main() {
  memset(dp, -1, sizeof dp);
  cin >> n >> mx_w;
  for (int i = 0; i < n; i++)
    cin >> w[i] >> c[i];
  cout << solve(0, 0) << '\n';
  return 0;
}


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

const int x = 101, y = 100001;
long long dp[x][y];
long long c[x], w[x], n, mx_w;

long long solve(int idx, int acc_w) {
  if (idx == n)
    return 0;
  if (dp[idx][acc_w] != -1)
    return dp[idx][acc_w];

  if (w[idx] + acc_w <= mx_w)
    return dp[idx][acc_w] = max(solve(idx + 1, acc_w),
                                c[idx] + solve(idx + 1, acc_w + w[idx]));
  else
    return dp[idx][acc_w] = solve(idx + 1, acc_w);
}
int main() {
  memset(dp, -1, sizeof dp);
  cin >> n >> mx_w;
  for (int i = 0; i < n; i++)
    cin >> w[i] >> c[i];
  cout << solve(0, 0) << '\n';
  return 0;
}