program kadai
implicit none

integer(8)  ::  n
integer(8)  ::  q
integer(8),allocatable  ::  a(:)
integer(8),allocatable  ::  b(:)
integer(8),allocatable  ::  p(:)
integer(8),allocatable  ::  x(:)
integer(8),allocatable  ::  counter(:)
integer(8),allocatable  ::  bu(:,:)
integer(8)  ::  i , j , k

read(*,*) n,q
allocate(a(n-1))
allocate(b(n-1))
allocate(p(q))
allocate(x(q))
allocate(counter(n))
allocate(bu(n,n))

do i=1,n-1
    read(*,*) a(i),b(i)
end do

do i=1,q
    read(*,*) p(i),x(i)
end do

bu=0
counter=0

do i=1,n
    k=1
    do j=1,n-1
        if (a(j)==i .and. k<n+1) then
            bu(i,k)=b(j)
            k=k+1
        end if
    end do
end do

do i=1,q
    call plus(counter,bu(p(i),:),x(i),n)
    j=1
    do while(bu(p(i),j)>0 .and. j<n+1)
        call plus(counter,bu(bu(p(i),j),:),x(i),n)
        j=j+1
    end do

    counter(p(i))=counter(p(i))+x(i)

end do

write(*,*) counter

stop
contains

subroutine plus(counter,num,x,n)
    implicit none
    integer(8),intent(inout)  ::  counter(:)
    integer(8),intent(in)  ::  num(:)
    integer(8),intent(in)  ::  x
    integer(8),intent(in)  ::  n
    integer(8)  ::  m

    m=1
    do while(num(m)>0 .and. m<n+1)
        counter(num(m))=counter(num(m))+x
        m=m+1
    end do

    return
end subroutine  plus

end program kadai


module pair_stack
    implicit none
    private
    integer, allocatable:: stack(:)
    integer:: depth, max_depth
    public:: init, pop, push, is_remaining

contains
    subroutine init(x)
        integer, intent(in):: x
        max_depth = x
        allocate(stack(x))
        stack(:) = 0
        depth = 0
    end subroutine

    subroutine pop(a)
        integer, intent(out):: a
        if (depth <= 0)stop '-- --Stack is empty'
        a = stack(depth)
        depth = depth - 1
        ! print*, '-- -- poped =>', a, b
    end subroutine

    subroutine push(a)
        integer, intent(in):: a
        if (depth >= max_depth)stop '-- --Stack over flow'
        ! print*, '-- -- pushed =>', a, b
        depth = depth + 1
        stack(depth) = a
    end subroutine

    function is_remaining() result(ret)
        logical:: ret
        if (depth <= 0)then
            ret = .false.
        else
            ret = .true.
        end if
    end function
end module

program main
    implicit none
    type to
        integer,allocatable :: arr(:)
    end type
    integer:: N, Q
    integer, allocatable:: px(:, :), counter(:), ab(:, :), cnum(:)
    type(to),allocatable :: tree(:)
    integer:: i,a,b

    read*, N,Q
    allocate(px(2, Q), counter(N), ab(2, N-1), cnum(N), source=0)
    allocate(tree(N))

    do i=1, N-1
        read*, a,b
        cnum(a) = cnum(a) + 1
        cnum(b) = cnum(b) + 1
        ab(1,i) = a
        ab(2,i) = b
    enddo

    read*, px(1:2, 1:Q)

    do i=1,N
        allocate(tree(i)%arr(cnum(i)),source=0)
    enddo

    cnum(1:N) = 1

    do i=1,N-1
        a = ab(1, i)
        b = ab(2, i)
        tree(a)%arr(cnum(a)) = b
        tree(b)%arr(cnum(b)) = a
        cnum(a) = cnum(a) + 1
        cnum(b) = cnum(b) + 1
    enddo

    do i=1,Q
        counter(px(1, i)) = counter(px(1, i)) + px(2, i)
    enddo

    call dfs()

    do i=1, N
        print*, counter(i)
    enddo

contains
    subroutine dfs()
        use pair_stack
        integer:: v, u, p, tmp, child
        logical:: visited(N)
        call init(10 + int(log10(real(N))))
        call push(1)
        child = 0
        p = 0
        visited(1:N) = .false.
        do while(is_remaining())
            call pop(v)
            visited(v) = .true.
            do i=1, size(tree(v)%arr)
                u = tree(v)%arr(i)
                if(visited(u)) cycle
                call push(u)
                counter(u) = counter(u) + counter(v)
            enddo
        enddo

    end subroutine
end program main

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

Score : 400 points

Problem Statement

Given is a rooted tree with N vertices numbered 1 to N.
The root is Vertex 1, and the i-th edge (1 \leq i \leq N - 1) connects Vertex a_i and b_i.

Each of the vertices has a counter installed. Initially, the counters on all the vertices have the value 0.

Now, the following Q operations will be performed:

Operation j (1 \leq j \leq Q): Increment by x_j the counter on every vertex contained in the subtree rooted at Vertex p_j.

Find the value of the counter on each vertex after all operations.

Constraints

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

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

1 \leq a_i < b_i \leq N

1 \leq p_j \leq N

1 \leq x_j \leq 10^4

The given graph is a tree.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
a_1 b_1
:
a_{N-1} b_{N-1}
p_1 x_1
:
p_Q x_Q

Output

Print the values of the counters on Vertex 1, 2, \ldots, N after all operations, in this order, with spaces in between.

Sample Input 1

4 3
1 2
2 3
2 4
2 10
1 100
3 1

Sample Output 1

100 110 111 110

The tree in this input is as follows:

Each operation changes the values of the counters on the vertices as follows:

Operation 1: Increment by 10 the counter on every vertex contained in the subtree rooted at Vertex 2, that is, Vertex 2, 3, 4. The values of the counters on Vertex 1, 2, 3, 4 are now 0, 10, 10, 10, respectively.

Operation 2: Increment by 100 the counter on every vertex contained in the subtree rooted at Vertex 1, that is, Vertex 1, 2, 3, 4. The values of the counters on Vertex 1, 2, 3, 4 are now 100, 110, 110, 110, respectively.

Operation 3: Increment by 1 the counter on every vertex contained in the subtree rooted at Vertex 3, that is, Vertex 3. The values of the counters on Vertex 1, 2, 3, 4 are now 100, 110, 111, 110, respectively.

Sample Input 2

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

Sample Output 2

20 20 20 20 20 20

Submitted source-code file size, in bytes.

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

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


module pair_stack
    implicit none
    private
    integer, allocatable:: stack(:)
    integer:: depth, max_depth
    public:: init, pop, push, is_remaining

contains
    subroutine init(x)
        integer, intent(in):: x
        max_depth = x
        allocate(stack(x))
        stack(:) = 0
        depth = 0
    end subroutine

    subroutine pop(a)
        integer, intent(out):: a
        if (depth <= 0)stop '-- --Stack is empty'
        a = stack(depth)
        depth = depth - 1
        ! print*, '-- -- poped =>', a, b
    end subroutine

    subroutine push(a)
        integer, intent(in):: a
        if (depth >= max_depth)stop '-- --Stack over flow'
        ! print*, '-- -- pushed =>', a, b
        depth = depth + 1
        stack(depth) = a
    end subroutine

    function is_remaining() result(ret)
        logical:: ret
        if (depth <= 0)then
            ret = .false.
        else
            ret = .true.
        end if
    end function
end module

program main
    implicit none
    type to
        integer,allocatable :: arr(:)
    end type
    integer:: N, Q
    integer, allocatable:: px(:, :), counter(:), ab(:, :), cnum(:)
    type(to),allocatable :: tree(:)
    integer:: i,a,b

    read*, N,Q
    allocate(px(2, Q), counter(N))
    allocate(tree(N), ab(2, N-1), cnum(N))
    cnum(1:N) = 0

    do i=1, N-1
        read*, a,b
        cnum(a) = cnum(a) + 1
        cnum(b) = cnum(b) + 1
        ab(1,i) = a
        ab(2,i) = b
    enddo

    read*, px(1:2, 1:Q)

    do i=1,N
        allocate(tree(i)%arr(cnum(i)))
    enddo

    cnum(1:N) = 1

    do i=1,N-1
        a = ab(1, i)
        b = ab(2, i)
        tree(a)%arr(cnum(a)) = b
        tree(b)%arr(cnum(b)) = a
        cnum(a) = cnum(a) + 1
        cnum(b) = cnum(b) + 1
    enddo

    do i=1,Q
        counter(px(1, i)) = counter(px(1, i)) + px(2, i)
    enddo

    call dfs()

    do i=1, N
        write(*, '(i0,1x)',advance='no') counter(i)
    enddo
    print*, ''

contains
    subroutine dfs()
        use pair_stack
        integer:: v, u, p, tmp, child
        logical:: visited(N)
        call init(10 + int(log10(real(N))))
        call push(1)
        child = 0
        p = 0
        visited(1:N) = .false.
        do while(is_remaining())
            call pop(v)
            visited(v) = .true.
            do i=1, size(tree(v)%arr)
                u = tree(v)%arr(i)
                if(visited(u)) cycle
                call push(u)
                counter(u) = counter(u) + counter(v)
            enddo
        enddo

    end subroutine
end program main


module pair_stack
    implicit none
    private
    integer, allocatable:: stack(:)
    integer:: depth, max_depth
    public:: init, pop, push, is_remaining

contains
    subroutine init(x)
        integer, intent(in):: x
        max_depth = x
        allocate(stack(x))
        stack(:) = 0
        depth = 0
    end subroutine

    subroutine pop(a)
        integer, intent(out):: a
        if (depth <= 0)stop '-- --Stack is empty'
        a = stack(depth)
        depth = depth - 1
        ! print*, '-- -- poped =>', a, b
    end subroutine

    subroutine push(a)
        integer, intent(in):: a
        if (depth >= max_depth)stop '-- --Stack over flow'
        ! print*, '-- -- pushed =>', a, b
        depth = depth + 1
        stack(depth) = a
    end subroutine

    function is_remaining() result(ret)
        logical:: ret
        if (depth <= 0)then
            ret = .false.
        else
            ret = .true.
        end if
    end function
end module

program main
    implicit none
    type to
        integer,allocatable :: arr(:)
    end type
    integer:: N, Q
    integer, allocatable:: px(:, :), counter(:), ab(:, :), cnum(:)
    type(to),allocatable :: tree(:)
    integer:: i,a,b

    read*, N,Q
    allocate(px(2, Q), counter(N))
    allocate(tree(N), ab(2, N-1), cnum(N))
    cnum(1:N) = 0

    do i=1, N-1
        read*, a,b
        cnum(a) = cnum(a) + 1
        cnum(b) = cnum(b) + 1
        ab(1,i) = a
        ab(2,i) = b
    enddo

    read*, px(1:2, 1:Q)

    do i=1,N
        allocate(tree(i)%arr(cnum(i)))
    enddo

    cnum(1:N) = 1

    do i=1,N-1
        a = ab(1, i)
        b = ab(2, i)
        tree(a)%arr(cnum(a)) = b
        tree(b)%arr(cnum(b)) = a
        cnum(a) = cnum(a) + 1
        cnum(b) = cnum(b) + 1
    enddo

    do i=1,Q
        counter(px(1, i)) = counter(px(1, i)) + px(2, i)
    enddo

    call dfs()

    do i=1, N
        print'(i0)', counter(i)
    enddo

contains
    subroutine dfs()
        use pair_stack
        integer:: v, u, p, tmp, child
        logical:: visited(N)
        call init(10 + int(log10(real(N))))
        call push(1)
        child = 0
        p = 0
        visited(1:N) = .false.
        do while(is_remaining())
            call pop(v)
            visited(v) = .true.
            do i=1, size(tree(v)%arr)
                u = tree(v)%arr(i)
                if(visited(u)) cycle
                call push(u)
                counter(u) = counter(u) + counter(v)
            enddo
        enddo

    end subroutine
end program main


module pair_stack
    implicit none
    private
    integer, allocatable:: stack(:)
    integer:: depth, max_depth
    public:: init, pop, push, is_remaining

contains
    subroutine init(x)
        integer, intent(in):: x
        max_depth = x
        allocate(stack(x))
        stack(:) = 0
        depth = 0
    end subroutine

    subroutine pop(a)
        integer, intent(out):: a
        if (depth <= 0)stop '-- --Stack is empty'
        a = stack(depth)
        depth = depth - 1
        ! print*, '-- -- poped =>', a, b
    end subroutine

    subroutine push(a)
        integer, intent(in):: a
        if (depth >= max_depth)stop '-- --Stack over flow'
        ! print*, '-- -- pushed =>', a, b
        depth = depth + 1
        stack(depth) = a
    end subroutine

    function is_remaining() result(ret)
        logical:: ret
        if (depth <= 0)then
            ret = .false.
        else
            ret = .true.
        end if
    end function
end module

program main
    implicit none
    integer:: N, Q
    integer, allocatable:: ab(:, :), px(:, :), counter(:)
    integer:: i

    read*, N,Q
    allocate(ab(2, N-1), px(2, Q), counter(N))
    read*, ab(1:2, 1:N-1)
    read*, px(1:2, 1:Q)
    counter(1:N) = 0

    do i=1,Q
        counter(px(1,i)) = counter(px(1, i)) + px(2,i)
    end do

    ! call quick_sort(ab)

    do i=1,N-1
        counter(ab(2,i)) = counter(ab(2,i)) + counter(ab(1,i))
    end do

    do i = 1, N
        write(*, '(i0,1x)',advance='no')counter(i)
    end do
    print*, ''

