prithivMLmods/Coder-Stat huggingface dataset:
Problem p02883 (C++)

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

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rept(i, k, n) for (int i = (k); i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define Sort(v) sort(v.begin(), v.end())
#define Reverse(v) reverse(v.begin(), v.end())

typedef long long ll;
typedef pair<int, int> Pii;
typedef pair<long long, long long> Pdd;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n), f(n);
  rep(i, n) cin >> a[i];
  rep(i, n) cin >> f[i];
  Sort(f);
  Sort(a);
  Reverse(f);
  vector<Pdd> af(n);
  rep(i, n) af[i] = Pdd(a[i] * f[i], f[i]);
  ll l = 0, r = 2e12;
  int cnt = 0;
  while (cnt != 100) {
    cnt++;
    ll mid = (l + r) / 2;
    ll sum = 0;
    rep(i, n) {
      if (af[i].first > mid) {
        sum += (af[i].first - mid - 1) / af[i].second + 1;
      }
    }
    if (sum > k) {
      l = mid;
    } else if (sum <= k) {
      r = mid;
    }
  }
  r = min(r, 500ll);
  while (1) {
    ll sum = 0;
    rep(i, n) {
      if (af[i].first > r) {
        sum += (af[i].first - r - 1) / af[i].second + 1;
      }
    }
    if (sum <= k) {
      cout << r << endl;
      return 0;
    }
    r++;
  }

  return 0;
}


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

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rept(i, k, n) for (int i = (k); i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define Sort(v) sort(v.begin(), v.end())
#define Reverse(v) reverse(v.begin(), v.end())

typedef long long ll;
typedef pair<int, int> Pii;
typedef pair<long long, long long> Pdd;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n), f(n);
  rep(i, n) cin >> a[i];
  rep(i, n) cin >> f[i];
  Sort(f);
  Sort(a);
  Reverse(f);
  vector<Pdd> af(n);
  rep(i, n) af[i] = Pdd(a[i] * f[i], f[i]);
  ll l = 0, r = 2e12;
  int cnt = 0;
  while (cnt != 100) {
    cnt++;
    ll mid = (l + r) / 2;
    ll sum = 0;
    rep(i, n) {
      if (af[i].first > mid) {
        sum += (af[i].first - mid - 1) / af[i].second + 1;
      }
    }
    if (sum > k) {
      l = mid;
    } else if (sum <= k) {
      r = mid;
    }
  }
  r = max(0ll, r - 500ll);
  while (1) {
    ll sum = 0;
    rep(i, n) {
      if (af[i].first > r) {
        sum += (af[i].first - r - 1) / af[i].second + 1;
      }
    }
    if (sum <= k) {
      cout << r << endl;
      return 0;
    }
    r++;
  }

  return 0;
}
Problem p02883 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repit(it, li) for (auto it = li.begin(); it != li.end(); it++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n), f(n);
  rep(i, n) cin >> a[i];
  rep(i, n) cin >> f[i];

  sort(a.rbegin(), a.rend());
  sort(f.begin(), f.end());
  ll l = -1, r = 1000000000000; // l ng  r ok
  while (l + 1 < r) {
    ll m = (l + r) / 2;
    ll c = 0;
    rep(i, n) c += max((ll)0, (ll)a[i] - m / f[i]);
    if (c <= k)
      r = m;
    else
      l = m;
  }

  cout << r << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repit(it, li) for (auto it = li.begin(); it != li.end(); it++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<int> a(n), f(n);
  rep(i, n) cin >> a[i];
  rep(i, n) cin >> f[i];

  sort(a.rbegin(), a.rend());
  sort(f.begin(), f.end());
  ll l = -1, r = 1000000000000; // l ng  r ok
  while (l + 1 < r) {
    ll m = (l + r) / 2;
    ll c = 0;
    rep(i, n) c += max((ll)0, (ll)a[i] - m / f[i]);
    if (c <= k)
      r = m;
    else
      l = m;
  }

  cout << r << endl;
  return 0;
}
Problem p02883 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

const int N = 100005;
const long long limit = 2e12;

long long a[N], b[N];

void usain_bolt() {
  ios::sync_with_stdio(false);
  cin.tie(0);
}

int main() {
  usain_bolt();

  long long n, m, mn = 2000000000005LL;
  cin >> n >> m;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  for (int i = 1; i <= n; ++i)
    cin >> b[i];

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

  long long left = 0, right = 2000000000005LL;

  while (left <= right) {
    long long mid = (left + right) / 2LL;
    long long need = 0;

    for (int i = 1; i <= n; ++i) {
      if (b[i]) {
        long long sterg = mid / b[i];
        need += max(0LL, a[i] - sterg);
      }
    }
    if (need <= m)
      mn = min(mn, mid), right = mid - 1LL;
    else
      left = mid + 1LL;
  }

  cout << mn;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int N = 200005;
const long long limit = 2e12;

long long a[N], b[N];

void usain_bolt() {
  ios::sync_with_stdio(false);
  cin.tie(0);
}

int main() {
  usain_bolt();

  long long n, m, mn = 2000000000005LL;
  cin >> n >> m;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  for (int i = 1; i <= n; ++i)
    cin >> b[i];

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

  long long left = 0, right = 2000000000005LL;

  while (left <= right) {
    long long mid = (left + right) / 2LL;
    long long need = 0;

    for (int i = 1; i <= n; ++i) {
      if (b[i]) {
        long long sterg = mid / b[i];
        need += max(0LL, a[i] - sterg);
      }
    }
    if (need <= m)
      mn = min(mn, mid), right = mid - 1LL;
    else
      left = mid + 1LL;
  }

  cout << mn;
  return 0;
}
Problem p02883 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using ld = long double;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define IFOR(i, a, b) for (int i = (b - 1); i >= (a); --i)
#define RPT(i, a, b) for (int i = (a); i < ((a) + (b)); ++i)
#define IRPT(i, a, b) for (int i = ((a) + (b)-1); i >= (a); --i)
#define ALL(x) x.begin(), x.end()

void solve() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n), f(n);
  FOR(i, 0, n) cin >> a[i];
  FOR(i, 0, n) cin >> f[i];
  sort(ALL(a));
  sort(ALL(f));
  reverse(ALL(f));
  vector<pair<ll, ll>> af(n + 1);
  FOR(i, 0, n) {
    af[i].first = a[i];
    af[i].second = f[i];
  }
  af[n].first = 0;
  af[n].second = 1;
  sort(ALL(af), [&](pair<ll, ll> a, pair<ll, ll> b) {
    if (a.first * a.second >= b.first * b.second)
      return true;
    else
      return false;
  });
  // for(auto x: af) cout << x.first << " " << x.second << endl; cout << endl;
  ll cost = (ll)1 << 45;
  ll cr = cost + 1, cl = -1, cm;
  while (cr - cl > 1) {
    cm = (cl + cr) / 2;
    ll sk = 0;
    FOR(i, 0, n) {
      if (a[i] * f[i] > cm) {
        ll tmp = (a[i] * f[i] - cm + f[i] - 1) / f[i];
        sk += tmp;
      }
    }
    if (sk > k)
      cl = cm;
    else
      cr = cm;
  }
  cout << cr << endl;
}

