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

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

// Define
using ll = int64_t;
using ul = uint64_t;
using ld = long double;
const ll MOD = 1e9 + 7;
const ll INF = LONG_MAX;
const ul MAX = ULONG_MAX;
const char nl = '\\n';

#define elif else if
#define def inline auto
#define run __attribute__((constructor)) def _

// Debug
#define debug(x) cerr << (x) << " (L:" << __LINE__ << ")" << '\\n'

// Loop
#define inc(i, a, n) for (ll i = (a), _##i = (n); i < _##i; ++i)
#define dec(i, a, n) for (ll i = (a), _##i = (n); i > _##i; --i)
#define rep(i, n) inc(i, 0, n)

// Stream
#define fout(n) cout << fixed << setprecision(n)
#define fasten cin.tie(0), ios::sync_with_stdio(0)

// Speed
run() { fasten, fout(10); }
#pragma GCC optimize("-O3")
#pragma GCC target("avx")

// Math
inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

signed main() {
  int N, M;
  cin >> N >> M;
  vector<int> cs(M);
  rep(i, M) cin >> cs[i];
  sort(begin(cs), end(cs));

  vector<ll> dp(N + 1, INF);
  dp[0] = 0;
  rep(i, cs.size()) {
    rep(y, dp.size()) {
      if (y - cs[i] >= 0)
        dp[y] = min(dp[y - cs[i]] + 1, dp[y]);
    }
  }
  cout << dp[N] << nl;
}


Error: 0
0 (L:46)
1 (L:46)
2 (L:46)
3 (L:46)
4 (L:46)
1 (L:46)
2 (L:46)
3 (L:46)
4 (L:46)
5 (L:46)
1 (L:46)
2 (L:46)
3 (L:46)
4 (L:46)
5 (L:46)
2 (L:46)
3 (L:46)
4 (L:46)
5 (L:46)
6 (L:46)
2 (L:46)
3 (L:46)
4 (L:46)
5 (L:46)
6 (L:46)
3 (L:46)
4 (L:46)
5 (L:46)
6 (L:46)
7 (L:46)
3 (L:46)
4 (L:46)
5 (L:46)
6 (L:46)
7 (L:46)
4 (L:46)
5 (L:46)
6 (L:46)
7 (L:46)
8 (L:46)
4 (L:46)
5 (L:46)
6 (L:46)
7 (L:46)
8 (L:46)
5 (L:46)
6 (L:46)
7 (L:46)
8 (L:46)
9 (L:46)
1 (L:46)
2 (L:46)
3 (L:46)
4 (L:46)
5 (L:46)
2 (L:46)


Fixed submission:

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

// Define
using ll = int64_t;
using ul = uint64_t;
using ld = long double;
const ll MOD = 1e9 + 7;
const ll INF = LONG_MAX;
const ul MAX = ULONG_MAX;
const char nl = '\\n';

#define elif else if
#define def inline auto
#define run __attribute__((constructor)) def _

// Debug
#define debug(x) cerr << (x) << " (L:" << __LINE__ << ")" << '\\n'

// Loop
#define inc(i, a, n) for (ll i = (a), _##i = (n); i < _##i; ++i)
#define dec(i, a, n) for (ll i = (a), _##i = (n); i > _##i; --i)
#define rep(i, n) inc(i, 0, n)

// Stream
#define fout(n) cout << fixed << setprecision(n)
#define fasten cin.tie(0), ios::sync_with_stdio(0)

inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

signed main() {
  int N, M;
  cin >> N >> M;
  vector<int> cs(M);
  rep(i, M) cin >> cs[i];
  sort(begin(cs), end(cs));

  vector<ll> dp(N + 1, INF);
  dp[0] = 0;
  rep(i, cs.size()) {
    rep(y, dp.size()) {
      if (y - cs[i] >= 0)
        dp[y] = min(dp[y - cs[i]] + 1, dp[y]);
    }
  }
  rep(i, dp.size()) debug(dp[i]);
  cout << dp[N] << nl;
}
Problem p02314 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <limits>
#include <vector>

