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

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

using namespace std;

template <class T> inline bool chmin(T &a, T b) { /* {{{ */
  if (a > b) {
    a = b;
    return true;
  }
  return false;
} /* }}} */

int main() {
  int H, N;
  cin >> H >> N;
  vector<int> A(N), B(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i] >> B[i];
  }
  vector<vector<int>> dp(N + 1, vector<int>(H + 1, 1e9));
  dp[0][0] = 0;
  for (int j = 1; j <= A[0]; j++) {
    dp[0][j] = B[0];
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= H; j++) {
      chmin(dp[i + 1][j], dp[i][j]);
      int v = dp[i][j] + B[i];
      if (j + A[i] < H) {
        chmin(dp[i][j + A[i]], v);
        chmin(dp[i + 1][j + A[i]], v);
      } else {
        chmin(dp[i][H], v);
        chmin(dp[i + 1][H], v);
      }
    }
  }
  cout << dp[N][H] << endl;

  return 0;
}
/* vim:set fdm=marker: */


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

using namespace std;

template <class T> inline bool chmin(T &a, T b) { /* {{{ */
  if (a > b) {
    a = b;
    return true;
  }
  return false;
} /* }}} */

int main() {
  int H, N;
  cin >> H >> N;
  vector<int> A(N), B(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i] >> B[i];
  }
  vector<vector<int>> dp(N + 1, vector<int>(H + 1, 1e9));
  dp[0][0] = 0;
  for (int j = 1; j <= A[0]; j++) {
    if (j <= H) {
      dp[0][j] = B[0];
    }
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= H; j++) {
      chmin(dp[i + 1][j], dp[i][j]);
      int v = dp[i][j] + B[i];
      if (j + A[i] < H) {
        chmin(dp[i][j + A[i]], v);
        chmin(dp[i + 1][j + A[i]], v);
      } else {
        chmin(dp[i][H], v);
        chmin(dp[i + 1][H], v);
      }
    }
  }
  cout << dp[N][H] << endl;

  return 0;
}
/* vim:set fdm=marker: */
Problem p02787 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define debug(x) cout << #x " = " << (x) << '\n';
#define P(x) cout << #x << '\n';
#define int long long
typedef pair<int, int> pii;
const int mod = 1000000007;

const int maxn = 1e5 + 10;
int a[maxn], b[maxn], dp[maxn];
const int inf = 1e15;
const int till = 3e4;

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << fixed << setprecision(9);
#ifdef LOCAL_DEFINE
  freopen("in.txt", "rt", stdin);
#endif

  for (int i = 0; i < maxn; ++i)
    dp[i] = inf;
  int h, n;
  cin >> h >> n;
  for (int i = 1; i <= n; ++i)
    cin >> a[i] >> b[i];
  dp[0] = 0;
  for (int i = 0; i <= till; ++i) {
    for (int j = 1; j <= n; ++j) {
      dp[i] = min(dp[i], dp[i - a[j]] + b[j]);
    }
  }

  int ans = inf;
  for (int i = h; i <= till; ++i)
    ans = min(ans, dp[i]);

  cout << ans << '\n';

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define debug(x) cout << #x " = " << (x) << '\n';
#define P(x) cout << #x << '\n';
#define int long long
typedef pair<int, int> pii;
const int mod = 1000000007;

const int maxn = 1e5 + 10;
int a[maxn], b[maxn], dp[maxn];
const int inf = 1e15;
const int till = 3e4;

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << fixed << setprecision(9);
#ifdef LOCAL_DEFINE
  freopen("in.txt", "rt", stdin);
#endif

  for (int i = 0; i < maxn; ++i)
    dp[i] = inf;
  int h, n;
  cin >> h >> n;
  for (int i = 1; i <= n; ++i)
    cin >> a[i] >> b[i];
  dp[0] = 0;
  for (int i = 0; i <= till; ++i) {
    for (int j = 1; j <= n; ++j) {
      if (i - a[j] >= 0)
        dp[i] = min(dp[i], dp[i - a[j]] + b[j]);
    }
  }

  int ans = inf;
  for (int i = h; i <= till; ++i)
    ans = min(ans, dp[i]);

  cout << ans << '\n';

  return 0;
}
Problem p02787 (C++)

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

#define SORT(c) sort((c).begin(), (c).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
#define ANS(ans) cout << (ans) << endl;
#define UNIQUE(v) (v).erase(unique((v).begin(), (v).end()), (v).end());
typedef vector<int> VI;
typedef pair<int, int> P;

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

struct Mystruct {
  long long atack;
  long long mp;
  double cost;

  Mystruct(long long input_a, long long input_b) {
    atack = input_a;
    mp = input_b;
    cost = input_a * 1.0 / input_b;
  }
  bool operator<(const Mystruct &another) const {
    if (cost == another.cost) {
      return mp < another.mp;
    }
    return cost < another.cost; // 年齢を比較
  };
};

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

  vector<Mystruct> magics;
  REP(i, n) {
    long long atack, mp;
    cin >> atack >> mp;

    magics.push_back(Mystruct(atack, mp));
  }
  long long MAX = pow(10, 9);
  vector<long long> dp(MAX, 0);
  // cout << MAX << endl;

  long long mp = 1;
  while (true) {
    long long temp_max = 0;

    REP(i, n) {
      long long temp = 0;
      if (mp - magics[i].mp >= 0) {
        temp = dp[mp - magics[i].mp] + magics[i].atack;
      }
      temp_max = max(temp_max, temp);
    }
    dp[mp] = temp_max;
    // cout << temp_max << endl;
    if (dp[mp] >= h) {
      ANS(mp);
      break;
    }
    mp++;
  }
  return 0;
}


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

#define SORT(c) sort((c).begin(), (c).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
#define ANS(ans) cout << (ans) << endl;
#define UNIQUE(v) (v).erase(unique((v).begin(), (v).end()), (v).end());
typedef vector<int> VI;
typedef pair<int, int> P;

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

struct Mystruct {
  long long atack;
  long long mp;
  double cost;

