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

#include <iostream>
#include <stdio.h>
using namespace std;

int main() {
  int n, wt;
  int v[105];
  int w[105];
  int dp[100010];

  cin >> n >> wt;

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

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

  cout << dp[wt] << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <stdio.h>
using namespace std;

int dp(int rest, int v[], int w[], int i) {
  if (i == 0 || rest == 0)
    return 0;
  if ((rest - w[i - 1]) < 0)
    return dp(rest, v, w, i - 1);
  return max(dp(rest, v, w, i - 1),
             v[i - 1] + dp(rest - w[i - 1], v, w, i - 1));
}

int main() {
  int n, wt;
  int v[105];
  int w[105];

  cin >> n >> wt;

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

  int ans = dp(wt, v, w, n);
  cout << ans << endl;

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

#include <stdio.h>
int n;
int f(int now, int w) { return now + w * n; }
int max(int a, int b) {
  if (a < b)
    return b;
  return a;
}
int main() {
  int m, dp[1010000], v, w;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++)
    dp[f(0, i)] = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &v, &w);
    for (int j = 0; j <= m; j++) {
      if (j < w)
        dp[f(i, j)] = dp[f(i - 1, j)];
      else
        dp[f(i, j)] = max(dp[f(i - 1, j)], dp[f(i - 1, j - w)] + v);
    }
  }
  printf("%d\\n", dp[f(n, m)]);
}

Error: TLE

Fixed submission:

#include <stdio.h>
struct item {
  int cost;
  int val;
};
int max(int a, int b) {
  if (a > b)
    return a;
  return b;
}
item a[100000];
int dp(int now, int w) {
  if (w < 0)
    return -99999999;
  if (now < 0)
    return 0;
  return max(dp(now - 1, w), dp(now - 1, w - a[now].cost) + a[now].val);
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &a[i].val, &a[i].cost);
  }
  printf("%d\\n", dp(n, m));
}
Problem p02315 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

// ---------------------
// repetition
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

// ---------------------
#define INF 922337203685477580
#define IINF (1 << 21)
typedef long long ll;

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

  int dp[N + 10][W + 10];
  int vi[N + 1];
  int wi[N + 1];

  REP(i, N) { cin >> vi[i + 1] >> wi[i + 1]; }

  REP(w, W + 1) {
    REP(i, N + 1) { dp[i][w] = 0; }
  }
  for (int i = 1; i <= N; i++) {
    for (int w = 0; w <= W; w++) {
      if (w - wi[i] < 0) {
        dp[i][w] = dp[i - 1][w];
        continue;
      }
      dp[i][w] = max(dp[i - 1][w], dp[i - 1][w - wi[i]] + vi[i]);
    }
  }

  // REP (i, N + 1) {
  //   REP (w, W + 1) {
  //     if (w) cout << " ";
  //     cerr << dp[i][w];
  //   }
  //   cerr << endl;
  // }

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

Error: WA

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

// ---------------------
// repetition
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

// ---------------------
#define INF 922337203685477580
#define IINF (1 << 21)
typedef long long ll;

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

  int dp[N + 10][W + 10];
  int vi[N + 1];
  int wi[N + 1];

  REP(i, N) { cin >> vi[i + 1] >> wi[i + 1]; }

  REP(w, W + 1) {
    REP(i, N + 1) { dp[i][w] = 0; }
  }
  for (int i = 1; i <= N; i++) {
    for (int w = wi[i]; w <= W; w++) {
      dp[i][w] = max(dp[i - 1][w], dp[i - 1][w - wi[i]] + vi[i]);
    }
  }

  REP(i, N + 1) {
    REP(w, W + 1) {
      if (w)
        cout << " ";
      cerr << dp[i][w];
    }
    cerr << endl;
  }

  cout << dp[N][W] << endl;
  return 0;
}
Problem p02315 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, j, k) for (int i = j; i < k; ++i)
#define rep(i, j) FOR(i, 0, j)
#define repr(i, j) for (int i = j; i >= 0; --i)
#define INF (1 << 30)
#define MOD 1e9 + 7

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

int N, W;
int v[101], w[101];
int dp[101][10001];

