#P3859. [TJOI2008] 小偷

[TJOI2008] 小偷

Description

The figure above shows the case of three rooms. The black parts are the doors connecting two adjacent rooms, numbered from left to right as 0,1,20,1,2\cdots. When the thief enters the storage through door 00, the timing system starts, and each door has its own closing time. Each room contains different kinds of gems. For each kind of gem, its value and the time the thief spends to take one are different. To simplify the problem, we assume the time to move between rooms is negligible, and the quantities of every kind of gem in all rooms are unlimited. What is the maximum total value of gems the thief can obtain while still being able to escape successfully?

Note: For each door, the thief must exit through it strictly before it closes.

Input Format

For each test case, the first line contains two integers NN and MM, representing that the storage has NN rooms and there are MM kinds of gems. The second line contains NN positive integers, where the ii-th integer denotes the closing time of door ii (doors are numbered from 00). The next MM lines each contain three integers rr, vv and tt, meaning that this kind of gem is located in room rr, its value is vv, and it takes time tt for the thief to take one.

Output Format

Output the maximum total value of gems the thief can obtain while successfully escaping the storage.

3 4
9 5 5
0 1 2
1 2 2
2 3 2
2 5 3

8

Hint

Sample Explanation

Although the gem worth 55 in room 22 looks good, it is better to take two gems worth 33, and then take two gems worth 11 in room 00, for a total value of 88.

Constraints and Conventions

For 100%100\% of the testdata, the number of rooms does not exceed 5050, the closing time of each door does not exceed 10001000, the number of gem types does not exceed 100100, and each value does not exceed 10001000.

Translated by ChatGPT 5