Colored Slime Balls

시간 제한2초메모리 제한2048 MB

요약
슬라임 공의 질량을 올려 판매하고 같은 색 이웃이 합쳐지도록 순서를 정해 순이익을 최대로 만든다.
난이도

어려움10점 중 8점

유형
동적 계획법, 구간, 그리디
정답자
아직 제출이 없습니다

문제

There are nn slime balls arranged in a row, where the ii-th slime ball has color c_ic\_i and mass m_im\_i.

You can perform any number of operations to increase the mass of a slime ball by 11, and it costs ww per operation.

However, 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 greater than or equal to kk. According to the market price, selling a slime ball with mass ii earns you income p_ip\_i.

It is guaranteed that p_i−p_i−1<wp\_i - p\_{i-1} < w, but p_ip\_i is not necessarily monotonically non-decreasing.

After you sell a slime ball, all the slime balls on its two sides move to close the gap. Moreover, if the ball you sold had two neighbors of the same color, they will merge into one slime ball whose mass is the sum of the two. This new slime ball may also need to be sold, continuing the process.

You want to know the maximum net profit after you sell all the slime balls.

입력

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 integer is the color c_ic\_i of the ii-th slime ball (1≤c_i≤n1 \le c\_i \le n). It is guaranteed that c_i≠c_i−1c\_i \ne c\_{i-1}. In other words, initially, there are no neighboring balls that have the same color.

The third line contains nn positive integers, where the ii-th integer represents the initial mass m_im\_i of the ii-th slime ball (1≤m_i<k)1 \le m\_i < k).

The fourth line contains k−1k - 1 integers representing the income from selling slime balls with masses kk to 2k−22 k - 2. Specifically, the numbers are p_k,p_k+1,…,p_2k−2p\_k, p\_{k + 1}, \ldots, p\_{2 k - 2} (0≤p_i≤1090 \le p\_i \le 10^9, and p_i−p_i−1<wp\_i - p\_{i - 1} < w).

출력

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

힌트

In the example, first, increase the mass of the slime ball with color 33. Then, it is sold, earning income 55.

Then, increase the mass of the slime ball with color 11 twice. Then, it is sold, earning income 55. Next, the two slime balls with color 22 merge and are sold, earning income 77.

After three operations incurring a total cost of 1818, the net profit is −1-1. It can be proved that there is no better solution.

예제1

  1. 예제 1

    입력
    4 5 6
    2 1 2 3
    3 3 3 4
    5 7 9 11
    
    예상 출력
    -1