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

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

using namespace std;

long long int num, w;
int wat[100000 + 5];
int val[100000 + 5];
long long dp[1000][1000];

long long knapsack(long long wt, long long n) {
  long long &ret = dp[wt][n];
  if (wt == 0 || n == 0)
    return 0;

  if (ret != -1)
    return ret;
  ret = 0;

  if (wat[n - 1] > wt)
    ret = knapsack(wt, n - 1);

  else {
    ret = max(ret, max((val[n - 1] + knapsack(wt - wat[n - 1], n - 1)),
                       knapsack(wt, n - 1)));
  }
  return ret;
}

int main() {
  cin >> num >> w;
  for (int i = 0; i < num; i++) {
    scanf("%lld %lld", &wat[i], &val[i]);
  }
  memset(dp, -1, sizeof dp);
  long long ans = knapsack(w, num);
  cout << ans << endl;
}


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

using namespace std;

long long int num, w;
int wat[100000 + 5];
int val[100000 + 5];
long long dp[100010][105];

long long knapsack(long long wt, long long n) {
  long long &ret = dp[wt][n];
  if (wt == 0 || n == 0)
    return 0;

  if (ret != -1)
    return ret;
  ret = 0;

  if (wat[n - 1] > wt)
    ret = knapsack(wt, n - 1);

  else {
    ret = max(ret, max((val[n - 1] + knapsack(wt - wat[n - 1], n - 1)),
                       knapsack(wt, n - 1)));
  }
  return ret;
}

int main() {
  cin >> num >> w;
  for (int i = 0; i < num; i++) {
    scanf("%lld %lld", &wat[i], &val[i]);
  }
  memset(dp, -1, sizeof dp);
  long long ans = knapsack(w, num);
  cout << ans << endl;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
// 解説AC
int main() {
  ll n, w;
  cin >> n >> w;
  vector<vector<ll>> item(n + 1, vector<ll>(2, 0));
  vector<vector<ll>> dp(n + 1, vector<ll>(w, 0));

  for (size_t i = 1; i <= n; i++) //[i][0]が重さ [i][1]が価値
  {
    cin >> item[i][0] >> item[i][1];
  }

  for (size_t i = 1; i <= n; i++) // 1-indexed
  {
    // 品物iを選んだとして、選んだ方が価値が大きくなるならdp[i][w]を更新
    for (size_t j = 0; j <= w; j++) {
      if (j >= item[i][0]) {
        ll weight = item[i][0];
        ll value = item[i][1];
        dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight] + value);
      } else {
        dp[i][j] = max(dp[i][j], dp[i - 1][j]);
      }
    }
    // 今までと、そのアイテムの重さ
  }

  cout << dp[n][w] << endl;
  /*
  rep(i,n+1)
  { cout<<"dp["<<i<<"] : ";
    rep(j,w+1)
    {
      cout << dp[i][j] <<" ";

    }
    cout<<endl;
  }*/
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
// 解説AC
int main() {
  ll n, w;
  cin >> n >> w;
  vector<vector<ll>> item(n + 1, vector<ll>(2, 0));
  vector<vector<ll>> dp(n + 1, vector<ll>(w + 1, 0));

  for (size_t i = 1; i <= n; i++) //[i][0]が重さ [i][1]が価値
  {
    cin >> item[i][0] >> item[i][1];
  }

  for (size_t i = 1; i <= n; i++) // 1-indexed
  {
    // 品物iを選んだとして、選んだ方が価値が大きくなるならdp[i][w]を更新
    for (size_t j = 0; j <= w; j++) {
      if (j >= item[i][0]) {
        ll weight = item[i][0];
        ll value = item[i][1];
        dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight] + value);
      } else {
        dp[i][j] = max(dp[i][j], dp[i - 1][j]);
      }
    }
    // 今までと、そのアイテムの重さ
  }

  cout << dp[n][w] << endl;
  /*
  rep(i,n+1)
  { cout<<"dp["<<i<<"] : ";
    rep(j,w+1)
    {
      cout << dp[i][j] <<" ";

    }
    cout<<endl;
  }*/
  return 0;
}
Problem p03163 (C++)

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


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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int w[105], val[105];
long long dp[10005];
int main() {
  int t, m, res = -1;
  scanf("%d%d", &m, &t);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &w[i], &val[i]);
  }
  for (int i = 1; i <= m; i++) {
    for (int j = t; j >= 0; j--) {
      if (j >= w[i]) {
        dp[j] = max(dp[j - w[i]] + val[i], dp[j]);
      }
    }
  }
  printf("%lld\n", dp[t]);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int w[105], val[105];
long long dp[100005];
int main() {
  int t, m, res = -1;
  scanf("%d%d", &m, &t);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &w[i], &val[i]);
  }
  for (int i = 1; i <= m; i++) {
    for (int j = t; j >= 0; j--) {
      if (j >= w[i]) {
        dp[j] = max(dp[j - w[i]] + val[i], dp[j]);
      }
    }
  }
  printf("%lld\n", dp[t]);
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
// #define ll long long
long long int dp[10005][10005];
// int val[100005];
// int wt[100005];
long long int fun(long long int w, long long int n, long long int val[],
                  long long int wt[]) {
  if (w == 0 || n == 0)
    return 0;

  if (wt[n - 1] > w)
    return dp[w][n] = fun(w, n - 1, val, wt);
  if (dp[w][n] != -1)
    return dp[w][n];

  return dp[w][n] = max(fun(w - wt[n - 1], n - 1, val, wt) + val[n - 1],
                        fun(w, n - 1, val, wt));
}

