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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-ffloat-store")
#pragma GCC optimize("-fno-defer-pop")
#define ll long long int
#define ps push_back
#define fs first
#define sc second
#define mkp make_pair
#define mod 1000000007
ll an[101][100001];
ll maxwt(ll W, ll n, ll w[], ll v[]) {
  if (n == 0 || W == 0) {
    return 0;
  }
  if (an[n][W] > -1)
    return an[n][W];
  if (w[n] > W) {
    an[n][W] = maxwt(W, n - 1, w, v);
  } else {
    an[n][W] = max(maxwt(W, n - 1, w, v), v[n] + maxwt(W - w[n], n - 1, w, v));
  }
  return an[n][W];
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll n, W, i, j, k, l;
  cin >> n >> W;
  for (i = 0; i < 101; i++) {
    for (j = 0; j < 100001; j++) {
      an[i][j] = -1;
    }
  }
  ll w[n + 1], v[n + 1];
  for (i = 1; i <= n; i++) {
    cin >> w[i] >> v[i];
  }

  k = maxwt(W, n, w, v);
  cout << k;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-ffloat-store")
#pragma GCC optimize("-fno-defer-pop")
#define ll long long int
#define ps push_back
#define fs first
#define sc second
#define mkp make_pair
#define mod 1000000007
ll an[101][100001];
ll maxwt(ll W, ll n, ll w[], ll v[]) {
  if (n == 0 || W == 0) {
    return 0;
  }
  if (an[n][W] > -1)
    return an[n][W];
  if (w[n] > W) {
    an[n][W] = maxwt(W, n - 1, w, v);
  } else {
    an[n][W] = max(maxwt(W, n - 1, w, v), v[n] + maxwt(W - w[n], n - 1, w, v));
  }
  return an[n][W];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll n, W, i, j, k, l;
  cin >> n >> W;
  for (i = 0; i < 101; i++) {
    for (j = 0; j < 100001; j++) {
      an[i][j] = -1;
    }
  }
  ll w[n + 1], v[n + 1];
  for (i = 1; i <= n; i++) {
    cin >> w[i] >> v[i];
  }

  k = maxwt(W, n, w, v);
  cout << k;
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
#define watch(x) cerr << "\n" << (#x) << " is " << (x) << endl;
int n, W;
ll w[100005], v[100005], memo[105][100005];
using namespace std;
ll dp(int i, int free_weight) {
  if (memo[i][free_weight] != -1) {
    return memo[i][free_weight];
  }
  if (i == n) {
    return 0;
  }
  ll ret1 = 0, ret2;
  if (free_weight - w[i] >= 0)
    ret1 = v[i] + dp(i + 1, free_weight - w[i]);
  ret2 = dp(i + 1, free_weight);
  return max(ret1, ret2);
}
int main() {
  scanf("%d%d", &n, &W);
  for (int i = 0; i < n; ++i) {
    scanf("%lld%lld", &w[i], &v[i]);
  }
  memset(memo, -1, sizeof memo);
  printf("%lld", dp(0, W));
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define watch(x) cerr << "\n" << (#x) << " is " << (x) << endl;
int n, W;
ll w[100005], v[100005], memo[105][100005];
using namespace std;
ll dp(int i, int free_weight) {
  if (memo[i][free_weight] != -1) {
    return memo[i][free_weight];
  }
  if (i == n) {
    return 0;
  }
  ll ret1 = 0, ret2;
  if (free_weight - w[i] >= 0)
    ret1 = v[i] + dp(i + 1, free_weight - w[i]);
  ret2 = dp(i + 1, free_weight);
  return memo[i][free_weight] = max(ret1, ret2);
}
int main() {
  scanf("%d%d", &n, &W);
  for (int i = 0; i < n; ++i) {
    scanf("%lld%lld", &w[i], &v[i]);
  }
  memset(memo, -1, sizeof memo);
  printf("%lld", dp(0, W));
  return 0;
}
Problem p03163 (C++)

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

typedef long long ll;
const ll MOD = 1e9 + 7;
const ll inf = 1LL << 60;
const double eps = 1e-9;

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

ll dp[100 + 1][100 + 1] = {0};

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

  ll w[N], v[N];
  for (int i = 0; i < N; i++)
    cin >> w[i] >> v[i];
  // for (int i = 0; i < N; i++) cout << w[i] << v[i] << endl;

  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= W; j++) {
      if (j >= w[i]) {
        // cout << dp[i][j-w[i]] << endl;
        chmax(dp[i + 1][j], dp[i][j - w[i]] + v[i]);
        // cout << dp[i+1][j] << endl;
      }
      chmax(dp[i + 1][j], dp[i][j]);
    }
  }
  /*
  for (int i = 0; i <= N; i++) {
      for (int j = 0; j <= W; j++) {
          cout << dp[i][j] << " ";
      }
      cout << endl;
  }*/
  cout << dp[N][W] << endl;
  return 0;
}


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

typedef long long ll;
const ll MOD = 1e9 + 7;
const ll inf = 1LL << 60;
const double eps = 1e-9;

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

ll dp[100 + 1][100000 + 1] = {0};

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

  ll w[N], v[N];
  for (int i = 0; i < N; i++)
    cin >> w[i] >> v[i];
  // for (int i = 0; i < N; i++) cout << w[i] << v[i] << endl;

  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= W; j++) {
      if (j >= w[i]) {
        // cout << dp[i][j-w[i]] << endl;
        chmax(dp[i + 1][j], dp[i][j - w[i]] + v[i]);
        // cout << dp[i+1][j] << endl;
      }
      chmax(dp[i + 1][j], dp[i][j]);
    }
  }
  /*
  for (int i = 0; i <= N; i++) {
      for (int j = 0; j <= W; j++) {
          cout << dp[i][j] << " ";
      }
      cout << endl;
  }*/
  cout << dp[N][W] << endl;
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
// This code is written by Shammi Anand
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define Max(a, b, c)      max((a),max((b),(c))
#define Min(a, b, c) min(a, min(b, c))
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define mod 1000000007
#define nl "\n"
#define w(x)                                                                   \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)

const int N = 200;
ll W[N], V[N], w, n;
ll dp[N][N];

ll solve(ll capacity, ll item) {
  if (capacity == 0 || item == 0) {
    return 0;
  }
  if (dp[item][capacity] != -1) {
    return dp[item][capacity];
  }
  if (W[item] > capacity) {
    return dp[item][capacity] = solve(capacity, item - 1);
  } else {
    return dp[item][capacity] =
               max(V[item] + solve(capacity - W[item], item - 1),
                   solve(capacity, item - 1));
  }
}

int main() {
  cin >> n >> w;
  for (int i = 1; i <= n; i++) {
    cin >> W[i] >> V[i];
  }
  memset(dp, -1, sizeof(dp));
  cout << solve(w, n) << nl;

  return 0;
}

Fixed submission:
// This code is written by Shammi Anand
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define Max(a, b, c)      max((a),max((b),(c))
#define Min(a, b, c) min(a, min(b, c))
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define mod 1000000007
#define nl "\n"
#define w(x)                                                                   \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)

const int N = 1e5 + 5;
ll W[105], V[105], w, n;
ll dp[105][N];

ll solve(ll capacity, ll item) {
  if (capacity == 0 || item == 0) {
    return 0;
  }
  if (dp[item][capacity] != -1) {
    return dp[item][capacity];
  }
  if (W[item] > capacity) {
    return dp[item][capacity] = solve(capacity, item - 1);
  } else {
    return dp[item][capacity] =
               max(V[item] + solve(capacity - W[item], item - 1),
                   solve(capacity, item - 1));
  }
}

int main() {
  cin >> n >> w;
  for (int i = 1; i <= n; i++) {
    cin >> W[i] >> V[i];
  }
  memset(dp, -1, sizeof(dp));
  cout << solve(w, n) << nl;

  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pii pair<int, int>
#define fi first
#define se second
#define int long long
#define pb push_back
#define mod 1000000007
#define pll pair<long long, long long>
using namespace std;
long long power(long long a, long long b) {
  long long ans = 1;
  while (b > 0) {
    if (b & 1) {
      ans = (ans * a) % mod;
    }
    a = (a * a) % mod;
    b >>= 1;
  }
  return ans;
}
bool prime(int n) {
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}
int dp[100005] = {0};
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, w;
  cin >> n >> w;
  int a[n], b[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    cin >> b[i];
  }
  for (int i = 0; i < n; i++) {
    for (int j = w; j >= 0; j--) {
      dp[j + a[i]] = max(dp[j + a[i]], dp[j] + b[i]);
    }
  }
  int ans = 0;
  for (int i = 0; i <= w; i++) {
    ans = max(ans, dp[i]);
  }
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define pii pair<int, int>
#define fi first
#define se second
#define int long long
#define pb push_back
#define mod 1000000007
#define pll pair<long long, long long>
using namespace std;
long long power(long long a, long long b) {
  long long ans = 1;
  while (b > 0) {
    if (b & 1) {
      ans = (ans * a) % mod;
    }
    a = (a * a) % mod;
    b >>= 1;
  }
  return ans;
}
bool prime(int n) {
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}
int dp[10000005] = {0};
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, w;
  cin >> n >> w;
  int a[n], b[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    cin >> b[i];
  }
  for (int i = 0; i < n; i++) {
    for (int j = w; j >= 0; j--) {
      dp[j + a[i]] = max(dp[j + a[i]], dp[j] + b[i]);
    }
  }
  int ans = 0;
  for (int i = 0; i <= w; i++) {
    ans = max(ans, dp[i]);
  }
  cout << ans;
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long knapsack(long long n, long long *w, long long *v, long long W) {
  long long dp[100][100];
  memset(dp, 0, sizeof(dp));
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= W; j++) {
      if (j - w[i - 1] >= 0) {
        dp[i][j] = max(v[i - 1] + dp[i - 1][j - w[i - 1]], dp[i - 1][j]);
      } else {
        dp[i][j] = dp[i - 1][j];
      }
    }
  }
  // for(long long i=0;i<=n;i++){
  //     for(long long j=0;j<=W;j++){
  //         cout<<dp[i][j]<<" ";
  //     }
  //     cout<<endl;
  // }
  return dp[n][W];
}
int main() {
  long long n, W;
  cin >> n >> W;
  long long w[n];
  long long v[n];
  for (long long i = 0; i < n; i++) {
    cin >> w[i];
    cin >> v[i];
  }
  // for(long long i=0;i<n;i++){
  //     cout<<w[i]<<" ";
  // }
  cout << knapsack(n, w, v, W);

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long knapsack(long long n, long long *w, long long *v, long long W) {
  long long dp[n + 1][W + 1];
  memset(dp, 0, sizeof(dp));
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= W; j++) {
      if (j - w[i - 1] >= 0) {
        dp[i][j] = max(v[i - 1] + dp[i - 1][j - w[i - 1]], dp[i - 1][j]);
      } else {
        dp[i][j] = dp[i - 1][j];
      }
    }
  }
  // for(long long i=0;i<=n;i++){
  //     for(long long j=0;j<=W;j++){
  //         cout<<dp[i][j]<<" ";
  //     }
  //     cout<<endl;
  // }
  return dp[n][W];
}
int main() {
  long long n, W;
  cin >> n >> W;
  long long w[n];
  long long v[n];
  for (long long i = 0; i < n; i++) {
    cin >> w[i];
    cin >> v[i];
  }
  // for(long long i=0;i<n;i++){
  //     cout<<w[i]<<" ";
  // }
  cout << knapsack(n, w, v, W);

  return 0;
}
Problem p03163 (C++)

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

