Problem:


Score : 200 points

Problem Statement

Given N integers A_1, ..., A_N, compute A_1 \times ... \times A_N.

However, if the result exceeds 10^{18}, print -1 instead.

Constraints

2 \leq N \leq 10^5

0 \leq A_i \leq 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 ... A_N

Output

Print the value A_1 \times ... \times A_N as an integer, or -1 if the value exceeds 10^{18}.

Sample Input 1

2
1000000000 1000000000

Sample Output 1

1000000000000000000

We have 1000000000 \times 1000000000 = 1000000000000000000.

Sample Input 2

3
101 9901 999999000001

Sample Output 2

-1

We have 101 \times 9901 \times 999999000001 = 1000000000000000001, which exceeds 10^{18}, so we should print -1 instead.

Sample Input 3

31
4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 4 3 3 8 3 2 7 9 5 0

Sample Output 3

0



Sample input:
2
1000000000 1000000000


Sample output:
1000000000000000000

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
ans = 1
if 0 in A:
    ans = 0
else:
    for a in A:
        ans *= a
        if ans > 10 ** 18:
            ans = -1
            break
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))



ans = 1
maxi = 10**18
if(0 in a):
    ans = 0
else:
    for i in range(n):
        ans *= a[i]
        if(ans > maxi):
            ans = -1
            break

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split(' ')))
res = 1
a = sorted(a)
for i in range(n):
    res = res * a[i]
    if(a[i] == 0):
      print(0)
      exit()
    if res > 1000000000000000000:
        print(-1)
        exit()
print(res)

Submission status: Accepted

Solution code:
import sys
a=int(input())
l=list(map(int,input().split()))
c=1
t=True
for i in l:
  if i==0:
    print(0)
    sys.exit()
  if t:
    c*=i
    if c>10**18:
      t=False
if t:
  print(c)
else:
  print(-1)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

LIMIT = 10 ** 18

ans = 1
if 0 in a:
    print(0)
    sys.exit(0)
for i in a:
    ans *= i
    if ans > LIMIT:
        print(-1)
        sys.exit(0)
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
A=sorted(list(map(int,input().split())))
ans=1
L=10**18

if A[0]==0:
  print(0)
  exit()

for i in range(N):
  ans*=A[i]
  if ans > L:
    print(-1)
    exit()

print(ans)

Submission status: Accepted

Solution code:
import sys
n=int(input())
a=list(map(int,input().split()))
max_num=10**18
ans = 1
if 0 in a:
    print(0)
    sys.exit()
for i in a:
    ans *= i
    if ans > max_num:
        ans = -1
        break
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = tuple(sorted(map(int, input().split())))
x = 1
for aa in a:
    x *= aa
    if x > 10 ** 18:
        print(-1)
        exit(0)
print(x)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
A=sorted(A)
INF=1e18
ans=1
f=0
for a in A:
    if ans*a>INF:
        f=1
        break
    ans*=a
if f==0:
    print(ans)
else:
    print(-1)

Submission status: Accepted

Solution code:
N = int(input())
A = sorted(list(map(int,input().split())))
answer = 1
for i in range(N):
    answer *= A[i]
    if answer > 10**18:
        answer = -1
        break
print(answer)

Submission status: Accepted

Solution code:
import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 19
MOD = 10 ** 9 + 7

N = INT()
A = LIST()

if A.count(0):
    print(0)
    exit()

ans = 1
for a in A:
    ans *= a
    if ans > 10**18:
        print(-1)
        exit()
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
INF = 10 ** 18

A.sort()
if A[0] == 0:
    print(0)
    exit()

cnt = 1
for a in A:
    cnt *= a
    if cnt > INF:
        print(-1)
        exit()
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
if 0 in a:
    print(0)
    exit()
x = 1
for i in a:
    x *= i
    if x > 10**18:
        print(-1)
        exit()
print(x)

Submission status: Accepted

Solution code:
N = int(input())
l = [int(x) for x in input().split()]
ans = 1
if l.count(0) > 0:
    print(0)
    exit()
for i in range(N):
    ans = ans * l[i]
    if ans > 10**18:
        print(-1)
        exit()
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int, input().split()))
 
ans = 1
if 0 in l:
  print(0)
  exit()
for i in range(n):
    ans *= l[i]
    if ans > 10**18:
        print(-1)
        exit()
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
aes = list(map(int, input().split()))

ans = 1

if 0 in aes:
  print(0)
