We are given: Harry has N food items, each takes ti minutes to eat. After each dish, Harry needs a break of 10 minutes to digest. Joseph cracks jokes that each take exactly 5 minutes. Harry cannot eat at the same time as Joseph cracks jokes (i.e., they cannot overlap). The party lasts d minutes. We need to schedule eating and jokes such that Harry tastes all dishes (i.e., all eating and required digestion breaks must fit within the schedule) and Joseph cracks as many jokes as possible. If it's impossible to schedule all dishes (including digestion breaks) within d minutes, output -1. Otherwise output the maximum number of jokes.
Essentially, we have to interleave eating periods (ti for each item) and digestion breaks (10 minutes after each dish, except possibly after last dish? The statement: "after tasting each dish, Harry needs a break of 10 minutes to digest it." That implies after each dish, there must be a 10-minute break. For the last dish, does he need a break? The problem says "after tasting each dish" which would include the last dish. However, after the last dish, there may be no further activities, but presumably the break must still be taken? Usually, these scheduling problems consider that after the last dish, he may not need a break because the party ends, but we need to interpret carefully. For example, the sample: Input: 3 30, t: 2 2 1. Output: 5 jokes.
Let's compute: Total time needed for eating and mandatory breaks (if breaks are required after each dish including last) would be: sum(ti) + 10N. That's 2+2+1 =5, plus 30 = 35 minutes. But d=30, so that's already >30, impossible. But the output is 5, not -1. So clearly, after the last dish, the 10-minute break is not mandatory. Maybe the break is needed only between dishes? The phrase "after tasting each dish, Harry needs a break of 10 minutes to digest it." Typically, you need a break after eating each dish before moving on to next dish. So it's required between dishes, but after the last dish, there is no next dish, so no break needed. Similarly, maybe before the first dish, there is no break needed. So total mandatory time: sum(ti) + 10(N-1) (breaks between dishes). Check: 5 + 20 = 25 minutes needed for eating+digestion. d=30, leaving 5 minutes for jokes. That yields jokes: 5 minutes of jokes could be 1 joke (5 min) but output says 5 jokes? That's 5 jokes = 25 minutes, but then total would be 25+25=50 >30. Wait, something's off.
Sample 1: N=3, d=30, t=2,2,1. Output is 5. That's 5 jokes, each 5 minutes, so jokes total time = 25 minutes. The total time spent on eating and digestion is 5 + (N-1)*10 = 5 +20 =25. So total = 25+25 = 50 >30. That is impossible. So maybe the joke time can be overlapped with digestion? But the statement: "Joseph ... wants everyone’s attention when he cracks a joke, so Harry cannot eat at the same time." However, during digestion break, Harry is not eating. The digestion break is a break where he is not eating; could Joseph crack jokes then? The problem says: "The problem she faces is that after tasting each dish, Harry needs a break of 10 minutes to digest it. On the other hand, Joseph being a very active person doesn’t need any rest but he wants everyone’s attention when he cracks a joke, so Harry cannot eat at the same time." It doesn't say that jokes cannot occur during digestion. In fact, digestion is a break where Harry is not eating, so it's plausible that Joseph could crack jokes during those 10-minute digestion periods. Also, maybe the break is not a separate activity but just a time where Harry is not eating; it's not a period where Harry must be idle; he just can't eat. So jokes could be scheduled during digestion. So the digestion time can be used for jokes. But careful: The break is a necessity: after each dish, there must be a period of 10 minutes during which Harry does not eat (while digesting). However, these 10 minutes can be used for jokes. Similarly, perhaps before the first dish, there could be jokes? The schedule must include all dishes, but jokes can be placed at any time when Harry is not eating. The party lasts exactly d minutes, meaning the schedule must fill exactly d minutes? Or the party goes on for d minutes, i.e., the total time allocated is d. We need to schedule activities within d minutes. Harry must taste all dishes (including the required digestion breaks?). The digestion breaks are necessary, but they don't have to be separate from jokes; they can coincide with jokes if Joseph is cracking jokes (since Harry cannot eat anyway). So essentially, we have mandatory periods: eating of ti minutes for each dish, which must be done sequentially (I assume order can be arbitrary, but we can reorder dishes? Probably we can schedule dishes in any order, but the breaks are required after each dish except maybe after last). During the 10-minute digestion after a dish, Harry cannot eat, but Joseph can crack jokes. Also, there could be additional joke time when Harry is not eating, i.e., before the first dish, after the last dish (no break required but jokes can be placed), and possibly between dishes if the digestion period is longer than 10 minutes? The digestion is exactly 10 minutes? "after tasting each dish, Harry needs a break of 10 minutes to digest it." So the break is exactly 10 minutes? Or at least 10 minutes? Usually, such problems: needs a break of exactly 10 minutes, i.e., he must have 10 minutes of not eating after each dish. So it's a mandatory no-eating period of length 10 after each dish (except perhaps after last, but after last there's no requirement). But if jokes are done during that period, they count as jokes, but they also satisfy the break requirement because Harry is not eating. So we can effectively treat the 10-minute break as a slot where no eating occurs, but it can be filled with jokes (or idle time). Similarly, the party length is fixed d; we want to maximize jokes. Jokes take 5 minutes each, and cannot overlap with eating. They can overlap with digestion breaks. Also, we may insert extra idle time if needed, but that would waste time and reduce jokes, so we wouldn't do that in an optimal schedule if we want to maximize jokes.
Thus, the problem reduces to: We have N eating intervals of lengths t_i. Between consecutive eating intervals, there must be a gap of at least 10 minutes (since digestion break). Additionally, we can have gaps before the first eating and after the last eating. Jokes can be placed in any gaps (including the mandatory 10-minute gaps) as long as each joke occupies a 5-minute slot. The total time from start of first eating to end of last eating plus gaps (including the mandatory minimum gaps) must not exceed d. We want to maximize the number of jokes (each 5 min) that can be placed in the schedule.
But wait: The party duration d is the total time the party lasts. The schedule should cover exactly d minutes? Or can it be less? Usually, the party can go on for d minutes, meaning that the activities must fit within d minutes; they can be exactly d or less, but if less, maybe there's idle time? But we aim to maximize jokes, so we would fill any remaining time with jokes (since Joseph wants as many jokes as possible). So we want to schedule eating and required breaks in a way that minimizes the total time used for mandatory activities, leaving as much remaining time as possible for jokes. However, there might be constraints that we cannot arbitrarily order the eating; but order can be chosen? The problem does not specify order; we can choose the order of dishes. Since the t_i values differ, we may want to arrange them to minimize the total mandatory time? The mandatory time consists of sum(t_i) + (N-1)10 (the minimum gaps between dishes). That is independent of order; sum(t_i) is fixed, and number of breaks (N-1) is fixed, each at least 10 minutes, so the minimal total time for eating+digestion is sum(t_i) + 10(N-1). But is that minimal? Could we sometimes make the gaps longer? That would increase total time, leaving less time for jokes, so we would not want that. So to maximize jokes, we want to use exactly the minimum required gaps (10 minutes each). Also, we might be able to start the first dish at time 0, and end last dish at time sum(t_i) + 10*(N-1). Then the remaining time (d - that) can be filled with jokes. But jokes can also be placed during the gaps? Wait, if we set gaps exactly to 10 minutes, then those 10 minutes are not "free" for jokes? They are part of the mandatory break, but jokes can be done during those 10 minutes, because Joseph can crack jokes during digestion. So jokes are not extra time beyond the breaks; they can occupy the break time. So the total time used for eating+digestion+jokes is not simply sum(t_i)+10*(N-1)+5*j; rather, the jokes can be interleaved within the breaks and possibly before/after. The total time must be <= d. In effect, we have to allocate time for eating (which cannot have jokes) and allocate time for digestion breaks (which must be at least 10 minutes each, but during which jokes can happen). Additionally, we can allocate extra time that is not eating and not part of digestion, which can also be used for jokes. However, any time not spent eating can be used for jokes, subject to the constraint that after each eating period there is at least 10 consecutive minutes of not eating? Actually, the digestion break: after each dish, Harry needs a break of 10 minutes. That likely means that after finishing a dish, there must be a period of at least 10 minutes before he starts the next dish. It doesn't require that the break is contiguous? Usually, a break of 10 minutes means a continuous 10-minute period. But could we split it? For example, could we have 5 minutes of jokes, then 5 minutes of idle, etc. As long as it's a period of 10 minutes where he is not eating. And jokes can be inside that period. So the break is a contiguous block of 10 minutes? The phrasing "needs a break of 10 minutes to digest it" suggests that he must have 10 minutes of digestion, presumably continuous. Could it be non-contiguous? Probably not; it's a continuous break. So we must have a continuous gap of 10 minutes after each dish before the next dish starts. During that gap, Joseph can tell jokes. So the gap is exactly 10 minutes? Could it be longer? The problem says "needs a break of 10 minutes". That implies exactly 10 minutes. Or could it be at least 10? Usually, if someone needs a break, they need at least that much. But in scheduling, we could give longer breaks, but that would waste time. So we'll assume exactly 10 minutes required. But we should verify with sample.
Sample 1: N=3, d=30, t = [2,2,1]. Sum(t)=5, mandatory breaks (between dishes) = 210=20, total mandatory = 25. Now if we have to set exactly 10 minute breaks, then we have 25 minutes of eating+break. The remaining 5 minutes could be used for jokes, but jokes could also be placed during the breaks. The breaks are 10 minutes each, so we could place jokes inside these 10-minute periods. So total joke time possible = the total time not spent eating. That is d - total_eating_time? Actually, total time d is fixed. The schedule must occupy exactly d minutes? Possibly we can finish earlier, but then there would be idle time? But we want to maximize jokes, so we would fill any unused time with jokes, but jokes cannot overlap with eating. So the total time spent on eating is sum(t_i) = 5 minutes. The rest of the time, d - 5 = 25 minutes, can be used for jokes or digestion. But we also have the constraint that after each dish, there must be a contiguous block of at least 10 minutes of digestion (non-eating). However, if we fill these digestion periods with jokes, that's fine. So the total non-eating time is d - sum(t_i) = 25 minutes. This non-eating time must be partitioned into N breaks? Actually, there are N dishes, so there are N "after-dish" periods? After the last dish, there is no required break, but we can have time after last dish. Also before the first dish, there may be time. The requirement is that for each dish except possibly the last, there must be a contiguous 10-minute break immediately after finishing that dish (i.e., before starting next dish). So the non-eating time must be allocated such that between eating intervals, there is at least a continuous 10-minute block dedicated as break. Also, there might be extra non-eating time before the first dish or after the last dish, which can be used for jokes without restrictions (they are just free time). But those extra times do not need to be contiguous with breaks. So essentially, we have a timeline from 0 to d. We place N eating intervals of lengths t_i in order (some permutation). Between consecutive intervals, we must have a gap of exactly (or at least) 10 minutes. Additionally, there can be gaps at the start and end. Jokes can occupy any time not used for eating, i.e., all gaps. So the total non-eating time is d - sum(t_i). This total non-eating time must be at least 10(N-1) to satisfy the minimum break requirements. If it's less, impossible. If it's equal or greater, we can allocate exactly 10 minutes between each consecutive pair, and the remaining non-eating time can be distributed as extra gaps (before first, after last, or extra time inserted between dishes beyond the 10 minutes). But we want to maximize jokes. Since jokes occupy 5-minute units, and they can be placed in any non-eating time, the maximum number of jokes is essentially floor( (total non-eating time) / 5 ), because we can fill all non-eating time with jokes (assuming we can break jokes into 5-min segments and they don't interfere with the break continuity requirement). But wait: if we fill the non-eating time with jokes, does that violate the break requirement that the break must be a continuous period of digestion? It doesn't say digestion cannot have jokes. So presumably jokes are allowed during the break. So the break can be filled with jokes; it remains a continuous period of 10 minutes where Harry is not eating. So as long as we allocate at least 10 minutes between consecutive eating intervals, we can fill that gap with jokes, and also any extra time elsewhere with jokes. So the maximum number of jokes is simply floor( (d - sum(t_i)) / 5 ), provided that d - sum(t_i) >= 10*(N-1). Because we need at least 10*(N-1) minutes of gaps to place between dishes. However, we must also consider that if we have extra time beyond the minimum gaps, we can distribute it anywhere and fill with jokes. So the condition for feasibility is: d >= sum(t_i) + 10*(N-1). If that holds, then we can schedule: place eating intervals in some order with exactly 10-minute gaps between them. That uses sum(t_i) + 10*(N-1) minutes. The remaining time R = d - (sum(t_i) + 10*(N-1)) can be allocated as extra gaps (before, after, or between dishes). In all that extra time, we can also tell jokes. So total non-eating time = 10*(N-1) + R = d - sum(t_i). So indeed, all non-eating time can be used for jokes. So maximum jokes = floor( (d - sum(t_i)) / 5 ). But check sample: d=30, sum(t_i)=5, so d - sum =25, floor(25/5)=5. Output is 5. That matches.
Second sample: N=3, d=20, t=2,1,1 => sum=4, d - sum=16, floor(16/5)=3. But output is -1. Why? Check condition: sum(t_i) + 10*(N-1) = 4 + 20 =24 > d=20, so infeasible. So output -1. Indeed, condition fails. So that aligns.
Thus the problem appears straightforward: compute total_eat = sum(t_i). min_required = total_eat + 10*(N-1). If d < min_required, output -1. Else output floor( (d - total_eat) / 5 ). Because the total time available for jokes is all non-eating time. But careful: Is it possible that we could schedule jokes in such a way that we can satisfy break requirements even if total non-eating time is less than 10*(N-1)? No, because each break must be at least 10 minutes, and they are distinct intervals (non-overlapping) between dishes. So the total non-eating time must be at least 10*(N-1). That's necessary. So condition as above.
But wait: Could we reorder dishes to reduce the need for breaks? No, breaks are after each dish except possibly last. So N-1 breaks regardless of order.
Edge case: N=1. Then there are no required breaks (since no dish after). So min_required = total_eat. Condition: d >= total_eat. Then jokes = floor((d - total_eat)/5). That seems plausible.
Also, note: The jokes are of exactly 5 minutes each. Joseph wants to crack as many jokes as possible. He can crack jokes during any non-eating time. So maximum is floor of available non-eating time divided by 5.
Thus the solution seems trivial. But wait, there might be nuance: The digestion break after each dish must be exactly 10 minutes, not just at least? If exactly, then we cannot have extra time inserted between dishes; the breaks must be exactly 10 minutes. Then the non-eating time is forced to be exactly 10*(N-1) plus any time before first or after last. Then total non-eating time = d - sum(t_i) must be at least 10*(N-1). But if it's exactly 10*(N-1), then we have exactly those breaks, no extra. That's fine. But if it's more, we need to allocate extra time somewhere, but we cannot extend the breaks beyond 10 minutes if they must be exactly 10. So we would have to put extra time before first or after last. That is allowed. So still total jokes = floor((d - sum(t_i))/5), as long as condition d - sum(t_i) >= 10*(N-1). Because we can set breaks exactly 10, and put remaining time as extra gaps, which can be filled with jokes. So same.
Now, check constraints: T up to 100, N up to 100, d up to 10000, ti up to 200. So sums are manageable.
But is there any scenario where we could schedule jokes overlapping with eating? No, they cannot.
Thus answer: For each test case, compute S = sum(t_i). If d < S + 10*(N-1), print -1. Else, print (d - S) // 5.
However, before finalizing, let's consider whether there might be a scenario where we could interleave jokes in such a way that reduces total required break time? For example, could we start a joke before finishing a dish and continue after? No, since Harry cannot eat while jokes are told; the joke time must be disjoint from eating. So no.
Also, consider the possibility that jokes could be told during the digestion break, but what if we can split the break? The break is required to be contiguous, but if we insert jokes, it's still contiguous non-eating. So fine.
Thus solution is simple.
But wait, the problem statement says: "The problem she faces is that after tasting each dish, Harry needs a break of 10 minutes to digest it." Possibly it means that after each dish, there must be a break of exactly 10 minutes where Harry does nothing (maybe not even listening to jokes?) But it says "Joseph ... wants everyone’s attention when he cracks a joke, so Harry cannot eat at the same time." It doesn't say Harry cannot listen while digesting. So it's okay.
Let's check more examples: Suppose N=2, d=30, t=[10,10]. Sum=20, min_required=20+10=30. So d - sum=10, floor(10/5)=2. Condition holds (d=30 exactly). So max jokes=2. Let's try to schedule: Dish1 10m, then break 10m, then dish2 10m. That totals 30m. During the break, we can tell 2 jokes? But each joke is 5 minutes, so 2 jokes fill 10 minutes exactly. So yes. Jokes=2.
If we had extra time, e.g., d=35, sum=20, d-sum=15, floor(15/5)=3. Condition holds because min=30 <=35. We schedule: dish1 10, break exactly 10 (jokes), dish2 10, then extra 5 minutes after last dish (or before first) for another joke. So total jokes=3.
Everything consistent.
Thus answer is straightforward.
But is there any trick? Possibly the order of dishes could be chosen to allow more jokes? Since jokes are fixed-length, total non-eating time is fixed (d - sum(t_i)). The only constraint is that we must allocate at least 10 minutes between consecutive dishes. If d - sum(t_i) is exactly 10*(N-1), then we must allocate exactly those minutes as breaks and no extra. If d - sum(t_i) > 10*(N-1), we have extra time. That extra time can be placed anywhere, and can be used for jokes. So the number of jokes is determined solely by total non-eating time, as long as the breaks can be arranged such that they are contiguous. Since we can always put extra time at the ends, it's always possible to schedule.
But what about the possibility that we could use the extra time to extend some breaks beyond 10 minutes, but still it's non-eating and can host jokes. So same total.
Thus answer holds.
Thus implement.
But wait, what about the case where d - sum(t_i) is not a multiple of 5? We output floor division. The problem asks for maximum number of jokes, which must be integer. So floor.
Now, one more nuance: Could jokes be cracked during eating if Harry stops eating to listen? No, because "Harry cannot eat at the same time." So they cannot overlap. So no.
Thus solution.
We should also consider that maybe the break must be exactly 10 minutes and must be used solely for digestion, not for jokes? The problem didn't forbid jokes during digestion. Usually these problems allow overlapping. So likely fine.
Given the simplicity, I'm surprised it's a contest problem. But it's plausible as an easy one.
Let's double-check sample: Input:
2
3 30
2 2 1
3 20
2 1 1
Output: 5 and -1.
Matches.
Now, test edge: N=1, d=5, t=5 -> sum=5, min_required=5 <=5 -> d-sum=0 -> jokes=0. OK.
N=1, d=10, t=5 -> d-sum=5 -> jokes=1. Schedule: eat 5, then 5 minutes joke after. Works.
N=2, d=14, t=1,1 -> sum=2, min_required=2+10=12 <=14 -> d-sum=12 -> jokes= floor(12/5)=2. That yields 12 minutes for jokes, but we have only 14 total, 2 for eating, so jokes time 12 -> total 14. But we need at least 10 minutes break between dishes. So schedule: eat 1, break 10 (jokes: two 5-min jokes fill 10), eat 1, then extra 2 minutes? Wait, d-sum=12, so total non-eating=12. But we must allocate 10 minutes between dishes as break, leaving 2 extra minutes. So we can put those 2 minutes either before first or after last. But jokes require 5 minutes each. If we have 12 minutes for jokes, that's 2 full jokes (10 min) plus 2 leftover minutes that cannot fit a full joke. But floor(12/5)=2, so we can have 2 jokes. We'll have 10 minutes of jokes during the break, and then 2 minutes idle (or could be used for partial joke? No, jokes are exactly 5 minutes, so can't have partial). So schedule: eat dish1 (1), then break (10) with two jokes, then dish2 (1), then 2 minutes of idle (or maybe a joke that is cut short? Not allowed). So total time =1+10+1+2=14, jokes=2. Works. Could we instead use the extra 2 minutes to extend the break to 12? But then we would have break of 12 minutes, still need only 10 min break. That's okay; then we could have 2 jokes (10 min) and 2 minutes idle within break. But maximum jokes is still 2.
Thus answer seems correct.
Therefore, the solution: For each test case, compute total = sum(ti). required = total + 10*(N-1). If required > d: print -1. Else: print (d - total) // 5.
But careful: d and total are integers; (d - total) is integer. So integer division.
Now, implement in code.
Check constraints: up to T=100, N=100, d up to 10000, ti up to 200, sum(ti) up to 20000? Actually N*200 = 20000, d up to 10000, so sum(ti) could exceed d. That's fine; if sum(ti) > d, then required > d anyway, so -1.
Edge: When d - total is negative? That would be sum > d, condition fails, so -1.
Thus code:
def solve():
import sys
data = sys.stdin.read().split()
it = iter(data)
T = int(next(it))
out = []
for _ in range(T):
N = int(next(it)); d = int(next(it))
t = [int(next(it)) for __ in range(N)]
total = sum(t)
if total + 10*(N-1) > d:
out.append("-1")
else:
jokes = (d - total) // 5
out.append(str(jokes))
sys.stdout.write("\n".join(out))
But wait: There's a nuance: The break of 10 minutes is after each dish, but if we schedule jokes during the break, can we fit a fractional joke? No.
All good.
Now, I should consider if there is any scenario where we could schedule such that we need less than 10*(N-1) break time by, say, having the break after a dish overlap with the break before the next? Not possible.
Thus solution stands.
But before finalizing, let's think about the possibility that the breaks are not mandatory contiguous but could be split. For example, after a dish, Harry needs to digest for total of 10 minutes, but could be interspersed with eating? No, digestion requires not eating, so he cannot eat during those 10 minutes. So he must have at least 10 minutes of non-eating after each dish before starting the next. Could those 10 minutes be split into multiple non-contiguous segments with short eating in between? That would contradict the notion of "break to digest". Usually, digestion break is a continuous period. So it's continuous.
Thus answer.
I will now provide the final answer with explanation.
But I'm ChatGPT, I need to output in final channel. Let's produce a well-explained solution.
Also, note that the input sample shows "Output: 5" and "-1". Good.
Thus final answer. The solution is straightforward. Let (S = \sum_{i=1}^{N} t_i) be the total time required to eat all dishes. After each dish (except the last) Harry needs a continuous break of exactly 10 minutes. Therefore the minimum time needed for eating and digestion is
[
S + 10 \cdot (N-1).
]
If the party duration (d) is less than this minimum, scheduling is impossible → output (-1).
Otherwise, the total time available for activities other than eating is (d - S). During any non‑eating time (including the mandatory 10‑minute breaks and any extra gaps before the first dish or after the last one) Joseph can crack jokes. Each joke lasts exactly 5 minutes, and jokes cannot overlap with eating. Hence the maximum number of jokes is the integer part of ((d - S) / 5).
Complexity: Each test case runs in (O(N)) time, which is easily within the limits.
import sys
def solve() -> None:
data = sys.stdin.read().split()
it = iter(data)
t = int(next(it))
out_lines = []
for _ in range(t):
n = int(next(it))
d = int(next(it))
times = [int(next(it)) for _ in range(n)]
total_eat = sum(times)
if total_eat + 10 * (n - 1) > d:
out_lines.append("-1")
else:
jokes = (d - total_eat) // 5
out_lines.append(str(jokes))
sys.stdout.write("\n".join(out_lines))
if __name__ == "__main__":
solve()