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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctype.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <vector>

#define ll long long
#define LL long long
#define ULL unsigned long long
#define ull unsigned long long
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < n; i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
const ll INF = -10000000000;
using namespace std;
ll dp[50000][20] = {};

int main() {
  int n, m;
  int c[20];

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

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

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

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctype.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <vector>

#define ll long long
#define LL long long
#define ULL unsigned long long
#define ull unsigned long long
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < n; i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
const ll INF = -10000000000;
using namespace std;

int main() {
  int n, m;
  int c[20];
  int dp[5000][20] = {};
  cin >> n >> m;
  REP(i, m) cin >> c[i];

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

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

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

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

  //??\\???
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int tmp;
    cin >> tmp;
    c.push_back(tmp);
  }
  sort(c.begin(), c.end());

  // dp?????????
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i <= n; i++)
    dp[0][i] = i;

  //???????¨?????????§?§£???
  for (int i = 1; i < m; i++) {
    for (int j = 0; j <= n; j++) {
      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];
    }
  }

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

  return 0;
}

Error: TLE

Fixed submission:

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

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

  //??\\???
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int tmp;
    cin >> tmp;
    c.push_back(tmp);
  }
  sort(c.begin(), c.end());

  // dp?????????
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i <= n; i++)
    dp[0][i] = i;

  //???????¨?????????§?§£???
  for (int i = 1; i < m; i++) {
    for (int j = 0; j <= n; j++) {
      int min_coins = dp[i - 1][j]; // ????°???????????????°
      int Cj_num = 1;               // Cj????????????????????°
      while (j - c[i] * Cj_num >= 0) {
        if (min_coins > dp[i - 1][j - c[i] * Cj_num] + Cj_num) {
          min_coins = dp[i - 1][j - c[i] * Cj_num] + Cj_num;
        }
        Cj_num++;
      }
      dp[i][j] = min_coins;
    }
  }

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

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

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DPL_1_A
#include <algorithm>
#include <climits>
#include <iostream>
#include <vector>

using namespace std;

int dp[20][65536];

int main(int argc, char **argv) {
  int n, m;
  cin >> n >> m;
  vector<int> c(m);
  for (int i = 0; i < m; i++)
    cin >> c[i];
  sort(c.begin(), c.end());
  for (int i = 0; i < m; i++) {
    for (int j = 0; j <= n; j++) {
      if (!i) {
        dp[i][j] = j;
      } else 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 - 1][n] << endl;
  return 0;
}

Error: TLE

Fixed submission:

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DPL_1_A
#include <algorithm>
#include <climits>
#include <iostream>
#include <vector>

using namespace std;

int dp[20][65536];

int main(int argc, char **argv) {
  int n, m;
  cin >> n >> m;
  vector<int> c(m);
  for (int i = 0; i < m; i++)
    cin >> c[i];
  sort(c.begin(), c.end());
  for (int i = 0; i < m; i++) {
    for (int j = 0; j <= n; j++) {
      if (!i) {
        dp[i][j] = j;
      } else {
        int a = INT_MAX;
        for (int k = j, l = 0; k >= 0; k -= c[i], l++)
          a = min(a, dp[i - 1][k] + l);
        dp[i][j] = a;
      }
    }
  }
  cout << dp[m - 1][n] << endl;
  return 0;
}
Problem p02314 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define REP(i, n) rep(i, 0, n)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
typedef pair<int, int> P;
const int mod = 1000000007;
const int INF = 1e12;

int dp[50010];

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  rep(i, 0, 50010) dp[i] = INF;
  vector<int> c(m);
  rep(i, 0, m) cin >> c[i];
  dp[0] = 0;
  rep(i, 0, m) {
    rep(j, 0, 50010) {
      if (j + c[i] > 50000)
        continue;
      dp[j + c[i]] = min(dp[j + c[i]], dp[j] + 1);
    }
  }
  cout << dp[n] << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define REP(i, n) rep(i, 0, n)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
typedef pair<int, int> P;
const int mod = 1000000007;
const int INF = 1e12;

int dp[10010];

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  rep(i, 0, 10010) dp[i] = INF;
  vector<int> c(m);
  rep(i, 0, m) cin >> c[i];
  dp[0] = 0;
  rep(i, 0, m) {
    rep(j, 0, 10010) {
      if (j + c[i] >= 10000)
        continue;
      dp[j + c[i]] = min(dp[j + c[i]], dp[j] + 1);
    }
  }
  cout << dp[n] << endl;
}
Problem p02314 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
#define INF 10000000

