program c172

implicit none
integer :: n, m, k, h, i, j, t, x, y, p, q, r, c, d
integer, allocatable :: a(:), b(:)

read *, n, m, k

allocate(a(0:n-1))
allocate(b(0:m-1))

read *, a(:)
read *, b(:)

t = 0
i = 0
j = 0
x = 0
y = 0

do c = 0, n-1
 if (t + a(c) <= k) then
  x = x + 1
 end if
end do
p = c

do d = 0, n-1
 if (t + b(d) <= k) then
  y = y + 1
 end if
end do
q = d

if (p <= q) then
 r = p
else
 r = q
end if

do

 if(i < n) then
  do h = i, i + r
   x = x + a(h)
  end do
 else
  x = 1145141919
 end if

 if(j < m) then
  do h = j, j + r
   y = y + b(h)
  end do
 else
  y = 1145141919
 end if

 if (x <= y .and. t + a(i) <= k .and. i < n) then
  !print *, a(i)
  t = t + a(i)
  i = i + 1
  r = r - 1
 else if (t + b(j) <= k .and. j < m) then
  !print *, b(j)
  t = t + b(j)
  j = j + 1
  r = r - 1
 else
  exit
 end if

x = 0
y = 0

end do

print *, i + j

end program c172


program main
	implicit none
	integer :: N,M,K,count,count_A,count_B,i
	integer, allocatable :: A(:),B(:)
		count = 0
		read(*,*) N,M,K
		count_A=N;count_B=M
		allocate(A(N),B(M))
		read(*,*) A
		read(*,*) B
		do i=1,200000
			if (K<0) then
				exit
			else
				if(count_A==0) then
					if(count_B==0) then
						exit
					else
						K = K - B(count_B)
						count_B = count_B - 1
						count = count + 1
					end if
				else if(count_B==0) then
					if(count_A==0) then
						exit
					else
						K = K - A(count_A)
						count_A = count_A - 1
						count = count + 1
					end if
				else if (A(count_A)<=B(count_B)) then
					K = K - A(count_A)
					count_A = count_A - 1
					count = count + 1
				else
					K = K - B(count_B)
					count_B = count_B - 1
					count = count + 1
				end if
			end if
		end do
		write(*,*) count
end program main

Global OOD problem IDs are disjoint from train, validation, and in-distribution test problem IDs.

Score : 300 points

Problem Statement

We have two desks: A and B. Desk A has a vertical stack of N books on it, and Desk B similarly has M books on it.

It takes us A_i minutes to read the i-th book from the top on Desk A (1 \leq i \leq N), and B_i minutes to read the i-th book from the top on Desk B (1 \leq i \leq M).

Consider the following action:

Choose a desk with a book remaining, read the topmost book on that desk, and remove it from the desk.

How many books can we read at most by repeating this action so that it takes us at most K minutes in total? We ignore the time it takes to do anything other than reading.

Constraints

1 \leq N, M \leq 200000

1 \leq K \leq 10^9

1 \leq A_i, B_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M K
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_M

Output

Print an integer representing the maximum number of books that can be read.

Sample Input 1

3 4 240
60 90 120
80 150 80 150

Sample Output 1

3

In this case, it takes us 60, 90, 120 minutes to read the 1-st, 2-nd, 3-rd books from the top on Desk A, and 80, 150, 80, 150 minutes to read the 1-st, 2-nd, 3-rd, 4-th books from the top on Desk B, respectively.

We can read three books in 230 minutes, as shown below, and this is the maximum number of books we can read within 240 minutes.

Read the topmost book on Desk A in 60 minutes, and remove that book from the desk.

Read the topmost book on Desk B in 80 minutes, and remove that book from the desk.

Read the topmost book on Desk A in 90 minutes, and remove that book from the desk.

Sample Input 2

3 4 730
60 90 120
80 150 80 150

Sample Output 2

7

Sample Input 3

5 4 1
1000000000 1000000000 1000000000 1000000000 1000000000
1000000000 1000000000 1000000000 1000000000

Sample Output 3

0

Watch out for integer overflows.

Submitted source-code file size, in bytes.

Execution CPU time reported by official Project CodeNet metadata, in milliseconds.

Peak memory usage reported by official Project CodeNet metadata, in kilobytes.


program main
implicit none
	integer(8):: N,i,j,t,K,X,M,o,p,q,a1,b1
	integer,dimension(1:2*100000)::A,B,C
	read *, N,M,K
    A=1000000000
    B=1000000000
    read(*,*) (A(i), i = 1, N)
    read(*,*) (B(i), i = 1, M)

   	if(N>=M)then
    q=N
    else
    q=M
    end if
    a1=1
    b1=1
    t=0
	do i=1,q
    	if(t+A(a1)+b(b1)<=K)then
        t=t+A(a1)+B(b1)
        a1=a1+1
        b1=b1+1
		else if(t+A(a1)<=K)then
        t=t+A(a1)
        a1=a1+1
		else if(t+b(b1)<=K)then
        t=t+b(b1)
        b1=b1+1
		end if
    end do

 	print'(I0)',a1+b1-2
end program main


program main
implicit none
	integer(8):: N,i,j,t,K,X,M,o,p,q,a1,b1
	integer,dimension(1:2*100000)::A,B,C
	read *, N,M,K
    A=1000000000
    B=1000000000
    read(*,*) (A(i), i = 1, N)
    read(*,*) (B(i), i = 1, M)

   	if(N>=M)then
    q=N
    else
    q=M
    end if
    a1=1
    b1=1
    t=0
	do i=1,q
    if(A(a1)<=B(b1))then
    	if(t+A(a1)+b(b1)<=K)then
        t=t+A(a1)+B(b1)
        a1=a1+1
        b1=b1+1
		else if(t+A(a1)<=K)then
        t=t+A(a1)
        a1=a1+1
		else if(t+b(b1)<=K)then
        t=t+b(b1)
        b1=b1+1
		end if
    else
    	if(t+A(a1)+b(b1)<=K)then
        t=t+A(a1)+B(b1)
        a1=a1+1
        b1=b1+1
        else if(t+b(b1)<=K)then
        t=t+b(b1)
        b1=b1+1
        else if(t+A(a1)<=K)then
        t=t+A(a1)
        a1=a1+1
		end if

    end if
    end do

 	print'(I0)',a1+b1-2
end program main


program main
  implicit none
  integer(8) i, N, M, K, ans, AllA, ta, tb, na, nb, j
  integer(8),allocatable :: A(:),B(:)
  read(*, *) N, M, K
  allocate(A(N))
  allocate(B(M))
  read(*, *) (A(i), i = 1,N)
  read(*, *) (B(i), i = 1,M)
  ans = 0
  tb = 0
  !全てBのとき
  do i = 1,M
    if (tb + B(i) > K) then
      exit
    else
      tb = tb + B(i)
      ans = ans + 1
    end if
  end do
  !Aを一冊づつ増やす
  ta = 0
  tb = sum(B(1:ans))
  na = 0
  nb = ans
  AllA = 0
  do i = 1,N
    na = na + 1
    ta = ta + A(na)
    ALLA = K - ta
    if (ta > K) then
      exit
    end if
    !nb = 0
    !tb = 0
    do
      if (tb <= ALLA) then
        exit
      else
        tb = tb - B(nb)
        nb = nb - 1
        !tb = tb + B(j)
      endif
    enddo
    if (ans < na + nb) then
      ans = na + nb
    end if
  enddo
  write(*, *) ans
end program main


program main
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: n,m,k,cnt=0,time,ia,i,ib, mc=0
    integer(int64), allocatable:: a(:), b(:)
    read*, n, m, k
    allocate(a(n), b(m))
    read*, a(:)
    read*, b(:)

    time = sum(a)
    cnt = n
    ia=n

    do while (time > k)
        time = time - a(ia)
        ia=ia-1
        cnt=cnt-1
    end do

    ib=1

    do while (ib <= m)
        if (time+b(ib) > k) exit
        time=time+b(ib)
        ib=ib+1
        cnt=cnt+1
    end do

    mc = cnt

    do i=ia,1,-1
        cnt=cnt-1
        time=time-a(i)
        do while (ib <= m)
            if (time + b(ib) > k) exit
            time=time+b(ib)
            ib=ib+1
            cnt=cnt+1
        end do
        mc = max(mc, cnt)
    end do
    print'(i0)', mc
end program main


program c172

implicit none
integer :: n, m, k, h, i, j, t, x, y, p, q, r
integer, allocatable :: a(:), b(:)

read *, n, m, k

allocate(a(0:n-1))
allocate(b(0:m-1))

read *, a(:)
read *, b(:)

t = 0
i = 0
j = 0
x = 0
y = 0

do i = 0, n-1
 if (t + a(i) <= k) then
  x = x + 1
 end if
end do
p = i

do j = 0, n-1
 if (t + b(j) <= k) then
  y = y + 1
 end if
end do
q = j

if (p <= q) then
 r = p
else
 r = q
end if

do

 do h = i, i + r
  x = x + a(h)
 end do

 do h = j, j + r
  y = y + b(h)
 end do

 if (x <= y .and. t + a(i) <= k .and. i < n) then
  t = t + a(i)
  i = i + 1
  r = r - 1
 else if (t + b(j) <= k .and. j < m) then
  t = t + b(j)
  j = j + 1
  r = r - 1
 else
  exit
 end if

x = 0
y = 0

end do

print *, i + j

end program c172


program probC
  implicit none
  integer(16) :: k, m
  integer(16) :: n, m2, sa, sb, c
  integer(16), allocatable :: a(:), b(:)

  read(*,*) n, m2, k
  allocate(a(n), b(m2))
  read(*,*) a
  read(*,*) b

  m = 0
  sa = 1
  sb = 1
  c = 0
  do while ( m <= k )
     if ( sa == n+1 .and. sb == m2+1 ) then
        c = c + 1
        exit
     else if ( sa == n+1 ) then
        m = m + b(sb)
        sb = sb + 1
     else if ( sb == m2+1 ) then
        m = m + a(sa)
        sa = sa + 1
     else if ( a(sa) <= b(sb) ) then
        m = m + a(sa)
        sa = sa + 1
     else
        m = m + b(sb)
        sb = sb + 1
     end if

     c = c + 1
  end do

  deallocate(a, b)

  write(*,'(i0)') c - 1

  stop
end program probC


program probC
  implicit none
  integer(16) :: k, m
  integer :: n, m2, sa, sb, c
  integer(16), allocatable :: a(:), b(:)

  read(*,*) n, m2, k
  allocate(a(n), b(m2))
  read(*,*) a
  read(*,*) b

  m = 0
  sa = 1
  sb = 1
  c = 0
  do while ( m <= k )
     if ( sa == n+1 .and. sb == m2+1 ) then
        c = c + 1
        exit
     else if ( sa == n+1 ) then
        m = m + b(sb)
        sb = sb + 1
     else if ( sb == m2+1 ) then
        m = m + a(sa)
        sa = sa + 1
     else if ( a(sa) <= b(sb) ) then
        m = m + a(sa)
        sa = sa + 1
     else
        m = m + b(sb)
        sb = sb + 1
     end if

     c = c + 1
  end do

  write(*,'(i0)') c - 1
  !revised
  stop
end program probC


program main
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: n,m,k,cnt=0, time=0, ia=1, ib=1
    integer(int64), allocatable:: a(:), b(:)

    read*, n, m, k
    allocate(a(n+1), b(m+1))
    a(n+1)=20000000000_8
    b(m+1)=20000000000_8
    read*, a(1:n)
    read*, b(1:m)

    do while(time <= k .and. ia <= n+1 .and. ib <= m+1)
        if (a(ia) <= b(ib)) then
            cnt=cnt+1
            time=time+a(ia)
            ia=ia+1
        else
            cnt=cnt+1
            time=time+b(ib)
            ib=ib+1
        end if
    end do

    if (time > k) cnt=max(cnt-1,0)
    print'(i0)', cnt
end program main


program main
    implicit none
    integer(8) n,m,k
    integer(8), allocatable, dimension(:) :: a, b
    integer(8) i, time, na, nb, c

    read(*,*) n,m,k
    allocate(a(n))
    read(*,*) a
    allocate(b(m))
    read(*,*) b

    time = k
    c = 0

    na = 1
    nb = 1
    do i = 1,n+m
        if(a(na) < b(nb)) then
            time = time - a(na)
            na = na + 1
        else if(a(na) > b(nb)) then
            time = time - b(nb)
            nb = nb + 1
        else if(a(na) == b(nb)) then
            if(a(na+1) < b(nb+1)) then
                time = time - a(na)
                na = na + 1
            else if(a(na+1) > b(nb+1)) then
                time = time - b(nb)
                nb = nb + 1
            else if(a(na+1) == b(nb+1)) then
                time = time - a(na)
                na = na + 1
            end if
        end if
        if(time < 0) then
            exit
        end if
        c = c + 1
    end do
    write(*,*) c

end program


program main
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: n,m,k,i, cnt=0, time=0, ia=1, ib=1
    integer(int64), allocatable:: a(:), b(:)

    read*, n, m, k
    allocate(a(n+1), b(m+1), source=20000000000_8)
    read*, a(:)
    read*, b(:)

    do while(time <= k .and. ia <= n+1 .and. ib <= m+1)
        if (a(ia) <= b(ib)) then
            cnt=cnt+1
            time=time+a(ia)
            ia=ia+1
        else
            cnt=cnt+1
            time=time+b(ib)
            ib=ib+1
        end if
    end do

    if (time > k) cnt=min(cnt-1,0)
    print'(i0)', cnt
end program main


program c172

implicit none
integer :: n, m, k, i, j, t
integer, allocatable :: a(:), b(:)

read *, n, m, k

allocate(a(0:n-1))
allocate(b(0:m-1))

read *, a(:)
read *, b(:)

t = 0
i = 0
j = 0

do
 if (a(i) <= b(j) .and. t + a(i) <= k .and. i < n) then
  !print *, a(i)
  t = t + a(i)
  i = i + 1
 else if (t + b(j) <= k .and. j < m) then
  !print *, b(j)
  t = t + b(j)
  j = j + 1
 else
  exit
 end if
end do

print *, i + j

end program c172


