program c
  integer :: N,i,mx,mx1
  integer,allocatable :: A(:),left(:,:)
  read(*,*) N
  allocate(A(N))
  do i=1,N
  read(*,*) A(i)
  enddo
  allocate(left(N,2))
  mx=maxval(A)
  left(1,1)=A(1)
  left(1,2)=A(N)
  do i=2,N
     left(i,1)=max(left(i-1,1),A(i))
     left(i,2)=max(left(i-1,2),A(N-i+1))
  enddo

  do i=1,N
     if(mx.eq.A(i)) then
        if(i.eq.1) then
           write(*,'(i0)') left(N-1,2)
        elseif(i.eq.N) then
           write(*,'(i0)') left(N-1,1)
        else
           write(*,'(i0)') max(left(i-1,1),left(N-i,2))
        endif
     else
        write(*,'(i0)') mx
     endif
  enddo

end program c


program abc134c
   implicit none
   integer(8) :: n, i
   integer(8), allocatable :: a(:)

   read(*,*) n
   allocate(a(n))
   do i = 1, n
      read(*,*) a(i)
   enddo

   ! i = 1
   write(*,*) maxval(a(2:n))

   do i = 2, n-1
      write(*,*) max(maxval(a(1:i-1)), maxval(a(i+1:n)) )
   end do

   ! i = n
   write(*,*) maxval(a(1:n-1))

   stop
end

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

Score : 300 points

Problem Statement

You are given a sequence of length N: A_1, A_2, ..., A_N.
For each integer i between 1 and N (inclusive), answer the following question:

Find the maximum value among the N-1 elements other than A_i in the sequence.

Constraints

2 \leq N \leq 200000

1 \leq A_i \leq 200000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

Print N lines. The i-th line (1 \leq i \leq N) should contain the maximum value among the N-1 elements other than A_i in the sequence.

Sample Input 1

3
1
4
3

Sample Output 1

4
3
4

The maximum value among the two elements other than A_1, that is, A_2 = 4 and A_3 = 3, is 4.

The maximum value among the two elements other than A_2, that is, A_1 = 1 and A_3 = 3, is 3.

The maximum value among the two elements other than A_3, that is, A_1 = 1 and A_2 = 4, is 4.

Sample Input 2

2
5
5

Sample Output 2

5
5

Submitted source-code file size, in bytes.

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

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


program main
  implicit none
  integer i,N,maxA,maxA2,maxl
  integer, allocatable :: A(:)
  !read(*, *) (L(i), i = 1,N)
  read(*, *) N
  allocate(A(N))
  do i = 1,N
    read(*,*) A(i)
  end do
  maxA = maxval(A)
  do i = 1,N
    if (A(i) == maxA) then
      A(i) = 0
      maxl = i
      exit
    end if
  end do
  do i = 1,N
    if (i == maxl) then
      write(*,*) maxval(A)
    else
      write(*, *) maxA
    end if
  end do
end program main


program A319D
    implicit none
    integer(8)::N,calc,i,j,bcount
    integer(8),allocatable,dimension(:)::A,ans
    read*,N
    allocate(A(N))
    allocate(ans(N))
    read*,A
    ans=0
    bcount=0
    do i=0,N-1
        calc=0
        do j=1,N/(N-i)
            calc=calc+ans(j*(N-i))
        end do
        if(A(N-i)==0)then
            if(mod(calc,2)==1)then
                ans(N-i)=1
                bcount=bcount+1
            end if
        else
            if(mod(calc,2)==0)then
                ans(N-i)=1
                bcount=bcount+1
            end if
        end if
    end do

    print'(i0)',bcount
    do i=1,N
        if(ans(i)==1)write(*,fmt='(i0,1x)',advance='no')i
    end do
    print'(A)',""
end program A319D

Score : 400 points

Problem Statement

There are N empty boxes arranged in a row from left to right.
The integer i is written on the i-th box from the left (1 \leq i \leq N).

For each of these boxes, Snuke can choose either to put a ball in it or to put nothing in it.

We say a set of choices to put a ball or not in the boxes is good when the following condition is satisfied:

For every integer i between 1 and N (inclusive), the total number of balls contained in the boxes with multiples of i written on them is congruent to a_i modulo 2.

Does there exist a good set of choices? If the answer is yes, find one good set of choices.

Constraints

All values in input are integers.

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

a_i is 0 or 1.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

If a good set of choices does not exist, print -1.

If a good set of choices exists, print one such set of choices in the following format:

M
b_1 b_2 ... b_M

where M denotes the number of boxes that will contain a ball, and b_1,\ b_2,\ ...,\ b_M are the integers written on these boxes, in any order.

Sample Input 1

3
1 0 0

Sample Output 1

1
1

Consider putting a ball only in the box with 1 written on it.

There are three boxes with multiples of 1 written on them: the boxes with 1, 2, and 3. The total number of balls contained in these boxes is 1.

There is only one box with a multiple of 2 written on it: the box with 2. The total number of balls contained in these boxes is 0.

There is only one box with a multiple of 3 written on it: the box with 3. The total number of balls contained in these boxes is 0.

Thus, the condition is satisfied, so this set of choices is good.

Sample Input 2

5
0 0 0 0 0

Sample Output 2

0

Putting nothing in the boxes can be a good set of choices.


program preparing_boxes
    implicit none
    integer(4):: n, i, j
    integer(4), allocatable:: a(:), b(:)

    read*, n
    allocate(a(n), b(n))
    read*, a(:)
    b(:)=0
    do i=n, 1,-1
        if (a(i) > 1)then
            print*, -1
            stop
        else if (a(i) == 1) then
            b(i) = 1
            do j=2,nint(sqrt(dble(i)))
                if (mod(i,j) == 0) a(i/j)=a(i/j)-1
            end do
        end if
        ! print*, 'b',b(:)
    end do

    print*, sum(b(:))
    do i=1,n
        if (b(i)==1) write(*,'(i0,1x)', advance='no') i
    end do
end program preparing_boxes


integer N
integer,allocatable,dimension(:)::A,C
integer i,j,cnt
read*,N
allocate(A(N),C(N))
read*,A
cnt=0
do i=n,1,-1
  if(A(i)/=0)then
    cnt=cnt+1
    c=[i,c]
    do j=1,i
      if(j*j>i)exit
      if(mod(i,j)==0)then
        A(j)=xor(A(j),1)
        if(j*j/=i)A(i/j)=xor(A(i/j),1)
      endif
    end do
  endif
end do
print"(I0)",cnt
do i=1,cnt
  if(i==1)then
  write(*,'(i0)', advance='no')c(i)
  else
  write(*,'(x,i0)', advance='no')c(i)
  endif
end do
end


program main
  implicit none
  integer i,N,i2,ball,j,t
  integer,allocatable :: a(:),b(:),b2(:)
  !read(*, *) (L(i), i = 1,N)
  read(*, *) N
  allocate(a(N))
  allocate(b(N))
  allocate(b2(N))
  read(*, *) (a(i), i = 1,N)
  b2 = a
  t = 1
  do i = 1,N
    i2 = N-i+1
    ball = 0
    do j = 1,N/i2
      ball = ball+b2(i2*j)
    end do
    !b(i2) = mod(ball,2)
    ball = mod(ball,2)
    b2(i2) = ball
    if (ball == 1) then
      b(t) = i2
      t = t+1
    end if
  end do
  write(*, *) t-1
  if (t /= 1) then
    write(*, *) b(1:t-1)
  end if
end program main


program sequence_decomposing
    implicit none
    integer(4):: n,i,c,cmax
    integer(8):: a1,a2

    read*,n
    read*,a1
    c=0
    cmax=0
    do i=1,n-1
        read*, a2

        if (a2 <= a1)then
            c=c+1
        else
            c=0
        end if
        cmax=max(cmax,c)
        a1=a2
    end do

    print*,cmax+1

end program sequence_decomposing

Score : 500 points

Problem Statement

You are given a sequence with N integers: A = \{ A_1, A_2, \cdots, A_N \}.
For each of these N integers, we will choose a color and paint the integer with that color. Here the following condition must be satisfied:

If A_i and A_j (i < j) are painted with the same color, A_i < A_j.

Find the minimum number of colors required to satisfy the condition.

Constraints

1 \leq N \leq 10^5

0 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

Print the minimum number of colors required to satisfy the condition.

Sample Input 1

5
2
1
4
5
3

Sample Output 1

2

We can satisfy the condition with two colors by, for example, painting 2 and 3 red and painting 1, 4, and 5 blue.

Sample Input 2

4
0
0
0
0

Sample Output 2

4

We have to paint all the integers with distinct colors.


program main
 implicit none
 integer n,val,ind,i,j
 integer, allocatable::a(:)
 read(*,*) n
 allocate(a(1:n))
 do i=1,n
  a(i) = -1
 end do
 read(*,*) val
 ind = 1
 a(1) = val
 do i=2,n
  read(*,*) val
  if(a(ind) >= val) then
   a(ind+1) = val
   ind = ind+1
  else
   do j=1,ind
    if(a(j) < val) then
     a(j) = val
     exit
    end if
   end do
  end if
 end do
 write(*,*) ind
end program main


program name
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: n,n1,n2
    integer(int32), allocatable:: a(:)

    read*, n

    n1 = n/3
    n2 = 2*n1
    allocate(a(n))
    read*, a(:)
    call merge_sort(a,1,n)
    if (a(n)==0) then
        print'(a)', 'Yes'
        stop
    end if
    if (mod(n,3) /= 0) then
        print'(a)', 'No'
        stop
    end if

    if (a(n1) == 0 .and. a(n1+1) > 0 .and. a(n1+1) == a(n)) then
        print'(a)', 'Yes'
        stop
    end if

    if (a(1)==(a(n1)) .and. a(n1+1)==a(n2) .and. a(n2+1)==a(n)) then
        if (xor(a(1),a(n1+1)) == a(n2+1)) then
            print'(a)', 'Yes'
            stop
        end if
    end if

    print'(a)', 'No'
contains
    recursive subroutine merge_sort(ar, fst, lst)
        integer(int32),intent(inout):: ar(:)
        integer(int32),intent(in):: fst,lst
        integer(int32):: mdl

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

        mdl = (fst+lst)/2
        call merge_sort(ar, fst, mdl)
        call merge_sort(ar, mdl+1, lst)
        call merge_(ar, fst, mdl, lst)
    end subroutine

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

        allocate(tmp(lst-fst+1))

        li=fst
        ri=mdl+1
        ti=1

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

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

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

    subroutine swap(x,y)
        integer(int32),intent(inout):: x,y
        integer(int32):: tmp
        tmp = x
        x = y
        y = tmp
    end subroutine
end program name

Score : 300 points

Problem Statement

Snuke has N hats. The i-th hat has an integer a_i written on it.

There are N camels standing in a circle.
Snuke will put one of his hats on each of these camels.

If there exists a way to distribute the hats to the camels such that the following condition is satisfied for every camel, print Yes; otherwise, print No.

The bitwise XOR of the numbers written on the hats on both adjacent camels is equal to the number on the hat on itself.

What is XOR?

The bitwise XOR x_1 \oplus x_2 \oplus \ldots \oplus x_n of n non-negative integers x_1, x_2, \ldots, x_n is defined as follows:

- When x_1 \oplus x_2 \oplus \ldots \oplus x_n is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if the number of integers among x_1, x_2, \ldots, x_n whose binary representations have 1 in the 2^k's place is odd, and 0 if that count is even.

For example, 3 \oplus 5 = 6.

Constraints

All values in input are integers.

3 \leq N \leq 10^{5}

0 \leq a_i \leq 10^{9}

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 \ldots a_{N}

Output

Print the answer.

Sample Input 1

3
1 2 3

Sample Output 1

Yes

If we put the hats with 1, 2, and 3 in this order, clockwise, the condition will be satisfied for every camel, so the answer is Yes.

Sample Input 2

4
1 2 4 8

Sample Output 2

No

There is no such way to distribute the hats; the answer is No.


program name
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: n,i,t
    integer(int32), allocatable:: a(:)

    read*, n

    if (mod(n,3) /= 0) then
        print'(a)', 'No'
        stop
    end if

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

    t=1
    do i=1,n-1
        if (a(i) == a(i+1)) cycle
        if (n/3*t==i) then
            t=t+1
        else
            print'(a)', 'No'
            stop
        end if
    end do

    print'(a)', 'Yes'

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

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

        mdl = (fst+lst)/2
        call merge_sort(ar, fst, mdl)
        call merge_sort(ar, mdl+1, lst)
        call merge_(ar, fst, mdl, lst)
    end subroutine

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

        allocate(tmp(lst-fst+1))

        li=fst
        ri=mdl+1
        ti=1

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

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

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

    subroutine swap(x,y)
        integer(int32),intent(inout):: x,y
        integer(int32):: tmp
        tmp = x
        x = y
        y = tmp
    end subroutine
