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

#include <algorithm>
#include <iostream>
#include <string.h>
using namespace std;
const int num = 10001, num1 = 101;
int dp[num1][num];
int w[num1], v[num1];
int n, W;
int main() {
  cin >> n >> W;
  for (int i = 0; i < n; i++)
    cin >> v[i] >> w[i];
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i < n; i++)
    for (int j = 0; j <= W; j++) {
      if (w[i] > j)
        dp[i + 1][j] = dp[i][j];
      else
        dp[i + 1][j] = max(dp[i][j], dp[i + 1][j - w[i]] + v[i]);
    }
  cout << dp[n][W] << endl;
  return 0;
}

Error: -11

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <algorithm>
#include <iostream>
using namespace std;
const int num = 10001, num1 = 101;
int dp[num1][num];
int w[num1], v[num1];
int n, W;
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++) {
      if (w[i] > j)
        dp[i + 1][j] = dp[i][j];
      else
        for (int k = 1; k <= j / w[i]; k++)
          dp[i + 1][j] = max(
              dp[i][j], max(dp[i + 1][j], dp[i - k][j - k * w[i]] + v[i] * k));
    }
  cout << dp[n][W] << endl;
  return 0;
}
Problem p02316 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N, W;
  cin >> N >> W;
  vector<vector<int>> vs(N + 1, vector<int>(W + 1, 0));
  for (int i = 1; i <= N; i++) {
    int v, w;
    cin >> v >> w;
    for (int j = 0; j <= W; j++) {
      vs[i][j] = vs[i - 1][j];
      if (j < w)
        continue;
      vs[i][j] = max(vs[i][j], vs[i][j - w] + v);
    }
  }

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

Error: TLE

Fixed submission:

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

int main() {
  int N, W;
  cin >> N >> W;
  vector<vector<int>> vs(N + 1, vector<int>(W + 1, -1));
  vs[0][0] = 0;
  for (int i = 1; i <= N; i++) {
    int v, w;
    cin >> v >> w;
    for (int j = 0; j <= W; j++) {
      if (vs[i - 1][j] < 0)
        continue;
      for (int k = 0; k * w + j <= W; k++) {
        vs[i][j + k * w] = max(vs[i][j + k * w], vs[i - 1][j] + k * v);
      }
    }
  }

  int res = 0;
  for (int i = 0; i <= W; i++) {
    res = max(res, vs[N][i]);
  }
  cout << res << endl;
}
Problem p02316 (C++) — buggy submission, verdict: Time Limit Exceeded

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

long long calc(int i, int c, const vector<int> &v, const vector<int> &w,
               vector<vector<long long>> &ans) {
  if (i <= -1 || c < 0) {
    return 0;
  } else if (c == 0) {
    ans[i][c] = 0;
  } else if (ans[i][c] >= 0) {
    return ans[i][c];
  } else if (c - w[i] < 0) {
    ans[i][c] = calc(i - 1, c, v, w, ans);
  } else {
    // int p = 1;
    // long long q = -1;
    // while(c - p*w[i] >= 0){
    //   q = max(
    //     q,
    //     p*v[i] + calc(i-1, c - p*w[i], v, w, ans)
    //   );
    //   p++;
    // }
    ans[i][c] =
        max(v[i] + calc(i, c - w[i], v, w, ans), calc(i - 1, c, v, w, ans));

    // ans[i][c] = max(
    //   v[i] + calc(i-1, c - w[i], v, w, ans),
    //   calc(i-1, c, v, w, ans)
    // );
  }
  return ans[i][c];
}

