program main
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: k,i
    character(3):: c="ACL"

    read*, k
    do i=1,k
        write(*,"(a)",advance="no") c
    end do
    print*, ""
end program main

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

Score : 100 points

Problem Statement

You are given an integer K.
Print the string obtained by repeating the string ACL K times and concatenating them.

For example, if K = 3, print ACLACLACL.

Constraints

1 \leq K \leq 5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

K

Output

Print the string obtained by repeating the string ACL K times and concatenating them.

Sample Input 1

3

Sample Output 1

ACLACLACL

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 union_find_mod
    use,intrinsic :: iso_fortran_env
    private
    type,public:: union_find
        integer(int32),allocatable:: parent(:)
    contains
        procedure:: unite => uf_unite
        procedure:: root => uf_root
    end type

    interface union_find
        module procedure:: uf_init
    end interface
contains
    function uf_init(n) result(uf)
        type(union_find):: uf
        integer(int32):: n

        allocate(uf%parent, source=[(i,i=1,n)])
    end function

    recursive function uf_root(uf,x) result(ret)
        class(union_find):: uf
        integer(int32):: x,ret

        if (uf%parent(x) == x) then
            ret = x
        else
            uf%parent(x) = uf%root(uf%parent(x))
            ret = uf%parent(x)
        end if
    end function

    subroutine uf_unite(uf,a,b)
        class(union_find):: uf
        integer(int32),intent(in):: a,b
        integer(int32):: ra, rb

        ra = uf%root(a); rb = uf%root(b)
        if (ra == rb) return
        uf%parent(ra) = rb
    end subroutine
end module

program main
    use,intrinsic :: iso_fortran_env
    use union_find_mod
    implicit none
    integer(int32):: n,m,i,a,b,ans
    type(union_find):: uf
    integer(int32), allocatable:: cnt(:)

    read*, n,m
    uf = union_find(n)
    allocate(cnt(n),source=0)

    do i=1,m
        read*, a,b
        call uf%unite(a,b)
    end do

    do i=1,n
        cnt(uf%root(i)) = cnt(uf%root(i)) + 1
    end do
    ans=0
    do i=1,n
        if (cnt(i) > 0) ans=ans+1
    end do
    print'(i0)',ans-1
end program main

Score : 300 points

Problem Statement

There are N cities numbered 1 through N, and M bidirectional roads numbered 1 through M.
Road i connects City A_i and City B_i.

Snuke can perform the following operation zero or more times:

Choose two distinct cities that are not directly connected by a road, and build a new road between the two cities.

After he finishes the operations, it must be possible to travel from any city to any other cities by following roads (possibly multiple times).

What is the minimum number of roads he must build to achieve the goal?

Constraints

2 \leq N \leq 100,000

1 \leq M \leq 100,000

1 \leq A_i < B_i \leq N

No two roads connect the same pair of cities.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
:
A_M B_M

Output

Print the answer.

Sample Input 1

3 1
1 2

Sample Output 1

1

Initially, there are three cities, and there is a road between City 1 and City 2.

Snuke can achieve the goal by building one new road, for example, between City 1 and City 3.
After that,

We can travel between 1 and 2 directly.

We can travel between 1 and 3 directly.

We can travel between 2 and 3 by following both roads (2 - 1 - 3).


program main
	implicit none
	integer(8):: X=0,Y,C,N,M,i,j,t,k,D,E
    integer(8),dimension(1:100000):: A,B,NN
    NN=1
	read *, N,M
    do i=1,M
    read *, A(i),B(i)
    NN(A(i))=0
    NN(B(i))=0
    end do
    do i=1,N
    X=X+NN(i)
    end do
    write(*,*)N-X-1

end program main


module number_theory_mod
    use,intrinsic :: iso_fortran_env
    implicit none
    private
    public:: fc_init,comb,inv,mod_pow
    public:: lcm, gcd
    integer(int64):: md,n
    integer(int64), allocatable:: frac(:), ifrac(:)

contains
    subroutine fc_init(input_n, input_md)
        integer(int64),intent(in):: input_n, input_md
        integer(int64):: i

        n = input_n; md = input_md
        allocate(frac(0:n), ifrac(0:n))
        frac(0) = 1; ifrac(0) = 1
        do i=1,n
            frac(i) = mod(frac(i-1)*i, md)
            ifrac(i) = inv(frac(i), md)
        end do
    end subroutine

    function comb(n,p) result(ret)
        integer(int64):: n,p
        integer(int64):: ret
        ret = mod(mod(frac(n)*ifrac(p),md)*ifrac(n-p),md)
    end function

    function inv(x,md) result(ret)
        integer(int64),intent(in):: x,md
        integer(int64):: ret
        ! print*, "inv"
        ret = mod_pow(x,md-2,md)
    end function

    function mod_pow(base,exponent,md) result(ret)
        use,intrinsic :: iso_fortran_env
        integer(int64),intent(in):: base,exponent,md
        integer(int64):: ret,a,x

        ret = 1
        a = base
        x = exponent
        do while(x > 0)
            if (btest(x,0)) ret = mod(ret*a,md)
            a=mod(a*a,md)
            x=rshift(x,1)
        end do
    end function

    function lcm(x,y) result(ret)
        integer(int32):: x,y,ret
        ret=x*y/gcd(x,y)
    end function

    recursive function gcd(x,y) result(ret)
        integer(int32):: x,y,ret

        if (mod(x,y) == 0) then
            ret = y
            return
        end if
        ret = gcd(y,mod(x,y))
    end function
end module

module lazy_segtree_operators
    use,intrinsic :: iso_fortran_env
    use number_theory_mod
    implicit none
    integer(int64),parameter:: md = 998244353
    type s_elem
        integer(int64):: v=0,k=1
    end type
    type f_elem
        integer(int64):: v=0
    end type
    integer(int64):: inv9 = 0
contains
    function f_same(f1,f2)
        type(f_elem),intent(in):: f1,f2
        logical:: f_same

        f_same = (f1%v == f2%v)
    end function

    function s_same(s1,s2)
        type(s_elem),intent(in):: s1,s2
        logical:: s_same

        s_same = (s1%v == s2%v .and. s1%k == s2%k)
    end function

    function e()
        type(s_elem):: e

        e = s_elem()
    end function

    function id()
        type(f_elem):: id

        id = f_elem()
    end function

    function op(a,b)
        type(s_elem),intent(in):: a,b
        type(s_elem):: op

        op%v = modulo(modulo(a%v*b%k,md) + b%v,md)
        op%k = modulo(a%k*b%k,md)
    end function

    function mapping(s,f) result(new_s)
        type(s_elem),intent(in):: s
        type(f_elem),intent(in):: f
        type(s_elem):: new_s

        if (inv9 ==0) inv9 = inv(9_8,md)

        if (f_same(f,id())) then
            new_s = s_elem(s%v,s%k)
        else
            new_s%v = modulo(modulo((s%k-1)*inv9,md)*f%v,md)
            new_s%k = s%k
        end if
    end function

    function composition(old_f,f) result(new_f)
        type(f_elem),intent(in):: old_f,f
        type(f_elem):: new_f
        if (f_same(f,id())) then
            new_f = old_f
        else
            new_f = f
        end if
    end function
end module

module lazy_segtree_mod
    use,intrinsic :: iso_fortran_env
    use lazy_segtree_operators
    implicit none

    type,public:: lazy_segtree
        type(s_elem),allocatable:: d(:)
        type(f_elem),allocatable:: lz(:)
        integer(int32):: len
    contains
        procedure:: leaf => lst_leaf
        procedure:: update => lst_update, lst_update_one
        procedure:: query => lst_query, lst_query_one
        procedure:: to_array => lst_to_array
        procedure:: set => lst_set
    end type
    interface lazy_segtree
        module procedure:: lst_init
    end interface
contains
    function lst_init(n) result(lst)
        type(lazy_segtree):: lst
        integer(int32),intent(in):: n
        integer(int32):: x

        lst%len = n

        x=1
        do while(n > x)
            x = 2*x
        end do
        allocate(lst%d(2*x-1), source=e())
        allocate(lst%lz(2*x-1),source=id())
    end function

    function lst_leaf(lst)
        class(lazy_segtree):: lst
        integer(int32):: lst_leaf

        lst_leaf = size(lst%d)/2+1
    end function

    subroutine lst_set(lst,i,s)
        class(lazy_segtree),intent(inout):: lst
        type(s_elem),intent(in):: s
        integer(int32),value:: i

        i=i+lst%leaf()-1
        lst%d(i) = s
        i=rshift(i,1)
        do while(i > 0)
            lst%d(i) = op(lst%d(i*2), lst%d(i*2+1))
            i=rshift(i,1)
        end do
    end subroutine

    subroutine lst_update_one(lst,i,f)
        class(lazy_segtree),intent(inout):: lst
        type(f_elem),intent(in):: f
        integer(int32),intent(in):: i

        call lst_update(lst,i,i,f)
    end subroutine

    subroutine lst_update(lst, l,r,f)
        class(lazy_segtree),intent(inout):: lst
        type(f_elem),intent(in):: f
        integer(int32),intent(in):: l,r

        call lst_update_sub(lst,l,r,f,1,lst%leaf(),1)
    end subroutine

    recursive subroutine lst_update_sub(lst,ql,qr,f,nl,nr,i)
        class(lazy_segtree),intent(inout):: lst
        integer(int32),intent(in):: ql,qr,nl,nr,i
        type(f_elem),intent(in):: f
        integer(int32):: nm

        call eval(lst,i)
        if (ql <= nl .and. nr <= qr) then
            lst%lz(i) = composition(lst%lz(i), f)
            call eval(lst,i)
        else if (ql <= nr .and. nl <= qr) then
            nm = (nl+nr)/2
            call lst_update_sub(lst,ql,qr,f,nl,  nm,i*2  )
            call lst_update_sub(lst,ql,qr,f,nm+1,nr,i*2+1)
            lst%d(i) = op(lst%d(i*2), lst%d(i*2+1))
        end if
    end subroutine

    subroutine eval(lst,i)
        class(lazy_segtree),intent(inout):: lst
        integer(int32):: i

        if (f_same(lst%lz(i),id())) return
        if (i < lst%leaf()) then
            lst%lz(i*2) = composition(lst%lz(i*2), lst%lz(i))
            lst%lz(i*2+1) = composition(lst%lz(i*2+1), lst%lz(i))
        end if
        lst%d(i) = mapping(lst%d(i),lst%lz(i))
        lst%lz(i) = id()
    end subroutine

    subroutine correct_laziness(lst)
        class(lazy_segtree):: lst
        integer(int32):: i

        do i=1,size(lst%d)
            call eval(lst,i)
        end do
    end subroutine

    function lst_query_one(lst,i) result(ret)
        class(lazy_segtree),intent(inout):: lst
        integer(int32),intent(in):: i
        type(s_elem):: ret

        ret = lst_query(lst,i,i)
    end function

    function lst_query(lst,l,r) result(ret)
        class(lazy_segtree),intent(inout):: lst
        integer(int32), intent(in):: l,r
        type(s_elem):: ret

        ret = lst_query_sub(lst,l,r,1,lst%leaf(),1)
    end function

    recursive function lst_query_sub(lst,ql,qr,nl,nr,i) result(ret)
        class(lazy_segtree),intent(inout):: lst
        integer(int32),intent(in):: ql,qr,nl,nr,i
        integer(int32):: nm
        type(s_elem):: ret,r1,r2

        call eval(lst,i)
        if (nr < ql .or. qr < nl) then
            ret = e()
        else if (ql <= nl .and. nr <= qr) then
            ret = lst%d(i)
        else
            nm = (nl+nr)/2
            r1 = lst_query_sub(lst,ql,qr,nl,  nm,i*2  )
            r2 = lst_query_sub(lst,ql,qr,nm+1,nr,i*2+1)
            ret = op(r1,r2)
        end if
    end function

    function lst_to_array(lst) result(ret)
        class(lazy_segtree):: lst
        type(s_elem):: ret(lst%len)

        call correct_laziness(lst)
        ret(:) = lst%d(lst%leaf():lst%leaf()+lst%len-1)
    end function

    subroutine debug_print(lst)
        class(lazy_segtree):: lst
        integer(int32):: l,r

        print'(a)', "datav"
        l=1; r=1
        do while(r <= size(lst%d))
            print'(*(i0,1x))', lst%d(l:r)%v
            l=l*2; r=r*2+1
        end do

        print'(a)', "datak"
        l=1; r=1
        do while(r <= size(lst%d))
            print'(*(i0,1x))', lst%d(l:r)%k
            l=l*2; r=r*2+1
        end do

        print'(a)', "lz"
        l=1; r=1
        do while(r <= size(lst%lz))
            print'(*(i0,1x))', lst%lz(l:r)%v
            l=l*2; r=r*2+1
        end do
    end subroutine
