alexjercan/bugnet huggingface dataset:
Problem p02316 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <stdio.h>

int n, w[112], v[112], W;
int dp[100 + 1][10000 + 1];

int main() {
  scanf("%d", &n);
  scanf("%d", &W);
  for (int i = 0; i < n; ++i) {
    scanf("%d%d", &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] = std::max(dp[i][j], dp[i + 1][j - w[i]] + v[i]);
      }
    }
  }

  printf("%d\\n", dp[n][W]);
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <stdio.h>
#include <string.h>
#define INF 123456789

int N, W, v[112], w[112];
int memo[112][11234];

int dp(int i, int j) {
  if (j < 0) {
    return -INF;
  }
  if (i == 0) {
    return 0;
  }
  if (memo[i][j] != -1) {
    return memo[i][j];
  }
  if (j < w[i]) {
    return memo[i][j] = dp(i - 1, j);
  } else {
    return memo[i][j] = std::max(dp(i - 1, j), dp(i, j - w[i]) + v[i]);
  }
}

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

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

  printf("%d\\n", dp(N, W));
  return 0;
}
Problem p02316 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#pragma warning(disable : 4996)
using namespace std;
using ll = long long;
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DPL_1_A&lang=jp
int main() {
  int N, W;
  cin >> N >> W;
  vector<int> v(N);
  vector<int> w(N);
  vector<vector<int>> dp(N + 1, vector<int>(W + 1, 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++) {
      dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
      if (j - w[i] >= 0) {
        dp[i + 1][j] = max(dp[i + 1][j], dp[i + 1][j - w[i]] + v[i]);
      }
    }
  }
  cout << dp[N][W] << endl;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#pragma warning(disable : 4996)
using namespace std;
using ll = long long;
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DPL_1_A&lang=jp
int main() {
  int N, W;
  cin >> N >> W;
  vector<int> v(N);
  vector<int> w(N);
  vector<vector<int>> dp(N + 1, vector<int>(W + 1, 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++) {
      dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
      if (j - w[i + 1]) {
        dp[i + 1][j] = max(dp[i + 1][j], dp[i + 1][j - w[i + 1]]);
      }
    }
  }
  cout << dp[N][W] << endl;
}
Problem p02316 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

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

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

  cout << dp[n][ws] << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

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

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

  for (int k = 1; k <= n; k++) {
    for (int i = 0; i <= ws; i++) {
      int temp = 0, j = 0;
      while (i >= w[k] * j) {
        dp[k][i] = max(dp[k][i], dp[k - 1][i - w[k] * j] + v[k] * j);
        j++;
      }
    }
  }

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

#include <algorithm>
#include <iostream>
#include <unordered_map>
#include <utility>
#include <vector>
// #include "../cxx-prettyprint/prettyprint.hpp"

// unordered_map用のハッシュ関数
namespace std {
template <> class hash<std::pair<int, int>> {
public:
  size_t operator()(const std::pair<int, int> &x) const {
    return x.first * 10000L + x.second;
  }
};
} // namespace std

std::vector<int> values;
std::vector<int> weights;
std::unordered_map<std::pair<int, int>, int> memo;

int get_maximum_value(int i, int w) {
  if (i < 0 || w == 0) {
    return 0;
  } else if (memo.find({i, w}) != memo.end()) {
    return memo[{i, w}];
  } else {
    int max_value;
    if (weights[i] <= w) {
      max_value = std::max(get_maximum_value(i, w - weights[i]) + values[i],
                           get_maximum_value(i - 1, w));
    } else {
      max_value = get_maximum_value(i - 1, w);
    }
    memo[{i, w}] = max_value;
    return max_value;
  }
}

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

  values.resize(N);
  weights.resize(N);
  for (int i = 0; i < N; i++) {
    std::cin >> values[i] >> weights[i];
  }

  std::cout << get_maximum_value(N - 1, W) << std::endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <utility>
#include <vector>
// #include "../cxx-prettyprint/prettyprint.hpp"

std::vector<int> values;
std::vector<int> weights;
std::map<std::pair<int, int>, int> memo;

int get_maximum_value(int i, int w) {
  if (i < 0 || w == 0) {
    return 0;
  } else if (memo.find({i, w}) != memo.end()) {
    return memo[{i, w}];
  } else {
    int max_value;
    if (weights[i] <= w) {
      max_value = std::max(get_maximum_value(i, w - weights[i]) + values[i],
                           get_maximum_value(i - 1, w));
    } else {
      max_value = get_maximum_value(i - 1, w);
    }
    memo[{i, w}] = max_value;
    return max_value;
  }
}

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

  values.resize(N);
  weights.resize(N);
  for (int i = 0; i < N; i++) {
    std::cin >> values[i] >> weights[i];
  }

  std::cout << get_maximum_value(N - 1, W) << std::endl;
}
Problem p02316 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < n; i++)
#define P(p) cout << (p) << endl;
using namespace std;
typedef long long ll;
int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};

