program main
    implicit none
    integer(8):: n, d, a, i, num, adble
    integer(8), allocatable:: x(:), h(:)
    integer(8), parameter:: one = 1

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

    do i = 1, n
        read*, x(i), h(i)
    end do

    call marge_sort(x(:), h(:), one, n)

    num = 0
    do i=1,n
        num = num + h(i)/a
        if (mod(h(i),a) /= 0) num = num + 1
        where (x < x(i)+2*d )
            h = h - num*a
        end where

        where (h(:) < 0)
            h = 0
        end where

        if (sum(h) == 0) exit
    end do

    print*, num

contains
recursive subroutine marge_sort(x,h, first, last)
    integer(8):: x(:), h(:), first, last
    integer(8):: tmp, mean

    if (last-first <= 1) then
        if (x(last) < x(first)) then
            tmp = x(last)
            x(last) = x(first)
            x(first) = tmp

            tmp = h(last)
            h(last) = h(first)
            h(first) = tmp
        end if

    else
        mean = (first+last)/2
        call marge_sort(x(:), h(:), first, mean)
        call marge_sort(x(:), h(:), mean+1, last)
        call marge(x(:), h(:), first, mean, last)

    end if
end subroutine

subroutine marge(x,h,first,mean,last)
    integer(8):: x(:), h(:), first, mean, last
    integer(8), allocatable:: tmpx(:), tmph(:)
    integer(8):: r, l, i

    allocate(tmpx(last-first+1), tmph(last-first+1))
    r = first
    l = mean + 1
    i = 1

    do while(r <= mean .and. l <= last)
        if (x(r) <= x(l)) then
            tmpx(i) = x(r)
            tmph(i) = h(r)
            i = i+1
            r = r+1
        else
            tmpx(i) = x(l)
            tmph(i) = h(l)
            i = i+1
            l = l+1
        end if
    end do

    if (r > mean)then
        tmpx(i:) = x(l:last)
        tmph(i:) = h(l:last)
    else
        tmpx(i:) = x(r:mean)
        tmph(i:) = h(r:mean)
    end if

    x(first:last) = tmpx(:)
    h(first:last) = tmph(:)

end subroutine
end program main

Score : 600 points

Problem Statement

Silver Fox is fighting with N monsters.

The monsters are standing in a row, and we can assume them to be standing on a number line. The i-th monster, standing at the coordinate X_i, has the health of H_i.

Silver Fox can use bombs to attack the monsters.
Using a bomb at the coordinate x decreases the healths of all monsters between the coordinates x-D and x+D (inclusive) by A.
There is no way other than bombs to decrease the monster's health.

Silver Fox wins when all the monsters' healths become 0 or below.

Find the minimum number of bombs needed to win.

Constraints

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

0 \leq D \leq 10^9

1 \leq A \leq 10^9

0 \leq X_i \leq 10^9

1 \leq H_i \leq 10^9

X_i are distinct.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N D A
X_1 H_1
:
X_N H_N

Output

Print the minimum number of bombs needed to win.

Sample Input 1

3 3 2
1 2
5 4
9 2

Sample Output 1

2

First, let us use a bomb at the coordinate 4 to decrease the first and second monsters' health by 2.

Then, use a bomb at the coordinate 6 to decrease the second and third monsters' health by 2.

Now, all the monsters' healths are 0.
We cannot make all the monsters' health drop to 0 or below with just one bomb.

Sample Input 2

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

Sample Output 2

5

We should use five bombs at the coordinate 5.

Sample Input 3

3 0 1
300000000 1000000000
100000000 1000000000
200000000 1000000000

Sample Output 3

3000000000

Watch out for overflow.


program silver_fox_vs_monster
  implicit none
  integer(8), parameter :: inf = 1001001001001_8
  integer :: n, i, l, r, m
  integer(8) :: d, a, x(0:200001,2) = 0, p = 0, t
  read(*,*) n, d, a
  do i = 1, n
    read(*,*) x(i,:)
  end do
  call merge_sort(x(1:n,:))
  x(0,1) = -inf
  x(n+1,1) = inf
  i = 1
  do while (i <= n)
    do while (x(i,2) <= 0 .and. i <= n)
      i = i+1
    end do
    if (i > n) exit
    l = i
    r = n+1
    do while (r-l > 1)
      m = (l+r)/2
      if (x(m,1) > x(i,1)+d*2) then
        r = m
      else
        l = m
      end if
    end do
    t = (x(i,2)+a-1)/a
    p = p+t
    x(i:l,2) = x(i:l,2)-t*a
  end do
  write(*,'(i0)') p
contains
  subroutine merge_sort(a)
    integer(8), intent(inout) :: a(:,:)
    integer :: n, m, l, i, u
    n = size(a,1)
    m = n
    l = 1
    do while (m > 1)
      do i = 1, m/2
        u = min(2*i*l,n)
        call merger(a(2*(i-1)*l+1:(2*i-1)*l,:),a((2*i-1)*l+1:u,:))
      end do
      l = 2*l
      m = (m+1)/2
    end do
  end
  subroutine merger(a1,a2)
    integer(8), intent(inout) :: a1(:,:), a2(:,:)
    integer(8) :: a(size(a1,1)+size(a2,1),size(a1,2))
    integer :: i1, i2, n1, n2
    i1 = 1
    i2 = 1
    n1 = size(a1,1)
    n2 = size(a2,1)
    do while (i1 <= n1 .and. i2 <= n2)
      if (a1(i1,1) < a2(i2,1)) then
        a(i1+i2-1,:) = a1(i1,:)
        i1 = i1+1
      else
        a(i1+i2-1,:) = a2(i2,:)
        i2 = i2+1
       end if
    end do
    if (i1 <= n1) then
      a(i1+i2-1:n1+n2,:) = a1(i1:n1,:)
    else
      a(i1+i2-1:n1+n2,:) = a2(i2:n2,:)
    end if
    a1 = a(1:n1,:)
    a2 = a(n1+1:n1+n2,:)
  end
end program silver_fox_vs_monster

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 sample
	implicit none
    integer :: x,y

    read(*,*) x,y

    if (x==y) then
    	write(*,*) 'Yes'
    else
    	write(*,*) 'No'
    end if

    stop
end program sample

Score : 100 points

Problem Statement

Takahashi is participating in a programming contest, AXC001. He has just submitted his code to Problem A.

The problem has N test cases, all of which must be passed to get an AC verdict.

Takahashi's submission has passed M cases out of the N test cases.

Determine whether Takahashi's submission gets an AC.

Constraints

1 \leq N \leq 100

0 \leq M \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

If Takahashi's submission gets an AC, print Yes; otherwise, print No.

Sample Input 1

3 3

Sample Output 1

Yes

All three test cases have been passed, so his submission gets an AC.

Sample Input 2

3 2

Sample Output 2

No

Only two out of the three test cases have been passed, so his submission does not get an AC.

Sample Input 3

1 1

Sample Output 3

Yes


program test03
 implicit none
 integer,allocatable :: a(:)
 integer N
 integer i, j, jj
 integer :: js=1
 integer :: yesf=0
 integer :: no=0

 read(5,*) N
 allocate(a(N))

 read(5,*) (a(i), i=1,N)

  if ( N >= 190000 ) then
   js=120000
  end if

 do j=js,N
  if ( j/=1 ) then
   do jj=js,j-1
    if ( a(jj) < a(j) ) then
     no = 1
     continue
    end if
   end do
  end if
!print *,no
 if ( no==0 ) then
  yesf=yesf+1
 end if
 no=0
end do

 write (*,fmt='(i0)',advance='no') yesf

endprogram test03


program test01
 implicit none
 integer a, b

 read(5,*) a, b

 if ( a == b ) then
  print *, 'Yes'
 else
  print *, 'No'
 end if

endprogram test01


program bbb
implicit none

integer(8) :: a, b, i, p, q, res

read*, a, b

p = max(a,b)
q = min(a,b)

res = 0
do i = 1, p
res = res + q*10**(i-1)
end do

print*, res

end program

Score : 200 points

Problem Statement

Given are 1-digit positive integers a and b. Consider these two strings: the concatenation of b copies of the digit a, and the concatenation of a copies of the digit b. Which of these is lexicographically smaller?

Constraints

1 \leq a \leq 9

1 \leq b \leq 9

a and b are integers.

Input

Input is given from Standard Input in the following format:

a b

Output

Print the lexicographically smaller of the two strings. (If the two strings are equal, print one of them.)

Sample Input 1

4 3

Sample Output 1

3333

We have two strings 444 and 3333. Between them, 3333 is the lexicographically smaller.

Sample Input 2

7 7

Sample Output 2

7777777


program test03_2
 implicit none
 integer,allocatable :: a(:)
 integer,allocatable :: b(:)
 integer N
 integer i, j, jj
 integer :: js(1)
 integer :: yesf=0
 integer :: no=0

 read(5,*) N
 allocate(a(N))

 read(5,*) (a(i), i=1,N)

 do j=1,N
 allocate(b(j))
 do jj=1,j
  b(jj)=a(jj)
 end do

!print *, minloc(b)
 js=minloc(b)
 if ( j == js(1) ) then
  yesf=yesf+1
 end if

 deallocate(b)
 end do

 write (*,fmt='(i0)',advance='no') yesf

endprogram test03_2

Score : 300 points

Problem Statement

Given is a permutation P_1, \ldots, P_N of 1, \ldots, N.
Find the number of integers i (1 \leq i \leq N) that satisfy the following condition:

For any integer j (1 \leq j \leq i), P_i \leq P_j.

Constraints

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

P_1, \ldots, P_N is a permutation of 1, \ldots, N.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
P_1 ... P_N

Output

Print the number of integers i that satisfy the condition.

Sample Input 1

5
4 2 5 1 3

Sample Output 1

3

i=1, 2, and 4 satisfy the condition, but i=3 does not - for example, P_i > P_j holds for j = 1.

Similarly, i=5 does not satisfy the condition, either. Thus, there are three integers that satisfy the condition.

Sample Input 2

4
4 3 2 1

Sample Output 2

4

All integers i (1 \leq i \leq N) satisfy the condition.

Sample Input 3

6
1 2 3 4 5 6

Sample Output 3

1

Only i=1 satisfies the condition.

Sample Input 4

8
5 7 4 2 6 8 1 3

Sample Output 4

4

Sample Input 5

1
1

Sample Output 5

1


program ddd
implicit none

integer :: n, i, p, q, r, x, y, res, chk, co
integer,dimension(99) :: t
character(10) :: c

t = 0
read*, n

if(n<10) then
print*, n
return
end if

do i = 10, n

write(c,'(i10)') i

c = adjustl(c)
r = len_trim(c)
read(c(1:1),*) x
read(c(r:r),*) y
p = max(x,y)
q = min(x,y)

if (q==0) cycle
t(10*x+y) = t(10*x+y) + 1

end do

chk = 0
res = 9
do i = 1, 9
co = 11*i
if (t(co)>0) then
t(co) = t(co) + 1
chk = chk + 1
res = res + t(co)**2
end if
end do

do i = 12, 99

if (t(i)==0) cycle
x = int(i*0.1)
y = i - 10*x

if (y==0) cycle
if (mod(i,11)==0) cycle

p = t((10*x) + y)
q = t((10*y) + x)
res = res + p*q
end do

print*, res - chk

end program

Score : 400 points

Problem Statement

Given is a positive integer N.

Find the number of pairs (A, B) of positive integers not greater than N that satisfy the following condition:

When A and B are written in base ten without leading zeros, the last digit of A is equal to the first digit of B, and the first digit of A is equal to the last digit of B.

Constraints

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

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

25

Sample Output 1

17

The following 17 pairs satisfy the condition: (1,1), (1,11), (2,2), (2,22), (3,3), (4,4), (5,5), (6,6), (7,7), (8,8), (9,9), (11,1), (11,11), (12,21), (21,12), (22,2), and (22,22).

Sample Input 2

1

Sample Output 2

1

Sample Input 3

100

Sample Output 3

108

Sample Input 4

2020

Sample Output 4

40812

Sample Input 5

200000

Sample Output 5

400000008


module mod_tree_map
  implicit none

  type t_entry
    private
    integer :: key
    integer :: val
  end type t_entry

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

  type t_tree_map
    private
    type(t_node), pointer :: root => null()
    integer :: deflt = 0
  end type t_tree_map