end program name


program kadai
implicit none
integer  ::  n,a,b
integer  ::  ans

read(*,*) n,a,b

if (n*a>b) then
    ans=b
else
    ans=n*a
end if
write(*,*) ans

stop

end program kadai

Score : 100 points

Problem Statement

N of us are going on a trip, by train or taxi.

The train will cost each of us A yen (the currency of Japan).

The taxi will cost us a total of B yen.

How much is our minimum total travel expense?

Constraints

All values in input are integers.

1 \leq N \leq 20

1 \leq A \leq 50

1 \leq B \leq 50

Input

Input is given from Standard Input in the following format:

N A B

Output

Print an integer representing the minimum total travel expense.

Sample Input 1

4 2 9

Sample Output 1

8

The train will cost us 4 \times 2 = 8 yen, and the taxi will cost us 9 yen, so the minimum total travel expense is 8 yen.

Sample Input 2

4 2 7

Sample Output 2

7

Sample Input 3

4 2 8

Sample Output 3

8


module ABC133

  ! <module>s to import
  use, intrinsic :: iso_fortran_env

  ! require all variables to be explicitly declared
  implicit none

  ! accessibility of <subroutine>s and <function>s in this <module>
  public  :: task_B
  private :: calc_distance2
  private :: is_GoodDistance

  ! variables for this <module>
  integer(INT32) :: num_point_all
  integer(INT32) :: num_point_GD
  integer(INT32) :: val_dimension

  ! arrays for this <module>
  integer(INT32), dimension(:,:), allocatable :: coordinate

  ! contained <subroutine>s and <function>s are below
  contains

  subroutine task_B

    ! support variables for this <subroutine>
    integer(INT32) :: itr
    integer(INT32) :: itr_orign, itr_target

    ! STEP.01
    ! read out the given data (1/2)
    read(unit=INPUT_UNIT, fmt=*) num_point_all, val_dimension

    ! STEP.02
    ! allocate the array to store the data of the points
    allocate( coordinate(1:val_dimension, 1:num_point_all) )

    ! STEP.03
    ! read out the given data (1/2)
    do itr = 1, num_point_all, 1
      read(unit=INPUT_UNIT, fmt=*) coordinate(:, itr)
    end do

    ! STEP.04
    ! calculate the answer of this task
    num_point_GD = 0_INT32

    do itr_orign  = 1,             num_point_all, 1
    do itr_target = itr_orign + 1, num_point_all, 1

      if ( is_GoodDistance( calc_distance2( coordinate(:, itr_orign), coordinate(:, itr_target) ) ) ) then
        num_point_GD = num_point_GD + 1_INT32
      else
        continue
      end if

    end do
    end do

    ! STEP.05
    ! output the answer of this task
    write(unit=OUTPUT_UNIT, fmt='(I0)', advance='yes') num_point_GD

    ! STEP.06
    ! deallocate the array to store the data of the points
    deallocate( coordinate )

    ! STEP.END
    return

  end subroutine task_B

  pure function calc_distance2 (coordinate_orign, coordinate_target) result(distance)

    ! arguments for this <function>
    integer(INT32), dimension(1:val_dimension), intent(in) :: coordinate_orign
    integer(INT32), dimension(1:val_dimension), intent(in) :: coordinate_target

    ! return value of this <function>
    integer(INT32) :: distance

    ! support variables for this <function>
    integer(INT32) :: difference
    integer(INT32) :: itr

    distance = 0_INT32

    do itr = 1, val_dimension, 1
      difference = coordinate_target(itr) - coordinate_orign(itr)
      distance   = distance + difference * difference
    end do

    return

  end function calc_distance2

  pure function is_GoodDistance (val_distance) result(stat)

    ! arguments for this <function>
    integer(INT32), intent(in) :: val_distance

    ! return value of this <function>
    logical :: stat

    ! support variables for this <subroutine>
    integer(INT32) :: itr, itr2

    stat = .false.

    do itr = 0_INT32, val_distance, 1_INT32

      itr2 = itr * itr

      if (itr2 .eq. val_distance) then
        stat = .true.
        exit
      end if

    end do

    return

  end function is_GoodDistance

end module ABC133

program main

  ! <module>s to import
  use, non_intrinsic :: ABC133

  ! require all variables to be explicitly declared
  implicit none

  call task_B

end program main

Score : 200 points

Problem Statement

There are N points in a D-dimensional space.

The coordinates of the i-th point are (X_{i1}, X_{i2}, ..., X_{iD}).

The distance between two points with coordinates (y_1, y_2, ..., y_D) and (z_1, z_2, ..., z_D) is \sqrt{(y_1 - z_1)^2 + (y_2 - z_2)^2 + ... + (y_D - z_D)^2}.

How many pairs (i, j) (i < j) are there such that the distance between the i-th point and the j-th point is an integer?

Constraints

All values in input are integers.

2 \leq N \leq 10

1 \leq D \leq 10

-20 \leq X_{ij} \leq 20

No two given points have the same coordinates. That is, if i \neq j, there exists k such that X_{ik} \neq X_{jk}.

Input

Input is given from Standard Input in the following format:

N D
X_{11} X_{12} ... X_{1D}
X_{21} X_{22} ... X_{2D}
\vdots
X_{N1} X_{N2} ... X_{ND}

Output

Print the number of pairs (i, j) (i < j) such that the distance between the i-th point and the j-th point is an integer.

Sample Input 1

3 2
1 2
5 5
-2 8

Sample Output 1

1

The number of pairs with an integer distance is one, as follows:

The distance between the first point and the second point is \sqrt{|1-5|^2 + |2-5|^2} = 5, which is an integer.

The distance between the second point and the third point is \sqrt{|5-(-2)|^2 + |5-8|^2} = \sqrt{58}, which is not an integer.

The distance between the third point and the first point is \sqrt{|-2-1|^2+|8-2|^2} = 3\sqrt{5}, which is not an integer.

Sample Input 2

3 4
-3 7 8 2
-12 1 10 2
-2 8 9 3

Sample Output 2

2

Sample Input 3

5 1
1
2
3
4
5

Sample Output 3

10


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

  read *, n
  allocate(a(n),ans(n))
  read *, a
  b = reshape(a(2:),[2,n/2])
  x = sum(a) - sum(b(1,:)) * 2
  ans(1) = x
  do i = 1,n-1
     x = a(i) * 2 - x
     ans(i+1) = x
  end do
  print *, ans
end program abc133d

Score : 400 points

Problem Statement

There are N mountains in a circle, called Mountain 1, Mountain 2, ..., Mountain N in clockwise order. N is an odd number.

Between these mountains, there are N dams, called Dam 1, Dam 2, ..., Dam N. Dam i (1 \leq i \leq N) is located between Mountain i and i+1 (Mountain N+1 is Mountain 1).

When Mountain i (1 \leq i \leq N) receives 2x liters of rain, Dam i-1 and Dam i each accumulates x liters of water (Dam 0 is Dam N).

One day, each of the mountains received a non-negative even number of liters of rain.

As a result, Dam i (1 \leq i \leq N) accumulated a total of A_i liters of water.

Find the amount of rain each of the mountains received. We can prove that the solution is unique under the constraints of this problem.

Constraints

All values in input are integers.

3 \leq N \leq 10^5-1

N is an odd number.

0 \leq A_i \leq 10^9

The situation represented by input can occur when each of the mountains receives a non-negative even number of liters of rain.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print N integers representing the number of liters of rain Mountain 1, Mountain 2, ..., Mountain N received, in this order.

Sample Input 1

3
2 2 4

Sample Output 1

4 0 4

If we assume Mountain 1, 2, and 3 received 4, 0, and 4 liters of rain, respectively, it is consistent with this input, as follows:

Dam 1 should have accumulated \frac{4}{2} + \frac{0}{2} = 2 liters of water.

Dam 2 should have accumulated \frac{0}{2} + \frac{4}{2} = 2 liters of water.

Dam 3 should have accumulated \frac{4}{2} + \frac{4}{2} = 4 liters of water.

Sample Input 2

5
3 8 7 5 5

Sample Output 2

2 4 12 2 8

Sample Input 3

3
1000000000 1000000000 0

Sample Output 3

0 2000000000 0


program main
  implicit none
  integer(8) i,N,n_min,j,t1
  integer(8),allocatable :: A(:)
  integer(8),allocatable :: rain(:)
  integer,allocatable :: V(:,:)
  read(*, *) N
  allocate(A(N))
  allocate(rain(N))
  allocate(V(N,N))
  read(*, *) (A(i), i = 1,N)
  V = 1
  do i = 1,N
    do j = 1,(N-1)/2
      if (i+2*j-1>N) then
        t1 = mod(i+2*j-1,N)
      else
        t1 = i+2*j-1
      end if
      V(i,t1) = -1
    end do
  end do
  rain = 0
  do i = 1,N
    do j = 1,N
      rain(i) = rain(i) + A(j)*V(i,j)
    end do
  end do
  rain = rain
  write(*,*) rain
end program main


program fifty_fifty
    implicit none
    integer(4):: i,ai,alphabet_counts(26) = 0, count
    character(4):: s

    read*, s

    do i=1,4
        ai=ichar(s(i:i))-64
        alphabet_counts(ai)=alphabet_counts(ai)+1
    end do

    do i=1,26
        count = alphabet_counts(i)
        if (count /= 2 .and. count/= 0) then
            print*, 'No'
            stop
        end if
    end do

    print*, 'Yes'
end program fifty_fifty

Score : 100 points

Problem Statement

You are given a 4-character string S consisting of uppercase English letters.
Determine if S consists of exactly two kinds of characters which both appear twice in S.

Constraints

The length of S is 4.

S consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If S consists of exactly two kinds of characters which both appear twice in S, print Yes; otherwise, print No.

Sample Input 1

ASSA

Sample Output 1

Yes

S consists of A and S which both appear twice in S.

Sample Input 2

STOP

Sample Output 2

No

Sample Input 3

FFEE

Sample Output 3

Yes

Sample Input 4

FREE

Sample Output 4

No


program aaa
implicit none

integer :: one, two, three, four
integer :: a, b, c
character(4) :: s

read*, s

one=iachar(s(1:1))  + 1000
two=iachar(s(2:2))  + 1000
three=iachar(s(3:3))+ 1000
four=iachar(s(4:4)) + 1000

a=(one-two)+(three-four)
b=(one-three)+(two-four)
c=(one-four)+(two-three)

if(a==0.or.b==0.or.c==0) then
write(*,'(a3)') 'Yes'
else
write(*,'(a2)') 'No'
end if
end program


program main
  implicit none
  character(4) S
  integer i,j,ans,x
  !read(*, *) (L(i), i = 1,N)
  read(*, *) S
  ans = 1
  do i = 1,4
    x = 0
    do j = 1,4
      if(S(i:i) == S(j:j)) then
        x = x + 1
      end if

    end do
    if(x < 2) then
      ans = 0
    end if
  end do

  if (ans == 0) then
    write(*, *) 'No'
  else
    write(*, *) 'Yes'
  end if
end program main


program fifty_fifty
  implicit none
  character(4) :: s
  integer :: a(26), i
  a = 0
  read(*,*) s
  do i = 1, 4
    a(ichar(s(i:i))-64) = a(ichar(s(i:i))-64)+1
  end do
  if (maxval(a).eq.2.and.minval(a,a.gt.0).eq.2) then
    write(*,'(a)') "Yes"
  else
    write(*,'(a)') "No"
  end if
  stop
end program fifty_fifty


program ABC132B
    implicit none
    integer::n,i,ans=0
    integer,allocatable::P(:)
    read*,n
    allocate(P(n))
    read*,P
    do i=1,n-2
        if(P(i+1)/=maxval(P(i:i+2)).and.P(i+1)/=minval(P(i:i+2)))ans=ans+1
    end do
    print'(i0)',ans
end program ABC132B

Score : 200 points

Problem Statement

We have a permutation p = {p_1,\ p_2,\ ...,\ p_n} of {1,\ 2,\ ...,\ n}.

Print the number of elements p_i (1 < i < n) that satisfy the following condition:

p_i is the second smallest number among the three numbers p_{i - 1}, p_i, and p_{i + 1}.

Constraints