end module

program main
    use,intrinsic :: iso_fortran_env
    use lazy_segtree_mod
    use lazy_segtree_operators
    implicit none
    integer(int32):: n,q,i
    integer(int32):: l,r,d
    type(lazy_segtree)::lst
    type(s_elem):: ans

    read*, n,q
    lst = lazy_segtree(n)
    do i=1,n
        call lst%set(i,s_elem(1,10))
    end do

    do i=1,q
        read*, l,r,d
        call lst%update(l,r,f_elem(d))
        ans = lst%query(1,n)
        print'(i0)', ans%v
    end do
end program main

Score : 500 points

Problem Statement

You have a string S of length N.
Initially, all characters in S are 1s.

You will perform queries Q times.
In the i-th query, you are given two integers L_i, R_i and a character D_i (which is a digit).
Then, you must replace all characters from the L_i-th to the R_i-th (inclusive) with D_i.

After each query, read the string S as a decimal integer, and print its value modulo 998,244,353.

Constraints

1 \leq N, Q \leq 200,000

1 \leq L_i \leq R_i \leq N

1 \leq D_i \leq 9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
L_1 R_1 D_1
:
L_Q R_Q D_Q

Output

Print Q lines.
In the i-th line print the value of S after the i-th query, modulo 998,244,353.

Sample Input 1

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

Sample Output 1

11222211
77772211
77333333
72333333
72311333

Sample Input 2

200000 1
123 456 7

Sample Output 2

641437905

Don't forget to take the modulo.


module mint_mod
    use, intrinsic :: iso_fortran_env
    implicit none
    integer(int64) :: md = 998244353
    type,public:: mint
        integer(int64), private :: val = 0_8
    contains
        procedure:: to_int => mint_to_int64
    end type

    interface mint
        module procedure:: mi_init
    end interface
    interface operator( + )
        module procedure :: mi_add
    end interface
    interface operator( - )
        module procedure :: mi_sub
    end interface
    interface operator( * )
        module procedure :: mi_mul
    end interface
    interface operator( / )
        module procedure :: mi_div
    end interface
    interface operator( ** )
        module procedure :: mi_pow
    end interface
    interface assignment( = )
        module procedure:: mi_asgn
    end interface
    interface dot_product
        module procedure :: dot_prod
    end interface dot_product
    interface matmul
        module procedure :: matmul1, matmul2, matmul3
    end interface matmul
contains
    pure elemental function mi_init(num) result(ret)
        class(*),intent(in):: num
        type(mint):: ret
        ret = to_modint(num)
    end function

    pure elemental function to_int64(num) result(ret)
        class(*), intent(in) :: num
        integer(int64):: ret

        select type (num)
            type is (mint)
                ret = num%val
            type is (integer(int64))
                ret = num
            type is (integer(int32))
                ret = int(num, int64)
            type is (integer(int16))
                ret = int(num, int64)
            type is (integer(int8))
                ret = int(num, int64)
            class default
                ret = 0_8
                ret = 1_8/ret
        end select
        ret=modulo(ret,md)
    end function

    pure elemental function mint_to_int64(mx) result(ret)
        class(mint),intent(in):: mx
        integer(int64):: ret

        ret = to_int64(mx)
    end function

    pure elemental function to_modint(num) result(ret)
        class(*), intent(in):: num
        type(mint):: ret

        ret%val = to_int64(num)
    end function

    pure elemental subroutine mi_asgn(xm,y)
        type(mint), intent(inout) :: xm
        class(*), intent(in) :: y

        xm%val = to_int64(y)
    end subroutine

    pure elemental function mi_add(xm,y) result(ret)
        class(mint), intent(in) :: xm
        class(*), intent(in) :: y
        type(mint):: ret

        ret%val = modulo(xm%val + to_int64(y), md)
    end function

    pure elemental function mi_sub(xm,y) result(ret)
        class(mint), intent(in) :: xm
        class(*), intent(in) :: y
        type(mint):: ret

        ret%val = modulo(xm%val - to_int64(y), md)
    end function

    pure elemental function mi_mul(xm,y) result(ret)
        class(mint), intent(in) :: xm
        class(*), intent(in) :: y
        type(mint) ret

        ret%val = modulo(xm%val * to_int64(y), md)
    end function

    pure elemental function mi_div(xm,y) result(ret)
        class(mint), intent(in) :: xm
        class(*), intent(in) :: y
        type(mint):: ret

        ret%val = xm%val * to_int64(inv(to_modint(y)))
    end function

    pure elemental function inv(xm) result(ret)
        class(mint), intent(in) :: xm
        type(mint):: ret
        integer(int64) :: a, b, c, n
        integer(int64) :: x, y, z, m

        a = xm%val
        b = md
        c = 0_8
        n = 1_8
        do while (b /= 0_8)
            x = b
            y = mod(a,b)
            z = n-a/b*c
            m = c
            a = x
            b = y
            c = z
            n = m
        end do
        ret%val = n
    end function

    pure elemental function mi_pow(xm,y) result(ret)
        class(mint), intent(in) :: xm
        class(*), intent(in) :: y
        type(mint):: ret
        integer(int64) :: n
        type(mint) :: i

        ret%val = 1_8
        i%val = xm%val
        n = to_int64(y)
        do while (n > 0_8)
            if (btest(n,0)) ret%val = to_int64(mi_mul(ret,i))
            i%val = to_int64(mi_mul(i,i))
            n = rshift(n,1)
        end do
    end function

    pure type(mint) function dot_prod(x,y) result(ret)
        type(mint), intent(in) :: x(:), y(:)
        integer(int64):: i
        if (size(x,1) /= size(y,1)) i = to_int64('')
        do i = 1, size(x,1)
            call mi_asgn(ret,mi_add(ret,mi_mul(x(i),y(i))))
        end do
    end function

    pure function matmul1(x,y) result(ret)
        type(mint), intent(in) :: x(:,:), y(:)
        type(mint) :: ret(size(x,1))
        integer :: i
        do i = 1, size(x,1)
            call mi_asgn(ret(i),dot_prod(x(i,:),y))
        end do
    end function

    pure function matmul2(x,y) result(ret)
        type(mint), intent(in) :: x(:), y(:,:)
        type(mint) :: ret(size(y,2))
        integer :: i
        do i = 1, size(y,2)
            call mi_asgn(ret(i),dot_prod(x,y(:,i)))
        end do
    end function

    pure function matmul3(x,y) result(ret)
        type(mint), intent(in) :: x(:,:), y(:,:)
        type(mint) :: ret(size(x,1),size(y,2))
        integer :: i
        do i = 1, size(x,1)
            call mi_asgn(ret(i,:),matmul2(x(i,:),y))
        end do
    end function
end module mint_mod

module lazy_segtree_operators
    use mint_mod
    use,intrinsic :: iso_fortran_env
    implicit none
    type s_elem
        type(mint):: v,k
    end type
    type f_elem
        type(mint):: v
    end type
contains
    function f_same(f1,f2)
        type(f_elem),intent(in):: f1,f2
        logical:: f_same

        f_same = (f1%v%to_int() == f2%v%to_int())
    end function

    function e()
        type(s_elem):: e

        e = s_elem(mint(0),mint(1))
    end function

    function id()
        type(f_elem):: id

        id = f_elem(mint(0))
    end function

    function op(a,b)
        type(s_elem),intent(in):: a,b
        type(s_elem):: op

        op%v = a%v*a%k + b%v
        op%k = a%k*b%k
    end function

    function mapping(s,f) result(new_s)
        type(s_elem),intent(in):: s
        type(f_elem),intent(in):: f
        type(s_elem):: new_s
        type(mint):: inv9
        inv9 = inv(mint(9))

        ! print*, "mp"
        if (f_same(f,id())) then
            new_s%v = s%v
            new_s%k = s%k
        else
            new_s%v = ((s%k-1)*inv9)*f%v
            new_s%k = s%k
            ! print'(*(i0,1x))', new_s%v%to_int()
        end if
    end function

    function composition(old_f,f) result(new_f)
        type(f_elem),intent(in):: old_f,f
        type(f_elem):: new_f
        ! print*, "cps"
        if (f_same(f,id())) then
            new_f = old_f
        else
            new_f = f
        end if
    end function
end module

module lazy_segtree_mod
    use,intrinsic :: iso_fortran_env
    use lazy_segtree_operators
    implicit none

    type,public:: lazy_segtree
        type(s_elem),allocatable:: d(:)
        type(f_elem),allocatable:: lz(:)
        integer(int32):: len
    contains
        procedure:: leaf => lst_leaf
        procedure:: update => lst_update, lst_update_one
        procedure:: query => lst_query, lst_query_one
        procedure:: to_array => lst_to_array
        procedure:: set => lst_set
    end type
    interface lazy_segtree
        module procedure:: lst_init
    end interface
