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

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

    do i=k+1,n
        if (a(i) > a(i-k)) then
            print'(a)', 'Yes'
        else
            print'(a)', 'No'
        end if
    end do
end program main

Score: 300 points

Problem Statement

M-kun is a student in Aoki High School, where a year is divided into N terms.

There is an exam at the end of each term. According to the scores in those exams, a student is given a grade for each term, as follows:

For the first through (K-1)-th terms: not given.

For each of the K-th through N-th terms: the multiplication of the scores in the last K exams, including the exam in the graded term.

M-kun scored A_i in the exam at the end of the i-th term.

For each i such that K+1 \leq i \leq N, determine whether his grade for the i-th term is strictly greater than the grade for the (i-1)-th term.

Constraints

2 \leq N \leq 200000

1 \leq K \leq N-1

1 \leq A_i \leq 10^{9}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 A_3 \ldots A_N

Output

Print the answer in N-K lines.

The i-th line should contain Yes if the grade for the (K+i)-th term is greater than the grade for the (K+i-1)-th term, and No otherwise.

Sample Input 1

5 3
96 98 95 100 20

Sample Output 1

Yes
No

His grade for each term is computed as follows:

3-rd term: (96 \times 98 \times 95) = 893760

4-th term: (98 \times 95 \times 100) = 931000

5-th term: (95 \times 100 \times 20) = 190000

Sample Input 2

3 2
1001 869120 1001

Sample Output 2

No

Note that the output should be No if the grade for the 3-rd term is equal to the grade for the 2-nd term.

Sample Input 3

15 7
3 1 4 1 5 9 2 6 5 3 5 8 9 7 9

Sample Output 3

Yes
Yes
No
Yes
Yes
No
Yes
Yes


program answer
  implicit none
  integer :: i, N, sum, ka
  integer, allocatable :: A(:)
  read(*,*) N
  allocate(A(N))
  read(*,*) A

  sum=1000
  ka=0

  do i = 1, N-1
     if(A(i)<=A(i+1)) then
        ka=sum/A(i)
        sum=sum-ka*A(i)
     else
        sum=sum+ka*A(i)
	ka=0
     end if
  end do
 write(*,*) sum

  deallocate(A)
  stop
 end program answer

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

Score: 400 points

Problem Statement

To become a millionaire, M-kun has decided to make money by trading in the next N days. Currently, he has 1000 yen and no stocks - only one kind of stock is issued in the country where he lives.

He is famous across the country for his ability to foresee the future. He already knows that the price of one stock in the next N days will be as follows:

A_1 yen on the 1-st day, A_2 yen on the 2-nd day, ..., A_N yen on the N-th day.

In the i-th day, M-kun can make the following trade any number of times (possibly zero), within the amount of money and stocks that he has at the time.

Buy stock: Pay A_i yen and receive one stock.

Sell stock: Sell one stock for A_i yen.

What is the maximum possible amount of money that M-kun can have in the end by trading optimally?

Constraints

2 \leq N \leq 80

100 \leq A_i \leq 200

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \cdots A_N

Output

Print the maximum possible amount of money that M-kun can have in the end, as an integer.

Sample Input 1

7
100 130 130 130 115 115 150

Sample Output 1

1685

In this sample input, M-kun has seven days of trading. One way to have 1685 yen in the end is as follows:

Initially, he has 1000 yen and no stocks.

Day 1: Buy 10 stocks for 1000 yen. Now he has 0 yen.

Day 2: Sell 7 stocks for 910 yen. Now he has 910 yen.

Day 3: Sell 3 stocks for 390 yen. Now he has 1300 yen.

Day 4: Do nothing.

Day 5: Buy 1 stock for 115 yen. Now he has 1185 yen.

Day 6: Buy 10 stocks for 1150 yen. Now he has 35 yen.

Day 7: Sell 11 stocks for 1650 yen. Now he has 1685 yen.

There is no way to have 1686 yen or more in the end, so the answer is 1685.

Sample Input 2

6
200 180 160 140 120 100

Sample Output 2

1000

In this sample input, it is optimal to do nothing throughout the six days, after which we will have 1000 yen.

Sample Input 3

2
157 193

Sample Output 3

1216

In this sample input, it is optimal to buy 6 stocks in Day 1 and sell them in Day 2, after which we will have 1216 yen.

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 answer
  implicit none
  integer :: i, N, sum, ka
  integer, allocatable :: A(:)
  read(*,*) N
  allocate(A(N))
  read(*,*) A

  sum=1000

  do i = 1, N-1
     if(A(i)<=A(i+1)) then
	sum=(int(sum/A(i)))*(A(i+1)-A(i))+sum
     end if
  end do

  write(*,*) sum

  deallocate(A)
  stop
 end program answer


module mod_two_dim_merge_sort
  implicit none
  integer, private, parameter :: intkind = 4
contains
  logical function less(a, b) result(res)
    integer(intkind), intent(in) :: a(:), b(:)
    res = a(1) < b(1) .or. (a(1) == b(1) .and. a(2) <= b(2))
  end
  subroutine merge_sort(a)
    integer(intkind), intent(inout) :: a(:, :)
    integer :: n, m, l, i, u
    n = size(a, 1)
    m = n
    l = 1
    do while (m > 1)
      do i = 1, m / 2
        u = min(2 * i * l, n)
        call merger(a(2 * (i - 1) * l + 1:(2 * i - 1) * l, :), &
        a((2 * i - 1) * l + 1:u, :))
      end do
      l = 2 * l
      m = (m + 1) / 2
    end do
  end
  subroutine merger(a1, a2)
    integer(intkind), intent(inout) :: a1(:, :), a2(:, :)
    integer(intkind) :: a(size(a1, 1) + size(a2, 1), size(a1, 2))
    integer :: i1, i2, n1, n2
    i1 = 1
    i2 = 1
    n1 = size(a1, 1)
    n2 = size(a2, 1)
    do while (i1 <= n1 .and. i2 <= n2)
      if (less(a1(i1, :), a2(i2, :))) then
        a(i1 + i2 - 1, :) = a1(i1, :)
        i1 = i1 + 1
      else
        a(i1 + i2 - 1, :) = a2(i2, :)
        i2 = i2 + 1
       end if
    end do
    if (i1 <= n1) then
      a(i1 + i2 - 1:n1 + n2, :) = a1(i1:n1, :)
    else
      a(i1 + i2 - 1:n1 + n2, :) = a2(i2:n2, :)
    end if
    a1 = a(1:n1, :)
    a2 = a(n1 + 1:n1 + n2, :)
  end
end module mod_two_dim_merge_sort
program air_safety
  use mod_two_dim_merge_sort
  implicit none
  integer, parameter :: inf = 1e9
  integer :: n, x(200000) = 0, y(200000) = 0, u(200000) = 0, i, j, k, z
  integer :: p(200000, 2) = 0, m(0:400000) = 0, ans = inf
  character(1) :: s
  type t_list
    integer, allocatable :: at(:, :)
  end type
  type(t_list) :: list(0:400000)
  read(*,*) n
  do i = 1, n
    read(*,*) x(i), y(i), s
    select case (s)
    case ('U')
      u(i) = 1
    case ('R')
      u(i) = 2
    case ('D')
      u(i) = 3
    case ('L')
      u(i) = 4
    end select
  end do
  do k = 1, 4
    if (k == 1 .or. k == 2) then
      m = 0
      do i = 1, n
        if (mod(u(i), 2) == 1) then
          m(x(i)) = m(x(i)) + 1
        end if
      end do
      do i = 0, 200000
        if (m(i) == 0) cycle
        allocate(list(i)%at(m(i), 2))
      end do
      m = 0
      do i = 1, n
        if (mod(u(i), 2) == 1) then
          m(x(i)) = m(x(i)) + 1
          list(x(i))%at(m(x(i)), 1) = y(i)
          list(x(i))%at(m(x(i)), 2) = u(i)
        end if
      end do
      do i = 0, 200000
        if (m(i) == 0) cycle
        call merge_sort(list(i)%at(1:m(i), :))
        do j = 1, m(i) - 1
          if (list(i)%at(j + 1, 2) - list(i)%at(j, 2) == 2) then
            ans = min(ans, 5 * (list(i)%at(j + 1, 1) - list(i)%at(j, 1)))
          end if
        end do
        deallocate(list(i)%at)
      end do
    end if
    m = 0
    do i = 1, n
      if (u(i) == 1 .or. u(i) == 2) then
        z = x(i) + y(i)
        m(z) = m(z) + 1
      end if
    end do
    do i = 0, 400000
      if (m(i) == 0) cycle
      allocate(list(i)%at(m(i), 2))
    end do
    m = 0
    do i = 1, n
      if (u(i) == 1 .or. u(i) == 2) then
        z = x(i) + y(i)
        m(z) = m(z) + 1
        list(z)%at(m(z), 1) = y(i)
        list(z)%at(m(z), 2) = u(i)
      end if
    end do
    do i = 0, 400000
      if (m(i) == 0) cycle
      call merge_sort(list(i)%at(1:m(i), :))
      do j = 1, m(i) - 1
        if (list(i)%at(j + 1, 2) - list(i)%at(j, 2) == 1) then
          ans = min(ans, 10 * (list(i)%at(j + 1, 1) - list(i)%at(j, 1)))
        end if
      end do
      deallocate(list(i)%at)
    end do
    do i = 1, n
      u(i) = u(i) + 1
      if (u(i) == 5) u(i) = 1
      z = x(i)
      x(i) = y(i)
      y(i) = 200000 - z
    end do
  end do
  if (ans == inf) then
    write(*,'(a)') 'SAFE'
  else
    write(*,'(i0)') ans
  end if
end program air_safety

Score: 600 points

Problem Statement

M-kun is a brilliant air traffic controller.

On the display of his radar, there are N airplanes numbered 1, 2, ..., N, all flying at the same altitude.

Each of the airplanes flies at a constant speed of 0.1 per second in a constant direction. The current coordinates of the airplane numbered i are (X_i, Y_i), and the direction of the airplane is as follows:

if U_i is U, it flies in the positive y direction;

if U_i is R, it flies in the positive x direction;

if U_i is D, it flies in the negative y direction;

if U_i is L, it flies in the negative x direction.

To help M-kun in his work, determine whether there is a pair of airplanes that will collide with each other if they keep flying as they are now.

If there is such a pair, find the number of seconds after which the first collision will happen.

We assume that the airplanes are negligibly small so that two airplanes only collide when they reach the same coordinates simultaneously.

Constraints

1 \leq N \leq 200000

0 \leq X_i, Y_i \leq 200000

U_i is U, R, D, or L.

The current positions of the N airplanes, (X_i, Y_i), are all distinct.

N, X_i, and Y_i are integers.

Input

Input is given from Standard Input in the following format:

N
X_1 Y_1 U_1
X_2 Y_2 U_2
X_3 Y_3 U_3
:
X_N Y_N U_N

Output

If there is a pair of airplanes that will collide with each other if they keep flying as they are now, print an integer representing the number of seconds after which the first collision will happen.

If there is no such pair, print SAFE.

Sample Input 1

2
11 1 U
11 47 D

Sample Output 1

230

If the airplanes keep flying as they are now, two airplanes numbered 1 and 2 will reach the coordinates (11, 24) simultaneously and collide.

Sample Input 2

4
20 30 U
30 20 R
20 10 D
10 20 L

Sample Output 2

SAFE

No pair of airplanes will collide.

Sample Input 3

8
168 224 U
130 175 R
111 198 D
121 188 L
201 116 U
112 121 R
145 239 D
185 107 L

Sample Output 3

100


program atcoder
implicit none
integer :: l , r , d , n
read *,l , r , d
if(l == 1)then
  print *, r
else if (mod(l,d) == 0)then
  n = int(r/d) - int(l/d) + 1
  print *,n
else
  n = int(r/d) - int(l/d)
  print *,n
end if
end program atcoder

Score : 100 points

Problem Statement

How many multiples of d are there among the integers between L and R (inclusive)?

Constraints

All values in input are integers.

1 \leq L \leq R \leq 100

1 \leq d \leq 100

Input

Input is given from Standard Input in the following format:

L R d

Output

Print the number of multiples of d among the integers between L and R (inclusive).

Sample Input 1

5 10 2

Sample Output 1

3

Among the integers between 5 and 10, there are three multiples of 2: 6, 8, and 10.

Sample Input 2

6 20 7

Sample Output 2

2

Among the integers between 6 and 20, there are two multiples of 7: 7 and 14.

Sample Input 3

1 100 1

Sample Output 3

100


program AIsing2020C
    implicit none
    integer(8)::N,i,j,k,A(10000),m
    read*,N
    A=0

    do i=1,100
        do j=1,100
            do k=1,100
                m=i**2+j**2+k**2+i*j+j*k+k*i
                if(m<=10000)A(m)=A(m)+1
            end do
        end do
    end do

    do i=1,N
        print'(i0)',A(i)
    end do
end program AIsing2020C

Score : 300 points

Problem Statement

Let f(n) be the number of triples of integers (x,y,z) that satisfy both of the following conditions:

