program main
implicit none
integer :: i, j, k, l, m
character :: s*200000

read(*,*) s
l = len_trim(s)
if( l.le.2 ) then
   write(*,'(i0)') 0
   stop
end if
k = 0
do
   j = 0
   m = 1
   do i = m, l-2
      if( s(i:i+2) == 'ABC' ) then
         k = k + 1
         j = 1
         s(i:i+2) ='BCA'
         m = i+3
      end if
   end do
   if( j == 0 ) then
      write(*,'(i0)') k
      stop
   end if
end do

end program main


program main
  implicit none
  integer(8) i, sosa, rensa, N
  character(200000) s
  read(*, *) s
  N = len_trim(s)
  if (N < 3) then
    sosa = 0
  else
    sosa = 0
    rensa = 0
    do !while(i < N-2)
      if (s(i:i) == 'A') then
        rensa = rensa + 1
      else
        rensa = 0
      endif
      if (rensa > 0 .and. s(i+1:i+2) == 'BC') then
        sosa = sosa + rensa
        rensa = 0
        s(i+2:i+2) = 'A'
        i = i+2
      else
        i = i+1
      end if
      if (i > N-2) then
        exit
      end if
    end do
  end if
  write(* ,*) sosa
end program main

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

Score : 600 points

Problem Statement

You are given a string s consisting of A, B and C.

Snuke wants to perform the following operation on s as many times as possible:

Choose a contiguous substring of s that reads ABC and replace it with BCA.

Find the maximum possible number of operations.

Constraints

1 \leq |s| \leq 200000

Each character of s is A, B and C.

Input

Input is given from Standard Input in the following format:

s

Output

Find the maximum possible number of operations.

Sample Input 1

ABCABC

Sample Output 1

3

You can perform the operations three times as follows: ABCABC → BCAABC → BCABCA → BCBCAA. This is the maximum result.

Sample Input 2

C

Sample Output 2

0

Sample Input 3

ABCACCBABCBCAABCB

Sample Output 3

6

Submitted source-code file size, in bytes.

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

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


program main
implicit none
integer :: i, j, k, l
character :: s*200000

read(*,*) s
l = len_trim(s)
if( l.le.2 ) then
   write(*,'(i0)') 0
   stop
end if
k = 0
do
   j = 0
   do i = 1, l-2
      if( s(i:i+2) == 'ABC' ) then
         k = k + 1
         j = 1
         s(i:i+2) ='BCA'
      end if
   end do
   if( j == 0 ) then
      write(*,'(i0)') k
      stop
   end if
end do

end program main


program abc
  implicit none
  character(200000) :: s
  integer :: n, m, i
  read(*,*) s
  s = trim(adjustl(s))
  n = len_trim(s)
  if (n.lt.3) then
    write(*,'(i0)') 0
    stop
  end if
  m = 0
  i = 1
  do while (i.le.n-2)
    if (s(i:i+2).eq."ABC") then
      s(i:i+2) = "BCA"
      m = m+1
      i = max(1,i-1)
    else
      i = i+1
    end if
  end do
  write(*,'(i0)') m
  stop
end program abc


module mod_que
    type queue
        integer,pointer:: v(:)
        integer(4):: lst=0, fst=0
    end type
contains
    subroutine que_init(q)
        type(queue):: q

        allocate(q%v(1))
    end subroutine

    subroutine que(q,num)
        type(queue):: q
        integer(4):: num

        q%lst=q%lst+1
        if (q%lst > size(q%v)) call add(q)
        q%v(q%lst) = num
    end subroutine

    subroutine add(q)
        type(queue):: q
        integer(4),pointer:: tmp(:)
        integer(4):: l

        l = size(q%v)
        allocate(tmp(l))
        tmp(1:l) = q%v(1:l)
        deallocate(q%v)
        allocate(q%v(2*l))
        q%v(1:l) = tmp(1:l)
        deallocate(tmp)
    end subroutine

    function deque(q) result(ret)
        type(queue):: q
        integer(4):: ret
        q%fst=q%fst+1
        ret = q%v(q%fst)
    end function
end module mod_que

program name
    use mod_que
    implicit none
    integer(4):: h,w,i,j,nx,ny,cnt
    character(1),allocatable:: a(:,:)
    character(:),allocatable:: aa
    integer(4),allocatable:: cn(:,:)
    logical,allocatable:: see(:,:)
    logical:: all_see
    type(queue):: x,y

    call que_init(x)
    call que_init(y)
    read*, h,w
    allocate(a(0:w+1,0:h+1), cn(0:w+1,0:h+1), see(0:w+1,0:h+1))
    allocate(character(w):: aa)

    cn(:,:) = -2
    see(:,:) = .false.

    do i=1,h
        read*, aa(:)
        do j=1,w
            a(j,i) = aa(j:j)
            if (a(j,i) == '#') then
                cn(j,i) = 0
                see(j,i) = .true.
                call que(x,j)
                call que(y,i)
            else
                cn(j,i) = -1
            end if
        end do
    end do

    cnt=0
    do while(x%lst-x%fst > 0)
        cnt=cnt+1
        nx = deque(x)
        ny = deque(y)
        call next(nx+1,ny, nx,ny)
        call next(nx-1,ny, nx,ny)
        call next(nx,ny-1, nx,ny)
        call next(nx,ny+1, nx,ny)
        all_see = .true.
    end do

    print*, maxval(cn)
contains
    subroutine next(ix,iy, ox,oy)
        integer(4):: ix,iy,ox,oy
        if (cn(ix,iy) /= -1) return
        call que(x,ix)
        call que(y,iy)
        cn(ix,iy) = cn(ox,oy)+1
        see(ix,iy) = .true.
    end subroutine
end program name

Score : 300 points

Problem Statement

You are given a grid of squares with H horizontal rows and W vertical columns, where each square is painted white or black.
HW characters from A_{11} to A_{HW} represent the colors of the squares.
A_{ij} is # if the square at the i-th row from the top and the j-th column from the left is black, and A_{ij} is . if that square is white.

We will repeatedly perform the following operation until all the squares are black:

Every white square that shares a side with a black square, becomes black.

Find the number of operations that will be performed.
The initial grid has at least one black square.

Constraints

1 \leq H,W \leq 1000

A_{ij} is # or ..

The given grid has at least one black square.

Input

Input is given from Standard Input in the following format:

H W
A_{11}A_{12}...A_{1W}
:
A_{H1}A_{H2}...A_{HW}

Output

Print the number of operations that will be performed.

Sample Input 1

3 3
...
.#.
...

Sample Output 1

2

After one operation, all but the corners of the grid will be black. After one more operation, all the squares will be black.

Sample Input 2

6 6
..#..#
......
#..#..
......
.#....
....#.

Sample Output 2

3


module mod_que
    type queue
        integer,pointer:: v(:)
        integer(4):: i=0, ii=0
    end type
contains
    subroutine que_init(q)
        type(queue):: q
        allocate(q%v(1))
    end subroutine

    subroutine que(q,num)
        type(queue):: q
        integer(4):: num

        q%i=q%i+1
        if (q%i > size(q%v)) call add(q)
        q%v(q%i) = num
    end subroutine

    subroutine add(q)
        type(queue):: q
        integer(4),pointer:: tmp(:)
        integer(4):: l

        l = size(q%v)
        allocate(tmp(l))
        tmp(1:l) = q%v(1:l)
        deallocate(q%v)
        allocate(q%v(2*l))
        q%v(1:l) = tmp(1:l)
        deallocate(tmp)
    end subroutine

    function deque(q) result(ret)
        type(queue):: q
        integer(4):: ret

        q%ii=q%ii+1
        ret = q%v(q%ii)
    end function
end module mod_que

program name
    use mod_que
    implicit none
    integer(4):: h,w,i,j,nx,ny,cnt
    character(1),allocatable:: a(:,:)
    character(:),allocatable:: aa
    integer(4),allocatable:: cn(:,:)
    logical,allocatable:: see(:,:)
    logical:: all_see
    type(queue):: x,y

    call que_init(x)
    call que_init(y)
    read*, h,w
    allocate(a(0:w+1,0:h+1), cn(0:w+1,0:h+1), see(0:w+1,0:h+1))
    allocate(character(w):: aa)

    cn(:,:) = -2
    see(:,:) = .false.

    do i=1,h
        read*, aa(:)
        do j=1,w
            a(j,i) = aa(j:j)
            if (a(j,i) == '#') then
                cn(j,i) = 0
                see(j,i) = .true.
                call que(x,j)
                call que(y,i)
            else
                cn(j,i) = -1
            end if
        end do
    end do

    cnt=0
    do while(x%i-x%ii > 0)
        cnt=cnt+1
        nx = deque(x)
        ny = deque(y)
        ! print*, '-'
        ! print*, x%v(x%ii:x%i)
        ! print*, y%v(x%ii:x%i)
        call next(nx+1,ny, nx,ny)
        call next(nx-1,ny, nx,ny)
        call next(nx,ny-1, nx,ny)
        call next(nx,ny+1, nx,ny)
        all_see = .true.

        ! do i=1,h
        !     do j=1,w
        !         if (.not. see(j,i)) all_see =.false.
        !     end do
        ! end do

        ! if(all_see) exit
    end do

    ! print*, 'cnt',cnt
    print*, maxval(cn)
contains
    subroutine next(ix,iy, ox,oy)
        integer(4):: ix,iy,ox,oy
        if (cn(ix,iy) /= -1) return
        call que(x,ix)
        call que(y,iy)
        cn(ix,iy) = cn(ox,oy)+1
        see(ix,iy) = .true.
    end subroutine
end program name


module mod_que
    type queue
        integer,pointer:: v(:)
        integer(4):: i=0, ii=0
    end type
contains
    subroutine que_init(q)
        type(queue):: q
        allocate(q%v(1))
    end subroutine

    subroutine que(q,num)
        type(queue):: q
        integer(4):: num

        q%i=q%i+1
        if (q%i > size(q%v)) call add(q)
        q%v(q%i) = num
    end subroutine

    subroutine add(q)
        type(queue):: q
        integer(4),pointer:: tmp(:)
        integer(4):: l

        l = size(q%v)
        allocate(tmp(l))
        tmp(1:l) = q%v(1:l)
        deallocate(q%v)
        allocate(q%v(2*l))
        q%v(1:l) = tmp(1:l)
        deallocate(tmp)
    end subroutine

    function deque(q) result(ret)
        type(queue):: q
        integer(4):: ret

        q%ii=q%ii+1
        ret = q%v(q%ii)
    end function
end module mod_que

program name
    use mod_que
    implicit none
    integer(4):: h,w,i,j,nx,ny,cnt
    character(1),allocatable:: a(:,:)
    character(:),allocatable:: aa
    integer(4),allocatable:: cn(:,:)
    logical,allocatable:: see(:,:)
    logical:: all_see
    type(queue):: x,y

    call que_init(x)
    call que_init(y)
    ! read*, h,w
    h=1000
    w=1000
    allocate(a(0:w+1,0:h+1), cn(0:w+1,0:h+1), see(0:w+1,0:h+1))
    allocate(character(w):: aa)

    cn(:,:) = -2
    see(:,:) = .false.

    do i=1,h
        ! read*, aa(:)
        do j=1,w
            ! a(j,i) = aa(j:j)
            a(j,i) = '.'
            a(1,1) = '#'
            a(100,120) = '#'
            a(400,59) = '#'
            a(300,120) = '#'
            if (a(j,i) == '#') then
                cn(j,i) = 0
                see(j,i) = .true.
                call que(x,j)
                call que(y,i)
            else
                cn(j,i) = -1
            end if
        end do
    end do

    cnt=0
    do while(x%i-x%ii > 0)
        cnt=cnt+1
        nx = deque(x)
        ny = deque(y)
        ! print*, '-'
        ! print*, x%v(x%ii:x%i)
        ! print*, y%v(x%ii:x%i)
        call next(nx+1,ny, nx,ny)
        call next(nx-1,ny, nx,ny)
        call next(nx,ny-1, nx,ny)
        call next(nx,ny+1, nx,ny)
        all_see = .true.

        ! do i=1,h
        !     do j=1,w
        !         if (.not. see(j,i)) all_see =.false.
        !     end do
        ! end do

        ! if(all_see) exit
    end do

    print*, 'cnt',cnt
    print*, maxval(cn)
contains
    subroutine next(ix,iy, ox,oy)
        integer(4):: ix,iy,ox,oy
        if (cn(ix,iy) /= -1) return
        call que(x,ix)
        call que(y,iy)
        cn(ix,iy) = cn(ox,oy)+1
        see(ix,iy) = .true.
    end subroutine
end program name


module mod_que
    type queue
        integer,pointer:: v(:)
        integer(4):: i=0, ii=0
    end type
contains
    subroutine que_init(q)
        type(queue):: q
        allocate(q%v(1))
    end subroutine

    subroutine que(q,num)
        type(queue):: q
        integer(4):: num

        q%i=q%i+1
        if (q%i > size(q%v)) call add(q)
        q%v(q%i) = num
    end subroutine

    subroutine add(q)
        type(queue):: q
        integer(4),pointer:: tmp(:)
        integer(4):: l

        l = size(q%v)
        allocate(tmp(l))
        tmp(1:l) = q%v(1:l)
        deallocate(q%v)
        allocate(q%v(2*l))
        q%v(1:l) = tmp(1:l)
        deallocate(tmp)
    end subroutine

    function deque(q) result(ret)
        type(queue):: q
        integer(4):: ret

        q%ii=q%ii+1
        ret = q%v(q%ii)
    end function
end module mod_que

program name
    use mod_que
    implicit none
    integer(4):: h,w,i,j,nx,ny,cnt
    character(1),allocatable:: a(:,:)
    character(:),allocatable:: aa
    integer(4),allocatable:: cn(:,:)
    logical,allocatable:: see(:,:)
    logical:: all_see
    type(queue):: x,y

    call que_init(x)
    call que_init(y)
    read*, h,w
    allocate(a(0:w+1,0:h+1), cn(0:w+1,0:h+1), see(0:w+1,0:h+1))
    allocate(character(w):: aa)

    cn(:,:) = -2
    see(:,:) = .false.

    do i=1,h
        read*, aa(:)
        do j=1,w
            a(j,i) = aa(j:j)
            if (a(j,i) == '#') then
                cn(j,i) = 0
                see(j,i) = .true.
                call que(x,j)
                call que(y,i)
            else
                cn(j,i) = -1
            end if
        end do
    end do

    ! cnt=0
    ! do while(x%i-x%ii > 0)
    !     cnt=cnt+1
    !     nx = deque(x)
    !     ny = deque(y)
    !     call next(nx+1,ny, nx,ny)
    !     call next(nx-1,ny, nx,ny)
    !     call next(nx,ny-1, nx,ny)
    !     call next(nx,ny+1, nx,ny)
    !     all_see = .true.

    !     do i=1,h
    !         do j=1,w
    !             if (.not. see(j,i)) all_see =.false.
    !         end do
    !     end do

    !     if(all_see) exit
    ! end do

    ! print*, cnt
    ! print*, maxval(cn)
