module mod_tree_map
  implicit none

  type t_entry
    private
    integer :: key
    integer :: val
  end type t_entry

  type t_node
    private
    type(t_node), pointer :: left => null(), right => null()
    integer :: level = 1
    type(t_entry), pointer :: e => null()
  end type t_node

  type t_tree_map
    private
    type(t_node), pointer :: root => null()
    integer :: deflt = 0
  end type t_tree_map

contains

  function new_entry(key,val) result(e)
    integer, intent(in) :: key
    integer, intent(in) :: val
    type(t_entry), pointer :: e
    e => null()
    allocate(e)
    e%key = key
    e%val = val
  end

  function new_node(e) result(n)
    type(t_entry), pointer, intent(in) :: e
    type(t_node), pointer :: n
    n => null()
    allocate(n)
    n%e => e
  end

  integer function level(n)
    type(t_node), pointer, intent(in) :: n
    level = 0
    if (.not.associated(n)) return
    level = n%level
  end

  logical function is_leaf(n)
    type(t_node), pointer, intent(in) :: n
    is_leaf = associated(n) .and. .not.associated(n%left) .and. &
    &         .not.associated(n%right)
  end

  recursive function tree_size(n) result(s)
    type(t_node), pointer, intent(in) :: n
    integer :: s
    s = 0
    if (.not.associated(n)) return
    s = 1+tree_size(n%left)+tree_size(n%right)
  end

  function skew(n) result(l)
    type(t_node), pointer, intent(in) :: n
    type(t_node), pointer :: l
    l => n
    if (.not.(associated(n) .and. associated(n%left) .and. &
    &   n%left%level == n%level)) return
    l => n%left
    n%left => l%right
    l%right => n
  end

  function split(n) result(r)
    type(t_node), pointer, intent(in) :: n
    type(t_node), pointer :: r
    r => n
    if (.not.(associated(n) .and. associated(n%right) .and. &
    &   associated(n%right%right) .and. n%right%right%level == n%level)) return
    r => n%right
    n%right => r%left
    r%left => n
    r%level = r%level+1
  end

  function predecessor(n) result(p)
    type(t_node), pointer, intent(in) :: n
    type(t_node), pointer :: p
    p => null()
    if (.not.associated(n%left)) return
    p => n%left
    do while (associated(p%right))
      p => p%right
    end do
  end

  function successor(n) result(s)
    type(t_node), pointer, intent(in) :: n
    type(t_node), pointer :: s
    s => null()
    if (.not.associated(n%right)) return
    s => n%right
    do while (associated(s%left))
      s => s%left
    end do
  end

  recursive function insert(n,e) result(t)
    type(t_node), pointer, intent(in) :: n
    type(t_entry), pointer, intent(in) :: e
    type(t_node), pointer :: t
    t => new_node(e)
    if (.not.associated(n)) return
    t => n
    if (e%key < t%e%key) then
      t%left => insert(t%left,e)
    else if (e%key > t%e%key) then
      t%right => insert(t%right,e)
    else
      t%e => e
    end if
    t => skew(t)
    t => split(t)
  end

  recursive function delete(n,e) result(t)
    type(t_node), pointer, intent(in) :: n
    type(t_entry), pointer, intent(in) :: e
    type(t_node), pointer :: t, l
    t => n
    if (.not.associated(n)) return
    if (e%key < t%e%key) then
      t%left => delete(t%left,e)
    else if (e%key > t%e%key) then
      t%right => delete(t%right,e)
    else
      if (is_leaf(t)) return
      if (.not.associated(t%left)) then
        l => successor(t)
        t%right => delete(t%right,l%e)
        t%e => l%e
      else
        l => predecessor(t)
        t%left => delete(t%left,l%e)
        t%e => l%e
      end if
    end if
    t => decrease_level(t)
    t => skew(t)
    t%right => skew(t%right)
    if (associated(t%right)) t%right%right => skew(t%right%right)
    t => split(t)
    t%right => split(t%right)
  end

  function decrease_level(n) result(t)
    type(t_node), pointer, intent(in) :: n
    type(t_node), pointer :: t
    integer :: should_be
    t => n
    should_be = min(level(t%left),level(t%right))+1
    if (t%level > should_be) then
      t%level = should_be
      if (level(t%right) > should_be) t%right%level = should_be
    end if
  end

  recursive subroutine release_tree(t)
    type(t_node), pointer, intent(inout) :: t
    if (.not.associated(t)) return
    call release_tree(t%left)
    call release_tree(t%right)
    deallocate(t)
  end

  recursive subroutine get_keys_list(t,keys,num)
    type(t_node), pointer, intent(in) :: t
    integer, intent(inout) :: keys(:)
    integer, intent(inout) :: num
    if (.not.associated(t)) return
    call get_keys_list(t%left,keys,num)
    num = num+1
    keys(num) = t%e%key
    call get_keys_list(t%right,keys,num)
  end

  integer function size_of(map)
    type(t_tree_map), intent(in) :: map
    size_of = tree_size(map%root)
  end

  subroutine clear(map)
    type(t_tree_map), intent(inout) :: map
    call release_tree(map%root)
    map%root => null()
  end

  subroutine set_default(map,deflt)
    type(t_tree_map), intent(inout) :: map
    integer, intent(in) :: deflt
    map%deflt = deflt
  end

  subroutine put_entry(map,e)
    type(t_tree_map), intent(inout) :: map
    type(t_entry), pointer, intent(in) :: e
    map%root => insert(map%root,e)
  end

  subroutine remove_entry(map,e)
    type(t_tree_map), intent(inout) :: map
    type(t_entry), pointer, intent(in) :: e
    map%root => delete(map%root,e)
  end

  function get_entry(map,e) result(ret)
    type(t_tree_map), intent(inout) :: map
    type(t_entry), pointer, intent(in) :: e
    type(t_node), pointer :: n
    type(t_entry), pointer :: ret
    ret => null()
    n => map%root
    do while (associated(n))
      if (e%key < n%e%key) then
        n => n%left
      else if (e%key > n%e%key) then
        n => n%right
      else
        ret => n%e
        return
      end if
    end do
  end

  function contain_entry(map,e) result(ret)
    type(t_tree_map), intent(inout) :: map
    type(t_entry), pointer, intent(in) :: e
    type(t_node), pointer :: n
    logical :: ret
    ret = .false.
    n => map%root
    do while (associated(n))
      if (e%key < n%e%key) then
        n => n%left
      else if (e%key > n%e%key) then
        n => n%right
      else
        ret = .true.
        return
      end if
    end do
  end

  function get_first_entry(map) result(ret)
    type(t_tree_map), intent(inout) :: map
    type(t_node), pointer :: n
    type(t_entry), pointer :: ret
    ret => null()
    n => map%root
    if (.not.associated(n)) return
    do while (associated(n%left))
      n => n%left
    end do
    ret => n%e
  end

  function poll_first_entry(map) result(ret)
    type(t_tree_map), intent(inout) :: map
    type(t_node), pointer :: n
    type(t_entry), pointer :: ret
    ret => null()
    n => map%root
    if (.not.associated(n)) return
    do while (associated(n%left))
      n => n%left
    end do
    ret => n%e
    map%root => delete(map%root,ret)
  end

  function get_last_entry(map) result(ret)
    type(t_tree_map), intent(inout) :: map
    type(t_node), pointer :: n
    type(t_entry), pointer :: ret
    ret => null()
    n => map%root
    if (.not.associated(n)) return
    do while (associated(n%right))
      n => n%right
    end do
    ret => n%e
  end

  function poll_last_entry(map) result(ret)
    type(t_tree_map), intent(inout) :: map
    type(t_node), pointer :: n
    type(t_entry), pointer :: ret
    ret => null()
    n => map%root
    if (.not.associated(n)) return
    do while (associated(n%right))
      n => n%right
    end do
    ret => n%e
    map%root => delete(map%root,ret)
  end

  function floor_entry(map,e) result(ret)
    type(t_tree_map), intent(inout) :: map
    type(t_entry), pointer, intent(in) :: e
    type(t_node), pointer :: n
    type(t_entry), pointer :: ret
    ret => null()
    n => map%root
    do while (associated(n))
      if (e%key < n%e%key) then
        n => n%left
      else if (e%key > n%e%key) then
        if (.not.associated(ret)) then
          ret => n%e
          cycle
        end if
        if (e%key-ret%key > e%key-n%e%key) ret => n%e
        n => n%right
      else
        ret => n%e
        return
      end if
    end do
  end

  function lower_entry(map,e) result(ret)
    type(t_tree_map), intent(inout) :: map
    type(t_entry), pointer, intent(in) :: e
    type(t_entry), pointer :: ret
    ret => floor_entry(map,new_entry(e%key-1,0))
  end

  function ceiling_entry(map,e) result(ret)
    type(t_tree_map), intent(inout) :: map
    type(t_entry), pointer, intent(in) :: e
    type(t_node), pointer :: n
    type(t_entry), pointer :: ret
    ret => null()
    n => map%root
    do while (associated(n))
      if (e%key < n%e%key) then
        if (.not.associated(ret)) then
          ret => n%e
          cycle
        end if
        if (e%key-ret%key < e%key-n%e%key) ret => n%e
        n => n%left
      else if (e%key > n%e%key) then
        n => n%right
      else
        ret => n%e
        return
      end if
    end do
  end

  function higher_entry(map,e) result(ret)
    type(t_tree_map), intent(inout) :: map
    type(t_entry), pointer, intent(in) :: e
    type(t_entry), pointer :: ret
    ret => ceiling_entry(map,new_entry(e%key+1,0))
  end

  subroutine get_keys(map,keys,num)
    type(t_tree_map), intent(inout) :: map
    integer, intent(inout) :: keys(:)
    integer, intent(inout) :: num
    keys = 0
    num = 0
    call get_keys_list(map%root,keys,num)
  end

  subroutine put(map,key,val)
    type(t_tree_map), intent(inout) :: map
    integer, intent(in) :: key
    integer, intent(in) :: val
    call put_entry(map,new_entry(key,val))
  end

  subroutine remove(map,key)
    type(t_tree_map), intent(inout) :: map
    integer, intent(in) :: key
    call remove_entry(map,new_entry(key,0))
  end

  function get(map,key) result(val)
    type(t_tree_map), intent(inout) :: map
    integer, intent(in) :: key
    type(t_entry), pointer :: tmp
    integer :: val
    val = map%deflt
    tmp => get_entry(map,new_entry(key,0))
    if (.not.associated(tmp)) return
    val = tmp%val
  end

  logical function contain(map,key)
    type(t_tree_map), intent(inout) :: map
    integer, intent(in) :: key
    contain = contain_entry(map,new_entry(key,0))
  end

  function get_first_key(map) result(key)
    type(t_tree_map), intent(inout) :: map
    type(t_entry), pointer :: tmp
    integer :: key
    key = map%deflt
    tmp => get_first_entry(map)
    if (.not.associated(tmp)) return
    key = tmp%key
  end

  function get_last_key(map) result(key)
    type(t_tree_map), intent(inout) :: map
    type(t_entry), pointer :: tmp
    integer :: key
    key = map%deflt
    tmp => get_last_entry(map)
    if (.not.associated(tmp)) return
    key = tmp%key
  end

  function floor_key(map,key) result(ret)
    type(t_tree_map), intent(inout) :: map
    integer, intent(in) :: key
    type(t_entry), pointer :: tmp
    integer :: ret
    ret = map%deflt
    tmp => floor_entry(map,new_entry(key,0))
    if (.not.associated(tmp)) return
    ret = tmp%key
  end

  function lower_key(map,key) result(ret)
    type(t_tree_map), intent(inout) :: map
    integer, intent(in) :: key
    type(t_entry), pointer :: tmp
    integer :: ret
    ret = map%deflt
    tmp => lower_entry(map,new_entry(key,0))
    if (.not.associated(tmp)) return
    ret = tmp%key
  end

  function ceiling_key(map,key) result(ret)
    type(t_tree_map), intent(inout) :: map
    integer, intent(in) :: key
    type(t_entry), pointer :: tmp
    integer :: ret
    ret = map%deflt
    tmp => ceiling_entry(map,new_entry(key,0))
    if (.not.associated(tmp)) return
    ret = tmp%key
  end

  function higher_key(map,key) result(ret)
    type(t_tree_map), intent(inout) :: map
    integer, intent(in) :: key
    type(t_entry), pointer :: tmp
    integer :: ret
    ret = map%deflt
    tmp => higher_entry(map,new_entry(key,0))
    if (.not.associated(tmp)) return
    ret = tmp%key
  end

