cswork / OAOpen practice workspace

AmazonOA #301

Minimum Preparation Time for Two Handlers

Estimated HardJudge-ready

Topics (AI-labelled)State MachinesHashing

Following the original order, assign each task to one of two processors; each processor remembers the type it handled last. If a task's type matches that processor's last type, it costs short[t], otherwise long[t]; a processor's first use costs long[t]. Find the minimum total cost of all tasks; the order may not change. short is not guaranteed to be ≤long.

Input

The first line contains n m, the second line contains n types in 1..m, the third line contains m short values, and the fourth line contains m long values. On this site 1≤n,m≤200000, 0≤short[t],long[t]≤10^9, and short>long as well as zero costs are allowed.

Output

Output a single integer, the answer.

Example 1

Input:

3 1
1 1 1
9
1

Output:

11

Example 2

Input:

4 2
1 2 1 2
0 0
5 7

Output:

12

Example 3

Input:

2 1
1 1
0
0

Output:

0

Example notes

The answers are 11, 12 and 0. In the first example the first two tasks each use an unused processor, and the third task must not invent a third cold-start processor.

Editorial

Loading solution…