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


#include <iostream>
#include <vector>

using namespace std;

int main() {
  int totalValueOfShares; // target sum
  int size;               // number of coin types

  cin >> totalValueOfShares >> size;

  vector<int> numOfShares(size);
  vector<int> values(totalValueOfShares + 1);

  for (int i = 0; i < size; i++) {
    cin >> numOfShares[i];
  }

  values[0] = 0;

  int minValue = 0x7FFFFFFF;

  for (int i = 0; i < size; i++) {
    if (numOfShares[i] < minValue) {
      minValue = numOfShares[i];
    }
  }

  for (int i = 1; i <= totalValueOfShares; i++) {
    values[i] = 0x7FFFFFFF;
  }

  for (int i = minValue; i <= totalValueOfShares; i++) {
    for (int j = 0; j < size; j++)
      if (numOfShares[j] <= i) {
        int sum = values[i - numOfShares[j]];
        if (sum < 0x7FFFFFFF && sum + 1 < values[i])
          values[i] = sum + 1;
      }
  }
  if (values[totalValueOfShares] == 0x7FFFFFFF) {
    cout << 0;
  }
  cout << values[totalValueOfShares] << "\\r\\n";
}


Error: 0

Fixed submission:


#include <iostream>
#include <vector>

using namespace std;

int main() {
  int totalValueOfShares; // target sum
  int size;               // number of coin types

  cin >> totalValueOfShares >> size;

  vector<int> numOfShares(size);
  vector<int> values(totalValueOfShares);

  for (int i = 0; i < size; i++) {
    cin >> numOfShares[i];
  }

  values[0] = 0;

  int minValue = 0x7FFFFFFF;

  for (int i = 0; i < size; i++) {
    if (numOfShares[i] < minValue) {
      minValue = numOfShares[i];
    }
  }

  for (int i = 1; i <= totalValueOfShares; i++) {
    values[i] = 0x7FFFFFFF;
  }

  for (int i = minValue; i <= totalValueOfShares; i++) {
    for (int j = 0; j < size; j++)
      if (numOfShares[j] <= i) {
        int sum = values[i - numOfShares[j]];
        if (sum < 0x7FFFFFFF && sum + 1 < values[i])
          values[i] = sum + 1;
      }
  }
  if (values[totalValueOfShares] == 0x7FFFFFFF) {
    cout << 0;
  }
  cout << values[totalValueOfShares] << "\\r\\n";
}
Problem p02314 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

#define MAX_NUM_COINS 20
#define MAX_PRICE 50000
#define CALC_MIN(a, b) (a < b ? a : b)

int coin[MAX_NUM_COINS];
int memo[MAX_NUM_COINS][MAX_PRICE + 1];

int is_less_than(const void *a, const void *b) { return *(int *)a < *(int *)b; }

int solve_sub_question(int index, int num_coin, int price) {
  if (memo[index][price] != -1) {
    return memo[index][price];
  }
  if (index == num_coin - 1) {
    memo[index][price] = price;
    return price;
  }
  if (price % coin[index] == 0) {
    memo[index][price] = price / coin[index];
    return price / coin[index];
  }
  int ans = INT_MAX;
  for (int i = 0; i <= price / coin[index]; i++) {
    int total_coin = i;
    int current_price = price - coin[index] * i;
    total_coin += solve_sub_question(index + 1, num_coin, current_price);
    ans = CALC_MIN(total_coin, ans);
  }
  memo[index][price] = ans;
  return ans;
}

