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

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

int N, W, weight[104];
long long value[104];
long long dp[104][10005];

long long solve(int i, int w) {
  if (i >= N) {
    return 0;
  }

  if (dp[i][w] != -1) {
    return dp[i][w];
  }

  long long X = solve(i + 1, w);
  int nw = w - weight[i];
  if (nw >= 0) {
    X = max(X, value[i] + solve(i + 1, nw));
  }

  return dp[i][w] = X;
}

int main() {
  cin >> N >> W;
  for (int i = 0; i < N; i++) {
    cin >> weight[i] >> value[i];
  }

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < 100005; j++) {
      dp[i][j] = -1;
    }
  }

  for (int i = 0; i <= W; i++) {
    if (i >= weight[N - 1]) {
      dp[N - 1][i] = value[N - 1];
    } else {
      dp[N - 1][i] = 0;
    }
  }

  cout << solve(0, W);

  return 0;
}

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

int N, W, weight[104];
long long value[104];
long long dp[104][100005];

long long solve(int i, int w) {
  if (i >= N) {
    return 0;
  }

  if (dp[i][w] != -1) {
    return dp[i][w];
  }

  long long X = solve(i + 1, w);
  int nw = w - weight[i];
  if (nw >= 0) {
    X = max(X, value[i] + solve(i + 1, nw));
  }

  return dp[i][w] = X;
}

int main() {
  cin >> N >> W;
  for (int i = 0; i < N; i++) {
    cin >> weight[i] >> value[i];
  }

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < 100005; j++) {
      dp[i][j] = -1;
    }
  }

  for (int i = 0; i <= W; i++) {
    if (i >= weight[N - 1]) {
      dp[N - 1][i] = value[N - 1];
    } else {
      dp[N - 1][i] = 0;
    }
  }

  cout << solve(0, W);

  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define ld long double
#define ff first
#define ss second
#define P pair<int, int>
#define pb push_back
#define mod 1000000007
#define inf 1e18
#define endl '\n'
const int N = 105; // change it when needed
const int W = 1e5;
int a[N], b[N];
int dp[N];
int n, w;
int go() {
  for (int i = 0; i < n; i++) {
    for (int j = w; j >= 0; j--) {
      if (j - a[i] >= 0) {
        dp[j] = max(dp[j], b[i] + dp[j - a[i]]);
      }
    }
  }
  return dp[w];
}
void solve() {
  cin >> n >> w;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
    dp[i] = 0;
  }
  cout << go();
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  // int t; cin >> t; while (t--)
  solve();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define ld long double
