program panasonic2020C
    implicit none
    integer(8)::a,b,c
    read*,a,b,c
    if(c-a-b>=0.and.4*a*b<(c-a-b)**2)then
        print'(A)',"Yes"
    else
        print'(A)',"No"
    end if
end program panasonic2020C

Score : 300 points

Problem Statement

Does \sqrt{a} + \sqrt{b} < \sqrt{c} hold?

Constraints

1 \leq a, b, c \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

a \ b \ c

Output

If \sqrt{a} + \sqrt{b} < \sqrt{c}, print Yes; otherwise, print No.

Sample Input 1

2 3 9

Sample Output 1

No

\sqrt{2} + \sqrt{3} < \sqrt{9} does not hold.

Sample Input 2

2 3 10

Sample Output 2

Yes

\sqrt{2} + \sqrt{3} < \sqrt{10} holds.


program name
    use,intrinsic :: iso_fortran_env
    implicit none
    real(real64):: a,b,c

    read*, a,b,c

    if (sqrt(a)+sqrt(b) < sqrt(c)) then
        print'(a)', 'Yes'
    else
        print'(a)', 'No'
    end if
end program name

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

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 PanasonicC
   Implicit none
   Integer(8)::a,b,c

      Read(*,*) a,b,c
      If (4*a*b<(c-a-b)**2.AND.c-a-b>0) then
         Write(*,*) 'Yes'
      Else
         Write(*,*) 'No'
      End If
      Stop

   End Program PanasonicC


   Program PanasonicC
   Implicit none
   Real(8)::a,b,c

      Read(*,*) a,b,c
      If (SQRT(a)+SQRT(b)<SQRT(c)) then
         Write(*,*) 'Yes'
      Else
         Write(*,*) 'No'
      End If
      Stop

   End Program PanasonicC


program main
    integer(16) :: a,b,c,d
    read*,a,b,c
    d = c-a-b
    if (d > 0 .and. d**2 > 4*a*b ) then
        print *, 'Yes'
    else
        print *, 'No'
    end if
end program main


program main
 implicit none
 double precision a,b,c,d

 read *, a,b,c

 a=sqrt(a)
 b=sqrt(b)
 c=sqrt(c)
 d=a+b

 if (d<c)  print '(A)','Yes'
 if (d>=c) print '(A)','No'

 print *,'a=',a
 print *,'b=',b
 print *,'c=',c

endprogram main


program main
  implicit none
  integer*8 :: a, b, c
  read *, a, b, c
  if (c-a-b>0 .and. 4*a*b<(c-a-b)**2) then
    print '(a)', 'Yes'
  else
    print '(a)', 'No'
  end if
end program


program sqrt
    implicit none
    real(kind=16) :: i, j, k
    real(kind=16) :: a, b, c
    read *, i, j, k

    i = real(i**1.00q0,kind=16)
    j = real(j**1.00q0,kind=16)
    k = real(k**1.00q0,kind=16)
    a = real(i**0.50q0,kind=16)
    b = real(j**0.50q0,kind=16)
    c = real(k**0.50q0,kind=16)

    if ((a+b)/c < 1.0q0) then
        print *, "Yes"
    else if ((a+b)/c >= 1.0q0) then
        print *, "No"
    end if

end program sqrt


program sqrt
    implicit none
    integer :: i, j, k
    real(kind=16) :: a, b, c
    read *, i, j, k

    i = real(i**1.00q0,kind=16)
    j = real(j**1.00q0,kind=16)
    k = real(k**1.00q0,kind=16)
    a = real(i**0.50q0,kind=16)
    b = real(j**0.50q0,kind=16)
    c = real(k**0.50q0,kind=16)

    if (a+b < c) then
        print *, "Yes"
    else if (a+b >= c) then
        print *, "No"
    end if

end program sqrt


program sqrt
    implicit none
    integer :: i, j, k
    real(kind=16) :: a, b, c
    read *, i, j, k

    a = real(i**0.50q0,kind=16)
    b = real(j**0.50q0,kind=16)
    c = real(k**0.50q0,kind=16)

    if (a+b < c) then
        print *, "Yes"
    else if (a+b >= c) then
        print *, "No"
    end if

end program sqrt


program main
 implicit none
 real(8) a,b,c,d

 read *, a,b,c

 a=sqrt(a)
 b=sqrt(b)
 c=sqrt(c)
 d=a+b

 if (d<c)  print '(A)','Yes'
 if (d>=c) print '(A)','No'

endprogram main


program abc
	implicit none
    integer(16) :: a,b,c,x,y
    double precision :: d,e,f,g

	read(*,*) a,b,c

	d = sqrt(dble(a))
	e = sqrt(dble(b))
	f = sqrt(dble(c))
    g = d + e

    x = 4 * a * b
    y = (c - a - b) ** 2

	if(x == y) then
    	g = 1
        f = 1
    endif

    if(g < f) then
    	write(*,*) "Yes"
    else
    	write(*,*) "No"
    endif

end program abc


program main_c
   implicit none
   integer(8) :: p, q, r
   real(8) :: result, a, b, c

   read(*,*) p, q, r
   a = dble(p)
   b = dble(q)
   c = dble(r)

   ! result = dble(p+q-r)  + 2d0*sqrt(a*b)
   ! result = (sqrt(a)+sqrt(b)) /sqrt(c)
   result = (a+b+2d0*sqrt(a*b)) /c

   if (result < 1d0) then
      print *, 'Yes'
   else
      print *, 'No'
   end if
end program main_c


program sqrt
    implicit none
    integer :: i, j, k

    read *, i, j, k

    i = real(i,kind=16)
    j = real(j,kind=16)
    k = real(k,kind=16)

    if ((real(i**0.50q0,kind=16) + real(j**0.50q0,kind=16)) < real(k**0.50q0,kind=16)) then
        print *, "Yes"
    else if ((real(i**0.50q0,kind=16) + real(j**0.50q0,kind=16)) > real(k**0.50q0,kind=16)) then
        print *, "No"
    else if ((real(i**0.50q0,kind=16) + real(j**0.50q0,kind=16)) == real(k**0.50q0,kind=16)) then

    end if

end program sqrt


program main
 implicit none
 real a,b,c

 read *, a,b,c

 a=sqrt(a)
 b=sqrt(b)
 c=sqrt(c)

 if (a+b<c)  print '(A)','Yes'
 if (a+b>=c) print '(A)','No'

endprogram main


program sqrt
    implicit none
    integer :: i, j, k

    read *, i, j, k

    i = dble(i)
    j = dble(j)
    k = dble(k)

    if (dble(dble(i**0.50d0) + dble(j**0.50d0)) < dble(k**0.50d0)) then
        print *, "Yes"
    else if (dble(dble(i**0.50d0) + dble(j**0.50d0)) > dble(k**0.50d0)) then
        print *, "No"
    else if (dble(dble(i**0.50d0) + dble(j**0.50d0)) == dble(k**0.50d0)) then

    end if

end program sqrt


program main

  implicit none

  integer(8) :: a, b, c, i,j,k

  read(*,*) a, b, c

  i = c**2
  j = -2*(a+b)*c
  k = (a-b)**2
  if ( i+j+k>0 .and. (c-(a+b)) > 0  ) then
    print*, "Yes"
  else
    print*, "No"
  end if

end program main


program sqrt
    implicit none
    integer :: i, j, k

    read *, i, j, k
!    print *, dble(i**0.50d0)
!    print *, dble(j**0.50d0)
!    print *, dble(k**0.50d0)

    if (dble(dble(i**0.50d0) + dble(j**0.50d0)) < dble(k**0.50d0)) then
        print *, "Yes"
    else if (dble(dble(i**0.50d0) + dble(j**0.50d0)) > dble(k**0.50d0)) then
        print *, "No"
    else if (dble(dble(i**0.50d0) + dble(j**0.50d0)) == dble(k**0.50d0)) then

    end if

end program sqrt


program example
	implicit none

	integer(8) a,b,c

	read(*,*) a,b,c

    if(dble(c)>dble(a)+dble(b)+2*sqrt(dble(a)*dble(b))) then
    	write(*,*) "Yes"
        goto 1
    else
    	write(*,*) "No"
        goto 1

    end if

1 end program example


program main

  implicit none

  integer(8) :: a, b, c
  real(8) :: ar, br, cr,i, j ,k

  read(*,*) a, b, c
  ar = real(a,kind=8)
  br = real(b,kind=8)
  cr = real(c,kind=8)

  i = cr
  j = -(ar+br)
  k = -sqrt( ar*br )*2e0
  if ( (i+j+k)>0 ) then
    print*, "Yes"
  else
    print*, "No"
  end if

end program main


program sqrt
    implicit none
    integer :: i, j, k

    read *, i, j, k
    print *, dble(i**0.50d0)
    print *, dble(j**0.50d0)
    print *, dble(k**0.50d0)

    if ((dble(i**0.50d0) + dble(j**0.50d0)) < dble(k**0.50d0)) then
        print *, "Yes"
    else if ((dble(i**0.50d0) + dble(j**0.50d0)) > dble(k**0.50d0)) then
        print *, "No"
    else if ((dble(i**0.50d0) + dble(j**0.50d0)) == dble(k**0.50d0)) then

    end if

end program sqrt


program abc
	implicit none
    integer(16) :: a,b,c
    double precision :: d,e,f,g

	read(*,*) a,b,c

	d = sqrt(dble(a))
	e = sqrt(dble(b))
	f = sqrt(dble(c))
    g = d + e

    if(g < f) then
    	write(*,*) "Yes"
    else
    	write(*,*) "No"
    endif

end program abc


program sqrt
    implicit none
    integer :: i, j, k

    read *, i, j, k
!    print *, dble(i**0.5)
!    print *, dble(j**0.5)
!    print *, dble(k**0.5)

    if ((dble(i**0.5) + dble(j**0.5)) < dble(k**0.5)) then
        print *, "Yes"
    else if ((dble(i**0.5) + dble(j**0.5)) > dble(k**0.5)) then
        print *, "No"
    else if ((dble(i**0.5) + dble(j**0.5)) == dble(k**0.5)) then

    end if

end program sqrt


program main

  implicit none

  integer(8) :: a, b, c
  real(8) :: ar, br, cr,i, j ,k

  read(*,*) a, b, c
  ar = real(a,kind=8)
  br = real(b,kind=8)
  cr = real(c,kind=8)
  !if ( (sqrt( real(a) ) + sqrt( real(b) )) < sqrt( real(c) ) ) then
  !  print*, "Yes"
  !else
  !  print*, "No"
  !end if
  i = cr**2
  j = -2*(ar+br)*cr
  k = (ar-br)**2
  if ( (i+j+k)>0 ) then
    print*, "Yes"
  else
    print*, "No"
  end if

end program main


program example
	implicit none

	integer(8) a,b,c

	read(*,*) a,b,c

    if(sqrt(dble(c))>sqrt(dble(a))+sqrt(dble(b))) then
    	write(*,*) "Yes"
    else
    	write(*,*) "No"

    end if

end program


program sqrt
    implicit none
    integer :: i, j, k

    read *, i, j, k

    if ((real(i**0.5) + real(j**0.5)) < real(k**0.5)) then
        print *, "Yes"
    else if ((real(i**0.5) + real(j**0.5)) > real(k**0.5)) then
        print *, "No"
    else if ((real(i**0.5) + real(j**0.5)) == real(k**0.5)) then

    end if

end program sqrt


program main
  implicit none
  real*8 :: a, b, c
  read *, a, b, c
  if (a+b+2*sqrt(a*b)<c) then
    print '(a)', 'Yes'
  else
    print '(a)', 'No'
  end if
end program


program main

  implicit none

  integer(8) :: a, b, c, i,j,k

  read(*,*) a, b, c

  !if ( (sqrt( real(a) ) + sqrt( real(b) )) < sqrt( real(c) ) ) then
  !  print*, "Yes"
  !else
  !  print*, "No"
  !end if
  i = c**2
  j = -2*(a+b)*c
  k = (a-b)**2
  if ( i+j+k>0 ) then
    print*, "Yes"
  else
    print*, "No"
  end if

end program main


program main
  implicit none
  real :: a, b, c
  read *, a, b, c
  if (sqrt(a)+sqrt(b)<sqrt(c)) then
    print '(a)', 'Yes'
  else
    print '(a)', 'No'
  end if
end program


program main
  integer(8) :: a,b,c

  read(*,*) a,b,c

!  if ( sqrt(real(a,8)) + sqrt(real(b,8)) - sqrt(real(c,8)) < 0.e-10 ) then
  if ( 2.d0*sqrt(real(a,8))*sqrt(real(b,8)) < c - (a + b)) then
     write(*,'(a)') 'Yes'
  else
     write(*,'(a)') 'No'
  end if

end program main


program main_c
   implicit none
   integer(8) :: p, q, r
   real(8) :: result, a, b, c

   read(*,*) p, q, r
   a = dble(p)
   b = dble(q)
   c = dble(r)

   result = dble(p+q-r)  + 2d0*sqrt(a*b)

   if (result < 0) then
      print *, 'Yes'
   else
      print *, 'No'
   end if
end program main_c


program sqrt
    implicit none
    integer :: i, j, k

    read *, i, j, k

    if ((i**0.5 + j**0.5) < k**0.5) then
        print *, "Yes"
    else if ((i**0.5 + j**0.5) > k**0.5) then
        print *, "No"
    end if

end program sqrt


program main_c
   implicit none
   integer :: p, q, r
   real(8) :: result, a, b, c

   read(*,*) p, q, r
   a = dble(p)
   b = dble(q)
   c = dble(r)

   result = dble(p+q-r)  + 2d0*sqrt(a*b)

   if (result < 0) then
      print *, 'Yes'
   else
      print *, 'No'
   end if
end program main_c


program sample
    implicit none

    integer(8) :: i,j,m,n,a,b,ans
    integer(8),allocatable :: x(:),y(:)

    read(*,*) n,m
    allocate(x(n))
    x(:)=-1
    if(m==0)then
        if(n==1)then
            write(*,*)0
            stop
        else
            write(*,*)10**(n-1)
            stop
        end if
    end if

    do i=1,m
        read(*,*)a,b
        if(a==1 .and. b==0 .and. n/=1)then
            write(*,*)'-1'
            stop
        else if(a>n)then
            write(*,*)'-1'
            stop
        else if(x(a)==-1)then
            x(a)=b
        else if(x(a)/=b)then
            write(*,*)'-1'
            stop
        end if
    end do
    ans=0
    do i=1,n
        if(x(i)==-1)then
            if(i==1)then
                x(i)=1
            else
                x(i)=0
            end if
        end if
        ans=ans+x(i)*10**(n-i)
    end do
    write(*,*)ans

    stop
end program sample

Score : 300 points

Problem Statement

If there is an integer not less than 0 satisfying the following conditions, print the smallest such integer; otherwise, print -1.

The integer has exactly N digits in base ten. (We assume 0 to be a 1-digit integer. For other integers, leading zeros are not allowed.)

