E - Road Repair Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 433 点

問題文

ある街に、一直線に伸びる道路があります。この道路は、端から順に区画 1、区画 2、\dots、区画 N の N 個の区画に分割されています。

現在、すべての区画が傷んでおり、補修工事を行う必要があります。

高橋君は、M 社の建設会社の中からいくつかを選んで工事を依頼することにしました。各会社に依頼できる回数はそれぞれ高々 1 回です。

i 番目の会社 (1 \leq i \leq M) に費用 C_i を支払って依頼すると、区画 L_i, L_i+1, \dots, R_i のすべての区画を補修してもらえます。

高橋君の目的は、区画 1 から区画 N までのすべての区画を補修することです。同じ区画が複数の会社によって補修されても構いません。

この目的を達成するために必要な費用の合計の最小値を求めてください。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq M \leq 10^5
  • 1 \leq L_i \leq R_i \leq N (1 \leq i \leq M)
  • 1 \leq C_i \leq 10^9 (1 \leq i \leq M)
  • 入力に含まれる値はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

N M
L_1 R_1 C_1
L_2 R_2 C_2
\vdots
L_M R_M C_M

出力

すべての区画を補修するために必要な費用の合計の最小値を 1 行で出力せよ。

ただし、どのように会社を選んでもすべての区画を補修することができない場合は、代わりに -1 を出力せよ。


入力例 1

5 4
1 3 10
2 4 10
3 5 20
4 5 15

出力例 1

25

入力例 2

5 3
1 2 5
4 5 10
1 1 2

出力例 2

-1

入力例 3

10 8
1 4 1000000000
1 3 500000000
3 6 400000000
4 8 300000000
2 7 800000000
6 10 300000000
7 10 600000000
8 10 200000000

出力例 3

1000000000

入力例 4

20 20
1 5 12
2 6 8
3 8 15
1 4 20
4 9 10
5 10 14
7 12 11
8 13 9
10 15 13
11 16 7
9 14 16
13 17 12
14 18 10
15 19 8
16 20 15
17 20 9
1 10 50
6 15 45
11 20 40
1 20 100

出力例 4

42

入力例 5

1 1
1 1 1000000000

出力例 5

1000000000

Score : 433 pts

Problem Statement

In a certain town, there is a road that extends in a straight line. This road is divided into N sections, numbered section 1, section 2, \dots, section N in order from one end.

Currently, all sections are damaged and require repair work.

Takahashi has decided to choose some construction companies from among M companies to request the work. Each company can be hired at most once.

If he pays a cost of C_i to hire the i-th company (1 \leq i \leq M), they will repair all sections L_i, L_i+1, \dots, R_i.

Takahashi's goal is to repair all sections from section 1 to section N. It is acceptable for the same section to be repaired by multiple companies.

Find the minimum total cost required to achieve this goal.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq M \leq 10^5
  • 1 \leq L_i \leq R_i \leq N (1 \leq i \leq M)
  • 1 \leq C_i \leq 10^9 (1 \leq i \leq M)
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N M
L_1 R_1 C_1
L_2 R_2 C_2
\vdots
L_M R_M C_M

Output

Print the minimum total cost required to repair all sections in a single line.

However, if it is impossible to repair all sections no matter how the companies are chosen, print -1 instead.


Sample Input 1

5 4
1 3 10
2 4 10
3 5 20
4 5 15

Sample Output 1

25

Sample Input 2

5 3
1 2 5
4 5 10
1 1 2

Sample Output 2

-1

Sample Input 3

10 8
1 4 1000000000
1 3 500000000
3 6 400000000
4 8 300000000
2 7 800000000
6 10 300000000
7 10 600000000
8 10 200000000

Sample Output 3

1000000000

Sample Input 4

20 20
1 5 12
2 6 8
3 8 15
1 4 20
4 9 10
5 10 14
7 12 11
8 13 9
10 15 13
11 16 7
9 14 16
13 17 12
14 18 10
15 19 8
16 20 15
17 20 9
1 10 50
6 15 45
11 20 40
1 20 100

Sample Output 4

42

Sample Input 5

1 1
1 1 1000000000

Sample Output 5

1000000000