The restaurant AtCoder serves the following five dishes:
ABC Don (rice bowl): takes A minutes to serve.
ARC Curry: takes B minutes to serve.
AGC Pasta: takes C minutes to serve.
APC Ramen: takes D minutes to serve.
ATC Hanbagu (hamburger patty): takes E minutes to serve.
Here, the time to serve a dish is the time between when an order is placed and when the dish is delivered.
This restaurant has the following rules on orders:
An order can only be placed at a time that is a multiple of 10 (time 0, 10, 20, ...).
Only one dish can be ordered at a time.
No new order can be placed when an order is already placed and the dish is still not delivered, but a new order can be placed at the exact time when the dish is delivered.
E869120 arrives at this restaurant at time 0. He will order all five dishes. Find the earliest possible time for the last dish to be delivered.
Here, he can order the dishes in any order he likes, and he can place an order already at time 0.
Constraints
A, B, C, D and E are integers between 1 and 123 (inclusive).
Input
Input is given from Standard Input in the following format:
A
B
C
D
E
Output
Print the earliest possible time for the last dish to be delivered, as an integer.
Examples
Input
29
20
7
35
120
Output
215
Input
101
86
119
108
57
Output
481
Input
123
123
123
123
123
Output
643
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to compute the minimal possible time when the last dish is delivered given that we must order all five dishes sequentially, with the constraint that orders can only be placed at times that are multiples of 10. The order can be placed at time t if t is multiple of 10 and no other order is pending (i.e., the previous dish has been delivered by that time, or exactly at that time delivery and then we can place new order). Actually the rule: "No new order can be placed when an order is already placed and the dish is still not delivered, but a new order can be placed at the exact time when the dish is delivered." So we can think of sequential processing: we start at time 0 (multiple of 10). We place an order for some dish, which takes its serving time (e.g., A minutes) to be delivered. The delivery occurs at time = order time + serving time. At that exact moment, we can place a new order (if we haven't ordered all dishes). However, the new order must be placed at a time that is a multiple of 10. The delivery time may not be a multiple of 10. But we can place an order at the exact delivery time only if that time is a multiple of 10? The rule says: "a new order can be placed at the exact time when the dish is delivered." It does not require that the exact time be a multiple of 10? Actually the first rule: "An order can only be placed at a time that is a multiple of 10 (time 0, 10, 20, ...)." So orders can only be placed at times that are multiples of 10. However, the third rule says: "No new order can be placed when an order is already placed and the dish is still not delivered, but a new order can be placed at the exact time when the dish is delivered." This indicates an exception: at the exact moment of delivery, you can place a new order even if that moment is not a multiple of 10? But the first rule seems strict: orders can only be placed at multiples of 10. The statement "a new order can be placed at the exact time when the dish is delivered" might be clarifying that you can place an order exactly at that moment (which might be a multiple of 10 or not?) Actually reading carefully: The first rule: "An order can only be placed at a time that is a multiple of 10 (time 0, 10, 20, ...)." So the order placement times must be multiples of 10. Then the third rule: "No new order can be placed when an order is already placed and the dish is still not delivered, but a new order can be placed at the exact time when the dish is delivered." This defines when you are allowed to place a new order: you cannot while an order is pending, except at the exact moment the dish is delivered. However, that exact moment still must satisfy the first rule: it must be a multiple of 10 to place the order. So if the dish is delivered at a time that is not a multiple of 10, you cannot place an order at that exact moment because the time is not a multiple of 10. But then you would have to wait until the next multiple of 10 to place the next order. But then the rule "a new order can be placed at the exact time when the dish is delivered" might be interpreted as overriding the multiple-of-10 rule? Typically in such scheduling problems (similar to ABC123 B?), I recall a problem: "Five Dishes" from AtCoder ABC 123 B? Actually there is AtCoder ABC 100 B? Not sure. Let's recall: There's a known AtCoder problem: "Five Dishes" from ABC 123 B? I think ABC 123 B is "Five Dishes". Yes, exactly. I've seen it. In that problem, you have five dishes with cooking times. You can order at time 0,10,20,... and you want the minimal time to finish all. The typical solution is to sort by "remainder" and place the dish with largest remainder last to minimize waiting time. I remember the problem: "AtCoder Beginner Contest 123 B - Five Dishes". The solution: compute total time = sum of times + waiting times. For each dish, you need to wait to align to next multiple of 10 if the time is not a multiple of 10. Actually the typical approach: Since you can only order at multiples of 10, you have to start each order at a time that is a multiple of 10. The first order can be at time 0. After a dish is delivered at time t, you can place the next order at the smallest multiple of 10 that is >= t, because you can't place an order until t (since you cannot have overlapping orders), and you can only place at multiples of 10. But if t is exactly a multiple of 10, you can place immediately. So the waiting time is (10 - t%10) %10, i.e., the time needed to reach the next multiple of 10, unless t%10==0 then 0. So the total completion time = sum_{i=1 to 5} (cooking_time_i) + sum of waiting times (except for the last dish, you don't need to wait after its delivery). However, you can reorder dishes to minimize total time, because waiting times depend on the remainders. The typical solution: sort dishes by their remainder (time mod 10) in descending order, but place the one with remainder 0? Actually you want to minimize idle waiting time. The total sum of cooking times is fixed. The waiting time occurs before ordering each dish except the first. The waiting time before dish i (i>=2) is (10 - (current_time mod 10)) mod 10, where current_time is the cumulative time after previous dish delivered. Since after each dish you might need to wait to the next multiple of 10 to order next. The waiting time is determined by the remainder of the cumulative time at that point. The cumulative time is sum of previous cooking times plus previous waiting times. The waiting time before dish i depends on the cumulative sum modulo 10 before ordering. Since waiting time adds to cumulative time, it's like you want to schedule dishes so that the cumulative time before each order is such that the waiting times are minimized. A known greedy: sort by remainder (time % 10) descending, but put the dish with remainder 0 at the end? Actually typical solution: compute total = sum of times. For each dish, the waiting time is (10 - t%10) %10 added to total if t%10 !=0, but you can subtract the maximum possible saving: the maximum of (10 - t%10) %10? Let's derive.
We start at time 0. We order dish 1 at time 0, it finishes at time t1. Then we can order dish 2 at time ceil(t1/10)*10 (since we must order at a multiple of 10 >= t1). So the start time for dish 2 is s2 = ceil(t1/10)*10. So the waiting time is w1 = s2 - t1 = (10 - (t1 mod 10)) mod 10. Then dish 2 finishes at time f2 = s2 + t2 = ceil(t1/10)*10 + t2. Then s3 = ceil(f2/10)*10, etc. Finally after dish 5, we don't care about waiting.
Goal: minimize f5.
We can choose order of dishes (permutation). Need to compute minimal f5.
This is a classic problem; solution: compute total = sum(t_i). For each dish, add extra waiting time to align to next multiple of 10 if its finish time not multiple of 10, except for the last dish we don't need to wait. So total f5 = total + sum_{i=1}^{4} waiting_i, but waiting_i depends on the cumulative time mod 10 before ordering dish i+1. But we can choose order to minimize the sum of waiting times.
Let the cooking times be a1..a5. Define r_i = a_i mod 10, with remainder in [0,9]. For a dish that finishes at time T, the waiting time to next multiple of 10 is (10 - (T mod 10)) mod 10. Initially T0 = 0. After ordering first dish at time 0, finish at T1 = a1. Waiting w1 = (10 - (a1 mod 10)) mod 10. Then T1' = T1 + w1 (which is multiple of 10). Then T2 = T1' + a2 = a1 + w1 + a2. w2 = (10 - ((a1 + w1 + a2) mod 10)) mod 10? But note that T1' is a multiple of 10, so (a1 + w1) is multiple of 10. So T1' mod 10 = 0. Then T2 = a2 + multiple of 10, so T2 mod 10 = a2 mod 10 = r2. So w2 = (10 - r2) mod 10. Then after adding w2, T2' becomes multiple of 10. So in this sequential process, if we always order at a time that is a multiple of 10 (which we do because we wait to align), then the finish time after each dish is the start time (multiple of 10) plus the dish's cooking time, so the remainder is just the cooking time mod 10. And we wait to next multiple of 10, which adds (10 - r_i) % 10, except we don't wait after the last dish. So the total time = sum_i a_i + sum_{i=1}^{4} ( (10 - r_{π(i)} ) % 10 ), where the order of dishes π determines which dish is last (no waiting after it) and the others contribute waiting based on their own remainder (not cumulative). However, is that correct? Let's examine.
If we always start each dish at a multiple of 10, then the start time for dish i is S_i, where S_1 = 0, S_i for i>1 is the previous finish time rounded up to multiple of 10. The finish time F_i = S_i + a_i. Then S_{i+1} = ceil(F_i/10)*10 = F_i + ( (10 - (F_i mod 10)) mod 10 ). So the waiting after dish i is w_i = (10 - (F_i mod 10)) mod 10, for i = 1..4. And F_i mod 10 = (S_i + a_i) mod 10 = (0 + a_i) mod 10 because S_i is multiple of 10. Indeed S_i is a multiple of 10, so (S_i mod 10)=0. So F_i mod 10 = a_i mod 10 = r_i. So w_i = (10 - r_i) mod 10, independent of previous wait times. So the total completion time = sum_i a_i + sum_{i=1}^{4} ((10 - r_i) mod 10). This suggests that the order doesn't matter except for which dish is considered the last one, because the last dish does not incur a waiting time after it, so we would like to have as the last dish the one with the largest waiting penalty? Actually, the waiting penalty for a dish is (10 - r) % 10. This is zero if r=0, and positive otherwise. If we put a dish with a positive penalty as the last dish, we avoid adding its penalty. So we can reduce total time by making the dish with the largest penalty be last, because we skip adding its waiting time. For other dishes, we add their penalties anyway. So the optimal order: choose one dish to be last, preferably the one with the largest (10 - r) % 10, i.e., the smallest positive remainder? Wait (10 - r) % 10 is 0 when r=0, otherwise it's 10 - r if r>0. This value is larger when r is smaller? For example, r=1 => penalty 9, r=9 => penalty 1. So the penalty is large when remainder is small (but not zero). The worst penalty is 9 (if remainder 1) and the smallest penalty (besides 0) is 1 (if remainder 9). So we want to avoid paying a large penalty, so we should make the dish with the largest penalty be last. So we want to choose the dish that maximizes (10 - r) % 10 (i.e., has the smallest non-zero remainder, or zero? Actually zero penalty is best; we would want to not waste the opportunity to skip a large penalty; if there is a dish with penalty 0 (r=0), skipping it yields no benefit, so better to have it not last and pay 0 anyway. So we want last dish to have the maximum penalty, to save that amount. So the minimal total time = sum(a_i) + sum_{i != last} penalty(i) = sum(a_i) + (total_penalty - max_penalty). Where penalty(i) = (10 - (a_i mod 10)) % 10. And total_penalty = sum of all five penalties. Then total = sum(a_i) + total_penalty - max_penalty.
But careful: Is this always valid? The reasoning assumed that after each dish, we wait to next multiple of 10, and that the waiting only depends on the dish just finished. However, is it possible that we don't need to wait after a dish if the next dish can be ordered exactly at the finish time even if it's not a multiple of 10? The rules say: "a new order can be placed at the exact time when the dish is delivered." But we also have the rule: "An order can only be placed at a time that is a multiple of 10." So the delivery time must be a multiple of 10 for the order to be placed exactly. If it's not, then you cannot place at that exact time because it's not allowed. So you must wait until the next multiple of 10. So our assumption is correct.
But is there any scenario where you might intentionally delay ordering a dish to a later multiple of 10 even if you could order earlier? That would increase time, so not optimal.
Thus the optimal schedule is to order dishes in any order, but the waiting times after each dish (except last) are precisely the penalties as defined. The order of the first four doesn't matter because each penalty is added based on that dish's own remainder. So the total time is sum of all cooking times plus sum of penalties for the first four dishes. So to minimize, we should put the dish with largest penalty as last.
But wait: There's a nuance: What if a dish has remainder 0? Then its penalty = 0. It doesn't matter if it's last or not. The max penalty among all might be 0 if all have remainder 0. Then total = sum + (0 - 0) = sum. That matches: if all times are multiples of 10, you can order at 0, finish at time a1, which is multiple of 10, order immediately next, etc., and finish at sum.
If some have positive penalties, we can skip the largest penalty.
But is there any scenario where the order of the first four matters because the waiting time after a dish might be influenced by the previous waiting? No, because as argued, S_i is multiple of 10, so F_i mod 10 = a_i mod 10. So independent. So ordering the first four in any order yields the same sum of their penalties. So only the last dish matters.
But is it always true that S_i is multiple of 10? Yes, because we start at 0 and after each wait we round up to multiple of 10. So indeed each order start time is a multiple of 10. So the remainder of finish times is exactly the remainder of the cooking time. So the waiting after each dish (except last) is determined solely by that dish's remainder.
Thus solution: read A,B,C,D,E. Compute times = [A,B,C,D,E]. For each time t, compute penalty = (10 - t % 10) % 10 (i.e., if t%10==0 then 0 else 10 - t%10). Sum all times = total_sum. Sum all penalties = total_penalty. Find max_penalty = max(penalties). Answer = total_sum + total_penalty - max_penalty.
Example 3:
All 123: each t%10=3 => penalty=7 each. Total_sum=5123=615. Total_penalty=57=35. max_penalty=7. Answer=615+35-7=643. Matches.
Thus solution correct.
But there is a twist: The problem statement says: "He will order all five dishes. Find the earliest possible time for the last dish to be delivered. Here, he can order the dishes in any order he likes, and he can place an order already at time 0." And constraints: values between 1 and 123 inclusive.
Thus implement as described.
But let's double-check that we didn't misinterpret the rule: "a new order can be placed at the exact time when the dish is delivered." Could it be interpreted that if a dish is delivered at time T, even if T is not a multiple of 10, you can place the next order at that exact time T (overriding the multiple-of-10 rule)? The wording: "An order can only be placed at a time that is a multiple of 10 (time 0, 10, 20, ...)." This is a general rule. "No new order can be placed when an order is already placed and the dish is still not delivered, but a new order can be placed at the exact time when the dish is delivered." This clarifies that you can place a new order exactly at the moment of delivery (i.e., you are allowed to place a new order even though there was an order just delivered). It doesn't say that the multiple-of-10 restriction is lifted. So the exact time must also be a multiple of 10. In the typical solution for ABC123 B, they used the same logic we used. So it's consistent.
Thus answer is straightforward.
Edge cases: All numbers are multiples of 10: each penalty=0, total=sum, answer=sum. Works.
One more: Suppose we have times: 1,1,1,1,1. Then each penalty=9. Total_sum=5. Total_penalty=45. max_penalty=9. answer=5+45-9=41. Let's simulate: order four dishes, each takes 1 minute, but after each you have to wait 9 minutes to next multiple of 10. So order dish1 at 0, finish at 1, wait to 10, order dish2 at 10, finish at 11, wait to 20, order dish3 at 20, finish at 21, wait to 30, order dish4 at 30, finish at 31, wait to 40, order dish5 at 40, finish at 41. So answer 41. Good.
If we put the last dish with penalty 9 as last, we save 9: indeed we saved one waiting after the last. If we had put a dish with penalty 9 earlier, we would have added that waiting. So optimal.
Thus solution.
Implementation: read 5 ints. Compute sum_t = 0; penalties list. For each t: sum_t+=t; r = t % 10; if r == 0: penalty = 0 else penalty = 10 - r. sum_pen += penalty; max_pen = max(max_pen, penalty). Then ans = sum_t + sum_pen - max_pen. Print ans.
But careful: if max_pen is 0 (all penalties 0), then sum_pen = 0, ans = sum_t. That's fine.
Now, note that we must consider that if there is a dish with penalty 0, it's okay.
Complexity O(1).
Thus final answer.
But I'll double-check the logic by deriving formally:
Let order π = (π1, π2, π3, π4, π5). Define start times S_π1 = 0. For i=1..4, finish F_πi = S_πi + a_πi. Then S_π(i+1) = ceil(F_πi / 10) * 10. Then final delivery time = S_π5 + a_π5.
We have S_πi are multiples of 10. So F_πi = S_πi + a_πi ≡ a_πi (mod 10). So S_π(i+1) = F_πi + (10 - (a_πi mod 10)) mod 10. So the cumulative time after waiting is increased by that penalty. Then S_π5 = sum_{i=1}^{4} (a_πi + penalty_i). And final = S_π5 + a_π5 = sum_{i=1}^{5} a_πi + sum_{i=1}^{4} penalty_i. Since penalties depend only on a_πi mod 10, the total sum of penalties for the first four dishes is sum of penalties for the dishes chosen in positions 1-4. Therefore, to minimize, we choose which dish to put in position 5: the one with maximum penalty.
Alternatively, one could compute answer = sum_i (ceil(t_i / 10) * 10) - extra? But the above is simpler.
Now confirm that rounding up each dish to nearest 10 and summing, then subtract the largest "saving"? Another known formula: answer = sum_{i=1}^{5} ceil(t_i/10)*10 - (10 - min_remainder)??? Actually typical solution: sort by remainder descending; then total = sum of ceil(t_i/10)*10 for all i, but subtract something. Let's derive: ceil(t/10)*10 = t + penalty(t) where penalty(t) = (10 - t%10)%10. So sum ceil(t_i/10)*10 = total_sum + total_penalty. But we don't need to add penalty for the last dish because we don't need to wait after it. So subtract max_penalty (the penalty of the dish we put last). So answer = sum ceil(t_i/10)*10 - max_penalty. That's another expression.
Edge Cases: If all times are multiples of 10, penalties=0, max_penalty=0, answer = total_sum.
One more: If one dish has remainder 0, penalty 0, but maybe max_penalty is from another dish.
All good.
Now produce final answer.
But wait, could there be a scenario where we might not need to wait after a dish if the next dish's ordering can be at the exact delivery time even if that time is not a multiple of 10? The problem statement's third rule: "No new order can be placed when an order is already placed and the dish is still not delivered, but a new order can be placed at the exact time when the dish is delivered." This could be interpreted as: You are allowed to place a new order exactly at the moment of delivery, even if that moment is not a multiple of 10. However, the first rule says orders can only be placed at multiples of 10. Which rule takes precedence? Usually, both must hold: to place an order, the time must be a multiple of 10 AND you must not have an order pending (except at delivery). So the third rule doesn't override the first; it just clarifies that at the exact moment of delivery, the condition of "no order pending" is satisfied, so you can place if also multiple of 10. The typical interpretation in AtCoder problem B - Five Dishes is as we used. I recall the editorial: "Let f(x) = ((x+9)//10)*10. The answer is f(A)+f(B)+f(C)+f(D)+f(E) - max(f(x)-x)" or something. That is exactly what we derived. So we are fine.
Thus final answer.
Now code:
def solve():
import sys
times = [int(sys.stdin.readline()) for _ in range(5)]
total = 0
total_pen = 0
max_pen = 0
for t in times:
total += t
r = t % 10
if r == 0:
pen = 0
else:
pen = 10 - r
total_pen += pen
if pen > max_pen:
max_pen = pen
ans = total + total_pen - max_pen
print(ans)
Or using ceil: ans = sum((t+9)//10*10 for t in times) - max((10 - t%10)%10 for t in times) but careful: if t%10==0, (10 - t%10)%10 = 0, okay.
Simplify: ans = sum((t+9)//10*10 for t in times) - max((10 - t%10)%10 for t in times)
But our earlier formula: sum ceil(t/10)*10 - max_penalty. Yes.
But the user expects a complete answer: we should output the solution in Python. I'll provide explanation as well.
But note: This is a standard problem from AtCoder. The answer is straightforward.
Given the conversation, the assistant is to respond with final answer.
Thus final output: code in python with solve().
But ensure to handle reading from input and printing.
Will also add if name == "main": solve()
Now produce final message.
```python
import sys
def solve() -> None:
times = [int(sys.stdin.readline()) for _ in range(5)]
code
total = 0 # sum of all cooking times
penalty_sum = 0 # sum of all penalties
max_penalty = 0 # maximum penalty among the five dishes
for t in times:
total += t
r = t % 10
penalty = 0 if r == 0 else 10 - r
penalty_sum += penalty
if penalty > max_penalty:
max_penalty = penalty
ans = total + penalty_sum - max_penalty
print(ans)