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



Sample input:
4
3 4 3 2


Sample output:
6 8

Submission status: Accepted

Solution code:
def examA():
    N, A, B = LI()
    if (A-B)%2==0:
        ans = "Alice"
    else:
        ans = "Borys"
    print(ans)
    return

def examB():
    K = I()
    A = LI()
    cur = 2
    for i in range(K-1,-1,-1):
        a = A[i]
        if A[i]>cur:
            print(-1)
            exit()
        cur = (cur//a+1)*a-1
#        print(cur)
    maxA = cur; cur = 2
    for i in range(K-1,-1,-1):
        a = A[i]
        if cur<=a:
            cur = a
        else:
            cur = a*((cur-1)//a+1)
    minA = cur
    if minA>maxA:
        print(-1)
        exit()
    print(minA,maxA)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

if __name__ == '__main__':
    examB()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    k=int(input())
    A=list(map(int,input().split()))
    l=2; r=2
    for a in A[::-1]:
        # l 以上の a の倍数
        l=((l-1)//a+1)*a
        # r 以下の a の倍数 + (a-1)
        r=(r//a)*a+(a-1)
        if(l>r or r<2):
            print(-1)
            return
    print(l,r)
resolve()

Submission status: Accepted

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

def solve() :            
    m, M = 2, 2
    for a in A[::-1] :
        if (m - 1) // a == M // a :
            return [-1]
            
        M = M // a * a + a - 1
        x = (m + a - 1) // a
        m = a * x
        
    return [m, M]
    
print(*solve())

Submission status: Accepted

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

Submission status: Accepted

Solution code:
k=int(input())
l=list(map(int,input().split()))
nowmax=2
nowmin=2
for i in range(k):
  num=l[-1-i]
  if nowmax-nowmin<num and nowmax%num>=nowmin%num and not(nowmax%num==0 or nowmin%num==0):
    print(-1)
    exit()
  else:
    nowmax,nowmin=(nowmax//num)*num+num-1,(((nowmin-1)//num)+1)*num
print(nowmin,nowmax)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
x = a[-1]
y = 4
if x != 2:
  print(-1)
  exit(0)
for i in range(N - 2, -1, -1):
  p = a[i]
  t = -(-x // p)
  x = t * p
  u = -(-y // p)
  y = u * p
t = x + 0
for i in range(N):
  t -= t % a[i]
if t == 2:
  print(x, y - 1)
else: print(-1)

Submission status: Accepted

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

A.reverse()

mi, ma = 2, 2

for a in A:
    if ma < ((mi + a - 1) // a) * a:
        print(-1)
        exit()
    mi = (mi + a - 1) // a * a
    ma = (ma // a + 1) * a - 1

print(mi, ma)

Submission status: Accepted

Solution code:
import math
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353

K = int(sys.stdin.buffer.readline())
A = list(map(int, sys.stdin.buffer.readline().split()))


def solve():
    if A[-1] == 1:
        lo = 2
        hi = 2
    elif A[-1] == 2:
        lo = 2
        hi = 3
    else:
        return None

    for a in reversed(A[:-1]):
        l = math.ceil(lo / a) * a
        h = math.floor(hi / a) * a
        if not lo <= l <= hi or not lo <= h <= hi:
            return None
        lo = l
        hi = h + a - 1
    return lo, hi


ans = solve()
if ans:
    print(*ans)
else:
    print(-1)

Submission status: Accepted

Solution code:
import math
k = int(input())
a = list(map(int, input().split()))
MAX = 2
MIN = 2
for i in a[::-1]:
    if MAX >= i:
        MAX = i * (MAX // i) + (i-1)
        MIN = i * (math.ceil(MIN / i)) 
        if MIN > MAX:
          print(-1)
          exit()
    else:
        print(-1)
        exit()
print(MIN,MAX)

Submission status: Accepted

Solution code:
k = int(input())
a = list(map(int, input().split()))
l, r = 0, 10 ** 15
while r - l > 1:
    m = (l + r) // 2
    t = m
    for x in a:
        m = m // x * x
    if m <= 2:
        l = t
    else:
        r = t
l2, r2 = 0, 10 ** 15
while r2 - l2 > 1:
    m = (l2 + r2) // 2
    t = m
    for x in a:
        m = m // x * x
    if m < 2:
        l2 = t
    else:
        r2 = t
if l < r2:
    print(-1)
else:
    print(r2, l)

Submission status: Accepted

Solution code:
def main():
	k, *a = map(int, open(0).read().split())
	m = M = 2
	for x in a[::-1]:
		M -= M % x
		if m > M:
			print(-1)
			exit()
		m += -m % x
		M += x - 1
	print(m, M)

if __name__=="__main__":
	main()

Submission status: Accepted

Solution code:
import math
K = int(input())
A = list(map(int, input().split()))
N, n = 2, 2
k = [0, 0]
for i in reversed(range(K)):
    k[0] = (N // A[i])*A[i]
    k[1] = math.ceil(n / A[i]) * A[i]
    if k[0]*A[i] >= n and k[1] <= N:
        N = k[0]+A[i] - 1
        n = k[1]
    else:
        print(-1)
        exit(0)
print(n, N)

Submission status: Accepted

Solution code:
K = int(input())
A = [int(_) for _ in input().split()]

A.reverse()
maximum = 2
minimum = 2
able = True

for i in range(K):
  maximum = (maximum // A[i]) * A[i] + A[i] - 1
  minimum = ((minimum - 1) // A[i] + 1) * A[i]
  if minimum > maximum:
    able = False
    break

if able:
  print(str(minimum) + ' ' + str(maximum))
else:
  print(-1)

Submission status: Accepted

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

ans1 = 2
ans2 = 2
for i in range(K - 1, 0, -1):
    if ans1 % A[i] != 0 or ans2 % A[i] != 0 or ans1 < 2 or ans2 < 2:
        print (-1)
        exit()
    ans1 = (ans1 + A[i] - 1) // A[i - 1] * A[i - 1] #最大
    ans2 = (ans2 + A[i - 1] - 1) // A[i - 1] * A[i - 1] #最小
    if ans2 > ans1: #逆転している = 存在できない
        print (-1)
        exit()

    # print (ans1, ans2)

if ans1 % A[0] != 0 or ans2 % A[0] != 0 or ans1 < 2 or ans2 < 2:
    print (-1)
    exit()

print (ans2, ans1 + A[0] - 1)

Submission status: Accepted

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

ans1 = 2
ans2 = 2
for i in range(K - 1, 0, -1):
    if ans1 % A[i] != 0 or ans2 % A[i] != 0 or ans1 < 2 or ans2 < 2:
        print (-1)
        exit()
    ans1 = (ans1 + A[i] - 1) // A[i - 1] * A[i - 1] #最大
    ans2 = (ans2 + A[i - 1] - 1) // A[i - 1] * A[i - 1] #最小
    if ans2 > ans1:
        print (-1)
        exit()
    if ans2 == 0:
        ans2 = ans1

    # print (ans1, ans2)

if ans1 % A[0] != 0 or ans2 % A[0] != 0 or ans1 < 2 or ans2 < 2:
    print (-1)
    exit()

print (ans2, ans1 + A[0] - 1)

Submission status: Accepted

Solution code:
K = int(input())
A = list(map(int, input().split()))
min = 2
max = 2
for i in range(K-1, -1, -1):
    min += (-min) % A[i]
    max -= max % A[i]
    max += A[i] - 1
if max < min:
  print(-1)
  exit()
print(min, max)

Submission status: Accepted

Solution code:
K = int(input())
A = list(map(int, input().split()))
min = 2
max = 2
for i in range(K-1, -1, -1):
    if max < A[i]:
        print(-1)
        exit()
    min += (-min) % A[i]
    max -= max % A[i]
    max += A[i] - 1
    if max < min:
        print(-1)
        exit()
print(min, max)

Submission status: Accepted

Solution code:
K = int(input())
A = list(map(int, input().split()))
min = 2
max = 2
for i in range(K-1, -1, -1):
    if max < A[i]:
        print(-1)
        exit()
    min = (min + A[i] - 1) // A[i] * A[i]
    max = (max // A[i] + 1) * A[i] - 1
    if max < min:
        print(-1)
        exit()
print(min, max)

Submission status: Accepted

Solution code:
n=int(input())
a=list(reversed(list(map(int,input().split()))))
p,q=2,2
for i in range(n):
	q2=((q-1)//a[i]+1)*a[i]
	p2=(p//a[i]+1)*a[i]-1
	p=p2
	q=q2
	if p<q:
		print(-1)
		break
else:
	print(q,p)

Submission status: Accepted

Solution code:
k=int(input())
a=[int(i) for i in input().split()]
l=2
r=3
for i in range(k-1,-1,-1):
    l=(l+a[i]-1)//a[i]*a[i]
    r=(r+a[i]-1)//a[i]*a[i]
if l==r:
    print(-1)
else:
    print(l,r-1)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int,input().split()))[::-1]


def b():
    if A[0] != 2:
        return -1
    else:
        ans = [2, 3]
        for i in range(1, n):
            r = ans[1] // A[i] * A[i]
            l = -((-ans[0] // A[i]) * A[i])
            # print(-ans[0] // A[i])
            # print(l, r)
            if r < l:
                return -1
            ans = [l, r+A[i]-1]
            # print(ans)
    return ans

ans = b()
if ans == -1:
    print(ans)
else:
    print(ans[0], ans[1])

Submission status: Accepted

Solution code:
import sys

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


MIN=2
MAX=2

for a in A[::-1]:
    M1=math.ceil(MIN/a)*a
    M2=MAX//a*a

    if M1>MAX:
        print(-1)
        sys.exit()

    else:
        MIN=M1
        MAX=M2+a-1

print(MIN,MAX)

Submission status: Accepted

Solution code:
# binary_search

def is_ok(x, f):
    for d in a:
        x = x - x % d
    return f(x)


def binary_search(f, ok, ng):
    while abs(ok - ng) > 1:
        mid = (ok + ng) // 2
        if is_ok(mid, f):
            ok = mid
        else:
            ng = mid
    return ok


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

max_ = binary_search(lambda x: x <= 2, ok=0, ng=10 ** 14 + 10)
min_ = binary_search(lambda x: x >= 2, ok=10 ** 14 + 10, ng=0)

if min_ > max_:
    print(-1)
else:
    print(min_, max_)

Submission status: Accepted

Solution code:
k = int(input())
a = tuple(map(int, input().split()))

l, r = 2, 2
ans = (-1,)
for x in reversed(a):
    l = ((l + x - 1) // x) * x
    # print(x, l, r)
    if r < l:
        break
    r = ((r // x) * x) + (x - 1)
else:
    ans = (l, r)
print(*ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))[::-1]

min_ans = 2
max_ans = 2

for i in range(n):
    min_ans = -((-min_ans) // a[i]) * a[i]
    
for i in range(n):
    max_ans = (max_ans// a[i]) * a[i] +a[i] - 1

a = a[::-1]
tmp_ans = min_ans
tmp_ans2 = max_ans
for i in range(n):
    tmp_ans = (tmp_ans // a[i]) * a[i]
    tmp_ans2 = (tmp_ans // a[i]) * a[i]
    
if tmp_ans == 2 and tmp_ans2 == 2:
    print(min_ans, max_ans)
else:
    print(-1)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)

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

def ceil(a, b):
    return -(-a // b)

def floor(a, b):
    return a // b

def L(i):
    if i == K:
        return 2
    return ceil(L(i+1), A[i]) * A[i]

def R(i):
    if i == K:
        return 2
    return floor(R(i+1), A[i]) * A[i] + A[i] - 1

L0 = L(0); R0 = R(0)
if L0 <= R0:
    print(L0, R0)
else:
    print(-1)

Submission status: Accepted

Solution code:
import sys

def updiv(a,b):

    if a % b == 0:
        return a // b
    else:
        return a // b + 1

K = int(input())

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

mini = 2
maxi = 2

for i in range(K):

    mini = updiv(mini, A[i]) * A[i]
    maxi = maxi // A[i] * A[i]

    if mini > maxi:
        print (-1)
        sys.exit()

    maxi += A[i]-1

print (mini,maxi)

Submission status: Accepted

Solution code:
K=int(input())
A=list(map(int,input().split()))
A=A[::-1]
#print(A)
if A[0]!=2:
    print(-1)
    exit()

M=3
m=2
import math
for i in range(1,K):
    if m%A[i]==0:
        Lm=m
    else:
        Lm=(m//A[i]+1)*A[i]
    #print(Lm)
    if Lm<m or M<Lm:
        print(-1)
        exit()
    LM=(M//A[i])*A[i]+A[i]-1
    if LM<Lm:
        print(-1)
        exit()
    M=LM
    m=Lm
print(m,M)

Submission status: Accepted

Solution code:
import math
K = int(input())
A = [int(i) for i in input().split()][::-1]

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

ans_low = 2
ans_high = 3

for i in range(1, K):
    next_ans_high = ((ans_high//A[i])+1)*A[i]-1
    next_ans_low = math.ceil(ans_low/A[i])*A[i]

    if (next_ans_high // A[i])*A[i] > ans_high or next_ans_low > next_ans_high:
        print(-1)
        exit()

    ans_high = next_ans_high
    ans_low = next_ans_low

    # print(A[i], ans_low, ans_high)

print(ans_low, ans_high)

Submission status: Accepted

Solution code:
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[]]*num
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))

#################

K = I()
A = III()

for i in range(K)[::-1]:
    if i==K-1:
        if A[i]==2:
            l = 2
            r = 3
        else:
            print(-1)
            exit()
    else:
        x = math.ceil(l/A[i])*A[i]
        y = math.floor(r/A[i])*A[i]
        if x<=r and y>=l:
            l = x
            r = y+A[i]-1
        else:
            print(-1)
            exit()

print(l,r)

Submission status: Accepted

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
inf=float("inf")
k=int(input())
a=list(map(int,input().split()))
min_num=2
max_num=3
for i in range(1,k):
    min_num=((min_num-1)//a[-i-1]+1)*a[-i-1]
    max_num=(max_num//a[-i-1]+1)*a[-i-1]-1

tmp=min_num
for i in range(k):
    tmp-=tmp%a[i]
if tmp!=2:
    print(-1)
    quit()

print(min_num)
print(max_num)

Submission status: Accepted

Solution code:
import math
K = int(input())
A = [int(_) for _ in input().split()]
l = r = 2
'''
if x children form groups consisting of y children each
 then z children are left
=> z=y*(x//y)
=> y*(math.ceil(z/y))<=x<y*(math.floor(z/y)+1)
'''
#necessary conditions
for a in A[::-1]:
    l = a * math.ceil(l / a)
    r = a * (math.floor(r / a) + 1) - 1
#sufficient conditions
for x in [l, r]:
    for a in A:
        x = a * (x // a)
    if x != 2:
        print(-1)
        exit()
print(l, r)

Submission status: Accepted

Solution code:
K = int(input())
A = [int(_) for _ in input().split()]
l = r = 2
for a in A[::-1]:
    l = a * ((l - 1) // a) + a
    r = a * (r // a + 1) - 1
for x in [l, r]:
    for a in A:
        x = a * (x // a)
    if x != 2:
        print(-1)
        exit()
print(l, r)

Submission status: Accepted

Solution code:
import math
K = int(input())
A = list(map(int,input().split()))
M,m = 2,2
A = reversed(A)
for i in A:
    M = (M//i)*i+(i-1)
    m = math.ceil(m/i)*i
    if m > M:
        print(-1)
        exit()
print(m,M)

Submission status: Accepted

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

if A[-1] != 2:
    print(-1)
    exit()
else:
    m, M = 2, 3
    for i in range(K-2, -1, -1):
        if A[i] > M:
            print(-1)
            exit()
        else:
            if m % A[i] != 0:
                m = (m // A[i] + 1) * A[i]
            M = (M // A[i] + 1) * A[i] - 1
if m > M:
    print(-1)
    exit()
else:
    print(m, M)

Submission status: Accepted

Solution code:
def getlist():
	return list(map(int, input().split()))

#受け取り
K = int(input())
A = getlist()
A = A[::-1]

#処理
ans = 0
dpM = [0] * (K)
dpm = [0] * (K)
dpM[0] = A[0] * 2 - 1
dpm[0] = A[0]
for i in range(1, K):
	if A[i] > dpM[i - 1]:
		ans = -1
		break
	elif A[i] >= dpm[i - 1]:
		dpM[i] = A[i] * 2 - 1
		dpm[i] = A[i]

	else:
		dpM[i] = A[i] * (dpM[i - 1] // A[i] + 1) - 1

		if dpm[i - 1] % A[i] == 0:
			dpm[i] = dpm[i - 1]
		else:
			dpm[i] = A[i] * (dpm[i - 1] // A[i] + 1)
	if dpm[i] > dpM[i]:
		ans = -1
		break

#出力
if ans == -1 or A[0] != 2:
	print(-1)
else:
	print(dpm[-1], dpM[-1])

Submission status: Accepted

Solution code:
# AGC020B - Ice Rink Game
def main():
    K, *A = map(int, open(0).read().split())
    l, h = 2, 2  # low, high
    for i in A[::-1]:
        l += -l % i
        h += i - 1 - h % i
    if l > h:
        print(-1)
    else:
        print(l, h)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# AGC020B - Ice Rink Game
def main():
    K, *A = map(int, open(0).read().split())
    if A.pop() != 2:
        print(-1)
        return
    mn, mx = 2, 3
    for i in A[::-1]:
        if i > mx:
            print(-1)
            return
        mx = mx // i * i + i - 1
        mn = ((mn + i - 1) // i) * i
    if mn > mx:
        print(-1)
    else:
        print(mn, mx)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
K, *A = map(int, open(0).read().split())

mi = ma = 2
for a in reversed(A):
    mi = -(-mi // a) * a
    ma = (ma // a) * a + a - 1

if ma < mi:
    print(-1)
else:
    print(mi, ma)

Submission status: Accepted

Solution code:
_,a=open(0)
l,r=-2,-3
for a in map(int,a.split()[::-1]):l,r=l//a*a,r//a*a
print(-(l==r)or'%d %d'%(-l,~r))

Submission status: Accepted

Solution code:
_,a=open(0)
l,r=-2,-3
for a in map(int,a.split()[::-1]):
  l,r=l//a*a,r//a*a
print(-(l==r)or'%d %d'%(-l,~r))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

def ceil(a, b):
    if a%b == 0:
        return a//b
    else:
        return a//b + 1

def f(l, r, a):
    # 区間[l, r]内の，最小・最大のaの倍数があれば返す
    min_ = ceil(l, a) * a
    max_ = (r // a) * a
    if min_ > max_:
        return False, min_, max_
    else:
        return True, min_, max_

def main():
    K = int(input())
    A = list(map(int, input().split()))[::-1]

    min_ = 2
    max_ = 2
    for i in range(K):
        jud, x, y = f(min_, max_, A[i])
        if not jud:
            print(-1)
            return
        min_ = x
        max_ = y + A[i] - 1
        # print(min_, max_)
    
    print(min_, max_)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
k=int(input())
a=list(map(int,input().split()))
s=2+k*max(a)
l,r=s+1,0
while l-r>1:
    t=(l+r)//2
    p=t
    for i in a:
        t-=t%i
    if t>=2:
        l=p
    else:
        r=p
e=l
l,r=s+1,0
while l-r>1:
    t=(l+r)//2
    p=t
    for i in a:
        t-=t%i
    if t>2:
        l=p
    else:
        r=p
print(str(max(e,2))+" "+str(r)if r>e or r<2 else -1)

Submission status: Accepted

Solution code:
K=int(input())
A=list(map(int,input().split()))
l,h=2,2
for a in A[::-1]:
    l=-(-l//a)*a
    h=(h//a+1)*a-1
    if l>h:
        l=h=-1
        break
if l==h==-1:
    print(-1)
else:
    print(l,h)

Submission status: Accepted

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


if K == 1:
    if A[0] == 2:
        print(2, 3)
    else:
        print(-1)
else:
    M = 2
    L = 2
    ok = True
    for i in range(K-1, -1, -1):
        a = A[i]
        nM = (M//a)*a
        if M%a != 0:
            nM += a
        nL = (L//a)*a + a - 1
        if nM >= M + a or nM > nL:
            ok = False
            break
        
        L, M = nL, nM
        #print(L, M, a, b)

    if not ok:
        print(-1)
    else:
        print(M, L)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#AGC20 B

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

def f(x):
    for i in a:
        if x < i:
            x = 0
            break
        x = x - (x % i)
    return x
k = I()
a = LI()
high,low = 10**20,0
while (high - low) > 1:
    mid = (high + low) // 2 
    if f(mid) >= 2:
        high = mid
    else:
        low = mid
ans1 = high
high,low = 10**20,0
while (high - low) > 1:
    mid = (high + low) // 2
    if f(mid) > 2:
        high = mid
    else:
        low = mid
ans2 = low
if ans1 > ans2:
    print(-1)
else:
    if f(ans1) != 2 or f(ans2) != 2:
        print(-1)
    else:
        print(ans1,ans2)

Submission status: Accepted

Solution code:
K = int(input())
A = [int(x) for x in input().split()]
C = [[0, 0] for _ in range(K + 1)]
C[K] = [2, 2]
for i in range(K - 1, -1, -1):
  C[i][0] = A[i] * -(-C[i + 1][0] // A[i])
  C[i][1] = A[i] * (C[i + 1][1] // A[i]) + (A[i] - 1)
  if C[i][0] > C[i + 1][1]:
    print(-1)
    break
else:
  print(C[0][0], C[0][1])

Submission status: Accepted

Solution code:
K = int(input())
A = [int(x) for x in input().split()]
nmin = 2
nmax = 2
last = 1
for a in A[::-1]:
    nmin = ((nmin-1)//a+1)*a
    if(nmin>nmax):
        print(-1)
        break
    nmax = (nmax//a)*a+a-1
    last = a


else:
    print(nmin,nmax)

Submission status: Accepted

Solution code:
import sys,bisect as bs,collections as cl,heapq as hq
sys.setrecursionlimit(100000)
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False
k = onem()

a = l()

ma = 3
mi = 2

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

for i in range(1,k):
    if ma >= (-(-mi//a[-(i+1)])) * a[-(i+1)]:
        ma = ma//a[-(i+1)] * a[-(i+1)]  + a[-(i+1)] - 1
        mi = (-(-mi//a[-(i+1)])) * a[-(i+1)]
    else:
        print(-1)
        exit()
print(mi,ma)

Submission status: Accepted

Solution code:
k=int(input())
a=[int(j) for j in input().split()]
if a[-1]!=2:
    print(-1)
    exit()

import math

amin=2
amax=2
for i in range(k)[::-1]:
    amin+=(-amin)%a[i]
    amax-=amax%a[i]
    amax+=a[i]-1

if amin>amax:
    print(-1)
else:
    print(amin,amax)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

N = map(int,input().split())
A = [int(x) for x in input().split()]

low,high = 2,2

for a in A[::-1]:
    low += (-low)%a
    high -= high%a
    high += (a-1)

if low > high:
    print(-1)
else:
    print(low,high)

Submission status: Accepted

Solution code:
#B問題
import math
K = int(input())
A = list(map(int,input().split()))
B = A[::-1]

MIN = 0
MAX = 0
flag = 0
for i in range(K):
    if i == 0:
        if B[i] != 2:
            flag = 1
            break
        else:
            MIN = 2
            MAX = 3
    else:
        if MAX < B[i]:
            flag = 1
            break
        else:
            MAX = (MAX//B[i] + 1)*B[i]-1
            MIN = math.ceil(MIN/B[i])*B[i]
            if MAX < MIN:
                flag = 1
                break
if flag == 1:
    print(-1)
else:
    print(MIN,MAX)

Submission status: Accepted

Solution code:
import sys
import heapq
from operator import itemgetter
from collections import deque, defaultdict
from bisect import bisect_left, bisect_right
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
MOD = 10**9 + 7

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

    maxAns = 2
    minAns = 2

    for a in reversed(A):
        maxAns = maxAns // a * a + a - 1
        minAns = -(-minAns // a) * a

    resMax = maxAns
    resMin = minAns

    for a in A:
        resMax = resMax // a * a
        resMin = resMin // a * a

    if resMax == resMin == 2:
        print(minAns, maxAns)
    else:
        print(-1)

sol()

Submission status: Accepted

Solution code:
K=int(input())
*A, = map(int,input().split())
m,M = 2,2
for a in A[::-1]:
    M = M//a*a+(a-1)
    m = ((m-1)//a+1)*a
    if m>M:
        print(-1)
        break
else:print(m,M)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys  # {{{
import os
import time
import re
from pydoc import help
import string
import math
from operator import itemgetter
from collections import Counter
from collections import deque
from collections import defaultdict as dd
import fractions
from heapq import heappop, heappush, heapify
import array
from bisect import bisect_left, bisect_right, insort_left, insort_right
from copy import deepcopy as dcopy
import itertools
# }}}

# pre-defined{{{
sys.setrecursionlimit(10**7)
INF = 10**20
GOSA = 1.0 / 10**10
MOD = 10**9+7
ALPHABETS = [chr(i) for i in range(ord('a'), ord('z')+1)]  # can also use string module
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 DP(N, M, first): return [[first] * M for n in range(N)]
def DP3(N, M, L, first): return [[[first] * L for n in range(M)] for _ in range(N)]
from inspect import currentframe
# }}}

def local_input():# {{{
    from pcm.utils import set_stdin
    import sys
    from pathlib import Path
    parentdir = Path(os.path.dirname(__file__)).parent
    inputfile = parentdir.joinpath('test/sample-1.in')
    if len(sys.argv) == 1:
        set_stdin(inputfile)
# }}}

def solve():
    k = int(input())
    a = list(map(int,input().split()))
    
    mi = 2
    ma = 2

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

    for i in range(k):
        if ma < a[k-1-i]:
            print(-1)
            exit()
        
        ma = a[k-1-i]*(ma // a[k-1-i]) + a[k-1-i] - 1
        
        if a[k-1-i]*(ma // a[k-1-i]) < mi:
            print(-1)
            exit() 
                    
        mi = a[k-1-i]* ((mi -1)  // a[k-1-i] + 1)

    print(mi,ma)

    return 0

if __name__ == "__main__":# {{{
    try:
        local_input()
        def dump(*args):
            names = {id(v):k for k,v in currentframe().f_back.f_locals.items()}
            print(', '.join(names.get(id(arg),'???')+' => '+repr(arg) for arg in args), file=sys.stderr)
    except:
        def dump(*args):
            pass

    solve()

# vim: set foldmethod=marker:}}}

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    n = I()

    return

#B
def B():
    def f(m):
        if d[m] != None:
            return d[m]
        n = m
        for i in a:
            n = n-n%i
        d[m] = n
        return n
    k = I()
    a = LI()
    d = defaultdict(lambda : None)
    l,r = 0,1e19
    while r-l > 1:
        m = (r+l)//2
        if f(m) < 2:
            l = m
        else:
            r = m
    mi = r
    l,r = 0,1e19
    while r-l > 1:
        m = (r+l)//2
        if f(m) <= 2:
            l = m
        else:
            r = m
    ma = l
    if mi <= ma:
        if f(mi) != 2:
            print(-1)
        else:
            print(int(mi),int(ma))
    else:
        print(-1)
    return

#C
def C():
    n = I()

    return

#D
def D():
    n = I()

    return

#E
def E():
    n = I()

    return

#F
def F():
    n = I()

    return


#Solve
if __name__ == "__main__":
    B()

Submission status: Accepted

Solution code:
n=int(input())
arr=list(map(int,input().split()))
arr=arr[::-1]
if arr[0]!=2:
  print(-1)
else:
  arr=arr[1:]
  l=2
  r=3
  for val in arr:
    if (l<=val*(l//val)<=r) or (l<=val*(r//val)<=r):
      if l%val!=0:
        l=val*(l//val+1)
      r=val*(r//val+1)-1
    else:
      print(-1)
      break
  else:
    print(l,r)

Submission status: Accepted

Solution code:
k=int(input())
a=list(map(int,input().split()))
l=2
h=2
for i in a[::-1]:
    tl=i*-(-l//i)
    th=i*(h//i)
    if tl>h or th<l:
        print(-1)
        quit()
    else:
        l=tl
        h=th+i-1
print(l,h)

Submission status: Accepted

Solution code:
def main():
    K = int(input())
    A = list(map(int, input().split()))
    mn = 2
    mx = 2
    for a in reversed(A):
        if mx < a:
            print(-1)
            return
        mn = -(-mn//a) * a
        mx = mx // a * a + (a-1)
        if mx < mn:
            print(-1)
            return
    print(mn, mx)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
K=int(input())
A=list(map(int,input().split()))
MAX=2
MIN=2
A=A[::-1]
ans=0
for a in A:
    MIN=(MIN-1)//a*a+a
    if MIN>MAX:
        ans=-1
        break
    MAX=MAX//a*a+a-1
if ans==-1:
    print(-1)
else:
    print(str(MIN)+" "+str(MAX))

Submission status: Accepted

Solution code:
k = int(input())
a = list(map(int, input().split()))
if a[-1] != 2:
    print(-1)
    quit()
l, r = 2, 3
for i in range(k-2, -1, -1):
    s = ((l-1)//a[i]+1) * a[i]
    if s > r:
        print(-1)
        quit()
    t = ((r-1)//a[i]+1) * a[i]
    t = t-1 if t > r else t+a[i]-1
    l, r = s, t
print(l, r)