#K8276. Maximize Liquid Distribution

    ID: 36047 Type: Default 1000ms 256MiB

Maximize Liquid Distribution

Maximize Liquid Distribution

You are given a tank with a total volume of liquid V and N containers, each with a maximum capacity. Your task is to distribute the liquid into the containers in a way that maximizes the total volume distributed, without exceeding any container's capacity.

In each test case, you will be given:

  • An integer V representing the total volume of liquid in the tank.
  • An integer N representing the number of containers.
  • A list of N integers representing the capacities of the containers.

You should distribute the liquid greedily starting from the container with the highest capacity. Formally, if we denote by \(c_1, c_2, \ldots, c_N\) the container capacities sorted in non-increasing order, then for each container i you distribute:

[ \text{distributed}i = \min{V{remaining}, c_i} ]

and update \(V_{remaining} = V_{remaining} - \text{distributed}_i\). The answer for the test case is the sum of the amounts distributed, i.e. the maximum liquid that can be allocated without overflowing any container.

inputFormat

The input is read from standard input (stdin) and is formatted as follows:

  • The first line contains an integer T, the number of test cases.
  • For each test case:
    • The first line contains two integers: V (the total volume of liquid) and N (the number of containers).
    • If N > 0, the next line contains N space-separated integers representing the capacities of the containers.

outputFormat

For each test case, output a single integer in a new line, representing the maximum volume of liquid that can be distributed among the containers without exceeding their capacities. The results are printed to standard output (stdout).

## sample
2
100 5
20 30 10 40 50
150 4
25 50 75 100
100

150

</p>