program tsundoku
  implicit none
  integer :: n, m, i, h = 0, l, r, p
  integer(8) :: k, t
  integer(8), dimension(0:200001) :: a = 0, b = 0, x = 0, y = 0
  read(*,*) n, m, k
  read(*,*) a(1:n)
  read(*,*) b(1:m)
  do i = 1, n
    x(i) = x(i - 1) + a(i)
  end do
  do i = 1, m
    y(i) = y(i - 1) + b(i)
  end do
  do i = 0, n
    t = k - x(i)
    if (t < 0) exit
    l = 0
    r = m + 1
    do while (r - l > 1)
      p = (l + r) / 2
      if (y(p) <= t) then
        l = p
      else
        r = p
      end if
    end do
    h = max(h, i + l)
  end do
  write(*,'(i0)') h
end program tsundoku


program main
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: n,m,k,i, cnt=0, time=0, ia=1, ib=1
    integer(int64), allocatable:: a(:), b(:)

    read*, n, m, k
    allocate(a(n), b(m))
    read*, a(:)
    read*, b(:)

    do while(time <= k .and. ia <= n .and. ib <= m)
        if (a(ia) <= b(ib)) then
            cnt=cnt+1
            time=time+a(ia)
            ia=ia+1
        else
            cnt=cnt+1
            time=time+b(ib)
            ib=ib+1
        end if
    end do

    if (time > k) then
        print'(i0)', cnt-1
        stop
    end if

    if (ia <= n .and. ib <= m) then
        print'(i0)', cnt
        stop
    end if

    if (ia <= n) then
        do while(time <= k .and. ia <= n)
            cnt=cnt+1
            time=time+a(ia)
            ia=ia+1
        end do
    else
        do while(time <= k .and. ib <= m)
            cnt=cnt+1
            time=time+b(ib)
            ib=ib+1
        end do
    end if

    if (time > k) cnt=cnt-1
    print'(i0)', cnt
end program main


      PROGRAM tsundoku
      IMPLICIT NONE
      INTEGER,PARAMETER :: p = 16
      integer(p) :: ans,n,m,i,j, k,time, caseNum
      integer(p),allocatable :: a(:),b(:)

      read*,n,m,k
      allocate( a(n),b(m) )
      read*,a(1:n)
      read*,b(1:m)

      a(1:n) = a(n:1:-1)
      b(1:m) = b(m:1:-1)

      i = n
      j = m
      ans = 0
      time = 0
      do while( .true. )
        if( i==0 ) caseNum = 2
        if( j==0 ) caseNum = 1

        if( a(i)<=b(j) )then
          caseNum = 1
        else
          caseNum = 2
        end if

        select case( caseNum )
          case( 1 )
            time = time + a(i)
            i = max( 0,i-1 )
          case( 2 )
            time = time + b(j)
            j = max( 0,j-1 )
        end select

      !  print*,caseNum,i,j

        if( time>k )then
          exit
        end if

        if( i==0.and.j==0 )then
          if( time<=k )then
            ans = ans + 1
          end if
          exit
        end if

        ans = ans + 1
      end do

      print*,ans

      stop
      !debugg

      END PROGRAM


program tsundoku
  implicit none
  integer(8), parameter :: infty = 100100100100_8
  integer :: n, m, i = 1, j = 1, x = 0
  integer(8) :: k, t = 0
  integer(8), dimension(200001) :: a = 0, b = 0
  read(*,*) n, m, k
  read(*,*) a(1:n)
  read(*,*) b(1:m)
  a(n + 1) = infty
  b(m + 1) = infty
  do
    if (a(i) < b(j)) then
      if (t + a(i) > k) exit
      t = t + a(i)
      i = i + 1
    else
      if (t + b(j) > k) exit
      t = t + b(j)
      j = j + 1
    end if
    x = x + 1
    if (i == n + 1 .and. j == m + 1) exit
  end do
  write(*,'(i0)') x
end program tsundoku


program tsundoku
  implicit none
  integer, parameter :: infty = 1100100100
  integer :: n, m, i = 1, j = 1, x = 0
  integer(8) :: k, t = 0
  integer(8), dimension(200001) :: a = 0, b = 0
  read(*,*) n, m, k
  read(*,*) a(1:n)
  read(*,*) b(1:m)
  a(n + 1) = infty
  b(m + 1) = infty
  do
    if (a(i) < b(j)) then
      if (t + a(i) > k) exit
      t = t + a(i)
      i = i + 1
      x = x + 1
    else
      if (t + b(j) > k) exit
      t = t + b(j)
      j = j + 1
      x = x + 1
    end if
  end do
  write(*,'(i0)') x
end program tsundoku


program tsundoku
  implicit none
  integer, parameter :: infty = 1100100100
  integer :: n, m, k, i = 1, j = 1, x = 0, t = 0
  integer, dimension(200001) :: a = 0, b = 0
  read(*,*) n, m, k
  read(*,*) a(1:n)
  read(*,*) b(1:m)
  a(n + 1) = infty
  b(m + 1) = infty
  do
    if (a(i) < b(j)) then
      if (t + a(i) > k) exit
      t = t + a(i)
      i = i + 1
      x = x + 1
    else
      if (t + b(j) > k) exit
      t = t + b(j)
      j = j + 1
      x = x + 1
    end if
  end do
  write(*,'(i0)') x
end program tsundoku


program abc36
 implicit none
 integer::N,ia,ans=0,x
 read(*,*)N
 do ia=1,N
  x=floor(dble(N)/dble(ia))
  ans=ans+x*(x+1)*ia/2
 end do
 print'(i0)',ans
end program abc36

Score : 400 points

Problem Statement

For a positive integer X, let f(X) be the number of positive divisors of X.

Given a positive integer N, find \sum_{K=1}^N K\times f(K).

Constraints

1 \leq N \leq 10^7

Input

Input is given from Standard Input in the following format:

N

Output

Print the value \sum_{K=1}^N K\times f(K).

Sample Input 1

4

Sample Output 1

23

We have f(1)=1, f(2)=2, f(3)=2, and f(4)=3, so the answer is 1\times 1 + 2\times 2 + 3\times 2 + 4\times 3 =23.

Sample Input 2

100

Sample Output 2

26879

Sample Input 3

10000000

Sample Output 3

838627288460105

Watch out for overflows.


program M24
 implicit none
 integer::N=0,ishota=0,ishotb=0,s=0,f=0
 real::b=0
 read(*,*)N
 do ishota=1,N
  f=0
  do ishotb=1,ishota
   b=mod(ishota,ishotb)
   if(b==0)then
    f=f+1
   end if
  end do
  s=s+ishota*f
 end do
 print'(i0)',s
end program M24


program answer
  implicit none

  integer(8) :: N, i, sum

  read(*,*) N
  sum = 0
  do i = 1, N
     sum = sum + i*yaku(i)
  end do

  write(*,*) sum

  stop
contains
  integer(8) function yaku(x)
    implicit none

    integer(8) :: i, x, count

    count=0

    do i = 1, x
       if(mod(x,i)==0) then
          count = count + 1
       end if
    end do

    yaku=count
  end function yaku

  end program answer


program d172

integer :: i, j, n, m
integer :: p(2:10000000)
integer(16) :: k, z, f

p(:) = 1
m = 1
z = 0

read *, n

do while (n > 1)
 do i = 2, n
 if (mod(n,i) == 0) then
  p(i) = p(i) + 1
  n = n / p(i)
  if(i > m) then
   m = i
  end if
  cycle
 end if
 end do
end do

do i = 1, n
 do j = 2, m
  f = f * p(j)
 end do
 k = f * i
 z = z + k
end do

print *, z

end program d172


program main
  implicit none
  integer(16) i, N, ans, p
  read(*, *) N
  ans = 0
  do i = 1,N
    p = N/i
    ans = ans + i * p * (p + 1)
  end do
  ans = ans/2
  !read(*, *) (L(i), i = 1,N)

  write(*, *) ans
end program main


program main
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: n,i,j,ans=0

    read*, n

    do i=1,n
        j = n/i
        ans = ans + ((j*(j+1))/2)*i
    end do
    print'(i0)', ans
end program main


program atcoder
implicit none
integer :: n , i , num , f1 , yakusuu
read *, n
num = 0
do i = 1 , n
  f1 = yakusuu(i)
  num = num + f1 * i
end do
print *,num
end program atcoder

function yakusuu(n)
implicit none
integer :: n , yakusuu , i
yakusuu = 0
do i = 1 , n
  if(mod(n,i) == 0)then
    yakusuu = yakusuu + 1
  end if
end do
end function yakusuu


program sample
    implicit none

    integer(8) :: i,j,m,n,a(10000000)

    integer(8),allocatable :: x(:),y(:)

    read(*,*) n

    do i=1,10000000
        a(i)=0
    end do
    do i=1,n
        do j=1,n/i
             a(i*j)=a(i*j)+1
         end do
    end do
    m=0
    do i=1,n
        m=m+a(i)*i

    end do
    write(*,*) m

    stop
end program sample


program main
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: n,i,j,l,ans=0
    integer(int32), allocatable:: a(:),divisor(:)

    read*, n
    allocate(a(n))
    read*, a(:)

    l = maxval(a)
    allocate(divisor(l),source=0)

    do i=1,n
        divisor(a(i))=divisor(a(i))+1
        if (divisor(a(i)) > 1) cycle
        j=2
        do while(a(i)*j <= l)
            divisor(a(i)*j)=divisor(a(i)*j)+1
            j=j+1
        end do
    end do

    do i=1,n
        if (divisor(a(i)) == 1) ans=ans+1
    end do
    print'(i0)', ans
end program main

Score : 400 points

Problem Statement

Given is a number sequence A of length N.

Find the number of integers i \left(1 \leq i \leq N\right) with the following property:

For every integer j \left(1 \leq j \leq N\right) such that i \neq j , A_j does not divide A_i.

Constraints

All values in input are integers.

1 \leq N \leq 2 \times 10^5

1 \leq A_i \leq 10^6

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \cdots A_N

Output

Print the answer.

Sample Input 1

5
24 11 8 3 16

Sample Output 1

3

The integers with the property are 2, 3, and 4.

Sample Input 2

4
5 5 5 5

Sample Output 2

0

Note that there can be multiple equal numbers.

Sample Input 3

10
33 18 45 28 8 19 89 86 2 4

Sample Output 3

5


program ABC170D
    implicit none
    integer(8)::N,i,j,ans=0,amax,as,flg
    integer(8),allocatable::A(:),B(:)
    read*,N
    allocate(A(N))
    read*,A
    amax=maxval(A)
    allocate(B(amax))
    B=0
    call heapsort(N,A)
    do i=1,N
        B(A(i))=1
    end do

    flg=0
    do i=1,N-1
        if(A(i)/=A(i+1).and.flg==1)then
            flg=0
            A(i)=0
        else if(A(i)==A(i+1))then
            flg=1
            A(i)=0
            if(i==N-1)A(N)=0
        end if
    end do

    do i=1,N
        if(A(i)/=0)then
            as=int(sqrt(real(A(i))))
            flg=0
            do j=2,as
                if(mod(A(i),j)==0)then
                    if(B(j)==1.or.B(A(i)/j)==1)then
                        flg=1
                        exit
                    end if
                end if
            end do
            if(flg==0)ans=ans+1
        end if
    end do

    print'(i0)',ans
end program ABC170D

subroutine heapsort(n,array)
    implicit none
    integer(8),intent(in) :: n
    integer(8),intent(inout) :: array(1:n)

    integer(8) ::i,k,j,l
    integer(8) :: t

    if(n.eq.1)return

    l=n/2+1
    k=n
    do while(k.ne.1)
       if(l.gt.1)then
          l=l-1
          t=array(L)
       else
          t=array(k)
          array(k)=array(1)
          k=k-1
          if(k.eq.1) then
             array(1)=t
             exit
          endif
       endif
       i=l
       j=l+l
       do while(j.le.k)
          if(j.lt.k)then
             if(array(j).lt.array(j+1))j=j+1
          endif
          if (t.lt.array(j))then
             array(i)=array(j)
             i=j
             j=j+j
          else
             j=k+1
          endif
       enddo
       array(i)=t
    enddo

    return
end subroutine heapsort


program ABC170D
    implicit none
    integer(8)::N,i,j,ans=0,amax,as,flg
    integer(8),allocatable::A(:),B(:)
    read*,N
    allocate(A(N))
    read*,A
    amax=maxval(A)
    allocate(B(amax))
    B=0
    call heapsort(N,A)
    do i=1,N
        B(A(i))=1
        if(i>=2.and.A(i)==A(i-1))then
            if(i/=N)A(i-1)=0
            if(i==N)then
                A(i-1)=0
                A(i)=0
            end if
        end if
    end do

    do i=1,N
        if(A(i)/=0)then
            as=int(sqrt(real(A(i))))
            flg=0
            do j=2,as
                if(mod(A(i),j)==0)then
                    if(B(j)==1.or.B(A(i)/j)==1)then
                        flg=1
                        exit
                    end if
                end if
            end do
            if(flg==0)ans=ans+1
        end if
    end do

    print'(i0)',ans
end program ABC170D

subroutine heapsort(n,array)
    implicit none
    integer(8),intent(in) :: n
    integer(8),intent(inout) :: array(1:n)

    integer(8) ::i,k,j,l
    integer(8) :: t

    if(n.eq.1)return

    l=n/2+1
    k=n
    do while(k.ne.1)
       if(l.gt.1)then
          l=l-1
          t=array(L)
       else
          t=array(k)
          array(k)=array(1)
          k=k-1
          if(k.eq.1) then
             array(1)=t
             exit
          endif
       endif
       i=l
       j=l+l
       do while(j.le.k)
          if(j.lt.k)then
             if(array(j).lt.array(j+1))j=j+1
          endif
          if (t.lt.array(j))then
             array(i)=array(j)
             i=j
             j=j+j
          else
             j=k+1
          endif
       enddo
       array(i)=t
    enddo

    return
end subroutine heapsort


program ABC170D
    implicit none
    integer(8)::N,i,j,flg,ans=0
    integer(8),allocatable::A(:),B(:)
    read*,N
    allocate(A(N))
    allocate(B(N))
    read*,A
    B=0
    call heapsort(N,A)
    do i=1,N
        flg=0
        if(B(i)==0)then
            do j=i+1,N
                if(mod(A(j),A(i))==0)then
                    if(A(j)==A(i))B(i)=1
                    B(j)=1
                end if
            end do
        end if
        if(B(i)==0)ans=ans+1
    end do
    print'(i0)',ans
end program ABC170D

