program main
implicit None
	character*10,allocatable::w(:)
	integer::n,i,j,c,k

	read *,n

	c = 0
	allocate(w(n))

	read *,w

	do i = 1,n-1
		do j = i+1,n
			if(w(i) == w(j))then
				c = 1
				print *,"a"
			endif
		enddo
	enddo

	do i = 2,n
		k = len_trim(w(i-1))
		if(w(i-1)(k:k) /= w(i)(1:1))then
			c = 1
		endif
	enddo

	if(c == 0)then
		print 101,"Yes"
		else
			print 101,"No"
	endif

101 format(a)
end program main

Score : 200 points

Problem Statement

Takahashi is practicing shiritori alone again today.

Shiritori is a game as follows:

In the first turn, a player announces any one word.

In the subsequent turns, a player announces a word that satisfies the following conditions:

That word is not announced before.

The first character of that word is the same as the last character of the last word announced.

In this game, he is practicing to announce as many words as possible in ten seconds.

You are given the number of words Takahashi announced, N, and the i-th word he announced, W_i, for each i. Determine if the rules of shiritori was observed, that is, every word announced by him satisfied the conditions.

Constraints

N is an integer satisfying 2 \leq N \leq 100.

W_i is a string of length between 1 and 10 (inclusive) consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
W_1
W_2
:
W_N

Output

If every word announced by Takahashi satisfied the conditions, print Yes; otherwise, print No.

Sample Input 1

4
hoge
english
hoge
enigma

Sample Output 1

No

As hoge is announced multiple times, the rules of shiritori was not observed.

Sample Input 2

9
basic
c
cpp
php
python
nadesico
ocaml
lua
assembly

Sample Output 2

Yes

Sample Input 3

8
a
aa
aaa
aaaa
aaaaa
aaaaaa
aaa
aaaaaaa

Sample Output 3

No

Sample Input 4

3
abc
arc
agc

Sample Output 4

No


program main
  implicit integer(a-z)
  character(100), dimension(100)::w
  character(3)::ans="Yes"

  read(*,*)n
  do i=1,n
     read(*,*)w(i)
  end do

  do i=2,n

     if(w(i-1)(len(trim(adjustl(w(i-1)))):len(trim(adjustl(w(i-1)))))&
          .ne.w(i)(1:1))then
        ans="No"
        write(*,*)i
     end if

     do j=1,i-1
        if(w(j).eq.w(i))then
           ans="No"
           !write(*,*)i,j
        end if
     end do
  end do

  write(*,*)ans
end program main

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

Submitted source-code file size, in bytes.

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

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


program main
  implicit integer(a-z)
  character(10), dimension(100)::w
  character(3)::ans="Yes"

  read(*,*)n
  do i=1,n
     read(*,*)w(i)
  end do

  do i=2,n

     if(w(i-1)(len(trim(adjustl(w(i-1)))):len(trim(adjustl(w(i-1)))))&
          .ne.w(i)(1:1))then
        ans="No"
        write(*,*)i
     end if

     do j=1,i-1
        if(w(j).eq.w(i))then
           ans="No"
           !write(*,*)i,j
        end if
     end do
  end do

  write(*,*)ans
end program main


program main
  implicit integer(a-z)
  character(100), dimension(10)::w
  character(3)::ans="Yes"

  read(*,*)n
  do i=1,n
     read(*,*)w(i)
  end do

  do i=2,n

     if(w(i-1)(len(trim(adjustl(w(i-1)))):len(trim(adjustl(w(i-1)))))&
          .ne.w(i)(1:1))then
        ans="No"
        write(*,*)i
     end if

     do j=1,i-1
        if(w(j).eq.w(i))then
           ans="No"
           !write(*,*)i,j
        end if
     end do
  end do

  write(*,*)ans
end program main


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

    read(*,*) n,k
    allocate(x(n))
    read(*,*)x
    m=10**9
    do i=1,n
        a=abs(x(i)-k)
        do
            if(mod(a,m)==0)then
                exit
            end if
            m=mod(a,m)
        end do
    end do
    write(*,*)m
    stop
end program sample

Score : 300 points

Problem Statement

There are N cities on a number line. The i-th city is located at coordinate x_i.

Your objective is to visit all these cities at least once.

In order to do so, you will first set a positive integer D.

Then, you will depart from coordinate X and perform Move 1 and Move 2 below, as many times as you like:

Move 1: travel from coordinate y to coordinate y + D.

Move 2: travel from coordinate y to coordinate y - D.

Find the maximum value of D that enables you to visit all the cities.

Here, to visit a city is to travel to the coordinate where that city is located.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq X \leq 10^9

1 \leq x_i \leq 10^9

x_i are all different.

x_1, x_2, ..., x_N \neq X

Input

Input is given from Standard Input in the following format:

N X
x_1 x_2 ... x_N

Output

Print the maximum value of D that enables you to visit all the cities.

Sample Input 1

3 3
1 7 11

Sample Output 1

2

Setting D = 2 enables you to visit all the cities as follows, and this is the maximum value of such D.

Perform Move 2 to travel to coordinate 1.

Perform Move 1 to travel to coordinate 3.

Perform Move 1 to travel to coordinate 5.

Perform Move 1 to travel to coordinate 7.

Perform Move 1 to travel to coordinate 9.

Perform Move 1 to travel to coordinate 11.

Sample Input 2

3 81
33 105 57

Sample Output 2

24

Sample Input 3

1 1
1000000000

Sample Output 3

999999999


program skip
  implicit none
  integer :: n, x(100001) = 0, i, m
  read(*,*) n, x(1)
  read(*,*) x(2:n+1)
  call quick_sort(x(1:n+1))
  m = x(2)-x(1)
  do i = 2, n
    m = gcd(m,x(i+1)-x(i))
  end do
  write(*,'(i0)') m
contains
  integer function gcd(a,b)
    integer, intent(in) :: a, b
    integer :: x, y
    x = max(abs(a),abs(b))
    y = min(abs(a),abs(b))
    do while (y > 0)
      gcd = y
      y = mod(x,y)
      x = gcd
    end do
  end
  recursive subroutine quick_sort(a)
    integer, intent(inout) :: a(:)
    integer :: n, l, r, m
    integer :: p
    n = size(a)
    l = 1
    r = n
    m = (n+1)/2
    p = a(l)+a(m)+a(r)-max(a(l),a(m),a(r))-min(a(l),a(m),a(r))
    do
      do while (a(l) < p)
        l = l+1
      end do
      do while (a(r) > p)
        r = r-1
      end do
      if (l >= r) exit
      a(l) = xor(a(l),a(r))
      a(r) = xor(a(l),a(r))
      a(l) = xor(a(l),a(r))
      l = l+1
      r = r-1
    end do
    if (l-1 > 1) call quick_sort(a(1:l-1))
    if (n > r+1) call quick_sort(a(r+1:n))
  end
end program skip


program name
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: n,ix,i,ans
    integer(int32), allocatable:: x(:),dx(:)

    read*, n,ix
    allocate(x(n+1))
    x(1) = ix
    read*, x(2:)

    call merge_sort(x,1,n+1)

    allocate(dx(n))
    do i=1,n
        dx(i) = x(i+1)-x(i)
    end do
    deallocate(x)
    ans=dx(1)
    do i=2,n
        ans=gcd(ans,dx(i))
    end do

    print'(i0)', ans
contains
recursive function gcd(x,y) result(ret)
    integer(int32):: x,y,ret

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

    ret = gcd(y,mod(x,y))
end function
recursive subroutine merge_sort(ar, fst, lst)
integer(int32),intent(inout):: ar(:)
integer(int32),intent(in):: fst,lst
integer(int32):: mdl

if (lst-fst < 2) then
    if (ar(fst) > ar(lst)) call swap(ar(fst),ar(lst))
    return
end if

mdl = (fst+lst)/2
call merge_sort(ar, fst, mdl)
call merge_sort(ar, mdl+1, lst)
call merge_(ar, fst, mdl, lst)
end subroutine

subroutine merge_(ar, fst, mdl, lst)
integer(int32),intent(inout):: ar(:)
integer(int32),intent(in):: fst, mdl, lst
integer(int32),allocatable:: tmp(:)
integer(int32):: li, ri, ti

allocate(tmp(lst-fst+1))

li=fst
ri=mdl+1
ti=1

do while (li <= mdl .and. ri <= lst)
    if (ar(li) <= ar(ri)) then
        tmp(ti) = ar(li)
        li=li+1
    else
        tmp(ti) = ar(ri)
        ri=ri+1
    end if
    ti=ti+1
end do

if (li <= mdl) then
    tmp(ti:) = ar(li:mdl)
else
    tmp(ti:) = ar(ri:lst)
end if

ar(fst:lst) = tmp(:)
deallocate(tmp)
end subroutine

subroutine swap(x,y)
integer(int32),intent(inout):: x,y
integer(int32):: tmp
tmp = x
x = y
y = tmp
end subroutine
end program name


implicit none
integer :: n,x0
integer,allocatable :: x(:)
integer :: i,p,q,r,gcd

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

do i = 1, n
   x(i) = abs(x(i) - x0)
enddo

gcd = x(1)
do i = 2, n
   p = min(gcd,x(i))
   q = max(gcd,x(i))

   r = 1
   do while (r > 0)
      r = mod(q,p)
      q = p
      p = r
   enddo

   if (i == 2 .or. q > gcd) then
      gcd = q
   endif
enddo

write(*,'(i0)') gcd
end


program main
implicit none
integer :: i, g
integer :: n, m
integer , allocatable :: x(:)

read(*,*) n, m
allocate( x(n) )
read(*,*) x(1:n)
x = abs(x -m)
if( n == 1 ) then
   write(*,'(i0)') x(1)
else
   do i = 1, n-1
      call gcdlcm(x(i), x(i+1), g)
      x(i+1) = g
   end do
   write(*,'(i0)') g
end if

stop
contains
  subroutine gcdlcm( a, b, g )
    integer , intent(in)  :: a, b
    integer , intent(out) :: g

    integer :: i, j, k,l

    i = a
    j = b
    do while( j /= 0 )
       k = mod(i,j)
       i = j
       j = k
    end do
    g = i
    l = a*b/g
  end subroutine gcdlcm

end program main


program main
implicit None
	integer(8)::n,x,p,i,q
	integer(8),allocatable::a(:)

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

	read*,a

	a = abs(a-x)
	p = a(1)

	if(n>1)then
	do i = 2,n
		call euc(p,a(i),q)
		p = q
	enddo
	endif

	print "(i0)",p
end program main

subroutine euc(a1,b1,c)
implicit None
integer(8)::a1,b1,c,a,b

	a =a1
	b =b1
	c =1

	do while (a /=0 .and. b /= 0)
		if (a > b)then
			c =b
			a =mod(a,b)
			else
			c =a
			b =mod(b,a)
		endif
	enddo
end subroutine euc


program test
integer(8)::n, x, ans, i, t, k, p, a
allocatable::a(:)
read(*,*)n, x
allocate(a(n))
read(*,*)(a(i), i=1,n)

if(n==1)then
t=a(1)-x
ans=abs(t)

else
t=x-a(1)
ans=abs(t)

do i=1,n
t=x-a(i)
k=abs(t)
call subr4(ans,k,p)
ans=p
end do
end if
write(*,*)ans

end program test

subroutine subr4(a,b,q)
  implicit none
  integer(8) :: a, b, q
  integer(8) :: m, n, k
  ! step 1
  m = a
  n = b
  do
     ! step 2
     k = mod(m, n)
     ! step 3
     if (k == 0) then
        q=n
        exit
     end if
     ! step 4
     m = n
     n = k
  end do
 end subroutine subr4


program test
integer::n, x, ans, i, t, k, p
allocatable::a(:)
read(*,*)n, x
allocate(a(n))
read(*,*)(a(i), i=1,n)

if(n==1)then
t=a(1)-x
ans=abs(t)

else
t=x-a(1)
ans=abs(t)

do i=1,n
t=x-a(i)
k=abs(t)
call subr4(ans,k,p)
ans=p
end do
end if
write(*,*)ans

end program test

subroutine subr4(a,b,q)
  implicit none
  integer :: a, b, q
  integer :: m, n, k
  ! step 1
  m = a
  n = b
  do
     ! step 2
     k = mod(m, n)
     ! step 3
     if (k == 0) then
        q=n
        exit
     end if
     ! step 4
     m = n
     n = k
  end do
 end subroutine subr4


program answer
  implicit none

  integer ::D, N

  read(*,*) D, N

  if(D==0) then
     if(N==100) then
        write(*,*) 101
        stop
     else
        write(*,*) N
        stop
     end if
  else if(D==1) then
     if(N==100) then
        write(*,*) 10100
        stop
     else
        write(*,*) 100*N
        stop
     end if
  else
     if(N==100) then
        write(*,*) 1010000
     else
        write(*,*) 10000*N
     end if
   end if

  stop
 end program answer

Score: 200 points

Problem Statement

Today, the memorable AtCoder Beginner Contest 100 takes place. On this occasion, Takahashi would like to give an integer to Ringo.

As the name of the contest is AtCoder Beginner Contest 100, Ringo would be happy if he is given a positive integer that can be divided by 100 exactly D times.

Find the N-th smallest integer that would make Ringo happy.

Constraints

D is 0, 1 or 2.

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

Input

Input is given from Standard Input in the following format:

D N

Output

Print the N-th smallest integer that can be divided by 100 exactly D times.

Sample Input 1

0 5

Sample Output 1

5

The integers that can be divided by 100 exactly 0 times (that is, not divisible by 100) are as follows: 1, 2, 3, 4, 5, 6, 7, ...

Thus, the 5-th smallest integer that would make Ringo happy is 5.

Sample Input 2

1 11

Sample Output 2

1100

The integers that can be divided by 100 exactly once are as follows: 100, 200, 300, 400, 500, 600, 700, 800, 900, 1 \ 000, 1 \ 100, ...

