Portals II


Portals II

You are standing at the first index of an array aa of length nn. At index ii, you can either take a step forward to i+1i+1, or if there is a portal (mm portals) x,y,zx,y,z such that i==xi==x , you can teleport to any index in [y,z][y,z]. The score of a path is defined as the maximum sum of values of the indicies you visit. What is the maximum score with which you can get to index n−1n-1, starting from the start of the array?

Sample Input

Input 1
5 1
1 2 -100 4 5
1 2 3

Sample Output

Output 1
12

Constraints

1≤n≤1e51 \le n \le 1e5

1≤m≤1e51 \le m \le 1e5

−1e3≤a[i]≤1e3-1e3 \le a[i] \le 1e3

0≤x<y≤z<n0 \le x < y \le z < n

Comments0


No comments yet

Be the first to comment.

New comment


Log in to join the discussion.