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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define dup(x, y) (((x) + (y)-1) / (y)) // dup * y >= x なる最小のdup.
using namespace std;
typedef long long ll;
using Graph = vector<vector<int>>;

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

  vector<vector<int>> dp(
      N, vector<int>(
             H + 1,
             0)); // dp[i][j] : 最初に魔法i, ダメージj を与える最小の消費魔力

  vector<ll> dp2(H, 0);

  // dp[i][0] = 0;
  for (int j = 1; j < H + 1; j++) {
    for (int i = 0; i < N; i++) {
      if (A[i] >= j) {
        dp[i][j] = B[i];
      } else { // A[i] < j;
        // ll tmp = dp[0][j-A[i]];
        // for(int k = 1; k<N; k++) {
        // 	if(tmp > dp[k][j-A[i]]) tmp = dp[k][j-A[i]];
        // }
        dp[i][j] = B[i] + dp2[j - A[i]];
      }
    }

    dp2[j] = dp[0][j];
    for (int k = 1; k < N; k++) {
      if (dp2[j] > dp[k][j])
        dp2[j] = dp[k][j];
    }
  }

  // for(int i=0; i<N; i++) {
  // 	for(int j=0; j<H+1; j++) {
  // 		cout << dp[i][j] << ' ';
  // 	}
  // 	cout << endl;
  // }

  ll ans = dp[0][H];
  rep(i, N) {
    if (ans > dp[i][H])
      ans = dp[i][H];
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define dup(x, y) (((x) + (y)-1) / (y)) // dup * y >= x なる最小のdup.
using namespace std;
typedef long long ll;
using Graph = vector<vector<int>>;

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

  vector<vector<int>> dp(
      N, vector<int>(
             H + 1,
             0)); // dp[i][j] : 最初に魔法i, ダメージj を与える最小の消費魔力

  vector<int> dp2(H, 0);

  // dp[i][0] = 0;
  for (int j = 1; j < H + 1; j++) {
    for (int i = 0; i < N; i++) {
      if (A[i] >= j) {
        dp[i][j] = B[i];
      } else { // A[i] < j;
        // ll tmp = dp[0][j-A[i]];
        // for(int k = 1; k<N; k++) {
        // 	if(tmp > dp[k][j-A[i]]) tmp = dp[k][j-A[i]];
        // }
        dp[i][j] = B[i] + dp2[j - A[i]];
      }
    }

    dp2[j] = dp[0][j];
    for (int k = 1; k < N; k++) {
      if (dp2[j] > dp[k][j])
        dp2[j] = dp[k][j];
    }
  }

  // for(int i=0; i<N; i++) {
  // 	for(int j=0; j<H+1; j++) {
  // 		cout << dp[i][j] << ' ';
  // 	}
  // 	cout << endl;
  // }

  ll ans = dp[0][H];
  rep(i, N) {
    if (ans > dp[i][H])
      ans = dp[i][H];
  }

  cout << ans << endl;

  return 0;
}
Problem p02787 (C++)

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

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

const int INF = 100100100;

int main() {
  int h, n;
  cin >> h >> n;
  int m = 2 * h + 100;
  vector<int> hs(m, INF);
  vector<P> ab(n);
  rep(i, n) {
    int a, b;
    cin >> a >> b;
    ab[i] = P(a, b);
  }
  hs[0] = 0;
  for (int i = 0; i <= h; i++) {
    rep(j, n) {
      int a = ab[j].first;
      int b = ab[j].second;
      hs[i + a] = min(hs[i + a], hs[i] + b);
    }
  }

  int ans = INF;
  for (int i = h; i < m; i++) {
    ans = min(hs[i], ans);
  }
  cout << ans << endl;
  return 0;
}


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

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

const int INF = 100100100;

int main() {
  int h, n;
  cin >> h >> n;
  int m = 1e8 + 10;
  vector<int> hs(m, INF);
  vector<P> ab(n);
  rep(i, n) {
    int a, b;
    cin >> a >> b;
    ab[i] = P(a, b);
  }
  hs[0] = 0;
  for (int i = 0; i <= h; i++) {
    rep(j, n) {
      int a = ab[j].first;
      int b = ab[j].second;
      hs[i + a] = min(hs[i + a], hs[i] + b);
    }
  }

  int ans = INF;
  for (int i = h; i < m; i++) {
    ans = min(hs[i], ans);
  }
  cout << ans << endl;
  return 0;
}
Problem p02787 (C++)

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

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define FILL0(x) memset(x, 0, sizeof(x))

using namespace std;

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

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

ll dp[10010];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);

  int H, N;
  cin >> H >> N;

  vector<P> ab(N);

  rep(i, N) { cin >> ab[i].first >> ab[i].second; }

  rep(i, 10010) { dp[i] = ll(1e8); }

  dp[0] = 0;

  rep(i, N) {
    rep(j, H + 1) {
      if (j - ab[i].second >= 0) // ピッタリ
      {
        chmin(dp[j], ab[i].second + dp[j - ab[i].first]);
      } else // 以上
      {
        chmin(dp[j], ab[i].second);
      }
    }
  }

  cout << dp[H] << endl;

  return 0;
}

// cout << setprecision(13);
// next_permutation();
//__gcd();
// reverse();
// set ,tuple ,pair;
// bitset
// vector.find


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

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define FILL0(x) memset(x, 0, sizeof(x))

using namespace std;

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

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

ll dp[10010];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);

  int H, N;
  cin >> H >> N;

  vector<P> ab(N);

  rep(i, N) { cin >> ab[i].first >> ab[i].second; }

  rep(i, 10010) { dp[i] = ll(1e8); }

  dp[0] = 0;

  rep(i, N) {
    rep(j, H + 1) {
      if (j - ab[i].first >= 0) // ピッタリ
      {
        chmin(dp[j], ab[i].second + dp[j - ab[i].first]);
      } else // 以上
      {
        chmin(dp[j], ab[i].second);
      }
    }
  }

  cout << dp[H] << endl;

  return 0;
}

// cout << setprecision(13);
// next_permutation();
//__gcd();
// reverse();
// set ,tuple ,pair;
// bitset
// vector.find
Problem p02787 (C++)

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

