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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#define rep(i, a, b) for (int(i) = (a); i < (b); i++)
#define INF 1000000000
#define MAX_N 100005
using namespace std;

int main() {
  int n, m;
  int c[21], dp[21][50001];

  cin >> n >> m;
  rep(i, 0, m) cin >> c[i];

  rep(i, 0, m + 1) rep(j, 0, n + 1) dp[i][j] = INF;
  rep(i, 0, m + 1) dp[i][0] = 0;

  rep(i, 0, m) {
    rep(j, 0, n + 1) {
      if (j < c[i])
        dp[i + 1][j] = dp[i][j];
      else
        dp[i + 1][j] = min(dp[i][j], dp[i + 1][j - c[i]] + 1);
    }
  }
  /*
  rep(i,0,m+1){
      rep(j,0,n+1){
          cout<<dp[i][j]<<" ";
      }
      cout<<endl;
  }
  */

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

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#define rep(i, a, b) for (int(i) = (a); i < (b); i++)
#define INF 1000000000
#define MAX_N 100005
using namespace std;

int main() {
  int n, m;
  int c[21], dp[21][50001];

  cin >> n >> m;
  rep(i, 0, m) cin >> c[i];

  rep(i, 0, m + 1) rep(j, 0, n + 1) dp[i][j] = INF;
  rep(i, 0, m + 1) dp[i][0] = 0;

  rep(i, 0, m + 1) {
    rep(j, 0, n + 1) {
      if (j < c[i])
        dp[i + 1][j] = dp[i][j];
      else
        dp[i + 1][j] = min(dp[i][j], dp[i + 1][j - c[i]] + 1);
    }
  }
  /*
  rep(i,0,m+1){
      rep(j,0,n+1){
          cout<<dp[i][j]<<" ";
      }
      cout<<endl;
  }
  */

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

#include <algorithm>
#include <iostream>
#include <math.h>
#include <string>
#include <vector>
using namespace std;
#define INF 10000000;

int partition(int A[], int p, int r) {
  int x = A[r];
  int i = p - 1;
  for (int j = p; j < r; j++) {
    if (x >= A[j]) {
      i = i + 1;
      swap(A[i], A[j]);
    }
  }
  swap(A[i + 1], A[r]);
  return i + 1;
}
void quicksort(int A[], int p, int r) {
  if (p < r) {
    int q = partition(A, p, r);
    quicksort(A, p, q - 1);
    quicksort(A, q + 1, r);
  }
}

int getNumberOfCoin(int C[], int n, int m) {
  int T[50000];
  for (int j = 0; j <= n; j++) {
    T[j] = INF;
  }
  T[0] = 0;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j <= n; j++) {
      if (j - C[i] >= 0) {
        T[j] = min(T[j], T[j - C[i]] + 1);
      }
    }
  }
  return T[n];
}

int main() {
  int n, m;
  cin >> n >> m;
  int C[20];
  for (int i = 0; i < m; i++) {
    cin >> C[i];
  }
  cout << getNumberOfCoin(C, n, m) << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <string>
#include <vector>
using namespace std;
#define INF 10000000;

int partition(int A[], int p, int r) {
  int x = A[r];
  int i = p - 1;
  for (int j = p; j < r; j++) {
    if (x >= A[j]) {
      i = i + 1;
      swap(A[i], A[j]);
    }
  }
  swap(A[i + 1], A[r]);
  return i + 1;
}
void quicksort(int A[], int p, int r) {
  if (p < r) {
    int q = partition(A, p, r);
    quicksort(A, p, q - 1);
    quicksort(A, q + 1, r);
  }
}

int getNumberOfCoin(int C[], int n, int m) {
  int T[1000];
  for (int j = 0; j <= n; j++) {
    T[j] = INF;
  }
  T[0] = 0;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j <= n; j++) {
      if (j - C[i] >= 0) {
        T[j] = min(T[j], T[j - C[i]] + 1);
      }
    }
  }
  return T[n];
}

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

