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 Scala (2.11.7) (judge verdict: Wrong Answer)

import java.io._
import java.util.StringTokenizer
import scala.util.control.Breaks.{breakable,break}
import scala.collection.mutable.{Map, Stack, Queue, PriorityQueue}

object Main extends App {
  val in = new InputReader(System.in)
  val pw = new PrintWriter(System.out)

  val N, M = in.next().toInt
  val _graph = new Array[((Int, Int), Long)](M)
  for (i <- 0 until M) {
    val L, R = in.next().toInt
    val C = in.next().toLong
    _graph(i) = ((L, R), C)
  }

  val graph = _graph.sorted


  //
  val rmq = new RMQ(1<<17)
  var ans = 0L

  var right = 1 // 届く右端

  var nowL = 0
  var now = 0L
  rmq.update(1, 0)

  breakable {
    for (node <- graph) {
      val L = node._1._1
      val R = node._1._2
      val C = node._2

      if (L > right) {
        ans = -1
        break
      }
      else {
        if (L != nowL) {
          nowL = L
          now = rmq.query(L, N+1)
        }

        right = Math.max(right, R)
        rmq.update(R, now + C)
      }
    }
  }

  if(ans != -1)
    ans = rmq.query(N, N+1)

  println(ans)
}

// MaxN : 2の乗数でないといけない。要素数に応じた値。(e.g. 10^5なら1<<17にしておくとよい)
class RMQ(MaxN: Int) {

  val dat = Array.fill[Long](2*MaxN - 1)(Long.MaxValue)

  // k番目の値(0-indexed)をaに変更
  def update(_k: Int, a: Long): Unit = {
    // 葉の節点
    var k = _k + (MaxN - 1)
    dat(k) = Math.min(dat(k), a)

    // 登りながら更新
    while (k > 0) {
      k = (k-1) / 2
      dat(k) = Math.min(dat(2*k + 1), dat(2*k + 2))
    }
  }

  // [a, b) の最小値を求める
  // 後ろの方の引数は、計算の簡単のための引数
  // kは節点の番号。l, r はその節点が[l, r) に対応づいていることを表す。
  def query(a: Int, b: Int): Long = {
    def func(k: Int, l: Int, r: Int): Long = {
      if (r <= a || b <= l) // [a, b)と[l, r)が交差しなければ、Int.MaxValue
        Long.MaxValue
      else if (a <= l && r <= b) // [a, b)が[l, r)を完全に含んでいれば、この節点の値
        dat(k)
      else { // そうでなければ、２つの子の最小値
        val vl = func(2*k + 1, l, (l+r)/2)
        val vr = func(2*k + 2, (l+r)/2, r)

        Math.min(vl, vr)
      }
    }

    func(0, 0, MaxN)
  }

  def print(n: Int): Unit = {
    println(dat.take(n).deep)
  }

}


class InputReader(stream: InputStream) {
  private[this] val reader = new BufferedReader(new InputStreamReader(stream))
  private[this] var tokenizer: StringTokenizer = new StringTokenizer(reader.readLine())
  def next(): String = {
    while (!tokenizer.hasMoreTokens()) {
      tokenizer = new StringTokenizer(reader.readLine())
    }
    tokenizer.nextToken()
  }
}

object Sub {
  def print2DimArray[A](mat: Array[Array[A]]): Unit = {
    mat.foreach(lin => println(lin.deep))
  }
}