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

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

        ret = x
        do while(ret /= uf%parent(ret))
            ret = uf%parent(ret)
        end do
    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

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

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).

Submitted source-code file size, in bytes.

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

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


program main
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: n,m,i,a,b,cnt=0
    integer(int32), allocatable:: ar(:)

    read*, n,m
    allocate(ar, source=[(i,i=1,n)])

    do i=1,m
        read*, a,b
        ar(b) = p(a)
    end do
    do i=1,n
        ar(i) = p(ar(i))
    end do

    do i=2,n
        if (ar(i-1) /= ar(i)) cnt=cnt+1
    end do
    print'(i0)', cnt
contains
recursive function p(a) result(ret)
    integer(int32):: a,ret

    if (ar(a) == a) then
        ret = a
    else
        ret = p(ar(a))
    end if
end function
end program main


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

end program main


program answer
  implicit none
  integer(8) :: i, j, N, K, ans, f, tmp, b, c
  integer(8), allocatable :: A(:), han(:), se(:)
  read(*,*) N, K
  allocate(A(N),han(N),se(N))
  do i = 1, N
     read(*,*) A(i)
  end do
  ans=1
  do i = 1, N-1
     if(abs(A(i)-A(i+1))<=K) then
        f=i
        exit
     end if
  end do

  ans=1
  han=0
  do i = 1, N-1
     se=0
     if(han(i)/=0) then
        cycle
     end if
     tmp=1
     b=A(i)
     j=1
     do
        if(i+j>=N+1) then
           exit
        end if
        if(han(i+j)/=0) then
           tmp=max(tmp,tmp+han(i+j))
           exit
        else
           if(abs(A(i+j)-b)<=K) then
              se(i)=tmp
              tmp=tmp+1
              b=A(i+j)
              j=j+1
           else
              j=j+1
           end if
        end if
     end do
     han(i)=tmp-se(i)
     ans=max(ans,tmp)
  end do

  write(*,*) ans
  stop
 end program answer

Score : 400 points

Problem Statement

You are given a sequence A_1, A_2, ..., A_N and an integer K.

Print the maximum possible length of a sequence B that satisfies the following conditions:

B is a (not necessarily continuous) subsequence of A.

For each pair of adjacents elements of B, the absolute difference of the elements is at most K.

Constraints

1 \leq N \leq 300,000

0 \leq A_i \leq 300,000

0 \leq K \leq 300,000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
A_1
A_2
:
A_N

Output

Print the answer.

Sample Input 1

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

Sample Output 1

7

For example, B = (1, 4, 3, 6, 9, 7, 4) satisfies the conditions.

It is a subsequence of A = (1, 5, 4, 3, 8, 6, 9, 7, 2, 4).

All of the absolute differences between two adjacent elements (|1-4|, |4-3|, |3-6|, |6-9|, |9-7|, |7-4|) are at most K = 3.


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

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.


program abc67
 implicit none
 integer(8)::length
 character(len=1000)::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 piyo
      IMPLICIT NONE
      integer :: n,i
      integer,allocatable :: d1(:),d2(:)
      character(3) :: ans = 'No '

      read*,n
      allocate( d1(n),d2(n) )
      do i = 1,n
        read*,d1(i),d2(i)
      end do

      do i = 2,n-1

        if( d1(i-1)/=d2(i-1) )cycle
        if( d1(i)/=d2(i) )cycle
        if( d1(i+1)/=d2(i+1) )cycle

        ans = 'Yes'

      end do

      print*,ans

      stop
      !debugg

      END PROGRAM

Score : 200 points

Problem Statement

Tak performed the following action N times: rolling two dice.
The result of the i-th roll is D_{i,1} and D_{i,2}.

Check if doublets occurred at least three times in a row.
Specifically, check if there exists at lease one i such that D_{i,1}=D_{i,2}, D_{i+1,1}=D_{i+1,2} and D_{i+2,1}=D_{i+2,2} hold.

Constraints

3 \leq N \leq 100

1\leq D_{i,j} \leq 6

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
D_{1,1} D_{1,2}
\vdots
D_{N,1} D_{N,2}

Output

Print Yes if doublets occurred at least three times in a row. Print No otherwise.

Sample Input 1

5
1 2
6 6
4 4
3 3
3 2

Sample Output 1

Yes

From the second roll to the fourth roll, three doublets occurred in a row.

Sample Input 2

5
1 1
2 2
3 4
5 5
6 6

Sample Output 2

No

Sample Input 3

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

Sample Output 3

Yes


      program CCC
!
      implicit none
      integer :: A,N,M
!
      read(*,*) N
!
      A = 0
!
      do A=1,N-1
        M = N/A + M
        if(mod(N,A) == 0) M = M-1
      end do
!
      write(*,*) M
!
      end program CCC

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 a_times_b_plus_c
  implicit none
  integer :: n, m = 0, i
  read(*,*) n
  do i = 1, n - 1
    m = m + (n - 1) / i
  end do
  write(*,'(i0)') m
end program a_times_b_plus_c


program main
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: n,k,i,j, ans=0
    integer(int64):: md = 998244353
    integer(int64), allocatable:: dp(:),dpsum(:),l(:),r(:)

    read*, n,k
    allocate(dp,source=[(merge(1_8,0_8,i==1),i=1,n)])
    allocate(dpsum(0:n),source=[(merge(1_8,0_8,i==1),i=0,n)])
    allocate(l(k),r(k))

    do i=1,k
        read*, l(i),r(i)
    end do

    do i=2,n
        do j=1,k; block; integer(int64):: ll,rr
            ll = i-r(j); rr=i-l(j)
            if (rr < 1) cycle
            ll = max(ll,1_8)
            dp(i)=mod(dp(i)+dpsum(rr)-dpsum(ll-1),md)
        end block; end do
        ! print'(*(i0,1x))', dp(:)
        dpsum(i)=dpsum(i-1)+dp(i)
    end do
    print'(i0)', dp(n)
end program main

Score : 400 points

Problem Statement

There are N cells arranged in a row, numbered 1, 2, \ldots, N from left to right.

Tak lives in these cells and is currently on Cell 1. He is trying to reach Cell N by using the procedure described below.

You are given an integer K that is less than or equal to 10, and K non-intersecting segments [L_1, R_1], [L_2, R_2], \ldots, [L_K, R_K].
Let S be the union of these K segments.
Here, the segment [l, r] denotes the set consisting of all integers i that satisfy l \leq i \leq r.

When you are on Cell i, pick an integer d from S and move to Cell i + d. You cannot move out of the cells.

To help Tak, find the number of ways to go to Cell N, modulo 998244353.

Constraints

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

1 \leq K \leq \min(N, 10)

1 \leq L_i \leq R_i \leq N

[L_i, R_i] and [L_j, R_j] do not intersect (i \neq j)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
L_1 R_1
L_2 R_2
:
L_K R_K

Output

Print the number of ways for Tak to go from Cell 1 to Cell N, modulo 998244353.

Sample Input 1

5 2
1 1
3 4

Sample Output 1

4

The set S is the union of the segment [1, 1] and the segment [3, 4], therefore S = \{ 1, 3, 4 \} holds.

There are 4 possible ways to get to Cell 5:

1 \to 2 \to 3 \to 4 \to 5,

1 \to 2 \to 5,

1 \to 4 \to 5 and

1 \to 5.

Sample Input 2

5 2
3 3
5 5

Sample Output 2

0

Because S = \{ 3, 5 \} holds, you cannot reach to Cell 5.
Print 0.

Sample Input 3

5 1
1 2

Sample Output 3

5

Sample Input 4

60 3
5 8
1 3
10 15

Sample Output 4

221823067

Note that you have to print the answer modulo 998244353.


program main
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: n,k,i,j,l,ns
    integer(int32):: md = 998244353
    integer(int32), allocatable:: lr(:,:), s(:),dp(:)

    read*, n, k
    allocate(lr(2,k))
    do i=1,k
        read*, lr(:,i)
    end do
    ns = sum(lr(2,:)-lr(1,:))+k
    allocate(s(ns))
    j=1
    do i=1,k
        s(j:j+(lr(2,i)-lr(1,i))) = [(l,l=lr(1,i),lr(2,i))]
        j=j+lr(2,i)-lr(1,i)+1
    end do

    allocate(dp(n),source=0)
    dp(1) = 1
    do i=1,n-1
        if (dp(i)==0) cycle
        do j=1,ns
            if (i+s(j) > n) cycle
            dp(i+s(j))=mod(dp(i+s(j))+dp(i),md)
        end do
        dp(i) = 0
        ! print'(*(i0,1x))', dp(:)
    end do
    print'(i0)', dp(n)
