Problem p03966 (python)

Fixed submission:
N=int(input())
 
X=[]
for i in range(N):
  a=input().split()
  X.append([int(a[0]),int(a[1])])
 
 
a=X[0][0]
b=X[0][1]
 
for i in range(1,N):
  c=X[i][0]
  d=X[i][1]
 

  x= (a + (c - 1)) // c
  y= (b + (d -1)) // d
  
  a1=1000000000000000000
  b1=1000000000000000000
  a2=1000000000000000000
  b2=1000000000000000000
  if d*x-b>=0:
    a1=c*x
    b1=d*x
 
  
  if c*y-a>=0:
    a2=c*y
    b2=d*y
  if (a1+b1)<(a2+b2):
    a=a1
    b=b1
  else:
    a=a2
    b=b2
 
  #print("i = "+str(i)+" a = "+str(a)+" b = "+str(b)+" a1 = "+str(a1)+" b1 = "+str(b1)+" a2 = "+str(a2)+" b2 = "+str(b2)+" c = "+str(c)+" d = "+str(d))
  
 
 
 
 
  
print(a+b)

Buggy submission (verdict: Runtime Error):
N=int(input())
 
X=[]
for i in range(N):
  a=input().split()
  X.append([int(a[0]),int(a[1])])
 
 
a=X[0][0]
b=X[0][1]
 
for i in range(1,N):
  c=X[i][0]
  d=X[i][1]
 

  x= (a + (c - 1)) // c
  y= (b + (d -1))) // d
  
  a1=1000000000000000000
  b1=1000000000000000000
  a2=1000000000000000000
  b2=1000000000000000000
  if d*x-b>=0:
    a1=c*x
    b1=d*x
 
  
  if c*y-a>=0:
    a2=c*y
    b2=d*y
  if (a1+b1)<(a2+b2):
    a=a1
    b=b1
  else:
    a=a2
    b=b2
 
  #print("i = "+str(i)+" a = "+str(a)+" b = "+str(b)+" a1 = "+str(a1)+" b1 = "+str(b1)+" a2 = "+str(a2)+" b2 = "+str(b2)+" c = "+str(c)+" d = "+str(d))
  
 
 
 
 
  
print(a+b)

Buggy submission (verdict: Runtime Error):
N=int(input())
 
X=[]
for i in range(N):
  a=input().split()
  X.append([int(a[0]),int(a[1])])
 
 
def ceil(x):
  if int(x)==x:
    return int(x)
  else:
    return int(x//1+1)
 
 
a=X[0][0]
b=X[0][1]
 
for i in range(1,N):
  c=X[i][0]
  d=X[i][1]
 
 
  #print("i = "+str(i)+" a = "+str(a)+" b = "+str(b)+" c = "+str(c)+" d = "+str(d))
  x=ceil(a/c)
  y=ceil(b/d)
  
 
  if d*x-b>=0:
    a=c*x
    b=d*x

    t=a+b

 
  
  if c*y-a>=0:

    if c*y+d*y<t:
      a=c*y
      b=d*y
 
 
  
print(a+b)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(100000)

N=int(input())

X=[]
for i in range(N):
  a=input().split()
  X.append([int(a[0]),int(a[1])])


def ceil(x):
  if int(x)==x:
    return int(x)
  else:
    return int(x//1+1)


a=X[0][0]
b=X[0][1]

def f(a,b,i):
  c=X[i+1][0]
  d=X[i+1][1]

  x=ceil(a/c)
  y=ceil(b/d)
  ans=1000000000000
  if i!=N-2:
    if c*y-a>=0:
      a=c*y
      b=d*y
      ans=min(ans,f(a,b,i+1))
    
    if d*x-b>=0:
      z=f(c*x,d*x,i+1)
      if z<ans:
        a=c*x
        b=d*x
        ans=z

  elif i==N-2:
    
    if c*y-a>=0:
      a=c*y
      b=d*y
      ans=min(ans,a+b)
    
    if d*x-b>=0:
      a=c*x
      b=d*x
      ans=min(ans,a+b)

  return ans



print(f(a,b,0))

Buggy submission (verdict: Runtime Error):
N=int(input())

X=[]
for i in range(N):
  a=input().split()
  X.append([int(a[0]),int(a[1])])


def ceil(x):
  if int(x)==x:
    return int(x)
  else:
    return int(x//1+1)


a=X[0][0]
b=X[0][1]

def f(a,b,i):
  c=X[i+1][0]
  d=X[i+1][1]

  x=ceil(a/c)
  y=ceil(b/d)
  ans=1000000000000
  if i!=N-2:
    if c*y-a>=0:
      a=c*y
      b=d*y
      ans=min(ans,f(a,b,i+1))
    
    if d*x-b>=0:
      a=c*x
      b=d*x
      ans=min(ans,f(a,b,i+1))

  elif i==N-2:
    
    if c*y-a>=0:
      a=c*y
      b=d*y
      ans=min(ans,a+b)
    
    if d*x-b>=0:
      a=c*x
      b=d*x
      ans=min(ans,a+b)

  return ans



print(f(a,b,0))

Buggy submission (verdict: Runtime Error):
N=int(input())

X=[]
for i in range(N):
  a=input().split()
  X.append([int(a[0]),int(a[1])])


def ceil(x):
  if int(x)==x:
    return int(x)
  else:
    return int(x//1+1)


a=X[0][0]
b=X[0][1]

def f(a,b,i):
  c=X[i+1][0]
  d=X[i+1][1]

  x=ceil(a/c)
  y=ceil(b/d)

  if i!=N-2:


    ans=100000000000
    if c*y-a>=0:
      a=c*y
      b=d*y
      ans=min(ans,f(a,b,i+1))
    
  
    if d*x-b>=0:
      a=c*x
      b=d*x
      ans=min(ans,f(a,b,i+1))
    
    return ans


  elif i==N-2:

    ans=100000000
    if c*y-a>=0:
      a=c*y
      b=d*y
      ans=min(ans,a+b)
    
  
    if d*x-b>=0:
      a=c*x
      b=d*x
      ans=min(ans,a+b)

    return ans



print(f(a,b,0))

Buggy submission (verdict: Runtime Error):
N=int(input())

X=[]
for i in range(N):
  a=input().split()
  X.append([int(a[0]),int(a[1])])


def ceil(x):
  if int(x)==x:
    return int(x)
  else:
    return int(x//1+1)


a=X[0][0]
b=X[0][1]

for i in range(1,N):
  c=X[i][0]
  d=X[i][1]
  a1=a*d/c
  if a1<b:
    y=math.ceil(a/c)*d-b
  else:
    y=math.ceil(a/c)*d-b

  while y<0:
    y+=d


  x=c*(b+y)//d-a
 
  #print("i = "+str(i)+" a = "+str(a)+" b = "+str(b)+" c = "+str(c)+" d = "+str(d)+" x = "+str(x)+" y = "+str(y))
  a=a+x
  b=b+y
print(a+b)