Problem p02883 (C++)

Buggy submission (status: Runtime Error):
// Gaurav Aggarwal

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

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define F first
#define S second
#define p(s) cout << (s) << endl
#define p2(s, t) cout << (s) << " " << (t) << endl
#define For(i, a, b) for (__typeof(a) i = a; i <= b; i++)
#define inp_arr(a, n)                                                          \
  for (__typeof(n) i = 0; i < n; ++i) {                                        \
    cin >> a[i];                                                               \
  }
#define debug_arr(a, n)                                                        \
  {                                                                            \
    {                                                                          \
      for (__typeof(n) i = 0; i < n; ++i) {                                    \
        cout << a[i] << " ";                                                   \
      }                                                                        \
      cout << "\n";                                                            \
    }                                                                          \
  }

ll power(ll x, ll y) {
  ll res = 1;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % mod;
    y = y >> 1;
    x = (x * x) % mod;
  }
  return res % mod;
}

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

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif

  int n;
  cin >> n;
  ll k;
  cin >> k;
  ll a[n], b[n];
  inp_arr(a, n);
  inp_arr(b, n);

  sort(a, a + n);
  sort(b, b + n);

  vector<ll> v;

  for (int i = 0; i < n; ++i) {
    v.pb(a[i] * b[n - i - 1]);
  }

  ll start = 0, end = 1e18;

  ll ans = 0;

  while (start <= end) {

    ll mid = (start + end) / 2;

    ll f = 0;

    for (int i = 0; i < n; ++i) {

      if (v[i] > mid) {

        ll diff = v[i] - mid;
        ll need = ceil((diff * 1.0) / b[n - i - 1]);
        f += need;
      }
    }

    // cout<<mid<<" "<<f<<"\n";

    if (f <= k) {
      ans = mid;
      end = mid - 1;
    } else
      start = mid + 1;
  }

  cout << ans << "\n";

  return 0;
}

Fixed submission:
// Gaurav Aggarwal

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

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define F first
#define S second
#define p(s) cout << (s) << endl
#define p2(s, t) cout << (s) << " " << (t) << endl
#define For(i, a, b) for (__typeof(a) i = a; i <= b; i++)
#define inp_arr(a, n)                                                          \
  for (__typeof(n) i = 0; i < n; ++i) {                                        \
    cin >> a[i];                                                               \
  }
#define debug_arr(a, n)                                                        \
  {                                                                            \
    {                                                                          \
      for (__typeof(n) i = 0; i < n; ++i) {                                    \
        cout << a[i] << " ";                                                   \
      }                                                                        \
      cout << "\n";                                                            \
    }                                                                          \
  }

ll power(ll x, ll y) {
  ll res = 1;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % mod;
    y = y >> 1;
    x = (x * x) % mod;
  }
  return res % mod;
}

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

  // #ifndef ONLINE_JUDGE
  // freopen("input.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
  // #endif

  int n;
  cin >> n;
  ll k;
  cin >> k;
  ll a[n], b[n];
  inp_arr(a, n);
  inp_arr(b, n);

  sort(a, a + n);
  sort(b, b + n);

  vector<ll> v;

  for (int i = 0; i < n; ++i) {
    v.pb(a[i] * b[n - i - 1]);
  }

  ll start = 0, end = 1e18;

  ll ans = 0;

  while (start <= end) {

    ll mid = (start + end) / 2;

    ll f = 0;

    for (int i = 0; i < n; ++i) {

      if (v[i] > mid) {

        ll diff = v[i] - mid;
        ll need = ceil((diff * 1.0) / b[n - i - 1]);
        f += need;
      }
    }

    // cout<<mid<<" "<<f<<"\n";

    if (f <= k) {
      ans = mid;
      end = mid - 1;
    } else
      start = mid + 1;
  }

  cout << ans << "\n";

  return 0;
}