All values in input are integers.

3 \leq n \leq 20

p is a permutation of {1,\ 2,\ ...,\ n}.

Input

Input is given from Standard Input in the following format:

n
p_1 p_2 ... p_n

Output

Print the number of elements p_i (1 < i < n) that satisfy the condition.

Sample Input 1

5
1 3 5 4 2

Sample Output 1

2

p_2 = 3 is the second smallest number among p_1 = 1, p_2 = 3, and p_3 = 5. Also, p_4 = 4 is the second smallest number among p_3 = 5, p_4 = 4, and p_5 = 2. These two elements satisfy the condition.

Sample Input 2

9
9 6 3 2 5 8 7 4 1

Sample Output 2

5


integer :: n,p(20)
integer :: i,ans

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

ans = 0

do i = 2,n-1
  if( (p(i-1)<p(i) .and. p(i)<p(i+1)) .or.&
      (p(i-1)>p(i) .and. p(i)>p(i+1))) ans = ans + 1
end do
print*,ans
end program


program Divide_the_problems
    implicit none
    integer(4):: n
    integer(4),allocatable:: d(:)

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

    call merge_sort(d,1,n)
    print*, d(n/2+1)-d(n/2)
contains
    recursive subroutine merge_sort(ar, fst, lst)
        integer(4),intent(inout):: ar(:)
        integer(4),intent(in):: fst,lst
        integer(4):: mdl

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

        mdl = (fst+lst)/2
        call merge_sort(ar,fst,mdl)
        call merge_sort(ar,mdl+1,lst)
        call merge_(ar,fst,mdl,lst)
    end subroutine

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

        allocate(tmp(lst-fst+1))

        li=fst
        ri=mdl+1
        ti=1

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

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

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

    subroutine swap(x,y)
        integer(4),intent(inout):: x,y
        integer(4):: t
        t=x
        x=y
        y=t
    end subroutine
end program Divide_the_problems

Score : 300 points

Problem Statement

Takahashi made N problems for competitive programming.
The problems are numbered 1 to N, and the difficulty of Problem i is represented as an integer d_i (the higher, the harder).

He is dividing the problems into two categories by choosing an integer K, as follows:

A problem with difficulty K or higher will be for ARCs.

A problem with difficulty lower than K will be for ABCs.

How many choices of the integer K make the number of problems for ARCs and the number of problems for ABCs the same?

Problem Statement

2 \leq N \leq 10^5

N is an even number.

1 \leq d_i \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
d_1 d_2 ... d_N

Output

Print the number of choices of the integer K that make the number of problems for ARCs and the number of problems for ABCs the same.

Sample Input 1

6
9 1 4 4 6 7

Sample Output 1

2

If we choose K=5 or 6, Problem 1, 5, and 6 will be for ARCs, Problem 2, 3, and 4 will be for ABCs, and the objective is achieved.
Thus, the answer is 2.

Sample Input 2

8
9 1 14 5 5 4 4 14

Sample Output 2

0

There may be no choice of the integer K that make the number of problems for ARCs and the number of problems for ABCs the same.

Sample Input 3

14
99592 10342 29105 78532 83018 11639 92015 77204 30914 21912 34519 80835 100000 1

Sample Output 3

42685


program abc132c
  implicit none

  integer :: n, diff
  integer, allocatable :: d(:)

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

  call heapsort(n, d(:))

  diff =  d(n/2+1) - d(n/2)
  print *, diff
  deallocate( d )
  stop

contains

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

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

    if(n.le.0)then
       write(6,*)"Error, at heapsort"; stop
    endif
    if(n.eq.1)return

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

    return
  end subroutine heapsort

end program abc132c


program main
  implicit none
  integer(8) :: n, k, i, m, res
  read (*, *) n, k
  m = 10 ** 9 + 7
  do i = 1, k
    if (n - k - i + 1 < 0) then
      res = 0
    else
      res = combi(k - 1, k - i, m) * combi(n - k + 1, n - k - i + 1, m)
      res = mod(res, m)
    end if
    write (*, "(i0)") res
  end do
  contains

  function combi(n, k, m)
    implicit none
    integer(8), intent(in) :: n, k, m
    integer(8) :: combi, numera, denomi
    numera = perm(n, k, m)
    denomi = mod(fact(k, m), m)
    combi = mod(numera * inv(denomi, m), m)
  end function combi

  function perm(n, k, m)
    integer(8), intent(in) :: n, k, m
    integer(8) :: i, perm
    perm = 1
    do i = n, n - k + 1, -1
      perm = mod(perm * i, m)
    end do
  end function perm

  function fact(n, m)
    implicit none
    integer(8), intent(in) :: n, m
    integer(8) :: i, fact
    fact = 1
    do i = 1, n
      fact = mod(fact * i, m)
    end do
  end function fact

  function inv(n, m)
    implicit none
    integer(8), intent(in) :: n, m
    integer(8) :: inv
    inv = pow(n, m - 2, m)
  end function inv

  recursive function pow(n, p, m) result(res)
    implicit none
    integer(8), intent(in) :: n, p, m
    integer(8) :: res
    if (p == 0) then
      res = 1
    else if (mod(p, 2_8) == 0) then
      res = mod(pow(n, p / 2, m) ** 2, m)
    else
      res = mod(pow(n, p - 1, m) * n, m)
    end if
  end function pow
end program main

Score : 400 points

Problem Statement

There are K blue balls and N-K red balls. The balls of the same color cannot be distinguished. Snuke and Takahashi are playing with these balls.

First, Snuke will arrange the N balls in a row from left to right.

Then, Takahashi will collect only the K blue balls. In one move, he can collect any number of consecutive blue balls. He will collect all the blue balls in the fewest moves possible.

How many ways are there for Snuke to arrange the N balls in a row so that Takahashi will need exactly i moves to collect all the blue balls? Compute this number modulo 10^9+7 for each i such that 1 \leq i \leq K.

Constraints

1 \leq K \leq N \leq 2000

Input

Input is given from Standard Input in the following format:

N K

Output

Print K lines. The i-th line (1 \leq i \leq K) should contain the number of ways to arrange the N balls so that Takahashi will need exactly i moves to collect all the blue balls, modulo 10^9+7.

Sample Input 1

5 3

Sample Output 1

3
6
1

There are three ways to arrange the balls so that Takahashi will need exactly one move: (B, B, B, R, R), (R, B, B, B, R), and (R, R, B, B, B). (R and B stands for red and blue, respectively).

There are six ways to arrange the balls so that Takahashi will need exactly two moves: (B, B, R, B, R), (B, B, R, R, B), (R, B, B, R, B), (R, B, R, B, B), (B, R, B, B, R), and (B, R, R, B, B).

There is one way to arrange the balls so that Takahashi will need exactly three moves: (B, R, B, R, B).

Sample Input 2

2000 3

Sample Output 2

1998
3990006
327341989

Be sure to print the numbers of arrangements modulo 10^9+7.


program BlueandRedBalls
    implicit none
    integer(8):: n, k, i
    integer(8), allocatable:: frac(:), ifrac(:)
    integer(8), parameter:: md = 1000000007

    read*, n, k

    call make_frac()

    do i=1,k
        print*, mod(comb(n-k+1,i)*comb(k-1,i-1),md)
    end do

contains

function comb(a,b) result(ret)
    integer(8):: a,b
    integer(8):: ret

    ret = mod(mod(frac(a)*ifrac(b), md)*ifrac(a-b),md)
end function

subroutine make_frac
    integer(8):: i
    allocate(frac(0:n), ifrac(0:n))

    frac(0) = 1
    ifrac(0) = 1

    do i=1,n
        frac(i) = mod(frac(i-1)*i, md)
        ifrac(i) = inv(frac(i))
    end do
end subroutine

function inv(x) result(ret)
    integer(8):: x, ret
    integer(8):: num, mul, pow

    num = md-2
    mul = x
    ret = 1

    do while (num >= 1)
        if (mod(num,2) == 1) then
            ret = mod(ret * mul, md)
        end if
        num=num/2
        mul=mod(mul*mul, md)
    end do
end function
end program BlueandRedBalls


module mod_t_item
  implicit none
  type :: t_item
    integer :: pos, cost
  end type t_item
  private
  public :: t_item, itemize, compare, swap
contains
  function itemize(pos,cost) result(i)
    implicit none
    integer :: pos, cost
    type(t_item) :: i
    i%pos = pos
    i%cost = cost
    return
  end function itemize
  function compare(i1,i2) result(c)
    implicit none
    type(t_item), intent(in) :: i1, i2
    integer :: c
    c = i1%cost-i2%cost
    return
  end function compare
  subroutine swap(i1,i2)
    implicit none
    type(t_item), intent(inout) :: i1, i2
    type(t_item) :: i
    i = i1
    i1 = i2
    i2 = i
    return
  end subroutine swap
end module mod_t_item
module mod_t_list
  use mod_t_item
  implicit none
  type :: t_list
    integer :: vol
    type(t_item), pointer :: arr(:)
  end type t_list
  private
  public :: t_list, init_list, release_list,push_back
  public :: pop_back, get_at, size_of_list
contains
  subroutine init_list(list)
    implicit none
    type(t_list), intent(inout) :: list
    list%vol = 0
    allocate(list%arr(1))
    return
  end subroutine init_list
  subroutine release_list(list)
    implicit none
    type(t_list), intent(inout) :: list
    if (associated(list%arr)) deallocate(list%arr)
    return
  end subroutine release_list
  subroutine push_back(list,item)
    implicit none
    type(t_list), intent(inout) :: list
    type(t_item), intent(in) :: item
    type(t_item), allocatable :: tmp(:)
    if (size(list%arr).eq.list%vol) then
      allocate(tmp(list%vol))
      tmp = list%arr
      deallocate(list%arr)
      allocate(list%arr(2*list%vol))
      list%arr(1:list%vol) = tmp
      deallocate(tmp)
    end if
    list%vol = list%vol+1
    list%arr(list%vol) = item
    return
  end subroutine push_back
  function pop_back(list) result(item)
    implicit none
    type(t_list), intent(inout) :: list
    type(t_item) :: item
    item = list%arr(list%vol)
    list%vol = list%vol-1
    return
  end function pop_back
  function get_at(list,i) result(item)
    implicit none
    type(t_list), intent(in) :: list
    integer, intent(in) :: i
    type(t_item) :: item
    item = list%arr(i)
    return
  end function get_at
  function size_of_list(list) result(s)
    implicit none
    type(t_list), intent(in) :: list
    integer :: s
    s = list%vol
    return
  end function size_of_list
end module mod_t_list
module mod_t_priority_queue
  use mod_t_item
  use mod_t_list
  implicit none
  type :: t_heap
    type(t_list) :: list
  end type t_heap
  private
  public :: t_heap, init_heap, release_heap, push, pop, size_of_heap
contains
  subroutine init_heap(heap)
    implicit none
    type(t_heap), intent(inout) :: heap
    call init_list(heap%list)
    return
  end subroutine init_heap
  subroutine release_heap(heap)
    implicit none
    type(t_heap), intent(inout) :: heap
    call release_list(heap%list)
    return
  end subroutine release_heap
  subroutine push(heap,item)
    implicit none
    type(t_heap), intent(inout) :: heap
    type(t_item), intent(in) :: item
    integer :: n, i
    call push_back(heap%list,item)
    n = heap%list%vol
    do while (n.gt.1)
      i = n/2
      if (compare(heap%list%arr(n),heap%list%arr(i)).lt.0_8) then
        call swap(heap%list%arr(n),heap%list%arr(i))
      end if
      n = i
    end do
    return
  end subroutine push
  function pop(heap) result(item)
    implicit none
    type(t_heap), intent(inout) :: heap
    type(t_item) :: item, tmp
    integer :: n, i, j
    n = heap%list%vol
    item = heap%list%arr(1)
    heap%list%arr(1) = heap%list%arr(n)
    tmp = pop_back(heap%list)
    i = 1
    do while (2*i.lt.n)
      j = 2*i
      if ((j.lt.n-1).and.(compare(heap%list%arr(j+1),heap%list%arr(j)).lt.0_8)) then
        j = j+1
      end if
      if (compare(heap%list%arr(j),heap%list%arr(i)).lt.0_8) then
        call swap(heap%list%arr(j),heap%list%arr(i))
      end if
      i = j
    end do
    return
  end function pop
  function size_of_heap(heap) result(s)
    implicit none
    type(t_heap), intent(in) :: heap
    integer :: s
    s = size_of_list(heap%list)
    return
  end function size_of_heap
