prithivMLmods/Coder-Stat huggingface dataset:
Problem p02787 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>

using namespace std;

int h, n, res = 1e9;
vector<vector<int>> f;

int main() {
  cin >> h >> n;
  f.resize(2 * h + 1, vector<int>(n + 1, 1e9));
  f[0][0] = 0;
  for (int i = 1, a, b; i <= n; ++i) {
    cin >> a >> b;
    for (int j = 0; j <= 2e4; ++j) {
      f[j][i] = f[j][i - 1];
      if (j >= a)
        f[j][i] = min(f[j][i], f[j - a][i] + b);
      if (j >= h)
        res = min(res, f[j][i]);
    }
  }
  cout << res;
}

Fixed submission:
#include <iostream>
#include <vector>

using namespace std;

int h, n, res = 1e9;
vector<vector<int>> f;

int main() {
  cin >> h >> n;
  f.resize(2e4 + 1, vector<int>(n + 1, 1e9));
  f[0][0] = 0;
  for (int i = 1, a, b; i <= n; ++i) {
    cin >> a >> b;
    for (int j = 0; j <= 2e4; ++j) {
      f[j][i] = f[j][i - 1];
      if (j >= a)
        f[j][i] = min(f[j][i], f[j - a][i] + b);
      if (j >= h)
        res = min(res, f[j][i]);
    }
  }
  cout << res;
}
Problem p02787 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep3(i, s, n, a) for (int i = (s); i < (int)(n); i += a)
#define rep2(i, s, n) rep3(i, s, n, 1)
#define rep(i, n) rep2(i, 0, n)

using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int h, n;
  cin >> h >> n;
  vector<int> a, b;
  rep(i, n) {
    int A, B;
    cin >> A >> B;
    a.push_back(A);
    b.push_back(B);
  }

  vector<int> dp(h, 0);
  dp[0] = 0;

  rep2(i, 1, h + 1) {
    int mincos = INT32_MAX;
    rep(j, n) {
      if (i <= a[j]) {
        mincos = min(mincos, b[j]);
      } else {
        mincos = min(mincos, dp[i - a[j]] + b[j]);
      }
    }
    dp[i] = mincos;
  }

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

Fixed submission:
#include <bits/stdc++.h>
#define rep3(i, s, n, a) for (int i = (s); i < (int)(n); i += a)
#define rep2(i, s, n) rep3(i, s, n, 1)
#define rep(i, n) rep2(i, 0, n)

using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int h, n;
  cin >> h >> n;
  vector<int> a, b;
  rep(i, n) {
    int A, B;
    cin >> A >> B;
    a.push_back(A);
    b.push_back(B);
  }

  vector<int> dp(h + 1, 0);
  dp[0] = 0;

  rep2(i, 1, h + 1) {
    int mincos = INT32_MAX;
    rep(j, n) {
      if (i <= a[j]) {
        mincos = min(mincos, b[j]);
      } else {
        mincos = min(mincos, dp[i - a[j]] + b[j]);
      }
    }
    dp[i] = mincos;
  }

  cout << dp[h] << endl;
  return 0;
}
Problem p02787 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main(int argc, char const *argv[]) {
  int h, n;
  cin >> h >> n;
  int a[n], b[n];
  for (int i = 0; i < n; ++i) {
    cin >> a[i] >> b[i];
  }
  int dp[h + 5];
  for (int i = 0; i < h + 5; ++i) {
    dp[i] = 200000000;
  }
  dp[0] = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= a[i]; ++j) {
      dp[j] = min(dp[j], b[i]);
    }
  }
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < n; ++j) {
      if (i + a[j] <= h) {
        dp[i + a[j]] = min(dp[i + a[j]], dp[i] + b[j]);
      }
    }
  }
  cout << dp[h] << endl;
  return 0;
}


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

int main(int argc, char const *argv[]) {
  int h, n;
  cin >> h >> n;
  int a[n], b[n];
  for (int i = 0; i < n; ++i) {
    cin >> a[i] >> b[i];
  }
  int dp[100005];
  for (int i = 0; i < h + 5; ++i) {
    dp[i] = 200000000;
  }
  dp[0] = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= a[i]; ++j) {
      dp[j] = min(dp[j], b[i]);
    }
  }
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < n; ++j) {
      if (i + a[j] <= h) {
        dp[i + a[j]] = min(dp[i + a[j]], dp[i] + b[j]);
      }
    }
  }
  cout << dp[h] << endl;
  return 0;
}
Problem p02787 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define INF 100000000

