We are asked to determine the maximum number of cookies Apollinaria can bake.
Given:
- n ingredients, each cookie requires a_i grams of ingredient i.
- Initially, have b_i grams of ingredient i.
- Additionally, have k grams of magic powder, each gram can become 1 gram of any ingredient (i.e., can allocate powder to supplement deficits).
We need to maximize number of cookies x, an integer, such that we can fulfill the total required amounts: For each ingredient i, we need a_i * x grams. We have b_i grams. The deficit, if positive, is a_i * x - b_i if that > 0. Total deficit across all ingredients must be covered by the magic powder, but note that powder can be allocated arbitrarily to ingredients. So the condition is: sum over i of max(0, a_i * x - b_i) <= k.
Additionally, we cannot have negative leftover? Actually we don't need to use excess b_i. So condition is exactly that.
We need to find maximum integer x satisfying that.
Constraints: n,k up to 1000 (simplified version). But the statement says there are two versions with small and large constraints. Possibly the large version has larger n,k up to 1e9? Actually typical Codeforces problem (460A? Actually it's from Codeforces Round #454 maybe? This problem is "Cookies" from Codeforces Round #... I recall problem: "Bake Cookies" or "Apollinaria and Cookie"? Actually I think it's "A. Cookies" but not sure. The usual constraints: n up to 1e5, k up to 1e9? Actually there is Codeforces problem 371C? No. I recall a problem "A. Cooking" hmm. But given here they mention two versions: one simplified (n,k <= 1000) and another large constraints. So we need to produce solution that works for both? It says: "If you can solve this problem in large constraints, then you can just write a single solution to the both versions. If you find the problem too difficult in large constraints, you can write solution to the simplified version only."
Thus we should aim to solve for large constraints. What might large constraints be? Usually in such problem, n can be up to 100,000 or maybe 1e5, k up to 1e9 or 1e12, and a_i,b_i up to 1e9. But not specified. To be safe, we can design solution that uses binary search on answer and checks condition in O(n). This is O(n log maxX). MaxX can be up to something like (max b_i + k) / min a_i, which could be up to 2e9 or 1e12, but binary search on 64-bit integer is fine. So we need to implement binary search.
We need to compute sum of deficits. However careful about overflow: when computing a_i * x, product might exceed 32-bit int. Use 64-bit.
So plan:
- Read n, k.
- Read array a (size n) and b (size n).
- Define a function can(x): computes total_needed = sum_i max(0, a_i * x - b_i). If total_needed <= k, return true; else false.
- Binary search for maximum x.
Lower bound: 0? Possibly 0 is always possible. Upper bound: can be something like max possible. Estimate: since k and b_i and a_i could be up to 1e9 or more, upper bound should be something like (max(b_i) + k) / min(a_i) + 1. But we can set an initial high to something like 2e9? Actually safe: Since constraints unknown, but maybe up to 2e9? Actually if all b_i = 1e9, a_i = 1, k = 1e12, x can be up to 1e12 + 1e9. So we need 64-bit. We can set hi = 2e12? But better to compute a safe hi: Since sum over a_i * x - b_i <= k, we can bound x <= (k + sum b_i) / min a_i? Actually we have sum_i a_i * x - sum_i b_i <= k? But note deficits are only positive terms, so it's not simply sum of all; but if all deficits are positive, then sum_i a_ix - sum_i b_i <= k, i.e., x <= (k + sum b_i) / sum a_i. However if some deficits are negative (excess), then we are not using excess, but still the sum of deficits is less than or equal to sum_i (a_i x - b_i) if all are positive? Actually max(0, a_i x - b_i) <= a_i x - b_i + |min(0,...)? Not straightforward. But we can bound x by (k + sum b_i) / min a_i? Not exactly. Safer to use a while loop doubling hi until can(hi) is false, starting from 1. Or we can set hi to something like 2e12? If constraints unknown maybe up to 2e9? But 2e12 is fine with 64-bit. But we must ensure it's large enough for any test. Since k and b_i and a_i may be up to 1e9? Actually typical Codeforces problem "Cookies" has constraints: n up to 1e5, k up to 1e9, a_i,b_i up to 1e9. So maximum x can be up to about (1e9 + 1e91e5?) Not; more precisely, worst-case: n=1, a_1=1, b_1=1e9, k=1e9 => x=2e9. For n=1e5, a_i=1e9 each, b_i=0, k=1e9 => x= floor(1e9/(1e9))? Actually if a_i large, x small. So max x is when a_i=1 and b_i and k are large, x up to b_i + k per ingredient? Actually with one ingredient: x <= floor((b_1 + k)/a_1). So max x could be up to (1e9+1e9)/1 = 2e9. So 2e9 is safe. But if constraints are larger (like up to 1e12), then we need bigger. Since the problem says "large constraints", it might be that k and b_i up to 1e12. I'll adopt generic binary search with hi=2e12? Could be not enough if k up to 1e18. Actually typical Codeforces problem "A. Cookies" maybe has k up to 1e9, but I'm not sure. To be robust, we can use while(ok(hi)) hi*=2; starting from 1. But we need to set an upper limit to avoid infinite loops in case ok(hi) always true? If k is huge and b_i huge, then x can be arbitrarily large? Actually if a_i>0, then as x increases, deficits increase linearly, so eventually ok(x) will be false. So doubling will eventually exceed. We'll start hi=1, and while can(hi) is true, hi *= 2. But careful: can(0) is true. So we need to find hi such that can(hi) is false. That's a typical approach. This ensures we don't need to guess.
Time complexity: O(n log finalX). With n up to maybe 1e5 and log up to about 60, fine.
Now implement:
Define can(x):
need = 0
for i in range(n):
required = a[i] * x
if required > b[i]:
need += required - b[i]
if need > k: # early exit
return False
return need <= k
But need can overflow 64-bit? required = a_i * x: a_i up to maybe 1e9, x up to maybe 1e12? product up to 1e21, which exceeds 64-bit (9e18). Actually 1e9 * 1e12 = 1e21 > 9.22e18, so overflow possible if using 64-bit signed. But if constraints are that large, we need to handle overflow. However in typical problems, constraints are within 64-bit. But to be safe, we can early check: if required > b[i] and required might overflow? Actually we can compute using Python's arbitrary integers, so no overflow. In C++ we would need to use 128-bit or careful. Since we are writing solution in Python, we have big integers, so fine. But we need to ensure performance. Python big integers for up to 1e21 are fine.
But in Python we can also detect overflow by comparing to a threshold. But not needed.
Thus the solution works for large numbers.
Edge Cases: x=0 always works.
Implementation steps:
Read input.
n,k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
Define function possible(x):
total_powder_needed = 0
for i in range(n):
required = a[i] * x
if required > b[i]:
total_powder_needed += required - b[i]
if total_powder_needed > k:
return False
return True
Binary search:
lo = 0
hi = 1
while possible(hi):
hi *= 2
while lo < hi:
mid = (lo + hi + 1) // 2
if possible(mid):
lo = mid
else:
hi = mid - 1
print(lo)
But careful: The while loop to find hi: we start hi=1, but if possible(1) false then hi stays 1? Actually we want hi such that possible(hi) is false. Starting from 1: if possible(1) is false, then hi=1 already false, okay. Then binary search between lo=0 and hi=1 will work: mid = (0+1+1)//2=1; if possible(1) false then hi=0; else lo=1. So final lo correct.
If possible(1) true, we double hi until false.
One more nuance: For very large numbers, hi doubling may take many steps (like up to 2^60). That's fine, only logarithmic.
Alternatively, we could set hi = (max(b) + k) // min(a) + 1, but that requires O(n) to compute min/max, but also may not be safe if some a_i very small. So doubling is robust.
Thus final solution.
But the problem statement says: "If you can solve this problem in large constraints, then you can just write a single solution to the both versions. If you find the problem too difficult in large constraints, you can write solution to the simplified version only." Since we can handle large constraints, we'll write a single solution.
We need to output maximum number of cookies.
Check sample:
Input1:
3 1
2 1 4
11 3 16
Compute:
For x=4: ingredient1: 24=8 <=11, need0; ingredient2:14=4 >3 => need1; ingredient3:4*4=16 <=16 => need0. total=1<=1 -> ok. x=5: ing1 10<=11, ing2 5>3 need2, ing3 20>16 need4 total=6>1 false. So max 4 -> matches.
Second sample: output 3. Works.
Thus solution is correct.
Now we need to write answer: Provide explanation and code.
We'll produce final answer in Python.
Edge Cases: n=1, all fine.
Complexities: O(n log(max achievable cookies)).
Now produce final output. ```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
n = int(next(it))
k = int(next(it))
a = [int(next(it)) for _ in range(n)]
b = [int(next(it)) for _ in range(n)]
# Check whether we can bake x cookies
def can(x: int) -> bool:
need = 0
for i in range(n):
required = a[i] * x
if required > b[i]:
need += required - b[i]
if need > k: # early exit
return False
return need <= k
# Find an upper bound where can(hi) is False
lo = 0
hi = 1
while can(hi):
hi <<= 1 # multiply by 2
# Binary search on [lo, hi]
while lo < hi:
mid = (lo + hi + 1) // 2
if can(mid):
lo = mid
else:
hi = mid - 1
print(lo)
if name == "main":
solve()