unsigned long long sttoi(std::string str) {
  unsigned long long ret;
  std::stringstream ss;
  ss << str;
  ss >> ret;
  return ret;
}

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

void solve() {
  int n, w;
  cin >> n >> w;
  int dp[20000];
  fill(dp, dp + w, 0);
  int value[101], weight[101];
  rep(i, n) { cin >> value[i] >> weight[i]; }
  for (int i = 0; i < n; i++) {
    for (int j = weight[i]; j <= w; j++) {
      dp[j] = max(dp[j], dp[j - weight[i]] + value[i]);
    }
  }
  int maxi = -1;
  rep(i, w + 1) { maxi = max(maxi, dp[i]); }
  P(maxi);
}

int main() {
  solve();
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < n; i++)
#define P(p) cout << (p) << endl;
using namespace std;
typedef long long ll;
int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};

unsigned long long sttoi(std::string str) {
  unsigned long long ret;
  std::stringstream ss;
  ss << str;
  ss >> ret;
  return ret;
}

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

void solve() {
  int n, w;
  cin >> n >> w;
  int dp[20000];
  fill(dp, dp + 20000, -1);
  int value[200], weight[200];
  rep(i, n) { cin >> value[i] >> weight[i]; }
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= w; j++) {
      if (dp[j] != -1) {
        int tmp = j;
        while (tmp + weight[i] <= w) {
          dp[tmp + weight[i]] = max(dp[tmp + weight[i]], dp[tmp] + value[i]);
          tmp += weight[i];
        }
      }
    }
  }
  int maxi = -1;
  rep(i, w + 1) { maxi = max(maxi, dp[i]); }
  P(maxi);
}

int main() {
  solve();
  return 0;
}
Problem p02316 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()

using namespace std;

using ll = long long;
using ld = long double;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }

int dp[20000];