else:
  for a in aes:
    ans *= a
    if ans > 10 ** 18:
      ans = -1
      break
  print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = 1
a.sort()
for i in range(0, n):
    if ans * a[i] <= 1_000_000_000_000_000_000:
        ans *= a[i]
    else:
        ans = 1_000_000_000_000_000_001
        break
if ans <= 1_000_000_000_000_000_000:
    print(ans)
else:
    print(-1)

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
if 0 in A:
    print(0)
    exit()
ans=1
for i in range(n):
    ans*=A[i]
    if ans>10**18:
        print(-1)
        exit()
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
a = [int(i) for i in input().split()]

ans = 1
a.sort()
for i in range(n):
    ans *= a[i]
    #print(ans)
    if ans > pow(10, 18):
        print(-1)
        exit()
print(ans)

Submission status: Accepted

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

prod = 1;
for a in A:
  prod *= a
  if prod < 0 or prod > 1000000000000000000:
    prod = -1

print(prod)

Submission status: Accepted

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

for a in A:
    if a == 0:
        print(0)
        exit()

res = 1
for a in A:
    res = res * a
    if res > 10e17:
        res = -1
        break

print(res)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
import numpy as np

n = int(input())
A = [int(a) for a in input().split()]
mul = 1
top = 1000000000000000000
if 0 in A:
    print(0)

else:
    for a in A:
        mul *= a
        if mul > top:
            mul =-1
            break
    print(mul)

Submission status: Accepted

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

for i in a:
    ans *= i
    if ans>10**18:
        ans = -1
        break
if 0 in a:
    ans =0
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
a = [int(x) for x in input().split()]
ans = 1
if 0 in a:
    print(0)
    sys.exit()
for i in a:
    ans *= i
    if ans > 10**18:
        print(-1)
        sys.exit()
print(ans)

Submission status: Accepted

Solution code:
import sys; input = sys.stdin.buffer.readline
from collections import defaultdict
con = 10 ** 9 + 7; INF = float("inf")

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

#処理内容
def main():
	N = int(input())
	A = getlist()
	X = 10 ** 18
	ans = 1
	if 0 in A:
		print(0)
		return

	for i in range(N):
		ans *= A[i]
		if ans > X:
			print(-1)
			return

	print(ans)

if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
import sys

input = lambda: sys.stdin.readline().rstrip()


def solve():
    N = int(input())
    A = list(map(int, input().split()))
    ans = 1
    over = 10 ** 18

    for i in range(N):
        if A[i] == 0:
            print(0)
            exit()

    for i in range(N):
        ans = ans * A[i]
        if ans > over:
            print('-1')
            exit()

    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
ans = 1
b = 10**18
for ai in A:
    if ai == 0:
        ans = 0
        break
    if ans < 0:
        continue
    ans *= ai
    if ans > b:
        ans = -1
        
print(ans)

Submission status: Accepted

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
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 = []
	if len(x) == 0:
		return []
	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

def pow_k(x, n):
    ans = 1
    while n:
        if n % 2:
            ans *= x
        x *= x
        n >>= 1
    return ans

"""
def nibu(x,n,r):
	ll = 0
	rr = r
	while True:
		mid = (ll+rr)//2

	if rr == mid:
		return ll
	if (ここに評価入れる):
		rr = mid
	else:
		ll = mid+1
"""

n = onem()

a = l()

ans = a[0]

for i in range(1,n):
	if ans == -1:
		if a[i] == 0:
			ans *= 0
			break
	if ans != -1:
		ans *= a[i]
	if ans > 10**18:
		ans = -1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))

ans = 1
for i in range(n):
    if a[i] == 0:
        print(0)
        exit()
for i in range(n):
    ans *= a[i]
    if ans > 1000000000000000000:
        print(-1)
        exit()
print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**8)
input = sys.stdin.readline

N = int(input())
A = list(map(int,input().split()))
A.sort()
ans = 1
for a in A:
    ans *= a
    if ans>10**18:
        print(-1)
        exit()

if ans>10**18:
    print(-1)
else:
    print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
ans = 1
for i in range(N):
  ans  *= A[i]
  if ans > 10**18:
      if not 0 in A:
          ans = -1
          break
      else:
          ans = 0
          break
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
a.sort()
ans = 1
for v in a:
  ans *= v
  if ans > 10 ** 18:
    ans = -1
    break
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
ara = list(map(int, input().split()))
ara.sort()
ret = 1

mx = 10**18