const int INF = 1 << 29;
int dp[10005]; // dp[j]:=体力jを消費するまでの魔力の最小値.

int main() {
  int H, N;
  int a[1000], b[1000];

  cin >> H >> N;
  for (int i = 0; i < N; i++)
    cin >> a[i] >> b[i];

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

  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= H; j++) {
      dp[j] = min(dp[j], dp[j - a[i]] + b[i]);
    }
  }
  cout << dp[H] << endl;
}


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

const int INF = 1 << 29;
int dp[10005]; // dp[j]:=体力jを消費するまでの魔力の最小値.

int main() {
  int H, N;
  int a[1000], b[1000];

  cin >> H >> N;
  for (int i = 0; i < N; i++)
    cin >> a[i] >> b[i];

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

  for (int i = 0; i < N; i++) {
    for (int j = 1; j <= H; j++) {
      if (j <= a[i])
        dp[j] = min(dp[j], b[i]);
      else
        dp[j] = min(dp[j], dp[j - a[i]] + b[i]);
    }
  }
  cout << dp[H] << endl;
}
Problem p02787 (Python)

Buggy submission (status: Time Limit Exceeded):
h, n = map(int, input().split())
x = [list(map(int, input().split())) for _ in range(n)]
a, b = x[0]
dp = [-(-i // a) * b for i in range(h + 1)]
for j in range(1, n):
    a, b = x[j]
    for i in range(h + 1):
        if i >= a:
            dp[i] = min(dp[i], dp[i - a] + b)
        else:
            dp[i] = min(dp[i], b)
print(dp[-1])


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

Buggy submission (status: Time Limit Exceeded):
H, N = map(int, input().split())
M = [tuple(map(int, input().split())) for i in range(N)]

dp = [(i + M[0][0] - 1) // M[0][0] * M[0][1] for i in range(H + 1)]
for i in range(N):
    Ai, Bi = M[i]
    ndp = [0] * (H + 1)
    if Ai >= H:
        ndp[0 : H + 1] = [min(dp[j], Bi) for j in range(H + 1)]
    else:
        ndp[0 : Ai + 1] = [min(dp[j], Bi) for j in range(Ai + 1)]
        for j in range(Ai + 1, H + 1):
            ndp[j] = min(dp[j], ndp[j - Ai] + Bi)
    dp = ndp
print(dp[H])


Fixed submission:
H, N = map(int, input().split())
M = [tuple(map(int, input().split())) for i in range(N)]

dp = [(j + M[0][0] - 1) // M[0][0] * M[0][1] for j in range(H + 1)]
for i in range(1, N):
    Ai, Bi = M[i]
    ndp = [0] * (H + 1)
    if Ai >= H:
        ndp[0 : H + 1] = [min(dp[j], Bi) for j in range(H + 1)]
    else:
        ndp[0 : Ai + 1] = [min(dp[j], Bi) for j in range(Ai + 1)]
        for j in range(Ai + 1, H + 1):
            ndp[j] = min(dp[j], ndp[j - Ai] + Bi)
    dp = ndp
print(dp[H])
Problem p02787 (Python)

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

H, N = map(int, input().split())
A = [0] * N
B = [0] * N

for i in range(N):
    a, b = map(int, sys.stdin.readline().rstrip().split())
    A[i] = a
    B[i] = b

max_A = max(A)
INF = 10**18
dp = [INF] * (H + max_A)
dp[0] = 0

for i in range(0, H):
    for j in range(N):
        dp[i + A[j]] = min(dp[i] + B[j], dp[i + A[j]])

print(min(dp[H : H + max_A]))


Fixed submission:
import sys

H, N = map(int, input().split())
A = [0] * N
B = [0] * N

for i in range(N):
    a, b = map(int, sys.stdin.readline().rstrip().split())
    A[i] = a
    B[i] = b

max_A = max(A)
INF = 10**18
dp = [INF] * (H + max_A)
dp[0] = 0

for i in range(H):
    for j in range(N):
        dp[i + A[j]] = min(dp[i] + B[j], dp[i + A[j]])

print(min(dp[H : H + max_A]))
Problem p02787 (C++)

Buggy submission (status: Runtime Error):
// #include<bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define mod 1000000007
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;

char mp[110][110][110];
int vis[110][110][110];
int dir[6][3] = {-1, 0, 0, 1, 0, 0, 0, 1, 0, 0, -1, 0, 0, 0, 1, 0, 0, -1};

int arr[1000000];
int brr[1000000];
int f[1000000] = {0};

int main() {

  int h, n;
  cin >> h >> n;
  for (int i = 0; i <= h; i++) {
    f[i] = 2e9;
  }
  for (int i = 0; i < n; i++) {
    cin >> arr[i] >> brr[i];
  }
  f[0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= h; j++) {
      int nj = min(j + arr[i], h);
      f[nj] = min(f[nj], f[nj - arr[i]] + brr[i]);
      //	cout<<f[j]<<endl;
    }
  }
  cout << f[h] << endl;
}


Fixed submission:
// #include<bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define mod 1000000007
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;

char mp[110][110][110];
int vis[110][110][110];
int dir[6][3] = {-1, 0, 0, 1, 0, 0, 0, 1, 0, 0, -1, 0, 0, 0, 1, 0, 0, -1};

int arr[1000000];
int brr[1000000];
int f[1000000] = {0};

int main() {

  int h, n;
  cin >> h >> n;
  for (int i = 0; i <= h; i++) {
    f[i] = 2e9;
  }
  for (int i = 0; i < n; i++) {
    cin >> arr[i] >> brr[i];
  }
  f[0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= h; j++) {
      int nj = min(j + arr[i], h);
      f[nj] = min(f[nj], f[j] + brr[i]);
      //	cout<<f[nj]<<endl;
    }
  }
  cout << f[h] << endl;
}
Problem p02787 (C++)

Buggy submission (status: Runtime Error):
// 5/6
// 個数制限なしナップサック問題

#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>;
using Graph = vector<vector<int>>;

ll dp[110][10010];
// const int INF = 1000000000;

int main() {
  int H, N;
  cin >> H >> N;

  vector<ll> A(N), B(N);
  rep(i, N) cin >> A[i] >> B[i];

  // dp初期化
  rep(i, H + 1) dp[0][i] = 10000000000;

  for (ll i = 0; i < N; i++) {
    for (ll j = 0; j <= H; j++) {
      if (j <= A[i]) {
        dp[i + 1][j] = min(B[i], dp[i][j]);
      } else {
        dp[i + 1][j] = min(dp[i + 1][j - A[i]] + B[i], dp[i][j]);
      }
    }
  }

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

Fixed submission:
// 5/6
// 個数制限なしナップサック問題

#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>;
using Graph = vector<vector<int>>;

ll dp[1010][10010];
// const int INF = 1000000000;

int main() {
  int H, N;
  cin >> H >> N;

  vector<ll> A(N), B(N);
  rep(i, N) cin >> A[i] >> B[i];

  // dp初期化
  rep(i, H + 1) dp[0][i] = 10000000000;

  for (ll i = 0; i < N; i++) {
    for (ll j = 0; j <= H; j++) {
      if (j <= A[i]) {
        dp[i + 1][j] = min(B[i], dp[i][j]);
      } else {
        dp[i + 1][j] = min(dp[i + 1][j - A[i]] + B[i], dp[i][j]);
      }
    }
  }

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

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

using namespace std;

int main() {

  long long H, N, ans = 10000000000;
  cin >> H >> N;
  vector<long long> A(N), B(N), cost(30000, 10000000000);
  for (size_t i = 0; i < N; i++) {
    cin >> A.at(i) >> B.at(i);
  }
  cost.at(0) = 0;
  for (size_t i = 0; i < N; i++) {
    cost.at(A.at(i)) = B.at(i);
  }
  for (size_t i = 0; i < 30000; i++) {
    for (size_t j = 0; j < N; j++) {
      if (i - A.at(j) >= 0) {
        cost.at(i) = min(cost.at(i - A.at(j)) + B.at(j), cost.at(i));
      }
    }
  }
  for (size_t i = H; i < 30000; i++) {
    ans = min(ans, cost.at(i));
  }
  cout << ans;
}


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

using namespace std;

int main() {

  long long H, N, ans = 10000000000;
  cin >> H >> N;
  vector<long long> A(N), B(N), cost(30000, 10000000000);
  for (size_t i = 0; i < N; i++) {
    cin >> A.at(i) >> B.at(i);
  }
  cost.at(0) = 0;
  for (size_t i = 0; i < N; i++) {
    cost.at(A.at(i)) = B.at(i);
  }
  for (size_t i = 0; i < 30000; i++) {
    for (size_t j = 0; j < N; j++) {
      if (i >= A.at(j)) {
        cost.at(i) = min(cost.at(i - A.at(j)) + B.at(j), cost.at(i));
      }
    }
  }
  for (size_t i = H; i < 30000; i++) {
    ans = min(ans, cost.at(i));
  }
  cout << ans;
}
Problem p02787 (C++)

Buggy submission (status: Time Limit Exceeded):
#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;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag,
         tree_order_statistics_node_update>; // greater<T> for sorting
                                             // decreasingly

#define ll long long
#define pll pair<ll, ll>
#define unique(a) a.resize(unique(a.begin(), a.end()) - a.begin())
#define fill(v, p) memset(v, p, sizeof v)
#define all(x) x.begin(), x.end()
#define at_(x, i) x.find_by_order(i)
#define koto_(x, i) x.order_of_key(i)
#define PI acos(-1.0)
#define EPS 0.000000001
#define INF 1LL << 62
#define MAX 300005
#define MOD 1000000007

map<ll, ll> vis[1005], dp[1005];

ll a[MAX], b[MAX], h, n;

ll rec(ll pos, ll health) {
  if (pos >= n) {
    if (health <= 0)
      return 0;
    return INT_MAX;
  }

  if (health <= 0)
    return 0;

  if (vis[pos][health])
    return dp[pos][health];

  ll p = b[pos] + rec(pos, health - a[pos]);
  ll q = rec(pos + 1, health);

  if (health < 0)
    health = 0;

  vis[pos][health] = 1;

  return dp[pos][health] = min(p, q);
}

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

  cin >> h >> n;

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

  cout << rec(0, h) << 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 std;
using namespace __gnu_pbds;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag,
         tree_order_statistics_node_update>; // greater<T> for sorting
                                             // decreasingly

#define ll long long
#define pll pair<ll, ll>
#define unique(a) a.resize(unique(a.begin(), a.end()) - a.begin())
#define fill(v, p) memset(v, p, sizeof v)
#define all(x) x.begin(), x.end()
#define at_(x, i) x.find_by_order(i)
#define koto_(x, i) x.order_of_key(i)
#define PI acos(-1.0)
#define EPS 0.000000001
#define INF 1LL << 62
#define MAX 300005
#define MOD 1000000007

ll vis[1005][10005], dp[1005][10005];

ll a[MAX], b[MAX], h, n;

ll rec(ll pos, ll health) {
  if (pos >= n) {
    if (health <= 0)
      return 0;
    return INT_MAX;
  }

  if (health <= 0)
    return 0;

  if (vis[pos][health])
    return dp[pos][health];

  ll p = b[pos] + rec(pos, health - a[pos]);
  ll q = rec(pos + 1, health);

  if (health < 0)
    health = 0;

  vis[pos][health] = 1;

  return dp[pos][health] = min(p, q);
}

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

  cin >> h >> n;

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

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

  return 0;
}
Problem p02787 (C++)

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

#define rep(i, a, b) for (int i = a; i < b; i++)
#define repd(i, a, b) for (int i = a; i >= b; i--)
#define fore(i, a) for (auto &i : a)
#define all(x) (x).begin(), (x).end()
using namespace std;
void _main();
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  _main();
}
typedef long long ll;
const int inf = INT_MAX / 2;
const ll infl = 1LL << 60;
const ll MOD = 1000000007;
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;
}

//---------------------------------------------------------------------------------------------------
void _main() {
  ll H, N;
  cin >> H >> N;
  vector<vector<ll>> dp(N + 1, vector<ll>(10000 * 1000 + 1, infl));
  dp[0][0] = 0;
  rep(i, 0, N) {
    ll a, b;
    cin >> a >> b;
    rep(j, 0, H + 1) {
      if (j > a)
        dp[i + 1][j] =
            min(dp[i][j], min(dp[i][j - a] + b, dp[i + 1][j - a] + b));
      else
        dp[i + 1][j] = min(dp[i][j], dp[i][0] + b);
    }
  }
  cout << dp[N][H] << endl;
}

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

#define rep(i, a, b) for (int i = a; i < b; i++)
#define repd(i, a, b) for (int i = a; i >= b; i--)
#define fore(i, a) for (auto &i : a)
#define all(x) (x).begin(), (x).end()
using namespace std;
void _main();
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  _main();
}
typedef long long ll;
const int inf = INT_MAX / 2;
const ll infl = 1LL << 60;
const ll MOD = 1000000007;
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;
}

