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

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
using ll = long long int;
#define rep(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define pb push_back
#define SORT(v, n) sort(v, v + n)
#define ALL(x) (x).begin(), (x).end()
#define debug(x) cerr << #x << ": " << x << '\\n'
#define elif else if
#define int ll
// const int INF = 100100100;
const int INF = (1LL << 32);
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
typedef vector<vector<int>> vvi;
typedef vector<vector<vector<int>>> vvvi;

signed main() {
  ios::sync_with_stdio(false);
  static const int MMAX = 20;
  static const int NMAX = 50000;
  static const int INFTY = (1 << 29);

  int n, m;
  cin >> n >> m;
  int C[21];
  int T[NMAX + 1];
  for (int i = 1; i <= m; i++)
    cin >> C[i];
  rep(i, NMAX) T[i] = INFTY;
  T[0] = 0;
  for (int i = 1; i <= m; i++) {
    for (int j = 0; j + C[i] <= n; j++) {
      T[j + C[i]] = min(T[j + C[i]], T[j] + 1);
    }
  }

  cout << T[n] << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <iostream>
using namespace std;

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

  int ans = 0;
  for (i = 5; i >= 0; i--) {
    int t = min(n / V[i], 1);
    n -= t * V[i];
    ans += t;
  }

  cout << ans - 1 << endl;

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

#include <algorithm>
#include <iostream>
#define N 50001
#define INFINITY 2000000000
using namespace std;
const int M = 21;
void getTheNumberOfCoin();
int n, m, T[M][N], C[M], i, j, minv;
int main() {
  cin >> n >> m;
  for (i = 1; i <= m; i++)
    cin >> C[i];
  getTheNumberOfCoin();
  return 0;
}
void getTheNumberOfCoin() {
  for (i = 1; i <= n; i++)
    T[0][i] = INFINITY;
  for (i = 1; i <= m; i++)
    T[i][0] = 0;
  for (i = 1; i <= m; i++) {
    for (j = 1; j <= n; j++) {
      if (j >= C[i]) {
        T[i][j] = min(T[i - 1][j], T[i][j - C[i]] + 1);
      } else {
        T[i][j] = T[i - 1][j];
      }
    }
  }
  minv = INFINITY;
  for (i = 1; i <= m; i++) {
    minv = min(minv, T[i][n]);
  }
  cout << minv << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#define N 10001
#define INFINITY 2000000000
using namespace std;
const int M = 21;
void getTheNumberOfCoin();
int n, m, T[M][N], C[M], i, j, minv;
int main() {
  cin >> n >> m;
  for (i = 1; i <= m; i++)
    cin >> C[i];
  getTheNumberOfCoin();
  return 0;
}
void getTheNumberOfCoin() {
  for (i = 1; i <= n; i++)
    T[0][i] = INFINITY;
  for (i = 1; i <= m; i++)
    T[i][0] = 0;
  for (i = 1; i <= m; i++) {
    for (j = 1; j <= n; j++) {
      if (j >= C[i]) {
        T[i][j] = min(T[i - 1][j], T[i][j - C[i]] + 1);
      } else {
        T[i][j] = T[i - 1][j];
      }
    }
  }
  minv = INFINITY;
  for (i = 1; i <= m; i++) {
    minv = min(minv, T[i][n]);
  }
  cout << minv << endl;
}
Problem p02314 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <array>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

class Coin {
public:
  int n, m;
  vector<int> c;
  vector<vector<int>> dp;
  Coin() {}
  Coin(int n, int m) : n(n), m(m), c(m), dp(m, vector<int>(n + 1)) {}
  int solve();
};

int Coin::solve() {
  for (auto i = 0; i <= n; ++i)
    dp[0][i] = i;

  for (auto i = 0; i < m; ++i)
    dp[i][0] = 0;

  sort(c.begin(), c.end());

  for (auto i = 1; i < m; ++i) {
    for (auto j = 1; j <= n; ++j) {
      int mc = dp[i - 1][j];

      if (j >= c[i]) {
        mc = min(mc, dp[i - 1][j - c[i]] + 1);
        mc = min(mc, dp[i][j - c[i]] + 1);
      }

      dp[i][j] = mc;
    }
  }

  cout << dp[m - 1][n] << endl;

  return 0;
}

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

  Coin c(n, m);

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

  c.solve();

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

class Coin {
public:
  int n, m;
  vector<int> c;
  vector<vector<int>> dp;
  Coin() {}
  Coin(int n, int m) : n(n), m(m), c(m), dp(m, vector<int>(n + 1)) {}
  int solve();
};

int Coin::solve() {
  for (auto i = 0; i <= n; ++i)
    dp[0][i] = i;

  for (auto i = 0; i < m; ++i)
    dp[i][0] = 0;

  sort(c.begin(), c.end());

  for (auto i = 1; i < m; ++i) {
    for (auto j = 1; j <= n; ++j) {
      int mc = dp[i - 1][j];

      for (auto k = 1; j >= k * c[i]; ++k) {
        mc = min(mc, dp[i - 1][j - k * c[i]] + k);
      }

      dp[i][j] = mc;
    }
  }

  cout << dp[m - 1][n] << endl;

  return 0;
}

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

  Coin c(n, m);

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

  c.solve();

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

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ALL(g) (g).begin(), (g).end()
#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define P(p) cout << (p) << endl;
#define p(p) cout << (p) << " ";
#define pb push_back
#define mp make_pair
#define INF 1 << 25

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pi;
typedef long long ll;

int dy[8] = {1, 1, 1, 0, 0, -1, -1, -1};
int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
struct S {
  int a, b, c;
};
bool asc(const S &left, const S &right) { return left.c > right.c; }
int dp[21][50001];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vi c(m);
  rep(i, m) cin >> c[i];
  rep(i, m + 1) rep(j, n + 1) dp[i][j] = INF;
  dp[0][0] = 0;
  rep(i, m) {
    rep(j, n + 1) {
      if (j - c[i] >= 0)
        dp[i + 1][j] = min(dp[i][j], dp[i + 1][j - c[i]] + 1);
      else
        dp[i + 1][j] = dp[i][j];
    }
  }
  /*REP(i,1,m+1){
          rep(j,n+1)p(dp[i][j]);
          cout<<endl;
  }*/
  P(dp[m][n]);
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ALL(g) (g).begin(), (g).end()
#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define P(p) cout << (p) << endl;
#define p(p) cout << (p) << " ";
#define pb push_back
#define mp make_pair
#define INF 1 << 25

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pi;
typedef long long ll;

int dy[8] = {1, 1, 1, 0, 0, -1, -1, -1};
int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
struct S {
  int a, b, c;
};
bool asc(const S &left, const S &right) { return left.c > right.c; }
int dp[21][10001];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vi c(m);
  rep(i, m) cin >> c[i];
  rep(i, m + 1) rep(j, n + 1) dp[i][j] = INF;
  dp[0][0] = 0;
  rep(i, m) {
    rep(j, n + 1) {
      if (j - c[i] >= 0)
        dp[i + 1][j] = min(dp[i][j], dp[i + 1][j - c[i]] + 1);
      else
        dp[i + 1][j] = dp[i][j];
    }
  }
  /*REP(i,1,m+1){
          rep(j,n+1)p(dp[i][j]);
          cout<<endl;
  }*/
  P(dp[m][n]);
  return 0;
}
Problem p02314 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ALL(g) (g).begin(), (g).end()
#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define P(p) cout << (p) << endl;
#define p(p) cout << (p) << " ";
#define pb push_back
#define mp make_pair
#define INF 1 << 25

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pi;
typedef long long ll;

int dy[8] = {1, 1, 1, 0, 0, -1, -1, -1};
int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
struct S {
  int a, b, c;
};
bool asc(const S &left, const S &right) { return left.c > right.c; }

int n, m;
int c[21];
int dp[21][50001]; // pos,yen/min
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n >> m;
  rep(i, m) cin >> c[i];
  rep(i, n + 1) dp[0][i] = i * c[0];
  REP(i, 1, m) {
    REP(j, 1, n + 1) {
      if (j - c[i] >= 0)
        dp[i][j] = min(dp[i - 1][j], dp[i][j - c[i]] + 1);
      else
        dp[i][j] = dp[i - 1][j];
    }
  }
  /*	rep(i,m){
                  REP(j,1,n+1)p(dp[i][j]);
                  cout<<endl;
          }*/
  P(dp[m - 1][n]);
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ALL(g) (g).begin(), (g).end()
#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define P(p) cout << (p) << endl;
#define p(p) cout << (p) << " ";
#define pb push_back
#define mp make_pair
#define INF 1 << 25

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pi;
typedef long long ll;

int dy[8] = {1, 1, 1, 0, 0, -1, -1, -1};
int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
struct S {
  int a, b, c;
};
bool asc(const S &left, const S &right) { return left.c > right.c; }

int n, m;
int c[21];
int dp[21][10001]; // pos,yen/min
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n >> m;
  rep(i, m) cin >> c[i];
  rep(i, n + 1) dp[0][i] = i * c[0];
  REP(i, 1, m) {
    REP(j, 1, n + 1) {
      if (j - c[i] >= 0)
        dp[i][j] = min(dp[i - 1][j], dp[i][j - c[i]] + 1);
      else
        dp[i][j] = dp[i - 1][j];
    }
  }
  /*	rep(i,m){
                  REP(j,1,n+1)p(dp[i][j]);
                  cout<<endl;
          }*/
  P(dp[m - 1][n]);
  return 0;
}
Problem p02314 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define REP(i, N) for (int i = 0; i < (int)N; i++)