contains
    function lst_init(n) result(lst)
        type(lazy_segtree):: lst
        integer(int32),intent(in):: n
        integer(int32):: x

        lst%len = n

        x=1
        do while(n > x)
            x = 2*x
        end do
        allocate(lst%d(2*x-1), source=e())
        allocate(lst%lz(2*x-1),source=id())
    end function

    function lst_leaf(lst)
        class(lazy_segtree):: lst
        integer(int32):: lst_leaf

        lst_leaf = size(lst%d)/2+1
    end function

    subroutine lst_set(lst,i,s)
        class(lazy_segtree),intent(inout):: lst
        type(s_elem),intent(in):: s
        integer(int32),value:: i

        i=i+lst%leaf()-1
        lst%d(i) = s
        i=rshift(i,1)
        do while(i > 0)
            lst%d(i) = op(lst%d(i*2), lst%d(i*2+1))
            i=rshift(i,1)
        end do
    end subroutine

    subroutine lst_update_one(lst,i,f)
        class(lazy_segtree),intent(inout):: lst
        type(f_elem),intent(in):: f
        integer(int32),intent(in):: i

        call lst_update(lst,i,i,f)
    end subroutine

    subroutine lst_update(lst, l,r,f)
        class(lazy_segtree),intent(inout):: lst
        type(f_elem),intent(in):: f
        integer(int32),intent(in):: l,r

        call lst_update_sub(lst,l,r,f,1,lst%leaf(),1)
    end subroutine

    recursive subroutine lst_update_sub(lst,ql,qr,f,nl,nr,i)
        class(lazy_segtree),intent(inout):: lst
        integer(int32),intent(in):: ql,qr,nl,nr,i
        type(f_elem),intent(in):: f
        integer(int32):: nm

        ! print'(*(i0,1x))', ql,qr,nl,nr
        ! print'(*(i0,1x))', f%v%to_int()
        call eval(lst,i)
        if (ql <= nl .and. nr <= qr) then
            ! print*, "u",1
            lst%lz(i) = composition(lst%lz(i), f)
            call eval(lst,i)
        else if (ql <= nr .and. nl <= qr) then
            ! print*, "u",2
            nm = (nl+nr)/2
            call lst_update_sub(lst,ql,qr,f,nl,  nm,i*2  )
            call lst_update_sub(lst,ql,qr,f,nm+1,nr,i*2+1)
            lst%d(i) = op(lst%d(i*2), lst%d(i*2+1))
        end if
    end subroutine

    subroutine eval(lst,i)
        class(lazy_segtree):: lst
        integer(int32):: i

        ! print*, "ev", i
        if (f_same(lst%lz(i),id())) return
        if (i < lst%leaf()) then
            ! print*, "comp",i
            lst%lz(i*2) = composition(lst%lz(i*2), lst%lz(i))
            lst%lz(i*2+1) = composition(lst%lz(i*2+1), lst%lz(i))
        end if
        ! print'(a)', "mp"
        lst%d(i) = mapping(lst%d(i),lst%lz(i))
        ! print'(a)', "id"
        lst%lz(i) = id()
    end subroutine

    subroutine correct_laziness(lst)
        class(lazy_segtree):: lst
        integer(int32):: i

        do i=1,size(lst%d)
            call eval(lst,i)
        end do
    end subroutine

    function lst_query_one(lst,i) result(ret)
        class(lazy_segtree),intent(inout):: lst
        integer(int32),intent(in):: i
        type(s_elem):: ret

        ret = lst_query(lst,i,i)
    end function

    function lst_query(lst,l,r) result(ret)
        class(lazy_segtree),intent(inout):: lst
        integer(int32), intent(in):: l,r
        type(s_elem):: ret

        ret = lst_query_sub(lst,l,r,1,lst%leaf(),1)
    end function

    recursive function lst_query_sub(lst,ql,qr,nl,nr,i) result(ret)
        class(lazy_segtree),intent(inout):: lst
        integer(int32),intent(in):: ql,qr,nl,nr,i
        integer(int32):: nm
        type(s_elem):: ret,r1,r2

        call eval(lst,i)
        if (nr < ql .or. qr < nl) then
            ret = e()
        else if (ql <= nl .and. nr <= qr) then
            ret = lst%d(i)
        else
            nm = (nl+nr)/2
            r1 = lst_query_sub(lst,ql,qr,nl,  nm,i*2  )
            r2 = lst_query_sub(lst,ql,qr,nm+1,nr,i*2+1)
            ret = op(r1,r2)
        end if
    end function

    function lst_to_array(lst) result(ret)
        class(lazy_segtree):: lst
        type(s_elem):: ret(lst%len)

        call correct_laziness(lst)
        ret(:) = lst%d(lst%leaf():lst%leaf()+lst%len-1)
    end function

    subroutine debug_print(lst)
        class(lazy_segtree):: lst
        integer(int32):: l,r

        print'(a)', "datav"
        l=1; r=1
        do while(r <= size(lst%d))
            print'(*(i0,1x))', lst%d(l:r)%v%to_int()
            l=l*2; r=r*2+1
        end do

        print'(a)', "datak"
        l=1; r=1
        do while(r <= size(lst%d))
            print'(*(i0,1x))', lst%d(l:r)%k%to_int()
            l=l*2; r=r*2+1
        end do

        print'(a)', "lz"
        l=1; r=1
        do while(r <= size(lst%lz))
            print'(*(i0,1x))', lst%lz(l:r)%v%to_int()
            l=l*2; r=r*2+1
        end do

    end subroutine
end module

program main
    use,intrinsic :: iso_fortran_env
    use lazy_segtree_mod
    use lazy_segtree_operators
    implicit none
    integer(int32):: n,q,i
    integer(int32):: l,r,d
    type(lazy_segtree)::lst
    type(s_elem):: ans

    read*, n,q
    lst = lazy_segtree(n)
    do i=1,n
        ! print'(i0)', i
        call lst%set(i,s_elem(mint(1),mint(10)))
    end do

    ! do i=1,q
    !     read*, l,r,d
    !     ! print'(a)', "update"
    !     call lst%update(l,r,f_elem(mint(d)))
    !     ! call debug_print(lst)
    !     ! print'(a)', "query"
    !     ans = lst%query(1,n)
    !     print'(i0)', ans%v%to_int()
    ! end do
end program main


program abc67
 implicit none
 integer(8)::length
 character(len=1002)::S
 read(*,*)S
 length=len_trim(S)
 if(S(length:length)=="s")then
  S(length+1:length+2)="es"
 else
  S(length+1:length+1)="s"
 end if
 print'(a)',trim(S)
 end program abc67

Score : 100 points

Problem Statement

In the Kingdom of AtCoder, people use a language called Taknese, which uses lowercase English letters.

In Taknese, the plural form of a noun is spelled based on the following rules:

If a noun's singular form does not end with s, append s to the end of the singular form.

If a noun's singular form ends with s, append es to the end of the singular form.

You are given the singular form S of a Taknese noun. Output its plural form.

Constraints

S is a string of length 1 between 1000, inclusive.

S contains only lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

Print the plural form of the given Taknese word.

Sample Input 1

apple

Sample Output 1

apples

apple ends with e, so its plural form is apples.

Sample Input 2

bus

Sample Output 2

buses

bus ends with s, so its plural form is buses.

Sample Input 3

box

Sample Output 3

boxs


program c179

implicit none
integer :: n, a, z, c
real :: b

read *, n

z = 0

do a = 1, n
 b = real(n) / real(a)
 c = int(b)
 z = z + c
 if(a*c == n) then
  z = z - 1
 end if
end do

print *, z

end program c179

Score : 300 points

Problem Statement

Given is a positive integer N.
How many tuples (A,B,C) of positive integers satisfy A \times B + C = N?

Constraints

2 \leq N \leq 10^6

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

3

Sample Output 1

3

There are 3 tuples of integers that satisfy A \times B + C = 3: (A, B, C) = (1, 1, 2), (1, 2, 1), (2, 1, 1).

Sample Input 2

100

Sample Output 2

473

Sample Input 3

1000000

Sample Output 3

13969985


program sequence_sum
  implicit none
  integer(8) :: n, num, k, tmp, a(0:100000) = 0
  integer :: x, m, b(0:100000) = 0, period, offset, rem, i
  read(*,*) n, x, m
  tmp = m
  if (n <= 2 * m) then
    num = x
    do k = 2, n
      x = mod(int(x, 8) * x, tmp)
      if (x == 0) exit
      num = num + x
    end do
    write(*,'(i0)') num
    stop
  end if
  a(1) = x
  b(x) = 1
  do i = 2, m + 1
    x = int(mod(int(x, 8) * x, tmp))
    a(i) = a(i - 1) + x
    if (b(x) > 0) then
      offset = b(x) - 1
      period = i - offset - 1
      exit
    end if
    b(x) = i
  end do
  num = (n - offset) / period
  rem = int(n - offset - num * period)
  write(*,'(i0)') (a(offset + period) - a(offset)) * num + a(offset + rem)
end program sequence_sum

Score : 500 points

Problem Statement

Let us denote by f(x, m) the remainder of the Euclidean division of x by m.

Let A be the sequence that is defined by the initial value A_1=X and the recurrence relation A_{n+1} = f(A_n^2, M).
Find \displaystyle{\sum_{i=1}^N A_i}.

Constraints

1 \leq N \leq 10^{10}

0 \leq X < M \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X M

Output

Print \displaystyle{\sum_{i=1}^N A_i}.

Sample Input 1

6 2 1001

Sample Output 1

1369

The sequence A begins 2,4,16,256,471,620,\ldots Therefore, the answer is 2+4+16+256+471+620=1369.

Sample Input 2

1000 2 16

Sample Output 2

6

The sequence A begins 2,4,0,0,\ldots Therefore, the answer is 6.

Sample Input 3

10000000000 10 99959

Sample Output 3

492443256176507


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

Score : 100 points

Problem Statement

Given is an integer x that is greater than or equal to 0, and less than or equal to 1.
Output 1 if x is equal to 0, or 0 if x is equal to 1.

Constraints

0 \leq x \leq 1

x is an integer

Input

Input is given from Standard Input in the following format:

x

Output

Print 1 if x is equal to 0, or 0 if x is equal to 1.

Sample Input 1

1

Sample Output 1

0

Sample Input 2

0

Sample Output 2

1


program a
	implicit none
    integer :: i
    write(*,'(i0)') 1-i
end program a


program Main
implicit none
    integer(16) ans,a,b,c,d,x,y
    read (*,*) a, b, c, d
    x=max(a,b)
    y=max(c,d)
    if ((x>0.and.y>0)) then
        ans=x*y
    else if(x<1 .and.y<1) then
        x=min(a,b)
        y=min(b,c)
        ans=x*y
    else if(x<0) then
        y=min(c,d)
        ans=x*y
    else
        x=min(a,b)
        ans=x*y
    end if
    write(*,*) ans
end program Main

Score : 200 points

Problem Statement

Given are integers a,b,c and d.
If x and y are integers and a \leq x \leq b and c\leq y \leq d hold, what is the maximum possible value of x \times y?

Constraints

-10^9 \leq a \leq b \leq 10^9

-10^9 \leq c \leq d \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

a b c d

Output

Print the answer.

Sample Input 1

1 2 1 1

Sample Output 1

2

If x = 1 and y = 1 then x \times y = 1.
If x = 2 and y = 1 then x \times y = 2.
Therefore, the answer is 2.

Sample Input 2

3 5 -4 -2

Sample Output 2

-6

The answer can be negative.

Sample Input 3

-1000000000 0 -1000000000 0

Sample Output 3

1000000000000000000


program redistribution
  implicit none
  integer :: s
  integer(8) :: m(3, 3) = 0
  read(*,*) s
  if (s < 6) then
    write(*,'(i0)') s / 3
    stop
  end if
  m(1, 1) = 1
  m(2, 1) = 1
  m(3, 2) = 1
  m(1, 3) = 1
  m = modmatpow(m, s - 6)
  write(*,'(i0)') mod(m(1, 1) + m(1, 1) + m(1, 2) + m(1, 3), 1000000007_8)
contains
  function modmatmul(x, y) result(res)
    integer(8), parameter :: modu = 1000000007_8
    integer(8), intent(in) :: x(3, 3), y(3, 3)
    integer(8) :: tmp, res(3, 3)
    integer :: j, k
    do j = 1, 3
      res(1, j) = 0
      res(2, j) = 0
      res(3, j) = 0
      do k = 1, 3
        tmp = y(k, j)
        res(1, j) = res(1, j) + x(1, k) * tmp
        res(2, j) = res(2, j) + x(2, k) * tmp
        res(3, j) = res(3, j) + x(3, k) * tmp
      end do
      res(1, j) = mod(res(1, j), modu)
      res(2, j) = mod(res(2, j), modu)
      res(3, j) = mod(res(3, j), modu)
    end do
  end
  function modmatpow(x, n) result(res)
    integer(8), intent(in) :: x(3, 3)
    integer, intent(in) :: n
    integer(8) :: res(3, 3), tmp(3, 3)
    integer :: m
    res(1, 1) = 1
    res(2, 1) = 0
    res(3, 1) = 0
    res(1, 2) = 0
    res(2, 2) = 1
    res(3, 2) = 0
    res(1, 3) = 0
    res(2, 3) = 0
    res(3, 3) = 1
    tmp = x
    m = n
    do while (m > 0)
      if (btest(m, 0)) res = modmatmul(res, tmp)
      tmp = modmatmul(tmp, tmp)
      m = rshift(m, 1)
    end do
  end