//---------------------------------------------------------------------------------------------------
void _main() {
  ll H, N;
  cin >> H >> N;
  vector<vector<ll>> dp(N + 1, vector<ll>(H + 1, infl));
  dp[0][0] = 0;
  rep(i, 0, N) {
    ll a, b;
    cin >> a >> b;
    rep(j, 0, H + 1) {
      if (j > a)
        dp[i + 1][j] =
            min(dp[i][j], min(dp[i][j - a] + b, dp[i + 1][j - a] + b));
      else
        dp[i + 1][j] = min(dp[i][j], dp[i][0] + b);
    }
  }
  cout << dp[N][H] << endl;
}
Problem p02787 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int main() {
  int h, n;
  cin >> h >> n;
  vector<int> a, b;
  for (int i = 0; i < n; i++) {
    int ai, bi;
    cin >> ai >> bi;
    a.push_back(ai);
    b.push_back(bi);
  }
  vector<vector<int>> dp(1001, vector<int>(10001, INF));
  dp[0][0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= h; j++) {
      if (dp[i][j] > dp[i - 1][j])
        dp[i][j] = dp[i - 1][j];
      if (j + a[i - 1] > h && dp[i][h] > dp[i][j] + b[i - 1])
        dp[i][h] = dp[i][j] + b[i - 1];
      else if (dp[i][j + a[i - 1]] > dp[i][j] + b[i - 1])
        dp[i][j + a[i - 1]] = dp[i][j] + b[i - 1];
    }
  }
  cout << dp[n][h];
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int main() {
  int h, n;
  cin >> h >> n;
  vector<int> a, b;
  for (int i = 0; i < n; i++) {
    int ai, bi;
    cin >> ai >> bi;
    a.push_back(ai);
    b.push_back(bi);
  }
  vector<vector<int>> dp(2000, vector<int>(20000, INF));
  dp[0][0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= h; j++) {
      if (dp[i][j] > dp[i - 1][j])
        dp[i][j] = dp[i - 1][j];
      if (j + a[i - 1] > h && dp[i][h] > dp[i][j] + b[i - 1])
        dp[i][h] = dp[i][j] + b[i - 1];
      else if (dp[i][j + a[i - 1]] > dp[i][j] + b[i - 1])
        dp[i][j + a[i - 1]] = dp[i][j] + b[i - 1];
    }
  }
  cout << dp[n][h];
  return 0;
}
Problem p02787 (C++)

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

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

  vector<int> dp(h + 1, 1000000000);
  for (int i = 0; i < n; ++i) {
    int a, b;
    cin >> a >> b;

    for (int j = 1; j <= a; ++j) {
      dp[j] = min(dp[j], b);
    }
    for (int j = a + 1; j <= h; ++j) {
      dp[j] = min(dp[j], dp[j - a] + b);
    }
  }

  cout << dp[h] << endl;

  return 0;
}

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

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

  vector<int> dp(h + 1, 1000000000);
  for (int i = 0; i < n; ++i) {
    int a, b;
    cin >> a >> b;

    for (int j = 1; j <= a; ++j) {
      if (h < j) {
        break;
      }
      dp[j] = min(dp[j], b);
    }
    for (int j = a + 1; j <= h; ++j) {
      dp[j] = min(dp[j], dp[j - a] + b);
    }
  }

  cout << dp[h] << endl;

  return 0;
}
Problem p02788 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1e5 + 10, M = N * 400, mod = 1e9 + 7;
int n;
struct node {
  LL x, h;
  bool operator<(const node t) const { return x < t.x; }
} s[N];
LL a, d;
LL p[N]; // 前缀
int main() {
  cin >> n >> d >> a;
  for (int i = 1; i <= n; i++) {
    scanf("%lld%lld", &s[i].x, &s[i].h);
  }
  sort(s + 1, s + 1 + n);
  int r = 0;
  LL ans = 0;
  for (int l = 1; l <= n; l++) {

    p[l] += p[l - 1];
    s[l].h += p[l];
    // cout<<s[l].x<<' '<<p[l]<<' '<<s[l].h<<' ';
    while (r < n && s[r + 1].x <= s[l].x + 2 * d)
      r++;
    if (s[l].h < 0)
      s[l].h = 0;
    LL k = s[l].h / a + (s[l].h % a > 0);
    ans += k;
    p[l] -= k * a;
    p[r + 1] += k * a;
    // cout<<l<<' '<<r<<' '<<ans<<' '<<p[l]<<endl;
  }
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 10, M = N * 400, mod = 1e9 + 7;
int n;
struct node {
  LL x, h;
  bool operator<(const node t) const { return x < t.x; }
} s[N];
LL a, d;
LL p[N]; // 前缀
int main() {
  cin >> n >> d >> a;
  for (int i = 1; i <= n; i++) {
    scanf("%lld%lld", &s[i].x, &s[i].h);
  }
  sort(s + 1, s + 1 + n);
  int r = 0;
  LL ans = 0;
  for (int l = 1; l <= n; l++) {

    p[l] += p[l - 1];
    s[l].h += p[l];
    // cout<<s[l].x<<' '<<p[l]<<' '<<s[l].h<<' ';
    while (r < n && s[r + 1].x <= s[l].x + 2 * d)
      r++;
    if (s[l].h < 0)
      s[l].h = 0;
    LL k = s[l].h / a + (s[l].h % a > 0);
    ans += k;
    p[l] -= k * a;
    p[r + 1] += k * a;
    // cout<<l<<' '<<r<<' '<<ans<<' '<<p[l]<<endl;
  }
  cout << ans;
  return 0;
}
Problem p02788 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#define int long long
#define mod 1000000007
#define for0(i, n) for (int i = 0; i < (n); i++)
#define for1(i, n) for (int i = 1; i <= (n); i++)
#define mp make_pair
using namespace std;
int get() {
  int r;
  cin >> r;
  return r;
}
int n, d, a;
pair<int, int> xh[123456];
int x[123456], k[123456];
signed main() {
  cin >> n >> d >> a;
  for1(i, n) cin >> xh[i].first >> xh[i].second;
  sort(xh + 1, xh + n + 1);
  x[0] = -1;
  for1(i, n) {
    if (i > 1) {
      int r = 0, l = i - 1;
      if (x[l] < xh[i].first)
        ;
      else {
        while (l - r > 1) {
          int mid = (r + l) / 2;
          if (x[mid] < xh[i].first)
            r = mid;
          else
            l = mid;
        }
        xh[i].second -= a * (k[i - 1] - k[r]);
      }
    }
    xh[i].second = max(xh[i].second, (int)0);
    int p = (xh[i].second + a - 1) / a;
    x[i] = xh[i].first + 2 * d;
    k[i] = k[i - 1] + p;
  }
  cout << k[n] << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#define int long long
#define mod 1000000007
#define for0(i, n) for (int i = 0; i < (n); i++)
#define for1(i, n) for (int i = 1; i <= (n); i++)
#define mp make_pair
using namespace std;
int get() {
  int r;
  cin >> r;
  return r;
}
int n, d, a;
pair<int, int> xh[234567];
int x[234567], k[234567];
signed main() {
  cin >> n >> d >> a;
  for1(i, n) cin >> xh[i].first >> xh[i].second;
  sort(xh + 1, xh + n + 1);
  x[0] = -1;
  for1(i, n) {
    if (i > 1) {
      int r = 0, l = i - 1;
      if (x[l] < xh[i].first)
        ;
      else {
        while (l - r > 1) {
          int mid = (r + l) / 2;
          if (x[mid] < xh[i].first)
            r = mid;
          else
            l = mid;
        }
        xh[i].second -= a * (k[i - 1] - k[r]);
      }
    }
    xh[i].second = max(xh[i].second, (int)0);
    int p = (xh[i].second + a - 1) / a;
    x[i] = xh[i].first + 2 * d;
    k[i] = k[i - 1] + p;
  }
  cout << k[n] << endl;
}
Problem p02788 (C++)

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

using namespace std;
typedef long long ll;

int main() {
  int N, D, A;
  cin >> N >> D >> A;
  vector<pair<int, int>> v(N + 1);
  v[N] = make_pair(1000000010, 0);
  for (int i = 0; i < N; i++) {
    cin >> v[i].first >> v[i].second;
    v[i].second = (v[i].second + A - 1) / A;
  }

  sort(v.begin(), v.end());

  vector<int> d(N, 0);

  int cur = 0;
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    cur -= d[i];
    // cout << v[i].first << " " << cur;
    if (cur < v[i].second) {
      ans += v[i].second - cur;
      int n = distance(v.begin(),
                       upper_bound(v.begin(), v.end(),
                                   make_pair(v[i].first + 2 * D + 1, 0)));
      d[n] += v[i].second - cur;
      cur += v[i].second - cur;
    }
    // cout << " " << ans << endl;
  }

  cout << ans << endl;

  return 0;
}


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

using namespace std;
typedef long long ll;

int main() {
  int N, D, A;
  cin >> N >> D >> A;
  vector<pair<int, int>> v(N + 1);
  v[N] = make_pair(1000000010, 0);
  for (int i = 0; i < N; i++) {
    cin >> v[i].first >> v[i].second;
    v[i].second = (v[i].second + A - 1) / A;
  }

  sort(v.begin(), v.end());

  vector<int> d(N + 1, 0);

  int cur = 0;
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    cur -= d[i];
    // cout << v[i].first << " " << cur;
    if (cur < v[i].second) {
      ans += v[i].second - cur;
      int n = distance(v.begin(),
                       upper_bound(v.begin(), v.end(),
                                   make_pair(v[i].first + 2 * D + 1, 0)));
      d[n] += v[i].second - cur;
      cur += v[i].second - cur;
    }
    // cout << " " << ans << endl;
  }

  cout << ans << endl;

  return 0;
}
Problem p02788 (C++)

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