#define ff first
#define ss second
#define P pair<int, int>
#define pb push_back
#define mod 1000000007
#define inf 1e18
#define endl '\n'
const int N = 105; // change it when needed
const int W = 1e5;
int a[N], b[N];
int dp[W];
int n, w;
int go() {
  for (int i = 0; i < n; i++) {
    for (int j = w; j >= 0; j--) {
      if (j - a[i] >= 0) {
        dp[j] = max(dp[j], b[i] + dp[j - a[i]]);
      }
    }
  }
  return dp[w];
}
void solve() {
  cin >> n >> w;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
    dp[i] = 0;
  }
  cout << go();
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  // int t; cin >> t; while (t--)
  solve();
  return 0;
}
Problem p03163 (C++)

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

Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int v[105], w[100005];
long long dp[100005] = {0};
long long knapsack(int n, int W) {
  int i, j;
  for (i = 0; i < n; i++) {
    for (j = W; j >= w[i]; j--)
      dp[j] = max(dp[j], dp[j - w[i]] + v[i]);
  }
  return dp[W];
}
int main() {
  int i, n, W;
  cin >> n >> W;
  for (i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
  }
  cout << knapsack(n, W) << endl;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
// author:  Harsh Nema
#include <bits/stdc++.h>
#define ll long long int
#define F first
#define S second
const ll mod = 1e9 + 7;
const ll INF = 10000000000000;
#define pb push_back
#define deb(x) cout << '>' << #x << ':' << x << endl;
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);
using namespace std;
int main() {
  fastio;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll n = 0, w = 0;
  cin >> n >> w;
  vector<ll> wt(n), val(n);
  for (ll i = 0; i < n; ++i) {
    cin >> wt[i] >> val[i];
  }
  ll dp[n + 1][w + 1];
  for (ll i = 0; i <= n; ++i) {
    for (ll j = 0; j <= w; ++j) {
      if (i == 0 || j == 0)
        dp[i][j] = 0;
      else 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];
  return 0;
}

Fixed submission:
// author:  Harsh Nema
#include <bits/stdc++.h>
#define ll long long int
#define F first
#define S second
const ll mod = 1e9 + 7;
const ll INF = 10000000000000;
#define pb push_back
#define deb(x) cout << '>' << #x << ':' << x << endl;
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);
using namespace std;
int main() {
  fastio;
  /*#ifndef ONLINE_JUDGE
      freopen("input.txt","r",stdin);
      freopen("output.txt","w",stdout);
  #endif*/
  ll n = 0, w = 0;
  cin >> n >> w;
  vector<ll> wt(n), val(n);
  for (ll i = 0; i < n; ++i) {
    cin >> wt[i] >> val[i];
  }
  ll dp[n + 1][w + 1];
  for (ll i = 0; i <= n; ++i) {
    for (ll j = 0; j <= w; ++j) {
      if (i == 0 || j == 0)
        dp[i][j] = 0;
      else 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];
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll n, we;
ll w[101], v[101];
ll memo[101][100001];
ll findans(ll i, ll n, ll sum) {
  if (i == n) {
    if (sum <= we) {
      return 0;
    } else {
      return -1000000000000000;
    }
  }
  /*if(i==n-1)
  {

      if(sum+w[i]<=we)
      {
          return v[i];
      }
      else
      {
          return 0;
      }
  }*/
  if (memo[i][sum] != -1) {
    return memo[i][sum];
  }
  ll ans1 = v[i] + findans(i + 1, n, sum + w[i]);
  ll ans2 = findans(i + 1, n, sum);
  return memo[i][sum] = max(ans1, ans2);
}
int main() {
  // Your code here!

  cin >> n >> we;
  for (ll i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
  }
  for (ll i = 0; i <= 100; i++) {
    for (ll j = 0; j <= 100000; j++) {
      memo[i][j] = -1;
    }
  }
  cout << findans(0, n, 0);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll n, we;
ll w[101], v[101];
ll memo[101][100001];
ll findans(ll i, ll n, ll sum) {
  if (i == n) {
    if (sum <= we) {
      return 0;
    } else {
      return -1000000000000000;
    }
  }
  /*if(i==n-1)
  {

      if(sum+w[i]<=we)
      {
          return v[i];
      }
      else
      {
          return 0;
      }
  }*/
  if (memo[i][sum] != -1) {
    return memo[i][sum];
  }
  ll ans1 = -1000000000000000;
  if (sum + w[i] <= we) {
    ans1 = v[i] + findans(i + 1, n, sum + w[i]);
  }
  ll ans2 = findans(i + 1, n, sum);
  return memo[i][sum] = max(ans1, ans2);
}
int main() {
  // Your code here!

  cin >> n >> we;
  for (ll i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
  }
  for (ll i = 0; i <= 100; i++) {
    for (ll j = 0; j <= 100000; j++) {
      memo[i][j] = -1;
    }
  }
  cout << findans(0, n, 0);
}
Problem p03163 (C++)

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

const int mxN = 100;
int wt[mxN], v[mxN], n, W;
long long dp[mxN + 1][mxN + 1];
array<int, 3> s[mxN];

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

  for (int i = 0; i < n + 1; i++) {
    for (int w = 0; w <= W; ++w) {
      if (i == 0 || w == 0) {
        dp[i][w] = 0;
      } else if (wt[i - 1] <= w) {
        dp[i][w] = max(dp[i - 1][w], v[i - 1] + dp[i - 1][w - wt[i - 1]]);
      } else {
        dp[i][w] = dp[i - 1][w];
      }
    }
  }
  cout << dp[n][W];
}

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

const int mxN = 100, mxW = 1e5;
long long wt[mxN], v[mxN], n, W;
long long dp[mxN + 1][mxW + 1];

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

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define rep(i, b) FOR(i, 0, b)
#define INF mugen
#define dump(x) cerr << #x << "=" << x << endl
#define all(a) (a).begin(), (a).end()
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<vii> viii;
typedef pair<int, int> P;
template <class T> void chmin(T &a, T const &b) {
  if (b < a)
    a = b;
}
template <typename T> string to_string(const T &n) {
  ostringstream stm;
  stm << n;
  return stm.str();
}
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
using ll = long long;
const ll mod = LLONG_MAX;

int N, W;
ll ans;
ll dp[212345];
ll w[110], v[110];
signed main() {
  cin >> N >> W;
  rep(i, N) cin >> w[i] >> v[i];

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

  for (int i = 0; i <= W; i++) {
    ans = max(ans, dp[i]);
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define rep(i, b) FOR(i, 0, b)
#define INF mugen
#define dump(x) cerr << #x << "=" << x << endl
#define all(a) (a).begin(), (a).end()
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<vii> viii;
typedef pair<int, int> P;
template <class T> void chmin(T &a, T const &b) {
  if (b < a)
    a = b;
}
template <typename T> string to_string(const T &n) {
  ostringstream stm;
  stm << n;
  return stm.str();
}
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
using ll = long long;
const ll mod = LLONG_MAX;

int N, W;
ll ans;
ll dp[212345];
ll w[110], v[110];
signed main() {
  cin >> N >> W;
  rep(i, N) cin >> w[i] >> v[i];

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

  for (int i = 0; i <= W; i++) {
    ans = max(ans, dp[i]);
  }
  cout << ans << endl;
}
Problem p03163 (C++)

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

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

// const ll INF = 1LL << 60;

int N, W;
vector<ll> weight, value;

int main() {
  cin >> N >> W;
  weight.resize(N);
  value.resize(N);
  for (int i = 0; i < N; i++) {
    cin >> weight[i] >> value[i];
  }

  // dp[i][w]:
  // i番目(0-index)までの品物の中から、重さの総和がw以下である時の価値の総和の最大値
  vector<vector<ll>> dp(N, vector<ll>(W, 0));
  for (int w = 0; w <= W; w++) {
    if (w >= weight[0]) {
      dp[0][w] = value[0];
    }
  }

  for (int i = 1; i < N; i++) {
    for (int w = 0; w <= W; w++) {
      if (w >= weight[i]) {
        chmax(dp[i][w], dp[i - 1][w - weight[i]] + value[i]);
      }
      chmax(dp[i][w], dp[i - 1][w]);
    }
  }

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

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

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

// const ll INF = 1LL << 60;

int N, W;
vector<ll> weight, value;

int main() {
  cin >> N >> W;
  weight.resize(N);
  value.resize(N);
  for (int i = 0; i < N; i++) {
    cin >> weight[i] >> value[i];
  }

  // dp[i][w]:
  // i番目(0-index)までの品物の中から、重さの総和がw以下である時の価値の総和の最大値
  vector<vector<ll>> dp(N, vector<ll>(W + 1, 0));

  for (int w = 0; w <= W; w++) {
    if (w >= weight[0]) {
      dp[0][w] = value[0];
    }
  }

  for (int i = 1; i < N; i++) {
    for (int w = 0; w <= W; w++) {
      if (w >= weight[i]) {
        chmax(dp[i][w], dp[i - 1][w - weight[i]] + value[i]);
      }
      chmax(dp[i][w], dp[i - 1][w]);
    }
  }

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

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

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

  if (wt[n - 1] <= W)
    return dp[n][W] =
               max(val[n - 1] + knapsack(val, wt, W - wt[n - 1], n - 1, dp),
                   knapsack(val, wt, W, n - 1, dp));

  else
    return dp[n][W] = knapsack(val, wt, W, n - 1, dp);
}

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

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

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

  if (wt[n - 1] <= W)
    return dp[n][W] =
               max(val[n - 1] + knapsack(val, wt, W - wt[n - 1], n - 1, dp),
                   knapsack(val, wt, W, n - 1, dp));

  else
    return dp[n][W] = knapsack(val, wt, W, n - 1, dp);
}

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

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

using namespace std;

#define MOD 1000000007
#define N 102
#define M 100005
#define ll long long
#define ld long double
#define pb push_back
#define ff first
#define ss second

ll dp[N][M];
ll v[N], w[N];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll n, W;
  cin >> n >> W;
  ll i, j, ans;
  for (i = 1; i <= n; i++) {
    cin >> w[i] >> v[i];
  }
  memset(dp, 0, sizeof dp);
  for (j = 0; j <= W; j++) {
    for (i = 1; i <= n; i++) {
      dp[i][j] = max(dp[i][j], dp[i - 1][j]);
      if (j >= w[i]) {
        dp[i][j] = max(dp[i][j], dp[i - 1][j - w[i]] + v[i]);
      }
    }
  }
  ans = 0;
  for (j = 0; j <= W; j++) {
    ans = max(ans, dp[n][j]);
  }
  cout << ans;
  return 0;
}

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

using namespace std;

#define MOD 1000000007
#define N 102
#define M 100005
#define ll long long
#define ld long double
#define pb push_back
#define ff first
#define ss second

ll dp[N][M];
ll v[N], w[N];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll n, W;
  cin >> n >> W;
  ll i, j, ans;
  for (i = 1; i <= n; i++) {
    cin >> w[i] >> v[i];
  }
  memset(dp, 0, sizeof dp);
  for (j = 0; j <= W; j++) {
    for (i = 1; i <= n; i++) {
      dp[i][j] = max(dp[i][j], dp[i - 1][j]);
      if (j >= w[i]) {
        dp[i][j] = max(dp[i][j], dp[i - 1][j - w[i]] + v[i]);
      }
    }
  }
  ans = 0;
  for (j = 0; j <= W; j++) {
    ans = max(ans, dp[n][j]);
  }
  cout << ans;
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
/**

    WARNING
          This Code Sucks
                        Just Like My Life :(

**/
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
template <class T>
using orderd_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
int n, w, weight[101], v[101];
long long dp[101][N];
int main() {
  scanf("%d%d", &n, &w);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &weight[i], &v[i]);
  }
  for (int i = 1; i <= n + 1; i++) {
    for (int j = 1; j <= w; j++) {
      dp[i][j] = dp[i - 1][j];
      if (weight[i - 1] <= j)
        dp[i][j] = max(dp[i - 1][j - weight[i - 1]] + v[i - 1], dp[i - 1][j]);
    }
  }
  printf("%lld", dp[n + 1][w]);
  return 0;
}


Fixed submission:
/**

    WARNING
          This Code Sucks
                        Just Like My Life :(

**/
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
template <class T>
using orderd_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
int n, w, weight[102], v[102];
long long dp[102][N];
int main() {
  scanf("%d%d", &n, &w);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &weight[i], &v[i]);
  }
  for (int i = 1; i <= n + 1; i++) {
    for (int j = 1; j <= w; j++) {
      dp[i][j] = dp[i - 1][j];
      if (weight[i - 1] <= j)
        dp[i][j] = max(dp[i - 1][j - weight[i - 1]] + v[i - 1], dp[i - 1][j]);
    }
  }
  printf("%lld", dp[n + 1][w]);
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, w;
  cin >> n >> w;
  long long int a[n + 1][2];
  for (int i = 1; i <= n; i++)
    cin >> a[i][0] >> a[i][1];
  vector<long long int> dp;
  for (int i = 1; i <= n; i++) {
    for (int j = w - a[i][0]; j > 0; j -= 1) {
      dp[j + a[i][0]] = max(dp[j + a[i][0]], a[i][1] + dp[j]);
    }
  }
  cout << dp[w];
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, w;
  cin >> n >> w;
  long long int wi, va;
  vector<long long int> dp(w + 1);
  for (long long int i = 1; i <= n; i++) {
    cin >> wi >> va;
    for (long long int j = w - wi; j >= 0; j -= 1) {
      dp[j + wi] = max(dp[j + wi], va + dp[j]);
    }
  }
  cout << dp[w];
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
const int N = 105;
long long dp[N][N];
int main() {
  long long n, W, v[N], w[N];
  cin >> n >> W;
  for (int i = 1; i <= n; i++) {
    cin >> 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], v[i] + dp[i - 1][j - w[i]]);
      }
    }
  }
  cout << dp[n][W] << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