end program redistribution

Score : 400 points

Problem Statement

Given is an integer S.
Find how many sequences there are whose terms are all integers greater than or equal to 3, and whose sum is equal to S.
The answer can be very large, so output it modulo 10^9 + 7.

Constraints

1 \leq S \leq 2000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

S

Output

Print the answer.

Sample Input 1

7

Sample Output 1

3

3 sequences satisfy the condition: \{3,4\}, \{4,3\} and \{7\}.

Sample Input 2

2

Sample Output 2

0

There are no sequences that satisfy the condition.

Sample Input 3

1729

Sample Output 3

294867501


program redistribution
  implicit none
  integer :: s
  integer(8) :: m(3, 3) = 0
  read(*,*) s
  if (s < 6) then
    write(*,'(i0)') s / 3
    stop
  end if
  m(1, 1) = 1
  m(2, 1) = 1
  m(3, 2) = 1
  m(1, 3) = 1
  m = modmatpow(m, s - 6)
  write(*,'(i0)') mod(m(1, 1) + m(1, 1) + m(1, 2) + m(1, 3), 1000000007_8)
contains
  function modmatmul(x, y) result(res)
    integer(8), parameter :: modu = 1000000007_8
    integer(8), intent(in) :: x(3, 3), y(3, 3)
    integer(8) :: tmp, res(3, 3)
    integer :: i, j, k
    do j = 1, 3
      do i = 1, 3
        res(i, j) = 0
      end do
      do k = 1, 3
        tmp = y(k, j)
        do i = 1, 3
          res(i, j) = mod(res(i, j) + x(i, k) * tmp, modu)
        end do
      end do
    end do
  end
  function modmatpow(x, n) result(res)
    integer(8), intent(in) :: x(3, 3)
    integer, intent(in) :: n
    integer(8) :: res(3, 3), tmp(3, 3)
    integer :: m
    res(1, 1) = 1
    res(2, 1) = 0
    res(3, 1) = 0
    res(1, 2) = 0
    res(2, 2) = 1
    res(3, 2) = 0
    res(1, 3) = 0
    res(2, 3) = 0
    res(3, 3) = 1
    tmp = x
    m = n
    do while (m > 0)
      if (btest(m, 0)) res = modmatmul(res, tmp)
      tmp = modmatmul(tmp, tmp)
      m = rshift(m, 1)
    end do
  end
end program redistribution


program redistribution
  implicit none
  integer(8), parameter :: modu = 1000000007
  integer :: s
  integer(8) :: m(3, 3) = 0
  read(*,*) s
  if (s < 6) then
    write(*,'(i0)') s / 3
    stop
  end if
  m(1, 1) = 1
  m(2, 1) = 1
  m(3, 2) = 1
  m(1, 3) = 1
  m = modmatpow(m, s - 6)
  write(*,'(i0)') mod(m(1, 1) + m(1, 1) + m(1, 2) + m(1, 3), modu)
contains
  function modmatmul(x, y) result(res)
    integer(8), parameter :: modu = 1000000007
    integer(8), intent(in) :: x(3, 3), y(3, 3)
    integer(8) :: tmp, res(3, 3)
    integer :: i, j, k
    do j = 1, 3
      do i = 1, 3
        res(i, j) = 0
      end do
      do k = 1, 3
        tmp = y(k, j)
        do i = 1, 3
          res(i, j) = mod(res(i, j) + x(i, k) * tmp, modu)
        end do
      end do
    end do
  end
  function modmatpow(x, n) result(res)
    integer(8), intent(in) :: x(3, 3)
    integer, intent(in) :: n
    integer(8) :: res(3, 3), tmp(3, 3)
    integer :: m
    res(1, 1) = 1
    res(2, 1) = 0
    res(3, 1) = 0
    res(1, 2) = 0
    res(2, 2) = 1
    res(3, 2) = 0
    res(1, 3) = 0
    res(2, 3) = 0
    res(3, 3) = 1
    tmp = x
    m = n
    do while (m > 0)
      if (btest(m, 0)) res = modmatmul(res, tmp)
      tmp = modmatmul(tmp, tmp)
      m = rshift(m, 1)
    end do
  end
end program redistribution


program redistribution
  implicit none
  integer(8), parameter :: modu = 1000000007
  integer :: s
  integer(8) :: m(3, 3) = 0
  read(*,*) s
  if (s < 6) then
    write(*,'(i0)') s / 3
    stop
  end if
  m(1, 1) = 1
  m(2, 1) = 1
  m(3, 2) = 1
  m(1, 3) = 1
  m = modmatpow(m, s - 6)
  write(*,'(i0)') mod(m(1, 1) * 2 + m(1, 2) + m(1, 3), modu)
contains
  function modmatmul(x, y) result(res)
    integer(8), intent(in) :: x(3, 3), y(3, 3)
    integer(8) :: tmp, res(3, 3)
    integer :: i, j, k
    do j = 1, 3
      do i = 1, 3
        res(i, j) = 0
      end do
      do k = 1, 3
        tmp = y(k, j)
        do i = 1, 3
          res(i, j) = mod(res(i, j) + x(i, k) * tmp, modu)
        end do
      end do
    end do
  end
  function modmatpow(x, n) result(res)
    integer(8), intent(in) :: x(3, 3)
    integer, intent(in) :: n
    integer(8) :: res(3, 3), tmp(3, 3)
    integer :: m, i
    res = 0
    do i = 1, 3
      res(i, i) = 1
    end do
    tmp = x
    m = n
    do while (m > 0)
      if (btest(m, 0)) res = modmatmul(res, tmp)
      tmp = modmatmul(tmp, tmp)
      m = rshift(m, 1)
    end do
  end
end program redistribution


module agc59
 implicit none
contains
 subroutine agc59a(a,x,y)
  integer(8),intent(in)::x,y
  integer(8),intent(in)::a(:)
  print'(i0)',a(x)+y
 end subroutine agc59a
 subroutine agc59b(a,x,y)
  integer(8),intent(in)::x,y
  integer(8),intent(in)::a(:)
  print'(i0)',sum(a(x:y-1))
 end subroutine agc59b
end module agc59

program abc59
 use agc59
 implicit none
 integer(8)::N,Q,p,x,y,ia,ib
 integer(8),allocatable::a(:)
 read(*,*)N,Q
 allocate(a(N))
 read(*,*)(a(ia),ia=1,N)
 do ib=1,Q
  read(*,*)p,x,y
  if(p==0)then
   x=x+1
   call agc59a(a,x,y)
  else
   x=x+1
   y=y+1
   call agc59b(a,x,y)
  end if
 end do
 deallocate(a)
end program abc59

Score : 100 points

Problem Statement

You are given an array a_0, a_1, ..., a_{N-1} of length N. Process Q queries of the following types.

0 p x: a_p \gets a_p + x

1 l r: Print \sum_{i = l}^{r - 1}{a_i}.

Constraints

1 \leq N, Q \leq 500,000

0 \leq a_i, x \leq 10^9

0 \leq p < N

0 \leq l_i < r_i \leq N

All values in Input are integer.

Input

Input is given from Standard Input in the following format:

N Q
a_0 a_1 ... a_{N - 1}
\textrm{Query}_0
\textrm{Query}_1
:
\textrm{Query}_{Q - 1}

Output

For each query of the latter type, print the answer.

Sample Input 1

5 5
1 2 3 4 5
1 0 5
1 2 4
0 3 10
1 0 5
1 0 3

Sample Output 1

15
7
25
6


program donot_be_late
    implicit none
    integer d, t, s
    real ans, a, b

    read *, d, t, s

    a=real(d)
    b=real(s)
    ans=a/b
    print *, ans
    if(t>=ans)then
        print *,"Yes"
    else
        print *,"No"
    endif
end program donot_be_late

Score : 100 points

Problem Statement

Takahashi is meeting up with Aoki.

They have planned to meet at a place that is D meters away from Takahashi's house in T minutes from now.

Takahashi will leave his house now and go straight to the place at a speed of S meters per minute.

Will he arrive in time?

Constraints

1 \leq D \leq 10000

1 \leq T \leq 10000

1 \leq S \leq 10000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

D T S

Output

If Takahashi will reach the place in time, print Yes; otherwise, print No.

Sample Input 1

1000 15 80

Sample Output 1

Yes

It takes 12.5 minutes to go 1000 meters to the place at a speed of 80 meters per minute. They have planned to meet in 15 minutes so he will arrive in time.

Sample Input 2

2000 20 100

Sample Output 2

Yes

It takes 20 minutes to go 2000 meters to the place at a speed of 100 meters per minute. They have planned to meet in 20 minutes so he will arrive just on time.

Sample Input 3

10000 1 1

Sample Output 3

No

He will be late.


program ABC177A
    implicit none
    integer(8)::D,T,S
    read*,D,T,S
    if(S*T>=D)then
        print'(A)',"Yes"
    else
        print'(A)',"No"
    end if
end program ABC177A


program substring
  implicit none
  character(1000) :: s, t
  integer :: ns, nt, d = 1e9, x, i, j
  read(*,*) s
  read(*,*) t
  ns = len_trim(s)
  nt = len_trim(t)
  do i = 1, ns - nt + 1
    x = 0
    do j = 1, nt
      if (s(i + j - 1:i + j - 1) /= t(j:j)) x = x + 1
    end do
    d = min(d, x)
  end do
  write(*,'(i0)') d
end program substring

Score : 200 points

Problem Statement

Given are two strings S and T.

Let us change some of the characters in S so that T will be a substring of S.

At least how many characters do we need to change?

Here, a substring is a consecutive subsequence. For example, xxx is a substring of yxxxy, but not a substring of xxyxx.

Constraints

The lengths of S and T are each at least 1 and at most 1000.

The length of T is at most that of S.

S and T consist of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S
T

Output

Print the minimum number of characters in S that need to be changed.

Sample Input 1

cabacc
abc

Sample Output 1

1

For example, changing the fourth character a in S to c will match the second through fourth characters in S to T.

Since S itself does not have T as its substring, this number of changes - one - is the minimum needed.

Sample Input 2

codeforces
atcoder

Sample Output 2

6