  Mystruct(long long input_a, long long input_b) {
    atack = input_a;
    mp = input_b;
    cost = input_a * 1.0 / input_b;
  }
  bool operator<(const Mystruct &another) const {
    if (cost == another.cost) {
      return mp < another.mp;
    }
    return cost < another.cost; // 年齢を比較
  };
};

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

  vector<Mystruct> magics;
  REP(i, n) {
    long long atack, mp;
    cin >> atack >> mp;

    magics.push_back(Mystruct(atack, mp));
  }
  long long MAX = pow(10, 8);
  vector<long long> dp(MAX, 0);
  // cout << MAX << endl;

  long long mp = 1;
  while (true) {
    long long temp_max = 0;

    REP(i, n) {
      long long temp = 0;
      if (mp - magics[i].mp >= 0) {
        temp = dp[mp - magics[i].mp] + magics[i].atack;
      }
      temp_max = max(temp_max, temp);
    }
    dp[mp] = temp_max;
    // cout << temp_max << endl;
    if (dp[mp] >= h) {
      ANS(mp);
      break;
    }
    mp++;
  }
  return 0;
}
Problem p02787 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {
  ll h, n;
  cin >> h >> n;
  vector<pair<ll, ll>> ab(n);
  rep(i, n) cin >> ab[i].first >> ab[i].second;

  vector<ll> dp(1e9, 0);
  for (ll i = 0; i <= 1e9; i++) {
    rep(j, n) {
      if (i - ab[j].second >= 0) {
        dp[i] = max(dp[i], dp[i - ab[j].second] + ab[j].first);
      }
    }
    if (dp[i] >= h) {
      cout << i << endl;
      return 0;
    }
  }

  // for (ll i = 0; i <= h; i++) {
  //     if (dp[i] >= h) {
  //         cout << i << endl;
  //         return 0;
  //     }
  // }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {
  ll h, n;
  cin >> h >> n;
  vector<pair<ll, ll>> ab(n);
  rep(i, n) cin >> ab[i].first >> ab[i].second;

  vector<ll> dp(1e8, 0);
  for (ll i = 0; i <= 1e8; i++) {
    rep(j, n) {
      if (i - ab[j].second >= 0) {
        dp[i] = max(dp[i], dp[i - ab[j].second] + ab[j].first);
      }
    }
    if (dp[i] >= h) {
      cout << i << endl;
      return 0;
    }
  }

  // for (ll i = 0; i <= h; i++) {
  //     if (dp[i] >= h) {
  //         cout << i << endl;
  //         return 0;
  //     }
  // }
}
Problem p02787 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, a, n) for (ll i = a; i < (ll)(n); i++)
#define memi cout << endl
#define kono(n) cout << fixed << setprecision(n)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define hina cout << ' '
#define in(n) cin >> n
#define in2(n, m) cin >> n >> m
#define in3(n, m, l) cin >> n >> m >> l
#define out(n) cout << n
const ll mei = (ll)1e9 + 7;

int main() {
  ll h, n, k, d;
  in2(h, n);
  vector<ll> a(n), b(n), c(h + 1, 1000000000);
  rep(i, n) in2(a[i], b[i]);
  c[0] = 0;
  rep2(i, 1, h + 1) {
    rep(j, n) {
      k = 1;
      while (i - (k - 1) * a[j] > 0) {
        d = max(i - k * a[j], 0ll);
        c[i] = min(c[i], c[d] + k * b[j]);
        k++;
      }
    }
  }
  out(c[h]);
  memi;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, a, n) for (ll i = a; i < (ll)(n); i++)
#define memi cout << endl
#define kono(n) cout << fixed << setprecision(n)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define hina cout << ' '
#define in(n) cin >> n
#define in2(n, m) cin >> n >> m
#define in3(n, m, l) cin >> n >> m >> l
#define out(n) cout << n
const ll mei = (ll)1e9 + 7;

int main() {
  ll h, n, k, d;
  in2(h, n);
  vector<ll> a(n), b(n), c(h + 1, 1000000000);
  rep(i, n) in2(a[i], b[i]);
  c[0] = 0;
  rep2(i, 1, h + 1) {
    rep(j, n) {
      d = max(i - a[j], 0ll);
      c[i] = min(c[i], c[d] + b[j]);
    }
  }
  out(c[h]);
  memi;
}
Problem p02787 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;
typedef long long ll;

int dp[150000000];

int main() {
  int H, N;
  int A[10000], B[10000];

  scanf("%d %d", &H, &N);
  for (int i = 0; i < N; i++) {
    scanf("%d %d", &A[i], &B[i]);
  }

  int jCount = 1000000;
  if (N <= 10) {
    jCount *= 150;
  }

  //	for(int i = 0; i < N+1; i++){
  for (int j = 0; j < jCount; j++) {
    dp[j] = 0;
  }
  //	}

  for (int i = 1; i < N + 1; i++) {
    for (int j = 0; j < jCount; j++) {
      if (j >= B[i - 1]) {
        int tmp = dp[j - B[i - 1]] + A[i - 1];
        dp[j] = max(dp[j], tmp);
      } else {
        // dp[j] = dp[j];
      }
    }
  }

  ll min = 9999999999;

  for (int j = 0; j < jCount; j++) {
    if (dp[j] >= H) {
      if (j < min) {
        min = j;
      }
    }
  }

  printf("%lld\n", min);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;
typedef long long ll;

int dp[150000000];

int main() {
  int H, N;
  int A[10000], B[10000];

  scanf("%d %d", &H, &N);
  for (int i = 0; i < N; i++) {
    scanf("%d %d", &A[i], &B[i]);
  }

  int jCount = 1000000;
  if (N <= 10) {
    jCount *= 110;
  }

  //	for(int i = 0; i < N+1; i++){
  for (int j = 0; j < jCount; j++) {
    dp[j] = 0;
  }
  //	}

  for (int i = 1; i < N + 1; i++) {
    for (int j = 0; j < jCount; j++) {
      if (j >= B[i - 1]) {
        int tmp = dp[j - B[i - 1]] + A[i - 1];
        dp[j] = max(dp[j], tmp);
      } else {
        // dp[j] = dp[j];
      }
    }
  }

  ll min = 9999999999;

  for (int j = 0; j < jCount; j++) {
    if (dp[j] >= H) {
      if (j < min) {
        min = j;
      }
    }
  }

  printf("%lld\n", min);

  return 0;
}
Problem p02787 (Python)

Buggy submission (status: Runtime Error):
n, m = map(int, input().split())
dmg = []
cost = []

for i in range(m):
    a, b = map(int, input().split())
    dmg.append(a)
    cost.append(b)

inf = 10**9
dp = [inf] * 10001
dp[0] = 0

for i in range(m):
    for j in range(n):
        if dp[j] != inf:
            dp[j + dmg[i]] = min(dp[j + dmg[i]], dp[j] + cost[i])
print(min(dp[n:]))


Fixed submission:
n, m = map(int, input().split())
dmg = []
cost = []

for i in range(m):
    a, b = map(int, input().split())
    dmg.append(a)
    cost.append(b)

inf = 10**9
dp = [inf] * 30000
dp[0] = 0

for i in range(m):
    for j in range(n):
        if dp[j] != inf:
            dp[j + dmg[i]] = min(dp[j + dmg[i]], dp[j] + cost[i])
print(min(dp[n:]))
Problem p02787 (C++)

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

typedef long long ll;
const int INF = 2e9;
// const ll INF = 9e18;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int H, N;
  cin >> H >> N;
  vector<int> A(N), B(N);
  for (int i = 0; i < N; i++)
    cin >> A[i] >> B[i];
  int amin = INF;
  for (int i = 0; i < N; i++)
    amin = min(amin, A[i]);
  vector<int> dp(H + 10, INF);
  dp[0] = 0;
  for (int i = 0; i <= H / amin; i++) {
    for (int j = 0; j < H; j++) {
      for (int k = 0; k < N; k++) {
        int nj = min(j + A[k], H);
        dp[nj] = min(dp[nj], dp[j] + B[k]);
      }
    }
  }
  cout << dp[H] << "\n";
}

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