using namespace std;

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

  vector<int> dp(n + 1);
  vector<int> coins(m);
  for (int i = 0; i < m; ++i) {
    int coin;
    cin >> coin;
    coins[i] = coin;
  }

  dp[0] = 0;

  for (int val = 1; val <= n; ++val) {
    int min = numeric_limits<int>::max();
    for (auto coin : coins) {
      if (val >= coin) {
        min = dp[val - coin] < min ? dp[val - coin] : min;
      }
    }
    dp[val] = 1 + min;
  }
  cout << dp[n] << endl;
}


Error: 0

Fixed submission:

#include <iostream>
#include <limits>
#include <vector>

using namespace std;

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

  vector<int> dp(n);
  vector<int> coins(m);
  for (int i = 0; i < m; ++i) {
    int coin;
    cin >> coin;
    coins[i] = coin;
  }

  dp[0] = 0;

  for (int val = 1; val <= n; ++val) {
    int min = numeric_limits<int>::max();
    for (auto coin : coins) {
      if (val >= coin) {
        min = dp[val - coin] < min ? dp[val - coin] : min;
      }
    }
    dp[val] = 1 + min;
  }
  cout << dp[n] << endl;
}
Problem p02314 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int stoi(string x) {
  stringstream ss;
  ss << x;
  int tmp;
  ss >> tmp;
  return tmp;
}
string itos(int x) {
  stringstream ss;
  ss << x;
  return ss.str();
}
int dp[25][51000];
int c[25];
const int INF = 99999999;
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= m; i++)
    cin >> c[i];
  for (int i = 0; i <= m; i++)
    for (int j = 1; j <= n; j++)
      dp[i][j] = INF;

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

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int stoi(string x) {
  stringstream ss;
  ss << x;
  int tmp;
  ss >> tmp;
  return tmp;
}
string itos(int x) {
  stringstream ss;
  ss << x;
  return ss.str();
}
int dp[25][5000];
int c[25];
const int INF = 99;
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= m; i++)
    cin >> c[i];
  for (int i = 0; i <= m; i++)
    for (int j = 1; j <= n; j++)
      dp[i][j] = INF;

  for (int i = 1; i <= m; i++) {
    for (int j = 0; j <= n; j++) {
      if (j < c[i])
        dp[i][j] = dp[i - 1][j];
      else
        dp[i][j] = min(dp[i - 1][j], dp[i][j - c[i]] + 1);
    }
  }
  cout << dp[m][n] << endl;
  // for(int i=0;i<=m;i++){for(int j=0;j<=n;j++){cout<<dp[i][j]<<"
  // ";}cout<<endl;}
  return 0;
}
Problem p02314 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>

using namespace std;
const int MOD = 1e9 + 7;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> v(m);
  for (int i = 0; i < m; i++)
    cin >> v[i];
  sort(v.begin(), v.end(), greater<int>());

  vector<int> dp(n + 1, 1e9);

  dp[0] = 0;

  for (int i = 0; i < m; i++) {
    for (int j = v[i]; j <= n; j++) {
      dp[j] = min(dp[j], dp[j - v[i]] + 1);
    }
  }
  cout << dp[n] << endl;
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>

using namespace std;
const int MOD = 1e9 + 7;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> v(m);
  for (int i = 0; i < m; i++)
    cin >> v[i];
  sort(v.begin(), v.end(), greater<int>());

  vector<int> dp(n + 1, 1e9);

  dp[0] = 1;
  int a = ((n + *min_element(v.begin(), v.end())) + 1) /
          *min_element(v.begin(), v.end());
  while (a--) {
    for (int i = 0; i < m; i++) {
      for (int j = n; j >= 0; j--) {
        if (dp[j] > 0) {
          if (j + v[i] > n)
            continue;
          else {
            dp[j + v[i]] = min(dp[j + v[i]], dp[j] + 1);
          }
        }
      }
    }
  }
  cout << dp[n] - 1 << endl;
}
Problem p02314 (C++) — buggy submission, verdict: Time Limit Exceeded

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

