In a country, there are N cities numbered from 1 to N.
There are M trains in the country which are used by people to move from one city to another. Each train starts from city Li and ends at city Ri and the cost of taking the train is Wi. One can travel between any two cities (from smaller index to larger one) that lie between Li and Ri ( both inclusive) with the cost of Wi.
More formally people can travel from city u to v (Li≤u<v≤Ri) at the cost of Wi (1≤i≤M).
Task
Determine the minimum cost required to travel from city 1 to city N. If the city N is not reachable from city 1 then print -1.
Notes
Example
Assumptions
Approach
Hence, the answer is 15.
Function description
Complete the function solve provided in the editor. This function takes the following 3 parameters and returns the minimum cost required to travel from city 1 to city N:
Input format
Note: This is the input format that you must use to provide custom input (available above the Compile and Test button).
Output format
For each test case in a new line, print the minimum cost required to travel from city 1 to city N. If the city N is not reachable from city 1 then print -1.
Constraints
1≤T≤102≤N≤1051≤M≤1051≤Li<Ri≤N1≤Wi≤109
Code snippets (also called starter code/boilerplate code)
This question has code snippets for C, CPP, Java, and Python.
The first line contains the number of test cases, T = 2.
The first test case
Given
Approach
The second test case
Given
Approach