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

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

using namespace std;
typedef long long ll;
const int N = 100, W = 1e5 + 1;
ll dp[N][W];
ll v[N], w[N];
ll n, cap;
ll rec(int i, ll rem) {
  if (i > n) {
    return 0;
  }
  ll &ret = dp[i][rem];
  if (ret != -1) {
    return ret;
  }
  ret = max(ret, rec(i + 1, rem));
  if (w[i] <= rem) {
    ret = max(ret, rec(i + 1, rem - w[i]) + v[i]);
  }
  return ret;
}
int main() {
  cin >> n >> cap;
  memset(dp, -1, sizeof dp);
  for (int i = 1; i <= n; i++) {
    cin >> w[i] >> v[i];
  }
  cout << rec(1, cap);
  return 0;
}


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

using namespace std;
typedef long long ll;
const int N = 101, W = 1e5 + 1;
ll dp[N][W];
ll v[N], w[N];
ll n, cap;
ll rec(int i, ll rem) {
  if (i > n) {
    return 0;
  }
  ll &ret = dp[i][rem];
  if (ret != -1) {
    return ret;
  }
  ret = max(ret, rec(i + 1, rem));
  if (w[i] <= rem) {
    ret = max(ret, rec(i + 1, rem - w[i]) + v[i]);
  }
  return ret;
}
int main() {
  cin >> n >> cap;
  memset(dp, -1, sizeof dp);
  for (int i = 1; i <= n; i++) {
    cin >> w[i] >> v[i];
  }
  cout << rec(1, cap);
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>
#define mod 1000000007
#define eps 1e-9
#define PI 3.14159265358979323846
#define F first
#define S second
#define ll long long
#define pb push_back
#define mp make_pair
#define Fr(i, a, b) for (int i = a; i <= b; i++)
#define RF(i, a, b) for (int i = a; i >= b; i--)
#define pii pair<ll, int>
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define vpii vector<pii>
using namespace std;

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

  int t = 1;
  // cin>>t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    vi a;
    vi b;
    int x, y;

    int sum = 0;
    Fr(i, 0, n - 1) {
      cin >> x >> y;
      a.pb(x);
      b.pb(y);
      sum += x;
    }
    vvi dp(n + 1, vi(sum + 1, -1));
    dp[0][0] = 0;
    ll ans = 0;
    Fr(i, 1, n) {
      Fr(j, 0, min(sum, k)) {
        dp[i][j] = dp[i - 1][j];
        if (j - a[i - 1] >= 0 && dp[i - 1][j - a[i - 1]] != -1) {
          dp[i][j] = max(dp[i][j], dp[i - 1][j - a[i - 1]] + b[i - 1]);
        }
        ans = max(ans, dp[i][j]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
#define mod 1000000007
#define eps 1e-9
#define PI 3.14159265358979323846
#define F first
#define S second
#define ll long long
#define pb push_back
#define mp make_pair
#define Fr(i, a, b) for (int i = a; i <= b; i++)
#define RF(i, a, b) for (int i = a; i >= b; i--)
#define pii pair<ll, int>
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define vpii vector<pii>
using namespace std;

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

  int t = 1;
  // cin>>t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    vi a;
    vi b;
    int x, y;

    int sum = 0;
    Fr(i, 0, n - 1) {
      cin >> x >> y;
      a.pb(x);
      b.pb(y);
      sum += x;
    }
    vvi dp(n + 1, vi(min(k + 1, sum + 1), -1));
    dp[0][0] = 0;
    ll ans = 0;
    Fr(i, 1, n) {
      Fr(j, 0, min(sum, k)) {
        dp[i][j] = dp[i - 1][j];
        if (j - a[i - 1] >= 0 && dp[i - 1][j - a[i - 1]] != -1) {
          dp[i][j] = max(dp[i][j], dp[i - 1][j - a[i - 1]] + b[i - 1]);
        }
        ans = max(ans, dp[i][j]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
//
// Created by Hideaki Imamura on 2020-03-15.
//
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;

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

#define EPS (1e-7)
// # define INF (1e9)
#define PI (acos(-1))
// const ll mod = 1000000007;

ll N, W;
ll w[110], v[110];

int main() {
  cin >> N >> W;
  for (int i = 0; i < N; ++i)
    cin >> w[i] >> v[i];

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

Fixed submission:
//
// Created by Hideaki Imamura on 2020-03-15.
//
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;

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

#define EPS (1e-7)
// # define INF (1e9)
#define PI (acos(-1))
// const ll mod = 1000000007;

ll N, W;
ll w[110], v[110];

int main() {
  cin >> N >> W;
  for (int i = 0; i < N; ++i)
    cin >> w[i] >> v[i];

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

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

int main() {
  int N;
  long long W;
  cin >> N >> W;
  long long v[110], w[110];
  for (int i = 0; i < N; i++)
    cin >> w[i] >> v[i];

  long long dp[110][110];

  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;

int main() {
  int N;
  long long W;
  cin >> N >> W;
  long long v[110], w[110];
  for (int i = 0; i < N; i++)
    cin >> w[i] >> v[i];

  long long dp[110][100010];

  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: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;
vector<vector<long long>> dp(101, vector<long long>(1e5 + 1, -1));
long long sol(vector<vector<long long>> a, int i, int k) {
  int n = a.size();
  if (i >= n)
    return 0;

  if (dp[i][k] != -1)
    return dp[i][k];
  long long ans = sol(a, i + 1, k);

  if (a[i][0] <= k)
    ans = max(ans, sol(a, i + 1, k - a[i][0]) + a[i][1]);

  return dp[i][k] = ans;
}

int main() {
  int n, k;
  cin >> n >> k;
  vector<vector<long long>> a(n, vector<long long>(2));

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 2; j++)
      cin >> a[i][j];
  }
  dp[0][8] = sol(a, 0, k);
  cout << dp[0][8] << endl;
}

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

using namespace std;
vector<vector<long long>> dp(101, vector<long long>(1e5 + 1, -1));
long long sol(vector<vector<long long>> &a, int i, int k) {
  int n = a.size();
  if (i >= n)
    return 0;

  if (dp[i][k] != -1)
    return dp[i][k];
  long long ans = sol(a, i + 1, k);

  if (a[i][0] <= k)
    ans = max(ans, sol(a, i + 1, k - a[i][0]) + a[i][1]);

  return dp[i][k] = ans;
}

int main() {
  int n, k;
  cin >> n >> k;
  vector<vector<long long>> a(n, vector<long long>(2));

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 2; j++)
      cin >> a[i][j];
  }
  dp[0][8] = sol(a, 0, k);
  cout << dp[0][8] << endl;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long dp[100][100005];
int main() {
  int n, w;
  long long m[105], v[105];

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

  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= w; j++) {
      if (m[i] <= j) {
        dp[i + 1][j] = max(dp[i][j - m[i]] + v[i], dp[i][j]);
      } else {
        dp[i + 1][j] = dp[i][j];
      }
    }
  }
  cout << dp[n][w] << '\n';
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long dp[105][100005];
int main() {
  int n, w;
  long long m[105], v[105];

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

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

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

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

Buggy submission (status: Runtime Error):
#include <iostream>
#include <stdio.h>
#define Re register int

int N, W, w[105], v[105];
long long f[10005];

template <typename T> inline void read(T &var) {
  T x = 0;
  int w = 0;
  char ch = 0;
  while (!isdigit(ch))
    w |= ch == '-', ch = getchar();
  while (isdigit(ch))
    x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
  var = w ? -x : x;
}
int main(int argc, char const *argv[]) {
  read(N), read(W);
  for (Re i = 1; i <= N; ++i)
    read(w[i]), read(v[i]);
  for (Re i = 1; i <= N; ++i)
    for (Re j = W; j >= w[i]; --j)
      f[j] = std::max(f[j], f[j - w[i]] + v[i]);
  printf("%lld\n", f[W]);
  return 0;
}

Fixed submission:
#include <iostream>
#include <stdio.h>
#define Re register int

int N, W, w[105], v[105];
long long f[100005];

template <typename T> inline void read(T &var) {
  T x = 0;
  int w = 0;
  char ch = 0;
  while (!isdigit(ch))
    w |= ch == '-', ch = getchar();
  while (isdigit(ch))
    x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
  var = w ? -x : x;
}
int main(int argc, char const *argv[]) {
  read(N), read(W);
  for (Re i = 1; i <= N; ++i)
    read(w[i]), read(v[i]);
  for (Re i = 1; i <= N; ++i)
    for (Re j = W; j >= w[i]; --j)
      f[j] = std::max(f[j], f[j - w[i]] + v[i]);
  printf("%lld\n", f[W]);
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define INF 1000000000
#define MAXN 110
#define MAXW 110
using namespace std;

int N, W;
long long dp[MAXW];
long long v[MAXN];
int w[MAXN];

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

Fixed submission:
#include <bits/stdc++.h>
#define INF 1000000000
#define MAXN 110
#define MAXW 110000
using namespace std;

int N, W;
long long dp[MAXW];
long long v[MAXN];
int w[MAXN];

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define peso first
#define beneficio second
int n, m;
vector<pair<int, int>> num(100001);
long long dp[10005];
int main() {
  cin >> n >> m;
  for (int qq = 1; qq <= n; qq++) {
    cin >> num[qq].peso >> num[qq].beneficio;
  }
  dp[0] = 0;
  for (int qq = 1; qq <= n; qq++) {
    for (int ww = m; ww >= num[qq].peso; ww--) {
      dp[ww] = max(dp[ww], dp[ww - num[qq].peso] + num[qq].beneficio);
    }
  }
  cout << dp[m];
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define peso first
#define beneficio second
int n, m;
vector<pair<int, int>> num(100001);
long long dp[100005];
int main() {
  cin >> n >> m;
  for (int qq = 1; qq <= n; qq++) {
    cin >> num[qq].peso >> num[qq].beneficio;
  }
  dp[0] = 0;
  for (int qq = 1; qq <= n; qq++) {
    for (int ww = m; ww >= num[qq].peso; ww--) {
      dp[ww] = max(dp[ww], dp[ww - num[qq].peso] + num[qq].beneficio);
    }
  }
  cout << dp[m];
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fastIO                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
#define ll long long
#define f(i, l, r) for (i = l; i < r; i++)
#define fd(i, l, r) for (i = l; i > r; i--)
#define mod 1000000007
#define pb push_back
using namespace std;
typedef pair<int, int> pi;

int main() {
  fastIO int test = 1;
  // cin >> test;
  while (test--) {
    int n, w;
    cin >> n >> w;
    int weight[n], i, j;
    long int value[n];
    f(i, 0, n) cin >> weight[i] >> value[i];
    ll knapsack[n + 1][w + 1];
    f(i, 0, n + 1) {
      f(j, 0, w + 1) {
        if (i == 0 || j == 0)
          knapsack[i][j] = 0;
        else if (weight[i - 1] <= w)
          knapsack[i][j] =
              max(value[i - 1] + knapsack[i - 1][j - weight[i - 1]],
                  knapsack[i - 1][j]);
        else
          knapsack[i][j] = knapsack[i - 1][j];
      }
    }
    cout << knapsack[n][w];
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define fastIO                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
#define ll long long
#define f(i, l, r) for (i = l; i < r; i++)
#define fd(i, l, r) for (i = l; i > r; i--)
#define mod 1000000007
#define pb push_back
using namespace std;
typedef pair<int, int> pi;

int main() {
  fastIO int test = 1;
  // cin >> test;
  while (test--) {
    int n, w;
    cin >> n >> w;
    int weight[n], i, j;
    long int value[n];
    f(i, 0, n) cin >> weight[i] >> value[i];
    ll knapsack[n + 1][w + 1];
    f(i, 0, n + 1) {
      f(j, 0, w + 1) {
        if (i == 0 || j == 0)
          knapsack[i][j] = 0;
        else if (weight[i - 1] <= j)
          knapsack[i][j] =
              max(value[i - 1] + knapsack[i - 1][j - weight[i - 1]],
                  knapsack[i - 1][j]);
        else
          knapsack[i][j] = knapsack[i - 1][j];
      }
    }
    cout << knapsack[n][w];
  }
}
Problem p03163 (C++)

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


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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <fstream>
#include <iostream>
using namespace std;
int n, w;
long long greutate[105] = {0}, cost[105] = {0};
long long matrice[105][105] = {0};

void read() {
  cin >> n >> w;
  for (int i = 1; i <= n; i++) {
    int x, y;
    cin >> x >> y;
    greutate[i] = x;
    cost[i] = y;
  }
}
void solve() {
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= w; j++) {
      matrice[i][j] = matrice[i - 1][j];
      if (j >= greutate[i]) {
        if (matrice[i - 1][j] > matrice[i - 1][j - greutate[i]] + cost[i])
          matrice[i][j] = matrice[i - 1][j];
        else
          matrice[i][j] = matrice[i - 1][j - greutate[i]] + cost[i];
      }
    }
}
int main() {
  read();
  solve();
  cout << matrice[n][w];
}

Fixed submission:
#include <algorithm>
#include <fstream>
#include <iostream>
using namespace std;
int n, w;
long long greutate[1000005] = {0}, cost[1000005] = {0};
long long matrice[105][1000005] = {0};

void read() {
  cin >> n >> w;
  for (int i = 1; i <= n; i++) {
    int x, y;
    cin >> x >> y;
    greutate[i] = x;
    cost[i] = y;
  }
}
void solve() {
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= w; j++) {
      matrice[i][j] = matrice[i - 1][j];
      if (j >= greutate[i]) {
        if (matrice[i - 1][j] > matrice[i - 1][j - greutate[i]] + cost[i])
          matrice[i][j] = matrice[i - 1][j];
        else
          matrice[i][j] = matrice[i - 1][j - greutate[i]] + cost[i];
      }
    }
}
int main() {
  read();
  solve();
  cout << matrice[n][w];
}
Problem p03163 (C++)

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

long long solve(long long taille, long long w) {
  if (taille < 0)
    return 0;
  if (dp[taille][w] != -1)
    return dp[taille][w];
  if (weight[taille - 1] > w)
    return dp[taille][w] = solve(taille - 1, w);
  else
    return dp[taille][w] = max(solve(taille - 1, w - weight[taille - 1]) +
                                   value[taille - 1],
                               solve(taille - 1, w));
}

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

  memset(dp, -1, sizeof(dp));
  cin >> n >> w;
  for (int i = 0; i < n; i++)
    cin >> weight[i] >> value[i];

  long long ans = solve(n, w);
  cout << ans;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 103;
long long weight[N], value[N], n, w;
long long dp[N][100003];

long long solve(long long taille, long long w) {
  if (taille < 0)
    return 0;
  if (dp[taille][w] != -1)
    return dp[taille][w];
  if (weight[taille - 1] > w)
    return dp[taille][w] = solve(taille - 1, w);
  else
    return dp[taille][w] = max(solve(taille - 1, w - weight[taille - 1]) +
                                   value[taille - 1],
                               solve(taille - 1, w));
}

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

  memset(dp, -1, sizeof(dp));
  cin >> n >> w;
  for (int i = 0; i < n; i++)
    cin >> weight[i] >> value[i];

  long long ans = solve(n, w);
  cout << ans;

  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <fstream>
#include <iostream>
#include <vector>
#define nmax 101

using namespace std;
ifstream fin("p.in");
ofstream fout("p.out");

struct object {
  long long val, g;
} obj[nmax];

int n, g_max;
long long sol;
long long val_curr[nmax][nmax * nmax];

int main() {
  cin >> n >> g_max;
  for (int i = 1; i <= n; i++)
    cin >> obj[i].g >> obj[i].val;

  for (int i = 1; i <= n; i++)
    for (int j = 0; j <= g_max; j++) {
      if (obj[i].g <= j)
        val_curr[i][j] =
            max(obj[i].val + val_curr[i - 1][j - obj[i].g], val_curr[i - 1][j]);
      else
        val_curr[i][j] = val_curr[i - 1][j];
    }

  cout << val_curr[n][g_max];
  return 0;
}


Fixed submission:
#include <algorithm>
#include <fstream>
#include <iostream>
#include <vector>
#define nmax 101

using namespace std;
ifstream fin("p.in");
ofstream fout("p.out");

struct object {
  long long val, g;
} obj[nmax];

int n, g_max;
long long sol;
long long val_curr[nmax][nmax * nmax * 10];

int main() {
  cin >> n >> g_max;
  for (int i = 1; i <= n; i++)
    cin >> obj[i].g >> obj[i].val;

  for (int i = 1; i <= n; i++)
    for (int j = 0; j <= g_max; j++) {
      if (obj[i].g <= j)
        val_curr[i][j] =
            max(obj[i].val + val_curr[i - 1][j - obj[i].g], val_curr[i - 1][j]);
      else
        val_curr[i][j] = val_curr[i - 1][j];
    }

  cout << val_curr[n][g_max];
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ms(s) memset(s, 0, sizeof(s))
const ll INF = 1e9;
inline ll read() {
  ll X = 0, w = 0;
  char ch = 0;
  while (!isdigit(ch)) {
    w |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch))
    X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
  return w ? -X : X;
}

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


Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ms(s) memset(s, 0, sizeof(s))
const ll INF = 1e9;
inline ll read() {
  ll X = 0, w = 0;
  char ch = 0;
  while (!isdigit(ch)) {
    w |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch))
    X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
  return w ? -X : X;
}

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
// #include <ext/numeric>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
#define oo 0x3f3f3f3f
#define OO 0x3f3f3f3f3f3f3f3f
#define ones(n) __builtin_popcount(n)
#define ONES(n) __builtin_popcountll(n)

using namespace std;
// using namespace __gnu_cxx;
using namespace __gnu_pbds;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

typedef pair<int, int> ii;
typedef pair<long long, pair<int, int>> iii;
typedef pair<ii, ii> iiii;

const double PI = acos(-1.0), EPS = 1e-6;
const int N = 100005, M = 102, mod = 998244353, mxLog = 20;
int n, w;
int W[M], V[M];
long long memo[M][N];
long long solve(int i, int cur_w) {
  if (i == n)
    return 0;
  long long &ret = memo[i][cur_w];
  if (~ret)
    return ret;
  ret = solve(i + 1, cur_w);
  if (cur_w + W[i] <= w) {
    ret = max(ret, solve(i + 1, cur_w + W[i]) + V[i]);
  }
  return ret;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "rt", stdin);
//    freopen("output.txt", "w", stdout);
#endif
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0), cout.precision(7),
      cout << fixed;

  cin >> n >> w;
  for (int i = 0; i < n; ++i) {
    cin >> W[i] >> V[i];
  }
  memset(memo, -1, sizeof memo);
  cout << solve(0, 0) << '\n';

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
// #include <ext/numeric>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
#define oo 0x3f3f3f3f
#define OO 0x3f3f3f3f3f3f3f3f
#define ones(n) __builtin_popcount(n)
#define ONES(n) __builtin_popcountll(n)

using namespace std;
// using namespace __gnu_cxx;
using namespace __gnu_pbds;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

typedef pair<int, int> ii;
typedef pair<long long, pair<int, int>> iii;
typedef pair<ii, ii> iiii;

const double PI = acos(-1.0), EPS = 1e-6;
const int N = 100005, M = 102, mod = 998244353, mxLog = 20;
int n, w;
int W[M], V[M];
long long memo[M][N];
long long solve(int i, int cur_w) {
  if (i == n)
    return 0;
  long long &ret = memo[i][cur_w];
  if (~ret)
    return ret;
  ret = solve(i + 1, cur_w);
  if (cur_w + W[i] <= w) {
    ret = max(ret, solve(i + 1, cur_w + W[i]) + V[i]);
  }
  return ret;
}

int main() {
#ifndef ONLINE_JUDGE
//    freopen("input.txt", "rt", stdin);
//    freopen("output.txt", "w", stdout);
#endif
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0), cout.precision(7),
      cout << fixed;

  cin >> n >> w;
  for (int i = 0; i < n; ++i) {
    cin >> W[i] >> V[i];
  }
  memset(memo, -1, sizeof memo);
  cout << solve(0, 0) << '\n';

  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int maxi = 105;
long long M[maxi][2], dp[maxi][maxi];
long long f(int n, int s) {
  if (n < 0 || s <= 0)
    return 0;
  if (dp[n][s] != -1)
    return dp[n][s];
  if (s - M[n][0] < 0)
    return dp[n][s] = f(n - 1, s);
  return dp[n][s] = max(M[n][1] + f(n - 1, s - M[n][0]), f(n - 1, s));
}
int main() {
  int n, w;
  cin >> n >> w;
  for (int i = 0; i < n; i++)
    cin >> M[i][0] >> M[i][1];
  memset(dp, -1, sizeof(dp));
  cout << f(n - 1, w);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxi = 1e5 + 10;
long long M[105][2], dp[105][maxi];
long long f(int n, int s) {
  if (n < 0 || s <= 0)
    return 0;
  if (dp[n][s] != -1)
    return dp[n][s];
  if (s - M[n][0] < 0)
    return dp[n][s] = f(n - 1, s);
  return dp[n][s] = max(M[n][1] + f(n - 1, s - M[n][0]), f(n - 1, s));
}
int main() {
  int n, w;
  cin >> n >> w;
  for (int i = 0; i < n; i++)
    cin >> M[i][0] >> M[i][1];
  memset(dp, -1, sizeof(dp));
  cout << f(n - 1, w);
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;
int a[111], wi[111];
long long dp[111111];
bool d[111111];
int main() {
  int n, w;
  cin >> n >> w;
  for (int i = 1; i <= n; i++) {
    cin >> wi[i] >> a[i];
  }
  d[0] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = w; j >= 0; j--) {
      if (d[j] == 1) {
        d[j + wi[i]] = 1;
        dp[j + wi[i]] = max(dp[j + wi[i]], dp[j] + a[i]);
      }
    }
  }
  long long m = 0;
  for (int i = 1; i <= w; i++) {
    m = max(dp[i], m);
  }
  cout << m;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
int a[111], wi[111];
long long dp[111111];
bool d[111111];
int main() {
  int n, w;
  cin >> n >> w;
  for (int i = 1; i <= n; i++) {
    cin >> wi[i] >> a[i];
  }
  d[0] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = w; j >= 0; j--) {
      if (d[j] == 1 && j + wi[i] <= w) {
        d[j + wi[i]] = 1;
        dp[j + wi[i]] = max(dp[j + wi[i]], dp[j] + a[i]);
      }
    }
  }
  long long m = 0;
  for (int i = 1; i <= w; i++) {
    m = max(dp[i], m);
  }
  cout << m;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define N 100005
#define M 10000000000
#define endl "\n"
using namespace std;

int m;

int Wt;

int wt[101];

long long val[101];

long long dp[100][N];

long long maxCost(int n, int W) {

  if (n - 1 < 0)
    return 0;

  if (W == 0)
    return 0;

  if (dp[n][W] != -1)
    return dp[n][W];

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

int32_t main() {

  cin >> m >> Wt;

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

  memset(dp, -1, sizeof(dp));

  cout << maxCost(m, Wt) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define N 100005
#define M 10000000000
#define endl "\n"
using namespace std;

int m;

int Wt;

int wt[101];

long long val[101];

long long dp[101][N];

long long maxCost(int n, int W) {

  if (n - 1 < 0)
    return 0;

  if (W == 0)
    return 0;

  if (dp[n][W] != -1)
    return dp[n][W];

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

int32_t main() {

  cin >> m >> Wt;

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

  memset(dp, -1, sizeof(dp));

  cout << maxCost(m, Wt) << endl;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
// In the name of God:)
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 110;

long long dp[MAXN], w[MAXN], v[MAXN];

int main() {
  long long n, mw;
  long long ans = 0;
  scanf("%lld%lld", &n, &mw);
  for (int i = 0; i < n; i++) {
    scanf("%lld%lld", w + i, v + i);
    for (int j = mw; j >= w[i]; j--) {
      dp[j] = max(dp[j], dp[j - w[i]] + v[i]);
      ans = max(ans, dp[j]);
    }
  }
  printf("%lld\n", ans);
}


Fixed submission:
// In the name of God:)
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;

long long dp[MAXN], w[MAXN], v[MAXN];

int main() {
  long long n, mw;
  long long ans = 0;
  scanf("%lld%lld", &n, &mw);
  for (int i = 0; i < n; i++) {
    scanf("%lld%lld", w + i, v + i);
    for (int j = mw; j >= w[i]; j--) {
      dp[j] = max(dp[j], dp[j - w[i]] + v[i]);
      ans = max(ans, dp[j]);
    }
  }
  printf("%lld\n", ans);
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long LL;
typedef pair<int, int> pii;

template <typename T> inline int Chkmax(T &a, T b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T> inline int Chkmin(T &a, T b) {
  return a > b ? a = b, 1 : 0;
}

template <typename T> inline T read() {
  T sum = 0;
  int fl = 1, ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      fl = -1;
  for (; isdigit(ch); ch = getchar())
    sum = (sum << 3) + (sum << 1) + ch - '0';
  return sum * fl;
}

const int maxn = 100 + 5;

int n, W;
int dp[maxn];

struct node {
  int w;
  int v;
} a[maxn];

inline void Solve() {
  for (int i = 1; i <= n; i++) {
    for (int j = W; j >= a[i].w; j--) {
      dp[j] = max(dp[j], dp[j - a[i].w] + a[i].v);
    }
  }
  printf("%lld\n", dp[W]);
}

inline void Input() {
  n = read<int>();
  W = read<int>();
  for (int i = 1; i <= n; i++) {
    a[i].w = read<int>();
    a[i].v = read<int>();
  }
}

signed main() {
  Input();
  Solve();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long LL;
typedef pair<int, int> pii;

template <typename T> inline int Chkmax(T &a, T b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T> inline int Chkmin(T &a, T b) {
  return a > b ? a = b, 1 : 0;
}

template <typename T> inline T read() {
  T sum = 0;
  int fl = 1, ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      fl = -1;
  for (; isdigit(ch); ch = getchar())
    sum = (sum << 3) + (sum << 1) + ch - '0';
  return sum * fl;
}

const int maxn = 100000 + 5;

int n, W;
int dp[maxn];

struct node {
  int w;
  int v;
} a[maxn];

inline void Solve() {
  for (int i = 1; i <= n; i++) {
    for (int j = W; j >= a[i].w; j--) {
      dp[j] = max(dp[j], dp[j - a[i].w] + a[i].v);
    }
  }
  printf("%lld\n", dp[W]);
}

inline void Input() {
  n = read<int>();
  W = read<int>();
  for (int i = 1; i <= n; i++) {
    a[i].w = read<int>();
    a[i].v = read<int>();
  }
}

signed main() {
  Input();
  Solve();
  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 mfill(x, y) memset(x, y, sizeof(x))
#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...) 42
#endif
#define y0 y12345
#define y1 y54321
typedef unsigned long ul;
typedef long long ll;
// typedef pair<int, int> mpair;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;
int n, m;
ul w[101], v[101];
ul dt[100][100001];
ul dp(int p, int nw) {
  ul res = 0;
  if (dt[p][nw] != -1) {
    return dt[p][nw];
  }
  if (p == n) {
    res = 0;
  } else if (w[p] + nw > m) {
    res = dp(p + 1, nw);
  } else {
    res = max(dp(p + 1, nw), v[p] + dp(p + 1, nw + w[p]));
  }
  return dt[p][nw] = res;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n >> m;
  memset(dt, -1, sizeof(dt));
  rep(i, n) { cin >> w[i] >> v[i]; }
  cout << dp(0, 0) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define mfill(x, y) memset(x, y, sizeof(x))
#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...) 42
#endif
#define y0 y12345
#define y1 y54321
typedef unsigned long ul;
typedef long long ll;
// typedef pair<int, int> mpair;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;
int n, m;
ul w[101], v[101];
ul dt[101][100001];
ul dp(int p, int nw) {
  ul res = 0;
  if (dt[p][nw] != -1) {
    return dt[p][nw];
  }
  if (p == n) {
    res = 0;
  } else if (w[p] + nw > m) {
    res = dp(p + 1, nw);
  } else {
    res = max(dp(p + 1, nw), v[p] + dp(p + 1, nw + w[p]));
  }
  return dt[p][nw] = res;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n >> m;
  memset(dt, -1, sizeof(dt));
  rep(i, n) { cin >> w[i] >> v[i]; }
  cout << dp(0, 0) << 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 REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;

ll n, W;
ll weight[110], value[110];

ll dp[110][100010];

int main() {
  int i, w;
  cin >> n >> W;
  for (i = 0; i < n; ++i)
    cin >> weight[i] >> value[i];
  for (w = 0; w <= W; w++)
    dp[w][0] = 0;
  for (i = 0; i < n; i++) {
    for (w = 0; w <= W; w++) {
      if (weight[i] <= w)
        dp[i + 1][w] = max(dp[i][w], dp[i][w - weight[i]] + value[i]);
      else
        dp[i + 1][w] = dp[i][w];
    }
  }
  cout << dp[n][W] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;

ll n, W;
ll weight[110], value[110];

ll dp[110][100010];

int main() {
  int i, w;
  cin >> n >> W;
  for (i = 0; i < n; ++i)
    cin >> weight[i] >> value[i];
  for (w = 0; w <= W; w++)
    dp[0][w] = 0;
  for (i = 0; i < n; i++) {
    for (w = 0; w <= W; w++) {
      if (weight[i] <= w)
        dp[i + 1][w] = max(dp[i][w], dp[i][w - weight[i]] + value[i]);
      else
        dp[i + 1][w] = dp[i][w];
    }
  }
  cout << dp[n][W] << endl;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
const char nl = '\n';
using namespace std;
typedef long long ll;
typedef long double ld;
typedef complex<ld> pt;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int N = 105, W = 1e5 + 10;
inline char get() {
  static char buf[10000], *S = buf, *T = buf;
  if (S == T) {
    T = (S = buf) + fread(buf, 1, 100000, stdin);
    if (S == T)
      return EOF;
  }
  return *S++;
}
inline void read(int &x) {
  static char c;
  x = 0;
  int sgn = 0;
  for (c = get(); c < '0' || c > '9'; c = get())
    if (c == '-')
      sgn = 1;
  for (; c >= '0' && c <= '9'; c = get())
    x = x * 10 + c - '0';
  if (sgn)
    x = -x;
}

int n, totw;
ll dp[W];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  read(n);
  read(totw);
  for (int i = 0; i < n; i++) {
    int w, v;
    read(w);
    read(v);
    for (int j = totw; j >= w; j--) {
      dp[j] = max(dp[j], dp[j - w] + v);
    }
  }
  cout << *max_element(dp, dp + W) << nl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
const char nl = '\n';
using namespace std;
typedef long long ll;
typedef long double ld;
typedef complex<ld> pt;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int N = 105, W = 1e5 + 10;
inline char get() {
  static char buf[10000], *S = buf, *T = buf;
  if (S == T) {
    T = (S = buf) + fread(buf, 1, 10000, stdin);
    if (S == T)
      return EOF;
  }
  return *S++;
}
inline void read(int &x) {
  static char c;
  x = 0;
  int sgn = 0;
  for (c = get(); c < '0' || c > '9'; c = get())
    if (c == '-')
      sgn = 1;
  for (; c >= '0' && c <= '9'; c = get())
    x = x * 10 + c - '0';
  if (sgn)
    x = -x;
}

int n, totw;
ll dp[W];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  read(n);
  read(totw);
  for (int i = 0; i < n; i++) {
    int w, v;
    read(w);
    read(v);
    for (int j = totw; j >= w; j--) {
      dp[j] = max(dp[j], dp[j - w] + v);
    }
  }
  cout << *max_element(dp, dp + W) << nl;
  return 0;
}
Problem p03163 (C++)

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

using namespace std;

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

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

using namespace std;

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h> // g++ -std=c++14 -o a a.cpp

using namespace std;

typedef pair<int, int> P;
typedef long long ll;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repto(i, n) for (int i = 0; i <= (n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007
#define DEBUG(x) cout << #x << ": " << x << endl;

__attribute__((constructor)) void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

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

  ll weight[101] = {0};
  ll value[101] = {0};
  ll vmax[101][10001] = {0};

  rep(i, N) {
    int wi, vi;
    cin >> wi >> vi;
    weight[i] = wi;
    value[i] = vi;
  }

  rep(j, W + 1) {
    if (j < weight[N - 1]) {
      vmax[N - 1][j] = 0;
    } else {
      vmax[N - 1][j] = value[N - 1];
    }
  }

  for (int i = N - 2; i >= 0; i--) {
    for (int j = 0; j <= W; j++) {
      if (j < weight[i]) {
        vmax[i][j] = vmax[i + 1][j];
      } else {
        vmax[i][j] = max(vmax[i + 1][j], value[i] + vmax[i + 1][j - weight[i]]);
      }
    }
  }

  // rep(i, N) {
  //     rep(j, W + 1) {
  //         DEBUG(i);
  //         DEBUG(j);
  //         DEBUG(vmax[i][j]);
  //     }
  // }

  cout << vmax[0][W] << endl;
}


Fixed submission:
#include <bits/stdc++.h> // g++ -std=c++14 -o a a.cpp

using namespace std;

typedef pair<int, int> P;
typedef long long ll;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repto(i, n) for (int i = 0; i <= (n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007
#define DEBUG(x) cout << #x << ": " << x << endl;

__attribute__((constructor)) void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

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

  ll weight[101] = {0};
  ll value[101] = {0};
  ll vmax[101][100001] = {0};

  rep(i, N) {
    int wi, vi;
    cin >> wi >> vi;
    weight[i] = wi;
    value[i] = vi;
  }

  rep(j, W + 1) {
    if (j < weight[N - 1]) {
      vmax[N - 1][j] = 0;
    } else {
      vmax[N - 1][j] = value[N - 1];
    }
  }

  for (int i = N - 2; i >= 0; i--) {
    for (int j = 0; j <= W; j++) {
      if (j < weight[i]) {
        vmax[i][j] = vmax[i + 1][j];
      } else {
        vmax[i][j] = max(vmax[i + 1][j], value[i] + vmax[i + 1][j - weight[i]]);
      }
    }
  }

  // rep(i, N) {
  //     rep(j, W + 1) {
  //         DEBUG(i);
  //         DEBUG(j);
  //         DEBUG(vmax[i][j]);
  //     }
  // }

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int n, w;
long a[101][2];
long dp[101][100001];

long solve(int n, int w) { // cout<<n<<" "<<w<<endl;
  if (w < 0)
    return (-a[n + 1][1] - 1);
  if (n == 1) {
    if (a[1][0] <= w) {
      dp[1][w] = a[1][1];
    } else
      dp[1][w] = 0; //-a[n+1][1]-1;
    return dp[1][w];
  } // basecase
  else {
    dp[n][w] = max(solve(n - 1, w), solve(n - 1, w - a[n][0]) + a[n][1]);
    return dp[n][w];
  }
}

int main() {
  // your code goes here
  cin >> n >> w;
  for (int i = 1; i <= n; ++i)
    cin >> a[i][0] >> a[i][1];
  cout << solve(n, w);
  /*for(int i=1;i<=n;++i){
      for(int j=1;j<=w;++j){
          cout<<dp[i][j]<<" ";
      }
      cout<<endl;
  }*/
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, w;
long a[101][2];
long dp[101][100001];

long solve(int n, int w) { // cout<<n<<" "<<w<<endl;
  if (w < 0)
    return (-a[n + 1][1] - 1);
  if (n == 1) {
    if (a[1][0] <= w) {
      dp[1][w] = a[1][1];
    } else
      dp[1][w] = 0; //-a[n+1][1]-1;
    return dp[1][w];
  } // basecase
  if (dp[n][w])
    return dp[n][w];
  else {
    dp[n][w] = max(solve(n - 1, w), solve(n - 1, w - a[n][0]) + a[n][1]);
    return dp[n][w];
  }
}

int main() {
  // your code goes here
  cin >> n >> w;
  for (int i = 1; i <= n; ++i)
    cin >> a[i][0] >> a[i][1];
  cout << solve(n, w);
  /*for(int i=1;i<=n;++i){
      for(int j=1;j<=w;++j){
          cout<<dp[i][j]<<" ";
      }
      cout<<endl;
  }*/
}
Problem p03163 (C++)

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

typedef long long ll;
typedef pair<int, ll> p;
const int maxn = 100000 + 10;

ll dp[110][maxn];

int n;

vector<p> snacks;

ll dfs(int step, int restSize) {
  if (step == n or restSize == 0)
    return 0;

  if (dp[step][restSize] != -1)
    return dp[step][restSize];

  ll value = 0;
  if (restSize >= snacks[step].first) {
    value = max(value, dfs(step + 1, restSize - snacks[step].first) +
                           snacks[step].second);
  }

  value = max(value, dfs(step + 1, restSize));

  return dp[step][restSize] = value;
}

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

  int w;
  cin >> n >> w;
  snacks.resize(n);

  for (int i = 0; i < maxn; i++)
    for (int j = 0; j < 110; j++)
      dp[i][j] = -1;

  for (int i = 0; i < n; i++)
    cin >> snacks[i].first >> snacks[i].second;

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

  return 0;
}


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

typedef long long ll;
typedef pair<int, ll> p;
const int maxn = 100000 + 10;

ll dp[110][maxn];

int n;

vector<p> snacks;

ll dfs(int step, int restSize) {
  if (step == n or restSize == 0)
    return 0;

  if (dp[step][restSize] != -1)
    return dp[step][restSize];

  ll value = 0;
  if (restSize >= snacks[step].first) {
    value = max(value, dfs(step + 1, restSize - snacks[step].first) +
                           snacks[step].second);
  }

  value = max(value, dfs(step + 1, restSize));

  return dp[step][restSize] = value;
}

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

  int w;
  cin >> n >> w;
  snacks.resize(n);

  for (int i = 0; i < maxn; i++)
    for (int j = 0; j < 110; j++)
      dp[j][i] = -1;

  for (int i = 0; i < n; i++)
    cin >> snacks[i].first >> snacks[i].second;

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

  return 0;
}
Problem p03163 (C++)

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

int N, G, p[5005], w[5005], a[10005], b[10005], i, j;

int main() {
  // ifstream cin("rucsac.in");
  cin >> N >> G;
  for (i = 1; i <= N; i++) {
    cin >> w[i] >> p[i];
  }
  for (i = 1; i <= N; ++i) {
    for (j = 1; j <= G; ++j) {
      if (w[i] > j)
        a[j] = b[j];
      else {
        a[j] = max(b[j], b[j - w[i]] + p[i]);
      }
    }
    for (j = 1; j <= G; ++j)
      b[j] = a[j];
  }
  cout << a[G];
}

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

int N, G, i, j;
long long p[105], w[105], a[100005], b[100005];
int main() {
  // ifstream cin("rucsac.in");
  cin >> N >> G;
  for (i = 1; i <= N; i++) {
    cin >> w[i] >> p[i];
  }
  for (i = 1; i <= N; ++i) {
    for (j = 1; j <= G; ++j) {
      if (w[i] > j)
        a[j] = b[j];
      else {
        a[j] = max(b[j], b[j - w[i]] + p[i]);
      }
    }
    for (j = 1; j <= G; ++j)
      b[j] = a[j];
  }
  cout << a[G];
}