int main() {
  int H, N;
  cin >> H >> N;
  vector<pair<int, int>> magic(N);
  int damage, point;
  for (int i = 0; i < N; i++) {
    cin >> damage >> point;
    magic[i] = make_pair(damage, point);
  }
  vector<vector<int>> dp(N + 1, vector<int>(2001, INF));
  dp[0][0] = 0;
  for (int i = 0; i < N; i++) {
    for (int h = 0; h < 20001; h++) {
      if (h >= magic[i].first) {
        dp[i + 1][h] =
            min(dp[i][h], dp[i + 1][h - magic[i].first] + magic[i].second);
      } else {
        dp[i + 1][h] = dp[i][h];
      }
    }
  }
  int ans = INF;
  for (int i = H; i < 20001; i++)
    ans = min(ans, dp[N][i]);
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define INF 100000000

int main() {
  int H, N;
  cin >> H >> N;
  vector<pair<int, int>> magic(N);
  int damage, point;
  for (int i = 0; i < N; i++) {
    cin >> damage >> point;
    magic[i] = make_pair(damage, point);
  }
  vector<vector<int>> dp(N + 1, vector<int>(20001, INF));
  dp[0][0] = 0;
  for (int i = 0; i < N; i++) {
    for (int h = 0; h < 20001; h++) {
      if (h >= magic[i].first) {
        dp[i + 1][h] =
            min(dp[i][h], dp[i + 1][h - magic[i].first] + magic[i].second);
      } else {
        dp[i + 1][h] = dp[i][h];
      }
    }
  }
  int ans = INF;
  for (int i = H; i < 20001; i++)
    ans = min(ans, dp[N][i]);
  cout << ans << endl;
  return 0;
}
Problem p02787 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <vector>

using namespace std;

#define PB push_back
#define MP make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
int dp[10010];
int main() {
  int h, n, a[110], b[110];
  cin >> h >> n;
  REP(i, n) cin >> a[i] >> b[i];

  REP(i, h + 1) dp[i] = 1e9;
  dp[0] = 0;

  REP(i, n) {
    // REP(j,h+1)cout<<dp[j]<<" ";cout<<endl;
    REP(j, h) {
      int to = min(h, j + a[i]);
      dp[to] = min(dp[to], dp[j] + b[i]);
    }
  }
  // REP(i,h+1)cout<<dp[i]<<" ";cout<<endl;
  cout << dp[h] << endl;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <vector>

using namespace std;

#define PB push_back
#define MP make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
int dp[10010];
int main() {
  int h, n, a[1010], b[1010];
  cin >> h >> n;
  REP(i, n) cin >> a[i] >> b[i];

  REP(i, h + 1) dp[i] = 1e9;
  dp[0] = 0;

  REP(i, n) {
    // REP(j,h+1)cout<<dp[j]<<" ";cout<<endl;
    REP(j, h) {
      int to = min(h, j + a[i]);
      dp[to] = min(dp[to], dp[j] + b[i]);
    }
  }
  // REP(i,h+1)cout<<dp[i]<<" ";cout<<endl;
  cout << dp[h] << endl;
}
Problem p02787 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int h, n, ans, i, j;
  double cur;
  cin >> h >> n;
  vector<pair<int, int>> a(n);
  vector<int> dp(h + 1, 1000000000);

  for (i = 0; i < n; i++) {
    cin >> a[i].first >> a[i].second;
  }

  dp[0] = 0;
  for (i = 0; i < n; i++) {
    for (j = 1; j <= a[i].first; j++) {
      dp[j] = min(dp[j], a[i].second);
    }
    for (j = a[i].first + 1; j <= h; j++) {
      dp[j] = min(dp[j], dp[j - a[i].first] + a[i].second);
    }
  }
  cout << dp[h] << endl;
}

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

int main() {
  int h, n, ans, i, j;
  double cur;
  cin >> h >> n;
  vector<pair<int, int>> a(n);
  vector<int> dp(h + 1, 1000000000);

  for (i = 0; i < n; i++) {
    cin >> a[i].first >> a[i].second;
    a[i].first = min(a[i].first, h);
  }

  dp[0] = 0;
  for (i = 0; i < n; i++) {
    for (j = 1; j <= a[i].first; j++) {
      dp[j] = min(dp[j], a[i].second);
    }
    for (j = a[i].first + 1; j <= h; j++) {
      dp[j] = min(dp[j], dp[j - a[i].first] + a[i].second);
    }
  }
  cout << dp[h] << endl;
}
Problem p02787 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define f first
#define s second

typedef long long ll;
typedef pair<ll, ll> pi;

const int MAXN = 1005, INF = 1e9 + 7;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll h, n;
  cin >> h >> n;
  vector<pi> spells(n);
  for (int i = 0; i < n; ++i)
    cin >> spells[i].f >> spells[i].s;
  vector<ll> dp(MAXN, INF);
  dp[0] = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= h; ++j) {
      if (j >= spells[i].f) {
        dp[j] = min(dp[j], dp[j - spells[i].f] + spells[i].s);
      } else {
        dp[j] = min(dp[j], spells[i].s);
      }
    }
  }
  cout << dp[h];
  // for (int i = 0; i <= 10; ++i) cout << dp[i] << ' ';
  return 0;
}


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

#define f first
#define s second

typedef long long ll;
typedef pair<ll, ll> pi;

const int MAXN = 50000005, INF = 1e9 + 7;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll h, n;
  cin >> h >> n;
  vector<pi> spells(n);
  for (int i = 0; i < n; ++i)
    cin >> spells[i].f >> spells[i].s;
  vector<ll> dp(MAXN, INF);
  dp[0] = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= h; ++j) {
      if (j >= spells[i].f) {
        dp[j] = min(dp[j], dp[j - spells[i].f] + spells[i].s);
      } else {
        dp[j] = min(dp[j], spells[i].s);
      }
    }
  }
  cout << dp[h];
  // for (int i = 0; i <= 10; ++i) cout << dp[i] << ' ';
  return 0;
}
Problem p02787 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <ratio>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

using ll = long long;
using ull = unsigned long long;
template <class T> using v = vector<T>;
template <class T, class U> using p = pair<T, U>;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define REP1(i, n) for (int i = 1; i <= n; ++i)

template <class T> T read() {
  T x{};
  cin >> x;
  return x;
}
template <class T> v<T> read(int N) {
  v<T> x(N);
  REP(i, N) cin >> x[i];
  return x;
}

template <class T> void write(const T &x) { cout << x << endl; }
void write(double x) { printf("%.15f\n", x); }

const ull MOD = 1000'000'007;

const ll NMAX = 1000;
const ll HMAX = 10000;
ll dp[NMAX + 10][HMAX + 10] = {};

ll N = 0;
ll H = 0;
ll A[NMAX] = {};
ll B[NMAX] = {};
ll COST[HMAX + 10] = {};

ll solve(ll h) {
  if (h <= 0) {
    return 0;
  }
  if (COST[h] > 0) {
    return COST[h];
  }
  ll cost = 1e15;
  for (ll i = 0; i < N; ++i) {
    for (ll j = 1; j <= A[i]; ++j) {
      cost = min(cost, B[i] + solve(h - j));
    }
  }
  COST[h] = cost;
  return cost;
}

