Colored Slime Balls
시간 제한2초메모리 제한2048 MB
슬라임 공의 질량을 올려 판매하고 같은 색 이웃이 합쳐지도록 순서를 정해 순이익을 최대로 만든다.
문제
There are slime balls arranged in a row, where the -th slime ball has color and mass .
You can perform any number of operations to increase the mass of a slime ball by , and it costs per operation.
However, once the mass of a slime ball reaches 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 . According to the market price, selling a slime ball with mass earns you income .
It is guaranteed that , but 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 , , (, , ).
The second line contains positive integers, where the -th integer is the color of the -th slime ball (). It is guaranteed that . In other words, initially, there are no neighboring balls that have the same color.
The third line contains positive integers, where the -th integer represents the initial mass of the -th slime ball (.
The fourth line contains integers representing the income from selling slime balls with masses to . Specifically, the numbers are (, and ).
출력
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 . Then, it is sold, earning income .
Then, increase the mass of the slime ball with color twice. Then, it is sold, earning income . Next, the two slime balls with color merge and are sold, earning income .
After three operations incurring a total cost of , the net profit is . It can be proved that there is no better solution.