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

#BF0000034. Dãy số nguyên theo các đoạn (Sequence)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Dãy số nguyên theo các đoạn (Sequence)

Sequence

Source: Luogu

Version: Phuoc Hung OJ Extended

Problem Statement

Find the minimum length of a sequence of distinct integers such that at least CiC_i elements fall inside each given closed interval [Li,Ri][L_i,R_i].

Input

First line NN, then NN triples Li,Ri,CiL_i,R_i,C_i.

Output

Print the minimum sequence length.

Subtasks

  • Subtask 1 (20 points): $1\le N\le20; 0\le L_i\le R_i\le30; 1\le C_i\le R_i-L_i+1$.
  • Subtask 2 (30 points): $1\le N\le200; 0\le L_i\le R_i\le300; 1\le C_i\le R_i-L_i+1$.
  • Subtask 3 (50 points): $1\le N\le1000; 0\le L_i\le R_i\le1000; 1\le C_i\le R_i-L_i+1$.

Examples

Input

4
4 5 1
6 10 3
7 10 3
5 6 1

Output

4

Explanation

The set {5,7,8,9}\{5,7,8,9\} satisfies every interval requirement: the intervals [6,10][6,10] and [7,10][7,10] contain three selected numbers; [4,5][4,5] and [5,6][5,6] each contain one. Thus an answer of 44 is possible and optimal.