using namespace std;

using ll = long long;
using ld = long double;

const double PI = 3.1415926535897932384626433832795;

const int dx[] = {0, 1, 0, -1};
const int dy[] = {-1, 0, 1, 0};

int gcd(int x, int y) { return y ? gcd(y, x % y) : abs(x); }
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : abs(x); }
int lcm(int x, int y) { return x / gcd(x, y) * y; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }

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

  ll n, W, dp[10001];
  fill_n(dp, 10001, -1);

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

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

  ll ans = *max_element(dp, dp + W + 1);
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"

using namespace std;

using ll = long long;
using ld = long double;

const double PI = 3.1415926535897932384626433832795;

const int dx[] = {0, 1, 0, -1};
const int dy[] = {-1, 0, 1, 0};

int gcd(int x, int y) { return y ? gcd(y, x % y) : abs(x); }
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : abs(x); }
int lcm(int x, int y) { return x / gcd(x, y) * y; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }

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

  ll n, W, dp[100001];
  fill_n(dp, 100001, -1);

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

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

  ll ans = *max_element(dp, dp + W + 1);
  cout << ans << endl;
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (ll i = 0; i < (ll)n; i++)
#define FOR(i, a, b) for (ll i = (a); i < (ll)b; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define INF 1000000000000000
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;
}
using namespace std;
typedef long long ll;
typedef double db;
typedef string str;
typedef pair<ll, ll> p;
const int MOD = 1000000007;