contains
    subroutine next(ix,iy, ox,oy)
        integer(4):: ix,iy,ox,oy
        if (cn(ix,iy) /= -1) return
        call que(x,ix)
        call que(y,iy)
        cn(ix,iy) = cn(ox,oy)+1
        see(ix,iy) = .true.
    end subroutine
end program name


module mod_que
    type queue
        integer,pointer:: v(:)
        integer(4):: i=0
    end type
contains
    subroutine que_init(q)
        type(queue):: q
        allocate(q%v(1))
    end subroutine

    subroutine que(q,num)
        type(queue):: q
        integer(4):: num

        q%i=q%i+1
        if (q%i > size(q%v)) call add(q)
        q%v(q%i) = num
    end subroutine

    subroutine add(q)
        type(queue):: q
        integer(4),pointer:: tmp(:)
        integer(4):: l

        l = size(q%v)
        allocate(tmp(l))
        tmp(1:l) = q%v(1:l)
        deallocate(q%v)
        allocate(q%v(2*l))
        q%v(1:l) = tmp(1:l)
        deallocate(tmp)
    end subroutine

    function deque(q) result(ret)
        type(queue):: q
        integer(4),pointer:: t(:)
        integer(4):: ret,l
        ret = q%v(1)
        q%i=q%i-1
        l = q%i
        allocate(t(l))
        t(:) = q%v(2:l+1)
        q%v(:l) = t(:)
        deallocate(t)
    end function
end module mod_que

program name
    use mod_que
    implicit none
    integer(4):: h,w,i,j,nx,ny,cnt
    character(1),allocatable:: a(:,:)
    character(:),allocatable:: aa
    integer(4),allocatable:: cn(:,:)
    logical,allocatable:: see(:,:)
    logical:: all_see
    type(queue):: x,y

    call que_init(x)
    call que_init(y)
    read*, h,w
    allocate(a(w,h), cn(w,h), see(w,h))
    allocate(character(w):: aa)

    cn(:,:) = -1
    see(:,:) = .false.
    do i=1,h
        read*, aa(:)
        do j=1,w
            a(j,i) = aa(j:j)
            if (a(j,i) == '#') then
                cn(j,i) = 0
                see(j,i) = .true.
                call que(x,j)
                call que(y,i)
            end if
        end do
    end do
    cnt=0
    do while(x%i > 0)
        cnt=cnt+1
        ! print*, cnt
        nx = deque(x)
        ny = deque(y)
        call next(nx+1,ny, nx,ny)
        call next(nx-1,ny, nx,ny)
        call next(nx,ny-1, nx,ny)
        call next(nx,ny+1, nx,ny)
        all_see = .true.

        do i=1,h
            do j=1,w
                if (.not. see(j,i)) all_see =.false.
            end do
        end do
        if(all_see) exit
    end do
    print*, maxval(cn)
contains
    subroutine next(ix,iy, ox,oy)
        integer(4):: ix,iy,ox,oy
        if (.not. (1<=ix .and. ix<=w .and. 1<=iy .and. iy<=h)) return
        if (see(ix,iy)) return
        call que(x,ix)
        call que(y,iy)
        cn(ix,iy) = cn(ox,oy)+1
        see(ix,iy) = .true.
    end subroutine
end program name


module mod_que
    type queue
        integer,pointer:: v(:)
        integer(4):: i=0
    end type
contains
    subroutine que_init(q)
        type(queue):: q
        allocate(q%v(1))
    end subroutine

    subroutine que(q,num)
        type(queue):: q
        integer(4):: num

        q%i=q%i+1
        if (q%i > size(q%v)) call add(q)
        q%v(q%i) = num
    end subroutine

    subroutine add(q)
        type(queue):: q
        integer(4),pointer:: tmp(:)
        integer(4):: l

        l = size(q%v)
        allocate(tmp(l))
        tmp(1:l) = q%v(1:l)
        deallocate(q%v)
        allocate(q%v(2*l))
        q%v(1:l) = tmp(1:l)
        deallocate(tmp)
    end subroutine

    function deque(q) result(ret)
        type(queue):: q
        integer(4),pointer:: t(:)
        integer(4):: ret,l
        ret = q%v(1)
        q%i=q%i-1
        l = q%i
        allocate(t(l))
        t(:) = q%v(2:l+1)
        q%v(:l) = t(:)
        deallocate(t)
    end function
end module mod_que

program name
    use mod_que
    implicit none
    integer(4):: h,w,i,j,nx,ny
    character(1),allocatable:: a(:,:)
    character(:),allocatable:: aa
    integer(4),allocatable:: cn(:,:)
    logical,allocatable:: see(:,:)
    logical:: all_see
    type(queue):: x,y

    call que_init(x)
    call que_init(y)
    read*, h,w
    allocate(a(w,h), cn(w,h), see(w,h))
    allocate(character(w):: aa)

    cn(:,:) = -1
    see(:,:) = .false.
    do i=1,h
        read*, aa(:)
        do j=1,w
            a(j,i) = aa(j:j)
            if (a(j,i) == '#') then
                cn(j,i) = 0
                see(j,i) = .true.
                call que(x,j)
                call que(y,i)
            end if
        end do
    end do

    do while(x%i > 0)
        nx = deque(x)
        ny = deque(y)
        call next(nx+1,ny, nx,ny)
        call next(nx-1,ny, nx,ny)
        call next(nx,ny-1, nx,ny)
        call next(nx,ny+1, nx,ny)
        all_see = .true.
    end do
    print*, maxval(cn)
contains
    subroutine next(ix,iy, ox,oy)
        integer(4):: ix,iy,ox,oy

        if (.not. (1<=ix .and. ix<=w .and. 1<=iy .and. iy<=h)) return
        if (see(ix,iy)) return
        call que(x,ix)
        call que(y,iy)
        cn(ix,iy) = cn(ox,oy)+1
        see(ix,iy) = .true.
    end subroutine
end program name


module mod_que
    type queue
        integer,pointer:: v(:)
        integer(4):: i=0
    end type
contains
    subroutine que_init(q)
        type(queue):: q
        allocate(q%v(1))
    end subroutine

    subroutine que(q,num)
        type(queue):: q
        integer(4):: num

        q%i=q%i+1
        if (q%i > size(q%v)) call add(q)
        q%v(q%i) = num
    end subroutine

    subroutine add(q)
        type(queue):: q
        integer(4),pointer:: tmp(:)
        integer(4):: l

        l = size(q%v)
        allocate(tmp(l))
        tmp(1:l) = q%v(1:l)
        deallocate(q%v)
        allocate(q%v(2*l))
        q%v(1:l) = tmp(1:l)
        deallocate(tmp)
    end subroutine

    function deque(q) result(ret)
        type(queue):: q
        integer(4),pointer:: t(:)
        integer(4):: ret,l
        ret = q%v(1)
        q%i=q%i-1
        l = q%i
        allocate(t(l))
        t(:) = q%v(2:l+1)
        q%v(:l) = t(:)
        deallocate(t)
    end function
end module mod_que

program name
    use mod_que
    implicit none
    integer(4):: h,w,i,j,nx,ny,dx,dy
    character(1),allocatable:: a(:,:)
    character(:),allocatable:: aa
    integer(4),allocatable:: cn(:,:)
    logical,allocatable:: see(:,:)
    logical:: all_see
    type(queue):: x,y

    call que_init(x)
    call que_init(y)
    read*, h,w
    allocate(a(w,h), cn(w,h), see(w,h))
    allocate(character(w):: aa)

    cn(:,:) = -1
    see(:,:) = .false.
    do i=1,h
        read*, aa(:)
        do j=1,w
            a(j,i) = aa(j:j)
            if (a(j,i) == '#') then
                cn(j,i) = 0
                see(j,i) = .true.
                call que(x,j)
                call que(y,i)
            end if
        end do
    end do

    do while(x%i > 0)
        nx = deque(x)
        ny = deque(y)
        see(nx,ny) = .true.
        ! print*, nx,ny
        call next(nx+1,ny, nx,ny)
        call next(nx-1,ny, nx,ny)
        call next(nx,ny-1, nx,ny)
        call next(nx,ny+1, nx,ny)
        all_see = .true.
        do i=1,h
            ! print*, see(:,i)
            do j=1,w
                if (.not. see(j,i)) all_see = .false.
            end do
        end do
        if (all_see) exit
    end do

    print*, maxval(cn)

    contains
    subroutine next(ix,iy, ox,oy)
        integer(4):: ix,iy,ox,oy

        ! print*, 1<=ix, ix<=w, 1<=iy, iy<=h, ix, iy
        if (.not. (1<=ix .and. ix<=w .and. 1<=iy .and. iy<=h)) return
        if (see(ix,iy)) return
        call que(x,ix)
        call que(y,iy)
        cn(ix,iy) = cn(ox,oy)+1
    end subroutine
end program name


module mod_que
    type queue
        integer,pointer:: v(:)
        integer(4):: i=0
    end type
contains
    subroutine que_init(q)
        type(queue):: q
        allocate(q%v(1))
    end subroutine

    subroutine que(q,num)
        type(queue):: q
        integer(4):: num

        q%i=q%i+1
        if (q%i > size(q%v)) call add(q)
        q%v(q%i) = num
    end subroutine

    subroutine add(q)
        type(queue):: q
        integer(4),pointer:: tmp(:)
        integer(4):: l

        l = size(q%v)
        allocate(tmp(l))
        tmp(1:l) = q%v(1:l)
        deallocate(q%v)
        allocate(q%v(2*l))
        q%v(1:l) = tmp(1:l)
        deallocate(tmp)
    end subroutine

    function deque(q) result(ret)
        type(queue):: q
        integer(4):: ret

        ret = q%v(1)
        q%i=q%i-1
        q%v(1:q%i) = q%v(2:q%i+1)
    end function
end module mod_que

program name
    use mod_que
    implicit none
    integer(4):: h,w,i,j,nx,ny,dx,dy
    character(1),allocatable:: a(:,:)
    character(:),allocatable:: aa
    integer(4),allocatable:: cn(:,:)
    logical,allocatable:: see(:,:)
    logical:: all_see
    type(queue):: x,y

    call que_init(x)
    call que_init(y)
    read*, h,w
    allocate(a(w,h), cn(w,h), see(w,h))
    allocate(character(w):: aa)

    cn(:,:) = -1
    see(:,:) = .false.
    do i=1,h
        read*, aa(:)
        do j=1,h
            a(j,i) = aa(j:j)
            if (a(j,i) == '#') then
                cn(j,i) = 0
                see(j,i) = .true.
                call que(x,j)
                call que(y,i)
            end if
        end do
    end do

    do while(x%i > 0)
        nx = deque(x)
        ny = deque(y)

        do dx=-1,1
            do dy=-1,1
                if (abs(dx) == abs(dy)) cycle
                if ((nx+dx < 1 .or. nx+dx > w) .or. (ny+dy < 1 .or. ny+dy > h)) cycle
                if (see(nx+dx,ny+dy))cycle
                cn(nx+dx,ny+dy) = cn(nx,ny)+1
                see(nx+dx,ny+dy) = .true.
                call que(x,nx+dx)
                call que(y,ny+dy)
            end do
        end do
        all_see = .true.
        do i=1,h
            do j=1,w
                if (.not. see(j,i)) all_see = .false.
            end do
        end do
        if (all_see) exit
    end do

    print*, maxval(cn)
end program name


module mod_que
    type queue
        integer,pointer:: v(:)
        integer(4):: i=0
    end type
contains
    subroutine que_init(q)
        type(queue):: q
        allocate(q%v(1))
    end subroutine

    subroutine que(q,num)
        type(queue):: q
        integer(4):: num

        q%i=q%i+1
        if (q%i > size(q%v)) call add(q)
        q%v(q%i) = num
    end subroutine

    subroutine add(q)
        type(queue):: q
        integer(4),pointer:: tmp(:)
        integer(4):: l

        l = size(q%v)
        allocate(tmp(l))
        tmp(1:l) = q%v(1:l)
        deallocate(q%v)
        allocate(q%v(2*l))
        q%v(1:l) = tmp(1:l)
        deallocate(tmp)
    end subroutine

    function deque(q) result(ret)
        type(queue):: q
        integer(4):: ret

        ret = q%v(1)
        q%i=q%i-1
        q%v(1:q%i) = q%v(2:q%i+1)
    end function
end module mod_que

program name
    use mod_que
    implicit none
    integer(4):: h,w,i,j,nx,ny,dx,dy
    character(1),allocatable:: a(:,:)
    character(:),allocatable:: aa
    integer(4),allocatable:: cn(:,:)
    type(queue):: x,y

    call que_init(x)
    call que_init(y)
    read*, h,w
    allocate(a(w,h), cn(w,h))
    allocate(character(w):: aa)

    cn(:,:) = -1

    do i=1,h
        read*, aa(:)
        do j=1,h
            a(j,i) = aa(j:j)
            if (a(j,i) == '#') then
                cn(j,i) = 0
                call que(x,j)
                call que(y,i)
            end if
        end do
    end do

    do while(x%i > 0)
        nx = deque(x)
        ny = deque(y)

        do dx=-1,1
            do dy=-1,1
                if (abs(dx) == abs(dy)) cycle
                if ((nx+dx < 1 .or. nx+dx > w) .or. (ny+dy < 1 .or. ny+dy > h)) cycle
                if (cn(nx+dx,ny+dy) == -1) then
                    cn(nx+dx,ny+dy) = cn(nx,ny)+1
                    call que(x,nx+dx)
                    call que(y,ny+dy)
                end if
            end do
        end do
    end do

    print*, maxval(cn)
end program name


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 t_node

  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 t_queue

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

module maze
integer,allocatable,dimension(:,:)::mazemap
integer::dirx(4)=[1,0,-1,0]
integer::diry(4)=[0,1,0,-1]

integer ans

contains

subroutine getmap(mazemap,R,C)
  integer,allocatable,dimension(:,:)::mazemap
  integer R,C
  character(1000) linereader
  allocate(mazemap(0:R+1,0:C+1))
  mazemap=0
  do i=1,R
    read*,linereader
    do j=1,C
      if(linereader(j:j)==".")mazemap(i,j)=-1
    end do
  end do