Thus, the integer we are seeking is 1 \ 100.

Sample Input 3

2 85

Sample Output 3

850000

The integers that can be divided by 100 exactly twice are as follows: 10 \ 000, 20 \ 000, 30 \ 000, ...

Thus, the integer we are seeking is 850 \ 000.


program sample
    implicit none

    integer(8) :: i,j,m,n,d
    real(8)::a,b
    integer(8),allocatable :: x(:),y(:)

    read(*,*) d,n
    i=(n-1)/99+1

    m=i*10+mod(n-1,99)+1
    write(*,*) m*100**d

    stop
end program sample


program answer
  implicit none

  integer ::D, N

  read(*,*) D, N

  write(*,*) (100**D)*N

  stop
 end program answer


program sample
	implicit none
    integer(8)::d,k

    read(*,*) d,k

	if(k/=100) then
		write(*,*) k*(100**d)
    else
    	write(*,*) (k+1)*(100**d)
    end if

    stop
end program sample


program sample
	implicit none
    integer(8)::d,k

    read(*,*) d,k

	write(*,*) k*(100**d)

    stop
end program sample


program sample
    implicit none

    integer(8) :: i,j,m,n,d
    real(8)::a,b
    integer(8),allocatable :: x(:),y(:)

    read(*,*) d,n

    m=(n/99)*10+mod(n-1,99)+1
    write(*,*) m*100**d

    stop
end program sample


program sample
    implicit none

    integer(8) :: i,j,m,n,d
    real(8)::a,b
    integer(8),allocatable :: x(:),y(:)

    read(*,*) d,n

    m=(n/9)*10+mod(n-1,9)+1
    write(*,*) m*100**d

    stop
end program sample


program main
  implicit none

  integer :: d, n, ans

  read(*,*) d, n

  select case(d)
  case(0)
     ans = 1
  case(1)
     ans = 100
  case(2)
     ans = 10000
  end select

  if (n /= 100) then
     ans = ans * n
  else
     ans = ans * 101
  end if

  write(*,*) ans

end program main


program main
  implicit none

  integer :: d, n, ans

  read(*,*) d, n

  select case(d)
  case(0)
     ans = 1
  case(1)
     ans = 100
  case(2)
     ans = 10000
  end select

  if (n /= 100) then
     ans = ans * n
  else
     ans = ans * 99
  end if

  write(*,*) ans

end program main


program main
  implicit none

  integer :: d, n, ans

  read(*,*) d, n

  select case(d)
  case(0)
     ans = 1
  case(1)
     ans = 100
  case(2)
     ans = 10000
  end select

  ans = ans * n

  write(*,*) ans

end program main


program prob3
    implicit none
    integer :: D, N
    integer :: ans

    read(*,*) D, N
    if(N == 100) then
        N = 101
    end if

    ans = 100**D *N
    write(*,*) ans

    stop
contains
end program prob3


program atcoder
  implicit none
  integer d, n, i
  read *, d, n    ! 変数mに、標準入力から数字を読み込み代入する
  if (N<100) then
   i=100**D*N
  else
   i=100**D*101
  end if
  print'(i0)', i    !  ","のあとに解答となる変数を書く
end program atcoder


  implicit none
  integer :: D, N, out

  read(*,*) D, N

  if ( D == 0 ) then
     if ( n /= 100) then
        write(*,'(i0)') N
     else
        write(*,'(i0)') N+1
     end if
  else if ( D == 1 ) then
     if ( n /= 100) then
        write(*,'(i0)') 100*N
     else
        write(*,'(i0)') 100*(N+1)
     end if
  else
     if ( n /= 100) then
        write(*,'(i0)') 100*100*N
     else
        write(*,'(i0)') 100*100*(N+1)
     end if
  end if

end


program name

    implicit none
    integer D,N,counter
    integer i
    read*, D,N
    D = 100**D
    counter = 0
    if ( D==1 ) then
        print*, D*N
    else
        do i = 1, 1000000, 1
            if ( mod(i,D)==0 .and. (.not.(mod(i/D,D)==0))) then
                counter = counter + 1
                if ( counter==N ) then
                    print*, i
                end if
            end if
        end do

    end if
end program


program name

    implicit none
    integer D,N,counter
    integer i
    read*, D,N
    D = 100**D
    counter = 0
    if ( D==1 ) then
        print*, D*N
    else
        do i = 1, 1000000, 1
            if ( mod(i,D)==0 .and. (.not.(i/D==D))) then
                counter = counter + 1
                if ( counter==N ) then
                    print*, i
                end if
            end if
        end do

    end if
end program


program name

    implicit none
    integer D,N
    read*, D,N
    print*, (100**D)*N

end program


program name

    implicit none
    integer D,N
    read*, D,N
    print*, 100**D*N

end program


program Ringo
	integer D, N
    read(*, *) D, N
    if (N.NE.100) then
    X = (100**D)*N
    print '(i0)', int(X)
    else if (N.EQ.100) then
    X = (100**D)*(N + 1)
	print '(i0)', int(X)
    end if
end program


program RFN
implicit none
integer ::d, n

read(*,*) d, n
if(n/=100)then
   write(*,*) n*100**d
else
   write(*,*) (n+1)*100**d
end if

end program RFN


INTEGER(16) A,B
READ*,A,B
PRINT"(I0)",B*100**A
END


program main
implicit none
integer ::d, n

read(*,*) d, n
if(n/=100)then
   write(*,'(i0)') n*100**d
else
   write(*,'(i0)') (n+1)*100**d
end if

end program main


program main
implicit none
integer ::d, n

read(*,*) d, n
write(*,'(i0)') n*100**d

end program main


program main
  implicit none
  integer, parameter :: imax = 1010000
  integer :: d, n, i, cnt
  read(*, *) d, n
  cnt = 0
  do i = 1, imax
    if (d == 0) then
      if (mod(i, 100) /= 0) cnt = cnt + 1
    else if (d == 1) then
      if (mod(i, 100) == 0 .and. mod(i, 10000) /= 0) cnt = cnt + 1
    else if (d == 2) then
      if (mod(i, 10000) == 0 .and. mod(i, 1000000) /= 0) cnt = cnt + 1
    else
      stop 1
    end if
    if (cnt == n) then
      write(*, "(i0)") i
      stop 0
    end if
  end do
end program main


program main
  implicit none
  integer, parameter :: imax = 1010000
  integer :: d, n, i, cnt
  read(*, *) d, n
  cnt = 0
  do i = 1, imax
    if (d == 0) then
      if (mod(i, 100) /= 0) cnt = cnt + 1
    else if (d == 1) then
      if (mod(i, 100) == 0 .and. mod(i, 10000) /= 0) cnt = cnt + 1
    else if (d == 2) then
      if (mod(i, 10000) == 0) cnt = cnt + 1
    else
      stop 1
    end if
    if (cnt == n) then
      write(*, "(i0)") i
      stop 0
    end if
  end do
end program main


program main
  implicit none
  integer, parameter :: imax = 1000000
  integer :: d, n, i, cnt
  read(*, *) d, n
  cnt = 0
  do i = 1, imax
    if (d == 0) then
      if (mod(i, 100) /= 0) cnt = cnt + 1
    else if (d == 1) then
      if (mod(i, 100) == 0 .and. mod(i, 10000) /= 0) cnt = cnt + 1
    else if (d == 2) then
      if (mod(i, 10000) == 0) cnt = cnt + 1
    else
      stop 1
    end if
    if (cnt == n) then
      write(*, "(i0)") i
      stop 0
    end if
  end do
end program main


program main
  implicit integer(a-z)

  read(*,*)d,n

  if(n.eq.100)n=n+1

  write(*,*)n*100**d
end program main


    program main
      implicit integer(a-z)

      read(*,*)d,n

      write(*,*)n*100**d
    end program main


program main
  implicit integer(a-z)

  read(*,*)d,n

  write(*,*)n*10**d
end program main


program apple
implicit none
integer::d, n
 read(5,*)d, n
  if(d==0) then
   print*,n
  else if(d==1)then
   print*,n*100
  else
   print*,n*10000
  end if
end program apple


program main
	integer D, N
	read(*, *) D, N
	if(D == 0) then
		if(N == 100) then
			write(*, *) N+1
		else
			write(*, *) N
		end if
	else if(D == 1) then
		if(N == 100) then
			write(*, *) N*(100**D) + 1
		else
			write(*, *) N
		end if
	else if(D == 2) then
		if(N == 100) then
			write(*, *) N*(100**D) + 1
		else
			write(*, *) N
		end if
	end if
end program main


implicit none
integer:: a,b,c,x,i,j,k,count

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

count=0
do i=0,a
do j=0,b
do k=0,c
if(x==500*i+100*j+50*k)then
count=count+1
endif
end do
end do
end do

write(*,*) count
end

Score : 200 points

Problem Statement

You have A 500-yen coins, B 100-yen coins and C 50-yen coins (yen is the currency of Japan).
In how many ways can we select some of these coins so that they are X yen in total?

Coins of the same kind cannot be distinguished. Two ways to select coins are distinguished when, for some kind of coin, the numbers of that coin are different.

Constraints

0 \leq A, B, C \leq 50

A + B + C \geq 1

50 \leq X \leq 20 000

A, B and C are integers.

X is a multiple of 50.

Input

Input is given from Standard Input in the following format:

A
B
C
X

Output

Print the number of ways to select coins.

Sample Input 1

2
2
2
100

Sample Output 1

2

There are two ways to satisfy the condition:

Select zero 500-yen coins, one 100-yen coin and zero 50-yen coins.

Select zero 500-yen coins, zero 100-yen coins and two 50-yen coins.

Sample Input 2

5
1
0
150

Sample Output 2

0

Note that the total must be exactly X yen.

Sample Input 3

30
40
50
6000

Sample Output 3

213


program B087
    implicit none
    integer(8) :: A, B, C, X
    integer(8) :: i, j, k, sum

    read(5,*) A
    read(5,*) B
    read(5,*) C
    read(5,*) X

    sum = 0
    do i = 0, A
        do j = 0, B
            do k = 0, C
                if (500*i+100*j+50*k == X) then

                    sum = sum + 1
                end if

            end do
        end do
    end do

    write(6,*) sum

end program B087


program main
    implicit none
    INTEGER :: a, b, c, x, sum = 0, ans = 0, i, j, k
    READ(*, *) a, b, c, x
    do i = 0, a
        do j = 0, b
            do k = 0, c
                sum = 500 * i + 100 * j + 50 * k
                if(sum == x) ans = ans + 1
                sum = 0
            enddo
        enddo
    enddo
    WRITE(*, *) ans
end program main


program ABC087B
    implicit none
    integer(8)::i,j,k
    integer(8)::A,B,C,X
    integer(8)::result=0
    read(5,*)A,B,C,X

    do i=0,A
        do j=0,B
            do k=0,C
                if(i*500+j*100+k*50==X)then
                    result=result+1
                end if
            end do
        end do
    end do
    print'(i0)',result
end program ABC087B


program coins
  implicit none
  integer :: a, b, c, x, i, j, k, m = 0
  read(*,*) a
  read(*,*) b
  read(*,*) c
  read(*,*) x
  x = x/50
  do i = 0, a
    if (10*i > x) exit
    do j = 0, b
      if (10*i+2*j > x) exit
      k = x-(10*i+2*j)
      if (k <= c) m = m+1
    end do
  end do
  write(*,'(i0)') m
end program coins


program test
	implicit none
    integer::a,b,c,x,i,j,k,ans
    read*,a,b,c,x
    ans=0
    do i=0,a
    	do j=0,b
        	do k=0,c
            	if(i*500+j*100+k*50==x)then
                	ans=ans+1
                end if
             end do
         end do
    end do
    print*,ans
end program test


program tenquestionfour
 implicit none
 integer A, B, C, X, i, j, k, res
 read(*,*) A
 read(*,*) B
 read(*,*) C
 read(*,*) X
 ! read(*,*) A, B, C, X でよい
 res = 0
 do i=0,A
  do j=0,B
   do k=0,C
   if ((500*i + 100*j + 50*k).eq.X) res = res + 1
   if ((500*i + 100*j + 50*k).gt.x) exit
   end do
   if ((500*i + 100*j).gt.x) exit
  end do
  if ((500*i).gt.x) exit
 end do
 write(*,*) res
end program


program tenquestionfour
 implicit none
 integer A, B, C, X, i, j, k, res, sum
 read(*,*) A
 read(*,*) B
 read(*,*) C
 read(*,*) X
 res = 0
 sum = 0
 do i=0,A
  do j=0,B
   do k=0,C
   sum = 500*i + 100*j + 50*k
   if(sum.eq.X) then
   res = res + 1
   end if
   end do
  end do
 end do
 write(*,*) res
end program


module global_variables
end module global_variables
program main
  use global_variables
  implicit none
  integer a,b,c,x,res
  integer i,j,k
  read *, a,b,c,x
  res = 0
  do i=a,0,-1
    do j=b,0,-1
      do k=c,0,-1
        if (i*500 + j*100 + k*50 == x) then
          res = res + 1
        endif
      enddo
    enddo
  enddo
  print '(i0)', res
end program main


program coins
	implicit none
    integer :: a,b,c,x
    integer :: a5,i,j,k,Total
    integer :: num

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

    num=0

	do i=0,a
    	do j=0,b
        	do k=0,c
            	Total=i*500+j*100+k*50
                if(Total.eq.x) then
                	num=num+1
                endif
            end do
        end do
    end do

    write(*,'(i0)') num

end program coins


program coins
  implicit none
  integer :: a, b, c, x
  integer :: y, z
  integer :: i, j, m
  read(*,*) a
  read(*,*) b
  read(*,*) c
  read(*,*) x

  if (x .gt. 500*a+100*b+50*c) then
     write(*,'(i0)') 0
     stop
  end if

  m = 0
  do i = 0, a
     y = x - 500*i
     if (y .lt. 0) exit
     do j = 0, b
        z = y - 100*j
        if (z .lt. 0) exit
        if (z/50 .le. c) m = m + 1
     end do
  end do

  write(*,'(i0)') m

  stop
