Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

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

ans = 0
seat = [[min(i, j, n-i-1, n-j-1) for j in range(n)] for i in range(n)]
reserved = [[True for j in range(n)] for i in range(n)]

vec = [(-1, 0), (1, 0), (0, -1), (0, 1)]


def bfs(x, y):
    q = [(x, y)]
    while q:
        x, y = q.pop()
        for dx, dy in vec:
            x1 = x+dx
            y1 = y+dy
            if 0 <= x1 < n and 0 <= y1 < n and seat[x1][y1] > seat[x][y] + reserved[x][y]:
                seat[x1][y1] = seat[x][y] + reserved[x][y]
                q.append((x1, y1))


for i in range(n**2):
    v = p[i]-1
    x, y = v//n, v % n
    reserved[x][y] = False
    ans += seat[x][y]
    bfs(x, y)

print(ans)


### FIXED CODE ###

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

ans = 0
seat = [[min(i, j, n-i-1, n-j-1) for j in range(n)] for i in range(n)]
reserved = [[True for j in range(n)] for i in range(n)]

vec = [(-1, 0), (1, 0), (0, -1), (0, 1)]

for i in range(n**2):
    v = p[i]-1
    x, y = v//n, v % n
    reserved[x][y] = False
    ans += seat[x][y]
    q = [(x, y)]
    while q:
        x, y = q.pop()
        if 0 <= x+1 < n and 0 <= y < n and seat[x+1][y] > seat[x][y] + reserved[x][y]:
            seat[x+1][y] = seat[x][y] + reserved[x][y]
            q.append((x+1, y))
        if 0 <= x-1 < n and 0 <= y < n and seat[x-1][y] > seat[x][y] + reserved[x][y]:
            seat[x-1][y] = seat[x][y] + reserved[x][y]
            q.append((x-1, y))
        if 0 <= x < n and 0 <= y+1 < n and seat[x][y+1] > seat[x][y] + reserved[x][y]:
            seat[x][y+1] = seat[x][y] + reserved[x][y]
            q.append((x, y+1))
        if 0 <= x < n and 0 <= y-1 < n and seat[x][y-1] > seat[x][y] + reserved[x][y]:
            seat[x][y-1] = seat[x][y] + reserved[x][y]
            q.append((x, y-1))


print(ans)
### BUGGY CODE ###

x,y,z,k = (int(x) for x in input().split())
a = list(reversed(sorted([int(x) for x in input().split()])))
b = list(reversed(sorted([int(x) for x in input().split()])))
c = list(reversed(sorted([int(x) for x in input().split()])))
ra = [a[0]-x for x in a]
rb = [b[0]-x for x in b]
rc = [c[0]-x for x in c]
ia = 0
ib = 0
ic = 0
while (ia+1)*(ib+1)*(ic+1) < k:
    na = ra[ia+1] if ia+1<x else 10000000001
    nb = rb[ib+1] if ib+1<y else 10000000001
    nc = rc[ic+1] if ic+1<z else 10000000001
    if na <= nb and na <= nc:
        ia += 1
    if nb <= na and nb <= nc:
        ib += 1
    if nc <= na and nc <= nb:
        ic += 1
cp = ra[ia]+rc[ib]+rc[ic]
ca = len([n for n in ra if n <= cp])
cb = len([n for n in rb if n <= cp])
cc = len([n for n in rc if n <= cp])
nums = []
for va in ra[0:ca]:
    for vb in rb[0:cb]:
        for vc in rc[0:cc]:
            nums.append(va+vb+vc)
nums.sort()
mx = a[0]+b[0]+c[0]
for num in nums[0:k]:
    print(mx-num)


### FIXED CODE ###

import heapq
x,y,z,k = (int(x) for x in input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
c = [int(x) for x in input().split()]
a.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)
# heap
h = []
g = set()
heapq.heappush(h, (-(a[0]+b[0]+c[0]),0,0,0))
for i in range(k):
    q,ia,ib,ic = heapq.heappop(h)
    print(-q)
    ja,jb,jc = ia+1,ib,ic
    if ja<x and (ja,jb,jc) not in g:
        heapq.heappush(h, (-(a[ja]+b[jb]+c[jc]),ja,jb,jc))
        g.add((ja,jb,jc))
    ja,jb,jc = ia,ib+1,ic
    if jb<y and (ja,jb,jc) not in g:
        heapq.heappush(h, (-(a[ja]+b[jb]+c[jc]),ja,jb,jc))
        g.add((ja,jb,jc))
    ja,jb,jc = ia,ib,ic+1
    if jc<z and (ja,jb,jc) not in g:
        heapq.heappush(h, (-(a[ja]+b[jb]+c[jc]),ja,jb,jc))
        g.add((ja,jb,jc))
