module module_sort
    implicit none
    private
    public int_heap_sort,real_heap_sort
    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(.not.associated(self%root)) 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(.not.associated(self%root)) 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(.not.associated(self%root)) 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(.not. associated(self%root)) 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(100)                       :: 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(.not.associated(self%root)) 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(100)                      :: 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(.not.associated(self%root)) 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(100)                         :: 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(.not.associated(self%root)) 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(100)               :: 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(100),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
        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(.not.associated(self%root)) then
                self%root      => u
                self%root%par  => heap_nil
                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)

        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(.not.associated(self%root)) 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)
                    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)

            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

module global
    use module_sort
    use module_deque
    use module_RedBlackTree
    use module_MinHeap
    implicit none
    integer(8) :: N,K,memo(100005),exa(100005)
    integer(8) :: modulo = 1e9_8+7_8
end module global

program main
    use global
    implicit none
    integer(8) :: i,j,mod_pow,ans = 0

    read*, N, K

    do i = 1,K
        memo(i) = mod_pow(K/i,N,modulo)
    end do

    do i = K, 1, -1
        exa(i) = memo(i)
        do j = i*2, K, i
            exa(i) = mod(exa(i)+modulo-exa(j),modulo)
        end do
        ans = mod(ans+exa(i)*i,modulo)
    end do

    print '(i0)', ans

end program main

Score : 500 points

Problem Statement

Consider sequences \{A_1,...,A_N\} of length N consisting of integers between 1 and K (inclusive).

There are K^N such sequences. Find the sum of \gcd(A_1, ..., A_N) over all of them.

Since this sum can be enormous, print the value modulo (10^9+7).

Here \gcd(A_1, ..., A_N) denotes the greatest common divisor of A_1, ..., A_N.

Constraints

2 \leq N \leq 10^5

1 \leq K \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the sum of \gcd(A_1, ..., A_N) over all K^N sequences, modulo (10^9+7).

Sample Input 1

3 2

Sample Output 1

9

\gcd(1,1,1)+\gcd(1,1,2)+\gcd(1,2,1)+\gcd(1,2,2)
+\gcd(2,1,1)+\gcd(2,1,2)+\gcd(2,2,1)+\gcd(2,2,2)
=1+1+1+1+1+1+1+2=9

Thus, the answer is 9.

Sample Input 2

3 200

Sample Output 2

10813692

Sample Input 3

100000 100000

Sample Output 3

742202979

Be sure to print the sum modulo (10^9+7).


program name
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: n,k,i,j,ans
    integer(int64), allocatable:: gcn(:)
    integer(int64),parameter:: md = 1000000007

    read*, n,k
    allocate(gcn(k))

    do i=1,k
        gcn(i) = calc_rj(k/i, n)
    end do

    do i=k,1,-1
        j=2
        do while(i*j <= k)
            gcn(i) = gcn(i) - gcn(i*j)
            j=j+1
        end do
    end do

    ans=0
    do i=1,k
        ans=mod(ans+gcn(i)*i,md)
    end do

    print*, ans

contains
    function calc_rj(x,y) result(ret)
        integer(int64):: x,y,ret
        integer(int64):: num, nx
        ret = 1
        num = y
        nx = x
        do while(num > 0)
            if (btest(num,0)) then
                ret=mod(ret*nx,md)
            end if
            nx=mod(nx*nx,md)
            num=rshift(num,1)
        end do
    end function
end program name

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.


program main
integer :: n,k,m
read(*,*)n,k

m = mod(n,k)
if (abs(m-k) < n) then
write(*,*)abs(m-k)
else
write(*,*)m
end if
end program main

Score : 300 points

Problem Statement

Given any integer x, Aoki can do the operation below.

Operation: Replace x with the absolute difference of x and K.

You are given the initial value of an integer N. Find the minimum possible value taken by N after Aoki does the operation zero or more times.

Constraints

0 ≤ N ≤ 10^{18}

1 ≤ K ≤ 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the minimum possible value taken by N after Aoki does the operation zero or more times.

Sample Input 1

7 4

Sample Output 1

1

Initially, N=7.

After one operation, N becomes |7-4| = 3.

After two operations, N becomes |3-4| = 1, which is the minimum value taken by N.

Sample Input 2

2 6

Sample Output 2

2

N=2 after zero operations is the minimum.

Sample Input 3

1000000000000000000 1

Sample Output 3

0


program main
      implicit none
      integer(8) :: n, k

      read(*,*) n, k
      if ( mod(n,k) < abs( mod(n,k) - k ) ) then
              print *, mod(n,k)
      else
              print *, abs( mod(n,k) - k )
      end if

end program


program main
      implicit none
      integer(8) n, k
      integer(8) nmin, i

      read *, n,k
      nmin = n

      do
        n = abs(n-k)
        if ( n <= nmin ) then
                nmin = n
        else if ( n > nmin ) then
                exit
        end if
      end do

      print *, nmin

end program main


program em
  implicit none
  integer::N,K,C
  character(3*10**5)::S,S1
  integer,allocatable,dimension(:)::M,U
  integer::i,j
  integer::num
  read*,N,K,C
  read*,S
  allocate(M(K))
  S1=S
  num=1
  do i=1,N
    if(S1(i:i)=="o")then
      M(num)=i
      num=num+1
      do j=1,C
        S1(i+j:i+j)="x"
      end do
      if(Num>K)exit
    endif
  end do
  S1=S
  allocate(U(K))
  num=K
  do i=N,1,-1
    if(S1(i:i)=="o")then
      U(num)=i
      num=num-1
      if(NUM<=0)exit
      do j=1,C
        if(i-j<=0)exit
        S1(i-j:i-j)="x"
      end do
    endif
  end do
  do i=1,K
    if(M(i)==U(i))print"(I0)",M(i)
  end do
  write(*,*)
contains

end program em

Score : 500 points

Problem Statement

Takahashi has decided to work on K days of his choice from the N days starting with tomorrow.

You are given an integer C and a string S. Takahashi will choose his workdays as follows:

After working for a day, he will refrain from working on the subsequent C days.

If the i-th character of S is x, he will not work on Day i, where Day 1 is tomorrow, Day 2 is the day after tomorrow, and so on.

Find all days on which Takahashi is bound to work.

Constraints

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

1 \leq K \leq N

0 \leq C \leq N

The length of S is N.

Each character of S is o or x.

Takahashi can choose his workdays so that the conditions in Problem Statement are satisfied.

Input

Input is given from Standard Input in the following format:

N K C
S

Output

Print all days on which Takahashi is bound to work in ascending order, one per line.

Sample Input 1

11 3 2
ooxxxoxxxoo

Sample Output 1

6

Takahashi is going to work on 3 days out of the 11 days. After working for a day, he will refrain from working on the subsequent 2 days.

There are four possible choices for his workdays: Day 1,6,10, Day 1,6,11, Day 2,6,10, and Day 2,6,11.

Thus, he is bound to work on Day 6.

Sample Input 2

5 2 3
ooxoo

Sample Output 2

1
5

There is only one possible choice for his workdays: Day 1,5.

Sample Input 3

5 1 0
ooooo

Sample Output 3

There may be no days on which he is bound to work.

Sample Input 4

16 4 3
ooxxoxoxxxoxoxxo

Sample Output 4

11
16


program abc
	implicit none
	character(len=6) :: fname
    character(len=1) :: a,b,c,d,e,f

	read(*,*) fname
	c = fname(3:3)
	d = fname(4:4)
	e = fname(5:5)
	f = fname(6:6)

    if(c == d .and. e == f) then
    	write(*,*) "Yes"
    else
    	write(*,*) "No"
    endif

end program abc

Score : 100 points

Problem Statement

A string of length 6 consisting of lowercase English letters is said to be coffee-like if and only if its 3-rd and 4-th characters are equal and its 5-th and 6-th characters are also equal.

Given a string S, determine whether it is coffee-like.

Constraints

S is a string of length 6 consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If S is coffee-like, print Yes; otherwise, print No.

Sample Input 1

sippuu

Sample Output 1

Yes

In sippuu, the 3-rd and 4-th characters are equal, and the 5-th and 6-th characters are also equal.

Sample Input 2

iphone

Sample Output 2

No

Sample Input 3

coffee

Sample Output 3

Yes


program main
  implicit none
  integer :: k, n, res
  integer, allocatable :: a(:)
  read (*, *) k, n
  allocate (a(0:n))
  read (*, *) a(1:n)
  a(0) = a(n) - k
  res = k - maxval(a(1:n) - a(0:n - 1))
  write (*, "(i0)") res
end program main

Score : 300 points

Problem Statement

There is a circular pond with a perimeter of K meters, and N houses around them.

The i-th house is built at a distance of A_i meters from the northmost point of the pond, measured clockwise around the pond.

When traveling between these houses, you can only go around the pond.

Find the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.

Constraints

2 \leq K \leq 10^6

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

0 \leq A_1 < ... < A_N < K

All values in input are integers.

Input

Input is given from Standard Input in the following format:

K N
A_1 A_2 ... A_N

Output

Print the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.

Sample Input 1

20 3
5 10 15

Sample Output 1

10

If you start at the 1-st house and go to the 2-nd and 3-rd houses in this order, the total distance traveled will be 10.