int main() {
  cout << setprecision(12) << fixed;
  int n, W;
  cin >> n >> W;
  REP(i, n) {
    int v, w;
    cin >> v >> w;
    REP(j, 10000)
    chmax(dp[j + w], dp[j] + v);
  }
  cout << dp[W] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int dp[1600][10005];
int main() {
  int n, t, v[105], w[105], poi = 1;
  cin >> n >> t;
  for (int i = 1; i <= n; i++) {
    int vv, ww;
    cin >> vv >> ww;
    int times = t / ww;
    for (int j = 0; j < times; j++) {
      v[poi] = vv;
      w[poi] = ww;
      poi++;
    }
  }
  for (int i = 0; i < 10005; i++)
    dp[1][i] = 0;
  dp[1][w[1]] = v[1];
  for (int i = 2; i < poi; i++)
    for (int vv = 0; vv <= t; vv++)
      if (vv - w[i] >= 0)
        dp[i][vv] = max(dp[i - 1][vv], dp[i - 1][vv - w[i]] + v[i]);
      else
        dp[i][vv] = dp[i - 1][vv];
  int ma = 0;
  for (int i = 0; i <= t; i++)
    ma = max(ma, dp[poi - 1][i]);
  cout << ma << endl;
}
Problem p02316 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string.h>
using namespace std;

int n, wt;
int v[100], w[100];
long a[101][10001];

int main() {
  cin >> n >> wt;

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

  memset(a, 0, sizeof(a));

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

  cout << a[n][wt] << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string.h>
using namespace std;

int n, wt;
int v[100], w[100];
int a[101][10001];

int main() {
  cin >> n >> wt;

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

  memset(a, 0, sizeof(a));

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= wt; j++) {
      a[i][j] = a[i - 1][j];
      int k = 1;
      while (j - w[i - 1] * k >= 0) {
        a[i][j] = max(a[i - 1][j - w[i - 1] * k] + v[i - 1] * k, a[i][j]);
        k++;
      }
    }
  }

  cout << a[n][wt] << endl;

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

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

#define MAX_W 100000

int dp[MAX_W], N, W, v[10000], w[10000];

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

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#define INF -2000000000
using namespace std;
int x[10000][10001], n, W, a, b, v[10001], MAX, MIN = -INF, MIN2;
int main() {
  memset(v, -127, sizeof(v));
  cin >> n >> W;
  for (int i = 0; i < n; i++) {
    cin >> a >> b;
    v[b] = max(a, v[b]);
    MIN = min(MIN, b);
    if (b == 1) {
      MIN2 = max(a, MIN2);
    }
  }
  if (MIN >= 2 || W < 5000) {
    for (int i = 1; i <= W / MIN; i++) {
      for (int j = 0; j <= W; j++) {
        if (v[j] > INF) {
          for (int k = 0; k < W; k++) {
            if (j + k <= W) {
              x[i][j + k] = max(x[i - 1][k] + v[j], x[i][j + k]);
            }
          }
        }
        MAX = max(MAX, x[i][j]);
      }
    }
  } else {
    MAX = W * MIN2;
  }
  cout << MAX << endl;
  return 0;
}
Problem p02316 (C++) — buggy submission, verdict: Runtime Error

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

signed main() {
  int a, b;
  cin >> a >> b;
  int dp[10001]{};
  for (int c = 0; c < a; c++) {
    int d, e;
    cin >> d >> e;
    for (int f = e; f <= b; f++) {
      dp[f] = max(dp[f], dp[f - e] + d);
    }
  }
  cout << dp[b] << endl;
}

Error: 0

Fixed submission:

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

signed main() {
  int a, b;
  cin >> a >> b;
  int dp[1001]{};
  for (int c = 0; c < a; c++) {
    int d, e;
    cin >> d >> e;
    for (int f = e; f <= b; f++) {
      dp[f] = max(dp[f], dp[f - e] + d);
    }
  }
  cout << dp[b] << endl;
}
Problem p02316 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

#define pb push_back

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
template <class T> using vv = vector<vector<T>>;

int main() {
  int n, max_w;
  cin >> n >> max_w;
  vector<map<string, int>> items;
  items.pb({{"dummy", 0}});
  for (int i = 0; i < n; i++) {
    int v_tmp, w_tmp;
    cin >> v_tmp >> w_tmp;
    map<string, int> mp;
    mp["value"] = v_tmp;
    mp["weight"] = w_tmp;
    items.pb(mp);
  }

  vvi max_value(n + 1, vi(max_w + 1, 0));
  for (int item_num = 1; item_num <= n; item_num++) {
    for (int w_left = 1; w_left <= max_w; w_left++) {
      max_value[item_num][w_left] = max_value[item_num - 1][w_left];
      if (w_left >= items[item_num]["weight"]) {
        max_value[item_num][w_left] =
            max(max_value[item_num][w_left],
                items[item_num]["value"] +
                    max_value[item_num][w_left - items[item_num]["weight"]]);
      }
    }
  }

  printf("%d\\n", max_value[n][max_w]);

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

#define pb push_back

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
template <class T> using vv = vector<vector<T>>;

int main() {
  int n, max_w;
  cin >> n >> max_w;
  vector<map<string, int>> items;
  items.pb({{"dummy", 0}});
  for (int i = 0; i < n; i++) {
    int v_tmp, w_tmp;
    cin >> v_tmp >> w_tmp;
    map<string, int> mp;
    mp["value"] = v_tmp;
    mp["weight"] = w_tmp;
    items.pb(mp);
  }

  vvi max_value(n + 1, vi(max_w + 1, 0));
  for (int item_num = 1; item_num <= n; item_num++) {
    for (int w_left = 1; w_left <= max_w; w_left++) {
      max_value[item_num][w_left] = max_value[item_num - 1][w_left];
      int w_left_r = w_left;
      for (int i = 1; w_left_r >= items[item_num]["weight"]; i++) {
        max_value[item_num][w_left] =
            max(max_value[item_num][w_left],
                i * items[item_num]["value"] +
                    max_value[item_num - 1]
                             [w_left - i * items[item_num]["weight"]]);
        w_left_r -= items[item_num]["weight"];
      }
    }
  }

  printf("%d\\n", max_value[n][max_w]);

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

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <vector>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) range(i, 0, b)
#define debug(x) cout << "debug " << x << endl;
using namespace std;

/*
N　入力の数
W　最大の重さ

v　価値
w　重さ
*/

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <vector>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) range(i, 0, b)
#define debug(x) cout << "debug " << x << endl;
using namespace std;

/*
N　入力の数
W　最大の重さ

v　価値
w　重さ
*/

int main() {
  int N, W;
  while (cin >> N >> W) {
    int dp[105][10005] = {0};
    range(i, 1, N + 1) {
      int v, w;
      cin >> v >> w;
      range(j, 1, W + 1) {
        if (j - w >= 0) {
          dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w] + v);
        } else
          dp[i][j] = dp[i - 1][j];
      }
      rep(k, (W + 1) / w) rep(j, W + 1) if (j + w <= W) dp[i][j + w] =
          max(dp[i][j + w], dp[i][j] + v);
    }
    cout << dp[N][W] << endl;
  }
}
Problem p02316 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <stdio.h>
#include <vector>