int main() {
  cin >> H >> N;
  REP(i, N) { cin >> A[i] >> B[i]; }
  write(solve(H));
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <ratio>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

using ll = long long;
using ull = unsigned long long;
template <class T> using v = vector<T>;
template <class T, class U> using p = pair<T, U>;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define REP1(i, n) for (int i = 1; i <= n; ++i)

template <class T> T read() {
  T x{};
  cin >> x;
  return x;
}
template <class T> v<T> read(int N) {
  v<T> x(N);
  REP(i, N) cin >> x[i];
  return x;
}

template <class T> void write(const T &x) { cout << x << endl; }
void write(double x) { printf("%.15f\n", x); }

const ull MOD = 1000'000'007;

const ll NMAX = 1000;
const ll HMAX = 10000;
ll dp[NMAX + 10][HMAX + 10] = {};

ll N = 0;
ll H = 0;
ll A[NMAX] = {};
ll B[NMAX] = {};
ll COST[HMAX + 10] = {};

ll solve(ll h) {
  if (h <= 0) {
    return 0;
  }
  if (COST[h] > 0) {
    return COST[h];
  }
  ll cost = 1e15;
  for (ll i = 0; i < N; ++i) {
    cost = min(cost, B[i] + solve(h - A[i]));
  }
  COST[h] = cost;
  return cost;
}

int main() {
  cin >> H >> N;
  REP(i, N) { cin >> A[i] >> B[i]; }
  write(solve(H));
}
Problem p02787 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, N) for (int i = 0; i < (N); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e12;
const int inf = 1e9;
typedef long long ll;
typedef pair<ll, int> P;
typedef set<int> S;
int h, n;
int main() {
  cin >> h >> n;
  vector<int> a(n, 0), b(n, 0);
  rep(i, n) cin >> a[i] >> b[i];
  vector<int> dp(n + 1, inf);
  // 配るDP
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < h + 1; j++) {
      int nj = min(j + a[i], h);
      dp[nj] = min(dp[nj], dp[j] + b[i]);
    }
  }
  /*
  rep(i,n+1){
      rep(j,h+1) cout<<dp[i][j]<<" ";
      cout<<endl;
  }*/
  cout << dp[h] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, N) for (int i = 0; i < (N); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e12;
const int inf = 1e9;
typedef long long ll;
typedef pair<ll, int> P;
typedef set<int> S;
int h, n;
int main() {
  cin >> h >> n;
  vector<int> a(n, 0), b(n, 0);
  rep(i, n) cin >> a[i] >> b[i];
  vector<int> dp(h + 1, inf);
  // 配るDP
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < h + 1; j++) {
      int nj = min(j + a[i], h);
      dp[nj] = min(dp[nj], dp[j] + b[i]);
    }
  }
  /*
  rep(i,n+1){
      rep(j,h+1) cout<<dp[i][j]<<" ";
      cout<<endl;
  }*/
  cout << dp[h] << endl;
  return 0;
}
Problem p02787 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define _GLIBCXX_DEBUG
#define all(v) (v).begin(), (v).end()

using namespace std;

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

const ll LINF = 1LL << 60;
const int INF = 1 << 29;
const int MOD = 1e9 + 7;

int main() {
  int H, N;
  cin >> H >> N;
  vi A(N), B(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i] >> B[i];
  }
  vector<ll> dp(H + 1, LINF);
  dp[H] = 0;
  for (int i = H; i >= 1; i--) {
    for (int j = 0; j < N; j++) {
      if (i + A[j] <= H) {
        dp[i] = min(dp[i], dp[i + A[j]] + B[j]);
      }
    }
  }
  ll res = LINF;
  for (int i = 0; i < N; i++) {
    for (int j = 1; j <= A[i]; j++) {
      res = min(res, dp[j] + B[i]);
    }
  }
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

#define _GLIBCXX_DEBUG
#define all(v) (v).begin(), (v).end()

using namespace std;

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

const ll LINF = 1LL << 60;
const int INF = 1 << 29;
const int MOD = 1e9 + 7;

int main() {
  int H, N;
  cin >> H >> N;
  vi A(N), B(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i] >> B[i];
  }
  vector<ll> dp(H + 1, LINF);
  dp[H] = 0;
  for (int i = H; i >= 1; i--) {
    for (int j = 0; j < N; j++) {
      if (i + A[j] <= H) {
        dp[i] = min(dp[i], dp[i + A[j]] + B[j]);
      }
    }
  }
  ll res = LINF;
  for (int i = 0; i < N; i++) {
    for (int j = 1; j <= min(A[i], H); j++) {
      res = min(res, dp[j] + B[i]);
    }
  }
  cout << res << endl;
  return 0;
}
Problem p02787 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
const int Maxn = 1000;
const int Maxm = 10000;
const int Inf = 0x3f3f3f3f;
int a[Maxn + 5], b[Maxn + 5];
int n, h;
int f[Maxm << 1 | 5];
int mn(int a, int b) { return a < b ? a : b; }
int main() {
  scanf("%d%d", &h, &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &a[i], &b[i]);
  }
  memset(f, 0x3f, sizeof f);
  f[0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= h + Maxm; j++) {
      f[j + a[i]] = mn(f[j + a[i]], f[j] + b[i]);
    }
  }
  int ans = Inf;
  for (int i = h; i <= h + Maxm; i++) {
    ans = mn(ans, f[i]);
  }
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <cstdio>
#include <cstring>
const int Maxn = 1000;
const int Maxm = 10000;
const int Inf = 0x3f3f3f3f;
int a[Maxn + 5], b[Maxn + 5];
int n, h;
int f[Maxm << 1 | 5];
int mn(int a, int b) { return a < b ? a : b; }
int main() {
  scanf("%d%d", &h, &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &a[i], &b[i]);
  }
  memset(f, 0x3f, sizeof f);
  f[0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= h + Maxm - a[i]; j++) {
      f[j + a[i]] = mn(f[j + a[i]], f[j] + b[i]);
    }
  }
  int ans = Inf;
  for (int i = h; i <= h + Maxm; i++) {
    ans = mn(ans, f[i]);
  }
  printf("%d\n", ans);
  return 0;
}
Problem p02787 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>

using namespace std;

int read() {
  int ans = 0, f = 1;
  char c = getchar();
  while (c > '9' || c < '0') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    ans = ans * 10 + c - '0';
    c = getchar();
  }
  return ans * f;
}

const int N = 1e4 + 5;
int h, n, a[N], b[N], dp[N * 2], ans, all;