int main() {
  solve();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using ld = long double;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define IFOR(i, a, b) for (int i = (b - 1); i >= (a); --i)
#define RPT(i, a, b) for (int i = (a); i < ((a) + (b)); ++i)
#define IRPT(i, a, b) for (int i = ((a) + (b)-1); i >= (a); --i)
#define ALL(x) x.begin(), x.end()

void solve() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n), f(n);
  FOR(i, 0, n) cin >> a[i];
  FOR(i, 0, n) cin >> f[i];
  sort(ALL(a));
  sort(ALL(f));
  reverse(ALL(f));
  // for(auto x: af) cout << x.first << " " << x.second << endl; cout << endl;
  ll cost = (ll)1 << 45;
  ll cr = cost + 1, cl = -1, cm;
  while (cr - cl > 1) {
    cm = (cl + cr) / 2;
    ll sk = 0;
    FOR(i, 0, n) {
      if (a[i] * f[i] > cm) {
        ll tmp = (a[i] * f[i] - cm + f[i] - 1) / f[i];
        sk += tmp;
      }
    }
    if (sk > k)
      cl = cm;
    else
      cr = cm;
  }
  cout << cr << endl;
}

int main() {
  solve();
  return 0;
}
Problem p02883 (C++)

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

using namespace std;

const int maxn = 2e5 + 5;
int n;
long long k;
long long a[maxn], f[maxn];
bool cmp(int x, int y) { return x > y; }
bool check(long long t) {
  long long tmp = 0;
  for (int i = 0; i < n; i++) {
    tmp += max(0LL, a[i] - t / f[i]);
  }
  //    printf("%lld %lldss\n",t,tmp);
  return tmp <= k;
}
int main() {
  scanf("%d%lld", &n, &k);
  for (int i = 0; i < n; i++) {
    scanf("%lld", a + i);
  }
  for (int i = 0; i < n; i++) {
    scanf("%lld", f + i);
  }
  sort(f, f + n, cmp);
  sort(a, a + n);
  long long l = 0, r = 1LL * a[n - 1] * f[0], ans;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (check(mid)) {
      ans = mid;
      r = mid - 1;
    } else {
      l = mid + 1;
    }
  }
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int maxn = 2e5 + 5;
int n;
long long k;
long long a[maxn], f[maxn];
bool cmp(int x, int y) { return x > y; }
bool check(long long t) {
  long long tmp = 0;
  for (int i = 0; i < n; i++) {
    tmp += max(0LL, a[i] - t / f[i]);
  }
  //    printf("%lld %lldss\n",t,tmp);
  return tmp <= k;
}
int main() {
  scanf("%d%lld", &n, &k);
  for (int i = 0; i < n; i++) {
    scanf("%lld", a + i);
  }
  for (int i = 0; i < n; i++) {
    scanf("%lld", f + i);
  }
  sort(f, f + n, cmp);
  sort(a, a + n);
  long long l = 0, r = 1LL * a[n - 1] * f[0], ans;
  while (l <= r) {
    long long mid = (l + r) >> 1;
    if (check(mid)) {
      ans = mid;
      r = mid - 1;
    } else {
      l = mid + 1;
    }
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p02883 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define F first
#define S second
#define PB push_back

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
typedef priority_queue<int> HEAP;
typedef priority_queue<int, vector<int>, greater<int>> RHEAP;

const int N = 100010, M = 1010;

int n;
LL m;
LL a[N], b[N];

bool check(LL x) {
  LL cnt = 0;
  for (int i = 0; i < n; i++)
    if (a[i] * b[i] > x)
      cnt += a[i] - x / b[i];
  return cnt <= m;
}

int main() {
  scanf("%d%lld", &n, &m);
  for (int i = 0; i < n; i++)
    scanf("%lld", &a[i]);
  for (int i = 0; i < n; i++)
    scanf("%lld", &b[i]);
  sort(a, a + n);
  sort(b, b + n);
  reverse(a, a + n);

  LL l = 0LL, r = 1e12 + 10;
  while (l < r) {
    LL mid = l + r >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid + 1LL;
  }
  printf("%lld\n", r);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

#define F first
#define S second
#define PB push_back

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
typedef priority_queue<int> HEAP;
typedef priority_queue<int, vector<int>, greater<int>> RHEAP;

const int N = 200010, M = 1010;

int n;
LL m;
LL a[N], b[N];

bool check(LL x) {
  LL cnt = 0;
  for (int i = 0; i < n; i++)
    if (a[i] * b[i] > x)
      cnt += a[i] - x / b[i];
  return cnt <= m;
}

int main() {
  scanf("%d%lld", &n, &m);
  for (int i = 0; i < n; i++)
    scanf("%lld", &a[i]);
  for (int i = 0; i < n; i++)
    scanf("%lld", &b[i]);
  sort(a, a + n);
  sort(b, b + n);
  reverse(a, a + n);

  LL l = 0LL, r = 1e12 + 10;
  while (l < r) {
    LL mid = l + r >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid + 1LL;
  }
  printf("%lld\n", r);
  return 0;
}
Problem p02883 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll N, K, A[20005], F[20005];

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

  cin >> N >> K;

  for (int i = 0; i < N; i++)
    cin >> A[i];
  for (int i = 0; i < N; i++)
    cin >> F[i];

  sort(A, A + N);
  sort(F, F + N, greater<ll>());

  ll lo = 0, hi = 1e18 + 1;
  while (lo < hi) {
    ll mid = (lo + hi) / 2;
    ll train = K;
    for (int i = 0; i < N; i++) {
      if (A[i] * F[i] > mid) {
        train -= A[i] - mid / F[i];
      }
    }
    if (train < 0)
      lo = mid + 1;
    else
      hi = mid;
  }

  cout << lo << '\n';
  return 0;
}


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

ll N, K, A[200005], F[200005];

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

  cin >> N >> K;

  for (int i = 0; i < N; i++)
    cin >> A[i];
  for (int i = 0; i < N; i++)
    cin >> F[i];

  sort(A, A + N);
  sort(F, F + N, greater<ll>());

  ll lo = 0, hi = 1e18 + 1;
  while (lo < hi) {
    ll mid = (lo + hi) / 2;
    ll train = K;
    for (int i = 0; i < N; i++) {
      if (A[i] * F[i] > mid) {
        train -= A[i] - mid / F[i];
      }
    }
    if (train < 0)
      lo = mid + 1;
    else
      hi = mid;
  }

  cout << lo << '\n';
  return 0;
}
Problem p02883 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;

ll n, k, A[N], F[N];

bool chk(ll x) {
  ll t = 0;
  for (int i = 0; i < n; ++i) {
    ll m = x / F[n - i - 1];
    if (A[i] > m) {
      t += A[i] - m;
      if (t > k)
        return false;
    }
  }
  return true;
}

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

  cin >> n >> k;
  for (int i = 0; i < n; ++i)
    cin >> A[i];
  for (int i = 0; i < n; ++i)
    cin >> F[i];
  sort(A, A + n);
  sort(F, F + n);
  ll l = 0, r = 0;
  for (int i = 0; i < n; ++i)
    r = max(r, A[i] * F[n - i - 1]);
  while (l <= r) {
    ll mid = (l + r) / 2;
    if (chk(mid))
      r = mid - 1;
    else
      l = mid + 1;
  }
  cout << l << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 1e9 + 7;
const int N = 1e6 + 10;

ll n, k, A[N], F[N];

bool chk(ll x) {
  ll t = 0;
  for (int i = 0; i < n; ++i) {
    ll m = x / F[n - i - 1];
    if (A[i] > m) {
      t += A[i] - m;
      if (t > k)
        return false;
    }
  }
  return true;
}

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

  cin >> n >> k;
  for (int i = 0; i < n; ++i)
    cin >> A[i];
  for (int i = 0; i < n; ++i)
    cin >> F[i];
  sort(A, A + n);
  sort(F, F + n);
  ll l = 0, r = 0;
  for (int i = 0; i < n; ++i)
    r = max(r, A[i] * F[n - i - 1]);
  while (l <= r) {
    ll mid = (l + r) / 2;
    if (chk(mid))
      r = mid - 1;
    else
      l = mid + 1;
  }
  cout << l << endl;
  return 0;
}
Problem p02883 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, N) for (int i = 0; i < (N); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e12;
const int inf = 1e9;
const int mod = 1e9 + 7;
typedef long long ll;
typedef pair<ll, int> P;
typedef set<int> S;
ll k;
int n;
bool check(ll mid, vector<ll> a, vector<ll> b) {
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    sum += max(a[i] - mid / b[i], 0ll);
  }
  if (sum > k)
    return 0;
  else
    return 1;
}
int main() {
  cout << fixed << setprecision(10);
  cin >> n >> k;
  vector<ll> a(n), f(n);
  rep(i, n) cin >> a[i];
  rep(i, n) cin >> f[i];
  sort(a.begin(), a.end());
  sort(f.begin(), f.end());
  reverse(f.begin(), f.end());
  ll left = 0, right = (ll)1e13;
  ll mid;
  ll cnt = 0;
  while (cnt < 1000) {
    mid = (left + right) / 2;
    if (check(mid, a, f)) {
      right = mid;
    } else {
      left = mid;
    }
    cnt++;
  }
  cout << right << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, N) for (int i = 0; i < (N); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e12;
const int inf = 1e9;
const int mod = 1e9 + 7;
typedef long long ll;
typedef pair<ll, int> P;
typedef set<int> S;
ll k;
int n;
bool check(ll mid, vector<ll> a, vector<ll> b) {
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    sum += max(a[i] - mid / b[i], 0ll);
  }
  if (sum > k)
    return 0;
  else
    return 1;
}
int main() {
  cout << fixed << setprecision(10);
  cin >> n >> k;
  vector<ll> a(n), f(n);
  rep(i, n) cin >> a[i];
  rep(i, n) cin >> f[i];
  sort(a.begin(), a.end());
  sort(f.begin(), f.end());
  reverse(f.begin(), f.end());
  ll left = 0, right = (ll)1e13;
  ll mid;
  ll cnt = 0;
  while (cnt < 100) {
    mid = (left + right) / 2;
    if (check(mid, a, f)) {
      right = mid;
    } else {
      left = mid;
    }
    cnt++;
  }
  cout << right << endl;
  return 0;
}
Problem p02883 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)

