Jou and Yae are a good couple. Jou is collecting prizes for capsule toy vending machines (Gachapon), and even when they go out together, when they find Gachapon, they seem to get so hot that they try it several times. Yae was just looking at Jou, who looked happy, but decided to give him a Gachapon prize for his upcoming birthday present. Yae wasn't very interested in Gachapon itself, but hopefully he would like a match with Jou.
For Gachapon that Yae wants to try, one prize will be given in one challenge. You can see how many types of prizes there are, including those that are out of stock, and how many of each prize remains. However, I don't know which prize will be given in one challenge. Therefore, regardless of the order in which the prizes are given, create a program that outputs the minimum number of challenges required for Yae to get two of the same prizes.
input
The input consists of multiple datasets. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
N
k1 k2 ... kN
Each dataset has two lines, and the first line is given the integer N (1 ≤ N ≤ 10000), which indicates how many types of prizes there are. The next line is given the integer ki (0 ≤ ki ≤ 10000), which indicates how many prizes are left.
The number of datasets does not exceed 100.
output
For each dataset, the minimum number of challenges required to get two identical prizes is output. However, if it is not possible, NA is output.
Example
Input
2
3 2
3
0 1 1
1
1000
0
Output
3
NA
2
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 are asked: given N types, with remaining counts k_i (>=0). We want to compute the minimum number of draws (challenges) needed to guarantee that we have two identical prizes? Or is it the worst-case minimum? Let's read carefully:
The problem statement: "Create a program that outputs the minimum number of challenges required for Yae to get two of the same prizes." It seems ambiguous: Is it the minimum number of draws required in the worst case? Or is it the expected? The phrasing "minimum number of challenges required" suggests we are to consider the worst-case scenario given that we don't know which prize will be given in one challenge, and "regardless of the order in which the prizes are given" might indicate that we consider any possible sequence of draws. So we want the smallest number m such that in any sequence of m draws (each draw picks a prize type according to the remaining stock? Actually, the machine dispenses a random prize among those still in stock? But the problem says "I don't know which prize will be given in one challenge. Therefore, regardless of the order in which the prizes are given, create a program that outputs the minimum number of challenges required for Yae to get two of the same prizes." This sounds like we want to guarantee that she gets two identical prizes, assuming an adversarial order of draws? Usually, a problem like "minimum number of draws to guarantee a pair" given counts is like: If we want to guarantee two of the same type, we must consider the worst-case scenario where we try to avoid getting a pair as long as possible. That is the pigeonhole principle: If you have N types, with each type having a certain number of copies, the worst-case to get a duplicate is to draw one from each type until you can't avoid duplication. But here counts may be limited: some types may have 0 items left, meaning they are out of stock and cannot be drawn. So the worst-case scenario to avoid getting two same prizes is to draw as many distinct types as possible. Since each type can be drawn at most its count, the maximum number of draws without having two identical is the sum over types of min(k_i, 1)? Actually, if a type has count at least 1, you can draw one of that type without duplication. But if a type has count >=2, you could theoretically draw multiple of that type, but if you draw two of the same type, you already have a duplicate, so to avoid duplication, you can only draw at most one from each type regardless of its stock (since drawing two from the same type would give a duplicate). So the maximum number of draws you can make while still having all distinct prizes is simply the number of types that have at least one remaining prize. Because you can draw one from each such type. However, there's also possibility that some types have 0 items, so they are not available. So let M be the number of types with k_i > 0. Then in the worst case, you could draw M distinct prizes, each of a different type, and the next draw (if there are still any items left of some types) would force a duplicate? But wait: After drawing one from each type, there may still be remaining items of some types. But if you draw an additional item, it must be from one of the types you've already drawn (since all types with remaining items have already been drawn). However, it's possible that after drawing one from each type, there are no remaining items left at all? For example, if all k_i = 1, then M draws give one each, and then there are no items left to draw. In that case, you cannot get two identical prizes at all. So the answer would be "NA" (not achievable). If there is at least one type with k_i >= 2, then after drawing one from each type, you can draw one more from any type with at least 2, which will duplicate. But the worst-case to get a duplicate is M+1 draws: first draw M distinct, then any further draw (which must be from some type already drawn) yields duplicate. However, what if some types have count >=1 but some have count 0? The maximum distinct draws is the number of types with at least 1. So the minimum number of challenges required to guarantee two identical (in worst-case) is M+1, provided that there exists at least one type with count >=2, because then after M distinct draws you still have at least one item left (since total items left after M draws is sum k_i - M. If any type has k_i >=2, then sum k_i >= M+ (something). Actually after drawing one from each type, we have drawn M items. The total number of items initially is S = sum k_i. For a duplicate to be possible, we need to be able to draw M+1 items? But if S >= M+1, then there exists at least one type with k_i >=2? Not exactly. Consider M=3, with counts 1,1,1, total S=3. Then M+1=4 > S, so you cannot draw 4 items because there are only 3 items total. So it's not possible to get a duplicate. So to guarantee a duplicate, we need that after drawing M distinct, there is still at least one item left to draw. That requires S > M. Equivalently, there exists at least one type with k_i >=2, because if all non-zero types have exactly 1, then S = M, and no duplicates are possible. If any type has k_i >=2, then S >= M+1? Let's check: M counts types with k_i>0. If at least one type has k_i >=2, then sum k_i >= 1*(M-1) + 2 = M+1? Actually if there is exactly one type with >=2 and the others have exactly 1, then total = (M-1)*1 + >=2 = M+ (>=1). So S >= M+1. So yes. So condition: if any k_i >=2, then answer is M+1? But is that always the minimal number to guarantee a duplicate? Let's consider a scenario: types: [2,0,0,...]. N=1, k1=2. M=1 (since only one type with >0). The maximum distinct draws: you can only draw one distinct type. So worst-case to get duplicate: you draw one item of type1 (distinct set size 1), then the next draw must be from type1 (since only type) yields duplicate. So number of draws needed: 2. M+1=2. That matches.
But consider another scenario: N=2, counts [3,1]. M=2 (both have at least 1). Worst-case to avoid duplicate: you draw one from type A, one from type B (2 draws, both distinct). Now you have drawn both types. The next draw, you must draw either from type A or type B. Both would produce a duplicate (since you already have one of each). So in worst-case, you need 3 draws to guarantee duplicate. M+1=3. But is it possible that you could get duplicate earlier? In some sequences, you might draw two type A in first two draws, giving duplicate in 2 draws. But we want the minimum number such that no matter the order (adversarial), you will have two of the same? Actually the phrase "minimum number of challenges required for Yae to get two of the same prizes" could be interpreted as: Given the counts, what is the smallest m such that in any possible sequence of draws (i.e., regardless of order), by the m-th draw you are guaranteed to have at least two identical prizes? That is the typical "pigeonhole principle" worst-case guarantee. So indeed it is M+1 if S > M; else NA.
But is there any nuance? For instance, if some types have count 0, they are not available, so the worst-case distinct draws cannot include them. So M is number of types with ki > 0.
Second:
3
0 1 1
N=3, counts 0,1,1 => types with >0: two (the ones with 1). M=2, S=2 => S == M? Actually S = 0+1+1=2, M=2 => S == M, no type with >=2. So duplicate impossible => NA. Sample output NA. Good.
Thus answer is straightforward: For each dataset, compute M = count of i such that k_i > 0. If any k_i >= 2, then output M+1, else output "NA". But careful: condition for duplicate possible: we need to be able to get two identical, which requires there exists some i with k_i >= 2. However, if there is at least one such, then M+1 is valid? Check: if there is a type with k_i >= 2, then S >= M+1, so you can indeed draw M+1 items? But what if total items S is less than M+1? Could that happen with a type having >=2? Let's try: Suppose N=2, counts [2,0]. M=1 (since only type1 >0). S=2. M+1=2 <= S, fine. Suppose N=3, counts [2,0,0] M=1, S=2, M+1=2 <=2. Suppose N=3, counts [2,1,0] M=2, S=3, M+1=3 <=3. Suppose N=3, counts [2,1,1] M=3, S=4, M+1=4. Works. So indeed if any ki>=2, then S >= M+1? Prove: Let T be set of types with ki>0. M = |T|. If at least one type in T has ki>=2, then sum_{i in T} ki >= 1*(M-1) + 2 = M+1. So S >= M+1. So drawing M+1 items is possible. So answer M+1 is feasible. So condition for possible is existence of a type with ki >= 2.
But wait: Could there be a scenario where duplicate is possible but M+1 is not enough? Consider: In worst-case, we want to guarantee that after m draws, we have two of same. The adversary will try to delay duplication as long as possible. The maximum number of draws without duplication is at most M, because after drawing one from each type, the next draw necessarily duplicates if there is any item left. However, could the adversary avoid duplication even after M draws if there are no items left? That's the NA case. So the worst-case minimal number to guarantee duplicate is exactly M+1 if there is at least one item left after drawing M distinct. So it's correct.
Thus algorithm: For each dataset, read N, then read list of ki. Compute M = number of positive ki. Also check if any ki >= 2. If yes, output M+1, else output "NA".
But careful: Input format: "The input consists of multiple datasets. The end of the input is indicated by a single line of zeros." That line of zeros: Is it just a line containing "0"? Probably. So we read until N=0. Also note that there could be multiple spaces? We'll parse accordingly.
Constraints: N up to 10000, ki up to 10000, number of datasets <=100. So simple.
But wait: Let's double-check sample: They gave first dataset: "2\n3 2\n" => N=2, counts 3 2 -> M=2, has ki>=2 => output 3. Second: "3\n0 1 1\n" -> M=2, all ki <=1, so output NA. Third: "1\n1000\n" -> M=1, has ki>=2 => output 2. Good.
But is there any catch? Could there be a case where some ki are zero, but we need to consider the number of types N, but M is only count of non-zero. Indeed, types with zero cannot be drawn, so they don't contribute to distinct draws.
Thus solution is straightforward.
However, I'm thinking: Is there any alternative interpretation like the expected minimum number? No, because sample matches the pigeonhole.
Thus answer is to compute the number of types that have at least one copy, call it t, and if any type has at least two copies, answer is t+1, else NA.
But one subtlety: The problem says: "regardless of the order in which the prizes are given, create a program that outputs the minimum number of challenges required for Yae to get two of the same prizes." This suggests that the order is unknown (adversarial), so the minimum number required to guarantee success is t+1. So it's correct.
Edge Cases:
N=1, k1=0: M=0, no types with >0, and no type with >=2. Then duplicate impossible. Output NA? But input constraint says N>=1, and ki can be 0. If all ki=0, then there are no prizes at all. Obviously cannot get two same. Output NA. But would such dataset appear? Possibly. Our logic: M=0, any ki>=2? No. So output NA. That seems correct.
N=1, k1=1: M=1, no ki>=2, output NA. Yes, can't get duplicate.
N=10000, many types, many with high counts.
Implementation: We'll read lines, parse ints. For each dataset, read N, if N==0 break, then read the next line containing N integers. Possibly the line might be split across multiple lines? Usually not, but to be safe, we might read tokens until we have N integers, as input may have line breaks. But problem statement says "The next line is given the integer ki" implying one line with N numbers. We'll just read a line and split. However, for safety, we could read tokens regardless of line breaks. Since we are writing in a language like Python, we can use sys.stdin.read and parse all ints, then process sequentially: read N, if 0 break, then read next N ints. That is simplest.
Complexity: O(total numbers) which is up to 100 * 10000 = 1e6, fine.
So solution in Python:
import sys
def main():
data = list(map(int, sys.stdin.read().split()))
idx = 0
out = []
while idx < len(data):
N = data[idx]; idx+=1
if N == 0:
break
# read next N ints
if idx + N > len(data):
break # error, but assume valid
k = data[idx:idx+N]; idx+=N
M = sum(1 for x in k if x > 0)
has_dup = any(x >= 2 for x in k)
if has_dup:
out.append(str(M+1))
else:
out.append("NA")
sys.stdout.write("\n".join(out))
if name == "main":
main()
That's it.
But is there any nuance regarding "minimum number of challenges required" meaning the smallest number of draws such that no matter what, you'll have two identical? Yes.
However, sometimes there is a concept of "coupon collector" or "birthday problem" but the phrasing "regardless of the order" indicates worst-case.
Double-check sample: It matches.
Thus answer is solved.
But maybe there is another twist: The machine might run out of stock? But we are drawing without replacement? Yes, each prize is taken out. So after each draw, the remaining counts decrease. The worst-case to avoid duplicate is to draw as many distinct types as possible. That indeed yields M distinct if we pick one from each type that has at least one. But note that if a type has count 1, we can draw it only once. So after M draws, we might have exhausted some types? Actually if all types have exactly count 1, then after M draws, all items are exhausted, so no further draws possible. So duplicate impossible. If there exists a type with count >=2, then after drawing one from each type, there is at least one remaining item, which will be from a type already drawn (since all types with any remaining items are already drawn). So the next draw forces duplicate. So the worst-case number of draws to guarantee duplicate is M+1.
But is it possible that the worst-case could require more than M+1? Could the adversary, by clever choice of draws, avoid duplicate even after M+1 draws? After M draws, we have one of each type. There is at least one type with count >=2 originally. That type still has at least one item left because we only took one from it. So there is at least one item left of that type. So the next draw, if the machine gives us that item, we get duplicate. But what if the machine's order is not adversarial but random? The phrase "regardless of the order" suggests that we consider all possible orders, so the worst-case is that the order is as unfavorable as possible. However, could there be an order that yields no duplicate even after M+1 draws? For that, we would need to have drawn M+1 distinct items, but that is impossible because there are only M distinct types with any items. Unless some type had count 0, but no. So after M+1 draws, by pigeonhole, at least one type appears twice. So indeed, any sequence of M+1 draws will contain a duplicate. But is it guaranteed that we can make M+1 draws? That requires that total items >= M+1, which we already established if any ki>=2. So the worst-case to avoid duplicate is M draws; the duplicate must appear by M+1 draws. So the guarantee is M+1.
But wait: The phrase "minimum number of challenges required for Yae to get two of the same prizes." If we interpret as: "Given that Yae wants to guarantee that she obtains two identical prizes, what is the minimal number of draws she needs to perform?" That is indeed M+1.
Thus solution stands.
But maybe the problem intends a different interpretation: Yae wants to know the minimum number of challenges she needs to perform to actually get two identical prizes, assuming she is lucky? That would be 2 (if she gets same in first two draws) but that's not what sample shows. Sample: first dataset: N=2, counts 3 2. Minimum possible draws to get two identical could be 2 (if she draws two of the same type in the first two). But they output 3. So it's not about best-case; it's about worst-case or guarantee.
Hence our interpretation is correct.
Let's double-check: The sample: second dataset: 0 1 1. Could she ever get two identical? No. So NA. Third: 1000. Minimum possible to get two identical is 2 (if she draws same type twice). But they output 2. Wait sample output third: "2". That matches both best-case and worst-case? For a single type with many copies, the worst-case is also 2, because after first draw, the only type is drawn; second draw must be same type. So worst-case = 2. But first dataset: best-case could be 2, but they output 3. So they are outputting worst-case guarantee.
Thus solution is correct.
Hence we proceed.
But let's think about a corner: N=2, counts [2,2] => M=2, has >=2 => output 3. Worst-case: draw A, then B (distinct), then third draw either A or B yields duplicate. So 3. Best-case could be 2 if draw two As or two Bs. So output 3.
All good.
Implementation: We'll just follow above.
But note: input might have extra spaces or line breaks; using read().split() is fine.
Complexities: O(total tokens).
We should output each answer on separate line.
Thus solution is trivial.
But wait: There's nuance: "You can see how many types of prizes there are, including those that are out of stock, and how many of each prize remains." So types with zero are out of stock, and you know that. So indeed M is number of types that are not out of stock.
Thus answer.
But maybe the problem expects a different condition: the minimum number such that no matter the order, you will have at least two identical. That is exactly M+1 if there is at least one type with at least 2 items. But is there any scenario where M+1 is not sufficient because some types have zero but we might think they are distinct? No.
Thus I'm confident.
Let's implement.
But I want to ensure that reading from stdin works for large inputs.
Now, we need to output "NA" exactly as uppercase.
Edge Cases: When N=0, we stop reading; but what about trailing data? Usually there is only one zero line.
Thus the Python code as above is fine.
But we could also solve in C++ if needed, but Python is accepted.
Now I'll produce final answer.
But wait: The problem statement might be from some contest; sometimes they have trick: "minimum number of challenges required" might be interpreted as "the smallest m such that for any possible sequence of draws, Yae will have two identical by the m-th draw." That's what we did.
Thus final answer: compute t = number of i with ki > 0; if max(ki) >= 2 then print t+1 else print NA.
But caution: what if there are types with ki=0? They don't count.
But what about the case where there is only one type with ki>=2 but all other types have 0? Then t = 1, answer = 2. That's fine.
All good.
Now, before finalizing, let's double-check the sample with our logic:
But the problem might have hidden complexities: For example, if there are types with large counts, but we might consider drawing duplicates after exhausting some types? No.
Thus answer is simple.
Given the problem's constraints, the solution is O(N) per dataset.
Now, I will write explanation and code.
But note: The input format says each dataset has two lines: first N, then second line with N integers. But we should handle the case where the second line may be broken across lines? Probably not, but it's safer to read N integers regardless of line breaks. Our token-based approach handles that.
Thus final code:
import sys
def solve():
it = iter(map(int, sys.stdin.read().split()))
out_lines = []
for n in it:
if n == 0:
break
ks = [next(it) for _ in range(n)]
types = sum(1 for k in ks if k > 0)
if any(k >= 2 for k in ks):
out_lines.append(str(types + 1))
else:
out_lines.append("NA")
sys.stdout.write("\n".join(out_lines))
if name == "main":
solve()
That's it.
Now, think about possible pitfalls: The number of datasets up to 100, N up to 10000, so reading all ints at once is fine.
Thus final answer. ```python
import sys
def main() -> None:
data = list(map(int, sys.stdin.read().split()))
idx = 0
out = []
while idx < len(data):
n = data[idx]
idx += 1
if n == 0:
break
# read the n counts (they may span lines, but tokenisation handles it)
counts = data[idx:idx + n]
idx += n
code
# number of types that still have at least one prize
non_zero = sum(1 for x in counts if x > 0)
# can we ever get two identical prizes?
if any(x >= 2 for x in counts):
out.append(str(non_zero + 1))
else:
out.append("NA")
sys.stdout.write("\n".join(out))