#define M 20
#define N 50001

int d[M];
int DP[M][N];

int solve(int i, int x) {
  int res = N;
  if (DP[i][x] != N)
    return DP[i][x];
  res = solve(i - 1, x);
  if (x - d[i] >= 0)
    res = min(res, solve(i, x - d[i]) + 1);
  DP[i][x] = res;
  return res;
}

int main() {
  int n, m;

  cin >> n >> m;
  REP(i, M) cin >> d[i];

  for (int x = 0; x <= n; x++) {
    for (int i = 0; i < m; i++)
      DP[i][x] = N;
  }
  for (int k = 0, kMax = n / d[0]; k <= kMax; k++)
    DP[0][k * d[0]] = k;

  cout << solve(m - 1, n) << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define REP(i, N) for (int i = 0; i < (int)N; i++)

#define M 20
#define N 50001

int d[M];
int DP[M][N];

int solve(int i, int x) {
  int res = N;
  if (DP[i][x] != N)
    return DP[i][x];
  for (int k = 0, kMax = x / d[i]; k <= kMax; k++)
    res = min(res, solve(i - 1, x - k * d[i]) + k);
  DP[i][x] = res;
  return res;
}

int main() {
  int n, m;

  cin >> n >> m;
  REP(i, M) cin >> d[i];

  for (int x = 0; x <= n; x++) {
    for (int i = 0; i < m; i++)
      DP[i][x] = N;
  }
  for (int k = 0, kMax = n / d[0]; k <= kMax; k++)
    DP[0][k * d[0]] = k;

  cout << solve(m - 1, n) << endl;

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define i_7 (ll)(1E9 + 7)
#define i_5 (ll)(1E9 + 5)
ll mod(ll a) {
  ll c = a % i_7;
  if (c >= 0)
    return c;
  else
    return c + i_7;
}
typedef pair<int, int> i_i;
typedef pair<ll, ll> l_l;
ll inf = (ll)1E12; /*10^12*/
#define rep(i, l, r) for (ll i = l; i <= r; i++)
#define pb push_back
ll max(ll a, ll b) {
  if (a < b)
    return b;
  else
    return a;
}
ll min(ll a, ll b) {
  if (a > b)
    return b;
  else
    return a;
}
////////////////////////////////////////

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, m;
  cin >> n >> m;
  ll c[m + 1];
  rep(i, 1, m) cin >> c[i];
  ll dp[m + 1][n + 1];
  fill(dp[0], dp[m + 1], inf);
  rep(i, 0, m) dp[i][0] = 0;
  rep(i, 1, m) {
    rep(j, 0, n) {
      dp[i][j] = dp[i - 1][j];
      if (j - c[i] >= 0) {
        ll q = min(dp[i][j - c[i]], dp[i - 1][j - c[i]]) + 1;
        dp[i][j] = min(q, dp[i][j]);
      }
    }
  }
  /*(i,0,m){
      rep(j,0,n){
          cout<<dp[i][j]<<" ";
      }
      cout<<endl;
  }*/
  cout << dp[m][n] << endl;

  return 0;
}


Error: TLE

Fixed submission:



#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
int mod(ll a) { return a % 1000000007; };
#define rep0(i, n) for (int i = 0; i < (n); i++)
#define rep(i, n) for (int i = 1; i <= n; i++)

#define N 100005
int n, m;
int c[25];
int dp[25][N];
int INF = 10000000;
int DP(int i, int j) {
  if (dp[i][j] != INF)
    ;
  else if (i == 0) {
    if (j == 0)
      dp[i][j] = 0;
    else
      dp[i][j] = INF;
  } else {
    dp[i][j] = DP(i - 1, j);
    int k = 1;
    while (j - c[i] * k >= 0) {
      dp[i][j] = min(dp[i][j], DP(i - 1, j - c[i] * k) + k);
      k++;
    }
  }
  return dp[i][j];
}

int main() {
  cin >> n >> m;
  rep(i, m) cin >> c[i];
  rep0(i, m + 1) rep0(j, n + 1) dp[i][j] = INF;
  dp[0][0] = 0;
  cout << DP(m, n) << endl;
  return 0;
}
Problem p02314 (C++) — buggy submission, verdict: Runtime Error

// DPL_1_A.cpp
//
#include <bits/stdc++.h>
using namespace std;
const int INF = INT_MAX;
int n, m;
int dp[50001], coin[20];
int solve() {
  fill(dp, dp + n + 1, INF);
  dp[0] = 0;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      if (i + coin[j] <= n)
        dp[i + coin[j]] = min(dp[i + coin[j]], dp[i] + 1);
  return dp[n];
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; ++i)
    cin >> coin[i];
  cout << solve() << endl;
  ;
  return 0;
}