end module mod_tree_map
program ABC152E
    use mod_tree_map
    implicit none
    integer::N
    integer,allocatable,dimension(:)::A
    type(t_tree_map),allocatable,dimension(:)::AYK
    type(t_tree_map)::KYK
    integer::i,j,k,cnt,JOU
    integer(16)::PLUS,KAKE
    integer::ans=0,mo=10**9+7
    read*,N
    allocate(A(N),AYK(N))
    read*,A
    do i=1,N
        do j=2,int(sqrt(real(A(i))+1))
            if(mod(A(i),j)/=0)cycle
            cnt=0
            do while(mod(A(i),j)==0)
                cnt=cnt+1
                A(i)=A(i)/j
            end do
            if(cnt/=0)then
                call put(AYK(i),j,cnt)
                if(get(KYK,j)<cnt)then
                    call put(KYK,j,cnt)
                endif
            endif
        end do
        if(A(i)/=1)then
            call put(AYK(i),A(i),1)
            if(get(KYK,A(i))<1)then
                call put(KYK,A(i),1)
            endif
        endif
    end do

    do i=1,N
        PLUS=1
        j=get_first_key(KYK)
        do while(j/=0)
            KAKE=j**(get(KYK,j)-get(AYK(i),j))
            if(KAKE>=mo)KAKE=mod(KAKE,mo)
            PLUS=PLUS*KAKE
            if(PLUS>=mo)PLUS=mod(PLUS,mo)
            j=higher_key(KYK,j)
        end do
        ANS=mod(ANS+PLUS,mo)
    end do
    print"(i0)",ANS
contains
end program ABC152E


program keyence2020A
    implicit none
    integer(8)::H,W,N
    read*,H,W,N
    print'(i0)',N/max(H,W)+min(1,mod(N,max(H,W)))
end program keyence2020A

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

Score : 100 points

Problem Statement

We have a grid with H rows and W columns, where all the squares are initially white.

You will perform some number of painting operations on the grid.
In one operation, you can do one of the following two actions:

Choose one row, then paint all the squares in that row black.

Choose one column, then paint all the squares in that column black.

At least how many operations do you need in order to have N or more black squares in the grid?
It is guaranteed that, under the conditions in Constraints, having N or more black squares is always possible by performing some number of operations.

Constraints

1 \leq H \leq 100

1 \leq W \leq 100

1 \leq N \leq H \times W

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H
W
N

Output

Print the minimum number of operations needed.

Sample Input 1

3
7
10

Sample Output 1

2

You can have 14 black squares in the grid by performing the "row" operation twice, on different rows.

Sample Input 2

14
12
112

Sample Output 2

8

Sample Input 3

2
100
200

Sample Output 3

2

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 key2020_A
    implicit none
    integer::H,W,N
    read*,H,W,N
    print"(i0)",(N-1)/max(H,W)+1
end program key2020_A


program ap
  implicit none
  integer :: ans, larg, cnttai,in
  integer :: h, w , n, i, sumers, csum, err, j, jc(1)
  integer,allocatable,dimension(:) :: X, xmax, xmin
  integer,allocatable,dimension(:) :: L, cnt,ers
    integer,allocatable,dimension(:,:) :: taio

  read(*,*) n

  allocate( X(n) )
  allocate( L(n) )
  allocate( xmax(n) )
  allocate( xmin(n) )
  allocate( cnt(n) )
  allocate( ers(n) )
  allocate( taio(n,n) )

  do i=1,n
     read(*,*) X(i), L(i)
     xmax(i)=X(i)+L(i)
     xmin(i)=X(i)-L(i)
  enddo

  ers(:)=0
  cnt(:)=0
  sumers=0
  csum=0
  taio(:,:)=0
  do i=1,n
     do j=1,n
        if(i.eq.j) goto 300
        w=0
        if(xmax(i).gt.xmin(j).and.xmax(i).le.xmax(j)) then
           w=w+1
        endif
        if(xmin(i).lt.xmax(j).and.xmin(i).ge.xmin(j)) then
           w=w+1
        endif

        if(w.eq.2) then
           ers(j)=1
!           cnt(j)=cnt(j)+1
           taio(j,i)=1
           print *, i, j
        elseif(w.eq.1) then
!           cnt(j)=cnt(j)+1
           taio(j,i)=1
        endif
        300 continue
     enddo
  enddo
  do j=1,n
     do i=1,n
        csum=csum+taio(j,i)
        !     sumers=sumers + ers(j)
     enddo
  enddo
  err=0

  do j=1,n
     if(ers(j).eq.1) then
        do i=1,n
           cnttai=cnttai+taio(j,i)
        enddo
        csum=csum-cnttai*2
        taio(j,:)=0
        taio(:,j)=0
        err=err+1
     endif
  enddo
!  print *, err

  if(sum(taio).le.0) goto 400

  cnt(:)=0
  do j =1,n
     do i=1,n
        cnt(j)=cnt(j)+taio(j,i)
     enddo
  enddo

  do i=1,n

     cnt(:)=0
     do j =1,n
        do in=1,n
           cnt(j)=cnt(j)+taio(j,in)
        enddo
     enddo

     jc=maxloc(cnt)
     err=err+1
     cnt(jc(1))=0
     taio(jc(i),:)=0
     taio(:,jc(i))=0
     if(sum(taio).le.0) goto 400

  enddo
400 continue

  print *,  n-err

end program ap

Score : 200 points

Problem Statement

In a factory, there are N robots placed on a number line.
Robot i is placed at coordinate X_i and can extend its arms of length L_i in both directions, positive and negative.

We want to remove zero or more robots so that the movable ranges of arms of no two remaining robots intersect.
Here, for each i (1 \leq i \leq N), the movable range of arms of Robot i is the part of the number line between the coordinates X_i - L_i and X_i + L_i, excluding the endpoints.

Find the maximum number of robots that we can keep.

Constraints

1 \leq N \leq 100,000

0 \leq X_i \leq 10^9 (1 \leq i \leq N)

1 \leq L_i \leq 10^9 (1 \leq i \leq N)

If i \neq j, X_i \neq X_j.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
X_1 L_1
X_2 L_2
\vdots
X_N L_N

Output

Print the maximum number of robots that we can keep.

Sample Input 1

4
2 4
4 3
9 3
100 5

Sample Output 1

3

By removing Robot 2, we can keep the other three robots.

Sample Input 2

2
8 20
1 10

Sample Output 2

1

Sample Input 3

5
10 1
2 1
4 1
6 1
8 1

Sample Output 3

5


program subarray_sum
  implicit none
  integer :: n, k, s, i, l = 1
  read(*,*) n, k, s
  if (s < 10**9) l = s+1
  do i = 1, k
    write(*,'(i0)') s
  end do
  do i = k+1, n
    write(*,'(i0)') l
  end do
end program subarray_sum

Score : 400 points

Problem Statement

Given are three integers N, K, and S.

Find a sequence A_1, A_2, ..., A_N of N integers between 1 and 10^9 (inclusive) that satisfies the condition below.
We can prove that, under the conditions in Constraints, such a sequence always exists.

There are exactly K pairs (l, r) of integers such that 1 \leq l \leq r \leq N and A_l + A_{l + 1} + \cdots + A_r = S.

Constraints

1 \leq N \leq 10^5

0 \leq K \leq N

1 \leq S \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K S

Output

Print a sequence satisfying the condition, in the following format:

A_1 A_2 ... A_N

Sample Input 1

4 2 3

Sample Output 1

1 2 3 4

Two pairs (l, r) = (1, 2) and (3, 3) satisfy the condition in the statement.

Sample Input 2

5 3 100

Sample Output 2

50 50 50 30 70


program key2020_C
    implicit none
    integer::N,K,S
    integer::i
    read*,N,K,S
    if(K/=0)then
        write(*,"(i0)",advance="no")S
    else
        write(*,"(i0)",advance="no")S+1
    endif
    do i=2,K
        write(*,"(A,i0)",advance="no")" ",S
    end do
    do i=K+1,N
         write(*,"(A,i0)",advance="no")" ",S+1
    end do
    write(*,*)
end program key2020_C


program main

  implicit none

  character(1) :: x

  read(*,*) x

  print*, achar( ichar(x)+1 )

end program main

Score : 100 points

Problem Statement

Given is a lowercase English letter C that is not z. Print the letter that follows C in alphabetical order.

Constraints

C is a lowercase English letter that is not z.

Input

Input is given from Standard Input in the following format:

C

Output

Print the letter that follows C in alphabetical order.

Sample Input 1

a

Sample Output 1

b

a is followed by b.

Sample Input 2

y

Sample Output 2

z

y is followed by z.


program ABC151A
    implicit none
    character  C
    read*,C
    print"(A)",char(ichar(C)+1)
end program ABC151A


program ABC151C
    implicit none
    integer(8)::N,M,p,i
    integer(8)::ac=0
    integer(8)::pen=0
    character(2)::S
    integer(8),allocatable,dimension(:)::flgs,w_count

    read(5,*)N,M
    allocate(flgs(N))
    allocate(w_count(N))
    flgs=0
    w_count=0
    if(M/=0)then
        do i=1,M
            read(5,*)p,S

            if(S=="WA")then
                if(flgs(p)==0)then
                    w_count(p)=w_count(p)+1
                end if
            else if(S=="AC")then
                if(flgs(p)==0)then
                    ac=ac+1
                    flgs(p)=1
                end if
            end if
        end do

        do i=1,N
            pen=pen+w_count(p)
        end do
    end if

    print'(i0,1x,i0)',ac,pen
end program ABC151C

Score : 300 points

Problem Statement

Takahashi participated in a contest on AtCoder.

The contest had N problems.

Takahashi made M submissions during the contest.

The i-th submission was made for the p_i-th problem and received the verdict S_i (AC or WA).

The number of Takahashi's correct answers is the number of problems on which he received an AC once or more.

The number of Takahashi's penalties is the sum of the following count for the problems on which he received an AC once or more: the number of WAs received before receiving an AC for the first time on that problem.

Find the numbers of Takahashi's correct answers and penalties.

Constraints

N, M, and p_i are integers.

1 \leq N \leq 10^5

0 \leq M \leq 10^5

1 \leq p_i \leq N

S_i is AC or WA.

Input

Input is given from Standard Input in the following format:

N M
p_1 S_1
:
p_M S_M

Output

Print the number of Takahashi's correct answers and the number of Takahashi's penalties.

Sample Input 1

2 5
1 WA
1 AC
2 WA
2 AC
2 WA

Sample Output 1

2 2

In his second submission, he received an AC on the first problem for the first time. Before this, he received one WA on this problem.

In his fourth submission, he received an AC on the second problem for the first time. Before this, he received one WA on this problem.

Thus, he has two correct answers and two penalties.

Sample Input 2

100000 3
7777 AC
7777 AC
7777 AC

Sample Output 2

1 0

Note that it is pointless to get an AC more than once on the same problem.

Sample Input 3

6 0

Sample Output 3

0 0


module deque_mod
    use,intrinsic :: iso_fortran_env
    implicit none
    type deque
        integer(int32),pointer:: v(:)
        integer(int32):: l,r
        integer(int32):: lmax, rmax
    contains
        procedure:: append => dq_append
        procedure:: appendleft => dq_appendleft
        procedure:: pop => dq_pop
        procedure:: popleft => dq_popleft
        procedure:: right => dq_right
        procedure:: left => dq_left
        procedure:: remaining_elements => dq_remaining_elements
        procedure:: remain => dq_remain
    end type

    interface deque
        procedure:: dq_init
    end interface