### BUGGY CODE ###

n = int(input())
s=['' for _ in range(n)]
for i in range(n):
    s[i]=input()
#x=[[0,0] for _ in range(n)]
hp,hm=0,0
p=[]
m=[]
for i in range(n):
    t=s[i]
    lv=0
    mn=0
    for u in t:
        if u == '(':
            lv+=1
        elif u == ')':
            lv-=1
        mn=min(mn,lv)
    #x[i][0],x[i][1]=lv,mn
    if lv>=0:
        p.append([lv,mn])
        hp+=lv
    else:
        lv*=-1
        m.append([lv,mn+lv])
        hm+=lv
if hp!=hm:
    print('No')
    exit()
p.sort(key=lambda x: x[1],reverse=1)
m.sort(key=lambda x: x[1],reverse=1)
h=0
lv,mn=0,0
for a,b in p:
    lv+=a
    mn+=b
    if mn<0:
        print('No')
        exit()
    mn=lv
lv,mn=0,0
for a,b in m:
    lv+=a
    mn+=b
    if mn<0:
        print('No')
        exit()
    mn=lv
print('Yes')

#print(lv,mn)
    

### FIXED CODE ###

n = int(input())
s=['' for _ in range(n)]
for i in range(n):
    s[i]=input()
#x=[[0,0] for _ in range(n)]
hp,hm=0,0
p=[]
m=[]
for i in range(n):
    t=s[i]
    lv=0
    mn=0
    for u in t:
        if u == '(':
            lv+=1
        elif u == ')':
            lv-=1
        mn=min(mn,lv)
    #x[i][0],x[i][1]=lv,mn
    if lv>=0:
        p.append([lv,mn])
        hp+=lv
    else:
        lv*=-1
        m.append([lv,mn+lv])
        hm+=lv
if hp!=hm:
    print('No')
    exit()
p.sort(key=lambda x: x[1],reverse=1)
m.sort(key=lambda x: x[1],reverse=1)
h=0
lv,mn=0,0
for a,b in p:
    lv+=a
    mn+=b
    if mn<0:
        print('No')
        exit()
    mn=lv
lv,mn=0,0
for a,b in m:
    lv+=a
    mn+=b
    if mn<0:
        print('No')
        exit()
    mn=lv
print('Yes')

#print(lv,mn)
### BUGGY CODE ###

def main():
    s = input()
    mod = 1000000007
    n = len(s)
    dp = [[0]*13 for i in range(n+1)]
    dp[0][0] = 1
    k = 1
    cnt = 0
    for i in range(n):
        t = 0
        for j in range(10):
            if s[n-i-1]=="?":
                for l in range(13):
                    dp[i+1][(l+t)%13]+=dp[i][l]
                    if cnt%1000==0:
                        dp[i+1][(l+t)%13]%=mod
            elif int(s[n-i-1])==j:
                for l in range(13):
                    dp[i+1][(l+t)%13]+=dp[i][l]
                    if cnt%1000==0:
                        dp[i+1][(l+t)%13]%=mod
            t+=k
            if t>=13:
                t-=13
            cnt+=1
        k=t
    print(dp[n][5]%mod)

if __name__ == '__main__':
    main()

### FIXED CODE ###

s = input()
mod = 1000000007
n = len(s)
dp = [[0]*13 for i in range(n+1)]
dp[0][0] = 1
k = 1
for i in range(n):
    t = 0
    for j in range(10):
        if s[n-i-1]=="?":
            for l in range(13):
                dp[i+1][(l+t)%13]+=dp[i][l]
                dp[i+1][(l+t)%13]%=mod
        elif int(s[n-i-1])==j:
            for l in range(13):
                dp[i+1][(l+t)%13]+=dp[i][l]
                dp[i+1][(l+t)%13]%=mod
        t+=k
        t%=13
    k=t
print(dp[n][5])
### BUGGY CODE ###