end program main


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


module vector_int64_mod
    use,intrinsic :: iso_fortran_env
    implicit none
    private
    public:: vec_to_array, vec_size
    type,public:: vector_int64
        integer(int64),allocatable:: array(:)
        integer(int64),private:: l=0
    contains
        procedure:: push_back=>vec_push_back, insert=>vec_insert
        procedure:: pop_back=>vec_pop_back, pop=>vec_pop, erase => vec_erase
        procedure:: at=>vec_at, back=>vec_back, head=>vec_head
    end type
contains
    function vec_size(vec) result(ret)
        type(vector_int64),intent(in):: vec
        integer(int64):: ret

        ret = vec%l
    end function

    subroutine check_array_allocation(vec)
        type(vector_int64),intent(inout):: vec

        if (.not. allocated(vec%array)) allocate(vec%array(1))
    end subroutine

    function vec_at(vec,i) result(ret)
        class(vector_int64),intent(inout):: vec
        integer(int64):: i,ret

        call check_array_allocation(vec)
        ret = vec%array(i)
    end function

    function vec_back(vec) result(ret)
        class(vector_int64),intent(inout):: vec
        integer(int64):: ret

        ret = vec%at(vec%l)
    end function

    function vec_head(vec) result(ret)
        class(vector_int64),intent(inout):: vec
        integer(int64):: ret

        ret = vec%at(1_8)
    end function

    subroutine vec_append_array(vec,l,r)
        type(vector_int64),intent(inout):: vec
        integer(int64),intent(in):: l,r
        integer(int64),allocatable:: tmp(:)

        allocate(tmp(l:2*r))
        tmp(l:r) = vec%array(l:r)
        call move_alloc(tmp, vec%array)
    end subroutine

    subroutine vec_reduce_array(vec,l,r)
        type(vector_int64),intent(inout):: vec
        integer(int64),intent(in):: l,r
        integer(int64),allocatable:: tmp(:)

        allocate(tmp(l:r/2))
        tmp(l:r/2) = vec%array(l:r/2)
        call move_alloc(tmp, vec%array)
    end subroutine

    subroutine check_allocation_size(vec)
        type(vector_int64),intent(inout):: vec
        integer(int64):: len_alloc

        call check_array_allocation(vec)
        len_alloc = size(vec%array)
        if (vec%l >= len_alloc) then
            call vec_append_array(vec,1_8,len_alloc)
        else if (vec%l <= len_alloc/2) then
            call vec_reduce_array(vec,1_8,len_alloc)
        end if
    end subroutine

    subroutine vec_push_back(vec, v)
        class(vector_int64),intent(inout):: vec
        integer(int64),intent(in):: v

        vec%l=vec%l+1
        call check_allocation_size(vec)
        vec%array(vec%l) = v
    end subroutine

    subroutine vec_insert(vec,i,v)
        class(vector_int64),intent(inout):: vec
        integer(int64),intent(in)::i, v

        vec%l=vec%l+1
        call check_allocation_size(vec)
        vec%array(i+1:vec%l+1) = vec%array(i:vec%l)
        vec%array(i) = v
    end subroutine

    function vec_pop_back(vec) result(ret)
        class(vector_int64),intent(inout):: vec
        integer(int64):: ret

        ret = vec%back()
        vec%l=vec%l-1
        call check_allocation_size(vec)
    end function

    function vec_pop(vec,i) result(ret)
        class(vector_int64),intent(inout):: vec
        integer(int64),intent(in):: i
        integer(int64):: ret

        ret = vec%at(i)
        vec%l=vec%l-1
        vec%array(i:vec%l) = vec%array(i+1:vec%l+1)
        call check_allocation_size(vec)
    end function

    subroutine vec_erase(vec,i)
        class(vector_int64):: vec
        integer(int64),intent(in):: i
        integer(int64):: dmp

        dmp = vec%pop(i)
    end subroutine

    function vec_to_array(vec) result(ret)
        type(vector_int64),intent(inout):: vec
        integer(int64):: ret(1:vec%l)

        call check_array_allocation(vec)
        ret = vec%array(1:vec%l)
    end function
end module

program main
    use,intrinsic :: iso_fortran_env
    use vector_int64_mod
    implicit none
    integer(int64):: n,x,m
    integer(int64):: i,j,l,ln,tot=0,s=0,ans=0
    integer(int64), allocatable:: id(:)
    type(vector_int64):: a

    read*, n,x,m
    allocate(id(0:m),source=-1_8)

    j=1
    do while(id(x) == -1_8)
        print'(*(i0,1x))', j,x,id(x)
        call a%push_back(x)
        tot=tot+x
        id(x)=j
        x=mod(x*x,m)
        j=j+1
    end do
    l = j-id(x)
    do i=id(x),j-1
        s=s+a%at(i)
    end do

    if (n<j) then
        do i=1,n
            ans=ans+a%at(i)
        end do
    else
        ans=tot
        ln = ((n-(j-1))/l)*s
        ans=ans+ln
        do i=id(x),id(x)+mod((n-(j-1)),l)
            ans=ans+a%at(i)
        end do
    end if
    print*, ans
end program main


module segment_tree_mod
    use,intrinsic :: iso_fortran_env
    implicit none

    type seg_tree
        integer(int64):: n,n2
        integer(int64),pointer:: v(:)
    contains
        procedure:: init => st_init
        procedure:: update => st_update
        procedure:: query => st_query
        procedure:: at => st_at
    end type
contains
    function f(a,b) result(ret)
        integer(int64):: a,b,ret
        ret = min(a,b)
    end function

    subroutine st_init(st, n)
        class(seg_tree):: st
        integer(int64),intent(in):: n
        integer(int64):: x
        x=1
        do while(n > x)
            x = 2*x
        end do
        st%n = x
        st%n2 = 2*x-1
        allocate(st%v(st%n2), source=10_int64**16)
    end subroutine

    subroutine st_update(st, i, x)
        class(seg_tree):: st
        integer(int64), intent(in):: i
        integer(int64):: ind
        integer(int64), intent(in):: x

        ind = i + st%n - 1
        st%v(ind) = x
        do while(ind > 1)
            ind = ind/2
            st%v(ind) = f(st%v(2*ind), st%v(2*ind+1))
        end do
    end subroutine

    function st_query(st, a, b) result(ret)
        class(seg_tree):: st
        integer(int64), intent(in):: a,b
        integer(int64):: ret
        ret = st_query_sub(st, a, b, 1_8, 1_8, st%n)
    end function

    function st_at(st, i) result(ret)
        class(seg_tree):: st
        integer(int64),intent(in):: i
        integer(int64):: ret
        ret = st%v(i+st%n-1)
    end function

    recursive function st_query_sub(st, a, b, k, l, r) result(ret)
        type(seg_tree),intent(in):: st
        integer(int64),intent(in):: a,b,k,l,r
        integer(int64):: ret, vl, vr

        if (r < a .or. b < l) then
            ret = 10_int64**16
        else if (a <= l .and. r <= b) then
            ret = st%v(k)
        else
            vl = st_query_sub(st,a,b,k*2, l, (l+r)/2)
            vr = st_query_sub(st,a,b,k*2+1, (l+r)/2+1, r)
            ret = f(vl,vr)
        end if
    end function
end module

program main
    use,intrinsic :: iso_fortran_env
    use segment_tree_mod
    implicit none
    integer(int64):: n,nq
    integer(int64):: i,ans,v
    integer(int64), allocatable:: q(:,:)
    type(seg_tree):: x_st, y_st

    read*, n,nq
    allocate(q(2,nq))
    do i=1,nq
        read*, q(:,i)
    end do

    ans = (n-2)*(n-2)
    call x_st%init(n)
    call y_st%init(n)
    do i=1,n-1
        call x_st%update(i,10_int64**16)
        call y_st%update(i,10_int64**16)
    end do
    call x_st%update(n,n)
    call y_st%update(n,n)

    do i=1,nq
        if (q(1,i) == 1) then
            v = x_st%query(q(2,i),n)
            ! print'("v: ",i0)', v
            ans=ans-(v-2)
            call y_st%update(v,min(y_st%at(v),q(2,i)))
        else
            v = y_st%query(q(2,i),n)
            ! print'("v: ",i0)', v
            ans=ans-(v-2)
            call x_st%update(v,min(x_st%at(v),q(2,i)))
        end if
        ! print'(i0)', ans
    end do
    print'(i0)', ans