contains
    function dq_init() result(dq)
        ! コンストラクタ
        type(deque):: dq
        dq%r=0; dq%rmax=1
        dq%l=1; dq%lmax=-1
        allocate(dq%v(dq%lmax:dq%rmax))
    end function

    subroutine dq_append(dq,num)
        ! 右端に挿入
        class(deque):: dq
        integer(int32):: num
        if (dq%r+1 > dq%rmax) call dq_add_(dq)
        dq%r=dq%r+1
        dq%v(dq%r) = num
    end subroutine

    subroutine dq_appendleft(dq,num)
        ! 左端に挿入
        class(deque):: dq
        integer(int32):: num
        if (dq%l-1 < dq%lmax) call dq_add_(dq)
        dq%l=dq%l-1
        dq%v(dq%l) = num
    end subroutine

    subroutine dq_add_(dq)
        ! arrayの延長
        class(deque):: dq
        integer(int32):: l
        integer(int32),pointer:: tmp(:)
        l = size(dq%v)
        allocate(tmp(l))
        tmp(:) = dq%v(:)
        deallocate(dq%v)
        allocate(dq%v(2*dq%lmax:2*dq%rmax))
        dq%v(dq%lmax:dq%rmax) = tmp(:)
        dq%lmax = 2*dq%lmax
        dq%rmax = 2*dq%rmax
    end subroutine

    function dq_pop(dq) result(ret)
        ! 右端から取り出し
        class(deque):: dq
        integer(int32):: ret
        ret = dq%v(dq%r)
        dq%r=dq%r-1
    end function

    function dq_popleft(dq) result(ret)
        ! 左端から取り出し
        class(deque):: dq
        integer(int32):: ret
        ret = dq%v(dq%l)
        dq%l=dq%l+1
    end function

    function dq_right(dq) result(ret)
        ! 右端を確認
        class(deque):: dq
        integer(int32):: ret
        ret = dq%v(dq%r)
    end function

    function dq_left(dq) result(ret)
        ! 左端を確認
        class(deque):: dq
        integer(int32):: ret
        ret = dq%v(dq%l)
    end function

    function dq_remaining_elements(dq) result(ret)
        ! 残りの要素数
        class(deque):: dq
        integer(int32):: ret
        ret = dq%r - dq%l + 1
    end function

    function dq_remain(dq) result(ret)
        ! 要素が残っているかどうか
        class(deque):: dq
        logical:: ret
        ret = dq%remaining_elements() > 0
    end function
end module

program abc151
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: h,w
    integer(int32):: x,y,ans
    character(int32), allocatable:: s(:,:)

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

    inpt_s : block
        character(w):: inpt
        do y=1,h
            read*, inpt
            do x=1,w
                s(x,y) = inpt(x:x)
            end do
        end do
    end block inpt_s
    ans=0
    do y=1,h
        do x=1,w
            if (s(x,y) == '#') cycle
            ans = max(ans, solve(x,y))
        end do
    end do
    print'(i0)', ans
contains
    function solve(x,y) result(ret)
        use deque_mod
        integer(int32):: x, y, ret
        integer(int32):: nx, ny
        integer(int32):: dx, dy, i
        integer(int32):: turn(w,h)
        integer(int32),parameter:: d(2,4) = reshape([1,0,0,1,-1,0,0,-1], [2,4])
        logical:: pass(w,h)
        type(deque):: task_x, task_y

        task_x = deque()
        task_y = deque()
        pass(:,:) = .false.
        turn(:,:) = 0

        call task_x%append(x)
        call task_y%append(y)

        do while(task_x%remain())
            nx = task_x%popleft()
            ny = task_y%popleft()
            pass(nx,ny) = .true.
            do i=1,4
                dx = d(1,i)
                dy = d(2,i)
                if (.not. in_maze(nx+dx,ny+dy)) cycle
                if (s(nx+dx,ny+dy) == '#') cycle
                if (pass(nx+dx,ny+dy)) cycle
                turn(nx+dx,ny+dy) = turn(nx,ny)+1
                call task_x%append(nx+dx)
                call task_y%append(ny+dy)
            end do
        end do
        ! print*, '---', x,y
        ! do i=1,h
        !     print'(*(i0,1x))', turn(:,i)
        ! end do

        ret = maxval(turn)
    end function

    function in_maze(x,y) result(ret)
        integer(int32):: x,y
        logical:: in_x, in_y, ret

        in_x = 1 <= x .and. x <= w
        in_y = 1 <= y .and. y <= h
        ret = in_x .and. in_y
    end function
end program abc151

Score : 400 points

Problem Statement

Takahashi has a maze, which is a grid of H \times W squares with H horizontal rows and W vertical columns.

The square at the i-th row from the top and the j-th column is a "wall" square if S_{ij} is #, and a "road" square if S_{ij} is ..

From a road square, you can move to a horizontally or vertically adjacent road square.

You cannot move out of the maze, move to a wall square, or move diagonally.

Takahashi will choose a starting square and a goal square, which can be any road squares, and give the maze to Aoki.

Aoki will then travel from the starting square to the goal square, in the minimum number of moves required.

In this situation, find the maximum possible number of moves Aoki has to make.

Constraints

1 \leq H,W \leq 20

S_{ij} is . or #.

S contains at least two occurrences of ..

Any road square can be reached from any road square in zero or more moves.

Input

Input is given from Standard Input in the following format:

H W
S_{11}...S_{1W}
:
S_{H1}...S_{HW}

Output

Print the maximum possible number of moves Aoki has to make.

Sample Input 1

3 3
...
...
...

Sample Output 1

4

If Takahashi chooses the top-left square as the starting square and the bottom-right square as the goal square, Aoki has to make four moves.

Sample Input 2

3 5
...#.
.#.#.
.#...

Sample Output 2

10

If Takahashi chooses the bottom-left square as the starting square and the top-right square as the goal square, Aoki has to make ten moves.


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

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

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

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

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

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

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

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

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

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

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

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

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

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

      PROGRAM mazeMaster
      use intQueueMod
      implicit none
      integer :: h,w,ans=0
      logical,allocatable :: maze(:,:),isVisited(:,:)
      character(20) :: s
      integer :: i,j
      integer,allocatable :: dist(:,:)

      read*,h,w
      allocate( maze(h,w),isVisited(h,w),source=.true. )
      allocate( dist(h,w),source=0 )
      do i = 1,h
        read*,s(1:w)
        do j = 1,w
          if(s(j:j)=='#') maze(i,j)=.false.
        end do
      end do

      do i = 1,h
        do j = 1,w

          if( .not.maze(i,j) )cycle
          call solve( i,j )
          ans = max(ans,maxval(dist))

        end do
      end do

      print*,ans

      contains
      subroutine solve( i,j )
        implicit none
        integer,intent(in) :: i,j
        type(intQueue) :: Qh,Qw,Qd
        integer :: nowi,nowj,ii(4),jj(4),iter

        isVisited(:,:) = .false.
        dist(:,:) = 0
        call Qh%enQueue(i)
        call Qw%enQueue(j)
        call Qd%enQueue(0)

        do while(.true.)
          if( Qh%isEmpty() )return

          nowi = Qh%dequeue()
          nowj = Qw%dequeue()
          dist(nowi,nowj) = Qd%dequeue()
          isVisited(nowi,nowj) = .true.

          ii = [-1,0,1,0]
          jj = [0,-1,0,1]

          do iter = 1,4
              if( nowi+ii(iter)<1 .or. h<nowi+ii(iter) .or. &
                  nowj+jj(iter)<1 .or. w<nowj+jj(iter) ) cycle
              if( .not.(maze(nowi+ii(iter),nowj+jj(iter))) ) cycle
              if( isVisited(nowi+ii(iter),nowj+jj(iter)) ) cycle

              isVisited(nowi+ii(iter),nowj+jj(iter) ) = .true.
              call Qh%enQueue( nowi+ii(iter) )
              call Qw%enQueue( nowj+jj(iter) )
              call Qd%enQueue( dist(nowi,nowj)+1 )
          end do !iter
        end do
      end subroutine

      END PROGRAM


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

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

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

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

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

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

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

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

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

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

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

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

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

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

      PROGRAM mazeMaster
      use intQueueMod
      implicit none
      integer :: h,w,ans=0
      logical,allocatable :: maze(:,:),isVisited(:,:)
      character(20) :: s
      integer :: i,j
      integer,allocatable :: dist(:,:)

      read*,h,w
      allocate( maze(h,w),isVisited(h,w),source=.true. )
      allocate( dist(h,w),source=0 )
      do i = 1,h
        read*,s(1:w)
        do j = 1,w
          if(s(j:j)=='#') maze(i,j)=.false.
        end do
      end do

      do i = 1,h
        do j = 1,w

          if( .not.maze(i,j) )cycle
          call solve( i,j )

          ans = max(ans,maxval(dist))

        end do
      end do

      print*,ans

      contains
      subroutine solve( i,j )
        implicit none
        integer,intent(in) :: i,j
        type(intQueue) :: Qh,Qw,Qd
        integer :: nowi,nowj,ii(4),jj(4),iter

        isVisited(:,:) = .false.
        dist(:,:) = 0
        call Qh%enQueue(i)
        call Qw%enQueue(j)
        call Qd%enQueue(0)

        do while(.true.)
          if( Qh%isEmpty() )return

          nowi = Qh%dequeue()
          nowj = Qw%dequeue()
          dist(nowi,nowj) = Qd%dequeue()
          isVisited(nowi,nowj) = .true.

          ii = [-1,0,1,0]
          jj = [0,-1,0,1]

          do iter = 1,4
              if( nowi+ii(iter)<1 .or. h<nowi+ii(iter) .or. &
                  nowj+jj(iter)<1 .or. w<nowj+jj(iter) ) cycle
              if( .not.(maze(nowi+ii(iter),nowj+jj(iter))) ) cycle
              if( isVisited(nowi+ii(iter),nowj+jj(iter)) ) cycle

              call Qh%enQueue( nowi+ii(iter) )
              call Qw%enQueue( nowj+jj(iter) )
              call Qd%enQueue( dist(nowi,nowj)+1 )
          end do !iter
        end do
      end subroutine

      END PROGRAM


module mod_queue
  implicit none
  type t_node
    private
    integer :: item
    type(t_node), pointer :: prev => null()
    type(t_node), pointer :: next => null()
  end type
  type t_queue
    private
    integer :: num = 0
    type(t_node), pointer :: head => null()
    type(t_node), pointer :: tail => null()
  contains
    procedure :: enqueue => enqueue
    procedure :: dequeue => dequeue
    procedure :: peek => peek
    procedure :: clear => clear
    procedure :: size => size_of
  end type
contains
  function new_node(item) result(node)
    integer, intent(in) :: item
    type(t_node), pointer :: node
    allocate(node)
    node%item = item
  end
  subroutine enqueue(this,item)
    class(t_queue), intent(inout) :: this
    integer, intent(in) :: item
    type(t_node), pointer :: node
    node => new_node(item)
    if (associated(this%head)) then
      node%prev => this%tail
      this%tail%next => node
    else
      this%head => node
    end if
    this%tail => node
    this%num = this%num+1
  end
  function dequeue(this) result(item)
    class(t_queue), intent(inout) :: this
    integer :: item
    type(t_node), pointer :: node
    item = this%head%item
    node => this%head%next
    deallocate(this%head)
    this%head => node
    if (associated(node)) then
      node%prev => null()
    else
      this%tail => null()
    end if
    this%num = this%num-1
  end
  function peek(this) result(item)
    class(t_queue), intent(in) :: this
    integer :: item
    item = this%head%item
  end
  subroutine clear(this)
    class(t_queue), intent(inout) :: this
    type(t_node), pointer :: node, next
    if (.not.associated(this%head)) return
    node => this%head
    do while (associated(node%next))
      next => node%next
      deallocate(node)
      node => next
    end do
    this%head => null()
    this%tail => null()
    this%num = 0
  end
  integer function size_of(this)
    class(t_queue), intent(in) :: this
    size_of = this%num
  end
end module mod_queue
program maze_master
  implicit none
  integer :: h, w, a(0:21,0:21) = -1, i, j, m = 0
  integer :: di(4) = (/-1,1,0,0/), dj(4) = (/0,0,-1,1/)
  logical :: b(20,20)
  character(20) :: s
  read(*,*) h, w
  do i = 1, h
    read(*,*) s
    do j = 1, w
      if (s(j:j) == ".") a(i,j) = 0
    end do
  end do
  do i = 1, h
    do j = 1, w
      if (a(i,j) /= -1) then
        b = .false.
        m = max(m,bfs(i,j))
      end if
    end do
  end do
  write(*,'(i0)') m
