We need to understand the problem: Polycarp wants to train for days: day k he must solve exactly k problems. He has a list of n contests, each with a_i problems. Each day, he selects a contest that he hasn't solved yet (i.e., hasn't used before). He then "solves" exactly k problems from that contest, meaning he uses up k problems from that contest? The problem says: "He solves exactly k problems from this contest. Other problems are discarded from it." So if a contest has a_i problems, and on a day he selects it and solves k problems, the remaining problems (a_i - k) are discarded (i.e., cannot be used later). So effectively, once a contest is chosen, it is used up entirely? But not exactly: he only uses k problems, but the rest are discarded, so the contest cannot be used again. So each contest can be used at most once, and the number of problems that can be taken from it is exactly k on the day it is chosen, but only if a_i >= k, otherwise he can't select it because it must have at least k problems. So basically, we have a multiset of contest sizes. On day k (starting from 1), we need to pick an unused contest with size at least k. Then we "use" it, and we discard it. The day count increases. The process continues as long as we can assign a distinct contest to each day k, such that the contest's size >= k.
Goal: maximize number of days (max k) we can go.
This is reminiscent of a classic problem: given an array of numbers, we want to find the maximum length L such that we can match days 1..L with distinct contests where contest value >= day index. This is similar to: sort the contests, then greedily assign smallest possible contest that satisfies the day requirement. Indeed, we can think: to maximize the number of days, we should use the contests in increasing order, assigning the smallest eligible contest to each day. Because if we have a contest that is large, we might want to save it for later days where requirement is larger. So the optimal strategy: sort the contests in non-decreasing order. Then iterate days from 1 upward, maintaining an index into sorted array. For each day d, we need to find the smallest unused contest that is >= d. If we have one, we use it and move on; else we stop. The number of days we can achieve is the answer.
This is exactly like "Maximum number of days" where we try to schedule contests.
Let's test with examples:
Example1: contests = [3,1,4,1] sorted = [1,1,3,4]
day1: need >=1, smallest is 1 (use first 1). index moves.
day2: need >=2, smallest unused >=2? We have remaining [1,3,4]. Smallest >=2 is 3. Use 3. index moves.
day3: need >=3, remaining [1,4] -> need >=3, smallest >=3 is 4. Use 4.
day4: need >=4, remaining [1] -> no contest >=4, stop. So 3 days. matches output.
Example2: [1,1,1] sorted = [1,1,1]
day1: use 1.
day2: need >=2, remaining [1,1] none >=2, stop. So 1. matches.
Example3: [1,1,1,2,2] sorted = [1,1,1,2,2]
day1: use 1.
day2: need >=2, smallest >=2? we have 2 (first 2). use it.
day3: need >=3, remaining [1,1,2] -> need >=3, none. So 2. matches.
Thus greedy works.
But is there any nuance? Could there be a case where using a slightly larger contest earlier could lead to more days? Classic problem "Maximum number of tasks" where tasks have requirements and we assign smallest possible each day. The greedy is optimal: we want to use as small as possible for current day to preserve larger for later. So yes.
Implementation: O(n log n) sorting.
Constraints: n up to 2e5, a_i up to 2e5. So sorting fine.
Thus answer is the count of days we can achieve via greedy.
But note: The problem might also be interpreted as: each day he picks a contest that hasn't been solved yet, and solves exactly k problems from it, discarding the rest. He cannot split a contest across multiple days; each contest can only be used once. So it's exactly as described.
Thus solution: sort array, then iterate through sorted array and count how many a_i >= current_day. Actually typical solution: after sorting, we maintain day = 1, count = 0. For each a in sorted array: if a >= day, then we can use this contest for day, so count++, day++. Else skip (since it's too small for current day and any later day with larger requirement, so we can discard it). This yields the maximum number of days. Let's check with examples: sorted [1,1,3,4] day=1: 1>=1 -> count=1, day=2; next 1>=2? false, skip; next 3>=2 -> count=2, day=3; next 4>=3 -> count=3, day=4; stop. gives 3. For [1,1,1], day1: 1>=1 -> count=1, day2; next 1>=2? false; next 1>=2? false; count=1. For [1,1,1,2,2], day1: 1>=1 ->1, day2; 1>=2? false; 1>=2? false; 2>=2 ->2, day3; 2>=3? false; so count=2.
This approach works because we are effectively skipping contests that are too small to be used for the current day (they would never be usable later because day only increases). So it's correct.
Therefore answer is as described.
But wait, is there any nuance about the problem that says: "If there are no contests consisting of at least k problems that Polycarp didn't solve yet during the k-th day, then Polycarp stops his training." So the process stops as soon as you cannot find a contest for that day. It does not allow skipping days. So the maximum days is the maximum k such that you can assign distinct contests to each day from 1 to k.
Thus solution.
Edge Cases: n=1, a1=1 => answer 1; if a1=0? but a_i>=1, so fine. If large a_i, but many small, answer limited by number of contests.
Complexity: O(n log n). Could also be done with counting sort since a_i <= 2e5, but not needed.
Now write solution.
But before finalizing, double-check: is there any scenario where we might want to not use a contest that is exactly equal to the day because we might need it later for a larger day? No, if a contest equals the day, it's just enough for that day; if we skip it now, later days require larger, so it won't be usable. So it's optimal to use it now if possible. And if a contest is larger than the current day, we might be tempted to skip it and use a smaller one, but we are processing sorted order, so we will use the smallest that is >= day. So it's optimal.
Thus answer.
But let's confirm with a tricky case: contests: [2,2]. Sorted: [2,2]. day1: first 2 >=1 -> use (count1, day2). day2: second 2>=2 -> use (count2, day3). So answer 2. Makes sense: day1 use one contest, solve 1 problem and discard 1; day2 use other contest, solve 2 problems (discard 0). total days 2.
If contests: [2]. day1: use 2, day2: need contest >=2 but none left, so answer 1. Ok.
If contests: [5]. day1: use 5, day2: need >=2 but none left, answer 1. So even though contest had more problems, we can't use it for multiple days.
Thus correct.
Now we produce code.
Pseudo:
def solve():
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
a.sort()
day = 1
ans = 0
for x in a:
if x >= day:
ans += 1
day += 1
print(ans)
But wait: check if day increments after each successful use. Yes.
Edge: For large n, day can go up to n if all a_i are large enough, but day can't exceed n+1? Actually max days is n because we have n contests. So day will not exceed n+1, but ans <= n.
So it's fine.
Now confirm with sample tests.
Sample1: n=4, a= [3,1,4,1] sorted = [1,1,3,4]. day=1: x=1>=1 -> ans=1, day=2; x=1: 1>=2? false; x=3: 3>=2 -> ans=2, day=3; x=4: 4>=3 -> ans=3, day=4; print 3. Ok.
Sample2: [1,1,1] -> sorted = [1,1,1]. day1: 1>=1->ans=1,day2; next 1>=2 false; next 1>=2 false => ans=1.
Sample3: [1,1,1,2,2] -> sorted = [1,1,1,2,2]. day1: 1>=1->ans=1,day2; 1>=2 false; 1>=2 false; 2>=2->ans=2,day3; 2>=3 false => ans=2.
Thus output matches.
Therefore answer.
But double-check: Might there be a scenario where we skip a contest that is large enough for current day but we want to use it later? For example, sorted: [2,3] day1: smallest >=1 is 2, we use it. day2: need >=2, we have 3 which works. So ans=2. If we had used 3 on day1, we'd have 2 for day2: day1 3>=1 works, day2 2>=2 works, also ans=2. So same. But suppose [2,2,100] day1: use 2, day2: use 2, day3: use 100 => 3 days. If we used 100 on day1, we'd have day2 need 2, we have 2 works, day3 need 3, we have 2 not enough => only 2 days. So it's crucial to use smallest possible each day. The greedy algorithm does that because it processes sorted list and only uses a contest when it's >= current day; it uses the smallest that meets because we are iterating in increasing order, and we skip those that are too small for current day. So the first one that meets condition is the smallest available that is >= day. So that's optimal.
Thus solution is correct.
Now we might want to think about an alternative solution: binary search on answer? But greedy is simpler.
But note: The problem statement may be ambiguous: "During each day Polycarp has to choose exactly one of the contests he didn't solve yet and solve it. He solves exactly k problems from this contest. Other problems are discarded from it." It could be interpreted as: On day k, he picks an unsolved contest, and from that contest he solves exactly k problems; the rest are discarded (so he doesn't solve them ever). So after using a contest, it's considered solved and cannot be used again. So our interpretation is correct.
Thus finalize.
Now I'll produce final answer with Python code.
We'll also consider reading input quickly because n up to 2e5.
Now some reasoning: It's essentially the classic "Maximum number of tasks you can complete" where you have tasks with required times and you need to schedule them in order. But here the requirement is day number.
Another perspective: This is equivalent to "find maximum k such that there are at least k contests with size >= k", after sorting? Actually it's similar to h-index? Let's think: If we sort and then count, it's like the largest d such that the d-th smallest contest (when sorted) is >= d? Not exactly: In h-index, we sort descending, but here we sort ascending and count. But we can think: The answer is the maximum d such that after sorting ascending, a[d] >= d? But careful: Because we skip small ones. For example, [1,2,2] sorted: [1,2,2]; our algorithm: day1 use 1, day2 use 2 (since 2>=2), day3 use 2 (2>=3? No) -> answer 2. Here a[2] (0-indexed?) If we use 1-indexed sorted: a[1]=2 >= 2? yes, a[2]=2 >=3? no. So d is 2. But if we had [1,3,3] sorted: day1:1, day2:3, day3:3 -> answer 3? Check: day1:1>=1, day2:3>=2, day3:3>=3, so answer 3. Sorted a[1]=1? Actually a[1]=1 which is not >=2, but we skipped it? Wait, algorithm uses not necessarily the d-th element; because we skip some. In [1,3,3], sorted: [1,3,3]. Process: x=1 >= day1 => use, day2. x=3 >= day2 => use, day3. x=3 >= day3 => use, answer 3. So here we used first, second, third elements. The first element was 1, which is less than day2? But it was used on day1. So all three were used. So essentially we used all three because they eventually met day conditions. The condition for a sorted array to allow d days is that there exists an increasing selection of d indices i1 < i2 < ... < id such that a[ij] >= j. This is equivalent to after sorting, the number of elements >= j is at least j for all j up to d? Actually, we can think: For each day j, we need a contest with value at least j that hasn't been used. The greedy algorithm essentially picks the first d elements that satisfy a[i] >= current day. This is like we can compute the maximum d such that after sorting, a[i] >= (i+1 - number of skipped)? Hmm not necessary.
But our algorithm is straightforward.
Thus final solution.
But wait, are we sure that the greedy is always optimal? Consider case: contests = [2,2,3] sorted: [2,2,3]. Greedy: day1: use 2 (first), day2: use 2 (second) day3: use 3 (third) => answer 3. Works. Another: [1,2,3] sorted: day1:1, day2:2, day3:3 => answer3. [1,2,2] sorted: day1:1, day2:2 (first 2), day3: need 3, next 2 not enough => answer2. Could there be a better assignment? If we skip 1 on day1 and use 2 on day1, then day2: we could use 2 (the other 2), day3: need 3 but only 1 left? Actually we have left [1]? Let's simulate: initially [1,2,2]. If day1 use 2 (choose 2), then day2 need >=2, remaining [1,2] -> can use 2, day3 need >=3, remaining [1] -> none, answer2. So same. If day1 use 1, day2 use 2, day3 none => answer2. So 2 is max. So greedy is fine.
Thus we can implement.
One nuance: The problem might also be interpreted as: He can choose a contest and solve exactly k problems, but the contest still has leftover problems that might be used later? But the statement: "During each day Polycarp has to choose exactly one of the contests he didn't solve yet and solve it. He solves exactly k problems from this contest. Other problems are discarded from it." The phrase "he didn't solve yet" means he hasn't solved that contest before. After solving, he has solved it, so it's considered solved and cannot be used again. So leftover problems are discarded, not available. So it's exactly one-time use.
Thus final.
Now code.
But maybe there is a trick: The input size large, but Python sorting is fine.
We'll implement:
def main():
import sys
n = int(sys.stdin.readline())
a = list(map(int, sys.stdin.readline().split()))
a.sort()
day = 1
ans = 0
for val in a:
if val >= day:
ans += 1
day += 1
print(ans)
if name == "main":
main()
That's it.
Now I'll produce final answer with explanation. ```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
n = int(data[0])
a = list(map(int, data[1:1+n]))
a.sort()
day = 1
ans = 0
for x in a:
if x >= day:
ans += 1
day += 1
print(ans)
if name == "main":
solve()