Painting the road
Practice
5 (1 votes)
Algorithms
Medium
Problem
53% Success 366 Attempts 30 Points 5s Time Limit 256MB Memory 1024 KB Max Code

There is a kingdom, that has a long straight road of length L meters and width 0. Coordinate x on the road denotes point that is at x meters from the start of the road. That is coordinate 0 denotes beginning of the road and coordinate L denotes end of the road. The road has to be painted with color. There are K proposals, Each proposal is of the form a, b, c which means that the road can be painted from coordinate a to coordinate b for the cost c. Note that some part of road can be painted multiple times and it is still considered to be painted.

The king wants to know if the whole road can be painted and if possible, also wants to know the minimum cost that is required to paint the entire road.

Input
First line of the input is the number of test cases T. It is followed by T test cases.In each test case first line has two space separated integers, the length of the road L and the number of proposals K. It is followed by K lines, which are K proposals, each proposal is 3 space separated integers a b and c.

Output
For each test case output a single integer, that is minimum cost required to paint the enitre road. If it is impossible to paint the entire road, print -1.

Constraints
1 <= T <= 10
1 <= L <= 256
1 <= K <= 512
0 <= a < b <= L
1 <= c <= 1000

Read the editorial here.

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
58 votes
Tags:
Dynamic ProgrammingOpenApprovedMedium
Points:30
1 votes
Tags:
Dynamic ProgrammingAlgorithmsC++3D dynamic programming
Points:30
106 votes
Tags:
Dynamic ProgrammingOpenApprovedMedium