alexjercan/bugnet huggingface dataset:
Problem p02314 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define rep(i, begin, end)                                                     \\
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \\
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))

using namespace std;

const int N_MAX = 100000;

int coins[N_MAX];
int dp[21][N_MAX];

int main() {
  int n, m;
  cin >> n >> m;
  rep(i, 0, m) { cin >> coins[i]; }

  // init
  rep(row, 0, m + 1) dp[row][0] = 0;
  rep(col, 1, n + 1) dp[0][col] = 50001;

  // fill
  rep(row, 1, m + 1) {
    rep(col, 1, n + 1) {
      if (col - coins[row - 1] >= 0)
        dp[row][col] =
            min(dp[row - 1][col - coins[row - 1]] + 1,
                min(dp[row - 1][col], dp[row][col - coins[row - 1]] + 1));
      else
        dp[row][col] = dp[row - 1][col];
      /*
      while(col - k * coins[row-1] >= 0){
        use_k_min = min(use_k_min, dp[row-1][col - k * coins[row-1]] + k);
        k++;
        }*/
    }
  }
  cout << dp[m][n] << endl;

  /*
  rep(row, 0, m+1){
    rep(col, 0, n+1){
      cout << dp[row][col] << " ";
    }
    cout << endl;
  }
  */
  return 0;
}


Error: TLE

Fixed submission:

#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define rep(i, begin, end)                                                     \\
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \\
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))

using namespace std;

const int N_MAX = 100000;

int main() {
  int n, m;
  cin >> n >> m;
  int coins[N_MAX];
  int dp[21][N_MAX];

  rep(i, 0, m) { cin >> coins[i]; }

  // init
  rep(row, 0, m + 1) dp[row][0] = 0;
  rep(col, 1, n + 1) dp[0][col] = 50001;

  // fill
  rep(row, 1, m + 1) {
    rep(col, 1, n + 1) {
      int use_k_min = INT_MAX;
      int k = 0;
      while (col - k * coins[row - 1] >= 0) {
        // if(col - k * coins[row-1] == 0 || k == 0){
        // if(row==2){
        //   cout << "col: " << col << ", k: " << k << ", coins[row-1]: " <<
        //   coins[row-1] << endl;
        // }
        use_k_min = min(use_k_min, dp[row - 1][col - k * coins[row - 1]] + k);
        //}
        k++;
      }
      dp[row][col] = use_k_min;
    }
  }

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

  /*
  rep(row, 0, m+1){
    rep(col, 0, n+1){
      cout << dp[row][col] << " ";
    }
    cout << endl;
  }
  */
  return 0;
}
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 INF INT_MAX / 2

