alexjercan/bugnet huggingface dataset:
Problem p02319 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <array>
#include <iostream>
#include <limits>

using namespace std;

struct Item {
  int value;
  int weight;
};

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

  array<int, 10001> value_to_weight;
  value_to_weight.fill(numeric_limits<int>::max());
  value_to_weight[0] = 0;

  for (int i = 0; i < n; ++i) {
    int value, weight;
    cin >> value >> weight;
    for (int total_value = 10000; total_value >= value; --total_value) {
      if (value_to_weight[total_value - value] == numeric_limits<int>::max())
        continue;
      value_to_weight[total_value] =
          min(value_to_weight[total_value],
              weight + value_to_weight[total_value - value]);
    }
  }
  int max_value = 0;

  for (int total_value = 0; total_value <= 10000; ++total_value) {
    if (value_to_weight[total_value] <= w && total_value > max_value)
      max_value = total_value;
  }
  cout << max_value << endl;
}


Error: MLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

struct Item {
  int value;
  int weight;
};

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

  vector<vector<int>> table(2, vector<int>(w + 1, 0));

  for (int i = 0; i < n; ++i) {
    int value, weight;
    cin >> value >> weight;
    Item item({value, weight});
    for (int cp = 1; cp <= w; ++cp) {
      table[1][cp] = table[0][cp];
      if (item.weight <= cp) {
        if (table[1][cp] < (item.value + table[0][cp - item.weight])) {
          table[1][cp] = item.value + table[0][cp - item.weight];
        }
      }
    }
    table[0].swap(table[1]);
  }
  cout << table[0][w] << endl;
}
Problem p02319 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define all(a) a.begin(), a.end()
typedef long long ll;

using P = pair<ll, ll>;
#define v first
#define w second

// v/w 高い順
bool comp1(P a, P b) { return a.v * b.w > b.v * a.w; }

// v高い順（vが同じならw軽い方優先）
bool comp(P a, P b) { return a.v == b.v ? a.w < b.w : a.v > b.v; }