int main() {
  long N, D, A;
  cin >> N >> D >> A;
  vector<pair<long, long>> monster;
  vector<long> damage(N + 1, 0);
  for (int i = 0; i < N; i++) {
    long x, h;
    cin >> x >> h;
    monster.push_back(make_pair(x, h));
  }
  long count = 0;
  sort(monster.begin(), monster.end());
  for (int i = 0; i < monster.size(); i++) {
    if (damage[i] < monster[i].second) {
      long pos;
      for (int j = i; j < monster.size(); j++) {
        if (monster[j].first > monster[i].first + 2 * D) {
          pos = j;
          break;
        } else if (j == monster.size() - 1) {
          pos = j + 1;
        }
      }
      long d = ((monster[i].second - damage[i]) + A - 1) / A;
      damage[i] += d * A;
      damage[pos] -= d * A;
      count += d;
    }
    damage[i + 1] += damage[i];
  }
  cout << count << endl;
}


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

int main() {
  long N, D, A;
  cin >> N >> D >> A;
  vector<pair<long, long>> monster;
  vector<long> damage(N + 1, 0);
  for (int i = 0; i < N; i++) {
    long x, h;
    cin >> x >> h;
    monster.push_back(make_pair(x, h));
  }
  long count = 0;
  sort(monster.begin(), monster.end());
  for (int i = 0; i < monster.size(); i++) {
    if (damage[i] < monster[i].second) {
      long pos = upper_bound(monster.begin(), monster.end(),
                             make_pair(monster[i].first + 2 * D + 1,
                                       numeric_limits<long>::min())) -
                 monster.begin();
      long d = ((monster[i].second - damage[i]) + A - 1) / A;
      damage[i] += d * A;
      damage[pos] -= d * A;
      count += d;
    }
    damage[i + 1] += damage[i];
  }
  cout << count << endl;
}
Problem p02788 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
#define all(x) (x).begin(), (x).end()
#define fsp(x) cout << fixed << setprecision(x)
const ll inf = LLONG_MAX;
const long double pi = acos(-1);
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }

// 二分探索
// main関数で呼び出す前に，l,
// rの一方が条件を満たし他方が満たさないことを確認すること
long long bs(long long l, long long r, ll d, vector<vll> v, ll i) {
  if (l + 1 == r) {
    if (v[l][0] - v[i][0] > 2 * d)
      return l;
    else
      return r;
  }

  long long mid = (l + r) / 2;
  if (v[mid][0] - v[i][0] > 2 * d)
    return bs(l, mid, d, v, i);
  else
    return bs(mid, r, d, v, i);
}

int main() {
  // ios::sync_with_stdio(false);
  // cin.tie(nullptr);

  ll n, d, a;
  cin >> n >> d >> a;
  vector<vll> v(n, vll(2));
  for (ll i = 0; i < n; i++)
    cin >> v[i][0] >> v[i][1];
  sort(all(v));

  ll ans = 0;
  vll imos(n, 0);
  for (ll i = 0; i < n; i++) {
    if (i > 0)
      imos[i] += imos[i - 1];
    if (v[i][1] <= imos[i])
      continue;
    ll x = ceil((double)(v[i][1] - imos[i]) / a);
    ans += x;
    imos[i] += a * x;
    if (v[n - 1][0] - v[i][0] <= 2 * d)
      continue;
    ll y = bs(i, n - 1, d, v, i);
    imos[y] -= a * x;
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
#define all(x) (x).begin(), (x).end()
#define fsp(x) cout << fixed << setprecision(x)
const ll inf = LLONG_MAX;
const long double pi = acos(-1);
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }

// 二分探索
// main関数で呼び出す前に，l,
// rの一方が条件を満たし他方が満たさないことを確認すること
// 配列を引数にするときは原則として参照渡しすること
long long bs(long long l, long long r, ll d, vector<vll> &v, ll i) {
  if (l + 1 == r) {
    if (v[l][0] - v[i][0] > 2 * d)
      return l;
    else
      return r;
  }

  long long mid = (l + r) / 2;
  if (v[mid][0] - v[i][0] > 2 * d)
    return bs(l, mid, d, v, i);
  else
    return bs(mid, r, d, v, i);
}

int main() {
  // ios::sync_with_stdio(false);
  // cin.tie(nullptr);

  ll n, d, a;
  cin >> n >> d >> a;
  vector<vll> v(n, vll(2));
  for (ll i = 0; i < n; i++)
    cin >> v[i][0] >> v[i][1];
  sort(all(v));

  ll ans = 0;
  vll imos(n, 0);
  for (ll i = 0; i < n; i++) {
    if (i > 0)
      imos[i] += imos[i - 1];
    if (v[i][1] <= imos[i])
      continue;
    ll x = ceil((double)(v[i][1] - imos[i]) / a);
    ans += x;
    imos[i] += a * x;
    if (v[n - 1][0] - v[i][0] <= 2 * d)
      continue;
    ll y = bs(i, n - 1, d, v, i);
    imos[y] -= a * x;
  }
  cout << ans << endl;
}
Problem p02788 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
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 main() {
  int N;
  ll D, A;
  cin >> N >> D >> A;
  vector<ll> cum(N, 0);

  using Mon = pair<ll, ll>;
  vector<Mon> M(N);
  for (int i = 0; i < (int)N; ++i)
    cin >> M[i].first >> M[i].second;

  sort((M).begin(), (M).end());

  ll ans = 0;
  for (int i = 0; i < (int)N; ++i) {
    if (i > 0)
      cum[i] += cum[i - 1];

    if (M[i].second + cum[i] <= 0)
      continue;
    ll k = (M[i].second + cum[i] + A - 1) / A;

    ans += k;
    cum[i] -= A * k;
    int j = distance(M.begin(),
                     upper_bound((M).begin(), (M).end(),
                                 make_pair(M[i].first + 2 * D, (ll)1e18)));
    cum[j] += A * k;
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
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 main() {
  int N;
  ll D, A;
  cin >> N >> D >> A;
  vector<ll> cum(N + 1, 0);

  using Mon = pair<ll, ll>;
  vector<Mon> M(N);
  for (int i = 0; i < (int)N; ++i)
    cin >> M[i].first >> M[i].second;

  sort((M).begin(), (M).end());

  ll ans = 0;
  for (int i = 0; i < (int)N; ++i) {
    if (i > 0)
      cum[i] += cum[i - 1];

    if (M[i].second + cum[i] <= 0)
      continue;
    ll k = (M[i].second + cum[i] + A - 1) / A;

    ans += k;
    cum[i] -= A * k;
    int j = distance(M.begin(),
                     upper_bound((M).begin(), (M).end(),
                                 make_pair(M[i].first + 2 * D, (ll)1e18)));
    cum[j] += A * k;
  }

  cout << ans << endl;

  return 0;
}
Problem p02788 (C++)

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

lint n, d, a;
vector<pair<lint, lint>> xh;

int main() {
  cin >> n >> d >> a;
  xh.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> xh[i].first >> xh[i].second;
  }
  sort(xh.begin(), xh.end());
  vector<lint> x(n), h(n);
  for (int i = 0; i < n; i++) {
    x[i] = xh[i].first, h[i] = xh[i].second;
  }
  // right[i]:[i,right[i])を攻撃できる
  vector<lint> right(n);
  for (int i = 0; i < n; i++) {
    right[i] = upper_bound(x.begin(), x.end(), x[i] + 2 * d) - x.begin();
  }
  vector<lint> damage(n, 0);
  lint dmg_now = 0;
  lint ans = 0;
  for (int i = 0; i < n; i++) {
    dmg_now -= damage[i];
    if (h[i] - dmg_now <= 0) {
      continue;
    } else {
      lint rem = h[i] - dmg_now;
      lint num = (rem + a - 1) / a;
      dmg_now += num * a;
      damage[right[i]] += num * a;
      ans += num;
    }
  }
  cout << ans << endl;
}

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

lint n, d, a;
vector<pair<lint, lint>> xh;

int main() {
  cin >> n >> d >> a;
  xh.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> xh[i].first >> xh[i].second;
  }
  sort(xh.begin(), xh.end());
  vector<lint> x(n), h(n);
  for (int i = 0; i < n; i++) {
    x[i] = xh[i].first, h[i] = xh[i].second;
  }
  // right[i]:[i,right[i])を攻撃できる
  vector<lint> right(n);
  for (int i = 0; i < n; i++) {
    right[i] = upper_bound(x.begin(), x.end(), x[i] + 2 * d) - x.begin();
  }
  vector<lint> damage(n, 0);
  lint dmg_now = 0;
  lint ans = 0;
  for (int i = 0; i < n; i++) {
    dmg_now -= damage[i];
    if (h[i] - dmg_now <= 0) {
      continue;
    } else {
      lint rem = h[i] - dmg_now;
      lint num = (rem + a - 1) / a;
      dmg_now += num * a;
      if (right[i] < n) {
        damage[right[i]] += num * a;
      }
      ans += num;
    }
  }
  cout << ans << endl;
}
Problem p02788 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;
using ll = long long int;
const long long int INF = 1001001001001001LL;
const int inf = 1000000007;
long long int MOD = 1000000007LL;
double PI = 3.1415926535897932;

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

  ll n, d, a;
  cin >> n >> d >> a;
  vector<pair<ll, ll>> p(n);
  rep(i, n) cin >> p[i].first >> p[i].second;
  sort(p.begin(), p.end());

  vector<ll> imos(n, 0);
  ll cnt = 0;
  rep(i, n) {
    if (i > 0)
      imos[i] += imos[i - 1];
    ll h = p[i].second - imos[i];

    if (h > 0) {
      ll c = (h + a - 1) / a;
      ll damage = c * a;

      ll out =
          upper_bound(p.begin(), p.end(), make_pair(p[i].first + 2 * d, INF)) -
          p.begin();

      imos[i] += damage;
      imos[out] -= damage;
      cnt += c;
    }
  }

  cout << cnt << 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 int;
const long long int INF = 1001001001001001LL;
const int inf = 1000000007;
long long int MOD = 1000000007LL;
double PI = 3.1415926535897932;

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

  ll n, d, a;
  cin >> n >> d >> a;
  vector<pair<ll, ll>> p(n);
  rep(i, n) cin >> p[i].first >> p[i].second;
  sort(p.begin(), p.end());

  vector<ll> imos(n + 1, 0);
  ll cnt = 0;
  rep(i, n) {
    if (i > 0)
      imos[i] += imos[i - 1];
    ll h = p[i].second - imos[i];

    if (h > 0) {
      ll c = (h + a - 1) / a;
      ll damage = c * a;

      ll out =
          upper_bound(p.begin(), p.end(), make_pair(p[i].first + 2 * d, INF)) -
          p.begin();

      imos[i] += damage;
      imos[out] -= damage;
      cnt += c;
    }
  }

  cout << cnt << endl;

  return 0;
}
Problem p02788 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define PI acos(-1)
#define pcnt __builtin_popcountll
#define rng(a) a.begin(), a.end()
#define sz(x) (int)(x).size()
#define v(T) vector<T>
#define vv(T) v(v(T))
#define fi first
#define se second

