character(10**5) S
integer,allocatable,dimension(:,:)::DP
integer keta(6)
integer SL
read*,S
SL=len_trim(S)
allocate(DP(0:12,SL))
DP=0
keta=[1,10,9,12,3,4]

if(S(SL:SL)=="?")then
  do i=0,9
    DP(i,1)=1
  end do
else
  DP(ichar(S(SL:SL))-48,1)=1
end if

do i=2,SL
  if(S(SL-i+1:SL-i+1)=="?")then
  do j=0,9
    do k=0,12
      jj=mod(j*Keta( mod(i-1,6) +1 )+k,13)
      DP(jj,i)=mod(DP(jj,i)+DP(k,i-1),10**9+7)
    end do
  end do
  else
    jj=(ichar(S(SL-i+1:SL-i+1))-48)*Keta( mod(i-1,6) +1 )
    do j=0,12
      DP(mod(jj+j,13),i)=DP(j,i-1)
    end do
  endif
end do

print"(i0)",DP(5,SL)
end

Score : 400 points

Problem Statement

Given is a string S. Each character in S is either a digit (0, ..., 9) or ?.

Among the integers obtained by replacing each occurrence of ? with a digit, how many have a remainder of 5 when divided by 13? An integer may begin with 0.

Since the answer can be enormous, print the count modulo 10^9+7.

Constraints

S is a string consisting of digits (0, ..., 9) and ?.

1 \leq |S| \leq 10^5

Input

Input is given from Standard Input in the following format:

S

Output

Print the number of integers satisfying the condition, modulo 10^9+7.

Sample Input 1

??2??5

Sample Output 1

768

For example, 482305, 002865, and 972665 satisfy the condition.

Sample Input 2

?44

Sample Output 2

1

Only 044 satisfies the condition.

Sample Input 3

7?4

Sample Output 3

0

We may not be able to produce an integer satisfying the condition.

Sample Input 4

?6?42???8??2??06243????9??3???7258??5??7???????774????4?1??17???9?5?70???76???

Sample Output 4

153716888


module mod_union_find
  implicit none
  type union_find
    integer :: n
    integer, pointer :: par(:), rnk(:), siz(:)
    logical, pointer :: ntr(:), usd(:)
  end type union_find
  private
  public :: union_find, init_union_find, release_union_find
  public :: find, same, unite, size_of, not_tree, used, is_used
contains
  subroutine init_union_find(uf,n)
    implicit none
    type(union_find), intent(inout) :: uf
    integer, intent(in) :: n
    integer :: i
    uf%n = n
    allocate(uf%par(n),uf%rnk(n),uf%siz(n),uf%ntr(n),uf%usd(n))
    uf%rnk = 0
    uf%siz = 1
    uf%ntr = .false.
    uf%usd = .false.
    do i = 1, 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) then
      uf%ntr(x) = .true.
      return
    end if
    if (uf%rnk(x).lt.uf%rnk(y)) then
      uf%par(x) = y
      uf%siz(y) = uf%siz(y)+uf%siz(x)
      if (uf%ntr(x)) uf%ntr(y) = .true.
    else
      uf%par(y) = x
      uf%siz(x) = uf%siz(x)+uf%siz(y)
      if (uf%ntr(y)) uf%ntr(x) = .true.
      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
  function not_tree(uf,i) result(b)
    implicit none
    type(union_find), intent(inout) :: uf
    integer, intent(in) :: i
    logical :: b
    b = uf%ntr(find(uf,i))
    return
  end function not_tree
  subroutine used(uf,i)
    implicit none
    type(union_find), intent(inout) :: uf
    integer, intent(in) :: i
    uf%usd(find(uf,i)) = .true.
    return
  end subroutine used
  function is_used(uf,i) result(b)
    implicit none
    type(union_find), intent(inout) :: uf
    integer, intent(in) :: i
    logical :: b
    b = uf%usd(find(uf,i))
    return
  end function is_used
end module mod_union_find
program strings_of_eternity
  use mod_union_find
  implicit none
  type(union_find) :: uf
  character(500000) :: s, t
  character(2500000) :: ss
  character(5000000) :: ts
  integer :: n, m, x, i, z(5000000) = 0
  logical :: ok(500000) = .false.
  read(*,*) s
  read(*,*) t
  n = len_trim(s)
  m = len_trim(t)
  ss = trim(s)//trim(s)//trim(s)
  if (len_trim(ss) < 2*m) ss = repeat(trim(ss),(2*m+len_trim(ss)-1)/len_trim(ss))
  ts = trim(t)//trim(ss)
  x = len_trim(ts)
  z(1:x) = z_algorithm(ts(1:x))
  do i = 1, n
    if (z(m+i) >= m) ok(i) = .true.
  end do
  call init_union_find(uf,n)
  do i = 1, n
    if (ok(i) .and. ok(mod(m+i-1,n)+1)) then
      if (same(uf,i,mod(m+i-1,n)+1)) then
        write(*,'(i0)') -1
        stop
      end if
      call unite(uf,i,mod(m+i-1,n)+1)
    end if
  end do
  x = 0
  do i = 1, n
    if (ok(i)) x = max(x,size_of(uf,i))
  end do
  write(*,'(i0)') x
  stop
contains
  function z_algorithm(s) result(z)
    implicit none
    character(*) :: s
    integer :: z(len_trim(s)), n, i, j, k
    z = 0
    n = len_trim(s)
    z(1) = n
    i = 1
    j = 0
    do while (i < n)
      do while (i+j < n .and. s(j+1:j+1) == s(i+j+1:i+j+1))
        j = j+1
      end do
      z(i+1) = j
      if (j == 0) then
        i = i+1
        cycle
      end if
      k = 1
      do while (i+k < n .and. k+z(k+1) < j)
        z(i+k+1) = z(k+1)
        k = k+1
      end do
      i = i+k
      j = j-k
    end do
    return
  end function z_algorithm
end program strings_of_eternity

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

Score : 600 points

Problem Statement

Given are two strings s and t consisting of lowercase English letters. Determine if the number of non-negative integers i satisfying the following condition is finite, and find the maximum value of such i if the number is finite.

There exists a non-negative integer j such that the concatenation of i copies of t is a substring of the concatenation of j copies of s.

Notes

A string a is a substring of another string b if and only if there exists an integer x (0 \leq x \leq |b| - |a|) such that, for any y (1 \leq y \leq |a|), a_y = b_{x+y} holds.

We assume that the concatenation of zero copies of any string is the empty string. From the definition above, the empty string is a substring of any string. Thus, for any two strings s and t, i = 0 satisfies the condition in the problem statement.

Constraints

1 \leq |s| \leq 5 \times 10^5

1 \leq |t| \leq 5 \times 10^5

s and t consist of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s
t

Output

If the number of non-negative integers i satisfying the following condition is finite, print the maximum value of such i; if the number is infinite, print -1.

Sample Input 1

abcabab
ab

Sample Output 1

3

The concatenation of three copies of t, ababab, is a substring of the concatenation of two copies of s, abcabababcabab, so i = 3 satisfies the condition.

On the other hand, the concatenation of four copies of t, abababab, is not a substring of the concatenation of any number of copies of s, so i = 4 does not satisfy the condition.

Similarly, any integer greater than 4 does not satisfy the condition, either. Thus, the number of non-negative integers i satisfying the condition is finite, and the maximum value of such i is 3.

Sample Input 2

aa
aaaaaaa

Sample Output 2

-1

For any non-negative integer i, the concatenation of i copies of t is a substring of the concatenation of 4i copies of s. Thus, there are infinitely many non-negative integers i that satisfy the condition.