contains
  integer function bfs(i,j)
    use mod_queue
    integer, intent(in) :: i, j
    integer :: k, p, q, x, y, idx
    type(t_queue) :: queue
    a(i,j) = 0
    call queue%enqueue(i*(w+1)+j)
    do while (queue%size() > 0)
      idx = queue%dequeue()
      p = idx/(w+1)
      q = idx-p*(w+1)
      b(p,q) = .true.
      do k = 1, 4
        x = p+di(k)
        y = q+dj(k)
        if (a(x,y) /= -1 .and. ((.not. b(x,y)) .or. a(x,y) > a(p,q)+1)) then
          a(x,y) = a(p,q)+1
          call queue%enqueue(x*(w+1)+y)
        end if
      end do
    end do
    bfs = maxval(a(1:h,1:w))
  end
end program maze_master


program ABC151F
    implicit none
    integer::N
    integer,allocatable,dimension(:,:)::xy
    real(16)::ANS
    integer::i,j,k
    read*,N
    allocate(xy(N,2))
    do i=1,N
        read*,xy(i,1),xy(i,2)
    end do
    if(N==2)then
        print"(f0.14)",dist(1,2)/2.0
        stop
    endif
    ANS=0
    do i=1,N-2
        do j=i+1,N-1
            do k=j+1,N
                if((XY(k,1)-XY(i,1))*(XY(j,2)-XY(i,2))==(XY(k,2)-XY(i,2))*(XY(j,1)-XY(i,1)))then
                    ANS=max(ANS,max(dist(i,j),dist(i,k),dist(j,k))/real(2.0,16))
                endif
                ANS=max(ANS,min(R(i,j,k)/real(2.0,16),max(dist(i,j),dist(i,k),dist(j,k))/real(2,16)))
            end do
        end do
    end do
    print"(f0.20)",ANS
contains
function R(i,j,k)
    integer::i,j,k
    real(16) R
    R=dist(j,k)/sqrt(1-NAI(i,j,k)**2)
    if(1-NAI(i,j,k)**2<0.00000001)R=0
end function
function dist(i,j)
    integer::i,j
    real(16)::dist
    dist=sqrt(real((XY(i,1)-XY(j,1))**2+(XY(i,2)-XY(j,2))**2,16))
end function
function NAI(i,j,k)
    integer::i,j,k
    real(16)::NAI
    NAI=real((XY(j,1)-XY(i,1))*(XY(k,1)-XY(i,1))+(XY(j,2)-XY(i,2))*(XY(k,2)-XY(i,2)),kind=16)/dist(i,j)/dist(i,k)
end function
end program ABC151F

Score : 600 points

Problem Statement

Given are N points (x_i, y_i) in a two-dimensional plane.

Find the minimum radius of a circle such that all the points are inside or on it.

Constraints

2 \leq N \leq 50

0 \leq x_i \leq 1000

0 \leq y_i \leq 1000

The given N points are all different.

The values in input are all integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
:
x_N y_N

Output

Print the minimum radius of a circle such that all the N points are inside or on it.

Your output will be considered correct if the absolute or relative error from our answer is at most 10^{-6}.

Sample Input 1

2
0 0
1 0

Sample Output 1

0.500000000000000000

Both points are contained in the circle centered at (0.5,0) with a radius of 0.5.

Sample Input 2

3
0 0
0 1
1 0

Sample Output 2

0.707106781186497524

Sample Input 3

10
10 9
5 9
2 0
0 0
2 7
3 3
2 5
10 0
3 7
1 9

Sample Output 3

6.726812023536805158

If the absolute or relative error from our answer is at most 10^{-6}, the output will be considered correct.


program ABC151F
    implicit none
    integer::N
    integer,allocatable,dimension(:,:)::xy
    real(16)::ANS
    integer::i,j,k
    read*,N
    allocate(xy(N,2))
    do i=1,N
        read*,xy(i,1),xy(i,2)
    end do
    if(N==2)then
        print"(f0.14)",dist(1,2)/2.0
        stop
    endif
    ANS=0
    do i=1,N-2
        do j=i+1,N-1
            do k=j+1,N
                ANS=max(ANS,min(R(i,j,k)/2.0,max(dist(i,j)/2.0,dist(i,k)/2.0,dist(j,k)/2.0)))
            end do
        end do
    end do
    print"(f0.14)",ANS
contains
function R(i,j,k)
    integer::i,j,k
    real(16) R
    R=dist(j,k)/sqrt(1-NAI(i,j,k)**2)
    if(1-NAI(i,j,k)**2<0.00000001)R=0
end function
function dist(i,j)
    integer::i,j
    real(16)::dist
    dist=sqrt(real((XY(i,1)-XY(j,1))**2+(XY(i,2)-XY(j,2))**2,16))
end function
function NAI(i,j,k)
    integer::i,j,k
    real(16)::NAI
    NAI=real((XY(j,1)-XY(i,1))*(XY(k,1)-XY(i,1))+(XY(j,2)-XY(i,2))*(XY(k,2)-XY(i,2)),kind=16)/dist(i,j)/dist(i,k)
end function
end program ABC151F


program fusing_slimes
  implicit none
  integer(8), parameter :: md = 1000000007
  integer :: n, i, j
  integer(8) :: x(100000) = 0, p = 0, d = 0, k = 1
  read(*,*) n
  read(*,*) x(1:n)
  do i = 1, n-1
    p = mod(p+inv(int(i,8)),md)
    d = mod(d+mod((x(i+1)-x(i))*p,md),md)
    k = mod(k*i,md)
  end do
  write(*,'(i0)') mod(d*k,md)
contains
  function inv(n) result(y)
    integer(8), intent(in) :: n
    integer(8) :: a, b, x, y, t, q
    a = modulo(n,md)
    b = md
    x = 0_8
    y = 1_8
    do while (b /= 0_8)
      q = a/b
      t = b
      b = mod(a,b)
      a = t
      t = y
      y = x
      x = t-q*x
    end do
    y = modulo(y,md)
  end
end program fusing_slimes

Score : 600 points

Problem Statement

There are N slimes standing on a number line.
The i-th slime from the left is at position x_i.

It is guaruanteed that 1 \leq x_1 < x_2 < \ldots < x_N \leq 10^{9}.

Niwango will perform N-1 operations. The i-th operation consists of the following procedures:

Choose an integer k between 1 and N-i (inclusive) with equal probability.

Move the k-th slime from the left, to the position of the neighboring slime to the right.

Fuse the two slimes at the same position into one slime.

Find the total distance traveled by the slimes multiplied by (N-1)! (we can show that this value is an integer), modulo (10^{9}+7). If a slime is born by a fuse and that slime moves, we count it as just one slime.

Constraints

2 \leq N \leq 10^{5}

1 \leq x_1 < x_2 < \ldots < x_N \leq 10^{9}

x_i is an integer.

Subtasks

400 points will be awarded for passing the test cases satisfying N \leq 2000.

Input

Input is given from Standard Input in the following format:

N
x_1 x_2 \ldots x_N

Output

Print the answer.

Sample Input 1

3
1 2 3

Sample Output 1

5

With probability \frac{1}{2}, the leftmost slime is chosen in the first operation, in which case the total distance traveled is 2.

With probability \frac{1}{2}, the middle slime is chosen in the first operation, in which case the total distance traveled is 3.

The answer is the expected total distance traveled, 2.5, multiplied by 2!, which is 5.

Sample Input 2

12
161735902 211047202 430302156 450968417 628894325 707723857 731963982 822804784 880895728 923078537 971407775 982631932

Sample Output 2

750927044

Find the expected value multiplied by (N-1)!, modulo (10^9+7).


      PROGRAM piyo
      IMPLICIT NONE
      integer :: n
      character(50) :: s
      integer :: i, ans=0

      read*,n
      read*,s

      do i = 1,n-2
        if( s(i:i+2)=='ABC' )then
          ans = ans + 1
        end if
      end do

      print*,ans

      END PROGRAM

Score : 200 points

Problem Statement

We have a string S of length N consisting of uppercase English letters.

How many times does ABC occur in S as contiguous subsequences (see Sample Inputs and Outputs)?

Constraints

3 \leq N \leq 50

S consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print number of occurrences of ABC in S as contiguous subsequences.

Sample Input 1

10
ZABCDBABCQ

Sample Output 1

2

Two contiguous subsequences of S are equal to ABC: the 2-nd through 4-th characters, and the 7-th through 9-th characters.

Sample Input 2

19
THREEONEFOURONEFIVE

Sample Output 2

0

No contiguous subsequences of S are equal to ABC.

Sample Input 3

33
ABCCABCBABCCABACBCBBABCBCBCBCABCB

Sample Output 3

5


program count_abc
  implicit none
  integer :: n, i, m = 0
  character(50) :: s
  read(*,*) n
  read(*,*) s
  do i = 1, n-2
    if (s(i:i+2) == "ABC") m = m+1
  end do
  write(*,'(i0)') m
end program count_abc


program count_order
    implicit none
    integer(4):: n, i
    integer(4), allocatable:: p(:), q(:)
    integer(4), parameter:: bi(8) = [1, 2, 6, 24, 120, 720, 5040, 40320]

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

    do i = 1, n-1
        where(p(i+1:) > p(i))
            p(i+1:) = p(i+1:) - 1
        end where
        where( q(i+1:) > q(i))
            q(i+1:) = q(i+1:) - 1
        end where
        p(i) = p(i) * bi(n-i)
        q(i) = q(i) * bi(n-i)
    end do

    print*, abs(sum(p-q))
end program count_order

Score : 300 points

Problem Statement

We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).

There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Notes

For two sequences X and Y, X is said to be lexicographically smaller than Y if and only if there exists an integer k such that X_i = Y_i~(1 \leq i < k) and X_k < Y_k.

Constraints

2 \leq N \leq 8

P and Q are permutations of size N.

Input

Input is given from Standard Input in the following format:

N
P_1 P_2 ... P_N
Q_1 Q_2 ... Q_N

Output

Print |a - b|.

Sample Input 1

3
1 3 2
3 1 2

Sample Output 1

3

There are 6 permutations of size 3: (1,~2,~3), (1,~3,~2), (2,~1,~3), (2,~3,~1), (3,~1,~2), and (3,~2,~1). Among them, (1,~3,~2) and (3,~1,~2) come 2-nd and 5-th in lexicographical order, so the answer is |2 - 5| = 3.

Sample Input 2

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

Sample Output 2

17517

Sample Input 3

3
1 2 3
1 2 3

Sample Output 3

0


program semi_common_multiple
    implicit none
    integer(4):: n, m, i
    integer(4), allocatable:: a(:)
    integer(8):: gcd, lcm

    read*, n, m
    allocate(a(n))
    read*, a(:)
    a(:) = a(:) / 2

    do while(mod(a(1), 2) == 0)
        do i=1, n
            if(mod(a(i),2) /= 0) call die
            a(i) = a(i) / 2
        end do
        m = m / 2
    end do

    do i = 1, n
        if (mod(a(i), 2) == 0) call die
    end do

    lcm = 1

    do i=1,n
        gcd = calc_gcd(lcm,int(a(i),8))
        lcm = lcm/gcd*a(i)
    end do

    print*, (m/lcm+1_16)/2_16

contains
recursive function calc_gcd(a,b) result(ret)
integer(8):: a, b, ret
    if (mod(a,b) == 0)then
        ret = b
    else
    ret = calc_gcd(b,mod(a,b))
    endif
end function

subroutine die
    print*, 0
    stop
end subroutine
end program

Score : 400 points

Problem Statement

Given are a sequence A= {a_1,a_2,......a_N} of N positive even numbers, and an integer M.

Let a semi-common multiple of A be a positive integer X that satisfies the following condition for every k (1 \leq k \leq N):

There exists a non-negative integer p such that X= a_k \times (p+0.5).

Find the number of semi-common multiples of A among the integers between 1 and M (inclusive).

