Problem:
Score : 600 points

Problem Statement

We have N points numbered 1 to N arranged in a line in this order.

Takahashi decides to make an undirected graph, using these points as the vertices.
In the beginning, the graph has no edge. Takahashi will do M operations to add edges in this graph.
The i-th operation is as follows:

The operation uses integers L_i and R_i between 1 and N (inclusive), and a positive integer C_i. For every pair of integers (s, t) such that L_i \leq s < t \leq R_i, add an edge of length C_i between Vertex s and Vertex t.

The integers L_1, ..., L_M, R_1, ..., R_M, C_1, ..., C_M are all given as input.

Takahashi wants to solve the shortest path problem in the final graph obtained. Find the length of the shortest path from Vertex 1 to Vertex N in the final graph.

Constraints

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq L_i < R_i \leq N

1 \leq C_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N M
L_1 R_1 C_1
:
L_M R_M C_M

Output

Print the length of the shortest path from Vertex 1 to Vertex N in the final graph.
If there is no shortest path, print -1 instead.

Sample Input 1

4 3
1 3 2
2 4 3
1 4 6

Sample Output 1

5

We have an edge of length 2 between Vertex 1 and Vertex 2, and an edge of length 3 between Vertex 2 and Vertex 4, so there is a path of length 5 between Vertex 1 and Vertex 4.

Sample Input 2

4 2
1 2 1
3 4 2

Sample Output 2

-1

Sample Input 3

10 7
1 5 18
3 4 8
1 3 5
4 7 10
5 9 8
6 10 5
8 10 3

Sample Output 3

28

Solution in C (GCC 5.4.1) (judge verdict: Accepted)

#include<stdio.h>
#include<stdlib.h>
int l[100005], r[100005];
long long int c[100005];
int asc(const void* a, const void* b)
{
	if (l[*(int*)a] > l[*(int*)b])return 1;
	if (l[*(int*)a] < l[*(int*)b])return -1;
	return 0;
}
long long int min(long long int a, long long int b)
{
	if (a < b)
		return a;
	else
		return b;
}
int main()
{
	int n, m;
	scanf("%d %d", &n, &m);
	int i, j;
	for (i = 0; i < m; i++)
	{
		scanf("%d %d %lld", &l[i], &r[i], &c[i]);
		l[i]--;
		r[i]--;
	}
	int a[100005];
	for (i = 0; i < m; i++)
		a[i] = i;
	qsort(a, m, sizeof(int), asc);
	long long int seg[400005];
	int p = 1;
	while (p < n)
		p *= 2;
	for (i = 0; i < 2 * p - 1; i++)
		seg[i] = 1000000000000000018;
	seg[p - 1] = 0;
	int sl, sr;
	for (i = 0; i < m; i++)
	{
		sl = 0;
		sr = p;
		j = 0;
		while (j < p - 1)
		{
			seg[2 * j + 1] = min(seg[2 * j + 1], seg[j]);
			seg[2 * j + 2] = min(seg[2 * j + 2], seg[j]);
			if (l[a[i]] < (sr + sl) / 2)
			{
				sr = (sl + sr) / 2;
				j = 2 * j + 1;
			}
			else
			{
				sl = (sl + sr) / 2;
				j = 2 * j + 2;
			}
		}
		sl = l[a[i]] + p - 1;
		sr = r[a[i]] + p - 1;
		while (sl != sr)
		{
			if (sl % 2 == 0)
				seg[sl] = min(seg[sl], seg[l[a[i]] + p - 1] + c[a[i]]);
			if (sr % 2 > 0)
			{
				seg[sr] = min(seg[sr], seg[l[a[i]] + p - 1] + c[a[i]]);
				sr--;
			}
			sl = (sl - 1) / 2;
			sr = (sr - 1) / 2;
		}
		seg[sl] = min(seg[sl], seg[l[a[i]] + p - 1] + c[a[i]]);
	}
	sl = 0;
	sr = p;
	j = 0;
	while (j < p - 1)
	{
		seg[2 * j + 1] = min(seg[2 * j + 1], seg[j]);
		seg[2 * j + 2] = min(seg[2 * j + 2], seg[j]);
		if (n - 1 < (sr + sl) / 2)
		{
			sr = (sl + sr) / 2;
			j = 2 * j + 1;
		}
		else
		{
			sl = (sl + sr) / 2;
			j = 2 * j + 2;
		}
	}
	if (seg[n + p - 2] > 1000000000000015)
		printf("-1\n");
	else
		printf("%lld\n", seg[n + p - 2]);
	return 0;
}