program lexicographic_constraints
  implicit none
  integer              :: n, m, i, j, k
  integer, allocatable :: a(:), s(:)
  integer              :: ng, ok
  logical              :: possible
  read(*,*) n
  allocate(a(n))
  read(*,*) a(:)
  m = maxval(a)+1
  allocate(s(m))
  ng = 0
  ok = n
  do while (ok-ng > 1)
     k = (ng+ok)/2
     possible = .true.
     s = 0
     if (n .gt. 1) then
        loop: do i = 2, n
           if (a(i) .gt. a(i-1)) cycle
           s(a(i)+1:a(i-1)+1) = 0
           s(a(i)) = s(a(i)) + 1
           if (s(a(i)) .gt. k-1) then
              if (a(i) .gt. 1) then
                 do j = a(i)-1, 1, -1
                    if (s(j) .lt. k-1) then
                       s(j) = s(j) + 1
                       s(j+1:a(i)) = 0
                       cycle loop
                    end if
                 end do
              end if
              possible = .false.
              exit loop
           end if
        end do loop
     end if
     if (possible) then
        ok = k
     else
        ng = k
     end if
  end do
  deallocate(a,s)
  write(*,'(i0)') ok
  stop
end program lexicographic_constraints

Score : 700 points

Problem Statement

There are N strings arranged in a row.
It is known that, for any two adjacent strings, the string to the left is lexicographically smaller than the string to the right.
That is, S_1<S_2<...<S_N holds lexicographically, where S_i is the i-th string from the left.

At least how many different characters are contained in S_1,S_2,...,S_N, if the length of S_i is known to be A_i?

Constraints

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

1 \leq A_i \leq 10^9

A_i is an integer.

Note

The strings do not necessarily consist of English alphabet; there can be arbitrarily many different characters (and the lexicographic order is defined for those characters).

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the minimum possible number of different characters contained in the strings.

Sample Input 1

3
3 2 1

Sample Output 1

2

The number of different characters contained in S_1,S_2,...,S_N would be 3 when, for example, S_1=abc, S_2=bb and S_3=c.

However, if we choose the strings properly, the number of different characters can be 2.

Sample Input 2

5
2 3 2 1 2

Sample Output 2

2


program lexicographic_constraints
  implicit none
  integer              :: n, m, i, j, k
  integer, allocatable :: a(:), s(:)
  read(*,*) n
  allocate(a(n))
  read(*,*) a(:)
  m = maxval(a)+1
  allocate(s(m))
  s = 0
  k = 1
  if (n .gt. 1) then
     loop: do i = 2, n
        if (a(i) .gt. a(i-1)) cycle
        s(a(i)+1:a(i-1)+1) = 0
        s(a(i)) = s(a(i)) + 1
        if (s(a(i)) .eq. k) then
           if (a(i) .gt. 1) then
              do j = a(i)-1, 1, -1
                 if (s(j) .ne. k-1) then
                    s(j) = s(j) + 1
                    s(j+1:a(i)) = 0
                    cycle loop
                 end if
              end do
           end if
           k = k + 1
        end if
     end do loop
  end if
  write(*,'(i0)') k
  deallocate(a,s)
  stop
end program lexicographic_constraints

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

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.


module module_sort
    implicit none
    interface sort
        module procedure int_heap_sort, real_heap_sort
    end interface

    contains

        subroutine int_heap_down(a,node,las)
            implicit none
            integer(8) a(*),x
            integer(8) node,par,ch1,ch2,las
            par = node
            x = a(par); ch1 = par*2
            do while(ch1 <= las)
                ch2 = ch1+1
                if(ch2 <= las .and. a(ch1) < a(ch2)) ch1 = ch2
                if(a(ch1) > x) then
                    a(par) = a(ch1); a(ch1) = x
                else
                    exit
                end if
                par = ch1; ch1 = par*2
            end do
        end subroutine int_heap_down

        subroutine int_heap_sort(a,n)
            implicit none
            integer(8) a(*),x
            integer(8) n,i
            do i = n/2, 1, -1
                call int_heap_down(a,i,n)
            end do
            do i=n,2,-1
                x = a(i); a(i) = a(1); a(1) = x
                call int_heap_down(a,1_8,i-1)
            end do
        end subroutine int_heap_sort

        subroutine real_heap_down(a,node,las)
            implicit none
            real a(*), x
            integer(8) node,par,ch1,ch2,las
            par = node
            x = a(par); ch1 = par*2
            do while(ch1 <= las)
                ch2 = ch1+1
                if(ch2 <= las .and. a(ch1) < a(ch2)) ch1 = ch2
                if(a(ch1) > x) then
                    a(par) = a(ch1); a(ch1) = x
                else
                    exit
                end if
                par = ch1; ch1 = par*2
            end do
        end subroutine real_heap_down

        subroutine real_heap_sort(a,n)
            implicit none
            real a(*),x
            integer(8) n,i
            do i = n/2, 1, -1
                call real_heap_down(a,i,n)
            end do
            do i=n,2,-1
                x = a(i); a(i) = a(1); a(1) = x
                call real_heap_down(a,1_8,i-1)
            end do
        end subroutine real_heap_sort

end module module_sort

module module_deque
    implicit none
    private
    type deque_node
        type(deque_node) ,pointer:: prev => null(),next => null()
        integer(8) val
    end type deque_node

    type,public:: deque
        type(deque_node) ,pointer:: first => null(),last => null(), ptr => null()
        integer(8):: size=0
        contains
            procedure:: push_back => deque_push_back
            procedure:: push_front => deque_push_front
            procedure:: pop_back => deque_pop_back
            procedure:: pop_front => deque_pop_front
    end type deque

    contains

        subroutine deque_push_back(self,val)
            class(deque)::self
            integer(8)::val
            if(self%size == 0) then
                allocate(self%first)
                self%last => self%first
                self%first%val = val
                self%size = 1
            else
                allocate(self%last%next)
                self%last%next%prev => self%last
                self%last => self%last%next
                self%last%val = val
                self%size = self%size+1
            end if
        end subroutine deque_push_back

        subroutine deque_push_front(self,val)
            class(deque)::self
            integer(8)::val
            if(self%size == 0) then
                allocate(self%first)
                self%last => self%first
                self%first%val = val
                self%size = 1
            else
                allocate(self%first%prev)
                self%first%prev%next => self%first
                self%first => self%first%prev
                self%first%val = val
                self%size = self%size+1
            end if
        end subroutine deque_push_front

        subroutine deque_pop_back(self)
            class(deque)::self
            if(self%size == 0) then
                return
            else if(self%size == 1) then
                self%size = 0
                deallocate(self%last)
                self%last => null(); self%first => null()
            else
                self%last => self%last%prev
                deallocate(self%last%next)
                self%last%next => null()
                self%size = self%size-1
            end if
        end subroutine deque_pop_back

        subroutine deque_pop_front(self)
            class(deque)::self
            if(self%size == 0) then
                return
            else if(self%size == 1) then
                self%size = 0
                deallocate(self%first)
                self%last => null(); self%first => null()
            else
                self%first => self%first%next
                deallocate(self%first%prev)
                self%first%prev => null()
                self%size = self%size-1
            end if
        end subroutine deque_pop_front

end module module_deque