The s_i-th digit from the left is c_i. \left(i = 1, 2, \cdots, M\right)

Constraints

All values in input are integers.

1 \leq N \leq 3

0 \leq M \leq 5

1 \leq s_i \leq N

0 \leq c_i \leq 9

Input

Input is given from Standard Input in the following format:

N M
s_1 c_1
\vdots
s_M c_M

Output

Print the answer.

Sample Input 1

3 3
1 7
3 2
1 7

Sample Output 1

702

702 satisfies the conditions - its 1-st and 3-rd digits are 7 and 2, respectively - while no non-negative integer less than 702 satisfies them.

Sample Input 2

3 2
2 1
2 3

Sample Output 2

-1

Sample Input 3

3 1
1 0

Sample Output 3

-1


program sample
    implicit none

    integer(8) :: i,j,m,n,a,b,ans
    integer(8),allocatable :: x(:),y(:)

    read(*,*) n,m
    allocate(x(n))
    x(:)=-1
    do i=1,m
        read(*,*)a,b
        if(a==1 .and. b==0 .and. n/=1)then
            write(*,*)'-1'
            stop
        else if(a>n)then
            write(*,*)'-1'
            stop
        else if(x(a)==-1)then
            x(a)=b
        else if(x(a)/=b)then
            write(*,*)'-1'
            stop
        end if
    end do
    ans=0
    do i=1,n
        if(x(i)==-1)then
            if(i==1)then
                x(i)=1
            else
                x(i)=0
            end if
        end if
        ans=ans+x(i)*10**(n-i)
    end do
    write(*,*)ans

    stop
end program sample


program sample
    implicit none

    integer(8) :: i,j,m,n,a,b,ans
    integer(8),allocatable :: x(:),y(:)

    read(*,*) n,m
    allocate(x(n))
    x(:)=-1
    do i=1,m
        read(*,*)a,b
        if(a==1 .and. b==0)then
            write(*,*)'-1'
            stop
        else if(x(a)==-1)then
            x(a)=b
        else if(x(a)/=b)then
            write(*,*)'-1'
            stop
        end if
    end do
    ans=0
    do i=1,n
        if(x(i)==-1)then
            x(i)=0
        end if
        ans=ans+x(i)*10**(n-i)
    end do
    write(*,*)ans

    stop
end program sample


program guess_the_number
  implicit none
  integer :: n, m, s, i
  character(1) :: c
  character(3) :: x = "   "
  read(*,*) n, m
  do i = 1, m
    read(*,'(i1,x,a)') s, c
    if (n > 1 .and. s == 1 .and. c == "0") then
      write(*,'(a)') "-1"
      stop
    end if
    if (x(s:s) == " " .or. x(s:s) == c) then
      x(s:s) = c
      cycle
    end if
    write(*,'(a)') "-1"
    stop
  end do
  do i = 1, n
    if (x(i:i) == " ") then
      x(i:i) = "0"
    end if
  end do
  if (n > 1 .and. x(1:1) == "0") x(1:1) = "1"
  write(*,'(a)') trim(x)
end program guess_the_number


program guess_the_number
  implicit none
  integer :: n, m, s, i
  character(1) :: c
  character(3) :: x = "   "
  read(*,*) n, m
  do i = 1, m
    read(*,'(i1,x,a)') s, c
    if (x(s:s) == " " .or. x(s:s) == c) then
      x(s:s) = c
      cycle
    end if
    write(*,'(a)') "-1"
    stop
  end do
  do i = 1, n
    if (x(i:i) == " ") then
      x(i:i) = "0"
    end if
  end do
  if (n > 1 .and. x(1:1) == "0") x(1:1) = "1"
  write(*,'(a)') trim(x)
end program guess_the_number


program guess_the_number
  implicit none
  integer :: n, m, s, i
  character(1) :: c
  character(3) :: x = "   "
  read(*,*) n, m
  do i = 1, m
    read(*,'(i1,x,a)') s, c
    if (x(s:s) == " " .or. x(s:s) == c) then
      x(s:s) = c
      cycle
    end if
    write(*,'(a)') "-1"
    stop
  end do
  do i = 1, n
    if (x(i:i) == " ") then
      x(i:i) = "0"
    end if
  end do
  if (n > 1 .and. x(1:1) == "0") then
    write(*,'(a)') "-1"
    stop
  end if
  write(*,'(a)') trim(x)
end program guess_the_number


      PROGRAM guessTheNumber
      IMPLICIT NONE
      integer :: n,num(3),m
      integer,allocatable :: s(:),c(:)
      integer :: i,ii,k

      read*,n,m
      allocate( s(m),c(m) )
      do i = 1,m
        read*,s(i),c(i)
      end do
 !     print*,n,m
 !     do i = 1,m
 !       print*,s(i),c(i)
 !     end do

      if( n==2 ) s(:) = s(:) + 1
      if( n==1 ) s(:) = s(:) + 2

      outer:do i = 0,999
        if( n==1 .and. .not.(0  <=i .and. i<=9  ) ) cycle
        if( n==2 .and. .not.(10 <=i .and. i<=99 ) ) cycle
        if( n==3 .and. .not.(100<=i .and. i<=999) ) cycle
        ii = i
        do k = 1,3
          num(k) = ii - ii/10*10
          ii = ii / 10
        end do
 ! print*,num

      do k = 1,m
        if( num(3-s(k)+1)/=c(k) ) cycle outer
      end do
      print*,i;stop

      end do outer

      print*,'-1'

      END PROGRAM


      PROGRAM guessTheNumber
      IMPLICIT NONE
      integer :: n,num(3),m
      integer,allocatable :: s(:),c(:)
      integer :: i,ii,k

      read*,n,m
      allocate( s(m),c(m) )
      do i = 1,m
        read*,s(i),c(i)
      end do
 !     print*,n,m
 !     do i = 1,m
 !       print*,s(i),c(i)
 !     end do

      if( n==2 ) s(:) = s(:) - 1
      if( n==1 ) s(:) = s(:) - 2

      outer:do i = 0,999
        if( n==1 .and. .not.(0  <=i .and. i<=9  ) ) cycle
        if( n==2 .and. .not.(10 <=i .and. i<=99 ) ) cycle
        if( n==3 .and. .not.(100<=i .and. i<=999) ) cycle
        ii = i
        do k = 1,3
          num(k) = ii - ii/10*10
          ii = ii / 10
        end do
 ! print*,num

      do k = 1,m
        if( num(3-s(k)+1)/=c(k) ) cycle outer
      end do
      print*,i;stop

      end do outer

      print*,'-1'

      END PROGRAM


      PROGRAM guessTheNumber
      IMPLICIT NONE
      integer :: n,num(3),m
      integer,allocatable :: s(:),c(:)
      integer :: i,ii,k

      read*,n,m
      allocate( s(m),c(m) )
      do i = 1,m
        read*,s(i),c(i)
      end do
 !     print*,n,m
 !     do i = 1,m
 !       print*,s(i),c(i)
 !     end do

      outer:do i = 0,999
        if( n==1 .and. i==10  ) exit
        if( n==2 .and. i==100 ) exit
        ii = i
        do k = 1,3
          num(k) = ii - ii/10*10
          ii = ii / 10
        end do

        if( n==2 .and. num(2)==0 ) cycle
        if( n==3 .and. num(3)==0 ) cycle
 ! print*,num

      do k = 1,m
        if( num(3-s(k)+1)/=c(k) ) cycle outer
      end do
      print*,i;stop

      end do outer

      print*,'-1'

      END PROGRAM


program ABC157_C
  integer::X,Y,Z,A
  integer,dimension(:),allocatable::S,C
  read(5,*)N,M
  allocate(S(M),C(M))
  X=0
  Y=0
  Z=0
  if(N==2) Y=1
  if(N==3) X=1
  K=0
  do I=1,M
     read(5,*)S(I),C(I)
  end do
  if(M>1) then
     do I=1,M-1
        if(N>1 .and. (S(I)==1 .and. C(I)==0) ) then
           K=1
           go to 10
        end if
        do J=2,M
           if(S(I)==S(J) .and. C(I)/=C(J)) then
              K=1
              go to 10
           end if
        end do
     end do
  end if
  do I=1,M
     if(N==3 .and. S(I)==1) then
        X=C(I)
     else if( (N==3 .and. S(I)==2) .or. (N==2 .and. S(I)==1)) then
        Y=C(I)
     else
        Z=C(I)
     end if
  end do
  A=100*X+10*Y+Z
  if(N==3 .and. A<100) K=1
  if(N==2 .and. A<10) K=1
10 if(K==1) then
     write(6,*)'-1'
  else
     write(6,*)A
  end if
end program ABC157_C


program main
  implicit none
  integer::N,M
  integer::ANS(3)
  integer::s,c
  integer::i
  read*,N,M
  ANS=-1
  do i=1,M
    read*,s,c
    if(ANS(s)>=0.and.ANS(s)/=c)then
      print"(i0)",-1
      stop
    endif
    if(s==1.and.c==0)then
      print"(i0)",-1
      stop
    endif
    ANS(s)=c
  end do
  do i=1,N
    ANS(i)=max(ANS(i),merge(1,0,i==1))
  end do
  do i=1,N
    write(*,"(i0)",advance="no") ANS(I)
  end do
  write(*,*)
end program main


program name
    implicit none
    integer(4):: n,m
    integer(4):: s,c
    integer(4),allocatable:: keta(:)
    integer(4):: i,ans

    read*, n,m
    allocate(keta(n))

    keta(:) = -1
    do i=1,m
        read*, s,c

        if (keta(s) /= -1 .and. keta(s) /= c) call die
        keta(s) = c
    end do

    if (keta(1) == 0 .and. n > 1) call die
    if (keta(1) == -1 .and. n > 1) keta(1) = 1

    do i=1,n
        if (keta(i) == -1) keta(i) = 0
    end do

    ans=0
    do i=1,n
        ans=ans+keta(i)*10**(n-i)
    end do

    print*, ans

contains
subroutine die
    print*, -1
    stop
end subroutine

end program name


program name
    implicit none
    integer(4):: n,m
    integer(4):: s,c
    integer(4),allocatable:: digit(:)
    integer(4):: i,ans

    read*, n,m
    allocate(digit(n))
    digit(:) = -1
    do i=1,m
        read*, s,c
        if (s==1 .and. c==0) call die
        if ((digit(s) /= -1 .and. digit(s) /= c)) call die
        digit(s) = c
    end do
    if (digit(1) == 0) call die
    if (digit(1) == -1 .and. n>1) digit(1) = 1

    do i=1,n
        if (digit(i) == -1) digit(i) = 0
    end do

    ans=0
    do i=1,n
        ans = ans+ digit(i)*10**(n-i)
    end do
    print*, ans

contains
subroutine die
    print*, -1
    stop
end subroutine

end program name


program name
    implicit none
    integer(4):: n,m
    integer(4):: s,c
    integer(4),allocatable:: digit(:)
    integer(4):: i,ans

    read*, n,m
    allocate(digit(n))
    digit(:) = -1
    do i=1,m
        read*, s,c
        if ((digit(s) /= -1 .and. digit(s) /= c)) call die
        digit(s) = c
    end do
    if (digit(1) == 0) call die
    if (digit(1) == -1) digit(1) = 1

    do i=2,n
        if (digit(i) == -1) digit(i) = 0
    end do

    ans=0
    do i=1,n
        ans = ans+ digit(i)*10**(n-i)
    end do
    print*, ans

contains
subroutine die
    print*, -1
    stop
end subroutine

end program name


   program abc157c
      implicit none
      integer::N,M,i,j
      integer,parameter::k=10
      integer,allocatable,dimension(:)::A
      integer,allocatable,dimension(:)::S,C

      read *,N,M
      allocate(A(N))
      A=k
      allocate(S(M),C(M))

      do i=1,M
         read *,S(i),C(i)
         if (S(i)==1.and.C(i)==0) then
            print *,'-1'
            stop
         end if
      end do

      do i=1,M
         do j=i+1,M
            if (S(i)==S(j).and.C(i)/=C(j)) then
               print *,'-1'
               stop
            end if
         end do
      end do

      do i=1,N
         do j=1,M
            A(S(j))=C(j)
         end do
         if (i==1.and.A(i)==k) A(i)=1
         if (i>1.and.A(i)==k) A(i)=0
         write(*,'(I1)',advance='no') A(i)
      end do

      stop

   end program abc157c


program Guess_The_Number
    implicit none
    integer(4):: n, m
    integer(4):: i,j,k
    integer(4), allocatable:: number(:), s(:), c(:)

    read*, n, m
    allocate(number(n), s(m), c(m))

    do i=1, m
        read*, s(i), c(i)
    end do

    number(:) = 0

    do i=1, m
        if(number(s(i)) /= 0 .and. number(s(i)) /= c(i)) call die()
        if(s(i) == 1 .and. c(i) == 0 .and. n > 1) call die()
        number(s(i)) = c(i)
    end do

    if (number(1) == 0 .and. n > 1) number(1) = 1

    block
        character(1):: c_num(n)

        do i=1, n
            write(c_num(i), '(i0)') number(i)
        end do

        print*, c_num(:)
    end block

contains
subroutine die()
    print*, -1
    stop
end subroutine
end program Guess_The_Number


program Guess_The_Number
    implicit none
    integer(4):: n, m
    integer(4):: i,j,k
    integer(4), allocatable:: number(:), s(:), c(:)
    character(1), allocatable:: c_num(:)
    character(1):: c_

    read*, n, m
    allocate(number(n), s(m), c(m), c_num(n))
    number(:) = -1

    do i=1, m
        read*, s(i), c(i)
    end do

    do i=1, m
        if (number(s(i)) /= -1 .and. number(s(i)) /= c(i)) then
            print*, -1
            stop
        end if

        number(s(i)) = c(i)
    end do

    if (number(1) == 0) then
        print*, -1
        stop
    end if

    if (number(1) == -1)  number(1) = 1

    do i=1, n
        if(number(i) == -1) number(i) = 0
        write(c_,'(i0)') number(i)
        c_num(i) = c_
    end do

    print*, c_num(:)

end program Guess_The_Number


program Guess_The_Number
    implicit none
    integer(4):: n, m
    integer(4):: i,j,k
    integer(4), allocatable:: number(:), s(:), c(:)

    read*, n, m
    allocate(number(n), s(m), c(m))

    number(:) = -1

    do i=1, m
        read*, s(i), c(i)
    end do

    do i=1, m
        if (number(s(i)) /= -1 .and. number(s(i)) /= c(i)) then
            print*, -1
            stop
        end if

        number(s(i)) = c(i)
    end do

    if (number(1) == 0) then
        print*, -1
        stop
    end if

    if (number(1) == -1)  number(1) = 1

    do i=1, n
        if(number(i) == -1) number(i) = 0
    end do

    print*, number(:)

end program Guess_The_Number