Sample Input 3

aba
baaab

Sample Output 3

0

As stated in Notes, i = 0 always satisfies the condition.

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 x1, x2, y1, y2, area

    read(*,*) area

    do y1 = 0, 1000000000
        do x1 = 0, y1
            do x2 = 0, x1
                do y2 = 0, y1
                    if(x1 /= x2 .or. y1 /= y2)then
                        if (area == abs(x1*y2-x2*y1))then
                            write(*,'(i0,a1,i0,a1,i0,a1,i0,a1,i0,a1,i0)')0,' ',0,' ', x1,' ',y1,' ', x2,' ',y2
                            stop

                        endif
                    endif
                enddo
            enddo
        enddo
    enddo

end program main

Score : 400 points

Problem Statement

Given is an integer S.
Find a combination of six integers X_1,Y_1,X_2,Y_2,X_3, and Y_3 that satisfies all of the following conditions:

0 \leq X_1,Y_1,X_2,Y_2,X_3,Y_3 \leq 10^9

The area of the triangle in a two-dimensional plane whose vertices are (X_1,Y_1),(X_2,Y_2), and (X_3,Y_3) is S/2.

We can prove that there always exist six integers that satisfy the conditions under the constraints of this problem.

Constraints

1 \leq S \leq 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

S

Output

Print six integers X_1,Y_1,X_2,Y_2,X_3, and Y_3 that satisfy the conditions, in this order, with spaces in between.
If multiple solutions exist, any of them will be accepted.

Sample Input 1

3

Sample Output 1

1 0 2 2 0 1

The area of the triangle in a two-dimensional plane whose vertices are (1,0),(2,2), and (0,1) is 3/2.
Printing 3 0 3 1 0 1 or 1 0 0 1 2 2 will also be accepted.

Sample Input 2

100

Sample Output 2

0 0 10 0 0 10

Sample Input 3

311114770564041497

Sample Output 3

314159265 358979323 846264338 327950288 419716939 937510582


program main
  implicit none
  integer :: r
  read *, r
  print "(i0)", 3 * r ** 2
end program main

Score : 100 points

Problem Statement

It is known that the area of a regular dodecagon inscribed in a circle of radius a is 3a^2.

Given an integer r, find the area of a regular dodecagon inscribed in a circle of radius r.

Constraints

1 \leq r \leq 100

r is an integer.

Input

Input is given from Standard Input in the following format:

r

Output

Print an integer representing the area of the regular dodecagon.

Sample Input 1

4

Sample Output 1

48

The area of the regular dodecagon is 3 \times 4^2 = 48.

Sample Input 2

15

Sample Output 2

675

Sample Input 3

80

Sample Output 3

19200


integer(16) :: r
read*,r
print*,3*r**2
end


program main
implicit none
integer :: i, j, k
integer :: n, d

read(*,*) n, d
do i = 1, 20
   if( (2*d+1)*i .ge. n ) then
      write(*,'(i0)') i
      stop
   end if
end do

end program main

Score : 200 points

Problem Statement

There are N apple trees in a row. People say that one of them will bear golden apples.

We want to deploy some number of inspectors so that each of these trees will be inspected.

Each inspector will be deployed under one of the trees. For convenience, we will assign numbers from 1 through N to the trees. An inspector deployed under the i-th tree (1 \leq i \leq N) will inspect the trees with numbers between i-D and i+D (inclusive).

Find the minimum number of inspectors that we need to deploy to achieve the objective.

Constraints

All values in input are integers.

1 \leq N \leq 20

1 \leq D \leq 20

Input

Input is given from Standard Input in the following format:

N D

Output

Print the minimum number of inspectors that we need to deploy to achieve the objective.

Sample Input 1

6 2

Sample Output 1

2

We can achieve the objective by, for example, placing an inspector under Tree 3 and Tree 4.

Sample Input 2

14 3

Sample Output 2

2

Sample Input 3

20 4

Sample Output 3

3


program ccc

implicit none
integer :: n, i, j
integer, allocatable, dimension(:) :: a

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

do i=1,n
j=a(i)
a(i)=0
write(*,'(i0)') maxval(a)
a(i)=j
end do

end program

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


program main
  implicit none

  integer, allocatable, dimension ( : ) :: A, B
  integer :: n, i, j, tmp

  read *, n

  allocate ( A ( 1:n ) )
  allocate ( B ( 1:2 ) )

  B = 0

  do i = 1, n
     read *, A ( i )
     if ( B ( 1 ) < A ( i ) ) then
        B ( 1 ) = A ( i )
        exit
     else if ( B ( 2 ) < A ( i ) ) then
        B ( 1 ) = A ( i )
        exit
     end if
  end do

  do i = 1, n
     if ( A ( i ) == B ( 1 ) ) then
        print *, B ( 2 )
     else
        print *, B ( 1 )
     end if
  end do

end program main


program abc134c3
   implicit none
   integer(8) :: n, i, lowMax, ans, highmax
   integer(8), allocatable :: a(:)

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

   highmax = 0
   lowMax = 0
   do i = 1, n
      if (i ==1) then
         highmax = maxval(a(2:n))
         write(*,*) highmax
         lowMax = a(1)
         cycle
      else if ( i == n) then
         write(*,*) lowMax
         cycle
      endif

      !lowMax = max(lowmax, a(i-1))
      if (highmax == a(i)) then
         highmax = maxval(a(i+1:n))
      endif
      write(*,*) max(lowMax, highMax)
      lowMax = max(lowmax, a(i))
   enddo

end


program c
  integer :: N,i,mx,mx1
  integer,allocatable :: A(:)
  read(*,*) N
  allocate(A(N))
  do i=1,N
  read(*,*) A(i)
  enddo

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

end program c


program main
  implicit none
  integer :: n, i
  integer, allocatable :: a(:), b(:)
  read (*, *) n
  allocate (a(n), b(n))
  read (*, *) a
  b(:) = 0
  do i = n, 1, -1
    if (mod(sum(b(i:n:i)), 2) /= a(i)) b(i) = 1
  end do
  write (*, "(i0)") sum(b)
  do i = 1, n
    if (b(i) == 1) write (*, "(i0)") i
  end do
end program main

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.


module queue
  implicit none
  type :: JagArray
    integer,allocatable :: array(:)
  end type JagArray
  contains
  subroutine push(A,b)
    integer,allocatable,dimension(:)::A
    integer::b
    A=[A,b]
  end subroutine
  subroutine pop(A)
  	integer,allocatable,dimension(:)::A
    A=A(2:)
  end subroutine
end module queue

use queue
integer N
integer,allocatable,dimension(:)::A,B,C
integer presum
read*,N
allocate(A(N),B(N))
read*,A
B=0
C=[integer::]
if(A(N)==1)then
  B(N)=1
  C=[N,c]
endif
do i=n-1,1,-1
  presum=0
  j=i+i
  do while(j<=N)
    presum=presum+B(j)
    j=j+i
  end do
  if(mod(presum+A(i),2)/=0)then
    B(i)=1
    C=[i,c]
  endif
end do
print"(I0)",size(c)
do i=1,size(c)
  if(i==1)then
  write(*,'(i0)', advance='no')c(i)
  else
  write(*,'(x,i0)', advance='no')c(i)
  endif