int main() {
  int n, m;
  int c[20];
  cin >> n >> m;
  for (int i = 0; i < m; i++)
    cin >> c[i];
  int dp[50000 + 1];
  fill_n(dp, n + 1, INF);
  dp[0] = 0;
  for (int i = 0; i < m; i++)
    for (int j = c[i]; j <= n; j++)
      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 INF INT_MAX / 2

int main() {
  int n, m;
  int c[20];
  cin >> n >> m;
  for (int i = 0; i < m; i++)
    cin >> c[i];
  int dp[50000 + 1];
  fill_n(dp, n + 1, INF);
  dp[0] = 0;
  for (int i = 0; i <= m; i++)
    for (int j = c[i]; j <= n; j++)
      dp[j] = min(dp[j], dp[j - c[i]] + 1);

  cout << dp[n] << endl;

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

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

int n, m;
int C[21];

int solve() {
  int t[21][50001];

  for (int i = 0; i <= m; i++)
    t[i][0] = 0;
  for (int j = 1; j <= n; j++)
    t[0][j] = 999999;

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

  return t[m][n];
}

int main(void) {

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

  int res = solve();

  cout << res << endl;

  return 0;
}

Error: 0

Fixed submission:

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

int n, m;
int C[50000];

int solve() {
  int t[20][50000];

  for (int i = 0; i <= m; i++)
    t[i][0] = 0;
  for (int j = 1; j <= n; j++)
    t[0][j] = 999999;

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

  return t[m][n];
}

int main(void) {

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

  int res = solve();

  cout << res << endl;

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

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

const int MAXN = 50000;
const int INF = 114514;

int main() {
  int N, M;
  int coin[21];
  int dp[MAXN + 1];

  memset(dp, INF, sizeof(dp));
  dp[0] = 0;

  scanf("%d%d", &N, &M);
  for (int i = 0; i < M; i++)
    scanf("%d", &coin[i]);

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

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

  return (0);
}

Error: 0

Fixed submission:

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

const int MAXN = 50000;
const int INF = 114514;

int main() {
  int N, M;
  int coin[21];
  int dp[MAXN + 1];

  memset(dp, INF, sizeof(dp));
  dp[0] = 0;

  scanf("%d%d", &N, &M);
  for (int i = 0; i < M; i++)
    scanf("%d", &coin[i]);

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

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

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

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <vector>
#define debug(x) cout << #x << "==" << x << endl;
using namespace std;

const int INF = 1e+8;

//??\\???
#define MAX_N 50000
#define MAX_M 20
int N;
int M;
int C[MAX_M];

// dp ????????????
int dp[MAX_N + 1];

void print_dp_array(int n) {
  for (int i = 0; i <= n; i++) {
    if (dp[i] == INF) {
      cout << "INF ";
    } else {
      cout << setw(3) << dp[i];
    }
  }
  cout << endl;
}

int main() {
  ios::sync_with_stdio(false);

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

  // ?????????
  fill(dp, dp + MAX_N + 1, INF);
  dp[0] = 0;

  for (int s = 0; s <= N; s++) {

    if (dp[s] != INF) {
      for (int i = 0; i < M; i++) {
        dp[s + C[i]] = min(dp[s + C[i]], dp[s] + 1);
      }
    }
  }

  cout << dp[N] << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <vector>
#define debug(x) cout << #x << "==" << x << endl;
using namespace std;

const int INF = 1e+8;

//??\\???
#define MAX_N 50000
#define MAX_M 20
int N;
int M;
int C[MAX_M];

// dp ????????????
int dp[MAX_N + 1];
bool checked[MAX_N + 1];

int least_coins(int s) {

  // if(checked[s] == true) { return dp[s]; }
  if (s == 0) {
    return 0;
  }
  if (s < 0) {
    return INF;
  }

  int mn = INF;
  for (int i = 0; i < M; i++) {
    mn = min(mn, least_coins(s - C[i]) + 1);
  }

  // checked[s] = true;
  return dp[s] = mn;
}

int main() {
  ios::sync_with_stdio(false);

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

  cout << least_coins(N) << endl;

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

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <vector>
#define debug(x) cout << #x << "==" << x << endl;
using namespace std;

const int INF = 1e+8;

//??\\???
#define MAX_N 50000
#define MAX_M 20
int N;
int M;
int C[MAX_M];

// dp ????????????
int dp[MAX_N + 1];

void print_dp_array(int n) {
  for (int i = 0; i <= n; i++) {
    if (dp[i] == INF) {
      cout << "INF ";
    } else {
      cout << setw(3) << dp[i];
    }
  }
  cout << endl;
}

int main() {
  ios::sync_with_stdio(false);

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

  // ?????????
  fill(dp, dp + MAX_N + 1, INF);
  dp[0] = 0;

  for (int s = 0; s <= N; s++) {

    if (dp[s] != INF) {
      for (int i = 0; i < M; i++) {
        dp[s + C[i]] = min(dp[s + C[i]], dp[s] + 1);
      }
    }
  }

  cout << dp[N] << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

#define INF 1000000

using namespace std;

//??\\???
#define MAX_N 50000 // amount we want to pay
#define MAX_M 20    // ?????????????¨????
int N, M;
int C[MAX_M];
int dp[MAX_N];

// ci ?????§????????????????????????T ?????????
int min_change(int T) {

  if (T == 0) {
    return 0;
  }

  if (T < 0) {
    return INF;
  }

  if (dp[T] < INF) {
    return dp[T];
  }

  int mn = INF;
  for (int i = 0; i < M; i++) {
    mn = min(mn, min_change(T - C[i]));
  }

  dp[T] = mn + 1;
  return mn + 1;
}

int main() {
  ios::sync_with_stdio(false);
  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    cin >> C[i];
  }
  for (int i = 0; i < N + 1; i++) {
    dp[i] = INF;
  }

  cout << min_change(N) << endl;

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

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

#define MAX_N 1000000

int main() {
  int dp[MAX_N];
  int n, m, c[100];

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

  cin >> n >> m;

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

  for (int i = 1; i <= m; i++) {
    dp[c[i]] = 1;
  }

  for (int i = 1; i <= 20; i++) {
    for (int j = 1; j <= n; j++) {
      if (dp[j] != -1) {
        for (int k = 1; k <= m; k++) {
          if (dp[j + c[k]] == -1 || dp[j + c[k]] > dp[j] + 1) {
            dp[j + c[k]] = dp[j] + 1;
          }
        }
      }
    }
  }

  cout << dp[n] << endl;

  return 0;
}

Error: 0

Fixed submission:

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

#define MAX_N 70

int main() {
  int dp[MAX_N];
  int n, m, c[100];

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

  cin >> n >> m;

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

  for (int i = 1; i <= m; i++) {
    dp[c[i]] = 1;
  }

  for (int i = 1; i <= 20; i++) {
    for (int j = 1; j <= n; j++) {
      if (dp[j] != -1) {
        for (int k = 1; k <= m; k++) {
          if (dp[j + c[k]] == -1 || dp[j + c[k]] > dp[j] + 1) {
            dp[j + c[k]] = dp[j] + 1;
          }
        }
      }
    }
  }

  cout << dp[n] << endl;

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

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define ll long long
#define inf 999999999
#define pa pair<int, int>
using namespace std;

int main() {
  int c[22], n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++)
    cin >> c[i];
  static int dp[22][50010];
  for (int i = 0; i < m + 1; i++) {
    for (int j = 0; j < n + 1; j++)
      dp[i][j] = inf;
    dp[i][0] = 0;
  }

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

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

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define ll long long
#define inf 999999999
#define pa pair<int, int>
using namespace std;

int main() {
  int c[22], n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++)
    cin >> c[i];
  static int dp[22][50010];
  for (int i = 0; i < m + 1; i++) {
    for (int j = 0; j < n + 1; j++)
      dp[i][j] = inf;
    dp[0][0] = 0;
  }

  for (int i = 0; i < m; i++) {
    // cout<<c[i]<<endl;
    for (int j = 0; j < n + 1; j++) {
      if (dp[i][j] != inf) {
        for (int k = 0; k <= n / c[i] + 1; k++) {
          if (j + k * c[i] <= n)
            dp[i + 1][j + k * c[i]] =
                min(dp[i + 1][j + k * c[i]], dp[i][j] + k);
          // cout<<k<<endl;
        }
      }
    }
  }
  for (int i = 0; i < 7; i++) {
    for (int j = 0; j < 20; j++) {
      // cout<<dp[i][j]<<" ";
    }
    // cout<<endl;
  }
  cout << dp[m][n] << endl;

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

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <list>
#include <string>
#include <vector>

using namespace std;

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

  vector<int> a(m);
  vector<int> pri(50001, 1000000000);

  for (int i = 0; i < m; i++) {
    cin >> a[i];
    pri[a[i]] = 1;
    if (n == a[i]) {
      cout << 1 << endl;
      return 0;
    }
  }
  int ans = 1;
  int a_max = *max_element(a.begin(), a.end());
  while (1) {
    for (int i = 0; i < m; i++) {

      for (int j = 1; j <= ans * (a_max) && j <= 50000; j++) {
        if (a[i] + j > 50000)
          break;
        if (pri[j] == ans) {
          // cout << "a[i]:" << a[i] << ", j:" << j << endl;
          if (pri[a[i] + j] > 1 + pri[j]) {
            pri[a[i] + j] = 1 + pri[j];
          }
          if (a[i] + j == n) {
            cout << pri[a[i] + j] << endl;
            return 0;
          }
        }
      }
    }
    ans++;
    // cout << ans << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <list>
#include <string>
#include <vector>

using namespace std;

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

  vector<int> a(m);
  vector<int> pri(50001, 1000000000);

  for (int i = 0; i < m; i++) {
    cin >> a[i];
    pri[a[i]] = 1;
    if (n == a[i]) {
      cout << 1 << endl;
      return 0;
    }
  }
  int ans = 1;
  while (1) {
    for (int i = 0; i < m; i++) {

      for (int j = 1; j <= 50000; j++) {
        if (a[i] + j > 50000)
          break;
        if (pri[j] == ans) {
          // cout << "a[i]:" << a[i] << ", j:" << j << endl;
          if (pri[a[i] + j] > 1 + pri[j]) {
            pri[a[i] + j] = 1 + pri[j];
          }
          if (a[i] + j == n) {
            cout << pri[a[i] + j] << endl;
            return 0;
          }
        }
      }
    }
    ans++;
    // cout << ans << endl;
  }

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

///
// File:  dpl_1_a.cpp
// Author: ymiyamoto
//
// Created on Wed Nov  1 01:41:22 2017
//

#include <cstdint>
#include <iostream>
#include <vector>

using namespace std;
#define MAX_N 50001
static vector<int32_t> coins;
static uint32_t dp[MAX_N];

int32_t main() {
  int32_t n, m;
  cin >> n >> m;
  for (int32_t i = 0; i < m; i++) {
    int32_t c;
    cin >> c;
    coins.push_back(c);
  }

  for (int32_t i = 0; i <= n; i++) {
    dp[i] = UINT32_MAX;
    dp[0] = 0;
    for (int32_t j = 0; j < (int32_t)coins.size(); j++) {
      if (i - coins[j] >= 0) {
        dp[i] = min(dp[i], 1 + dp[i - coins[j]]);
      }
    }
  }

  cout << dp[n] << endl;

  return 0;
}

Error: 0

Fixed submission:

///
// File:  dpl_1_a.cpp
// Author: ymiyamoto
//
// Created on Wed Nov  1 01:41:22 2017
//

#include <cstdint>
#include <iostream>
#include <vector>

using namespace std;
#define MAX_N 10001
static vector<int32_t> coins;
static uint32_t dp[MAX_N];

int32_t main() {
  int32_t n, m;
  cin >> n >> m;
  for (int32_t i = 0; i < m; i++) {
    int32_t c;
    cin >> c;
    coins.push_back(c);
  }

  for (int32_t i = 0; i <= n; i++) {
    dp[i] = UINT32_MAX;
    dp[0] = 0;
    for (int32_t j = 0; j < (int32_t)coins.size(); j++) {
      if (i - coins[j] >= 0) {
        dp[i] = min(dp[i], 1 + dp[i - coins[j]]);
      }
    }
  }

  cout << dp[n] << endl;

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

#include <iostream>
#include <vector>

using namespace std;

#define INF 10000000

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

  vector<int> d(m);
  vector<int> dp(n + 1, INF);

  for (vector<int>::size_type i = 0; i < m; i++) {
    cin >> d[i];
  }

  dp[0] = 0;

  for (unsigned i = 1; i <= n; i++) {
    for (vector<int>::size_type j = 0; j < m; j++) {
      if (i >= d[j]) {
        if (dp[i] >= dp[i - d[j]] + 1) {
          dp[i] = dp[i - d[j]] + 1;
        }
      }
    }
  }

  cout << dp[n] << endl;

  return 0;
}

Error: WA

Fixed submission:

#include <climits>
#include <iostream>
#include <vector>

using namespace std;

#define INF INT_MAX

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

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

  for (int i = 0; i < m; i++) {
    int temp;
    cin >> temp;

    d.push_back(temp);
  }

  dp[0] = 0;

  for (vector<int>::size_type i = 1; i <= n; i++) {
    for (vector<int>::size_type j = 0; j < m; j++) {
      if (i - d[j] >= 0) {
        if (dp[i] < dp[i - d[j]] + 1) {
          dp[i] = dp[i - d[j]] + 1;
        }
      }
    }
  }

  cout << dp[n] << endl;

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

#include <cstring>
#include <iostream>

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

const int INF = 1e9;

using namespace std;

int m, n;
int c[21];

int dp[21][100005];

int dfs(int i, int sum) {
  if (i == m) {
    if (n == sum)
      return 0;
    else
      return INF;
  }
  if (dp[i][sum] != -1) {
    return dp[i][sum];
  }
  int ret;
  ret = dfs(i + 1, sum);
  if (sum + c[i] <= n) {
    ret = min(ret, dfs(i, sum + c[i]) + 1);
  }

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

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

Error: TLE

Fixed submission:

#include <iostream>

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

const int INF = 1e9;

using namespace std;

int m, n;
int c[21];

int dfs(int i, int sum) {
  if (i == m) {
    if (n == sum)
      return 0;
    else
      return INF;
  }
  int ret;
  ret = dfs(i + 1, sum);
  if (sum + c[i] <= n)
    ret = min(ret, dfs(i, sum + c[i]) + 1);

  return ret;
}

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

#include <algorithm>
#include <array>
#include <climits>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

const double pi = 4 * atan(1.0);

int mod = 1000000007;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> Coins(m);
  for (auto &val : Coins)
    cin >> val;

  sort(begin(Coins), end(Coins));

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

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

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

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

  // system("pause");
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <climits>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

const double pi = 4 * atan(1.0);

int mod = 1000000007;

int get_ans(int price, int coin_index, const vector<int> &Coins,
            vector<vector<int>> &dp) {
  if (coin_index > Coins.size() - 1) {
    if (price == 0)
      return 0;
    else
      return INT_MAX;
  }

  if (dp[price][coin_index] >= 0)
    return dp[price][coin_index];

  int ans = INT_MAX;
  for (int i = 0; i < price / Coins[coin_index] + 1; ++i) {
    int tmp = get_ans(price - i * Coins[coin_index], coin_index + 1, Coins, dp);
    if (tmp < INT_MAX) {
      ans = min(ans, i + tmp);
    }
  }
  if (price % Coins[coin_index] == 0) {
    ans = min(ans, price / Coins[coin_index]);
  }

  dp[price][coin_index] = ans;
  return dp[price][coin_index];
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> Coins(m);
  for (auto &val : Coins)
    cin >> val;

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

  cout << get_ans(n, 0, Coins, dp) << endl;

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

#include <stdio.h>
int i, j, a, b, c[20], d[50001], min = 2000000000;
int main(void) {

  scanf("%d %d", &a, &b);
  for (i = 0; i < b; i++) {
    scanf("%d", &c[i]);
  }
  for (j = 0; j <= a; j++) {
    d[j] = min;
  }
  d[0] = 0;
  for (i = 0; i < b; i++) {
    for (j = 0; j <= a; j++) {
      if (d[j] != min && j + c[i] <= a) {
        if (d[j] + 1 < d[j + c[i]]) {
          d[j + c[i]] = d[j] + 1;
        }
      }
    }
  }
  printf("%d\\n", d[a]);
  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>
int i, j, a, b, c[20], d[50001], min = 2000000000;
int main(void) {

  scanf("%d %d", &a, &b);
  for (i = 0; i < b; i++) {
    scanf("%d", &c[i]);
  }
  for (j = 0; j <= a; j++) {
    d[j] = min;
  }
  d[0] = 0;
  for (i = 0; i < b; i++) {
    for (j = 0; j <= a; j++) {
      if (d[j] != min) {
        if (d[j] + 1 < d[j + c[i]] && j + c[i] <= a) {
          d[j + c[i]] = d[j] + 1;
        }
      }
    }
  }
  printf("%d\\n", d[a]);
  return 0;
}
Problem p02314 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>

using namespace std;

int n, m;
const int MAX_N = 50000, MAX_M = 20, INF = 1000000;
int c[MAX_M + 2];
int dp[MAX_M + 2][MAX_N + 2];

void solve() {
  for (int j = 0; j <= n; j++) {
    dp[0][j] = INF;
  }

  for (int i = 0; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      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);
      }
    }
  }
  cout << dp[m][n] << endl;
}

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

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

int n, m;
const int MAX_N = 50000, MAX_M = 20, INF = 1000000;
int c[MAX_M + 1];
int dp[MAX_M + 1][MAX_N + 2];

void solve() {
  for (int j = 0; j <= n; j++) {
    dp[0][j] = INF;
  }

  for (int i = 0; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      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);
      }
    }
  }
  cout << dp[m][n] << endl;
}

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