We are given a problem:
Andrew wakes up at time hh:mm (24-hour format). The cat's current hunger level is H points. Each minute without food increases hunger by D points. So if Andrew waits t minutes from wake-up time before buying buns, the hunger becomes H' = H + t * D.
He can visit the store at any time (instant travel). At the store, he can buy buns. Each bun costs C roubles and decreases hunger by N points. However, buns cannot be fractional; they are integer. He must buy enough buns so that after feeding, the hunger <= 0. So if hunger is H' (integer? H, D, N, C are integers; t is integer minutes? Actually time is minutes; we can consider t as integer minutes because hunger increases by D per minute. But we can consider continuous time? Probably t is integer number of minutes? The problem: "each minute without food increases his hunger by D points". So if he goes at time = wake-up + t minutes, where t is non-negative integer (maybe real? but minutes are discrete). Usually such problems treat t as integer minute count. However, the discount starts at 20:00. That time might be at a fractional minute? But hh:mm are integers, so 20:00 is exactly. So we can assume t is integer minutes from wake-up to some time of day, which is also integer minutes.
Thus we need to compute the minimum cost over possible purchase times (including immediate and any later time, possibly after 20:00). Because buns are whole, and cost may be discounted after 20:00 (20% off, i.e., price becomes 0.8C). Note: The discount is for buns starting from 20:00. So if Andrew goes at or after 20:00, the buns cost 0.8C each. If he goes before 20:00, the price is C each.
But careful: If he goes exactly at 20:00, discount applies.
Goal: min money to spend.
Let current time be T0 = hh*60 + mm minutes since midnight.
Define T = time in minutes (integer) when he buys buns. T >= T0. Then hunger at T: H_T = H + (T - T0) * D.
Number of buns needed: ceil(H_T / N). Because each bun reduces by N, need total reduction >= H_T, integer buns.
If T < 20*60 = 1200, price per bun = C; else price per bun = C * 0.8.
Total cost = ceil(H_T / N) * price_per_bun.
We need to find minimum cost over T integer >= T0, possibly up to some limit? Since hunger increases linearly, and after discount price reduces, there may be optimal waiting until discount time if that reduces cost enough. However, waiting also increases number of buns needed. So we need to compare.
Additionally, it might be optimal to wait beyond 20:00, not just exactly at 20:00, because hunger continues to increase, but price remains discounted. The cost after discount is: cost = ceil((H + (T - T0)D) / N) * (0.8C). This cost is non-decreasing in T because the ceil argument increases, so cost can increase (but stepwise). However, the factor 0.8 is constant. So for T >= 1200, cost is monotonic non-decreasing in T. So the minimum among times at or after 20:00 is at the earliest such time: T = max(T0, 1200). Actually if T0 < 1200, the earliest discounted time is 1200. If T0 >= 1200, then he is already at or after discount, and immediate purchase gives discount. So the optimal discounted cost is at T = max(T0, 1200). But careful: If T0 < 1200, waiting until exactly 20:00 yields cost with hunger = H + (1200 - T0)*D, price 0.8C. That might be better than buying earlier at full price. However, waiting a bit longer than 20:00? Could that be lower? The cost after discount is (0.8C) * ceil((H + (T - T0)*D)/N). As T increases, the argument inside ceil increases, so the cost can only stay same or increase. It cannot decrease because ceil is monotonic non-decreasing and multiplied by positive constant. So the minimum for T >= 1200 is at the smallest T >= 1200, i.e., T = max(T0, 1200). But if T0 is exactly 1200? Then immediate purchase with discount is the same. If T0 > 1200, then immediate purchase already discount. So candidate times: immediate (i.e., T = T0) and possibly waiting until 20:00 if T0 < 1200. Also could consider waiting a little before 20:00? That gives full price, and hunger is lower than at 20:00, but no discount. The cost before discount is C * ceil((H + (T - T0)*D)/N) for T in [T0, 1199] (if T0 <= 1199). This cost is also monotonic non-decreasing in T? Because hunger increases, so ceil increases. So the cost before discount is also non-decreasing with T. So the minimum cost among T < 1200 is at the earliest time: T = T0 (i.e., buy immediately). Because if you wait any minute before discount, hunger increases, so cost will be equal or higher. So the only two candidates we need to consider are:
-
Buy immediately at T0 (full price if T0 < 1200, discount if T0 >= 1200). Actually if T0 >= 1200, immediate purchase already discounted; that's the only candidate for discounted scenario (since waiting further only increases cost). But still could consider immediate vs later? But later would be higher. So the minimal cost for T0 >= 1200 is immediate.
-
If T0 < 1200, then candidate: immediate purchase (full price) and waiting until exactly 20:00 (discount) - because any other time before or after cannot be better than these.
But is it possible that waiting a few minutes after 20:00 yields a lower cost? No because cost after discount is monotonic non-decreasing from T=1200 onward. So T=1200 gives the minimum among discounted times.
But wait: There is an edge case: The number of buns needed is ceil(H_T / N). This is piecewise constant. Could it be that waiting a little longer after 20:00 results in the same ceil? Possibly if the increase in hunger doesn't cross the threshold to require an extra bun, the cost remains the same as at 20:00. But that doesn't give a lower cost; it's equal. So we don't need to consider further times for minimal. So the only candidate that might be better than immediate purchase is T = 1200 if T0 < 1200.
Thus the problem reduces to computing cost_immediate = ceil(H / N) * C if T0 < 1200, else cost_immediate = ceil(H / N) * (0.8*C). And if T0 < 1200, compute cost_discount = ceil( (H + (1200 - T0)D ) / N ) * (0.8C). Then answer = min(cost_immediate, cost_discount).
But is that always? Let's double-check. Could there be a scenario where waiting a few minutes before 20:00 but after T0 yields a lower cost than immediate? Since cost before discount increases with hunger, immediate is minimal. So no.
But could there be a scenario where it's beneficial to wait until after 20:00 but not exactly at 20:00 because the ceil might jump at 20:00? Actually if you buy at 20:00, you get discount. If you buy at 19:59, you pay full price. So obviously discount is better if the number of buns needed is the same. But could there be a scenario where buying at 19:59 with full price is cheaper than buying at 20:00 with discount? Let's think: At 20:00 hunger is larger by D, so number of buns might increase by 1, making cost at 20:00 perhaps higher. However, discount factor 0.8 might compensate. But we should also consider the possibility that buying at some time after 20:00 but before the next bun threshold could yield the same number of buns as at 20:00, but price same discount, so cost same. So no better.
But is it possible that buying at a time before 20:00 but after T0 yields a lower cost than both immediate and 20:00? Not likely because immediate is minimal among pre-discount. So only immediate and 20:00 are candidates.
But what if the discount starts exactly at 20:00, but if we wait a little after 20:00, the hunger increased, but maybe the number of buns needed is lower because we can purchase a non-integer number? No, buns are whole, so cannot.
So the problem appears simple: compute the two costs, take min, output with three decimal digits? Actually the output specification says "Output the minimum amount of money to within three decimal digits." The examples show four decimal digits (25200.0000). But typical output requirement is to print with sufficient precision, e.g., using double. They check error 1e-4. So we can output with, say, 6 decimal places, or just print the computed number (maybe as a double). But we need to ensure rounding? We can compute the exact rational value? The cost might be integer multiples of C or 0.8C, which might be rational if C not divisible by 5? Actually 0.8C = C * 4/5, which could be non-integer if C is not a multiple of 5. But we output as floating. So we should compute as double.
But careful: Ceil operation leads to integer number of buns. So we can compute:
num_immediate = ceil(1.0 * H / N)
cost_immediate = num_immediate * (T0 < 1200 ? C : C * 0.8)
If T0 < 1200:
wait_minutes = 1200 - T0
hunger_at_20 = H + wait_minutes * D
num_discount = ceil(1.0 * hunger_at_20 / N)
cost_discount = num_discount * C * 0.8
Answer = min(cost_immediate, cost_discount) if T0 < 1200 else cost_immediate.
But wait: Are we allowed to purchase buns at any time, and the discount applies to all buns purchased after 20:00? The problem says: "Since the demand for bakery drops heavily in the evening, there is a special 20% discount for buns starting from 20:00 (note that the cost might become rational)." That implies that if the purchase time is >=20:00, the buns are at discounted price. So yes.
But could Andrew also buy buns at multiple times? The problem says: "At any time Andrew can visit the store where tasty buns are sold (you can assume that it doesn't take time to get to the store and back). One such bun costs C roubles and decreases hunger by N points." It doesn't say he can only go once. But typically, you would want to minimize total cost; if you could split purchases, you might buy some now and some later. However, the cat's hunger increases over time; if you feed the cat some buns, the hunger reduces. If you feed partially, the cat might still be hungry, and hunger continues to increase. But the objective is to get hunger <= 0. Could splitting purchases lead to lower cost? Possibly, because you could buy some buns before discount to reduce hunger, then later after discount buy remaining cheaper buns. But is that allowed? The problem doesn't explicitly restrict to a single purchase. It says: "At any time Andrew can visit the store ... Determine the minimum amount of money Andrew has to spend in order to feed his cat." This suggests he can visit multiple times. So we need to consider strategies involving multiple purchases, perhaps buying some buns before discount and some after discount.
Wait, the problem statement from typical competitive programming problem? I recall a Codeforces problem "Buying a TV Set" or something? Actually there is Codeforces problem 907A? Not sure. But I remember a problem about buying buns for a cat with discount after 20:00. I think it's Codeforces Round 450? Possibly problem "Feed the cat" or something. Let me recall: There is Codeforces problem 934A? Not sure. Actually, I recall a problem: "Andrew and Taxi"? No.
Better think: The problem as described seems to be from Codeforces: "A. New Year and the Treasure Geolocation"? No.
Maybe it's from Codeforces Round 441? Or "B. Shopping" something. I'll search memory: "After waking up at hh:mm, Andrew realised that he had forgotten to feed his only cat for yet another time (guess why there's only one cat)." That's a known Codeforces problem: "A. Feed the cat" from Codeforces Round #441 (Div. 2?) Actually I recall a problem: "A. Buying A House" is different.
Let me think: I've seen a problem with cat, hunger, buns, discount at 20:00. Yes, it's Codeforces problem 922A? Not sure. Quick memory: There is problem "A. Chewbaсca and Number". Not that.
Actually, I've definitely seen a problem: "A. Borya's Diagnosis"? No.
Maybe it's from Codeforces 2017-2018. Let's not rely on memory; we need to solve correctly.
Given the typical structure, many participants would consider only two options: buy now or wait until discount. But is that truly optimal? Suppose you can buy some buns now at full price, feed the cat, reducing hunger, then later after discount, buy additional buns if needed. Because hunger increases over time, but after feeding, the hunger becomes negative? Actually, the cat's hunger level is a single number; feeding decreases it. The goal is to make hunger <= 0. If you feed some buns, hunger reduces. But if you feed only some, it may still be positive. Then over time, it increases again by D per minute. So you might want to feed enough to bring it to zero exactly, or to a negative number? But you can overfeed. The cat's hunger cannot go below zero? Actually, it can become negative if you feed more than needed, but it's still fed (hunger <= 0). Overfeeding might be wasteful but maybe beneficial if it allows you to take advantage of discount for part of buns? For example, you could buy a few buns now at full price to reduce hunger so that later, after discount, you need fewer buns, overall cost might be lower than either pure immediate or pure wait.
But can you feed the cat multiple times? The problem statement: "At any time Andrew can visit the store where tasty buns are sold (you can assume that it doesn't take time to get to the store and back). One such bun costs C roubles and decreases hunger by N points." It doesn't say he can only feed the cat once. He can presumably feed the cat whenever he buys buns. So yes, multiple visits allowed.
But is there any reason to split purchases? Possibly to take advantage of the discount for part of the buns. Consider: If immediate purchase requires many buns full price, but waiting until discount requires even more buns but at cheaper price. Might be cheaper to buy some buns now to reduce the hunger growth, then buy the rest after discount. For instance, you could buy enough buns now to bring hunger to some level, then after 20:00, buy the remaining buns at discount. Because the part bought after discount is cheaper. However, buying buns now also costs full price. The growth of hunger before 20:00 can be mitigated if you reduce hunger early. But if you wait until 20:00 without feeding, hunger increases by D per minute. If you feed early, you reduce hunger, so the hunger at 20:00 is lower, thus fewer discounted buns needed. The trade-off: full price buns now vs discounted buns later.
Thus the problem becomes an optimization over two-stage feeding: choose number of buns bought before discount (at price C) and after discount (at price 0.8*C). However, note that the time of the first purchase could be at any time before 20:00, and the second purchase could be at or after 20:00. But we can consider that if we are going to buy some buns before discount, the optimal time to buy them would be immediately (at T0). Why? Because if you delay the pre-discount purchase, hunger grows, making it less effective: you would need to buy more buns later (or at the pre-discount time) to achieve the same final state. Since pre-discount price is constant, and hunger increases with time, you want to feed as early as possible to minimize growth. So if you intend to buy some buns before discount, you should do it at T0. Similarly, if you then buy additional buns after discount, you should do it at the earliest discount time (20:00) because waiting further only increases hunger (if you haven't fed after discount, hunger would continue to increase, requiring more buns). But note that after feeding at 20:00, the hunger becomes non-positive? Actually if you feed at 20:00 enough to bring hunger <= 0, you're done. But you could also feed some at 20:00 and maybe more later? That would be unnecessary because you could just feed all needed at 20:00. So if you plan to buy after discount, you would buy at 20:00 all remaining needed to satisfy hunger at that time.
But what if you feed at T0, then hunger becomes something, then it grows until 20:00, and then you feed the rest. That is plausible.
So we can consider: Let x be number of buns purchased at T0 (full price). Then after feeding, hunger becomes H0 = H - xN (could be negative). But if it becomes negative, that's okay; but we may have overfed. However, if H0 <= 0, then cat is fed, no further purchases needed. So x could be enough to finish immediately. In that case cost = xC.
If H0 > 0, then hunger will increase from T0 to 20:00: total minutes = M = max(0, 1200 - T0). Actually if T0 >= 1200, discount already active, so we don't have two-stage; we just buy all at discount. So consider only T0 < 1200. Then after M minutes, hunger becomes H1 = H0 + MD = H - xN + M*D.
At time 20:00, we can buy additional buns at discounted price to reduce hunger to <= 0. Let y = ceil(H1 / N) if H1 > 0? Actually if H1 <= 0, then we are done after waiting; but note if H0 > 0, H1 will be > H0, so likely positive. But if H0 is positive but small, and MD maybe zero? M>0 if T0<1200. So H1 is positive. But could H0 be <=0? If x is large enough, then cat fed immediately; that case we already have cost = xC. So for two-stage, we assume H0 > 0. Then y = ceil(H1 / N). Total cost = xC + y(0.8*C).
Goal: minimize over integer x >= 0. Also we could choose to buy all at T0 (x = ceil(H/N), y=0) or all at 20:00 (x=0, y = ceil((H+M*D)/N)). Also could choose x such that after feeding at T0, hunger becomes exactly some level that maybe after growth, H1 is an exact multiple of N, minimizing ceil? But since ceil, we might overshoot. However, we could also overfeed at 20:00 to reduce number of buns? Actually y = ceil(H1/N). If we feed extra at 20:00, that would be extra cost, not beneficial. So we would only buy exactly y to make hunger <= 0. So H1/N may be fractional, leading to some waste. But we could also adjust x to possibly reduce total cost.
Thus the problem is a mixed integer optimization: minimize Cx + 0.8C* ceil( (H - Nx + MD) / N ) over integer x >=0, subject to H - Nx may be negative (but then the argument of ceil becomes negative? Actually if H - Nx < 0, then after feeding, hunger is negative, cat fed. But then we don't need to buy any discounted buns. In that case, the cost is just Cx. However, if H - Nx is negative, the expression for y would be ceil(negative/N) which is <=0; we can set y=0. So we should treat separately: if H - Nx <= 0, then total cost = Cx. Otherwise, cost = Cx + 0.8C * ceil( (H - Nx + MD) / N ).
Now note that M = 1200 - T0 if T0 < 1200, else no discount? But we are considering T0 < 1200 case. So we need to compute this.
Now is this two-stage strategy possibly better than the two pure strategies? Possibly yes. Let's test with some numbers.
Suppose H=100, D=1, C=10, N=20. T0 = 19:00 (i.e., 1140 minutes). M = 60 minutes. Then pure immediate: ceil(100/20)=5 buns, cost=50. Pure wait: hunger at 20:00 = 100+60=160, buns=ceil(160/20)=8, cost=88=64? Actually discounted price = 100.8=8, so 88=64. So immediate is better. But two-stage: we could buy some buns at T0, then rest at discount. Let x be number of buns at full price. Then H1 = 100 -20x +60. Need y = ceil((160 -20x)/20) = ceil(8 - x) if 160-20x positive? Actually (160-20x)/20 = 8 - x. So y = max(0, 8 - x) because if 8-x <=0 then y=0. But careful: ceil(8 - x) = 8 - x if 8-x integer? Since 8-x is integer. So y = max(0, 8 - x). Cost = 10x + 8y = 10x + 8*(8-x) = 10x +64 -8x = 64 +2x for x <=8. For x=0, cost=64; x=5, cost=64+10=74? Wait, plug x=5: y=3, cost=105+83=50+24=74. So minimum among integer x from 0 to 8? At x=0, 64; x=1, 66; x=2, 68; x=3,70; x=4,72; x=5,74; x=6,76; x=7,78; x=8, 10*8=80 (y=0). Actually x=8 gives cost 80. So the minimum is 64 at x=0 (pure wait) or 50 at pure immediate? But x=5 gave 74, so not better. So pure immediate better. So two-stage not better in this case.
But consider scenario where discount factor is big and D is large, making wait expensive due to many extra buns, but mixing might lower total cost. Example: H=100, D=10, N=20, C=10, T0=19:00 (M=60). Pure immediate: 510=50. Pure wait: hunger=100+600=700, buns=ceil(700/20)=35, cost=358=280. So immediate wins. Not better.
Consider case where full price is expensive, discount is good, but waiting increases hunger a lot; maybe buying some now reduces the number needed later, but those now are expensive. Could the mix be better than both? Let's try H=100, D=1, N=30, C=100, M=600 (i.e., T0=10:00? Actually M large). So pure immediate: ceil(100/30)=4 buns? Actually 100/30=3.333, ceil=4, cost=400. Pure wait: hunger=100+600=700, buns=ceil(700/30)=24? 700/30=23.33 -> 24, cost=24*80=1920. So immediate better. Not mixing.
We need scenario where immediate cost is high because many buns needed, and discount reduces price per bun significantly, but waiting also increases buns a lot; maybe by buying a few full price buns early, we can reduce the number of discounted buns needed enough to offset the high price. For instance, suppose N is small relative to H, so many buns needed. C moderate. Discount factor 0.8. M moderate.
Let's design: H=1000, D=10, N=1, C=10. Then each bun reduces hunger by 1. So buns needed equals hunger exactly (since integer, hunger integer). Pure immediate: 1000 buns 10 = 10000. Pure wait: M minutes? Let's set T0=19:00, M=60: hunger=1000+600=1600, cost=16008=12800. So immediate better. Wait, we want discount to be attractive but waiting increases buns a lot; but immediate is still cheaper? Actually with N=1, buns = hunger. Cost immediate = CH = 101000=10000. Discount cost = 0.8C(H+ MD) = 81600=12800 >10000. So not good.
But maybe if C is larger and M is small? Actually discount is 20% off, so price per bun becomes 0.8C. The ratio of discounted to full price is 0.8. So waiting reduces price per bun by 20%. However, waiting increases number of buns by factor (H+MD)/H. If that factor is less than 1/0.8 = 1.25, then waiting is beneficial; otherwise, immediate is better. Specifically, cost_wait = 0.8C * ceil((H+MD)/N) ≈ 0.8C * (H+MD)/N (if we ignore ceil). Cost_now = C * ceil(H/N) ≈ CH/N. So waiting cheaper if 0.8*(H+MD) < H => 0.8H + 0.8 M D < H => 0.8 M D < 0.2 H => M D < 0.25 H. So if MD < H/4, waiting may be cheaper (ignoring ceil). So if M*D is relatively small compared to H, waiting could be beneficial. In such case, immediate might be more expensive because full price, but waiting only adds a few buns. So pure wait may be optimal.
Now consider mixing: we buy x buns now at price C. Then after waiting, we need y = ceil((H - Nx + MD)/N). Cost = Cx + 0.8Cy. We want to see if there exists x>0 such that this cost is less than both Cceil(H/N) and 0.8Cceil((H+MD)/N). Since C>0, we can divide by C: f(x) = x + 0.8 * y, where y = ceil((H - Nx + MD)/N) if H - Nx + MD > 0, else y=0. And we compare to f_imm = ceil(H/N) and f_wait = 0.8 * ceil((H+MD)/N). Since all terms are multiples of 0.2? Not necessarily.
Now, is there any scenario where mixing beats both? This resembles a knapsack/linear programming with two prices. Since the per-bun price after discount is cheaper, we want to shift as many buns as possible to after discount, but the catch is that buns bought after discount have to cover the extra hunger accumulated. However, buns bought before discount reduce the base hunger and thus also reduce the number needed after discount, but at a higher price. So it's like we have two "sources": full-price buns that can be bought at time 0, and discount buns bought after waiting. The total buns needed to cover initial hunger H and the accumulated hunger M*D, minus any overfeeding.
More precisely, we need to cover total hunger that must be neutralized: initial H plus the growth over M minutes (MD). The growth is inevitable because we cannot stop time. However, if we feed some buns at time 0, that reduces the hunger at time 0, but the growth still occurs from the remaining hunger? Actually careful: If we feed at time 0, we reduce the hunger immediately. Then the hunger will increase from that reduced level over the M minutes. So the total hunger that needs to be neutralized by the sum of buns from both stages is: The buns at time 0 reduce H by Nx. Then the buns at time M (20:00) must reduce the resulting hunger after growth: (H - Nx) + MD. So total reduction provided by all buns = Nx + Ny >= H + MD? Actually if we exactly meet requirement, Nx + Ny = H + MD + epsilon, where epsilon is overshoot (non-negative). But the condition is after final feeding, hunger <=0. So the total reduction needed is at least H + MD (the hunger that would exist at time M if no feeding at time 0, but if we feed at time 0, the hunger at time M is (H - Nx) + MD, so we need Ny >= (H - Nx) + MD). So total reduction Nx + Ny >= H + MD. So indeed, the sum of bun effects must cover H + MD.
Thus the total number of buns needed (if we could buy fractional) would be at least (H + MD)/N. Since buns are integral, total buns = ceil((H + MD)/N) if we can allocate them arbitrarily between times? But note that if we buy some buns at time 0, we might end up with more total buns than the ceil of total required because of the timing constraints? Actually, suppose total needed is T = ceil((H + MD)/N). Could we always achieve feeding with exactly T buns? Possibly yes, by choosing x and y such that x+y = T and NT >= H + MD, and also ensure that after feeding x at time 0, the remaining hunger at time M is <= Ny. That imposes: (H - Nx) + MD <= Ny. Since y = T - x, this becomes H - Nx + MD <= N(T - x) => H + MD <= NT, which holds by definition of T (since NT >= H+MD). So any split with x+y=T works as long as we also ensure that at time 0, we don't overfeed excessively? Actually if x is too large, H - Nx might be negative, then the hunger after feeding is negative, cat is fed immediately, and we don't need to buy y buns. In that case, total buns = x, which could be > T? Possibly if we overfeed, we might use more buns than the minimal total needed. But we wouldn't do that because it increases cost. So we would aim to use exactly T buns (or maybe T is not achievable because we need integer x, y? But we can always set x = something, y = T - x). However, there is an additional constraint: The buns at time 0 are bought at full price, those at time M at discount. So we want to minimize Cx + 0.8Cy = C*(x + 0.8y) = C(0.8T + 0.2x) because y = T - x => x + 0.8*(T - x) = x + 0.8T - 0.8x = 0.8T + 0.2x. So the cost is linear in x: cost = C*(0.8T + 0.2x). Since coefficient of x is positive (0.2C), we want x as small as possible. The smallest possible x is such that we can still feed the cat with total T buns split as x and y. However, we need to check feasibility: With x buns at time 0 and y = T - x at time M, is the cat fed? Condition: after feeding at time 0, hunger becomes H' = H - Nx. This could be negative (fed), then we don't need y. But if H' <= 0, we actually don't need to buy y buns; we can just stop, and total buns = x, which might be less than T. In that case cost = Cx. So we need to consider that if x is large enough to make H' <= 0, then we don't purchase discount buns, and total cost is Cx, which could be less than C(0.8T + 0.2x) because we saved buying y buns. But note if H' <= 0, we would not buy y, so the split analysis with y = T - x is invalid because we wouldn't buy those extra buns. So we need to handle the case x >= ceil(H/N) separately.
Therefore, the optimization can be thought as: we want to minimize over x (integer >=0) of:
if x >= ceil(H/N): cost = Cx (since immediate feed). Actually careful: If x >= ceil(H/N), then after feeding at time 0, hunger <=0, cat fed, so no need to wait or buy more. So cost = Cx. However, the minimal x that achieves this is ceil(H/N). So any x > ceil(H/N) is wasteful; the minimum among those is at x = ceil(H/N). So the candidate of buying all at time 0 is essentially x = ceil(H/N). So we don't need to consider larger x.
If x < ceil(H/N): then H' > 0. Then we will need to buy additional buns at discount. However, the total number of buns needed at discount is not necessarily T - x? Because T = ceil((H + MD)/N) is the total buns needed if we buy none at time 0. But if we buy x at time 0, the total required to cover H + MD may still be T? Actually, the necessary condition: We need Nx + Ny >= H + MD. Since y must be integer >=0, the minimal y is ceil((H + MD - Nx)/N) if that quantity positive. Let y_min(x) = max(0, ceil((H + MD - Nx)/N)). However, note that if H + MD - Nx <= 0, then y_min=0, but that would mean x >= ceil((H+MD)/N), which is larger than ceil(H/N) generally. So for x < ceil(H/N), it's likely that H + MD - Nx > 0, so y_min = ceil((H + MD - Nx)/N). Then total buns = x + y_min(x). This total may be greater than T? Let's check: Since T = ceil((H+MD)/N). For given x, we have H+MD - Nx <= Nceil((H+MD)/N) - Nx = N*(T - x) if NT >= H+MD. However, ceil((H+MD - Nx)/N) might be equal to T - x or T - x + 1? Because (H+MD) may not be a multiple of N. Let's define A = H+MD. Then T = ceil(A/N). For any integer x, let r = A mod N (with 0 <= r < N). Then A = qN + r, with q = floor(A/N), r != 0 => T = q+1 if r>0 else q. Then A - Nx = (q - x)N + r. Then ceil((A - Nx)/N) = q - x + ceil(r/N) = q - x + 1 if r>0 else q - x. So y_min = max(0, q - x + (r>0 ? 1 : 0)). That is exactly T - x if r=0? Actually if r>0, T = q+1, so T - x = q+1 - x, while y_min = q - x + 1 = T - x. If r=0, T = q, y_min = q - x = T - x, as long as q - x >=0. So indeed, for x <= q (when r=0) or x <= q (when r>0? Actually if r>0, q = floor(A/N). For x <= q, A - Nx >= Nq + r - Nx = N(q-x) + r > 0? If x = q, then A - Nq = r >0, so y_min = 1 = T - q because T = q+1, so T - x = 1. So works. If x = q+1 (and r>0), then A - N(q+1) = r - N < 0, so y_min = 0, but then x > q, so total buns = x, which might be > T? T = q+1, x = q+1, total = q+1, equal to T. But x = q+1 >= T. So for x >= T, y_min=0, and total buns = x (which is >=T). So for x < T, y_min = T - x exactly? Let's check: If x < T, then A - Nx > 0? Since T = ceil(A/N). For x < T, we have Nx < NT, and NT >= A, but could Nx be > A? Possibly if A < NT, which is always true unless A is multiple of N. For x = T-1: if T = q+1 (r>0), then x = q, A - Nq = r >0, y_min=1 = T - q = 1. So yes. If T = q (r=0), then x = q-1, A - N(q-1) = N + 0? Actually A = qN, so A - N*(q-1) = N, y_min = ceil(N/N) = 1 = T - (q-1) = 1. So yes. So for x < T, y_min = T - x. For x = T, if A is multiple of N, then A - NT = 0, y_min = 0 (since ceil(0)=0). That's consistent with T - x =0. If A not multiple, then x = T = q+1, A - N(q+1) = r - N < 0, but ceil of negative? Actually we define y_min only if >0, else 0. But for x = T, A - NT = N(q+1) = Nq + N > A because N > r. So A - ND? But wait, if we feed x >= T at time 0, we may be overfeeding. But T is based on total hunger H+Mx. To cover H (initial) only, we need at least ceil(H/N). Since T may be larger than ceil(H/N) (because MD. Since each bun gives N, total buns must be at least ceil((H+M*D)/N) = T. So T is a lower bound on total buns if we use any strategy that involves feeding after time 0 (i.e., not finishing at time 0). If we finish at time 0, total buns is ceil(H/N), which may be less than T. So the minimal total buns overall is min(ceil(H/N), T). But the cost depends on the distribution.
Now cost for a split strategy with x < ceil(H/N) (so we must buy discount buns) is:
cost = Cx + 0.8Cy, where y = T - x (since we argued that minimal y = T - x for x < T, provided that H - Nx > 0? Actually we need to verify: For x such that H - Nx > 0 (i.e., x < ceil(H/N)), but also x may be less than T. Since ceil(H/N) could be less than T or greater than T? Let's compare ceil(H/N) and T. T = ceil((H+MD)/N) >= ceil(H/N) because adding MD increases the argument. So T >= ceil(H/N). So if x < ceil(H/N), then certainly x < T (since T >= ceil(H/N) > x). So indeed for x in [0, ceil(H/N)-1], we have x < T. Also H - Nx > 0. Then we need y = ceil((H - Nx + MD)/N). As argued, that equals T - x because A = H+MD, and A - Nx >0, so ceil((A - Nx)/N) = T - x (since T = ceil(A/N)). Let's prove: Since A = Nfloor(A/N) + r, 0 <= r < N, T = floor(A/N) + (r>0?1:0). Then A - Nx = N(floor(A/N)-x) + r. Ceil((A - N*x)/N) = floor(A/N) - x + (r>0?1:0) = T - x. So indeed y = T - x. Good.
Thus for x in [0, ceil(H/N)-1], cost = Cx + 0.8C*(T - x) = C*(0.8T + 0.2x). This is increasing in x (since coefficient 0.2 positive). So to minimize cost among split strategies, we want the smallest possible x, i.e., x = 0. But x=0 corresponds to pure wait (all at discount). However, is x=0 allowed? Yes, if x=0, then we buy no buns at time 0, wait, then buy T buns at discount. That yields cost = 0.8CT. But is there any constraint that x must be integer and also maybe we need H - N*x > 0? For x=0, H >0 (since H>=1), so condition holds. So indeed the cheapest split (with feeding both times) is x=0. So mixed strategies are never better than pure wait if we restrict to x < ceil(H/N). Because cost increases with x.
But wait: What about the possibility that x is such that after feeding at time 0, the hunger becomes exactly zero or negative before time M, i.e., x >= ceil(H/N)? That case is covered by immediate feeding (x = ceil(H/N) is enough). For x > ceil(H/N), cost increases linearly, so not optimal. So the only candidates are:
- x = ceil(H/N) (immediate feed) cost = C * ceil(H/N)
- x = 0 (wait until discount) cost = 0.8C * ceil((H+MD)/N)
Thus the earlier simple analysis stands: only two candidates.
But is there any scenario where buying some buns at time 0 and then buying fewer than T - x at discount because we overfeed at time 0 such that after growth, we still need fewer? No, because if x >= ceil(H/N), we are done; if x < ceil(H/N), we need to cover the remaining hunger and growth, which totals A - Nx, and the minimal y is exactly ceil((A - Nx)/N) = T - x, as derived. So any feasible strategy that doesn't finish at time 0 will have cost at least Cx + 0.8C*(T - x). Since this is linear increasing in x, the minimum is at x=0.
But could there be a strategy where we feed at time 0 and then also feed at some time before 20:00 (i.e., multiple purchases before discount)? For example, feed some at time 0, then feed more at time 19:30 (still full price), then at 20:00 discounted. Could that reduce cost? Since all purchases before discount have the same price C, they are equivalent to one purchase at time 0 (since feeding earlier reduces hunger sooner, thus reduces growth). Actually if you feed at time t1 > T0, you let hunger grow from T0 to t1, which might be detrimental. So the best among all pre-discount purchases is to do them as early as possible (i.e., at T0). So multiple purchases before discount are suboptimal compared to one purchase at T0 of the same total number of pre-discount buns.
What about purchasing some buns after discount but before final feeding? Could we purchase part of the discounted buns at 20:00 and then later more? That would be worse because waiting longer increases hunger. So we would do all discounted purchases at the earliest possible discount time.
Thus the only two candidate strategies are indeed the ones we considered.
But wait, there's a nuance: The discount starts from 20:00. If Andrew wakes up after 20:00, then immediate purchase already discount. If he wakes up before 20:00, immediate purchase is full price. But could it be optimal to wait a bit after 20:00? That would increase hunger and possibly increase number of buns, so not better.
Thus answer is as described.
But let's test with the given examples:
Example 1:
19:00 => T0 = 1960 = 1140.
H=255, D=1, C=100, N=1.
Immediate: ceil(255/1)=255 buns, cost=255100=25500.
Wait: M=1200-1140=60, hunger=255+60=315, buns=315, discount price=80, cost=315*80=25200.
min=25200. Matches.
Example 2:
17:41 => T0 = 1760+41 = 1020+? 1760=1020, +41=1061.
H=1000, D=6, C=15, N=11.
Immediate: ceil(1000/11) = ceil(90.909)=91 buns, cost=9115=1365.
Wait: M=1200-1061=139 minutes, hunger=1000+1396=1000+834=1834, ceil(1834/11)=? 11166=1826, 11167=1837, so ceil=167 buns, discount price=150.8=12, cost=16712=2004. So immediate cheaper => 1365. Matches.
So both examples align.
Now, what about a scenario where T0 is after 20:00? For instance, Input: 21 00, H=100, D=1, C=10, N=20. Then immediate discount: hunger 100, buns=5, cost=5*8=40. That's the answer.
But is there any scenario where it's beneficial to wait even if you are already after discount? Since cost increases with time, no.
Thus solution seems trivial: compute two costs, take min.
But before finalizing, we must confirm that there is no trick: Could there be a case where buying buns before discount and after discount but with x>0 could be cheaper than both pure strategies because of the ceil effect? Since cost for split is C*(0.8T + 0.2x). For x=0, cost=0.8CT. For x=ceil(H/N) (immediate), cost = Cceil(H/N). Compare these two. Could there be an x in between where the ceil of (A - Nx)/N is not exactly T - x because maybe A - Nx becomes negative? But if x < ceil(H/N) <= T, then A - Nx >0? Actually could it be that x is such that A - Nx <= 0 while x < ceil(H/N)? That would imply Nx >= A = H+MD, but since ceil(H/N) <= T, and T = ceil(A/N), x >= T would imply Nx >= NT >= A. But x < ceil(H/N) <= T, so x cannot be >= T unless ceil(H/N)=T and x can be equal to T? If ceil(H/N)=T, then the minimal x that makes A - Nx <= 0 is x = T (since NT >= A). But x < ceil(H/N) means x <= T-1, so A - N*x >0. So safe.
But what about the effect of ceil in the immediate cost: ceil(H/N) might be larger than H/N, and T might be ceil(A/N). The linear formula cost = Cx + 0.8Cceil((A - Nx)/N) assumed that ceil((A - Nx)/N) = T - x for x < T. But is that always true? Let's verify with specific numbers where ceil may behave differently due to the fact that A - Nx may be a multiple of N? The formula we derived seems solid: For any integer x, ceil((A - Nx)/N) = ceil(A/N) - x if A - Nx > 0? Let's test with A=10, N=3. Then ceil(A/N)=ceil(10/3)=4. For x=1: A - N*x=7, ceil(7/3)=3. But 4-1=3, works. x=2: A-6=4, ceil(4/3)=2, 4-2=2, works. x=3: A-9=1, ceil(1/3)=1, 4-3=1, works. For x=0: 4-0=4, ceil(10/3)=4, works. So seems correct.
Proof: Let T = ceil(A/N). Then (T-1)N < A <= TN. Subtract Nx: (T-1-x)N < A - Nx <= (T - x)N. Since x integer, if A - Nx > 0, then lower bound (T-1-x)N could be negative? But the inequality still holds. Then dividing by N: T-1-x < (A - Nx)/N <= T - x. Taking ceiling: ceil((A - Nx)/N) = T - x, because the number is greater than T-1-x and at most T-x, so its ceiling is the smallest integer greater than or equal to it, which is T - x if (A - Nx)/N is not an integer? But if it's exactly equal to T - x? That would require A - Nx = (T - x)N => A = TN. But if A = TN, then A is multiple of N, and T = A/N exactly. Then for x < T, A - Nx = N*(T - x) > 0, so (A - Nx)/N = T - x, an integer, so ceil = T - x. Works. So indeed the formula holds for any x with A - Nx > 0.
Thus the linear relationship holds.
Therefore, the cheapest split is x=0. So the only candidates are x=0 and x=ceil(H/N). So indeed the problem reduces to comparing two numbers.
But wait: There is a nuance: The cat's hunger is integer, D, N, H are integers. The number of minutes M = 1200 - T0 is integer if T0 integer (which it is). So A = H + M*D is integer. So ceil division works with integers.
But what about the possibility that we might want to feed some at time 0 and then also feed some at 20:00, but also we could feed some after 20:00 but before the cat's hunger becomes zero? That would be wasteful.
Thus solution is straightforward.
But is there any scenario where buying all at discount is not optimal because of the 20% discount making the price rational, but the total cost might be lower if we buy some now to reduce the number of buns after discount, because the per bun discount might not compensate for the extra buns? But as we saw, the cost function is linear increasing in x for split. So x=0 gives minimum among split. So no.
But consider the possibility that we can buy buns at time 0, but we could also wait beyond 20:00? Not beneficial.
Therefore answer: compute cost_now = ceil(H/N) * (T0 >= 1200 ? C*0.8 : C). If T0 < 1200: cost_wait = ceil((H + (1200 - T0)*D) / N) * (C * 0.8). Then answer = min(cost_now, cost_wait) if T0<1200 else cost_now.
But we must also consider the possibility that we could wait until exactly 20:00 even if T0 > 1200? That would be later, so not beneficial.
Also consider if T0 == 1200 exactly: then cost_now = ceil(H/N) * (0.8*C) because discount applies. That's same as cost_wait (since waiting zero minutes). So min same.
Edge cases: H, D, C, N up to 1e5 etc. Compute ceil with integer arithmetic: (a + b - 1) // b for positive a,b.
Now, about output precision: They want within 1e-4 absolute or relative error. Since we are dealing with rational numbers: C may be integer, and 0.8C may be rational (C * 4/5). Since C up to 100, product up to 1e21e5=1e7, within double precision fine. We can compute as double: cost = num_buns * price, where price is double (C or C*0.8). Or we can compute as rational and output as floating. Example outputs show four decimal places, but we can output with, e.g., std::fixed << std::setprecision(10) to be safe.
Alternatively, we can compute exact rational as fraction: price = C * 4 / 5 for discount, but then cost may be integer multiple of 0.2? Actually if C not multiple of 5, cost may be decimal. For example, C=7, discount price = 5.6. Number of buns integer, so cost = integer * 5.6. That's a decimal with one digit after decimal? Actually 5.6 = 28/5, so cost = (28/5)buns, which may be fraction with denominator 5. So we can compute exactly using integers: cost_numer = num_buns * (if discount: C4 else C*5? Actually C * 0.8 = C * 4 / 5. So cost = num_buns * C * (discount? 4 : 5) / (discount? 5 : 1). So we can compute as a rational and then output as double. Using double is safe.
Implementation details: Read hh, mm as ints. Compute cur = hh*60+mm. Read H, D, C, N as ints. Compute num_now = (H + N - 1) // N. If cur >= 1200: price = C * 0.8; cost_now = num_now * price; answer = cost_now. Else: cost_now = num_now * C; // full price. Then compute wait_minutes = 1200 - cur; hunger_wait = H + wait_minutes * D; num_wait = (hunger_wait + N - 1) // N; cost_wait = num_wait * C * 0.8; answer = min(cost_now, cost_wait). Print answer with appropriate precision.
But hold on: The problem statement says "the cost might become rational". That's due to discount. They want output with absolute or relative error 1e-4. So we can use double.
Now, before finalizing, double-check that there is no scenario where buying some buns at time 0 and some at discount could be beneficial because of the ceiling of H/N vs ceil((H+MD)/N) differences. But we already argued that the cost for split strategies is linear increasing in x, so best is x=0. However, we must be careful: The derivation that y = T - x holds for x < T and H - Nx > 0. But could there be a scenario where x is such that H - Nx <= 0 (i.e., immediate feed) but we still consider buying discount buns? No, because if H - Nx <= 0, the cat is fed immediately, so we wouldn't buy discount buns. That would just be waste. So not beneficial.
What about the scenario where we feed some at time 0, but not enough to fully feed, and then we feed some at a time before 20:00? That would be full price anyway, so equivalent to feeding more at time 0 (since earlier feeding reduces growth more). So no.
Thus solution stands.
But let me search memory: I recall a Codeforces problem "A. Feed the cat" maybe? I think the intended solution is exactly that. Many participants might think about mixed strategy, but the linear nature shows it's not needed.
Double-check with a custom case where ceil(H/N) is much larger than T? But T >= ceil(H/N) because adding MD >= 0. So T >= ceil(H/N). So immediate cost = C * ceil(H/N); wait cost = 0.8CT. So wait is cheaper if 0.8T < ceil(H/N). Since T >= ceil(H/N), this can happen only if 0.8T < ceil(H/N) <= T => requires 0.8T < ceil(H/N) => ceil(H/N) > 0.8T. For example, if ceil(H/N) = T, then 0.8T < T => true if T>0. So wait could be cheaper even if T = ceil(H/N) (i.e., when A is exactly multiple of N? Actually T = ceil(H/N) only if MD is zero? Because T = ceil((H+MD)/N) >= ceil(H/N). If M=0, i.e., T0 >=1200? Then discount applies immediate, and we already are in discount. If M>0, then T > ceil(H/N) generally, unless H/N is already fractional and adding MD pushes it just to the next integer without increasing ceil? Actually possible: Suppose H=9, N=10, ceil(H/N)=1. If MD=1, then A=10, T=1, same as ceil(H/N). So T = ceil(H/N) possible if (H+MD) exactly reaches a multiple of N that is the same as ceil(H/N)N? Example: H=9, N=10, ceil(H/N)=1 because 9/10 ceil=1. If MD=1, then A=10, T=1. So yes T can equal ceil(H/N). In such case, wait cost = 0.8C1 = 0.8C, immediate = 1C = C. So wait cheaper. That's correct.
But consider if ceil(H/N) is much smaller than T, wait cost may be higher. So min picks appropriate.
Now, what about the possibility that we could purchase buns at some time after 20:00 but before the cat's hunger becomes too high? That's same as wait cost at earliest discount.
Thus algorithm is O(1).
Now, I'll write solution accordingly.
But before final answer, let's double-check the problem statement: "At any time Andrew can visit the store where tasty buns are sold (you can assume that is doesn't take time to get to the store and back)." That suggests he can go multiple times, but we argued that doesn't help.
Let's try to construct a counterexample where mixed could be better. Suppose we have parameters such that the total buns needed T is much larger than ceil(H/N) (so immediate is cheaper than pure wait). But maybe by buying a few full-price buns early, we can reduce T to something like T' (which would be the new ceil after reduction) maybe more than T - x? Actually if we buy x buns early, the total buns needed becomes x + ceil((A - Nx)/N). As we argued, that's x + (T - x) = T, unless A - Nx <= 0. So total buns remains T. So the total number of buns needed is constant T regardless of x as long as x < T and x < ceil(H/N)? Actually check: For x < T, y = T - x, so total = T. So total buns constant. That's an important observation: The total number of buns required to feed the cat (if we don't finish immediately) is exactly T, independent of how many we buy before discount, as long as we don't finish early. Because the total hunger to neutralize is H + MD, and each bun gives N. So if we use any number x of buns before discount, the remaining hunger is A - Nx, requiring y = ceil((A - Nx)/N) buns. But since A may not be a multiple of N, the sum x+y may be either T or T+1? Let's test with A not multiple of N. Suppose A=10, N=3, T=4. For x=1, y=ceil(7/3)=3, total=4. x=2, y=ceil(4/3)=2, total=4. x=3, y=ceil(1/3)=1, total=4. For x=4, A - Nx = -2, negative, but we wouldn't buy y, total=4 (x=4) but that's > T? Actually x=4 is >= T? T=4, x=4, then we are done (maybe overfed). But if we forced to still buy some y? Not needed. So total buns could be x if x >= ceil(H/N) and possibly > T. So the minimal total buns to feed without overfeeding before discount is either ceil(H/N) (if feed immediately) or T (if we wait). So the total buns if we split is exactly T as long as x < ceil(H/N) (since then we must buy discount, and x+y = T). So indeed, any split uses exactly T buns. Therefore, cost = Cx + 0.8C*(T - x) = 0.8CT + 0.2Cx, which is increasing in x. So the cheapest split is x=0.
Thus no mixed beats pure wait.
But wait: What if x is such that H - Nx <= 0? That would mean we are done at time 0, and we wouldn't buy any discount buns, so total buns = x, which could be less than T. And cost = Cx. Since x could be ceil(H/N) which may be less than T. So immediate feeding could use fewer buns (since we don't need to counteract growth). So that is the alternative.
Thus the decision is between immediate (fewer buns at full price) and waiting (more buns but cheaper per bun). No mixed.
Thus solution is correct.
Now, implement.
But there's one more nuance: The discount is 20% off, i.e., price becomes 0.8*C. However, the problem says "the cost might become rational". That's fine.
Now, output: The examples show four decimal digits, but we can output with e.g., printf("%.10lf\n", answer) or similar.
Make sure to use double for multiplication to avoid integer overflow? Numbers: H up to 1e5, D up to 1e2, wait_minutes up to 1440? Actually maximum minutes from early time: if T0=0, wait_minutes=1200, so M up to 1200 (since 00:00 to 20:00 is 1200 minutes). Then MD <= 1200100 = 120,000. So A = H + MD <= 1e5+1.2e5 = 220,000. N up to 100, so num_buns up to ceil(220,000/1) = 220,000 if N=1. C up to 100, so cost up to 220,000100 = 22,000,000 for immediate; discounted cost up to 220,000*80=17,600,000. These fit in 32-bit int but multiplication of num_buns (up to 220k) and C (up to 100) yields up to 22 million, within int32. However, if we use integers for cost_now and cost_wait as integers when no discount (C integer), but discount introduces fraction. We can compute as double directly: double price = (cur >= 1200 ? C * 0.8 : C); double cost = num_buns * price. That's safe.
But careful: If we compute num_buns as integer, then multiply by double price. That's fine.
Alternatively, compute cost as long double for extra precision, but not needed.
Now, we must consider the possibility that T0 could be exactly 1200? Then cur >= 1200, so immediate discount. No need to compute wait.
Edge cases: If H=0? But constraints say H >= 1. So okay.
Now, final answer.
But wait, let's test a scenario where H is very large and D is large such that wait cost is huge, immediate is better. Works.
Now, some might argue about the possibility of buying buns at 20:00 but also feeding the cat before 20:00? But feeding before 20:00 would be at full price, so we already considered that.
Now, the problem statement says "you can assume that is doesn't take time to get to the store and back". So we don't account travel time.
Thus solution is correct.
Now, let's produce final answer.
But hold on: There is another nuance: The discount starts from 20:00. It says "starting from 20:00". Does that include 20:00? Typically yes, "starting from" includes the time. In the first sample, they visited at exactly 20:00 and got discount. So yes.
Now, check if there could be an advantage to buying at 20:00 and then feeding the cat, but we could also feed the cat before 20:00 with some buns? Already considered.
Thus implement.
Now, about output formatting: They show "25200.0000" in first sample, "1365.0000" in second. So we should output with four decimal places or more. We'll output with, say, 10 decimal places, or fixed 4. Since the tolerance is 1e-4, printing with 6 decimal places ensures. We'll use printf("%.10f", answer) or cout with fixed and setprecision(10). Or to match samples, we could output with exactly 4 decimal places; but it's fine as long as error within limit.
Better to use cout << fixed << setprecision(10) << answer << endl;.
Now, I'll write the solution in C++.
But before final, let's double-check the integer calculations: num_now = (H + N - 1) / N. But H is integer, N integer.
Similarly, hunger_wait = H + wait_minutes * D. wait_minutes = 1200 - cur. Ensure that wait_minutes is non-negative: if cur >= 1200, we don't compute wait; else it's positive.
num_wait = (hunger_wait + N - 1) / N.
Now, cost_now = num_now * (cur >= 1200 ? C * 0.8 : C);
cost_wait = num_wait * C * 0.8;
All good.
Now, maybe we should also consider the case where waiting until discount yields a lower cost, but could it be beneficial to wait even longer because the number of buns might not increase immediately? For example, if hunger increases but still within the same ceil threshold, cost remains the same. So cost at T=1200 is the same as T=1200+1 if the ceil doesn't increase. But that doesn't give lower cost than 1200; it's equal. So the minimum is still at 1200.
Thus done.
But hold on: The problem statement says "The cat's current hunger level is H points, moreover each minute without food increases his hunger by D points." It doesn't say that hunger stops increasing after feeding? Actually after feeding, the cat's hunger is reduced. If we feed at time 0, the hunger becomes H - N*x. Then it continues increasing at D per minute. So our modeling is correct.
Now, consider the possibility of feeding the cat multiple times after discount to reduce hunger gradually? No benefit.
Now, confirm with a test where H is 1, D large, N large, C moderate. For instance, H=1, D=100, N=100, C=10, T0=19:59 (cur=1199), M=1. Immediate: ceil(1/100)=1 bun, cost=10. Wait: hunger=1+100=101, ceil(101/100)=2 buns, discount price=8, cost=16. So immediate better.
Another test: H=100, D=1, N=100, C=10, T0=10:00 (cur=600), M=600. Immediate: ceil(100/100)=1 bun, cost=10. Wait: hunger=100+600=700, ceil(700/100)=7 buns, cost=7*8=56. Immediate better.
Test where waiting better: H=100, D=1, N=20, C=10, T0=19:00 (1140), M=60. Immediate: ceil(100/20)=5, cost=50. Wait: hunger=160, ceil=8, cost=64? Actually 88=64, so immediate better. Not better. Need MD small relative to H. For waiting to be better, we need 0.8*(H+MD)/N < H/N => 0.8(H+MD) < H => 0.8MD < 0.2H => MD < H/4. So choose H=1000, D=1, N=1, C=10, T0=19:30? Let's compute: T0=19:30=1170, M=30, M*D=30, H/4=250, so 30<250, so wait should be better? Let's compute: Immediate: buns=1000, cost=10000. Wait: hunger=1030, buns=1030, discount price=8, cost=8240. So yes wait better. And immediate is 10000, wait is 8240, so wait better. Our formula would pick wait.
Now, what about a mixed strategy? Suppose x=200 (buy 200 buns at time 0, cost 2000, hunger becomes 800, then wait M=30, hunger becomes 830, need 830 buns discounted, cost 830*8=6640, total 8640 >8240. So worse. x=500: cost 5000, hunger becomes 500, then 530 after wait, need 530 discounted -> 4240, total 9240. So worse. So pure wait best.
Now, consider scenario where H is such that ceil(H/N) is much less than T, but maybe immediate is cheaper. That's okay.
Thus answer stands.
Now, implement.
But before final, I'll consider potential pitfalls: The problem might have input where the discount is applied at 20:00, but the time format is 00 ≤ hh ≤ 23, 00 ≤ mm ≤ 59. So 20:00 is 1200. But what about 24:00? Not included. So fine.
Now, implement in code.
Pseudo:
int main() {
int hh, mm;
cin >> hh >> mm;
int H, D, C, N;
cin >> H >> D >> C >> N;
int cur = hh * 60 + mm;
int num_now = (H + N - 1) / N;
double cost_now;
if (cur >= 1200) {
cost_now = num_now * (C * 0.8);
} else {
cost_now = num_now * C;
}
double ans = cost_now;
if (cur < 1200) {
int wait = 1200 - cur;
long long hunger = H + 1LL * wait * D; // may be large up to 220k, fits in int but safe.
int num_wait = (hunger + N - 1) / N;
double cost_wait = num_wait * (C * 0.8);
if (cost_wait < ans) ans = cost_wait;
}
cout << fixed << setprecision(10) << ans << endl;
return 0;
}
But careful: C * 0.8 is double; but if we use integer multiplication then cast? In C++, C * 0.8 is double because 0.8 is double. So okay.
Alternatively, to avoid floating errors, we could compute cost_wait as (num_wait * C * 4) / 5.0. But double is fine.
Now, note: The answer must be within 1e-4. Double precision is sufficient.
Now, maybe there is a scenario where buying at 20:00 exactly yields discount but if you buy at 20:00 and maybe the cat's hunger is exactly H + M*D, but if you feed the cat before 20:00? Already considered.
Now, I think it's correct.
But wait, I recall a Codeforces problem where the discount starts at 20:00, and you could also feed the cat before 20:00 with some buns, and then after 20:00 with discounted buns. However, the official solution might be just comparing two options. I want to double-check by looking up memory: There is Codeforces problem 828A? No. Let's search mental: I recall a problem "B. Masha and geometric depression"? Not.
Actually, I think the problem is Codeforces Round 437 (Div. 2) problem A "Between the Offices"? Not.
Better recall: There is a problem titled "A. New Year and Hurry" but no.
Maybe it's "A. Buying A House". No.
The text "After waking up at hh:mm, Andrew realised that he had forgotten to feed his only cat" is from Codeforces problem 902A? Actually I've seen this: It's from Codeforces Round #441 (Div. 2), problem A. Let me check: I recall a problem "A. Trip For Meal" not.
Wait, maybe it's from Codeforces "A. Pizza, Pizza, Pizza!!!" no.
Let's search memory: There's a known problem "A. Cheap Travel" about buying tickets with monthly and single tickets. Not that.
Actually, the problem "A. Feed the Cat" appears in Codeforces Round 953? Not sure.
I'll quickly google in mind: The sample: first sample: 19 00, 255 1 100 1 output 25200. That's specific. I've definitely seen this before. I'm almost certain the official solution is exactly to compare two options.
Nevertheless, I will double-check the reasoning with a more complex scenario: Suppose H=10, N=9, D=10, C=10, T0=19:55 (cur=1195), M=5. Then:
Immediate: ceil(10/9)=2 buns, cost=20.
Wait: hunger=10+510=60, ceil(60/9)=7 (since 96=54, 97=63), cost=78=56. So immediate cheaper.
Now, consider mixed: x=1 (buy 1 bun at time 0, cost 10, hunger becomes 10-9=1, then after 5 min, hunger=1+50=51, need y=ceil(51/9)=6, cost=6*8=48, total=58. Worse than both. x=2: immediate already done, cost=20. So immediate best.
Now, consider H=9, N=10, D=1, C=10, T0=19:50 (cur=1190), M=10. Immediate: ceil(9/10)=1, cost=10. Wait: hunger=9+10=19, ceil(19/10)=2, cost=28=16. Wait cheaper. Mixed: x=1? But x=1 would be immediate feed (since 1 bun reduces by 10, hunger becomes -1, done) cost=10. So immediate cost 10, wait cost 16, immediate better? Wait: Wait cost 16 > 10, so immediate better. But earlier we thought wait might be better. Let's compute correctly: H=9, N=10, D=1, M=10, A=19, T=2. Wait cost=28=16. Immediate=110=10. So immediate better. According to inequality 0.8T < ceil(H/N)? T=2, ceil(H/N)=1, 0.8*2=1.6 >1, so immediate better. That's consistent. So wait is not always better when T > ceil(H/N) by a margin.
Now, consider case where T = ceil(H/N) = 1, wait cost = 0.8C, immediate = C, so wait better. That would happen if A is exactly multiple of N and equal to ceil(H/N)N? Example: H=9, N=10, ceil=1. Need MD such that A=10. So MD=1. So if M*D=1, wait cost better. So that's possible.
Now, we must ensure M is integer; M*D can be 1 if D=1 and M=1.
Now, is there any scenario where mixed beats both? Suppose T is larger than ceil(H/N), but the cost function for split is 0.8CT + 0.2Cx. Since x>=0, this is >= 0.8CT. So mixed cost is at least wait cost, with equality only at x=0. So mixed never beats wait. So no.
Thus solution stands.
Now, implement.
One more nuance: The discount is 20% off, meaning price = C * 0.8. But careful: The problem says "special 20% discount for buns starting from 20:00 (note that the cost might become rational)." So we apply discount to each bun. So our formula correct.
Now, we need to print answer with at least three decimal digits? The statement says "to within three decimal digits." Actually: "Output the minimum amount of money to within three decimal digits." That phrasing might mean output with three decimal places? But the examples show four decimal digits (e.g., 25200.0000). However, they might accept any format as long as error is within 1e-4. So we can output with, say, 10 decimal digits.
Thus code.
Now, I'll produce final answer.
But before final, let's think about possible integer overflow: wait_minutes up to 1200, D up to 100 => wait_minutes*D up to 120,000, fits 32-bit signed int. H up to 100,000, sum up to 220,000, fits. num_buns up to 220,000. Multiplying by C (100) gives 22 million, fits. But if we compute using long long for safety, it's fine.
Now, double-check the input reading: first line: two integers hh and mm. second line: four integers H, D, C, N.
Now, output: In the first sample, output 25200.0000. In second, 1365.0000.
Our output might be 25200.0000000000 if we use setprecision(10). That's fine.
Now, let's produce final solution.
But I recall a subtle point: The discount is 20% off, i.e., you pay 80% of original price. So price after discount = C * 0.8. But in some locales, they might consider that "cost might become rational", so we should use double.
Now, potential rounding: If we compute cost_wait = num_wait * (C * 0.8), that is double multiplication. Should be accurate enough.
Now, let's write final answer.
Now, I will craft the final response with explanation. ```cpp
#include
#include
#include
using namespace std;
int main() {
int hh, mm;
cin >> hh >> mm;
int H, D, C, N;
cin >> H >> D >> C >> N;
int cur = hh * 60 + mm; // current time in minutes since midnight
int num_now = (H + N - 1) / N; // buns needed immediately
double cost_now;
if (cur >= 1200) { // discount already active
cost_now = num_now * (C * 0.8);
} else {
cost_now = num_now * C; // full price
}
double ans = cost_now;
// if we are before 20:00, consider waiting until 20:00
if (cur < 1200) {
int wait = 1200 - cur;
long long hunger = H + 1LL * wait * D; // hunger at 20:00
int num_wait = (hunger + N - 1) / N; // buns needed then
double cost_wait = num_wait * (C * 0.8); // discounted price
ans = min(ans, cost_wait);
}
cout << fixed << setprecision(10) << ans << endl;
return 0;
}