using namespace std;

int n, w_max;
int dp[101][10001];
bool f[101][100001];
vector<pair<int, int>> item(101);

int saiki(int i, int j) {
  if (f[i][j]) {
    return dp[i][j];
  }

  int res;
  if (i == n) {
    res = 0;
  } else if (j < item[i].second) {
    res = saiki(i + 1, j);
  } else {
    res = max(saiki(i + 1, j), saiki(i, j - item[i].second) + item[i].first);
  }

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

int main() {
  cin >> n >> w_max;
  for (int i = 0; i < n; i++) {
    cin >> item[i].first >> item[i].second;
  }

  cout << saiki(0, w_max) << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <stdio.h>
#include <vector>

using namespace std;

int n, w_max;
int dp[100][10000];
bool f[100][100000];
vector<pair<int, int>> item(101);

int saiki(int i, int j) {
  if (f[i][j]) {
    return dp[i][j];
  }

  int res;
  if (i == n) {
    res = 0;
  } else if (j < item[i].second) {
    res = saiki(i + 1, j);
  } else {
    res = max(saiki(i + 1, j), saiki(i, j - item[i].second) + item[i].first);
  }

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

int main() {
  cin >> n >> w_max;
  for (int i = 0; i < n; i++) {
    cin >> item[i].first >> item[i].second;
  }

  cout << saiki(0, w_max) << endl;

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

#include <iostream>
#define INF 100000000
#define MAX(a, b) (((a) > (b)) ? (a) : (b))

using namespace std;

int main(void) {
  int N, W;
  int dp[101][10001];
  int w[101], v[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 <= W; j++) {
      dp[i][j] = 0;
      dp[0][j] = 0;
    }
    dp[0][i] = 0;
  }
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= W; j++) {
      if (j - w[i] >= 0) {
        dp[i][j] = MAX(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]);
        dp[i][j] = MAX(dp[i][j - w[i]] + v[i], dp[i][j]);
      } else
        dp[i][j] = dp[i - 1][j];
      // for(int k=1;k<=N;k++){
      // dp[i][j]=MAX(dp[i][j-w[k]]+v[k],dp[i][j]);
      // }
    }
  }
  cout << dp[N][W] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#define INF 100000000
#define MAX(a, b) (((a) > (b)) ? (a) : (b))

using namespace std;

int main(void) {
  int N, W;
  int dp[101][10001];
  int w[101], v[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 <= W; j++) {
      dp[i][j] = 0;
      dp[0][j] = 0;
    }
    dp[0][i] = 0;
  }
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= W; j++) {
      if (j - w[i] >= 0)
        dp[i][j] = MAX(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]);
      else
        dp[i][j] = dp[i - 1][j];
      for (int k = 1; j - k * w[i] >= 0; k++) {
        dp[i][j] = MAX(dp[i - 1][j - k * w[i]] + k * v[i], dp[i][j]);
      }
    }
  }
  cout << dp[N][W] << endl;
}
Problem p02316 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define inf 1e9
#define ll long long
#define ull unsigned long long
#define M 1000000007
#define P pair<int, int>
#define PLL pair<ll, ll>
#define FOR(i, m, n) for (int i = (int)m; i < (int)n; i++)
#define RFOR(i, m, n) for (int i = (int)m; i >= (int)n; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n, 0)
#define all(a) a.begin(), a.end()
#define IN(n, a)                                                               \\
  rep(i, n) { cin >> a[i]; }
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};
#define PI 3.14159265

