Problem p03472 (C++)

Buggy submission (status: Runtime Error):
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <valarray>
#include <vector>
using namespace std;
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define trace(x) cerr << #x << ": " << x << " " << endl;

typedef int64_t ll;

#define endl '\n'
#define int ll
ll mod = 1e9 + 7;
ll mod1 = 1e9 + 5;
ll power(ll a, ll b) {
  if (b == 0)
    return 1;
  else if (b % 2 == 0)
    return power((((a % mod) * (a % mod)) % mod), b / 2) % mod;
  else
    return ((a % mod) * (power((((a % mod) * (a % mod)) % mod), b / 2) % mod)) %
           mod;
}
int n, m, t;
vector<pair<int, int>> mat[5001];
vector<pair<int, int>> ulta[5001];
int32_t main() {
  IOS

#ifndef ONLINE_JUDGE

      freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  int n, h;
  cin >> n >> h;
  vector<pair<int, int>> v;
  for (int i = 0; i < n; ++i) {
    int a, b;
    cin >> a >> b;
    v.push_back({a, b});
  }
  sort(v.begin(), v.end());
  if (v[n - 1].second >= h) {
    cout << 1;
    return 0;
  }
  vector<pair<int, int>> v1;
  for (int i = 0; i < v.size() - 1; ++i) {
    v1.push_back({v[i].second, v[i].first});
  }
  sort(v1.begin(), v1.end());
  int cost = 0;
  for (int i = v1.size() - 1; i >= 0; --i) {
    if (h <= v[n - 1].second) {
      cost++;
      cout << cost;
      return 0;
    }
    if (v1[i].first > v[n - 1].first) {
      h -= v1[i].first;
      cost++;
      if (h <= 0)
        break;
    }
  }
  if (h <= 0) {
    cout << cost;
    return 0;
  }
  if (v[n - 1].first >= v[n - 1].second) {
    cout << cost + (h + v[n - 1].first - 1) / v[n - 1].first;
    return 0;
  } else {
    cost++;
    h -= v[n - 1].second;
    cout << cost + (h + v[n - 1].first - 1) / v[n - 1].first;
  }
}


Fixed submission:
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <valarray>
#include <vector>
using namespace std;
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define trace(x) cerr << #x << ": " << x << " " << endl;

typedef int64_t ll;

#define endl '\n'
#define int ll
ll mod = 1e9 + 7;
ll mod1 = 1e9 + 5;
ll power(ll a, ll b) {
  if (b == 0)
    return 1;
  else if (b % 2 == 0)
    return power((((a % mod) * (a % mod)) % mod), b / 2) % mod;
  else
    return ((a % mod) * (power((((a % mod) * (a % mod)) % mod), b / 2) % mod)) %
           mod;
}
int n, m, t;
vector<pair<int, int>> mat[5001];
vector<pair<int, int>> ulta[5001];
int32_t main() {
  IOS

      int n,
      h;
  cin >> n >> h;
  vector<pair<int, int>> v;
  for (int i = 0; i < n; ++i) {
    int a, b;
    cin >> a >> b;
    v.push_back({a, b});
  }
  sort(v.begin(), v.end());
  if (v[n - 1].second >= h) {
    cout << 1;
    return 0;
  }
  vector<pair<int, int>> v1;
  for (int i = 0; i < v.size() - 1; ++i) {
    v1.push_back({v[i].second, v[i].first});
  }
  sort(v1.begin(), v1.end());
  int cost = 0;
  for (int i = v1.size() - 1; i >= 0; --i) {
    if (h <= v[n - 1].second) {
      cost++;
      cout << cost;
      return 0;
    }
    if (v1[i].first > v[n - 1].first) {
      h -= v1[i].first;
      cost++;
      if (h <= 0)
        break;
    }
  }
  if (h <= 0) {
    cout << cost;
    return 0;
  }
  if (v[n - 1].first >= v[n - 1].second) {
    cout << cost + (h + v[n - 1].first - 1) / v[n - 1].first;
    return 0;
  } else {
    cost++;
    h -= v[n - 1].second;
    cout << cost + (h + v[n - 1].first - 1) / v[n - 1].first;
  }
}