program main
  implicit none
  integer :: n, i, j, m, res
  integer, allocatable :: s(:)
  character(4) :: si
  character(1), allocatable :: c(:)
  read (*, *) n, m
  allocate (s(m), c(m))
  do i = 1, m
    read (*, *) s(i), c(i)
  end do

  res = -1
  iloop: do i = 0, 999
    write (si, "(i0)") i
    if (len(trim(si)) /= n) cycle iloop
    do j = 1, m
      if (si(s(j):s(j)) /= c(j)) cycle iloop
    end do
    res = i
    exit iloop
  end do iloop
  write (*, "(i0)") res
end program main


program main

  implicit none

  integer    :: n, m
  integer,allocatable :: s(:), c(:)

  integer,allocatable :: o(:,:)
  integer    :: i, j, k, ans

  read(*,*) n, m
  allocate( s(m) )
  allocate( c(m) )
  do i = 1, m
    read(*,*) s(i), c(i)
  end do
  allocate( o(n,2) )

  ans = 0
  o   = 0
  do i = 1, m
    o(s(i),1) = o(s(i),1) + 1
    if( o(s(i),1) >= 1 .and. o(s(i),2) <= c(i) ) cycle
    o(s(i),2) = c(i)
  end do

  do i = 1, n
    if( o(i,1) >= 1 ) cycle
    if( i == 1 ) then
      o(i,2) = 1
    else
      o(i,2) = 0
    end if
    o(i,1) = 1
  end do

  do i = 1, n
    ans = ans + o(i,2) * int( 10**( n - i ) )
  end do

  if ( o(1,2) == 0 .or. maxval( o(:,1) ) > 1 ) ans = -1

  print*, ans

end program main


program main
  implicit none
  integer :: n, i, j, m, res
  integer, allocatable :: s(:)
  character(4) :: si
  character(1), allocatable :: c(:)
  read (*, *) n, m
  allocate (s(m), c(m))
  do i = 1, m
    read (*, *) s(i), c(i)
  end do

  res = -1
  iloop: do i = 1, 999
    write (si, "(i0)") i
    if (len(trim(si)) == n) then
      do j = 1, m
        if (si(s(j):s(j)) /= c(j)) cycle iloop
      end do
      res = i
      exit
    end if
  end do iloop
  write (*, "(i0)") res
end program main


program main

  implicit none

  integer    :: n, m
  integer,allocatable :: s(:), c(:)

  integer,allocatable :: o(:,:)
  integer    :: i, j, k, ans

  read(*,*) n, m
  allocate( s(m) )
  allocate( c(m) )
  do i = 1, m
    read(*,*) s(i), c(i)
  end do
  allocate( o(n,2) )

  ans = 0
  o   = 0
  do i = 1, m
    if( o(s(i),1) >= 1 .and. o(s(i),2) <= c(i) ) cycle
    o(s(i),1) = o(s(i),1) + 1
    o(s(i),2) = c(i)
  end do

  do i = 1, n
    if( o(i,1) >= 1 ) cycle
    if( i == 1 ) then
      o(i,2) = 1
    else
      o(i,2) = 0
    end if
    o(i,1) = 1
  end do

  do i = 1, n
    ans = ans + o(i,2) * int( 10**( n - i ) )
  end do

  if ( o(1,2) == 0 .and. ans > 9 ) ans = -1

  print*, ans

end program main


program main

  implicit none

  integer    :: n, m
  integer,allocatable :: s(:), c(:)

  integer,allocatable :: o(:,:)
  integer    :: i, j, k, ans

  read(*,*) n, m
  allocate( s(m) )
  allocate( c(m) )
  do i = 1, m
    read(*,*) s(i), c(i)
  end do
  allocate( o(n,2) )

  ans = 0
  o   = 0
  do i = 1, m
    if( o(s(i),1) >= 1 .and. o(s(i),2) <= c(i) ) cycle
    o(s(i),1) = o(s(i),1) + 1
    o(s(i),2) = c(i)
  end do

  do i = 1, n
    if( o(i,1) >= 1 ) cycle
    o(i,1) = 1
    o(i,2) = 0
  end do

  do i = 1, n
    ans = ans + o(i,2) * int( 10**( n - i ) )
  end do

  if ( o(1,2) == 0 .and. ans > 9 ) ans = -1

  print*, ans

end program main


program main

  implicit none

  integer    :: n, m
  integer,allocatable :: s(:), c(:)

  integer,allocatable :: o(:,:)
  integer    :: i, j, k, ans

  read(*,*) n, m
  allocate( s(m) )
  allocate( c(m) )
  do i = 1, m
    read(*,*) s(i), c(i)
  end do
  allocate( o(n,2) )

  ans = 0
  o   = 0
  do i = 1, m
    if( o(s(i),1) >= 1 .and. o(s(i),2) <= c(i) ) cycle
    o(s(i),1) = o(s(i),1) + 1
    o(s(i),2) = c(i)
  end do

  do i = 1, n
    if( o(s(i),1) >= 1 ) cycle
    o(i,1) = 1
    o(i,2) = 0
  end do

  do i = 1, 3
    ans = ans + o(i,2) * int( 10**( n - i ) )
  end do

  if ( o(1,2) == 0 .and. ans > 9 ) ans = -1

  print*, ans

end program main


program main

  implicit none

  integer    :: n, m
  integer,allocatable :: s(:), c(:)

  integer,allocatable :: o(:,:)
  integer    :: i, j, k, ans

  read(*,*) n, m
  allocate( s(m) )
  allocate( c(m) )
  do i = 1, m
    read(*,*) s(i), c(i)
  end do
  allocate( o(n,2) )

  ans = 0
  o   = 0
  do i = 1, m
    if( o(s(i),1) >= 1 .and. o(s(i),2) <= c(i) ) cycle
    o(s(i),1) = o(s(i),1) + 1
    o(s(i),2) = c(i)
  end do

  do i = 1, n
    if( o(s(i),1) >= 1 ) cycle
    o(i,1) = 1
    o(i,2) = 0
  end do

  do i = 1, n
    ans = ans + o(i,2) * int( 10**( n - i ) )
  end do

  if ( o(1,2) == 0 .and. ans > 9 ) ans = -1

  print*, ans

end program main


program main
    implicit none
    integer n, m
    integer, allocatable, dimension(:) :: s, c
    integer i,j,k,l, flag
    integer, dimension(3) :: x
    read(*,*) n, m
    allocate(s(m))
    allocate(c(m))

    do i = 1, m
        read(*,*) s(i), c(i)
    end do

    x = 0
    flag = 0

    do i = 1,m
        if(x(s(i)) /= 0) then
            if(x(s(i)) /= c(i)) then
                flag = -1
                exit
            end if
        else
            x(s(i)) = c(i)
        end if

!--------------
        if(n == 3) then
            if(s(i) == 1) then
                if(c(i) == 0) then
                    flag = -1
                end if
            end if
        end if

!---------------
        if(n == 2) then
            if(s(i) == 1) then
                if(c(i) == 0) then
                    flag = -1
                end if
            end if
        end if

    end do

    if (flag == -1) then
        write(*,*) -1
    else
        if (n == 3) then
            if (x(1) == 0) then
                x(1) = 1
            end if
            write(*,*) 100*x(1)+10*x(2)+x(3)
        else if (n == 2) then
            if (x(1) == 0) then
                x(1) = 1
            end if
            write(*,*) 10*x(1)+x(2)
        else
            write(*,*) x(1)
        end if

    end if

end program


program example
	implicit none

	integer N,M,i,j
    integer,allocatable :: s(:),c(:)
	character a(10)
    character(32) b

	a(:)="0"

    read(*,*) N,M

    if (M==0) then
    	write(*,*) "0"
        goto 1
    end if

    allocate(s(M),c(M))

    do i=1,M
    	read(*,*)s(i),c(i)
	end do

    do i=1,M

    	if(s(i)==1 .and. c(i)==0 .and. N>=2) then
        	write(*,*) "-1"
            goto 1
    	end if

        if (s(i)>N) then
        	write(*,*) "-1"
            goto 1
        end if

    	do j=i+1,M
        	if(s(i)==s(j) .and. c(i)/=c(j)) then
            	write(*,*) "-1"
                goto 1
            end if
        end do
    end do

    do i=1,M
    	do j=1,N
			if (s(i)==j .and. c(i)>=0) then
            	write(b,*) c(i)
        		a(j)=trim(adjustl(b))
            end if
        end do
    end do

    write(*,*) adjustl(a(1:N))

1 end program example


program example
	implicit none

	integer N,M,i,j
    integer,allocatable :: s(:),c(:),seisuu(:)
	character a(10)
    character(32) b

	a(:)="0"

    read(*,*) N,M

    allocate(s(M),c(M),seisuu(N))

    do i=1,M
    	read(*,*)s(i),c(i)
	end do

    do i=1,M

    	if(s(i)==1 .and. c(i)==0) then
        	write(*,*) "-1"
            goto 1
    	end if

        if (s(i)>N) then
        	write(*,*) "-1"
            goto 1
        end if

    	do j=i+1,M
        	if(s(i)==s(j) .and. c(i)/=c(j)) then
            	write(*,*) "-1"
                goto 1
            end if
        end do
    end do

    do i=1,M
    	do j=1,N
			if (s(i)==j .and. c(i)>=0) then
            	write(b,*) c(i)
        		a(j)=trim(adjustl(b))
            end if
        end do
    end do

    write(*,*) adjustl(a(1:N))

1 end program example


program example
	implicit none

	integer N,M,i,j
    integer,allocatable :: s(:),c(:),seisuu(:)
	character a(10)
    character(32) b

	a(:)="0"

    read(*,*) N,M

    allocate(s(M),c(M),seisuu(N))

    do i=1,M
    	read(*,*)s(i),c(i)
	end do

    do i=1,M

    	if(s(i)==1 .and. c(i)==0) then
        	write(*,*) "-1"
            goto 1
    	end if

        if (s(i)>N) then
        	write(*,*) "-1"
            goto 1
        end if

    	do j=i+1,M
        	if(s(i)==s(j) .and. c(i)/=c(j)) then
            	write(*,*) "-1"
                goto 1
            end if
        end do
    end do

    do i=1,M
    	do j=1,N
			if (s(i)==j) then
            	write(b,*) c(i)
        		a(j)=trim(adjustl(b))
            end if
        end do
    end do

    write(*,*) adjustl(a(1:N))

1 end program example


program example
	implicit none

	integer N,M,i,j
    integer,allocatable :: s(:),c(:),seisuu(:)
	character a(10)
    character(32) b

	a(:)="0"

    read(*,*) N,M

    allocate(s(M),c(M),seisuu(N))

    do i=1,M
    	read(*,*)s(i),c(i)
	end do

    do i=1,M

    	if(s(i)==1 .and. c(i)==0) then
        	write(*,*) -1
            goto 1
    	end if

        if (s(i)>N) then
        	write(*,*) -1
            goto 1
        end if

    	do j=i+1,M
        	if(s(i)==s(j) .and. c(i)/=c(j)) then
            	write(*,*) -1
                goto 1
            end if
        end do
    end do

    do i=1,M
    	do j=1,N
			if (s(i)==j) then
            	write(b,*) c(i)
        		a(j)=trim(adjustl(b))
            end if
        end do
    end do

    write(*,*) adjustl(a(1:N))

1 end program example


program main
 implicit none
 integer N, M
 integer,allocatable :: s(:),c(:)
 integer i
 integer :: kurai(3)=-1

 read *,N, M
 if ( M==0 ) goto 100
 allocate(s(M))
 allocate(c(M))
 do i=1,M
  read *,s(i),c(i)
 end do

 do i=1,M
  if ( kurai(s(i)) == -1 ) then
   kurai(s(i))=c(i)
  else
   if ( kurai(s(i)) /= c(i) ) then
    print '(i0)', -1
    stop
   end if
  end if
! if ( kurai(1) == 0 ) then
!   print '(i0)', -1
!   stop
! end if
 end do

 if ( N==1 ) then
  if ( kurai(1) == -1 .and. kurai(2) == -1 ) goto 100
  if ( kurai(1) == -1 .and. kurai(2) == 0 ) goto 100
  if ( kurai(1) == 0 .and. kurai(2) == -1 ) goto 100
  if ( kurai(1) == 0 .and. kurai(2) == 0 ) goto 100
  if ( kurai(1) /= -1 .or. kurai(2) /= -1 ) then
    print '(i0)', -1
    stop
  end if
 end if

 if ( N==2 ) then
  if ( kurai(1) == 0 .and. kurai(2) >= 1 ) goto 100
  if ( kurai(1) == -1 .and. kurai(2) >= 1 ) goto 100
  if ( kurai(1) /= -1 ) then
    print '(i0)', -1
    stop
  end if
  if ( kurai(2) == 0 ) then
    print '(i0)', -1
    stop
  end if
 end if

 if ( N==3 ) then
  if ( kurai(1) == 0 ) then
    print '(i0)', -1
    stop
  end if
 end if

100  continue

if (N==1) then
  if (kurai(1) == -1) kurai(1)=0
  if (kurai(2) == -1) kurai(2)=0
  if (kurai(3) == -1) kurai(3)=0
end if

if (N==2) then
  if (kurai(1) == -1) kurai(1)=0
  if (kurai(2) == -1) kurai(2)=1
  if (kurai(3) == -1) kurai(3)=0
end if

if (N==3) then
  if (kurai(1) == -1) kurai(1)=1
  if (kurai(2) == -1) kurai(2)=0
  if (kurai(3) == -1) kurai(3)=0
end if

 if (N==1) print '(i0)', kurai(3)
 if (N==2) print '(i0)', kurai(2)*10+kurai(3)
 if (N==3) print '(i0)', kurai(1)*100+kurai(2)*10+kurai(3)

!print *,''
!print *,kurai(1),kurai(2),kurai(3)
 endprogram main


program example
	implicit none

	integer N,M,i,j
    integer,allocatable :: s(:),c(:),seisuu(:)
	character a(10)
    character(32) b

    read(*,*) N,M

    allocate(s(M),c(M),seisuu(N))

    do i=1,M
    	read(*,*)s(i),c(i)
	end do

    do i=1,M

    	if(s(i)==1 .and. c(i)==0) then
        	write(*,*) -1
            goto 1
    	end if

        if (s(i)>N) then
        	write(*,*) -1
            goto 1
        end if

    	do j=i+1,M
        	if(s(i)==s(j) .and. c(i)/=c(j)) then
            	write(*,*) -1
                goto 1
            end if
        end do
    end do

    do i=1,M
    	do j=1,N
			if (s(i)==j) then
            	write(b,*) c(i)
        		a(j)=trim(adjustl(b))
            else
            	a(j)="0"
            end if
        end do
    end do

    write(*,*) adjustl(a(1:N))

1 end program example


