#P15944. [JOI Final 2026] 传送机 2 / Teleporter 2

[JOI Final 2026] 传送机 2 / Teleporter 2

Problem Description

There are NN points on a straight path, numbered 1,2,,N1,2,\dots,N from left to right. The path is one-way from left to right.

There are also MM teleporter devices numbered 1,2,,M1,2,\dots,M. By using device ii (1iM1 \leq i \leq M), one can warp from point SiS_i to point TiT_i (Si<TiS_i < T_i).

Bitaro is currently at point 11 and wants to reach point NN. When Bitaro is at point jj (1jN11 \leq j \leq N-1), he can take one of the following actions:

  • Move on foot to point j+1j+1.
  • Choose an ii (1iM1 \leq i \leq M) satisfying Si=jS_i = j, use device ii, and warp to point TiT_i.

It is known that warp travel places stress on the body. You are concerned about Bitaro’s safety, so you decide to destroy zero or more teleporter devices so that, no matter which route Bitaro takes, the number of warp travels is at most KK. Device ii can be destroyed by paying cost CiC_i; if so, Bitaro can no longer use that device.

Find the minimum possible total cost you need to pay when destroying zero or more teleporter devices so that, regardless of Bitaro’s route, the number of warp travels is at most KK.

Input Format

Read the following data from the standard input.

NN MM KK
S1S_1 T1T_1 C1C_1
S2S_2 T2T_2 C2C_2
\vdots
SMS_M TMT_M CMC_M

Output Format

Write one line to the standard output containing the minimum possible total cost.

8 4 1
1 4 3
2 3 5
3 6 2
5 8 2
4
12 7 2
1 5 3
4 8 2
2 4 5
2 4 8
7 9 4
9 11 7
3 10 5
6
6 3 2
1 4 2
2 5 4
3 6 3
0

Hint

Sample 1

Consider the case where devices 33 and 44 are destroyed.

Then Bitaro can use only devices 11 and 22. When moving from point 11 to point 88, Bitaro will always make at most one warp travel, so the condition is satisfied.

In this case, the total paid cost is 44. Since it is impossible to make the cost at most 33, the correct output is 44.

This input example satisfies the constraints of all subtasks.

Sample 2

Destroying devices 22 and 55 is optimal.

This input example satisfies the constraints of subtasks 2,3,4,5,62,3,4,5,6.

Sample 3

In this case, there is no need to destroy any device.

This input example satisfies the constraints of subtasks 2,3,4,5,62,3,4,5,6.

Constraints

  • 2N1000002 \leq N \leq 100000.
  • 1KM1000001 \leq K \leq M \leq 100000.
  • 1Si<TiN1 \leq S_i < T_i \leq N (1iM1 \leq i \leq M).
  • 1Ci1091 \leq C_i \leq 10^9 (1iM1 \leq i \leq M).
  • Given values are all integers.

Subtasks

  1. (5 points) K=1K = 1.
  2. (3 points) N20N \leq 20, M20M \leq 20.
  3. (29 points) N500N \leq 500, M500M \leq 500.
  4. (23 points) N4000N \leq 4000, M4000M \leq 4000.
  5. (24 points) N40000N \leq 40000, M40000M \leq 40000.
  6. (16 points) No additional constraints.