int main() {
  memset(dp, 0x7f, sizeof(dp));
  ans = 0x7fffffff;
  h = read();
  n = read();
  for (int i = 1; i <= n; ++i) {
    a[i] = read();
    b[i] = read();
    all += a[i];
  }
  dp[0] = 0;
  for (int i = 1; i <= n; ++i)
    for (int j = a[i]; j <= h * 10; ++j) {
      dp[j] = min(dp[j], dp[j - a[i]] + b[i]);
      if (j >= h)
        ans = min(ans, dp[j]);
    }
  printf("%d", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>

using namespace std;

int read() {
  int ans = 0, f = 1;
  char c = getchar();
  while (c > '9' || c < '0') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    ans = ans * 10 + c - '0';
    c = getchar();
  }
  return ans * f;
}

const int N = 1e4 + 5;
int h, n, a[N], b[N], dp[N * 10], ans, all;

int main() {
  memset(dp, 0x7f, sizeof(dp));
  ans = 0x7fffffff;
  h = read();
  n = read();
  for (int i = 1; i <= n; ++i) {
    a[i] = read();
    b[i] = read();
    all += a[i];
  }
  dp[0] = 0;
  for (int i = 1; i <= n; ++i)
    for (int j = a[i]; j <= h * 10; ++j) {
      dp[j] = min(dp[j], dp[j - a[i]] + b[i]);
      if (j >= h)
        ans = min(ans, dp[j]);
    }
  printf("%d", ans);
  return 0;
}
Problem p02787 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>

#define pb emplace_back
#define re return
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using namespace std;
using namespace __gnu_cxx;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1e9 + 7;
const double pi = 3.14159265359;

ll x, n;
pair<int, int> a[1000];
ll dp[10050];
ll solve(ll h) {
  if (!h)
    re 0;
  if (dp[h] != -1)
    re dp[h];
  ll ans = 1e18;
  for (int i = 0; i < n; i++)
    ans = min(ans, a[i].se + solve(h - a[i].fi));
  dp[h] = ans;
  re ans;
}
int main() {
  // freopen("distance.in", "r", stdin);
  // freopen("distance.out", "w", stdout);
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> x >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i].fi >> a[i].se;
  memset(dp, -1, sizeof(dp));
  cout << solve(x);
}
/// ACTUAL CODE STARTS HERE:
//  I'm red in soul
/*
     ▄▀▀▀▄
▄███▀░◐░░░▌
    ▌░░░░░▐
    ▐░░░░░▐
    ▌░░░░░▐▄▄
    ▌░░░░▄▀▒▒▀▀▀▀▄
   ▐░░░░▐▒▒▒▒▒▒▒▒▀▀▄
   ▐░░░░▐▄▒▒▒▒▒▒▒▒▒▒▀▄
    ▀▄░░░░▀▄▒▒▒▒▒▒▒▒▒▒▀▄
      ▀▄▄▄▄▄█▄▄▄▄▄▄▄▄▄▄▄▀▄
           ▌▌░▌▌
           ▌▌░▌▌
         ▄▄▌▌▄▌▌        */


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>

#define pb emplace_back
#define re return
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using namespace std;
using namespace __gnu_cxx;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1e9 + 7;
const double pi = 3.14159265359;

ll x, n;
pair<int, int> a[1000];
ll dp[10050];
ll solve(ll h) {
  if (h <= 0)
    re 0;
  if (dp[h] != -1)
    re dp[h];
  ll ans = 1e18;
  for (int i = 0; i < n; i++)
    ans = min(ans, a[i].se + solve(h - a[i].fi));
  dp[h] = ans;
  re ans;
}
int main() {
  // freopen("distance.in", "r", stdin);
  // freopen("distance.out", "w", stdout);
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> x >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i].fi >> a[i].se;
  memset(dp, -1, sizeof(dp));
  cout << solve(x);
}
/// ACTUAL CODE STARTS HERE:
//  I'm red in soul
/*
     ▄▀▀▀▄
▄███▀░◐░░░▌
    ▌░░░░░▐
    ▐░░░░░▐
    ▌░░░░░▐▄▄
    ▌░░░░▄▀▒▒▀▀▀▀▄
   ▐░░░░▐▒▒▒▒▒▒▒▒▀▀▄
   ▐░░░░▐▄▒▒▒▒▒▒▒▒▒▒▀▄
    ▀▄░░░░▀▄▒▒▒▒▒▒▒▒▒▒▀▄
      ▀▄▄▄▄▄█▄▄▄▄▄▄▄▄▄▄▄▀▄
           ▌▌░▌▌
           ▌▌░▌▌
         ▄▄▌▌▄▌▌        */
Problem p02787 (C++)

Buggy submission (status: Runtime Error):
// #define _GLIBCXX_DEBUG
#include <algorithm>
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
using namespace std;
// using Graph = vector<vector<int>>;
typedef long long ll;
using Graph = vector<vector<pair<ll, ll>>>;
const int mod = 1e+9 + 7;

const int dy[4] = {0, 1, 0, -1};
const int dx[4] = {1, 0, -1, 0};
const ll INF = 1e10;

int main() {
  ll h, n;
  cin >> h >> n;
  vector<ll> dp(n + 1, INF);
  dp[0] = 0;
  rep(i, n) {
    ll a, b;
    cin >> a >> b;
    rep(j, h + 1) {
      int idx = min(j + a, h);
      dp[idx] = min(dp[idx], dp[j] + b);
    }
  }
  cout << dp[h] << endl;
}


Fixed submission:
// #define _GLIBCXX_DEBUG
#include <algorithm>
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
using namespace std;
// using Graph = vector<vector<int>>;
typedef long long ll;
using Graph = vector<vector<pair<ll, ll>>>;
const int mod = 1e+9 + 7;

const int dy[4] = {0, 1, 0, -1};
const int dx[4] = {1, 0, -1, 0};
const ll INF = 1e10;

int main() {
  ll h, n;
  cin >> h >> n;
  vector<ll> dp(h + 1, INF);
  dp[0] = 0;
  rep(i, n) {
    ll a, b;
    cin >> a >> b;
    rep(j, h + 1) {
      int idx = min(j + a, h);
      dp[idx] = min(dp[idx], dp[j] + b);
    }
  }
  cout << dp[h] << endl;
}
Problem p02787 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <fstream>
#include <iostream>
#include <limits>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <type_traits>
#include <vector>
using namespace std;
typedef long long ll;

const ll MOD = 1e9 + 7;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define out(str) cout << str << endl
#define ALL(a) (a).begin(), (a).end()
#define INF_INT (1 << 30)
#define INF_LL (1ll << 62)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

ll dp[1010][10100], H, N;

ll min(ll a, ll b) {
  if (a > b)
    return b;
  else
    return a;
}

int main() {
  cin >> H >> N;
  ll A[N], B[N];
  rep(i, N) { cin >> A[i] >> B[i]; }
  rep(i, N) rep(j, 10100) if (j) dp[i][j] = 10000000;
  rep(i, N) {
    rep(j, H + 1) {
      if (j < A[i])
        dp[i][j] = min(dp[i - 1][j], B[i]);
      else
        dp[i][j] = min(min(dp[i - 1][j], dp[i - 1][j - A[i]] + B[i]),
                       dp[i][j - A[i]] + B[i]);
    }
  }
  out(dp[N - 1][H]);
}