end program coins


  implicit none

  integer::a,b,c,x
  integer::count
  integer::i,j,k

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

  count=0
  do i=0,a
     do j=0,b
        do k=0,c
           if(x==i*500+j*100+k*50)count=count+1
        end do
     end do
  end do

  write(*,*)count

end program


implicit none
integer:: x,a,b,c,count,i,j,k
read(5,*) a,b,c,x
count=0

do i=0,a
do j=0,b
do k=0,c
    if (x==500*i+100*j+50*k) then
        count=count+1
    endif
enddo
enddo
enddo

write(6,*) count

end


program probB
  implicit none
  integer a,b,c,x,n
  integer i,j,k

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

  n=0
  do i=0,a
     do j=0,b
        do k=0,c
           if(500*i+100*j+50*k==x) n=n+1
        end do
     end do
  end do

  write(*,*) n

end program probB


program main
    integer :: a, b

    read(*,*) a, b

    if (mod(a,2).ne.0 .and. mod(b,2).ne.0) then
        write(*,*) 'Odd'
    else
        write(*,*) 'Even'
    end if
end program

Score : 100 points

Problem Statement

AtCoDeer the deer found two positive integers, a and b.
Determine whether the product of a and b is even or odd.

Constraints

1 ≤ a,b ≤ 10000

a and b are integers.

Input

Input is given from Standard Input in the following format:

a b

Output

If the product is odd, print Odd; if it is even, print Even.

Sample Input 1

3 4

Sample Output 1

Even

As 3 × 4 = 12 is even, print Even.

Sample Input 2

1 21

Sample Output 2

Odd

As 1 × 21 = 21 is odd, print Odd.


program main
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: a,b
    read*, a,b
    if (mod(a*b,2)==0) then
        print'(a)', 'Even'
    else
        print'(a)', 'Odd'
    end if
end program main


program main
  implicit double precision (a-h, o-z)
  read(*, *) i, j
  k = ibits(i, 0, 1) * ibits(j, 0, 1)
  if (k == 0) then
    write(*, *) "Even"
  else
    write(*, *) "Odd"
  endif
  stop
end program main


program sample1
implicit none

integer a,b
real c
read(*,*) a,b
c=a*b
If(mod(c,2.0)==0) then
   write(*,*) "Even"
else if(mod(c,2.0)==1) then
write(*,*) "Odd"
end if
stop
end program sample1


program sample1
implicit none

integer a,b
real c
read(*,*) a,b
c=a*b
If(mod(c,2.0)==0) then
   write(*,*) "Even"
else
write(*,*) "Odd"
end if
stop
end program sample1


module ABC086

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

  ! require all variables to be explicitly declared
  implicit none

  type obj_taskA

    ! data field
    integer(INT16), private :: a
    integer(INT16), private :: b

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

    procedure :: Product    => taskA_Product
    procedure :: IsProdEven => taskA_IsProdEven

  end type obj_taskA

  ! constructor
  interface obj_taskA
    module procedure taskA_constructor
  end interface obj_taskA

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

  function taskA_constructor () result(obj)

    ! return value of this <function>
    type(obj_taskA) :: obj

    read(unit=INPUT_UNIT, fmt=*) obj%a, obj%b

    return

  end function taskA_constructor

  pure function taskA_Product (obj_self) result(Product)

    ! arguments for this <function>
    class(obj_taskA), intent(in) :: obj_self

    ! return value of this <function>
    integer(INT16) :: Product

    Product = obj_self%a * obj_self%b

    return

  end function taskA_Product

  pure function taskA_IsProdEven (obj_self) result(stat)

    ! arguments for this <function>
    class(obj_taskA), intent(in) :: obj_self

    ! return value of this <function>
    logical :: stat

    stat = (mod( a=obj_self%product(), p=2_INT16 ) .eq. 0_INT16)

    return

  end function taskA_IsProdEven

end module ABC086

program main

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

  ! require all variables to be explicitly declared
  implicit none

  type(obj_taskA) :: obj_given_data

  ! STEP.01
  ! initialize the instace
  obj_given_data = obj_taskA()

  ! STEP.02
  ! compute & output the answer of ABC086 A
  if ( obj_given_data%IsProdEven() ) then
    write(unit=OUTPUT_UNIT, fmt='(A)', advance='yes') 'Even'
  else
    write(unit=OUTPUT_UNIT, fmt='(A)', advance='yes') 'Odd'
  end if

end program main


program dot
implicit none
integer a,b
read(*,*)a,b
if (MOD(a*b,2)==0) then
    write(*,*)"Even"
else
    write(*,*)"Odd"
end if
stop
end program dot


implicit none
integer:: a,b
read(5,*) a,b

if(mod(a*b,2) == 0) then
  write(6,*) "Even"
else
  write(6,*) "Odd"
endif

end


program product_
  implicit none
  integer :: a, b
  read(*,*) a, b
  if (mod(a*b,2) == 0) then
    write(*,'(a)') "Even"
  else
    write(*,'(a)') "Odd"
  end if
end program product_


      PROGRAM product
      IMPLICIT NONE
      INTEGER :: a,b,pro

      read*,a,b

      pro = a*b

      IF(mod(pro,2) == 0)THEN
        print'(A)', 'Even'
      ELSE
        print'(A)', 'Odd'
      END IF
      END PROGRAM


program test
implicit none
integer::a,b
read*,a,b
if(mod(a*b,2)==0) then
print*,'Even'
else
print*,'Odd'
end if
end program


program test
implicit none
integer::a,b
if(mod(a*b,2)==0) then
print*,'Even'
else
print*, 'Odd'
end if
end program


program A_product
 implicit none
 integer :: A, B, Ans
 read(*,*) A, B
 Ans = A*B
 if( mod(Ans, 2) == 0) then
 	write(*,*) "Even"
 else
 	write(*,*) "Odd"
 end if
end program A_product


! AtCoder Beginner Contest 086 A
! [URL]      https://atcoder.jp/contests/abc086/tasks/abc086_a
! [compiler] fortran (gfortran v4.8.4)
!
module ABC086

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

	! require all variables to be explicitly declared
	implicit none

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

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

	subroutine ABC086_A

		! variables for this <subroutine>
		character( len=4, kind=1 ) :: stat
		integer( kind= INT32 )     :: a, b

		! STEP.01
		! read out the target positive integers
		read *, a, b

		! STEP.02
		! judge whether the product of `a` and `b` is even or odd
		if( mod( a*b, 2 ) .eq. 0 ) then
			stat = 'Even'
		else
			stat = 'Odd'
		end if

		! STEP.03
		! output the result
		write( unit=OUTPUT_UNIT, fmt='(A)', advance='yes' ) stat

		! STEP.END
		return

	end subroutine ABC086_A

end module ABC086

program main

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

	! require all variables to be explicitly declared
	implicit none

	call ABC086_A

end program main


!ABC 086 A
implicit none
INTEGER :: a,b
READ(5,*) a,b

if (MOD(a*b,2)==0) then
    WRITE(6,*) "Even"
else
    WRITE(6,*) "Odd"
endif

end


program evenodddeer
	implicit none
    integer :: a,b

   	read(*,*) a,b
	if(mod((a*b),2)==0) then
    	write(*,'(a)') 'Even'
    else
    	write(*,'(a)') 'Odd'
    endif

end program evenodddeer


program product
  implicit none
  integer :: a, b
  read(*,*) a, b
  if (mod(a,2) == 0 .or. mod(b,2) == 0) then
     write(*,'(a)') "Even"
  else
     write(*,'(a)') "Odd"
  end if
  stop
end program product


integer a, b
read *, a, b

if(mod(a*b,2) == 0) then
    write(*,'(A)') 'Even'
else
    write(*,'(A)') 'Odd'
endif
end


program ABC_086_A_Product
   implicit none
   integer a, b
   read(*, *) a, b
   if( mod(a*b, 2) == 0 ) then
      write(*, *)'Even'
   else if( mod(a*b, 2) == 1 ) then
      write(*, *)'Odd'
   end if
end program ABC_086_A_Product


program ABC_086_A_Product
   implicit none
   integer a, b
   write(*, '(a)', advance = 'no')'input a, b : '
   read(*, *) a, b
   if( mod(a*b, 2) == 0 ) then
      write(*, *)'Even'
   else if( mod(a*b, 2) == 1 ) then
      write(*, *)'Odd'
   end if
end program ABC_086_A_Product


program ABC_086_A_Product
   implicit none
   integer a, b
   write(*, '(a)', advance = 'no')'input a : '
   read(*, *) a
   write(*, '(a)', advance = 'no')'input b : '
   read(*, *) b
   if( mod(a*b, 2) == 0 ) then
      write(*, *)'Even'
   else if( mod(a*b, 2) == 1 ) then
      write(*, *)'Odd'
   end if
end program ABC_086_A_Product


implicit none

integer:: a,b
read(5,*) a,b

if (mod(a*b,2)==0) then
    write(*,*) "Even"
else
    write(*,*) "Odd"
endif

end


program proA
  implicit none
  integer a,b,prod

  read(*,*) a,b
  prod=a*b
  if(mod(prod,2)==1) then
     write(*,*) 'Odd'
  else
     write(*,*) 'Even'
  end if
end program proA


program main
  integer flag, n, y
  flag = 0
  read *, n, y
  do i=0, n
     do j=0,n
        if(i+j<=n .and. 1000*i + 5000*j + 10000*(n-i-j)==y ) then
           print '(i5 i5 i5)', i, j, n-i-j
           stop
        end if
     end do
  end do
  print '(a)', "-1 -1 -1"
  stop
end program main

Score : 300 points

Problem Statement

The commonly used bills in Japan are 10000-yen, 5000-yen and 1000-yen bills. Below, the word "bill" refers to only these.

According to Aohashi, he received an otoshidama (New Year money gift) envelope from his grandfather that contained N bills for a total of Y yen, but he may be lying. Determine whether such a situation is possible, and if it is, find a possible set of bills contained in the envelope. Assume that his grandfather is rich enough, and the envelope was large enough.

Constraints

1 ≤ N ≤ 2000

1000 ≤ Y ≤ 2 × 10^7

N is an integer.

Y is a multiple of 1000.

Input

Input is given from Standard Input in the following format:

N Y

Output

If the total value of N bills cannot be Y yen, print -1 -1 -1.

If the total value of N bills can be Y yen, let one such set of bills be "x 10000-yen bills, y 5000-yen bills and z 1000-yen bills", and print x, y, z with spaces in between. If there are multiple possibilities, any of them may be printed.

Sample Input 1

9 45000

Sample Output 1

4 0 5

If the envelope contained 4 10000-yen bills and 5 1000-yen bills, he had 9 bills and 45000 yen in total. It is also possible that the envelope contained 9 5000-yen bills, so the output 0 9 0 is also correct.

Sample Input 2

20 196000

Sample Output 2

-1 -1 -1

When the envelope contained 20 bills in total, the total value would be 200000 yen if all the bills were 10000-yen bills, and would be at most 195000 yen otherwise, so it would never be 196000 yen.

Sample Input 3

1000 1234000

Sample Output 3

14 27 959

There are also many other possibilities.

Sample Input 4

2000 20000000

Sample Output 4

2000 0 0


implicit none
integer:: n,y,i,j

read(*,*) n,y

do i=0,n
do j=0,n
if(10000*i+5000*j+1000*(n-i-j)==y)then
write(*,*) i,j,n-i-j
goto 100
endif
end do
end do

write(*,*) -1,-1,-1

100 end


implicit none
integer:: n,y,i,j,k

read(*,*) n,y

do i=0,n
do j=0,n
do k=0,n
if(i+j+k==n.and.10000*i+5000*j+1000*k==y)then
write(*,*) i,j,k
goto 100
endif
end do
end do
end do

write(*,*) -1,-1,-1

100 end


program C085
    implicit none
    integer(8) :: N, Y
    integer(8) :: i, j, k, in, jn, kn, ju

    read(5,*) N, Y

    ju = 0
    do i = 0, N
        do j = 0, N-i
            k = N-i-j
            if (10000*i+5000*j+1000*k == Y) then
                ju = ju + 1
                in = i
                jn = j
                kn = k
            end if
        end do
    end do

    if (ju == 0) then
        write(6,*) "-1 -1 -1"
    else
        write(6,*) in, jn, kn
    end if

end program C085


program main
   implicit none
   INTEGER :: n, x, i, j, k
   READ(*, *) n, x
   do i = 0, n
      do j = 0, n - i
        k = n - i - j
        if(10000 * i + 5000 * j + 1000 * k == x) then
            WRITE(*, *) i, j, k
            goto 100
        endif
        enddo
    enddo
    WRITE(*, *) -1, -1, -1
    100 end
!end program main


program SLR
 implicit none
 integer::N,Y,ishota=0,ishotb=0,ishotc=0,p=0,q=0,r=0
 read(*,*)N,Y
 do ishota=0,N
  do ishotb=0,N-ishota
   do ishotc=0,N-ishota-ishotb
    if(10000*ishota+5000*ishotb+1000*ishotc==Y.and.ishota+ishotb+ishotc==N)then
     p=ishota
     q=ishotb
     r=ishotc
    end if
   end do
  end do
 end do
 if(p==0.and.q==0.and.r==0)then
  print'(i0,a,i0,a,i0)',-1," ",-1," ",-1
 else
  print'(i0,a,i0,a,i0)',p," ",q," ",r
 end if
