Food Stalls
InterviewTime limit30sMemory limit1024 MB
Pick one warehouse spot and K stall spots from N candidates to minimize total cost, where each stall also pays its distance to the warehouse.
Problem
Everybody loves street food, especially the local residents of Bitetown. For this reason, you have decided to build exactly food stalls and one warehouse on the main street of Bitetown.
The main street is a straight line that is metres long. There are spots where you are allowed to build a stall or the warehouse. You may not build anywhere else on the street. The -th spot is metres from the left end of the street.
Each spot can hold at most one stall or the warehouse, but not both. Building a stall or the warehouse at the -th spot costs dollars. In addition, if the warehouse is at the -th spot, then building a stall at the -th spot costs an extra dollars.
Find the minimum cost to build exactly food stalls and one warehouse.
Input
The first line contains the number of test cases . Each test case starts with a line containing two integers and , the number of stalls and the number of spots.
The second line contains integers , where is the distance of the -th spot from the left end of the street, in metres.
The third line contains integers , where is the cost of building a stall or the warehouse at the -th spot.
Output
For each test case, output one line in the form Case #x: y, where is the test case number starting from 1, and is the minimum cost to build stalls.
Limits
- 1 ≤ T ≤ 100
- 1 ≤ K < N
- 1 ≤ C_i ≤ 10^9 for all i
- 1 ≤ X_i ≤ 10^9 for all i
- X_i ≠ X_j for all i ≠ j
Hint
In Sample Case 1, you must build stalls and one warehouse, and there are spots. One optimal plan is to build the warehouse on the 3rd spot for 80 dollars, and build stalls on the 2nd and 4th spots.
- The stall on the 2nd spot costs dollars.
- The stall on the 4th spot costs dollars.
The total is 178 dollars, which is the minimum, so the answer is 178.
In Sample Case 2, you must build stall and one warehouse, and there are spots. One optimal plan is to build the warehouse on the 2nd spot for 35 dollars, and build the stall on the 3rd spot, which costs dollars. The total is 62 dollars, which is the minimum.
In Sample Case 3, you must build stalls and one warehouse, and there are spots. One optimal plan is to build the warehouse on the 4th spot and build the 6 stalls on the other 6 spots. The total is 82 dollars, and proving that no cheaper plan exists is left to the reader. Note that the spots are not listed in ascending order of distance in this case.