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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long h, a = 1, s = 0;
  cin >> h;
  while (h >= 1) {
    s += a;
    a *= 2;
  }
  cout << s;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long h, a = 1, s = 0;
  cin >> h;
  while (h >= 1) {
    s += a;
    a *= 2;
    h /= 2;
  }
  cout << s;
}
Problem p02786 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>
using namespace std;
#define ll long long
// 16:12 from

int main() {
  // the biggest power of 2 that doesnot exceed n
  ll h;
  ll b = 1;
  while (2 * b <= h)
    b *= 2;
  cout << 2 * b - 1;
}
// int h;
// 	cin>>h;
// 	vector<int> dp(h+1);
// 	dp[0]=0;
// 	dp[1]=1;
// 	for(int i=2;i<=h;i++){
// 		dp[i]=1+2*dp[i/2];
// 	}
// 	cout<<dp[h];
// 	return 0;

// #include <bit>
// This containe __builtin
// look at the video from 16:12

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
#define ll long long
// 16:12 from

int main() {
  // the biggest power of 2 that doesnot exceed n
  ll h;
  cin >> h;
  ll b = 1;
  while (2 * b <= h)
    b *= 2;
  cout << 2 * b - 1;
}
// int h;
// 	cin>>h;
// 	vector<int> dp(h+1);
// 	dp[0]=0;
// 	dp[1]=1;
// 	for(int i=2;i<=h;i++){
// 		dp[i]=1+2*dp[i/2];
// 	}
// 	cout<<dp[h];
// 	return 0;

// #include <bit>
// This containe __builtin
// look at the video from 16:12
Problem p02787 (Python)

Buggy submission (status: Time Limit Exceeded):
MAX_LIMIT = 1e30
H, N = list(map(int, input().split()))
A, B = [], []
for _ in range(N):
    a, b = list(map(int, input().split()))
    A.append(a)  # HP
    B.append(b)  # power

# dp[i] means minimum power used to reduce HP i
dp = [MAX_LIMIT] * (H + max(A))
dp[0] = 0

for i in range(1, H + max(A)):
    cands = []
    for a, b in zip(A, B):
        if i - a >= 0:
            cands.append(b + dp[i - a])
    if len(cands) == 0:
        dp[i] = MAX_LIMIT  # we cannot directly reach this point
    else:
        dp[i] = min(cands)

# print(dp)
print(min(dp[H:]))


Fixed submission:
h, n = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(n)]
dp = [0] * (h + max(a for a, b in ab))
for i in range(1, h + 1):
    dp[i] = min(dp[i - a] + b for a, b in ab)
print(dp[h])
Problem p02787 (C++)

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

// マクロ
// forループ関係
// 引数は、(ループ内変数,動く範囲)か(ループ内変数,始めの数,終わりの数)、のどちらか
// Dがついてないものはループ変数は1ずつインクリメントされ、Dがついてるものはループ変数は1ずつデクリメントされる
#define REP(i, n) for (lol i = 0; i < lol(n); i++)
#define REPD(i, n) for (lol i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (lol i = a; i <= lol(b); i++)
#define FORD(i, a, b) for (lol i = a; i >= lol(b); i--)
#define VL vector<lol>
// xにはvectorなどのコンテナ
#define ALL(x) x.begin(), x.end() // sortなどの引数を省略したい
#define SIZE(x) lol(x.size())     // sizeをsize_tからllに直しておく
// 定数
#define PI 3.1415926535897932385 // pi
#define INF 1000000000000        // 10^12:極めて大きい値,∞
#define MOD 1000000007           // 10^9+7:合同式の法
#define MAXR 100000 // 10^5:配列の最大のrange(素数列挙などで使用)
// 最大値最小値
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;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

lol dp[1005][10005], n;
lol a[1005], b[10005];

lol f(lol idx, lol hp) {
  if (hp <= 0)
    return 0;
  if (idx > n)
    return INF;
  lol sol = dp[idx][hp];
  if (sol != -1)
    return sol;
  return sol = min(f(idx + 1, hp), f(idx, hp - a[idx]) + b[idx]);
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  lol result{0};
  lol h;
  cin >> h >> n;
  FOR(i, 1, n) { cin >> a[i] >> b[i]; }
  memset(dp, -1, sizeof(dp));
  result = f(1, h);

  cout << result << '\n';
  return 0;
}

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

// マクロ
// forループ関係
// 引数は、(ループ内変数,動く範囲)か(ループ内変数,始めの数,終わりの数)、のどちらか
// Dがついてないものはループ変数は1ずつインクリメントされ、Dがついてるものはループ変数は1ずつデクリメントされる
#define REP(i, n) for (lol i = 0; i < lol(n); i++)
#define REPD(i, n) for (lol i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (lol i = a; i <= lol(b); i++)
#define FORD(i, a, b) for (lol i = a; i >= lol(b); i--)
#define VL vector<lol>
// xにはvectorなどのコンテナ
#define ALL(x) x.begin(), x.end() // sortなどの引数を省略したい
#define SIZE(x) lol(x.size())     // sizeをsize_tからllに直しておく
// 定数
#define PI 3.1415926535897932385 // pi
#define INF 1000000000000        // 10^12:極めて大きい値,∞
#define MOD 1000000007           // 10^9+7:合同式の法
#define MAXR 100000 // 10^5:配列の最大のrange(素数列挙などで使用)
// 最大値最小値
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;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

lol dp[1005][10005], n;
lol a[1005], b[10005];

lol f(lol idx, lol hp) {
  if (hp <= 0)
    return 0;
  if (idx > n)
    return INF;
  lol &sol = dp[idx][hp];
  if (sol != -1)
    return sol;
  return sol = min(f(idx + 1, hp), f(idx, hp - a[idx]) + b[idx]);
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  lol result{0};
  lol h;
  cin >> h >> n;
  FOR(i, 1, n) { cin >> a[i] >> b[i]; }
  memset(dp, -1, sizeof(dp));
  result = f(1, h);

  cout << result << '\n';
  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 < (int)(n); i++)