end program SLR


      PROGRAM otoshidama
      IMPLICIT NONE
      INTEGER :: N,Y
      INTEGER :: a,b,c, s,ans(3)
      LOGICAL :: flag
      !a10000 b5000 c1000

      READ*,N ,Y

      flag = .FALSE.

      outer:DO a = 0,N
        DO b = 0,N-a
          c = N - a - b
          s = 10000*a + 5000*b + 1000*c
          IF(s==Y)THEN
            flag = .TRUE.
            ans(1)=a;ans(2)=b;ans(3)=c
          END IF
        END DO
      END DO outer

      IF(flag)THEN
        print'(I0,X,I0,X,I0)',ans(:)
      ELSE
        print'(A)','-1 -1 -1'
      END IF

      END PROGRAM


      PROGRAM otoshidama
      IMPLICIT NONE
      INTEGER :: N,Y
      INTEGER :: a,b,c, s
      LOGICAL :: flag
      !a10000 b5000 c1000

      READ*,N ,Y

      DO a = 0,N
        DO b = 0,N
          DO c = 0,N
            s = a*10000 + b*5000 + c*1000
            IF(Y==s)THEN
              IF(a+b+c==N)THEN
                flag = .TRUE.
                GOTO 10
              END IF
            END IF
          END DO
        END DO
      END DO

  10  IF(flag)THEN
        print'(I0,X,I0,X,I0)',a,b,c
      ELSE
        print'(A)','-1 -1 -1'
      END IF

      END PROGRAM


implicit none

integer :: n, y
integer :: i, j, k

read *, n, y

y = y / 1000

do i = 0, n
   do j = 0, n-i
      k = n - i - j
      if (i*10 + j*5 + k == y) then
         write(6,'(i0,1x,i0,1x,i0)') i,j,k
         stop
      endif
   enddo
enddo

write(6,'(a)') '-1 -1 -1'
end


program main
implicit none
integer :: i, j, k
integer :: n, y

read(*,*) n, y
do i = 0, n
   do j = 0, n-i
      k = n - i - j
      if( 1000*i + 5000*j + 10000*k == y ) then
         write(*,'(i0,a1,i0,a1,i0)') k, ' ', j, ' ', i
         stop
      end if
   end do
end do
write(*,'(i0,a1,i0,a1,i0)') -1, ' ', -1, ' ', -1

end program main


program main
implicit none
integer :: i, j, k
integer :: n, y

read(*,*) n, y
do i = 0, n
   do j = 0, n-i
      k = n - i - j
      if( 1000*i + 5000*j + 10000*k == y ) then
         write(*,'(i0,a1,i0,a1,i0)') k, ' ', i, ' ', j
         stop
      end if
   end do
end do
write(*,'(i0,a1,i0,a1,i0)') -1, ' ', -1, ' ', -1

end program main


program test
implicit none
integer::N,Y,i,j,check
read(*,*)N,Y
check=0
do i=0,N
	do j=0,N-i
    	if(i*10000+j*5000+(N-i-j)*1000==Y)then
    	print*,i,j,(N-i-j)
    	check=1
    	exit
    	end if
    end do
    if(check==1)exit
end do
if(check==0)then
print*,-1,-1,-1
end if
end program


program main
    read(*, *)n,m
    do i=0,n
       do j=0,n-i
          k = n - i - j
          if(i*10000 + j*5000 + k*1000 /= m)cycle
          write(*, *)i, j, k
          stop
       enddo
    enddo
    write(*, *) -1, -1, -1
end


program main
	integer N, Y
	read(*, *) N, Y
	do i = 0, N
		do j = 0, N-i
			if(Y == 1000*i + 5000*j + 10000*(N-i-j)) then
				write(*, *) N-i-j, j, i
				goto 1
			end if
		end do
	end do
1 continue
	if(Y /= 1000*i + 5000*j + 10000*(N-i-j)) then
		write(*, *) -1, -1, -1
	end if
end program main


implicit none
integer(8):: n,y,i,j

read(5,*) n,y

do i=0,n
    do j=0,n-i
    if (10000*i+5000*j+1000*(n-i-j)==y) then
        write(6,*) i,j,n-i-j
        goto 100
 endif

 enddo
enddo

 write(6,*) -1,-1,-1

100 end


program otoshidama
  implicit none
  integer i,j,man,sen,gosen
  integer*8 n,total

  read(*,*) n,total
  total=total/1000

  do man = 0,n
     do gosen = 0,n-man
        sen = n - man - gosen
        select case(total-sen-5*gosen-10*man)
        case(0)
           write(*,*) man,gosen,sen
           stop
        end select
     end do
  end do

  write(*,*) -1,-1,-1

end program otoshidama


program ABC085D
    implicit none
    integer(8)::N,H,i
    integer(8)::result=0
    integer(8),allocatable,dimension(:)::A,B,A_sort,B_sort
    read(5,*)N,H
    allocate(A(N))
    allocate(A_sort(N))
    allocate(B(N))
    allocate(B_sort(N))

    do i=1,N
        read(5,*)A_sort(i),B_sort(i)
    end do

    A=A_sort
    B=B_sort
    A_sort=iqsort(A_sort)
    B_sort=iqsort(B_sort)

    do i=1,N
        if(H<=0)then
            exit
        end if

        if(B(i)>=A_sort(N))then
            H=H-B(i)
            result=result+1
        end if
    end do

    if(H>0)then
        result=result+H/A_sort(N)

        if(mod(H,A_sort(N))/=0)then
            result=result+1
        end if
    end if

    print'(i0)',result

contains
    recursive function iqsort(ix) result(ires)
      integer(8), allocatable :: ires(:)
      integer(8), intent(in)  :: ix(:)
      integer :: k
      if (size(ix) <= 1) then
        ires = ix
      else
        k = ix(size(ix) / 2)
        ires = [iqsort(pack(ix, ix < k)), pack(ix, ix == k), iqsort(pack(ix, ix > k))]
      end if
    end function iqsort
end program ABC085D

Score : 400 points

Problem Statement

You are going out for a walk, when you suddenly encounter a monster. Fortunately, you have N katana (swords), Katana 1, Katana 2, …, Katana N, and can perform the following two kinds of attacks in any order:

Wield one of the katana you have. When you wield Katana i (1 ≤ i ≤ N), the monster receives a_i points of damage. The same katana can be wielded any number of times.

Throw one of the katana you have. When you throw Katana i (1 ≤ i ≤ N) at the monster, it receives b_i points of damage, and you lose the katana. That is, you can no longer wield or throw that katana.

The monster will vanish when the total damage it has received is H points or more. At least how many attacks do you need in order to vanish it in total?

Constraints

1 ≤ N ≤ 10^5

1 ≤ H ≤ 10^9

1 ≤ a_i ≤ b_i ≤ 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

N H
a_1 b_1
:
a_N b_N

Output

Print the minimum total number of attacks required to vanish the monster.

Sample Input 1

1 10
3 5

Sample Output 1

3

You have one katana. Wielding it deals 3 points of damage, and throwing it deals 5 points of damage. By wielding it twice and then throwing it, you will deal 3 + 3 + 5 = 11 points of damage in a total of three attacks, vanishing the monster.

Sample Input 2

2 10
3 5
2 6

Sample Output 2

2

In addition to the katana above, you also have another katana. Wielding it deals 2 points of damage, and throwing it deals 6 points of damage. By throwing both katana, you will deal 5 + 6 = 11 points of damage in two attacks, vanishing the monster.

Sample Input 3

4 1000000000
1 1
1 10000000
1 30000000
1 99999999

Sample Output 3

860000004

Sample Input 4

5 500
35 44
28 83
46 62
31 79
40 43

Sample Output 4

9


program ABC085D
    implicit none
    integer(16)::N,H,i
    integer(16)::result=0
    integer(16),allocatable,dimension(:)::A,B,A_sort,B_sort
    read(5,*)N,H
    allocate(A(N))
    allocate(A_sort(N))
    allocate(B(N))
    allocate(B_sort(N))

    do i=1,N
        read(5,*)A_sort(i),B_sort(i)
    end do

    A=A_sort
    B=B_sort
    A_sort=iqsort(A_sort)
    B_sort=iqsort(B_sort)

    do i=1,N
        if(B(i)>A_sort(N))then
            H=H-B(i)
            result=result+1
        end if

        if(H<=0)then
            exit
        end if
    end do

    if(H>0)then
        result=result+H/A_sort(N)

        if(mod(H,A_sort(N))/=0)then
            result=result+1
        end if
    end if

    print'(i0)',result

contains
    recursive function iqsort(ix) result(ires)
      integer(16), allocatable :: ires(:)
      integer(16), intent(in)  :: ix(:)
      integer :: k
      if (size(ix) <= 1) then
        ires = ix
      else
        k = ix(size(ix) / 2)
        ires = [iqsort(pack(ix, ix < k)), pack(ix, ix == k), iqsort(pack(ix, ix > k))]
      end if
    end function iqsort
end program ABC085D


program ABC085D
    implicit none
    integer(8)::N,H,i
    integer(8)::result=0
    integer(8),allocatable,dimension(:)::A,B,A_sort,B_sort
    read(5,*)N,H
    allocate(A(N))
    allocate(A_sort(N))
    allocate(B(N))
    allocate(B_sort(N))

    do i=1,N
        read(5,*)A_sort(i),B_sort(i)
    end do

    A=A_sort
    B=B_sort
    A_sort=iqsort(A_sort)
    B_sort=iqsort(B_sort)

    do i=1,N
        if(B(i)>A_sort(N))then
            H=H-B(i)
            result=result+1
        end if

        if(H<=0)then
            exit
        end if
    end do

    if(H>0)then
        result=result+H/A_sort(N)

        if(mod(H,A_sort(N))/=0)then
            result=result+1
        end if
    end if

    print'(i0)',result

contains
    recursive function iqsort(ix) result(ires)
      integer(8), allocatable :: ires(:)
      integer(8), intent(in)  :: ix(:)
      integer :: k
      if (size(ix) <= 1) then
        ires = ix
      else
        k = ix(size(ix) / 2)
        ires = [iqsort(pack(ix, ix < k)), pack(ix, ix == k), iqsort(pack(ix, ix > k))]
      end if
    end function iqsort
end program ABC085D


program katana
  implicit none
  integer*8 i
  integer*8 n,h,huru_max,nageru_max
  integer*8,allocatable :: huru(:),nageru(:)

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

  do i=1,n
     read(*,*) huru(i),nageru(i)
  end do

  i=0
  huru_max=maxval(huru)
  nageru_max=maxval(nageru)
  do while(h>0 .and. nageru_max>huru_max)
     i=i+1
     h=h-maxval(nageru)
     nageru(maxloc(nageru))=0
     nageru_max=maxval(nageru)
  end do

  if(h<=0)then
     write(*,*) i
  else
     if(mod(h,huru_max)==0) then
        write(*,*) i+h/huru_max
     else
        write(*,*) i+h/huru_max+1
     end if
  end if

end program katana


program main
  implicit none

  integer :: n,a,b,m,i,j,s,h,sn,cou=0,o

  read(*,*)n,a,b

  do i = 1, n
    s = 0
    if (i <= 9) then
      s = i
    else if (i <= 99) then
      j = int(i/10)
      o = i - j*10
      s = j + o
    else if (i <= 999) then
      h = int(i/100)
      j = int((i - h*100)/10)
      o = i - h*100 - j*10
      s = h+j+o
    else if (i <= 9999) then
      sn = int(i/1000)
      h = int((i - sn*1000)/100)
      j = int((i - sn*1000 - h*100)/10)
      o = i - sn*1000 - h*100 - j*10
      s = sn+h+j+o
    else
      s = 1
    end if

    if ((a <= s) .and. (s <= b)) then
      cou = cou + i
    end if
  end do

  write(*,*)cou
end program main

Score : 200 points

Problem Statement

Find the sum of the integers between 1 and N (inclusive), whose sum of digits written in base 10 is between A and B (inclusive).

Constraints

1 \leq N \leq 10^4

1 \leq A \leq B \leq 36

All input values are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print the sum of the integers between 1 and N (inclusive), whose sum of digits written in base 10 is between A and B (inclusive).

Sample Input 1

20 2 5

Sample Output 1

84

Among the integers not greater than 20, the ones whose sums of digits are between 2 and 5, are: 2,3,4,5,11,12,13,14 and 20. We should print the sum of these, 84.

Sample Input 2

10 1 2

Sample Output 2

13

Sample Input 3

100 4 16

Sample Output 3

4554


      PROGRAM someSums
      IMPLICIT NONE
      INTEGER :: N,A,B, ans
      INTEGER :: num(4), digit, i, piyo, aa,bb,cc,dd , s

      READ*,N,A,B
      ans = 0

      num(:) = 0
      digit = 1000
      piyo = N
      DO i = 1,4
        DO while(.TRUE.)
          IF(piyo>=digit)THEN
            piyo = piyo - digit
            num(i) = num(i) + 1
          ELSE
            digit = digit / 10
            EXIT
          END IF
        END DO
      END DO

      alldo:DO aa = 0,9
        DO bb = 0,9
          DO cc = 0,9
            DO dd = 0,9
              s = aa + bb + cc + dd
              IF(A<=s.AND.B>=s)THEN
                ans = ans + aa*1000+bb*100+cc*10+dd
              END IF
              IF(dd==num(4).AND.cc==num(3).AND. &
                bb==num(2).AND.aa==num(1))THEN
                EXIT alldo
              END IF
            END DO
          END DO
        END DO
      END DO alldo

      print'(I0)',ans

      END PROGRAM


      PROGRAM someSums
      IMPLICIT NONE
      INTEGER :: N,A,B, ans
      INTEGER :: num(4), digit, i, piyo, aa,bb,cc,dd , s

      READ*,N,A,B
      ans = 0

      num(:) = 0
      digit = 1000
      piyo = N
      DO i = 1,4
        DO while(.TRUE.)
          IF(piyo>=digit)THEN
            piyo = piyo - digit
            num(i) = num(i) + 1
          ELSE
            digit = digit / 10
            EXIT
          END IF
        END DO
      END DO

      alldo:DO aa = 0,9
        DO bb = 0,9
          DO cc = 0,9
            DO dd = 0,9
              s = aa + bb + cc + dd
              IF(A<=s.AND.B>=s)THEN
                ans = ans + aa*1000+bb*100+cc*10+dd
              END IF
              IF(dd==num(4).AND.cc==num(3).AND. &
              bb==num(2).AND.aa==num(1))THEN
              EXIT alldo
              END IF
            END DO
          END DO
        END DO
      END DO alldo

      print'(I0)',ans

      END PROGRAM


      PROGRAM someSums
      IMPLICIT NONE
      INTEGER :: N,A,B, ans
      INTEGER :: num(4), digit, i, piyo, aa,bb,cc,dd , s

      READ*,N,A,B
      ans = 0

      num(:) = 0
      digit = 1000
      piyo = N
      DO i = 1,4
        DO while(.TRUE.)
          IF(piyo>=digit)THEN
            piyo = piyo - digit
            num(i) = num(i) + 1
          ELSE
            digit = digit / 10
            EXIT
          END IF
        END DO
      END DO

      DO aa = 0,9
        DO bb = 0,9
          DO cc = 0,9
            DO dd = 0,9
              s = aa + bb + cc + dd
              IF(A<=s.AND.B>=s)THEN
                ans = ans + aa*1000+bb*100+cc*10+dd
              END IF
              IF(dd==num(4).AND.cc==num(3).AND. &
              bb==num(2).AND.aa==num(1))THEN
                print'(I0)',ans
                STOP
              END IF
            END DO
          END DO
        END DO
      END DO

      END PROGRAM