int main() {
  std::ifstream in("input");
  std::cin.rdbuf(in.rdbuf());
  ll n, k, mx = 0;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) { cin >> a[i]; }
  vector<ll> f(n);
  rep(i, n) { cin >> f[i]; }
  sort(a.begin(), a.end());
  sort(f.rbegin(), f.rend());
  rep(i, n) mx = max(a[i] * f[i], mx);

  auto judge = [&](ll val) {
    ll num = 0;
    rep(i, n) {
      if (a[i] * f[i] > val)
        num += a[i] - val / f[i];
    }
    if (num > k)
      return false;
    else
      return true;
  };
  auto binarysearch = [&](ll lowerbound, ll upperbound) {
    if (!judge(lowerbound))
      return -1LL;
    ll lower = lowerbound, upper = upperbound;
    while (1) {
      if (judge((lower + upper) / 2))
        lower = (lower + upper) / 2;
      else
        upper = (lower + upper) / 2;
      if (abs(upper - lower) <= 1) {
        if (judge(upper))
          return upper;
        else
          return lower;
      }
    }
  };

  cout << binarysearch(mx, 0) << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)

int main() {

  ll n, k, mx = 0;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) { cin >> a[i]; }
  vector<ll> f(n);
  rep(i, n) { cin >> f[i]; }
  sort(a.begin(), a.end());
  sort(f.rbegin(), f.rend());
  rep(i, n) mx = max(a[i] * f[i], mx);

  auto judge = [&](ll val) {
    ll num = 0;
    rep(i, n) {
      if (a[i] * f[i] > val)
        num += a[i] - val / f[i];
    }
    if (num > k)
      return false;
    else
      return true;
  };
  auto binarysearch = [&](ll lowerbound, ll upperbound) {
    if (!judge(lowerbound))
      return -1LL;
    ll lower = lowerbound, upper = upperbound;
    while (1) {
      if (judge((lower + upper) / 2))
        lower = (lower + upper) / 2;
      else
        upper = (lower + upper) / 2;
      if (abs(upper - lower) <= 1) {
        if (judge(upper))
          return upper;
        else
          return lower;
      }
    }
  };

  cout << binarysearch(mx, 0) << endl;
  return 0;
}
Problem p02883 (C++)

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