int main() {
  scanf("%d %d", &N, &W);
  rep(i, N) scanf("%d %d", &v[i], &w[i]);
  repr(i, N - 1) rep(j, W + 1) {
    if (j < w[i])
      dp[i][j] = dp[i + 1][j];
    else {
      dp[i][j] = max(dp[i + 1][j], dp[i + 1][j - w[i]] + v[i]);
    }
  }
  printf("%d\\n", dp[0][W]);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, j, k) for (int i = j; i < k; ++i)
#define rep(i, j) FOR(i, 0, j)
#define repr(i, j) for (int i = j; i >= 0; --i)
#define INF (1 << 30)
#define MOD 1e9 + 7

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

int N, W;
int v[101], w[101];
int dp[101][10001];

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

int main() {
  scanf("%d %d", &N, &W);
  memset(dp, -1, sizeof(dp));
  dp[0][0] = 0;
  rep(i, N) scanf("%d %d", &v[i], &w[i]);
  printf("%d\\n", solve(0, W));
  return 0;
}
Problem p02315 (C++) — buggy submission, verdict: Time Limit Exceeded

// その２：メモ化再帰

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define MAX_N 100
#define MAX_W 10000
using namespace std;
int dp[MAX_N][MAX_W];
int n, W;
struct Item {
  int weight;
  int value;
};
typedef struct Item Item;
Item data[MAX_N];

// pos番目の荷物から、重さj以下になるように選んで行く
int knapsack(int pos, int j) {
  if (pos >= n)
    return (0);
  if (dp[pos][j] != -1)
    return (dp[pos][j]);
  else if (j < data[pos].weight)
    return (knapsack(pos + 1, j));
  else {
    return (dp[pos][j] =
                max(knapsack(pos + 1, j),
                    knapsack(pos + 1, j - data[pos].weight) + data[pos].value));
  }
}

void solve() {
  memset(dp, -1, sizeof(dp));
  cout << knapsack(0, W) << endl;
}