Constraints

1 \leq N \leq 10^5

1 \leq M \leq 10^9

2 \leq a_i \leq 10^9

a_i is an even number.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
a_1 a_2 ... a_N

Output

Print the number of semi-common multiples of A among the integers between 1 and M (inclusive).

Sample Input 1

2 50
6 10

Sample Output 1

2

15 = 6 \times 2.5

15 = 10 \times 1.5

45 = 6 \times 7.5

45 = 10 \times 4.5

Thus, 15 and 45 are semi-common multiples of A. There are no other semi-common multiples of A between 1 and 50, so the answer is 2.

Sample Input 2

3 100
14 22 40

Sample Output 2

0

The answer can be 0.

Sample Input 3

5 1000000000
6 6 2 6 2

Sample Output 3

166666667


program main
	implicit none
    character(100)::s,t
    integer::lens,lent
    read(*,*) s,t
    lens=len_trim(s)
    lent=len_trim(t)
    write(*,*) t(1:lent)//s(1:lens)
    stop
end program main

Score : 100 points

Problem Statement

Given are two strings S and T consisting of lowercase English letters. Concatenate T and S in this order, without space in between, and print the resulting string.

Constraints

S and T are strings consisting of lowercase English letters.

The lengths of S and T are between 1 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

S T

Output

Print the resulting string.

Sample Input 1

oder atc

Sample Output 1

atcoder

When S = oder and T = atc, concatenating T and S in this order results in atcoder.

Sample Input 2

humu humu

Sample Output 2

humuhumu


program example
	implicit none

	integer(8) A,B,K

    read(*,*) A,B,K

    write(*,*) A,B

 	if(A+B>=K) then
    	if(A>=K) then
        	A=A-K
        else
        	B=A+B-K
        	A=0
        end if
    else
    	A=0
        B=0
    end if

    write(*,*) A,B

end program example

Score : 200 points

Problem Statement

Takahashi has A cookies, and Aoki has B cookies.
Takahashi will do the following action K times:

If Takahashi has one or more cookies, eat one of his cookies.

Otherwise, if Aoki has one or more cookies, eat one of Aoki's cookies.

If they both have no cookies, do nothing.

In the end, how many cookies will Takahashi and Aoki have, respectively?

Constraints

0 \leq A \leq 10^{12}

0 \leq B \leq 10^{12}

0 \leq K \leq 10^{12}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B K

Output

Print the numbers of Takahashi's and Aoki's cookies after K actions.

Sample Input 1

2 3 3

Sample Output 1

0 2

Takahashi will do the following:

He has two cookies, so he eats one of them.

Now he has one cookie left, and he eats it.

Now he has no cookies left, but Aoki has three, so Takahashi eats one of them.

Thus, in the end, Takahashi will have 0 cookies, and Aoki will have 2.

Sample Input 2

500000000000 500000000000 1000000000000

Sample Output 2

0 0

Watch out for overflows.


program abc149_b
	implicit none
	integer(8) :: A, B, K
    read(*, *) A, B, K

    if( K >= A+B ) then
    	print *, 0, 0
    else if( A <= K .and. K < A+B ) then
    	print *, 0, B-K+A
    else
    	print *, A-K, B
    endif
end program abc149_b


program main
      implicit none
      integer :: x
      integer :: i, j
      logical :: flag

      read *, x
      do i = x, 100003
        flag = .true.
        do j = 2, int(sqrt(real(x))+1)
          if ( mod(i,j) == 0 ) then
            flag = .false.
            exit
          end if
        end do
        if ( flag ) then
          print *, i
          stop
        end if
      end do

end program main

Score: 300 points

Problem Statement

Find the minimum prime number greater than or equal to X.

Notes

A prime number is an integer greater than 1 that cannot be evenly divided by any positive integer except 1 and itself.

For example, 2, 3, and 5 are prime numbers, while 4 and 6 are not.

Constraints

2 \le X \le 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X

Output

Print the minimum prime number greater than or equal to X.

Sample Input 1

20

Sample Output 1

23

The minimum prime number greater than or equal to 20 is 23.

Sample Input 2

2

Sample Output 2

2

X itself can be a prime number.

Sample Input 3

99992

Sample Output 3

100003


program example
	implicit none

    integer(8) :: X,i

    read(*,*) X

    do i=X,10**5

    	if(i==2 .or. i==3) then
    		write(*,*) i
            exit
        else
			if(mod(i,2)/=0 .and. mod(i,3)/=0) then
        		write(*,*) i
                exit
        	end if
        end if

    end do

end program example


module mod_ntt
  implicit none
  integer, parameter :: intkind = 8
  integer(intkind), parameter :: modu = 998244353
contains
  function convolve(a, na, b, nb) result(res)
    integer, intent(in) :: na, nb
    integer(intkind), intent(in) :: a(0:na), b(0:nb)
    integer(intkind), allocatable :: fa(:), fb(:), f(:)
    integer(intkind) :: res(0:na + nb)
    integer :: p
    p = find_pow2(na + nb + 1)
    allocate(fa(0:p - 1), fb(0:p - 1), f(0:p - 1))
    call init(a, na, fa, p)
    call ntt(fa, p)
    call init(b, nb, fb, p)
    call ntt(fb, p)
    f = mod(fa * fb, modu)
    call ntt(f, p, .true.)
    res = f(0:na + nb)
  end
  integer function find_pow2(n) result(res)
    integer, intent(in) :: n
    res = n - 1
    res = or(res, rshift(res, 1))
    res = or(res, rshift(res, 2))
    res = or(res, rshift(res, 4))
    res = or(res, rshift(res, 8))
    res = or(res, rshift(res, 16))
    res = res + 1
  end
  subroutine init(a, n, f, p)
    integer, intent(in) :: n, p
    integer(intkind), intent(in) :: a(0:n)
    integer(intkind), intent(out) :: f(0:p - 1)
    f(0:n) = a(0:n)
    f(n + 1:p - 1) = 0
  end
  subroutine ntt(f, p, invert)
    integer, intent(in) :: p
    integer(intkind), intent(inout) :: f(0:p - 1)
    logical, optional, intent(in) :: invert
    integer(intkind) :: w, z, pz, x, y
    integer :: i, j, r, m
    logical :: inverse
    inverse = merge(invert, .false., present(invert))
    w = pow(3_intkind, (modu - 1) / p)
    if (inverse) w = inv(w)
    r = 0
    do i = 1, p - 2
      j = p / 2
      do
        r = xor(r, j)
        if (j <= r) exit
        j = j / 2
      end do
      if (i < r) call swap(f(i), f(r))
    end do
    m = 1
    do while (m < p)
      pz = 1
      z = pow(w, int(p / (2 * m), intkind))
      do i = 0, m - 1
        do j = i, p - 1, 2 * m
          x = f(j)
          y = mod(f(j + m) * pz, modu)
          f(j) = mod(x + y, modu)
          f(j + m) = modulo(x - y, modu)
        end do
        pz = mod(pz * z, modu)
      end do
      m = m * 2
    end do
    if (inverse) f = mod(f * inv(int(p, intkind)), modu)
  end
  integer(intkind) function pow(a, b) result(res)
    integer(intkind), intent(in) :: a, b
    integer(intkind) :: r, p
    res = 1
    r = b
    p = a
    do while (r > 0)
      if (btest(r, 0)) res = mod(res * p, modu)
      p = mod(p * p, modu)
      r = rshift(r, 1)
    end do
  end
  integer(intkind) function inv(a) result(res)
    integer(intkind), intent(in) :: a
    res = pow(a, modu - 2)
  end
  subroutine swap(a, b)
    integer(intkind), intent(inout) :: a, b
    a = xor(a, b)
    b = xor(a, b)
    a = xor(a, b)
  end
end module mod_ntt
program handshake
  use mod_ntt
  implicit none
  integer :: n, i, maxa
  integer, dimension(100000) :: a
  integer(8) :: m, cnt = 0, x = 0
  integer(intkind), dimension(0:100000) :: p = 0, q = 0
  integer(intkind), dimension(0:200000) :: c = 0
  read(*,*) n, m
  read(*,*) a(1:n)
  maxa = maxval(a(1:n))
  do i = 1, n
    p(a(i)) = p(a(i)) + 1
  end do
  q = p
  c(0:2 * maxa) = convolve(p, maxa, q, maxa)
  do i = 2 * maxa, 0, -1
    if (c(i) + cnt >= m) then
      x = x + (m - cnt) * i
      exit
    else
      x = x + c(i) * i
      cnt = cnt + c(i)
    end if
  end do
  write(*,'(i0)') x
end program handshake

Score : 500 points

Problem Statement

Takahashi has come to a party as a special guest.
There are N ordinary guests at the party. The i-th ordinary guest has a power of A_i.

Takahashi has decided to perform M handshakes to increase the happiness of the party (let the current happiness be 0).
A handshake will be performed as follows:

Takahashi chooses one (ordinary) guest x for his left hand and another guest y for his right hand (x and y can be the same).

Then, he shakes the left hand of Guest x and the right hand of Guest y simultaneously to increase the happiness by A_x+A_y.

However, Takahashi should not perform the same handshake more than once. Formally, the following condition must hold:

Assume that, in the k-th handshake, Takahashi shakes the left hand of Guest x_k and the right hand of Guest y_k. Then, there is no pair p, q (1 \leq p < q \leq M) such that (x_p,y_p)=(x_q,y_q).

What is the maximum possible happiness after M handshakes?

Constraints

1 \leq N \leq 10^5

1 \leq M \leq N^2

1 \leq A_i \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 ... A_N

Output

Print the maximum possible happiness after M handshakes.

Sample Input 1

5 3
10 14 19 34 33

Sample Output 1

202

Let us say that Takahashi performs the following handshakes:

In the first handshake, Takahashi shakes the left hand of Guest 4 and the right hand of Guest 4.

In the second handshake, Takahashi shakes the left hand of Guest 4 and the right hand of Guest 5.

In the third handshake, Takahashi shakes the left hand of Guest 5 and the right hand of Guest 4.

Then, we will have the happiness of (34+34)+(34+33)+(33+34)=202.

We cannot achieve the happiness of 203 or greater, so the answer is 202.

Sample Input 2

9 14
1 3 5 110 24 21 34 5 3

Sample Output 2

1837

Sample Input 3

9 73
67597 52981 5828 66249 75177 64141 40773 79105 16076

Sample Output 3

8128170


program eee
implicit none

integer(8) :: n, m, i, j, k, res
integer(8),allocatable,dimension(:) :: a, b

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

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

call heapsort(n**2,b)

res = 0
do i = 1, m
res = res + b(n**2-i+1)
end do

print*, res

end program

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

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

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

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

  return
end subroutine heapsort


program ABC149E
    implicit none
    integer(16)::N,M
    integer(16),allocatable,dimension(:)::A,R
    integer(16)::ans,OK,NG,MD
    integer(16)::i,j
    read*,N,M
    allocate(A(0:N),R(0:N))
    read*,A(1:N);call heapsort(N,A(1:N))
    OK=0;NG=A(N)*2+1
    MD=(OK+NG)/2
    do while(NG-OK>1)
        if(CNT(MD)>=M)then
            OK=MD
        else
            NG=MD
        endif
        MD=(OK+NG)/2
    end do

    ans=0
    do i=N,1,-1
        do j=N,1,-1
            if(A(i)+A(j)>=MD)then
                ans=ans+A(i)+A(j)
            else
                exit
            endif
        end do
    end do
    print"(i0)",ans-MD*(cnt(MD)-M)
contains
integer(16) function cnt(MID)
    integer(16),intent(in)::MID
    integer(16)::i,j
    R=0;j=N
    do i=1,N
        do while(A(i)+A(j)>=MID.and.j>0)
            j=j-1
            R(i)=R(i)+1
        end do
    end do
    do i=1,N
        R(i)=R(i-1)+R(i)
    end do
    cnt=0
    do i=1,N
        cnt=cnt+R(i)
    end do
end function
subroutine heapsort(n,array)
  implicit none
!ここの入力は状況に応じて変更すること
  integer(16),intent(in) :: n
  integer(16),intent(inout) :: array(1:n)
  integer(16)::i,k,j,l
  integer(16):: 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 ABC149E


