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;
using ull = unsigned long long;
using lld = long double;
// #ifndef ONLINE_JUDGE
// #include "E:\Personal\cpp_codes\debugalgo.h"
// #else
// #define debug(x)
// #endif
const ll inf = 1e16;
void solve(int tc)
{
ll n,m;
cin>>n>>m;
ll mx = 0;
vector<pair<ll,ll>> a(n);
for(int i=0; i<n; i++)
{
cin>>a[i].first;
mx = max(mx, a[i].first*m);
}
for(int i=0; i<n; i++) {
cin>>a[i].second;
mx = max(mx,a[i].second*m);
}
vector<pair<ll,ll>> largerA;
vector<ll> largerB;
for(int i=0; i<n; i++)
{
if(a[i].first>a[i].second)
{
largerA.push_back(a[i]);
}
else
{
largerB.push_back(a[i].second);
}
}
auto feasible = [&](ll mid)
{
ll carry = 0;
for(auto &x: largerB)
{
ll need = (mid + x-1)/x;
carry += (m-need);
if(carry<=-inf) return 0;
}
for(auto &[x,y]: largerA)
{
if(m>=(mid+x-1)/x)
{
carry+= (m - (mid+x-1)/x);
}
else
{
ll need = ((mid - x*m) + y - 1)/y;
carry -= need;
if(carry<=-inf) return 0;
}
}
return int(carry>=0);
};
ll l = 1;
ll r = mx;
ll ans = 0;
while(l<=r)
{
ll mid = l + (r-l)/2;
if(feasible(mid))
{
ans = mid;
l = mid + 1;
}
else r = mid - 1;
}
cout<<ans<<"\n";
}
int main()
{
ios_base:: sync_with_stdio(0);
cin.tie(0);
// #ifndef ONLINE_JUDGE
// freopen("Error.txt", "w", stderr);
// #endif
int tt = 1;
// cin >> tt;
for(int tc=1; tc<=tt; tc++)
{
solve(tc);
}
}
# | 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... |