Untitled

mail@pastecode.io avatar
unknown
c_cpp
a year ago
1.5 kB
0
Indexable
Never
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

constexpr int SIZE = (1e+5);

ll Items[SIZE];
ll L[SIZE];

int n; ll m;

ll binary(ll target, ll level, ll items) {
    ll lo = 0, hi = items;
    while (lo <= hi) {
        ll mid = lo + (hi - lo) / 2;
        ll required_items = (mid) * (level + level + mid - 1) / 2;
        if (required_items >= target) {
            hi = mid - 1;
        } else {
            lo = mid + 1;
        }
    }
    return lo;
}

void sol() {

    long long lo = 1;
    long long hi = (1e18);
    long long ans = -1;
    while (lo + 1 < hi) {
        ll mid = lo + (hi - lo) / 2;
        ll cost = 0;
        for (int i = 0; i < n; i++) {
            if (mid > L[i]) {
                ll K = binary(mid - L[i], L[i], Items[i]); // 현재 아이템으로 키울 수 있는 레벨의 최댓값 
                if(L[i] + K >= mid){
                    continue;
                }
                cost += mid - L[i] - K; // 필요한 성장 비뱍의 개수 
            }
        }
        if (cost <= m && mid > 0) {
            lo = mid + 1;
            ans = mid;
        } else {
            hi = mid - 1;
        }
    }
    cout << ans << '\n';
}

int main(void) {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        cin >> L[i];
    }
    for (int i = 0; i < n; i++) {
        cin >> Items[i];
    }
    sol();
    return 0;
}