contains

  function new_entry(key,val) result(e)
    integer, intent(in) :: key
    integer, intent(in) :: val
    type(t_entry), pointer :: e
    e => null()
    allocate(e)
    e%key = key
    e%val = val
  end

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

  integer function level(n)
    type(t_node), pointer, intent(in) :: n
    level = 0
    if (.not.associated(n)) return
    level = n%level
  end

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

  recursive function tree_size(n) result(s)
    type(t_node), pointer, intent(in) :: n
    integer :: s
    s = 0
    if (.not.associated(n)) return
    s = 1+tree_size(n%left)+tree_size(n%right)
  end

  function skew(n) result(l)
    type(t_node), pointer, intent(in) :: n
    type(t_node), pointer :: l
    l => n
    if (.not.(associated(n) .and. associated(n%left) .and. &
    &   n%left%level == n%level)) return
    l => n%left
    n%left => l%right
    l%right => n
  end

  function split(n) result(r)
    type(t_node), pointer, intent(in) :: n
    type(t_node), pointer :: r
    r => n
    if (.not.(associated(n) .and. associated(n%right) .and. &
    &   associated(n%right%right) .and. n%right%right%level == n%level)) return
    r => n%right
    n%right => r%left
    r%left => n
    r%level = r%level+1
  end

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

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

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

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

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

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

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

  integer function size_of(map)
    type(t_tree_map), intent(in) :: map
    size_of = tree_size(map%root)
  end

  subroutine clear(map)
    type(t_tree_map), intent(inout) :: map
    call release_tree(map%root)
    map%root => null()
  end

  subroutine set_default(map,deflt)
    type(t_tree_map), intent(inout) :: map
    integer, intent(in) :: deflt
    map%deflt = deflt
  end

  subroutine put_entry(map,e)
    type(t_tree_map), intent(inout) :: map
    type(t_entry), pointer, intent(in) :: e
    map%root => insert(map%root,e)
  end

  subroutine remove_entry(map,e)
    type(t_tree_map), intent(inout) :: map
    type(t_entry), pointer, intent(in) :: e
    map%root => delete(map%root,e)
  end

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

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

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

  function poll_first_entry(map) result(ret)
    type(t_tree_map), intent(inout) :: map
    type(t_node), pointer :: n
    type(t_entry), pointer :: ret
    ret => null()
    n => map%root
    if (.not.associated(n)) return
    do while (associated(n%left))
      n => n%left
    end do
    ret => n%e
    map%root => delete(map%root,ret)
  end

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

  function poll_last_entry(map) result(ret)
    type(t_tree_map), intent(inout) :: map
    type(t_node), pointer :: n
    type(t_entry), pointer :: ret
    ret => null()
    n => map%root
    if (.not.associated(n)) return
    do while (associated(n%right))
      n => n%right
    end do
    ret => n%e
    map%root => delete(map%root,ret)
  end

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

  function lower_entry(map,e) result(ret)
    type(t_tree_map), intent(inout) :: map
    type(t_entry), pointer, intent(in) :: e
    type(t_entry), pointer :: ret
    ret => floor_entry(map,new_entry(e%key-1,0))
  end

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

  function higher_entry(map,e) result(ret)
    type(t_tree_map), intent(inout) :: map
    type(t_entry), pointer, intent(in) :: e
    type(t_entry), pointer :: ret
    ret => ceiling_entry(map,new_entry(e%key+1,0))
  end

  subroutine get_keys(map,keys,num)
    type(t_tree_map), intent(inout) :: map
    integer, intent(inout) :: keys(:)
    integer, intent(inout) :: num
    keys = 0
    num = 0
    call get_keys_list(map%root,keys,num)
  end

  subroutine put(map,key,val)
    type(t_tree_map), intent(inout) :: map
    integer, intent(in) :: key
    integer, intent(in) :: val
    call put_entry(map,new_entry(key,val))
  end

  subroutine remove(map,key)
    type(t_tree_map), intent(inout) :: map
    integer, intent(in) :: key
    call remove_entry(map,new_entry(key,0))
  end

  function get(map,key) result(val)
    type(t_tree_map), intent(inout) :: map
    integer, intent(in) :: key
    type(t_entry), pointer :: tmp
    integer :: val
    val = map%deflt
    tmp => get_entry(map,new_entry(key,0))
    if (.not.associated(tmp)) return
    val = tmp%val
  end

  logical function contain(map,key)
    type(t_tree_map), intent(inout) :: map
    integer, intent(in) :: key
    contain = contain_entry(map,new_entry(key,0))
  end

  function get_first_key(map) result(key)
    type(t_tree_map), intent(inout) :: map
    type(t_entry), pointer :: tmp
    integer :: key
    key = map%deflt
    tmp => get_first_entry(map)
    if (.not.associated(tmp)) return
    key = tmp%key
  end

  function get_last_key(map) result(key)
    type(t_tree_map), intent(inout) :: map
    type(t_entry), pointer :: tmp
    integer :: key
    key = map%deflt
    tmp => get_last_entry(map)
    if (.not.associated(tmp)) return
    key = tmp%key
  end

  function floor_key(map,key) result(ret)
    type(t_tree_map), intent(inout) :: map
    integer, intent(in) :: key
    type(t_entry), pointer :: tmp
    integer :: ret
    ret = map%deflt
    tmp => floor_entry(map,new_entry(key,0))
    if (.not.associated(tmp)) return
    ret = tmp%key
  end

  function lower_key(map,key) result(ret)
    type(t_tree_map), intent(inout) :: map
    integer, intent(in) :: key
    type(t_entry), pointer :: tmp
    integer :: ret
    ret = map%deflt
    tmp => lower_entry(map,new_entry(key,0))
    if (.not.associated(tmp)) return
    ret = tmp%key
  end

  function ceiling_key(map,key) result(ret)
    type(t_tree_map), intent(inout) :: map
    integer, intent(in) :: key
    type(t_entry), pointer :: tmp
    integer :: ret
    ret = map%deflt
    tmp => ceiling_entry(map,new_entry(key,0))
    if (.not.associated(tmp)) return
    ret = tmp%key
  end

  function higher_key(map,key) result(ret)
    type(t_tree_map), intent(inout) :: map
    integer, intent(in) :: key
    type(t_entry), pointer :: tmp
    integer :: ret
    ret = map%deflt
    tmp => higher_entry(map,new_entry(key,0))
    if (.not.associated(tmp)) return
    ret = tmp%key
  end

end module mod_tree_map
program ABC152E
    use mod_tree_map
    implicit none
    integer::N
    integer,allocatable,dimension(:)::A
    type(t_tree_map),allocatable,dimension(:)::AYK
    type(t_tree_map)::KYK
    integer::i,j,k,cnt,JOU
    integer(16)::PLUS,KAKE
    integer::ans=0,mo=10**9+7
    read*,N
    allocate(A(N),AYK(N))
    read*,A
    do i=1,N
        do j=2,int(sqrt(real(A(i))))
            if(A(i)/j/=0)cycle
            cnt=0
            do while(mod(A(i),j)==0)
                cnt=cnt+1
                A(i)=A(i)/j
            end do
            if(cnt/=0)then
                call put(AYK(i),j,cnt)
                if(get(KYK,j)<cnt)then
                    call put(KYK,j,cnt)
                endif
            endif
        end do
        if(A(i)/=1)then
            call put(AYK(i),A(i),1)
            if(get(KYK,A(i))<1)then
                call put(KYK,A(i),1)
            endif
        endif
    end do

    do i=1,N
        PLUS=1
        j=get_first_key(KYK)
        do while(j/=0)
            KAKE=1
            JOU=get(KYK,j)-get(AYK(i),j)
            do k=1,JOU
                KAKE=KAKE*j
                if(KAKE>=mo)KAKE=mod(KAKE,mo)
            end do
            PLUS=mod(PLUS*KAKE,mo)
            j=higher_key(KYK,j)
        end do
        ANS=mod(ANS+PLUS,mo)
    end do
    print"(i0)",ANS
contains
end program ABC152E

Score : 500 points

Problem Statement

Given are N positive integers A_1,...,A_N.

Consider positive integers B_1, ..., B_N that satisfy the following condition.

Condition: For any i, j such that 1 \leq i < j \leq N, A_i B_i = A_j B_j holds.

Find the minimum possible value of B_1 + ... + B_N for such B_1,...,B_N.

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

Constraints

1 \leq N \leq 10^4

1 \leq A_i \leq 10^6

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 ... A_N

Output

Print the minimum possible value of B_1 + ... + B_N for B_1,...,B_N that satisfy the condition, modulo (10^9 +7).

Sample Input 1

3
2 3 4

Sample Output 1

13

Let B_1=6, B_2=4, and B_3=3, and the condition will be satisfied.

Sample Input 2

5
12 12 12 12 12

Sample Output 2

5

We can let all B_i be 1.

Sample Input 3

3
1000000 999999 999998

Sample Output 3

996989508

Print the sum modulo (10^9+7).


module merge_sort_mod
    use,intrinsic :: iso_fortran_env
    implicit none
    interface merge_sort
        module procedure ms32, ms64!, msc
    end interface

    interface double_merge_sort
        module procedure msd3232, msd6464!, msdc32, msdc64, msd32c, msd64c
    end interface
    private
    public merge_sort, double_merge_sort