module mod_skip_list
  implicit none
  integer, private, parameter :: key_kind = 4
  integer, private, parameter :: val_kind = 4
  real(8), private, parameter :: threshold = 0.5d0
  integer(key_kind), private, parameter :: infty = lshift(1_key_kind, 8 * key_kind - 2)
  type t_node
    private
    integer(key_kind) :: key
    integer(val_kind) :: val
    integer :: length = 0
    type(t_node), pointer :: prev => null(), next => null()
    type(t_node), pointer :: above => null(), below => null()
  end type
  type t_skip_list
    integer :: size = 0
    integer :: level = 0
    integer(val_kind) :: default = 0
    type(t_node), private, pointer :: head => null()
    type(t_node), private, pointer :: tail => null()
  contains
    procedure :: search => search
    procedure :: contains => contain
    procedure :: insert => insert
    procedure :: index_of => index_of
    procedure :: get_key_at => get_key_at
    procedure :: remove => remove
    procedure :: remove_key_at => remove_key_at
    procedure :: first_key => first_key
    procedure :: poll_first => poll_first
    procedure :: last_key => last_key
    procedure :: poll_last => poll_last
    procedure :: floor_key => floor_key
    procedure :: lower_key => lower_key
    procedure :: ceiling_key => ceiling_key
    procedure :: higher_key => higher_key
    final :: finalize
  end type
  interface skip_list
    module procedure :: newsl0
  end interface
  private :: t_node, random_seed_clock, new_node, finalize, increase_level, search_node
contains
  subroutine random_seed_clock()
    integer :: nseed, clock
    integer, allocatable :: seed(:)
    call system_clock(clock)
    call random_seed(size = nseed)
    allocate(seed(nseed))
    seed = clock
    call random_seed(put = seed)
    deallocate(seed)
  end
  logical function flip_coin() result(res)
    real(8) :: rand
    call random_number(rand)
    res = rand < threshold
  end
  function new_node(key, val, length) result(res)
    integer(key_kind), intent(in) :: key
    integer(val_kind), intent(in) :: val
    integer, intent(in) :: length
    type(t_node), pointer :: res
    allocate(res)
    res%key = key
    res%val = val
    res%length = length
  end
  subroutine finalize(this)
    type(t_skip_list), intent(inout) :: this
    call clear(this)
  end
  subroutine clear(this)
    class(t_skip_list), intent(inout) :: this
    type(t_node), pointer :: node, next, above
    if (.not.associated(this%head)) return
    node => this%head
    do while (associated(node%below))
      node => node%below
    end do
    do while (associated(node))
      next => node%next
      do while (associated(node))
        above => node%above
        deallocate(node)
        node => above
      end do
      node => next
    end do
    this%head => null()
    this%tail => null()
    this%size = 0
  end
  type(t_skip_list) function newsl0() result(res)
    type(t_node), pointer :: head, tail
    call random_seed_clock()
    head => new_node(-infty, 0, 1)
    tail => new_node(infty, 0, infty)
    head%next => tail
    tail%next => head
    res%head => head
    res%tail => tail
  end
  subroutine increase_level(this, level)
    type(t_skip_list), intent(inout) :: this
    integer, intent(in) :: level
    type(t_node), pointer :: head, tail, habove, tabove
    integer :: i
    if (this%level >= level) return
    head => this%head
    tail => this%tail
    do i = 1, this%level
      head => head%above
      tail => tail%above
    end do
    do i = this%level + 1, level
      habove => new_node(-infty, 0, 1)
      head%above => habove
      habove%below => head
      tabove => new_node(infty, 0, infty)
      tail%above => tabove
      tabove%below => tail
      head => habove
      tail => tabove
      head%next => tail
      tail%prev => head
    end do
    this%level = level
  end
  function search_node(this, key) result(res)
    type(t_skip_list), intent(in) :: this
    integer(key_kind), intent(in) :: key
    type(t_node), pointer :: res
    res => this%head
    do while (associated(res%above))
      res => res%above
    end do
    do
      do while (key >= res%next%key)
        res => res%next
      end do
      if (.not.associated(res%below)) exit
      res => res%below
    end do
  end
  integer(val_kind) function search(this, key) result(res)
    class(t_skip_list), intent(in) :: this
    integer(key_kind), intent(in) :: key
    type(t_node), pointer :: node
    node => search_node(this, key)
    res = merge(node%val, this%default, node%key == key)
  end
  logical function contain(this, key) result(res)
    class(t_skip_list), intent(in) :: this
    integer(key_kind), intent(in) :: key
    type(t_node), pointer :: node
    node => search_node(this, key)
    res = node%key == key
  end
  subroutine insert(this, key, val)
    class(t_skip_list), intent(inout) :: this
    integer(key_kind), intent(in) :: key
    integer(val_kind), intent(in) :: val
    type(t_node), pointer :: node, prev, next, above
    integer :: i, level, length, prevlength
    prev => search_node(this, key)
    if (prev%key == key) then
      prev%val = val
      return
    end if
    this%size = this%size + 1
    node => new_node(key, val, 1)
    next => prev%next
    prev%next => node
    node%prev => prev
    node%next => next
    next%prev => node
    level = 0
    do while (flip_coin())
      level = level + 1
    end do
    call increase_level(this, level)
    prevlength = 1
    length = 1
    do i = 1, level
      do while (.not.associated(prev%above))
        prev => prev%prev
        prevlength = prevlength + prev%length
      end do
      prev => prev%above
      prev%length = prevlength
      do while (.not.associated(next%above))
        length = length + next%length
        next => next%next
      end do
      next => next%above
      above => new_node(key, val, length)
      above%below => node
      node%above => above
      node => above
      prev%next => node
      node%prev => prev
      node%next => next
      next%prev => node
    end do
    do i = level + 1, this%level
      do while (.not.associated(prev%above))
        prev => prev%prev
      end do
      prev => prev%above
      prev%length = prev%length + 1
    end do
  end
  integer function index_of(this, key) result(res)
    class(t_skip_list), intent(in) :: this
    integer(key_kind), intent(in) :: key
    type(t_node), pointer :: node
    res = 0
    node => this%head
    do while (associated(node%above))
      node => node%above
    end do
    do
      do while (key >= node%next%key)
        res = res + node%length
        node => node%next
      end do
      if (node%key == key) exit
      if (.not.associated(node%below)) exit
      node => node%below
    end do
    if (node%key /= key) res = -(res + 1)
  end
  integer(key_kind) function get_key_at(this, idx) result(res)
    class(t_skip_list), intent(in) :: this
    integer, intent(in) :: idx
    integer :: length
    type(t_node), pointer :: node
    if (idx < 1) then
      res = -infty
      return
    end if
    if (idx > this%size) then
      res = infty
      return
    end if
    length = 0
    node => this%head
    do while (associated(node%above))
      node => node%above
    end do
    do
      do while (length + node%length <= idx)
        length = length + node%length
        node => node%next
      end do
      if (length == idx) exit
      if (.not.associated(node%below)) exit
      node => node%below
    end do
    res = node%key
  end
  subroutine remove(this, key)
    class(t_skip_list), intent(inout) :: this
    integer(key_kind), intent(in) :: key
    type(t_node), pointer :: node, prev, next, above
    integer :: i, level
    node => search_node(this, key)
    if (node%key /= key) return
    this%size = this%size - 1
    level = 0
    prev => node%prev
    next => node%next
    prev%next => next
    next%prev => prev
    do
      above => node%above
      deallocate(node)
      node => above
      level = level + 1
      if (.not.associated(node)) exit
      do while (.not.associated(prev%above))
        prev => prev%prev
      end do
      prev => prev%above
      prev%length = prev%length + node%length - 1
      next => node%next
      prev%next => next
      next%prev => prev
    end do
    do i = level, this%level
      do while (.not.associated(prev%above))
        prev => prev%prev
      end do
      prev => prev%above
      prev%length = prev%length - 1
    end do
  end
  subroutine remove_key_at(this, idx)
    class(t_skip_list), intent(inout) :: this
    integer, intent(in) :: idx
    integer :: key
    if (idx < 1 .or. idx > this%size) return
    key = get_key_at(this, idx)
    call remove(this, key)
  end
  integer(key_kind) function first_key(this) result(res)
    class(t_skip_list), intent(in) :: this
    res = merge(-infty, this%head%next%key, this%size == 0)
  end
  integer(key_kind) function poll_first(this) result(res)
    class(t_skip_list), intent(inout) :: this
    type(t_node), pointer :: node
    res = merge(-infty, this%head%next%key, this%size == 0)
    if (this%size > 0) call remove(this, res)
  end
  integer(key_kind) function last_key(this) result(res)
    class(t_skip_list), intent(in) :: this
    res = merge(infty, this%tail%prev%key, this%size == 0)
  end
  integer(key_kind) function poll_last(this) result(res)
    class(t_skip_list), intent(inout) :: this
    res = merge(infty, this%tail%prev%key, this%size == 0)
    if (this%size > 0) call remove(this, res)
  end
  integer(key_kind) function floor_key(this, key) result(res)
    class(t_skip_list), intent(in) :: this
    integer(key_kind), intent(in) :: key
    type(t_node), pointer :: node
    node => search_node(this, key)
    res = node%key
  end
  integer(key_kind) function lower_key(this, key) result(res)
    class(t_skip_list), intent(in) :: this
    integer(key_kind), intent(in) :: key
    type(t_node), pointer :: node
    node => search_node(this, key)
    res = merge(node%prev%key, node%key, node%key == key)
  end
  integer(key_kind) function ceiling_key(this, key) result(res)
    class(t_skip_list), intent(in) :: this
    integer(key_kind), intent(in) :: key
    type(t_node), pointer :: node
    node => search_node(this, key)
    res = merge(node%key, node%next%key, node%key == key)
  end
  integer(key_kind) function higher_key(this, key) result(res)
    class(t_skip_list), intent(in) :: this
    integer(key_kind), intent(in) :: key
    type(t_node), pointer :: node
    node => search_node(this, key)
    res = node%next%key
  end
end module mod_skip_list
program i_hate_shortest_path_problem
  use mod_skip_list
  implicit none
  integer :: h, w, a, b, c, d, e, key, val, i
  type(t_skip_list) :: map, cnt
  map = skip_list()
  cnt = skip_list()
  read(*,*) h, w
  do i = 1, w
    call map%insert(i, i)
  end do
  call cnt%insert(0, w)
  do i = 1, h
    read(*,*) a, b
    key = map%ceiling_key(a)
    b = b + 1
    c = -1
    do while (key <= b)
      val = map%search(key)
      c = max(c, val)
      d = key - val
      e = cnt%search(d) - 1
      if (e == 0) then
        call cnt%remove(d)
      else
        call cnt%insert(d, e)
      end if
      call map%remove(key)
      key = map%ceiling_key(key)
    end do
    if (c > 0 .and. b <= w) then
      call map%insert(b, c)
      d = b - c
      call cnt%insert(d, cnt%search(d) + 1)
    end if
    write(*,'(i0)') merge(cnt%first_key() + i, -1, cnt%size > 0)
  end do
end program i_hate_shortest_path_problem

Score : 600 points

Problem Statement

There is a grid of squares with H+1 horizontal rows and W vertical columns.

You will start at one of the squares in the top row and repeat moving one square right or down. However, for each integer i from 1 through H, you cannot move down from the A_i-th, (A_i + 1)-th, \ldots, B_i-th squares from the left in the i-th row from the top.

For each integer k from 1 through H, find the minimum number of moves needed to reach one of the squares in the (k+1)-th row from the top. (The starting square can be chosen individually for each case.) If, starting from any square in the top row, none of the squares in the (k+1)-th row can be reached, print -1 instead.

Constraints

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

1 \leq A_i \leq B_i \leq W

Input

Input is given from Standard Input in the following format:

H W
A_1 B_1
A_2 B_2
:
A_H B_H

Output

Print H lines. The i-th line should contain the answer for the case k=i.

Sample Input 1

4 4
2 4
1 1
2 3
2 4

Sample Output 1

1
3
6
-1

Let (i,j) denote the square at the i-th row from the top and j-th column from the left.

For k=1, we need one move such as (1,1) → (2,1).