end subroutine
end module maze
use mod_queue
use maze
integer H,W
type(t_queue) :: pq(2)
read*,H,W
call getmap(mazemap,H,W)
do i=1,H
  do j=1,W
    if(mazemap(i,j)==0)then
      do k=1,4
        if(mazemap(i+dirx(k),j+diry(k) )==-1)then
          mazemap(i+dirx(k),j+diry(k) )=1
          call enqueue(pq(1),i+dirx(k))
          call enqueue(pq(2),j+diry(k))
        endif
      end do
    endif
  end do
end do

ans=0
do while(size_of(pq(1))/=0)
  itemsize=size_of(pq(1))
  do ii=1,itemsize
    i=dequeue(pq(1))
    j=dequeue(pq(2))
    do k=1,4
      if(mazemap(i+dirx(k),j+diry(k) )==-1)then
        mazemap(i+dirx(k),j+diry(k) )=mazemap(i,j)+1
        ans=max(ans,mazemap(i,j)+1)
        call enqueue(pq(1),i+dirx(k))
        call enqueue(pq(2),j+diry(k))
      endif
    end do
  end do
end do

print"(I0)",ans

end


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 t_node

  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 t_queue

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

module maze
integer,allocatable,dimension(:,:)::mazemap
integer::dirx(4)=[1,0,-1,0]
integer::diry(4)=[0,1,0,-1]

integer ans

contains

subroutine getmap(mazemap,R,C)
  integer,allocatable,dimension(:,:)::mazemap
  integer R,C
  character(1000) linereader
  allocate(mazemap(0:R+1,0:C+1))
  mazemap=0
  do i=1,R
    read*,linereader
    do j=1,C
      if(linereader(j:j)==".")mazemap(i,j)=-1
    end do
  end do
end subroutine
end module maze
use mod_queue
use maze
integer H,W
type(t_queue) :: pq(2)
read*,H,W
call getmap(mazemap,H,W)
do i=1,H
  do j=1,W
    if(mazemap(i,j)==0)then
      do k=1,4
        if(mazemap(i+dirx(k),j+diry(k) )==-1)then
          mazemap(i+dirx(k),j+diry(k) )=1
          call enqueue(pq(1),i+dirx(k))
          call enqueue(pq(2),j+diry(k))
        endif
      end do
    endif
  end do
end do

ans=0
do while(size_of(pq(1))/=0)
  itemsize=size_of(pq(1))
  do ii=1,itemsize
    i=dequeue(pq(1))
    j=dequeue(pq(2))
    do k=1,4
      if(mazemap(i+dirx(k),j+diry(k) )==-1)then
        mazemap(i+dirx(k),j+diry(k) )=mazemap(i,j)+1
        ans=max(ans,mazemap(i,j)+1)
        call enqueue(pq(1),i+dirx(k))
        call enqueue(pq(2),i+diry(k))
      endif
    end do
  end do
end do

print"(I0)",ans

end


program agc033a
  implicit none
  character(1000) s
  integer h,w,x,y
  integer,allocatable :: grid(:,:)

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

  do y = 1,h
     read *,s
     do x = 1,w
        grid(x,y) = merge(0,1000000,s(x:x)=='#')
     end do
  end do

  do x = 2,w
     grid(x,:) = merge(grid(x-1,:)+1,grid(x,:),grid(x,:)-grid(x-1,:)>1)
     grid(w-x+1,:) = merge(grid(w-x+2,:)+1,grid(w-x+1,:),grid(w-x+1,:)-grid(w-x+2,:)>1)
  end do

  do y = 2,h
     grid(:,y) = merge(grid(:,y-1)+1,grid(:,y),grid(:,y)-grid(:,y-1)>1)
     grid(:,h-y+1) = merge(grid(:,h-y+2)+1,grid(:,h-y+1),grid(:,h-y+1)-grid(:,h-y+2)>1)
  end do

  print *,maxval(grid)
end program agc033a


program ccc
implicit none

integer(8) :: n, i, one, two, gl, gr, g, res
integer(8),allocatable,dimension(:) :: a, l, r

read*, n
allocate(a(n),l(0:n+1),r(0:n+1))
read*, a

l=0
r=0
l(1)=a(1)
r(n)=a(n)

do i=2,n
one=l(i-1)
two=a(i)
call gcd(one,two,gl)
l(i)=gl
end do

do i=n-1,1,-1
one=r(i+1)
two=a(i)
call gcd(one,two,gr)
r(i)=gr
end do

res=0
g=0
do i=1,n
gl=l(i-1)
gr=r(i+1)
if(gl==0) gl=gr
if(gr==0) gr=gl
call gcd(gl,gr,g)
res=max(res,g)
end do

write(*,'(i0)') res

end program

subroutine gcd(a,b,ans)
integer(8) :: a, b, aa, bb, m, ans

aa=max(a,b)
bb=min(a,b)
a=aa
b=bb
m=1

do while(m/=0)
 m=mod(a,b)
 a=b
 b=m
end do
ans=a
end subroutine gcd

Score : 300 points

Problem Statement

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

You will choose one of them and replace it with an integer of your choice between 1 and 10^9 (inclusive), possibly the same as the integer originally written.

Find the maximum possible greatest common divisor of the N integers on the blackboard after your move.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq A_i \leq 10^9

Output

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible greatest common divisor of the N integers on the blackboard after your move.

Sample Input 1

3
7 6 8

Sample Output 1

2

If we replace 7 with 4, the greatest common divisor of the three integers on the blackboard will be 2, which is the maximum possible value.

Sample Input 2

3
12 15 18

Sample Output 2

6

Sample Input 3

2
1000000000 1000000000

Sample Output 3

1000000000

We can replace an integer with itself.


program ccc
implicit none

integer(8) :: n, i, one, two, g, dumy, res1, res2
integer(8),dimension(1) :: l
integer(8),allocatable,dimension(:) :: a, gg

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

one=a(1)
gg(1)=one
do i=2,n
one=a(1)
two=a(i)
call gcd(one,two,g)
gg(i)=g
end do

l=minloc(gg)
gg(l)=one
l=minloc(gg)
res1=gg(l(1))

dumy=a(1)
a(1)=a(2)
a(2)=dumy

one=a(1)
gg(1)=one
do i=2,n
one=a(1)
two=a(i)
call gcd(one,two,g)
gg(i)=g
end do

l=minloc(gg)
gg(l)=one
l=minloc(gg)
res2=gg(l(1))

write(*,'(i0)') max(res1,res2)

end program

subroutine gcd(a,b,ans)
integer(8) :: a, b, aa, bb, m, ans

aa=max(a,b)
bb=min(a,b)
a=aa
b=bb
m=1
do while(m/=0)
 m=mod(a,b)
 a=b
 b=m
end do
ans=a
end subroutine gcd


program ccc
implicit none

integer(8) :: n, i, one, two, g, res1, res2
integer(8),dimension(1) :: l
integer(8),allocatable,dimension(:) :: a, gg

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

one=a(1)
gg(1)=one
do i=2,n
one=a(1)
two=a(i)
call gcd(one,two,g)
gg(i)=g
end do

l=minloc(gg)
gg(l)=one
l=minloc(gg)
res1=gg(l(1))

one=(a(2))
gg(1)=a(2)
do i=2,n
one=a(2)
two=a(i)
call gcd(one,two,g)
gg(i)=g
end do

l=minloc(gg)
res2=gg(l(1))

write(*,'(i0)') max(res1,res2)

end program

subroutine gcd(a,b,ans)
integer(8) :: a, b, aa, bb, m, ans

aa=max(a,b)
bb=min(a,b)
a=aa
b=bb
m=1
do while(m/=0)
 m=mod(a,b)
 a=b
 b=m
end do
ans=a
end subroutine gcd


module ABC125

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

  ! require all variables to be explicitly declared
  implicit none

  ! accessibility of <subroutine>s and <function>s in this <module>
  public  :: task_C
  private :: calculate_gcd

  ! variables for this <module>
  integer(INT32), private              :: num_integers
  integer(INT32), private              :: gcd_max
  integer(INT32), private, allocatable :: list_integer (:)
  integer(INT32), private, allocatable :: list_gcd_fore(:)
  integer(INT32), private, allocatable :: list_gcd_rear(:)

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

  subroutine task_C

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

    ! STEP.01
    ! read out the number of given integers
    read(unit=INPUT_UNIT, fmt=*) num_integers

    ! STEP.02
    ! allocate the array to store the given integers
    allocate( list_integer (1:num_integers  ) )
    allocate( list_gcd_fore(0:num_integers+1) )
    allocate( list_gcd_rear(1:num_integers+1) )

    ! STEP.03
    ! read out the given integers
    read(unit=INPUT_UNIT, fmt=*) list_integer(1:num_integers)

    ! STEP.04
    ! calculate the greatesst common divisors
    list_gcd_fore(0               ) = 0_INT32
    list_gcd_rear(num_integers + 1) = 0_INT32

    do itr = 0_INT32, num_integers, 1_INT32
      list_gcd_fore(itr+1) = calculate_gcd( list_gcd_fore(itr), list_integer(itr) )
    end do

    do itr = num_integers, 1_INT32, -1_INT32
      list_gcd_rear(itr) = calculate_gcd( list_gcd_rear(itr+1), list_integer(itr) )
    end do

    gcd_max = 0_INT32

    do itr = 1_INT32, num_integers, 1_INT32
      gcd_max = max(gcd_max, calculate_gcd( list_gcd_fore(itr), list_gcd_rear(itr+1) ))
    end do

    ! STEP.05
    ! output the maximum greatest common divisor
    write(unit=OUTPUT_UNIT, fmt='(I0)', advance='yes') gcd_max

    ! STEP.06
    ! deallocate the array to store the given integers
    deallocate( list_integer  )
    deallocate( list_gcd_fore )
    deallocate( list_gcd_rear )

    ! STEP.END
    return

  end subroutine task_C

  recursive function calculate_gcd (x, y) result(gcd)

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

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

    if (y .eq. 0_INT32) then
      gcd = x
    else
      gcd = calculate_gcd(y, mod(x, y))
    end if

    return

  end function calculate_gcd

end module ABC125

program main

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

  ! require all variables to be explicitly declared
  implicit none

  call task_C

end program main


module ABC125

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

  ! require all variables to be explicitly declared
  implicit none

  ! accessibility of <subroutine>s and <function>s in this <module>
  public  :: task_C
  private :: calculate_gcd

  ! variables for this <module>
  integer(INT32), private              :: num_integers
  integer(INT32), private, allocatable :: list_integer (:)
  integer(INT32), private, allocatable :: list_gcd_fore(:)
  integer(INT32), private, allocatable :: list_gcd_rear(:)
  integer(INT32), private, allocatable :: list_gcd_max (:)

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

  subroutine task_C

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

    ! STEP.01
    ! read out the number of given integers
    read(unit=INPUT_UNIT, fmt=*) num_integers

    ! STEP.02
    ! allocate the array to store the given integers
    allocate( list_integer (1:num_integers  ) )
    allocate( list_gcd_fore(0:num_integers+1) )
    allocate( list_gcd_rear(1:num_integers+1) )
    allocate( list_gcd_max (1:num_integers  ) )

    ! STEP.03
    ! read out the given integers
    read(unit=INPUT_UNIT, fmt=*) list_integer(1:num_integers)

    ! STEP.04
    ! calculate the greatesst common divisors
    list_gcd_fore(0               ) = 0_INT32
    list_gcd_rear(num_integers + 1) = 0_INT32

    do itr = 0_INT32, num_integers, 1_INT32
      list_gcd_fore(itr+1) = calculate_gcd( list_gcd_fore(itr), list_integer(itr) )
    end do

    do itr = num_integers, 1_INT32, -1_INT32
      list_gcd_rear(itr) = calculate_gcd( list_gcd_rear(itr+1), list_integer(itr) )
    end do

    do itr = 1_INT32, num_integers, 1_INT32
      list_gcd_max(itr) = calculate_gcd( list_gcd_fore(itr), list_gcd_rear(itr+1) )
    end do

    ! STEP.05
    ! output the maximum greatest common divisor
    write(unit=OUTPUT_UNIT, fmt='(I0)', advance='yes') maxval( list_gcd_max(:), dim=1 )

    ! STEP.06
    ! deallocate the array to store the given integers
    deallocate( list_integer  )
    deallocate( list_gcd_fore )
    deallocate( list_gcd_rear )
    deallocate( list_gcd_max  )

    ! STEP.END
    return

  end subroutine task_C

  recursive function calculate_gcd (x, y) result(gcd)

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

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

    if (y .eq. 0_INT32) then
      gcd = x
    else
      gcd = calculate_gcd(y, mod(x, y))
    end if

    return

  end function calculate_gcd

end module ABC125

program main

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

  ! require all variables to be explicitly declared
  implicit none

  call task_C

end program main


PROGRAM ATCODER

implicit none
integer :: n,i,r1,r2,b1,b2,c1,c2
integer,allocatable :: a(:),a_l(:),a_r(:),ans(:)

read*, n
allocate(a(n),a_l(n),a_r(n),ans(n))
read*, a(1:n)

a_l(1) = a(1)
a_r(1) = a(n)

do i = 2, n
  b1 = a(i)
  c1 = a_l(i-1)
  b2 = a(n+1-i)
  c2 = a_r(i-1)
  do
    r1 = mod(c1,b1)
    if ( r1 == 0 ) exit
    c1 = b1
    b1 = r1
  end do
  do
    r2 = mod(c2,b2)
    if ( r2 == 0 ) exit
    c2 = b2
    b2 = r2
  end do
  a_l(i) = b1
  a_r(i) = b2
end do

ans(1) = a_r(n-1)
ans(n) = a_l(n-1)

do i = 2, n-1
  c1 = a_l(i-1)
  b1 = a_r(n-i)
  do
    r1 = mod(c1,b1)
    if ( r1 == 0 ) exit
    c1 = b1
    b1 = r1
  end do
  ans(i) = b1
end do

print'(i0)', maxval(ans)

END PROGRAM ATCODER