typedef long long ll;
const int INF = 2e9;
// const ll INF = 9e18;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int H, N;
  cin >> H >> N;
  vector<int> A(N), B(N);
  for (int i = 0; i < N; i++)
    cin >> A[i] >> B[i];
  int amin = INF;
  for (int i = 0; i < N; i++)
    amin = min(amin, A[i]);
  vector<int> dp(H + 10, INF);
  dp[0] = 0;
  for (int j = 0; j < H; j++) {
    for (int k = 0; k < N; k++) {
      int nj = min(j + A[k], H);
      dp[nj] = min(dp[nj], dp[j] + B[k]);
    }
  }
  cout << dp[H] << "\n";
}
Problem p02787 (C++)

Buggy submission (status: Time Limit Exceeded):
// Author : Mohamed Sameh
#include <bits/stdc++.h>
typedef long long ll;
#define pb push_back
#define f first
#define s second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define SZ(a) (int)a.size()
#define Flush fflush(stdout);
using namespace std;
int h, n;
const int M = 1e3;
int a[M][2];
ll dp[M][10001];
ll solve(int idx = 0, int rem = h) {
  if (rem <= 0)
    return 0;
  if (idx >= n)
    return 1e12;
  // cout << idx <<" " << rem <<"\n";
  ll &ret = dp[idx][rem];
  if (~ret)
    return ret;
  ret = 1e12;
  ll curcost = 0;
  int currem = rem;
  while (rem) {
    rem -= a[idx][0];
    curcost += a[idx][1];
    ret = min(ret, curcost + solve(idx + 1, rem));
    rem = max(0, rem);
  }
  rem = currem;
  ret = min(ret, solve(idx + 1, rem));

  return ret;
}
int main() {
  memset(dp, -1, sizeof dp);
  scanf("%d%d", &h, &n);
  for (int i = 0; i < n; i++) {
    scanf("%d%d", a[i], a[i] + 1);
  }
  printf("%lld\n", solve());
}


Fixed submission:
// Author : Mohamed Sameh
#include <bits/stdc++.h>
typedef long long ll;
#define pb push_back
#define f first
#define s second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define SZ(a) (int)a.size()
#define Flush fflush(stdout);
using namespace std;
int h, n;
const int M = 1e3;
int a[M][2];
ll dp[M][10001];
ll solve(int idx = 0, int rem = h) {
  if (rem <= 0)
    return 0;
  if (idx >= n)
    return 1e12;
  // cout << idx <<" " << rem <<"\n";
  ll &ret = dp[idx][rem];
  if (~ret)
    return ret;
  ret = solve(idx + 1, rem);
  ret = min(ret, a[idx][1] + solve(idx, rem - a[idx][0]));
  return ret;
}
int main() {
  memset(dp, -1, sizeof dp);
  scanf("%d%d", &h, &n);
  for (int i = 0; i < n; i++) {
    scanf("%d%d", a[i], a[i] + 1);
  }
  printf("%lld\n", solve());
}
Problem p02787 (C++)

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

#define F first
#define S second

using namespace std;

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

const int N = 1010, M = 20010;

int n, m;
int a[N], b[N];
int f[M];

int main() {
  scanf("%d%d", &m, &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 = m + a[i]; j >= 0; j--)
      for (int k = 0; k <= j / a[i]; k++)
        f[j] = min(f[j], f[j - k * a[i]] + k * b[i]);

  int res = 0x3f3f3f3f;
  for (int i = m; i < M; i++)
    res = min(res, f[i]);
  printf("%d\n", res);
  return 0;
}


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

#define F first
#define S second

using namespace std;

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

const int N = 1010, M = 20010;

int n, m;
int a[N], b[N];
int f[M];

int main() {
  scanf("%d%d", &m, &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 = a[i]; j <= m + a[i]; j++)
      f[j] = min(f[j], f[j - a[i]] + b[i]);

  int res = 0x3f3f3f3f;
  for (int i = m; i < M; i++)
    res = min(res, f[i]);
  printf("%d\n", res);
  return 0;
}
Problem p02787 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define vout(x) rep(i, x.size()) cout << x[i] << " "
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int, int>;
const int inf = 1e9;
const ll inf_l = 1e18;
const int MAX = 1e4;

