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# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using static MyIO;

public struct Range
{
	public int Left;
	public int Right;
	public long Cost;

	public Range(int l, int r, long c)
	{
		Left = l;
		Right = r;
		Cost = c;
	}
}

public class D
{
	public static void Main() => (new Solver()).Solve();
}

public class Solver
{
	public void Solve()
	{
		int N = GetInt();
		int M = GetInt();
		int[] L = new int[M];
		int[] R = new int[M];
		long[] C = new long[M];
		for(int i = 0; i < M; i++)
		{
			L[i] = GetInt() - 1;
			R[i] = GetInt() - 1;
			C[i] = GetLong();
		}

		var pq = new PriorityQueue<Range>(M, (x, y) => y.Right - x.Right);
		for(int i = 0; i < M; i++)
			pq.Push(new Range(L[i], R[i], C[i]));

		var next = new SortedSet<int>();
		long[] d = Enumerable.Repeat(-1L, N).ToArray();
		int pos = N - 1;
		d[pos] = 0;
		while(pq.Size > 0)
		{
			var range = pq.Pop();
			while(next.Count > 0 && pos > range.Right)
			{
				pos = next.Max;
				next.Remove(pos);
			}
			if(pos > range.Right)
				break;

			if(pos < range.Left)
				continue;

			if(d[range.Left] == -1)
				d[range.Left] = d[pos] + range.Cost;
			else
				chmin(ref d[range.Left], d[pos] + range.Cost);

			next.Add(range.Left);
		}

		if(d[0] == -1)
			Console.WriteLine(-1);
		else
			Console.WriteLine(d[0]);
	}

	private void chmin(ref long x, long y) => x = Math.Min(x, y);
}

public class PriorityQueue<T>
{
	public long Size { get; private set; } = 0;
	public long MaxSize { get; private set; } = 0;
	private T[] m_heap;
	private Comparison<T> Comp = null;
	
	public PriorityQueue(long maxSize, Comparison<T> comp)
	{
		if(maxSize <= 0)
			throw new Exception();
		MaxSize = maxSize;
		m_heap = new T[maxSize];

		Comp = comp;
	}
	
	public void Push(T x)
	{
		if(Size == MaxSize)
		{
			T[] new_heap = new T[MaxSize << 1];
			Array.Copy(m_heap, new_heap, MaxSize);
			m_heap = new_heap;
			MaxSize <<= 1;
		}
		
		long i = Size++;
		while(i > 0)
		{
			long p = (i - 1)/2;
			if(Comp(m_heap[p], x) <= 0)
				break;
			m_heap[i] = m_heap[p];
			i = p;			
		}
		m_heap[i] = x;		
	}
	
	public T Pop()
	{
		if(Size == 0)
			throw new Exception("Queue is empty.");
		
		T result = m_heap[0];
		T x = m_heap[--Size];
		long i = 0;
		while(i * 2 + 1 < Size)
		{
			long c1 = i * 2 + 1, c2 = i * 2 + 2;
			if(c2 < Size && Comp(m_heap[c2], m_heap[c1]) < 0)
				c1 = c2;
			if(Comp(m_heap[c1], x) >= 0)
				break;
			m_heap[i] = m_heap[c1];
			i = c1;
		}
		m_heap[i] = x;
		
		return result;		
	}	
}

public static class MyIO
{
	private static string[] args = null;
	private static int num = -1;
	private static int used = -1;

	private static string getArg()
	{
		if(used == num)
		{
			args = Console.ReadLine().Split(' ');
			num = args.Length;
			used = 0;
		}
		return args[used++];
	}

	public static int GetInt() => int.Parse(getArg());
	public static long GetLong() => long.Parse(getArg());
	public static double GetDouble() => double.Parse(getArg());
	public static string GetString() => getArg();
	public static char GetChar() => getArg()[0];
	public static int[] GetInts(int N) => Enumerable.Range(0, N).Select(_ => GetInt()).ToArray();
	public static long[] GetLongs(int N) => Enumerable.Range(0, N).Select(_ => GetLong()).ToArray();
	public static double[] GetDoubles(int N) => Enumerable.Range(0, N).Select(_ => GetDouble()).ToArray();
	public static string[] GetStrings(int N) => Enumerable.Range(0, N).Select(_ => GetString()).ToArray();
}