1 \leq x,y,z

x^2 + y^2 + z^2 + xy + yz + zx = n

Given an integer N, find each of f(1),f(2),f(3),\ldots,f(N).

Constraints

All values in input are integers.

1 \leq N \leq 10^4

Input

Input is given from Standard Input in the following format:

N

Output

Print N lines. The i-th line should contain the value f(i).

Sample Input 1

20

Sample Output 1

0
0
0
0
0
1
0
0
0
0
3
0
0
0
0
0
3
3
0
0

For n=6, only (1,1,1) satisfies both of the conditions. Thus, f(6) = 1.

For n=11, three triples, (1,1,2), (1,2,1), and (2,1,1), satisfy both of the conditions. Thus, f(6) = 3.

For n=17, three triples, (1,2,2), (2,1,2), and (2,2,1), satisfy both of the conditions. Thus, f(17) = 3.

For n=18, three triples, (1,1,3), (1,3,1), and (3,1,1), satisfy both of the conditions. Thus, f(18) = 3.


program r20711c

implicit none
integer :: x, y, z, n, f, i
integer :: a(1:11000)

a = 0

read *, n
roop: do x = 1, 100
  do y = 1, x
   do z = 1, y
    f = x ** 2 + y ** 2 + z ** 2 + x * y + y * z + z * x
    if(f > 11000) then
     exit roop
    end if
    !print '(5(I2, 1x))', i, x, y, z, f
    if(x == y .and. y == z) then
     a(f) = a(f) + 1
    else if(x == y .or. y == z) then
     a(f) = a(f) + 3
    else
     a(f) = a(f) + 6
    end if
   end do
  end do
end do roop

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

end program r20711c


program tsundoku
  implicit none
  integer :: n, m, i,j, h = 0, l, r, p
  integer(8) :: k, t
  integer(8), dimension(0:200001) :: a = 0, b = 0,c=0, x = 0, y = 0
  read(*,*) n
    do t=1,n
    m=int(sqrt(dble(t/1.5)))
    p=0
	do i=1,m
    	do j=1,m
        	do k=1,m
            	if((i+j)*(i+j)+(i+k)*(i+k)+(k+j)*(k+j)==2*t)then
!                a(i)=i
!                b(i)=j
!                c(i)=k
                p=p+1
                end if
            end do
        end do
    end do
!   do i=1,m

!    if()
    write(*,*)p
    end do
end program tsundoku


program answer
  implicit none

  integer(8) :: N, i, j, k, count, eg(10000)
  read(*,*) N
  eg=0
  do i = 1, 100
     do j = 1, 100
        do k= 1, 100
           if(i**2+j**2+k**2+i*j+j*k+k*i<=10000) then
              eg(i**2+j**2+k**2+i*j+j*k+k*i)=eg(i**2+j**2+k**2+i*j+j*k+k*i)+1
           end if
        end do
     end do
  end do

  do i = 1, N
     write(*,*) eg(i)
  end do
  stop
 end program answer


program testcase
    integer(8) n
    integer(8) :: i= 0
    integer(8) :: c0 = 0
    integer(8) :: c1 = 0
    integer(8) :: c2 = 0
    integer(8) :: c3 = 0
    character(3) str
    read *, n
    do
        if(i == n)exit
        read *, str
        if(str .eq. "AC")then
            c0 = c0 + 1
        else if(str .eq. "WA")then
            c1 = c1 + 1
        else if(str .eq. "TLE")then
            c2 = c2 + 1
        else if(str .eq. "RE")then
            c3 = c3 + 1
        end if
        i = i + 1
    end do
    print "('AC x ',i0)", c0
    print "('WA x ',i0)", c1
    print "('TLE x ',i0)", c2
    print "('RE x ',i0)", c3
end program

Score : 200 points

Problem Statement

Takahashi is participating in a programming contest called AXC002, and he has just submitted his code to Problem A.

The problem has N test cases.

For each test case i (1\leq i \leq N), you are given a string S_i representing the verdict for that test case. Find the numbers of test cases for which the verdict is AC, WA, TLE, and RE, respectively.

See the Output section for the output format.

Constraints

1 \leq N \leq 10^5

S_i is AC, WA, TLE, or RE.

Input

Input is given from Standard Input in the following format:

N
S_1
\vdots
S_N

Output

Let C_0, C_1, C_2, and C_3 be the numbers of test cases for which the verdict is AC, WA, TLE, and RE, respectively. Print the following:

AC x C_0
WA x C_1
TLE x C_2
RE x C_3

Sample Input 1

6
AC
TLE
AC
AC
WA
TLE

Sample Output 1

AC x 3
WA x 1
TLE x 2
RE x 0

We have 3, 1, 2, and 0 test case(s) for which the verdict is AC, WA, TLE, and RE, respectively.

Sample Input 2

10
AC
AC
AC
AC
AC
AC
AC
AC
AC
AC

Sample Output 2

AC x 10
WA x 0
TLE x 0
RE x 0


program testcase
    integer(8) n
    integer(8) :: i= 0
    integer(8) :: c0 = 0
    integer(8) :: c1 = 0
    integer(8) :: c2 = 0
    integer(8) :: c3 = 0
    character(3) str
    read *, n
    do
        if(i == n)exit
        read *, str
        if(str .eq. "AC")then
            c0 = c0 + 1
        else if(str .eq. "WA")then
            c1 = c1 + 1
        else if(str .eq. "TLE")then
            c2 = c2 + 1
        else if(str .eq. "RE")then
            c3 = c3 + 1
        end if
        i = i + 1
    end do
    print "('AC × ',i0)", c0
    print "('WA × ',i0)", c1
    print "('TLE × ',i0)", c2
    print "('RE × ',i0)", c3
end program


program testcase
    integer n, i
    character str
    integer :: ac = 0
    integer :: tle = 0
    integer :: wa = 0
    integer :: re = 0
    character, allocatable :: s(:)
    read *, n
    allocate (s(n))
    do i = 1, n
        read *, str
        if(str == 'AC')then
            ac = ac + 1
        else if(str == 'WA')then
            wa = wa + 1
        else if(str == 'RE')then
            re = re + 1
        else if(str == 'TLE')then
            tle = tle + 1
        end if
    end do
    print *, 'AC ×', ac
    print *, 'WA ×', wa
    print *, 'RE ×', re
    print *, 'TLE ×', tle
end program


program r20718j

implicit none
integer :: ac, wa, tle, re, n, i
character(len=3) :: a

ac = 0
wa = 0
tle = 0
re = 0

read *, n

do i = 1, n

 read *, a

 if (trim(a) == 'AC') then
  ac = ac + 1
 else if (trim(a) == 'WA') then
  wa = wa + 1
 else if (trim(a) == 'TLE') then
  tle = tle + 1
 else
  re = re + 1
 end if
end do

print *, 'AC x ', ac
print *, 'WA x ', wa
print *, 'TLE x ', tle
print *, 'RE x ', re

end program r20718j


program sample
    implicit none
    character(3)::s
    integer(8) :: i,j,m,n,w,t,a,r
    integer(8),allocatable :: x(:),y(:)

    read(*,*) n
    w=0
    a=0
    r=0
    t=0
    do i=1,n
        read(*,*)s
        if (s .eq. 'WA')then
            w=w+1
        else if(s .eq. 'TLE')then
            t=t+1
        else if(s .eq. 'AC')then
            a=a+1
        else
            r=r+1
        end if
    end do
    write(*,'(a,x,a,x,i6)')'AC','*',a
    write(*,'(a,x,a,x,i6)')'WA','*',w
    write(*,'(a,x,a,x,i6)')'TLE','*',t
    write(*,'(a,x,a,x,i6)')'RE','*',r
    stop
end program sample


program main
	implicit none
	integer(8):: N,i,j1=0,j2=0,j3=0,j4=0,t,k
	character(200000):: S
	read *, N
	do i=1,N
	read *,S
	if(S=="AC")j1=1+j1
	if(S=="WA")j2=1+j2
	if(S=="TLE")j3=1+j3
	if(S=="RE")j4=1+j4
	end do
	write(*,*)"AC×",j1
	write(*,*)"WA×",j2
	write(*,*)"TLE×",j3
	write(*,*)"RE×",j4
	end program main


program h_and_v
  implicit none
  integer :: h, w, k, i, j, l, u, a(6, 6) = 0, b(6, 6) = 0, m = 0
  character(6) :: s
  read(*,*) h, w, k
  do i = 1, h
    read(*,*) s
    do j = 1, w
      if (s(j:j) == '#') a(i, j) = 1
    end do
  end do
  u = lshift(1, h + w) - 1
  do l = 0, u
    b = a
    do i = 1, h
      if (btest(l, i - 1)) b(i, :) = -1
    end do
    do j = 1, w
      if (btest(l, j + h - 1)) b(:, j) = -1
    end do
    if (sum(b, b > 0) == k) m = m + 1
  end do
  write(*,'(i0)') m
end program h_and_v

Score : 300 points

Problem Statement

We have a grid of H rows and W columns of squares. The color of the square at the i-th row from the top and the j-th column from the left (1 \leq i \leq H, 1 \leq j \leq W) is given to you as a character c_{i,j}: the square is white if c_{i,j} is ., and black if c_{i,j} is #.

Consider doing the following operation:

Choose some number of rows (possibly zero), and some number of columns (possibly zero). Then, paint red all squares in the chosen rows and all squares in the chosen columns.

You are given a positive integer K. How many choices of rows and columns result in exactly K black squares remaining after the operation? Here, we consider two choices different when there is a row or column chosen in only one of those choices.

Constraints

1 \leq H, W \leq 6

1 \leq K \leq HW

c_{i,j} is . or #.

Input

Input is given from Standard Input in the following format:

H W K
c_{1,1}c_{1,2}...c_{1,W}
c_{2,1}c_{2,2}...c_{2,W}
:
c_{H,1}c_{H,2}...c_{H,W}

Output

Print an integer representing the number of choices of rows and columns satisfying the condition.

Sample Input 1

2 3 2
..#
###

Sample Output 1

5

Five choices below satisfy the condition.

The 1-st row and 1-st column

The 1-st row and 2-nd column

The 1-st row and 3-rd column

The 1-st and 2-nd column

The 3-rd column

Sample Input 2

2 3 4
..#
###

Sample Output 2

1

One choice, which is choosing nothing, satisfies the condition.

Sample Input 3

2 2 3
##
##

Sample Output 3

0

Sample Input 4

6 6 8
..##..
.#..#.
#....#
######
#....#
#....#

Sample Output 4

208


module module_sort
    use iso_fortran_env
    implicit none

    interface quicksort
        module procedure :: int32_qsort
        module procedure :: int64_qsort
    end interface quicksort
contains

    recursive function int32_qsort(m) result(res)
        integer(int32), intent(in) :: m(:)
        integer(int32) :: res(size(m))

        if (size(m) <= 1) then; res = m
        else; res = [int32_qsort( pack(m(2:), m(2:) >= m(1)) ), m(1), int32_qsort( pack(m(2:), m(2:) < m(1)) )]
        end if

    end function int32_qsort

    recursive function int64_qsort(m) result(res)
        integer(int64), intent(in) :: m(:)
        integer(int64) :: res(size(m))

        if (size(m) <= 1) then; res = m
        else; res = [int64_qsort( pack(m(2:), m(2:) >= m(1)) ), m(1), int64_qsort( pack(m(2:), m(2:) < m(1)) )]
        end if

    end function int64_qsort

end module module_sort

program main
    use module_sort
    use iso_fortran_env
    implicit none
    integer(int32) :: n
    integer(int32), allocatable :: ai(:)
    integer(int32) :: i, ie
    integer(int32) :: ans

    read *, n
    allocate( ai(n) )
    read *, ai( 1:n )
    ai = quicksort( ai )
!    call qsort( ai )
!    ai(n:1:-1) = ai(1:n)
!    ai = intQsort( ai )
!    ai(n:1:-1) = ai(1:n)

    if ( mod((n-1), 2) == 0 ) then
        ie = (n-1) / 2
        ans = ai(1) + ai(ie + 1)
    else
        ie = n / 2
        ans = ai(1)
    endif

    do i = 2, ie
        ans = ans + ai(i) * 2
    enddo

    print *, ans

contains

recursive subroutine qsort(a)
  implicit none
  integer(8),intent(inout) :: a(:)
  integer(8)::i,j,toobig,toosmall,pv,n

  n = size(a)
i = 1
j = n
pv = a((i+j)/2)
do
   do
     if (a(i) < pv) then
       i = i+1
     else
       exit
     end if
   end do
   do
     if(a(j) > pv) then
       j = j-1
     else
       exit
     end if
   end do
   if (i>=j) then
     exit
   else
     toobig = a(j)
     toosmall = a(i)
     a(i) = toobig
     a(j) = toosmall
     i = i+1
     j = j-1
   end if
 end do
 if (i > 2) then
   call qsort(a(1:i-1))
 end if
 if (j < n-1) then
   call qsort(a(j+1:n))
 end if
end subroutine qsort

