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

#BS0000467. Hai sự kiện không giao nhau tốt nhất (Two Best Non-Overlapping Events)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Hai sự kiện không giao nhau tốt nhất (Two Best Non-Overlapping Events)

Two Best Non-Overlapping Events

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn events, each with start sis_i, end eie_i and reward viv_i for completing it. Select at most two events without overlap. Both endpoints are inclusive, so the next start must be strictly greater than the previous end. 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): 2≤n≤302\le n\le 30; 1≤si≤ei≤1091\le s_i\le e_i\le10^9; 1≤vi≤10000001\le v_i\le 1000000 (1≤i≤n1\le i\le n).
  • Subtask 2 (30 points): 2≤n≤20002\le n\le 2000; 1≤si≤ei≤1091\le s_i\le e_i\le10^9; 1≤vi≤10000001\le v_i\le 1000000 (1≤i≤n1\le i\le n).
  • Subtask 3 (50 points): 2≤n≤1000002\le n\le 100000; 1≤si≤ei≤1091\le s_i\le e_i\le10^9; 1≤vi≤10000001\le v_i\le 1000000 (1≤i≤n1\le i\le n).

Examples

Example 1

Input

3
1 3 2
4 5 2
2 4 3

Output

4

Explanation

Events 1 and 2 yield 4; event 3 overlaps both.

Example 2

Input

3
1 3 2
4 5 2
1 5 5

Output

5

Explanation

Event 3 alone is better than 2+2.

Example 3

Input

3
1 5 3
1 5 1
6 6 5

Output

8

Explanation

Events 1 and 3 yield 8.