Problem p02883 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPr(i, n) for (int i = (n)-1; i >= 0; --i)
#define FORq(i, m, n) for (int i = (m); i <= (n); ++i)
#define FORqr(i, m, n) for (int i = (n); i >= (m); --i)
#define PB push_back
#define MP make_pair
#define fst first
#define snd second
#define SIN(x, S) (S.count(x) != 0)
#define M0(x) memset(x, 0, sizeof(x))
#define FILL(x, y) memset(x, y, sizeof(x))
#define MM(x) memset(x, -1, sizeof(x))
#define ALL(x) (x).begin(), (x).end()
#define DB(x) cerr << #x << " = " << x << endl
#define DB2(x, y)                                                              \
  cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")\n";
#define DEBUG                                                                  \
  int x12345;                                                                  \
  cin >> x12345;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef long long ll;
typedef long long integer;
///////////////////////////////////////////////
const ll MOD = 1e9 + 7;
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <int MOD> struct Fp {
  long long val;

  constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
    if (val < 0)
      v += MOD;
  }

  constexpr int getmod() { return MOD; }

  constexpr Fp operator-() const noexcept { return val ? MOD - val : 0; }

  constexpr Fp operator+(const Fp &r) const noexcept { return Fp(*this) += r; }

  constexpr Fp operator-(const Fp &r) const noexcept { return Fp(*this) -= r; }

  constexpr Fp operator*(const Fp &r) const noexcept { return Fp(*this) *= r; }

  constexpr Fp operator/(const Fp &r) const noexcept { return Fp(*this) /= r; }

  constexpr Fp &operator+=(const Fp &r) noexcept {
    val += r.val;
    if (val >= MOD)
      val -= MOD;
    return *this;
  }

  constexpr Fp &operator-=(const Fp &r) noexcept {
    val -= r.val;
    if (val < 0)
      val += MOD;
    return *this;
  }

  constexpr Fp &operator*=(const Fp &r) noexcept {
    val = val * r.val % MOD;
    return *this;
  }

  constexpr Fp &operator/=(const Fp &r) noexcept {
    long long a = r.val, b = MOD, u = 1, v = 0;
    while (b) {
      long long t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    val = val * u % MOD;
    if (val < 0)
      val += MOD;
    return *this;
  }

  constexpr bool operator==(const Fp &r) const noexcept {
    return this->val == r.val;
  }

  constexpr bool operator!=(const Fp &r) const noexcept {
    return this->val != r.val;
  }

  friend constexpr ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept {
    return os << x.val;
  }

  friend constexpr istream &operator>>(istream &is, Fp<MOD> &x) noexcept {
    return is >> x.val;
  }

  friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
    if (n == 0)
      return 1;
    auto t = modpow(a, n / 2);
    t = t * t;
    if (n & 1)
      t = t * a;
    return t;
  }
};

using mint = Fp<MOD>;

void scdvec(vector<int> &a, int n, int s = 0) {
  for (int i = s; i < n; ++i) {
    scanf("%d", &a[i]);
  }
}

void sclldvec(vector<long long> &a, int n, int s = 0) {
  for (int i = s; i < n; ++i) {
    scanf("%lld", &a[i]);
  }
}

///////////////////////////////////////////////
///  ((((っ・ω・)っ |AC|
ll N, K;
vector<ll> A, F;
bool check(ll target) {
  ll rest = K;
  REP(i, N) {
    ll a, f;
    a = A[i];
    f = F[i];
    if (a * f <= target)
      continue;
    ll change = target / f;
    rest -= (A[i] - change);
  }
  if (rest >= 0)
    return true;
  else
    return false;
}

ll binary_search() {
  // lower_bound (a-1,b] (ans = [a,b])
  // <= left never satisfy
  // right <= always satisfy

  ll left = -1;
  ll right = 1e12 + 3; // index of size + 1;

  while (right - left > 1) {
    int mid = left + (right - left) / 2;

    if (check(mid))
      right = mid;
    else
      left = mid;
  }

  // left is the maximum that don't satisfy the condition.
  // right is the minimum that satisfies the condtion.

  return right;
}

