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

#include <iomanip>
#include <iostream>

using namespace std;

int value[100], weight[100];
int table[101][10001] = {};

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 w = 0; w <= W; ++w) {
      if (w >= weight[i - 1])
        table[i][w] =
            max(table[i - 1][w], value[i - 1] + table[i][w - weight[i - 1]]);
      else
        table[i][w] = table[i - 1][w];
    }
  }

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

Error: TLE

Fixed submission:

#include <iomanip>
#include <iostream>

using namespace std;

int value[100], weight[100];
int table[101][10001] = {};

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 w = 0; w <= W; ++w) {
      table[i][w] = table[i - 1][w];
      for (int n = 1; w >= n * weight[i - 1]; ++n) {
        table[i][w] = max(table[i][w], n * value[i - 1] +
                                           table[i - 1][w - n * weight[i - 1]]);
      }
    }
  }

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

#if 0

#endif

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

int N, W;
int v[110];
int w[110];
int V[110][10010];

int main() {
  cin >> N >> W;
  for (int i = 1; i <= N; i++)
    cin >> v[i] >> w[i];
  for (int i = 0; i <= N; i++) {
    for (int c = 0; c <= W; c++) {
      if (i == 0 || c == 0)
        V[i][c] = 0;
      else if (c - w[i] < 0)
        V[i][c] = V[i - 1][c];
      else {
        int a[3] = {v[i] + V[i][c - w[i]], v[i] + V[i - 1][c - w[i]],
                    V[i - 1][c]};
        V[i][c] = *max_element(a, a + 3);
      }
    }
  }
  cout << V[N][W] << endl;
}


Error: TLE

Fixed submission:

#if 0

#endif

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

int N, W;
int v[110];
int w[110];
int V[110][10010];

int main() {
  cin >> N >> W;
  for (int i = 1; i <= N; i++)
    cin >> v[i] >> w[i];
  for (int i = 0; i <= N; i++) {
    for (int c = 0; c <= W; c++) {
      if (i == 0 || c == 0)
        V[i][c] = 0;
      else if (c - w[i] < 0)
        V[i][c] = V[i - 1][c];
      else {
        int n = 1;
        int t = 0;
        while (c - w[i] * n >= 0) {
          t = max(t, v[i] * n + V[i - 1][c - w[i] * n]);
          n++;
        }
        V[i][c] = max(t, V[i - 1][c]);
      }
    }
  }
  cout << V[N][W] << endl;
}
Problem p02316 (C++) — buggy submission, verdict: Time Limit Exceeded

//
#include <bits/stdc++.h>

using namespace std;

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

  int w[n], v[n];

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

  int dp[20000], go[20000];
  memset(dp, 0, sizeof(dp));
  memset(go, -1, sizeof(go));

  for (int i = n - 1; i >= 0; i--) {
    for (int j = w[i]; j <= ww; j++) {
      dp[j] = max(dp[j], dp[j - w[i]] + v[i]);
    }
  }
  cout << dp[ww] << endl;
  // for(int i = 0;i <= ww;i++)printf("%3d ",i);
  // cout << endl;
  // for(int j = 0;j < n;j++){
  // 	for(int i = 0;i <= ww;i++){
  // 		printf("%3d ",dp[j][i]);
  // 	}
  // 	cout << endl;
  // }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

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

  int w[n], v[n];

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

  int dp[n + 1][20000], go[n + 1][20000];
  memset(dp, 0, sizeof(dp));
  memset(go, -1, sizeof(go));

  for (int i = n - 1; i >= 0; i--) {
    for (int j = 0; j <= ww; j++) {
      if (w[i] > j) {
        dp[i][j] = dp[i + 1][j];
      } else {
        dp[i][j] = dp[i + 1][j];
        for (int k = 1; k * w[i] <= j; k++) {
          if (dp[i + 1][j - k * w[i]] + k * v[i] > dp[i + 1][j] &&
              dp[i + 1][j - k * w[i]] + k * v[i] > dp[i][j]) {
            dp[i][j] = dp[i + 1][j - k * w[i]] + k * v[i];
          }
        }
      }
    }
  }
  cout << dp[0][ww] << endl;
  // for(int i = 0;i <= ww;i++)printf("%3d ",i);
  // cout << endl;
  // for(int j = 0;j < n;j++){
  // 	for(int i = 0;i <= ww;i++){
  // 		printf("%3d ",dp[j][i]);
  // 	}
  // 	cout << endl;
  // }

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