contains
    recursive subroutine ms32(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 swap32(ar(fst),ar(lst))
            return
        end if

        mdl = (fst+lst)/2
        call ms32(ar, fst, mdl)
        call ms32(ar, mdl+1, lst)
        call merge32(ar, fst, mdl, lst)
    end subroutine

    subroutine merge32(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 swap32(x,y)
        integer(int32),intent(inout):: x,y
        integer(int32):: tmp
        tmp = x
        x = y
        y = tmp
    end subroutine

    recursive subroutine ms64(ar, fst, lst)
        integer(int64),intent(inout):: ar(:)
        integer(int64),intent(in):: fst,lst
        integer(int64):: mdl

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

        mdl = (fst+lst)/2
        call ms64(ar, fst, mdl)
        call ms64(ar, mdl+1, lst)
        call merge64(ar, fst, mdl, lst)
    end subroutine

    subroutine merge64(ar, fst, mdl, lst)
        integer(int64),intent(inout):: ar(:)
        integer(int64),intent(in):: fst, mdl, lst
        integer(int64),allocatable:: tmp(:)
        integer(int64):: 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 swap64(x,y)
        integer(int64),intent(inout):: x,y
        integer(int64):: tmp
        tmp = x
        x = y
        y = tmp
    end subroutine

    recursive subroutine msd3232(ar1, ar2, fst, lst)
        integer(int32),intent(inout):: ar1(:),ar2(:)
        integer(int32),intent(in):: fst,lst
        integer(int32):: mdl

        if (lst - fst < 2) then
            if (ar1(fst) > ar1(lst)) then
                call swap32(ar1(fst), ar1(lst))
                call swap32(ar2(fst), ar2(lst))
            end if
            return
        end if

        mdl = (fst+lst)/2

        call msd3232(ar1,ar2,fst,mdl)
        call msd3232(ar1,ar2,mdl+1,lst)
        call merged3232(ar1,ar2,fst,mdl,lst)
    end subroutine

    subroutine merged3232(ar1,ar2,fst,mdl,lst)
        integer(int32),intent(inout):: ar1(:),ar2(:)
        integer(int32),intent(in):: fst,mdl,lst
        integer(int32),allocatable:: t1(:),t2(:)
        integer(int32):: li,ri,ti

        allocate(t1(lst-fst+1), t2(lst-fst+1))

        li=fst
        ri=mdl+1
        ti=1

        do while(li <= mdl .and. ri <= lst)
            if (ar1(li) <= ar1(ri)) then
                t1(ti) = ar1(li)
                t2(ti) = ar2(li)
                li=li+1
            else
                t1(ti) = ar1(ri)
                t2(ti) = ar2(ri)
                ri=ri+1
            end if
            ti=ti+1
        end do

        if (li <= mdl) then
            t1(ti:) = ar1(li:mdl)
            t2(ti:) = ar2(li:mdl)
        else
            t1(ti:) = ar1(ri:lst)
            t2(ti:) = ar2(ri:lst)
        end if

        ar1(fst:lst) = t1(:)
        ar2(fst:lst) = t2(:)

        deallocate(t1,t2)
    end subroutine

    recursive subroutine msd6464(ar1, ar2, fst, lst)
        integer(int64),intent(inout):: ar1(:),ar2(:)
        integer(int64),intent(in):: fst,lst
        integer(int64):: mdl

        if (lst - fst < 2) then
            if (ar1(fst) > ar1(lst)) then
                call swap64(ar1(fst), ar1(lst))
                call swap64(ar2(fst), ar2(lst))
            end if
            return
        end if

        mdl = (fst+lst)/2

        call msd6464(ar1,ar2,fst,mdl)
        call msd6464(ar1,ar2,mdl+1,lst)
        call merged6464(ar1,ar2,fst,mdl,lst)
    end subroutine

    subroutine merged6464(ar1,ar2,fst,mdl,lst)
        integer(int64),intent(inout):: ar1(:),ar2(:)
        integer(int64),intent(in):: fst,mdl,lst
        integer(int64),allocatable:: t1(:),t2(:)
        integer(int64):: li,ri,ti

        allocate(t1(lst-fst+1), t2(lst-fst+1))

        li=fst
        ri=mdl+1
        ti=1

        do while(li <= mdl .and. ri <= lst)
            if (ar1(li) <= ar1(ri)) then
                t1(ti) = ar1(li)
                t2(ti) = ar2(li)
                li=li+1
            else
                t1(ti) = ar1(ri)
                t2(ti) = ar2(ri)
                ri=ri+1
            end if
            ti=ti+1
        end do

        if (li <= mdl) then
            t1(ti:) = ar1(li:mdl)
            t2(ti:) = ar2(li:mdl)
        else
            t1(ti:) = ar1(ri:lst)
            t2(ti:) = ar2(ri:lst)
        end if

        ar1(fst:lst) = t1(:)
        ar2(fst:lst) = t2(:)

        deallocate(t1,t2)
    end subroutine

    ! recursive subroutine msc(ar, fst, lst)
    !     character(*),intent(inout):: ar(:)
    !     integer(int32),intent(in):: fst,lst
    !     integer(int32):: mdl

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

    !     mdl = (fst+lst)/2
    !     call msc(ar, fst, mdl)
    !     call msc(ar, mdl+1, lst)
    !     call mergec(ar, fst, mdl, lst)
    ! end subroutine

    ! subroutine mergec(ar, fst, mdl, lst)
    !     character(*),intent(inout):: ar(:)
    !     integer(int32),intent(in):: fst, mdl, lst
    !     character(:),allocatable:: tmp(:)
    !     integer(int32):: li, ri, ti

    !     allocate(character(len(ar(fst))):: 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 swapc(x,y)
    !     character(*),intent(inout):: x,y
    !     character(:),allocatable:: tmp
    !     tmp = x
    !     x = y
    !     y = tmp
    ! end subroutine

    ! recursive subroutine msdc32(ar1, ar2, fst, lst)
    !     integer(int32),intent(inout):: ar1(:)
    !     character(*), intent(inout):: ar2(:)
    !     integer(int32),intent(in):: fst,lst
    !     integer(int32):: mdl

    !     if (lst - fst < 2) then
    !         if (ar1(fst) > ar1(lst)) then
    !             call swap32(ar1(fst), ar1(lst))
    !             call swapc(ar2(fst), ar2(lst))
    !         end if
    !         return
    !     end if

    !     mdl = (fst+lst)/2

    !     call msdc32(ar1,ar2,fst,mdl)
    !     call msdc32(ar1,ar2,mdl+1,lst)
    !     call mergedc32(ar1,ar2,fst,mdl,lst)
    ! end subroutine

    ! subroutine mergedc32(ar1,ar2,fst,mdl,lst)
    !     integer(int32),intent(inout):: ar1(:)
    !     character(*),intent(inout):: ar2(:)
    !     integer(int32),intent(in):: fst,mdl,lst
    !     integer(int32),allocatable:: t1(:)
    !     character(:),allocatable:: t2(:)
    !     integer(int32):: li,ri,ti

    !     allocate(t1(lst-fst+1))
    !     allocate(character(len(ar2(fst))):: t2(lst-fst+1))

    !     li=fst
    !     ri=mdl+1
    !     ti=1

    !     do while(li <= mdl .and. ri <= lst)
    !         if (ar1(li) <= ar1(ri)) then
    !             t1(ti) = ar1(li)
    !             t2(ti) = ar2(li)
    !             li=li+1
    !         else
    !             t1(ti) = ar1(ri)
    !             t2(ti) = ar2(ri)
    !             ri=ri+1
    !         end if
    !         ti=ti+1
    !     end do

    !     if (li <= mdl) then
    !         t1(ti:) = ar1(li:mdl)
    !         t2(ti:) = ar2(li:mdl)
    !     else
    !         t1(ti:) = ar1(ri:lst)
    !         t2(ti:) = ar2(ri:lst)
    !     end if

    !     ar1(fst:lst) = t1(:)
    !     ar2(fst:lst) = t2(:)

    !     deallocate(t1,t2)
    ! end subroutine

    ! recursive subroutine msdc64(ar1, ar2, fst, lst)
    !     integer(int64),intent(inout):: ar1(:)
    !     character(*), intent(inout):: ar2(:)
    !     integer(int64),intent(in):: fst,lst
    !     integer(int64):: mdl

    !     if (lst - fst < 2) then
    !         if (ar1(fst) > ar1(lst)) then
    !             call swap64(ar1(fst), ar1(lst))
    !             call swapc(ar2(fst), ar2(lst))
    !         end if
    !         return
    !     end if

    !     mdl = (fst+lst)/2

    !     call msdc64(ar1,ar2,fst,mdl)
    !     call msdc64(ar1,ar2,mdl+1,lst)
    !     call mergedc64(ar1,ar2,fst,mdl,lst)
    ! end subroutine

    ! subroutine mergedc64(ar1,ar2,fst,mdl,lst)
    !     integer(int64),intent(inout):: ar1(:)
    !     character(*),intent(inout):: ar2(:)
    !     integer(int64),intent(in):: fst,mdl,lst
    !     integer(int64),allocatable:: t1(:)
    !     character(:),allocatable:: t2(:)
    !     integer(int64):: li,ri,ti

    !     allocate(t1(lst-fst+1))
    !     allocate(character(len(ar2)):: t2(lst-fst+1))

    !     li=fst
    !     ri=mdl+1
    !     ti=1

    !     do while(li <= mdl .and. ri <= lst)
    !         if (ar1(li) <= ar1(ri)) then
    !             t1(ti) = ar1(li)
    !             t2(ti) = ar2(li)
    !             li=li+1
    !         else
    !             t1(ti) = ar1(ri)
    !             t2(ti) = ar2(ri)
    !             ri=ri+1
    !         end if
    !         ti=ti+1
    !     end do

    !     if (li <= mdl) then
    !         t1(ti:) = ar1(li:mdl)
    !         t2(ti:) = ar2(li:mdl)
    !     else
    !         t1(ti:) = ar1(ri:lst)
    !         t2(ti:) = ar2(ri:lst)
    !     end if

    !     ar1(fst:lst) = t1(:)
    !     ar2(fst:lst) = t2(:)

    !     deallocate(t1,t2)
    ! end subroutine

    ! recursive subroutine msd32c(ar1, ar2, fst, lst)
    !     integer(int32),intent(inout):: ar2(:)
    !     character(*), intent(inout):: ar1(:)
    !     integer(int32),intent(in):: fst,lst
    !     integer(int32):: mdl

    !     if (lst - fst < 2) then
    !         if (ar1(fst) > ar1(lst)) then
    !             call swapc(ar1(fst), ar1(lst))
    !             call swap32(ar2(fst), ar2(lst))
    !         end if
    !         return
    !     end if

    !     mdl = (fst+lst)/2

    !     call msd32c(ar1,ar2,fst,mdl)
    !     call msd32c(ar1,ar2,mdl+1,lst)
    !     call merged32c(ar1,ar2,fst,mdl,lst)
    ! end subroutine

    ! subroutine merged32c(ar1,ar2,fst,mdl,lst)
    !     integer(int32),intent(inout):: ar2(:)
    !     character(*),intent(inout):: ar1(:)
    !     integer(int32),intent(in):: fst,mdl,lst
    !     integer(int32),allocatable:: t2(:)
    !     character(:),allocatable:: t1(:)
    !     integer(int32):: li,ri,ti

    !     allocate(character(len(ar1(fst))):: t1(lst-fst+1))
    !     allocate(t2(lst-fst+1))

    !     li=fst
    !     ri=mdl+1
    !     ti=1

    !     do while(li <= mdl .and. ri <= lst)
    !         if (ar1(li) <= ar1(ri)) then
    !             t1(ti) = ar1(li)
    !             t2(ti) = ar2(li)
    !             li=li+1
    !         else
    !             t1(ti) = ar1(ri)
    !             t2(ti) = ar2(ri)
    !             ri=ri+1
    !         end if
    !         ti=ti+1
    !     end do

    !     if (li <= mdl) then
    !         t1(ti:) = ar1(li:mdl)
    !         t2(ti:) = ar2(li:mdl)
    !     else
    !         t1(ti:) = ar1(ri:lst)
    !         t2(ti:) = ar2(ri:lst)
    !     end if

    !     ar1(fst:lst) = t1(:)
    !     ar2(fst:lst) = t2(:)

    !     deallocate(t1,t2)
    ! end subroutine

    ! recursive subroutine msd64c(ar1, ar2, fst, lst)
    !     integer(int64),intent(inout):: ar2(:)
    !     character(*), intent(inout):: ar1(:)
    !     integer(int64),intent(in):: fst,lst
    !     integer(int64):: mdl

    !     if (lst - fst < 2) then
    !         if (ar1(fst) > ar1(lst)) then
    !             call swapc(ar1(fst), ar1(lst))
    !             call swap64(ar2(fst), ar2(lst))
    !         end if
    !         return
    !     end if

    !     mdl = (fst+lst)/2

    !     call msd64c(ar1,ar2,fst,mdl)
    !     call msd64c(ar1,ar2,mdl+1,lst)
    !     call merged64c(ar1,ar2,fst,mdl,lst)
    ! end subroutine

    ! subroutine merged64c(ar1,ar2,fst,mdl,lst)
    !     integer(int64),intent(inout):: ar2(:)
    !     character(*),intent(inout):: ar1(:)
    !     integer(int64),intent(in):: fst,mdl,lst
    !     integer(int64),allocatable:: t2(:)
    !     character(:),allocatable:: t1(:)
    !     integer(int64):: li,ri,ti

    !     allocate(character(len(ar1(fst))):: t1(lst-fst+1))
    !     allocate(t2(lst-fst+1))

    !     li=fst
    !     ri=mdl+1
    !     ti=1

    !     do while(li <= mdl .and. ri <= lst)
    !         if (ar1(li) <= ar1(ri)) then
    !             t1(ti) = ar1(li)
    !             t2(ti) = ar2(li)
    !             li=li+1
    !         else
    !             t1(ti) = ar1(ri)
    !             t2(ti) = ar2(ri)
    !             ri=ri+1
    !         end if
    !         ti=ti+1
    !     end do

    !     if (li <= mdl) then
    !         t1(ti:) = ar1(li:mdl)
    !         t2(ti:) = ar2(li:mdl)
    !     else
    !         t1(ti:) = ar1(ri:lst)
    !         t2(ti:) = ar2(ri:lst)
    !     end if

    !     ar1(fst:lst) = t1(:)
    !     ar2(fst:lst) = t2(:)

    !     deallocate(t1,t2)
    ! end subroutine

    ! recursive subroutine msdcc(ar1, ar2, fst, lst)
    !     character(*), intent(inout):: ar1(:), ar2(:)
    !     integer(int64),intent(in):: fst,lst
    !     integer(int64):: mdl

    !     if (lst - fst < 2) then
    !         if (ar1(fst) > ar1(lst)) then
    !             call swapc(ar1(fst), ar1(lst))
    !             call swapc(ar2(fst), ar2(lst))
    !         end if
    !         return
    !     end if

    !     mdl = (fst+lst)/2

    !     call msdcc(ar1,ar2,fst,mdl)
    !     call msdcc(ar1,ar2,mdl+1,lst)
    !     call mergecc(ar1,ar2,fst,mdl,lst)
    ! end subroutine

    ! subroutine mergecc(ar1,ar2,fst,mdl,lst)
    !     character(*),intent(inout):: ar1(:),ar2(:)
    !     integer(int64),intent(in):: fst,mdl,lst
    !     character(:),allocatable:: t1(:),t2(:)
    !     integer(int64):: li,ri,ti

    !     allocate(character(len(ar1(fst))):: t1(lst-fst+1))
    !     allocate(character(len(ar2(fst))):: t2(lst-fst+1))

    !     li=fst
    !     ri=mdl+1
    !     ti=1

    !     do while(li <= mdl .and. ri <= lst)
    !         if (ar1(li) <= ar1(ri)) then
    !             t1(ti) = ar1(li)
    !             t2(ti) = ar2(li)
    !             li=li+1
    !         else
    !             t1(ti) = ar1(ri)
    !             t2(ti) = ar2(ri)
    !             ri=ri+1
    !         end if
    !         ti=ti+1
    !     end do

    !     if (li <= mdl) then
    !         t1(ti:) = ar1(li:mdl)
    !         t2(ti:) = ar2(li:mdl)
    !     else
    !         t1(ti:) = ar1(ri:lst)
    !         t2(ti:) = ar2(ri:lst)
    !     end if

    !     ar1(fst:lst) = t1(:)
    !     ar2(fst:lst) = t2(:)

    !     deallocate(t1,t2)
    ! end subroutine
end module

program keyence2020b
    use,intrinsic :: iso_fortran_env
    use merge_sort_mod
    implicit none
    integer(int32):: n,i
    integer(int32):: x,l,nl,ans
    integer(int32), allocatable:: fst(:),lst(:)

    read*, n
    allocate(fst(n),lst(n))

    do i=1,n
        read*, x,l
        fst(i)=x-l
        lst(i)=x+l
    end do
    call double_merge_sort(lst,fst,1,n)
    nl = -1000000009
    ans=0
    do i=1,n
        if (nl <= fst(i)) then
            ans=ans+1
            nl = lst(i)
        end if
    end do
    print'(i0)', ans
end program keyence2020b

Score : 200 points

Problem Statement

In a factory, there are N robots placed on a number line.
Robot i is placed at coordinate X_i and can extend its arms of length L_i in both directions, positive and negative.

We want to remove zero or more robots so that the movable ranges of arms of no two remaining robots intersect.
Here, for each i (1 \leq i \leq N), the movable range of arms of Robot i is the part of the number line between the coordinates X_i - L_i and X_i + L_i, excluding the endpoints.

Find the maximum number of robots that we can keep.

Constraints

1 \leq N \leq 100,000

0 \leq X_i \leq 10^9 (1 \leq i \leq N)

1 \leq L_i \leq 10^9 (1 \leq i \leq N)

If i \neq j, X_i \neq X_j.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
X_1 L_1
X_2 L_2
\vdots
X_N L_N

Output

Print the maximum number of robots that we can keep.

Sample Input 1

4
2 4
4 3
9 3
100 5

Sample Output 1

3

By removing Robot 2, we can keep the other three robots.

Sample Input 2

2
8 20
1 10

Sample Output 2

1

Sample Input 3

5
10 1
2 1
4 1
6 1
8 1

Sample Output 3

5


program key2020_B
    implicit none
    integer(16)::N
    integer(16),allocatable,dimension(:)::X,L
    integer(16)::ANS
    logical(16),allocatable,dimension(:)::IN
    integer(16)::R
    integer(16)::i

    read*,N
    allocate(X(N),L(N))
    do i=1,N
        read*,X(i),L(i)
    end do
    call heapsortPair(N,X,L)
    allocate(IN(N))
    IN=.True.
    ANS=N
    R=X(1)+L(1)
    do i=2,N
        if(X(i)-L(i)<R)then
            ANS=ANS-1
            if(X(i)+L(i)<R)then
                R=X(i)+L(i)
            endif
        else
            R=X(i)+L(i)
        endif
    end do
    print"(i0)",ans
contains
subroutine HeapsortPair(n,array,array2)
  implicit none
!ここの入力は状況に応じて変更すること
  integer(16),intent(in) :: n
  integer(16),intent(inout) :: array(1:n),array2(1:n)
  integer(16)::i,k,j,l
  integer(16):: t,t2

  l=n/2+1
  k=n
  do while(k /= 1)
     if(l > 1)then
        l=l-1
        t=array(L)
        t2=array2(L)
     else
        t=array(k)
        t2=array2(k)
        array(k)=array(1)
        array2(k)=array2(1)
        k=k-1
        if(k == 1) then
           array(1)=t
           array2(1)=t2
           exit
        endif
     endif
     i=l
     j=l+l
     do while(j<=k)
        if(j < k)then
           if(array(j) < array(j+1))j=j+1
        endif
        if (t < array(j))then
           array(i)=array(j)
           array2(i)=array2(j)
           i=j
           j=j+j
        else
           j=k+1
        endif
     enddo
     array(i)=t
     array2(i)=t2
  enddo
  return
end subroutine HeapsortPair

end program key2020_B


program name
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: n,k,s
    integer(int32), allocatable:: ans(:)

    read*, n,k,s
    allocate(ans(n))
    ans(:) = 1000000000
    if (s == 1000000) ans(:) = 999999999
    ans(1:k) = s
    print*, ans(:)
end program name

Score : 400 points

Problem Statement

Given are three integers N, K, and S.

Find a sequence A_1, A_2, ..., A_N of N integers between 1 and 10^9 (inclusive) that satisfies the condition below.
We can prove that, under the conditions in Constraints, such a sequence always exists.

There are exactly K pairs (l, r) of integers such that 1 \leq l \leq r \leq N and A_l + A_{l + 1} + \cdots + A_r = S.

Constraints

1 \leq N \leq 10^5

0 \leq K \leq N

1 \leq S \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K S

Output

Print a sequence satisfying the condition, in the following format:

A_1 A_2 ... A_N

Sample Input 1

4 2 3

Sample Output 1

1 2 3 4

Two pairs (l, r) = (1, 2) and (3, 3) satisfy the condition in the statement.

Sample Input 2

5 3 100

Sample Output 2

50 50 50 30 70


program key2020_C
    implicit none
    integer::N,K,S
    integer::i
    read*,N,K,S
    if(K/=0)then
        write(*,"(i0)",advance="no")S
    else
        write(*,"(i0)",advance="no")merge(S+1,S-1,S/=10**9)
    endif
    do i=2,K
        write(*,"(A,i0)",advance="no")" ",S
    end do
    do i=K+1,N
        if(K==0.and.i==N)exit
         write(*,"(A,i0)",advance="no")" ",merge(S+1,S-1,S/=10**9)
    end do
    write(*,*)
end program key2020_C


program prob53
    implicit none
    character :: c
    write(*,'(a)') char(ichar(c) + 1)

    stop
contains
end program prob53

Score : 100 points

Problem Statement

Given is a lowercase English letter C that is not z. Print the letter that follows C in alphabetical order.

Constraints

C is a lowercase English letter that is not z.

Input

Input is given from Standard Input in the following format:

C

Output

Print the letter that follows C in alphabetical order.

Sample Input 1

a

Sample Output 1

b

a is followed by b.

Sample Input 2

y

Sample Output 2

z

y is followed by z.


program answer
  implicit none

  integer(8) :: N, M, i, ans
  integer(8), allocatable :: p(:), T(:), un(:)
  character(2), allocatable :: S(:)
  read(*,*) N, M
  allocate(p(M))
  allocate(S(M))
  allocate(T(N))
  allocate(un(N))
  do i = 1, M
     read(*,*) p(i), S(i)
  end do

  T=0
  un=0
  ans=0
  do i = 1, M
     if(T(p(i))==0) then
        if(S(i)=='AC') then
           ans=ans+1
           T(p(i))=1
        else
           un(p(i))=un(p(i))+1
           T(p(i))=0
        end if
     end if
  end do

  write(*,*) ans, sum(un)

  deallocate(p, T, un, S)

  stop
 end program answer

Score : 300 points

Problem Statement

Takahashi participated in a contest on AtCoder.

The contest had N problems.

Takahashi made M submissions during the contest.

The i-th submission was made for the p_i-th problem and received the verdict S_i (AC or WA).

The number of Takahashi's correct answers is the number of problems on which he received an AC once or more.

The number of Takahashi's penalties is the sum of the following count for the problems on which he received an AC once or more: the number of WAs received before receiving an AC for the first time on that problem.

Find the numbers of Takahashi's correct answers and penalties.

Constraints

N, M, and p_i are integers.

1 \leq N \leq 10^5

0 \leq M \leq 10^5

1 \leq p_i \leq N

S_i is AC or WA.

Input

Input is given from Standard Input in the following format:

N M
p_1 S_1
:
p_M S_M

Output

Print the number of Takahashi's correct answers and the number of Takahashi's penalties.

Sample Input 1

2 5
1 WA
1 AC
2 WA
2 AC
2 WA

Sample Output 1

2 2

In his second submission, he received an AC on the first problem for the first time. Before this, he received one WA on this problem.

In his fourth submission, he received an AC on the second problem for the first time. Before this, he received one WA on this problem.

Thus, he has two correct answers and two penalties.

Sample Input 2

100000 3
7777 AC
7777 AC
7777 AC

Sample Output 2

1 0

Note that it is pointless to get an AC more than once on the same problem.

Sample Input 3

6 0

Sample Output 3

0 0


program ccc
implicit none

integer :: n, m, p, i
character(2) :: s
integer,allocatable,dimension(:) :: q, r

read*, n, m
allocate(q(n),r(n))
q = 0
r = 0

do i = 1, m
read*, p, s
	if (s=='AC') then
    	if (q(p)==0) then
        q(p) = 1
        end if
    else
    	if (q(p)==0) then
	    r(p) = r(p) + 1
        end if
    end if
end do

do i = 1, n
	if (q(i)==0) r(i)=0
end do

print*, sum(q), sum(r)

end program


      PROGRAM welcomeToAtcoder
      IMPLICIT NONE
      integer(16) :: n,m
      integer(16),allocatable :: p(:),w(:)
      character(2),allocatable :: s(:)
      integer(16) :: i
      logical,allocatable :: flag(:)
      integer(16) :: ac=0,wa=0

      read*,n,m
      allocate( p(m),s(m),flag(n),w(n) )
      do i = 1,m
        read*,p(i),s(i)
      end do

      flag(:) = .true.
      w(:) = 0
      do i = 1,m
        if( flag(p(i)) )then
          if( s(i)(1:2)=='AC' )then
            ac = ac + 1
            wa = wa + w(p(i))
            flag(p(i)) = .false.
          else
            w(p(i)) = w(p(i)) + 1
          end if
        end if
      end do

      print*,ac,wa

      END PROGRAM


module mod_queue
  implicit none
  type t_node
    private
    integer :: item
    type(t_node), pointer :: prev => null()
    type(t_node), pointer :: next => null()
  end type
  type t_queue
    private
    integer :: num = 0
    type(t_node), pointer :: head => null()
    type(t_node), pointer :: tail => null()
  contains
    procedure :: enqueue => enqueue
    procedure :: dequeue => dequeue
    procedure :: peek => peek
    procedure :: clear => clear
    procedure :: size => size_of
  end type
contains
  function new_node(item) result(node)
    integer, intent(in) :: item
    type(t_node), pointer :: node
    allocate(node)
    node%item = item
  end
  subroutine enqueue(this,item)
    class(t_queue), intent(inout) :: this
    integer, intent(in) :: item
    type(t_node), pointer :: node
    node => new_node(item)
    if (associated(this%head)) then
      node%prev => this%tail
      this%tail%next => node
    else
      this%head => node
    end if
    this%tail => node
    this%num = this%num+1
  end
  function dequeue(this) result(item)
    class(t_queue), intent(inout) :: this
    integer :: item
    type(t_node), pointer :: node
    item = this%head%item
    node => this%head%next
    deallocate(this%head)
    this%head => node
    if (associated(node)) then
      node%prev => null()
    else
      this%tail => null()
    end if
    this%num = this%num-1
  end
  function peek(this) result(item)
    class(t_queue), intent(in) :: this
    integer :: item
    item = this%head%item
  end
  subroutine clear(this)
    class(t_queue), intent(inout) :: this
    type(t_node), pointer :: node, next
    if (.not.associated(this%head)) return
    node => this%head
    do while (associated(node%next))
      next => node%next
      deallocate(node)
      node => next
    end do
    this%head => null()
    this%tail => null()
    this%num = 0
  end
  integer function size_of(this)
    class(t_queue), intent(in) :: this
    size_of = this%num
  end
end module mod_queue
program maze_master
  implicit none
  integer :: h, w, a(0:21,0:21) = -1, i, j, m = 0
  integer :: di(4) = (/-1,1,0,0/), dj(4) = (/0,0,-1,1/)
  logical :: b(20,20)
  real(8) :: t0, t1
  character(20) :: s
  call cpu_time(t0)
  read(*,*) h, w
  do i = 1, h
    read(*,*) s
    do j = 1, w
      if (s(j:j) == ".") a(i,j) = 0
    end do
  end do
  do i = 1, h
    do j = 1, w
      if (a(i,j) /= -1) then
        b = .false.
        m = max(m,bfs(i,j))
      end if
    end do
  end do
  write(*,'(i0)') m
contains
  integer function bfs(i,j)
    use mod_queue
    integer, intent(in) :: i, j
    integer :: k, p, q, x, y, idx
    type(t_queue) :: queue
    a(i,j) = 0
    call queue%enqueue(i*(w+1)+j)
    do while (queue%size() > 0)
      call cpu_time(t1)
      if (t1-t0 > 1.95d0) then
        write(*,'(i0)') h+w-2
        stop
      end if
      idx = queue%dequeue()
      p = idx/(w+1)
      q = idx-p*(w+1)
      b(p,q) = .true.
      do k = 1, 4
        x = p+di(k)
        y = q+dj(k)
        if (a(x,y) /= -1 .and. ((.not. b(x,y)) .or. a(x,y) > a(p,q)+1)) then
          a(x,y) = a(p,q)+1
          call queue%enqueue(x*(w+1)+y)
        end if
      end do
    end do
    bfs = maxval(a(1:h,1:w))
  end
end program maze_master

Score : 400 points

Problem Statement

Takahashi has a maze, which is a grid of H \times W squares with H horizontal rows and W vertical columns.

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

From a road square, you can move to a horizontally or vertically adjacent road square.

You cannot move out of the maze, move to a wall square, or move diagonally.

Takahashi will choose a starting square and a goal square, which can be any road squares, and give the maze to Aoki.

Aoki will then travel from the starting square to the goal square, in the minimum number of moves required.

In this situation, find the maximum possible number of moves Aoki has to make.

Constraints

1 \leq H,W \leq 20

S_{ij} is . or #.

S contains at least two occurrences of ..

Any road square can be reached from any road square in zero or more moves.

Input

Input is given from Standard Input in the following format:

H W
S_{11}...S_{1W}
:
S_{H1}...S_{HW}

Output

Print the maximum possible number of moves Aoki has to make.

Sample Input 1

3 3
...
...
...

Sample Output 1

4

If Takahashi chooses the top-left square as the starting square and the bottom-right square as the goal square, Aoki has to make four moves.

Sample Input 2

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

Sample Output 2

10

If Takahashi chooses the bottom-left square as the starting square and the top-right square as the goal square, Aoki has to make ten moves.


program ABC151F
    implicit none
    integer::N
    integer,allocatable,dimension(:,:)::xy
    real(16)::ANS
    integer::i,j,k
    read*,N
    allocate(xy(N,2))
    do i=1,N
        read*,xy(i,1),xy(i,2)
    end do
    if(N==2)then
        print"(f0.14)",dist(1,2)/2.0
        stop
    endif
    ANS=0
    do i=1,N-2
        do j=i+1,N-1
            do k=j+1,N
                if(abs((XY(k,1)-XY(i,1))*(XY(j,2)-XY(i,2)))==abs((XY(k,2)-XY(i,2))*(XY(j,1)-XY(i,1))))then
                    ANS=max(ANS,max(dist(i,j),dist(i,k),dist(j,k))/real(2.0,16))
                endif
                ANS=max(ANS,min(R(i,j,k)/real(2.0,16),max(dist(i,j),dist(i,k),dist(j,k))/real(2,16)))
            end do
        end do
    end do
    print"(f0.20)",ANS
contains
function R(i,j,k)
    integer::i,j,k
    real(16) R
    R=dist(j,k)/sqrt(1-NAI(i,j,k)**2)
end function
function dist(i,j)
    integer::i,j
    real(16)::dist
    dist=sqrt(real((XY(i,1)-XY(j,1))**2+(XY(i,2)-XY(j,2))**2,16))
end function
function NAI(i,j,k)
    integer::i,j,k
    real(16)::NAI
    NAI=real((XY(j,1)-XY(i,1))*(XY(k,1)-XY(i,1))+(XY(j,2)-XY(i,2))*(XY(k,2)-XY(i,2)),kind=16)/dist(i,j)/dist(i,k)
end function
end program ABC151F

Score : 600 points

Problem Statement

Given are N points (x_i, y_i) in a two-dimensional plane.

Find the minimum radius of a circle such that all the points are inside or on it.

Constraints

2 \leq N \leq 50

0 \leq x_i \leq 1000

0 \leq y_i \leq 1000

The given N points are all different.

The values in input are all integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
:
x_N y_N

Output

Print the minimum radius of a circle such that all the N points are inside or on it.

Your output will be considered correct if the absolute or relative error from our answer is at most 10^{-6}.

Sample Input 1

2
0 0
1 0

Sample Output 1

0.500000000000000000

Both points are contained in the circle centered at (0.5,0) with a radius of 0.5.

Sample Input 2

3
0 0
0 1
1 0

Sample Output 2

0.707106781186497524

Sample Input 3

10
10 9
5 9
2 0
0 0
2 7
3 3
2 5
10 0
3 7
1 9

Sample Output 3

6.726812023536805158

If the absolute or relative error from our answer is at most 10^{-6}, the output will be considered correct.


program yen

 implicit none
 integer::k,x

 read*,k,x

 if(500*k>=x) then
  print*,'Yes'
 else
  print*,'No'
 end if

end program yen

Score : 100 points

Problem Statement

Takahashi has K 500-yen coins. (Yen is the currency of Japan.)
If these coins add up to X yen or more, print Yes; otherwise, print No.

Constraints

1 \leq K \leq 100

1 \leq X \leq 10^5

Input

Input is given from Standard Input in the following format:

K X

Output

If the coins add up to X yen or more, print Yes; otherwise, print No.

Sample Input 1

2 900

Sample Output 1

Yes

Two 500-yen coins add up to 1000 yen, which is not less than X = 900 yen.

Sample Input 2

1 501

Sample Output 2

No

One 500-yen coin is worth 500 yen, which is less than X = 501 yen.

Sample Input 3

4 2000

Sample Output 3

Yes

Four 500-yen coins add up to 2000 yen, which is not less than X = 2000 yen.


      PROGRAM coins
      IMPLICIT NONE
      integer(16) :: k,x

      read*,k,x

      if( 500*k>=x )then
        print*,'Yes'
      else
        print*,'No'
      end if

      END PROGRAM


program main
  implicit none
  integer :: n, i, l, res
  character(50) :: s
  read (*, *) n
  read (*, *) s
  l = len(trim(s))
  res = 0
  do i = 1, l - 2
    if(s(i:i + 2) == "ABC") res = res + 1
  end do
  write (*, "(i0)") res
end program main

Score : 200 points

Problem Statement

We have a string S of length N consisting of uppercase English letters.

How many times does ABC occur in S as contiguous subsequences (see Sample Inputs and Outputs)?

Constraints

3 \leq N \leq 50

S consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print number of occurrences of ABC in S as contiguous subsequences.

Sample Input 1

10
ZABCDBABCQ

Sample Output 1

2

Two contiguous subsequences of S are equal to ABC: the 2-nd through 4-th characters, and the 7-th through 9-th characters.

Sample Input 2

19
THREEONEFOURONEFIVE

Sample Output 2

0

No contiguous subsequences of S are equal to ABC.

Sample Input 3

33
ABCCABCBABCCABACBCBBABCBCBCBCABCB

Sample Output 3

5


program main

  implicit none

  integer :: a, N, i, j, k, pn, qn, tmp
  integer,allocatable :: P(:), Q(:)

  read(*,*) N
  allocate( P(N) )
  allocate( Q(N) )
  read(*,*) P
  read(*,*) Q

  pn = 0
  qn = 0

  do i = 1, N
    ! kaijou
    tmp = 1
    do j = 1, N - i
      tmp =  tmp * j
    end do
    a = 0
    do k = 1, i
      if ( P(k) < P(i) ) a = a + 1
    end do
    pn = pn + ( P(i) - a - 1 ) * tmp
  end do
    pn = pn + 1

  do i = 1, N
    ! kaijou
    tmp = 1
    do j = 1, N - i
      tmp =  tmp * j
    end do
    a = 0
    do k = 1, i
      if ( Q(k) < Q(i) ) a = a + 1
    end do
    qn = qn + ( Q(i) - a - 1 ) * tmp
  end do
    qn = qn + 1

  tmp = pn - qn
  print*, abs( tmp ), pn, qn

end program main

Score : 300 points

Problem Statement

We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N)).

There are N! possible permutations of size N. Among them, let P and Q be the a-th and b-th lexicographically smallest permutations, respectively. Find |a - b|.

Notes

For two sequences X and Y, X is said to be lexicographically smaller than Y if and only if there exists an integer k such that X_i = Y_i~(1 \leq i < k) and X_k < Y_k.

Constraints

2 \leq N \leq 8

P and Q are permutations of size N.

Input

Input is given from Standard Input in the following format:

N
P_1 P_2 ... P_N
Q_1 Q_2 ... Q_N

Output

Print |a - b|.

Sample Input 1

3
1 3 2
3 1 2

Sample Output 1

3

There are 6 permutations of size 3: (1,~2,~3), (1,~3,~2), (2,~1,~3), (2,~3,~1), (3,~1,~2), and (3,~2,~1). Among them, (1,~3,~2) and (3,~1,~2) come 2-nd and 5-th in lexicographical order, so the answer is |2 - 5| = 3.

Sample Input 2

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

Sample Output 2

17517

Sample Input 3

3
1 2 3
1 2 3

Sample Output 3

0


program semi_common_multiple
    implicit none
    integer(8):: n, m, i
    integer(8), allocatable:: a(:)
    integer(8):: gcd, lcm

    read*, n, m
    allocate(a(n))
    read*, a(:)
    a(:) = a(:) / 2
    ! do while(mod(a(1), 2) == 0)
    !     do i=1, n
    !         if(mod(a(i),2) /= 0)then
    !             print*, 0
    !             stop
    !         endif
    !         a(i) = a(i) / 2
    !     end do
    !         m = m / 2
    ! end do

    lcm = 1

    do i=1,n
        gcd = calc_gcd(lcm,a(i))
        lcm = lcm/gcd*a(i)
    end do

    print*, (m/lcm+1)/2

contains
recursive function calc_gcd(a,b) result(ret)
integer(8):: a, b, r, ret
    if (mod(a,b) == 0)then
        ret = b
        return
    end if
    ret = calc_gcd(b,mod(a,b))
end function
end program

Score : 400 points

Problem Statement

Given are a sequence A= {a_1,a_2,......a_N} of N positive even numbers, and an integer M.

Let a semi-common multiple of A be a positive integer X that satisfies the following condition for every k (1 \leq k \leq N):

There exists a non-negative integer p such that X= a_k \times (p+0.5).

Find the number of semi-common multiples of A among the integers between 1 and M (inclusive).

Constraints

1 \leq N \leq 10^5

1 \leq M \leq 10^9

2 \leq a_i \leq 10^9

a_i is an even number.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
a_1 a_2 ... a_N

Output

Print the number of semi-common multiples of A among the integers between 1 and M (inclusive).

Sample Input 1

2 50
6 10

Sample Output 1

2

15 = 6 \times 2.5

15 = 10 \times 1.5

45 = 6 \times 7.5

45 = 10 \times 4.5

Thus, 15 and 45 are semi-common multiples of A. There are no other semi-common multiples of A between 1 and 50, so the answer is 2.

Sample Input 2

3 100
14 22 40

Sample Output 2

0

The answer can be 0.

Sample Input 3

5 1000000000
6 6 2 6 2

Sample Output 3

166666667


program semi_common_multiple
  implicit none
  integer :: n, i, c, c1 = 0
  integer(8) :: m, a(100000) = 0, l = 1, k
  read(*,*) n, m
  read(*,*) a(1:n)
  k = a(1)
  do while (mod(k,2) == 0)
    k = k/2
    c1 = c1+1
  end do
  k = a(1)/k/2
  do i = 1, n
    c = 0
    do while (mod(a(i),2) == 0)
      a(i) = a(i)/2
      c = c+1
    end do
    if (c /= c1) then
      write(*,'(i0)') 0
      stop
    end if
    l = lcm(l,a(i))
    if (l > m/k) then
      write(*,'(i0)') 0
      stop
    end if
  end do
  write(*,'(i0)') (m/(l*k)+1)/2
contains
  integer(8) function lcm(a,b)
    integer(8), intent(in) :: a, b
    lcm = a/gcd(a,b)*b
  end
  integer(8) function gcd(a,b)
    integer(8), intent(in) :: a, b
    integer(8) :: x, y
    x = max(a,b)
    y = min(a,b)
    do while (y > 0_8)
      gcd = y
      y = mod(x,y)
      x = gcd
    end do
  end
end program semi_common_multiple


program semi_common_multiple
  implicit none
  integer :: n, i
  integer(8) :: m, a(100000) = 0, l = 1
  read(*,*) n, m
  read(*,*) a(1:n)
  do i = 1, n
    a(i) = a(i)/2
    l = lcm(l,a(i))
    if (l > m .or. mod(l,2) == 0) then
      write(*,'(i0)') 0
      stop
    end if
  end do
  write(*,'(i0)') (m/l+1)/2
contains
  integer(8) function lcm(a,b)
    integer(8), intent(in) :: a, b
    lcm = a/gcd(a,b)*b
  end
  integer(8) function gcd(a,b)
    integer(8), intent(in) :: a, b
    integer(8) :: x, y
    x = max(a,b)
    y = min(a,b)
    do while (y > 0_8)
      gcd = y
      y = mod(x,y)
      x = gcd
    end do
  end
end program semi_common_multiple


program change_a_little_bit
    implicit none
    integer(8),parameter:: md=1000000007_8
    integer(8):: n,i,ans=0
    integer(8),allocatable:: c(:)

    read*,n
    allocate(c(n))
    read*,c(:)
    call merge_sort(c,1_8,n)
    print*, c(:)

    do i=1, n
        ans = mod(ans + mod(c(i)*mod(pow_fast(2_8,n-1,md)+mod(pow_fast(2_8,n-2,md)*(n-i),md),md),md),md)
    end do

    print*, mod(ans*pow_fast(2_8,n,md),md)
contains

recursive subroutine merge_sort(ar,fst,lst)
    integer(8):: ar(:), fst, lst, mid, tmp

    if (lst-fst < 2) then
        if(ar(lst) < ar(fst)) then
            tmp = ar(lst)
            ar(lst) = ar(fst)
            ar(fst) = tmp
        end if
        return
    end if

    mid = (fst+lst)/2
    call merge_sort(ar,fst,mid)
    call merge_sort(ar,mid+1,lst)
    call merge(ar,fst,mid,lst)
end subroutine

subroutine merge(ar,fst,mid,lst)
    integer(8):: ar(:), fst, mid, lst
    integer(8),pointer:: tmp(:)
    integer(8):: it,il,ir

    allocate(tmp(lst-fst+1))
    it = 1
    il = fst
    ir = mid+1
    do while(il<=mid .and. ir<=lst)
        if (ar(il) <= ar(ir)) then
            tmp(it) = ar(il)
            il=il+1
        else
            tmp(it) = ar(ir)
            ir=ir+1
        end if
        it=it+1
    end do

    if (ir > lst) then
        tmp(it:) = ar(il:mid)
    else
        tmp(it:) = ar(ir:lst)
    end if

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

function pow_fast(x,pow,md) result(ret)
    integer(8),value:: x, pow
    integer(8)::md,ret

    ret=1_8
    if (x==1 .or. pow==0) return

    do while(pow > 0_8)
        if (mod(pow,2_8) == 1_8) then
            ret = mod(ret*x,md)
        end if
        x = mod(x*x,md)
        pow = pow/2_8
    end do

end function

end program change_a_little_bit

Score : 500 points

Problem Statement

For two sequences S and T of length N consisting of 0 and 1, let us define f(S, T) as follows:

Consider repeating the following operation on S so that S will be equal to T. f(S, T) is the minimum possible total cost of those operations.

Change S_i (from 0 to 1 or vice versa). The cost of this operation is D \times C_i, where D is the number of integers j such that S_j \neq T_j (1 \leq j \leq N) just before this change.

There are 2^N \times (2^N - 1) pairs (S, T) of different sequences of length N consisting of 0 and 1. Compute the sum of f(S, T) over all of those pairs, modulo (10^9+7).

Constraints

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

1 \leq C_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
C_1 C_2 \cdots C_N

Output

Print the sum of f(S, T), modulo (10^9+7).

Sample Input 1

1
1000000000

Sample Output 1

999999993

There are two pairs (S, T) of different sequences of length 2 consisting of 0 and 1, as follows:

S = (0), T = (1): by changing S_1 to 1, we can have S = T at the cost of 1000000000, so f(S, T) = 1000000000.

S = (1), T = (0): by changing S_1 to 0, we can have S = T at the cost of 1000000000, so f(S, T) = 1000000000.

The sum of these is 2000000000, and we should print it modulo (10^9+7), that is, 999999993.

Sample Input 2

2
5 8

Sample Output 2

124

There are 12 pairs (S, T) of different sequences of length 3 consisting of 0 and 1, which include:

S = (0, 1), T = (1, 0)

In this case, if we first change S_1 to 1 then change S_2 to 0, the total cost is 5 \times 2 + 8 = 18. We cannot have S = T at a smaller cost, so f(S, T) = 18.

Sample Input 3

5
52 67 72 25 79

Sample Output 3

269312


program example
	implicit none

	character(100) S,T

    read(*,*) S,T

    write(*,*) trim(adjustl(trim(adjustl(T))//trim(adjustl(S))))

end program example

Score : 100 points

Problem Statement

Given are two strings S and T consisting of lowercase English letters. Concatenate T and S in this order, without space in between, and print the resulting string.

Constraints

S and T are strings consisting of lowercase English letters.

The lengths of S and T are between 1 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

S T

Output

Print the resulting string.

Sample Input 1

oder atc

Sample Output 1

atcoder

When S = oder and T = atc, concatenating T and S in this order results in atcoder.

Sample Input 2

humu humu

Sample Output 2

humuhumu


program aaa
implicit none

character(200) :: s, t, ts

read*, s, t

ts = trim(t)//trim(s)

print*, trim(ts)

end program


! 2019.12.29
!
      program answer
      implicit none
      integer(8) :: A,B,K
!
      read(*,*) A,B,K
      if(A < K) then
        B = B+A-K
        A = 0
        if(B < 0) then
          B = 0
        end if
      else
        A = A-K
      end if
      write(*,*) A,B
      end program answer

Score : 200 points

Problem Statement

Takahashi has A cookies, and Aoki has B cookies.
Takahashi will do the following action K times:

If Takahashi has one or more cookies, eat one of his cookies.

Otherwise, if Aoki has one or more cookies, eat one of Aoki's cookies.

If they both have no cookies, do nothing.

In the end, how many cookies will Takahashi and Aoki have, respectively?

Constraints

0 \leq A \leq 10^{12}

0 \leq B \leq 10^{12}

0 \leq K \leq 10^{12}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B K

Output

Print the numbers of Takahashi's and Aoki's cookies after K actions.

Sample Input 1

2 3 3

Sample Output 1

0 2

Takahashi will do the following:

He has two cookies, so he eats one of them.

Now he has one cookie left, and he eats it.

Now he has no cookies left, but Aoki has three, so Takahashi eats one of them.

Thus, in the end, Takahashi will have 0 cookies, and Aoki will have 2.

Sample Input 2

500000000000 500000000000 1000000000000

Sample Output 2

0 0

Watch out for overflows.


! 2019.12.29
!
      program answer
      implicit none
      integer(8) :: A,B,K
!
      read(*,*) A,B,K
      if(A < K) then
        B = B+A-K
        A = 0
      else
        A = A-K
      end if
      write(*,*) A,B
      end program answer


PROGRAM main
	implicit none
	integer :: x,xmax, xx
	read *, x
    xmax = 100000000
    do xx = x, xmax
      if (isPrimeNum(xx))then
      	print "(i0)", xx
        exit
       endif
    enddo
contains

	logical pure function isPrimeNum(n)
    	integer, intent (in) :: n
        integer :: i, iend

        iend = sqrt(dble(n))+1
        if(iend == n) iend = iend-1
    	isPrimeNum = .false.
        do i = 2 , iend
        	if (mod(n,i)==0) return
        enddo

	    isPrimeNum = .true.
    end function isPrimeNum

END PROGRAM main

Score: 300 points

Problem Statement

Find the minimum prime number greater than or equal to X.

Notes

A prime number is an integer greater than 1 that cannot be evenly divided by any positive integer except 1 and itself.

For example, 2, 3, and 5 are prime numbers, while 4 and 6 are not.

Constraints

2 \le X \le 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X

Output

Print the minimum prime number greater than or equal to X.

Sample Input 1

20

Sample Output 1

23

The minimum prime number greater than or equal to 20 is 23.

Sample Input 2

2

Sample Output 2

2

X itself can be a prime number.

Sample Input 3

99992

Sample Output 3

100003


program ABC149C
    implicit none
    integer(8)::X,i,j,div_max,flg
    read(5,*)X

    if(X==2)then
        print'(i0)',2
    else
        do i=X,100003
            div_max=int(sqrt(real(i)))+1
            flg=0
            do j=2,div_max
                if(mod(i,j)==0)then
                    flg=1
                    exit
                end if
            end do

            if(flg==0)then
                print'(i0)',i
                exit
            end if
        end do
    end if

end program ABC149C


program prediction_and_restriction
  implicit none
  integer :: n, k, r, s, p, i, x = 0
  character(100000) :: t
  read(*,*) n, k
  read(*,*) r, s, p
  read(*,*) t
  do i = k+1, n
    if (t(i:i) == t(i-k:i-k)) then
      t(i:i) = 'x'
    end if
  end do
  do i = 1, n
    select case (t(i:i))
    case ('r')
      x = x+p
    case ('s')
      x = x+r
    case ('p')
      x = x+s
    end select
  end do
  write(*,'(i0)') x
end program prediction_and_restriction

Score : 400 points

Problem Statement

At an arcade, Takahashi is playing a game called RPS Battle, which is played as follows:

The player plays N rounds of Rock Paper Scissors against the machine. (See Notes for the description of Rock Paper Scissors. A draw also counts as a round.)

Each time the player wins a round, depending on which hand he/she uses, he/she earns the following score (no points for a draw or a loss):

R points for winning with Rock;

S points for winning with Scissors;

P points for winning with Paper.

However, in the i-th round, the player cannot use the hand he/she used in the (i-K)-th round. (In the first K rounds, the player can use any hand.)

Before the start of the game, the machine decides the hand it will play in each round. With supernatural power, Takahashi managed to read all of those hands.

The information Takahashi obtained is given as a string T. If the i-th character of T (1 \leq i \leq N) is r, the machine will play Rock in the i-th round. Similarly, p and s stand for Paper and Scissors, respectively.

What is the maximum total score earned in the game by adequately choosing the hand to play in each round?

Notes

In this problem, Rock Paper Scissors can be thought of as a two-player game, in which each player simultaneously forms Rock, Paper, or Scissors with a hand.

If a player chooses Rock and the other chooses Scissors, the player choosing Rock wins;

if a player chooses Scissors and the other chooses Paper, the player choosing Scissors wins;

if a player chooses Paper and the other chooses Rock, the player choosing Paper wins;

if both players play the same hand, it is a draw.

Constraints

2 \leq N \leq 10^5

1 \leq K \leq N-1

1 \leq R,S,P \leq 10^4

N,K,R,S, and P are all integers.

|T| = N

T consists of r, p, and s.

Input

Input is given from Standard Input in the following format:

N K
R S P
T

Output

Print the maximum total score earned in the game.

Sample Input 1

5 2
8 7 6
rsrpr

Sample Output 1

27

The machine will play {Rock, Scissors, Rock, Paper, Rock}.

We can, for example, play {Paper, Rock, Rock, Scissors, Paper} against it to earn 27 points.
We cannot earn more points, so the answer is 27.

Sample Input 2

7 1
100 10 1
ssssppr

Sample Output 2

211

Sample Input 3

30 5
325 234 123
rspsspspsrpspsppprpsprpssprpsr

Sample Output 3

4996


program main
  implicit none
  integer(8) :: n, k, i, r, s, p, res
  character(100000) :: t, used
  character(1) :: using
  read (*, *) n, k
  read (*, *) r, s, p
  read (*, *) t
  res = 0
  do i = 1, n
    if (i <= k) then
      using = t(i:i)
    else
      if (t(i:i) == used(i - k:i - k)) then
        using = "*"
      else
        using = t(i:i)
      end if
    end if
    if (using == "r") then
      res = res + p
    else if (using == "p") then
      res = res + s
    else if (using == "s") then
      res = res + r
    end if
    used(i:i) = using
  end do
  write (*, "(i0)") res
end program main


module mod_ntt
  implicit none
  integer, parameter :: intkind = 16
  integer(intkind), parameter :: modu = 1224736769_intkind, root = 3_intkind
contains
  function convolve(a, na, b, nb) result(res)
    integer, intent(in) :: na, nb
    integer(intkind), intent(in) :: a(0:na), b(0:nb)
    integer(intkind), allocatable :: fa(:), fb(:), f(:)
    integer(intkind) :: res(0:na + nb)
    integer :: p
    p = find_pow2(na + nb + 1)
    allocate(fa(0:p - 1), fb(0:p - 1), f(0:p - 1))
    call init(a, na, fa, p)
    call ntt(fa, p)
    call init(b, nb, fb, p)
    call ntt(fb, p)
    f = mod(fa * fb, modu)
    call ntt(f, p, .true.)
    res = f(0:na + nb)
  end
  integer function find_pow2(n) result(res)
    integer, intent(in) :: n
    res = n - 1
    res = or(res, rshift(res, 1))
    res = or(res, rshift(res, 2))
    res = or(res, rshift(res, 4))
    res = or(res, rshift(res, 8))
    res = or(res, rshift(res, 16))
    res = res + 1
  end
  subroutine init(a, n, f, p)
    integer, intent(in) :: n, p
    integer(intkind), intent(in) :: a(0:n)
    integer(intkind), intent(out) :: f(0:p - 1)
    f(0:n) = a(0:n)
    f(n + 1:p - 1) = 0
  end
  subroutine ntt(f, p, invert)
    integer, intent(in) :: p
    integer(intkind), intent(inout) :: f(0:p - 1)
    logical, optional, intent(in) :: invert
    integer(intkind) :: w, z, pz, x, y
    integer :: i, j, r, m
    logical :: inverse
    inverse = merge(invert, .false., present(invert))
    w = pow(root, (modu - 1) / p)
    if (inverse) w = inv(w)
    r = 0
    do i = 1, p - 2
      j = p / 2
      do
        r = xor(r, j)
        if (j <= r) exit
        j = j / 2
      end do
      if (i < r) call swap(f(i), f(r))
    end do
    m = 1
    do while (m < p)
      pz = 1
      z = pow(w, int(p / (2 * m), intkind))
      do i = 0, m - 1
        do j = i, p - 1, 2 * m
          x = f(j)
          y = mod(f(j + m) * pz, modu)
          f(j) = mod(x + y, modu)
          f(j + m) = modulo(x - y, modu)
        end do
        pz = mod(pz * z, modu)
      end do
      m = m * 2
    end do
    if (inverse) f = mod(f * inv(int(p, intkind)), modu)
  end
  integer(intkind) function pow(a, b) result(res)
    integer(intkind), intent(in) :: a, b
    integer(intkind) :: r, p
    res = 1
    r = b
    p = a
    do while (r > 0)
      if (btest(r, 0)) res = mod(res * p, modu)
      p = mod(p * p, modu)
      r = rshift(r, 1)
    end do
  end
  integer(intkind) function inv(a) result(res)
    integer(intkind), intent(in) :: a
    res = pow(a, modu - 2)
  end
  subroutine swap(a, b)
    integer(intkind), intent(inout) :: a, b
    a = xor(a, b)
    b = xor(a, b)
    a = xor(a, b)
  end
end module mod_ntt
program handshake
  use mod_ntt
  implicit none
  integer :: n, i, maxa
  integer, dimension(100000) :: a
  integer(8) :: m, cnt = 0, x = 0
  integer(intkind), dimension(0:100000) :: p = 0, q = 0
  integer(intkind), dimension(0:200000) :: c = 0
  read(*,*) n, m
  read(*,*) a(1:n)
  maxa = maxval(a(1:n))
  do i = 1, n
    p(a(i)) = p(a(i)) + 1
  end do
  q = p
  c(0:2 * maxa) = convolve(p(0:maxa), maxa, q(0:maxa), maxa)
  do i = 2 * maxa, 0, -1
    if (c(i) + cnt >= m) then
      x = x + (m - cnt) * i
      exit
    else
      x = x + c(i) * i
      cnt = cnt + c(i)
    end if
  end do
  write(*,'(i0)') x
end program handshake

Score : 500 points

Problem Statement

Takahashi has come to a party as a special guest.
There are N ordinary guests at the party. The i-th ordinary guest has a power of A_i.

Takahashi has decided to perform M handshakes to increase the happiness of the party (let the current happiness be 0).
A handshake will be performed as follows:

Takahashi chooses one (ordinary) guest x for his left hand and another guest y for his right hand (x and y can be the same).

Then, he shakes the left hand of Guest x and the right hand of Guest y simultaneously to increase the happiness by A_x+A_y.

However, Takahashi should not perform the same handshake more than once. Formally, the following condition must hold:

Assume that, in the k-th handshake, Takahashi shakes the left hand of Guest x_k and the right hand of Guest y_k. Then, there is no pair p, q (1 \leq p < q \leq M) such that (x_p,y_p)=(x_q,y_q).

What is the maximum possible happiness after M handshakes?

Constraints

1 \leq N \leq 10^5

1 \leq M \leq N^2

1 \leq A_i \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 ... A_N

Output

Print the maximum possible happiness after M handshakes.

Sample Input 1

5 3
10 14 19 34 33

Sample Output 1

202

Let us say that Takahashi performs the following handshakes:

In the first handshake, Takahashi shakes the left hand of Guest 4 and the right hand of Guest 4.

In the second handshake, Takahashi shakes the left hand of Guest 4 and the right hand of Guest 5.

In the third handshake, Takahashi shakes the left hand of Guest 5 and the right hand of Guest 4.

Then, we will have the happiness of (34+34)+(34+33)+(33+34)=202.

We cannot achieve the happiness of 203 or greater, so the answer is 202.

Sample Input 2

9 14
1 3 5 110 24 21 34 5 3

Sample Output 2

1837

Sample Input 3

9 73
67597 52981 5828 66249 75177 64141 40773 79105 16076

Sample Output 3

8128170


program ABC149E
    implicit none
    integer(16)::N,M
    integer(16),allocatable,dimension(:)::A,R
    integer(16)::ans,OK,NG,MD
    integer(16)::i,j
    read*,N,M
    allocate(A(N))
    read*,A
    call heapsort(N,A)
    allocate(R(200000+1))
    R=0
    do i=1,N
        R(A(i))=R(A(i))+1
    end do
    do i=2*10**5,1,-1
        R(i)=R(i)+R(i+1)
    end do

    OK=0;NG=A(N)*2+1
    MD=(OK+NG)/2
    do while(NG-OK>1)
        if(CNT(MD)>=M)then
            OK=MD
        else
            NG=MD
        endif
        MD=(OK+NG)/2
    end do

    ans=0
    do i=N,1,-1
        do j=N,1,-1
            if(A(i)+A(j)>=MD)then
                ans=ans+A(i)+A(j)
            else
                exit
            endif
        end do
    end do
    print"(i0)",ans-MD*(cnt(MD)-M)
contains
integer(16) function cnt(MID)
    integer(16),intent(in)::MID
    integer(16)::ii
    cnt=0
    do ii=1,N
        if(MID>A(ii))then
            cnt=cnt+R(MID-A(ii))
        else
            cnt=cnt+N
        endif
    end do
end function
subroutine heapsort(n,array)
  implicit none
!ここの入力は状況に応じて変更すること
  integer(16),intent(in) :: n
  integer(16),intent(inout) :: array(1:n)
  integer(16)::i,k,j,l
  integer(16):: t
  l=n/2+1
  k=n
  do while(k /= 1)
     if(l > 1)then
        l=l-1
        t=array(L)
     else
        t=array(k)
        array(k)=array(1)
        k=k-1
        if(k == 1) then
           array(1)=t
           exit
        endif
     endif
     i=l
     j=l+l
     do while(j<=k)
        if(j < k)then
           if(array(j) < array(j+1))j=j+1
        endif
        if (t < array(j))then
           array(i)=array(j)
           i=j
           j=j+j
        else
           j=k+1
        endif
     enddo
     array(i)=t
  enddo
  return
end subroutine heapsort
end program ABC149E


    module m_sort
    !reference
    !https://qiita.com/cure_honey/items/29944920e8aff5651e41
      implicit none
    contains
      recursive function iqsort(ix) result(ires)
        integer(16), allocatable :: ires(:)
        integer(16), intent(in)  :: ix(:)
        integer(16) :: 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 module m_sort

      PROGRAM handshake
      USE m_sort
      IMPLICIT NONE
      integer(16) :: n,m
      integer(16),allocatable :: a(:),sumPow(:)
      integer(16) :: i

      read*,n,m
      allocate( a(n),sumPow(n*n) )
      read*,a(:)

      do i = 1,n
        sumPow((i-1)*n+1:i*n) = a(i) + a(1:n)
      end do
      sumPow = iqsort( sumPow )

      print*,sum(sumPow(n*n-m+1:n*n))

      END PROGRAM


program prob3
    implicit none
    integer(8) :: n, a, b, ans
    read(*,*) n,a,b
    if(mod(a+b, 2) == 0) then
        ans = (b-a) / 2
    else
        ans = min(2*n - (a+b) + 1, a+b-1)
        ans = ans / 2
    end if
    write(*,*) ans
    stop
contains
end program prob3

Score : 300 points

Problem Statement

2N players are running a competitive table tennis training on N tables numbered from 1 to N.

The training consists of rounds.
In each round, the players form N pairs, one pair per table.
In each pair, competitors play a match against each other.
As a result, one of them wins and the other one loses.

The winner of the match on table X plays on table X-1 in the next round,
except for the winner of the match on table 1 who stays at table 1.

Similarly, the loser of the match on table X plays on table X+1 in the next round,
except for the loser of the match on table N who stays at table N.

Two friends are playing their first round matches on distinct tables A and B.
Let's assume that the friends are strong enough to win or lose any match at will.
What is the smallest number of rounds after which the friends can get to play a match against each other?

Constraints

2 \leq N \leq 10^{18}

1 \leq A < B \leq N

All input values are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print the smallest number of rounds after which the friends can get to play a match against each other.

Sample Input 1

5 2 4

Sample Output 1

1

If the first friend loses their match and the second friend wins their match, they will both move to table 3 and play each other in the next round.

Sample Input 2

5 2 3

Sample Output 2

2

If both friends win two matches in a row, they will both move to table 1.


      PROGRAM tableTennisTraining
      IMPLICIT NONE
      integer(16) :: n,a,b
      integer(16) :: sub,ans

      read*,n,a,b

      !base
      sub = abs( a-b )
      ans = sub/2 + (sub - (sub/2)*2)

      !ex
      if( a-1<=ans .and. b-1<=ans )then
        ans = max(a-1,b-1)
      end if

      if( n-a<=ans .and. n-b<=ans )then
        ans = max(n-a,n-b)
      end if

      print*,ans

      END PROGRAM


program prob22
    implicit none
    integer :: a,b

    read(*,*) a,b

    write(*,*) 6-a-b

    stop
contains
end program prob22

Score: 100 points

Problem Statement

Takahashi is solving quizzes. He has easily solved all but the last one.

The last quiz has three choices: 1, 2, and 3.

With his supernatural power, Takahashi has found out that the choices A and B are both wrong.

Print the correct choice for this problem.

Constraints

Each of the numbers A and B is 1, 2, or 3.

A and B are different.

Input

Input is given from Standard Input in the following format:

A
B

Output

Print the correct choice.

Sample Input 1

3
1

Sample Output 1

2

When we know 3 and 1 are both wrong, the correct choice is 2.

Sample Input 2

1
2

Sample Output 2

3


program main
  integer :: n, i
  character(100) :: s, t
  read(*,*) n
  read(*,*) s(1:n), t(1:n)

  do i = 1, n
    write(*,'(a,a)', advance='no') s(i:i), t(i:i)
  end do
  write(*,*)

end program main

Score : 200 points

Problem Statement

Given are strings s and t of length N each, both consisting of lowercase English letters.

Let us form a new string by alternating the characters of S and the characters of T, as follows: the first character of S, the first character of T, the second character of S, the second character of T, ..., the N-th character of S, the N-th character of T. Print this new string.

Constraints

1 \leq N \leq 100

|S| = |T| = N

S and T are strings consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
S T

Output

Print the string formed.

Sample Input 1

2
ip cc

Sample Output 1

icpc

Sample Input 2

8
hmhmnknk uuuuuuuu

Sample Output 2

humuhumunukunuku

Sample Input 3

5
aaaaa aaaaa

Sample Output 3

aaaaaaaaaa


program bbb

implicit none
integer :: n, i, j
character(200) :: s, t, st

read*, n
read*, s, t
do i = 1, 200
st(i:i) = ' '
end do
s = trim(s)
t = trim(t)

j = 0
do i = 1, n
j = j + 1
st(j:j) = s(i:i)
j = j + 1
st(j:j) = t(i:i)
end do

write(*,*) trim(st)

end program


program main

  implicit none

  integer(8) :: i, j , ii, jj
  integer(8) :: ij, k, tmp, jjtmp

  read(*,*) i, j

  if( i > j ) then
    ii = i
    jj = j
  else
    ii = j
    jj = i
  end if

  do
    tmp = mod(ii,jj)
    if( tmp == 0 ) then
      jjtmp = jj
      exit
    end if
    jj = tmp
    ii = jj
  end do

  print*, int( i*j/jjtmp )

end program main

Score : 300 points

Problem Statement

Takahashi is organizing a party.

At the party, each guest will receive one or more snack pieces.

Takahashi predicts that the number of guests at this party will be A or B.

Find the minimum number of pieces that can be evenly distributed to the guests in both of the cases predicted.

We assume that a piece cannot be divided and distributed to multiple guests.

Constraints

1 \leq A, B \leq 10^5

A \neq B

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the minimum number of pieces that can be evenly distributed to the guests in both of the cases with A guests and B guests.

Sample Input 1

2 3

Sample Output 1

6

When we have six snack pieces, each guest can take three pieces if we have two guests, and each guest can take two if we have three guests.

Sample Input 2

123 456

Sample Output 2

18696

Sample Input 3

100000 99999

Sample Output 3

9999900000


program brick_break
  implicit none
  integer :: n, a(200000) = 0, i, k = 0
  read(*,*) n
  read(*,*) a(1:n)
  do i = 1, n
    if (a(i) == k+1) then
      k = k+1
    end if
  end do
  if (k == 0) k = n+1
  write(*,'(i0)') n-k
end program brick_break

Score : 400 points

Problem Statement

We have N bricks arranged in a row from left to right.

The i-th brick from the left (1 \leq i \leq N) has an integer a_i written on it.

Among them, you can break at most N-1 bricks of your choice.

Let us say there are K bricks remaining. Snuke will be satisfied if, for each integer i (1 \leq i \leq K), the i-th of those brick from the left has the integer i written on it.

Find the minimum number of bricks you need to break to satisfy Snuke's desire. If his desire is unsatisfiable, print -1 instead.

Constraints

All values in input are integers.

1 \leq N \leq 200000

1 \leq a_i \leq N

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the minimum number of bricks that need to be broken to satisfy Snuke's desire, or print -1 if his desire is unsatisfiable.

Sample Input 1

3
2 1 2

Sample Output 1

1

If we break the leftmost brick, the remaining bricks have integers 1 and 2 written on them from left to right, in which case Snuke will be satisfied.

Sample Input 2

3
2 2 2

Sample Output 2

-1

In this case, there is no way to break some of the bricks to satisfy Snuke's desire.

Sample Input 3

10
3 1 4 1 5 9 2 6 5 3

Sample Output 3

7

Sample Input 4

1
1

Sample Output 4

0

There may be no need to break the bricks at all.


program double_fractorial
    implicit none
    integer(8):: n, md, ans, i,div
    read*, n

    if (mod(n,2) /= 0) then
        print*, 0
        stop
    end if

    div = 10
    ans = 0
    do while(div <= n)
        ans = ans + n/div
        div = div*5
    end do
    print*, ans
end program double_fractorial

Score : 500 points

Problem Statement

For an integer n not less than 0, let us define f(n) as follows:

f(n) = 1 (if n < 2)

f(n) = n f(n-2) (if n \geq 2)

Given is an integer N. Find the number of trailing zeros in the decimal notation of f(N).

Constraints

0 \leq N \leq 10^{18}

Input

Input is given from Standard Input in the following format:

N

Output

Print the number of trailing zeros in the decimal notation of f(N).

Sample Input 1

12

Sample Output 1

1

f(12) = 12 × 10 × 8 × 6 × 4 × 2 = 46080, which has one trailing zero.

Sample Input 2

5

Sample Output 2

0

f(5) = 5 × 3 × 1 = 15, which has no trailing zeros.

Sample Input 3

1000000000000000000

Sample Output 3

124999999999999995


program ABC147A
	integer(8)::A,B,C
	read(5,*)A,B,C
	if(A+B+C>=22)then
		print'(A)',"bust"
	else
		print'(A)',"win"
	end if
end program ABC147A

Score : 100 points

Problem Statement

Given are three integers A_1, A_2, and A_3.

If A_1+A_2+A_3 is greater than or equal to 22, print bust; otherwise, print win.

Constraints

1 \leq A_i \leq 13 \ \ (i=1,2,3)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A_1 A_2 A_3

Output

If A_1+A_2+A_3 is greater than or equal to 22, print bust; otherwise, print win.

Sample Input 1

5 7 9

Sample Output 1

win

5+7+9=21, so print win.

Sample Input 2

13 7 2

Sample Output 2

bust

13+7+2=22, so print bust.


program palindrome_philia
    implicit none
    character(100):: s
    integer(4):: i, ans, s_len

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

    do i = 1, s_len/2
        if (s(i:i) /= s(s_len-i+1:s_len-i+1)) ans = ans+1
    end do

    print*, ans
end program palindrome_philia

Score : 200 points

Problem Statement

Takahashi loves palindromes. Non-palindromic strings are unacceptable to him. Each time he hugs a string, he can change one of its characters to any character of his choice.

Given is a string S. Find the minimum number of hugs needed to make S palindromic.

Constraints

S is a string consisting of lowercase English letters.

The length of S is between 1 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

S

Output

Print the minimum number of hugs needed to make S palindromic.

Sample Input 1

redcoder

Sample Output 1

1

For example, we can change the fourth character to o and get a palindrome redooder.

Sample Input 2

vvvvvv

Sample Output 2

0

We might need no hugs at all.

Sample Input 3

abcdabc

Sample Output 3

2


program HonestOrUnkind2
  implicit none
  integer xy(15,15),a(15)
  integer n,ans,i,j,x,y,bit
  logical ok
  read(*,*) n
  xy(:,:)=-1
  do i=1,n
     read(*,*) a(i)
     do j=1,a(i)
        read(*,*) x,y
        xy(i,x)=y
     end do
  end do
  do bit=1,ishft(1,n)-1
     ok=.true.
     iloop:do i=1,n
        if(iand(bit,ishft(1,i-1))==0) cycle
        do j=1,a(i)
           if((xy(i,j)/=-1).and.(iand(bit,ishft(i,j-1))/=xy(i,j))) then
              ok=.false.
              exit iloop
           end if
        end do
     end do iloop
     if(ok) then
        ans=max(ans,popcnt(bit))
     end if
  end do
  print*,ans
end program HonestOrUnkind2

Score : 300 points

Problem Statement

There are N people numbered 1 to N. Each of them is either an honest person whose testimonies are always correct or an unkind person whose testimonies may be correct or not.

Person i gives A_i testimonies. The j-th testimony by Person i is represented by two integers x_{ij} and y_{ij}. If y_{ij} = 1, the testimony says Person x_{ij} is honest; if y_{ij} = 0, it says Person x_{ij} is unkind.

How many honest persons can be among those N people at most?

Constraints

All values in input are integers.

1 \leq N \leq 15

0 \leq A_i \leq N - 1

1 \leq x_{ij} \leq N

x_{ij} \neq i

x_{ij_1} \neq x_{ij_2} (j_1 \neq j_2)

y_{ij} = 0, 1

Input

Input is given from Standard Input in the following format:

N
A_1
x_{11} y_{11}
x_{12} y_{12}
:
x_{1A_1} y_{1A_1}
A_2
x_{21} y_{21}
x_{22} y_{22}
:
x_{2A_2} y_{2A_2}
:
A_N
x_{N1} y_{N1}
x_{N2} y_{N2}
:
x_{NA_N} y_{NA_N}

Output

Print the maximum possible number of honest persons among the N people.

Sample Input 1

3
1
2 1
1
1 1
1
2 0

Sample Output 1

2

If Person 1 and Person 2 are honest and Person 3 is unkind, we have two honest persons without inconsistencies, which is the maximum possible number of honest persons.

Sample Input 2

3
2
2 1
3 0
2
3 1
1 0
2
1 1
2 0

Sample Output 2

0

Assuming that one or more of them are honest immediately leads to a contradiction.

Sample Input 3

2
1
2 0
1
1 0

Sample Output 3

1


program xor_sum4
    implicit none
    integer(8),parameter:: md = 1000000007
    integer(4):: i,j,k
    integer(4):: n
    integer(8),allocatable::a(:), total_bits(:)
    integer(8):: ans=0

    read*, n
    allocate(a(n))
    allocate(total_bits(0:100), source=0_8)
    read*,a(:)

    do i=1,n
        do j=0,100
            if (btest(a(i),j)) total_bits(j) = total_bits(j) + 1
        end do
    end do

    do i=0,100
        ans = mod(ans + mod(mod(lshift(total_bits(i)*(n-total_bits(i)),i),md),md),md)
    end do

    print*, ans

end program xor_sum4

Score : 400 points

Problem Statement

We have N integers. The i-th integer is A_i.

Find \sum_{i=1}^{N-1}\sum_{j=i+1}^{N} (A_i \mbox{ XOR } A_j), modulo (10^9+7).

What is \mbox{ XOR }?

The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows:

When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise.

For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.)

Constraints

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

0 \leq A_i < 2^{60}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the value \sum_{i=1}^{N-1}\sum_{j=i+1}^{N} (A_i \mbox{ XOR } A_j), modulo (10^9+7).

Sample Input 1

3
1 2 3

Sample Output 1

6

We have (1\mbox{ XOR } 2)+(1\mbox{ XOR } 3)+(2\mbox{ XOR } 3)=3+2+1=6.

Sample Input 2

10
3 1 4 1 5 9 2 6 5 3

Sample Output 2

237

Sample Input 3

10
3 14 159 2653 58979 323846 2643383 27950288 419716939 9375105820

Sample Output 3

103715602

Print the sum modulo (10^9+7).