end program main

Score : 600 points

Problem Statement

There is a grid with N rows and N columns of squares. Let (i, j) be the square at the i-th row from the top and the j-th column from the left.

Each of the central (N-2) \times (N-2) squares in the grid has a black stone on it.
Each of the 2N - 1 squares on the bottom side and the right side has a white stone on it.

Q queries are given. We ask you to process them in order.
There are two kinds of queries. Their input format and description are as follows:

1 x: Place a white stone on (1, x). After that, for each black stone between (1, x) and the first white stone you hit if you go down from (1, x), replace it with a white stone.

2 x: Place a white stone on (x, 1). After that, for each black stone between (x, 1) and the first white stone you hit if you go right from (x, 1), replace it with a white stone.

How many black stones are there on the grid after processing all Q queries?

Constraints

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

0 \leq Q \leq \min(2N-4,2\times 10^5)

2 \leq x \leq N-1

Queries are pairwise distinct.

Input

Input is given from Standard Input in the following format:

N Q
Query_1
\vdots
Query_Q

Output

Print how many black stones there are on the grid after processing all Q queries.

Sample Input 1

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

Sample Output 1

1

After each query, the grid changes in the following way:

Sample Input 2

200000 0

Sample Output 2

39999200004

Sample Input 3

176527 15
1 81279
2 22308
2 133061
1 80744
2 44603
1 170938
2 139754
2 15220
1 172794
1 159290
2 156968
1 56426
2 77429
1 97459
2 71282

Sample Output 3

31159505795


program answer
  implicit none
  integer :: x
  read(*,*) x

  if(x==0) then
     write(*,*) '1'
  else
     write(*,*) '0'
  end if

  stop
 end program answer

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 sample
  implicit none
  integer(8) :: a,b,c,d,e

  read(*,*) a,b,c,d

  e=max(a*c,a*d,b*c,b*d)

  write(*,*) e

  stop
end program sample

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(8), parameter :: modu = 1000000007
  integer :: s
  integer(8) :: m(3, 3) = 0
  read(*,*) s
  if (s < 3) then
    write(*,'(i0)') 0
    stop
  end if
  m(1, 1) = 1
  m(2, 1) = 1
  m(3, 2) = 1
  m(1, 3) = 1
  m = modmatpow(m, s - 3)
  write(*,'(i0)') m(1, 1)
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

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 atcoder
implicit none
integer d, t, s
real(8) time
read(*,*) d,t,s

time = dble(d)/dble(s)

if (time <= t) then
	write(*,*) 'Yes'
else
	write(*,*) 'No'
end if
end program

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 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
    if(t>=ans)then
        print *,"Yes"
    else
        print *,"No"
    endif
end program donot_be_late


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

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


program answer
  implicit none
  integer :: D, T, S
  read(*,*) D, T, S
  if(dble(D)/S>T) then
     write(*,*) 'No'
  else
     write(*,*) 'Yes'
  end if

  stop
 end program answer


program main
  implicit none
  integer :: D, T,S

  read(*,*) D,T,S

  if (real(D)/real(S) <= T ) then
     write(*,'(a)') 'Yes'
  else
     write(*,'(a)') 'No'
  end if

end program


program main
    implicit none
    character(1000)::s,t
    integer::n,m,ans,i,j,nm
    integer,allocatable::aaa(:)

    ans=0

    read*,s,t
    !print*,s,t

    n=len_trim(s)
    m=len_trim(t)
    nm=n-m
    !print*,n,m,nm
    allocate(aaa(nm+1))
    !print*,s(n:n),t(m:m)
    if(nm==0) then
        do i=1,m
            if(s(i:i)/=t(i:i)) then
                !print*,s(i:i)
                ans=ans+1
            end if
        end do
        go to 100
    end if

    ans=0
    if(nm>0) then
        !print*,s(1:1)
        do i=1,nm+1
            aaa(i)=0
            do j=i,m+i-1
               ! print*,s(j:j)
                if(s(j:j)/=t(j-i+1:j-i+1)) then
                   ! print*,s(j:j)
                    aaa(i)=aaa(i)+1
                end if
            end do
        end do
        ans=minval(aaa)
    end if

100 print*,ans
end program main

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


program p2
    implicit none
    character(len=1005)::s, t
    integer::tmp, ans, i, j, ls, lt
    read(*,*) s, t
    ls = len_trim(s)
    lt = len_trim(t)
    ans = lt
    do i = 1, ls-lt+1
        tmp = 0
        do j = 1, lt
            if(s(i+j-1:i+j-1) .ne. t(j:j))  tmp = tmp + 1
        end do
        ans = min(tmp,ans)
    end do
    write(*,*) ans
    stop
end program


program answer
  implicit none
  integer :: i, N, M, count, h
  integer, allocatable :: A(:), B(:), C(:), D(:,:)
  read(*,*) N, M
  allocate(A(M),B(M),C(N),D(N,N))
  do i = 1, M
     read(*,*) A(i), B(i)
  end do

  do i = 1, M
     if(B(i)<A(i)) then
        h=B(i)
        B(i)=A(i)
        A(i)=h
     end if
  end do

  D=0
  C=0
  do i = 1, M
     if(D(A(i),B(i))==1) then
        cycle
     end if
     D(A(i),B(i))=1
     C(A(i))=C(A(i))+1
  end do

  write(*,*) maxval(C)+1
  deallocate(A,B,C,D)
  stop
 end program answer

Score : 400 points

Problem Statement

There are N persons called Person 1 through Person N.

You are given M facts that "Person A_i and Person B_i are friends." The same fact may be given multiple times.

If X and Y are friends, and Y and Z are friends, then X and Z are also friends. There is no friendship that cannot be derived from the M given facts.

Takahashi the evil wants to divide the N persons into some number of groups so that every person has no friend in his/her group.

At least how many groups does he need to make?

Constraints

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

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

1\leq A_i,B_i\leq N

A_i \neq B_i

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
\vdots
A_M B_M

Output

Print the answer.

Sample Input 1

5 3
1 2
3 4
5 1

Sample Output 1

3

Dividing them into three groups such as \{1,3\}, \{2,4\}, and \{5\} achieves the goal.

Sample Input 2

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

Sample Output 2

4

Sample Input 3

10 4
3 1
4 1
5 9
2 6

Sample Output 3

3


module mod_tree_map
  implicit none
  integer, private, parameter :: key_kind = 4
  integer, private, parameter :: val_kind = 4

  type t_entry
    private
    integer(key_kind) :: key
    integer(val_kind) :: val
  end type

  type t_node
    private
    type(t_node), pointer :: left => null(), right => null()
    integer :: level = 1
    type(t_entry), pointer :: e => null()
  end type

  type tree_map
    integer :: size = 0
    type(t_node), private, pointer :: root => null()
    integer(val_kind) :: default = 0
  contains
    procedure :: clear => clear
    procedure :: is_empty => is_empty
    procedure :: is_not_empty => is_not_empty
    procedure :: get_keys => get_keys
    procedure :: put => put
    procedure :: remove => remove
    procedure :: get => get
    procedure :: contains => contain
    procedure :: first_key => get_first_key
    procedure :: poll_first => poll_first
    procedure :: last_key => get_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
  end type

