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

#FT0000092. Nhà máy vòng tháp Hà Nội (Hanoi Factory)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Nhà máy vòng tháp Hà Nội (Hanoi Factory)

Hanoi Factory

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

A factory has nn manufactured rings in stock. Ring ii has inner radius aia_i, outer radius bib_i, and height hih_i. Its inner radius is strictly smaller than its outer radius.

The factory wishes to build the tallest possible tower using some of these rings, each at most once. If ring jj is placed immediately above ring ii, both conditions must hold:

  • The upper ring's outer radius cannot exceed that of the lower ring: bj≤bib_j\le b_i.
  • The upper ring must not fall into the lower ring's hole: bj>aib_j>a_i.

The tower's height is the sum of the chosen rings' heights. A tower of one ring is allowed. Find the maximum achievable height.

Input

The first line contains the integer nn. Each of the next nn lines contains three integers aia_i, bib_i, and hih_i for ring ii.

Output

Print one integer: the maximum total height of a valid tower.

Subtasks

  • Subtask 1 (20 points): 1≤n≤101\le n\le10; 1≤ai<bi≤1091\le a_i<b_i\le10^9; 1≤hi≤1091\le h_i\le10^9 for every ii.
  • Subtask 2 (30 points): 1≤n≤20001\le n\le2000; 1≤ai<bi≤1091\le a_i<b_i\le10^9; 1≤hi≤1091\le h_i\le10^9 for every ii.
  • Subtask 3 (50 points): 1≤n≤1051\le n\le10^5; 1≤ai<bi≤1091\le a_i<b_i\le10^9; 1≤hi≤1091\le h_i\le10^9 for every ii.

Examples

Example 1

Input

3
1 5 1
2 6 2
3 7 3

Output

6

Explanation

The rings have heights 1,2,31,2,3. Place ring 33 (outer radius 77) at the bottom, ring 22 (66) in the middle, and ring 11 (55) on top. The adjacent pairs satisfy 6≤76\le7, 6>36>3, 5≤65\le6, and 5>25>2. The total height is 3+2+1=63+2+1=6. Since this uses every ring, no tower can be taller.

Example 2

Input

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

Output

4

Explanation

Rings 22 and 11 can form a tower: b1=2≤3=b2b_1=2\le3=b_2 and b1=2>a2=1b_1=2>a_2=1. Their height is 3+1=43+1=4. Rings 44 and 33 also form a valid tower, but its height is only 1+2=31+2=3. The two groups cannot be stacked together while maintaining the radius rules. Therefore, the answer is 44.