The lines at the air pump at your gas station are getting too long! You want to optimize the process to help customers more quickly inflate their tires, sports balls, giant parade balloon animals, and other products.
The pump is automatic: you set the pressure to a specific number of pascals and plug the pump into the inflatable product, and it will inflate as needed to that exact pressure. There are only two buttons on the pump: up and down. They increase and decrease the target pressure, respectively, by exactly pascal.
There is a line of customers, each of whom brings exactly products that they need to get inflated by the pump. You know the target pressure of each product. You can inflate the products from a customer in any order you want, but you cannot change the order of the customers. Specifically, you must inflate all products from the customer before inflating any from the -th customer. In between handling two products, if those two products have different target pressures, you need to use the buttons on the pump.
The pump is initially set to pascals, and it can be left at any number after all products of all customers have been inflated. If you order the products of each customer optimally, what is the minimum number of button presses you need?
Input Specification
The first line of the input gives the number of test cases, . test cases follow. Each test case starts with a line containing two integers, and : the number of customers and the number of products each customer brings, respectively. Then, lines follow. The of these lines contains integers , representing that the product that the customer brings has a target pressure of pascals.
Output Specification
For each test case, output one line containing Case #x: y
, where is the test case number (starting from 1) and is the minimum number of button presses needed to inflate all products according to their specified pressures.
Limits
Time limit: 5 seconds.
Memory limit: 1 GB.
.
, for all .
Test Set 1
.
.
Test Set 2
.
.
Sample Input
2
3 3
30 10 40
20 50 60
60 60 50
5 2
1 1000000000
500000000 1000000000
1 1000000000
500000000 1
1 1000000000
Sample Output
Case #1: 110
Case #2: 4999999996
In Sample Case #1, an optimal way to use the pump is:
- press up times, setting the pump to ; pump the product (from customer 1) that needs pascals,
- press up times, setting the pump to ; pump the product (from customer 1) that needs pascals,
- press down times, setting the pump to ; pump the product (from customer 1) that needs pascals,
- press down times, setting the pump to ; pump the product (from customer 2) that needs pascals,
- press up times, setting the pump to ; pump the product (from customer 2) that needs pascals,
- press up times, setting the pump to ; pump the product (from customer 2) and the two products (from customer 3) that need pascals, and finally
- press down times, setting the pump to ; pump the product (from customer 3) that needs pascals.
This is a total of button presses.
In Sample Case #2, notice that the answer can be larger than .
Comments