end module mod_t_priority_queue
module mod_t_graph
  use mod_t_item
  use mod_t_list
  use mod_t_priority_queue
  implicit none
  integer, parameter :: infty = 1000000000
  type :: t_graph
    type(t_list), pointer :: edges(:)
  end type t_graph
  private
  public :: infty
  public :: t_graph, init_graph, release_graph, add_edge, add, dijkstra, dfs
contains
  subroutine init_graph(graph,n)
    implicit none
    type(t_graph), intent(inout) :: graph
    integer, intent(in) :: n
    integer :: i
    allocate(graph%edges(n))
    do i = 1, n
      call init_list(graph%edges(i))
    end do
    return
  end subroutine init_graph
  subroutine release_graph(graph)
    implicit none
    type(t_graph), intent(inout) :: graph
    integer :: n, i
    if (.not.associated(graph%edges)) return
    n = size(graph%edges)
    do i = 1, n
      call release_list(graph%edges(i))
    end do
    deallocate(graph%edges)
    return
  end subroutine release_graph
  subroutine add_edge(graph,i,edge)
    implicit none
    type(t_graph), intent(inout) :: graph
    type(t_item), intent(in) :: edge
    integer, intent(in) :: i
    call push_back(graph%edges(i),edge)
    return
  end subroutine add_edge
  subroutine add(graph,i,pos,cost)
    implicit none
    type(t_graph), intent(inout) :: graph
    integer, intent(in) :: i, pos, cost
    call push_back(graph%edges(i),itemize(pos,cost))
    return
  end subroutine add
  function dijkstra(graph,start) result(c)
    implicit none
    type(t_graph), intent(in) :: graph
    integer, intent(in) :: start
    type(t_heap) :: pq
    type(t_item) :: now, next
    integer :: n, i, c(size(graph%edges))
    n = size(graph%edges)
    c = infty
    call init_heap(pq)
    c(start) = 0
    call push(pq,itemize(start,c(start)))
    do while (size_of_heap(pq).gt.0)
      now = pop(pq)
      if (now%cost.gt.c(now%pos)) cycle
      do i = 1, size_of_list(graph%edges(now%pos))
        next = get_at(graph%edges(now%pos),i)
        if (c(next%pos).gt.c(now%pos)+next%cost) then
          c(next%pos) = c(now%pos)+next%cost
          call push(pq,itemize(next%pos,c(next%pos)))
        end if
      end do
    end do
    call release_heap(pq)
    return
  end function dijkstra
  recursive subroutine dfs(graph,newgraph,s,v,c)
    implicit none
    type(t_graph), intent(in) :: graph
    type(t_graph), intent(inout) :: newgraph
    integer, intent(in) :: s, v, c
    type(t_item) :: next
    integer :: i
    if (c.eq.3) then
      call add(newgraph,s,v,1)
      return
    end if
    do i = 1, size_of_list(graph%edges(v))
      next = get_at(graph%edges(v),i)
      call dfs(graph,newgraph,s,next%pos,c+1)
    end do
    return
  end subroutine dfs
end module mod_t_graph
program hopscotch_addict
  use mod_t_graph
  implicit none
  type(t_graph) :: org, kng
  integer :: n, m, u, v, s, t, d(100000), i, k
  d = 0
  read(*,*) n, m
  call init_graph(org,n)
  call init_graph(kng,n)
  do i = 1, m
    read(*,*) u, v
    call add(org,u,v,1)
  end do
  read(*,*) s, t
  do i = 1, n
    call dfs(org,kng,i,i,0)
  end do
  d(1:n) = dijkstra(kng,s)
  if (d(t).eq.infty) then
    write(*,'(i0)') -1
  else
    write(*,'(i0)') d(t)
  end if
  call release_graph(org)
  call release_graph(kng)
  stop
end program hopscotch_addict

Score : 500 points

Problem Statement

Ken loves ken-ken-pa (Japanese version of hopscotch). Today, he will play it on a directed graph G.
G consists of N vertices numbered 1 to N, and M edges. The i-th edge points from Vertex u_i to Vertex v_i.

First, Ken stands on Vertex S. He wants to reach Vertex T by repeating ken-ken-pa. In one ken-ken-pa, he does the following exactly three times: follow an edge pointing from the vertex on which he is standing.

Determine if he can reach Vertex T by repeating ken-ken-pa. If the answer is yes, find the minimum number of ken-ken-pa needed to reach Vertex T. Note that visiting Vertex T in the middle of a ken-ken-pa does not count as reaching Vertex T by repeating ken-ken-pa.

Constraints

2 \leq N \leq 10^5

0 \leq M \leq \min(10^5, N (N-1))

1 \leq u_i, v_i \leq N(1 \leq i \leq M)

u_i \neq v_i (1 \leq i \leq M)

If i \neq j, (u_i, v_i) \neq (u_j, v_j).

1 \leq S, T \leq N

S \neq T

Input

Input is given from Standard Input in the following format:

N M
u_1 v_1
:
u_M v_M
S T

Output

If Ken cannot reach Vertex T from Vertex S by repeating ken-ken-pa, print -1.
If he can, print the minimum number of ken-ken-pa needed to reach vertex T.

Sample Input 1

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

Sample Output 1

2

Ken can reach Vertex 3 from Vertex 1 in two ken-ken-pa, as follows: 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 in the first ken-ken-pa, then 4 \rightarrow 1 \rightarrow 2 \rightarrow 3 in the second ken-ken-pa. This is the minimum number of ken-ken-pa needed.

Sample Input 2

3 3
1 2
2 3
3 1
1 2

Sample Output 2

-1

Any number of ken-ken-pa will bring Ken back to Vertex 1, so he cannot reach Vertex 2, though he can pass through it in the middle of a ken-ken-pa.

Sample Input 3

2 0
1 2

Sample Output 3

-1

Vertex S and Vertex T may be disconnected.

Sample Input 4

6 8
1 2
2 3
3 4
4 5
5 1
1 4
1 5
4 6
1 6

Sample Output 4

2


program main
character::S*4
integer::i,check
read*,S
check=0
do i=1,3
	if(S(i:i)==S(i+1:i+1))then
    	check=check+1
    else
    	check=check
    end if
end do
if(check==0)then
print*,'Good'
else
print*,'Bad'
end if
end program main

Score : 100 points

Problem Statement

The door of Snuke's laboratory is locked with a security code.

The security code is a 4-digit number. We say the security code is hard to enter when it contains two consecutive digits that are the same.

You are given the current security code S. If S is hard to enter, print Bad; otherwise, print Good.

Constraints

S is a 4-character string consisting of digits.

Input

Input is given from Standard Input in the following format:

S

Output

If S is hard to enter, print Bad; otherwise, print Good.

Sample Input 1

3776

Sample Output 1

Bad

The second and third digits are the same, so 3776 is hard to enter.

Sample Input 2

8080

Sample Output 2

Good

There are no two consecutive digits that are the same, so 8080 is not hard to enter.

Sample Input 3

1333

Sample Output 3

Bad

Sample Input 4

0024

Sample Output 4

Bad


program main
 implicit none
 character*80 s,a
 integer*8 i,j

 read(*,*) s
 a = s(1:1)
 j = 0
 do i=2,len_trim(s)
  if (a == s(i:i))then
   j = 1
  end if
  a = s(i:i)
 end do
 if(j==0) then
  write(*,*) 'Good'
 else
  write(*,*) 'Bad'
 end if
end program main


program a
implicit none
character(4) s
character(1) i, j
integer n
read*, s
 do n=1, 3
 i=s(n:n)
 j=s(n+1:n+1)
 if (i.eq.j) then
 print*, "Bad"
 return
 end if
 end do
print*, "Good"
end program


program main
implicit none
integer :: i
character :: s*4

read(*,*) s
do i = 1, 3
  if( s(i+1:i+1) == s(i:i) ) then
     write(*,*) 'Bad'
     stop
  end if
end do
write(*,*) 'Good'

end program main


program pie
  implicit none
  integer ::N,L,ful,kake,i,sa
  integer :: tabe

  read(*,*) N,L
  ful=L*N+N*(N-1)/2
  sa=N*1000
  do i=1,N
     kake=ful-L-(i-1)
     if(abs(kake-ful).lt.sa) then
        sa=abs(kake-ful)
        tabe=kake
     endif
  enddo
  write(*,'(i0)') tabe

end program pie

Score : 200 points

Problem Statement

You have N apples, called Apple 1, Apple 2, Apple 3, ..., Apple N. The flavor of Apple i is L+i-1, which can be negative.

You can make an apple pie using one or more of the apples. The flavor of the apple pie will be the sum of the flavors of the apples used.

You planned to make an apple pie using all of the apples, but being hungry tempts you to eat one of them, which can no longer be used to make the apple pie.

You want to make an apple pie that is as similar as possible to the one that you planned to make. Thus, you will choose the apple to eat so that the flavor of the apple pie made of the remaining N-1 apples will have the smallest possible absolute difference from the flavor of the apple pie made of all the N apples.

Find the flavor of the apple pie made of the remaining N-1 apples when you choose the apple to eat as above.

We can prove that this value is uniquely determined.

Constraints

2 \leq N \leq 200

-100 \leq L \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N L

Output

Find the flavor of the apple pie made of the remaining N-1 apples when you optimally choose the apple to eat.

Sample Input 1

5 2

Sample Output 1

18

The flavors of Apple 1, 2, 3, 4, and 5 are 2, 3, 4, 5, and 6, respectively. The optimal choice is to eat Apple 1, so the answer is 3+4+5+6=18.

Sample Input 2

3 -1

Sample Output 2

0

The flavors of Apple 1, 2, and 3 are -1, 0, and 1, respectively. The optimal choice is to eat Apple 2, so the answer is (-1)+1=0.

Sample Input 3

30 -50

Sample Output 3

-1044


integer(8) A,B,C,D,E
integer(8) ans
read*,A,B,C,D
E=C*D/gcd(C,D)
ans=B-A+1

ans=ans-(B-A+1)/C-(B-A+1)/D +(B-A+1)/E
if(mod(B,C)<mod(A,C))ans=ans-1
if(mod(B,D)<mod(A,D))ans=ans-1
if(mod(B,E)<mod(A,E))ans=ans+1

print"(i0)",ans

contains

pure function gcd(a,b)
integer(8),intent(in)::a,b
integer(8) gcd
integer(8) s,t
s=a
gcd=b

t=mod(s,gcd)
do while(t/=0)
  s=gcd
  gcd=t
  t=mod(s,gcd)
end do
end function gcd
end

Score : 300 points

Problem Statement

You are given four integers A, B, C, and D. Find the number of integers between A and B (inclusive) that can be evenly divided by neither C nor D.

Constraints

1\leq A\leq B\leq 10^{18}

1\leq C,D\leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C D

Output

Print the number of integers between A and B (inclusive) that can be evenly divided by neither C nor D.

Sample Input 1

4 9 2 3

Sample Output 1

2

5 and 7 satisfy the condition.

Sample Input 2

10 40 6 8

Sample Output 2

23

Sample Input 3

314159265358979323 846264338327950288 419716939 937510582

Sample Output 3

532105071133627368


program main
implicit none
integer :: n
integer , allocatable :: a(:), b(:)
integer :: i, j, s

read(*,*) n
allocate( a(n) )
allocate( b(n) )
do i = 1, n
   read(*,*) a(i), b(i)
end do
call quicksort(n, b, a, 1, n)
s = 0.
do i = 1, n
   s = s + a(i)
   if(s .gt. b(i) ) then
      write(*,*) 'No'
      stop
   end if
end do
write(*,*) 'Yes'

stop
contains
recursive subroutine quicksort(n, a, b, first, last)
  implicit none
  integer :: a(n), b(n)
  integer :: first, last
  integer ::  i, j, n
  integer ::  x, y, t, s

  x = a( (first+last) / 2 )
  i = first
  j = last
  do
     do while (a(i) < x)
        i=i+1
     end do
     do while (x < a(j))
        j=j-1
     end do
     if (i >= j) exit
     t = a(i)
    a(i) = a(j)
    a(j) = t
    s = b(i)
    b(i) = b(j)
    b(j) = s
    i=i+1
    j=j-1
  end do
  if (first < i - 1) call quicksort(n, a, b, first, i - 1)
  if (j + 1 < last)  call quicksort(n, a, b, j + 1, last)
end subroutine quicksort
end program main

Score: 400 points

Problem Statement

Kizahashi, who was appointed as the administrator of ABC at National Problem Workshop in the Kingdom of AtCoder, got too excited and took on too many jobs.