pure recursive function intQsort( a ) result( r )
implicit none
integer(16), intent(in) :: a(:)
integer(16) :: r( size(a) )
integer(16) :: sample(3), len, i,j

len = size( a(:) )
if( len<=1 )then
  r = a
else
  !select pivot
  sample(1:3) = [a(1), a(len/2), a(len)]
  do i = 1,2
    do j = 2,3
      if( sample(i)>sample(j) )then
        len = sample(i) !len is buffer
        sample(i) = sample(j)
        sample(j) = len
      end if
    end do
  end do

  r = [ intQsort( pack(a(:), a(:)< sample(2)) ), &
                  pack(a(:), a(:)==sample(2))  , &
        intQsort( pack(a(:), a(:)> sample(2)) ) ]
end if
end function intQsort

end program main

Score: 400 points

Problem Statement

Quickly after finishing the tutorial of the online game ATChat, you have decided to visit a particular place with N-1 players who happen to be there. These N players, including you, are numbered 1 through N, and the friendliness of Player i is A_i.

The N players will arrive at the place one by one in some order. To make sure nobody gets lost, you have set the following rule: players who have already arrived there should form a circle, and a player who has just arrived there should cut into the circle somewhere.

When each player, except the first one to arrive, arrives at the place, the player gets comfort equal to the smaller of the friendliness of the clockwise adjacent player and that of the counter-clockwise adjacent player. The first player to arrive there gets the comfort of 0.

What is the maximum total comfort the N players can get by optimally choosing the order of arrivals and the positions in the circle to cut into?

Constraints

All values in input are integers.

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

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \dots A_N

Output

Print the maximum total comfort the N players can get.

Sample Input 1

4
2 2 1 3

Sample Output 1

7

By arriving at the place in the order Player 4, 2, 1, 3, and cutting into the circle as shown in the figure, they can get the total comfort of 7.

They cannot get the total comfort greater than 7, so the answer is 7.

Sample Input 2

7
1 1 1 1 1 1 1

Sample Output 2

6


program sample
    implicit none

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

    read(*,*) n
    allocate(x(n))
    read(*,*)x
    call msort(x)
    m=x(n)
    if (mod(n,2)==0)then
        do i=n/2+1,n-1
            m=m+x(i)*2
        end do
    else
        m=m+x(n/2+1)
        do i=n/2+2,n-1
            m=m+x(i)*2
        end do
    end if

    write(*,*)m
    stop
    contains
    recursive subroutine msort(x)
    implicit none
    integer(8), intent(inout) :: x(:)

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

    n = size(x)

    if(n == 1) then
      return
    end if

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

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

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

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

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

    deallocate(tmp)
    return
  end subroutine msort
end program sample


program main
  implicit none
  integer(8) i, ans, frag, r, N
  integer(8),allocatable :: A(:)

  read(*, *) N
  allocate(A(N))
  read(*, *) (A(i), i = 1,N)
  call qsort(A)
  ans = 0
  r = N
  frag = 1
  do i = 1,N-1
    ans = ans + A(r)
    if (frag == 1) then
      frag = 0
      r = r-1
    else
      frag = 1
    end if
  end do

  write(*, *) ans

contains

recursive subroutine qsort(a)
implicit none
!integer(8),intent(in) :: n
integer(8),intent(inout) :: a(:)
integer(8)::i,j,toobig,toosmall,pv,n
n = size(a)
i = 1
j = n
pv = a((i+j)/2)
do
   do
     if (a(i) < pv) then
       i = i+1
     else
       exit
     end if
   end do
   do
     if(a(j) > pv) then
       j = j-1
     else
       exit
     end if
   end do
   if (i>=j) then
     exit
   else
     toobig = a(j)
     toosmall = a(i)
     a(i) = toobig
     a(j) = toosmall
     i = i+1
     j = j-1
   end if
 end do
 if (i > 2) then
   call qsort(a(1:i-1))
 end if
 if (j < n-1) then
   call qsort(a(j+1:n))
 end if
end subroutine qsort
end program main


program multiplication_4
  implicit none
  integer(8), parameter :: m = 1000000007
  integer :: n, k, i, j, cnt = 0
  integer(8) :: a(200000, 2) = 0, nega = -1, posi = -1, x = 1, y = -1, z = -1
  real(8) :: nn = -1.d0, pp = -1.d0
  read(*,*) n, k
  read(*,*) a(1:n, 1)
  if (n == k) then
    do i = 1, n
      x = modulo(x * a(i, 1), m)
    end do
    write(*,'(i0)') x
    stop
  end if
  call merge_sort(a(1:n, :))
  if (a(1, 1) < 0 .and. mod(k, 2) == 1) then
    do i = 1, k
      x = modulo(x * a(i, 1), m)
    end do
    write(*,'(i0)') x
    stop
  end if
  do i = 1, n
    if (a(i, 1) >= 0) then
      a(i, 2) = 0
    else
      a(i, 1) = -a(i, 1)
      a(i, 2) = 1
    end if
  end do
  call merge_sort(a(1:n, :))
  do i = 1, k
    x = mod(x * a(i, 1), m)
    if (a(i, 2) == 1) then
      cnt = cnt + 1
      nega = a(i, 1)
    else
      posi = a(i, 1)
    end if
  end do
  if (x == 0 .or. mod(cnt, 2) == 0) then
    write(*,'(i0)') x
    stop
  end if
  if (posi /= -1) then
    do i = k + 1, n
      if (a(i, 2) == 1) then
        y = mod(x * a(i, 1), m)
        y = mod(y * inv(posi), m)
        pp = a(i, 1) / real(posi, 8)
        exit
      end if
    end do
  end if
  if (nega /= -1) then
    do i = k + 1, n
      if (a(i, 2) == 0) then
        z = mod(x * a(i, 1), m)
        z = mod(z * inv(nega), m)
        nn = a(i, 1) / real(nega, 8)
        exit
      end if
    end do
  end if
  write(*,'(i0)') merge(y, z, pp > nn)
contains
  subroutine merge_sort(a)
    integer(8), intent(inout) :: a(:, :)
    integer :: n, m, l, i, u
    n = size(a, 1)
    m = n
    l = 1
    do while (m > 1)
      do i = 1, m / 2
        u = min(2 * i * l, n)
        call merger(a(2 * (i - 1) * l + 1:(2 * i - 1) * l, :), a((2 * i - 1) * l + 1:u, :))
      end do
      l = 2 * l
      m = (m + 1) / 2
    end do
  end
  subroutine merger(a1, a2)
    integer(8), intent(inout) :: a1(:, :), a2(:, :)
    integer(8) :: a(size(a1, 1) + size(a2, 1), size(a1, 2))
    integer :: i1, i2, n1, n2
    i1 = 1
    i2 = 1
    n1 = size(a1, 1)
    n2 = size(a2, 1)
    do while (i1 <= n1 .and. i2 <= n2)
      if (a1(i1, 1) > a2(i2, 1) .or. &
      (a1(i1, 1) == a2(i2, 1) .and. a1(i1, 2) <= a2(i2, 2))) then
        a(i1 + i2 - 1, :) = a1(i1, :)
        i1 = i1 + 1
      else
        a(i1 + i2 - 1, :) = a2(i2, :)
        i2 = i2 + 1
       end if
    end do
    if (i1 <= n1) then
      a(i1 + i2 - 1:n1 + n2, :) = a1(i1:n1, :)
    else
      a(i1 + i2 - 1:n1 + n2, :) = a2(i2:n2, :)
    end if
    a1 = a(1:n1, :)
    a2 = a(n1 + 1:n1 + n2, :)
  end
  pure elemental integer(8) function inv(v) result(res)
    integer(8), intent(in) :: v
    integer(8) :: a, b, c, x, y, z
    a = v
    b = m
    c = 0
    res = 1
    do while (b /= 0)
      x = b
      y = mod(a, b)
      z = res - a / b * c
      res = c
      a = x
      b = y
      c = z
    end do
    res = modulo(res, m)
  end
end program multiplication_4

Score : 500 points

Problem Statement

Given are N integers A_1,\ldots,A_N.

We will choose exactly K of these elements. Find the maximum possible product of the chosen elements.

Then, print the maximum product modulo (10^9+7), using an integer between 0 and 10^9+6 (inclusive).

Constraints

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

|A_i| \leq 10^9

Input

Input is given from Standard Input in the following format:

N K
A_1 \ldots A_N

Output

Print the maximum product modulo (10^9+7), using an integer between 0 and 10^9+6 (inclusive).

Sample Input 1

4 2
1 2 -3 -4

Sample Output 1

12

The possible products of the two chosen elements are 2, -3, -4, -6, -8, and 12, so the maximum product is 12.

Sample Input 2

4 3
-1 -2 -3 -4

Sample Output 2

1000000001

The possible products of the three chosen elements are -24, -12, -8, and -6, so the maximum product is -6.

We print this value modulo (10^9+7), that is, 1000000001.

Sample Input 3

2 1
-1 1000000000

Sample Output 3

1000000000

The possible products of the one chosen element are -1 and 1000000000, so the maximum product is 1000000000.

Sample Input 4

10 10
1000000000 100000000 10000000 1000000 100000 10000 1000 100 10 1

Sample Output 4

999983200

Be sure to print the product modulo (10^9+7).


program multiplication_4
  implicit none
  integer(8), parameter :: m = 1000000007
  integer :: n, k, i, j, cnt = 0
  integer(8) :: a(200000, 2) = 0, nega = -1, posi = -1, x = 1, y = -1, z = -1
  real(8) :: nn = -1.d0, pp = -1.d0
  read(*,*) n, k
  read(*,*) a(1:n, 1)
  if (n == k) then
    do i = 1, n
      x = modulo(x * a(i, 1), m)
    end do
    write(*,'(i0)') x
    stop
  end if
  call merge_sort(a(1:n, :))
  if (a(1, 1) < 0) then
    if (mod(k, 2) == 0) then
      do i = 1, k
        x = modulo(x * a(n - i + 1, 1), m)
      end do
    else
      do i = 1, k
        x = modulo(x * a(i, 1), m)
      end do
    end if
    write(*,'(i0)') x
    stop
  end if
  do i = 1, n
    if (a(i, 1) >= 0) then
      a(i, 2) = 0
    else
      a(i, 1) = -a(i, 1)
      a(i, 2) = 1
    end if
  end do
  call merge_sort(a(1:n, :))
  do i = 1, k
    x = mod(x * a(i, 1), m)
    if (a(i, 2) == 1) then
      cnt = cnt + 1
      nega = a(i, 1)
    else
      posi = a(i, 1)
    end if
  end do
  if (x == 0 .or. mod(cnt, 2) == 0) then
    write(*,'(i0)') x
    stop
  end if
  if (posi /= -1) then
    do i = k + 1, n
      if (a(i, 2) == 1) then
        y = mod(x * a(i, 1), m)
        y = mod(y * inv(posi), m)
        pp = a(i, 1) / real(posi, 8)
        exit
      end if
    end do
  end if
  if (nega /= -1) then
    do i = k + 1, n
      if (a(i, 2) == 0) then
        z = mod(x * a(i, 1), m)
        z = mod(z * inv(nega), m)
        nn = a(i, 1) / real(nega, 8)
        exit
      end if
    end do
  end if
  write(*,'(i0)') merge(y, z, pp > nn)
contains
  subroutine merge_sort(a)
    integer(8), intent(inout) :: a(:, :)
    integer :: n, m, l, i, u
    n = size(a, 1)
    m = n
    l = 1
    do while (m > 1)
      do i = 1, m / 2
        u = min(2 * i * l, n)
        call merger(a(2 * (i - 1) * l + 1:(2 * i - 1) * l, :), a((2 * i - 1) * l + 1:u, :))
      end do
      l = 2 * l
      m = (m + 1) / 2
    end do
  end
  subroutine merger(a1, a2)
    integer(8), intent(inout) :: a1(:, :), a2(:, :)
    integer(8) :: a(size(a1, 1) + size(a2, 1), size(a1, 2))
    integer :: i1, i2, n1, n2
    i1 = 1
    i2 = 1
    n1 = size(a1, 1)
    n2 = size(a2, 1)
    do while (i1 <= n1 .and. i2 <= n2)
      if (a1(i1, 1) > a2(i2, 1) .or. &
      (a1(i1, 1) == a2(i2, 1) .and. a1(i1, 2) <= a2(i2, 2))) then
        a(i1 + i2 - 1, :) = a1(i1, :)
        i1 = i1 + 1
      else
        a(i1 + i2 - 1, :) = a2(i2, :)
        i2 = i2 + 1
       end if
    end do
    if (i1 <= n1) then
      a(i1 + i2 - 1:n1 + n2, :) = a1(i1:n1, :)
    else
      a(i1 + i2 - 1:n1 + n2, :) = a2(i2:n2, :)
    end if
    a1 = a(1:n1, :)
    a2 = a(n1 + 1:n1 + n2, :)
  end
  pure elemental integer(8) function inv(v) result(res)
    integer(8), intent(in) :: v
    integer(8) :: a, b, c, x, y, z
    a = v
    b = m
    c = 0
    res = 1
    do while (b /= 0)
      x = b
      y = mod(a, b)
      z = res - a / b * c
      res = c
      a = x
      b = y
      c = z
    end do
    res = modulo(res, m)
  end