int main() {
  long N, K;
  cin >> N >> K;
  vector<long> A(N), F(N);
  for (int i = 0; i < N; i++)
    cin >> A.at(i);
  for (int i = 0; i < N; i++)
    cin >> F.at(i);
  sort(A.begin(), A.end());
  sort(F.begin(), F.end());
  reverse(F.begin(), F.end());
  long left = -1;
  long right = 1000000000001;
  while (left < right) {
    long count = 0;
    long X = (left + right) / 2;
    for (int i = 0; i < N; i++) {
      if (A.at(i) * F.at(i) > X)
        count += A.at(i) - X / F.at(i);
    }
    if (count > K)
      left = X + 1;
    else
      right = X;
  }
  cout << right << endl;
}


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

int main() {
  long N, K;
  cin >> N >> K;
  vector<long> A(N), F(N);
  for (int i = 0; i < N; i++)
    cin >> A.at(i);
  for (int i = 0; i < N; i++)
    cin >> F.at(i);
  sort(A.begin(), A.end());
  sort(F.begin(), F.end());
  reverse(F.begin(), F.end());
  long left = 0;
  long right = 1000000000001;
  while (left < right) {
    long count = 0;
    long X = (left + right) / 2;
    for (int i = 0; i < N; i++) {
      if (A.at(i) * F.at(i) > X)
        count += A.at(i) - X / F.at(i);
    }
    if (count > K)
      left = X + 1;
    else
      right = X;
  }
  cout << right << endl;
}
Problem p02883 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n;
long long k, s = 0;
int a[100100];
int b[100100];
bool cmp(int &s1, int &s2) { return s1 > s2; }
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++)
    cin >> b[i];
  sort(a + 1, a + 1 + n);
  sort(b + 1, b + 1 + n, cmp);
  long long l = 0, r = 1e14;
  while (l < r) {
    long long mid = (l + r) / 2;
    s = 0;
    for (int i = 1; i <= n; i++)
      s += max(0LL, a[i] - mid / b[i]);
    if (s <= k)
      r = mid;
    else
      l = mid + 1;
  }
  cout << l << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n;
long long k, s = 0;
int a[1000100];
int b[1000100];
bool cmp(int &s1, int &s2) { return s1 > s2; }
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++)
    cin >> b[i];
  sort(a + 1, a + 1 + n);
  sort(b + 1, b + 1 + n, cmp);
  long long l = 0, r = 1e14;
  while (l < r) {
    long long mid = (l + r) / 2;
    s = 0;
    for (int i = 1; i <= n; i++)
      s += max(0LL, a[i] - mid / b[i]);
    if (s <= k)
      r = mid;
    else
      l = mid + 1;
  }
  cout << l << endl;
  return 0;
}
Problem p02883 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define maxn 200010

using namespace std;

long long n, k, a[maxn], f[maxn];

inline bool cmp(long long x, long long y) { return x > y; }

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

int main() {
  scanf("%lld%lld", &n, &k);
  for (register int i = 1; i <= n; ++i)
    scanf("%lld", &a[i]);
  for (register int i = 1; i <= n; ++i)
    scanf("%lld", &f[i]);
  sort(a + 1, a + n + 1);
  sort(f + 1, f + n + 1, cmp);
  long long head = 0, tail = 1e12 + 10, ans;
  while (head <= tail) {
    long long mid = (head + tail) >> 1;
    if (check(mid))
      ans = mid, tail = mid - 1;
    else
      head = head + 1;
  }
  printf("%lld", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define maxn 200010

using namespace std;

long long n, k, a[maxn], f[maxn];

inline bool cmp(long long x, long long y) { return x > y; }

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

int main() {
  scanf("%lld%lld", &n, &k);
  for (register int i = 1; i <= n; ++i)
    scanf("%lld", &a[i]);
  for (register int i = 1; i <= n; ++i)
    scanf("%lld", &f[i]);
  sort(a + 1, a + n + 1);
  sort(f + 1, f + n + 1, cmp);
  long long head = 0, tail = 1e12 + 10, ans;
  while (head <= tail) {
    long long mid = (head + tail) >> 1;
    if (check(mid))
      ans = mid, tail = mid - 1;
    else
      head = mid + 1;
  }
  printf("%lld", ans);
  return 0;
}
Problem p02883 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize("Ofast")
#include <algorithm>
#include <stdio.h>
#define maxn 200005
using namespace std;
typedef long long loli;

loli A[maxn], F[maxn];
loli N, K;

bool cal(loli x) {
  loli tmp, cnt = 0;
  for (int i = 1; i <= N; i++) {
    tmp = x / F[i];
    if (A[N - i + 1] > tmp) {
      cnt += A[N - i + 1] - tmp;
    }
  }
  return cnt <= K;
}

int main() {
  scanf("%lld%lld", &N, &K);
  for (int i = 1; i <= N; i++) {
    scanf("%lld", A + i);
  }
  for (int i = 1; i <= N; i++) {
    scanf("%lld", F + i);
  }
  sort(A + 1, A + N + 1);
  sort(F + 1, F + N + 1);
  loli L = -1, R = 1e12;
  while (R - L > 1) {
    int M = (L + R) >> 1;
    if (cal(M))
      R = M;
    else
      L = M;
  }
  printf("%lld\n", R);
  return 0;
}


Fixed submission:
#pragma GCC optimize("Ofast")
#include <algorithm>
#include <stdio.h>
#define maxn 200005
using namespace std;
typedef long long loli;

loli A[maxn], F[maxn];
loli N, K;

bool cal(loli x) {
  loli tmp, cnt = 0;
  for (int i = 1; i <= N; i++) {
    tmp = x / F[i];
    if (A[N - i + 1] > tmp) {
      cnt += A[N - i + 1] - tmp;
    }
  }
  return cnt <= K;
}

int main() {
  scanf("%lld%lld", &N, &K);
  for (int i = 1; i <= N; i++) {
    scanf("%lld", A + i);
  }
  for (int i = 1; i <= N; i++) {
    scanf("%lld", F + i);
  }
  sort(A + 1, A + N + 1);
  sort(F + 1, F + N + 1);
  loli L = -1, R = 1e12;
  while (R - L > 1) {
    loli M = (L + R) >> 1;
    if (cal(M))
      R = M;
    else
      L = M;
  }
  printf("%lld\n", R);
  return 0;
}
Problem p02883 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int INF = 1001001001;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(0);
  vector<int> f(0);
  for (int i = 0; i < n; i++) {
    int input;
    cin >> input;
    a.push_back(input);
  }
  for (int i = 0; i < n; i++) {
    int input;
    cin >> input;
    f.push_back(input);
  }
  sort(a.begin(), a.end());
  sort(f.rbegin(), f.rend());
  ll l = -1, r = 1e12;
  while (l + 1 < r) {
    ll c = (l + r) / 2;
    bool ok = [&] {
      ll s = 0;
      for (int i = 0; i < n; i++) {
        s += max(0ll, a.at(i) - (c / f.at(i)));
      }
      return s <= k;
    }();
    if (ok)
      r = c;
    else
      l = c;
  }
  cout << r << endl;
}

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

const int INF = 1001001001;

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<int> a(0);
  vector<int> f(0);
  for (int i = 0; i < n; i++) {
    int input;
    cin >> input;
    a.push_back(input);
  }
  for (int i = 0; i < n; i++) {
    int input;
    cin >> input;
    f.push_back(input);
  }
  sort(a.begin(), a.end());
  sort(f.rbegin(), f.rend());
  ll l = -1, r = 1e12;
  while (l + 1 < r) {
    ll c = (l + r) / 2;
    bool ok = [&] {
      ll s = 0;
      for (int i = 0; i < n; i++) {
        s += max(0ll, a.at(i) - (c / f.at(i)));
      }
      return s <= k;
    }();
    if (ok)
      r = c;
    else
      l = c;
  }
  cout << r << endl;
}
Problem p02883 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ll long long int
const int INF = 2147483647;
const ll MOD = 1000000007;