int main(void) {
  ll N, W;
  cin >> N >> W;
  vector<P> a(N);
  rep(i, N) {
    ll v, w;
    cin >> v >> w;
    a[i] = {v, w};
  }
  sort(all(a), comp1);
  vector<P> dp = {{0, 0}};

  rep(i, N) {
    vector<P> tmp;
    for (auto &s : dp) {
      tmp.push_back(s);
      tmp.push_back({s.v + a[i].v, s.w + a[i].w});
    }

    sort(all(tmp), comp);
    dp.clear();
    ll w_border = W + 1;
    for (auto &s : tmp) {
      if (w_border > s.w) {
        w_border = s.w;
        dp.push_back(s);
      }
    }
    assert(dp.size() <= N * N * 2);
  }
  cout << dp[0].v << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define all(a) a.begin(), a.end()
typedef long long ll;

using P = pair<ll, ll>;
#define v first
#define w second

// v/w 高い順
bool comp1(P a, P b) { return a.v * b.w > b.v * a.w; }

// v高い順（vが同じならw軽い方優先）
bool comp(P a, P b) { return a.v == b.v ? a.w < b.w : a.v > b.v; }

int main(void) {
  ll N, W;
  cin >> N >> W;
  vector<P> a(N);
  rep(i, N) {
    ll v, w;
    cin >> v >> w;
    a[i] = {v, w};
  }
  sort(all(a), comp1);
  vector<P> dp = {{0, 0}};

  rep(i, N) {
    vector<P> tmp;
    for (auto &s : dp) {
      tmp.push_back(s);
      tmp.push_back({s.v + a[i].v, s.w + a[i].w});
    }

    sort(all(tmp), comp);
    dp.clear();
    ll w_border = W + 1;
    for (auto &s : tmp) {
      if (w_border > s.w) {
        w_border = s.w;
        dp.push_back(s);
      }
    }
    assert(dp.size() <= N * N);
  }
  cout << dp[0].v << endl;
  return 0;
}
Problem p02319 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
typedef long long ll;
#define INF 100000000000000
using namespace std;

int main() {
  ll a[10001];
  a[0] = 0;
  int b, c;
  scanf("%d%d", &b, &c);
  for (int d = 1; d <= b * 100; d++) {
    a[d] = INF;
  }
  for (int e = 0; e < b; e++) {
    int f, g;
    scanf("%d%d", &f, &g);
    for (int h = b * 100; h >= f; h--) {
      a[h] = min(a[h], a[h - f] + g);
    }
  }
  int s = 0;
  for (int i = 0; i <= b * 100; i++) {
    if (a[i] <= c)
      s = i;
  }
  printf("%d\\n", s);
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

int main() {
  int a, b;
  scanf("%d%d", &a, &b);
  int dp[20001];
  for (int d = 0; d < 20001; d++) {
    dp[d] = 1 << 29;
  }
  dp[0] = 0;
  for (int e = 0; e < a; e++) {
    int f, g;
    scanf("%d%d", &f, &g);
    for (int h = b * 101; h >= f; h--) {
      dp[h] = min(dp[h], dp[h - f] + g);
    }
  }
  int x = 0;
  for (int s = 0; s < a * 101; s++) {
    if (dp[s] != 1 << 29 && dp[s] <= b)
      x = s;
  }
  cout << x << endl;
}
Problem p02319 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
#define INF 2000000000
#define J 10000
#define MIN(a, b) ((a > b) ? (b) : (a))
using namespace std;

int main(void) {
  int N, W, dp[101][J + 1], v[101], w[101];
  cin >> N >> W;
  for (int i = 1; i <= N; i++)
    cin >> v[i] >> w[i];
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j <= J; j++)
      dp[i][j] = INF;
  }
  for (int i = 0; i <= N; i++)
    dp[i][0] = 0;
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= J; j++) {
      if (j - v[i] >= 0 && dp[i - 1][j - v[i]] + w[i] <= W)
        dp[i][j] = MIN(dp[i - 1][j], dp[i - 1][j - v[i]] + w[i]);
      else
        dp[i][j] = dp[i - 1][j];
    }
  }
  for (int j = J; j >= 0; j--) {
    if (dp[N][j] != INF && dp[N][j] <= W) {
      cout << j << endl;
      break;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#define MAX(a, b) ((a > b) ? (a) : (b))
using namespace std;

int maxw;
int v[100];
int n;
int w[100];
int solve(int i, int vi, int wi) {
  if (wi > maxw)
    return 0;
  if (i == n)
    return vi;
  int a = solve(i + 1, vi + v[i], wi + w[i]);
  int b = solve(i + 1, vi, wi);
  return MAX(a, b);
}

int main(void) {
  cin >> n >> maxw;
  for (int i = 0; i < n; i++)
    cin >> v[i] >> w[i];
  cout << solve(0, 0, 0) << endl;
  return 0;
}
Problem p02319 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int main(void) {
  long long dp[10010];
  int n, m;
  int v, w, k = 0, sum = 0;
  cin >> n >> m;

  dp[0] = 0;
  for (int i = 1; i <= 10000; i++)
    dp[i] = 1000000010;

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

  for (int i = sum; i >= 1; i--) {
    if (dp[i] <= m) {
      k = i;
      break;
    }
  }

  cout << k << endl;

  return 0;
}


Error: -11

Fixed submission:

#include <iostream>
using namespace std;

int main(void) {
  int ans, n, m, v[101], w[101];
  int dp[101][1000000001];
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> v[i] >> w[i];
  }

  for (int i = 0; i <= 100 * n; i++)
    dp[0][i] = 0;

  for (int i = 0; i < n; i++)
    for (int j = 0; j <= 100 * n; 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]);
    } // dp[i]でvalue jがweight

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

  return 0;
}
Problem p02319 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