s = int (input ())
l = []
a = 0
l.append (s)
while True:
  a += 1
  if s%2 == 0:
    s = round(s/2)
  else:
    s = s*3+1
  if l.count (s) != 0:
    print (a+1)
    break
  

### FIXED CODE ###

s = int (input ())
l = []
a = 0
l.append (s)
for i in range (1000000):
  a += 1
  if s%2 == 0:
    s = round(s/2)
  else:
    s = s*3+1
  if l.count (s) != 0:
    print (a+1)
    break
  else:
    l.append (s)
### BUGGY CODE ###

#!/usr/bin/env python3
from collections import deque


H, W = [int(str) for str in input().strip().split()]
A = [list(input().strip()) for _ in range(H)]


def bfs(starts):
    moves = ((1, 0), (0, 1), (-1, 0), (0, -1))
    dists = [[-1] * W for _ in range(H)]
    que = deque()
    for start in starts:
        dists[start[0]][start[1]] = 0
        que.append(start)
    while len(que):
        ch, cw = que.popleft()
        for dh, dw in moves:
            nh, nw = ch + dh, cw + dw
            if (    0 <= nh < H 
                and 0 <= nw < W 
                and dists[nh][nw] == -1):
                dists[nh][nw] = dists[ch][cw] + 1
                que.append((nh, nw))
    return max([max(i) for i in dists])


def solve():
    starts = ((i, j) for i in range(H) for j in range(W) if A[i][j] == '#')
    print(bfs(starts))


solve()


### FIXED CODE ###

#!/usr/bin/env python3
from collections import deque


H, W = [int(str) for str in input().strip().split()]
A = [list(input().strip()) for _ in range(H)]


def bfs(starts):
    moves = ((1, 0), (0, 1), (-1, 0), (0, -1))
    dists = [[-1] * W for _ in range(H)]
    max_dist = 0
    que = deque()
    for start in starts:
        dists[start[0]][start[1]] = 0
        que.append(start)
    while len(que):
        ch, cw = que.popleft()
        for dh, dw in moves:
            nh, nw = ch + dh, cw + dw
            if (    0 <= nh < H 
                and 0 <= nw < W 
                and dists[nh][nw] == -1):
                dists[nh][nw] = dists[ch][cw] + 1
                que.append((nh, nw))
                max_dist = max(max_dist, dists[nh][nw])
    return max_dist


def solve():
    starts = ((i, j) for i in range(H) for j in range(W) if A[i][j] == '#')
    print(bfs(starts))


solve()
### BUGGY CODE ###

import sys
from functools import lru_cache
sys.setrecursionlimit(10**9)

@lru_cache(None)
def f(n,a,b,c,d):
  if n==0:return 0
  if n==1:return d
  if n==2:return min(d*2,a+d)
  if n==3:return min(d*3,d+a+d,b+d)
  if n==4:return min(d*4,a+a+d,d+b+d,d+c+d)
  if n==5:return min(d*5,d+a+a+d,d+d+b+d,d+a+b+d,c+d)
  if n==6:return min(d*6,a+b+d,a+d+a+d,b+d+b+d,d+c+d)
  # print(n,a,b,c,d)
  p=n*d
  for i in range(-10, 11):
    nn=n+i
    if nn<0:continue
    if nn%2==0and nn//2<n:p=min(p,f(nn//2,a,b,c,d)+abs(i)*d+a)
    if nn%3==0and nn//3<n:p=min(p,f(nn//3,a,b,c,d)+abs(i)*d+b)
    if nn%5==0and nn//5<n:p=min(p,f(nn//5,a,b,c,d)+abs(i)*d+c)
  return p

for _ in range(int(input())):
  n,a,b,c,d = map(int,input().split())
  print(f(n,a,b,c,d))

### FIXED CODE ###

from functools import lru_cache

def solve(n,a,b,c,d):
    @lru_cache(None)
    def rec(cur):
        if cur == 0:    return 0
        if cur == 1:    return d
        ret = cur * d
        p,q = divmod(cur, 2)
        if q == 0:  ret = min(ret, rec(p) + a)
        if q == 1:  ret = min(ret, rec(p) + a + d, rec(p + 1) + a + d)
        p,q = divmod(cur, 3)
        if q == 0:  ret = min(ret, rec(p) + b)
        if q == 1:  ret = min(ret, rec(p) + b + d)
        if q == 2:  ret = min(ret, rec(p) + b + d * 2, rec(p + 1) + b + d)
        p,q = divmod(cur, 5)
        if q == 0:  ret = min(ret, rec(p) + c)
        if q == 1:  ret = min(ret, rec(p) + c + d)
        if q == 2:  ret = min(ret, rec(p) + c + d * 2)
        if q == 3:  ret = min(ret, rec(p) + c + d * 3, rec(p + 1) + c + d * 2)
        if q == 4:  ret = min(ret, rec(p) + c + d * 4, rec(p + 1) + c + d)
        return ret

    return rec(n)