contains
    subroutine quick_sort(arr)
        use pair_stack
        integer, intent(inout):: arr(:, :)
        integer:: p_seek, sorted,trg
        integer:: afa
        integer:: i,j

        do i = 1,N-1
            if(arr(1,i) == 1)then
                call swap(arr(1,i), arr(1,1))
                call swap(arr(2,i), arr(2,1))
                exit
            endif
        enddo
        call init(N+1)
        call push(arr(1,1))
        call push(arr(2,1))
        sorted = 1
        do while(is_remaining())
            call pop(trg)
            do i = sorted+1, N-1
                if(arr(1,i) == trg)then
                    call swap(arr(1,i), arr(1,sorted+1))
                    call swap(arr(2,i), arr(2,sorted+1))
                    sorted = sorted + 1
                    call push(arr(1,sorted))
                    call push(arr(2,sorted))
                    exit
                else if(arr(2,i) == trg)then
                    call swap(arr(1,i), arr(2,i))
                    call swap(arr(1,i), arr(1,sorted+1))
                    call swap(arr(2,i), arr(2,sorted+1))
                    sorted = sorted + 1
                    call push(arr(1,sorted))
                    call push(arr(2,sorted))
                    exit
                end if
            end do
        end do
    end subroutine

    subroutine swap(a,b)
        integer, intent(inout):: a,b
        integer:: r
        r = a; a = b; b = r
    end subroutine
end program main


module pair_stack
    implicit none
    private
    integer, allocatable:: stack(:)
    integer:: depth, max_depth
    public:: init, pop, push, is_remaining

contains
    subroutine init(x)
        integer, intent(in):: x
        max_depth = x
        allocate(stack(x))
        stack(:) = 0
        depth = 0
    end subroutine

    subroutine pop(a)
        integer, intent(out):: a
        if (depth <= 0)stop '-- --Stack is empty'
        a = stack(depth)
        depth = depth - 1
        ! print*, '-- -- poped =>', a, b
    end subroutine

    subroutine push(a)
        integer, intent(in):: a
        if (depth >= max_depth)stop '-- --Stack over flow'
        ! print*, '-- -- pushed =>', a, b
        depth = depth + 1
        stack(depth) = a
    end subroutine

    function is_remaining() result(ret)
        logical:: ret
        if (depth <= 0)then
            ret = .false.
        else
            ret = .true.
        end if
    end function
end module

program main
    implicit none
    integer:: N, Q
    integer, allocatable:: ab(:, :), px(:, :), counter(:)
    integer:: i

    read*, N,Q
    allocate(ab(2, N-1), px(2, Q), counter(N))
    read*, ab(1:2, 1:N-1)
    read*, px(1:2, 1:Q)
    counter(1:N) = 0

    do i=1,Q
        counter(px(1,i)) = counter(px(1, i)) + px(2,i)
    end do

    call quick_sort(ab)

    do i=1,N-1
        counter(ab(2,i)) = counter(ab(2,i)) + counter(ab(1,i))
    end do

    do i = 1, N
        write(*, '(i0,1x)',advance='no')counter(i)
    end do
    print*, ''

contains
    subroutine quick_sort(arr)
        use pair_stack
        integer, intent(inout):: arr(:, :)
        integer:: p_seek, sorted,trg
        integer:: afa
        integer:: i,j

        do i = 1,N-1
            if(arr(1,i) == 1)then
                call swap(arr(1,i), arr(1,1))
                call swap(arr(2,i), arr(2,1))
                exit
            endif
        enddo
        call init(N+1)
        call push(arr(1,1))
        call push(arr(2,1))
        sorted = 1
        do while(is_remaining())
            call pop(trg)
            do i = sorted+1, N-1
                if(arr(1,i) == trg)then
                    call swap(arr(1,i), arr(1,sorted+1))
                    call swap(arr(2,i), arr(2,sorted+1))
                    sorted = sorted + 1
                    call push(arr(1,sorted))
                    call push(arr(2,sorted))
                    exit
                else if(arr(2,i) == trg)then
                    call swap(arr(1,i), arr(2,i))
                    call swap(arr(1,i), arr(1,sorted+1))
                    call swap(arr(2,i), arr(2,sorted+1))
                    sorted = sorted + 1
                    call push(arr(1,sorted))
                    call push(arr(2,sorted))
                    exit
                end if
            end do
        end do
    end subroutine

    subroutine swap(a,b)
        integer, intent(inout):: a,b
        integer:: r
        r = a; a = b; b = r
    end subroutine
end program main


module pair_stack
    implicit none
    private
    integer, allocatable:: stack(:,:)
    integer:: depth, max_depth
    public:: init, pop, push, is_remaining

contains
    subroutine init(x)
        integer, intent(in):: x
        max_depth = x
        allocate(stack(2,x))
        stack(:, :) = 0
        depth = 0
    end subroutine

    subroutine pop(a, b)
        integer, intent(out):: a, b
        if (depth <= 0)stop '-- --Stack is empty'
        a = stack(1, depth)
        b = stack(2, depth)
        depth = depth - 1
        ! print*, '-- -- poped =>', a, b
    end subroutine

    subroutine push(a, b)
        integer, intent(in):: a, b
        if (depth >= max_depth)stop '-- --Stack over flow'
        ! print*, '-- -- pushed =>', a, b
        depth = depth + 1
        stack(1, depth) = a
        stack(2, depth) = b
    end subroutine

    function is_remaining() result(ret)
        logical:: ret
        if (depth <= 0)then
            ret = .false.
        else
            ret = .true.
        end if
    end function
end module

program main
    implicit none
    integer:: N, Q
    integer, allocatable:: ab(:, :), px(:, :), counter(:)
    integer:: i

    read*, N,Q
    allocate(ab(2, N-1), px(2, Q), counter(N))
    read*, ab(1:2, 1:N-1)
    read*, px(1:2, 1:Q)
    counter(1:N) = 0

    do i=1,Q
        counter(px(1,i)) = counter(px(1, i)) + px(2,i)
    end do

    call quick_sort(ab)

    do i=1,N-1
        counter(ab(2,i)) = counter(ab(2,i)) + counter(ab(1,i))
    end do

    do i = 1, N
        write(*, '(i0,1x)',advance='no')counter(i)
    end do

contains
    subroutine quick_sort(arr)
        use pair_stack
        integer, intent(inout):: arr(:, :)
        integer:: left, right, pl, pr
        integer:: pv
        call init(int(log10(real(size(arr(1, :)))) + 10))
        call push(1,size(arr(1, :)))
        do while(is_remaining())
            ! print*, '-- -- 1'
            call pop(left, right)
            pl = left; pr = right
            pv = (left+right)/2
            ! print*, '-- left, right, pv =>', left, right, pv
            do while(pl <= pr)
                ! print*, '-- serch_exchange'
                do while(arr(1, pl) < arr(1, pv))
                    pl = pl + 1
                end do
                ! print*, '-- -- left >>', pl, arr(1, pl), arr(1, pv)

                do while(arr(1, pr) > arr(1, pv))
                    pr = pr - 1
                end do
                ! print*, '-- -- right >>', pr, arr(1, pr), arr(1, pv)

                if(pl <= pr)then
                    call swap(arr(1,pl), arr(1,pr))
                    call swap(arr(2,pl), arr(2,pr))
                    pl = pl + 1
                    pr = pr - 1
                    ! print*, '-- -- swapped'

                end if
            end do

            if((pl-1)-left >= right - pl)then
                if(pl < right)then
                    call push(left, pl-1)
                    call push(pl, right)
                else if(left < pr-1)then
                    call push(left, pl-1)
                end if
            else
                if(left < pl-1) then
                    call push(pl, right)
                    call push(left, pl-1)
                else if(pl < right)then
                    call push(pl, right)
                end if
            end if
        end do
    end subroutine

    subroutine swap(a,b)
        integer, intent(inout):: a,b
        integer:: r
        r = a; a = b; b = r
    end subroutine
end program main


module pair_stack
    implicit none
    private
    integer, allocatable:: stack(:,:)
    integer:: depth, max_depth
    public:: init, pop, push, is_remaining

contains
    subroutine init(x)
        integer, intent(in):: x
        max_depth = x
        allocate(stack(2,x))
        stack(:, :) = 0
        depth = 0
    end subroutine

    subroutine pop(a, b)
        integer, intent(out):: a, b
        if (depth <= 0)stop '-- --Stack is empty'
        a = stack(1, depth)
        b = stack(2, depth)
        depth = depth - 1
        ! print*, '-- -- poped =>', a, b
    end subroutine

    subroutine push(a, b)
        integer, intent(in):: a, b
        if (depth >= max_depth)stop '-- --Stack over flow'
        ! print*, '-- -- pushed =>', a, b
        depth = depth + 1
        stack(1, depth) = a
        stack(2, depth) = b
    end subroutine

    function is_remaining() result(ret)
        logical:: ret
        if (depth <= 0)then
            ret = .false.
        else
            ret = .true.
        end if
    end function
end module

program main
    implicit none
    integer:: N, Q
    integer, allocatable:: ab(:, :), px(:, :), counter(:)
    integer:: i

    read*, N,Q
    allocate(ab(2, N-1), px(2, Q), counter(N))
    read*, ab(1:2, 1:N-1)
    read*, px(1:2, 1:Q)
    counter(1:N) = 0

    do i=1,Q
        counter(px(1,i)) = counter(px(1, i)) + px(2,i)
    end do

    !call quick_sort(ab)

    do i=1,N-1
        counter(ab(2,i)) = counter(ab(2,i)) + counter(ab(1,i))
    end do

    do i = 1, N
        write(*, '(i0,1x)',advance='no')counter(i)
    end do

contains
    subroutine quick_sort(arr)
        use pair_stack
        integer, intent(inout):: arr(:, :)
        integer:: left, right, pl, pr
        integer:: pv
        call init(int(log10(real(size(arr(1, :)))) + 10))
        call push(1,size(arr(1, :)))
        do while(is_remaining())
            ! print*, '-- -- 1'
            call pop(left, right)
            pl = left; pr = right
            pv = (left+right)/2
            ! print*, '-- left, right, pv =>', left, right, pv
            do while(pl <= pr)
                ! print*, '-- serch_exchange'
                do while(arr(1, pl) < arr(1, pv))
                    pl = pl + 1
                end do
                ! print*, '-- -- left >>', pl, arr(1, pl), arr(1, pv)

                do while(arr(1, pr) > arr(1, pv))
                    pr = pr - 1
                end do
                ! print*, '-- -- right >>', pr, arr(1, pr), arr(1, pv)

                if(pl <= pr)then
                    call swap(arr(1,pl), arr(1,pr))
                    call swap(arr(2,pl), arr(2,pr))
                    pl = pl + 1
                    pr = pr - 1
                    ! print*, '-- -- swapped'

                end if
            end do

            if((pl-1)-left >= right - pl)then
                if(pl < right)then
                    call push(left, pl-1)
                    call push(pl, right)
                else if(left < pr-1)then
                    call push(left, pl-1)
                end if
            else
                if(left < pl-1) then
                    call push(pl, right)
                    call push(left, pl-1)
                else if(pl < right)then
                    call push(pl, right)
                end if
            end if
        end do
    end subroutine

    subroutine swap(a,b)
        integer, intent(inout):: a,b
        integer:: r
        r = a; a = b; b = r
    end subroutine
end program main


module pair_stack
    implicit none
    private
    integer, allocatable:: stack(:,:)
    integer:: depth, max_depth
    public:: init, pop, push, is_remaining

contains
    subroutine init(x)
        integer, intent(in):: x
        max_depth = x
        allocate(stack(2,x))
        stack(:, :) = 0
        depth = 0
    end subroutine

    subroutine pop(a, b)
        integer, intent(out):: a, b
        if (depth <= 0)stop '-- --Stack is empty'
        a = stack(1, depth)
        b = stack(2, depth)
        depth = depth - 1
        ! print*, '-- -- poped =>', a, b
    end subroutine

    subroutine push(a, b)
        integer, intent(in):: a, b
        if (depth >= max_depth)stop '-- --Stack over flow'
        ! print*, '-- -- pushed =>', a, b
        depth = depth + 1
        stack(1, depth) = a
        stack(2, depth) = b
    end subroutine

    function is_remaining() result(ret)
        logical:: ret
        if (depth <= 0)then
            ret = .false.
        else
            ret = .true.
        end if
    end function
end module

program main
    implicit none
    integer:: N, Q
    integer, allocatable:: ab(:, :), px(:, :), counter(:)
    integer:: i

    read*, N,Q
    allocate(ab(2, N-1), px(2, Q), counter(N))
    read*, ab(1:2, 1:N-1)
    read*, px(1:2, 1:Q)
    counter(1:N) = 0

    do i=1,Q
        counter(px(1,i)) = counter(px(1, i)) + px(2,i)
    end do

    call quick_sort(ab)

    do i=1,N-1
        counter(ab(2,i)) = counter(ab(2,i)) + counter(ab(1,i))
    end do

    print*, counter(1:N)

contains
    subroutine quick_sort(arr)
        use pair_stack
        integer, intent(inout):: arr(:, :)
        integer:: left, right, pl, pr
        integer:: pv
        call init(int(log10(real(size(arr(1, :)))) + 10))
        call push(1,size(arr(1, :)))
        do while(is_remaining())
            ! print*, '-- -- 1'
            call pop(left, right)
            pl = left; pr = right
            pv = (left+right)/2
            ! print*, '-- left, right, pv =>', left, right, pv
            do while(pl <= pr)
                ! print*, '-- serch_exchange'
                do while(arr(1, pl) < arr(1, pv))
                    pl = pl + 1
                end do
                ! print*, '-- -- left >>', pl, arr(1, pl), arr(1, pv)

                do while(arr(1, pr) > arr(1, pv))
                    pr = pr - 1
                end do
                ! print*, '-- -- right >>', pr, arr(1, pr), arr(1, pv)

                if(pl <= pr)then
                    call swap(arr(1,pl), arr(1,pr))
                    call swap(arr(2,pl), arr(2,pr))
                    pl = pl + 1
                    pr = pr - 1
                    ! print*, '-- -- swapped'

                end if
            end do

            if((pl-1)-left >= right - pl)then
                if(pl < right)then
                    call push(left, pl-1)
                    call push(pl, right)
                else if(left < pr-1)then
                    call push(left, pl-1)
                end if
            else
                if(left < pl-1) then
                    call push(pl, right)
                    call push(left, pl-1)
                else if(pl < right)then
                    call push(pl, right)
                end if
            end if
        end do
    end subroutine

    subroutine swap(a,b)
        integer, intent(inout):: a,b
        integer:: r
        r = a; a = b; b = r
    end subroutine