int main() {
  int n, w = 0;
  std::cin >> n >> w;
  std::vector<std::vector<long>> dp(n + 1, std::vector<long>(n * 100 + 1, 0));
  dp[0][0] = 0;
  for (int i = 1; i < dp[0].size(); ++i) {
    dp[0][i] = 1000000001;
  }
  std::vector<int> iv;
  std::vector<long> iw;
  for (int i = 0; i < n; ++i) {
    int a, b = 0;
    std::cin >> a >> b;
    iv.emplace_back(a);
    iw.emplace_back(b);
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j <= dp[0].size(); ++j) {
      if (j < iv[i - 1]) {
        dp[i][j] = dp[i - 1][j];
      } else {
        dp[i][j] = std::min(dp[i - 1][j], dp[i - 1][j - iv[i - 1]] + iw[i - 1]);
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < dp[0].size(); ++i) {
    if (dp[n][i] <= w)
      ans = i;
  }
  std::cout << ans << std::endl;
  return 0;
}


Error: -6
malloc(): corrupted top size


Fixed submission:

#include <bits/stdc++.h>

int main() {
  int n, w = 0;
  std::cin >> n >> w;
  std::vector<std::vector<long>> dp(n + 1, std::vector<long>(n * 100 + 1, 0));
  dp[0][0] = 0;
  for (int i = 1; i < dp[0].size(); ++i) {
    dp[0][i] = LONG_MAX;
  }
  std::vector<int> iv;
  std::vector<long> iw;
  for (int i = 0; i < n; ++i) {
    int a, b = 0;
    std::cin >> a >> b;
    iv.emplace_back(a);
    iw.emplace_back(b);
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j <= dp[0].size(); ++j) {
      if (j < iv[i - 1]) {
        dp[i][j] = dp[i - 1][j];
      } else {
        dp[i][j] = std::min(dp[i - 1][j], dp[i - 1][j - iv[i - 1]] + iw[i - 1]);
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < dp[0].size(); ++i) {
    if (dp[n][i] <= w)
      ans = i;
  }
  std::cout << ans << std::endl;
  return 0;
}
Problem p02319 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <limits>

constexpr int MAX_NV = 100;
constexpr long MAX_W = 1000000000;
constexpr long INF = std::numeric_limits<long>::max();

int main() {
  int N, W, v[MAX_NV], w[MAX_NV];
  long dp[MAX_NV * MAX_NV + 1]{};

  scanf("%d%d", &N, &W);

  for (int i = 0; i <= N * MAX_NV; ++i) {
    dp[i] = INF;
  }

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

  dp[0] = 0;

  for (int i = 0; i < N; ++i) {
    for (int j = N * MAX_NV; j >= 0; --j) {
      if (j >= v[i]) {
        if (dp[j - v[i]] != INF && dp[j - v[i]] + w[i] <= W) {
          dp[j] = std::min(dp[j - v[i]] + w[i], dp[j]);
        }
      }
    }
  }

  int ans = 0;

  for (int i = 0; i <= MAX_NV * N; ++i) {
    if (dp[i] != INF) {
      ans = i;
    }
  }

  printf("%d\\n", ans);

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <limits>

constexpr int MAX_NV = 100;
constexpr long MAX_W = 1000000000;
constexpr long INF = std::numeric_limits<long>::max();

int main() {
  int N, W, v[MAX_NV], w[MAX_NV];
  long dp[MAX_NV + 1]{};

  std::fill(dp, dp + MAX_NV, INF);

  scanf("%d%d", &N, &W);

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

  dp[0] = 0;

  for (int i = 0; i < N; ++i) {
    for (int j = N * W; j >= 0; --j) {
      if (j >= v[i]) {
        if (dp[j - v[i]] != INF && dp[j - v[i]] + w[i] <= W) {
          dp[j] = std::min(dp[j - v[i]] + w[i], dp[j]);
        }
      }
    }
  }

  int ans = 0;

  for (int i = 0; i <= N * W; ++i) {
    if (dp[i] != INF) {
      ans = i;
    }
  }

  printf("%d\\n", ans);

  return 0;
}
Problem p02319 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define P pair<ll, ll>
#define FOR(I, A, B) for (ll I = (A); I < (B); ++I)
#define FORR(I, A, B) for (ll I = (B - 1); I >= (A); --I)
const ll INF = 1e18 + 7;
const ll MOD = 1e9 + 7;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll N, W;
  cin >> N >> W;
  vector<ll> v(N), w(N);
  FOR(i, 0, N) cin >> v[i] >> w[i];
  ll dp[N + 1][100 * N + 1];
  FOR(i, 0, N + 1) FOR(j, 0, 100 * N + 1) dp[i][j] = INF;
  FOR(i, 0, N + 1) dp[i][0] = 0;
  FOR(i, 1, N + 1) {
    FOR(j, 0, 100 * N + 1) {
      dp[i][j] = min(dp[i][j], dp[i - 1][j]);
      if (j + v[i - 1] <= (100 * N + 1))
        dp[i][j + v[i - 1]] =
            min(dp[i - 1][j + v[i - 1]], dp[i - 1][j] + w[i - 1]);
    }
  }
  FORR(i, 0, 100 * N + 1) {
    if (dp[N][i] <= W) {
      cout << i << endl;
      return 0;
    }
  }
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define P pair<ll, ll>
#define FOR(I, A, B) for (ll I = (A); I < (B); ++I)
#define FORR(I, A, B) for (ll I = (B - 1); I >= (A); --I)
const ll INF = 1e18 + 7;
const ll MOD = 1e9 + 7;

struct nap {
  vector<ll> v, w;
  ll N, W;
  vector<vector<ll>> dp;
  nap(ll n, ll wmax) {
    N = n;
    W = wmax;
    v.resize(n);
    w.resize(n);
    vector<ll> wv(W + 1, 0);
    FOR(i, 0, n + 1) { dp.push_back(wv); }
  }
  void calc() {
    FOR(i, 0, N) {
      FOR(j, 0, W + 1) {
        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]);
        }
      }
    }
  }
  ll maxval(ll w) { return dp[N][w]; }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n, w;
  cin >> n >> w;
  nap na(n, w);
  FOR(i, 0, n) { cin >> na.v[i] >> na.w[i]; }
  na.calc();
  cout << na.maxval(w) << endl;
}
Problem p02319 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define P pair<ll, ll>
#define FOR(I, A, B) for (ll I = (A); I < (B); ++I)
#define FORR(I, A, B) for (ll I = ((B)-1); I >= (A); --I)
#define TO(x, t, f) ((x) ? (t) : (f))
#define SORT(x) (sort(x.begin(), x.end()))   // 0 2 2 3 4 5 8 9
#define REV(x) (reverse(x.begin(), x.end())) // reverse
ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  if (a % b == 0)
    return b;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
  ll c = gcd(a, b);
  return ((a / c) * (b / c) * c);
} // saisyo kobaisu
#define NEXTP(x) next_permutation(x.begin(), x.end())
const ll INF = 1e18 + 7;
const ll MOD = 1e9 + 7;
#define pri(a) cout << (a) << endl

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll N, W;
  cin >> N >> W;
  vector<ll> v(N), w(N);
  FOR(i, 0, N) cin >> v[i] >> w[i];
  ll dp[2][10200], ans = 0;
  FOR(j, 0, 10200) dp[1][j] = dp[0][j] = MOD;
  dp[0][0] = dp[1][0] = 0;
  FOR(i, 1, N + 1) {
    FOR(j, 0, N * 101) {
      ll k = i % 2;
      ll l = 1 - k;
      dp[k][j] = min(dp[l][j], dp[k][j]);
      dp[k][j + v[i - 1]] = min(dp[l][j + v[i - 1]], dp[l][j] + w[i - 1]);
    }
  }
  FOR(i, 0, 2) {
    FOR(j, 0, N * 101) {
      if (dp[i][j] <= W)
        ans = max(ans, j);
    }
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define P pair<ll, ll>
#define FOR(I, A, B) for (ll I = (A); I < (B); ++I)
#define FORR(I, A, B) for (ll I = ((B)-1); I >= (A); --I)
#define TO(x, t, f) ((x) ? (t) : (f))
#define SORT(x) (sort(x.begin(), x.end()))   // 0 2 2 3 4 5 8 9
#define REV(x) (reverse(x.begin(), x.end())) // reverse
ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  if (a % b == 0)
    return b;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
  ll c = gcd(a, b);
  return ((a / c) * (b / c) * c);
} // saisyo kobaisu
#define NEXTP(x) next_permutation(x.begin(), x.end())
const ll INF = 1e18 + 7;
const ll MOD = 1e9 + 7;
#define pri(a) cout << (a) << endl

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll N, W;
  cin >> N >> W;
  vector<ll> v(N), w(N);
  FOR(i, 0, N) cin >> v[i] >> w[i];
  ll dp[N + 1][N * 102], ans = 0;
  FOR(i, 0, N + 1) FOR(j, 0, N * 102) dp[i][j] = MOD;
  FOR(i, 0, N + 1) dp[i][0] = 0;
  FOR(i, 1, N + 1) {
    FOR(j, 0, N * 101) {
      dp[i][j + v[i - 1]] =
          min(dp[i - 1][j + v[i - 1]], dp[i - 1][j] + w[i - 1]);
    }
  }
  FOR(i, 1, N + 1) {
    FOR(j, 0, N * 101) {
      if (dp[i][j] <= W)
        ans = max(ans, j);
    }
  }
  cout << ans << endl;
}
Problem p02319 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <set>
#include <vector>
using namespace std;