end program multiplication_4


program incremental_scoring
  implicit none
  integer :: d, c(26) = 0, s(26, 365) = 0, t(365) = 0, m, p, q, i
  read(*,*) d
  read(*,*) c
  read(*,*) (s(:, i), i = 1, d)
  read(*,*) (t(i), i = 1, d)
  read(*,*) m
  do i = 1, m
    read(*,*) p, q
    t(p) = q
    write(*,'(i0)') score()
  end do
contains
  integer function score() result(res)
    integer :: last(26), i
    last = 0
    res = 0
    do i = 1, d
      last(t(i)) = i
      res = res + s(t(i), i) - sum(c * (i - last))
    end do
  end
end program incremental_scoring

(Please read problem A first. The maximum score you can get by solving this problem C is 1, which will have almost no effect on your ranking.)

Beginner's Guide

"Local search" is a powerful method for finding a high-quality solution.
In this method, instead of constructing a solution from scratch, we try to find a better solution by slightly modifying the already found solution.
If the solution gets better, update it, and if it gets worse, restore it.
By repeating this process, the quality of the solution is gradually improved over time.
The pseudo-code is as follows.

solution = compute an initial solution (by random generation, or by applying other methods such as greedy)
while the remaining time > 0:
slightly modify the solution (randomly)
if the solution gets worse:
restore the solution

For example, in this problem, we can use the following modification: pick the date d and contest type q at random and change the type of contest to be held on day d to q.
The pseudo-code is as follows.

t[1..D] = compute an initial solution (by random generation, or by applying other methods such as greedy)
while the remaining time > 0:
pick d and q at random
old = t[d] # Remember the original value so that we can restore it later
t[d] = q
if the solution gets worse:
t[d] = old

The most important thing when using the local search method is the design of how to modify solutions.

If the amount of modification is too small, we will soon fall into a dead-end (local optimum) and, conversely, if the amount of modification is too large, the probability of finding an improving move becomes extremely small.

In order to increase the number of iterations, it is desirable to be able to quickly calculate the score after applying a modification.

In this problem C, we focus on the second point.
The score after the modification can, of course, be obtained by calculating the score from scratch.
However, by focusing on only the parts that have been modified, it may be possible to quickly compute the difference between the scores before and after the modification.
From another viewpoint, the impossibility of such a fast incremental calculation implies that a small modification to the solution affects a majority of the score calculation.
In such a case, we may need to redesign how to modify solutions, or there is a high possibility that the problem is not suitable for local search.
Let's implement fast incremental score computation.
It's time to demonstrate the skills of algorithms and data structures you have developed in ABC and ARC!

In this kind of contest, where the objective is to find a better solution instead of the optimal one, a bug in a program does not result in a wrong answer, which may delay the discovery of the bug.
For early detection of bugs, it is a good idea to unit test functions you implemented complicated routines.
For example, if you implement fast incremental score calculation, it is a good idea to test that the scores computed by the fast implementation match the scores computed from scratch, as we will do in this problem C.

Problem Statement

You will be given a contest schedule for D days and M queries of schedule modification.
In the i-th query, given integers d_i and q_i, change the type of contest to be held on day d_i to q_i, and then output the final satisfaction at the end of day D on the updated schedule.
Note that we do not revert each query. That is, the i-th query is applied to the new schedule obtained by the (i-1)-th query.

Input

Input is given from Standard Input in the form of the input of Problem A followed by the output of Problem A and the queries.

D
c_1 c_2 \cdots c_{26}
s_{1,1} s_{1,2} \cdots s_{1,26}
\vdots
s_{D,1} s_{D,2} \cdots s_{D,26}
t_1
t_2
\vdots
t_D
M
d_1 q_1
d_2 q_2
\vdots
d_M q_M

The constraints and generation methods for the input part are the same as those for Problem A.

For each d=1,\ldots,D, t_d is an integer generated independently and uniformly at random from {1,2,\ldots,26}.

The number of queries M is an integer satisfying 1\leq M\leq 10^5.

For each i=1,\ldots,M, d_i is an integer generated independently and uniformly at random from {1,2,\ldots,D}.

For each i=1,\ldots,26, q_i is an integer satisfying 1\leq q_i\leq 26 generated uniformly at random from the 25 values that differ from the type of contest on day d_i.

Output

Let v_i be the final satisfaction at the end of day D on the schedule after applying the i-th query.
Print M integers v_i to Standard Output in the following format:

v_1
v_2
\vdots
v_M

Sample Input 1

5
86 90 69 51 2 96 71 47 88 34 45 46 89 34 31 38 97 84 41 80 14 4 50 83 7 82
19771 12979 18912 10432 10544 12928 13403 3047 10527 9740 8100 92 2856 14730 1396 15905 6534 4650 11469 3628 8433 2994 10899 16396 18355 11424
6674 17707 13855 16407 12232 2886 11908 1705 5000 1537 10440 10711 4917 10770 17272 15364 19277 18094 3929 3705 7169 6159 18683 15410 9092 4570
6878 4239 19925 1799 375 9563 3445 5658 19857 11401 6997 6498 19933 3848 2426 2146 19745 16880 17773 18359 3921 14172 16730 11157 5439 256
8633 15862 15303 10749 18499 7792 10317 5901 9395 11433 3514 3959 5202 19850 19469 9790 5653 784 18500 10552 17975 16615 7852 197 8471 7452
19855 17918 7990 10572 4333 438 9140 9104 12622 4985 12319 4028 19922 12132 16259 17476 2976 547 19195 19830 16285 4806 4471 9457 2864 2192
1
17
13
14
13
5
1 7
4 11
3 4
5 24
4 19

Sample Output 1

72882
56634
38425
27930
42884

Note that this example is a small one for checking the problem specification. It does not satisfy the constraint D=365 and is never actually given as a test case.

Next Step

Let's go back to Problem A and implement the local search algorithm by utilizing the incremental score calculator you just implemented!
For this problem, the current modification "pick the date d and contest type q at random and change the type of contest to be held on day d to q" is actually not so good. By considering why it is not good, let's improve the modification operation.
One of the most powerful and widely used variant of the local search method is "Simulated Annealing (SA)", which makes it easier to reach a better solution by stochastically accepting worsening moves.
For more information about SA and other local search techniques, please refer to the editorial that will be published after the contest.


program calc
  implicit none
  integer :: a
  read(*,*) a
  write(*,'(i0)') ((a + 1) * a + 1) * a
end program calc

Score : 100 points

Problem Statement

Given an integer a as input, print the value a + a^2 + a^3.

Constraints

1 \leq a \leq 10

a is an integer.

Input

Input is given from Standard Input in the following format:

a

Output

Print the value a + a^2 + a^3 as an integer.

Sample Input 1

2

Sample Output 1

14

When a = 2, we have a + a^2 + a^3 = 2 + 2^2 + 2^3 = 2 + 4 + 8 = 14.

Print the answer as an input. Outputs such as 14.0 will be judged as incorrect.

Sample Input 2

10

Sample Output 2

1110


program minor

 implicit none
 character::s*200000,t*200000,ss*200000,tt*200000
 integer::length,i,ans

 read*,s,t
 length=len_trim(s)
 !print*,length
 !ss=s(1:length)
 !tt=t(1:length)
 !print*,s,t

 ans=0
 do i=1,length
  if(s(i:i)/=t(i:i)) then
   ans=ans+1
  end if
 end do

 print*,ans

end program minor

Score : 200 points

Problem Statement

Given are strings S and T. Consider changing S to T by repeating the operation below. Find the minimum number of operations required to do so.

Operation: Choose one character of S and replace it with a different character.

Constraints

S and T have lengths between 1 and 2\times 10^5 (inclusive).

S and T consists of lowercase English letters.

S and T have equal lengths.

Input

Input is given from Standard Input in the following format:

S
T

Output

Print the answer.

Sample Input 1

cupofcoffee
cupofhottea

Sample Output 1

4

We can achieve the objective in four operations, such as the following:

First, replace the sixth character c with h.

Second, replace the eighth character f with t.

Third, replace the ninth character f with t.

Fourth, replace the eleventh character e with a.

Sample Input 2

abcde
bcdea

Sample Output 2

5

Sample Input 3

apple
apple

Sample Output 3

0

No operations may be needed to achieve the objective.


program main
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: ls, i, cnt=0
    character(200000):: s,t

    read*, s
    read*, t

    do i=1,len_trim(s)
        if (s(i:i) /= t(i:i)) cnt=cnt+1
    end do
    print'(i0)', cnt
end program main


program main
    use iso_fortran_env
    implicit none
    integer(int32) :: n, i
    integer(int64), allocatable :: a(:)
    integer(int64) :: s, x, d, ans
    logical :: bx, bd

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

    x = 0
    d = 0
    s = a(1) + a(2)
    ans = 0

    if ( n>2 ) then
        do i = 3, n
            x = ieor(x, a(i))
        enddo
    endif

    if ( trailz(s-x) >= 1 ) then
        d = (s - x) / 2
    else
        print *, "-1"
        stop
    endif

    if ( iand(x, d) /= 0) then
        print *, "-1"
        stop
    endif

    do i = 0, 63
        bd = btest(d, i)
        if (bd) then
            ans = ibset(ans, i)
        endif
    enddo

    do i = 63, 0, -1
        bx = btest(x, i)
        if (bx) then
            if ( ibset(ans, i) <= a(1) ) then
                ans = ibset(ans, i)
            endif
        endif
    enddo

    if (ans == 0) then
        print *, "-1"
    else
        print *, a(1) - ans
    endif

end program main

Score : 600 points

Problem Statement

There are N piles of stones. The i-th pile has A_i stones.

Aoki and Takahashi are about to use them to play the following game:

Starting with Aoki, the two players alternately do the following operation:

Operation: Choose one pile of stones, and remove one or more stones from it.

When a player is unable to do the operation, he loses, and the other player wins.

When the two players play optimally, there are two possibilities in this game: the player who moves first always wins, or the player who moves second always wins, only depending on the initial number of stones in each pile.

In such a situation, Takahashi, the second player to act, is trying to guarantee his win by moving at least zero and at most (A_1 - 1) stones from the 1-st pile to the 2-nd pile before the game begins.

If this is possible, print the minimum number of stones to move to guarantee his victory; otherwise, print -1 instead.

Constraints

2 \leq N \leq 300

1 \leq A_i \leq 10^{12}

Input

Input is given from Standard Input in the following format:

N
A_1 \ldots A_N

Output

Print the minimum number of stones to move to guarantee Takahashi's win; otherwise, print -1 instead.

Sample Input 1

2
5 3

Sample Output 1

1

Without moving stones, if Aoki first removes 2 stones from the 1-st pile, Takahashi cannot win in any way.

If Takahashi moves 1 stone from the 1-st pile to the 2-nd before the game begins so that both piles have 4 stones, Takahashi can always win by properly choosing his actions.

Sample Input 2

2
3 5

Sample Output 2

-1

It is not allowed to move stones from the 2-nd pile to the 1-st.

Sample Input 3

3
1 1 2

Sample Output 3

-1

It is not allowed to move all stones from the 1-st pile.

Sample Input 4

8
10 9 8 7 6 5 4 3

Sample Output 4

3

Sample Input 5

3
4294967297 8589934593 12884901890

Sample Output 5

1

Watch out for overflows.


program alphabet
    implicit none
    character alpha
    read *, alpha
    if(alpha >= 'a' .and. alpha <= 'z')then
        print *, 'a'
    else if(alpha >= 'A' .and. alpha <= 'Z')then
        print *, 'A'
    end if
end program

Score : 100 points

Problem Statement

An uppercase or lowercase English letter \alpha will be given as input.
If \alpha is uppercase, print A; if it is lowercase, print a.

Constraints

\alpha is an uppercase (A - Z) or lowercase (a - z) English letter.

Input

Input is given from Standard Input in the following format:

α

Output

If \alpha is uppercase, print A; if it is lowercase, print a.

Sample Input 1

B

Sample Output 1

A

B is uppercase, so we should print A.

Sample Input 2

a

Sample Output 2

a

a is lowercase, so we should print a.


character :: alpha

read*,alpha

if( ichar(alpha)<ichar('a') )then
  print'(a)','A'
else
  print'(A)','a'
end if

end program


character :: alpha

read*,alpha

if( ichar('a')<=ichar(alpha) .and. ichar(alpha)<ichar('A') )then
  print'(a)','a'
else
  print'(A)','A'
end if
end program


program ABC171A
    implicit none
    character(1)::a
    read*,a
    if(ichar(a)<=90.and.ichar(a)>=65)print'(A)',"A"
    if(ichar(a)<=122.and.ichar(a)>=97)print'(A)',"a"
end program ABC171A