Fixed submission:
#include <algorithm>
#include <fstream>
#include <iostream>
#include <limits>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <type_traits>
#include <vector>
using namespace std;
typedef long long ll;

const ll MOD = 1e9 + 7;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define out(str) cout << str << endl
#define ALL(a) (a).begin(), (a).end()
#define INF_INT (1 << 30)
#define INF_LL (1ll << 62)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

ll dp[1010][10100], H, N;

ll min(ll a, ll b) {
  if (a > b)
    return b;
  else
    return a;
}

int main() {
  cin >> H >> N;
  ll A[N], B[N];
  rep(i, N) { cin >> A[i] >> B[i]; }
  rep(i, N) rep(j, 10010) if (j) dp[i][j] = 10000000;
  repi(j, 1, H + 1) {
    if (j < A[0])
      dp[0][j] = B[0];
    else
      dp[0][j] = dp[0][j - A[0]] + B[0];
  }
  repi(i, 1, N) {
    rep(j, H + 1) {
      if (j < A[i])
        dp[i][j] = min(dp[i - 1][j], B[i]);
      else
        dp[i][j] = min(min(dp[i - 1][j], dp[i - 1][j - A[i]] + B[i]),
                       dp[i][j - A[i]] + B[i]);
    }
  }
  out(dp[N - 1][H]);
}
Problem p02787 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n - 1; i >= 0; i--)

typedef long long ll;
#define INF 1e18

int main() {
  ll h;
  cin >> h;
  ll n;
  cin >> n;
  vector<ll> a(n);
  vector<ll> b(n);
  REP(i, n) {
    cin >> a[i];
    cin >> b[i];
  }
  vector<ll> m(h + 1, INF);
  m[0] = 0;
  FOR(i, 0, h) {
    REP(j, n) {
      FOR(k, i + 1, i + a[j] + 1) {
        if (k > h)
          break;
        m[k] = min(m[k], m[i] + b[j]);
      }
    }
    // cout << "m: "; REP(i,(signed)m.size()) cout<<m[i]<<" "; cout << endl;
    // //----------cout----------//
  }
  cout << m[h] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n - 1; i >= 0; i--)

typedef long long ll;
#define INF 1e18

int main() {
  ll h;
  cin >> h;
  ll n;
  cin >> n;
  vector<ll> a(n);
  vector<ll> b(n);
  REP(i, n) {
    cin >> a[i];
    cin >> b[i];
  }
  vector<ll> m(h + 1, INF);
  m[0] = 0;
  REP(j, n) {
    FOR(k, 1, a[j] + 1) {
      if (k > h)
        break;
      m[k] = min(m[k], b[j]);
    }
  }
  // cout << "m: "; REP(i,(signed)m.size()) cout<<m[i]<<" "; cout << endl;
  // //----------cout----------//
  REP(i, h) {
    FOR(j, 1, h) {
      if (j + i > h)
        break;
      m[j + i] = min(m[j + i], m[i] + m[j]);
    }
    // cout << "m: "; REP(i,(signed)m.size()) cout<<m[i]<<" "; cout << endl;
    // //----------cout----------//
  }
  cout << m[h] << endl;
  return 0;
}
Problem p02787 (C++)

Buggy submission (status: Runtime Error):
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author Kamol Paul
 */

#include <fstream>
#include <iostream>

#include <bits/stdc++.h>
using namespace std;
#define long long long
typedef pair<long, long> pii;

class ECrestedIbisVsMonster {
public:
  static bool cmp(pii a, pii b) {
    return (a.second / a.first) > (b.second / b.first);
  }

  void solve(std::istream &in, std::ostream &out) {
    long h, n;
    in >> h >> n;
    vector<pair<long, long>> data(n);
    for (auto &x : data)
      in >> x.first >> x.second;
    sort(data.begin(), data.end(), cmp);
    long tk[h + 100];
    for (int i = 0; i <= h + 10; i++) {
      tk[i] = LONG_MAX;
    }
    tk[0] = 0;
    for (int i = 0; i < n; i++) {
      long cost = data[i].second;
      long cover = data[i].first;
      for (int j = 1; j < cover; j++) {
        tk[j] = min(tk[j], cost);
      }
      for (int j = cover; j <= h; j++) {
        tk[j] = min(tk[j], (tk[j - cover]) + cost);
      }
    }
    out << tk[h] << endl;
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ECrestedIbisVsMonster solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}


Fixed submission:
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author Kamol Paul
 */

#include <fstream>
#include <iostream>

#include <bits/stdc++.h>
using namespace std;
#define long long long
typedef pair<long, long> pii;

class ECrestedIbisVsMonster {
public:
  static bool cmp(pii a, pii b) {
    return (a.second / a.first) > (b.second / b.first);
  }

  void solve(std::istream &in, std::ostream &out) {
    long h, n;
    in >> h >> n;
    vector<pair<long, long>> data(n);
    for (auto &x : data)
      in >> x.first >> x.second;
    sort(data.begin(), data.end(), cmp);
    long tk[h + 100];
    for (int i = 0; i <= h + 10; i++) {
      tk[i] = LONG_MAX;
    }
    tk[0] = 0;
    for (int i = 0; i < n; i++) {
      long cost = data[i].second;
      long cover = data[i].first;
      for (int j = 1; j < cover && j <= h; j++) {
        tk[j] = min(tk[j], cost);
      }
      for (int j = cover; j <= h; j++) {
        tk[j] = min(tk[j], (tk[j - cover]) + cost);
      }
    }
    out << tk[h] << endl;
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ECrestedIbisVsMonster solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}
Problem p02787 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  // freopen("input.txt","r",stdin);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int h, n, i, j;
  cin >> h >> n;
  int a[n + 2], b[n + 2];
  int dp[10003];
  memset(dp, 0x3f, sizeof(dp));
  dp[0] = 0;
  for (i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
    for (j = 0; j < h; j++) {
      dp[min(a[i] + j, h)] = min(min(dp[a[i] + j], h), dp[j] + b[i]);
    }
  }
  cout << dp[h];
  return 0;
}

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

int main() {
  // freopen("input.txt","r",stdin);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int h, n, i, j;
  cin >> h >> n;
  int a[n + 2], b[n + 2];
  int dp[10003];
  memset(dp, 0x3f, sizeof(dp));
  dp[0] = 0;
  for (i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
    for (j = 0; j < h; j++) {
      dp[min(a[i] + j, h)] = min(dp[min(a[i] + j, h)], dp[j] + b[i]);
    }
  }
  cout << dp[h];
  return 0;
}
Problem p02787 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ALL(A) (A).begin(), (A).end()
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)

