#include <bits/stdc++.h>
using namespace std;
#define int int64_t
#define ar array
#define all(v) begin(v), end(v)
#define allr(v) rbegin(v), rend(v)
#define pb push_back
const int INF = 1e10;
void solve() {
int n, m; cin >> n >> m;
vector<int> a(n), b(n);
for (int &x : a) cin >> x;
for (int &x : b) cin >> x;
for (int i = 0; i < n; i++) a[i] = max(a[i], b[i]);
vector<int> ind(n);
iota(all(ind), 0);
sort(all(ind), [&](int i, int j){
if (a[i] == a[j]) return b[j] < b[i];
return a[i] < a[j];
});
auto joke = [&](int ans) {
vector<int> nigga(n);
for (int i = 0; i < n; i++) nigga[i] = a[i] * m;
int i = 0, j = n-1;
while (nigga[ind[i]] < ans && i < n && j >= 0) {
while (nigga[ind[j]] - a[ind[j]] >= ans && nigga[ind[i]] < ans) {
nigga[ind[j]] -= a[ind[j]];
nigga[ind[i]] += b[ind[i]];
}
if (nigga[ind[j]] - a[ind[j]] < ans) j--;
if (nigga[ind[i]] >= ans) i++;
}
// cout << ans << ":";
// for (int x : nigga) cout << x << ' ';
// cout << '\n';
for (int x : nigga) if (x < ans) return false;
return true;
};
int l = 0, r = 1e18;
while (r-l > 1) {
int mid = (l + r) / 2;
if (joke(mid)) l = mid;
else r = mid;
}
cout << l << '\n';
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1; //cin >> t;
while (t--) {
solve();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |