This is the easy version of this problem. The only difference is the constraint on k — the number of gifts in the offer. In this version: k=2.
Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky — today the offer "k of goods for the price of one" is held in store. Remember, that in this problem k=2.
Using this offer, Vasya can buy exactly k of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has.
More formally, for each good, its price is determined by a_i — the number of coins it costs. Initially, Vasya has p coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary:
Vasya can buy one good with the index i if he currently has enough coins (i.e p ≥ a_i). After buying this good, the number of Vasya's coins will decrease by a_i, (i.e it becomes p := p - a_i).
Vasya can buy a good with the index i, and also choose exactly k-1 goods, the price of which does not exceed a_i, if he currently has enough coins (i.e p ≥ a_i). Thus, he buys all these k goods, and his number of coins decreases by a_i (i.e it becomes p := p - a_i).
Please note that each good can be bought no more than once.
For example, if the store now has n=5 goods worth a_1=2, a_2=4, a_3=3, a_4=5, a_5=7, respectively, k=2, and Vasya has 6 coins, then he can buy 3 goods. A good with the index 1 will be bought by Vasya without using the offer and he will pay 2 coins. Goods with the indices 2 and 3 Vasya will buy using the offer and he will pay 4 coins. It can be proved that Vasya can not buy more goods with six coins.
Help Vasya to find out the maximum number of goods he can buy.
Input
The first line contains one integer t (1 ≤ t ≤ 10^4) — the number of test cases in the test.
The next lines contain a description of t test cases.
The first line of each test case contains three integers n, p, k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ p ≤ 2⋅10^9, k=2) — the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them.
The second line of each test case contains n integers a_i (1 ≤ a_i ≤ 10^4) — the prices of goods.
It is guaranteed that the sum of n for all test cases does not exceed 2 ⋅ 10^5. It is guaranteed that in this version of the problem k=2 for all test cases.
Output
For each test case in a separate line print one integer m — the maximum number of goods that Vasya can buy.
We need to solve problem: maximize number of goods that can be purchased with budget p, given special offer: can buy k goods (with k=2) paying only the price of the most expensive among them (i.e., the higher price). Also can buy individually paying full price. Each good at most once. n up to 2e5 per test, sum n <= 2e5.
We need to output max number of goods.
Observation: Since k=2, offer is "buy two items, pay the higher price". So effectively, if we pair items, we pay the max of the two prices. Also we can buy single items at full price.
Goal: maximize count under budget.
Classic problem: given prices a_i, we can either take single items at cost a_i, or take a pair at cost max(a_i, a_j). How to maximize count?
We can think of sorting prices. Usually, for k>1, a greedy approach works: sort ascending. Then we consider using offers on items from the end? Actually, typical solution for "k items for price of most expensive" is to sort descending? Let's think.
Given offer: buy two items, pay the higher price. So if we pair a cheap and an expensive item, we pay the expensive price, but we get both. So pairing cheap with expensive is not beneficial if cheap is very cheap? Actually, it's still beneficial because we get the cheap item for free (we only pay the expensive one). However, if we buy the expensive alone, we pay its price and get only it; if we pair with cheap, we get both for same price, so it's better. So it's always beneficial to pair items, as long as we can afford the expensive price.
But we must be careful: if we pair two items, we could instead buy only the expensive one (if we can't afford it?), but we can always buy individually. So to maximize count, we want to use as many offers as possible, but we also need to consider that using an offer uses up two items and costs the larger price. We want to pick items such that total cost of the "paid" prices (the ones we actually pay: each single item or each pair's max) doesn't exceed p.
We can treat each pair as we pay the larger price and get two items. So if we decide to use some number of pairs, we need to choose items accordingly.
Standard solution: sort prices ascending. Then iterate over possible numbers of items bought individually before switching to pairs? Actually, many solutions for this problem (like from Codeforces "Buying a TV Set"?) Not exactly. There's known problem: "Maximum number of items that can be purchased with discount: pay for every k-th item?" But this is different.
Observation: Since k=2, the pair discount essentially gives the cheaper item for free. So the "free" item must have price <= the paid item's price.
Thus, we can think: We will pay for some items (the ones that are the expensive in each pair, or singles). For each paid item, we may also get one free item (if we pair it) provided there exists an unpaid item with price <= that paid item. But each item can be used at most once.
Thus, the problem reduces to: Choose a set of items to pay for (call them "paid items") and assign to each paid item at most one "free" item (the cheap one) such that each free item's price <= the price of the paid item it's assigned to, and no item is used more than once. And total sum of prices of paid items <= p. Goal maximize total items (paid + free).
Additionally, paid items can be used without free items (i.e., single items), which is like assigning no free to them.
We want to maximize count. Since we can assign at most one free per paid item, the total number of items = #paid + #free, with #free <= #paid (since each free attached to a paid). So total items <= 2 * (#paid). But we want to maximize.
Given that, we would like to have many paid items? Not necessarily, because each paid item costs something; maybe using fewer paid items but each with a free yields higher total items for given budget? For a fixed budget, we want to minimize total cost per item acquired. For a single paid item, the cost per item is its price. For a pair, cost per item = price/2 (since we get 2 items for the price). So pairs are more efficient. So we want to use as many pairs as possible, i.e., we want to have as many paid items with a free assigned. But the free items must be cheaper than the paid.
So the optimal strategy likely involves sorting prices ascending and then choosing some prefix of cheapest items as free? Or maybe we should sort ascending and consider pairs from the largest? Let's examine examples.
Example: a = [2,4,3,5,7], p=6. Sorted: [2,3,4,5,7]. The optimal is buy 3 items: maybe buy 2 and pair 3 with 4? Actually from description: bought item 1 without offer (price 2). Then bought items 2 and 3 with offer paying 4. That is: paid items: price 2 (single) and price 4 (pair). Free item: price 3 (attached to price 4). So total items = 3. Paid sum = 2+4=6. Here we have one single and one pair.
If we try to use two pairs: we would need to pay two expensive items. Suppose we pair (2,3) pay 3, and (4,5) pay 5 total = 8 >6 not possible. Pair (2,4) pay 4 and (3,5) pay 5 total 9 >6. So only one pair possible.
Now consider if p is larger: example 2: n=5, p=11. Sorted [2,3,4,5,7]. Could buy 4 items: maybe pay 4 for pair (4,3) and pay 5 for pair (5,2) total 9? That uses items 2,3,4,5? Actually pairing (2,5) pay 5, (3,4) pay 4, total paid =9, items =4. And left 2 coins maybe can't buy 7? But 4 items achieved. Another possibility: pay 7 for pair (7,5) pay 7 and then buy 2 and 3 individually? That would be 2+3+7=12 >11. So 4 is max.
So pattern: we take some number of items as "free" and some as "paid". The decision is which items become paid.
Observation: Since we can assign at most one free to each paid, and free must be cheaper, an optimal assignment likely pairs each paid item with the most expensive possible free item that is <= it, but to maximize total count, we want to use as many items as possible. Typically, we sort ascending, and then consider a split: we will use some items as free (the cheaper ones) and some as paid (the more expensive ones), but the constraint that each paid can have at most one free means number of free <= number of paid. In such pairing, we can think of taking a suffix of items as paid (maybe including some cheap items also paid if needed). Actually, suppose we decide to buy exactly x pairs (so 2x items) and y singles (y items). Then total items = 2x+y, total cost = sum of the x paid items (the expensive ones from each pair) + sum of y singles.
But the set of items used includes: for each pair, two items: one paid (the higher price) and one free (the lower). And singles are just paid items.
We need to select which items are paid and which are free. For each paid item, we can optionally assign a free item (if available) that is not used elsewhere and has price <= the paid item.
To maximize count for a given set of paid items, we would assign the free items to the largest paid items? Actually we want to maximize number of free items, given we have a set of paid items. The maximum number of free items we can assign is min( #unassigned items that are cheaper than some paid ), but we need to assign each free to a distinct paid. Since each paid can take at most one free, the maximum free count = min( #paid, #cheaper items available). But we can also decide which cheaper items become free; we would want to use the most expensive among the cheap items as free? Actually it doesn't affect cost; cost only depends on paid items. So given the set of paid items, we can always assign free items from the remaining items, as long as for each paid we can assign a distinct item with price <= it. To maximize free count, we need to check if we can match each paid item with a distinct free item (cheaper) from the remaining pool. This is like a bipartite matching condition: sort paid items ascending, sort free candidates ascending; we need to be able to assign each paid a free <= it. This is possible if, after sorting both, the i-th paid (in sorted order) is >= the i-th free (in sorted order) of some selection. But we have freedom to choose which items become free from the remaining set. The necessary and sufficient condition for being able to assign free items to all paid items (i.e., get #free = #paid) is that among all items, there are at least 2paid distinct items, and the smallest 2paid items? Not exactly.
But we can structure it differently: Since we can also have singles (paid without free), maybe it's easier to think in terms of choosing the number of items we take, and then checking if we can arrange purchases within budget.
Alternatively, there is known solution for this problem (k arbitrary but sum n limited). For k=2, it's simpler. Many solutions use DP or greedy. Let's think about a greedy algorithm: sort the array. Then consider two strategies: buying items from the left (cheapest) individually, and then buying pairs from the right? Actually, typical solution for "k items for price of the most expensive" is to sort ascending and then consider taking the items in groups of k, but paying the most expensive in each group. Since we can also take singles, we need to find the maximum possible count.
I recall Codeforces problem 1437C? No. Another problem: "Buying a TV Set"? Not.
Maybe it's helpful to think about dynamic programming: For each item sorted, we can decide to either buy it singly, or use it as the expensive item in a pair with a cheaper one that we haven't used yet. Since k=2, the cheaper must be taken from items before? Because if we sort ascending, then for any pair, the expensive is a later (higher) element. So we can consider processing sorted prices, and maintain which items are taken as free.
Standard approach: Sort a ascending. Then we try to buy items from the rightmost (most expensive) using offers, because it's cheaper per item to buy two with the higher price than to buy them singly. But the budget may limit.
Actually, let's derive optimal selection.
Let the sorted array be b[1..n] (1-indexed) with b1 <= b2 <= ... <= bn.
Suppose we decide to buy t items. Can we achieve that with budget p? This is a decision problem. Since t up to n, we could binary search t and check feasibility. That might be O(n log n) per test, acceptable with n up to 2e5 total, t up to 1e4 but sum n limited, so O(n log n) overall is fine. But we need efficient check.
Check feasibility for a given t: can we select t items and pay total cost <= p, using allowed purchase operations.
Given we have t items selected. How to minimize cost for that set? We can use offers to reduce cost. For the set S of selected items, we can partition them into pairs (each pair we pay the max of the two) and singles (pay full). To minimize total cost, we would like to pair items such that each pair's max is as low as possible, but we also want to pair as many as possible because pairing reduces cost: for two items with prices x <= y, buying singly costs x+y, pairing costs y. So pairing saves x. So we want to maximize total savings = sum of cheaper items in each pair. So given set S, the minimal cost is achieved by pairing the smallest items with larger ones in an optimal way: we want to pair each of the largest items with a smaller one to "cover" the cheaper one for free. However, we cannot pair more than floor(|S|/2) pairs. Each pair saves the cost of the cheaper item. So total cost = sum(S) - sum of savings, where savings = sum of prices of items that are taken as free (i.e., the cheaper in each pair). But we cannot save on all items; we can save on at most floor(t/2) items, and those must be the cheapest among the selected? Not necessarily: Suppose we have selected items with prices [1,100,101]. To minimize cost, we could pair 1 with 100 (pay 100, save 1) and buy 101 singly -> total cost 201. Or pair 1 with 101 (pay 101, save 1) and buy 100 singly -> total cost 201. Or not pair: all singly = 202. So minimal cost = 201. Here the free item is the cheapest. If we selected [50,60,70], pairing any two yields cost = max of pair + remaining singly. For [50,60,70], if we pair 50&70 -> pay 70, save 50, plus 60 singly => 130. If we pair 50&60 -> pay 60, save 50, plus 70 => 130. If we pair 60&70 -> pay 70, save 60, plus 50 => 120? Actually pairing 60&70 -> pay 70, save 60, plus 50 singly => 120. So pairing the two largest gives savings of the larger of the two? Wait: In pair (60,70), cheaper=60, expensive=70, cost=70, save 60. So total cost=70+50=120. This is lower than pairing 50 with 70 (130). So pairing the two largest and leaving the smallest as single yields cost = max(largest two) + smallest. But compare sum = 180. Pairing largest two saves the second largest; pairing largest with smallest saves the smallest. Since second largest > smallest, saving more is better. So to maximize savings, we should pair the largest items with the next largest possible items? Actually, we can only pair items, and each pair saves the cheaper of the two. So to maximize total savings, we want the saved items (the cheaper in each pair) to be as expensive as possible, because savings subtract from sum. But there's a constraint: for each pair, the saved item must be cheaper than the paid item. So if we sort the selected set, we can consider pairing the largest with the next largest, then the third largest with the fourth largest, etc. That would give savings from the second largest, fourth largest, etc. Or we could pair largest with smallest, then second largest with second smallest, etc. Which yields more savings? Let's compare for a sorted list of selected items: x1 <= x2 <= ... <= xt.
We want to partition into pairs and possibly one single if t odd. For each pair (a,b) with a <= b, cost = b, saving = a. Total cost = sum of all - sum of saved = sum of all - sum of saved. To minimize cost, we want sum of saved (the cheaper items in pairs) as large as possible, because we subtract them. However, note that each pair contributes exactly one saved (the cheaper). So we choose which items become the saved (free) items. The number of saved items is floor(t/2). They must be distinct and for each saved item, there must be a distinct paid item (the expensive) with price >= saved. Moreover, the paid items are the items not saved (plus possibly the single if t odd). So essentially, we need to select a subset of size floor(t/2) to be free (saved), and the rest (t - floor(t/2)) to be paid. The condition is that for each free item, there is a distinct paid item that is >= it. Since we can assign, a necessary and sufficient condition is that the largest floor(t/2) items can serve as paid? Actually, we can think of matching: if we sort the selected items, we can pair the smallest with the largest, second smallest with second largest, etc. That yields saved items = the smallest in each pair. The saved sum = sum of the smaller items in each pair, which is sum of the first floor(t/2) items (if we pair (1,t), (2,t-1), ...). That sum is relatively small. Alternatively, if we pair adjacent items from the sorted list: (x1,x2), (x3,x4), ... then saved items = x1, x3, ... which are the odd-indexed items (the smaller of each adjacent pair). That sum may be larger or smaller than the previous? For example, [1,2,100,101]: t=4. Pair smallest-largest: (1,101) cost 101, (2,100) cost 100, total = 201, saved =1+2=3, sum =204-3=201. Pair adjacent: (1,2) cost 2, (100,101) cost 101, total=103? Wait cost: for pair (1,2) pay 2, (100,101) pay 101, total=103? That seems too low because sum is 204, savings would be 1+100=101, cost=103. Check: cost = max(1,2)=2 + max(100,101)=101 =103. Yes! That's much lower. So pairing adjacent (largest with second largest, etc.) yields larger savings because the saved items are the smaller of each pair, which can be relatively large if the two numbers are close. In the adjacent pairing, saved items are x2? Actually for (1,2), saved =1; for (100,101), saved=100. So saved sum = 101, which is higher than 3. So we want to maximize sum of saved items, so we want the saved items to be as large as possible, while respecting that each saved must be paired with a larger paid. If we sort, the natural optimal matching for minimizing cost is to pair the largest with the next largest, the third largest with the fourth largest, etc. Because then each saved item is the smaller of the two, which is as large as possible (since the pairs consist of items with similar values). Formally, given sorted list x1..xt, to maximize sum of the cheaper items in pairs (i.e., the sum of the minima of each pair), under the constraint that each item belongs to exactly one pair or is single, the optimal is to pair consecutive items after sorting. This is a known result: to maximize sum of minima in pairings, you should pair adjacent numbers (because if you have a<b<c<d, pairing (a,d) and (b,c) gives minima a and b; pairing (a,b) and (c,d) gives minima a and c, which is larger if c>b). So adjacent pairing yields larger sum of minima. Therefore minimal total cost for set S is achieved by pairing items as much as possible and pairing the largest with the next largest, etc.
Moreover, if t is odd, we will have one unpaired item (which we buy singly). Then the minimal cost = sum of all - sum of the floor(t/2) smallest items among the paired? Actually we need to compute properly.
Let's formalize: S sorted ascending. We consider partitioning into pairs and possibly one single. Let the items be arranged such that we take the largest 2q items (where q = floor(t/2)) and pair them among themselves in a way that the smaller of each pair is as large as possible. To maximize the sum of the smaller items, we should pair the largest with the second largest, third with fourth, etc., within that set. So the paired items are the 2q largest items. The unpaired items (if any) are the remaining t-2q = t mod 2 items, which are the smallest ones. For odd t, t=2q+1, we have the smallest item as single, and the 2q largest items are paired.
Then total cost = (sum of all items) - (sum of the smaller items in each pair). For the 2q largest items paired as described, the smaller in each pair are: the second largest, fourth largest, ... i.e., the items at positions (t-2), (t-4), ...? Let's index from smallest (1) to largest (t). Items: x1 <= x2 <= ... <= xt.
If t=2q, we pair (x_{2q-1}, x_{2q})? Actually to pair adjacent from the top: we pair (x_{t}, x_{t-1}) as one pair, (x_{t-2}, x_{t-3}) as next, etc. So the smaller in each pair are x_{t-1}, x_{t-3}, ..., x_{1?} No, if t is even, the pairing covers all items. Let's define: For i from 1 to q, pair x_{2i-1} and x_{2i}? That's adjacent from the bottom. But we argued bottom adjacent gives small savings. So we want top adjacent: pair x_{t} with x_{t-1}, x_{t-2} with x_{t-3}, ... So the smaller items are x_{t-1}, x_{t-3}, ... etc. This indeed gives larger saved sum. However, note that if we pair from the top, we are using the largest items as paid and the second largest as free? But is that allowed? In a pair (x_{t}, x_{t-1}), we pay max = x_t (largest) and get x_{t-1} free. So saved = x_{t-1}. So we are saving a relatively large item, which reduces cost. That seems optimal because we save a high value rather than a low one.
But wait, is there any constraint that the free item must have price <= the paid? Yes, x_{t-1} <= x_t, so fine.
Now, what about the remaining items? If t is even, we have used all items. If t is odd, we have one leftover, the smallest, which we buy singly.
Thus, for a given t, the minimal achievable cost is:
Sort all selected items? But we haven't selected which t items. We want to know if there exists a subset of size t such that the minimal cost for that subset <= p. Since we can choose which items to buy, we can choose the t items that minimize this cost.
So we need to choose the t items that yield the smallest minimal cost. Since we are free to pick any t items, we want the cheapest cost for any t items. So we need to compute, for each possible t, the minimum possible cost to buy t items.
Given that we can use offers, the best set of t items likely consists of the t cheapest items? Not necessarily, because we can get some expensive items for "free" if paired with a slightly more expensive? But free items are the cheaper ones in each pair, so if we take some expensive items, they might serve as the paid items, and we can get some moderately expensive items for free, reducing effective cost per item. For example, consider prices [1,100,101]. The cheapest set of 2 items is [1,100] with minimal cost? For 2 items, we can pair them, cost = max(1,100)=100. That's cost 100 for 2 items. The set [100,101] would cost max(100,101)=101. So cheaper set gives lower cost. For 3 items, the only set is all three, cost as earlier: 201? Actually minimal cost = 201 (since we can pair two and buy one singly). If we had a set of 3 items that are cheaper? But we need to pick any 3; there are only 3, so that's it. In general, does picking cheaper items reduce cost? Let's test: Suppose we have [2,3,100,101]. For t=3, we could choose the three cheapest: [2,3,100]. Minimal cost? Pair the two largest among these three: sort [2,3,100]. Using optimal pairing: pair 100 and 3? Actually for 3 items, we pair the two largest (100 and 3) -> cost = 100, and single 2 => total 102. Alternatively, pair 2 and 100 -> cost=100+3? Wait careful: For set S sorted [2,3,100]. Optimal: we can either have one pair and one single. To maximize savings, we pair the two largest: 3 and 100? Actually largest two are 3 and 100, but 3 is not the largest? Sorted: x1=2, x2=3, x3=100. Largest two: x2 and x3. Pair them: (3,100) cost=100, saved=3, plus single 2 -> total cost=102. Alternatively pair (2,100): cost=100, saved=2, plus single 3 -> total 103. So minimal cost=102. Another set: [2,100,101] cost = pair 100,101 -> cost=101, plus single 2 -> total 103. So cheaper set gives lower cost. For t=4 (all items), cost: pair top two (100,101) cost=101, pair next two (2,3) cost=3? Actually pair (2,3) cost=3, total=104. So cost 104. That's higher than budget maybe? But anyway.
Thus, it seems selecting the t cheapest items gives the minimal total cost, because any replacement with a more expensive item would increase either the sum or the pairing cost. But is it always optimal to take the t cheapest? Let's reason: The cost formula for a given set S depends on the sorted order. Let's define for a set S, after sorting, the minimal cost C(S) = if |S| even: sum_{i=1}^{|S|} x_i - sum_{j=1}^{|S|/2} x_{2j-1}? Wait careful with top pairing: For even t=2q, we pair (x_{2q}, x_{2q-1}), (x_{2q-2}, x_{2q-3}), ... (x2, x1)? That would be pairing from top, but then the smaller items are x_{2q-1}, x_{2q-3}, ..., x1. That is essentially the odd-indexed items if we index from 1 to 2q. But note: x1 is smallest. So saved items would include the smallest, which seems not optimal? Let's recompute properly with top-adjacent pairing.
Take sorted ascending: x1 <= x2 <= ... <= xt.
Option A: Pair from the top: (xt, x_{t-1}), (x_{t-2}, x_{t-3}), ... This yields saved items: x_{t-1}, x_{t-3}, ... . For t even, we have pairs covering all. For t=4, pairing (x4,x3) and (x2,x1). Saved items: x3 and x1. Cost = (x4 + x3 + x2 + x1) - (x3 + x1) = x4 + x2. So cost = x4 + x2. Option B: Pair from the bottom: (x1,x2), (x3,x4) yields cost = x2 + x4. Actually same? For t=4, bottom pairing gives saved = x1 + x3, cost = x2 + x4. So it's identical! Because x2 + x4 = x4 + x2. So for even t, both pairing methods yield the same cost? Let's check: In top pairing we got cost = x4 + x2. In bottom pairing cost = x2 + x4. So they are equal. Wait, that suggests that for even t, any perfect matching that pairs each item with a distinct partner yields the same cost? No, not any matching. Let's test with specific numbers: x=[1,2,3,100]. Bottom pairing: (1,2) cost 2, (3,100) cost 100 => total 102. Top pairing: (100,3) cost 100, (2,1) cost 2 => total 102. So same. Pair (1,100) cost 100, (2,3) cost 3 => total 103. So only some matchings give minimal cost? The minimal cost appears to be x2 + x4? But in [1,2,3,100], x2=2, x4=100 => total 102. So the minimal is 102. Pair (1,100)+(2,3) gives 103, not minimal. So minimal cost for even t is sum of the largest item in each pair being the larger? Actually we need to derive the general minimal cost formula.
I recall known result: For the problem "pairs cost max of two", the minimum total cost to buy all items in a set is equal to sum of the items at even positions (if sorted ascending) when the number of items is even? Or maybe odd? Let's test: [1,2,3,100] sorted: positions 1,2,3,4. Sum of even positions: x2 + x4 = 2+100=102. That matches. For [1,4,5,7] sorted: x1=1,x2=4,x3=5,x4=7, even sum = 4+7=11. Check: minimal pairing? Pair (1,4) cost4, (5,7) cost7 => total11. Yes. For odd t? t=3, sorted x1,x2,x3. What is minimal cost? Example [1,2,3]: we can pair (2,3) cost3, single1 => total4. Or (1,3) cost3, single2 => total5. So minimal = x1 + x3? Actually 1+3=4 matches. Another [1,2,100]: pairing (2,100) cost100, single1 =>101, sum =101. x1 + x3 =1+100=101. Yes. Another [2,3,4]: x1=2,x2=3,x3=4, x1+x3=6. Check: pair (3,4) cost4 + single2 =6. Good. So for odd t, minimal cost seems = x1 + x3 + x5 + ...? i.e., sum of the items at odd positions? Let's test: t=3, odd positions 1 and 3 sum = x1+x3. t=5? Suppose [1,2,3,4,5]. According to formula maybe cost = x1 + x3 + x5? That would be 1+3+5=9. Let's compute minimal: We want to maximize savings. For 5 items, we can have 2 pairs and 1 single. Pair the two largest: (4,5) cost5 saved4? Actually we need to decide pairing. Approach: take largest 4 items (2,3,4,5) and pair them optimally: pair (5,4) cost5 saved4, and pair (3,2) cost3 saved2? But that uses 2,3,4,5 and leaves 1 as single. Cost = 5+3+1=9. Yes matches. Another pairing: (5,2) cost5 saved2, (4,3) cost4 saved3, plus 1 => 5+4+1=10. So minimal 9. So pattern: For sorted ascending, minimal cost = sum of elements at odd indices (1-indexed) if t is odd? For t=5, odd indices: 1,3,5 => sum = x1+x3+x5 = 1+3+5=9. For t=3, x1+x3 = 1+3=4. For t=1, cost = x1, which is odd index sum (x1). For t even, minimal cost = sum of elements at even indices? For t=4, even indices: x2+x4 = 2+4? Wait [1,2,3,4] even indices sum =2+4=6. Check minimal cost: pair (1,2) cost2, (3,4) cost4 => total6. Yes. For [1,2,3,100] even indices sum=2+100=102. So pattern holds.
Let's prove: For a set S sorted ascending, the minimal total cost to purchase all items (using the offer optimally) is:
If |S| = 2q: cost = sum_{i=1}^{q} x_{2i} (i.e., the even positions).
If |S| = 2q+1: cost = sum_{i=0}^{q} x_{2i+1} (i.e., the odd positions). (Indexing from 1)
Why? We can think: The cost can be derived from the idea that we can think of paying for every second item starting from the second? Actually, we can transform: In each pair, we pay the larger; when we have an odd count, the smallest is left as single. So essentially, after sorting, we pay for items at positions 2,4,6,... and also possibly position 1 if odd? Let's test: For odd: positions 1,3,5,... are paid. For even: positions 2,4,6,... are paid. So pattern: we pay for items at positions: if t even, pay indices 2,4,...,t; if t odd, pay indices 1,3,5,...,t.
This is plausible as the optimal strategy: Buy the cheapest item alone if odd, and then for the rest, pair each with a more expensive one such that the cheaper is free. The optimal pairing is to pair the second cheapest with the cheapest? Hmm. Actually, for even t, the optimal is to pair (1,2), (3,4), ...? That yields paying for the even indices (the larger in each adjacent pair). That's exactly the pattern. But earlier we thought pairing from top gives same cost. So indeed the minimal cost is achieved by pairing adjacent items after sorting, paying the larger in each pair, and if odd, the smallest remains single. Because this pairing yields cost = sum of evens (if even) or sum of odds (if odd). And we can argue that any other pairing cannot reduce cost below that. This is a known result: For the "pair and pay max" problem, the greedy pairing of sorted items from smallest to largest (i.e., consecutive) gives minimal total cost. Actually, we need to verify with examples where there is a big gap: [1,50,51,100] sorted. Adjacent pairing: (1,50) cost50, (51,100) cost100 total=150. Another pairing: (1,100) cost100, (50,51) cost51 total=151. So adjacent is better. So yes.
Thus, the minimal cost for a given set S is sum of elements at specific positions after sorting S: the positions that are "paid". Those positions are determined by the parity of the number of items taken from the sorted order of S. But crucially, if we choose a subset S of size t, its sorted order is not the same as the global sorted order. However, we are free to choose S arbitrarily. To minimize the cost, we would like the paid items (as per the rule) to be as cheap as possible. That suggests we want the items that end up in paid positions to be cheap. Since the paid positions are either all even positions in S (if t even) or all odd positions (if t odd), we can choose S to minimize the sum of those positions.
One common approach in such problems (like CF 1472D? Actually there is problem "Buying gifts" etc.) is to sort all items globally, then consider taking some number of items, and the cost can be computed from the global sorted order if we take a prefix? But not exactly because we may skip some cheap items to get better pairing? Let's test.
If we take the t cheapest items (a prefix of the sorted array), then after sorting that prefix (which is just the prefix), the paid indices are determined as above. The cost would be sum of some of these items (either all at even positions in the prefix, or odd). Would taking the t cheapest always yield minimal cost? Possibly yes, because any alternative set would involve including a more expensive item while excluding a cheaper one. That would increase the sorted list at some positions, likely increasing the paid sum. But we need to check if there are cases where excluding a cheap item and including a more expensive one could reduce the cost, due to the pairing rule shifting which items become paid. For example, suppose we have items: [1,100,101]. For t=2, the cheapest two are [1,100] with cost = even positions? Since t=2, cost = x2 (second item) = 100. If we take [100,101], cost = second item = 101. So cheaper set is better. For t=3, only one set. For t=2, maybe there is a scenario where [cheap, expensive] cost = expensive; but if we take two moderately priced items, maybe cost is lower? For instance, items: [1,10,11]. Cheapest two: [1,10] cost=10. Alternative: [10,11] cost=11. So cheapest wins. Could there be a situation where the cheapest two include a very cheap item that ends up as paid? In t=2, paid is the larger, so the cheap item is free. So including a very cheap item doesn't hurt; it's free. So it's beneficial to include cheap items because they can be free. Thus, to minimize cost for a fixed t, we would like to include as many cheap items as possible because they can serve as free items, reducing the paid sum. However, if we include too many cheap items, does it force some expensive items to become paid? The paid positions are determined by the order. For t even, paid are items at even positions. So if we take a set that includes many cheap items, they will occupy the early positions (since sorted), leaving the more expensive items at later positions. In even t, paid are even positions, which include some of the cheap items if they fall on even positions. For example, take t=4 from list: [1,2,3,100]. Cheapest 4 are exactly those: cost = even positions: x2=2, x4=100 => 102. If we instead take [2,3,100,101] (excluding 1, including 101), sorted: [2,3,100,101] cost = even positions: 3+101=104, higher. So including 1 instead of 101 reduces cost. So cheapest prefix seems best.
But what if we consider t odd? For t=3, cheapest three: [1,2,3] cost = odd positions: x1+x3=1+3=4. If we take [2,3,100], cost = x1+x3 =2+100=102, worse. So again cheapest wins.
Thus, intuition: the optimal set of t items to minimize cost is the t cheapest items globally. Let's attempt a proof: Suppose we have sorted global array b1 ≤ b2 ≤ ... ≤ bn. For any set S of size t, after sorting S, we have a sequence of t items. Compare with the set P consisting of the t smallest items globally. We want to show that the minimal cost for S (as per the rule) is at least that for P. This is akin to saying that for each index i in the sorted order of S, the i-th smallest in S is ≥ the i-th smallest in P (since P consists of the globally smallest t items). That is, for all i, s_i ≥ p_i. Then we need to compare the cost formulas. For even t: cost(S) = sum_{i even} s_i, cost(P) = sum_{i even} p_i. Since s_i ≥ p_i for each even i, we have cost(S) ≥ cost(P). For odd t: cost(S) = sum_{i odd} s_i, and similarly s_i ≥ p_i for odd i, so cost(S) ≥ cost(P). So indeed, taking the t globally smallest minimizes the cost. This argument assumes that the cost function is monotonic non-decreasing in each s_i for the positions that are summed (the paid positions). Since cost(S) is a sum of a subset of its sorted elements (specifically, every other one starting from a certain offset), and s_i are sorted, we have the component-wise dominance. Because p_i are the smallest possible for each rank i, any other set will have larger or equal values at each rank. Therefore, the minimal cost for buying exactly t items is achieved by taking the t cheapest items globally, and the cost is computed as described on that prefix.
Thus, the problem reduces to: sort the array a ascending. For each possible t from 0 to n, compute the minimal cost C(t) to buy t items, which is sum of certain elements in the prefix of length t (specifically, if t even: sum of elements at indices 2,4,...,t in the sorted list; if t odd: sum of elements at indices 1,3,...,t). Then find the largest t such that C(t) ≤ p.
We need to compute these prefix sums efficiently. Since n up to 2e5, we can precompute prefix sums of the sorted array and then compute C(t) using those sums. Let's denote sorted array as b[1..n]. For t even: t=2q, C(2q) = b[2] + b[4] + ... + b[2q]. For t odd: t=2q+1, C(2q+1) = b[1] + b[3] + ... + b[2q+1].
We can compute two prefix sums: one for odd indices (1-based), and one for even indices. Then C(t) can be obtained accordingly.
But careful: The cost formula derived is for the set being exactly the prefix of length t? That's what we argued: the cheapest t items. So indeed, for the sorted b, the cost for the first t items (the t cheapest) is as above.
However, we must ensure that the pairing that achieves this cost is feasible within the offer rules. According to our earlier reasoning, the optimal strategy for a given set is to pair adjacent items (in sorted order) and pay the larger in each pair, and if odd, keep smallest as single. But does this pairing respect the condition that the free item's price does not exceed the paid item's price? Yes, because in each pair, the smaller is free and the larger is paid. So it's valid.
Thus, solution is straightforward: sort, compute prefix sums for odd and even positions, then iterate t from n down to 0 (or binary search) to find max t with C(t) <= p.
But let's test with provided examples.
Example 1: n=5, p=6, k=2. Sorted b = [2,3,4,5,7] (since input: 2,4,3,5,7 -> sorted: 2,3,4,5,7). Compute C(t):
t=0: 0
t=1: odd: b1=2 => 2
t=2: even: b2=3? Wait t=2 even -> sum even indices among first 2: b2=3? Actually b[2]=3? Sorted: index1=2, index2=3, index3=4, index4=5, index5=7. So b2=3. So C(2)=3.
t=3: odd: b1+b3=2+4=6
t=4: even: b2+b4=3+5=8
t=5: odd: b1+b3+b5=2+4+7=13
Find max t with C<=6: t=3 (C=6) works. Output 3. matches.
Example 2: p=11: C(3)=6, C(4)=8, C(5)=13. So t=4 (8<=11) is possible, output 4. matches.
Example 3: n=2, p=10000, a=[10000,10000] sorted [10000,10000]. C(1)=10000, C(2)= even: b2=10000 =>10000. Both <=10000, so max t=2. Output 2. matches.
Example 4: p=9999, same array. C(1)=10000 > 9999, so t=0. Output 0. matches.
Example 5: n=5, p=13, a=[8,2,8,2,5] sorted: [2,2,5,8,8]. Compute C:
t1: odd:2
t2: even: b2=2 =>2
t3: odd: b1+b3=2+5=7
t4: even: b2+b4=2+8=10
t5: odd: b1+b3+b5=2+5+8=15
Max t with C<=13: t=4 (10<=13) works, maybe t=5 is 15 >13. So output 4. matches.
Example 6: n=3, p=18, a=[1,2,3] sorted [1,2,3]. C1=1, C2=2, C3=1+3=4. All <=18, max t=3. Output 3. matches.
All examples consistent.
Thus, solution is correct.
Now we need to implement for t test cases, sum n <= 2e5. We'll sort each test case's array, then compute prefix sums for odd and even indices. Then we can find max t via a loop from 0 to n (or binary search). Since n per test case can be up to 2e5 but sum total is 2e5, O(n log n) sorting is fine. Then computing C(t) per t can be O(1) after precomputing two prefix sum arrays.
But we need to be careful: The cost formula uses indices in the sorted order, not the original. So we need to compute prefix sums for odd and even positions.
Let sorted = a[0..n-1] (0-indexed). For convenience, convert to 1-indexed b[1..n].
Define:
pref_odd[i] = sum of b[j] for j odd, j<=i.
pref_even[i] = sum of b[j] for j even, j<=i.
Alternatively, we can compute C(t) as:
if t % 2 == 0:
C = pref_even[t] (since even indices up to t)
else:
C = pref_odd[t] (since odd indices up to t)
But careful: For t odd, we sum odd indices up to t. For t even, sum even indices up to t. That matches.
We can loop from n down to 0, but n can be up to 2e5 per test, sum 2e5, so O(n) per test is fine. Or binary search O(log n) but O(n) is fine because total n sum = 2e5. So we can just iterate t from 0 to n and keep track of max satisfying. Actually better to loop from n downwards and break when condition satisfied; that might be faster. But either works.
Edge Cases: p may be huge, but a_i up to 1e4, so sums up to 2e9 (2e5 * 1e4 = 2e9) which fits in 64-bit. Use long long.
Now we need to confirm that the assumption that taking the t cheapest items is always optimal holds for all cases, even when using the offer? Let's double-check with a more complex scenario. Could there be a situation where we can buy more items by skipping some cheap items and taking more expensive ones? For example, suppose we have prices: [1, 100, 101, 102]. Budget p = 202. The cheapest 4 items cost (even indices sum) = b2 + b4 = 100 + 102 = 202, so we can buy all 4. That's max. If budget were 201, cheapest 4 cost 202 >201, maybe we can buy 3 items. Cheapest 3 cost = b1+b3 = 1+101 = 102 <=201, so we can buy 3. But could we buy 3 items including a more expensive one? E.g., take [100,101,102] cost = odd sum = 100+102=202 >201, worse. So cheapest works.
Now consider case where cheap items are many but have high values? But cheap means low price. Including a cheap item can only help reduce cost because it can be free. So indeed, for a given t, the cheapest t minimize the sum of paid positions, as argued by component-wise comparison.
But wait: Is there any scenario where the pairing used for the cheapest t set is not feasible because the free item must be exactly one per paid, and we need to ensure that the free items are indeed not exceeding the paid items? In cheapest t set, pairing as described is feasible because we pair adjacent in sorted order, so each free (the smaller) is indeed ≤ paid. So okay.
Thus, solution is correct.
Now implement:
For each test case:
read n, p, k (k=2 but we can ignore)
read array a (n ints)
sort a
create vector b (1-indexed) maybe just use 0-index but compute prefix sums accordingly.
Let's use 0-index and compute prefix sums for odd/even as per 0-index parity? Easier to use 1-index to avoid confusion. We'll convert.
Implementation steps:
code
int t;
cin >> t;
while(t--) {
int n, p, k;
cin >> n >> p >> k;
vector<long long> a(n);
for(int i=0;i<n;i++) cin >> a[i];
sort(a.begin(), a.end());
// now a is sorted ascending, 0-indexed.
// We'll create prefix sums for odd and even positions considering 1-indexed.
vector<long long> odd(n+1,0), even(n+1,0); // 1-indexed, size n+1
for(int i=1;i<=n;i++) {
if(i%2==1) { // odd index
odd[i] = odd[i-1] + a[i-1];
even[i] = even[i-1];
} else {
even[i] = even[i-1] + a[i-1];
odd[i] = odd[i-1];
}
}
int ans = 0;
for(int cnt=1; cnt<=n; cnt++) {
long long cost = (cnt%2==0) ? even[cnt] : odd[cnt];
if(cost <= p) ans = cnt;
}
cout << ans << endl;
}
But careful: The prefix sums as built: For i=1 (odd), odd[1] = a0, even[1]=0. For i=2 (even), even[2]=a1, odd[2]=odd[1]=a0. Works.
Alternatively, we can do binary search: while low<=high... But linear scan is O(n) per test, total O(2e5) which is fine.
But we must consider the possibility that we can buy more than the prefix scheme? Could there be a situation where we don't take a contiguous prefix of sorted items but achieve same t with lower cost? Our proof says no. But let's double-check with a tricky case.
Suppose a = [1,1,100,100,100]. Budget p=200. Cheapest 5 items cost? Sorted: [1,1,100,100,100]. t=5 odd: cost = odd indices sum: indices 1,3,5 = 1+100+100 = 201 >200. t=4 even: indices 2,4 = 1+100=101 <=200. So max t=4 from prefix. But could we buy all 5 items with a different selection? For example, select items: [1,100,100,100,100]? But we only have three 100's. Actually we have only three 100s. So cannot get 5 items not using all items because total items are 5. The only set of 5 items is all five. So t=5 cost 201 >200, so cannot buy 5. Could we buy 5 with a different pairing that reduces cost below 201? The minimal cost for the set of all 5 is 201, as computed. So no.
What about a scenario where the cheapest t set yields cost > p, but a different set of t items yields cost <= p? That would contradict our claim that cheapest t set minimizes cost. So we need to verify the monotonicity argument carefully: For any set S of size t, let s_i be the i-th smallest in S. For the set P of the t globally smallest, we have p_i = b_i (the i-th smallest globally). For each i, s_i ≥ p_i because the t globally smallest are the smallest possible values at each rank. The cost function C(S) is sum_{i in I(t)} s_i where I(t) is either all even i (if t even) or all odd i (if t odd). Since for each i in I(t), s_i ≥ p_i, we have C(S) ≥ C(P). So indeed P gives minimal cost.
Thus, if C(P) > p, no other set of size t can be affordable. So we are safe.
Hence solution is correct.
But wait: The cost formula we used for even t sums even positions. For example, t=2, even indices: index2. For t=2, the minimal cost is indeed the larger of the two, which equals the second smallest in the set. Since the set is the two cheapest, that's b2. Could there be a cheaper way to buy two items if we don't take the two cheapest? For two items, the cost is max of the two. The minimal possible max for any two items is the smallest possible value of the larger element in a pair, which is achieved by taking the two smallest items (the second smallest). So indeed, the two cheapest minimize cost.
All good.
Now let's implement as described. But need to note that k=2 is fixed, but the problem statement says k=2. However, we might consider if k could be other values? But in this easy version, k=2. So we can ignore k.
Now, the solution complexity O(n log n) sorting per test, plus O(n) scanning.
Potential pitfalls: The input constraints: sum n over test cases <= 2e5, so using O(n log n) sorting per test total O(2e5 log 2e5) is fine. Use long long for prefix sums because a_i up to 1e4 and n up to 2e5 => sum up to 2e9, fits in 32-bit signed? 2e9 < 2^31-1 (approx 2.147e9), so it's borderline but safe for 32-bit? Actually 2e5 * 1e4 = 2e9 exactly. But p can be up to 2e9, also fits. However, when we sum only even indices, max sum could be up to 1e4 * 1e5 = 1e9, which is fine in int. But to be safe, use long long.
Now, could there be a scenario where using the offer in a different way (e.g., not pairing adjacent in sorted order) could yield lower cost for a given set? We argued adjacent pairing is optimal. Let's prove quickly: For a sorted list, the minimal cost to purchase all items is the sum of the max in each pair (if we pair). The pairing problem is to partition into pairs (and possibly one single) to minimize sum of maxima. It's equivalent to: we need to select which items are "paid". Each paid item covers itself and possibly one free item ≤ it. Since each free must be ≤ its paid and each paid can cover at most one free, the optimal is to have the paid items be as cheap as possible while still covering the free ones. This is like covering the set with pairs. The known optimal strategy: sort and pair consecutively. Proof by exchange argument: If there is a pair with a gap, swapping can't increase cost.
Thus, our formula holds.
Now implement.
But wait: The operation description says: "Vasya can buy a good with the index i, and also choose exactly k-1 goods, the price of which does not exceed a_i, if he currently has enough coins (i.e p ≥ a_i). Thus, he buys all these k goods, and his number of coins decreases by a_i". So when using offer, you must pick the paid item (i) and then choose k-1 other goods whose price <= a_i. This means the paid item is the expensive one (since you pay its price and the free items must have price ≤ it). So our model matches: For each paid item, we can assign up to k-1 free items (with k=2, exactly 1 free). So our pairing is exactly that: each paid item can have at most one free. So it's consistent.
Now consider if we could assign a free item to a paid item that is not the larger one? Actually the rule says you choose the paid item i and then choose k-1 goods with price not exceeding a_i. That implies a_i is the most expensive among the k items, because the chosen others have price ≤ a_i. So yes, a_i is the max.
Now, in our pairing strategy for the prefix, we pair items as (b1,b2), (b3,b4), ...? But then the paid items are the even indices (b2,b4,...). However, note that in pair (b1,b2), b2 is the paid, and b1 is free (since b1 ≤ b2). So that's valid. In pair (b3,b4), paid = b4, free = b3. So indeed, the paid items are the even-indexed ones, which are not necessarily the larger in the whole set? Actually within each pair they are the larger. But note: Could we also pair differently to reduce cost? We argued adjacent pairing is optimal; indeed cost = sum of even indices.
But there is a nuance: In the actual purchase process, you could buy items in any order, and you could use a single item as the "paid" item and attach a free item that is not necessarily its immediate neighbor in sorted order. However, the total cost is still sum of the paid items. The question is: For a given set S, what is the minimum possible sum of paid items? This is equivalent to selecting a subset P ⊆ S (the paid items) such that the remaining items F = S \ P can be injectively assigned to paid items with the condition that each free item's price ≤ the price of its assigned paid item. And we want to minimize sum_{i∈P} a_i. This is a classic matching problem. Since we want to minimize sum of paid, we would like P to be as small (in terms of sum) as possible while covering all items. For a given S, the minimal sum of paid items is achieved by taking the largest items as paid? Let's think: We want to cover all items. Each paid item can cover itself and at most one free item (k-1=1). So essentially, we need to select a set P such that |P| >= ceil(|S|/2) because each paid can cover at most two items. Actually, each paid covers itself plus possibly one free, so to cover t items, we need at least ceil(t/2) paid items. Moreover, we must be able to assign each free item to a paid item with price >= free. The condition is that if we sort S, we can assign the smallest items as free to the largest paid, etc. This is a bipartite matching. The minimal sum of paid items is achieved by selecting the paid items to be the largest items? Not necessarily: If we choose the largest items as paid, we might be able to cover many cheap items for free, reducing the number of paid items, but each paid still contributes its high price. Alternatively, we could choose cheaper items as paid, but then each paid can only cover free items that are ≤ them, so we might need more paid items (since cheap paid cannot cover expensive items, so expensive items must be paid themselves). So there is a trade-off.
Our earlier pairing (adjacent) leads to paid items being the second, fourth, ... which are not necessarily the largest overall. In a set [1,100,101], t=3: we said optimal paid items are {1,101} (odd indices) sum=102. But could we instead choose paid items = {100,101}? That would be 2 paid items, but they'd have to cover the free item (which would be 1). But is it allowed to assign free item 1 to paid 100? Yes, because 1 ≤ 100. And paid 101 covers itself. So total paid sum = 100+101=201, which is worse. Could we choose paid = {101} only? That would cover at most 2 items (101 itself plus one free). But we have 3 items, so need at least 2 paid items. So minimal paid set size = ceil(3/2)=2. So we need two paid. Which two? The cheapest paid sum would be to choose the smallest two? But if we choose {1,100}, can we cover the third item 101? Free items are assigned to paid items with condition price ≤ paid. The free item must be from the set not paid. If we pay for 1 and 100, the free items are the remaining: 101. But can we assign 101 as a free item to any paid? Condition: free price ≤ paid price. 101 ≤ 1? No. 101 ≤ 100? No. So not possible. So the paid set must be such that every item not in P has price ≤ some item in P. In other words, the maximum among unpaid items must be ≤ some paid item? More precisely, there must be an injection from F to P such that each free ≤ assigned paid. This is possible iff when sorting all items, the largest unpaid cannot exceed the smallest paid? Not exactly. For a feasible assignment, after sorting both sets, we need that the i-th smallest free ≤ i-th smallest paid? Actually, if we sort P ascending: p1 ≤ p2 ≤ ... ≤ p_m, and sort F ascending: f1 ≤ f2 ≤ ... ≤ f_{t-m}. A necessary and sufficient condition for existence of injection with f_j ≤ assigned paid is that for all j, f_j ≤ p_{?}. The classic condition is that if we pair the largest free with the largest paid, etc., it's possible if after sorting both, each free is ≤ its paired paid. So we can match by pairing the largest free with the largest paid, then next largest with next largest, etc. This works if when sorted ascending, for each i (from 1 to |F|), the i-th smallest free ≤ the i-th smallest paid? Actually consider matching largest free to largest paid: Suppose we have m paid items. We want to assign each free to a distinct paid. A greedy that works: sort free ascending, sort paid ascending, then try to assign each free to a paid that is at least as large. This is like bipartite matching that can be solved by sorting and checking if for each free (starting from largest) there is a paid >= it. Equivalent condition: after sorting all items, if we take the largest m items as paid, then all free are smaller, and we can assign. But if we don't take the largest as paid, we might still be able to assign if there are enough large paid to cover the free. The minimal sum of paid might be achieved by taking the smallest possible paid items while still covering the free. This is akin to selecting the paid items as the "ceil(t/2)" items in sorted order with some pattern. The known result for k=2 is exactly as we derived: the minimal paid sum is sum of elements at positions: if t even, 2,4,...,t; if t odd, 1,3,...,t. This is equivalent to selecting paid items as the larger in each consecutive pair when pairing adjacent after sorting. Let's verify that this selection is feasible: For t even, paid are the even indices; free are the odd indices. Since each paid (even) is ≥ its corresponding odd (since sorted), we can assign each free odd to the paid that is the next even? Actually we need a specific matching: we can match each odd with the even that is immediately after it (i.e., pair (1,2), (3,4), ...). That works because each free (odd) ≤ paid (even). So feasible. For t odd, paid are the odd indices: 1,3,5,...; free are the even indices: 2,4,... . Then we can match each even with the next odd? But need to ensure even ≤ some odd. Since sorted, even index i has value ≤ next odd index i+1? Actually for odd t, indices: 1(paid),2(free),3(paid),4(free),... t(paid). So we can match free 2 with paid 3? 2 ≤ 3? Since sorted, a2 ≤ a3, yes. Free 4 with paid 5, etc. The last free? If t=5, free are indices 2 and 4, paid are 1,3,5. We can match 2 with 3, 4 with 5. And paid 1 remains unmatched (but it's paid anyway). So feasible. So that selection yields feasible assignment.
Now, is it minimal sum? Let's try to see if we can get lower sum than that by a different selection. For t even, could we choose paid set as the odd indices (which are smaller) and free as even indices? But then we need to assign free (even indices) to paid (odd indices). However, an even index item may be larger than its preceding odd index (since sorted, even index ≥ odd index). For example, [1,100]: paid=1, free=100 cannot assign because 100 > 1. So not feasible. Could we choose paid as a mix: say for t=4, could paid be {b1, b4} and free be {b2, b3}? Then need to assign b2 and b3 to paid. b2 ≤ b4? Yes, b2 ≤ b4. b3 ≤ b4? Yes. But can we assign both to b4? No, each paid can take at most one free. So we need two paid items. So we need a second paid. Could we use b1 as paid and assign one free to it? b1 is very small, cannot take b2 or b3. So b1 cannot cover any free except maybe itself? Actually b1 is paid and could cover a free if free ≤ b1, but there is none. So the only useful paid is b4. So we need at least one more paid. Could we choose paid = {b2, b3}? Then free = {b1, b4}. b4 cannot be assigned to b2 or b3 because b4 > b2 and b3. So not feasible. So the only feasible paid sets for t=4 seem to be those where the largest free is ≤ some paid, and there are enough paid. The known optimal is indeed the even-indexed paid. Could there be a paid set with sum less than b2+b4? Suppose b1 is very small, b2 moderate, b3 moderate, b4 large. Could paid = {b2, b3}? Sum maybe less? For example, b=[1,50,51,100]. b2+b4=50+100=150. b2+b3=50+51=101. Is paid={50,51} feasible? Then free={1,100}. We need to assign free items to paid. 1 can be assigned to either 50 or 51. 100 cannot be assigned to 50 or 51 (since 100 > both). So not feasible. Paid={50,100}? Sum=150. Free={1,51}. 1 can go to 50 or 100; 51 can go to 100 (since 51 ≤ 100). But need distinct paid: assign 1 to 50, 51 to 100 -> works? Check: Paid set = {50,100}. Free set = {1,51}. Assign free 1 to paid 50 (1≤50) and free 51 to paid 100 (51≤100). That's feasible! Sum = 150. That's same as even-index sum (50+100=150). Could we have paid={51,100}? Sum=151. So 150 is minimal. Could we have paid={1,100}? Sum=101? That would be 1+100=101. Free={50,51}. Can we assign free 50 and 51 to paid? Each paid can take one free. Paid 1 can take free only if free ≤ 1: no. So fails. Paid={1,51}? Sum=52. Free={50,100}. 50 ≤ 51? Yes, assign 50 to 51; 100 cannot be assigned to 1. So fails. So minimal sum appears to be 150.
Thus, even-index sum seems minimal. So our formula stands.
Now, does the prefix of cheapest t always give minimal cost? We argued yes due to component-wise dominance. That argument relies on the fact that the cost is a sum of specific positions in the sorted order of the set, and those positions are fixed (even indices for even t, odd for odd t). But is that always the case? For any set S, the minimal cost is not necessarily the sum of even/odd indices in its sorted order? We claimed that the minimal cost equals that sum when we use the adjacent pairing. But we need to verify that this pairing indeed yields the minimal cost for that set. As argued, pairing adjacent is optimal. So for a given S, C_min(S) = sum of elements at positions: if |S| even: even indices; if odd: odd indices, after sorting S. That seems correct based on known result. Let's prove quickly: The problem of minimizing sum of maxima over pairs (with possibly one unpaired) is equivalent to: we need to choose which items are "paid". Each paid item can cover at most one other item (free) that is ≤ it. So we need to select a set P of paid items covering all items. The cost is sum(P). The condition for feasibility: after sorting all items, if we denote the items in sorted order, we can assign free items to paid items such that each free ≤ its paid. This is feasible iff when we take the largest |P| items as paid? Not exactly. But there is a known lemma: The minimum sum of paid items is achieved by taking every second item in sorted order (starting from the second if even, or first if odd). This can be proven by exchange: Consider any optimal paid set. Sort all items. If there is a paid item that is not one of those positions, we can swap with a non-paid to reduce or keep sum. I'm convinced.
Given that, our cost function for a set S is indeed C(S) = sum of those positions. So the component-wise argument holds.
Thus, the solution is sound.
Now, there is also an alternative approach often used in solutions for the general k problem (like using DP on modulo k). For k=2, this prefix sums method is simpler.
Now implement accordingly.
But note: The problem statement says "k=2" in this version, but we must still read k from input and ignore. But in the input, they give k as 2, but we should treat it generically? Not necessary.
Now, before coding, we should also consider if there is any nuance about the operation: "Vasya can buy one good with the index i if he currently has enough coins (i.e p ≥ a_i). After buying this good, the number of Vasya's coins will decrease by a_i". This is the standard single purchase. The other operation: "Vasya can buy a good with the index i, and also choose exactly k-1 goods, the price of which does not exceed a_i, if he currently has enough coins (i.e p ≥ a_i). Thus, he buys all these k goods, and his number of coins decreases by a_i (i.e it becomes p := p - a_i)." Note that when performing the offer, you must choose exactly k-1 other goods (so exactly 2 goods total). So you cannot choose to take only one good with the offer (i.e., you can't take a paid item without a free). So each use of the offer consumes exactly two items. So our model of pairs is accurate: each paid item is associated with exactly one free item when using the offer. But can we also buy a paid item without a free? That's the single purchase operation. So we can have some paid items that are solo. So our mixture of pairs and singles is allowed.
Now, is it allowed to use the offer multiple times, possibly overlapping? No, each good once.
Thus, all good.
Now, is there any scenario where we would want to use an offer but not take a free item? Not allowed; the offer requires exactly k-1 other goods, so you must take k items. So each paid item used in an offer must be paired with exactly one free. So our pairing model exactly matches that: each paid item that is part of a pair is used in an offer, and the other item is free. Singles are not used in an offer.
Thus, solution is final.
Now, we must implement for multiple test cases efficiently.
Implementation details:
Use fast I/O (ios_base::sync_with_stdio(false); cin.tie(NULL);)
Use vector or vector for a, but sum may exceed 2^31, so use long long for prefix sums. Since a_i <= 1e4, sum <= 2e9, which fits in 32-bit signed (2,147,483,647) but I'll use long long to be safe.
Now edge: p could be 0? p >=1 per constraints, so at least 1.
Now consider if we need to handle t=0? ans initialized 0 covers.
Now, also note that we are computing prefix sums for odd and even based on sorted array indices. We could also compute directly without storing both arrays by iterating and accumulating cost on the fly and record max t where accumulated <= p. But due to the pattern, it's easier to compute prefix sums.
Now, final answer.
But before finalizing, we must think: Is there any scenario where the cheapest t items might not be purchasable because the pairing condition might not be satisfiable? But we argued it is always satisfiable with the adjacent pairing. However, we need to ensure that the pairing we intend is actually allowed by the order of operations: you can perform operations in any order. For the adjacent pairing, we need to ensure that for each pair, at the time of purchase, the paid item's price is affordable and the free item is still available and its price ≤ paid. That can be done by first buying the paid items? Actually, when we use the offer, we buy both at once: we pay the price of the paid item and get both. So we need to have the free item still unpurchased. The order can be arranged. So it's feasible.
Thus, solution is correct.
Now, as a final check, let's test some random cases to be sure.
Case: n=1, p=5, a=[10]. Sorted [10]. t=1 odd: cost=10 >5 => ans=0. Can't buy. Indeed, single purchase requires 10 coins, not enough. So output 0.
Case: n=2, p=5, a=[3,4]. Sorted [3,4]. t=2 even: cost = b2=4 <=5, ans=2. Indeed, buy both with offer paying 4.
Case: n=2, p=3, a=[3,4]. t=2 cost=4 >3; t=1 cost=3 <=3 -> ans=1. So can buy only the cheaper individually. That's correct.
Now consider scenario: n=3, a=[1,2,100], p=101. Cheapest 3 cost = b1+b3=1+100=101 <=101, ans=3. But can we actually buy all 3 with budget 101? Our plan: pair (2,100) cost=100, and single 1 cost=1 total 101. But note: In pair (2,100), paid is 100, free is 2. That's allowed because 2 ≤ 100. Then we also buy 1 singly. Works. So okay.
Now scenario: a=[1,50,51], p=101. Cheapest 3 cost=1+51=52? Wait t=3 odd: b1+b3 = 1+51=52? Actually sorted: [1,50,51]; b1=1, b3=51 => sum=52. That's <=101, so ans=3. But can we buy all 3? Pair (50,51) cost=51, and single 1 cost=1 total=52. Works. So even though p=101, we could also do other combos, but we get 3.
Now scenario: a=[10,20,30,40], p=60. Cheapest 4 cost even sum = b2+b4=20+40=60 => ans=4. Can we buy all 4? Pair (10,20) cost=20, (30,40) cost=40 total 60. Works.
Now scenario: a=[5,5,5,100,100], p=105. Cheapest 5 sorted [5,5,5,100,100]. t=5 odd: b1+b3+b5=5+5+100=110 >105. t=4 even: b2+b4=5+100=105 <=105 => ans=4. So can buy 4 items. Which 4? The cheapest 4: [5,5,5,100] cost even sum = b2+b4 = 5+100=105. Pairing: maybe pair (5,5) cost=5, and (5,100) cost=100? But careful: Set of four items: [5,5,5,100]. Sorted: [5,5,5,100]. To achieve cost = sum of even indices: indices 2 and 4 => second element 5 and fourth 100 => 105. Can we achieve that? Pairing adjacent: (5,5) cost=5, (5,100) cost=100 total=105. That uses items: first two 5's as a pair (pay 5, get both), and then third 5 and 100 as a pair (pay 100, get both). That's valid because in pair (5,100), free is 5, paid 100. So we have all 4 items. Yes.
But could we buy 5 items with a different selection? Maybe choose [5,5,100,100] (4 items) already, but we have 5 items total. For 5 items, any set of 5 must include all items. The minimal cost we computed is 110 >105, so cannot. So answer 4 is correct.
Thus, solution works.
Now, we can implement.
But wait: The problem statement says "k=2" in this version. However, we might need to consider that the offer is "k of goods for the price of one", i.e., you pay only for the most expensive. So our modeling is correct.
Now, double-check that we aren't missing a possibility: Could we use an offer to buy a good and also choose a free good that is more expensive? The condition says "the price of which does not exceed a_i". So the free must be cheaper or equal. So no.
Thus, solution stands.
Now, we can implement as described.
Potential improvements: Instead of two prefix arrays, we can compute on the fly: iterate i from 0 to n-1, and maintain current cost for buying up to i+1 items? But the pattern depends on parity. However, computing prefix sums is fine.
Now, think about large p: up to 2e9, but sums fine.
Now, about reading input: t up to 1e4, sum n 2e5, so reading using cin should be okay with fast I/O.
Now, let's produce final solution.
But I want to double-check for odd t: The cost formula we used (odd indices sum) includes the smallest item (b1). Could there be a scenario where we could achieve lower cost for odd t by using a different pairing that doesn't force the smallest to be paid? For odd t, we have an extra item beyond even pairs. In our scheme, we make the smallest item the single (paid). Could we instead make a larger item single and pair the smallest with something else? For example, set of 3 items: [1,2,3]. Our cost = 1+3=4 (paid: 1 and 3). Alternative: pay for 2 and 3 (paid sum=5) and get 1 free? But free must be ≤ paid. If paid are 2 and 3, free could be 1 assigned to either 2 or 3, but then we have only two paid covering three items? Actually with two paid, we can cover at most 2 paid + up to 2 free? Wait each paid can have at most one free, so with two paid we can cover up to 4 items. For three items, we need at least 2 paid (ceil(3/2)=2). If we choose paid = {2,3}, free = {1}. Assign 1 to 2 (since 1≤2). Then total cost = 2+3=5. So higher. Could we choose paid = {1,2}? Then free = {3}. But 3 cannot be assigned to 1 or 2. So not feasible. Paid = {1,3} works, cost 4. So indeed minimal cost is 4. So formula holds.
For larger odd t, can we avoid paying the smallest? Suppose t=5, items sorted: [1,2,3,4,5]. Our cost = 1+3+5=9. Alternative: paid = {2,4,5}? That would be 2+4+5=11, free = {1,3}. Assign 1 to 2, 3 to 4? 3≤4 yes. So feasible, cost 11 >9. Paid = {1,4,5}? cost=1+4+5=10, free={2,3}. Assign 2 to 4, 3 to 5? 3≤5 yes, so cost 10 >9. Paid = {2,3,5}? cost=10, free={1,4}. 4 cannot be assigned to 2 or 3, fails. Paid = {3,4,5}? cost=12, free={1,2}, assign both to 3? cannot because each paid only one free, need two paid for two free; 1≤3, 2≤4, okay? Actually paid {3,4,5} can assign free 1 to 3, 2 to 4, cost 12. So minimal seems 9. So formula stands.
Thus, final answer.
Now, we should ensure we compute prefix sums correctly with 1-index.
Alternative: we can compute an array dp or something, but not needed.
Now, I will write the solution.
Implementation:
code
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
int n, p, k;
cin >> n >> p >> k;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
sort(a.begin(), a.end());
vector<long long> odd(n+1, 0), even(n+1, 0);
for (int i = 1; i <= n; i++) {
if (i % 2 == 1) {
odd[i] = odd[i-1] + a[i-1];
even[i] = even[i-1];
} else {
even[i] = even[i-1] + a[i-1];
odd[i] = odd[i-1];
}
}
int ans = 0;
for (int cnt = 1; cnt <= n; cnt++) {
long long cost = (cnt % 2 == 0) ? even[cnt] : odd[cnt];
if (cost <= p) ans = cnt;
}
cout << ans << '\n';
}
return 0;
}
But we can also combine odd and even into a single prefix array for "cost for prefix length i" computed on the fly. But this is fine.
Now, test with examples.
All good.
Now, but I want to double-check the logic for even t: sum of even indices. In our prefix calculation, for i=2 (even), even[2] = a[1] (0-index) which is the second element. That's correct. For i=4, even[4] = a[1]+a[3]. That's sum of indices 2 and 4 in 1-index, which correspond to positions 2 and 4 in sorted prefix. So correct.
Now, one more nuance: The problem allows us to buy goods in any order, but we must ensure that when we use the offer, we have enough coins at that moment. Our total cost is the sum of the paid items, but could there be an ordering issue if we need to buy a paid item after spending some coins? Since we sum the paid items, if total sum <= p, we can always arrange purchases such that we never exceed p? For example, suppose we need to pay for a high-cost paid item first, but we may not have enough coins if we also plan to buy a cheap single later? However, we can reorder purchases. Since we can always buy the cheaper items first (singles) to reduce coins? Actually, we want to ensure that at the time of each purchase, we have enough coins. If total sum <= p, we can always perform purchases in an order that never exceeds p, because we can spend coins gradually. But is there any scenario where we need to have more coins at some point than the total sum? For instance, if we use an offer, we pay the price of the paid item and get both. That's a single transaction costing that price. The free item doesn't cost extra. So each transaction costs exactly the price of the paid item. So the total coins spent is sum of paid items. The sequence of transactions can be done in any order. Since each transaction costs some amount, as long as the sum of all transactions <= p, we can schedule them in increasing order of cost to avoid having a momentary deficit? Actually, we must ensure that before each transaction, we have enough coins. If we start with p, we can perform transactions in any order, but if we attempt a transaction with cost c, we need current coins >= c. If we perform higher-cost transactions first, we might run out before we can do smaller ones? But since we have total sum <= p, after performing a high-cost transaction, our remaining coins are p - c. We still need to perform other transactions whose total cost is sum(others) <= p - c, so we will have enough if we do them after? The remaining coins after first transaction is p - c, and we need to cover the sum of the rest. Since sum(others) <= p - c, we have enough. So as long as we don't exceed remaining, it's fine. But if we do a high-cost transaction first, the remaining is p-c, and the sum of remaining costs is <= p-c, so we can do them. However, we must also ensure that at the moment of each subsequent transaction, the cost of that transaction is <= current coins. Since the sum of remaining costs is <= current coins, but it's possible that a single remaining transaction cost exceeds current coins? Example: total sum = 100, p=100. Suppose we have one transaction costing 60 and another costing 40. If we do the 60 first, remaining 40, then the 40 <= 40, ok. If we have transactions 90 and 10: do 90 first, remaining 10, then 10 ok. So seems always possible as long as each individual transaction cost <= current coins. But could it be that after some transactions, the next transaction cost is greater than the current coins, even though total remaining sum is less? For instance, after some spends, current coins = 30, next transaction cost = 50, but there are other transactions later that sum to less? However, if the next transaction cost is 50, we need at least 50. But if current coins is 30, we cannot perform it. But we could reorder: do smaller ones first. Since we can choose order arbitrarily, we can always order transactions in increasing order of cost. Starting from smallest paid to largest. Then at each step, the current coins will be p - sum of previous smaller costs, which is >= the next cost? Actually, if we sort paid items ascending, then before paying the i-th paid, we have spent sum of previous i-1 smaller paid, so remaining = p - sum_{j< i} paid_j. Since total sum of all paid <= p, we have sum_{j< i} paid_j <= p - paid_i? Not necessarily, but we need that paid_i <= remaining before paying i-th. That is, paid_i <= p - sum_{j< i} paid_j, i.e., sum_{j<= i} paid_j <= p. That holds because total sum <= p. So if we process in increasing order, before each payment, the remaining is at least paid_i because sum_{j<= i} paid_j <= p implies p - sum_{j< i} paid_j >= paid_i. Indeed, p - sum_{j< i} paid_j >= sum_{j<= i} paid_j - sum_{j< i} paid_j = paid_i. So it's safe. Thus, any feasible set of paid items (with total <= p) can be purchased by ordering them from cheapest to most expensive. So no ordering issue.
Thus, solution stands.
Now, time to write final answer.
But the problem originally might be from Codeforces Round #1462? Actually it's "New Year's Problem" maybe. The easy version has k fixed as 2. So our solution matches.
Now, I'll produce final output.
But I'd like to consider if there is a more efficient O(n) method without sorting? No, because we need sorted order to compute minimal cost. So sorting is necessary.
Now, confirm constraints: sum n <= 2e5, so sorting each test case individually is fine.
Thus, final code.
One more nuance: The problem input includes k, but we don't need to use it. However, we should still read it. But since k=2 guaranteed, we could ignore.
Potential Pitfall: When t is large (10^4), but sum n is 2e5, using vector per test and sorting is okay.
Now, final answer.
But wait, could there be a scenario where the minimal cost for t items is not achieved by the prefix, because the prefix might have a different parity distribution? Let's test with an example where the cheapest t items have a very high value at an even position, but if we skip that high and take a slightly higher at odd position, maybe the sum of paid positions changes? But the component-wise argument says that for each position i in sorted order of the set, the i-th smallest in any other set is at least as large as the i-th smallest in the prefix. Since the cost sums over specific indices (either all even or all odd), and those indices are fixed ranks, the sum for any other set cannot be smaller. This argument relies on the fact that the indices summed are monotone increasing with rank. Since for each rank i in the sum, the value in another set is >= the value in prefix. So sum >= prefix sum. Therefore prefix yields minimal cost. This is solid.
Thus, solution is rigorous.
Now, think about scenario where t is odd: sum over odd indices (1,3,5,...). Suppose we have prefix: [1,100,101]. Then cost=1+101=102. Could there be another set of 3 items with lower cost? For any other set of 3, say [1,100,102]? Actually 102 not present. Let's consider available items: [1,2,100,101,102]. We want t=3. Prefix of 3 cheapest is [1,2,100] cost = 1+100=101. Another set: [1,100,101] cost=1+101=102 >101. [2,100,101] cost=2+101=103. So prefix is best.
Now, consider a case where the cheapest items are many but the even positions of the prefix might be high because of distribution. For instance, items: [1, 100, 101, 102, 103, 104]. For t=4, prefix = [1,100,101,102] cost = b2+b4=100+102=202. Another set of 4 items: [100,101,102,103] cost = 101+103=204 >202. So prefix lower.
What about selecting [1,101,102,103]? cost = (if sorted: [1,101,102,103]) even indices: 101+103=204 >202. So prefix wins.
Thus, seems correct.
Hence, finalize.
Now, we should output answer for each test case.
Now, final solution code.
But perhaps we can do binary search for maximum t to be more efficient for each test? Not needed.
Implementation details: Use long long for p as well because p can be up to 2e9, fits in 32-bit signed, but to avoid overflow when comparing with cost (which may be up to 2e9), we can use long long but it's fine to use int for all? But cost could be up to sum of up to n items, which is 2e9, fits in 32-bit signed int. However, if we use int, multiplication? No issue. But it's safer to use long long to avoid overflow if we ever sum more than 2^31-1. But the maximum sum for the whole array is 2e9, which is less than 2^31-1 (2,147,483,647). So int is safe. However, if we later extend to larger n? But constraints say n <= 2e5 and a_i <= 1e4, so maximum sum = 2e9 exactly, which is less than 2^31-1 = 2,147,483,647. So int is safe. But p up to 2e9, also safe. So we could use int. But using long long is more consistent. I'll use long long for cost and p as long long.
Now, final code:
code
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
int n, k;
ll p;
cin >> n >> p >> k;
vector<int> a(n);
for (int &x : a) cin >> x;
sort(a.begin(), a.end());
vector<ll> odd(n+1, 0), even(n+1, 0);
for (int i = 1; i <= n; i++) {
if (i % 2 == 1) {
odd[i] = odd[i-1] + a[i-1];
even[i] = even[i-1];
} else {
even[i] = even[i-1] + a[i-1];
odd[i] = odd[i-1];
}
}
int ans = 0;
for (int cnt = 1; cnt <= n; cnt++) {
ll cost = (cnt % 2 == 0) ? even[cnt] : odd[cnt];
if (cost <= p) ans = cnt;
}
cout << ans << '\n';
}
return 0;
}
We should also consider that for some test cases, we might want to skip computing both prefix arrays by using a single array that stores the cost for each prefix length directly. For clarity, we can compute on the fly:
code
vector<ll> cost(n+1,0);
for(int i=1;i<=n;i++){
if(i%2==1) cost[i] = cost[i-1] + a[i-1]; // odd: add current
else cost[i] = cost[i-2] + a[i-1]; // even: replace? Actually, for even i, cost[i] = cost[i-2] + a[i-1]? Let's derive: For even i, cost(i) = sum of even indices up to i. That equals cost(i-2) + a_{i-1}? Because the even indices up to i are the same as even indices up to i-2 plus the new even index i (since i is even). So cost[i] = cost[i-2] + a[i-1] (since 1-indexed). And for odd i, cost(i) = sum of odd indices up to i = cost(i-2) + a[i-1]? Let's test: For odd i, odd indices up to i include odd i, and the previous odd indices up to i-2. So cost[i] = cost[i-2] + a[i-1]. Actually, both even and odd follow the recurrence cost[i] = cost[i-2] + a[i-1]? But the base differs. For i=1 (odd): cost[1] should be a[0]. If we set cost[-1]? Not good. So we need separate. But we can compute directly with two arrays. It's fine.
Alternatively, we can compute an array dp where dp[i] = minimal cost to buy i items from the first i items? Actually, it's exactly the prefix cost we defined.
So whatever.
Now, I'll provide final answer.
But wait, is there any catch regarding the fact that k=2, but the problem statement says "k=2 for all test cases" but we still read it. Possibly we can ignore.
Now, final answer.
But I recall that some solutions for this problem (CF 1462C? Actually it's problem "Buying a TV Set"?) Not.
Anyway.
Now, I'll produce the final explanation and code.
But the user asked "This is the easy version... Help Vasya to find out the maximum number of goods he can buy." So we need to output m.
Thus, final answer.
Now, I'll prepare the final message.</think> ```cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
int n, k;
ll p;
cin >> n >> p >> k; // k is always 2, but we read it anyway
vector<int> a(n);
for (int &x : a) cin >> x;
sort(a.begin(), a.end());
// prefix sums for odd and even positions (1‑based)
vector<ll> odd(n + 1, 0), even(n + 1, 0);
for (int i = 1; i <= n; ++i) {
if (i % 2 == 1) { // odd index
odd[i] = odd[i - 1] + a[i - 1];
even[i] = even[i - 1];
} else { // even index
even[i] = even[i - 1] + a[i - 1];
odd[i] = odd[i - 1];
}
}
int ans = 0;
for (int cnt = 1; cnt <= n; ++cnt) {
ll cost = (cnt % 2 == 0) ? even[cnt] : odd[cnt];
if (cost <= p) ans = cnt;
}
cout << ans << '\n';
}
return 0;
}