#P16731. [GKS 2019 #C] Catch Some

    ID: 19063 远端评测题 3000ms 1024MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>动态规划 DP2019背包 DPGoogle Kick Start

[GKS 2019 #C] Catch Some

Problem Description

Bundle is an animal researcher and needs to go observe KK dogs. She lives on a horizontal street marked at metre increments with consecutive numbers 00, 11, 22, 33 and so on. She begins in her home, which is at position 00. There are also NN dogs on the street. The ii-th dog is PiP_i metres to the right of her home on the street (multiple dogs can share the same position).

Dogs come in different colors, which are denoted by positive integers. The ii-th animal is of color AiA_i.

If Bundle is at her home, she can change the current color of her shirt. This is important since the dogs are very shy! Bundle can only observe a dog if she is at the same position as that dog, and is wearing a shirt of the same color as the dog.

It takes Bundle one second to move one metre to the left or right on the street. It takes her no time to change shirts or observe a dog.

What is the least amount of time it will take Bundle to observe KK dogs? Note that she does not have to return home after observing KK dogs.

Input Format

The first line of the input gives the number of test cases, TT. TT test cases follow. Each testcase begins with a line containing the two integers NN and KK, the number of dogs on the number line and the number of dogs Bundle needs to observe, respectively. The second line contains NN integers, the ii-th of which is PiP_i, the position of the ii-th dog. The third line contains NN integers, the ii-th of which is AiA_i, the color of the ii-th dog.

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 least time Bundle needs to observe KK dogs.

3
4 3
1 2 4 9
3 3 2 3
4 3
1 2 3 4
1 8 1 8
6 6
4 3 3 1 3 10000
1 2 8 9 5 7
Case #1: 8
Case #2: 6
Case #3: 10028

Hint

In Sample Case #1, there are N=4N = 4 dogs and Bundle needs to observe K=3K = 3 dogs. One way that she can achieve this is as follows:

  • Put on a shirt of color 33.
  • Move one metre to the right and observe the dog there.
  • Move one metre to the right again and observe the dog there.
  • Move two metres to the left, returning to her home.
  • Change into a shirt of color 22.
  • Move four metres to the right and observe the dog there.

In total, this takes Bundle 88 seconds which is the least time possible, so the answer is 88.

In Sample Case #2, there are N=4N = 4 dogs and Bundle needs to observe K=3K = 3 dogs. One way that she can achieve this is as follows:

  • Put on a shirt of color 11.
  • Move one metre to the right and observe the dog there.
  • Move one metre to the left, returning to her home.
  • Change into a shirt of color 88.
  • Move two metres to the right and observe the dog there.
  • Move two metres to the right again and observe the dog there. Note that Bundle is unable to observe the dog she passes at position 33, since her shirt is the wrong color (even though she was wearing the right colored shirt previously).

In total, this takes Bundle 66 seconds which is the least time possible, so the answer is 66.

In Sample Case #3, note that:

  • Multiple dogs can share the same position and
  • Dogs are not necessarily given in ascending order of position.

No explanation is provided for the answer to this case.

Limits

1T1001 \le T \le 100.

1KN1 \le K \le N.

1Ai10001 \le A_i \le 1000.

1Pi1051 \le P_i \le 10^5.

Test set 1 (Visible)

1N501 \le N \le 50.

Test set 2 (Hidden)

1N10001 \le N \le 1000.