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

#GL0000065. Tối thiểu tổng phiếu bầu (AtCoDeer and Election Report)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Tối thiểu tổng phiếu bầu (AtCoDeer and Election Report)

AtCoDeer and Election Report

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

Two election candidates receive votes over time; neither candidates vote count can ever decrease. AtCoder observes N reports in chronological order. Report i specifies the current ratio T_i:A_i of their positive vote counts, but not their actual counts. Find the minimum possible sum of the two vote counts at the final observation that is consistent with all reports.

Input

First line: N. The next N lines each contain T_i A_i.

Output

The minimum possible sum of votes at the last observation.

Subtasks

  • Subtask 1 (20 points): 1≤N≤101\le N\le 10, 1≤Ti,Ai≤101\le T_i,A_i\le 10, gcd⁡(Ti,Ai)=1\gcd(T_i,A_i)=1, answer ≤1018\le10^{18}.
  • Subtask 2 (30 points): 1≤N≤2001\le N\le 200, 1≤Ti,Ai≤1001\le T_i,A_i\le 100, gcd⁡(Ti,Ai)=1\gcd(T_i,A_i)=1, answer ≤1018\le10^{18}.
  • Subtask 3 (50 points): 1≤N≤10001\le N\le 1000, 1≤Ti,Ai≤10001\le T_i,A_i\le 1000, gcd⁡(Ti,Ai)=1\gcd(T_i,A_i)=1, answer ≤1018\le10^{18}.

Examples

Input

3
2 3
1 1
3 2

Output

10

Explanation

The sequence of vote counts (2,3), (3,3), (6,4) respects all ratios, never decreases either count, and finishes at total 10.