Error: 0

Fixed submission:

// DPL_1_A.cpp
//
#include <bits/stdc++.h>
using namespace std;
const int INF = INT_MAX;
int n, m;
int dp[10001], coin[20];
int solve() {
  fill(dp, dp + n + 1, INF);
  dp[0] = 0;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      if (i + coin[j] <= n)
        dp[i + coin[j]] = min(dp[i + coin[j]], dp[i] + 1);
  return dp[n];
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; ++i)
    cin >> coin[i];
  cout << solve() << endl;
  ;
  return 0;
}
Problem p02314 (C++) — buggy submission, verdict: Runtime Error

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

const int MAX = 0x3f3f3f3f;
const int maxn = 50000 + 5;
const int maxm = 20 + 5;
int a[maxn];
int dp[maxn];

int main() {
  int n, m;
  while (scanf("%d %d", &n, &m) != EOF) {
    for (int i = 1; i <= m; i++)
      scanf("%d", &a[i]);

    for (int i = 1; i <= n; i++)
      dp[i] = MAX;
    dp[0] = 0;

    for (int i = 1; i <= m; i++)
      for (int j = a[i]; j <= n; j++) {
        dp[j] = min(dp[j], dp[j - a[i]] + 1);
        // printf("dp[%d][%d]=%d\\n", i, j, dp[j]);
      }

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


Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int N(5e4 + 10);
int c[30];
int t[N];

int main() {
  freopen("in.txt", "r", stdin);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++)
    cin >> c[i];
  for (int i = 0; i < N; i++)
    t[i] = 1 << 21;
  t[0] = 0;
  for (int i = 0; i < m; i++)
    for (int j = 0; j + c[i] <= n; j++)
      t[j + c[i]] = min(t[j + c[i]], t[j] + 1);
  cout << t[n] << endl;

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

/*************************************************************************
    > File Name: Coin_Changing_Problem.cpp
    > Author: Frade
    > Mail: frade@vip.sina.com.cn
    > Created Time: 2017年05月02日 星期二 16?09分33秒
 ************************************************************************/

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

int Getint() {
  char c = getchar();
  bool flag = false;
  int ans = 0;
  while (c != '-' && (c < '0' || c > '9'))
    c = getchar();
  if (c == '-')
    flag = true, c = getchar();
  while (c >= '0' && c <= '9')
    ans = ans * 10 + c - '0', c = getchar();
  return (flag == true) ? -ans : ans;
}

const int maxn = 50000;

int w[maxn], f[maxn];

int main() {
  for (int j = 1; j < maxn; j++)
    f[j] = -999999;

  int W = Getint();
  int N = Getint();
  for (int i = 1; i <= N; i++)
    w[i] = Getint();

  f[0] = 0;

  for (int i = 1; i <= N; i++) {
    for (int j = w[i]; j <= W; j++) {
      if (f[j] <= 0)
        f[j] = max(f[j], f[j - w[i]] + 1);
      else
        f[j] = min(f[j], f[j - w[i]] + 1);
    }
  }
  printf("%d\\n", f[W]);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
// #include<bits/stdc++.h> !POJ可用
using namespace std;

const int M = 50010;
const int N = 30;
int dp[M];
// int coin[5]={1,5,7,25};
// int coin[5]={1,5,10,23};
int coin[N];
int len = 5;
int MAX = 4000000;
int dfs(int n, int m) {
  if (dp[n] != 0) {
    return dp[n];
  }
  int result = MAX;
  int i;
  for (i = 0; i < m; ++i) {
    if (n - coin[i] > 0) {
      result = min(result, dfs(n - coin[i], m) + 1);
    }
  }
  return dp[n] = result;
}
void Init(int m) {
  int i;
  memset(dp, 0, sizeof(dp));
  for (i = 0; i < m; ++i) {
    scanf("%d", &coin[i]);
  }
  for (i = 0; i < m; ++i) {
    dp[coin[i]] = 1;
  }
}

int main() {
  int n, m;
  while (scanf("%d%d", &n, &m)) {
    Init(m);
    dfs(n, m);
    printf("%d\\n", dp[n]);
  }
}
Problem p02314 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int n, m;
vector<int> c;
int dp[50001][20];
void sortt() {
  sort(c.begin(), c.end());
  reverse(c.begin(), c.end());
}

void ume() {

  for (int i = 0; i < 20; ++i) {
    dp[0][i] = 0;
    for (int j = 1; j < 50001; ++j) {
      dp[j][i] = 100000;
    }
  }
}
void toku() {
  for (int i = 1; i < n + 1; ++i) { //??????
    for (int j = 0; j < m; ++j) {   //?????¨???????????????
      if (i - c[j] >= 0) {
        dp[i][j] = dp[i - c[j]][j] + 1;
      }
      if (j != 0) {

        dp[i][j] = min(dp[i][j], dp[i][j - 1]);
      }
    }
  }
}

int main() {

  cin >> n >> m;

  for (int i = 0; i < m; ++i) {
    int a;
    cin >> a;
    c.emplace_back(a);
  }
  sortt();
  ume();
  toku();

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


Error: TLE

Fixed submission:

#include <iostream>
#include <set>

using namespace std;

int n, m;
int c[20];
set<int> a[50000];

int solve(int p) {
  set<int>::iterator it;
  for (it = a[p - 1].begin(); it != a[p - 1].end(); it++) {
    for (int i = 0; i < m; ++i) {
      int v = *it + c[i];
      if (v == n)
        return p;
      a[p].insert(v);
    }
  }
  return 0;
}

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

  a[0].insert(0);
  for (int j = 1; j <= 50000; ++j) {
    if (solve(j)) {
      cout << j << endl;
      break;
    }
  }

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

#include <algorithm>
#include <iostream>

using namespace std;

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

  int coins[m];
  for (int i = 0; i < m; ++i) {
    cin >> coins[i];
  }
  sort(coins, coins + m, greater<>());

  int dp[m + 1][50001];
  for (int i = 0; i <= 50000; ++i) {
    if (i == 0)
      dp[m][i] = 0;
    else
      dp[m][i] = 1'000'000'000;
  }

  for (int i = m - 1; i >= 0; --i) {
    for (int j = 0; j <= n; ++j) {
      if (j < coins[i]) {
        dp[i][j] = dp[i + 1][j];
      } else {
        dp[i][j] = min(dp[i + 1][j], dp[i][j - coins[i]] + 1);
      }
    }
  }

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


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

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

  int coins[m];
  for (int i = 0; i < m; ++i) {
    cin >> coins[i];
  }
  sort(coins, coins + m, greater<>());

  int dp[m + 1][50001];
  for (int i = 0; i <= 50000; ++i) {
    if (i == 0)
      dp[m][i] = 0;
    else
      dp[m][i] = 1'000'000'000;
  }

  for (int i = m - 1; i >= 0; --i) {
    for (int j = 0; j <= n; ++j) {
      if (j < coins[i]) {
        dp[i][j] = dp[i + 1][j];
      } else {
        int min_val = dp[i + 1][j];
        for (int k = 0; k <= (j / coins[i]); ++k) {
          min_val = min(min_val, dp[i + 1][j - coins[i] * k] + k);
        }
        dp[i][j] = min_val;
      }
    }
  }

  cout << dp[0][n] << endl;
}
Problem p02314 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>

using namespace std;

int main() {
  int kosuto[100000];
  int n, m;
  cin >> n >> m;
  int num[m];
  for (int i = 0; i < m; i++) {
    cin >> num[i];
    kosuto[num[i]] = 1;
  }
  int flag = 1;
  while (flag) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (kosuto[i] > 0) {
          if (kosuto[i + num[j]] == 0)
            kosuto[i + num[j]] = kosuto[i] + 1;
          else {
            kosuto[i + num[j]] = min(kosuto[i + num[j]], kosuto[i] + 1);
            if (i + num[j] == n)
              flag = 0;
          }
        }
      }
    }
  }
  cout << kosuto[n] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

int main() {
  int kosuto[100000];
  int n, m;
  cin >> n >> m;
  int num[m];
  for (int i = 0; i < m; i++) {
    cin >> num[i];
    kosuto[num[i]] = 1;
  }
  while (1) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (kosuto[i] > 0) {
          if (kosuto[i + num[j]] == 0)
            kosuto[i + num[j]] = kosuto[i] + 1;
          else {
            kosuto[i + num[j]] = min(kosuto[i + num[j]], kosuto[i] + 1);
            if (i + num[j] == n)
              break;
          }
        }
      }
    }
  }
  cout << kosuto[n] << endl;
  return 0;
}