This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 3e5 + 10;
const ll INF = 1e18;
ll n, m;
ll A[N], B[N];
// bool check1(ll x) {
// vector<ll> C(n + 1);
// vector<ll> val(n + 1);
// for(int i = 1; i <= n; i++) {
// C[i] = max(A[i], B[i]);
// }
// ll t = 0;
// bool us[n + 1] = {};
// while(1) {
// int mx = -1;
// for(int i = 1; i <= n; i++) {
// if(us[i]) continue;
// if(mx == -1 || (max(x - val[i], 0ll) + C[i] - 1) / C[i] < (max(x - val[mx], 0ll) + C[mx] - 1) / C[mx])
// mx = i;
// }
// if(mx == -1) break;
// ll k = (max(x - val[mx], 0ll) + C[mx] - 1) / C[mx];
// t += k;
// us[mx] = 1;
// val[mx] += k * C[mx];
// if(t > m) return 0;
// int mn = -1;
// for(int i = 1; i <= n; i++) {
// if(us[i]) continue;
// val[i] += k * C[i];
// if(mn == -1 || (max(x - val[i], 0ll) + C[i] - 1) / C[i] > (max(x - val[mn], 0ll) + C[mn] - 1) / C[mn])
// mn = i;
// }
// if(mn != -1) C[mn] += B[mn];
// }
// return 1;
// }
bool check(ll x) {
vector<ll> C(n + 1);
vector<ll> val(n + 1), ti(n + 1);
set<pair<ll, int>> st;
for(int i = 1; i <= n; i++) {
C[i] = max(A[i], B[i]);
st.insert({ti[i] + (max(x - val[i], 0ll) + C[i] - 1) / C[i], i});
}
ll t = 0;
while(!st.empty()) {
int i = (st.begin())->second;
ll t = (st.begin())->first;
if(t > m) return 0;
val[i] += (t - ti[i]) * C[i];
ti[i] = t;
C[i] = 0;
st.erase(st.begin());
if(!st.empty()) {
int j = (--st.end())->second;
st.erase(--st.end());
val[j] += (t - ti[j]) * C[j];
ti[j] = t;
C[j] += B[j];
st.insert({ti[j] + (max(x - val[j], 0ll) + C[j] - 1) / C[j], j});
}
}
return 1;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin >> n >> m;
for(int i = 1; i <= n; i++)
cin >> A[i];
for(int i = 1; i <= n; i++)
cin >> B[i];
ll l = 0, r = INF;
while(r - l > 1) {
ll mid = (l + r) / 2;
if(check(mid)) l = mid;
else r = mid;
}
cout << l;
}
Compilation message (stderr)
Main.cpp: In function 'bool check(ll)':
Main.cpp:53:8: warning: unused variable 't' [-Wunused-variable]
53 | ll t = 0;
| ^| # | 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... |