using namespace std;

int main() {
  int n;
  ll k;
  cin >> n >> k;

  vector<ll> a(n);
  vector<ll> f(n);
  REP(i, n) { cin >> a[i]; }
  REP(i, n) { cin >> f[i]; }

  sort(a.begin(), a.end());
  sort(f.begin(), f.end(), greater<ll>());

  ll ng = -1;
  ll ok = 1000000002000;

  while (abs(ok - ng) > 1) {
    int mid = (ok + ng) / 2;
    ll c = 0;

    REP(i, n) {
      if (a[i] * f[i] <= mid)
        continue;
      c += (a[i] * f[i] - mid + f[i] - 1) / f[i];
    }
    if (c <= k) {
      ok = mid;
    } else {
      ng = mid;
    }
  }

  cout << ok << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ll long long int
const int INF = 2147483647;
const ll MOD = 1000000007;

using namespace std;

int main() {
  int n;
  ll k;
  cin >> n >> k;

  vector<ll> a(n);
  vector<ll> f(n);
  REP(i, n) { cin >> a[i]; }
  REP(i, n) { cin >> f[i]; }

  sort(a.begin(), a.end());
  sort(f.begin(), f.end(), greater<ll>());

  ll ng = -1;
  ll ok = 1000000002000;

  while (abs(ok - ng) > 1) {
    ll mid = (ok + ng) / 2;
    ll c = 0;

    REP(i, n) {
      if (a[i] * f[i] <= mid)
        continue;
      c += (a[i] * f[i] - mid + f[i] - 1) / f[i];
    }
    if (c <= k) {
      ok = mid;
    } else {
      ng = mid;
    }
  }

  cout << ok << endl;
}
Problem p02883 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 50;

int n, a[maxn], f[maxn];
ll k, le, ri;

bool check(ll x) {
  ll cnt = k;
  for (int i = 0; i < n; ++i) {
    if (1LL * a[i] * f[i] <= x)
      continue;
    ll need = 1LL * a[i] - x / f[i];
    if (cnt >= need)
      cnt -= need;
    else
      return false;
  }
  return true;
}

int main() {
  scanf("%d%lld", &n, &k);
  for (int i = 0; i < n; ++i)
    scanf("%d", &a[i]);
  for (int i = 0; i < n; ++i)
    scanf("%d", &f[i]);
  sort(a, a + n);
  sort(f, f + n, greater<int>());
  le = -1;
  for (int i = 0; i < n; ++i) {
    ri += 1LL * a[i] * f[i];
  }
  while (le + 1 < ri) {
    ll mid = (le + ri) >> 1;
    if (check(mid))
      ri = mid;
    else
      le = mid;
  }
  printf("%lld\n", ri);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 50;

int n, a[maxn], f[maxn];
ll k, le, ri;

bool check(ll x) {
  ll cnt = k;
  for (int i = 0; i < n; ++i) {
    if (1LL * a[i] * f[i] <= x)
      continue;
    ll need = 1LL * a[i] - x / f[i];
    if (cnt >= need)
      cnt -= need;
    else
      return false;
  }
  return true;
}

int main() {
  scanf("%d%lld", &n, &k);
  for (int i = 0; i < n; ++i)
    scanf("%d", &a[i]);
  for (int i = 0; i < n; ++i)
    scanf("%d", &f[i]);
  sort(a, a + n);
  sort(f, f + n, greater<int>());
  le = -1;
  for (int i = 0; i < n; ++i) {
    ri += 1LL * a[i] * f[i];
  }
  while (le + 1 < ri) {
    ll mid = (le + ri) >> 1;
    if (check(mid))
      ri = mid;
    else
      le = mid;
  }
  printf("%lld\n", ri);
  return 0;
}
Problem p02883 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
const int maxn = 1e5 + 10;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
ll a[maxn], f[maxn];
int n;
ll k;
bool check(ll x) {
  ll sum = k;
  for (int i = 0; i < n; i++) {
    ll now = a[i] * f[i];
    if (now > x) {
      ll num = (now - x) / f[i];
      if ((now - x) % f[i])
        num++;
      sum -= num;
    }
  }
  if (sum < 0)
    return false;
  else
    return true;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    cin >> f[i];
  sort(a, a + n);
  sort(f, f + n, greater<ll>());
  ll l = 0, r = 1e15;
  while (l + 1 < r) {
    ll mid = (l + r) / 2;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  if (check(l))
    cout << l << '\n';
  else
    cout << r << '\n';
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
const int maxn = 2e5 + 10;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
ll a[maxn], f[maxn];
int n;
ll k;
bool check(ll x) {
  ll sum = k;
  for (int i = 0; i < n; i++) {
    ll now = a[i] * f[i];
    if (now > x) {
      ll num = (now - x) / f[i];
      if ((now - x) % f[i])
        num++;
      sum -= num;
    }
  }
  if (sum < 0)
    return false;
  else
    return true;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    cin >> f[i];
  sort(a, a + n);
  sort(f, f + n, greater<ll>());
  ll l = 0, r = 1e15;
  while (l + 1 < r) {
    ll mid = (l + r) / 2;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  if (check(l))
    cout << l << '\n';
  else
    cout << r << '\n';
  return 0;
}
Problem p02883 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;
typedef long long LL;
#define exp double(1e-6)
#define PI acos(-1)
const int MAXN = int(1e5 + 5);
int n;
LL k, K;
int a[MAXN], f[MAXN];
int a2[MAXN], f2[MAXN];
bool cmp(int a, int b) { return a > b; }
void Debug() {
  for (int i = 1; i <= n; i++)
    printf("%d ", a[i]);
  puts("");
  for (int i = 1; i <= n; i++)
    printf("%d ", f[i]);
  puts("");
}
bool Check(LL x) {
  for (int i = 1; i <= n; i++) {
    if ((LL)a[i] * f[i] <= x)
      continue;
    else
      k -= ((LL)a[i] * f[i] - x + f[i] - 1) / f[i];
  }
  return k >= 0;
}
LL BS() {
  LL l = 0, r = (LL)a[n] * f[1];
  while (l + 1 < r) {
    k = K;
    LL mid = (l + r) / 2;
    if (Check(mid))
      r = mid;
    else
      l = mid;
  }
  k = K;
  return Check(l) ? l : r;
}
int main() {
  scanf("%d%lld", &n, &k);
  K = k;
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++)
    scanf("%d", &f[i]);
  sort(a + 1, a + n + 1);
  sort(f + 1, f + n + 1, cmp);
  // Debug();
  printf("%lld", BS());
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;
typedef long long LL;
#define exp double(1e-6)
#define PI acos(-1)
const int MAXN = int(2e5 + 5);
int n;
LL k, K;
int a[MAXN], f[MAXN];
int a2[MAXN], f2[MAXN];
bool cmp(int a, int b) { return a > b; }
void Debug() {
  for (int i = 1; i <= n; i++)
    printf("%d ", a[i]);
  puts("");
  for (int i = 1; i <= n; i++)
    printf("%d ", f[i]);
  puts("");
}
bool Check(LL x) {
  for (int i = 1; i <= n; i++) {
    if ((LL)a[i] * f[i] <= x)
      continue;
    else
      k -= ((LL)a[i] * f[i] - x + f[i] - 1) / f[i];
  }
  return k >= 0;
}
LL BS() {
  LL l = 0, r = (LL)a[n] * f[1];
  while (l + 1 < r) {
    k = K;
    LL mid = (l + r) / 2;
    if (Check(mid))
      r = mid;
    else
      l = mid;
  }
  k = K;
  return Check(l) ? l : r;
}
int main() {
  scanf("%d%lld", &n, &k);
  K = k;
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++)
    scanf("%d", &f[i]);
  sort(a + 1, a + n + 1);
  sort(f + 1, f + n + 1, cmp);
  // Debug();
  printf("%lld", BS());
}
Problem p02883 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define PI acos(-1)

using namespace std;
using ll = long long;
using P = pair<int, int>;
using LP = pair<ll, ll>;

ll max(ll a, ll b) {
  if (a > b)
    return a;
  return b;
}

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

  int N, K;
  cin >> N >> K;
  vector<ll> as(N);
  vector<ll> fs(N);
  rep(i, N) cin >> as[i];
  rep(i, N) cin >> fs[i];

  sort(as.begin(), as.end());
  sort(fs.begin(), fs.end());
  reverse(fs.begin(), fs.end());

  ll l = -1;
  ll r = 1001001001001;
  while (l + 1 < r) {
    ll m = (l + r) / 2;
    ll k = 0;
    rep(i, N) { k += max(0, as[i] - m / fs[i]); }
    if (k > K)
      l = m;
    else
      r = m;
  }
  cout << r << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define PI acos(-1)

using namespace std;
using ll = long long;
using P = pair<int, int>;
using LP = pair<ll, ll>;

ll max(ll a, ll b) {
  if (a > b)
    return a;
  return b;
}

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

  ll N, K;
  cin >> N >> K;
  vector<ll> as(N);
  vector<ll> fs(N);
  rep(i, N) cin >> as[i];
  rep(i, N) cin >> fs[i];

  sort(as.begin(), as.end());
  sort(fs.begin(), fs.end());
  reverse(fs.begin(), fs.end());

  ll l = -1;
  ll r = 1001001001001;
  while (l + 1 < r) {
    ll m = (l + r) / 2;
    ll k = 0;
    rep(i, N) { k += max(0, as[i] - m / fs[i]); }
    if (k > K)
      l = m;
    else
      r = m;
  }
  cout << r << endl;
}
Problem p02883 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define ll long long
const int maxn = 1e6 + 5;
ll a[maxn], f[maxn];

int main() {

  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    cin >> f[i];
  sort(a, a + n);
  sort(f, f + n, greater<ll>());

  ll l = 0, r = 1e12;
  while (l < r) {
    ll x = (l + r) >> 1;
    ll need = 0;
    for (int i = 0; i < n; i++) {
      need += max(0ll, a[i] - x / f[i]);
    }
    if (need <= k) {
      r = x;
    } else {
      l = x + 1;
    }
  }
  cout << r << endl;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define ll long long
const int maxn = 1e6 + 5;
ll a[maxn], f[maxn];

int main() {

  ios::sync_with_stdio(false);
  cin.tie(0);

  ll n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    cin >> f[i];
  sort(a, a + n);
  sort(f, f + n, greater<ll>());

  ll l = 0, r = 1e12;
  while (l < r) {
    ll x = (l + r) >> 1;
    ll need = 0;
    for (int i = 0; i < n; i++) {
      need += max(0ll, a[i] - x / f[i]);
    }
    if (need <= k) {
      r = x;
    } else {
      l = x + 1;
    }
  }
  cout << r << endl;
}
Problem p02883 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> P;

int main() {
  LL N, K;
  cin >> N >> K;
  vector<LL> a(N), f(N);
  for (int i = 0; i < N; i++)
    cin >> a[i];
  for (int i = 0; i < N; i++)
    cin >> f[i];
  sort(a.begin(), a.end());
  sort(f.rbegin(), f.rend());
  vector<P> pp(N);
  for (int i = 0; i < N; i++) {
    pp[i] = P(a[i] * f[i], f[i]);
  }
  LL cnt2 = 0;
  for (int i = 0; i < N; i++)
    cnt2 += a[i];
  sort(pp.begin(), pp.end());
  vector<LL> p2(N);
  for (int i = 0; i < N; i++)
    p2[i] = pp[i].first;
  LL l = 0, r = 1e12;
  while (l + 1 < r) {
    int mm = (l + r) / 2;
    auto itr = lower_bound(p2.begin(), p2.end(), mm);
    int id = itr - p2.begin();
    if (itr == p2.end()) {
      r = mm;
    } else {
      LL cnt = 0;
      for (int i = id; i < N; i++) {
        cnt += (p2[i] - mm + pp[i].second - 1) / (pp[i].second);
      }
      if (cnt <= K)
        r = mm;
      else
        l = mm;
    }
  }
  if (cnt2 <= K)
    cout << 0 << endl;
  else
    cout << r << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> P;

int main() {
  LL N, K;
  cin >> N >> K;
  vector<LL> a(N), f(N);
  for (int i = 0; i < N; i++)
    cin >> a[i];
  for (int i = 0; i < N; i++)
    cin >> f[i];
  sort(a.begin(), a.end());
  sort(f.rbegin(), f.rend());
  vector<P> pp(N);
  for (int i = 0; i < N; i++) {
    pp[i] = P(a[i] * f[i], f[i]);
  }
  LL cnt2 = 0;
  for (int i = 0; i < N; i++)
    cnt2 += a[i];
  sort(pp.begin(), pp.end());
  vector<LL> p2(N);
  for (int i = 0; i < N; i++)
    p2[i] = pp[i].first;
  LL l = 0, r = 1e12;
  while (l + 1 < r) {
    LL mm = (l + r) / 2;
    auto itr = lower_bound(p2.begin(), p2.end(), mm);
    int id = itr - p2.begin();
    if (itr == p2.end()) {
      r = mm;
    } else {
      LL cnt = 0;
      for (int i = id; i < N; i++) {
        cnt += (p2[i] - mm + pp[i].second - 1) / (pp[i].second);
      }
      if (cnt <= K)
        r = mm;
      else
        l = mm;
    }
  }
  if (cnt2 <= K)
    cout << 0 << endl;
  else
    cout << r << endl;
}
Problem p02883 (C++)

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

using namespace std;

long long n, a[200002], v[200002];
long long k;

bool ok(long long x) {
  long long sum = 0;
  for (int i = 1; i <= n; i++) {
    long long t = x / v[i];
    sum += max(0ll, a[i] - t);
  }
  if (sum <= k)
    return 1;
  else
    return 0;
}

int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++)
    cin >> v[i];
  sort(a + 1, a + n + 1);
  sort(v + 1, v + n + 1);
  reverse(v + 1, v + n + 1);
  long long st = 0, dr = 0, ans;
  for (int i = 1; i <= n; i++)
    dr = max(dr, a[i] * v[i]);
  while (st <= dr) {
    int mij = (st + dr) / 2;
    if (ok(mij)) {
      ans = mij;
      dr = mij - 1;
    } else
      st = mij + 1;
  }
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

long long n, a[200002], v[200002];
long long k;

bool ok(long long x) {
  long long sum = 0;
  for (int i = 1; i <= n; i++) {
    long long t = x / v[i];
    sum += max(0ll, a[i] - t);
  }
  if (sum <= k)
    return 1;
  else
    return 0;
}

int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++)
    cin >> v[i];
  sort(a + 1, a + n + 1);
  sort(v + 1, v + n + 1);
  reverse(v + 1, v + n + 1);
  long long st = 0, dr = 0, ans;
  for (int i = 1; i <= n; i++)
    dr = max(dr, a[i] * v[i]);
  while (st <= dr) {
    long long mij = (st + dr) / 2;
    if (ok(mij)) {
      ans = mij;
      dr = mij - 1;
    } else
      st = mij + 1;
  }
  cout << ans;
  return 0;
}
Problem p02884 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
using ll = long long;

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> edg(n), eid(n);
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    edg[u].push_back(v);
    eid[u].push_back(i);
  }
  double ans = 1e18;
  vector<double> dp(n);
  for (int i = -1; i < m; i++) {
    for (int j = 0; j < n; j++)
      dp[j] = 0.;
    for (int j = n - 2; j >= 0; j--) {
      int c = edg[j].size();
      double ma = 0.;
      for (int t = 0; t < c; t++) {
        dp[j] += dp[edg[j][t]];
        if (edg[j][t] != n - 1) {
          ma = max(ma, dp[edg[j][t]]);
        }
      }
      if (j == i && c > 1) {
        dp[j] -= ma;
        dp[j] /= (c - 1);
      } else {
        dp[j] /= c;
      }
      dp[j]++;
    }
    ans = min(ans, dp[0]);
  }
  printf("%.15f\n", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
using ll = long long;

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> edg(n), eid(n);
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    edg[u].push_back(v);
    eid[u].push_back(i);
  }
  double ans = 1e18;
  vector<double> dp(n);
  for (int i = -1; i < n; i++) {
    for (int j = 0; j < n; j++)
      dp[j] = 0.;
    for (int j = n - 2; j >= 0; j--) {
      int c = edg[j].size();
      double ma = 0.;
      for (int t = 0; t < c; t++) {
        dp[j] += dp[edg[j][t]];
        if (edg[j][t] != n - 1) {
          ma = max(ma, dp[edg[j][t]]);
        }
      }
      if (j == i && c > 1) {
        dp[j] -= ma;
        dp[j] /= (c - 1);
      } else {
        dp[j] /= c;
      }
      dp[j]++;
    }
    ans = min(ans, dp[0]);
  }
  printf("%.15f\n", ans);
  return 0;
}
Problem p02884 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define all(V) V.begin(), V.end()
#define ll long long
#define MOD 1000000007