module module_RedBlackTree
    implicit none
    private
    integer(8),parameter :: red = 1,black = 0

    type RBT_Int_node
        type(RBT_Int_node),pointer :: par=>null(), left => null(),right => null()
        integer(8)         :: key
        integer(8)         :: val=0
        integer(8)         :: color=red
    end type RBT_Int_node

    type RBT_Char_node
        type(RBT_Char_node),pointer :: par=>null(), left => null(),right => null()
        character(100)     :: key
        integer(8)         :: val=0
        integer(8)         :: color=red
    end type RBT_Char_node

    type(RBT_Int_node) ,pointer,save  :: rbt_int_nil
    type(RBT_Char_node) ,pointer,save :: rbt_char_nil

    type,public:: RedBlackTree_Int
        type(RBT_Int_node),pointer :: root => null()
        integer(8)                 :: size = 0
        contains
            procedure:: insert  => Int_Insert
            procedure:: get_val => Int_Get
            procedure:: find    => Int_Find
            procedure:: add     => Int_Add
            procedure:: FixUp   => Int_FixUp
    end type RedBlackTree_Int

    type,public:: RedBlackTree_Char
        type(RBT_Char_node),pointer :: root => null()
        integer(8)                  :: size = 0
        contains
            procedure:: insert  => Char_Insert
            procedure:: get_val => Char_Get
            procedure:: find    => Char_Find
            procedure:: add     => Char_Add
            procedure:: FixUp   => Char_FixUp
    end type RedBlackTree_Char

    contains

        subroutine Int_Init()
            implicit none

            if(.not.associated(rbt_int_nil)) then
                allocate(rbt_int_nil)
                rbt_int_nil%color = black
            end if

        end subroutine  Int_Init

        recursive function Int_Get(self,key) result(val)
            implicit none
            class(RedBlackTree_Int), intent(in) :: self
            integer(8)                      :: key
            integer(8)                      :: val
            type(RBT_Int_node),pointer          :: u

            if(.not.associated(rbt_int_nil)) then
                allocate(rbt_int_nil)
                rbt_int_nil%color = black
            end if

            if(self%size == 0) then
                val = -1
                return
            end if

            u   => Int_SearchTree(self%root,key)
            val = u%val
            return

        end function Int_Get

        recursive function Int_Find(self,key) result(res)
            implicit none
            class(RedBlackTree_Int), intent(in) :: self
            integer(8)                          :: key
            logical                             :: res
            type(RBT_Int_node),pointer          :: u

            if(.not.associated(rbt_int_nil)) then
                allocate(rbt_int_nil)
                rbt_int_nil%color = black
            end if

            if(self%size == 0) then
                res = .false.
                return
            end if

            u   => Int_SearchTree(self%root,key)
            res = (.not.associated(u,rbt_int_nil))
            return

        end function Int_Find

        recursive subroutine Int_Add(self,key,val)
            implicit none
            class(RedBlackTree_Int), intent(inout) :: self
            integer(8)                         :: key
            type(RBT_Int_node),pointer             :: u
            integer(8)                         :: val

            if(.not.associated(rbt_int_nil)) then
                allocate(rbt_int_nil)
                rbt_int_nil%color = black
            end if

            if(self%size == 0) then
                call self%insert(key,val)
                return
            end if

            u   => Int_SearchTree(self%root,key)
            if (associated(u,rbt_int_nil)) then
                call self%insert(key,val)
            else
                u%val = u%val+val
            end if
            return

        end subroutine Int_Add

        recursive function Int_SearchTree(u,key) result(res)
            implicit none
            type(RBT_Int_node), pointer :: u,res
            integer(8)              :: key

            if(associated(u,rbt_int_nil)) then
                res => rbt_int_nil
                return
            end if

            if(key < u%key) then
                res => Int_SearchTree(u%left,key)
                return
            else if(key > u%key) then
                res => Int_SearchTree(u%right,key)
                return
            else
                res => u
                return
            end if

        end function Int_SearchTree

        subroutine Int_Insert(self,key,val)
            implicit none
            class(RedBlackTree_Int),intent(inout):: self
            integer(8),intent(in)            :: key
            integer(8),intent(in)            :: val
            type(RBT_Int_node),pointer,save      :: now, u

            if(.not.associated(rbt_int_nil)) then
                allocate(rbt_int_nil)
                rbt_int_nil%color = black
            end if

            !allocate new RBT_Int_node
            allocate(u)
            u%key = key; u%val = val
            u%left => rbt_int_nil; u%right => rbt_int_nil

            !insert new RBT_Int_node
            if(self%size == 0) then
                u%left => rbt_int_nil; u%right => rbt_int_nil; u%par => rbt_int_nil
                self%root => u
                self%root%color = black
                self%size = 1
                return
            else
                now => self%root
                if(.not.Insert_RBT_Int_node(u,now)) return
            end if

            !Fix Tree
            call self%FixUp(u)

            self%size = self%size+1

        end subroutine Int_Insert

        recursive function Insert_RBT_Int_node(u,now) result(added)
            implicit none
            type(RBT_Int_node), pointer:: u,now
            logical            :: added

            if(u%key < now%key) then
                if(associated(now%left,rbt_int_nil)) then
                    now%left => u
                    u%par => now
                    added = .true.
                else
                    now => now%left
                    added = Insert_RBT_Int_node(u,now)
                end if
                return
            else if(u%key > now%key) then
                if(associated(now%right,rbt_int_nil)) then
                    now%right => u
                    u%par => now
                    added = .true.
                else
                    now => now%right
                    added = Insert_RBT_Int_node(u,now)
                end if
                return
            else
                added = .false.
                return
            end if

        end function Insert_RBT_Int_node

        subroutine Int_FixUp(self,u)
            implicit none
            class(RedBlackTree_Int),intent(inout)     :: self
            type(RBT_Int_node),pointer,intent(inout)  :: u
            type(RBT_Int_node),pointer                :: w,g

            if(.not.associated(rbt_int_nil)) then
                allocate(rbt_int_nil)
                rbt_int_nil%color = black
            end if

            nullify(w,g)
            do while(u%color == red)
                if(u%key == self%root%key) then
                    u%color = black
                    return
                end if
                w => u%par
                if(w%left%color == black) then
                    call Int_FripLeft(w,self%root)
                    u => w
                    w => u%par
                end if
                if(w%color == black) return
                g => w%par
                if(g%right%color == black) then
                    call Int_FripRight(g,self%root)
                    return
                else
                    call Int_PushBlack(g)
                    u => g
                end if
            end do

        end subroutine Int_FixUp

        subroutine Int_PushBlack(u)
            implicit none
            type(RBT_Int_node), pointer:: u

            u%color = red; u%left%color = black; u%right%color = black;

        end subroutine Int_PushBlack

        subroutine Int_PullBlack(u)
            implicit none
            type(RBT_Int_node), pointer:: u

            u%color = black; u%left%color = red; u%right%color = red;

        end subroutine Int_PullBlack

        subroutine Int_FripLeft(u,root)
            implicit none
            type(RBT_Int_node), pointer, intent(inout) :: root
            type(RBT_Int_node), pointer                :: u,w
            integer(8)                             :: tmp

            tmp = u%color; u%color = u%right%color; u%right%color = tmp

            w => u%right
            w%par => u%par
            if(.not.associated(u%par,rbt_int_nil)) then
                if(associated(w%par%left,u)) then
                     w%par%left=>w
                else
                    w%par%right=>w
                end if
            end if
            u%right => w%left
            if(.not.associated(u%right,rbt_int_nil))u%right%par => u
            u%par => w
            w%left => u
            if(associated(u,root)) then
                root => w
                root%par => rbt_int_nil
            end if

        end subroutine Int_FripLeft

        subroutine Int_FripRight(u,root)
            implicit none
            type(RBT_Int_node), pointer,intent(inout):: root
            type(RBT_Int_node), pointer              :: u,w
            integer(8)                           :: tmp

            tmp = u%color; u%color = u%left%color; u%left%color = tmp

            w => u%left
            w%par => u%par
            if(.not.associated(u%par,rbt_int_nil)) then
                if(associated(w%par%left,u)) then
                    w%par%left=>w
                else
                    w%par%right=>w
                end if
            end if
            u%left => w%right
            if(.not.associated(u%left,rbt_int_nil))u%left%par => u
            u%par => w
            w%right => u
            if(associated(u,root)) then
                root => w
                root%par => rbt_int_nil
            end if

        end subroutine Int_FripRight

        subroutine Char_Init()
            implicit none

            if(.not.associated(rbt_char_nil)) then
                allocate(rbt_char_nil)
                rbt_char_nil%color = black
            end if

        end subroutine  Char_Init

        recursive function Char_Get(self,key) result(val)
            implicit none
            class(RedBlackTree_Char), intent(in) :: self
            Character(*)                         :: key
            integer(8)                           :: val
            type(RBT_Char_node),pointer          :: u

            if(.not.associated(rbt_char_nil)) then
                allocate(rbt_char_nil)
                rbt_char_nil%color = black
            end if

            if(self%size == 0) then
                val = -1
                return
            end if

            u   => Char_SearchTree(self%root,key)
            val = u%val
            return

        end function Char_Get

        recursive function Char_Find(self,key) result(res)
            implicit none
            class(RedBlackTree_Char), intent(in) :: self
            Character(*)                         :: key
            logical                              :: res
            type(RBT_Char_node),pointer          :: u

            if(.not.associated(rbt_char_nil)) then
                allocate(rbt_char_nil)
                rbt_char_nil%color = black
            end if

            if(self%size == 0) then
                res = .false.
                return
            end if

            u   => Char_SearchTree(self%root,key)
            res = (.not.associated(u,rbt_char_nil))
            return

        end function Char_Find

        recursive subroutine Char_Add(self,key,val)
            implicit none
            class(RedBlackTree_Char), intent(inout) :: self
            Character(*)                            :: key
            type(RBT_Char_node),pointer             :: u
            integer(8)                              :: val

            if(.not.associated(rbt_char_nil)) then
                allocate(rbt_char_nil)
                rbt_char_nil%color = black
            end if

            if(self%size == 0) then
                call self%insert(key,val)
                return
            end if

            u   => Char_SearchTree(self%root,key)
            if (associated(u,rbt_char_nil)) then
                call self%insert(key,val)
            else
                u%val = u%val+val
            end if
            return

        end subroutine Char_Add

        recursive function Char_SearchTree(u,key) result(res)
            implicit none
            type(RBT_Char_node), pointer :: u,res
            Character(*)                 :: key

            if(associated(u,rbt_char_nil)) then
                res => rbt_char_nil
                return
            end if

            if(key < u%key) then
                res => Char_SearchTree(u%left,key)
                return
            else if(key > u%key) then
                res => Char_SearchTree(u%right,key)
                return
            else
                res => u
                return
            end if

        end function Char_SearchTree

        subroutine Char_Insert(self,key,val)
            implicit none
            class(RedBlackTree_Char),intent(inout) :: self
            Character(*),intent(in)                :: key
            integer(8),intent(in)                  :: val
            type(RBT_Char_node),pointer,save       :: now, u

            if(.not.associated(rbt_char_nil)) then
                allocate(rbt_char_nil)
                rbt_char_nil%color = black
            end if

            !allocate new RBT_Char_node
            allocate(u)
            u%key = key; u%val = val
            u%left => rbt_char_nil; u%right => rbt_char_nil

            !insert new RBT_Char_node
            if(.not. associated(self%root)) then
                u%left => rbt_char_nil; u%right => rbt_char_nil; u%par => rbt_char_nil
                self%root => u
                self%root%color = black
                self%size = 1
                return
            else
                now => self%root
                if(.not.Insert_RBT_Char_node(u,now)) return
            end if

            !Fix Tree
            call self%FixUp(u)

            self%size = self%size+1

        end subroutine Char_Insert

        recursive function Insert_RBT_Char_node(u,now) result(added)
            implicit none
            type(RBT_Char_node), pointer:: u,now
            logical            :: added

            if(u%key < now%key) then
                if(associated(now%left,rbt_char_nil)) then
                    now%left => u
                    u%par => now
                    added = .true.
                else
                    now => now%left
                    added = Insert_RBT_Char_node(u,now)
                end if
                return
            else if(u%key > now%key) then
                if(associated(now%right,rbt_char_nil)) then
                    now%right => u
                    u%par => now
                    added = .true.
                else
                    now => now%right
                    added = Insert_RBT_Char_node(u,now)
                end if
                return
            else
                added = .false.
                return
            end if

        end function Insert_RBT_Char_node

        subroutine Char_FixUp(self,u)
            implicit none
            class(RedBlackTree_Char),intent(inout)      :: self
            type(RBT_Char_node),pointer,intent(inout)  :: u
            type(RBT_Char_node),pointer                :: w,g

            if(.not.associated(rbt_char_nil)) then
                allocate(rbt_char_nil)
                rbt_char_nil%color = black
            end if

            nullify(w,g)
            do while(u%color == red)
                if(u%key == self%root%key) then
                    u%color = black
                    return
                end if
                w => u%par
                if(w%left%color == black) then
                    call Char_FripLeft(w,self%root)
                    u => w
                    w => u%par
                end if
                if(w%color == black) return
                g => w%par
                if(g%right%color == black) then
                    call Char_FripRight(g,self%root)
                    return
                else
                    call Char_PushBlack(g)
                    u => g
                end if
            end do

        end subroutine Char_FixUp

        subroutine Char_PushBlack(u)
            implicit none
            type(RBT_Char_node), pointer:: u

            u%color = red; u%left%color = black; u%right%color = black;

        end subroutine Char_PushBlack

        subroutine Char_PullBlack(u)
            implicit none
            type(RBT_Char_node), pointer:: u

            u%color = black; u%left%color = red; u%right%color = red;

        end subroutine Char_PullBlack

        subroutine Char_FripLeft(u,root)
            implicit none
            type(RBT_Char_node), pointer, intent(inout) :: root
            type(RBT_Char_node), pointer                :: u,w
            integer(8)                             :: tmp

            tmp = u%color; u%color = u%right%color; u%right%color = tmp

            w => u%right
            w%par => u%par
            if(.not.associated(u%par,rbt_char_nil)) then
                if(associated(w%par%left,u)) then
                     w%par%left=>w
                else
                    w%par%right=>w
                end if
            end if
            u%right => w%left
            if(.not.associated(u%right,rbt_char_nil))u%right%par => u
            u%par => w
            w%left => u
            if(associated(u,root)) then
                root => w
                root%par => rbt_char_nil
            end if

        end subroutine Char_FripLeft

        subroutine Char_FripRight(u,root)
            implicit none
            type(RBT_Char_node), pointer,intent(inout):: root
            type(RBT_Char_node), pointer              :: u,w
            integer(8)                                :: tmp

            tmp = u%color; u%color = u%left%color; u%left%color = tmp

            w => u%left
            w%par => u%par
            if(.not.associated(u%par,rbt_char_nil)) then
                if(associated(w%par%left,u)) then
                    w%par%left=>w
                else
                    w%par%right=>w
                end if
            end if
            u%left => w%right
            if(.not.associated(u%left,rbt_char_nil))u%left%par => u
            u%par => w
            w%right => u
            if(associated(u,root)) then
                root => w
                root%par => rbt_char_nil
            end if

        end subroutine Char_FripRight

end module module_RedBlackTree