int main() {

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
// #define ll long long
long long int dp[100005][105];
// int val[100005];
// int wt[100005];
long long int fun(long long int w, long long int n, long long int val[],
                  long long int wt[]) {
  if (w == 0 || n == 0)
    return 0;

  if (wt[n - 1] > w)
    return dp[w][n] = fun(w, n - 1, val, wt);
  if (dp[w][n] != -1)
    return dp[w][n];

  return dp[w][n] = max(fun(w - wt[n - 1], n - 1, val, wt) + val[n - 1],
                        fun(w, n - 1, val, wt));
}

int main() {

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

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

typedef long long int ll;
ll n, w, dp[107];

int main() {
  cin >> n >> w;
  for (ll i = 0; i < n; ++i) {
    ll weight, value;
    cin >> weight >> value;

    for (ll j = w - weight; j >= 0; --j) {
      dp[j + weight] = max(dp[j + weight], dp[j] + value);
    }
  }

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

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

typedef long long int ll;
ll n, w, dp[100007];

int main() {
  cin >> n >> w;
  for (ll i = 0; i < n; ++i) {
    ll weight, value;
    cin >> weight >> value;

    for (ll 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: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define MOD int(1e9 + 9)
#define PI 3.1415926535898
using namespace std;
int main(int argc, char const *argv[]) {
  ll dp[1001][100001], val[101], w[101], n, W;
  cin >> n >> W;
  for (int i = 0; i < n; i++) {
    cin >> w[i] >> val[i];
  }
  for (int i = 0; i <= n; i++)
    dp[i][0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= W; j++) {
      if (i == 0 && j == 0)
        dp[i][j] = 0;
      if (j < w[i - 1]) {
        dp[i][j] = dp[i - 1][j];
      } else {
        dp[i][j] = max(dp[i - 1][j], val[i - 1] + dp[i - 1][j - w[i - 1]]);
      }
    }
  }
  cout << dp[n][W] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define MOD int(1e9 + 9)
#define PI 3.1415926535898
using namespace std;
int main(int argc, char const *argv[]) {
  ll dp[101][100001], val[101], w[101], n, W;
  cin >> n >> W;
  for (int i = 0; i < n; i++) {
    cin >> w[i] >> val[i];
  }
  for (int i = 0; i <= n; i++)
    dp[i][0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= W; j++) {
      if (i == 0 && j == 0)
        dp[i][j] = 0;
      if (j < w[i - 1]) {
        dp[i][j] = dp[i - 1][j];
      } else {
        dp[i][j] = max(dp[i - 1][j], val[i - 1] + dp[i - 1][j - w[i - 1]]);
      }
    }
  }
  cout << dp[n][W] << endl;
}
Problem p03163 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <iostream>
#include <memory>
#include <numeric>

#include <array>
#include <deque>
#include <map>
#include <set>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <chrono>
#include <ostream>
#include <type_traits>
#include <utility>

#ifdef ONLINE_JUDGE
#pragma GCC optimize("O3")
#pragma GCC target("avx,avx2")
#endif

using namespace std;

using u64 = uint64_t;
using i64 = int64_t;
using u32 = uint32_t;
using vi = vector<int>;
using vi64 = vector<i64>;

#define seq(i, a, b) for (int i = (a); i < (b); ++i)
#define rev(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define sz(x) (x.size())
#define all(x) x.begin(), x.end()
#define dbg(x)                                                                 \
  { cerr << #x << ": " << x << endl; }

int main(int argc, char *argv[]) {
  ios::sync_with_stdio(false);
  cin.tie(NULL);

  int n, maxw;
  cin >> n >> maxw;
  vi64 dp(maxw + 1);
  dp[0] = 0;

  seq(_, 1, maxw + 1) {
    int w, v;
    cin >> w >> v;
    rev(i, 0, maxw - w + 1) { dp[i + w] = max(dp[i] + v, dp[i + w]); }
  }
  cout << dp[maxw] << endl;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <iostream>
#include <memory>
#include <numeric>

#include <array>
#include <deque>
#include <map>
#include <set>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <chrono>
#include <ostream>
#include <type_traits>
#include <utility>

#ifdef ONLINE_JUDGE
#pragma GCC optimize("O3")
#pragma GCC target("avx,avx2")
#endif

using namespace std;

using u64 = uint64_t;
using i64 = int64_t;
using u32 = uint32_t;
using vi = vector<int>;
using vi64 = vector<i64>;

#define seq(i, a, b) for (int i = (a); i < (b); ++i)
#define rev(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define sz(x) (x.size())
#define all(x) x.begin(), x.end()
#define dbg(x)                                                                 \
  { cerr << #x << ": " << x << endl; }

int main(int argc, char *argv[]) {
  ios::sync_with_stdio(false);
  cin.tie(NULL);

  int n, maxw;
  cin >> n >> maxw;
  vi64 dp(maxw + 1);
  dp[0] = 0;

  seq(_, 0, n) {
    int w, v;
    cin >> w >> v;
    rev(i, 0, maxw - w + 1) { dp[i + w] = max(dp[i] + v, dp[i + w]); }
  }
  cout << dp[maxw] << endl;
}
Problem p03163 (C++)

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

using namespace std;

const int maxn = 1e5, maxw = 100;
int n, W;
int w[maxn];
int v[maxn];
int dp[maxw][maxn];

int32_t main() {
  scanf("%lld%lld", &n, &W);

  for (int i = 0; i < n; i++) {
    scanf("%lld%lld", &w[i], &v[i]);
  }

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

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

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


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

using namespace std;

const int maxn = 100 + 10, maxw = 1e5 + 10;
int n, W;
int w[maxn];
int v[maxn];
int dp[maxw][maxn];

int32_t main() {
  scanf("%lld%lld", &n, &W);

  for (int i = 0; i < n; i++) {
    scanf("%lld%lld", &w[i], &v[i]);
  }

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

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

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

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

using namespace std;

#define int long long
#define endl "\n"

void rwf() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

const int N = 105, mxW = 1e5 + 5;
int n, W, w[N], v[N];

int dp[N][mxW];

int solve(int wr, int m) {
  if (m == 0 || wr == 0)
    return 0;

  if (dp[m][wr] != -1)
    return dp[m][wr];

  if (wr - w[m] < 0)
    return dp[m][wr] = solve(wr, m - 1);

  return dp[m][wr] = max(solve(wr, m - 1), v[m] + solve(wr - w[m], m - 1));
}

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

  rwf();

  cin >> n >> W;

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

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

  int ans = solve(W, n);

  cout << ans;

  return 0;
}

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

using namespace std;

#define int long long
#define endl "\n"

void rwf() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

const int N = 105, mxW = 1e5 + 5;
int n, W, w[N], v[N];

int dp[N][mxW];

int solve(int wr, int m) {
  if (m == 0 || wr == 0)
    return 0;

  if (dp[m][wr] != -1)
    return dp[m][wr];

  if (wr - w[m] < 0)
    return dp[m][wr] = solve(wr, m - 1);

  return dp[m][wr] = max(solve(wr, m - 1), v[m] + solve(wr - w[m], m - 1));
}

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

  // rwf();

  cin >> n >> W;

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

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

  int ans = solve(W, n);

  cout << ans;

  return 0;
}
Problem p03163 (C++)

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

using namespace std;
int n;
int peso[1000];
long long int valor[1000];
long long int dp[100][100001];
int calculado[100][100001];
long long int solve(int ind, int aguenta) {
  if (ind == n && aguenta >= 0) {
    return 0LL;
  }
  /*if(calculado[ind][aguenta] == true){
          return dp[ind][aguenta];
  }*/
  long long int ans = solve(ind + 1, aguenta);
  if (peso[ind] <= aguenta) {
    ans = max(ans, valor[ind] + solve(ind + 1, aguenta - peso[ind]));
  }
  calculado[ind][aguenta] = true;
  dp[ind][aguenta] = ans;
  return ans;
}
int main() {
  int k;
  cin >> n >> k;
  memset(calculado, 0, sizeof calculado);
  for (int i = 0; i < n; i++) {
    cin >> peso[i] >> valor[i];
  }
  cout << solve(0, k) << endl;
  return 0;
}


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

using namespace std;
int n;
int peso[1000];
long long int valor[1000];
long long int dp[100][100001];
int calculado[100][100001];
long long int solve(int ind, int aguenta) {
  if (ind == n && aguenta >= 0) {
    return 0LL;
  }
  if (calculado[ind][aguenta] == true) {
    return dp[ind][aguenta];
  }
  long long int ans = solve(ind + 1, aguenta);
  if (peso[ind] <= aguenta) {
    ans = max(ans, valor[ind] + solve(ind + 1, aguenta - peso[ind]));
  }
  calculado[ind][aguenta] = true;
  dp[ind][aguenta] = ans;
  return ans;
}
int main() {
  int k;
  cin >> n >> k;
  memset(calculado, 0, sizeof calculado);
  for (int i = 0; i < n; i++) {
    cin >> peso[i] >> valor[i];
  }
  cout << solve(0, k) << endl;
  return 0;
}
Problem p03163 (C++)

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

#define LOCAL 1
#define UPLOAD 2

#define ll long long
const long long INF = 1LL << 60;

template <class T = long long> T input() {
  T x;
  cin >> x;
  return x;
}
template <class T> void output(T x) { cout << x << endl; }
#define debug(x) cerr << #x << ": " << x << '\n';
#define debugBit(x, n) cerr << #x << ": " << std::bitset<n>(x) << '\n';

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;
}
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }

const long long mod = 1e9 + 7;

int main() {
#if ENVIRONMENT == LOCAL
  std::ifstream in("input.txt");
  std::cin.rdbuf(in.rdbuf());
#endif

  ll N = input();
  ll W = input();
  std::vector<std::pair<ll, ll>> values;
  for (int i = 0; i < N; i++) {
    values.push_back({input(), input()});
  }

  vector<vector<ll>> dp(N + 1, vector<ll>(W));

  for (ll i = 0; i < N; i++) {
    for (ll w = 0; w <= W; w++) {
      if (values[i].first <= w) {
        chmax(dp[i + 1][w], dp[i][w - values[i].first] + values[i].second);
      }
      chmax(dp[i + 1][w], dp[i][w]);
    }
  }

  output(dp[N][W]);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

#define LOCAL 1
#define UPLOAD 2

#define ll long long
const long long INF = 1LL << 60;

template <class T = long long> T input() {
  T x;
  cin >> x;
  return x;
}
template <class T> void output(T x) { cout << x << endl; }
#define debug(x) cerr << #x << ": " << x << '\n';
#define debugBit(x, n) cerr << #x << ": " << std::bitset<n>(x) << '\n';

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;
}
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }

const long long mod = 1e9 + 7;

int main() {
#if ENVIRONMENT == LOCAL
  std::ifstream in("input.txt");
  std::cin.rdbuf(in.rdbuf());
#endif

  ll N = input();
  ll W = input();
  std::vector<std::pair<ll, ll>> values;
  for (int i = 0; i < N; i++) {
    values.push_back({input(), input()});
  }

  vector<vector<ll>> dp(N + 1, vector<ll>(W + 1));

  for (ll i = 0; i < N; i++) {
    for (ll w = 0; w <= W; w++) {
      if (values[i].first <= w) {
        chmax(dp[i + 1][w], dp[i][w - values[i].first] + values[i].second);
      }
      chmax(dp[i + 1][w], dp[i][w]);
    }
  }

  output(dp[N][W]);

  return 0;
}
Problem p03163 (C++)

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

using namespace std;
struct Item {
  long long val;
  int wgt;
};
long long dp[100][100002];
Item *obj;

long long knapsck(int n, int w) {
  if (n == 0 || w < 0)
    return 0ll;
  if (dp[n][w] != -1)
    return dp[n][w];
  long long a = -1;
  if (w - obj[n - 1].wgt >= 0)
    a = knapsck(n - 1, w - obj[n - 1].wgt) + obj[n - 1].val;
  long long b = knapsck(n - 1, w);
  return dp[n][w] = max(a, b);
}

int main() {
  int n, w;
  cin >> n >> w;
  memset(dp, -1, sizeof(dp));
  obj = new Item[n];
  for (int i = 0; i < n; i++)
    cin >> obj[i].wgt >> obj[i].val;

  cout << knapsck(n, w);

  return 0;
}

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

using namespace std;
struct Item {
  long long val;
  int wgt;
};
long long dp[101][100002];
Item *obj;

long long knapsck(int n, int w) {
  if (n == 0 || w < 0)
    return 0ll;
  if (dp[n][w] != -1)
    return dp[n][w];
  long long a = -1;
  if (w - obj[n - 1].wgt >= 0)
    a = knapsck(n - 1, w - obj[n - 1].wgt) + obj[n - 1].val;
  long long b = knapsck(n - 1, w);
  return dp[n][w] = max(a, b);
}

int main() {
  int n, w;
  cin >> n >> w;
  memset(dp, -1, sizeof(dp));
  obj = new Item[n];
  for (int i = 0; i < n; i++)
    cin >> obj[i].wgt >> obj[i].val;

  cout << knapsck(n, w);

  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define MAXN 10005
#define INF 0x3f3f3f3f
#define rint register int
#define LL long long
#define LD long double
using namespace std;

int n, k, w[MAXN], v[MAXN];
LL ans, f[MAXN];

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

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define MAXN 100005
#define INF 0x3f3f3f3f
#define rint register int
#define LL long long
#define LD long double
using namespace std;

int n, k, w[MAXN], v[MAXN];
LL ans, f[MAXN];

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define loop(i, a, b) for (int i = a; i < b; ++i)
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll N, W;
  cin >> N >> W;
  vector<ll> weights(N + 1), values(N + 1);
  for (int i = 1; i <= N; ++i)
    cin >> weights[i] >> values[i];
  vector<vector<ll>> dp(N + 1, vector<ll>(W + 1));
  for (int i = 0; i <= W; ++i)
    dp[0][i] = 0;
  for (int i = 1; i <= N; ++i) {
    for (int j = 1; i <= W; ++j) {
      if (weights[i] > j)
        dp[i][j] = dp[i - 1][j];
      else
        dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weights[i]] + values[i]);
    }
  }
  cout << dp[N][W] << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define loop(i, a, b) for (int i = a; i < b; ++i)
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll N, W;
  cin >> N >> W;
  vector<ll> weights(N + 1), values(N + 1);
  for (int i = 1; i <= N; ++i)
    cin >> weights[i] >> values[i];
  vector<vector<ll>> dp(N + 1, vector<ll>(W + 1));
  for (int i = 0; i <= W; ++i)
    dp[0][i] = 0;
  for (int i = 1; i <= N; ++i) {
    for (int j = 1; j <= W; ++j) {
      if (weights[i] > j)
        dp[i][j] = dp[i - 1][j];
      else
        dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weights[i]] + values[i]);
    }
  }
  cout << dp[N][W] << "\n";
  return 0;
}
Problem p03163 (C++)

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

typedef long long ll;
typedef double ld;
#define rep(i, a, n) for (ll i = (a); i < (n); i++)
#define per(i, a, n) for (ll i = (n - 1); i >= (a); i--)
#define F first
#define S second
#define maxx(a, b) a = max(a, b)
#define minn(a, b) a = min(a, b)
#define db(x) cerr << #x << " = " << x << endl
#define N 1111111
#define mod 1000000007
#define rt return
#define inf 12345678908761

using namespace std;

ll a[111111], w[111], v[111];

int main() {

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

  // freopen("Ulug'bek","r",stdin);
  // freopen("Abdimanabov","w",stdout);

  int n, W;
  cin >> n >> W;
  rep(i, 1, n + 1) cin >> w[i] >> v[i];

  memset(a + 1, -1, sizeof a);

  rep(i, 1, n + 1) per(j, w[i], W + 1) if (a[j - w[i]] != -1)
      maxx(a[j], a[j - w[i]] + v[i]);

  cout << *max_element(a, a + W + 1);

  rt 0;
}


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

typedef long long ll;
typedef double ld;
#define rep(i, a, n) for (ll i = (a); i < (n); i++)
#define per(i, a, n) for (ll i = (n - 1); i >= (a); i--)
#define F first
#define S second
#define maxx(a, b) a = max(a, b)
#define minn(a, b) a = min(a, b)
#define db(x) cerr << #x << " = " << x << endl
#define N 1111111
#define mod 1000000007
#define rt return
#define inf 12345678908761

using namespace std;

ll a[111111], w[111], v[111];

int main() {

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

  // freopen("Ulug'bek","r",stdin);
  // freopen("Abdimanabov","w",stdout);

  int n, W;
  cin >> n >> W;
  rep(i, 1, n + 1) cin >> w[i] >> v[i];

  memset(a, -1, sizeof a);
  a[0] = 0;

  rep(i, 1, n + 1) per(j, w[i], W + 1) if (a[j - w[i]] != -1)
      maxx(a[j], a[j - w[i]] + v[i]);

  cout << *max_element(a, a + W + 1);

  rt 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <cstdlib>
#include <iostream>
#include <limits>
using namespace std;
int imax = std::numeric_limits<int>::max();
long long dp[100][100002], i, j, n, k, w, vi[100], wi[100];
int main() {

  // cout<<"P:";
  cin >> n >> w;
  for (i = 1; i <= n; i++)
    cin >> wi[i] >> vi[i];
  for (i = 1; i <= n; i++) {
    for (j = 0; j <= w; j++) {
      dp[i][j] = dp[i - 1][j];
      if (j - wi[i] >= 0)
        dp[i][j] = max(dp[i][j], dp[i - 1][j - wi[i]] + vi[i]);
    }
  }
  /*
  for(i=1;i<=n;i++)
      {
          for(j=1;j<=w;j++)
              cout<<dp[i][j]<<" ";
          cout<<"\n";
      }
   */
  cout << dp[n][w];
  return 0;
}

Fixed submission:
#include <cstdlib>
#include <iostream>
#include <limits>
using namespace std;
int imax = std::numeric_limits<int>::max();
long long dp[101][100002], i, j, n, k, w, vi[101], wi[101];
int main() {

  // cout<<"P:";
  cin >> n >> w;
  for (i = 1; i <= n; i++)
    cin >> wi[i] >> vi[i];
  for (i = 1; i <= n; i++) {
    for (j = 0; j <= w; j++) {
      dp[i][j] = dp[i - 1][j];
      if (j - wi[i] >= 0)
        dp[i][j] = max(dp[i][j], dp[i - 1][j - wi[i]] + vi[i]);
    }
  }
  /*
  for(i=1;i<=n;i++)
      {
          for(j=1;j<=w;j++)
              cout<<dp[i][j]<<" ";
          cout<<"\n";
      }
   */
  cout << dp[n][w];
  return 0;
}
Problem p03163 (C++)

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

using namespace std;

// using Matrix = vector< vector<int> >;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9 + 10;
// const ll INF = 1e18 + 10LL;
int dx[5] = {0, 0, 1, -1, 0}, dy[5] = {1, -1, 0, 0, 0};
const double EPS = 1e-10;
bool cmp(P a, P b) {
  return a.second < b.second;
} // End sort (Interval scheduling problem)
ll MOD = 1e9 + 7;

int n;
ll W;
ll w[120], v[120];

ll dp[120][120];

ll rec(int i, int j) {
  if (dp[i][j] >= 0)
    return dp[i][j];

  ll res;
  if (i == n)
    res = 0;
  else if (j < w[i])
    res = rec(i + 1, j);
  else
    res = max(rec(i + 1, j), rec(i + 1, j - w[i]) + v[i]);

  return dp[i][j] = res;
}

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

  memset(dp, -1, sizeof(dp));
  printf("%lld\n", rec(0, W));

  return 0;
}

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

using namespace std;

// using Matrix = vector< vector<int> >;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9 + 10;
// const ll INF = 1e18 + 10LL;
int dx[5] = {0, 0, 1, -1, 0}, dy[5] = {1, -1, 0, 0, 0};
const double EPS = 1e-10;
bool cmp(P a, P b) {
  return a.second < b.second;
} // End sort (Interval scheduling problem)
ll MOD = 1e9 + 7;

int n;
ll W;
ll w[120], v[120];

ll dp[120][100010];

ll rec(int i, int j) {
  if (dp[i][j] >= 0)
    return dp[i][j];

  ll res;
  if (i == n)
    res = 0;
  else if (j < w[i])
    res = rec(i + 1, j);
  else
    res = max(rec(i + 1, j), rec(i + 1, j - w[i]) + v[i]);

  return dp[i][j] = res;
}

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

  memset(dp, -1, sizeof(dp));
  printf("%lld\n", rec(0, W));

  return 0;
}
Problem p03163 (C++)

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