for val in ara:
	ret *= val
	if ret > mx:
		print(-1)
		exit(0)

print(ret)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
ans = 1
for i in range(n):
    ans *= A[i]
    if ans > 10**18:
        ans = -1
        break
if 0 in A:
    ans = 0
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin

rInt       = lambda : int(stdin.readline().strip("\n"))
rIntArray  = lambda : [int(x) for x in stdin.readline().split()]

rString    = lambda : stdin.readline().strip("\n")
rStrArray  = lambda : [x for x in stdin.readline().split()]

# for _ in range(rInt()):

n = rInt()
arr = rIntArray()

flag = False

for i in arr:
	if i == 0:
		flag = True

prod = 1
for i in arr:
	prod *= i
	if prod > 10**18:
		print("0" if flag else "-1")
		quit()

print(prod)

Submission status: Accepted

Solution code:
N = int(input())
s = 1
A = sorted([int(a) for a in input().split()])
for a in A:
    s *= a
    if s > 10 ** 18:
        print(-1)
        break
else:
    print(s)

Submission status: Accepted

Solution code:
import sys
import math
def I():return int(sys.stdin.readline().replace("\n",""))
def I2():return map(int,sys.stdin.readline().replace("\n","").split())
def S():return str(sys.stdin.readline().replace("\n",""))
def L():return list(sys.stdin.readline().replace("\n",""))
def Intl():return [int(k) for k in sys.stdin.readline().replace("\n","").split()]
def Lx(k):return list(map(lambda x:int(x)*-k,sys.stdin.readline().replace("\n","").split()))

n = I()
a = Intl()
s = 1
a.sort()
for i in range(n):
    s *= a[i]
    if s > 10**18:
        print(-1)
        exit()
print(s)

Submission status: Accepted

Solution code:
n=int(input())
ans = 1
a=list(map(int, input().split()))
if 0 not in a:
    for i in a:
        ans*=i
        if ans>10**18:
            ans = -1
            break
    print(ans)
else:
    print(0)

Submission status: Accepted

Solution code:
import sys

N = int(input())
A = list(input().split())
A = list(sorted([int(A[i]) for i in range(N)], reverse=True))
ans = 1

if 0 in A:
    print(0)
    sys.exit()
else:
    for i in range(N):
        ans = ans * A[i]
        if ans > 10 ** 18:
            print(-1)
            sys.exit()

print(ans)

Submission status: Accepted

Solution code:
import sys


def main():
  readline = sys.stdin.readline
  
  n = int(readline().rstrip())
  a = [int(a_i) for a_i in readline().rstrip().split()]
  
  if 0 in a:
    print(0)
    return
  
  pr = 1
  for a_i in a:
    pr *= a_i
    
    if pr > 10 ** 18:
      print(-1)
      return
    
  print(pr)
  
  
if __name__ == "__main__":
  main()

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = 1
x = 1000000000000000000
for i in range(n):
    if a[i] == 0:
        print(0)
        exit()
for i in range(n):
    ans *= a[i]
    if ans > x:
        print(-1)
        exit()
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
m = 1
if 0 in a:
    print(0)
    exit()
for i in range(len(a)):
    m = m * a[i]
    if m > 10 ** 18:
        print(-1)
        exit()
print(m)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=1
if a.count(0)==0:
  for i in range(n):
    if ans*a[i]>10**18:
      print(-1)
      break
    else:
      ans*=a[i]
  else:
    print(ans)
else:
  print(0)

Submission status: Accepted

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

limit = 10**18
ans = 1
if 0 in A:
    print(0)
else:
    for one in A:
        ans*=one
        if ans>limit:
            ans=-1
            break
    print(ans)

Submission status: Accepted

Solution code:
import sys
n = int(input())
a = list(map(int,input().split()))
ans = 1
c = 10**18
if 0 in a:
    print(0)
else:
    for i in a:
        ans *= i
        if ans > c:
            print(-1)
            sys.exit()
    print(ans)

Submission status: Accepted

Solution code:
import collections
N = int(input())
A = list(map(int, input().split()))

cnt = collections.Counter(A)

ans = 1
if cnt[0] == 1:
    print(0)
    exit()
else:
    for i in A:
        ans = ans * i
        if ans > 10**18:
            print(-1)
            exit()
print(ans)

Submission status: Accepted

Solution code:
import sys
n = int(input())
ls = list(map(int,input().split()))
ls.sort()
a = 1
for i in range(n):
    a *= ls[i]
    if a > 10**18:
        print("-1")
        sys.exit()
    elif a == 0:
        print("0")
        sys.exit()