const int INF = 1001001001;

int main() {
  int n, h;
  cin >> h >> n;
  vector<int> a(n), b(n);
  rep(i, n) cin >> a[i] >> b[i];

  // dp[i][j] i番目までの魔法、モンスターのHPjまでの最小魔法消費量
  vector<vector<int>> dp(n + 1, vector<int>(h + 1));
  rep(i, n + 1) rep(j, h + 2) dp[i][j] = INF;
  rep(i, n) dp[i][0] = 0;
  rep(i, n) {
    rep(j, h + 1) {
      if (j - a[i] >= 0) {
        dp[i + 1][j] = min(dp[i][j], dp[i + 1][j - a[i]] + b[i]);
      } else
        dp[i + 1][j] = min(dp[i][j], dp[i + 1][0] + b[i]);
      // cout << dp[i+1][j] << " ";
    }
    // cout << endl;
  }
  cout << dp[n][h] << endl;
}

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

int main() {
  int n, h;
  cin >> h >> n;
  vector<int> a(n), b(n);
  rep(i, n) cin >> a[i] >> b[i];

  // dp[i][j] i番目までの魔法、モンスターのHPjまでの最小魔法消費量
  vector<vector<int>> dp(n + 1, vector<int>(h + 1));
  rep(i, n + 1) rep(j, h + 1) dp[i][j] = INF;
  rep(i, n) dp[i][0] = 0;
  rep(i, n) {
    rep(j, h + 1) {
      if (j - a[i] >= 0) {
        dp[i + 1][j] = min(dp[i][j], dp[i + 1][j - a[i]] + b[i]);
      } else
        dp[i + 1][j] = min(dp[i][j], dp[i + 1][0] + b[i]);
      // cout << dp[i+1][j] << " ";
    }
    // cout << endl;
  }
  cout << dp[n][h] << endl;
}
Problem p02787 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define MOD (long long)(1E9 + 7)
#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

int main() {
  int h, n, t = 0;
  cin >> h >> n;
  int dp[n + 1][h + 1];
  vector<int> a(n);
  vector<int> b(n);
  rep(i, n) {
    cin >> a[i] >> b[i];
    for (int j = 0; j <= h; j++) {
      t = (b[i] * ((j - 1) / a[i] + 1));
      if (i == 0) {
        dp[i][j] = t;
        continue;
      }
      dp[i][j] = min(t, dp[i - 1][j]);
      for (int k = 1; j >= a[i] * k; k++) {
        dp[i][j] = min(dp[i][j], dp[i - 1][j - a[i] * k] + b[i] * k);
      }
    }
  }
  cout << dp[n - 1][h] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define MOD (long long)(1E9 + 7)
#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

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

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

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) v.begin(), v.end()

template <class T> bool chmin(T &v, const T &v2) {
  if (v > v2) {
    v = v2;
    return true;
  } else
    return false;
}

using namespace std;

auto main() -> int {
  int H, N;
  cin >> H >> N;
  vector<int> dp(10001, 1e9);
  dp[0] = 0;

  while (N--) {
    int damage, mp;
    cin >> damage >> mp;
    REP(i, H) chmin(dp[i + damage], dp[i] + mp);
  }

  REPR(i, dp.size() - 2) chmin(dp[i], dp[i + 1]);
  cout << dp[H] << endl;
}

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

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) v.begin(), v.end()

template <class T> bool chmin(T &v, const T &v2) {
  if (v > v2) {
    v = v2;
    return true;
  } else
    return false;
}

using namespace std;

auto main() -> int {
  int H, N;
  cin >> H >> N;
  vector<int> dp(100000, 1e9);
  dp[0] = 0;

  while (N--) {
    int damage, mp;
    cin >> damage >> mp;
    REP(i, H) chmin(dp[i + damage], dp[i] + mp);
  }

  REPR(i, dp.size() - 2) chmin(dp[i], dp[i + 1]);
  cout << dp[H] << endl;
}
Problem p02787 (C++)

Buggy submission (status: Runtime Error):
/* #                          */

#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
// #pragma warning(disable:4996)
using namespace std;

/** Type Define **/

typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<char> vch;

constexpr int inf = 0x3f3f3f3f;
constexpr long long INF = 9123456789123456789;
constexpr long long MOD = 1000000007LL;
/*************************************************/
int H, N;
vi dp;

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