end program main


program kadai
implicit none

integer  ::  n
integer  ::  q
integer,allocatable  ::  a(:)
integer,allocatable  ::  b(:)
integer,allocatable  ::  p(:)
integer,allocatable  ::  x(:)
integer,allocatable  ::  count(:)
integer,allocatable  ::  bu(:,:)
integer  ::  i , j , k

read(*,*) n,q
allocate(a(n-1))
allocate(b(n-1))
allocate(p(q))
allocate(x(q))
allocate(count(n))
allocate(bu(n,n))

count=0
do i=1,n-1
    read(*,*) a(i),b(i)
end do

do i=1,q
    read(*,*) p(i),x(i)
end do

bu=0

do i=1,n
    k=1
    do j=1,n-1
        if (a(j)==i .and. k<n+1) then
            bu(i,k)=b(j)
            k=k+1
        end if
    end do
end do

do i=1,q
    call plus(count,bu(p(i),:),x(i),n)
    j=1
    do while(bu(p(i),j)>0 .and. j<n+1)
        call plus(count,bu(bu(p(i),j),:),x(i),n)
        j=j+1
    end do

    count(p(i))=count(p(i))+x(i)

end do

write(*,*) count

stop
contains

subroutine plus(count,num,x,n)
    implicit none
    integer,intent(inout)  ::  count(:)
    integer,intent(in)  ::  num(:)
    integer,intent(in)  ::  x
    integer,intent(in)  ::  n
    integer  ::  i

    i=1
    do while(num(i)>0 .and. i<n+1)
        count(num(i))=count(num(i))+x
        i=i+1
    end do

    return
end subroutine  plus

end program kadai


program kadai
implicit none

integer  ::  n
integer  ::  q
integer,allocatable  ::  a(:)
integer,allocatable  ::  b(:)
integer,allocatable  ::  p(:)
integer,allocatable  ::  x(:)
integer,allocatable  ::  count(:)
integer,allocatable  ::  bu(:,:)
integer  ::  i , j , k

read(*,*) n,q
allocate(a(n-1))
allocate(b(n-1))
allocate(p(q))
allocate(x(q))
allocate(count(n))
allocate(bu(n,n))

count=0
do i=1,n-1
    read(*,*) a(i),b(i)
end do

do i=1,q
    read(*,*) p(i),x(i)
end do

bu=0

do i=1,n
    k=1
    do j=1,n-1
        if (a(j)==i) then
            bu(i,k)=b(j)
            k=k+1
        end if
    end do
end do

do i=1,q
    call plus(count,bu(p(i),:),x(i),n)
    j=1
    do while(bu(p(i),j)>0 .and. j<n+1)
        call plus(count,bu(bu(p(i),j),:),x(i),n)
        j=j+1
    end do

    count(p(i))=count(p(i))+x(i)

end do

write(*,*) count

stop
contains

subroutine plus(count,num,x,n)
    implicit none
    integer,intent(inout)  ::  count(:)
    integer,intent(in)  ::  num(:)
    integer,intent(in)  ::  x
    integer,intent(in)  ::  n
    integer  ::  i

    i=1
    do while(num(i)>0 .and. i<n+1)
        count(num(i))=count(num(i))+x
        i=i+1
    end do

    return
end subroutine  plus

end program kadai


program kadai
implicit none

integer  ::  n
integer  ::  q
integer,allocatable  ::  a(:)
integer,allocatable  ::  b(:)
integer,allocatable  ::  p(:)
integer,allocatable  ::  x(:)
integer,allocatable  ::  count(:)
integer,allocatable  ::  bu(:,:)
integer  ::  i , j , k

read(*,*) n,q
allocate(a(n-1))
allocate(b(n-1))
allocate(p(q))
allocate(x(q))
allocate(count(n))
allocate(bu(n,n))

count=0
do i=1,n-1
    read(*,*) a(i),b(i)
end do

do i=1,q
    read(*,*) p(i),x(i)
end do

bu=0

do i=1,n
    k=1
    do j=1,n-1
        if (a(j)==i) then
            bu(i,k)=b(j)
            k=k+1
        end if
    end do
end do

do i=1,q
    call plus(count,bu(p(i),:),x(i))
    j=1
    do while(bu(p(i),j)>0 .and. j<100000)
        call plus(count,bu(bu(p(i),j),:),x(i))
        j=j+1
    end do

    count(p(i))=count(p(i))+x(i)

end do

write(*,*) count

stop
contains

subroutine plus(count,num,x)
    implicit none
    integer,intent(inout)  ::  count(:)
    integer,intent(in)  ::  num(:)
    integer,intent(in)  ::  x
    integer  ::  i

    i=1
    do while(num(i)>0 .and. i<100000)
        count(num(i))=count(num(i))+x
        i=i+1
    end do

    return
end subroutine  plus

end program kadai


module mod_weighted_graph
  implicit none
  integer(8), parameter :: long_infty = lshift(1_8, 62)
  integer, parameter :: int_infty = lshift(1, 30)

  type t_edge
    integer :: from = 0
    integer :: to = 0
    integer :: capacity = 0
    integer :: reverse = 0
    integer(8) :: weight = 0
  end type

  type t_list
    type(t_edge), allocatable :: at(:)
  end type

  type weighted_graph
    type(t_list), allocatable :: list(:)
  contains
    procedure :: dijkstra => dijkstra
    procedure :: ford_fulkerson => ford_fulkerson
  end type

  type t_node
    type(t_node), private, pointer :: left => null(), right => null()
    integer :: rank = 1
    integer :: number
    integer(8) :: cost
  end type

  type leftist_heap
    integer :: size = 0
    type(t_node), private, pointer :: root => null()
  end type

  interface weighted_graph
    module procedure :: newwg0
  end interface weighted_graph

contains

  type(t_edge) function newe0(from, to, weight, capacity, reverse) result(res)
    integer, intent(in) :: from, to
    integer(8), intent(in) :: weight
    integer, optional, intent(in) :: capacity, reverse
    res%from = from
    res%to = to
    res%weight = weight
    if (present(capacity)) res%capacity = capacity
    if (present(reverse)) res%reverse = reverse
  end

  type(weighted_graph) function newwg0(n, m, a, b, weight, capacity, undirected) result(res)
    integer, intent(in) :: n, m, a(:), b(:)
    integer(8), intent(in) :: weight(:)
    integer, optional, intent(in) :: capacity(:)
    logical, optional, intent(in) :: undirected
    logical :: u, cap
    integer :: c(n), i
    u = merge(undirected, .true., present(undirected))
    cap = present(capacity)
    c = 0
    do i = 1, m
      c(a(i)) = c(a(i)) + 1
      if (cap) c(b(i)) = c(b(i)) + 1
      if (u) then
        c(b(i)) = c(b(i)) + 1
        if (cap) c(a(i)) = c(a(i)) + 1
      end if
    end do
    allocate(res%list(n))
    do i = 1, n
      allocate(res%list(i)%at(c(i)))
    end do
    do i = m, 1, -1
      if (cap) then
        res%list(a(i))%at(c(a(i))) = newe0(a(i), b(i), weight(i), capacity(i), c(b(i)))
        res%list(b(i))%at(c(b(i))) = newe0(b(i), a(i), weight(i), 0, c(a(i)))
        c(a(i)) = c(a(i)) - 1
        c(b(i)) = c(b(i)) - 1
      else
        res%list(a(i))%at(c(a(i))) = newe0(a(i), b(i), weight(i))
        c(a(i)) = c(a(i)) - 1
      end if
      if (u) then
        if (cap) then
          res%list(b(i))%at(c(b(i))) = newe0(b(i), a(i), weight(i), capacity(i), c(a(i)))
          res%list(a(i))%at(c(a(i))) = newe0(a(i), b(i), weight(i), 0, c(b(i)))
          c(a(i)) = c(a(i)) - 1
          c(b(i)) = c(b(i)) - 1
        else
          res%list(b(i))%at(c(b(i))) = newe0(b(i), a(i), weight(i))
          c(b(i)) = c(b(i)) - 1
        end if
      end if
    end do
  end

  logical function less(a, b) result(res)
    type(t_node), pointer, intent(in) :: a, b
    res = a%cost < b%cost
  end

  subroutine swap(a, b)
    type(t_node), pointer, intent(inout) :: a, b
    type(t_node), pointer :: c
    c => a
    a => b
    b => c
  end

  function newn0(number, cost) result(res)
    integer, intent(in) :: number
    integer(8), intent(in) :: cost
    type(t_node), pointer :: res
    res => null()
    allocate(res)
    res%number = number
    res%cost = cost
  end

  recursive function meld_node(a, b) result(res)
    type(t_node), pointer, intent(in) :: a, b
    type(t_node), pointer :: res
    logical :: f
    if (.not.associated(a)) then
      res => b; return
    end if
    if (.not.associated(b)) then
      res => a; return
    end if
    if (less(a, b)) then
      res => a
      res%right => meld_node(res%right, b)
    else
      res => b
      res%right => meld_node(res%right, a)
    end if
    f = .not.associated(res%left)
    if (.not.f) f = res%left%rank < res%right%rank
    if (f) call swap(res%left, res%right)
    res%rank = merge(res%right%rank, 0, associated(res%right)) + 1
  end

  function poll(this) result(res)
    class(leftist_heap), intent(inout) :: this
    type(t_node), pointer :: res
    res => this%root
    this%root => meld_node(this%root%left, this%root%right)
    this%size = this%size - 1
  end

  subroutine offer(this, number, cost)
    class(leftist_heap), intent(inout) :: this
    integer, intent(in) :: number
    integer(8), intent(in) :: cost
    this%root => meld_node(this%root, newn0(number, cost))
    this%size = this%size + 1
  end

  function dijkstra(this, start) result(res)
    class(weighted_graph), intent(in) :: this
    integer, intent(in) :: start
    integer :: number, i
    integer(8) :: res(size(this%list)), cost
    type(t_node), pointer :: node
    type(leftist_heap) :: pq
    type(t_edge) :: e
    res = long_infty
    res(start) = 0
    call offer(pq, start, res(start))
    do while (pq%size > 0)
      node => poll(pq)
      number = node%number
      cost = node%cost
      deallocate(node)
      if (cost > res(number)) cycle
      do i = 1, size(this%list(number)%at)
        e = this%list(number)%at(i)
        if (res(e%to) > res(number) + e%weight) then
          res(e%to) = res(number) + e%weight
          call offer(pq, e%to, res(e%to))
        end if
      end do
    end do
  end

  integer function ford_fulkerson(this, start, goal) result(res)
    class(weighted_graph), intent(inout) :: this
    integer, intent(in) :: start, goal
    integer :: dflow
    logical :: used(size(this%list))
    res = 0
    do
      used = .false.
      dflow = ffdfs(this, used, start, goal, int_infty)
      if (dflow == 0) return
      res = res + dflow
    end do
  end

  recursive integer function ffdfs(this, used, u, goal, dflow) result(res)
    class(weighted_graph), intent(inout) :: this
    logical, intent(inout) :: used(:)
    integer, intent(in) :: u, goal
    integer, intent(in) :: dflow
    integer :: i, v, capacity, r
    if (u == goal) then
      res = dflow
      return
    end if
    used(u) = .true.
    do i = 1, size(this%list(u)%at)
      v = this%list(u)%at(i)%to
      capacity = this%list(u)%at(i)%capacity
      if (used(v) .or. capacity <= 0) cycle
      res = ffdfs(this, used, v, goal, min(dflow, capacity))
      r = this%list(u)%at(i)%reverse
      if (res > 0) then
        this%list(u)%at(i)%capacity = this%list(u)%at(i)%capacity - res
        this%list(v)%at(r)%capacity = this%list(v)%at(r)%capacity + res
        return
      end if
    end do
    res = 0
  end

  integer(8) function bellman_ford(n, m, a, b, weight, start, goal) result(res)
    integer, intent(in) :: n, m, a(:), b(:), start, goal
    integer(8), intent(in) :: weight(:)
    type(weighted_graph) :: gs, gg
    logical :: useds(n), usedg(n), changed
    integer :: i, u, v, cnt, x(m), y(m), step
    integer(8) :: w(m), cost(n)
    gs = newwg0(n, m, a, b, weight, undirected = .false.)
    gg = newwg0(n, m, b, a, weight, undirected = .false.)
    useds = .false.
    usedg = .false.
    call bfdfs(gs, useds, start)
    call bfdfs(gg, usedg, goal)
    cnt = 0
    do i = 1, m
      u = a(i)
      v = b(i)
      if (useds(u) .and. useds(v) .and. usedg(u) .and. usedg(v)) then
        cnt = cnt + 1
        x(cnt) = u
        y(cnt) = v
        w(cnt) = weight(i)
      end if
    end do
    cost = long_infty
    cost(start) = 0
    res = long_infty
    step = 0
    changed = .true.
    do while (changed)
      changed = .false.
      do i = 1, cnt
        if (cost(y(i)) > cost(x(i)) + w(i)) then
          cost(y(i)) = cost(x(i)) + w(i)
          changed = .true.
        end if
      end do
      step = step + 1
      if (step > n) then
        res = -long_infty; exit
      end if
    end do
    if (res == long_infty) res = cost(goal)
  end

  recursive subroutine bfdfs(this, used, u)
    type(weighted_graph), intent(in) :: this
    logical, intent(inout) :: used(:)
    integer, intent(in) :: u
    integer :: i, v
    used(u) = .true.
    do i = 1, size(this%list(u)%at)
      v = this%list(u)%at(i)%to
      if (used(v)) cycle
      call bfdfs(this, used, v)
    end do
  end

