Problem p03472 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <locale>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#define all(a) a.begin(), a.end()
#define VV(T) vector<vector<T>>
#define rep(i, N) for (int i = 0; i < N; i++)
#define repsg(i, s, g) for (int i = s; i < g; i++)
#define name(x) #x
using namespace std;

const int INF = INT_MAX;
const long long INFL = LONG_LONG_MAX;
const double PI = atan(1) * 4;
const int MOD = pow(10, 9) + 7;
int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};

typedef long long ll, LL;
typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long>> vvll, vvLL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pLL;

// vector入力
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}
// vector出力
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) {
    os << vec[i] << (i + 1 == vec.size() ? "" : " ");
  }
  return os;
}

template <typename T> struct edge {
  int src, to;
  T cost;
  edge(int to, T cost) : src(-1), to(to), cost(cost) {}
  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
  edge &operator=(const int &x) {
    to = x;
    return *this;
  }
  operator int() const { return to; }
};

bool is_prime(int N) {

  if (N == 1)
    return false;

  int sqn = sqrt(N);

  for (auto i = 2; i <= sqn; i++) {
    if (N % i == 0)
      return false;
  }
  return true;
}

int main() {
  int n, h;
  cin >> n >> h;
  vector<pii> k(n);
  rep(i, n) { cin >> k[i].first >> k[i].second; }

  sort(all(k), [](pii a, pii b) { return a.second > b.second; });
  auto mx =
      *max_element(all(k), [](pii a, pii b) { return a.first < b.first; });
  int p = 0;
  while (k[p].second > mx.first) {
    p++;
  }

  k.erase(next(k.begin(), p), k.end());

  ll attacksum = 0;
  for (auto el : k) {
    attacksum += el.second;
  }

  int count = 0;

  count = (h - attacksum) / mx.first;
  h -= count * mx.first;

  int q = 0;
  while (h > 0) {
    h -= k[q].second;
    count++;
    q++;
  }

  cout << count << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <locale>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#define all(a) a.begin(), a.end()
#define VV(T) vector<vector<T>>
#define rep(i, N) for (int i = 0; i < N; i++)
#define repsg(i, s, g) for (int i = s; i < g; i++)
#define name(x) #x
using namespace std;

const int INF = INT_MAX;
const long long INFL = LONG_LONG_MAX;
const double PI = atan(1) * 4;
const int MOD = pow(10, 9) + 7;
int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};

typedef long long ll, LL;
typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long>> vvll, vvLL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pLL;

// vector入力
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}
// vector出力
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) {
    os << vec[i] << (i + 1 == vec.size() ? "" : " ");
  }
  return os;
}

template <typename T> struct edge {
  int src, to;
  T cost;
  edge(int to, T cost) : src(-1), to(to), cost(cost) {}
  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
  edge &operator=(const int &x) {
    to = x;
    return *this;
  }
  operator int() const { return to; }
};

bool is_prime(int N) {

  if (N == 1)
    return false;

  int sqn = sqrt(N);

  for (auto i = 2; i <= sqn; i++) {
    if (N % i == 0)
      return false;
  }
  return true;
}

int main() {
  int n, h;
  cin >> n >> h;
  vector<pii> k(n);
  rep(i, n) { cin >> k[i].first >> k[i].second; }

  sort(all(k), [](pii a, pii b) { return a.second > b.second; });
  auto mx =
      *max_element(all(k), [](pii a, pii b) { return a.first < b.first; });
  int p = 0;
  while (k[p].second > mx.first) {
    p++;
  }

  k.erase(next(k.begin(), p), k.end());

  ll attacksum = 0;
  for (auto el : k) {
    attacksum += el.second;
  }

  int count = 0;

  if (h > attacksum) {
    count = (h - attacksum) / mx.first;
    h -= count * mx.first;

    if (h > attacksum) {
      count++;
      h -= mx.first;
    }
  }

  int q = 0;
  while (h > 0) {
    h -= k[q].second;
    count++;
    q++;
  }

  cout << count << endl;

  return 0;
}