const ll mod = 1e9 + 7;
const ll INF = -1 * ((1LL << 63) + 1);
const int inf = -1 * ((1 << 31) + 1);

using namespace std;
ll dp[10005]; // dp[i] := HPをi減らす時の最小の魔力消費量
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  ll H, N;
  cin >> H >> N;
  vector<ll> A(N), B(N);
  rep(i, N) { cin >> A[i] >> B[i]; }
  rep(i, 1e4 + 5) dp[i] = 1e18;
  dp[0] = 0;
  rep(a, 1e2) {
    for (int j = 0; j <= 1e4; j++) {
      rep(i, N) {
        if (j - A[i] >= 0)
          dp[j] = min(dp[j], dp[j - A[i]] + B[i]);
      }
    }
  }
  ll ans = 1e18;
  for (int i = 1e4 + 4; i >= H; i--) {
    ans = min(ans, dp[i]);
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define ALL(A) (A).begin(), (A).end()
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)

const ll mod = 1e9 + 7;
const ll INF = -1 * ((1LL << 63) + 1);
const int inf = -1 * ((1 << 31) + 1);

using namespace std;
ll dp[10005]; // dp[i] := HPをi減らす時の最小の魔力消費量
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  ll H, N;
  cin >> H >> N;
  vector<ll> A(N), B(N);
  rep(i, N) { cin >> A[i] >> B[i]; }
  rep(i, 1e4 + 5) dp[i] = 1e18;
  dp[0] = 0;
  rep(a, 1e1) {
    for (int j = 0; j <= 1e4; j++) {
      rep(i, N) {
        if (j - A[i] >= 0)
          dp[j] = min(dp[j], dp[j - A[i]] + B[i]);
      }
    }
  }
  ll ans = 1e18;
  for (int i = 1e4 + 4; i >= H; i--) {
    ans = min(ans, dp[i]);
  }
  cout << ans << endl;
}
Problem p02787 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {

  int h, n;
  cin >> h >> n;
  vector<int> a(n), b(n);
  for (int i = 0; i < n; i++)
    cin >> a.at(i) >> b.at(i);

  vector<int> dp(2 * pow(10, 9));
  for (int i = 0; i < pow(10, 9); i++) {
    if (dp.at(i) >= h) {
      cout << i << endl;
      break;
    }
    for (int j = 0; j < n; j++) {
      if (dp.at(i) + a.at(j) > dp.at(i + b.at(j)))
        dp.at(i + b.at(j)) = dp.at(i) + a.at(j);
    }
  }

  return 0;
}

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

int main() {

  int h, n;
  cin >> h >> n;
  vector<int> a(n), b(n);
  for (int i = 0; i < n; i++)
    cin >> a.at(i) >> b.at(i);

  vector<int> dp(2 * pow(10, 8));
  for (int i = 0; i < 10 + pow(10, 8); i++) {
    if (dp.at(i) >= h) {
      cout << i << endl;
      break;
    }
    for (int j = 0; j < n; j++) {
      if (dp.at(i) + a.at(j) > dp.at(i + b.at(j)))
        dp.at(i + b.at(j)) = dp.at(i) + a.at(j);
    }
  }

  return 0;
}
Problem p02787 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
using namespace std;