end module mod_weighted_graph
program coins_respawn
  use mod_weighted_graph
  implicit none
  integer :: n, m, a(5000) = 0, b(5000) = 0, i
  integer(8) :: p, c(5000) = 0, ans = 0
  read(*,*) n, m, p
  do i = 1, m
    read(*,*) a(i), b(i), c(i)
    c(i) = p - c(i)
  end do
  ans = bellman_ford(n, m, a, b, c, 1, n)
  write(*,'(i0)') merge(-1_8, merge(0_8, -ans, ans > 0), ans == -long_infty)
end program coins_respawn

Score : 500 points

Problem Statement

There is a directed graph with N vertices numbered 1 to N and M edges.
The i-th edge is directed from Vertex A_i to Vertex B_i, and there are C_i coins placed along that edge.
Additionally, there is a button on Vertex N.

We will play a game on this graph.
You start the game on Vertex 1 with zero coins, and head for Vertex N by traversing the edges while collecting coins.
It takes one minute to traverse an edge, and you can collect the coins placed along the edge each time you traverse it.
As usual in games, even if you traverse an edge once and collect the coins, the same number of coins will reappear next time you traverse that edge, which you can collect again.

When you reach Vertex N, you can end the game by pressing the button. (You can also choose to leave Vertex N without pressing the button and continue traveling.)
However, when you end the game, you will be asked to pay T \times P coins, where T is the number of minutes elapsed since the start of the game. If you have less than T \times P coins, you will have to pay all of your coins instead.

Your score will be the number of coins you have after this payment.
Determine if there exists a maximum value of the score that can be obtained. If the answer is yes, find that maximum value.

Constraints

2 \leq N \leq 2500

1 \leq M \leq 5000

1 \leq A_i, B_i \leq N

1 \leq C_i \leq 10^5

0 \leq P \leq 10^5

All values in input are integers.

Vertex N can be reached from Vertex 1.

Input

Input is given from Standard Input in the following format:

N M P
A_1 B_1 C_1
:
A_M B_M C_M

Output

If there exists a maximum value of the score that can be obtained, print that maximum value; otherwise, print -1.

Sample Input 1

3 3 10
1 2 20
2 3 30
1 3 45

Sample Output 1

35

There are two ways to travel from Vertex 1 to Vertex 3:

Vertex 1 \rightarrow 2 \rightarrow 3: You collect 20 + 30 = 50 coins on the way. After two minutes from the start of the game, you press the button, pay 2 \times 10 = 20 coins, and you have 50 - 20 = 30 coins left.

Vertex 1 \rightarrow 2: You collect 45 coins on the way. After one minute from the start of the game, you press the button, pay 1 \times 10 = 10 coins, and you have 45 - 10 = 35 coins left.

Thus, the maximum score that can be obtained is 35.

Sample Input 2

2 2 10
1 2 100
2 2 100

Sample Output 2

-1

The edge extending from Vertex 1 takes you to Vertex 2. If you then traverse the edge extending from Vertex 2 to itself t times and press the button, your score will be 90 + 90t. Thus, you can infinitely increase your score, which means there is no maximum value of the score that can be obtained.

Sample Input 3

4 5 10
1 2 1
1 4 1
3 4 1
2 2 100
3 3 100

Sample Output 3

0

There is no way to travel from Vertex 1 to Vertex 4 other than traversing the edge leading from Vertex 1 to Vertex 4 directly. You will pick up one coin along this edge, but after being asked to paying 10 coins, your score will be 0.

Note that you can collect an infinite number of coins if you traverse the edge leading from Vertex 1 to Vertex 2, but this is pointless since you can no longer reach Vertex 4 and end the game.


module mod_graph
  implicit none
  integer(8), parameter :: inf = 1000000000000000000_8

  type item
    integer :: id = 0
    integer(8) :: wt = 0_8
  end type

  type priorityqueue
    integer :: num = 0
    type(item), pointer :: heap(:) => null()
  contains
    !final :: finalizei
  end type

  type edge
    integer :: fm = 0, to = 0, cp = 0, rv = 0
    integer(8) :: wt = 0_8
  end type

  type arraylist
    integer :: num = 0
    type(edge), pointer :: arr(:) => null()
  contains
    procedure :: add => adde
    procedure :: bellman_ford => bellman_ford
    !final :: finalizee
  end type

  type graph
    type(arraylist), pointer :: egs(:) => null()
    logical, pointer :: used(:) => null()
  contains
    procedure :: add => add
    procedure :: dijkstra => dijkstra
    procedure :: ford_fulkerson => ford_fulkerson
    !final :: finalize
  end type

  interface graph
    module procedure :: newg
  end interface graph

contains

  function newi(id,wt) result(ret)
    integer, intent(in) :: id
    integer(8), intent(in) :: wt
    type(item) :: ret
    ret%id = id
    ret%wt = wt
  end

  subroutine swapi(a,b)
    type(item), intent(inout) :: a, b
    type(item) :: c
    c = a
    a = b
    b = c
  end

  subroutine appendi(a)
    type(item), pointer, intent(inout) :: a(:)
    integer :: n
    type(item), allocatable :: tmp(:)
    n = size(a)
    allocate(tmp(n))
    tmp = a
    deallocate(a)
    allocate(a(2*n))
    a(1:n) = tmp
    deallocate(tmp)
  end

  function lessi(a,b) result(ret)
    type(item), intent(in) :: a, b
    logical :: ret
    ret = a%wt < b%wt
  end

  subroutine finalizei(pq)
    type(priorityqueue), intent(inout) :: pq
    if (associated(pq%heap)) deallocate(pq%heap)
    pq%num = 0
  end

  subroutine offer(pq,it)
    type(priorityqueue), intent(inout) :: pq
    type(item), intent(in) :: it
    integer :: i, j
    if (.not.associated(pq%heap)) allocate(pq%heap(1))
    if (pq%num == size(pq%heap)) call appendi(pq%heap)
    pq%num = pq%num+1
    pq%heap(pq%num) = it
    i = pq%num
    do while (i > 1)
      j = i/2
      if (lessi(pq%heap(i),pq%heap(j))) call swapi(pq%heap(i),pq%heap(j))
      i = j
    end do
  end

  function poll(pq) result(ret)
    type(priorityqueue), intent(inout) :: pq
    type(item) :: ret
    integer :: n, i, j
    n = pq%num
    ret = pq%heap(1)
    pq%heap(1) = pq%heap(n)
    pq%num = pq%num-1
    i = 1
    do while (2*i < n)
      j = 2*i
      if (j+1 < n .and. lessi(pq%heap(j+1),pq%heap(j))) j = j+1
      if (lessi(pq%heap(j),pq%heap(i))) call swapi(pq%heap(j),pq%heap(i))
      i = j
    end do
  end

  function newe(to,wt,fm,cp,rv) result(ret)
    integer, intent(in) :: to
    integer(8), intent(in) :: wt
    integer, intent(in), optional :: fm, cp, rv
    type(edge) :: ret
    if (present(fm)) ret%fm = fm
    ret%to = to
    ret%wt = wt
    if (present(cp) .and. present(rv)) then
      ret%cp = cp
      ret%rv = rv
    end if
  end

  subroutine appende(a)
    type(edge), pointer, intent(inout) :: a(:)
    integer :: n
    type(edge), allocatable :: tmp(:)
    n = size(a)
    allocate(tmp(n))
    tmp = a
    deallocate(a)
    allocate(a(2*n))
    a(1:n) = tmp
    deallocate(tmp)
  end

  function lesse(a,b) result(ret)
    type(edge), intent(in) :: a, b
    logical :: ret
    ret = a%wt < b%wt
  end

  subroutine finalizee(list)
    type(arraylist), intent(inout) :: list
    if (associated(list%arr)) deallocate(list%arr)
    list%num = 0
  end

  subroutine adde(list,e)
    class(arraylist), intent(inout) :: list
    type(edge), intent(in) :: e
    if (.not.associated(list%arr)) allocate(list%arr(1))
    if (list%num == size(list%arr)) call appende(list%arr)
    list%num = list%num+1
    list%arr(list%num) = e
  end

  function newg(n) result(ret)
    integer, intent(in) :: n
    type(graph) :: ret
    allocate(ret%egs(n))
  end

  subroutine add(g,fm,to,wt,cp)
    class(graph), intent(inout) :: g
    integer, intent(in) :: fm, to
    integer, intent(in), optional :: cp
    integer(8) :: wt
    if (present(cp)) then
      call adde(g%egs(fm),newe(to,wt,cp=cp,rv=g%egs(to)%num))
      call adde(g%egs(to),newe(fm,wt,cp=0,rv=g%egs(fm)%num-1))
    else
      call adde(g%egs(fm),newe(to,wt))
    end if
  end

  subroutine finalize(g)
    type(graph), intent(inout) :: g
    integer :: i
    if (associated(g%used)) deallocate(g%used)
    if (.not.associated(g%egs)) return
    do i = 1, size(g%egs)
      call finalizee(g%egs(i))
    end do
    deallocate(g%egs)
  end

  function dijkstra(g,s) result(ret)
    class(graph), intent(in) :: g
    integer, intent(in) :: s
    integer(8) :: ret(size(g%egs))
    type(priorityqueue) :: pq
    type(item) :: it
    type(edge) :: e
    integer :: i
    ret = inf
    ret(s) = 0_8
    call offer(pq,newi(s,ret(s)))
    do while (pq%num > 0)
      it = poll(pq)
      if (it%wt > ret(it%id)) cycle
      do i = 1, g%egs(it%id)%num
        e = g%egs(it%id)%arr(i)
        if (ret(e%to) > ret(it%id)+e%wt) then
          ret(e%to) = ret(it%id)+e%wt
          call offer(pq,newi(e%to,ret(e%to)))
        end if
      end do
    end do
    call finalizei(pq)
  end

  recursive subroutine dfs_bf(g,u)
    type(graph), intent(inout) :: g
    integer, intent(in) :: u
    integer :: i, v
    g%used(u) = .true.
    do i = 1, g%egs(u)%num
      v = g%egs(u)%arr(i)%to
      if (g%used(v)) cycle
      call dfs_bf(g,v)
    end do
  end

  function bellman_ford(es,s,t) result(ret)
    class(arraylist), intent(in) :: es
    integer, intent(in) :: s, t
    integer(8) :: ret
    integer :: n, i, fm, to, step
    integer(8) :: wt
    logical :: updated
    integer(8), allocatable :: tmp(:)
    type(graph) :: gs, gt
    type(arraylist) :: reach
    n = 0
    do i = 1, es%num
      n = max(n,es%arr(i)%fm,es%arr(i)%to)
    end do
    gs = newg(n)
    gt = newg(n)
    allocate(gs%used(n),gt%used(n))
    gs%used = .false.
    gt%used = .false.
    do i = 1, es%num
      fm = es%arr(i)%fm
      to = es%arr(i)%to
      wt = es%arr(i)%wt
      call add(gs,fm,to,wt)
      call add(gt,to,fm,wt)
    end do
    call dfs_bf(gs,s)
    call dfs_bf(gt,t)
    n = 0
    do i = 1, es%num
      fm = es%arr(i)%fm
      to = es%arr(i)%to
      if (gs%used(fm) .and. gt%used(fm) .and. gs%used(to) .and. gt%used(to)) then
        call adde(reach,es%arr(i))
        n = max(n,fm,to)
      end if
    end do
    call finalize(gs)
    call finalize(gt)
    allocate(tmp(n))
    tmp = inf
    tmp(s) = 0_8
    step = 0
    updated = .true.
    ret = inf
    do while (updated)
      updated = .false.
      do i = 1, reach%num
        fm = reach%arr(i)%fm
        to = reach%arr(i)%to
        wt = reach%arr(i)%wt
        if (tmp(to) > tmp(fm)+wt) then
          tmp(to) = tmp(fm)+wt
          updated = .true.
        end if
      end do
      step = step+1
      if (step > n) then
        ret = -inf
        exit
      end if
    end do
    if (ret == inf) ret = tmp(t)
    deallocate(tmp)
    call finalizee(reach)
  end

  recursive function dfs_ff(g,u,t,f) result(ret)
    class(graph), intent(inout) :: g
    integer, intent(in) :: u, t, f
    integer :: ret
    integer :: i, v, cp, rv
    ret = f
    if (u == t) return
    g%used(u) = .true.
    do i = 1, g%egs(u)%num
      v = g%egs(u)%arr(i)%to
      cp = g%egs(u)%arr(i)%cp
      if (g%used(v) .or. cp <= 0) cycle
      ret = dfs_ff(g,v,t,min(f,cp))
      rv = g%egs(u)%arr(i)%rv
      if (ret > 0) then
        g%egs(u)%arr(i)%cp = g%egs(u)%arr(i)%cp-ret
        g%egs(v)%arr(rv)%cp = g%egs(v)%arr(rv)%cp+ret
        return
      end if
    end do
    ret = 0
  end

  function ford_fulkerson(g,s,t) result(ret)
    class(graph), intent(inout) :: g
    integer, intent(in) :: s, t
    integer :: ret
    integer :: f
    ret = 0
    if (associated(g%used) .and. size(g%used) /= size(g%egs)) deallocate(g%used)
    if (.not.associated(g%used)) allocate(g%used(size(g%egs)))
    do
      g%used = .false.
      f = dfs_ff(g,s,t,1000000000)
      if (f == 0) then
        deallocate(g%used)
        return
      end if
      ret = ret+f
    end do
  end

