eolymp
bolt
Try our new interface for solving problems
Problems

Dividing the Loot

Dividing the Loot

\includegraphics{https://static.e-olymp.com/content/29/29531f08442ccd6a810cfee1a7ca3276ed642986.jpg} You have successfully led a group of pirates in taking over a commercial vessel. You have seized gold coins, silver coins, and other precious goods. Now it comes down to dividing the loot. It is important to keep everyone satisfied, otherwise you risk mutiny. A pirate is not satisfied if another pirate gets more items than he does. Therefore you might have to content yourself with fewer items than the other pirates or throw some items into open sea. Fortunately, the other pirates have no notion of the value of the items, while you do. Can you make the most out of this without mutiny? \InputFile The first line contains the number of test cases to follow. Each test case has the following format: \begin{itemize} \item One line with two integers \textbf{p }and \textbf{n} (\textbf{0 }≤ \textbf{p }≤ \textbf{1000}, \textbf{1 }≤ \textbf{n }≤ \textbf{1000}) - the number of other pirates you have to share the loot with and the number of items, respectively. \item One line with \textbf{n }integers, \textbf{v_i}, satisfying \textbf{1 }≤ \textbf{v_i} ≤ \textbf{1000}: the value of each item. \end{itemize} \OutputFile For every test case in the input, the output should contain one integer on a single line: the maximum total value of all the items that you can give yourself, while keeping all other pirates satisfied.
Time limit 1 second
Memory limit 64 MiB
Input example #1
2
2 7
1 1 1 1 3 3 7
5 9
2 2 4 4 6 8 11 11 13
Output example #1
10
13
Source 2011 Benelux Algorithm Programming Contest, Preliminaries, Problem F