测评会员优惠活动进行中 · 开通 VIP,有效期内测评不限次 VIP 优惠中 · 测评不限次 立即查看

A3160. Hot Start Up - Hard Version

编程题 提高+/省选-
知识点

题目描述

有两个 CPU 和 $k$ 种程序,你可以在 CPU 上运行程序。

如果你选择的 CPU 上一次运行的程序不是你当前运行的程序,则需要花费 $cold_i$ 的时间。

否则,需要花费 $hot_i$ 的时间。

现在有 $n$ 个程序需要运行,第 $i$ 次需要运行的程序类型为 $a_i$,每次可以任意选择一个 CPU 运行程序,一个 CPU 同一时间最多运行一个程序。

求最短运行完全部程序的时间。

共有 $t$ 组数据。

$1 \le n,k \le 3 \times 10^5$,$1 \le hot_i \le cold_i \le 10^9$,$1 \le a_i \le k$,$1 \le t \le 10^5$,$\sum k \le 3 \times 10^5$,$\sum n \le 3 \times 10^5$

Data Credits: [Macw07](https://www.acgo.cn/person/929871)。

输入格式

输入包括多个测试用例。第一行包含一个整数 $t$ ,表示测试用例的数量($1 \le t \le 10^5$)。

每个测试用例的第一行包含两个整数 $n$ 和 $k$ ($1 \le n, k \le 3 \cdot 10^5$)。

每个测试用例的第二行包含 $n$ 个整数 $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le k$)。

每个测试用例的第三行包含 $k$ 个整数 $cold_1, cold_2, \ldots, cold_k$ ($1 \le cold_i \le 10^9$)。

每个测试用例的第四行包含 $k$ 个整数 $hot_1, hot_2, \ldots, hot_k$ ($1 \le hot_i \le cold_i$)。

保证所有测试用例中 $n$ 的总和以及 $k$ 的总和不超过 $3 \cdot 10^5$。

输出格式

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

对于每一个 $\mathtt{Testcase}$,输出一行一个整数,表示运行完所有程序所需要的时间。

输入输出样例

输入 #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 $a_1 = 1$ on CPU $1$ . It takes $cold_1 = 3$ seconds to run. 在第一个CPU中运行第一个程序,需要花费三秒钟冷却。
- Run program $a_2 = 2$ on CPU $2$ . It takes $cold_2 = 2$ seconds to run. 在第二个CPU中运行第二个程序,需要花费两秒钟冷却。
- Run program $a_3 = 2$ on CPU $2$ . The last program run on this CPU was also program $2$ , so it takes $hot_2 = 1$ second to run. 在第二个CPU中运行第三个程序,由于第三个CPU刚执行完相同的程序,因此只需要花费一秒钟热启动即可。

In total, we need $3 + 2 + 1 = 6$ seconds to run them all. We can show this is optimal. 答案为 $6$。

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

- Run program $a_1 = 1$ on CPU $1$ . It takes $cold_1 = 5$ seconds to run.
- Run program $a_2 = 2$ on CPU $2$ . It takes $cold_2 = 3$ seconds to run.
- Run program $a_3 = 1$ on CPU $1$ . The last program run on this CPU was also program $1$ , so it takes $hot_1 = 2$ seconds to run.
- Run program $a_4 = 2$ on CPU $2$ . The last program run on this CPU was also program $2$ , so it takes $hot_2 = 1$ second to run.

In total, we need $5 + 3 + 2 + 1 = 11$ seconds. We can show this is optimal.
上一题 去做题 下一题