program main
 implicit none
 integer N, M
 integer,allocatable :: s(:),c(:)
 integer i
 integer :: kurai(3)=-1

 read *,N, M
 if ( M==0 ) goto 100
 allocate(s(M))
 allocate(c(M))
 do i=1,M
  read *,s(i),c(i)
 end do

 do i=1,M
  if ( kurai(s(i)) == -1 ) then
   kurai(s(i))=c(i)
  else
   if ( kurai(s(i)) /= c(i) ) then
    print '(i0)', -1
    stop
   end if
  end if
! if ( kurai(1) == 0 ) then
!   print '(i0)', -1
!   stop
! end if
 end do

 if ( N==1 ) then
  if ( kurai(1) == -1 .and. kurai(2) == -1 ) goto 100
  if ( kurai(1) == -1 .and. kurai(2) == 0 ) goto 100
  if ( kurai(1) == 0 .and. kurai(2) == -1 ) goto 100
  if ( kurai(1) == 0 .and. kurai(2) == 0 ) goto 100
  if ( kurai(1) /= -1 .or. kurai(2) /= -1 ) then
    print '(i0)', -1
    stop
  end if
 end if

 if ( N==2 ) then
  if ( kurai(1) == 0 .and. kurai(2) >= 1 ) goto 100
  if ( kurai(1) == -1 .and. kurai(2) >= 1 ) goto 100
  if ( kurai(1) /= -1 ) then
    print '(i0)', -1
    stop
  end if
  if ( kurai(2) == 0 ) then
    print '(i0)', -1
    stop
  end if
 end if

 if ( N==3 ) then
  if ( kurai(1) == 0 ) then
    print '(i0)', -1
    stop
  end if
 end if

100  continue

if (N==1) then
  if (kurai(1) == -1) kurai(1)=0
  if (kurai(2) == -1) kurai(2)=0
  if (kurai(3) == -1) kurai(3)=1
end if

if (N==2) then
  if (kurai(1) == -1) kurai(1)=0
  if (kurai(2) == -1) kurai(2)=1
  if (kurai(3) == -1) kurai(3)=0
end if

if (N==3) then
  if (kurai(1) == -1) kurai(1)=1
  if (kurai(2) == -1) kurai(2)=0
  if (kurai(3) == -1) kurai(3)=0
end if

 if (N==1) print '(i0)', kurai(3)
 if (N==2) print '(i0)', kurai(2)*10+kurai(3)
 if (N==3) print '(i0)', kurai(1)*100+kurai(2)*10+kurai(3)

!print *,''
!print *,kurai(1),kurai(2),kurai(3)
 endprogram main


program main
    implicit none
    integer n, m
    integer, allocatable, dimension(:) :: s, c
    integer i,j,k,l, flag
    integer, dimension(3) :: x
    read(*,*) n, m
    allocate(s(m))
    allocate(c(m))

    do i = 1, m
        read(*,*) s(i), c(i)
    end do

    x = 0
    flag = 0

    do i = 1,m
        if(x(s(i)) /= 0) then
            if(x(s(i)) /= c(i)) then
                flag = -1
                exit
            end if
        else
            x(s(i)) = c(i)
        end if

!--------------
        if(n == 3) then
            if(s(i) == 1) then
                if(c(i) == 0) then
                    flag = -1
                end if
            end if
        end if

!---------------
        if(n == 2) then
            if(s(i) == 1) then
                if(c(i) == 0) then
                    flag = -1
                end if
            end if
        end if

    end do

    if (flag == -1) then
        write(*,*) -1
    else
        if (n == 3) then
            if (x(1) == 0) then
                x(1) = 1
            end if
        end if

        if (n == 2) then
            if (x(2) == 0) then
                x(2) = 1
            end if
        end if

        write(*,*) 100*x(1)+10*x(2)+x(3)
    end if

end program


program main
    implicit none
    integer n, m
    integer, allocatable, dimension(:) :: s, c
    integer i,j,k,l, flag
    integer, dimension(3) :: x
    read(*,*) n, m
    allocate(s(m))
    allocate(c(m))

    do i = 1, m
        read(*,*) s(i), c(i)
    end do

    x = 0
    flag = 0

    do i = 1,m
        if(x(s(i)) /= 0) then
            if(x(s(i)) /= c(i)) then
                flag = -1
                exit
            end if
        else
            x(s(i)) = c(i)
        end if

!--------------
        if(n == 3) then
            if(s(i) == 1) then
                if(c(i) == 0) then
                    flag = -1
                end if
            end if
        end if

!---------------
        if(n == 2) then
            if(s(i) == 1) then
                if(c(i) == 0) then
                    flag = -1
                end if
            end if
        end if

    end do

    if (flag == -1) then
        write(*,*) -1
    else
        if (n == 3) then
            if (x(3) == 0) then
                x(3) = 1
            end if
        end if

        if (n == 2) then
            if (x(2) == 0) then
                x(2) = 1
            end if
        end if

        write(*,*) 100*x(1)+10*x(2)+x(3)
    end if

end program


program main
 implicit none
 integer N, M
 integer,allocatable :: s(:),c(:)
 integer i
 integer :: kurai(3)=-1

 read *,N, M
 allocate(s(M))
 allocate(c(M))
 do i=1,M
  read *,s(i),c(i)
 end do

 do i=1,M
  if ( kurai(s(i)) == -1 ) then
   kurai(s(i))=c(i)
  else
   if ( kurai(s(i)) /= c(i) ) then
    print '(i0)', -1
    stop
   end if
  end if
! if ( kurai(1) == 0 ) then
!   print '(i0)', -1
!   stop
! end if
 end do

!do i=1,3
! if (kurai(i) == -1) kurai(i)=0
!end do

 if ( N==1 ) then
  if ( kurai(1) == -1 .and. kurai(2) == -1 ) goto 100
  if ( kurai(1) == -1 .and. kurai(2) == 0 ) goto 100
  if ( kurai(1) == 0 .and. kurai(2) == -1 ) goto 100
  if ( kurai(1) == 0 .and. kurai(2) == 0 ) goto 100
  if ( kurai(1) /= -1 .or. kurai(2) /= -1 ) then
    print '(i0)', -1
    stop
  end if
 end if

 if ( N==2 ) then
! if ( kurai(1) /= 0 .or. kurai(2) == 0 ) then
  if ( kurai(1) == 0 .or. kurai(2) /= -1 ) goto 100
  if ( kurai(1) /= -1 ) then
    print '(i0)', -1
    stop
  end if
  if ( kurai(2) == -1 ) then
    print '(i0)', -1
    stop
  end if
 end if

 if ( N==3 ) then
  if ( kurai(1) == 0 .or. kurai(2) == 0 ) then
    print '(i0)', -1
    stop
  end if
 end if

100  continue

if (N==1) then
 do i=1,3
  if (kurai(1) == -1) kurai(i)=1
  if (kurai(2) == -1) kurai(i)=0
  if (kurai(3) == -1) kurai(i)=0
 end do
end if

if (N==2) then
 do i=1,3
  if (kurai(1) == -1) kurai(i)=0
  if (kurai(2) == -1) kurai(i)=1
  if (kurai(3) == -1) kurai(i)=0
 end do
end if

if (N==3) then
 do i=1,3
  if (kurai(1) == -1) kurai(i)=1
  if (kurai(2) == -1) kurai(i)=0
  if (kurai(3) == -1) kurai(i)=0
 end do
end if

 if (N==1) print '(i0)', kurai(3)
 if (N==2) print '(i0)', kurai(2)*10+kurai(3)
 if (N==3) print '(i0)', kurai(1)*100+kurai(2)*10+kurai(3)

!print *,''
!print *,kurai(1),kurai(2),kurai(3)
 endprogram main


program main
    implicit none
    integer n, m
    integer, allocatable, dimension(:) :: s, c
    integer i,j,k,l, flag
    integer, dimension(3) :: x
    read(*,*) n, m
    allocate(s(m))
    allocate(c(m))

    do i = 1, m
        read(*,*) s(i), c(i)
    end do

    x = 0
    flag = 0

    do i = 1,m
        if(x(s(i)) /= 0) then
            if(x(s(i)) /= c(i)) then
                flag = -1
                exit
            end if
        else
            x(s(i)) = c(i)
        end if

!--------------
        if(n == 3) then
            if(s(i) == 1) then
                if(c(i) == 0) then
                    flag = -1
                end if
            end if
        end if

!---------------
        if(n == 2) then
            if(s(i) == 1) then
                if(c(i) == 0) then
                    flag = -1
                end if
            end if
        end if

    end do

    if (flag == -1) then
        write(*,*) -1
    else
        write(*,*) 100*x(1)+10*x(2)+x(3)
    end if

end program


program main

  implicit none

  integer    :: n, m
  integer,allocatable :: s(:), c(:)

  integer,allocatable :: o(:,:)
  integer    :: i, j, k, ans

  read(*,*) n, m
  allocate( s(m) )
  allocate( c(m) )
  do i = 1, m
    read(*,*) s(i), c(i)
  end do
  allocate( o(n,2) )

  ans = 0
  o   = 0
  do i = 1, m
    if( o(s(i),1) == 1 .and. o(s(i),2) <= c(i) ) cycle
    o(s(i),1) = o(s(i),1) + 1
    o(s(i),2) = c(i)
  end do

  do i = 1, 3
    ans = ans + o(i,2) * int( 10**( n - i ) )
  end do

  if ( o(n,1) == 0 ) ans = -1

  print*, ans

end program main


program main

  implicit none

  integer    :: n, m
  integer,allocatable :: s(:), c(:)

  integer,allocatable :: o(:,:)
  integer    :: i, j, k, ans

  read(*,*) n, m
  allocate( s(m) )
  allocate( c(m) )
  do i = 1, m
    read(*,*) s(i), c(i)
  end do
  allocate( o(n,2) )

  ans = 0
  o   = 0
  do i = 1, m
    if( o(s(i),1) == 1 .and. o(s(i),2) <= c(i) ) cycle
    ans = ans + c(i) * int( 10**( n - s(i) ) )
    o(s(i),1) = o(s(i),1) + 1
    o(s(i),2) = s(i)
  end do

  if ( o(n,1) == 0 ) ans = -1

  print*, ans

end program main


program main
 implicit none
 integer N, M
 integer,allocatable :: s(:),c(:)
 integer i
 integer :: kurai(3)=-1

 read *,N, M
 allocate(s(M))
 allocate(c(M))
 do i=1,M
  read *,s(i),c(i)
 end do

 do i=1,M
  if ( kurai(s(i)) == -1 ) then
   kurai(s(i))=c(i)
  else
   if ( kurai(s(i)) /= c(i) ) then
    print '(i0)', -1
    stop
   end if
  end if
! if ( kurai(1) == 0 ) then
!   print '(i0)', -1
!   stop
! end if
 end do

 do i=1,3
  if (kurai(i) == -1) kurai(i)=0
 end do

 if ( N==1 ) then
  if ( kurai(1) /= 0 .or. kurai(2) /= 0 ) then
    print '(i0)', -1
    stop
  end if
 end if

 if ( N==2 ) then
! if ( kurai(1) /= 0 .or. kurai(2) == 0 ) then
  if ( kurai(1) /= 0 ) then
    print '(i0)', -1
    stop
  end if
  if ( kurai(2) == 0 ) then
    print '(i0)', -1
    stop
  end if
 end if

 if ( N==3 ) then
  if ( kurai(1) == 0 .or. kurai(2) == 0 ) then
    print '(i0)', -1
    stop
  end if
 end if

 if (N==1) print '(i0)', kurai(3)
 if (N==2) print '(i0)', kurai(2)*10+kurai(3)
 if (N==3) print '(i0)', kurai(1)*100+kurai(2)*10+kurai(3)

!print *,''
!print *,kurai(1),kurai(2),kurai(3)
 endprogram main


program main
  integer :: N,M
  integer,allocatable :: s(:),c(:)
  integer :: max_, min_, tmp
  integer :: i,j
  logical :: check
  read(*,*) N,M
  if (M == 0 .and. N /= 1) then
     write(*,'(i0)') 10**(N-1)
     stop
  else if (M == 0 .and. N == 1 ) then
     write(*,'(i0)') 0
     stop
  end if
  allocate(s(M),c(M))
  do i = 1,M
     read(*,*) s(i), c(i)
  end do
  !
  if (N /= 1) then
     do i = 1,M
        if (S(i) == 1 .and. c(i) == 0) then
           write(*,'(i0)') -1
           stop
        end if
     end do
  end if
  !
  do i = 1,M
     do j = i,M
        if ( S(j) == S(i) ) then
           if (c(j) /= c(i) ) then
              write(*,'(i0)') -1
              stop
           end if
        end if
     end do
  end do
  !
  !
  !
  max_ = 10**N - 1
  if (N /= 1) then
     min_ = 10**(N-1)
  else
     min_ = 0
  end if
  tmp = max_ + 1
  do j = min_, max_
     check = .false.
     do i = 1,M
        if ( digit(s(i),j,N)  == c(i) ) then
           check = .true.
        else
           check = .false.
           exit
        end if
     end do
     if (check .and. tmp > j) then
        tmp = j
     end if
  end do
  if (tmp > max_) then
     write(*,'(i0)') -1
  else
     write(*,'(i0)') tmp
  end if
contains
  integer function digit(s,i,n)
    integer :: s,i,n
    character(len=N) :: tmp

    write(tmp,'(i0)') i
    read(tmp(s:s),*) digit
    return
  end function digit
end program main


program main
  implicit none
  integer :: i, n, m, s, c, ans = 0
  integer, allocatable :: x(:)

  read *, n, m
  allocate (x(n))
  x(:) = -1

  do i = 1, m
    read *, s, c
    if (x(s) /= -1 .and. x(s) /= c) ans = -1
    if (s == 1 .and. n /= 1 .and. c == 0) ans = -1
    x(s) = c
  end do
  if (ans == -1) then
    print '(i0)', -1
  else
    if (n == 1) then
      ans = max(0, x(1))
    else
      ans = ans + max(1, x(1)) * (10 ** (n-1))
      do i = 2, n
        ans = ans + max(0, x(i)) * (10 ** (n-i))
      end do
    end if
    print '(i0)', ans
  end if
end program


program kadai
integer  ::  N,M,i
integer,allocatable  ::  a(:)
integer  ::  ans
integer,allocatable  ::  s(:),c(:)

read(*,*) N,M
allocate(s(M))
allocate(c(M))
allocate(a(N))
do i=1,M
    read(*,*) s(i),c(i)
end do

a=10
do i=1,M
    if (a(s(i))==10) then
        a(s(i))=c(i)
    else if (a(s(i))/=c(i)) then
        write(*,*) -1
        stop
    end if
end do

if (N>1 .and. a(1)==0) then
    write(*,*) -1
    stop
end if

if (N>1 .and. a(1)==10) then
    a(1)=1
end if
if (N==1 .and. a(1)==10) then
    a(1)=0
end if
if (N>1) then
    do i=2,N
        if (a(i)==10) then
            a(i)=0
        end if
    end do
end if

ans=0
!#write(*,*) a
do i=1,N
    ans=ans+a(i)*(10**(N-i))
