Problem p02883 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define all(a) a.begin(), a.end()
#define F first
#define S second
#define sz(x) (int)x.size()
#define pb push_back
#define ll long long
#define vi vector<long long>
#define pi pair<long long, long long>
#define mp make_pair
#define endl '\n'
#define _ << ' '
#define FAST_LOG2(x)                                                           \
  (sizeof(unsigned long) * 8 - 1 - __builtin_clzl((unsigned long)(x)))
#define clog2(x) (((x) - (1 << FAST_LOG2(x))) ? FAST_LOG2(x) + 1 : FAST_LOG2(x))
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    idxset;
// find_by_order(i) : iterator to the value at index i, order_of_key(x) : the
// index of value x
const ll mod = (ll)1e9 + 7;
const ll INF = (ll)1e18;
const ll N = (1 << 5);
const double PI = 3.1415926535897;

ll n, kk, a[200001], f[200001], ans;

bool check(ll x) {
  ll k = kk;
  for (int i = 0; i < n; i++) {
    if (a[i] * f[i] > x) {
      k -= (a[i] * f[i] - x + f[i] - 1) / f[i];
    }
    if (k < 0)
      return 0;
  }
  return 1;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("C.txt", "r", stdin);
#endif
  cin >> n >> kk;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    cin >> f[i];
  sort(a, a + n, greater<ll>());
  sort(f, f + n);
  ll lb = 0, rb = (ll)1e12 + 1;
  while (lb < rb) {
    ll mid = (lb + rb) / 2;
    if (check(mid)) {
      ans = mid;
      rb = mid;
    } else {
      lb = mid + 1;
    }
  }
  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define all(a) a.begin(), a.end()
#define F first
#define S second
#define sz(x) (int)x.size()
#define pb push_back
#define ll long long
#define vi vector<long long>
#define pi pair<long long, long long>
#define mp make_pair
#define endl '\n'
#define _ << ' '
#define FAST_LOG2(x)                                                           \
  (sizeof(unsigned long) * 8 - 1 - __builtin_clzl((unsigned long)(x)))
#define clog2(x) (((x) - (1 << FAST_LOG2(x))) ? FAST_LOG2(x) + 1 : FAST_LOG2(x))
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    idxset;
// find_by_order(i) : iterator to the value at index i, order_of_key(x) : the
// index of value x
const ll mod = (ll)1e9 + 7;
const ll INF = (ll)1e18;
const ll N = (1 << 5);
const double PI = 3.1415926535897;

ll n, kk, a[200001], f[200001], ans;

bool check(ll x) {
  ll k = kk;
  for (int i = 0; i < n; i++) {
    if (a[i] * f[i] > x) {
      k -= (a[i] * f[i] - x + f[i] - 1) / f[i];
    }
    if (k < 0)
      return 0;
  }
  return 1;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
// freopen("C.txt", "r", stdin);
#endif
  cin >> n >> kk;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    cin >> f[i];
  sort(a, a + n, greater<ll>());
  sort(f, f + n);
  ll lb = 0, rb = (ll)1e12 + 1;
  while (lb < rb) {
    ll mid = (lb + rb) / 2;
    if (check(mid)) {
      ans = mid;
      rb = mid;
    } else {
      lb = mid + 1;
    }
  }
  cout << ans;
}