Master Yang, a braver, is going to beat the monster. Yang and the monster each have 3 attributes: hitpoints (HP), offensive power (ATK) and defensive power (DEF).
During the battle, every second the monster's HP decrease by max(0, ATKY - DEFM), while Yang's HP decreases by max(0, ATKM - DEFY), where index Y denotes Master Yang and index M denotes monster. Both decreases happen simultaneously Once monster's HP ≤ 0 and the same time Master Yang's HP > 0, Master Yang wins.
Master Yang can buy attributes from the magic shop of Cyberland: h bitcoins per HP, a bitcoins per ATK, and d bitcoins per DEF.
Now Master Yang wants to know the minimum number of bitcoins he can spend in order to win.
Input
The first line contains three integers HPY, ATKY, DEFY, separated by a space, denoting the initial HP, ATK and DEF of Master Yang.
The second line contains three integers HPM, ATKM, DEFM, separated by a space, denoting the HP, ATK and DEF of the monster.
The third line contains three integers h, a, d, separated by a space, denoting the price of 1 HP, 1 ATK and 1 DEF.
All numbers in input are integer and lie between 1 and 100 inclusively.
Output
The only output line should contain an integer, denoting the minimum bitcoins Master Yang should spend in order to win.
Examples
Input
1 2 1
1 100 1
1 100 100
Output
99
Input
100 100 100
1 1 1
1 1 1
Output
0
Note
For the first sample, prices for ATK and DEF are extremely high. Master Yang can buy 99 HP, then he can beat the monster with 1 HP left.
For the second sample, Master Yang is strong enough to beat the monster, so he doesn't need to buy anything.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to find minimal cost to buy extra HP, ATK, DEF such that after purchasing, Yang's stats (HPY + hp_bought, ATKY + atk_bought, DEFY + def_bought) allow him to win: i.e., in the battle, simultaneously each second, monster's HP decreases by max(0, ATK_Y - DEF_M) and Yang's HP decreases by max(0, ATK_M - DEF_Y). The battle continues until either monster's HP <= 0 or Yang's HP <= 0 (or both). Yang wins if at the moment when monster's HP <= 0, Yang's HP > 0. That implies that the process must result in monster dying while Yang still has positive HP.
Given simultaneous updates, we need to model the fight. Let's denote:
Let deltaY = M_ATK - Y_DEF. Damage to Yang per second: dmgY = max(0, deltaY). Similarly, damage to monster per second: dmgM = max(0, Y_ATK - M_DEF).
We need to determine if Yang wins.
If dmgM == 0, then monster never takes damage, so Yang cannot kill monster (unless monster HP <= 0 initially, but given inputs are at least 1, so no). So requirement: Y_ATK > M_DEF, else impossible (unless monster HP is 0 but not). So we need Y_ATK > M_DEF.
If dmgY == 0, then Yang takes no damage, so he definitely wins as long as he can kill monster (which he will eventually). But note: If dmgY == 0, then Yang's HP never decreases, so he will survive. So condition: Y_ATK > M_DEF and (dmgY == 0 OR Y_HP > 0 after fight). Typically, need Y_HP > 0 when monster's HP reaches <= 0.
Let's compute the number of seconds needed to kill monster: T = ceil(M_HP / dmgM). Because each second deals dmgM damage, and monster dies at the moment when its HP <= 0. The precise condition: after T seconds, total damage = T * dmgM. Monster dies at the end of the T-th second? Let's interpret: "every second the monster's HP decrease by ... while Yang's HP decreases by ... Both decreases happen simultaneously. Once monster's HP ≤ 0 and the same time Master Yang's HP > 0, Master Yang wins." This suggests that after each simultaneous tick, we check conditions. So if initially both are positive, after each second, we subtract damages. If after a second, monster HP <= 0 and Yang HP > 0, Yang wins. If both become <=0 simultaneously, then Yang loses because at that time his HP is not > 0. Actually "Once monster's HP ≤ 0 and the same time Master Yang's HP > 0, Master Yang wins." So if both <=0, Yang does not win.
Thus we need to guarantee that after the T-th second, monster HP <= 0 and Yang HP > 0.
But also if Yang HP becomes <=0 at an earlier second, he loses. So we need that during the T seconds, his HP stays positive after each second? Actually if at some second t < T, Yang's HP becomes <=0, then at that moment monster HP is still >0, so Yang loses immediately; we don't consider later seconds. So we need that for all seconds from 1 to T, after subtracting damage, Yang's HP remains >0 at each step until the step where monster HP <=0, and at that step Yang HP >0.
Thus the condition: T = min integer t such that t * dmgM >= M_HP (so monster dies at time t). At time t, after damage, Yang's HP = Y_HP - t * dmgY. Must be > 0. Additionally, for all i=1..t-1, Y_HP - i * dmgY > 0 (so he hasn't died earlier). But note that if dmgY > 0, then Y_HP - (t-1)dmgY > 0 is automatically ensured if Y_HP - tdmgY > 0? Not necessarily, because if dmgY >0, then Y_HP - (t-1)dmgY = Y_HP - tdmgY + dmgY > Y_HP - tdmgY, so if Y_HP - tdmgY > 0, then Y_HP - (t-1)dmgY > dmgY > 0. So it's automatically >0. Actually we need >0. If Y_HP - tdmgY >0, then Y_HP - (t-1)dmgY = (Y_HP - tdmgY) + dmgY > 0 because dmgY >= 0. So it's automatically positive. The only risk is if dmgY =0, then no issue. So the condition reduces to: Y_HP - T * dmgY > 0. Because if after T seconds Y_HP >0, then for earlier times it's also >0.
But there is a corner: if monster HP exactly becomes 0 at some time t? If monster HP becomes 0 exactly after t seconds, i.e., tdmgM = M_HP, then after t seconds monster HP = 0, condition monster HP <=0 satisfied. Yang HP after t seconds must be >0. If monster HP becomes negative (i.e., tdmgM > M_HP) it's also okay. So we need Y_HP > T * dmgY.
Thus win condition: dmgM > 0 (i.e., Y_ATK > M_DEF) and Y_HP > T * dmgY, where T = ceil(M_HP / dmgM). And dmgY = max(0, M_ATK - Y_DEF).
If dmgY = 0, then condition simplifies to Y_HP > 0, which always true as Y_HP >0 (since HP must be positive after purchase, but initial HP can be >0, and we can buy HP). So it's fine.
Thus overall problem: minimize cost = hp_cost * (hp_buy) + atk_cost * (atk_buy) + def_cost * (def_buy), with integer nonnegative purchases, such that after setting:
All numbers between 1 and 100 initially, and costs between 1 and 100. Purchases can be any nonnegative integers? There is no explicit upper bound given, but we can consider that we don't need to buy beyond certain limits because beyond a point it's wasteful. We need to find minimal cost.
Since values are small (initial up to 100, costs up to 100) we could try to brute-force over reasonable ranges. But we must ensure we capture optimal solution.
We can reason about the constraints: The condition involves product of ceil division and max. This is a combinatorial optimization with three variables. However, we can treat variables as possibly large. But note that if we increase ATK beyond some point, the number of rounds T reduces, and if we increase DEF, dmgY reduces possibly to 0. Similarly, HP needed increases with T * dmgY.
The costs may be such that it's cheaper to increase some attribute. Since all inputs up to 100, we can set bounds for purchases up to some safe limit, like maybe 200 or 1000. But we can derive theoretical bounds: To guarantee victory, we need at least Y_ATK' > DEFM. So we may need to increase ATK if ATKY <= DEFM. Also, we need Y_HP' > T * dmgY. Since HP cost may be high, it might be cheaper to increase ATK or DEF to reduce T or dmgY. But we don't want to overbuy.
Given that all inputs are at most 100, we can consider that we might not need to buy more than, say, 200 or 300 in each attribute. But we can set generous upper bound: Since monster HP <= 100, even if dmgM = 1, T <= 100. Then dmgY <= ATKM (max 100) - Y_DEF' could be up to 100 if DEF is low. So maximum needed Y_HP' is up to T * dmgY <= 100 * 100 = 10000. So HP could need to be as high as 10000. But buying 10000 HP may be costly, but perhaps cheaper than increasing other attributes? Costs can be as low as 1, so maximum needed HP maybe 10000, which is large but still within brute-force if we search over HP? But we need to search over three dimensions. However, we can structure search: For each possible atk_buy and def_buy (or atk' and def'), we can compute the minimal required HP to satisfy Y_HP' > T * dmgY, and then compute cost as aatk_buy + ddef_buy + h*(max(0, required HP - HPY)). But we must also ensure that atk' > DEFM. So we can iterate over possible final ATK from ATKY to some max, and final DEF from DEFY to some max (or maybe lower? Actually decreasing DEF would not help; we might consider not buying DEF or even decreasing? But we can only increase stats (buy). So final DEF >= DEFY, final ATK >= ATKY, final HP >= HPY). So we only increase.
Thus we can iterate over atk' from ATKY to some upper bound U_atk, and def' from DEFY to some upper bound U_def. For each pair, we compute dmgM = atk' - DEFM. If dmgM <= 0, skip (cannot win). Otherwise T = ceil(HPM / dmgM). Let dmgY = max(0, ATKM - def'). Then needed HP = T * dmgY + 1? Actually condition: Y_HP' > T * dmgY. Since Y_HP' must be strictly greater than that product. If Y_HP' is integer, then condition is Y_HP' >= T * dmgY + 1. Because TdmgY may be integer (both integers). But careful: dmgY is integer, T is integer (since ceil). So TdmgY is integer. Condition: Y_HP' > TdmgY, i.e., Y_HP' >= TdmgY + 1. So minimal HP needed = max(HPY, TdmgY+1) ? Actually we can buy HP to reach that, but we cannot decrease HP below HPY. So if HPY already >= needed, no purchase; else need to buy (needed - HPY). So minimal HP cost for given atk', def' is h * max(0, (TdmgY+1) - HPY). But wait: is it exactly TdmgY+1? Let's double-check condition: We need Y_HP' > T * dmgY. So if Y_HP' = TdmgY + 1, then after T seconds, Y_HP' - TdmgY = 1 > 0. That works. If Y_HP' = TdmgY exactly, then after T seconds, Y_HP' - TdmgY = 0, not >0. So Yang would have 0 HP at same time as monster dies? But if both become 0, does Yang win? The problem says: "Once monster's HP ≤ 0 and the same time Master Yang's HP > 0, Master Yang wins." So if Yang's HP = 0, not >0, so does not win. So need >0. So Y_HP' must be at least TdmgY+1.
But also we should consider that if dmgY = 0, then condition is Y_HP' > 0, which is always true because Y_HP' >= HPY >= 1 (since input HPY is at least 1). Actually HPY could be 1. So if dmgY=0, then Y_HP' = HPY (if we don't buy HP) is at least 1 > 0, so condition satisfied. So we don't need to increase HP. So needed HP = 1 is enough. But note: Y_HP' must be at least HPY; but if HPY is already >0, it's fine. So minimal needed is HPY (since we cannot lower HP). So formula: if dmgY = 0, required minimal HP = HPY (since Y_HP' = HPY + hp_buy >= HPY > 0). Actually we could even have Y_HP' = HPY (no extra). So needed extra HP = 0. So formula works: T*dmgY+1 = 0+1 = 1, and HPY is at least 1, so max(0, 1 - HPY) = 0 if HPY >=1. So that's fine. But what if HPY=0? According to input constraints, HPY is at least 1. So all good.
Thus for each atk', def', we compute:
if atk' <= DEFM: skip.
T = ceil(HPM / (atk' - DEFM)). Let dmgY = max(0, ATKM - def').
Now we need to choose appropriate ranges for atk' and def'.
We need atk' at least ATKY and up to some upper bound. Since costs might be high, we might not want to buy huge amounts. But we must consider scenarios where buying huge atk reduces T drastically, and reduces needed HP if dmgY > 0, but maybe it's cheaper to buy HP. Similarly, buying huge def might reduce dmgY to zero, eliminating need for HP. So we need to consider possibilities up to where further increase does not change the condition (i.e., T becomes 1, dmgY becomes 0). Then beyond that, additional atk or def is wasteful because they increase cost but no further reduction in needed HP (since needed HP would be either 1 if dmgY=0, or maybe if dmgY>0 but T=1, need_HP = dmgY + 1). But we might also consider that atk' could be increased to reduce T further? Once T=1, increasing atk' does not change T, but might increase dmgY? dmgY is unaffected by atk. So no effect on needed HP. So there is no benefit beyond making T=1 and dmgY=0. However, maybe we need to consider that atk' must be > DEFM; that's minimal atk'. So the effective range for atk' is from max(ATKY, DEFM+1) up to some bound beyond which T=1 and perhaps dmgY can't be reduced further (but def can reduce dmgY). But T becomes 1 when atk' - DEFM >= HPM. So atk' >= HPM + DEFM. Since HPM <= 100 and DEFM <= 100, atk' <= 200 for T=1. Actually if atk' - DEFM >= HPM, then T=1. So atk' >= HPM + DEFM. So atk' up to HPM+DEFM is enough to get T=1. But could there be a scenario where we need atk' higher than that? Not for T=1. However, consider dmgY: def' can be increased to reduce dmgY. If we increase def' to >= ATKM, then dmgY=0. ATKM <= 100, DEFY <= 100, so def' up to ATKM is enough to get dmgY=0. Actually need def' >= ATKM to make dmgY=0 (since dmgY = max(0, ATKM - def')). So def' max needed is ATKM. But could there be scenario where we increase def' beyond ATKM? No effect. So we can limit def' to ATKM (or maybe ATKM if we want dmgY=0, but also if we keep def' lower, dmgY positive; but we might not need to go beyond ATKM). So def' upper bound: max(DEFY, ATKM). Actually we might not need to increase def' beyond ATKM because that gives dmgY=0; any higher is waste. So we can limit def' to ATKM. But note: if ATKM is <= DEFY, then we already have dmgY=0, no need to increase. So def' from DEFY to ATKM (if ATKM > DEFY) else only DEFY. So upper bound for def' is max(DEFY, ATKM). But is there any scenario where buying def' beyond ATKM reduces need_HP further? No, because dmgY already zero, need_HP = 1. So no.
Similarly, atk' upper bound: we can set atk' from max(ATKY, DEFM+1) to maybe something like DEFM + HPM (since that gives T=1) but also possibly atk' could be increased beyond that if dmgY > 0 to reduce T? T already 1, cannot reduce further. So no benefit. But what if we need atk' > DEFM + HPM to also reduce needed HP? T already 1, need_HP = dmgY * 1 + 1 = dmgY + 1, which is independent of atk'. So no. So atk' max needed is max(ATKY, DEFM + HPM). But also there could be synergy: If we increase atk', we also might reduce T, which reduces needed HP if dmgY > 0. So we need to consider atk' up to DEFM + HPM. However, if HPM is 100 and DEFM 100, then atk' could be up to 200. That's fine.
But wait, there is also the possibility that we might not need to increase atk' to exactly DEFM+HPM; maybe a slightly lower atk' gives T=2 but dmgY is high, so buying HP could be cheaper than further increasing atk'. Our enumeration over atk' up to DEFM+HPM should capture that because we consider all atk' values. But what if atk' lower than DEFM+HPM leads to T > 1, but maybe increasing atk' beyond DEFM+HPM is never beneficial because beyond that T=1 and no further reduction in needed HP. So we can set atk' max = DEFM + HPM (or maybe DEFM+HPM -1? Actually to achieve T=1 we need atk' - DEFM >= HPM, so atk' >= DEFM + HPM. So the smallest atk' that yields T=1 is DEFM + HPM. So atk' = DEFM + HPM yields T=1. Atk' = DEFM + HPM - 1 yields T = ceil(HPM / (HPM-1)) = 2 if HPM >1; if HPM=1 then atk'=DEFM yields T=inf? Actually if HPM=1, then atk'=DEFM+1 gives dmgM=1, T=1. So atk' >= DEFM+1 yields T=1. So for HPM=1, DEFM+HPM = DEFM+1, so atk'=DEFM+1 gives T=1. So indeed atk' up to DEFM+HPM suffices. But also we should consider if ATKY itself may be larger than DEFM+HPM; then we can consider atk' = ATKY (no purchase) if it's already >= DEFM+1. So upper bound could be max(ATKY, DEFM+HPM). But if ATKY > DEFM+HPM, then we might not need to consider larger values because that would be wasteful. However, we must also consider that ATKY could be larger than DEFM+HPM and still dmgY>0; we might still need HP. So we need to consider atk' = ATKY if it's > DEFM. So we can set atk' range from ATKY (or at least DEFM+1) up to max(ATKY, DEFM+HPM). Because if ATKY > DEFM+HPM, increasing further does not help. But what about ATKY being less than DEFM+HPM? We'll go up to DEFM+HPM. That's reasonable.
But is it possible that buying atk' beyond DEFM+HPM could allow us to reduce def' or HP? Since atk' doesn't affect dmgY, no. So no.
Thus atk' bound = max(ATKY, DEFM + HPM). Similarly, def' bound = max(DEFY, ATKM). However, there is a nuance: Could we increase def' beyond ATKM to reduce dmgY to negative? But max(0, ATKM - def') ensures dmgY is zero for def' >= ATKM. So beyond ATKM gives dmgY=0, same as at ATKM. So no benefit.
But what about increasing atk' beyond DEFM+HPM but also maybe def' such that dmgY still >0, but then T stays 1, so need_HP = dmgY+1, unchanged. So no benefit.
Thus these bounds should suffice. But we should also consider that we might not need to increase def' up to ATKM if we can compensate with HP and atk'. So enumeration up to these bounds is fine.
But what about HP? The minimal HP needed is determined. We compute hp_buy = max(0, need_HP - HPY). Since need_HP may be as high as T * dmgY + 1, with T up to maybe 100 (if dmgM=1) and dmgY up to ATKM (max 100) if def' low, so need_HP up to 100*100+1=10001. So hp_buy could be up to 10001 - HPY, which is about 10000. But we are not enumerating HP; we compute it directly. So that's fine.
Thus algorithm: Loop for atk' from ATKY to atk_max, increment by 1. For def' from DEFY to def_max, increment by 1. If atk' <= DEFM, skip. Compute dmgM = atk' - DEFM. T = ceil(HPM / dmgM). Compute dmgY = max(0, ATKM - def'). need_HP = T * dmgY + 1. hp_buy = max(0, need_HP - HPY). cost = a*(atk'-ATKY) + d*(def'-DEFY) + h*hp_buy. Keep min.
But note that we might also consider not buying any attribute if ATKY > DEFM and the condition already holds without extra HP or def. So the loop will include atk' = ATKY, def' = DEFY, compute need_HP and see if need_HP <= HPY. If so, cost = 0. So that's fine.
Complexity: atk_max - ATKY + 1 <= maybe 200 if ATKY=1 and DEFM+HPM up to 200, so ~200. def_max - DEFY + 1 <= ATKM - DEFY + 1 <= 100. So at most 200*100 = 20,000 iterations, trivial.
But are there any edge cases where increasing def' beyond ATKM could be beneficial because it might reduce something else? No. So bound is safe.
But wait: What about increasing atk' beyond DEFM+HPM? Possibly to reduce T? T already 1, can't reduce further. So no.
But what if ATKM is 0? Input constraints say ATKM >= 1. So okay.
One nuance: When dmgY=0, need_HP = 1. But if HPY is 0? Not possible. So fine.
But is it always enough to consider def' up to ATKM? Could there be scenario where we increase def' beyond ATKM and this somehow reduces need_HP further? Since need_HP = T*dmgY+1, and dmgY=0 for def' >= ATKM, need_HP=1. That's minimal. So no.
Our loop:
ATKY=2, DEFY=1. atk_max = max(ATKY, DEFM+HPM) = max(2, 1+1=2) =2. So atk'=2 only.
def_max = max(DEFY, ATKM) = max(1,100)=100. So def' from 1 to 100.
We skip atk' <= DEFM? atk'=2, DEFM=1, so OK.
dmgM=2-1=1. T = ceil(1/1)=1.
For def'=1: dmgY=100-1=99, need_HP=199+1=100, HPY=1 => hp_buy=99. cost = a(0) + d*(0) + 199 = 99. def'=2: dmgY=98, need_HP=99, hp_buy=98 -> cost=98? Wait, but we also pay d cost for def' increase? def' increase from 1 to 2 costs d=100 per point, so cost = a0 + d*(2-1=1)100 + h? Actually hp_buy = need_HP - HPY = (198+1=99) -1 =98? Actually dmgY = max(0, 100-2)=98, need_HP=981+1=99, hp_buy=98, cost = 0 + 1001 + 198 = 198, larger. So best is def'=1, hp_buy=99, cost=99. But note: Could we increase atk'? But atk_max is only 2. Actually maybe increasing atk' to something higher could reduce need_HP? But atk' cannot go beyond 2 according to our bound, but ATKY=2 and DEFM+HPM=2, so bound 2. But what if we buy ATK? That would increase atk' to 3, which is > 2. But then atk_max would be max(2,2) =2, missing 3. Our bound is max(ATKY, DEFM+HPM). In this case ATKY=2, DEFM+HPM=2, so bound=2. But is it possible that buying ATK (making atk'=3) could lead to lower cost? Let's check: atk'=3, DEFM=1 => dmgM=2, T=ceil(1/2)=1. dmgY still 99 if def'=1 => need_HP=100, hp_buy=99, cost = a*(3-2=1)*100 =100 plus hp_buy=99? Actually need_HP=100, hp_buy=99, cost=100+99=199, plus no def increase. So cost=199, worse. But could we reduce hp_buy by increasing ATK? Since T=1 anyway, need_HP unchanged. So increasing ATK only adds cost. So not beneficial. So bound is okay. However, what about scenarios where DEFM+HPM is less than ATKY? Then bound is ATKY. But if ATKY is huge (like 200) but initial condition maybe not winning. But then we might need to increase ATK further? If ATKY > DEFM+HPM, then T=1 already (since atk' already >= DEFM+HPM). So we don't need to increase ATK; we could even consider decreasing ATK? But we cannot decrease. So atk' = ATKY is the minimal (since we can't decrease). So it's enough to consider only atk' = ATKY? But what if ATKY > DEFM+HPM but also ATKY is less than something? Actually if ATKY > DEFM, we already satisfy dmgM > 0. If ATKY >= DEFM+HPM, then T=1. But we might still need to buy HP or DEF. So atk' fixed at ATKY is the only choice? But we could also buy more ATK, but that would increase cost without benefit (since T already 1). However, if ATKY > DEFM+HPM, then atk'=ATKY is the base. So we need only consider atk' = ATKY. But there could be scenario where ATKY > DEFM but < DEFM+HPM, then we consider up to DEFM+HPM. So bound = max(ATKY, DEFM+HPM) is correct because we can't go below ATKY, and we don't need to go above that bound.
But wait: What about scenario where ATKY > DEFM+HPM? Then our bound max = ATKY, so we will only consider atk' = ATKY. That's fine. Because any higher atk' only adds cost.
Thus algorithm stands.
But we must also consider that we could buy only HP, or only ATK, or only DEF, or combinations. Our loops include combos.
One more nuance: The condition Y_HP' > TdmgY. But is it necessary that Y_HP' > TdmgY? Let's test scenario with dmgY > 0. Suppose Y_HP' = T*dmgY exactly. Then after T seconds, Y_HP' becomes 0. At that moment, monster HP is <=0. But is it <=0? Let's see: T = ceil(HPM / dmgM). Let actual damage dealt = dmgM * T >= HPM. If it's exactly HPM, monster HP becomes 0. So both become 0. According to rule, Yang does not win because his HP is not >0. So correct.
What if damage to monster exceeds HPM, so monster HP becomes negative earlier? But we simulate per second simultaneously. At each second, we subtract. Suppose after some second, monster HP goes <=0, but also Yang HP goes <=0 at the same second? Actually the condition is "Once monster's HP ≤ 0 and the same time Master Yang's HP > 0, Master Yang wins." So at the end of the second, we check both. So if after the same second, monster HP <=0 and Yang HP >0, win. If both <=0, lose. So our T is the number of seconds needed for monster HP to become <=0. Could it be that monster dies earlier than T? Since T is the smallest integer such that TdmgM >= HPM. So yes, after T seconds monster HP <=0. If TdmgM > HPM, monster HP becomes negative during that second? But it's simultaneous: we subtract dmgM each second; after T-1 seconds, monster HP >0; after T seconds, we subtract dmgM and then check. So monster HP becomes HPM - T*dmgM <= 0. So it's <=0 at that moment. So our T is correct. So condition is as above.
But what if we kill monster in the middle of the second? No, it's discrete per second.
Thus algorithm is sound.
Edge Cases: If dmgY = 0, need_HP = 1, okay.
If ATKY <= DEFM, we need to buy at least enough ATK to exceed DEFM by at least 1. So atk' minimal = DEFM+1. Since we start loop at ATKY, we might miss DEFM+1 if ATKY < DEFM+1. But we include from ATKY to atk_max, so DEFM+1 will be considered if it's within range. Since atk_max = max(ATKY, DEFM+HPM), which is at least DEFM+HPM, definitely >= DEFM+1. So it's included.
But what if DEFM+HPM is less than DEFM+1? That would be impossible because HPM>=1 => DEFM+HPM >= DEFM+1.
Thus fine.
Now, about the hp_buy formula: need_HP = T * dmgY + 1. But if dmgY = 0, need_HP = 1. Since HPY >=1, hp_buy = max(0, 1 - HPY) = 0. So good.
But is there any scenario where we could have Y_HP' exactly = T*dmgY + epsilon? That's covered.
But consider dmgY = 0 but also maybe Yang dies before monster? No, because he takes no damage.
Thus minimal cost computed.
But let's test some additional cases manually to ensure no oversight.
Case: Yang initial: HP=10, ATK=5, DEF=0. Monster: HP=10, ATK=5, DEF=0. Prices: all 1. Initially, dmgM = 5-0=5, T = ceil(10/5)=2. dmgY = 5-0=5. Need HP > 25=10 => need >10, so need at least 11. HP=10, so need 1 more HP. cost=1. But can we win with 0 cost? If we don't buy, after 1st second: Yang HP=5, monster HP=5; after 2nd second: Yang HP=0, monster HP=0 => lose. So need 1 HP. So answer 1. Our algorithm: atk'=5, def'=0 => need_HP = 25+1=11, hp_buy=1, cost=1. Good.
Case: HP=20, ATK=5, DEF=0, Monster: HP=10, ATK=5, DEF=0. Then dmgM=5, T=2, need HP > 10, HP=20 >10, so win without purchases. Our algorithm: need_HP = 2*5+1=11, HPY=20 => hp_buy=0, cost=0. Good.
Case: ATK already > DEFM, but DEF is low such that dmgY high; buying DEF may be cheaper than buying HP. Example: HPY=1, ATKY=10, DEFY=0; Monster: HPM=100, ATKM=100, DEFM=0. Prices: h=100, a=1, d=1. Initially, dmgM=10, T=ceil(100/10)=10. dmgY=100, need_HP=10100+1=1001, hp_buy=1000 => cost=1001000=100000 (if only HP). If we increase ATK: atk'=11 => dmgM=11, T=ceil(100/11)=10 (since 911=99, 1011=110, so T=10 still? Actually ceil(100/11)=10), dmgY still 100, need_HP=1001, hp_buy still 1000, but plus atk cost 1 => 100001. Not helpful. Increase ATK to 20: dmgM=20, T=5, need_HP=5100+1=501, hp_buy=500, cost = a(10)+ h500 =10 + 50000 =50010. Increase ATK to 100: dmgM=100, T=1, need_HP=100+1=101, hp_buy=100, cost = a90 + h100 = 90 + 10000 = 10090. Now consider increasing DEF: def' increase to 100 (cost d=1 per point). atk'=10, def'=100 => dmgY = max(0,100-100)=0, need_HP=1, hp_buy=0 (since HPY=1), cost = d100 = 100. That's much cheaper. So algorithm should find that: atk'=10 (fixed), def'=100 (DEFY=0 to 100, cost 100), need_HP=1, cost=100. That's minimal. So works.
Now, could there be scenario where we need to increase both ATK and DEF? Already considered.
Now, what about scenario where ATKY <= DEFM, so we must buy ATK. That's also covered.
Now, are there any scenarios where we need to consider increasing HP beyond need_HP = T*dmgY+1 because of integer rounding? Already used strict >. So done.
But one more nuance: The condition Y_HP' > TdmgY ensures after T seconds Yang is alive, but what about before T seconds? As argued, if Y_HP' > TdmgY, then for any t<T, Y_HP' - tdmgY >= Y_HP' - (T-1)dmgY = (Y_HP' - TdmgY) + dmgY > 0 because dmgY >=0 and Y_HP' - TdmgY >= 1? Actually if Y_HP' > TdmgY, then Y_HP' - TdmgY >= 1. Then Y_HP' - (T-1)dmgY = Y_HP' - TdmgY + dmgY >= 1 + dmgY > 0. So safe. So condition is sufficient.
Thus algorithm is correct.
Now, some implementation details: Ceil division: (HPM + dmgM - 1) // dmgM. But careful: dmgM could be 0; we skip those.
Now, we need to ensure we use 64-bit ints for cost, but cost maximum? worst case: we might buy a lot of HP. HP needed could be up to 10000, HP cost up to 100 => 1e6. ATK and DEF purchases up to 200 each, cost up to 100 => 200*100=20000 each. So total under 2e6, fits 32-bit. But safe to use long long.
Now, boundaries: atk_max = max(ATKY, DEFM + HPM). However, what if ATKY is already > DEFM+HPM, then atk_max = ATKY, but that means we only consider atk' = ATKY. But could there be scenario where ATKY > DEFM+HPM but we still might want to increase ATK to reduce something else? Since T is already 1, dmgY unaffected, so need_HP unchanged. So no benefit. So fine.
But what about def'? def_max = max(DEFY, ATKM). If DEFY already >= ATKM, def_max = DEFY, meaning we only consider def' = DEFY. That is correct because we cannot decrease DEF, so def' must be >= DEFY. If DEFY >= ATKM, then dmgY = max(0, ATKM - def') = 0 (since def' >= DEFY >= ATKM), so need_HP = 1. So no need to increase DEF further. So only def'=DEFY needed.
But wait: Could there be scenario where DEFY > ATKM, but increasing DEF further might be beneficial? No, because dmgY already zero. So no.
Thus bound is correct.
Now, one more thought: Could it be beneficial to increase ATK beyond DEFM+HPM if dmgY > 0? Not for need_HP, but could it be that T becomes 1 and then need_HP = dmgY+1; but if we also increase DEF, need_HP reduces; but increasing ATK beyond that doesn't help. So not.
Now, are there any tricky cases where dmgY is positive but we could still win without buying HP if initial HP is high enough? That's covered.
Now, implement.
But wait: Could there be scenario where Y_ATK' > DEFM, but also Y_HP' exactly = TdmgY, but we can win if monster dies earlier? Let's reconsider: Suppose monster HP=10, dmgM=6, then after 1 second: damage=6, monster HP=4; after 2 seconds: damage=12, monster HP = -2. So T=2. Yang HP needed > 2dmgY. But what if after 2 seconds, Yang HP = 2dmgY exactly, which is 0? Actually 2dmgY. So he dies at the same second. But what about after 1 second? He still alive. At the end of 2 seconds, both die. No win. However, could it be that after 2 seconds, monster HP becomes negative, but Yang HP maybe positive if damage to monster exceeded required? But we subtract full dmgM each second; the overkill doesn't matter. So condition remains.
But what about if dmgM is such that after first second, monster HP <=0? Then T=1. So condition is Y_HP > dmgY. If Y_HP == dmgY, after first second Yang HP becomes 0 and monster HP becomes <=0. So lose.
Thus condition stands.
Now, consider if dmgY = 0, then we only need Y_HP > 0, trivial.
All good.
Now, we should also consider the possibility that we might not need to strictly increase ATK beyond DEFM; maybe we can win if ATKY == DEFM? Let's test: if ATKY == DEFM, dmgM = 0, monster never takes damage, cannot win. So need >.
Now, confirm with scenario: Monster DEF high, but we can buy ATK to exceed.
Now, consider scenario where ATKY > DEFM, but after adding HP and maybe DEF, we satisfy condition. Good.
Now, we should also consider the possibility that we could buy negative stats? No.
We need to find minimal cost. Let's think: ATK must be >1, so at least 2. So atk' from 2 to DEFM+HPM=1+100=101. def' from 1 to ATKM=100. We'll compute many combos.
We could compute programmatically later, but conceptually, cheapest might be to raise ATK just enough to make dmgM high, and rely on HP? Or raise DEF to reduce dmgY. Because dmgY high (99 if def=1). Let's try:
Option2: Increase DEF only. Keep atk=1? But atk=1 not > DEFM=1, so need atk>1, so must increase ATK at least to 2. So we consider combos. Increase DEF to 100 (cost 99) and ATK to 2 (cost1). Then dmgY=0, need_HP=1, hp_buy=0. Total cost = 1+99=100. That's cheaper. Could we do better: increase DEF to 99 (cost 98), ATK to 2 (cost1). Then dmgY= max(0,100-99)=1, need_HP = TdmgY+1. For atk'=2, dmgM=1, T=100, need_HP=1001+1=101, hp_buy=100, cost = atk(1) + def(98) + hp(100) = 199. Worse. So def=100 better. Maybe ATK=3, def=100: cost 2+99=101, need_HP=1, total 101. So minimal seems 100.
Option3: Increase ATK to 101 (cost100), def=1, hp_buy=99 => 199. So best is 100. Our algorithm should find atk'=2, def'=100, cost=100.
Now, confirm that atk'=2 is allowed? atk' must be >= ATKY=1, we loop from 1 to atk_max=101, so includes 2. def' from 1 to 100, includes 100. So yes.
Now, what about atk'=101, def'=100? dmgM=100, T=1, dmgY=0, need_HP=1, cost = atk(100) + def(99) =199. Not better.
So algorithm works.
Now, is there any scenario where we need to consider atk' values beyond DEFM+HPM because increasing atk' may allow us to reduce def'? But def' reduction would increase dmgY, requiring more HP. Since we already have def' as variable, we can choose def' high to reduce dmgY. But we might also increase atk' to reduce T and thus need_HP if dmgY>0. However, the bound for atk' for reducing T is up to DEFM+HPM. Beyond that T=1. So atk' beyond that does not reduce T further. So no.
But what if we increase atk' to an extreme so that T becomes 1, but we also need to ensure that dmgY is zero or low. But that is independent. So the combination (atk'=something between ATKY and DEFM+HPM, def' high) might be cheaper than (atk'=DEFM+HPM, def' high). Our loop includes all atk' within bound, so we will compare.
Thus algorithm is complete.
But wait: Could there be scenario where buying HP is cheaper than buying ATK or DEF, and we need to consider atk' values beyond DEFM+HPM? No because atk' beyond DEFM+HPM doesn't change T, so doesn't reduce needed HP. So it would be strictly more expensive than atk'=DEFM+HPM (or the minimal atk' that yields T=1). However, if ATKY > DEFM+HPM, then the minimal atk' is ATKY, and any higher atk' is worse. So we don't need beyond bound.
Now, one nuance: What about the possibility that we could also buy negative HP? Not allowed.
Thus answer is minimum over loop.
Now, I want to double-check the condition "Once monster's HP ≤ 0 and the same time Master Yang's HP > 0, Master Yang wins." Does it mean that if at any point monster HP <=0 and Yang HP >0, he wins, even if later Yang HP becomes <=0? But since the battle ends when someone dies? Typically it would stop. The description: "Both decreases happen simultaneously Once monster's HP ≤ 0 and the same time Master Yang's HP > 0, Master Yang wins." It implies that the condition is checked after each simultaneous decrease. As soon as it happens, Yang wins and battle stops. So our condition that after T seconds monster HP <=0 and Yang HP >0 is necessary and sufficient. And we don't need to consider after that.
Now, confirm the formula for need_HP: Y_HP' must be > TdmgY. But consider if dmgY is so high that Yang dies before T seconds? But we argued if Y_HP' > TdmgY, then for any t < T, Y_HP' > tdmgY (since dmgY >=0). However, if Y_HP' = TdmgY + 0.5, but integer, so Y_HP' >= T*dmgY+1 ensures >. So fine.
Now, consider if dmgY is 0, then no damage, so condition trivially satisfied.
Thus algorithm stands.
Now, we need to code.
Implementation steps:
Read HPY, ATKY, DEFY.
Read HPM, ATKM, DEFM.
Read h, a, d.
Set atk_min = ATKY, atk_max = max(ATKY, DEFM + HPM). (Because atk' must be >= ATKY, and we consider up to DEFM+HPM, but if ATKY is larger, that's the max we consider.)
But also we might want to consider atk' values a bit higher than DEFM+HPM if ATKY > DEFM+HPM? Actually then atk_max = ATKY, which is fine.
However, to be safe we might set atk_max = max(ATKY, DEFM + HPM). Actually if ATKY > DEFM+HPM, then atk_max = ATKY, but we would not consider any atk' higher than ATKY. But maybe ATKY is huge, but we don't need higher; it's fine.
But what if ATKY < DEFM+HPM, then atk_max = DEFM+HPM. That includes needed range.
Could there be scenario where ATKY > DEFM+HPM but we could win without buying any ATK? Yes if ATKY > DEFM and condition satisfied. But if not satisfied, we may need to buy HP or DEF. But we could also consider buying more ATK if it somehow reduces required HP? But T is already 1 because ATKY > DEFM+HPM? Actually if ATKY > DEFM+HPM, then dmgM = ATKY - DEFM > HPM, so T = 1. So T cannot be reduced further. So buying more ATK only increases cost without benefit. So we should not consider higher ATK. So bound is okay.
However, what if ATKY > DEFM+HPM, but ATKY is not > DEFM? That can't happen because if > DEFM+HPM then > DEFM.
So bound is safe.
Set def_min = DEFY, def_max = max(DEFY, ATKM). Similar reasoning. If DEFY > ATKM, def_max = DEFY; else def_max = ATKM.
Initialize answer = large value (like 10^9).
Loop for atk' from atk_min to atk_max inclusive:
for def' from def_min to def_max inclusive:
if atk' <= DEFM: continue
dmgM = atk' - DEFM
T = (HPM + dmgM - 1) // dmgM
dmgY = max(0, ATKM - def')
need_HP = T * dmgY + 1
hp_buy = need_HP - HPY
if hp_buy < 0: hp_buy = 0
cost = a * (atk' - ATKY) + d * (def' - DEFY) + h * hp_buy
ans = min(ans, cost)
Output ans.
But wait: Is it correct to loop over def' from def_min to def_max? If def' is increased, cost increases, but need_HP may decrease. Our loop includes all possible def'. But is there any reason to consider def' beyond ATKM? No, as argued. So bound is correct.
Similarly, is there any reason to consider def' lower than def_min? No, we can only increase.
Now, but what about atk'? We loop from atk_min which is ATKY. But if ATKY <= DEFM, then some atk' values between ATKY and DEFM are invalid (<=DEFM). We skip them. So we start from ATKY but we need to consider atk' values from DEFM+1 onward. Since we include all atk' from ATKY to atk_max, and skip those <=DEFM, it's fine.
Now, what about atk' values less than ATKY? Not allowed. So start from ATKY.
Now, are there any scenarios where we don't need to buy any ATK but we may want to buy less ATK? Not possible.
Now, one more nuance: The condition need_HP = T*dmgY + 1. But if dmgY = 0, need_HP=1. But what if HPY is 0? Input says HPY >=1, so fine.
Now, does the formula need to consider that if dmgY > 0 and HPY is huge, we might not need to buy HP, but we could also consider buying less HP? No.
Now, test on a corner: HPY=1, ATKY=100, DEFY=0, Monster: HPM=1, ATKM=100, DEFM=0. Prices all 1. Atk'=100 (no purchase), def'=0, dmgM=100, T=1, dmgY=100, need_HP=101, hp_buy=100, cost=100. But maybe we can buy DEF to reduce dmgY: def'=100, cost=100, need_HP=1, total=100, same cost. But maybe buy ATK to 101? T still 1, dmgY=100, need_HP=101, hp_buy=100, atk cost 1 => 101, worse. So answer 100. But can we win with less? Actually initial: Yang HP=1, ATK=100, DEF=0. Monster HP=1, ATK=100, DEF=0. They hit each other: after 1 sec, Yang takes 100 damage, goes to -99, monster takes 100 damage, goes to 0. At that instant, Yang HP is -99 (<=0), monster HP=0, Yang does not win. So need to increase HP or DEF. So min cost is 100. So ok.
Now, what if prices: h=100, a=1, d=1. Then maybe increase ATK to 101? Then dmgM=101, T=1, dmgY=100, need_HP=101, hp_buy=100 cost=100*100=10000 if buy HP, but atk cost 1, total=10001. Better to increase DEF to 100: cost=100, need_HP=1, total=100. So still 100. So algorithm would find that.
Now, what if prices: h=1, a=100, d=100. Then buying HP 100 cost 100, total 100. So answer 100. Good.
Now, let's think about another corner: Could it be optimal to buy both ATK and DEF to intermediate values? Our loops include combos.
Now, we should also consider that buying HP may be cheaper than increasing DEF. Our loops will consider all def' values, including def'=DEFY, and compute needed HP accordingly.
Now, is it possible that we might need to buy more HP than just need_HP to satisfy > condition? No, because if we have exactly need_HP we are > TdmgY? Actually need_HP = TdmgY+1 ensures >. So minimal.
Now, verify that hp_buy could be negative if need_HP <= HPY? We set to 0.
Now, one more nuance: The battle might end earlier if monster HP becomes <=0 before the full T seconds? Actually T is the minimum number of seconds such that monster HP <=0, so it's exactly the number of seconds needed. So no earlier.
Now, check scenario where dmgM is so high that after first second monster HP <=0, T=1. That's covered.
Now, final answer should be integer.
Now, we must also consider that we could buy zero of some attributes; loops include that.
Now, before coding, let's test with some random small cases by mental or quick script, but we trust.
Now, I'll consider if there is any scenario where increasing ATK beyond atk_max could allow us to reduce needed HP due to rounding? For instance, if dmgM is such that TdmgY product is not monotonic? As ATK increases, dmgM increases, T decreases (non-increasing). The product TdmgY may not be monotonic in atk' because T can drop only at thresholds, while dmgY unchanged (since def fixed). So the product is stepwise decreasing as atk' increases. But beyond atk' = DEFM+HPM, T=1. So product = 1*dmgY, which is constant. So further increase does not change product. So no.
Now, what about increasing def'? dmgY decreases, product decreases. Once def' >= ATKM, product=0. So beyond that no change.
Thus bounds fine.
Now, the loop size: atk_max - ATKY <= max(0, DEFM+HPM - ATKY). Since ATKY min 1, DEFM+HPM max 200, so at most 199. def_max - DEFY <= max(0, ATKM - DEFY) <= 99. So at most 200*100=20k iterations.
Now, one more nuance: what if ATKY is already > DEFM+HPM? atk_max = ATKY. Then the loop for atk' only includes that single value. But could there be a scenario where ATKY > DEFM+HPM but we might want to increase ATK further because we also increase DEF? But increasing ATK further does not affect needed HP because T=1 and dmgY unchanged. So it only adds cost. So not needed.
Thus algorithm is correct.
Now, let's think about a pathological scenario: Prices such that buying ATK is cheap, but HP is expensive. Could we benefit from buying so much ATK that T becomes 1, but also need HP is high because dmgY >0? But we could also buy DEF to reduce dmgY. But maybe buying ATK to extreme reduces T but dmgY stays same, so need_HP may still be high but maybe cheaper than buying HP? Let's test: HPY=10, ATKY=5, DEFY=0; HPM=100, ATKM=100, DEFM=0; prices: h=1000, a=1, d=100. Initially need: atk'=5: dmgM=5, T=20, dmgY=100, need_HP=20100+1=2001, hp_buy=1991 cost=19911000=1,991,000 + atk cost 0? Actually no atk purchase yet, so huge. Buying ATK: atk'=100 (cost 95), dmgM=100, T=1, dmgY=100, need_HP=101, hp_buy=91, cost = 951 + 911000 = 95 + 91000 = 91095. Buying DEF: def'=100 (cost 100*100=10000), atk'=5: dmgM=5,T=20,dmgY=0,need_HP=1,hp_buy=0, total cost=10000. That's cheaper. Buying ATK=100 and DEF=100: cost=95+10000=10095, need_HP=1, total 10095, slightly higher than just DEF. So best is DEF only. Our loops would find that: atk'=5, def'=100 gives cost=0+10000+0=10000. atk'=100, def'=100 gives 95+10000=10095, not better. So algorithm finds min.
Now, what about buying ATK to 101? Not considered because atk_max = max(5,0+100=100) = 100. So atk'=101 not considered. That's fine because cost would be 96+10000=10096 >10000.
Now, consider scenario where DEFY already > ATKM, so dmgY=0. Then need_HP=1. The condition reduces to atk' > DEFM. If ATKY > DEFM, then cost 0. If not, we need to buy ATK. So algorithm: def_max = max(DEFY, ATKM) = DEFY (since DEFY > ATKM), so def' only = DEFY. Loop over atk' from ATKY to atk_max. This includes the needed ATK increase. Good.
Now, consider scenario where ATKY > DEFM but not enough to win because HP is insufficient. Then we might buy HP or DEF. Loop includes def' from DEFY to ATKM, could be DEFY if DEFY <= ATKM. So fine.
Thus algorithm works.
Now, there is one more nuance: When computing T = ceil(HPM / dmgM), we must be careful with integer division. Use (HPM + dmgM - 1) // dmgM.
Now, all good.
Now, let's code in C++.
Pseudo:
#include
#include
#include
using namespace std;
int main() {
int HPY, ATKY, DEFY;
int HPM, ATKM, DEFM;
int h, a, d;
cin >> HPY >> ATKY >> DEFY;
cin >> HPM >> ATKM >> DEFM;
cin >> h >> a >> d;
code
int atk_min = ATKY;
int atk_max = max(ATKY, DEFM + HPM); // but also we might want to consider atk_max = max(ATKY, DEFM+ HPM) maybe plus something? Not needed.
int def_min = DEFY;
int def_max = max(DEFY, ATKM);
long long ans = 1e18; // large
for (int atk = atk_min; atk <= atk_max; ++atk) {
if (atk <= DEFM) continue;
int dmgM = atk - DEFM;
int T = (HPM + dmgM - 1) / dmgM;
for (int def = def_min; def <= def_max; ++def) {
int dmgY = max(0, ATKM - def);
long long needHP = (long long)T * dmgY + 1;
long long hp_buy = needHP - HPY;
if (hp_buy < 0) hp_buy = 0;
long long cost = (long long)a * (atk - ATKY) + (long long)d * (def - DEFY) + (long long)h * hp_buy;
if (cost < ans) ans = cost;
}
}
cout << ans << endl;
return 0;
}
Now, but wait: Is it safe to set atk_max = max(ATKY, DEFM + HPM)? Consider scenario where DEFM+HPM is 200, but ATKY is 150. That's fine. But what if ATKY is 250? Then atk_max = 250, but we argued we don't need to consider higher than ATKY. That's correct because we can't lower ATK, so only atk=ATKY is possible without purchase, but we could purchase more ATK, but it's wasteful. However, could there be scenario where increasing ATK beyond ATKY reduces need_HP if dmgY >0? But T already 1 if ATKY > DEFM+HPM? Let's check: if ATKY > DEFM+HPM, then dmgM > HPM, so T=1. So need_HP = dmgY + 1. Increasing ATK doesn't change dmgY. So no effect. So increasing ATK is wasteful. So we can skip. But our loop would include atk values from ATKY to atk_max (ATKY). That's only one value. But if ATKY > DEFM+HPM, atk_max = ATKY. That's fine. But if ATKY > DEFM+HPM, but we might need to increase ATK because ATKY itself might be > DEFM+HPM but less than something? Already > DEFM+HPM ensures T=1. So no need. So atk_max = ATKY is correct. But our code sets atk_max = max(ATKY, DEFM+HPM). That would be ATKY if ATKY >= DEFM+HPM, else DEFM+HPM. That's correct.
But what about a scenario where ATKY is > DEFM+HPM, but we might want to consider atk values between DEFM+HPM and ATKY? That's not possible because ATKY is the starting point; we only consider atk >= ATKY. So atk values below ATKY are not allowed. So we only consider atk = ATKY. That's fine.
Now, consider if ATKY is huge, but we might want to consider atk values lower than ATKY? Not allowed. So okay.
Thus code works.
But wait, is there any scenario where we might want to buy both ATK and DEF, but not increase ATK to atk_max? That's covered.
Now, one more nuance: We assumed that we can buy any integer amount. Yes.
Now, let's test with some random values via reasoning.
Edge: HPY=100, ATKY=1, DEFY=1; HPM=100, ATKM=1, DEFM=100; prices: all 1. Here DEFM=100, ATKY=1, so dmgM = 1-100 <=0, need to buy ATK to at least 101. atk_max = max(1,100+100=200) =200. So atk' from 1 to 200, skip <=100. For atk'=101, dmgM=1, T=100. dmgY = max(0,1 - def'). def' can be DEFY=1, dmgY=0 (since ATKM=1, def'=1 => dmgY=0? max(0,1-1)=0). Actually ATKM=1, DEFY=1, so dmgY=0. So need_HP=1. So cost = a*(100) + 0 =100. But could we also buy ATK=102? cost 101, same dmgM=2? Actually atk'=102, dmgM=2, T=50, need_HP=1, cost=101. So best is atk'=101 cost 100. Also could we buy ATK=101 and also maybe reduce def'? Not needed. So answer 100. But is there a cheaper option? Could we buy DEF to reduce something? No effect. So 100.
Now, check if we can also buy HP? No need. So algorithm gives 100.
Now, test: HPY=1, ATKY=100, DEFY=0; HPM=100, ATKM=1, DEFM=0; prices: h=1, a=100, d=100. Here dmgM=100, T=1, dmgY = max(0,1-0)=1, need_HP=11+1=2, hp_buy=1, cost = a0 + d0 + h1 = 1. Could we also buy DEF to 1 to make dmgY=0? Then need_HP=1, hp_buy=0, but def cost d=100 => cost=100. So not better. So answer 1.
Now, test: HPY=100, ATKY=50, DEFY=0; HPM=100, ATKM=100, DEFM=0; prices: h=1, a=1, d=10. Options: Atk'=50, dmgM=50, T=2, dmgY=100, need_HP=201, hp_buy=101, cost=101. Atk'=100, dmgM=100, T=1, dmgY=100, need_HP=101, hp_buy=1, cost = atk 50 + hp 1 = 51. Atk'=100, def'=100, need_HP=1, cost= atk 50 + def 100*10=1000? Actually def cost: d=10 per point, from 0 to 100 => 1000, total 1050, worse. Atk'=100, def'=50: dmgY=50, need_HP=51, hp_buy=0? HPY=100, need_HP=51, so hp_buy=0, cost = atk 50 + def 500? Actually def from 0 to 50 cost 500, total 550, worse. Atk'=60: dmgM=60, T=2 (ceil(100/60)=2), dmgY=100, need_HP=201, hp_buy=101, cost = atk10 + hp101 = 111. So best is 51. Our loop includes atk'=100, def'=0, cost=51. So answer 51.
Now, test: HPY=50, ATKY=10, DEFY=0; HPM=100, ATKM=10, DEFM=0; prices: h=10, a=1, d=1. Options: atk'=10, dmgM=10, T=10, dmgY=10, need_HP=1010+1=101, hp_buy=51, cost = hp 5110 = 510. atk'=20, dmgM=20, T=5, dmgY=10, need_HP=510+1=51, hp_buy=1, cost = atk 10 + hp 10 = 20? Actually atk'=20 => atk_buy=10, cost=10, hp_buy=1 cost=10, total 20. atk'=100, dmgM=100, T=1, need_HP=11, hp_buy=0 (HPY=50), cost = atk 90 = 90. So best seems 20. Could we also buy DEF? def'=10 => dmgY=0, atk'=10, need_HP=1, hp_buy=0, cost = def 101 = 10, total 10. That's better! Indeed: increase DEF to 10 (cost 10) and keep ATK=10, then dmgY=0, need_HP=1, HPY=50 >0, so win. Cost 10. Our loop: atk' from 10 to max(10, 0+100=100) =100; def' from 0 to max(0,10)=10. So includes atk'=10, def'=10, cost = a0 + d10 + h*0 =10. So answer 10. Good.
Now, test extreme: HPM=1, DEFM=100, ATKY=1. Then atk_max = max(1, 100+1=101)=101. So we consider atk'=101? Actually atk_min=1. For atk'=101, dmgM=1, T=1, dmgY depends on def. So cost high. But maybe atk'=101 is not needed; atk'=101 is the smallest that gives dmgM=1? Actually to exceed DEFM=100, need atk'>=101. So atk_min=1, so we must go up to 101. That's correct. So our loop goes from 1 to 101, but we skip those <=100. So only atk'=101 considered. Good.
Now, note that atk_max = max(ATKY, DEFM+HPM) ensures we include the minimal atk needed for T=1, but also maybe some atk between ATKY and that. For DEFM=100, HPM=1, atk_max = max(ATKY,101). If ATKY=1, atk_max=101. That's correct, we include atk=101. But also we might consider atk=102,103,... up to 101? Actually atk_max=101, so only up to 101. But is it possible that atk=102 could be cheaper? No because atk=102 costs more than atk=101, and T=1 for both, dmgY same, so need_HP same. So 101 is cheaper. So fine.
Now, what about scenario where ATKY=150, DEFM=100, HPM=1: then atk_max = max(150,101)=150. So we consider atk=150 only. But could atk=151 be cheaper? It would be more expensive, so not needed. So fine.
Now, what about scenario where ATKY is less than DEFM+1 but also we might want to increase ATK beyond DEFM+HPM? Already covered.
Now, ensure that we are not missing any scenario where increasing ATK beyond DEFM+HPM could allow us to not need to buy HP if HPY is low? But T already 1, need_HP = dmgY+1. That doesn't change. So no.
Now, we also need to consider that buying ATK might affect dmgM and T, which could affect product TdmgY. But the product may not be monotonic if dmgY also changes with def. Since we also vary def, the product TdmgY can be minimized by combos. Our double loop covers combos of atk and def within bounds.
Now, let's double-check the bounds for def_max: max(DEFY, ATKM). But consider scenario where ATKM is 0? Input says ATKM between 1 and 100, so not 0. So fine.
Now, consider scenario where ATKM is 100, but DEFY is 0, so def_max=100. Good.
Now, one more nuance: The condition "Master Yang's HP > 0" at the same time monster HP <=0. Suppose dmgY = 0, then Yang never takes damage, so HP stays initial. So if initial HP > 0, condition holds regardless of T. So need_HP = 1 is fine. But what if initial HP is 1? Then need_HP = 1, ok.
Now, one more: Could we have situation where dmgY is positive but we could still win with HP = T*dmgY if monster dies before the end of the T-th second? But no, simultaneous.
Thus algorithm is solid.
Now, I'm thinking: Could there be a scenario where buying extra ATK reduces T but also increases dmgM? That's fine.
Now, test with random values using brute force exhaustive search over some larger ranges to confirm that our bounded loops are sufficient. Since we can't do it here, but we trust.
But I'll do a mental check: Suppose we set atk' = DEFM+HPM + 100. T = 1, need_HP = dmgY+1. Same as atk' = DEFM+HPM. But if we also have def' low, need_HP may be large. So the minimal cost would be at some atk' <= DEFM+HPM, because any atk' > that gives same or higher need_HP and higher ATK cost (since ATKY might be less). So the optimum cannot have atk' > atk_max.
But consider the case where ATKY is already > DEFM+HPM, but we might also need to increase DEF. Then atk' = ATKY is minimal. So atk' > ATKY not needed.
Thus bound is safe.
Now, what about def'? Could the optimal def' be > ATKM? If def' > ATKM, dmgY = 0, need_HP = 1, same as def' = ATKM. But cost higher. So not optimal.
Thus bound safe.
Now, one more nuance: The cost for hp_buy is h * (need_HP - HPY). But need_HP is computed as T * dmgY + 1. However, note that if dmgY = 0, need_HP = 1. But if HPY is 0 (not allowed), it's okay.
Now, consider potential overflow: T * dmgY could be up to 100 * 100 = 10000, fits int. But we should use long long for cost.
Now, let's implement.
Now, before finalizing, I'll test the algorithm on a few more cases.
Case: Input:
1 1 1
1 1 100
1 1 1
Here monster DEFM=100, ATKY=1, so need to buy ATK to at least 101. atk_max = max(1, 100+1=101)=101. So loop atk'=101, def' from 1 to max(1,1)=1? ATKM=1, def_max = max(1,1)=1. So atk'=101, def'=1: dmgM=1, T=1, dmgY = max(0,1-1)=0, need_HP=1, hp_buy=0 (HPY=1). cost = a*(100) + d0 + h0 = 100. So answer 100. Could there be cheaper? Maybe buy ATK=102? Not considered because atk_max=101, but cost would be 101, so not better. So answer 100.
But maybe we could buy DEF? No, need ATK. So fine.
Now, case:
10 10 10
10 10 10
1 1 1
Here initial: atk'=10, def'=10, dmgM=0? Actually DEFM=10, atk=10 => not >, so need atk>10. So atk_min=10, atk_max=max(10,10+10=20)=20. So we need atk>=11. Let's evaluate atk'=11, def'=10: dmgM=1, T=10, dmgY = max(0,10-10)=0, need_HP=1, hp_buy=0 (HPY=10). cost = a*1=1. So answer 1. Could we also win with atk'=11? Yes. So answer 1.
Now, if we didn't buy ATK, can't win. So answer 1.
Now, case where initial ATKY > DEFM but HP low:
HPY=5, ATKY=20, DEFY=0; HPM=100, ATKM=10, DEFM=0; prices: h=10, a=100, d=1.
Options: atk'=20, def'=0: dmgM=20, T=5, dmgY=10, need_HP=510+1=51, hp_buy=46, cost=4610=460. atk'=20, def'=10: def'=10 cost d=10, dmgY=0, need_HP=1, cost=10. That's cheaper. atk'=20, def'=5: dmgY=5, need_HP=5*5+1=26, hp_buy=21, cost=5 + 210 =215, worse. So best is def'=10 cost 10. Our loops: atk' from 20 to atk_max = max(20,0+100=100)=100, but for atk'=20, def' from 0 to ATKM=10. So includes def'=10, cost=10. So answer 10.
Now, what about atk'=21, def'=10: cost = a1 + d10 =100+10=110, worse.
Thus algorithm finds min.
Now, consider case where ATKY > DEFM, but not enough to win because HP low, and DEF also low. Could there be scenario where it's cheaper to increase ATK a lot to reduce T, and then HP needed is less, and that combination is cheaper than buying DEF? Our loops cover that.
Now, I think it's good.
Now, but wait: we set atk_max = max(ATKY, DEFM + HPM). However, what if DEFM + HPM is less than ATKY? Then atk_max = ATKY. That means we consider only atk' = ATKY. But is it possible that ATKY is not sufficient to kill because T is high and we need HP, but increasing ATK further (even though ATKY > DEFM+HPM is already > DEFM+HPM) yields T=1, so maybe we could avoid buying HP? But if ATKY > DEFM+HPM, then T=1 already. So no further reduction. So no.
But what if ATKY > DEFM but not > DEFM+HPM, so T>1, and ATKY could be larger than DEFM+HPM? Actually if ATKY > DEFM but not > DEFM+HPM, then ATKY <= DEFM+HPM. So our bound includes up to DEFM+HPM. So we consider all atk' from ATKY to DEFM+HPM. That's correct.
Now, what if ATKY > DEFM+HPM? Then T=1. So need_HP = dmgY+1. So if dmgY > 0, we might need HP. To avoid buying HP, we could increase DEF. But that's separate.
Thus bound fine.
Now, we must also consider the scenario where we might need to buy HP, but the cost of HP is so high that it might be cheaper to buy ATK to reduce T, even if ATKY > DEFM+HPM? Not possible.
Now, test scenario: HPY=1, ATKY=200, DEFY=0; HPM=100, ATKM=200, DEFM=0. Here ATKY=200 > DEFM+HPM=100, T=1, dmgY=200, need_HP=201, hp_buy=200, cost = h200. If h=1000, that's 200k. But maybe we can buy DEF to 200, cost d 200, if d is cheap, say d=1, then cost=200, which is better. Our loop: atk_max = max(200,100) =200, so atk'=200 only. def_max = max(0,200)=200. So we will try atk'=200, def'=200: cost = a0 + d200 + h*0 = 200. That's optimal. So good.
Now, what about buying ATK to 300? Not considered because atk_max=200. But could buying ATK to 300 reduce need_HP? T already 1, so no. So not needed.
Now, think about scenario where ATKY is exactly DEFM+HPM. Then T=1. So atk' = ATKY yields T=1. But we might consider atk' = ATKY+1? That would also T=1, but cost higher. So not beneficial.
Thus bound fine.
Now, let's also think about scenario where we might want to increase ATK beyond DEFM+HPM because we also need to reduce dmgY? ATK doesn't affect dmgY, so no.
Now, consider scenario where buying ATK reduces T, but also maybe we could buy less DEF? That's captured.
Now, I think the algorithm is correct.
But hold on: The problem statement says: "All numbers in input are integer and lie between 1 and 100 inclusively." So initial stats are between 1 and 100. Prices also 1..100. So our bounds are safe.
Now, we should also consider if we need to buy HP, but maybe we can also buy extra ATK or DEF that also change need_HP. Our loop covers.
Now, let's verify with a tricky case where need_HP formula might be off due to simultaneous damage and integer rounding when monster HP is not multiple. Example: HPM=10, dmgM=4 => T = ceil(10/4) = 3 (since 24=8, 34=12). After 3 seconds, monster HP = -2. Yang HP reduces by 3dmgY. Need > 3dmgY. That's correct.
Now, check if there is any scenario where monster dies exactly at the end of a second, but Yang dies at same second if HP equals exactly T*dmgY. So > needed.
Thus correct.
Now, think about scenario where dmgM is 0: skip.
Now, also think about scenario where dmgY is 0, but we also have atk' > DEFM, so win. need_HP=1. So done.
Now, potential optimization: none needed.
Now, we can code.
But wait, there's a nuance: We are assuming that after purchasing, Yang's stats are exactly as we set. But we might also consider that we can buy fractional attributes? No, integer.
Thus final.
Now, before final answer, let's run through the sample:
But let's also consider if there is any possibility that we need to buy HP even if dmgY=0? No, need_HP=1, HPY>=1, so no.
Now, edge: HPY=0? Not allowed.
Thus final answer.
Now, I'll implement in C++.
However, a nuance: In the loops, we set atk_max = max(ATKY, DEFM + HPM). But what if ATKY is less than DEFM+1? Then atk_max = DEFM+HPM. This includes all atk values from ATKY up to DEFM+HPM. But also we might need to consider atk values beyond DEFM+HPM if they could somehow reduce hp_buy because T is 1 but maybe dmgY decreases? dmgY depends on def, not atk. So no. So we are good.
Now, we could also compute the needed HP more precisely: need_HP = T * dmgY + 1. But is there a scenario where we might need to add 1 only if dmgY > 0? Actually if dmgY = 0, need_HP = 1, but if HPY=0? not allowed. So fine.
Now, I'll write the code.
But before finalizing, I'll think about possible alternative interpretations: Could it be that the battle happens in continuous time? No, it says every second.
Now, what about the order: "both decreases happen simultaneously". So as we did.
Now, is there any possibility that if both die at same time, Yang loses, as we assumed. So condition strict >.
Now, we good.
Now, final code.
Now, test some random cases with a script? Not possible now.
We need atk >100 => atk>=101. atk_max = max(1,100+100=200)=200. So atk from 1 to 200, but we skip <=100. So atk'=101 to 200.
def_max = max(1,100)=100.
We need to find min cost. Let's compute for atk'=101, def'=1: dmgM=1, T=100, dmgY=99, need_HP=10099+1=9901, hp_buy=9900, cost= atk_buy 100 + def_buy 0 + hp_buy 9900 = 10000? Actually atk'=101 => atk_buy=100, cost 100; hp_buy=9900, cost 9900; total 10000. For atk'=101, def'=100: dmgY=0, need_HP=1, hp_buy=0, cost = atk_buy 100 + def_buy 99 = 199. That's better. For atk'=200, def'=100: atk_buy=199, def_buy=99, cost=298, worse. For atk'=101, def'=50: dmgY=50, need_HP=10050+1=5001, hp_buy=5000, cost=100 + 49 + 5000 = 5149, worse. So min is 199. Could we do atk'=150, def'=100: atk_buy=149, def_buy=99, cost=248, worse. So answer 199.
Now, is there any cheaper? Possibly atk'=101, def'=101? But def'=101 > ATKM, dmgY=0, same as def'=100, but cost more. So no.
Thus answer 199.
Now, check if any scenario where atk'=101, def'=100 is valid: atk'=101 > DEFM=100, yes.
Now, our loop includes def' up to def_max = max(DEFY, ATKM)=max(1,100)=100, so def'=100 included.
Thus algorithm yields 199.
Now, let's compute by brute force: atk' from 101 to 200, def' from 1 to 100. That's 100*100=10k combos, okay.
Now, ensure that atk'=101 is within atk_max=200, yes.
Now, all good.
Now, consider a scenario where ATKY is already > DEFM, but DEFY is also > ATKM? Then dmgY=0, need_HP=1, and atk' > DEFM so condition satisfied. So cost 0. Our loop: atk_min=ATKY, atk_max=ATKY (since ATKY > DEFM+HPM? Not necessarily, but even if atk_max larger, we will still have atk'=ATKY considered, and def'=DEFY considered, cost 0, ans 0. But we might also consider atk' higher, but those cost >0, so min remains 0. So answer 0.
Now, confirm that when atk_max = max(ATKY, DEFM+HPM) and ATKY <= DEFM+HPM, we might loop over atk' up to DEFM+HPM. If ATKY already > DEFM, then some atk' > ATKY might be considered, but they won't beat 0 if initial already wins. But if initial doesn't win (e.g., HP insufficient), then we need to consider higher atk' maybe cheaper than buying HP. So that's fine.
Now, what about scenario where ATKY > DEFM, but T may be >1 and dmgY>0, HP insufficient. Could we consider atk' lower than ATKY? No, because we can't reduce ATK. So we only consider atk' >= ATKY. So if ATKY is high, we can't go down; but maybe increasing ATK further could reduce T and reduce needed HP, which might be cheaper than buying HP. Our loop includes atk' >= ATKY, so we consider higher atk' as well.
Now, what about scenario where ATKY is high but we might want to also increase DEF to reduce dmgY. That's considered.
Thus all good.
Now, code now.
But before final, I want to verify if the condition need_HP = T * dmgY + 1 might be too strict? Let's simulate a scenario with dmgY > 0 and need_HP = TdmgY exactly might be sufficient if the monster dies earlier due to overkill? For example, suppose HPM=10, dmgM=6, T=2 (since 26=12). After first second: monster HP=4, Yang HP reduced by dmgY. After second second: monster HP = 4-6 = -2, Yang HP = initial - 2dmgY. At the moment after second second, monster HP <=0, Yang HP = initial - 2dmgY. If initial = 2*dmgY, then Yang HP = 0 => lose. So need >.
But what if dmgM is such that monster HP becomes exactly 0 at the end of first second? For example, HPM=10, dmgM=10, T=1. Then after first second, monster HP=0, Yang HP = initial - dmgY. If initial = dmgY, Yang HP=0, lose. So need >.
Thus strict > needed.
Now, is there any scenario where the monster dies before the end of a second due to simultaneous? No, discrete.
Thus correct.
Now, could there be scenario where the monster's HP becomes <=0 during a second but before the second ends? Problem says "every second the monster's HP decrease by ... while Yang's HP decreases by ... Both decreases happen simultaneously." That suggests they happen at the end of the second, not gradually. So no.
Thus algorithm is correct.
Now, implement.
But wait: Are we allowed to purchase zero or positive integer amounts? Yes.
Now, one more nuance: The cost for buying attributes: "h bitcoins per HP, a bitcoins per ATK, and d bitcoins per DEF." So each point costs that many bitcoins.
Now, final answer.
Now, I'll produce final code with comments.
Edge Cases to test:
Already winning: output 0.
Need only HP: Example1.
Need only ATK: if def is high enough and HP enough.
Need only DEF: as in earlier examples.
Need combination.
Now, implement.
Potential pitfalls: Use long long for need_HP to avoid overflow? T * dmgY max 100100=10000, fine as int. But cost may be up to maybe 200100+200100+10000100? Actually hp_buy max maybe around 10000, h up to 100, so cost up to 1e6, fits 32-bit signed int, but safer to use long long.
Now, we can write.
Now, one more thought: Could we further prune the loops? Not needed.
Now, finalize.
But I'll do a final mental check on boundary: Suppose HPM=100, DEFM=100, ATKM=100, HPY=1, ATKY=1, DEFY=1, h=1,a=1,d=1. We did similar and got answer 199. But let's recalc using our loop: atk_max = max(1,100+100=200)=200; def_max = max(1,100)=100. So loop from atk=1 to 200, skip atk<=100, so from 101 to 200. For each atk, def from 1 to 100. We need to compute min. The best should be atk=101, def=100: atk_buy=100, def_buy=99, cost=199. Is there any atk=101, def=101? Not allowed because def_max=100. Could atk=200, def=100 give cost 199? atk_buy=199, def_buy=99, cost=298. So 199 is min. Could there be a combo with atk=150, def=90? dmgY=10, T for atk=150: dmgM=50, T=2 (ceil(100/50)=2), need_HP=2*10+1=21, hp_buy=20, cost = atk_buy=149 + def_buy=89 + hp_buy=20 = 258, not better. So 199 is min.
Now, what about atk=101, def=99? dmgY=1, T=1? Actually dmgM=1, T=100, need_HP=100*1+1=101, hp_buy=100, cost = atk_buy 100 + def_buy 98 + hp_buy 100 = 298. Not better.
So answer 199.
Now, check if we could buy ATK=101 and def=100 but also maybe reduce HP? Already zero. So fine.
Now, if h is very high and d is low, buying def is best. If d is high and h low, buying HP might be best. Our algorithm covers.
Now, final answer.
Now, code.
But wait, let's double-check the bound for atk_max: In the case HPM=1, DEFM=100, ATKY=1, then atk_max = max(1, 100+1=101)=101. That's fine.
Now, think about scenario where ATKY is 200, HPM=100, DEFM=100, then atk_max = max(200,200)=200. That's fine.
Now, think about scenario where ATKY is 150, HPM=100, DEFM=100, then atk_max = max(150,200)=200. So we consider atk' from 150 to 200. But could we also consider atk' lower than 150? Not allowed because we can't reduce ATK. So our loop starts at 150. But what if ATKY is 150, but the optimal solution might require atk'=151? That's included. However, could the optimal solution require atk'=200? Possibly, but we include up to 200. So okay.
But consider: ATKY=150, DEFM=100, HPM=100. Then atk_min=150, atk_max=200. Suppose we need to also buy DEF to reduce dmgY. The best might be atk'=150, def'=100. But also maybe atk'=200, def'=100 would be more expensive. So min will be found.
Now, is there any scenario where we would want to buy ATK beyond 200? Atk'=201 gives dmgM=101, T=1 (since 101>100). That's T=1. But atk'=200 also gives dmgM=100, T=1? Actually 200-100=100, which equals HPM=100, so T=ceil(100/100)=1. So both give T=1. So atk'=200 and atk'=201 both give T=1. So atk'=200 is cheaper than 201 (since ATKY=150, buying 50 points vs 51). So no need beyond 200. So bound safe.
Now, what if HPM=100, DEFM=99, then dmgM needed for T=1 is atk' >= 199. Actually DEFM+HPM = 99+100=199. So atk_max = max(150,199)=199. So we consider up to 199. Atk'=199 gives T=1? dmgM=100, ceil(100/100)=1. So yes. Atk'=200 would also give T=1 but cost more. So bound safe.
Now, what about HPM=100, DEFM=100, then atk' >=200 for T=1. So bound 200. So all good.
Now, def_max = max(DEFY, ATKM). If ATKM=100, def_max=100 if DEFY<=100. If DEFY=120, def_max=120? But ATKM max 100, DEFY could be up to 100? Actually initial DEFY <=100, so DEFY cannot be >100 because input range is 1..100. So DEFY <=100, ATKM <=100, so def_max <= max(100,100)=100. So def_max is at most 100. So loop small.
Now, atk_max is at most max(100, 100+100)=200. So loops are fine.
Thus algorithm is efficient.
Now, implement.
Now, before final answer, check if there is any oversight: Could it be optimal to buy negative HP? No.
Now, what about the possibility that we might need to buy HP but also increase DEF or ATK to reduce needed HP, and we might be able to reduce HP purchase to less than need_HP - HPY? Actually need_HP is the minimal HP required for given atk and def. So if we choose those stats, we must have at least that HP. So we cannot reduce HP purchase further. So our cost calculation is exact for that combo.
Now, is there any scenario where we would want to buy more HP than needed? That would increase cost, so not optimal.
Thus our approach yields optimal.
Now, final code.
But one more thing: In the loop, we compute need_HP = T * dmgY + 1. However, if dmgY=0, need_HP=1. But what if HPY=0? Not allowed. So fine.
Now, I'm comfortable.
Now, let's think if there is any alternative approach: Could we have solved by enumerating hp_buy as well? Not needed.
Now, I'll provide the final answer in code.
Now, I'll produce a final answer with explanation.
But as ChatGPT, I need to output only the code? The user asked: "A monster is attacking the Cyberland! ... Now Master Yang wants to know the minimum number of bitcoins he can spend in order to win." They provided examples. They expect a solution.
I should output the code in the response? Usually the answer should contain the solution (code) for the problem. So I'll output a C++ program.
Now, I'll write it.
But also ensure that we include necessary headers.
Now, final code:
#include
#include
using namespace std;
int main() {
int HPY, ATKY, DEFY;
int HPM, ATKM, DEFM;
int h, a, d;
cin >> HPY >> ATKY >> DEFY;
cin >> HPM >> ATKM >> DEFM;
cin >> h >> a >> d;
code
int atk_min = ATKY;
int atk_max = max(ATKY, DEFM + HPM);
int def_min = DEFY;
int def_max = max(DEFY, ATKM);
long long ans = 1e18;
for (int atk = atk_min; atk <= atk_max; ++atk) {
if (atk <= DEFM) continue; // cannot damage monster
int dmgM = atk - DEFM;
int T = (HPM + dmgM - 1) / dmgM;
for (int def = def_min; def <= def_max; ++def) {
int dmgY = max(0, ATKM - def);
long long needHP = (long long)T * dmgY + 1;
long long hp_buy = needHP - HPY;
if (hp_buy < 0) hp_buy = 0;
long long cost = (long long)a * (atk - ATKY) + (long long)d * (def - DEFY) + (long long)h * hp_buy;
if (cost < ans) ans = cost;
}
}
cout << ans << endl;
return 0;
}
Now, I'll double-check that atk_max and def_max are correct for all cases. Since input numbers up to 100, atk_max <= 200, def_max <= 100. So loops are fine.
Now, final answer.
But consider scenario where ATKY is less than DEFM+1, but ATKY could be, say, 50, DEFM=90, HPM=100: atk_max = max(50, 90+100=190)=190. So loop includes many values. That's okay.
Now, one more nuance: Could there be scenario where the optimal solution involves not buying any ATK (i.e., atk' = ATKY) but ATKY is less than or equal to DEFM, making it impossible to win? Then such atk' would be skipped because atk' <= DEFM. So we would not consider that invalid atk'. That means if ATKY <= DEFM, we cannot win without buying ATK, so we must consider atk' > DEFM. Since our loop includes atk' up to atk_max, and atk_min = ATKY, it will include the needed values (like DEFM+1). So fine.
Now, confirm that if ATKY > DEFM, atk' = ATKY may be valid, and we consider it.
Now, edge: ATKY > DEFM but ATKY is huge, say 500? But input says <=100. So not an issue.
Thus final.
Now, I'll output the code. ```cpp
#include
#include
using namespace std;
int main() {
int HPY, ATKY, DEFY;
int HPM, ATKM, DEFM;
int h, a, d;
cin >> HPY >> ATKY >> DEFY;
cin >> HPM >> ATKM >> DEFM;
cin >> h >> a >> d;
code
int atk_min = ATKY;
int atk_max = max(ATKY, DEFM + HPM);
int def_min = DEFY;
int def_max = max(DEFY, ATKM);
long long ans = 1e18;
for (int atk = atk_min; atk <= atk_max; ++atk) {
if (atk <= DEFM) continue; // cannot damage monster
int dmgM = atk - DEFM;
int T = (HPM + dmgM - 1) / dmgM; // rounds needed
for (int def = def_min; def <= def_max; ++def) {
int dmgY = max(0, ATKM - def);
long long needHP = (long long)T * dmgY + 1;
long long hp_buy = needHP - HPY;
if (hp_buy < 0) hp_buy = 0;
long long cost = (long long)a * (atk - ATKY)
+ (long long)d * (def - DEFY)
+ (long long)h * hp_buy;
if (cost < ans) ans = cost;
}
}
cout << ans << endl;
return 0;