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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int N, W;
int dp[110][10010];
vector<int> v(1100);
vector<int> w(1100);
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() {
  memset(dp, -1, sizeof(dp));
  cin >> N >> W;
  rep(i, N) { cin >> v[i] >> w[i]; }
  cout << rec(0, W) << endl;
  return 0;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int N, W;
int dp[10010][10010];
vector<int> v(1100);
vector<int> w(1100);
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() {
  memset(dp, -1, sizeof(dp));
  cin >> N >> W;
  rep(i, N) { cin >> v[i] >> w[i]; }
  cout << rec(0, W) << endl;
  return 0;
}
Problem p02315 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#define REP(i, n) for (int i = 0; i < n; i++)
using namespace std;
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, W;
  cin >> N >> W;
  int value[N];
  int weight[N];
  REP(n, N) {
    cin >> value[n];
    cin >> weight[n];
  }

  int dp[101][10002] = {{}};

  REP(n, N) {
    REP(w, W + 1) {
      weight[n] > w
          ? dp[n + 1][w] = dp[n][w]
          : dp[n + 1][w] = max(dp[n][w], dp[n][w - weight[n]] + value[n]);
    }
  }

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

  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#define REP(i, n) for (int i = 0; i < n; i++)
using namespace std;
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, W;
  cin >> N >> W;
  int value[N];
  int weight[N];
  REP(n, N) {
    cin >> value[n];
    cin >> weight[n];
  }

  int dp[100][10001] = {{}};

  REP(n, N) {
    REP(w, W + 1) {
      weight[n] > w
          ? dp[n + 1][w] = dp[n][w]
          : dp[n + 1][w] = max(dp[n][w], dp[n][w - weight[n]] + value[n]);
    }
  }

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

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

#include <iostream>
#define REP(i, n) for (int i = 0; i < n; i++)
using namespace std;
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, W;
  cin >> N >> W;
  int value[N];
  int weight[N];
  REP(n, N) {
    cin >> value[n];
    cin >> weight[n];
  }

  int dp[110][11000] = {{}};

  REP(n, N) {
    REP(w, W + 1) {
      weight[n] > w
          ? dp[n + 1][w] = dp[n][w]
          : dp[n + 1][w] = max(dp[n][w], dp[n][w - weight[n]] + value[n]);
    }
  }

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

  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#define REP(i, n) for (int i = 0; i < n; i++)
using namespace std;
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, W;
  cin >> N >> W;
  int value[N];
  int weight[N];
  REP(n, N) {
    cin >> value[n];
    cin >> weight[n];
  }

  int dp[100][10000] = {{}};

  REP(n, N) {
    REP(w, W + 1) {
      weight[n] > w
          ? dp[n + 1][w] = dp[n][w]
          : dp[n + 1][w] = max(dp[n][w], dp[n][w - weight[n]] + value[n]);
    }
  }

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

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

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <vector>

using namespace std;

vector<int> dp(100000);

int main() {

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

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <vector>

using namespace std;

vector<int> dp(100000);

int main() {

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

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

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

Error: 0

Fixed submission:

#include <stdio.h>
int dp[101][10001];
int main(void) {
  int n, w;
  int i, j;
  scanf("%d %d", &n, &w);
  for (i = 0; i <= n; i++) {
    for (j = 0; j <= w; j++) {
      dp[i][j] = -1;
    }
  }
  dp[0][0] = 0;
  int a, b;
  for (i = 0; i <= n; i++) {
    scanf("%d %d", &a, &b);
    for (j = 0; j <= w; j++) {
      if (dp[i][j] >= 0 && dp[i + 1][j + b] < dp[i][j] + a && j + b <= w) {
        dp[i + 1][j + b] = dp[i][j] + a;
      }
    }
    for (j = 0; j <= w; j++) {
      if (dp[i + 1][j] < dp[i][j]) {
        dp[i + 1][j] = dp[i][j];
      }
    } /*
     for(j=0;j<=w;j++){
             printf("%d ",dp[i][j]);
     }
     printf("\\n");*/
  }
  int max = 0;
  for (j = 0; j <= w; j++) {
    if (dp[n][j] > max) {
      max = dp[n][j];
    }
  }
  printf("%d\\n", max);
  return 0;
}
Problem p02315 (C++) — buggy submission, verdict: Runtime Error

// ------------------------------------
// Date:2018/ 3/ 2
// Problem:AOJ DPL 0-1 Knapsack Problem 1_b.cpp
//
// ------------------------------------

#include <bits/stdc++.h>

using namespace std;

#define EACH(i, a) for (auto &&i : a)
#define FOR(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define RFOR(i, a, b) for (int i = (int)b - 1; i >= (int)a; --i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
#define debug(x) cerr << #x << ":" << x << endl

typedef long long ll;

// static const int MOD = 1000000007;

// dp[今見てる品物の個数][まだ詰め込める量]
int dp[101][10101];

// v:value / w:weight
int v[1010];
int w[1010];

// N:品物の個数 / W:ナップサックの容量
int N, W;

int main() {
  // 初期化
  memset(dp, 0, sizeof(dp));
  memset(v, 0, sizeof(v));
  memset(w, 0, sizeof(w));
  scanf("%d %d", &N, &W);
  REP(i, N) { scanf("%d %d", &v[i], &w[i]); }

  RREP(num, N) {
    REP(weight, W + 1) {
      if (weight < w[num]) {
        // 見ている品物の重さが、まだ詰め込める量より多いとき
        dp[num][weight] = dp[num + 1][weight];
      } else {
        // 詰め込むことが可能な状態で、max(詰め込まない or 詰め込む
        dp[num][weight] =
            max(dp[num + 1][weight], dp[num + 1][weight - w[num]] + v[num]);
      }
    }
  }

  int ans = dp[0][W];
  printf("%d\\n", ans);

  return 0;
}


Error: 0

Fixed submission:

// ------------------------------------
// Date:2018/ 3/ 2
// Problem:AOJ DPL 0-1 Knapsack Problem 1_b.cpp
//
// ------------------------------------

#include <bits/stdc++.h>

using namespace std;

#define EACH(i, a) for (auto &&i : a)
#define FOR(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define RFOR(i, a, b) for (int i = (int)b - 1; i >= (int)a; --i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
#define debug(x) cerr << #x << ":" << x << endl

typedef long long ll;

// static const int MOD = 1000000007;

// dp[今見てる品物の個数][まだ詰め込める量]
int dp[101][10101];

// v:value / w:weight
int v[1010];
int w[1010];

// N:品物の個数 / W:ナップサックの容量
int N, W;

int main() {
  // 初期化
  memset(dp, 0, sizeof(dp));
  memset(v, 0, sizeof(v));
  memset(w, 0, sizeof(w));
  scanf("%d %d", &N, &W);
  REP(i, N) { scanf("%d %d", &v[i], &w[i]); }

  RREP(num, N + 1) {
    REP(weight, W + 1) {
      if (weight < w[num]) {
        // 見ている品物の重さが、まだ詰め込める量より多いとき
        dp[num][weight] = dp[num + 1][weight];
      } else {
        // 詰め込むことが可能な状態で、max(詰め込まない or 詰め込む
        dp[num][weight] =
            max(dp[num + 1][weight], dp[num + 1][weight - w[num]] + v[num]);
      }
    }
  }

  int ans = dp[0][W];
  printf("%d\\n", ans);

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

#include <iostream>
#include <vector>
using namespace std;
#define max(x, y) (x) > (y) ? (x) : (y)
int main() {
  int N, W;
  cin >> N;
  cin >> W;
  vector<int> f(20, 0);
  vector<int> value(10, 0);
  vector<int> weight(10, 0);
  for (int i = 1; i <= N; i++) {
    if (i >= 10) {
      value.push_back(0);
      weight.push_back(0);
    }
    cin >> value[i] >> weight[i];
  }
  if (W >= 20)
    for (int j = 20; j <= W; j++)
      f.push_back(0);
  for (int i = 1; i <= N; i++)
    for (int j = W; j >= weight[i]; j--)
      f[j] = max(f[j], f[j - weight[i]] + value[i]);
  cout << f[W] << endl;
  return 0;
}

Error: 13

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
#define max(x, y) (x) > (y) ? (x) : (y)
int main() {

  int N, W;
  cin >> N;
  cin >> W;
  vector<int> f(20, 0);
  vector<int> value(10, 0);
  vector<int> weight(10, 0);
  for (int i = 1; i <= N; i++) {
    if (i >= 10) {
      value.push_back(0);
      weight.push_back(0);
    }
    cin >> value[i] >> weight[i];
  }
  if (W >= 20)
    for (int j = 20; j <= W; j++)
      f.push_back(0);
  for (int i = 1; i <= N; i++)
    for (int j = W; j >= weight[i]; j--)
      f[j] = max(f[j], f[j - weight[i]] + value[i]);

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

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

vector<vector<int>> dp(100, vector<int>(10001, -1));
int N;
vector<int> v(100, 0), w(100, 0);

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

int main() {
  int W;
  cin >> N >> W;
  for (int i = 0; i < N; i++) {
    cin >> v[i] >> w[i];
  }
  cout << solve(0, W) << endl;
}

Error: TLE

Fixed submission:

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

#define PI 4 * atan(1)

int n, W, v[100], w[1000];

int func(int i, int j) {
  int a;
  if (i == n) {
    return 0;
  } else if (j < w[i]) {
    a = func(i + 1, j);
  } else {
    a = max(func(i + 1, j), func(i + 1, j - w[i]) + v[i]);
  }
  return a;
}

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

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

#if __has_include("print.hpp")
#include "print.hpp"
#endif

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

typedef long long ll;
typedef pair<int, int> p;

int n, w;
vector<vector<int>> dp(500, vector<int>(10500, -1));

vector<int> wv;
vector<int> vv;

int dfs(int step, int maxw) {
  int res = 0;
  if (dp[step][maxw] != -1)
    return dp[step][maxw];
  if (step == n)
    res = 0;
  else if (wv[step] > maxw)
    res = dfs(step + 1, maxw);
  else
    res = max(dfs(step + 1, maxw), dfs(step + 1, maxw - wv[step]) + vv[step]);
  return dp[step][maxw] = res;
}

int main() {
  cin >> n >> w;
  for (int i = 0; i < n; i++) {
    int tmp1, tmp2;
    cin >> tmp1 >> tmp2;
    vv.push_back(tmp1);
    wv.push_back(tmp2);
  }

  cout << dfs(0, w) << endl;
}


Error: MLE

Fixed submission:

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

#if __has_include("print.hpp")
#include "print.hpp"
#endif

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

typedef long long ll;
typedef pair<int, int> p;

int n, w;
vector<vector<int>> dp(n + 10000, vector<int>(w + 10000, -1));

vector<int> wv;
vector<int> vv;

int dfs(int step, int maxw) {
  int res = 0;
  if (dp[step][maxw] != -1)
    return dp[step][maxw];
  if (step == n)
    return res;
  if (wv[step] > maxw)
    res = dfs(step + 1, maxw);
  else
    res = max(dfs(step + 1, maxw), dfs(step + 1, maxw - wv[step]) + vv[step]);
  return dp[step][maxw] = res;
}

int main() {
  cin >> n >> w;
  for (int i = 0; i < n; i++) {
    int tmp1, tmp2;
    cin >> tmp1 >> tmp2;
    vv.push_back(tmp1);
    wv.push_back(tmp2);
  }

  cout << dfs(0, w) << endl;
}
Problem p02315 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <stdio.h>
#include <string>

using namespace std;

long long N, W, K, v[300], w[300];
long long dp[1000][10100];

int main() {

  cin >> N >> W;

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

  for (int i = 1; i <= W; i++)
    dp[N + 1][i] = 0;

  for (int i = N; i >= 1; i--) {
    for (int j = 0; j <= W; j++) {
      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]);
    }
  }

  /*
  for(int i=1;i<=N;i++){
    for(int j=0;j<=W;j++){
      printf("%4d",dp[i][j]);
    }
    cout<<endl;
  }
*/

  cout << dp[1][W] << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <stdio.h>
#include <string>

using namespace std;

long long N, W, K, v[300], w[300];
long long dp[100][10100];

int main() {

  cin >> N >> W;

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

  for (int i = 1; i <= W; i++)
    dp[N + 1][i] = 0;

  for (int i = N; i >= 1; i--) {
    for (int j = 0; j <= W; j++) {
      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]);
    }
  }

  /*
  for(int i=1;i<=N;i++){
    for(int j=0;j<=W;j++){
      printf("%4d",dp[i][j]);
    }
    cout<<endl;
  }
*/

  cout << dp[1][W] << endl;
}
Problem p02315 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
int i, j, a, b, c[101], d[101], e[102][10001], f = -1;
int main(void) {
  scanf("%d %d", &a, &b);
  for (i = 0; i <= a; i++) {
    for (j = 0; j <= b; j++) {
      e[i][j] = -1;
    }
  }
  for (i = 0; i < a; i++) {
    scanf("%d %d", &c[i], &d[i]);
  }
  e[0][0] = 0;
  for (i = 0; i < a; i++) {
    for (j = 0; j <= b; j++) {
      if (e[i][j] != -1) {
        if (e[i][j] + c[i] > e[i + 1][j + d[i]] && j + d[i] <= b) {
          e[i + 1][j + d[i]] = e[i][j] + c[i];
        }
        if (e[i][j] > e[i + 1][j]) {
          e[i + 1][j] = e[i][j];
        }
      }
    }
  }
  for (i = 0; i <= a; i++) {
    for (j = 0; j <= b; j++) {
      if (f < e[i][j]) {
        f = e[i][j];
      }
    }
  }
  printf("%d\\n", f);
  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>
int i, j, a, b, c[101], d[101], e[101][10001], f = -1;
int main(void) {
  scanf("%d %d", &a, &b);
  for (i = 0; i <= a; i++) {
    for (j = 0; j <= b; j++) {
      e[i][j] = -1;
    }
  }
  for (i = 0; i < a; i++) {
    scanf("%d %d", &c[i], &d[i]);
  }
  e[0][0] = 0;
  for (i = 0; i < a; i++) {
    for (j = 0; j <= b; j++) {
      if (e[i][j] != -1) {
        if (e[i][j] + c[i] > e[i + 1][j + d[i]] && j + d[i] <= b) {
          e[i + 1][j + d[i]] = e[i][j] + c[i];
        }
        if (e[i][j] > e[i + 1][j]) {
          e[i + 1][j] = e[i][j];
        }
      }
    }
  }
  for (i = 0; i <= a; i++) {
    for (j = 0; j <= b; j++) {
      if (f < e[i][j]) {
        f = e[i][j];
      }
    }
  }
  printf("%d\\n", f);
  return 0;
}
Problem p02315 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iostream>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

#define MP make_pair
typedef unsigned long long ULL;

// dp[i+][j]
// i番目まで使って重さj以下になるように選んだ時の最大価値
long dp[101][10001];

int main() {

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

  vector<int> v(N), w(N);
  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;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iostream>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

#define MP make_pair
typedef unsigned long long ULL;

// dp[i+][j]
// i番目まで使って重さj以下になるように選んだ時の最大価値
long dp[100][10001];

int main() {

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

  vector<int> v(N), w(N);
  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;

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

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define CI cin >>
#define CO cout <<
#define E << endl;

using namespace std;

typedef pair<int, int> P;
typedef pair<long, long> LP;
typedef pair<int, P> PP;
typedef pair<long, LP> LPP;

int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};

int A = 0, B = 0, K = 0, T = 0, W = 0, N = 0, H = 0;
int n = 0;
double L = 0;
double S = 0;
double ar = 0, br = 0, cr = 0;
int answer = 0;

string C;
string sA, strB;
vector<vector<char>> map;
vector<double> num;
vector<string> str;

int sum = 0;

vector<int> value;
vector<int> weight;

int dp[110][10010];

void input() {
  CI N >> W;

  int temp;

  for (int i = 0; i < N; i++) {
    CI temp;
    value.push_back(temp);
    CI temp;
    weight.push_back(temp);
  }

  return;
}

int main(void) {
  input();

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

  CO dp[N][W] E

      return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define CI cin >>
#define CO cout <<
#define E << endl;

using namespace std;

typedef pair<int, int> P;
typedef pair<long, long> LP;
typedef pair<int, P> PP;
typedef pair<long, LP> LPP;

int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};

int A = 0, B = 0, K = 0, T = 0, W = 0, N = 0, H = 0;
int n = 0;
double L = 0;
double S = 0;
double ar = 0, br = 0, cr = 0;
int answer = 0;

string C;
string sA, strB;
vector<vector<char>> map;
vector<double> num;
vector<string> str;

int sum = 0;

vector<int> value;
vector<int> weight;

int dp[100][10000];

void input() {
  CI N >> W;

  int temp;

  for (int i = 0; i < N; i++) {
    CI temp;
    value.push_back(temp);
    CI temp;
    weight.push_back(temp);
  }

  return;
}

int main(void) {
  input();

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

  CO dp[N][W] E

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

#include <algorithm>
#include <iostream>

using namespace std;

int main() {
  int N, W, v[101], w[101], dp[101][10001];
  cin >> N >> W;
  for (int i = 1; i <= N; i++)
    cin >> v[i] >> w[i];
  for (int i = 0; i < 10001; i++)
    dp[0][i] = 0;
  for (int i = 1; i <= N; i++) {
    for (int 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 - 1][j - w[i]] + v[i]);
    }
  }
  cout << dp[N][W] << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

int N, W, v[200], w[200];

int dfs(int w_now, int n, int val) {
  int best = 0;
  if (w_now > W)
    return 0;
  if (w_now == W || n == N)
    return val;
  for (int i = n + 1; i <= N; i++) {
    best = max(best, dfs(w_now + w[i], i, val + v[i]));
  }
  return best;
}

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

#include <algorithm>
#include <stdio.h>
using namespace std;
int p[10005][105];
int w[105];
int v[105];
int main() {
  int n;
  int W;
  int sum = 0;
  scanf("%d%d", &n, &W);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &v[i], &w[i]);
  }
  for (int i = 1; i <= n; i++)
    p[0][i] = 0;
  for (int i = 1; i <= W; i++) {
    for (int j = 1; j <= n; j++) {
      p[i][j] = p[i][j - 1];
      if (w[j] > i)
        continue;
      else
        p[i][j] = max(p[i][j - 1], p[i - w[j]][j - 1] + v[j]);
    }
  }
  printf("%d\\n", p[W][n]);
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <stdio.h>
using namespace std;
int p[105][10005];
int w[105];
int v[105];
int main() {
  int n;
  int W;
  int sum = 0;
  scanf("%d%d", &n, &W);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &v[i], &w[i]);
  }
  for (int i = 1; i <= n; i++)
    p[0][i] = 0;
  for (int i = 1; i <= W; i++) {
    for (int j = 1; j <= n; j++) {

      p[i][j] = p[i][j - 1];
      if (w[j] > i)
        continue;
      else
        p[i][j] = max(p[i][j - 1], p[i - w[j]][j - 1] + v[j]);
    }
  }
  printf("%d\\n", p[W][n]);
  return 0;
}
Problem p02315 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

const int maxn = 10005;
const int maxm = 1050;

int N, W;
int v[105], w[105];
int f[105][10050];

int main() {
  cin >> N >> W;
  for (int i = 1; i <= N; i++)
    cin >> v[i] >> w[i];
  memset(f, 0, sizeof(f));
  for (int i = 1; i <= N; i++)
    for (int j = 1; j <= W; j++) {
      f[i][j] = (i == 1 ? 0 : f[i - 1][j]);
      if (j >= w[i])
        f[i][j] = max(f[i][j], f[i - 1][j - w[i]] + v[i]);
    }
  cout << f[N][W] << endl;
  return 0;
}

Error: MLE

Fixed submission:

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

const int maxn = 10050;

int N, W;
int v[maxn], w[maxn];
int f[maxn][maxn];

int main() {
  cin >> N >> W;
  for (int i = 1; i <= N; i++)
    cin >> v[i] >> w[i];
  memset(f, 0, sizeof(f));
  for (int i = 1; i <= N; i++)
    for (int j = 1; j <= W; j++) {
      f[i][j] = (i == 1 ? 0 : f[i - 1][j]);
      if (j >= w[i])
        f[i][j] = max(f[i][j], f[i - 1][j - w[i]] + v[i]);
    }
  cout << f[N][W] << endl;
  return 0;
}