program sample
    implicit none

    integer(8) :: i,j,m,n,k,l,q,t(100000)

    integer(8),allocatable :: a(:),b(:),c(:)

    read(*,*) n
    allocate(a(n))
    read(*,*)a
    read(*,*)q
    allocate(b(q))
    allocate(c(q))
    do i=1,q
        read(*,*)b(i),c(i)
    end do
    do i=1,10000
        t(i)=0
    end do
    do i=1,n
        m=a(i)
        t(m)=t(m)+1
    end do
    m=sum(a)

    do i=1,q
        k=b(i)
        l=c(i)
        m=m+(l-k)*t(k)
        t(l)=t(l)+t(k)
        t(k)=0

        write(*,*)m
    end do

    stop
end program sample


program moji
character(1)::a
k=0
read*,a
 if(a=='A')then
 print*,'A'
 k=1
 end if
  if(a=='B')then
 print*,'A'
  k=1
 end if
  if(a=='C')then
 print*,'A'
  k=1
 end if
  if(a=='D')then
 print*,'A'
  k=1
 end if
  if(a=='E')then
 print*,'A'
  k=1
 end if
  if(a=='F')then
 print*,'A'
  k=1
 end if
  if(a=='G')then
 print*,'A'
  k=1
 end if
  if(a=='H')then
 print*,'A'
  k=1
 end if
  if(a=='I')then
 print*,'A'
  k=1
 end if
  if(a=='J')then
 print*,'A'
  k=1
 end if
  if(a=='K')then
 print*,'A'
  k=1
 end if
  if(a=='L')then
 print*,'A'
  k=1
 end if
 if(a=='M')then
 print*,'A'
  k=1
 end if
  if(a=='N')then
 print*,'A'
  k=1
 end if
  if(a=='O')then
 print*,'A'
  k=1
 end if
  if(a=='P')then
 print*,'A'
  k=1
 end if
  if(a=='Q')then
 print*,'A'
  k=1
 end if
   if(a=='R')then
 print*,'A'
  k=1
 end if
   if(a=='S')then
 print*,'A'
  k=1
 end if
   if(a=='T')then
 print*,'A'
  k=1
 end if
   if(a=='U')then
 print*,'A'
  k=1
 end if
    if(a=='V')then
 print*,'A'
  k=1
 end if
    if(a=='W')then
 print*,'A'
  k=1
 end if
    if(a=='X')then
 print*,'A'
  k=1
 end if
    if(a=='Y')then
 print*,'A'
  k=1
 end if
    if(a=='Z')then
 print*,'A'
  k=1
 end if

 IF(K==0)THEN
 PRINT*,'a'
 end if
end program moji


program alphabet
	implicit none
    integer:: i
    character(len=1)::x
    character,parameter :: large='ABCDEFGHIJKLMNOPQRSTUVWXYZ'

	read(*,'(a)') x

    do i = 1, 26
    	if ( x==large(i:i) ) then
    		write(*,'(a)') 'A'
    	    stop
        end if
    end do

    write(*,'(a)') 'a'

    stop
end program alphabet


program alphabet

 character::yomu*1

 read*,yomu

 if(yomu>='a' .and. yomu<='z') then
  print*,'a'
 end if

 if(yomu>='A' .and. yomu<='Z') then
  print*,'A'
 end if

end program alphabet


program b171

implicit none
integer :: n, k, z, m, i, j
integer, allocatable :: p(:)

read *, n, k

allocate(p(1:n))

read *, p(:)

do j = 1, n-1
 do i = 1, n-1
  if(p(i) > p(i+1)) then
   m = p(i)
   p(i) = p(i+1)
   p(i+1) = m
  end if
 end do
end do

z = 0

do i = 1, k
 z = z + p(i)
end do

print *, z

end program b171

Score : 200 points

Problem Statement

A shop sells N kinds of fruits, Fruit 1, \ldots, N, at prices of p_1, \ldots, p_N yen per item, respectively. (Yen is the currency of Japan.)

Here, we will choose K kinds of fruits and buy one of each chosen kind. Find the minimum possible total price of those fruits.

Constraints

1 \leq K \leq N \leq 1000

1 \leq p_i \leq 1000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
p_1 p_2 \ldots p_N

Output

Print an integer representing the minimum possible total price of fruits.

Sample Input 1

5 3
50 100 80 120 80

Sample Output 1

210

This shop sells Fruit 1, 2, 3, 4, and 5 for 50 yen, 100 yen, 80 yen, 120 yen, and 80 yen, respectively.

The minimum total price for three kinds of fruits is 50 + 80 + 80 = 210 yen when choosing Fruit 1, 3, and 5.

Sample Input 2

1 1
1000

Sample Output 2

1000


program juice
	implicit none
    integer :: n, k, i, m, j
    integer,allocatable :: p(:)

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

    read(*,*) p

    do i = n, 2, -1
    	do j = 1, i-1
        	if ( p(j)>=p(j+1) ) then
            	m = p(j+1)
                p(j+1) = p(j)
                p(j) = m
            end if
        end do
    end do

    write(*,*) sum(p(1:k))

    stop
end program juice


program main
    use iso_fortran_env
    implicit none

    integer(int64) :: n, m
    character(:),allocatable :: txt
    character(26) :: alp

    alp = "abcdefghijklmnopqrstuvwxyz"

    read(*,*) n
    txt = ""

    do
        if (n <= 0) exit
        m = mod(n-1, 26)
        n = (n-1)/26
        txt = alp(m+1:m+1)//txt
    enddo

    print'(a)', trim(txt)

end program main

Score : 300 points

Problem Statement

1000000000000001 dogs suddenly appeared under the roof of Roger's house, all of which he decided to keep. The dogs had been numbered 1 through 1000000000000001, but he gave them new names, as follows:

the dogs numbered 1,2,\cdots,26 were respectively given the names a, b, ..., z;

the dogs numbered 27,28,29,\cdots,701,702 were respectively given the names aa, ab, ac, ..., zy, zz;

the dogs numbered 703,704,705,\cdots,18277,18278 were respectively given the names aaa, aab, aac, ..., zzy, zzz;

the dogs numbered 18279,18280,18281,\cdots,475253,475254 were respectively given the names aaaa, aaab, aaac, ..., zzzy, zzzz;

the dogs numbered 475255,475256,\cdots were respectively given the names aaaaa, aaaab, ...;

and so on.

To sum it up, the dogs numbered 1, 2, \cdots were respectively given the following names:

a, b, ..., z, aa, ab, ..., az, ba, bb, ..., bz, ..., za, zb, ..., zz, aaa, aab, ..., aaz, aba, abb, ..., abz, ..., zzz, aaaa, ...

Now, Roger asks you:

"What is the name for the dog numbered N?"

Constraints

N is an integer.

1 \leq N \leq 1000000000000001

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer to Roger's question as a string consisting of lowercase English letters.

Sample Input 1

2

Sample Output 1

b

Sample Input 2

27

Sample Output 2

aa

Sample Input 3

123456789

Sample Output 3

jjddja


program main
    use iso_fortran_env
    implicit none

    integer(int64) :: n
    integer(int64) :: i, m, k, left, right
    character(1),allocatable :: name(:)
    character(26) :: alp
    alp = "zabcdefghijklmnopqrstuvwxy"

    read(*,*) n

    i = 0
    left = 0
    right = 0

    do
        left = left + (26**(i) * 1)
        right = right + (26**(i) * 26)
        if ( (left <= n).and.(n <= right) ) then
            exit
        endif
        i = i + 1
!        print *, i, left, right
    enddo
    allocate(name(0:i))

    do k = 0, i
        m = mod(n, 26)
        name( i - k ) = alp(m+1:m+1)
        n = int( (n-1) / 26)
    enddo
    print *, name

end program main


program main
    use iso_fortran_env
    implicit none

    integer(int64) :: n
    integer(int32) :: i, m, k, left, right
    character(1),allocatable :: name(:)
    character(26) :: alp
    alp = "zabcdefghijklmnopqrstuvwxy"

    read(*,*) n

    i = 0
    left = 0
    right = 0

    do
        left = left + (26**(i) * 1)
        right = right + (26**(i) * 26)
        if ( (left <= n).and.(n <= right) ) then
            exit
        endif
        i = i + 1
    enddo

    allocate(name(0:i))

    do k = 0, i
        m = mod(n, 26)
        name( i - k ) = alp(m+1:m+1)
        n = int( (n-1) / 26)
    enddo
    print *, name

end program main


program main
    use iso_fortran_env
    implicit none

    integer(int32) :: n
    integer(int32) :: i, m, k, left, right
    character(1),allocatable :: name(:)
    character(26) :: alp
    alp = "zabcdefghijklmnopqrstuvwxy"

    read(*,*) n

    i = 0
    left = 0
    right = 0

    do
        left = left + (26**(i) * 1)
        right = right + (26**(i) * 26)
        if ( (left <= n).and.(n <= right) ) then
            exit
        endif
        i = i + 1
    enddo

    allocate(name(0:i))

    do k = 0, i
        m = mod(n, 26)
        name( i - k ) = alp(m+1:m+1)
        n = int( (n-1) / 26)
    enddo
    print *, name

end program main


program c171

implicit none
integer(16) :: n, m, l
integer :: i, j
character(len=12) :: a, b

read *, n

i = 0

do while (n > 26)
 i = i + 1
 m = mod(n,26)
 if (m == 0) then
   m = 26
 end if
  a(i:i) = char(96 + m)
  n = n - m
  n = n / 26
end do

a(i+1:i+1) = char(96 + n)

do j = 1, i+1
 b(j:j) = a(i+2-j:i+2-j)
end do

if (i+1 == 12) then
 print '(A12)', trim(adjustl(b))
else if (i+1 == 11) then
 print '(A11)', trim(adjustl(b))
else if (i+1 == 10) then
 print '(A10)', trim(adjustl(b))
else if (i+1 == 9) then
 print '(A9)', trim(adjustl(b))
else if (i+1 == 8) then
 print '(A8)', trim(adjustl(b))
else if (i+1 == 7) then
 print '(A7)', trim(adjustl(b))
else if (i+1 == 6) then
 print '(A6)', trim(adjustl(b))
else if (i+1 == 5) then
 print '(A5)', trim(adjustl(b))
else if (i+1 == 4) then
 print '(A4)', trim(adjustl(b))
else if (i+1 == 3) then
 print '(A3)', trim(adjustl(b))
else if (i+1 == 2) then
 print '(A2)', trim(adjustl(b))
else
 print '(A1)', trim(adjustl(b))
end if

end program c171


program ABC171_C
  character(11)::B
  integer(16)::N,cnt=1,i,j
  integer(16),allocatable::C(:)
  read(*,*)N
  do i=1,11
     if(N>26**i) then
        N=N-26**i
        cnt=cnt+1
     else
        allocate(C(cnt))
        C=97
        do j=1,cnt
           C(j)=mod(N,26)+C(j)-1
           if(C(j)==96) C(j)=122
           N=N/26
           if(N==0)  then
              if(cnt>1) then
                 do k=2,cnt
                    if(C(k)/=97) C(k)=C(k)+1
                 end do
              end if
              exit
           end if
        end do
        exit
     end if
  end do
  do i=1,cnt
     B(cnt+1-i:cnt+1-i)=char(C(i))
  end do
  B=adjustl(B)
  B=trim(B)
  write(*,'(a)')B
end program ABC171_C


program abc171
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: n, ni, val
    character(20):: ans

    read*, n
    ! n=n-1
    ans = ''
    do while(n>0)
        ni = mod(n-1,26_8)
        val = ni + ichar('a')
        ans = char(val) //trim(ans)
        if (n==26) exit
        n=n/26
    end do
    print'(a)', ans
end program abc171


program main
    use iso_fortran_env
    implicit none
    integer(int64) :: n, q
    integer(int64) :: an(100000)
    integer(int64),allocatable :: a(:), b(:), c(:)
    integer(int64) :: i
    integer(int64) :: s

    read *, n
    allocate(a(n))
    read *, (a(i), i = 1, n)
    read *, q
    allocate(b(q), c(q))
    read *, (b(i), c(i), i = 1, q)

    an = 0
    do i = 1, n
        an(a(i)) = an(a(i)) + 1
    enddo

    s = sum(a)

    do i = 1, q
        s = s + (c(i) - b(i)) * an(b(i))
        an(c(i)) = an(c(i)) + an(b(i))
        an(b(i)) = 0
        print *, s
    enddo

end program main

Score : 400 points

Problem Statement

You have a sequence A composed of N positive integers: A_{1}, A_{2}, \cdots, A_{N}.

You will now successively do the following Q operations:

In the i-th operation, you replace every element whose value is B_{i} with C_{i}.

For each i (1 \leq i \leq Q), find S_{i}: the sum of all elements in A just after the i-th operation.

Constraints

All values in input are integers.

1 \leq N, Q, A_{i}, B_{i}, C_{i} \leq 10^{5}

B_{i} \neq C_{i}

Input

Input is given from Standard Input in the following format:

N
A_{1} A_{2} \cdots A_{N}
Q
B_{1} C_{1}
B_{2} C_{2}
\vdots
B_{Q} C_{Q}