using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

// using Matrix = vector< vector<int> >;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1 << 30;
int dx[5] = {0, 0, 1, -1, 0}, dy[5] = {1, -1, 0, 0, 0};
bool cmp(P a, P b) {
  return a.second < b.second;
} // End sort (Interval scheduling problem)

int n, W;
ll w[110], v[110];

ll dp[110][110];

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

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

  printf("%lld\n", dp[n][W]);

  return 0;
}

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

using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

// using Matrix = vector< vector<int> >;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1 << 30;
int dx[5] = {0, 0, 1, -1, 0}, dy[5] = {1, -1, 0, 0, 0};
bool cmp(P a, P b) {
  return a.second < b.second;
} // End sort (Interval scheduling problem)

int n, W;
ll w[110], v[110];

ll dp[110][100010];

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

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

  printf("%lld\n", dp[n][W]);

  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define f(i, x, n) for (int i = x; i < n; i++)
#define all(c) c.begin(), c.end()
#define int ll
#define print(x) cerr << (#x) << "is " << x << "\n"
using ll = long long;
using pii = pair<int, int>;
const int MOD = 1e9 + 7, N = 1e5 + 10;

int n, w, dp[110][N];
int a[110][2];

int dfs(int in, int we) {
  if (we < 0) {
    return INT_MIN;
  }
  if (we == 0)
    return 0;
  if (in == n) {
    if (we - a[n][0] >= 0) {
      return a[n][1];
    } else {
      return 0;
    }
  }
  int &ans = dp[in][we];
  if (ans != -1)
    return ans;
  ans = 0;
  return ans =
             max({ans, dfs(in + 1, we - a[in][0]) + a[in][1], dfs(in + 1, we)});
}

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  cin >> n >> w;
  f(i, 1, n + 1) { cin >> a[i][0] >> a[i][1]; }
  f(i, 1, n + 1) {
    f(j, 0, w + 10) { dp[i][j] = -1; }
  }
  cout << dfs(1, w) << '\n';
  // f(i,1,n+1){
  //    f(j,0,w+1){
  //       cout << i << ' ' << j << ' ' << dp[i][j] << '\n';
  //    }
  // }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define f(i, x, n) for (int i = x; i < n; i++)