print(a)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(x) for x in input().split()]
if 0 in a:
    print(0)
    exit()
ans = 1
for x in a:
  ans *= x
  if ans > 10**18:
    print(-1)
    exit()
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = a[0]
if a.count(0) >= 1:
    ans = 0

for i in range(1, n):
    ans *= a[i]
    if ans > 10**18:
        ans = -1
        break

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
k = 0 in A
flag = True
ans = 1
if k:
  print(0)
else:
  for i in range(N):
    ans *= A[i]
    if ans > 10**18:
      flag = False
      break

  if flag:
    print(ans)
  else:
    print(-1)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
ans = 1
lim = 10**18
if 0 in A:
    print(0)
else:
    for k in range(N):
        ans *= A[k]
        if ans > lim:
            print(-1)
            break
    else:
        print(ans)

Submission status: Accepted

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

if A[0] == 0:
    print(0)
    exit()
else:
    ans = 1
    for i in range(N):
        ans *= A[i]
        if ans > 10**18:
            print(-1)
            exit()
    print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = 1
if 0 in a:
    b = 0
else:
    for i in range(n):
        b *= a[i]
        if b > 10**18:
            b = -1
            break;
print(b)

Submission status: Accepted

Solution code:
N = int(input())
A_list = list(map(int, input().split()))

if any(a == 0 for a in A_list):
	print(0)
else:
	prod = 1
	for a in A_list:
		prod *= a
		if prod > 10 ** 18:
			prod = -1
			break
	print(prod)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
ans = 1

if 0 in A:
    print(0)
    exit()
for i in range(N):
    ans *= A[i]
    if ans > 10**18:
        print(-1)
        exit()

print(ans)

Submission status: Accepted

Solution code:
N = input()
A = list(map(int,input().split()))
x = 1
b = 0

A.sort()

for n in A:
    x*=n
    if x > 10**18:
        print(-1)
        b = 1
        break
    if x == 0:
        print(0)
        b = 1
        break
if b == 0:
    print(x)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = 1
if 0 in a:
    print(0)
    exit()
for i in range(n):
    ans *= a[i]
    if ans > 10 ** 18:
        print(-1)
        exit()
print(ans)

Submission status: Accepted

Solution code:
from math import gcd

from math import factorial as f

from math import ceil,floor,sqrt
import math

import bisect
import re
import heapq


from copy import deepcopy
import itertools
from itertools import permutations

from sys import exit

ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))


yes = "Yes"
no = "No"


def main():
    n = ii()
    a = li()
    BIG = 10**18
    ans = a[0]
    if 0 in a:
        print(0)
        exit()
    for i in range(1,n):
        if BIG//a[i] < ans:
            print(-1)
            exit()
        else:
            ans*= a[i]
        if ans > BIG:
            print(-1)
            exit()
    print(ans)


main()

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
ans = 1
if 0 in a:
  print("0")
  exit()
for i in range(n):
    ans *= a[i]
    if ans >10**18:
      print("-1")
      exit()
    
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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

if 0 in As:
    t = 0
else:
    t = 1
    for A in As:
        t *= A
        if t > 10 ** 18:
            t = -1
            break

print(t)

Submission status: Accepted

Solution code:
# coding: utf-8


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

    if 0 in A:
        return 0

    ans = 1
    for a in A:
        ans *= a
        if ans > 10**18:
            return -1
    
    return ans


print(main())

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
ans = 1
if 0 in A:
    print(0)
else:
    for i in range(n):
        ans *= A[i]
        if ans > 10 ** 18:
            print(-1)
            break
    else:
        print(ans)

Submission status: Accepted

Solution code:
import numpy as np
def main():
  N = int(input())
  A = list(map(int, input().split()))
  ans = 1
  for i in range(N):
    ans *= A[i]
    if ans > 10**18:
      ans = -1
      break
  if 0 in A:
    ans = 0
  print(ans)
if __name__  == "__main__":
  main()

Submission status: Accepted

Solution code:
n = int(input())
ls = list(map(int,input().strip().split()))
maxi = 10**18
ans = 1
zc = ls.count(0)
if zc>0: print(0)
else:
    for val in ls:
        ans *=val
        if ans>maxi:
            ans = -1
            break
    print(ans)

Submission status: Accepted

