#C1766. Minimum Ticket Purchase Cost
Minimum Ticket Purchase Cost
Minimum Ticket Purchase Cost
You are given T test cases. For each test case, you are provided with two integers N and M followed by M ticket prices. Your task is to determine the minimum total cost to purchase exactly N tickets. The strategy is to choose the N cheapest tickets. However, if it is impossible to purchase N tickets (i.e. if N > M), output \(-1\).
Input Format:
- The first line contains an integer T, denoting the number of test cases.
- For each test case, the first line contains two integers N and M.
- The second line contains M space-separated integers representing the ticket prices.
Output Format:
- For each test case, output a single line with the minimum total cost or \(-1\) if it is not possible to buy exactly N tickets.
Constraints and Details:
- If \(N > M\), output \(-1\).
- If \(N \le M\), sort the ticket prices in ascending order and sum up the smallest N prices.
The mathematical formulation of the cost when \(N \le M\) is given by:
[ \text{Cost} = \sum_{i=1}^{N} p_i]
where \(p_1, p_2, \ldots, p_M\) are the sorted ticket prices (in non-decreasing order).
inputFormat
The input is read from standard input (stdin). The first line of input contains an integer T denoting the number of test cases. For each test case, the first line contains two space-separated integers N and M. The second line contains M space-separated integers representing the ticket prices.
outputFormat
For each test case, output a single integer in a new line—the minimum total cost to purchase exactly N tickets. If it is not possible, output \(-1\).
## sample2
3 5
5 1 4 7 10
4 6
12 15 10 6 8 13
10
36
</p>