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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define shosu(n) setprecision(n)
#define INF 999999999
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
typedef vector<vb> vvb;
typedef vector<vpii> vvpii;

int main() {
  int n, m;
  cin >> n >> m;
  vi v(m), dp(70000, INF);
  rep(i, m) {
    cin >> v[i];
    dp[v[i]] = 1;
  }
  dp[0] = 0;
  rep(i, 50008) {
    rep(j, m) { dp[i + v[j]] = min(dp[i] + 1, dp[i + v[j]]); }
  }
  cout << dp[n] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define shosu(n) setprecision(n)
#define INF 999999999
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
typedef vector<vb> vvb;
typedef vector<vpii> vvpii;

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

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

const int INF = 1e9 + 7;

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

int solve(int n) {
  if (dp[n] != INF)
    return dp[n];
  int res = INF;
  if (n == 0)
    res = 0;
  for (int i = 0; i < m; ++i) {
    if (n - c[i] >= 0)
      res = min(res, solve(n - c[i]) + 1);
  }
  return dp[n] = res;
}

int main() {

  cin >> n >> m;

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

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

  cout << solve(n) << endl;
}

Error: TLE

Fixed submission:

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

const int INF = 1e9 + 7;

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

int solve(int n) {
  int res = INF;
  if (n == 0)
    res = 0;
  for (int i = 0; i < m; ++i) {
    if (n - c[i] >= 0)
      res = min(res, solve(n - c[i]) + 1);
  }
  return res;
}

int main() {

  cin >> n >> m;

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

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

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

#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
#define N_MAX 50000
#define M_MAX 20
#define NIL 1000000000

int DP[N_MAX + 1][M_MAX + 1];
int C[M_MAX + 1];

int main(void) {
  int n, m;
  scanf("%d %d", &n, &m);

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

  for (int i = 0; i <= n; i++) {
    for (int k = 0; k <= m; k++) {
      DP[i][k] = NIL;
    }
  }
  for (int k = 0; k <= m; k++) {
    DP[0][k] = 0;
  }

  for (int k = 1; k <= m; k++) {
    for (int i = 0; i <= n; i++) {
      if (i - C[k] < 0) {
        DP[i][k] = DP[i][k - 1];
      } else {
        DP[i][k] = min(DP[i][k - 1], DP[i - C[k]][k] + 1);
      }
      // printf("DP[%d][%d]: %d\\n", i, k, DP[i][k]);
    }
  }

  printf("%d\\n", DP[n][m]);
}


Error: TLE

Fixed submission:

#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
#define N_MAX 50000
#define M_MAX 20
#define NIL 1000000000

int DP[N_MAX + 1][M_MAX + 1];
int C[M_MAX + 1];

int main(void) {
  int n, m;
  scanf("%d %d", &n, &m);

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

  for (int i = 0; i <= n; i++) {
    for (int k = 0; k <= m; k++) {
      DP[i][k] = NIL;
    }
  }
  for (int k = 0; k <= m; k++) {
    DP[0][k] = 0;
  }

  for (int k = 1; k <= m; k++) {
    for (int i = 0; i <= n; i++) {
      int coin_n = 0;
      while (i - C[k] * coin_n >= 0) {
        DP[i][k] = min(DP[i][k], DP[i - C[k] * coin_n][k - 1] + coin_n);
        if (DP[i][k] < coin_n) {
          break;
        }
        coin_n++;
      }
      // printf("DP[%d][%d]: %d\\n", i, k, DP[i][k]);
    }
  }

  printf("%d\\n", DP[n][m]);
}
Problem p02314 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

static const int INF = 1e9 + 7;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> c(m);
  c.resize(m);
  for (auto &&i : c) {
    cin >> i;
  }
  sort(begin(c), end(c));
  vector<int> 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] << "\\n";
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, s, n) for (int i = s; i < (int)n; i++)
#define per(i, n) for (int i = n; i >= 0; i--)
#define ROF(i, s, n) for (int i = s; i >= (int)n; i--)
#define FORIT(i, A) for (auto i : A)
#define PRINT(x) cout << (x) << "\\n"
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define MP make_pair
#define EACH(i, n)                                                             \\
  for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i)
