Under construction

Still being built. Things may change

Colored Slime Balls

Time limit2sMemory limit2048 MB

Summary
Find the maximum net profit from selling every slime ball in a row, paying w per mass increase, where same-colored neighbors merge when the ball between them is sold.
Level

Hard9 of 10

Topics
Dynamic programming, Intervals
Solved
No attempts yet

Problem

There are nn slime balls arranged in a row. The ii-th slime ball has color cic_i and mass mim_i. You can perform any number of operations, each increasing the mass of one slime ball by 11 at a cost of ww per operation. Once the mass of a slime ball reaches kk or more, it becomes unstable and must be sold before the next operation. You can only sell slime balls with mass at least kk. Selling a slime ball with mass ii earns income pip_i. It is guaranteed that pi−pi−1<wp_i - p_{i-1} < w, but pip_i is not necessarily non-decreasing.

After you sell a slime ball, the slime balls on its two sides move to close the gap. If the sold ball had two neighbors of the same color, they merge into one slime ball whose mass is the sum of their masses. This new slime ball may also need to be sold, and the process continues. Find the maximum net profit after you sell all the slime balls.

Input

The first line contains three positive integers nn, kk, ww (1≤n≤1501 \le n \le 150, 2≤k≤102 \le k \le 10, 1≤w≤1061 \le w \le 10^6). The second line contains nn positive integers, where the ii-th is the color cic_i (1≤ci≤n1 \le c_i \le n). It is guaranteed that ci≠ci−1c_i \ne c_{i-1}, so no two neighboring balls share a color initially. The third line contains nn positive integers, where the ii-th is the initial mass mim_i (1≤mi<k1 \le m_i < k). The fourth line contains k−1k - 1 integers pk,pk+1,…,p2k−2p_k, p_{k+1}, \ldots, p_{2k-2} (0≤pi≤1090 \le p_i \le 10^9, pi−pi−1<wp_i - p_{i-1} < w).

Output

Print a single integer: the maximum net profit from selling all the slime balls.

Hint

In the example, first increase the mass of the slime ball with color 33. It is then sold, earning 55. Next, increase the mass of the slime ball with color 11 twice. It is then sold, earning 55. After that, the two slime balls with color 22 merge and are sold, earning 77. The three operations cost 1818 in total, so the net profit is −1-1. No better solution exists.

Examples1

  1. Example 1

    Input
    4 5 6
    2 1 2 3
    3 3 3 4
    5 7 9 11
    
    Expected output
    -1