We need to parse problem: We have n friends, each with k_i (1 ≤ k_i ≤ m). There are m presents with costs c_1 ≤ c_2 ≤ ... ≤ c_m. We can buy at most one of each present. For each friend i, we have two options:
- Give them a present j such that j ≤ k_i (i.e., any present among first k_i presents), costing c_j. That present is then used (can't be given again).
- Or give them directly c_{k_i} dollars.
Goal: minimize total cost.
We need to choose which friends get a present (and which present) and which get cash. Since cash amount is c_{k_i}, which depends on k_i. Note that if we give a present j (j ≤ k_i), its cost c_j could be less than, equal to, or greater than c_{k_i}. Typically c_j ≤ c_{k_i} because j ≤ k_i and costs non-decreasing. So giving a present is potentially cheaper than cash? Actually c_j could be less or equal: because c_j ≤ c_{k_i} for j ≤ k_i. So giving a present is never more expensive than giving cash, but there is limited supply of presents. So we want to assign presents to some friends to save money compared to cash, but we may not have enough presents, or the presents we have may not be the best match.
Observation: For a friend i, if we give them a present j, the saving compared to giving cash is (c_{k_i} - c_j). Since c_{k_i} ≥ c_j, saving is non-negative. So we want to maximize total savings by assigning presents to friends.
We have m presents, each can be used at most once. For each friend, they can only receive presents among the first k_i items. So this is like: we have a set of presents (with costs), and friends each have a constraint that they can only take presents from a prefix of the sorted presents (by index). Since presents are sorted by cost, the cheaper ones have lower index, so each friend can accept any present whose index is ≤ k_i, meaning they can accept presents that are among the first k_i (cheapest) presents. But note that presents themselves have different costs, but they are sorted increasing. So the cheapest present is c_1, then c_2, etc. Friend with larger k_i can accept more expensive presents (since higher index, cost higher), but they also can accept cheaper ones. Actually, friend with k_i can accept any present with index <= k_i, so they can accept cheaper presents as well.
But cash alternative: friend receives c_{k_i}. That is the cost of the present at index k_i (which is the most expensive among those they could receive if we gave a present). So effectively, giving a present instead of cash yields a saving of c_{k_i} - c_j, where j ≤ k_i.
Thus we want to match presents to friends such that for each friend assigned a present j, we have j ≤ k_i, and we maximize total saving sum (c_{k_i} - c_j). Since total cost = sum_i c_{k_i} - total saving. So minimize total cost = sum_i c_{k_i} - max_saving.
So we need to compute max possible total savings given presents constraints.
Alternatively, we can think directly: we can either give a present or cash. We have m presents. For each friend, we can consider that we might give them cash, but if we give them a present, we pay less. Since we can give cash to any number of friends (no limit), the only limitation is presents and the constraint that friend i can only receive present j if j ≤ k_i.
We need to minimize sum of costs: sum over friends given cash: c_{k_i} plus sum over presents used: c_j.
But careful: If we assign a present to a friend, we pay c_j, and we don't pay c_{k_i} (the cash alternative). So total = sum over all i of cash cost (c_{k_i}) minus sum over assigned friends of (c_{k_i} - c_j). But easier: decide assignment.
Because n,m up to 3e5 per test case and sum over tests also up to 3e5, we can handle O((n+m) log n) or sorting.
We need an efficient way to assign presents to friends.
Observation: Presents are sorted by cost. The cash cost for each friend is determined by k_i. If we give a present to a friend i, we would typically want to give the cheapest present possible (to maximize saving) because saving = c_{k_i} - c_j, and since c_j is smaller for smaller j, saving larger if we give a cheaper present. However, the cheapest presents are limited and may also be usable by many friends. So it's like we have a set of "discounts": giving present j yields saving c_{k_i} - c_j for that friend i. But the saving depends on both i and j.
Alternatively, we can think of it as we have to decide which friends get presents. For each present j, we can assign it to at most one friend i such that k_i ≥ j (since j ≤ k_i). The benefit of assigning present j to friend i is that we avoid paying c_{k_i} and instead pay c_j, so we save (c_{k_i} - c_j). But we can also just not assign the present, and we pay c_{k_i} anyway. But we could also not buy the present at all (if we don't assign it, we don't need to buy it). So we want to select a set of assignments covering at most one friend per present, to maximize total saving.
This is a bipartite matching problem with n friends and m presents, but with constraint: present j can be assigned only to friends with k_i ≥ j. However, due to monotonic costs, there is a greedy solution.
Consider sorting friends by k_i, maybe descending or ascending? Let's explore.
If we think of the presents, the cheapest present (j=1) could be given to any friend with k_i ≥ 1, which is all friends. But its cost c_1 is smallest, so the saving for friend i is c_{k_i} - c_1, which is large for friends with large k_i (and thus large c_{k_i}). So we'd want to give the cheapest presents to friends with largest c_{k_i} to maximize saving. Similarly, present j (cost c_j) should go to a friend with as large c_{k_i} as possible (since saving is c_{k_i} - c_j, which is larger when c_{k_i} is larger). However, the friend also must satisfy k_i ≥ j. So a friend with a given k_i can only receive presents with index up to k_i. So if we have a friend with small k_i, they cannot receive high-index presents (expensive ones), but they can receive cheap ones. But giving cheap presents to friends with small k_i yields modest saving because c_{k_i} is also small. So to maximize total saving, we should assign presents to friends with largest possible c_{k_i}, but also ensure that each present j is assigned to a friend with k_i ≥ j.
Thus a natural greedy: sort friends by cash cost c_{k_i} descending (or by k_i descending, since c is non-decreasing, so larger k_i generally gives larger c_{k_i}, but careful: c might have same values for different indices? But it's non-decreasing, so c_{k_i} increases with k_i, but not necessarily strictly. So sorting by k_i descending is equivalent to sorting by c_{k_i} descending if c is strictly increasing? Actually c is non-decreasing, so if there are equal costs, two different k_i might have same c. But sorting by k_i descending ensures that the friend with larger k_i can accept more presents (including higher index), and also typically have higher cash cost. So that's promising.
We also have a set of presents, each can be used once. We can think of iterating over presents from cheapest to most expensive, or the opposite.
One known solution for this problem (CF problem 1472D? No, it's "Presents for Friends"? Actually this looks like Codeforces Round # problem "Petya and Presents"? Might be similar to problem "1462C" not. But there is known greedy: sort k_i decreasing, then for each friend from largest k_i to smallest, we consider if there is a present that we can give them cheaper than cash. But we need to be careful.
Alternatively, we can think of it as we have two options: for each friend, we can either pay c_{k_i} or buy a present j <= k_i at cost c_j. Since we can buy at most one of each present, we can treat it as we have a pool of presents (indices 1..m) with costs c_j. We want to select a subset of friends to assign presents, and each present used at most once, to minimize sum of chosen present costs + sum of cash costs for others. Equivalent to: we pay cash for everyone initially (total = sum_i c_{k_i}), then we have the option to "replace" cash payment with a present purchase. For each friend i, we could replace with a present j, but we must pay c_j instead of c_{k_i}, saving (c_{k_i} - c_j). However, each present j can only be used for at most one friend, and only if that friend's k_i >= j. So we want to choose up to m replacements maximizing total saving.
This is like: we have a set of possible "edges" (i, j) with benefit b_{i,j} = c_{k_i} - c_j. Want to select a matching of size at most m (one per present) maximizing sum benefit. Since presents are distinct and have monotonic index constraints, we can find optimal matching greedily.
Observations: For a given present j, its benefit when assigned to friend i is c_{k_i} - c_j. Since c_j is fixed, maximizing benefit is equivalent to maximizing c_{k_i} among friends with k_i >= j. But we also need to consider that each friend can get at most one present.
So one greedy approach: sort friends by c_{k_i} descending. Then iterate friends from highest cash cost to lowest. For each friend i, we want to assign them the cheapest available present that satisfies j ≤ k_i, because that gives the maximum saving for that friend (since saving = c_{k_i} - c_j, and we want to minimize c_j). But we also need to consider that cheaper presents might be more valuable to later friends with smaller cash cost? Actually if we give the cheapest present to the friend with the highest cash cost, that yields the largest saving, and preserves slightly more expensive but still cheap presents for other friends. However, we have to respect that each friend can only accept presents up to their k_i. So for friend with high cash cost (and likely high k_i), they can accept even the cheapest present. For friend with low cash cost (and low k_i), they might only be able to accept very cheap presents (low index). So it might be optimal to assign the cheapest present to the friend with highest cash cost, then the next cheapest present to the next highest cash cost that can accept it, and so on.
But we must also consider that some presents may be too expensive (high index) that only friends with high k_i can accept. Those presents may not be useful to give to low k_i friends anyway. So we need to assign presents to friends such that each present j is given to some friend with k_i >= j. And we want to maximize total saving.
This is reminiscent of the classic problem: We have items (presents) each with cost c_j, and each friend i can receive an item if its index <= k_i; we want to minimize total cost. This can be solved by sorting friends by k_i descending, and then using a pointer for presents: we maintain an index for the cheapest present not yet used. For each friend in descending order of k_i, if the cheapest available present index <= k_i and its cost is less than c_{k_i} (i.e., it saves money), then we give that present; otherwise we give cash. This is typical.
Let's test this greedy on examples.
Example 1:
n=5,m=4
k = [2,3,4,3,2]
c = [3,5,12,20]
Compute cash costs: for each friend: c_{k_i}: indices: 2->5, 3->12, 4->20, 3->12, 2->5. Total cash sum = 5+12+20+12+5=54.
Now, presents: costs: 3,5,12,20.
Sort friends by k descending: we need a consistent tie-breaking. Let's sort by k descending: values: 4,3,3,2,2 (corresponding to cash: 20,12,12,5,5). Actually, friend with k=4 (cash 20), then two with k=3 (cash 12 each), then two with k=2 (cash 5 each). The greedy: we have pointer to cheapest available present, start at index 1 (cost 3). For friend with k=4, cheapest available present index 1 (<=4) and cost 3 < cash 20? Yes, give present cost 3. Pointer moves to next present index 2 cost 5.
Next friend: k=3 (cash 12), cheapest available index 2 cost 5 <= 3? index=2 <= 3 yes, cost 5 < 12, give present cost 5. Pointer moves to index 3 cost 12.
Next friend: k=3 (cash 12), cheapest available index 3 cost 12 <= 3? index=3 <= 3 yes, cost 12 < 12? No, equal, but if we give present cost 12, we pay 12 instead of cash 12, saving 0. So it's not worse, but we might as well not give present because using it may prevent a future better saving? However, we can decide to give cash if cost is not less. But the problem says "either buy them a present j ≤ k_i, which costs c_j dollars, or just give them c_{k_i} dollars directly." If cost equal, both options cost same. But using the present gives no benefit, but maybe using it could be detrimental if later we could use it to get a saving. But if it's equal, it's not beneficial to use it, but we can choose either. So greedy should only use present if it yields a positive saving (strictly less) OR if saving >=0, but we might want to avoid using it if there is a better use later. However, the present with cost 12 is index 3. Only friends with k_i >=3 can accept it. The remaining friends are: one with k=2 (cash 5) cannot accept index 3 because 2<3, so it's not usable. The other friend with k=2 also cannot. So this present can only be used by the current friend (k=3) or the next friend (the other k=3 we already processed? Actually we have one more friend k=3? No we had two with k=3, and we processed one, now the second. The next friend after this is k=2. So the present index 3 can't be used by any remaining friend (since they have k=2). So if we don't use it now, it will be unused, which is fine. Using it gives no saving, not harmful (except we pay same as cash). But total cost would be same as cash. So either way total cost same. But we need to compute minimum cost. So giving cash for this friend costs 12, and we still have the present unused. But we could later give that present to no one, so it's not used. So total cost = used presents (3,5) + cash for others: 12 + 5 + 5 = 25? Let's compute: we gave present 3 to first friend (cost 3), present 5 to second friend (cost 5). For third friend (k=3), we could give cash 12. For fourth friend (k=2) cash 5, fifth friend (k=2) cash 5. Total = 3+5+12+5+5 = 30. That matches output 30. If we had used present 12 for third friend, we would have cost: 3+5+12 + cash for last two = 5+5 = 30 as well. So same. So greedy that uses present only if cost < cash (strict) or <= cash? It would also produce 30 if it uses it only when cheaper; maybe it would skip and give cash, resulting same 30. So fine.
But we must ensure we get optimal. Let's test second example:
n=5,m=5
k = [5,4,3,2,1]
c = [10,40,90,160,250]
Cash costs: c5=250, c4=160, c3=90, c2=40, c1=10 => total = 250+160+90+40+10=550.
We need min cost output 190.
Now, presents: 10,40,90,160,250.
Sort friends by k descending: k=5 (cash 250), k=4 (160), k=3 (90), k=2 (40), k=1 (10).
Now greedy: cheapest available present index1 cost10.
Friend k=5: present index1 <=5, cost10 < cash250 -> give present10, pointer index2 cost40.
Friend k=4: present index2 cost40 <=4? index2 <=4 yes, cost40 <160 -> give present40, pointer index3 cost90.
Friend k=3: present index3 cost90 <=3? index3 <=3 yes, cost90 <90? No, equal. Strictly less? It's equal, so maybe not give. If we don't give, then we give cash90. Pointer remains index3 cost90. Remaining friends: k=2 (cash40), k=1 (cash10). present index3 cost90 cannot be given to k=2 or k=1 because index3 > k_i (3>2,3>1). So present index3 remains unused. Next friend k=2: cheapest available present index3 is not usable; so we consider next present? Actually the pointer would still be at index3, but it's not <=2, so we cannot use it. So we need to check if there is any available present with index <= k_i. The next available present after index3 is index4 cost160, also >2. So no presents usable. So we give cash40. Similarly for k=1: cash10. Total cost = present10 + present40 + cash90 + cash40 + cash10 = 10+40+90+40+10 = 190. That matches output.
If we had given present90 to the third friend (cost90), total would be 10+40+90 + cash40 + cash10 = 190 as well. So fine.
Thus greedy seems plausible: sort friends by k_i descending, then iterate through friends, maintain a pointer to the cheapest still-unused present (starting from index 1). For each friend, if pointer <= m and pointer <= k_i and c[pointer] < c[k_i], then give that present and increment pointer; else give cash.
But we need to be careful: what if c[pointer] >= c[k_i]? In that case, giving present yields no saving or negative saving? Actually, since c is non-decreasing and pointer is smallest index unused, its cost is the smallest among remaining presents. If it's >= c[k_i], then any other unused present (which have index >= pointer) will have cost >= c[pointer] (since costs non-decreasing) and thus also >= c[k_i]. So no present can provide a saving (since saving = c[k_i] - c_j <= 0). So giving cash is optimal. However, there is a nuance: what if c[pointer] > c[k_i]? That would mean we pay more than cash, which is worse. So we should not give any present to this friend.
But wait: there could be a present with index > pointer but still <= k_i that has cost < c[k_i]? Since c is non-decreasing, if the smallest unused present cost is already >= c[k_i], then all presents with index >= pointer have cost >= c[pointer] >= c[k_i], thus none cheaper. So yes, no saving.
Thus the greedy is: sort k descending, then for each friend, if there is any unused present with index <= k_i and cost < c[k_i], we want to give the cheapest such present (which is the smallest index among unused ones). Because that yields maximum saving and also preserves larger index presents for later friends who may have higher k_i and can accept them, but those presents might be more expensive, but we might still want to give them if they provide saving. But the cheapest one is the most beneficial for any friend, so it's best to assign it to the friend with the highest cash cost (which we are iterating in descending k_i). However, note that if the cheapest present's index > k_i for some friend, that friend cannot accept it. In that case, we might want to skip that friend and move to the next, maybe using a slightly more expensive present that they can accept? But if they can't accept the cheapest, maybe they can accept a present with higher index that is still <= k_i. But if that present's cost is < c[k_i], it might still be beneficial. The greedy as described only checks the current pointer (the smallest index unused). But if that smallest index is > k_i, then we cannot give it to this friend. However, there might be other unused presents with index <= k_i (maybe index = some value > pointer but still <= k_i). Since pointer is smallest index unused, if pointer > k_i, then any unused present index >= pointer > k_i, so none <= k_i. So no present is usable for this friend. So we must give cash. That's correct.
If pointer <= k_i but c[pointer] >= c[k_i], then no saving, but could there be another present with index > pointer but <= k_i that has cost < c[k_i]? Since c non-decreasing, if c[pointer] >= c[k_i], then for any j >= pointer, c_j >= c[pointer] >= c[k_i], so no saving. So cash is optimal.
Thus the greedy algorithm works: sort friends by k_i descending; maintain a variable p = 1 (next present index). For each friend in sorted order, if p <= m and p <= k_i and c[p] < c[k_i] then give present p: add c[p] to total, increment p; else give cash: add c[k_i] to total.
But we must be careful: the condition p <= k_i ensures that the present index is within allowed range. However, if p > m, no presents left.
But is it always optimal to give the present with index p (the cheapest unused) to the current friend if possible and beneficial? Let's try to think of counterexample.
Consider: friends sorted descending: Friend A with large k (say 100) and cash cost large; Friend B with slightly smaller k (say 99) and cash cost slightly smaller; but maybe the cheapest present is index 1 (cost small) can be taken by both. Giving it to A yields saving = c_{k_A} - c_1, giving to B yields saving = c_{k_B} - c_1, which is smaller because c_{k_B} < c_{k_A}. So indeed giving to A yields larger saving. So using cheapest present for highest cash friend is beneficial.
But what about a case where the cheapest present is not usable by the highest cash friend? That could happen if highest cash friend has a small k? But higher cash cost correlates with larger k (since c non-decreasing). However, it's possible that c is not strictly increasing: e.g., many c_j are same value for many indices. Could there be a situation where a friend with larger cash cost has a smaller k than another friend with smaller cash cost? Since cash cost is c_{k_i}. Because c is non-decreasing, if k_i is larger, c_{k_i} >= c_{k_j} for k_i > k_j. So larger k implies larger or equal cash cost. So sorting by k descending also sorts by cash cost descending (non-increasing). So that's fine.
Now, consider scenario: two friends: friend1 k=2, cash=c2; friend2 k=1, cash=c1 (c1 ≤ c2). Presents: c1, c2. c1 <= c2. Greedy: sort descending k: friend1 (k=2) then friend2 (k=1). pointer p=1. Friend1: p=1 <=2 and c1 < c2? likely yes, so give present1 to friend1. p becomes 2. Friend2: p=2, but k=1, p <= k? 2 <=1 false, so no present, give cash c1. Total = c1 + c1 = 2c1. Could there be a better assignment? Alternative: give present1 to friend2 (since friend2 can only accept index1) and give present2 to friend1? But present2 index2 is allowed for friend1 because k=2, so assign present2 to friend1 (cost c2) and present1 to friend2 (cost c1). Total = c2 + c1 = c1 + c2. Compare with greedy total 2c1. Since c2 >= c1, c1+c2 >= 2c1? Actually c1+c2 >= 2c1 because c2 >= c1. So greedy gives smaller total? Let's compute: greedy total = c1 + c1 = 2c1. Alternative total = c1 + c2. Since c2 >= c1, alternative total >= 2c1. So greedy is better or equal. So indeed giving the cheaper present to the friend with larger k (who could also take the more expensive present) and the more expensive present to the smaller k friend is not allowed because the smaller k friend cannot take the more expensive present if its index > k. In this case, friend2 can only take index1, so cannot take present2. So the only assignment that uses both presents is to give present1 to friend2 and present2 to friend1. But that yields total c1 + c2, which is >= 2c1? Actually if c2 > c1, then alternative total > 2c1, so greedy (use present1 for friend1, cash for friend2) yields lower total. If c2 = c1, then both totals equal. So greedy is optimal.
Now consider a case where there are multiple presents and some friends have moderate k, and there is a present that is not the cheapest but still cheaper than cash for some friend, while the cheapest present might be unusable for that friend? But cheapest present index 1 is always usable by any friend because k_i ≥1. So index1 is always usable. So that scenario can't happen: the cheapest present is always usable by all friends (since k_i >=1). Actually if a friend has k_i =0? Not allowed; min k_i is 1. So yes, present1 is always available to all. So pointer p will start at 1, and it's usable for all friends. So the only time a friend cannot use the cheapest unused present is when we have used up some presents and the next cheapest unused present might have index > k_i. But initially, all friends can take present1.
So the greedy is: always assign the smallest-index unused present to the friend with highest remaining cash cost that can still accept it, i.e., while iterating descending k_i, if the current friend can take the current smallest present, and it's beneficial, do it.
Is it ever better to skip assigning a present to a high cash-cost friend even though it's beneficial, to reserve that present for a later friend that might yield an even larger saving? Since we are processing friends in descending cash cost, any later friend will have cash cost <= current friend's cash cost. So the saving from giving the current smallest present to a later friend would be at most the saving from giving it to the current friend (since cash cost of later friend <= current). So it's never better to skip a beneficial assignment for the current friend.
But careful: there might be a scenario where giving the smallest present to the current friend yields a saving, but later there is a friend who cannot accept any present other than that smallest one (because they have a small k_i) and if we give it away, they would have to take cash, which might be costly. But if the later friend has smaller k_i, they may have a lower cash cost, so the saving from giving them the smallest present is smaller. So it's better to give it to the current friend and later friend may use a slightly larger present if possible, or take cash. However, if the later friend cannot accept any other present because their k_i is very small (e.g., k=1), then they can only accept present1. If we give present1 to an earlier friend, that later friend will have to take cash (c1). Giving present1 to later friend yields saving = c1 - c1 = 0 (since cash cost is c1). Actually if k=1, cash cost = c1. Giving present1 costs c1, saving 0. So no benefit. So it's fine to give present1 to earlier friend with high cash cost to gain positive saving, while later friend gets cash with zero saving. So no loss.
But what about present2? Consider friends: A (k=2, cash=c2), B (k=1, cash=c1). Present costs: c1=1, c2=100 (but c is non-decreasing, so c2 >= c1, but can be much larger). We have presents 1 and 2. Greedy: sort descending k: A (k=2) then B (k=1). p=1, present1 cost 1, can give to A? condition: p<=k_i (1<=2) and c1 < c2? yes (1<100). So give present1 to A. p=2, present2 cost 100. For B: p=2, but k=1, so 2<=1 false, cannot use present2. So B gets cash c1=1. Total = 1 + 1 = 2. Alternative: give present1 to B (cost 1) and present2 to A (cost 100). Total = 1+100 = 101. Worse. So greedy optimal.
Now consider scenario where c1 is very close to c2, e.g., c1=99, c2=100. Then giving present1 to A yields saving 1, giving present2 to A yields saving 0? Actually if we give present2 to A, cost 100 vs cash 100, saving 0. So alternative: give present1 to B (cost 99) and cash to A (100) total = 199. Greedy: present1 to A (99) + cash to B (99) = 198. So greedy slightly better. So fine.
Now consider a scenario with three friends and two presents. Might need to check if greedy always works.
Let's attempt to construct a potential counterexample.
We have friends with different k values. The greedy algorithm processes friends in descending k. It always picks the smallest unused present (lowest index) that is <= k_i, and if beneficial, assigns it. This is essentially a variant of matching where we want to maximize sum of c_{k_i} - c_j. Equivalent to minimize sum of c_j for assigned presents plus sum of c_{k_i} for unassigned. Another viewpoint: The optimal solution can be found by considering we have m presents, we can choose to "buy" some of them and assign to friends. Since the cash alternative for each friend is c_{k_i}, we could also think of it as we can replace cash payments with present purchases. The net effect is that for each present j we give to some friend i, we incur cost c_j instead of c_{k_i}. So we want to maximize sum of c_{k_i} for the friends who receive a present? Actually we want to minimize total cost: sum_{i not assigned} c_{k_i} + sum_{j assigned} c_j. That's like we choose a set of friends S to assign presents, and we assign distinct presents to them with constraints j_i <= k_i. Then total = sum_i c_{k_i} - sum_{i in S} (c_{k_i} - c_{j_i}). So we want to maximize sum_{i in S} (c_{k_i} - c_{j_i}). Since c_{k_i} are fixed, this is equivalent to minimizing sum_{i in S} c_{j_i} given constraints.
But also note that we could decide to not use all presents. Using extra presents when they don't give saving only increases cost? Actually if c_j == c_{k_i}, using present costs same as cash, but it could allow us to give cash to some other friend? No, each friend either gets cash or present. So if present yields no saving, it's neutral, but we might use it and still have same total cost. However, using a present with no saving doesn't reduce cost, but might block using that present for another friend where it could have saved? But if it's no saving, it means c_j >= c_{k_i}. For any other friend i' with cash c_{k_i'}, we have c_{k_i'} <= c_{k_i} (if we process descending) maybe? But not necessarily? Actually if we give a present with c_j = c_{k_i} to friend i, and if there is another friend i' with cash cost lower, then c_j might be > c_{k_i'}, giving negative saving. So better not to give. So we should avoid giving presents with zero or negative saving, unless maybe it enables something? But no, because we can always choose to not give that present, and just give cash to i' and i, same total. So we only want to assign presents that provide positive saving (c_j < c_{k_i}). In case of zero saving, either assignment yields same total, but to simplify we can treat as not using. However, it's safe to use them because they might be the only way to use a present? But using them doesn't hurt if we don't have a better use. But to avoid complication, we can only assign when c_j < c_{k_i}.
Now, is the greedy of assigning the smallest available present to the friend with largest k (or cash) always optimal? We should try to prove or at least think of potential pitfalls.
One typical proof: Consider any optimal assignment. We can transform it to match greedy by exchange arguments. Suppose we have friends sorted by k descending. The greedy picks friend 1 (largest k) and gives the smallest present j1 that satisfies j1 <= k1 and c_{j1} < c_{k1} (if any). In an optimal solution, if friend 1 gets some present j', maybe not the smallest, we can swap to make it use the smallest without decreasing total saving. Also, if friend 1 gets no present in optimal but greedy gives one, we can argue that giving a present to friend 1 and removing it from some other friend (if needed) does not reduce saving because friend1 has higher cash cost. This is similar to assignment problems with matroid properties. This is a known solution for Codeforces problem 1472B? Actually I've seen this problem: Codeforces Round #1462, maybe "Presents" but I recall similar problem: "Presents for Friends" or "Buying presents". I think it's CF 1369C? Not sure.
But we can trust this greedy. However, we need to be careful about the condition "c[p] < c[k_i]" – is strict inequality necessary? If equal, no saving, but we could still use it, but then we might use a present that could have provided saving to another friend? If it's equal, then for any friend with smaller k, c_{k_i} is <= c_{k_current}, so if c_{k_i} is also equal, maybe same. But if we give it to current friend, we lose the opportunity to give it to a later friend who might have cash cost exactly equal to c[p] as well. But if they have same cash cost, giving present yields zero saving anyway, so no difference. If later friend has cash cost less than c[p], then giving present would be worse (negative saving). So using it now with zero saving is fine, but we could also not use it at all. So either way, it doesn't affect total cost. So we can safely skip when c[p] >= c[k_i], because using it would not reduce total cost (could be negative or zero), and we want to minimize, so we skip to keep present for possibly later? But later friend with higher k? Actually later friend has smaller k, so cash cost <= current, so if c[p] >= current cash cost, then for later friends cash cost is smaller, so c[p] >= cash cost of later friends as well (since c[p] >= current cash cost >= later cash cost due to monotonicity? Need to check: Because cash cost c_{k_i} is non-decreasing with k_i, and we process descending, so later friends have smaller k_i, so cash cost <= current. So if c[p] >= current cash cost, then c[p] >= later cash cost as well. So using it for later would also give zero or negative saving. So it's never beneficial to use that present for any friend. So we can just ignore such presents entirely: they are too expensive (relative to cash costs of friends who can accept them). But wait, what if current friend has k_i such that p <= k_i but c[p] >= c_{k_i}, but there might be another present with larger index j>p that is actually cheaper? No, costs are non-decreasing, so larger index -> larger or equal cost. So no cheaper present. So indeed if smallest unused present is not cheaper than current cash cost, no unused present can be cheaper than current cash cost because all unused have cost >= c[p] >= c_{k_i}. But could there be a used present that is cheaper? Already used. So no saving possible for current friend or any later friend (since later friends have lower cash costs). So we could stop assigning any more presents at this point, because further friends will have even lower cash cost, and all remaining presents cost at least c[p] which is >= c_{k_current} >= c_{k_later}. So no saving. However, we must also consider that maybe some present with index > p could be used by current friend, but its cost is higher, so not beneficial. So we could break the loop early. But careful: What if current friend has k_i small such that p > k_i (so cannot take the smallest unused), but there exists some other unused present with index > p that is <= k_i? Not possible because if p > k_i, then any unused present index >= p > k_i, so cannot be used. So no presents for this friend. So we have to give cash. And later friends have even smaller k_i, so they also cannot use any present. So we can break. So algorithm can stop assigning presents when either p > m, or p > max k_i among remaining friends, or c[p] >= c_{k_i} for the current friend? Actually we need to continue because maybe p <= k_i but c[p] >= c_{k_i} means no saving, but later friends might have even smaller cash cost, so c[p] >= c_{k_i} for all later friends as well (since c_{k_i} decreasing), so still no saving. So we can break. But to be safe, we can just continue iterating, but each will give cash anyway.
Now, complexity: For each test case, we need to sort k_i (or just process them in descending order). n,m up to 3e5, sum over tests up to 3e5. Sorting O(n log n). Then we iterate O(n) with pointer. That's fine.
But we need to compute total cost. Implementation: For each test case:
- Read n, m.
- Read list k (size n).
- Read list c (size m) (1-indexed maybe).
- Compute sum_cash = sum_{i=1..n} c[ k_i ] (note careful: c is 1-indexed).
- Sort k in descending order.
- Initialize present_idx = 0 (0-indexed in array c), maybe we use 0-index for c (costs c[0]..c[m-1]).
- For each k in sorted list:
- if present_idx < m and present_idx < k (since present index 0-based? Let's define: present indices from 1 to m. We'll keep pointer p = 0 (0-based index). Condition: p < m and (p+1) <= k (since present number j = p+1) and c[p] < c[k-1] (since c[k-1] is cash cost). If true, then we would "use" this present: we add c[p] to total? Actually total cost = sum of presents given + sum of cash for others. We can compute total cost directly: start total = 0. For each friend, if we decide to give present, add c[p]; else add c[k-1]. So we can compute directly without sum_cash.
- But careful: The present's cost c[p] might be used instead of cash. If condition holds, we add c[p] to total and increment p. Else, add c[k-1].
Alternatively, compute total as sum_cash - sum_saving, but easier to compute directly.
But we must ensure that when we give a present, we are using it, and we shouldn't give more than m presents. The loop will stop when we run out of presents.
Potential edge cases: Some friends may have k_i > m? No, k_i ≤ m per constraints.
Now, the greedy as described seems correct. However, let's test some edge cases.
Case: n=1, m=1, k1=1, c1=1. Sorted: [1], p=0, present_idx 0 < m and (0+1) <= 1? Yes, and c[0] < c[0]? 1 < 1 false. So we give cash: total = 1. Output 1. That's correct. If c1=0? But constraints say c_j >= 1. So okay.
Case: n=2, m=2, k=[2,2], c=[1,100]. Cash sum: c2=100 each, total 200. Greedy: sort descending: [2,2]. p=0, present idx1: 1 <=2 and 1 < 100 -> give to first friend, add 1, p=1. Second friend: p=1, present idx2: j=2 <=2? yes, 2<=2, c[1]=100 < cash 100? false (equal). So give cash 100. Total = 1+100=101. Alternative: give present1 to friend2 and present2 to friend1? But friend2 can take present2? Yes, both can take both. But present2 costs 100, same as cash. So best is to give present1 to one friend (saving 99) and cash to other (100). Or could give present1 to both? Not allowed, only one each. So total 101 is minimum? Could we give present2 to one friend and cash to other? 100+100=200. Or give both presents? present1+present2=101, same as 1+100=101. So optimum 101. Greedy yields 101.
Case: n=2, m=2, k=[1,2], c=[1,100]. Cash: friend1: c1=1, friend2: c2=100, total 101. Greedy: sort descending: [2,1]. p=0: present1, j=1 <=2? yes, c0=1 < cash 100 -> give to friend2, add 1, p=1. friend1: p=1, present2, j=2 <=1? false, so give cash 1. total = 1+1=2? Wait: friend1 cash is 1, so total = 1 (present for friend2) + 1 (cash for friend1) = 2. That seems too low! Check: friend2 receives present1 cost 1, friend1 receives cash c1=1. So total = 2. But we have to pay for presents, we used present1 costing 1, and gave friend1 1 dollar, total 2. Is that valid? But friend2's k=2, they can receive present1 (since j=1 <=2). Friend1's k=1, they receive cash c1=1. Total expense 2. But is there any constraint that we must buy presents? No, we can choose not to buy some presents. That seems legit. But is it feasible given the problem statement? Let's see: we have presents: two presents, one costing 1, one costing 100. We didn't buy the 100 one. So total cost 2. But is there any hidden condition that we must buy presents? No, we can buy any subset. So 2 seems plausible. But let's compute alternative: if we gave present1 to friend1 (cost 1) and cash to friend2 (100), total=101. Worse. Giving present2 to friend2 (100) and cash to friend1 (1) total=101. So minimum is 2. So answer should be 2. Is there any reason this is not allowed? The problem says: "Petya can either buy them a present j ≤ k_i, which costs c_j dollars, or just give them c_{k_i} dollars directly." It doesn't say he must buy all presents or anything. So yes, 2 is correct. But wait, is there any condition that each present can be bought at most once, but not required to buy all. So it's fine. So answer 2. Our greedy gave 2. Good.
Now consider a more tricky case: Suppose c = [5, 6], k = [2,2]. Cash sum = c2=6+6=12. Greedy: sort [2,2]. p=0: present1 (5) <=2, 5 < 6 -> give to first friend, total +=5, p=1. Second friend: p=1 present2 (6) <=2, 6 < 6? false -> give cash 6. total=11. Alternative: give present1 to second, present2 to first? first friend can take present2? yes, j=2 <=2, cost6, saving 0; total =5+6=11 as well. So 11. Could we do better? Present1 to one, cash to other: 5+6=11. Present2 to one, cash to other:6+6=12. So 11 is min. Good.
Now consider c = [5,5], k=[2,2]. Cash sum =5+5=10. Greedy: present1 5<5? false, so both cash: total=10. Could we give present1 to one, cash to other: total=5+5=10. Same.
Now consider a case where the cheapest present is not beneficial for the friend with largest k, but maybe beneficial for a friend with smaller k? That can't happen because if cheapest present cost c1 is not less than cash cost of largest k friend (c_{k_max}), then c1 >= c_{k_max}. Since c is non-decreasing, c_{k_max} is largest among all cash costs, so c1 >= all cash costs. Thus no present can provide any saving for any friend. So we give all cash.
Now consider a case where there are more presents than friends, and some presents are very expensive, but we only use the cheap ones. Our greedy will only use presents while they provide saving, and stop when the next present's cost is not less than current friend's cash cost, or when we run out of presents. Since we process friends in descending cash, once we reach a friend where c[p] >= c[k_i], we can stop because later friends have lower cash. But we need to consider that maybe a present with index > p could be used for a later friend even if c[p] >= current cash? That later friend's cash is even lower, so c[p] >= current cash >= later cash, so c[p] >= later cash; all remaining presents have cost >= c[p] >= later cash, so no saving. So indeed we can break. But careful: It's possible that the smallest unused present is not usable because p > k_i (index too high) but a larger-index present might still be <= k_i? That can't happen because if p > k_i, then all unused presents have index >= p > k_i, so none are usable. So break as well.
Thus algorithm can be:
For each test:
- read n, m.
- read ks.
- read c array.
- sort ks in descending order.
- Initialize ptr = 0 (index for c, 0-based).
- total = 0.
- For each k in sorted ks:
if ptr < m and ptr+1 <= k and c[ptr] < c[k-1]:
total += c[ptr]
ptr += 1
else:
total += c[k-1]
- Print total.
But we must be careful about indexing: k_i is 1-indexed present index, so cash cost = c[k_i - 1] (if c is 0-indexed). Present indices: first present index=1 corresponds to c[0], etc. So condition for using present at index ptr (0-based) is: present_number = ptr+1 <= k_i. And cost c[ptr] < c[k_i-1].
Edge: If ptr >= m, we simply add cash.
Time complexity: O(n log n + m) per test, but note that sorting n up to 3e5, sum over tests up to 3e5, so total sorting O(Σ n log n) which might be up to 3e5 log 3e5 acceptable. But we can also use counting sort or bucket because m up to 3e5? But not needed.
Let's test with sample:
Test1: n=5,m=4, k=[2,3,4,3,2] sorted desc = [4,3,3,2,2]; c=[3,5,12,20]; ptr=0.
- k=4: ptr=0, ptr+1=1 <=4 true, c[0]=3 < c[3]=20? yes -> total=3, ptr=1.
- k=3: ptr=1, present2 <=3 true, c[1]=5 < c[2]=12? yes -> total=8, ptr=2.
- k=3: ptr=2, present3 <=3? 3 <=3 true, c[2]=12 < c[2]=12? false -> else, total+=c[2]=12 => total=20.
- k=2: ptr=2, present3 <=2? false -> else total+=c[1]=5 => 25.
- k=2: ptr=2, same, total+=5 => 30. matches.
Test2: [5,4,3,2,1] sorted desc same; c=[10,40,90,160,250]; ptr=0.
- k=5: ptr0, 1<=5, 10<250 -> total=10, ptr=1.
- k=4: ptr1, 2<=4, 40<160 -> total=50, ptr=2.
- k=3: ptr2, 3<=3, 90<90? false -> total+=c[2]=90 => 140.
- k=2: ptr2, 3<=2? false -> total+=c[1]=40 => 180.
- k=1: ptr2, 3<=1? false -> total+=c[0]=10 => 190. matches.
Now, what about cases where we have more presents than friends? Suppose n=2,m=5, k=[5,5], c=[1,2,3,100,200]. Sorted [5,5]. ptr=0, k=5: present1,1<=5,1< c4? Actually cash for k=5 is c[4]=200? Wait c indices: c[0]=1, c[1]=2, c[2]=3, c[3]=100, c[4]=200, so cash = 200 each. Condition: 1 < 200 true, total+=1, ptr=1. Next friend: ptr=1, present2,2<=5,2<200 true, total+=2, ptr=2. Now all friends processed, total=3. That seems extremely low. Is that correct? We gave present1 (cost1) to first friend, present2 (cost2) to second friend. Both friends had k=5, so they could receive any present <=5. We gave them the cheapest two. Total cost 3. Alternative: give cash: 200+200=400. So 3 is indeed optimal. And it's allowed: we only used two presents, left others unbought. So that's fine.
Now consider a case where k_i are not all large enough to accept the cheapest presents? But everyone can accept present1 as k_i>=1. So they all can accept the cheapest present. So we will always give present1 to the friend with largest cash cost, etc.
But what about when some friends have k_i < ptr+1? As we go through, after we've assigned some presents, ptr increases, and we may encounter a friend with k_i less than the current present index. In that case, we cannot give them any present (since all remaining presents have index >= ptr+1 > k_i). So they get cash. That's fine.
Now, the greedy seems straightforward. But is it always optimal? Let's try to find a counterexample where it's better not to give a present to the friend with largest k_i, but to give it to a friend with slightly smaller k_i because the larger k_i could receive a slightly more expensive present that still yields saving, and the cheapest present could then be given to the smaller k_i? But giving cheapest to larger yields more saving because saving = c_{k_large} - c_cheap, while giving cheapest to smaller yields c_{k_small} - c_cheap, which is less because c_{k_small} <= c_{k_large}. Giving the more expensive present to larger yields saving = c_{k_large} - c_expensive. Since c_expensive >= c_cheap, that saving is less than or equal to c_{k_large} - c_cheap. So the total saving from assigning cheapest to large and expensive to small vs. cheapest to small and expensive to large is:
Option A: large gets cheap (saving A1 = cL - cheap), small gets expensive (saving A2 = cS - expensive). Total saving = cL + cS - (cheap+expensive).
Option B: large gets expensive (saving B1 = cL - expensive), small gets cheap (saving B2 = cS - cheap). Total saving = cL + cS - (expensive+cheap). Same total! So they are equal! Actually both give same total saving. So swapping assignments doesn't change total saving if both friends can receive both presents. So the assignment of which present to which friend among those who can accept both doesn't matter in terms of total saving if we assign exactly those two presents to those two friends. However, careful: if cheap is cheap and expensive is expensive, total cost of presents is cheap+expensive regardless of who gets which. So total cost = sum of present costs + cash for others. Since both friends are getting presents, the total cost is cheap+expensive + cash for others. So assignment doesn't matter. So the greedy picking cheap for the largest friend is not harmful; it yields same total.
But what if the smaller friend cannot receive the expensive present because its index is too high? That is the crucial point. In that case, swapping might not be feasible. Suppose we have two friends: friend L with large k (say 5), friend S with small k (say 2). Presents: cheap present index 1 (cost c1), expensive present index 3 (cost c3). Friend S can only accept presents with index ≤ 2, so cannot accept index 3. Friend L can accept both. So if we give cheap to L and expensive to S, that's infeasible because S cannot take index 3. So we must give cheap to S (since S can only take cheap) and expensive to L. In that case, total cost = c1 + c3. Our greedy, processing L first, would see cheap present available (index1) and assign it to L because condition holds (cheap < cash for L). Then for S, remaining present is index3, but index3 > k_S (2), so S gets cash c_{k_S}. So total = c1 + c_{k_S}. Compare: alternative (optimal) would be give cheap to S, expensive to L: total = c1 + c3. Which is better? Compare c_{k_S} vs c3. Since c_{k_S} is cash cost for S = c2 (since k_S=2). So we compare c1 + c2 (greedy) vs c1 + c3 (alternative). Which is smaller? Since c2 ≤ c3 (since indices 2 ≤ 3 and costs non-decreasing), c1+c2 ≤ c1+c3, so greedy total is less than or equal to alternative. Actually c2 could be less than c3, making greedy better. Wait, but alternative uses expensive present for L, which might be more costly than cash for S? Let's compute numbers: c = [1, 10, 100], k_L=3, k_S=2. cash: c3=100, c2=10. Greedy: L gets present1 (cost1), S gets cash10 -> total = 11. Alternative: L gets present3 (cost100), S gets present1 (cost1) -> total = 101. So greedy is far better. So greedy is optimal.
Now consider scenario: cheap present is not as cheap relative to cash for S. Actually greedy gave cash to S (cost c2=10) and used cheap present for L, total 11. Alternative gives both presents, total 101. So clearly greedy wins.
What if we have three friends? Need to ensure greedy picks the right assignments.
Let's try to create a scenario where greedy might fail: we need a situation where giving the cheapest present to the highest-k friend prevents us from using a slightly more expensive present for that friend that would still yield saving, and the cheapest present could be given to a mid-level friend where it yields a saving that is more valuable than the saving we would get from giving a more expensive present to the highest-k friend? Actually we need to compare savings.
But the greedy we described is essentially: process friends in descending k, and for each friend, if the smallest unused present is assignable (index <= k_i) and cheaper than cash (c[p] < c[k_i]), then assign it; otherwise give cash. This is like: always give the smallest available present to the current friend if it helps. Is it ever better to skip giving the smallest present to the current friend, and instead give a larger present (which still yields some saving), and reserve the smallest for a later friend who cannot take the larger one? In that case, the later friend can only take the smallest (due to low k_i). If we give the smallest to the current friend (who could also take larger), we may be forced to give cash to the later friend (since no other present fits). That could be suboptimal if the saving from giving the smallest to later friend is larger than the saving from giving a larger present to current friend, compared to the alternative of giving the smallest to later and the larger to current.
So let's formalize:
- We have two friends: High (H) with large k_H, can accept presents with indices up to k_H. Low (L) with small k_L, can only accept presents with indices up to k_L (so k_L < k_H). We have two presents: one small index j_s (≤ k_L) with cost c_s, and one larger index j_l (with j_s < j_l ≤ k_H, but j_l > k_L) with cost c_l ≥ c_s. (Because j_l > j_s, and costs non-decreasing, c_l ≥ c_s). Possibly we also have more presents, but this is the core conflict.
We want to decide assignment to maximize saving (or minimize total cost). Options:
Option 1 (greedy): Give present j_s to H, and cash to L. Total cost = c_s + c_{k_L}.
Option 2: Give present j_s to L, and present j_l to H. Total cost = c_s + c_l.
Option 3: Give present j_l to H, and cash to L: total = c_l + c_{k_L}. (But this is worse than option 2? Not necessarily; we compare.)
Option 4: Give present j_s to L, cash to H: total = c_s + c_{k_H}. Usually c_{k_H} is large, so not good.
Clearly we will use at most one present for each friend. If we use both presents, we have Option 2. If we use only one present, we can either give it to H (Option 1 without using j_l) or to L (Option 4). But using both presents may or may not be better than using only the cheap one. In greedy we only assign present if it provides saving; we might assign both presents if both provide saving and are assignable. In this scenario, for H, both presents provide saving if c_l < c_{k_H} (or c_s < c_{k_H}). For L, only j_s provides saving if c_s < c_{k_L}; j_l cannot be assigned. So the decision is: should we assign j_s to H and j_l to H? Actually we can assign at most one present per friend, so we cannot assign both to H. So we need to allocate one present to H and possibly one to L. The possibilities for using both presents: j_s to L, j_l to H. Option 2. Using only j_s: assign to H (Option 1) or to L (Option 4). Using only j_l: assign to H (Option 3). Using none: cash both.
We want to minimize total cost.
We compare Option 1 (greedy's likely outcome) vs Option 2. Greedy would consider H first; it will see the smallest unused present j_s; if c_s < c_{k_H}, it will assign j_s to H. Then later, when processing L, the remaining present is j_l, which L cannot take, so L gets cash. So total = c_s + c_{k_L}.
Option 2 total = c_s + c_l.
Which is smaller? Compare: Option 1 cost = c_s + c_{k_L}; Option 2 cost = c_s + c_l. Since c_l ≥ c_s, but we need to compare c_l vs c_{k_L}. Because c_{k_L} is the cash cost for L. Which is smaller? c_{k_L} is the cost of present at index k_L. Note that j_l > k_L, so index j_l > k_L, and since costs are non-decreasing, c_l ≥ c_{k_L}. Actually, because j_l > k_L, we have c_l ≥ c_{k_L} (since c is non-decreasing). So c_l ≥ c_{k_L}. Therefore Option 2 cost = c_s + c_l ≥ c_s + c_{k_L} = Option 1 cost. So Option 1 is at most Option 2. So greedy is not worse.
Thus, in the situation where the low friend can only take the small present, assigning the small present to the high friend and giving cash to low yields cost ≤ assigning small to low and large to high. So greedy is optimal.
Now what if there are more than two friends and multiple presents? The above reasoning can be extended: The greedy algorithm that always gives the smallest available present to the highest remaining friend (if beneficial) yields a minimal total cost. This is analogous to a matroid intersection or greedy matching problem known as "assign gifts to friends with constraints". I recall a Codeforces problem "1472D - Even-Odd Game" no. Actually "1472B" no. The problem is CF Round # 1288 B? Not sure.
We should also consider the possibility that for a friend, the smallest available present might be cheaper than cash, but giving it to that friend might prevent using a later present that could also be given to that friend with some saving, but if we skip, we could give the small present to a later friend who can only take that one. But as we argued, the later friend's cash cost is lower, so giving small present to the higher friend yields higher or equal saving compared to giving it to lower friend. And the higher friend might still be able to take a larger present, but the larger present's cost is higher, so the saving would be smaller. The total saving from giving small to high and large to low (if possible) vs small to low and large to high, we already compared and found that giving small to high + cash to low is at least as good as small to low + large to high (since c_large >= c_{k_low}). However, what if we have three friends: H (high), M (medium), L (low). Presents: small (j_s), medium (j_m) that only H and M can take? and large (j_l) that only H can take? Actually we need to think of more complex interactions.
Let's create a scenario with three friends: H (k_H=3), M (k_M=2), L (k_L=1). Presents: three presents: j=1 (c1=1), j=2 (c2=100), j=3 (c3=101). Cash: c3=101, c2=100, c1=1. Total cash sum = 101+100+1=202.
We have to assign presents (each at most once). Let's compute greedy:
Sorted k desc: 3,2,1.
ptr=0, present1: c1=1, k=3: 1<=3 and 1<101 -> assign to H, total=1, ptr=1.
ptr=1, present2: c2=100, k=2: 2<=2 and 100<100? false -> give cash to M: total+=c2=100 => 101.
ptr=1 (still), k=1: present2 index2 >1 -> give cash to L: total+=c1=1 => 102.
Total = 102.
Now consider alternative: Could we do better? What if we assign present1 to L, present2 to M, present3 to H? Check: L can take only present1 (since k=1). M can take present1 or present2 (since k=2). H can take any. Option: present1->L (cost1), present2->M (cost100), present3->H (cost101). Total = 1+100+101=202. That's worse than 102. Option: present1->M, present2->H, present3 unused? But H could take present2 (cost100) which is less than cash 101, and M could take present1 (cost1) which is less than cash 100. That would be: H gets present2 (100), M gets present1 (1), L gets cash 1. Total = 100+1+1 = 102. Same as greedy? Greedy gave H present1 (1), M cash 100, L cash 1 = 102. Alternative gives H present2 (100), M present1 (1), L cash 1 = 102. So same.
What if we assign present1->M, present2->H, present3 unused -> total =1+100+1(L cash) = 102. present1->L, present2->H, M cash =100 -> total =1+100+100? Wait compute: present1->L (1), present2->H (100), M cash (100) = 201. Not good. So best is 102.
Now, what if costs are different: c1=50, c2=60, c3=100, with k: H=3 (cash100), M=2 (cash60), L=1 (cash50). Cash sum = 100+60+50=210.
Greedy: sorted 3,2,1.
ptr0 present1=50, assign to H (since 50<100) -> total 50, ptr1.
ptr1 present2=60, for M: 2<=2 and 60<60? false -> give cash 60 -> total 110.
ptr1 present2 not usable for L -> give cash 50 -> total 160.
Alternative: assign present1 to L (50), present2 to M (60), H cash 100 -> total 210. Worse.
Alternative: present1 to M (50), present2 to H (60), L cash 50 -> total 50+60+50=160. Same as greedy? Greedy total=160. Alternative: present1 to M (50), present2 to H (60) => same total 160. So greedy optimal.
Now try to find a case where greedy might be suboptimal by not using a present that is slightly more expensive than cash for the current friend, but could be used for a later friend with even lower cash? That wouldn't help because using a present that is not cheaper than cash gives no saving or negative saving; so not using it is better.
Potential subtlety: The condition c[ptr] < c[k-1] to assign present ensures we only assign when it yields positive saving. But what if c[ptr] == c[k-1]? That yields zero saving; assigning it doesn't change total cost (since we pay same as cash). But if we assign it, we use up a present that might have been used later for a friend where it could yield positive saving? But later friends have cash <= current cash (since k descending). If c[ptr] == c[k-1], then for later friends, cash <= current cash, so c[ptr] >= cash? Actually if later friend's cash is less than or equal to current cash, and c[ptr] equals current cash, then c[ptr] >= later cash (since later cash ≤ current cash). So if we use this present for later friend, saving would be non-positive. So no benefit. So using it now is neutral, but not harmful. However, could there be a scenario where using a present with zero saving now prevents using it later with a friend who has same cash cost but could combine with something else? If cash cost same, zero saving as well. So no issue. So we can either assign or not; it doesn't affect total. Our algorithm chooses not to assign (since condition requires strict less). That's fine.
Now, consider case where we have multiple presents with same cost but different indices. Since costs are non-decreasing, equal costs may appear. In such case, the present with lower index is not strictly cheaper, but maybe it's cheaper in index sense? But cost is the same, so no saving. However, we might need to consider that using a present with equal cost could be beneficial if it allows us to use another present that yields saving? That seems unlikely because if the present cost equals the cash cost, using it just replaces cash with same cost, and we lose the present. But we could also not use it, and the friend pays cash. So same.
Thus greedy with strict inequality is safe.
But wait, there is an alternative approach: we could also think of it as we have two options for each friend: either cash or present. Since we can only use each present once, and the presents have increasing costs, it's like we want to allocate the cheapest presents to friends with largest k_i to maximize savings. This is essentially a matching problem that can be solved by sorting friends descending and presents ascending, then using two pointers.
But we need to be careful about the constraint j ≤ k_i. The greedy of just matching sorted friends descending with sorted presents ascending (starting from cheapest) fails if a friend's k_i is less than the index of the present? Because presents are indexed by order, not by cost. Actually we match present index (not cost) constraint j ≤ k_i. The cost order does not align perfectly with index order. However, since costs are non-decreasing, index order is correlated with cost. But it's possible that a present with lower index is cheaper, but its index also matters for the constraint. Our greedy uses the present's index (position) to check if it's allowed. It uses the pointer p which goes through presents in order of index (1,2,3,...). This ensures we only use each present once and we always consider the smallest-index unused present. Is it always optimal to use the smallest-index present rather than maybe skipping to a larger-index present? Suppose we have friend with high k, and the smallest unused present is index 1 with cost c1, but maybe we want to save index 1 for a later friend with low k who can only take index 1, while the high friend could take index 2 as well, and index 2 might be slightly more expensive but still provides saving. If we give index 1 to high friend, then low friend may get no present and have to pay cash, which might be suboptimal if the saving from giving index 1 to low friend is larger than the loss from giving index 2 to high friend instead of index 1. Let's analyze.
We already did for two friends, showing that giving index 1 to high and cash to low yields total = c1 + c_{k_low}, while giving index 1 to low and index 2 to high yields total = c1 + c2. Compare: we need to see if c1 + c_{k_low} <= c1 + c2? That is c_{k_low} <= c2. Since index2 > k_low? Actually in the scenario, low friend's k_low is such that index2 > k_low, so low friend cannot take index2. So the alternative is: index1 -> low, index2 -> high. Then total = c1 + c2. Greedy gives total = c1 + c_{k_low}. Since c2 >= c_{k_low} because index2 > k_low => c2 >= c_{k_low} (since costs non-decreasing). So greedy total <= alternative. So greedy is at least as good.
But what if there are more than one low friend who can only take index1? For example, two low friends with k=1, and one high friend with k=2. Presents: index1 cost 1, index2 cost 100. Cash: c1=1, c2=100? Actually