Problem:
 Score : 500 points    Problem Statement An adult game master and N children are playing a game on an ice rink.
The game consists of K rounds.
In the i -th round, the game master announces:   Form groups consisting of A_i children each!   Then the children who are still in the game form as many groups of A_i children as possible.
One child may belong to at most one group.
Those who are left without a group leave the game. The others proceed to the next round.
Note that it's possible that nobody leaves the game in some round.  In the end, after the K -th round, there are exactly two children left, and they are declared the winners.  You have heard the values of A_1 , A_2 , ..., A_K . You don't know N , but you want to estimate it.  Find the smallest and the largest possible number of children in the game before the start, or determine that no valid values of N exist.      Constraints  1 \leq K \leq 10^5  2 \leq A_i \leq 10^9  All input values are integers.         Input Input is given from Standard Input in the following format:  K  A_1  A_2  ...  A_K       Output Print two integers representing the smallest and the largest possible value of N , respectively,
or a single integer -1 if the described situation is impossible.        Sample Input 1 4
3 4 3 2      Sample Output 1 6 8  For example, if the game starts with 6 children, then it proceeds as follows:   In the first round, 6 children form 2 groups of 3 children, and nobody leaves the game.  In the second round, 6 children form 1 group of 4 children, and 2 children leave the game.  In the third round, 4 children form 1 group of 3 children, and 1 child leaves the game.  In the fourth round, 3 children form 1 group of 2 children, and 1 child leaves the game.   The last 2 children are declared the winners.       Sample Input 2 5
3 4 100 3 2      Sample Output 2 -1  This situation is impossible.
In particular, if the game starts with less than 100 children, everyone leaves after the third round.       Sample Input 3 10
2 2 2 2 2 2 2 2 2 2      Sample Output 3 2 3

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(10000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(K, A):
    L = H = 2
    for a in A[::-1]:
        if math.ceil(L/a)*a > H:
            return -1
        L = math.ceil(L/a)*a
        H = math.floor(H/a)*a + a-1

    return ' '.join(map(str, [L, H]))


def main():
    K = read_int()
    A = read_int_n()
    print(slv(K, A))


if __name__ == '__main__':
    main()

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(10000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(K, A):
    L = H = 2
    for a in A[::-1]:
        if math.ceil(L/a)*a > H:
            return -1
        L = math.ceil(L/a)*a
        H = math.floor(H/a)*a + a-1

    return ' '.join(map(str, [L, H]))


def main():
    K = read_int()
    A = read_int_n()
    print(slv(K, A))


if __name__ == '__main__':
    main()

Solution (Python):


import sys

k = int(input())
a = list(map(int,input().split()))[::-1] 
x=2
y=2
if a[0]!=2:
    print(-1)
    sys.exit(0)

for t in a:
    x=((x-1)//t+1)*t
    y=y//t*t+t-1
    

if x > y:
    print(-1)
    sys.exit(0)
print(str(x)+" " + str(y))

Solution (Python):
import math
k = int(input())
a = list(map(int,input().split()))
ar = list(reversed(a))
syou = 0
for i in ar:
  if syou > i:
    l = math.ceil(syou/i)
    syou = l * i
  else:
    syou = i
dai = 0
for i in ar:
  if dai > i:
    l = math.ceil(dai/i)
    if dai == l * i:
      dai = l* i + i -1
    else:
      dai = max(l * i -1,dai)
  else:
    dai = i + i -1
kaku = syou + 0
for i in a:
  kaku = (kaku//i) *i
if kaku ==2:
  print(syou,dai)
else:
  print(-1)

Solution (Python):
K = int(input())
A = list(map(int, input().split()))
mi = 2
ma = 2
for i in range(K-1, -1, -1):
    a = A[i]
    if ma < a:
        print(-1)
        exit()
    mi = ((mi-1)//a+1)*a
    ma = ma//a*a+a-1
    if mi > ma:
        print(-1)
        exit()
print(mi, ma)

Solution (Python):
K = int(input())
A = [int(i) for i in input().split()]

N_min = 2
N_max = 2
for a in A[K::-1]:
	if N_min + (a - N_min) % a > N_max:
		print(-1)
		exit(0)
	else:
		
		N_min += (a - N_min) % a
		
		N_max += (a - N_max - 1) % a
print(N_min, N_max)

Solution (Python):
k = int(input())
a = [int(i) for i in input().split()]
a,flag = a[::-1],True
if a[0]!=2: flag = False
else:
	x,y = 2,3
	for i in range(1,k):
		x2 = x-x%a[i]+min(x%a[i],1)*a[i]
		if x2>y:
			flag = False
			break
		else: x,y = x2,y-y%a[i]+a[i]-1
if flag: print(x,y)
else: print(-1)

Solution (Python):
import math

k = int(input())
al = [int(x) for x in input().split()]

pmax = 2
pmin = 2
for a in reversed(al):
    pmin = a*math.ceil(pmin/a)
    pmax = a*math.floor(pmax/a) + a -1

if pmin > pmax:
    print(-1)
else:
    print(pmin, pmax)

Solution (Python):
import math
n = int(input())
A = list(map(int,input().split()))

L = 2
R = 2
k = 0
for i in range(n):
    if L//A[n-1-i] == R//A[n-1-i] and L % A[n-1-i] != 0:
        k = 1
        break
    else:
        L = (math.ceil(L/A[n-1-i]))*A[n-1-i]
        R = (R//A[n-1-i]+1) * A[n-1-i] - 1
print(L,R) if k == 0 else print("-1")

Solution (Python):


import sys, math, copy




HUGE = 2147483647
HUGEL = 9223372036854775807
ABC = "abcdefghijklmnopqrstuvwxyz"

def get_max_min_mod(a, minp, maxp):
    if minp % a == 0:
        minp_new = (minp // a) * a
    else:
        minp_new = (minp // a + 1) * a
    maxp_new = (maxp // a) * a
    return minp_new, maxp_new

def main():
    k = int(input())
    ak = list(map(int, reversed(input().split())))
    assert len(ak) == k
    minp = maxp = 2
    for j in range(k):
        minp, maxp = get_max_min_mod(ak[j], minp, maxp)
        if minp > maxp:
            print(-1)
            sys.exit(0)
        maxp += ak[j] - 1
    print(minp, maxp)

main()

Solution (Python):
K = int(raw_input())
A = map(int, raw_input().split())

ma = mi = 2
for a in reversed(A):
    mi = (mi + a - 1) // a * a
    ma = (ma ) // a * a + a - 1
if mi <= ma:
    print mi, ma
else:
    print -1

Solution (Python):
K = int(input())
A = list(map(int, input().split()))

lo = 2
hi = 10**18


def check1(people):
    for a in A:
        people = (people // a) * a
    if people > 2:
        return False
    else:
        return True


def check2(people):
    for a in A:
        people = (people // a) * a
    if people < 2:
        return False
    else:
        return True


for i in range(100):
    center = (lo + hi) // 2
    if check1(center):
        lo = center
    else:
        hi = center

Mz = center

lo = 2
hi = 10**18

for i in range(100):
    center = (lo + hi) // 2
    if center > Mz:
        hi = center
        continue
    if check2(center):
        hi = center
    else:
        lo = center

mz = hi
if Mz >= mz:
    print(mz, Mz)
else:
    print(-1)

Solution (Python):
import math
K = int(input())
A = list(map(int, input().split()))
N_min = 2
N_max = 2

for i in range(K-1,-1,-1):
    N_max = int(math.floor(N_max/A[i]))*A[i] + A[i]-1
    N_min = int(math.ceil(N_min/A[i]))*A[i]
    if N_max < A[i]:
        print(-1)
        exit()
    if N_max < N_min:
        print(-1)
        exit()

print(N_min,N_max)

Solution (Python):
K = int(input())
A = [int(i) for i in input().split()]

N_min = 2
N_max = 2
for a in A[K::-1]:
	if N_min + (a - N_min) % a > N_max:
		print(-1)
		exit(0)
	else:
		
		N_min += (a - N_min) % a
		
		N_max += (a - N_max - 1) % a
print(N_min, N_max)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**15
mod = 10**9+7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def bs(f, mi, ma):
    mm = -1
    while ma > mi:
        mm = (ma+mi) // 2
        if f(mm):
            mi = mm + 1
        else:
            ma = mm
    if f(mm):
        return mm + 1
    return mm

def main():
    n = I()
    a = LI()

    def f(i):
        for c in a:
            i -= i%c
        return i < 2

    def g(i):
        for c in a:
            i -= i%c
        return i <= 2

    mi = bs(f,0,sum(a)+3)
    ma = bs(g,0,sum(a)+3) - 1
    if mi <= ma:
        return '{} {}'.format(mi,ma)
    return -1




print(main())

Solution (Python):
from math import ceil
K = int(input())
A = list(map(int, input().split()))
L, R = 2, 2

for n in A[::-1]:
    L = ceil(L / n) * n
    R = (R // n) * n + n - 1
    if L > R:
        print(-1)
        exit()

print(L, R)

Solution (Python):

K = int(input())
A = list(map(int, input().split()))


if A[-1]!=2:
    print(-1)
    exit()
if K>1 and A[-2]!=2 and A[-2]!=3:
    print(-1)
    exit()


min_n = 2
max_n = 3
for i in A[::-1][1:]:
    a = (min_n - 1)//i
    b = max_n//i
    if a==b:
        print(-1)
        exit()

    if min_n%i==0:
        pass
    else:
        min_n = min_n + (i - min_n%i)
    max_n = max_n - max_n%i + i - 1
    


print(min_n, max_n)

Solution (Python):
K=int(input())
s=input().split(' ')
a=[int(s[i]) for i in range(len(s))]
n=len(s)

def solve():
	x,y=2,2
	for i in range(n-1,-1,-1):
		u=(x+a[i]-1)//a[i]
		v=y//a[i]
		if (a[i]*v<x): return -1,-1
		x=a[i]*u
		y=a[i]*(v+1)-1
	return x,y

x,y=solve()
if (x==-1): print(-1)
else: print('%d %d'%(x,y))

Solution (Python):
K=int(input())
s=input().split(' ')
a=[int(s[i]) for i in range(len(s))]
n=len(s)

def solve():
	x,y=2,2
	for i in range(n-1,-1,-1):
		u=(x+a[i]-1)//a[i]
		v=y//a[i]
		if (a[i]*v<x): return -1,-1
		x=a[i]*u
		y=a[i]*(v+1)-1
	return x,y

x,y=solve()
if (x==-1): print(-1)
else: print('%d %d'%(x,y))

Solution (Python):
import pdb

K=int(input())
s=input().split(' ')
a=[int(s[i]) for i in range(len(s))]
n=len(s)

def solve():
	x,y=2,2
	for i in range(n-1,-1,-1):
		u=(x+a[i]-1)//a[i]
		v=y//a[i]
		if (a[i]*v<x): return -1,-1
		x=a[i]*u
		y=a[i]*(v+1)-1
	return x,y

x,y=solve()
if (x==-1): print(-1)
else: print('%d %d'%(x,y))

Solution (Python):
import sys
import math
readline = sys.stdin.readline

K = int(readline())
A = reversed(list(map(int, readline().split())))

a_min = 2
a_max = 2
for a in A:
    a_min = math.ceil(a_min / a) * a
    a_max = a * (a_max // a) + a - 1
    if a_min > a_max:
        print(-1)
        exit()
print('{} {}'.format(str(a_min), str(a_max)))

Solution (Python):
import sys
k = int(raw_input())
a = map(int,raw_input().split())
l = 2; r = 2;
for i in range(k-1,-1,-1) :
    tl = (l-1)/a[i] + 1; tr = r/a[i];
    if tl > tr :
        print "-1"; sys.exit(0);
    l = tl * a[i]; r = tr * a[i] + a[i]-1;
print l,r

Solution (Python):
k = int(input())
ai = reversed([int(x) for x in input().split()])
b = 2; t = 2
f = False
for a in ai:
    b = (((b-1)//a)+1)*a
    t = ((t//a)+1)*a-1
    if b > t:
        f = True
        break
if f:
    print(-1)
else:
    print(b,t)

Solution (Python):
import sys

k = int(input())
a = list(map(int, input().split()))


x, y = 2, 2

for i in range(k-1, -1, -1):
    minx = x + (a[i] - x % a[i]) % a[i]
    maxx = y - y % a[i]
    if minx > maxx:
        print(-1)
        sys.exit(0)
    else:
        x, y = minx, maxx + a[i] - 1


print(x, y)

Solution (Python):
N = int(input())
P = list(reversed(list(map(int, input().split()))))

s, t = 2, 2
for i in P:
    p = max((s + i - 1) // i, 1)
    q = t // i
    if q < p:
        print(-1)
        quit(0)
    s, t = p * i, q * i + i - 1

print(s, t)

Solution (Python):
from math import ceil, floor
k, a, l, r = input(), map(float, raw_input().split()), 2., 2.
for v in a[::-1]:
    l = ceil(l / v) * v
    r = floor(r / v) * v + v - 1
print '%d %d' % (l, r) if l < r else -1

Solution (Python):
K = int(input())
A = [int(i) for i in input().split()]
A.reverse()

min_A=2
max_A=3
cnt=True
if A[0]!=2:
  print(-1)
  cnt=False
for i in range(1,K):
  a=(min_A+A[i]-1)//A[i]*A[i]
  b=max_A//A[i]*A[i]
  if a>b and cnt:
    print(-1)
    cnt=False
    break
  else:
    min_A=a
    max_A=b+A[i]-1

if cnt:
  print(min_A,max_A)

Solution (Python):
import copy
import math


def main():
    n=int(input())
    temp=input().split(" ")
    nums=list(map(int,temp))
    nums.reverse()
    l=2 
    r=2 
    for num in nums:
       
        r=(int(r/num)+1)*(num)-1
        l= math.ceil(l/num)*num
        
        if l>r:
             print("-1")
             return 0
    print(str(l)+" "+str(r))      
    



if __name__ == '__main__':
    main()

Solution (Python):
from math import floor,ceil
K = int(input())
A = [int(x) for x in input().split()]
A.reverse()
jmax, jmin = 2,2

for i in range(0,K):
    if jmin % A[i] != 0:
        if (jmin + (A[i] - (jmin % A[i]))) > jmax:
            print(-1)
            exit()

    jmin = ceil(jmin / A[i]) * A[i]
    jmax = floor(jmax / A[i]) * A[i] + A[i] -1
    
print(jmin, jmax)

Solution (Python):
from math import floor,ceil
K = int(input())
A = [int(x) for x in input().split()]
A.reverse()
jmax, jmin = 2,2

for i in range(0,K):
    mmax,mmin = jmax,jmin
    if mmin % A[i] != 0:
        if (mmin + (A[i] - (mmin % A[i]))) > mmax:
            print(-1)
            exit()

    jmin = ceil(jmin / A[i]) * A[i]
    jmax = floor(jmax / A[i]) * A[i] + A[i] -1

print(jmin, jmax)

Solution (Python):
from math import ceil, floor

K = int(input())
As = list(map(int, input().split()))

L = 2
R = 2
for A in reversed(As):
    if R // A * A < L:
        print('-1')
        exit()
    L = ceil(L / A) * A
    R = floor(R / A) * A + A - 1

print(L, R)

Solution (Python):
mn = 2
mx = 2
n = int(input())
k = [int(i) for i in input().split()]
k.reverse()
for i in range(n):
  t1 = (mn + k[i] - 1) // k[i] * k[i]
  t2 = (mx) // k[i] * k[i]
  if t1 > mx:
    print(-1)
    exit()
  mn = t1
  mx = t2 + k[i] - 1
print(mn, mx)

Solution (Python):
from math import floor, ceil
k = int(input())
a = [int(i) for i in input().split()]
l,r = 2,2
for ai in a[::-1]:
    xi = ceil(l/ai)
    yi = floor(r/ai)
    if xi > yi:
        print(-1)
        exit()
    l = xi * ai
    r = yi * ai + ai - 1
print(l, r)

Solution (Python):
import math
K = int(input())
A = list(map(int,input().split()))
a = 1
L,S = 2,2 

while a <= K:
    if math.ceil(S/A[-a])*A[-a] < S or L < math.ceil(S/A[-a])*A[-a]:
        print('-1')
        exit()
    S = math.ceil(S/A[-a])*A[-a]
    L = math.floor(L/A[-a])*A[-a] + A[-a] -1
    a += 1
print(S,L)

Solution (Python):
import sys

def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [k] = [int(input_raw[0])]
    input_raw = read_func().strip().split()
    num_list = []
    for i in range(k):
        num_list.append(long(input_raw[i]))

    max_num = long(2)
    min_num = long(2)
    is_exist = True
    if num_list[k-1] != 2:
        is_exist = False
    for i in range(k-1, -1, -1):
        if is_exist == False:
            break;
        min_min_num = min_num;
        
        
        max_max_num = max_num + num_list[i] - 1
        if i > 0:
            min_min_num = num_list[i - 1] * (min_min_num / num_list[i - 1] + (0 if (min_min_num % num_list[i - 1] == 0)  else 1) )
            
            
            max_max_num = num_list[i - 1] * long(max_max_num / num_list[i - 1] )

        min_num = min_min_num
        max_num = max_max_num













        if min_num > max_num:
            is_exist = False
            break;

    if is_exist == True:
        print min_num, max_num
    else:
        print -1


if __name__ == '__main__':
    main()

Solution (Python):
k = int(input())
a = list(map(int, input().split()))
def solve():

    if a[k-1] != 2:
        print(-1)
        return

    mi = 2
    mx = 2

    for i in range(k):
        j = k-i-1

        mx = mx + a[j] - 1

        if j == 0:
            pass
        else:
            na = a[j-1]
            
            if mx % na != 0 and mi % na != 0 and ((mx % na) - (mi % na)) == (mx - mi):
                print(-1)
                return

            if mi%na != 0:
                mi = mi + (na - mi%na)

            if mx%na != 0:
                mx = mx - (mx % na)

    print(mi, mx)
solve()

Solution (Python):
input()
L=R=2
for A in list(map(int,input().split()))[::-1]:
    L=(L-1)//A*A+A
    R=R//A*A+A-1
    if L>R:
        print(-1)
        exit()
print(L,R)

Solution (Python):
import numpy as np
import math

n = int(input())
a = list(map(int, input().split(" ")))

min = 2
max = 2

for i in reversed(a):
    min = math.ceil(min / i) * i
    max = math.floor(max / i) * i + i - 1
    if(min > max):
        print(-1)
        exit()

print("%d %d" %(min, max))

Solution (Python):
input()
L=R=2
for A in list(map(int,input().split()))[::-1]:
    L=((L-1)//A+1)*A
    R=(R//A+1)*A-1
    if L>R:
        print(-1)
        exit()
print(L,R)

Solution (Python):
input()
L=R=2
for A in list(map(int,input().split()))[::-1]:
    L=(L-1)//A*A+A
    R=R//A*A+A-1
    if L>R:
        print(-1)
        exit()
print(L,R)

Solution (Python):
k = int(input())
a = list(map(int, input().split()))[::-1]
l, r = 2, 2
for i in range(k):
    l = ((l-1)//a[i] + 1) * a[i]
    r = (r//a[i] + 1) * a[i] - 1
    if l > r:
        print(-1)
        exit()
print(l, r)

Solution (Python):
k = int(raw_input())
a = map(int,raw_input().split(" "))

if a[-1] != 2:
  print "-1"
  exit(0)

r = [2,3]
ainv = a[::-1]

for now in ainv[1:]:
  lower = (r[0]/now)*now
  if lower < r[0]:
    lower += now
  upper = (r[1]/now)*now

  q = [lower,upper]

  if q[0] > r[1]:
    print "-1"
    exit(0)
  
  q[1] = q[1] + now - 1
  r = q

print "%d %d" % (r[0],r[1])

Solution (Python):
import math

K = int(input())

A = input().split()

for i in range(K):
    A[i] = int(A[i])

maxlst = [0 for i in range(K)]
minlst = [0 for i in range(K)]

if A[K-1] != 2:
    print(-1)
    quit()

maxlst[K-1] = 3
minlst[K-1] = 2

for i in reversed(range(K-1)):
    if maxlst[i+1] < A[i] or (maxlst[i+1] % A[i] != 0 and minlst[i+1] % A[i] != 0 and int(maxlst[i+1]/A[i]) == int(minlst[i+1]/A[i])):
        print(-1)
        quit()
    maxlst[i] = int(A[i] - 1 + A[i] * math.floor(maxlst[i+1] / A[i]))
    minlst[i] = int(math.ceil(minlst[i+1] / A[i]) * A[i])

print(minlst[0], maxlst[0])

Solution (Python):
K = int(input())
A = list(map(int,input().split()))

minn = 2
maxn = 3
if A[-1] != 2:
	minn = -1

if minn != -1:
	for i in range(K-2,-1,-1):
		m = A[i]
		a = (minn + m - 1) // m * m
		b = maxn // m * m
		if b < a:
			minn = -1
			break
		minn = a
		maxn = b + m - 1

if minn == -1:
	print(minn)
else:
	print(minn,maxn)

Solution (Python):
K=int(input())
A=input().split(' ')
min=2
max=2
flag=0
while K>0:
  K=K-1
  i=int(A[K])
  if max<i:
    print('-1')
    flag=1
    break
  elif i<min:
    if min%i!=0:min=min+i-(min%i)
    else:min=min+(min%i)
    max=max+i-(max%i)-1
    if min>max:
      print('-1')
      flag=1
      break
  else:
    min=i
    max=max+i-(max%i)-1
if flag==0:
  print('{0} {1}'.format(min,max))

Solution (Python):

from collections import Counter
import numpy as np
import sys

def IMPOSSIBLE():
    print(-1)
    sys.exit()
    

k = int(input())
a = list(map(int,input().split()))[::-1]
min_num = 2
max_num = 3

if(a[0]!=2):IMPOSSIBLE()

for i in range(len(a)-1):
    if(a[i+1] > max_num):IMPOSSIBLE()
    else:
        min_num = a[i+1]*(min_num//a[i+1] if min_num%a[i+1]==0 else min_num//a[i+1]+1)
        max_num = a[i+1]*((max_num//a[i+1])+1)-1
        

tmpmin = min_num
tmpmax = max_num
for i in range(len(a)):
    tmpmin -= tmpmin%a[len(a)-i-1]
    tmpmax -= tmpmax%a[len(a)-i-1]
if(tmpmin != 2 or tmpmax != 2):IMPOSSIBLE()
    
print(str(min_num)+' '+str(max_num))

Solution (Python):
K = int(input())
A = list(map(lambda x:int(x), input().split(" ")))
A.reverse()

n = [2,2]

for a in A:

    n1 = (n[0]//a+(1 if n[0]%a>0 else 0))*a
    n2 = (n[1]//a)*a
    
    if n1>n2:
        print("-1")
        exit()
    n[0] = n1
    n[1] = n2+a-1

if len(n)>0:
    print(min(n),max(n))
else:
    print("-1")

Solution (Python):
from pprint import pprint as pp
import collections
import math
import itertools

K, = map(int, input().split())
A = tuple(map(int, input().split()))

old_a = A[-1]
x_max = 2 // A[-1]
x_min = 2 // A[-1]
for a in reversed(A):
  
  if a >= (old_a * (x_max +1)):
    x_max = -1
    break
  x_max = (old_a * (x_max+1)-1) // a
  x_min = math.ceil(old_a * x_min / a)
  if x_max < x_min:
    x_max = -1
    break
  old_a = a

if x_max == -1:
  print(-1)
else:
  print(old_a * x_min, (old_a * (x_max+1))-1)

Solution (Python):
K = int(input())
A = list(map(int, input().split()))

def solve():
    if A[K - 1] != 2:
        print(-1)
        return;
    M = 2
    m = 2
    for i in range(K - 1, 0, -1):
        nM = (M + A[i] - 1) - (M + A[i] - 1) % A[i - 1]
        if m % A[i - 1] == 0:
            nm = m
        else:
            nm = m + A[i - 1] - m % A[i - 1]
        
        if nM < m or nm >= M + A[i]:
            print(-1)
            return;
        
        M, m = nM, nm
    M = M + A[0] - 1
    
    print(m, M)
solve()

Solution (Python):
K = int(input())
A = list(map(int, input().split()))
A.reverse()
A.append(1)
ansmin = 2
ansmax = 2
for a in A:
  tmin = ((ansmin+a-1)//a)*a
  tmax = (ansmax//a)*a
  if tmin > tmax:
    print('-1')
    exit()
  ansmin = tmin
  ansmax = tmax + a - 1
print(ansmin, ansmax)

Solution (Python):
K = int(input())
AT = list(map(int,input().split()))

A=reversed(AT)
s = 2;b = 1;lis=[]
for i in A:
    ns = i
    if s > ns:
        ns +=(((s-ns)//i)*i)
        if s > ns:
            ns+=i
    s = ns
sn = s
for i in AT:
    sn = sn-sn%i

if sn!=2:
    print(-1)
    exit()
b = 1
AT.reverse()
for i in AT:
    b=b-b%i
    nb = i
    if b > nb:
        nb +=(((b-nb)//i)*i)
        if b > nb:
            nb+=i
    b= (nb+i-1)
print(s,b)

Solution (Python):
def main():
    K = int(input())
    A = [int(a) for a in input().split()]

    if not A[K - 1] == 2:
        print(-1)
        return

    nmin, nmax = 2, 2
    ii = list(range(K))
    ii.reverse()
    for i in ii:
        Ai = A[i]
        if Ai > nmax:
            print(-1)
            return
        if Ai < nmin and Ai * 2 > nmax:
            print(-1)
            return
        if Ai < nmin:
            if nmin % Ai == 0:
                pass
            else:
                nmin = Ai * (nmin // Ai + 1)
        else:
            nmin = Ai

        if nmax > Ai:
            
            
            
            
            nmax = Ai * (nmax // Ai + 1) - 1
        else:
            nmax = Ai * 2 - 1
        
        if nmin > nmax:
            print(-1)
            return
    print(nmin, nmax)


main()

Solution (Python):

K = int(input())
A_list = list(map(int, input().split()))

res_min = [2]*(K+1)
res_max = [2]*(K+1)
possible = True
A_list.append(2)

for i in range(K-1, -1, -1):
    
    if res_min[i+1] % A_list[i] == 0:
        res_min[i] = res_min[i+1]
    else:
        res_min[i] = res_min[i+1] + A_list[i] - res_min[i+1] % A_list[i]
        
    
    if (res_min[i] // A_list[i+1]) * A_list[i+1] > res_max[i+1]:
        possible = False
    
    
    if (res_max[i+1] + A_list[i+1]) % A_list[i] == 0:
        res_max[i] = res_max[i+1] + A_list[i+1] - A_list[i]
    else:
        res_max[i] = ((res_max[i+1] + A_list[i+1]) // A_list[i]) * A_list[i]
        
    
    if (res_max[i] // A_list[i+1]) * A_list[i+1] < res_min[i+1]:
        possible = False

        
if A_list[K-1] != 2:
    possible = False
if possible:
    print(res_min[0], res_max[0]+A_list[0]-1)
else:
    print(-1)

Solution (Python):

from bisect import bisect, bisect_left, insort
from itertools import accumulate
def inpl(): return tuple(map(int, input().split()))

K = int(input())
A = inpl()
if A[-1] != 2:
    print(-1)
    exit()

l, r = 2, 3
for a in A[:-1][::-1]:
    l = (l//a)*a + int(l%a!=0)*a
    r = (r//a)*a + (a-1)
    if r < l:
        print(-1)
        break
else:
    print(l, r)

Solution (Python):

import math

N = int(input())
As = list(map(int, input().split()))

if As[-1] != 2:
    print(-1)
    exit()
if N == 1:
    print(2, 3)
    exit()

p_min = As[-1]
p_max = As[-1] + 1

for i in range(N-2,-1,-1):
    p_min = As[i] * math.ceil(p_min/As[i])
    p_max = As[i] * math.floor(p_max/As[i])
    if p_min > p_max:
        print(-1)
        exit()

    p_max = p_max + As[i] - 1


print(p_min, p_max)

Solution (Python):
import sys

K = int(raw_input())
A = [int(val) for val in raw_input().split(" ")]

if A[-1] > 2:
    print "-1"
    sys.exit()

MIN, MAX = 2, 2
for k in range(1, K)[::-1]:
    if MIN%A[k-1]==0:
        pass
    else:
        MIN = (MIN/A[k-1]+1)*A[k-1]
    MAX_ = MAX + A[k] - 1
    if MAX_%A[k-1]==0:
        MAX = MAX_
    else:
        MAX = (MAX_/A[k-1])*A[k-1]
    if MIN > MAX:
        print "-1"
        sys.exit()
MAX = MAX + A[0] - 1

print "%d %d"%(MIN, MAX)

Solution (Python):
K = int(input())
A = list(map(int, input().split()))

if A[-1] != 2:
    result = -1
else:
    result = 0
    cand_max = 2
    cand_min = 2
    for i in range(len(A))[::-1]:
        comp = int(cand_min / A[i]) * A[i]
        if cand_min <= comp <= cand_max:
            pass
        elif cand_min <= comp + A[i] <= cand_max:
            comp += A[i]
        else:
            result = -1
            break
        cand_max = int(cand_max / A[i] + 1) * A[i] - 1
        cand_min = comp

if result == -1:
    print(-1)
else:
    print(cand_min, cand_max)

Solution (Python):
k = int(input())
A = list(map(int, input().split()))
l, r = 2, 2
for a in reversed(A):
    l += (a - l%a) % a
    r -= r % a
    r += a - 1
    if l > r:
        print(-1)
        break
else:
    print("{} {}".format(l, r))

Solution (Python):

K = int(input())
A = list(map(int, input().split()))

A.reverse()
mn, mx = 2, 2
for i in range(K):
    mn += (A[i] - mn % A[i]) % A[i]
    mx -= mx % A[i]
    mx += A[i] - 1
    if mn > mx:
        print(-1)
        break
else:
    print(mn, mx)

Solution (Python):
n=int(input())
a=list(reversed(list(map(int,input().split()))))
if a[0]>2:
	print("-1")
	exit()
a1,a2=2,2
for i in range(1,n):
	a1+=(a[i]-a1%a[i])%a[i]
	pre=a2
	a2+=a[i-1]-1
	a2-=a2%a[i]
	if a1>a2:
		print("-1")
		exit()	
print(a1,a2+a[n-1]-1)

Solution (Python):
K = int(input())
src = list(map(int,input().split()))
if src[-1] != 2:
    print(-1)
    exit()

mn = 2
mx = 3
for a in reversed(src[:-1]):
    mn2 = mn + ((a - mn%a) % a)
    mx2 = mx - (mx%a)
    if mn2 > mx2:
        print(-1)
        exit()
    mn = mn2
    mx = mx2 + a - 1
print(str(mn) + ' ' + str(mx))

Solution (Python):
import sys
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt, floor
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache, reduce
from operator import xor
from heapq import heappush, heappop
INF = float("inf")
sys.setrecursionlimit(10**7)


dy4, dx4 = [0, -1, 0, 1], [1, 0, -1, 0]


def inside(y: int, x: int, H: int, W: int) -> bool: return 0 <= y < H and 0 <= x < W
def ceil(a, b):return (a + b - 1) // b


def up(a, b):
    s = ceil(a, b)
    for i in range(s - 1, s + 3):
        if i * b > a:
            return i * b


def check(n, A):
    for a in A:
        n = (n // a) * a
    return n == 2


def solve(A):


    if A[-1] != 2:
        return [-1]

    min_a, max_a = 2, 3
    for a in A[::-1][1:]:
        if a > max_a:
            return [-1]
        else:
            max_a = up(max_a, a) - 1

        if a >= min_a:
            min_a = a
        else:
            min_a = ceil(min_a, a) * a

    if min_a > max_a:
        return [-1]
    return min_a, max_a

def solve2(A):
    min_a = INF
    max_a = 0
    for i in range(0, 100):
        if check(i, A):
            min_a = min(min_a, i)
            max_a = max(max_a, i)

    if min_a == INF or max_a == 0:
        return [-1]
    return min_a, max_a

def solve3(A):
    
    min_a = INF
    max_a = 0

    low, high = 0, int(1e14) - 1
    while high - low > 1:
        middle = (low + high) // 2
        if check(middle, A):
            min_a = middle
            low = middle
        else:
            high = middle

    low, high = 0, int(1e14) - 1
    while high - low > 1:
        middle = (low + high) // 2
        if check(middle, A):
            max_a = middle
            high = middle
        else:
            low = middle

    if min_a == INF or max_a == 0:
        return [-1]
    return min_a, max_a

def main():
    K = int(input())
    A = list(map(int, input().split()))

    print(*solve(A))



if __name__ == '__main__':
    main()

Solution (Python):
K = int(input())
A = list(map(int,input().split()))

if A[-1] != 2:
	print(-1)
	exit(0)

minval = 2
maxval = 3
for i in range(K-1,-1,-1):
	if maxval < A[i] or (minval > A[i] and ((maxval - (maxval % A[i]) < minval))):
		print(-1)
		exit(0)

	if minval > A[i]:
		if minval % A[i] != 0:
			minval = minval + (A[i] - (minval % A[i]))

		maxval = (maxval - (maxval % A[i]) + A[i] - 1)

	else:
		maxval = A[i] + (A[i] - 1)
		minval = A[i]
		

print(str(minval) + " " + str(maxval))

Solution (Python):


def main():
    k = int(input())
    aa = list(map(int, input().split()))[::-1] + [1]
    if aa[0] != 2:
        print(-1)
        return
    min_n, max_n = 2, 2
    for i in range(k):
        a = aa[i]
        b = aa[i + 1]
        if min_n % a:
            min_n = (min_n // a + 1) * a
        else:
            min_n = min_n // a * a
        m = max_n + a - 1
        m -= m % b
        if min_n > m:
            print(-1)
            return
        else:
            max_n = m
    print(min_n, max_n)


main()