int main() {
  memset(memo, -1, sizeof(memo));
  for (int i = 0; i < MAX_NUM_COINS; i++) {
    memo[i][0] = 0;
  }

  int price, num_coin;
  scanf("%d%d", &price, &num_coin);
  for (int i = 0; i < num_coin; i++) {
    scanf("%d", &coin[i]);
  }
  qsort(coin, num_coin, sizeof(int), is_less_than);

  printf("%d\\n", solve_sub_question(0, num_coin, price));
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

#define MAX_NUM_COINS 20
#define MAX_PRICE 50000
#define CALC_MIN(a, b) (a < b ? a : b)

int coin[MAX_NUM_COINS];
int memo[MAX_NUM_COINS][MAX_PRICE + 1];

int is_less_than(const void *a, const void *b) { return *(int *)a < *(int *)b; }

int solve_sub_question(int index, int num_coin, int price) {
  if (memo[index][price] != -1) {
    return memo[index][price];
  }
  if (index == num_coin - 1) {
    memo[index][price] = price;
    return price;
  }
  int ans = INT_MAX;
  for (int i = 0; i <= price / coin[index]; i++) {
    int total_coin = i;
    int current_price = price - coin[index] * i;
    total_coin += solve_sub_question(index + 1, num_coin, current_price);
    ans = CALC_MIN(total_coin, ans);
  }
  memo[index][price] = ans;
  return ans;
}

int main() {
  memset(memo, -1, sizeof(memo));
  for (int i = 0; i < MAX_NUM_COINS; i++) {
    memo[i][0] = 0;
  }

  int price, num_coin;
  scanf("%d%d", &price, &num_coin);
  for (int i = 0; i < num_coin; i++) {
    scanf("%d", &coin[i]);
  }
  qsort(coin, num_coin, sizeof(int), is_less_than);

  printf("%d\\n", solve_sub_question(0, num_coin, price));
  return 0;
}
Problem p02314 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
using namespace std;
int n, m;
int memo[100001];
int a[21];
int main() {
  for (int i = 0; i < 100000; i++) {
    memo[i] = 99999;
  }
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a[i];
  }
  memo[0] = 0;
  for (int i = 0; i < n; i++) {
    for (int l = 0; l < m; l++) {
      memo[i + a[l]] = min(memo[i + a[l]], memo[i] + 1);
    }
  }
  cout << memo[n] << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
int n, m;
int memo[50001];
int a[21];
int main() {
  for (int i = 0; i < 50001; i++) {
    memo[i] = 99999;
  }
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a[i];
  }
  memo[0] = 0;
  for (int i = 0; i < n; i++) {
    for (int l = 0; l < m; l++) {
      memo[i + a[l]] = min(memo[i + a[l]], memo[i] + 1);
    }
  }
  cout << memo[n] << endl;

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

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

int dp[50000] = {0};
int n, m, c[20];

int solve(int money) {
  if (money < 0)
    return 1 << 30;
  if (money == 0)
    return 0;
  if (dp[money] != 0)
    return dp[money];
  int ans = 1 << 30;
  for (int i = 0; i < m; i++) {
    ans = min(solve(money - c[i]) + 1, ans);
  }
  dp[money] = ans;
  return dp[money];
}

int main() {
  cin >> n;
  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> c[i];
  }
  cout << solve(n) << endl;
}

Error: -11

Fixed submission:

#include <iostream>
using namespace std;

int dp[50000] = {0};
int n, m, c[20];

int solve(int money) {
  if (money == 0)
    return 0;
  if (dp[money] != 0)
    return dp[money];
  int min = 1 << 30, ans;
  for (int i = 0; i < m; i++) {
    ans = solve(money - c[i]) + 1;
    if (ans > min)
      ans = min;
  }
  dp[money] = ans;
  return dp[money];
}

int main() {
  cin >> n;
  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> c[i];
  }
  cout << solve(n);
}
Problem p02314 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int M = 20, N = 50000;
int dp[N + 5], a[M + 5], n, m;
signed main(void) {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++)
    scanf("%d", a + i);
  memset(dp, 0x3f, sizeof(dp));
  dp[0] = 0;
  for (int i = 1; i <= m; i++)
    for (int j = 0; j <= n; j++)
      if (j - a[i] >= 0)
        dp[j] = min(dp[j], dp[j - a[i]] + 1);
  printf("%d\\n", dp[n]);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int M = 20, N = 50000;
int dp[M + 1][N + 1], a[M + 1], n, m;
inline int DP(int x, int y) {
  if (~dp[x][y])
    return dp[x][y];
  if (x == 0)
    return 0x3f3f3f3f;
  else
    dp[x][y] = 0x3f3f3f3f;
  for (int k = 0; k * a[x] <= y; k++)
    dp[x][y] = min(dp[x][y], DP(x - 1, y - k * a[x]) + k);
  return dp[x][y];
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++)
    cin >> a[i];
  memset(dp, -1, sizeof(dp));
  dp[0][0] = 0;
  cout << DP(m, n) << endl;
  return 0;
}
Problem p02314 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>

#include <algorithm>

using namespace std;

int ans[50000 + 1];