signed main() {
  int H, N;
  cin >> H >> N;
  vector<int> dp(10000 + 1000, 999999999999);
  vector<int> A(N);
  vector<int> B(N);
  dp[0] = 0;
  for (int i = 0; i < N; i++)
    cin >> A[i] >> B[i];
  for (int j = 0; j <= H; j++) {
    for (int i = 0; i < N; i++) {
      dp[j + A[i]] = min(dp[j + A[i]], dp[j] + B[i]);
    }
  }
  int maxA = *max_element(A.begin(), A.end());
  int ans = dp[H];
  for (int i = 0; i <= maxA; i++) {
    ans = min(ans, dp[H + i]);
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
using namespace std;

signed main() {
  int H, N;
  cin >> H >> N;
  vector<int> dp(100000 + 1000, 999999999999);
  vector<int> A(N);
  vector<int> B(N);
  dp[0] = 0;
  for (int i = 0; i < N; i++)
    cin >> A[i] >> B[i];
  for (int j = 0; j <= H; j++) {
    for (int i = 0; i < N; i++) {
      dp[j + A[i]] = min(dp[j + A[i]], dp[j] + B[i]);
    }
  }
  int maxA = *max_element(A.begin(), A.end());
  int ans = dp[H];
  for (int i = 0; i <= maxA; i++) {
    ans = min(ans, dp[H + i]);
  }
  cout << ans << endl;
}
Problem p02787 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

const int MOD = 1e9 + 7;

// const int MOD=998244353;

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

  int n, h;
  cin >> h >> n;

  vector<pair<int, int>> vp(n);
  for (auto &p : vp) {
    int x, y;
    cin >> x >> y;
    p = {x, y};
  }

  vector<int> dists(h + 1, MOD);

  dists[h] = 0;
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      pq;
  pq.push({dists[h], h});
  while (!pq.empty()) {
    auto cur = pq.top();
    pq.pop();
    int x = cur.second;

    for (auto &p : vp) {
      int y = max(0, x - p.first);
      if (dists[y] > dists[x] + p.second) {
        dists[y] = dists[x] + p.second;
        pq.push({dists[y], y});
      }
    }
  }
  cout << dists[0];

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int MOD = 1e9 + 7;

// const int MOD=998244353;

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

  int n, h;
  cin >> h >> n;

  vector<pair<int, int>> vp(n);
  for (auto &p : vp) {
    int x, y;
    cin >> x >> y;
    p = {x, y};
  }

  vector<int> dists(h + 1, MOD);

  dists[h] = 0;
  for (int i = h - 1; i >= 0; i--) {
    for (auto p : vp) {
      int y = min(h, i + p.first);
      dists[i] = min(dists[i], dists[y] + p.second);
    }
  }

  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      pq;
  pq.push({dists[h], h});
  while (!pq.empty()) {
    auto cur = pq.top();
    pq.pop();
    int x = cur.second;

    for (auto &p : vp) {
      int y = max(0, x - p.first);
      if (dists[y] > dists[x] + p.second) {
        dists[y] = dists[x] + p.second;
        pq.push({dists[y], y});
      }
    }
  }
  cout << dists[0];

  return 0;
}
Problem p02787 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define MOD 1000000007
#define ll long long
#define mp make_pair
#define pb push_back
using namespace std;
int A[10100], B[10100], n, dp[10110][1010];
int fun(int a, int b) {
  if (a <= 0)
    return 0;
  if (b == n)
    return 1e9;
  if (dp[a][b] != -1)
    return dp[a][b];
  return min(B[b] + fun(a - A[b], b), fun(a, b + 1));
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int h;
  cin >> h >> n;
  for (int i = 0; i < n; i++)
    cin >> A[i] >> B[i];
  memset(dp, -1, sizeof(dp));
  cout << fun(h, 0) << '\n';
}

Fixed submission:
#include <bits/stdc++.h>
#define MOD 1000000007
#define ll long long
#define mp make_pair
#define pb push_back
using namespace std;
int A[10100], B[10100], n, dp[10110][1010];
int fun(int a, int b) {
  if (a <= 0)
    return 0;
  if (b == n)
    return 1e9;
  if (dp[a][b] != -1)
    return dp[a][b];
  return min(B[b] + fun(a - A[b], b), fun(a, b + 1));
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int h;
  cin >> h >> n;
  for (int i = 0; i < n; i++)
    cin >> A[i] >> B[i];
  for (int i = 0; i < n; i++)
    dp[0][i] = 0;
  for (int i = 1; i <= h; i++) {
    for (int j = 0; j < n; j++) {
      dp[i][j] = 1e9;
      dp[i][j] = min(B[j] + dp[max(0, i - A[j])][j], dp[i][j]);
      if (j)
        dp[i][j] = min(dp[i][j], dp[i][j - 1]);
    }
  }
  cout << dp[h][n - 1] << '\n';
}
Problem p02787 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define ll int64_t
#define _GLIBCXX_DEBUG
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int main() {
  int h, n;
  cin >> h >> n;
  vector<int> a(n), b(n);
  rep(i, n) cin >> a.at(i) >> b.at(i);
  int dp[h + 1];
  rep(i, h) dp[i] = 2000000000;
  dp[h] = 0;
  rep(i, n) {
    for (int j = h - a.at(i); j >= 0; j--)
      dp[j] = min(dp[j], dp[a.at(i) + j] + b.at(i));
    for (int j = a.at(i) - 1; j > 0; j--)
      dp[0] = min(dp[0], dp[j] + b.at(i));
  }
  cout << dp[0] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define ll int64_t
#define _GLIBCXX_DEBUG
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int main() {
  int h, n;
  cin >> h >> n;
  vector<int> a(n), b(n);
  rep(i, n) cin >> a.at(i) >> b.at(i);
  int dp[h + 1];
  rep(i, h) dp[i] = 2000000000;
  dp[h] = 0;
  rep(i, n) {
    if (a.at(i) > h) {
      dp[0] = min(dp[0], b.at(i));
      continue;
    }
    for (int j = h - a.at(i); j >= 0; j--)
      dp[j] = min(dp[j], dp[a.at(i) + j] + b.at(i));
    for (int j = a.at(i) - 1; j > 0; j--)
      dp[0] = min(dp[0], dp[j] + b.at(i));
  }
  cout << dp[0] << endl;
}
Problem p02787 (Python)

Buggy submission (status: Runtime Error):
import sys
import numpy as np

h, n = list(map(int, input().split()))
INF = 10**18
dp = np.full(h + 10001, INF, dtype=np.int64)
dp[0] = 0
for line in sys.stdin:
    a, b = map(int, line.split())
    for s in range(0, h, a):
        sa = s + a
        s2a = s + 2 * a
        dp[sa:s2a] = np.minimum(dp[sa:s2a], dp[s:sa] + b)
print(dp[h:].min())


Fixed submission:
import sys
import numpy as np

h, n = map(int, input().split())
ab = np.array(sys.stdin.read().split(), dtype=np.int64)
aaa = ab[0::2]
bbb = ab[1::2]

dp = np.zeros(10001, dtype=np.int64)
for i in range(1, h + 1):
    dp[i] = (dp[i - aaa] + bbb).min()
print(dp[h])
Problem p02787 (Python)

Buggy submission (status: Time Limit Exceeded):
H, N = map(int, input().split())
magic = [list(map(int, input().split())) for _ in range(N)]


max_magic = 0
for a, _ in magic:
    max_magic = max(max_magic, a)

amount = H + 1
dp = [0] * (amount + max_magic)
dp[0] = 0

ret = float("inf")

for amt in range(1, amount + max_magic):
    t = [dp[amt - a] + b if amt - a >= 0 else float("inf") for a, b in magic]
    dp[amt] = min(t)

    if amt >= H:
        ret = min(dp[amt], ret)

print(ret)


Fixed submission:
H, N = map(int, input().split())
magic = [list(map(int, input().split())) for _ in range(N)]


max_magic = 0
for a, _ in magic:
    max_magic = max(max_magic, a)

amount = H + 1
dp = [0] * (amount + max_magic)
dp[0] = 0

ret = float("inf")

for amt in range(1, amount + max_magic):
    t = [dp[amt - a] + b if amt - a >= 0 else float("inf") for a, b in magic]

    dp[amt] = min(t)

    if amt >= H:
        ret = min(dp[amt], ret)

print(ret)
Problem p02787 (Python)

Buggy submission (status: Time Limit Exceeded):
import sys
import functools


def inpl():
    return list(map(int, input().split()))


@functools.lru_cache(maxsize=None)
def solve(h):
    if h <= 0:
        return 0

    ret_val = float("inf")
    for a, b in AB:
        ret_val = min(ret_val, b + solve(h - a))
    return ret_val


sys.setrecursionlimit(100000)

H, N = inpl()
AB = [inpl() for i in range(N)]
AB.sort(key=lambda ab: (ab[0] / ab[1], -ab[0]), reverse=True)
# print(AB)
print(solve(H))


Fixed submission:
import sys
import functools


def inpl():
    return list(map(int, input().split()))


@functools.lru_cache(maxsize=None)
def solve(h):
    if h <= 0:
        return 0

    ret_val = float("inf")
    for a, b in AB:
        val = b + solve(h - a)
        if val < ret_val:
            ret_val = val
        else:
            break
    return ret_val


sys.setrecursionlimit(100000)

H, N = inpl()
AB = [inpl() for i in range(N)]
AB.sort(key=lambda ab: (ab[0] / ab[1], -ab[0]), reverse=True)
# print(AB)
print(solve(H))
Problem p02787 (C++)

Buggy submission (status: Runtime Error):
#define HAVE_STRUCT_TIMESPEC
#include <bits/stdc++.h>
using namespace std;
int a[1007], b[1007], f[1007];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int h, n;
  cin >> h >> n;
  for (int i = 1; i <= n; ++i)
    cin >> a[i] >> b[i];
  for (int i = 1; i <= h; ++i)
    f[i] = 1e9;
  f[0] = 0;
  for (int i = 1; i <= n; ++i)
    for (int j = 0; j <= h; ++j) {
      int x = max(0, j - a[i]);
      f[j] = min(f[j], f[x] + b[i]);
    }
  cout << f[h];
  return 0;
}


Fixed submission:
#define HAVE_STRUCT_TIMESPEC
#include <bits/stdc++.h>
using namespace std;
int a[10007], b[10007], f[10007];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int h, n;
  cin >> h >> n;
  for (int i = 1; i <= n; ++i)
    cin >> a[i] >> b[i];
  for (int i = 1; i <= h; ++i)
    f[i] = 1e9;
  f[0] = 0;
  for (int i = 1; i <= n; ++i)
    for (int j = 0; j <= h; ++j) {
      int x = max(0, j - a[i]);
      f[j] = min(f[j], f[x] + b[i]);
    }
  cout << f[h];
  return 0;
}
Problem p02787 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, n) for (int i = 0; i < (n); i++)
#define MOD 1000000007
#define all(n) (n).begin(), (n).end()
typedef long long ll;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  } else {
    return 0;
  }
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  } else {
    return 0;
  }
}
template <class T> inline T GCD(T a, T b) {
  T c;
  while (b != 0) {
    c = a % b;
    a = b;
    b = c;
  }
  return a;
}
template <class T> inline T LCM(T a, T b) { return a * b / GCD(a, b); }