#define all(c) c.begin(), c.end()
#define int ll
#define print(x) cerr << (#x) << "is " << x << "\n"
using ll = long long;
using pii = pair<int, int>;
const int MOD = 1e9 + 7, N = 1e5 + 10;

int n, w, dp[110][N];
int a[110][2];

int dfs(int in, int we) {
  if (we < 0) {
    return INT_MIN;
  }
  if (we == 0)
    return 0;
  if (in == n) {
    if (we - a[n][0] >= 0) {
      return a[n][1];
    } else {
      return 0;
    }
  }
  int &ans = dp[in][we];
  if (ans != -1)
    return ans;
  ans = 0;
  return ans =
             max({ans, dfs(in + 1, we - a[in][0]) + a[in][1], dfs(in + 1, we)});
}

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  // #ifndef ONLINE_JUDGE
  //    freopen("input.txt", "r", stdin);
  //    freopen("output.txt", "w", stdout);
  // #endif
  cin >> n >> w;
  f(i, 1, n + 1) { cin >> a[i][0] >> a[i][1]; }
  f(i, 1, n + 1) {
    f(j, 0, w + 10) { dp[i][j] = -1; }
  }
  cout << dfs(1, w) << '\n';
  // f(i,1,n+1){
  //    f(j,0,w+1){
  //       cout << i << ' ' << j << ' ' << dp[i][j] << '\n';
  //    }
  // }
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#define ll long long
using namespace std;
int main() {
  int n, c;
  cin >> n >> c;
  ll wt[103], v[103];
  for (int i = 0; i < n; i++) {
    cin >> wt[i] >> v[i];
  }
  ll dp[103][103];
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= c; j++) {
      if (i == 0 || j == 0) {
        dp[i][j] = 0;
      } else if (wt[i - 1] <= j) {
        dp[i][j] = max(v[i - 1] + dp[i - 1][j - wt[i - 1]], dp[i - 1][j]);
      } else {
        dp[i][j] = dp[i - 1][j];
      }
    }
  }
  cout << dp[n][c];
}


