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

#include <algorithm>
#include <cstring>
#include <iostream>

#define nMAX 200
#define vMAX 2000
#define wMAX 2000
#define WMAX 20000

int n, W;
int v[nMAX], w[nMAX];
int dp[nMAX][WMAX];

using namespace std;

int rec(int i, int j) {
  if (dp[i][j] >= 0)
    return dp[i][j];
  int 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) {
  std::ios_base::sync_with_stdio();

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

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>

#define nMAX 1000
#define vMAX 10000
#define wMAX 10000
#define WMAX 100000

int n, W;
int v[nMAX], w[nMAX];
int dp[nMAX][WMAX];

using namespace std;

int rec(int i, int j) {
  if (dp[i][j] >= 0)
    return dp[i][j];
  int 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) {
  std::ios_base::sync_with_stdio();

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

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

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

int n, W, w[100], v[100], memo[101][10001];
int rec(int i, int j) {
  if (memo[i][j] != -1)
    return memo[i][j];
  int 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 memo[i][j] = res;
}
int main() {
  cin >> n >> W;
  for (int i = 0; i < n; ++i) {
    cin >> v[i] >> w[i];
  }
  memset(memo, -1, sizeof(memo));
  cout << rec(0, W) << endl;
}

Error: TLE

Fixed submission:

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

int n, W, w[100], v[100], memo[101][10001];
int rec(int i, int j) {
  if (memo[i][j] != -1)
    return memo[i][j];
  int 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 res;
}
int main() {
  cin >> n >> W;
  for (int i = 0; i < n; ++i) {
    cin >> v[i] >> w[i];
  }
  memset(memo, -1, sizeof(memo));
  cout << rec(0, W) << endl;
}
Problem p02315 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
int DP[10001][101];
int main() {
  int N, W;
  cin >> N >> W;
  int v[100], w[100];
  for (int i = 0; i < N; i++) {
    cin >> v[i] >> w[i];
  }
  for (int i = 1; i <= W; i++) {
    for (int j = 1; j <= N; j++) {
      if (i - w[j - 1] < 0) {
        DP[i][j] = DP[i][j - 1];
      } else {
        DP[i][j] = max(DP[i][j - 1], DP[i - w[j - 1]][j - 1] + v[j - 1]);
      }
    }
  }
  cout << DP[W][N] << endl;
  cin >> N;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int n, W;
int w[100], v[100];
int rec(int i, int j) {
  int 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 res;
}

int main() {
  cin >> n >> W;
  for (int i = 0; i < n; i++) {
    cin >> v[i] >> w[i];
  }
  cout << rec(0, W) << endl;
  return 0;
}
Problem p02315 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int n, W;
int v[100];
int w[100];
int dp[101][10001];
int rec_dp(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 = rec_dp(i + 1, j);
  } else {
    res = max(rec_dp(i + 1, j), rec_dp(i + 1, j - w[i]) + v[i]);
  }
  dp[i][j] = res;
  return res;
}
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[i][j] = -1;
    }
  }
  cout << rec_dp(0, W) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int n, W;
int v[100];
int w[100];
int rec(int i, int j) {
  int 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 res;
}
int main() {
  cin >> n >> W;
  for (int i = 0; i < n; i++) {
    cin >> v[i] >> w[i];
  }
  cout << rec(0, W) << endl;
  return 0;
}
Problem p02315 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {

  int N;
  int W;
  int vi[1000];
  int wi[1000];
  int DP[1001][1001];

  cin >> N >> W;

  for (int i = 0; i < N; i++) {
    cin >> vi[i] >> wi[i];
  }

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

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

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

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int N, W;

int vi[1000];
int wi[1000];

int rec(int a, int b) {
  if (a == N)
    return 0;
  if (b < wi[a]) {
    return rec(a + 1, b);
  }
  int res1 = rec(a + 1, b);
  int res2 = rec(a + 1, b - wi[a]) + vi[a];
  if (res1 > res2)
    return res1;
  else
    return res2;
}
int main() {

  cin >> N >> W;

  for (int k = 0; k < N; k++) {
    cin >> vi[k] >> wi[k];
  }

  cout << rec(0, W) << endl;

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

#include <iostream>

using namespace std;

int dp[101][10001];
int value[100];
int weight[100];
int NEGA_INF = -10000000;

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

  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= w; j++) {
      dp[i][j] =
          max(dp[i - 1][j], j - weight[i - 1] >= 0
                                ? dp[i - 1][j - weight[i - 1]] + value[i - 1]
                                : 0);
    }
  }
  cout << dp[n][w] << endl;
}

Error: -11

Fixed submission:

#include <cstring>
#include <iostream>

using namespace std;

