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.
Examples
Input
3
2 3
1 1
3 2
Output
10
Input
4
1 1
1 1
1 5
1 100
Output
101
Input
5
3 10
48 17
31 199
231 23
3 2
Output
6930
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to understand problem: We have sequential reports. At each step i, we know the ratio of votes (T_i : A_i) at that time. Initially (before first report) we have some unknown numbers of votes (T0, A0) but we know that at first report the ratio is T_1:A_1 and both have at least one vote when checked for the first time. Actually statement: "It is known that each candidate had at least one vote when he checked the report for the first time." So at step 1, we have (x1, y1) such that x1 : y1 = T1 : A1 and x1>=1, y1>=1. The numbers of votes never decrease. So each subsequent step, we have new numbers (x_i, y_i) such that x_i >= x_{i-1}, y_i >= y_{i-1}, and x_i : y_i = T_i : A_i (in reduced form, they are coprime). We need to find minimum possible total x_N + y_N after N reports.
Essentially we need to find minimal nonnegative integer increments to satisfy ratios while keeping monotonic nondecreasing.
We can think: start with some base (x0=0, y0=0) but first check ensures (x1,y1) satisfy ratio T1:A1. Since votes never decrease, we can think of building sequentially.
At step i, we need to find the smallest (x_i, y_i) such that:
x_i >= x_{i-1}, y_i >= y_{i-1}
And we want to minimize final sum x_N + y_N.
This is a typical greedy: compute minimal multiplier k_i that satisfies the lower bounds. That is, given previous (x_{i-1}, y_{i-1}) and ratio (T_i, A_i), we need the smallest integer k >= 1 such that kT_i >= x_{i-1} and kA_i >= y_{i-1}. Then set (x_i, y_i) = (kT_i, kA_i). This yields the minimal possible numbers at step i given previous numbers. And because we want to minimize final sum, it's optimal to take minimal at each step (since any later steps only increase). So just simulate from i=1 to N.
But careful: Is this always optimal? Usually yes, because at step i, making (x_i,y_i) larger than necessary could only make later constraints tighter and possibly increase final sum; but could it allow a smaller later sum? Possibly if a larger earlier choice leads to a smaller multiplier later? Actually think: The objective is to minimize final sum, not intermediate ones. By picking minimal feasible (x_i,y_i) at each step, we are minimizing the lower bound for future steps. Since future steps require to satisfy ratios and monotonicity, having smaller lower bounds cannot force a larger final sum; it can only allow smaller final sums. However, we need to ensure that picking minimal at each step doesn't lead to infeasibility? But it's always feasible because we can always increase later. So it's optimal.
But there is nuance: The minimal k_i from conditions may yield values that satisfy the ratio, but we also have condition that both candidates had at least one vote when first report checked. That's automatically satisfied if we take k1>=1 because T1,A1>=1, so x1>=1,y1>=1. But could it be that the minimal k1 computed from x0=0,y0=0 is k1=0? But we need positive votes, so we need k1 >= 1. Since constraints x_{0}=0, y_{0}=0, condition kT1>=0 and kA1>=0 holds for k=0 but we need at least one vote. However, we require both have at least one vote, so we need k1 >= 1. So we start with previous (0,0) but enforce k1 >= 1. This is consistent because from (0,0) the minimal k that satisfies x>=0,y>=0 is 0, but we need positive, so we set k1=1? Wait: Actually at step 1, we don't have previous numbers? The votes are numbers after some increments? We assume before first report there were some votes; we only know that when first report checked, ratio is T1:A1 and each had at least one vote. There is no previous step constraint other than that the numbers cannot decrease from before first report? Actually votes never decrease over time, so at the moment of first report, numbers (x1,y1) are the current counts. There is no prior reported numbers, but there was a time before first report, and we don't know initial votes. However, we only care about the numbers at report times. The monotonicity constraint applies between report times: from time of i-1 report to i report, votes only increase. So for i=1, there is no constraint from a previous report. So we only need (x1,y1) such that x1:y1 = T1:A1 and x1>=1, y1>=1. So we can choose any positive multiple. So we should also minimize (x1,y1) to help later minimization. But minimal such pair is (T1,A1) itself, i.e., k1=1, because T1 and A1 are positive integers. However, is there any hidden constraint that numbers might have been higher before? No, because they could have been exactly that. So we can set (x1,y1) = (T1,A1). But could it be that we need to consider that before first report, there might have been some votes already? But if we set (x1,y1) as (T1,A1), that's consistent with "each had at least one vote". There is no lower bound other than positivity. So indeed we can start with (x1,y1) = (T1,A1). However, what if later we need to be able to increase from (x1,y1) to (x2,y2) with ratio T2:A2? Starting with minimal (T1,A1) gives the most flexibility and likely minimal final sum. So we can set (x1,y1) = (T1,A1). However, is it always minimal? Possibly if T1 and A1 are not both 1? Then k1=1 gives minimal sum.
But we must check if there is any scenario where choosing a larger k1 could reduce final sum. Intuitively, no: larger x1,y1 can only force larger numbers later, not smaller. Because future numbers must be at least as large, and ratio constraints are fixed. So minimal k1 is best.
Thus algorithm: start with (x,y) = (T1, A1). For i from 2 to N: given (x,y) from previous step, we need to find smallest positive integer k such that kT_i >= x and kA_i >= y. Then set (x,y) = (kT_i, kA_i). After loop, output x+y.
We need to handle large numbers up to 1e18, so use 64-bit integers.
But careful: The ratio T_i:A_i is given as coprime numbers. But we are using multiples k. So we compute k = max(ceil(x/T_i), ceil(y/A_i)). However, x and y can be large; we must compute ceil division without overflow. Use (x + T_i - 1) / T_i, but ensure use of long long.
Edge case: If T_i=0? But constraints T_i>=1, so fine.
Double-check with examples:
Example1:
N=3
2 3 -> start (2,3)
Next 1 1: need k>=max(ceil(2/1)=2, ceil(3/1)=3) => k=3 => (3,3) (since 1:1 ratio)
Next 3 2: need k>=max(ceil(3/3)=1, ceil(3/2)=2) => k=2 => (6,4). Sum=10, matches.
Example2:
(1,1) start (1,1)
Next (1,1): k=max(ceil(1/1)=1, ceil(1/1)=1)=1 => (1,1)
Next (1,5): k=max(ceil(1/1)=1, ceil(1/5)=1)=1? ceil(1/5)=1 because integer division ceil? Actually 1/5=0.2, ceil=1. So k=1 => (1,5) but check: y=5 >= previous y=1, x=1 >= previous x=1 ok. Sum=6? Wait next step: (1,100): need k >= max(ceil(1/1)=1, ceil(5/100)=1) since ceil(5/100)=1? Actually 5/100=0.05 ceil=1 => k=1 => (1,100) sum=101 final. That's output. So it's correct.
But wait: At step 3 (1,5) we got (1,5). Is that minimal? Could we have had (2,10) with k=2? That would be larger. So yes minimal.
Example3: Let's simulate quickly to verify.
5
3 10 -> start (3,10)
Next 48 17: compute k1 = ceil(3/48)=1? Actually 3/48=0.0625 ceil=1; ceil(10/17)=1? 10/17=0.588 ceil=1; k=1 => (48,17) But then check: x=48 >= 3 yes; y=17 >=10 yes. So (48,17). Wait (48,17) sum=65.
Next 31 199: need k >= max(ceil(48/31)=2 (since 48/31≈1.548, ceil=2), ceil(17/199)=1) => k=2 => (62,398) because 231=62, 2199=398. sum=460.
Next 231 23: compute k >= max(ceil(62/231)=1? 62/231 <1 so ceil=1, ceil(398/23)= ceil(398/23=17.304...)=18) => k=18 => (23118=4158, 2318=414) Actually 2318=414. So (4158,414). But check: 4158>=62, 414>=398? Yes. Sum=4572? Wait 4158+414=4572.
Next 3 2: need k >= max(ceil(4158/3)=1386 (since 4158/3=1386 exactly), ceil(414/2)=207) => k=1386 => (4158,2772)? Actually 13863=4158, 1386*2=2772. So (4158,2772) sum=6930. matches output.
So algorithm works.
Implementation details: Use long long. For each i, compute needed k = max( (x + T_i - 1) / T_i , (y + A_i - 1) / A_i ). Then set x = k * T_i, y = k * A_i.
Edge Cases: Ensure that the first step uses x = T1, y = A1 (with k=1). That's consistent with the formula if we set previous (0,0) but then ceil(0/T1)=0, ceil(0/A1)=0, max=0 but need k>=1, so we can manually set first or handle by initializing x=0,y=0 but force k=1 for first step.
Alternatively, we can start with (0,0) and for i=1..N, compute k = max( ceil(x/T_i), ceil(y/A_i) ), but if that k=0, set k=1. Then update. That works as well.
But careful: In case (x,y) = (0,0) and T_i, A_i positive, ceil division yields 0, then k=0, but we need k>=1 to have positive votes. So we set k = max(1, ... )? However, what about later steps? For i>1, if both x and y are zero? But after first step they become positive, so later k>= something >=1 anyway.
Better to handle first step separately.
Complexity O(N). N up to 1000 trivial.
Potential pitfalls: Overflow in multiplication when k * T_i might exceed 1e18? The problem guarantees answer at most 1e18, but intermediate values might be up to that. Using 64-bit (signed) up to 9e18, but careful: k can be up to about 1e18 / min(T_i,A_i). So product could be up to 1e18, well within 2^63-1 (~9.22e18). So safe.
But we must ensure multiplication does not overflow beyond 1e19? Since max answer 1e18, worst-case intermediate maybe similar. So safe.
Thus solution straightforward.
But wait: Is there any scenario where choosing minimal k at each step might be suboptimal for final sum? Let's think more thoroughly. The problem is to minimize final sum, not necessarily intermediate sums. Suppose we have two steps. Step1 ratio (a,b). Step2 ratio (c,d). Starting with (x1,y1) = k1*(a,b) where k1>=1. Then step2 we need (x2,y2) = k2*(c,d) with k2 such that k2c >= k1a, k2d >= k1b. The total sum = k2*(c+d). For fixed k1, the minimal k2 is max(ceil(k1a/c), ceil(k1b/d)). So total = (c+d) * max(ceil(k1a/c), ceil(k1b/d)). This is a function of k1. Possibly increasing k1 might reduce the max factor because it could make the two ceilings more balanced? Actually consider example: a=1,b=100, c=99, d=1. Suppose k1=1 gives (1,100). Then k2 = max(ceil(1/99)=1, ceil(100/1)=100) => 100 => total = 100*(100)=10000? Actually (c+d)=100, so total=10000. If we take k1=99: (99,9900). Then k2 = max(ceil(99/99)=1, ceil(9900/1)=9900) => 9900 => total=9900*100=990000, worse. Not better.
Maybe scenario where increasing k1 reduces the second ceil because the ratio of a to c and b to d might be such that the larger ceil decreases? Example: a=3,b=5; c=4,d=7. For k1=1: (3,5). k2 = max(ceil(3/4)=1, ceil(5/7)=1) =>1 => total = (4+7)1=11. For k1=2: (6,10). k2 = max(ceil(6/4)=2, ceil(10/7)=2) =>2 => total =112=22. Larger.
But what if one of the ceilings is high while the other is low, and increasing k1 could make both roughly equal but maybe reduce the max? The max of two numbers cannot decrease when you increase the arguments (since ceilings are monotonic). If you increase k1, both ceil(k1a/c) and ceil(k1b/d) are non-decreasing (strictly non-decreasing eventually). So the max is non-decreasing. However, there might be a case where one of them jumps down due to integer rounding? Ceil is monotonic: if k1 increases, the argument increases, so ceil never decreases. So for fixed ratios, the minimal k2 is non-decreasing in k1. Therefore the total sum = (c+d)*k2 is non-decreasing in k1. So minimal total sum is achieved at minimal k1. This reasoning extends to multiple steps by induction: at each step, choosing minimal feasible k_i yields minimal possible (x_i,y_i) in the Pareto sense (i.e., both coordinates minimal). Since future constraints require larger coordinates, any larger (x_i,y_i) cannot lead to a smaller final sum. So greedy is optimal.
We must also consider that the ratio at step i is given as coprime, but we are using multiples. That is fine.
Thus the solution stands.
Implementation:
Read N.
Initialize long long x=0, y=0.
For i from 1 to N:
read T, A.
compute k = max( (x + T - 1) / T, (y + A - 1) / A );
if (k == 0) k = 1; // for first iteration where x=y=0.
x = k * T;
y = k * A;
After loop, print x+y.
But careful: When x=y=0, both ceil divisions yield 0, so k=0; we set to 1. That works.
But what if T or A is 0? Not possible.
One subtlety: For i>1, could k be 0? No, because x,y >0 after first step, so k >=1.
Thus solution is done.
But some may prefer to set x = T1, y = A1 then loop from 2. Both fine.
Check with given examples.
Let's test with first: start x=0,y=0.
i=1: T=2,A=3; k = max((0+2-1)/2 = (1)/2=0 integer division? Actually (0+2-1)/2 = 1/2 = 0 in integer division floor. But we need ceil(0/2)=0. However (x + T - 1)/T gives ceil for positive x? The standard ceil division for non-negative x is (x + T -1)//T. For x=0, (0+T-1)//T = (1)//2 = 0, correct. So k=0, then we set k=1. Then x=2,y=3.
i=2: T=1,A=1; k = max((2+1-1)/1=2/1=2, (3+1-1)/1=3/1=3) =>3; x=3,y=3.
i=3: T=3,A=2; k = max((3+3-1)/3=5/3=1, (3+2-1)/2=4/2=2) =>2; x=6,y=4; sum=10.
Works.
Edge Cases: Suppose first ratio is (1,1000) and second ratio is (1000,1). Starting minimal (1,1000). Then second step: k = max(ceil(1/1000)=1, ceil(1000/1)=1000) =>1000; (1000000,1000) sum=1001000. That seems huge but it's minimal? Could we have chosen larger first step to reduce total? If we set first step as (1000,1000000) by k1=1000, then second step k2 = max(ceil(1000/1000)=1, ceil(1000000/1)=1000000) =>1000000, total = 1001*1000000 = 1.001e9, larger. So minimal is better.
But is there any scenario where taking a larger first step might allow k2 to be smaller? The second step's k2 is determined by the max of two ceilings. Since both ceilings are monotonic, increasing k1 cannot reduce either ceiling. The only possible reduction is if one ceiling is dominant and the other is not, but increasing k1 may cause the dominant one to increase as well. However, could the dominant one decrease? No, because it's monotonic. So greedy holds.
Thus solution is correct.
Potential pitfalls: When T_i and A_i are coprime but the multiplier k may cause them to be not reduced? We don't need to reduce.
Implementation in C++:
#include
#include
using namespace std;
using ll = long long;
int main() {
int N;
cin >> N;
ll x = 0, y = 0;
for (int i = 0; i < N; i++) {
ll T, A;
cin >> T >> A;
ll k = max((x + T - 1) / T, (y + A - 1) / A);
if (k == 0) k = 1;
x = k * T;
y = k * A;
}
cout << x + y << endl;
return 0;
}
But careful: (x + T - 1) / T might overflow if x is near 1e18 and T=1? But x+T-1 <= ~1e18+1000, within 2^63. So fine.
Alternatively, use __int128 if concerned, but not needed.
Now, confirm constraints: N up to 1000, each T_i,A_i up to 1000, so the computation is trivial.
But there's nuance: The problem states: "It is known that each candidate had at least one vote when he checked the report for the first time." That is satisfied with k1=1. Could there be a scenario where we need to set k1 > 1 to satisfy later constraints? No, because we can always increase later. So minimal is best.
Thus answer.
We should also consider possibility of using rational numbers? No.
Thus final answer.
But we must also consider that the ratio is given as coprime. So the minimal representation is the ratio itself. In our method we multiply by k, which could produce numbers that are not necessarily in reduced form but that's fine.
All set.
Now produce final answer in the required format.
Make sure to use long long (64-bit). Possibly use unsigned long long for safety, but signed is fine.
Now I'll produce the final response: explanation and solution code.
But the problem is from AtCoder (ABC 045 C? Actually it's ABC 046 C? Or ARC 062? Actually it's AtCoder problem "AtCoDeer and Election Report" from ABC 046 C / ARC 062 A? Let's recall. But anyway.
I'll write the solution in Python as well? The user didn't specify language; but typical we output explanation and code. I'll provide Python code as it's simple.
Time to write final answer. We need to find the smallest possible total votes at the N-th report, given that the votes never decrease and at each report the ratio is known. The optimal strategy is to take the smallest possible numbers satisfying each ratio while respecting the non-decreasing condition. This leads to a greedy algorithm: start with the first ratio as (T1, A1). For each subsequent ratio (Ti, Ai), compute the smallest integer k such that kTi ≥ current Takahashi votes and kAi ≥ current Aoki votes. Then update the votes to (kTi, kAi). Finally, output the sum after processing all reports.
The reasoning: at each step, picking the minimal feasible numbers cannot worsen the future possibilities because any larger choice would only force larger numbers later.
Complexities: O(N) time, O(1) extra space. Numbers stay within 10¹⁸, so 64‑bit integers are sufficient.
Below are implementations in C++ and Python.
cpp
#include <iostream>
#include <algorithm>
using namespace std;
using ll = long long;
int main() {
int N;
cin >> N;
ll x = 0, y = 0;
for (int i = 0; i < N; ++i) {
ll T, A;
cin >> T >> A;
ll k = max((x + T - 1) / T, (y + A - 1) / A);
if (k == 0) k = 1; // first report: need at least one vote each
x = k * T;
y = k * A;
}
cout << x + y << endl;
return 0;
}
python
def main():
import sys
input = sys.stdin.readline
N = int(input())
x = y = 0
for _ in range(N):
T, A = map(int, input().split())
k = max((x + T - 1) // T if T else 0,
(y + A - 1) // A if A else 0)
if k == 0:
k = 1
x = k * T
y = k * A
print(x + y)
if __name__ == "__main__":
main()