const int N = 105;
long long dp[N][100005];
int main() {
  long long n, W, v[N], w[N];
  cin >> n >> W;
  for (int i = 1; i <= n; i++) {
    cin >> 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], v[i] + dp[i - 1][j - w[i]]);
      }
    }
  }
  cout << dp[n][W] << endl;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define PI 3.14159265358979
typedef long long int ll;

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

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define PI 3.14159265358979
typedef long long int ll;

int main() {
  long long int dp[110][100010] = {};
  int N, W;
  cin >> N >> W;
  vector<int> w(N), v(N);
  for (int i = 0; i < N; i++)
    cin >> w[i] >> v[i];
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= W; j++) {
      dp[i][j] = dp[i - 1][j];
      if (j - w[i - 1] >= 0)
        dp[i][j] = max(dp[i][j], dp[i - 1][j - w[i - 1]] + v[i - 1]);
    }
  }
  cout << dp[N][W];
}
Problem p03163 (C++)

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

#define ll long long
#define mod 1000000007

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

  for (ll i = 0; i <= n; i++)
    for (ll j = 0; j <= W; j++) {

      if (i == 0 || j == 0)
        dp[i][j] = 0;
      else if (w[i - 1] <= j)
        dp[i][j] = max(dp[i - 1][j], v[i - 1] + dp[i - 1][j - w[i - 1]]);
      else
        dp[i][j] = dp[i - 1][j];
    }

  cout << dp[n][W];
  return 0;
}


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