end do
end


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

    read*, n

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

    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 (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 main
integer :: x,y,z,m,n,i,j,k
integer,allocatable :: p(:),q(:),r(:)
character*16 :: a,b,c,d,e

read(*,*)n,x,y
if (n*x >= y) then
write(*,*)y
else
write(*,*)n*x
end if

contains

end program main

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


      integer :: n,d
      integer,allocatable :: x(:,:)
      integer :: i,j,k, dist2,ans

      read*,n,d
      allocate( x(n,d) )
      do i = 1,n
        read*,x(i,:)
      end do

      ans = 0
      do i = 1,n-1
        do j = i+1,n

          dist2 = 0
          do k = 1,d
            dist2 = dist2 + ( x(i,k)-x(j,k) )**2
          end do

          if( dist2==int(sqrt(real(dist2)))**2 )then
            ans = ans + 1
          end if
        end do
      end do

      print*,ans
      end

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 main
integer :: n,i,j,d,c
real(8),allocatable :: p(:,:)
real(8) :: a,b

c=0

read(*,*)n,d
allocate(p(n,d))
do i = 1,n
read(*,*)(p(i,j),j=1,d)
end do

do i = 1,n-1
do j = i+1,n
a = dsqrt(sum((p(i,:) - p(j,:))**2))
b = int(a)
if (a == b) c=c+1
end do
end do

write(*,*)c

end program main


program main
 implicit none
 integer*16 N,i,S
 integer*16, allocatable::A(:),B(:)
 read(*,*) N
 allocate(A(1:N), B(1:N))
 read(*,*) A
 S = 0D0
 do i=1,N
  S = S + A(i)
 end do
 do i=1,(N-1)/2
  S = S - 2*A(2*i)
 end do
 B(1) = S
 do i=2,N
  B(i) = 2*A(i-1) - B(i-1)
 end do
 write(*,*) B(1:N)
end program main

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 aaa
implicit none

character :: one, two, three, four
character(4) :: s

read*, s

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

if(one==two.and.three==four) then
write(*,'(a3)') 'Yes'
else if(one==three.and.two==four) then
write(*,'(a3)') 'Yes'
else if(one==four.and.two==three) then
write(*,'(a3)') 'Yes'
else
write(*,'(a2)') 'No'
end if
end program

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
write(*,*) one, two, three, four
end program


program main
character*16 :: a
integer :: x,i,j

x=0
read(*,*) a
do i=1,4
do j=1,4
if(a(i:i) == a(j:j)) then
	x = x + 1
end if
end do
end do
write(*,*)x
if (x==8) then
write(*,*) "Yes"
else
write(*,*) "No"
end if

end program main


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 main
	implicit none
    integer::N,i,ans
    integer, allocatable :: a(:)
    read(*,*) N
    allocate(a(N))
    read(*, *)(a(i), i = 1,N)
    ans=0
    do i=2,N-1
    	if(a(i-1)<a(i) .and. a(i)<a(i+1)) then
        	ans=ans+1
        else if(a(i-1)>a(i) .and. a(i)>a(i+1)) then
            ans=ans+1
        else
        	ans=ans
        end if
    end do
    print*,ans
end program

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


program ordinary_number
  implicit none
  integer :: n, p(20), m, i
  p = 0
  m = 0
  read(*,*) n
  read(*,*) p(1:n)
  do i = 2, n-1
    if (p(i).eq.sum(p(i-1:i+1))-maxval(p(i-1:i+1))-minval(p(i-1:i+1))) m = m+1
  end do
  write(*,'(i0)') m
  stop
end program ordinary_number


program cut
  implicit none
  integer(8) :: n, ans
  integer(8),allocatable :: d(:)

  read *, n
  allocate(d(n))
  read *, d
  d = qsort(d)
  ans = d(n/2+1)-d(n/2)
  print '(i0)', ans
  stop

contains
  recursive function qsort(a) result(sorted)
    implicit none
    integer(8):: countl,countg, pivot, i, n
    integer(8),intent(in):: a(:)
    integer(8),allocatable:: sorted(:),less(:),great(:)

    n = size(a)
    allocate(sorted(n))
    if (n <= 1) then
      sorted = a
    else
      sorted = 0
      countl = 1
      countg = n
      pivot = a(1)
      do i =2, n
        if (pivot >= a(i)) then
          sorted(countl) = a(i)
          countl = countl + 1
        else
          sorted(countg) = a(i)
          countg = countg - 1
        end if
      end do
      allocate(less(countl-1),great(n-countg))
      less = sorted(1:countl - 1)
      great = sorted(countg+1:n)
      sorted(1:countl-1) = qsort(less)
      sorted(countl) = pivot
      sorted(countg+1:n) = qsort(great)
    end if
  end function
end program cut

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 main
  implicit none
  integer(8) i,N,al,av
  integer(8),allocatable :: d(:)
  !read(*, *) (L(i), i = 1,N)
  read(*, *) N
  allocate(d(N))
  read(*, *) (d(i), i = 1,N)
  do i = 1,N/2+1
      al = minloc(d(i:N),1)
      av = minval(d(i:N))
      d(al+i-1) = d(i)
      !write(*,*)d(i),d(al)
      d(i) = av
      !write(*,*) d(i)
      !write(*, *) d
  end do
  !write(*, *) d

  write(*, *) abs(d(N/2)-d(N/2+1))
end program main


integer(16) N,K,aka
integer(16) mo
integer(16) ans
integer(16) i
integer(16),allocatable,dimension(:)::Kaijo

read*,n,k
aka=n-k
mo=10**9+7

allocate(Kaijo(0:N))
Kaijo(0)=1
do i=1,N
  Kaijo(i)=mod(Kaijo(i-1)*i,mo)
end do

do i=1,K
  ans=0
  if(aka==0.and.i==1)ans=1
  if(aka>=i-1.and.i-2>=0)then
    ans=mod(ans +mod(combination(K-1,i-1,mo)*combination(aka-1,i-2,mo),mo),mo)
  endif

  if(aka>=i)then
    ans=mod(ans +mod(combination(K-1,i-1,mo)*combination(aka-1,i-1,mo)*2,mo),mo)
  endif

  if(aka>=i+1)then
    ans=mod(ans +mod(combination(K-1,i-1,mo)*combination(aka-1,i,mo),mo),mo)
  endif

  print"(i0)",ans
end do

contains

function combination(n,k,l)
integer(16),intent(in)::n,k,l
integer(16) combination,i

combination=mod(Kaijo(N)*inv(Kaijo(K),l)*inv(Kaijo(N-K),l),l)

end function combination

function inv(K,L)
integer(16),intent(in)::k,l
integer(16) l2,y
integer(16) inv
inv=1
l2=l-2
y=k
do
  if(mod(l2,2)==1)inv=mod(inv*y,l)
  y=mod(y*y,l)
  l2=l2/2
  if(l2==0)exit
end do
end function inv

end

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.


module m_deque ! 双端キュー. リングバッファ実装. itemtypeを変更して使え
  implicit none

  private
  public :: itemtype, dequetype
  integer, parameter :: MINSIZE = 1

  type itemtype
    integer :: x(3)
  end type itemtype

  type dequetype
    integer :: n = 0, st = 1, memsize = 0
    type(itemtype), allocatable :: ary(:)
  contains
    procedure :: push_left, push_right, pop_left, pop_right, slice, dump
  end type dequetype

  contains

  subroutine resize_up(dq)
    ! 初めの要素追加または n == memsize で呼ばれる
    type(dequetype), intent(inout) :: dq
    type(itemtype), allocatable :: tmp(:)
    if (dq%memsize == 0) then
      dq%memsize = MINSIZE
      allocate (dq%ary(dq%memsize))
      dq%n = 0
      dq%st = max(1, dq%memsize / 8)
    else
      if (dq%n /= dq%memsize) stop 2
      allocate (tmp(dq%n))
      tmp(:) = slice(dq, 1, dq%n)
      dq%memsize = dq%memsize * 2
      deallocate (dq%ary)
      allocate (dq%ary(dq%memsize))
      dq%st = max(1, dq%memsize / 8)
      dq%ary(dq%st:dq%st + dq%n - 1) = tmp(:)
      deallocate (tmp)
    end if
  end subroutine resize_up

  subroutine may_resize_down(dq)
    ! 必要なら小さい配列に移し替える
    type(dequetype), intent(inout) :: dq
    type(itemtype), allocatable :: tmp(:)
    if (dq%memsize <= MINSIZE) return
    if (mod(dq%memsize, 2) /= 0) stop 1
    if (dq%n * 4 > dq%memsize) return

    allocate (tmp(dq%n))
    tmp(:) = slice(dq, 1, dq%n)
    dq%memsize = dq%memsize / 2
    deallocate (dq%ary)
    allocate (dq%ary(dq%memsize))
    dq%st = max(1, dq%memsize / 8)
    dq%ary(dq%st:dq%st + dq%n - 1) = tmp(:)
    deallocate (tmp)
  end subroutine may_resize_down

  subroutine push_left(dq, x)
    class(dequetype), intent(inout) :: dq
    type(itemtype), intent(in) :: x

    if (dq%n == dq%memsize) call resize_up(dq)
    dq%n = dq%n + 1
    dq%st = imod(dq%st - 1, dq%memsize)
    dq%ary(dq%st) = x
  end subroutine push_left

  subroutine push_right(dq, x)
    class(dequetype), intent(inout) :: dq
    type(itemtype), intent(in) :: x
    integer :: idx

    if (dq%n == dq%memsize) call resize_up(dq)
    dq%n = dq%n + 1
    idx = dq%st + dq%n - 1
    idx = imod(idx, dq%memsize)
    dq%ary(idx) = x
  end subroutine push_right

  type(itemtype) function pop_left(dq)
    class(dequetype), intent(inout) :: dq
    if (dq%n == 0) stop 1
    pop_left = dq%ary(dq%st)
    dq%n = dq%n - 1
    dq%st = imod(dq%st + 1, dq%memsize)
    call may_resize_down(dq)
  end function pop_left

  type(itemtype) function pop_right(dq)
    class(dequetype), intent(inout) :: dq
    if (dq%n == 0) stop 1
    pop_right = dq%ary(imod(dq%st + dq%n - 1, dq%memsize))
    dq%n = dq%n - 1
    call may_resize_down(dq)
  end function pop_right

  function slice(dq, istart, iend)
    ! 実データの [istart:iend] の範囲を切り出す
    class(dequetype), intent(in) :: dq
    type(itemtype), allocatable :: slice(:)
    integer, intent(in) :: istart, iend  ! 実データ内のインデックス
    integer :: dstart, dend
    if (istart < 1) stop 1
    if (iend < istart) stop 1
    if (iend > dq%n) stop 1

    dstart = imod(istart + dq%st - 1, dq%memsize)
    dend = imod(dstart + (iend - istart), dq%memsize)

    allocate (slice(iend - istart + 1))
    if (dend < dstart) then
      slice(1:dq%memsize - dstart + 1) = dq%ary(dstart:dq%memsize)
      slice(dq%memsize - dstart + 2:iend - istart + 1) = dq%ary(1:dend)
    else
      slice(:) = dq%ary(dstart:dend)
    end if
  end function slice

  subroutine dump(dq)
    class(dequetype), intent(in) :: dq
    print *, "Data count:               ", dq%n
    print *, "Physical starting index:  ", dq%st
    print *, "Physical memory allocated:", dq%memsize
    print *, slice(dq, 1, dq%n)
  end subroutine dump

  pure integer function imod(idx, ms)
    ! メモリサイズ ms でインデックスを丸める
    ! [1 - ms <= idx <= ms + ms] を仮定
    integer, intent(in) :: idx, ms
    imod = idx
    if (imod > ms) then
      imod = imod - ms
    else if (imod < 1) then
      imod = imod + ms
    end if
  end function imod
end module m_deque

program main
  use m_deque, only: dequetype, itemtype
  implicit none
  integer :: n, m, u, v, i, j, k, k2, d, s, t, res, x
  logical, allocatable :: queued(:, :)
  type(dequetype), allocatable :: dest(:)
  type(dequetype) :: q
  type(itemtype) :: tri, tri2(1)

  read *, n, m
  allocate (dest(n), queued(0:2, n))
  do i = 1, m
    read *, u, v
    call dest(u) % push_right(itemtype((/v, 0, 0/)))
  end do
  read *, s, t
  queued(:, :) = .false.
  call q % push_right(itemtype((/s, 0, 0/)))
  queued(0, s) = .true.
  res = -1
  do while (q % n > 0)
    tri = q % pop_left()
    i = tri % x(1)
    k = tri % x(2)
    d = tri % x(3)
    if (i == t .and. k == 0) then
      res = d / 3
      exit
    end if
    k2 = modulo((k + 1), 3)
    do x = 1, dest(i) % n
      tri2 = dest(i) % slice(x, x)
      j = tri2(1) % x(1)
      if (queued(k2, j)) cycle
      call q % push_right(itemtype((/j, k2, d + 1/)))
      queued(k2, j) = .true.
    end do
  end do
  print "(i0)", res
end program main

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


module m_deque ! 双端キュー. リングバッファ実装. itemtypeを変更して使え
  implicit none

  private
  public :: itemtype, dequetype
  integer, parameter :: MINSIZE = 4

  type itemtype
    integer :: x(3)
  end type itemtype

  type dequetype
    integer :: n = 0, st = 1, memsize = 0
    type(itemtype), allocatable :: ary(:)
  contains
    procedure :: push_left, push_right, pop_left, pop_right, slice, dump
  end type dequetype

  contains

  subroutine resize_up(dq)
    ! 初めの要素追加または n == memsize で呼ばれる
    type(dequetype), intent(inout) :: dq
    type(itemtype), allocatable :: tmp(:)
    if (dq%memsize == 0) then
      dq%memsize = MINSIZE
      allocate (dq%ary(dq%memsize))
      dq%n = 0
      dq%st = max(1, dq%memsize / 8)
    else
      if (dq%n /= dq%memsize) stop 2
      allocate (tmp(dq%n))
      tmp(:) = slice(dq, 1, dq%n)
      dq%memsize = dq%memsize * 2
      deallocate (dq%ary)
      allocate (dq%ary(dq%memsize))
      dq%st = max(1, dq%memsize / 8)
      dq%ary(dq%st:dq%st + dq%n - 1) = tmp(:)
      deallocate (tmp)
    end if
  end subroutine resize_up

  subroutine may_resize_down(dq)
    ! 必要なら小さい配列に移し替える
    type(dequetype), intent(inout) :: dq
    type(itemtype), allocatable :: tmp(:)
    if (dq%memsize <= MINSIZE) return
    if (mod(dq%memsize, 2) /= 0) stop 1
    if (dq%n * 4 > dq%memsize) return

    allocate (tmp(dq%n))
    tmp(:) = slice(dq, 1, dq%n)
    dq%memsize = dq%memsize / 2
    deallocate (dq%ary)
    allocate (dq%ary(dq%memsize))
    dq%st = max(1, dq%memsize / 8)
    dq%ary(dq%st:dq%st + dq%n - 1) = tmp(:)
    deallocate (tmp)
  end subroutine may_resize_down

  subroutine push_left(dq, x)
    class(dequetype), intent(inout) :: dq
    type(itemtype), intent(in) :: x

    if (dq%n == dq%memsize) call resize_up(dq)
    dq%n = dq%n + 1
    dq%st = imod(dq%st - 1, dq%memsize)
    dq%ary(dq%st) = x
  end subroutine push_left

  subroutine push_right(dq, x)
    class(dequetype), intent(inout) :: dq
    type(itemtype), intent(in) :: x
    integer :: idx

    if (dq%n == dq%memsize) call resize_up(dq)
    dq%n = dq%n + 1
    idx = dq%st + dq%n - 1
    idx = imod(idx, dq%memsize)
    dq%ary(idx) = x
  end subroutine push_right

  type(itemtype) function pop_left(dq)
    class(dequetype), intent(inout) :: dq
    if (dq%n == 0) stop 1
    pop_left = dq%ary(dq%st)
    dq%n = dq%n - 1
    dq%st = imod(dq%st + 1, dq%memsize)
    call may_resize_down(dq)
  end function pop_left

  type(itemtype) function pop_right(dq)
    class(dequetype), intent(inout) :: dq
    if (dq%n == 0) stop 1
    pop_right = dq%ary(imod(dq%st + dq%n - 1, dq%memsize))
    dq%n = dq%n - 1
    call may_resize_down(dq)
  end function pop_right

  function slice(dq, istart, iend)
    ! 実データの [istart:iend] の範囲を切り出す
    class(dequetype), intent(in) :: dq
    type(itemtype), allocatable :: slice(:)
    integer, intent(in) :: istart, iend  ! 実データ内のインデックス
    integer :: dstart, dend
    if (istart < 1) stop 1
    if (iend < istart) stop 1
    if (iend > dq%n) stop 1

    dstart = imod(istart + dq%st - 1, dq%memsize)
    dend = imod(dstart + (iend - istart), dq%memsize)

    allocate (slice(iend - istart + 1))
    if (dend < dstart) then
      slice(1:dq%memsize - dstart + 1) = dq%ary(dstart:dq%memsize)
      slice(dq%memsize - dstart + 2:iend - istart + 1) = dq%ary(1:dend)
    else
      slice(:) = dq%ary(dstart:dend)
    end if
  end function slice

  subroutine dump(dq)
    class(dequetype), intent(in) :: dq
    print *, "Data count:               ", dq%n
    print *, "Physical starting index:  ", dq%st
    print *, "Physical memory allocated:", dq%memsize
    print *, slice(dq, 1, dq%n)
  end subroutine dump

  pure integer function imod(idx, ms)
    ! メモリサイズ ms でインデックスを丸める
    ! [1 - ms <= idx <= ms + ms] を仮定
    integer, intent(in) :: idx, ms
    imod = idx
    if (imod > ms) then
      imod = imod - ms
    else if (imod < 1) then
      imod = imod + ms
    end if
  end function imod
end module m_deque

program main
  use m_deque, only: dequetype, itemtype
  implicit none
  integer :: n, m, u, v, i, j, k, k2, d, s, t, res, x
  logical, allocatable :: queued(:, :)
  type(dequetype), allocatable :: dest(:)
  type(dequetype) :: q
  type(itemtype) :: tri, tri2(1)

  read *, n, m
  allocate (dest(n), queued(0:2, n))
  do i = 1, m
    read *, u, v
    call dest(u) % push_right(itemtype((/v, 0, 0/)))
  end do
  read *, s, t
  queued(:, :) = .false.
  call q % push_right(itemtype((/s, 0, 0/)))
  queued(0, s) = .true.
  res = -1
  do while (q % n > 0)
    tri = q % pop_left()
    i = tri % x(1)
    k = tri % x(2)
    d = tri % x(3)
    if (i == t .and. k == 0) then
      res = d / 3
      exit
    end if
    k2 = modulo((k + 1), 3)
    do x = 1, dest(i) % n
      tri2 = dest(i) % slice(x, x)
      j = tri2(1) % x(1)
      if (queued(k2, j)) cycle
      call q % push_left(itemtype((/j, k2, d + 1/)))
    end do
  end do
  print "(i0)", res
end program main


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
  logical :: used(100000)
  type :: t_graph
    type(t_list), pointer :: edges(:)
  end type t_graph
  private
  public :: infty, used
  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)
    implicit none
    type(t_graph), intent(in) :: graph
    type(t_graph), intent(inout) :: newgraph
    integer, intent(in) :: s
    type(t_item) :: next
    integer :: i, c(size(graph%edges)), n
    used(s) = .true.
    c = dijkstra(graph,s)
    n = size(graph%edges)
    do i = 1, n
      if (used(i)) cycle
      if (mod(c(i),3).eq.0) then
        call add(newgraph,s,i,c(i)/3)
        call dfs(graph,newgraph,i)
      end if
    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
  used = .false.
  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
  call dfs(org,kng,s)
  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