void print(const std::vector<int> &v) {
  std::for_each(v.begin(), v.end(), [](int x) { std::cout << x << " "; });
  std::cout << std::endl;
}

// dp[i][sum] i-1番目の品物をつかって重さがsumを超えないように選んだ時の
// 価値の最大値
int main() {
  int N, W;
  cin >> N >> W;
  vector<int> weight(N), value(N);
  vector<vector<ll>> dp(N + 1, vector<ll>(W, 0));
  REP(i, N) { cin >> weight[i] >> value[i]; }
  REP(i, N) {
    REP(sum_w, W + 1) {
      if (sum_w - weight[i] >= 0) {
        chmax(dp[i + 1][sum_w], dp[i][sum_w - weight[i]] + value[i]);
      }
      chmax(dp[i + 1][sum_w], dp[i][sum_w]);
    }
  }
  cout << dp[N][W] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (ll i = 0; i < (ll)n; i++)
#define FOR(i, a, b) for (ll i = (a); i < (ll)b; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define INF 1000000000000000
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;
}
using namespace std;
typedef long long ll;
typedef double db;
typedef string str;
typedef pair<ll, ll> p;
const int MOD = 1000000007;

void print(const std::vector<int> &v) {
  std::for_each(v.begin(), v.end(), [](int x) { std::cout << x << " "; });
  std::cout << std::endl;
}

// dp[i][sum] i-1番目の品物をつかって重さがsumを超えないように選んだ時の
// 価値の最大値
int main() {
  int N, W;
  cin >> N >> W;
  vector<int> weight(N), value(N);
  vector<vector<ll>> dp(N + 1, vector<ll>(W + 1, 0));
  REP(i, N) { cin >> weight[i] >> value[i]; }
  REP(i, N) {
    REP(sum_w, W + 1) {
      if (sum_w - weight[i] >= 0) {
        chmax(dp[i + 1][sum_w], dp[i][sum_w - weight[i]] + value[i]);
      }
      chmax(dp[i + 1][sum_w], dp[i][sum_w]);
    }
  }
  cout << dp[N][W] << endl;
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 101;
const int maxw = 1e5 + 1;

ll w[maxn], v[maxn], dp[4][9];

int main() {
#ifdef LOCAL
  freopen("data.txt", "r", stdin);
#endif
  ll n, W;
  scanf("%lld%lld", &n, &W);
  for (int i = 1; i <= n; ++i)
    scanf("%lld%lld", &w[i], &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], dp[i - 1][j - w[i]] + v[i]);
  printf("%lld\n", dp[n][W]);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 101;
const int maxw = 1e5 + 1;

ll w[maxn], v[maxn], dp[maxn][maxw];

int main() {
#ifdef LOCAL
  freopen("data.txt", "r", stdin);
#endif
  ll n, W;
  scanf("%lld%lld", &n, &W);
  for (int i = 1; i <= n; ++i)
    scanf("%lld%lld", &w[i], &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], dp[i - 1][j - w[i]] + v[i]);
  printf("%lld\n", dp[n][W]);
}
Problem p03163 (C++)

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