int dp[100][10001];
int value[100];
int weight[100];
int NEGA_INF = -10000000;

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

  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < w; j++) {
      dp[j][i] =
          max(dp[j - 1][i],
              i - weight[j] > 0 ? dp[j - 1][i - weight[j]] + value[j] : 0);
    }
  }
  cout << dp[n - 1][w] << endl;
}
Problem p02315 (C++) — buggy submission, verdict: Runtime Error

#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];
  }
  return memo[i][j] =
             std::max(dp(i - 1, j), dp(i - 1, j - w[i - 1]) + v[i - 1]);
}

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

Error: 0

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 memo[i][j] = -INF;
  }
  if (i == 0) {
    return memo[i][j] = 0;
  }
  if (memo[i][j] != -1) {
    return memo[i][j];
  }
  return memo[i][j] =
             std::max(dp(i - 1, j), dp(i - 1, j - w[i - 1]) + v[i - 1]);
}

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 p02315 (C++) — buggy submission, verdict: Time Limit Exceeded

// メモ化再帰
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DPL_1_B&lang=jp
#include <algorithm>
#include <iostream>
#include <vector>
#define MAX_N 101
#define MAX_W 10001

using namespace std;
int n, W;
int w[MAX_N];
int v[MAX_N];
int dp[MAX_N + 1][MAX_W + 1]; // メモ化テーブル
// i番目以降の品物から重さの総和がj以下となるように選ぶ
int rec(int i, int j) {
  if (dp[i][j] >= 0) {
    // すでに調べたことがあるならばその結果を再利用
    return dp[i][j];
  }
  int 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() {
  cin >> n >> W;
  for (int i = 0; i < n; i++) {
    cin >> v[i] >> w[i];
  }
  for (int i = 0; i < MAX_N; i++) {
    for (int j = 0; j < MAX_W; j++) {
      dp[i][j] = -1;
    }
  }
  cout << rec(0, W) << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
#define MAX_N 101
#define MAX_W 10001

using namespace std;
int n, W;
int w[MAX_N];
int v[MAX_N];

// i番目以降の荷物から重さの総和がj以下になるように選ぶ
int rec(int i, int j) {
  int 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 res;
}
int main() {
  cin >> n >> W;
  for (int i = 0; i < n; i++) {
    cin >> v[i] >> w[i];
  }
  cout << rec(0, W) << endl;
}
Problem p02315 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define MAX_N 101
#define MAX_W 100001
long long dp[MAX_N][MAX_W];

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


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define MAX_N 100
#define MAX_W 100001
long long dp[MAX_N][MAX_W];

int main() {
  long long N;
  cin >> N;
  long long W;
  cin >> W;
  vector<vector<long long>> v(N, vector<long long>(2));
  for (long long i = 0; i < N; i++) {
    cin >> v[i][0] >> v[i][1];
  }
  for (long long i = 0; i <= W; i++) {
    dp[0][i] = 0;
  }
  for (long long i = 0; i < N; i++) {
    for (long long j = 0; j <= W; j++) {
      if (j < v[i][1]) {
        dp[i + 1][j] = dp[i][j];
      } else {
        dp[i + 1][j] = max(dp[i][j], dp[i][j - v[i][1]] + v[i][0]);
      }
    }
  }
  cout << dp[N][W] << endl;
  return 0;
}
Problem p02315 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)

#define V 1000

int v[101];
int w[101];

int dp[1001][10001];

int N;
int W;

int dfs(int i, int j) {

  if (i == N) {
    return 0;
  } else if (j < w[i]) {
    return dfs(i + 1, j);
  } else if (dp[i][j] != -1) {
    return dp[i][j];
  } else {

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

int main() {

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

  cin >> N >> W;

  REP(i, N) { cin >> v[i] >> w[i]; }

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

  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)

#define MAX_W 100
#define V 1000

int v[1000];
int w[1000];

int dp[1001][1001];

int N;
int W;

int dfs(int i, int j) {

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

  if (i == N) {
    return 0;
  } else if (j < w[i]) {
    return dfs(i + 1, j);
  } else if (dp[i][j] != -1) {
    return dp[i][j];
  } else {

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

int main() {

  cin >> N >> W;

  REP(i, N) { cin >> v[i] >> w[i]; }

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

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

#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

// #define int long long
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

int dp[2][10010];

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, W;
  cin >> N >> W;
  vector<int> v(N), w(N);
  rep(i, 0, N) { cin >> v[i] >> w[i]; }
  memset(dp, 0, sizeof(dp));
  rep(i, 0, N) {
    rep(j, 0, W + 1) {
      chmax(dp[(i + 1) & 1][j], dp[i & 1][j]);
      if (j + w[i] > W)
        continue;
      chmax(dp[(i + 1) & 1][j + w[i]], dp[i & 1][j] + v[i]);
    }
  }
  cout << dp[N & 1][W] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

// #define int long long
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

int dp[2][10010];

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, W;
  cin >> N >> W;
  vector<int> v(N), w(N);
  rep(i, 0, N) { cin >> v[i] >> w[i]; }
  memset(dp, 0, sizeof(dp));
  rep(i, 0, N) {
    rep(j, 0, W + 1) {
      chmax(dp[(i + 1) & 1][j + w[i]], dp[i & 1][j] + v[i]);
      chmax(dp[(i + 1) & 1][j], dp[i & 1][j]);
    }
  }
  cout << dp[N & 1][W] << endl;
  return 0;
}
Problem p02315 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
using namespace std;

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

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

  int res;
  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];
  }
  for (int i = 0; i < 101; i++) {
    for (int j = 0; j < 10001; j++) {
      dp[i][j] = -1;
    }
  }
  cout << func(0, W) << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
using namespace std;

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

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

  int res;
  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];
  }
  for (int i = 0; i < 10000; i++) {
    for (int j = 0; j < 10000; j++) {
      dp[i][j] = -1;
    }
  }
  cout << func(0, W) << endl;
  return 0;
}
Problem p02315 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int main() {
  int dp[120][10001] = {0}, w[1000] = {0}, k[1000] = {0}, N, M, i, j;
  cin >> N >> M;
  for (i = 1; i <= N; i++) {
    cin >> k[i] >> w[i];
  }
  for (i = 1; i <= N; i++) {
    for (j = 1; j <= M; j++) {
      if (i == 1) {
        if (j >= w[i]) {
          dp[i][j] = k[i];
          if (dp[i][j] <= dp[i - 1][j]) {
            dp[i][j] = dp[i - 1][j];
          }
        }

      } else {
        if (dp[i][j] <= dp[i - 1][j]) {
          dp[i][j] = dp[i - 1][j];
        }
        if (j >= w[i]) {
          if (k[i] + dp[i - 1][j - w[i]] > dp[i][j]) {
            dp[i][j] = k[i] + dp[i - 1][j - w[i]];
          }
        }
      }
    }
  }
  cout << dp[N][M] << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int dp[1000][10010] = {0}, w[10000] = {0}, k[10000] = {0}, N, M, i, j;
  cin >> N >> M;
  for (i = 1; i <= N; i++) {
    cin >> k[i] >> w[i];
  }
  for (i = 1; i <= N; i++) {
    for (j = 1; j <= M; j++) {
      if (i == 1) {
        if (j >= w[i]) {
          dp[i][j] = k[i];
          if (dp[i][j] <= dp[i - 1][j]) {
            dp[i][j] = dp[i - 1][j];
          }
        }

      } else {
        if (dp[i][j] <= dp[i - 1][j]) {
          dp[i][j] = dp[i - 1][j];
        }
        if (j >= w[i]) {
          if (k[i] + dp[i - 1][j - w[i]] > dp[i][j]) {
            dp[i][j] = k[i] + dp[i - 1][j - w[i]];
          }
        }
      }
    }
  }
  cout << dp[N][M] << endl;
  return 0;
}
Problem p02316 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int N, W;
vector<int> v, w;
vector<vector<int>> dp;