end do
write(*,*) ans

stop

end program kadai


program kadai
integer  ::  N,M,i
integer,allocatable  ::  a(:)
integer  ::  ans
integer,allocatable  ::  s(:),c(:)

read(*,*) N,M
allocate(s(M))
allocate(c(M))
allocate(a(N))
do i=1,M
    read(*,*) s(i),c(i)
end do

a=10
do i=1,M
    if (a(s(i))==10) then
        a(s(i))=c(i)
    else if (a(s(i))/=c(i)) then
        write(*,*) -1
        stop
    end if
end do

if (N>1 .and. a(1)==0) then
    write(*,*) -1
    stop
end if

if (a(1)==10) then
    a(1)=1
end if
if (N>1) then
    do i=2,N
        if (a(i)==10) then
            a(i)=0
        end if
    end do
end if

ans=0
!#write(*,*) a
do i=1,N
    ans=ans+a(i)*(10**(N-i))
end do
write(*,*) ans

stop

end program kadai


program main
  implicit none
  integer :: i, n, m, s, c, ans = 0
  integer, allocatable :: x(:)

  read *, n, m
  allocate (x(n))
  x(:) = -1

  do i = 1, m
    read *, s, c
    if (x(s) /= -1 .and. x(s) /= c) ans = -1
    if (s == 1 .and. n /= 1 .and. c == 0) ans = -1
    x(s) = c
  end do
  do i = 1, n
    x(i) = max(0, x(i))
  end do
  if (ans == -1) then
    print '(i0)', -1
  else
    if (n == 1) then
      print '(i0)', x(1)
    else
      ans = ans + max(1, x(1)) * (10 ** (n-1))
    do i = 2, n
      ans = ans + x(i) * (10 ** (n-i))
    end do
    end if
    print '(i0)', ans
  end if
end program


program main
  implicit none
  integer(8) :: n, k, mi, ma, md, i
  integer(8), allocatable :: a(:)
  read *, n, k
  allocate (a(n))
  read *, a(:)
  call quicksort(a)

  ! 積がthres以下になるペアがk個以上ある最小のthres
  mi = -2 * 10_8 ** 18  ! NG
  ma = 2 * 10_8 ** 18  ! OK
  do i = 1, 70
    md = mi + (ma - mi) / 2
    if (isok(md)) then
      ma = md
    else
      mi = md
    end if
    if (ma - mi <= 1) exit
  end do

  print "(i0)", ma

  contains

  logical function isok(x)
    ! 積がx以下になるペアがk個以上ある
    integer(8), intent(in) :: x
    integer(8) :: i, cnt, jmin, jmax, count_j, mi, ma, md, j
    cnt = 0
    jmax = n
    if (x < 0) then
      jmin = 1
    else
      jmin = n + 1
    end if
    do i = 1, n - 1
      if (a(i) >= 0) then
        ! a(i) * a(j) <= x となる最大のj > iを求める
        jmax = max(jmax, i)
        do while (jmax > i)
          if (a(jmax) * a(i) > x) then
            jmax = jmax - 1
          else
            exit
          end if
        end do
        count_j = jmax - i
      else
        ! a(i) * a(j) <= x となる最小のj > iを求める
        jmin = max(jmin, i + 1)
        if (x < 0) then
          do while (jmin <= n)
            if (a(jmin) * a(i) > x) then
              jmin = jmin + 1
            else
              exit
            end if
          end do
          count_j = n - (jmin - 1)
        else
          do while (jmin > i + 1)
            if (a(jmin - 1) * a(i) <= x) then
              jmin = jmin - 1
            else
              exit
            end if
          end do
          count_j = n - (jmin - 1)
          ! mi = i  ! ng
          ! ma = n + 1  ! ok
          ! do j = 1, 30
          !   md = mi + (ma - mi) / 2
          !   if (a(i) * a(md) <= x) then
          !     ma = md
          !   else
          !     mi = md
          !   end if
          !   if (ma - mi <= 1) exit
          ! end do
          ! count_j = n - (ma - 1)
        end if
      end if
      cnt = cnt + count_j
    end do
    isok = (cnt >= k)
  end function isok

  recursive subroutine quicksort(a)
    integer(8), intent(inout) :: a(:)
    integer(8) :: pivot, i, j, k, tmp, n
    real(8) :: r
    n = size(a)
    if (n <= 1) return
    i = 1
    j = n
    call random_number(r)
    k = 1 + floor(r * n)
    k = max(1_8, min(k, n))
    pivot = a(k)
    do
      do while (.not. pivot <= a(i))
        i = i + 1
      end do
      do while (.not. a(j) <= pivot)
        j = j - 1
      end do
      if (i >= j) exit
      tmp = a(i)
      a(i) = a(j)
      a(j) = tmp
      i = i + 1
      j = j - 1
    end do
    call quicksort(a(1:i - 1))
    call quicksort(a(j + 1:n))
  end subroutine quicksort
end program main

Score: 400 points

Problem Statement

We have N integers A_1, A_2, ..., A_N.

There are \frac{N(N-1)}{2} ways to choose two of them and form a pair. If we compute the product of each of those pairs and sort the results in ascending order, what will be the K-th number in that list?

Constraints

All values in input are integers.

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

1 \leq K \leq \frac{N(N-1)}{2}

-10^9 \leq A_i \leq 10^9\ (1 \leq i \leq N)

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \dots A_N

Output

Print the answer.

Sample Input 1

4 3
3 3 -4 -2

Sample Output 1

-6

There are six ways to form a pair. The products of those pairs are 9, -12, -6, -12, -6, 8.

Sorting those numbers in ascending order, we have -12, -12, -6, -6, 8, 9. The third number in this list is -6.

Sample Input 2

10 40
5 4 3 2 -1 0 0 0 0 0

Sample Output 2

6

Sample Input 3

30 413
-170202098 -268409015 537203564 983211703 21608710 -443999067 -937727165 -97596546 -372334013 398994917 -972141167 798607104 -949068442 -959948616 37909651 0 886627544 -20098238 0 -948955241 0 -214720580 277222296 -18897162 834475626 0 -425610555 110117526 663621752 0

Sample Output 3

448283280358331064


program main
  implicit none
  integer(8) :: n, k, mi, ma, md, i
  integer(8), allocatable :: a(:)
  read *, n, k
  allocate (a(n))
  read *, a(:)
  call quicksort(a)

  ! 積がthres以下になるペアがk個以上ある最小のthres
  mi = -2 * 10_8 ** 18  ! NG
  ma = 2 * 10_8 ** 18  ! OK
  do i = 1, 70
    md = mi + (ma - mi) / 2
    if (isok(md)) then
      ma = md
    else
      mi = md
    end if
    if (ma - mi <= 1) exit
  end do

  print "(i0)", ma

  contains

  logical function isok(x)
    ! 積がx以下になるペアがk個以上ある
    integer(8), intent(in) :: x
    integer(8) :: i, cnt, jmin, jmax, count_j, mi, ma, md, j
    cnt = 0
    jmax = n
    if (x < 0) then
      jmin = 1
    else
      jmin = n + 1
    end if
    do i = 1, n - 1
      if (a(i) >= 0) then
        ! a(i) * a(j) <= x となる最大のj > iを求める
        jmax = max(jmax, i)
        do while (jmax > i)
          if (a(jmax) * a(i) > x) then
            jmax = jmax - 1
          else
            exit
          end if
        end do
        count_j = jmax - i
      else
        ! a(i) * a(j) <= x となる最小のj > iを求める
        jmin = max(jmin, i + 1)
        if (x < 0) then
          do while (jmin <= n)
            if (a(jmin) * a(i) > x) then
              jmin = jmin + 1
            else
              exit
            end if
          end do
          count_j = n - (jmin - 1)
        else
          ! do while (jmin > i)
          !   if (a(jmin - 1) * a(i) > x) then
          !     jmin = jmin - 1
          !   else
          !     exit
          !   end if
          ! end do
          ! count_j = n - (jmin - 1)
          mi = i  ! ng
          ma = n + 1  ! ok
          do j = 1, 30
            md = mi + (ma - mi) / 2
            if (a(i) * a(md) <= x) then
              ma = md
            else
              mi = md
            end if
            if (ma - mi <= 1) exit
          end do
          count_j = n - (ma - 1)
        end if
      end if
      cnt = cnt + count_j
    end do
    isok = (cnt >= k)
  end function isok

  recursive subroutine quicksort(a)
    integer(8), intent(inout) :: a(:)
    integer(8) :: pivot, i, j, k, tmp, n
    real(8) :: r
    n = size(a)
    if (n <= 1) return
    i = 1
    j = n
    call random_number(r)
    k = 1 + floor(r * n)
    k = max(1_8, min(k, n))
    pivot = a(k)
    do
      do while (.not. pivot <= a(i))
        i = i + 1
      end do
      do while (.not. a(j) <= pivot)
        j = j - 1
      end do
      if (i >= j) exit
      tmp = a(i)
      a(i) = a(j)
      a(j) = tmp
      i = i + 1
      j = j - 1
    end do
    call quicksort(a(1:i - 1))
    call quicksort(a(j + 1:n))
  end subroutine quicksort
end program main


program main
  implicit none
  integer(8) :: n, k, mi, ma, md, i
  integer(8), allocatable :: a(:)
  read *, n, k
  allocate (a(n))
  read *, a(:)
  call quicksort(a)

  ! 積がthres以下になるペアがk個以上ある最小のthres
  mi = -2 * 10_8 ** 18  ! NG
  ma = 2 * 10_8 ** 18  ! OK
  do i = 1, 70
    md = mi + (ma - mi) / 2
    if (isok(md)) then
      ma = md
    else
      mi = md
    end if
    if (ma - mi <= 1) exit
  end do

  print "(i0)", ma

  contains

  logical function isok(x)
    ! 積がx以下になるペアがk個以上ある
    integer(8), intent(in) :: x
    integer(8) :: i, cnt, jmin, jmax, count_j, mi, ma, md, j
    cnt = 0
    jmax = n
    jmin = 1
    do i = 1, n - 1
      if (a(i) >= 0) then
        ! a(i) * a(j) <= x となる最大のj > iを求める
        jmax = max(jmax, i)
        do while (jmax > i)
          if (a(jmax) * a(i) > x) then
            jmax = jmax - 1
          else
            exit
          end if
        end do
        count_j = jmax - i
      else
        ! a(i) * a(j) <= x となる最小のj > iを求める
        jmin = max(jmin, i + 1)
        do while (jmin <= n)
          if (a(jmin) * a(i) > x) then
            jmin = jmin + 1
          else
            exit
          end if
        end do
        count_j = n - (jmin - 1)
        ! mi = i  ! ng
        ! ma = n + 1  ! ok
        ! do j = 1, 30
        !   md = mi + (ma - mi) / 2
        !   if (a(i) * a(md) <= x) then
        !     ma = md
        !   else
        !     mi = md
        !   end if
        !   if (ma - mi <= 1) exit
        ! end do
        ! count_j = n - (ma - 1)
      end if
      cnt = cnt + count_j
    end do
    isok = (cnt >= k)
  end function isok

  recursive subroutine quicksort(a)
    integer(8), intent(inout) :: a(:)
    integer(8) :: pivot, i, j, k, tmp, n
    real(8) :: r
    n = size(a)
    if (n <= 1) return
    i = 1
    j = n
    call random_number(r)
    k = 1 + floor(r * n)
    k = max(1_8, min(k, n))
    pivot = a(k)
    do
      do while (.not. pivot <= a(i))
        i = i + 1
      end do
      do while (.not. a(j) <= pivot)
        j = j - 1
      end do
      if (i >= j) exit
      tmp = a(i)
      a(i) = a(j)
      a(j) = tmp
      i = i + 1
      j = j - 1
    end do
    call quicksort(a(1:i - 1))
    call quicksort(a(j + 1:n))
  end subroutine quicksort
end program main


program main
  implicit none
  integer(8) :: n, k, mi, ma, md, i
  integer(8), allocatable :: a(:)
  read *, n, k
  allocate (a(n))
  read *, a(:)
  call quicksort(a)

  ! 積がthres以下になるペアがk個以上ある最小のthres
  mi = -2 * 10_8 ** 18  ! NG
  ma = 2 * 10_8 ** 18  ! OK
  do i = 1, 70
    md = mi + (ma - mi) / 2
    if (isok(md)) then
      ma = md
    else
      mi = md
    end if
    if (ma - mi <= 1) exit
  end do

  print "(i0)", ma

  contains

  logical function isok(x)
    ! 積がx以下になるペアがk個以上ある
    integer(8), intent(in) :: x
    integer(8) :: i, cnt, jmin, jmax, count_lower, mi, ma, md, j
    cnt = 0
    jmax = n
    jmin = 1
    do i = 1, n - 1
      if (a(i) >= 0) then
        ! a(i) * a(j) <= x となる最大のjを求める
        jmax = max(jmax, i)
        do while (jmax > i)
          if (a(jmax) * a(i) > x) then
            jmax = jmax - 1
          else
            exit
          end if
        end do
        count_lower = jmax - i
      else
        ! a(i) * a(j) <= x となる最小のjを求める
        ! jmin = max(jmin, i + 1)
        ! do while (jmin <= n)
        !   if (a(jmin) * a(i) > x) then
        !     jmin = jmin + 1
        !   else
        !     exit
        !   end if
        ! end do
        ! count_lower = n - jmin + 1
        ! a(i) * a(j) <= x となる最小のjを求める
        mi = i  ! ng
        ma = n + 1  ! ok
        do j = 1, 30
          md = mi + (ma - mi) / 2
          if (a(i) * a(md) <= x) then
            ma = md
          else
            mi = md
          end if
          if (ma - mi <= 1) exit
        end do
        count_lower = n - (ma - 1)
      end if
      cnt = cnt + count_lower
    end do
    isok = (cnt >= k)
  end function isok

  recursive subroutine quicksort(a)
    integer(8), intent(inout) :: a(:)
    integer(8) :: pivot, i, j, k, tmp, n
    real(8) :: r
    n = size(a)
    if (n <= 1) return
    i = 1
    j = n
    call random_number(r)
    k = 1 + floor(r * n)
    k = max(1_8, min(k, n))
    pivot = a(k)
    do
      do while (.not. pivot <= a(i))
        i = i + 1
      end do
      do while (.not. a(j) <= pivot)
        j = j - 1
      end do
      if (i >= j) exit
      tmp = a(i)
      a(i) = a(j)
      a(j) = tmp
      i = i + 1
      j = j - 1
    end do
    call quicksort(a(1:i - 1))
    call quicksort(a(j + 1:n))
  end subroutine quicksort
end program main