Fixed submission:
#include <iostream>
#define ll long long
using namespace std;
int main() {
  int n, c;
  cin >> n >> c;
  ll wt[103], v[103];
  for (int i = 0; i < n; i++) {
    cin >> wt[i] >> v[i];
  }
  ll dp[103][c + 1];
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= c; j++) {
      if (i == 0 || j == 0) {
        dp[i][j] = 0;
      } else if (wt[i - 1] <= j) {
        dp[i][j] = max(v[i - 1] + dp[i - 1][j - wt[i - 1]], dp[i - 1][j]);
      } else {
        dp[i][j] = dp[i - 1][j];
      }
    }
  }
  cout << dp[n][c];
}
Problem p03163 (C++)

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

int w[100], v[100];
int n, W;
long long dp[100][100001];

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

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

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

int w[100], v[100];
int n, W;
long long dp[101][100001];

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

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

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

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

  for (int i = 0; i <= W; i++)
    for (int j = 0; j <= n; j++) {
      dp[j][i] = -1;
      if (i == 0 || j == 0)
        dp[j][i] = 0;
    }

  for (int i = 1; i <= n; i++) {
    for (int w1 = 1; w1 <= W; w1++) {
      dp[i][w1] = dp[i - 1][w1];
      if (w[i] <= w1)
        dp[i][w1] = max(dp[i][w1], dp[i - 1][w1 - w[i]] + v[i]);
    }
  }
  cout << dp[n][W] << '\n';
}

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

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

  for (int i = 0; i <= W; i++)
    for (int j = 0; j <= n; j++) {
      dp[j][i] = -1;
      if (i == 0 || j == 0)
        dp[j][i] = 0;
    }

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

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