Let the current time be time 0. Kizahashi has N jobs numbered 1 to N.

It takes A_i units of time for Kizahashi to complete Job i. The deadline for Job i is time B_i, and he must complete the job before or at this time.

Kizahashi cannot work on two or more jobs simultaneously, but when he completes a job, he can start working on another immediately.

Can Kizahashi complete all the jobs in time? If he can, print Yes; if he cannot, print No.

Constraints

All values in input are integers.

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

1 \leq A_i, B_i \leq 10^9 (1 \leq i \leq N)

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
.
.
.
A_N B_N

Output

If Kizahashi can complete all the jobs in time, print Yes; if he cannot, print No.

Sample Input 1

5
2 4
1 9
1 8
4 9
3 12

Sample Output 1

Yes

He can complete all the jobs in time by, for example, doing them in the following order:

Do Job 2 from time 0 to 1.

Do Job 1 from time 1 to 3.

Do Job 4 from time 3 to 7.

Do Job 3 from time 7 to 8.

Do Job 5 from time 8 to 11.

Note that it is fine to complete Job 3 exactly at the deadline, time 8.

Sample Input 2

3
334 1000
334 1000
334 1000

Sample Output 2

No

He cannot complete all the jobs in time, no matter what order he does them in.

Sample Input 3

30
384 8895
1725 9791
170 1024
4 11105
2 6
578 1815
702 3352
143 5141
1420 6980
24 1602
849 999
76 7586
85 5570
444 4991
719 11090
470 10708
1137 4547
455 9003
110 9901
15 8578
368 3692
104 1286
3 4
366 12143
7 6649
610 2374
152 7324
4 7042
292 11386
334 5720

Sample Output 3

Yes


program main
implicit none
integer :: n
integer , allocatable :: a(:), b(:)
integer :: i, j

read(*,*) n
allocate( a(n) )
allocate( b(n) )
do i = 1, n
   read(*,*) a(i), b(i)
end do
call quicksort(n, b, a, 1, n)
do i = 1, n
   if(sum(a(1:i)) .gt. b(i) ) then
      write(*,*) 'No'
      stop
   end if
end do
write(*,*) 'Yes'

stop
contains
recursive subroutine quicksort(n, a, b, first, last)
  implicit none
  integer :: a(n), b(n)
  integer :: first, last
  integer ::  i, j, n
  integer ::  x, y, t, s

  x = a( (first+last) / 2 )
  i = first
  j = last
  do
     do while (a(i) < x)
        i=i+1
     end do
     do while (x < a(j))
        j=j-1
     end do
     if (i >= j) exit
     t = a(i)
    a(i) = a(j)
    a(j) = t
    s = b(i)
    b(i) = b(j)
    b(j) = s
    i=i+1
    j=j-1
  end do
  if (first < i - 1) call quicksort(n, a, b, first, i - 1)
  if (j + 1 < last)  call quicksort(n, a, b, j + 1, last)
end subroutine quicksort
end program main


program friendships
  implicit none
  integer :: n, k, m, x, i, j
  read(*,*) n, k
  m = ((n-1)*(n-2))/2
  if (k.gt.m) then
    write(*,'(i0)') -1
    stop
  end if
  write(*,'(i0)') n-1+m-k
  do i = 2, n
    write(*,'(i0,x,i0)') 1, i
  end do
  x = 0
  do i = 2, n-1
    do j = i+1, n
      if (x.eq.m-k) stop
      x = x+1
      write(*,'(i0,x,i0)') i, j
    end do
  end do
  stop
end program friendships

Score: 500 points

Problem Statement

Does there exist an undirected graph with N vertices satisfying the following conditions?

The graph is simple and connected.

The vertices are numbered 1, 2, ..., N.

Let M be the number of edges in the graph. The edges are numbered 1, 2, ..., M, the length of each edge is 1, and Edge i connects Vertex u_i and Vertex v_i.

There are exactly K pairs of vertices (i,\ j)\ (i < j) such that the shortest distance between them is 2.

If there exists such a graph, construct an example.

Constraints

All values in input are integers.

2 \leq N \leq 100

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

Input

Input is given from Standard Input in the following format:

N K

Output

If there does not exist an undirected graph with N vertices satisfying the conditions, print -1.

If there exists such a graph, print an example in the following format (refer to Problem Statement for what the symbols stand for):

M
u_1 v_1
:
u_M v_M

If there exist multiple graphs satisfying the conditions, any of them will be accepted.

Sample Input 1

5 3

Sample Output 1

5
4 3
1 2
3 1
4 5
2 3

This graph has three pairs of vertices such that the shortest distance between them is 2: (1,\ 4), (2,\ 4), and (3,\ 5). Thus, the condition is satisfied.

Sample Input 2

5 8

Sample Output 2

-1

There is no graph satisfying the conditions.


module mod_union_find
  implicit none
  type union_find
    integer :: n
    integer, pointer :: par(:), rnk(:), siz(:)
  end type union_find
  private
  public :: union_find
  public :: init_union_find, release_union_find, find, same, unite, size_of
contains
  subroutine init_union_find(uf,n)
    implicit none
    type(union_find), intent(inout) :: uf
    integer, intent(in) :: n
    integer :: i
    allocate(uf%par(-n:n),uf%rnk(-n:n),uf%siz(-n:n))
    uf%rnk = 0
    uf%siz = 1
    do i = -n, n
      uf%par(i) = i
    end do
    return
  end subroutine init_union_find
  subroutine release_union_find(uf)
    implicit none
    type(union_find), intent(inout) :: uf
    if (associated(uf%par)) deallocate(uf%par)
    if (associated(uf%rnk)) deallocate(uf%rnk)
    if (associated(uf%siz)) deallocate(uf%siz)
    return
  end subroutine release_union_find
  recursive function find(uf,i) result(j)
    implicit none
    type(union_find), intent(inout) :: uf
    integer, intent(in) :: i
    integer :: j
    if (uf%par(i).ne.i) then
      uf%par(i) = find(uf,uf%par(i))
    end if
    j = uf%par(i)
    return
  end function find
  function same(uf,i,j) result(y)
    implicit none
    type(union_find), intent(inout) :: uf
    integer, intent(in) :: i, j
    logical :: y
    y = find(uf,i).eq.find(uf,j)
  end function same
  subroutine unite(uf,i,j)
    implicit none
    type(union_find), intent(inout) :: uf
    integer, intent(in) :: i, j
    integer :: x, y
    x = find(uf,i)
    y = find(uf,j)
    if (x.eq.y) return
    if (uf%rnk(x).lt.uf%rnk(y)) then
      uf%par(x) = y
      uf%siz(y) = uf%siz(y)+uf%siz(x)
    else
      uf%par(y) = x
      uf%siz(x) = uf%siz(x)+uf%siz(y)
      if (uf%rnk(x).eq.uf%rnk(y)) uf%rnk(x) = uf%rnk(x)+1
    end if
    return
  end subroutine unite
  function size_of(uf,i) result(s)
    implicit none
    type(union_find), intent(inout) :: uf
    integer, intent(in) :: i
    integer :: s
    s = uf%siz(find(uf,i))
  end function size_of
end module mod_union_find
program must_be_rectangular
  use mod_union_find
  implicit none
  type(union_find) :: uf
  integer :: n, x, y, m(-100000:100001,2), i, j
  integer(8) :: z
  call init_union_find(uf,100000)
  m = 0
  read(*,*) n
  do i = 1, n
    read(*,*) x, y
    call unite(uf,x,-y)
  end do
  m(-100000:100000,1) = uf%par
  m(100001,1) = 1000010000
  do i = -100000, 100000
    m(i,2) = i
  end do
  call merge_sort(m(-100000:100000,1:2),1)
  i = -100000
  z = 0_8
  do while (i.lt.100001)
    x = 0
    y = 0
    do j = i, 100001
      if (m(i,1).eq.m(j,1)) then
        if (m(j,2).gt.0) then
          x = x+1
        else
          y = y+1
        end if
      else
        z = z+int(x,8)*int(y,8)
        i = j
        exit
      end if
    end do
  end do
  write(*,'(i0)') z-int(n,8)
  stop
contains
  subroutine merge_sort(c,k)
    implicit none
    integer, intent(inout) :: c(:,:)
    integer, intent(in) :: k
    integer :: n, m, l, u, i
    n = size(c(:,1))
    m = n
    l = 1
    do while (m.gt.1)
      do i = 1, m/2
        u = min(2*i*l,n)
        call merger(c(2*(i-1)*l+1:(2*i-1)*l,:),c((2*i-1)*l+1:u,:),k)
      end do
      l = 2*l
      m = (m+1)/2
    end do
    return
  end subroutine merge_sort
  subroutine merger(c1,c2,k)
    implicit none
    integer, intent(inout) :: c1(:,:), c2(:,:)
    integer, intent(in) :: k
    integer :: c(size(c1(:,1))+size(c2(:,1)),size(c2(1,:)))
    integer :: i1, i2, n1, n2
    i1 = 1
    i2 = 1
    n1 = size(c1(:,1))
    n2 = size(c2(:,1))
    do while ((i1.le.n1).and.(i2.le.n2))
      if (c1(i1,k).le.c2(i2,k)) then
        c(i1+i2-1,:) = c1(i1,:)
        i1 = i1+1
      else
        c(i1+i2-1,:) = c2(i2,:)
        i2 = i2+1
      end if
    end do
    if (i1.le.n1) then
      c(i1+i2-1:n1+n2,:) = c1(i1:n1,:)
    else if (i2.le.n2) then
      c(i1+i2-1:n1+n2,:) = c2(i2:n2,:)
    end if
    c1 = c(1:n1,:)
    c2 = c(n1+1:n1+n2,:)
    return
  end subroutine merger
end program must_be_rectangular

Score : 600 points

Problem Statement

There are N dots in a two-dimensional plane. The coordinates of the i-th dot are (x_i, y_i).

We will repeat the following operation as long as possible:

Choose four integers a, b, c, d (a \neq c, b \neq d) such that there are dots at exactly three of the positions (a, b), (a, d), (c, b) and (c, d), and add a dot at the remaining position.

We can prove that we can only do this operation a finite number of times. Find the maximum number of times we can do the operation.

Constraints

1 \leq N \leq 10^5

1 \leq x_i, y_i \leq 10^5

If i \neq j, x_i \neq x_j or y_i \neq y_j.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
:
x_N y_N

Output

Print the maximum number of times we can do the operation.

Sample Input 1

3
1 1
5 1
5 5

Sample Output 1

1

By choosing a = 1, b = 1, c = 5, d = 5, we can add a dot at (1, 5). We cannot do the operation any more, so the maximum number of operations is 1.

Sample Input 2

2
10 10
20 20

Sample Output 2

0

There are only two dots, so we cannot do the operation at all.

Sample Input 3

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

Sample Output 3

16

We can do the operation for all choices of the form a = 1, b = 1, c = i, d = j (2 \leq i,j \leq 5), and no more. Thus, the maximum number of operations is 16.


program prob51
    implicit none
    integer :: x,a
    read(*,*) x,a
    if(x >= a)then
        write(*,*) "10"
    else
        write(*,*) "0"
    end if

    stop
contains
end program prob51

Score : 100 points

Problem Statement

X and A are integers between 0 and 9 (inclusive).

If X is less than A, print 0; if X is not less than A, print 10.

Constraints

0 \leq X, A \leq 9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X A

Output

If X is less than A, print 0; if X is not less than A, print 10.

Sample Input 1

3 5

Sample Output 1

0

3 is less than 5, so we should print 0.

Sample Input 2

7 5

Sample Output 2

10

7 is not less than 5, so we should print 10.

Sample Input 3

6 6

Sample Output 3

10

6 is not less than 6, so we should print 10.


integer :: x,a
read*,a,x

if(x>=a)then
print*,10
else
print*,0
endif
end


implicit none
integer :: n,x
integer :: l(100)
integer :: i,d,ans
read *,n,x
read *,(l(i),i=1,n)

ans = 0
d = 0
do i = 1, n+1
   if (d <= x) ans = ans + 1
   d = d + l(i)
enddo

print '(i0)',ans
end

Score : 200 points

Problem Statement

A ball will bounce along a number line, making N + 1 bounces. It will make the first bounce at coordinate D_1 = 0, and the i-th bounce (2 \leq i \leq N+1) at coordinate D_i = D_{i-1} + L_{i-1}.

How many times will the ball make a bounce where the coordinate is at most X?

Constraints

1 \leq N \leq 100

1 \leq L_i \leq 100