contains

  function new_entry(key, val) result(res)
    integer(key_kind), intent(in) :: key
    integer(val_kind), optional, intent(in) :: val
    type(t_entry), pointer :: res
    res => null()
    allocate(res)
    res%key = key
    res%val = merge(val, 0_val_kind, present(val))
  end

  function new_node(e) result(res)
    type(t_entry), pointer, intent(in) :: e
    type(t_node), pointer :: res
    res => null()
    allocate(res)
    res%e => e
  end

  integer function level(node) result(res)
    type(t_node), pointer, intent(in) :: node
    res = merge(node%level, 0, associated(node))
  end

  logical function is_leaf(node) result(res)
    type(t_node), pointer, intent(in) :: node
    res = associated(node) .and. &
      .not.associated(node%left) .and. &
      .not.associated(node%right)
  end

  function skew(node) result(res)
    type(t_node), pointer, intent(in) :: node
    type(t_node), pointer :: res
    if (.not.associated(node)) then
      res => null()
    else if (.not.associated(node%left)) then
      res => node
    else if (level(node%left) == level(node)) then
      res => node%left
      node%left => res%right
      res%right => node
    else
      res => node
    end if
  end

  function split(node) result(res)
    type(t_node), pointer, intent(in) :: node
    type(t_node), pointer :: res
    if (.not.associated(node)) then
      res => null()
    else if (.not.associated(node%right)) then
      res => node
    else if (.not.associated(node%right%right)) then
      res => node
    else if (level(node%right%right) == level(node)) then
      res => node%right
      node%right => res%left
      res%left => node
      res%level = res%level + 1
    else
      res => node
    end if
  end

  function predecessor(node) result(res)
    type(t_node), pointer, intent(in) :: node
    type(t_node), pointer :: res
    res => null()
    if (.not.associated(node%left)) return
    res => node%left
    do while (associated(res%right))
      res => res%right
    end do
  end

  function successor(node) result(res)
    type(t_node), pointer, intent(in) :: node
    type(t_node), pointer :: res
    res => null()
    if (.not.associated(node%right)) return
    res => node%right
    do while (associated(res%left))
      res => res%left
    end do
  end

  recursive function insert(node, e) result(res)
    type(t_node), pointer, intent(in) :: node
    type(t_entry), pointer, intent(in) :: e
    type(t_node), pointer :: res
    res => new_node(e)
    if (.not.associated(node)) return
    res => node
    if (e%key < res%e%key) then
      res%left => insert(res%left, e)
    else if (e%key > res%e%key) then
      res%right => insert(res%right, e)
    else
      res%e => e
    end if
    res => skew(res)
    res => split(res)
  end

  recursive function delete(node, e) result(res)
    type(t_node), pointer, intent(in) :: node
    type(t_entry), pointer, intent(in) :: e
    type(t_node), pointer :: res, l
    res => node
    if (.not.associated(node)) return
    if (e%key < res%e%key) then
      res%left => delete(res%left,e)
    else if (e%key > res%e%key) then
      res%right => delete(res%right,e)
    else
      if (is_leaf(res)) then
        res => null()
        return
      end if
      if (.not.associated(res%left)) then
        l => successor(res)
        res%right => delete(res%right, l%e)
        res%e => l%e
      else
        l => predecessor(res)
        res%left => delete(res%left, l%e)
        res%e => l%e
      end if
    end if
    res => decrease_level(res)
    res => skew(res)
    res%right => skew(res%right)
    if (associated(res%right)) res%right%right => skew(res%right%right)
    res => split(res)
    res%right => split(res%right)
  end

  function decrease_level(node) result(res)
    type(t_node), pointer, intent(in) :: node
    type(t_node), pointer :: res
    integer :: should_be
    res => node
    should_be = min(level(res%left), level(res%right)) + 1
    if (res%level > should_be) then
      res%level = should_be
      if (level(res%right) > should_be) res%right%level = should_be
    end if
  end

  recursive subroutine release_tree(node)
    type(t_node), pointer, intent(inout) :: node
    if (.not.associated(node)) return
    call release_tree(node%left)
    call release_tree(node%right)
    deallocate(node)
  end

  recursive subroutine get_keys_list(node, keys, num)
    type(t_node), pointer, intent(in) :: node
    integer(key_kind), intent(inout) :: keys(:)
    integer, intent(inout) :: num
    if (.not.associated(node)) return
    call get_keys_list(node%left, keys, num)
    num = num + 1
    keys(num) = node%e%key
    call get_keys_list(node%right, keys, num)
  end

  subroutine clear(this)
    class(tree_map), intent(inout) :: this
    call release_tree(this%root)
    this%root => null()
  end

  logical function is_empty(this) result(res)
    class(tree_map), intent(in) :: this
    res = .not.associated(this%root)
  end

  logical function is_not_empty(this) result(res)
    class(tree_map), intent(in) :: this
    res = associated(this%root)
  end

  subroutine put_entry(map, e)
    type(tree_map), intent(inout) :: map
    type(t_entry), pointer, intent(in) :: e
    if (.not.contains_entry(map, e)) map%size = map%size + 1
    map%root => insert(map%root, e)
  end

  subroutine remove_entry(map, e)
    type(tree_map), intent(inout) :: map
    type(t_entry), pointer, intent(in) :: e
    if (contains_entry(map, e)) then
      map%root => delete(map%root, e)
      map%size = map%size - 1
    end if
  end

  function get_entry(map, e) result(res)
    type(tree_map), intent(in) :: map
    type(t_entry), pointer, intent(in) :: e
    type(t_node), pointer :: n
    type(t_entry), pointer :: res
    res => null()
    n => map%root
    do while (associated(n))
      if (e%key < n%e%key) then
        n => n%left
      else if (e%key > n%e%key) then
        n => n%right
      else
        res => n%e
        return
      end if
    end do
  end

  function contains_entry(map, e) result(res)
    type(tree_map), intent(in) :: map
    type(t_entry), pointer, intent(in) :: e
    type(t_node), pointer :: n
    logical :: res
    res = .false.
    n => map%root
    do while (associated(n))
      if (e%key < n%e%key) then
        n => n%left
      else if (e%key > n%e%key) then
        n => n%right
      else
        res = .true.
        return
      end if
    end do
  end

  function get_first_entry(map) result(res)
    type(tree_map), intent(in) :: map
    type(t_node), pointer :: n
    type(t_entry), pointer :: res
    res => null()
    n => map%root
    if (.not.associated(n)) return
    do while (associated(n%left))
      n => n%left
    end do
    res => n%e
  end

  function poll_first_entry(map) result(res)
    type(tree_map), intent(inout) :: map
    type(t_node), pointer :: n
    type(t_entry), pointer :: res
    res => get_first_entry(map)
    map%root => delete(map%root, res)
  end

  function get_last_entry(map) result(res)
    type(tree_map), intent(in) :: map
    type(t_node), pointer :: n
    type(t_entry), pointer :: res
    res => null()
    n => map%root
    if (.not.associated(n)) return
    do while (associated(n%right))
      n => n%right
    end do
    res => n%e
  end

  function poll_last_entry(map) result(res)
    type(tree_map), intent(inout) :: map
    type(t_node), pointer :: n
    type(t_entry), pointer :: res
    res => get_last_entry(map)
    map%root => delete(map%root, res)
  end

  function floor_entry(map, e) result(res)
    type(tree_map), intent(in) :: map
    type(t_entry), pointer, intent(in) :: e
    type(t_node), pointer :: n
    type(t_entry), pointer :: res
    res => null()
    n => map%root
    do while (associated(n))
      if (e%key < n%e%key) then
        n => n%left
      else if (e%key > n%e%key) then
        if (.not.associated(res)) then
          res => n%e
          cycle
        end if
        if (e%key - res%key > e%key - n%e%key) res => n%e
        n => n%right
      else
        res => n%e
        return
      end if
    end do
  end

  function lower_entry(map, e) result(res)
    type(tree_map), intent(in) :: map
    type(t_entry), pointer, intent(in) :: e
    type(t_entry), pointer :: res
    res => floor_entry(map, new_entry(e%key - 1))
  end

  function ceiling_entry(map, e) result(res)
    type(tree_map), intent(in) :: map
    type(t_entry), pointer, intent(in) :: e
    type(t_node), pointer :: n
    type(t_entry), pointer :: res
    res => null()
    n => map%root
    do while (associated(n))
      if (e%key < n%e%key) then
        if (.not.associated(res)) then
          res => n%e
          cycle
        end if
        if (e%key - res%key < e%key - n%e%key) res => n%e
        n => n%left
      else if (e%key > n%e%key) then
        n => n%right
      else
        res => n%e
        return
      end if
    end do
  end

  function higher_entry(map, e) result(res)
    type(tree_map), intent(in) :: map
    type(t_entry), pointer, intent(in) :: e
    type(t_entry), pointer :: res
    res => ceiling_entry(map, new_entry(e%key + 1))
  end

  function get_keys(this) result(res)
    class(tree_map), intent(in) :: this
    integer(key_kind) :: res(this%size)
    integer :: num
    res = 0
    num = 0
    call get_keys_list(this%root, res, num)
  end

  subroutine put(this, key, val)
    class(tree_map), intent(inout) :: this
    integer(key_kind), intent(in) :: key
    integer(val_kind), intent(in) :: val
    call put_entry(this, new_entry(key, val))
  end

  subroutine remove(this, key)
    class(tree_map), intent(inout) :: this
    integer(key_kind), intent(in) :: key
    call remove_entry(this, new_entry(key))
  end

  integer(val_kind) function get(this, key) result(res)
    class(tree_map), intent(in) :: this
    integer(key_kind), intent(in) :: key
    type(t_entry), pointer :: e
    res = this%default
    e => get_entry(this, new_entry(key))
    if (.not.associated(e)) return
    res = e%val
  end

  logical function contain(this, key) result(res)
    class(tree_map), intent(in) :: this
    integer(key_kind), intent(in) :: key
    res = contains_entry(this, new_entry(key))
  end

  integer(key_kind) function get_first_key(this) result(res)
    class(tree_map), intent(in) :: this
    type(t_entry), pointer :: e
    res = this%default
    e => get_first_entry(this)
    if (.not.associated(e)) return
    res = e%key
  end

  integer(key_kind) function poll_first(this) result(res)
    class(tree_map), intent(inout) :: this
    type(t_entry), pointer :: e
    res = this%default
    e => poll_first_entry(this)
    if (.not.associated(e)) return
    res = e%key
  end

  integer(key_kind) function get_last_key(this) result(res)
    class(tree_map), intent(in) :: this
    type(t_entry), pointer :: e
    res = this%default
    e => get_last_entry(this)
    if (.not.associated(e)) return
    res = e%key
  end

  integer(key_kind) function poll_last(this) result(res)
    class(tree_map), intent(inout) :: this
    type(t_entry), pointer :: e
    res = this%default
    e => poll_last_entry(this)
    if (.not.associated(e)) return
    res = e%key
  end

  integer(key_kind) function floor_key(this, key) result(res)
    class(tree_map), intent(in) :: this
    integer(key_kind), intent(in) :: key
    type(t_entry), pointer :: e
    res = this%default
    e => floor_entry(this, new_entry(key))
    if (.not.associated(e)) return
    res = e%key
  end

  integer(key_kind) function lower_key(this, key) result(res)
    class(tree_map), intent(in) :: this
    integer(key_kind), intent(in) :: key
    type(t_entry), pointer :: e
    res = this%default
    e => lower_entry(this, new_entry(key))
    if (.not.associated(e)) return
    res = e%key
  end

  integer(key_kind) function ceiling_key(this, key) result(res)
    class(tree_map), intent(in) :: this
    integer(key_kind), intent(in) :: key
    type(t_entry), pointer :: e
    res = this%default
    e => ceiling_entry(this, new_entry(key))
    if (.not.associated(e)) return
    res = e%key
  end

  integer(key_kind) function higher_key(this, key) result(res)
    class(tree_map), intent(in) :: this
    integer(key_kind), intent(in) :: key
    type(t_entry), pointer :: e
    res = this%default
    e => higher_entry(this, new_entry(key))
    if (.not.associated(e)) return
    res = e%key
  end