character(4) s
read *,s
if (s(1:1)==s(2:2) .or. s(2:2)==s(3:3) .or. s(3:3)==s(4:4)) then
print '(a)','Bad'
else
print '(a)','Good'
endif
end

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 sample
  implicit none
  integer N, L, M, k, i, j, e
  read(*,*) N, L
  M = 0
  do i =1, N
  M = M + L + i - 1
  end do
  if ( abs(L + N - 1) >= abs(L )) then
   k = abs(L + N - 1)
  else
   k  = abs (L)
  endif
  do j = 1, N
   if (  abs( L + j - 1) < k) then
    k = abs( L + j - 1)
    e = j
   endif
  end do
  write(*,*) M - L - e + 1
  stop
end program sample

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


program sample
  implicit none
  integer N, L, M, k, i, j
  read(*,*) N, L
  M = 0
  do i =1, N
  M = M + L + i - 1
  end do
  k = M
  do j = 1, N
   if (  abs( M - L - j + 1) < k) then
    k = abs(M - L - j + 1)
   endif
  end do
  write(*,*) k
  stop
end program sample


program main
  implicit none

  integer :: n, l, a, i, mi = 300, su = 0

  read(*,*)n, l
  do i = 1, n
    a = l + i - 1
    if (abs(a) < abs(mi)) then
      mi = a
    end if
    su = su + a
  end do
  write(*,*)su - mi