#include <bits/stdc++.h>
using namespace std;
int n, napw;
int w[100], v[100];
int memo[101][10001];
int solve(int i, int j) {

  if (memo[i][j] != -1)
    return memo[i][j];
  int ret;
  if (i == n)
    ret = 0;
  else if (j < w[i])
    ret = solve(i + 1, j);
  else {
    int retp[3] = {solve(i + 1, j), solve(i + 1, j - w[i]) + v[i],
                   solve(i, j - w[i]) + v[i]};
    sort(retp, retp + 3);
    ret = retp[2];
  }
  return memo[i][j] = ret;
}
int main() {

  cin >> n >> napw;
  for (int i = 0; i < n; ++i)
    cin >> v[i] >> w[i];
  memset(memo, -1, sizeof(memo));
  cout << solve(0, napw) << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, napw;
int w[1000], v[1000];
int solve(int i, int j) {

  int ret;
  if (i == n)
    ret = 0;
  else if (j < w[i])
    ret = solve(i + 1, j);
  else {
    int retp[3] = {solve(i + 1, j), solve(i + 1, j - w[i]) + v[i],
                   solve(i, j - w[i]) + v[i]};
    sort(retp, retp + 3);
    ret = retp[2];
  }
  return ret;
}
int main() {

  cin >> n >> napw;
  for (int i = 0; i < n; ++i)
    cin >> v[i] >> w[i];
  cout << solve(0, napw) << endl;
}
Problem p02316 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int main() {
  int n, mw;
  int v[1005], w[1005];
  cin >> n >> mw;
  for (int i = 0; i < n; i++) {
    cin >> v[i] >> w[i];
  }
  int dp[mw + 2][n];
  fill(&dp[0][0], &dp[mw + 1][n - 1], 0);
  for (int i = 1; i <= mw; i++) {
    for (int j = 0; j < n; j++) {
      dp[i][j] = i - w[j] >= 0 ? max(dp[i][j - 1], dp[i - w[j]][j] + v[j])
                               : dp[i][j - 1];
    }
  }
  cout << dp[mw][n - 1] << endl;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
int dp[1001][10001];
int main() {
  int n, mw;
  int v[1005], w[1005];
  cin >> n >> mw;
  for (int i = 0; i < n; i++) {
    cin >> v[i] >> w[i];
  }
  for (int i = 1; i <= mw; i++) {
    for (int j = 0; j < n; j++) {
      dp[i][j] = i - w[j] >= 0 ? max(dp[i][j - 1], dp[i - w[j]][j] + v[j])
                               : dp[i][j - 1];
    }
  }
  cout << dp[mw][n - 1] << endl;
}
Problem p02316 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>

int dp[101][10001];

int main() {
  int n, w;
  scanf("%d %d\\n", &n, &w);
  int val[n], wei[n];
  for (int i = 1; i <= n; i++) {
    scanf("%d %d\\n", &val[i], &wei[i]);
  }
  // init. dp table
  for (int i = 0; i <= n; i++) {
    dp[i][0] = 0;
  }
  for (int j = 0; j <= w; j++) {
    dp[0][j] = 0;
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= w; j++) {
      if (wei[i] <= j) {
        dp[i][j] = std::max(dp[i][j - wei[i]] + val[i], dp[i - 1][j]);
      } else {
        dp[i][j] = dp[i - 1][j];
      }
    }
  }

  printf("%d\\n", dp[n][w]);
  /*
      for(int i=0; i<=n; i++){
          for(int j=0; j<=w; j++){
              printf("%d ",dp[i][j]);
          }
          printf("\\n");
      }
  */

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>

int dp[101][10001];

int main() {
  int n, w;
  scanf("%d %d\\n", &n, &w);
  int val[n], wei[n];
  for (int i = 1; i <= n; i++) {
    scanf("%d %d\\n", &val[i], &wei[i]);
  }
  // init. dp table
  for (int i = 0; i <= n; i++) {
    dp[i][0] = 0;
  }
  for (int j = 0; j <= w; j++) {
    dp[0][j] = 0;
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= w; j++) {
      if (wei[i] <= j) {
        dp[i][j] = std::max(dp[i - 1][j - wei[i]] + val[i], dp[i - 1][j]);
        for (int k = 2;; k++) {
          if (k * wei[i] <= j) {
            dp[i][j] =
                std::max(dp[i - 1][j - k * wei[i]] + k * val[i], dp[i][j]);
          } else {
            break;
          }
        }
      } else {
        dp[i][j] = dp[i - 1][j];
      }
    }
  }

  printf("%d\\n", dp[n][w]);
  /*
      for(int i=0; i<=n; i++){
          for(int j=0; j<=w; j++){
              printf("%d ",dp[i][j]);
          }
          printf("\\n");
      }
  */

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

#include <bits/stdc++.h>
using namespace std;
int N, W;
int v[1001], w[1001];
int a[101][10001];
int c;
int main() {
  cin >> N >> W;
  for (int i = 1; i <= N; i++) {
    cin >> v[i] >> w[i];
  }
  for (int i = 0; i <= N; i++) {
    a[i][0] = 0;
  }
  for (int i = 0; i <= W; i++) {
    a[0][i] = 0;
  }
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= W; j++) {
      if (w[i] <= j) {
        a[i][j] = max(a[i - 1][j], a[i][j - w[i]] + v[i]);
      } else {
        a[i][j] = a[i - 1][j];
      }
    }
  }
  cout << a[N][W] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
string x, y;
int N, W;
int v[1001], w[1001];
int a[101][10001];
int c;
int main() {
  cin >> N >> W;
  for (int i = 1; i <= N; i++) {
    cin >> v[i] >> w[i];
  }
  for (int i = 0; i <= N; i++) {
    a[i][0] = 0;
  }
  for (int i = 0; i <= W; i++) {
    a[0][i] = 0;
  }
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= W; j++) {
      c = j / w[i];
      a[i][j] = a[i - 1][j];
      if (c > 0) {
        for (int k = 1; k <= c; k++) {
          a[i][j] = max(a[i][j], a[i - 1][j - (k * w[i])] + k * v[i]);
        }
      }
    }
  }
  cout << a[N][W] << endl;
  return 0;
}
Problem p02316 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
#include <map>
using namespace std;
int main(void) {
  int n, W;
  cin >> n >> W;
  // int v[n];
  // int w[n];
  map<int, int> bests;
  for (int i = 0; i < n; i++) {
    int v, w;
    cin >> v >> w;
    if (bests.find(w) == bests.end()) {
      bests[w] = v;
    } else if (bests[w] < v) {
      bests[w] = v;
    }
  }
  n = bests.size();
  int v[n], w[n];
  int index = 0;
  auto itr = bests.begin();
  while (itr != bests.end()) {
    w[index] = itr->first;
    v[index] = itr->second;
    itr++;
    index++;
  }
  int table[n + 1][W + 1];
  for (int i = 0; i < n + 1; i++) {
    for (int j = 0; j < W + 1; j++) {
      table[i][j] = 0;
    }
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < W + 1; j++) {
      if (j < w[i - 1]) {
        table[i][j] = table[i - 1][j];
      } else {
        int c = (j - (j % w[i - 1])) / w[i - 1];
        int result = table[i - 1][j];

        for (int k = 1; k <= c; k++) {
          if (table[i - 1][j] < table[i - 1][j - w[i - 1] * k] + v[i - 1] * k) {
            int tempResult = table[i - 1][j - w[i - 1] * k] + v[i - 1] * k;
            result = max(tempResult, result);
          }
        }
        table[i][j] = result;
      }
    }
  }
  cout << table[n][W] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