int main() {
  int h, n;
  cin >> h >> n;
  vvint dp(1005, vint(10005, inf));
  dp[0][0] = 0;
  REP(i, n + 1) {
    int a, b;
    cin >> a >> b;
    rep(j, 10005) {
      chmin(dp[i][j], dp[i - 1][j]);
      for (int k = max(0, j - a); k <= j - 1; k++) {
        chmin(dp[i][j], dp[i][k] + b);
      }
    }
  }
  cout << dp[n][h] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define vout(x) rep(i, x.size()) cout << x[i] << " "
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int, int>;
const int inf = 1e9;
const ll inf_l = 1e18;
const int MAX = 1e4;

int main() {
  int h, n;
  cin >> h >> n;
  vvint dp(1005, vint(10005, inf));
  dp[0][0] = 0;
  REP(i, n + 1) {
    int a, b;
    cin >> a >> b;
    rep(j, 10005) {
      chmin(dp[i][j], dp[i - 1][j]);
      chmin(dp[i][j], dp[i][max(j - a, 0)] + b);
    }
  }
  cout << dp[n][h] << endl;
}
Problem p02787 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

vector<vector<int>> dp(10000, vector<int>(10000, 999999999));
vector<int> A(10000), B(10000);
int H, N;

void input() {
  cin >> H >> N;
  for (int i = 0; i < N; i++) {
    cin >> A.at(i) >> B.at(i);
  }
  return;
}

void DP() {
  dp.at(0).at(0) = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= H; j++) {
      if (A.at(i) >=
          j) { // 今考える魔法が与えるダメージが体力j以上のとき（＝一撃で倒せるとき）
        dp.at(i + 1).at(j) = min(dp.at(i).at(j), B.at(i));
      } else {
        dp.at(i + 1).at(j) =
            min(dp.at(i).at(j), dp.at(i + 1).at(j - A.at(i)) + B.at(i));
      }
    }
  }
  return;
}

void output() {
  cout << dp.at(N).at(H) << endl;
  return;
}

int main() {
  input();
  DP();
  output();
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

vector<vector<int>> dp(10010, vector<int>(10010, 999999999));
vector<int> A(10010), B(10010);
int H, N;

void input() {
  cin >> H >> N;
  for (int i = 0; i < N; i++) {
    cin >> A.at(i) >> B.at(i);
  }
  return;
}

void DP() {
  dp.at(0).at(0) = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= H; j++) {
      if (A.at(i) >=
          j) { // 今考える魔法が与えるダメージが体力j以上のとき（＝一撃で倒せるとき）
        dp.at(i + 1).at(j) = min(dp.at(i).at(j), B.at(i));
      } else {
        dp.at(i + 1).at(j) =
            min(dp.at(i).at(j), dp.at(i + 1).at(j - A.at(i)) + B.at(i));
      }
    }
  }
  return;
}

void output() {
  cout << dp.at(N).at(H) << endl;
  return;
}

int main() {
  input();
  DP();
  output();
  return 0;
}
Problem p02787 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;

#define MOD 1000000007

int main() {
  int h, n;
  cin >> h >> n;
  vector<int> a(n), b(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i] >> b[i];
  }
  int dp[n + 1][h + 100001];
  for (int i = 0; i <= n; ++i) {
    for (int j = 0; j <= h + 10000; ++j) {
      dp[i][j] = 1e9;
    }
  }
  dp[0][0] = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= h + 10000; ++j) {
      dp[i + 1][j] = min(dp[i][j], dp[i + 1][j - a[i]] + b[i]);
    }
  }
  int ans = 1e9;
  for (int i = h; i <= h + 10000; ++i) {
    ans = min(ans, dp[n][i]);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;

#define MOD 1000000007

int main() {
  int h, n;
  cin >> h >> n;
  vector<int> a(n), b(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i] >> b[i];
  }
  int dp[n + 1][h + 10001];
  for (int i = 0; i <= n; ++i) {
    for (int j = 0; j <= h + 10000; ++j) {
      dp[i][j] = 1e9;
    }
  }
  dp[0][0] = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= h + 10000; ++j) {
      dp[i + 1][j] = min(dp[i][j], dp[i + 1][j - a[i]] + b[i]);
    }
  }
  int ans = 1e9;
  for (int i = h; i <= h + 10000; ++i) {
    ans = min(ans, dp[n][i]);
  }
  cout << ans << endl;
  return 0;
}
Problem p02787 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include "bits/stdc++.h"
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a), i##_len = (b); i < i##_len; ++i)
#define MSVC_UNKO(x) x
#define rep(...)                                                               \
  MSVC_UNKO(_overload3(__VA_ARGS__, repi, _rep, _rep)(__VA_ARGS__))
#define all(c) c.begin(), c.end()
#define write(x) cout << (x) << '\n'
using namespace std;
typedef long long ll;
template <class T> using vv = vector<vector<T>>;
template <class T> auto vvec(int n, int m, T v) {
  return vv<T>(n, vector<T>(m, v));
}
constexpr int INF = 1 << 29, MOD = int(1e9) + 7;
constexpr ll LINF = 1LL << 60;
struct aaa {
  aaa() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(10);
  };
} aaaa;

int main() {
  int H, N;
  cin >> H >> N;
  vector<int> A(N), B(N);
  rep(i, N) cin >> A[i] >> B[i];

  rep(i, N) {
    for (int a = A[i], b = B[i]; a < H;) {
      a *= 2, b *= 2;
      A.push_back(a);
      B.push_back(b);
      N++;
    }
  }

  vv<int> dp = vvec(N + 1, 4e4 + 1, INF);
  dp[0][0] = 0;
  rep(i, N) rep(h, 0, 2e4 + 1) {
    dp[i + 1][h + A[i]] = dp[i][h] + B[i];
    dp[i + 1][h] = min(dp[i + 1][h], dp[i][h]);
  }
  int ans = INF;
  rep(h, H, 2e4 + 1) ans = min(ans, dp[N][h]);
  write(ans);
}

Fixed submission:
#include "bits/stdc++.h"
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a), i##_len = (b); i < i##_len; ++i)
#define MSVC_UNKO(x) x
#define rep(...)                                                               \
  MSVC_UNKO(_overload3(__VA_ARGS__, repi, _rep, _rep)(__VA_ARGS__))
#define all(c) c.begin(), c.end()
#define write(x) cout << (x) << '\n'
using namespace std;
typedef long long ll;
template <class T> using vv = vector<vector<T>>;
template <class T> auto vvec(int n, int m, T v) {
  return vv<T>(n, vector<T>(m, v));
}
constexpr int INF = 1 << 29, MOD = int(1e9) + 7;
constexpr ll LINF = 1LL << 60;
struct aaa {
  aaa() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(10);
  };
} aaaa;