end program main


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

read(*,*) n, l
allocate( a(n) )
b= n
do i = 1, n
   a(i) = l + i -1
   if( abs( a(i) ) .le. b ) then
      b = a(i)
   end if
end do

write(*,'(i0)') sum(a) -b

end program main


program ABC
    implicit none
    integer(8) a, b, c, d, LCM
    read*, a, b, c, d
    a = a - 1
    LCM = c * d / GCD(c, d)
    print"(i0)", ((b - b / c - b / d + b / LCM) - (a - a / c - a / d + a / LCM))
contains
    !GCDを求めるよ！
    recursive integer(8) function GCD (a, b) result (res)
        implicit none
        integer(8) a, b
        if (b > 0) then
            res = GCD(b, mod(a, b))
        else
            res = a
        end if
    end function
end program ABC

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*16 a,b,c,d,t1,t2,gcd,lcm,p,q,ans
 read(*,*) a,b,c,d
 t1 = c
 t2 = d
 do while( t2 /= 0 )
     gcd = t1
     t1 = t2
     t2 = mod(gcd, t2)
 end do
 gcd = abs(t1)
 lcm = c*d / gcd
 p = b / c + b / d - b / lcm
 q = (a-1) / c + (a-1) / d - (a-1) / lcm
 ans = b - (a-1) - (p - q)
 write(*,*) ans