subroutine heapsort(n,array)
    implicit none
    integer(8),intent(in) :: n
    integer(8),intent(inout) :: array(1:n)

    integer(8) ::i,k,j,l
    integer(8) :: t

    if(n.eq.1)return

    l=n/2+1
    k=n
    do while(k.ne.1)
       if(l.gt.1)then
          l=l-1
          t=array(L)
       else
          t=array(k)
          array(k)=array(1)
          k=k-1
          if(k.eq.1) then
             array(1)=t
             exit
          endif
       endif
       i=l
       j=l+l
       do while(j.le.k)
          if(j.lt.k)then
             if(array(j).lt.array(j+1))j=j+1
          endif
          if (t.lt.array(j))then
             array(i)=array(j)
             i=j
             j=j+j
          else
             j=k+1
          endif
       enddo
       array(i)=t
    enddo

    return
end subroutine heapsort


program sample
    implicit none
    integer(8) ::c,i,j,m,n,k,a1,b1,c1
    integer(8)::x,b(1000000)
    integer(8),allocatable::a(:)
    read(*,*) n

    allocate(a(n))

    read(*,*)a
    do i=1,10**6
        b(i)=1
    end do
    do i=1,n
        x=a(i)
        do j=2,n/x
            b(x*j)=0
        end do
    end do
    m=0
    do i=1,n
        if (b(a(i))==1)then
            m=m+1
            b(a(i))=2
        else if (b(a(i))==2)then
            m=m-1
            b(a(i))=0
        end if
    end do

    write(*,*)m
    stop

    contains

    recursive subroutine msort(x)
    implicit none
    integer(8), intent(inout) :: x(:)

    integer(8) :: n, mid, i, j, k
    integer(8), allocatable :: tmp(:)

    n = size(x)

    if(n == 1) then
      return
    end if

    !
    ! 前半と後半に分けてソート
    ! 1~midとmid+1~nをそれぞれソート済みにする
    !
    mid = n / 2
    call msort(x(:mid))
    call msort(x(mid+1:))

    !
    ! tmpという配列にxを一時的に保管
    ! マージソートは外部ソート
    !
    allocate(tmp(n))
    tmp = x

    ! tmpの後半部分を左右反転
    do k = n, mid+1, -1
      tmp(k) = x(mid + n - k + 1)
    end do

    ! iは前半のリストのうちまだ見ていないものの最小値
    ! jは後半のリストのうちまだ見ていないものの最小値
    ! kは見た回数の合計
    i = 1
    j = n

    do k = 1, n
      !
      ! 一番左と一番右のうち小さい方をxに入れていく
      ! ここが等号つき不等号なので、安定ソートとなる
      !
      if(tmp(i) <= tmp(j)) then
        x(k) = tmp(i)
        i = i + 1
      else
        x(k) = tmp(j)
        j = j - 1
      end if
    end do

    deallocate(tmp)
    return
  end subroutine msort

end program sample


program D
read*,in

call sub(in)
end program

subroutine sub(in)
implicit double precision(a-h,o-z)
integer::na(1:in)
integer::ic(1:200000)
do k=1,200000
ic(k)=0
end do
read*,(na(j),j=1,in)
icou=0
do i=in,1,-1
id=0
if (ic(i)==1)then
id=1
goto 90
end if
do j=1,i-1
if(id==0)then
if (abs(dble(na(i)/na(j))-(dble(na(i))/dble(na(j))))<1.E-6)then
id=1
end if
end if
if(ic(j)==0)then
if (abs(dble(na(j)/na(i))-(dble(na(j))/dble(na(i))))<1.E-6)then
ic(j)=1
end if
end if
end do
90 if(id==0)then
icou=icou+1
end if
end do
print*,icou
end subroutine


program D
read*,in

call sub(in)
end program

subroutine sub(in)
implicit double precision(a-h,o-z)
integer::na(1:in)
integer::ic(1:200000)
do k=1,200000
ic(k)=0
end do
read*,(na(j),j=1,in)
icou=0
do i=in,1,-1
id=0
if (ic(i)==1)then
id=1
goto 90
end if
 do j=1,i
 if (i.ne.j)then
  if (abs(dble(na(i)/na(j))-(dble(na(i))/dble(na(j))))<1.E-6)then
  id=1
  end if
   if (abs(dble(na(j)/na(i))-(dble(na(j))/dble(na(i))))<1.E-6)then
   ic(j)=1
  end if
 end if
 end do
 90 if(id==0)then
 icou=icou+1
 end if
end do
print*,icou
end subroutine


program D
read*,in

call sub(in)
end program

subroutine sub(in)
implicit double precision(a-h,o-z)
integer::na(1:in)
integer::ic(1:200000)
do k=1,200000
ic(k)=0
end do
read*,(na(j),j=1,in)
icou=0
do i=in,1,-1
id=0
if (ic(i)==1)then
id=1
goto 90
end if
 do j=1,in
 if (i.ne.j)then
  if (abs(dble(na(i)/na(j))-(dble(na(i))/dble(na(j))))<1.E-6)then
  id=1
  end if
   if (abs(dble(na(j)/na(i))-(dble(na(j))/dble(na(i))))<1.E-6)then
   ic(j)=1
  end if
 end if
 end do
 90 if(id==0)then
 icou=icou+1
 end if
end do
print*,icou
end subroutine


program sample
    implicit none
    integer(8) ::c,i,j,m,n,k,a1,b1,c1
    integer(8)::x,y(100)
    integer(8),allocatable::a(:),b(:)
    read(*,*) n

    allocate(a(n))
    allocate(b(n))
    read(*,*)a
    do i=1,n
        b(i)=1
    end do
    call msort(a)

    do i=1,n
        x=a(i)
        m=i+1
        if (b(i)==0)then
            cycle
        end if
        do while(a(m)==x)
            b(m)=0
            b(i)=0
            m=m+1
        end do
        do j=m,n

             if (mod(a(j),x)==0)then
                    b(j)=0
            end  if
        end do

    end do

    write(*,*)sum(b)
    stop

    contains

    recursive subroutine msort(x)
    implicit none
    integer(8), intent(inout) :: x(:)

    integer(8) :: n, mid, i, j, k
    integer(8), allocatable :: tmp(:)

    n = size(x)

    if(n == 1) then
      return
    end if

    !
    ! 前半と後半に分けてソート
    ! 1~midとmid+1~nをそれぞれソート済みにする
    !
    mid = n / 2
    call msort(x(:mid))
    call msort(x(mid+1:))

    !
    ! tmpという配列にxを一時的に保管
    ! マージソートは外部ソート
    !
    allocate(tmp(n))
    tmp = x

    ! tmpの後半部分を左右反転
    do k = n, mid+1, -1
      tmp(k) = x(mid + n - k + 1)
    end do

    ! iは前半のリストのうちまだ見ていないものの最小値
    ! jは後半のリストのうちまだ見ていないものの最小値
    ! kは見た回数の合計
    i = 1
    j = n

    do k = 1, n
      !
      ! 一番左と一番右のうち小さい方をxに入れていく
      ! ここが等号つき不等号なので、安定ソートとなる
      !
      if(tmp(i) <= tmp(j)) then
        x(k) = tmp(i)
        i = i + 1
      else
        x(k) = tmp(j)
        j = j - 1
      end if
    end do

    deallocate(tmp)
    return
  end subroutine msort

end program sample


      MODULE divisorMod
! 約数列挙のためのモジュール
      IMPLICIT NONE

      integer,parameter,private :: prec = 8

      contains
! 引数Nの約数全て(1-N)を割り付け配列で返す関数．
! N>=1のみ．
! O( sqrt(N) )
      function allDivisor( N ) result( resArray )
        implicit none
        integer(prec),intent(in) :: N
        integer(prec),allocatable :: resArray(:),buffer(:)
        integer(prec) :: i,c

        if( N<=0 )then !error
          resArray = [0]
          return
        end if

        allocate( buffer(floor(sqrt(real(N)))*2) )

        !c : counter
        c = 1
        do i = 1,floor(sqrt(real(N+1)))
          if( mod(N,i)==0 )then
            buffer(c) = i
            c = c + 1
          end if
        end do

        !c is allways over 1
        c = c - 1

        if( buffer(c)*buffer(c)==N )then
          !n is Square number
          buffer(c:2*c-1) = N / buffer(c:1:-1)
          c = 2*c - 1
        else
          buffer(c+1:2*c) = N / buffer(c:1:-1)
          c = 2*c
        end if

        !c is now number of divisor
        allocate( resArray(1:c) )
        resArray(1:c) = buffer(1:c)
      end function allDivisor
      END MODULE divisorMod

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

      PROGRAM NotDivisible
      use divisorMod
      IMPLICIT NONE
      INTEGER,PARAMETER :: prec = 8
      integer(prec) :: n, ans=0
      integer(prec), allocatable :: a(:)
      integer(prec) :: i,j
      integer(prec) :: list(10**6) = 0
      integer(prec) ,allocatable :: div(:)

      read*,n
      allocate( a(n) )
      read*,a(1:n)

      do i = 1,n
        list( a(i) ) = list( a(i) ) + 1
      end do

      outer:&
      do i = 1,n
        div = allDivisor( a(i) )
       ! print'(*(x,i0))',div(:)

        do j = 1,size(div)-1
          if( list(div(j))/=0 )then
            cycle outer
          end if
        end do

        if( list(a(i))>=2 )then
          cycle outer
        end if

        ans = ans + 1
        deallocate( div )
      end do outer

      print*,ans

     ! stop
      !debugg

      END PROGRAM


module merge_sort_mod
    use,intrinsic :: iso_fortran_env
    implicit none
    private
    public:: merge_sort, double_merge_sort
    interface merge_sort
        module procedure ms32, ms64
    end interface

    interface double_merge_sort
        module procedure msd3232, msd6464
    end interface

contains
    recursive subroutine ms32(ar, fst, lst)
        integer(int32),intent(inout):: ar(:)
        integer(int32),intent(in):: fst,lst
        integer(int32):: mdl

        if (lst-fst < 2) then
            if (ar(fst) > ar(lst)) call swap32(ar(fst),ar(lst))
            return
        end if

        mdl = (fst+lst)/2
        call ms32(ar, fst, mdl)
        call ms32(ar, mdl+1, lst)
        call merge32(ar, fst, mdl, lst)
    end subroutine

    subroutine merge32(ar, fst, mdl, lst)
        integer(int32),intent(inout):: ar(:)
        integer(int32),intent(in):: fst, mdl, lst
        integer(int32),allocatable:: tmp(:)
        integer(int32):: li, ri, ti

        allocate(tmp(lst-fst+1))

        li=fst
        ri=mdl+1
        ti=1

        do while (li <= mdl .and. ri <= lst)
            if (ar(li) <= ar(ri)) then
                tmp(ti) = ar(li)
                li=li+1
            else
                tmp(ti) = ar(ri)
                ri=ri+1
            end if
            ti=ti+1
        end do

        if (li <= mdl) then
            tmp(ti:) = ar(li:mdl)
        else
            tmp(ti:) = ar(ri:lst)
        end if

        ar(fst:lst) = tmp(:)
        deallocate(tmp)
    end subroutine

    subroutine swap32(x,y)
        integer(int32),intent(inout):: x,y
        integer(int32):: tmp
        tmp = x
        x = y
        y = tmp
    end subroutine

    recursive subroutine ms64(ar, fst, lst)
        integer(int64),intent(inout):: ar(:)
        integer(int64),intent(in):: fst,lst
        integer(int64):: mdl

        if (lst-fst < 2) then
            if (ar(fst) > ar(lst)) call swap64(ar(fst),ar(lst))
            return
        end if

        mdl = (fst+lst)/2
        call ms64(ar, fst, mdl)
        call ms64(ar, mdl+1, lst)
        call merge64(ar, fst, mdl, lst)
    end subroutine

    subroutine merge64(ar, fst, mdl, lst)
        integer(int64),intent(inout):: ar(:)
        integer(int64),intent(in):: fst, mdl, lst
        integer(int64),allocatable:: tmp(:)
        integer(int64):: li, ri, ti

        allocate(tmp(lst-fst+1))

        li=fst
        ri=mdl+1
        ti=1

        do while (li <= mdl .and. ri <= lst)
            if (ar(li) <= ar(ri)) then
                tmp(ti) = ar(li)
                li=li+1
            else
                tmp(ti) = ar(ri)
                ri=ri+1
            end if
            ti=ti+1
        end do

        if (li <= mdl) then
            tmp(ti:) = ar(li:mdl)
        else
            tmp(ti:) = ar(ri:lst)
        end if

        ar(fst:lst) = tmp(:)
        deallocate(tmp)
    end subroutine

    subroutine swap64(x,y)
        integer(int64),intent(inout):: x,y
        integer(int64):: tmp
        tmp = x
        x = y
        y = tmp
    end subroutine

    recursive subroutine msd3232(ar1, ar2, fst, lst)
        integer(int32),intent(inout):: ar1(:),ar2(:)
        integer(int32),intent(in):: fst,lst
        integer(int32):: mdl

        if (lst - fst < 2) then
            if (ar1(fst) > ar1(lst)) then
                call swap32(ar1(fst), ar1(lst))
                call swap32(ar2(fst), ar2(lst))
            end if
            return
        end if

        mdl = (fst+lst)/2

        call msd3232(ar1,ar2,fst,mdl)
        call msd3232(ar1,ar2,mdl+1,lst)
        call merged3232(ar1,ar2,fst,mdl,lst)
    end subroutine

    subroutine merged3232(ar1,ar2,fst,mdl,lst)
        integer(int32),intent(inout):: ar1(:),ar2(:)
        integer(int32),intent(in):: fst,mdl,lst
        integer(int32),allocatable:: t1(:),t2(:)
        integer(int32):: li,ri,ti

        allocate(t1(lst-fst+1), t2(lst-fst+1))

        li=fst
        ri=mdl+1
        ti=1

        do while(li <= mdl .and. ri <= lst)
            if (ar1(li) <= ar1(ri)) then
                t1(ti) = ar1(li)
                t2(ti) = ar2(li)
                li=li+1
            else
                t1(ti) = ar1(ri)
                t2(ti) = ar2(ri)
                ri=ri+1
            end if
            ti=ti+1
        end do

        if (li <= mdl) then
            t1(ti:) = ar1(li:mdl)
            t2(ti:) = ar2(li:mdl)
        else
            t1(ti:) = ar1(ri:lst)
            t2(ti:) = ar2(ri:lst)
        end if

        ar1(fst:lst) = t1(:)
        ar2(fst:lst) = t2(:)

        deallocate(t1,t2)
    end subroutine

    recursive subroutine msd6464(ar1, ar2, fst, lst)
        integer(int64),intent(inout):: ar1(:),ar2(:)
        integer(int64),intent(in):: fst,lst
        integer(int64):: mdl

        if (lst - fst < 2) then
            if (ar1(fst) > ar1(lst)) then
                call swap64(ar1(fst), ar1(lst))
                call swap64(ar2(fst), ar2(lst))
            end if
            return
        end if

        mdl = (fst+lst)/2

        call msd6464(ar1,ar2,fst,mdl)
        call msd6464(ar1,ar2,mdl+1,lst)
        call merged6464(ar1,ar2,fst,mdl,lst)
    end subroutine

    subroutine merged6464(ar1,ar2,fst,mdl,lst)
        integer(int64),intent(inout):: ar1(:),ar2(:)
        integer(int64),intent(in):: fst,mdl,lst
        integer(int64),allocatable:: t1(:),t2(:)
        integer(int64):: li,ri,ti

        allocate(t1(lst-fst+1), t2(lst-fst+1))

        li=fst
        ri=mdl+1
        ti=1

        do while(li <= mdl .and. ri <= lst)
            if (ar1(li) <= ar1(ri)) then
                t1(ti) = ar1(li)
                t2(ti) = ar2(li)
                li=li+1
            else
                t1(ti) = ar1(ri)
                t2(ti) = ar2(ri)
                ri=ri+1
            end if
            ti=ti+1
        end do

        if (li <= mdl) then
            t1(ti:) = ar1(li:mdl)
            t2(ti:) = ar2(li:mdl)
        else
            t1(ti:) = ar1(ri:lst)
            t2(ti:) = ar2(ri:lst)
        end if

        ar1(fst:lst) = t1(:)
        ar2(fst:lst) = t2(:)

        deallocate(t1,t2)
    end subroutine