1 \leq X \leq 10000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X
L_1 L_2 ... L_{N-1} L_N

Output

Print the number of times the ball will make a bounce where the coordinate is at most X.

Sample Input 1

3 6
3 4 5

Sample Output 1

2

The ball will make a bounce at the coordinates 0, 3, 7 and 12, among which two are less than or equal to 6.

Sample Input 2

4 9
3 3 3 3

Sample Output 2

4

The ball will make a bounce at the coordinates 0, 3, 6, 9 and 12, among which four are less than or equal to 9.


module ABC130

  ! <module>s to import
  use, intrinsic :: iso_fortran_env

  ! require all variables to be explicitly declared
  implicit none

  ! accessibility of <subroutine>s and <function>s in this <module>
  public  :: task_B

  ! contained <subroutine>s and <function>s are below
  contains

  subroutine task_B

    ! variables for this <subroutine>
    integer(INT32)                            :: num_bound_total
    integer(INT32)                            :: coordinate_limit
    integer(INT32), allocatable, dimension(:) :: coordinate_bound
    integer(INT32)                            :: coordinate_current

    ! support variables for this <subroutine>
    integer(INT32) :: counter

    ! STEP.01
    ! read out the number of bound
    ! read out the limit of coordinate
    read(unit=INPUT_UNIT, fmt=*) num_bound_total, coordinate_limit

    ! STEP.02
    ! allocate the arrays
    allocate(coordinate_bound(1:num_bound_total))

    ! STEP.03
    ! read out the coordinate where to bound
    read(unit=INPUT_UNIT, fmt=*) coordinate_bound(1:num_bound_total)

    ! STEP.04
    ! count the number of bound which satisfies the given condition
    coordinate_current = 0_INT32
    counter            = 0_INT32

    do while (coordinate_current .le. coordinate_limit .or. counter .eq. num_bound_total)
      coordinate_current = coordinate_current + coordinate_bound(counter+1)
      counter = counter + 1
    end do

    ! STEP.04
    ! output the result
    write(unit=OUTPUT_UNIT, fmt='(I0)', advance='yes') counter

    ! STEP.END
    return

  end subroutine task_B

end module ABC130

program main

  ! <module>s to import
  use, non_intrinsic :: ABC130

  ! require all variables to be explicitly declared
  implicit none

  call task_B

end program main


program main
implicit none
integer :: n, x
integer , allocatable :: l(:)
integer :: i, nc, j

read(*,*) n, x
allocate( l(n))
read(*,*) l(:)
j = 0
nc = 1
do i = 1, n
   j = j+l(i)
   if( j .le. x ) then
      nc = nc + 1
   end if
end do
write(*,'(i0)') nc

end program main


program sample
    implicit none

    integer(8) :: i,j,m,n,w,h,x,y
    real(8)::a,b

    read(*,*) w,h,x,y

    write(*,'(f29.10)',advance='no') w/2.0*h
    if (mod(w,2)==1 .or. mod(h,2)==1)then
        write(*,*)0
    else
        if (x==w/2 .and. y==h/2) then
            write(*,*)1
        else
            write(*,*)0
        end if
    end if

    stop
end program sample

Score : 300 points

Problem Statement

There is a rectangle in a coordinate plane. The coordinates of the four vertices are (0,0), (W,0), (W,H), and (0,H).
You are given a point (x,y) which is within the rectangle or on its border. We will draw a straight line passing through (x,y) to cut the rectangle into two parts. Find the maximum possible area of the part whose area is not larger than that of the other. Additionally, determine if there are multiple ways to cut the rectangle and achieve that maximum.

Constraints

1 \leq W,H \leq 10^9

0\leq x\leq W

0\leq y\leq H

All values in input are integers.

Input

Input is given from Standard Input in the following format:

W H x y

Output

Print the maximum possible area of the part whose area is not larger than that of the other, followed by 1 if there are multiple ways to cut the rectangle and achieve that maximum, and 0 otherwise.

The area printed will be judged correct when its absolute or relative error is at most 10^{-9}.

Sample Input 1

2 3 1 2

Sample Output 1

3.000000 0

The line x=1 gives the optimal cut, and no other line does.

Sample Input 2

2 2 1 1

Sample Output 2

2.000000 1


program prob3
    implicit none
    integer(8)::W, H, x, y
    real(16):: xm,ym, ans
    read(*,*) W, H, x, y
    xm = real(W,kind=16)/2.0_16
    ym = real(H,kind=16)/2.0_16

    ans = real(H*W,kind=16)/2.0_16
    if(mod(W,2)==0 .and. mod(H,2)==0)then
        if(x == W/2 .and. y == H/2)then
            write(*,*) ans, 1
            stop
        end if
    end if

    write(*,*) ans,0

    stop
end program


program name
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: w,h,x,y,bunkatsu

    read*, w,h,x,y
    bunkatsu=0
    if (2*x == w .and. 2*y==h) bunkatsu=1
    print'(f11.9,1x,i0)', dble(w*h)/2d0, bunkatsu
end program name


program main
  implicit none
  integer :: x, y, w, h
  real :: area
  integer :: flag

  read(*,*) w, h, x, y
  area = w*h/2.0

  if (x == w/2 .and. y == h/2) then
    flag = 1
  else
    flag = 0
  endif

  write(*,*) area, flag

end program main


program kadai
   implicit none
integer(8)  ::  n,m,i,j,a,b
integer(8),allocatable  ::  s(:),t(:)
integer(8),allocatable  ::  dp(:,:),sum(:,:)
integer(8)  ::  sa

read(*,*) n,m

allocate(s(n))
allocate(t(m))
allocate(dp(n,m))
allocate(sum(n,m))
read(*,*) s
read(*,*) t

sum=0
dp=0
do i=1,n
    do j=1,m
        if (s(i)==t(j)) then
            if (i>1 .and. j>1) then
                dp(i,j)=sum(i-1,j-1)
            end if
            dp(i,j)=dp(i,j)+1
        else
            dp(i,j)=0
        end if
        if (i>1 .and. j>1) then
            sum(i,j)=sum(i-1,j)+sum(i,j-1)-sum(i-1,j-1)+dp(i,j)
        elseif (i==1 .and. j>1) then
            sum(1,j)=sum(1,j-1)+dp(1,j)
        elseif (i>1 .and. j==1) then
            sum(i,1)=sum(i-1,1)+dp(i,1)
        else
            sum(1,1)=dp(1,1)
        end if
        sum(i,j)=mod(sum(i,j),10**9+7)
    end do
end do

write(*,*) mod(sum(n,m)+1,10**9+7)

stop
contains

end program kadai

Score : 500 points

Problem Statement

You are given two integer sequences S and T of length N and M, respectively, both consisting of integers between 1 and 10^5 (inclusive).

In how many pairs of a subsequence of S and a subsequence of T do the two subsequences are the same in content?

Here the subsequence of A is a sequence obtained by removing zero or more elements from A and concatenating the remaining elements without changing the order.

For both S and T, we distinguish two subsequences if the sets of the indices of the removed elements are different, even if the subsequences are the same in content.

Since the answer can be tremendous, print the number modulo 10^9+7.

Constraints

1 \leq N, M \leq 2 \times 10^3

The length of S is N.

The length of T is M.

1 \leq S_i, T_i \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
S_1 S_2 ... S_{N-1} S_{N}
T_1 T_2 ... T_{M-1} T_{M}

Output

Print the number of pairs of a subsequence of S and a subsequence of T such that the subsequences are the same in content, modulo 10^9+7.

Sample Input 1

2 2
1 3
3 1

Sample Output 1

3

S has four subsequences: (), (1), (3), (1, 3).

T has four subsequences: (), (3), (1), (3, 1).

There are 1 \times 1 pair of subsequences in which the subsequences are both (), 1 \times 1 pair of subsequences in which the subsequences are both (1), and 1 \times 1 pair of subsequences in which the subsequences are both (3), for a total of three pairs.

Sample Input 2

2 2
1 1
1 1

Sample Output 2

6

S has four subsequences: (), (1), (1), (1, 1).

T has four subsequences: (), (1), (1), (1, 1).

There are 1 \times 1 pair of subsequences in which the subsequences are both (), 2 \times 2 pairs of subsequences in which the subsequences are both (1), and 1 \times 1 pair of subsequences in which the subsequences are both (1,1), for a total of six pairs.
Note again that we distinguish two subsequences if the sets of the indices of the removed elements are different, even if the subsequences are the same in content.

Sample Input 3

4 4
3 4 5 6
3 4 5 6

Sample Output 3

16

Sample Input 4

10 9
9 6 5 7 5 9 8 5 6 7
8 6 8 5 5 7 9 9 7

Sample Output 4

191

Sample Input 5

20 20
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Sample Output 5

846527861

Be sure to print the number modulo 10^9+7.


program common_subsequence
  implicit none
  integer(8), parameter :: md = 1000000007_8
  integer :: n, m, s(2000), t(2000), i, j
  integer(8) :: dp(0:2000,0:2000)
  s = 0
  t = 0
  dp = 0_8
  read(*,*) n, m
  read(*,*) s(1:n)
  read(*,*) t(1:m)
  do i = 1, n
    do j = 1, m
      if (s(i).eq.t(j)) then
        dp(i,j) = mod(1_8+dp(i,j-1)+dp(i-1,j),md)
      else
        dp(i,j) = mod(mod(dp(i,j-1)+dp(i-1,j),md)+mod(md-dp(i-1,j-1),md),md)
      end if
    end do
  end do
  write(*,'(i0)') dp(n,m)+1_8
  stop
end program common_subsequence


module mod_bounding_box
  implicit none
  real(8), parameter :: infty = 1.e12
  type :: bounding_box
    real(8) :: l(-1:1), r(-1:1)
  end type bounding_box
  private
  public :: infty
  public :: mins, maxs
  public :: bounding_box, init, add, get, event
contains
  subroutine mins(x,y)
    implicit none
    real(8), intent(inout) :: x
    real(8), intent(in) :: y
    x = min(x,y)
    return
  end subroutine mins
  subroutine maxs(x,y)
    implicit none
    real(8), intent(inout) :: x
    real(8), intent(in) :: y
    x = max(x,y)
    return
  end subroutine maxs
  subroutine init(bb)
    implicit none
    type(bounding_box), intent(inout) :: bb
    bb%l = infty
    bb%r = -infty
    return
  end subroutine init
  subroutine add(bb,z,i)
    implicit none
    type(bounding_box), intent(inout) :: bb
    real(8), intent(in) :: z
    integer :: i
    call mins(bb%l(i),z)
    call maxs(bb%r(i),z)
    return
  end subroutine add
  function get(bb,t) result(d)
    implicit none
    type(bounding_box), intent(in) :: bb
    real(8), intent(in) :: t
    real(8) :: d, l, r
    integer :: i
    l = infty
    r = -infty
    do i = -1, 1
      call mins(l,bb%l(i)+real(i,8)*t)
      call maxs(r,bb%r(i)+real(i,8)*t)
    end do
    d = r-l
    return
  end function get
  function event(bb) result(t)
    implicit none
    type(bounding_box), intent(in) :: bb
    real(8) :: t(6)
    t(1) = bb%l(-1)-bb%l(0)
    t(2) = 0.5d0*(bb%l(-1)-bb%l(1))
    t(3) = bb%l(0)-bb%l(1)
    t(4) = bb%r(-1)-bb%r(0)
    t(5) = 0.5d0*(bb%r(-1)-bb%r(1))
    t(6) = bb%r(0)-bb%r(1)
    return
  end function event
end module mod_bounding_box
program minimum_bounding_box
  use mod_bounding_box
  implicit none
  integer :: n, i
  real(8) :: x, y, ans, events(14)
  type(bounding_box) :: xbb, ybb
  character(1) :: d
  character(64) :: s
  call init(xbb)
  call init(ybb)
  events = 0.d0
  read(*,*) n
  do i = 1, n
    read(*,*) x, y, d
    select case(d)
    case("R")
      call add(xbb,x,1)
      call add(ybb,y,0)
    case("L")
      call add(xbb,x,-1)
      call add(ybb,y,0)
    case("U")
      call add(xbb,x,0)
      call add(ybb,y,1)
    case("D")
      call add(xbb,x,0)
      call add(ybb,y,-1)
    end select
  end do
  events(1:6) = event(xbb)
  events(7:12) = event(ybb)
  events(13) = 0.d0
  events(14) = infty
  ans = infty**3
  do i = 1, 14
    if (events(i).lt.0.d0) cycle
    call mins(ans,get(xbb,events(i))*get(ybb,events(i)))
  end do
  write(s,'(f40.16)') ans
  write(*,'(a)') trim(adjustl(s))
  stop