vector<int> G[610];
pair<int, int> E[100000];
double cost[610], les[610];

int main() {
  int N, M, s, t, sz;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    cin >> s >> t;
    G[s].push_back(t);
    E[i] = {s, t};
  }
  for (int i = N; i > 0; i--) {
    cost[i] = 1.0;
    //(接続先の期待値+1)*(そこに進む確率)
    for (int j = i - 1; j > 0; j--) {
      sz = G[j].size();
      for (int k : G[j]) {
        cost[j] += cost[k] / sz;
      }
    }
    les[i] = cost[1];
    for (int j = 1; j <= N; j++)
      cost[j] = 0.0;
  }

  for (int i = N; i > 0; i--) {
    sz = G[i].size();
    for (int k : G[i]) {
      cost[i] += (cost[k] + 1) / sz;
    }
  }

  double ans = 0, tp;
  for (int i = 0; i < M; i++) {
    tie(s, t) = E[i];
    sz = G[s].size();
    if (sz == 1)
      continue;
    tp = cost[s] - (cost[t] + 1.0) / sz;
    tp *= (double)sz / (sz - 1);
    tp = (cost[s] - tp) * les[s];
    ans = max(tp, ans);
  }
  printf("%.10f", cost[1] - ans);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define all(V) V.begin(), V.end()
