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

#FT0000197. Thời gian hoạt động tối thiểu (Minimum Time to Complete All Tasks)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Thời gian hoạt động tối thiểu (Minimum Time to Complete All Tasks)

Minimum Time to Complete All Tasks

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

A computer can process an unlimited number of tasks simultaneously while it is on. There are nn tasks. Task ii may use only integer time slots in its inclusive interval [si,ei][s_i,e_i] and needs at least did_i active time slots. Its active slots need not be consecutive.

You may decide for each integer time slot whether the computer is on. A slot in which the computer is on can simultaneously count towards every task whose interval contains that slot, but it contributes only one unit to the total on-time.

Find the minimum number of time slots for which the computer must be on to complete all tasks.

Input

  • The first line contains nn, the number of tasks.
  • Each of the next nn lines contains three integers si,ei,dis_i,e_i,d_i: the inclusive start time, inclusive end time, and required running seconds of task ii.

Output

Print one integer: the minimum total number of active time slots needed to satisfy every task.

Subtasks

  • Subtask 1 (20 points): 1≤n≤101\le n\le 10; 1≤si≤ei≤121\le s_i\le e_i\le 12; 1≤di≤ei−si+11\le d_i\le e_i-s_i+1 for all ii.

  • Subtask 2 (30 points): 1≤n≤3001\le n\le 300; 1≤si≤ei≤5001\le s_i\le e_i\le 500; 1≤di≤ei−si+11\le d_i\le e_i-s_i+1 for all ii.

  • Subtask 3 (50 points): 1≤n≤20001\le n\le 2000; 1≤si≤ei≤20001\le s_i\le e_i\le 2000; 1≤di≤ei−si+11\le d_i\le e_i-s_i+1 for all ii.

Examples

Input

3
2 3 1
4 5 1
1 5 2

Output

2

Explanation

Task 1 needs one active slot in [2,3][2,3], task 2 needs one in [4,5][4,5], and task 3 needs two in [1,5][1,5]. Turn the computer on at times 33 and 55. The first two tasks receive one slot each, and the third receives both slots. The first two intervals are disjoint, so a single active time slot cannot suffice; the minimum is 22.