end program main


program ccc
implicit none
integer(16) res
integer(16) a, b, c, d, x, y, z
integer(16) s
read*, a, b, c, d
x=max(c,d)
y=min(c,d)
res=1
z=c*d
 do while (res.ne.0)
 res=mod(x,y)
 if(res.ne.0) then
 y=res
 end if
 if(res.eq.0) then
 z=(c*d)/y
 end if
 end do
s=int(b/c)+int(b/d)-int((a-1)/c)-int((a-1)/d)-int(b/(z))+int((a-1)/(z))
s=(b-a+1)-s
print*, s, z
end program


program kadai
implicit none
integer  ::  n
integer,allocatable  ::  a(:,:)
integer  ::  i,j,time

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

time=0
j=0
do i=1,n
    j=minloc(a(2,:),1)
    time=time+a(1,j)
    if (time>a(2,j)) then
        write(*,*) "No"
        stop
    end if
    a(2,j)=1000000001
end do
write(*,*) "Yes"
stop

end program kadai

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 X,A,ans
  read(*, *)X,A
  if (X<A) then
    ans = 0
  else
    ans = 10
  end if
  write(* ,*) ans
end program main

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.


program main
	implicit none
    integer::n,x,c,l,i
    integer, allocatable :: a(:)
    read*,n,x
    allocate(a(n))
    read(*, *)(a(i), i = 1,n)
    c=1
    l=0
    i=1
    do i=1,n
    l=l+a(i)
   		if (l<=x) then
    	c=c+1
    	else
        c=c+0
        end if
    end do

print*,c
end program main

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.


program bounding
  implicit none
  integer :: n, l(100), x, d, m, i
  l = 0
  read(*,*) n, x
  read(*,*) l(1:n)
  d = 0
  m = 0
  do while ((d.le.x).and.(m.lt.n))
    m = m+1
    d = d+l(m)
  end do
  write(*,'(i0)') m
  stop
end program bounding


program sample
    implicit none

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

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

    write(*,'(f28.10)',advance='no') w*h/2.0
    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 prob5
    implicit none
    integer(8) :: W, H, x, y
    character(len=100) :: S
    read(*,*) W, H, x, y

    write(S,'(f24.12)') real(W, kind=8)*real(H, kind=8)/2.0_8

    write(*,'(a)',advance='no') trim(adjustl(S))

    if(W==0 .and. H==0) then
        write(*,*) ' 1'
        stop
    end if
    if(W==0 .or. H==0) then
        write(*,*) ' 0'
        stop
    end if
    if( mod(W, 2) == 0 .and. mod(H, 2) == 0 .and. x == W/2 .and. y == H/2) then
        write(*,*) ' 1'
    else
        write(*,*) ' 0'
    end if

    stop
contains
end program prob5


program abc130c
  integer(8) :: w, h, x, y
  read *, w, h, x, y
  print *, dble(w)*dble(h)/2, merge(1,0,x*2==w.and.y*2==h)
end program abc130c


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_C
  private :: adjust_point
  private :: exchange_coordinate
  private :: rotate_rectangle

  ! <type> for this <module>
  type coordinate
    integer(INT32), public :: x
    integer(INT32), public :: y
  end type coordinate

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

  subroutine task_C

    ! variables for this <subroutine>
    type(coordinate) :: rectangle
    type(coordinate) :: point

    ! STEP.01
    ! read out the coordinates
    read(unit=INPUT_UNIT, fmt=*) &!
      rectangle%x, rectangle%y, &!
      point%x,     point%y

    ! STEP.02
    ! adjust the given data
    call exchange_coordinate (point, rectangle)
    call rotate_rectangle    (rectangle)
    call adjust_point        (point, rectangle)

    ! STEP.03
    ! calculate & output the result
    if (rectangle%x/2 .eq. point%x) then
      write(unit=OUTPUT_UNIT, fmt='(ES23.15e3,1X,I1)', advance='yes') &!
        5.0e-001_REAL64 * rectangle%x * rectangle%y, &!
        1_INT8
    else
      write(unit=OUTPUT_UNIT, fmt='(ES23.15e3,1X,I1)', advance='yes') &!
        5.0e-001_REAL64 * point%x * rectangle%y, &!
        0_INT8
    end if

    ! STEP.END
    return

  end subroutine task_C

  subroutine adjust_point (data_point, data_rectangle)

    ! arguments for this <subroutine>
    type(coordinate), intent(inout) :: data_point
    type(coordinate), intent(in)    :: data_rectangle

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

    if (data_point%x .gt. data_rectangle%x / 2) then
      data_point%x = data_rectangle%x - data_point%x
    end if

    return

  end subroutine adjust_point

  subroutine rotate_rectangle (data_rectangle)

    ! arguments for this <subroutine>
    type(coordinate), intent(inout) :: data_rectangle

    ! variables for this <subroutine>
    integer(INT32) :: buffer_min, buffer_max

    buffer_min = min(data_rectangle%x, data_rectangle%y)
    buffer_max = max(data_rectangle%x, data_rectangle%y)

    data_rectangle%x = buffer_max
    data_rectangle%y = buffer_min

    return

  end subroutine rotate_rectangle

  subroutine exchange_coordinate (data_point, data_rectangle)

    ! arguments for this <subroutine>
    type(coordinate), intent(inout) :: data_point
    type(coordinate), intent(in)    :: data_rectangle

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

    if (data_rectangle%x .lt. data_rectangle%y) then
      buffer       = data_point%y
      data_point%y = data_point%x
      data_point%x = buffer
    end if

    return

  end subroutine exchange_coordinate

end module ABC130

program main

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

  ! require all variables to be explicitly declared
  implicit none

  call task_C

end program main


program cutting
implicit none
integer w, h, x, y
integer res
real(8) ss, ww, hh, xx, yy, mm, nn ,ms
read*, w, h, x, y
xx=x
yy=y
ww=w
ww=ww/2
hh=h
hh=h/2
if((xx.eq.ww).and.(yy.eq.hh)) then
res=1
else if ((xx.ne.ww).or.(yy.ne.hh)) then
res=0
end if
ss=w*h
ss=ss/2
ss=w*h-ss
mm=max((ww*2),(hh*2))
nn=min((ww*2),(hh*2))
mm=mm-1
ms=mm*nn
if(((ss-ms)/ss).lt.(0.000000001)) then
res=1
ss=ms/2
end if
print*, ss, res
end program


program cutting
implicit none
integer w, h, x, y
integer res
real(4) ss, ww, hh, xx, yy, mm, nn ,ms
read*, w, h, x, y
xx=x
yy=y
ww=w
ww=ww/2
hh=h
hh=h/2
if((xx.eq.ww).and.(yy.eq.hh)) then
res=1
else if ((xx.ne.ww).or.(yy.ne.hh)) then
res=0
end if
ss=w*h
ss=ss/2
ss=w*h-ss
mm=max(ww,hh)
nn=min(ww,hh)
mm=mm-1
ms=mm*nn
if(((ss-ms)/ss).lt.(0.01)) then
res=1
ss=ms
end if
print*, ss, res
end program


program rectangle_cutting
  implicit none
  real(8) :: w, h, x, y
  integer :: m
  character(32) :: s
  read(*,*) w, h, x, y
  write(s,'(f18.12)') 0.5d0*w*h
  m = 0
  if ((x.eq.0.5d0*w).and.(y.eq.0.5d0*h)) m = 1
  write(*,'(a,x,i0)') trim(adjustl(s)), m
  stop
