We need to solve the problem: given a recipe string containing B, S, C, count required numbers per hamburger: b_needed, s_needed, c_needed. Then we have initial inventory: nb, ns, nc. Prices: pb, ps, pc. Money r. We can buy additional ingredients with money. Need to maximize number of hamburgers we can make.
We can decide to make x hamburgers. For each ingredient type, we need to have total amount = x * need_i. Initially we have some amount inv_i. So deficit_i = max(0, x * need_i - inv_i). The total cost to buy deficit_i is sum_i deficit_i * price_i. This must be ≤ r. Also x must be non-negative integer. The answer is the maximum x such that condition holds.
We need to search over x. The constraints: nb, ns, nc up to 100, need_i derived from string length ≤100, so need_i ≤100. r up to 1e12. The answer can be huge because we can buy many. For example, if need_i > 0 for all, we can compute bound: if we use all money to buy the most cost-effective? But the direct search could use binary search. Since r up to 1e12, and need_i up to 100, the maximum possible hamburgers could be huge: e.g., if recipe is "B" (only bread), and price pb = 1, r = 1e12, then we can buy about 1e12 bread plus initial nb up to 100, so x up to ~1e12+100. So binary search from 0 to some high upper bound. Need to ensure upper bound covers max possible. The maximum x can be when we have no initial inventory, we can only buy all ingredients: each burger costs sum(need_i * price_i) in terms of money if we need to purchase everything. So an upper bound is floor( (r + (nbpb?) Actually careful: The cost for x burgers is sum_i max(0, xneed_i - inv_i)price_i. If we ignore initial inventory, minimal cost per burger = sum_i need_i * price_i (if we need to buy all). But because we have some initial inventory, it reduces cost. So x can be as high as something like floor( (r + nbpb + nsps + ncpc) / (need_iprice_i) )? Actually if we treat initial inventory as convertible to money? Not exactly: we cannot sell them. But the total resource we have is: initial stock plus what we can buy with r. Equivalent to: we have money r and ingredients. The total effective "purchasing power" is r + (if we could buy the initial stock? Not exactly, because we cannot convert ingredients back to money; they can only be used. But we can consider that we have some amount of each ingredient. So the maximum number of burgers x is limited by: for each ingredient i, we have inv_i + floor(r / price_i) if we concentrate all money on that ingredient? But we need all three types simultaneously. So the maximum x limited by each ingredient: we can at most get inv_i + (r / price_i) if we spent all money on that ingredient. But we may need to allocate money across types. So a safe upper bound is max over i of floor((inv_i + r/price_i) / need_i) if need_i > 0; else if need_i = 0, that ingredient doesn't bound. But we need to account that money can be spent across types; but worst-case we could allocate money to the limiting ingredient. But to be safe, we can compute an upper bound as: suppose we ignore money constraints, just based on ingredient availability if we could buy infinite? Actually money is limited, so we need to consider cost. However, we can compute a very high upper bound: since need_i >=1 if present, and price_i >=1, the minimum cost per burger is at least 1 (some combination). So the maximum x cannot exceed r + sum(inv_i * price_i?) Not correct. Let's think: The total cost to produce x burgers is sum_i max(0, xneed_i - inv_i)price_i. That cost must be ≤ r. If we ignore the max, we can think: we need to acquire xneed_i total units. But we have inv_i initially, and we can buy additional. The total monetary value of resources needed is sum_i xneed_iprice_i. But we already have inv_i, which have value sum_i inv_iprice_i if we had to buy them. However, we cannot get that value back. The inequality sum_i max(0, xneed_i - inv_i)price_i ≤ r is equivalent to sum_i xneed_iprice_i ≤ r + sum_i inv_iprice_i. Because sum_i max(0, xneed_i - inv_i)price_i = sum_i (xneed_iprice_i) - sum_i min(inv_i, xneed_i)price_i ≤ sum_i (xneed_iprice_i) - sum_i (something). Actually, let's derive: For each i, define used_i = min(inv_i, xneed_i) (we can only use at most inv_i, but we may have leftovers if inv_i > xneed_i). The cost to buy is (xneed_i - used_i)price_i. But we don't have control over using less than inv_i? Actually we will use as much as possible from inventory to reduce cost, so used_i = min(inv_i, xneed_i). So the purchase cost = (xneed_i - used_i)price_i. So the inequality is sum_i (xneed_i - min(inv_i, xneed_i))price_i ≤ r. That is not exactly sum_i xneed_iprice_i - sum_i min(inv_i, xneed_i)price_i ≤ r. This is ≤ sum_i xneed_iprice_i. So if we take sum_i xneed_iprice_i ≤ r + sum_i inv_iprice_i, does that guarantee? Let's check: We want to derive an upper bound. Note that min(inv_i, xneed_i) ≤ inv_i. So the left side = sum_i xneed_iprice_i - sum_i min(inv_i, xneed_i)price_i ≥ sum_i xneed_iprice_i - sum_i inv_iprice_i. So we have sum_i xneed_iprice_i - sum_i inv_iprice_i ≤ LHS? Actually LHS is >= that? Because subtracting a smaller min? Wait: Since min(inv_i, xneed_i) ≤ inv_i, then sum_i min(...)price_i ≤ sum_i inv_iprice_i. So xneed_iprice_i - min(...)price_i ≥ xneed_iprice_i - inv_iprice_i. So LHS ≥ sum_i (xneed_iprice_i - inv_iprice_i) = sum_i xneed_iprice_i - sum_i inv_iprice_i. Therefore the condition LHS ≤ r implies that sum_i xneed_iprice_i - sum_i inv_iprice_i ≤ r. So we get necessary condition: sum_i xneed_iprice_i ≤ r + sum_i inv_iprice_i. This is a necessary condition, but it's not sufficient because sometimes we might waste ingredients? Actually it's necessary because we cannot have cost less than the difference if we used all inventory. But wait, we might not be able to use all inventory if some ingredient is more than needed? If inv_i > xneed_i, then min = xneed_i, so that ingredient contributes zero to purchase cost. But in the necessary condition we used inv_iprice_i which is larger than min; we derived sum_i xneed_iprice_i - sum_i inv_iprice_i ≤ LHS? Actually we got LHS >= sum_i xneed_iprice_i - sum_i inv_iprice_i, so LHS ≤ r implies the RHS of inequality? Let's do step:
We have cost = sum_i (xneed_i - min(inv_i, xneed_i))price_i.
Define A = sum_i xneed_iprice_i.
Define B = sum_i min(inv_i, xneed_i)*price_i.
Then cost = A - B.
Since min(inv_i, xneed_i) ≤ inv_i, we have B ≤ sum_i inv_iprice_i. Therefore cost = A - B ≥ A - sum_i inv_i*price_i. So cost ≥ A - total_inv_value.
Given cost ≤ r, we have A - total_inv_value ≤ cost ≤ r => A ≤ r + total_inv_value. So indeed A ≤ r + total_inv_value is a necessary condition (if A > r+total_inv_value, then cost > r). It is not sufficient because cost could be > r even if A ≤ r+total_inv_value, because B might be less than total_inv_value due to waste of inventory (i.e., some ingredients in inventory can't be fully used because we have more of that ingredient than needed and less of others). Actually if inv_i > xneed_i for some i, then B = sum_i min(inv_i, xneed_i)price_i = sum_i xneed_iprice_i for those i? For those i, min = xneed_i, which is less than inv_i. So B is less than total_inv_value if any inv_i exceeds xneed_i. So cost = A - B > A - total_inv_value (since B < total_inv_value). So it's possible that A ≤ r + total_inv_value, but cost = A - B > r because B is smaller. That is, we might have to purchase more of other ingredients while some inventory of a particular ingredient goes unused, thus increasing cost. So the necessary condition is weaker, but it can give an upper bound that is possibly higher than actual feasible x. But we can use that to bound binary search safely: we can compute an upper bound as floor((r + total_inv_value) / (sum_i need_iprice_i))? Actually A = x * (sum_i need_iprice_i). So the condition A ≤ r + total_inv_value gives x ≤ (r + total_inv_value) / (sum_i need_iprice_i). That's a valid upper bound (since if x violates it, it's impossible). This bound might be huge but it's finite and easy to compute. However, we must consider if sum_i need_iprice_i could be zero? That would happen only if recipe has no ingredients? But recipe is non-empty string, so need_i >= 1 for at least one ingredient? Actually could be e.g., recipe "B"? Then sum_i need_iprice_i = need_B * pb, need_B >=1, price >=1, so >0. So safe. So we can compute upper = (r + nbpb + nsps + ncpc) / (b_neededpb + s_neededps + c_neededpc). But careful: If recipe includes multiple types, but some need_i may be 0, then sum_i need_iprice_i is based on those present. So we compute cost per burger = total_price_per_burger = b_neededpb + s_neededps + c_neededpc. Then upper_bound = (r + nbpb + nsps + nc*pc) / cost_per_burger. Since everything integer, we can compute floor. But this may be not large enough? Actually as argued, x must satisfy A ≤ r + total_inv_value, so x ≤ (r + total_inv_value) / cost_per_burger. So that is an upper bound. However, if cost_per_burger is zero? Not possible because all price_i ≥ 1 and need_i≥1 for at least one. So ok.
But is this bound high enough? It might be large, but we can also add something like 1e12 to be safe. But we can compute a bound that is certainly feasible: For example, we can also consider that even if we ignore cost, the maximum hamburgers limited by one ingredient if we have infinite money: the number of hamburgers limited by ingredient supply alone is infinite because we can buy unlimited. So the only real limit is money. The money limit bound is: if we spend all money on the most restrictive ingredient? Actually the cost function is linear but piecewise? But we can do binary search up to this upper bound. Since r up to 1e12 and costs up to 100100? Actually need_i ≤ 100, price_i ≤ 100, so cost_per_burger ≤ 1001003? Wait need_i for each ingredient can be up to 100 (since string length ≤100), so each need_i ≤ 100, price_i ≤ 100, so cost_per_burger ≤ 100100 + 100100 + 100100 = 310000 = 30000. So lower bound of cost_per_burger? At least 1 (if need_i=1 and price_i=1). So upper bound could be up to (1e12 + up to 100100 + 100100 + 100100) / 1 ≈ 1e12 + 30000, which is about 1e12. So binary search over up to 1e12+some is fine: log2(1e12) ~ 40 steps. So we can safely do binary search from 0 to, say, 1e12 + something. But we need to be careful about overflow in intermediate computations when computing cost for x. Since x may be up to 1e12, xneed_i could be up to 1e12 * 100 = 1e14, which fits in 64-bit? 1e14 < 9.22e18 (max of 64-bit). So using 64-bit (long long) is fine. But also we multiply by price_i (≤100), so up to 1e16, still within 2^63-1 (~9.22e18). Actually 1e14 * 100 = 1e16, still safe. But sum of three such terms could be up to 3e16, also safe. So using unsigned long long or signed long long is fine. However, we might also compute upper bound using that formula which could be up to (1e12 + total_inv_value) / cost_per_burger; total_inv_value ≤ 100100 * 3? Actually nb ≤100, pb ≤100, so nb*pb ≤ 10000. Similarly others, so total_inv_value ≤ 30000. So upper bound ≤ (1e12 + 30000) / 1 = about 1e12. So that's safe.
But there is nuance: If cost_per_burger = 0? Not possible. So we can compute hi = (r + nbpb + nsps + ncpc) / (b_neededpb + s_neededps + c_neededpc). But careful: In the derivation, we used total_inv_value = sum_i inv_i * price_i, but is that correct? The necessary condition is sum_i xneed_iprice_i ≤ r + sum_i inv_iprice_i. That is correct. So hi = floor((r + sum_i inv_iprice_i) / sum_i need_i*price_i). But is it always an integer? We can take floor.
But consider scenario where some need_i = 0. Then cost_per_burger would not include that ingredient, and total_inv_value includes that ingredient's value, but that ingredient's inventory doesn't help reduce cost if it's not needed. The bound derived still holds: Since need_i = 0, its term in A is 0, and B includes min(inv_i,0)price_i = 0, so that ingredient does not affect cost. The necessary condition A ≤ r + total_inv_value still holds (since total_inv_value includes that ingredient's value). But if that ingredient is not needed, its inventory does not help making burgers, but it also doesn't hurt. The bound might be looser because we add extra value that cannot be used. However, the bound remains safe (i.e., any feasible x must satisfy it). For example, recipe "B", nb=100 (bread), ns=1000000 (sausage not needed), pb=1, ps=1000, r=0. Then total_inv_value = 1001 + 10000001000 = huge, bound would be huge but actual maximum x limited by bread only: x <= nb = 100. But the bound is not a tight upper bound, but it's still an upper bound? Let's check: A = x * (need_B * pb) = x1. total_inv_value includes massive sausage value. Then A ≤ r + total_inv_value gives x ≤ 0+ huge, so that's not an upper bound that restricts x; it's too large. That's fine; we still need an upper bound that is finite and not too large to cause binary search to overflow. But if we set hi to that bound, it could be astronomically large if inv_i for unneeded ingredients are huge? But note constraints: nb, ns, nc ≤ 100, so inv_i values are bounded! Wait, input constraints: nb, ns, nc are between 1 and 100 inclusive? Actually says: (1 ≤ nb, ns, nc ≤ 100). Yes, all three are at most 100. So even though unneeded ingredient inventory is at most 100, price at most 100, so total_inv_value ≤ 100100 = 10000 per ingredient, total ≤ 30000. So the bound is still reasonable. So we can safely compute hi = (r + nbpb + nsps + ncpc) / (b_neededpb + s_neededps + c_needed*pc). However, if the denominator is zero? Not possible because need_i are from non-empty string, at least one need > 0, and price_i >0, so denominator >0.
But there is another nuance: What if need_i = 0 for some ingredient, but the initial inventory for that ingredient is large? Actually max 100. So total_inv_value is at most 30000. So hi ≤ (1e12+30000)/1 ≈ 1e12. So it's fine.
Alternative: We can set hi = 1e12 + 100 (or something). But better compute the bound to be safe.
So algorithm:
- Read recipe string s.
- Count needed: b_need = count of 'B', s_need = count of 'S', c_need = count of 'C'.
- Read nb, ns, nc.
- Read pb, ps, pc.
- Read r (as long long).
- Define function can_make(x) returns true if total cost ≤ r.
cost = max(0, x*b_need - nb)pb + max(0, xs_need - ns)ps + max(0, xc_need - nc)*pc.
Return cost <= r.
- Binary search lo=0, hi = something like (r + nbpb + nsps + ncpc) / (b_needpb + s_needps + c_needpc) + 1? Actually we need to ensure hi is an upper bound where can_make(hi) is false? For binary search typical: find max x such that true. We can set lo = 0, hi = bound + 1 such that can_make(lo) true (0 always true? cost 0 ≤ r? Since r ≥ 1, cost for x=0 is 0, so yes true). And can_make(hi) maybe false. We'll ensure hi is high enough to be false. So we can compute a hi that is certainly too high: e.g., hi = (r + nbpb + nsps + ncpc) / (b_needpb + s_needps + c_needpc) + 1? Actually the derived bound is necessary, so any x greater than that bound must be false. So if we set hi = that bound + 1, then can_make(hi) should be false (but we need to be absolutely sure it's false; the bound derived is necessary, not sufficient. So if x > bound, then A > r + total_inv_value, which implies cost > r? Actually we derived cost ≥ A - total_inv_value. If A > r + total_inv_value, then A - total_inv_value > r, thus cost > r. So indeed, if x > (r+total_inv_value)/cost_per_burger, then A - total_inv_value > r, hence cost > r. So can_make is false for any integer x > that value. So we can set hi = (r + total_inv_value) / cost_per_burger + 1. But we need to be careful with integer division: Let floor_val = (r + total_inv_value) // cost_per_burger. Then any x > floor_val is guaranteed false. So we can set hi = floor_val + 1. However, what if cost_per_burger is zero? Not possible.
But we also need to consider that the bound may be huge but within 64-bit.
Alternatively, we can just set hi to something like 1e12 + 10000 (since worst-case: if recipe is "B", pb=1, r=1e12, then we can make at most nb (max 100) + floor(r/pb) = about 1e12+100. So hi=1e12+1000 would suffice. But to be safe we could use hi = 2e12 or something. Since r up to 1e12, the answer cannot exceed r + 100 (if each burger costs at least 1 ruble). Actually if recipe uses only one ingredient with need=1, price=1, then each burger costs at most 1 ruble if we need to buy. But we also have initial inventory up to 100, so total could be r + 100. So hi could be r + max(nb,ns,nc) maybe. But there is scenario where price could be 100 and need 1, then cost per burger =100, so max burgers = (r/100)+100. So still ≤ r+100. Actually worst-case burgers when price per burger is minimal, minimal price per burger is 1 (if need_i=1, price=1). So maximum burgers could be r + initial stock (since initial stock may provide free burgers). So max burgers ≤ r + max(nb,ns,nc) if each burger requires only one ingredient? Actually if need=1 for one ingredient, each burger requires 1 unit of that ingredient. Initially we have at most 100 units of that ingredient, so we can make at most 100 + floor(r/price). Since price≥1, floor(r/price) ≤ r. So answer ≤ 100 + r. Since r ≤ 1e12, answer ≤ 1e12+100. So hi = 1e12 + 1000 is safe. But to be precise, if need_i could be zero for some, then the burgers might require multiple ingredients, but still each burger's cost is at least min(price_i for needed ingredients) times needed count. But minimal cost per burger is at least 1 (since price_i >=1 and need_i >=1 for some). So still answer ≤ r + something. But that something could be sum of initial inventory? Actually if a burger requires 2 ingredients, we might need both. But initial inventory provides up to 100 of each, so total possible burgers from inventory alone is limited by the scarcest needed ingredient. For example, if recipe needs B and S each 1, and we have nb=100, ns=100, then we can make 100 from inventory. So initial inventory contributes at most 100 (since each ingredient ≤100). So answer ≤ 100 + floor(r / min_cost_per_burger). min_cost_per_burger could be as low as 1 (if need_i=1 for an ingredient with price=1). So ≤ 100 + r. So hi = r + 200 is safe. Since r up to 1e12, hi up to 1e12+200 fits in 64-bit. So we can set hi = r + 200 or even hi = 2e12 to be safe. But in binary search we need to ensure hi is large enough to be false. If we set hi = r + 200, does it guarantee false for all cases? Suppose recipe requires B (need=1), pb=1, nb=100, r=1e12. Then maximum burgers = 100 + 1e12. So hi = r+200 = 1e12+200. That is greater than the maximum possible? Actually maximum possible = 100+1e12 = 1e12+100, so hi = 1e12+200 is greater than that. Can we guarantee that for x = 1e12+200, cost > r? Let's compute cost: need=1, nb=100, pb=1. For x = r+200 = 1e12+200, cost = max(0, (1e12+200) - 100)*1 = 1e12+100. That's > r=1e12, yes. So false. Good. But what about scenario where price is 100 and need=1, nb=100, r=1e12. Maximum burgers = 100 + floor(1e12/100) = 100 + 1e10 = 1e10+100. hi = r+200 = 1e12+200, which is much larger. cost for that hi would be huge, definitely > r. So false. So hi = r+200 works. However, we should consider edge cases where price is extremely small? price min is 1. So indeed hi = r+200 works. But what if recipe requires zero ingredients? Not possible. So hi = r + (max initial inventory) maybe 100, plus 1. To be safe, we can set hi = r + 10000? But we can compute hi exactly as the derived necessary bound + 1 to be precise. That is also easy.
I think binary search using hi = 1e12 + 1000 is safe. But to be correct for all constraints, I'll compute hi using the formula: hi = (r + nbpb + nsps + ncpc) / (b_needpb + s_needps + c_needpc) + 1. But ensure we handle case where denominator might be 0? Not needed.
But there is a subtlety: The bound derived is necessary, but if we add 1 to the floor, we guarantee that can_make(hi) is false? Let's double-check: Suppose x0 = floor((r+total_inv_value)/cost_per_burger). Then for x = x0+1, we have A = (x0+1)cost_per_burger > r + total_inv_value (since x0cost_per_burger ≤ r+total_inv_value < (x0+1)cost_per_burger if the floor is exact? Actually it's possible that (x0)cost_per_burger ≤ r+total_inv_value, and (x0+1)cost_per_burger could be ≤ r+total_inv_value if there is remainder? Wait, floor division: Let D = cost_per_burger, N = r+total_inv_value. Then floor(N/D) = x0. Then (x0+1)D > N (since x0 is the largest integer such that x0D ≤ N). So yes, (x0+1)D > N. So for x = x0+1, A > N. Then we have cost ≥ A - total_inv_value > N - total_inv_value = r. So cost > r. So false. So hi = x0+1 is definitely false. Great. So we can set hi = floor((r + nbpb + nsps + ncpc) / (b_needpb + s_needps + c_needpc)) + 1. However, we also need to handle the case where cost_per_burger might be 0? Not possible.
One more nuance: What if the denominator cost_per_burger is not integer? It is integer because it's sum of integers. So okay.
But note: The derived bound uses total_inv_value. But is it possible that can_make(x) is false even for x ≤ floor(N/D)? Yes, that's why we binary search. But for hi = floor(N/D)+1 it's guaranteed false. So we have a valid hi.
Edge Cases: If recipe has no B, S, or C? Actually recipe is non-empty, so at least one need_i > 0. So denominator > 0.
Implementation Steps:
-
Parse input.
-
Count needed.
-
Read nb, ns, nc.
-
Read pb, ps, pc.
-
Read r (as long long).
-
If any need is zero, we treat that ingredient as not needed. But the cost function already uses max(0, xneed - inv) which will be 0 if need=0 because x0=0, and max(0, 0 - inv) = 0? Actually max(0, 0 - inv) = 0 because 0 - inv is negative (inv≥1), so max(0, negative)=0. So cost contribution = 0. That's fine.
-
Binary search: lo = 0, hi = compute bound: Let total_inv_value = (long long)nbpb + (long long)nsps + (long long)ncpc; cost_per_burger = (long long)b_needpb + (long long)s_needps + (long long)c_needpc; hi = (r + total_inv_value) / cost_per_burger + 1. However, we must ensure hi is large enough if cost_per_burger might be 0? But not possible.
-
But what if cost_per_burger is extremely large, and (r+total_inv_value)/cost_per_burger is 0? Then hi = 0+1 = 1. That's okay. lo=0, hi=1. We'll binary search.
-
We also need to check that hi may be less than actual possible maximum? Actually hi is an upper bound that is guaranteed false for values >= hi. But if hi is 1, then we need to ensure that for x=0, can_make(0) is true, and for x=1 maybe true? But hi=1 means we set hi such that can_make(hi) is false. If can_make(1) could be true, then hi=1 would be wrong because we assume hi is false. Let's check: hi = floor((r+total_inv_value)/cost_per_burger)+1. If floor((r+total_inv_value)/cost_per_burger) = 0, then hi = 1. That means (r+total_inv_value) < cost_per_burger. So A for x=1 is cost_per_burger, and we have A > r+total_inv_value, so cost > r, thus can_make(1) false. So hi=1 is indeed false. So hi is false. Good.
-
Then while lo < hi - 1? Use typical pattern: while (lo < hi) { mid = lo + (hi - lo) / 2; if can_make(mid) lo = mid; else hi = mid; } Or use inclusive high: we want max x with true. We set lo as last true, hi as first false. Then binary search narrows until hi = lo+1. Then answer = lo.
Implementation of can_make(x):
- Compute required B = x * b_need. If required B > nb then buy = required B - nb else 0. Similarly for S and C.
- Compute cost = buy_B * pb + buy_S * ps + buy_C * pc.
- Return cost <= r.
But careful: x may be very large, compute multiplication could overflow if we use 32-bit. Use long long.
Edge Cases: If b_need, s_need, c_need are zero? Already handled.
Now test with examples:
Example 1:
recipe "BBBSSC": counts: B=3? Let's count: B B B S S C => B=3, S=2, C=1.
nb=6, ns=4, nc=1.
pb=1, ps=2, pc=3.
r=4.
cost_per_burger = 31+22+13=3+4+3=10.
total_inv_value = 61+42+13=6+8+3=17.
hi = (4+17)/10 +1 = 21/10=2 +1=3. So hi=3, lo=0.
Check can_make(2): needed B=6, S=4, C=2? Actually 2 burgers: need B=6 (23), S=4 (22), C=2 (21). We have nb=6, ns=4, nc=1. So need to buy: B:0, S:0, C:1. Cost=13=3 ≤ 4 -> true.
Check can_make(3): need B=9, S=6, C=3. Buy: B=3, S=2, C=2? Actually nc=1 => C buy 2. Cost=31+22+2*3=3+4+6=13 >4 false. So answer 2. Good.
Example 2:
recipe "BBC": B=2, S=0? Actually "BBC" -> B, B, C -> B=2, C=1, S=0.
nb=1, ns=10, nc=1.
pb=1, ps=10, pc=1.
r=21.
cost_per_burger = 21 + 010 + 11 = 2+0+1=3.
total_inv_value = 11 + 1010 + 11 =1+100+1=102.
hi = (21+102)/3 +1 =123/3=41 +1=42.
Binary search yields answer 7? Let's compute manually: We need B and C. Initial B=1, C=1. Each burger needs 2B and 1C. So after using initial, we can make 0? Actually with one B and one C, we cannot make a full burger because need 2B. So need to buy. With r=21, price B=1, C=1. So each burger costs 2B + 1C = 3, but we may have initial inventory. Compute max:
We can make x burgers. Need total B = 2x, total C = x. We have B=1, C=1. So deficit B = max(0, 2x-1), deficit C = max(0, x-1). Cost = (2x-1)*1 + (x-1)1 = 3x-2, but only if x≥1. For x=1: cost = (2-1)+(1-1)=1+0=1 <=21. x=2: B need 4, deficit B=3? Actually 2x-1=3, deficit C = max(0,2-1)=1, cost=3+1=4 <=21. x=7: 27-1=13, C=7-1=6, cost=19 <=21. x=8: B deficit=15, C deficit=7, cost=22 >21. So max 7. Works.
Example 3:
recipe "BSC": B=1, S=1, C=1.
nb=1, ns=1, nc=1.
pb=1, ps=1, pc=3.
r=1e12.
cost_per_burger = 1+1+3=5.
total_inv_value = 1+1+3=5.
hi = (1e12+5)/5 +1 = 2e11? Actually 1e12/5 = 2e11, plus 1? Let's compute: 1e12=1000000000000, /5=200000000000, plus 1 = 200000000001? Actually (1e12+5)/5 = 200000000001? Let's compute: 1e12/5 = 200,000,000,000 exactly? 1,000,000,000,000 / 5 = 200,000,000,000. So (1e12+5)/5 = 200,000,000,001? Because 1e12+5 = 1000000000005, divided by 5 = 200000000001. So hi = 200000000001 + 1 = 200000000002. But expected answer is 200000000001. So hi is false for 200000000002, correct.
Now check can_make(200000000001): Need B=200000000001, S=200000000001, C=200000000001. Initial: nb=1, ns=1, nc=1. Deficits: B: 200000000000, S:200000000000, C:200000000000. Cost = 2000000000001 + 2000000000001 + 2000000000003 = 200000000000 + 200000000000 + 600000000000 = 1,000,000,000,000 = r. So true. For x+1, cost = (200000000001? Actually deficits increase by need each: B deficit increases by 1, etc. So cost becomes r+5 > r. So answer 200000000001. Good.
So binary search with hi computed this way works.
But we must be careful: total_inv_value = nbpb + nsps + nc*pc might overflow 32-bit, but with nb up to 100, pb up to 100, product up to 10000, sum up to 30000, so fine in 32-bit. But we use long long for consistency.
Now consider scenario where cost_per_burger could be zero if need_i all zero? Not possible. So fine.
Now about reading: The recipe string may contain uppercase letters B, S, C only. Input lines: first line is string (maybe with newline). Then three ints, then three ints, then long long r.
Implementation in C++: use getline? But easier: cin >> recipe; then cin >> nb >> ns >> nc; then cin >> pb >> ps >> pc; then cin >> r. But careful: The recipe line might have spaces? No, it's a string without spaces. So fine.
Now the binary search: We'll implement while (lo < hi) pattern to find last true. Let's define:
long long lo = 0, hi = (r + total_inv_value) / cost_per_burger + 1;
while (lo < hi) {
long long mid = lo + (hi - lo) / 2;
if (can_make(mid))
lo = mid + 1; // but we need to preserve that hi is false eventually? Actually typical pattern for max true: set lo as current true, hi as current false, while lo+1 < hi, mid = (lo+hi)/2, if can_make(mid) then lo=mid else hi=mid. At end lo is answer.
}
Better: set low = 0, high = bound+1 (where bound is something we know false). Then while (low + 1 < high) { mid = (low+high)/2; if (can_make(mid)) low = mid; else high = mid; } answer = low.
Initialize: low = 0, high = hi (where hi is false). We need to ensure that can_make(high) is false. We have hi = floor((r+total_inv_value)/cost_per_burger) + 1. We argued that hi is false. But we should also consider if hi could be 0? hi is at least 1 because we add 1 to a non-negative floor. So high >=1. But need to guarantee can_make(high) false. Let's verify in edge case where floor((r+total_inv_value)/cost_per_burger) could be -1? No, because numerator and denominator positive, so floor >=0. So hi >=1. And we argued hi is false. But what if the recipe doesn't need any ingredients? Not possible.
But consider case where cost_per_burger is extremely large and r+total_inv_value is small, say r=1, total_inv_value=3, cost_per_burger=10. Then floor((1+3)/10) = 0, hi = 1. Then can_make(1) should be false because we argued. Let's test: needed ingredients? Suppose recipe "BBB", b_need=3, pb=10? Actually to get cost_per_burger=10, maybe need=1, price=10? Actually any. For x=1, required = need_i. If initial inventory covers some but cost may be less than r? Possibly if initial inventory already covers all needed, then can_make(1) could be true even if cost_per_burger > r+total_inv_value? Wait, our argument: cost ≥ A - total_inv_value, and A = cost_per_burger. If A > r + total_inv_value, then A - total_inv_value > r, so cost > r. But is it possible that cost is not exactly A - total_inv_value? Because we used min(inv_i, need_i) in cost. cost = A - B, where B = sum_i min(inv_i, need_i)*price_i. Since min(inv_i, need_i) ≤ inv_i, we have B ≤ total_inv_value. So cost = A - B ≥ A - total_inv_value. So if A - total_inv_value > r, then cost > r. So indeed can_make(1) false. However, what if B is strictly less than total_inv_value? That would make cost even larger than A - total_inv_value? Actually if B < total_inv_value, then cost = A - B > A - total_inv_value, so it's even larger. So still > r. So false. So hi=1 is false indeed. So safe.
But what about the scenario where cost_per_burger is defined as sum(need_i*price_i). That's A for x=1. So if A > r+total_inv_value, then indeed can_make(1) false.
But wait, there is scenario where some need_i = 0. Then cost_per_burger excludes that ingredient. total_inv_value includes its value. But our inequality A ≤ r+total_inv_value is still necessary. For x=1, A = sum(need_i*price_i). If A > r+total_inv_value, then can_make(1) false. Good.
Now we need to also check if hi may overflow long long? r up to 1e12, total_inv_value ≤ 30000, so hi ≤ (1e12+30000)/1 + 1 ≤ 1e12+30001, fits in long long (signed 64-bit up to 9e18). So safe.
Thus we can implement.
But before coding, think about potential pitfalls: The binary search loop must handle large numbers, but number of iterations ~log2(1e12) ≈ 40, fine.
Now, implement can_make as:
bool can_make(long long x) {
long long needB = b_need * x;
long long needS = s_need * x;
long long needC = c_need * x;
long long buyB = max(0LL, needB - nb);
long long buyS = max(0LL, needS - ns);
long long buyC = max(0LL, needC - nc);
long long cost = buyB * pb + buyS * ps + buyC * pc;
return cost <= r;
}
But careful: multiplication might overflow if we use ints? Use long long for all. b_need, etc are ints but x is long long, product fits in long long.
Now, we need to watch out: pb, ps, pc are ints, but we can multiply with long long buy which may be up to 1e12, product up to 1e12 * 100 = 1e14, fits.
Now, implement binary search:
long long lo = 0;
long long hi = (r + nb * pb + ns * ps + nc * pc) / (b_need * pb + s_need * ps + c_need * pc) + 1;
But careful: The denominator is int product sums, which could be up to 30000, but we need to cast to long long to avoid overflow? Actually nbpb is up to 100100=10000, but we can compute as (long long)nb * pb. To be safe, compute total_inv_value = (long long)nb * pb + (long long)ns * ps + (long long)nc * pc; cost_per_burger = (long long)b_need * pb + (long long)s_need * ps + (long long)c_need * pc; Then hi = (r + total_inv_value) / cost_per_burger + 1;.
But note that if cost_per_burger could be zero? Not.
Now, we must also consider the case where the initial inventory might be more than needed for a certain x, but that's fine.
Now test with some custom cases:
Case: Recipe "B", nb=100, pb=10, r=0. Then total_inv_value = 100*10 = 1000, cost_per_burger = 10, hi = (0+1000)/10 +1 = 100+1=101. So hi=101. Binary search: low=0, high=101. We need to find max x such that can_make true. For x=100: needB=100, buyB = 0, cost=0 <=0 => true. x=101: needB=101, buyB=1, cost=10 >0 => false. So answer 100. Good.
Case: Recipe "B", nb=0, pb=1, r=5. total_inv_value=0, cost_per_burger=1, hi = (5+0)/1+1=5+1=6. can_make(5): needB=5, buy=5, cost=5 <=5 true. can_make(6): cost=6 >5 false. Answer 5. Good.
Case: Recipe "BC", B=1, C=1. nb=0, nc=0, pb=1, pc=1, r=3. cost_per_burger=2, total_inv_value=0, hi = (3+0)/2+1=1+1=2? Actually floor(3/2)=1, +1=2. So hi=2. can_make(1): need B=1 C=1, buy both cost=2 <=3 true. can_make(2): need 2 B,2 C, cost=4 >3 false. Answer=1. Works.
Case: Recipe "BC", nb=1, nc=0, pb=1, pc=1, r=2. cost_per_burger=2, total_inv_value = 11+01=1, hi = (2+1)/2 +1 = floor(3/2)=1 +1=2. can_make(1): need B=1 C=1, we have B=1, need to buy C=1, cost=1 <=2 true. can_make(2): need B=2 C=2, we have B=1 => buy B=1, C=2, cost=1+2=3 >2 false. Answer 1. correct.
Case: Recipe "BSC", nb=0, ns=0, nc=0, pb=1, ps=2, pc=3, r=5. cost_per_burger=6, total_inv_value=0, hi = (5)/6+1=0+1=1. hi=1, but can_make(1) cost = 6 >5 false, so answer 0. Good.
Now, we must also consider if initial inventory is more than needed for some ingredients, and we need to buy others. The can_make function handles that.
All good.
Now, potential overflow in cost computation: buyB * pb might be large. buyB could be as high as x*b_need, and x up to ~1e12, b_need up to 100, so buyB up to 1e14, pb up to 100 => product up to 1e16. Sum of three up to 3e16, still within signed 64-bit (9.22e18). So safe.
Now, we must be careful that in the binary search hi might be such that hi * b_need could overflow even in can_make if we call can_make(hi) for hi false? In binary search we might evaluate can_make(mid) where mid is less than hi, but hi could be as large as 1e12+something, still within safe multiplication. However, consider if recipe has b_need=0, but others positive. Then mid * b_need = 0, fine. So overall safe.
But what about scenario where cost_per_burger is extremely small? Actually min cost_per_burger = 1, hi = (r+total_inv_value)/1 + 1 = r+total_inv_value+1, which could be up to 1e12 + 30000 + 1 ≈ 1e12, still safe. So hi is at most ~1e12+30001. So mid up to that, times need up to 100 yields up to 1e14, safe.
Thus algorithm is sound.
Now, coding details:
We'll include , , using namespace std; etc.
Edge: The recipe may have length up to 100, but counting is straightforward.
Now, note that input constraint says nb, ns, nc are between 1 and 100 inclusive. But what if some need_i is zero and nb is >0? That's fine.
Now, implement:
int main() {
string recipe;
cin >> recipe;
int nb, ns, nc;
cin >> nb >> ns >> nc;
int pb, ps, pc;
cin >> pb >> ps >> pc;
long long r;
cin >> r;
int b_need=0, s_need=0, c_need=0;
for (char ch : recipe) {
if (ch == 'B') b_need++;
else if (ch == 'S') s_need++;
else if (ch == 'C') c_need++;
}
auto can_make = [&](long long x) -> bool {
long long needB = (long long)b_need * x;
long long needS = (long long)s_need * x;
long long needC = (long long)c_need * x;
long long buyB = max(0LL, needB - nb);
long long buyS = max(0LL, needS - ns);
long long buyC = max(0LL, needC - nc);
long long cost = buyB * pb + buyS * ps + buyC * pc;
return cost <= r;
};
long long cost_per_burger = (long long)b_need * pb + (long long)s_need * ps + (long long)c_need * pc;
long long total_inv_value = (long long)nb * pb + (long long)ns * ps + (long long)nc * pc;
long long lo = 0;
long long hi;
// special case: if cost_per_burger == 0? Not possible.
hi = (r + total_inv_value) / cost_per_burger + 1;
// However, we must be careful: if cost_per_burger is 0, that would be division by zero; but not happen.
// Also, to be safe, we could also set hi to something like 1e13 if cost_per_burger is 0, but not needed.
// Binary search for the largest x such that can_make(x) true.
while (lo < hi) {
long long mid = lo + (hi - lo) / 2;
if (can_make(mid)) {
lo = mid + 1;
} else {
hi = mid;
}
}
// After loop, lo is the first x for which can_make false, so answer = lo - 1.
long long ans = lo - 1;
cout << ans << endl;
return 0;
}
But we need to ensure that can_make(mid) for mid near hi doesn't overflow. hi = (r+total_inv_value)/cost_per_burger + 1. Since hi is such that can_make(hi) is false, we may not call can_make(hi) because we use lo<hi and lo starts 0, hi is false. In the loop, when we compute mid, it's always less than hi (since integer division). So mid might be hi-1 at most. So we only call can_make for values less than hi, which are <= hi-1. hi-1 is floor((r+total_inv_value)/cost_per_burger). That value may be feasible? Possibly true, but not necessarily always true? It could be false too, but we treat it accordingly. That's fine.
Now, but we need to consider the case where cost_per_burger is extremely large such that (r+total_inv_value)/cost_per_burger = 0, then hi = 1. Then we binary search with lo=0, hi=1. Loop condition lo < hi? 0<1 true. mid = 0+(1-0)/2 = 0. can_make(0) true => lo = mid+1 = 1. Now lo=1, hi=1, loop ends. ans = lo-1 = 0. Correct.
Edge: If can_make(0) is always true (cost 0 <= r). So answer at least 0.
Now, what about scenario where hi computed may be less than actual maximum because our derived bound is based on necessary condition? Wait, we argued that any x > floor((r+total_inv_value)/cost_per_burger) is impossible, but maybe x = floor(...) could also be impossible? That's fine; we binary search to find the maximum feasible x which could be less than floor(...). So hi is a safe upper bound (first impossible). So binary search works.
Potential issue: If cost_per_burger computed as zero? Only if all need_i = 0. But recipe non-empty, so at least one need_i >0 and price>0 => cost_per_burger>0. So fine.
Now, let's test with an extreme scenario: Recipe "B", nb=100, pb=100, r=1e12. cost_per_burger=100, total_inv_value=10000. hi = (1e12+10000)/100 +1 = 1e10 + 100 +1? Actually 1e12/100 = 1e10, plus 10000/100=100, so = 1e10 + 100 + 1 = 10000000101? Let's compute: 1e12 = 1000000000000. Divide by 100 = 10000000000. Add 10000/100=100 => 10000000100. +1 = 10000000101. So hi is about 1e10+101. can_make(10000000100) maybe? need B = 10000000100, initial 100, so buy = 10000000000? Actually 10000000100 - 100 = 10000000000, cost = 10000000000*100 = 1e12. r = 1e12. So cost = 1e12 exactly? Let's compute: (10000000100 - 100) = 10000000000; times 100 = 1,000,000,000,000 which is r. So can_make(10000000100) true. hi = 10000000101, false. So binary search should find answer 10000000100. That's correct.
Now, we must ensure that in the binary search we are not missing the maximum due to integer overflow when computing needB = b_need * x. b_need up to 100, x up to 1e10+? Actually x up to around 1e12 (if pb=1, need=1, nb=0, r=1e12, answer = 1e12. Then b_need * x = 1 * 1e12 = 1e12, safe. If need=100, answer could be around 1e10, product 100*1e10 = 1e12, still safe. So okay.
Now, one more nuance: The constraint says nb, ns, nc are between 1 and 100 inclusive. So initial inventory cannot be zero? Actually it says 1 ≤ nb, ns, nc ≤ 100. So they are at least 1. So we always have at least 1 piece of each ingredient. However, the problem statement may have misprinted? Usually in such problems, they can be zero. But here it's given 1 ≤ nb, ns, nc ≤ 100. So they are at least 1. That means we always have some inventory of each ingredient. But that might not matter; our code works for zero too. We'll keep it general.
Now, we should test with the given examples.
Example 1: Already tested.
Example 2: Already tested.
Example 3: Already tested.
All good.
Now, consider potential pitfalls: When computing hi, we use (r + total_inv_value) / cost_per_burger. This is integer division. But if cost_per_burger doesn't divide exactly, floor works. hi = floor(...)+1 ensures that for hi, cost > r. However, could there be a scenario where for x = floor(...)+1, cost is still <= r? That would contradict our derived inequality. Let's re-derive: For any x, cost = A - B, with A = x * cost_per_burger, B = sum_i min(inv_i, x*need_i)*price_i. Since B ≤ total_inv_value, we have cost ≥ A - total_inv_value. If A - total_inv_value > r, then cost > r. So if x satisfies A > r + total_inv_value, then cost > r. For x = floor((r+total_inv_value)/cost_per_burger) + 1, we have A = (floor(...)+1)*cost_per_burger > r+total_inv_value (since floor(...)*cost_per_burger ≤ r+total_inv_value). So indeed A > r+total_inv_value, thus cost > r. So can_make false. So safe.
Now, about using long long: In C++, we should use long long for all large numbers.
Now, we must also consider the case where cost_per_burger might be > r+total_inv_value, so floor((r+total_inv_value)/cost_per_burger) = 0, hi = 1. That's fine.
Now, test a corner case: Recipe "B", nb=1, pb=1, r=0. cost_per_burger=1, total_inv_value=1. hi = (0+1)/1 +1 = 1+1=2. So hi=2. can_make(1): needB=1, buyB=0, cost=0 <=0 true. can_make(2): needB=2, buyB=1, cost=1 >0 false. So answer=1. Works.
Now, test case where initial inventory more than needed for some but not others: Recipe "BSC", nb=100, ns=1, nc=1, pb=1, ps=1, pc=1, r=0. cost_per_burger=3, total_inv_value = 100+1+1=102. hi = (0+102)/3+1 =34+1=35. can_make up to limited by S and C. For x=1: needed B=1,S=1,C=1, we have enough, true. x=34: need B=34,S=34,C=34; we have B=100 (enough), S=1 => need buy 33, C=1 => buy 33, cost =33+33=66 >0? Actually r=0, so cannot buy. So can_make(34) false. Actually maximum x with r=0 is limited by the scarcest ingredient we have without buying: S and C only 1 each, so we can make only 1 hamburger. So answer=1. Our binary search with hi=35 will eventually find lo=1? Let's see: lo=0, hi=35. can_make(17) will compute cost = (max(0,17-100)=0) + (max(0,17-1)=16)*1 + (max(0,17-1)=16)*1 = 32 >0 false => hi set to 17. etc. Eventually should converge to 1. So answer 1. Good.
Now, we should also consider the case where recipe contains no 'B', but we have nb>0. That's fine.
Now, implement in code.
One more thought: The binary search while (lo < hi) with lo = mid+1 and hi = mid yields after loop lo being the first false. That's fine. But we must ensure that can_make(mid) for mid values that are huge doesn't overflow the multiplication when computing needB, etc. Since mid < hi, and hi may be up to about 1e12+some, needB = b_need * mid may be up to 100 * 1e12 = 1e14, still safe. So fine.
But note: In can_make, we compute needB = b_need * x; if b_need = 0, it's zero. So that's fine.
Now, consider potential issue: In the hi calculation, we divide (r + total_inv_value) by cost_per_burger. Since both are long long, integer division works.
But what if cost_per_burger is very small, like 1, and r is up to 1e12, then hi = 1e12+ total_inv_value + 1. That's about 1e12+30001. So hi fits.
Now, is there any chance that hi computed this way could be less than the actual max feasible x? That would be a problem because then we would never consider values above hi. But we proved hi is an upper bound (first false). Actually we proved that any x > floor((r+total_inv_value)/cost_per_burger) is impossible, so the maximum feasible x ≤ floor((r+total_inv_value)/cost_per_burger). So hi = floor(...)+1 is indeed greater than the maximum feasible x. So it's a valid upper bound (false for all x >= hi). So we aren't missing any feasible x.
But could there be feasible x > floor((r+total_inv_value)/cost_per_burger)? No, because if x > floor(...), then x * cost_per_burger > r+total_inv_value, leading to cost > r. So not feasible.
Thus hi is correct.
Now, implement and test.
Now, I think about possible pitfalls: The cost_per_burger might be 0 only if need_i = 0 for all, but not possible. So fine.
Now, about the reading: The problem says "Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier." So we will use cin/cout with long long.
Now, code.
But wait: We need to handle the case where the recipe might have characters other than B, S, C? It says only letters B, S, C.
Now, additional check: In can_make, we compute buyB * pb. pb is int, buyB could be very large, up to ~1e14, product up to 1e16, still within long long. But if we do buyB * pb, both are converted to long long? buyB is long long, pb is int, product is long long. Good.
Now, I'll write code.
However, there is also a direct formula approach: we can compute the number of burgers that can be made with current inventory alone without buying: init = min( floor(nb/b_need) if b_need>0 else INF, similarly for s_need, c_need) but that's not directly because ingredients are not independent? Actually we need all three, so the number from inventory alone is limited by the min of floor(inv_i / need_i) for those with need_i>0. Then we can simulate buying additional. But binary search is simpler.
But we must be careful: The initial inventory could be enough to make some burgers without spending money. Then we can use money to make more. The binary search will naturally handle that.
Now, complexity is fine.
Now, testing with custom cases:
Case: Recipe = "B", nb=100, pb=10, r=5. cost_per_burger=10, total_inv_value=1000, hi = (5+1000)/10+1=100+? floor(1005/10)=100, +1=101. Max burgers? We have 100 from inventory, can we make more? Need to buy one B costs 10, but only have 5 rubles, so can't. So answer=100. Binary search: can_make(100) true, can_make(101) false => answer=100.
Case: Recipe = "B", nb=0, pb=10, r=25. cost_per_burger=10, total_inv_value=0, hi = (25)/10+1=2+1=3. can_make(2): cost=20 <=25 true; can_make(3): cost=30 >25 false => answer 2. Works.
Case: Recipe = "BS", B=1, S=1. nb=1, ns=1, pb=1, ps=10, r=10. cost_per_burger=11, total_inv_value=1+10=11, hi = (10+11)/11+1=21/11=1+1=2. can_make(1): needB=1,S=1, have enough, cost=0 <=10 true; can_make(2): needB=2,S=2, need buy B=1, S=1, cost=1+10=11 >10 false => answer 1. Works.
Now, consider scenario where need_i zero: Recipe = "B", but we include zero for others. Already handled.
Now, all good.
But wait, one more nuance: In the computation of total_inv_value = nbpb + nsps + nc*pc, we multiply inventory by price. However, note that the inventory items may not be fully used if the recipe doesn't need them. So in the inequality we used total_inv_value, which is an upper bound on B (the value of used inventory). This is valid.
Now, implement the code.
Now, potential overflow: In hi = (r + total_inv_value) / cost_per_burger + 1; if r is 1e12, total_inv_value up to 30000, cost_per_burger up to 30000, the division yields up to about 1e12. Adding 1 is safe.
Now, we might want to ensure that hi does not overflow if we compute r+total_inv_value, but that's at most 1e12+30000, safe.
Now, I think we are ready.
But, before finalizing, let's think about an alternative scenario: Suppose recipe has need_B=1, need_S=1, need_C=1. pb=1, ps=1, pc=1. nb=100, ns=0, nc=0, r=100. Then total_inv_value=100+0+0=100. cost_per_burger=3. hi = (100+100)/3+1=200/3=66+1=67. can_make(66): need B=66,S=66,C=66. B: have 100, need buy 0; S: buy 66; C: buy 66; cost=132 >100 false. Actually can_make(33): need B=33,S=33,C=33; B:0 buy, S:33, C:33 => cost=66 <=100 true. can_make(50): B:0, S:50, C:50 => cost=100 <=100 true. can_make(51): cost=102 >100 false. So answer 50. Our hi=67 is false for 67, and binary search will find 50. Good.
Now, one more: Could the derived bound ever be less than the true maximum? Actually if the necessary condition is not sufficient, the true max could be less than the bound. That's okay; hi is greater than true max because hi = floor(...)+1, and floor(...) might be >= true max? Actually true max could be less than or equal to floor(...). So hi > true max. So hi is a valid upper bound.
Now, everything is fine.
Now, produce final answer.
Let's also consider if there's any risk of integer overflow in can_make for values less than hi but hi may be as high as 1e12+... and need_i up to 100, product up to 1e14. So safe.
Now, code.
But we need to be careful that in some C++ compilers, if we use 32-bit ints in multiplication without casting, it could overflow. So we should cast or use long long. We'll use long long for all variables that might involve large numbers: b_need, s_need, c_need can stay int, but when multiplying with x, we cast.
Now, we'll write:
long long requiredB = (long long)b_need * x;
But we can also write: long long requiredB = 1LL * b_need * x;
Similarly for cost_per_burger: long long cost_per_burger = 1LL * b_need * pb + 1LL * s_need * ps + 1LL * c_need * pc;
total_inv_value = 1LL * nb * pb + 1LL * ns * ps + 1LL * nc * pc;
Now, maybe we should also consider that r can be up to 1e12, so we should use long long for r.
Now, finalize.
Now, I'll also test with a scenario where the recipe string may be read with spaces? Probably not. Use cin >> recipe.
Now, double-check: The input format:
First line: recipe string.
Second line: three integers nb, ns, nc.
Third line: three integers pb, ps, pc.
Fourth line: integer r.
We'll follow that.
Now, output the answer.
Now, there is also possibility that the answer might be 0 if cannot make any burger even with purchases? For instance, if recipe needs something and we have no money and inventory insufficient. The code will output 0.
Now, implement.
Now, is there any scenario where the binary search might loop infinitely? With while (lo < hi) and mid = lo + (hi-lo)/2, and updates, it will terminate because interval shrinks.
Now, final code.
But I'm thinking: In the binary search pattern I chose: while (lo < hi) { mid = lo + (hi - lo)/2; if (can_make(mid)) lo = mid+1; else hi = mid; } answer = lo-1; This works when we have lo starting at 0 and hi being first known false. At the end, lo is the first false, so answer = lo-1.
But we must ensure that can_make(mid) for mid near hi-1 is computed correctly. It's fine.
Alternatively, we could use while (lo <= hi) but above is fine.
Let's test on a simple case: hi=3, lo=0. Initial: lo=0, hi=3, mid=1, can_make(1) true => lo=2. Now lo=2, hi=3, mid=2, can_make(2) true => lo=3. Now lo=3, hi=3, exit. answer = 2. That's correct if 2 is max true, 3 false.
If hi=1, lo=0: mid=0, can_make(0) true => lo=1. exit. answer=0. correct.
If hi=2, lo=0: suppose can_make(0) true, can_make(1) false? Then: lo=0, hi=2, mid=1, can_make(1) false => hi=1. Now lo=0, hi=1, mid=0, can_make(0) true => lo=1. exit, answer=0. correct.
So pattern works.
Now, we must guarantee that hi is indeed false. We argued hi false. But what if total_inv_value is huge relative to r? Actually hi = floor((r+total_inv_value)/cost_per_burger) +1. For hi to be false, we need can_make(hi) false. Our proof says it's false because A > r+total_inv_value => cost > r. But is it possible that cost <= r even if A > r+total_inv_value? That would require B > total_inv_value, which is impossible because B ≤ total_inv_value. So no. So hi false.
Thus code stands.
Now, consider the possibility that the recipe string may be empty? The problem says non-empty string. So fine.
Now, finalize.
Edge: In can_make, we might want to check if x is too large such that needB overflows? But we already bounded hi such that x is limited to about 1e12, and needB = b_need * x ≤ 100 * 1e12 = 1e14, within 9e18, safe.
Now, final answer.
But before finalizing, test with max possible values: r=1e12, nb=ns=nc=100, pb=ps=pc=100, recipe = "BBBBBBBBBB... (100 chars)"? Actually length max 100, so need_B could be up to 100, others 0. Then cost_per_burger = 100100 = 10000. total_inv_value = 100100 + 100100 + 100100 = 30000. hi = (1e12+30000)/10000 +1 = 100000000 + 3? Actually 1e12/10000 = 1e8 = 100,000,000. plus 30000/10000=3 => 100,000,003. +1 => 100,000,004. So hi=100,000,004. needB for x=100,000,004 = 100 * 100,000,004 = 10,000,000,400? Actually 100,000,004*100 = 10,000,000,400, which is 1e10, still safe. So all good.
Now, code.
Potential improvements: none.
Now, write final code with proper includes.
Also, we could implement a direct while loop for binary search but it's fine.
Now, include iostream, string, algorithm.
Now, final answer.
But there is one more nuance: In the binary search we compute mid = lo + (hi - lo) / 2. This is integer division, but since lo and hi are long long, it's fine. In C++, if lo and hi are long long, (hi-lo)/2 is long long. So ok.
Now, I'll produce final code.
But wait, we should check the possibility that cost_per_burger might be 0 if need_i=0 for all, but not.
Now, finalize.
One more test: Recipe "BSC", nb=1, ns=1, nc=1, pb=1, ps=1, pc=1, r=0. cost_per_burger=3, total_inv_value=3, hi = (0+3)/3 +1 =1+1=2. can_make(1) true, can_make(2) false => answer 1. correct.
Now, we are ready.
But I realize something: The problem says nb, ns, nc are between 1 and 100, but in the examples, they are given as e.g., 6,4,1; that's okay.
Now, I'll write the solution.
Now, include comment? Not needed.
Now, produce final answer.
But we might consider using unsigned long long? Not necessary; long long covers up to 9e18, and our maximum numbers are about 1e16, so safe.
Now, produce final solution.
Now, there is an alternative: we can compute using arithmetic without binary search? But binary search is simpler.
Now, I'll write the final answer. ```cpp
#include
#include
#include
using namespace std;
int main() {
string recipe;
cin >> recipe;
int nb, ns, nc;
cin >> nb >> ns >> nc;
int pb, ps, pc;
cin >> pb >> ps >> pc;
long long r;
cin >> r;
// count required ingredients per hamburger
int b_need = 0, s_need = 0, c_need = 0;
for (char ch : recipe) {
if (ch == 'B') b_need++;
else if (ch == 'S') s_need++;
else if (ch == 'C') c_need++;
}
// function to check if we can make x hamburgers
auto can_make = [&](long long x) -> bool {
long long needB = (long long)b_need * x;
long long needS = (long long)s_need * x;
long long needC = (long long)c_need * x;
long long buyB = max(0LL, needB - nb);
long long buyS = max(0LL, needS - ns);
long long buyC = max(0LL, needC - nc);
long long cost = buyB * pb + buyS * ps + buyC * pc;
return cost <= r;
};
// cost of one hamburger if we had to buy all ingredients
long long cost_per_burger = (long long)b_need * pb + (long long)s_need * ps + (long long)c_need * pc;
// total "value" of initial inventory (upper bound for what we can use)
long long total_inv_value = (long long)nb * pb + (long long)ns * ps + (long long)nc * pc;
// upper bound: any number larger than this is impossible
long long hi = (r + total_inv_value) / cost_per_burger + 1;
long long lo = 0;
// binary search for the maximum x with can_make(x) == true
while (lo < hi) {
long long mid = lo + (hi - lo) / 2;
if (can_make(mid))
lo = mid + 1;
else
hi = mid;
}
// lo is the first false, so answer = lo - 1
cout << lo - 1 << endl;
return 0;
}