#define SZ(a) int((a).size())
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define CLR(a) memset((a), 0, sizeof(a))
#define NCLR(a) memset((a), -1, sizeof(a))
#define dump(x) cout << #x << " = " << (x) << "\\n";
#define debug(x)                                                               \\
  cout << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << "\\n";
#define sq(n) (n) * (n)
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef priority_queue<int> maxpq;
typedef priority_queue<int, vector<int>, greater<int>> minpq;
static const double EPS = 1e-10;
static const double PI = acos(-1.0);
static const int mod = 1000000007;
static const int INF = 1 << 30;
static const LL LL_INF = 1152921504606846976;
static const int dx[] = {-1, 0, 1, 0, 1, -1, 1, -1};
static const int dy[] = {0, -1, 0, 1, 1, 1, -1, -1};

int n, m, ans = 0;
VI money;
int dp[20][50001];
int solve(int now, int many) {
  if (many == 0) {
    return 0;
  }
  if (now == m) {
    return INF;
  }
  if (dp[now][many] >= 0) {
    return dp[now][many];
  }
  int res = INF;
  for (int i = 0; money[now] * i <= many; i++) {
    res = min(solve(now + 1, many - money[now] * i) + i, res);
  }
  return dp[now][many] = res;
}

int main() {
  scanf("%d %d", &n, &m);
  money.resize(m);
  for (int i = 0; i < m; i++) {
    scanf("%d", &money[i]);
  }
  sort(money.begin(), money.end());
  NCLR(dp);
  ans = solve(0, n);
  printf("%d\\n", ans);
  return 0;
}
Problem p02314 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int i, j, n, m;
  scanf("%d %d", &n, &m);
  int dp[m + 1][n + 1];
  fill(dp[0], dp[m], 1e9);
  int l[m + 1];
  for (i = 1; i < m + 1; i++) {
    scanf("%d", &l[i]);
  }
  sort(l, l + m + 1);
  for (i = 0; i < n + 1; i++) {
    dp[0][i] = 1e9;
  }
  for (i = 0; i < m + 1; i++) {
    dp[i][0] = 0;
  }
  for (i = 1; i < m + 1; i++) {
    for (j = 1; j < n + 1; j++) {
      if (j - l[i] >= 0) {
        dp[i][j] = min(dp[i - 1][j], dp[i][j - l[i]] + 1);
      } else {
        dp[i][j] = dp[i - 1][j];
      }
    }
  }
  int res = 1e9;
  for (i = 1; i < m + 1; i++) {
    res = min(res, dp[i][n]);
  }
  printf("%d\\n", res);
  return 0;
}


Error: 0

Fixed submission:

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

int main() {
  int i, j, k, n, m, o;
  cin >> n >> m;
  int c[m];
  int dp[m + 1][n + 1];
  fill(dp[0], dp[m], n + 1);
  for (i = 0; i < m; i++) {
    cin >> c[i];
    dp[1][c[i]] = 1;
  }
  for (i = 2; i < m + 1; i++) {
    for (j = 1; j < n + 1; j++) {
      if (j - c[i] > 0) {
        dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - c[i]] + 1);
      } else {
        dp[i][j] = dp[i - 1][j];
      }
    }
  }
  o = 1e9;
  for (i = 1; i < m + 1; i++) {
    o = min(o, dp[i][n]);
  }
  cout << o << endl;
  return 0;
}
Problem p02314 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
// #define int long long
#define range(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) range(i, 0, a)
#define all(a) (a).begin(), (a).end()
using namespace std;
const int MOD = 1e9 + 7, INF = 1e9;
using vi = vector<int>;
using vvi = vector<vi>;
// g++ -std==c++14