Output

Print Q integers S_{i} to Standard Output in the following format:

S_{1}
S_{2}
\vdots
S_{Q}

Note that S_{i} may not fit into a 32-bit integer.

Sample Input 1

4
1 2 3 4
3
1 2
3 4
2 4

Sample Output 1

11
12
16

Initially, the sequence A is 1,2,3,4.

After each operation, it becomes the following:

2, 2, 3, 4

2, 2, 4, 4

4, 4, 4, 4

Sample Input 2

4
1 1 1 1
3
1 2
2 1
3 5

Sample Output 2

8
4
4

Note that the sequence A may not contain an element whose value is B_{i}.

Sample Input 3

2
1 2
3
1 100
2 100
100 1000

Sample Output 3

102
200
2000


program d171

implicit none
integer :: n, q, i, j, s, b, c
integer,allocatable :: a(:)

s = 0

read *, n

allocate(a(1:n))

read *, a(:)
read *, q

do i = 1, q
 read *, b, c
  do j = 1, n
   if (a(j) == b) then
    a(j) = c
   end if
   s = s + a(j)
  end do
 print *, s
 s = 0
end do

end program d171


program abc171
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: n,i,q,ans
    integer(int64):: cnt(100000)=0
    integer(int32), allocatable:: a(:),b(:),c(:)

    read*, n
    allocate(a(n))
    read*, a(:)
    ans = sum(a)
    do i=1,n
        cnt(a(i))=cnt(a(i))+1
    end do
    read*, q
    allocate(b(q),c(q))
    do i=1,q
        read*, b(i),c(i)
    end do
    do i=1,q
        ans = ans+(c(i)-b(i))*cnt(b(i))
        cnt(c(i)) = cnt(c(i)) + cnt(b(i))
        cnt(b(i)) = 0
        print'(i0)', ans
    end do
end program abc171


program shift
  implicit none
  integer(8), parameter :: p = 998244353
  character(300) :: s
  integer :: k, n, c(0:301) = 0, m = 0, i, j, x = 0
  integer(8) :: dp(0:301, 0:301) = 0
  integer(8) :: ans = 0
  read(*,*) s, k
  n = len_trim(s)
  do i = 1, n
    if (s(i:i) == '0') m = m + 1
  end do
  if (m == 0 .or. m == n) then
    write(*,'(i0)') 0
    stop
  end if
  m = m + 1
  j = m
  do i = n, 1, -1
    if (s(i:i) == '0') then
      j = j - 1
    else
      c(j) = c(j) + 1
    end if
  end do
  dp(m, 0) = 1
  x = c(m)
  do i = m - 1, 0, -1
    do j = x, 0, -1
      dp(i, j) = mod(sum(dp(i + 1, max(j - k, 0):j)), p)
    end do
    x = x + c(i)
  end do
  do i = 0, k
    ans = mod(ans + dp(0, i), p)
  end do
  write(*,'(i0)') ans
end program shift

Score : 800 points

Problem Statement

Given is a string S consisting of 0 and 1. Find the number of strings, modulo 998244353, that can result from applying the following operation on S between 0 and K times (inclusive):

Choose a pair of integers i, j (1\leq i < j\leq |S|) such that the i-th and j-th characters of S are 0 and 1, respectively. Remove the j-th character from S and insert it to the immediate left of the i-th character.

Constraints

1 \leq |S| \leq 300

0 \leq K \leq 10^9

S consists of 0 and 1.

Input

Input is given from Standard Input in the following format:

S K

Output

Find the number of strings, modulo 998244353, that can result from applying the operation on S between 0 and K times (inclusive).

Sample Input 1

0101 1

Sample Output 1

4

Four strings, 0101, 0110, 1001, and 1010, can result.

Sample Input 2

01100110 2

Sample Output 2

14

Sample Input 3

1101010010101101110111100011011111011000111101110101010010101010101 20

Sample Output 3

113434815


   program abc170a
      implicit none
      integer,dimension(5)::x
      integer::i

         read *, x(1:5)
         do i=1,5
            if ( x(i)==0 ) then
               print *, i
               stop
            end if
         end do

   end program abc170a

Score : 100 points

Problem Statement

We have five variables x_1, x_2, x_3, x_4, and x_5.

The variable x_i was initially assigned a value of i.

Snuke chose one of these variables and assigned it 0.

You are given the values of the five variables after this assignment.

Find out which variable Snuke assigned 0.

Constraints

The values of x_1, x_2, x_3, x_4, and x_5 given as input are a possible outcome of the assignment by Snuke.

Input

Input is given from Standard Input in the following format:

x_1 x_2 x_3 x_4 x_5

Output

If the variable Snuke assigned 0 was x_i, print the integer i.

Sample Input 1

0 2 3 4 5

Sample Output 1

1

In this case, Snuke assigned 0 to x_1, so we should print 1.

Sample Input 2

1 2 0 4 5

Sample Output 2

3


program five
  implicit none
  integer :: i, a(5)
  read(*,*) a
  do i = 1, 5
     if (a(i)==0) then
        write(*,*) i
        stop
     end if
  end do
  stop
end program five


program main
integer::x,y
read*,x,y
if(mod(x,2)==0 .and. 2*x<=y .and. y<=4*x)then
 print*,"Yes"
else
 print*,"No"
end if
end program main

Score : 200 points

Problem Statement

There are some animals in a garden. Each of them is a crane with two legs or a turtle with four legs.

Takahashi says: "there are X animals in total in the garden, and they have Y legs in total." Determine whether there is a combination of numbers of cranes and turtles in which this statement is correct.

Constraints

1 \leq X \leq 100

1 \leq Y \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

If there is a combination of numbers of cranes and turtles in which the statement is correct, print Yes; otherwise, print No.

Sample Input 1

3 8

Sample Output 1

Yes

The statement "there are 3 animals in total in the garden, and they have 8 legs in total" is correct if there are two cranes and one turtle. Thus, there is a combination of numbers of cranes and turtles in which the statement is correct.

Sample Input 2

2 100

Sample Output 2

No

There is no combination of numbers of cranes and turtles in which this statement is correct.

Sample Input 3

1 2

Sample Output 3

Yes

We also consider the case in which there are only cranes or only turtles.


program b170

implicit none
integer :: x, y

read *, x, y

if (mod(y,2) == 1) then
 print *, 'No'
else if (y > x * 4) then
 print *, 'No'
else if (y < x * 2) then
 print *, 'No'
else
 print *, 'Yes'
end if

end program b170


program main
implicit none
integer i, j
if(j < 2*i .or. j > 4*i) then
     write(*, *) "No"
else if (mod(j, 2)/=0) then
     write(*, *) "No"
else if (mod(j, 2)==0) then
     write(*, *) "Yes"
endif
stop
end program main


program abc170b
   implicit none
   integer :: x, y
   real(8) :: a, b, err=1d-3

   read(*,*) x, y

   a = dble(y)/2d0 - dble(x)
   b = dble(x) - a

   if (a >= 0d0 .and. b >=0d0) then
      if (a-int(a) <= err .and. b-int(b) <= err) then
         print *, "Yes"
      endif
   else
      print *, "No"
   endif
end program abc170b


program main
	implicit none
	integer(8):: X,Y,A,B,C,N,i,j,t,k,D,E
	read *, X,Y

   	if(mod(Y,2)==1)then
   	print'(a)',"No"
   	stop
   	end if
   	if((2*X-Y/2)>=0.and.(Y/2-X)>=0)then
  	print'(a)',"Yes"
  	stop
  	else
  	print'(a)',"No"
	stop
    end if

end program main


program answer
  implicit none

  integer :: x, y, i

  read(*,*) x, y

  do i = 0, x
     if(4*x-2*i==y) then
        write(*,*) 'Yes'
        stop
     end if
  end do

  write(*,*) 'No'

  stop
 end program answer


   program abc170c
      implicit none
      integer::x,n
      integer,allocatable,dimension(:)::p
      logical,allocatable,dimension(:)::check
      integer::i,step

         read *, x,n
         allocate( p(n),check(-100:200) )
         check=.false.
         read *, p(1:n)

         if ( n==0 ) then
            print *, x
            stop
         end if

         do i=1,n
            check(p(i))=.true.
         end do

         if ( check(x) .eqv. .false. ) then
            print *, x
            stop
         end if

         step=0
         do while ( check(x+step) .and. check(x-step) )
            step=step+1
            if ( check(x+step) .eqv. .false. ) then
               if ( check(x-step) .eqv. .false. ) then
                  print *, x-step
                  stop
               else
                  print *, x+step
                  stop
               end if
            end if
         end do

   end program abc170c

Score : 300 points

Problem Statement

Given are an integer X and an integer sequence of length N: p_1, \ldots, p_N.

Among the integers not contained in the sequence p_1, \ldots, p_N (not necessarily positive), find the integer nearest to X, that is, find the integer whose absolute difference with X is the minimum. If there are multiple such integers, report the smallest such integer.

Constraints

1 \leq X \leq 100

0 \leq N \leq 100

1 \leq p_i \leq 100

p_1, \ldots, p_N are all distinct.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X N
p_1 ... p_N

Output

Print the answer.

Sample Input 1

6 5
4 7 10 6 5

Sample Output 1

8

Among the integers not contained in the sequence 4, 7, 10, 6, 5, the one nearest to 6 is 8.

Sample Input 2

10 5
4 7 10 6 5

Sample Output 2

9

Among the integers not contained in the sequence 4, 7, 10, 6, 5, the ones nearest to 10 are 9 and 11. We should print the smaller one, 9.

Sample Input 3

100 0

Sample Output 3

100

When N = 0, the second line in the input will be empty. Also, as seen here, X itself can be the answer.


program main
  implicit none

  integer :: x, y, z, n, i, k
  integer,allocatable :: p(:)

  read(*,*) x, n

  if(n == 0) then
     write(*,*) x
     stop
  end if

  allocate(p(n))
  read(*,*) p

  call heapsort(p)

  do i = 1, n
     if(p(i) == x) then
        k = i
        exit
     else
        k = -1
     end if
  end do

  if(k == -1) then
     write(*,*) x
     stop
  else
     if(k == 1) then
        y = x-1
     else

        do i = 1, k-1
           if(p(k-i) /= x-i) then
              y = x-i
              exit
           else
              y = x - k
           end if
        end do
     end if

     if(k == n) then
        z = x + 1
     else

        do i = 1, n-k
           if(p(k+i) /= x+i) then
              z = x+i
              exit
           else
              z = x + (n - k + 1)
           end if
        end do
     end if

  end if

  if(abs(y-x) <= abs(z-x))then
     write(*,*) y
  else
     write(*,*) z
  end if

  stop

  contains
  subroutine heapsort(array)
  implicit none
  integer, intent(inout) :: array(:)

  integer :: i,k,j,l
  double precision :: t

  if(n <= 0)then
     write(6,*)"Error, at heapsort"; stop
  endif
  if(n == 1)return

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

  return
end subroutine heapsort

end program main


program main
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: cnt(0:101), x,n,i,l,r
    integer(int32), allocatable:: p(:)

    read*, x,n
    allocate(p(n))
    read*, p(:)
    cnt(:) = 0
    do i=1,n
        cnt(p(i))=cnt(p(i))+1
    end do

    l=x
    r=x

    do while(0 /= l .or. r /= 101)
        if (cnt(l) == 0) then
            print'(i0)', l
            stop
        end if

        if (cnt(r) == 0) then
            print'(i0)', r
            stop
        end if

        l=max(l-1,0)
        r=min(r+1,101)
    end do
end program main


program probA
    implicit none
    integer::i
    character(len=21)::S
    character(len=1)::v(3)
    read(*,*) S

    do i = 1,3
        v(i) = S(i:i)
    end do

    write(*,*) v
    stop
end program

Score : 100 points

Problem Statement

When you asked some guy in your class his name, he called himself S, where S is a string of length between 3 and 20 (inclusive) consisting of lowercase English letters.
You have decided to choose some three consecutive characters from S and make it his nickname. Print a string that is a valid nickname for him.

Constraints

3 \leq |S| \leq 20

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

Print your answer.

Sample Input 1

takahashi

Sample Output 1

tak

Sample Input 2

naohiro

Sample Output 2

nao


program a613

implicit none
character(len=20) :: s

read *, s

print '(A3)', trim(s(:3))

end program a613


program answer
  implicit none

  integer :: A, V, B, W, T

  read(*,*) A, V
  read(*,*) B, W
  read(*,*) T

  if(W>=V) then
     write(*,*) 'NO'
     stop
  end if

  if(A<B) then
   if(B+W*T<=A+V*T) then
     write(*,*) 'YES'
   else
     write(*,*) 'NO'
    end if
 else
    if(B-W*T<=A-V*T) then
       write(*,*) 'YES'
    else
       write(*,*) 'NO'
    end if
  end if

  stop
 end program answer

Score : 200 points

Problem Statement

Two children are playing tag on a number line. (In the game of tag, the child called "it" tries to catch the other child.) The child who is "it" is now at coordinate A, and he can travel the distance of V per second.
The other child is now at coordinate B, and she can travel the distance of W per second.