int coin[20];

int main() {
  int n, m;
  scanf("%d %d", &n, &m);

  int i, j;
  for (i = 0; i < m; ++i) {
    scanf("%d", &coin[i]);

    ++ans[coin[i]];
  }

  for (i = 1; i < n; ++i) {
    for (j = 0; j < m; ++j) {
      if (i + coin[j] <= n) {
        ans[i + coin[j]] =
            ans[i + coin[j]] ? min(ans[i + coin[j]], ans[i] + 1) : ans[i] + 1;
      }
    }
  }

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

  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>

#include <algorithm>

using namespace std;

int ans[50000 + 1];

int coin[20];

int main() {
  int n, m;
  scanf("%d %d", &n, &m);

  int i, j;
  for (i = 0; i < m; ++i) {
    scanf("%d", &coin[i]);

    ++ans[coin[i]];
  }

  for (i = 1; i < n; ++i) {
    for (j = 0; j < m; ++j) {
      if (ans[i] + coin[j] <= n) {
        ans[i + coin[j]] =
            ans[i + coin[j]] ? min(ans[i + coin[j]], ans[i] + 1) : ans[i] + 1;
      }
    }
  }

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

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

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

int main() {
  int n, m;
  vector<int> c, dp;

  cin >> n >> m;
  c.resize(m);
  dp.resize(n + 1);

  for (int i = 0; i < m; ++i)
    cin >> c[i];

  dp[0] = 0;
  for (int i = 1; i <= n; ++i) {
    int min = -1;
    for (int j = 0; j < m; ++j) {
      if (i - c[j] >= 0) {
        if (min == -1 || min > dp[i - c[j]])
          min = dp[i - c[j]];
      }
    }
    dp[i] = min + 1;
  }
  cout << dp[n] << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

int Min;

void judge(int n, int *d, int m, int t) {

  if (n == 0) {
    if (t < Min)
      Min = t;
  }

  if (n > 0) {

    if (t < Min) {
      for (int i = m; i > 0; --i)
        judge(n - d[i - 1], d, m, t + 1);
    }
  }
}

int main() {
  int n, m, d[20];
  Min = 50000;

  cin >> n >> m;
  for (int i = 0; i < m; ++i)
    cin >> d[i];

  judge(n, d, m, 0);

  cout << Min << endl;
}
Problem p02314 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <stdlib.h>

using namespace std;

int comp(const void *a, const void *b) { return *(int *)a - *(int *)b; }

int main() {
  int n, m, c[20], T[50001];
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> c[i];
  }
  qsort(c, m, sizeof(int), comp);
  for (int j = 0; j <= n; j++) {
    T[j] = 0;
  }
  for (int i = 0; i < m; i++) {
    for (int j = c[i]; j <= n; j++) {
      T[j] = T[j] && (T[j] < T[j - c[i]] + 1) ? T[j] : T[j - c[i]] + 1;
    }
  }
  cout << T[n] << endl;

  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#include <stdlib.h>

using namespace std;

int comp(const void *a, const void *b) { return *(int *)b - *(int *)a; }

int numofbits(long bits) {
  bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555);
  bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333);
  bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f);
  bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff);
  return (bits & 0x0000ffff) + (bits >> 16 & 0x0000ffff);
}
int comp2(const void *a, const void *b) {
  return numofbits(*(int *)a) - numofbits(*(int *)b);
}

int main() {
  int n, m, c[20], sum;
  long bita[1 << 21];
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> c[i];
  }
  qsort(c, m, sizeof(int), comp);
  for (long i = 0; i < 1 << 21; i++) {
    bita[i] = i;
  }
  qsort(bita, 1 << 21, sizeof(long), comp2);
  for (long i = 0; i < 1 << 21; i++) {
    sum = 0;
    for (int b = 0; b < 21; b++) {
      if (bita[i] >> b & 1) {
        sum += c[b];
      }
    }
    if (sum == n) {
      cout << numofbits(bita[i]) << endl;
      return 0;
    }
  }
  return 0;
}
Problem p02314 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstring>
#include <iostream>

#define rep(i, n) for (int i = 0; i < (n); i++)

const int INF = 1e9;

using namespace std;

int m, n;
int c[21];

int dp[21][100005];