using namespace std;
int main(void) {
  int n, W;
  cin >> n >> W;
  int v[n];
  int w[n];
  for (int i = 0; i < n; i++) {
    cin >> v[i] >> w[i];
  }
  int table[n + 1][W + 1];
  for (int i = 0; i < n + 1; i++) {
    for (int j = 0; j < W + 1; j++) {
      table[i][j] = 0;
    }
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < W + 1; j++) {
      if (j < w[i - 1]) {
        table[i][j] = table[i - 1][j];
      } else {
        int c = (j - (j % w[i - 1])) / w[i - 1];
        int result = table[i - 1][j];
        for (int k = 1; k <= c; k++) {
          if (table[i - 1][j] < table[i - 1][j - w[i - 1] * k] + v[i - 1] * k) {
            int tempResult = table[i - 1][j - w[i - 1] * k] + v[i - 1] * k;
            result = max(tempResult, result);
          }
        }
        table[i][j] = result;
      }
    }
  }
  cout << table[n][W] << endl;
  return 0;
}
Problem p02316 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int main() {
  int N, W;
  cin >> N >> W;
  int item[101][2];
  item[0][0] = 0;
  item[0][1] = 0;
  for (int i = 1; i < N + 1; ++i) {
    int v, w;
    cin >> v >> w;
    item[i][0] = v;
    item[i][1] = w;
  }
  int c[101][10001];
  for (int i = 0; i < W + 1; i++) {
    c[0][i] = 0;
  }
  for (int j = 0; j < N + 1; j++) {
    c[j][0] = 0;
  }
  for (int i = 1; i < N + 1; ++i) {
    int v = item[i][0];
    int w = item[i][1];
    for (int j = 1; j < W + 1; ++j) {
      if (j < w) {
        c[i][j] = c[i - 1][j];
      } else {
        c[i][j] = max(c[i - 1][j], c[i][j - w] + v);
      }
    }
  }
  cout << c[N][W] << endl;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int N, W;
  cin >> N >> W;
  int item[101][2];
  item[0][0] = 0;
  item[0][1] = 0;
  for (int i = 1; i < N + 1; ++i) {
    int v, w;
    cin >> v >> w;
    item[i][0] = v;
    item[i][1] = w;
  }
  int c[101][10001];
  for (int i = 0; i < W + 1; i++) {
    c[0][i] = 0;
  }
  for (int j = 0; j < N + 1; j++) {
    c[j][0] = 0;
  }
  for (int i = 1; i < N + 1; ++i) {
    int v = item[i][0];
    int w = item[i][1];
    for (int j = 1; j < W + 1; ++j) {
      if (j < w) {
        c[i][j] = c[i - 1][j];
      } else {
        int tmp = c[i - 1][j];
        int max_n = j / w;
        for (int k = 1; k < max_n + 1; ++k) {
          if (tmp < c[i - 1][j - w * k] + v * k) {
            tmp = c[i - 1][j - w * k] + v * k;
          }
        }
        c[i][j] = tmp;
      }
    }
  }
  cout << c[N][W] << endl;
}
Problem p02316 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;
typedef struct {
  int val, w;
  double v;
} knap;
int main(void) {
  int n, lw;
  cin >> n >> lw;
  vector<int> v(n), w(n);
  vector<vector<int>> val(lw + 1, vector<int>(n + 1, 0));
  for (int i = 0; i < n; i++) {
    cin >> v[i] >> w[i];
  }

  for (int i = 0; i < n; i++) {
    for (int j = 1; j < lw + 1; j++) {
      if (j >= w[i]) {

        val[j][i + 1] = max(val[j][i], val[j - w[i]][i + 1] + v[i]);
      } else {
        val[j][i + 1] = val[j][i];
      }
    }
  }
  cout << val[lw][n] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
int main(void) {
  int n, lw;
  cin >> n >> lw;
  vector<int> v(n, 0), w(n, 0);
  vector<vector<int>> val(lw + 1, vector<int>(2, 0));
  for (int i = 0; i < n; i++) {
    cin >> v[i] >> w[i];
  }
  for (int i = 0; i < n; i++) {
    for (int k = 0; k < lw / w[i]; k++) {
      for (int j = 1; j <= lw; j++) {
        if (j >= w[i])
          val[j][1] = max(val[j][0], val[j - w[i]][0] + v[i]);
        else
          val[j][1] = val[j][0];
      }
      for (int j = 0; j <= lw; j++) {
        val[j][0] = val[j][1];
      }
    }
  }
  cout << val[lw][0] << endl;

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

#include <iostream>
// #define int long long
#define SIZE 101
#define VALUE 10001
// #define _DEBUG
using namespace std;
int N, W, Vs[SIZE], Ws[SIZE], T[SIZE][VALUE];
int solve() {
  for (int i = 0; i <= N; i++) {
    for (int w = 0; w <= W; w++) {
      if (i == 0 || w == 0) {
        T[i][w] = 0;
      } else if (w < Ws[i - 1]) {
        T[i][w] = T[i - 1][w];
      } else {
        T[i][w] =
            max(T[i - 1][w],
                max(T[i - 1][w - Ws[i - 1]], T[i][w - Ws[i - 1]]) + Vs[i - 1]);
      }
    }
  }
#ifdef _DEBUG
  for (int i = 0; i <= N; i++) {
    if (i > 0)
      cout << Vs[i - 1] << "," << Ws[i - 1] << "\\t";
    else
      cout << "\\t";
    for (int w = 0; w <= W; w++) {
      cout << T[i][w] << " ";
    }
    cout << endl;
  }
#endif
  return T[N][W];
}
signed main() {
  while (cin >> N >> W) {
    for (int i = 0; i < N; i++) {
      cin >> Vs[i] >> Ws[i];
    }
    cout << solve() << endl;
  }
}


Error: TLE

Fixed submission:

#include <stdio.h>
// #include <iostream>
// #define int long long
#define SIZE 101
#define VALUE 10001
// #define _DEBUG
using namespace std;
int N, W, Vs[SIZE], Ws[SIZE], T[SIZE][VALUE];
int solve() {
  for (int i = 0; i <= N; i++) {
    for (int w = 0; w <= W; w++) {
      if (i == 0 || w == 0) {
        T[i][w] = 0;
      } else {
        int m = T[i - 1][w];
        for (int k = 1; Ws[i - 1] * k <= w; k++) {
          m = ((m > T[i - 1][w - Ws[i - 1] * k] + Vs[i - 1] * k)
                   ? m
                   : T[i - 1][w - Ws[i - 1] * k] + Vs[i - 1] * k);
        }
        T[i][w] = m;
      }
    }
  }
#ifdef _DEBUG
  for (int i = 0; i <= N; i++) {
    if (i > 0)
      cout << Vs[i - 1] << "," << Ws[i - 1] << "\\t";
    else
      cout << "\\t";
    for (int w = 0; w <= W; w++) {
      cout << T[i][w] << " ";
    }
    cout << endl;
  }
#endif
  return T[N][W];
}
signed main() {
  // while(
  scanf("%d%d", &N, &W); //){
  for (int i = 0; i < N; i++) {
    scanf("%d%d", &Vs[i], &Ws[i]);
  }
  printf("%d\\n", solve());
  //}
}
Problem p02316 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

int val[110], weight[10010];
int res[110][10010];

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

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

  // 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(res[i - 1][j], val[i - 1] + res[i][j - weight[i - 1]]);
      }
    }
  }
  // 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;
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>