using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;

template <typename T> inline istream &operator>>(istream &i, v(T) & v) {
  rep(j, sz(v)) i >> v[j];
  return i;
}
template <typename T1, typename T2>
inline istream &operator>>(istream &i, pair<T1, T2> &v) {
  return i >> v.fi >> v.se;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

ll INF = 1001001001;
ll LINF = 1001001001001001001ll;

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

  ll n, d, a;
  cin >> n >> d >> a;
  vector<LP> xh(n);
  rep(i, n) cin >> xh[i].fi >> xh[i].se;
  sort(rng(xh));

  vl ds(n);
  ll s = 0;
  ll ans = 0;
  rep(i, n) {
    s += ds[i];
    if (xh[i].se > s) {
      ll r = xh[i].se - s;
      ll c = r / a;
      if (r % a)
        c++;
      s += a * c;

      ll it = upper_bound(rng(xh), LP(xh[i].fi + 2 * d, LINF)) - xh.begin();
      ds[it] -= a * c;
      ans += c;
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define PI acos(-1)
#define pcnt __builtin_popcountll
#define rng(a) a.begin(), a.end()
#define sz(x) (int)(x).size()
#define v(T) vector<T>
#define vv(T) v(v(T))
#define fi first
#define se second

using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;

template <typename T> inline istream &operator>>(istream &i, v(T) & v) {
  rep(j, sz(v)) i >> v[j];
  return i;
}
template <typename T1, typename T2>
inline istream &operator>>(istream &i, pair<T1, T2> &v) {
  return i >> v.fi >> v.se;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

ll INF = 1001001001;
ll LINF = 1001001001001001001ll;

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

  ll n, d, a;
  cin >> n >> d >> a;
  vector<LP> xh(n);
  rep(i, n) cin >> xh[i].fi >> xh[i].se;
  sort(rng(xh));

  vl ds(n + 10);
  ll s = 0;
  ll ans = 0;
  rep(i, n) {
    s += ds[i];
    if (xh[i].se > s) {
      ll r = xh[i].se - s;
      ll c = r / a;
      if (r % a)
        c++;
      s += a * c;

      ll it = upper_bound(rng(xh), LP(xh[i].fi + 2 * d, LINF)) - xh.begin();
      ds[it] -= a * c;
      ans += c;
    }
  }
  cout << ans << endl;
}
Problem p02788 (C++)

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

using namespace std;

typedef long long ll;

double getTime() { return clock() / (double)CLOCKS_PER_SEC; }

void read(){};

template <typename T, typename... Args> void read(T &a, Args &...args) {
  cin >> a;
  read(args...);
}

void print(){};

template <typename T, typename... Args> void print(T a, Args... args) {
  cout << a << " \n"[sizeof...(args) == 0];
  print(args...);
}

int main() {
  int n;
  read(n);
  ll d, A;
  read(d, A);
  vector<pair<ll, ll>> v(n);
  for (int i = 0; i < n; i++) {
    read(v[i].first, v[i].second);
    v[i].second = (v[i].second + A - 1) / A;
  }
  sort(v.begin(), v.end());
  vector<ll> pf(n + 1, 0);
  int j = 0;
  ll moves = 0;
  for (int i = 0; i < n; i++) {
    while (v[i].first + d + d >= v[j].first)
      j++;
    ll need = max(0LL, v[i].second - pf[i]);
    moves += need;
    pf[i] += need;
    pf[j] -= need;
    pf[i + 1] += pf[i];
  }
  print(moves);
}

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

using namespace std;

typedef long long ll;

double getTime() { return clock() / (double)CLOCKS_PER_SEC; }

void read(){};

template <typename T, typename... Args> void read(T &a, Args &...args) {
  cin >> a;
  read(args...);
}

void print(){};

template <typename T, typename... Args> void print(T a, Args... args) {
  cout << a << " \n"[sizeof...(args) == 0];
  print(args...);
}

int main() {
  int n;
  read(n);
  ll d, A;
  read(d, A);
  vector<pair<ll, ll>> v(n + 1);
  v[n] = {1e18, 1e18};
  for (int i = 0; i < n; i++) {
    read(v[i].first, v[i].second);
    v[i].second = (v[i].second + A - 1) / A;
  }
  sort(v.begin(), v.end());
  vector<ll> pf(n + 1, 0);
  int j = 0;
  ll moves = 0;
  for (int i = 0; i < n; i++) {
    while (v[i].first + d + d >= v[j].first)
      j++;
    ll need = max(0LL, v[i].second - pf[i]);
    moves += need;
    pf[i] += need;
    pf[j] -= need;
    pf[i + 1] += pf[i];
  }
  print(moves);
}
Problem p02788 (C++)

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

// #define DEBUG

using namespace std;

const int N = 1e2 + 7;
const int MOD = 1e9 + 7;
const double eps = 1e-11;
typedef pair<int, int> pii;

struct Node {
  int x, h;
  friend bool operator<(const Node &n1, const Node &n2) { return n1.x < n2.x; }
  friend istream &operator>>(istream &is, Node &nd) {
    return is >> nd.x >> nd.h;
  }
} b[N];

int n, d, a;
int del[N], hhh[N];

inline int getpos(const long long &x) {
  int l = 1, r = n;
  while (l < r) {
    int mid = (l + r) >> 1;
    if (b[mid].x <= x)
      l = mid + 1;
    else
      r = mid;
  }
  return b[l].x > x ? l : n + 1;
}

inline void solve() {
  cin >> n >> d >> a;
  for (int i = 1; i <= n; ++i)
    cin >> b[i];

  sort(b + 1, b + n + 1);
  long long res = 0;
  for (int i = 1, j = 1, now = 0, t, k; i <= n; ++i) {
    while (j <= i)
      now -= del[j++] * a;
    if (now >= b[i].h)
      continue;
    t = (b[i].h - now) / a + ((b[i].h - now) % a != 0);
    res += t;
    now += t * a;
    del[getpos(1ll * b[i].x + d + d)] += t;
  }
  cout << res << endl;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int T = 1;
  while (T--) {
    solve();
  }
  return 0;
}

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

// #define DEBUG

using namespace std;

const int N = 2e5 + 7;
const int MOD = 1e9 + 7;
const double eps = 1e-11;
typedef pair<int, int> pii;

struct Node {
  int x, h;
  friend bool operator<(const Node &n1, const Node &n2) { return n1.x < n2.x; }
  friend istream &operator>>(istream &is, Node &nd) {
    return is >> nd.x >> nd.h;
  }
} b[N];

int n, d, a;
int del[N], hhh[N];

inline int getpos(const long long &x) {
  int l = 1, r = n;
  while (l < r) {
    int mid = (l + r) >> 1;
    if (b[mid].x <= x)
      l = mid + 1;
    else
      r = mid;
  }
  return b[l].x > x ? l : n + 1;
}

inline void solve() {
  cin >> n >> d >> a;
  for (int i = 1; i <= n; ++i)
    cin >> b[i];

  sort(b + 1, b + n + 1);
  long long res = 0;
  for (int i = 1, j = 1, now = 0, t, k; i <= n; ++i) {
    while (j <= i)
      now -= del[j++] * a;
    if (now >= b[i].h)
      continue;
    t = (b[i].h - now) / a + ((b[i].h - now) % a != 0);
    res += t;
    now += t * a;
    del[getpos(1ll * b[i].x + d + d)] += t;
  }
  cout << res << endl;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int T = 1;
  while (T--) {
    solve();
  }
  return 0;
}
Problem p02788 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
struct node {
  int x, h;
} t[100001];
bool cmp(node a, node b) { return a.x < b.x; }
signed main() {
  int n, d, a;
  scanf("%lld%lld%lld", &n, &d, &a);
  for (int i = 1; i <= n; i++)
    scanf("%lld%lld", &t[i].x, &t[i].h);
  sort(t + 1, t + n + 1, cmp);
  long long add = 0, ans = 0;
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      q;
  for (int i = 1; i <= n; i++) {
    while (!q.empty() && t[i].x - q.top().first > 2 * d) {
      add -= 1ll * q.top().second * a;
      q.pop();
    }
    int delta = t[i].h - add;
    if (delta > 0) {
      int tmp = (delta + a - 1) / a;
      ans += tmp;
      add += tmp * a;
      q.push(make_pair(t[i].x, tmp));
    }
  }
  printf("%lld", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
struct node {
  int x, h;
} t[1000001];
bool cmp(node a, node b) { return a.x < b.x; }
signed main() {
  int n, d, a;
  scanf("%lld%lld%lld", &n, &d, &a);
  for (int i = 1; i <= n; i++)
    scanf("%lld%lld", &t[i].x, &t[i].h);
  sort(t + 1, t + n + 1, cmp);
  long long add = 0, ans = 0;
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      q;
  for (int i = 1; i <= n; i++) {
    while (!q.empty() && t[i].x - q.top().first > 2 * d) {
      add -= 1ll * q.top().second * a;
      q.pop();
    }
    int delta = t[i].h - add;
    if (delta > 0) {
      int tmp = (delta + a - 1) / a;
      ans += tmp;
      add += tmp * a;
      q.push(make_pair(t[i].x, tmp));
    }
  }
  printf("%lld", ans);
  return 0;
}