program test
	implicit none
    integer::a,b,N,ans,i,x,y
    read*,N,a,b
    ans=0
    do i=1,N
    	x=i
        y=0
    	do while(x>0)
        	y=mod(x,10)+y
        	x=x/10
        end do
        if(y>=a .and. b>=y)then
        	ans=ans+i
        end if
    end do
    print*,ans
end program test


implicit none
INTEGER :: n,i,j,count,a,b,keta_sum,s_int
CHARACTER(5) :: s
READ(5,*) n,a,b

count = 0

do i=1,n
    keta_sum = 0
    WRITE(s,"(i0)") i

    do j=1,len(s)
        if (s(j:j)/="") then
            READ(s(j:j),*) s_int
            keta_sum = keta_sum+s_int
        endif
    enddo

    if (keta_sum>=a .and. b>=keta_sum) then
        count = count+i
    endif

enddo

WRITE(6,*) count

end


program A081
    implicit none
    character(3) :: s
    integer(4) :: i, sum

    read(5,*) s
    sum = 0
    do i = 1, 3
        if (s(i:i) == "1") then
            sum = sum + 1
        end if

    end do

    write(6,*) sum

end program A081

Score : 100 points

Problem Statement

Snuke has a grid consisting of three squares numbered 1, 2 and 3.
In each square, either 0 or 1 is written. The number written in Square i is s_i.

Snuke will place a marble on each square that says 1.
Find the number of squares on which Snuke will place a marble.

Constraints

Each of s_1, s_2 and s_3 is either 1 or 0.

Input

Input is given from Standard Input in the following format:

s_{1}s_{2}s_{3}

Output

Print the answer.

Sample Input 1

101

Sample Output 1

2

A marble will be placed on Square 1 and 3.

Sample Input 2

000

Sample Output 2

0

No marble will be placed on any square.


program main
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: ans,i
    character(3):: s
    read*, s
    ans=0
    do i=1,3
        if (s(i:i) == '1') ans=ans+1
    end do
    print'(i0)', ans
end program main


read (*, '(b3)') i
print *, popcnt(i)
end


      PROGRAM A
      IMPLICIT NONE
      INTEGER :: input(3),ans
      INTEGER :: index

      read'(I1,I1,I1)', input(1),input(2),input(3)

      ans = 0
      DO index = 1,3
        IF(input(index)==1)THEN
        ans = ans + 1
        END IF
      END DO

      print'(I0)',ans

      END PROGRAM


program test
implicit none
integer::c,i
character::s*3

read*,s
c=0

do i=1,3
	if (s(i:i)=='1') then
    	c=c+1
    else
    	c=c
    end if
end do

print*,c

end program


program tenquestiontwo
 implicit none
 character(3) s
 integer i, n, res
 res = 0
 read(*,*) s
  do 10 i = 1,3
  read(s(i:i),*) n
   if (n.EQ.1) then
   res = res + 1
   end if
  10 continue
 write(*,*) res
end program


module global_variables
end module global_variables
program main
  use global_variables
  implicit none
  integer i
  integer a(3)
  character(3) s
  read *, s
!  print '(a)', s
  do i=1,3
    read(s(i:i),*) a(i)
  enddo
  print '(i0)', sum(a)
end program main


module global_variables
end module global_variables
program main
  use global_variables
  implicit none
  integer i,res
  character(3) s
  read *, s
  res = 0
  do i=1,3
    if (s(i:i) .eq. '1') then
      res = res + 1
    endif
  enddo
  print '(i0)', res
end program main


module ABC081

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

	! require all variables to be explicitly declared
	implicit none

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

	! constants for this <module>
	integer( kind=INT32 ), parameter, private :: num_char = 3

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

	subroutine ABC081_A

		! variables for this <subroutine>
		character(len=num_char, kind=1) :: buffer
		integer( kind=INT16 )           :: itr, count

		! STEP.01
		! read out the target positive integers
		read *, buffer

		! STEP.02
		! count the number of the `1`
		count = 0

		do itr = 1, num_char, 1
			if( ichar( buffer(itr:itr) ) .eq. ichar('1') ) count = count+1
		end do

		! STEP.03
		! output the result
		write( unit=OUTPUT_UNIT, fmt='(I0)', advance='yes' ) count

		! STEP.END
		return

	end subroutine ABC081_A

end module ABC081

program main

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

	! require all variables to be explicitly declared
	implicit none

	call ABC081_A

end program main


implicit none
INTEGER :: i
INTEGER,DIMENSION(3) :: a
CHARACTER :: s*3

READ(5,*) s

do i=1,3
    READ(s(i:i),*) a(i)
enddo

WRITE(*,*) sum(a)

end


program snuke
	implicit none
	!character(len=3) :: Masu
    integer ::i,j,Masu
    j=0
    read(*,*) Masu
  	do i=1,3
    	!write(*,*) mod(Masu/(10**(i-1)),10)
    	if(mod(Masu/(10**(i-1)),10)==1) then
     		j=j+1
        else
        endif
	end do
    write(*,'(i0)') j
end program snuke


program main
   read(*,'(b3)') i
   write(*,*) sum([(ibits(i, j, 1), j=0,2)])
end


character(3) s
integer i, j
read *, s

j = 0
do i = 1,3
    if(s(i:i) == '1') then
        j = j+1
    endif
enddo
write(*,'(I0)') j
end


implicit none
integer:: a,b,c
character:: s*3
read(5,*) s

read(s(1:1),*) a
read(s(2:2),*) b
read(s(3:3),*) c

write(*,*) a+b+c

end


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

    read(*,*) s
    read(*,*)t
    n=len_trim(s)
    m=len_trim(t)
    if(m>n)then
        write(*,*)'UNRESTORABLE'
        stop
    end if
    a=-1
    do i=n,m,-1
        do j=m,1,-1
            if(s(i-m+j:i-m+j) .ne. t(j:j) .and. s(i-m+j:i-m+j) .ne. '?')then
                exit
            elseif(j==1)then
                a=i-m
                s(i-m+1:i)=t
            end if
        end do
        if(a>=0)then
            exit
        elseif(a==-1 .and. i==m)then
            write(*,*)'UNRESTORABLE'
            stop
        end if
    end do
    do i=1,n
        if(s(i:i) .eq. '?')then
            s(i:i)='a'
        end if
    end do
    write(*,*)s
    stop
end program sample

Score : 300 points

Problem Statement

E869120 found a chest which is likely to contain treasure.

However, the chest is locked. In order to open it, he needs to enter a string S consisting of lowercase English letters.

He also found a string S', which turns out to be the string S with some of its letters (possibly all or none) replaced with ?.

One more thing he found is a sheet of paper with the following facts written on it:

Condition 1: The string S contains a string T as a contiguous substring.

Condition 2: S is the lexicographically smallest string among the ones that satisfy Condition 1.

Print the string S.

If such a string does not exist, print UNRESTORABLE.

Constraints

1 \leq |S'|, |T| \leq 50

S' consists of lowercase English letters and ?.

T consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S
T'

Output

Print the string S.

If such a string does not exist, print UNRESTORABLE instead.

Sample Input 1

?tc????
coder

Sample Output 1

atcoder

There are 26 strings that satisfy Condition 1: atcoder, btcoder, ctcoder,..., ztcoder.
Among them, the lexicographically smallest is atcoder, so we can say S = atcoder.

Sample Input 2

??p??d??
abc

Sample Output 2

UNRESTORABLE

There is no string that satisfies Condition 1, so the string S does not exist.


program abc076
    use,intrinsic :: iso_fortran_env
    implicit none
    character(50):: s,t
    character(50):: pred_s, ans
    integer(int32):: sn,tn,i,j
    logical:: contain

    read*, s
    read*, t
    sn = len_trim(s)
    tn = len_trim(t)
    ans = 'zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz'
    do i=1, (sn-tn+1)
        contain = .true.
        do j=1,tn
            if (s(i+j-1:i+j-1) == '?') cycle
            if (s(i+j-1:i+j-1) /= t(j:j)) contain = .false.
        end do
        if (.not. contain) cycle
        pred_s = s(1:i-1) // trim(t(:)) // s(i+tn:sn)
        do j=1,sn
            if (pred_s(j:j) == '?') pred_s(j:j) = 'a'
        end do
        ans = min(ans,pred_s)
    end do
    if (ans /= 'zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz') then
        print'(a)', trim(ans)
    else
        print'(a)', 'UNRESTORABLE'
    end if
end program abc076


program abc076
    use,intrinsic :: iso_fortran_env
    implicit none
    character(50):: s,t
    character(50):: pred_s, ans
    integer(int32):: sn,tn,i,j
    logical:: contain

    read*, s
    read*, t
    sn = len_trim(s)
    tn = len_trim(t)
    ans = 'zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz'
    do i=1, (sn-tn+1)
        contain = .true.
        do j=1,tn
            contain = contain .and. (s(i+j-1:i+j-1) == t(j:j) .or. s(i+j-1:i+j-1) == '?')
        end do
        if (.not. contain) cycle
        pred_s = s(1:i-1) // trim( t(:)) // s(i+j:sn)
        do j=1,sn
            if (pred_s(j:j) == '?') pred_s(j:j) = 'a'
        end do
        ans = min(ans,pred_s)
    end do
    if (ans /= 'zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz') then
        print'(a)', trim(ans)
    else
        print'(a)', 'UNRESTORABLE'
    end if
end program abc076


character(50)S,T,ans
read*,S
read*,T
do i=len_trim(S)-len_trim(T)+1,1,-1
  if(check(S,T,i))then
    S(i:i+len_trim(T)-1)=trim(T)
    do j=1,Len_trim(S)
      if(S(j:j)=="?")S(j:j)="a"
    end do
    print"(A)",trim(S)
    stop
  endif
end do
print"(A)","UNRESTORABLE"
contains
pure function check(S,T,ind)
  implicit none
  character(50),intent(in)::S,T
  integer,intent(in)::ind
  integer::i
  logical::check
  check=.true.
  do i=1,len_trim(T)
    if(S(ind+i-1:ind+i-1)/=T(i:i).and.S(ind+i-1:ind+i-1)/="?")check=.false.
  end do
  return
end function
end


program main
  implicit none
  integer :: h,w,i,j
  integer,allocatable :: x(:,:)
  character(50) :: s,t
  character :: a

  read(*,*) h,w
  allocate(x(0:h+1,0:w+1))
  x(:,:)=0

  do i=1,h
     read(*,*) s

     do j=1,w
        if(s(j:j)=='#')then
           x(i,j)=-10
           x(i,j+1)=x(i,j+1)+1
           x(i,j-1)=x(i,j-1)+1
           x(i-1,j)=x(i-1,j)+1
           x(i-1,j+1)=x(i-1,j+1)+1
           x(i-1,j-1)=x(i-1,j-1)+1
           x(i+1,j)=x(i+1,j)+1
           x(i+1,j+1)=x(i+1,j+1)+1
           x(i+1,j-1)=x(i+1,j-1)+1
        endif
     enddo
  enddo

  do i=1,h
     t='0'
     do j=1,w
        if(x(i,j)<0)then
           t(j:j)='#'
        else
           write(a,'(i1)') x(i,j)
           t(j:j)=a
        endif
     enddo
     write(*,*) t
  enddo

end program main

Score : 200 points

Problem Statement

You are given an H × W grid.

The squares in the grid are described by H strings, S_1,...,S_H.

The j-th character in the string S_i corresponds to the square at the i-th row from the top and j-th column from the left (1 \leq i \leq H,1 \leq j \leq W).

. stands for an empty square, and # stands for a square containing a bomb.

Dolphin is interested in how many bomb squares are horizontally, vertically or diagonally adjacent to each empty square.

(Below, we will simply say "adjacent" for this meaning. For each square, there are at most eight adjacent squares.)

He decides to replace each . in our H strings with a digit that represents the number of bomb squares adjacent to the corresponding empty square.

Print the strings after the process.

Constraints

1 \leq H,W \leq 50

S_i is a string of length W consisting of # and ..

Input

Input is given from Standard Input in the following format:

H W
S_1
:
S_H

Output

Print the H strings after the process.

The i-th line should contain a string T_i of length W, where the j-th character in T_i corresponds to the square at the i-th row from the top and j-th row from the left in the grid (1 \leq i \leq H, 1 \leq j \leq W).

Sample Input 1

3 5
.....
.#.#.
.....

Sample Output 1

11211
1#2#1
11211

For example, let us observe the empty square at the first row from the top and first column from the left.

There is one bomb square adjacent to this empty square: the square at the second row and second column.

Thus, the . corresponding to this empty square is replaced with 1.

Sample Input 2