int main() {
  int n, c;
  cin >> n >> c;
  vector<int> v(n), w(n);
  for (int i = 0; i < n; i++)
    cin >> v[i] >> w[i];
  vector<vector<long long>> ans(n, vector<long long>(c + 1, -1));
  cout << calc(n - 1, c, v, w, ans) << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

long long calc(int i, int c, const vector<int> &v, const vector<int> &w,
               vector<vector<long long>> &ans) {
  if (i <= -1 || c < 0) {
    return 0;
  } else if (c == 0) {
    ans[i][c] = 0;
  } else if (ans[i][c] >= 0) {
    return ans[i][c];
  } else if (c - w[i] < 0) {
    ans[i][c] = calc(i - 1, c, v, w, ans);
  } else {
    int p = 1;
    long long q = -1;
    while (c - p * w[i] >= 0) {
      q = max(q, p * v[i] + calc(i - 1, c - p * w[i], v, w, ans));
      p++;
    }
    ans[i][c] = max(q, calc(i - 1, c, v, w, ans));

    // ans[i][c] = max(
    //   v[i] + calc(i-1, c - w[i], v, w, ans),
    //   calc(i-1, c, v, w, ans)
    // );
  }
  return ans[i][c];
}

int main() {
  int n, c;
  cin >> n >> c;
  vector<int> v(n), w(n);
  for (int i = 0; i < n; i++)
    cin >> v[i] >> w[i];
  vector<vector<long long>> ans(n, vector<long long>(c + 1, -1));
  cout << calc(n - 1, c, v, w, ans) << endl;
  return 0;
}
Problem p02316 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int N, W, value[101], weight[101], dp[101][10001];

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int N, W, value[101], weight[101], dp[101][10001];

int main() {
  cin >> N >> W;
  for (int i = 0; i < N; i++) {
    cin >> value[i] >> weight[i];
  }
  for (int i = 0; i < N; ++i) {
    for (int w = 0; w <= W; ++w) {
      for (int k = 0; k * weight[i] <= w; ++k) {
        dp[i + 1][w] =
            max(dp[i + 1][w], dp[i][w - k * weight[i]] + k * value[i]);
      }
    }
  }
  cout << dp[N][W] << endl;
}
Problem p02316 (C++) — buggy submission, verdict: Runtime Error

// ------------------------------------
// Date:2018/ 2/25
// Problem:AOJ DPL KnapsackProblem 1_a.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

using ll = long long;

#define MOD 1000000007

// dp[num][weight]
int dp[111][11111];

int main() {
  int N;
  int W;
  cin >> N >> W;
  // v:value, w:weight
  int v[111];
  int w[111];
  memset(dp, 0, sizeof(dp));
  memset(v, 0, sizeof(v));
  memset(w, 0, sizeof(w));
  REP(i, N) { cin >> v[i] >> w[i]; }
  REP(num, N + 1) {
    REP(weight, W + 1) {
      if (weight - w[num] >= 0) {
        // max(中に入れる or 中に入れない)
        dp[num + 1][weight] =
            max(dp[num][weight], dp[num + 1][weight - w[num]] + v[num]);
      } else {
        // これ以上入らないのでスルー
        dp[num + 1][weight] = dp[num][weight];
      }
    }
  }
  cout << dp[N][W] << endl;
  return 0;
}


Error: 0

Fixed submission:

// ------------------------------------
// Date:2018/ 2/25
// Problem:AOJ DPL KnapsackProblem 1_a.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

using ll = long long;

#define MOD 1000000007

// dp[num][weight]
int dp[101][10101];

int main() {
  int N;
  int W;
  cin >> N >> W;
  // v:value, w:weight
  int v[101];
  int w[101];
  REP(i, N) { cin >> v[i] >> w[i]; }
  dp[0][0] = 0;
  REP(num, N + 1) {
    REP(weight, W + 1) {
      if (weight - w[num] >= 0) {
        // max(中に入れる or 中に入れない)
        dp[num + 1][weight] =
            max(dp[num][weight], dp[num + 1][weight - w[num]] + v[num]);
      } else {
        // これ以上入らないのでスルー
        dp[num + 1][weight] = dp[num][weight];
      }
    }
  }
  cout << dp[N][W] << endl;
  return 0;
}
Problem p02316 (C++) — buggy submission, verdict: Runtime Error

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

Error: 0

Fixed submission:

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

#include <iostream>
using namespace std;

int V[110][10010]; // V[i][c] 品物iまでの世界でコスト制限cでの価値最大値

int main() {
  int N, M;
  cin >> N >> M;
  int v[110];
  int w[110];
  for (auto i = 0; i < N; i++) {
    cin >> v[i] >> w[i];
  }
  for (auto j = 0; j <= M; j++) {
    int k = j / w[0];
    V[0][j] = k * v[0];
  }
  for (auto i = 1; i < N; i++) {
    for (auto j = 0; j <= M; j++) {
      V[i][j] = V[i - 1][j];
      /* 次のように書くとTLEする。
      int k = 0;
      int tw;
      while ((tw = j - k*w[i]) >= 0) {
        V[i][j] = max(V[i][j], V[i-1][tw] + k*v[i]);
        k++;
      }
      これは、V[i][j-w[i]]を計算するときも、
      j方向に同じ計算をしていることになる。
      これに気づけば次のように書ける。
      */
      if (j - w[i] >= 0) {
        V[i][j] = max(V[i][j], V[i][j - w[i]] + v[i]);
      }
    }
  }
  cout << V[N - 1][M] << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int V[110][10010]; // V[i][c] 品物iまでの世界でコスト制限cでの価値最大値

int main() {
  int N, M;
  cin >> N >> M;
  int v[110];
  int w[110];
  for (auto i = 0; i < N; i++) {
    cin >> v[i] >> w[i];
  }
  for (auto j = 0; j <= M; j++) {
    int k = j / w[0];
    V[0][j] = k * v[0];
  }
  for (auto i = 1; i < N; i++) {
    for (auto j = 0; j <= M; j++) {
      V[i][j] = V[i - 1][j];
      int k = 0;
      int tw;
      while ((tw = j - k * w[i]) >= 0) {
        V[i][j] = max(V[i][j], V[i - 1][tw] + k * v[i]);
        k++;
      }
    }
  }
  cout << V[N - 1][M] << endl;
}
Problem p02316 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

using namespace std;

int main() {
  int N, W;

  scanf("%d", &N);
  scanf("%d", &W);

  vector<int> v(N);
  vector<int> w(N);

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

  vector<vector<int>> dp(N + 1, vector<int>(W + 1, 0));

  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= W; j++) {
      dp[i][j] = dp[i - 1][j];

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

  printf("%d\\n", dp[N][W]);

  return 0;
}

Error: MLE

Fixed submission:

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

using namespace std;

int main() {
  // ------ 1. Input ------ //

  int N, W;

  cin >> N >> W;

  vector<int> v(N);
  vector<int> w(N);

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

  int MIN_W = *min_element(w.begin(), w.end());

  vector<vector<int>> dp(W / MIN_W + 1, vector<int>(W + 1, 0));

  // ------ 2. DP Calculation ------ //

  for (int i = 1; i <= W / MIN_W; ++i) {
    for (int j = 0; j <= W; ++j) {
      for (int k = 0; k < N; ++k) {
        int temp;

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

        if (dp[i][j] < temp) {
          dp[i][j] = temp;
        }
      }
    }
  }

  // ------ 3. Output ------ //

  cout << dp[W / MIN_W][W] << endl;

  return 0;
}

// Time : O( N * W * MIN_W )
Problem p02316 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define ll long long
#define INF 1000000001
#define mod 1000000007
#define p pair<int, int>
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
using namespace std;
int POW(int x, int y) { return int(pow(double(x), double(y))); }

int main() {
  int n;
  cin >> n;
  int weight;
  cin >> weight;
  vector<int> w(n), v(n);
  rep(i, n) { cin >> v[i] >> w[i]; }

  vector<vector<int>> a(n);
  rep(i, n) {
    a[i].resize(weight + 1);
    rep(j, weight + 1) {
      if (i == 0) {
        a[0][j] = (j / w[0]) * v[0];
        /*
        if(j>=w[0])a[0][j]=v[0];
        else a[0][j]=0;
        */
      } else {
        if (j < w[i])
          a[i][j] = a[i - 1][j];
        else
          a[i][j] = max(a[i - 1][j],
                        max(a[i - 1][j - w[i]] + v[i], a[i][j - w[i]] + v[i]));
      }
    }
  }
  cout << a[n - 1][weight] << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define ll long long
#define INF 1000000001
#define mod 1000000007
#define p pair<int, int>
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
using namespace std;
int POW(int x, int y) { return int(pow(double(x), double(y))); }

int main() {
  int n;
  cin >> n;
  int weight;
  cin >> weight;
  vector<int> w(n), v(n);
  rep(i, n) { cin >> v[i] >> w[i]; }

  vector<vector<int>> a(n);
  rep(i, n) {
    a[i].resize(weight + 1);
    rep(j, weight + 1) {
      if (i == 0) {
        a[0][j] = (j / w[0]) * v[0];
        /*
        if(j>=w[0])a[0][j]=v[0];
        else a[0][j]=0;
        */
      } else {
        int k = 0;
        int m = a[i - 1][j];
        while (j - k * w[i] >= 0) {
          m = max(m, a[i - 1][j - k * w[i]] + k * v[i]);
          k++;
        }
        a[i][j] = m;
        /*
        if(j<w[i]) a[i][j]=a[i-1][j];
        else a[i][j]=max(a[i-1][j],a[i-1][j-w[i]]+v[i]);
        */
      }
    }
  }
  cout << a[n - 1][weight] << endl;
}
Problem p02316 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>

using namespace std;

int n, W, v[110], w[110], dp[110][10010];
int calc() {
  for (int i = n - 1; i >= 0; 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][j - w[i]] + v[i]);
    }
  }
  return dp[0][W];
}
int main() {
  int ans;
  cin >> n >> W;
  for (int i = 0; i < n; i++) {
    cin >> v[i] >> w[i];
  }
  ans = calc();
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

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

int calc(int i, int j) {
  int ret = 0;
  if (memo[i][j] != -1)
    return memo[i][j];
  if (i == n)
    ret = 0;

  else {
    for (int k = 0; k * w[i] <= j; k++) {
      ret = max(ret, calc(i + 1, j - k * w[i]) + k * v[i]);
    }
  }
  return memo[i][j] = ret;
}
int main() {
  for (int i = 0; i < 101; i++) {
    for (int j = 0; j < 10001; j++) {
      memo[i][j] = -1;
    }
  }

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

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

#define repeat(i, n) for (int i = 0; i < (n); ++i)
#define repeat_rev(i, n) for (int i = (n)-1; 0 <= i; ++i)

#define MAX_N 100
#define MAX_W 10000

struct item {
  int v, w;
};
int N, W;
item t[MAX_N];
int dp[2][MAX_W + 1];

int main() {
  cin >> N >> W;
  repeat(i, N) cin >> t[i].v >> t[i].w;
  int *cur = dp[0];
  fill(cur, cur + W + 1, 0);
  int *prv = dp[1];
  repeat(n, N) {
    swap(cur, prv);
    repeat(w, W + 1) cur[w] =
        max(prv[w], (0 <= w - t[n].w ? cur[w - t[n].w] + t[n].v : 0));
  }
  repeat_rev(i, W + 1) if (cur[i]) {
    cout << cur[i] << endl;
    break;
  }
  return 0;
}

Error: 0
0 0 4 4 8 8 12 12 16 
0 0 5 5 10 10 15 15 20 
0 2 5 7 10 12 15 17 20 
0 2 5 8 10 13 16 18 21 


Fixed submission:

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

#define repeat(i, n) for (int i = 0; i < (n); ++i)
#define repeat_rev(i, n) for (int i = (n)-1; 0 <= i; ++i)

#define MAX_N 100
#define MAX_W 10000

struct item {
  int v, w;
};
int N, W;
item t[MAX_N];
int dp[2][MAX_W + 1];

int main() {
  cin >> N >> W;
  repeat(i, N) cin >> t[i].v >> t[i].w;
  int *cur = dp[0];
  fill(cur, cur + W + 1, 0);
  int *prv = dp[1];
  repeat(n, N) {
    swap(cur, prv);
    repeat(w, W + 1) cur[w] =
        max(prv[w], (0 <= w - t[n].w ? cur[w - t[n].w] + t[n].v : 0));
    repeat(w, W + 1) cerr << cur[w] << ' ';
    cerr << endl;
  }
  repeat_rev(i, W + 1) if (cur[i]) {
    cout << cur[i] << endl;
    break;
  }
  return 0;
}
Problem p02316 (C++) — buggy submission, verdict: Runtime Error

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

struct knapsack {
  int value;
  int weight;
};

int main() {
  int N, W = 0;
  knapsack sack[120];
  int memo[120][10020];

  cin >> N >> W;

  for (long long int n = 0; n < N; n++) {
    cin >> sack[n].value >> sack[n].weight;
  }

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

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

  return 0;
}

Error: 0

Fixed submission:

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

struct knapsack {
  long long int value;
  long long int weight;
};

int main() {
  long long int N, W = 0;
  knapsack sack[1001];
  long long int memo[101][10001];

  cin >> N >> W;

  for (int n = 0; n < N; n++) {
    cin >> sack[n].value >> sack[n].weight;
  }

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

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

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

#include <stdio.h>
int i, j, k, a, b, c[1001], d[1001], e[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] = -1;
    }
  }
  for (i = 0; i < a; i++) {
    scanf("%d %d", &c[i], &d[i]);
  }
  e[0] = 0;
  for (i = 0; i <= b; i++) {
    if (e[i] != -1) {
      for (j = 0; j <= a; j++) {
        if (e[i + d[j]] < e[i] + c[j] && i + d[j] <= b) {
          e[i + d[j]] = e[i] + c[j];
        }
      }
    }
  }
  for (i = 0; i <= b; i++) {
    if (f < e[i]) {
      f = e[i];
    }
  }
  printf("%d\\n", f);
  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>
int i, j, k, a, b, c[1001], d[1001], e[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] = -1;
    }
  }
  for (i = 0; i < a; i++) {
    scanf("%d %d", &c[i], &d[i]);
  }
  e[0] = 0;
  for (i = 0; i <= b; i++) {
    if (e[i] != -1) {
      for (j = 0; j <= a; j++) {
        if (e[i + d[j]] < e[i] + c[j]) {
          e[i + d[j]] = e[i] + c[j];
        }
      }
    }
  }
  for (i = 0; i <= b; i++) {
    if (f < e[i]) {
      f = e[i];
    }
  }
  printf("%d\\n", f);
  return 0;
}
Problem p02316 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>