For k=2, we need three moves such as (1,1) → (2,1) → (2,2) → (3,2).

For k=3, we need six moves such as (1,1) → (2,1) → (2,2) → (3,2) → (3,3) → (3,4) → (4,4).

For k=4, it is impossible to reach any square in the fifth row from the top.


program i_hate_shortest_path_problem
  implicit none
  integer :: h, w, a, b, x = 1, i
  read(*,*) h, w
  do i = 1, h
    read(*,*) a, b
    if (a <= x .and. x <= b) then
      x = b + 1
    end if
    write(*,'(i0)') merge(-1, x + i - 1, x > w)
  end do
end program i_hate_shortest_path_problem


program step
	implicit none
    integer(8):: N
    integer(8):: step_size(200000), height(200000)
    integer(8):: i, h1, h2

	read(*,*) N
    read(*,*) (height(i),i=1,N)
    step_size(1:N)=0

	do i = 1, N-1
    	h1 = height(i)+step_size(i)
        h2 = height(i+1)+step_size(i+1)
    	if (h1>h2) then
        	step_size(i+1)=step_size(i+1)+h1-h2
        end if
    end do
    write(*,'(i0)') sum(step_size(1:N))
end program

Score : 300 points

Problem Statement

N persons are standing in a row. The height of the i-th person from the front is A_i.

We want to have each person stand on a stool of some heights - at least zero - so that the following condition is satisfied for every person:

Condition: Nobody in front of the person is taller than the person. Here, the height of a person includes the stool.

Find the minimum total height of the stools needed to meet this goal.

Constraints

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

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 \ldots A_N

Output

Print the minimum total height of the stools needed to meet the goal.

Sample Input 1

5
2 1 5 4 3

Sample Output 1

4

If the persons stand on stools of heights 0, 1, 0, 1, and 2, respectively, their heights will be 2, 2, 5, 5, and 5, satisfying the condition.

We cannot meet the goal with a smaller total height of the stools.

Sample Input 2

5
3 3 3 3 3

Sample Output 2

0

Giving a stool of height 0 to everyone will work.


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

    read(*,*) n
    allocate(x(n))
    read(*,*)x
    m=0
    do i=1,n
        if(x(i)>m)then
            m=x(i)
        elseif(x(i)<m)then
            ans=ans+m-x(i)
        endif
    end do
	write(*,*)ans
    stop
end program sample


module mod_weighted_graph
  implicit none
  integer(8), parameter :: long_infty = lshift(1_8, 60)
  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
    procedure :: prim => prim
  end type

  type t_node
    type(t_node), private, pointer :: left => null(), right => null()
    integer :: rank = 1
    integer :: vertex, from
    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(vertex, cost) result(res)
    integer, intent(in) :: vertex
    integer(8), intent(in) :: cost
    type(t_node), pointer :: res
    res => null()
    allocate(res)
    res%vertex = vertex
    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, vertex, cost, from)
    class(leftist_heap), intent(inout) :: this
    integer, intent(in) :: vertex
    integer(8), intent(in) :: cost
    integer, intent(in), optional :: from
    type(t_node), pointer :: node
    node => newn0(vertex, cost)
    if (present(from)) node%from = from
    this%root => meld_node(this%root, node)
    this%size = this%size + 1
  end

  function dijkstra(this, start) result(res)
    class(weighted_graph), intent(in) :: this
    integer, intent(in) :: start
    integer :: i, v
    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)
      v = node%vertex
      cost = node%cost
      deallocate(node)
      if (cost > res(v)) cycle
      do i = 1, size(this%list(v)%at)
        e = this%list(v)%at(i)
        if (res(e%to) > res(v) + e%weight) then
          res(e%to) = res(v) + 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

  type(weighted_graph) function prim(this) result(res)
    class(weighted_graph), intent(in) :: this
    logical :: used(size(this%list))
    integer :: i, v, from, a(size(this%list) - 1), b(size(this%list) - 1), cnt
    integer(8) :: cost, weight(size(this%list) - 1)
    type(t_node), pointer :: node
    type(leftist_heap) :: pq
    type(t_edge) :: e
    do i = 1, size(this%list(1)%at)
      e = this%list(1)%at(i)
      call offer(pq, e%to, e%weight, 1)
    end do
    used = .false.
    used(1) = .true.
    cnt = 0
    do while (pq%size > 0)
      node => poll(pq)
      v = node%vertex
      from = node%from
      cost = node%cost
      deallocate(node)
      if (used(v)) cycle
      used(v) = .true.
      cnt = cnt + 1
      a(cnt) = from
      b(cnt) = v
      weight(cnt) = cost
      do i = 1, size(this%list(v)%at)
        e = this%list(v)%at(i)
        if (used(e%to)) cycle
        call offer(pq, e%to, e%weight, v)
      end do
    end do
    res = newwg0(size(this%list), size(this%list) - 1, a, b, weight)
  end

end module mod_weighted_graph
program wizard_in_maze
  use mod_weighted_graph
  implicit none
  integer :: h, w, xs, ys, xg, yg, i, j, k, n = 0, cnt = 0
  integer :: g(-10:1010, -10:1010) = -1
  integer :: dx(4) = (/0, 1, 0, -1/)
  integer :: dy(4) = (/1, 0, -1, 0/)
  character(1000) :: s = ''
  logical, allocatable :: u(:), v(:)
  logical :: z(0:1001, 0:1001) = .false.
  integer, allocatable :: a(:), b(:)
  integer(8), allocatable :: d(:)
  type(weighted_graph) :: graph
  read(*,*) h, w
  read(*,*) xs, ys
  read(*,*) xg, yg
  g(1:h, 1:w) = 0
  do i = 1, h
    read(*,*) s
    do j = 1, w
      if (s(j:j) == '#') g(i, j) = -1
    end do
  end do
  do i = 1, h
    do j = 1, w
      if (g(i, j) == 0) then
        n = n + 1
        call dfs1(i, j, n)
      end if
    end do
  end do
  allocate(u(n), v(n))
  v = .false.
  do i = 1, h
    do j = 1, w
      if (g(i, j) < 0) cycle
      if (v(g(i, j))) cycle
      v(g(i, j)) = .true.
      u = .false.
      call dfs2(i, j)
      do k = 1, n
        if (k /= g(i, j) .and. u(k)) cnt = cnt + 1
      end do
    end do
  end do
  allocate(a(cnt), b(cnt), d(max(n, cnt)))
  cnt = 0
  v = .false.
  do i = 1, h
    do j = 1, w
      if (g(i, j) < 0) cycle
      if (v(g(i, j))) cycle
      v(g(i, j)) = .true.
      u = .false.
      call dfs2(i, j)
      do k = 1, n
        if (k /= g(i, j) .and. u(k)) then
          cnt = cnt + 1
          a(cnt) = g(i, j)
          b(cnt) = k
        end if
      end do
    end do
  end do
  d = 1_8
  graph = weighted_graph(n, cnt, a, b, d, undirected = .true.)
  d(1:n) = graph%dijkstra(g(xs, ys))
  write(*,'(i0)') merge(-1_8, d(g(xg, yg)), d(g(xg, yg)) == long_infty)
contains
  recursive subroutine dfs1(x0, y0, n)
    integer, intent(in) :: x0, y0, n
    integer :: x, y, i
    g(x0, y0) = n
    do i = 1, 4
      x = x0 + dx(i)
      y = y0 + dy(i)
      if (g(x, y) == 0) call dfs1(x, y, n)
    end do
  end
  recursive subroutine dfs2(x0, y0)
    integer, intent(in) :: x0, y0
    integer :: x, y, i
    z(x0, y0) = .true.
    do x = x0 - 2, x0 + 2
      do y = y0 - 2, y0 + 2
        if (g(x, y) > 0) u(g(x, y)) = .true.
      end do
    end do
    do i = 1, 4
      x = x0 + dx(i)
      y = y0 + dy(i)
      if (.not.z(x, y) .and. g(x, y) > 0) call dfs2(x, y)
    end do
  end
end program wizard_in_maze

Score : 400 points

Problem Statement

A maze is composed of a grid of H \times W squares - H vertical, W horizontal.

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

There is a magician in (C_h,C_w). He can do the following two kinds of moves:

Move A: Walk to a road square that is vertically or horizontally adjacent to the square he is currently in.

Move B: Use magic to warp himself to a road square in the 5\times 5 area centered at the square he is currently in.

In either case, he cannot go out of the maze.

At least how many times does he need to use the magic to reach (D_h, D_w)?

Constraints

1 \leq H,W \leq 10^3

1 \leq C_h,D_h \leq H

1 \leq C_w,D_w \leq W

S_{ij} is # or ..

S_{C_h C_w} and S_{D_h D_w} are ..

(C_h,C_w) \neq (D_h,D_w)

Input

Input is given from Standard Input in the following format:

H W
C_h C_w
D_h D_w
S_{11}\ldots S_{1W}
\vdots
S_{H1}\ldots S_{HW}

Output

Print the minimum number of times the magician needs to use the magic. If he cannot reach (D_h,D_w), print -1 instead.

Sample Input 1

4 4
1 1
4 4
..#.
..#.
.#..
.#..

Sample Output 1

1

For example, by walking to (2,2) and then using the magic to travel to (4,4), just one use of magic is enough.

Note that he cannot walk diagonally.

Sample Input 2

4 4
1 4
4 1
.##.
####
####
.##.

Sample Output 2

-1

He cannot move from there.

Sample Input 3

4 4
2 2
3 3
....
....
....
....

Sample Output 3

0

No use of magic is needed.

Sample Input 4

4 5
1 2
2 5
#.###
####.
#..##
#..##

Sample Output 4

2


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
    procedure :: prim => prim
  end type

  type t_node
    type(t_node), private, pointer :: left => null(), right => null()
    integer :: rank = 1
    integer :: vertex, from
    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(vertex, cost) result(res)
    integer, intent(in) :: vertex
    integer(8), intent(in) :: cost
    type(t_node), pointer :: res
    res => null()
    allocate(res)
    res%vertex = vertex
    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, vertex, cost, from)
    class(leftist_heap), intent(inout) :: this
    integer, intent(in) :: vertex
    integer(8), intent(in) :: cost
    integer, intent(in), optional :: from
    type(t_node), pointer :: node
    node => newn0(vertex, cost)
    if (present(from)) node%from = from
    this%root => meld_node(this%root, node)
    this%size = this%size + 1
  end

  function dijkstra(this, start) result(res)
    class(weighted_graph), intent(in) :: this
    integer, intent(in) :: start
    integer :: i, v
    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)
      v = node%vertex
      cost = node%cost
      deallocate(node)
      if (cost > res(v)) cycle
      do i = 1, size(this%list(v)%at)
        e = this%list(v)%at(i)
        if (res(e%to) > res(v) + e%weight) then
          res(e%to) = res(v) + 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

  type(weighted_graph) function prim(this) result(res)
    class(weighted_graph), intent(in) :: this
    logical :: used(size(this%list))
    integer :: i, v, from, a(size(this%list) - 1), b(size(this%list) - 1), cnt
    integer(8) :: cost, weight(size(this%list) - 1)
    type(t_node), pointer :: node
    type(leftist_heap) :: pq
    type(t_edge) :: e
    do i = 1, size(this%list(1)%at)
      e = this%list(1)%at(i)
      call offer(pq, e%to, e%weight, 1)
    end do
    used = .false.
    used(1) = .true.
    cnt = 0
    do while (pq%size > 0)
      node => poll(pq)
      v = node%vertex
      from = node%from
      cost = node%cost
      deallocate(node)
      if (used(v)) cycle
      used(v) = .true.
      cnt = cnt + 1
      a(cnt) = from
      b(cnt) = v
      weight(cnt) = cost
      do i = 1, size(this%list(v)%at)
        e = this%list(v)%at(i)
        if (used(e%to)) cycle
        call offer(pq, e%to, e%weight, v)
      end do
    end do
    res = newwg0(size(this%list), size(this%list) - 1, a, b, weight)
  end