end module mod_graph

program coins_respawn
  use mod_graph
  implicit none
  integer :: n, m, p, a, b, c, i
  type(arraylist) :: es
  integer(8) :: ans = 0_8
  read(*,*) n, m, p
  do i = 1, m
    read(*,*) a, b, c
    call es%add(newe(b,int(p-c,8),a))
  end do
  ans = es%bellman_ford(1,n)
  call finalizee(es)
  if (ans == -inf) then
    ans = -1_8
  else if (ans > 0_8) then
    ans = 0_8
  else
    ans = -ans
  end if
  write(*,'(i0)') ans
end program coins_respawn


module mod_graph
  implicit none
  integer(8), parameter :: inf = 1000000000000000000_8

  type item
    integer :: id = 0
    integer(8) :: wt = 0_8
  end type

  type priorityqueue
    integer :: num = 0
    type(item), pointer :: heap(:) => null()
  contains
    !final :: finalizei
  end type

  type edge
    integer :: fm = 0, to = 0, cp = 0, rv = 0
    integer(8) :: wt = 0_8
  end type

  type arraylist
    integer :: num = 0
    type(edge), pointer :: arr(:) => null()
  contains
    procedure :: add => adde
    procedure :: bellman_ford => bellman_ford
    !final :: finalizee
  end type

  type graph
    type(arraylist), pointer :: egs(:) => null()
    logical, pointer :: used(:) => null()
  contains
    procedure :: add => add
    procedure :: dijkstra => dijkstra
    procedure :: ford_fulkerson => ford_fulkerson
    !final :: finalize
  end type

  interface graph
    module procedure :: newg
  end interface graph

contains

  function newi(id,wt) result(ret)
    integer, intent(in) :: id
    integer(8), intent(in) :: wt
    type(item) :: ret
    ret%id = id
    ret%wt = wt
  end

  subroutine swapi(a,b)
    type(item), intent(inout) :: a, b
    type(item) :: c
    c = a
    a = b
    b = c
  end

  subroutine appendi(a)
    type(item), pointer, intent(inout) :: a(:)
    integer :: n
    type(item), allocatable :: tmp(:)
    n = size(a)
    allocate(tmp(n))
    tmp = a
    deallocate(a)
    allocate(a(2*n))
    a(1:n) = tmp
    deallocate(tmp)
  end

  function lessi(a,b) result(ret)
    type(item), intent(in) :: a, b
    logical :: ret
    ret = a%wt < b%wt
  end

  subroutine finalizei(pq)
    type(priorityqueue), intent(inout) :: pq
    if (associated(pq%heap)) deallocate(pq%heap)
    pq%num = 0
  end

  subroutine offer(pq,it)
    type(priorityqueue), intent(inout) :: pq
    type(item), intent(in) :: it
    integer :: i, j
    if (.not.associated(pq%heap)) allocate(pq%heap(1))
    if (pq%num == size(pq%heap)) call appendi(pq%heap)
    pq%num = pq%num+1
    pq%heap(pq%num) = it
    i = pq%num
    do while (i > 1)
      j = i/2
      if (lessi(pq%heap(i),pq%heap(j))) call swapi(pq%heap(i),pq%heap(j))
      i = j
    end do
  end

  function poll(pq) result(ret)
    type(priorityqueue), intent(inout) :: pq
    type(item) :: ret
    integer :: n, i, j
    n = pq%num
    ret = pq%heap(1)
    pq%heap(1) = pq%heap(n)
    pq%num = pq%num-1
    i = 1
    do while (2*i < n)
      j = 2*i
      if (j+1 < n .and. lessi(pq%heap(j+1),pq%heap(j))) j = j+1
      if (lessi(pq%heap(j),pq%heap(i))) call swapi(pq%heap(j),pq%heap(i))
      i = j
    end do
  end

  function newe(to,wt,fm,cp,rv) result(ret)
    integer, intent(in) :: to
    integer(8), intent(in) :: wt
    integer, intent(in), optional :: fm, cp, rv
    type(edge) :: ret
    if (present(fm)) ret%fm = fm
    ret%to = to
    ret%wt = wt
    if (present(cp) .and. present(rv)) then
      ret%cp = cp
      ret%rv = rv
    end if
  end

  subroutine appende(a)
    type(edge), pointer, intent(inout) :: a(:)
    integer :: n
    type(edge), allocatable :: tmp(:)
    n = size(a)
    allocate(tmp(n))
    tmp = a
    deallocate(a)
    allocate(a(2*n))
    a(1:n) = tmp
    deallocate(tmp)
  end

  function lesse(a,b) result(ret)
    type(edge), intent(in) :: a, b
    logical :: ret
    ret = a%wt < b%wt
  end

  subroutine finalizee(list)
    type(arraylist), intent(inout) :: list
    if (associated(list%arr)) deallocate(list%arr)
    list%num = 0
  end

  subroutine adde(list,e)
    class(arraylist), intent(inout) :: list
    type(edge), intent(in) :: e
    if (.not.associated(list%arr)) allocate(list%arr(1))
    if (list%num == size(list%arr)) call appende(list%arr)
    list%num = list%num+1
    list%arr(list%num) = e
  end

  function newg(n) result(ret)
    integer, intent(in) :: n
    type(graph) :: ret
    allocate(ret%egs(n))
  end

  subroutine add(g,fm,to,wt,cp)
    class(graph), intent(inout) :: g
    integer, intent(in) :: fm, to
    integer, intent(in), optional :: cp
    integer(8) :: wt
    if (present(cp)) then
      call adde(g%egs(fm),newe(to,wt,cp=cp,rv=g%egs(to)%num))
      call adde(g%egs(to),newe(fm,wt,cp=0,rv=g%egs(fm)%num-1))
    else
      call adde(g%egs(fm),newe(to,wt))
    end if
  end

  subroutine finalize(g)
    type(graph), intent(inout) :: g
    integer :: i
    if (associated(g%used)) deallocate(g%used)
    if (.not.associated(g%egs)) return
    do i = 1, size(g%egs)
      call finalizee(g%egs(i))
    end do
    deallocate(g%egs)
  end

  function dijkstra(g,s) result(ret)
    class(graph), intent(in) :: g
    integer, intent(in) :: s
    integer(8) :: ret(size(g%egs))
    type(priorityqueue) :: pq
    type(item) :: it
    type(edge) :: e
    integer :: i
    ret = inf
    ret(s) = 0_8
    call offer(pq,newi(s,ret(s)))
    do while (pq%num > 0)
      it = poll(pq)
      if (it%wt > ret(it%id)) cycle
      do i = 1, g%egs(it%id)%num
        e = g%egs(it%id)%arr(i)
        if (ret(e%to) > ret(it%id)+e%wt) then
          ret(e%to) = ret(it%id)+e%wt
          call offer(pq,newi(e%to,ret(e%to)))
        end if
      end do
    end do
    call finalizei(pq)
  end

  recursive subroutine dfs_bf(g,u)
    type(graph), intent(inout) :: g
    integer, intent(in) :: u
    integer :: i, v
    g%used(u) = .true.
    do i = 1, g%egs(u)%num
      v = g%egs(u)%arr(i)%to
      if (g%used(v)) cycle
      call dfs_bf(g,v)
    end do
  end

  function bellman_ford(es,s,t) result(ret)
    class(arraylist), intent(in) :: es
    integer, intent(in) :: s, t
    integer(8) :: ret
    integer :: n, i, fm, to, step
    integer(8) :: wt
    logical :: updated
    integer(8), allocatable :: tmp(:)
    type(graph) :: gs, gt
    type(arraylist) :: reach
    n = 0
    do i = 1, es%num
      n = max(n,es%arr(i)%fm,es%arr(i)%to)
    end do
    gs = newg(n)
    gt = newg(n)
    allocate(gs%used(n),gt%used(n))
    gs%used = .false.
    gt%used = .false.
    do i = 1, es%num
      fm = es%arr(i)%fm
      to = es%arr(i)%to
      wt = es%arr(i)%wt
      call add(gs,fm,to,wt)
      call add(gt,to,fm,wt)
    end do
    call dfs_bf(gs,s)
    call dfs_bf(gt,t)
    n = 0
    do i = 1, es%num
      fm = es%arr(i)%fm
      to = es%arr(i)%to
      if (gs%used(fm) .and. gt%used(fm) .and. gs%used(to) .and. gt%used(to)) then
        call adde(reach,es%arr(i))
        n = max(n,fm,to)
      end if
    end do
    call finalize(gs)
    call finalize(gt)
    allocate(tmp(n))
    tmp = inf
    tmp(s) = 0_8
    step = 0
    updated = .true.
    ret = inf
    do while (updated)
      updated = .false.
      do i = 1, reach%num
        fm = reach%arr(i)%fm
        to = reach%arr(i)%to
        wt = reach%arr(i)%wt
        if (tmp(to) > tmp(fm)+wt) then
          tmp(to) = tmp(fm)+wt
          updated = .true.
        end if
      end do
      step = step+1
      if (step > n) then
        ret = -inf
        exit
      end if
    end do
    if (ret == inf) ret = tmp(t)
    deallocate(tmp)
    call finalizee(reach)
  end

  recursive function dfs_ff(g,u,t,f) result(ret)
    class(graph), intent(inout) :: g
    integer, intent(in) :: u, t, f
    integer :: ret
    integer :: i, v, cp, rv
    ret = f
    if (u == t) return
    g%used(u) = .true.
    do i = 1, g%egs(u)%num
      v = g%egs(u)%arr(i)%to
      cp = g%egs(u)%arr(i)%cp
      if (g%used(v) .or. cp <= 0) cycle
      ret = dfs_ff(g,v,t,min(f,cp))
      rv = g%egs(u)%arr(i)%rv
      if (ret > 0) then
        g%egs(u)%arr(i)%cp = g%egs(u)%arr(i)%cp-ret
        g%egs(v)%arr(rv)%cp = g%egs(v)%arr(rv)%cp+ret
        return
      end if
    end do
    ret = 0
  end

  function ford_fulkerson(g,s,t) result(ret)
    class(graph), intent(inout) :: g
    integer, intent(in) :: s, t
    integer :: ret
    integer :: f
    ret = 0
    if (associated(g%used) .and. size(g%used) /= size(g%egs)) deallocate(g%used)
    if (.not.associated(g%used)) allocate(g%used(size(g%egs)))
    do
      g%used = .false.
      f = dfs_ff(g,s,t,1000000000)
      if (f == 0) then
        deallocate(g%used)
        return
      end if
      ret = ret+f
    end do
  end

end module mod_graph

program coins_respawn
  use mod_graph
  implicit none
  integer :: n, m, p, a, b, c, i
  type(arraylist) :: es
  integer(8) :: ans = 0_8
  read(*,*) n, m, p
  do i = 1, m
    read(*,*) a, b, c
    call es%add(newe(b,int(p-c,8),a))
  end do
  ans = es%bellman_ford(1,n)
  if (ans == -inf) then
    ans = -1_8
  else if (ans > 0_8) then
    ans = 0_8
  else
    ans = -ans
  end if
  write(*,'(i0)') ans
end program coins_respawn


      PROGRAM buildStairs
      IMPLICIT NONE
      integer(16) :: n
      integer(16),allocatable :: h(:)
      integer(16) :: i

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

      do i = n,2,-1
        if( h(i-1)-1>h(i) )then
          print*,'No';stop
        else if( h(i-1)-1==h(i) )then
          h(i-1) = h(i-1) - 1
        end if
      end do

      print*,'Yes'

      END PROGRAM

Score : 300 points

Problem Statement

There are N squares arranged in a row from left to right. The height of the i-th square from the left is H_i.

For each square, you will perform either of the following operations once:

Decrease the height of the square by 1.

Do nothing.

Determine if it is possible to perform the operations so that the heights of the squares are non-decreasing from left to right.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq H_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
H_1 H_2 ... H_N

Output

If it is possible to perform the operations so that the heights of the squares are non-decreasing from left to right, print Yes; otherwise, print No.

Sample Input 1

5
1 2 1 1 3

Sample Output 1

Yes

You can achieve the objective by decreasing the height of only the second square from the left by 1.

Sample Input 2

4
1 3 2 1

Sample Output 2

No

Sample Input 3

5
1 2 3 4 5

Sample Output 3

Yes

Sample Input 4

1
1000000000

Sample Output 4

Yes


program Build_Stairs
    implicit none
    integer(4):: n,i,nmax
    integer(4),allocatable:: h(:)

    read*, n
    allocate(h(n))
    read*, h(:)
    nmax=h(1)
    do i=2,n
        if (h(i) > nmax+1) then
            print*, 'No'
            stop
        end if
        nmax = h(i)
    end do
    print*, 'Yes'

end program Build_Stairs


program main
  implicit none
  integer :: n, i, x
  integer, allocatable :: h(:)
  read (*, *) n
  allocate (h(n))
  read (*, *) h(:)
  x = h(n)
  do i = n - 1, 1, -1
    if (h(i) <= x) then
      continue
    else if (h(i) == x + 1) then
      h(i) = x
    else
      write (*, "(a)") "No"
      stop
    end if
    x = h(i)
  end do
  write (*, "(a)") "Yes"
end program main


program ccc

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

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