void sol() {
  for (int i = 0; i < N; i++) {
    int a, b; // a: damage, b: cost.
    cin >> a >> b;
    for (int i = 0; i < a; i++)
      Min(dp[i], b);
    for (int i = a; i <= H; i++)
      Min(dp[i], dp[i - a] + b);
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  // cout << fixed << setprecision(10);
  cin.tie(nullptr);
  cin >> H >> N;
  dp.assign(H + 1, inf);
  dp[0] = 0;

  sol();
  cout << dp[H] << '\n';

  return 0;
}


Fixed submission:
/* #                          */

#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
// #pragma warning(disable:4996)
using namespace std;

/** Type Define **/

typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<char> vch;

constexpr int inf = 0x3f3f3f3f;
constexpr long long INF = 9123456789123456789;
constexpr long long MOD = 1000000007LL;
/*************************************************/
int H, N;
vi dp;

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

void sol() {
  for (int i = 0; i < N; i++) {
    int a, b; // a: damage, b: cost.
    cin >> a >> b;
    for (int i = 0; i < a; i++)
      Min(dp[i], b);
    for (int i = a; i <= H; i++)
      Min(dp[i], dp[i - a] + b);
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  // cout << fixed << setprecision(10);
  cin.tie(nullptr);
  cin >> H >> N;
  dp.assign(10001, INT32_MAX);
  dp[0] = 0;

  sol();
  cout << dp[H] << '\n';

  return 0;
}
Problem p02787 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000 + 20;
const int INF = 0x3f3f3f3f;
int p[maxn], v[maxn];
int dp[10000 + 50];
int main() {
  int n, m;
  while (scanf("%d %d", &m, &n) != EOF) {
    for (int i = 1; i <= n; i++) {
      scanf("%d %d", &v[i], &p[i]);
    }
    memset(dp, INF, sizeof(dp));
    dp[0] = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = v[i]; j <= 20000; j++) {
        dp[j] = min(dp[j], dp[j - v[i]] + p[i]);
      }
    }

    int maxx = dp[m];
    int ans = m;
    for (int i = m; i <= 10000; i++) {
      if (dp[i] <= maxx) {
        maxx = dp[i];
        ans = i;
      }
    }
    printf("%d\n", maxx);
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000 + 20;
const int INF = 0x3f3f3f3f;
int p[maxn], v[maxn];
int dp[20000 + 50];
int main() {
  int n, m;
  while (scanf("%d %d", &m, &n) != EOF) {
    for (int i = 1; i <= n; i++) {
      scanf("%d %d", &v[i], &p[i]);
    }
    memset(dp, INF, sizeof(dp));
    dp[0] = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = v[i]; j <= 20000; j++) {
        dp[j] = min(dp[j], dp[j - v[i]] + p[i]);
      }
    }

    int maxx = dp[m];
    int ans = m;
    for (int i = m; i <= 10000; i++) {
      if (dp[i] <= maxx) {
        maxx = dp[i];
        ans = i;
      }
    }
    printf("%d\n", maxx);
  }

  return 0;
}
Problem p02787 (C++)

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

#ifdef NON_SUBMIT
#define TEST(n) (n)
#define tout cerr
#else
#define TEST(n) ((void)0)
#define tout cin
#endif

using namespace std;

int D[10001];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  TEST(freopen("input.txt", "r", stdin));
  TEST(freopen("output.txt", "w", stdout));
  TEST(freopen("debug.txt", "w", stderr));
  int H, N;
  cin >> H >> N;
  memset(D, 0x7f, sizeof(D));
  D[0] = 0;
  while (N--) {
    int A, B;
    cin >> A >> B;
    for (int i = A; i <= 30000; i++)
      D[i] = min(D[i], D[i - A] + B);
    for (int i = 30000; i > 0; i--)
      D[i - 1] = min(D[i - 1], D[i]);
  }
  cout << D[H] << '\n';
  return 0;
}

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

#ifdef NON_SUBMIT
#define TEST(n) (n)
#define tout cerr
#else
#define TEST(n) ((void)0)
#define tout cin
#endif

using namespace std;