int main() {
  scanf("%lld%lld", &N, &K);
  REP(i, N) {
    ll x;
    scanf("%lld", &x);
    A.PB(x);
  }
  REP(i, N) {
    ll x;
    scanf("%lld", &x);
    F.PB(x);
  }

  sort(ALL(A));
  sort(F.rbegin(), F.rend());

  cout << binary_search() << endl;
  // ll target; cin >> target;
  // cout << check(target) << endl;
}

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

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPr(i, n) for (int i = (n)-1; i >= 0; --i)
#define FORq(i, m, n) for (int i = (m); i <= (n); ++i)
#define FORqr(i, m, n) for (int i = (n); i >= (m); --i)
#define PB push_back
#define MP make_pair
#define fst first
#define snd second
#define SIN(x, S) (S.count(x) != 0)
#define M0(x) memset(x, 0, sizeof(x))
#define FILL(x, y) memset(x, y, sizeof(x))
#define MM(x) memset(x, -1, sizeof(x))
#define ALL(x) (x).begin(), (x).end()
#define DB(x) cerr << #x << " = " << x << endl
#define DB2(x, y)                                                              \
  cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")\n";
#define DEBUG                                                                  \
  int x12345;                                                                  \
  cin >> x12345;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef long long ll;
typedef long long integer;
///////////////////////////////////////////////
const ll MOD = 1e9 + 7;
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <int MOD> struct Fp {
  long long val;

  constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
    if (val < 0)
      v += MOD;
  }

  constexpr int getmod() { return MOD; }

  constexpr Fp operator-() const noexcept { return val ? MOD - val : 0; }

  constexpr Fp operator+(const Fp &r) const noexcept { return Fp(*this) += r; }

  constexpr Fp operator-(const Fp &r) const noexcept { return Fp(*this) -= r; }

  constexpr Fp operator*(const Fp &r) const noexcept { return Fp(*this) *= r; }

  constexpr Fp operator/(const Fp &r) const noexcept { return Fp(*this) /= r; }

  constexpr Fp &operator+=(const Fp &r) noexcept {
    val += r.val;
    if (val >= MOD)
      val -= MOD;
    return *this;
  }

  constexpr Fp &operator-=(const Fp &r) noexcept {
    val -= r.val;
    if (val < 0)
      val += MOD;
    return *this;
  }

  constexpr Fp &operator*=(const Fp &r) noexcept {
    val = val * r.val % MOD;
    return *this;
  }

  constexpr Fp &operator/=(const Fp &r) noexcept {
    long long a = r.val, b = MOD, u = 1, v = 0;
    while (b) {
      long long t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    val = val * u % MOD;
    if (val < 0)
      val += MOD;
    return *this;
  }

  constexpr bool operator==(const Fp &r) const noexcept {
    return this->val == r.val;
  }

  constexpr bool operator!=(const Fp &r) const noexcept {
    return this->val != r.val;
  }

  friend constexpr ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept {
    return os << x.val;
  }

  friend constexpr istream &operator>>(istream &is, Fp<MOD> &x) noexcept {
    return is >> x.val;
  }

  friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
    if (n == 0)
      return 1;
    auto t = modpow(a, n / 2);
    t = t * t;
    if (n & 1)
      t = t * a;
    return t;
  }
};

using mint = Fp<MOD>;

void scdvec(vector<int> &a, int n, int s = 0) {
  for (int i = s; i < n; ++i) {
    scanf("%d", &a[i]);
  }
}

void sclldvec(vector<long long> &a, int n, int s = 0) {
  for (int i = s; i < n; ++i) {
    scanf("%lld", &a[i]);
  }
}

///////////////////////////////////////////////
///  ((((っ・ω・)っ |AC|
ll N, K;
vector<ll> A, F;
bool check(ll target) {
  ll rest = K;
  REP(i, N) {
    ll a, f;
    a = A[i];
    f = F[i];
    if (a * f <= target)
      continue;
    ll change = target / f;
    rest -= (A[i] - change);
  }
  if (rest >= 0)
    return true;
  else
    return false;
}

ll binary_search() {
  // lower_bound (a-1,b] (ans = [a,b])
  // <= left never satisfy
  // right <= always satisfy

  ll left = -1;
  ll right = 1e12 + 3; // index of size + 1;

  while (right - left > 1) {
    ll mid = left + (right - left) / 2;

    if (check(mid))
      right = mid;
    else
      left = mid;
  }

  // left is the maximum that don't satisfy the condition.
  // right is the minimum that satisfies the condtion.

  return right;
}

int main() {
  scanf("%lld%lld", &N, &K);
  REP(i, N) {
    ll x;
    scanf("%lld", &x);
    A.PB(x);
  }
  REP(i, N) {
    ll x;
    scanf("%lld", &x);
    F.PB(x);
  }

  sort(ALL(A));
  sort(F.rbegin(), F.rend());

  cout << binary_search() << endl;
  // ll target; cin >> target;
  // cout << check(target) << endl;
}