h(1) = h(1) - 1
if(h(n)>h(n-1)) then
h(n)=h(n)-1
end if

do i=n-1,2,-1
if(h(i)>h(i-1)) then
h(i) = h(i) -1
end if
end do
do i=1,n-1
if((h(i)>h(i+1))) then
write(*,'(a2)') 'No'
return
end if
end do

write(*,'(a3)') 'Yes'
end program


program ccc

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

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

h(1) = h(1) - 1

if(h(2)<h(1)) then
h(1) = h(1) -1
end if
if((h(2)<h(1))) then
write(*,'(a2)') 'No'
return
end if

do i=2,n-1
if(h(i+1)<h(i)) then
h(i) = h(i) -1
end if
if((h(i+1)<h(i)).or.(h(i)<h(i-1))) then
write(*,'(a2)') 'No'
return
end if
end do

write(*,'(a3)') 'Yes'
end program


program ccc

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

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

if(h(2)<h(1)) then
h(1) = h(1) -1
end if
if((h(2)<h(1))) then
write(*,'(a2)') 'No'
return
end if

do i=2,n-1
if(h(i+1)<h(i)) then
h(i) = h(i) -1
end if
if((h(i+1)<h(i)).or.(h(i)<h(i-1))) then
write(*,'(a2)') 'No'
return
end if
end do

write(*,'(a3)') 'Yes'
end program


program ccc

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

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

if(h(2)<h(1)) then
h(1) = h(1) -1
end if
if((h(2)<h(1))) then
write(*,'(a2)') 'No'
return
end if

do i=2,n-1
if(h(i+1)<h(i)) then
h(i) = h(i) -1
end if
if((h(i+1)<h(i)).or.(h(i)<h(i-1))) then
write(*,'(a2)') 'No'
return
end if
end do

write(*,'(a3)') 'Yes'
write(*,*) h
end program


program c

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

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

h(1) = h(1) - 1

do i=2,n-1
if(h(i+1)<h(i)) then
h(i) = h(i) -1
end if
if((h(i+1)<h(i)).or.(h(i)<h(i-1))) then
write(*,'(a2)') 'No'
return
end if
end do

write(*,'(a3)') 'Yes'
end program


program c

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

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

do i=2,n-1
if(h(i+1)<h(i)) then
h(i) = h(i) -1
end if
if((h(i+1)<h(i)).or.(h(i)<h(i-1))) then
write(*,'(a2)') 'No'
return
end if
end do

write(*,'(a3)') 'Yes'
end program


program c

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

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

do i=2,n-1
if(h(i+1)<h(i)) then
h(i) = h(i) -1
end if
if((h(i+1)<h(i)).or.(h(i)<h(i-1))) then
write(*,'(a2)') 'No'
return
end if
end do

write(*,'(a3)') 'Yes'
end program


program main
  implicit none

  integer :: n, i, no, two
  integer, allocatable, dimension ( : ) :: H
  no  = 0
  two = 0
  read *, n

  allocate ( H ( n ) )

!  do i = 1, n
     read *, H ( : )
!     print *, H ( i )
!  end do

  do i = 1, n - 1
     if ( H ( i ) > H ( i + 1 ) ) then
        H ( i ) = H ( i ) - 1
        if ( H ( i ) /= H ( i + 1 ) ) then
           no = 1
           exit
        else if ( i - two == 1 ) then
           no = 1
           exit
        end if
        two = i
     end if
  end do

  if ( no == 0 ) then
     print *, "yes"
  else
     print *, "no"
  end if

end program main


program main
  implicit none

  integer :: n, a(100000), i,j

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

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

end program main


module ABC136

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

  ! require all variables to be explicitly declared
  implicit none

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

  contains

  subroutine task_C

    ! variables for this <subroutine>
    integer(INT32) :: num_cell
    logical        :: isPossible

    ! arrays for this <subroutine>
    integer(INT32), dimension(:), allocatable :: val_height

    ! STEP.01
    ! read out the number of cells
    read(unit=INPUT_UNIT, fmt=*) num_cell

    ! STEP.02
    ! allocate the array to store the height of each cell
    allocate( val_height(1:num_cell) )

    ! STEP.03
    ! read out the height of each cell
    read(unit=INPUT_UNIT, fmt=*) val_height(:)

    ! STEP.04
    ! calculate the answer of this task
    call judge_isPossible(num_cell, val_height(:), isPossible)

    ! STEP.04
    ! output the answer of this task
    if ( isPossible ) then
      write(unit=OUTPUT_UNIT, fmt='(A)', advance='yes') 'Yes'
    else
      write(unit=OUTPUT_UNIT, fmt='(A)', advance='yes') 'No'
    end if

    ! STEP.END
    return

  end subroutine task_C

  subroutine judge_isPossible (num_cell, val_height, isPossible)

    ! arguments for this <subroutine>
    integer(INT32), intent(in)    :: num_cell
    integer(INT32), intent(inout) :: val_height(:)
    logical,        intent(inout) :: isPossible

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

    ! STEP.01
    ! initialize the return value of this subroutine
    isPossible = .true.

    ! STEP.02
    ! judge the possibility using the number of cells
    if (num_cell .eq. 1_INT32) then
      return
    end if

    ! STEP.03
    ! judge the possibility using the value of height
    do itr = num_cell - 1_INT32, 1_INT32, -1_INT32

      if ( val_height(itr) .le. val_height(itr + 1_INT32) ) then
        continue
      else if ( val_height(itr) - val_height(itr + 1_INT32) .eq. 1_INT32 ) then
        val_height(itr) = val_height(itr) - 1_INT32
      else
        isPossible = .false.
        exit
      end if

    end do

    ! STEP.END
    return

  end subroutine judge_isPossible

end module ABC136

program main

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

  call task_C

end program main


program abc136c
   implicit none
   logical :: flag=.true.
   integer(8) :: n, num_negative, i, j
   integer(16),allocatable :: h(:), diff(:)

   read(*,*) n

   allocate(h(n), diff(n-1))
   read(*,*) h(:)

   if (n == 1) then
      write(*,*) "Yes"
      stop
   end if

   diff = 0
   do i = 1, n-1
      diff(i) = h(i+1) - h(i)
   enddo

   do i = 1, n-1
      if ( diff(i) == -1) then
         do j = i-1, 1, -1
            if (diff(j) == 0) then
               cycle
            else if (diff(j) >= 1) then
               flag = .true.
               exit
            else if (diff(j) <= -1) then
               flag = .false.
               exit
            end if
         end do

      endif
   enddo

   if (minVal(diff) <= -2) flag = .false.

   ! ans
   if (flag) then
      write(*,*) "Yes"
   else
      write(*,*) "No"
   endif

end program abc136c


program abc136c
   implicit none
   logical :: flag=.true.
   integer(8) :: n, num_negative, i, j
   integer(8),allocatable :: h(:), diff(:)

   read(*,*) n

   allocate(h(n), diff(n-1))
   read(*,*) h(:)

   if (n == 1) then
      write(*,*) "Yes"
      stop
   end if

   diff = 0
   do i = 1, n-1
      diff(i) = h(i+1) - h(i)
   enddo

   do i = 1, n-1
      if ( diff(i) == -1) then
         do j = i-1, 1, -1
            if (diff(j) == 0) then
               cycle
            else if (diff(j) >= 1) then
               flag = .true.
               exit
            else if (diff(j) <= -1) then
               flag = .false.
               exit
            end if
         end do

      endif
   enddo

   if (minVal(diff) <= -2) flag = .false.

   ! ans
   if (flag) then
      write(*,*) "Yes"
   else
      write(*,*) "No"
   endif

end program abc136c


program abc136c
   implicit none
   logical :: flag=.true.
   integer(8) :: n, num_negative, i, j
   integer(8),allocatable :: h(:), diff(:)

   read(*,*) n

   allocate(h(n), diff(n-1))
   read(*,*) h(:)

   if (n == 1) then
      write(*,*) "Yes"
      stop
   end if

   diff = 0
   do i = 1, n-1
      diff(i) = h(i+1) - h(i)
   enddo

   do i = 1, n-1
      if ( diff(i) == -1) then
         do j = i-1, 1, -1
            if (diff(j) == 0) then
               cycle
            else if (diff(j) >= 1) then
               flag = .true.
               exit
            else if (diff(j) <= -1) then
               flag = .false.
            end if
         end do

      endif
   enddo

   if (minVal(diff) <= -2) flag = .false.

   ! ans
   if (flag) then
      write(*,*) "Yes"
   else
      write(*,*) "No"
   endif

end program abc136c


program abc136c
   implicit none
   logical :: flag=.true.
   integer(8) :: n, num_negative, i
   integer(8),allocatable :: h(:), diff(:)

   read(*,*) n

   allocate(h(n), diff(n-1))
   read(*,*) h(:)

   if (n == 1) then
      write(*,*) "Yes"
      stop
   end if

   diff = 0
   do i = 1, n-1
      diff(i) = h(i+1) - h(i)
   enddo

   do i = 1, n-1
      if ( diff(i) == -1) then
         if(diff(i-1) == 1 .or. diff(i-1)==0) then
            flag = .true.
         else
            flag = .false.
         endif
      endif
   enddo

   if (minVal(diff) <= -2) flag = .false.

   ! ans
   if (flag) then
      write(*,*) "Yes"
   else
      write(*,*) "No"
   endif

end program abc136c


program abc136c
   implicit none
   logical :: flag=.true.
   integer(8) :: n, num_negative, i
   integer(8),allocatable :: h(:), diff(:)

   read(*,*) n

   allocate(h(n), diff(n-1))
   read(*,*) h(:)

   if (n == 1) then
      write(*,*) "Yes"
      stop
   end if

   diff = 0
   do i = 1, n-1
      diff(i) = h(i+1) - h(i)
   enddo
   if (minVal(diff) <= -2) flag = .false.

   do i = 1, n-1
      if ( diff(i) == -1) then
         if(diff(i-1) == 1) then
            flag = .true.
         else
            flag = .false.
         endif
      endif
   enddo

   ! ans
   if (flag) then
      write(*,*) "Yes"
   else
      write(*,*) "No"
   endif

end program abc136c


program kadai
implicit none
integer ::  n,i
integer,allocatable :: h(:)

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

do i=1,n-1
    if (h(i+1)-h(i)<-1) then
        write(*,*) "No"
        stop
    end if
    if (h(i+1)-h(i)==-1) then
        h(i+1)=h(i+1)+1
    end if
end do

write(*,*) "Yes"

stop

end program kadai


program abc136c
   implicit none
   integer(8) :: n, num_negative, i
   integer(8),allocatable :: h(:), diff(:)

   read(*,*) n

   allocate(h(n), diff(n-1))
   read(*,*) h(:)

   if (n == 1) then
      write(*,*) "Yes"
      stop
   end if

   do i = 1, n-1
      diff(i) = h(i+1) - h(i)
   enddo

   num_negative =  sum(diff(:), mask=(diff<0))

   if (num_negative >= -1) then
      write(*,*) "Yes"
   else
      write(*,*) "No"
   end if

end program abc136c


program abc136c
   implicit none
   integer(8) :: n, num_negative, i
   integer(8),allocatable :: h(:), diff(:)

   read(*,*) n
   allocate(h(n), diff(n-1))
   read(*,*) h(:)

   do i = 1, n-1
      diff(i) = h(i+1) - h(i)
   enddo

   num_negative =  sum(diff(:), mask=(diff<0))

   if (num_negative >= -1) then
      write(*,*) "YES"
   else
      write(*,*) "NO"
   end if

end program abc136c


program ms
  integer :: N
  integer,allocatable :: H(:)

  integer :: i,zti
  logical :: ok

  read(*,*) N
  allocate(H(N))
  read(*,*) H
  ok=.true.
  if(N.gt.1) then
     zti=H(1)
     do i=2,N
        if(H(i).gt.(zti+1)) then
           zti=H(i)-1
        elseif(H(i).lt.zti) then
           ok=.false.
        else
        endif
     enddo
  endif

  if(ok) then
     write(*,'(a)') 'Yes'
  else
     write(*,'(a)') 'No'
  endif

end program ms


program main
  implicit none
  integer :: i, n
  integer,allocatable :: h(:)
  logical :: out = .true., prev = .false.

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

  do i = 2, n
    if (h(n-1) - h(n) >= 2) then
      out = .false.
      exit
    else if (h(n-1) - h(n) == 1) then
      if (prev) then
        out = .false.
        exit
      else
        prev = .true.
      end if
    else if (h(n-1) < h(n)) then
      prev = .false.
    end if
  end do

  if (out) then
    print '(a)', 'Yes'
  else
    print '(a)', 'No'
  end if
end program


program main
  implicit none
  integer :: i, n
  integer,allocatable :: h(:)
  logical :: out = .true., prev = .false.

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

  do i = 2, n
    if (h(n-1) - h(n) >= 2) then
      out = .false.
      exit
    else if (h(n-1) - h(n) == 1) then
      if (prev) then
        out = .false.
        exit
      else
        prev = .true.
      end if
    else if (h(n-1) < h(n)) then
      prev = .false.
    end if
  end do

  if (out) then
    print '(a)', 'YES'
  else
    print '(a)', 'NO'
  end if
end program


program main
 implicit none
 integer*8 N,i,flag
 integer*8, allocatable::H(:)
 read(*,*) N
 allocate(H(1:N))
 read(*,*) H
 flag = 0
 do i=1,N
  if(i>1)then
   if(H(i-1) - H(i) > 1) then
    flag = -1
    exit
   else if(H(i-1) - H(i) == 1) then
    H(i-1) = H(i-1) - 1
    if(i>2) then
     if(H(i-2) > H(i-1))then
      flag = -1
      exit
     end if
    end if
   end if
  end if
 end do

 if(flag == 0) then
  write(*,*) 'Yes'
 else
  write(*,*) 'No'
 end if