#define ll long long
#define mod 1000000007

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

  for (ll i = 0; i <= n; i++)
    for (ll j = 0; j <= W; j++) {

      if (i == 0 || j == 0)
        dp[i][j] = 0;
      else if (w[i - 1] <= j)
        dp[i][j] = max(dp[i - 1][j], v[i - 1] + dp[i - 1][j - w[i - 1]]);
      else
        dp[i][j] = dp[i - 1][j];
    }

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

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

typedef long long int ll;
typedef long double ld;
typedef vector<ll> vec;

#define vecc vector<vector<ll>>
#define LM LLONG_MAX

#define int long long int
#define P pair<ll, ll>

#define ff first
#define ss second

#define FAST                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"

#define N 200005
#define MOD (ll)1000000007

ll dp[105][N], a[105][3];

ll solve(ll n, ll w) {
  if (n < 1 && w < 1)
    return 0;

  ll &ans = dp[n][w];

  if (ans != 0)
    return ans;

  ans = solve(n - 1, w);

  if (w >= a[n][1])
    ans = max(ans, a[n][2] + solve(n - 1, w - a[n][1]));

  return ans;
}

signed main() {
  FAST;

  ll n, w;
  cin >> n >> w;

  memset(dp, 0, sizeof(dp));

  for (int i = 1; i <= n; i++)
    cin >> a[i][1] >> a[i][2];

  cout << solve(n, w) << "\n";
}

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

typedef long long int ll;
typedef long double ld;
typedef vector<ll> vec;

#define vecc vector<vector<ll>>
#define LM LLONG_MAX

#define int long long int
#define P pair<ll, ll>

#define ff first
#define ss second

#define FAST                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"

#define N 200005
#define MOD (ll)1000000007

ll dp[105][N], a[105][3];

ll solve(ll n, ll w) {
  if (n < 1 || w < 1)
    return 0;

  ll &ans = dp[n][w];

  if (ans != 0)
    return ans;

  ans = solve(n - 1, w);

  if (w >= a[n][1])
    ans = max(ans, a[n][2] + solve(n - 1, w - a[n][1]));

  return ans;
}

signed main() {
  FAST;

  ll n, w;
  cin >> n >> w;

  memset(dp, 0, sizeof(dp));

  for (int i = 1; i <= n; i++)
    cin >> a[i][1] >> a[i][2];

  cout << solve(n, w) << "\n";
}
Problem p03163 (C++)

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