3 5
#####
#####
#####

Sample Output 2

#####
#####
#####

It is possible that there is no empty square.

Sample Input 3

6 6
#####.
#.#.##
####.#
.#..#.
#.##..
#.#...

Sample Output 3

#####3
#8#7##
####5#
4#65#2
#5##21
#4#310


program sample
    implicit none
    character(50)::s,t
    character::a
    integer(8) :: i,j,m,n,h,w
    integer(8),allocatable :: x(:,:),y(:)
    read(*,*)h,w
    allocate(x(0:h+1,0:w+1))
    x(:, :)=0
    do i=1,h
        read(*,*) s
        do j=1,w
            if (s(j:j) .eq. '#')then
                x(i,j)=-10
                x(i-1,j-1)=x(i-1,j-1)+1
                x(i,j-1)=x(i,j-1)+1
                x(i+1,j-1)=x(i+1,j-1)+1
                x(i-1,j)=x(i-1,j)+1
                x(i+1,j)=x(i+1,j)+1
                x(i-1,j+1)=x(i-1,j+1)+1
                x(i,j+1)=x(i,j+1)+1
                x(i+1,j+1)=x(i+1,j+1)+1
            end if
        end do
    end do
    do i=1,h
        t='0'
        do j=1,w
            if(x(i,j)<0)then
                t(j:j)='#'
            else
                write(a,'(i1)')x(i,j)
                t(j:j)=a
            end if

        end do
        write(*,'(a)') t

    end do

    stop
end program sample


program bomb
    implicit none
    integer :: h, w, i, c, j
    character(len=55), allocatable :: s(:), ans(:)

    read(*,*) h, w
    allocate(s(h),ans(h))
    do i = 1, h
       read(*,'(a)') s(i)
    end do

    call bombcount(s, h, w, ans)

    do i = 1, h
       write(*,'(a)') ' ' // trim(ans(i))
    end do
    deallocate(s, ans)

    stop
  contains

    subroutine bombcount(s, h, w, ans)
      implicit none
      character(len=55) :: s(h), ans(h)
      integer :: i, j, h, w, num(0:h+1,0:w+1)
      integer :: a, c

      num = 0
      do i = 1, h
         do j = 1, w
            ans(i)(j:j) = '0'
         end do
      end do

      do i = 1, h
         do j = 1, w
            if ( s(i)(j:j) == '#' ) then
               do a = i-1, i+1
                  do c = j-1, j+1
                     num(a,c) = num(a,c) + 1
                  end do
               end do
            end if
         end do
      end do

      do i = 1, h
         do j = 1, w
            if ( s(i)(j:j) == '#' ) then
               write(ans(i)(j:j),'(a)') '#'
            else
               write(ans(i)(j:j),'(i1)') num(i,j)
            end if
         end do
      end do

    end subroutine bombcount
  end program bomb


program bomb
  implicit none
  integer :: h, w, i, c, j
  character(len=55), allocatable :: s(:), ans(:)

  read(*,*) h, w
  allocate(s(h),ans(h))
  do i = 1, h
     read(*,'(a)') s(i)
  end do

  call bombcount(s, h, w, ans)

  do i = 1, h
     write(*,'(a)') trim(ans(i))
  end do
  deallocate(s, ans)

  stop
contains

  subroutine bombcount(s, h, w, ans)
    implicit none
    character(len=55) :: s(h), ans(h)
    integer :: i, j, h, w, num(0:h+1,0:w+1)
    integer :: a, c

    num = 0
    do i = 1, h
       do j = 1, w
          ans(i)(j:j) = '0'
       end do
    end do

    do i = 1, h
       do j = 1, w
          if ( s(i)(j:j) == '#' ) then
             do a = i-1, i+1
                do c = j-1, j+1
                   num(a,c) = num(a,c) + 1
                end do
             end do
          end if
       end do
    end do

    do i = 1, h
       do j = 1, w
          if ( s(i)(j:j) == '#' ) then
             write(ans(i)(j:j),'(a)') '#'
          else
             write(ans(i)(j:j),'(i1)') num(i,j)
          end if
       end do
    end do

  end subroutine bombcount
end program bomb


program bomb
  implicit none
  integer :: h, w, i, c, j
  character(len=50), allocatable :: s(:), ans(:)

  read(*,*) h, w
  allocate(s(h),ans(h))
  do i = 1, h
     read(*,*) s(i)
  end do

  call bombcount(s, h, w, ans)

  do i = 1, h
     write(*,'(a)') trim(ans(i))
  end do
  deallocate(s, ans)

  stop
contains

  subroutine bombcount(s, h, w, ans)
    implicit none
    character(len=50) :: s(h), ans(h)
    integer :: i, j, h, w, num(0:h+1,0:w+1)
    integer :: a, c

    num = 0
    do i = 1, h
       do j = 1, w
          ans(i)(j:j) = '0'
       end do
    end do

    do i = 1, h
       do j = 1, w
          if ( s(i)(j:j) == '#' ) then
             do a = i-1, i+1
                do c = j-1, j+1
                   num(a,c) = num(a,c) + 1
                end do
             end do
          end if
       end do
    end do

    do i = 1, h
       do j = 1, w
          if ( s(i)(j:j) == '#' ) then
             write(ans(i)(j:j),'(a)') '#'
          else
             write(ans(i)(j:j),'(i1)') num(i,j)
          end if
       end do
    end do

  end subroutine bombcount
end program bomb


program prob3
    implicit none
    character(len=1), dimension(:, :), allocatable :: S
    integer :: H, W, i, j, k, tmp, x, y
    integer, dimension(8) :: dx, dy
    dx = (/1, 1, 1, 0, 0,-1,-1,-1/)
    dy = (/1, 0,-1, 1,-1, 1, 0,-1/)

    read(*,*) H, W
    allocate(S(H, W))
    do i = 1, H
        do j = 1, W
            read(*,'(a)',advance='no') S(i, j)
        end do
        read(*,*)
    end do

    do i = 1, H
        do j = 1, W
            if(S(i, j) == '.') then
                tmp = 0

                do k = 1, 8
                    y = i + dy(k)
                    x = j + dx(k)
                    if(y > H .or. y < 1) then
                        cycle
                    end if
                    if(x > W .or. x < 1) then
                        cycle
                    end if

                    if(S(y, x) == '#')then
                        tmp = tmp + 1
                    end if

                end do
                S(i, j) = char(ichar('0') + tmp)
            end if
        end do
    end do

    do i = 1, H
        write(*,*) S(i, :)
    end do

    stop
contains
end program prob3


program bomb
  implicit none
  integer :: h, w, i, c, j
  character(len=50), allocatable :: s(:), ans(:)

  read(*,*) h, w
  allocate(s(h),ans(h))
  do i = 1, h
     read(*,*) s(i)
  end do

  do i = 1, h
     do j = 1, w
        call bombcount(s, i, j, h, w, ans)
     end do
  end do

  do i = 1, h
     write(*,'(a)') ans(i)
  end do

  stop
contains
  subroutine bombcount(s,i,j,h,w,ans)
    implicit none
    character(len=50) :: s(h), ans(h)
    integer :: i,j,h,w
    integer :: a, b, c, d, e, f, g

    if (s(i)(j:j) == '#' ) then
       write(ans(i)(j:j),'(a)') '#'
       return
    end if

    c = 0
    do a = j-1, j+1
       if (i == 1) then
          exit
       else if (a == 0) then
          cycle
       else if ( a == w+1 ) then
          cycle
       else
          if (s(i-1)(a:a) == '#' ) then
             c = c + 1
          end if
       end if
    end do

    if ( j /= 1 ) then
       if (s(i)(j-1:j-1) == '#' ) then
          c = c + 1
       end if
    end if

    if ( j /= w ) then
       if (s(i)(j+1:j+1) == '#' ) then
          c = c + 1
       end if
    end if

    do a = j-1, j+1
       if (i== h) then
          exit
       else if (a == 0) then
          cycle
       else if ( a == w+1 ) then
          cycle
       else
          if (s(i+1)(a:a) == '#' ) then
             c = c + 1
          end if
       end if
    end do

    write(ans(i)(j:j),'(i1)') c

    return
  end subroutine bombcount
end program bomb


program prob3
    implicit none
    character(len=1), dimension(:, :), allocatable :: S
    integer :: H, W, i, j, tmp

    read(*,*) H, W
    allocate(S(H, W))
    do i = 1, H
        do j = 1, W
            read(*,'(a)',advance='no') S(i, j)
        end do
        read(*,*)
    end do

    do i = 1, H
        do j = 1, W
            if(S(i, j) == '.') then
                tmp = 0
                if(i>=2) then
                    if(j>=2)then
                        if(S(i-1, j-1) == '#') then
                            tmp = tmp + 1
                        end if
                    end if
                    if(j<=W-1)then
                        if(S(i-1, j+1) == '#') then
                            tmp = tmp + 1
                        end if
                    end if
                    if(S(i-1, j) == '#') then
                        tmp = tmp + 1
                    end if
                end if
                if(i<=H-1) then
                    if(j>=2)then
                        if(S(i+1, j-1) == '#') then
                            tmp = tmp + 1
                        end if
                    end if
                    if(j<=W-1)then
                        if(S(i+1, j+1) == '#') then
                            tmp = tmp + 1
                        end if
                    end if
                    if(S(i+1, j) == '#') then
                        tmp = tmp + 1
                    end if
                end if
                if(j>=2)then
                    if(S(i, j-1) == '#') then
                        tmp = tmp + 1
                    end if
                end if
                if(j<=W-1)then
                    if(S(i, j+1) == '#') then
                        tmp = tmp + 1
                    end if
                end if
                S(i, j) = char(ichar('0') + tmp)
            end if
        end do
    end do

    do i = 1, H
        write(*,*) S(i, :)
    end do

    stop
contains
end program prob3


program name
    implicit none
    character(1),allocatable:: s(:,:)
    integer(4),allocatable:: num(:,:)
    integer(4):: h,w,i,j, dj,di

    read*, h,w
    allocate(s(w,h), num(0:w+1,0:h+1))
    num(:,:) = 0
    block
        character(w):: inpt_s
        do i=1,h
            read*, inpt_s
            do j=1,w
                s(j,i) = inpt_s(j:j)
                if (s(j,i) == '#') then
                    do di = -1,1
                        do dj = -1,1
                            if (di==0 .and. dj==0) cycle
                            num(j+dj,i+di) = num(j+dj,i+di) + 1
                        end do
                    end do
                end if
            end do
        end do
    end block

    do i=1,h
        do j=1,w
            if (s(j,i) == '.') s(j,i) = char(num(j,i) + ichar('0'))
        end do
        print'(a)', s(:,i)
    end do
end program name


program ABC075B
    implicit none
    integer(8)::H,W,i,j,k
    integer(8),allocatable,dimension(:,:)::O
    integer(8),dimension(8)::dx=(/-1,0,1,-1,1,-1,0,1/)
    integer(8),dimension(8)::dy=(/-1,-1,-1,0,0,1,1,1/)
    character(50)::S
    read(5,*)H,W
    allocate(O(W,H))
    O=0

    do i=1,H
        read(5,*)S
        do j=1,W
            if(S(j:j)=='#')then
                O(j,i)=-1
                do k=1,8
                    if(j+dx(k)>=1 .and. j+dx(k)<=W .and. i+dy(k)>=1 .and. i+dy(k)<=H .and. O(j+dx(k),i+dy(k))/=-1)then
                        O(j+dx(k),i+dy(k))=O(j+dx(k),i+dy(k))+1
                    end if
                end do
            end if
        end do
    end do

    do i=1,H
        do j=1,W
            if(O(j,i)==-1)then
                write(*,fmt='(a)',advance='no') "#"
            else
                write(*,fmt='(i0)',advance='no') O(j,i)
            end if
        end do
        write(*,fmt='(a)') ""
    end do

end program ABC075B


program test
implicit none
character(len=100) :: s(100),ans(100)
character :: char
integer(8) :: i,j,h,w,is_bomb(0:100,0:100)=0

read(*,*) h,w
do i=1,h
	read(*,*) s(i)
	do j=1,w
		if(s(i)(j:j) == "#") then
			is_bomb(i,j) = 1
		endif
	enddo
enddo

do i=1,h
	do j=1,w
		if(is_bomb(i,j) == 1)then
			ans(i)(j:j) = "#"
		else
			write(char,'(I0)') sum(is_bomb(i-1:i+1,j-1:j+1))
			ans(i)(j:j) = char
		endif
	enddo
enddo

do i=1,h
	write(*,*) ans(i)(1:w)
enddo

end program


program ABC073B
    implicit none
    integer(8)::N,l,r,i
    integer(8)::result=0
    read(5,*)N

    do i=1,N
        read(5,*)l,r
        result=result+r-l+1
    end do

    print'(i0)',result
end program ABC073B

Score : 200 points

Problem Statement

Joisino is working as a receptionist at a theater.

The theater has 100000 seats, numbered from 1 to 100000.

According to her memo, N groups of audiences have come so far, and the i-th group occupies the consecutive seats from Seat l_i to Seat r_i (inclusive).

How many people are sitting at the theater now?

Constraints

1≤N≤1000

1≤l_i≤r_i≤100000

No seat is occupied by more than one person.

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
l_1 r_1
:
l_N r_N

Output

Print the number of people sitting at the theater.

Sample Input 1

1
24 30

Sample Output 1

7

There are 7 people, sitting at Seat 24,25,26,27,28,29 and 30.

Sample Input 2

2
6 8
3 3

Sample Output 2

4


program main
  implicit none

  integer :: n,l,r,i,s=0

  read(*,*)n
  do i = 1, n
    read(*,*)l,r
    s = s + r - l + 1
  end do
  write(*,*)s
end program main


program main
 integer i,N,l,r,isum
 isum = 0
 read(*,*) N
 do i=1, N
   read(*,*)  l,r
   isum = isum + r - l + 1
 end do
 write(*,*) isum
