Crystals
Unrated
English
Language
Contribute a translation- Time limit
- 1000 ms
- Memory limit
- 1024 MB
- Submissions
- 0
- Correct
- 0
- Solved by
- 0
- AC rate
- —
Statement
A row contains $N$ crystals. The type of the $i$-th crystal from the left is $A_{i}$.
For each type $c$, removing crystals of type $c$ has an activation cost $X_{c}$.
You may perform the following operation any number of times:
- Choose a nonempty contiguous block of crystals that all have the same type $c$, and remove every crystal in the chosen block, paying a cost of $X_{c}$.
- The crystals to the left and right of the removed block close the gap while preserving their relative order.
Determine the minimum total cost required to remove all crystals.
Input
The first line contains two integers $N$ and $M$.
The second line contains integers $A_1,A_2,\ldots,A_N$, separated by spaces.
The third line contains integers $X_1,X_2,\ldots,X_M$, separated by spaces.
Constraints
- $1 \le N \le 400$
- $1 \le M \le N$
- $1 \le A_{i} \le M$
- Every type from $1$ to $M$ appears at least once.
- $1 \le X_{c} \le 10^{9}$
Output
Print the minimum total cost required to remove all crystals.
Examples
Sample input 1
3 2 1 2 1 5 2
Sample output 1
7
First remove the crystal of type $2$, paying $2$. The two crystals of type $1$ then become adjacent and can be removed together for $5$.
Sample input 2
5 2 1 2 2 1 2 7 3
Sample output 2
13
Sample input 3
5 5 1 2 3 4 5 10 10 10 10 10
Sample output 3
50
Tags
No tags yet