end module mod_weighted_graph
program wizard_in_maze
  use mod_weighted_graph
  implicit none
  integer :: h, w, xs, ys, xg, yg, i, j, k, n = 0, cnt = 0
  integer :: g(-10:1010, -10:1010) = -1
  integer :: dx(4) = (/0, 1, 0, -1/)
  integer :: dy(4) = (/1, 0, -1, 0/)
  character(1000) :: s = ''
  logical, allocatable :: u(:), v(:)
  logical :: z(0:1001, 0:1001) = .false.
  integer, allocatable :: a(:), b(:)
  integer(8), allocatable :: d(:)
  type(weighted_graph) :: graph
  read(*,*) h, w
  read(*,*) xs, ys
  read(*,*) xg, yg
  g(1:h, 1:w) = 0
  do i = 1, h
    read(*,*) s
    do j = 1, w
      if (s(j:j) == '#') g(i, j) = -1
    end do
  end do
  do i = 1, h
    do j = 1, w
      if (g(i, j) == 0) then
        n = n + 1
        call dfs1(i, j, n)
      end if
    end do
  end do
  allocate(u(n), v(n))
  v = .false.
  do i = 1, h
    do j = 1, w
      if (g(i, j) < 0) cycle
      if (v(g(i, j))) cycle
      v(g(i, j)) = .true.
      u = .false.
      call dfs2(i, j)
      do k = 1, n
        if (k /= g(i, j) .and. u(k)) cnt = cnt + 1
      end do
    end do
  end do
  allocate(a(cnt), b(cnt), d(max(n, cnt)))
  cnt = 0
  v = .false.
  do i = 1, h
    do j = 1, w
      if (g(i, j) < 0) cycle
      if (v(g(i, j))) cycle
      v(g(i, j)) = .true.
      u = .false.
      call dfs2(i, j)
      do k = 1, n
        if (k /= g(i, j) .and. u(k)) then
          cnt = cnt + 1
          a(cnt) = g(i, j)
          b(cnt) = k
        end if
      end do
    end do
  end do
  d = 1_8
  graph = weighted_graph(n, cnt, a, b, d, undirected = .true.)
  d(1:n) = graph%dijkstra(g(xs, ys))
  write(*,'(i0)') merge(-1_8, d(g(xg, yg)), d(g(xg, yg)) == long_infty)
contains
  recursive subroutine dfs1(x0, y0, n)
    integer, intent(in) :: x0, y0, n
    integer :: x, y, i
    g(x0, y0) = n
    do i = 1, 4
      x = x0 + dx(i)
      y = y0 + dy(i)
      if (g(x, y) == 0) call dfs1(x, y, n)
    end do
  end
  recursive subroutine dfs2(x0, y0)
    integer, intent(in) :: x0, y0
    integer :: x, y, i
    z(x0, y0) = .true.
    do x = x0 - 2, x0 + 2
      do y = y0 - 2, y0 + 2
        if (g(x, y) > 0) u(g(x, y)) = .true.
      end do
    end do
    do i = 1, 4
      x = x0 + dx(i)
      y = y0 + dy(i)
      if (.not.z(x, y) .and. g(x, y) > 0) call dfs2(x, y)
    end do
  end
end program wizard_in_maze


module mod_selection_sort
  implicit none
  integer, private, parameter :: intkind = 4
contains
  logical function less(a, b) result(res)
    integer(intkind), intent(in) :: a, b
    res = a <= b
  end
  subroutine swap(a, b)
    integer(intkind), intent(inout) :: a, b
    a = xor(a, b)
    b = xor(a, b)
    a = xor(a, b)
  end
  subroutine selection_sort(a)
    integer(intkind), intent(inout) :: a(:)
    integer :: n, i, j, m
    n = size(a)
    do i = 1, n - 1
      m = i
      do j = i + 1, n
        if (.not.less(a(m), a(j))) m = j
      end do
      if (i /= m) call swap(a(i), a(m))
    end do
  end
end module mod_selection_sort
program brave_chain
  use mod_selection_sort
  implicit none
  integer, parameter :: inf = 1e9
  integer :: n, a(6000) = 0, dp(2000, 2000) = -inf, m = 0, b(3) = 0, v, i, j
  integer, dimension(2000) :: dp0 = 0, mv = -inf, mv0 = 0
  read(*,*) n
  read(*,*) a(1:3 * n)
  call update(a(1), a(2), 0)
  do i = 3, 3 * n - 2, 3
    if (a(i) == a(i + 1) .and. a(i) == a(i + 2)) then
      m = m + 1
      cycle
    end if
    call selection_sort(a(i:i + 2))
    if (a(i + 1) == a(i + 2)) call swap(a(i), a(i + 2))
    do j = 1, n
      dp0(j) = dp(min(a(i), j), max(a(i), j))
    end do
    mv0(1:n) = mv(1:n)
    v = max(0, maxval(mv(1:n)))
    do j = 1, 3
      b(j) = dp(a(i + j - 1), a(i + j - 1))
    end do
    if (a(i) == a(i + 1)) then
      do j = 1, n
        call update(a(i + 2), j, dp0(j) + 1)
      end do
    end if
    call update(a(i + 1), a(i + 2), b(1) + 1)
    call update(a(i), a(i + 2), b(2) + 1)
    call update(a(i), a(i + 1), b(3) + 1)
    call update(a(i + 1), a(i + 2), v)
    call update(a(i), a(i + 2), v)
    call update(a(i), a(i + 1), v)
    do j = 1, n
      call update(a(i), j, mv0(j))
      call update(a(i + 1), j, mv0(j))
      call update(a(i + 2), j, mv0(j))
    end do
  end do
  i = a(3 * n)
  write(*,'(i0)') max(dp(i, i) + 1, maxval(mv(1:n))) + m
contains
  subroutine update(a, b, z)
    integer, intent(in) :: a, b, z
    integer :: x, y
    if (a > b) then
      x = b
      y = a
    else
      x = a
      y = b
    end if
    if (dp(x, y) >= z) return
    dp(x, y) = z
    mv(x) = max(mv(x), z)
    mv(y) = max(mv(y), z)
  end
end program brave_chain

Score : 600 points

Problem Statement

We have 3N cards arranged in a row from left to right, where each card has an integer between 1 and N (inclusive) written on it. The integer written on the i-th card from the left is A_i.

You will do the following operation N-1 times:

Rearrange the five leftmost cards in any order you like, then remove the three leftmost cards. If the integers written on those three cards are all equal, you gain 1 point.

After these N-1 operations, if the integers written on the remaining three cards are all equal, you will gain 1 additional point.

Find the maximum number of points you can gain.

Constraints

1 \leq N \leq 2000

1 \leq A_i \leq N

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \cdots A_{3N}

Output

Print the maximum number of points you can gain.

Sample Input 1

2
1 2 1 2 2 1

Sample Output 1

2

Let us rearrange the five leftmost cards so that the integers written on the six cards will be 2\ 2\ 2\ 1\ 1\ 1 from left to right.

Then, remove the three leftmost cards, all of which have the same integer 2, gaining 1 point.

Now, the integers written on the remaining cards are 1\ 1\ 1.

Since these three cards have the same integer 1, we gain 1 more point.

In this way, we can gain 2 points - which is the maximum possible.

Sample Input 2

3
1 1 2 2 3 3 3 2 1

Sample Output 2

1

Sample Input 3

3
1 1 2 2 2 3 3 3 1

Sample Output 3

3


module mod_selection_sort
  implicit none
  integer, private, parameter :: intkind = 4
contains
  logical function less(a, b) result(res)
    integer(intkind), intent(in) :: a, b
    res = a <= b
  end
  subroutine swap(a, b)
    integer(intkind), intent(inout) :: a, b
    a = xor(a, b)
    b = xor(a, b)
    a = xor(a, b)
  end
  subroutine selection_sort(a)
    integer(intkind), intent(inout) :: a(:)
    integer :: n, i, j, m
    n = size(a)
    do i = 1, n - 1
      m = i
      do j = i + 1, n
        if (.not.less(a(m), a(j))) m = j
      end do
      if (i /= m) call swap(a(i), a(m))
    end do
  end
end module mod_selection_sort
program brave_chain
  use mod_selection_sort
  implicit none
  integer, parameter :: inf = 1e9
  integer :: n, a(6000) = 0, t, dp(2000, 2000) = -inf, m = 0, b(3) = 0, v, i, j
  integer, dimension(2000) :: dp0 = 0, mv = -inf, mv0 = 0
  read(*,*) n
  t = 3 * n
  read(*,*) a(1:t)
  call update(a(1), a(2), 0)
  do i = 3, t - 2, 3
    if (a(i) == a(i + 1) .and. a(i) == a(i + 2)) then
      m = m + 1
      cycle
    end if
    call selection_sort(a(i:i + 2))
    if (a(i + 1) == a(i + 2)) call swap(a(i), a(i + 2))
    do j = 1, n
      dp0(j) = dp(min(a(i), j), max(a(i), j))
    end do
    mv0(1:n) = mv(1:n)
    v = max(0, maxval(mv(1:n)))
    do j = 1, 3
      b(j) = dp(a(i + j - 1), a(i + j - 1))
    end do
    if (a(i) == a(i + 1)) then
      do j = 1, n
        call update(a(i + 2), j, dp0(j) + 1)
      end do
    end if
    call update(a(i + 1), a(i + 2), b(1) + 1)
    call update(a(i), a(i + 2), b(2) + 1)
    call update(a(i), a(i + 1), b(3) + 1)
    call update(a(i + 1), a(i + 2), v)
    call update(a(i), a(i + 2), v)
    call update(a(i), a(i + 1), v)
    do j = 1, n
      call update(a(i), j, mv0(j))
      call update(a(i + 1), j, mv0(j))
      call update(a(i + 2), j, mv0(j))
    end do
  end do
  write(*,'(i0)') max(dp(a(t), a(t)) + 1, maxval(mv(1:n))) + m
contains
  subroutine update(a, b, z)
    integer, intent(in) :: a, b, z
    integer :: x, y
    if (a > b) then
      x = b
      y = a
    else
      x = a
      y = b
    end if
    if (dp(x, y) >= z) return
    dp(x, y) = z
    mv(x) = max(mv(x), z)
    mv(y) = max(mv(y), z)
  end
end program brave_chain


program main
  implicit none
  integer :: n, i, j, k, counts
  integer, allocatable :: l(:)

  read(*,*) n

  allocate( l(n) )
  read(*,*) ( l(i), i = 1, n )

  counts = 0

  do i = 1, n-2
    do j = i+1, n-1
      do k = j+2, n
        if ( l(i)+l(j) > l(k) .and. l(j)+l(k) > l(i) .and. l(k)+l(i) > l(j) ) then
          if( l(i) == l(j) .or. l(i) == l(k) .or. l(j) == l(k) ) then
            counts = counts
          else
            counts = counts + 1
          endif
        else
          counts = counts
        endif
      enddo
    enddo
  enddo

  write(*,*) counts

end program main

Score : 100 points

