We need to solve a problem: We have n turns. Each turn we are given a set of cards. For each turn we can choose a subset (sequence) of these cards to play, with total cost ≤ 3 (per turn). Cards not played are discarded. The overall sequence of cards played across turns matters because every 10th card (i.e., the 10th, 20th, 30th, ...) deals double damage. So the ordering across turns matters: we can choose the order of cards within a turn, and also the order across turns? Actually, turns happen sequentially: we start with turn 1, then turn 2, etc. So the global sequence of played cards is the concatenation of sequences chosen each turn, in the order of turns, and within a turn we can order the cards arbitrarily. So we have control over which cards are played in which turn and their order within the turn.
Goal: maximize total damage, where each card's damage d is possibly doubled if its position is a multiple of 10.
We can choose to play up to 3 cost per turn, and each card cost is 1, 2, or 3.
Thus each turn we can play a combination of cards whose total cost ≤ 3. Since costs are integers, possible total cost values are 0,1,2,3. For each turn we can choose any subset of the given cards such that sum(c) ≤ 3. The order of cards within that subset can be arbitrary. So effectively, each turn we can select a multiset (set) of cards of total cost ≤ 3 from the available ones. Since cards are distinct, we choose a subset.
Because the global ordering of cards across turns matters for the 10th doubling, we might want to schedule high-damage cards to be at positions that are multiples of 10. But we also have the constraint that each turn's total cost ≤ 3, so we cannot play too many cards per turn. Also, we can decide to not play some cards even if we could.
The total number of cards across all turns is up to 2e5, n up to 2e5, but total ki ≤ 2e5.
We need to compute maximum total damage.
Let's analyze the doubling effect: Every 10th card double damage. This is reminiscent of scheduling tasks with a reward that depends on position modulo 10 (only positions ≡0 mod10 get doubled). So essentially, we have a sequence of played cards; we sum damage, but damage of a card at position i is either d (if i mod10 != 0) or 2d (if i mod10 == 0). So total damage = sum d_i + sum_{i: i mod10=0} d_i. So it's base sum plus extra sum for those positions.
We can think of it as each card gives base damage d, and if placed at a multiple of 10, it gives an extra d. So we want to maximize base sum (just sum of all played cards) plus extra from cards placed at those positions.
Since base sum is just sum of damages of selected cards (regardless of order), we want to maximize the total base damage subject to per-turn cost constraints. But note: we might choose to not play some cards if they are low damage, to free cost for future turns? Actually, the per-turn cost constraint is only within that turn; not playing a card doesn't affect future turns because we cannot carry over cards; each turn's cards are only available that turn. So the decision of which cards to play each turn is independent if we ignore the doubling effect? Wait, the doubling effect links turns because the order across turns matters: the position of a card depends on how many cards we played in previous turns. So the decision of how many cards to play each turn (and which ones) influences the positions of later cards. Thus we have a global scheduling problem: we need to decide, for each turn, which cards to play and in which order, to maximize total damage with the doubling every 10th card.
We need to consider that we can also reorder cards within a turn arbitrarily; so effectively we can arrange the global sequence such that the ordering among cards from the same turn is free.
But the key is that the per-turn cost constraint restricts the number and type of cards we can play each turn. The total cost per turn is ≤3, and each card cost is 1,2, or 3. So the maximum number of cards we can play in a turn is at most 3 (if all cost 1), but could be 1 if cost 3. So each turn we can play at most 3 cards, but also limited by cost sum.
We can treat each turn as a small knapsack: we can select a subset of its cards with total cost ≤3. For each possible total cost used (0,1,2,3), we can have various total base damage and also we might want to know something about the individual cards because their ordering relative to the 10th position may require that we consider their damages for potential extra. However, we can rearrange cards within a turn, so we can decide which cards from the turn occupy which positions relative to the global count. Since we can order cards arbitrarily within the turn, we can effectively decide which card from that turn gets placed at which relative position among the cards played in that turn. And the global position is determined by cumulative count.
So perhaps we can treat the process as we go sequentially through turns. At each turn, we know the current position modulo 10 (or the number of cards played so far mod 10). Then we decide which cards to play this turn, and also we decide the order among them. The extra damage from a card played at a position that becomes a multiple of 10 will be its damage d (extra). So if we know that among the cards we play this turn, some will fall on positions that are multiples of 10, we can allocate high-damage cards to those slots to maximize extra.
Thus, each turn, we can decide to play a set of cards, and we can also decide which of them get the "bonus" positions, depending on how many cards we have played so far (mod 10) and how many we will play this turn.
Let current total cards played before this turn = t (so next position is t+1). We know t mod 10. Let r = t mod 10, i.e., the position of the next card within the decile. The positions within this turn will be t+1, t+2, ..., t+m where m is number of cards played this turn. Among these positions, those that are multiples of 10 correspond to indices i such that (t + i) mod 10 = 0. That is, i ≡ -t mod 10 = (10 - r) mod 10? Actually, if r = t mod 10, then the first position is t+1, so (t+1) mod 10 = (r+1) mod 10. The multiples of 10 occur when (t + i) mod 10 = 0 => i ≡ -t mod 10 = (10 - r) mod 10. So the set of i (1-indexed within turn) that get doubled are: if we let offset = (10 - r) % 10, then i = offset if offset ∈ [1,m]; then i = offset+10, offset+20, ... but since m ≤ 3 (max cards per turn), at most one card per turn can be a multiple of 10, because the gap between multiples of 10 is 10, and we play at most 3 cards per turn. So within a turn, we can have at most one card that gets doubled. Indeed, with m ≤ 3, there can be at most one position that is a multiple of 10. So that's a crucial simplification: Each turn, at most one of its played cards can be a bonus card.
Thus, the extra damage from this turn can be either 0, or if we have a bonus slot, we can assign the highest damage card among those played to that slot to get extra d (the extra is the same d as base, so total becomes 2d). But note: we could also choose to put a lower damage card in that slot if we want to save high damage for later? However, because the bonus is extra damage on top of base, we always want to maximize the extra damage subject to constraints: the bonus extra is additive based on the card's damage. Since we are going to play a set of cards anyway, the base damage sum is independent of which card gets the bonus. So for a given set of cards we play, the extra we can gain is the maximum d among those cards if there is a bonus slot, else 0. Actually careful: If there is a bonus slot, we can choose any one of the played cards to occupy that position. The other cards occupy non-bonus positions. So total damage = sum_{cards in set} d + (d_max if bonus slot exists). So we want to maximize sum(d) + (bonus? d_max). So the bonus is essentially an extra reward equal to the maximum damage among the cards played in that turn, provided that the turn contains a multiple-of-10 position.
Thus, the problem reduces to: We have a sequence of turns. In each turn i, we have a set of cards (each with cost and damage). We can choose a subset of total cost ≤3 to play. The total base damage contributed is sum of damages of chosen cards. Additionally, if the number of cards we play in this turn causes that among the positions from current total+1 to current total+m we have a position that is a multiple of 10, then we get an extra equal to the maximum damage among the chosen cards for that turn. If multiple such positions? Not possible as argued. So at most one extra per turn.
Thus, the overall optimization: We need to decide for each turn, based on the current total modulo 10, whether to aim to have a bonus slot in that turn (i.e., schedule such that the turn includes the 10th position) and if so, choose a subset that includes a high-damage card for the bonus, possibly sacrificing total base damage? Because the subset choice is subject to cost constraint. Also, we could also decide to not play some cards to shift the bonus to a later turn.
We need to maximize total sum over turns: base sum + extra from bonus turns.
Let total cards played before turn i be T_{i-1}. Then r = T_{i-1} mod 10. Let m_i = number of cards we decide to play this turn (0 ≤ m_i ≤ something, but determined by which subset we select). The bonus occurs if there exists i in [1, m_i] such that (T_{i-1} + i) mod 10 = 0. This condition is equivalent to: The interval (r+1, r+2, ..., r+m_i) modulo 10 contains 0. Since r from 0 to 9, and m_i ≤ 3, it's easy to determine condition based on r and m_i.
Specifically, let r = T_{i-1} % 10. Then the next positions mod 10 are (r+1) % 10, (r+2)%10, ... up to (r+m_i)%10. We have a bonus if 0 appears in that set, i.e., if (r + x) % 10 = 0 for some x in 1..m_i => x ≡ -r mod 10, and x ≤ m_i. Since -r mod 10 is between 1 and 10 (if r=0 then -0=0 mod10, but x starts at 1, so we need x ≡ 0 mod10? Actually if r=0, then (0+ x) %10 = 0 => x ≡0 mod10. The smallest positive x would be 10, but m_i ≤3, so impossible. So for r=0, no bonus in this turn regardless of m_i (since we need x=10,20,...). So for r=0, the first card of turn is at position t+1 where t mod10=0 => t+1 mod10=1, so positions 1,2,... within turn will be 1,2,3; none is 0 mod10. So indeed no bonus if r=0.
For r=9: r+1 = 0 mod10 => first card gets bonus. So if r=9 and m_i >=1, then bonus occurs (on the first card). For r=8: r+1=9, r+2=0 mod10 => if m_i>=2, bonus on second card. For r=7: r+1=8, r+2=9, r+3=0 => if m_i>=3, bonus on third card. For r=6: r+1=7, r+2=8, r+3=9 => no 0 within ≤3. So r=6 gives no bonus. For r=5: r+1=6, r+2=7, r+3=8 => no. r=4: 5,6,7 => no. r=3: 4,5,6 => no. r=2: 3,4,5 => no. r=1: 2,3,4 => no. r=0 we already did. So bonus occurs only when r ∈ {9,8,7} and m_i is at least (10 - r) (i.e., for r=9 need m_i>=1, r=8 need m_i>=2, r=7 need m_i>=3). Actually for r=7 need m_i≥3 to have third card position = 0 mod10? Check: r=7 => t mod10=7, so positions: t+1 mod10=8, t+2=9, t+3=0 => yes. So only those three residues allow bonus, and only if we play enough cards to reach that position.
If r=6, the positions would be 7,8,9; none is 0. r=5->6,7,8; r=4->5,6,7; r=3->4,5,6; r=2->3,4,5; r=1->2,3,4; r=0->1,2,3. So indeed only r=9,8,7.
Thus, the bonus occurrence depends on the current residue and the number of cards played this turn.
We can think of state as the current total modulo 10 (0..9). At each turn, we have a set of options: choose a subset of cards (with total cost ≤3) to play, which yields a certain number of cards m (which is the size of the subset, not sum of costs but number of cards; but cost sum constraint matters) and a total base damage S, and a max damage M among those cards (if any). Then the new total modulo 10 becomes (r + m) mod 10. And the damage gained from this turn is S + (1 if bonus? then M else 0). Where bonus = ( (r >=7 && m >= (10 - r) )? Actually as derived: bonus occurs if r in {7,8,9} and m >= (10 - r). For r=9 need m>=1, for r=8 need m>=2, for r=7 need m>=3.
So DP over turns with state = residue r (0..9). At each turn, we need to consider all possible subsets of the given cards with total cost ≤3. For each such subset, we know m (size) and S (sum d) and M (max d). Then update DP[r'] = max(DP_prev[r] + S + bonus*M). But the number of subsets per turn could be large if k_i is large. However, k_i total across all turns ≤ 2e5, so per turn we can process efficiently.
Since cost per card is only 1-3, and total cost limit is 3, the possible subsets are limited by small knapsack. We can categorize cards by cost. For each turn, we have some number of cost 1, cost 2, cost 3 cards. The best subsets for each possible (cost used, number of cards, max damage, sum damage) can be precomputed efficiently by taking the best cards.
But careful: The subset determines m (number of cards) and total cost ≤3. However, m is not directly tied to total cost; e.g., cost could be 3 from one card (m=1) or from three cost-1 cards (m=3). So we need to consider subsets with different combinations. However, we can enumerate all possibilities of how many cards of each cost we take, because the total cost constraint is small.
But we also need to know S and M. For a given combination (e.g., take a set of cards), the contribution depends on sum of damages and max among them. So to maximize DP, we need for each possible (m, total cost used maybe not needed but to ensure ≤3) the maximum (S + bonus*M) given that we can choose a subset with that m (and some cost composition) but also we need to consider that bonus depends on M only if bonus applies; otherwise we just need S.
However, DP state only cares about r and we will transition with m and gain = S + (bonus? M). So for each turn, we need to compute for each possible m (0,1,2,3) and each possible "has bonus" condition (which depends on r) the best gain for that m? But bonus also depends on whether we get a bonus, which depends on r and m. So for each r (0..9) we need to consider all possible actions (subset) and compute new residue r' = (r+m) mod 10 and gain = S + (bonus? M). Since r is small (10), we can compute transitions for each r separately.
Now, for a given turn, we need to compute, for each possible m from 0 to 3 (and also for each possible composition, because different subsets with same m may yield different S and M), the best (S + something) for both cases (bonus needed or not?). But the bonus factor multiplies M. So essentially, for a given m, we need to know:
- The maximum total base damage S for any subset of total cost ≤3 that has exactly m cards? But note: m is number of cards; total cost may vary; it just needs to be ≤3. So we need to consider subsets where total cost ≤3 and size = m. However, maybe subsets with same m but different cost composition could yield higher S. So we can precompute, for each possible m (0,1,2,3), the best possible pair (S, M) that maximizes something? But since the gain is S + λ * M where λ is either 0 or 1 (bonus or not), we need to maximize S + λ*M.
Thus, for a given turn, we might want to consider all subsets, but we can summarize by computing, for each subset (characterized by its size m and some other attribute), the best possible value of (S, M) or rather the best S + λM for each λ.
But because λ is either 0 or 1, we can compute two things for each possible m and for each possible cost composition? However, DP will consider all subsets directly if we can enumerate all feasible subsets efficiently. Since total cost ≤3 and each card cost small, the number of possible subsets per turn is not huge? Actually, if there are many cards, but we can only choose up to 3 cards because cost limit and also because m ≤3? Not necessarily: Could we choose more than 3 cards? If we have cards of cost 1, we could take up to 3 cards (since total cost ≤3). So maximum number of cards is 3. So the subset size is at most 3. That means the number of subsets we need to consider is bounded by number of ways to pick up to 3 cards from the k_i cards, which could be huge if k_i large (like 1e5). But we don't need to consider all combinations because we only care about the best subsets in terms of sum and max.
Because each card has two attributes (c,d) and we are limited to total cost ≤3, we can think of it as: choose at most 3 cards with total cost ≤3. The best subsets can be found by sorting cards by damage and considering best options per cost. Since we only care about up to 3 cards, we can consider the best few cards of each cost.
Specifically, we can consider all combinations of:
- 0 cards: trivial.
- 1 card: any card with cost ≤3. The best for each cost? But we need best S and M = d. For m=1, S = d, M = d. So for each possible cost (1,2,3), the best d is relevant.
- 2 cards: two cards with total cost ≤3. Options: both cost 1; or one cost1 and one cost2 (sum=3). Could also be cost1+cost1? Actually two cost1 is cost2; cost1+cost2=3; cost1+cost1 is allowed (cost2). Cost2+cost2=4 >3 not allowed. Cost3+any >3 (except cost0 none) not allowed. So the only valid 2-card combinations: (1,1) and (1,2). Also (2,1) same. Could there be (1,1,?) no that's 2 cards. So we need best pairs for these cases.
- 3 cards: total cost ≤3 => only possible if all three are cost1 (total cost=3). Also could be (1,1,1) only. No other combos because cost1+1+2=4>3. So only three cost1 cards.
Also we could have a single card of cost3; two cards of (1,2); two cards of (1,1); three cards of (1,1,1). That's all possible sets in terms of cost composition. Also maybe a single card of cost2 or cost1. Also possibly play 0 cards (do nothing). So the number of card-type combinations is limited. But within each combination, we want the subset that maximizes S + λM.
For λ=0 (no bonus), we just want to maximize S (total damage) under cost constraint. For λ=1 (bonus), we want to maximize S + M.
Thus, for each turn, we can compute:
- best0: maximum total base damage S achievable with any subset (including possibly 0 cards) with total cost ≤3.
- best1: maximum value of S + M achievable with any subset (cost ≤3), where M is max damage in that subset, and S is sum.
But careful: For λ=1, we need to consider that the bonus is only applied if the turn actually yields a bonus slot (i.e., condition holds). In DP, when we are at residue r, if the chosen subset yields m cards, then we get gain = S + (bonus? M). So if the condition is satisfied (bonus true), we want to add S + M; else we add S. So it's not simply that we always want to use best1 or best0; because the subset that yields max S might be different from the subset that yields max S+M, and also m matters for the condition and for updating residue. So we need to consider for each possible m and each possible (S,M) combination that are optimal in some sense for that m, because m determines the new residue and the bonus condition.
Thus, we need to consider actions characterized by (m, S, M). For each m (0,1,2,3), we need the best possible S for cases where we don't care about M? But if bonus is false, gain = S, so we want max S for that m (subject to cost constraint). However, could there be a situation where for the same m, a subset with lower S but higher M might be better if bonus is true? For bonus true, gain = S + M, so we want to maximize S+M for that m (subject to cost constraint). However, note that m influences the condition; but given r, the bonus condition depends on m and r. So for each r, we need to consider each possible m that is allowed (i.e., there exists a subset with that m and cost ≤3). Then for that m, we need the maximum gain for that r: if bonus condition holds, we use best value of S+M for that m; else we use best value of S for that m. But is it sufficient to just take the max S for m (no bonus) and max S+M for m (bonus)? Yes, because the gain for the turn depends only on S and possibly M, and we can choose any subset that yields that m. So for each m we should precompute:
- best_sum[m] = maximum total damage sum S achievable with any subset of total cost ≤3 that has exactly m cards? But careful: m is number of cards. However, we also need to ensure that the subset has exactly m cards? Because m determines the transition to new residue. If we choose a subset, its number of cards is m. So we need to consider m exactly. But could there be subsets with same m but different total cost? Yes, but all are allowed as long as total cost ≤3. So for each m, we want the best possible S, and separately best possible S+M.
But is it always optimal to use exactly m cards? Maybe we could play fewer cards than allowed, e.g., m=2 even though there exists a m=3 subset with higher S but maybe that changes residue and affects future bonuses. So we need to consider all possible m values (0 to 3) that are feasible. So we need to compute for each m, the best S and best S+M.
But note: There might be no subset with a given m. For example, if we have no cost-1 cards, then m=3 is impossible because only three cost-1 cards can give m=3; but also maybe one cost3 and one cost0? No cost0. So m=3 only possible if at least three cost-1 cards. Similarly, m=2 possible with (1,1) or (1,2). So need to check feasibility.
Now, within a given m, there could be multiple subsets with different S and M. For the no-bonus case, we only care about S, so we want max S. For the bonus case, we want max S+M. Could there be a scenario where the max S+M for a given m is not achieved by the subset that also gives max S? Possibly yes, so we need both.
But also, note that for m=1, S = d, M = d, so S+M = 2d, and S = d. So max S for m=1 is just max d among cards with cost ≤3 (i.e., max damage card overall in the turn? but must cost ≤3, which all cards are cost 1-3, so it's the overall max damage). Similarly, max S+M for m=1 is 2 * max d. So both can be derived from the max d card.
For m=2, we need to consider two cases: (1,1) or (1,2). For (1,1), we need two cost-1 cards; best sum S would be sum of two highest damage cost-1 cards. Best S+M: since M is max of the two, S+M = sum + max = (d1+d2) + max(d1,d2) = (d1+d2) + max. For two cards, that's essentially sum + max = (d1+d2) + max = if we sort d1≥d2, then S+M = d1+d2+d1 = 2*d1 + d2. So to maximize that, we want the highest d1 (max) and then the next highest d2 maybe? But maybe a pair (cost1, cost2) could give higher S+M even if the max is from cost2. So we need to evaluate both combinations.
For m=3, only (1,1,1). So best S is sum of top three cost-1 damages. S+M = sum + max = (d1+d2+d3) + d1 = 2*d1 + d2 + d3.
Thus, per turn, we can compute:
- For cost-1 cards: we need the top few (up to 3) damages, sorted descending.
- For cost-2 cards: we need the top damage (maybe also second if needed? but only appears in combinations with one cost1, so we need the best cost2 card and possibly the best cost1 card for that pair).
- For cost-3 cards: only single card.
We also need to consider possibility of playing two cards where one is cost3? Not allowed because total cost would exceed 3 if cost3+anything else (cost3+1=4). So cost3 can only be alone.
Similarly, two cost2 cards not allowed (4>3). So indeed only those combos.
Additionally, we might consider playing zero cards (m=0, S=0, M=0, no bonus). That's always an option.
So per turn, the number of possible actions is small: we can compute the best subsets for each m and for each "type": we will have at most a few candidate subsets:
- For m=1: best single card (max d). Also maybe we want to consider different single cards? But only max matters for S and S+M because both are monotonic in d: higher d gives higher S and higher S+M. So only the best single card is needed. However, what if we want to consider m=1 but not the highest damage because we might want to save that high damage for later? But we cannot save cards: each turn, we must decide which cards to play now; any card not played is discarded, so there is no saving across turns. So if we don't play a card in its turn, we lose it forever. Thus, for a given turn, if we decide to play exactly 1 card, we should play the best available (max damage) because playing any other yields lower immediate gain and doesn't affect future (since the card is discarded anyway). So for DP, when we consider actions, we can assume we always choose the best possible subset for given m and objective (S or S+M). Because future turns are independent of which specific cards we play, only depend on number of cards played (m) and the residue. Since the card set per turn is separate, there's no cross-turn resource other than the count. So we can safely take the optimal subset for each m and each objective. There is no reason to play a suboptimal subset for the same m, because it would give lower immediate damage and same future state (since m is the same). So we only need the best possible S for each m (if no bonus) and best possible S+M for each m (if bonus). However, we also need to consider that the best S for m might be achieved with a subset that has a different composition than the best S+M for same m. That's fine; we store separately.
But we also need to consider that for m=2, there might be two types: (1,1) and (1,2). The best S for m=2 would be the maximum of the best sum from two cost1 cards and the best sum from cost1+cost2. But note: the cost1+cost2 combination might yield higher sum even if the two cost1 sum is lower. So we compute best_S_m2 = max( best_two_cost1_sum, best_cost1 + best_cost2 ). However, careful: the best cost1 and best cost2 might be the same card? They are distinct cards, but we must pick distinct cards. Since cards are distinct, we can pick the best cost1 and best cost2 as long as they are different cards; they are from different cost groups, so no conflict. So best_cost1+best_cost2 is valid.
Similarly, for S+M, we need to compute the maximum of (S+M) over both types. For (1,1): S+M = 2d1 + d2 where d1≥d2 are the top two cost1 damages. For (1,2): S+M = (d1 + d2) + max(d1,d2) = if d1 (cost1) ≥ d2 (cost2) then S+M = d1+d2+d1 = 2d1 + d2; else if d2> d1 then S+M = d1+d2+d2 = d1+2*d2. So we need to consider the best pair among cost1 and cost2 that maximizes that expression.
Also, we might consider that for m=1, the best S+M = 2*max_d (any card). So that's fine.
For m=3, only (1,1,1): best S = sum of top three cost1; best S+M = 2*max_d1 + d2 + d3.
Now, what about m=0? That's S=0, M undefined but bonus not applied anyway. So gain =0.
Now, we need DP over turns. DP state: dp[r] = maximum total damage up to current turn, with total number of cards played so far mod 10 = r. Initially before any turns, total cards =0 => r=0, dp[0]=0, others = -inf.
For each turn, we compute the best possible actions for each m (0,1,2,3) that are feasible, with two versions: base_gain_no_bonus[m] = best_S[m], and base_gain_bonus[m] = best_S_plus_M[m] (i.e., S+M). Actually for m=0, both are 0.
But careful: For m where no subset exists, we set it to -inf (or not consider).
Then for each previous state r, we consider each feasible m. Compute new_r = (r+m) % 10. Determine if bonus occurs: if (r >=7 && m >= (10-r)? Actually condition: bonus = (r == 9 && m >=1) or (r == 8 && m >=2) or (r == 7 && m >=3). Equivalently: bonus = (r + 1 <= m? Not exactly). So:
if r == 9 and m >= 1: bonus true.
else if r == 8 and m >= 2: bonus true.
else if r == 7 and m >= 3: bonus true.
else: bonus false.
Then gain = (bonus ? best_S_plus_M[m] : best_S[m]).
But wait: The best_S_plus_M[m] we computed is the maximum S+M over subsets with exactly m cards. However, is it always optimal to use that subset when bonus applies? Yes, because we want to maximize gain for this turn given m, and future depends only on new_r (which is determined by m). So we should choose the subset that gives maximum gain for that m under the condition (bonus or not). So using best_S[m] for no bonus, and best_S_plus_M[m] for bonus.
Thus, transition: dp_new[new_r] = max(dp_new[new_r], dp[r] + gain).
After processing all turns, answer = max_{r} dp[r] (since we can end with any residue). Actually we want total damage after all turns, so answer = max over dp[r] after last turn.
That seems straightforward.
But we must ensure that we correctly handle cases where for some m, there might be multiple subsets that yield same m but different S and M, and maybe the best_S_plus_M[m] subset has a higher S but lower M? Actually we compute the maximum S+M, so that's fine.
Now, we need to compute for each turn the best_S[m] and best_S_plus_M[m] for m=0,1,2,3.
Implementation details:
For each turn, read cards. Separate by cost. For cost 1, collect all damages; sort descending. For cost 2, collect damages; sort descending. For cost 3, collect damages; sort descending.
We'll need:
-
For m=0: best_S[0] = 0, best_S_plus_M[0] = 0 (though bonus never applied to m=0, but we can define).
-
For m=1: best single card: the maximum damage among all cards (cost1, cost2, cost3). Let max1 = max( max(cost1_damage if any, max(cost2_damage if any, max(cost3_damage if any )). best_S[1] = max1. best_S_plus_M[1] = 2 * max1. (since S+M = d + d = 2d)
-
For m=2: we need to consider two cases:
Case A: two cost1 cards. Need at least two cost1 cards. Let a1, a2 be top two damages in cost1. Then sum = a1 + a2; max = a1; S+M = 2*a1 + a2.
Case B: one cost1 and one cost2. Need at least one cost1 and one cost2. Let b1 = best cost1, c1 = best cost2. Then sum = b1 + c1; max = max(b1, c1); S+M = if b1 >= c1 then 2b1 + c1 else b1 + 2c1.
Also note: There's also possibility of two cost? (2,2) not allowed; (1,1) and (1,2) are only valid. Also (3,? ) none. (2,2) cost 4 >3. (1,3) cost 4 >3. So only these.
For best_S[2], we take max of sum_A (if exists) and sum_B (if exists). If only one type feasible, take that.
For best_S_plus_M[2], we take max of (2*a1 + a2) if A exists, and (as computed for B) if B exists.
-
For m=3: only three cost1 cards. Need at least three cost1 cards. Let top three be a1, a2, a3. Then sum = a1 + a2 + a3; S+M = 2*a1 + a2 + a3.
Also, is there any possibility of m=3 with other composition? For instance, one cost1 and one cost2 and one cost0? No. So only this.
So best_S[3] = sum if exists else -inf. best_S_plus_M[3] = 2*a1 + a2 + a3.
But wait: Could we have m=3 with one cost3 and something else? cost3+? would exceed cost. So no.
Thus, per turn, we can compute these values.
Edge Cases: What about playing fewer cards than the maximum possible? For example, we might have m=2 but maybe there is a subset that yields m=2 with higher S+M than the best_S[2]? Actually best_S_plus_M[2] is defined as max S+M over subsets of size exactly 2. So that's covered.
But what about subsets that have total cost ≤3 but not exactly of size m? For each m, we consider exactly m cards. However, could there be a subset with total cost ≤3 that has, say, 2 cards but we want to treat it as m=2. So yes.
Now, is there any scenario where playing a subset with m cards but not of maximum S or S+M might be beneficial because it changes the composition of damages such that the max M is different, and that might affect future? No, future only depends on m (the number of cards), not on which specific cards or their damages. So indeed we only need the best possible gain for each m under the bonus condition.
Thus DP seems correct.
But we need to be careful: The DP transition uses best_S[m] for no-bonus and best_S_plus_M[m] for bonus. However, what about the case where bonus condition holds but we might want to use a subset that does not maximize S+M? That would be suboptimal because we can always choose the best subset for that m, and it yields higher immediate damage without affecting future state (since m same). So we should always take the best for that m.
Thus, DP is simple.
But let's test with example.
Example input:
5 turns:
1: 3 cards: (1,6),(1,7),(1,5)
2: 2 cards: (1,4),(1,3)
3: 3 cards: (1,10),(3,5),(2,3)
4: 3 cards: (1,15),(2,4),(1,10)
5: 1 card: (1,100)
Let's compute per turn bests.
Turn1:
cost1: [7,6,5] sorted descending: a1=7,a2=6,a3=5
cost2: none
cost3: none
m=1: best_S=7, S+M=14
m=2: from two cost1: sum=7+6=13, S+M=27+6=20
(1,2) not possible.
So best_S[2]=13, best_S+M[2]=20
m=3: three cost1: sum=7+6+5=18, S+M=27+6+5=14+11? 2*7+6+5=14+11=25? Actually 14+11=25. Wait 7+6+5=18, plus max (7) =25? S+M = sum + max = 18+7=25. Yes.
So values:
m0: 0,0
m1: 7,14
m2: 13,20
m3: 18,25
Turn2:
cards: (1,4),(1,3)
cost1: [4,3]
m=1: best_S=4, S+M=8
m=2: two cost1: sum=4+3=7, S+M=24+3=8+3=11? Actually 24+3=11.
m=3: not enough cost1 (need 3) -> infeasible.
Also no cost2/cost3.
So m0:0,0; m1:4,8; m2:7,11; m3: -inf.
Turn3:
cards: (1,10), (3,5), (2,3)
cost1: [10]
cost2: [3]
cost3: [5]
m=1: max among 10,3,5 = 10 -> best_S=10, S+M=20
m=2: two options:
(1,1): need two cost1 -> only one cost1, so infeasible.
(1,2): cost1+ cost2: sum=10+3=13, max=10 => S+M=2*10+3=23 (or if 3>10? no). So best_S[2]=13, best_S+M[2]=23.
Could also be (1,3)? cost1+ cost3=15 >3? cost1+ cost3 = 1+3=4 >3 not allowed. So only (1,2) works.
m=3: need three cost1 -> not enough.
Thus m3 infeasible.
m0:0,0; m1:10,20; m2:13,23.
Turn4:
cards: (1,15),(2,4),(1,10)
cost1: [15,10]
cost2: [4]
cost3: none
m=1: max=15 -> S=15, S+M=30
m=2: options:
(1,1): two cost1: 15+10=25, S+M=215+10=30+10=40? Actually 215+10=40.
(1,2): cost1+ cost2: 15+4=19, max=15 => S+M=2*15+4=34? 30+4=34. Or if 4>15? no. So best_S[2]= max(25,19)=25, best_S+M[2]= max(40,34)=40.
m=3: three cost1: need three cost1 but only two -> infeasible.
Also (2,2) not allowed.
So m3 infeasible.
Turn5:
cards: (1,100)
cost1: [100]
m=1: best_S=100, S+M=200.
others infeasible.
Now DP simulation.
Initialize dp[0]=0, others -inf.
Turn1:
r from 0 to 9, but only dp[0]=0, others -inf.
For each m feasible.
We need to compute for each r (only r=0) transitions.
r=0, m=0: new_r=0, bonus? r=0 => no bonus (since need r>=7). gain = best_S[0]=0. dp_new[0]=0.
m=1: new_r=1, gain = best_S[1]=7 (since no bonus). dp_new[1]=7.
m=2: new_r=2, gain = best_S[2]=13. dp_new[2]=13.
m=3: new_r=3, gain = best_S[3]=18. dp_new[3]=18.
So after turn1: dp[0]=0, dp[1]=7, dp[2]=13, dp[3]=18, others -inf.
Turn2:
We need to compute best_S and best_S+M for turn2 as above.
Now consider each previous state r where dp[r] not -inf.
r=0 (dp=0):
- m=0: new_r=0, gain=0 => dp_new[0]=0
- m=1: new_r=1, gain= best_S[1]=4 => dp_new[1]= max(?, 0+4=4)
- m=2: new_r=2, gain= best_S[2]=7 => dp_new[2]=7
- m=3: infeasible
r=1 (dp=7):
- r=1: bonus? Condition: r=1, need r>=7? no. So no bonus.
- m=0: new_r=1 (since (1+0)%10=1), gain=0 => dp_new[1]=max(prev? dp_new[1] currently 4, now 7+0=7 => dp_new[1]=7)
- m=1: new_r=2, gain=4 => dp_new[2]=max(7, 7+4=11) -> dp_new[2]=11 (since 11>7)
- m=2: new_r=3, gain=7 => dp_new[3]=7+7=14
- m=3: infeasible
r=2 (dp=13):
- no bonus (r=2)
- m=0: new_r=2, gain=0 => dp_new[2]=max(11,13)=13? Wait dp_new[2] currently 11, now 13+0=13 => dp_new[2]=13
- m=1: new_r=3, gain=4 => dp_new[3]=max(14,13+4=17) => dp_new[3]=17
- m=2: new_r=4, gain=7 => dp_new[4]=13+7=20
- m=3: infeasible
r=3 (dp=18):
- no bonus (r=3)
- m=0: new_r=3, gain=0 => dp_new[3]=max(17,18)=18
- m=1: new_r=4, gain=4 => dp_new[4]=max(20,18+4=22) => 22
- m=2: new_r=5, gain=7 => dp_new[5]=18+7=25
- m=3: infeasible
Also r= others -inf.
So after turn2: dp[0]=0, dp[1]=7, dp[2]=13, dp[3]=18, dp[4]=22, dp[5]=25, others -inf.
But wait we also had r=1 from m=0? Actually we computed for r=1 m=0 gave new_r=1, gain 0 => dp_new[1] updated to 7. So dp[1]=7. That's consistent.
Turn3:
best_S: m1=10, m2=13, m3=-inf.
Now iterate over states.
Initialize dp_new array to -inf.
From r=0 (dp=0):
- m=0: new_r=0, gain=0 => dp_new[0]=0
- m=1: new_r=1, gain=10 (no bonus) => dp_new[1]=10
- m=2: new_r=2, gain=13 => dp_new[2]=13
- m=3: infeasible
r=1 (dp=7):
- r=1: no bonus
- m=0: new_r=1, gain=0 => dp_new[1]=max(10,7)=10? Actually 7+0=7, so dp_new[1] remains 10.
- m=1: new_r=2, gain=10 => dp_new[2]=max(13,7+10=17) => 17
- m=2: new_r=3, gain=13 => dp_new[3]=7+13=20
- m=3: infeasible
r=2 (dp=13):
- no bonus
- m=0: new_r=2, gain=0 => dp_new[2]=max(17,13)=17 (still 17)
- m=1: new_r=3, gain=10 => dp_new[3]=max(20,13+10=23) => 23
- m=2: new_r=4, gain=13 => dp_new[4]=13+13=26
- m=3: infeasible
r=3 (dp=18):
- no bonus (r=3)
- m=0: new_r=3, gain=0 => dp_new[3]=max(23,18)=23
- m=1: new_r=4, gain=10 => dp_new[4]=max(26,18+10=28) => 28
- m=2: new_r=5, gain=13 => dp_new[5]=18+13=31
- m=3: infeasible
r=4 (dp=22):
- r=4: no bonus
- m=0: new_r=4, gain=0 => dp_new[4]=max(28,22)=28
- m=1: new_r=5, gain=10 => dp_new[5]=max(31,22+10=32) => 32
- m=2: new_r=6, gain=13 => dp_new[6]=22+13=35
- m=3: infeasible
r=5 (dp=25):
- r=5: no bonus
- m=0: new_r=5, gain=0 => dp_new[5]=max(32,25)=32
- m=1: new_r=6, gain=10 => dp_new[6]=max(35,25+10=35) => 35 (equal)
- m=2: new_r=7, gain=13 => dp_new[7]=25+13=38
- m=3: infeasible
others -inf.
After turn3, we have dp:
0:0, 1:10, 2:17, 3:23, 4:28, 5:32, 6:35, 7:38, others -inf.
Turn4:
best_S: m1=15, m2=25, m3=-inf; best_S+M: m1=30, m2=40.
Now process each state. But note: bonus condition: r=7,8,9. So for r=7, if m>=3 (since 10-r=3) we get bonus; for r=8, m>=2; for r=9, m>=1. In our DP, some states have r=7,8,9 might be reachable. Currently we have r=7 = 38, r=8 and 9 are -inf.
Let's compute transitions from each r.
Initialize new dp to -inf.
r=0 (dp=0):
- no bonus
- m=0: new_r=0, gain=0 => dp_new[0]=0
- m=1: new_r=1, gain=15 => dp_new[1]=15
- m=2: new_r=2, gain=25 => dp_new[2]=25
- m=3: infeasible
r=1 (dp=10):
- no bonus
- m=0: new_r=1, gain=0 => dp_new[1]=max(15,10)=15
- m=1: new_r=2, gain=15 => dp_new[2]=max(25,10+15=25) => 25
- m=2: new_r=3, gain=25 => dp_new[3]=10+25=35
- m=3: infeasible
r=2 (dp=17):
- no bonus
- m=0: new_r=2, gain=0 => dp_new[2]=max(25,17)=25
- m=1: new_r=3, gain=15 => dp_new[3]=max(35,17+15=32) => 35
- m=2: new_r=4, gain=25 => dp_new[4]=17+25=42
- m=3: infeasible
r=3 (dp=23):
- no bonus
- m=0: new_r=3, gain=0 => dp_new[3]=max(35,23)=35
- m=1: new_r=4, gain=15 => dp_new[4]=max(42,23+15=38) => 42
- m=2: new_r=5, gain=25 => dp_new[5]=23+25=48
- m=3: infeasible
r=4 (dp=28):
- no bonus
- m=0: new_r=4, gain=0 => dp_new[4]=max(42,28)=42
- m=1: new_r=5, gain=15 => dp_new[5]=max(48,28+15=43) => 48
- m=2: new_r=6, gain=25 => dp_new[6]=28+25=53
- m=3: infeasible
r=5 (dp=32):
- no bonus
- m=0: new_r=5, gain=0 => dp_new[5]=max(48,32)=48
- m=1: new_r=6, gain=15 => dp_new[6]=max(53,32+15=47) => 53
- m=2: new_r=7, gain=25 => dp_new[7]=32+25=57
- m=3: infeasible
r=6 (dp=35):
- r=6: no bonus
- m=0: new_r=6, gain=0 => dp_new[6]=max(53,35)=53
- m=1: new_r=7, gain=15 => dp_new[7]=max(57,35+15=50) => 57
- m=2: new_r=8, gain=25 => dp_new[8]=35+25=60
- m=3: infeasible
r=7 (dp=38):
- r=7: bonus condition: if m >= 3 then bonus. But m=3 is infeasible (since no three cost1). So bonus never occurs. So no bonus for any m. So same as no bonus.
- m=0: new_r=7, gain=0 => dp_new[7]=max(57,38)=57
- m=1: new_r=8, gain=15 => dp_new[8]=max(60,38+15=53) => 60
- m=2: new_r=9, gain=25 => dp_new[9]=38+25=63
- m=3: infeasible
r=8 (dp=-inf) skip.
r=9 (-inf) skip.
Also r=... other states.
Now after turn4, we have dp_new:
0:0
1:15
2:25
3:35
4:42
5:48
6:53
7:57
8:60
9:63
Let's verify: For r=7 we also did m=2 gives new_r=9 with gain 25 -> 38+25=63, dp_new[9]=63. Good.
Now turn5:
best_S: m1=100, m2=-inf, m3=-inf. So only m=0 and m=1 feasible.