typedef long long ll;
typedef unsigned long long int ull;
#define endl "\n"
#define pb push_back
#define sq(a) (a) * (a)
#define debug(x) cerr << #x << '=' << (x) << endl;
#define debugv(v)                                                              \
  cerr << #v << " : ";                                                         \
  for (auto x : v)                                                             \
    cerr << x << ' ';                                                          \
  cerr << endl;
#define MOD 1000000007
#define PI 3.141592653589793238
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll exp(ll x, ll n, ll mod) {
  ll result = 1;
  while (n > 0) {
    if (n & 1 == 1)
      result = (result * x) % mod;
    x = (x * x) % mod;
    n = n >> 1;
  }
  return result;
}

int32_t main() {
  IOS

      ll n,
      w;
  cin >> n >> w;

  ll v[n], wt[n];

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

  ll dp[w + 1][n + 1];

  for (ll i = 0; i <= w; i++) {
    for (ll j = 0; j <= n; j++) {
      if (i == 0 || j == 0)
        dp[i][j] = 0;

      else if (wt[j - 1] > i)
        dp[i][j] = dp[i][j - 1];

      else
        dp[i][j] = max(v[i - 1] + dp[i - wt[j - 1]][j - 1], dp[i][j - 1]);
    }
  }

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

  return 0;
}


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

typedef long long ll;
typedef unsigned long long int ull;
#define endl "\n"
#define pb push_back
#define sq(a) (a) * (a)
#define debug(x) cerr << #x << '=' << (x) << endl;
#define debugv(v)                                                              \
  cerr << #v << " : ";                                                         \
  for (auto x : v)                                                             \
    cerr << x << ' ';                                                          \
  cerr << endl;
#define MOD 1000000007
#define PI 3.141592653589793238
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll exp(ll x, ll n, ll mod) {
  ll result = 1;
  while (n > 0) {
    if (n & 1 == 1)
      result = (result * x) % mod;
    x = (x * x) % mod;
    n = n >> 1;
  }
  return result;
}

int32_t main() {
  IOS

      ll n,
      w;
  cin >> n >> w;

  ll v[n], wt[n];

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

  ll dp[w + 1][n + 1];

  for (ll i = 0; i <= w; i++) {
    for (ll j = 0; j <= n; j++) {
      if (i == 0 || j == 0)
        dp[i][j] = 0;

      else if (wt[j - 1] > i)
        dp[i][j] = dp[i][j - 1];

      else
        dp[i][j] = max(v[j - 1] + dp[i - wt[j - 1]][j - 1], dp[i][j - 1]);
    }
  }

  cout << dp[w][n] << 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++)
#define PI 3.14159265359
#define INF 1000100100
#define all(x) (x).begin(), (x).end()
typedef long long ll;
using namespace std;

