Problem p03472 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define PB push_back
using namespace std;
typedef pair<int, int> P1;
typedef pair<int, pair<int, int>> P2;
static const int INF = 1ll << 60;
static const int dx[] = {
    1,
    -1,
    0,
    0,
};
static const int dy[] = {0, 0, 1, -1};
static const int mod = 1000000007;

int N, H;
vector<P1> Katana;

int maxSwingPower;
int throwPower[100005];

int AttackCount(int n) {
  int h = H - n * maxSwingPower;
  int it = upper_bound(throwPower, throwPower + N, h) - throwPower;
  if (throwPower[it - 1] == h)
    it--;
  // cout<<h<<" "<<n<<endl;
  // cout<<n*maxSwingPower<<" "<<throwPower[it]<<" "<<it<<endl;
  if (h <= 0)
    return n;
  else if (h - throwPower[it] > 0)
    return INF;
  else
    return n + it;
}

int a[100005];
int b[100005];

signed main() {
  cin >> N >> H;
  for (int i = 0; i < N; ++i) {
    int s, t;
    cin >> s >> t;
    Katana.push_back(P1(t, s));
    maxSwingPower = max(s, maxSwingPower);
    a[i] = s;
    b[i] = t;
  }
  sort(b, b + N);
  // cout<<maxSwingPower<<endl;
  sort(Katana.begin(), Katana.end(), greater<P1>());
  for (int i = 1; i <= N; ++i) {
    throwPower[i] = throwPower[i - 1] + Katana[i - 1].first;
    // cout<<throwPower[i]<<endl;
  }
  /*int l=0,r=1000000000;
  int minAttackCount = INF;
  for(int i=0;i<1000005;++i){
      minAttackCount=min(minAttackCount,AttackCount(i));
  }
  cout<<minAttackCount<<endl;*/
  int it = upper_bound(b, b + N, maxSwingPower) - b;
  int h = H - throwPower[N - it];
  int p = h / maxSwingPower;
  if (maxSwingPower * p < h)
    p++;
  if (h <= 0) {
    cout << 1 / 0 << endl;
  }
  // cout<<h<<" "<<p<<" "<<N-it<<endl;
  cout << p + N - it << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define PB push_back
using namespace std;
typedef pair<int, int> P1;
typedef pair<int, pair<int, int>> P2;
static const int INF = 1ll << 60;
static const int dx[] = {
    1,
    -1,
    0,
    0,
};
static const int dy[] = {0, 0, 1, -1};
static const int mod = 1000000007;

int N, H;
vector<P1> Katana;

int maxSwingPower;
int throwPower[100005];

int AttackCount(int n) {
  int h = H - n * maxSwingPower;
  int it = upper_bound(throwPower, throwPower + N, h) - throwPower;
  if (throwPower[it - 1] == h)
    it--;
  // cout<<h<<" "<<n<<endl;
  // cout<<n*maxSwingPower<<" "<<throwPower[it]<<" "<<it<<endl;
  if (h <= 0)
    return n;
  else if (h - throwPower[it] > 0)
    return INF;
  else
    return n + it;
}

int a[100005];
int b[100005];

signed main() {
  cin >> N >> H;
  for (int i = 0; i < N; ++i) {
    int s, t;
    cin >> s >> t;
    Katana.push_back(P1(t, s));
    maxSwingPower = max(s, maxSwingPower);
    a[i] = s;
    b[i] = t;
  }
  sort(b, b + N);
  // cout<<maxSwingPower<<endl;
  sort(Katana.begin(), Katana.end(), greater<P1>());
  for (int i = 1; i <= N; ++i) {
    throwPower[i] = throwPower[i - 1] + Katana[i - 1].first;
    // cout<<throwPower[i]<<endl;
  }
  /*int l=0,r=1000000000;
  int minAttackCount = INF;
  for(int i=0;i<1000005;++i){
      minAttackCount=min(minAttackCount,AttackCount(i));
  }
  cout<<minAttackCount<<endl;*/
  int it = upper_bound(b, b + N, maxSwingPower) - b;
  int h = H - throwPower[N - it];
  int p = h / maxSwingPower;
  if (maxSwingPower * p < h)
    p++;
  if (h < 0) {
    for (int i = 0; i <= N; ++i) {
      if (H <= throwPower[i]) {
        cout << i << endl;
        return 0;
      }
    }
  }
  // cout<<h<<" "<<p<<" "<<N-it<<endl;
  cout << p + N - it << endl;
}