/*
int dfs(int pos, int cap) {
        if (pos == N) return 0;
        if (dp[pos][cap] != -1) return dp[pos][cap];
        int& res = dp[pos][cap];
        if (w[pos] > cap) res = dfs(pos+1, cap);
        else res = max(dfs(pos+1, cap), dfs(pos, cap-w[pos])+v[pos]);
        return res;
}
*/

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

Error: TLE

Fixed submission:

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

int N, W;
vector<int> v, w;
vector<vector<int>> dp;

int dfs(int pos, int cap) {
  if (pos == N)
    return 0;
  if (dp[pos][cap] != -1)
    return dp[pos][cap];
  int res = 0;
  for (auto i = 0; w[pos] * i <= cap; i++)
    res = max(res, v[pos] * i + dfs(pos + 1, cap - w[pos] * i));
  return dp[pos][cap] = res;
}

int main() {
  cin >> N >> W;
  v.resize(N);
  w.resize(N);
  for (auto i = 0; i < N; i++)
    cin >> v[i] >> w[i];
  dp = vector<vector<int>>(N, vector<int>(W + 1, -1));
  cout << dfs(0, W) << endl;
  return 0;
}
Problem p02316 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> int_pair;
typedef pair<ll, ll> ll_pair;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
const int N = 100;
const int W = 10000;
int n, w, v[N], wi[N];
int dp[N + 1][W + 1];