module ABC125

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

  ! require all variables to be explicitly declared
  implicit none

  ! accessibility of <subroutine>s and <function>s in this <module>
  public  :: task_C
  private :: calc_GCD

  ! constants for this <program>
  integer(INT32), parameter, private :: num_integers_max = 10_INT32 ** 5_INT32

  ! variables for this <module>
  integer(INT32), private :: num_integers

  ! arrays for this <module>
  integer(INT32), private :: val_integer(1:num_integers_max)
  integer(INT32), private :: val_GCD    (1:num_integers_max)

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

  subroutine task_C

    ! support variables for this <subroutine>
    integer :: iter_elem
    integer :: iter_trgt

    ! STEP.01
    ! read out the number of target integers
    read(unit=INPUT_UNIT, fmt=*) num_integers

    ! STEP.02
    ! read out the value of the given integers
    read(unit=INPUT_UNIT, fmt=*) val_integer(1:num_integers)

    val_GCD(:) = 0_INT32

    do iter_trgt = 1, num_integers, 1

      do iter_elem = 1, num_integers, 1
        if (iter_elem .ne. iter_trgt) then
          val_GCD(iter_trgt) = calc_GCD(val_GCD(iter_trgt), val_integer(iter_elem))
        end if
      end do

    end do

    ! STEP.03
    ! output the maximum value of the greatest common divisor
    write(unit=OUTPUT_UNIT, fmt='(I0)', advance='yes') maxval(val_GCD(:), dim=1)

    ! STEP.END
    return

  end subroutine task_C

  pure function calc_GCD(target1, target2) result(gcd)

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

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

    ! variables for this <function>
    integer(INT32) :: buffer, buffer1, buffer2

    ! STEP.01
    ! handle the exception
    if (target1 .eq. 0_INT32) then; gcd = target2; return; end if
    if (target2 .eq. 0_INT32) then; gcd = target1; return; end if

    ! STEP.02
    ! store the values of the given integers
    buffer1 = target1
    buffer2 = target2

    ! STEP.03
    ! calculate the greatest common divisor
    do while (buffer2 .ne. 0_INT32)
      buffer  = mod(buffer1, buffer2)
      buffer1 = buffer2
      buffer2 = buffer
    end do

    gcd = buffer1

    ! STEP.END
    return

  end function calc_GCD

end module ABC125

program main

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

  ! require all variables to be explicitly declared
  implicit none

  call task_C

end program main


module ABC125

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

  ! require all variables to be explicitly declared
  implicit none

  ! accessibility of <subroutine>s and <function>s in this <module>
  public  :: task_C
  private :: calc_GCD

  ! constants for this <program>
  integer(INT32), parameter, private :: num_integers_max = 10_INT32 ** 5_INT32

  ! variables for this <module>
  integer(INT32), private :: num_integers

  ! arrays for this <module>
  integer(INT32), private :: val_integer(1:num_integers_max)
  integer(INT32), private :: val_GCD    (1:num_integers_max)

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

  subroutine task_C

    ! support variables for this <subroutine>
    integer :: iter_elem
    integer :: iter_trgt

    ! STEP.01
    ! read out the number of target integers
    read(unit=INPUT_UNIT, fmt=*) num_integers

    ! STEP.02
    ! read out the value of the given integers
    read(unit=INPUT_UNIT, fmt=*) val_integer(1:num_integers)

    val_GCD(:) = 0_INT32

    do iter_trgt = 1, num_integers, 1

      do iter_elem = 1, num_integers, 1
        if (iter_elem .ne. iter_trgt) then
          val_GCD(iter_trgt) = calc_GCD(val_GCD(iter_trgt), val_integer(iter_elem))
        end if
      end do

    end do

    ! STEP.03
    ! output the maximum value of the greatest common divisor
    write(unit=OUTPUT_UNIT, fmt='(I0)', advance='yes') maxval(val_GCD(:), dim=1)

    ! STEP.END
    return

  end subroutine task_C

  pure function calc_GCD(target1, target2) result(gcd)

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

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

    ! variables for this <function>
    integer(INT32) :: buffer, buffer1, buffer2

    ! STEP.01
    ! handle the exception
    if (target1 .eq. 0_INT32) then; gcd = target2; end if
    if (target2 .eq. 0_INT32) then; gcd = target1; end if

    ! STEP.02
    ! store the values of the given integers
    buffer1 = target1
    buffer2 = target2

    ! STEP.03
    ! calculate the greatest common divisor
    do while (buffer2 .ne. 0_INT32)
      buffer  = mod(buffer1, buffer2)
      buffer1 = buffer2
      buffer2 = buffer
    end do

    gcd = buffer1

    ! STEP.END
    return

  end function calc_GCD

end module ABC125

program main

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

  ! require all variables to be explicitly declared
  implicit none

  call task_C

end program main


program main
  implicit none
  integer(8) :: i, j, n, result = 0
  integer(8), allocatable :: a(:), l(:), r(:)

  read *, n
  allocate (a(n), l(0:n), r(n+1))
  read *, a

  l(0) = 0
  l(1) = a(1)
  r(n+1) = 0
  r(n) = a(n)
  do i = 2, n
    l(i) = euclid(l(i-1), a(i))
    r(n+1-i) = euclid(r(n+2-i), a(n+1-i))
  end do

  do i = 1, n
    result  = max(euclid(l(i-1), r(i+1)), result)
  end do

  print '(i0)', result

contains
  function euclid(a, b) result (res)
    integer(8) :: a, b, u, v, res
    u = a; v = b
    do while(v /= 0)
      res = mod(u, v)
      u = v
      v = res
    end do
    res = u
  end function
end program


integer n
integer(16),allocatable,dimension(:)::a,l,r
integer ans
read*,n
allocate(a(n),L(0:N),R(1:N+1) )
read*,a

l(1)=a(1)
do i=2,N
  l(i)=getgcd( l(i-1),a(i) )
end do

r(N)=a(N)
do i=N-1,1,-1
  r(i)=getgcd( r(i+1),a(i) )
end do
l(0)=r(2)
r(N+1)=l(N-1)
ans=1
do i=1,N
  ans=max(ans,getgcd(l(i-1),r(i+1) ) )
end do

print"(I0)",ans
contains
pure function getgcd(a,b)
integer(16),intent(in)::a,b
integer(16) getgcd
integer(16) s,t
s=a+b
getgcd=b

t=mod(a+b,b)
do while(t/=0)
  s=getgcd
  getgcd=t
  t=mod(s,getgcd)
end do
end function getgcd
end


program main
  implicit none
  integer N
  integer(8) A(10**5), koyakusu_TOP, dai, sho, koyakusu, yakurow(10**5), yakurank(2)
  integer i, j, t

  read(*, *) N
  read(*, *) (A(i), i = 1,N)

  !yakurow = 10**9
  koyakusu_TOP = 10**9
  do i = 1,N
    koyakusu_TOP = 10**9
    do j = 1,N
      !do j = i+1,N

        dai = max(A(i),A(j))
        sho = min(A(i),A(j))
        koyakusu = yaku(dai,sho)
        if (koyakusu < koyakusu_TOP) then
          koyakusu_TOP = koyakusu
        end if
      !end do
    end do
    yakurow(i) = koyakusu_TOP
  end do
  if (N == 2) then
    write(* ,'(i0)') max(A(1),A(2))
  else
    yakurank = 10**9
    do i = 1,N
      if (yakurank(1) < yakurow(i)) then
        yakurank(2) = yakurank(1)
        yakurank(1) = yakurow(i)
      else if (yakurank(2) < yakurow(i)) then
        yakurank(2) = yakurow(i)
      end if
    end do
    write(* ,'(i0)') yakurank(2)
  end if

  stop
contains

  integer(8) function yaku(dai, sho)
    implicit none
    integer(8) dai, sho, ans, dai2
    do while (mod(dai,sho) /= 0)
      dai2 = mod(dai, sho)
      dai = sho
      sho = dai2
    end do
    yaku = sho
    return
  end function yaku

end program main


program main
Implicit None
	integer(8)::n,p1,p2,p3,x,y,m
	integer(8),allocatable::a(:)

	read*,n
	allocate(a(n))

	read*,a

	if(n == 2)then
		print"(i0)",max(a(1),a(2))
		stop
	endif

	call h1(a,n)

	call euc(a(1),a(2),p1)
	call euc(a(1),a(3),p2)
	call euc(a(3),a(2),p3)

	if(p1 == 1 .and. p2 == 1 .and. p3 == 1)then
		print"(i0)",1
		stop
	endif

	x = 1 ;y = 2
	call ec(a,n,x,y,p1)
	y = 3
	call ec(a,n,x,y,p2)
	x = 2
	call ec(a,n,x,y,p3)

	print"(i0)",max(p1,p2,p3)
end program main

recursive subroutine ec(a,n,p,q,c)
Implicit None
	integer(8)::a(n),n,p,q,c,i,k,t

	k = 0
	call euc(a(p),a(q),c)
	if(c ==1) return
	do i = 1,n
		if(mod(a(i),c) /= 0)then
			if(k == 0) t = i
			k = k+1
			if(k > 1)then
				call ec(a,n,p,t,c)
				return
			endif
		endif
	enddo
end subroutine ec

subroutine euc(a1,b1,c)
implicit None
integer(8)::a1,b1,c,a,b
	a = a1
	b = b1
	c = 1

	do while (a /=0 .and. b /= 0)
		if (a > b)then
			c = b
			a = mod(a,b)
			else
			c = a
			b = mod(b,a)
		endif
	enddo
end subroutine euc

subroutine h1(a,n)
implicit None
	integer(8)::a(n),x
	integer(8)::n,i,ie,i0,i1,i2

	do i = n/2,1,-1
		x = a(i);ie = n + 1; i0 = i; i1 = i0 * 2
		do while(i1 < ie)
			i2 = i1 + 1
			if(i2 < ie .and. a(i1) < a(i2)) i1 = i2
			if(a(i1) > x)then
				a(i0) = a(i1);a(i1) = x
				else
				exit
			endif
			i0 = i1;i1 = i0 *2
		enddo
	enddo
	do ie = n,2,-1
		x = a(ie)
		a(ie) = a(1)
		a(1) = x
		i0 =1;i1 =2
		do while(i1 <ie)
			i2 = i1 +1
			if(i2 < ie .and. a(i1) < a(i2)) i1 = i2
			if(a(i1) > x)then
				a(i0) = a(i1);a(i1) = x
				else
				exit
			endif
			i0 = i1;i1 = i0 *2
		enddo
	enddo
end subroutine h1


program main
Implicit None
	integer(8)::n,p1,p2,p3,x,y
	integer(8),allocatable::a(:)

	read*,n
	allocate(a(n))

	read*,a

	if(n == 2)then
		print"(i0)",max(a(1),a(2))
		stop
	endif

	x = 1 ;y = 2
	call ec(a,n,x,y,p1)
	y = 3
	call ec(a,n,x,y,p2)
	x = 2
	call ec(a,n,x,y,p3)

	print"(i0)",max(p1,p2,p3)
end program main

recursive subroutine ec(a,n,p,q,c)
Implicit None
	integer(8)::a(n),n,p,q,c,i,k,t

	k = 0
	call euc(a(p),a(q),c)
	if(c ==1) return
	do i = 1,n
		if(mod(a(i),c) /= 0)then
			if(k == 0) t = i
			k = k+1
			if(k > 1)then
				call ec(a,n,p,t,c)
				return
			endif
		endif
	enddo
end subroutine ec

subroutine euc(a1,b1,c)
implicit None
integer(8)::a1,b1,c,a,b
	a = a1
	b = b1
	c = 1

	do while (a /=0 .and. b /= 0)
		if (a > b)then
			c = b
			a = mod(a,b)
			else
			c = a
			b = mod(b,a)
		endif
	enddo
end subroutine euc


integer n
integer(16),allocatable,dimension(:)::A
integer(16) gcd,pre,ans
read*,n
allocate(A(n))
read*,A

ans=A(2)
do j=2,N
  ans=getgcd(A(j),ans)
end do
!ansの初期値
pre=A(1)
!preの初期値
do i=2,N-1
  gcd=pre
  do j=i+1,N
    gcd=getgcd(A(j),gcd)
    if(gcd==1)exit
  end do
  ans=max(ans,gcd)
  pre=getgcd(A(i+1),pre)
end do
ans=max(ans,pre)
print"(I0)",ans

contains

pure function getgcd(a,b)
integer(16),intent(in)::a,b
integer(16) getgcd
integer(16) s,t
s=a+b
getgcd=b

t=mod(a+b,b)
do while(t/=0)
  s=getgcd
  getgcd=t
  t=mod(s,getgcd)
end do
end function getgcd
end


integer n
integer(16),allocatable,dimension(:)::A
integer(16) gcd,ans
read*,n
allocate(A(n))
read*,A

ans=1
do i=1,N
  gcd=A(mod(i,N)+1)
  do j=1,N
    if(i==j)cycle
    do
      if(mod(A(j),gcd)==0)exit
      gcd= abs(A(j)-gcd)
    end do
  end do
  ans=max(ans,gcd)
end do
print"(I0)",ans
end


PROGRAM ATCODER

implicit none
integer(8) :: n,i,j,count=1,m=998244353,count1=0,count2=0,s
integer(8),allocatable :: a(:),dp(:,:)

read*, n
allocate(a(n))
read*, a(1:n)
allocate(dp(0:n,0:sum(a)))

!! 全ての通り
do i = 1, n, 1
  count = count*3
end do

dp = 0
dp(0,0) = 1
!! dp(t,r)=最初のt個の整数を3色で塗り分け、赤い整数の和がrであるような場合の数
do i = 0, n-1, 1
  do j = 0, sum(a), 1
    !! i+1番目を他の2色で塗る場合
    dp(i+1,j) = mod(dp(i+1,j)+2*dp(i,j),m)
    !! 赤で塗る場合
    if ( j+a(i+1) <= sum(a) ) then
      dp(i+1,j+a(i+1)) = mod(dp(i+1,j+a(i+1))+dp(i,j),m)
    end if
  end do
end do

do j = 1, 3
  do i = ceiling(real(sum(a))/2.d0,8), sum(a), 1
    count1 = mod(count1+dp(n,i),m)
  end do
end do

dp = 0
dp(0,0) = 1
!! dp(t,r)=最初のt個の整数を2色で塗り分け、赤い整数の和がrであるような場合の数
do i = 0, n-1, 1
  do j = 0, sum(a), 1
    !! i+1番目を他の色で塗る場合
    dp(i+1,j) = mod(dp(i+1,j)+dp(i,j),m)
    !! 赤で塗る場合
    if ( j+a(i+1) <= sum(a) ) then
      dp(i+1,j+a(i+1)) = mod(dp(i+1,j+a(i+1))+dp(i,j),m)
    end if
  end do
end do

if (mod(sum(a),2) == 0) then
  do i = 1, 3, 1
    count2 = mod(count2+dp(n,sum(a)/2),m)
  end do
else
  count2 = 0
end if

print'(i0)', count-count1+count2

END PROGRAM ATCODER

Score : 600 points

Problem Statement