end module

program main
    use,intrinsic :: iso_fortran_env
    use merge_sort_mod
    implicit none
    integer(int64):: n,i,div,b,ans,cnt(1000000)=0
    integer(int64), allocatable:: a(:)
    logical:: ng(1000000) = .false., ok(1000000) = .true., ook

    read*, n
    allocate(a(n))
    read*, a(:)

    call merge_sort(a,1_8,n)
    ans = 0
    do i=1,n
        b = a(i)
        cnt(b)=cnt(b)+1
        ng(b) = .true.
        div=2
        ook = .true.
        do while(div**2 <= b)
            do while(mod(b,div)==0)
                b=b/div
                if (ng(b) .or. ng(div)) then
                    ook = .false.
                end if
            end do
            div=div+1
        end do
    ! print*, i, ok(i)
        ok(a(i)) = ook
        if (ook) ans=ans+1
    end do

    if (cnt(1) == 1) then
        print'(i0)', 1
    else if (cnt(1) > 1) then
        print'(i0)', 0
    else
        do i=1,1000000
            if (cnt(i) > 1 .and. ok(i)) then
                ! print*, ans, cnt(i)
                ans=ans-cnt(i)
            end if
        end do

        print'(i0)', ans
    end if
end program main


program sample
    implicit none
    integer(8) ::c,i,j,m,n,k,a1,b1,c1
    integer(8)::x,y(100)
    integer(8),allocatable::a(:),b(:)
    read(*,*) n

    allocate(a(n))
    allocate(b(n))
    read(*,*)a
    do i=1,n
        b(i)=1
    end do
    call msort(a)

    do i=1,n
        x=a(i)
        m=i+1
        do while(a(m)==x)
            b(m)=0
            b(i)=0
            m=m+1
        end do
        do j=m,n

             if (mod(a(j),x)==0)then
                    b(j)=0
            end  if
        end do

    end do

    write(*,*)sum(b)
    stop

    contains

    recursive subroutine msort(x)
    implicit none
    integer(8), intent(inout) :: x(:)

    integer(8) :: n, mid, i, j, k
    integer(8), allocatable :: tmp(:)

    n = size(x)

    if(n == 1) then
      return
    end if

    !
    ! 前半と後半に分けてソート
    ! 1~midとmid+1~nをそれぞれソート済みにする
    !
    mid = n / 2
    call msort(x(:mid))
    call msort(x(mid+1:))

    !
    ! tmpという配列にxを一時的に保管
    ! マージソートは外部ソート
    !
    allocate(tmp(n))
    tmp = x

    ! tmpの後半部分を左右反転
    do k = n, mid+1, -1
      tmp(k) = x(mid + n - k + 1)
    end do

    ! iは前半のリストのうちまだ見ていないものの最小値
    ! jは後半のリストのうちまだ見ていないものの最小値
    ! kは見た回数の合計
    i = 1
    j = n

    do k = 1, n
      !
      ! 一番左と一番右のうち小さい方をxに入れていく
      ! ここが等号つき不等号なので、安定ソートとなる
      !
      if(tmp(i) <= tmp(j)) then
        x(k) = tmp(i)
        i = i + 1
      else
        x(k) = tmp(j)
        j = j - 1
      end if
    end do

    deallocate(tmp)
    return
  end subroutine msort

end program sample


module merge_sort_mod
    use,intrinsic :: iso_fortran_env
    implicit none
    private
    public:: merge_sort, double_merge_sort
    interface merge_sort
        module procedure ms32, ms64
    end interface

    interface double_merge_sort
        module procedure msd3232, msd6464
    end interface

contains
    recursive subroutine ms32(ar, fst, lst)
        integer(int32),intent(inout):: ar(:)
        integer(int32),intent(in):: fst,lst
        integer(int32):: mdl

        if (lst-fst < 2) then
            if (ar(fst) > ar(lst)) call swap32(ar(fst),ar(lst))
            return
        end if

        mdl = (fst+lst)/2
        call ms32(ar, fst, mdl)
        call ms32(ar, mdl+1, lst)
        call merge32(ar, fst, mdl, lst)
    end subroutine

    subroutine merge32(ar, fst, mdl, lst)
        integer(int32),intent(inout):: ar(:)
        integer(int32),intent(in):: fst, mdl, lst
        integer(int32),allocatable:: tmp(:)
        integer(int32):: li, ri, ti

        allocate(tmp(lst-fst+1))

        li=fst
        ri=mdl+1
        ti=1

        do while (li <= mdl .and. ri <= lst)
            if (ar(li) <= ar(ri)) then
                tmp(ti) = ar(li)
                li=li+1
            else
                tmp(ti) = ar(ri)
                ri=ri+1
            end if
            ti=ti+1
        end do

        if (li <= mdl) then
            tmp(ti:) = ar(li:mdl)
        else
            tmp(ti:) = ar(ri:lst)
        end if

        ar(fst:lst) = tmp(:)
        deallocate(tmp)
    end subroutine

    subroutine swap32(x,y)
        integer(int32),intent(inout):: x,y
        integer(int32):: tmp
        tmp = x
        x = y
        y = tmp
    end subroutine

    recursive subroutine ms64(ar, fst, lst)
        integer(int64),intent(inout):: ar(:)
        integer(int64),intent(in):: fst,lst
        integer(int64):: mdl

        if (lst-fst < 2) then
            if (ar(fst) > ar(lst)) call swap64(ar(fst),ar(lst))
            return
        end if

        mdl = (fst+lst)/2
        call ms64(ar, fst, mdl)
        call ms64(ar, mdl+1, lst)
        call merge64(ar, fst, mdl, lst)
    end subroutine

    subroutine merge64(ar, fst, mdl, lst)
        integer(int64),intent(inout):: ar(:)
        integer(int64),intent(in):: fst, mdl, lst
        integer(int64),allocatable:: tmp(:)
        integer(int64):: li, ri, ti

        allocate(tmp(lst-fst+1))

        li=fst
        ri=mdl+1
        ti=1

        do while (li <= mdl .and. ri <= lst)
            if (ar(li) <= ar(ri)) then
                tmp(ti) = ar(li)
                li=li+1
            else
                tmp(ti) = ar(ri)
                ri=ri+1
            end if
            ti=ti+1
        end do

        if (li <= mdl) then
            tmp(ti:) = ar(li:mdl)
        else
            tmp(ti:) = ar(ri:lst)
        end if

        ar(fst:lst) = tmp(:)
        deallocate(tmp)
    end subroutine

    subroutine swap64(x,y)
        integer(int64),intent(inout):: x,y
        integer(int64):: tmp
        tmp = x
        x = y
        y = tmp
    end subroutine

    recursive subroutine msd3232(ar1, ar2, fst, lst)
        integer(int32),intent(inout):: ar1(:),ar2(:)
        integer(int32),intent(in):: fst,lst
        integer(int32):: mdl

        if (lst - fst < 2) then
            if (ar1(fst) > ar1(lst)) then
                call swap32(ar1(fst), ar1(lst))
                call swap32(ar2(fst), ar2(lst))
            end if
            return
        end if

        mdl = (fst+lst)/2

        call msd3232(ar1,ar2,fst,mdl)
        call msd3232(ar1,ar2,mdl+1,lst)
        call merged3232(ar1,ar2,fst,mdl,lst)
    end subroutine

    subroutine merged3232(ar1,ar2,fst,mdl,lst)
        integer(int32),intent(inout):: ar1(:),ar2(:)
        integer(int32),intent(in):: fst,mdl,lst
        integer(int32),allocatable:: t1(:),t2(:)
        integer(int32):: li,ri,ti

        allocate(t1(lst-fst+1), t2(lst-fst+1))

        li=fst
        ri=mdl+1
        ti=1

        do while(li <= mdl .and. ri <= lst)
            if (ar1(li) <= ar1(ri)) then
                t1(ti) = ar1(li)
                t2(ti) = ar2(li)
                li=li+1
            else
                t1(ti) = ar1(ri)
                t2(ti) = ar2(ri)
                ri=ri+1
            end if
            ti=ti+1
        end do

        if (li <= mdl) then
            t1(ti:) = ar1(li:mdl)
            t2(ti:) = ar2(li:mdl)
        else
            t1(ti:) = ar1(ri:lst)
            t2(ti:) = ar2(ri:lst)
        end if

        ar1(fst:lst) = t1(:)
        ar2(fst:lst) = t2(:)

        deallocate(t1,t2)
    end subroutine

    recursive subroutine msd6464(ar1, ar2, fst, lst)
        integer(int64),intent(inout):: ar1(:),ar2(:)
        integer(int64),intent(in):: fst,lst
        integer(int64):: mdl

        if (lst - fst < 2) then
            if (ar1(fst) > ar1(lst)) then
                call swap64(ar1(fst), ar1(lst))
                call swap64(ar2(fst), ar2(lst))
            end if
            return
        end if

        mdl = (fst+lst)/2

        call msd6464(ar1,ar2,fst,mdl)
        call msd6464(ar1,ar2,mdl+1,lst)
        call merged6464(ar1,ar2,fst,mdl,lst)
    end subroutine

    subroutine merged6464(ar1,ar2,fst,mdl,lst)
        integer(int64),intent(inout):: ar1(:),ar2(:)
        integer(int64),intent(in):: fst,mdl,lst
        integer(int64),allocatable:: t1(:),t2(:)
        integer(int64):: li,ri,ti

        allocate(t1(lst-fst+1), t2(lst-fst+1))

        li=fst
        ri=mdl+1
        ti=1

        do while(li <= mdl .and. ri <= lst)
            if (ar1(li) <= ar1(ri)) then
                t1(ti) = ar1(li)
                t2(ti) = ar2(li)
                li=li+1
            else
                t1(ti) = ar1(ri)
                t2(ti) = ar2(ri)
                ri=ri+1
            end if
            ti=ti+1
        end do

        if (li <= mdl) then
            t1(ti:) = ar1(li:mdl)
            t2(ti:) = ar2(li:mdl)
        else
            t1(ti:) = ar1(ri:lst)
            t2(ti:) = ar2(ri:lst)
        end if

        ar1(fst:lst) = t1(:)
        ar2(fst:lst) = t2(:)

        deallocate(t1,t2)
    end subroutine
end module

program main
    use,intrinsic :: iso_fortran_env
    use merge_sort_mod
    implicit none
    integer(int64):: n,i,div,b,ans,cnt(1000000)=0
    integer(int64), allocatable:: a(:)
    logical:: ng(1000000) = .false., ok(1000000) = .true.

    read*, n
    allocate(a(n))
    read*, a(:)

    call merge_sort(a,1_8,n)
    ans = 0
    do i=1,n
        b = a(i)
        cnt(b)=cnt(b)+1
        ng(b) = .true.
        div=2
        do while(div**2 <= b)
            do while(mod(b,div)==0)
                b=b/div
                if (ng(b) .or. ng(div)) then
                    ok(i) = .false.
                end if
            end do
            div=div+1
        end do
    ! print*, i, ok(i)
        if (ok(i)) ans=ans+1
    end do

    if (cnt(1) == 1) then
        print'(i0)', 1
    else if (cnt(1) > 1) then
        print'(i0)', 0
    else
        do i=1,1000000
            if (cnt(i) > 1 .and. ok(i)) ans=ans-cnt(i)
        end do

        print'(i0)', ans
    end if
end program main


module merge_sort_mod
    use,intrinsic :: iso_fortran_env
    implicit none
    private
    public:: merge_sort, double_merge_sort
    interface merge_sort
        module procedure ms32, ms64
    end interface

    interface double_merge_sort
        module procedure msd3232, msd6464
    end interface