#define ll long long
#define MOD 1000000007

vector<int> G[610];
pair<int, int> E[200000];
double cost[610], les[610];

int main() {
  int N, M, s, t, sz;
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    cin >> s >> t;
    G[s].push_back(t);
    E[i] = {s, t};
  }
  for (int i = N; i > 0; i--) {
    cost[i] = 1.0;
    //(接続先の期待値+1)*(そこに進む確率)
    for (int j = i - 1; j > 0; j--) {
      sz = G[j].size();
      for (int k : G[j]) {
        cost[j] += cost[k] / sz;
      }
    }
    les[i] = cost[1];
    for (int j = 1; j <= N; j++)
      cost[j] = 0.0;
  }

  for (int i = N; i > 0; i--) {
    sz = G[i].size();
    for (int k : G[i]) {
      cost[i] += (cost[k] + 1) / sz;
    }
  }

  double ans = 0, tp;
  for (int i = 0; i < M; i++) {
    tie(s, t) = E[i];
    sz = G[s].size();
    if (sz == 1)
      continue;
    tp = cost[s] - (cost[t] + 1.0) / sz;
    tp *= (double)sz / (sz - 1);
    tp = (cost[s] - tp) * les[s];
    ans = max(tp, ans);
  }
  printf("%.10f", cost[1] - ans);
}
Problem p02884 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <vector>