int main() {
  int H, N;
  cin >> H >> N;
  vector<int> A(N), B(N);
  rep(i, N) cin >> A[i] >> B[i];

  vector<int> dp(2e4 + 1, INF);
  dp[0] = 0;
  rep(i, N) rep(h, H + 1) { dp[h + A[i]] = min(dp[h + A[i]], dp[h] + B[i]); }
  write(*min_element(dp.begin() + H, dp.end()));
}
Problem p02787 (C++)

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

using namespace std;

const int N = 5000;
long long dp[N];
int a[N], b[N];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  int h, n;
  cin >> h >> n;
  for (int i = 0; i < h; ++i) {
    dp[i] = 1e18;
  }
  for (int i = 0; i < n; ++i) {
    cin >> a[i] >> b[i];
  }
  for (int i = 0; i < n; ++i) {
    for (int j = h; j >= 0; --j) {
      int v = max(0, j - a[i]);
      dp[v] = min(dp[v], dp[j] + b[i]);
    }
  }
  cout << dp[0];

  return 0;
}

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

using namespace std;

const int N = 1e4 + 10;
long long dp[N];
int a[N], b[N];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  int h, n;
  cin >> h >> n;
  for (int i = 0; i < h; ++i) {
    dp[i] = 1e18;
  }
  for (int i = 0; i < n; ++i) {
    cin >> a[i] >> b[i];
  }
  for (int i = 0; i < n; ++i) {
    for (int j = h; j >= 0; --j) {
      int v = max(0, j - a[i]);
      dp[v] = min(dp[v], dp[j] + b[i]);
    }
  }
  cout << dp[0];

  return 0;
}
Problem p02787 (Python)

Buggy submission (status: Runtime Error):
def main():
    h, n = map(int, input().split())
    ab = [list(map(int, input().split())) for _ in range(n)]

    dp = [0] + [0] * h
    for i in range(1, h + 1):
        dp[i] = min(dp[i - a] + b for a, b in ab)
    print(dp[h])


if __name__ == "__main__":
    main()


Fixed submission:
def main():
    h, n = map(int, input().split())
    ab = [list(map(int, input().split())) for _ in range(n)]

    amax = max(a for a, b in ab)
    dp = [0] + [0] * (h + amax)
    for i in range(1, h + 1):
        dp[i] = min(dp[i - a] + b for a, b in ab)
    print(dp[h])


if __name__ == "__main__":
    main()
Problem p02787 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 10010;
int h, n, a, b, dp[MAXN];
int main() {
  scanf("%d%d", &h, &n);
  memset(dp, 0x3f, sizeof(dp));
  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &a, &b);
    for (int i = a; i <= h; i++) {
      dp[i] = min(dp[i], dp[i - a] + b);
    }
    for (int i = h - a; i <= h; i++) {
      dp[h] = min(dp[h], dp[i] + b);
    }
  }
  printf("%d\n", dp[h]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 10010;
int h, n, a, b, dp[MAXN];
int main() {
  scanf("%d%d", &h, &n);
  memset(dp, 0x3f, sizeof(dp));
  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &a, &b);
    for (int i = a; i <= h; i++) {
      dp[i] = min(dp[i], dp[i - a] + b);
    }
    for (int i = max(h - a, 0); i <= h; i++) {
      dp[h] = min(dp[h], dp[i] + b);
    }
  }
  printf("%d\n", dp[h]);
  return 0;
}
Problem p02787 (Python)

Buggy submission (status: Runtime Error):
H, N = map(int, input().split())
AB = [tuple(map(int, input().split())) for _ in range(N)]
AB.sort(key=lambda x: x[1])

dp = [0] * (H + 1)
for h in range(1, H + 1):
    dp[h] = min([dp[h - a] + b for a, b in AB])

print(dp[-1])


Fixed submission:
H, N = map(int, input().split())
AB = [tuple(map(int, input().split())) for _ in range(N)]
AB.sort(key=lambda x: x[1])

dp = [0] * (H + 1)
for h in range(1, H + 1):
    dp[h] = min([dp[max(h - a, 0)] + b for a, b in AB])

