This page is still under construction.

Parts of this page are still being built. What you see may change.

Food Stalls

Interview

Time limit30sMemory limit1024 MB

Summary
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.
Level

Medium6 of 10

Topics
Heap, Sorting, Greedy
Solved
No attempts yet

Problem

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 straight line that is 10910^9 metres long. There are NN spots where you are allowed to build a stall or the warehouse. You may not build anywhere else on the street. The ii-th spot is XiX_i 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 ii-th spot costs CiC_i dollars. In addition, if the warehouse is at the jj-th spot, then building a stall at the ii-th spot costs an extra ∣Xj−Xi∣|X_j - X_i| dollars.

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

Input

The first line contains the number of test cases TT. Each test case starts with a line containing two integers KK and NN, the number of stalls and the number of spots.

The second line contains NN integers X1,X2,...,XNX_1, X_2, ..., X_N, where XiX_i is the distance of the ii-th spot from the left end of the street, in metres.

The third line contains NN integers C1,C2,...,CNC_1, C_2, ..., C_N, where CiC_i is the cost of building a stall or the warehouse at the ii-th spot.

Output

For each test case, output one line in the form Case #x: y, where xx is the test case number starting from 1, and yy is the minimum cost to build KK 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 K=2K = 2 stalls and one warehouse, and there are N=4N = 4 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 70+∣3−2∣=7170 + |3 - 2| = 71 dollars.
  • The stall on the 4th spot costs 20+∣3−10∣=2720 + |3 - 10| = 27 dollars.

The total is 178 dollars, which is the minimum, so the answer is 178.

In Sample Case 2, you must build K=1K = 1 stall and one warehouse, and there are N=5N = 5 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 26+∣301−300∣=2726 + |301 - 300| = 27 dollars. The total is 62 dollars, which is the minimum.

In Sample Case 3, you must build K=6K = 6 stalls and one warehouse, and there are N=7N = 7 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.

Examples1

  1. Example 1

    Input
    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
    
    Expected output
    Case #1: 178
    Case #2: 62
    Case #3: 82