Sample Input 2

20 3
0 5 15

Sample Output 2

10

If you start at the 2-nd house and go to the 1-st and 3-rd houses in this order, the total distance traveled will be 10.


      MODULE qSortMod
      implicit none
      interface qSort
        module procedure :: i16qSort
      end interface
      contains

      pure recursive function i16qSort( a ) result( r )
          !ref
          !https://t.doujin-constant.net/
          !post/143374686394/fortranをより使いこなしたquick-sort
          !
          !left is smaller!
        implicit none
        integer(16),intent(in) :: a(:)
        integer(16) :: r( size(a) )
        !integer(16),allocatable :: r(:) !is OK
        integer(16) :: sample(3),len,i,j

        len = size(a)
        if( len>1 )then
          sample(1) = a(1)
          sample(2) = a(len/2)
          sample(3) = a(len)
          !select median
          do i = 1,2
            do j = 2,3
              if( sample(i)>sample(j) )then
                len = sample(i) !len is buffer
                sample(i) = sample(j)
                sample(j) = len
              end if
            end do
          end do

          r = [ qSort( pack( a(:),a(:)< sample(2) ) ), &
                       pack( a(:),a(:)==sample(2) )  , &
                qSort( pack( a(:),a(:)> sample(2) ) ) ]
        else
          r = a
        end if
      end function i16qSort

      END MODULE qSortMod

      PROGRAM piyo
      use qSortMod
      IMPLICIT NONE
      integer(16) :: n,k
      integer(16),allocatable :: a(:)
      integer(16) :: ans,ant,post,i

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

      a = qSort( a )

      if( n==2 )then
        print*,abs( a(2)-a(1) );stop
      end if

      ans = k
      do i = 1,n-1
        ans = min( ans,k-(a(i+1)-a(i)) )
      end do
      !i=n
      ans = min( ans,a(n)-a(1) )

      print*,ans

      !debugg
    !  print*,k,n
    !  print*,a

      END PROGRAM


program main
  implicit none
  integer :: n, x, y, i, j, dist, k
  integer, allocatable :: cnt(:)
  read (*, *) n, x, y
  allocate (cnt(n - 1))
  cnt(:) = 0
  do i = 1, n - 1
    do j = i + 1, n
      dist = j - i
      dist = min(dist, abs(j - y) + abs(i - x) + 1)
      dist = min(dist, abs(j - x) + abs(i - y) + 1)
      cnt(dist) = cnt(dist) + 1
    end do
  end do

  do k = 1, n - 1
    write (*, "(i0)") cnt(k)
  end do
end program main

Score : 400 points

Problem Statement

We have an undirected graph G with N vertices numbered 1 to N and N edges as follows:

For each i=1,2,...,N-1, there is an edge between Vertex i and Vertex i+1.

There is an edge between Vertex X and Vertex Y.

For each k=1,2,...,N-1, solve the problem below:

Find the number of pairs of integers (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j in G is k.

Constraints

3 \leq N \leq 2 \times 10^3

1 \leq X,Y \leq N

X+1 < Y

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X Y

Output

For each k=1, 2, ..., N-1 in this order, print a line containing the answer to the problem.

Sample Input 1

5 2 4

Sample Output 1

5
4
1
0

The graph in this input is as follows:

There are five pairs (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j is 1: (1,2)\,,(2,3)\,,(2,4)\,,(3,4)\,,(4,5).

There are four pairs (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j is 2: (1,3)\,,(1,4)\,,(2,5)\,,(3,5).

There is one pair (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j is 3: (1,5).

There are no pairs (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j is 4.

Sample Input 2

3 1 3

Sample Output 2

3
0

The graph in this input is as follows:

Sample Input 3

7 3 7

Sample Output 3

7
8
4
2
0
0

Sample Input 4

10 4 8

Sample Output 4

10
12
10
8
4
1
0
0
0


program atcoder322_1
  read(5,*)N,M
  if(N<=1) then
     K=0
  else
     K=N*(N-1)/2
  end if
  if(M<=1) then
     K=K+0
  else
     K=K+M*(M-1)/2
  end if
  write(6,*)M
end program atcoder322_1

Score : 100 points

Problem Statement

We have N+M balls, each of which has an integer written on it.

It is known that:

The numbers written on N of the balls are even.

The numbers written on M of the balls are odd.

Find the number of ways to choose two of the N+M balls (disregarding order) so that the sum of the numbers written on them is even.

It can be shown that this count does not depend on the actual values written on the balls.

Constraints

0 \leq N,M \leq 100

2 \leq N+M

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

Print the answer.

Sample Input 1

2 1

Sample Output 1

1

For example, let us assume that the numbers written on the three balls are 1,2,4.

If we choose the two balls with 1 and 2, the sum is odd;

If we choose the two balls with 1 and 4, the sum is odd;

If we choose the two balls with 2 and 4, the sum is even.

Thus, the answer is 1.

Sample Input 2

4 3

Sample Output 2

9

Sample Input 3

1 1

Sample Output 3

0

Sample Input 4

13 3

Sample Output 4

81

Sample Input 5

0 3

Sample Output 5

3


program am
    implicit none
    integer::N,M
    read*,N,M
    print"(I0)",(N*(N-1))/2+(M*(M-1))/2
end program am


program main
    implicit none
    integer i, n, m, l, c, flag
    character s*99

    read(*,*) s
    n = len_trim(s)

    flag = 0

    do i = 1, (n-1)/2
        if(s(i:i) == s((n+1-i):(n+1-i))) then
            flag = 0
        else
            flag = 1
        end if
    end do

    if (flag == 0) then
        m = (n-1)/2

        do i = 1, m/2
            if(s(i:i) == s((m+1-i):(m+1-i))) then
                flag = 0
            else
                flag = 1
            end if
        end do
    end if

    if (flag == 0) then

        l = (n+3)/2
        c = 1

        do i = l, (n-l+1)/2+l
            if(s(i:i) == s((n+1-c):(n+1-c))) then
                flag = 0
            else
                flag = 1
            end if
            c = c + 1
        end do
    end if

    if (flag == 0) then
        write(*,*) "Yes"
    else
        write(*,*) "No"
    end if

end program

Score : 200 points

Problem Statement

A string S of an odd length is said to be a strong palindrome if and only if all of the following conditions are satisfied:

S is a palindrome.

Let N be the length of S. The string formed by the 1-st through ((N-1)/2)-th characters of S is a palindrome.

The string consisting of the (N+3)/2-st through N-th characters of S is a palindrome.

Determine whether S is a strong palindrome.

Constraints

S consists of lowercase English letters.

The length of S is an odd number between 3 and 99 (inclusive).

Input

Input is given from Standard Input in the following format:

S

Output

If S is a strong palindrome, print Yes;
otherwise, print No.

Sample Input 1

akasaka

Sample Output 1

Yes

S is akasaka.

The string formed by the 1-st through the 3-rd characters is aka.

The string formed by the 5-th through the 7-th characters is aka.
All of these are palindromes, so S is a strong palindrome.

Sample Input 2

level

Sample Output 2

No

Sample Input 3

atcoder

Sample Output 3

No


program example
	implicit none

    character(100) S,A,B
    integer :: N,i,countA=0,countB=0

    read(*,*) S

    S=adjustl(S)

    N=len_trim(S)

    A=S(1:(N-1)/2)

    B=S((N+3)/2:N)

	do i=1,(N-1)/2

    	if(A(i:i)==A(((N-1)/2)+1-i:((N-1)/2)+1-i)) then
        	countA=countA+1
        end if

    end do

    do i=(N+3)/2,N

    	if(B(i:i)==B(N+((N+3)/2)-i:N+((N+3)/2)-i)) then
        	countB=countB+1
        end if

    end do

    if(countA==((N-1)/2) .and. countB==((N-1)/2)) then
    	write(*,*) "Yes"
    else
    	write(*,*) "No"

    end if

end program


program ABC159B
    implicit none
    integer(8)::i,N,flg
    character(99)::S
    read*,S
    N=len_trim(S)
    flg=0

    do i=1,N/2
        if(S(i:i)/=S(N-i+1:N-i+1))flg=1
    end do

    do i=1,N/4
        if(S(i:i)/=S(N/2-i+1:N/2-i+1))flg=1
        if(S(N/2+1+i:N/2+1+i)/=S(N-i+1:N-i+1))flg=1
    end do
    if(flg==0)print'(A)',"Yes"
    if(flg==1)print'(A)',"No"
end program ABC159B


program max_volume
    implicit none
    integer n
    double precision m
    double precision  :: maxv = 0.0
    read *, n
    m = dble(n)
    maxv = (m/3)**3
    print *, maxv
end program

Score : 300 points

Problem Statement

Given is a positive integer L.
Find the maximum possible volume of a rectangular cuboid whose sum of the dimensions (not necessarily integers) is L.

Constraints

1 ≤ L ≤ 1000

L is an integer.

Input

Input is given from Standard Input in the following format:

L

Output

Print the maximum possible volume of a rectangular cuboid whose sum of the dimensions (not necessarily integers) is L.
Your output is considered correct if its absolute or relative error from our answer is at most 10^{-6}.

Sample Input 1

3

Sample Output 1

1.000000000000

For example, a rectangular cuboid whose dimensions are 0.8, 1, and 1.2 has a volume of 0.96.

On the other hand, if the dimensions are 1, 1, and 1, the volume of the rectangular cuboid is 1, which is greater.

Sample Input 2

999

Sample Output 2

36926037.000000000000


      program c159
      integer L
      real*8 V

      read(*,*)L

      V = (L/3)**3

        write(*,*)V

      end program c159


program atcoder322_D
  integer,dimension(200000) :: A,B
  read(5,*)N
  read(5,*)A(1:N)
  do I=1,N-1
        do J=I+1,N
           if(A(J)==A(I)) B(I)=B(I)+1
        end do
  do K=1,N
     write(6,*)sum(B)-B(K)
  end do
end do
end program atcoder322_D

Score : 400 points

Problem Statement

We have N balls. The i-th ball has an integer A_i written on it.

For each k=1, 2, ..., N, solve the following problem and print the answer.

Find the number of ways to choose two distinct balls (disregarding order) from the N-1 balls other than the k-th ball so that the integers written on them are equal.

Constraints

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

1 \leq A_i \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

For each k=1,2,...,N, print a line containing the answer.

Sample Input 1

5
1 1 2 1 2

Sample Output 1

2
2
3
2
3

Consider the case k=1 for example. The numbers written on the remaining balls are 1,2,1,2.

From these balls, there are two ways to choose two distinct balls so that the integers written on them are equal.

Thus, the answer for k=1 is 2.

Sample Input 2

4
1 2 3 4

Sample Output 2

0
0
0
0

No two balls have equal numbers written on them.

Sample Input 3

5
3 3 3 3 3

Sample Output 3

6
6
6
6
6

Any two balls have equal numbers written on them.

Sample Input 4

8
1 2 1 4 2 1 4 1

Sample Output 4

5
7
5
7
7
5
7
5


program main
    implicit none
    integer(8) n, m, sum
    integer(8), allocatable, dimension(:) :: a, b
    integer(8) i,k

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

    b = 0

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

    sum = 0

    do i = 1, n
        if(b(i) > 1) then
            sum = sum + b(i)*(b(i)-1)/2
        end if
    end do

    do k = 1, n
        if (b(a(k)) > 2) then
            write(*,*) sum - b(a(k))*(b(a(k))-1)/2 + (b(a(k))-1)*(b(a(k))-2)/2
        else if (b(a(k)) == 2) then
            write(*,*) sum - 1
        else
            write(*,*) sum
        end if
    end do

end program


program main

  implicit none

  integer :: n
  integer, allocatable :: a(:), b(:), c(:)

  integer :: i,j, tmp
  integer(8) :: sumb

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

  b = 0
  c = 0
  do i = 1, n
    c(a(i)) =  c(a(i)) + 1
  end do
  sumb = 0
  do i = 1, n
    if( mod(c(i),2) == 0 ) then
      tmp = c(i) / 2
      b(i) =  tmp*(c(i)-1)
    else
      tmp = (c(i)-1) / 2
      b(i) =  c(i)*tmp
    end if
    sumb = sumb + b(i)
  end do

  do i = 1, n
    if( mod(c(a(i)),2) == 0 ) then
      tmp = (c(a(i))-2) / 2
      print*, sumb - b(a(i)) + tmp*(c(a(i))-1)
    else
      tmp = (c(a(i))-1) / 2
      print*, sumb - b(a(i)) + tmp*(c(a(i))-2)
    end if
  end do

end program main


program main

  implicit none

  integer :: n
  integer, allocatable :: a(:), b(:), c(:)

  integer :: i,j
  integer(8) :: sumb

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

  b = 0
  c = 0
  do i = 1, n
    c(a(i)) =  c(a(i)) + 1
  end do
  sumb = 0
  do i = 1, n
    b(i) =  c(i)*(c(i)-1)/2
    sumb = sumb + b(i)
  end do

  do i = 1, n
    print*, sumb - b(a(i)) + (c(a(i))-2)*(c(a(i))-1)/2
  end do

end program main


program main
    implicit none
    integer n, m, sum
    integer, allocatable, dimension(:) :: a, b
    integer i,k

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

    b = 0

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

    sum = 0

    do i = 1, n
        if(b(i) > 1) then
            sum = sum + b(i)*(b(i)-1)/2
        end if
    end do

    do k = 1, n
        if (b(a(k)) > 2) then
            write(*,*) sum - b(a(k))*(b(a(k))-1)/2 + (b(a(k))-1)*(b(a(k))-2)/2
        else if (b(a(k)) == 2) then
            write(*,*) sum - 1
        else
            write(*,*) sum
        end if
    end do

end program


program main
    implicit none
    integer(8):: h=10,w=1000,k=1000
    integer(8):: i,j
    integer(8), allocatable:: c(:,:), now(:)
    integer(8), parameter:: inf = 1001001001
    character(1), allocatable:: s(:,:)
    read*, h,w,k

    allocate(s(w,h))

    block
        character(:), allocatable:: input_s
        allocate(character(w)::input_s)

        do i=1,h
            read*, input_s(:)
            do j=1,w
                s(j,i) = input_s(j:j)
            end do
        end do
    end block

    s(:,:) = '0'

    block
        integer(8):: div, g, ints, num
        integer(8):: ans = inf
        integer(8), allocatable:: id(:)
        logical:: emp, ok

        allocate(id(h))
        do div=1, lshift(1, h-1)
            g = 1

            do i=1, h
                id(i) = g
                if (and(rshift(div-1,i-1), 1) == 1) g=g+1
            end do

            allocate(c(w, maxval(id)))
            c(:,:) = 0

            do i=1,h
                do j=1,w
                    read(s(j,i),'(i1)') ints
                    c(j,id(i))=c(j,id(i))+ints
                end do
            end do

            ! ok=.true.
            ! do i=1,g
            !     do j=1,w
            !         if(c(j,i) > k) ok = .false.
            !     end do
            ! end do

            ! if (.not. ok) then
            !     deallocate(c)
            !     cycle
            ! end if

            allocate(now(g+1))
            ! num = g-1
            now(:) = 0
            ! do j=1,w
            !     if (.not. add(j,g)) then
            !         num=num+1
            !         now(:)=0
            !         emp = add(j,g)
            !     end if
            ! end do

            ans = min(ans,num)
            deallocate(c)
            deallocate(now)
        end do

        print*, ans
    end block

contains
function add(j,g) result(ok)
    integer(8):: i,j,g
    logical:: ok

    do i=1,g
        now(i)=now(i)+c(j,i)
    end do

    ok=.true.
    do i=1,g+1
        if (now(i) > k) ok=.false.
    end do
end function

end program main

Score : 500 points

Problem Statement

We have a chocolate bar partitioned into H horizontal rows and W vertical columns of squares.

The square (i, j) at the i-th row from the top and the j-th column from the left is dark if S_{i,j} is 0, and white if S_{i,j} is 1.

We will cut the bar some number of times to divide it into some number of blocks. In each cut, we cut the whole bar by a line running along some boundaries of squares from end to end of the bar.

How many times do we need to cut the bar so that every block after the cuts has K or less white squares?

Constraints

1 \leq H \leq 10

1 \leq W \leq 1000

1 \leq K \leq H \times W

S_{i,j} is 0 or 1.

Input

Input is given from Standard Input in the following format:

H W K
S_{1,1}S_{1,2}...S_{1,W}
:
S_{H,1}S_{H,2}...S_{H,W}

Output

Print the number of minimum times the bar needs to be cut so that every block after the cuts has K or less white squares.

Sample Input 1

3 5 4
11100
10001
00111

Sample Output 1

2

For example, cutting between the 1-st and 2-nd rows and between the 3-rd and 4-th columns - as shown in the figure to the left - works.

Note that we cannot cut the bar in the ways shown in the two figures to the right.

Sample Input 2

3 5 8
11100
10001
00111

Sample Output 2

0

No cut is needed.

Sample Input 3

4 10 4
1110010010
1000101110
0011101001
1101000111

Sample Output 3

3


program main
    implicit none
    integer(8):: h=10,w=1000,k=1000
    integer(8):: i,j
    integer(8), allocatable:: c(:,:), now(:)
    integer(8), parameter:: inf = 1001001001
    character(1), allocatable:: s(:,:)
    read*, h,w,k

    allocate(s(w,h))

    block
        character(:), allocatable:: input_s
        allocate(character(w)::input_s)

        do i=1,h
            read*, input_s(:)
            do j=1,w
                s(j,i) = input_s(j:j)
            end do
        end do
    end block

    s(:,:) = '0'

    block
        integer(8):: div, g, ints, num
        integer(8):: ans = inf
        integer(8), allocatable:: id(:)
        logical:: emp, ok

        allocate(id(h))
        do div=1, lshift(1, h-1)
            g = 1

            do i=1, h
                id(i) = g
                if (and(rshift(div-1,i-1), 1) == 1) g=g+1
            end do

            allocate(c(w, maxval(id)))
            c(:,:) = 0

            do i=1,h
                do j=1,w
                    read(s(j,i),*) ints
                    c(j,id(i))=c(j,id(i))+ints
                end do
            end do

            ok=.true.
            do i=1,g
                do j=1,w
                    if(c(j,i) > k) ok = .false.
                end do
            end do

            if (.not. ok) then
                deallocate(c)
                cycle
            end if

            allocate(now(g+1))
            ! num = g-1
            ! now(:) = 0
            ! do j=1,w
            !     if (.not. add(j,g)) then
            !         num=num+1
            !         now(:)=0
            !         emp = add(j,g)
            !     end if
            ! end do

            ans = min(ans,num)
            deallocate(c)
            deallocate(now)
        end do

        print*, ans
    end block

contains
function add(j,g) result(ok)
    integer(8):: i,j,g
    logical:: ok

    do i=1,g
        now(i)=now(i)+c(j,i)
    end do

    ok=.true.
    do i=1,g+1
        if (now(i) > k) ok=.false.
    end do
end function

end program main


      PROGRAM DividingChocolate
      IMPLICIT NONE
      integer :: h,w,k
      integer,allocatable :: choco(:,:)
      integer,allocatable :: memo1(:),memo2(:),counter2(:),this(:)
      character(1000) :: s
      integer :: i,j,buffer,piyo

      read*,h,w,k
      allocate( choco(h,w),memo1(h-1),memo2(w-1) )
      choco = 0
      memo1 = 0
      memo2 = 0
      do i = 1,h
        read*,s
        do j = 1,w
          if( s(j:j)=='1' )then
            choco(i,j) = 1
          end if
        end do
      end do

      !tate
      buffer = sum( choco(1,:) )
      do i = 2,h
        if( buffer+sum(choco(i,:))>k )then
          buffer = sum( choco(i,:) )
          memo1(i-1) = 1
        end if
      end do

      !yoko
      allocate( counter2(sum(memo1)),this(sum(memo1)) )
      counter2 = 0
      piyo = 1
      do i = 1,h
        counter2(piyo) = counter2(piyo) + choco(i,1)
        if(memo1(i)==1) piyo = piyo + 1
      end do
      this = 0
      do j = 2,w
        piyo = 1

        do i = 1,h
          this(piyo) = this(piyo) + choco(i,j)

          if(memo1(i)==1)then
            piyo = piyo + 1
          end if
        end do

        if( maxval(counter2+this)>k )then
          counter2 = this
          memo2(j-1) = 1
        end if
      end do

      print*,sum(memo1) + sum(memo2)

      !debugg
    !  print*,trim(s)
    !  do i = 1,h
    !    print*,choco(i,:)
    !  end do
    !
    !  print*,memo1
    !  print*,memo2

      END PROGRAM


program AGC43_A
  integer::H,W
  character(100)::S
  integer,allocatable::maze(:,:),dp(:,:)
  integer::i,j,k,dx(2)=[-1,0],dy(2)=[0,-1]
  read(*,*)H,W
  allocate(maze(H,W),dp(H,W))
  maze=0
  do i=1,H
     read(*,*)S
     do j=1,W
        if(S(j:j)=='#') maze(i,j)=1
     end do
  end do
  dp=huge(i)/2
  dp(1,1)=maze(1,1)
  do i=1,H
     do j=1,W
        do k=1,2
           dp(i,j)=min(dp(i,j),dp(max(1,i+dx(k)),max(1,j+dy(k)))&
           +merge(1,0,maze(max(1,i+dx(k)),max(1,j+dy(k)))==0 .and. maze(i,j)==1))
           !merge(a,b,c)はcが真ならa，偽ならb
        end do
     end do
  end do
  write(*,*)dp(H,W)
end program AGC43_A

Score : 400 points

Problem Statement

Consider a grid with H rows and W columns of squares. Let (r, c) denote the square at the r-th row from the top and the c-th column from the left.
Each square is painted black or white.

The grid is said to be good if and only if the following condition is satisfied:

From (1, 1), we can reach (H, W) by moving one square right or down repeatedly, while always being on a white square.

Note that (1, 1) and (H, W) must be white if the grid is good.

Your task is to make the grid good by repeating the operation below. Find the minimum number of operations needed to complete the task. It can be proved that you can always complete the task in a finite number of operations.

Choose four integers r_0, c_0, r_1, c_1(1 \leq r_0 \leq r_1 \leq H, 1 \leq c_0 \leq c_1 \leq W). For each pair r, c (r_0 \leq r \leq r_1, c_0 \leq c \leq c_1), invert the color of (r, c) - that is, from white to black and vice versa.

Constraints

2 \leq H, W \leq 100

Input

Input is given from Standard Input in the following format:

H W
s_{11} s_{12} \cdots s_{1W}
s_{21} s_{22} \cdots s_{2W}
\vdots
s_{H1} s_{H2} \cdots s_{HW}

Here s_{rc} represents the color of (r, c) - # stands for black, and . stands for white.

Output

Print the minimum number of operations needed.

Sample Input 1

3 3
.##
.#.
##.

Sample Output 1

1

Do the operation with (r_0, c_0, r_1, c_1) = (2, 2, 2, 2) to change just the color of (2, 2), and we are done.

Sample Input 2

2 2
#.
.#

Sample Output 2

2

Sample Input 3

4 4
..##
#...
###.
###.

Sample Output 3

0

No operation may be needed.

Sample Input 4

5 5
.#.#.
#.#.#
.#.#.
#.#.#
.#.#.

Sample Output 4

4


program am
integer::H,W
integer,allocatable,dimension(:,:)::maze,DP
character(100)::S
integer::dx(2)=[-1,0],dy(2)=[0,-1]
integer::i,j,k
read*,H,W
allocate(maze(H,W))
maze=0
do i=1,H
    read*,S
    do j=1,W
        if(S(j:j)=="#")maze(i,j)=1
    end do
end do
allocate(DP(H,W))
DP=huge(i)/2
DP(1,1)=maze(1,1)
do i=1,H
    do j=1,W
        do k=1,2
            DP(i,j)=min(DP(i,j),&
            DP(max(1,i+dx(k)),max(1,j+dy(k)))+merge(1,0,maze(max(1,i+dx(k)),max(1,j+dy(k)))==0.and.maze(i,j)==1))
        end do
    end do
end do
print"(I0)",DP(H,W)
end program am


module ch
  implicit none
  type char
     character(100)      :: n
  end type char
  type(char),allocatable :: s(:)
end module ch
program main

  use ch, only : s
  implicit none

  integer              ::  h, w
  integer              ::  i, j
  integer,allocatable  ::  ss(:,:), sb(:,:)

  read(*,*) h, w
  allocate( s(h) )
  allocate( ss(h,w) )
  allocate( sb(h,w) )
  do i = 1, h
    read(*,*) s(i)%n
  end do

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

      if( s(i)%n(j:j) == "#" ) then
        ss(i,j) = ss(i,j) + 1
      end if

      if( i == 1 .and. j /= 1 ) then
        ss(i,j) = ss(i,j) + ss(i,j-1)
        if ( s(i)%n(j:j) == "#" .and. s(i)%n(j-1:j-1) == "#" ) then
          ss(i,j) = ss(i,j) - 1
        end if
      end if

      if( i /= 1 .and. j == 1 ) then
        ss(i,j) = ss(i,j) + ss(i-1,j)
        if ( s(i)%n(j:j) == "#" .and. s(i-1)%n(j:j) == "#" ) then
          ss(i,j) = ss(i,j) - 1
        end if
      end if

      if( i /= 1 .and. j /= 1 ) then
        if( ss(i,j-1) <= ss(i-1,j) ) then
          ss(i,j) = ss(i,j) + ss(i,j-1)
          if ( s(i)%n(j:j) == "#" .and. s(i)%n(j-1:j-1) == "#" ) then
            ss(i,j) = ss(i,j) - 1
          end if
        else
          ss(i,j) = ss(i,j) + ss(i-1,j)
          if ( s(i)%n(j:j) == "#" .and. s(i-1)%n(j:j) == "#" ) then
            ss(i,j) = ss(i,j) - 1
          end if
        end if
      end if

    end do
  end do

  !print*, ss(h,w)

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

      if( s(i)%n(j:j) == "." ) then
        sb(i,j) = sb(i,j) + 1
      end if

      if( i == 1 .and. j /= 1 ) then
        sb(i,j) = sb(i,j) + sb(i,j-1)
        if ( s(i)%n(j:j) == "." .and. s(i)%n(j-1:j-1) == "." ) then
          sb(i,j) = sb(i,j) - 1
        end if
      end if

      if( i /= 1 .and. j == 1 ) then
        sb(i,j) = sb(i,j) + sb(i-1,j)
        if ( s(i)%n(j:j) == "." .and. s(i-1)%n(j:j) == "." ) then
          sb(i,j) = sb(i,j) - 1
        end if
      end if

      if( i /= 1 .and. j /= 1 ) then
        if( sb(i,j-1) <= sb(i-1,j) ) then
          sb(i,j) = sb(i,j) + sb(i,j-1)
          if ( s(i)%n(j:j) == "." .and. s(i)%n(j-1:j-1) == "." ) then
            sb(i,j) = sb(i,j) - 1
          end if
        else
          sb(i,j) = sb(i,j) + sb(i-1,j)
          if ( s(i)%n(j:j) == "." .and. s(i-1)%n(j:j) == "." ) then
            sb(i,j) = sb(i,j) - 1
          end if
        end if
      end if

    end do
  end do

  if( ss(h,w) <= (sb(h,w)+1)) then
    print*, ss(h,w)
  else
    print*, sb(h,w)+1
  end if

end program main


module ch
  implicit none
  type char
     character(100)      :: n
  end type char
  type(char),allocatable :: s(:)
end module ch
program main

  use ch, only : s
  implicit none

  integer(8)              ::  h, w
  integer(8)              ::  i, j
  integer(8),allocatable  ::  ss(:,:), sb(:,:)

  read(*,*) h, w
  allocate( s(h) )
  allocate( ss(h,w) )
  allocate( sb(h,w) )
  do i = 1, h
    read(*,*) s(i)%n
  end do

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

      if( s(i)%n(j:j) == "#" ) then
        ss(i,j) = ss(i,j) + 1
      end if

      if( i == 1 .and. j /= 1 ) then
        ss(i,j) = ss(i,j) + ss(i,j-1)
        if ( s(i)%n(j:j) == "#" .and. s(i)%n(j-1:j-1) == "#" ) then
          ss(i,j) = ss(i,j) - 1
        end if
      end if

      if( i /= 1 .and. j == 1 ) then
        ss(i,j) = ss(i,j) + ss(i-1,j)
        if ( s(i)%n(j:j) == "#" .and. s(i-1)%n(j:j) == "#" ) then
          ss(i,j) = ss(i,j) - 1
        end if
      end if

      if( i /= 1 .and. j /= 1 ) then
        if( ss(i,j-1) <= ss(i-1,j) ) then
          ss(i,j) = ss(i,j) + ss(i,j-1)
          if ( s(i)%n(j:j) == "#" .and. s(i)%n(j-1:j-1) == "#" ) then
            ss(i,j) = ss(i,j) - 1
          end if
        else
          ss(i,j) = ss(i,j) + ss(i-1,j)
          if ( s(i)%n(j:j) == "#" .and. s(i-1)%n(j:j) == "#" ) then
            ss(i,j) = ss(i,j) - 1
          end if
        end if
      end if

    end do
  end do

  !print*, ss(h,w)

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

      if( s(i)%n(j:j) == "." ) then
        sb(i,j) = sb(i,j) + 1
      end if

      if( i == 1 .and. j /= 1 ) then
        sb(i,j) = sb(i,j) + sb(i,j-1)
        if ( s(i)%n(j:j) == "." .and. s(i)%n(j-1:j-1) == "." ) then
          sb(i,j) = sb(i,j) - 1
        end if
      end if

      if( i /= 1 .and. j == 1 ) then
        sb(i,j) = sb(i,j) + sb(i-1,j)
        if ( s(i)%n(j:j) == "." .and. s(i-1)%n(j:j) == "." ) then
          sb(i,j) = sb(i,j) - 1
        end if
      end if

      if( i /= 1 .and. j /= 1 ) then
        if( sb(i,j-1) <= sb(i-1,j) ) then
          sb(i,j) = sb(i,j) + sb(i,j-1)
          if ( s(i)%n(j:j) == "." .and. s(i)%n(j-1:j-1) == "." ) then
            sb(i,j) = sb(i,j) - 1
          end if
        else
          sb(i,j) = sb(i,j) + sb(i-1,j)
          if ( s(i)%n(j:j) == "." .and. s(i-1)%n(j:j) == "." ) then
            sb(i,j) = sb(i,j) - 1
          end if
        end if
      end if

    end do
  end do

  if( ss(h,w) <= (sb(h,w)+1)) then
    print*, ss(h,w)
  else
    print*, sb(h,w)+1
  end if

end program main


program main
    implicit none
    integer n
    integer, allocatable, dimension(:) :: a
    integer, allocatable, dimension(:,:) :: x
    integer i, j, flag
    character s*1000000

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

    do i = 1,n
        read(s(i:i),*) a(i)
    end do

    do i = 1,n
        x(1,i) = a(i)
    end do

    do j = 2, n
        if(mod(j,2) == 0) then
            do i = 1, n+1-j
                x(2,i) = abs(x(1,i)-x(1,i+1))
            end do
            flag = 2
        else
            do i = 1, n+1-j
                x(1,i) = abs(x(2,i)-x(2,i+1))
            end do
            flag = 1
        end if
    end do

    if (flag == 1) then
        write(*,*) x(1,1)
    else if(flag == 2) then
        write(*,*) x(2,1)
    end if

end program

Score : 700 points

Problem Statement

Given is a sequence of N digits a_1a_2\ldots a_N, where each element is 1, 2, or 3.
Let x_{i,j} defined as follows:

x_{1,j} := a_j \quad (1 \leq j \leq N)

x_{i,j} := | x_{i-1,j} - x_{i-1,j+1} | \quad (2 \leq i \leq N and 1 \leq j \leq N+1-i)

Find x_{N,1}.

Constraints

2 \leq N \leq 10^6

a_i = 1,2,3 (1 \leq i \leq N)

Input

Input is given from Standard Input in the following format:

N
a_1a_2\ldotsa_N

Output

Print x_{N,1}.

Sample Input 1

4
1231

Sample Output 1

1

x_{1,1},x_{1,2},x_{1,3},x_{1,4} are respectively 1,2,3,1.

x_{2,1},x_{2,2},x_{2,3} are respectively |1-2| = 1,|2-3| = 1,|3-1| = 2.

x_{3,1},x_{3,2} are respectively |1-1| = 0,|1-2| = 1.

Finally, x_{4,1} = |0-1| = 1, so the answer is 1.

Sample Input 2

10
2311312312

Sample Output 2

0


program example
	implicit none

	integer(16) :: i=1,j=1,N
    integer(16),allocatable :: x(:,:),A(:)

    read(*,*) N

    allocate(x(N,N),A(N))

	do
		read(*,"(i1)",advance="no",end=100) A(i)
    	x(1,j)=A(i)
        if (mod(j,2000)==0) then
        	deallocate(A)
            allocate(A(N))
        	i=1
            j=j+1
        else

        	i=i+1
        	j=j+1

        end if

    end do

100    do i=2,N

    	do j=1,N+1-i

        	x(i,j)=abs(x(i-1,j)-x(i-1,j+1))

        end do

    end do

    write(*,*) x(N,1)

end program


program example
	implicit none

	integer(8) H,W

    read(*,*) H,W

    if(W==1 .or. H==1) then
    	write(*,*) 1

    else if (mod(H,2)==0) then
    	write(*,*) W*(H/2)

    else

    	write(*,*) (W*(H/2))+(W/2)+mod(W,2)

    end if

end program example

Score : 200 points

Problem Statement

We have a board with H horizontal rows and W vertical columns of squares.
There is a bishop at the top-left square on this board.
How many squares can this bishop reach by zero or more movements?

Here the bishop can only move diagonally.
More formally, the bishop can move from the square at the r_1-th row (from the top) and the c_1-th column (from the left) to the square at the r_2-th row and the c_2-th column if and only if exactly one of the following holds:

r_1 + c_1 = r_2 + c_2

r_1 - c_1 = r_2 - c_2

For example, in the following figure, the bishop can move to any of the red squares in one move:

Constraints

1 \leq H, W \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H \ W

Output

Print the number of squares the bishop can reach.

Sample Input 1

4 5

Sample Output 1

10

The bishop can reach the cyan squares in the following figure:

Sample Input 2

7 3

Sample Output 2

11

The bishop can reach the cyan squares in the following figure:

Sample Input 3

1000000000 1000000000

Sample Output 3

500000000000000000


module mod_two_dim_merge_sort
  implicit none
  integer, private, parameter :: intkind = 8
contains
  logical function less(a, b) result(res)
    integer(intkind), intent(in) :: a(:), b(:)
    real(8) :: a1, b1
    a1 = a(1) / real(a(2) + 1, 8)
    b1 = b(1) / real(b(2) + 1, 8)
    res = a1 > b1 .or. (a1 == b1 .and. a(2) >= b(2))
  end
  subroutine merge_sort(a)
    integer(intkind), intent(inout) :: a(:, :)
    integer :: n, m, l, i, u
    n = size(a, 1)
    m = n
    l = 1
    do while (m > 1)
      do i = 1, m / 2
        u = min(2 * i * l, n)
        call merger(a(2 * (i - 1) * l + 1:(2 * i - 1) * l, :), &
        a((2 * i - 1) * l + 1:u, :))
      end do
      l = 2 * l
      m = (m + 1) / 2
    end do
  end
  subroutine merger(a1, a2)
    integer(intkind), intent(inout) :: a1(:, :), a2(:, :)
    integer(intkind) :: a(size(a1, 1) + size(a2, 1), size(a1, 2))
    integer :: i1, i2, n1, n2
    i1 = 1
    i2 = 1
    n1 = size(a1, 1)
    n2 = size(a2, 1)
    do while (i1 <= n1 .and. i2 <= n2)
      if (less(a1(i1, :), a2(i2, :))) then
        a(i1 + i2 - 1, :) = a1(i1, :)
        i1 = i1 + 1
      else
        a(i1 + i2 - 1, :) = a2(i2, :)
        i2 = i2 + 1
       end if
    end do
    if (i1 <= n1) then
      a(i1 + i2 - 1:n1 + n2, :) = a1(i1:n1, :)
    else
      a(i1 + i2 - 1:n1 + n2, :) = a2(i2:n2, :)
    end if
    a1 = a(1:n1, :)
    a2 = a(n1 + 1:n1 + n2, :)
  end
end module mod_two_dim_merge_sort
program manga_market
  use mod_two_dim_merge_sort
  implicit none
  integer(8), parameter :: inf = 3e18
  integer, parameter :: k = 30
  integer :: n, l, i, j, m = 0
  integer(8) :: t, r, tmp
  integer(8), allocatable :: c(:, :), dp(:, :), s(:)
  read(*,*) n, t
  allocate(c(n, 2))
  do i = 1, n
    read(*,*) c(i, :)
  end do
  call merge_sort(c(1:n, 1:2))
  l = n + 1
  do while (c(l - 1, 1) == 0)
    l = l - 1
  end do
  allocate(dp(0:k, 0:l - 1), s(0:n - l + 1))
  dp = inf
  dp(0, :) = 0
  do i = 1, l - 1
    do j = 1, min(k, i)
      dp(j, i) = dp(j, i - 1)
      if (dp(j - 1, i - 1) == inf) cycle
      tmp = dp(j - 1, i - 1) + 1
      dp(j, i) = min(dp(j, i), tmp + c(i, 1) * tmp + c(i, 2))
    end do
  end do
  s(0) = 0
  do i = n, l, -1
    s(n - i + 1) = s(n - i) + c(i, 2) + 1
  end do
  do i = 0, min(k, l - 1)
    r = t - dp(i, l - 1)
    if (r < 0) cycle
    do j = 0, n - l + 1
      if (s(j) > r) exit
      m = max(m, i + j)
    end do
  end do
  write(*,'(i0)') m
end program manga_market

Score : 800 points

Problem Statement

There are N stores called Store 1, Store 2, \cdots, Store N. Takahashi, who is at his house at time 0, is planning to visit some of these stores.

It takes Takahashi one unit of time to travel from his house to one of the stores, or between any two stores.

If Takahashi reaches Store i at time t, he can do shopping there after standing in a queue for a_i \times t + b_i units of time. (We assume that it takes no time other than waiting.)

All the stores close at time T + 0.5. If Takahashi is standing in a queue for some store then, he cannot do shopping there.

Takahashi does not do shopping more than once in the same store.

Find the maximum number of times he can do shopping before time T + 0.5.

Constraints

All values in input are integers.

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

0 \leq a_i \leq 10^9

0 \leq b_i \leq 10^9

0 \leq T \leq 10^9

Input

Input is given from Standard Input in the following format:

N T
a_1 b_1
a_2 b_2
\vdots
a_N b_N

Output

Print the answer.

Sample Input 1

3 7
2 0
3 2
0 3

Sample Output 1

2

Here is one possible way to visit stores:

From time 0 to time 1: in 1 unit of time, he travels from his house to Store 1.

From time 1 to time 3: for 2 units of time, he stands in a queue for Store 1 to do shopping.

From time 3 to time 4: in 1 unit of time, he travels from Store 1 to Store 3.

From time 4 to time 7: for 3 units of time, he stands in a queue for Store 3 to do shopping.

In this way, he can do shopping twice before time 7.5.

Sample Input 2

1 3
0 3

Sample Output 2

0

Sample Input 3

5 21600
2 14
3 22
1 3
1 10
1 9

Sample Output 3

5

Sample Input 4

7 57
0 25
3 10
2 4
5 15
3 22
2 14
1 15

Sample Output 4

3


module mod_two_dim_merge_sort
  implicit none
  integer, private, parameter :: intkind = 8
contains
  logical function less(a, b) result(res)
    integer(intkind), intent(in) :: a(:), b(:)
    real(8) :: a1, b1
    a1 = a(1) / real(a(2) + 1, 8)
    b1 = b(1) / real(b(2) + 1, 8)
    res = a1 > b1 .or. (a1 == b1 .and. a(2) >= b(2))
  end
  subroutine merge_sort(a)
    integer(intkind), intent(inout) :: a(:, :)
    integer :: n, m, l, i, u
    n = size(a, 1)
    m = n
    l = 1
    do while (m > 1)
      do i = 1, m / 2
        u = min(2 * i * l, n)
        call merger(a(2 * (i - 1) * l + 1:(2 * i - 1) * l, :), &
        a((2 * i - 1) * l + 1:u, :))
      end do
      l = 2 * l
      m = (m + 1) / 2
    end do
  end
  subroutine merger(a1, a2)
    integer(intkind), intent(inout) :: a1(:, :), a2(:, :)
    integer(intkind) :: a(size(a1, 1) + size(a2, 1), size(a1, 2))
    integer :: i1, i2, n1, n2
    i1 = 1
    i2 = 1
    n1 = size(a1, 1)
    n2 = size(a2, 1)
    do while (i1 <= n1 .and. i2 <= n2)
      if (less(a1(i1, :), a2(i2, :))) then
        a(i1 + i2 - 1, :) = a1(i1, :)
        i1 = i1 + 1
      else
        a(i1 + i2 - 1, :) = a2(i2, :)
        i2 = i2 + 1
       end if
    end do
    if (i1 <= n1) then
      a(i1 + i2 - 1:n1 + n2, :) = a1(i1:n1, :)
    else
      a(i1 + i2 - 1:n1 + n2, :) = a2(i2:n2, :)
    end if
    a1 = a(1:n1, :)
    a2 = a(n1 + 1:n1 + n2, :)
  end
end module mod_two_dim_merge_sort
program manga_market
  use mod_two_dim_merge_sort
  implicit none
  integer(8), parameter :: inf = 3e18
  integer :: n, l, i, j, m = 0, p, q, h
  integer(8) :: t, r
  integer(8), allocatable :: c(:, :), dp(:, :), s(:)
  read(*,*) n, t
  allocate(c(n, 2))
  do i = 1, n
    read(*,*) c(i, :)
  end do
  call merge_sort(c(1:n, 1:2))
  l = n + 1
  do while (c(l - 1, 1) == 0)
    l = l - 1
  end do
  allocate(dp(0:28, 0:l - 1), s(0:n - 1 + 1))
  dp = inf
  dp(0, :) = 0
  do i = 1, l - 1
    do j = 1, min(28, l - 1)
      dp(j, i) = dp(j, i - 1)
      if (dp(j - 1, i - 1) == inf) cycle
      dp(j, i) = min(dp(j, i), dp(j - 1, i - 1) + c(i, 1) * dp(j - 1, i - 1) + c(i, 2))
    end do
  end do
  s(0) = 0
  do i = n, l, -1
    s(n - i + 1) = s(n - i) + c(i, 2)
  end do
  do i = 0, min(28, l - 1)
    r = t - dp(i, l - 1)
    if (r < 0) cycle
    p = -1
    q = n - l + 1
    do while (q - p > 1)
      h = (p + q) / 2
      if (s(h) < r) then
        p = h
      else
        q = h
      end if
    end do
    m = max(m, i + q)
  end do
  write(*,'(i0)') m
end program manga_market


program main
      character(3) :: s
      integer(4) :: i

      read(*,*) s
      if ( s(1:1) == s(2:2) ) then

        if ( s(2:2) == s(3:3) ) then
          print *, 'No'
        else
          print *, 'Yes'
        end if
      else
        print *, 'Yes'
      end if

end program main

Score : 100 points

Problem Statement

In AtCoder City, there are three stations numbered 1, 2, and 3.

Each of these stations is operated by one of the two railway companies, A and B. A string S of length 3 represents which company operates each station. If S_i is A, Company A operates Station i; if S_i is B, Company B operates Station i.

To improve the transportation condition, for each pair of a station operated by Company A and one operated by Company B, there will be a bus service connecting them.

Determine if there is a pair of stations that will be connected by a bus service.

Constraints

Each character of S is A or B.

|S| = 3

Input

Input is given from Standard Input in the following format:

S

Output

If there is a pair of stations that will be connected by a bus service, print Yes; otherwise, print No.

Sample Input 1

ABA

Sample Output 1

Yes

Company A operates Station 1 and 3, while Company B operates Station 2.

There will be a bus service between Station 1 and 2, and between Station 2 and 3, so print Yes.

Sample Input 2

BBA

Sample Output 2

Yes

Company B operates Station 1 and 2, while Company A operates Station 3.

There will be a bus service between Station 1 and 3, and between Station 2 and 3, so print Yes.

Sample Input 3

BBB

Sample Output 3

No

Company B operates all the stations. Thus, there will be no bus service, so print No.


program main
integer(16) :: n,a,b,c,d,e
read *,n,a,b

c = n/(a+b)
d=mod(n,(a+b))
e = c*a+min(d,a)
print '(i0)',e

end


program prob2
  implicit none
  integer(16) :: n, a, b, ans

  read(*,*) n, a, b

  ans = n / (a + b)
  ans = a * ans

  ans = ans + min(mod(n, a+b), a)

  write(*,*) ans
  stop
end program prob2

Score : 200 points

Problem Statement

Takahashi has many red balls and blue balls. Now, he will place them in a row.

Initially, there is no ball placed.

Takahashi, who is very patient, will do the following operation 10^{100} times:

Place A blue balls at the end of the row of balls already placed. Then, place B red balls at the end of the row.

How many blue balls will be there among the first N balls in the row of balls made this way?

Constraints

1 \leq N \leq 10^{18}

A, B \geq 0

0 < A + B \leq 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print the number of blue balls that will be there among the first N balls in the row of balls.

Sample Input 1

8 3 4

Sample Output 1

4

Let b denote a blue ball, and r denote a red ball. The first eight balls in the row will be bbbrrrrb, among which there are four blue balls.

Sample Input 2

8 0 4

Sample Output 2

0

He placed only red balls from the beginning.

Sample Input 3

6 2 4

Sample Output 3

2

Among bbrrrr, there are two blue balls.


program prob2
    implicit none
    integer(16)::n,a,b,c
    read(*,*) n,a,b

    c = a+b

    if(n <= a+b) then
        write(*,*) a
    else
        write(*,*) n - n/c * b
    end if

    stop
end program


program ABC158B
    implicit none
    integer(8)::A,B,N,ans
    read(5,*)N,A,B
    ans=A*(N/(A+B))+min(A,mod(N,A+B))
    print'(i0)',ans
end program ABC158B


program example
	implicit none

	integer(8) N,A,B,amari

    read(*,*)N,A,B

    if(A==0) then
    	write(*,*)"0"
    else
    	amari=mod(N,A+B)

        if(amari<=A) then
        	write(*,*) (N*A/(A+B))+amari
        else
        	write(*,*) (N*A/(A+B))+A
        end if
    end if

end program


program main
integer a,b,as,al,bs,bl
read *,a,b

as = int(100./8.*dble(a))
al = int(100./8.*dble(a+0.999))
bs = int(100./10.*dble(b))
bl = int(100./10.*dble(b+0.999))

print *,as,al
print *,bs,bl
do i=as,al
	do j=bs,bl
    	if(i==j) then
        	print *,i
            stop
        end if
     end do
end do
print *,-1

end

Score : 300 points

Problem Statement

Find the price of a product before tax such that, when the consumption tax rate is 8 percent and 10 percent, the amount of consumption tax levied on it is A yen and B yen, respectively. (Yen is the currency of Japan.)

Here, the price before tax must be a positive integer, and the amount of consumption tax is rounded down to the nearest integer.

If multiple prices satisfy the condition, print the lowest such price; if no price satisfies the condition, print -1.

Constraints

1 \leq A \leq B \leq 100

A and B are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

If there is a price that satisfies the condition, print an integer representing the lowest such price; otherwise, print -1.

Sample Input 1

2 2

Sample Output 1

25

If the price of a product before tax is 25 yen, the amount of consumption tax levied on it is:

When the consumption tax rate is 8 percent: \lfloor 25 \times 0.08 \rfloor = \lfloor 2 \rfloor = 2 yen.

When the consumption tax rate is 10 percent: \lfloor 25 \times 0.1 \rfloor = \lfloor 2.5 \rfloor = 2 yen.

Thus, the price of 25 yen satisfies the condition. There are other possible prices, such as 26 yen, but print the minimum such price, 25.

Sample Input 2

8 10

Sample Output 2

100

If the price of a product before tax is 100 yen, the amount of consumption tax levied on it is:

When the consumption tax rate is 8 percent: \lfloor 100 \times 0.08 \rfloor = 8 yen.

When the consumption tax rate is 10 percent: \lfloor 100 \times 0.1 \rfloor = 10 yen.

Sample Input 3

19 99

Sample Output 3

-1

There is no price before tax satisfying this condition, so print -1.


program main
integer a,b,as,al,bs,bl
read *,a,b

as = int(100./8.*dble(a))
al = nint(100./8.*dble(a))
bs = int(100./10.*dble(b))
bl = nint(100./10.*dble(a))

do i=as,al
	do j=bs,bl
    	if(i==j) then
        	print *,i
            stop
        end if
     end do
end do
print *,-1

end


program ABC157_A
  read(5,*)N
  if(N/2*2==N) then
     write(6,*)N/2
  else
     write(6,*)N/2+1
  end if
end program ABC157_A

Score : 100 points

Problem Statement

Takahashi wants to print a document with N pages double-sided, where two pages of data can be printed on one sheet of paper.

At least how many sheets of paper does he need?

Constraints

N is an integer.

1 \leq N \leq 100

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

5

Sample Output 1

3

By printing the 1-st, 2-nd pages on the 1-st sheet, 3-rd and 4-th pages on the 2-nd sheet, and 5-th page on the 3-rd sheet, we can print all the data on 3 sheets of paper.

Sample Input 2

2

Sample Output 2

1

Sample Input 3

100

Sample Output 3

50


program sample
    implicit none

    integer(8) :: i,j,k,m,n,h,w,b

    integer(8),allocatable :: y(:)
    integer(8)::a(3,3),che(3,3)
    read(*,*) a
    read(*,*) n

    do i=1,n
        read(*,*)b
        do j=1,3
            do k=1,3

                if (b==a(j,k))then
                    che(j,k)=1
                end if

            end do
        end do
    end do
    do i=1,3
        if (che(i,1)==1 .and. che(i,2)==1 .and. che(i,3)==1)then
            write(*,*)'Yes'
            stop
        end if
    end do
    do i=1,3
        if (che(1,i)==1 .and. che(2,i)==1 .and. che(3,i)==1)then
            write(*,*)'Yes'
            stop
        end if
    end do

    if (che(1,1)==1 .and. che(2,2)==1 .and. che(3,3)==1)then
            write(*,*)'Yes'
            stop
    end if

    if (che(1,3)==1 .and. che(2,2)==1 .and. che(3,1)==1)then
        write(*,*)'Yes'
        stop

    end if
    write(*,*)'No'
    stop
end program sample

Score : 200 points

Problem Statement

We have a bingo card with a 3\times3 grid. The square at the i-th row from the top and the j-th column from the left contains the number A_{i, j}.

The MC will choose N numbers, b_1, b_2, \cdots, b_N. If our bingo sheet contains some of those numbers, we will mark them on our sheet.

Determine whether we will have a bingo when the N numbers are chosen, that is, the sheet will contain three marked numbers in a row, column, or diagonal.

Constraints

All values in input are integers.

1 \leq A_{i, j} \leq 100

A_{i_1, j_1} \neq A_{i_2, j_2} ((i_1, j_1) \neq (i_2, j_2))

1 \leq N \leq 10

1 \leq b_i \leq 100

b_i \neq b_j (i \neq j)

Input

Input is given from Standard Input in the following format:

A_{1, 1} A_{1, 2} A_{1, 3}
A_{2, 1} A_{2, 2} A_{2, 3}
A_{3, 1} A_{3, 2} A_{3, 3}
N
b_1
\vdots
b_N

Output

If we will have a bingo, print Yes; otherwise, print No.

Sample Input 1

84 97 66
79 89 11
61 59 7
7
89
7
87
79
24
84
30

Sample Output 1

Yes

We will mark A_{1, 1}, A_{2, 1}, A_{2, 2}, A_{3, 3}, and complete the diagonal from the top-left to the bottom-right.

Sample Input 2

41 7 46
26 89 2
78 92 8
5
6
45
16
57
17

Sample Output 2

No

We will mark nothing.

Sample Input 3

60 88 34
92 41 43
65 73 48
10
60
43
88
11
48
73
65
41
92
34

Sample Output 3

Yes

We will mark all the squares.


program main
  implicit none
  type arr_list
     integer :: N
     integer,allocatable :: point(:)
  end type arr_list
  type(arr_list),allocatable :: data(:)
  integer :: num(97:122)
  integer :: N, Q
  character(len=500000) :: S
  character(:),allocatable :: tmp
  character(len=1) :: c,b,c2
  integer,allocatable :: tmp2(:)
  integer :: a
  integer :: i,j, l,r
  integer :: out
  read(*,*) N
  read(*,*) S
  read(*,*) Q
  allocate(data(97:122))
  allocate(tmp2(0:N-1))
  data(:)%N = 0
  num(:) = 0
  do i = 1,N
     data(ichar(S(i:i)))%N = data(ichar(S(i:i)))%N+1
  end do
  do i = 97,122
     if (data(i)%N > 0) allocate(data(i)%point(0:data(i)%N-1))
  end do
  do i = 1,N
     data(ichar(S(i:i)))%point( num(ichar(S(i:i))) ) = i-1
     num(ichar(S(i:i))) = num(ichar(S(i:i))) + 1
  end do
!  do i = 97,97+4
!     write(*,*) data(i)%point
!  end do
  do i = 1, Q
     read(*,'(i1,1x,a,1x,a)') a,b,c
     if (a == 1) then
        backspace(5)
        !read(*,'(i1,1x,g0,1x,a)') a,j,c
        read(*,*) a,j,c
        c2 = S(j:j)
        call insert_delete(data,j-1,c,c2,tmp2)
        S(j:j) = c
     else
        backspace(5)
!        read(*,'(i1,1x,g0,1x,g0)') a,l,r
        read(*,*) a,l,r
        !tmp = S(l:r)
        !call count_char(S(l:r),out)
        call count_char(data,l-1,r-1,out)
        write(*,'(i0)') out
        !deallocate(tmp)
     end if

  end do
contains
  subroutine insert_delete(data,key,c,c2,tmp)
    implicit none
    type(arr_list),intent(inout) :: data(97:122)
    integer,intent(in) :: key ! key
    character,intent(in) :: c,c2 ! char
    integer :: tmp(:)
    integer :: i,i2
    integer :: j
    integer :: n

    i = ichar(c)
    i2 = ichar(c2)
    if ( i /= i2) then ! insert and delete
       if ( data(i)%N > 0) then
          j = found_point(data(i)%point(:),data(i)%n, key)
       else
          data(i)%N=data(i)%N+1
          allocate(data(i)%point(0:data(i)%N-1))
          data(i)%point(0:0) = key
          !
          ! delete
          !
          n = data(i2)%n
          data(i2)%n = data(i2)%n - 1
          if ( data(i2)%n > 0) then
             j = found_point(data(i2)%point(:),n, key)
             !allocate(tmp(0:n-1))
             tmp(0:n-1) = data(i2)%point(0:n-1)
             deallocate( data(i2)%point )
             allocate( data(i2)%point(0:data(i2)%n-1) )
             data(i2)%point(0:j-1) = tmp(0:j-1)
             data(i2)%point(j:) = tmp(j+1:n-1)
             !deallocate(tmp)
          else
             deallocate( data(i2)%point )
          end if
          return
       end if
       if ( j == data(i)%N) then ! out bound
          n = data(i)%n
          !allocate(tmp(0:n-1))
          tmp(0:n-1) = data(i)%point(0:n-1)
          deallocate( data(i)%point )
          data(i)%n = data(i)%n + 1
          allocate( data(i)%point(0:data(i)%n-1) )
          data(i)%point(0:n-1) = tmp(0:n-1)
          data(i)%point(n) = key
          !deallocate(tmp)
          !
          ! delete
          !
          n = data(i2)%n
          data(i2)%n = data(i2)%n - 1
          if ( data(i2)%n > 0) then
             j = found_point(data(i2)%point(:),n, key)
             !allocate(tmp(0:n-1))
             tmp(0:n-1) = data(i2)%point(0:n-1)
             deallocate( data(i2)%point )
             allocate( data(i2)%point(0:data(i2)%n-1) )
             data(i2)%point(0:j-1) = tmp(0:j-1)
             data(i2)%point(j:) = tmp(j+1:n-1)
             !deallocate(tmp)
          else
             deallocate( data(i2)%point )
          end if
          return
       else
          n = data(i)%n
          !allocate(tmp(0:n-1))
          tmp(0:n-1) = data(i)%point(0:n-1)
          deallocate( data(i)%point )
          data(i)%n = data(i)%n + 1
          allocate( data(i)%point(0:data(i)%n-1) )
          if ( j == 0 ) then
             data(i)%point(0:0) = key
             data(i)%point(j+1:) = tmp(0:n-1)
          else
             data(i)%point(0:j-1) = tmp(0:j-1)
             data(i)%point(j) = key
             data(i)%point(j+1:) = tmp(j:n-1)
          end if
          !deallocate(tmp)
          !
          ! delete
          !
          n = data(i2)%n
          data(i2)%n = data(i2)%n - 1
          if ( data(i2)%n > 0) then
             j = found_point(data(i2)%point(:),n, key)
             !allocate(tmp(0:n-1))
             tmp(0:n-1) = data(i2)%point(0:n-1)
             deallocate( data(i2)%point )
             allocate( data(i2)%point(0:data(i2)%n-1) )
             data(i2)%point(0:j-1) = tmp(0:j-1)
             data(i2)%point(j:) = tmp(j+1:n-1)
             !deallocate(tmp)
          else
             deallocate( data(i2)%point )
          end if
       end if
    end if
    return
  end subroutine insert_delete

  function found_point(a,n,key) result(ok)
    integer,intent(in) :: n
    integer,intent(in) :: a(0:n-1)
    integer,intent(in) :: key
    integer :: ng
    integer :: ok
    integer :: mid
    integer :: test
    ng = -1
    ok = n
    !do while( )
    test = 0
    do
       mid = (ok + ng)/2
       if (isOK(mid,key,a,n)) then
          ok = mid
       else
          ng = mid
       end if
       if (abs(ok-ng) <= 1) exit
    end do
  end function found_point

  function isOK (index, key,a,n) result(out)
    integer,intent(in):: n
    integer,intent(in) :: a(0:n-1)
    integer,intent(in) :: index, key
    logical :: out
    if ( a(index) >= key) then
       out = .true.
    else
       out = .false.
    end if
  end function isOK
  subroutine count_char(data,l,r,out)
    type(arr_list) :: data(97:122)
    integer :: l,r
    integer :: out
    integer :: N
    integer :: i, j, tmp
    out = 0
    do i = 97,122
       if ( data(i)%n > 0) then
          j = found_point(data(i)%point(:),data(i)%n,l)
          if ( j < data(i)%N) then
             if ( l<=data(i)%point(j) .and. data(i)%point(j) <= r) then
                out = out + 1
             end if
!!             if ( j+1 < data(i)%N) then
!!                if ( l<=data(i)%point(j) .and. data(i)%point(j) <= r) then
!!                   out = out + 1
!!                   cycle
!!                end if
!!                if ( data(i)%point(j) < l .and. data(i)%point(j+1) <= r) then
!!                   out = out + 1
!!                end if
!!             else
!!             end if
          end if
       end if
    end do
  end subroutine count_char

  subroutine count_char2(S,out)
    character(*) :: S
    integer :: out
    integer :: N
    integer :: i, tmp
    integer :: c(97:122)
    N = len_trim(S)
    c(97:122) = 0
    do i = 1,N
       tmp = ichar(S(i:i))
       c (tmp) = c(tmp) + 1
    end do
    out = count(c /=0)
  end subroutine count_char2
end program main

Score : 500 points

Problem Statement

You are given a string S of length N consisting of lowercase English letters.

Process Q queries of the following two types:

Type 1: change the i_q-th character of S to c_q. (Do nothing if the i_q-th character is already c_q.)

Type 2: answer the number of different characters occurring in the substring of S between the l_q-th and r_q-th characters (inclusive).

Constraints

N, Q, i_q, l_q, and r_q are integers.

S is a string consisting of lowercase English letters.

c_q is a lowercase English letter.

1 \leq N \leq 500000

1 \leq Q \leq 20000

|S| = N

1 \leq i_q \leq N

1 \leq l_q \leq r_q \leq N

There is at least one query of type 2 in each testcase.

Input

Input is given from Standard Input in the following format:

N
S
Q
Query_1
\vdots
Query_Q

Here, Query_i in the 4-th through (Q+3)-th lines is one of the following:

1 i_q c_q

2 l_q r_q

Output

For each query of type 2, print a line containing the answer.

Sample Input 1

7
abcdbbd
6
2 3 6
1 5 z
2 1 1
1 4 a
1 7 d
2 1 7

Sample Output 1

3
1
5

In the first query, cdbb contains three kinds of letters: b , c , and d, so we print 3.

In the second query, S is modified to abcdzbd.

In the third query, a contains one kind of letter: a, so we print 1.

In the fourth query, S is modified to abcazbd.

In the fifth query, S does not change and is still abcazbd.

In the sixth query, abcazbd contains five kinds of letters: a, b, c, d, and z, so we print 5.


program main
  implicit none
  type arr_list
     integer :: N
     integer,allocatable :: point(:)
  end type arr_list
  type(arr_list),allocatable :: data(:)
  integer :: num(97:122)
  integer :: N, Q
  character(len=500000) :: S
  integer(2),allocatable :: S2(:)
  !character(:),allocatable :: tmp
  integer(2),allocatable :: tmp(:)
  character(len=1) :: c,b,c2
  integer :: a
  integer :: i,j, l,r
  integer :: out
  read(*,*) N
  read(*,*) S
  read(*,*) Q
  allocate(S2(N))
  do i = 1,N
     S2(i) =  ichar(S(i:i))
  end do
  do i = 1, Q
     read(*,'(i1,1x,a,1x,a)') a,b,c
     if (a == 1) then
        backspace(5)
        !read(*,'(i1,1x,g0,1x,a)') a,j,c
        read(*,*) a,j,c
        S2(j:j) = ichar(c)
     else
        backspace(5)
!        read(*,'(i1,1x,g0,1x,g0)') a,l,r
        read(*,*) a,l,r
        !tmp = S(l:r)
        !call count_char(S(l:r),out)
        call count_char2(S2(l:r),out)
        write(*,'(i0)') out
        !deallocate(tmp)
     end if

  end do
contains
  subroutine count_char2(S,out)
    integer(2) :: S(:)
    integer :: out
    integer :: N
    integer :: i, tmp
    integer :: c(97:122)
    N = size(S)
    c(97:122) = 0
    do i = 1,N
       c (S(i)) = c(S(i)) + 1
    end do
    out = count(c /=0)
  end subroutine count_char2
end program main