static const int INFTY = 50000;

int main() {
  int n, m;
  cin >> n >> m;
  int T[n + 1], C[m + 1];
  for (int i = 1; i <= n; i++) {
    T[i] = INFTY;
  }
  T[0] = 0;

  for (int i = 1; i <= m; i++) {
    cin >> C[i];
  }

  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); // C[i]???????????????????????????
    }
  }

  cout << T[n] << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

static const int INFTY = 50000;

int main() {
  int n, m;
  cin >> n >> m;
  int T[n + 1];
  for (int i = 1; i <= n; i++) {
    T[i] = INFTY;
  }

  int num, cnt = 1;
  for (int i = 0; i < m; i++) {
    cin >> num;
    if (num <= n)
      T[num] = cnt;
  }

  while (T[n] == INFTY) {
    cnt++;
    for (int i = 1; i <= n; i++) {
      for (int j = i; j <= n; j++) {
        if (T[i] < INFTY && T[j] < INFTY && i + j <= n)
          T[i + j] = min(T[i + j], T[i] + T[j]);
      }
    }
  }

  cout << T[n] << endl;

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

#include <iostream>
#define INF 1e+9
using namespace std;

int main() {
  int n, m;
  int gaku[20];
  int dp[21][50001];
  cin >> n >> m;
  for (int i = m - 1; i >= 0; i--)
    cin >> gaku[i];
  for (int i = 0; i <= 50000; i++) {
    for (int j = 0; j <= 20; j++)
      dp[j][i] = INF;
  }
  dp[0][0] = 0;
  for (int i = 1; i <= m; i++) {
    for (int j = 0; j <= n; j++) {
      if (j - gaku[i - 1] < 0)
        dp[i][j] = dp[i - 1][j];
      else
        dp[i][j] = min(dp[i - 1][j], dp[i][j - gaku[i - 1]] + 1);
    }
  }
  cout << dp[m][n] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#define INF 1e+9
using namespace std;

int main() {
  int n, m;
  int gaku[20];
  int dp[21][50001];
  cin >> n >> m;
  for (int i = m - 1; i >= 0; i--)
    cin >> gaku[i];
  for (int i = 0; i <= 50000; i++) {
    for (int j = 0; j <= 20; j++)
      dp[j][i] = INF;
  }
  dp[0][0] = 0;
  for (int i = 1; i <= m; i++) {
    for (int j = 0; j <= n; j++) {
      for (int k = 0; k <= j / gaku[i - 1]; k++) {
        dp[i][j] = min(dp[i][j], dp[i - 1][j - k * gaku[i - 1]] + k);
      }
    }
  }
  cout << dp[m][n] << endl;
  return 0;
}
Problem p02314 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, m;
  cin >> n >> m;
  int d[m];
  REP(i, m)
  cin >> d[i];
  int cnt[n];
  REP(i, n)
  cnt[i] = n + 1;
  REP(i, n)
  REP(j, m) {
    if (d[j] == i + 1)
      cnt[i] = 1;
    else if (i - d[j] >= 0)
      cnt[i] = min(cnt[i], cnt[i - d[j]] + 1);
  }
  cout << cnt[n - 1] << '\\n';
}

Error: TLE

Fixed submission:

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

int main() {
  int n, m;
  cin >> n >> m;
  int d[m];
  REP(i, m)
  cin >> d[i];
  sort(d, d + m);
  // l[(n+1)*n]??????????????????????¨????????????°???
  bool l1[n];
  bool l2[n];

  REP(i, n) {
    l1[i] = false;
    l2[i] = false;
  }
  REP(i, m)
  l2[d[i] - 1] = true;
  REP(cnt, n) {
    if (l2[n - 1]) {
      cout << cnt + 1 << '\\n';
      return 0;
    }
    REP(i, n) {
      l1[i] = l2[i];
      l2[i] = false;
    }
    REP(i, n) {
      if (!l1[i])
        continue;
      else {
        REP(j, m) {
          if (i + d[j] < n)
            l2[i + d[j]] = true;
        }
      }
    }
  }
}
Problem p02314 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

unsigned short dp[20][50010];

int main(void) {
  int N, M;
  cin >> N >> M;
  vector<int> c(M);
  for (auto &i : c)
    cin >> i;
  sort(c.begin(), c.end());
  for (int j = 0; j <= N; j++)
    dp[0][j] = j;
  int m;
  for (int i = 1; i < M; i++) {
    for (int j = 0; j <= N; j++) {
      m = dp[i - 1][j];
      for (int k = j / c[i]; j / c[i] - k < 10 && k > 0; k--)
        m = min(m, dp[i - 1][j - k * c[i]] + k);
      dp[i][j] = m;
    }
  }
  cout << dp[M - 1][N] << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

using namespace std;

unsigned short dp[20][50010];

int main(void) {
  int N, M;
  cin >> N >> M;
  vector<int> c(M);
  for (auto &i : c)
    cin >> i;
  sort(c.begin(), c.end());
  for (int j = 0; j <= N; j++)
    dp[0][j] = j;
  int m;
  for (int i = 1; i < M; i++) {
    for (int j = 0; j <= N; j++) {
      m = dp[i - 1][j];
      for (int k = 1; j >= k * c[i]; k++)
        m = min(m, dp[i - 1][j - k * c[i]] + k);
      dp[i][j] = m;
    }
  }
  cout << dp[M - 1][N] << endl;
  return 0;
}
Problem p02314 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define P pair<ll, ll>
#define FOR(I, A, B) for (ll I = (A); I < (B); ++I)
#define FORR(I, A, B) for (ll I = (B - 1); I >= (A); --I)
const ll INF = 1e18 + 7;
const ll MOD = 1e9 + 7;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n, m;
  cin >> n >> m;
  vector<ll> c(m);
  FOR(i, 0, m) cin >> c[i];
  vector<ll> dp(n + 1);
  FOR(i, 0, n + 1) dp[i] = i;
  FOR(i, 0, m) {
    FOR(j, 0, n + 1) {
      if (j + c[i] <= n)
        dp[j + c[i]] = min(dp[j + c[i]], dp[j] + 1);
    }
  }
  cout << dp[n] << endl;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define P pair<ll, ll>
#define FOR(I, A, B) for (ll I = (A); I < (B); ++I)
#define FORR(I, A, B) for (ll I = (B - 1); I >= (A); --I)
const ll INF = 1e18 + 7;
const ll MOD = 1e9 + 7;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n, m;
  cin >> n >> m;
  vector<ll> c(m);
  FOR(i, 0, m) cin >> c[i];
  vector<ll> dp(n + 1);
  FOR(i, 0, n + 1) dp[i] = i;
  FOR(i, 0, m) {
    FORR(j, 0, n + 1) {
      if (j + c[j] <= n)
        dp[j + c[i]] = min(dp[j + c[i]], dp[j] + 1);
    }
  }
  cout << dp[n] << endl;
  return 0;
}
Problem p02314 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstring>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 1000000000
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;

int dp[22][50010];

int main() {

  REP(i, 22) {
    REP(j, 50010) { dp[i][j] = INF; }
  }

  int n, m;
  cin >> n >> m;
  vector<int> c(m, 0);
  REP(i, m) {
    cin >> c[i];
    // cout << c[i] << endl;
  }

  // dp[i][j]:i番目までを使って総和がjとなるような枚数の最小

  dp[0][0] = 0; // 0番目までで総和が0の枚数

  for (int i = 1; i <= c.size(); i++) {
    // cout << c[i-1] << endl;
    for (int j = 0; j <= n; j++) {

      if (c[i - 1] > j) { // c[i]を使えない場合
        dp[i][j] = dp[i - 1][j];
        // cout << "a" << " " << j << " " << dp[i][j] << endl;
      } else { // c[i]を使う場合
        dp[i][j] = min(dp[i - 1][j], dp[i][j - c[i - 1]] + 1);
        // cout << "b" << " " << j << " " << dp[i][j] << endl;
      }
    }
    // cout << endl;
  }

  cout << dp[c.size()][n] << endl;

  return 0;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstring>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 1000000000
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;

int dp[22][50010];

int main() {

  REP(i, 10000) {
    REP(j, 10000) { dp[i][j] = INF; }
  }

  int n, m;
  cin >> n >> m;
  vector<int> c(m, 0);
  REP(i, m) {
    cin >> c[i];
    // cout << c[i] << endl;
  }

  // dp[i][j]:i番目までを使って総和がjとなるような枚数の最小

  dp[0][0] = 0; // 0番目までで総和が0の枚数

  for (int i = 1; i <= c.size(); i++) {
    // cout << c[i-1] << endl;
    for (int j = 0; j <= n; j++) {

      if (c[i - 1] > j) { // c[i]を使えない場合
        dp[i][j] = dp[i - 1][j];
        // cout << "a" << " " << j << " " << dp[i][j] << endl;
      } else { // c[i]を使う場合
        dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - c[i - 1]] + 1);
        // cout << "b" << " " << j << " " << dp[i][j] << endl;
      }
    }
    // cout << endl;
  }

  cout << dp[c.size()][n] << endl;

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

#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int n, m, a[30][50001];
vector<int> c;

int solve(int i, int sum) {
  if (sum > n) {
    // cout<<i<<" "<<sum<<" "<<ans;
    return 50001;
  }
  if (a[i][sum] != 0)
    return a[i][sum];
  if (i == m && sum == n)
    return 0;
  if (i == m)
    return 50001;
  else {
    a[i][sum] = min(solve(i + 1, sum), solve(i + 1, sum + c[i]) + 1);
    a[i][sum] = min(solve(i, sum + c[i]) + 1, a[i][sum]);
    return a[i][sum];
  }
}

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

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int n, m, a[30][60000][30];
vector<int> c;

int solve(int i, int sum, int ans) {
  if (a[i][sum][ans] != 0)
    return a[i][sum][ans];
  if (i == m && sum == n)
    return ans;
  if (i == m)
    return 50001;
  if (sum > n) {
    // cout<<i<<" "<<sum<<" "<<ans;
    return 50001;
  } else {
    a[i][sum][ans] =
        min(solve(i + 1, sum, ans), solve(i + 1, sum + c[i], ans + 1));
    a[i][sum][ans] = min(solve(i, sum + c[i], ans + 1), a[i][sum][ans]);
    return a[i][sum][ans];
  }
}

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <vector>
#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define int long long
using namespace std;
typedef long long ll;
typedef long double ld;
const int inf = 999999999999999999;
using namespace std;
int n, m, a[20], dp[60001];
signed main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++)
    cin >> a[i];
  fill(dp, dp + 60001, inf);
  dp[0] = 0;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j <= n; j++) {
      dp[j + a[i]] = min(dp[j + a[i]], dp[j] + 1);
    }
  }
  cout << dp[n] << endl;
  getchar();
  getchar();
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <vector>
#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define int long long
using namespace std;
typedef long long ll;
typedef long double ld;
const int inf = 999999999999999999;
using namespace std;
int n, m, a[20], dp[60001];
signed main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++)
    cin >> a[i];
  fill(dp, dp + 60001, inf);
  dp[0] = 0;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j <= n; j++) {
      int z = 1;
      while (a[i] * z + j <= n) {
        dp[j + a[i] * z] = min(dp[j + a[i] * z], dp[j] + z);
        z++;
      }
    }
  }
  cout << dp[n] << endl;
  getchar();
  getchar();
}
Problem p02314 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int N = 50000;
const int INF = 1e8;
const int M = 20;