int D[30001];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  TEST(freopen("input.txt", "r", stdin));
  TEST(freopen("output.txt", "w", stdout));
  TEST(freopen("debug.txt", "w", stderr));
  int H, N;
  cin >> H >> N;
  memset(D, 0x7f, sizeof(D));
  D[0] = 0;
  while (N--) {
    int A, B;
    cin >> A >> B;
    for (int i = A; i <= 30000; i++)
      D[i] = min(D[i], D[i - A] + B);
    for (int i = 30000; i > 0; i--)
      D[i - 1] = min(D[i - 1], D[i]);
  }
  cout << D[H] << '\n';
  return 0;
}
Problem p02787 (C++)

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>
using namespace std;
int w[300], c[300], f[300010];
int V, n, mx;
int main() {
  scanf("%d%d", &V, &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &w[i], &c[i]);
    mx = max(mx, w[i]);
  }
  memset(f, 0x3f, sizeof f);
  f[0] = 0;
  for (int i = 1; i <= n; i++)
    for (int j = w[i]; j <= V + mx; j++)
      f[j] = min(f[j], f[j - w[i]] + c[i]);
  int ans = f[V];
  for (int i = V + 1; i <= V + mx; ++i) {
    ans = min(f[i], ans);
  }
  printf("%d\n", ans);
  return 0;
}

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int w[300010], c[300010], f[300010];
int V, n, mx;
int main() {
  scanf("%d%d", &V, &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &w[i], &c[i]);
    mx = max(mx, w[i]);
  }
  memset(f, 0x3f, sizeof f);
  f[0] = 0;
  for (int i = 1; i <= n; i++)
    for (int j = w[i]; j <= V + mx; j++)
      f[j] = min(f[j], f[j - w[i]] + c[i]);
  int ans = f[V];
  for (int i = V + 1; i <= V + mx; ++i) {
    ans = min(f[i], ans);
  }
  printf("%d\n", ans);
  return 0;
}
Problem p02787 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int dp[14010][10010];
int main() {
  int H, N;
  cin >> H >> N;
  int A[1010], B[1010];
  for (int i = 1; i <= N; i++) {
    cin >> A[i] >> B[i];
  }
  vector<int> P, Q;
  for (int i = 1; i <= N; i++) {
    int nowA = A[i];
    int nowB = B[i];
    P.push_back(nowA);
    Q.push_back(nowB);
    while (nowA < H) {
      nowA <<= 1;
      P.push_back(nowA);
      nowB <<= 1;
      Q.push_back(nowB);
    }
  }
  for (int i = 0; i <= P.size(); i++) {
    for (int j = 0; j <= H; j++) {
      dp[i][j] = 1e9 + 7;
    }
  }
  dp[0][0] = 0;
  for (int i = 0; i < P.size(); i++) {
    for (int j = 0; j <= H; j++) {
      if (dp[i][j] == 1e9 + 7)
        continue;
      dp[i + 1][min(j + P[i], H)] =
          min(dp[i + 1][min(j + P[i], H)], dp[i][j] + Q[i]);
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
    }
  }
  cout << dp[P.size()][H] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int dp[15010][10010];
int main() {
  int H, N;
  cin >> H >> N;
  int A[1010], B[1010];
  for (int i = 1; i <= N; i++) {
    cin >> A[i] >> B[i];
  }
  vector<int> P, Q;
  for (int i = 1; i <= N; i++) {
    int nowA = A[i];
    int nowB = B[i];
    P.push_back(nowA);
    Q.push_back(nowB);
    while (nowA < H) {
      nowA <<= 1;
      P.push_back(nowA);
      nowB <<= 1;
      Q.push_back(nowB);
    }
  }
  for (int i = 0; i <= P.size(); i++) {
    for (int j = 0; j <= H; j++) {
      dp[i][j] = 1e9 + 7;
    }
  }
  dp[0][0] = 0;
  for (int i = 0; i < P.size(); i++) {
    for (int j = 0; j <= H; j++) {
      if (dp[i][j] == 1e9 + 7)
        continue;
      dp[i + 1][min(j + P[i], H)] =
          min(dp[i + 1][min(j + P[i], H)], dp[i][j] + Q[i]);
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
    }
  }
  cout << dp[P.size()][H] << endl;
}
Problem p02787 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
#define dunk(a) cout << (a) << endl
using namespace std;
typedef long long ll;

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

int main() {
  ll h, n;
  cin >> h >> n;
  vector<ll> a(n);
  vector<ll> b(n);

  rep(i, n) cin >> a[i] >> b[i];

  ll dp[10010];
  ll INF = 1e9;
  rep(i, 10010) { dp[i] = INF; }
  dp[0] = 0;

  for (int i = 1; i <= h; i++) {
    for (int j = 0; j <= n; j++) {
      chmin(dp[i], dp[i - a[j]] + b[j]);
    }
  }
  dunk(dp[h]);
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
#define dunk(a) cout << (a) << endl
using namespace std;
typedef long long ll;

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

int main() {
  ll h, n;
  cin >> h >> n;
  vector<ll> a(n);
  vector<ll> b(n);

  rep(i, n) cin >> a[i] >> b[i];

  ll dp[10010];
  ll INF = 1e9;
  rep(i, 10010) { dp[i] = INF; }
  dp[0] = 0;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= h; j++) {
      chmin(dp[min(j + a[i], h)], dp[j] + b[i]);
    }
  }
  dunk(dp[h]);
}
Problem p02787 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;
const int INT_INF = 1 << 30;
#define MOD 1000000007LL
#define endl "\n"

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll H, N;
  cin >> H >> N;
  vll A(N), B(N);
  vvll dp(N + 1, vll(H + 1, INF));
  for (int i = 0; i < N; i++) {
    cin >> A.at(i) >> B.at(i);
  }
  for (int i = 0; i <= N; i++) {
    dp.at(i).at(0) = 0LL;
  }
  for (int i = 1; i <= H; i++) {
    dp.at(1).at(i) = (i + A.at(0) - 1) / A.at(0) * B.at(0);
  }

  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= H; j++) {
      for (int k = 0; k <= i; k++) {
        chmin(dp.at(i + 1).at(j), dp.at(k).at(j));
      }
      if (j - A.at(i) >= 0) {
        chmin(dp.at(i + 1).at(j), dp.at(i + 1).at(j - A.at(i)) + B.at(i));
      } else {
        chmin(dp.at(i + 1).at(j), B.at(i));
      }
    }
  }
  cout << dp.at(N).at(H) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;
const int INT_INF = 1 << 30;
#define MOD 1000000007LL
#define endl "\n"

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll H, N;
  cin >> H >> N;
  vll A(N), B(N);
  vvll dp(N + 1, vll(H + 1, INF));
  for (int i = 0; i < N; i++) {
    cin >> A.at(i) >> B.at(i);
  }
  for (int i = 0; i <= N; i++) {
    dp.at(i).at(0) = 0LL;
  }
  for (int i = 1; i <= H; i++) {
    dp.at(1).at(i) = (i + A.at(0) - 1) / A.at(0) * B.at(0);
  }

  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= H; j++) {
      chmin(dp.at(i + 1).at(j), dp.at(i).at(j));
      if (j - A.at(i) >= 0) {
        chmin(dp.at(i + 1).at(j), dp.at(i + 1).at(j - A.at(i)) + B.at(i));
      } else {
        chmin(dp.at(i + 1).at(j), B.at(i));
      }
    }
  }
  cout << dp.at(N).at(H) << 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);
  vector<int> B(N);

  int maxA = 0;
  for (int i = 0; i < N; i++) {
    cin >> A.at(i) >> B.at(i);
    if (A.at(i) > maxA) {
      maxA = A.at(i);
    }
  }

  cerr << "maxA " << maxA << endl;
  int l = H + maxA + 1;
  int bigM = 1000000000;
  vector<int> dp(l, bigM);

  dp[0] = 0;
  // initialize
  for (int i = 0; i < l; i++) {
    if (dp[i] == bigM)
      continue;
    for (int j = 0; j < N; j++) {
      int nd = i + A.at(j);
      int nb = dp[i] + B.at(j);
      if (nb < dp[nd]) {
        dp[nd] = nb;
      }
    }
  }

  int ans = dp[H];
  for (int i = H; i < l; i++) {
    // cerr << i << " " << dp[i] << endl;
    if (dp[i] < ans)
      ans = dp[i];
  }
  cout << ans << endl;
}


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