int main(void) {
  cin >> n >> W;

  for (int r = 0; r < n; r++)
    cin >> data[r].value >> data[r].weight;
  solve();
  return (0);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define MAX_N 100
#define MAX_W 10000
using namespace std;
int n, W;
struct Item {
  int weight;
  int value;
};
typedef struct Item Item;
Item data[MAX_N];

// pos番目の荷物から、重さj以下になるように選んで行く
int knapsack(int pos, int j) {
  if (pos >= n)
    return (0);
  else if (j < data[pos].weight)
    return (knapsack(pos + 1, j));
  else {
    return (max(knapsack(pos + 1, j),
                knapsack(pos + 1, j - data[pos].weight) + data[pos].value));
  }
}

void solve() { cout << knapsack(0, W) << endl; }

int main(void) {
  cin >> n >> W;

  for (int r = 0; r < n; r++)
    cin >> data[r].value >> data[r].weight;
  solve();
  return (0);
}
Problem p02315 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100;
const int MAX_W = 10000;

int dp[MAX_N + 1][MAX_W + 1], v[MAX_N], w[MAX_N], N, W;

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

  for (int j = 0; j <= W; j++)
    dp[0][j] = 0;

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


Error: TLE

Fixed submission:

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

const int MAX_N = 100, MAX_W = 1000;
int w[MAX_W], v[MAX_N];
int N, W;

int solve(int i, int j) {
  int 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 res;
}

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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define ll long long
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define ROF(i, a, b) for (int i = b - 1; i >= a; i--)
using namespace std;
//
const int inf = 1e9 + 7;
const int mod = 1e9 + 7;
//
struct poi {
  int X;
  int Y;
  int Z;
  bool operator<(const poi &R) const {
    return X == R.X ? Y == R.Y ? Z < R.Z : Y < R.Y : X < R.X;
  }
};
//
ll GCD(ll a, ll b) { return (b == 0) ? (a) : (GCD(b, a % b)); }
ll LCM(ll a, ll b) { return a / GCD(a, b) * b; }
//
int DP[101][10001];
int main() {
  int N, W;
  int v[100], w[100];
  cin >> N >> W;
  FOR(i, 0, N) { cin >> v[i] >> w[i]; }
  FOR(i, 0, N) {
    FOR(j, 0, W + 1) {
      DP[i + 1][j] = max(DP[i + 1][j], DP[i][j]);
      if (W - j >= w[i]) {
        DP[i + 1][j + w[i]] = max(DP[i + 1][j + w[i]], DP[i][j] + v[i]);
      }
    }
  }
  cout << DP[N][W] << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define ll long long
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define ROF(i, a, b) for (int i = b - 1; i >= a; i--)
using namespace std;
//
const int inf = 1e9 + 7;
const int mod = 1e9 + 7;
//
struct poi {
  int X;
  int Y;
  int Z;
  bool operator<(const poi &R) const {
    return X == R.X ? Y == R.Y ? Z < R.Z : Y < R.Y : X < R.X;
  }
};
//
ll GCD(ll a, ll b) { return (b == 0) ? (a) : (GCD(b, a % b)); }
ll LCM(ll a, ll b) { return a / GCD(a, b) * b; }
//
int DP[101][10001];
int main() {
  int N, W;
  int v[100], w[100];
  cin >> N >> W;
  FOR(i, 0, N) { cin >> v[i] >> w[i]; }
  FOR(i, 0, N) {
    FOR(j, 0, W + 1) {
      DP[i + 1][j] = max(DP[i + 1][j], DP[i][j]);
      if (W - j >= w[i]) {
        DP[i + 1][j + w[i]] = max(DP[i + 1][j + w[i]], DP[i][j] + v[i]);
      } else {
        DP[i + 1][j + w[i]] = DP[i][j + w[i]];
      }
    }
  }
  cout << DP[N][W] << endl;
  return 0;
}
Problem p02315 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

vector<int> v, w;

int main() {
  ostringstream out;

  int N, W, v, w, m = 0;
  cin >> N >> W;
  vector<int> list(W + 1);

  for (int i = 0; i < N; i++) {
    cin >> v >> w;
    for (int i = list.size() - 1; i >= 0; i--) {
      if (i + w < list.size() && (list[i] || i == 0)) {
        list[i + w] = max(list[i + w], list[i] + v);
        m = max(m, list[i + w]);
      }
    }
  }

  out << m << endl;

  cout << out.str();
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

int main() {
  ostringstream out;

  int N, W, v, w;
  while (cin >> N >> W, N | W) {
    vector<int> list(W + 1);
    for (int i = 0; i < N; i++) {
      cin >> v >> w;
      for (int j = list.size() - 1; j >= 0; j--)
        if (list[j] && j + w < list.size())
          list[j + w] = max(list[j + w], list[j] + v);
      if (w < list.size())
        list[w] = max(list[w], v);
      for (int i = 0; i < list.size(); i++)
        cout << list[i] << " ";
      cout << endl;
    }
    for (int i = list.size() - 1; i >= 0; i--)
      if (list[i]) {
        out << list[i] << endl;
        break;
      }
  }

  cout << out.str();
  return 0;
}
Problem p02315 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define fi first
#define se second
using namespace std;
bool value(int y, int x, int R, int C) {
  return 0 <= y && y < R && 0 <= x && x < C;
}
typedef pair<int, int> pii;
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<vi> vvi;
double pie = acos(-1);
int dx[4] = {0, -1, 0, 1};
int dy[4] = {-1, 0, 1, 0};
int dp[150][10500];
int main() {
  int N;
  int v[105], w[105];
  int W;
  cin >> N >> W;
  for (int i = 0; i < N; i++) {
    cin >> v[i] >> w[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]);
      }
    }
  }
  cout << dp[N][W] << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define fi first
#define se second
using namespace std;
bool value(int y, int x, int R, int C) {
  return 0 <= y && y < R && 0 <= x && x < C;
}
typedef pair<int, int> pii;
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<vi> vvi;
double pie = acos(-1);
int dx[4] = {0, -1, 0, 1};
int dy[4] = {-1, 0, 1, 0};
int main() {
  int N;
  int v[1050], w[1050];
  int W;
  int dp[150][10500];
  cin >> N >> W;
  for (int i = 0; i < N; i++) {
    dp[0][i] = 0;
  }
  for (int i = 0; i < W; i++) {
    dp[i][0] = 0;
  }
  for (int i = 0; i < N; i++) {
    cin >> v[i] >> w[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]);
      }
    }
  }
  cout << dp[N][W] << endl;
}
Problem p02315 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
int dp[101][10001] = {0};
int c[101][2] = {0}; // c[i][0]..v, c[i][1]...w
int n, w;
int main() {
  scanf("%d %d", &n, &w);
  for (int i = 0; i < n; ++i) {
    scanf("%d %d", &c[i + 1][0], &c[i + 1][1]);
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= w; ++j) {
      if (j < c[i][1]) {
        dp[i][j] = dp[i - 1][j];
      } else {
        dp[i][j] = max(dp[i - 1][j - c[i][1]] + c[i][0], dp[i - 1][j]);
      }
    }
  }
  printf("%d\\n", dp[n][w]);
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdio.h>
using namespace std;
int dp[101][10001] = {0};
int c[100][2] = {0}; // c[i][0]..v, c[i][1]...w
int n, w;
int main() {
  scanf("%d %d", &n, &w);
  for (int i = 0; i < n; ++i) {
    scanf("%d %d", &c[i][0], &c[i][1]);
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= w; ++j) {
      if (j < c[i][1]) {
        dp[i][j] = dp[i - 1][j];
      } else {
        if (dp[i - 1][j] > dp[i][j - c[i][1]] + c[i][0])
          dp[i][j] = dp[i - 1][j];
        else {
          dp[i][j] = dp[i][j - c[i][1]] + c[i][0];
        }
      }
    }
  }
  printf("%d\\n", dp[n - 1][w]);
}
Problem p02315 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;