using namespace std;
using ll = long long;

using Field = vector<vector<int>>;
using Graph = vector<vector<int>>;
using VI = vector<int>;
using VC = vector<char>;
using PI = pair<int, int>;

#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(x) x.begin(), x.end()

const long long INF = 1LL << 60;
const int mod = 1000000007;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int N, W;
vector<ll> w(110);
vector<ll> v(110);

vector<vector<ll>> dp(110, vector<ll>(110, 0));

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

  cin >> N >> W;
  REP(i, N) cin >> w.at(i) >> v.at(i);

  dp.at(0).at(0) = 0;

  for (int i = 0; i < N; i++) {
    for (int sum_w = 0; sum_w <= W; sum_w++) {
      if (sum_w < w.at(i)) {
        chmax(dp.at(i + 1).at(sum_w), dp.at(i).at(sum_w));
      } else {
        chmax(dp.at(i + 1).at(sum_w), dp.at(i).at(sum_w));
        chmax(dp.at(i + 1).at(sum_w), dp.at(i).at(sum_w - w.at(i)) + v.at(i));
      }
    }
  }

  cout << dp.at(N).at(W) << endl;

  return 0;
}

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

using namespace std;
using ll = long long;

using Field = vector<vector<int>>;
using Graph = vector<vector<int>>;
using VI = vector<int>;
using VC = vector<char>;
using PI = pair<int, int>;

#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(x) x.begin(), x.end()

const long long INF = 1LL << 60;
const int mod = 1000000007;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int N, W;
vector<ll> w(110);
vector<ll> v(110);

vector<vector<ll>> dp(110, vector<ll>(100100, 0));

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

  cin >> N >> W;
  REP(i, N) cin >> w.at(i) >> v.at(i);

  dp.at(0).at(0) = 0;

  for (int i = 0; i < N; i++) {
    for (int sum_w = 0; sum_w <= W; sum_w++) {
      if (sum_w < w.at(i)) {
        chmax(dp.at(i + 1).at(sum_w), dp.at(i).at(sum_w));
      } else {
        chmax(dp.at(i + 1).at(sum_w), dp.at(i).at(sum_w));
        chmax(dp.at(i + 1).at(sum_w), dp.at(i).at(sum_w - w.at(i)) + v.at(i));
      }
    }
  }

  cout << dp.at(N).at(W) << endl;

  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
struct edge {
  int to;
  ll cap;
  int rev;
};
int main() {
  ll N, W;
  cin >> N >> W;
  vector<ll> w(N), v(N);
  rep(i, N) cin >> w[i] >> v[i];

  vector<vector<ll>> dp(2, vector<ll>(1e8 + 5));

  rep(i, 2) rep(j, 100010) dp[i][j] = -inf;
  int crt = 0, nxt = 1;
  dp[crt][0] = 0;
  rep(i, N) {
    rep(j, 100010) dp[nxt][j] = -inf;
    rep(j, W + 1) {
      if (dp[crt][j] == -inf)
        continue;
      if (j + w[i] <= W)
        dp[nxt][j + w[i]] = max(dp[nxt][j + w[i]], dp[crt][j] + v[i]);
      dp[nxt][j] = max(dp[nxt][j], dp[crt][j]);
    }
    swap(crt, nxt);
  }

  ll res = -inf;
  rep(j, W + 1) res = max(res, dp[crt][j]);
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
struct edge {
  int to;
  ll cap;
  int rev;
};
int main() {
  ll N, W;
  cin >> N >> W;
  vector<ll> w(N), v(N);
  rep(i, N) cin >> w[i] >> v[i];

  vector<vector<ll>> dp(2, vector<ll>(100010));

  rep(i, 2) rep(j, 100010) dp[i][j] = -inf;
  int crt = 0, nxt = 1;
  dp[crt][0] = 0;
  rep(i, N) {
    rep(j, 100010) dp[nxt][j] = -inf;
    rep(j, W + 1) {
      if (dp[crt][j] == -inf)
        continue;
      if (j + w[i] <= W)
        dp[nxt][j + w[i]] = max(dp[nxt][j + w[i]], dp[crt][j] + v[i]);
      dp[nxt][j] = max(dp[nxt][j], dp[crt][j]);
    }
    swap(crt, nxt);
  }

  ll res = -inf;
  rep(j, W + 1) res = max(res, dp[crt][j]);
  cout << res << endl;
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
#define mod 1000000007
#define pb push_back
#define fi first
#define si second
#define mp make_pair
#define f(n) for (ll i = 0; i < n; i++)
#define ff(m) for (ll j = 0; j < m; j++)
#define fr(i, p, n) for (ll i = p; i < n; i++)
#define in insert
ll exp(ll a, ll b, ll m);
#define vl vector<ll>
#define pll pair<ll, ll>

void solve() {

  int n, W;
  cin >> n >> W;

  vl v(n + 1, 0);
  vl w(n + 1, 0);

  fr(i, 1, n + 1) { cin >> w[i] >> v[i]; }
  ll dp[n + 1][n + 1];
  memset(dp, 0, sizeof(dp));

  fr(i, 1, n + 1) {
    fr(j, 1, W + 1) {
      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]]);
    }
  }

  // dp(i,j)=max(dp(i-1,j),v[i]+dp(i-1,j-w[i]))
  cout << dp[n][W] << endl;
}