module module_MinHeap
    implicit none
    private
    type MH_node
        type(MH_node),pointer :: par=>null(), left => null(),right => null()
        integer(8)            :: key,val
        integer(8)            :: size
    end type MH_node
    type(MH_node) ,pointer,save :: heap_nil

    type,public:: MinHeap
        type(MH_node),pointer :: root,last
        integer(8)            :: size = 0
        contains
            procedure:: push => MinHeap_Insert_MH_node
            procedure:: pop  => MinHeap_Pop_MH_node
    end type MinHeap

    contains

        subroutine MinHeap_Up(root,u)
            implicit none
            type(MH_node),pointer  :: root,u
            integer(8)             :: tmp

            do while(.not.associated(root,u))
                if(u%key < u%par%key) then
                    tmp        = u%key
                    u%key      = u%par%key
                    u%par%key  = tmp
                    tmp        = u%val
                    u%val      = u%par%val
                    u%par%val  = tmp
                    u => u%par
                else
                    return
                end if
            end do
            return

        end subroutine MinHeap_Up

        subroutine MinHeap_Down(root)
            implicit none
            type(MH_node),pointer:: root,u
            integer(8)           :: tmp

            u => root
            do while(.not.associated(u%left,heap_nil))

                if(.not.associated(u%right,heap_nil)) then
                    if (u%right%key < u%left%key .and. u%right%key < u%key) then
                        tmp         = u%key
                        u%key       = u%right%key
                        u%right%key = tmp
                        tmp         = u%val
                        u%val       = u%right%val
                        u%right%val = tmp
                        u => u%right
                        cycle
                    end if
                end if
                if(u%left%key < u%key) then
                    tmp        = u%key
                    u%key      = u%left%key
                    u%left%key = tmp
                    tmp        = u%val
                    u%val      = u%left%val
                    u%left%val = tmp
                    u => u%left
                    cycle
                end if
                exit
            end do
            return

        end subroutine MinHeap_Down

        subroutine Update_Size(root,u,is_add)
            implicit none
            type(MH_node),pointer :: root,u
            logical               :: is_add

            do while(.not.associated(root,u))
                if(is_add) then
                    u%size = u%size+1
                else
                    u%size = u%size-1
                end if
                u => u%par
            end do

            if(is_add) then
                u%size = u%size+1
            else
                u%size = u%size-1
            end if

        end subroutine Update_size

        subroutine MinHeap_Insert_MH_node(self,key,val)
            implicit none
            class(MinHeap)        :: self
            type(MH_node),pointer :: u,now
            integer(8)            :: key,val

            if(.not.associated(heap_nil)) allocate(heap_nil)
            u => null()
            allocate(u)
            u%key  = key
            u%val  = val
            u%left => heap_nil; u%right => heap_nil
            u%size = 1

            if(self%size == 0) then
                self%root      => u
                self%root%par  => heap_nil
                self%size      = 1
                return
            end if

            now => self%root
            do
                now%size = now%size+1
                if(.not.associated(now%left,heap_nil)) then
                    if(.not.associated(now%right,heap_nil)) then
                        if(now%right%size < now%left%size) then
                            now => now%right; cycle
                        else
                            now => now%left;  cycle
                        end if
                    else
                        now%right => u
                        u%par     => now
                        exit
                    end if
                else
                    now%left => u
                    u%par    => now
                    exit
                end if
            end do

            call MinHeap_Up(self%root,u)
            self%size = self%size+1

        end subroutine MinHeap_Insert_MH_node

        subroutine MinHeap_Pop_MH_node(self)
            implicit none
            class(MinHeap)        :: self
            type(MH_node),pointer :: u,now
            integer(8)            :: tmp

            if(.not.associated(heap_nil)) allocate(heap_nil)
            if(self%size == 0) return

            now => self%root
            do
                now%size = now%size-1
                if(.not.associated(now%left,heap_nil)) then
                    if(.not.associated(now%right,heap_nil)) then
                        if(now%right%size >= now%left%size) then
                            now => now%right; cycle
                        else
                            now => now%left;  cycle
                        end if
                    else
                        u => now%left
                        tmp           = u%key
                        u%key         = self%root%key
                        self%root%key = tmp
                        tmp           = u%val
                        u%val         = self%root%val
                        self%root%val = tmp
                        now%left => heap_nil
                        exit
                    end if
                else if(associated(now,self%root)) then
                    deallocate(self%root)
                    self%size = 0
                    return
                else
                    u => now
                    tmp           = u%key
                    u%key         = self%root%key
                    self%root%key = tmp
                    tmp           = u%val
                    u%val         = self%root%val
                    self%root%val = tmp
                    if(associated(now%par%left,now)) then
                        now%par%left => heap_nil
                    else
                        now%par%right => heap_nil
                    end if
                    exit
                end if
            end do

            deallocate(u)
            self%size = self%size-1

            call MinHeap_Down(self%root)

        end subroutine MinHeap_Pop_MH_node

end module module_MinHeap

recursive function  gcd(a,b) result(res)
    implicit none
    integer(8) :: a,b,res

    if(a < b) then
        res = gcd(b,a)
        return
    end if

    if(mod(a,b) == 0) then
        res = b
        return
    else
        res = gcd(b,mod(a,b))
        return
    end if

end function gcd

recursive function mod_pow(a,b,modulo) result(res)
    implicit none
    integer(8) :: a,b,modulo,res

    if(b == 0) then
        res = 1
        return
    end if

    if(mod(b,2) == 1) then
        res = mod(a*mod_pow(a,b-1,modulo),modulo)
    else
        res = mod(mod_pow(a,b/2,modulo)**2,modulo)
    end if

    return

end function mod_pow

subroutine compress(lis,size)
    use module_sort
    use module_RedBlackTree
    implicit none
    integer(8)             :: size,res
    integer(8)             :: lis(*),tmp_lis(size)
    integer(8)             :: i
    type(RedBlackTree_Int) :: map

    do i = 1, size
        call map%insert(lis(i),0_8)
    end do

    tmp_lis(1:size) = lis(1:size)
    call sort(tmp_lis,size)

    res = 0

    call map%add(tmp_lis(1),res)

    do i = 2, size
        if(tmp_lis(i) /= tmp_lis(i-1)) then
            res = res+1
            call map%add(tmp_lis(i),res)
        end if
    end do

    do i = 1, size
        lis(i) = map%get_val(lis(i))
    enddo
end subroutine compress

module global
    use module_sort
    use module_deque
    use module_RedBlackTree
    use module_MinHeap
    implicit none
end module global

program main
    use global
    implicit none
    integer(8) :: H,W,N,X(200005),Y(200005),l,minimum,ans
    type(deque):: que(200005)
    type(RedBlackTree_Int) :: map
    integer(8) :: i

    read *,H,W,N

    do i = 1, N
        read *, X(i),Y(i)
        call map%insert(X(i)*1000000+Y(i),0_8)
        call que(X(i))%push_back(Y(i))
    end do

    do i = 1, W
        call map%insert((H+1)*1000000+i,0_8)
        call que(H+1)%push_back(i)
    end do

    l = 1
    do i = 1, H
        minimum = 1e9_8
        if(que(i+1)%size /= 0) then
            que(i+1)%ptr => que(i+1)%first
            do
                minimum = min(que(i+1)%ptr%val,minimum)
                if(associated(que(i+1)%ptr,que(i+1)%last)) exit
                que(i+1)%ptr => que(i+1)%ptr%next
            enddo
        end if
        !print *,i, minimum
        if(minimum <= l) then
            ans = i
            exit
        endif
        if(.not.map%find((i+1)*1000000+l+1)) l = l+1
    end do

    print '(i0)',ans

end program main

Score : 800 points

Problem Statement

Takahashi and Aoki will play a game using a grid with H rows and W columns of square cells.
There are N obstacles on this grid; the i-th obstacle is at (X_i,Y_i).
Here, we represent the cell at the i-th row and j-th column (1 \leq i \leq H, 1 \leq j \leq W) by (i,j).
There is no obstacle at (1,1), and there is a piece placed there at (1,1).

Starting from Takahashi, he and Aoki alternately perform one of the following actions:

Move the piece to an adjacent cell.
Here, let the position of the piece be (x,y). Then Takahashi can only move the piece to (x+1,y), and Aoki can only move the piece to (x,y+1).
If the destination cell does not exist or it is occupied by an obstacle, this action cannot be taken.

Do not move the piece, and end his turn without affecting the grid.

The game ends when the piece does not move twice in a row.

Takahashi would like to perform as many actions (including not moving the piece) as possible before the game ends, while Aoki would like to perform as few actions as possible before the game ends.
How many actions will Takahashi end up performing?

Constraints

1 \leq H,W \leq 2\times 10^5

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

1 \leq X_i \leq H

1 \leq Y_i \leq W

If i \neq j, (X_i,Y_i) \neq (X_j,Y_j)

(X_i,Y_i) \neq (1,1)

X_i and Y_i are integers.

Input

Input is given from Standard Input in the following format:

H W N
X_1 Y_1
:
X_N Y_N

Output

Print the number of actions Takahashi will end up performing.

Sample Input 1

3 3 1
3 2

Sample Output 1

2

For example, the game proceeds as follows:

Takahashi moves the piece to (2,1).

Aoki does not move the piece.

Takahashi moves the piece to (3,1).

Aoki does not move the piece.

Takahashi does not move the piece.

Takahashi performs three actions in this case, but if both players play optimally, Takahashi will perform only two actions before the game ends.

Sample Input 2

10 10 14
4 3
2 2
7 3
9 10
7 7
8 1
10 10
5 4
3 4
2 8
6 4
4 4
5 8
9 2

Sample Output 2

6

Sample Input 3

100000 100000 0

Sample Output 3

100000


program main
   implicit none
   integer(8)::n,k,i
   integer(8),allocatable,dimension(:)::h,turn,dummy
   real(8),allocatable,dimension(:)::abss
   real(8)::heikin
   read(*,*)n,k
   allocate(h(n),abss(n),turn(n))
   read(*,*)h
!   heikin=real(sum(h),8)/size(h)
 !  abss=abs(h-heikin)
   call heapsort2(n,h,turn)
   do i=1,n-k
   enddo
   allocate(dummy(n-k+1))
   do i=1,n-k+1
     dummy(i)=abs(h(i)-h(i+k-1))
   enddo
   write(*,*)minval(dummy)
 !  do i=1,k
  !    dummy(i)=h(turn(i))
   !enddo
   !write(*,*)maxval(dummy)-minval(dummy)
   !write(*,*)dummy
contains
subroutine heapsort2(n,array,turn)
  implicit none
  integer(8),intent(in)::n
  integer(8),intent(out)::turn(1:n)
  integer(8),intent(inout)::array(1:n)

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

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

  do i=1,N
     turn(i)=i
  enddo

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

  return
end subroutine heapsort2
end program main

Score : 300 points

Problem Statement

In some other world, today is Christmas Eve.

There are N trees planted in Mr. Takaha's garden. The height of the i-th tree (1 \leq i \leq N) is h_i meters.

He decides to choose K trees from these trees and decorate them with electric lights. To make the scenery more beautiful, the heights of the decorated trees should be as close to each other as possible.

More specifically, let the height of the tallest decorated tree be h_{max} meters, and the height of the shortest decorated tree be h_{min} meters. The smaller the value h_{max} - h_{min} is, the better. What is the minimum possible value of h_{max} - h_{min}?

Constraints

2 \leq K < N \leq 10^5

1 \leq h_i \leq 10^9

h_i is an integer.

Input

Input is given from Standard Input in the following format:

N K
h_1
h_2
:
h_N

Output

Print the minimum possible value of h_{max} - h_{min}.

Sample Input 1

5 3
10
15
11
14
12

Sample Output 1

2

If we decorate the first, third and fifth trees, h_{max} = 12, h_{min} = 10 so h_{max} - h_{min} = 2. This is optimal.

Sample Input 2

5 3
5
7
5
7
7

Sample Output 2

0

If we decorate the second, fourth and fifth trees, h_{max} = 7, h_{min} = 7 so h_{max} - h_{min} = 0. This is optimal.