using namespace std;

const int INF = 0x3f3f3f3f;
const int MAXN = 1e2 + 5;
const int MAXW = 1e5 + 5;

int n, capacidade;
int p[MAXW], v[MAXW];
int memo[MAXN][MAXW];

int solve(int i, int peso) {
  if (memo[peso][i] != -1)
    return memo[peso][i];

  if (i > n || peso == 0)
    return 0;

  int pegar = 0, nao_pegar = 0;

  if (p[i] <= peso) {
    pegar = v[i] + solve(i + 1, peso - p[i]);
  }
  nao_pegar = solve(i + 1, peso);

  return memo[peso][i] = max(pegar, nao_pegar);
}

int32_t main() {
  cin >> n >> capacidade;

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

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

  cout << solve(1, capacidade);
}

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

using namespace std;

const int INF = 0x3f3f3f3f;
const int MAXN = 1e2 + 5;
const int MAXW = 1e5 + 5;

int n, capacidade;
int p[MAXW], v[MAXW];
int memo[MAXW][MAXN];

int solve(int i, int peso) {
  if (memo[peso][i] != -1)
    return memo[peso][i];

  if (i > n || peso == 0)
    return 0;

  int pegar = 0, nao_pegar = 0;

  if (p[i] <= peso) {
    pegar = v[i] + solve(i + 1, peso - p[i]);
  }
  nao_pegar = solve(i + 1, peso);

  return memo[peso][i] = max(pegar, nao_pegar);
}