int main() {

  int t = 1;
  // cin>>t;
  while (t--)
    solve();

  return 0;
}

ll exp(ll a, ll b, ll m) {
  if (b == 0) {
    return 1;
  }
  ll temp = exp(a, b / 2, m);
  temp = (temp * temp) % m;
  if (b & 1) {
    return (temp * (a % m)) % m;
  }
  return temp;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
#define mod 1000000007
#define pb push_back
#define fi first
#define si second
#define mp make_pair
#define f(n) for (ll i = 0; i < n; i++)
#define ff(m) for (ll j = 0; j < m; j++)
#define fr(i, p, n) for (ll i = p; i < n; i++)
#define in insert
ll exp(ll a, ll b, ll m);
#define vl vector<ll>
#define pll pair<ll, ll>

void solve() {

  int n, W;
  cin >> n >> W;

  vl v(n + 1, 0);
  vl w(n + 1, 0);

  fr(i, 1, n + 1) { cin >> w[i] >> v[i]; }
  ll dp[n + 1][W + 1];
  memset(dp, 0, sizeof(dp));

  fr(i, 1, n + 1) {
    fr(j, 1, W + 1) {
      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]]);
    }
  }

  // dp(i,j)=max(dp(i-1,j),v[i]+dp(i-1,j-w[i]))
  cout << dp[n][W] << endl;
}

int main() {

  int t = 1;
  // cin>>t;
  while (t--)
    solve();

  return 0;
}