for _ in range(int(input())):
    n,a,b,c,d = map(int,input().split())
    print(solve(n,a,b,c,d))
### BUGGY CODE ###

import sys
N,Q = map(int,sys.stdin.readline().rstrip().split())
S = sys.stdin.readline().rstrip()
lr = [list(map(int,sys.stdin.readline().rstrip().split())) for _ in range(Q)]
for i in lr:  
  print(S[i[0]-1:i[1]].count("AC"))


### FIXED CODE ###

import sys
N,Q = map(int,sys.stdin.readline().rstrip().split())
S = sys.stdin.readline().rstrip()
D = [0]
DD = 0
for i in range(1,len(S)):
  if S[i-1] + S[i] == "AC":
    DD += 1
  D.append(DD)
for _ in range(Q):
  l,r  = map(int,sys.stdin.readline().rstrip().split())
  print(D[r-1]- D[l-1])
  del l
  del r
### BUGGY CODE ###

N = int(input())
A_array = [list(map(int, input().split())) for _ in range(N)]

# print(A_array)

s_index = [0] * N

ans_date = 1
finish_player = 0
match_count = 0
M = N * (N-1) // 2
while(1):
    flag = 0
    match_array = [0] * N
    for i in range(N):
        if match_array[i] == 1 or s_index[i] == N - 1:
            continue
        vs = A_array[i][s_index[i]] - 1
        # print(i,vs)
        if match_array[vs] != 1 and A_array[vs][s_index[vs]] - 1 == i:
            match_count += 1
            match_array[i] = 1
            s_index[i] += 1
            match_array[vs] = 1
            s_index[vs] += 1
            flag = 1
    if flag == 0:
        print(-1)
        exit()
    if match_count == M:
        break
    ans_date += 1
print(ans_date)


### FIXED CODE ###

import sys
input = sys.stdin.readline
N = int(input())
A_array = [list(map(int, input().split())) for _ in range(N)]

# print(A_array)

s_index = [0] * N

ans_date = 1
finish_player = 0
match_count = 0
M = N * (N-1) // 2
first_flag = 1
while(1):
    flag = 0
    match_array = [0] * N
    if first_flag:
        match_candidate = [i for i in range(N)]
        first_flag = 0
    next_match = []
    for i in match_candidate:
        if match_array[i] == 1 or s_index[i] == N - 1:
            continue
        vs = A_array[i][s_index[i]] - 1
        # print(i,vs)
        if match_array[vs] != 1 and A_array[vs][s_index[vs]] - 1 == i:
            match_count += 1
            match_array[i] = 1
            s_index[i] += 1
            match_array[vs] = 1
            s_index[vs] += 1
            flag = 1
            next_match.append(i)
            next_match.append(vs)
    if flag == 0:
        print(-1)
        exit()
    if match_count == M:
        break
    ans_date += 1
    match_candidate = next_match
print(ans_date)
### BUGGY CODE ###

n = int(input())
list = []
max = 0

for i in range(n):
    num = int(input())
    if num > max:
        max = num
    list.append(num)

max_index = [i for i,x in enumerate(list) if x == max]
#print(max_index)
for i in range(n):
    if (i in max_index) & (len(max_index) == 1):
        list2 = list.copy()
        del(list2[i])
        print(sorted(list2)[-1])
    else:
        print(max)


### FIXED CODE ###

n = int(input())
list = []
max = 0
second = 0
max_index = []

for i in range(n):
    num = int(input())
    if (num > max):
        max = num
    elif (num > second):
        second = num
    list.append(num)

counter = list.count(max)

for i,x in enumerate(list):
    if (x == max) & (counter == 1):
        print(second)
    else:
        print(max)
### BUGGY CODE ###

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

def comb(n, r):
  if n<=1:
    return 0
  else:
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
  
cnt={}
for a in set(A):
  cnt[a]=A.count(a)