end program rectangle_cutting


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 kadai
   implicit none
integer(8)  ::  n,m,i,j,a,b
integer,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)=mod(dp(i,j)+1,10**9+7)
        else
            dp(i,j)=0
        end if
        if (i>1 .and. j>1) then
            sum(i,j)=mod(sum(i-1,j)+sum(i,j-1)-sum(i-1,j-1)+dp(i,j),10**9+7)
        elseif (i==1 .and. j>1) then
            sum(1,j)=mod(sum(i,j-1)+dp(1,j),10**9+7)
        elseif (i>1 .and. j==1) then
            sum(i,1)=mod(sum(i-1,1)+dp(i,1),10**9+7)
        else
            sum(1,1)=dp(1,1)
        end if
    end do
end do

!write(*,*) sum
!write(*,*) dp
write(*,*) sum(n,m)+1

stop
contains

end program kadai


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

read(*,*) n,m

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

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

sa=0
do i=1,n
    do j=1,m
        sa=sa+dp(i,j)
        if (sa>10**9+7) then
            sa=mod(sa,10**9+7)
        end if
    end do
end do

!write(*,*) dp
write(*,*) mod(sa+1,10**9+7)

stop
contains

end program kadai


program sample
	integer ::a,b

    read(*,*) a,b

    if (b==1) then
    	write(*,*) 0
    else
    	write(*,*) a-b
    end if
    stop
end program sample

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 prob6
    implicit none
    integer(8) :: N, i, j, K, L, m, tmp, s, ind
    integer(8), allocatable :: a(:)
    character(40) :: x, y
    logical :: check

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

    read(*,*) a

    K = 0
    L = 0
    do i = 1, N
        if(a(i) > 0)then
            K = K + 1
        else if(a(i) < 0)then
            L = L + 1
        end if
    end do

    s = 0
    do i = 1, N
        s = s + abs(a(i))
    end do

    if(K == N) then
        m = 100000
        do i = 1, N
            if(m > a(i)) then
                m = a(i)
                ind = i
            end if
        end do
        write(x,*) s - 2 * m
        write(*,'(a)') trim(adjustl(x))

        if(mod(N, 2) == 1) then
            tmp = m
            do i = 1, N
                if(i == ind) then
                    cycle
                end if
                write(x,*) a(i)
                write(y,*) tmp
                write(*,'(a, 1X, a)') trim(adjustl(x)), trim(adjustl(y))
                tmp = a(i) - tmp
            end do
        else
            tmp = m
            check = .false.
            do i = 1, N
                if(i == ind) then
                    cycle
                end if
                if(check .eqv. .false.) then
                    write(x,*) tmp
                    write(y,*) a(i)
                    write(*,'(a, 1X, a)') trim(adjustl(x)), trim(adjustl(y))
                    tmp = tmp - a(i)
                else
                    write(x,*) a(i)
                    write(y,*) tmp
                    write(*,'(a, 1X, a)') trim(adjustl(x)), trim(adjustl(y))
                    tmp = a(i) - tmp
                end if
            end do
        end if

    else if(L == N) then
        m = -100000
        do i = 1, N
            if(m < a(i)) then
                m = a(i)
                ind = i
            end if
        end do
        write(x,*) s + 2 * m
        write(*,'(a)') trim(adjustl(x))
        tmp = m
        do i = 1, N
            if(i == ind) then
                cycle
            end if
            write(x,*) tmp
            write(y,*) a(i)
            write(*,'(a, 1X, a)') trim(adjustl(x)), trim(adjustl(y))
            tmp = tmp - a(i)
        end do

    else
        do i = 1, N
            if(a(i) <= 0) then
                m = a(i)
                ind = i
                exit
            end if
        end do
        write(x,*) s
        write(*,'(a)') trim(adjustl(x))

        if(mod(K, 2) == 1) then
            tmp = m
            do j = 1, N
                if(a(j) > 0) then
                    write(x,*) a(j)
                    write(y,*) tmp
                    write(*,'(a, 1X, a)') trim(adjustl(x)), trim(adjustl(y))
                    tmp = a(j) - tmp
                end if
            end do
            do j = ind+1, N
                if(a(j) <= 0) then
                    write(x,*) tmp
                    write(y,*) a(j)
                    write(*,'(a, 1X, a)') trim(adjustl(x)), trim(adjustl(y))
                    tmp = tmp - a(j)
                end if
            end do
        else
            tmp = m
            check = .false.
            do j = 1, N
                if(a(j) > 0) then
                    if(check .eqv. .false.) then
                        write(x,*) tmp
                        write(y,*) a(j)
                        write(*,'(a, 1X, a)') trim(adjustl(x)), trim(adjustl(y))
                        tmp = tmp - a(j)
                        check = .true.
                    else
                        write(x,*) a(j)
                        write(y,*) tmp
                        write(*,'(a, 1X, a)') trim(adjustl(x)), trim(adjustl(y))
                        tmp = a(j) - tmp
                    end if
                end if
            end do
            do j = ind+1, N
                if(a(j) <= 0) then
                    write(x,*) tmp
                    write(y,*) a(j)
                    write(*,'(a, 1X, a)') trim(adjustl(x)), trim(adjustl(y))
                    tmp = tmp - a(j)
                end if
            end do
        end if
    end if

    deallocate(a)

    stop
contains
end program prob6

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,allocatable,dimension(:)::ansx,ansy
integer minuscnt,ans
read*,N
allocate(X(N))
allocate(ansx(N-1),ansy(N-1))
read*,X
call heapsort(N,X)

minuscnt=0
do i=1,N
  if(x(i)<0)minuscnt=minuscnt+1
end do

if(minuscnt>=1.and. N-minuscnt>=2)then
  ansx(1)=X(1)
  ansy(1)=X(N-1)
  do i=2,(N-minuscnt)-1
    ansx(i)=ansx(i-1)-ansy(i-1)
    ansy(i)=X(minuscnt+i-1)
  end do
    ansy(N-minuscnt)=ansx(N-minuscnt-1)-ansy(N-minuscnt-1)
    ansx(N-minuscnt)=X(N)
  if(N-minuscnt+1/=N)then
    do i=N-minuscnt+1,N-1
      ansx(i)=ansx(i-1)-ansy(i-1)
      ansy(i)=X(i-N+minuscnt+1)
    end do
  endif
else if(minuscnt==0)then
  ansx(1)=X(1)
  ansy(1)=X(N-1)
  do i=2,N-2
    ansx(i)=ansx(i-1)-ansy(i-1)
    ansy(i)=X(i)
  end do
  ansx(N-1)=X(N)
  ansy(N-1)=ansx(N-2)-ansy(N-2)
else if(minuscnt>0)then
  ansx(1)=X(N)
  ansy(1)=X(1)
  do i=2,N-1
    ansx(i)=ansx(i-1)-ansy(i-1)
    ansy(i)=X(i)
  end do
end if

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


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

minuscnt=0
do i=1,N
  if(x(i)<0)minuscnt=minuscnt+1
end do

if(minuscnt>=1.and. N-minuscnt>=1)then
  ansx(1)=X(1)
  ansy(1)=X(N-1)
  do i=2,(N-minuscnt)-1
    ansx(i)=ansx(i-1)-ansy(i-1)
    ansy(i)=X(minuscnt+i-1)
  end do
    ansy(N-minuscnt)=ansx(N-minuscnt-1)-ansy(N-minuscnt-1)
    ansx(N-minuscnt)=X(N)
  if(N-minuscnt+1/=N)then
    do i=N-minuscnt+1,N-1
      ansx(i)=ansx(i-1)-ansy(i-1)
      ansy(i)=X(i-N+minuscnt+1)
    end do
  endif