int32_t main() {
  cin >> n >> capacidade;

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

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

  cout << solve(1, capacidade);
}
Problem p03163 (C++)

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

int dp[105][10010];

int main() {

  int n, W;
  int v[105];
  int w[105];

  cin >> n >> W;
  rep(i, n) cin >> w[i] >> v[i];

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

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

  return 0;
}

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

ll dp[105][100100];

int main() {

  int n, W;
  int v[105];
  int w[105];

  cin >> n >> W;
  rep(i, n) cin >> w[i] >> v[i];

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

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

  return 0;
}
Problem p03163 (C++)

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

using namespace std;
long long tab[1008];
pair<long long, long long> p[101];

#define f first
#define s second

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

  long long a, b, n, w;
  cin >> n >> w;

  for (int i = 0; i < n; i++) {
    cin >> p[i].f >> p[i].s;
  }

  for (int i = 0; i < n; i++) {
    for (int j = w + 3; j >= 0; j--) {
      if (j + p[i].f <= w && tab[j] != 0) {
        tab[j + p[i].f] = max(tab[j + p[i].f], tab[j] + p[i].s);
      }
    }
    tab[p[i].f] = max(tab[p[i].f], p[i].s);
  }

  long long wynik = 0;

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

  cout << wynik;

  return 0;
}


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

using namespace std;
long long tab[100008];
pair<long long, long long> p[101];