using namespace std;

int val[110], weight[10010];
int res[110][10010];

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

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

  // 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 {
        int resmax = 0;
        for (int l = 1; weight[i - 1] * l < j + 1; l++)
          if (resmax < max((val[i - 1] * l + res[i - l][j - weight[i - 1] * l]),
                           res[i - 1][j])) {

            resmax = max((val[i - 1] * l + res[i - l][j - weight[i - 1] * l]),
                         res[i - 1][j]);
          }
        res[i][j] = resmax;
      }
    }
  }
  // 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;
  return 0;
}
Problem p02316 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N, W, x, y, j;
  cin >> N >> W;
  vector<int> value;
  vector<int> weight;
  for (int i = 0; i < N; i++) {
    cin >> x >> y;
    for (j = 0; j < value.size(); j++) {
      if (weight[j] == y) {
        if (value[j] <= x) {
          value[j] = x;
        }
        break;
      }
    }
    if (j == value.size()) {
      value.push_back(x);
      weight.push_back(y);
    }
  }

  N = value.size();
  /*
  for (int k=0;k < value.size();k++){
          cout<<weight[k] <<endl;
  }
  */

  int table[W + 1][N];
  for (int i = 0; i < W + 1; i++) {
    if (i == 0) {
      table[i][0] = 0;
    } else if (i % weight[0] == 0) {
      table[i][0] = value[0] * (i / weight[0]);
    } else {
      table[i][0] = -1;
    }
  }

  for (int j = 1; j < N; j++) {
    table[0][j] = 0;
    for (int i = 1; i < W + 1; i++) {
      int k = 0;
      int val = -1;
      while (i - k * weight[j] >= 0) {
        if (val < table[i - k * weight[j]][j - 1] + k * value[j]) {
          val = table[i - k * weight[j]][j - 1] + k * value[j];
        }
        k++;
      }
      table[i][j] = val;
    }
  }

  int res = 0;
  for (int i = 0; i < W + 1; i++) {
    if (res < table[i][N - 1]) {
      res = table[i][N - 1];
    }
  }
  cout << res << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int N, W, x, y, j;
  cin >> N >> W;
  vector<int> value;
  vector<int> weight;
  for (int i = 0; i < N; i++) {
    cin >> x >> y;
    for (j = 0; j < value.size(); j++) {
      if (weight[j] == y) {
        if (value[j] <= x) {
          value[j] = x;
        }
        break;
      }
    }
    value.push_back(x);
    weight.push_back(y);
  }

  N = value.size();

  int table[W + 1][N];
  for (int i = 0; i < W + 1; i++) {
    if (i == 0) {
      table[i][0] = 0;
    } else if (i % weight[0] == 0) {
      table[i][0] = value[0] * (i / weight[0]);
    } else {
      table[i][0] = -1;
    }
  }

  for (int j = 1; j < N; j++) {
    table[0][j] = 0;
    for (int i = 1; i < W + 1; i++) {
      int k = 0;
      int val = -1;
      while (i - k * weight[j] >= 0) {
        if (val < table[i - k * weight[j]][j - 1] + k * value[j]) {
          val = table[i - k * weight[j]][j - 1] + k * value[j];
        }
        k++;
      }
      table[i][j] = val;
    }
  }

  int res = 0;
  for (int i = 0; i < W + 1; i++) {
    if (res < table[i][N - 1]) {
      res = table[i][N - 1];
    }
  }
  cout << res << endl;

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