int main() {
  int N;
  long long W, tV = 0, tW = 0, x = 0;
  scanf("%d%lld", &N, &W);
  vector<pair<int, int>> v(N);
  for (int i = 0; i < N; i++)
    scanf("%d%d", &v[i].first, &v[i].second), tV += v[i].first,
        tW += v[i].second;
  if (W > tW)
    W = tW;
  if (tW < 2000000) {
    vector<long long> bag(tW + 1);
    bag[0] = 1;
    for (auto &e : v) {
      x += e.second;
      for (int j = x; j >= e.second; j--)
        if (bag[j - e.second] && bag[j] < bag[j - e.second] + e.first)
          bag[j] = bag[j - e.second] + e.first;
    }
    for (int i = 0; i < W; i++)
      bag[i + 1] = max(bag[i + 1], bag[i]);
    printf("%lld\\n", bag[W] - 1);
  } else if (tV < 2000000) {
    vector<long long> bag(tV + 1);
    bag[0] = 1;
    for (auto &e : v) {
      x += e.first;
      for (int j = x; j >= e.first; j--)
        if (bag[j - e.first] &&
            (!bag[j] || bag[j] > bag[j - e.first] + e.second))
          bag[j] = bag[j - e.first] + e.second;
    }
    int i = 0, mi;
    for (; i <= tV; i++)
      if (bag[i] && bag[i] - 1 <= W)
        mi = i;
    printf("%d\\n", mi);
  } else {
    return 1;
  }
}