program main
  implicit none
  integer(8) :: n, k, mi, ma, md, i
  integer(8), allocatable :: a(:)
  read *, n, k
  allocate (a(n))
  read *, a(:)
  call quicksort(a)

  ! 積がthres以下になるペアがk個以上ある最小のthres
  mi = -1 * 10_8 ** 18 - 1  ! NG
  ma = 1 * 10_8 ** 18 + 1  ! OK
  do i = 1, 70
    md = mi + (ma - mi) / 2
    if (isok(md)) then
      ma = md
    else
      mi = md
    end if
    if (ma - mi <= 1) exit
  end do

  print "(i0)", ma

  contains

  logical function isok(x)
    ! 積がx以下になるペアがk個以上ある
    integer(8), intent(in) :: x
    integer(8) :: i, cnt, jmin, jmax, count_lower
    cnt = 0
    jmax = n
    jmin = 1
    do i = 1, n - 1
      if (a(i) >= 0) then
        ! a(i) * a(j) <= x となる最大のjを求める
        jmax = max(jmax, i)
        do while (jmax > i)
          if (a(jmax) * a(i) > x) then
            jmax = jmax - 1
          else
            exit
          end if
        end do
        count_lower = jmax - i
      else
        ! a(i) * a(j) <= x となる最小のjを求める
        jmin = max(jmin, i + 1)
        do while (jmin <= n)
          if (a(jmin) * a(i) > x) then
            jmin = jmin + 1
          else
            exit
          end if
        end do
        count_lower = n - jmin + 1
      end if
      cnt = cnt + count_lower
    end do
    isok = (cnt >= k)
  end function isok

  recursive subroutine quicksort(a)
    integer(8), intent(inout) :: a(:)
    integer(8) :: pivot, i, j, k, tmp, n
    real(8) :: r
    n = size(a)
    if (n <= 1) return
    i = 1
    j = n
    call random_number(r)
    k = 1 + floor(r * n)
    k = max(1_8, min(k, n))
    pivot = a(k)
    do
      do while (.not. pivot <= a(i))
        i = i + 1
      end do
      do while (.not. a(j) <= pivot)
        j = j - 1
      end do
      if (i >= j) exit
      tmp = a(i)
      a(i) = a(j)
      a(j) = tmp
      i = i + 1
      j = j - 1
    end do
    call quicksort(a(1:i - 1))
    call quicksort(a(j + 1:n))
  end subroutine quicksort
end program main


program main
  implicit none
  integer(8) :: n, k, mi, ma, md, i
  integer(8), allocatable :: a(:)
  read *, n, k
  allocate (a(n))
  read *, a(:)
  call quicksort(a)

  ! 積がthres以下になるペアがk個以上ある最小のthres
  mi = -1 * 10_8 ** 18 - 1  ! NG
  ma = 1 * 10_8 ** 18 + 1  ! OK
  do i = 1, 70
    md = mi + (ma - mi) / 2
    if (isok(md)) then
      ma = md
    else
      mi = md
    end if
    if (ma - mi <= 1) exit
  end do

  print "(i0)", ma

  contains

  logical function isok(x)
    ! 積がx以下になるペアがk個以上ある
    integer(8), intent(in) :: x
    integer(8) :: i, cnt, jmin, jmax, count_lower
    cnt = 0
    jmax = n
    jmin = 1
    do i = 1, n
      if (a(i) >= 0) then
        ! a(i) * a(j) <= x となる最大のjを求める
        jmax = max(jmax, i)
        do while (jmax > i)
          if (a(jmax) * a(i) > x) then
            jmax = jmax - 1
          else
            exit
          end if
        end do
        count_lower = jmax - i
      else
        ! a(i) * a(j) <= x となる最小のjを求める
        jmin = max(jmin, i + 1)
        do while (jmin <= n)
          if (a(jmin) * a(i) > x) then
            jmin = jmin + 1
          else
            exit
          end if
        end do
        count_lower = n - jmin + 1
      end if
      cnt = cnt + count_lower
    end do
    isok = (cnt >= k)
  end function isok

  recursive subroutine quicksort(a)
    integer(8), intent(inout) :: a(:)
    integer(8) :: pivot, i, j, k, tmp, n
    real(8) :: r
    n = size(a)
    if (n <= 1) return
    i = 1
    j = n
    call random_number(r)
    k = 1 + floor(r * n)
    k = max(1_8, min(k, n))
    pivot = a(k)
    do
      do while (.not. pivot <= a(i))
        i = i + 1
      end do
      do while (.not. a(j) <= pivot)
        j = j - 1
      end do
      if (i >= j) exit
      tmp = a(i)
      a(i) = a(j)
      a(j) = tmp
      i = i + 1
      j = j - 1
    end do
    call quicksort(a(1:i - 1))
    call quicksort(a(j + 1:n))
  end subroutine quicksort
end program main


program main
  implicit none
  integer(8) :: n, k, mi, ma, md, i
  integer(8), allocatable :: a(:)
  read *, n, k
  allocate (a(n))
  read *, a(:)
  call quicksort(a)

  ! 積がthres以下になるペアがk個以上ある最小のthres
  mi = -2 * 10_8 ** 18  ! NG
  ma = 2 * 10_8 ** 18  ! OK
  do i = 1, 70
    md = mi + (ma - mi) / 2
    if (isok(md)) then
      ma = md
    else
      mi = md
    end if
    if (ma - mi <= 1) exit
  end do

  print "(i0)", ma

  contains

  logical function isok(x)
    ! 積がx以下になるペアがk個以上ある
    integer(8), intent(in) :: x
    integer(8) :: i, cnt
    cnt = 0
    do i = 1, n
      cnt = cnt + count_lower(i, x)
    end do
    isok = (cnt >= k)
  end function isok

  integer(8) function count_lower(i, x)
    ! a(i) * a(j) <= x となるj>iの数
    integer(8), intent(in) :: i, x
    integer(8) :: j, mi, md, ma
    if (a(i) >= 0) then
      ! a(i) * a(j) <= x となる最大のjを求める
      mi = i  ! ok
      ma = n + 1  ! ng
      do j = 1, 30
        md = mi + (ma - mi) / 2
        if (a(i) * a(md) <= x) then
          mi = md
        else
          ma = md
        end if
        if (ma - mi <= 1) exit
      end do
      count_lower = mi - i
    else
      ! a(i) * a(j) <= x となる最小のjを求める
      mi = i  ! ng
      ma = n + 1  ! ok
      do j = 1, 30
        md = mi + (ma - mi) / 2
        if (a(i) * a(md) <= x) then
          ma = md
        else
          mi = md
        end if
        if (ma - mi <= 1) exit
      end do
      count_lower = n - (ma - 1)
    end if
  end function count_lower

  recursive subroutine quicksort(a)
    integer(8), intent(inout) :: a(:)
    integer(8) :: pivot, i, j, k, tmp, n
    real(8) :: r
    n = size(a)
    if (n <= 1) return
    i = 1
    j = n
    call random_number(r)
    k = 1 + floor(r * n)
    k = max(1_8, min(k, n))
    pivot = a(k)
    do
      do while (.not. pivot <= a(i))
        i = i + 1
      end do
      do while (.not. a(j) <= pivot)
        j = j - 1
      end do
      if (i >= j) exit
      tmp = a(i)
      a(i) = a(j)
      a(j) = tmp
      i = i + 1
      j = j - 1
    end do
    call quicksort(a(1:i - 1))
    call quicksort(a(j + 1:n))
  end subroutine quicksort
end program main


program main
  implicit none
  integer(8) :: n, k, mi, ma, md, i
  integer(8), allocatable :: a(:)
  read *, n, k
  allocate (a(n))
  read *, a(:)
  call quicksort(a)

  ! 積がthres以下になるペアがk個以上ある最小のthres
  mi = -2 * 10_8 ** 18  ! NG
  ma = 2 * 10_8 ** 18  ! OK
  do i = 1, 70
    md = mi + (ma - mi) / 2
    if (isok(md)) then
      ma = md
    else
      mi = md
    end if
  end do

  print "(i0)", ma

  contains

  logical function isok(x)
    ! 積がx以下になるペアがk個以上ある
    integer(8), intent(in) :: x
    integer(8) :: i, cnt
    isok = .false.
    cnt = 0
    do i = 1, n
      cnt = cnt + count_lower(i, x)
      if (cnt >= k) then
        isok = .true.
        return
      end if
    end do
  end function isok

  integer(8) function count_lower(i, x)
    ! a(i) * a(j) <= x となるj>iの数
    integer(8), intent(in) :: i, x
    integer(8) :: j, mi, md, ma
    if (a(i) >= 0) then
      ! a(i) * a(j) <= x となる最大のjを求める
      mi = i  ! ok
      ma = n + 1  ! ng
      do j = 1, 30
        md = mi + (ma - mi) / 2
        if (a(i) * a(md) <= x) then
          mi = md
        else
          ma = md
        end if
        if (ma - mi <= 1) exit
      end do
      count_lower = mi - i
    else
      ! a(i) * a(j) <= x となる最小のjを求める
      mi = i  ! ng
      ma = n + 1  ! ok
      do j = 1, 30
        md = mi + (ma - mi) / 2
        if (a(i) * a(md) <= x) then
          ma = md
        else
          mi = md
        end if
        if (ma - mi <= 1) exit
      end do
      count_lower = n - (ma - 1)
    end if
  end function count_lower

  recursive subroutine quicksort(a)
    integer(8), intent(inout) :: a(:)
    integer(8) :: pivot, i, j, k, tmp, n
    real(8) :: r
    n = size(a)
    if (n <= 1) return
    i = 1
    j = n
    call random_number(r)
    k = 1 + floor(r * n)
    k = max(1_8, min(k, n))
    pivot = a(k)
    do
      do while (.not. pivot <= a(i))
        i = i + 1
      end do
      do while (.not. a(j) <= pivot)
        j = j - 1
      end do
      if (i >= j) exit
      tmp = a(i)
      a(i) = a(j)
      a(j) = tmp
      i = i + 1
      j = j - 1
    end do
    call quicksort(a(1:i - 1))
    call quicksort(a(j + 1:n))
  end subroutine quicksort
end program main


program main
  implicit none
  integer(8) :: n, k, mi, ma, md, i
  integer(8), allocatable :: a(:)
  read *, n, k
  allocate (a(n))
  read *, a(:)
  call quicksort(a)

  ! 積がthres以下になるペアがk個以上ある最小のthres
  mi = -2 * 10_8 ** 18  ! NG
  ma = 2 * 10_8 ** 18  ! OK
  do i = 1, 70
    md = mi + (ma - mi) / 2
    if (isok(md)) then
      ma = md
    else
      mi = md
    end if
  end do

  print "(i0)", ma

  contains

  logical function isok(x)
    ! 積がx以下になるペアがk個以上ある
    integer(8), intent(in) :: x
    integer(8) :: i, cnt
    cnt = 0
    do i = 1, n
      cnt = cnt + count_lower(i, x)
    end do
    isok = (cnt >= k)
  end function isok

  integer(8) function count_lower(i, x)
    ! a(i) * a(j) <= x となるj>iの数
    integer(8), intent(in) :: i, x
    integer(8) :: j, mi, md, ma
    if (a(i) >= 0) then
      ! a(i) * a(j) <= x となる最大のjを求める
      mi = i  ! ok
      ma = n + 1  ! ng
      do j = 1, 30
        md = mi + (ma - mi) / 2
        if (a(i) * a(md) <= x) then
          mi = md
        else
          ma = md
        end if
        if (ma - mi <= 1) exit
      end do
      count_lower = mi - i
    else
      ! count_lower = 0
      ! do j = i + 1, n
      !   if (a(i) * a(j) <= x) count_lower = count_lower + 1
      ! end do

      ! a(i) * a(j) <= x となる最小のjを求める
      mi = i  ! ng
      ma = n + 1  ! ok
      do j = 1, 30
        md = mi + (ma - mi) / 2
        if (a(i) * a(md) <= x) then
          ma = md
        else
          mi = md
        end if
        if (ma - mi <= 1) exit
      end do
      count_lower = n - (ma - 1)
    end if
  end function count_lower

  recursive subroutine quicksort(a)
    integer(8), intent(inout) :: a(:)
    integer(8) :: pivot, i, j, k, tmp, n
    real(8) :: r
    n = size(a)
    if (n <= 1) return
    i = 1
    j = n
    call random_number(r)
    k = 1 + floor(r * n)
    k = max(1_8, min(k, n))
    pivot = a(k)
    do
      do while (.not. pivot <= a(i))
        i = i + 1
      end do
      do while (.not. a(j) <= pivot)
        j = j - 1
      end do
      if (i >= j) exit
      tmp = a(i)
      a(i) = a(j)
      a(j) = tmp
      i = i + 1
      j = j - 1
    end do
    call quicksort(a(1:i - 1))
    call quicksort(a(j + 1:n))
  end subroutine quicksort
end program main


program main
  implicit none
  integer(8) :: n, k, mi, ma, md, i, x, y, z, negs, zeros, poss
  integer(8), allocatable :: a(:)
  read (*, *) n, k
  allocate (a(n))
  read (*, *) a(:)
  call quicksort(a)
  call count_pairs(a, n, negs, zeros, poss, x, y, z)
  if (negs < k .and. k <= negs + zeros) then  ! Zero
    write (*, "(i0)") 0
  else
    mi = -2 * 10_8 ** 18  ! Not large enough
    ma = 2 * 10_8 ** 18  ! Large enough
    do i = 1, 200
      md = mi + (ma - mi) / 2
      if (m_is_large_enough(md, k, n, a, negs, x)) then
        ma = md
      else
        mi = md
      end if
    end do
    write (*, "(i0)") ma
  end if

  contains

  logical function m_is_large_enough(m, k, n, a, negs, x)
    integer(8), intent(in) :: n, k, m, a(n), x, negs
    integer(8) :: cnt
    if (k <= negs) then  ! Negative
      cnt = shakutori_n(x, z, a(1:x), a(x + y + 1:n), m)
    else
      cnt = 0  ! ttk shakutori_p(a(1:x), a(x + y + 1:n), m)
    end if
    m_is_large_enough = (cnt >= k)
  end function m_is_large_enough

  integer(8) function shakutori_n(x, z, negs, poss, m)
    integer(8), intent(in) :: x, z, negs(x), poss(z), m
    integer(8) :: p1, p2
    ! Count combinations no more than m
    shakutori_n = 0
    p2 = z + 1
    outer: do p1 = 1, x
      do while (negs(p1) * poss(p2 - 1) > m)
        p2 = p2 - 1
        if (p2 <= 1) exit outer
      end do
      shakutori_n = shakutori_n + p2 - 1
    end do outer
  end function shakutori_n

  recursive subroutine quicksort(a)
    integer(8), intent(inout) :: a(:)
    integer(8) :: pivot, i, j, k, tmp, n
    real(4) :: r
    n = size(a)
    if (n <= 1) return
    i = 1
    j = n
    call random_number(r)
    k = 1 + floor(r * n)
    k = max(1, min(k, n))
    pivot = a(k)
    do
      do while (.not. pivot <= a(i))
        i = i + 1
      end do
      do while (.not. a(j) <= pivot)
        j = j - 1
      end do
      if (i >= j) exit
      tmp = a(i)
      a(i) = a(j)
      a(j) = tmp
      i = i + 1
      j = j - 1
    end do
    call quicksort(a(1:i - 1))
    call quicksort(a(j + 1:n))
  end subroutine quicksort

  subroutine count_pairs(a, n, negs, zeros, poss, nega, zeroa, posa)
    integer(8), intent(in) :: n, a(n)
    integer(8), intent(out) :: negs, zeros, poss, nega, zeroa, posa
    integer(8) :: i
    do i = 1, n
      if (a(i) < 0) nega = i
      if (a(i) <= 0) zeroa = i
    end do
    posa = n - zeroa
    zeroa = zeroa - nega
    negs = nega * posa
    zeros = zeroa * (posa + nega) + (zeroa * (zeroa - 1)) / 2
    poss = (n * (n - 1)) / 2 - negs - zeros
  end subroutine count_pairs
