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

#GL0000329. Nhãn giá các gói kẹo (Candy Store)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Nhãn giá các gói kẹo (Candy Store)

Candy Store

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

A store sells nn candy types in the fixed order 1,2,…,n1,2,\ldots,n. For type ii, there are aia_i candies, each costing bib_i coins.

All candies must be packed, each pack containing candies of only one type. For type ii, choose a positive integer did_i candies per pack with ai mod di=0a_i\bmod d_i=0. One pack of this type costs ci=bidic_i=b_i d_i.

A single price tag can cover a contiguous segment of types l,l+1,…,rl,l+1,\ldots,r only when cl=cl+1=⋯=crc_l=c_{l+1}=\cdots=c_r. Choose the pack sizes to minimize the total number of tags. Nonadjacent segments may have the same price but still require different tags.

PHOJ input adaptation: Each test file contains one instance with nn candy types; the original contest's leading number of test cases tt is omitted.

Input

The first line contains nn. Each of the next nn lines contains ai,bia_i,b_i.

Output

Print the minimum number of price tags.

Subtasks

  • Subtask 1 (20 points): 1≤n≤81\le n\le8; 1≤ai,bi≤201\le a_i,b_i\le20.
  • Subtask 2 (30 points): 1≤n≤50001\le n\le5000; 1≤ai≤1061\le a_i\le10^6, 1≤bi≤10001\le b_i\le1000.
  • Subtask 3 (50 points): 1≤n≤2000001\le n\le200000; 1≤ai≤1091\le a_i\le10^9, 1≤bi≤100001\le b_i\le10000.

Examples

Example 1

Input

4
20 3
6 2
14 5
20 7

Output

2

Explanation

Choose d=[4,6,7,5]d=[4,6,7,5]; pack prices become [12,12,35,35][12,12,35,35]. One tag covers types 11–22, another covers 33–44. A single price for all four types is impossible.

Example 2

Input

3
444 5
2002 10
2020 2

Output

1

Explanation

Choose d=[4,2,10]d=[4,2,10], so all pack prices equal 2020: 5⋅4=10⋅2=2⋅10=205\cdot4=10\cdot2=2\cdot10=20. Every did_i divides aia_i.

Example 3

Input

5
7 7
6 5
15 2
10 3
7 7

Output

3

Explanation

Use segments [1][1], [2,3,4][2,3,4], [5][5]. The middle pack price can be 3030, using d2=6,d3=15,d4=10d_2=6,d_3=15,d_4=10; the ends cost 77. The end segments are not contiguous and thus require separate tags.