Solution code:
#B - Multiplication 2
N = int(input())
A = list(map(int, input().split()))
A.sort()
ans = 1
for i in range(N):
    if A[i] == 0:
        print(0)
        exit()
    else:
        pass
    ans *= A[i]
    if ans <= 1000000000000000000:
        pass
    else:
        print(-1)
        exit()
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys

N, *A = map(int, sys.stdin.buffer.read().split())

answer = 1
for a in A:
  answer *= a
  if answer>10**18 or answer<0:
    answer = -1

print(answer)

Submission status: Accepted

Solution code:
import sys
import heapq
import re
from itertools import permutations
from bisect import bisect_left, bisect_right
from collections import Counter, deque
from math import factorial, sqrt, ceil, gcd
from functools import lru_cache, reduce
INF = 1 << 60
MOD = 1000000007
sys.setrecursionlimit(10 ** 7)

# UnionFind
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

# ダイクストラ
def dijkstra_heap(s, edge, n):
    #始点sから各頂点への最短距離
    d = [10**20] * n
    used = [True] * n #True:未確定
    d[s] = 0
    used[s] = False
    edgelist = []
    for a,b in edge[s]:
        heapq.heappush(edgelist,a*(10**6)+b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        #まだ使われてない頂点の中から最小の距離のものを探す
        if not used[minedge%(10**6)]:
            continue
        v = minedge%(10**6)
        d[v] = minedge//(10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
    return d

# 素因数分解
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr

# 2数の最小公倍数
def lcm(x, y):
    return (x * y) // gcd(x, y)

# リストの要素の最小公倍数
def lcm_list(numbers):
    return reduce(lcm, numbers, 1)

# リストの要素の最大公約数
def gcd_list(numbers):
    return reduce(gcd, numbers)

# 素数判定
def is_prime(n):
    if n <= 1:
        return False
    p = 2
    while True:
        if p ** 2 > n:
            break
        if n % p == 0:
            return False
        p += 1
    return True


# limit以下の素数を列挙
def eratosthenes(limit):
    A = [i for i in range(2, limit+1)]
    P = []

    while True:
        prime = min(A)
        
        if prime > sqrt(limit):
            break
            
        P.append(prime)
            
        i = 0
        while i < len(A):
            if A[i] % prime == 0:
                A.pop(i)
                continue
            i += 1
            
    for a in A:
        P.append(a)
            
    return P

# 同じものを含む順列
def permutation_with_duplicates(L):

    if L == []:
        return [[]]

    else:
        ret = []

        # set（集合）型で重複を削除、ソート
        S = sorted(set(L))

        for i in S:

            data = L[:]
            data.remove(i)

            for j in permutation_with_duplicates(data):
                ret.append([i] + j)

        return ret


# ここから書き始める
n = int(input())
a = list(map(int, input().split()))
ans = 1
if 0 in a:
    print(0)
    sys.exit()
for i in a:
    ans *= i
    if ans > 10 ** 18:
        print(-1)
        break
else:
    print(ans)

Submission status: Accepted

Solution code:
N = int(input())
al = list(map(int,input().split()))

ans = 1
if 0 in al:
    print('0')
    exit()
else:
    for i in al:
        ans *= i
        if ans > 10**18:
            print('-1')
            exit()

    print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A  = list(map(int,(input().split())))
    con = 1
    m = 10**18
    
    for a in A:
        if(a==0):
            print(0)
            return

    for a in A:
        con*=a
        if con > m :
            print(-1)
            return
    print(con)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#デフォルト
import itertools
from collections import defaultdict
import collections
import math
import sys
sys.setrecursionlimit(200000)
mod = 1000000007

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

ans = 1
sub = 10 ** 18
for i in range(n):
    if a[i] == 0:
        print("0")
        exit()
for i in a:
    ans *= i
    if ans > sub:
        ans = -1
        break

print(ans)

Submission status: Accepted

Solution code:
n =int(input())
As = list(map(int,input().split()))
judge = False

for i in range(n):
    if As[i] == 0 :
        judge = True

ans = 1
if not judge:
    for i in range(n):
        ans *= As[i]
        if ans > 10**18:
            ans = -1
            break

if judge:
    print(0)
else:
    print(ans)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int, input().split()))
ans=1
if 0 in A:
  print(0)
  exit()
  
for i in A:
  ans*=i
  if ans>10**18:
    print(-1)
    exit()
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from collections import *

N = int(input())
A = list(map(int, input().split()))
A.sort()
ans = 1

