PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#CT00000055. Lịch hoạt động (Activity Schedule)

Bài tập này chưa được xếp vào lộ trình.

Lịch hoạt động (Activity Schedule)

Activity Schedule

Version: Phuoc Hung OJ Extended

Problem Statement

A festival offers NN activities, numbered from 11 to NN in input order. Activity ii begins at time sis_i, ends at time tit_i, and awards viv_i points if attended in full.

The activity occupies the half-open interval [si,ti)[s_i,t_i): it includes its starting time sis_i but not its ending time tit_i. Selected activities must not overlap. If one activity ends exactly when another begins, both may be chosen. Every selected activity must be attended in full.

A student may choose at most KK activities and need not choose exactly KK. Choosing no activity is allowed and gives a total score of 00. The score is the sum of viv_i over the selected activities.

Find the maximum achievable total score.

Input

  • The first line contains two positive integers N,KN,K: the number of activities and the maximum number of activities that may be selected.
  • Each of the next NN lines contains three integers si,ti,vis_i,t_i,v_i: the starting time, ending time, and score of activity ii. The activities are not necessarily listed in chronological order.

Output

Print one integer: the maximum total score obtainable by selecting at most KK pairwise non-overlapping activities.

Subtasks

The following general constraints apply to all subtasks: 1≤N≤50001\le N\le 5000; 1≤K≤min⁡(N,50)1\le K\le\min(N,50); 0≤si<ti≤1090\le s_i<t_i\le 10^9; 1≤vi≤1091\le v_i\le 10^9.

  • Subtask 1 (30%): N≤20N\le 20.
  • Subtask 2 (30%): K=1K=1.
  • Subtask 3 (40%): No additional restrictions beyond the general constraints.

Examples

Example 1

Input

4 2
1 3 5
3 5 6
2 6 9
5 7 4

Output

11

Explanation

Activity 11 occupies [1,3)[1,3) and is worth 55 points; activity 22 occupies [3,5)[3,5) and is worth 66 points. The first ends exactly when the second begins, so both may be selected, giving 5+6=115+6=11 points with K=2K=2 activities.

Another compatible pair, activities 22 and 44, gives 6+4=106+4=10 points. Activity 33 gives 99 points but overlaps the other activities it could be paired with to exceed 1111. Hence the maximum is 1111.

Example 2

Input

3 1
1 4 3
2 3 5
3 8 4

Output

5

Explanation

Since K=1K=1, at most one activity may be chosen. The three activities award 3,5,43,5,4 points, respectively. Choosing activity 22 yields the maximum score of 55.

Example 3

Input

4 3
1 2 4
2 3 5
3 4 6
1 4 100

Output

100

Explanation

The first three activities occupy [1,2)[1,2), [2,3)[2,3), and [3,4)[3,4); they can all be attended consecutively, giving 4+5+6=154+5+6=15 points. Activity 44 occupies [1,4)[1,4) and overlaps each of those three, so none may be selected together with it. Choosing activity 44 alone yields 100100 points, greater than 1515. The answer is 100100.