You are given N integers. The i-th integer is a_i.
Find the number, modulo 998244353, of ways to paint each of the integers red, green or blue so that the following condition is satisfied:

Let R, G and B be the sums of the integers painted red, green and blue, respectively. There exists a triangle with positive area whose sides have lengths R, G and B.

Constraints

3 \leq N \leq 300

1 \leq a_i \leq 300(1\leq i\leq N)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_1
:
a_N

Output

Print the number, modulo 998244353, of ways to paint each of the integers red, green or blue so that the condition is satisfied.

Sample Input 1

4
1
1
1
2

Sample Output 1

18

We can only paint the integers so that the lengths of the sides of the triangle will be 1, 2 and 2, and there are 18 such ways.

Sample Input 2

6
1
3
2
3
5
2

Sample Output 2

150

Sample Input 3

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

Sample Output 3

563038556


program answer
  implicit none
  integer :: A, B, C
  read(*,*) A, B, C
  if(B/A>=C) then
     write(*,*) C
  else
     write(*,*) B/A
  end if

  stop
 end program answer

Score : 100 points

Problem Statement

Takahashi likes the sound when he buys a drink from a vending machine.

That sound can be heard by spending A yen (the currency of Japan) each time.

Takahashi has B yen. He will hear the sound as many times as he can with that money, but at most C times, as he would be satisfied at that time.

How many times will he hear the sound?

Constraints

All values in input are integers.

1 \leq A, B, C \leq 100

Input

Input is given from Standard Input in the following format:

A B C

Output

Print the number of times Takahashi will hear his favorite sound.

Sample Input 1

2 11 4

Sample Output 1

4

Since he has not less than 8 yen, he will hear the sound four times and be satisfied.

Sample Input 2

3 9 5

Sample Output 2

3

He may not be able to be satisfied.

Sample Input 3

100 1 10

Sample Output 3

0


program main
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: a,b,c

    read*, a,b,c

    print'(i0)', min(c,b/a)
end program main


integer :: a,b,c
read*,a,b,c
print*,min((b/a),c)
end


integer a,b,c
read*, a,b,c
print*, min(b/a,c)
end


program aaa
implicit none

integer :: a, b, c

read*, a, b, c

write(*,'(i0)') min(b/a,c)

end program


program main
  implicit none
  integer :: a, b, c

  read *, a, b, c
  print '(i0)', min(c, int(b / a))
end


integer a,b,c
read*,a,b,c
print"(I0)",min(a/b,c)
end


program ho
	integer :: a,b,c,f

    read(*,*) a,b,c
    if((b/a).gt.c) then
    	write(*,'(i0)') c
    else
    	write(*,'(i0)') b/a
    endif

end program ho


read*,i,j,k
j = j/i
write(*,'(I0)')min(j,k)
end


PROGRAM ATCODER

implicit none
integer :: a,b,c

read(*,*) a,b,c    !ここが入力欄

! ここに出力結果
write(*,'(i0)') min(b/a,c)

END PROGRAM ATCODER


program main
  implicit none
  integer :: n
  integer :: A,B,C
  integer :: out

  read(*,*) A,B,C
  out = min( B/A,C )
  write(*,'(i0)') out
  stop
end program main


program main
   implicit none
   integer::a,b,c
   integer::i,j,k

  ! integer,allocatable,dimension(:)::c
   read(*,*)a,b,c
   write(*,*)min(c,b/a)
end program main


program main
  implicit none

  integer :: n, i
  integer(16) :: inf
  integer(16), allocatable :: h(:), dp(:)

  read(*,*) n
  allocate(h(n))
  allocate(dp(n))
  read(*,*) h

  inf = 1000000000000_16
  do i = 1, n
     dp(i) = inf
  end do

  dp(1) = 0
  dp(2) = abs(h(2) - h(1))

  do i = 3, n
     dp(i) = min(dp(i), dp(i-1)+abs((h(i) - h(i-1))))
     dp(i) = min(dp(i), dp(i-2)+abs((h(i) - h(i-2))))
  end do

  write(*,*) dp(n)

end program main

Score : 100 points

Problem Statement

There are N stones, numbered 1, 2, \ldots, N.
For each i (1 \leq i \leq N), the height of Stone i is h_i.

There is a frog who is initially on Stone 1.
He will repeat the following action some number of times to reach Stone N:

If the frog is currently on Stone i, jump to Stone i + 1 or Stone i + 2. Here, a cost of |h_i - h_j| is incurred, where j is the stone to land on.

Find the minimum possible total cost incurred before the frog reaches Stone N.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq h_i \leq 10^4

Input

Input is given from Standard Input in the following format:

N
h_1 h_2 \ldots h_N

Output

Print the minimum possible total cost incurred.

Sample Input 1

4
10 30 40 20

Sample Output 1

30

If we follow the path 1 → 2 → 4, the total cost incurred would be |10 - 30| + |30 - 20| = 30.

Sample Input 2

2
10 10

Sample Output 2

0

If we follow the path 1 → 2, the total cost incurred would be |10 - 10| = 0.

Sample Input 3

6
30 10 60 10 60 50

Sample Output 3

40

If we follow the path 1 → 3 → 5 → 6, the total cost incurred would be |30 - 60| + |60 - 60| + |60 - 50| = 40.


program EDPCA
    implicit none
    integer(8)::N,i
    integer(8),allocatable,dimension(:)::H,dp
    read(5,*)N
    allocate(H(N))
    allocate(dp(N))
    read(5,*)(H(i),i=1,N)
    dp=0

    do i=1,N
        if(i==2)then
            dp(i)=abs(H(i)-H(i-1))
        else if(i>2)then
            dp(i)=min(dp(i-1)+abs(H(i)-H(i-1)),dp(i-2)+abs(H(i)-H(i-2)))
        end if
    end do

    print'(i0)',dp(N)
end program EDPCA


program eee
implicit none

integer(8) :: n, w, i, j, k
integer(8),allocatable,dimension(:) :: ww, vv
integer(8),allocatable,dimension(:,:) :: dp
! 初期値の設定
read*, n, w
allocate(ww(n),vv(n))
do i=1, n
	read*, ww(i), vv(i)
end do
k=sum(vv)
allocate(dp(0:n,0:k))
dp=w*n+1
dp(0,0)=0
! end 初期値の設定

do i=1,n
	do j=0,k
		if(j>=vv(i)) then					!追加可能パターン
        	dp(i,j)=dp(i-1,j-vv(i))+ww(i)	!i-1の場合の、価値j-v(i)から参照
        end if
        dp(i,j)=min(dp(i,j),dp(i-1,j))		!追加したものとしていないものの比較を行う
    end do
end do

do j=k,0,-1
    if(dp(n,j)<=w) then
    	write(*,'(i0)') j
        return
    end if
end do

end program


program aaa

integer :: n, i
integer,allocatable,dimension(:) :: h, dp

read*, n
allocate(h(n))
allocate(dp(n))
read*, h

dp=0
dp(1)=0
dp(2)=abs(h(2)-h(1))

if(n==2) then
write(*,'(i0)') dp(2)
return
end if

do i=3, n
dp(i)=min(dp(i-2)+abs(h(i-2)-h(i)),dp(i-1)+abs(h(i-1)-h(i)))
end do

write(*,'(i0)') dp(n)

end program


program flog1
    implicit none
    integer::n,i,dis1,dis2
    integer,allocatable::a(:),b(:)
    read*,n
    allocate(a(n),b(n))
    a = 0
    read*,b
    a(1) = 0
    a(2) = abs(b(2) - b(1))
    do i = 2,n
        dis1 = a(i-1) + abs(b(i)-b(i-1))
        dis2 = a(i-2) + abs(b(i)-b(i-2))
        a(i) = min(dis1,dis2)
    end do
    print"(i0)",a(n)
    stop
end program flog1


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

read(5,*)n
allocate(dp(n),h(n))
read(5,*)(h(i),i=1,n)

dp(1)=0
dp(2:n)=100000

do j=2,n
dp(j)=min(dp(j),dp(j-1)+abs(h(j)-h(j-1)))

if(j>2) then
dp(j)=min(dp(j),dp(j-2)+abs(h(j)-h(j-2)))
endif
enddo

write(6,*) dp(n)

deallocate(dp,h)
end


integer N
integer,allocatable,dimension(:)::H
integer DP(10**5)
read*,N
allocate(H(N))
read*,H

DP(1)=0
DP(2)=abs(H(2)-H(1))
if(N==2)then
  print"(i0)",DP(N)
  STOP
endif
do i=3,N
  DP(i)=min(DP(i-1)+abs( H(i)-H(i-1) ),DP(i-2)+abs( H(i)-H(i-2) ) )
end do
print"(i0)",DP(N)
end


PROGRAM ATCODER

implicit none
integer :: n, i
integer, allocatable :: h(:), dp(:)

read(*,*) n
allocate(h(n), dp(n))
read(*,*) (h(i), i=1,n)

dp = 10**6
dp(1) = 0
dp(2) = dp(1) + abs(h(1)-h(2))

do i = 3, n
	call chmin(dp(i), dp(i-1)+abs(h(i)-h(i-1)))
    call chmin(dp(i), dp(i-2)+abs(h(i)-h(i-2)))
end do

write(*,'(i0)') dp(n)

END PROGRAM ATCODER

subroutine chmin(dp,x)
implicit none
integer :: dp, x

if (dp > x) then
	dp = x
	return
end if

end subroutine


PROGRAM ATCODER

implicit none
integer :: n, i
integer, allocatable :: h(:), dp(:)

read(*,*) n
allocate(h(n), dp(n))
read(*,*) (h(i), i=1,n)

dp = 100000
dp(1) = 0
dp(2) = dp(1) + abs(h(1)-h(2))

do i = 3, n
	call chmin(dp(i), dp(i-1)+abs(h(i)-h(i-1)))
    call chmin(dp(i), dp(i-2)+abs(h(i)-h(i-2)))
end do

write(*,'(i0)') dp(n)

END PROGRAM ATCODER

subroutine chmin(dp,x)
implicit none
integer :: dp, x

if (dp > x) then
	dp = x
	return
end if

return

end subroutine


program atcoder
    implicit none
    integer :: i,n
    integer,allocatable,dimension(:) :: a,dp
    read*,n
    allocate(a(0:n))
    allocate(dp(0:n+3))
    read(*,*)(a(i),i=1,n)
    dp = 1000000007
    dp(1) = 0
    do i = 1,n
        dp(i+1) = min( dp(i+1) , dp(i)+abs(a(i+1)-a(i)))
        dp(i+2) = min( dp(i+2) , dp(i)+abs(a(i+2)-a(i)))
    end do
    write(*,'(I0)')dp(n)
    stop
!---------------------------------------
contains

end program atcoder


program Frog_2
    implicit none
    integer(4):: n,k
    integer(4):: i,j
    integer(4),parameter:: inf=200000*10000+1
    integer(4), allocatable:: h(:), dp(:)

    read*, n,k
    allocate(h(n), dp(n))

    read*, h(:)
    dp(:) = inf
    dp(1) = 0
    do i=1,n
        do j=1,k
            if (i+j > n) cycle
            dp(i+j) = min(dp(i+j), dp(i) + abs(h(i)-h(i+j)))
        end do
    end do

    print*, dp(n)

end program Frog_2

Score : 100 points

Problem Statement

There are N stones, numbered 1, 2, \ldots, N.
For each i (1 \leq i \leq N), the height of Stone i is h_i.

There is a frog who is initially on Stone 1.
He will repeat the following action some number of times to reach Stone N:

If the frog is currently on Stone i, jump to one of the following: Stone i + 1, i + 2, \ldots, i + K. Here, a cost of |h_i - h_j| is incurred, where j is the stone to land on.

Find the minimum possible total cost incurred before the frog reaches Stone N.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq K \leq 100

1 \leq h_i \leq 10^4

Input

Input is given from Standard Input in the following format:

N K
h_1 h_2 \ldots h_N

Output

Print the minimum possible total cost incurred.

Sample Input 1

5 3
10 30 40 50 20

Sample Output 1

30

If we follow the path 1 → 2 → 5, the total cost incurred would be |10 - 30| + |30 - 20| = 30.

Sample Input 2

3 1
10 20 10

Sample Output 2

20

If we follow the path 1 → 2 → 3, the total cost incurred would be |10 - 20| + |20 - 10| = 20.

Sample Input 3

2 100
10 10

Sample Output 3

0

If we follow the path 1 → 2, the total cost incurred would be |10 - 10| = 0.

Sample Input 4

10 4
40 10 20 70 80 10 20 70 80 60

Sample Output 4

40

If we follow the path 1 → 4 → 8 → 10, the total cost incurred would be |40 - 70| + |70 - 70| + |70 - 60| = 40.


!ref
!https://qiita.com/drken/items/dc53c683d6de8aeacf5a
      PROGRAM Frog2
      IMPLICIT NONE
      integer(16) :: n,k
      integer(16),allocatable :: h(:),dp(:)
      integer(16) :: i,j

      read*, n,k
      allocate( h(1:n) )
      read*, h(:)

      allocate( dp(1:n) )
      dp(:) = 10**9
      dp(1) = 0

      do i = 1,n
        do j = 1,k
          if( i+j>n ) cycle
          dp(i+j) = min( dp(i+j), dp(i)+abs( h(i+j)-h(i) ) )
        end do
      end do

      print*,dp(n)

      END PROGRAM


!ref
!https://qiita.com/drken/items/dc53c683d6de8aeacf5a
      PROGRAM Frog2
      IMPLICIT NONE
      integer(16) :: n,k
      integer(16),allocatable :: h(:),dp(:)
      integer(16) :: i,j

      read*, n,k
      allocate( h(1:n) )
      read*, h(:)

      !set dp table longer
      allocate( dp(1:n+k) )
      dp(:) = 10**5
      dp(1) = 0

      do i = 1,n
        do j = 1,k
          if( i+j>n ) cycle
          dp(i+j) = min( dp(i+j), dp(i)+abs( h(i+j)-h(i) ) )
        end do
      end do

      print*,dp(n)

      END PROGRAM


integer N,K
integer,allocatable,dimension(:)::H
integer DP(10**5)
read*,N,K
allocate(H(N))
read*,H

DP=huge(DP)/2
DP(1)=0
DP(2)=abs(H(2)-H(1))
if(N==2)then
  print"(i0)",DP(N)
  STOP
endif
do i=3,N
  do j=1,min(K,i-1)
    DP(i)=min(DP(i-j)+abs( H(i)-H(i-j) ),DP(i))
  end do
end do
print"(i0)",DP(N)
end


PROGRAM ATCODER

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

read(*,*) n, k
allocate(h(n), dp(n))
read(*,*) (h(i), i=1,n)