end module mod_tree_map
program i_hate_shortest_path_problem
  use mod_tree_map
  implicit none
  integer :: h, w, a, b, c, d, e, key, val, i
  type(tree_map) :: map, cnt
  read(*,*) h, w
  do i = 1, w
    call map%put(i, i)
  end do
  call cnt%put(0, w)
  do i = 1, h
    read(*,*) a, b
    key = map%ceiling_key(a)
    b = b + 1
    c = -1
    do while (key <= b .and. key > 0)
      val = map%get(key)
      c = max(c, val)
      d = key - val
      e = cnt%get(d) - 1
      if (e == 0) then
        call cnt%remove(d)
      else
        call cnt%put(d, e)
      end if
      call map%remove(key)
      key = map%ceiling_key(key)
    end do
    if (c > 0 .and. b <= w) then
      call map%put(b, c)
      d = b - c
      call cnt%put(d, cnt%get(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 answer
  implicit none
  integer(16) :: N, i, a, b
  integer(16), allocatable :: s(:)
  read(*,*) N
  if(N==0) then
     write(*,*) 'Yes'
     stop
  end if

  do i = 1, 200000
     if(10**(i-1)<=N.and.N<10**i) then
        a=i
        allocate(s(a))
        exit
     end if
  end do

  do i = 1, a
     s(a-i+1)=int(N/10**(a-i))
     N=N-10**(a-i)*int(N/10**(a-i))
  end do

  if(mod(sum(s),9)==0) then
     write(*,*) 'Yes'
  else
     write(*,*) 'No'
  end if
  deallocate(s)
  stop
 end program answer

Score : 200 points

Problem Statement

An integer N is a multiple of 9 if and only if the sum of the digits in the decimal representation of N is a multiple of 9.

Determine whether N is a multiple of 9.

Constraints

0 \leq N < 10^{200000}

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If N is a multiple of 9, print Yes; otherwise, print No.

Sample Input 1

123456789

Sample Output 1

Yes

The sum of these digits is 1+2+3+4+5+6+7+8+9=45, which is a multiple of 9, so 123456789 is a multiple of 9.

Sample Input 2

0

Sample Output 2

Yes

Sample Input 3

31415926535897932384626433832795028841971693993751058209749445923078164062862089986280

Sample Output 3

No


program b176

implicit none
character(len=200000) :: a
integer :: n, i, t

read *, a

n = 0

do i = 1, 200000
 if (a(i:i) == ' ') then
  exit
 else
  read(a(i:i),*) t
  n = n + t
 end if
end do

!print *, n

if(mod(n, 9) == 0) then
 print *, 'Yes'
else
 print *, 'No'
end if

end program b176


      PROGRAM piyo
      IMPLICIT NONE
      INTEGER,PARAMETER :: p = 8
      integer(p) :: N(2*10**5+1)
      N = 0
      read'(*(i1))',N

      if( mod(sum(N),9)==0 )then
        print*,'Yes'
      else
        print*,'No'
      end if

      stop
      !debugg

      print*,n(1:10)

      END PROGRAM


program ABC176B
    implicit none
    integer(8)::i,ans=0
    character(2000000)::S
    read*,S
    do i=1,len_trim(S)
        ans=ans+ichar(S(i:i))-48
    end do

    if(mod(ans,9)==0)then
        print'(A)',"Yes"
    else
        print'(A)',"No"
    end if
end program ABC176B


program main

 implicit none
 integer::n,i,answer
 real(8)::ans,maxx
 real(8),allocatable::a(:)

 read*,n
 allocate(a(n))
 read*,a
 !print*,a

ans=0
maxx=a(1)
 do i=2,n
   if(maxx>a(i)) then
    ans=ans+(maxx-a(i))
    !print*,ans
   end if
   if(maxx<a(i)) then
    maxx=a(i)
   end if
  end do

answer=int(ans)
 print*,answer

end program main

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.


module deque_mod
    use,intrinsic :: iso_fortran_env
    implicit none
    type deque
        integer(int32),pointer:: v(:)
        integer(int32):: l,r
        integer(int32):: lmax, rmax
    contains
        procedure:: append => dq_append
        procedure:: appendleft => dq_appendleft
        procedure:: pop => dq_pop
        procedure:: popleft => dq_popleft
        procedure:: right => dq_right
        procedure:: left => dq_left
        procedure:: remaining_elements => dq_remaining_elements
        procedure:: remain => dq_remain
    end type

    interface deque
        procedure:: dq_init
    end interface
contains
    function dq_init() result(dq)
        ! 初期化
        type(deque):: dq
        dq%r=0; dq%rmax=1
        dq%l=1; dq%lmax=-1
        allocate(dq%v(dq%lmax:dq%rmax))
    end function

    subroutine dq_append(dq,num)
        ! 右端に挿入
        class(deque):: dq
        integer(int32):: num
        if (dq%r+1 > dq%rmax) call dq_add_(dq)
        dq%r=dq%r+1
        dq%v(dq%r) = num
    end subroutine

    subroutine dq_appendleft(dq,num)
        ! 左端に挿入
        class(deque):: dq
        integer(int32):: num
        if (dq%l-1 < dq%lmax) call dq_add_(dq)
        dq%l=dq%l-1
        dq%v(dq%l) = num
    end subroutine

    subroutine dq_add_(dq)
        ! arrayの延長
        class(deque):: dq
        integer(int32):: l
        integer(int32),pointer:: tmp(:)
        l = size(dq%v)
        allocate(tmp(l))
        tmp(:) = dq%v(:)
        deallocate(dq%v)
        allocate(dq%v(3*dq%lmax:3*dq%rmax))
        dq%v(dq%lmax:dq%rmax) = tmp(:)
        dq%lmax = 3*dq%lmax
        dq%rmax = 3*dq%rmax
    end subroutine

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

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

    function dq_right(dq) result(ret)
        ! 右端を確認
        class(deque):: dq
        integer(int32):: ret
        ret = dq%v(dq%r)
    end function

    function dq_left(dq) result(ret)
        ! 左端を確認
        class(deque):: dq
        integer(int32):: ret
        ret = dq%v(dq%l)
    end function

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

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

program main
    use,intrinsic :: iso_fortran_env
    use deque_mod
    implicit none
    integer(int32):: h,w,sx,sy,gx,gy,i,j,x,y,dx,dy,nx,ny
    integer(int32),allocatable:: nm(:,:)
    character(1),allocatable:: s(:,:)
    logical,allocatable:: passed(:,:)
    type(deque):: tx, ty, tx2,ty2

    tx = deque()
    ty = deque()
    tx2 = deque()
    ty2 = deque()
    read*, h,w
    read*, sy,sx
    read*, gy,gx

    allocate(nm(w,h), source=1000000000)
    allocate(s(w,h))
    allocate(passed(w,h),source=.false.)

    do y=1,h
        block; character(w):: inpt_s
            read*, inpt_s
            do x=1,w
                s(x,y) = inpt_s(x:x)
            end do
        end block
    end do

    call tx%append(sx)
    call ty%append(sy)
    nm(sx,sy) = 0

    do while(tx%remain())
        do while(tx%remain())
            x = tx%popleft()
            y = ty%popleft()
            ! print'(*(i0,1x))', x,y
            passed(x,y) = .true.
            call tx2%append(x)
            call ty2%append(y)
            ! do i=1,h
            !     print'(*(i0,1x))', nm(:,i)
            ! end do

            do dy = -1,1
                do dx = -1,1
                    if (dx==0 .and. dy==0) cycle
                    if (dx /= 0 .and. dy /= 0) cycle
                    nx = x+dx; ny = y+dy
                    if (.not. in_s(nx,ny)) cycle
                    if (s(nx,ny) == "#") cycle
                    if (nm(nx,ny) > nm(x,y)) then
                        ! print'(*(i0,1x))', nx,ny
                        nm(nx,ny) = nm(x,y)
                        call tx%append(nx)
                        call ty%append(ny)
                    end if
                end do
            end do
        end do

        do while(tx2%remain())
            x = tx2%popleft()
            y = ty2%popleft()
            do dy=-2,2
                do dx=-2,2
                    nx = dx+x; ny=dy+y
                    if (.not. in_s(nx,ny)) cycle
                    if (s(nx, ny) == "#") cycle
                    if (nm(nx,ny) > nm(x,y)+1) then
                        ! print'(*(i0,1x))', nx,ny
                        nm(nx,ny) = nm(x,y)+1
                        call tx%append(nx)
                        call ty%append(ny)
                    end if
                end do
            end do
        end do
    end do

    if (nm(gx,gy) == 1000000000) then
        print'(i0)', -1
        stop
    end if
    print'(i0)', nm(gx,gy)
contains
    function in_s(x,y) result(ret)
        integer(int32):: x,y
        logical:: ret

        ret = 1 <= x .and. x <= w
        ret = ret .and. (1 <= y .and. y <= h)
    end function
end program main

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_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, a1, a2, a3, 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))
    a1 = a(i)
    a2 = a(i + 1)
    a3 = a(i + 2)
    do j = 1, n
      dp0(j) = dp(min(a1, j), max(a1, j))
    end do
    mv0(1:n) = mv(1:n)
    v = max(0, maxval(mv(1:n)))
    b(1) = dp(a1, a1)
    b(2) = dp(a2, a2)
    b(3) = dp(a3, a3)
    if (a1 == a2) then
      do j = 1, n
        call update(a3, j, dp0(j) + 1)
      end do
    end if
    call update(a2, a3, b(1) + 1)
    call update(a1, a3, b(2) + 1)
    call update(a1, a2, b(3) + 1)
    call update(a2, a3, v)
    call update(a1, a3, v)
    call update(a1, a2, v)
    do j = 1, n
      call update(a1, j, mv0(j))
      call update(a2, j, mv0(j))
      call update(a3, 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


program main
implicit none

character*3 :: s
character*1 :: r
integer :: curLongest,totalLongest,i
read(*,*) s

r = 'R'

totalLongest = 0
curLongest = 0

do i=1,3
  if(s(i:i).eq.r) then
    ! write(*,*) s(i:i),r
    curLongest = curLongest + 1
  else
    curLongest = 0
  endif
  if(totalLongest.lt.curLongest) then
    totalLongest = curLongest
  endif
enddo

write(*,*) totalLongest
end program

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 rainy_season
  implicit none
  character(3) :: s
  integer :: n
  read(*,*) s
  if (s == 'RRR') then
    n = 3
  else if (s == 'SRR' .or. s == 'RRS') then
    n = 2
  else if (s == 'RRR') then
    n = 0
  else
    n = 1
  end if
  write(*,'(i0)') n
end program rainy_season


program make_triangle
      integer :: n
      integer,allocatable :: l(:)
      integer :: i, j, k
      integer :: t(3)
      integer :: ans = 0

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

      do i = 1, n-1
        do j = i + 1, n
          if(l(i) .gt. l(j))then
            k = t(i)
            t(i) = t(j)
            t(j) = k
          end if
        end do
      end do

      do i  = 1, n
        do j = i+1, n
          do k = j+1, n
            if(t(k) /= t(j) .and. t(i) /= t(j) .and. t(i) + t(j) > t(k)) then
              ans = ans + 1
            end if
          end do
        end do
      end do
      print *, ans
end program

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 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 = i+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


      program WalkingTakahashi
!
      implicit none
      integer(8) :: X,K,D
      integer(8) :: ans1,ans2
      intrinsic abs,mod
!
      read(*,*) X,K,D
!
      ans1 = abs(X)/D
      ans2 = ans1 + 1
!
      if(K >= ans1) then
        if(mod(K-ans1,2) == 0) write(*,*) X-ans1*D
        if(mod(K-ans1,2) == 1) write(*,*) ans2*D-X
      else
        write(*,*) X-K*D
      end if
!
      end program WalkingTakahashi

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.


program D175
    implicit none
    integer(16) :: N, K
    integer(16), allocatable, dimension(:) :: P, C
    integer(16) :: i, j, l, ans, max1, max2

    read(5,*) N, K
    allocate(P(N), C(N))
    read(5,*) (P(i), i = 1, N)
    read(5,*) (C(i), i = 1, N)

    do i = 1, N
        ans = 0
        l = i
        do j = 1, K
            l = P(l)
            ans = ans + C(l)
            if (j == 1) then
                max2 = ans
            else
                if (max2 < ans) then
                    max2 = ans
                end if
            end if
        end do

        if (i == 1) then
            max1 = max2
        else
            if (max1 < max2) then
                max1 = max2
            end if
        end if

    end do

    write(6,*) max1

end program D175

Score : 400 points

Problem Statement

Takahashi will play a game using a piece on an array of squares numbered 1, 2, \cdots, N. Square i has an integer C_i written on it. Also, he is given a permutation of 1, 2, \cdots, N: P_1, P_2, \cdots, P_N.

Now, he will choose one square and place the piece on that square. Then, he will make the following move some number of times between 1 and K (inclusive):

In one move, if the piece is now on Square i (1 \leq i \leq N), move it to Square P_i. Here, his score increases by C_{P_i}.

Help him by finding the maximum possible score at the end of the game. (The score is 0 at the beginning of the game.)

Constraints

2 \leq N \leq 5000

1 \leq K \leq 10^9

1 \leq P_i \leq N

P_i \neq i

P_1, P_2, \cdots, P_N are all different.

-10^9 \leq C_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
P_1 P_2 \cdots P_N
C_1 C_2 \cdots C_N

Output

Print the maximum possible score at the end of the game.

Sample Input 1

5 2
2 4 5 1 3
3 4 -10 -8 8

Sample Output 1

8

When we start at some square of our choice and make at most two moves, we have the following options:

If we start at Square 1, making one move sends the piece to Square 2, after which the score is 4. Making another move sends the piece to Square 4, after which the score is 4 + (-8) = -4.

If we start at Square 2, making one move sends the piece to Square 4, after which the score is -8. Making another move sends the piece to Square 1, after which the score is -8 + 3 = -5.

If we start at Square 3, making one move sends the piece to Square 5, after which the score is 8. Making another move sends the piece to Square 3, after which the score is 8 + (-10) = -2.

If we start at Square 4, making one move sends the piece to Square 1, after which the score is 3. Making another move sends the piece to Square 2, after which the score is 3 + 4 = 7.

If we start at Square 5, making one move sends the piece to Square 3, after which the score is -10. Making another move sends the piece to Square 5, after which the score is -10 + 8 = -2.

The maximum score achieved is 8.

Sample Input 2

2 3
2 1
10 -7

Sample Output 2

13

Sample Input 3

3 3
3 1 2
-1000 -2000 -3000

Sample Output 3

-1000

We have to make at least one move.

Sample Input 4

10 58
9 1 6 7 8 4 3 2 10 5
695279662 988782657 -119067776 382975538 -151885171 -177220596 -169777795 37619092 389386780 980092719

Sample Output 4

29507023469

The absolute value of the answer may be enormous.


program main
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: n,k,ans,si,i
    integer(int64), allocatable:: p(:), c(:)

    read*, n,k
    allocate(p(n), c(n))

    read*, p(:)
    read*, c(:)
    ans = -6e18
    do si=1,n
        block; integer(int64):: x, s(n), l, tot, now,t,e
            x = si
            l=0
            tot = 0
            do while(.true.)
                l=l+1
                x = p(x)
                s(l) = c(x)
                tot = tot + c(x)
                if (x == si) exit
            end do
            t=0
            do i=1,l
                t = t + s(i)
                if (i > k) exit
                now = t
                if (tot > 0)then
                    e = (k-i)/l
                    now = now + tot*e
                end if
                ans = max(ans, now)
            end do
        end block
    end do
    print'(i0)', ans
end program main


program making_palindrome
  implicit none
  integer(8), parameter :: inf = 1e18
  integer :: n, i
  character(20) :: s(50, 2) = ''
  integer(8) :: c(50) = 0, m(50, 21, 2) = 0, x = inf
  logical :: u(50, 21, 2) = .false.
  read(*,*) n
  do i = 1, n
    read(*,*) s(i, 1), c(i)
    s(i, 2) = reverse(s(i, 1)(1:len_trim(s(i, 1))))
  end do
  do i = 1, n
    x = min(x, solve(i, 1, 1) + c(i))
  end do
  write(*,'(i0)') merge(x, -1_8, x < inf)
contains
  function reverse(s) result(res)
    character(*), intent(in) :: s
    character(len_trim(s)) :: res
    integer :: n, i
    n = len_trim(s)
    do i = 1, n
      res(i:i) = s(n - i + 1:n - i + 1)
    end do
  end
  recursive integer(8) function solve(i, j, k) result(res)
    integer, intent(in) :: i, j, k
    integer :: l, r, t
    logical :: f
    res = m(i, j, k)
    if (u(i, j, k)) return
    u(i, j, k) = .true.
    m(i, j, k) = inf
    l = j
    r = len_trim(s(i, k))
    f = .true.
    do while (l < r)
      if (s(i, k)(l:l) /= s(i, k)(r:r)) then
        f = .false.
        exit
      end if
      l = l + 1
      r = r - 1
    end do
    if (f) then
      m(i, j, k) = 0
      res = 0
      return
    end if
    do t = 1, n
      l = j
      r = 1
      do while (l <= len_trim(s(i, k)) .and. r <= len_trim(s(t, 3 - k)))
        if (s(i, k)(l:l) /= s(t, 3 - k)(r:r)) exit
        l = l + 1
        r = r + 1
      end do
      if (l == len_trim(s(i, k)) + 1) then
        m(i, j, k) = min(m(i, j, k), solve(t, r, 3 - k) + c(t))
      end if
      if (r == len_trim(s(t, 3 - k)) + 1) then
        m(i, j, k) = min(m(i, j, k), solve(i, l, k) + c(t))
      end if
    end do
    res = m(i, j, k)
  end
end program making_palindrome

Score : 600 points

Problem Statement

We have N strings of lowercase English letters: S_1, S_2, \cdots, S_N.

Takahashi wants to make a string that is a palindrome by choosing one or more of these strings - the same string can be chosen more than once - and concatenating them in some order of his choice.

The cost of using the string S_i once is C_i, and the cost of using it multiple times is C_i multiplied by that number of times.

Find the minimum total cost needed to choose strings so that Takahashi can make a palindrome.

If there is no choice of strings in which he can make a palindrome, print -1.

Constraints

1 \leq N \leq 50

1 \leq |S_i| \leq 20

S_i consists of lowercase English letters.

1 \leq C_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
S_1 C_1
S_2 C_2
:
S_N C_N

Output

Print the minimum total cost needed to choose strings so that Takahashi can make a palindrome, or -1 if there is no such choice.

Sample Input 1

3
ba 3
abc 4
cbaa 5

Sample Output 1

7

We have ba, abc, and cbaa.

For example, we can use ba once and abc once for a cost of 7, then concatenate them in the order abc, ba to make a palindrome.
Also, we can use abc once and cbaa once for a cost of 9, then concatenate them in the order cbaa, abc to make a palindrome.

We cannot make a palindrome for a cost less than 7, so we should print 7.

Sample Input 2

2
abcab 5
cba 3

Sample Output 2

11

We can choose abcab once and cba twice, then concatenate them in the order abcab, cba, cba to make a palindrome for a cost of 11.

Sample Input 3

4
ab 5
cba 3
a 12
ab 10

Sample Output 3

8

We can choose a once, which is already a palindrome, but it is cheaper to concatenate ab and cba.

Sample Input 4

2
abc 1
ab 2

Sample Output 4

-1

We cannot make a palindrome, so we should print -1.


program integer_product
  implicit none
  integer :: n, p2(200000) = 0, p5(200000) = 0, i, j, m
  integer(8) :: a
  character(16) :: s
  read(*,*) n
  do i = 1, n
    read(*,*) s
    m = len_trim(s)
    do j = 1, m
      if (s(j:j) == '.') then
        p2(i) = j - m
        p5(i) = j - m
        s(j:m - 1) = s(j + 1:m)
        s(m:) = ''
        exit
      end if
    end do
    read(s,*) a
    do while (mod(a, 2_8) == 0)
      a = a / 2
      p2(i) = p2(i) + 1
    end do
    do while (mod(a, 5_8) == 0)
      a = a / 5
      p5(i) = p5(i) + 1
    end do
  end do
  a = 0
  do i = 1, n - 1
    do j = i + 1, n
      if (p2(i) + p2(j) >= 0 .and. p5(i) + p5(j) >= 0) a = a + 1
    end do
  end do
  write(*,'(i0)') a
end program integer_product

Score : 300 points

Problem Statement

You are given N real values A_1, A_2, \ldots, A_N.
Compute the number of pairs of indices (i, j)
such that i < j and the product A_i \cdot A_j is integer.

Constraints

2 \leq N \leq 200\,000

0 < A_i < 10^4

A_i is given with at most 9 digits after the decimal.

Input

Input is given from Standard Input in the following format.

N
A_1
A_2
\vdots
A_N

Output

Print the number of pairs with integer product A_i \cdot A_j (and i < j).

Sample Input 1

5
7.5
2.4
17.000000001
17
16.000000000

Sample Output 1

3

There are 3 pairs with integer product:

7.5 \cdot 2.4 = 18

7.5 \cdot 16 = 120

17 \cdot 16 = 272

Sample Input 2

11
0.9
1
1
1.25
2.30000
5
70
0.000000001
9999.999999999
0.999999999
1.000000001

Sample Output 2

8


program main
	implicit none
    integer a
    read *,a
    if (a>=30) then
    	print *, "Yes"
    else
    	print *, "No"
    end if
end program main

Score : 100 points

Problem Statement

You will turn on the air conditioner if, and only if, the temperature of the room is 30 degrees Celsius or above.

The current temperature of the room is X degrees Celsius. Will you turn on the air conditioner?

Constraints

-40 \leq X \leq 40

X is an integer.

Input

Input is given from Standard Input in the following format:

X

Output

Print Yes if you will turn on the air conditioner; print No otherwise.

Sample Input 1

25

Sample Output 1

No

Sample Input 2

30

Sample Output 2

Yes


program main
implicit none

integer x

read(*,*) x
if(x.ge.30) then
  write(*,'(A)') "YES"
else
  write(*,'(A)')"NO"
endif
end program


program distance
    integer :: n, d, i
    integer :: ans = 0
    integer, allocatable :: x(:)
    integer, allocatable :: y(:)

    read *, n, d
    allocate(x(n))
    allocate(y(n))
    print *, "座標を入力"
    do i = 1, n
        read *, x(i), y(i)
        if(real(d) >= sqrt(real((x(i)**2)+(y(i)**2))))then
            ans = ans+1
        end if
    end do
    print *, ans
end program

Score : 200 points

Problem Statement

We have N points in the two-dimensional plane. The coordinates of the i-th point are (X_i,Y_i).

Among them, we are looking for the points such that the distance from the origin is at most D. How many such points are there?

We remind you that the distance between the origin and the point (p, q) can be represented as \sqrt{p^2+q^2}.

Constraints

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

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

|X_i|,|Y_i| \leq 2\times 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N D
X_1 Y_1
\vdots
X_N Y_N

Output

Print an integer representing the number of points such that the distance from the origin is at most D.

Sample Input 1

4 5
0 5
-2 4
3 4
4 -4

Sample Output 1

3

The distance between the origin and each of the given points is as follows:

\sqrt{0^2+5^2}=5

\sqrt{(-2)^2+4^2}=4.472\ldots

\sqrt{3^2+4^2}=5

\sqrt{4^2+(-4)^2}=5.656\ldots

Thus, we have three points such that the distance from the origin is at most 5.

Sample Input 2

12 3
1 1
1 1
1 1
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3

Sample Output 2

7

Multiple points may exist at the same coordinates.

Sample Input 3

20 100000
14309 -32939
-56855 100340
151364 25430
103789 -113141
147404 -136977
-37006 -30929
188810 -49557
13419 70401
-88280 165170
-196399 137941
-176527 -61904
46659 115261
-153551 114185
98784 -6820
94111 -86268
-30401 61477
-55056 7872
5901 -163796
138819 -185986
-69848 -96669

Sample Output 3

6


program test
 implicit none
 integer(8)::N,i,ans
 real(8)::X,Y,D
 read*,N,D
 ans=0
 do i=1,N
 	read*,X,Y
 	if(sqrt(X*X+Y*Y)<=D) then
    	ans=ans+1
    else
    	ans=ans
    end if
end do
print*,ans
end program test


program ABC174B
    implicit none
    integer(8)::N,D,i,X,Y,ans=0
    read*,N,D
    do i=1,N
        read*,X,Y
        if(X**2+Y**2<=D**2)ans=ans+1
    end do
    print'(i0)',ans
end program ABC174B


program main
    implicit none
    integer::n,d,xx,yy,i,cou
    real(8),allocatable::x(:),y(:)
    real(8)::z

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

    do i=1,n
    read*,xx,yy
    x(i)=xx
    y(i)=yy
    !print*,x(i),y(i)
    end do
    !print*,x,y

    cou=0
    do i=1,n
        z=sqrt(x(i)**2+y(i)**2)
        if(z<=d) then
            cou=cou+1
        end if
     end do

     print*,cou

end program main


program baisuu_search
    integer(8) :: K, i
    integer(8) :: c = 0
    logical :: f = .false.

    read *, K
    do i = 7 ,2**20-1, i*10+1
        c = c + 1
        if(mod(i, K) .eq. 0)then
            f = .true.
        end if
        if(f .eqv. .true.)exit
    end do
    if(f .eqv. .true.)then
        print *, c
    else
        print *, '-1'
    end if
end program

Score : 300 points

Problem Statement

Takahashi loves the number 7 and multiples of K.

Where is the first occurrence of a multiple of K in the sequence 7,77,777,\ldots? (Also see Output and Sample Input/Output below.)

If the sequence contains no multiples of K, print -1 instead.

Constraints

1 \leq K \leq 10^6

K is an integer.

Input

Input is given from Standard Input in the following format:

K

Output

Print an integer representing the position of the first occurrence of a multiple of K. (For example, if the first occurrence is the fourth element of the sequence, print 4.)

Sample Input 1

101

Sample Output 1

4

None of 7, 77, and 777 is a multiple of 101, but 7777 is.

Sample Input 2

2

Sample Output 2

-1

All elements in the sequence are odd numbers; there are no multiples of 2.

Sample Input 3

999983

Sample Output 3

999982


program main
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: k, n, ans
    logical,allocatable:: kishutu(:)

    read*, k
    allocate(kishutu(0:k), source = .false.)
    if (mod(k,7)==0) k=k/7
    n = mod(1,k)
    ans=1
    do while(n /= 0)
        if (kishutu(n)) then
            print'(i0)', -1
            stop
        end if
        kishutu(n) = .true.
        n = mod(n*10+1,k)
        ans=ans+1
    end do
    print'(i0)', ans
end program main


program alter_altar
    implicit none
    character(200001) :: c = '0'
    integer n
    integer :: r(200001) = 0
    integer :: w(200001) = 0
    integer :: i = 1
    integer :: k = 1
    integer :: j = 0
    read *, n
    read *, c
    do
        if(i /= 1)then
            w(i) = index(c(w(i+1):),'W')
            r(k) = index(c(:r(k-1)),'R',back=.true.)
            if(w(i) > r(k) .or. w(i) == 0 .or. r(i) == 0) exit
            !c(w(i):w(i)) = 'R'
            !c(r(k):r(k)) = 'W'
            j = j + 1
        else if(i == 1)then
            w(i) = index(c,'W')
            r(k) = index(c,'R',back=.true.)
            if(w(i) > r(k) .or. w(i) == 0 .or. r(i) == 0) exit
            if(w(i) < r(k))then
                !c(w(i):w(i)) = 'R'
                !c(r(k):r(k)) = 'W'
                j = j + 1
            end if
        end if
    end do
    print "('',i0)", j
end program

Score : 400 points

Problem Statement

An altar enshrines N stones arranged in a row from left to right. The color of the i-th stone from the left (1 \leq i \leq N) is given to you as a character c_i; R stands for red and W stands for white.

You can do the following two kinds of operations any number of times in any order:

Choose two stones (not necessarily adjacent) and swap them.

Choose one stone and change its color (from red to white and vice versa).

According to a fortune-teller, a white stone placed to the immediate left of a red stone will bring a disaster. At least how many operations are needed to reach a situation without such a white stone?

Constraints

2 \leq N \leq 200000

c_i is R or W.

Input

Input is given from Standard Input in the following format:

N
c_{1}c_{2}...c_{N}

Output

Print an integer representing the minimum number of operations needed.

Sample Input 1

4
WWRR

Sample Output 1

2

For example, the two operations below will achieve the objective.

Swap the 1-st and 3-rd stones from the left, resulting in RWWR.

Change the color of the 4-th stone from the left, resulting in RWWW.

Sample Input 2

2
RR

Sample Output 2

0

It can be the case that no operation is needed.

Sample Input 3

8
WRWWRWRR

Sample Output 3

3


program main
 implicit none
 integer n,i,j,k,ans
 character*200000 C
 read(*,*) n
 read(*,*) c

 ans = 0
 k = n
 do i=1,n
   if(i >= k)then
     exit
   end if
   if(C(i:i)=='W')then
     do j=k,1,-1
       if(C(j:j)=='R')then
         C(i:i)='R'
         C(j:j)='W'
         k=j-1
         ans = ans + 1
         exit
       end if
     end do
   end if
 end do

 write(*,*) ans
end program main