Problem:
Score : 300 points

Problem Statement

AtCoDeer the deer is seeing a quick report of election results on TV.
Two candidates are standing for the election: Takahashi and Aoki.
The report shows the ratio of the current numbers of votes the two candidates have obtained, but not the actual numbers of votes.
AtCoDeer has checked the report N times, and when he checked it for the i-th (1≦i≦N) time, the ratio was T_i:A_i.
It is known that each candidate had at least one vote when he checked the report for the first time.

Find the minimum possible total number of votes obtained by the two candidates when he checked the report for the N-th time.
It can be assumed that the number of votes obtained by each candidate never decreases.

Constraints

1≦N≦1000

1≦T_i,A_i≦1000 (1≦i≦N)

T_i and A_i (1≦i≦N) are coprime.

It is guaranteed that the correct answer is at most 10^{18}.

Input

The input is given from Standard Input in the following format:

N
T_1 A_1
T_2 A_2
:
T_N A_N

Output

Print the minimum possible total number of votes obtained by Takahashi and Aoki when AtCoDeer checked the report for the N-th time.

Sample Input 1

3
2 3
1 1
3 2

Sample Output 1

10

When the numbers of votes obtained by the two candidates change as 2,3 → 3,3 → 6,4, the total number of votes at the end is 10, which is the minimum possible number.

Sample Input 2

4
1 1
1 1
1 5
1 100

Sample Output 2

101

It is possible that neither candidate obtained a vote between the moment when he checked the report, and the moment when he checked it for the next time.

Sample Input 3

5
3 10
48 17
31 199
231 23
3 2

Sample Output 3

6930

Solution in C (Clang 3.8.0) (judge verdict: Wrong Answer)

#include <stdio.h>
#include <string.h>
#define FOR(i, a, b) for(i = a; i < b; ++i)
#define REP(i, n) FOR(i, 0, n)

#define N 100000

int result(char atc, char topc){
    if(atc == 'p' && topc == 'g'){
        return 1;
    }else if(atc == 'g' && topc == 'p'){
        return -1;
    }else{
        return 0;
    }
}

int max(int a, int b){
    return a > b? a : b;
}

int rock_paper(unsigned int n, unsigned int p, unsigned int len, int dp[len + 1][len + 1], char S[N]){
    if(dp[n][p] != -N){
        return dp[n][p];
    }
    if(n == len){
        return dp[n][p] = 0;
    }

    int rock = rock_paper(n + 1, p + 1, len, dp, S) + result('g', S[n]);
    if(p == 0){
        return dp[n][p] = rock;
    }else{
        int paper = rock_paper(n + 1, p - 1, len, dp, S) + result('p', S[n]);
        return dp[n][p] = max(rock, paper);
    }
}

int main(){
    char S[N];
    scanf("%s", S);
    unsigned int len = strlen(S);
    int dp[len + 1][len + 1];
    unsigned int i, j;

    if(len == 1){
        if(S[0] == 'p'){
            printf("-1\n");
        }else{
            printf("0\n");
        }
    }
    else{
        REP(i, len + 1){
            REP(j, len + 1){
                dp[i][j] = -N;
            }
        }
        printf("%d\n", rock_paper(0, 0, len, dp, S));
        
        REP(i, len + 1){
            REP(j, len + 1){
                printf("%d ", dp[i][j]);
            }
            printf("\n");
        }

    }
    return 0;
}

/*
nターン目以降を最適化した時の最大化された点数：dp[n][p]
n:何ターン目か, p:パーを出せる回数の上限
*/