end program main


program pairs
  implicit none
  integer :: n, i
  integer(8) :: k, l = -1e18, r = 1e18, m, c
  integer(8), dimension(200000) :: a = 0, b = 0
  read(*,*) n, k
  read(*,*) a(1:n)
  call quick_sort(a(1:n))
  do while (r-l > 1)
    m = (l+r)/2
    c = 0
    do i = 1, n
      if (a(i) < 0) then
        c = c+count_lower(a(n:i+1:-1),a(i),m)
      else
        c = c+count_lower(a(i+1:n),a(i),m)
      end if
    end do
    if (c < k) then
      l = m
    else
      r = m
    end if
  end do
  write(*,'(i0)') l
contains
  function count_lower(a,b,c) result(r)
    integer(8), intent(in) :: a(:), b, c
    integer :: l, r, m
    l = 0
    r = size(a)+1
    do while (r-l > 1)
      m = (l+r)/2
      if (a(m)*b < c) then
        l = m
      else
        r = m
      end if
    end do
    r = r-1
  end
  recursive subroutine quick_sort(a)
    integer(8), intent(inout) :: a(:)
    integer :: n, l, r, m
    integer(8) :: p
    n = size(a,1)
    l = 1
    r = n
    m = (n+1)/2
    p = a(l)+a(m)+a(r)-max(a(l),a(m),a(r))-min(a(l),a(m),a(r))
    do
      do while (a(l) < p)
        l = l+1
      end do
      do while (a(r) > p)
        r = r-1
      end do
      if (l >= r) exit
      a(l) = xor(a(l),a(r))
      a(r) = xor(a(l),a(r))
      a(l) = xor(a(l),a(r))
      l = l+1
      r = r-1
    end do
    if (l-1 > 1) call quick_sort(a(1:l-1))
    if (n > r+1) call quick_sort(a(r+1:n))
  end
end program pairs


program main

  implicit none

  integer    :: n,k
  integer,allocatable :: a(:)

  integer    :: i,j,pn
  integer(8),allocatable :: p(:)

  read(*,*) n, k
  allocate( a(n) )
  !allocate( p(int(n*(n-1))+1) )
  allocate( p(n*(n-1)/2) )
  read(*,*) a

  pn = 0
  do i = 1, n
    do j = i+1, n
      pn = pn + 1
      p(pn) = a(i) * a(j)
    end do
  end do

  !call quicksort1( p, 1, int( n*(n-1) )+1 )
  call quicksort1( p, 1, n*(n-1)/2 )

  print*, p(k)

end program main

recursive subroutine quicksort1(a, first, last)
  implicit none
  integer(8),intent(inout)::a(*)
  integer,intent(in)::first,last
  !
  ! Original
  ! https://gist.github.com/t-nissie/479f0f16966925fa29ea
  !
  integer(8)::x,t
  integer::i,j

  x = a((first+last)/2)
  i = first
  j = last
  do
     do while (a(i) < x)
        i=i+1
     end do
     do while (x < a(j))
        j=j-1
     end do
     if (i >= j) exit
     t = a(i)
     a(i) = a(j)
     a(j) = t
     i=i+1
     j=j-1
  end do
  if (first < i-1) call quicksort1(a, first, i-1)
  if (j+1 < last)  call quicksort1(a, j+1, last)

  return
end subroutine quicksort1


program main

  implicit none

  integer    :: n,k
  integer(8),allocatable :: a(:)

  integer    :: i,j,pn
  integer(8),allocatable :: p(:)

  read(*,*) n, k
  allocate( a(n) )
  !allocate( p(int(n*(n-1))+1) )
  allocate( p(n*(n-1)/2) )
  read(*,*) a

  pn = 0
  do i = 1, n
    do j = i+1, n
      pn = pn + 1
      p(pn) = a(i) * a(j)
    end do
  end do

  !call quicksort1( p, 1, int( n*(n-1) )+1 )
  call quicksort1( p, 1, n*(n-1)/2 )

  print*, p(k)

end program main

recursive subroutine quicksort1(a, first, last)
  implicit none
  integer(8),intent(inout)::a(*)
  integer,intent(in)::first,last
  !
  ! Original
  ! https://gist.github.com/t-nissie/479f0f16966925fa29ea
  !
  integer(8)::x,t
  integer::i,j

  x = a((first+last)/2)
  i = first
  j = last
  do
     do while (a(i) < x)
        i=i+1
     end do
     do while (x < a(j))
        j=j-1
     end do
     if (i >= j) exit
     t = a(i)
     a(i) = a(j)
     a(j) = t
     i=i+1
     j=j-1
  end do
  if (first < i-1) call quicksort1(a, first, i-1)
  if (j+1 < last)  call quicksort1(a, j+1, last)

  return
end subroutine quicksort1


program prob4
    implicit none
    integer :: a,b
    read(*,*) a,b

    if(a <= 9 .and. b <= 9) then
        write(*,*) a*b
    else
        write(*,*) "-1"
    end if

    stop
contains
end program prob4

Score : 100 points

Problem Statement

Having learned the multiplication table, Takahashi can multiply two integers between 1 and 9 (inclusive) together. He cannot do any other calculation.

Given are two integers A and B.

If Takahashi can calculate A \times B, print the result; if he cannot, print -1 instead.

Constraints

1 \leq A \leq 20

1 \leq B \leq 20

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

If Takahashi can calculate A \times B, print the result; if he cannot, print -1.

Sample Input 1

2 5

Sample Output 1

10

2 \times 5 = 10.

Sample Input 2

5 10

Sample Output 2

-1

5\times 10 = 50, but Takahashi cannot do this calculation, so print -1 instead.

Sample Input 3

9 9

Sample Output 3

81


program prob
	implicit none
    integer :: a,b

    read(*,*) a,b
    if(a <= 9 .and. b <= 9) then
    	write(*,*) a*b
    else
    	write(*,*) "-1"
    end if
    stop

end program prob


program main
  implicit none
  integer :: a, b
  read *, a, b
  if (a < 10 .and. b < 10) then
    print "(i0)", a * b
  else
    print "(i0)", -1
  end if
end program main


integer a,b
read*,a,b
if(a>9 .or. b>9)then
  print"(i0)",-1
else
  print"(i0)",a*b
endif
end


program main
 implicit none
 integer a,b
 read(*,*) a,b
 if(a > 9 .or. b > 9) then
  write(*,*) -1
 else
  write(*,*) a*b
 end if
end program main


! 2019.10.27
!
      program answer
      implicit none
      integer :: A
      integer :: B
      integer :: ans
!
      read(*,*) A,B
!
      ans = A * B
      if(A > 9 .or. B > 9) then
        ans = -1
      end if
      write(*,*) ans
      end program answer


program ABC144A
    implicit none
    integer::A,B
    read*,A,B
    if((A>=1 .and. A<=9).and.(B>=1 .and. B<=9)) then
        print'(i0)',A*B
    else
        print'(i0)',-1
    end if

end program ABC144A


program AGC039A
    implicit none
    character(100)::S
    integer(8)::K,i,ans,cont,ans_add,cont_first,flg=0
    read*,S,K
    ans=0
    cont=1
    do i=2,len_trim(S)
        if(S(i:i)==S(i-1:i-1))then
            cont=cont+1
        else
            if(flg==0)then
                cont_first=cont
                flg=1
            end if
            ans=ans+cont/2
            cont=1
        end if
    end do

    if(S(1:1)==S(len_trim(S):len_trim(S)))then
        ans=ans-cont_first/2
        ans=ans*K
        ans_add=(cont+cont_first)/2
        ans=ans+(K-1)*ans_add
        ans=ans+cont_first/2+cont/2
    else
        ans=ans+cont/2
        ans=ans*K
    end if
    print'(i0)',ans
end program AGC039A

Score : 300 points

Problem Statement

Given is a string S. Let T be the concatenation of K copies of S.
We can repeatedly perform the following operation: choose a character in T and replace it with a different character.
Find the minimum number of operations required to satisfy the following condition: any two adjacent characters in T are different.

Constraints

1 \leq |S| \leq 100

S consists of lowercase English letters.

1 \leq K \leq 10^9

K is an integer.

Input

Input is given from Standard Input in the following format:

S
K

Output

Print the minimum number of operations required.

Sample Input 1

issii
2

Sample Output 1

4

T is issiiissii. For example, we can rewrite it into ispiqisyhi, and now any two adjacent characters are different.

Sample Input 2

qq
81

Sample Output 2

81

Sample Input 3

cooooooooonteeeeeeeeeest
999993333

Sample Output 3

8999939997


program AGC39_A
  character(100)::S
  integer(8)::i,J=1,K,L(100)=1,M(100)=1,N
  read(*,*)S
  read(*,*)K
  N=len_trim(S)
  do i=1,N-1
     if(S(i:i)==S(i+1:i+1)) then
        L(J)=L(J)+1
     else
        J=J+1
     end if
  end do
  if(S(1:1)/=S(N:N)) then
     L=L/2
     write(*,*)sum(L)*K
  else
     M=L
     if(J/=1) then
        M(J)=M(J)+M(1)
        M(1)=0
        M=M/2
        L=L/2
        write(*,*)L(1)+sum(M)*K-M(J)+L(J)
     else
        write(*,*)M(1)*K/2
     end if
  end if
end program AGC39_A


program name
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: k,i,n,a1,a2
    character(100):: s
    character(200):: s2
    logical:: one_word = .true.

    read*, s
    read*, k
    n = len_trim(s)
    do i=1,n
        if (s(1:1) /= s(i:i)) one_word = .false.
    end do

    if (one_word) then
        print'(i0)', (n*k)/2
        stop
    end if

    s2 = trim(s) // trim(s)

    a1 = serch(s,n)
    a2 = serch(s2,2*n)

    print'(i0)', a1*k + (a2-2*a1)*(k-1)

contains
    function serch(str,n) result(ret)
        character(*):: str
        integer(int64):: n,ret
        integer(int64):: i

        ret = 0
        do i=2,n
            if (str(i:i) == str(i-1:i-1)) then
                ret=ret+1
                str(i:i)='0'
            end if
        end do
    end function
end program name


program name
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: k,i,n,a1,a2
    character(100):: s
    character(200):: s2

    read*, s
    read*, k
    n = len_trim(s)
    s2 = trim(s) // trim(s)

    a1 = serch(s,n)
    a2 = serch(s2,2*n)

    print'(i0)', a1*k + (0)*(k-1)

contains
    function serch(str,n) result(ret)
        character(*):: str
        integer(int64):: n,ret
        integer(int64):: i

        ret = 0
        do i=2,n
            if (str(i:i) == str(i-1:i-1)) then
                ret=ret+1
                str(i:i)='0'
            end if
        end do
    end function
end program name


program name
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: k,i,n,a1,a2
    character(100):: s
    character(200):: s2

    read*, s
    read*, k
    n = len_trim(s)
    s2 = trim(s) // trim(s)

    a1 = serch(s,n)
    a2 = serch(s2,2*n)

    print'(i0)', a1*k + (a2-2*a1)*(k-1)

contains
    function serch(str,n) result(ret)
        character(*):: str
        integer(int64):: n,ret
        integer(int64):: i

        ret = 0
        do i=2,n
            if (str(i:i) == str(i-1:i-1)) then
                ret=ret+1
                str(i:i)='0'
            end if
        end do
    end function
end program name


program aaa
implicit none

integer(16) :: k, i, j, l, left, right, mid, res
character(105) :: s
character(1) :: a

read*, s
read*, k

left=0
right=0
mid=0
res=0

l=len_trim(s)
s=trim(s)

if(s(1:1)==s(l:l)) then
a=s(1:1)
 do i=1,l
  if(a==s(i:i)) then
  left=left+1
  else
  exit
  end if
 end do
 do i=l,1,-1
  if(a==s(i:i)) then
  right=right+1
  else
  exit
  end if
 end do
 do i=1,left
 s(i:i)='#'
 end do
 do i=l,(l-right+1),-1
 s(i:i)='#'
 end do
end if

if(2*l==left+right) then
write(*,'(i0)') k*l/2
return
end if

do i=1,l
 if (s(i:i)/='#') then
  do j=i+1,l
   if(s(i:i)==s(j:j)) then
   mid=mid+1
   s(j:j)='#'
    if(j/=l) then
     if(s(i:i)/=s(j+1:j+1)) then
     mid=mid+1
     res=res+mid/2
     mid=0
     end if
    end if
   else
   exit
   end if
  end do
 end if
end do

res=res*k+((left+right)/2)*(k-1)+left/2+right/2
write(*,'(i0)') res

end program


program main
implicit none
integer:: i,ind,oc
integer(8):: k, ans
character(101):: rs
character(:), allocatable:: s
integer, allocatable:: counter(:)
character(1):: cfirst, cend, currentchar, os

read*, rs,k
allocate(character(len_trim(rs)):: s)
s = trim(rs)
! print*, len(s)
allocate(counter(len(s)))
cfirst = s(1:1)
cend = s(len(s):len(s))
ind = 0
counter(1:len(s)) = 0
currentchar = '0'
if (len(s) == 1)then
    print*, k/2
    stop
end if

do i=2, len(s)
    os = s(i-1:i-1)
    if (.not. os == s(i:i)) exit
    if (i==len(s))then
        if (mod(k*len(s),2) == 0)then
            print*, (len(s)*k)/2
        else
            print*, (len(s)*k)/2
        end if
        stop
    endif

end do

do i=1, len(s)
    if(currentchar == s(i:i))then
        ! print*, '#1-1'
        counter(ind) = counter(ind) + 1
    else
        ! print*, '#1-2'
        currentchar = s(i:i)
        ind = ind + 1
        counter(ind) = counter(ind) + 1
    end if
    ! print*, counter
end do

ans = sum(counter(1:len(s))/2)
! print*, counter
if (cfirst == cend)then

    oc = counter(1)
    counter(ind) = counter(ind) + counter(1)
    counter(1) = counter(1) - oc
end if

ans = ans + sum(counter(1:len(s))/2)*(k-1)
! print*, counter

print*, ans
end program


