Under construction

Still being built. Things may change

Deleting Segments

Time limit1sMemory limit1024 MB

Summary
Delete the cheapest set of segments on a number line so that no group of mutually connected overlapping segments has more than K members.
Level

Medium6 of 10

Topics
Intervals, Dynamic programming, Sorting
Solved
No attempts yet

Statement

A number line has NN segments numbered 1…N1 \ldots N. Segment ii has endpoints SiS_i and EiE_i. Consider a graph GG with one vertex per segment, where two vertices are connected by an edge if their segments share at least one point (even if they only share an endpoint).

You must make sure that no connected component of this graph has more than KK vertices. To do this, you may delete some of the given segments. Deleting segment ii costs a fixed price WiW_i. Find a way to delete segments with the minimum total price.

Input

The first line contains integers NN and KK (1≤K≤N≤2 5001 \le K \le N \le 2\,500). Each of the next NN lines describes one segment with three integers SiS_i, EiE_i, and WiW_i (1≤Si≤Ei≤1091 \le S_i \le E_i \le 10^9, 1≤Wi≤1091 \le W_i \le 10^9).

Output

Print the minimum total price of the deletions on a single line.

Examples3

  1. Example 1

    Input
    5 2
    1 4 1
    3 6 2
    5 8 5
    7 10 2
    9 12 1
    
    Expected output
    3
    
  2. Example 2

    Input
    5 3
    2 3 6
    3 12 9
    12 14 20
    14 17 15
    17 26 9
    
    Expected output
    9
    
  3. Example 3

    Input
    6 1
    1 2 1000000000
    1 2 1000000000
    1 2 1000000000
    1 2 1000000000
    1 2 1000000000
    1 2 1000000000
    
    Expected output
    5000000000