We are to solve a problem about buying buckwheat flour from three shops A, B, C. Each shop sells bags with fixed amounts: A:200g, B:300g, C:500g. The nominal price per bag: A:380 yen, B:550 yen, C:850 yen. Discounts are given when buying multiple bags: Shop A gives 20% discount per 5 bags (i.e., for every group of 5 bags, they are discounted? Actually the description: "Discounted units: per 5 bags" and "Discount rate: reduced by 20%". Example: when you buy 12 bags at shop A, price is reduced by 20% for 10 bags (two groups of 5?), but not for other 2. So essentially, for every 5 bags you get 20% discount on those 5? Wait: The example says: "when you buy 12 bags of flour at shop A, the price is reduced by 20% for 10 bags, but not for other 2 bags." That implies that the discount applies to groups of 5 bags: if you have 12 bags, you have 2 full groups of 5 (10 bags) which get discount, and remaining 2 are not discounted. So yes, discount is applied per set of 5 bags: each such set gets 20% off the total for those 5? Actually they said "the price is reduced by 20 % for 10 bags" i.e., the unit price for those 10 bags is multiplied by 0.8. So effectively the discount is applied to each bag in the group? But careful: If you have exactly 5 bags, you'd get 20% off total price, i.e., 38050.8 = 1520? Without discount it's 1900, so discount saves 380 yen. For 10 bags, you get 20% off all 10 because it's two groups. So discount is applied per each group of 5 bags. So the pricing formula for shop A: total price = (number_of_bags // 5) * (53800.8) + (number_of_bags % 5) * 380.
Similarly shop B: discount 15% per 4 bags. So price = (n // 4) * (45500.85) + (n % 4) * 550.
Shop C: discount 12% per 3 bags. Price = (n // 3) * (38500.88) + (n % 3) * 850.
We can mix purchases from different shops? The problem says: "You can visit three shops, A, B and C." It does not say you must buy from only one shop. It says you went shopping to buy buckwheat flour. You can visit three shops. The table shows each shop's bag amounts and discounts. It doesn't explicitly say you can combine purchases from multiple shops, but typical knapsack-like problems allow mixing. However, the example: Input 500 -> output 850. That's just buying one bag from shop C (500g) at 850 yen. Could also be buying 5 bags from shop A (5*200=1000g) but we need exactly 500g, so not possible because you need exactly the same amount. So mixing might be needed for some amounts, like 2200. Let's check output for 2200 = 3390. We need to see if mixing yields that. Let's explore possible combinations.
We need to achieve exactly a grams, where a is multiple of 100, between 500 and 5000 inclusive. We can buy any non-negative integer numbers of bags from each shop. The total cost is sum of costs from each shop, where each shop's cost is computed with its own discount scheme based on number of bags bought from that shop.
Goal: minimize total cost.
This is a classic integer programming/knapsack with multiple items and group discounts. Since a <= 5000 (grams) and bag sizes up to 500g, max number of bags is at most 5000/200 = 25 if only A, 5000/300 ≈ 16.7 so up to 17, 5000/500=10. So total combinations are limited, but we can brute force over possible numbers of bags from each shop? Number of possibilities for A: from 0 to 25, B: 0 to 16, C:0 to 10. That's up to 261711 = 4862 combos per dataset, times 50 datasets = 243100, trivial. But we need to consider that we may also buy more than one shop. However, the discount structure is per shop, so we can precompute cost for buying k bags from each shop. Then the problem becomes: choose non-negative integers x_A, x_B, x_C such that 200 x_A + 300 x_B + 500 x_C = a, and minimize cost(x_A) + cost(x_B) + cost(x_C), where cost functions are piecewise linear with discount groups. We could precompute the minimal cost for each amount up to 5000 using DP (unbounded knapsack style) but with limited bag sizes and discount schemes, we could also generate all combinations of x_A, x_B, x_C that sum to that weight.
But wait: Since bag sizes are not too large, we can enumerate x_A, x_B, x_C in nested loops. However, a max of 5000 means x_A could be up to 25, x_B up to 16, x_C up to 10. So loops 261711 = 4862 combos per dataset, which is fine. But we need to compute cost for each shop separately using discount formula.
But we need to verify that there is no scenario where buying from multiple shops yields a lower cost than buying only from one? Typically, due to discounts, mixing may be beneficial. For a=2200, we need to check if output 3390 matches any combination.
Let's compute some costs:
Shop A: per bag 200g, price 380, discount per 5 bags: 20% off on groups of 5.
Cost function: cost_A(n) = (n // 5) * (53800.8) + (n % 5) * 380 = (n // 5) * 1520 + (n % 5)*380.
Because 5*380=1900, 20% off = 1520.
Shop B: per bag 300g, 550, discount per 4 bags: 15% off on groups of 4. 4550=2200, 15% off => 22000.85=1870. So cost_B(n) = (n // 4)*1870 + (n % 4)*550.
Shop C: per bag 500g, 850, discount per 3 bags: 12% off on groups of 3. 3850=2550, 12% off => 25500.88=2244. So cost_C(n) = (n // 3)*2244 + (n % 3)*850.
Now for a=2200.
We need to find combination (a,b,c) such that 200a+300b+500c=2200, minimize total cost.
Let's compute possible combos manually:
If we buy only from C: 500g per bag, need exactly 2200g? 2200/500 = 4.4, not integer. 4 bags = 2000g (200g short), 5 bags=2500g (300g excess). So pure C cannot exactly match.
Only B: 300g per bag, 2200/300 ≈ 7.333, not integer. 7 bags =2100g (100g short), 8 bags=2400g (200g excess). Not exact.
Only A: 200g per bag, 2200/200 = 11 exactly. So 11 bags from A: cost_A(11) = (11//5)1520 + (11%5)380 = 21520 + 1380 = 3040+380=3420? Wait: 2*1520=3040, plus 380=3420. But output is 3390, which is lower than 3420. So pure A is 3420, not minimal. So mixing yields lower.
Let's try mixing:
We need combinations that sum to 2200g.
Equation: 200x + 300y + 500z = 2200 => divide 100: 2x+3y+5z=22.
We need integer non-negative solutions.
We can search over z from 0 to 4 (since 5z ≤22 => z≤4). For each, solve for x,y.
z=0: 2x+3y=22. Solutions: y can be even? Solve: 2x = 22-3y => x = (22-3y)/2 must be integer ≥0. So 22-3y even => 3y even => y even. y=0 => x=11 (cost 3420). y=2 => 22-6=16 => x=8 (cost?). Compute cost_A(8)+cost_B(2). cost_A(8)= (8//5)1520 + 3380? Wait 8%5=3, so 11520 + 3380 =1520+1140=2660. cost_B(2)= 01870 +2550=1100. Total=3760 >3420.
y=4 => 22-12=10 => x=5 => cost_A(5)=11520=1520, cost_B(4)= (4//4)1870 =1870? Actually 4%4=0, so 11870=1870, total=3390? 1520+1870=3390. That's output! So combination: A:5 bags, B:4 bags, C:0 => weight=5200=1000, B:4*300=1200, total=2200. Cost=3390. So indeed mixing gives lower cost than pure A. So we must consider mixed purchases.
Thus the solution is to compute min cost over all non-negative integer triples (a,b,c) satisfying weight constraint.
Given max a = 5000, bag sizes: 200,300,500. Maximum numbers: a_max = 5000/200 = 25, b_max= floor(5000/300)=16, c_max=10. So triple loop 261711=4862 combos. That's trivial. But we must be careful: Input size max 50 datasets, so 50*5000 ~ 243,100 combos, more than fine.
However, we can perhaps reduce further but not needed.
But ensure that we consider all combos? Since max a=5000, but maybe combos that exceed 5000? We'll loop only up to those maxima and check equality.
Alternatively, we can use DP: for each possible total weight from 0 to max_a, compute minimal cost. But since a is divisible by 100, and bag sizes are multiples of 100, we could scale down by 100 to get integer units: bag sizes 2,3,5. Then total weight a' = a/100, ranging from 5 to 50. Then max numbers: A: up to 25, B: up to 16, C: up to 10. That's the same.
But note: the discounts are per number of bags, not per weight. So cost functions depend on counts.
We'll just implement triple nested loops for each dataset. But maybe we can precompute a table of minimal cost for each weight up to 50 (after scaling) by enumerating all possible combinations of counts. That would be even simpler: for each weight w (multiple of 100? Actually scaled weight: 1 unit = 100g), w = a/100, from 5 to 50. Precompute min cost for all w from 0 to 50. Then for each input, output mincost[w]. Since the constraints do not change, we can precompute once. That would be efficient.
But careful: the discount schemes are fixed. So we can compute min cost for each w (0..50). Since w up to 50, we can enumerate all possible numbers of bags: a from 0 to 25, b from 0 to 16, c from 0 to 10, compute total weight = 2a+3b+5c, and if ≤ 50, record cost. Then for each weight w, take min over all combos with exactly w. That's similar to DP but by enumeration. Since max combos 4862, and we need to test weights up to 50, we can just fill an array of size 51 with large numbers and update.
But we also need to consider that weight may exceed 50? max a is 5000, so w=50. So we need up to 50.
Implementation steps:
- Precompute cost_A[n] for n up to 25? Actually n can go up to 25 (since 25*200=5000). But when mixing, n could be up to 25 if others 0, but also could be less. We'll compute cost_A, cost_B, cost_C for n up to their maxima.
- Then triple loop: for a in 0..25, for b in 0..16, for c in 0..10:
weight = 2a + 3b + 5*c (since scaling factor 100g)
if weight <= 50:
total_cost = cost_A[a] + cost_B[b] + cost_C[c]
update min_cost[weight] = min(min_cost[weight], total_cost)
But we also must ensure that weight exactly matches a given input? We'll compute for all weights. But note: we only need to answer for exactly given a, but we can compute the whole table.
One nuance: Some weights may be unattainable? Since bag sizes are 2,3,5, we can achieve many numbers, but not all? For example weight 1? Not needed because a>=500 (w>=5). But within range 5-50, likely all reachable, but we can just set initial min_cost large.
Also, note that the discount scheme may cause cost_A[0] = 0, etc.
Now, is this method correct? It enumerates all combinations of counts, which covers all possible purchases. There's no constraint that we must buy from each shop at most some number. So it's exhaustive.
But we must verify that weight can exceed 50 in the enumeration? We're capping at 25,16,10 but note that 225=50, plus 316=48, plus 510=50, maximum weight is 50+48+50=148? Actually 225=50, 316=48, 510=50, sum = 148, which is >50. However we are only interested in weights up to 50, so we can skip if weight>50 to avoid storing large array. But for enumeration we will compute weight up to 148, but we can limit loops to values that yield weight <=50 to reduce computations? Not necessary. But we can also restrict loops to values that keep weight ≤50. But it's easier to just check if weight <=50 and update.
Since number of combos is small (261711=4862), it's fine.
After precomputing min_cost[0..50], for each input a (in grams) that is not zero, compute w = a/100, output min_cost[w].
But verify with example: w=5 (500g) => min_cost[5] should be 850. Let's compute possible combos: a=0,b=0,c=1 -> weight=5, cost=850. Any other combos? a=1? 2*1=2, need additional weight 3, maybe b=1? weight=5 => a=1,b=1 gives 2+3=5, cost = cost_A[1] + cost_B[1] = 380+550=930 >850. a=0,b=0,c=1 is best. So min=850.
For w=22 (2200g) => min should be 3390. Let's see combos: a=5,b=4,c=0 gives weight=25+34=10+12=22, cost=1520+1870=3390. a=11,b=0,c=0 gives 3420. a=1,b=0,c=4? 2+20=22? Actually c=4 gives 20, plus a=1 gives 2 =>22, cost: cost_A[1]=380, cost_C[4]=? cost_C[4] = (4//3)2244 + (1)850? 4//3=1, 4%3=1, so 2244+850=3094? Actually compute: 12244 + 1850 = 3094. total = 380+3094=3474 >3390. So min is 3390.
Thus works.
But careful: Are there any combos where buying more bags than needed, but getting discount, and then discarding? No, we must buy exactly the same amount, no extra, and we cannot waste. So exactly the weight.
Thus solution is straightforward.
But there is a nuance: The problem statement says "you can visit three shops, A, B and C." It might be interpreted as you can only go to one shop? But the example for 2200 yields 3390 which is achieved by buying 5 bags from A and 4 from B, so you visited both A and B. So clearly mixing is allowed.
Thus implement precomputation.
But also note that there might be discounts applied across shops? No, each shop independently.
Thus simple.
Let's think about correctness and edge cases.
Input constraints: a is divisible by 100, between 500 and 5000 inclusive. So w from 5 to 50 inclusive. There's also possibility of a=0 as termination.
Number of datasets up to 50.
Precomputation method:
Initialize min_cost array size 51 (index 0..50) with INF (some large number). Then triple nested loops for a, b, c up to max counts that could potentially contribute to weight <=50? Actually to be safe, we can set max a = 25, max b = 16, max c = 10. But as noted, combinations with weight >50 will be ignored. This yields correct min cost for each weight up to 50.
But is it possible that a combination with a > 25, b > 16, or c > 10 could still produce weight <=50? For a>25, 2a > 50 so weight >50. So a cannot exceed 25. For b>16, 3b > 48? Actually 316=48, 317=51 >50, so b can be at most 16 if other contributions zero; but if b=17 and a=0,c=0, weight=51>50, so we can skip. So maximum b that could possibly combine with negative a to bring weight down? No, a and c are non-negative, so adding more b can only increase weight. So for any combination with b>16, weight >=317=51 >50, regardless of a,c? Wait, if a negative? Not allowed. So b cannot exceed 16 if total weight ≤50. But could b=17 if a negative? Not allowed. So max b is 16 for weight <=50. Similarly, c max is 10 because 510=50, so c can be 10 only if a=b=0; with any additional a or b, weight would exceed. So c max is 10. Also a max is 25 (2*25=50). So our loops up to those maxima cover all combos that could yield weight ≤50.
Thus fine.
Implementation details:
- Precompute cost_A array for a=0..25 using formula.
- cost_B for b=0..16.
- cost_C for c=0..10.
Alternatively compute on the fly within loops.
But note: cost functions may produce non-integer? Discount rates are percentages, resulting in integer amounts? Let's check: 38050.8 = 1520 integer. 3805=1900, 20% off => 380 discount, integer. 4550=2200, 15% off => 22000.85 = 1870 (since 22000.85 = 1870, 22000.15=330, integer). 3850=2550, 12% off => 25500.88 = 2244 (25500.12=306, integer). So all costs are integer. So we can store ints.
Thus min_cost array will have ints.
Now, one might wonder: Could there be a scenario where buying more than needed weight and discarding some flour is cheaper? The problem says "you should buy the flour of exactly the same amount as the given input." So no, must be exact.
Thus our enumeration of exact weight is correct.
Now, can we be sure that the minimum cost for a given weight can always be achieved with bag counts within the loops? Since we have covered all possible bag counts that can yield that weight (because any solution's counts must satisfy the weight equation; if any count exceeds our loop max, then weight would exceed 50, but if weight is ≤50, the counts cannot exceed these maxima. Proof: Suppose we have a solution (a,b,c) with weight = 2a+3b+5c = w ≤ 50. Then a ≤ w/2 ≤ 25, so a ≤ 25. Similarly b ≤ w/3 ≤ 50/3 ≈16.66, so b ≤ 16 (since integer, b ≤ floor(50/3)=16). Similarly c ≤ floor(50/5)=10. So indeed any solution is within our loops. So enumeration is exhaustive.
Thus solution is correct.
We can implement.
Now, let's also think about if there might be multiple datasets, precomputation once.
Edge case: weight = 0? Not in input, but we can ignore.
Thus code:
Pseudo:
def cost_A(n):
groups = n // 5
remainder = n % 5
return groups * 1520 + remainder * 380
Similarly for B: groups = n // 4; return groups * 1870 + (n%4)*550
C: groups = n // 3; return groups * 2244 + (n%3)*850
Precompute min_cost array size 51 with INF=10**9
for a in range(0,26):
cost_a = cost_A(a)
for b in range(0,17):
cost_b = cost_B(b)
for c in range(0,11):
w = 2a + 3b + 5*c
if w <= 50:
total = cost_a + cost_b + cost_c
if total < min_cost[w]:
min_cost[w] = total
Then read lines, for each integer a != 0, compute w = a // 100, print min_cost[w].
But careful: a is given in grams, divisible by 100, so integer division by 100 gives w. E.g., 500 -> 5.
Now check example: input 500 -> w=5 -> output min_cost[5] = 850, correct. 2200 -> w=22 -> min_cost[22] = 3390.
Now let's test some other values manually to ensure no surprises.
Consider a=1000 (w=10). What's the best? Options: from C: 2500 = 1000g, cost_C(2)= 2850=1700 (no discount because need 3 for discount). From A: 5*200=1000, cost_A(5)=1520. So 1520 is cheaper. Also mixing? Maybe 1 B (300) + ? 300+?=1000 -> need 700, which could be 3.5 A not integer; maybe 1 C (500) + ? =500 left, could be 2.5 A not integer; maybe 2 B (600) + 2 A (400) =1000, cost: B2=1100, A2=760 total=1860 >1520. So best is A:5 bags =1520. So output 1520. Let's see if our enumeration yields that.
Check w=10. Our loops include a=5,b=0,c=0 -> cost=1520. Also a=0,b=0,c=2 -> cost=1700. a=2,b=2,c=0: weight=22+32=4+6=10 cost: A2=2380=760, B2=2550=1100 total=1860. So min is 1520.
Now a=1500 (w=15). Options: C: 3 bags = 1500g, cost_C(3)=2244 (discount). A: 7.5 bags not integer; B: 5 bags=1500g, cost_B(5)= 11870 + 1550? Actually 5 bags: groups = 1 (since 5//4=1) remainder 1 => 1870 + 550 = 2420. Mix: 1C(500)+?1000: could be 5A=1000 -> total 1C+5A =1500, cost: 5A=1520, 1C=850 total=2370. Or 3B=900 + 3A=600 =1500: A3=3380=1140, B3 = cost_B(3)= 01870+3550=1650 total=2790. Or 2C=1000 + ?500: maybe 2A+?100? Actually 2C=1000g, need 500g -> either 1C again (makes 3C) or 2.5A not integer, 1B+?200? 1B=300 -> need 200 = 1A -> weight=2C + 1B + 1A =1000+300+200=1500: cost: 2C=2850=1700, B1=550, A1=380 total=2630. So best seems C:3 bags =2244. However, maybe 5A + 1C = 1520+850=2370 >2244. So 2244 is best. But maybe 5A + 1B? 5A=1000, 1B=300, total 1300, not 1500. 5A+2B=1000+600=1600 >1500. So indeed 2244. Our enumeration will capture.
Now a=1800 (w=18). Options: A:9 bags =1800g, cost_A(9)= (9//5)1520 + (9%5)380 =11520 +4380=1520+1520=3040. B:6 bags =1800g, cost_B(6)= (6//4)1870 + (6%4)550 =11870+2550=1870+1100=2970. C: 3C=1500g +?300: maybe 1B? Actually 3C=1500, need 300 -> 1B => total=1800: cost=2244+550=2794. Or 2C=1000 + ?800: 2C+? => 800 could be 4A (800) -> total cost: 2C=1700, 4A=1520? Actually 4A=4380=1520, total=3220. Or 1C+?1300: 1C+? maybe 2B+?200: 2B=600, need 700=3.5A not integer. So min seems 2794? But check 3C+1B = 1500+300=1800 weight, cost=2244+550=2794. However also 4C would be 2000 >1800, not allowed. Also combination of A and B: 6A=1200, 2B=600 -> 1800: cost: A6= (6//5)1520 + 1380 =1520+380=1900? Wait 6//5=1, 6%5=1 => cost=1520 + 380=1900? Actually 1520+380=1900. B2=1100 total=3000. That's worse. 4A+2B: 4A=4380=1520, 2B=1100 total=2620? 1520+1100=2620, weight=4200+2300=800+600=1400 not 1800. Mist: 4A+2B =800+600=1400. Not 1800. Let's recalc: To get 1800 with A and B, solve 2a+3b=18. Solutions: b even? Let's list: b=0 => a=9 (cost A9=3040). b=2 => a=(18-6)/2=6 => cost A6=? A6= (6//5)1520 + (6%5)380 = 11520 + 1380=1900, B2=1100 total=3000. b=4 => a=(18-12)/2=3 => cost A3=1140, B4=1870 total=3010. b=6 => a=0 => cost B6=2970. So min among A/B mixes is 2970 from B6. C+B: 3C+1B=2244+550=2794. C+A: 3C+? 300? but 3C=1500, need 300 -> either 1.5A not integer, 1B works. So 2794 is lower than 2970. So best so far 2794. Check C only: 1800 not multiple of 500. Check A only: 3040. Check B only: 2970. So 2794 is candidate. But maybe mixing A and C: 2C=1000 + 4A=800 =>1800, cost: 2C=1700, 4A=1520 total=3220. 1C=500 +?1300: 1300/200=6.5, not integer; 1C+2B=500+600=1100, need 700 = 3.5A, no; 1C+5A=500+1000=1500, need 300 =1B -> that is the same as 1C+1B+5A? Actually 1C+5A=1500, plus 1B=300 gives 1800; that is 1C+5A+1B: cost=850+1520+550=2920. So 2794 still lower. What about 4B=1200 + 2A=400 ->1600, not 1800; 4B+2A=1200+400=1600; need 200 -> 1A gives 1800: that's 4B+3A: weight=1200+600=1800, cost: B4=1870, A3=1140 total=3010. So 2794 seems best. But maybe 5A+2C? Actually 5A=1000, 2C=1000=2000 >1800. 4A+2C? 4A=800,2C=1000=1800, cost:4A=1520? Wait 4A: 4 bags: (4//5=0) so 4*380=1520; 2C=1700 total=3220. So not better.
Now check if there is a combination with more bags to take advantage of discounts across shops? For example, if we buy many A bags to get discount, and some B without discount. The total cost is additive, no cross-shop discount. So the computed min is correct.
But wait: Could there be a combination with negative total? No.
Thus enumeration yields correct.
Now, is there any possibility that the optimal solution uses more than 25 A bags for a weight <=50? That would be impossible because 26*2=52>50. So safe.
Thus precomputation is fine.
Now, regarding performance, we can even compute per dataset without precomputation, but it's fine.
Now, code in Python or any language.
But we need to consider that input is multiple lines, ends with 0. Output each result on separate line.
Now, let's write code.
Pseudo code in Python:
import sys
def cost_A(n):
return (n//5)*1520 + (n%5)*380
def cost_B(n):
return (n//4)*1870 + (n%4)*550
def cost_C(n):
return (n//3)*2244 + (n%3)*850
def precompute():
INF = 10**9
min_cost = [INF]51
for a in range(0,26):
ca = cost_A(a)
for b in range(0,17):
cb = cost_B(b)
for c in range(0,11):
w = 2a + 3b + 5c
if w <= 50:
total = ca + cb + cost_C(c) # careful: we already computed cb, but cost_C(c) maybe compute here
if total < min_cost[w]:
min_cost[w] = total
return min_cost
def main():
min_cost = precompute()
out_lines = []
for line in sys.stdin:
line = line.strip()
if not line:
continue
a = int(line)
if a == 0:
break
w = a // 100 # because a in grams divisible by 100
out_lines.append(str(min_cost[w]))
sys.stdout.write("\n".join(out_lines))
if name == "main":
main()
But check: In the inner loop, we call cost_C(c) inside. We could precompute cost_C list too. But it's fine.
We must ensure cost_C is defined for c up to 10. Yes.
Potential overflow? No.
Edge Cases: w=0? Not possible because a>=500. So no.
Now, is there any scenario where the minimal cost might be found by a combination that uses more bags than our loop maxima but weight <=50? As argued, impossible. Because each bag adds at least 2 units (200g). So if a >25, weight >50. Similarly b>16 gives weight >48, but plus any other positive would push >50. But is there a case where b=17 and a negative? Not allowed. So b max is 16 for weight <=50 if a=c=0. But if b=17 and a = -1? Not allowed. So safe.
Thus precomputation is exhaustive.
Now, test with sample.
Compute min_cost[5] and min_cost[22] as we did.
All good.
Now, could there be a scenario where the minimal cost is not captured because we didn't consider ordering across shops? No.
Thus solution is correct.
But before finalizing, let's double-check discount interpretation. The problem statement: "Discounted units: per 5 bags", "Discount rate: reduced by 20%". The example: 12 bags, reduced by 20% for 10 bags. So they treat discount for each group of 5. So our interpretation is correct.
But what about the possibility that the discount applies only to the number of bags that are in groups, not to the entire groups? That's same.
Now, we might think about whether we can get a better price by buying extra bags beyond needed and discarding flour? The problem explicitly says "you should buy the flour of exactly the same amount as the given input." So no.
Thus done.
Now, we can consider if there's any optimization needed. No.
Now, we might implement in C++ as required? The problem statement is generic, but we can answer in Python. Since we are ChatGPT, we can produce code in Python.
But typical competition might want C/C++ but Python is fine for small inputs.
Thus final answer: Provide Python code.
Now, I'd like to test some more values manually to verify that our precomputation yields expected results.
Let's compute min_cost for all w from 5 to 50, maybe some interesting ones.
We can also cross-check with DP to ensure correctness.
But we trust enumeration.
Potential pitfalls: We used cost_C(c) in the innermost loop, but we didn't precompute an array for C. That's fine; we might compute many times, but only 261711=4862 calls to cost_C, trivial.
But we could precompute arrays for each shop: cost_A_arr = [cost_A(i) for i in range(26)], etc. That would be slightly more efficient.
We'll do that.
Implementation details:
cost_A_arr = [cost_A(i) for i in range(26)] etc.
Then loops:
for a in range(26):
for b in range(17):
for c in range(11):
w = 2a + 3b + 5*c
if w <= 50:
total = cost_A_arr[a] + cost_B_arr[b] + cost_C_arr[c]
if total < min_cost[w]:
min_cost[w] = total
Now, cost_A_arr[0]=0, cost_B_arr[0]=0, cost_C_arr[0]=0.
Now, is there any possibility that weight exactly 0? not needed.
Now, check if for some w, min_cost remains INF (unreachable). For w>=5, it should be reachable because bag sizes 2,3,5 can generate all integers >= something? The Frobenius coin problem: with 2 and 3, all numbers >= 2 are representable? Actually with coin denominations 2 and 3, the largest not representable is 1. Since 2 and 3 are coprime? gcd(2,3)=1, so all integers >= (2*3 - 2 - 3 + 1 = 2) maybe? Actually Frobenius number for two coprime a,b is ab - a - b = 6-2-3=1, so all integers >= 2 are representable as nonnegative combos of 2 and 3. So with denominations 2,3,5 (including 5), all integers >= 2? Actually with 2 and 3 we already cover all integers >= 2 except 1. So any w>=2 can be represented using 2 and 3 alone. However we have w min 5, so definitely reachable. So min_cost[w] will be set.
But what about w=1? Not needed.
Thus ok.
Now, confirm that our cost functions produce integer results for all n up to maxima. Check:
A: n=0 => 0. n=5 =>1520. Good.
B: n=4 => 1870. n=5 => 1870+550=2420. Good.
C: n=3 =>2244. n=4 =>2244+850=3094. Good.
Now, edge case: Suppose we want to buy 5000g (w=50). Options: A only: 25 bags, cost_A(25)= (25//5)1520 + (25%5)0 = 51520=7600. B only: max 16 bags gives 4800g, not 5000; 16300=4800, need 200g -> not exact. Actually B only cannot make 5000 because 5000/300 not integer. C only: 10 bags=5000g, cost_C(10)= (10//3)2244 + (10%3)850 = 32244 + 1850? 10//3=3, 10%3=1 => 6732+850=7582. Mix may give lower? For example, 20 A (4000g) + 2 B (600g) + 1 C (500g) = 5100g >5000. Let's try find exact 5000. Solve 2a+3b+5c=50. Options: c=10,a=b=0 gives 7582. c=9 =>45, need 5 => a= ? 2a+3b=5 => possible (a=1,b=1) gives 2+3=5, total cost: c9: cost_C(9)= (9//3)2244 + (9%3)850 = 32244 + 0 = 6732? Actually 9//3=3, 9%3=0 => cost = 32244 = 6732. a=1 cost_A(1)=380, b=1 cost_B(1)=550. total=6732+380+550=7662 >7582. c=8 =>40, need 10 => 2a+3b=10. Solutions: (a=5,b=0) cost A5=1520; (a=2,b=2) cost=760+1100=1860; (a= -? ) (a=0,b=3.33 no) (a=1,b=8/3 no) So best is a=5,b=0 cost 1520, total = cost_C(8)+1520. cost_C(8)= (8//3)2244 + (8%3)850 = 22244 + 2850 = 4488+1700=6188. total=7708. Not better. c=7 =>35, need 15 => 2a+3b=15. Solutions: (a=0,b=5) cost B5=2420; (a=3,b=3) cost A3=1140, B3=1650 total=2790; (a=6,b=1) A6=1900, B1=550 total=2450; (a= ? ) (a= ? ) find min cost. Compute min cost for weight 15 from A and B? That's a subproblem. But anyway add cost_C(7). cost_C(7)= (7//3)2244 + (7%3)850 = 22244 + 1850 = 4488+850=5338. Adding min cost for weight 15 from A/B? But we must satisfy exact weight 15. The min cost for weight 15 from A and B might be something. Let's compute: all combos with 2a+3b=15. List b odd/even? 3b mod 2? Actually 15 odd, so b must be odd? 2a is even, so 3b must be odd => b odd. b=1 => a=6 cost: A6=1900, B1=550 total=2450. b=3 => a=3 => A3=1140, B3=1650 total=2790. b=5 => a=0 => B5=2420. So min is 2450 from (6,1). So total = 5338+2450=7788. So not better.
c=6 =>30, need 20 => 2a+3b=20. Find min cost. b must be even? 20 even, 2a even, so 3b even => b even. b=0 => a=10 cost A10= (10//5)1520 +0=21520=3040. b=2 => a=7 => A7= (7//5)1520 + 2380 = 11520 + 760 =2280? Wait 7%5=2 so 1520+760=2280, B2=1100 total=3380. b=4 => a=4 => A4=1520? Actually 4%5=4 => 01520+4380=1520, B4=1870 total=3390. b=6 => a=1 => A1=380, B6=2970 total=3350. b=8 => a=-2 no. So min is 3040 from (10,0). But also maybe other combos with b=... Actually (10,0) gives 3040. Check (0,?) can't because b=20/3 not integer. So min 3040. cost_C(6)= (6//3)2244 + (6%3)850 = 22244 = 4488. total = 4488+3040=7528. Better than 7582? 7528 < 7582. So lower than pure C. Let's check if any even better: c=5 =>25, need 25 => 2a+3b=25. b odd? 25 odd => b odd. b=1 => a=11 => A11=21520+1380=3040+380=3420, B1=550 total=3970. b=3 => a=8 => A8=11520+3380=1520+1140=2660, B3=1650 total=4310. b=5 => a=5 => A5=1520, B5=2420 total=3940. b=7 => a=2 => A2=760, B7= (7//4)1870 + (7%4)550 = 11870+3550=1870+1650=3520 total=4280. b=9 => a=-1 no. So min is 3970? Actually check b=1 total 3970, b=5 total 3940, which is slightly lower 3940. So min is 3940. cost_C(5)= (5//3)2244 + (5%3)850 = 12244 + 2850 = 2244+1700=3944. Wait compute: 5//3=1, 5%3=2 -> cost = 12244 + 2850 = 2244+1700=3944. Then total = 3944 + 3940 = 7884. So worse.
c=4 =>20, need 30 => 2a+3b=30. b even? 30 even => b even. b=0 => a=15 => A15= (15//5)1520 + (15%5)0? 15%5=0 => 31520=4560. b=2 => a=12 => A12= (12//5)1520 + (12%5)380 = 21520 + 2380 =3040+760=3800, B2=1100 total=4900. b=4 => a=9 => A9=11520+4380=1520+1520=3040, B4=1870 total=4910. b=6 => a=6 => A6=11520+1380=1900, B6=2970 total=4870. b=8 => a=3 => A3=1140, B8= (8//4)1870+0? 8//4=2, 21870=3740? Actually 8%4=0 => cost = 21870=3740, total=4880. b=10 => a=0 => B10= (10//4)1870 + (10%4)550 = 21870 + 2550 =3740+1100=4840? Actually 21870=3740, plus 2550=1100 =4840. So min among these? A15=4560, but A15 is pure A weight=30? Actually we need weight 30 from A/B combo to add to c=4 (20) to total 50. So the cost for weight 30 from A/B alone is min over combos of (a,b) with 2a+3b=30. Let's compute properly: Actually we want to minimize total cost = cost_C(4) + cost_A(a)+cost_B(b). So we need to find min over (a,b) of cost_A(a)+cost_B(b) subject to 2a+3b=30. We can compute all possibilities. Let's compute systematically: The costs we computed: (a,b) possibilities: (15,0) cost_A(15)= 31520=4560; (12,2) cost_A(12)= 21520+2380=3040+760=3800, cost_B(2)=1100 total=4900; (9,4) cost_A(9)= 11520+4380=1520+1520=3040, cost_B(4)=1870 total=4910; (6,6) cost_A(6)= 11520+1380=1900, cost_B(6)= (6//4)1870 + (6%4)550 =11870+2550=1870+1100=2970 total=4870; (3,8) cost_A(3)=1140, cost_B(8)= (8//4)1870=21870=3740 total=4880; (0,10) cost_B(10)= (10//4)1870 + (10%4)550 =21870 + 2550 =3740+1100=4840. So min appears to be 4560 from (15,0). However, we also need to consider if there are other combos like b=12 => a=-3 no. So min is 4560. Then cost_C(4)=? cost_C(4)= (4//3)2244 + (4%3)850 = 12244 + 1850 = 3094? Actually 4%3=1 => +850, so 2244+850=3094. So total = 3094+4560 = 7654. That's larger than 7528 (c=6 case). So far best is 7528 from c=6, a=10,b=0. Wait, that combination gave total 7528. But is that exact? c=6 gives 30 weight, a=10 gives 20 weight, total 50. cost_C(6)=4488, cost_A(10)=3040, sum=7528. But earlier we computed cost_A(10)=3040 (since 10//5=2 => 21520=3040). Yes. So 7528.
Now c=3 =>15, need 35 => 2a+3b=35. b odd? 35 odd => b odd. Let's find min cost for weight 35 from A and B. Possibly we could have a=10,b=5? 210+35=20+15=35. cost: A10=3040, B5=2420 total=5460. a=7,b=7 =>14+21=35, cost: A7=11520+2380=1520+760=2280? Wait A7: 7//5=1, 7%5=2 => 1520+760=2280, B7= (7//4)1870+(7%4)550=11870+3550=1870+1650=3520 total=5800. a=4,b=9 =>8+27=35, A4=1520, B9= (9//4)1870+(9%4)550=21870+1550=3740+550=4290 total=5810. a=1,b=11 =>2+33=35, A1=380, B11= (11//4)1870+(11%4)550=21870+3550=3740+1650=5390 total=5770. a=13,b=3 =>26+9=35, A13= (13//5)1520 + (13%5)380 = 21520 + 3380 =3040+1140=4180, B3=1650 total=5830. a=16,b=1 =>32+3=35, A16= (16//5)1520 + (16%5)380 =31520 +1380=4560+380=4940, B1=550 total=5490. So min seems 5460 from (10,5) or maybe (10,5) is 5460, check others: (a=5,b= ? 2*5+3b=35 => 3b=25 => not integer. So min is maybe 5460? But also maybe (a=0,b=35/3 not integer). So min cost for weight 35 is 5460. Add cost_C(3)=2244 => total=7704. So not better.
c=2 =>10, need 40 => 2a+3b=40. b even? 40 even => b even. Options: b=0 => a=20 cost_A(20)= (20//5)1520 + (20%5)0 =41520=6080. b=2 => a=17 => 217+6=40? Actually 32=6, 40-6=34 => a=17, cost_A(17)= (17//5)1520 + (17%5)380 =31520 + 2380=4560+760=5320, B2=1100 total=6420. b=4 => a=14 => A14= (14//5)1520 + (14%5)380 =21520 + 4380 =3040+1520=4560, B4=1870 total=6430. b=6 => a=11 => A11=21520+1380=3040+380=3420, B6=2970 total=6390. b=8 => a=8 => A8=1520+1140=2660, B8= (8//4)1870=21870=3740 total=6400. b=10 => a=5 => A5=1520, B10=4840 total=6360. b=12 => a=2 => A2=760, B12= (12//4)1870=31870=5610? Actually 12//4=3, 12%4=0 => cost=31870=5610 total=6370. b=14 => a=-1 no. So min is 6080? Actually check b=0 =>6080, but b=10 =>6360, so min is 6080? However, we should also check b= ? maybe b= 13? b must be even? Actually 3b mod 2: if b is odd, 3b is odd, then 2a = 40 - odd = odd => a not integer. So b must be even. So min is 6080 from (20,0). Also check b=... Actually b=0 gives 6080. b=2 gives 6420, b=4 6430, etc. So 6080 is min. Add cost_C(2)=1700 total=7780.
c=1 =>5, need 45 => 2a+3b=45. b odd? 45 odd => b odd. b=1 => a=21 => A21= (21//5)1520 + (21%5)380 =41520 + 1380=6080+380=6460, B1=550 total=7010. b=3 => a=18 => A18= (18//5)1520 + (18%5)380 =31520 + 3380=4560+1140=5700, B3=1650 total=7350. b=5 => a=15 => A15=31520=4560, B5=2420 total=6980. b=7 => a=12 => A12=21520+2380=3040+760=3800, B7=3520 total=7320. b=9 => a=9 => A9=1520+1520=3040, B9= (9//4)1870+(9%4)550=21870+1550=3740+550=4290 total=7330. b=11 => a=6 => A6=1520+380=1900, B11= (11//4)1870+ (11%4)550=21870+3550=3740+1650=5390 total=7290. b=13 => a=3 => A3=1140, B13= (13//4)1870+(13%4)550=31870+1550=5610+550=6160 total=7300. b=15 => a=0 => B15= (15//4)1870+(15%4)550=31870+3550=5610+1650=7260? Actually 31870=5610, 3*550=1650, total=7260. So min seems 6980? Actually b=5 gives 6980, b=1 gives 7010, b=5 gives 6980. Check b=5: A15=4560, B5=2420 total=6980. b= ? b= ? Maybe b= - no. So min ~6980. Add cost_C(1)=850 => total=7830.
c=0 => need 50 from A/B. 2a+3b=50. b even? 50 even => b even. b=0 => a=25 cost_A(25)=51520=7600. b=2 => a=22 => A22= (22//5)1520 + (22%5)380 =41520 + 2380=6080+760=6840, B2=1100 total=7940. b=4 => a=19 => A19= (19//5)1520 + (19%5)380 =31520+4380=4560+1520=6080, B4=1870 total=7950. b=6 => a=16 => A16=31520+1380=4560+380=4940, B6=2970 total=7910. b=8 => a=13 => A13=21520+3380=3040+1140=4180, B8=21870=3740 total=7920. b=10 => a=10 => A10=3040, B10=4840 total=7880. b=12 => a=7 => A7=1520+760=2280, B12=3*1870=5610 total=7890. b=14 => a=4 => A4=1520, B14= (14//4)1870+(14%4)550=31870+2550=5610+1100=6710 total=8230? Actually 1520+6710=8230. b=16 => a=1 => A1=380, B16= (16//4)1870=41870=7480 total=7860. So min is 7600 from pure A? Actually pure A is 7600. Also maybe b=... b=0 gives 7600. Could there be lower? Let's check b= ? Actually b=0 gives 7600. b=2 gives 7940, so not lower. So min for pure A/B is 7600. That's more than 7528 from c=6, a=10. So overall min for weight 50 seems to be 7528. So the answer for 5000g would be 7528? But we haven't seen that in sample inputs. It's plausible.
Now, our precomputation should capture that.
Thus the approach is solid.
Now, consider potential alternative: Could there be a scenario where buying more than one shop yields lower cost than any single shop for that weight? Yes, as seen.
Thus solution is correct.
Now, note that we must handle multiple datasets, and we have precomputed min_cost for all possible w. So for each input, we just output min_cost[w].
Now, check for w=22 -> 3390, we already matched.
Now, let's test w=24 maybe? Not required.
Now, note that the problem statement says "The number of datasets does not exceed 50." So we can safely precompute.
Now, let's think about any tricky part: The discount is "reduced by 20%" meaning 20% off, i.e., price becomes 80% of original. So our calculation is correct.
Now, check shop B: discount per 4 bags reduced by 15% -> so price for group of 4 is 55040.85 = 1870, correct.
Shop C: 12% off -> 85030.88=2244, correct.
Now, is there any nuance about overlapping discounts? For example, if you buy 8 bags at shop A, do you get discount for 5 bags twice, i.e., 10 bags discounted and 3 not? That's what we did: 8 //5 =1, remainder 3, so discount applied to 5 bags, not to 3. So correct.
Now, the problem says "Discounted units: per 5 bags" which might be interpreted as "for every 5 bags, you get a discount", not cumulative? Yes.
Now, there is no additional global discount.
Thus code is ready.
But before final answer, we should consider if we need to handle the case where the input includes a number not reachable? It says a is divisible by 100 and between 500 and 5000 inclusive. So reachable as argued.
Now, let's implement in Python.
Now, possible improvement: Since we are doing triple loops, we could also compute min_cost via DP: for each shop, we could treat it as an item with multiple copies and discount structure, but enumeration is simpler.
Thus final answer: Provide the Python code.
Now, let's double-check the limits: For a=5000, w=50. Our loops go a up to 25, b up to 16, c up to 10. That's fine. But note that combination with a=0, b=16, c=0 gives weight 48, which is <=50; a=25,b=0,c=0 weight=50; a=25,b=16,c=10 weight=148 >50 but we skip.
Now, memory: min_cost array size 51.
Now, we also must be careful that cost_C(c) for c up to 10 is computed. For c=10, cost_C(10) = (10//3)2244 + (10%3)850 = 32244 + 1850 = 6732+850=7582? Actually 3*2244=6732, plus 850 = 7582. Yes.
Now, we may also compute cost_B for b=16: b=16 => (16//4)*1870 + (16%4)550 = 41870 + 0 = 7480.
cost_A for a=25 => 5*1520 = 7600.
Now, the triple loops may cause some waste as we compute cost_A_arr, cost_B_arr, cost_C_arr once.
Now, we can write:
def precompute_min_cost():
INF = 10**9
minc = [INF]*51
costA = [0]*26
for i in range(1,26):
costA[i] = (i//5)*1520 + (i%5)*380
costB = [0]*17
for i in range(1,17):
costB[i] = (i//4)*1870 + (i%4)*550
costC = [0]*11
for i in range(1,11):
costC[i] = (i//3)*2244 + (i%3)*850
for a in range(26):
for b in range(17):
for c in range(11):
w = 2*a + 3*b + 5*c
if w <= 50:
total = costA[a] + costB[b] + costC[c]
if total < minc[w]:
minc[w] = total
return minc
But we need to include i=0: costX[0]=0.
Now, run precompute_min_cost.
But note: We might have to consider that the minimal cost for weight w might be achieved by combination with numbers exceeding the maxima? As argued, no.
Now, we should also consider that maybe some weight can be achieved with a combination where a>25 but b negative? No.
Thus finalize.
Now, test with sample:
Input:
500
2200
0
Our precomputed minc[5] and minc[22] should be 850 and 3390.
We'll simulate quickly:
Precompute minc[5]: combos: (a,b,c) such that 2a+3b+5c=5. c can be 1 => weight5, costC[1]=850, a=b=0 => total=850. Others: (a=1,b=1,c=0) weight5, costA1=380, costB1=550 total=930 >850. (a=0,b=0,c=1) already. (a=2,b=?? 4, need1 => not possible). So minc[5]=850.
minc[22]: combos: we found (a=5,b=4,c=0) costA5=1520, costB4=1870 total=3390. Others: (a=11,b=0,c=0)=3420; (a=8,b=2,c=0)= costA8=1520+1140=2660? Wait A8=1520+1140=2660? Actually 8//5=1, remainder 3 => 1520+3*380=1520+1140=2660, B2=1100 => total=3760; (a=2,b=6,c=0): A2=760, B6=2970 total=3730; (a=0,b=0,c=?) c=4 =>20 need 2 => a=1 => weight=2+20=22, cost: C4=2244+850=3094, A1=380 total=3474; etc. So min is 3390.
Thus code outputs correctly.
Now, we should ensure that we handle input reading correctly, and stop at 0.
Now, consider possibility that a is exactly 0? Only as termination.
Now, final code.
But we must provide explanation and code. As per instructions, we need to "Write a program". So we can output the code.
Now, possible pitfalls: Python's default recursion? Not used.
Now, maybe we can micro-optimize by limiting loops to weight <=50, but not needed.
Thus final answer is the Python code.
Now, the problem might be from an online judge expecting C++? But we are ChatGPT, we can output Python code. The user didn't specify language, so Python is fine.
Thus answer: Provide code with explanation? The instruction says "Write a program which reads...". So we can just output the code.
Now, I'll produce final answer with code.
But also include comments.
Let's write final code:
import sys
def precompute():
INF = 10**9
min_cost = [INF] * 51 # index = weight/100
# compute costs for each shop
costA = [0] * 26
for i in range(1, 26):
costA[i] = (i // 5) * 1520 + (i % 5) * 380
costB = [0] * 17
for i in range(1, 17):
costB[i] = (i // 4) * 1870 + (i % 4) * 550
costC = [0] * 11
for i in range(1, 11):
costC[i] = (i // 3) * 2244 + (i % 3) * 850
for a in range(26):
ca = costA[a]
for b in range(17):
cb = costB[b]
for c in range(11):
w = 2*a + 3*b + 5*c
if w <= 50:
total = ca + cb + costC[c]
if total < min_cost[w]:
min_cost[w] = total
return min_cost
def main():
min_cost = precompute()
out = []
for line in sys.stdin:
line = line.strip()
if not line:
continue
a = int(line)
if a == 0:
break
w = a // 100 # because a is in grams, divisible by 100
out.append(str(min_cost[w]))
sys.stdout.write("\n".join(out))
if __name__ == "__main__":
main()
Now, ensure that we have considered all combos. Yes.
One small nuance: For a=5000, w=50, min_cost[50] should be 7528 as computed. Let's confirm that our loops include a=10,b=0,c=6. Yes, a=10, b=0, c=6: weight=210+0+56=20+30=50, total= costA[10]=3040 + costB[0]=0 + costC[6]=4488 => 7528. Also check a=0,b=0,c=10 => 7582, a=25,b=0,c=0 => 7600. So 7528 is min. Our loops will set min_cost[50] accordingly.
But is there a combination with a=5,b=10,c=0? weight=25+310=10+30=40, not 50. a=5,b=0,c=8? weight=10+40=50, a=5,c=8 => costA5=1520, costC8=6188 total=7708. a=0,b=10,c=4 => weight=30+20=50, costB10=4840, costC4=3094 total=7934. a=0,b=5,c=7? weight=15+35=50, costB5=2420, costC7=5338 total=7758. a=15,b=0,c=4? weight=30+20=50, costA15=4560, costC4=3094 total=7654. So 7528 seems best.
But wait, we found earlier that c=6,a=10 gave 7528. Could there be a combination with a=7,b=2,c=6? weight=14+6+30=50, costA7=2280, costB2=1100, costC6=4488 total=7868. a=8,b=2,c=6? weight=16+6+30=52 >50, not considered because w>50; we skip. So okay.
Now, consider a=13,b=8,c=0? weight=26+24=50, costA13=4180, costB8=3740 total=7920. So 7528 seems minimal.
Thus code is correct.
Now, finalize.
But also note: The problem statement says "Note that you should buy the flour of exactly the same amount as the given input." So we cannot buy more and discard. We already enforced exact weight.
Now, one more check: The input numbers are divisible by 100, but do we need to consider that the bag amounts are all multiples of 100 (200,300,500), so the total weight will always be a multiple of 100. Good.
Now, we can produce final answer.
Thus final answer: Python code as above. ```python
import sys
def precompute_min_cost():
"""Precompute the minimal cost for each weight (in units of 100g)."""
INF = 10 ** 9
min_cost = [INF] * 51 # index = weight / 100, max 5000g -> 50
# cost for shop A: 200g bags, 380 yen, 20% off per 5 bags
costA = [0] * 26 # up to 25 bags (25*200 = 5000g)
for i in range(1, 26):
costA[i] = (i // 5) * 1520 + (i % 5) * 380 # 5*380*0.8 = 1520
# cost for shop B: 300g bags, 550 yen, 15% off per 4 bags
costB = [0] * 17 # up to 16 bags (16*300 = 4800g)
for i in range(1, 17):
costB[i] = (i // 4) * 1870 + (i % 4) * 550 # 4*550*0.85 = 1870
# cost for shop C: 500g bags, 850 yen, 12% off per 3 bags
costC = [0] * 11 # up to 10 bags (10*500 = 5000g)
for i in range(1, 11):
costC[i] = (i // 3) * 2244 + (i % 3) * 850 # 3*850*0.88 = 2244
# enumerate all possible combinations of bag counts
for a in range(26):
ca = costA[a]
for b in range(17):
cb = costB[b]
for c in range(11):
w = 2 * a + 3 * b + 5 * c # weight in units of 100g
if w <= 50:
total = ca + cb + costC[c]
if total < min_cost[w]:
min_cost[w] = total
return min_cost
def main():
min_cost = precompute_min_cost()
out = []
for line in sys.stdin:
line = line.strip()
if not line:
continue
a = int(line)
if a == 0:
break
w = a // 100 # convert grams to units of 100g
out.append(str(min_cost[w]))
sys.stdout.write("\n".join(out))
if name == "main":
main()