#P7302. [NOI1998] 免费的馅饼

    ID: 8177 远端评测题 1000ms 256MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>动态规划 DP1998线段树树状数组NOI

[NOI1998] 免费的馅饼

Problem Description

SERKOI has recently released a game called "Free Pies". The game is played on a stage. The stage has a width of ww cells (numbered from 11 to ww from left to right), and the player occupies one cell. At the beginning, the player may stand anywhere on the stage and holds a tray. The figure below shows a moment when the ceiling height is 44 cells and the player is catching pies.

After the game starts, pies continuously appear from the top cells of the stage ceiling and fall straight down. The player moves left and right to catch the pies. Each second, the player may move 11 or 22 cells to the left, move 11 or 22 cells to the right, or stay in place.

If at some moment a pie arrives exactly at the cell where the player is standing, the player collects that pie. If a pie lands in a cell where the player is not present, the pie disappears.

Write a program to help the player collect pies so that the sum of the scores of the collected pies is maximized.

Input Format

The first line contains two positive integers separated by spaces, giving the stage width ww and the number of pies nn.

The next nn lines each provide information about one pie.

Each line contains three positive integers, which are: the time tit_i when the pie reaches the stage, the index pip_i of the cell where it falls, and the score value viv_i.

The game starts at time 00.

In the input file, adjacent items on the same line are separated by one space.

In the input data, it is possible that two pies have the same tit_i and pip_i.

Output Format

Output one number, representing the maximum total score the player can obtain.

3 4
1 2 3
5 2 3
6 3 4
1 1 5
 
12

Hint

For 100%100\% of the data, 1w1081 \leq w \leq 10^8, 1n1051 \leq n \leq 10^5, 1ti1081 \leq t_i \leq 10^8, 1piw1 \leq p_i \leq w, 1vi10001 \leq v_i \leq 1000

Translated by ChatGPT 5