int main() {
  int n, w;
  cin >> n >> w;
  int wv[n][2];
  rep(i, n) cin >> wv[i][0] >> wv[i][1];
  ll dp[w + 1];
  rep(i, w + 1) dp[i] = 0;

  rep(i, n + 1) {
    for (int j = w; j >= wv[i][0]; j--) {
      dp[j] = max(dp[j - wv[i][0]] + wv[i][1], dp[j]);
    }
  }

  cout << dp[w] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define PI 3.14159265359
#define INF 1000100100
#define all(x) (x).begin(), (x).end()
typedef long long ll;
using namespace std;

int main() {
  int n, w;
  cin >> n >> w;
  ll wv[n][2];
  rep(i, n) cin >> wv[i][0] >> wv[i][1];
  ll dp[w + 1];
  rep(i, w + 1) dp[i] = 0;

  rep(i, n + 1) {
    for (int j = w; j >= wv[i][0]; j--) {
      dp[j] = max(dp[j - wv[i][0]] + wv[i][1], dp[j]);
    }
  }

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

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

#define clr(i, j) memset(i, j, sizeof(i))
#define pb push_back
#define PI acos(-1)
#define MOD ((int)1e9 + 7)
typedef long long ll;
const int mx = 1e9 + 5;
ll dp[100][100009];
// dp[i][w] aktr value ttgab b awl i elements w el weight bt3ha w
int main() {
  ll n, w;
  cin >> n >> w;
  ll ww[n + 7];
  ll v[n + 7];
  for (ll i = 1; i <= n; i++) {
    cin >> ww[i] >> v[i];
    dp[i][ww[i]] = v[i];
  }
  /*
  3 8
3 30
4 50
5 60*/

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

    for (ll j = 0; j <= w; j++) {
      if (j != 0) {
        dp[0][j] = -1000000;
      }
      dp[i][j] = -1000000;
      if (j - ww[i] >= 0) {
        dp[i][j] = max(dp[i - 1][j - ww[i]] + v[i], dp[i - 1][j]);
      }
      dp[i][j] = max(dp[i][j], dp[i - 1][j]);
    }
  }
  ll ans = 0;
  for (ll j = 0; j <= w; j++) {
    ans = max(ans, dp[n][j]);
  }
  cout << ans;
}

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

#define clr(i, j) memset(i, j, sizeof(i))
#define pb push_back
#define PI acos(-1)
#define MOD ((int)1e9 + 7)
typedef long long ll;
const int mx = 1e9 + 5;
ll dp[107][100009];
// dp[i][w] aktr value ttgab b awl i elements w el weight bt3ha w
int main() {
  ll n, w;
  cin >> n >> w;
  ll ww[n + 7];
  ll v[n + 7];
  for (ll i = 1; i <= n; i++) {
    cin >> ww[i] >> v[i];
    dp[i][ww[i]] = v[i];
  }
  /*
  3 8
3 30
4 50
5 60*/

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

    for (ll j = 0; j <= w; j++) {
      if (j != 0) {
        dp[0][j] = -1000000;
      }
      dp[i][j] = -1000000;
      if (j - ww[i] >= 0) {
        dp[i][j] = max(dp[i - 1][j - ww[i]] + v[i], dp[i - 1][j]);
      }
      dp[i][j] = max(dp[i][j], dp[i - 1][j]);
    }
  }
  ll ans = 0;
  for (ll j = 0; j <= w; j++) {
    ans = max(ans, dp[n][j]);
  }
  cout << ans;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll N, W, w[1000], v[1000], dp[100][100005];
ll fun(ll i, ll tw) {
  if (tw > W || i > N + 1)
    return -1000000000000000000;
  if (i == N + 1)
    return 0;
  if (dp[i][tw] != -1)
    return dp[i][tw];
  else
    dp[i][tw] = max(fun(i + 1, tw), v[i] + fun(i + 1, tw + w[i]));
  return dp[i][tw];
}
int main() {
  ll i, ans;
  cin >> N >> W;
  for (i = 1; i <= N; i++)
    cin >> w[i] >> v[i];
  memset(dp, -1, sizeof dp);
  ans = fun(1, 0);
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll N, W, w[1000], v[1000], dp[105][100005];
ll fun(ll i, ll tw) {
  if (tw > W || i > N + 1)
    return -1000000000000000000;
  if (i == N + 1)
    return 0;
  if (dp[i][tw] != -1)
    return dp[i][tw];
  else
    dp[i][tw] = max(fun(i + 1, tw), v[i] + fun(i + 1, tw + w[i]));
  return dp[i][tw];
}
int main() {
  ll i, ans;
  cin >> N >> W;
  for (i = 1; i <= N; i++)
    cin >> w[i] >> v[i];
  memset(dp, -1, sizeof dp);
  ans = fun(1, 0);
  cout << ans;
  return 0;
}
Problem p03163 (C++)

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

long long dp[100][100000];
int main() {
  int n, w;
  scanf("%d%d", &n, &w);
  for (int i = 1, wi, vi; i <= n; ++i) {
    scanf("%d%d", &wi, &vi);
    for (int j = 1; j <= w; ++j)
      if (wi <= j)
        dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - wi] + vi);
      else
        dp[i][j] = dp[i - 1][j];
  }
  printf("%lld\n", dp[n][w]);
}

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

long long dp[101][100001];
int main() {
  int n, w;
  scanf("%d%d", &n, &w);
  for (int i = 1, wi, vi; i <= n; ++i) {
    scanf("%d%d", &wi, &vi);
    for (int j = 1; j <= w; ++j)
      if (wi <= j)
        dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - wi] + vi);
      else
        dp[i][j] = dp[i - 1][j];
  }
  printf("%lld\n", dp[n][w]);
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int number, w;
long long dp[100100];
int main() {
  cin >> number >> w;
  for (int i = 1; i <= number; i++) {
    int weight, value;
    cin >> weight >> value;
    for (int j = w - weight; j >= 0; j--)
      dp[j + w] = max(dp[j + w], dp[j] + value);
  }
  cout << dp[w] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int number, w;
long long dp[100100];
int main() {
  cin >> number >> w;
  for (int i = 1; i <= number; i++) {
    int weight, value;
    cin >> weight >> value;
    for (int j = w - weight; j >= 0; j--)
      dp[j + weight] = max(dp[j + weight], dp[j] + value);
  }
  cout << dp[w] << endl;
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
#define deb(x) cout << #x << " " << x << "\n";
#define MAX 9223372036854775807
#define MIN -9223372036854775807
#define PI 3.141592653589
#define setbits(n) __builtin_popcountll(n)
const ll mod = 1e9 + 7;

const int N = 100 + 1, W = 1e5 + 1;
ll dp[W][N], n, p;
vector<ll> w(N), v(N);

ll go(ll left, ll pos) {
  if (pos == n)
    return 0;
  if (dp[left][n] != -1)
    return dp[left][n];
  ll ans = 0;
  if (w[pos] <= left)
    ans = v[pos] + go(left - w[pos], pos + 1);
  ans = max(ans, go(left, pos + 1));
  return dp[left][pos] = ans;
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll T = clock();

  cin >> n >> p;
  for (ll i = 0; i < n; i++)
    cin >> w[i] >> v[i];
  memset(dp, -1, sizeof(dp));
  cout << go(p, 0);

  cerr << "\n\nTIME: " << (double)(clock() - T) / CLOCKS_PER_SEC << " sec\n";
  T = clock();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
#define deb(x) cout << #x << " " << x << "\n";
#define MAX 9223372036854775807
#define MIN -9223372036854775807
#define PI 3.141592653589
#define setbits(n) __builtin_popcountll(n)
const ll mod = 1e9 + 7;

const int N = 100 + 1, W = 1e5 + 1;
ll dp[W][N], n, p;
vector<ll> w(N), v(N);

ll go(ll left, ll pos) {
  if (pos == n)
    return 0;
  if (dp[left][pos] != -1)
    return dp[left][pos];
  ll ans = 0;
  if (w[pos] <= left)
    ans = v[pos] + go(left - w[pos], pos + 1);
  ans = max(ans, go(left, pos + 1));
  return dp[left][pos] = ans;
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll T = clock();

  cin >> n >> p;
  for (ll i = 0; i < n; i++)
    cin >> w[i] >> v[i];
  memset(dp, -1, sizeof(dp));
  cout << go(p, 0);

  cerr << "\n\nTIME: " << (double)(clock() - T) / CLOCKS_PER_SEC << " sec\n";
  T = clock();
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const int INF = 1001001001;

int main() {
  int n, W;
  cin >> n >> W;
  vector<int> w(n);
  vector<ll> v(n);
  rep(i, n) cin >> w[i] >> v[i];
  // dp[i][j] i番目までで、重さWを選ぶ場合の最大価値
  vvl dp(n + 1, vector<ll>(W, 0));

  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][j], dp[i][j - w[i]] + v[i]);
      else
        dp[i + 1][j] = dp[i][j];
    }
  }
  cout << dp[n][W] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const int INF = 1001001001;

int main() {
  int n, W;
  cin >> n >> W;
  vector<int> w(n);
  vector<ll> v(n);
  rep(i, n) cin >> w[i] >> v[i];
  // dp[i][j] i番目までで、重さWを選ぶ場合の最大価値
  vvl dp(n + 1, vector<ll>(W + 1, 0));

  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][j], dp[i][j - w[i]] + v[i]);
      else
        dp[i + 1][j] = dp[i][j];
    }
  }
  cout << dp[n][W] << endl;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <limits.h>