int dp[110][10100];
int N;
int W;
int val[110];
int wei[110];

int main(void) {

  cin >> N >> W;

  for (int i = 0; i < N; i++)
    cin >> val[i] >> wei[i];

  for (int w = 0; w <= N; w++)
    dp[0][w] = 0;

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

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


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

int dp[110][10100];

int main(void) {
  int N;
  int W;
  int val[110];
  int wei[110];

  cin >> N >> W;

  for (int i = 0; i <= N; i++)
    cin >> val[i] >> wei[i];

  for (int w = 0; w <= N; w++)
    dp[0][w] = 0;

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

  cout << dp[N][W] << "\\n";
}
Problem p02315 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

int main() {
  int N, W;
  cin >> N >> W;
  int val[110], weight[10010];
  for (int i = 0; i < N; i++) {
    cin >> val[i] >> weight[i];
  }
  int res[110][10010];
  // res[number][weight]
  for (int i = 0; i < N + 1; i++) {
    res[i][0] = 0;
  }
  for (int i = 0; i < W + 1; i++) {
    res[0][i] = 0;
  }

  for (int i = 1; i < N + 1; i++) {
    for (int j = 1; j < W + 1; j++) {

      if (weight[i - 1] > j) {

        res[i][j] = res[i - 1][j];

      } else {

        res[i][j] =
            max((val[i - 1] + res[i - 1][j - weight[i - 1]]), res[i - 1][j]);
      }
    }
  }
  // for(int j=1;j<W+1;j++) {
  //	cout << endl;
  //	for(int i=1;i<N+1;i++) {
  //		cout << res[i][j] << " ";
  //	}
  // }

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

Error: -11

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int N, W;
  cin >> N >> W;
  int val[1000], weight[10000];
  for (int i = 0; i < N; i++) {
    cin >> val[i] >> weight[i];
  }
  int res[1000][10000];
  // res[number][weight]
  for (int i = 0; i < N + 1; i++) {
    res[i][0] = 0;
  }
  for (int i = 0; i < W + 1; i++) {
    res[0][i] = 0;
  }

  for (int i = 1; i < N + 1; i++) {
    for (int j = 1; j < W + 1; j++) {

      if (weight[i - 1] > j) {

        res[i][j] = res[i - 1][j];

      } else {

        res[i][j] =
            max((val[i - 1] + res[i - 1][j - weight[i - 1]]), res[i - 1][j]);
      }
    }
  }
  // for(int j=1;j<W+1;j++) {
  //	cout << endl;
  //	for(int i=1;i<N+1;i++) {
  //		cout << res[i][j] << " ";
  //	}
  // }

  cout << res[N][W] << endl;
}
Problem p02315 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

const size_t INF = 1uLL << 57;

size_t binary_knapsack(vector<size_t> w, vector<size_t> v, size_t c) {
  size_t n = w.size();
  size_t V = *max_element(v.begin(), v.end());
  vector<size_t> dp(n * V + 1, INF);
  dp[0] = 0;

  for (size_t i = 0; i < n; ++i)
    for (size_t j = n * V; j >= v[i]; --j)
      if (dp[j] > dp[j - v[i]] + w[i])
        dp[j] = dp[j - v[i]] + w[i];

  size_t res = 0;
  for (size_t k = 0; k <= n * V; ++k)
    if (dp[k] <= c)
      res = k;

  return res;
}

int main() {
  size_t N, W;
  scanf("%zu %zu", &N, &W);

  vector<size_t> v(N), w(N);
  for (size_t i = 0; i < N; ++i)
    scanf("%zu %zu", &v[i], &w[i]);

  printf("%zu\\n", binary_knapsack(w, v, W));
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

const size_t INF = 1uLL << 57;

size_t binary_knapsack(vector<size_t> w, vector<size_t> v, size_t c) {
  size_t n = w.size();
  size_t V = *max_element(v.begin(), v.end());
  vector<vector<size_t>> dp(n + 1, vector<size_t>(n * V + 1, INF));
  dp[0][0] = 0;

  for (size_t i = 0; i < n; ++i)
    for (size_t j = 0; j <= n * V; ++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]);
      }

  size_t res = 0;
  for (size_t k = 0; k <= n * V; ++k)
    if (dp[n][k] <= c)
      res = k;

  return res;
}