#include <algorithm>
#include <iostream>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int main() {
  int INF = 100000000;
  int n, m;
  cin >> n >> m;
  int dp[50010];
  rep(i, 50010) dp[i] = INF;
  dp[0] = 0;
  int c[m];
  rep(i, m) cin >> c[i];
  rep(i, n + 1) {
    rep(j, m) {
      int tmp = INF;
      if (i - c[j] >= 0)
        tmp = dp[i - c[j]] + 1;
      dp[i] = min(tmp, dp[i]);
    }
  }
  cout << dp[n] << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int main() {
  int INF = 100000000;
  int n, m;
  cin >> n >> m;
  int dp[10010];
  rep(i, 10010) dp[i] = INF;
  dp[0] = 0;
  int c[m];
  rep(i, m) cin >> c[i];
  rep(i, n + 1) {
    rep(j, m) {
      int tmp = INF;
      if (i - c[j] >= 0)
        tmp = dp[i - c[j]] + 1;
      dp[i] = min(tmp, dp[i]);
    }
  }
  cout << dp[n] << endl;
  return 0;
}
Problem p02314 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int n, m;
int c[20];
int lp[50005] = {};

int solve(int x) {
  if (lp[x] != 0)
    return lp[x];
  int t = INT_MAX;
  for (int i = 0; i < m; i++)
    if (x > c[i])
      t = min(t, solve(x - c[i]) + 1);
  lp[x] = t;
  return t;
}

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

Error: TLE

Fixed submission:

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

int n, m;
int c[20];
int lp[50005] = {};

int solve(int x) {
  if (lp[x] != 0)
    return lp[x];
  int t = INT_MAX;
  for (int i = 1; i <= x / 2; i++)
    t = min(t, solve(i) + solve(x - i));
  lp[x] = t;
  return t;
}

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


#include <iostream>
using namespace std;
int main() {
  int n, m, t;
  int c[21];
  int T[21][100001];
  cin >> n >> m;
  int counter = 0;
  for (int k = 0; k < m; ++k) {
    cin >> t;
    c[k] = t;
    if (c[k] > n) {
      c[k] = c[k + 1];
      counter++;
    }
    for (int j = 1; j <= n; ++j) {
      T[k][0] = 0;
      T[0][j] = 100000;
    }
  }
  int m1 = m - counter;
  for (int i = 1; i <= m1; ++i) {
    for (int j = 1; j <= n; ++j) {
      if (c[i - 1] <= j) {
        T[i][j] = min(T[i - 1][j], T[i][j - c[i - 1]] + 1);
      } else
        T[i][j] = T[i - 1][j];
    }
  }
  cout << T[m1][n] << endl;
}

Error: 0

Fixed submission:


#include <iostream>
using namespace std;
int main() {
  int n, m, t;
  int c[21];
  int T[21][10001];
  cin >> n >> m;
  int counter = 0;
  for (int k = 0; k < m; ++k) {
    cin >> t;
    c[k] = t;
    if (c[k] > n) {
      c[k] = c[k + 1];
      counter++;
    }
    for (int j = 1; j <= n; ++j) {
      T[k][0] = 0;
      T[0][j] = 100000;
    }
  }
  int m1 = m - counter;
  for (int i = 1; i <= m1; ++i) {
    for (int j = 1; j <= n; ++j) {
      if (c[i - 1] <= j) {
        T[i][j] = min(T[i - 1][j], T[i][j - c[i - 1]] + 1);
      } else
        T[i][j] = T[i - 1][j];
    }
  }
  cout << T[m1][n] << endl;
}
Problem p02314 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
int min(int, int);
int main(void) {
  int i, j, k, x, y;
  int n, m;
  int c[20];
  int a[50001];
  int b[50001];

  scanf("%d%d", &n, &m);

  for (i = 0; i < m; i++) {
    scanf("%d", &c[i]);
  }

  if (m == 1) {
    printf("%d\\n", n);
    return 0;
  }

  std::sort(c, c + m);

  for (i = 0; i <= n; i++) {
    a[i] = i / c[1] + i % c[1];
    b[i] = a[i];
  }
  // for(j=0;j<=n;j++){
  //		printf("%d ",a[j]);
  //	}
  // printf("\\n");
  bool l = true;
  for (i = 2; i < m; i++) {

    if (l) {
      //	printf("ok\\n");
      for (j = 0; j <= n; j++) {
        if (j < c[i]) {
          b[j] = a[j];
        } else {
          b[j] = min(b[j - c[i]] + 1, a[j]);
        }
      }

      l = false;
    } else {
      for (j = 0; j <= n; j++) {
        if (j < c[i]) {
          a[j] = b[j];
        } else {
          a[j] = min(a[j - c[i]] + 1, b[j]);
        }
      }
      l = true;
    }

    //	for(j=0;j<=n;j++){
    //		printf("%d ",a[j]);
    //	}
    //	for(j=0;j<=n;j++){
    //		printf("%d ",b[j]);
    //	}
    //	printf("\\n");
    //	for(j=0;j<=n;j++){
    //		a[j] = b[j];
    //	}
  }
  if (m % 2 == 0) {
    printf("%d\\n", a[n]);
  } else {
    printf("%d\\n", b[n]);
  }
  return 0;
}