int rec(int i, int j) {
  if (dp[i][j] != -1) {
    return dp[i][j];
  }
  int res;
  if (i == n) {
    res = 0;
  } else if (j < wi[i]) {
    res = rec(i + 1, j);
  } else {
    int a = rec(i + 1, j);
    int b = rec(i, j - wi[i]) + v[i];
    res = max(a, b);
  }
  return dp[i][j] = res;
}

int main() {
  FOR(i, 0, N + 1) FOR(j, 0, W + 1) dp[i][j] = -1;
  cin >> n >> w;
  FOR(i, 0, n) { cin >> v[i] >> wi[i]; }
  cout << rec(0, w) << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> int_pair;
typedef pair<ll, ll> ll_pair;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
const int N = 100;
const int W = 10000;
int n, w, v[N], wi[N];
int dp[N + 1][W + 1];

int rec(int i, int j) {
  if (dp[i][j] != -1) {
    return dp[i][j];
  }
  int res;
  if (i == n) {
    res = 0;
  } else if (j < wi[i]) {
    res = rec(i + 1, j);
  } else {
    int a = rec(i + 1, j);
    int b = rec(i + 1, j - wi[i]) + v[i];
    int k = 2;
    while (j - wi[i] * k >= 0) {
      int c = rec(i + 1, j - wi[i] * k) + v[i] * k;
      if (c > b) {
        b = c;
      }
      k++;
    }
    res = max(a, b);
  }
  return dp[i][j] = res;
}

int main() {
  FOR(i, 0, N + 1) FOR(j, 0, W + 1) dp[i][j] = -1;
  cin >> n >> w;
  FOR(i, 0, n) { cin >> v[i] >> wi[i]; }
  cout << rec(0, w) << endl;
  return 0;
}
Problem p02316 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N, W;
  // vector<vector<int>/*??????*/>??¨???????????¨>>????§????
  vector<vector<int>> item;
  cin >> N >> W;
  item.resize(N + 1);
  for (int i = 0; i <= W; i++) {
    item[0].push_back(0);
  }
  for (int i = 1; i <= N; i++) {
    int v, w;
    cin >> v >> w;
    for (int j = 0; j <= W; j++) {
      int Max = item[i - 1][j];
      if (j >= w) {
        Max = max(item[i][j - w] + v, item[i - 1][j]);
      }

      item[i].push_back(Max);
    }
  }
  cout << item[N][W] << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int N, W;
  // vector<vector<int>/*??????*/>??¨???????????¨>>????§????
  vector<vector<int>> item;
  cin >> N >> W;
  item.resize(N + 1);
  for (int i = 0; i <= W; i++) {
    item[0].push_back(0);
  }
  for (int i = 1; i <= N; i++) {
    int v, w;
    cin >> v >> w;
    for (int j = 0; j <= W; j++) {
      int tmp = j / w;
      int max = 0;
      for (int k = 0; k <= tmp; k++) {
        int a = v * k + item[i - 1][j - w * k];
        if (max < a) {
          max = a;
        }
      }
      item[i].push_back(max);
    }
  }
  cout << item[N][W] << endl;

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

#include <algorithm>
#include <iostream>
#include <string>
#include <utility>

using namespace std;

#define REP(var, count) for (int var = 0; var < count; var++)

int DP[110][10010]; // N???????????§????????????????¨?w

int main(void) {

  int N, W;
  cin >> N >> W;

  REP(i, N) {
    int v, w;
    cin >> v >> w;

    REP(c, W + 1) {
      if (c < w) {
        // ????????????????????????
        DP[i + 1][c] = DP[i][c];
      } else {
        DP[i + 1][c] = max({DP[i][c - w] + v, DP[i + 1][c - w] + v, DP[i][c]});
      }
    }
  }

  int ret = 0;
  REP(c, W + 1) { ret = max(ret, DP[N][c]); }

  cout << ret << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <utility>

using namespace std;

#define REP(var, count) for (int var = 0; var < count; var++)

int DP[110][10010]; // N???????????§????????????????¨?w

int main(void) {

  int N, W;
  cin >> N >> W;

  REP(i, N) {
    int v, w;
    cin >> v >> w;

    REP(c, W + 1) {
      DP[i + 1][c] = DP[i][c];
      for (int cnt = 1; cnt <= c / w; cnt++) {
        DP[i + 1][c] = max(DP[i][c - w * cnt] + v * cnt, DP[i + 1][c]);
      }
    }
  }

  int ret = 0;
  REP(c, W + 1) { ret = max(ret, DP[N][c]); }

  cout << ret << endl;
}