else if(minuscnt==0)then
  ansx(1)=X(1)
  ansy(1)=X(N-1)
  do i=2,N-2
    ansx(i)=ansx(i-1)-ansy(i-1)
    ansy(i)=X(i)
  end do
  ansx(N-1)=X(N)
  ansy(N-1)=ansx(N-2)-ansy(N-2)
else if(minuscnt==N)then
  ansx(1)=X(N)
  ansy(1)=X(1)
  do i=2,N-1
    ansx(i)=ansx(i-1)-ansy(i-1)
    ansy(i)=X(i)
  end do
end if

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


integer N
integer,allocatable,dimension(:)::X
integer(8),allocatable,dimension(:)::ansx,ansy
integer(8)minuscnt,tmp
read*,N
allocate(X(N))
allocate(ansx(N-1),ansy(N-1))
read*,X
call heapsort(N,X)

minuscnt=0
do i=1,N
  if(x(i)<0)minuscnt=minuscnt+1
end do

if(minuscnt>=1.and. N-minuscnt>=1)then
  ansx(1)=X(1)
  ansy(1)=X(N-1)
  do i=2,(N-minuscnt)-1
    ansx(i)=ansx(i-1)-ansy(i-1)
    ansy(i)=X(minuscnt+i-1)
  end do
    ansy(N-minuscnt)=ansx(N-minuscnt-1)-ansy(N-minuscnt-1)
    ansx(N-minuscnt)=X(N)
  do i=N-minuscnt+1,N-1
    ansx(i)=ansx(i-1)-ansy(i-1)
    ansy(i)=X(i-N+minuscnt+1)
  end do
else
  ansx(1)=X(1)
  ansy(1)=X(N-1)
  do i=2,N-2
    ansx(i)=ansx(i-1)-ansy(i-1)
    ansy(i)=X(i)
  end do
  ansx(N-1)=X(N)
  ansy(N-1)=ansx(N-2)-ansy(N-2)
end if

ans=ansx(N-1)-ansy(N-1)
if(ans<0)then
  tmp=ansx(N-1)
  ansx(N-1)=ansy(N-1)
  ansy(N-1)=tmp
  ans=-ans
endif

print"(i0)",ansx(N-1)-ansy(N-1)
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 main
  implicit none
  integer(8) i,N,Np,Nn,ip,in
  integer(8),allocatable :: A(:),pos(:),neg(:)
  read(*, *) N
  allocate(A(N))
  read(*, *) (A(i), i = 1,N)
  !正の数と負の数のカウントを行います。
  Np = 0
  Nn = 0
  do i = 1,N
    if (A(i) < 0) then
      Nn = Nn + 1
    else
      Np = Np + 1
    end if
  end do
  if (Nn == 0) then
    allocate(pos(N-1))
    allocate(neg(1))
    neg(1) = A(1)
    do i = 2,N
      if (A(i) <neg(1)) then
        pos(i-1) = neg(1)
        neg(1) = A(i)
      else
        pos(i-1) = A(i)
      end if
    end do
    write (*,*) sum(pos) -neg(1)
    do i = 1,N-2
      write(*, *) neg(1),pos(i)
      neg(1) = neg(1)-pos(i)
    end do
    write(*, *) pos(N-1),neg(1)

  else if (Np == 0) then
    allocate(pos(N))
    allocate(neg(N-1))
    pos(1) = A(1)
    do i = 2,N
      if (A(i) > pos(1)) then
        neg(i-1) = pos(1)
        pos(1) = A(i)
      else
        neg(i-1) = A(i)
      end if
    end do
    write (*,*) pos(1)-sum(pos)
    do i = 1,N-1
      write(*, *) pos(1),neg(i)
      pos(1) = pos(1)-neg(i)
    end do

  else
    allocate(pos(Np))
    allocate(neg(Nn))
    ip = 1
    in = 1
    do i = 1,N
      if (A(i) < 0) then
        neg(in) = A(i)
        in = in + 1
      else
        pos(ip) = A(i)
        ip = ip + 1
      end if
    end do
    write(*, *) sum(pos)-sum(neg)
    if (Np > 1) then
      do i = 2,Np
        write(*, *) neg(1),pos(i)
        neg(1)=neg(1)-pos(i)
      end do
    end if
    if (Nn > 1) then
      do i = 2,Nn
        write(*, *) pos(1),neg(i)
        pos(1)=pos(1)-neg(i)
      end do
    end if
    write(*,*) pos(1),neg(1)
  end if
end program main


integer N
integer,allocatable,dimension(:)::X
integer,allocatable,dimension(:)::ansx,ansy
integer 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 quick_sort(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
  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


integer :: n,w(100),i,ans
ans = 10**7

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

do i = 1,n-1
  ans = min( ans, abs(sum(w(1:i))-sum(w(i+1:n))) )
end do

print*,ans
end

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 ccc

implicit none
integer(16) :: n, m, i, j, k, st
integer(16) :: one, two, thr, x, y, z, co, rs
integer(16),allocatable,dimension(:) :: a

read*, n, m
allocate(a(m+2))
do i=2,m+1
read*, a(i)
end do
a(1)=-1
a(m+2)=n+1
rs=1

do i=1,m+1
st=a(i+1)-a(i)-2
if(st<0) then
rs=0
exit
end if
co=0
do j=0,st/2
 one=st-2*j
 two=j
 thr=one+two
 x=1
 y=1
 z=1
  do k=1, one
  x=x*k
  end do
  do k=1,two
  y=y*k
  end do
  do k=1,thr
  z=z*k
  end do
  co=co+z/(x*y)
 end do
 rs=mod((rs*co),1000000007)
end do
if(rs<0) rs=rs+1000000007
write(*,'(i0)') rs
end program

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 steps
  implicit none
  integer :: n, m, i, maxstep, ans
  integer,allocatable :: a(:), b(:), fib(:)

  read *, n, m
  allocate(a(m+2),b(m+1))
  a(1) = -1
  do i = 1, m
    read *, a(i+1)
  end do
  a(m+2) = n+1
  do i = 1, m+1
    b(i) = a(i+1) - a(i) - 2
  end do
  maxstep = maxval(b)
  allocate(fib(maxstep+1))
  fib(1) = 1
  fib(2) = 1
  do i = 3, maxstep+1
    fib(i) = mod(fib(i-1)+fib(i-2),1000000007)
  end do
  ans = 1
  do i = 1, m+1
    ans = mod(ans * fib(b(i)+1),1000000007)
  end do
  print '(i0)', ans
end program steps


program main
  implicit none
  integer i
  integer(8) N,M,loc,a,ans
  read(*, *) N,M
  loc = 0
  ans = 1
  do i = 1,M
    read(*, *) a
    ans = ans*fibonacci(a-loc)
    do
      if (ans > 1000000007) then
        ans = ans-1000000007
      else
        exit
      end if
    end do
    loc = a+1
  end do
  write(* ,*) mod(ans*fibonacci(N-loc+1),1000000007)

  stop
contains

  integer(8) function fibonacci(n)
    implicit none
    integer(8) xn, xn1, xn2, n, i
    if (n == 0) then
      xn = 0
    else if (n == 1) then
      xn = 1
    else
      xn2 = 0
      xn1 = 1
      do i = 2,n
        xn = xn1 + xn2
        xn2 = xn1
        xn1 = xn
      end do
    end if
    fibonacci = xn
    return
  end function fibonacci

end program main