#P10108. [GESP202312 六级] 闯关游戏

[GESP202312 六级] 闯关游戏

Background

Related multiple-choice and true/false questions: https://ti.luogu.com.cn/problemset/1138.

Problem Description

You have arrived at a level-clearing game.

This game has a total of NN levels. Each level has MM passages. You need to choose one passage to move to later levels. The ii-th passage lets you move forward by aia_i levels. That is, if you are currently at level xx, then after choosing the ii-th passage, you will go directly to level x+aix+a_i (in particular, if x+aiNx + a_i \geq N, then you clear the game). Also, when you successfully leave level ss, you will gain bsb_s points.

At the start of the game, you are at level 00. What is the maximum total score you can get when you clear the game?

Input Format

The first line contains two integers NN and MM, representing the number of levels and the number of passages in each level.

The next line contains MM integers a0,a1,aM1a_0,a_1\cdots,a_{M-1} separated by single spaces. It is guaranteed that 1aiN1\le a_i \le N.

The next line contains NN integers b0,b1,bN1b_0,b_1\cdots,b_{N-1} separated by single spaces. It is guaranteed that bi105|b_i|\le 10^5.

Output Format

Output one integer in one line, representing the maximum score you can obtain when you clear the game.

6 2 
2 3
1 0 30 100 30 30
131
6 2
2 3
1 0 30 100 30 -1
101

Hint

Sample Explanation 1

You can choose the 11-st passage at level 00, gain 11 point, and arrive at level 33. Then choose the 00-th passage, gain 100100 points, and arrive at level 55. Finally, choose any passage, and you can gain 3030 points and clear the game. In this way, the total score is 1+100+30=1311+100+30=131.

Sample Explanation 2

Note that the scores of some levels may be negative.

Constraints

For 20%20\% of the testdata, it is guaranteed that M=1M=1.

For 40%40\% of the testdata, it is guaranteed that N20N \le 20 and M2M\le 2.

For all testdata, it is guaranteed that 1N1041 \le N \le 10^4 and 1M1001 \le M\le 100.

Translated by ChatGPT 5