He can catch her when his coordinate is the same as hers.
Determine whether he can catch her within T seconds (including exactly T seconds later).
We assume that both children move optimally.

Constraints

-10^9 \leq A,B \leq 10^9

1 \leq V,W \leq 10^9

1 \leq T \leq 10^9

A \neq B

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A V
B W
T

Output

If "it" can catch the other child, print YES; otherwise, print NO.

Sample Input 1

1 2
3 1
3

Sample Output 1

YES

Sample Input 2

1 2
3 2
3

Sample Output 2

NO

Sample Input 3

1 2
3 3
3

Sample Output 3

NO


program answer
  implicit none

  integer :: A, V, B, W, T

  read(*,*) A, V
  read(*,*) B, W
  read(*,*) T

  if(A<B) then
   if(B+W*T<=A+V*T) then
     write(*,*) 'YES'
   else
     write(*,*) 'NO'
    end if
 else
    if(B-W*T<=A-V*T) then
       write(*,*) 'YES'
    else
       write(*,*) 'NO'
    end if
  end if

  stop
 end program answer


      program tokyoA
      implicit none

      integer(8) :: i,k,l,m,j,d,p
      real(8)::n
      integer(8),allocatable::B(:),A(:)

      read(*,*)n,k
      allocate(A(n),B(n))

      read(*,*)A(:)

      if (K >n*log(n))then
        A(:)=n
      else
      do i =1,k
        do j = 1,n
        B(j) =0
        end do
        do m =1,n
            d = A(m)
            B(m) =B(m)+1
            do l = 1,d
                if ((m+l).le.n)then
                    B(m+l) =B(m+l)+1
                endif
                if (m.gt.l)then
                    B(m-l) = B(m-l)+1
                endif
            end do
        end do
        do p = 1,n
            A(p) = B(p)
        end do

      end do
      endif

      write(*,*)A

      end program

Score : 500 points

Problem Statement

We have N bulbs arranged on a number line, numbered 1 to N from left to right.
Bulb i is at coordinate i.

Each bulb has a non-negative integer parameter called intensity.
When there is a bulb of intensity d at coordinate x, the bulb illuminates the segment from coordinate x-d-0.5 to x+d+0.5.
Initially, the intensity of Bulb i is A_i. We will now do the following operation K times in a row:

For each integer i between 1 and N (inclusive), let B_i be the number of bulbs illuminating coordinate i. Then, change the intensity of each bulb i to B_i.

Find the intensity of each bulb after the K operations.

Constraints

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

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

0 \leq A_i \leq N

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \ldots A_N

Output