program main
implicit none
integer:: i,ind,oc
integer(8):: k, ans
character(101):: rs
character(:), allocatable:: s
integer, allocatable:: counter(:)
character(1):: cfirst, cend, currentchar, os

read*, rs,k
allocate(character(len_trim(rs)):: s)
s = trim(rs)
! print*, len(s)
allocate(counter(len(s)))
cfirst = s(1:1)
cend = s(len(s):len(s))
ind = 0
counter(1:len(s)) = 0
currentchar = '0'
if (len(s) == 1)then
    print*, k/2
    stop
end if

do i=2, len(s)
    os = s(i-1:i-1)
    if (.not. os == s(i:i)) exit
    if (i==len(s))then
        if (mod(k*len(s),2) == 0)then
            print*, (len(s)*k)/2
        else
            print*, (len(s)*k)/2+1
        end if
        stop
    endif

end do

do i=1, len(s)
    if(currentchar == s(i:i))then
        ! print*, '#1-1'
        counter(ind) = counter(ind) + 1
    else
        ! print*, '#1-2'
        currentchar = s(i:i)
        ind = ind + 1
        counter(ind) = counter(ind) + 1
    end if
    ! print*, counter
end do

ans = sum(counter(1:len(s))/2)
! print*, counter
if (cfirst == cend)then

    oc = counter(1)
    counter(ind) = counter(ind) + counter(1)
    counter(1) = counter(1) - oc
end if

ans = ans + sum(counter(1:len(s))/2)*(k-1)
! print*, counter

print*, ans
end program


      PROGRAM connectionAndDisconnection
      IMPLICIT NONE
      character(100) :: s
      INTEGER(16) :: k, ans

      INTEGER(16),allocatable :: flags(:)
      INTEGER :: len, i
      LOGICAL :: lastFlag = .false., sameFlag = .true.

      read*,s
      READ*,k

      len = len_trim( s )
      allocate ( flags(len) )
      flags = 0

      DO i = 1,len-1
        IF( s(i:i)/=s(i+1:i+1) )THEN
          sameFlag = .false.
          EXIT
        END IF
      END DO

      DO i = 1,len-1
        IF( s(i:i)==s(i+1:i+1) )THEN
          flags(i) = 1
          s(i+1:i+1) = 'X'
        END IF
      END DO
      !i = len
      IF( s(len:len)==s(1:1) )THEN
        flags(len) = 1
      END IF

      IF( flags(len-1)==0 )THEN
        IF( flags(len)==1 )THEN
          lastFlag = .true.
        END IF
      END IF

      ans = sum( flags(:) ) * k
      IF(lastFlag)THEN
        ans = ans -1
      END IF

      IF( len==1 )THEN
        IF( mod(k,2)==0 )THEN
          ans = k/2
        ELSE
          ans = (k-1)/2
        END IF
      END IF

      IF( sameFlag )THEN
        IF( mod(k*len,2)==0 )THEN
          ans = k*len/2
        ELSE
          ans = (k*len-1)/2
        END IF
      END IF
      print*,ans

      END PROGRAM


program main
implicit none
integer:: i,ind,oc
integer(8):: k, ans
character(101):: rs
character(:), allocatable:: s
integer, allocatable:: counter(:)
character(1):: cfirst, cend, currentchar, os

read*, rs,k
allocate(character(len_trim(rs)):: s)
s = trim(rs)
! print*, len(s)
allocate(counter(len(s)))
cfirst = s(1:1)
cend = s(len(s):len(s))
ind = 0
counter(1:len(s)) = 0
currentchar = '0'
if (len(s) == 1)then
    print*, k/2
    stop
end if

do i=2, len(s)
    os = s(i-1:i-1)
    if (.not. os == s(i:i)) exit
    if (i==len(s))then
        ! print*, len(s)*k
        print*, ceiling(real(len(s)*k)/2.,kind=8)
        stop
    endif

end do

do i=1, len(s)
    if(currentchar == s(i:i))then
        ! print*, '#1-1'
        counter(ind) = counter(ind) + 1
    else
        ! print*, '#1-2'
        currentchar = s(i:i)
        ind = ind + 1
        counter(ind) = counter(ind) + 1
    end if
    ! print*, counter
end do

ans = sum(counter(1:len(s))/2)
! print*, counter
if (cfirst == cend)then

    oc = counter(1)
    counter(ind) = counter(ind) + counter(1)
    counter(1) = counter(1) - oc
end if

ans = ans + sum(counter(1:len(s))/2)*(k-1)
! print*, counter

print*, ans
end program


program main
implicit none
integer:: i,ind,oc
integer(8):: k, ans
character(101):: rs
character(:), allocatable:: s
integer, allocatable:: counter(:)
character(1):: cfirst, cend, currentchar, os

read*, rs,k
allocate(character(len_trim(rs)):: s)
s = trim(rs)
! print*, len(s)
allocate(counter(len(s)))
cfirst = s(1:1)
cend = s(len(s):len(s))
ind = 0
counter(1:len(s)) = 0
currentchar = '0'
if (len(s) == 1)then
    print*, k/2
    stop
end if

do i=2, len(s)
    os = s(i-1:i-1)
    if (.not. os == s(i:i)) exit
    if (i==len(s))then
        print*, int(ceiling(real(len(s)*k)/2.))
        stop
    endif

end do

do i=1, len(s)
    if(currentchar == s(i:i))then
        ! print*, '#1-1'
        counter(ind) = counter(ind) + 1
    else
        ! print*, '#1-2'
        currentchar = s(i:i)
        ind = ind + 1
        counter(ind) = counter(ind) + 1
    end if
    ! print*, counter
end do

ans = sum(counter(1:len(s))/2)
! print*, counter
if (cfirst == cend)then

    oc = counter(1)
    counter(ind) = counter(ind) + counter(1)
    counter(1) = counter(1) - oc
end if

ans = ans + sum(counter(1:len(s))/2)*(k-1)
! print*, counter

print*, ans
end program


program main
implicit none
integer:: i,ind,oc
integer(8):: k, ans
character(101):: rs
character(:), allocatable:: s
integer, allocatable:: counter(:)
character(1):: cfirst, cend, currentchar, os

read*, rs,k
allocate(character(len_trim(rs)):: s)
s = trim(rs)
! print*, len(s)
allocate(counter(len(s)))
cfirst = s(1:1)
cend = s(len(s):len(s))
ind = 0
counter(1:len(s)) = 0
currentchar = '0'
do i=2, len(s)
    os = s(i-1:i-1)
    if (.not. os == s(i:i)) exit
    if (i==len(s))then
        print*, int(ceiling(real(len(s)*k)/2.))
        stop
    endif

end do

do i=1, len(s)
    if(currentchar == s(i:i))then
        ! print*, '#1-1'
        counter(ind) = counter(ind) + 1
    else
        ! print*, '#1-2'
        currentchar = s(i:i)
        ind = ind + 1
        counter(ind) = counter(ind) + 1
    end if
    ! print*, counter
end do

ans = sum(counter(1:len(s))/2)
! print*, counter
if (cfirst == cend)then
    oc = counter(1)
    counter(ind) = counter(ind) + counter(1)
    counter(1) = counter(1) - oc
end if

ans = ans + sum(counter(1:len(s))/2)*(k-1)
! print*, counter

print*, ans
end program


program main
implicit none
integer:: i,ind,oc
integer(8):: k, ans
character(101):: rs
character(:), allocatable:: s
integer, allocatable:: counter(:)
character(1):: cfirst, cend, currentchar, os

read*, rs,k
allocate(character(len_trim(rs)):: s)
s = trim(rs)
! print*, len(s)
allocate(counter(len(s)))
cfirst = s(1:1)
cend = s(len(s):len(s))
ind = 0
counter(1:len(s)) = 0
currentchar = '0'
do i=2, len(s)
    os = s(i-1:i-1)
    if (.not. os == s(i:i)) exit
    if (i==len(s))then
        print*, ceiling(real(len(s)*k)/2.)
        stop
    endif

end do

do i=1, len(s)
    if(currentchar == s(i:i))then
        ! print*, '#1-1'
        counter(ind) = counter(ind) + 1
    else
        ! print*, '#1-2'
        currentchar = s(i:i)
        ind = ind + 1
        counter(ind) = counter(ind) + 1
    end if
    ! print*, counter
end do

ans = sum(counter(1:len(s))/2)
! print*, counter
if (cfirst == cend)then
    oc = counter(1)
    counter(ind) = counter(ind) + counter(1)
    counter(1) = counter(1) - oc
end if

ans = ans + sum(counter(1:len(s))/2)*(k-1)
! print*, counter

print*, ans
end program


program main
implicit none
integer:: i,ind,oc
integer(8):: k, ans
character(101):: rs
character(:), allocatable:: s
integer, allocatable:: counter(:)
character(1):: cfirst, cend, currentchar, os

read*, rs,k
allocate(character(len_trim(rs)):: s)
s = trim(rs)
! print*, len(s)
allocate(counter(len(s)))
cfirst = s(1:1)
cend = s(len(s):len(s))
ind = 0
counter(1:len(s)) = 0
currentchar = '0'
do i=2, len(s)
    os = s(i-1:i-1)
    if (.not. os == s(i:i)) exit
    if (i==len(s))then
        print*, (len(s)*k)/2.
        stop
    endif

end do

do i=1, len(s)
    if(currentchar == s(i:i))then
        ! print*, '#1-1'
        counter(ind) = counter(ind) + 1
    else
        ! print*, '#1-2'
        currentchar = s(i:i)
        ind = ind + 1
        counter(ind) = counter(ind) + 1
    end if
    ! print*, counter
end do

ans = sum(counter(1:len(s))/2)
! print*, counter
if (cfirst == cend)then
    oc = counter(1)
    counter(ind) = counter(ind) + counter(1)
    counter(1) = counter(1) - oc
end if

ans = ans + sum(counter(1:len(s))/2)*(k-1)
! print*, counter

print*, ans
end program


      PROGRAM connectionAndDisconnection
      IMPLICIT NONE
      character(100) :: s
      INTEGER(16) :: k, ans

      INTEGER,allocatable :: flags(:)
      INTEGER :: len, i
      LOGICAL :: lastFlag = .false.

      read*,s
      READ*,k

      len = len_trim( s )
      allocate ( flags(len) )
      flags = 0

      DO i = 1,len-1
        IF( s(i:i)==s(i+1:i+1) )THEN
          flags(i) = 1
          s(i+1:i+1) = 'X'
        END IF
      END DO
      !i = len
      IF( s(len:len)==s(1:1) )THEN
        flags(len) = 1
      END IF

      IF( flags(len-1)==0 )THEN
        IF( flags(len)==1 )THEN
          lastFlag = .true.
        END IF
      END IF

      ans = sum( flags(:) ) * k
      IF(lastFlag)THEN
        ans = ans -1
      END IF

      IF( len==1 )THEN
        IF( mod(k,2)==0 )THEN
          ans = k/2
        ELSE
          ans = (k-1)/2
        END IF
      END IF
      print*,ans

      END PROGRAM


      PROGRAM connectionAndDisconnection
      IMPLICIT NONE
      character(100) :: s
      INTEGER(16) :: k, ans

      INTEGER,allocatable :: flags(:)
      INTEGER :: len, i
      LOGICAL :: lastFlag = .false.

      read*,s
      READ*,k

      len = len_trim( s )
      allocate ( flags(len) )
      flags = 0

      DO i = 1,len-1
        IF( s(i:i)==s(i+1:i+1) )THEN
          flags(i) = 1
          s(i+1:i+1) = 'X'
        END IF
      END DO
      !i = len
      IF( s(len:len)==s(1:1) )THEN
        flags(len) = 1
      END IF

      IF( flags(len-1)==0 )THEN
        IF( flags(len)==1 )THEN
          lastFlag = .true.
        END IF
      END IF

      ans = sum( flags(:) ) * k
      IF(lastFlag)THEN
        ans = ans -1
      END IF

      IF( len==1 )THEN
        print*,k-1
      ELSE
        print*,ans
      END IF

      END PROGRAM


program main
implicit none
integer:: i,ind,oc
integer(8):: k, ans
character(101):: rs
character(:), allocatable:: s
integer, allocatable:: counter(:)
character(1):: cfirst, cend, currentchar

read*, rs,k
allocate(character(len_trim(rs)):: s)
s = trim(rs)
! print*, len(s)
allocate(counter(len(s)))
cfirst = s(1:1)
cend = s(len(s):len(s))
ind = 0
counter(1:len(s)) = 0
currentchar = '101'
do i=1, len(s)
    if(currentchar == s(i:i))then
        ! print*, '#1-1'
        counter(ind) = counter(ind) + 1
    else
        ! print*, '#1-2'
        currentchar = s(i:i)
        ind = ind + 1
        counter(ind) = counter(ind) + 1
    end if
    ! print*, counter
end do

ans = sum(counter(1:len(s))/2)
! print*, counter
if (cfirst == cend)then
    oc = counter(1)
    counter(ind) = counter(ind) + counter(1)
    counter(1) = counter(1) - oc
end if

ans = ans + sum(counter(1:len(s))/2)*(k-1)
! print*, counter

print*, ans
end program


      PROGRAM connectionAndDisconnection
      IMPLICIT NONE
      character(100) :: s
      INTEGER(8) :: k, ans

      INTEGER,allocatable :: flags(:)
      INTEGER :: len, i
      LOGICAL :: lastFlag = .false.

      read*,s
      READ*,k

      len = len_trim( s )
      allocate ( flags(len) )
      flags = 0

      DO i = 1,len-1
        IF( s(i:i)==s(i+1:i+1) )THEN
          flags(i) = 1
          s(i+1:i+1) = 'X'
        END IF
      END DO
      !i = len
      IF( s(len:len)==s(1:1) )THEN
        flags(len) = 1
      END IF

      IF( flags(len-1)==0 )THEN
        IF( flags(len)==1 )THEN
          lastFlag = .true.
        END IF
      END IF

      ans = sum( flags(:) ) * k
      IF(lastFlag)THEN
        ans = ans -1
      END IF

      print*,ans

      END PROGRAM


program connection_and_disconnection
  implicit none
  character(100) :: s
  integer :: n, i, x(100) = 0, y(100) = 0
  integer(8) :: k, a1 = 0_8, a2 = 0_8
  read(*,*) s
  read(*,*) k
  n = len_trim(s)
  do i = 1, n
    x(i) = ichar(s(i:i))-96
    y(i) = x(i)
  end do
  do i = 2, n
    if (x(i) == x(i-1)) then
      x(i) = 100
      a1 = a1+1_8
    end if
  end do
  a1 = k*a1
  if (x(n) == x(1)) a1 = a1+k-1_8
  do i = n-1, 1, -1
    if (y(i) == y(i+1)) then
      y(i) = 100
      a2 = a2+1_8
    end if
  end do
  a2 = k*a2
  if (y(1) == y(n)) a2 = a2+k-1_8
  write(*,'(i0)') min(a1,a2)
end program connection_and_disconnection