using namespace std;

vector<int> e[611];
double dis1[611], dis0[611];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; ++i) {
    int t1, t2;
    scanf("%d%d", &t1, &t2);
    e[t1].push_back(t2);
  }
  for (int i = 0; i < m; ++i)
    dis0[i] = dis1[i] = 0;

  for (int i = n - 1; i > 0; --i) {
    for (int j = 0; j < e[i].size(); ++j) {
      dis0[i] += dis0[e[i][j]];
    }
    if (!e[i].size())
      continue;
    dis0[i] /= (1.0 * e[i].size());
    dis0[i] += 1.0;
    dis1[i] = dis0[i];
  }
  double ans = dis0[1];
  for (int i = 1; i < n; ++i) {
    double tmp_max = 0;
    if (e[i].size() == 1)
      continue;
    dis1[i] = 0;
    for (int j = 0; j < e[i].size(); ++j) {
      if (dis1[e[i][j]] > tmp_max)
        tmp_max = dis1[e[i][j]];
      dis1[i] += dis1[e[i][j]];
    }
    dis1[i] -= tmp_max;
    dis1[i] /= (1.0 * e[i].size() - 1.0);
    dis1[i] += 1.0;
    for (int j = i - 1; j > 0; --j) {
      dis1[j] = 0;
      for (int k = 0; k < e[j].size(); ++k)
        dis1[j] += dis1[e[j][k]];
      if (!e[j].size())
        continue;
      dis1[j] /= (1.0 * e[j].size());
      dis1[j] += 1.0;
    }
    if (dis1[1] < ans)
      ans = dis1[1];
  }
  printf("%.8lf", ans);
}


Fixed submission:
#include <stdio.h>
#include <vector>

using namespace std;

vector<int> e[611];
double dis1[611], dis0[611];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; ++i) {
    int t1, t2;
    scanf("%d%d", &t1, &t2);
    e[t1].push_back(t2);
  }
  for (int i = 0; i <= n; ++i)
    dis0[i] = dis1[i] = 0;

  for (int i = n - 1; i > 0; --i) {
    for (int j = 0; j < e[i].size(); ++j) {
      dis0[i] += dis0[e[i][j]];
    }
    if (!e[i].size())
      continue;
    dis0[i] /= (1.0 * e[i].size());
    dis0[i] += 1.0;
    dis1[i] = dis0[i];
  }
  double ans = dis0[1];
  for (int i = 1; i < n; ++i) {
    double tmp_max = 0;
    if (e[i].size() == 1)
      continue;
    dis1[i] = 0;
    for (int j = 0; j < e[i].size(); ++j) {
      if (dis1[e[i][j]] > tmp_max)
        tmp_max = dis1[e[i][j]];
      dis1[i] += dis1[e[i][j]];
    }
    dis1[i] -= tmp_max;
    dis1[i] /= (1.0 * e[i].size() - 1.0);
    dis1[i] += 1.0;
    for (int j = i - 1; j > 0; --j) {
      dis1[j] = 0;
      for (int k = 0; k < e[j].size(); ++k)
        dis1[j] += dis1[e[j][k]];
      if (!e[j].size())
        continue;
      dis1[j] /= (1.0 * e[j].size());
      dis1[j] += 1.0;
    }
    if (dis1[1] < ans)
      ans = dis1[1];
  }
  printf("%.8lf", ans);
}