program prob3
    implicit none
    integer(16)::n, a, b
    read(*,*) n, a, b
    if(mod(abs(a-b),2) == 0) then
        write(*,*) abs(a-b)/2
    else
        if(a-1 <= n-b) then
            b = b - a - 1
            write(*,*) b/2 + a
        else
            a = a + n - b - 1
            write(*,*) (b-a)/2 + (n-b)
        end if
    end if
    stop
end program

Score : 300 points

Problem Statement

2N players are running a competitive table tennis training on N tables numbered from 1 to N.

The training consists of rounds.
In each round, the players form N pairs, one pair per table.
In each pair, competitors play a match against each other.
As a result, one of them wins and the other one loses.

The winner of the match on table X plays on table X-1 in the next round,
except for the winner of the match on table 1 who stays at table 1.

Similarly, the loser of the match on table X plays on table X+1 in the next round,
except for the loser of the match on table N who stays at table N.

Two friends are playing their first round matches on distinct tables A and B.
Let's assume that the friends are strong enough to win or lose any match at will.
What is the smallest number of rounds after which the friends can get to play a match against each other?

Constraints

2 \leq N \leq 10^{18}

1 \leq A < B \leq N

All input values are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print the smallest number of rounds after which the friends can get to play a match against each other.

Sample Input 1

5 2 4

Sample Output 1

1

If the first friend loses their match and the second friend wins their match, they will both move to table 3 and play each other in the next round.

Sample Input 2

5 2 3

Sample Output 2

2

If both friends win two matches in a row, they will both move to table 1.


program table_tennis_training
  implicit none
  integer(8) :: n, a, b
  read(*,*) n, a, b
  write(*,'(i0)') min(a, n - b + 1) * mod(b - a, 2) + (b - a) / 2
end program table_tennis_training


program example
	implicit none

    integer(16) A,B,N

    read(*,*) N,A,B

    if(mod(B-A,2)==0) then

    	write(*,*)  (B-A)/2

    else
 		write(*,*) min(N-B,A-1)+1+((B-A-1)/2)
    end if

end program example


      PROGRAM tableTennisTraining
      IMPLICIT NONE
      integer(16) :: n,a,b
      integer(16) :: sub,ans,ans1,ansn

      read*,n,a,b

      sub = abs( a-b )
      if( mod(sub,2)==0 )then
        ans = sub/2
      else
        ans1 = max(a-1,b-1)
        ansn = max(n-a,n-b)
        ans = min(ans1,ansn)
      endif

      print*,ans

      END PROGRAM


program main
  integer :: n, i, j, d
  read(*,*) n
  if(mod(n,2) == 1) then
    write(*,*) -1
    stop
  endif

  do j = 1, n
    do i = 1, n
      if(abs(i-j)<=1) then
        if(i==j) then
          if (mod(i,2) ==1) then
            write(6,'(a)',advance="no") 'a'
          else
            write(6,'(a)',advance="no") 'b'
          end if
        else if(i==j-1 .and. mod(i,2)==1) then
          write(6,'(a)',advance="no") 'a'
        else if(mod(i,2) == 0 .and. i==j+1) then
          write(6,'(a)',advance="no") 'b'
        else
          write(6,'(a)', advance="no") '.'
        end if
      else
        write(6,'(a)',advance="no") '.'
      end if
    end do
    write(6,*)
  end do
end program main

Score : 900 points

Problem Statement

Let us consider a grid of squares with N rows and N columns. You want to put some domino pieces on this grid.
Each domino piece covers two squares that have a common side. Each square can be covered by at most one piece.

For each row of the grid, let's define its quality as the number of domino pieces that cover at least one square in this row.
We define the quality of each column similarly.

Find a way to put at least one domino piece on the grid so that the quality of every row is equal to the quality of every column,
or determine that such a placement doesn't exist.

Constraints

2 \le N \le 1000

Input

Input is given from Standard Input in the following format:

N

Output

If the required domino placement doesn't exist, print a single integer -1.

Otherwise, output your placement as N strings of N characters each.
If a square is not covered, the corresponding character must be . (a dot).
Otherwise, it must contain a lowercase English letter.
Squares covered by the same domino piece must contain the same letter.
If two squares have a common side but belong to different pieces, they must contain different letters.

Sample Input 1

6

Sample Output 1

aabb..
b..zz.
ba....
.a..aa
..a..b
..a..b

The quality of every row and every column is 2.

Sample Input 2

2

Sample Output 2

-1


program strings_with_the_same_length
  implicit none
  integer :: n, i
  character(100) :: s, t
  read(*,*) n
  read(*,*) s, t
  do i = 1, n
    write(*,'(a)',advance='no') s(i:i)//t(i:i)
  end do
  write(*,*)
end program strings_with_the_same_length

Score : 200 points

Problem Statement

Given are strings s and t of length N each, both consisting of lowercase English letters.

Let us form a new string by alternating the characters of S and the characters of T, as follows: the first character of S, the first character of T, the second character of S, the second character of T, ..., the N-th character of S, the N-th character of T. Print this new string.

Constraints

1 \leq N \leq 100

|S| = |T| = N

S and T are strings consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
S T

Output

Print the string formed.

Sample Input 1

2
ip cc

Sample Output 1

icpc

Sample Input 2

8
hmhmnknk uuuuuuuu

Sample Output 2

humuhumunukunuku

Sample Input 3

5
aaaaa aaaaa

Sample Output 3

aaaaaaaaaa


   program abc148c
      implicit none
      integer(8)::a,b

         read *, a,b
         print *, lcm(a,b)
         stop

   contains

      function gcd(a,b)
         implicit none
         integer(8),intent(in)::a,b
         integer(8)::gcd
         integer(8)::l,m,n

            l=a
            m=b
            n=mod(l,m)
            do while (n/=0)
               l=m
               m=n
               n=mod(l,m)
            end do
            gcd=m
            return
      end function gcd

      function lcm(a,b)
         implicit none
         integer(8),intent(in)::a,b
         integer(8)::lcm

            lcm=a*b/gcd(a,b)          ! gcd(a,b)*(a/gcd(a,b))*(b/gcd(a,b))
            return
      end function lcm

   end program abc148c

Score : 300 points

Problem Statement

Takahashi is organizing a party.

At the party, each guest will receive one or more snack pieces.

Takahashi predicts that the number of guests at this party will be A or B.

Find the minimum number of pieces that can be evenly distributed to the guests in both of the cases predicted.

We assume that a piece cannot be divided and distributed to multiple guests.

Constraints

1 \leq A, B \leq 10^5

A \neq B

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the minimum number of pieces that can be evenly distributed to the guests in both of the cases with A guests and B guests.

Sample Input 1

2 3

Sample Output 1

6

When we have six snack pieces, each guest can take three pieces if we have two guests, and each guest can take two if we have three guests.

Sample Input 2

123 456

Sample Output 2

18696

Sample Input 3

100000 99999

Sample Output 3

9999900000


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

  write (*, "(i0)") a * b / gcd(a, b)
  contains

  function gcd(x, y) result(a)
    integer(8), intent(in) :: x, y
    integer(8) :: a, b, r
    a = max(x, y)
    b = min(x, y)
    do while (b > 0)
      r = mod(a, b)
      a = b
      b = r
    end do
  end function gcd
end program main


program main

  implicit none
  integer(8) :: a, b !911
  integer(8) :: nl, ns, tmp

  read(*,*) a, b

  if ( a >= b ) then
    nl = a
    ns = b
  else
    nl = b
    ns = a
  end if

  do
    tmp = mod( nl, ns )
    nl = ns
    ns = tmp
    if ( ns == 0 ) exit
  end do

  print*, a*b/nl

end program main


program main
  integer(8) :: n, i, num
  integer(8), allocatable :: a(:)
  read(*,*) n
  allocate(a(n))
  read(*,*) a

  num = 1
  do i = 1, n
    if(a(i)==num) then
      num = num + 1
    end if
  end do
  num = num - 1
  if (num == 0) then
    write(6,*) -1
  else
    write(6,*) n-num
  end if
end program main

Score : 400 points

Problem Statement

We have N bricks arranged in a row from left to right.

The i-th brick from the left (1 \leq i \leq N) has an integer a_i written on it.

Among them, you can break at most N-1 bricks of your choice.

Let us say there are K bricks remaining. Snuke will be satisfied if, for each integer i (1 \leq i \leq K), the i-th of those brick from the left has the integer i written on it.

Find the minimum number of bricks you need to break to satisfy Snuke's desire. If his desire is unsatisfiable, print -1 instead.

Constraints

All values in input are integers.

1 \leq N \leq 200000

1 \leq a_i \leq N

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the minimum number of bricks that need to be broken to satisfy Snuke's desire, or print -1 if his desire is unsatisfiable.

Sample Input 1

3
2 1 2

Sample Output 1

1

If we break the leftmost brick, the remaining bricks have integers 1 and 2 written on them from left to right, in which case Snuke will be satisfied.

Sample Input 2

3
2 2 2

Sample Output 2

-1

In this case, there is no way to break some of the bricks to satisfy Snuke's desire.

Sample Input 3

10
3 1 4 1 5 9 2 6 5 3

Sample Output 3

7

Sample Input 4

1
1

Sample Output 4

0

There may be no need to break the bricks at all.


program brick_break
    implicit none
    integer(4):: n, i, ni
    integer(4), allocatable:: a(:)

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

    ni = 0
    i = 1
    do i=1,n
        if (a(i) == ni+1) ni = ni + 1
    end do

    if (ni == 0) then
        print*, -1
    else
        print*, n-ni
    endif
end program brick_break


program abc148e
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: n
    integer(int64):: div,ans

    read*, n
    if (n < 10) then
        print'(i0)', 0
        stop
    end if

    if (mod(n,2_8) == 1) then
        print'(i0)', 0
        stop
    end if
    div=10
    ans=0
    do while (div <= n)
        ans = ans + n/div
        div=div*5
    end do
    print'(i0)', ans
end program abc148e

Score : 500 points

Problem Statement

For an integer n not less than 0, let us define f(n) as follows:

f(n) = 1 (if n < 2)

f(n) = n f(n-2) (if n \geq 2)

Given is an integer N. Find the number of trailing zeros in the decimal notation of f(N).

Constraints

0 \leq N \leq 10^{18}

Input

Input is given from Standard Input in the following format:

N

Output

Print the number of trailing zeros in the decimal notation of f(N).

Sample Input 1

12

Sample Output 1

1

f(12) = 12 × 10 × 8 × 6 × 4 × 2 = 46080, which has one trailing zero.

Sample Input 2

5

Sample Output 2

0

f(5) = 5 × 3 × 1 = 15, which has no trailing zeros.

Sample Input 3

1000000000000000000

Sample Output 3

124999999999999995


program main
	implicit none
    integer::a,b,c
    read(*,*)a,b,c
    if(a+b+c<=21)then
    	write(*,*)'win'
    else
    	write(*,*)'bust'
    end if
    stop
end program main

Score : 100 points

Problem Statement

Given are three integers A_1, A_2, and A_3.

If A_1+A_2+A_3 is greater than or equal to 22, print bust; otherwise, print win.

Constraints

1 \leq A_i \leq 13 \ \ (i=1,2,3)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A_1 A_2 A_3

Output

If A_1+A_2+A_3 is greater than or equal to 22, print bust; otherwise, print win.

Sample Input 1

5 7 9

Sample Output 1

win

5+7+9=21, so print win.

Sample Input 2

13 7 2

Sample Output 2

bust

13+7+2=22, so print bust.


program palindrome_philia
  implicit none
  character(100) :: s
  integer :: n, i, m = 0
  read(*,*) s
  n = len_trim(s)
  do i = 1, n/2
    if (s(i:i) /= s(n-i+1:n-i+1)) m = m+1
  end do
  write(*,'(i0)') m
end program palindrome_philia

Score : 200 points

Problem Statement

Takahashi loves palindromes. Non-palindromic strings are unacceptable to him. Each time he hugs a string, he can change one of its characters to any character of his choice.

Given is a string S. Find the minimum number of hugs needed to make S palindromic.

Constraints