#include <map>
#include <string>
#include <vector>
using namespace std;

#define p(s) std::cout << s << endl;
#define printIf(j, s1, s2) cout << (j ? s1 : s2) << endl;
#define YES(j) cout << (j ? "YES" : "NO") << endl;
#define Yes(j) std::cout << (j ? "Yes" : "No") << endl;

const int limit = 1e5 + 1;

long long int dp[100][limit];

long long int dfs(int depth, int capacity, int n, long long int w[],
                  long long int v[]) {
  // メモ化してたら返す
  if (dp[depth][capacity] >= 0)
    return dp[depth][capacity];

  long long int res;
  if (depth == n) {
    // 全て考慮した
    res = 0;
  } else if (w[depth] > capacity) {
    // 入らないので次に行く
    res = dfs(depth + 1, capacity, n, w, v);
  } else {
    // 入れる場合と入れない場合の両方試す
    res = max(dfs(depth + 1, capacity - w[depth], n, w, v) + v[depth],
              dfs(depth + 1, capacity, n, w, v));
  }
  return dp[depth][capacity] = res;
}

int main() {
  int n;
  long long int w[limit], v[limit];

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

  p(dfs(0, W, n, w, v))
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <limits.h>
#include <map>
#include <string>
#include <vector>
using namespace std;

#define p(s) std::cout << s << endl;
#define printIf(j, s1, s2) cout << (j ? s1 : s2) << endl;
#define YES(j) cout << (j ? "YES" : "NO") << endl;
#define Yes(j) std::cout << (j ? "Yes" : "No") << endl;

const int limit = 1e5 + 1;

long long int dp[101][limit];

long long int dfs(int depth, int capacity, int n, long long int w[],
                  long long int v[]) {
  // メモ化してたら返す
  if (dp[depth][capacity] >= 0)
    return dp[depth][capacity];

  long long int res;
  if (depth == n) {
    // 全て考慮した
    res = 0;
  } else if (w[depth] > capacity) {
    // 入らないので次に行く
    res = dfs(depth + 1, capacity, n, w, v);
  } else {
    // 入れる場合と入れない場合の両方試す
    res = max(dfs(depth + 1, capacity - w[depth], n, w, v) + v[depth],
              dfs(depth + 1, capacity, n, w, v));
  }
  return dp[depth][capacity] = res;
}

int main() {
  int n;
  long long int w[limit], v[limit];

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

  p(dfs(0, W, n, w, v))
}
Problem p03163 (C++)

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

ll dp[100][(uint)1e5 + 1];
int main() {
  int n, W;
  cin >> n >> W;
  int w[100];
  ll v[100];
  for (int i = 0; i < n; i++)
    cin >> w[i] >> v[i];
  for (int i = 0; i < n; i++) {
    for (int j = 1; j <= W; j++) {
      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;
using ll = long long;
using uint = unsigned int;

ll dp[101][(uint)1e5 + 1];
int main() {
  int n, W;
  cin >> n >> W;
  int w[100];
  ll v[100];
  for (int i = 0; i < n; i++)
    cin >> w[i] >> v[i];
  for (int i = 0; i < n; i++) {
    for (int j = 1; j <= W; j++) {
      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 <bits/stdc++.h>
#define st first
#define nd second
#define mp make_pair
#define pb push_back
#define N 100005
using namespace std;
typedef long long ll;
ll n, k, v[N], w[N], dp[105][N];

ll f(ll kal, ll x) {
  ll &r = dp[kal][x];
  if (r != -1)
    return r;
  if (x == n + 1)
    return 0;
  r = f(kal, x + 1);
  if (w[x] <= kal)
    r = max(r, v[x] + f(kal - w[x], x + 1));
  return r;
}

int main() {
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
  memset(dp, -1, sizeof dp);
  scanf("%lld %lld", &n, &k);
  for (ll i = 1; i <= n; i++)
    scanf("%lld %lld", w + i, v + i);
  printf("%lld\n", f(k, 1));
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define st first
#define nd second
#define mp make_pair
#define pb push_back
#define N 100005
using namespace std;
typedef long long ll;
ll n, k, v[N], w[N], dp[N][105];

ll f(ll kal, ll x) {
  ll &r = dp[kal][x];
  if (r != -1)
    return r;
  if (x == n + 1)
    return 0;
  r = f(kal, x + 1);
  if (w[x] <= kal)
    r = max(r, v[x] + f(kal - w[x], x + 1));
  return r;
}

int main() {
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
  memset(dp, -1, sizeof dp);
  scanf("%lld %lld", &n, &k);
  for (ll i = 1; i <= n; i++)
    scanf("%lld %lld", w + i, v + i);
  printf("%lld\n", f(k, 1));
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main() {
  int n, W;
  cin >> n >> W;
  int *dp = new int[W + 1];
  for (int i = 1; i <= n; i++) {
    int weight, value;
    cin >> weight >> value;
    for (int j = W; j >= 0; j--) {
      dp[j + weight] = max(dp[j] + value, dp[j + weight]);
    }
  }
  cout << dp[W];
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main() {
  int n, W;
  cin >> n >> W;
  int *dp = new int[W + 1];
  for (int i = 1; i <= n; i++) {
    int weight, value;
    cin >> weight >> value;
    for (int j = W; j >= weight; j--) {
      dp[j] = max(dp[j - weight] + value, dp[j]);
    }
  }
  cout << dp[W];
}
Problem p03163 (C++)

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

ll knapsack(ll a[][2], ll i, ll w, ll n) {
  if (w <= 0)
    return 0;
  if (i == n)
    return 0;
  if (dp[i][w] != 0)
    return dp[i][w];
  ll op1 = INT_MIN;
  if (w - a[i][0] >= 0)
    op1 = a[i][1] + knapsack(a, i + 1, w - a[i][0], n);
  ll op2 = knapsack(a, i + 1, w, n);
  return dp[i][w] = max(op1, op2);
}

int main() {
  ll n, w;
  cin >> n >> w;
  //{weight, value}
  ll a[n][2];
  // memset(dp,-1,sizeof dp);
  for (ll i = 0; i < n; i++)
    cin >> a[i][0] >> a[i][1];
  cout << knapsack(a, 0, w, n);
}


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

ll knapsack(ll a[][2], ll i, ll w, ll n) {
  if (w <= 0)
    return 0;
  if (i == n)
    return 0;
  if (dp[i][w] != 0)
    return dp[i][w];
  ll op1 = INT_MIN;
  if (w - a[i][0] >= 0)
    op1 = a[i][1] + knapsack(a, i + 1, w - a[i][0], n);
  ll op2 = knapsack(a, i + 1, w, n);
  return dp[i][w] = max(op1, op2);
}

int main() {
  ll n, w;
  cin >> n >> w;
  //{weight, value}
  ll a[n][2];
  // memset(dp,-1,sizeof dp);
  for (ll i = 0; i < n; i++)
    cin >> a[i][0] >> a[i][1];
  cout << knapsack(a, 0, w, n);
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define DEBUG(x) cout << #x << " = " << (x) << endl;
#define SORT(x) sort(ALL(x));
#define RSORT(x) sort(RALL(x));
#define SUM(x) accumulate(ALL(x), 0);
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using tiii = tuple<int, int, int>;
const ll mod = 1000000007;
const int INF = 1e+9;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
// cout << fixed << setprecision(10);

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int w[110], v[110];
int dp[110][110] = {};

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

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

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

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define DEBUG(x) cout << #x << " = " << (x) << endl;
#define SORT(x) sort(ALL(x));
#define RSORT(x) sort(RALL(x));
#define SUM(x) accumulate(ALL(x), 0);
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using tiii = tuple<int, int, int>;
const ll mod = 1000000007;
const int INF = 1e+9;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
// cout << fixed << setprecision(10);

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int w[110], v[110];
ll dp[110][100100] = {};

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

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

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

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

ll dp[101][101010];

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

  rep(i, N) cin >> w[i + 1] >> v[i + 1];

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

  ll ans = 0;
  for (int i = 0; i <= W; ++i)
    ans = max(ans, dp[N][i]);
  cout << ans << endl;
}


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

ll dp[101][101010];

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

  rep(i, N) cin >> w[i + 1] >> v[i + 1];

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

  ll ans = 0;
  for (int i = 0; i <= W; ++i)
    ans = max(ans, dp[N][i]);
  cout << ans << endl;
}