#include <iostream>
using namespace std;

int N = 0; //??°
int W = 0; //?????§???
int v[100000], w[10000];

int main() {
  cin >> N >> W;
  int DP[N + 1][W + 1];

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

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

      DP[j][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][j - w[i - 1]] + v[i - 1]) //???:???????¶????
      {
        DP[i][j] = DP[i - 1][j];

      } else {
        DP[i][j] =
            DP[i][j - w[i - 1]] +
            v[i -
              1]; // DP[i][j]
                  // ?????????????????´DP[i][j-w[i-1]],i?????????N????????§v[i-1]
      }
    }
  }

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

  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
using namespace std;

int N; //??°
int W; //?????§???
int v[100000], w[10000];
int DP[10000][10000];

int main() {
  cin >> N >> W;
  for (int j = 0; j < N; j++) {
    cin >> v[j];
    cin >> w[j];
  }

  for (int i = 0; i < 10000; i++) {
    for (int j = 0; j < 10000; j++) {

      DP[j][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][j - w[i - 1]] + v[i - 1]) //???:???????¶????
      {
        DP[i][j] = DP[i - 1][j];

      } else {
        DP[i][j] =
            DP[i][j - w[i - 1]] +
            v[i -
              1]; // DP[i][j]
                  // ?????????????????´DP[i][j-w[i-1]],i?????????N????????§v[i-1]
      }
    }
  }

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

  return 0;
}