combs,total={},0
for c in cnt:
  tmp=comb(cnt[c],2)
  combs[c]=tmp
  total+=tmp
for a in A:
  print(total-combs[a]+comb(cnt[a]-1,2))

### FIXED CODE ###

from operator import mul
from functools import reduce
import collections
n=int(input())
A=list(map(int,input().split()))

def comb(n,r):
    r = min(n-r,r)
    if n<=1: return 0
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under
  
c=collections.Counter(A)
combs,total=[0]*n,0
for a in set(A):
  tmp=comb(c[a],2)
  combs[a-1]=tmp
  total+=tmp
for a in A:
  print(total-combs[a-1]+comb(c[a]-1,2))
### BUGGY CODE ###

N=int(input())
S=input()

L=[0]*9
D=[0]*100
C=0
R=len(set(S))**3

for i in range(N-2):
    if(C==R):
        break
    for j in range(i+1,N-1):
        s=str(S[i])+str(S[j])
        if(D[int(s)]==0):
            D[int(s)]=1
            C+=len(set(S[j+1:N]))
print(C)



### FIXED CODE ###

n = int(input())
s = input()
ans = 0
for a in range(10):
  for b in range(10):
    for c in range(10):
      a, b, c = str(a), str(b), str(c)
      a_find = s.find(a)
      if a_find == -1:
        continue
      b_find = s.find(b, a_find + 1)
      if b_find == -1:
        continue
      c_find = s.find(c, b_find + 1)
      if c_find == -1:
        continue
      ans += 1
print(ans)
### BUGGY CODE ###

N=input()
A = list(map(int, input().split()))
T=True
sucl =[]
for i in range(0,int(N)):
  T=True
  for j in range(0,int(N)):
    if T== False:
      break
    if A[i] % A[j]==0:
      if i==j:
        pass
      else:
        T=False
        
  if T==True:
    sucl.append(i)
print(len(sucl))
    

### FIXED CODE ###

N = input()
A = list(map(int, input().split()))
N = int(N)
MA = max(A)
s=0
LS = [0 for i in range(0,MA)]
for i in range(0,N):
  k=1
  while A[i]*k<=MA:
    LS[A[i]*k-1] +=1
    k +=1
for j in range(0,N):
  if LS[A[j]-1]==1:
    s+=1
print(s)
### BUGGY CODE ###

N = int(input())
A = [list(map(lambda x:int(x)-1,input().split())) for i in range(N)]
B = [0]*N
S = 0
ans = 0
while True:
    C = [0]*N
    for n in range(N):
        if C[n]==0 and B[n]!=N-1:
            b = A[n][B[n]]
            if C[b]==1: continue
            if A[b][B[b]]==n:
                B[n]+=1
                B[b]+=1
                C[n],C[b]=1,1
    if sum(C)==0:break
    ans +=1
print(ans if sum(B)==N*(N-1) else -1)

### FIXED CODE ###

