#P1541. Turtle Chess Maximum Score

    ID: 14827 Type: Default 1000ms 256MiB

Turtle Chess Maximum Score

Turtle Chess Maximum Score

On his birthday, Xiao Ming received a Turtle Chess game as a gift from his father. The game board consists of a single row of \(N\) cells, where each cell has a nonnegative integer score. The first cell (cell 1) is the starting point and the \(N\)th cell is the destination. When playing the game, a turtle piece starts at cell 1 (automatically collecting its score) and, by using a series of crawling cards, moves along the board until it exactly lands on cell \(N\).

There are \(M\) crawling cards available. Each card is labeled with one of the four numbers \(1, 2, 3, 4\). Note that not all card types need to be present among the \(M\) cards. In each move, the player must choose one of the unused cards, and the turtle moves forward by the number of cells indicated on the card. Each card can only be used once.

During the game, the turtle collects the score at the starting cell and then every time it lands on a new cell it collects the score of that cell. The final game score is the sum of the scores of all cells that the turtle has visited from start to finish.

Different orders in which the cards are used will yield different sets of visited cells, and therefore different totals of collected scores. Xiao Ming wishes to maximize his score. Given the scores on each cell of the board and the values on the \(M\) cards, determine the maximum score that can be obtained.

Note: It is guaranteed that the sum of the card values is exactly \(N-1\) so that the turtle can reach the destination.

inputFormat

The first line contains two integers \(N\) and \(M\) separated by a space.

The second line contains \(N\) nonnegative integers, where the \(i\)th integer represents the score of the \(i\)th cell on the board.

The third line contains \(M\) integers. Each integer is one of {1, 2, 3, 4} and represents the value on a crawling card.

It is guaranteed that \(\sum_{\text{card in cards}} (\text{card}) = N-1\).

outputFormat

Output a single integer, which is the maximum score that can be obtained by selecting an optimal order of using the cards.

sample

5 2
1 2 3 4 5
3 1
10