ll exp(ll a, ll b, ll m) {
  if (b == 0) {
    return 1;
  }
  ll temp = exp(a, b / 2, m);
  temp = (temp * temp) % m;
  if (b & 1) {
    return (temp * (a % m)) % m;
  }
  return temp;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MM 1000000000
#define MOD MM + 7
#define MAX 101000
#define MAP 110
#define initial_value -1
#define Pair pair<int, int>
#define lPair pair<ll, ll>
#define chmax(a, b) (a < b ? a = b : 0)
#define chmin(a, b) (a > b ? a = b : 0)
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};
ll N, W;
ll w[101], v[101];
int main() {
  cin >> N >> W;
  for (ll i = 1; i <= N; i++)
    cin >> w[i] >> v[i];

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MM 1000000000
#define MOD MM + 7
#define MAX 101000
#define MAP 110
#define initial_value -1
#define Pair pair<int, int>
#define lPair pair<ll, ll>
#define chmax(a, b) (a < b ? a = b : 0)
#define chmin(a, b) (a > b ? a = b : 0)
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};
ll N, W;
ll w[101], v[101];
int main() {
  cin >> N >> W;
  for (ll i = 1; i <= N; i++)
    cin >> w[i] >> v[i];

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

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

using namespace std;
#define int long long int
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define vi vector<int>
#define sz(s) (int)s.size()
#define pii pair<int, int>
#define piii pair<int, pair<int, int>>
#define tiii tuple<int, int, int>
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
#define rall(v) v.rbegin(), v.rend()
#define endl '\n'
#define matrix vector<vector<int>>
#define boost                                                                  \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define NAYAN                                                                  \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout);
#define N 500006
#define mod 1000000007
#define mod2 998244353
#define inf 1e18

vector<pii> edges;
set<int> st[N];

int32_t main() {
  boost
#ifndef ONLINE_JUDGE
      NAYAN
#endif

      int n,
      W;

  cin >> n >> W;

  int dp[W + 1];
  memset(dp, 0, sizeof dp);
  int i, j;
  int w[n], v[n];
  for (i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
  }

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

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

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

using namespace std;
#define int long long int
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define vi vector<int>
#define sz(s) (int)s.size()
#define pii pair<int, int>
#define piii pair<int, pair<int, int>>
#define tiii tuple<int, int, int>
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
#define rall(v) v.rbegin(), v.rend()
#define endl '\n'
#define matrix vector<vector<int>>
#define boost                                                                  \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define NAYAN                                                                  \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout);
#define N 500006
#define mod 1000000007
#define mod2 998244353
#define inf 1e18

vector<pii> edges;
set<int> st[N];

int32_t main() {
  boost
      // #ifndef ONLINE_JUDGE
      // NAYAN
      // #endif

      int n,
      W;

  cin >> n >> W;

  int dp[W + 1];
  memset(dp, 0, sizeof dp);
  int i, j;
  int w[n], v[n];
  for (i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
  }

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

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

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

#define int long long int
int n, capc;
int dp[102][10005];

int fill_knapsack(int i, int w, int val[], int wt[]) {
  if (i < 0)
    return 0;
  if (dp[i][w] != -1)
    return dp[i][w];

  if (wt[i] > w) {
    dp[i][w] = fill_knapsack(i - 1, w, val, wt);
  } else {
    dp[i][w] = max(val[i] + fill_knapsack(i - 1, w - wt[i], val, wt),
                   fill_knapsack(i - 1, w, val, wt));
  }
  return dp[i][w];
}

void solve() {
  cin >> n >> capc;
  int val[n];
  int wt[n];

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

  memset(dp, -1, sizeof(dp));
  cout << fill_knapsack(n - 1, capc, val, wt) << endl;
}

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

  solve();
}

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

#define int long long int
int n, capc;
int dp[102][100005];

int fill_knapsack(int i, int w, int val[], int wt[]) {
  if (i < 0)
    return 0;
  if (dp[i][w] != -1)
    return dp[i][w];

  if (wt[i] > w) {
    dp[i][w] = fill_knapsack(i - 1, w, val, wt);
  } else {
    dp[i][w] = max(val[i] + fill_knapsack(i - 1, w - wt[i], val, wt),
                   fill_knapsack(i - 1, w, val, wt));
  }
  return dp[i][w];
}

void solve() {
  cin >> n >> capc;
  int val[n];
  int wt[n];

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

  memset(dp, -1, sizeof(dp));
  cout << fill_knapsack(n - 1, capc, val, wt) << endl;
}

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

  solve();
}
Problem p03163 (C++)

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

typedef long long int ll;

ll weights[105];
ll values[105];

ll knap[205][205];

bool isPrime(ll n) {
  // Corner cases
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;

  // This is checked so that we can skip
  // middle five numbers in below loop
  if (n % 2 == 0 || n % 3 == 0)
    return false;

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

  return true;
}

int main() {
  ios::sync_with_stdio(false);

  ll i, j, k, l, m, n, w;

  cin >> n >> w;

  for (i = 0; i < n; i++) {
    cin >> weights[i] >> values[i];
  }

  for (i = 0; i <= n; i++) {
    for (j = 0; j <= w; j++) {
      if (i == 0 || j == 0)
        knap[i][j] = 0;
      else if (weights[i - 1] <= j) {
        knap[i][j] = max(values[i - 1] + knap[i - 1][j - weights[i - 1]],
                         knap[i - 1][j]);
      } else
        knap[i][j] = knap[i - 1][j];

      // cout << knap[i][j] << " ";
    }
    // cout << '\n';
  }

  cout << knap[n][w] << '\n';
}

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

typedef long long int ll;

ll weights[105];
ll values[105];

ll knap[205][100005];

bool isPrime(ll n) {
  // Corner cases
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;

  // This is checked so that we can skip
  // middle five numbers in below loop
  if (n % 2 == 0 || n % 3 == 0)
    return false;

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

  return true;
}

int main() {
  ios::sync_with_stdio(false);

  ll i, j, k, l, m, n, w;

  cin >> n >> w;

  for (i = 0; i < n; i++) {
    cin >> weights[i] >> values[i];
  }

  for (i = 0; i <= n; i++) {
    for (j = 0; j <= w; j++) {
      if (i == 0 || j == 0)
        knap[i][j] = 0;
      else if (weights[i - 1] <= j) {
        knap[i][j] = max(values[i - 1] + knap[i - 1][j - weights[i - 1]],
                         knap[i - 1][j]);
      } else
        knap[i][j] = knap[i - 1][j];

      // cout << knap[i][j] << " ";
    }
    // cout << '\n';
  }

  cout << knap[n][w] << '\n';
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <cstring>
#include <iostream>
using namespace std;
long long wt[110];
long long val[110];
long long dp[110][110];
long long n, w;
long long knapsack(long long wt[], long long val[], long long n, long long w) {
  if (n == 0 || w == 0)
    return 0;
  if (dp[n][w] != -1)
    return dp[n][w];
  if (wt[n - 1] <= w) {
    return dp[n][w] = max(knapsack(wt, val, n - 1, w),
                          val[n - 1] + knapsack(wt, val, n - 1, w - wt[n - 1]));
  } else {
    return dp[n][w] = knapsack(wt, val, n - 1, w);
  }
}
int main() {
  cin >> n >> w;
  for (long long i = 0; i < n; i++) {
    cin >> wt[i] >> val[i];
  }
  /*for (int i = 0; i < n; i++)
  {
      cin >> val[i];
  }*/
  memset(dp, -1, sizeof(dp));
  cout << knapsack(wt, val, n, w);
  return 0;
}

Fixed submission:
#include <cstring>
#include <iostream>
using namespace std;
long long wt[110];
long long val[110];
long long dp[101][100005];
long long n, w;
long long knapsack(long long wt[], long long val[], long long n, long long w) {
  if (n == 0 || w == 0)
    return 0;
  if (dp[n][w] != -1)
    return dp[n][w];
  if (wt[n - 1] <= w) {
    return dp[n][w] = max(knapsack(wt, val, n - 1, w),
                          val[n - 1] + knapsack(wt, val, n - 1, w - wt[n - 1]));
  } else {
    return dp[n][w] = knapsack(wt, val, n - 1, w);
  }
}
int main() {
  cin >> n >> w;
  for (long long i = 0; i < n; i++) {
    cin >> wt[i] >> val[i];
  }
  /*for (int i = 0; i < n; i++)
  {
      cin >> val[i];
  }*/
  memset(dp, -1, sizeof(dp));
  cout << knapsack(wt, val, n, w);
  return 0;
}
Problem p03163 (C++)

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

#define ll long long
#define pb push_back
#define mp make_pair

bool comp(pair<int, int> a, pair<int, int> b) { return (a.first < b.first); }

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

#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r+", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w+", stdout);
#endif

  int n;
  int w;
  cin >> n >> w;
  vector<pair<int, int>> ele(n);
  for (int i = 0, a, b; i < n; i++) {
    cin >> a >> b;
    ele[i] = mp(a, b);
  }
  sort(ele.begin(), ele.end(), comp);
  // for(int i=0;i<n;i++) {
  // 	cout << ele[i].first << " " << ele[i].second << '\n';
  // }

  ll dp[n + 1][w + 1];
  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++) {
      pair<int, int> obj = ele[i - 1];
      int wt = obj.first;
      int val = obj.second;
      if (j >= wt) {
        dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - wt] + val);
      } else {
        dp[i][j] = dp[i - 1][j];
      }
    }
  }
  // for(int i=0;i<=n;i++) {
  // 	for(int j=0;j<=w;j++) {
  // 		cout << dp[i][j] << " ";
  // 	}
  // 	cout << '\n';
  // }
  cout << dp[n][w] << '\n';
}

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