int main() {
  ll h, n;
  cin >> h >> n;
  ll a[1010], b[1010];
  FOR(i, n) { cin >> a[i] >> b[i]; }
  ll dp[20010];
  FOR(i, 20010) dp[i] = MOD;
  dp[0] = 0;
  for (int i = 0; i < h; i++) {
    FOR(j, n) { chmin(dp[i + a[j]], dp[i] + b[j]); }
  }

  ll ans = MOD;
  for (int i = h; i < 200010; i++) {
    chmin(ans, dp[i]);
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, n) for (int i = 0; i < (n); i++)
#define MOD 1000000007
#define all(n) (n).begin(), (n).end()
typedef long long ll;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  } else {
    return 0;
  }
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  } else {
    return 0;
  }
}
template <class T> inline T GCD(T a, T b) {
  T c;
  while (b != 0) {
    c = a % b;
    a = b;
    b = c;
  }
  return a;
}
template <class T> inline T LCM(T a, T b) { return a * b / GCD(a, b); }

int main() {
  ll h, n;
  cin >> h >> n;
  ll a[1010], b[1010];
  FOR(i, n) { cin >> a[i] >> b[i]; }
  ll dp[20010];
  FOR(i, 20010) dp[i] = MOD;
  dp[0] = 0;
  for (int i = 0; i < h; i++) {
    FOR(j, n) { chmin(dp[i + a[j]], dp[i] + b[j]); }
  }

  ll ans = MOD;
  for (int i = h; i < 20010; i++) {
    chmin(ans, dp[i]);
  }
  cout << ans << endl;
}
Problem p02787 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define all(a) (a).begin(), (a).end()
typedef long long ll;
const ll mod = 1000000007;
#define repi(i, a, b) for (ll i = ll(a); i < ll(b); ++i)

int main() {
  ll h, n;
  cin >> h >> n;
  ll inf = mod;
  vector<ll> a(n), b(n);
  for (ll i = 0; i < n; ++i)
    cin >> a[i] >> b[i];
  ll dp[6 * h + 1][n + 1];
  for (ll j = 0; j <= 6 * h; ++j) {
    for (ll i = 0; i < n + 1; ++i) {
      if (j == 0) {
        dp[j][i] = 0;
      } else {
        dp[j][i] = inf;
      }
    }
  }
  ll out = inf;
  for (ll j = 1; j <= 6 * h; ++j) {
    for (ll i = 1; i <= n; ++i) {
      if (j - a[i - 1] >= 0) {
        dp[j][i] = min(dp[j - a[i - 1]][i] + b[i - 1], dp[j][i - 1]);
      } else {
        dp[j][i] = dp[j][i - 1];
      }
    }
    if (j >= h)
      out = min(out, dp[j][n]);
  }
  /*for(int i=0;i<=n;++i){
    cout << dp[h][i] << " ";
  }*/
  // cout << endl;
  cout << out << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define all(a) (a).begin(), (a).end()
typedef long long ll;
const ll mod = 1000000007;
#define repi(i, a, b) for (ll i = ll(a); i < ll(b); ++i)

int main() {
  ll h, n;
  cin >> h >> n;
  ll inf = mod;
  vector<ll> a(n), b(n);
  for (ll i = 0; i < n; ++i)
    cin >> a[i] >> b[i];
  vector<vector<ll>> dp(6 * h + 1, vector<ll>(n + 1, inf));
  for (ll i = 0; i < n + 1; ++i)
    dp[0][i] = 0;
  ll out = inf;
  for (ll j = 1; j <= 6 * h; ++j) {
    for (ll i = 1; i <= n; ++i) {
      if (j - a[i - 1] >= 0) {
        dp[j][i] = min(dp[j - a[i - 1]][i] + b[i - 1], dp[j][i - 1]);
      } else {
        dp[j][i] = dp[j][i - 1];
      }
    }
    if (j >= h)
      out = min(out, dp[j][n]);
  }
  /*for(int i=0;i<=n;++i){
    cout << dp[h][i] << " ";
  }*/
  // cout << endl;
  cout << out << endl;
  return 0;
}