We need to maximize total points given constraints: n tasks, each with k subtasks. Subtask times are t1,...,tk (same for all tasks). Solving any subtask yields 1 point. Completing all subtasks of a single task yields an extra point, making total points for that task = k+1 if fully solved, otherwise points = number of solved subtasks (which can be between 0 and k-1). However, note that extra point is only given if the task is fully completed. It is not given per partial completion, only when all k subtasks of that task are solved.
Goal: Given total time M, maximize total points.
We have n tasks, each can be considered independently, but we have to decide how many tasks we fully solve (i.e., all subtasks) and how many partially solved subtasks for the remaining tasks. However, partial subtasks can be done across any tasks, order doesn't matter. But there is a nuance: The extra point is per task completely solved. So it's beneficial to fully complete a task only if we can afford all its k subtasks, and it yields an extra point.
But also, we could solve subtasks from different tasks without completing any, and we could also solve some tasks partially. Since subtask times are the same across tasks, the only thing that matters is how many of each type of subtask (index j) we solve overall. However, the extra points depend on grouping: to get the extra point for a task, we must have solved all k subtasks of that task. So we need to decide the number of fully completed tasks, say x (0 <= x <= n). Then those tasks consume x * sum_{j=1..k} t_j time and give x*(k+1) points.
For the remaining n - x tasks, we can solve any subset of subtasks, but we cannot get the extra point for them unless we fully solve them (but then they would be part of x). So for incomplete tasks, we earn points equal to number of subtasks solved from those tasks.
Additionally, we could also consider solving subtasks from the fully completed tasks beyond the full set? Actually, each subtask can only be solved once per task. For a fully completed task, we already solve all subtasks; we cannot solve extra subtasks beyond that because there are only k. So for fully completed tasks, we've already accounted for all subtasks.
Thus, the problem reduces to: allocate time M across two parts:
- fully complete x tasks: cost C_full = x * T where T = sum t_j, gain P_full = x * (k+1).
- among the remaining n - x tasks, we can solve some subtasks, but we can also intermix: we could also choose to solve subtasks from tasks that we later fully complete? But careful: if we decide to fully complete some tasks, we might as well allocate the required subtasks for them as part of the full completion. For the remaining tasks, we may decide to solve some subtasks across them arbitrarily.
But there might be a more optimal approach that is not captured by this "first pick x fully completed tasks, then allocate remaining time to partial tasks" because partial tasks could be from tasks that eventually become fully completed? However, if we want to fully complete a task, it's always optimal to do so in a batch? The order of solving subtasks doesn't matter, we just care about the set of solved subtasks. If we decide to fully complete a set of tasks, we can just mark those tasks as fully solved. The time for those is exactly x*T. If we have any additional solved subtasks from other tasks (including possibly from tasks we later decide to fully complete but haven't yet), it's better to allocate those subtasks to new tasks rather than increasing the count of fully completed tasks if we don't have enough time to fully complete them? But if we have some solved subtasks from a task that is not fully completed, and then later we solve the remaining subtasks of that task, we would get an extra point. So there might be a strategy where we partially solve many tasks and later complete some of them fully, gaining the extra point. The decision of which tasks to fully complete could be made after we've already solved some subtasks. However, we can think in terms of overall counts: let y_j be the number of tasks for which we have solved at least j subtasks? That's complicated.
Alternatively, we can think of the problem as: we have n tasks, each has k subtasks with identical times. We want to select a multiset of subtasks (some of which may lead to extra points if all k for a task are selected). This is akin to: each task i can be in one of several states: number of solved subtasks s_i in {0,1,...,k}, with points = s_i + (1 if s_i == k else 0). Total points = sum_i [s_i + (s_i == k)]. Total time = sum_i sum_{j: subtask j solved} t_j, but careful: the subtask times differ by index. For each task i, if s_i = p (0 <= p <= k), that means we solved the first p subtasks? But we can solve any set of subtasks, not necessarily in order? Problem says "He can solve subtasks in any order." So we can choose any subset of the k subtasks per task. So for each task, we can pick a subset of indices, not necessarily contiguous. So the time cost for a task with a particular set S of solved subtasks is sum_{j in S} t_j. The points gained: |S| if S != all k, else k+1.
Thus we need to maximize sum_i (|S_i| + indicator(S_i = full)) given sum_i sum_{j in S_i} t_j <= M.
Because tasks are identical, the optimal solution is symmetric: we will treat tasks equally, but perhaps we will solve some tasks fully, and for the rest we will solve a certain subset of subtasks that yields highest points per time. However, because the extra point only applies if we solve all subtasks, there might be an incentive to solve full tasks only if it's efficient compared to partial subsets.
We can think in terms of a knapsack: For each task, we have 2^k possible subsets (with extra point for full set). But k <= 45, too large. However n also <= 45, but we could use DP over number of tasks? Another approach: Since tasks are independent, and times are identical across tasks, the optimal solution might involve sorting subtasks by efficiency? But the extra point complicates.
Let's try to reason about the structure of optimal solution.
Observation: For a given set of solved subtasks across tasks, we can reassign subtask solves to tasks arbitrarily because tasks are indistinguishable? Actually, the assignment matters for extra points. Suppose we have total count of solved subtasks per index: let a_j be the number of tasks for which subtask j is solved. Then total points = sum_j a_j + (number of tasks that have all k subtasks solved). Because each solved subtask gives 1 point, plus extra points for fully solved tasks. But the number of fully solved tasks is limited by min_j a_j? Actually, for a task to be fully solved, it must have all its k subtasks solved. That means we need to assign the solved subtasks to tasks such that some tasks have all indices. Given counts a_j (number of tasks with subtask j solved), the maximum number of tasks that can be fully solved is min_j a_j (since each fully solved task uses one of each subtask). But it's not necessary that we actually achieve that many fully solved tasks; we could have more than min_j a_j if we allocate subtasks in a way that not all tasks that could be fully solved are? Actually, given counts a_j, the maximum number of fully solved tasks we can form is min_j a_j, because each fully solved task requires one unit from each subtask type. And we can always achieve that many by grouping one of each from the subtask pools together. Any extra subtasks beyond that will belong to tasks that are incomplete. So the maximum total points for given counts a_j is sum_j a_j + min_j a_j. Is it always optimal to form as many full tasks as possible? Yes, because extra points are additive and do not cost extra time; grouping extra subtasks into full tasks yields extra point without any additional cost (the subtasks are already solved). So given we have solved subtask counts a_j, we should always form as many complete tasks as possible, i.e., x = min_j a_j. Then points = sum_j a_j + x.
Thus the problem reduces to: choose nonnegative integers a_j (0 <= a_j <= n) to maximize sum_j a_j + min_j a_j, subject to sum_j a_j * t_j <= M. The tasks are n, so each a_j cannot exceed n because we only have n tasks; you can't solve a subtask of a particular index in more than n tasks because each task has exactly one subtask of that index. So a_j <= n.
But note: We also need to ensure that we can assign the subtasks to tasks so that each task gets at most one of each subtask index (i.e., we can't assign two subtasks of same index to same task). However, since each task has only one subtask per index, the counts a_j naturally represent how many tasks have that subtask solved. As long as a_j <= n, we can assign them arbitrarily: we can think of having n slots (tasks). For each j, we select a_j of these tasks to have subtask j solved. After selecting all j, each task i gets a set of indices for which it was selected. That yields a valid assignment. There is no further constraint: we can independently choose for each subtask which tasks have it solved. So any vector a_j with each a_j <= n is feasible.
But is there any hidden constraint from the fact that a task cannot have duplicate subtask? No, because each task has exactly one subtask of each index, so we can independently decide per index per task.
Thus the problem becomes: maximize f(a_1,...,a_k) = sum a_j + min a_j, subject to 0 <= a_j <= n, integer, and sum a_j * t_j <= M.
Now we need to compute this maximum. n up to 45, k up to 45, M up to 2e9. t_j up to 1e6.
We have up to 45 variables but constraints are moderate. The objective has a non-linear min term. However, we can brute force over the minimum value? Since a_j are bounded by n (45), we can iterate over possible values of m = min_j a_j, from 0 to n. For a fixed m, we need to enforce that each a_j >= m, and we also have a_j <= n. The objective becomes sum a_j + m. So given m, to maximize sum a_j, we want to maximize sum a_j under constraints a_j >= m, sum a_j * t_j <= M. That is a knapsack-like but with lower bounds. Since we want to maximize sum a_j, it's essentially: we must allocate at least m subtasks of each type, costing m * T (where T = sum t_j). After that, we have remaining time R = M - m * T (if negative then m not feasible). Then for each subtask j, we can solve additional b_j = a_j - m, where 0 <= b_j <= n - m (since a_j <= n). Each extra subtask j gives +1 point and costs t_j time. And the objective becomes (sum a_j) + m = (sum (m + b_j)) + m = mk + sum b_j + m = m(k+1) + sum b_j.
So for fixed m, we need to maximize sum b_j subject to: 0 <= b_j <= n - m, sum b_j * t_j <= R. This is a bounded knapsack (each item type j with profit 1, weight t_j, maximum count n - m). Also note that we must have R >= 0 to be feasible.
Thus the overall maximum points = max over feasible m of [m*(k+1) + max_sum_b], where max_sum_b is the maximum number of items (i.e., total count) that can be selected from k types with capacities (n - m) each, total weight <= R.
This is a classic unbounded/bounded knapsack with identical profit per item? Actually each item gives profit 1, so maximizing total count is like: we want to pick as many items as possible given weight limit, with each type having a max count. Since profit per item is uniform, the optimal strategy is to greedily pick items with smallest t_j first, up to their limit. However, we need to check: because we have a limit per type (n - m). Since all items give same profit (1), to maximize count under weight limit, we should always pick the cheapest items first. So the solution for max_sum_b is: sort subtasks by time t_j ascending, then fill as many as possible from the cheapest, respecting per-type limit (n - m), until we run out of time.
But is that always optimal? Yes, because if profit per weight is not uniform? Actually profit per item is 1, so we want to minimize total time per item, i.e., pick items with smallest t_j. This is a standard fractional knapsack but since items are indivisible and profits equal, greedy by taking smallest weight items first yields maximum count. However, we must ensure integer counts. So we can just take items in increasing order of t_j, take up to cap = n - m of each, until we cannot take the next full item due to time. This yields the maximum number because any selection that includes a heavier item could be replaced by a lighter one (if available) to get same count with less time, enabling possibly more items? But careful: suppose we have capacity limits per type; if we skip a cheap item (i.e., not take up to its full limit) and instead take a heavier one, we get fewer items for same time or same count with more time, so that's not optimal for maximizing count given time constraint. So indeed the greedy works.
But we need to consider that we have to also consider the possibility of not taking the full m? Actually m is the minimum a_j. But we also have the possibility that the optimal min_j a_j might not be equal to some m, but perhaps the min is not forced? In our transformation we set m = min a_j, and then we forced a_j >= m. This is valid. So we can iterate m.
Thus the solution is: compute T_total = sum t_j. For each m from 0 to n:
- Required time for the baseline: m * T_total. If m * T_total > M, break (since m larger will also be infeasible, because T_total >= positive).
- Compute remaining time R = M - m * T_total.
- Then we need to maximize the number of additional subtasks (b_j) we can solve, where each subtask j can be taken at most c = n - m times (since a_j <= n).
- To maximize count, we sort t_j (maybe we have them already sorted). Then iterate j from smallest t_j to largest, taking as many as possible: for each j, we can take up to c, each costing t_j. So we take x_j = min(c, floor(R / t_j))? Actually we need to fill greedily: start from smallest t_j, take as many as allowed until we run out of time. Since all items have equal value, greedy by taking smallest weight items yields max count. So we can compute: remaining R, total_b = 0; for each t in sorted order:
max_take = min(c, R // t)
total_b += max_take
R -= max_take * t
- Then total points = m*(k+1) + total_b.
Take maximum over m.
But is that always correct? Let's test with examples.
Example 1:
n=3, k=4, M=11, t=[1,2,3,4]
T_total = 10.
Compute m=0: R=11, c = 3. sorted t = [1,2,3,4].
Take t=1: max_take = min(3, 11/1=11) => 3, total_b=3, R=8.
t=2: min(3, 8/2=4) => 3? Actually cap c=3, so take 3, total_b=6, R=8-6=2.
t=3: min(3, 2/3=0) =>0, stop.
total_b=6, points = 0*(5) +6 =6.
m=1: required time=10, R=1, c=2. sorted t: t=1: min(2, 1/1=1) =>1, total_b=1, R=0 -> points = 1*5 +1 =6.
m=2: required time=20 >11, infeasible.
Maximum 6. matches output.
Example2:
n=5,k=5,M=10, t=[1,2,4,8,16]
T_total = 31.
m=0: c=5, R=10.
sorted t=[1,2,4,8,16].
t=1: min(5,10)=5, total_b=5, R=5.
t=2: min(5,5/2=2) =>2, total_b=7, R=5-4=1.
t=4: no more.
points=7.
m=1: required=31>10 infeasible.
So max=7. matches.
Thus the greedy works.
But is there any scenario where the greedy fails due to the fact that we have per-type caps? Since we always take as many as possible of the cheapest, that is optimal for maximizing count under weight limit, given all items have equal value. To be rigorous: Suppose we have items with weights w_i (positive integers) and each type i has max count c_i. We want to maximize number of items subject to sum w_i * count_i <= R. Since profit per item is 1, we want to minimize total weight per item, so we should choose the lightest items first. This is analogous to: sort items by weight; then take items in order until budget exhausted. This yields optimal count because any optimal solution that deviates can be transformed to one with more light items: if an optimal solution uses a heavier item while a lighter item is not at its cap, we can swap one heavy for one light, reducing total weight or same, thereby either freeing weight for more items or at least not worsening count. Formal proof: if there exists an item i lighter than item j (w_i <= w_j) with count_i < c_i and count_j > 0, then replace one j with one i; total count unchanged, weight decreases or stays same, giving potential to add more items. So any optimal solution can be transformed to one that respects the greedy ordering (i.e., the vector of counts is such that for any lighter item, it is at capacity before heavier items are used). So greedy works.
Thus the problem seems solved.
But we must consider that M can be up to 2e9, and t_j up to 1e6, n up to 45, so counts are small (n - m <= 45). So the total number of additional subtasks we can take is at most (n - m)*k <= 2025, which is small. Actually the maximum total additional b_j sum cannot exceed (n - m)k, but that's at most nk = 2025. So the greedy process will iterate over k types (<=45) and compute counts, trivial.
But wait: is there any nuance about the assignment of b_j to tasks and the effect on the min? Because we set m = min a_j. After we add b_j to each type, the new a_j = m + b_j. Then the new min_j a_j might be larger than m if some b_j are zero? Actually we defined m as the minimum of a_j. But in our construction for a fixed m, we enforce a_j >= m. After we maximize sum b_j, the min may become > m if all b_j > 0? But our objective uses m as the min, not the actual min after adding b_j. However, we defined m as the minimum of a_j. If we set a_j = m + b_j, the minimum among them is m + min b_j. Since b_j >= 0, min b_j could be >= 0, so the actual minimum could be greater than m. That would mean that our solution corresponding to this m may actually have a higher min, which would yield more points than we accounted for (since points = sum a_j + min a_j). In our formula, we used points = m*(k+1) + sum b_j. But if min b_j > 0, then actual points would be (m + min b_j)*(k+1) + sum (b_j - min b_j)? Let's recalc.
We need to be careful: The reduction to m = min a_j and then writing a_j = m + b_j with b_j >= 0, but the min of a_j is m + min b_j. Actually if we let m0 = min a_j, then define b_j = a_j - m0, so that min b_j = 0. That's the natural decomposition. But in our approach we fixed m as the intended min, but then we allowed b_j >= 0 with no constraint that min b_j = 0. That could lead to overcounting if we add b_j that all positive, making actual min > m. But then the resulting solution would have a higher min, which would correspond to a larger m' (the actual min) and we would also have counted its points incorrectly if we treat it under m. However, since we are iterating over all possible m, we would also consider m' as a candidate. And the solution we obtain for m' might be different because the baseline cost includes m'*T_total and then we allocate additional b' with caps n - m'. But the solution we got by taking m and then adding b_j (some possibly all >0) is actually feasible for m'? Let's examine.
Suppose we have a vector a_j. Let m0 = min a_j. Then we can consider the representation: a_j = m0 + b_j where min b_j = 0. This solution would be found when we set m = m0 in our iteration. In our algorithm for fixed m0, we maximize sum b_j given a_j >= m0 and a_j <= n, but we also allow b_j to be any nonnegative, and we do not enforce that some b_j must be zero. But we compute max total b_j under the time constraint. However, the optimal solution for a given m0 might have some b_j that are all positive? If min b_j > 0, then that solution's actual min is m0 + min b_j > m0, so its points would be sum a_j + min a_j = (m0k + sum b_j) + (m0 + min b_j) = m0(k+1) + sum b_j + min b_j. But our formula for m0 gave m0*(k+1) + sum b_j, which is lower by min b_j. So such a solution would be undervalued by our calculation, but it might still be considered because it is also feasible for m' = m0 + min b_j. In the iteration for m', we would consider a baseline of m'T_total and then allocate additional b'_j = a_j - m', where min b'_j = 0. Then the points computed would be m'(k+1) + sum b'_j. Let's check if that matches the true points: a_j = m' + b'_j, so sum a_j = m'*k + sum b'_j; min a_j = m' + min b'_j = m'; so total = m'k + sum b'_j + m' = m'(k+1) + sum b'_j. That equals the true points. So the solution would be accounted for under the higher m'.
Now, is it possible that the solution we compute for a given m (by greedy) yields a vector with min b_j > 0, but for m' > m we might not be able to achieve the same total b'_j? Let's see. Suppose we have a feasible solution with a_j = m + b_j, and let d = min b_j > 0. Then define m' = m + d. Then we have a_j = m' + (b_j - d) with b_j - d >= 0 and min(b_j - d)=0. The time cost: sum a_j * t_j = m*T_total + sum b_j t_j. But also = m'*T_total + sum (b_j - d) t_j. Since m' = m+d, we have m'T_total = mT_total + d * T_total. So the time cost can be written as (m'T_total) + sum (b_j - d) t_j = mT_total + sum b_j t_j. So if we have R' = M - m'T_total, then we need sum (b_j - d) t_j <= R'. That imposes R' = M - (m+d)T_total. But originally we had M >= mT_total + sum b_j t_j. So M - (m+d)T_total >= sum b_j t_j - dT_total = sum (b_j - d) t_j + (sum d t_j) - d T_total? Wait: Let's compute: sum b_j t_j - dT_total = sum b_j t_j - d * sum t_j = sum (b_j - d) t_j. So indeed M - (m+d)*T_total >= sum b_j t_j - d T_total = sum (b_j - d) t_j. So it's feasible. So any solution with a_j can be represented with a higher baseline m' and lower additional b'_j = b_j - d. Therefore, it will also be considered when we enumerate m' (provided m' <= n). However, when we do the greedy for m', we are maximizing sum b'_j, not necessarily this specific solution. Since the greedy gives the maximum possible sum b'_j for that m', the points for m' will be at least as high as the points from that solution (since sum b'_j might be larger). Thus the true optimal solution will be captured at the correct min value.
Thus enumerating m from 0 to n, and for each m compute the maximum possible sum of b_j (additional subtasks) using greedy, and then compute points = m*(k+1) + best_b, gives the correct optimum.
But is it always optimal to enforce that for a given m, we first take m copies of each subtask (cost mT) and then allocate extra subtasks? Could there be a solution where min a_j = m but the optimal allocation does not have exactly m of each subtask? Wait, if min a_j = m, then for each j we have a_j >= m. So there exists at least m subtasks solved of each type. So we can think of a baseline of m full tasks (i.e., m copies of each subtask). The remaining time is used to add extra subtasks. However, it might be that we do not need to allocate those m full tasks explicitly as whole tasks; but the time consumed by the m baseline is exactly mT because we must have at least m of each subtask, and the minimal time to achieve that is exactly mT, assuming we cannot get these subtasks for free. But is it possible that achieving a minimum of m across all types could be done with total time less than mT? Could we achieve a_j >= m for all j without having m full sets? For example, suppose we have two types, times t1=1, t2=100. To have min a_j = 1, we need at least one of each type. The minimal time to get a1>=1 and a2>=1 is 1+100=101. That's exactly 1T. In general, to achieve a lower bound of at least m for each type, you must select at least m of each type, costing at least m * sum t_j = mT. So the minimal time for that part is m*T. And any extra time beyond that can be used to add more subtasks of any type.
Thus, for a given m, the time needed for the "baseline" is exactly mT. However, it might be that the optimal solution with min a_j = m actually uses more than m of some types and exactly m of others, but the baseline of mT is always included. So we can separate it.
Thus algorithm seems correct.
Now, we need to implement. Complexity: O(n * k) with sorting once, because for each m we need to compute greedy. n <= 45, k <= 45, so trivial.
Edge cases: M may be less than some m*T; we skip those m. Also note that m can be 0; then baseline cost 0, and we can take up to n of each subtask (since a_j <= n). So c = n.
But is it possible that m > n? No, because min a_j <= n because a_j <= n.
Thus answer = max_{0<=m<=n} if mT <= M: m(k+1) + max_additional(m).
Now, we need to compute max_additional(m) efficiently. Since we will call for each m, we could precompute sorted t. For each m, the cap per type is c = n - m. Then we need to compute how many items we can take. Because k and n are small, we can just loop over sorted t and subtract. Even if we do for each m separately, O(n*k) = 2025.
But note: M can be large (2e9) and t up to 1e6, but the counts are small, so the loop will quickly exit when R becomes small.
One subtlety: In the greedy, we may not need to process all types if R becomes zero early. So it's fine.
But we should be careful: The greedy takes as many as possible of each cheap type, but we must also consider that we might not want to take all available cheap items because taking them might prevent taking an even cheaper? Actually cheaper is smaller time, so we always want as many as possible of the cheapest. Since they are the cheapest, taking them uses less time per item, so it's always beneficial to take as many as possible. So yes.
Thus, solution:
- Read n, k, M.
- Read list t of length k.
- Sort t ascending.
- Compute T = sum(t).
- Initialize ans = 0.
- For m in 0..n:
if m * T > M: break
R = M - m * T
c = n - m
total_b = 0
for each time in sorted t:
# max number of this type we can take: min(c, R // time)
if time == 0? but t_j >= 1, so fine.
take = min(c, R // time)
total_b += take
R -= take * time
points = m * (k + 1) + total_b
ans = max(ans, points)
- Print ans.
Is this always correct? Let's test with more scenarios.
Edge: n=1, k=1, M=0, t=[5]. T=5.
m=0: R=0, c=1, sorted [5], take=0, total_b=0, points=0. m=1: 5 >0 -> break. ans=0. Should be 0 (no time). Good.
n=1, k=1, M=5, t=[5].
m=0: R=5, c=1, take= min(1,1)=1, total_b=1, points=1. m=1: 5<=5, R=0, c=0, total_b=0, points=1*(2)=2. So ans=2. Indeed we can solve the single subtask and get extra point for completing the task (since k=1, completing means solving the only subtask). So total points=2. Good.
n=1, k=2, M=... Let's test: n=1, k=2, t=[1,100]. So T=101. M=101. m=1: feasible, points=1*(3)=3, total_b=0 => 3. m=0: R=101, c=1, sorted [1,100]. t=1: take min(1,101//1=101)=1, total_b=1, R=100. t=100: take min(1,1)=1, total_b=2, R=0. points=0+2=2. So max=3. That matches: full solve gives 3 points.
But M=100: m=1 requires 101 >100, so skip. m=0: R=100, take t=1:1, total_b=1, R=99, t=100: min(1,0)=0, total_b=1, points=1. So answer 1. Indeed can only solve first subtask, get 1 point. Good.
Now, n=2, k=2, t=[1,100]. T=101. M=150. Let's compute: m=0: R=150, c=2. sorted [1,100]. t=1: take min(2,150)=2, total_b=2, R=148. t=100: take min(2,148//100=1)=1, total_b=3, R=48. points=3. m=1: required 101, R=49, c=1. t=1: take min(1,49)=1, total_b=1, R=48; t=100: take min(1,0)=0 => total_b=1, points = 13+1=4. m=2: required 202>150 infeasible. So ans=4. Let's verify: Could we achieve 4 points? Full solution of one task (2 subtasks) gives 3 points, and we have 49 minutes left. With 49 minutes, we can solve the cheap subtask of the other task (cost 1) to get +1 point, total 4. That's feasible: time 101+1=102? Wait 101 for full task + 1 = 102, but we have M=150, so yes 102 <=150. Actually 101+1=102, but we earlier computed R=49 after taking m=1 baseline? Wait m=1 baseline uses 101, leaving 49. Then we added one cheap subtask: cost 1, total 102. So 102 <=150. So 4 points. Could we get 5 points? That would require maybe both tasks fully solved? That would be 2(k+1)=6 but need 202 time >150. Or maybe one full and both cheap subtasks of second? The second task has two subtasks, but we can solve both? That would be full too, because if we solve both subtasks of the second task, then it becomes full, making two full tasks, requiring 202 time >150. So 4 is max. Good.
Now, consider scenario where the greedy for b_j might not be optimal because we might want to leave some capacity for cheaper items? But we take as many cheap items as possible. However, suppose we have per-type caps. Since the cheap items are the most time-efficient (lowest time per point), we want as many as possible. So greedy works.
But there is a nuance: In the decomposition, we treat the baseline as m full sets (i.e., m copies of each subtask). However, what if the optimal solution with min a_j = m does not actually include m full sets? But as argued, if min a_j = m, then there are at least m of each subtask, so the total time spent on those at least m subtasks per type is at least mT. But could the optimal solution have the property that some subtask j, we have exactly m copies, but others we have more; the time spent on the mandatory m copies could be arranged in a way that the m copies of each type are not necessarily coming from the same tasks? That doesn't affect the total time: the sum of times for the mandatory copies is exactly msum t_j, because each mandatory copy of type j costs t_j, and we need at least m of them. To minimize time for a given m, we would choose exactly m of each type, costing mT. However, it might be that the optimal solution uses more than m for some types, but then the baseline of mT is still part of the time. So we can always subtract m*T from total time, and the remaining time is for extra items beyond the mandatory ones. So that's valid.
Thus algorithm stands.
But we should double-check constraints: n up to 45, k up to 45, t_j up to 1e6, M up to 2e9. With m up to 45, mT can be up to 45 * (451e6)=4545e6=2.025e9, slightly above 2e9. So mT may exceed M for many m, we break early.
Potential overflow: Use 64-bit (long long) for computations.
Now, think about if there is any scenario where the greedy for b_j is not optimal because of the per-type caps but also we might want to not use all cheap items to allow more items overall? That seems contradictory: taking a cheap item uses less time, so if you skip a cheap item to take a more expensive one, you would end up with the same number of items but using more time, or fewer items for same time. So greedy yields max count.
Thus solution is straightforward.
However, I want to examine more complex examples to ensure no flaw: Let's try n=3, k=3, t=[2,3,100], M=??.
We can compute T=105.
M=200.
m=0: c=3, R=200. Sorted [2,3,100].
Take 2: min(3,200/2=100) =>3, total_b=3, R=200-6=194.
Take 3: min(3,194/3=64) =>3, total_b=6, R=194-9=185.
Take 100: min(3,185/100=1) =>1, total_b=7, R=85. Points=7.
m=1: needed 105, R=95, c=2.
Take 2: min(2,95/2=47)=>2, total_b=2, R=95-4=91.
Take 3: min(2,91/3=30)=>2, total_b=4, R=91-6=85.
Take 100: min(2,85/100=0)=>0. Points=1*4+4=8.
m=2: needed 210 >200 infeasible. So ans=8.
Can we get 8 points? Full task (4 points) plus extra? Actually m=1 gives 1 full task (points 4) plus extra subtasks: we took 2 of subtask1 (cost 2 each) and 2 of subtask2 (cost 3 each), total extra subtasks 4, points 4, total 8. Time: full task 105 + 22 + 23 = 105+4+6=115 <=200. Yes. Could we get 9? Maybe m=1 and take extra 5 subtasks? But caps c=2 per type, so max extra subtasks = 2+2+? Actually we could take up to 2 per type, so max extra total = 6. But do we have time? The cheapest extra after m=1: we can take up to 2 of type1 (cost 2 each), 2 of type2 (cost3 each), and 2 of type3 (cost100 each). To take 2 of type3 would cost 200, too much. So max total_b maybe 5 if we take both type1, both type2, and one type3? That would be total extra cost = 4+6+100=110, plus baseline 105 =215 >200. So can't. Could we do m=0 and get 8? That would be 7 as computed. So 8 seems max.
Now, what about a scenario where we might not want to take the full capacity of the cheapest because we might want to keep some tasks for the extra point? But the extra point is accounted by m. In m=0 we are not getting any extra points. But we could have a solution with min a_j = 0 that yields points = sum a_j (since min=0). That's less than if we shift some subtasks to form a full task. So it's always beneficial to try to form as many full tasks as possible? Not necessarily if the times are such that full tasks are inefficient? But the extra point is essentially a bonus that costs no extra time beyond the subtasks themselves. So if we have a set of subtasks, we can always group them to maximize extra points by making as many full tasks as possible. So given a selection of subtasks (counts a_j), the points are sum a_j + min a_j. So the extra point per full task is essentially an extra point for each set of k subtasks we can form. So it's like we get a bonus for each complete set.
Thus our transformation is correct.
Now, one more nuance: In our enumeration, we set m = min a_j. But what about the case where min a_j is not integer? No.
All good.
Edge Cases: Since M can be up to 2e9, and T may be large, but we are safe.
Now, can we improve beyond O(n*k) or just implement as described.
But I want to consider if there is any case where the greedy for b_j fails because we need to consider that the extra point from a full task is not just the min but also depends on the distribution of b_j? No, as we argued, after fixing m as the min, the points are m*(k+1) + sum b_j, independent of distribution of b_j as long as b_j >= 0. Actually careful: If we have a_j = m + b_j, and some b_j might be zero, the min a_j is m. But if we have b_j all positive, the min becomes > m, which would actually increase points. However, in our evaluation for fixed m we are not giving that extra credit. But as argued, those solutions are covered by higher m. But is it possible that for some m, the greedy algorithm that maximizes sum b_j might produce a solution where min b_j > 0, and that solution yields points that are higher than what we compute for m, but we might not be able to achieve the same total points under the higher m because the greedy for that higher m might not produce as many total points? Let's try to construct a counterexample.
Suppose we have n=2, k=2, t = [1, 10]. T=11.
M=21.
Now, enumerate:
m=0: c=2, R=21. Sorted [1,10].
t=1: min(2,21)=2, total_b=2, R=19.
t=10: min(2,1)=1, total_b=3, R=9. Points=3.
m=1: baseline 11, R=10, c=1.
t=1: min(1,10)=1, total_b=1, R=9.
t=10: min(1,0)=0 => total_b=1, points=1*3+1=4.
m=2: baseline 22>21 infeasible.
So ans=4.
Now, could there be a solution with actual min = 1 and total points 5? That would require a_j values: sum a_j + min a_j =5 with min=1 => sum a_j =4 => a_1 + a_2 =4 with min>=1 and each <=2 (since n=2). So possible a_1=2, a_2=2 gives sum=4, min=2 -> points=6, but that would require time = 21+210=22>21, infeasible. a_1=2, a_2=1 gives min=1, sum=3 -> points=4 (not 5). a_1=1, a_2=2 -> min=1, sum=3 -> points=4. So 4 is max.
Now, try a case where greedy for m yields b_j all positive? For m=0, we got b_j: a_1=2, a_2=1 => min b_j =? a1 =2 => b1=2, a2=1 => b2=1 => min b =1 >0. So this solution is actually min a_j = 1? No, because a2=1, a1=2, min=1, so actual min is 1. So this solution is covered by m=1. In m=1 we found points=4, same as this solution? Actually points for this solution: sum a=3, min=1 => points=4. And m=1 greedy gave also 4. So consistent.
Now, could there be a case where for m=0, greedy yields b_j all >= 1 (so actual min >0) and points computed for m=0 underestimates true points of that solution, but the same solution would be considered for the true m (say m=1) but the greedy for m=1 might produce a different (maybe lower) total points than the original solution? However, that solution is feasible for m=1 with b'_j = a_j -1. So it would be a candidate for m=1. Since our algorithm for m=1 computes the maximum possible sum b'_j (which might be higher than this solution's b'_j), it will output at least as many points as that solution, because we are taking max. So overall ans will be at least the true points of that solution. So no problem.
Thus algorithm yields correct optimum.
Now, we need to think about any oversight: Could the optimal solution have a_j > n for some j? No, because there are only n tasks.
Could we have a situation where taking extra subtasks beyond n - m per type is not allowed because that would exceed the per-task limit? Actually a_j <= n, so after baseline m, we can at most add n - m to each type. That's accounted.
Now, is it always beneficial to sort t ascending? Yes.
But there is one more subtlety: In the baseline of m full tasks, we assumed that the time for those mandatory subtasks is exactly m*T. However, note that when we later add extra subtasks, we may also add subtasks to the same tasks that already have the mandatory ones, but that doesn't affect the baseline cost. The baseline cost is fixed.
Thus, overall algorithm is simple.
Now, to be thorough, we could also solve using DP (knapsack) but no need.
However, I want to consider if there might be a scenario where it's optimal to have min a_j = m but not take all possible extra subtasks because taking some extra subtasks could increase min? Actually if we take extra subtasks, we might increase min, leading to more points via the min term. Our approach of enumerating m accounts for that because we can always consider the higher m. But maybe for a given m, the greedy for maximizing sum b_j might not be optimal for the overall points across m? But we are taking max over m, so we need for each m the best possible sum b_j, which yields the best possible points for that m. If for some m, there is a solution that yields sum b_j less than the greedy maximum, but also yields a larger increase in the effective min (i.e., it pushes the min higher), that solution would actually correspond to a higher m. So it will be captured when we consider that higher m. For that higher m, we will again compute the maximum sum b'_j. Since raising m increases baseline points by (k+1) per unit, but also reduces the cap for extra subtasks (since c = n - m decreases). There is a trade-off. So our exhaustive enumeration over m captures it.
Thus the problem reduces to a one-dimensional search over m, with greedy subproblem.
Now, can we do better than O(n*k) by using prefix sums? Not needed.
We should also consider that sorting t once O(k log k) is fine.
Implementation details:
-
Use long long for M, T, R, product etc. Since n<=45, t_j <=1e6, mT <= 4545e6=2.025e9 fits in 32-bit signed? 2.025e9 > 2^31-1=2.147e9? Actually 2.025e9 < 2.147e9, so fits in int32, but for safety use long long (int64). M up to 2e9 also fits in int32, but multiplication might overflow 32-bit if using int. So we use long long.
-
Loop m from 0 to n inclusive. If m*T > M, break.
-
For each m, compute remaining R and cap.
-
For each t in sorted list, compute take = min(c, R / t). Use integer division.
-
Add to total_b, subtract from R.
-
Compute points = m*(k+1) + total_b, update ans.
-
Print ans.
Let's test with more random small instances by brute force to verify.
I can do mental brute force for very small n,k to ensure.
Take n=2, k=2, t=[2,3]. T=5.
M=0: only m=0 feasible, points=0.
M=1: m=0: R=1, take t=2? sorted [2,3] -> can't take any, total_b=0, points=0. m=1 requires 5>1. So 0.
M=2: m=0: take t=2? min(2,1)=0? Actually 2//2=1 but R=2, take min(2,1)=1? Wait 2//2=1, so take=1, total_b=1, R=0, points=1. m=1 infeasible. So ans=1.
M=3: m=0: R=3, t=2: take min(2,1)=1, total_b=1, R=1; t=3: min(2,0)=0 => total_b=1, points=1. m=1 infeasible (5>3). So ans=1.
M=4: m=0: R=4, t=2: min(2,2)=2, total_b=2, R=0; points=2. m=1 infeasible. So ans=2.
M=5: m=0: R=5, t=2: min(2,2)=2, total_b=2, R=1; t=3: min(2,0)=0 => points=2. m=1: R=0, c=1, total_b=0, points=3. So ans=3.
M=6: m=0: R=6, t=2: min(2,3)=2, total_b=2, R=2; t=3: min(2,0)=0 => points=2. m=1: R=1, c=1, t=2: min(1,0)=0; total_b=0, points=3. So ans=3? But maybe better: Could we get 4 points? For M=6, one full task (cost5) yields 3 points, remaining 1 minute can't do any subtask (since cheapest is 2). So 3. So ans=3.
M=7: m=0: R=7, t=2: min(2,3)=2, total_b=2, R=3; t=3: min(2,1)=1, total_b=3, R=0 -> points=3. m=1: R=2, c=1, t=2: min(1,1)=1, total_b=1, R=0 -> points=4. So ans=4.
M=8: m=0: R=8, t=2: min(2,4)=2, total_b=2,R=4; t=3: min(2,1)=1, total_b=3,R=1-> points=3. m=1: R=3, c=1, t=2: min(1,1)=1, total_b=1,R=1; t=3: min(1,0)=0-> points=4. m=2: 10>8 infeasible. So ans=4.
M=9: m=0: t=2:2,t=3: min(2,? after 2 items of 2 cost4, R=5, then t=3: min(2,1)=1, total_b=3,R=2-> points=3. m=1: R=4, t=2:1,R=2, t=3: min(1,0)=0 -> points=4. m=2: 10>9 infeasible. So ans=4.
M=10: m=0: t=2:2,R=6; t=3:2? Actually after 2 items of 2, R=6, t=3: min(2,6/3=2)=2, total_b=4,R=0 -> points=4. m=1: R=5, t=2:1,R=3; t=3:1, total_b=2,R=0 -> points=5. m=2: R=0, c=0, points=6. So ans=6? Wait m=2: mT=10, R=0, points=23=6. So ans=6. Indeed two full tasks cost 10, give 6 points. So ans=6. So the algorithm yields that.
Now, check a scenario where the greedy for b_j for a given m might not give the maximum sum b_j because we have a cap per type, but maybe taking fewer of the cheapest to allow taking more of a slightly more expensive but still within caps? For example, suppose t=[2,3,3], caps c=1 each, R=5. Greedy: take t=2: 1 (cost2, R=3), then t=3: 1 (cost3, R=0), total_b=2. But if we skip t=2 and take two of t=3? Can't because cap is 1 per type. So cannot. So 2 is max.
Another: t=[2,4,4], caps c=2 each? Actually c maybe 2, R=9. Greedy: t=2: take 2 (cost4, R=5), t=4: take min(2,5//4=1) =1, total_b=3. Alternatively, take t=2:1 (cost2,R=7), then t=4:2 (cost8,R=-1)? That would be 2 of t=4? Actually cap 2, but need R>=8, which is >7, not possible. So 3 is max.
Now, scenario where cheaper item has lower cap than a more expensive item, and the cheaper item's cap is small, so after taking all cheap items we may have leftover time that could be used for more expensive items. That's what greedy does: it takes as many cheap as allowed, then moves to more expensive. That yields maximum count because any alternative that takes fewer cheap items would free up time for more expensive items, but since cheap items are cheaper, replacing a cheap item with an expensive one reduces the count for the same time or same count but more time, so would not increase count. To increase total count, you want as many items as possible, so you want to use cheapest items first.
Thus greedy is correct.
Now, is there any possibility that we should not take all possible cheap items because they may cause the min to increase? That effect is captured by the fact that we are enumerating m. But within a fixed m, we are allowed to increase min; but that would be a different m. However, what if increasing min by taking extra cheap items could give a higher point gain per time than taking additional items while keeping min fixed? But we are already considering that through the enumeration over m. So no issue.
Now, we need to be mindful that the baseline mT uses the sum of t_j. But we might be able to achieve a configuration where min a_j = m but we don't actually need to solve exactly m of each type? Wait, if min a_j = m, then there must be at least m of each type. So the total time for those m mandatory subtasks per type is at least m * sum t_j. However, could it be more than m * sum t_j? Yes if we have more than m of some type, but the mandatory ones are just the minimum across types. The time for the mandatory portion is exactly m * sum t_j only if we take exactly m of each type. But if we take more than m of some types, the baseline still includes at least m of each type; the extra are accounted as additional. So the baseline cost is m * sum t_j exactly, but we might have used more time for the mandatory portion if we take more than m of some types? But we are separating: we consider that we first allocate m of each type (cost mT). Then the extra b_j are additional beyond that. So total time = mT + sum b_j t_j. That's always a valid representation for any vector a_j with min a_j >= m. It doesn't require that we actually solved exactly m of each type; we can imagine subtracting m from each a_j to get b_j. The cost of the m baseline is indeed mT. So the representation is accurate. So for any feasible a_j with min a_j = m, the time is exactly m*T + sum b_j t_j. So it's consistent.
Thus, the greedy for extra b_j is independent.
Now, one more nuance: In the representation, we assume that we can always subtract m from each a_j; but if some a_j = m, then b_j = 0; if a_j > m, b_j > 0. So total time is m*T + sum b_j t_j. So it's correct.
Thus the algorithm yields optimum.
Now, double-check constraints: n and k up to 45, so loop 0..n is at most 46 iterations, each iterating over k (max 45) -> 2070 steps. Very fast.
Now, I'll also consider if there is any scenario where we might want to consider m beyond n? No.
Now, let's think about potential pitfalls: Sorting t changes the order of indices, but since the problem is symmetric across types (the only thing that differs is the time), sorting is fine because we treat each type equally. The caps are uniform across types (c = n - m). So sorting doesn't break anything.
Thus, solution is trivial.
But wait, recall that the extra point is only given if all subtasks of a task are solved. In our reduction we got points = sum a_j + min a_j. Let's derive that formally: total points = sum over tasks (solved subtasks count + indicator(all solved)). Let x be the number of fully solved tasks. Then these tasks contribute x*(k+1) points. For the remaining tasks, they contribute sum of their solved subtasks. But note that the fully solved tasks also count their subtasks individually, which is already included in the x*(k+1) (since that includes k points for the k subtasks plus extra). So total points = x*(k+1) + sum over incomplete tasks of (number of solved subtasks). But we can also express sum a_j = total number of solved subtasks across all tasks. That equals xk + sum over incomplete tasks of solved subtasks. So total points = x(k+1) + (sum a_j - x*k) = sum a_j + x. Since x = number of fully solved tasks. But we also have that x <= a_j for all j because each fully solved task uses one of each subtask. The maximum possible x given a_j is min a_j. And we can always achieve x = min a_j by appropriately grouping. So total points maximum for given a_j is sum a_j + min a_j. Yes.
Now, also note that we cannot exceed n tasks, but min a_j cannot exceed n anyway. So correct.
Now, implement.
But wait, is there any possibility that we cannot achieve the grouping to get x = min a_j? That would require that we can assign subtasks to tasks such that min a_j tasks get all k subtasks. This is a classic assignment: we have a_j copies of each subtask type. We need to form as many tasks as possible each containing one of each type. This is possible if and only if we have enough of each type, which we do because we take x = min a_j. Then we can just assign the first x tasks to have all subtasks, and then allocate the remaining subtasks arbitrarily to other tasks. Since tasks are independent and we can assign any subtask to any task, it's always possible. So yes.
Thus solution stands.
Now, we should test with some random small parameters via brute force to be absolutely certain. Since I'm not coding now, I'll think of potential pitfalls.
Consider n=2, k=3, t=[1,2,100], M=203. T=103.
m=0: c=2, R=203.
Sorted [1,2,100].
t=1: take min(2,203)=2, total_b=2,R=201.
t=2: take min(2,201/2=100)=>2, total_b=4,R=197.
t=100: take min(2,197/100=1)=>1, total_b=5,R=97. Points=5.
m=1: baseline 103,R=100,c=1.
t=1: min(1,100)=1,total_b=1,R=99.
t=2: min(1,99/2=49)=>1,total_b=2,R=97.
t=100: min(1,0)=0 => total_b=2, points=1*4+2=6.
m=2: baseline 206>203 infeasible. So ans=6.
Check if we can get 6 points: full task gives 4 points, cost 103, leaving 100. With 100 we can solve subtask1 and subtask2 of the other task? Actually each additional subtask: we can take at most 1 extra per type because c=1. So we can take subtask1 (cost1) and subtask2 (cost2) -> total extra points 2, total time 103+3=106? Wait 1+2=3, plus 103 =106, but M=203, we have 100 left, not 3. Actually we used 103 for full task, left 100. Then we can take subtask1 (1) and subtask2 (2) => total additional time 3, total 106, well within 203. But we could also take subtask3? That costs 100, but if we take subtask3 we cannot take both 1 and 2 because time 1+2+100=103, total would be 206>203, but we only have 100 left after full task? Actually if we take subtask3 (100) we cannot take subtask1 or 2 because would exceed? After full task, time left 100; if we take subtask3, time used 100 exactly, no room for 1 or 2. That yields extra points 1, total 5. So best extra is 2 points from subtask1 and 2. So total 6. Could there be a solution with 0 full tasks but many subtasks giving 6? With m=0 we got 5 only. So 6 is max.
Now, what about a scenario where it's better to not fully complete any task because the extra point from full tasks is not worth the time needed to collect all subtasks? But the extra point is free if you already have the subtasks. However, to get the extra point you need to have all k subtasks for a task. So you might need to solve some expensive subtasks that you wouldn't otherwise solve. For example, if one subtask is extremely expensive, you might avoid it and instead solve many cheap subtasks across many tasks. In such case, the optimal might have m=0. That's captured.
Now, we need to confirm the greedy for b_j is correct when we have a cap per type. Since profit per item is 1, maximizing count under weight constraint and per-item quantity caps is indeed a bounded knapsack where all profits are equal. The greedy by taking lightest items first is optimal: This is essentially the "maximum number of items" problem which is like: sort by weight, take as many as possible. It's like we want to minimize total weight for a given number of items, so to maximize count, we want to include the lightest items. Formal proof: Suppose we have an optimal solution that does not take the maximum possible number of the lightest item. Then there exists some light item not taken to its cap while a heavier item is taken. Replacing one heavier item with one light item (if capacity permits) yields same count with less or equal weight, which cannot worsen feasibility, and may free up weight to add more items. By repeating, we can transform to the greedy solution.
Thus algorithm is solid.
Now, we can implement.
But one more nuance: The problem statement says "Polycarp can solve subtasks in any order." This means he can interleave subtasks from different tasks, but it doesn't affect the total time. So our model holds.
Now, I'll produce final answer with explanation.
But wait, let's re-check the input constraints: n, k up to 45, M up to 2e9, t_j up to 1e6. Our algorithm uses long long.
Now, potential overflow in multiplication: m * T, where T sum up to k * 1e6 = 45e6 = 45,000,000. m up to 45 => product up to 2,025,000,000. That's < 2^31? 2^31-1 = 2,147,483,647, so fits in 32-bit signed. But safer to use long long.
Now, let's think if any edge case like t_j very large and M very small; greedy just returns 0.
Now, is there any scenario where the greedy for b_j for a given m might not be optimal because we might want to save time for later more efficient items? But since we process in increasing order, we are using the cheapest first, which is efficient.
But wait, suppose we have t = [3,4,5] and caps c = 2, R = 10. Greedy: take 3: 2 (cost6,R=4), take 4: 1 (cost4,R=0) => total_b=3. Could we instead take 3:1 (cost3,R=7), then 4:1 (cost4,R=3), then 5:0 => total_b=2, worse. Or 3:2,R=4, then 5:0 =>2? no. So greedy best.
Another: t=[3,5,6], caps 2, R=11. Greedy: 3:2(c6,R5), 5:1(c5,R0) => total_b=3. Alternative: 3:1(R8),5:1(R3), then? total_b=2. So yes.
Thus fine.
Now, confirm with a scenario where caps differ? Here caps are all equal to n - m. So they are the same. So even simpler: we have c copies of each type, total of c*k possible additional items, but we can only take up to c per type. So greedy: take as many as possible from smallest t, up to c each. That's optimal.
Thus algorithm is O(n*k). Could we compute more efficiently by precomputing prefix sums and binary search? Not needed.
Now, I think we have the solution.
But before finalizing, let's test with more random data manually (maybe small n,k brute force enumeration to ensure). Since I'm not coding, I'll try to enumerate for a small case manually to double-check the reduction.
Consider n=2, k=2, t=[1,3]. T=4.
All possible selections: For each task, we can choose any subset of subtasks. Since tasks are symmetric, the state can be described by counts of each subtask solved: a1, a2 (0<=a1,a2<=2). Points = a1+a2 + min(a1,a2). Time = a11 + a23.
We can enumerate all (a1,a2) and compute points and time, see max points <= M.
List:
(0,0): points0, time0
(0,1): points1, time3
(0,2): points2, time6
(1,0): points1, time1
(1,1): points2? Actually a1=1,a2=1 => sum=2, min=1 => total=3? Wait sum a_j = 2, min=1 => total=3. But is that correct? Two subtasks solved, but are they in same task or different? If we have a1=1 and a2=1, we can assign both to the same task? That would make that task fully solved (since both subtasks solved) => points = k+1 =3. The other task unsolved. So yes points=3. Time = 1+3=4.
(1,2): a1=1,a2=2 => sum=3, min=1 => points=4. Time=1+6=7. Can we achieve? a2=2 means both tasks have subtask2 solved; a1=1 means one task has subtask1 solved. The task that has both subtasks solved is fully solved (points 3), and the other task has only subtask2 solved (points 1). Total 4. Time=11+23=7.
(2,0): points2, time2
(2,1): a1=2,a2=1 => sum=3, min=1 => points=4. Time=2+3=5.
(2,2): sum=4, min=2 => points=6, time=2+6=8.
Now, our algorithm: sorted t = [1,3].
For each m:
m=0: c=2,R=M.
M=0: points=0.
M=1: take t=1: min(2,1)=1,R=0 => points=1 -> ans=1. Check enumeration: feasible with (1,0) points1, or (0,0) points0 => max 1. OK.
M=2: m=0: t=1: min(2,2)=2? Actually R=2, 2//1=2, take=2,R=0 => points=2. m=1 infeasible. Enumeration: (2,0) points2, time2 => yes.
M=3: m=0: t=1: min(2,3)=2, total_b=2,R=1 => points=2. m=1: need 4>3 infeasible. But enumeration: (0,1) points1, (1,0) points1, (2,0) points2 but time2, but we can do (1,0) plus something? Actually best is (2,0) points2 time2 or (0,1) points1 time3. Could we get points3? (1,1) needs time4 >3. So max=2. Good.
M=4: m=0: t=1:2, total_b=2,R=2? Wait R=4, after taking 2 of 1: cost2, R=2; then t=3: min(2,0)=0 => points=2. m=1: R=0, c=1, total_b=0 => points=4? Actually m=1 gives baseline 4, R=0, points=13+0=3? Wait k=2 so k+1=3, points=3. But enumeration says (1,1) gives 3 points time4, (2,0) gives 2 points time2, (2,1) gives 4 points time5>4, not allowed. So max=3. Our algorithm gave m=1 points=3. But m=0 gave 2. So max=3. Good.
M=5: m=0: t=1:2,R=3; t=3: min(2,1)=1, total_b=3,R=0 => points=3. m=1: R=1, c=1, t=1: min(1,1)=1, total_b=1,R=0 => points=3+1=4? Wait m=1 points = m(k+1)+total_b = 13+1=4. But is that feasible? Time: baseline 4 + 11 =5, points=4. Enumeration: (2,1) gives points4 time5. So yes max=4. Good.
M=6: m=0: t=1:2,R=4; t=3: min(2,1)=1, total_b=3,R=1 -> points=3. m=1: R=2, t=1:1,R=1; t=3:0 => total_b=1, points=4. m=2: baseline 8>6. So ans=4. Enumeration: (2,1) time5 points4, (1,2) time7>6, (0,2) time6 points2, (2,2) time8>6. So max=4. Good.
M=7: m=0: t=1:2,R=5; t=3: min(2,1)=1, total_b=3,R=2 => points=3. m=1: R=3, t=1:1,R=2; t=3:0 => total_b=1, points=4. m=2: baseline8>7. So ans=4. But enumeration: (1,2) time7 points4, (2,1) time5 points4, (2,2) time8>7. So 4 max. Good.
M=8: m=0: t=1:2,R=6; t=3:2? After t=1 take 2, R=6; t=3: min(2,6/3=2)=2, total_b=4,R=0 => points=4. m=1: R=4, t=1:1,R=3; t=3:1 => total_b=2,R=0, points=3+2=5. m=2: baseline8, R=0, points=2*3=6. So ans=6. Enumeration: (2,2) gives 6 points time8. So good.
Thus our algorithm matches brute force for this case.
Now, test n=3, k=2, t=[1,3] to see more. n=3, T=4.
Enumerate a1,a2 from 0..3.
Points = a1+a2+min(a1,a2).
Our algorithm: sorted t = [1,3].
Loop m 0..3.
M varies.
Test M=10.
Our algorithm: m=0: c=3,R=10. t=1: min(3,10)=3, total_b=3,R=7. t=3: min(3,7/3=2)=2, total_b=5,R=1. points=5.
m=1: baseline4,R=6,c=2. t=1: min(2,6)=2,total_b=2,R=4. t=3: min(2,4/3=1)=1,total_b=3,R=1. points=13+3=6.
m=2: baseline8,R=2,c=1. t=1: min(1,2)=1,total_b=1,R=1. t=3: min(1,0)=0 => points=23+1=7.
m=3: baseline12>10 infeasible. ans=7.
Now, enumeration: What are possible (a1,a2) with time <=10? Compute all:
(0,0):0
(1,0):1
(2,0):2
(3,0):3
(0,1):3
(0,2):6
(0,3):9
(1,1): time4, points=1+1+1=3? Wait sum=2 min=1 =>3, same as earlier. Actually (1,1): a1=1,a2=1 => sum=2, min=1 =>3, but we have three tasks; we could assign both solved subtasks to the same task for full solve? But a1=1 means exactly one task has subtask1 solved; a2=1 means exactly one task has subtask2 solved. If they are the same task, then that task is fully solved, and the other two tasks have none. That's allowed. So points= (full task)3 + other tasks 0 =3. Alternatively, if they are different tasks, we would have two partial tasks each with one subtask, total points 2, but we can always assign to maximize points, i.e., assign both to same task, giving 3. So points=3. Good.
(1,2): a1=1,a2=2 => time=1+6=7, sum=3, min=1 => points=4? But is that correct? With a2=2, both tasks? Actually n=3. a1=1 means one task has subtask1. a2=2 means two tasks have subtask2. To maximize full tasks, we want to pair a subtask1 with a subtask2 to form a full task. Since we have only one subtask1, we can form at most one full task. So full tasks = min(1,2)=1. The remaining subtasks: after using one of each for the full task, we have a1 left 0, a2 left 1. So we have one task with just subtask2 (point 1). Total points = (k+1)1 + 1 = 3+1=4. Yes. So points=4. Good.
(1,3): a1=1,a2=3 => time=1+9=10, sum=4, min=1 => points=5? Let's compute: full tasks = min(1,3)=1. After using one of each, left a1=0,a2=2 => two tasks each with subtask2 only -> 2 points. Total=13+2=5. So points=5.
(2,1): a1=2,a2=1 => time=2+3=5, sum=3, min=1 => points=4? full tasks=1, leftover a1=1,a2=0 -> 1 point -> total 4.
(2,2): time=2+6=8, sum=4, min=2 => points=6? full tasks=2, leftover? After using 2 of each, leftover 0, so points=23=6.
(2,3): time=2+9=11 >10 not allowed.
(3,1): time=3+3=6, sum=4, min=1 => points=5? full tasks=1, leftover a1=2,a2=0 -> 2 points -> total 5.
(3,2): time=3+6=9, sum=5, min=2 => points=7? full tasks=2, leftover a1=1,a2=0 -> 1 point -> total 23+1=7.
(3,3): time=3+9=12 >10.
So max points for M=10 is 7 from (3,2) time9 points7, or (2,2) time8 points6, (3,2) gives 7. Yes. So our algorithm gave 7. Good.
Thus the algorithm works.
Now, consider a case where times are not sorted initially; we sort them. That is fine.
Now, I'd like to also think about the possibility of a more efficient approach: Since n and k are small, we could also do DP over tasks. But the greedy is simpler.
Now, we should check the constraint: "Polycarp can solve subtasks in any order." That might affect if we need to consider the order of solving to get the extra point? The extra point is given if the task is completely solved, irrespective of order. So no.
Now, we have to write the solution.
But wait, before finalizing, we must ensure that our reduction to a_j and min a_j is indeed correct when we consider that we can assign subtasks to tasks arbitrarily to maximize points. Yes.
Now, I'll produce final answer.
Implementation details in code:
- read n, k, M.
- read array t of length k.
- sort t.
- compute sumT = accumulate(t.begin(), t.end(), 0LL).
- initialize ans = 0.
- for m in range(0, n+1):
if m * sumT > M: break
remaining = M - m * sumT
cap = n - m
total_b = 0
for time in t:
if remaining < time: break? but we might still have larger times that are affordable? Actually if remaining < smallest time, we break. But since we iterate in sorted order, if remaining < current time, all later times are >= current time, so can't take any. So we can break.
take = min(cap, remaining // time)
total_b += take
remaining -= take * time
points = m * (k + 1) + total_b
ans = max(ans, points)
- print ans
Edge case: m can be n, then cap=0, total_b=0.
Now, we need to use long long for remaining and ans.
Now, I'll consider if there is any scenario where we might want to take a subtask of a larger time even if we can't take a smaller one because we have reached cap for the smaller. In greedy, we take as many of the smaller as allowed (up to cap). If cap is reached, we move to next. That's correct.
Now, what if we have t = [1,1,100] and cap=1, R=100? Actually cap=1 for each type. Then greedy: t=1 (first 1): take 1, R=99; next t=1 (second 1): cap=1, take 1, R=98; t=100: take 0? Actually 98<100, so total_b=2. That's max because we can only take 2 items max (since cap per type=1, total items max =3 but we have only 2 cheap ones; but we could take the 100 instead of one cheap? That would give 1 item instead of 2. So 2 is max.
Now, the algorithm seems correct.
Now, test with given examples: example1 yields 6, example2 yields 7.
Thus final solution.
Now, let's produce answer.
But wait, is there any need to consider the possibility of solving subtasks across tasks in a way that we cannot realize the min a_j grouping because the tasks are distinct? Actually tasks are distinct, but we can assign solved subtasks to any task arbitrarily. There's no restriction that we have to decide which task gets which subtask before solving; we can decide after. So we can always group.
Now, I will produce final code.
But I realize that we could also solve by a DP on the number of fully solved tasks and then use greedy for the rest, which is what we do.
Now, let's think about the possibility of an off-by-one: The points from fully solved tasks are k+1, not k+ something else. Our formula m*(k+1) for baseline is correct.
Now, one more test: n=45, k=45, M=2e9, t_j all = 1e6. Then T=45e6. m max = floor(M/T)= floor(2e9 / 45e6) ≈ 44.44 so m=44? 4445e6=1.98e9, remaining 20 million. c=n-m=1. Then we can take one extra subtask? But extra subtasks: each costs 1e6, we can take at most c=1 per type. With sorted t all equal, we can take up to 1 of each, but we only have 20 million, so we can take 20 of them? Wait if all t equal, the order doesn't matter. cap=1 per type, so max total_b = number of types we can take. Since each costs 1e6, with R=20e6, we can take up to 20 types. But we have k=45 types, so we can take min(45, 20) = 20. So total_b=20. So points = 44(46) + 20 = 2024 + 20 = 2044? Actually k+1=46, 4446=2024, +20=2044. Let's compute manually: m=44 full tasks give 4446=2024 points, time 4445e6=1.98e9, remaining 20e6 allows 20 extra subtasks, each gives 1 point, total 2044. Could we do better with m=43? Then baseline 4345e6=1.935e9, remaining 65e6, c=2 per type (n-m=2). With all t equal, we can take up to 2 per type, total possible extra = 245=90, but we only have 65e6, so can take 65 items? Actually each costs 1e6, so we can take up to 65 items. But we are capped per type at 2, so maximum we can take is 245=90, but we can only afford 65. So we can take 65 items. So points = 4346 + 65 = 1978 + 65 = 2043. That's slightly less than 2044. So m=44 better. m=45: baseline 4545e6=2.025e9 > M, not feasible. So ans=2044. This seems plausible.
Now, what about m=0: c=45, can take up to 45*45=2025 items? Actually each type up to 45, total capacity 2025, but R=2e9, each cost 1e6, so can take up to 2000 items (since 2e9/1e6=2000). But we are capped per type: each type max 45, so 45 types * 45 = 2025 >2000, so we can take 2000 items, points = 2000. That's less than 2044. So m=44 better.
Thus algorithm seems to handle this.
Now, consider t with varied values, the greedy for extra b_j might not be able to achieve the maximum count because of per-type caps? But we already argued it does.
Now, I'm satisfied.
Now, implement.
But before final code, I want to reflect on the possibility that there could be a better approach than enumerating all m because m is at most 45, fine.
Now, we need to output the answer as an integer.
Now, double-check the note: In first example they got 6. Our algorithm yields 6.
Thus answer.
Now, final code in C++.
But also be mindful: The input t_j may not be sorted, so we sort.
Now, potential pitfall: The greedy for b_j for a given m uses R and cap, but if we break early when remaining < time, we might miss later times that are equal or larger? Actually if remaining < current time, then all later times are >= current time, so cannot take any, break is safe. But it's not necessary, we can continue with take=0. But breaking improves speed.
Now, edge: time could be 0? No, t_j >=1.
Now, I will produce final solution.
Thus the answer is to implement as described.
Now, I will produce the final message with explanation and code.
```cpp
#include
#include
#include
#include
using namespace std;
int main() {
int n, k;
long long M;
cin >> n >> k >> M;
vector<int> t(k);
for (int i = 0; i < k; ++i) {
cin >> t[i];
}
// sort subtask times to apply greedy later
sort(t.begin(), t.end());
long long total_time_per_task = accumulate(t.begin(), t.end(), 0LL);
long long best = 0;
// try every possible number of fully solved tasks
for (int m = 0; m <= n; ++m) {
// base cost for the m complete tasks
long long base_time = m * total_time_per_task;
if (base_time > M) break; // larger m will be even worse
long long remaining = M - base_time;
int cap = n - m; // how many extra subtasks of each type we may add
long long extra = 0; // total number of extra subtasks b_j
for (int time : t) {
if (remaining < time) break; // cannot take any more
long long take = min((long long)cap, remaining / time);
extra += take;
remaining -= take * time;
}
long long points = m * (k + 1LL) + extra;
if (points > best) best = points;
}
cout << best << endl;
return 0;
}