NKPANO - Billboard painting

no tags 

A group of K painters has to paint a rectangular billboard of size 1xN. The billboard is divided into N cells of size 1x1. The cells are numbered from 1 to N in left to right order.

The i-th painter (1 ≤ i ≤ K) is currently standing in front of the cell Si. He either can paint no cells at all or can paint a consecutive number of cells that must containt the cell Si. Moreover, he can only paint at most Li cells, and for each painted cell, he will receive a payment of Pi dollars. Each cell can be painted by at most one person.

Your task is to find a way for the painters to receive the largest amount of payment.


  • The first line of the input contains two positive integers N, K (N ≤ 16000; K ≤ 100).
  • The i-th line of the next K lines contains three positive integers Li , Pi, Si (i = 1, 2, … K) separated by spaces (1 ≤ Pi ≤ 10000, 1 ≤ Li , Si ≤ N ). The numbers S1, S2, . . . SK are pairwise distinct.


A single line contains the largest payment that the painters can receive.


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


The first painter will paint the first two cells, the second painter will paint the next two cells, the third one will paint the remaining cells and the last won't have to paint any cells.

Added by:Jimmy
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET
Resource:Prof. Nguyen Duc Nghia