Error: 0

Fixed submission:

#include <cstdio>
#include <set>
#include <vector>
using namespace std;

int main() {
  int N;
  long long W, tV = 0, tW = 0, x = 0;
  scanf("%d%lld", &N, &W);
  vector<pair<int, int>> v(N);
  for (int i = 0; i < N; i++)
    scanf("%d%d", &v[i].first, &v[i].second), tV += v[i].first,
        tW += v[i].second;
  if (tW < 2000000) {
    vector<long long> bag(tW + 1);
    bag[0] = 1;
    for (auto &e : v) {
      x += e.second;
      for (int j = x; j >= e.second; j--)
        if (bag[j - e.second] && bag[j] < bag[j - e.second] + e.first)
          bag[j] = bag[j - e.second] + e.first;
    }
    for (int i = 0; i < W; i++)
      bag[i + 1] = max(bag[i + 1], bag[i]);
    printf("%lld\\n", bag[W] - 1);
  } else {
    vector<long long> bag(tV + 1);
    bag[0] = 1;
    for (auto &e : v) {
      x += e.first;
      for (int j = x; j >= e.first; j--)
        if (bag[j - e.first] &&
            (!bag[j] || bag[j] > bag[j - e.first] + e.second))
          bag[j] = bag[j - e.first] + e.second;
    }
    int i = 0, mi;
    for (; i <= tV; i++)
      if (bag[i] && bag[i] - 1 <= W)
        mi = i;
    printf("%d\\n", mi);
  }
}
Problem p02319 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define INF (1 << 30)
#define NUMMAX 10000
int main() {
  int N, W;
  cin >> N >> W;
  vector<int> line(NUMMAX + 1, INF);
  line[0] = 0;
  for (int i = 0; i < N; ++i) {
    int v, w;
    cin >> v >> w;
    for (int j = NUMMAX - v; j >= 0; --j) {
      line[j + v] = min(line[j + v], line[j] + w);
    }
  }
  int ans = 0;
  for (int j = NUMMAX; j > 0; --j) {
    if (line[j] <= W) {
      ans = j;
      break;
    }
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
#define N_MAX 100

int N, W;
int v[N_MAX], w[N_MAX];
vector<vector<int>> dp;

int func(int i, int j) {
  int res;
  if (dp[i][j] >= 0)
    return dp[i][j];
  if (i == N) {
    res = 0;
  } else if (j < w[i]) {
    res = func(i + 1, j);
  } else {
    res = max(func(i + 1, j), func(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 >> v[i] >> w[i];
  dp.resize(N + 1);
  for (int i = 0; i < N + 1; i++)
    dp[i].resize(W + 1);
  for (int i = 0; i < N + 1; i++) {
    for (int j = 0; j < W + 1; j++) {
      dp[i][j] = -1;
    }
  }
  cout << func(0, W) << endl;
  return 0;
}
Problem p02319 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

const long long int INF = 1000000001;

int main() {
  int n, weight;

  cin >> n >> weight;

  int v[n], w[n];

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

  int maxv = v[0];

  for (int i = 0; i < n; i++) {
    if (maxv < v[i]) {
      maxv = v[i];
    }
  }

  long long int dp[n + 1][n * maxv + 1];

  fill(dp[0], dp[0] + n * maxv + 1, INF);

  dp[0][0] = 0;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= n * maxv; j++) {

      if (j < v[i]) {
        dp[i + 1][j] = dp[i][j];
      }

      else {
        dp[i + 1][j] = min(dp[i][j], dp[i][j - v[i]] + w[i]);
      }
    }
  }

  /*for(int i=0; i<n; i++){
    for(int j=0; j<=n*maxv+1; j++){
      cout << dp[i][j] << " ";
    }
    cout << endl;
  }*/

  int ans = 0;

  for (int i = 0; i <= n * maxv; i++) {
    if (dp[n][i] <= weight) {
      ans = i;
    }
  }

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n, weight;

  cin >> n >> weight;

  int v[n], w[n];

  int dp[n + 1][weight + 1];

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

  for (int i = 0; i < weight + 1; i++) {
    dp[0][i] = 0;
  }

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

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

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

  return 0;
}
Problem p02319 (C++) — buggy submission, verdict: Runtime Error

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
// #include "MyMath.h"
// #include "DataStructure.h"
#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <vector>
using namespace std;
typedef pair<int, int> p;
const int INF = 1000000000;
int dp[101][10001];
int main() {
  int n, W;
  cin >> n >> W;
  int *v = new int[n];
  int *w = new int[n];
  int sumv = 0;
  for (int i = 0; i < n; i++) {
    cin >> v[i] >> w[i];
    sumv += v[i];
  }
  fill(dp[0], dp[n + 1], INF);
  dp[0][0] = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j <= sumv; j++) {
      if (v[i] > j)
        dp[i + 1][j] = dp[i][j];
      else
        dp[i + 1][j] = min(dp[i][j], dp[i][j - v[i]] + w[i]);
    }
  for (int i = sumv; i >= 0; i--)
    if (dp[n][i] <= W) {
      cout << i << endl;
      break;
    }
  delete[] v, w;
  return 0;
}

Error: -11

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
// #include "MyMath.h"
// #include "DataStructure.h"
#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <vector>
using namespace std;
typedef pair<int, int> p;
const int INF = 100000;
int dp[101][10001];
int main() {
  int n, W;
  cin >> n >> W;
  int *v = new int[n];
  int *w = new int[n];
  int sumv = 0;
  for (int i = 0; i < n; i++) {
    cin >> v[i] >> w[i];
    sumv += v[i];
  }
  fill(dp[0], dp[10001], INF);
  dp[0][0] = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j <= 10001; j++) {
      if (v[i] > j)
        dp[i + 1][j] = dp[i][j];
      else
        dp[i + 1][j] = min(dp[i][j], dp[i][j - v[i]] + w[i]);
    }
  for (int i = 10001; i >= 0; i--)
    if (dp[n][i] <= W) {
      cout << i << endl;
      break;
    }
  delete[] v, w;
  return 0;
}
Problem p02319 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

int main() {
  int n, w = 0;
  std::cin >> n >> w;
  std::vector<std::vector<long>> dp(n + 1, std::vector<long>(n * 100 + 1, 0));
  dp[0][0] = 0;
  for (int i = 1; i < dp[0].size(); ++i) {
    dp[0][i] = 1000000001;
  }
  std::vector<int> iv;
  std::vector<long> iw;
  for (int i = 0; i < n; ++i) {
    int a, b = 0;
    std::cin >> a >> b;
    iv.emplace_back(a);
    iw.emplace_back(b);
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j <= dp[0].size(); ++j) {
      if (j < iv[i - 1]) {
        dp[i][j] = dp[i - 1][j];
      } else {
        dp[i][j] = std::min(dp[i - 1][j], dp[i - 1][j - iv[i - 1]] + iw[i - 1]);
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < dp[0].size(); ++i) {
    if (dp[n][i] <= w)
      ans = i;
  }
  std::cout << ans << std::endl;
  return 0;
}


Error: -6
malloc(): corrupted top size


Fixed submission:

#include <bits/stdc++.h>

int main() {
  int n, w = 0;
  std::cin >> n >> w;
  std::vector<std::vector<long>> dp(n + 1, std::vector<long>(n * 100 + 1, 0));
  dp[0][0] = 0;
  for (int i = 1; i <= dp[0].size(); ++i) {
    dp[0][i] = 1000000001L;
  }
  std::vector<int> iv;
  std::vector<long> iw;
  for (int i = 0; i < n; ++i) {
    int a, b = 0;
    std::cin >> a >> b;
    iv.emplace_back(a);
    iw.emplace_back(b);
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j <= dp[0].size(); ++j) {
      if (j < iv[i - 1]) {
        dp[i][j] = dp[i - 1][j];
      } else {
        dp[i][j] = std::min(dp[i - 1][j], dp[i - 1][j - iv[i - 1]] + iw[i - 1]);
      }
    }
  }
  int ans = 0;
  for (int i = 0; i <= dp[0].size(); ++i) {
    if (dp[n][i] <= w)
      ans = i;
  }
  std::cout << ans << std::endl;
  return 0;
}