print(dp[-1])
Problem p02787 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
using P = pair<int, int>;
int dp[11000];
int h, n;
const int INF = 1001001001;
int main() {
  cin >> h >> n;
  rep(j, h + 1) dp[j] = INF;
  dp[0] = 0;
  rep(i, n) {
    int a, b;
    cin >> a >> b;
    rep(j, h + 1) { dp[j] = min(dp[j], dp[j - a] + b); }
  }
  cout << dp[h] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
using P = pair<int, int>;
int dp[11000];
int h, n;
const int INF = 1001001001;
int main() {
  cin >> h >> n;
  rep(j, h + 1) dp[j] = INF;
  dp[0] = 0;
  rep(i, n) {
    int a, b;
    cin >> a >> b;
    rep(j, h + 1) { dp[min(j + a, h)] = min(dp[min(j + a, h)], dp[j] + b); }
  }
  cout << dp[h] << endl;
  return 0;
}
Problem p02787 (Python)

Buggy submission (status: Runtime Error):
import sys


def solve(h, n, ab):
    ab.sort(key=lambda x: x[1] / x[0])
    INF = 10**18
    cache = {}
    # とりあえず一番コスパいいやつだけを使った結果を暫定最良値としておく
    best = ((h - 1) // ab[0][0] + 1) * ab[0][1]

    def dp(k, i=0, cost=0):
        nonlocal best
        if k <= 0:
            return cost
        if (k, i) in cache:
            return cache[k, i] + cost
        a, b = ab[i]
        if k / a * b + cost >= best:
            # どう足掻いても残りでbestより良い結果は得られない
            return INF
        c = (k - 1) // a + 1
        if i == n - 1:
            ret = cost + b * c
        else:
            ret = min(dp(k - a * j, i + 1, cost + b * j) for j in range(c + 1))
        cache[k, i] = ret - cost
        best = min(best, ret)
        return ret

    dp(h)
    return best


sys.setrecursionlimit(1001)

h, n = map(int, input().split())
ab = [tuple(map(int, line.split())) for line in sys.stdin]
print(solve(h, n, ab))


Fixed submission:
import sys


def solve(h, n, ab):
    ab.sort(key=lambda x: x[1] / x[0])
    INF = 10**18
    cache = {}
    # とりあえず一番コスパいいやつだけを使った結果を暫定最良値としておく
    best = ((h - 1) // ab[0][0] + 1) * ab[0][1]

    def dp(k, i=0, cost=0):
        nonlocal best
        if k <= 0:
            return cost
        if (k, i) in cache:
            return cache[k, i] + cost
        a, b = ab[i]
        if k / a * b + cost >= best:
            # どう足掻いても残りでbestより良い結果は得られない
            return INF
        c = (k - 1) // a + 1
        if i == n - 1:
            ret = cost + b * c
        else:
            ret = min(dp(k - a * j, i + 1, cost + b * j) for j in range(c + 1))
        cache[k, i] = ret - cost
        best = min(best, ret)
        return ret

    dp(h)
    return best


sys.setrecursionlimit(10001)

h, n = map(int, input().split())
ab = [tuple(map(int, line.split())) for line in sys.stdin]
print(solve(h, n, ab))
Problem p02787 (Python)

Buggy submission (status: Runtime Error):
h, n, *L = map(int, open(0).read().split())
dp = [0] * (h + 1)
for i in range(1, h + 1):
    dp[i] = min(dp[i - a] + b for a, b in zip(*[iter(L)] * 2))

print(dp[h])


Fixed submission:
h, n, *L = map(int, open(0).read().split())
dp = [0] * (h + 99999)
for i in range(1, h + 1):
    dp[i] = min(dp[i - a] + b for a, b in zip(*[iter(L)] * 2))

print(dp[h])
Problem p02787 (Python)

Buggy submission (status: Runtime Error):
def main():
    h, n = map(int, input().split())
    magic_list = []

    for _ in range(n):
        a, b = map(int, input().split())
        magic_list.append((a, b))

    ans = solve(h, magic_list)
    print(ans)


def solve(h, magic_list):
    # 1-origin
    dp = [0] * (h + 1)

    # time O(HN)
    for hp in range(1, h + 1):
        dp[hp] = min(dp[hp - attack] + mp for attack, mp in magic_list)

    return dp[h]


main()


Fixed submission:
def main():
    h, n = map(int, input().split())
    magic_list = []

    for _ in range(n):
        a, b = map(int, input().split())
        magic_list.append((a, b))

    ans = solve(h, magic_list)
    print(ans)


def solve(h, magic_list):
    # 1-origin
    max_attack = max(magic_list)[0]
    dp = [0] * (h + max_attack)

    # time O(HN)
    for hp in range(1, h + 1):
        dp[hp] = min(dp[hp - attack] + mp for attack, mp in magic_list)

    return dp[h]


main()
Problem p02787 (C++)

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

int main() {
  int H, N;
  cin >> H >> N;
  vector<int> A(N), B(N);
  int maxA = 0;
  for (int i = 0; i < N; i++) {
    cin >> A[i] >> B[i];
    maxA = max(maxA, A[i]);
  }

  int Hmax = H + maxA + 1;
  vector<vector<int>> dp(N, vector<int>(Hmax, 1e9));
  for (int h = 0; h <= Hmax; h++) {
    dp[0][h] = (h + A[0] - 1) / A[0] * B[0];
  }

  for (int i = 0; i < N - 1; i++)
    for (int h = 0; h <= Hmax; h++) {
      dp[i + 1][h] = dp[i][h];
      if (h - A[i + 1] >= 0)
        dp[i + 1][h] = min(dp[i + 1][h], dp[i + 1][h - A[i + 1]] + B[i + 1]);
    }

  int ans = 1e9;
  for (int h = H; h <= Hmax; h++) {
    ans = min(ans, dp[N - 1][h]);
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
using namespace std;

int main() {
  int H, N;
  cin >> H >> N;
  vector<int> A(N), B(N);
  int maxA = 0;
  for (int i = 0; i < N; i++) {
    cin >> A[i] >> B[i];
    maxA = max(maxA, A[i]);
  }

  int Hmax = H + maxA + 1;
  vector<vector<int>> dp(N, vector<int>(Hmax + 1, 1e9));
  for (int h = 0; h <= Hmax; h++) {
    dp[0][h] = (h + A[0] - 1) / A[0] * B[0];
  }

  for (int i = 0; i < N - 1; i++)
    for (int h = 0; h <= Hmax; h++) {
      dp[i + 1][h] = dp[i][h];
      if (h - A[i + 1] >= 0)
        dp[i + 1][h] = min(dp[i + 1][h], dp[i + 1][h - A[i + 1]] + B[i + 1]);
    }

  int ans = 1e9;
  for (int h = H; h <= Hmax; h++) {
    ans = min(ans, dp[N - 1][h]);
  }
  cout << ans << endl;
}
Problem p02787 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define DEBUG 1
using namespace std;
constexpr int kMod = 1000000007;
typedef long long LL;
constexpr LL kInf = 1e18;

template <typename T> istream &operator>>(istream &is, vector<T> &vs) {
  for (T &v : vs)
    is >> v;
  return is;
}

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vs) {
  for (const T &v : vs)
    os << v << " ";
  return os;
}

int main() {
  LL H, N;
  cin >> H >> N;
  vector<vector<LL>> dp(N + 1, vector<LL>(H + 1, kInf));
  for (int i = 0; i <= N; ++i)
    dp[i][H] = 0;
  for (int i = 1; i <= N; ++i) {
    LL A, B;
    cin >> A >> B;
    for (int j = H - 1; j > H - A; --j) {
      dp[i][j] = min(dp[i - 1][j], B);
    }
    for (int j = H - A; j >= 0; --j) {
      dp[i][j] = min(dp[i - 1][j], dp[i][j + A] + B);
    }
  }
  cout << dp[N][0] << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define DEBUG 1
using namespace std;
constexpr int kMod = 1000000007;
typedef long long LL;
constexpr LL kInf = 1e18;

template <typename T> istream &operator>>(istream &is, vector<T> &vs) {
  for (T &v : vs)
    is >> v;
  return is;
}

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vs) {
  for (const T &v : vs)
    os << v << " ";
  return os;
}

int main() {
  LL H, N;
  cin >> H >> N;
  vector<vector<LL>> dp(N + 1, vector<LL>(H + 1, kInf));
  for (int i = 0; i <= N; ++i)
    dp[i][H] = 0;
  for (int i = 1; i <= N; ++i) {
    LL A, B;
    cin >> A >> B;
    for (int j = H - 1; j >= max(0LL, H - A - 1); --j) {
      dp[i][j] = min(dp[i - 1][j], B);
    }
    for (int j = H - A; j >= 0; --j) {
      dp[i][j] = min(dp[i - 1][j], dp[i][j + A] + B);
    }
  }
  cout << dp[N][0] << endl;
}
Problem p02787 (Python)

Buggy submission (status: Runtime Error):
h, n = map(int, input().split())
a = []
b = []
for i in range(n):
    a_, b_ = map(int, input().split())
    a.append(a_)
    b.append(b_)

dp = [float("inf")] * (h + 1010)
dp[0] = 0

for i in range(h + 1):
    for j in range(n):
        dp[i + a[j]] = min(dp[i + a[j]], dp[i] + b[j])
print(min(dp[h:]))


Fixed submission:
h, n = map(int, input().split())
a = []
b = []
for i in range(n):
    a_, b_ = map(int, input().split())
    a.append(a_)
    b.append(b_)

dp = [float("inf")] * (h + 10010)
dp[0] = 0

for i in range(h + 1):
    for j in range(n):
        dp[i + a[j]] = min(dp[i + a[j]], dp[i] + b[j])
print(min(dp[h:]))
Problem p02787 (C++)

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

#define ll long long
#define mod 1000000007
#define pb push_back
#define pi pair<int, int>
#define vi vector<int>
#define vpi vector<pi>
const int N = 1005;
int dp[10 * N], a[N], b[N];
int main() {

  int h, n;
  cin >> h >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> b[i];
  }
  for (int j = 1; j <= h + 10000; j++)
    dp[j] = mod;
  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = a[i]; j <= h + 10000; j++) {
      dp[j] = min(dp[j], dp[j - a[i]] + b[i]);
      // cout<<j<<" "<<dp[j]<<"\n";
    }
  }
  for (int i = h + 1; i <= h + 10000; i++)
    dp[h] = min(dp[h], dp[i]);
  if (dp[h] >= mod / 2)
    dp[h] = -1;
  cout << dp[h];

  return 0;
}

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

#define ll long long
#define mod 1000000007
#define pb push_back
#define pi pair<int, int>
#define vi vector<int>
#define vpi vector<pi>
const int N = 1005;
int dp[20 * N], a[N], b[N];
int main() {

  int h, n;
  cin >> h >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> b[i];
  }
  for (int j = 1; j <= h + 10000; j++)
    dp[j] = mod;
  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = a[i]; j <= h + 10000; j++) {
      dp[j] = min(dp[j], dp[j - a[i]] + b[i]);
      // cout<<j<<" "<<dp[j]<<"\n";
    }
  }
  for (int i = h + 1; i <= h + 10000; i++)
    dp[h] = min(dp[h], dp[i]);
  if (dp[h] >= mod / 2)
    dp[h] = -1;
  cout << dp[h];

  return 0;
}
Problem p02787 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define all(x) (x).begin(), (x).end()
#define int long long
#define fr first
#define sc second
#define vc vector
#define endl '\n'
#define pii pair<int, int>
#define msi map<string, int>
#define mii map<int, int>
#define psi pair<string, int>
#define pis pair<int, string>
#define mem1(a) memset(a, -1, sizeof(a))
#define mem0(a) memset(a, 0, sizeof(a))
#define rep(i, a, b) for (int i = a; i < b; i++)
#define LSB(i) ((i) & -(i))