contains
    recursive subroutine ms32(ar, fst, lst)
        integer(int32),intent(inout):: ar(:)
        integer(int32),intent(in):: fst,lst
        integer(int32):: mdl

        if (lst-fst < 2) then
            if (ar(fst) > ar(lst)) call swap32(ar(fst),ar(lst))
            return
        end if

        mdl = (fst+lst)/2
        call ms32(ar, fst, mdl)
        call ms32(ar, mdl+1, lst)
        call merge32(ar, fst, mdl, lst)
    end subroutine

    subroutine merge32(ar, fst, mdl, lst)
        integer(int32),intent(inout):: ar(:)
        integer(int32),intent(in):: fst, mdl, lst
        integer(int32),allocatable:: tmp(:)
        integer(int32):: li, ri, ti

        allocate(tmp(lst-fst+1))

        li=fst
        ri=mdl+1
        ti=1

        do while (li <= mdl .and. ri <= lst)
            if (ar(li) <= ar(ri)) then
                tmp(ti) = ar(li)
                li=li+1
            else
                tmp(ti) = ar(ri)
                ri=ri+1
            end if
            ti=ti+1
        end do

        if (li <= mdl) then
            tmp(ti:) = ar(li:mdl)
        else
            tmp(ti:) = ar(ri:lst)
        end if

        ar(fst:lst) = tmp(:)
        deallocate(tmp)
    end subroutine

    subroutine swap32(x,y)
        integer(int32),intent(inout):: x,y
        integer(int32):: tmp
        tmp = x
        x = y
        y = tmp
    end subroutine

    recursive subroutine ms64(ar, fst, lst)
        integer(int64),intent(inout):: ar(:)
        integer(int64),intent(in):: fst,lst
        integer(int64):: mdl

        if (lst-fst < 2) then
            if (ar(fst) > ar(lst)) call swap64(ar(fst),ar(lst))
            return
        end if

        mdl = (fst+lst)/2
        call ms64(ar, fst, mdl)
        call ms64(ar, mdl+1, lst)
        call merge64(ar, fst, mdl, lst)
    end subroutine

    subroutine merge64(ar, fst, mdl, lst)
        integer(int64),intent(inout):: ar(:)
        integer(int64),intent(in):: fst, mdl, lst
        integer(int64),allocatable:: tmp(:)
        integer(int64):: li, ri, ti

        allocate(tmp(lst-fst+1))

        li=fst
        ri=mdl+1
        ti=1

        do while (li <= mdl .and. ri <= lst)
            if (ar(li) <= ar(ri)) then
                tmp(ti) = ar(li)
                li=li+1
            else
                tmp(ti) = ar(ri)
                ri=ri+1
            end if
            ti=ti+1
        end do

        if (li <= mdl) then
            tmp(ti:) = ar(li:mdl)
        else
            tmp(ti:) = ar(ri:lst)
        end if

        ar(fst:lst) = tmp(:)
        deallocate(tmp)
    end subroutine

    subroutine swap64(x,y)
        integer(int64),intent(inout):: x,y
        integer(int64):: tmp
        tmp = x
        x = y
        y = tmp
    end subroutine

    recursive subroutine msd3232(ar1, ar2, fst, lst)
        integer(int32),intent(inout):: ar1(:),ar2(:)
        integer(int32),intent(in):: fst,lst
        integer(int32):: mdl

        if (lst - fst < 2) then
            if (ar1(fst) > ar1(lst)) then
                call swap32(ar1(fst), ar1(lst))
                call swap32(ar2(fst), ar2(lst))
            end if
            return
        end if

        mdl = (fst+lst)/2

        call msd3232(ar1,ar2,fst,mdl)
        call msd3232(ar1,ar2,mdl+1,lst)
        call merged3232(ar1,ar2,fst,mdl,lst)
    end subroutine

    subroutine merged3232(ar1,ar2,fst,mdl,lst)
        integer(int32),intent(inout):: ar1(:),ar2(:)
        integer(int32),intent(in):: fst,mdl,lst
        integer(int32),allocatable:: t1(:),t2(:)
        integer(int32):: li,ri,ti

        allocate(t1(lst-fst+1), t2(lst-fst+1))

        li=fst
        ri=mdl+1
        ti=1

        do while(li <= mdl .and. ri <= lst)
            if (ar1(li) <= ar1(ri)) then
                t1(ti) = ar1(li)
                t2(ti) = ar2(li)
                li=li+1
            else
                t1(ti) = ar1(ri)
                t2(ti) = ar2(ri)
                ri=ri+1
            end if
            ti=ti+1
        end do

        if (li <= mdl) then
            t1(ti:) = ar1(li:mdl)
            t2(ti:) = ar2(li:mdl)
        else
            t1(ti:) = ar1(ri:lst)
            t2(ti:) = ar2(ri:lst)
        end if

        ar1(fst:lst) = t1(:)
        ar2(fst:lst) = t2(:)

        deallocate(t1,t2)
    end subroutine

    recursive subroutine msd6464(ar1, ar2, fst, lst)
        integer(int64),intent(inout):: ar1(:),ar2(:)
        integer(int64),intent(in):: fst,lst
        integer(int64):: mdl

        if (lst - fst < 2) then
            if (ar1(fst) > ar1(lst)) then
                call swap64(ar1(fst), ar1(lst))
                call swap64(ar2(fst), ar2(lst))
            end if
            return
        end if

        mdl = (fst+lst)/2

        call msd6464(ar1,ar2,fst,mdl)
        call msd6464(ar1,ar2,mdl+1,lst)
        call merged6464(ar1,ar2,fst,mdl,lst)
    end subroutine

    subroutine merged6464(ar1,ar2,fst,mdl,lst)
        integer(int64),intent(inout):: ar1(:),ar2(:)
        integer(int64),intent(in):: fst,mdl,lst
        integer(int64),allocatable:: t1(:),t2(:)
        integer(int64):: li,ri,ti

        allocate(t1(lst-fst+1), t2(lst-fst+1))

        li=fst
        ri=mdl+1
        ti=1

        do while(li <= mdl .and. ri <= lst)
            if (ar1(li) <= ar1(ri)) then
                t1(ti) = ar1(li)
                t2(ti) = ar2(li)
                li=li+1
            else
                t1(ti) = ar1(ri)
                t2(ti) = ar2(ri)
                ri=ri+1
            end if
            ti=ti+1
        end do

        if (li <= mdl) then
            t1(ti:) = ar1(li:mdl)
            t2(ti:) = ar2(li:mdl)
        else
            t1(ti:) = ar1(ri:lst)
            t2(ti:) = ar2(ri:lst)
        end if

        ar1(fst:lst) = t1(:)
        ar2(fst:lst) = t2(:)

        deallocate(t1,t2)
    end subroutine
end module

program main
    use,intrinsic :: iso_fortran_env
    use merge_sort_mod
    implicit none
    integer(int64):: n,i,div,b,ans,cnt(1000000)=0
    integer(int64), allocatable:: a(:)
    logical:: ng(1000000) = .false., ok(1000000) = .true.

    read*, n
    allocate(a(n))
    read*, a(:)

    call merge_sort(a,1_8,n)
    ans = 0
    do i=1,n
        b = a(i)
        cnt(b)=cnt(b)+1
        ng(b) = .true.
        div=2
        do while(div**2 <= b)
            do while(mod(b,div)==0)
                b=b/div
                if (ng(b) .or. ng(div)) then
                    ok(i) = .false.
                end if
            end do
            div=div+1
        end do
    print*, i, ok(i)
        if (ok(i)) ans=ans+1
    end do

    if (cnt(1) == 1) then
        print'(i0)', 1
    else if (cnt(1) > 1) then
        print'(i0)', 0
    else
        do i=1,1000000
            if (cnt(i) > 1 .and. ok(i)) ans=ans-cnt(i)
        end do

        print'(i0)', ans
    end if
end program main


module merge_sort_mod
    use,intrinsic :: iso_fortran_env
    implicit none
    private
    public:: merge_sort, double_merge_sort
    interface merge_sort
        module procedure ms32, ms64
    end interface

    interface double_merge_sort
        module procedure msd3232, msd6464
    end interface

contains
    recursive subroutine ms32(ar, fst, lst)
        integer(int32),intent(inout):: ar(:)
        integer(int32),intent(in):: fst,lst
        integer(int32):: mdl

        if (lst-fst < 2) then
            if (ar(fst) > ar(lst)) call swap32(ar(fst),ar(lst))
            return
        end if

        mdl = (fst+lst)/2
        call ms32(ar, fst, mdl)
        call ms32(ar, mdl+1, lst)
        call merge32(ar, fst, mdl, lst)
    end subroutine

    subroutine merge32(ar, fst, mdl, lst)
        integer(int32),intent(inout):: ar(:)
        integer(int32),intent(in):: fst, mdl, lst
        integer(int32),allocatable:: tmp(:)
        integer(int32):: li, ri, ti

        allocate(tmp(lst-fst+1))

        li=fst
        ri=mdl+1
        ti=1

        do while (li <= mdl .and. ri <= lst)
            if (ar(li) <= ar(ri)) then
                tmp(ti) = ar(li)
                li=li+1
            else
                tmp(ti) = ar(ri)
                ri=ri+1
            end if
            ti=ti+1
        end do

        if (li <= mdl) then
            tmp(ti:) = ar(li:mdl)
        else
            tmp(ti:) = ar(ri:lst)
        end if

        ar(fst:lst) = tmp(:)
        deallocate(tmp)
    end subroutine

    subroutine swap32(x,y)
        integer(int32),intent(inout):: x,y
        integer(int32):: tmp
        tmp = x
        x = y
        y = tmp
    end subroutine

    recursive subroutine ms64(ar, fst, lst)
        integer(int64),intent(inout):: ar(:)
        integer(int64),intent(in):: fst,lst
        integer(int64):: mdl

        if (lst-fst < 2) then
            if (ar(fst) > ar(lst)) call swap64(ar(fst),ar(lst))
            return
        end if

        mdl = (fst+lst)/2
        call ms64(ar, fst, mdl)
        call ms64(ar, mdl+1, lst)
        call merge64(ar, fst, mdl, lst)
    end subroutine

    subroutine merge64(ar, fst, mdl, lst)
        integer(int64),intent(inout):: ar(:)
        integer(int64),intent(in):: fst, mdl, lst
        integer(int64),allocatable:: tmp(:)
        integer(int64):: li, ri, ti

        allocate(tmp(lst-fst+1))

        li=fst
        ri=mdl+1
        ti=1

        do while (li <= mdl .and. ri <= lst)
            if (ar(li) <= ar(ri)) then
                tmp(ti) = ar(li)
                li=li+1
            else
                tmp(ti) = ar(ri)
                ri=ri+1
            end if
            ti=ti+1
        end do

        if (li <= mdl) then
            tmp(ti:) = ar(li:mdl)
        else
            tmp(ti:) = ar(ri:lst)
        end if

        ar(fst:lst) = tmp(:)
        deallocate(tmp)
    end subroutine

    subroutine swap64(x,y)
        integer(int64),intent(inout):: x,y
        integer(int64):: tmp
        tmp = x
        x = y
        y = tmp
    end subroutine

    recursive subroutine msd3232(ar1, ar2, fst, lst)
        integer(int32),intent(inout):: ar1(:),ar2(:)
        integer(int32),intent(in):: fst,lst
        integer(int32):: mdl

        if (lst - fst < 2) then
            if (ar1(fst) > ar1(lst)) then
                call swap32(ar1(fst), ar1(lst))
                call swap32(ar2(fst), ar2(lst))
            end if
            return
        end if

        mdl = (fst+lst)/2

        call msd3232(ar1,ar2,fst,mdl)
        call msd3232(ar1,ar2,mdl+1,lst)
        call merged3232(ar1,ar2,fst,mdl,lst)
    end subroutine

    subroutine merged3232(ar1,ar2,fst,mdl,lst)
        integer(int32),intent(inout):: ar1(:),ar2(:)
        integer(int32),intent(in):: fst,mdl,lst
        integer(int32),allocatable:: t1(:),t2(:)
        integer(int32):: li,ri,ti

        allocate(t1(lst-fst+1), t2(lst-fst+1))

        li=fst
        ri=mdl+1
        ti=1

        do while(li <= mdl .and. ri <= lst)
            if (ar1(li) <= ar1(ri)) then
                t1(ti) = ar1(li)
                t2(ti) = ar2(li)
                li=li+1
            else
                t1(ti) = ar1(ri)
                t2(ti) = ar2(ri)
                ri=ri+1
            end if
            ti=ti+1
        end do

        if (li <= mdl) then
            t1(ti:) = ar1(li:mdl)
            t2(ti:) = ar2(li:mdl)
        else
            t1(ti:) = ar1(ri:lst)
            t2(ti:) = ar2(ri:lst)
        end if

        ar1(fst:lst) = t1(:)
        ar2(fst:lst) = t2(:)

        deallocate(t1,t2)
    end subroutine

    recursive subroutine msd6464(ar1, ar2, fst, lst)
        integer(int64),intent(inout):: ar1(:),ar2(:)
        integer(int64),intent(in):: fst,lst
        integer(int64):: mdl

        if (lst - fst < 2) then
            if (ar1(fst) > ar1(lst)) then
                call swap64(ar1(fst), ar1(lst))
                call swap64(ar2(fst), ar2(lst))
            end if
            return
        end if

        mdl = (fst+lst)/2

        call msd6464(ar1,ar2,fst,mdl)
        call msd6464(ar1,ar2,mdl+1,lst)
        call merged6464(ar1,ar2,fst,mdl,lst)
    end subroutine

    subroutine merged6464(ar1,ar2,fst,mdl,lst)
        integer(int64),intent(inout):: ar1(:),ar2(:)
        integer(int64),intent(in):: fst,mdl,lst
        integer(int64),allocatable:: t1(:),t2(:)
        integer(int64):: li,ri,ti

        allocate(t1(lst-fst+1), t2(lst-fst+1))

        li=fst
        ri=mdl+1
        ti=1

        do while(li <= mdl .and. ri <= lst)
            if (ar1(li) <= ar1(ri)) then
                t1(ti) = ar1(li)
                t2(ti) = ar2(li)
                li=li+1
            else
                t1(ti) = ar1(ri)
                t2(ti) = ar2(ri)
                ri=ri+1
            end if
            ti=ti+1
        end do

        if (li <= mdl) then
            t1(ti:) = ar1(li:mdl)
            t2(ti:) = ar2(li:mdl)
        else
            t1(ti:) = ar1(ri:lst)
            t2(ti:) = ar2(ri:lst)
        end if

        ar1(fst:lst) = t1(:)
        ar2(fst:lst) = t2(:)

        deallocate(t1,t2)
    end subroutine
end module

program main
    use,intrinsic :: iso_fortran_env
    use merge_sort_mod
    implicit none
    integer(int32):: n,i,div,b,ans,cnt(1000000)=0
    integer(int32), allocatable:: a(:)
    logical:: ng(1000000) = .false., ok(1000000) = .true.

    read*, n
    allocate(a(n))
    read*, a(:)

    call merge_sort(a,1,n)
    ans = 0
    do i=1,n
        b = a(i)
        cnt(b)=cnt(b)+1
        ng(b) = .true.
        div=2
        do while(div**2 <= b)
            do while(mod(b,div)==0)
                b=b/div
                if (ng(b) .or. ng(div)) then
                    ok(i) = .false.
                end if
            end do
            div=div+1
        end do
    ! print*, i, ok(i)
        if (ok(i)) ans=ans+1
    end do

    do i=1,1000000
        if (cnt(i) > 1 .and. ok(i)) ans=ans-cnt(i)
    end do

    print'(i0)', ans
end program main