import time
start = time.time()
N = int(input())
A = [list(map(lambda x:int(x)-1,input().split())) for i in range(N)]
B = [0]*N
S = 0
ans = 0
while True:
    C = [0]*N
    for n in range(N):
        if C[n]==0 and B[n]!=N-1:
            b = A[n][B[n]]
            if C[b]==1: continue
            if A[b][B[b]]==n:
                B[n]+=1
                B[b]+=1
                C[n],C[b]=1,1
    if sum(C)==0:break
    ans +=1
    if time.time()-start>1.7:
        print(N*(N-1)//2)
        exit()
print(ans if sum(B)==N*(N-1) else -1)
### BUGGY CODE ###

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

if D[0] != 0:
  print(0)
else:
  M = max(D)
  z = 0
  L = []
  for u in range(M+1):
    s = 0
    for v in range(1, len(D)):
      if D[v] == u:
        s += 1
    L.append(s)
  if L[0] != 0:
    print(0)
  else:
    P = 1
    for x in range(1, len(L)-1):
      P = (P * (L[x] ** L[x+1])) % 998244353
    print(P)

### FIXED CODE ###

N = int(input())
A = list(map(int, input().split()))
M = max(A)
D = {}
for i in range(N):
  if A[i] in D:
    D[A[i]] += 1
  else:
    D[A[i]] = 1
sorted(D)
S = 1
if A[0] == 0 and D[0] == 1:
  for i in D:
    if i != M:
      if i + 1 not in D:
        S = 0
        break
      else:
        S *= (D[i] ** D[i+1]) % 998244353
        S %= 998244353
else:
  S = 0
print(S % 998244353)
### BUGGY CODE ###

def count_the_character(string,character):
    count = 0
    for e in list(string):
        if e == character:
            count += 1
        else:pass
    return count
    
def return_if_the_lists_same(lst1,lst2):
    for a in range(26):
        if lst1[a] == lst2[a]:
            pass
        else:return False
        
    return True
    
n = int(input())
lst = [0 for i in range(n)]
lst_of_count = [[0 for i in range(26)] for i in range(n)]
alphabets = list("abcdefghijklmnopqrstuvwxyz")
for i in range(n):
    lst[i] = input()
    
    for a in range(26):
        lst_of_count[i][a] = count_the_character(lst[i],alphabets[a])
        
        

ans = 0
for i in range(n-1):
    for j in range(i+1,n):
        if return_if_the_lists_same(lst_of_count[i],lst_of_count[j]):
            ans+=1
print(ans)

### FIXED CODE ###

def return_sorted_string(string):
    lst=list(map(ord,list(string)))
    lst.sort()
    lst=list(map(chr,lst))
    string="".join(lst)
    return string
    



n = int(input())
strings=[input() for i in range(n)]
dic={}
for i in range(n):
    string=return_sorted_string(strings[i])
    if string in dic:
        dic[string]+=1
    else:
        dic[string]=1

count=0        

for i in dic.values():
    count += i*(i-1)/2


print(int(count))
### BUGGY CODE ###

argN,argM = list(map(int,input().split()))
aryA = list(map(int,input().split()))

aryB = [0] * argM
aryC = [0] * argM
for i in range(argM):
  aryB[i],aryC[i] = list(map(int,input().split()))

wkA = aryA[:]
wkA.sort()
for i in range(argM):
  for j in range(aryB[i]):
    if wkA[j] < aryC[i]:
      wkA[j] = aryC[i]
  wkA.sort()

print(sum(wkA))



### FIXED CODE ###

argN,argM = list(map(int,input().split()))
aryA = list(map(int,input().split()))
aryCB = [] 
for i in range(argM):
  aryBC = list(map(int,input().split()))
  #print(aryBC)
  aryBC.reverse()
  aryCB.append(aryBC[:])
#print(aryCB)
aryCB.sort(reverse=True)
#print(aryCB)

aryD = []
for i in range(len(aryCB)):
  if len(aryD) + aryCB[i][1] < argN:
    aryD += [aryCB[i][0]] * aryCB[i][1]
  elif len(aryD) < argN:
    wk = argN - len(aryD)
    aryD += [aryCB[i][0]] * wk

wkA = aryA[:] + aryD[:]
#print(wkA)
wkA.sort(reverse=True)
print(sum(wkA[:len(aryA)]))
### BUGGY CODE ###

import numpy as np
N, M = map(int, input().split())

cards = np.zeros(N) 
for j in range(M):
    L, R = map(int, input().split())
    for i in range(1, N+1):
        if L > i or R < i:
            cards[i-1] = 1

print(int(len(cards)-np.sum(cards)) )

### FIXED CODE ###

import numpy as np
N, M = map(int, input().split())

L = np.zeros(M)
R = np.zeros(M)
for j in range(M):
    L[j], R[j] = map(int, input().split())

if int(min(R) - max(L) + 1) < 0:
    print(0)
else:
    print(int(min(R) - max(L) + 1))
### BUGGY CODE ###


N = int(input())
        

D = []
for i in range(N):
    s = input()
    d = {"a": 0, "b": 0, "c": 0, "d": 0, "e": 0, "f": 0, "g": 0, "h": 0, "i": 0, "j": 0, "k": 0, "l": 0, "m": 0, "n": 0, "o": 0, "p": 0, "q": 0, "r": 0, "s": 0, "t": 0, "u": 0, "v": 0, "w": 0, "x": 0, "y": 0, "z": 0}
    for j in s:
        d[j] += 1
    
    D.append(d)

#print(D)
count = 0
while len(D):
    s = D[0]
    #print(D.count(s))
    n = D.count(s)
    count += n * (n-1) // 2
    while n:
        D.remove(s)
        n -= 1

print(count)

### FIXED CODE ###


N = int(input())

l = []
for i in range(N):
    s = input()
    s = sorted(s)
    l.append("".join(s))
    
import collections

c = collections.Counter(l)
#print(c)
issubclass(type(c), dict)
C = list(c.values())
#print(C)

count = 0
for i in C:
    count += i * (i-1) // 2
    
print(count)
### BUGGY CODE ###

def main():
    N = int(input())
    A = list(map(int, input().split()))
    L = [0] * (10**6+1)
    for a in A:
        L[a] += 1
    ans = 0
    Ng = [False] * (10**6+1)
    for a in A:
        for i in range(a*2, 10**6+1, a):
            Ng[i] = True
    for a, (cnt, ng) in enumerate(zip(L, Ng)):
        if cnt == 1 and not ng:
            ans += 1
    print(ans)

main()


### FIXED CODE ###

def main():
    N = int(input())
    A = list(map(int, input().split()))
    L = [0] * (10**6+1)
    for a in A:
        L[a] += 1
    ans = 0
    Ng = [False] * (10**6+1)
    for a, cnt in enumerate(L):
        if cnt:
            for i in range(a*2, 10**6+1, a):
                Ng[i] = True
    for a, (cnt, ng) in enumerate(zip(L, Ng)):
        if cnt == 1 and not ng:
            ans += 1
    print(ans)

main()
### BUGGY CODE ###

import sys
input=sys.stdin.readline
N=int(input())
C=[int(input())]
for i in range(N-1):
  tmp=int(input())
  if tmp != C[-1]:
    C.append(tmp)
  
import collections
table=collections.defaultdict(list)
N=len(C)
dp=[0]*(N+1)
dp[N]=1
mod=10**9 + 7
for i,x in enumerate(C[::-1]):
  dp[N-1-i]=dp[N-i]
  for y in table[x]:
    dp[N-1-i]+=dp[y+1]

  table[x].append(N-1-i)

print(dp[0]%mod)


### FIXED CODE ###

import sys
input=sys.stdin.readline
N=int(input())
C=[int(input())]
for i in range(N-1):
  tmp=int(input())
  if tmp != C[-1]:
    C.append(tmp)
  
import collections
table=collections.defaultdict(int)
N=len(C)
dp=[0]*(N+1)
dp[N]=1
mod=10**9 + 7
for i,x in enumerate(C[::-1]):
  dp[N-1-i]=dp[N-i] + table[x] 
  dp[N-1-i]%=mod
  table[x]+=dp[N-i]
print(dp[0])
### BUGGY CODE ###

from collections import Counter
 
    
S = [int(s) for s in input()]
N = len(S)
y = 2019
T = [S[-1]] * N
U = [0] * y
U[0] += 1

for i in range(N-2, -1, -1):
    t = (T[i+1] + S[i] * 10 ** (N-1-i)) % y
    T[i] = t
    U[t] += 1


cnt = 0
for u in U:
    cnt += u * (u - 1) // 2

print(cnt)

### FIXED CODE ###

from queue import deque
 
    
S = deque(input())
y = 2019
U = [0] * y
t = 0
U[0] = 1
k = 1

for s in reversed(S):
    t = t + int(s) * k
    t %= y
    U[t] += 1
    k *= 10
    k %= y

cnt = 0
for u in U:
    cnt = cnt + u * (u - 1) // 2

print(cnt)
### BUGGY CODE ###

N=int(input())
P=list(map(int,input().split()))
sFlag=True
i=0
sumCount=0
while(sFlag):
  Pi=P[i]
  for j in range(0,i+1):
    Pj=P[j]
    if(Pi>Pj):
      break
    if(j==i):
      sumCount=sumCount+1
  i=i+1
  if(i==N):
    sFlag=False
print(sumCount)

### FIXED CODE ###

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

num = p[0]
ans = 1

for i in range(1, n):
    if num >= p[i]:
        ans += 1
        num = p[i]

print(ans)
### BUGGY CODE ###

n = input()
a = list(map(int,input().split()))
total = 1
for i in a:
    total = total * i
if total > 10**18:
    print("-1")
else:
    print(total)



### FIXED CODE ###

N = input()
A = list(map(int,input().split()))
def main(A):
    if 0 in A:
        print(0)
        return

    prod = 1
    for a in A:
        prod *= a
        if prod > 1000000000000000000:
            print(-1)
            return

    print(prod)

main(A)