#define ll long long
#define pb push_back
#define mp make_pair

bool comp(pair<int, int> a, pair<int, int> b) { return (a.first < b.first); }

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

  /*#ifndef ONLINE_JUDGE
// for getting input from input.txt
freopen("input.txt", "r+", stdin);
// for writing output to output.txt
freopen("output.txt", "w+", stdout);
  #endif*/

  int n;
  int w;
  cin >> n >> w;
  vector<pair<int, int>> ele(n);
  for (int i = 0, a, b; i < n; i++) {
    cin >> a >> b;
    ele[i] = mp(a, b);
  }
  sort(ele.begin(), ele.end(), comp);
  // for(int i=0;i<n;i++) {
  // 	cout << ele[i].first << " " << ele[i].second << '\n';
  // }

  ll dp[n + 1][w + 1];
  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++) {
      pair<int, int> obj = ele[i - 1];
      int wt = obj.first;
      int val = obj.second;
      if (j >= wt) {
        dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - wt] + val);
      } else {
        dp[i][j] = dp[i - 1][j];
      }
    }
  }
  // for(int i=0;i<=n;i++) {
  // 	for(int j=0;j<=w;j++) {
  // 		cout << dp[i][j] << " ";
  // 	}
  // 	cout << '\n';
  // }
  cout << dp[n][w] << '\n';
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, a) for (int i = 0; i < (a); i++)
#define ALL(a) (a).begin(), (a).end()
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
const int MOD = 1e9 + 7;

#define MAX_N 100
#define MAX_W 100000

ll dp[MAX_N][MAX_W];

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, a) for (int i = 0; i < (a); i++)
#define ALL(a) (a).begin(), (a).end()
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
const int MOD = 1e9 + 7;

#define MAX_N 100
#define MAX_W 100000

ll dp[MAX_N + 1][MAX_W + 1];

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

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

using namespace std;

using ll = long long;
int N, Weight;
ll w[101], v[101];
ll dp[101][10001];

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


Fixed submission:
#include <iostream>

using namespace std;

using ll = long long;
int N, Weight;
ll w[101], v[101];
ll dp[101][100001];

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

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

using namespace std;
typedef long long ll;
int n, t, wt[100 + 9];
ll ps[100 + 9];
ll dp[100 + 9][100 + 9];

int main() {
  scanf("%d%d", &n, &t);
  for (int i = 0; i < n; i++)
    scanf("%d%lld", &wt[i], &ps[i]);
  for (int i = wt[n - 1]; i <= t; i++)
    dp[n - 1][i] = ps[n - 1];

  for (int i = n - 2; i >= 0; i--) {
    for (int j = t; j >= 0; j--) {
      dp[i][j] = dp[i + 1][j];
      if (j > wt[i])
        dp[i][j] = max(dp[i][j], dp[i + 1][j - wt[i]] + ps[i]);
    }
  }

  cout << dp[0][t] << endl;

  return 0;
}


Fixed submission:
#include <iostream>

using namespace std;
typedef long long ll;
int n, t, wt[100 + 9];
ll ps[100 + 9];
ll dp[100 + 9][100000 + 9];

int main() {
  scanf("%d%d", &n, &t);
  for (int i = 0; i < n; i++)
    scanf("%d%lld", &wt[i], &ps[i]);
  for (int i = wt[n - 1]; i <= t; i++)
    dp[n - 1][i] = ps[n - 1];

  for (int i = n - 2; i >= 0; i--) {
    for (int j = t; j >= 0; j--) {
      dp[i][j] = dp[i + 1][j];
      if (j > wt[i])
        dp[i][j] = max(dp[i][j], dp[i + 1][j - wt[i]] + ps[i]);
    }
  }

  cout << dp[0][t] << endl;

  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int MAX_N = 105;
const int MAX_W = 10005;
ll w[MAX_N], v[MAX_N];
ll dp[MAX_N][MAX_W];

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

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

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

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int MAX_N = 105;
const int MAX_W = 100005;
ll w[MAX_N], v[MAX_N];
ll dp[MAX_N][MAX_W];

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

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

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

Buggy submission (status: Runtime Error):
// I SELL YOU...!
#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll dp[100][110000] = {};
signed main() {
  ll n, W, ans = 0;
  cin >> n >> W;
  ll 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++) {
      dp[i + 1][j] = dp[i][j];
      if (j - w[i] >= 0) {
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j - w[i]] + v[i]);
        ans = max(ans, dp[i + 1][j]);
      }
    }
  }
  cout << ans << endl;
}