program not_divisible
  implicit none
  integer :: n, a(200000) = 0, c(1000000) = 0, m = 0, i, j
  read(*,*) n
  read(*,*) a(1:n)
  do i = 1, n
    c(a(i)) = c(a(i))+1
  end do
  select case(c(1))
  case(1)
    write(*,'(i0)') 1
    stop
  case(2:)
    write(*,'(i0)') 0
    stop
  end select
  do i = 1, n
    if (c(a(i)) > 1) cycle
    do j = 2, a(i)
      if (j*j > a(i)) exit
      if (mod(a(i),j) /= 0) cycle
      if (c(j) > 0 .or. c(a(i)/j) > 0) goto 100
    end do
    m = m+1
  100 end do
  write(*,'(i0)') m
end program not_divisible


program b169b

implicit none
integer(16), allocatable :: a(:)
integer(16) :: b, c, t
integer :: i, n

t = 1.0
b = 1000000000
c = 1000000000

read *, n

allocate(a(1:n))
 read *, a(:)

do i= 1, n
 t = t * a(i)
end do

if (t <= b * c) then
 print *, t
else
 print *, -1
end if

end program b169b

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


program main
	implicit none
    integer(16)::ans
    integer(8)::n,i
    integer(16),allocatable::a(:)
    read(*,*) n
    allocate(a(n))
    read(*,*)a
    ans=1
    do i=1,n
    	ans=ans*a(i)
    end do

    if(ans>10_16**18)then
    	write(*,*) -1
    else
    	write(*,*) ans
    end if
    deallocate(a)
    stop
end program main


program test2
implicit none
integer :: n , i , num
integer(8) :: a

read *, n

num = 0
do i = 1 , n
 	read *, a
	num = num * a
 	if(num > 1e+18) then
    	print *,-1
    end if
end do

print *,num

end program test2


program test2
implicit none
integer :: n , i , num
integer :: a

read *, n

num = 0
do i = 1 , n
  read *, a
	num = num * a
 	if(num > 1e+18) then
    	print *,-1
    end if
end do

print *,num

end program test2


program kk
    implicit none
    integer(8), allocatable :: a(:)
    integer(8) :: n, i
    integer(8) :: s
    read(*,*) n
    allocate(a(n))
    read(*,*) a
    s = 1
    do i = 1, n
    	if (a(i) == 0) then
        	write(*,*) 0
        	stop
     	end if
    end do
    do i = 1, n
    	s = s * a(i)
    	if (s > 10_8**18) then
        	write(*,*) -1
        	stop
        end if
    end do

    write(*,*) s

    deallocate(a)
    stop
end program kk


program kk
	implicit none
    integer(8), allocatable :: a(:)
    integer(8) :: n, i
    integer(8) :: s
    read(*,*) n
    allocate(a(n))
    read(*,*) a
    s = 1
    do i = 1, n
    	if (a(i) == 0) then
        	write(*,*) 0
            stop
        end if
    	s = s * a(i)
    end do
    if (s > 10_8**18) then
        write(*,*) -1
        stop
    else
    	write(*,*) s
    end if

    deallocate(a)
	stop
end program kk


program kk
	implicit none
    integer(16), allocatable :: a(:)
    integer(16) :: n, i
    integer(16) :: s
    read(*,*) n
    allocate(a(n))
    read(*,*) a
    s = 1
    do i = 1, n
    	s = s * a(i)
    end do
    if (s > 1e+17) then
        write(*,*) -1
    else
    	write(*,*) s
    end if

    deallocate(a)

end program kk


program kk
	implicit none
    integer(16), allocatable :: a(:)
    integer(16) :: n, i
    integer(16) :: s
    read(*,*) n
    allocate(a(n))
    read(*,*) a
    s = 1
    do i = 1, n
    	s = s * a(i)
        if (s > 1e+18) then
        	stop
        end if
    end do
    write(*,*) s

    deallocate(a)

end program kk


program ABC169B
    implicit none
    integer(8)::N,i
    integer(16)::ans
    integer(8),allocatable,dimension(:)::A
    read*,N
    allocate(A(N))
    read*,A
    call heapsort(N,A)
    if(A(1)==0)then
        print'(i0)',0
    else
        ans=1
        do i=1,N
            ans=ans*A(i)
            if(ans>10_8**18)then
                ans=-1
                exit
            end if
        end do
        print'(i0)',ans
    end if
end program ABC169B

subroutine heapsort(n,array)
    implicit none
    integer(8),intent(in) :: n
    integer(8),intent(inout) :: array(1:n)

    integer(8) ::i,k,j,l
    integer(8) :: t

    if(n.eq.1)return

    l=n/2+1
    k=n
    do while(k.ne.1)
       if(l.gt.1)then
          l=l-1
          t=array(L)
       else
          t=array(k)
          array(k)=array(1)
          k=k-1
          if(k.eq.1) then
             array(1)=t
             exit
          endif
       endif
       i=l
       j=l+l
       do while(j.le.k)
          if(j.lt.k)then
             if(array(j).lt.array(j+1))j=j+1
          endif
          if (t.lt.array(j))then
             array(i)=array(j)
             i=j
             j=j+j
          else
             j=k+1
          endif
       enddo
       array(i)=t
    enddo

    return
end subroutine heapsort


program prob2
    implicit none
    integer(8)::N, i
    integer(16):: ans
    integer(8), allocatable::A(:)
    read(*,*) N
    allocate(A(N))
    ans = 1

    read(*,*) A
    do i = 1,N
        if(A(i) == 0)then
            write(*,*) 0
            stop
        end if
    end do

    do i = 1, N
        ans = ans*A(i)
        if(ans > 10_8**18)then
            write(*,*) -1
            stop
        end if
    end do
    write(*,*) ans

    deallocate(A)
    stop
end program prob2


program abc169b
  use iso_c_binding
  implicit none

  integer :: n, i
  integer(c_int128_t) :: x, a(100000)
  integer(c_int128_t),parameter :: mx = 10_16**18

  read *, n
  read *, a(1:n)

  if (any(a(1:n) == 0)) then
     print *, 0
     stop
  end if

  x = 1

  do i = 1,n
     x = x * a(i)
     if (x > mx) then
        x = -1
        exit
     end if
  end do

  print *, x
end program abc169b


program abc169b
  use iso_c_binding
  implicit none

  integer :: n, i
  integer(c_int128_t) :: x, a(100000)
  integer(c_int128_t),parameter :: mx = 10_8**18

  read *, n
  read *, a(1:n)

  if (any(a(1:n) == 0)) then
     print *, 0
     stop
  end if

  x = 1

  do i = 1,n
     x = x * a(i)
     if (x > mx) then
        x = -1
        exit
     end if
  end do

  print *, x
end program abc169b


program abc169b
  use iso_fortran_env
  implicit none

  integer :: n, i
  integer(int64) :: x, a(100000)
  integer(int64),parameter :: mx = 10_8**18

  read *, n
  read *, a(1:n)

  if (any(a(1:n) == 0)) then
     print *, 0
     stop
  end if

  x = 1

  do i = 1,n
     if((mx+x-1) / x * a(i) > mx .and. (mx+a(i)-1) / a(i) * x > mx) then
        print *, -1
        stop
     else
        x = x * a(i)
     end if
  end do

  print *, x
end program abc169b


program answer
  implicit none

  integer(16), allocatable :: A(:)
  integer(16) :: ans, N, i

  read(*,*) N
  allocate(A(N))
  read(*,*) (A(i), i=1,N)
  ans=1_16
 do i = 1, N
     if	(A(i)==0) then
	write(*,*) 0
      	stop
     end if
  end do

  do i = 1, N
     ans = A(i)*ans
  if (ans>10_16**18) then
      ans = -1
     stop
   end if
  end do
  write(*,*) ans

  deallocate(A)
  stop
end program answer


program answer
  implicit none

  integer(16), allocatable :: A(:)
  integer(16) :: ans, N, i

  read(*,*) N
  allocate(A(N))
  read(*,*) (A(i), i=1,N)
  ans=1_16
 do i = 1, N
     if	(A(i)==0) then
	write(*,*) 0
      	stop
     end if
  end do

  do i = 1, N
     ans = A(i)*ans
   end do

    if (ans>10_16**18) then
      ans = -1
    end if
  write(*,*) ans

  deallocate(A)
  stop
end program answer


program answer
  implicit none

  integer(16), allocatable :: A(:)
  integer(16) :: ans, N, i

  read(*,*) N
  allocate(A(N))
  read(*,*) (A(i), i=1,N)
  ans=1_16
  do i = 1, N
     ans = A(i)*ans
    if (ans>10_16**18) then
      ans = -1
    end if
   end do

  write(*,*) ans

  deallocate(A)
  stop
end program answer


program mondai2
implicit none
integer*8 i, n, b
integer*8 a(100000)
b = 1
i = 1
read(*,*) n
read(*,*) (a(i), i=1,n)
i = 1
do while (i<=n)
  if (a(i) == 0)then
    b = 0
    exit
  end if
  b = b * a(i)
  i = i + 1
  if (dble(b) / 10.0 ** 18.0 > 1) then
    b = -1
    exit
  end if
end do

write(*,*) int(b)
end program mondai2


program mondai2
implicit none
integer*8 i, n, b
integer*8 a(100000)
b = 1
i = 1
read(*,*) n
read(*,*) (a(i), i=1,n)
i = 1
do while (i<=n)
  b = b * a(i)
  i = i + 1
  if (dble(b) / 10.0 ** 18.0 > 1) then
    b = -1
    exit
  end if
end do

write(*,*) int(b)
end program mondai2


program mondai2
implicit none
integer*8 i, n, b
integer*8 a(100000)
b = 1
i = 1
read(*,*) n
read(*,*) (a(i), i=1,n)
i = 1
do while (i<=n)
  b = b * a(i)
  i = i + 1
end do
if (dble(b) / (10.0 ** 18.0) > 1) then
  b = -1
end if
write(*,*) int(b)
end program mondai2


program ABC169B
    implicit none
    integer(8)::N,ans,i
    integer(8),allocatable,dimension(:)::A
    read*,N
    allocate(A(N))
    read*,A
    call heapsort(N,A)
    ans=1
    do i=1,N
        ans=ans*A(i)
        if(ans/1000000000+min(1,mod(ans,1000000000))>1000000000)then
            ans=-1
            exit
        end if
    end do
    print'(i0)',ans
end program ABC169B

subroutine heapsort(n,array)
    implicit none
    integer(8),intent(in) :: n
    integer(8),intent(inout) :: array(1:n)

    integer(8) ::i,k,j,l
    integer(8) :: t

    if(n.eq.1)return

    l=n/2+1
    k=n
    do while(k.ne.1)
       if(l.gt.1)then
          l=l-1
          t=array(L)
       else
          t=array(k)
          array(k)=array(1)
          k=k-1
          if(k.eq.1) then
             array(1)=t
             exit
          endif
       endif
       i=l
       j=l+l
       do while(j.le.k)
          if(j.lt.k)then
             if(array(j).lt.array(j+1))j=j+1
          endif
          if (t.lt.array(j))then
             array(i)=array(j)
             i=j
             j=j+j
          else
             j=k+1
          endif
       enddo
       array(i)=t
    enddo

    return
end subroutine heapsort


program mp2
  implicit none
  integer(16) :: N, i
  integer(16),allocatable :: a(:)
  integer(16) :: r

  read(*,*) N
  allocate(a(N))
  read(*,*) a

  r = 1_16
  do i = 1, N
     if (a(i) == 0) then
        write(*,*) 0
     end if
     stop
  end do

  do i = 1, N
     if (a(i) > 10_16**18_16) then
        write(*,*) -1
        stop
     else
        r = r * a(i)
        if (r > 10_16**18_16) then
           write(*,*) -1
           stop
        end if
     end if
  end do
  write(*,*) r
  stop
end program mp2


program multi2
  implicit none

  integer(16), allocatable :: a(:)
  integer(16) :: n, i, ans, l, t
  read(*,*) n

  allocate(a(n))
  read(*,*) a

  l = 1
  do i = 1, 18
     l = l * 10
  end do

  if (product(a) /= 0) then
  ans = 1
  do i = 1, n
     ans = ans * a(i)
     if (ans > l) then
        write(*,*) -1
        t = 0
        exit
     end if

     t = 1
  end do

  if (t == 1) then
     write(*,*) ans
  end if

else
   write(*,*) 0
end if

  deallocate(a)

  stop
end program multi2


program multi2
  implicit none

  integer(16), allocatable :: a(:)
  real(16), allocatable :: ar(:), b(:)
  integer(16) :: n, i, ans, l, t
  read(*,*) n

  allocate(a(n))
  allocate(ar(n))
  allocate(b(n))
  read(*,*) a

  l = 1
  do i = 1, 18
     l = l * 10
  end do

  if (product(a) /= 0) then
  ans = 1
  do i = 1, n
     ans = ans * a(i)
     if (ans > l) then
        write(*,*) -1
        t = 0
        exit
     end if

     t = 1
  end do

  if (t == 1) then
     write(*,*) ans
  end if

else
   write(*,*) 0
end if

  deallocate(a, ar, b)

  stop
end program multi2


program b169b

implicit none
integer(8), allocatable :: a(:)
integer(8) :: b, c, t
integer :: i, n

t = 1.0
b = 1000000000
c = 1000000000

read *, n

allocate(a(1:n))
 read *, a(:)

do i= 1, n
 t = t * a(i)
end do

if (t <= b * c) then
 print *, t
else
 print *, -1
end if

end program b169b


program mondai2
implicit none
integer*8 i, n, b
integer*8 a(100000)
b = 1
i = 1
read(*,*) n
read(*,*) (a(i), i=1,n)
i = 1
do while (i<=n)
  b = b * a(i)
  i = i + 1
end do
if (b / 10.0 ** 18.0 > 0) then
  b = -1
end if
write(*,*) b
end program mondai2


program b169b

implicit none
integer(16), allocatable :: a(:)
integer(16) :: b, c, t
integer :: i, n

t = 1.0
b = 1000000000
c = 1000000000

read *, n

allocate(a(1:n))
 read *, a(:)

do i= 1, n
 t = t * a(i)
end do

if (t <= b * c) then
 print *, t
else
 print *, -1
end if

end program b169b


program mondai2
implicit none
integer*8 i, n, b
integer*8 a(100000)
b = 1
i = 1
read(*,*) n
read(*,*) (a(i), i=1,n)
i = 1
do while (i<=n)
  b = b * a(i)
  i = i + 1
end do
if (dble(b) / 10.0 ** 18.0 > 1) then
  b = -1.0