end program minimum_bounding_box

Score : 600 points

Problem Statement

There are N points in a two-dimensional plane. The initial coordinates of the i-th point are (x_i, y_i). Now, each point starts moving at a speed of 1 per second, in a direction parallel to the x- or y- axis. You are given a character d_i that represents the specific direction in which the i-th point moves, as follows:

If d_i = R, the i-th point moves in the positive x direction;

If d_i = L, the i-th point moves in the negative x direction;

If d_i = U, the i-th point moves in the positive y direction;

If d_i = D, the i-th point moves in the negative y direction.

You can stop all the points at some moment of your choice after they start moving (including the moment they start moving).
Then, let x_{max} and x_{min} be the maximum and minimum among the x-coordinates of the N points, respectively. Similarly, let y_{max} and y_{min} be the maximum and minimum among the y-coordinates of the N points, respectively.

Find the minimum possible value of (x_{max} - x_{min}) \times (y_{max} - y_{min}) and print it.

Constraints

1 \leq N \leq 10^5

-10^8 \leq x_i,\ y_i \leq 10^8

x_i and y_i are integers.

d_i is R, L, U, or D.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1 d_1
x_2 y_2 d_2
.
.
.
x_N y_N d_N

Output

Print the minimum possible value of (x_{max} - x_{min}) \times (y_{max} - y_{min}).

The output will be considered correct when its absolute or relative error from the judge's output is at most 10^{-9}.

Sample Input 1

2
0 3 D
3 0 L

Sample Output 1

0

After three seconds, the two points will meet at the origin. The value in question will be 0 at that moment.

Sample Input 2

5
-7 -10 U
7 -6 U
-8 7 D
-3 3 D
0 -6 R

Sample Output 2

97.5

The answer may not be an integer.

Sample Input 3

20
6 -10 R
-4 -9 U
9 6 D
-3 -2 R
0 7 D
4 5 D
10 -10 U
-1 -8 U
10 -6 D
8 -5 U
6 4 D
0 3 D
7 9 R
9 -4 R
3 10 D
1 9 U
1 -6 U
9 -8 R
6 7 D
7 -3 D

Sample Output 3

273


program main
    implicit none
    integer(8)::K,N
    read*,N,K
    print'(i0)',mod(N,K)
end program main

Score : 100 points

Problem Statement

Takahashi is distributing N balls to K persons.

If each person has to receive at least one ball, what is the maximum possible difference in the number of balls received between the person with the most balls and the person with the fewest balls?

Constraints

1 \leq K \leq N \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the maximum possible difference in the number of balls received.

Sample Input 1

3 2

Sample Output 1

1

The only way to distribute three balls to two persons so that each of them receives at least one ball is to give one ball to one person and give two balls to the other person.

Thus, the maximum possible difference in the number of balls received is 1.

Sample Input 2

3 1

Sample Output 2

0

We have no choice but to give three balls to the only person, in which case the difference in the number of balls received is 0.

Sample Input 3

8 5

Sample Output 3

3

For example, if we give 1, 4, 1, 1, 1 balls to the five persons, the number of balls received between the person with the most balls and the person with the fewest balls would be 3, which is the maximum result.


program diverta2019_2_b
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: n,i,j,p,q,cost
    integer(int32), allocatable:: x(:), y(:)
    logical,allocatable:: u(:)

    read*, n
    allocate(x(n), y(n), u(n))

    do i=1,n
        read*, x(i), y(i)
    end do
    cost = 0
    do i=1,n
        do j=1,n
            if (j==i) cycle
            p = x(j)-x(i)
            q = y(j)-y(i)
            u(:) = .false.
            cost = max(cost, calc_cost(p,q))
        end do
    end do
    print'(i0)', n-cost
contains
    recursive function calc_cost(p,q) result(ret)
        integer(int32):: p,q,ret
        integer(int32):: i,j
        ret = 0
        do i=1,n
            do j=1,n
                if (x(j)-x(i) == p .and. y(j)-y(i) == q) ret=ret+1
            end do
        end do
    end function
end program diverta2019_2_b

Score : 300 points

Problem Statement

There are N balls in a two-dimensional plane. The i-th ball is at coordinates (x_i, y_i).

We will collect all of these balls, by choosing two integers p and q such that p \neq 0 or q \neq 0 and then repeating the following operation:

Choose a ball remaining in the plane and collect it. Let (a, b) be the coordinates of this ball. If we collected a ball at coordinates (a - p, b - q) in the previous operation, the cost of this operation is 0. Otherwise, including when this is the first time to do this operation, the cost of this operation is 1.

Find the minimum total cost required to collect all the balls when we optimally choose p and q.

Constraints

1 \leq N \leq 50

|x_i|, |y_i| \leq 10^9

If i \neq j, x_i \neq x_j or y_i \neq y_j.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
:
x_N y_N

Output

Print the minimum total cost required to collect all the balls.

Sample Input 1

2
1 1
2 2

Sample Output 1

1

If we choose p = 1, q = 1, we can collect all the balls at a cost of 1 by collecting them in the order (1, 1), (2, 2).

Sample Input 2

3
1 4
4 6
7 8

Sample Output 2

1

If we choose p = -3, q = -2, we can collect all the balls at a cost of 1 by collecting them in the order (7, 8), (4, 6), (1, 4).

Sample Input 3

4
1 1
1 2
2 1
2 2

Sample Output 3

2


program successive_subtraction
  implicit none
  integer :: n, i, l, r
  integer(8) :: a(100000), m, s
  a = 0_8
  read(*,*) n
  read(*,*) a(1:n)
  call quick_sort(a(1:n))
  l = 0
  do while ((l.lt.n).and.(a(l+1).lt.0))
    l = l+1
  end do
  if (l.eq.0) then
    write(*,'(i0)') sum(a(1:n))-2_8*a(1)
    m = a(1)
    do i = 2, n-1
      write(*,'(i0,x,i0)') m, a(i)
      m = m-a(i)
    end do
    write(*,'(i0,x,i0)') a(n), m
    stop
  end if
  r = n+1
  do while ((r.gt.2).and.(a(r-1).gt.0))
    r = r-1
  end do
  if (r.eq.n+1) then
    write(*,'(i0)') 2_8*a(n)-sum(a(1:n))
    m = a(n)
    do i = n-1, 1, -1
      write(*,'(i0,x,i0)') m, a(i)
      m = m-a(i)
    end do
    stop
  end if
  write(*,'(i0)') sum(a(r:n))-sum(a(1:l))
  m = a(n)
  do i = 2, r-1
    write(*,'(i0,x,i0)') m, a(i)
    m = m-a(i)
  end do
  s = a(1)
  do i = r, n-1
    write(*,'(i0,x,i0)') s, a(i)
    s = s-a(i)
  end do
  write(*,'(i0,x,i0)') m, s
  stop
contains
  recursive subroutine quick_sort(a)
    implicit none
    integer(8), intent(inout) :: a(:)
    integer(8) :: p, c
    integer :: n, l, r
    n = size(a)
    l = 1
    r = n
    p = a((l+r)/2)+a(l)+a(r)-max(a((l+r)/2),a(l),a(r))-min(a((l+r)/2),a(l),a(r))
    do
      do while (a(l).lt.p)
        l = l+1
      end do
      do while (a(r).gt.p)
        r = r-1
      end do
      if (l.ge.r) exit
      c = a(l)
      a(l) = a(r)
      a(r) = c
      l = l+1
      r = r-1
    end do
    if (l.gt.2) call quick_sort(a(1:l-1))
    if (n.gt.r+1) call quick_sort(a(r+1:n))
  end subroutine quick_sort
end program successive_subtraction

Score : 500 points

Problem Statement

There are N integers, A_1, A_2, ..., A_N, written on a blackboard.

We will repeat the following operation N-1 times so that we have only one integer on the blackboard.

Choose two integers x and y on the blackboard and erase these two integers. Then, write a new integer x-y.

Find the maximum possible value of the final integer on the blackboard and a sequence of operations that maximizes the final integer.

Constraints

2 \leq N \leq 10^5

-10^4 \leq A_i \leq 10^4

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible value M of the final integer on the blackboard, and a sequence of operations x_i, y_i that maximizes the final integer, in the format below.

Here x_i and y_i represent the integers x and y chosen in the i-th operation, respectively.

If there are multiple sequences of operations that maximize the final integer, any of them will be accepted.

M
x_1 y_1
:
x_{N-1} y_{N-1}

Sample Input 1

3
1 -1 2

Sample Output 1

4
-1 1
2 -2

If we choose x = -1 and y = 1 in the first operation, the set of integers written on the blackboard becomes (-2, 2).

Then, if we choose x = 2 and y = -2 in the second operation, the set of integers written on the blackboard becomes (4).

In this case, we have 4 as the final integer. We cannot end with a greater integer, so the answer is 4.

Sample Input 2

3
1 1 1

Sample Output 2

1
1 1
1 0


integer N
integer,allocatable,dimension(:)::X
integer(8),allocatable,dimension(:)::ansx,ansy
integer(8) ans
read*,N
allocate(X(N))
read*,X
call heapsort(N,X)

allocate(ansx(N-1),ansy(N-1))
if(mod(N,2)==1)then
  ansx(1)=x(1)
  ansy(1)=x(N)
  do i=2,N-1
    if(mod(i,2)==0)then
      ansx(i)=x(1+i/2)
    else
      ansx(i)=x(N-i/2)
    endif
      ansy(i)=ansx(i-1)-ansy(i-1)
  end do
else
  ansx(1)=x(N)
  ansy(1)=x(1)
  do i=2,N-1
    if(mod(i,2)==0)then
    ansx(i)=x(1+i/2)
  else
    ansx(i)=x(N-i/2)
  endif
    ansy(i)=ansx(i-1)-ansy(i-1)
  end do
endif

ans=ansx(N-1)-ansy(N-1)

print"(i0)",ans
do i=1,N-1
  print"(i0,A,i0)",ansx(i)," ",ansy(i)
end do
contains

subroutine heapsort(n,array)
  implicit none
!ここの入力は状況に応じて変更すること
  integer,intent(in) :: n
  integer,intent(inout) :: array(1:n)
  integer::i,k,j,l
  integer:: t

  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) < 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 successive_subtraction
  implicit none
  integer :: n, i
  integer(8) :: a(100000), m
  a = 0_8
  read(*,*) n
  read(*,*) a(1:n)
  call merge_sort(n,a(1:n))
  if (mod(n,2).eq.1) then
    m = a(n)
    do i = 1, n/2
      m = m+a(n-i)-a(i)
    end do
    write(*,'(i0)') m
    do i = 1, n/2
      write(*,'(i0,x,i0)') a(i), a(n-i)
    end do
    m = a(n)
    do i = 1, n/2
      write(*,'(i0,x,i0)') m, a(i)-a(n-i)
      m = m+a(n-i)-a(i)
    end do
  else
    m = a(n)-a(1)
    do i = 2, n/2
      m = m+a(n-i+1)-a(i)
    end do
    write(*,'(i0)') m
    write(*,'(i0)') a(n), a(1)
    do i = 2, n/2
      write(*,'(i0,x,i0)') a(i), a(n-i+1)
    end do
    m = a(n)-a(1)
    do i = 2, n/2
      write(*,'(i0,x,i0)') m, a(i)-a(n-i+1)
      m = m+a(n-i+1)-a(i)
    end do
  end if
  stop
contains
  subroutine merge_sort(n,c)
    implicit none
    integer, intent(in) :: n
    integer(8), intent(inout) :: c(n)
    integer :: m, l, i, u
    m = n
    l = 1
    do while (m.gt.1)
      do i = 1, m/2
        u = min(2*i*l,n)
        call merge(c(2*(i-1)*l+1:(2*i-1)*l),c((2*i-1)*l+1:u))
      end do
      l = 2*l
      m = (m+1)/2
    end do
    return
  end subroutine merge_sort
  subroutine merge(c1,c2)
    implicit none
    integer(8), intent(inout) :: c1(:), c2(:)
    integer(8) :: c(size(c1)+size(c2))
    integer :: i1, i2, n1, n2
    i1 = 1
    i2 = 1
    n1 = size(c1)
    n2 = size(c2)
    do while (i1.le.n1.and.i2.le.n2)
      if (c1(i1).le.c2(i2)) then
        c(i1+i2-1) = c1(i1)
        i1 = i1+1
      else
        c(i1+i2-1) = c2(i2)
        i2 = i2+1
       end if
    end do
    if (i1.le.n1) then
      c(i1+i2-1:n1+n2) = c1(i1:n1)
    else if (i2.le.n2) then
      c(i1+i2-1:n1+n2) = c2(i2:n2)
    end if
    c1 = c(1:n1)
    c2 = c(n1+1:n1+n2)
    return
  end subroutine merge