int min(int a, int b) {
  if (a < b) {
    return a;
  } else {
    return b;
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
int main(void) {
  int i, j, k, x, y;
  int n, m;
  int c[20];
  int a[50001];
  int b[50001];

  scanf("%d%d", &n, &m);

  for (i = 0; i < m; i++) {
    scanf("%d", &c[i]);
  }

  if (m == 1) {
    printf("%d\\n", n);
    return 0;
  }

  std::sort(c, c + m);

  for (i = 0; i <= n; i++) {
    a[i] = i / c[1] + i % c[1];
    b[i] = a[i];
  }
  // for(j=0;j<=n;j++){
  //		printf("%d ",a[j]);
  //	}
  // printf("\\n");
  bool l = true;
  for (i = 2; i < m; i++) {

    if (l) {
      //	printf("ok\\n");
      for (j = 0; j <= n; j++) {

        for (k = 0; c[i] * k <= j; k++) {
          x = k + a[j - k * c[i]];
          if (x < b[j]) {
            b[j] = x;
          }
        }
      }
      l = false;
    } else {
      for (j = 0; j <= n; j++) {

        for (k = 0; c[i] * k <= j; k++) {
          x = k + b[j - k * c[i]];
          if (x < a[j]) {
            a[j] = x;
          }
        }
      }
      l = true;
    }

    //	for(j=0;j<=n;j++){
    //		printf("%d ",a[j]);
    //	}
    //	for(j=0;j<=n;j++){
    //		printf("%d ",b[j]);
    //	}
    //	printf("\\n");
    //	for(j=0;j<=n;j++){
    //		a[j] = b[j];
    //	}
  }

  if (m % 2 == 0) {
    printf("%d\\n", a[n]);
  } else {
    printf("%d\\n", b[n]);
  }
  return 0;
}
Problem p02314 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define M 21
#define N 100010
using namespace std;
int dp[N];

int Min(int &a, int b) { return a = min(a, b); }

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

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

  for (int i = 0; i < N; i++)
    dp[i] = 1e9;
  dp[0] = 0;
  for (int i = 0; i < m; i++)
    for (int j = 0; j <= n; j++)
      Min(dp[j + coin[i]], dp[j] + 1);

  cout << dp[n] << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define M 21
#define N 50010
using namespace std;
int dp[N];

int Min(int &a, int b) { return a = min(a, b); }

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

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

  for (int i = 0; i < N; i++)
    dp[i] = 1e9;
  dp[0] = 0;
  for (int i = 0; i < m; i++)
    for (int j = 0; j <= n; j++)
      Min(dp[j + coin[i]], dp[j] + 1);

  cout << dp[n] << endl;

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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <stdio.h>

using namespace std;

int N, M;
int C[21];
int dp[51000];

int main(void) {
  scanf("%d %d", &N, &M);

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

  memset(dp, 0x3f, sizeof(dp));

  dp[0] = 0;
  for (int i = 0; i < M; ++i) {
    for (int j = C[i]; j < N + 1; ++j) {
      dp[j] = min(dp[j], dp[j - C[i]] + 1);
    }
  }

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


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <stdio.h>

using namespace std;

int N, M;
int C[21];
int dp[21][50001];

int calc(int, int);

int main(void) {
  scanf("%d %d", &N, &M);

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

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

  printf("%d\\n", calc(M, N));
}

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

  if (i == 1)
    return j;

  int t = 51000;

  for (int k = 0; k <= j / C[i - 1]; ++k) {
    t = min(t, calc(i - 1, j - (k * C[i - 1])) + k);
  }

  dp[i][j] = t;

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

#include <algorithm>
#include <cstdlib>
#include <stdio.h>

using namespace std;

const int N_MAX = 50001, INF = 100000;

int dp[N_MAX], n, m, i, j, d[20];

int main() {

  scanf("%d%d", &n, &m);
  for (i = 0; i < m; i++) {
    scanf("%d", &d[i]);
  }

  fill(dp, dp + n + 1, INF);
  dp[0] = 0;
  for (i = 0; i <= n; i++) {
    for (j = 0; j < m; j++) {
      if (d[j] <= i) {
        dp[i] = min(dp[i - d[j]] + 1, dp[i]);
      }
    }
  }

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

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <stdio.h>

using namespace std;

const int N_MAX = 10001, INF = 100000;

int dp[N_MAX], n, m, i, j, d[20];

int main() {

  scanf("%d%d", &n, &m);
  for (i = 0; i < m; i++) {
    scanf("%d", &d[i]);
  }

  fill(dp, dp + n + 1, INF);
  dp[0] = 0;
  for (i = 0; i <= n; i++) {
    for (j = 0; j < m; j++) {
      if (d[j] <= i) {
        dp[i] = min(dp[i - d[j]] + 1, dp[i]);
      }
    }
  }

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

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

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

#define REP(i, n) for (int i = 0; i < (n); i++)
#define REAP(i, a, n) for (int i = (a); i < (n); i++)
#define YES cout << "Yes" << endl
#define NO cout << "No" << endl
#define fr first
#define sc second
#define pb push_back
#define All(v) v.begin(), v.end()
typedef long long ll;

int n, m;
int dp[50001];
int c[20];

int solve() {
  dp[0] = 0;
  REP(i, n + 1) {
    REP(j, m) {
      if (i >= c[j]) {
        dp[i] = min(dp[i], dp[i - c[j]] + 1);
      }
    }
  }
  return dp[n];
}

int main() {
  cin >> n >> m;

  REP(i, m) cin >> c[i];
  REP(i, 50001) dp[i] = 50001;

  int ans = solve();
  cout << ans << endl;
}


Error: 0

Fixed submission:

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

#define REP(i, n) for (int i = 0; i < (n); i++)
#define REAP(i, a, n) for (int i = (a); i < (n); i++)
#define YES cout << "Yes" << endl
#define NO cout << "No" << endl
#define fr first
#define sc second
#define pb push_back
#define All(v) v.begin(), v.end()
typedef long long ll;

int n, m;
int dp[10001];
int c[20];

int solve() {
  dp[0] = 0;
  REP(i, n + 1) {
    REP(j, m) {
      if (i >= c[j]) {
        dp[i] = min(dp[i], dp[i - c[j]] + 1);
      }
    }
  }
  return dp[n];
}

int main() {
  cin >> n >> m;

  REP(i, m) cin >> c[i];
  REP(i, 10001) dp[i] = 50001;

  int ans = solve();
  cout << ans << endl;
}
Problem p02314 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int i, j, n, m, t[50001], c[21];
  cin >> n >> m;
  for (i = 0; i < m; i++)
    cin >> c[i];
  for (j = 0; j <= n; j++)
    t[j] = 10001;
  t[0] = 0;

  for (i = 0; i < m; i++) {
    for (j = c[i]; j <= n; j++) {
      t[j] = min(t[j], t[j - c[i]] + 1);
    }
  }

  cout << t[n] << endl;
  ;

  return 0;
}

Error: 0

Fixed submission:

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

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

  for (i = 0; i < m; i++) {
    for (j = c[i]; j <= n; j++) {
      t[j] = min(t[j], t[j - c[i]] + 1);
    }
  }

  cout << t[n] << endl;
  ;

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

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

#define int long long

const int INF = 1e15;

int dp[55555];
int c[22];

int n, m;

int rec(int money) {
  if (money == 0) {
    return dp[money] = 0;
  }

  if (dp[money] != INF) {
    return dp[money];
  }

  int ans = INF;
  for (int i = 0; i < m; i++) {
    if (money - c[i] < 0)
      continue;
    ans = min(ans, rec(money - c[i]) + 1);
  }

  return dp[money] = ans;
}

signed main() {
  cin >> n >> m;
  fill(dp, dp + n + 10, INF);
  for (int i = 0; i < m; i++) {
    cin >> c[i];
  }

  cout << rec(n) << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

#define int long long

const int INF = 1e15;

int dp[55555];
int c[22];

int n, m;

int rec(int money) {
  if (money == 0) {
    return 0;
  }

  int ans = INF;
  for (int i = 0; i < m; i++) {
    if (money - c[i] < 0)
      continue;
    ans = min(ans, rec(money - c[i]) + 1);
  }

  return ans;
}

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

  cout << rec(n) << endl;

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

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
static const int MAX = 10000;

int solve_coin_changing1(vector<int> coins, int target) {
  vector<vector<int>> com = {{0}};
  com.push_back(coins);
  bool data[50000] = {false};

  for (int n = 2; n <= 10000; n++) {
    vector<int> b;
    cout << n << endl;
    for (int i = 1; i <= n / 2; i++) {
      for (int j = 0; j < com[i].size(); j++) {
        for (int k = j; k < com[n - i].size(); k++) {
          int val = com[i][j] + com[n - i][k];
          if (val == target) {
            return n;
          }
          if (!data[val]) {
            b.push_back(val);
            data[val] = true;
          }
        }
      }
    }
    cout << b.size() << endl;
    com.push_back(b);
  }
  return -1;
}
int solve_coin_changing(vector<int> coins, int target) {
  int need_ncoin[50001];
  sort(coins.begin(), coins.end());
  auto cp = coins.begin();

  for (int i = 1; i <= target; i++) {
    if (*cp == i) {
      need_ncoin[i] = 1;
      cp++;
      continue;
    }
    int minimum = 50000;
    for (int j = 1; j <= i / 2; j++) {
      int ncoin = need_ncoin[i - j] + need_ncoin[j];
      minimum = min(minimum, ncoin);
      if (ncoin == 2)
        break;
    }
    need_ncoin[i] = minimum;
  }
  return need_ncoin[target];
}

int main() {
  int ncoin, target;
  cin >> target >> ncoin;
  vector<int> coins;
  for (int i = 0; i < ncoin; i++) {
    int val;
    cin >> val;
    coins.push_back(val);
  }
  cout << solve_coin_changing(coins, target) << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
static const int MAX = 50000;

int solve_coin_changing(vector<int> coins, int target) {
  vector<vector<int>> com = {{0}};
  com.push_back(coins);

  for (int n = 2; n <= MAX; n++) {
    vector<int> b;
    for (int i = 1; i <= n / 2; i++) {
      for (int val1 : com[i]) {
        for (int val2 : com[n - i]) {
          if (val1 + val2 == target) {
            return n;
          }
          b.push_back(val1 + val2);
        }
      }
    }
    com.push_back(b);
  }
  return -1;
}

int main() {
  int ncoin, target;
  cin >> target >> ncoin;
  vector<int> coins;
  for (int i = 0; i < ncoin; i++) {
    int val;
    cin >> val;
    coins.push_back(val);
  }
  cout << solve_coin_changing(coins, target) << endl;
}
Problem p02314 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
static const int INF = int(1e8);
using namespace std;

int dp[55555] = {};
int denom[55555] = {};
int main() {
  int n;
  int m;
  cin >> n;
  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> denom[i];
  }
  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    dp[i] = INF;
    for (int j = 0; j <= m; j++) {
      if (i >= denom[j]) {
        dp[i] = min(dp[i], dp[i - denom[j]] + 1);
      }
    }
  }
  cout << dp[n] << "\\n";
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
static const int INF = int(1e8);
using namespace std;

int dp[10001] = {};
int denom[10001] = {};
int main() {
  int n;
  int m;
  cin >> n;
  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> denom[i];
  }
  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    dp[i] = INF;
    for (int j = 0; j <= m; j++) {
      if (i >= denom[j]) {
        dp[i] = min(dp[i], dp[i - denom[j]] + 1);
      }
    }
  }
  cout << dp[n] << "\\n";
  return 0;
}
Problem p02314 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  constexpr int INF = 1e9;
  int n, m;
  cin >> n >> m;
  vector<int> dp(n + 1, INF), d(m);
  for (auto i = 0; i < m; i++) {
    cin >> d[i];
    dp[d[i]] = 1;
  }
  for (auto i = 0; i < n + 1; i++) {
    for (auto &j : d) {
      if (i + j >= n + 1)
        continue;
      dp[i + j] = min(dp[i + j], dp[i] + 1);
    }
  }
  cout << dp[n] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  constexpr int INF = 1e9;
  int n, m;
  cin >> n >> m;
  vector<int> dp(n + 1, INF), d(m);
  for (auto i = 0; i < m; i++) {
    cin >> d[i];
    dp[d[i]] = 1;
  }
  for (auto i = 0; i < n + 1; i++) {
    for (auto &j : d) {
      if (i + j > n + 1)
        continue;
      dp[i + j] = min(dp[i + j], dp[i] + 1);
    }
  }
  cout << dp[n] << endl;
  return 0;
}
Problem p02314 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
#define INF (1 << 30)
using namespace std;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<long long> dp(n + 1, INF);
  vector<int> c(m, 0);
  for (int i = 0; i < m; i++) {
    cin >> c[i];
  }
  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    for (auto val : c) {
      if (i - val < 0)
        continue;
      dp[i] = min(dp[i], dp[i - val] + 1);
    }
  }
  cout << dp[n] << endl;
}