Problem Statement

We have weather records at AtCoder Town for some consecutive three days. A string of length 3, S, represents the records - if the i-th character is S, it means it was sunny on the i-th day; if that character is R, it means it was rainy on that day.

Find the maximum number of consecutive rainy days in this period.

Constraints

|S| = 3

Each character of S is S or R.

Input

Input is given from Standard Input in the following format:

S

Output

Print the maximum number of consecutive rainy days in the period.

Sample Input 1

RRS

Sample Output 1

2

We had rain on the 1-st and 2-nd days in the period. Here, the maximum number of consecutive rainy days is 2, so we should print 2.

Sample Input 2

SSS

Sample Output 2

0

It was sunny throughout the period. We had no rainy days, so we should print 0.

Sample Input 3

RSR

Sample Output 3

1

We had rain on the 1-st and 3-rd days - two "streaks" of one rainy day, so we should print 1.


PROGRAM A
  IMPLICIT NONE
  CHARACTER(len=3) :: S
  CHARACTER(len=1) :: w1,w2,w3
  INTEGER :: i=0

  READ *, S
  w1 = S(1:1)
  w2 = S(2:2)
  w3 = S(3:3)

  IF (w1=="R") THEN
    i=i+1
    IF (w2==w1) THEN
      i=i+1
      IF (w3==w2) THEN
        i=i+1
      ELSE
      END IF
    ELSE
    END IF
  ELSE
    IF (w2=="R") THEN
      i=i+1
      IF (w3==w2) THEN
        i=i+1
      ELSE
      END IF
    ELSE
      IF (w3=="R") THEN
        i=i+1
      ELSE
      END IF
    END IF
  END IF

  PRINT *, i
END PROGRAM A


      program RainySeason
!variable
      implicit none
      character(len=3) :: S
      integer :: n_rainy
      integer :: n_max
      integer :: i
!read
      read(*,*) S
!
      n_rainy = 0
      n_max = 0
      do i=1,3
        if(S(i:i) == 'R') n_rainy = n_rainy + 1
        if(S(i:i) == 'S') then
          if(n_max < n_rainy) n_max = n_rainy
          n_rainy = 0
        end if
      end do
!
      write(*,*) n_max
!
      end program RainySeason


      program MakingTriangle
!
      implicit none
      integer(8) :: i,j,k
      integer(8) :: N
      integer(8),allocatable,dimension(:) :: L
      integer(8) :: ans
!
      read(*,*) N
      allocate(L(N))
      read(*,*) (L(i),i=1,N)
      i=1
      call quicksort(N,L,i,N)
      ans = 0
!
      do i=1,N-2
        do j=i+1,N-1
          if(L(i) /= L(j) ) then
            do k=j+1,N
              if(L(j) /= L(k)) then
              if(L(i)+L(j) > L(k)) ans = ans+1
              end if
            end do
          end if
        end do
      end do
!
      write(*,*) ans
!
      end program MakingTriangle
!-------------------------------------------------
      recursive subroutine quicksort(n,x,l,r)
!-------------------------------------------------
! Variables I/O
!-------------------------------------------------
! n     ...  the number of data (IN) : integer
! x(i)  ...  data (INOUT) : integer
! l     ...  the smallest label in a sorted region
!            (compute label)
! r     ...  the largest label in a sorted region
!            (compute label)
!-------------------------------------------------
! Variables (The others)
!-------------------------------------------------
! c     ...  the average of l and r : (l+r)/2
! ls    ...  the smallest label in a sorted region
!            (Input label)
! i     ...  the largest label in a sorted region
!            (Input label)
!-------------------------------------------------
! Contain
!-------------------------------------------------
! Subroutine : swap(a,b) : swaping a for b
!-------------------------------------------------
      implicit none
      integer(8),intent(in) :: n,l,r
      integer(8),dimension(n),intent(inout) :: x
      integer(8) :: c,ls,i
!
      if(l < r) then
        c = (l+r) / 2 ! representative value
        call swap(x(l),x(c))
        ls = l
        do i=l+1,r
          if(x(i) < x(l)) then
            ls = ls + 1
            call swap(x(ls),x(i))
          end if
        end do
        call swap(x(ls),x(l))
        call quicksort(n,x,l,ls-1)
        call quicksort(n,x,ls+1,r)
      end if
      contains
!-------------------------------------------------
      subroutine swap(a,b)
!-------------------------------------------------
        integer(8),intent(inout) :: a,b
        integer(8) :: w
        w=a
        a=b
        b=w
      end subroutine swap
      end subroutine quicksort
!

Score : 200 points

Problem Statement

We have sticks numbered 1, \cdots, N. The length of Stick i (1 \leq i \leq N) is L_i.

In how many ways can we choose three of the sticks with different lengths that can form a triangle?

That is, find the number of triples of integers (i, j, k) (1 \leq i < j < k \leq N) that satisfy both of the following conditions:

L_i, L_j, and L_k are all different.

There exists a triangle whose sides have lengths L_i, L_j, and L_k.

Constraints

1 \leq N \leq 100

1 \leq L_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
L_1 L_2 \cdots L_N

Output

Print the number of ways to choose three of the sticks with different lengths that can form a triangle.

Sample Input 1

5
4 4 9 7 5

Sample Output 1

5

The following five triples (i, j, k) satisfy the conditions: (1, 3, 4), (1, 4, 5), (2, 3, 4), (2, 4, 5), and (3, 4, 5).

Sample Input 2

6
4 5 4 3 3 5

Sample Output 2

8

We have two sticks for each of the lengths 3, 4, and 5. To satisfy the first condition, we have to choose one from each length.

There is a triangle whose sides have lengths 3, 4, and 5, so we have 2 ^ 3 = 8 triples (i, j, k) that satisfy the conditions.

Sample Input 3

10
9 4 6 1 9 6 10 6 6 8

Sample Output 3

39

Sample Input 4

2
1 1

Sample Output 4

0

No triple (i, j, k) satisfies 1 \leq i < j < k \leq N, so we should print 0.


program MakingTriangle
    implicit none

    integer(4) :: N
    real(8), allocatable :: L(:)
    integer(4) :: count
    integer(4) :: i, j, k

    read(*, *) N
    allocate(L(1:N))
    read(*, *) L

    count = 0
    do i = 1, N
        do j = i + 1, N
            do k = j + 1, N
                if (L(i) .ne. L(j) .and. L(i) .ne. L(k) .and. L(j) .ne. L(k) .and. &
                &   abs(L(i)-L(j)) < L(k) .and. L(k) < L(i) + L(j)) then
                    count  = count + 1
                end if
            end do
        end do
    end do

    write(*, *) count

end program MakingTriangle


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

    read(*,*) n
    allocate(x(n))
    read(*,*)x
    call msort(x)
    m=0
    do i=1,n-2
        do j=i+1,n-1
            do k=j+1,n
                if(x(i)+x(j)>x(k) .and. x(i)/=x(j) .and. x(j)/=x(k))then
                    m=m+1
                end if
            end do
        end do
    end do
    write(*,*)m

    stop
    contains
    recursive subroutine msort(x)
    implicit none
    integer(8), intent(inout) :: x(:)

    integer(8) :: n, mid, i, j, k
    integer(8), allocatable :: tmp(:)

    n = size(x)

    if(n == 1) then
      return
    end if

    !
    ! 前半と後半に分けてソート
    ! 1~midとmid+1~nをそれぞれソート済みにする
    !
    mid = n / 2
    call msort(x(:mid))
    call msort(x(mid+1:))

    !
    ! tmpという配列にxを一時的に保管
    ! マージソートは外部ソート
    !
    allocate(tmp(n))
    tmp = x

    ! tmpの後半部分を左右反転
    do k = n, mid+1, -1
      tmp(k) = x(mid + n - k + 1)
    end do

    ! iは前半のリストのうちまだ見ていないものの最小値
    ! jは後半のリストのうちまだ見ていないものの最小値
    ! kは見た回数の合計
    i = 1
    j = n

    do k = 1, n
      !
      ! 一番左と一番右のうち小さい方をxに入れていく
      ! ここが等号つき不等号なので、安定ソートとなる
      !
      if(tmp(i) <= tmp(j)) then
        x(k) = tmp(i)
        i = i + 1
      else
        x(k) = tmp(j)
        j = j - 1
      end if
    end do

    deallocate(tmp)
    return
  end subroutine msort
end program sample


program answer
  implicit none
  integer(8) :: i, x, k, d, count
  read(*,*) X, K, D

if(x==0) then
     if(mod(k,2)==0) then
        write(*,*) D
        stop
     else
        write(*,*) '0'
        stop
     end if
  end if

  if(x<0) then
     if(x+k*d>=0) then
        count=0
        do
           if(x>=0) then
              exit
           end if
           x=x+d
           count=count+1
        end do
        if(mod(count,2)==0) then
           if(mod(k,2)==0) then
              write(*,*) min(abs(x),abs(x-2*d),abs(x+2*d))
              stop
           else
              write(*,*) min(abs(x-d),abs(x+d))
              stop
           end if
        else
           if(mod(k,2)==0) then
              write(*,*) min(abs(x-d),abs(x+d))
              stop
           else
              write(*,*) min(abs(x),abs(x-2*d),abs(x+2*d))
              stop
           end if
        end if
      else
           write(*,*) abs(x+k*d)
           stop
     end if
  else
     if(x-k*d<=0) then
        count=0
        do
           if(x<=0) then
              exit
           end if
           x=x-d
           count=count+1
        end do
        if(mod(count,2)==0) then
           if(mod(k,2)==0) then
              write(*,*) min(abs(x),abs(x+2*d),abs(x-2*d))
              stop
           else
              write(*,*) min(abs(x+d),abs(x-d))
              stop
           end if
        else
           if(mod(k,2)==0) then
              write(*,*) min(abs(x-d),abs(x+d))
              stop
           else
              write(*,*) min(abs(x),abs(x+2*d),abs(x-2*d))
              stop
           end if
        end if
     else
        write(*,*) x-k*d
        stop
     end if
  end if

  stop
 end program answer

Score : 300 points

Problem Statement

Takahashi, who lives on the number line, is now at coordinate X. He will make exactly K moves of distance D in the positive or negative direction.

More specifically, in one move, he can go from coordinate x to x + D or x - D.

He wants to make K moves so that the absolute value of the coordinate of the destination will be the smallest possible.

Find the minimum possible absolute value of the coordinate of the destination.

Constraints

-10^{15} \leq X \leq 10^{15}

1 \leq K \leq 10^{15}

1 \leq D \leq 10^{15}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X K D

Output

Print the minimum possible absolute value of the coordinate of the destination.

Sample Input 1

6 2 4

Sample Output 1

2

Takahashi is now at coordinate 6. It is optimal to make the following moves:

Move from coordinate 6 to (6 - 4 =) 2.

Move from coordinate 2 to (2 - 4 =) -2.

Here, the absolute value of the coordinate of the destination is 2, and we cannot make it smaller.

Sample Input 2

7 4 3

Sample Output 2

1

Takahashi is now at coordinate 7. It is optimal to make, for example, the following moves:

Move from coordinate 7 to 4.

Move from coordinate 4 to 7.

Move from coordinate 7 to 4.

Move from coordinate 4 to 1.

Here, the absolute value of the coordinate of the destination is 1, and we cannot make it smaller.

Sample Input 3

10 1 2

Sample Output 3

8

Sample Input 4

1000000000000000 1000000000000000 1000000000000000

Sample Output 4

1000000000000000

The answer can be enormous.