int main() {
  size_t N, W;
  scanf("%zu %zu", &N, &W);

  vector<size_t> v(N), w(N);
  for (size_t i = 0; i < N; ++i)
    scanf("%zu %zu", &v[i], &w[i]);

  printf("%zu\\n", binary_knapsack(w, v, W));
  return 0;
}
Problem p02315 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

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

  int v[100], w[1000];
  for (int i = 0; i < N; i++) {
    cin >> v[i] >> w[i];
  }

  int DP[N + 1][W + 1];
  for (int i = 0; i <= W; i++) {
    DP[0][i] = 0;
  }

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

  cout << DP[N][W] << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
using namespace std;

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

  int v[100], w[1000];
  for (int i = 0; i < N; i++) {
    cin >> v[i] >> w[i];
  }

  int DP[N + 1][W + 1];
  for (int i = 0; i <= W; i++) {
    DP[0][i] = 0;
    DP[i][0] = 0;
  }

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

  cout << DP[N][W] << endl;

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

#include <bits/stdc++.h>
#define INF 2000000000
#define int long long int
#define MOD 1000000007
#define pb push_back
#define cin1(n) (cin >> n)
#define cin2(a, b) (cin >> a >> b)
#define cin3(a, b, c) (cin >> a >> b >> c)
#define cout(n) (cout << n << endl)
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define fr first
#define sc second
#define EPS (1e-10)

using namespace std;

typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<pair<int, int>> vpii;
typedef vector<string> vs;
typedef vector<vector<string>> vss;
typedef pair<int, int> pii;

int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int n, m;
int a, b;
int p, q;

signed main() {
  int N, W;
  cin2(N, W);

  vi v(N + 1, 0), w(N + 1, 0);
  rep(i, 1, N + 1) { cin2(v[i], w[i]); }

  vvi dp(N + 1, vi(W + 1, 0));

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

  cout(dp[N][W]);

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define INF 2000000000
#define int long long int
#define MOD 1000000007
#define pb push_back
#define cin1(n) (cin >> n)
#define cin2(a, b) (cin >> a >> b)
#define cin3(a, b, c) (cin >> a >> b >> c)
#define cout(n) (cout << n << endl)
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define fr first
#define sc second
#define EPS (1e-10)

using namespace std;

typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<pair<int, int>> vpii;
typedef vector<string> vs;
typedef vector<vector<string>> vss;
typedef pair<int, int> pii;

int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int n, m;
int a, b;
int p, q;

signed main() {
  int N, W;
  cin2(N, W);

  vi v(N + 1, 0), w(W + 1, 0);
  rep(i, 1, N + 1) { cin2(v[i], w[i]); }

  vvi dp(N + 1, vi(W + 1, 0));

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

  cout(dp[N][W]);

  return 0;
}