for Ai in A:
    ans *= Ai
    
    if ans>10**18:
        print(-1)
        exit()
        
print(ans)

Submission status: Accepted

Solution code:
S = int(input())
a = list(map(int,input().split()))
ans = 1

if 0 in a:
    print(0)
    exit()
else:
    for i in range(S):
        ans*=a[i]
        if ans > 10**18:
            print(-1)
            exit()

print(ans)

Submission status: Accepted

Solution code:
n=int(input())

a=[int(_) for _ in input().split()]

ans=1
if 0 in a:
    ans*=0

for i in range(n):
    ans *=a[i]
    if ans>10**18:
        ans=-1
        break

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
As = list(map(int, input().split()))
ans = 1
if 0 in As:
    print(0)
else:
    for a in As:
        ans *= a
        if ans>10**18:
            break
    if ans>10**18:
        print(-1)
    else:
        print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().rstrip().split(" ")))
flag = True
ans = 1
for i in range(N):
    if A[i] == 0:
        ans = 0
for i in range(N):
    ans *= A[i]
    if ans > 10 ** 18:
        flag = False
        break
if flag:
    print(ans)
else:
    print(-1)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=1
a=sorted(a)
for i in range(n):
    ans*=a[i]
    if ans>10**18:
        print(-1)
        quit()
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin, stdout
n = int(stdin.readline())
nums = list(map(int, stdin.readline().strip().split()))

for num in nums:
    if num == 0:
        print(0)
        exit()
        
if max(nums)>10**18:
    print(-1)
else:
    nums.sort(reverse=True)

    ans = 1
    for num in nums:
        ans*=num
        if ans > 10**18:
            ans = -1
            break
    
    print(ans)

Submission status: Accepted

Solution code:
n = int(input())
li = list(map(int,input().split()))
ans = 1
zero = 0
for ele in li:
	if ele == 0:
		zero = 1
		break
if zero == 1:
	print(0)
else:
	ans = 1
	for ele in li:
		ans = ans * ele
		if ans > 1000000000000000000:
			ans = -1
			break
	print(ans)

Submission status: Accepted

Solution code:
def main():
    k = int(input())
    a = list(map(int, input().split()))
    ret = 1
    if 0 in set(a):
        print(0)
        return
    for _a in a:
        ret *= _a
        if ret > 10**18:
            print(-1)
            return
    print(ret)

if __name__=="__main__":
    main()

Submission status: Accepted

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

N=int(readline())
A=list(map(int,readline().split()))
if 0 in A:
  print(0)
  exit(0)
limit = 10 ** 18

ans = 1
for a in A:
  ans *= a
  if ans > limit:
    print(-1)
    exit(0)
else:
  print(ans)

Submission status: Accepted

Solution code:
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
N = I()
A = LI()
if 0 in A:
    print(0)
    exit()
ans = 1
for a in A:
    ans *= a
    if ans>10**18:
        print(-1)
        break
else:
    print(ans)

Submission status: Accepted

Solution code:
n=int(input())

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


s=1
for i in range(n):
    s=s*A[i]
    if s>10**18:
        s=-1
        break

if 0 in A:
    s=0
print(s)

Submission status: Accepted

Solution code:
n=int(input())
lis=list(map(int,input().split()))
keep=0
ans=1
for i in range(n):
  ans*=lis[i]
  if ans>10**18:
    keep=1
    ans=0
  if lis[i]==0:
    keep=0
if keep==0:
  print(ans)
else:
  print("-1")

Submission status: Accepted

Solution code:
def resolve():
    int(input())
    A = [int(i) for i in input().split()]
    ans = A[0]
    mmax = 10**18
    if 0 in A:
        print(0)
        return
    for a in A[1:]:
        ans = ans * a
        if ans > mmax:
            print(-1)
            return
    print(ans)


resolve()

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
ans=1
if min(l)==0:
  print(0)
  quit()
for x in l:
  ans=ans*x  
  if ans>10**18:
    print(-1)
    quit()
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
ans = 1
inf = 10 ** 18 + 1
if 0 in a:
    print(0)
    exit()
for x in a:
    ans *= x
    if ans >= inf:
        print(-1)
        exit()
print(ans)

Submission status: Accepted

Solution code:
import sys

n = int(input())
a = [int(x) for x in input().split()]

if 0 in a:
    print(0)
    sys.exit()

ans = a[0]
for i in range(1, n):
    ans *= a[i]
    if 10**18 < ans:
        print(-1)
        sys.exit()
    
print(ans)