#P10978. Fence

Fence

Problem Description

A team of kk workers (1k1001 \leq k \leq 100) needs to paint a fence consisting of NN wooden boards, numbered from left to right as 11 to NN (1N16,0001 \leq N \leq 16,000). Each worker ii (1ik1 \leq i \leq k) should sit in front of board SiS_i. He can only paint one continuous interval (meaning the boards in the interval must be consecutive). This interval must contain board SiS_i (in particular, the interval may also be empty, i.e., a worker may choose not to work). In addition, the number of boards painted by worker ii must not exceed LiL_i, and he earns PiP_i dollars for each board he paints (1Pi10,0001 \leq P_i \leq 10,000). Each board can be painted by at most one worker. All SiS_i are distinct.

As the team leader, you need to decide the interval each worker should paint and make sure the total income is maximized. The total income is the sum of all workers' individual incomes.

Write a program to determine the maximum possible total income earned by the kk workers.

Note: you do not have to paint all boards.

Input Format

The first line of input contains two positive integers N,kN, k.

The next kk lines each contain three positive integers Li,Pi,SiL_i, P_i, S_i.

Output Format

Output one integer, representing the maximum total income.

8 4
3 2 2
3 2 3
3 3 5
1 1 7 
17

Hint

Translated by ChatGPT 5