Fixed submission:
// I SELL YOU...!
#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll dp[110][110000] = {};
signed main() {
  ll n, W, ans = 0;
  cin >> n >> W;
  ll 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++) {
      dp[i + 1][j] = dp[i][j];
      if (j - w[i] >= 0) {
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j - w[i]] + v[i]);
        ans = max(ans, dp[i + 1][j]);
      }
    }
  }
  cout << ans << endl;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, j, n) for (int i = (int)(j); i < (int)(n); i++)
#define REP(i, j, n) for (int i = (int)(j); i <= (int)(n); i++)
#define MOD 1000000007
#define int long long
#define ALL(a) (a).begin(), (a).end()
#define vi vector<int>
#define vii vector<vi>
#define pii pair<int, int>
#define priq priority_queue<int>
#define disup(A, key) distance(A.begin(), upper_bound(ALL(A), (int)(key)))
#define dislow(A, key) distance(A.begin(), lower_bound(ALL(A), (int)(key)))
#define tii tuple<int, int, int>
signed main() {
  int N, W;
  cin >> N >> W;
  vii dp(N + 1, vi(W + 1));
  REP(i, 1, N) {
    int w, v;
    cin >> w >> v;
    rep(j, 0, w) dp[i][j] = dp[i - 1][j];
    REP(j, w, v) { dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w] + v); }
  }
  cout << dp[N][W] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, j, n) for (int i = (int)(j); i < (int)(n); i++)
#define REP(i, j, n) for (int i = (int)(j); i <= (int)(n); i++)
#define MOD 1000000007
#define int long long
#define ALL(a) (a).begin(), (a).end()
#define vi vector<int>
#define vii vector<vi>
#define pii pair<int, int>
#define priq priority_queue<int>
#define disup(A, key) distance(A.begin(), upper_bound(ALL(A), (int)(key)))
#define dislow(A, key) distance(A.begin(), lower_bound(ALL(A), (int)(key)))
#define tii tuple<int, int, int>
signed main() {
  int N, W;
  cin >> N >> W;
  vii dp(N + 1, vi(W + 1));
  REP(i, 1, N) {
    int w, v;
    cin >> w >> v;
    rep(j, 0, w) dp[i][j] = dp[i - 1][j];
    REP(j, w, W) { dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w] + v); }
  }
  cout << dp[N][W] << endl;
}
Problem p03163 (C++)

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

#define itf ios_base::sync_with_stdio(false)
#define ll long long int
#define ct cin.tie(NULL)

ll Z[102][10007];

ll step(ll a[], ll v[], ll w, ll n) {
  if (n == 0)
    return 0;

  if (Z[n][w] != -1)
    return Z[n][w];

  if (w >= a[n - 1])
    return (Z[n][w] = max(v[n - 1] + step(a, v, w - a[n - 1], n - 1),
                          step(a, v, w, n - 1)));
  else
    return Z[n][w] = step(a, v, w, n - 1);
}

int main() {

  memset(Z, -1, sizeof(Z));
  ll n, w;
  cin >> n >> w;
  ll a[n], v[n];

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

  cout << step(a, v, w, n) << " ";

  return 0;
}


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

#define itf ios_base::sync_with_stdio(false)
#define ll long long int
#define ct cin.tie(NULL)

ll Z[102][1000007];

ll step(ll a[], ll v[], ll w, ll n) {
  if (n == 0)
    return 0;

  if (Z[n][w] != -1)
    return Z[n][w];

  if (w >= a[n - 1])
    return (Z[n][w] = max(v[n - 1] + step(a, v, w - a[n - 1], n - 1),
                          step(a, v, w, n - 1)));
  else
    return Z[n][w] = step(a, v, w, n - 1);
}

int main() {

  memset(Z, -1, sizeof(Z));
  ll n, w;
  cin >> n >> w;
  ll a[n], v[n];

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

  cout << step(a, v, w, n) << " ";

  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define MAXN 20000
using namespace std;
long long n, W, w[MAXN], v[MAXN], dp[MAXN];
int main() {
  scanf("%lld%lld", &n, &W);
  for (int i = 0; i < n; i++) {
    scanf("%lld%lld", 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]] + v[i]);
    }
  }
  printf("%lld\n", dp[W]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define MAXN 200000
using namespace std;
long long n, W, w[MAXN], v[MAXN], dp[MAXN];
int main() {
  scanf("%lld%lld", &n, &W);
  for (int i = 0; i < n; i++) {
    scanf("%lld%lld", 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]] + v[i]);
    }
  }
  printf("%lld\n", dp[W]);
  return 0;
}