signed main() {
  int n, m;
  cin >> n >> m;
  vi c(m);
  rep(i, m) cin >> c[i];

  vi va(n + 1, INF);
  va[0] = 0;

  rep(i, m) {
    rep(j, n + 1) {
      if (va[j] == INF || j + c[i] > n)
        continue;
      va[j + c[i]] = min(va[j + c[i]], va[j] + 1);
    }
  }

  cout << va[n] << endl;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
// #define int long long
#define range(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) range(i, 0, a)
#define all(a) (a).begin(), (a).end()
using namespace std;
const int MOD = 1e9 + 7, INF = 1e9;
using vi = vector<int>;
using vvi = vector<vi>;
// g++ -std==c++14

signed main() {
  int n, m;
  cin >> n >> m;
  vi c(m);
  rep(i, m) cin >> c[i];

  vi va(n + 1, INF);
  va[0] = 0;

  while (1) {
    for (int i = n; i >= 0; i--) {
      if (va[i] == INF)
        continue;
      bool flag = false;
      rep(j, m) {
        if (i + c[j] > n)
          continue;
        va[i + c[j]] = min(va[i + c[j]], va[i] + 1);
        if (i + c[j] == n)
          flag = true;
      }
      if (flag) {
        cout << va[n] << endl;
        return 0;
      }
    }
  }
}
Problem p02314 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>

#define NMAX 50000
#define MMAX 20
#define INF 100000000

using namespace std;

int main(void) {
  std::ios_base::sync_with_stdio();

  int n, m;
  int co[MMAX];
  int dp[NMAX + 1];

  cin >> n >> m;

  for (int i = 0; i < m; i++)
    cin >> co[i];
  for (int j = 0; j <= n; j++)
    dp[j] = INF;
  dp[0] = 0;

  for (int i = 0; i <= n; i++) {
    if (dp[i] != INF) {
      for (int j = 0; j < m; j++) {
        if (i + co[j] <= n) {
          dp[i + co[j]] = min(dp[i] + 1, dp[i + co[j]]);
        }
      }
    }
  }

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

#define NMAX 50000
#define MMAX 20
#define INF 1000000000

int n, m;
int co[MMAX];
int dp[NMAX];
using namespace std;

int dfs(int sum, int mai) {
  if (sum == n)
    return mai;
  if (sum > n)
    return INF;
  if (dp[sum] != -1 && dp[sum] <= mai)
    return dp[sum];
  int a = INF;
  for (int i = 0; i < m; i++) {
    a = min(a, dfs(sum + co[i], mai + 1));
  }
  dp[sum] = a;
  return a;
}

int main(void) {

  std::ios_base::sync_with_stdio();

  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> co[i];
  }
  for (int i = 0; i < NMAX; i++)
    dp[i] = -1;
  cout << dfs(0, 0) << endl;

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

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, s, e) for ((i) = (s); (i) < (int)(e); (i)++)
#define REP(i, e) FOR(i, 0, e)

#define all(o) (o).begin(), (o).end()
#define psb(x) push_back(x)
#define mp(x, y) make_pair((x), (y))

typedef long long ll;
typedef pair<int, int> PII;

const double EPS = 1e-10;

const int INF = 1000 * 1000 * 1000;
const int N = 500000;
const int M = 20;
int n, m;
int dp[M + 1][N + 1];
int c[N + 1];

int main() {
  int i, j, k;
  scanf("%d%d ", &n, &m);
  for (i = 0; i < m; i++)
    scanf("%d ", c + i + 1);

  for (i = 0; i <= m; i++)
    for (j = 0; j <= n; j++)
      dp[i][j] = INF;

  for (j = 0; j <= n; j++)
    if (!(j % c[1]))
      dp[1][j] = j / c[1];

  for (i = 2; i <= m; i++)
    for (j = 0; j <= n; j++) {
      if (j - c[i] < 0)
        dp[i][j] = dp[i - 1][j];
      else
        dp[i][j] = min(dp[i - 1][j], dp[i][j - c[i]] + 1);
      //      for (k=0; j-c[i]*k>=0; k++)
      //        dp[i][j] = min(dp[i][j], dp[i-1][j-c[i]*k]+k);
    }
  printf("%d\\n", dp[m][n]);

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, s, e) for ((i) = (s); (i) < (int)(e); (i)++)
#define REP(i, e) FOR(i, 0, e)

#define all(o) (o).begin(), (o).end()
#define psb(x) push_back(x)
#define mp(x, y) make_pair((x), (y))

typedef long long ll;
typedef pair<int, int> PII;

const double EPS = 1e-10;

const int INF = 1000 * 1000 * 1000;
const int N = 500000;
const int M = 20;
int n, m;
int dp[M + 1][N + 1];
int c[N + 1];

int main() {
  int i, j, k;
  scanf("%d%d ", &n, &m);
  for (i = 0; i < m; i++)
    scanf("%d ", c + i + 1);

  for (i = 0; i <= m; i++)
    for (j = 0; j <= n; j++)
      dp[i][j] = INF;

  for (j = 0; j <= n; j++)
    if (!(j % c[1]))
      dp[1][j] = j / c[1];

  for (i = 2; i <= m; i++)
    for (j = 0; j <= n; j++)
      for (k = 0; j - c[i] * k >= 0; k++)
        dp[i][j] = min(dp[i][j], dp[i - 1][j - c[i] * k] + k);
  printf("%d\\n", dp[m][n]);

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

#include <bits/stdc++.h>

#define F first
#define S second
#define mp make_pair
// everything go according to my plan
#define pb push_back
#define sz(a) (int)(a.size())
#define vec vector
// shimkenttin kyzdary, dzyn, dzyn, dzyn...
#define y1 Y_U_NO_y1
#define left Y_U_NO_left
#define right Y_U_NO_right

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;

const int Mod = (int)1e9 + 7;
const int MX = 1073741822;
const ll MXLL = 4e18;
const int Sz = 1110111;
// a pinch of soul
inline void Read_rap() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
inline void randomizer3000() {
  unsigned int seed;
  asm("rdtsc" : "=A"(seed));
  srand(seed);
}
void files(string problem) {
  if (fopen((problem + ".in").c_str(), "r")) {
    freopen((problem + ".in").c_str(), "r", stdin);
    freopen((problem + ".out").c_str(), "w", stdout);
  }
}
void localInput(bool local, string in = "s") {
  if (!local)
    return;

  if (fopen(in.c_str(), "r"))
    freopen(in.c_str(), "r", stdin);
  else
    cerr << "Input file not found" << endl;
}
const int N = 5e4 + 1;

int dp[N];

int n, m;

int main() {
  localInput(false);
  Read_rap();
  cin >> n >> m;
  fill(dp + 1, dp + n + 1, MX);

  for (int i = 1; i <= m; i++) {
    int x;
    cin >> x;
    for (int j = 0; j + x <= n; j++)
      dp[j + x] = min(dp[j + x], dp[j] + 1);
  }
  cout << dp[n];

  cout << endl;

  return 0;
}

// Coded by Z..


Error: 0
Input file not found


Fixed submission:

#include <bits/stdc++.h>

#define F first
#define S second
#define mp make_pair
// everything go according to my plan
#define pb push_back
#define sz(a) (int)(a.size())
#define vec vector
// shimkenttin kyzdary, dzyn, dzyn, dzyn...
#define y1 Y_U_NO_y1
#define left Y_U_NO_left
#define right Y_U_NO_right

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;

const int Mod = (int)1e9 + 7;
const int MX = 1073741822;
const ll MXLL = 4e18;
const int Sz = 1110111;
// a pinch of soul
inline void Read_rap() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
inline void randomizer3000() {
  unsigned int seed;
  asm("rdtsc" : "=A"(seed));
  srand(seed);
}
void files(string problem) {
  if (fopen((problem + ".in").c_str(), "r")) {
    freopen((problem + ".in").c_str(), "r", stdin);
    freopen((problem + ".out").c_str(), "w", stdout);
  }
}
void localInput(string in = "s") {
  if (fopen(in.c_str(), "r"))
    freopen(in.c_str(), "r", stdin);
  else
    cerr << "Input file not found" << endl;
}
const int N = 5e4 + 1;

int dp[N];

int n, m;

int main() {
  localInput();
  Read_rap();
  cin >> n >> m;
  fill(dp + 1, dp + n + 1, MX);

  for (int i = 1; i <= m; i++) {
    int x;
    cin >> x;
    for (int j = 0; j + x <= n; j++)
      dp[j + x] = min(dp[j + x], dp[j] + 1);
  }
  cout << dp[n];

  cout << endl;

  return 0;
}

// Coded by Z..
Problem p02314 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define F first
#define S second
#define mp make_pair
// everything go according to my plan
#define pb push_back
#define sz(a) (int)(a.size())
#define vec vector
// shimkenttin kyzdary, dzyn, dzyn, dzyn...
#define y1 Y_U_NO_y1
#define left Y_U_NO_left
#define right Y_U_NO_right

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;

const int Mod = (int)1e9 + 7;
const int MX = 1073741822;
const ll MXLL = 4e18;
const int Sz = 1110111;
// a pinch of soul
inline void Read_rap() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
inline void randomizer3000() {
  unsigned int seed;
  asm("rdtsc" : "=A"(seed));
  srand(seed);
}
void files(string problem) {
  if (fopen((problem + ".in").c_str(), "r")) {
    freopen((problem + ".in").c_str(), "r", stdin);
    freopen((problem + ".out").c_str(), "w", stdout);
  }
}
void localInput(string in = "s") {
  if (fopen(in.c_str(), "r"))
    freopen(in.c_str(), "r", stdin);
  else
    cerr << "Input file not found" << endl;
}
const int N = 5e4 + 1;

int dp[N];

int n, m;

int main() {
  cin >> n >> m;
  fill(dp + 1, dp + n + 1, MX);

  for (int i = 1; i <= m; i++) {
    int x;
    cin >> x;
    for (int j = 0; j + x <= n; j++)
      dp[j + x] = min(dp[j + x], dp[j] + 1);
  }
  cout << dp[n];

  cout << endl;

  return 0;
}

// Coded by Z..


Error: 0
Input file not found


Fixed submission:

#include <bits/stdc++.h>

#define F first
#define S second
#define mp make_pair
// everything go according to my plan
#define pb push_back
#define sz(a) (int)(a.size())
#define vec vector
// shimkenttin kyzdary, dzyn, dzyn, dzyn...
#define y1 Y_U_NO_y1
#define left Y_U_NO_left
#define right Y_U_NO_right

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;

const int Mod = (int)1e9 + 7;
const int MX = 1073741822;
const ll MXLL = 4e18;
const int Sz = 1110111;
// a pinch of soul
inline void Read_rap() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
inline void randomizer3000() {
  unsigned int seed;
  asm("rdtsc" : "=A"(seed));
  srand(seed);
}
void files(string problem) {
  if (fopen((problem + ".in").c_str(), "r")) {
    freopen((problem + ".in").c_str(), "r", stdin);
    freopen((problem + ".out").c_str(), "w", stdout);
  }
}
void localInput(string in = "s") {
  if (fopen(in.c_str(), "r"))
    freopen(in.c_str(), "r", stdin);
  else
    cerr << "Input file not found" << endl;
}
const int N = 5e4 + 1;

int dp[N];

int n, m;

int main() {
  localInput();
  Read_rap();
  cin >> n >> m;
  fill(dp + 1, dp + n + 1, MX);

  for (int i = 1; i <= m; i++) {
    int x;
    cin >> x;
    for (int j = 0; j + x <= n; j++)
      dp[j + x] = min(dp[j + x], dp[j] + 1);
  }
  cout << dp[n];

  cout << endl;

  return 0;
}

// Coded by Z..
Problem p02314 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <iterator>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
#define FOR(I, F, N) for (int I = F; I < (int)(N); I++)
#define rep(i, n) FOR(i, 0, n)
#define FIN(V) cout << V << endl
#define pb push_back
#define INF (1 << 30)
typedef pair<int, int> P;
typedef long long ll;
typedef priority_queue<int> pq;
int StrToInt(string);
string IntToStr(int);
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

int main(void) {
  int n, m;
  cin >> n >> m;
  vector<int> v;
  rep(i, m) {
    int a;
    cin >> a;
    v.pb(a);
  }
  int dp[n + 1];
  fill_n(dp, n + 1, INF);

  dp[0] = 0;
  rep(i, n + 1) {
    rep(j, v.size()) {
      if (i - v[j] >= 0)
        dp[i] = min(dp[i - v[j]] + 1, dp[i]);
    }
  }
  FIN(dp[n]);

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <iterator>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
#define FOR(I, F, N) for (int I = F; I < (int)(N); I++)
#define rep(i, n) FOR(i, 0, n)
#define FIN(V) cout << V << endl
#define pb push_back
#define INF (1 << 30)
typedef pair<int, int> P;
typedef long long ll;
typedef priority_queue<int> pq;
int StrToInt(string);
string IntToStr(int);
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

int main(void) {
  int n, m;
  cin >> n >> m;
  vector<int> v;
  rep(i, m) {
    int a;
    cin >> a;
    v.pb(a);
  }
  int dp[10001];
  fill_n(dp, 10001, INF);

  dp[0] = 0;
  rep(i, n + 1) {
    rep(j, v.size()) {
      if (i - v[j] >= 0)
        dp[i] = min(dp[i - v[j]] + 1, dp[i]);
    }
  }
  FIN(dp[n]);

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

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

using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> cs(m);
  for (int i = 0; i < m; ++i) {
    cin >> cs[i];
  }
  sort(cs.begin(), cs.end());
  vector<vector<int>> cache(n + 1, vector<int>(m));
  for (int i = 0; i < m; ++i)
    cache[0][i] = 0;
  for (int i = 0; i <= n; ++i)
    cache[i][0] = i;
  for (int ni = 1; ni <= n; ++ni) {
    for (int mi = 1; mi < m; ++mi) {
      int t;
      if (cs[mi] > ni)
        t = min(cache[ni][mi - 1], cache[ni - 1][mi] + 1);
      else {
        t = min(cache[ni][mi - 1], cache[ni - 1][mi] + 1);
        t = min(t, cache[ni - cs[mi]][mi] + 1);
      }
      cache[ni][mi] = t;
    }
  }
  cout << cache[n][m - 1] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> cs(m - 1);
  int mi = 0;
  for (int i = 0; i < m; ++i) {
    cin >> cs[mi];
    if (cs[mi] == 1)
      continue;
    ++mi;
  }
  vector<int> cache(n + 1);
  cache[0] = 0;
  cache[1] = 1;
  for (int ni = 2; ni <= n; ++ni) {
    int t = ni;
    for (auto &c : cs) {
      int cnt = 1;
      while (c * cnt <= ni) {
        int rem = ni - c * cnt;
        t = min(t, cnt + cache[rem]);
        ++cnt;
      }
    }
    cache[ni] = t;
  }
  cout << cache[n] << endl;
  return 0;
}
Problem p02314 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define req(i, j) for (int i = 0; i < j; i++)

int n, m;

int coin[20];

int dp[50001][21];

int rec(int i, int j) {

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

  if (j == m) {
    return dp[i][j] = 50001;
  }

  if (i == 0) {
    return dp[i][j] = 0;
  }

  if (coin[j] > i) {
    return dp[i][j] = rec(i, j + 1);
  }

  int rec1 = rec(i, j + 1);
  int rec2 = rec(i - coin[j], j) + 1;

  return dp[i][j] = min(rec1, rec2);
}

int main() {

  cin >> n >> m;

  req(i, m) { cin >> coin[i]; }

  req(i, n + 1) {
    req(j, m + 1) { dp[i][j] = -1; }
  }

  cout << rec(n, 0) << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

#define req(i, j) for (int i = 0; i < j; i++)

int n, m;

int coin[20];

int dp[50001][21];

int rec(int i, int j) {

  if (j == m) {
    return dp[i][j] = 50001;
  }

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

  if (coin[j] > i) {
    return rec(i, j + 1);
  }

  int rec1 = rec(i, j + 1);
  int rec2 = rec(i - coin[j], j) + 1;

  return min(rec1, rec2);
}

int main() {

  cin >> n >> m;

  req(i, m) { cin >> coin[i]; }

  cout << rec(n, 0) << endl;

  return 0;
}