using namespace std;

int main(int argc, char const *argv[]) {
  int n, m;
  cin >> n >> m;
  int *c = new int[m];
  int **dp = new int *[m];
  for (int i = 0; i < m; i++) {
    cin >> c[i];
    dp[i] = new int[n + 1];
    for (int j = 0; j <= n; j++) {
      if (j == 0) {
        dp[i][j] = 0;
      } else {
        dp[i][j] = INF;
      }
    }
  }
  for (int i = 0; i <= n / c[0]; i++) {
    dp[0][i * c[0]] = i;
  }
  for (int i = 0; i < m - 1; i++) {
    for (int j = 0; j <= n; j++) {
      dp[i + 1][j] = dp[i][j];
      if (j - c[i + 1] >= 0) {
        dp[i + 1][j] = min(dp[i + 1][j], dp[i + 1][j - c[i + 1]] + 1);
      }
    }
  }
  cout << dp[m - 1][n] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#define INF 100000000

using namespace std;

int *c;
int n, m;
int **cost;

int calc(int k, int sum) {
  if (k == m) {
    if (sum == 0)
      return 0;
    else
      return -1;
  }
  if (cost[k][sum] != -2)
    return cost[k][sum];
  int min = INF;
  for (int i = 0; i <= sum / c[k]; i++) {
    int res = calc(k + 1, sum - c[k] * i);
    if (res == -1)
      continue;
    int result = i + res;
    if (result < min)
      min = result;
  }
  if (min == INF)
    return cost[k][sum] = -1;
  return cost[k][sum] = min;
}

int main(int argc, char const *argv[]) {
  cin >> n >> m;
  c = new int[m];
  cost = new int *[m];
  for (int i = 0; i < m; i++) {
    cin >> c[i];
    cost[i] = new int[n + 1];
    for (int j = 0; j <= n; j++)
      cost[i][j] = -2;
  }
  sort(c, c + m, greater<int>());
  cout << calc(0, n) << endl;
  return 0;
}
Problem p02314 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <array>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <thread>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define FOR_LT(i, beg, end) for (int i = beg; i < end; i++)
#define FOR_LE(i, beg, end) for (int i = beg; i <= end; i++)
#define FOR_DW(i, beg, end) for (int i = beg; end <= i; i--)

using namespace std;

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

  vector<int> ms(m);
  for (auto &coin : ms) {
    cin >> coin;
  }
  sort(ms.begin(), ms.end(), greater<int>());

  array<int, 50001> dp = {0};
  FOR_LE(i, 1, n) {
    int min_c = INT_MAX;
    FOR_LT(c, 0, m) {
      if (i - ms[c] < 0)
        continue;
      min_c = min(min_c, dp[i - ms[c]] + 1);
    }
    dp[i] = min_c;
  }

  cout << dp[n] << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <array>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <thread>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define FOR_LT(i, beg, end) for (int i = beg; i < end; i++)
#define FOR_LE(i, beg, end) for (int i = beg; i <= end; i++)
#define FOR_DW(i, beg, end) for (int i = beg; end <= i; i--)

using namespace std;

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

  vector<int> ms(m);
  for (auto &coin : ms) {
    cin >> coin;
  }
  sort(ms.begin(), ms.end(), greater<int>());

  array<int, 10001> dp = {0};
  FOR_LE(i, 1, n) {
    int min_c = INT_MAX;
    FOR_LT(c, 0, m) {
      if (i - ms[c] < 0)
        continue;
      min_c = min(min_c, dp[i - ms[c]] + 1);
    }
    dp[i] = min_c;
  }

  cout << dp[n] << endl;

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

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

typedef long long ll;
#define INF 1000000

int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> c;
  for (ll i = 0; i < m; i++) {
    ll tmp;
    cin >> tmp;
    c.push_back(tmp);
  }
  ll dp[n + 1]; // dp[i]:=i????????????????????????????????????????°??????°
  for (ll i = 0; i <= n; i++)
    dp[i] = INF;
  dp[0] = 0;
  for (ll i = 0; i < c.size(); i++) {
    for (ll j = 0; j <= n; j++) {
      if (c[i] <= j)
        dp[j] = min(dp[j], dp[j - c[i]] + 1);
    }
  }
  cout << dp[n] << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

typedef long long ll;
#define INF 1000000

int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> c;
  for (ll i = 0; i < m; i++) {
    ll tmp;
    cin >> tmp;
    c.push_back(tmp);
  }
  ll dp[n + 1]; // dp[i]:=i????????????????????????????????????????°??????°
  for (ll i = 0; i <= n; i++)
    dp[i] = INF;
  dp[0] = 0;
  for (ll i = 0; i < c.size(); i++) {
    for (ll j = 0; j <= n; j++) {
      ll mini = dp[j];
      for (ll k = 1; j - c[i] * k >= 0; k++) {
        if (mini > dp[j - c[i] * k] + k)
          mini = dp[j - c[i] * k] + k;
      }
      dp[j] = mini;
    }
  }
  cout << dp[n] << endl;
  return 0;
}
Problem p02314 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

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

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