int main() {
  int H, N;
  cin >> H >> N;
  vector<int> A(N);
  vector<int> B(N);

  int maxA = 0;
  for (int i = 0; i < N; i++) {
    cin >> A.at(i) >> B.at(i);
    if (A.at(i) > maxA) {
      maxA = A.at(i);
    }
  }

  cerr << "maxA " << maxA << endl;
  int l = H + maxA + 1;
  int bigM = 1000000000;
  vector<int> dp(l, bigM);

  dp[0] = 0;
  // initialize
  for (int i = 0; i <= H; i++) {
    if (dp[i] == bigM)
      continue;
    for (int j = 0; j < N; j++) {
      int nd = i + A.at(j);
      int nb = dp[i] + B.at(j);
      if (nb < dp[nd]) {
        dp[nd] = nb;
      }
    }
  }

  int ans = dp[H];
  for (int i = H; i < l; i++) {
    // cerr << i << " " << dp[i] << endl;
    if (dp[i] < ans)
      ans = dp[i];
  }
  cout << ans << endl;
}
Problem p02787 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vll = vector<vl>;
using Pll = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(v) v.begin(), v.end()
#define sz(x) ((int)x.size())
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
const int MOD = 1e9 + 7;
const ll INF = 2e15;
template <class T> void print(const T &t) { cout << t << endl; }
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; }

int main() {
  ll h, n;
  cin >> h >> n;
  vl a(n + 1);
  vl b(n + 1);
  for (ll i = 1; i <= n; i++) {
    cin >> a[i] >> b[i];
  }
  ll dp[n + 1][h + 1];
  dp[0][0] = 0;
  for (ll i = 1; i <= h; i++) {
    dp[0][i] = INF;
  }
  for (ll i = 1; i <= n; i++) {
    rep(j, a[i]) {
      if (dp[i - 1][j] > b[i]) {
        dp[i][j] = b[i];
      } else {
        dp[i][j] = dp[i - 1][j];
      }
    }
    for (ll j = a[i]; j <= h; j++) {
      if (dp[i - 1][j] > dp[i][j - a[i]] + b[i]) {
        dp[i][j] = dp[i][j - a[i]] + b[i];
      } else {
        dp[i][j] = dp[i - 1][j];
      }
    }
  }
  print(dp[n][h]);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vll = vector<vl>;
using Pll = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(v) v.begin(), v.end()
#define sz(x) ((int)x.size())
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
const int MOD = 1e9 + 7;
const ll INF = 2e15;
template <class T> void print(const T &t) { cout << t << endl; }
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; }