template <typename T>
using oset =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const int H = 1e4 + 5;
const int N = 1e3 + 3;
int n, h;
int dp[N];
int a[N], b[N];

int dynamic(int health) {
  if (health <= 0)
    return 0;
  if (dp[health] != -1)
    return dp[health];
  int ans = INT_MAX;
  for (int i = 0; i < n; i++) {
    ans = min(ans, dynamic(health - a[i]) + b[i]);
  }
  dp[health] = ans;
  return ans;
}

void solve() {
  mem1(dp);
  cin >> h >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i] >> b[i];
  cout << dynamic(h);
}
signed main() {
  ios_base::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define all(x) (x).begin(), (x).end()
#define int long long
#define fr first
#define sc second
#define vc vector
#define endl '\n'
#define pii pair<int, int>
#define msi map<string, int>
#define mii map<int, int>
#define psi pair<string, int>
#define pis pair<int, string>
#define mem1(a) memset(a, -1, sizeof(a))
#define mem0(a) memset(a, 0, sizeof(a))
#define rep(i, a, b) for (int i = a; i < b; i++)
#define LSB(i) ((i) & -(i))

template <typename T>
using oset =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const int H = 1e4 + 5;
const int N = 1e3 + 3;
int n, h;
int dp[H];
int a[N], b[N];

int dynamic(int health) {
  if (health <= 0)
    return 0;
  if (dp[health] != -1)
    return dp[health];
  int ans = INT_MAX;
  for (int i = 0; i < n; i++) {
    ans = min(ans, dynamic(health - a[i]) + b[i]);
  }
  dp[health] = ans;
  return ans;
}

void solve() {
  mem1(dp);
  cin >> h >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i] >> b[i];
  cout << dynamic(h);
}
signed main() {
  ios_base::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
  return 0;
}
Problem p02787 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int long long int
#define mod 1000000007
#define string_mod 2549536629329_base_255
#define pb push_back
#define F first
#define S second
#define ff first
#define endl "\n"
#define ss second
#define all(v) v.begin(), v.end()
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
int power(int x, unsigned int y, int p) {
  int res = 1;
  x = x % p;
  //  cout << x << " ";
  while (y > 0) {

    if (y & 1)
      res = (res * x) % p;

    y = y >> 1;
    x = (x * x) % p;
  }
  return res % p;
}
int lcm(int a, int b) { return (a * b) / __gcd(a, b); }
int h, n;
vector<pair<int, int>> v(10001);
int dp[10001][10001];
int solve(int in, int sum) {
  // cout << sum << " ";
  if (in >= n && sum > 0) {
    return 1e15;
  }
  if (sum <= 0) {
    return 0;
  }
  int ze = 0;
  if (dp[in][max(ze, sum)] != -1) {
    return dp[in][max(ze, sum)];
  }
  int ans = 1e15;
  int s = sum - v[in].F;
  ans = min(ans, solve(in + 1, sum));
  ans = min(ans, v[in].S + solve(0, s));
  return dp[in][max(ze, sum)] = ans;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int tc;
  tc = 1;
  memset(dp, -1, sizeof(dp));
  while (tc--) {

    cin >> h >> n;
    for (int i = 0; i < n; i++) {
      cin >> v[i].F >> v[i].S;
    }
    int ans = solve(0, h);
    cout << ans << endl;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int long long int
#define mod 1000000007
#define string_mod 2549536629329_base_255
#define pb push_back
#define F first
#define S second
#define ff first
#define endl "\n"
#define ss second
#define all(v) v.begin(), v.end()
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
int power(int x, unsigned int y, int p) {
  int res = 1;
  x = x % p;
  //  cout << x << " ";
  while (y > 0) {

    if (y & 1)
      res = (res * x) % p;

    y = y >> 1;
    x = (x * x) % p;
  }
  return res % p;
}
int lcm(int a, int b) { return (a * b) / __gcd(a, b); }
int h, n;
vector<pair<int, int>> v(10001);
int dp[10001][10001];
int solve(int in, int sum) {
  // cout << sum << " ";
  if (in >= n && sum > 0) {
    return 1e15;
  }
  if (sum <= 0) {
    return 0;
  }
  int ze = 0;
  if (dp[in][max(ze, sum)] != -1) {
    return dp[in][max(ze, sum)];
  }
  int ans = 1e15;
  int s = sum - v[in].F;
  ans = min(ans, solve(in + 1, sum));
  ans = min(ans, v[in].S + solve(in, s));
  return dp[in][max(ze, sum)] = ans;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int tc;
  tc = 1;
  memset(dp, -1, sizeof(dp));
  while (tc--) {

    cin >> h >> n;
    for (int i = 0; i < n; i++) {
      cin >> v[i].F >> v[i].S;
    }
    int ans = solve(0, h);
    cout << ans << endl;
  }

  return 0;
}
Problem p02787 (C++)

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

int dp[2000];

void upd(int &x, int y) {
  if (x > y)
    x = y;
}

int a[2000], b[2000];
signed main() {
  memset(dp, 0x3f, sizeof dp);
  dp[0] = 0;
  int n, h;
  cin >> h >> n;
  int ans = 2e9;
  for (int i = 1; i <= n; i++)
    cin >> a[i] >> b[i];
  for (int i = 1; i <= n; i++) {
    //	cout<<i<<endl;
    for (int j = 0; j <= 2e4; j++) {
      if (j - a[i] >= 0)
        upd(dp[j], dp[j - a[i]] + b[i]);
      if (j >= h)
        upd(ans, dp[j]);
      //	cout<<j<<" "<<dp[j]<<endl;
    }
  }
  cout << ans << endl;

  getchar();
  getchar();
}

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

int dp[25000];

void upd(int &x, int y) {
  if (x > y)
    x = y;
}

int a[2000], b[2000];
signed main() {
  memset(dp, 0x3f, sizeof dp);
  dp[0] = 0;
  int n, h;
  cin >> h >> n;
  int ans = 2e9;
  for (int i = 1; i <= n; i++)
    cin >> a[i] >> b[i];
  for (int i = 1; i <= n; i++) {
    //	cout<<i<<endl;
    for (int j = 0; j <= 2e4; j++) {
      if (j - a[i] >= 0)
        upd(dp[j], dp[j - a[i]] + b[i]);
      if (j >= h)
        upd(ans, dp[j]);
      //	cout<<j<<" "<<dp[j]<<endl;
    }
  }
  cout << ans << endl;

  getchar();
  getchar();
}
Problem p02787 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define FOR(i, a, b) for (ll i = a; i <= b; i++)
#define FORD(i, a, b) for (ll i = a; i >= b; i--)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) int((a).size())
#define SORT(c) sort((c).begin(), (c).end())
#define PB push_back
#define MP make_pair
#define MEMSET(v, h) memset((v), h, sizeof(v))
#define sqr(x) ((x) * (x))
#define cub(x) ((x) * (x) * (x))
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
// ll lcm(ll a,ll b){return a/gcd(a,b)*b;}

int dp[20500];
const int MAX = 1001001001;

int main() {
  int h, n;
  cin >> h >> n;
  FOR(i, 1, 20499) { dp[i] = MAX; }
  FOR(i, 1, n) {
    int a, b;
    cin >> a >> b;
    FOR(j, 0, 20000 - a) {
      int ref = 1;
      while (j + a * ref <= 20000) {
        chmin(dp[j + a * ref], dp[j] + b * ref);
        ++ref;
      }
    }

    // FOR(j,0,10) cout << dp[i][j] << " ";
    // cout << endl;
  }

  int ans = MAX;
  FOR(i, h, 20000) { chmin(ans, dp[i]); }

  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define FOR(i, a, b) for (ll i = a; i <= b; i++)
#define FORD(i, a, b) for (ll i = a; i >= b; i--)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) int((a).size())
#define SORT(c) sort((c).begin(), (c).end())
#define PB push_back
#define MP make_pair
#define MEMSET(v, h) memset((v), h, sizeof(v))
#define sqr(x) ((x) * (x))
#define cub(x) ((x) * (x) * (x))
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
// ll lcm(ll a,ll b){return a/gcd(a,b)*b;}

int dp[20500];
const int MAX = 1001001001;

int main() {
  int h, n;
  cin >> h >> n;
  FOR(i, 1, 20499) { dp[i] = MAX; }
  FOR(i, 1, n) {
    int a, b;
    cin >> a >> b;
    FOR(j, 0, 20000 - a - 1) { chmin(dp[j + a], dp[j] + b); }

    // FOR(j,0,10) cout << dp[i][j] << " ";
    // cout << endl;
  }

  int ans = MAX;
  FOR(i, h, 20000) { chmin(ans, dp[i]); }

  cout << ans << endl;
}