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

#BS0000462. Lập lịch công việc có lợi nhuận lớn nhất (Maximum Profit in Job Scheduling)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Lập lịch công việc có lợi nhuận lớn nhất (Maximum Profit in Job Scheduling)

Maximum Profit in Job Scheduling

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn jobs, each with start sis_i, end eie_i and reward viv_i for completing it. Select any subset of the jobs without overlap. A job may start exactly when the previous job ends. Maximize the total reward.

Input

The first line contains nn. The next nn lines each contain three integers si,ei,vis_i,e_i,v_i.

Output

Print the largest total reward.

Subtasks

  • Subtask 1 (20 points): 1≤n≤301\le n\le 30; 1≤si<ei≤1091\le s_i<e_i\le10^9; 1≤vi≤100001\le v_i\le 10000 (1≤i≤n1\le i\le n).
  • Subtask 2 (30 points): 1≤n≤20001\le n\le 2000; 1≤si<ei≤1091\le s_i<e_i\le10^9; 1≤vi≤100001\le v_i\le 10000 (1≤i≤n1\le i\le n).
  • Subtask 3 (50 points): 1≤n≤500001\le n\le 50000; 1≤si<ei≤1091\le s_i<e_i\le10^9; 1≤vi≤100001\le v_i\le 10000 (1≤i≤n1\le i\le n).

Examples

Example 1

Input

4
1 3 50
2 4 10
3 5 40
3 6 70

Output

120

Explanation

Choose jobs 1 and 4; equality at time 3 is allowed.

Example 2

Input

5
1 3 20
2 5 20
3 10 100
4 6 70
6 9 60

Output

150

Explanation

Jobs 1,4,5 yield 150.

Example 3

Input

3
1 2 5
1 3 6
1 4 4

Output

6

Explanation

All start together; job 2 gives 6.