There are not too many trees in these sample inputs, but note that there can be at most one hundred thousand trees (we just can't put a sample with a hundred thousand lines here).


integer a
read*, a
if(a==3 .or. a==5 .or. a==7)then
	print*,"YES"
else
	print*,"NO"
end if
end

Score : 100 points

Problem Statement

Shichi-Go-San (literally "Seven-Five-Three") is a traditional event in a certain country to celebrate the growth of seven-, five- and three-year-old children.

Takahashi is now X years old. Will his growth be celebrated in Shichi-Go-San this time?

Constraints

1 ≤ X ≤ 9

X is an integer.

Input

Input is given from Standard Input in the following format:

X

Output

If Takahashi's growth will be celebrated, print YES; if it will not, print NO.

Sample Input 1

5

Sample Output 1

YES

The growth of a five-year-old child will be celebrated.

Sample Input 2

6

Sample Output 2

NO

See you next year.


module deque_mod
    implicit none
    type deque
        integer(4),pointer:: v(:)
        integer(4):: l,r
        integer(4):: lmax, rmax
    end type
    private
    public:: deque
    public:: init_deque
    public:: append, appendleft
    public:: pop, popleft
    public:: right, left
    public:: remaining_elements
    public:: remain

contains
    subroutine init_deque(dq)
        ! 初期化
        type(deque):: dq
        dq%r=0; dq%rmax=1
        dq%l=1; dq%lmax=-1
        allocate(dq%v(dq%lmax:dq%rmax))
    end subroutine

    subroutine append(dq,num)
        ! 右端に挿入
        type(deque):: dq
        integer(4):: num
        if (dq%r+1 > dq%rmax) call add_(dq)
        dq%r=dq%r+1
        dq%v(dq%r) = num
    end subroutine

    subroutine appendleft(dq,num)
        ! 左端に挿入
        type(deque):: dq
        integer(4):: num
        if (dq%l-1 < dq%lmax) call add_(dq)
        dq%l=dq%l-1
        dq%v(dq%l) = num
    end subroutine

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

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

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

    function right(dq) result(ret)
        ! 右端を確認
        type(deque):: dq
        integer(4):: ret
        ret = dq%v(dq%r)
    end function

    function left(dq) result(ret)
        ! 左端を確認
        type(deque):: dq
        integer(4):: ret
        ret = dq%v(dq%l)
    end function

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

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

program name
    use deque_mod
    implicit none
    integer(4):: n,i,cnt,nmax
    type(deque) d

    call init_deque(d)

    read*, n

    call append(d,3)
    call append(d,5)
    call append(d,7)

    nmax = popleft(d)
    cnt=0
    do while(nmax < n)
        if (shitigosan(nmax))cnt=cnt+1
        call append(d,10*nmax+3)
        call append(d,10*nmax+5)
        call append(d,10*nmax+7)
        nmax = popleft(d)
    end do

    print*, cnt

contains
function shitigosan(num) result(ret)
    logical:: ret, ok3, ok5, ok7
    integer(4):: num,inum,i

    inum = num
    ok3=.false.
    ok5=.false.
    ok7=.false.
    do while(inum>0)
        i = mod(inum,10)
        if (i==3) ok3=.true.
        if (i==5) ok5=.true.
        if (i==7) ok7=.true.
        inum=inum/10
    end do
    ret = ok3 .and. ok5 .and. ok7

end function

end program name

Score : 300 points

Problem Statement

You are given an integer N. Among the integers between 1 and N (inclusive), how many Shichi-Go-San numbers (literally "Seven-Five-Three numbers") are there?

Here, a Shichi-Go-San number is a positive integer that satisfies the following condition:

When the number is written in base ten, each of the digits 7, 5 and 3 appears at least once, and the other digits never appear.

Constraints

1 \leq N < 10^9

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the number of the Shichi-Go-San numbers between 1 and N (inclusive).

Sample Input 1

575

Sample Output 1

4

There are four Shichi-Go-San numbers not greater than 575: 357, 375, 537 and 573.

Sample Input 2

3600

Sample Output 2

13

There are 13 Shichi-Go-San numbers not greater than 3600: the above four numbers, 735, 753, 3357, 3375, 3537, 3557, 3573, 3575 and 3577.

Sample Input 3

999999999

Sample Output 3

26484


implicit none
integer(16) N
read*,N
print"(i0)",dfs(.false.,.false.,.false.,0_16)

contains

recursive function dfs(a,b,c,num)result(res)
  implicit none
  logical,intent(in)::a,b,c
  integer(16),intent(in)::num
  integer::res
  res=0
  if(num<=N)then
    if(a.and.b.and.c)res=1
    res = res + dfs(.true., b, c, num*10 + 7)
    res = res + dfs( a,.true., c, num*10 + 5)
    res = res + dfs( a, b,.true., num*10 + 3)
  endif
  return
end function
end


program speedrun
  implicit none
  integer::N
  integer,allocatable,dimension(:)::a
  integer::ANS
  real(8)::AVE,L
  integer::i
  read*,N
  allocate(a(N))
  read*,a
  AVE=real(SUM(A))/real(N)
  L=huge(L)
  do i=N,1,-1
    if(ABS(A(i)-AVE)<=L)then
        ANS=i-1
        L=ABS(A(i)-AVE)
    endif
end do
print"(i0)",ANS
end program speedrun

Score : 200 points

Problem Statement

Niwango-kun is an employee of Dwango Co., Ltd.

One day, he is asked to generate a thumbnail from a video a user submitted.

To generate a thumbnail, he needs to select a frame of the video according to the following procedure:

Get an integer N and N integers a_0, a_1, ..., a_{N-1} as inputs. N denotes the number of the frames of the video, and each a_i denotes the representation of the i-th frame of the video.

Select t-th frame whose representation a_t is nearest to the average of all frame representations.

If there are multiple such frames, select the frame with the smallest index.

Find the index t of the frame he should select to generate a thumbnail.

Constraints

1 \leq N \leq 100

1 \leq a_i \leq 100

All numbers given in input are integers

Input

Input is given from Standard Input in the following format:

N
a_{0} a_{1} ... a_{N-1}

Output

Print the answer.

Sample Input 1

3
1 2 3

Sample Output 1

1

Since the average of frame representations is 2, Niwango-kun needs to select the index 1, whose representation is 2, that is, the nearest value to the average.

Sample Input 2

4
2 5 2 5

Sample Output 2

0

The average of frame representations is 3.5.

In this case, every frame has the same distance from its representation to the average.

Therefore, Niwango-kun should select index 0, the smallest index among them.


program sum_and_subarray
  implicit none
  integer :: n, k, i, m, l, r
  integer(8) :: a(1000), s(500500), u, t, x, p, v
  a = 0_8
  s = 0_8
  read(*,*) n, k
  read(*,*) a(1:n)
  u = (n*(n+1))/2
  i = 1
  do l = 1, n
    v = 0_8
    do r = l, n
      v = v + a(r)
      s(i) = v
      i = i + 1
    end do
  end do
  x = 0_8
  do t = 40_8, 1_8, -1_8
    m = 0
    p = 2_8**(t-1_8)
    do i = 1, u
      if (and(x+p,s(i)).eq.x+p) m = m + 1
    end do
    if (m.ge.k) x = x + p
  end do
  write(*,'(i0)') x
  stop
end program sum_and_subarray

Score : 400 points

Problem Statement

One day, Niwango-kun, an employee of Dwango Co., Ltd., found an integer sequence (a_1, ..., a_N) of length N.
He is interested in properties of the sequence a.

For a nonempty contiguous subsequence a_l, ..., a_r (1 \leq l \leq r \leq N) of the sequence a, its beauty is defined as a_l + ... + a_r. Niwango-kun wants to know the maximum possible value of the bitwise AND of the beauties of K nonempty contiguous subsequences among all N(N+1)/2 nonempty contiguous subsequences. (Subsequences may share elements.)

Find the maximum possible value for him.

Constraints

2 \leq N \leq 1000

1 \leq a_i \leq 10^9

1 \leq K \leq N(N+1)/2

All numbers given in input are integers

Input

Input is given from Standard Input in the following format:

N K
a_1 a_2 ... a_N

Output

Print the answer.

Sample Input 1

4 2
2 5 2 5

Sample Output 1

12

There are 10 nonempty contiguous subsequences of a. Let us enumerate them:

contiguous subsequences starting from the first element: \{2\}, \{2, 5\}, \{2, 5, 2\}, \{2, 5, 2, 5\}

contiguous subsequences starting from the second element: \{5\}, \{5, 2\}, \{5, 2, 5\}

contiguous subsequences starting from the third element: \{2\}, \{2, 5\}

contiguous subsequences starting from the fourth element: \{5\}

(Note that even if the elements of subsequences are equal, subsequences that have different starting indices are considered to be different.)

The maximum possible bitwise AND of the beauties of two different contiguous subsequences is 12.
This can be achieved by choosing \{5, 2, 5\} (with beauty 12) and \{2, 5, 2, 5\} (with beauty 14).

Sample Input 2

8 4
9 1 8 2 7 5 6 4

Sample Output 2

32


program atcoder
    implicit none
    integer(8) :: N,Q,k,i,j,ans,D,M,DM,qq
    integer(8),allocatable,dimension(:) :: a
    character(1000007) S
    read(*,*)N,S,Q
    allocate( a(Q) )
    read(*,*)(a(i),i=1,Q)
    do qq = 1,Q
        k = a(qq)
        ans = 0
        D = 0
        M = 0
        DM = 0
        do i = 1,k
            if(S(i:i)=='D')then
                D = D+1
            else if(S(i:i)=='M')then
                M = M+1
                DM = DM + D
            else if(S(i:i)=='C')then
                ans = ans + DM
            endif
        enddo
        do i = k+1,N
            if(S(i-K:i-K)=='D')then
                D = D-1
                DM = DM-M
            else if(S(i-K:i-K)=='M')then
                M = M-1
            end if
            if(S(i:i)=='D')then
                D = D + 1
            else if(S(i:i)=='M')then
                M = M+1
                DM = DM+D
            else if(S(i:i)=='C')then
                ans = ans + DM
            endif
        enddo
        write(*,'(I0)')ans
    end do
end program atcoder

Score : 600 points

Problem Statement

In Dwango Co., Ltd., there is a content distribution system named 'Dwango Media Cluster', and it is called 'DMC' for short.

The name 'DMC' sounds cool for Niwango-kun, so he starts to define DMC-ness of a string.

Given a string S of length N and an integer k (k \geq 3),
he defines the k-DMC number of S as the number of triples (a, b, c) of integers that satisfy the following conditions:

0 \leq a < b < c \leq N - 1

S[a] = D

S[b] = M

S[c] = C

c-a < k

Here S[a] is the a-th character of the string S. Indexing is zero-based, that is, 0 \leq a \leq N - 1 holds.

For a string S and Q integers k_0, k_1, ..., k_{Q-1}, calculate the k_i-DMC number of S for each i (0 \leq i \leq Q-1).

Constraints

3 \leq N \leq 10^6

S consists of uppercase English letters

1 \leq Q \leq 75

3 \leq k_i \leq N

All numbers given in input are integers

Input

Input is given from Standard Input in the following format:

N
S
Q
k_{0} k_{1} ... k_{Q-1}

Output

Print Q lines.
The i-th line should contain the k_i-DMC number of the string S.

Sample Input 1

18
DWANGOMEDIACLUSTER
1
18

Sample Output 1

1

(a,b,c) = (0, 6, 11) satisfies the conditions.

Strangely, Dwango Media Cluster does not have so much DMC-ness by his definition.

Sample Input 2

18
DDDDDDMMMMMCCCCCCC
1
18

Sample Output 2

210

The number of triples can be calculated as 6\times 5\times 7.

Sample Input 3

54
DIALUPWIDEAREANETWORKGAMINGOPERATIONCORPORATIONLIMITED
3
20 30 40

Sample Output 3

0
1
2

(a, b, c) = (0, 23, 36), (8, 23, 36) satisfy the conditions except the last one, namely, c-a < k_i.

By the way, DWANGO is an acronym for "Dial-up Wide Area Network Gaming Operation".

Sample Output 4

30
DMCDMCDMCDMCDMCDMCDMCDMCDMCDMC
4
5 10 15 20

Sample Output 4

10
52
110
140


program a
	integer X,Y
    read(*,*) X,Y
    write(*,*) X+Y/2

end program a

Score: 100 points

Problem Statement

There is a train going from Station A to Station B that costs X yen (the currency of Japan).

Also, there is a bus going from Station B to Station C that costs Y yen.

Joisino got a special ticket. With this ticket, she can take the bus for half the fare if she travels from Station A to Station B by train and then travels from Station B to Station C by bus.

How much does it cost to travel from Station A to Station C if she uses this ticket?

Constraints

1 \leq X,Y \leq 100

Y is an even number.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

If it costs x yen to travel from Station A to Station C, print x.

Sample Input 1

81 58

Sample Output 1

110

The train fare is 81 yen.

The train fare is 58 ⁄ 2=29 yen with the 50% discount.

Thus, it costs 110 yen to travel from Station A to Station C.

Sample Input 2

4 54

Sample Output 2

31


program jihanki
  implicit none
  integer :: x,y

  read(*,*)x,y
  write(*,*)x+y/2

end program jihanki


program prob2
    implicit none
    integer(8)::n, i, j, t, a, ans, tmp
    integer(8), allocatable::h(:)
    read(*,*) n, t, a
    a = a * 1000
    t = t * 1000
    tmp = 1e9
    allocate(h(n))
    read(*,*) h
    do i = 1, n
        if(abs(t - h(i) * 6 - a) < tmp) then
            ans = i
            tmp = abs(t - h(i) * 6 - a)
        end if
    end do
    print*, ans
    stop
end program

Score: 200 points

Problem Statement

A country decides to build a palace.

In this country, the average temperature of a point at an elevation of x meters is T-x \times 0.006 degrees Celsius.

There are N places proposed for the place. The elevation of Place i is H_i meters.

Among them, Princess Joisino orders you to select the place whose average temperature is the closest to A degrees Celsius, and build the palace there.

Print the index of the place where the palace should be built.

It is guaranteed that the solution is unique.

Constraints

1 \leq N \leq 1000

0 \leq T \leq 50

-60 \leq A \leq T

0 \leq H_i \leq 10^5

All values in input are integers.

The solution is unique.

Input

Input is given from Standard Input in the following format:

N
T A
H_1 H_2 ... H_N

Output

Print the index of the place where the palace should be built.

Sample Input 1

2
12 5
1000 2000

Sample Output 1

1

The average temperature of Place 1 is 12-1000 \times 0.006=6 degrees Celsius.

The average temperature of Place 2 is 12-2000 \times 0.006=0 degrees Celsius.

Thus, the palace should be built at Place 1.

Sample Input 2

3
21 -11
81234 94124 52141

Sample Output 2

3


program main
   implicit none
   integer n,i,j
   real:: t,a,d=10000,re
   real,allocatable,dimension(:)::h
   reaD(*,*)n,t,a
   allocate(h(1:n))
   read(*,*)h(1:n)
   do i=1,n
      re=t-h(i)*0.006
      if(abs(re-a)<d)then
        d=abs(re-a)
        j=i
      endif
   enddo
   write(*,*)j
end program main


program main
implicit None
	integer::n,i,t1,a1,k
	real::a,t
	real,allocatable::h(:)

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

	read *,h

	h = a - t + h*0.006

	k = 1
	do i = 1,n
		if(abs(h(i)) < abs(h(k))) k = i
	enddo

	print 101,k

101 format(i0)
end program main


integer(16)::n,m,i,j,q,r
integer(16),allocatable::p(:),y(:),a(:),b(:),c(:)
read*,n,m
allocate(p(m),y(m),a(m))
do i = 1,m
  read*,p(i),y(i)
  a(i) = i
end do

call HeapsortTri(M,p,a,y)
q = 1;r = 1
do
  do
    if(r+1 > m)exit
    if(p(r+1)/=p(r))exit
	r = r+1
  end do
  j = r-q+1
  allocate(b(j),c(j))
  b(1:j) = y(q:r)
  c(1:j) = a(q:r)

  call HeapsortPair(J,b,c)

  do i = 1,j
    b(i) = i
  enddo
  y(q:r) = b(1:j)
  a(q:r) = c(1:j)
  q = r+1
  r = r+1
  deallocate(b,c)
  if(r>m)exit
end do

call heapsorttri(m,a,p,y)

do i = 1,m
  print"(i12.12)",p(i)*1000000+y(i)
end do

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

  l=n/2+1
  k=n
  do while(k /= 1)
     if(l > 1)then
        l=l-1
        t=array(L)
        t2=array2(L)
        t3=array3(L)
     else
        t=array(k)
        t2=array2(k)
        t3=array3(k)
        array(k)=array(1)
        array2(k)=array2(1)
        array3(k)=array3(1)
        k=k-1
        if(k == 1) then
           array(1)=t
           array2(1)=t2
           array3(1)=t3
           exit
        endif
     endif
     i=l
     j=l+l
     do while(j<=k)
        if(j < k)then
           if(array(j) < array(j+1))j=j+1
        endif
        if (t < array(j))then
           array(i)=array(j)
           array2(i)=array2(j)
           array3(i)=array3(j)
           i=j
           j=j+j
        else
           j=k+1
        endif
     enddo
     array(i)=t
     array2(i)=t2
     array3(i)=t3
  enddo
  return
end subroutine HeapsortTri

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

  l=n/2+1
  k=n
  do while(k /= 1)
     if(l > 1)then
        l=l-1
        t=array(L)
        t2=array2(L)
     else
        t=array(k)
        t2=array2(k)
        array(k)=array(1)
        array2(k)=array2(1)
        k=k-1
        if(k == 1) then
           array(1)=t
           array2(1)=t2
           exit
        endif
     endif
     i=l
     j=l+l
     do while(j<=k)
        if(j < k)then
           if(array(j) < array(j+1))j=j+1
        endif
        if (t < array(j))then
           array(i)=array(j)
           array2(i)=array2(j)
           i=j
           j=j+j
        else
           j=k+1
        endif
     enddo
     array(i)=t
     array2(i)=t2
  enddo
  return
end subroutine HeapsortPair
end

Score: 300 points

Problem Statement

In Republic of Atcoder, there are N prefectures, and a total of M cities that belong to those prefectures.

City i is established in year Y_i and belongs to Prefecture P_i.

You can assume that there are no multiple cities that are established in the same year.

It is decided to allocate a 12-digit ID number to each city.

If City i is the x-th established city among the cities that belong to Prefecture i, the first six digits of the ID number of City i is P_i, and the last six digits of the ID number is x.

Here, if P_i or x (or both) has less than six digits, zeros are added to the left until it has six digits.

Find the ID numbers for all the cities.

Note that there can be a prefecture with no cities.

Constraints

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq P_i \leq N

1 \leq Y_i \leq 10^9

Y_i are all different.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
P_1 Y_1
:
P_M Y_M

Output

Print the ID numbers for all the cities, in ascending order of indices (City 1, City 2, ...).

Sample Input 1

2 3
1 32
2 63
1 12

Sample Output 1

000001000002
000002000001
000001000001

As City 1 is the second established city among the cities that belong to Prefecture 1, its ID number is 000001000002.

As City 2 is the first established city among the cities that belong to Prefecture 2, its ID number is 000002000001.

As City 3 is the first established city among the cities that belong to Prefecture 1, its ID number is 000001000001.

Sample Input 2

2 3
2 55
2 77
2 99

Sample Output 2

000002000001
000002000002
000002000003


module mod_t_list
  implicit none
  type :: t_list
    integer :: vol
    integer, pointer :: arr(:)
  end type t_list
  private
  public :: t_list, init_list, release_list, push_back
  public :: pop_back, poll_back, get_at, size_of_list
contains
  subroutine init_list(list)
    implicit none
    type(t_list), intent(inout) :: list
    list%vol = 0
    nullify(list%arr)
    allocate(list%arr(1))
    return
  end subroutine init_list
  subroutine release_list(list)
    implicit none
    type(t_list), intent(inout) :: list
    if (associated(list%arr)) deallocate(list%arr)
    return
  end subroutine release_list
  subroutine push_back(list,n)
    implicit none
    type(t_list), intent(inout) :: list
    integer, intent(in) :: n
    integer, allocatable :: tmp(:)
    if (size(list%arr).eq.list%vol) then
      allocate(tmp(list%vol))
      tmp = list%arr
      deallocate(list%arr)
      allocate(list%arr(2*list%vol))
      list%arr(1:list%vol) = tmp
      deallocate(tmp)
    end if
    list%vol = list%vol+1
    list%arr(list%vol) = n
    return
  end subroutine push_back
  function pop_back(list) result(n)
    implicit none
    type(t_list), intent(inout) :: list
    integer :: n
    n = list%arr(list%vol)
    list%vol = list%vol-1
    return
  end function pop_back
  subroutine poll_back(list)
    implicit none
    type(t_list), intent(inout) :: list
    list%vol = list%vol-1
    return
  end subroutine poll_back
  function get_at(list,i) result(n)
    implicit none
    type(t_list), intent(in) :: list
    integer, intent(in) :: i
    integer :: n
    n = list%arr(i)
    return
  end function get_at
  function size_of_list(list) result(s)
    implicit none
    type(t_list), intent(in) :: list
    integer :: s
    s = list%vol
    return
  end function size_of_list
end module mod_t_list
program crossing
  use mod_t_list
  implicit none
  type(t_list) :: ilist(0:400), jlist(0:400)
  integer :: n, m, i, j, s
  s = 0
  read(*,*) n
  m = (nint(sqrt(8.d0*real(n,8)+1.d0))-1)/2
  if (n.ne.m*(m+1)/2) then
    write(*,'(a)') "No"
    stop
  end if
  do i = 0, 400
    call init_list(ilist(i))
    call init_list(jlist(i))
  end do
  write(*,'(a)') "Yes"
  write(*,'(i0)') m+1
  s = 0
  write(*,'(i0)',advance='no') m
  do i = 1, m
    do j = 1, i
      call push_back(ilist(i),s+j)
      if (j.ne.i) call push_back(jlist(j),s+j)
    end do
    s = s+i
    write(*,'(x,i0)',advance='no') s
  end do
  write(*,*)
  do i = 1, m
    write(*,'(i0)',advance='no') m
    do j = 1, size_of_list(ilist(i))
      write(*,'(x,i0)',advance='no') get_at(ilist(i),j)
    end do
    do j = 1, size_of_list(jlist(i))
      write(*,'(x,i0)',advance='no') get_at(jlist(i),j)
    end do
    write(*,*)
  end do
  do i = 0, 400
    call release_list(ilist(i))
    call release_list(jlist(i))
  end do
  stop
end program crossing

Score : 500 points

Problem Statement

You are given an integer N. Determine if there exists a tuple of subsets of \{1,2,...N\}, (S_1,S_2,...,S_k), that satisfies the following conditions:

Each of the integers 1,2,...,N is contained in exactly two of the sets S_1,S_2,...,S_k.

Any two of the sets S_1,S_2,...,S_k have exactly one element in common.

If such a tuple exists, construct one such tuple.

Constraints

1 \leq N \leq 10^5

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If a tuple of subsets of \{1,2,...N\} that satisfies the conditions does not exist, print No.
If such a tuple exists, print Yes first, then print such subsets in the following format:

k
|S_1| S_{1,1} S_{1,2} ... S_{1,|S_1|}
:
|S_k| S_{k,1} S_{k,2} ... S_{k,|S_k|}

where S_i={S_{i,1},S_{i,2},...,S_{i,|S_i|}}.

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

Sample Input 1

3

Sample Output 1

Yes
3
2 1 2
2 3 1
2 2 3

It can be seen that (S_1,S_2,S_3)=(\{1,2\},\{3,1\},\{2,3\}) satisfies the conditions.

Sample Input 2

4

Sample Output 2

No


program equilateral
  implicit none
  integer :: h, w, a(0:600,0:600) = 0, i, j, k, d
  integer :: x(0:600,0:600) = 0, y(0:600,0:600) = 0
  integer(8) :: ans = 0_8
  character(300) :: s
  read(*,*) h, w
  do i = 1, h
    read(*,*) s
    s = trim(adjustl(s))
    do j = 1, w
      if (s(j:j) == "#") a(i+j-1,i-j+w) = 1
    end do
  end do
  do i = 1, h+w
    do j = 1, h+w
      x(i,j) = x(i,j-1)+a(i,j)
      y(i,j) = y(i-1,j)+a(i,j)
    end do
  end do
  do i = 0, h+w
    do j = 0, h+w
      do k = j+1, h+w
        d = k-j
        if (a(i,j)*a(i,k) == 1) then
          if (i-d >= 0) ans = ans+int(x(i-d,k)-x(i-d,j-1)-a(i-d,k)-a(i-d,j),8)
          if (i+d <= h+w) ans = ans+int(x(i+d,k)-x(i+d,j-1)-a(i+d,k)-a(i+d,j),8)
        end if
        if (a(j,i)*a(k,i) == 1) then
          if (i-d >= 0) ans = ans+int(y(k,i-d)-y(j-1,i-d),8)
          if (i+d <= h+w) ans = ans+int(y(k,i+d)-y(j-1,i+d),8)
        end if
      end do
    end do
  end do
  write(*,'(i0)') ans
  stop
end program equilateral

Score : 700 points

Problem Statement

There are some coins in the xy-plane.
The positions of the coins are represented by a grid of characters with H rows and W columns.
If the character at the i-th row and j-th column, s_{ij}, is #, there is one coin at point (i,j); if that character is ., there is no coin at point (i,j). There are no other coins in the xy-plane.

There is no coin at point (x,y) where 1\leq i\leq H,1\leq j\leq W does not hold.
There is also no coin at point (x,y) where x or y (or both) is not an integer.
Additionally, two or more coins never exist at the same point.

Find the number of triples of different coins that satisfy the following condition:

Choosing any two of the three coins would result in the same Manhattan distance between the points where they exist.

Here, the Manhattan distance between points (x,y) and (x',y') is |x-x'|+|y-y'|.
Two triples are considered the same if the only difference between them is the order of the coins.

Constraints

1 \leq H,W \leq 300

s_{ij} is # or ..

Input

Input is given from Standard Input in the following format:

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

Output

Print the number of triples that satisfy the condition.

Sample Input 1

5 4
#.##
.##.
#...
..##
...#

Sample Output 1

3

((1,1),(1,3),(2,2)),((1,1),(2,2),(3,1)) and ((1,3),(3,1),(4,4)) satisfy the condition.

Sample Input 2

13 27
......#.........#.......#..
#############...#.....###..
..............#####...##...
...#######......#...#######
...#.....#.....###...#...#.
...#######....#.#.#.#.###.#
..............#.#.#...#.#..
#############.#.#.#...###..
#...........#...#...#######
#..#######..#...#...#.....#
#..#.....#..#...#...#.###.#
#..#######..#...#...#.#.#.#
#..........##...#...#.#####

Sample Output 2

870


program prob1
    implicit none
    character(len=3)::S,T
    integer::l
    read(*,*) S
    l = len_trim(S)
    if(l == 2) then
        write(*,'(a)') trim(S)
    else
        T(1:1) = S(3:3)
        T(2:2) = S(2:2)
        T(3:3) = S(1:1)
        write(*,'(a)') trim(T)
    end if
    stop
end program

Score : 100 points

Problem Statement

You are given a string S of length 2 or 3 consisting of lowercase English letters. If the length of the string is 2, print it as is; if the length is 3, print the string after reversing it.

Constraints

The length of S is 2 or 3.

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If the length of S is 2, print S as is; if the length is 3, print S after reversing it.

Sample Input 1

abc

Sample Output 1

cba

As the length of S is 3, we print it after reversing it.

Sample Input 2

ac

Sample Output 2

ac

As the length of S is 2, we print it as is.


program exchange
  implicit none
  integer :: a, b, k, i
  read(*,*) a, b, k
  do i = 1, k
    a = (a-mod(a,2))/2
    b = b+a
    call swap(a,b)
  end do
  if (mod(k,2) == 1) call swap(a,b)
  write(*,'(i0,x,i0)') a, b
contains
  subroutine swap(a,b)
    integer, intent(inout) :: a, b
    a = xor(a,b)
    b = xor(a,b)
    a = xor(a,b)
  end
end program exchange

Score : 200 points

Problem Statement

In the beginning, Takahashi has A cookies, and Aoki has B cookies.
They will perform the following operation alternately, starting from Takahashi:

If the number of cookies in his hand is odd, eat one of those cookies; if the number is even, do nothing. Then, give one-half of the cookies in his hand to the other person.

Find the numbers of cookies Takahashi and Aoki respectively have after performing K operations in total.

Constraints

1 \leq A,B \leq 10^9

1 \leq K \leq 100

A,B and K are integers.

Input

Input is given from Standard Input in the following format:

A B K

Output

Print the number of cookies Takahashi has, and the number of cookies Aoki has, in this order, after performing K operations in total.

Sample Input 1

5 4 2

Sample Output 1

5 3

The process will go as follows:

In the beginning, Takahashi and Aoki have 5 and 4 cookies, respectively.

Takahashi eats one cookie and gives two cookies to Aoki. They now have 2 and 6 cookies, respectively.

Aoki gives three cookies to Takahashi. They now have 5 and 3 cookies, respectively.

Sample Input 2

3 3 3

Sample Output 2

1 3

Sample Input 3

314159265 358979323 84

Sample Output 3

448759046 224379523


program main
integer :: n,t,m=10000
integer,allocatable :: c(:),t2(:)
integer :: i
read(*,*)n,t
allocate(c(n),t2(n))
do i=1,n
	read(*,*)c(i),t2(i)
end do

do i=1,n
	if (t2(i) <= t) then
    	m = min(m,c(i))
    end if
end do
if (m == 10000) then
	write(*,*)'TLE'
else
	write(*,*)m
end if
end program

Score : 200 points

Problem Statement

When Mr. X is away from home, he has decided to use his smartwatch to search the best route to go back home, to participate in ABC.

You, the smartwatch, has found N routes to his home.

If Mr. X uses the i-th of these routes, he will get home in time t_i at cost c_i.

Find the smallest cost of a route that takes not longer than time T.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq T \leq 1000

1 \leq c_i \leq 1000

1 \leq t_i \leq 1000

The pairs (c_i, t_i) are distinct.

Input

Input is given from Standard Input in the following format:

N T
c_1 t_1
c_2 t_2
:
c_N t_N

Output

Print the smallest cost of a route that takes not longer than time T.

If there is no route that takes not longer than time T, print TLE instead.

Sample Input 1

3 70
7 60
1 80
4 50

Sample Output 1

4

The first route gets him home at cost 7.

The second route takes longer than time T = 70.

The third route gets him home at cost 4.

Thus, the cost 4 of the third route is the minimum.

Sample Input 2

4 3
1 1000
2 4
3 1000
4 500

Sample Output 2

TLE

There is no route that takes not longer than time T = 3.

Sample Input 3

5 9
25 8
5 9
4 10
1000 1000
6 1

Sample Output 3

5


program abc112_d
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: n,m
    integer(int32):: i

    read*, n,m
    do i=m/n,1,-1
        if (mod(m,i) == 0) then
            print'(i0)', i
            stop
        end if
    end do
end program abc112_d

Score : 400 points

Problem Statement

You are given integers N and M.

Consider a sequence a of length N consisting of positive integers such that a_1 + a_2 + ... + a_N = M. Find the maximum possible value of the greatest common divisor of a_1, a_2, ..., a_N.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

N \leq M \leq 10^9

Input

Input is given from Standard Input in the following format:

N M

Output

Print the maximum possible value of the greatest common divisor of a sequence a_1, a_2, ..., a_N that satisfies the condition.

Sample Input 1

3 14

Sample Output 1

2

Consider the sequence (a_1, a_2, a_3) = (2, 4, 8). Their greatest common divisor is 2, and this is the maximum value.

Sample Input 2

10 123

Sample Output 2

3

Sample Input 3

100000 1000000000

Sample Output 3

10000


program test
implicit none

integer(8) :: n,m,i,j,k,sosuu(32000)=1,temp,sosuu_list(32000)=0,soinsuu_list(32000)=0,count,rest,ans=1,value
integer(8) :: sosuu_list2(32000),soinsuu_list2(32000),count2,bits(32000)=0
read(*,*) n,m

! まず、Mを素因数分解をする
! 次に、N以上を満たす最小のMの約数を探しYとする
! 答えはM/Y

! 素数判定
sosuu(1)=0
do i=2,32000
	if(sosuu(i) == 0)then
		goto 100
	else
		temp = 2*i
		do while(temp <= 32000)
			sosuu(temp) = 0
			temp = temp + i
		enddo
	endif
	100 continue
enddo

! 素数リスト
count=0
do i=2,32000
	if(sosuu(i) == 1)then
		count = count + 1
		sosuu_list(count) = i
	endif
enddo

! 素因数分解をする
rest = m
do i=1,count
	do while(mod(rest,sosuu_list(i))==0)
		rest = rest/sosuu_list(i)
		soinsuu_list(i) = soinsuu_list(i) + 1
	enddo
enddo

count2=0
do i=1,count
	if(soinsuu_list(i) >= 1)then
		count2 = count2 + 1
		sosuu_list2(count2) = sosuu_list(i)
		soinsuu_list2(count2) = soinsuu_list(i)
	endif
enddo

! 最後に残ったrestが素数かもしれない
if(rest >= 2)then
	count2 = count2 + 1
	sosuu_list2(count2) = rest
	soinsuu_list2(count2) = 1
endif

if(m >= 2) then
do
	value = 1
	do i=1,count2
		value = value * sosuu_list2(i) ** bits(i)
	enddo

	if(M/value >= N )then
		ans = max(ans,value)
	endif

	bits(1) = bits(1) + 1
	do i=1,count2
		if(bits(i) > soinsuu_list2(i)) then
			if(i == count2)then
				goto 200
			else
				bits(i) = 0
				bits(i+1) = bits(i+1) + 1
			endif
		endif
	enddo

enddo
200 continue
endif

write(*,*) ans

end program


program sunuke911
implicit none
integer :: N,i,j,M

read(*,*) N
M=10-mod(N,10)
i=(mod(N,100)-mod(N,10))/10
j=N/100
M=M+(10-i)*10+(10-j)*100

write(*,'(i0)') M

end program sunuke911

Score : 100 points

Problem Statement

Cat Snuke is learning to write characters.
Today, he practiced writing digits 1 and 9, but he did it the other way around.

You are given a three-digit integer n written by Snuke.
Print the integer obtained by replacing each digit 1 with 9 and each digit 9 with 1 in n.

Constraints

111 \leq n \leq 999

n is an integer consisting of digits 1 and 9.

Input

Input is given from Standard Input in the following format:

n

Output

Print the integer obtained by replacing each occurrence of 1 with 9 and each occurrence of 9 with 1 in n.

Sample Input 1

119

Sample Output 1

991

Replace the 9 in the ones place with 1, the 1 in the tens place with 9 and the 1 in the hundreds place with 9. The answer is 991.

Sample Input 2

999

Sample Output 2

111


program prob3
    implicit none
    integer::n, i, j, tmp, emax, omax, ei, oi
    integer, allocatable::v(:)
    integer::e(100000), o(100000)
    read(*,*) n
    allocate(v(n))
    read(*,*) v
    e = 0
    o = 0
    emax = 0
    omax = 0
    do i = 1, n
        tmp = v(i)
        if(mod(i,2) == 0) then
            e(tmp) = e(tmp) + 1
        else
            o(tmp) = o(tmp) + 1
        end if
    end do
    do i = 1, 100000
        if(e(i) > emax) then
            emax = e(i)
            ei = i
        end if
        if(o(i) > omax) then
            omax = o(i)
            oi = i
        end if
    end do
    if(ei /= oi) then
        write(*,*) n - emax - omax
    else
        call heapsort(100000,e)
        call heapsort(100000,o)
        write(*,*) n - max(e(99999)+o(100000), e(100000)+o(99999))
    end if
    stop
contains
    subroutine heapsort(n,array)
        implicit none
        integer,intent(in) :: n
        integer,intent(inout) :: array(1:n)

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

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

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

        return
    end subroutine heapsort
end program

Score : 300 points

Problem Statement

A sequence a_1,a_2,... ,a_n is said to be /\/\/\/ when the following conditions are satisfied:

For each i = 1,2,..., n-2, a_i = a_{i+2}.

Exactly two different numbers appear in the sequence.

You are given a sequence v_1,v_2,...,v_n whose length is even.
We would like to make this sequence /\/\/\/ by replacing some of its elements.
Find the minimum number of elements that needs to be replaced.

Constraints

2 \leq n \leq 10^5

n is even.

1 \leq v_i \leq 10^5

v_i is an integer.

Input

Input is given from Standard Input in the following format:

n
v_1 v_2 ... v_n

Output

Print the minimum number of elements that needs to be replaced.

Sample Input 1

4
3 1 3 2

Sample Output 1

1

The sequence 3,1,3,2 is not /\/\/\/, but we can make it /\/\/\/ by replacing one of its elements: for example, replace the fourth element to make it 3,1,3,1.

Sample Input 2

6
105 119 105 119 105 119

Sample Output 2

0

The sequence 105,119,105,119,105,119 is /\/\/\/.

Sample Input 3

4
1 1 1 1

Sample Output 3

2

The elements of the sequence 1,1,1,1 are all the same, so it is not /\/\/\/.


program main
	implicit none
	integer :: n
	integer, allocatable :: v(:)
	integer :: i, baketu(1:100010), ans

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

	baketu = 0
	do i = 1, n
		baketu(v(i)) = baketu(v(i)) + 1
	end do

	call heapsort(100010,baketu)

	ans = 0
	ans = n-(baketu(100010)+baketu(100010-1))
	if(n/2 - baketu(100010-1) < 0) then
		ans = ans + abs(n/2 - baketu(100010-1))
	else if(n/2 - baketu(100010) < 0) then
		ans = ans + abs(n/2 - baketu(100010))
	end if

	write(*, *) ans

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

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

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

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

		return
	end subroutine heapsort
end program main


program prob1
    implicit none
    integer::k
    read*,k
    if(mod(k,2) == 0) then
        print*, k/2*k/2
    else
        print*, k/2*(k/2+1)
    end if
    stop
end program

Score : 100 points

Problem Statement

Find the number of ways to choose a pair of an even number and an odd number from the positive integers between 1 and K (inclusive). The order does not matter.

Constraints

2\leq K\leq 100

K is an integer.

Input

Input is given from Standard Input in the following format:

K

Output

Print the number of ways to choose a pair of an even number and an odd number from the positive integers between 1 and K (inclusive).

Sample Input 1

3

Sample Output 1

2

Two pairs can be chosen: (2,1) and (2,3).

Sample Input 2

6

Sample Output 2

9

Sample Input 3

11

Sample Output 3

30

Sample Input 4

50

Sample Output 4

625


program sample
    implicit none
    character(100)::s,t
    integer(8) :: i,j,k,m,n
    integer(8)::a,b,c,d
    integer(8),allocatable::x(:),y(:)

    read(*,*) a
    if(mod(a,2)==0)then
        write(*,*)(a/2)**2
    else
        write(*,*)a/2*(a/2+1)
    endif
    stop
end program sample


program a20829

implicit none
real :: k

read *, k

print *, int(k/2) * nint(k/2)

end program a20829


program prob2
    implicit none
    integer::x1, x2, x3, x4, y1, y2, y3, y4, dx, dy
    read(*,*) x1, y1, x2, y2
    dx = x2 - x1
    dy = y2 - y1
    if(dx >= 0 .and. dy >= 0) then !-+
        x3 = x2 - dy
        y3 = y2 + dx
    else if(dx >= 0 .and. dy <= 0) then !++
        x3 = x2 - dy
        y3 = y2 + dx
    else if(dx <= 0 .and. dy >= 0) then !--
        x3 = x2 - dy
        y3 = y2 + dx
    else !+-
        x3 = x2 - y2
        y3 = y2 + x2
    end if
    x4 = x3 - dx
    y4 = y3 - dy
    write(*,*) x3, y3, x4, y4
    stop
end program

Score : 200 points

Problem Statement

There is a square in the xy-plane. The coordinates of its four vertices are (x_1,y_1),(x_2,y_2),(x_3,y_3) and (x_4,y_4) in counter-clockwise order.
(Assume that the positive x-axis points right, and the positive y-axis points up.)

Takahashi remembers (x_1,y_1) and (x_2,y_2), but he has forgot (x_3,y_3) and (x_4,y_4).

Given x_1,x_2,y_1,y_2, restore x_3,y_3,x_4,y_4. It can be shown that x_3,y_3,x_4 and y_4 uniquely exist and have integer values.

Constraints

|x_1|,|y_1|,|x_2|,|y_2| \leq 100

(x_1,y_1) ≠ (x_2,y_2)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

x_1 y_1 x_2 y_2

Output

Print x_3,y_3,x_4 and y_4 as integers, in this order.

Sample Input 1

0 0 0 1

Sample Output 1

-1 1 -1 0

(0,0),(0,1),(-1,1),(-1,0) is the four vertices of a square in counter-clockwise order.
Note that (x_3,y_3)=(1,1),(x_4,y_4)=(1,0) is not accepted, as the vertices are in clockwise order.

Sample Input 2

2 3 6 6

Sample Output 2

3 10 -1 7

Sample Input 3

31 -41 -59 26

Sample Output 3

-126 -64 -36 -131


program train
  implicit none
  integer :: n, i
  read(*,*) n, i
  write(*,'(i0)') n-i+1
end program train

Score : 100 points

Problem Statement

There is an N-car train.

You are given an integer i. Find the value of j such that the following statement is true: "the i-th car from the front of the train is the j-th car from the back."

Constraints

1 \leq N \leq 100

1 \leq i \leq N

Input

Input is given from Standard Input in the following format:

N i

Output

Print the answer.

Sample Input 1

4 2

Sample Output 1

3

The second car from the front of a 4-car train is the third car from the back.

Sample Input 2

1 1

Sample Output 2

1

Sample Input 3

15 11

Sample Output 3

5


program main
  implicit none
  integer :: n, i
  read(*, *) n, i
  write(*, "(i0)") n - i + 1
end program main


program abc107c
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: n,k,i,l,r,ans
    integer(int32), allocatable:: x(:)

    read*, n,k
    allocate(x(n))
    read*, x(:)
    ans = 200000000
    do i=1,n-k+1
        l = x(i)
        r = x(i+k-1)
        ans = min(ans,abs(l)+abs(r-l), abs(r)+abs(r-l))
    end do
    print'(i0)', ans
end program abc107c

Score : 300 points

Problem Statement

There are N candles placed on a number line.
The i-th candle from the left is placed on coordinate x_i.
Here, x_1 < x_2 < ... < x_N holds.

Initially, no candles are burning.
Snuke decides to light K of the N candles.

Now, he is at coordinate 0.
He can move left and right along the line with speed 1.
He can also light a candle when he is at the same position as the candle, in negligible time.

Find the minimum time required to light K candles.

Constraints

1 \leq N \leq 10^5

1 \leq K \leq N

x_i is an integer.

|x_i| \leq 10^8

x_1 < x_2 < ... < x_N

Input

Input is given from Standard Input in the following format:

N K
x_1 x_2 ... x_N

Output

Print the minimum time required to light K candles.

Sample Input 1

5 3
-30 -10 10 20 50

Sample Output 1

40

He should move and light candles as follows:

Move from coordinate 0 to -10.

Light the second candle from the left.

Move from coordinate -10 to 10.

Light the third candle from the left.

Move from coordinate 10 to 20.

Light the fourth candle from the left.

Sample Input 2

3 2
10 20 30

Sample Output 2

20

Sample Input 3

1 1
0

Sample Output 3

0

There may be a candle placed at coordinate 0.

Sample Input 4

8 5
-9 -7 -4 -3 1 2 3 4

Sample Output 4

10


implicit none
integer :: n,k
integer,allocatable :: x(:)
integer :: i,j,d,t,tmin

read *,n,k
allocate(x(n))
read *,(x(i),i=1,n)

tmin = 1000000000
j = k
do i = 1,n-k+1
   d = abs(x(j) - x(i))
   t = min(abs(x(i)) + d, abs(x(j)) + d)
   if (t < tmin) then
      tmin = t
   endif
   j = j + 1
enddo

print '(i0)',tmin
end


program main
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: a,b
    read*, a,b
    print'(i0)', (a-1)*(b-1)
end program main

Score: 100 points

Problem Statement

There is a farm whose length and width are A yard and B yard, respectively. A farmer, John, made a vertical road and a horizontal road inside the farm from one border to another, as shown below: (The gray part represents the roads.)

What is the area of this yard excluding the roads? Find it.

Note

It can be proved that the positions of the roads do not affect the area.

Constraints

A is an integer between 2 and 100 (inclusive).

B is an integer between 2 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

A B

Output

Print the area of this yard excluding the roads (in square yards).

Sample Input 1

2 2

Sample Output 1

1

In this case, the area is 1 square yard.

Sample Input 2

5 7

Sample Output 2

24

In this case, the area is 24 square yards.


program sample
  implicit none
  integer :: n,i,j,count8
  integer,allocatable :: count(:)

  read(*,*) n
  allocate(count(n))

  count=0
  count8=0
  count(1)=1
  do i=2,n
     do j=1,i
        if(mod(i,j)==0)then
           count(i)=count(i)+1
        endif
     enddo
     if(count(i)==8.and.mod(i,2)==1)then
        count8=count8+1
     endif
  enddo

  write(*,*) count8

  stop

end program sample

Score: 200 points

Problem Statement

The number 105 is quite special - it is odd but still it has eight divisors.
Now, your task is this: how many odd numbers with exactly eight positive divisors are there between 1 and N (inclusive)?

Constraints

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

Input

Input is given from Standard Input in the following format:

N

Output

Print the count.

Sample Input 1

105

Sample Output 1

1

Among the numbers between 1 and 105, the only number that is odd and has exactly eight divisors is 105.

Sample Input 2

7

Sample Output 2

0

1 has one divisor. 3, 5 and 7 are all prime and have two divisors. Thus, there is no number that satisfies the condition.


   program abc106b
      implicit none
      integer::n
      integer::i,j,count,ans

         read *, n

         ans=0
         do i=1,n,2
            count=0
            do j=1,n
               if (mod(n,j)==0) count=count+1
            end do
            if (count==8) ans=ans+1
         end do

         print *, ans

   end program abc106b


program main
  implicit none

  integer :: i,n,j,c,cc=0

  read(*,*)n

  do i = 1, n, 2
    c = 0
    do j = 1, n
      if (mod(i,j) == 0) then
        c = c + 1
      end if
    end do
    if (c == 8) then
      cc = cc + 1
    end if
  end do
  write(*,*)cc
end program main


INTEGER N,ANS
READ*,N
IF (N<3*5*7) THEN
  PRINT"(I0)",0
ELSE IF (N<3*5*11) THEN
  PRINT"(I0)",1
ELSE IF (N<3*5*13) THEN
  PRINT"(I0)",2
ELSE
  PRINT"(I0)",3
ENDIF
END


program prime_numbers
    implicit none
    integer(8) i, j,n,m,Q,l,r,y,p1,q1
    integer, allocatable :: x(:,:),p(:,:)

    read (*,*)n ,m,q
    allocate(x(2,m))
    allocate(p(2,q))
    read(*,*)x
    read(*,*)p
    !transpose(x)
    !transpose(p)
    do i=1,q
        y=0
        p1=p(1,i)
        q1=p(2,i)
        do j=1,m
          if (p1<=x(1,j) .and. x(2,j)<=q1)then
            y=y+1
          end if
        end do
        write(*,*)y
    end do
    deallocate(x)
    deallocate(p)

    stop
end program prime_numbers

Score: 400 points

Problem Statement

In Takahashi Kingdom, there is a east-west railroad and N cities along it, numbered 1, 2, 3, ..., N from west to east.
A company called AtCoder Express possesses M trains, and the train i runs from City L_i to City R_i (it is possible that L_i = R_i).
Takahashi the king is interested in the following Q matters:

The number of the trains that runs strictly within the section from City p_i to City q_i, that is, the number of trains j such that p_i \leq L_j and R_j \leq q_i.

Although he is genius, this is too much data to process by himself. Find the answer for each of these Q queries to help him.

Constraints

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

M is an integer between 1 and 200 \ 000 (inclusive).

Q is an integer between 1 and 100 \ 000 (inclusive).

1 \leq L_i \leq R_i \leq N (1 \leq i \leq M)

1 \leq p_i \leq q_i \leq N (1 \leq i \leq Q)

Input

Input is given from Standard Input in the following format:

N M Q
L_1 R_1
L_2 R_2
:
L_M R_M
p_1 q_1
p_2 q_2
:
p_Q q_Q

Output

Print Q lines. The i-th line should contain the number of the trains that runs strictly within the section from City p_i to City q_i.

Sample Input 1

2 3 1
1 1
1 2
2 2
1 2

Sample Output 1

3

As all the trains runs within the section from City 1 to City 2, the answer to the only query is 3.

Sample Input 2

10 3 2
1 5
2 8
7 10
1 7
3 10

Sample Output 2

1
1

The first query is on the section from City 1 to 7. There is only one train that runs strictly within that section: Train 1.
The second query is on the section from City 3 to 10. There is only one train that runs strictly within that section: Train 3.

Sample Input 3

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

Sample Output 3

7
9
10
6
8
9
6
7
8
10


program test
implicit none

integer(8) :: i,j,k,l,m,n,r,q
integer(8) :: ll(200000),rr(200000),pp(100000),qq(100000),nn(501,501)=0,nn2(0:501,0:501)=0

read(*,*) n,m,q
do i= 1,m
read(*,*) ll(i),rr(i)
enddo

do i=1,q
read(*,*) pp(i),qq(i)
enddo

do i=1,m
do j=1,ll(i)

nn(j,rr(i)) = nn(j,rr(i)) + 1

enddo
enddo

do i=1,n
do j=1,n
nn2(i,j) = nn2(i,j-1) + nn(i,j)
enddo
enddo

!do i=1,n+1
!write(*,*) nn2(i,1:n+1)
!enddo

do i=1,q
write(*,*) nn2(pp(i),qq(i))
enddo

end program


Score : 100 points

Problem Statement

Takahashi has decided to distribute N AtCoder Crackers to K users of as evenly as possible.
When all the crackers are distributed, find the minimum possible (absolute) difference between the largest number of crackers received by a user and the smallest number received by a user.

Constraints

1 \leq N,K \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the minimum possible (absolute) difference between the largest number of crackers received by a user and the smallest number received by a user.

Sample Input 1

7 3

Sample Output 1

1

When the users receive two, two and three crackers, respectively, the (absolute) difference between the largest number of crackers received by a user and the smallest number received by a user, is 1.

Sample Input 2

100 10

Sample Output 2

0

The crackers can be distributed evenly.

Sample Input 3

1 1

Sample Output 3

0


program test
implicit none
integer :: i,j,k,m,n,ans

read(*,*) n

ans = 0

do i= 0,25
do j =0,25
if(4*i + 7*j == n) then
ans =1
endif
enddo
enddo

if(ans == 1) then
write(*,*) 'Yes'
else
write(*,*) 'No'
endif

end program

Score : 200 points

Problem Statement

La Confiserie d'ABC sells cakes at 4 dollars each and doughnuts at 7 dollars each.
Determine if there is a way to buy some of them for exactly N dollars. You can buy two or more doughnuts and two or more cakes, and you can also choose to buy zero doughnuts or zero cakes.

Constraints

N is an integer between 1 and 100, inclusive.

Input

Input is given from Standard Input in the following format:

N

Output

If there is a way to buy some cakes and some doughnuts for exactly N dollars, print Yes; otherwise, print No.

Sample Input 1

11

Sample Output 1

Yes

If you buy one cake and one doughnut, the total will be 4 + 7 = 11 dollars.

Sample Input 2

40

Sample Output 2

Yes

If you buy ten cakes, the total will be 4 \times 10 = 40 dollars.

Sample Input 3

3

Sample Output 3

No

The prices of cakes (4 dollars) and doughnuts (7 dollars) are both higher than 3 dollars, so there is no such way.


program test
implicit none
integer(8) :: i,j,k,m,n,nn,ans(1000)=0,ii
character :: ans2(1000)

read(*,*) n
nn = n
i = 1
ii = 1

do
if(nn == 0) then
	goto 100
else
	if(mod(nn,-2*ii) == 0) then
		continue
	else
		ans(i) = 1
		nn = nn - ii
	endif
endif
i=i+1
ii = ii * (-2)
enddo

100 continue
if(n == 0) then
write(*,*) 0
else

write(*,'(1000(I0))') ans(i-1:1:-1)

endif

end program

Score : 300 points

Problem Statement

Given an integer N, find the base -2 representation of N.

Here, S is the base -2 representation of N when the following are all satisfied:

S is a string consisting of 0 and 1.

Unless S = 0, the initial character of S is 1.

Let S = S_k S_{k-1} ... S_0, then S_0 \times (-2)^0 + S_1 \times (-2)^1 + ... + S_k \times (-2)^k = N.

It can be proved that, for any integer M, the base -2 representation of M is uniquely determined.

Constraints

Every value in input is integer.

-10^9 \leq N \leq 10^9

Input

Input is given from Standard Input in the following format:

N

Output

Print the base -2 representation of N.

Sample Input 1

-9

Sample Output 1

1011

As (-2)^0 + (-2)^1 + (-2)^3 = 1 + (-2) + (-8) = -9, 1011 is the base -2 representation of -9.

Sample Input 2

123456789

Sample Output 2

11000101011001101110100010101

Sample Input 3

0

Sample Output 3

0


program test
implicit none
integer(8) :: i,j,k,m,n,nn,a(100000),ma(100000),suma(100000),sumb(100000),ans=0,combo=1

read(*,*) n,m
read(*,*) (a(i),i=1,n)

do i=1,n
	ma(i) = mod(a(i),m)
enddo

do i=1,n
	if(i==1)then
	suma(i) = ma(i)
	else
	suma(i) = mod(suma(i-1) + ma(i),m)
	endif
enddo

call heapsort2(n,suma(1:n),sumb(1:n))

do i=2,n
	if(suma(i) == suma(i-1)) then
		combo = combo + 1
	else
		if(suma(i-1) == 0) then
			ans = ans + i-1
		endif
		ans = ans + combo*(combo-1)/2
		combo = 1
	endif
enddo

if(combo > 1) then
		ans = ans + combo*(combo-1)/2
endif

if(suma(n) == 0) then
	ans = ans + n
endif

write(*,*) ans

contains

subroutine heapsort2(n,y1,y2)

	implicit none
	integer(8),intent(in) :: n
	integer(8),intent(inout) :: y1(1:n)
	integer(8),intent(inout) :: y2(1:n)
	integer(8) :: i,k,j,l
	integer(8) :: t1,t2

	l = n/2+1
	k = n

	do while (k /= 1)
		 if(l > 1) then
				l = l-1
				t1 = y1(l)
				t2 = y2(l)

		 else
				t1 = y1(k)
				t2 = y2(k)
				y1(k) = y1(1)
				y2(k) = y2(1)
				k = k-1
				if(k == 1) then
					 y1(1) = t1
					 y2(1) = t2
					 exit
				endif
		 endif

		 i = l
		 j = l+l

		 do while(j <= k)
				if(j < k) then
					 if(y1(j) < y1(j+1)) j = j+1
				endif

				if (t1 < y1(j)) then
					 y1(i) = y1(j)
					 y2(i) = y2(j)
					 i = j
					 j = j+j
				else
					 j = k+1
				endif

		 enddo

		 y1(i) = t1
		 y2(i) = t2

	enddo
	return

end subroutine heapsort2

end program

Score : 400 points

Problem Statement

There are N boxes arranged in a row from left to right. The i-th box from the left contains A_i candies.

You will take out the candies from some consecutive boxes and distribute them evenly to M children.

Such being the case, find the number of the pairs (l, r) that satisfy the following:

l and r are both integers and satisfy 1 \leq l \leq r \leq N.

A_l + A_{l+1} + ... + A_r is a multiple of M.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

2 \leq M \leq 10^9

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 ... A_N

Output

Print the number of the pairs (l, r) that satisfy the conditions.

Note that the number may not fit into a 32-bit integer type.

Sample Input 1

3 2
4 1 5

Sample Output 1

3

The sum A_l + A_{l+1} + ... + A_r for each pair (l, r) is as follows:

Sum for (1, 1): 4

Sum for (1, 2): 5

Sum for (1, 3): 10

Sum for (2, 2): 1

Sum for (2, 3): 6

Sum for (3, 3): 5

Among these, three are multiples of 2.

Sample Input 2

13 17
29 7 5 7 9 51 7 13 8 55 42 9 81

Sample Output 2

6

Sample Input 3

10 400000000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 3

25


program test
implicit none
integer(8) :: i,j,k,m,n,nn,a(100000),ma(100000),suma(100000),sumb(100000),ans=0,combo=1

read(*,*) n,m
read(*,*) (a(i),i=1,n)

do i=1,n
	ma(i) = mod(a(i),m)
enddo

do i=1,n
	if(i==1)then
	suma(i) = ma(i)
	else
	suma(i) = mod(suma(i-1) + ma(i),m)
	endif
enddo

call heapsort2(n,suma(1:n),sumb(1:n))

do i=2,n
	if(suma(i) == suma(i-1)) then
		combo = combo + 1
	else
		if(suma(i-1) == 0) then
			ans = ans + i-1
		endif
		ans = ans + combo*(combo-1)/2
		combo = 1
	endif
enddo

if(combo > 1) then
		ans = ans + combo*(combo-1)/2
endif

if(suma(n) == 0) then
	ans = ans + i
endif

write(*,*) ans

contains

subroutine heapsort2(n,y1,y2)

	implicit none
	integer(8),intent(in) :: n
	integer(8),intent(inout) :: y1(1:n)
	integer(8),intent(inout) :: y2(1:n)
	integer(8) :: i,k,j,l
	integer(8) :: t1,t2

	l = n/2+1
	k = n

	do while (k /= 1)
		 if(l > 1) then
				l = l-1
				t1 = y1(l)
				t2 = y2(l)

		 else
				t1 = y1(k)
				t2 = y2(k)
				y1(k) = y1(1)
				y2(k) = y2(1)
				k = k-1
				if(k == 1) then
					 y1(1) = t1
					 y2(1) = t2
					 exit
				endif
		 endif

		 i = l
		 j = l+l

		 do while(j <= k)
				if(j < k) then
					 if(y1(j) < y1(j+1)) j = j+1
				endif

				if (t1 < y1(j)) then
					 y1(i) = y1(j)
					 y2(i) = y2(j)
					 i = j
					 j = j+j
				else
					 j = k+1
				endif

		 enddo

		 y1(i) = t1
		 y2(i) = t2

	enddo
	return

end subroutine heapsort2

end program