#define f first
#define s second

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

  long long a, b, n, w;
  cin >> n >> w;

  for (int i = 0; i < n; i++) {
    cin >> p[i].f >> p[i].s;
  }

  for (int i = 0; i < n; i++) {
    for (int j = w + 3; j >= 0; j--) {
      if (j + p[i].f <= w && tab[j] != 0) {
        tab[j + p[i].f] = max(tab[j + p[i].f], tab[j] + p[i].s);
      }
    }
    tab[p[i].f] = max(tab[p[i].f], p[i].s);
  }

  long long wynik = 0;

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

  cout << wynik;

  return 0;
}
Problem p03163 (C++)

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


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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
const ll mod = 1e9 + 7;
const int maxn = 2e5 + 50;
using namespace std;
int main()
// int found; found=chaine.find(cara); if (found!=std::string::npos) how to
// verify if a caracter exists in a chaine
//  toupper(caractere) ==> return the majuscule of a caractere
// floor(x) Returns the largest integer smaller than x
// ceil(x) returns  Returns the smallest integer greater
{

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  int n, w;
  cin >> n >> w;
  int weight[n + 1];
  ll value[n + 1];
  for (int i = 0; i < n; i++) {
    cin >> weight[i + 1] >> value[i + 1];
  }
  ll mat[n + 1][w + 1];
  for (int av = 0; av <= w; av++) {
    if (av < weight[1]) {
      mat[1][av] = 0;
    } else {
      mat[1][av] = value[1];
    }
  }

  for (int i = 2; i <= n; i++) {
    for (int av = 0; av <= w; av++) {
      if (weight[i] > av) {
        mat[i][av] = mat[i - 1][av];

      } else {
        mat[i][av] = max(mat[i - 1][av - weight[i]] + value[i], mat[i - 1][av]);
      }
    }
  }

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


Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
const ll mod = 1e9 + 7;
const int maxn = 2e5 + 50;
using namespace std;
int main()
// int found; found=chaine.find(cara); if (found!=std::string::npos) how to
// verify if a caracter exists in a chaine
//  toupper(caractere) ==> return the majuscule of a caractere
// floor(x) Returns the largest integer smaller than x
// ceil(x) returns  Returns the smallest integer greater
{
  int n, w;
  cin >> n >> w;
  int weight[n + 1];
  ll value[n + 1];
  for (int i = 0; i < n; i++) {
    cin >> weight[i + 1] >> value[i + 1];
  }
  ll mat[n + 1][w + 1];
  for (int av = 0; av <= w; av++) {
    if (av < weight[1]) {
      mat[1][av] = 0;
    } else {
      mat[1][av] = value[1];
    }
  }

  for (int i = 2; i <= n; i++) {
    for (int av = 0; av <= w; av++) {
      if (weight[i] > av) {
        mat[i][av] = mat[i - 1][av];

      } else {
        mat[i][av] = max(mat[i - 1][av - weight[i]] + value[i], mat[i - 1][av]);
      }
    }
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define RREP(i, n) for (int i = 0; i > (int)(n); i--)
#define ALL(v) v.begin(), v.end()
#define INF 2e9
typedef long long ll;
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 w[101], v[101];
ll dp[101][101010];

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

  REP(i, W + 1) {
    if (i < w[i]) {
      dp[0][i] = 0;
    } else {
      dp[0][i] = v[0];
    }
  }
  REP(i, N - 1) {
    REP(k, W + 1) { dp[i + 1][k] = dp[i][k]; }
    REP(k, W) {
      if (k + w[i + 1] > W) {
        break;
      }
      chmax(dp[i + 1][k + w[i + 1]], dp[i][k] + v[i + 1]);
    }
  }

  cout << dp[N - 1][W] << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define RREP(i, n) for (int i = 0; i > (int)(n); i--)
#define ALL(v) v.begin(), v.end()
#define INF 2e9
typedef long long ll;
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 w[101], v[101];
ll dp[101][101010];

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

  REP(i, W + 1) {
    if (i < w[0]) {
      dp[0][i] = 0;
    } else {
      dp[0][i] = v[0];
    }
  }
  REP(i, N - 1) {
    REP(k, W + 1) { dp[i + 1][k] = dp[i][k]; }
    REP(k, W) {
      if (k + w[i + 1] > W) {
        break;
      }
      chmax(dp[i + 1][k + w[i + 1]], dp[i][k] + v[i + 1]);
    }
  }

  cout << dp[N - 1][W] << "\n";
  return 0;
}
Problem p03163 (C++)

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

using namespace std;

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

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

  vector<int64_t> dp(W + 1, 0);

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

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

  return 0;
}

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

using namespace std;

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

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

  vector<int64_t> dp(W + 1, 0);

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

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

  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int MAXN = 1e3 + 5;

void wynik(ll &a, ll b) { a = max(a, b); }

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

  int n, w;
  cin >> n >> w;
  ll dp[MAXN];
  for (int i = 0; i < n; i++) {
    int waga, koszt;
    cin >> waga >> koszt;
    for (int j = w - waga; j >= 0; j--)
      wynik(dp[j + waga], dp[j] + koszt);
  }
  ll R = 0;
  for (int i = 0; i <= w; i++)
    wynik(R, dp[i]);
  cout << R;
}


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

void wynik(ll &a, ll b) { a = max(a, b); }

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

  int n, w;
  cin >> n >> w;
  ll dp[MAXN];
  for (int i = 0; i < n; i++) {
    int waga, koszt;
    cin >> waga >> koszt;
    for (int j = w - waga; j >= 0; j--)
      wynik(dp[j + waga], dp[j] + koszt);
  }
  ll R = 0;
  for (int i = 0; i <= w; i++)
    wynik(R, dp[i]);
  cout << R;
}
Problem p03163 (C++)

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

ll wei[105];
ll val[105];
ll dp[105][105] = {0};

ll knapsack1(ll w, ll i) {
  if (i < 0)
    return 0;

  if (w - wei[i] < 0)
    return knapsack1(w, i - 1);
  if (dp[w][i] != 0)
    return dp[w][i];

  dp[w][i] = max(knapsack1(w, i - 1), knapsack1(w - wei[i], i - 1) + val[i]);
  return dp[w][i];
}

int main() {
  ll a, b, c, k, d, e, f, n, mx, t;
  cin >> n >> mx;
  for (int i = 0; i < n; i++) {
    cin >> wei[i] >> val[i];
  }
  a = knapsack1(mx, n - 1);
  cout << a;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
const ll INF = (ll)(1e18);

ll wei[105];
ll val[105];
ll dp[100005][105] = {0};

ll knapsack1(ll w, ll i) {
  if (i < 0)
    return 0;

  if (w - wei[i] < 0)
    return knapsack1(w, i - 1);
  if (dp[w][i] != 0)
    return dp[w][i];

  dp[w][i] = max(knapsack1(w, i - 1), knapsack1(w - wei[i], i - 1) + val[i]);
  return dp[w][i];
}

int main() {
  ll a, b, c, k, d, e, f, n, mx, t;
  cin >> n >> mx;
  for (int i = 0; i < n; i++) {
    cin >> wei[i] >> val[i];
  }
  a = knapsack1(mx, n - 1);
  cout << a;

  return 0;
}