Colored Slime Balls
Time limit2sMemory limit2048 MB
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 slime balls arranged in a row. The -th slime ball has color and mass . You can perform any number of operations, each increasing the mass of one slime ball by at a cost of per operation. 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 at least . Selling a slime ball with mass earns income . It is guaranteed that , but 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 , , (, , ). The second line contains positive integers, where the -th is the color (). It is guaranteed that , so no two neighboring balls share a color initially. The third line contains positive integers, where the -th is the initial mass (). The fourth line contains integers (, ).
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 . It is then sold, earning . Next, increase the mass of the slime ball with color twice. It is then sold, earning . After that, the two slime balls with color merge and are sold, earning . The three operations cost in total, so the net profit is . No better solution exists.