dp = 10**9
dp(1) = 0

do i = 2, n
	do j = 1, min(k,i-1)
		call chmin(dp(i), dp(i-j)+abs(h(i)-h(i-j)))
    end do
end do

write(*,'(i0)') dp(n)

END PROGRAM ATCODER

subroutine chmin(dp,x)
implicit none
integer :: dp, x

if (dp > x) then
	dp = x
	return
end if

end subroutine


program main
  implicit none
  integer :: i
  integer,allocatable,dimension(:) :: c, h
  integer :: N, K
  read(*, *), N, K
  allocate(h(N), c(N))
  read(*, *), h
  c(1) = 0
  c(2) = abs(h(1) - h(2))
  do i = 3, N
    c(i) = minval([(c(i-k)+abs(h(i-k)-h(i)), k=1,min(i,K))])
  end do
  print*,(c(N))
end program


program main
  implicit none

  integer(8), allocatable :: wv(:,:), dp(:,:)
  integer(8) :: n, wmax, i, j

  read(*,*) n, wmax

  allocate(wv(2,n))
  allocate(dp(n,wmax))

  read(*,*) wv

  do i = 1, n
     do j = 1, wmax
        dp(i,j) = -1
     end do
  end do

  do j = 1, wmax
     if(j - wv(1,1) >= 0) then
        dp(1,j) = wv(2,1)
     else
        dp(1,j) = 0
     end if
  end do

  do j = 1, wmax
     do i = 2, n
        if(j - wv(1,i) >= 0) then
           if(j - wv(1,i) == 0) then
              dp(i,j) = max(dp(i,j), 0 + wv(2,i))
           else
              dp(i,j) = max(dp(i,j), dp(i-1,j-wv(1,i)) + wv(2,i))
           end if
        end if

        dp(i,j) = max(dp(i,j), dp(i-1, j))
     end do
  end do

  write(*,*) dp(n,wmax)

end program main

Score : 100 points

Problem Statement

There are N items, numbered 1, 2, \ldots, N.
For each i (1 \leq i \leq N), Item i has a weight of w_i and a value of v_i.

Taro has decided to choose some of the N items and carry them home in a knapsack.
The capacity of the knapsack is W, which means that the sum of the weights of items taken must be at most W.

Find the maximum possible sum of the values of items that Taro takes home.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq W \leq 10^5

1 \leq w_i \leq W

1 \leq v_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N W
w_1 v_1
w_2 v_2
:
w_N v_N

Output

Print the maximum possible sum of the values of items that Taro takes home.

Sample Input 1

3 8
3 30
4 50
5 60

Sample Output 1

90

Items 1 and 3 should be taken.
Then, the sum of the weights is 3 + 5 = 8, and the sum of the values is 30 + 60 = 90.

Sample Input 2

5 5
1 1000000000
1 1000000000
1 1000000000
1 1000000000
1 1000000000

Sample Output 2

5000000000

The answer may not fit into a 32-bit integer type.

Sample Input 3

6 15
6 5
5 6
6 4
6 6
3 5
7 2

Sample Output 3

17

Items 2, 4 and 5 should be taken.
Then, the sum of the weights is 5 + 6 + 3 = 14, and the sum of the values is 6 + 6 + 5 = 17.


program Knapsack_1
    implicit none
    integer(4):: n,wn
    integer(8), allocatable:: w(:), v(:)
    integer(8), allocatable:: dp(:), ndp(:)
    integer(4):: i, j

    read*, n, wn
    allocate(w(n), v(n), dp(0:wn), ndp(0:wn))
    do i=1, n
        read*, w(i), v(i)
    end do

    dp(:) = 0
    ndp(:) = 0

    do i=1,n
        do j=1, wn
            if (j < w(i)) then
                ndp(j) = dp(j)
            else
                ndp(j) = max(dp(j), dp(j-w(i)) + v(i))
            end if
        end do
        dp(:) = ndp(:)
    end do

    print*, dp(n)
end program Knapsack_1


program ddd
implicit none

integer(8) :: n, w, i, j
integer(8),allocatable,dimension(:) :: g, v, dp

read*, n, w
allocate(g(n))
allocate(v(n))
allocate(dp(0:w))
do i=1,n
read*, g(i), v(i)
end do
dp=0

do j=1, n
 do i=0,w
 if(i>=g(j)) then
 dp(i)=max(dp(i),dp(i-g(j))+v(j))
 end if
 end do
end do

write(*,'(i0)') dp(w)

end program


program ddd
implicit none

integer(8) :: n, w, i, j
integer(8),allocatable,dimension(:) :: g, v, dp

read*, n, w
allocate(g(n))
allocate(v(n))
allocate(dp(0:w))
do i=1,n
read*, g(i), v(i)
end do
dp=0

do j=1, n
 do i=w,0,-1
 if(i>=g(j)) then
 dp(i)=max(dp(i),dp(i-g(j))+v(j))
 end if
 end do
end do

write(*,'(i0)') dp(w)

end program


implicit none
integer(8),allocatable,dimension(:)::w,v
integer(8),allocatable,dimension(:,:)::dp
integer(8) n,w_sum,i,j,k,tmp

read(5,*) n,w_sum
allocate(w(n),v(n))
allocate(dp(0:n,0:w_sum))
dp(:,:)=0

do i=1,n
read(5,*) w(i),v(i)
enddo

do j=0,n-1
do k=0,w_sum
if(k-w(j).ge.0)then
tmp=w(j)
dp(j+1,k)=max(dp(j+1,k),dp(j,k-tmp)+v(j))
else
dp(j+1,k)=dp(j,k)
endif

enddo
enddo

write(6,*)dp(n,w_sum)

deallocate(w,v,dp)
end


program main
integer N,sup_w!supはsupremum(上限)の略
integer,allocatable,dimension(:)::w,v
integer(16),allocatable,dimension(:,:)::DP
read*,N,sup_w
allocate(w(n),v(n))
allocate(DP(0:N,0:sup_w))
do i=1,n
  read*,w(i),v(i)
end do

DP(:,:)=0
do i=1,N
  do j=0,sup_w
    if(j-w(i)>=0)DP(i,j)=DP(i-1,j-w(i))+v(i)
    DP(i,j)=max(DP(i,j),DP(i-1,j))
  end do
end do

print"(i0)",DP(N,sup_w)
end program main


program main
integer N,sup_w!supはsupremum(上限)の略
integer,allocatable,dimension(:)::w,v
integer(16),allocatable,dimension(:,:)::DP
read*,N,sup_w
allocate(w(n),v(n))
allocate(DP(0:N,0:sup_w))
do i=1,n
  read*,w(i),v(i)
end do

DP(:,:)=0
do i=1,N
  do j=0,sup_w
    if(j-w(i)>=0)then
      DP(i,j)=DP(i-1,j-w(i))+v(i)
    else
      DP(i,j)=DP(i-1,j)
    end if
  end do
end do

print"(i0)",DP(N,sup_w)
end program main


program main
integer N,sup_w!supはsupremum(上限)の略
integer,allocatable,dimension(:)::w,v
integer(16),allocatable,dimension(:,:)::DP
read*,N,sup_w
allocate(w(n),v(n))
allocate(DP(0:N,0:sup_w))
do i=1,n
  read*,w(i),v(i)
end do

DP(:,:)=0
do i=1,N
  do j=0,sup_w
    if(j-w(i)>=0)then
      DP(i,j)=max(DP(i,j),DP(i-1,j-w(i))+v(i))
    else
      DP(i,j)=DP(i-1,j)
    end if
  end do
end do

print"(i0)",DP(N,sup_w)
end program main


PROGRAM ATCODER

implicit none
integer(16) :: n, w_max, i, j, sum_w
integer(16), allocatable :: w(:), v(:), dp(:,:)

read(*,*) n, w_max
allocate(dp(0:n,0:w_max), w(0:n), v(0:n))

dp = 0

do i = 0, n-1
	read(*,*) w(i), v(i)
end do

do i = 0, n-1
	do sum_w = 0, w_max
    	if (sum_w-w(i) >= 0) then
        	call chmax(dp(i+1,sum_w), dp(i,sum_w-w(i))+v(i))
        else
			call chmax(dp(i+1,sum_w), dp(i,sum_w))
        end if
    end do
end do

write(*,'(i0)') maxval(dp)

END PROGRAM ATCODER

subroutine chmax(dp,x)
implicit none
integer(16) :: dp, x

if (dp < x) then
	dp = x
	return
end if

end subroutine


PROGRAM ATCODER

implicit none
integer(16) :: n, w_max, i, j, sum_w
integer(16), allocatable :: w(:), v(:), dp(:,:)

read(*,*) n, w_max
allocate(dp(0:n,0:w_max), w(0:n), v(0:n))

dp = 0

do i = 0, n-1
	read(*,*) w(i), v(i)
end do

do i = 0, n-1
	do sum_w = 0, w_max
    	if (sum_w-w(i) >= 0) then
        	call chmax(dp(i+1,sum_w), dp(i,sum_w-w(i))+v(i))
        end if
		call chmax(dp(i+1,sum_w), dp(i,sum_w))
    end do
end do

write(*,'(i0)') maxval(dp)

END PROGRAM ATCODER

subroutine chmax(dp,x)
implicit none
integer(16) :: dp, x

if (dp < x) then
	dp = x
	return
end if

end subroutine


PROGRAM ATCODER

implicit none
integer(16) :: n, w_max, i, j, sum_w
integer(16), allocatable :: w(:), v(:), dp(:,:)

read(*,*) n, w_max
allocate(dp(n+1,w_max+1), w(n), v(n))

dp = 0

do i = 1, n
	read(*,*) w(i), v(i)
end do

do i = 1, n
	do sum_w = 1, w_max+1
    	if (sum_w-w(i) > 0) then
        	call chmax(dp(i+1,sum_w), dp(i,sum_w-w(i))+v(i))
        end if
        call chmax(dp(i+1,sum_w), dp(i,sum_w))
    end do
end do

write(*,'(i0)') maxval(dp)

END PROGRAM ATCODER

subroutine chmax(dp,x)
implicit none
integer :: dp, x

if (dp < x) then
	dp = x
	return
end if

end subroutine


program main
  implicit none
  integer :: i, j, Nmax, Wmax
  integer(16),allocatable,dimension(:) :: v, DP
  integer(16),allocatable,dimension(:) :: w
  read*, Nmax, Wmax
  allocate(v(Nmax), w(Nmax), DP(0:Wmax))

  do i = 1, Nmax
    read*, w(i), v(i)
  enddo

  DP = 0
  do i = 1, Nmax
    do j = Wmax, 0, -1
      if(j >= w(i))then
        DP(j) = max(DP(j - w(i)) + v(i), DP(j))
      endif
    enddo
  enddo
  print*, DP(Wmax)
end program


program EDPCF
    implicit none
    integer(8)::i,j,slen,tlen,cursor,dp(3100,3100)
    character(3000)::s,t,ans
    read*,s,t
    slen=len_trim(s)
    tlen=len_trim(t)

    dp=0

    do i=1,slen
        do j=1,tlen
            if(s(i:i)==t(j:j))then
                dp(j+1,i+1)=max(dp(j,i)+1,dp(j,i+1),dp(j+1,i))
            else
                dp(j+1,i+1)=max(dp(j,i+1),dp(j+1,i))
            end if
        end do
    end do

    i=slen+1
    j=tlen+1
    cursor=dp(tlen+1,slen+1)

    if(dp(tlen+1,slen+1)>0)then
        do
            if(i<=0.or.j<=0)exit
            if(dp(j,i)==dp(j,i-1))then
                i=i-1
            else if(dp(j,i)==dp(j-1,i))then
                j=j-1
            else
                ans(cursor:cursor)=s(i-1:i-1)
                cursor=cursor-1
                i=i-1
                j=j-1
            end if
        end do

        print'(A)',ans(1:dp(tlen+1,slen+1))
    else
        print'(A)',""
    end if
end program EDPCF

Score : 100 points

Problem Statement

You are given strings s and t.
Find one longest string that is a subsequence of both s and t.

Notes

A subsequence of a string x is the string obtained by removing zero or more characters from x and concatenating the remaining characters without changing the order.

Constraints

s and t are strings consisting of lowercase English letters.

1 \leq |s|, |t| \leq 3000

Input

Input is given from Standard Input in the following format:

s
t

Output

Print one longest string that is a subsequence of both s and t.
If there are multiple such strings, any of them will be accepted.

Sample Input 1

axyb
abyxb

Sample Output 1

axb

The answer is axb or ayb; either will be accepted.

Sample Input 2

aa
xayaz

Sample Output 2

aa

Sample Input 3

a
z

Sample Output 3

The answer is (an empty string).

Sample Input 4

abracadabra
avadakedavra

Sample Output 4

aaadara


program main
character(3000) s,t
integer,allocatable,dimension(:,:)::DP
character(3000) ans
read*,s,t
allocate( DP(0:len_trim(s),0:len_trim(t)) )

!DP(i,j)にはSのi文字目までの部分とTのj文字目までの部分のLCSの長さが格納されています
!初期条件としてDP(0,:)=0及びDP(:,0)=0が既知です。
!一番左上に(0,0)を据えて右下に展開していくとします。
!このときある(i,j)について
!一つ上でかつ一つ左の値と一つ上の値と一つ左の値を知っていたらば(i,j)を求めることが可能です。

DP=0
do i= 1, len_trim(s)
  do j= 1, len_trim(t)
    if(s(i:i)==t(j:j))then
      DP(i,j)=DP(i-1,j-1)+1
    else
      DP(i,j)=max(DP(i-1,j),DP(i,j-1))
    endif
  end do
end do

!一番DPの値が高いところ(DP(len_trim(s),len_trim(t)))から1段ずつ降っていきます
ans=""
i = len_trim(s)
j = len_trim(t)
do
  if( DP(i,j)==DP(i-1,j) )then
    i=i-1
  else if( DP(i,j)==DP(i,j-1) )then
    j=j-1
  else
    ans = s(i:i)// trim(ans)
    i=i-1
    j=j-1
  end if
  if(DP(i,j)==0)exit
end do

print"(A)",trim(ans)
end program main


program main
character(3000) s,t
integer,allocatable,dimension(:,:)::DP
character(3000) ans
read*,s,t
allocate( DP(0:len_trim(s),0:len_trim(t)) )

!DP(i,j)にはSのi文字目までの部分とTのj文字目までの部分のLCSの長さが格納されています
!初期条件としてDP(0,:)=0及びDP(:,0)=0が既知です。
!一番左上に(0,0)を据えて右下に展開していくとします。
!このときある(i,j)について一つ上の値と一つ左の値を知っていたらば(i,j)を求めることが可能です。