int N, W;
int dp[110000] = {};
int v[1000], w[1000];

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

  int ans = 0;

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

  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define inf 1e9
#define ll long long
#define ull unsigned long long
#define M 1000000007
#define P pair<int, int>
#define PLL pair<ll, ll>
#define FOR(i, m, n) for (int i = (int)m; i < (int)n; i++)
#define RFOR(i, m, n) for (int i = (int)m; i >= (int)n; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n, 0)
#define all(a) a.begin(), a.end()
#define IN(n, a)                                                               \\
  rep(i, n) { cin >> a[i]; }
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};
#define PI 3.14159265

int N, W;
int dp[1100] = {};
int v[1000], w[1000];

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

  int ans = 0;

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

  cout << ans << endl;
}
Problem p02316 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;
typedef unsigned int ui;
// using ui = unsigned int;

int main(void) {
  string line;
  ui max_n, max_w;
  cin >> max_n >> max_w;
  ui v, w;

  vector<ui> table(max_w + 1, 0);

  for (int i = 1; i <= max_n; ++i) {
    cin >> v >> w;
    if (w <= max_w && table[w] < v)
      table[w] = v;
    for (int j = 1; j <= max_w - w; j++) {
      if (table[j] != 0 && table[w + j] < table[j] + v)
        table[w + j] = table[j] + v;
    }
  }
  cout << *(max_element(begin(table), end(table))) << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;
typedef unsigned int ui;
// using ui = unsigned int;

int main(void) {
  string line;
  ui max_n, max_w;
  cin >> max_n >> max_w;
  ui v, w;

  vector<ui> table(max_w + 1, 0);

  for (int i = 1; i <= max_n; ++i) {
    cin >> v >> w;
    ui dv = v, dw = w;
    for (; w <= max_w; v += dv, w += dw) {
      for (int j = max_w - w; j >= 1; --j) {
        if (table[j] != 0 && table[w + j] < table[j] + v)
          table[w + j] = table[j] + v;
      }
      if (w <= max_w && table[w] < v)
        table[w] = v;
    }
  }
  cout << *(max_element(begin(table), end(table))) << endl;

  return 0;
}