using namespace std;

int main() {
  int n, w;
  struct {
    int v, w;
  } item[101];
  cin >> n >> w;
  for (int i = 0; i <= n; i++)
    cin >> item[i].v >> item[i].w;
  int dp[101][10001] = {};
  for (int i = 0; i < n; i++)
    for (int j = 0; j <= w; j++)
      if (item[i].w <= j)
        dp[i + 1][j] = max(dp[i][j], dp[i + 1][j - item[i].w] + item[i].v);
      else
        dp[i + 1][j] = dp[i][j];
  cout << dp[n][w] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>

using namespace std;

int main() {
  int n, w;
  struct {
    int v, w;
  } item[101];
  cin >> n >> w;
  for (int i = 0; i <= n; i++)
    cin >> item[i].v >> item[i].w;
  int dp[101][10001] = {};
  for (int i = 0; i <= n; i++)
    for (int j = 1; j <= w; j++)
      if (item[i].w <= j)
        dp[i + 1][j] = max(dp[i][j], dp[i + 1][j - item[i].w] + item[i].v);
      else
        dp[i + 1][j] = dp[i][j];
  cout << dp[n][w] << endl;
  return 0;
}
Problem p02316 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define N 100
#define maxW 10000
int v[N];
int w[N];
int C[N + 1][maxW + 1];

int main() {
  std::istream &c_in = cin;
  int n, W;
  c_in >> n >> W;

  {
    int k = 0;
    for (int i = 0; i < n; i++) {
      // c_in >> v[i + 1] >> w[i + 1];
      int vv, ww;
      c_in >> vv >> ww;
      int j;
      for (j = 0; j < i; ++j) {
        if (ww == w[j + 1]) {
          if (vv > v[j + 1]) {
            v[j + 1] = vv;
          }
          break;
        }
      }
      if (j == i) {
        v[k + 1] = vv;
        w[k + 1] = ww;
        k++;
      }
    }
    n = k;

    for (int i = 0; i <= n; ++i)
      C[i][0] = 0;
    for (int j = 0; j <= W; ++j)
      C[0][j] = 0;
  }

  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= W; ++j) {
      int maxv = C[i - 1][j];
      for (int s = 1; s <= j / w[i]; ++s) {
        int value = s * v[i] + C[i - 1][j - s * w[i]];
        if (value > maxv) {
          maxv = value;
        }
      }
      C[i][j] = maxv;
    }
  }
  printf("%d\\n", C[n][W]);
  return 0;
}

Error: TLE

Fixed submission:

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

#define N 100
#define maxW 10000
int v[N];
int w[N];
int C[N + 1][maxW + 1];

int main() {
  std::istream &c_in = cin;
  int n, W;
  c_in >> n >> W;

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

    for (int i = 0; i <= n; ++i)
      C[i][0] = 0;
    for (int j = 0; j <= W; ++j)
      C[0][j] = 0;
  }

  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= W; ++j) {
      int maxv = C[i - 1][j];
      for (int s = 1; s <= j / w[i]; ++s) {
        if (w[i] * s > j)
          break;
        int value = s * v[i] + C[i - 1][j - s * w[i]];
        if (value > maxv) {
          maxv = value;
        }
      }
      C[i][j] = maxv;
    }
  }
  printf("%d\\n", C[n][W]);
  return 0;
}