end program main


program theater
  implicit none
  integer :: n, l, r, i, m
  read(*,*) n
  m = 0
  do i = 1, n
    read(*,*) l, r
    m = m + r - l + 1
  end do
  write(*,'(i0)') m
end program theater


INTEGER N,L,R,A
READ*,N
A=0
DO I=1,N
  READ*,L,R
  A=A+R-L+1
END DO
PRINT"(I0)",A
END


INTEGER N,L,R,A
READ*,N
A=0
DO I=1,N
  READ*,L,R
  A=A+(R-L+1)
END DO
PRINT"(I0)",A
END


program main
    implicit none
    integer N, i, ans
    integer :: l(1010) = 0
    integer :: r(1010) = 0
    ans = 0
    read *, N
    do i = 1,N
        read *, l(i), r(i)
        ans = ans + r(i) - l(i) + 1
    end do

    print *, ans
end program main


program ABC_073_B_Theater
   implicit none
   integer N, i
   integer, allocatable :: l(:), r(:)
   read(*, *) N
   allocate(l(1: N))
   allocate(r(1: N))
   do i = 1, N
       read(*, *) l(i), r(i)
   end do
   write(*, *) sum(r - l) + N
end program ABC_073_B_Theater


program main
	implicit none
	integer :: n,l(1000),r(1000),sv
	integer::i
	read(*,*)n
	do i=1,n
		read(*,*)l(i),r(i)
	enddo
	do i=1,n
		sv=sv+r(i)-l(i)+1
	enddo
	write(*,*)sv
end program main


character(100000) s
character(26):: abc = 'abcdefghijklmnopqrstuvwxyz'
integer i
read *, s
i = verify(abc, s)
if (i == 0) then
  print '(a)', 'None'
else
  print '(a)', abc(i:i)
end if
end

Score : 200 points

Problem Statement

You are given a string S consisting of lowercase English letters.
Find the lexicographically (alphabetically) smallest lowercase English letter that does not occur in S.
If every lowercase English letter occurs in S, print None instead.

Constraints

1 \leq |S| \leq 10^5 (|S| is the length of string S.)

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

Print the lexicographically smallest lowercase English letter that does not occur in S.
If every lowercase English letter occurs in S, print None instead.

Sample Input 1

atcoderregularcontest

Sample Output 1

b

The string atcoderregularcontest contains a, but does not contain b.

Sample Input 2

abcdefghijklmnopqrstuvwxyz

Sample Output 2

None

This string contains every lowercase English letter.

Sample Input 3

fajsonlslfepbjtsaayxbymeskptcumtwrmkkinjxnnucagfrg

Sample Output 3

d


program main
  implicit none
  character(100000) :: s
  logical :: appear(26)
  integer :: i, j
  read(*, *) s
  appear(:) = .false.
  do i = 1, len(trim(s))
    j = ichar(s(i:i)) - ichar("a") + 1
    appear(j) = .true.
  end do
  do i = 1, 26
    if (.not. appear(i)) then
      write(*, "(a)") char(i + ichar("a") - 1)
      stop 0
    end if
  end do
  write(*, "(a)") "None"
end program main


program main
    character :: c*(10**5)
    character(10**5) :: ch
    character :: anser(26)
    integer :: word(26) = 0
    integer :: max_array, a
    do i = 1, 26
        anser(i) = achar(iachar('a')+i-1)
    end do

    read(5, '(a)') c
    max_array = len(trim(c))
    ch = trim(c)
    do i=1,max_array
        a = iachar(ch(i:i)) - iachar('a') + 1
        word(a) = 1
    end do

    do i=1,26
        if (word(i) == 0) then
            print *, anser(i)
            exit
        end if
        if (i == 26) then
            print *, "None"
        end if
    end do
end program main


program main
    character :: c*(10**5)
    character(10**5) :: ch
    character :: anser(26)
    integer :: word(26) = 0
    integer :: max_array, a
    do i = 1, 26
        anser(i) = achar(iachar('a')+i-1)
    end do

    read(5, '(a)') c
    max_array = len(trim(c))
    ch = trim(c)
    do i=1,max_array
        a = iachar(ch(i:i)) - iachar('a') + 1
        word(a) = 1
    end do

    do i=1,26
        if (word(i) == 0) then
            print *, anser(i)
            exit
        end if
        if (i == 26) then
            print *, "none"
        end if
    end do
end program main


program name
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: n,i,mul4=0, odd=0,nm4
    integer(int32), allocatable:: a(:)

    read*, n
    allocate(a(n))
    read*, a(:)
    do i=1,n
        if (btest(a(i),0))then
            odd=odd+1
        else if (mod(a(i),4) == 0) then
            mul4=mul4+1
        end if
    end do

    nm4 = odd
    if (mul4+odd == n) nm4=nm4-1

    if (mul4 >= nm4) then
        print'(a)', 'Yes'
    else
        print'(a)', 'No'
    end if
end program name

Score : 400 points

Problem Statement

We have a sequence of length N, a = (a_1, a_2, ..., a_N).
Each a_i is a positive integer.

Snuke's objective is to permute the element in a so that the following condition is satisfied:

For each 1 ≤ i ≤ N - 1, the product of a_i and a_{i + 1} is a multiple of 4.

Determine whether Snuke can achieve his objective.

Constraints

2 ≤ N ≤ 10^5

a_i is an integer.

1 ≤ a_i ≤ 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

If Snuke can achieve his objective, print Yes; otherwise, print No.

Sample Input 1

3
1 10 100

Sample Output 1

Yes

One solution is (1, 100, 10).

Sample Input 2

4
1 2 3 4

Sample Output 2

No

It is impossible to permute a so that the condition is satisfied.

Sample Input 3

3
1 4 1

Sample Output 3

Yes

The condition is already satisfied initially.

Sample Input 4

2
1 1

Sample Output 4

No

Sample Input 5

6
2 7 1 8 2 8

Sample Output 5

Yes


program name
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: n,i,mul4=0, odd=0
    integer(int32), allocatable:: a(:)

    read*, n
    allocate(a(n))
    read*, a(:)
    do i=1,n
        if (btest(a(i),0))then
            odd=odd+1
        else if (mod(a(i),4) == 0) then
            mul4=mul4+1
        end if
    end do

    if (mul4 >= odd) then
        print'(a)', 'Yes'
    else
        if (mul4+1 == odd .and. mul4+odd == n) then
            print'(a)', 'Yes'
        else
            print'(a)', 'No'
        end if
    end if
end program name


integer N
integer,allocatable,dimension(:)::A
integer cnt4,cnt2,cntodd
read*,N
allocate(A(N))
read*,A
cnt4=0;cnt2=0;cntodd=0
do i=1,N
  select case(mod(A(i),4))
    case(0)
      cnt4=cnt4+1
    case(2)
      cnt2=cnt2+1
    case default
      cntodd=cntodd+1
  end select
end do
if(cnt2>0)cntodd=cntodd+1
print"(A)",trim(merge("Yes","No ",cntodd<=cnt4+1))
end


program name
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: n,k,i,nk,ans
    integer(int32), allocatable:: a(:), cnt(:), num(:)

    read*, n,k
    allocate(a(n))
    read*, a(:)
    allocate(cnt(maxval(a)), source=0)

    do i=1,n
        cnt(a(i))=cnt(a(i))+1
    end do
    deallocate(a)

    nk=0
    do i=1,size(cnt)
        if (cnt(i) /= 0) nk=nk+1
    end do

    allocate(num(nk))
    nk=0

    do i=1,size(cnt)
        if (cnt(i) /= 0) then
            nk=nk+1
            num(nk)=cnt(i)
        end if
    end do
    deallocate(cnt)
    call merge_sort(num,1,nk)
    ans=0
    i=0
    do while (nk-i > k)
        i=i+1
        ans=ans+num(i)
    end do
    print'(i0)',ans
contains
    recursive subroutine merge_sort(ar, fst, lst)
        integer(4),intent(inout):: ar(:)
        integer(4),intent(in):: fst,lst
        integer(4):: mdl

        if (lst-fst < 2) then
            if (ar(fst) > ar(lst)) call swap(ar(fst),ar(lst))
            return
        end if

        mdl = (fst+lst)/2
        call merge_sort(ar, fst, mdl)
        call merge_sort(ar, mdl+1, lst)
        call merge_(ar, fst, mdl, lst)
    end subroutine

    subroutine merge_(ar, fst, mdl, lst)
        integer(4),intent(inout):: ar(:)
        integer(4),intent(in):: fst, mdl, lst
        integer(4),allocatable:: tmp(:)
        integer(4):: li, ri, ti

        allocate(tmp(lst-fst+1))

        li=fst
        ri=mdl+1
        ti=1

        do while (li <= mdl .and. ri <= lst)
            if (ar(li) <= ar(ri)) then
                tmp(ti) = ar(li)
                li=li+1
            else
                tmp(ti) = ar(ri)
                ri=ri+1
            end if
            ti=ti+1
        end do

        if (li <= mdl) then
            tmp(ti:) = ar(li:mdl)
        else
            tmp(ti:) = ar(ri:lst)
        end if

        ar(fst:lst) = tmp(:)
        deallocate(tmp)
    end subroutine

    subroutine swap(x,y)
        integer(4),intent(inout):: x,y
        integer(4):: tmp
        tmp = x
        x = y
        y = tmp
    end subroutine
end program name

Score : 200 points

Problem Statement

Snuke has N integers. Among them, the smallest is A, and the largest is B.
We are interested in the sum of those N integers. How many different possible sums there are?

Constraints

1 ≤ N,A,B ≤ 10^9

A and B are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print the number of the different possible sums.

Sample Input 1

4 4 6

Sample Output 1

5

There are five possible sums: 18=4+4+4+6, 19=4+4+5+6, 20=4+5+5+6, 21=4+5+6+6 and 22=4+6+6+6.

Sample Input 2

5 4 3

Sample Output 2

0

Sample Input 3

1 7 10

Sample Output 3

0

Sample Input 4

1 3 3

Sample Output 4

1


program name
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: n,a,b

    read*, n,a,b

    print'(i0)', max(0_8,(b-a)*(n-2)+1)
end program name


program answer
  implicit none
  integer(8) :: i, count1, count2, n
  character(len=200000) :: s
  read(*,*) s
  n=len_trim(s)
  do i = 1, N
     if(s(i:i)=='A') then
        count1=i
        exit
     end if
  end do

  do i = 1, N
     if(S(i:i)=='Z') then
        count2=i
     end if
  end do

  write(*,*) count2-count1+1
  stop
 end program answer

Score : 200 points

Problem Statement

Snuke has decided to construct a string that starts with A and ends with Z, by taking out a substring of a string s (that is, a consecutive part of s).

Find the greatest length of the string Snuke can construct. Here, the test set guarantees that there always exists a substring of s that starts with A and ends with Z.

Constraints

1 ≦ |s| ≦ 200{,}000

s consists of uppercase English letters.

There exists a substring of s that starts with A and ends with Z.

Input

The input is given from Standard Input in the following format:

s

Output

Print the answer.

Sample Input 1

QWERTYASDFZXCV

Sample Output 1

5

By taking out the seventh through eleventh characters, it is possible to construct ASDFZ, which starts with A and ends with Z.

Sample Input 2

ZABCZ

Sample Output 2

4

Sample Input 3

HASFJGHOGAKZZFEGA

Sample Output 3

12


program prob2
    implicit none
    character(len=200005)::s
    integer::i, l, r, p
    read(*,*) s
    p = 0
    do i = 1, len_trim(s)
        if(s(i:i) .eq. 'A' .and. p == 0) then
            l = i
            p = 1
        end if
        if(s(i:i) .eq. 'Z') r = i
    end do
    write(*,*) r - l + 1
    stop
end program


program prob1
    implicit none
    integer :: n
    integer :: a, b
    integer :: i
    character(200005) :: s
    read(*,*) s
    n = len_trim(s)
    do i = 1, n
        if(s(i:i) == 'A') then
            a = i
        end if
        if(s(n-i+1:n-i+1) == 'Z') then
            b = n-i+1
        end if
    end do
    write(*,*) b-a+1

    stop
contains
end program prob1


implicit none
character(200000) :: s
integer :: n,a,z,i
read *,s
n = len_trim(s)
do i = 1, n
   if (s(i:i) == 'A') then
      a = i
      exit
   endif
enddo
do i = n, 1, -1
   if (s(i:i) == 'Z') then
      z = i
      exit
   endif
enddo
print '(i0)',z-a+1
end


program abc053b
  implicit none
  character(200000) s

  read *, s
  print '(i0)', scan(s, 'Z', .true.) - scan(s, 'A') + 1
end program abc053b


program answer
  implicit none

  integer :: K, S, i, j, count

  read(*,*) K, S

  count=0
  do i = 0, K
     do j = 0, S-i
        if(j<=K.and.S-i-j>=0.and.S-i-j<=K) then
           count=count+1
        end if
     end do
  end do
  write(*,*) count
 stop
end program answer

Score : 200 points

Problem Statement

You are given two integers K and S.

Three variable X, Y and Z takes integer values satisfying 0≤X,Y,Z≤K.

How many different assignments of values to X, Y and Z are there such that X + Y + Z = S?

Constraints

2≤K≤2500

0≤S≤3K

K and S are integers.

Input

The input is given from Standard Input in the following format:

K S

Output

Print the number of the triples of X, Y and Z that satisfy the condition.

Sample Input 1

2 2

Sample Output 1

6

There are six triples of X, Y and Z that satisfy the condition:

X = 0, Y = 0, Z = 2

X = 0, Y = 2, Z = 0

X = 2, Y = 0, Z = 0

X = 0, Y = 1, Z = 1

X = 1, Y = 0, Z = 1

X = 1, Y = 1, Z = 0

Sample Input 2

5 15

Sample Output 2

1

The maximum value of X + Y + Z is 15, achieved by one triple of X, Y and Z.