Print the intensity A{'}_i of each bulb i after the K operations to Standard Output in the following format:

A{'}_1 A{'}_2 \ldots A{'}_N

Sample Input 1

5 1
1 0 0 1 0

Sample Output 1

1 2 2 1 2

Initially, only Bulb 1 illuminates coordinate 1, so the intensity of Bulb 1 becomes 1 after the operation.
Similarly, the bulbs initially illuminating coordinate 2 are Bulb 1 and 2, so the intensity of Bulb 2 becomes 2.

Sample Input 2

5 2
1 0 0 1 0

Sample Output 2

3 3 4 4 3


program c613

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

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

read *, a(:)

b(:) = 0

do h = 1, k
 do i = 1, n
  do j = i - a(i), i + a(i)
   b(j) = b(j) + 1
  end do
 end do
 a(:) = b(:)
end do

print *, a(:)

end program c613


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

Score : 100 points

Problem Statement

Compute A \times B.

Constraints

1 \leq A \leq 100

1 \leq B \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the value A \times B as an integer.

Sample Input 1

2 5

Sample Output 1

10

We have 2 \times 5 = 10.

Sample Input 2

100 100

Sample Output 2

10000


program main
  implicit none
  integer(8) i, N, lim, m(39), t, ans, exp, level
  !integer(8),allocatable:: A(:,:)
  read(*, *) N
  lim = floor(N ** 0.5) + 1
  !allocate(A(N,2)
  m = 0
  t = 0
  do i = 1,39
    m(i) = t+i
    t = m(i)
  end do
  !read(*, *) (L(i), i = 1,N)
  t = 2
  exp = 0
  level = 0
  ans = 0
  if (N == 1) then
    ans = 0
  else
    do
      if (mod(N,t) == 0) then
        exp = exp + 1
        N = N/t
        if (exp == m(level+1)) then
          level = level + 1
        end if
      else
        ans = ans + level
        exp = 0
        level = 0
        t = t+1
      endif
      if (t == lim) then
        if (ans == 0) then
          ans = ans + 1
        endif
        exit
      endif
    end do
  endif
  write(*, *) ans
end program main

Score : 400 points

Problem Statement

Given is a positive integer N. Consider repeatedly applying the operation below on N:

First, choose a positive integer z satisfying all of the conditions below:

z can be represented as z=p^e, where p is a prime number and e is a positive integer;

z divides N;

z is different from all integers chosen in previous operations.

Then, replace N with N/z.

Find the maximum number of times the operation can be applied.

Constraints

All values in input are integers.

1 \leq N \leq 10^{12}

Input

Input is given from Standard Input in the following format:

N

Output

Print the maximum number of times the operation can be applied.

Sample Input 1

24

Sample Output 1

3

We can apply the operation three times by, for example, making the following choices:

Choose z=2 (=2^1). (Now we have N=12.)

Choose z=3 (=3^1). (Now we have N=4.)

Choose z=4 (=2^2). (Now we have N=1.)

Sample Input 2

1

Sample Output 2

0

We cannot apply the operation at all.

Sample Input 3

64

Sample Output 3

3

We can apply the operation three times by, for example, making the following choices:

Choose z=2 (=2^1). (Now we have N=32.)

Choose z=4 (=2^2). (Now we have N=8.)

Choose z=8 (=2^3). (Now we have N=1.)

Sample Input 4

1000000007

Sample Output 4

1

We can apply the operation once by, for example, making the following choice:

z=1000000007 (=1000000007^1). (Now we have N=1.)

Sample Input 5

997764507000

Sample Output 5

7


program d
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64)::n,i,j,ans
    integer(int64):: dn(1000000)=0, bn(0:5050)
    logical:: pn(1000000) = .true.

    read*, n
    pn(1) = .false.

    do i=2,1000000
        if (pn(i)) then
            j=2
            do while(i*j <= 1000000)
                pn(i*j) = .false.
                j=j+1
            end do
        end if
    end do

    do i=2,1000000
        if (pn(i)) then
            do while(mod(n,i) == 0)
                n=n/i
                dn(i) = dn(i)+1
            end do
        end if
    end do
    ans = 0

    if (n > 1) ans=ans+1
    bn(:) = 0
    do i=100,1,-1
        bn((i*(i+1))/2) = i
    end do
    do i=2,5050
        bn(i) = max(bn(i),bn(i-1))
    end do

    do i=1,1000000
        ans=ans+bn(dn(i))
    end do
    print'(i0)', ans

end program d


program a0530

implicit none
integer :: h1, h2, m1, m2, k, t

read *, h1, m1, h2, m2, k
t = (h2 - h1) * 60 + m2 - m1

print *, t - k

end program a0530

Score : 100 points

Problem Statement

In this problem, we use the 24-hour clock.

Takahashi gets up exactly at the time H_1 : M_1 and goes to bed exactly at the time H_2 : M_2. (See Sample Inputs below for clarity.)
He has decided to study for K consecutive minutes while he is up.
What is the length of the period in which he can start studying?

Constraints

0 \le H_1, H_2 \le 23

0 \le M_1, M_2 \le 59

The time H_1 : M_1 comes before the time H_2 : M_2.

K \ge 1

Takahashi is up for at least K minutes.

All values in input are integers (without leading zeros).

Input

Input is given from Standard Input in the following format:

H_1 M_1 H_2 M_2 K

Output

Print the length of the period in which he can start studying, as an integer.

Sample Input 1

10 0 15 0 30

Sample Output 1

270

Takahashi gets up at exactly ten in the morning and goes to bed at exactly three in the afternoon.
It takes 30 minutes to do the study, so he can start it in the period between ten o'clock and half-past two. The length of this period is 270 minutes, so we should print 270.

Sample Input 2

10 0 12 0 120

Sample Output 2

0

Takahashi gets up at exactly ten in the morning and goes to bed at exactly noon. It takes 120 minutes to do the study, so he has to start it at exactly ten o'clock. Thus, we should print 0.


    program main

    integer H1,H2,M1,M2,K,ANS
    ANS = 0
    read *,H1,M1,H2,M2,K
    ANS = ( H2 * 60 + M2 ) - ( H1 * 60 + M1 ) - K
    print *,ANS
    end program main


program main
    implicit none
    character t*200000
    character, allocatable :: s(:)
    integer i,n

    read(*,*) t
    n = len_trim(t)
    allocate(s(n))
    do i = 1,n
        if(t(i:i) == "?") then
            s(i:i) = "D"
        else
            s(i:i) = t(i:i)
        end if
    end do

    write(*,*) s

end program

Score : 200 points

Problem Statement

For a string S consisting of the uppercase English letters P and D, let the doctoral and postdoctoral quotient of S be the total number of occurrences of D and PD in S as contiguous substrings. For example, if S = PPDDP, it contains two occurrences of D and one occurrence of PD as contiguous substrings, so the doctoral and postdoctoral quotient of S is 3.

We have a string T consisting of P, D, and ?.

Among the strings that can be obtained by replacing each ? in T with P or D, find one with the maximum possible doctoral and postdoctoral quotient.

Constraints

1 \leq |T| \leq 2 \times 10^5

T consists of P, D, and ?.

Input

Input is given from Standard Input in the following format:

T

Output

Print one string with the maximum possible doctoral and postdoctoral quotient among the strings that can be obtained by replacing each ? in T with P or D.
If there are multiple such strings, you may print any of them.

Sample Input 1

PD?D??P

Sample Output 1

PDPDPDP

This string contains three occurrences of D and three occurrences of PD as contiguous substrings, so its doctoral and postdoctoral quotient is 6, which is the maximum doctoral and postdoctoral quotient of a string obtained by replacing each ? in T with P or D.

Sample Input 2

P?P?

Sample Output 2

PDPD


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

    read*, n
    allocate(a(0:n))
    read*, a(:)
    asum = sum(a)
    pmax = 1
    i=0
    do while(pmax < asum)
        ans = ans + min(pmax, asum)
        if (a(i) > pmax) then
            print'(i0)', -1
            stop
        end if
        asum=asum-a(i)
        pmax=(pmax-a(i))*2
        i=i+1
    end do

    do j=i,n
        ans = ans + asum
        asum=asum-a(j)
    end do
    print'(i0)', ans
end program main

Score : 600 points

Problem Statement

Given is an integer sequence of length N+1: A_0, A_1, A_2, \ldots, A_N. Is there a binary tree of depth N such that, for each d = 0, 1, \ldots, N, there are exactly A_d leaves at depth d? If such a tree exists, print the maximum possible number of vertices in such a tree; otherwise, print -1.

Notes

A binary tree is a rooted tree such that each vertex has at most two direct children.

A leaf in a binary tree is a vertex with zero children.

The depth of a vertex v in a binary tree is the distance from the tree's root to v. (The root has the depth of 0.)

The depth of a binary tree is the maximum depth of a vertex in the tree.

Constraints

0 \leq N \leq 10^5

0 \leq A_i \leq 10^{8} (0 \leq i \leq N)

A_N \geq 1

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_0 A_1 A_2 \cdots A_N

Output

Print the answer as an integer.

Sample Input 1

3
0 1 1 2

Sample Output 1

7

Below is the tree with the maximum possible number of vertices. It has seven vertices, so we should print 7.

Sample Input 2

4
0 0 1 0 2

Sample Output 2

10

Sample Input 3

2
0 3 1

Sample Output 3

-1

Sample Input 4

1
1 1

Sample Output 4

-1

Sample Input 5

10
0 0 1 1 2 3 5 8 13 21 34

Sample Output 5

264


program NOMURA2020C
  integer(8)::N,i
  integer(8),allocatable::A(:),k(:),l(:)
  read(*,*)N
  allocate(A(0:N),k(0:N),l(0:N))
  read(*,*)A
  if(A(0)==0) then
     k(0)=1_8
  else
     k(0)=0_8
  end if
  do i=1,N
     k(i)=k(i-1)*2_8-A(i)
     if((k(i)<=0 .and. i<N) .or. (k(i)<0 .and. i==N)) then
        write(*,*)'-1'
        stop
     end if
  end do
  l(N)=A(N)
  if(l(N)>k(N)+A(N)) then
     write(*,*)'-1'
     stop
     end if
  do i=N-1_8,0_8,-1_8
     l(i)=l(i+1)+A(i)
     if(l(i)>k(i)+A(i)) l(i)=k(i)+A(i)
     if(l(i)<k(i-1) .and. i>0) then
        write(*,*)'-1'
        stop
     end if
  end do
  write(*,*) sum(l)
end program NOMURA2020C


program therefore

 implicit none
 character::n*3
 integer::m

 read*,n
 m=len_trim(n)

 if(n(m:m)=='0'.or.n(m:m)=='1'.or.n(m:m)=='6'.or.n(m:m)=='8') then
  print*,'pon'
 else if(n(m:m)=='3') then
  print*,'bon'
 else
  print*,'hon'
 end if

end program therefore

Score: 100 points

Problem Statement

The cat Snuke wants to play a popular Japanese game called ÅtCoder, so Iroha has decided to teach him Japanese.

When counting pencils in Japanese, the counter word "本" follows the number. The pronunciation of this word varies depending on the number. Specifically, the pronunciation of "本" in the phrase "N 本" for a positive integer N not exceeding 999 is as follows:

hon when the digit in the one's place of N is 2, 4, 5, 7, or 9;

pon when the digit in the one's place of N is 0, 1, 6 or 8;

bon when the digit in the one's place of N is 3.

Given N, print the pronunciation of "本" in the phrase "N 本".

Constraints

N is a positive integer not exceeding 999.

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

16

Sample Output 1

pon

The digit in the one's place of 16 is 6, so the "本" in "16 本" is pronounced pon.

Sample Input 2

2

Sample Output 2

hon

Sample Input 3

183

Sample Output 3

bon


program therefore

 implicit none
 character::n
 integer::m

 read*,n
 m=len_trim(n)

 if(n(m:m)=='0'.or.n(m:m)=='1'.or.n(m:m)=='6'.or.n(m:m)=='8') then
  print*,'pon'
 else if(n(m:m)=='3') then
  print*,'bon'
 else
  print*,'hon'
 end if

end program therefore


program main
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: n, ni

    read*, n
    ni = mod(n,10)

    if (ni==3) then
        print'(a)', 'bon'
    else if (ni==0 .or. ni==1 .or. ni==6 .or. ni==8) then
        print'(a)', 'pon'
    else
        print'(a)', 'hon'
    end if
end program main


program main
character S*100
integer K,K2,I

read *,K
read *,S
I = K + 1
K2 = len_trim(S)
if( K < K2 ) then
  S(I:K2) = ""
  S(I:I) = "."
  S(I+1:I+1) = "."
  S(I+2:I+2) = "."
end if
print '(A)', trim(S)
end program main

Score: 200 points

Problem Statement

We have a string S consisting of lowercase English letters.

If the length of S is at most K, print S without change.

If the length of S exceeds K, extract the first K characters in S, append ... to the end of them, and print the result.

Constraints

K is an integer between 1 and 100 (inclusive).

S is a string consisting of lowercase English letters.

The length of S is between 1 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

K
S

Output

Print a string as stated in Problem Statement.

Sample Input 1

7
nikoandsolstice

Sample Output 1

nikoand...

nikoandsolstice has a length of 15, which exceeds K=7.

We should extract the first 7 characters in this string, append ... to the end of them, and print the result nikoand....

Sample Input 2

40
ferelibenterhominesidquodvoluntcredunt

Sample Output 2

ferelibenterhominesidquodvoluntcredunt

The famous quote from Gaius Julius Caesar.


program main
character S*100
integer K,K2,I

read *,K
read *,S
K2 = len_trim(S)
I = K + 1
do
S(I:I) = "."
I = I + 1
  if (I > K2) then
      exit
  end if
end do
print '(A)', trim(S)
end program main


program main
integer :: k
character*100 ::s

read(*,*)k
read(*,*)s
if (len_trim(s) <= k) then
write(*,*) s
else
do i = (len_trim(s) - k),len_trim(s)
s(i:i) = ''
end do
write(*,*)s//'...'
end if
end program


program main
      implicit none
      integer :: a, b, h, m
      real(16) :: a2, b2, rad, c

      read *, a, b, h, m

      a2 = real(a)
      b2 = real(b)
      rad = abs( 0.5*(real(h)*60+m) - 6*m )
      rad = 3.141592653589793238462643383279*rad / 180.0

      c = a2**2 + b2**2 - 2*a2*b2*cos(rad)

      print *, sqrt(c)

end program main

Score: 300 points

Problem Statement

Consider an analog clock whose hour and minute hands are A and B centimeters long, respectively.

An endpoint of the hour hand and an endpoint of the minute hand are fixed at the same point, around which each hand rotates clockwise at constant angular velocity. It takes the hour and minute hands 12 hours and 1 hour to make one full rotation, respectively.

At 0 o'clock, the two hands overlap each other. H hours and M minutes later, what is the distance in centimeters between the unfixed endpoints of the hands?

Constraints

All values in input are integers.

1 \leq A, B \leq 1000

0 \leq H \leq 11

0 \leq M \leq 59

Input

Input is given from Standard Input in the following format:

A B H M

Output

Print the answer without units. Your output will be accepted when its absolute or relative error from the correct value is at most 10^{-9}.

Sample Input 1

3 4 9 0

Sample Output 1

5.00000000000000000000

The two hands will be in the positions shown in the figure below, so the answer is 5 centimeters.

Sample Input 2

3 4 10 40

Sample Output 2

4.56425719433005567605

The two hands will be in the positions shown in the figure below. Note that each hand always rotates at constant angular velocity.


program main
    use,intrinsic :: iso_fortran_env
    implicit none
    real(real64):: a,b,h,m
    real(real64)x1,y1,x2,y2
    real(real64),parameter:: pi=acos(-1d0)
    read*, a,b,h,m
    h=(h+m/60)/12*2*pi
    m=m/60d0*2*pi

    x1 = a*cos(h)
    y1 = a*sin(h)
    x2 = b*cos(m)
    y2 = b*sin(m)
    print*, sqrt((x1-x2)**2 + (y1-y2)**2)
end program main


      MODULE intQueueMod
      implicit none
! Module for QUEUE : First in, First out
! reference : プログラミングコンテスト攻略のための
!              アルゴリズムとデータ構造

      integer(16),parameter :: binSize = 10**6
      integer(16),parameter :: precision = 16

      type intQueue
        private
        integer(precision) :: bin( 1:binSize ) = 0
        integer(precision) :: head = 1,   tail = 1
        !head : Where the head value is.
        !tail : Where the blank next to tail is.
        contains
        procedure,public,pass :: initialize => intInit
        procedure,public,pass :: isEmpty    => intIsEmpty
        procedure,public,pass :: isFull     => intIsFull
        procedure,public,pass :: enQueue    => intEnQ
        procedure,public,pass :: deQueue    => intDeQ
      end type

      contains
!Reset queue. Not nessesary in first time.
      pure subroutine intInit( q )
        implicit none
        class(intQueue),intent(inout) :: q

        q%head = 1
        q%tail = 1
      end subroutine intInit

!Check status
      pure function intIsEmpty( q ) result( ans )
        implicit none
        class(intQueue),intent(in) :: q
        logical :: ans

        ans = q%head==q%tail
      end function intIsEmpty

      pure function intIsFull( q ) result( ans )
        implicit none
        class(intQueue),intent(in) :: q
        logical :: ans

        ans = q%head==mod( q%tail+1,binSize )
      end function intIsFull

!set x to tail
      subroutine intEnQ( q,x )
        implicit none
        class(intQueue)      ,intent(inout) :: q
        integer(precision),intent(in   ) :: x

        if( q%isFull() )then
          print*,'Queue is full,Error!';stop
        else
          q%bin(q%tail) = x
          if( q%tail==binSize )then
            q%tail = 1
          else
            q%tail = q%tail + 1
          end if
        end if
      end subroutine intEnQ

!get x from head
      impure function intDeQ( q ) result( x )
        implicit none
        class(intQueue),intent(inout) :: q
        integer(precision) :: x

        if( q%isEmpty() )then
          print*,'Stack is empty, Error!';stop
        else
          x = q%bin(q%head)
          if( q%head==binSize )then
            q%head = 1
          else
            q%head = q%head + 1
          end if
        end if
      end function intDeQ
      END MODULE intQueueMod

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
      MODULE graphMod
      implicit none
! 重みなしのグラフを扱うためのモジュール

      integer(16),parameter,private :: prec = 16

      type :: nodes
        integer(prec),allocatable :: next(:)
        integer(prec) :: degree = 0
        contains
        procedure,public,pass :: appendNode
      end type

      contains
      subroutine appendNode( node, nodeNumber )
        implicit none
        class(nodes),intent(inout) :: node
        integer(prec),intent(in)  :: nodeNumber
        integer(prec),allocatable :: buffer(:)

        if(.not.(allocated(node%next)))then
          allocate(node%next(1))
          node%degree = 0
        end if

        if( node%degree==0 )then
          node%next(1) = nodeNumber
          node%degree = 1
          return
        end if

        node%degree = node%degree + 1
        buffer = node%next(1:node%degree-1)
        if( size(node%next)<node%degree )then
          deallocate( node%next )
          allocate( node%next(node%degree*2) )
        end if
        node%next(1:node%degree) = [buffer(1:node%degree-1), nodeNumber]
        deallocate( buffer )
      end subroutine appendNode

      END MODULE graphMod

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

      module piyoMod
      use intQueueMod
      use graphMod

      integer(16),allocatable :: flag(:)
      type(intQueue) :: nextQ,beforeQ

      contains
      subroutine solve( rIn,roomNum,before )
        implicit none
        type(nodes),intent(in) :: rIn(:)
        integer(16),intent(in) :: roomNum,before
        integer(16) :: i

        if( flag(roomNum)/=-1 ) return
        flag( roomNum ) = before
        if( rIn(roomNum)%degree==0 ) return

        do i = 1,rIn(roomNum)%degree
          call nextQ%enQueue( rIn(roomNum)%next(i) )
          call beforeQ%enQueue( roomNum )
        end do

      end subroutine
      end module

      PROGRAM doubleDots
      use piyoMod
      IMPLICIT NONE
      integer(16) :: n,m,numNei,buffer(5*10**5+1)
      integer(16),allocatable :: a(:),b(:)
      type(nodes),allocatable :: room(:)
      integer(16) :: i

      read*,n,m
      allocate( a(m),b(m),room(n),flag(n) )
      flag(1:n) = -1
      do i = 1,m
        read*,a(i),b(i)
      end do

      do i = 1,m
        call room(a(i))%appendNode( b(i) )
        call room(b(i))%appendNode( a(i) )
      end do

 !     call nextQ%enQueue(1_16)
 !     call beforeQ%enQueue(0_16) !dummy
 !     do while( .true. )
 !       if( nextQ%isEmpty() ) exit
 !       call solve( room,nextQ%deQueue(),beforeQ%deQueue() )
 !     end do

      if( count(flag(2:n)==-1) >=1 )then
        print*,'No';stop
      end if

      print*,'Yes'
      do i = 2,2
        print*,flag(i)
      end do

   !   print*,n,m
   !   do i = 1,m
   !     print*,a(i),b(i)
   !   end do

   !   do i= 1,n
   !     print*,'room',i,room(i)%numNei
   !     print*,room(i)%nei
   !   end do

      END PROGRAM

Score: 400 points

Problem Statement

There is a cave.

The cave has N rooms and M passages. The rooms are numbered 1 to N, and the passages are numbered 1 to M. Passage i connects Room A_i and Room B_i bidirectionally. One can travel between any two rooms by traversing passages. Room 1 is a special room with an entrance from the outside.

It is dark in the cave, so we have decided to place a signpost in each room except Room 1. The signpost in each room will point to one of the rooms directly connected to that room with a passage.

Since it is dangerous in the cave, our objective is to satisfy the condition below for each room except Room 1.

If you start in that room and repeatedly move to the room indicated by the signpost in the room you are in, you will reach Room 1 after traversing the minimum number of passages possible.

Determine whether there is a way to place signposts satisfying our objective, and print one such way if it exists.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

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

1 \leq A_i, B_i \leq N\ (1 \leq i \leq M)

A_i \neq B_i\ (1 \leq i \leq M)

One can travel between any two rooms by traversing passages.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
:
A_M B_M

Output

If there is no way to place signposts satisfying the objective, print No.

Otherwise, print N lines. The first line should contain Yes, and the i-th line (2 \leq i \leq N) should contain the integer representing the room indicated by the signpost in Room i.

Sample Input 1

4 4
1 2
2 3
3 4
4 2

Sample Output 1

Yes
1
2
2

If we place the signposts as described in the sample output, the following happens:

Starting in Room 2, you will reach Room 1 after traversing one passage: (2) \to 1. This is the minimum number of passages possible.

Starting in Room 3, you will reach Room 1 after traversing two passages: (3) \to 2 \to 1. This is the minimum number of passages possible.

Starting in Room 4, you will reach Room 1 after traversing two passages: (4) \to 2 \to 1. This is the minimum number of passages possible.

Thus, the objective is satisfied.

Sample Input 2

6 9
3 4
6 1
2 4
5 3
4 6
1 5
6 2
4 5
5 6

Sample Output 2

Yes
6
5
5
1
1

If there are multiple solutions, any of them will be accepted.