/*int rec(int n,int m){
    if(dp[n][m] != INF){
        return dp[n][m];
    }

    if(v[m] > n){
        return dp[n][m] = rec(n,m-1);
    }else{
        return dp[n][m] = min(rec(n,m),rec(n-n/v[m]*v[m],m-1));
    }
}*/

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

  vector<int> v(m);
  int dp[M + 1][N + 1];

  for (auto &t : v) {
    cin >> t;
  }

  for (int i = 0; i <= M; i++) {
    for (int j = 0; j <= N; j++) {
      dp[i][j] = (j == 0 ? 0 : INF);
    }
  }

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

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 50000;
const int INF = 1e8;
const int M = 20;

int main() {
  int n, m;

  cin >> n >> m;

  vector<int> v(m);
  int dp[N + 1];

  fill(dp, dp + N + 1, INF);

  for (auto &t : v) {
    cin >> t;
  }

  dp[0] = 0;

  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < v.size(); j++) {
      for (int k = 1; i - k * v[j] >= 0; k++) {
        dp[i] = min(dp[i], dp[i - k * v[j]] + k);
      }
    }
  }

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

#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>

using namespace std;

int dp[21][50001]; // i ????????§j?????????????°?????????°?????????

int main() {
  int n, m;

  cin >> n >> m;

  int d[m];

  for (int i = 0; i < m; i++) {
    cin >> d[i];
  }
  for (int i = 0; i < m + 1; i++) {
    for (int j = 0; j <= n; j++) {
      dp[i][j] = INT_MAX;
    }
  }

  for (int i = 1; i < m + 1; i++) {
    int curD = d[i - 1];

    for (int j = 1; j <= n; j++) {
      if (j % curD == 0) {
        dp[i][j] = j / curD;
      } else {
        dp[i][j] = INT_MAX;
      }
      dp[i][j] = min(dp[i - 1][j], dp[i][j]);

      if (j - curD > 0) {
        if (dp[i - 1][j - curD] != INT_MAX) {
          dp[i][j] = min(dp[i][j], dp[i - 1][j - curD] + 1);
        }
        if (dp[i][j - curD] != INT_MAX) {
          dp[i][j] = min(dp[i][j], dp[i][j - curD] + 1);
        }
      }
    }
  }

  cout << dp[m][n] << endl;
  /*
          for (int i = 1; i < m + 1; i++) {
                  for(int j = 1; j <= n; j++) {
                          if (dp[i][j] == INT_MAX) {
                                  cout << setw(4) << -1;
                          } else {
                                  cout << setw(4) << dp[i][j];
                          }
                  }
                  cout << endl;
          }
  */

  return 0;
}

Error: TLE

Fixed submission:

#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>

using namespace std;

int dp[21][50001]; // i ????????§j?????????????°?????????°?????????

int main() {
  int n, m;

  cin >> n >> m;

  int d[m];

  for (int i = 0; i < m; i++) {
    cin >> d[i];
  }
  for (int i = 0; i < m + 1; i++) {
    for (int j = 0; j <= n; j++) {
      dp[i][j] = INT_MAX;
    }
  }

  for (int i = 1; i < m + 1; i++) {
    int curD = d[i - 1];

    for (int j = 1; j <= n; j++) {
      int mod = j % curD;

      if (mod == 0) {
        dp[i][j] = j / curD;
      } else {
        dp[i][j] = INT_MAX;
      }

      for (int k = mod; k <= j; k += curD) {
        if (dp[i - 1][k] < INT_MAX) {
          dp[i][j] = min(dp[i][j], dp[i - 1][k] + (j - k) / curD);
        }
      }
    }
  }

  cout << dp[m][n] << endl;
  /*
          for (int i = 0; i < m + 1; i++) {
                  for(int j = 0; j <= n; j++) {
                          cout << setw(4) << dp[i][j];
                  }
                  cout << endl;
          }
  */

  return 0;
}