int dfs(int i, int sum) {
  if (i == m) {
    if (n == sum)
      return 0;
    else
      return INF;
  }

  if (dp[i][sum] != -1)
    return dp[i][sum];

  int ret;

  ret = dfs(i + 1, sum);

  if (sum + c[i] <= n) {
    ret = min(ret, dfs(i, sum + c[i]) + 1);
  }
  return dp[i][sum] = ret;
}
int main() {
  rep(i, 21) rep(j, 100005) dp[i][j] = -1;
  cin >> n >> m;
  rep(i, m) cin >> c[i];
  cout << dfs(0, 0) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>

#define rep(i, n) for (int i = 0; i < (n); i++)

const int INF = 1e9;

using namespace std;

int m, n;
int c[21];

int dfs(int i, int sum) {
  if (i == m) {
    if (n == sum)
      return 0;
    else
      return INF;
  }

  int ret;

  ret = dfs(i + 1, sum);

  if (sum + c[i] <= n) {
    ret = min(ret, dfs(i, sum + c[i]) + 1);
  }
  return ret;
}
int main() {
  cin >> n >> m;
  rep(i, m) cin >> c[i];
  cout << dfs(0, 0) << endl;
  return 0;
}
Problem p02314 (C++) — buggy submission, verdict: Time Limit Exceeded

// DPL_1_A.cpp
// Combinatorial - Coin Changing Problem
// TLE - ??????8???????????¨???

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int MAXN = 50000;
const int MAXM = 20;
const int INF = 50000;
int res;
vector<int> nums(MAXM);
vector<int> dp(MAXN);
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> nums[i];
  }
  for (int i = 0; i <= n; i++)
    dp[i] = INF;
  dp[0] = 0;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j + nums[i] <= n; j++) {
      dp[j + nums[i]] = min(dp[j + nums[i]], dp[j] + 1);
    }
  }
  cout << dp[n] << endl;

  return 0;
}

Error: TLE

Fixed submission:

// DPL_1_A.cpp
// Combinatorial - Coin Changing Problem

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int MAXM = 20;
const int INF = 50000;
int res;
vector<int> nums(MAXM);
int dfs(int sum, int pos) {
  if (sum < 0 || pos < 0)
    return INF;
  if (sum == 0) {
    return res;
  }
  if (sum < nums[pos]) {
    return dfs(sum, pos - 1);
  } else {
    return min(dfs(sum - nums[pos], pos) + 1, dfs(sum, pos - 1));
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> nums[i];
  }
  sort(nums.begin(), nums.begin() + m);
  int mini = dfs(n, m - 1);
  cout << mini << endl;
  return 0;
}
Problem p02314 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define MAX_N 50001
#define MAX_M 21
using namespace std;
int n, m;
int value[MAX_M];

int solve() {
  int coin[MAX_N];
  memset(coin, MAX_N, sizeof(coin));
  coin[0] = 0;

  for (int r = 0; r < m; r++) {
    int val = value[r];
    for (int c = val; c <= n; c++) {
      coin[c] = min(coin[c], coin[c - val] + 1);
    }
  }
  return (coin[n]);
}

