CF1799D1.Hot Start Up (easy version)

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

This is an easy version of the problem. The constraints of tt , nn , kk are the only difference between versions.

You have a device with two CPUs. You also have kk programs, numbered 11 through kk , that you can run on the CPUs.

The ii -th program ( 1ik1 \le i \le k ) takes coldicold_i seconds to run on some CPU. However, if the last program we ran on this CPU was also program ii , it only takes hotihot_i seconds ( hoticoldihot_i \le cold_i ). Note that this only applies if we run program ii multiple times consecutively — if we run program ii , then some different program, then program ii again, it will take coldicold_i seconds the second time.

You are given a sequence a1,a2,,ana_1, a_2, \ldots, a_n of length nn , consisting of integers from 11 to kk . You need to use your device to run programs a1,a2,,ana_1, a_2, \ldots, a_n in sequence. For all 2in2 \le i \le n , you cannot start running program aia_i until program ai1a_{i - 1} has completed.

Find the minimum amount of time needed to run all programs a1,a2,,ana_1, a_2, \ldots, a_n in sequence.

输入格式

Input consists of multiple test cases. The first line contains a single integer tt , the number of test cases ( 1t50001 \le t \le 5000 ).

The first line of each test case contains nn and kk ( 1n,k50001 \le n, k \le 5000 ).

The second line of each test case contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n ( 1aik1 \le a_i \le k ).

The third line of each test case contains kk integers cold1,cold2,,coldkcold_1, cold_2, \ldots, cold_k ( 1coldi1091 \le cold_i \le 10^9 ).

The fourth line of each test case contains kk integers hot1,hot2,,hotkhot_1, hot_2, \ldots, hot_k ( 1hoticoldi1 \le hot_i \le cold_i ).

It is guaranteed the sum of nn and the sum of kk over all test cases do not exceed 50005000 .

输出格式

For each test case, print the minimum time needed to run all programs in the given order.

输入输出样例

  • 输入#1

    9
    3 2
    1 2 2
    3 2
    2 1
    4 2
    1 2 1 2
    5 3
    2 1
    4 3
    1 2 3 1
    100 100 100
    1 1 1
    5 2
    2 1 2 1 1
    65 45
    54 7
    5 3
    1 3 2 1 2
    2 2 2
    1 1 1
    5 1
    1 1 1 1 1
    1000000000
    999999999
    5 6
    1 6 1 4 1
    3 6 4 1 4 5
    1 1 1 1 4 1
    1 3
    3
    4 5 6
    1 2 3
    8 3
    3 3 3 1 2 3 2 1
    10 10 8
    10 10 5

    输出#1

    6
    11
    301
    225
    8
    4999999996
    11
    6
    63

说明/提示

In the first test case, we can do the following:

  • Run program a1=1a_1 = 1 on CPU 11 . It takes cold1=3cold_1 = 3 seconds to run.
  • Run program a2=2a_2 = 2 on CPU 22 . It takes cold2=2cold_2 = 2 seconds to run.
  • Run program a3=2a_3 = 2 on CPU 22 . The last program run on this CPU was also program 22 , so it takes hot2=1hot_2 = 1 second to run.

In total, we need 3+2+1=63 + 2 + 1 = 6 seconds to run them all. We can show this is optimal.

In the second test case, we can use do the following:

  • Run program a1=1a_1 = 1 on CPU 11 . It takes cold1=5cold_1 = 5 seconds to run.
  • Run program a2=2a_2 = 2 on CPU 22 . It takes cold2=3cold_2 = 3 seconds to run.
  • Run program a3=1a_3 = 1 on CPU 11 . The last program run on this CPU was also program 11 , so it takes hot1=2hot_1 = 2 seconds to run.
  • Run program a4=2a_4 = 2 on CPU 22 . The last program run on this CPU was also program 22 , so it takes hot2=1hot_2 = 1 second to run.

In total, we need 5+3+2+1=115 + 3 + 2 + 1 = 11 seconds. We can show this is optimal.

首页