end if
write(*,*) b
end program mondai2


program b169b

implicit none
integer(16), allocatable :: a(:), t
integer :: i, n

t = 1.0

read *, n

allocate(a(1:n))
 read *, a(:)

do i= 1, n
 t = t * a(i)
end do

if (t > 1.0 ** 18.0) then
 print *, -1
else
 print *, t
end if

end program b169b


program mp2
  implicit none
  integer(16) :: N, i
  integer(16),allocatable :: a(:)
  integer(16) :: r

  read(*,*) N
  allocate(a(N))
  read(*,*) a

  r = 1_16
  do i = 1, N
     if (a(i) > 10_16**18_16) then
        write(*,*) -1
        stop
     else
        r = r * a(i)
        if (r > 10_16**18_16) then
           write(*,*) -1
           stop
        end if
     end if
  end do
  write(*,*) r
  stop
end program mp2


program multi2
  implicit none

  integer(16), allocatable :: a(:)
  real(16), allocatable :: ar(:), b(:)
  integer(16) :: n, i, ans
  read(*,*) n

  allocate(a(n))
  allocate(ar(n))
  allocate(b(n))
  read(*,*) a

  if (product(a) == 0) then
     write(*,*) 0
  else

  do i = 1, n
     ar(i) = real(a(i),16)
     b(i) = log(ar(i))

  end do
  if (sum(b) > 18.0_16 * log(10.0_16)) then
     write(*,*) -1
  else
        ans = product(a)

        write(*,*) ans
  end if
end if

  deallocate(a, ar, b)

  stop
end program multi2


program main
  implicit none
  integer :: n
  integer(16), allocatable :: an(:)
  integer :: i
  integer(16) :: threshold, ans
  logical :: flag
  threshold = 1

  do i = 1, 18
    threshold = threshold * 10
  enddo

  read *, n
  allocate(an(n))
  read *, an(1:n)

  if (minval(an) == 0)then
    print "(i0)", 0
  else

    flag = .true.
    ans = 1
    do i = 1, n
      ans = ans * an(i)
      if (ans > threshold)then
        flag = .false.
        exit
      endif
    enddo

    if (flag)then
      print "(i0)", ans
    else
      print "(i0)", -1
    endif
  endif
end program


program main
integer(8), allocatable :: x(:)
integer(8) :: N,S,A
A = 10**18_8
read *,N
allocate(x(N))
read *,x
S = 1
do i = 1, N
S = S * x(i)
end do

if ( S > A ) then
	S = -1
end if
print '(i0)',S
end program main


program main
  implicit none
  integer :: n
  integer(16), allocatable :: an(:)
  integer :: i
  integer(16) :: threshold, ans
  logical :: flag
  threshold = 1

  do i = 1, 18
    threshold = threshold * 10
  enddo

  read *, n
  allocate(an(n))
  read *, an(1:n)

  flag = .true.
  ans = 1
  do i = 1, n
    ans = ans * an(i)
    if (ans > threshold)then
      flag = .false.
      exit
    endif
  enddo

  if (flag)then
    print "(i0)", ans
  else
    print "(i0)", -1
  endif
end program


program main
  implicit none
  integer :: n
  integer(16), allocatable :: an(:)
  integer :: i
  integer(16) :: threshold, ans
  logical :: flag
  threshold = 1

  do i = 1, 18
    threshold = threshold * 10
  enddo

  read *, n
  allocate(an(n))
  read *, an(1:n)

  flag = .true.
  ans = 1
  do i = 1, n
    ans = ans * an(n)
    if (ans > threshold)then
      flag = .false.
      exit
    endif
  enddo

  if (flag)then
    print "(i0)", ans
  else
    print "(i0)", -1
  endif
end program


program mainb
  implicit none
  integer(8)::n,i
  real(8),allocatable::a(:)
  real(8)::ans

   read*,n
   allocate(a(n))

  read*,a
  !print*,a

  ans=1
  do i=1,n
   ans=a(i)*ans
   !print*,ans

  end do

if (ans/100.0>10000000000000000.0) then
     ans=-1
   end if

200 print*,int(ans)

end program mainb


program mainb
  implicit none
  integer(8)::n,i
  real(8),allocatable::a(:)
  real(8)::ans

   read*,n
   allocate(a(n))

  read*,a
  print*,a

  ans=1
  do i=1,n
   ans=a(i)*ans
   print*,ans

  end do

if (ans>1000000000000000000.0) then
     ans=-1
   end if

200 print*,int(ans)

end program mainb


program sample
    implicit none

    integer(8) :: y,n,i
    integer(8),allocatable :: x(:)

    read(*,*)n
    allocate(x(n))
    read(*,*)x
    y=1
    do i=1,n
        if (x(i)==0)then
            write(*,*) 0
            deallocate(x)
            stop
        end if
    end do
    do i=1,n
        y=y*x(i)
        if (y>10_8**18_8) then
            write(*,*) -1
            deallocate(x)
            stop
        end if
    end do
    write(*,*) y
    deallocate(x)
    stop
end program sample


program multi2
  implicit none

  integer(8), allocatable :: a(:)
  integer(8) :: n, i, ans

  read(*,*) n

  allocate(a(n))
  read(*,*) a

  ans = 1
  do i = 1, n
     ans = ans * a(i)
  end do

  if(ans > 10**18_8) then
     write(*,*) -1
  else
     write(*,*) ans
  end if

  deallocate(a)

  stop
end program multi2


program main
	implicit none
    real N, i, Ans
    integer,allocatable::A(:)
    read(*,*)N
    allocate(A(1:N))
    read(*,*)A
    Ans = 1
    do i=1,N
    	Ans = Ans * A(i)
    end do
    if (log(Ans)>18) then
    	Ans = -1
    end if
    write(*,*)Ans
end program main


program multi2
  implicit none

  integer(16), allocatable :: a(:)
  integer(16) :: n, i, ans
  integer(16), parameter :: l = 10 ** 18_16

  read(*,*) n

  allocate(a(n))
  read(*,*) a

  ans = 1
  do i = 1, n
     ans = ans * a(i)
     if(ans > l) then
        write(*,*) -1
        exit
     end if

  end do

  write(*,*) ans

  deallocate(a)

  stop
end program multi2


program main
	implicit none
    integer N, i, Ans
    integer,allocatable::A(:)
    read(*,*)N
    allocate(A(1:N))
    read(*,*)A
    Ans = 1
    do i=1,N
    	Ans = Ans * A(i)
    end do
    if ((log(real(Ans))>18).or.(Ans==0)) then
    	Ans = -1
    end if
    write(*,*)Ans
end program main


program main
	implicit none
    integer N, i, Ans
    integer,allocatable::A(:)
    read(*,*)N
    allocate(A(1:N))
    read(*,*)A
    Ans = 1
    do i=1,N
    	Ans = Ans * A(i)
    end do
    if (log(real(Ans))>18) then
    	Ans = -1
    end if
    write(*,*)Ans
end program main


program b169b

implicit none
integer(16), allocatable :: a(:), t
integer :: i, n

t = 1.0

read *, n

allocate(a(1:n))
 read *, a(:)

do i= 1, n
 t = t * a(i)
end do

if (t > 1.0E18) then
 print *, -1
else
 print '(I18)', t
end if

end program b169b


program main
  implicit none
  integer(16) i, N, ans, lim, frag, frag2
  integer(16),allocatable :: A(:)
  read(*, *)N
  allocate(A(N))
  read(*, *) (A(i), i = 1,N)
  ans = 1
  lim = 1
  frag = 0
  frag2 = 1
  do i = 1,18
    lim = lim * 10
  end do
  !lim = 10**18
  do i = 1,N
    ans = ans*A(i)
    if (ans > lim) then
      frag = 1
    endif
    if (A(i) == 0) then
      frag2 = 0
    endif
  end do
  !frag = frag * frag2
  if (frag == 1) then
    ans = -1
  end if
  if (frag2 == 0) then
    ans = 0
  end if
  write(*, *) ans
end program main


program sample
    implicit none

    integer(8) :: y,n,i
    integer(8),allocatable :: x(:)

    ! 整数を読み込む
    read(*,*)n
    allocate(x(n))
    read(*,*)x
    y=1
    do i=1,n
        y=y*x(i)
        if (y>10_8**18_8) then
            write(*,*) -1
            deallocate(x)
            stop
        end if
    end do
    write(*,*) y
    deallocate(x)
    stop
  end program sample


program main
  implicit none
  integer(8) i, N, ans, lim, frag, frag2
  integer(8),allocatable :: A(:)
  read(*, *)N
  allocate(A(N))
  read(*, *) (A(i), i = 1,N)
  ans = 1
  lim = 1
  frag = 0
  frag2 = 1
  do i = 1,18
    lim = lim * 10
  end do
  !lim = 10**18
  do i = 1,N
    ans = ans*A(i)
    if (ans > lim) then
      frag = 1
    endif
    if (A(i) == 0) then
      frag2 = 0
    endif
  end do
  !frag = frag * frag2
  if (frag == 1) then
    ans = -1
  end if
  if (frag2 == 0) then
    ans = 0
  end if
  write(*, *) ans
end program main


program mp2
  implicit none
  integer(16) :: N, i
  integer(16),allocatable :: a(:)
  integer(16) :: r
  read(*,*) N
  allocate(a(N))
  read(*,*) a
  r = 1_16
  do i = 1, N
     r = r * a(i)
     if (r >= 10_16**18_16) then
        write(*,*) -1
        stop
     end if
  end do
  write(*,*) r
  stop
end program mp2


program b
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: n,ans,i
    integer(int64),allocatable:: a(:)

    read*, n
    allocate(a(n))
    read*, a(:)

    if (minval(a) == 0) then
        print'(i0)', 0
        stop
    end if

    ans=1

    do i=1,n
        if (log10(dble(ans))+log10(dble(a(i))) > 18) then
            print'(i0)', -1
            stop
        else
            if (ans*a(i) > 1000000000000000000_8) then
                print'(i0)', -1
                stop
            end if
            ans=ans*a(i)
        end if
    end do
    print'(i0)', ans
end program b


program abc169b
   integer :: n, i
   integer(8), allocatable :: a(:)
   integer(8) :: ans, limit=10**9

   read(*,*) n
   allocate(a(n))
   read(*,*) a(:)

   ans = a(1)
   do i = 2, n
      ans = ans * a(i)
   enddo

   limit = limit**2
   if (ans > limit ) then
      print *, -1
   else
      print *, ans
   end if
end program abc169b


program b
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: n,ans,i
    integer(int64),allocatable:: a(:)

    read*, n
    allocate(a(n))
    read*, a(:)

    if (minval(a) == 0) then
        print'(i0)', 0
        stop
    end if

    ans=1

    do i=1,n
        if (log10(dble(ans))+log10(dble(a(i))) > 1d18) then
            print'(i0)', -1
            stop
        else
            if (ans*a(i) > 1000000000000000000_8) then
                print'(i0)', -1
                stop
            end if
            ans=ans*a(i)
        end if
    end do
    print'(i0)', ans
end program b


program mp2
  implicit none
  integer(4) :: N, i
  integer(16),allocatable :: a(:)
  integer(16) :: r
  read(*,*) N
  allocate(a(N))
  read(*,*) a
  r = 1_16
  do i = 1, N
     r = r * a(i)
     if (r > 10**18_16) then
        write(*,*) -1
        stop
     end if
  end do
  write(*,*) r
  stop
end program mp2


program b
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: n,ans,i
    integer(int64),allocatable:: a(:)

    read*, n
    allocate(a(n))
    read*, a(:)

    if (minval(a) == 0) then
        print'(i0)', 0
        stop
    end if

    ans=1
    do i=1,n
        if (log10(dble(ans))+log10(dble(a(i))) > 10d0**18) then
            print'(i0)', -1
            stop
        else
            if (ans*a(i) > 10_8**18) then
                print'(i0)', -1
                stop
            end if
            ans=ans*a(i)
        end if
    end do
    print'(i0)', ans
end program b


program multiplication2
  implicit none
  integer(16), parameter :: u = 10_16**18
  integer :: n, i
  integer(16) :: a(100000) = 0, x = 1
  read(*,*) n
  read(*,*) a(1:n)
  if (minval(a(1:n)) == 0) then
    write(*,'(i0)') 0
    stop
  end if
  do i = 1, n
    x = x*a(i)
    if (x > u) then
      write(*,'(i0)') -1
      stop
    end if
  end do
  write(*,'(i0)') x
end program multiplication2


program main
	implicit none
    integer(16)::a
    real(16)::b
    read(*,*) a,b
    write(*,*) int(a*b,kind=16)
    stop
end program main

Score : 300 points

Problem Statement

Compute A \times B, truncate its fractional part, and print the result as an integer.

Constraints

0 \leq A \leq 10^{15}

0 \leq B < 10

A is an integer.

B is a number with two digits after the decimal point.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the answer as an integer.

Sample Input 1

198 1.10

Sample Output 1

217

We have 198 \times 1.10 = 217.8. After truncating the fractional part, we have the answer: 217.

Sample Input 2

1 0.01

Sample Output 2

0

Sample Input 3

1000000000000000 9.99

Sample Output 3

9990000000000000


program prob3
	implicit none
    integer(16) :: A
    real(16) :: B
    integer(16) :: intB,ans
    read(*,*) A, B
    intB = nint(B*100_16)
    ans = A*intB
    ans = ans / 100
    write(*,*) ans
	stop
end program prob3


program prob3
	implicit none
    integer(16) :: A
    real(16) :: B
    integer(16) :: ans
    read(*,*) A, B
    ans = int(A*B+0.001,kind=16)
    write(*,*) ans
	stop
end program prob3


program prob3
	implicit none
    integer(16) :: A
    real(16) :: B
    integer(16) :: ans
    read(*,*) A, B
    ans = int(A*B+0.5,kind=16)
    write(*,*) ans
	stop
end program prob3


program ABC169C
    implicit none
    real(16)::a,b
    read*,a,b
    print*,int(a*b,16)
end program ABC169C


program abc169c
  use iso_fortran_env
  implicit none
  integer(int64) :: a
  real :: b
  read *, a, b
  print *, a * nint(b*100) / 100
end program abc169c


program c_
    use,intrinsic :: iso_fortran_env
    use, intrinsic:: ieee_arithmetic
    implicit none
    integer(int64):: a
    real(real32):: b
    call ieee_set_rounding_mode(ieee_up)
    read*, a,b
    print'(i0)', (a*int(100*b))/100