int main(void) {
  cin >> n >> m;
  for (int r = 0; r < m; r++)
    cin >> value[r];

  cout << solve() << endl;
  return (0);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cstdio>
#include <cstring>
#include <iostream>
#define MAX_N 50001
#define MAX_M 20
using namespace std;
int dp[MAX_N];
int n, m;
int value[MAX_M];

void knapsack(int pos, int sum) {
  // cout << sum << endl;
  if (pos > m)
    return;
  if (sum == n) {
    dp[sum] = min(dp[sum], pos);
  }

  for (int r = 0; r < m; r++) {
    knapsack(pos + 1, sum);            // 選ばない
    knapsack(pos + 1, sum + value[r]); // 選ぶ
  }
  return;
}

int main(void) {

  memset(dp, MAX_N, sizeof(dp));
  cin >> n >> m;
  for (int r = 0; r < m; r++)
    cin >> value[r];

  knapsack(0, 0);
  cout << dp[n] << endl;
  return (0);
}
Problem p02314 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define MAX_N 50001
using namespace std;
int n, m;
int dp[MAX_N]; // I think that when i use dp, using one demention array is too
               // difficult to coding for me.

int knapsack() {
  // cout << "let's napsack!" << endl;
  // initialize
  memset(dp, 0, sizeof(dp));

  // input
  int value[m];
  for (int r = 0; r < m; r++) {
    cin >> value[r];
    dp[value[r]]++;
  }

  // calculate
  // for(int r = 0; r < m; r++) { //Usually, I confuse about roop's end. ->
  // unnecessary
  for (int c = 1; c < n; c++) {
    // This 'if' statement's contents usually complecated
    if (dp[c] != 0) {
      for (int k = 0; k < m; k++) {
        if (c + value[k] > n)
          continue;
        if (dp[c + value[k]] == 0) {
          dp[c + value[k]] = dp[c] + 1;
          continue;
        }
        dp[c + value[k]] = min(dp[c + value[k]], dp[c] + 1);
      }
    }
  }
  //}
  return (dp[n]);
}

int main(void) {
  // cout << "start" << endl;
  cin >> n >> m;

  cout << knapsack() << endl;
  return (0);
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define MAX_N 50001
using namespace std;
int n, m;
int dp[MAX_N]; // I think that when i use dp, using one demention array is too
               // difficult to coding for me.

int knapsack() {
  // cout << "let's napsack!" << endl;
  // initialize
  memset(dp, 0, sizeof(dp));

  // input
  int value[m];
  for (int r = 0; r < m; r++) {
    cin >> value[r];
    dp[value[r]]++;
  }

  // calculate
  // for(int r = 0; r < m; r++) { //Usually, I confuse about roop's end.
  for (int c = 1; c < n; c++) {
    // This 'if' statement's contents usually complecated
    if (dp[c] != 0) {
      for (int k = 0; k < m; k++) {
        if (dp[c + value[k]] == 0) {
          dp[c + value[k]] = dp[c] + 1;
          continue;
        }
        dp[c + value[k]] = min(dp[c + value[k]], dp[c] + 1);
      }
    }
  }
  //}
  return (dp[n]);
}

int main(void) {
  // cout << "start" << endl;
  cin >> n >> m;

  cout << knapsack() << endl;
  return (0);
}
Problem p02314 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstring>
#include <iostream>

#define rep(i, n) for (int i = 0; i < (n); i++)

const int INF = 1e9;

using namespace std;

int m, n;
int c[21];

int dp[21][100005];

int dfs(int i, int sum) {
  if (i == m) {
    if (n == sum)
      return 0;
    else
      return INF;
  }

  if (dp[i][sum] != -1)
    return dp[i][sum];

  int ret;

  ret = dfs(i + 1, sum);

  if (sum + c[i] <= n) {
    ret = min(ret, dfs(i, sum + c[i]) + 1);
  }

  return dp[i][sum] = ret;
}

int main() {
  rep(i, 21) rep(j, 100005) dp[i][j] = -1;
  cin >> n >> m;
  rep(i, m) cin >> c[i];
  cout << dfs(0, 0) << endl;
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>

#define rep(i, n) for (int i = 0; i < (n); i++)

const int INF = 1e9;

using namespace std;

int m, n;
int c[21];

int dfs(int i, int sum) {
  if (i == m) {
    if (n == sum)
      return 0;
    else
      return INF;
  }

  int ret;

  ret = dfs(i + 1, sum);

  if (sum + c[i] <= n) {
    ret = min(ret, dfs(i, sum + c[i]) + 1);
  }

  return ret;
}

int main() {
  cin >> n >> m;
  rep(i, m) cin >> c[i];
  cout << dfs(0, 0) << endl;
}
Problem p02314 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  int c[20];
  for (int i = 0; i < m; i++)
    cin >> c[i];

  int table[50001];
  table[0] = 0;
  for (int i = 1; i <= n; i++) {
    int mi = table[i - 1] + 1;
    for (int j = 0; j < m; j++) {
      if (c[j] > i)
        continue;
      mi = min(mi, table[i - c[j]] + 1);
    }

    table[i] = mi;
  }

  cout << table[n] << endl;

  return 0;
}

Error: 0

Fixed submission:


#include <iostream>
using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  int c[20];
  for (int i = 0; i < m; i++)
    cin >> c[i];

  int table[10001];
  table[0] = 0;
  for (int i = 1; i <= n; i++) {
    int mi = table[i - 1] + 1;
    for (int j = 0; j < m; j++) {
      if (c[j] > i)
        continue;
      mi = min(mi, table[i - c[j]] + 1);
    }

    table[i] = mi;
  }

  cout << table[n] << endl;

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

#include <bits/stdc++.h>

using namespace std;
int n, m;
int c[21];
int dp[21][50001];
int main() {
  cin >> n >> m;
  for (int i = 0; i < 21; i++) {
    dp[i][0] = 0;
  }
  for (int i = 0; i < 21; i++) {
    for (int k = 1; k < 50001; k++) {
      dp[i][k] = 20000;
    }
  }
  for (int i = 1; i <= m; i++) {
    cin >> c[i];
  }
  for (int i = 1; i <= m; i++) {
    for (int k = 1; k <= 50000; k++) {
      if (k >= c[i])
        dp[i][k] = min(dp[i][k - c[i]] + 1, dp[i - 1][k]);
      else
        dp[i][k] = dp[i - 1][k];
    }
  }

  cout << dp[m][n] << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
int n, m;
int c[21];
int dp[21][50001];
int main() {
  cin >> n >> m;
  for (int i = 0; i < 21; i++) {
    dp[i][0] = 0;
  }
  for (int i = 0; i < 21; i++) {
    for (int k = 1; k < 50001; k++) {
      dp[i][k] = 20000;
    }
  }
  for (int i = 1; i <= m; i++) {
    cin >> c[i];
  }
  for (int i = 1; i <= m; i++) {
    for (int k = 1; k <= 500000; k++) {
      if (k >= c[i])
        dp[i][k] = min(dp[i][k - c[i]] + 1, dp[i - 1][k]);
      else
        dp[i][k] = dp[i - 1][k];
    }
  }

  cout << dp[m][n] << endl;
}
Problem p02314 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
// #define int long long
#define SIZE 21
#define VALUE 50001
const int infty = 99999;
// #define _DEBUG
using namespace std;
int N, M, Cs[SIZE], T[SIZE][VALUE];
int solve() {
  for (int i = 0; i <= M; i++) {
    for (int n = 0; n <= N; n++) {
      if (n == 0) {
        T[i][n] = 0;
      } else if (i == 0) {
        T[i][n] = infty;
      } else if (n < Cs[i - 1]) {
        T[i][n] = T[i - 1][n];
      } else {
        T[i][n] = min(T[i - 1][n],
                      min(T[i - 1][n - Cs[i - 1]], T[i][n - Cs[i - 1]]) + 1);
      }
    }
  }
#ifdef _DEBUG
  for (int i = 0; i <= M; i++) {
    if (i > 0)
      cout << Cs[i - 1] << "\\t";
    else
      cout << "i\\\\n\\t";
    for (int n = 0; n <= N; n++) {
      cout << T[i][n] << "\\t";
    }
    cout << endl;
  }
#endif
  return T[M][N];
}
signed main() {
  while (cin >> N >> M) {
    for (int i = 0; i < M; i++) {
      cin >> Cs[i];
    }
    cout << solve() << endl;
  }
}


Error: 0

Fixed submission:

#include <iostream>
// #define int long long
#define SIZE 21
#define VALUE 10001
const int infty = 999;
// #define _DEBUG
using namespace std;
int N, M, Cs[SIZE], T[SIZE][VALUE];
int solve() {
  for (int i = 0; i <= M; i++) {
    for (int n = 0; n <= N; n++) {
      if (n == 0) {
        T[i][n] = 0;
      } else if (i == 0) {
        T[i][n] = infty;
      } else if (n < Cs[i - 1]) {
        T[i][n] = T[i - 1][n];
      } else {
        T[i][n] = min(T[i - 1][n],
                      min(T[i - 1][n - Cs[i - 1]], T[i][n - Cs[i - 1]]) + 1);
      }
    }
  }
#ifdef _DEBUG
  for (int i = 0; i <= M; i++) {
    if (i > 0)
      cout << Cs[i - 1] << "\\t";
    else
      cout << "i\\\\n\\t";
    for (int n = 0; n <= N; n++) {
      cout << T[i][n] << "\\t";
    }
    cout << endl;
  }
#endif
  return T[M][N];
}
signed main() {
  while (cin >> N >> M) {
    for (int i = 0; i < M; i++) {
      cin >> Cs[i];
    }
    cout << solve() << endl;
  }
}