S is a string consisting of lowercase English letters.

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

Input

Input is given from Standard Input in the following format:

S

Output

Print the minimum number of hugs needed to make S palindromic.

Sample Input 1

redcoder

Sample Output 1

1

For example, we can change the fourth character to o and get a palindrome redooder.

Sample Input 2

vvvvvv

Sample Output 2

0

We might need no hugs at all.

Sample Input 3

abcdabc

Sample Output 3

2


module mod_bitset
  implicit none
  integer, private, parameter :: address_bits_per_word = 6
  integer, private, parameter :: bits_per_word = lshift(1, address_bits_per_word)
  integer, private, parameter :: bit_index_mask = bits_per_word - 1
  integer(8), private, parameter :: word_mask = -1
  type t_bitset
    integer(8), private, allocatable :: words(:) ! 0-indexed, [start, end)
    integer, private :: words_in_use = 0
  contains
    procedure, private :: flip0
    procedure, private :: flip1
    generic :: flip => flip0, flip1
    procedure, private :: set0
    procedure, private :: set1
    procedure, private :: setval0
    procedure, private :: setval1
    generic :: set => set0, set1, setval0, setval1
    procedure, private :: clear0
    procedure, private :: clear1
    procedure, private :: clear2
    generic :: clear => clear0, clear1, clear2
    procedure, private :: get0
    procedure, private :: get1
    generic :: get => get0, get1
    procedure :: next_set_bit => next_set_bit
    procedure :: next_clear_bit => next_clear_bit
    procedure :: previous_set_bit => previous_set_bit
    procedure :: previous_clear_bit => previous_clear_bit
    procedure :: length => length
    procedure :: is_empty => is_empty
    procedure :: is_not_empty => is_not_empty
    procedure :: intersects => intersects
    procedure :: cardinality => cardinality
    procedure :: and => and0
    procedure :: or => or0
    procedure :: xor => xor0
    procedure :: and_not => and_not0
    procedure :: size => size0
  end type
  interface bitset
    module procedure :: newb0, newb3
  end interface bitset
  interface slshift
    module procedure :: slshift0, slshift1
  end interface slshift
  interface urshift
    module procedure :: urshift0, urshift1
  end interface urshift
contains
  integer function slshift0(i, n) result(res)
    integer, intent(in) :: i
    integer, intent(in) :: n
    integer :: m
    m = and(n, 31)
    res = lshift(i, m)
  end
  integer(8) function slshift1(i, n) result(res)
    integer(8), intent(in) :: i
    integer, intent(in) :: n
    integer :: m
    m = and(n, 63)
    res = lshift(i, m)
  end
  integer function urshift0(i, n) result(res)
    integer, intent(in) :: i
    integer, intent(in) :: n
    integer :: m
    m = and(n, 31)
    if (m == 0) then
      res = i
    else
      res = rshift(ibclr(rshift(i, 1), 31), m - 1)
    end if
  end
  integer(8) function urshift1(i, n) result(res)
    integer(8), intent(in) :: i
    integer, intent(in) :: n
    integer :: m
    m = and(n, 63)
    if (m == 0) then
      res = i
    else
      res = rshift(ibclr(rshift(i, 1), 63), m - 1)
    end if
  end
  integer function number_of_leading_zeros(i) result(res)
    integer(8), intent(in) :: i
    integer :: x
    if (i == 0) then
      res = 64
      return
    end if
    res = 1
    x = int(urshift(i, 32))
    if (x == 0) then
      res = res + 32
      x = int(i)
    end if
    if (urshift(x, 16) == 0) then
      res = res + 16
      x = lshift(x, 16)
    end if
    if (urshift(x, 24) == 0) then
      res = res + 8
      x = lshift(x, 8)
    end if
    if (urshift(x, 28) == 0) then
      res = res + 4
      x = lshift(x, 4)
    end if
    if (urshift(x, 30) == 0) then
      res = res + 2
      x = lshift(x, 2)
    end if
    res = res - urshift(x, 31)
  end
  integer function number_of_trailing_zeros(i) result(res)
    integer(8), intent(in) :: i
    integer :: x, y
    if (i == 0) then
      res = 64
      return
    end if
    res = 63
    y = int(i)
    if (y /= 0) then
      res = res - 32
      x = y
    else
      x = int(urshift(i, 32))
    end if
    y = lshift(x, 16)
    if (y /= 0) then
      res = res - 16
      x = y
    end if
    if (y /= 0) then
      res = res - 8
      x = y
    end if
    if (y /= 0) then
      res = res - 4
      x = y
    end if
    if (y /= 0) then
      res = res - 2
      x = y
    end if
    res = res - urshift(lshift(x, 1), 31)
  end
  integer function words_index(bit_index) result(res)
    integer, intent(in) :: bit_index
    res = rshift(bit_index, address_bits_per_word)
  end
  subroutine assert(flag, message)
    logical, intent(in) :: flag
    character(*), intent(in), optional :: message
    if (flag) return
    write(*,*) "*** Error occurred ***"
    if (present(message)) write(*,'(a)') message
    stop
  end
  subroutine check_invariants(this)
    type(t_bitset), intent(in) :: this
    integer :: u, n
    u = this%words_in_use
    n = size(this%words)
    if (u /= 0) call assert(this%words(u - 1) /= 0, "At check_invariants() (1)")
    call assert(u >= 0 .and. u <= n, "At check_invariants() (2)")
    if (u /= n) call assert(this%words(u) == 0, "At check_invariants() (3)")
  end
  subroutine recalculate_words_in_use(this)
    type(t_bitset), intent(inout) :: this
    integer :: i
    do i = this%words_in_use - 1, 0, -1
      if (this%words(i) /= 0) then
        this%words_in_use = i + 1
        return
      end if
    end do
    this%words_in_use = 0
  end
  subroutine init_words(this, nbits)
    type(t_bitset), intent(inout) :: this
    integer, intent(in) :: nbits
    if (allocated(this%words)) deallocate(this%words)
    allocate(this%words(0:words_index(nbits - 1)))
    this%words = 0
  end
  type(t_bitset) function newb0() result(res)
    call init_words(res, bits_per_word)
  end
  type(t_bitset) function newb1(nbits) result(res)
    integer, intent(in) :: nbits
    call assert(nbits >= 0, "nbits < 0 at newb1()")
    call init_words(res, nbits)
  end
  type(t_bitset) function newb2(words) result(res)
    integer(8), intent(in) :: words(:)
    allocate(res%words(0:size(words) - 1))
    res%words = words
    res%words_in_use = size(words)
    call check_invariants(res)
  end
  type(t_bitset) function newb3(longs) result(res)
    integer(8), intent(in) :: longs(:)
    integer :: i
    do i = size(longs), 1, -1
      if (longs(i) /= 0) then
        res = newb2(longs(1:i))
        return
      end if
    end do
    res = newb0()
  end
  subroutine ensure_capacity(this, words_required)
    type(t_bitset), intent(inout) :: this
    integer, intent(in) :: words_required
    integer :: request
    integer(8), allocatable :: copy(:)
    if (size(this%words) >= words_required) return
    request = max(2 * size(this%words), words_required)
    allocate(copy(size(this%words)))
    copy = this%words
    deallocate(this%words)
    allocate(this%words(0:request - 1))
    this%words = 0
    this%words(0:size(copy) - 1) = copy
  end
  subroutine expand_to(this, word_index)
    type(t_bitset), intent(inout) :: this
    integer, intent(in) :: word_index
    integer :: words_required
    words_required = word_index + 1
    if (this%words_in_use < words_required) then
      call ensure_capacity(this, words_required)
      this%words_in_use = words_required
    end if
  end
  subroutine check_range(from_index, to_index)
    integer, intent(in) :: from_index, to_index
    call assert(0 <= from_index .and. from_index <= to_index, "At check_range()")
  end
  subroutine flip0(this, bit_index)
    class(t_bitset), intent(inout) :: this
    integer, intent(in) :: bit_index
    integer :: word_index
    call assert(bit_index >= 0, "bit_index < 0 at flip0()")
    word_index = words_index(bit_index)
    call expand_to(this, word_index)
    this%words(word_index) = xor(this%words(word_index), slshift(1_8, bit_index))
    call recalculate_words_in_use(this)
    call check_invariants(this)
  end
  subroutine flip1(this, from_index, to_index)
    class(t_bitset), intent(inout) :: this
    integer, intent(in) :: from_index, to_index
    integer :: start_word_index, end_word_index, idx
    integer(8) :: first_word_mask, last_word_mask
    call check_range(from_index, to_index)
    if (from_index == to_index) return
    start_word_index = words_index(from_index)
    end_word_index = words_index(to_index - 1)
    call expand_to(this, end_word_index)
    first_word_mask = slshift(word_mask, from_index)
    last_word_mask = urshift(word_mask, -to_index)
    if (start_word_index == end_word_index) then
      idx = start_word_index
      this%words(idx) = xor(this%words(idx), and(first_word_mask, last_word_mask))
    else
      idx = start_word_index
      this%words(idx) = xor(this%words(idx), first_word_mask)
      do idx = start_word_index + 1, end_word_index - 1
        this%words(idx) = xor(this%words(idx), word_mask)
      end do
      idx = end_word_index
      this%words(idx) = xor(this%words(idx), last_word_mask)
    end if
    call recalculate_words_in_use(this)
    call check_invariants(this)
  end
  subroutine set0(this, bit_index)
    class(t_bitset), intent(inout) :: this
    integer, intent(in) :: bit_index
    integer :: word_index
    call assert(bit_index >= 0, "bit_index < 0 at set0()")
    word_index = words_index(bit_index)
    call expand_to(this, word_index)
    this%words(word_index) = or(this%words(word_index), slshift(1_8, bit_index))
    call check_invariants(this)
  end
  subroutine clear0(this, bit_index)
    class(t_bitset), intent(inout) :: this
    integer, intent(in) :: bit_index
    integer :: word_index
    call assert(bit_index >= 0, "bit_index < 0 at clear0()")
    word_index = words_index(bit_index)
    if (word_index >= this%words_in_use) return
    this%words(word_index) = and(this%words(word_index), not(slshift(1_8, bit_index)))
    call recalculate_words_in_use(this)
    call check_invariants(this)
  end
  subroutine setval0(this, bit_index, val)
    class(t_bitset), intent(inout) :: this
    integer, intent(in) :: bit_index
    logical, intent(in) :: val
    if (val) then
      call set0(this, bit_index)
    else
      call clear0(this, bit_index)
    end if
  end
  subroutine set1(this, from_index, to_index)
    class(t_bitset), intent(inout) :: this
    integer, intent(in) :: from_index, to_index
    integer :: start_word_index, end_word_index, idx
    integer(8) :: first_word_mask, last_word_mask
    call check_range(from_index, to_index)
    if (from_index == to_index) return
    start_word_index = words_index(from_index)
    end_word_index = words_index(to_index - 1)
    call expand_to(this, end_word_index)
    first_word_mask = slshift(word_mask, from_index)
    last_word_mask = urshift(word_mask, -to_index)
    if (start_word_index == end_word_index) then
      idx = start_word_index
      this%words(idx) = or(this%words(idx), and(first_word_mask, last_word_mask))
    else
      idx = start_word_index
      this%words(idx) = or(this%words(idx), first_word_mask)
      do idx = start_word_index + 1, end_word_index - 1
        this%words(idx) = word_mask
      end do
      idx = end_word_index
      this%words(idx) = or(this%words(idx), last_word_mask)
    end if
    call check_invariants(this)
  end
  subroutine clear1(this, from_index, to_index_)
    class(t_bitset), intent(inout) :: this
    integer, intent(in) :: from_index, to_index_
    integer :: to_index
    integer :: start_word_index, end_word_index, idx
    integer(8) :: first_word_mask, last_word_mask
    to_index = to_index_
    call check_range(from_index, to_index)
    if (from_index == to_index) return
    start_word_index = words_index(from_index)
    if (start_word_index >= this%words_in_use) return
    end_word_index = words_index(to_index - 1)
    if (end_word_index >= this%words_in_use) then
      to_index = length(this)
      end_word_index = this%words_in_use - 1
    end if
    first_word_mask = slshift(word_mask, from_index)
    last_word_mask = urshift(word_mask, -to_index)
    if (start_word_index == end_word_index) then
      idx = start_word_index
      this%words(idx) = and(this%words(idx), not(and(first_word_mask, last_word_mask)))
    else
      idx = start_word_index
      this%words(idx) = and(this%words(idx), not(first_word_mask))
      do idx = start_word_index + 1, end_word_index - 1
        this%words(idx) = 0
      end do
      idx = end_word_index
      this%words(idx) = and(this%words(idx), not(last_word_mask))
    end if
    call recalculate_words_in_use(this)
    call check_invariants(this)
  end
  subroutine setval1(this, from_index, to_index, val)
    class(t_bitset), intent(inout) :: this
    integer, intent(in) :: from_index, to_index
    logical, intent(in) :: val
    if (val) then
      call set1(this, from_index, to_index)
    else
      call clear1(this, from_index, to_index)
    end if
  end
  subroutine clear2(this)
    class(t_bitset), intent(out) :: this
    this%words(0:this%words_in_use - 1) = 0
    this%words_in_use = 0
  end
  logical function get0(this, bit_index) result(res)
    class(t_bitset), intent(in) :: this
    integer, intent(in) :: bit_index
    integer :: word_index
    call assert(bit_index >= 0, "bit_index < 0 at get0()")
    call check_invariants(this)
    word_index = words_index(bit_index)
    if (word_index >= this%words_in_use) then
      res = .false.
      return
    end if
    res = and(this%words(word_index), slshift(1_8, bit_index)) /= 0
  end
  type(t_bitset) function get1(this, from_index, to_index_) result(res)
    class(t_bitset), intent(in) :: this
    integer, intent(in) :: from_index, to_index_
    integer :: to_index, len
    integer :: target_words, source_index, idx
    logical :: word_aligned
    integer(8) :: last_word_mask
    to_index = to_index_
    call check_range(from_index, to_index)
    call check_invariants(this)
    len = length(this)
    if (len <= from_index .or. from_index == to_index) then
      res = newb0()
      return
    end if
    if (to_index > len) to_index = len
    res = newb1(to_index - from_index)
    target_words = words_index(to_index - from_index - 1) + 1
    source_index = words_index(from_index)
    word_aligned = and(from_index, bit_index_mask) == 0
    do idx = 0, target_words - 2
      if (word_aligned) then
        res%words(idx) = this%words(source_index)
      else
        res%words(idx) = or(urshift(this%words(source_index), from_index), slshift(this%words(source_index + 1), -from_index))
      end if
      source_index = source_index + 1
    end do
    last_word_mask = urshift(word_mask, -to_index)
    if (and(to_index - 1, bit_index_mask) < and(from_index, bit_index_mask)) then
      res%words(target_words - 1) = or(urshift(this%words(source_index), from_index), &
        slshift(and(this%words(source_index + 1), last_word_mask), -from_index))
    else
      res%words(target_words - 1) = urshift(and(this%words(source_index), last_word_mask), from_index)
    end if
    res%words_in_use = target_words
    call recalculate_words_in_use(res)
    call check_invariants(res)
  end
  integer function next_set_bit(this, from_index) result(res)
    class(t_bitset), intent(in) :: this
    integer, intent(in) :: from_index
    integer :: u
    integer(8) :: word
    call assert(from_index >= 0, "from_index < 0 at next_set_bit()")
    call check_invariants(this)
    u = words_index(from_index)
    if (u >= this%words_in_use) then
      res = -1
      return
    end if
    word = and(this%words(u), slshift(word_mask, from_index))
    do
      if (word /= 0) then
        res = (u * bits_per_word) + number_of_trailing_zeros(word)
        return
      end if
      u = u + 1
      if (u == this%words_in_use) then
        res = -1
        return
      end if
      word = this%words(u)
    end do
  end
  integer function next_clear_bit(this, from_index) result(res)
    class(t_bitset), intent(in) :: this
    integer, intent(in) :: from_index
    integer :: u
    integer(8) :: word
    call assert(from_index >= 0, "from_index < 0 at next_clear_bit()")
    call check_invariants(this)
    u = words_index(from_index)
    if (u >= this%words_in_use) then
      res = from_index
      return
    end if
    word = and(not(this%words(u)), slshift(word_mask, from_index))
    do
      if (word /= 0) then
        res = (u * bits_per_word) + number_of_trailing_zeros(word)
        return
      end if
      u = u + 1
      if (u == this%words_in_use) then
        res = this%words_in_use * bits_per_word
        return
      end if
      word = not(this%words(u))
    end do
  end
  integer function previous_set_bit(this, from_index) result(res)
    class(t_bitset), intent(in) :: this
    integer, intent(in) :: from_index
    integer :: u
    integer(8) :: word
    if (from_index < 0) then
      call assert(from_index == -1, "from_index < -1 at previous_set_bit()")
      res = -1
      return
    end if
    call check_invariants(this)
    u = words_index(from_index)
    if (u >= this%words_in_use) then
      res = length(this) - 1
      return
    end if
    word = and(this%words(u), urshift(word_mask, -(from_index + 1)))
    do
      if (word /= 0) then
        res = (u + 1) * bits_per_word - 1 - number_of_leading_zeros(word)
        return
      end if
      if (u == 0) then
        res = -1
        return
      end if
      u = u - 1
      word = this%words(u)
    end do
  end
  integer function previous_clear_bit(this, from_index) result(res)
    class(t_bitset), intent(in) :: this
    integer, intent(in) :: from_index
    integer :: u
    integer(8) :: word
    if (from_index < 0) then
      call assert(from_index == -1, "from_index < -1 at previous_clear_bit()")
      res = -1
      return
    end if
    call check_invariants(this)
    u = words_index(from_index)
    if (u >= this%words_in_use) then
      res = from_index
      return
    end if
    word = and(not(this%words(u)), urshift(word_mask, -(from_index + 1)))
    do
      if (word /= 0) then
        res = (u + 1) * bits_per_word - 1 - number_of_leading_zeros(word)
        return
      end if
      if (u == 0) then
        res = -1
        return
      end if
      u = u - 1
      word = not(this%words(u))
    end do
  end
  integer function length(this) result(res)
    class(t_bitset), intent(in) :: this
    integer :: u, n
    u = this%words_in_use
    if (u == 0) then
      res = 0
    else
      n = number_of_leading_zeros(this%words(u - 1))
      res = bits_per_word * (u - 1) + (bits_per_word - n)
    end if
  end
  logical function is_empty(this) result(res)
    class(t_bitset), intent(in) :: this
    res = this%words_in_use == 0
  end
  logical function is_not_empty(this) result(res)
    class(t_bitset), intent(in) :: this
    res = this%words_in_use > 0
  end
  logical function intersects(this, other) result(res)
    class(t_bitset), intent(in) :: this
    type(t_bitset), intent(in) :: other
    integer :: m, i
    m = min(this%words_in_use, other%words_in_use)
    do i = m, 0, -1
      if (and(this%words(i), other%words(i)) /= 0) then
        res = .true.
      end if
    end do
    res = .false.
  end
  integer function cardinality(this) result(res)
    class(t_bitset), intent(in) :: this
    res = sum(popcnt(this%words(0:this%words_in_use - 1)))
  end
  subroutine and0(this, other)
    class(t_bitset), intent(inout) :: this
    type(t_bitset), intent(in) :: other
    integer :: i
    do while (this%words_in_use > other%words_in_use)
      this%words_in_use = this%words_in_use - 1
      this%words(this%words_in_use) = 0
    end do
    do i = 0, this%words_in_use - 1
      this%words(i) = and(this%words(i), other%words(i))
    end do
    call recalculate_words_in_use(this)
    call check_invariants(this)
  end
  subroutine or0(this, other)
    class(t_bitset), intent(inout) :: this
    type(t_bitset), intent(in) :: other
    integer :: u, i
    u = min(this%words_in_use, other%words_in_use)
    do i = 0, u - 1
      this%words(i) = or(this%words(i), other%words(i))
    end do
    if (u < other%words_in_use) then
      call ensure_capacity(this, other%words_in_use)
      this%words_in_use = other%words_in_use
      this%words(u:this%words_in_use - 1) = other%words(u:other%words_in_use - 1)
    end if
    call check_invariants(this)
  end
  subroutine xor0(this, other)
    class(t_bitset), intent(inout) :: this
    type(t_bitset), intent(in) :: other
    integer :: u, i
    u = min(this%words_in_use, other%words_in_use)
    do i = 0, u - 1
      this%words(i) = xor(this%words(i), other%words(i))
    end do
    if (u < other%words_in_use) then
      call ensure_capacity(this, other%words_in_use)
      this%words_in_use = other%words_in_use
      this%words(u:this%words_in_use - 1) = other%words(u:other%words_in_use - 1)
    end if
    call recalculate_words_in_use(this)
    call check_invariants(this)
  end
  subroutine and_not0(this, other)
    class(t_bitset), intent(inout) :: this
    type(t_bitset), intent(in) :: other
    integer :: u, i
    u = min(this%words_in_use, other%words_in_use)
    do i = 0, u - 1
      this%words(i) = and(this%words(i), not(other%words(i)))
    end do
    call recalculate_words_in_use(this)
    call check_invariants(this)
  end
  integer function size0(this) result(res)
    class(t_bitset), intent(in) :: this
    res = size(this%words) * bits_per_word
  end