// ---------------------
#define INF 922337203685477580
#define IINF (1 << 21)
typedef long long ll;

int dp[22][50100];
int n, m;
int coin[22];

int main() {
  cin >> n >> m;
  REP(i, m) { cin >> coin[i + 1]; }

  REP(i, 50100) { dp[0][i] = IINF; }
  REP(i, 22) { dp[i][0] = 0; }

  // coin, kane

  FOR(c, 1, m + 1) {
    FOR(k, 1, n + 1) {
      if (k - coin[c] >= 0) {
        dp[c][k] = min(dp[c][k - coin[c]] + 1, dp[c - 1][k]);
      } else {
        dp[c][k] = dp[c - 1][k];
      }
    }
  }

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

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

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

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

// ---------------------
#define INF 922337203685477580
#define IINF (1 << 21)
typedef long long ll;

int dp[22][10100];
int n, m;
int coin[22];

int main() {
  cin >> n >> m;
  REP(i, m) { cin >> coin[i + 1]; }

  REP(i, 10100) { dp[0][i] = IINF; }
  REP(i, 22) { dp[i][0] = 0; }

  // coin, kane

  FOR(c, 1, m + 1) {
    FOR(k, 1, n + 1) {
      if (k - coin[c] >= 0) {
        dp[c][k] = min(dp[c][k - coin[c]] + 1, dp[c - 1][k]);
      } else {
        dp[c][k] = dp[c - 1][k];
      }
    }
  }

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

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

int value[100000];
int dp[100000];

int main() {

  int n;
  int m;

  while (~scanf("%d%d", &n, &m)) {
    for (int i = 0; i < m; i++)
      scanf("%d", &value[i]);
    for (int i = 0; i <= n; i++) {
      dp[i] = i + 1;
    }

    dp[0] = 0;

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

    // for(int i=0;i<n;i++) printf("%d ",dp[i]);

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

Error: 0

Fixed submission:

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

int value[1000];
int dp[1000];

int main() {

  int n;
  int m;

  while (~scanf("%d%d", &n, &m)) {
    for (int i = 0; i < m; i++)
      scanf("%d", &value[i]);
    for (int i = 0; i <= n; i++)
      dp[i] = i + 1;

    dp[0] = 0;

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

    // for(int i=0;i<n;i++) printf("%d ",dp[i]);

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

55 4
1 5 10 50
15 6
1 2 7 8 12 50
65 6
1 2 7 8 12 50

*/
Problem p02314 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define rep(index, from, to) for (int index = from; index < to; index++)

using namespace std;

int main() {
  int n, m;
  int c[25];
  int dp[50010];
  cin >> n >> m;
  rep(i, 0, m) cin >> c[i];
  sort(c, c + m);
  rep(j, 0, n + 1) dp[j] = j;
  rep(i, 1, m) {
    rep(j, 0, n + 1 - c[i]) { dp[j + c[i]] = min(dp[j + c[i]], dp[j] + 1); }
  }

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

Error: TLE

Fixed submission:


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

#define rep(index, from, to) for (int index = from; index < to; index++)

using namespace std;

int main() {
  int n, m;
  int c[25];
  int dp[50010];
  cin >> n >> m;
  rep(i, 0, m) cin >> c[i];
  sort(c, c + m);
  rep(j, 0, n + 1) dp[j] = j;
  rep(i, 1, m) {
    rep(j, c[i], n + 1) {
      for (int k = 1; j - c[i] * k >= 0; k++) {
        dp[j] = min(dp[j], dp[j - c[i] * k] + k);
      }
    }
  }

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <string>

using namespace std;

const long long int MOD = 1000000007;
const long long int INF = 100000000;
long long int N, M;
long long int C[30];
long long int dp[21][100000];

int DP(int n, int v) {
  if (n < 0 || v < 0)
    return INF;

  if (dp[n][v] != -1)
    return dp[n][v];
  return dp[n][v] = min(DP(n - 1, v), DP(n, v - C[n]) + 1);
}

int main() {
  cin >> N >> M;

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

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

  for (int i = 0; i < 21; i++) {
    dp[i][0] = 0;
  }
  cout << DP(M - 1, N) << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <string>

using namespace std;

const long long int MOD = 1000000007;
const long long int INF = 100000000;
long long int N, M;
long long int C[30];
long long int dp[21][100000];

int DP(int n, int v) {
  if (n < 0 || v < 0)
    return INF;

  if (dp[n][v] != -1)
    return dp[n][v];
  return min(DP(n - 1, v), DP(n, v - C[n]) + 1);
}

int main() {
  cin >> N >> M;

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

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

  for (int i = 0; i < 21; i++) {
    dp[i][0] = 0;
  }
  cout << DP(M - 1, N) << endl;

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

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

const int MAX_N = 50000;
const int MAX_M = 20;

int d[MAX_M];
int dp[MAX_M + 1][MAX_N + 1];
int n, m;

void solve() {
  for (int i = 0; i <= m; i++) {
    for (int j = 0; j <= n; j++) {
      if (j == 0) {
        dp[i][j] = 0;
      } else if (i == 0) {
        dp[i][j] = 10000;
      } else if (j < d[i - 1]) {
        dp[i][j] = dp[i - 1][j];
      } else {
        dp[i][j] = min(dp[i - 1][j], dp[i][j - d[i - 1]] + 1);
      }
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d", &d[i]);
  }

  solve();

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

Error: -11

Fixed submission:

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

const int MAX_N = 50000;
const int MAX_M = 20;

int d[MAX_M];
int dp[MAX_M + 1][MAX_N + 1];
int n, m;

void solve() {
  for (int i = 0; i <= m; i++) {
    for (int j = 0; j <= n; j++) {
      if (j == 0) {
        dp[i][j] = 0;
      } else if (j < d[i]) {
        dp[i][j] = dp[i - 1][j];
      } else {
        dp[i][j] = min(dp[i - 1][j], dp[i][j - d[i - 1]] + 1);
      }
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d", &d[i]);
  }
  memset(dp, 300, sizeof(dp));

  solve();

  printf("%d\\n", dp[m][n]);
  return 0;
}
Problem p02314 (C++) — buggy submission, verdict: Runtime Error

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

const int MAX = 20;
const int MAX_VALUE = 50001;
const int INF = 100000000;

int main() {
  int n, m;
  int c[MAX];
  int table[MAX_VALUE];

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

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

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

Error: 0

Fixed submission:

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

const int MAX = 20;
const int MAX_VALUE = 50000;
const int INF = 100000000;

int main() {
  int n, m;
  int c[MAX];
  int table[50001];

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

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

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

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

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef long long ll;
#define mod 1000000
#define INF 10000000

int main() {
  int n, m;
  cin >> n >> m;
  vi C(m);
  for (int i = 0; i < m; i++) {
    cin >> C[i];
  }
  vi dp(2 * n + 10, INF);
  dp[0] = 0;
  for (int i = 0; i < m; i++) {
    int c = C[i];
    for (int j = 0; j <= n; j++) {
      dp[j + c] = min(dp[j + c], 1 + dp[j]);
    }
    /*for (int j = 0; j <= n; j++) {
            cout << dp[j] << " ";
    }cout << endl;*/
  }
  cout << dp[n] << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef long long ll;
#define mod 1000000
#define INF 10000000

int main() {
  int n, m;
  cin >> n >> m;
  vi C(m);
  for (int i = 0; i < m; i++) {
    cin >> C[i];
  }
  vi dp(2 * n + 10, INF);
  dp[0] = 0;
  for (int i = 0; i < m; i++) {
    int c = C[i];
    for (int j = n; j >= 0; j--) {
      for (int k = 1; j + k * c <= n; k++) {
        dp[j + k * c] = min(dp[j + k * c], k + dp[j]);
      }
    }
    /*for (int j = 0; j <= n; j++) {
            cout << dp[j] << " ";
    }cout << endl;*/
  }
  cout << dp[n] << endl;
  return 0;
}