end program main


integer N,baseline
integer,allocatable,dimension(:)::H
logical check
read*,N
allocate(H(N))
read*,H
check=.true.
baseline=H(1)-1
do i=2,N
  if(H(i)>=baseline+1)baseline=H(i)-1
  if(H(I) < baseline)check=.false.
end do
print"(A)",merge("Yes","No ",check)
end


program build_stairs
  implicit none
  integer :: n, h(100000) = 0, i
  read(*,*) n
  read(*,*) h(1:n)
  do i = n-1, 1, -1
    if (h(i) > h(i+1)) h(i) = h(i)-1
    if (h(i) > h(i+1)) then
      write(*,'(a)') "No"
      stop
    end if
  end do
  write(*,'(a)') "Yes"
  stop
end program build_stairs


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

    read(*,*) a,b
    if(mod(a,2019)==0 .or. mod(b,2019)==0)then

        write(*,*)0
        stop
    elseif(b/2019-a/2019/=0)then
        write(*,*)0
        stop
    else
        a=mod(a,2019)
        b=mod(b,2019)
    end if
    ans=10000
    do i=a,b-1
        do j=i+1,b
            c=mod(i*j,2019)
            if(c==0)then
                write(*,*)0
                stop
            elseif(c<ans)then
                ans=c
            end if
        end do
    end do
    write(*,*)ans

    stop
end program sample

Score : 300 points

Problem Statement

You are given two non-negative integers L and R.
We will choose two integers i and j such that L \leq i < j \leq R.
Find the minimum possible value of (i \times j) \mbox{ mod } 2019.

Constraints

All values in input are integers.

0 \leq L < R \leq 2 \times 10^9

Input

Input is given from Standard Input in the following format:

L R

Output

Print the minimum possible value of (i \times j) \mbox{ mod } 2019 when i and j are chosen under the given condition.

Sample Input 1

2020 2040

Sample Output 1

2

When (i, j) = (2020, 2021), (i \times j) \mbox{ mod } 2019 = 2.

Sample Input 2

4 5

Sample Output 2

20

We have only one choice: (i, j) = (4, 5).


      PROGRAM RemainderMinimization2019
      IMPLICIT NONE
      integer(16) :: l,r, i,j
      integer(16) :: ans=2*10**8+1,buffer

      read*,l,r

      do i = l,r-1
        do j = i+1,r
          buffer = mod(i*j,2019)
          ans = min( ans,buffer )
          if( ans==0 )then
            print*,ans;stop
          end if
        end do
      end do

      print*,ans

      END PROGRAM


program ABC133C
    implicit none
    integer(8)::L,R,ans,i,j
    read*,L,R

    if(R/2019-(L-1)/2019>=1)then
        print'(i0)',0
    else
        ans=2019
        do i=L,R-1
            do j=i+1,R
                ans=min(ans,mod(i*j,2019))
            end do
        end do
        print'(i0)',ans
    end if
end program ABC133C


program name
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: l,r,i,j,md

    read*, l,r
    if (r-l+1 >= 2019) then
        print'(i0)', 0
        stop
    end if

    if (mod(l,2019) <= mod(r,2019)) then
        print*, mod(l,2019)*(mod(l,2019)+1)
    else
        print*,0
    end if
end program name


program name
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: l,r,i,j,md

    read*, l,r
    if (r-l+1 >= 3000) then
        print'(i0)', 0
        stop
    end if
    md=50000000
    do i=l,r-1
        do j=i+1,r
            md=min(md,mod(i*j,2019))
        end do
    end do
    print'(i0)', md
end program name


program name
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: l,r,i,j,md

    read*, l,r
    if (r-l+1 >= 3000) then
        print'(i0)', 0
        stop
    end if
    md=50000000
    do i=l,r-1
        do j=i+1,r
            md=min(md,mod(i*j,2019))
        end do
    end do
    print'(i0)', md
end program name


program name
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: l,r,i,j,md

    read*, l,r
    if (r-l+1 >= 2019) then
        print'(i0)', 0
        stop
    end if
    md=50000000
    do i=l,r-1
        do j=i+1,r
            md=min(md,mod(i*j,2019))
        end do
    end do
    print'(i0)', md
end program name


program main
  implicit none
  integer(8) :: l, r, i, j, res
  read (*, *) l, r
  if (r - l >= 2018) then
    write (*, "(i0)") 0
  else
    res = 2019
    do i = l, r - 1
      do j = i + 1, r
        res = min(res, mod(i * j, 2019_8))
      end do
    end do
    write (*, "(i0)") res
  end if
end program main


program remainder_minimization_2019
    implicit none
    integer(8):: l,r,c,i,j
    integer(8),parameter:: max_loop=2019
    c=2019
    read*,l,r
    do i=l, min(r,l+max_loop)-1
        do j=i+1,min(r,l+max_loop)
            c = min(c,mod(i*j,2019))
        end do
    end do
    print*, c

end program remainder_minimization_2019


program ccc

implicit none
integer(8) :: l, r, i, j, ans

read*, l, r

do i=1,10000000
if(l>=2019) then
l=l-2019
r=r-2019
else
exit
end if
end do

if(r>=2019) then
write(*,'(i0)') 0
return
end if

ans=1000000

do i=l, r
 do j=i+1, r
 ans=min(ans,mod(i*j,2019))
 end do
end do

write(*,'(i0)') ans

end program


program ccc

implicit none
integer(8) :: l, r, i, j, ans

read*, l, r

do i=1,10000
if(l>=2019) then
l=l-2019
r=r-2019
else
exit
end if
end do

if(r>=2019) then
write(*,'(i0)') 0
return
end if

ans=1000000

do i=l, r
 do j=i+1, r
 ans=min(ans,mod(i*j,2019))
 end do
end do

write(*,'(i0)') ans

end program


program ccc

implicit none
integer(8) :: l, r, i, j, ans

read*, l, r

do i=1,10000
if(l>=2019) then
l=l-2019
r=r-2019
else
exit
end if
end do

if(r>=2019) then
write(*,'(i0)') 0
return
end if

ans=1000000

do i=l, r-1
 do j=i+1, r-1
 ans=min(ans,mod(i*j,2019))
 end do
end do

write(*,'(i0)') ans

end program


program ccc

implicit none
integer(8) :: l, r, i, ans

read*, l, r

do i=1,10000
if(l>=2019) then
l=l-2019
r=r-2019
else
exit
end if
end do

if(r>=2019) then
write(*,'(i0)') 0
return
end if

ans=1000000

do i=l, r-1
ans=min(ans,mod(i*(i+1),2019))
end do

write(*,'(i0)') ans

end program


program ccc

implicit none
integer(8) :: l, r, i, one, two

read*, l, r

do i=1,10000
if(l>=2019) then
l=l-2019
r=r-2019
else
exit
end if
end do

if(r>=2019) then
write(*,'(i0)') 0
return
end if

one=mod(l*(l+1),2019)
two=mod(r*(r-1),2019)

write(*,'(i0)') min(one,two)

end program


program ccc

implicit none
integer(16) :: l, r, i, one, two

read*, l, r

do i=1,10000
if(l>=2019) then
l=l-2019*i
else
exit
end if
end do

one=l*(l+1)

do i=1,10000
if(r>=2019) then
r=r-2019*i
else
exit
end if
end do

two=r*(r-1)

write(*,'(i0)') min(one,two)

end program


implicit none
integer(8) :: l,r
integer(8),allocatable :: m(:)
integer(8) :: i,s

read *,l,r
allocate(m(l:min(r,l+2019)))
m(:) = 2019
do i = l,min(r,l+2019)
   m(i) = mod(i,2019)
enddo

i = minloc(m,DIM=1) + l - 1
s = m(i)
m(i) = 2019-1

write(*,'(i0)') mod(s * minval(m),2019)
end


program main
implicit none
integer(8) :: l, r, i, j, res

read(*,*) l, r
if( r - l .ge. 2019 ) then
   write(*,'(i0)') 0
   stop
else
   res = 2018
   do i = l, r-1
      do j = i+1, r
         if( mod( mod(i,2019)*mod(j,2019), 2019 ) .le. res ) then
            res = mod( mod(i,2019)*mod(j,2019), 2019 )
         end if
      end do
   end do
   write(*,'(i0)') res
end if
end program main


program min
  implicit none
  integer(8) :: l, r, lmod, ans, rmod, i, j
  integer(8),parameter :: a = 2019

  read *, l, r
  lmod = mod(l,a)
  rmod = mod(r,a)
  if (r >= (l/a+1)*a) then
    ans = 0
  else
    do i = l, r
      do j = i+1, r
        if (mod(i*j,a) < ans) ans = mod(i*j,a)
      end do
    end do
  end if
  print '(i0)', ans
  stop
end program min


module ABC133

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

  ! require all variables to be explicitly declared
  implicit none

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

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

  subroutine task_C (base)

    ! arguments for this <subroutine>
    integer(INT32), intent(in) :: base

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

    ! STEP.01
    ! read out given data
    read(unit=INPUT_UNIT, fmt=*) range_min, range_max

    ! STEP.02
    ! output the answer of this task
    write(unit=OUTPUT_UNIT, fmt='(I0)', advance='yes') calculate_mod_minimum(base, range_min, range_max)

    ! STEP.END
    return

  end subroutine task_C

  pure function calculate_mod_minimum (base, range_min, range_max) result(mod_minimum)

    ! arguments for this <function>
    integer(INT32), intent(in) :: base
    integer(INT32), intent(in) :: range_min
    integer(INT32), intent(in) :: range_max

    ! arrays for this <function>
    integer(INT32), dimension(:), allocatable :: data_mod

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

    ! variables for this <function>
    integer(INT32) :: mod_minimum_1st
    integer(INT32) :: mod_minimum_2nd

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

    if (range_max - range_min .ge. base) then
      mod_minimum = 0_INT32
      return
    else

      allocate( data_mod(range_min:range_max) )

      do itr = range_min, range_max, 1_INT32
        data_mod(itr) = mod(itr, base)
      end do

      mod_minimum_1st = minval(data_mod(:), dim=1)

      if (mod_minimum_1st .eq. 0_INT32) then
        mod_minimum = 0_INT32
        return
      else
        mod_minimum_2nd = minval(data_mod(:), dim=1, mask=data_mod .gt. mod_minimum_1st)
        mod_minimum     = mod(mod_minimum_1st * mod_minimum_2nd, base)
      end if

      deallocate( data_mod )

    end if

    return

  end function calculate_mod_minimum

end module ABC133

program main

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

  ! require all variables to be explicitly declared
  implicit none

  call task_C (2019_INT32)

end program main


program abc133c
  use,intrinsic :: iso_fortran_env
  implicit none
  integer(int64) :: l, r, i, j, ans
  read *, l, r
  if (l - r + 1 >= 2019) then
     print *, 0
  else
     ans = 2019
     do j = l+1,r
        do i = l,j
           ans = min(ans,mod(i*j,2019))
           if (ans == 0) exit
        end do
     end do
     print *, ans
  end if
end program abc133c


program abc133c

   implicit none

   integer(8) :: L, R, i, j , skip
   integer :: ans, calc_mod

   read(*,*) L, R

   ans = 2018
   R = min(R, L+673)

   do i = L, R-1
      do j =i+1, R

         calc_mod = mod(i*j,2019)
         if (ans > calc_mod ) then
            ans = calc_mod
            !if (ans == 0) goto 999  ! 0になったら打ち切る
         else
            cycle
         end if
      enddo
   enddo
999 continue

   print *, ans

end program abc133c


program abc133c

   implicit none

   integer(8) :: L, R, i, j , skip
   integer :: ans, calc_mod

   read(*,*) L, R

   ans = 2018
   R = min(R, L+2019)

   do i = L, R-1
      do j =i+1, R

         calc_mod = mod(i*j,2019)
         if (ans > calc_mod ) then
            ans = calc_mod
            !if (ans == 0) goto 999  ! 0になったら打ち切る
         else
            cycle
         end if
      enddo
   enddo
999 continue

   print *, ans

end program abc133c


program kadai
implicit none
integer(8)  ::  l,r
integer  ::  i,j,k
integer,allocatable  ::  ans(:)
integer  ::  mn

read(*,*) l,r

if (r/2019-l/2019>0) then
    write(*,*) 0
    stop
end if

l=mod(l,2019)
r=mod(r,2019)

allocate(ans(r-l))
ans=2020
k=1
do i=l,r
    do j=i+1,r
        ans(k)=mod(i*j,2019)
        k=k+1
    end do
end do

mn=minval(ans)
!write(*,*) ans
write(*,*) mn!,l,r

stop

end program kadai


program test

integer(8) :: i,j,k,l,r,ll,rr,ans=10**9,new

read(*,*) l,r

ll = mod(l,2019)
rr = mod(r,2019)

if(ll == 0 .or. rr == 0 .or. ll >= rr)then
  ans = 0
else
  do i=ll,rr-1
    do j=i+1,rr
      new = mod(i*j,2019)
      ans = min(new,ans)
    enddo
  enddo
endif

write(*,*) ans

end program


program main
 implicit none
 integer*16 L,R,i,j,m,v,ans
 data m /2019/
 read(*,*) L,R
 ans = 4000
 do i=L,R-1
  do j=i+1,R
   v = mod(i*j,m)
   if(ans >= v) then
    ans = v
   end if
   if(v == 0) then
    exit
   end if
  end do
 end do
 write(*,*) ans
end program main