DP=0
do i= 1, len_trim(s)
  do j= 1, len_trim(t)
    DP(i,j)=max(DP(i-1,j),DP(i,j-1)) + merge(1,0,s(i:i)==t(j:j) )
  end do
end do

!一番DPの値が高いところ(DP(len_trim(s),len_trim(t)))から1段ずつ降っていきます
ans=""
i = len_trim(s)
j = len_trim(t)
do
  if( DP(i,j)==DP(i-1,j) )then
    i=i-1
  else if( DP(i,j)==DP(i,j-1) )then
    j=j-1
  else
    ans = s(i:i)// trim(ans)
    i=i-1
    j=j-1
  end if
  if(DP(i,j)==0)exit
end do

print"(A)",trim(ans)
end program main


program main
character(3000) s,t
integer,allocatable,dimension(:,:)::DP
character(3000) ans
read*,s,t
allocate( DP(0:len_trim(s),0:len_trim(t)) )

!DP(i,j)にはSのi文字目までの部分とTのj文字目までの部分のLCSの長さが格納されています
!初期条件としてDP(0,:)=0及びDP(:,0)=0が既知です。
!一番左上に(0,0)を据えて右下に展開していくとします。
!このときある(i,j)について一つ上の値と一つ左の値を知っていたらば(i,j)を求めることが可能です。

DP=0
do i= 1, len_trim(s)
  do j= 1, len_trim(t)
    DP(i,j)=max(DP(i-1,j),DP(i,j-1)) + merge(1,0,s(i:i)==t(j:j) )
  end do
end do

!一番DPの値が高いところ(DP(len_trim(s),len_trim(t)))から1段ずつ降っていきます
ans=""
i = len_trim(s)
j = len_trim(t)
do
  if( DP(i,j)==DP(i-1,j) )then
    i=i-1
  else if( DP(i,j)==DP(i,j-1) )then
    j=j-1
  else
    ans = s(i:i)// ans
    i=i-1
    j=j-1
  end if
  if(DP(i,j)==0)exit
end do

print"(A)",trim(ans)
end program main


program main
character(3000) s,t
integer,allocatable,dimension(:,:)::DP
character(3000) ans
read*,s,t
allocate( DP(0:len_trim(s),0:len_trim(t)) )

!DP(i,j)にはSのi文字目までの部分とTのj文字目までの部分のLCSの長さが格納されています
!初期条件としてDP(0,:)=0及びDP(:,0)=0が既知です。
!一番左上に(0,0)を据えて右下に展開していくとします。
!このときある(i,j)について一つ上の値と一つ左の値を知っていたらば(i,j)を求めることが可能です。

DP=0
do i= 1, len_trim(s)
  do j= 1, len_trim(t)
    DP(i,j)=max(DP(i-1,j),DP(i,j-1)) + merge(1,0,s(i:i)==t(j:j) )
  end do
end do

!一番DPの値が高いところ(DP(len_trim(s),len_trim(t)))から1段ずつ降っていきます
ans=""
i = len_trim(s)
j = len_trim(t)
do
  if( DP(i,j)==DP(i-1,j) )then
    i=i-1
  else if( DP(i,j)==DP(i,j-1) )then
    j=j-1
  else
    if(DP(i,j)==DP(i-1,j)+1)then
      ans=s(i:i)//ans
      i=i-1
    else !(DP(i,j)==DP(i,j-1)+1)のとき
      ans=t(j:j)//ans
      j=j-1
    end if
  end if
  if(i+j==0)exit
end do

print"(A)",trim(ans)
end program main


program main
character(3000) s,t
integer,allocatable,dimension(:,:)::DP
character(3000) ans
read*,s,t
allocate( DP(0:len_trim(s),0:len_trim(t)) )

!DP(i,j)にはSのi文字目までの部分とTのj文字目までの部分のLCSの長さが格納されています
!初期条件としてDP(0,:)=0及びDP(:,0)=0が既知です。
!一番左上に(0,0)を据えて右下に展開していくとします。
!このときある(i,j)について一つ上の値と一つ左の値を知っていたらば(i,j)を求めることが可能です。

DP=0
do i= 1, len_trim(s)
  do j= 1, len_trim(t)
    DP(i,j)=max(DP(i-1,j),DP(i,j-1)) + merge(1,0,s(i:i)==t(j:j) )
  end do
end do

!一番DPの値が高いところ(DP(len_trim(s),len_trim(t)))から1段ずつ降っていきます
ans=""
i = len_trim(s)
j = len_trim(t)
do
  if( DP(i,j)==DP(i-1,j) )then
    i=i-1
  else if( DP(i,j)==DP(i,j-1) )then
    j=j-1
  else
    ans = s(i:i)// ans
    i=i-1
    j=j-1
  end if
  if(i+j==0)exit
end do

print"(A)",trim(ans)
end program main


program main
	implicit none
    integer::d
    read(*,*) d
    if(d==25)then
    	write(*,*) 'Christmas'
    else if(d==24)then
    	write(*,*) 'Christmas Eve'
    else if(d==23)then
    	write(*,*) 'Christmas Eve Eve'
    else
    	write(*,*) 'Christmas Eve Eve Eve'
    end if
    stop
end program main

Score : 100 points

Problem Statement

In some other world, today is December D-th.

Write a program that prints Christmas if D = 25, Christmas Eve if D = 24, Christmas Eve Eve if D = 23 and Christmas Eve Eve Eve if D = 22.

Constraints

22 \leq D \leq 25

D is an integer.

Input

Input is given from Standard Input in the following format:

D

Output

Print the specified string (case-sensitive).

Sample Input 1

25

Sample Output 1

Christmas

Sample Input 2

22

Sample Output 2

Christmas Eve Eve Eve

Be sure to print spaces between the words.


program prob48
    implicit none
    integer :: n, i
    read(*,*) n
    write(*,'(a)',advance='no') "Christmas"
    do i = 1, 25-n
        write(*,'(a)',advance='no') " Eve"
    end do

    stop
contains
end program prob48


program name
    implicit none
    integer(4):: d

    read*, d
    if (d == 25) then
        print*, 'Christmas'
    else if (d == 24) then
        print*, 'Christmas Eve'
    else if (d == 23) then
        print*, 'Christmas Eve Eve'
    else if (d == 22) then
        print*, 'Christmas Eve Eve Eve'
    end if
end program name


character(21) :: s = 'Christmas Eve Eve Eve'
integer :: l,d

read*,d

if( d==22 ) l = 21
if( d==23 ) l = 21-4
if( d==24 ) l = 21-8
if( d==25 ) l = 21-12

print*,s(1:l)
end


integer a
read*, a
if(a==25) then
	print*, "Christmas"
else if(a==24) then
	print*, "Christmas Eve"
else if(a==23) then
	print*, "Christmas Eve Eve"
else
	print*, "Christmas Eve Eve Eve"
end if
end


program aaa
implicit none

integer :: d

read*, d

if(d==25) then
write(*,*) 'Christmas'
else if (d==24) then
write(*,*) 'Christmas Eve'
else if (d==23) then
write(*,*) 'Christmas Eve Eve'
else if (d==22) then
write(*,*) 'Christmas Eve Eve Eve'
end if

end program


program christmas_eve_eve_eve
  implicit none
  integer :: d
  read(*,*) d
  write(*,'(a)',advance='no') "Christmas"
  d = 25-d
  do while (d > 0)
    write(*,'(a)',advance='no') " Eve"
    d = d-1
  end do
  write(*,*)
end program christmas_eve_eve_eve


integer N
character(100) m
read*,N
m="Christmas"
do while (25>N)

m=trim(m)//" "//"Eve"
N=N+1
end do
print"(A)",trim(m)
end


implicit none
integer:: n, i, j, a, b, count_1, count_2, count_3
integer,allocatable,dimension(:):: p

read(5,*) n

if (n == 25) then
    write(6,*) "Christmas"
else if (n == 24) then
    write(6,*) "Christmas Eve"
else if (n == 23) then
    write(6,*) "Christmas Eve Eve"
else
    write(6,*) "Christmas Eve Eve Eve"
endif

end


program atcoder
    implicit none
    integer :: x
    character(len=4) :: c = ' Eve';
    read *,x
    write(*,'(a)')'Christmas'//repeat(c,25-x)
    stop
!----------------------functions--------------------------
contains

end program atcoder


program main
implicit None
integer::n,k,t,i,p
integer,allocatable::h(:)
	read *,n,k
	allocate(h(n))

	read *,h

	call hsort(h,n)

	do i = 1,n-k+1
		p = h(i+k-1) - h(i)
		if(t > p .or. i == 1)then
			t = p
		endif
	enddo

	print 101,t

101 format(i0)
end program main

subroutine makeheap(a,n)
Implicit None
	integer::n,i,c1,c2,p,t,m
	integer::a(n)

	do i = n/2,1,-1
		p = i; c1 = i * 2
		do while (c1 <= n)
			c2 = c1 + 1
			if(c2 <= n .and. a(c2) > a(c1)) c1 = c2
			if(a(p) < a(c1))then
				t = a(p)
				a(p) = a(c1)
				a(c1) = t
				p = c1
				c1 = p*2
				else
					exit
			endif
		enddo
	enddo

end subroutine makeheap

subroutine hsort(a,n)
Implicit None
	integer::n,t,i,p,c1,c2,j
	integer::a(n)

	call makeheap(a,n)

	do i = n,2,-1
		t = a(i)
		a(i) = a(1)
		a(1) = t

		p = 1
		do
			c1 = p*2
			if (c1 >= i) exit

			c2 = c1 +1
			if(c2 < i .and. a(c2) > a(c1))then
					c1 = c2
			endif

			if(a(c1) >a(p))then
				t = a(p)
				a(p) = a(c1)
				a(c1) = t
				p = c1
				else
					exit
			endif
		enddo
	enddo
end subroutine hsort


program christmas_eve_eve_eve
  implicit none
  integer :: d
  read(*,*) d
  if (d .eq. 25) then
     write(*,'(a)') 'Christmas'
  else if (d .eq. 24) then
     write(*,'(a)') 'Christmas Eve'
  else if (d .eq. 23) then
     write(*,'(a)') 'Christmas Eve Eve'
  else
     write(*,'(a)') 'Christmas Eve Eve Eve'
  end if
  stop
end program christmas_eve_eve_eve


program pyramid
  implicit none
  integer :: n, x(100) = 0, y(100) = 0, h(100) = 0, i
  integer :: cx, cy, ch, mx, my, mh = -1
  read(*,*) n
  do i = 1, n
    read(*,*) x(i), y(i), h(i)
    if (mh < h(i)) then
      mx = x(i)
      my = y(i)
      mh = h(i)
    end if
  end do
  do cx = 0, 100
    do cy = 0, 100
      ch = mh+abs(mx-cx)+abs(my-cy)
      do i = 1, n
        if (max(ch-abs(cx-x(i))-abs(cy-y(i)),0) /= h(i)) goto 100
      end do
      write(*,'(i0,x,i0,x,i0)') cx, cy, ch
      stop
100 end do
  end do
end program pyramid

Score: 300 points

Problem Statement

In the Ancient Kingdom of Snuke, there was a pyramid to strengthen the authority of Takahashi, the president of AtCoder Inc.

The pyramid had center coordinates (C_X, C_Y) and height H. The altitude of coordinates (X, Y) is max(H - |X - C_X| - |Y - C_Y|, 0).

Aoki, an explorer, conducted a survey to identify the center coordinates and height of this pyramid. As a result, he obtained the following information:

C_X, C_Y was integers between 0 and 100 (inclusive), and H was an integer not less than 1.

Additionally, he obtained N pieces of information. The i-th of them is: "the altitude of point (x_i, y_i) is h_i."

This was enough to identify the center coordinates and the height of the pyramid. Find these values with the clues above.

Constraints

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

x_i and y_i are integers between 0 and 100 (inclusive).

h_i is an integer between 0 and 10^9 (inclusive).

The N coordinates (x_1, y_1), (x_2, y_2), (x_3, y_3), ..., (x_N, y_N) are all different.

The center coordinates and the height of the pyramid can be uniquely identified.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1 h_1
x_2 y_2 h_2
x_3 y_3 h_3
:
x_N y_N h_N

Output

Print values C_X, C_Y and H representing the center coordinates and the height of the pyramid in one line, with spaces in between.

Sample Input 1

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

Sample Output 1

2 2 6

In this case, the center coordinates and the height can be identified as (2, 2) and 6.

Sample Input 2

2
0 0 100
1 1 98

Sample Output 2

0 0 100

In this case, the center coordinates and the height can be identified as (0, 0) and 100.

Note that C_X and C_Y are known to be integers between 0 and 100.

Sample Input 3

3
99 1 191
100 1 192
99 0 192

Sample Output 3

100 0 193

In this case, the center coordinates and the height can be identified as (100, 0) and 193.


program main
integer :: n,cx,cy
integer,allocatable :: x(:),y(:),h(:),hx(:)
integer :: k
read(*,*)n
allocate(x(n),y(n),h(n),hx(n))
do k=1,n
	read(*,*)x(k),y(k),h(k)
end do

loop1:do cx=0,100
	do cy=0,100
		do k=1,n
        	hx(k) = h(k) +abs(x(k)-cx)+abs(y(k)-cy)
        end do
        if (maxval(hx) == minval(hx)) then
        	exit loop1
        end if
    end do
end do loop1

write(*,'(i0,x,i0,x,i0)')cx,cy,hx(1)
end program


program main
Implicit None
integer(8)::n,i,j,u,xa,ya,ha,hb,k,m
integer(8),allocatable::x(:),y(:),h(:)

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

	do i = 1,n
		read *,x(i),y(i),h(i)
	enddo

	i = 1
	do while(h(i) == 0)
		i = i +1
	enddo

	u = h(i);xa = x(i);ya = y(i)

	do k = 0,100
		do j = 0,100
			ha = abs(k - xa)+abs(j - ya) + u
			do m = i + 1,n
				hb = ha - abs(x(m) - k) - abs(y(m) - j)
				if(hb < 0) hb = 0
				if(hb /= h(m)) exit
				if(m == n)then
					print 101,k,j,ha
					stop
				endif
			enddo
		enddo
	enddo

101 format(i0,1xi0,1xi0)
end program main


