Problem p02883 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
// #include <tr1/unordered_map>
// #include "Bignum/bignum.h"
// #define big bignum
#define lowbit(x) (x & -x)
#define debug(x) (cout << "#x = " << (x) << endl)
#define Set(x, i) memset(x, i, sizeof(x))
#define R register
#define For(i, j, k) for (R int i = (j); i <= (int)(k); ++i)
#define Rep(i, j, k) for (R int i = (j); i >= (int)(k); --i)
#define Cross(i, j, k) for (R int i = (j); i; i = (k))
using namespace std;
typedef long long ll;
const ll N = 100011;
const ll INF = 5e16;

namespace IO {

inline char gc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  return (p1 == p2) &&
                 (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}

#define dd ch = getchar()
inline ll read() {
  R ll x = 0;
  R int f = 0;
  R char dd;
  for (; !isdigit(ch); dd)
    f ^= (ch == '-');
  for (; isdigit(ch); dd)
    x = x * 10 + (ch ^ 48);
  return f ? -x : x;
}
#undef dd

inline void write(ll x) {
  if (x < 0)
    x = -x, putchar('-');
  if (x > 9)
    write(x / 10);
  putchar(x % 10 | 48);
}

inline void wrn(ll x) { write(x), putchar(' '); }

inline void wln(ll x) { write(x), putchar('\n'); }

inline void wlnn(ll x, ll y) { wrn(x), wln(y); }

} // namespace IO

using IO::read;
using IO::wln;

namespace Cesare {

ll n, k, a[N], f[N];

inline int Check(ll x) {
  ll s = k;
  For(i, 1, n) if (a[i] * f[i] > x) {
    ll cc = x / f[i];
    if (a[i] - cc > s)
      return 0;
    s -= a[i] - cc;
  }
  return s >= 0;
}

ll Ans;

int main() {
  n = read(), k = read();
  For(i, 1, n) a[i] = read();
  For(i, 1, n) f[i] = read();
  sort(a + 1, a + n + 1);
  sort(f + 1, f + n + 1, greater<ll>());

  ll l = 0, r = 1e12;
  while (l <= r) {
    ll Mid = (l + r) >> 1;
    Check(Mid) ? Ans = Mid, r = Mid - 1 : l = Mid + 1;
  }
  return wln(Ans), 0;
}

} // namespace Cesare

int main() {
  //	freopen(".in", "r", stdin);
  //	freopen(".out", "w", stdout);

  return Cesare ::main();
}

/*

*/


Fixed submission:
#include <bits/stdc++.h>
// #include <tr1/unordered_map>
// #include "Bignum/bignum.h"
// #define big bignum
#define lowbit(x) (x & -x)
#define debug(x) (cout << "#x = " << (x) << endl)
#define Set(x, i) memset(x, i, sizeof(x))
#define R register
#define For(i, j, k) for (R int i = (j); i <= (int)(k); ++i)
#define Rep(i, j, k) for (R int i = (j); i >= (int)(k); --i)
#define Cross(i, j, k) for (R int i = (j); i; i = (k))
using namespace std;
typedef long long ll;
const ll N = 1000011;
const ll INF = 5e16;

namespace IO {

inline char gc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  return (p1 == p2) &&
                 (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}

#define dd ch = getchar()
inline ll read() {
  R ll x = 0;
  R int f = 0;
  R char dd;
  for (; !isdigit(ch); dd)
    f ^= (ch == '-');
  for (; isdigit(ch); dd)
    x = x * 10 + (ch ^ 48);
  return f ? -x : x;
}
#undef dd

inline void write(ll x) {
  if (x < 0)
    x = -x, putchar('-');
  if (x > 9)
    write(x / 10);
  putchar(x % 10 | 48);
}

inline void wrn(ll x) { write(x), putchar(' '); }

inline void wln(ll x) { write(x), putchar('\n'); }

inline void wlnn(ll x, ll y) { wrn(x), wln(y); }

} // namespace IO

using IO::read;
using IO::wln;

namespace Cesare {

ll n, k, a[N], f[N];

inline int Check(ll x) {
  ll s = k;
  For(i, 1, n) if (a[i] * f[i] > x) {
    ll cc = x / f[i];
    if (a[i] - cc > s)
      return 0;
    s -= a[i] - cc;
  }
  return s >= 0;
}

ll Ans;

int main() {
  n = read(), k = read();
  For(i, 1, n) a[i] = read();
  For(i, 1, n) f[i] = read();
  sort(a + 1, a + n + 1);
  sort(f + 1, f + n + 1, greater<ll>());

  ll l = 0, r = 1e12;
  while (l <= r) {
    ll Mid = (l + r) >> 1;
    Check(Mid) ? Ans = Mid, r = Mid - 1 : l = Mid + 1;
  }
  return wln(Ans), 0;
}

} // namespace Cesare

int main() {
  //	freopen(".in", "r", stdin);
  //	freopen(".out", "w", stdout);

  return Cesare ::main();
}

/*

*/