end module mod_bitset
program balanced_path
  use mod_bitset
  implicit none
  integer :: h, w, a(81, 81) = 0, b(80) = 0, i, j, k
  type(t_bitset) :: dp(81, 81)
  read(*,*) h, w
  do i = 1, h
    read(*,*) a(i, 1:w)
  end do
  do i = 1, h
    read(*,*) b(1:w)
    a(i, 1:w) = abs(a(i, 1:w)-b(1:w))
  end do
  do i = 1, h + 1
    do j = 1, w + 1
      dp(i, j) = bitset()
    end do
  end do
  call dp(1, 1)%set(a(1, 1))
  do i = 1, h
    do j = 1, w
      do k = 0, 12800
        if (.not.dp(i, j)%get(k)) cycle
        call dp(i, j + 1)%set(abs(k - a(i, j + 1)))
        call dp(i, j + 1)%set(k + a(i, j + 1))
        call dp(i + 1, j)%set(abs(k - a(i + 1, j)))
        call dp(i + 1, j)%set(k + a(i + 1, j))
      end do
    end do
  end do
  do k = 0, 12800
    if (dp(h, w)%get(k)) then
      write(*,'(i0)') k
      stop
    end if
  end do
end program balanced_path

Score : 500 points

Problem Statement

We have a grid with H horizontal rows and W vertical columns. Let (i,j) denote the square at the i-th row from the top and the j-th column from the left.

The square (i, j) has two numbers A_{ij} and B_{ij} written on it.

First, for each square, Takahashi paints one of the written numbers red and the other blue.

Then, he travels from the square (1, 1) to the square (H, W). In one move, he can move from a square (i, j) to the square (i+1, j) or the square (i, j+1). He must not leave the grid.

Let the unbalancedness be the absolute difference of the sum of red numbers and the sum of blue numbers written on the squares along Takahashi's path, including the squares (1, 1) and (H, W).

Takahashi wants to make the unbalancedness as small as possible by appropriately painting the grid and traveling on it.

Find the minimum unbalancedness possible.

Constraints

2 \leq H \leq 80

2 \leq W \leq 80

0 \leq A_{ij} \leq 80

0 \leq B_{ij} \leq 80

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H W
A_{11} A_{12} \ldots A_{1W}
:
A_{H1} A_{H2} \ldots A_{HW}
B_{11} B_{12} \ldots B_{1W}
:
B_{H1} B_{H2} \ldots B_{HW}

Output

Print the minimum unbalancedness possible.

Sample Input 1

2 2
1 2
3 4
3 4
2 1

Sample Output 1

0

By painting the grid and traveling on it as shown in the figure below, the sum of red numbers and the sum of blue numbers are 3+3+1=7 and 1+2+4=7, respectively, for the unbalancedness of 0.

Sample Input 2

2 3
1 10 80
80 10 1
1 2 3
4 5 6

Sample Output 2

2