program main
Implicit None
integer::n,i,j,u,xa,ya,ha,hb,k,m
integer,allocatable::x(:),y(:),h(:)

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

	do i = 1,n
		read *,x(i),y(i),h(i)
	enddo

	i = 1
	do while(h(i) == 0)
		i = i +1
	enddo

	u = h(i);xa = x(i);ya = y(i)

	do k = 0,100
		do j = 0,100
			ha = abs(k - xa)+abs(j - ya) + u
			do m = i + 1,n
				hb = ha - abs(x(m) - k) - abs(y(m) - j)
				if(hb < 0) hb = 0
				if(hb /= h(m)) exit
				if(m == n)then
					print 101,k,j,ha
					stop
				endif
			enddo
		enddo
	enddo

101 format(i0,1xi0,1xi0)
end program main


program pyramid
  implicit none
  integer              :: n, highest, idx, i, j, k, hc
  integer, allocatable :: x(:), y(:), h(:)
  logical              :: invalid
  read(*,*) n
  allocate(x(n),y(n),h(n))
  highest = 0
  idx = 0
  do i = 1, n
     read(*,*) x(i),y(i),h(i)
     if (h(i) .gt. highest) then
        highest = h(i)
        idx = i
     end if
  end do
  do i = 0, 100
     do j = 0, 100
        hc = highest + abs(i-x(idx)) + abs(j-y(idx))
        invalid = .false.
        do k = 1, n
           if (h(k) .ne. max(hc - abs(i-x(k)) - abs(j-y(k)),0)) then
              invalid = .true.
              exit
           end if
        end do
        if (invalid) cycle
        deallocate(x,y,h)
        write(*,'(i0,x,i0,x,i0)') i, j, hc
        stop
     end do
  end do
end program pyramid


integer :: a(3),ma,mi,ce,i

read*,a(:)

ma = maxval( a )
mi = minval( a )

do i = 1,3
  if( a(i)>=mi .and. a(i)<=ma ) ce = a(i)
end do

print*,ma*10+mi+ce
end

Score : 100 points

Problem Statement

You have decided to give an allowance to your child depending on the outcome of the game that he will play now.

The game is played as follows:

There are three "integer panels", each with a digit between 1 and 9 (inclusive) printed on it, and one "operator panel" with a + printed on it.

The player should construct a formula of the form X + Y, by arranging the four panels from left to right. (The operator panel should not be placed at either end of the formula.)

Then, the amount of the allowance will be equal to the resulting value of the formula.

Given the values A, B and C printed on the integer panels used in the game, find the maximum possible amount of the allowance.

Constraints

All values in input are integers.

1 \leq A, B, C \leq 9

Input

Input is given from Standard Input in the following format:

A B C

Output

Print the maximum possible amount of the allowance.

Sample Input 1

1 5 2

Sample Output 1

53

The amount of the allowance will be 53 when the panels are arranged as 52+1, and this is the maximum possible amount.

Sample Input 2

9 9 9

Sample Output 2

108

Sample Input 3

6 6 7

Sample Output 3

82


integer a,b,c
read*, a,b,c
print*, a+b+c+9*max(a,b,c)
end


program maximize_the_formula
  implicit none
  integer :: a, b, c
  read(*,*) a, b, c
  write(*,'(i0)') a+b+c+9*max(a,b,c)
end program maximize_the_formula


module ABC110

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

  ! require all variables to be explicitly declared
  implicit none

  ! accessibility of <subroutine>s and <function>s in this <module>
  public  :: task_A
  private :: calculate_maximum_allowance

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

  subroutine task_A

    ! variables for this <subroutine>
    integer(INT8) :: A, B, C

    ! STEP.01
    ! read out the given data
    read(unit=INPUT_UNIT, fmt=*) A, B, C

    ! STEP.02
    ! calculate & output the answer of the task
    write(unit=OUTPUT_UNIT, fmt='(I0)', advance='yes') calculate_maximum_allowance (A, B, C)

    ! STEP.END
    return

  end subroutine task_A

  pure function calculate_maximum_allowance (A, B, C) result(allowance)

    ! arguments for this <function>
    integer(INT8), intent(in) :: A, B, C

    ! return value of this <function>
    integer(INT8) :: allowance

    allowance =     10_INT8 * A + B + C
    allowance = max(10_INT8 * B + C + A, allowance)
    allowance = max(10_INT8 * C + A + B, allowance)

    return

  end function calculate_maximum_allowance

end module ABC110

program main

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

  ! require all variables to be explicitly declared
  implicit none

  call task_A

end program main


program main
  implicit none
  integer :: a(3)
  read(*, *) a(:)
  write(*, "(i0)") sum(a) + maxval(a) * 9
end program main


program abc110a
  implicit none
  integer a(3)
  read *, a
  print '(i0)', sum(a) + maxval(a) * 9
end program abc110a


program maximize
  implicit none
  integer :: a, b, c
  integer :: lower, middle, upper
  read(*,*) a, b, c

  upper = max(a,b,c)
  lower = min(a,b,c)
  middle = a+b+c-upper-lower

  write(*,'(i0)') 10*upper+middle+lower
  stop
end program maximize


program main
integer ::a,b,c,d,e1=1,e2=1,i,j
integer,allocatable :: x(:),y(:)

read(*,*)a,b,c,d
allocate(x(a),y(b))
read(*,*)x(1:a)
read(*,*)y(1:b)
do i=c+1,d-1
	e1 = 1
    e2 = 1
	do j =1,a
    	if (x(j)>=i) then
        	e1=0
            exit
        end if
    end do
    do j =1,b
    	if (y(j)<i) then
        	e2=0
            exit
        end if
    end do
    if (e1==1 .and. e2==1) exit
end do
if(e1==1 .and. e2==1) then
	write(*,*)"No War"
else
	write(*,*)"War"
end if
end program

Score : 200 points

Problem Statement

Our world is one-dimensional, and ruled by two empires called Empire A and Empire B.

The capital of Empire A is located at coordinate X, and that of Empire B is located at coordinate Y.

One day, Empire A becomes inclined to put the cities at coordinates x_1, x_2, ..., x_N under its control, and Empire B becomes inclined to put the cities at coordinates y_1, y_2, ..., y_M under its control.

If there exists an integer Z that satisfies all of the following three conditions, they will come to an agreement, but otherwise war will break out.

X < Z \leq Y

x_1, x_2, ..., x_N < Z

y_1, y_2, ..., y_M \geq Z

Determine if war will break out.

Constraints

All values in input are integers.

1 \leq N, M \leq 100

-100 \leq X < Y \leq 100

-100 \leq x_i, y_i \leq 100

x_1, x_2, ..., x_N \neq X

x_i are all different.

y_1, y_2, ..., y_M \neq Y

y_i are all different.

Input

Input is given from Standard Input in the following format:

N M X Y
x_1 x_2 ... x_N
y_1 y_2 ... y_M

Output

If war will break out, print War; otherwise, print No War.

Sample Input 1

3 2 10 20
8 15 13
16 22

Sample Output 1

No War

The choice Z = 16 satisfies all of the three conditions as follows, thus they will come to an agreement.

X = 10 < 16 \leq 20 = Y

8, 15, 13 < 16

16, 22 \geq 16

Sample Input 2

4 2 -48 -1
-20 -35 -91 -23
-22 66

Sample Output 2

War

Sample Input 3

5 3 6 8
-10 3 1 5 -100
100 6 14

Sample Output 3

War


INTEGER N,M,X,Y
INTEGER,ALLOCATABLE,DIMENSION(:)::A,B
READ*,N,M,X,Y
ALLOCATE(A(N))
ALLOCATE(B(M))
READ*,A
READ*,B
PRINT"(A)",MERGE("No War","War   ",MAX( X,MAXVAL(A) )<MIN( Y,MINVAL(B) ) )
END


INTEGER N,M,X,Y
INTEGER,ALLOCATABLE,DIMENSION(:)::A,B
READ*,N,M,X,Y
ALLOCATE(A(N))
ALLOCATE(B(M))
READ*,X
READ*,Y
PRINT"(A)",MERGE("No War","   War",MAX( X,MAXVAL(A) )<MIN( Y,MINVAL(B) ) )
END


program abc110b
  implicit none
  integer n, m, x, y
  integer, dimension(:), allocatable :: xs, ys

  read *, n, m, x, y
  allocate(xs(n + 1), ys(m + 1))
  read *, xs(1:n), ys(1:m)
  xs(n + 1) = x
  ys(m + 1) = y
  if (maxval(xs) < minval(ys)) then
     print '(a)', "No War"
  else
     print '(a)', "War"
  end if
  deallocate(xs, ys)
end program abc110b


program onedimensional_worlds_tale
  implicit none
  integer n, m, x, y
  integer,allocatable :: xs(:), ys(:)

  read *, n, m, x, y
  allocate(xs(n + 1), ys(m + 1))
  read *, xs(1:n), ys(1:m)
  xs(n + 1) = x
  ys(m + 1) = y
  if (maxval(xs) < minval(ys)) then
     print '(a)', "No War"
  else
     print '(a)', "War"
  end if
  deallocate(xs, ys)
end program onedimensional_worlds_tale


program onedimensional_worlds_tale
  implicit none
  integer n, m, x, y
  integer,allocatable :: xs(:)
  integer,allocatable :: ys(:)

  read *, n, m, x, y
  allocate(xs(n + 1))
  allocate(ys(m + 1))
  read *, xs(1:n)
  read *, ys(1:m)
  xs(n + 1) = x
  ys(m + 1) = y
  if (maxval(xs) < minval(ys)) then
     print '(a)', "No War"
  else
     print '(a)', "War"
  end if
  deallocate(xs)
  deallocate(ys)
end program onedimensional_worlds_tale


program main
implicit None
integer::n,m,x,y,z,i,j,k
integer,allocatable::a(:),b(:)

read *,n,m,x,y
allocate(a(n))
allocate(b(m))

read *,a
read *,b

z = x

do i = 1,n
	if(z < a(i))then
		z = a(i)
	endif
enddo

k = y

do i = 1,m
	if(k >= b(i))then
		k = b(i)
	endif
enddo

j = 0
if (z >= y)then
	j = 1
endif

if (z >= k)then
	j = 1
endif

if (j == 0)then
	print 101,"No War"
	else
		print 101,"War"
endif

101 format(a)
end program main


program main
implicit None
integer::n,m,x,y,z,i,k
integer,allocatable::a(:),b(:)

read *,n,m,x,y
allocate(a(n))
allocate(b(m))

read *,a
read *,b

z = x

do i = 1,n
	if(z < a(i))then
		z = a(i)
	endif
enddo

k = y

do i = 1,m
	if(k >= b(i))then
		k = b(i)
	endif
enddo

if(k <= z)then
	print 101,'War'
	else
		print 101,'No war'
endif

101 format(a)
end program main


program main
implicit None
integer::n,m,x,y,z,i,k
integer,allocatable::a(:),b(:)

read *,n,m,x,y
allocate(a(n))
allocate(b(m))

read *,a
read *,b

z = x

do i = 1,n
	if(z < a(i))then
		z = a(i)
	endif
enddo

z = z+1

k = y

do i = 1,m
	if(k >= b(i))then
		k = b(i)
	endif
enddo

if(k < z)then
	print 101,'War'
	else
		print 101,'No war'
endif

101 format(a)
end program main


program main
implicit None
integer::n,m,x,y,i,p,q
integer,allocatable::a(:),b(:)

read *,n,m,x,y
allocate(a(n))
allocate(b(m))

read *,a
read *,b

p = x
q = y

do i = 1,n
	if (a(i) > p)then
		p = a(i)
	endif
enddo

do i = 1,n
	if (b(i) < q)then
	q = b(i)
	endif
enddo

if (p < q)then
	print 101,"No War"
	else
		print 101,"War"
endif

101 format(a)
end program main


program worlds_tale
  implicit none
  integer              :: n, m, x, y
  integer, allocatable :: xi(:), yi(:)
  integer              :: inf, sup
  read(*,*) n, m, x, y
  allocate(xi(n),yi(m))
  read(*,*) xi(:)
  read(*,*) yi(:)
  sup = maxval(xi)
  inf = minval(yi)
  if (min(inf,y)-max(sup,x) .ge. 1) then
     write(*,'(a)') "No War"
  else
     write(*,'(a)') "War"
  end if
  deallocate(xi,yi)
  stop
end program worlds_tale


program sample
	implicit none
    integer(8) :: A,B

    read(*,*) A,B

	if (mod(A*B,2)==1) then
    	write(*,*) 'Yes'
	else
    	write(*,*) 'No'
    end if

	stop
end program sample

Score : 100 points

Problem Statement

You are given integers A and B, each between 1 and 3 (inclusive).

Determine if there is an integer C between 1 and 3 (inclusive) such that A \times B \times C is an odd number.

Constraints

All values in input are integers.

1 \leq A, B \leq 3

Input

Input is given from Standard Input in the following format:

A B

Output

If there is an integer C between 1 and 3 that satisfies the condition, print Yes; otherwise, print No.

Sample Input 1

3 1

Sample Output 1

Yes

Let C = 3. Then, A \times B \times C = 3 \times 1 \times 3 = 9, which is an odd number.

Sample Input 2

1 2

Sample Output 2

No

Sample Input 3

2 2

Sample Output 3

No


program main
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: a,b
    read*, a,b
    if (mod(a,2) == 1 .and. mod(b,2) == 1) then
        print'(a)', 'Yes'
    else
        print'(a)', 'No'
    end if
end program main


read*, n,m
if(mod((n*m),2)==1)then
	print*, "Yes"
else
	print*, "No"
endif
end


integer :: a,b
logical :: flag = .true.

read*,a,b

if( mod(a,2)==0 ) flag = .false.
if( mod(b,2)==0 ) flag = .true.

if( flag )then
  print*,'Yes'
else
  print*,'No'
end if
end


integer a,b
read*, a,b
if(mod(a*b,2)>0) then
	print*, "Yes"
else
	print*, "No"
end if
end


integer a,b
read*,a,b
if(mod(a*b,2)==1)then
	print"(A)","Yes"
else
	print"(A)","No"
endif
end


program abc109a
  implicit none
  integer a(2)

  read *, a
  if (maxval(trailz(a)) == 0) then
     print '(a)', 'Yes'
  else
     print '(a)', 'No'
  end if
end program abc109a


program abc109a
  implicit none
  integer a(2)

  read *, a
  if (minval(trailz(a)) == 0) then
     print '(a)', 'Yes'
  else
     print '(a)', 'No'
  end if
end program abc109a


program main
implicit None

integer::a,b

	read *,a,b

	if(mod(a*b,2) == 0)then
		print 101,"No"
		else
		print 101,"Yes"
	endif

101 format (a)
end program main


program test
implicit none
integer::a, b
read(*,*)a, b
if(a==2 .or. b==2) then
write(*,*)"No"
else
write(*,*)"Yes"
end if
end program test