Error: -6
munmap_chunk(): invalid pointer


Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
#define INF (1 << 30)
using namespace std;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<long long> dp(n, INF);
  vector<int> c(m, 0);
  for (int i = 0; i < m; i++) {
    cin >> c[i];
  }
  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    for (auto val : c) {
      if (i - val < 0)
        continue;
      dp[i] = min(dp[i], dp[i - val] + 1);
    }
  }
  cout << dp[n] << endl;
}
Problem p02314 (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;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)

#define INF INT_MAX / 2 - 1
#define PI 2 * acos(0.0)
#define ll long long

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

int main() {
  int n, m;
  cin >> n >> m;
  int c[20];
  REP(i, m)
  cin >> c[i];
  int dp[50001];
  fill_n(dp, 50001, INF);
  dp[0] = 0;
  REP(i, m)
  FOR(j, c[i], n + 1)
  dp[j] = min(dp[j], dp[j - c[i]] + 1);
  cout << dp[n] << 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;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)

#define INF INT_MAX / 2 - 1
#define PI 2 * acos(0.0)
#define ll long long

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

int main() {
  int n, m;
  cin >> n >> m;
  int c[20];
  REP(i, m)
  cin >> c[i];
  int dp[10001];
  fill_n(dp, 10001, INF);
  dp[0] = 0;
  REP(i, m)
  FOR(j, c[i], n + 1)
  dp[j] = min(dp[j], dp[j - c[i]] + 1);
  cout << dp[n] << endl;
  return 0;
}