#P16734. [GKS 2019 #D] Food Stalls

[GKS 2019 #D] Food Stalls

Problem Description

Everybody loves street food, especially the local residents of Bitetown! For this reason, you have decided to build exactly KK food stalls and one warehouse on the main street of Bitetown.

The main street is a long horizontal line that is 10910^9 metres long. There are NN spots that you are allowed to build stalls or a warehouse on. You may not build anywhere else on the street. The ii-th spot is XiX_i meters from the left end of the street.

You can build at most one stall or warehouse at the ii-th spot (but not both), which costs CiC_i dollars. Additionally, if the warehouse is in the jj-th spot, then building a stall in the ii-th spot costs an extra XjXi|X_j - X_i| dollars.

Please find the minimum cost to build exactly KK food stalls and one warehouse.

Input Format

The first line of the input gives the number of test cases, TT. TT test cases follow. Each test case starts with a line containing two integers KK and NN, the number of stalls you must build and the number of spots on the street, respectively.

The second line contains NN integers XiX_i; the ii-th of these is the distance of the ii-th spot from the left end of the street, in meters.

The third line contains NN integers CiC_i; the ii-th of these is the cost of building a stall or warehouse at the ii-th spot.

Output Format

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the minimum cost to build KK stalls.

3
2 4
1 2 3 10
100 70 80 20
1 5
150 300 301 400 700
8 35 26 5 2
6 7
22 21 20 23 26 25 24
10 10 10 10 10 10 10
Case #1: 178
Case #2: 62
Case #3: 82

Hint

In Sample Case 1, you must build K=2K = 2 stalls and one warehouse, and there are N=4N = 4 spots to build on. One possible solution is to build the warehouse on the 3rd spot with cost 80 dollars, and build the two stalls on the 2nd and 4th spot.

  • The stall on the 2nd spot costs 70+32=7170 + |3 - 2| = 71 dollars.
  • The stall on the 4nd spot costs 20+310=2720 + |3 - 10| = 27 dollars.

This costs 178 dollars in total, which is the minimum possible, so the answer is 178.

In Sample Case 2, you must build K=1K = 1 stalls and one warehouse, and there are N=5N = 5 spots to build on. One possible solution is to build the warehouse on the 2nd spot with cost 35 dollars, and build the stall on the 3rd spot, which costs 26+301300=2726 + |301 - 300| = 27 dollars. This costs 62 dollars in total, which is the minimum possible.

In Sample Case 3, you must build K=6K = 6 stalls and one warehouse, and there are N=7N = 7 spots to build on. One possible solution is to build the warehouse on the 4th spot and build the 6 stalls on the 6 remaining spots. It is left as an exercise to the contestant to verify that this costs 82 dollars in total and is the minimum possible. Note that in this example, the spots are not in ascending order of their distance from the left end of the street.

Limits

1T1001 \le T \le 100.

1K<N1 \le K < N.

1Ci1091 \le C_i \le 10^9, for all ii.

1Xi1091 \le X_i \le 10^9, for all ii.

XiXjX_i \neq X_j for all iji \neq j.

Test set 1 (Visible)

2N1002 \le N \le 100.

Test set 2 (Hidden)

There will be at most 55 cases with 500<N105500 < N \le 10^5.

The remaining cases will have 2N5002 \le N \le 500.