program main
 implicit none
 integer*16 L,R,i,j,m,v,ans
 data m /2019/
 read(*,*) L,R
 write(*,*) L,R
 ans = 4000
 do i=L,R-1
  do j=i+1,R
   v = mod(i*j,m)
   if(ans >= v) then
    ans = v
   end if
   if(v == 0) then
    stop
   end if
  end do
 end do
 write(*,*) ans
end program main


module ABC133

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

  ! require all variables to be explicitly declared
  implicit none

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

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

  subroutine task_C

    ! variables for this <subroutine>
    integer(INT64) :: range_min
    integer(INT64) :: range_max
    integer(INT64) :: buffer
    integer(INT64) :: answer

    ! support variables for this <subroutine>
    integer(INT64) :: itr_sml, itr_lrg

    ! STEP.01
    ! read out given data
    read(unit=INPUT_UNIT, fmt=*) range_min, range_max

    ! STEP.02
    ! calculate the answer of this task
    answer = huge(answer)

    do itr_sml = range_min,         range_max - 1_INT64, 1_INT64
    do itr_lrg = itr_sml + 1_INT64, range_max,           1_INT64
      buffer = mod(itr_sml * itr_lrg, 2019_INT64)
      if (buffer .lt. answer) answer = buffer
    end do
    end do

    ! STEP.03
    ! output the answer of this task
    write(unit=OUTPUT_UNIT, fmt='(I0)', advance='yes') answer

    ! STEP.END
    return

  end subroutine task_C

end module ABC133

program main

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

  ! require all variables to be explicitly declared
  implicit none

  call task_C

end program main


program main
integer(16) :: l,r,i,j,m,mo,tmp,a
m=2019
read(*,*)l,r

do i = l,r
if (mod(i,2019)==0) then
m = 0
endif
end do

l = mod(l,2019)
r = mod(r,2019)
!if (r < l) then
!tmp = r
!r = l
!l = tmp
!end if

if (l /= r) then
do i = l,r-1
do j = i+1,r
mo = mod(i*j,2019)
if (m > mo) m = mo
end do
end do
write(*,*)m

else
write(*,*)l
end if

end program main


program kadai
implicit none
integer  ::  l,r
integer  ::  i,j
integer,allocatable  ::  ans(:)
integer  ::  mn

read(*,*) l,r

if (r/2019-l/2019>1) then
    write(*,*) 0
    stop
end if

l=mod(l,2019)
r=mod(r,2019)

allocate(ans(r-l))
ans=2020
do i=l,r
    do j=i+1,r
        ans(i+j-2*l)=mod(i*j,2019)
    end do
end do

mn=minval(ans)
!write(*,*) ans
write(*,*) mn,l,r

stop

end program kadai


program main
integer(16) :: l,r,i,j,m,mo,tmp
m=2019
read(*,*)l,r

l = mod(l,2019)
r = mod(r,2019)
if (r < l) then
tmp = r
r = l
l = tmp
end if

do i = l,r-1
do j = i+1,r
mo = mod(i*j,2019)
if (m > mo) m = mo
end do
end do

write(*,*)m

end program main


program main
integer(16) :: l,r,i,j,m,mo,a,b

m=2019
read(*,*)l,r

!l = mod(l,2019)
!r = mod(r,2019)
do i = l,r-1
do j = i+1,r
a = mod(i,2019)
b = mod(j,2019)
mo = mod(a*b,2019)
if (m > mo) m = mo
end do
end do

write(*,*)m

end program main


program main
integer(16) :: l,r,i,j,m,mo

m=2019
read(*,*)l,r

l = mod(l,2019)
r = mod(r,2019)
do i = l,r-1
do j = i+1,r
mo = mod(i*j,2019)
if (m > mo) m = mo
end do
end do

write(*,*)m

end program main


program main
integer(8) :: l,r,i,j,m,mo

m=2019
read(*,*)l,r

l = mod(l,2019)
r = mod(r,2019)
do i = l,r-1
do j = i+1,r
mo = mod(i*j,2019)
if (m > mo) m = mo
end do
end do

write(*,*)m

end program main


program abc133c
   implicit none

   integer(8) :: L, R, i, j , skip
   integer :: ans, calc_mod

   read(*,*) L, R

   ans = 2018

   do i = L, R-1
      do j =i+1, R, i

         calc_mod = mod(i*j,2019)
         if (ans > calc_mod ) then
            ans = calc_mod
         else
            cycle
         end if
      enddo
   enddo

   print *, ans

end program abc133c


program main
integer(8) :: l,r,i,j,m,mo

m=2019
read(*,*)l,r

l = mod(l,2019)
r = mod(r,2019)
do i = l,r
do j = i+1,r
mo = mod(i*j,2019)
if (m > mo) m = mo
end do
end do

write(*,*)m

end program main


program test
integer,allocatable::x(:),y(:),z(:)
integer::r,l,ans,k,i,check
check=1
read(*,*)l,r
if(r-l+1>=2019)then
	ans=0
else
	k=r-l+1
    allocate(x(0:k))
    allocate(y(1:k))
    allocate(z(1:k))
    do i=1,k
    	x(i)=l+i-1
    end do
    do i=1,k
    	z(i)=mod(x(i),673)
    end do
		if(minval(z)==0 .and. k>=3)then
        	ans=0
        else
    		x(0)=x(k)
    		do i=1,k
    		y(i)=mod(x(i-1)*x(i),2019)
    		end do
  			ans=minval(y)
        end if
end if
print*,ans
end program test


program main
integer(4) :: l,r,i,j,m,mo

m=10000
read(*,*)l,r

do i = l,r-1
do j = i+1,r
mo = mod(i*j,2019)
if (m > mo) m = mo
end do
end do

write(*,*)m

end program main


program l2019
  integer(8) :: l,r,lm,rm
  integer :: i,j,m

  read(*,*) l,r
  m=2020
  if((l/673).ne.(r/673)) then
     m=0
  else
     lm=mod(l,2019)
     rm=mod(r,2019)
     do	i=lm,rm-1
	do j=lm+1,rm
           if(mod(i*j,2019).lt.m) then
	      m=mod(i*j,2019)
	   endif
        enddo
     enddo
  endif
  write(*,'(i0)') m

end program l2019


program main
integer :: l,r,i,j,m,mo

m=10000
read(*,*)l,r

do i = l,r-1
do j = i+1,r
mo = mod(i*j,2019)
if (m > mo) m = mo
end do
end do

write(*,*)m

end program main


program main
 implicit none
 integer*8 L,R,i,j,v,mini,m
 data m /2019/
 read(*,*) L,R
 L = mod(L,m)
 R = mod(R,m)
 if(L >= R) then
  write(*,*) 0
 else
  mini = 2018
  do i=L,R-1
   do j=i+1,R
    v = mod(i,m)*mod(j,m)
    v = mod(v,m)
    if(v < mini) then
     mini = v
    end if
   end do
  end do
  write(*,*) mini
 end if
end program main


program main
 implicit none
 integer*8 L,R,i,j,v,mini,m
 data m /2019/
 read(*,*) L,R
 L = mod(L,m)
 R = mod(R,m)
 if(L > R) then
  write(*,*) 0
 else
  mini = 2018
  do i=L,R-1
   do j=i+1,R
    v = mod(i,m)*mod(j,m)
    v = mod(v,m)
    if(v < mini) then
     mini = v
    end if
   end do
  end do
  write(*,*) mini
 end if
end program main


program main
integer(8) :: l,r,i,j,m,mo

m=10000
read(*,*)l,r
do i = l,r
do j = i+1,r
mo = mod(i*j,2019)
if (m > mo) m = mo
end do
end do

write(*,*)m

end program main


program main
 implicit none
 integer*8 L,R,i,j,v,mini,m
 data m /2019/
 read(*,*) L,R
 mini = 2018
 do i=L,R-1
  do j=i+1,R
   v = mod(i,m)*mod(j,m)
   v = mod(v,m)
   if(v < mini) then
    mini = v
   end if
  end do
 end do
 write(*,*) mini
end program main


program l2019
  integer :: l,r
  integer :: i,j,m

  read(*,*) l,r
  m=2019
  if(l-r.lt.2019) then
     do i=l,r-1
        do j=l+1,r
           if(mod(i*j,2019).lt.m) then
              m=mod(i*j,2019)
           endif
           enddo
     enddo
  else
     m=0
  endif
     write(*,'(i0)') m

end program l2019


character(2*10**5) S
integer(16) cntA
integer(16) ans
logical BC
read*,S
cntA=0
ans=0;BC=.false.
do i=1,len_trim(S)
  if(BC)then
    BC=.false.
    cycle
  endif
  if(S(i:i)=="A")cntA=cntA+1
  if(S(i:i)=="B")then
    if(S(i+1:i+1)=="C")then
      ans=ans+cntA
      BC=.true.
    else
      cntA=0
    endif
  endif
  if(S(i:i)=="C")cntA=0
end do

print"(I0)",ans

end

Score : 600 points

Problem Statement

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

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

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

Find the maximum possible number of operations.

Constraints

1 \leq |s| \leq 200000

Each character of s is A, B and C.

Input

Input is given from Standard Input in the following format:

s

Output

Find the maximum possible number of operations.

Sample Input 1

ABCABC

Sample Output 1

3

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

Sample Input 2

C

Sample Output 2

0

Sample Input 3

ABCACCBABCBCAABCB

Sample Output 3

6


character(2*10**5) S
integer cntA
integer ans
logical BC
read*,S
cntA=0
ans=0;BC=.false.
do i=1,len_trim(S)
  if(BC)then
    BC=.false.
    cycle
  endif
  if(S(i:i)=="A")cntA=cntA+1
  if(S(i:i)=="B")then
    if(S(i+1:i+1)=="C")then
      ans=ans+cntA
      BC=.true.
    else
      cntA=0
    endif
  endif
  if(S(i:i)=="C")cntA=0
end do

print"(I0)",ans

end


program abc
  implicit none
  character(200010) :: s
  integer :: x(200010), a, n, m, i
  x = -1
  read(*,*) s
  s = trim(adjustl(s))
  n = len_trim(s)
  if (n.lt.3) then
    write(*,'(i0)') 0
    stop
  end if
  i = 1
  m = 0
  do while (i.le.n-1)
    m = m+1
    if (s(i:i).eq."A") then
      x(m) = 1
    else if (s(i:i+1).eq."BC") then
      x(m) = 0
      i = i+1
    end if
    i = i+1
  end do
  n = m+1
  m = 0
  a = 0
  i = 1
  do i = 1, n
    select case(x(i))
      case(-1)
        a = 0
      case(0)
        m = m+a
      case(1)
        a = a+1
    end select
  end do
  write(*,'(i0)') m
  stop
end program abc


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


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


program kenken
 implicit none
 integer n, k, i, j, sum
 character(200000) s
 sum=0
 n=1
 j=1
 k=len_trim(s)
 read*, s
a: do while(n.ne.0)
 do i=j,k
  n=index(s,"ABC")
   if(n.ne.0) then
   s(n:n+2)="BCA"
   sum=sum+1
   j=n
   cycle a
   end if
 end do
 end do a
   if(n.eq.0) then
   print*, sum
   return
   end if
end program


program kadai
implicit none
integer  ::  i,nag,num,flag,j
character(len=200000)  ::  s

read(*,*) s
nag=len(s)
num=0

do i=1,nag-2
    if (s(i:i)=="A") then
        flag=0
        j=i
        do while(flag==0)
            j=j+1
            if (s(j:j+1)=="BB" .or. s(j:j+1)=="CC" .or. s(j:j+1)=="CB" .or. j>nag-1) then
                flag=1
            end if
            if (s(j:j+1)=="BC") then
                num=num+1
                j=j+1
            end if
        end do
    end if
end do

write(*,*) num

end program kadai


program kadai
implicit none
integer  ::  i,nag,num,flag,j
character(len=200000)  ::  s

read(*,*) s
nag=len(s)
num=0

do i=1,nag-2
    if (s(i:i)=="A") then
        flag=0
        j=i
        do while(flag==0)
            j=j+1
            if (s(j:j+1)=="BB" .or. s(j:j+1)=="CC" .or. s(j:j+1)=="CB") then
                flag=1
            end if
            if (s(j:j+1)=="BC") then
                num=num+1
                j=j+1
            end if
        end do
    end if
end do

write(*,*) num

end program kadai


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

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

end program main


program abc
  implicit none
  character(200000) :: s
  integer :: x(200000), a(3), b(3), n, m, i
  x = 0
  read(*,*) s
  s = trim(adjustl(s))
  n = len_trim(s)
  if (n.lt.3) then
    write(*,'(i0)') 0
    stop
  end if
  do i = 1, n
    x(i) = ichar(s(i:i))-66
  end do
  a = (/-1,0,1/)
  b = (/0,1,-1/)
  m = 0
  i = 1
  do while (i.le.n-2)
    if (sum(abs(x(i:i+2)-a)).eq.0) then
      x(i:i+2) = b
      m = m+1
      i = max(1,i-1)
      if (x(i).ne.-1) i = i+2
    else
      i = i+1
    end if
  end do
  write(*,'(i0)') m
  stop
end program abc


program abc
  implicit none
  character(200000) :: s
  integer :: x(200000), a(3), b(3), n, m, i
  x = 0
  read(*,*) s
  s = trim(adjustl(s))
  n = len_trim(s)
  if (n.lt.3) then
    write(*,'(i0)') 0
    stop
  end if
  do i = 1, n
    x(i) = ichar(s(i:i))-66
  end do
  a = (/-1,0,1/)
  b = (/0,1,-1/)
  m = 0
  i = 1
  do while (i.le.n-2)
    if (sum(abs(x(i:i+2)-a)).eq.0) then
      x(i:i+2) = b
      m = m+1
      i = max(1,i-1)
    else
      i = i+1
    end if
  end do
  write(*,'(i0)') m
  stop
end program abc