int main() {
  ll h, n;
  cin >> h >> n;
  vl a(n + 1);
  vl b(n + 1);
  for (ll i = 1; i <= n; i++) {
    cin >> a[i] >> b[i];
  }
  ll dp[n + 1][h + 1];
  dp[0][0] = 0;
  for (ll i = 1; i <= h; i++) {
    dp[0][i] = INF;
  }
  for (ll i = 1; i <= n; i++) {
    rep(j, a[i]) {
      if (dp[i - 1][j] > b[i]) {
        dp[i][j] = b[i];
      } else {
        dp[i][j] = dp[i - 1][j];
      }
      if (j == h) {
        break;
      }
    }
    for (ll j = a[i]; j <= h; j++) {
      if (dp[i - 1][j] > dp[i][j - a[i]] + b[i]) {
        dp[i][j] = dp[i][j - a[i]] + b[i];
      } else {
        dp[i][j] = dp[i - 1][j];
      }
    }
  }
  print(dp[n][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 ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << #x << ": " << (x) << endl
#define INF (int)1e9
#define MOD ((int)1e9 + 7)
using namespace std;
typedef long long llong;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef pair<int, int> pii;
template <class Type> void line(const Type &a) {
  int cnt = 0;
  for (auto &elem : a) {
    if (cnt++)
      cout << ' ';
    cout << elem;
  }
  cout << endl;
}

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

  vi a(n), b(n);
  REP(i, n) cin >> a[i] >> b[i];

  vi dp(h + 1, INF);
  dp[h] = 0;
  for (int i = h; i > 0; --i) {
    if (dp[i] == INF) {
    } else {
      REP(j, n) {
        if (i - a[j] < 0)
          dp[0] = min(dp[0], dp[i] + b[j]);
        else
          dp[i - a[i]] = min(dp[i - a[i]], dp[i] + b[i]);
      }
    }
  }

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

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << #x << ": " << (x) << endl
#define INF (int)1e9
#define MOD ((int)1e9 + 7)
using namespace std;
typedef long long llong;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef pair<int, int> pii;
template <class Type> void line(const Type &a) {
  int cnt = 0;
  for (auto &elem : a) {
    if (cnt++)
      cout << ' ';
    cout << elem;
  }
  cout << endl;
}

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

  vi a(n), b(n);
  REP(i, n) cin >> a[i] >> b[i];

  vi dp(h + 1, INF);
  dp[h] = 0;
  for (int i = h; i > 0; --i) {
    if (dp[i] == INF) {
    } else {
      REP(j, n) {
        if (i - a[j] < 0)
          dp[0] = min(dp[0], dp[i] + b[j]);
        else
          dp[i - a[j]] = min(dp[i - a[j]], dp[i] + b[j]);
      }
    }
  }

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
using ll = long long;

struct P {
  int a, b;
};

template <class T> void chmin(T &a, T b) {
  if (b < a)
    a = b;
}

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

  vector<P> p(n);
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    p[i] = {a, b};
  }

  vector<int> dp(h + 1, 1 << 30); // 体力をj減らせる最小の魔力消費
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    int a = p[i].a;
    int b = p[i].b;
    for (int j = 0; j < a; j++) {
      chmin(dp[j], b);
    }
    for (int j = a; j <= h; j++) {
      chmin(dp[j], dp[j - a] + b);
    }
  }

  cout << dp[h] << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
using ll = long long;

struct P {
  int a, b;
};

template <class T> void chmin(T &a, T b) {
  if (b < a)
    a = b;
}

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

  vector<P> p(n);
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    p[i] = {a, b};
  }

  vector<int> dp(h + 1, 1 << 30); // 体力をj減らせる最小の魔力消費
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    int a = p[i].a;
    int b = p[i].b;
    for (int j = 0; j <= min(a, h); j++) {
      chmin(dp[j], b);
    }
    for (int j = a; j <= h; j++) {
      chmin(dp[j], dp[j - a] + b);
    }
  }

  cout << dp[h] << endl;

  return 0;
}
Problem p02787 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 0x3f3f3f;
const ll maxh = 11111;
const ll maxn = 1010;
ll a[maxn], b[maxn];
ll dp[maxh];
int main() {
  ios::sync_with_stdio(false);
  ll h, n;
  while (~scanf("%lld%lld", &h, &n)) {
    for (ll i = 0; i < n; i++)
      scanf("%lld%lld", &a[i], &b[i]);
    memset(dp, inf, sizeof(dp));
    dp[0] = 0;
    for (ll i = 0; i < n; i++)
      for (ll j = 0; j <= h; j++) {
        dp[j + a[i]] = min(dp[j + a[i]], dp[j] + b[i]);
      }
    ll minn = inf;
    for (ll i = h; i < maxh; i++) {
      minn = min(minn, dp[i]);
    }
    printf("%lld\n", minn);
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 1000000010;
const ll maxh = 5e4 + 10;
const ll maxn = 1010;
ll a[maxn], b[maxn];
ll dp[maxh];
int main() {
  ios::sync_with_stdio(false);
  ll h, n;
  while (~scanf("%lld%lld", &h, &n)) {
    for (ll i = 0; i < n; i++)
      scanf("%lld%lld", &a[i], &b[i]);
    memset(dp, inf, sizeof(dp));
    dp[0] = 0;
    for (ll i = 0; i < n; i++)
      for (ll j = 0; j <= h; j++) {
        dp[j + a[i]] = min(dp[j + a[i]], dp[j] + b[i]);
      }
    ll minn = inf;
    for (ll i = h; i < maxh; i++) {
      minn = min(minn, dp[i]);
    }
    printf("%lld\n", minn);
  }
  return 0;
}
Problem p02787 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int main() {
  int H, N;
  cin >> H >> N;
  int dp[H + 1];
  const int INF = 1 << 30;
  fill(dp, dp + H, INF);
  dp[H] = 0;
  while (N--) {
    int a, b;
    cin >> a >> b;
    for (int i = H; i >= a; i--)
      dp[i - a] = min(dp[i - a], b + dp[i]);
    for (int i = a - 1; i >= 0; i--)
      dp[0] = min(dp[0], b + dp[i]);
  }
  cout << dp[0] << endl;
}


Fixed submission:
#include <iostream>
using namespace std;
int main() {
  int H, N;
  cin >> H >> N;
  int dp[H + 1];
  const int INF = 1 << 30;
  fill(dp, dp + H, INF);
  dp[H] = 0;
  while (N--) {
    int a, b;
    cin >> a >> b;
    for (int i = H; i >= a; i--)
      dp[i - a] = min(dp[i - a], b + dp[i]);
    for (int i = min(H, a - 1); i >= 0; i--)
      dp[0] = min(dp[0], b + dp[i]);
  }
  cout << dp[0] << endl;
}
Problem p02787 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define LL long long
#define maxn 200010
#define inf 0x3f3f3f3f3f3f
using namespace std;

LL dp[10010];

LL a[1010], b[1010];

int main() {
  int n, w;
  cin >> w >> n;
  LL ma = 0;
  for (int i = 0; i < n; ++i) {
    cin >> a[i] >> b[i];
    ma = max(ma, b[i]);
  }
  for (int i = 0; i <= w + ma; ++i) {
    dp[i] = inf;
  }
  for (int i = 0; i <= w + ma; ++i) {
    if (i % a[0] == 0) {
      dp[i] = b[0] * (i / a[0]);
    }
  }
  for (int i = 1; i < n; ++i) {
    for (int j = a[i]; j <= w + ma; ++j) {
      dp[j] = min(dp[j], dp[j - a[i]] + b[i]);
    }
  }
  LL ans = inf;
  for (int i = w; i <= w + ma; ++i) {
    ans = min(ans, dp[i]);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define LL long long
#define maxn 200010
#define inf 0x3f3f3f3f3f3f
using namespace std;

LL dp[100010];

LL a[1010], b[1010];

int main() {
  int n, w;
  cin >> w >> n;
  LL ma = 0;
  for (int i = 0; i < n; ++i) {
    cin >> a[i] >> b[i];
    ma = max(ma, b[i]);
  }
  for (int i = 0; i <= w + ma; ++i) {
    dp[i] = inf;
  }
  for (int i = 0; i <= w + ma; ++i) {
    if (i % a[0] == 0) {
      dp[i] = b[0] * (i / a[0]);
    }
  }
  for (int i = 1; i < n; ++i) {
    for (int j = a[i]; j <= w + ma; ++j) {
      dp[j] = min(dp[j], dp[j - a[i]] + b[i]);
    }
  }
  LL ans = inf;
  for (int i = w; i <= w + ma; ++i) {
    ans = min(ans, dp[i]);
  }
  cout << ans << endl;
  return 0;
}
Problem p02787 (C++)

Buggy submission (status: Runtime Error):
#include <array>
#include <cstdio>

constexpr int inf = 1e9 + 334 + 810 + 1919;
constexpr int MAX_H = 1000;
std::array<int, MAX_H + 1> DP;

int readInt(void) {
  char c;
  do {
    c = getchar_unlocked();
  } while (c < '0');
  int res = 0;
  do {
    res = 10 * res + c - '0';
    c = getchar_unlocked();
  } while (c >= '0');
  return res;
}

int main(void) {
  int H, n;
  H = readInt();
  n = readInt();
  for (int i = 0; i < H; ++i)
    DP[i] = inf;
  DP[H] = 0;
  for (int _ = 0; _ < n; ++_) {
    int a, b;
    a = readInt();
    b = readInt();
    int i;
    for (i = H; i >= a; --i)
      if (DP[i - a] > DP[i] + b)
        DP[i - a] = DP[i] + b;
    for (; i; --i)
      if (DP[0] > DP[i] + b)
        DP[0] = DP[i] + b;
  }
  printf("%d\n", DP[0]);
  return 0;
}

Fixed submission:
#include <array>
#include <cstdio>

constexpr int inf = 1e9 + 334 + 810 + 1919;
constexpr int MAX_H = 1e4;
std::array<int, MAX_H + 1> DP;

int readInt(void) {
  char c;
  do {
    c = getchar_unlocked();
  } while (c < '0');
  int res = 0;
  do {
    res = 10 * res + c - '0';
    c = getchar_unlocked();
  } while (c >= '0');
  return res;
}

int main(void) {
  int H, n;
  H = readInt();
  n = readInt();
  for (int i = 0; i < H; ++i)
    DP[i] = inf;
  DP[H] = 0;
  for (int _ = 0; _ < n; ++_) {
    int a, b;
    a = readInt();
    b = readInt();
    int i;
    for (i = H; i >= a; --i)
      if (DP[i - a] > DP[i] + b)
        DP[i - a] = DP[i] + b;
    for (; i; --i)
      if (DP[0] > DP[i] + b)
        DP[0] = DP[i] + b;
  }
  printf("%d\n", DP[0]);
  return 0;
}
Problem p02787 (C++)

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

using namespace std;

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

#define FOR(i, n, m) for (ll(i) = (m); (i) < (n); ++(i))
#define REP(i, n) FOR(i, n, 0)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll)1e15;

pll A[105];
ll dp[105][10005];

int main() {
  ll H, N;
  cin >> H >> N;
  REP(i, N) { cin >> A[i].first >> A[i].second; }
  REP(i, N + 1) {
    REP(j, H + 1) { dp[i][j] = INF; }
  }
  dp[0][0] = 0;

  REP(i, N) {
    REP(j, H + 1) {
      dp[i + 1][j] = std::min(dp[i][j], dp[i + 1][j]);
      ll d = std::min(H, j + A[i].first);
      dp[i + 1][d] = std::min(dp[i + 1][d], dp[i + 1][j] + A[i].second);
    }
  }

  cout << dp[N][H] << endl;
  return 0;
}

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

using namespace std;

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

#define FOR(i, n, m) for (ll(i) = (m); (i) < (n); ++(i))
#define REP(i, n) FOR(i, n, 0)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll)1e15;

pll A[1005];
ll dp[1005][10005];

int main() {
  ll H, N;
  cin >> H >> N;
  REP(i, N) { cin >> A[i].first >> A[i].second; }
  REP(i, N + 1) {
    REP(j, H + 1) { dp[i][j] = INF; }
  }
  dp[0][0] = 0;

  REP(i, N) {
    REP(j, H + 1) {
      dp[i + 1][j] = std::min(dp[i][j], dp[i + 1][j]);
      ll d = std::min(H, j + A[i].first);
      dp[i + 1][d] = std::min(dp[i + 1][d], dp[i + 1][j] + A[i].second);
    }
  }

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repr(i, 0, n)
#define INF 2e9
// #define MOD 1000000007
#define MOD 998244353
#define LINF (long long)4e18
#define jck 3.141592

using ll = long long;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;

int dp[10010];

int main() {
  int H, N;
  cin >> H >> N;
  vector<int> A(N), B(N);
  rep(i, N) cin >> A[i] >> B[i];
  rep(i, 10010) dp[i] = INF;
  dp[0] = 0;
  rep(i, N) rep(j, H + 1) {
    if (j >= A[i])
      dp[j] = min(dp[j], dp[j - A[i]] + B[i]);
    if (j == H)
      repr(k, j - A[i], j) dp[j] = min(dp[j], dp[k] + B[i]);
  }
  cout << dp[H] << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repr(i, 0, n)
#define INF 2e9
// #define MOD 1000000007
#define MOD 998244353
#define LINF (long long)4e18
#define jck 3.141592

using ll = long long;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;

int dp[10010];

int main() {
  int H, N;
  cin >> H >> N;
  vector<int> A(N), B(N);
  rep(i, N) cin >> A[i] >> B[i];
  rep(i, 10010) dp[i] = INF;
  dp[0] = 0;
  rep(i, N) rep(j, H + 1) {
    if (j >= A[i])
      dp[j] = min(dp[j], dp[j - A[i]] + B[i]);
    if (j == H)
      repr(k, max(0, j - A[i]), j) dp[j] = min(dp[j], dp[k] + B[i]);
  }
  cout << dp[H] << endl;
}
Problem p02787 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <deque>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
using ll = long long;

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<ll>> dp(n + 1, vector<ll>(h + 1, 1e18));
  dp[0][0] = 0;
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j < a[i - 1]; ++j) {
      dp[i][j] = min(dp[i][j], dp[i - 1][j]);
      dp[i][j] = min(dp[i][j], dp[i - 1][0] + b[i - 1]);
    }
    for (int j = a[i - 1]; j <= h; ++j) {
      dp[i][j] = min(dp[i][j], dp[i - 1][j]);
      dp[i][j] = min(dp[i][j], dp[i][j - a[i - 1]] + b[i - 1]);
    }
  }
  cout << dp[n][h] << "\n";
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <deque>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
using ll = long long;

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<ll>> dp(n + 1, vector<ll>(h + 1, 1e18));
  dp[0][0] = 0;
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j < min(h + 1, a[i - 1]); ++j) {
      dp[i][j] = min(dp[i][j], dp[i - 1][j]);
      dp[i][j] = min(dp[i][j], dp[i - 1][0] + b[i - 1]);
    }
    for (int j = a[i - 1]; j <= h; ++j) {
      dp[i][j] = min(dp[i][j], dp[i - 1][j]);
      dp[i][j] = min(dp[i][j], dp[i][j - a[i - 1]] + b[i - 1]);
    }
  }
  cout << dp[n][h] << "\n";
  return 0;
}
Problem p02787 (C++)

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

const int INF = 1001001001;

int main() {
  int h, n;
  cin >> h >> n;
  vector<vector<int>> ab(n, vector<int>(2, 0));

  for (auto &v : ab) {
    cin >> v[0] >> v[1];
  }

  vector<vector<int>> dp(n + 1, vector<int>(h + 1, INF));
  rep(j, h + 1) { dp[0][j] = ((j + ab[0][0] - 1) / ab[0][0]) * ab[0][1]; }

  rep(i, n) {
    rep(j, h + 1) {
      int t = j / ab[i][0];
      rep(k, t + 2) {
        if (i == 0)
          continue;
        dp[i][j] = min(min(dp[min(i - 1, n)][j],
                           dp[i][max(j - k * ab[i][0], 0)] + k * ab[i][1]),
                       dp[i][j]);
      }
    }
  }

  cout << dp[n - 1][h] << endl;
  return 0;
}


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

const int INF = 1001001001;

int main() {
  int h, n;
  cin >> h >> n;
  vector<vector<int>> ab(n, vector<int>(2, 0));

  for (auto &v : ab) {
    cin >> v[0] >> v[1];
  }

  vector<vector<int>> dp(n + 1, vector<int>(h + 1, INF));
  rep(j, h + 1) { dp[0][j] = ((j + ab[0][0] - 1) / ab[0][0]) * ab[0][1]; }

  rep(i, n) {
    rep(j, h + 1) {
      if (i == 0)
        continue;
      dp[i][j] =
          min(min(dp[min(i - 1, n)][j], dp[i][max(j - ab[i][0], 0)] + ab[i][1]),
              dp[i][j]);
    }
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long hxtype;
typedef pair<int, int> pii;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
#define pque priority_queue
#define rep(i, l, r) for (i = (l); i <= (r); ++i)
#define per(i, l, r) for (i = (l); i >= (r); --i)
#define REP(i, l, r) for (i = (l); i < (r); ++i)
#define PER(i, l, r) for (i = (l); i > (r); --i)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
template <class IT> inline void cmax(IT &a, IT b) {
  if (a < b)
    a = b;
}
template <class IT> inline void cmin(IT &a, IT b) {
  if (b < a)
    a = b;
}
const int N = 10005, inf = 1000000000;
int dp[N];
int main() {
  int h, n, a, b, i, ans = inf;
  scanf("%d%d", &h, &n);
  REP(i, 1, h) dp[i] = inf;
  while (n--) {
    scanf("%d%d", &a, &b);
    REP(i, a, h) cmin(dp[i], dp[i - a] + b);
    REP(i, h - a, h) cmin(ans, dp[i] + b);
    //		REP(i, 0 ,h)printf("%d ",dp[i]);printf("ans=%d\n",ans);
  }
  printf("%d", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long hxtype;
typedef pair<int, int> pii;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
#define pque priority_queue
#define rep(i, l, r) for (i = (l); i <= (r); ++i)
#define per(i, l, r) for (i = (l); i >= (r); --i)
#define REP(i, l, r) for (i = (l); i < (r); ++i)
#define PER(i, l, r) for (i = (l); i > (r); --i)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
template <class IT> inline void cmax(IT &a, IT b) {
  if (a < b)
    a = b;
}
template <class IT> inline void cmin(IT &a, IT b) {
  if (b < a)
    a = b;
}
const int N = 10005, inf = 1000000000;
int dp[N];
int main() {
  int h, n, a, b, i, ans = inf;
  scanf("%d%d", &h, &n);
  REP(i, 1, h) dp[i] = inf;
  while (n--) {
    scanf("%d%d", &a, &b);
    REP(i, a, h) cmin(dp[i], dp[i - a] + b);
    REP(i, max(h - a, 0), h) cmin(ans, dp[i] + b);
    //		REP(i, 0 ,h)printf("%d ",dp[i]);printf("ans=%d\n",ans);
  }
  printf("%d", ans);
  return 0;
}
Problem p02787 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <type_traits>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, int> P;
#define rep(i, N) for (ll(i) = 0; (i) < (N); (i)++)
const int mod = 1000000007;
const int INF = 1001001001;

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

Fixed submission:
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <type_traits>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, int> P;
#define rep(i, N) for (ll(i) = 0; (i) < (N); (i)++)
const int mod = 1000000007;
const int INF = 1001001001;

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

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

using namespace std;

struct magic {
  int a, b;
};

int main() {
  int h, n;
  const int h_max = 1e9;
  cin >> h >> n;

  vector<magic> v(n);
  for (auto &vi : v)
    cin >> vi.a >> vi.b;
  vector<int> mpv(h + 1, h_max);
  mpv.at(0) = 0;
  for (const auto &vi : v) {
    for (int j = vi.a; j < h; j++)
      mpv.at(j) = min(mpv.at(j - vi.a) + vi.b, mpv.at(j));
    for (int j = 0; j < vi.a; j++)
      mpv.at(h) = min(mpv.at(h - vi.a + j) + vi.b, mpv.at(h));
  }

  cout << mpv.at(h) << endl;
  return 0;
}

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

using namespace std;

struct magic {
  int a, b;
};

int main() {
  int h, n;
  const int h_max = 1e9;
  cin >> h >> n;

  vector<magic> v(n);
  for (auto &vi : v)
    cin >> vi.a >> vi.b;
  vector<int> mpv(h + 1, h_max);
  mpv.at(0) = 0;
  for (const auto &vi : v) {
    for (int j = vi.a; j < h; j++)
      mpv.at(j) = min(mpv.at(j - vi.a) + vi.b, mpv.at(j));
    // for(int j=0; j<vi.a; j++) mpv.at(h) = min(mpv.at(h-vi.a+j)+vi.b,
    // mpv.at(h)); // ここにバグが入るのんだろう。
    for (int j = max(h - vi.a, 0); j < h; j++)
      mpv.at(h) = min(mpv.at(j) + vi.b, mpv.at(h));
  }

  cout << mpv.at(h) << endl;
  return 0;
}