end program successive_subtraction


program diverta_city
  integer :: n, i, j
  integer(8), dimension(10) :: a = (/0,1,2,4,7,12,20,29,38,52/), f = 0
  read(*,*) n
  f(3) = 1
  do i = 4, n
    f(i) = f(i-1)*(a(i-1)+a(i-2)+1)
  end do
  do i = 1, n
    do j = 1, n
      if (i == j) then
        write(*,'(i0,x)',advance='no') 0
        cycle
      end if
      write(*,'(i0,x)',advance='no') f(max(i,j))*a(min(i,j)+1)+1
    end do
    write(*,*)
  end do
end program diverta_city

Score : 900 points

Problem Statement

Diverta City is a new city consisting of N towns numbered 1, 2, ..., N.

The mayor Ringo is planning to connect every pair of two different towns with a bidirectional road. The length of each road is undecided.

A Hamiltonian path is a path that starts at one of the towns and visits each of the other towns exactly once.
The reversal of a Hamiltonian path is considered the same as the original Hamiltonian path.

There are N! / 2 Hamiltonian paths. Ringo wants all these paths to have distinct total lengths (the sum of the lengths of the roads on a path), to make the city diverse.

Find one such set of the lengths of the roads, under the following conditions:

The length of each road must be a positive integer.

The maximum total length of a Hamiltonian path must be at most 10^{11}.

Constraints

N is a integer between 2 and 10 (inclusive).

Input

Input is given from Standard Input in the following format:

N

Output

Print a set of the lengths of the roads that meets the objective, in the following format:

w_{1, 1} \ w_{1, 2} \ w_{1, 3} \ ... \ w_{1, N}
w_{2, 1} \ w_{2, 2} \ w_{2, 3} \ ... \ w_{2, N}
: : :
w_{N, 1} \ w_{N, 2} \ w_{N, 3} \ ... \ w_{N, N}

where w_{i, j} is the length of the road connecting Town i and Town j, which must satisfy the following conditions:

w_{i, i} = 0

w_{i, j} = w_{j, i} \ (i \neq j)

1 \leq w_{i, j} \leq 10^{11} \ (i \neq j)

If there are multiple sets of lengths of the roads that meet the objective, any of them will be accepted.

Sample Input 1

3

Sample Output 1

0 6 15
6 0 21
15 21 0

There are three Hamiltonian paths. The total lengths of these paths are as follows:

1 → 2 → 3: The total length is 6 + 21 = 27.

1 → 3 → 2: The total length is 15 + 21 = 36.

2 → 1 → 3: The total length is 6 + 15 = 21.

They are distinct, so the objective is met.

Sample Input 2

4

Sample Output 2

0 111 157 193
111 0 224 239
157 224 0 258
193 239 258 0

There are 12 Hamiltonian paths, with distinct total lengths.


program main
	implicit none
    integer :: p, q, r, a(3), minimum
    read(*, *) p, q, r
    a(1) = p + q
    a(2) = q + r
    a(3) = r + p
    minimum = minval(a)
    write(*, *) minimum
stop
end program main

Score : 100 points

Problem Statement

There are three airports A, B and C, and flights between each pair of airports in both directions.

A one-way flight between airports A and B takes P hours, a one-way flight between airports B and C takes Q hours, and a one-way flight between airports C and A takes R hours.

Consider a route where we start at one of the airports, fly to another airport and then fly to the other airport.

What is the minimum possible sum of the flight times?

Constraints

1 \leq P,Q,R \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

P Q R

Output

Print the minimum possible sum of the flight times.

Sample Input 1

1 3 4

Sample Output 1

4

The sum of the flight times in the route A \rightarrow B \rightarrow C: 1 + 3 = 4 hours

The sum of the flight times in the route A \rightarrow C \rightarrow C: 4 + 3 = 7 hours

The sum of the flight times in the route B \rightarrow A \rightarrow C: 1 + 4 = 5 hours

The sum of the flight times in the route B \rightarrow C \rightarrow A: 3 + 4 = 7 hours

The sum of the flight times in the route C \rightarrow A \rightarrow B: 4 + 1 = 5 hours

The sum of the flight times in the route C \rightarrow B \rightarrow A: 3 + 1 = 4 hours

The minimum of these is 4 hours.

Sample Input 2

3 2 3

Sample Output 2

5


program a
  implicit none
  integer :: N,i,ale,ue,dif
  integer,allocatable :: W(:)
  read(*,*) N
  allocate(W(N))
  read(*,*) W
  ale=sum(W)
  dif=ale
  do i=1,N
     ue=sum(W(1:i))
     if(dif.gt.abs(ale-2*ue)) then
        dif=abs(ale-2*ue)
     endif
  enddo
  write(*,'(i0)') dif

  deallocate(W)
end program a

Score : 200 points

Problem Statement

We have N weights indexed 1 to N. The mass of the weight indexed i is W_i.

We will divide these weights into two groups: the weights with indices not greater than T, and those with indices greater than T, for some integer 1 \leq T < N. Let S_1 be the sum of the masses of the weights in the former group, and S_2 be the sum of the masses of the weights in the latter group.

Consider all possible such divisions and find the minimum possible absolute difference of S_1 and S_2.

Constraints

2 \leq N \leq 100

1 \leq W_i \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
W_1 W_2 ... W_{N-1} W_N

Output

Print the minimum possible absolute difference of S_1 and S_2.

Sample Input 1

3
1 2 3

Sample Output 1

0

If T = 2, S_1 = 1 + 2 = 3 and S_2 = 3, with the absolute difference of 0.

Sample Input 2

4
1 3 1 1

Sample Output 2

2

If T = 2, S_1 = 1 + 3 = 4 and S_2 = 1 + 1 = 2, with the absolute difference of 2. We cannot have a smaller absolute difference.

Sample Input 3

8
27 23 76 2 3 5 62 52

Sample Output 3

2


program abc129c
  implicit none
  integer :: n, m, i
  integer,dimension(:),allocatable :: s, a
  read *, n, m
  allocate(s(-1:n))
  s = 1
  s(-1) = 0
  if (m/=0) then
     allocate(a(m))
     read *, a
     s(a) = 0
  end if
  do i = 1,n
     if (s(i)/=0) s(i) = mod(s(i-1)+s(i-2),10**9+7)
  end do
  print *, s(n)
end program abc129c

Score : 300 points

Problem Statement

There is a staircase with N steps. Takahashi is now standing at the foot of the stairs, that is, on the 0-th step.
He can climb up one or two steps at a time.

However, the treads of the a_1-th, a_2-th, a_3-th, \ldots, a_M-th steps are broken, so it is dangerous to set foot on those steps.

How many are there to climb up to the top step, that is, the N-th step, without setting foot on the broken steps?
Find the count modulo 1\ 000\ 000\ 007.

Constraints

1 \leq N \leq 10^5

0 \leq M \leq N-1

1 \leq a_1 < a_2 < ... < a_M \leq N-1

Input

Input is given from Standard Input in the following format:

N M
a_1
a_2
.
.
.
a_M

Output

Print the number of ways to climb up the stairs under the condition, modulo 1\ 000\ 000\ 007.

Sample Input 1

6 1
3

Sample Output 1

4

There are four ways to climb up the stairs, as follows:

0 \to 1 \to 2 \to 4 \to 5 \to 6

0 \to 1 \to 2 \to 4 \to 6

0 \to 2 \to 4 \to 5 \to 6

0 \to 2 \to 4 \to 6

Sample Input 2

10 2
4
5

Sample Output 2

0

There may be no way to climb up the stairs without setting foot on the broken steps.

Sample Input 3

100 5
1
23
45
67
89

Sample Output 3

608200469

Be sure to print the count modulo 1\ 000\ 000\ 007.


program abc129_d
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: h,w,x,y
    integer(int32):: l
    integer(int32), allocatable:: a(:,:),ta(:)
    character(1), allocatable:: s(:,:)

    read*, h,w
    allocate(a(w,h), s(w,h))

    input_s : block
        character(w):: inpt_s
        do y=1,h
            read*, inpt_s
            do x=1,w
                s(x,y) = inpt_s(x:x)
            end do
        end do
    end block input_s

    allocate(ta(0:w+1))
    a(:,:) = 0
    do y=1,h
        ta(:) = 0
        do x=1,w
            if (s(x,y) == '.') then
                ta(x) = ta(x-1) + 1
            else
                ta(x) = 0
            end if
        end do
        a(:,y) = a(:,y) + ta(1:w)
        ta(:) = 0
        do x=w,1,-1
            if (s(x,y) == '.') then
                ta(x) = ta(x+1)+1
            else
                ta(x) = 0
            end if
        end do
        a(:,y) = a(:,y) + ta(1:w)
    end do

    deallocate(ta)
    allocate(ta(0:h+1))

    do x=1,w
        ta(:) = 0
        do y=1,h
            if (s(x,y) == '.') then
                ta(y) = ta(y-1) + 1
            else
                ta(y) = 0
            end if
        end do
        a(x,:) = a(x,:) + ta(1:h)
        ta(:) = 0
        do y=h,1,-1
            if (s(x,y) == '.') then
                ta(y) = ta(y+1) + 1
            else
                ta(y) = 0
            end if
        end do
        a(x,:) = a(x,:) + ta(1:h)
    end do

    a(:,:)=a(:,:)-3
    print'(i0)', maxval(a)
end program abc129_d

Score : 400 points

Problem Statement

There is a grid with H horizontal rows and W vertical columns, and there are obstacles on some of the squares.

Snuke is going to choose one of the squares not occupied by an obstacle and place a lamp on it.
The lamp placed on the square will emit straight beams of light in four cardinal directions: up, down, left, and right.
In each direction, the beam will continue traveling until it hits a square occupied by an obstacle or it hits the border of the grid. It will light all the squares on the way, including the square on which the lamp is placed, but not the square occupied by an obstacle.

Snuke wants to maximize the number of squares lighted by the lamp.

You are given H strings S_i (1 \leq i \leq H), each of length W. If the j-th character (1 \leq j \leq W) of S_i is #, there is an obstacle on the square at the i-th row from the top and the j-th column from the left; if that character is ., there is no obstacle on that square.

Find the maximum possible number of squares lighted by the lamp.

Constraints

1 \leq H \leq 2,000

1 \leq W \leq 2,000

S_i is a string of length W consisting of # and ..

. occurs at least once in one of the strings S_i (1 \leq i \leq H).

Input

Input is given from Standard Input in the following format:

H W
S_1
:
S_H

Output

Print the maximum possible number of squares lighted by the lamp.

Sample Input 1

4 6
#..#..
.....#
....#.
#.#...

Sample Output 1

8

If Snuke places the lamp on the square at the second row from the top and the second column from the left, it will light the following squares: the first through fifth squares from the left in the second row, and the first through fourth squares from the top in the second column, for a total of eight squares.

Sample Input 2

8 8
..#...#.
....#...
##......
..###..#
...#..#.
##....#.
#...#...
###.#..#

Sample Output 2

13


program abc129d
  implicit none
  character(2000),dimension(:),allocatable :: s
  integer :: h, w, i, j
  integer,dimension(:,:,:),allocatable :: grid

  read *, h, w
  allocate(s(h),grid(w,h,3))
  read *, s

  do i = 1,h
     do j = 1,w
        grid(j,i,:) = merge(0,1,s(i)(j:j)=='#')
     end do
  end do

  do j = 2,w
     grid(j,:,2) = merge(grid(j,:,2)+grid(j-1,:,2),grid(j,:,2),grid(j,:,1)/=0)
  end do

  do j = w-1,1,-1
     grid(j,:,2) = merge(grid(j+1,:,2),grid(j,:,2),grid(j,:,1)/=0.and.grid(j+1,:,1)/=0)
  end do

  do i = 2,h
     grid(:,i,3) = merge(grid(:,i,3)+grid(:,i-1,3),grid(:,i,3),grid(:,i,1)/=0)
  end do

  do i = h-1,1,-1
     grid(:,i,3) = merge(grid(:,i+1,3),grid(:,i,3),grid(:,i,1)/=0.and.grid(:,i+1,1)/=0)
  end do

  print *, maxval(grid(:,:,2)+grid(:,:,3))-1
end program abc129d