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

#QHD0000075. Xếp chồng các hộp (Boxes)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Xếp chồng các hộp (Boxes)

Boxes

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

There are NN equal-sized boxes, numbered 11 through NN. Box ii has weight wiw_i and load capacity lil_i. The total weight of every box above box ii, excluding its own weight, must not exceed lil_i.

Select any subset of boxes and arrange them into one vertical stack, with at most one box directly above any box. The serial-number rule forbids placing a lower-numbered box on a higher-numbered box. Consequently, indices must be strictly increasing from the bottom to the top of the stack. For example, using boxes 1,3,51,3,5 requires the bottom-to-top order 1,3,51,3,5.

Each selected box must support the total weight above it, not just the adjacent box. Find the maximum possible number of boxes in a valid stack. The selected indices do not need to be printed.

Input

The first line contains NN. Each of the next NN lines contains the integers wi,liw_i,l_i for box ii.

Output

Print the maximum number of boxes that can be placed in one valid stack.

Subtasks

  • Subtask 1 (20 points): 1≤N≤141\le N\le14; 0≤wi,li≤300\le w_i,l_i\le30; indices increase strictly from bottom to top.

  • Subtask 2 (30 points): 1≤N≤1501\le N\le150; 0≤wi,li≤3000\le w_i,l_i\le300; indices increase strictly from bottom to top.

  • Subtask 3 (50 points): 1≤N≤10001\le N\le1000; 0≤wi,li≤30000\le w_i,l_i\le3000; indices increase strictly from bottom to top.

Examples

Example 1

Input

5
19 15
7 13
5 7
6 8
1 2

Output

4

Explanation

Place boxes 1,2,3,51,2,3,5 from bottom to top. The loads above them are 7+5+1=13≤157+5+1=13\le15, 5+1=6≤135+1=6\le13, 1≤71\le7, and 0≤20\le2. Four boxes are valid, whereas five are not.

Example 2

Input

3
1 0
1 0
1 0

Output

1

Explanation

Every box has zero load capacity while each weighs one unit, so no box can support another; the answer is one.