end program c_


      PROGRAM piyo
      IMPLICIT NONE
      INTEGER,PARAMETER :: prec = 16
      real(prec) :: b
      integer(prec) :: aa,bb

      read*,aa,b

      bb = nint( b*100_prec )
      print*,aa*bb/100_prec

      stop
      !debugg

      !BADBADBADBADBADBADBADBADBADBADBADBAD!

      END PROGRAM


      PROGRAM piyo
      IMPLICIT NONE
      INTEGER,PARAMETER :: prec = 16
      real(prec) :: b
      integer(prec) :: aa,bb

      read*,aa,b

      bb = nint( b*100_prec )
      print'(i0)',aa*bb/100_prec

      stop
      !debugg

      END PROGRAM


program c_
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: a,ans
    real(real128):: b

    read*, a,b
    ans = int(a*b, kind=int64)
    print'(i0)', ans
end program c_


program main
integer(16) :: A,D
real(16) :: B,C
read *,A,B
C = A*B
D = int(C,16)
print '(i0)',D
end program main


program main
integer(16) :: A,D
double precision :: B,C
read *,A,B
C = A*B
D = int(C,16)
print '(i0)',D
end program main


program ABC169_C
  integer(16)::A,C
  real(16)::B
  read(*,*)A,B
  write(*,*)int(A*B,16)
end program ABC169_C


program prob3
    implicit none
    integer(16)::A, ans
    real(16)::B
    read(*,*) A, B
    ans = int(A*B)
    write(*,*) ans

    stop
end program prob3


program mainc

 implicit none
 real(8)::a,b,ans
 integer(8)::answer

 read*,a,b

 ans=a*b*100
 answer=int(ans)
 print*,answer

end program mainc


program c_
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: a,b1,b2,b3
    real(real64):: b

    read*, a,b
    b1 = int(b)
    b2 = int(b*10)-b1*10
    b3 = int(b*100)-b2*10-b1*100
    print'(i0)', a*b1+a*b2/10+a*b3/100
end program c_


program c_
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: a
    real(real64):: b

    read*, a,b
    print'(i0)', (a*int(b*1d2))/100
end program c_


program b169c

implicit none
integer(16) :: a, c, d
real :: b

read *, a, b

c = int(b * 100)
d = int(a * c / 100)

print *, d

end program b169c


program coin
implicit none
integer(16) :: a, r, n, ans, lim, fir, i
real(16) :: b, bb

read*,a, b

bb = a * b
#print*,bb
r = int(bb)

print ' (i0)',r

end program coin


program abc169c
   implicit none
   integer(8) :: a
   real(8) :: b
   character(21) :: str

   read(*,*) a, b

   write(str,'(f21.2)') dble(a)*b
   write(*,*) str(1:18)

end program abc169c


program answer
  implicit none

  integer :: A, C
  real(8) :: B
  read(*,*) A, B

  C=int(A*B)

  write(*,*) C

  stop
end program answer


program abc169c
   implicit none
   integer(8) :: a
   real(8) :: b
   character(20) :: str

   read(*,*) a, b

   write(str,'(f20.2)') dble(a)*b
   write(*,*) str(1:17)

end program abc169c


   program abc169c
      implicit none
      integer(16)::a,ans
      real(16)::b

         read *, a,b
         ans=a*b
         print *, ans

   end program abc169c


   program abc169c
      implicit none
      integer(8)::a,ans
      double precision::b

         read *, a,b
         ans=a*b
         print *, ans

   end program abc169c


program b169c

implicit none
integer(16) :: a, c
real :: b

read *, a, b

c = a * b

print *, c

end program b169c


program ABC169_C
  integer(8)::A,C
  double precision::B
  read(*,*)A,B
  B=int(B*100+0.1)
  C=A*B/100
  write(*,*)C
end program ABC169_C


program main
integer(8) :: a
real(8) :: b
read(*,*)a,b
write(*,*)int(a*b,8)
end program


program ABC169_C
  integer(8)::A,C
  double precision::B
  read(*,*)A,B
  B=int(B*100)
  C=A*B/100
  write(*,*)C
end program ABC169_C


      PROGRAM piyo
      IMPLICIT NONE
      INTEGER,PARAMETER :: prec = 16
      real(prec) :: a,b
      integer(prec) :: aa,bb

      read*,aa,b

    !  aa = int( a,16 )
      bb = int( b*100_prec )
   !   print*,aa,bb
      print*,aa*bb/100_prec

      stop
      !debugg

      END PROGRAM


program c
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: a,bb
    real(real64):: b

    read*, a,b
    bb = int(b*100, kind=int32)
    print*, (a*bb)/100

end program c


program c
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: a,bb
    real(8):: b

    read*, a,b
    bb = int(b*100, kind=int32)
    print*, a*bb/100

end program c


program ABC169_C
  integer(8)::A,C
  double precision::B
  read(*,*)A,B
  C=dint(A*B)
  write(*,*)C
end program ABC169_C


program prob3
	implicit none
    integer(16) :: A
    real(8) :: B
	integer(16) :: ans
    read(*,*) A, B
    ans = A*B

    write(*,*) ans
	stop
end program prob3


program ABC167D
    implicit none
    integer(16)::N,K,i
    integer(16),allocatable,dimension(:)::A,B
    read*,N,K
    allocate(A(N))
    allocate(B(N))
    read*,A
    B(1)=1
    i=2
    do
        if(A(B(i-1))==1)exit
        B(i)=A(B(i-1))
        i=i+1
    end do
    i=i-1
    print'(i0)',B(mod(K,i)+1)

end program ABC167D

Score: 400 points

Problem Statement

The Kingdom of Takahashi has N towns, numbered 1 through N.

There is one teleporter in each town. The teleporter in Town i (1 \leq i \leq N) sends you to Town A_i.

Takahashi, the king, loves the positive integer K. The selfish king wonders what town he will be in if he starts at Town 1 and uses a teleporter exactly K times from there.

Help the king by writing a program that answers this question.

Constraints

2 \leq N \leq 2 \times 10^5

1 \leq A_i \leq N

1 \leq K \leq 10^{18}

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \dots A_N

Output

Print the integer representing the town the king will be in if he starts at Town 1 and uses a teleporter exactly K times from there.

Sample Input 1

4 5
3 2 4 1

Sample Output 1

4

If we start at Town 1 and use the teleporter 5 times, our travel will be as follows: 1 \to 3 \to 4 \to 1 \to 3 \to 4.

Sample Input 2

6 727202214173249351
6 5 2 5 3 2

Sample Output 2

2


   program abc167d
      implicit none
      integer(8)::n
      integer(8),allocatable,dimension(:)::a,s,flag,flag_turn
      integer(8)::k
      integer(8)::i
      integer(8)::num_to_loop,num_loop

         read *, n,k
         allocate(a(n),s(0:n),flag(n),flag_turn(n))
         read *, a(1:n)

         s(0)=1; flag=0; flag(1)=1; flag_turn=0; flag_turn(1)=0
         do i=1,n
            s(i)=a(s(i-1))
            if (flag(s(i))/=1) then  ! if not visited
               flag(s(i))=1          ! marking 'visited'
               flag_turn(s(i))=i
            else
               num_to_loop=i
               num_loop=i-flag_turn(s(i))
               exit
            end if
         end do

         if (num_to_loop>=k) then
            print *, s(k)
         else
            print *, s( mod( k - num_to_loop, num_loop ) + &
                     ( num_to_loop - num_loop) )
         end if

   end program abc167d


program free
	implicit none
	integer(8):: n,k,ans,loop,machi=1,count=0,i
	integer(8),allocatable :: a(:),trace(:)
	read (*,*) n,k
	allocate(a(n))
	allocate(trace(n+1))
	do i=1,n+1
    	trace(i)=0
    end do
	read (*,*) a
	do i=1,k
		count=count+1
		if(trace(machi)/=0) exit
		trace(machi)=i
		machi=a(machi)
	end do
	if(count==2) then
		write (*,'(i0)') 1
	else if(count==k) then
		write (*,'(i0)') machi
	else
		loop=count-trace(machi)
		ans=mod(k-trace(machi)+1,loop)
		do i=1,ans
			machi=a(machi)
		end do
		write (*,'(i0)') machi
	end if
	end program free


program free
	implicit none
	integer(8):: n,k,ans,loop,machi=1,count=0,i
	integer(8),allocatable :: a(:),trace(:)
	read (*,*) n,k
	allocate(a(n))
	allocate(trace(n+1))
	do i=1,n+1
    	trace(i)=0
    end do
	read (*,*) a
	do i=1,k
		count=count+1
		if(trace(machi)/=0) exit
		trace(machi)=i
		machi=a(machi)
	end do
	if(count==2) then
		write (*,'(i0)') 1
	else
		loop=count-trace(machi)
		ans=mod(k-trace(machi)+1,loop)
		do i=1,ans
			machi=a(machi)
		end do
		write (*,'(i0)') machi
	end if
	end program free


program free
	implicit none
	integer :: i, count=0
	integer(8):: n,k,ans,loop,machi=1
	integer(8),allocatable :: a(:),trace(:)
	read (*,*) n,k
	allocate(a(n))
	allocate(trace(n+1))
	do i=1,n+1
    	trace(i)=0
    end do
	read (*,*) a
	do i=1,n
		count=count+1
		if(trace(machi)/=0) exit
		trace(machi)=i
		machi=a(machi)
	end do
	if(count==2) then
		write (*,'(i0)') 1
	else
		loop=count-trace(machi)
		ans=mod(k-trace(machi)+1,loop)
		do i=1,ans
			machi=a(machi)
		end do
		write (*,'(i0)') machi
	end if
	end program free


program main

  implicit none
  integer(16) :: n, k, i, now, cyc,tmp, num
  integer(16),allocatable :: a(:), b(:), bo(:), cnt(:)

  read(*,*) n,k
  allocate(a(n))
  allocate(b(n))
  allocate(bo(n))
  allocate(cnt(n))
  read(*,*) a

  ! initialize
  b  = 0
  bo = 0
  cyc = 0
  cnt = 0
  num = 0

  now = 1
  b(now) = 0
  bo(now) = 0
  cnt(now) = 1

  if( k > n ) then
    do i = 1, k+1
      cnt(a(now)) = cnt(a(now)) + 1
      if( cnt(a(now)) == 2 )then
        cyc = i - bo(a(now))
        num = 1
        exit
      end if
      now = a(now)
      b(now) = i
      bo(now) = b(now)
    end do
    !print*, cyc, now, b(now), bo(a(now)), bo(now), k

    if( num == 1 ) then
      !print*, mod(k-b(now),cyc)
      do i = 1, mod(k-b(now),cyc)
        now = a(now)
      end do
    end if
  else
    now = 1
    do i = 1, k
      now = a(now)
    end do
  end if

  print*, now

end program main


program main

  implicit none
  integer(16) :: n, k, i, now, cyc,tmp, num
  integer(16),allocatable :: a(:), b(:), bo(:), cnt(:)

  read(*,*) n,k
  allocate(a(n))
  allocate(b(n))
  allocate(bo(n))
  allocate(cnt(n))
  read(*,*) a

  ! initialize
  b  = 0
  bo = 0
  cyc = 0
  cnt = 0
  num = 0

  now = 1
  b(now) = 0
  bo(now) = 0
  cnt(now) = 1

  if( k > n ) then
    do i = 1, k+1
      cnt(a(now)) = cnt(a(now)) + 1
      if( cnt(a(now)) == 2 )then
        cyc = i - bo(a(now))
        num = 1
        exit
      end if
      now = a(now)
      b(now) = i
      bo(now) = b(now)
    end do
    !print*, cyc, now, b(now), bo(a(now)), bo(now), k

    if( num == 1 ) then
      !print*, mod(k-b(now),cyc)
      do i = 1, mod(k-b(now),cyc)
        now = a(now)
      end do
    end if
  else
    now = 1
    do i = 1, n
      now = a(now)
    end do
  end if

  print*, now

end program main


program main
  implicit none
  integer(8) :: N,K
  integer(8),allocatable :: A(:)
  integer(8),allocatable :: num(:)
  logical,allocatable :: flag(:)
  integer(8) :: loopnum
  integer(8) :: now, loop_start, now_save
  integer(8) :: step
  integer(8) :: i,j

  read(*,*) N,K
  allocate(A(N))
  read(*,*) A(:)
  allocate(flag(N))
  allocate(num(N))
  flag(:) = .false.
  flag(1) = .true.
  num(1) = 0
  now = 1
  now = A(now)
  do i = 1,N+1
     if ( flag(now) ) then
        exit
     else
        flag(now) = .true.
        num(now) = i
        now = A(now)
     end if
  end do
  ! loop num
  loopnum = i - num(now)
  now_save = now
  now = 1
  do i = 1,N
     now = A(now)
     if (now == now_save ) exit
  end do
  if (  K  <= (loopnum + i) ) then
     now = 1
     do i = 1,K
        now = A(now)
     end do
     write(*,'(i0)') now
     stop
  end if
  !write(*,*) loopnum
  step = mod( K-num(now), loopnum )
  !write(*,*) now,step
  now = now_save
  do i = 1,step
     !num(int(now)) = i
     now = A(now)
  end do
  write(*,'(i0)') now
end program main


program main

  implicit none
  integer(16) :: n,k ,i, ans, now
  integer(16),allocatable :: a(:)

  read(*,*) n,k
  allocate(a(n))
  read(*,*) a

  if( k > n) then
    now = 1
    do i = 1, n
      ans = a(now)
      now = ans
    end do
    if( mod(k,n) > 0) then
      do i = 1, mod(k,n)
        ans = a(now)
        now = ans
      end do
    else
    end if
  else
  now = 1
  do i = 1, k
    ans = a(now)
    now = ans
  end do
  end if

  print*, ans

end program main


program ABC167_D
  integer(8)::N,K,X,Y,Z
  integer(8),allocatable::A(:),S(:),T(:)
  read(*,*)N,K
  allocate(A(N),S(N),T(N))
  read(*,*)A
  M=1
  L=1
  S=0
  T=0
  T(1)=1
  S(1)=1
  do i=1,N
     M=A(M)
     S(M)=S(M)+1
     if(S(M)==2) then
        do j=1,N
           if(L==M) then
              X=i-j+1
              Y=i
              Z=j
              go to 10
           end if
           L=A(L)
        end do
     end if
  end do
10 if(K>N) then
      K=mod(K-Z+1,X)+Z-1
      end if

  M=1
  do i=1,K
     M=A(M)
  end do
  write(*,*)M
end program ABC167_D