program answer
  implicit none

  integer :: K, S, i, j, count

  read(*,*) K, S

  if(S==3*K) then
     write(*,*) '1'
     stop
  else if(S==0) then
     write(*,*) '1'
     stop
  end if

  count=0
  if(S<=K) then
     do i = 0, S
        do j = 0, S-i
           if(S-i-j>=0.and.S-i-j<=K) then
              count=count+1
           end if
        end do
     end do
     write(*,*) count
     stop
  end if

  do i = 0, K
     do j = 0, S-K
        if(j<=S-i.and.S-i-j>=0.and.S-i-j<=K) then
           count=count+1
        end if
     end do
  end do

  write(*,*) count
  stop
 end program answer


program answer
  implicit none

  integer :: K, S, i, j, count

  read(*,*) K, S

  if(S==3*K) then
     write(*,*) '1'
     stop
  else if(S==0) then
     write(*,*) '1'
     stop
  end if

  count=0
  if(S<=K) then
     do i = 0, S
        do j = 0, S-i
           count=count+1
        end do
     end do
     write(*,*) count
     stop
  end if

  do i = 0, K
     do j = 0, S-K
        if(j<=S-i.and.S-i-j>=0.and.S-i-j<=K) then
           count=count+1
        end if
     end do
  end do

  write(*,*) count
  stop
 end program answer


program answer
  implicit none

  integer :: K, S, i, j, count

  read(*,*) K, S

  if(S==3*K) then
     write(*,*) '1'
     stop
  else if(S==0) then
     write(*,*) '1'
     stop
  end if

  count=0
  if(S<=K) then
     do i = 0, S
        do j = 0, S-i
           count=count+1
        end do
     end do
     write(*,*) count
     stop
  end if

  do i = 0, K
     do j = 0, min(S-i,K)
        count=count+1
     end do
  end do

  write(*,*) count
  stop
 end program answer


program answer
  implicit none

  integer :: K, S, i, j, count

  read(*,*) K, S

  if(S==3*K) then
     write(*,*) '1'
     stop
  else if(S==0) then
     write(*,*) '1'
     stop
  end if

  count=0
  do i = 0, K
     do j = 0, min(S-i,K)
        count=count+1
     end do
  end do

  write(*,*) count
  stop
 end program answer


program main
	implicit none
    integer(8)::k,s,ksmin,i,ans,j
    ans=0
    read(*,*)k,s
    ksmin=min(k,s)
    do i=0,ksmin
    	do j=0,min(s-i,k)
        	if(s-i-j<=k .and. 0<=s-i-j)then
            	ans=ans+1
            end if
        end do
    end do

    write(*,*) ans
    stop
end program main


program prob5
    implicit none
    integer :: K, S
    integer :: x, y
    integer :: ans

    read(*,*) K, S

    ans = 0
    do x = 0, K
        do y = 0, K
            if(x + y >= S - K .and. x + y <= S) then
                ans = ans + 1
            end if
        end do
    end do

    write(*,*) ans
    stop
contains
end program prob5


program sum_of_three_integers
  implicit none
  integer :: k, s, i, m = 0
  read(*,*) k, s
  do i = 0, min(k, s)
    m = m + max(0, min(k, s - i) - max(0, s - i - k) + 1)
  end do
  write(*,'(i0)') m
end program sum_of_three_integers


integer(16) :: s,k,x,y,z,ans=0

read*,k,s

do x = 0,k
do y = 0,k
  z = s - x - y
  if( 0<=z .and. z<=k )then
    ans = ans + 1
  end if
end do
end do

print*,ans
end program


program main
  implicit none
  integer(8) :: k, s, x, y, z, res
  read *, k, s
  res = 0
  do x = 0, k
    do y = 0, k
      z = s - x - y
      if (0 <= z .and. z <= k) res = res + 1
    end do
  end do
  print "(i0)", res
end program main


program ABC051B
    implicit none
    integer(8)::K,S,h,i,j
    integer(8)::result=0

    read(5,*) K,S

    do i=0,K
        do j=0,K
            if(S-i-j>=0 .and. S-i-j<=K)then
                result=result+1
            end if
        end do
    end do

    print'(i0)',result
end program ABC051B


program sum_of_three_integers
  implicit none
  integer :: k, s, i, m
  read(*,*) k, s
  m = 0
  do i = 0, min(k,s)
    m = m+max(0,min(k,s-i)-max(0,s-i-k)+1)
  end do
  write(*,'(i0)') m
  stop
end program sum_of_three_integers


program name

    implicit none
    integer K,S,X,Y,Z
    integer counter
    read*, K,S
    counter = 0
    do X = 0, K, 1
        do Y = 0, K, 1
            do Z = 0, K, 1
                if ( X+Y+Z==S ) then
                    counter = counter + 1
                end if
            end do
        end do
    end do
print*, counter
end program


program name

    implicit none
    integer K,S,X,Y,Z
    integer counter
    read*, K,S
    counter = 0
    do X = 1, K, 1
        do Y = 1, K, 1
            do Z = 1, K, 1
                if ( X+Y+Z==K ) then
                    counter = counter + 1
                end if
            end do
        end do
    end do
print*, counter
end program


  !Https://atcoder.jp/contests/abc051/tasks/abc051_b
program main
  implicit none
  integer :: K,S
  integer :: out,out2
  integer :: i,j,l
  read(*,*) K,S
  out = 0
  ! 重複なし
  do l = 0,K-2
  do j = l+1,K-1
  do i = j+1,K
     if (i+j+l == S ) out=out+1
  end do
  end do
  end do
  out = out*3*2
  out2=0
  do j = 0,K
  do i = j+1,K
     if (i+j+j == S ) then
        out2 = out2+1
        cycle
     end if
     if (i+i+j == S ) out2 = out2+1
  end do
  end do
  out = out + out2*3
  out2 = 0
  do i = 1,K
     if ( i*3 == S ) out2=out2+1
  end do
  out = out + out2
  write(*,'(i0)') out
  stop
end program main


  !https://atcoder.jp/contests/abc051/tasks/abc051_b
program main
  implicit none
  integer :: K,S
  integer :: out,out2
  integer :: i,j,l
  read(*,*) K,S
  out = 0
  ! 重複なし
  do l = 0,K-2
  do j = l+1,K
  do i = j+1,K
     if (i+j+l == S ) out=out+1
  end do
  end do
  end do
  out = out*3*2
  out2=0
  do j = 0,K
  do i = j+1,K
     if (i+j+j == S ) out2 = out2+1
     if (i+i+j == S ) out2 = out2+1
  end do
  end do
  out = out + out2*3
  out2 = 0
  do i = 1,K
     if ( i*3 == S ) out2=out2+1
  end do
  out = out + out2
  write(*,'(i0)') out
  stop
end program main


program ABC_051_B_SumofThreeIntegers
   implicit none
   integer X, Y, Z, K, S, countN
   read(*, *) K, S
   countN = 0
   do X = 0, K
       do Y = 0, K
	      Z = S - (X + Y)
		  if(X + Y + Z == S .and. Z >=0 .and. Z <= K ) then
			  countN = countN + 1
		  end if
	   end do
   end do
   write(*, *) countN
end program ABC_051_B_SumofThreeIntegers


program ABC_051_B_SumofThreeIntegers
   implicit none
   integer X, Y, Z, K, S, countN
   read(*, *) K, S
   countN = 0
   do X = 0, K
       do Y = 0, K
	      Z = S - (X + Y)
		  if(X + Y + Z == S) then
			  countN = countN + 1
		  end if
	   end do
   end do
   write(*, *) countN
end program ABC_051_B_SumofThreeIntegers


program	main
  implicit none
  integer k,s,i,j
  integer,allocatable::a(:,:)
  read*,k,s
  allocate(a(k+1,k+1))
  forall(i=0:k,j=0:k)a(i+1,j+1)=s-i-j

  print*,count(a.ge.0.and.a.le.k)
!  print*,a.ge.0

     !forall(j=0:k)a(i+1,j+1)=i+j
end program main


program	main
  implicit none
  integer k,s,i,j
  integer,allocatable::a(:,:)
  read*,k,s
  allocate(a(k+1,k+1))
  forall(i=0:k,j=0:k)a(i+1,j+1)=i+j

  print'(i0)',count(s-a(:,:)>=0.and.s.ge.0)
end program main


implicit none
character(10**5+5) s,s_rev
character(5):: a
character(5):: b
character(6):: c
character(7):: d
integer:: s_len,i,count

a='maerd'
b='esare'
c='resare'
d='remaerd'
s_rev=''

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

do i=1,s_len
s_rev(s_len+1-i:s_len+1-i)=s(i:i)
end do

count=0
do while (count<=s_len)
if(index(s_rev,a)==1)then
count=count+len(a)
s_rev=s_rev(len(a)+1:)

else if(index(s_rev,b)==1)then
count=count+len(b)
s_rev=s_rev(len(b)+1:)

else if(index(s_rev,c)==1)then
count=count+len(c)
s_rev=s_rev(len(c)+1:)

else if(index(s_rev,d)==1)then
count=count+len(d)
s_rev=s_rev(len(d)+1:)

else
write(*,*) 'NO'
goto 100
endif
end do

write(*,*) 'YES'

100 end

Score : 300 points

Problem Statement

You are given a string S consisting of lowercase English letters.
Another string T is initially empty.
Determine whether it is possible to obtain S = T by performing the following operation an arbitrary number of times:

Append one of the following at the end of T: dream, dreamer, erase and eraser.

Constraints

1≦|S|≦10^5

S consists of lowercase English letters.

Input

The input is given from Standard Input in the following format:

S

Output

If it is possible to obtain S = T, print YES. Otherwise, print NO.

Sample Input 1

erasedream

Sample Output 1

YES

Append erase and dream at the end of T in this order, to obtain S = T.

Sample Input 2

dreameraser

Sample Output 2

YES

Append dream and eraser at the end of T in this order, to obtain S = T.

Sample Input 3

dreamerer

Sample Output 3

NO


program C049
    implicit none
    character(100000) :: S
    integer(8) :: i, j, N

    read(5,*) S
    N = len_trim(S)

    i = 0
    j = 0

    do while(i < N)
        if (S(i+1:i+7) == "dreamer") then
            if (S(i+8:i+8) /= "a")then
                i = i + 7
            else
                i = i + 5
            end if
        else if (S(i+1:i+5) == "dream") then
            i = i + 5
        else if (S(i+1:i+6) == "eraser") then
            i = i + 6
        else if (S(i+1:i+5) == "erase") then
            i = i + 5
        else
            i = N
            j = 1
        end if
    end do

    if (j == 0) then
        write(6,*) "YES"
    else
        write(6,*) "NO"
    end if

end program C049


program abc049c
    use,intrinsic :: iso_fortran_env
    implicit none
    character(100000):: s
    character(:),allocatable:: dream, dreamer, erase, eraser
    integer(int32):: i

    dream = 'dream'
    dreamer = 'dreamer'
    erase = 'erase'
    eraser = 'eraser'

    read*, s

    do while(len_trim(s) > 0)

        i = index(s,dream//eraser)
        if (i > 0) then
            s=s(12:)
            cycle
        end if
        i = index(s,dream//erase)
        if (i > 0) then
            s=s(11:)
            cycle
        end if

        i = index(s,dreamer)
        if (i > 0) then
            s = s(8:)
            cycle
        end if

        i = index(s,dream)
        if (i > 0) then
            s = s(6:)
            cycle
        end if

        i = index(s,eraser)
        if (i > 0) then
            s = s(7:)
            cycle
        end if

        i = index(s,erase)
        if (i > 0) then
            s = s(6:)
            cycle
        end if

        print'(a)', 'NO'
        stop
    end do
    print'(a)', 'YES'
end program abc049c


program main
   implicit none
   character(100000) s,st
   integer le,i
   read(*,*)st
   le=len_trim(st)
   do i=1,le
      s(i:i)=st(le-i+1:le-i+1)
   enddo
   i=1
   do
    !  write(*,*)i
      if(i==le+1)then
         write(*,"(A3)")"YES"
         STOP
      endif
      if(s(i:i+4)=="maerd")then
      i=i+5
      cycle;
     endif
      if(s(i:i+6)=="remaerd")then
      i=i+7
      cycle;
     endif
     if(s(i:i+4)=="esare")then
      i=i+5
      cycle;
     endif
      if(s(i:i+5)=="resare")then
      i=i+6
      cycle;
     endif
     write(*,"(A2)")"NO"
     STOP

   enddo
end program main


program main
   implicit none
   character(100000) s,st
   integer le,i
   read(*,*)st
   le=len_trim(st)
   do i=1,le
      s(i:i)=st(le-i+1:le-i+1)
   enddo
   i=1
   do
    !  write(*,*)i
      if(i==le+1)then
         write(*,"(A3)")"YES"
         STOP
      endif
      if(s(i:i+4)=="maerd")then
      i=i+5
      cycle;
     endif
      if(s(i:i+6)=="remaerd")then
      i=i+7
      cycle;
     endif
     if(s(i:i+4)=="esare")then
      i=i+5
      cycle;
     endif
      if(s(i:i+5)=="resare")then
      i=i+6
      cycle;
     endif
     write(*,"(A2)")"No"
     STOP

   enddo
end program main


program main
   implicit none
   character(100000) s,st
   integer le,i
   read(*,*)st
   le=len_trim(st)
   do i=1,le
      s(i:i)=st(le-i+1:le-i+1)
   enddo
   i=1
   do
      !write(*,*)i
      if(i==le+1)then
         write(*,"(A3)")"YES"
         STOP
      endif
      if(s(i:i+4)=="maerd")then
      i=i+5
      cycle;
     endif
      if(s(i:i+6)=="remaerd")then
      i=i+7
      cycle;
     endif
     if(s(i:i+4)=="esare")then
      i=i+5
      cycle;
     endif
      if(s(i:i+5)=="resare")then
      i=i+6
      cycle;
     endif
     write(*,"(A2)")"No"
     STOP

   enddo
end program main