제출 #1329568

#제출 시각아이디문제언어결과실행 시간메모리
1329568Valters07Self Study (JOI22_ho_t2)C++20
100 / 100
136 ms2788 KiB
#include <bits/stdc++.h>
#define fio ios_base::sync_with_stdio(0);cin.tie(0);
#define ll long long
#define pb push_back
#define fi first
#define se second
#define en exit(0);
using namespace std;
const int N = 3e5 + 5;
int a[N], b[N];
bool can(ll mn, int n, int m)
{
    ll need = 0, have = 1ll * n * m;
    // need - how many b[i] choices we need
    // have - how many b[i] choices we have left
    for(int i = 1;i <= n;i++)
    {
        if(a[i] > b[i])
        {
            ll op = (mn + a[i] - 1) / a[i];
            // op - how many a[i]'s to have taken >= mn in total
            ll take = min(1ll * m, op);
            //take - can't take more than m a[i]'s
            have -= take;
            ll lft = max(0ll, mn - 1ll * a[i] * take);
            // lft - how much we have left after taking the a[i]'s
            need += (lft + b[i] - 1) / b[i];
        }
        else
            need += (mn + b[i] - 1) / b[i];
        if(need > n * m)
            return 0;
    }
    return (have >= need);
}
int main()
{
    fio
//    ifstream cin("in.in");
    int n, m;
    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 = 1, r = 1e18;
    while(l < r)
    {
        ll mid = (l + r + 1) / 2;
        if(can(mid, n, m))
            l = mid;
        else
            r = mid - 1;
    }
    cout << l;
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...