program ABC158_D
  character(100000)::S,R
  integer::Q,T,F,rev
  character(5)::query
  character(1)::C
  read(5,*)S
  read(5,*)Q
  N=len_trim(S)
  rev=0
  do I=1,Q
     read(5,'(A)')query
     if(query(1:1)=='1') then
        rev=rev+1
     else
        read(query,*)T,F,C
        if(F==1) then
           if(mod(rev,2)==1) then
              S(N+1:N+1)=C
              N=N+1
           else
              do J=N,1,-1
                 S(J+1:J+1)=S(J:J)
              end do
              S(1:1)=C
              N=N+1
           end if
        else
           if(mod(rev,2)==1) then
              do J=N,1,-1
                 S(J+1:J+1)=S(J:J)
              end do
              S(1:1)=C
              N=N+1
           else
              S(N+1:N+1)=C
              N=N+1
           end if
        end if
     end if
  end do
  write(6,*)S(1:N)
end program ABC158_D

Score : 400 points

Problem Statement

Takahashi has a string S consisting of lowercase English letters.

Starting with this string, he will produce a new one in the procedure given as follows.

The procedure consists of Q operations. In Operation i (1 \leq i \leq Q), an integer T_i is provided, which means the following:

If T_i = 1: reverse the string S.

If T_i = 2: An integer F_i and a lowercase English letter C_i are additionally provided.

If F_i = 1 : Add C_i to the beginning of the string S.

If F_i = 2 : Add C_i to the end of the string S.

Help Takahashi by finding the final string that results from the procedure.

Constraints

1 \leq |S| \leq 10^5

S consists of lowercase English letters.

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

T_i = 1 or 2.

F_i = 1 or 2, if provided.

C_i is a lowercase English letter, if provided.

Input

Input is given from Standard Input in the following format:

S
Q
Query_1
:
Query_Q

In the 3-rd through the (Q+2)-th lines, Query_i is one of the following:

1

which means T_i = 1, and:

2 F_i C_i

which means T_i = 2.

Output

Print the resulting string.

Sample Input 1

a
4
2 1 p
1
2 2 c
1

Sample Output 1

cpa

There will be Q = 4 operations. Initially, S is a.

Operation 1: Add p at the beginning of S. S becomes pa.

Operation 2: Reverse S. S becomes ap.

Operation 3: Add c at the end of S. S becomes apc.

Operation 4: Reverse S. S becomes cpa.

Thus, the resulting string is cpa.

Sample Input 2

a
6
2 2 a
2 1 b
1
2 2 c
1
1

Sample Output 2

aabc

There will be Q = 6 operations. Initially, S is a.

Operation 1: S becomes aa.

Operation 2: S becomes baa.

Operation 3: S becomes aab.

Operation 4: S becomes aabc.

Operation 5: S becomes cbaa.

Operation 6: S becomes aabc.

Thus, the resulting string is aabc.

Sample Input 3

y
1
2 1 x

Sample Output 3

xy


program String_Formation
    implicit none
    character S*400005,C*1,query*100
    character ans(-400005:400005)
    integer Q,l,r,i,T,rev,F
    integer sf(100)

    read *,S,Q
    l = 0
    r = len_trim(S)+1
    rev = 0
    ans(1:) = transfer(S,ans,size=len(S))

    do i = 1, Q
        read (*,'(a)')query
        if(query(1:1) == '1') then
             rev = rev+1
        else
            read (query,*)T,F,C
            if(F == 1) then
                if(mod(rev,2) == 1) then
                    ans(r:r) = C
                    r = r+1
                else
                    ans(l:l) = C
                    l = l-1
                endif
            else
                if(mod(rev,2) == 0) then
                    ans(r:r) = C
                    r = r+1
                else
                    ans(l:l) = C
                    l = l-1
                endif
            endif
        endif
    enddo

    if(mod(rev,2) == 0) then
        print *,ans(l+1:r-1)
    else
        print *,ans(r-1:l+1:-1)
    endif

end program String_Formation

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 divisible_substring
  implicit none
  integer :: n, p, i, j, d, k = 0, cnt(0:10000) = 0, hash = 0, pow = 1
  character(200000) :: s
  integer(8) :: x = 0
  read(*,*) n, p
  read(*,*) s
  if (p == 2 .or. p == 5) then
    do i = 1, n
      d = ichar(s(i:i)) - 48
      if (mod(d, p) == 0) then
        x = x + i
      end if
    end do
    write(*,'(i0)') x
    stop
  end if
  cnt(0) = 1
  do i = n, 1, -1
    d = ichar(s(i:i)) - 48
    hash = mod(d * pow + hash, p)
    x = x + cnt(hash)
    cnt(hash) = cnt(hash) + 1
    pow = mod(10 * pow, p)
  end do
  write(*,'(i0)') x
end program divisible_substring

Score : 500 points

Problem Statement

Takahashi has a string S of length N consisting of digits from 0 through 9.

He loves the prime number P. He wants to know how many non-empty (contiguous) substrings of S - there are N \times (N + 1) / 2 of them - are divisible by P when regarded as integers written in base ten.

Here substrings starting with a 0 also count, and substrings originated from different positions in S are distinguished, even if they are equal as strings or integers.

Compute this count to help Takahashi.

Constraints

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

S consists of digits.

|S| = N

2 \leq P \leq 10000

P is a prime number.

Input

Input is given from Standard Input in the following format:

N P
S

Output

Print the number of non-empty (contiguous) substrings of S that are divisible by P when regarded as an integer written in base ten.

Sample Input 1

4 3
3543

Sample Output 1

6

Here S = 3543. There are ten non-empty (contiguous) substrings of S:

3: divisible by 3.

35: not divisible by 3.

354: divisible by 3.

3543: divisible by 3.

5: not divisible by 3.

54: divisible by 3.

543: divisible by 3.

4: not divisible by 3.

43: not divisible by 3.

3: divisible by 3.

Six of these are divisible by 3, so print 6.

Sample Input 2

4 2
2020

Sample Output 2

10

Here S = 2020. There are ten non-empty (contiguous) substrings of S, all of which are divisible by 2, so print 10.

Note that substrings beginning with a 0 also count.

Sample Input 3

20 11
33883322005544116655

Sample Output 3

68


program divisible_substring
  implicit none
  integer :: n, p, i, m, d = 0, k, l
  character(200000) :: s
  integer(8) :: cnt(0:10000) = 0, ans = 0
  read(*,*) n, p
  read(*,*) s
  k = inv(10)
  do i = 1, n
    m = mod(ichar(s(i:i))-48,p)
    l = mod(d+m,p)
    cnt(l) = cnt(l)+1
    d = modulo(d-m*k,p)
    ans = ans+cnt(d)
  end do
  write(*,'(i0)') ans
contains
  function inv(n) result(y)
    integer, intent(in) :: n
    integer :: a, b, x, y, t, q
    a = modulo(n,p)
    b = p
    x = 0
    y = 1
    do while (b /= 0)
      q = a/b
      t = b
      b = mod(a,b)
      a = t
      t = y
      y = x
      x = t-q*x
    end do
    y = modulo(y,p)
  end
end program divisible_substring


      PROGRAM A

      INTEGER N, Ans
      READ(*,*) N
      IF(mod(N,2).EQ.1)THEN
         Ans = (N + 1) / 2
      ELSE IF(mod(N,2).EQ.0)THEN
         Ans = N / 2
      END IF
      WRITE(*,*) Ans

      END

Score : 100 points

Problem Statement

Takahashi wants to print a document with N pages double-sided, where two pages of data can be printed on one sheet of paper.

At least how many sheets of paper does he need?

Constraints

N is an integer.

1 \leq N \leq 100

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

5

Sample Output 1

3

By printing the 1-st, 2-nd pages on the 1-st sheet, 3-rd and 4-th pages on the 2-nd sheet, and 5-th page on the 3-rd sheet, we can print all the data on 3 sheets of paper.

Sample Input 2

2

Sample Output 2

1

Sample Input 3

100

Sample Output 3

50


program sample
    implicit none

    integer(8) :: i,j,k,m,n,h,w,b

    integer(8)::a(3,3),che(3,3)
    read(*,*) a
    read(*,*) n

    do i=1,n
        read(*,*)b
        do j=1,3
            do k=1,3

                if (b==a(j,k))then
                    che(j,k)=1
                end if

            end do
        end do
    end do
    do i=1,3
        if (che(i,1)==1 .and. che(i,2)==1 .and. che(i,3)==1)then
            write(*,*)'Yes'
            stop
        end if
    end do
    do i=1,3
        if (che(1,i)==1 .and. che(2,i)==1 .and. che(3,i)==1)then
            write(*,*)'Yes'
            stop
        end if
    end do

    if (che(1,1)==1 .and. che(2,2)==1 .and. che(3,3)==1)then
            write(*,*)'Yes'
            stop
    end if

    if (che(1,3)==1 .and. che(2,2)==1 .and. che(3,1)==1)then
        write(*,*)'Yes'
        stop
    end if

    write(*,*)'No'
    stop
end program sample

Score : 200 points

Problem Statement

We have a bingo card with a 3\times3 grid. The square at the i-th row from the top and the j-th column from the left contains the number A_{i, j}.

The MC will choose N numbers, b_1, b_2, \cdots, b_N. If our bingo sheet contains some of those numbers, we will mark them on our sheet.

Determine whether we will have a bingo when the N numbers are chosen, that is, the sheet will contain three marked numbers in a row, column, or diagonal.

Constraints

All values in input are integers.

1 \leq A_{i, j} \leq 100

A_{i_1, j_1} \neq A_{i_2, j_2} ((i_1, j_1) \neq (i_2, j_2))

1 \leq N \leq 10

1 \leq b_i \leq 100

b_i \neq b_j (i \neq j)

Input

Input is given from Standard Input in the following format:

A_{1, 1} A_{1, 2} A_{1, 3}
A_{2, 1} A_{2, 2} A_{2, 3}
A_{3, 1} A_{3, 2} A_{3, 3}
N
b_1
\vdots
b_N

Output

If we will have a bingo, print Yes; otherwise, print No.

Sample Input 1

84 97 66
79 89 11
61 59 7
7
89
7
87
79
24
84
30

Sample Output 1

Yes

We will mark A_{1, 1}, A_{2, 1}, A_{2, 2}, A_{3, 3}, and complete the diagonal from the top-left to the bottom-right.

Sample Input 2

41 7 46
26 89 2
78 92 8
5
6
45
16
57
17

Sample Output 2

No

We will mark nothing.

Sample Input 3

60 88 34
92 41 43
65 73 48
10
60
43
88
11
48
73
65
41
92
34

Sample Output 3

Yes

We will mark all the squares.


program bingo
  implicit none
  integer :: a(3,3), n, b, i, j, k
  logical :: x
  read(*,*) a
  read(*,*) n
  do i = 1, n
    read(*,*) b
    do j = 1, 3
      do k = 1, 3
        if (a(j,k) == b) a(j,k) = -1
      end do
    end do
  end do
  x = a(1,1) < 0 .and. a(2,2) < 0 .and. a(3,3) < 0
  x = x .or. (a(3,1) < 0 .and. a(2,2) < 0 .and. a(1,3) < 0)
  do i = 1, 3
    x = x .or. all(a(i,:) < 0)
    x = x .or. all(a(:,i) < 0)
  end do
  write(*,'(a)') trim(merge("Yes","No ",x))
end program bingo


program example
	implicit none

    integer N,A(3,3),i,j,k,count
    integer,allocatable :: b(:)

    do i=1,3
    	read(*,*) A(i,1:3)
    end do

    read(*,*) N

    allocate (b(N))

    do i=1,N
    	read(*,*) b(i)
    end do

    do i=1,3
    	count=0
    	do j=1,3
        	do k=1,N
            	if(A(i,j)==b(k)) then
                	count=count+1
                end if
            end do
        end do
        if (count==3) then
        	write(*,*) "Yes"
         	goto 1
    	end if
    end do

    do i=1,3
    	count=0
    	do j=1,3
        	do k=1,N
            	if(A(j,i)==b(k)) then
                	count=count+1
                end if
            end do
        end do
        if (count==3) then
        	write(*,*) "Yes"
         	goto 1
    	end if
    end do

    count=0

    do i=1,3
    	do j=1,N
    		if(A(i,i)==b(j)) then
            	count=count+1
            end if
        end do
    end do

    if (count==3) then
    	write(*,*) "Yes"
        goto 1
    end if

    count=0

    do i=1,N
    	if(A(1,3)==b(i)) then
        	count=count+1
        end if

    	if(A(2,2)==b(i)) then
        	count=count+1
        end if

    	if(A(3,1)==b(i)) then
        	count=count+1
        end if
    end do

    if (count==3) then
    	write(*,*) "Yes"
        goto 1
    end if

    write(*,*) "No"

1 end program example


program main
  implicit none
  integer :: i, j, k, n, a(1:3,1:3)
  integer, allocatable :: b(:)
  logical :: judge = .false.
  do i = 1, 3
    read *, a(i, :)
  end do
  read *, n
  allocate (b(n))
  do i = 1, n
    read *, b(i)
  end do

  do i = 1, n
    do j = 1, 3
      do k = 1, 3
        if (a(j, k) == b(i)) then
          a(j, k) = 0
        end if
      end do
    end do
  end do
  do j = 1, 3
    if (a(j, 1) + a(j, 2) + a(j, 3) == 0) then
      judge = .true.
    else if (a(1, j) + a(2, j) + a(3, j) == 0) then
      judge = .true.
    end if
    if (judge) exit
  end do
  if (.not. judge) then
    if (a(1,1) + a(2,2)+ a(3,3) == 0) then
      judge = .true.
    else if (a(1,3) + a(2,2) + a(3,1) == 0) then
      judge = .true.
    end if
  end if

  if (judge) then
    print '(a)', "Yes"
  else
    print '(a)', "No"
  end if

end program


program main
  implicit none
  integer, parameter :: binsize = 2000, nbin = 250
  integer :: n, i, t, l, r, numq, cnt(26, nbin)
  character(500000) :: s
  character(100) :: q
  character(1) :: c
  read *, n
  read *, s
  call prep
  read *, numq
  do i = 1, numq
    read *, t, l, q
    if (t == 1) then
      read (q, *) c
      call type1(l, c)
    else
      read (q, *) r
      call type2(l, r)
    end if
  end do
  contains

  subroutine prep()
    integer :: i, j, b
    cnt(:, :) = 0
    do i = 1, n
      j = iachar(s(i:i)) - iachar("a") + 1
      b = i_to_bin(i)
      cnt(j, b) = cnt(j, b) + 1
    end do
  end subroutine prep

  subroutine type1(i, c)
    integer, intent(in) :: i
    character(1), intent(in) :: c
    integer :: b, j
    b = i_to_bin(i)
    j = iachar(s(i:i)) - iachar("a") + 1
    cnt(j, b) = cnt(j, b) - 1
    s(i:i) = c(1:1)
    j = iachar(s(i:i)) - iachar("a") + 1
    cnt(j, b) = cnt(j, b) + 1
  end subroutine type1

  subroutine type2(l, r)
    integer, intent(in) :: l, r
    integer :: scnt(26), b, tot, a
    scnt(:) = 0
    do b = 1, nbin
      scnt(:) = scnt(:) + query(b, l, r)
    end do
    tot = 0
    do a = 1, 26
      if (scnt(a) > 0) tot = tot + 1
    end do
    print "(i0)", tot
  end subroutine type2

  pure integer function i_to_bin(i)
    integer, intent(in) :: i
    i_to_bin = (i - 1) / binsize + 1
  end function i_to_bin

  pure integer function b_to_i(b)
    integer, intent(in) :: b
    b_to_i = (b - 1) * binsize + 1
  end function b_to_i

  function query(b, l, r)
    integer :: query(26)
    integer, intent(in) :: b, l, r
    integer :: i, j, k, ic
    query(:) = 0
    i = b_to_i(b)
    j = i + binsize - 1
    if (r < i) return
    if (l > j) return
    if (l <= i .and. j <= r) then
      query(:) = cnt(:, b)
    else
      do k = max(i, l), min(j, r)
        ic = iachar(s(k:k)) - iachar("a") + 1
        query(ic) = query(ic) + 1
      end do
    end if
  end function query
end program main

Score : 500 points

Problem Statement

You are given a string S of length N consisting of lowercase English letters.

Process Q queries of the following two types:

Type 1: change the i_q-th character of S to c_q. (Do nothing if the i_q-th character is already c_q.)

Type 2: answer the number of different characters occurring in the substring of S between the l_q-th and r_q-th characters (inclusive).

Constraints

N, Q, i_q, l_q, and r_q are integers.

S is a string consisting of lowercase English letters.

c_q is a lowercase English letter.

1 \leq N \leq 500000

1 \leq Q \leq 20000

|S| = N

1 \leq i_q \leq N

1 \leq l_q \leq r_q \leq N

There is at least one query of type 2 in each testcase.

Input

Input is given from Standard Input in the following format:

N
S
Q
Query_1
\vdots
Query_Q

Here, Query_i in the 4-th through (Q+3)-th lines is one of the following:

1 i_q c_q

2 l_q r_q

Output

For each query of type 2, print a line containing the answer.

Sample Input 1

7
abcdbbd
6
2 3 6
1 5 z
2 1 1
1 4 a
1 7 d
2 1 7

Sample Output 1

3
1
5

In the first query, cdbb contains three kinds of letters: b , c , and d, so we print 3.

In the second query, S is modified to abcdzbd.

In the third query, a contains one kind of letter: a, so we print 1.

In the fourth query, S is modified to abcazbd.

In the fifth query, S does not change and is still abcazbd.

In the sixth query, abcazbd contains five kinds of letters: a, b, c, d, and z, so we print 5.


program main
  implicit none
  integer :: N, Q
  character(len=500000) :: S
  character(:),allocatable :: tmp
  character(len=1) :: c,b
  integer :: a
  integer :: i,j, l,r
  integer :: out
  read(*,*) N
  read(*,*) S
  read(*,*) Q

  do i = 1, Q
     read(*,'(i1,1x,a,1x,a)') a,b,c

     if (a == 1) then
        backspace(5)
        !read(*,'(i1,1x,g0,1x,a)') a,j,c
        read(*,*) a,j,c
        S(j:j) = c
     else
        backspace(5)
!        read(*,'(i1,1x,g0,1x,g0)') a,l,r
        read(*,*) a,l,r
        !tmp = S(l:r)
        call count_char(S(l:r),out)
        write(*,'(i0)') out
        !deallocate(tmp)
     end if

  end do
contains
  subroutine count_char(S,out)
    character(*) :: S
    integer :: out
    integer :: N
    integer :: i, tmp
    integer :: c(97:122)
    N = len_trim(S)
    c(97:122) = 0
    do i = 1,N
       c (ichar(S(i:i))) =  1
    end do
    out = count(c /=0)
  end subroutine count_char
end program main


program main
  implicit none
  integer :: N, Q
  character(len=500000) :: S
  character(:),allocatable :: tmp
  character(len=1) :: c,b
  integer :: a
  integer :: i,j, l,r
  integer :: out
  read(*,*) N
  read(*,*) S
  read(*,*) Q

  do i = 1, Q
     read(*,'(i1,1x,a,1x,a)') a,b,c

     if (a == 1) then
        backspace(5)
        !read(*,'(i1,1x,g0,1x,a)') a,j,c
        read(*,*) a,j,c
        S(j:j) = c
     else
        backspace(5)
!        read(*,'(i1,1x,g0,1x,g0)') a,l,r
        read(*,*) a,l,r
        !tmp = S(l:r)
        call count_char(S(l:r),out)
        write(*,'(i0)') out
        !deallocate(tmp)
     end if

  end do
contains
  subroutine count_char(S,out)
    character(*) :: S
    integer :: out
    integer :: N
    integer :: i, tmp
    integer :: c(97:122)
    N = len_trim(S)
    c(97:122) = 0
    do i = 1,N
       c (ichar(S(i:i))) = c(ichar(S(i:i))) + 1
    end do
    out = count(c /=0)
  end subroutine count_char
end program main


program main
	implicit none
    integer::a,b
    read(*,*)a,b
    if(a>=10)then
    	write(*,*)b
    else
    	write(*,*) b+100*(10-a)
    end if
    stop
end program main

Score : 100 points

Problem Statement

Takahashi is a member of a programming competition site, ButCoder.

Each member of ButCoder is assigned two values: Inner Rating and Displayed Rating.

The Displayed Rating of a member is equal to their Inner Rating if the member has participated in 10 or more contests. Otherwise, the Displayed Rating will be their Inner Rating minus 100 \times (10 - K) when the member has participated in K contests.

Takahashi has participated in N contests, and his Displayed Rating is R. Find his Inner Rating.

Constraints

All values in input are integers.

1 \leq N \leq 100

0 \leq R \leq 4111

Input

Input is given from Standard Input in the following format:

N R

Output

Print his Inner Rating.

Sample Input 1

2 2919

Sample Output 1

3719

Takahashi has participated in 2 contests, which is less than 10, so his Displayed Rating is his Inner Rating minus 100 \times (10 - 2) = 800.

Thus, Takahashi's Inner Rating is 2919 + 800 = 3719.

Sample Input 2

22 3051

Sample Output 2

3051


program a
integer :: n,r

read(*,*) n,r
if (n >= 10) then
write(*,*) r
else
write(*,*) r+100*n
end if

end program a


  Program Main

    Implicit None
    Integer              :: N,R

    Read(*,*) N,R

    If (N >= 10) then
       Write(*,"(I0)") R
    Else
       R = R + 100*(10-N)
       Write(*,"(I0)") R
    Endif

  End Program


program main
 implicit  none
 integer :: n,k,i
 read *, n,k
  do i=0,100
   if (n>k**i.and.n<k**(i+1)) then
    write(*,'(i0)') i+1
    stop
   else if (n==k**i) then
    write(*,'(i0)') i+1
    stop
   end if
  end do
end program main

Score : 200 points

Problem Statement

Given is an integer N. Find the number of digits that N has in base K.

Notes

For information on base-K representation, see Positional notation - Wikipedia.

Constraints

All values in input are integers.

1 \leq N \leq 10^9

2 \leq K \leq 10

Input

Input is given from Standard Input in the following format:

N K

Output

Print the number of digits that N has in base K.

Sample Input 1

11 2

Sample Output 1

4

In binary, 11 is represented as 1011.

Sample Input 2

1010101 10

Sample Output 2

7

Sample Input 3

314159265 3

Sample Output 3

18


  Program Main

    Implicit None
    Integer(8)           :: N,K,M,digit

    N = 0
    K = 0
    digit = 1
    Read(*,*) N,K
    M = K
    N = N - M
    If (N == 0) digit = digit + 1
    M = M*(K-1)
    If (N>0)then
       Check:Do
                digit = digit + 1
                N = N - M
                If (N <= 0) then
                   Exit Check
                Elseif (N == 0) then
                   digit = digit + 1
                   Exit Check
                Endif
                M = M*K
             Enddo Check
    Endif

    Write(*,"(I0)") digit

  End Program


program main

  implicit none
  integer :: n, k ,i

  read(*,*) n, k

  i = 1
  do
    if ( n <  k ) exit
    n = n / k
    i = i + 1
  end do
  print*, i

end program main


program sample
    implicit none

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

    read(*,*) n
    allocate(a(n))
    read(*,*)a
    k=sum(a)/n
    m=k+1
    x=0
    y=0
    do i=1,n
        x=x+(a(i)-k)**2
        y=y+(a(i)-m)**2

    end do
    if (x<=y)then
        write(*,*) x

    else
        write(*,*) y
    end if

    deallocate(a)
    stop
end program sample

Score : 300 points

Problem Statement

There are N people living on a number line.

The i-th person lives at coordinate X_i.

You are going to hold a meeting that all N people have to attend.

The meeting can be held at any integer coordinate. If you choose to hold the meeting at coordinate P, the i-th person will spend (X_i - P)^2 points of stamina to attend the meeting.

Find the minimum total points of stamina the N people have to spend.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq X_i \leq 100

Input

Input is given from Standard Input in the following format:

N
X_1 X_2 ... X_N

Output

Print the minimum total stamina the N people have to spend.

Sample Input 1

2
1 4

Sample Output 1

5

Assume the meeting is held at coordinate 2. In this case, the first person will spend (1 - 2)^2 points of stamina, and the second person will spend (4 - 2)^2 = 4 points of stamina, for a total of 5 points of stamina. This is the minimum total stamina that the 2 people have to spend.

Note that you can hold the meeting only at an integer coordinate.

Sample Input 2

7
14 14 2 13 56 2 37

Sample Output 2

2354


program main
    implicit none
    integer(8):: n, a, b, ans=0
    integer(8):: i,j,k
    integer(8),parameter:: md=1000000007
    integer(8),allocatable:: f(:), inv_f(:)

    read*, n, a, b

    allocate(f(0:max(n,200000)), inv_f(0:max(n,200000)))
    print*, 0
    ! f(:)=0; inv_f(:)=0
    f(0) = 1
    inv_f(0) = 1
    print*, 1
    do i=1,n
        f(i) = mod(f(i-1)*i,md)
        if (i==a .or. i==b .or. i==n-a .or. i==n-b) then
            inv_f(i) = inverse(f(i), md)
        end if
    end do
    print*, 2
    ans = 1
    block;integer(8):: p,ta
        p=n
        ta=2
        do while(p>0)
            if (mod(p,2) == 1) ans=mod(ans*ta,md)
            p=p/2
            ta=mod(ta*ta,md)
        end do
        end block

    if (ans-1 -comb(n,a)-comb(n,b) > 0) then
        print*, ans-1 -comb(n,a)-comb(n,b)
    else
        print*, mod(ans-1 -comb(n,a)-comb(n,b) + md, md)
    end if

contains
function comb(a,b) result(ret)
    integer(8):: a, b, ret
    ret = mod(mod(f(a)*inv_f(b),md)*inv_f(a-b),md)
end function

function inverse(x, md) result(inv)
    integer(8),value:: x
    integer(8):: md, inv
    integer(8):: p
    inv=1
    p = md-2

    do while(p > 0)
        if (mod(p,2) == 1) inv = mod(inv*x, md)
        p = rshift(p,1)
        x=mod(x*x,md)
    end do

end function

end program

Score : 400 points

Problem Statement

Akari has n kinds of flowers, one of each kind.

She is going to choose one or more of these flowers to make a bouquet.

However, she hates two numbers a and b, so the number of flowers in the bouquet cannot be a or b.

How many different bouquets are there that Akari can make?

Find the count modulo (10^9 + 7).

Here, two bouquets are considered different when there is a flower that is used in one of the bouquets but not in the other bouquet.

Constraints

All values in input are integers.

2 \leq n \leq 10^9

1 \leq a < b \leq \textrm{min}(n, 2 \times 10^5)

Input

Input is given from Standard Input in the following format:

n a b

Output

Print the number of bouquets that Akari can make, modulo (10^9 + 7). (If there are no such bouquets, print 0.)

Sample Input 1

4 1 3

Sample Output 1

7

In this case, Akari can choose 2 or 4 flowers to make the bouquet.

There are 6 ways to choose 2 out of the 4 flowers, and 1 way to choose 4, so there are a total of 7 different bouquets that Akari can make.

Sample Input 2

1000000000 141421 173205

Sample Output 2

34076506

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


program main

  implicit none

  integer :: n, a, b
  integer, allocatable :: sumi(:)
  integer    :: i

  integer(8)   :: ans

  read(*,*) n, a, b
  allocate( sumi(n) )

  ans = 1
  sumi = 0
  do i = 1, n
    ans = ans * int( i, kind=8 )
    sumi(i) = ans
  end do

  ans = 0
  do i = 1, n
    if( i == a .or. i == b ) cycle
    if( i == n ) then
      ans = ans + 1
    else
      ans = ans + int( sumi(n)/sumi(i)/sumi(i) )
    end if
  end do

  print*, mod( ans, ( 10*9 + 7 ) )

end program main


      PROGRAM piyo
      IMPLICIT NONE

      integer(16) :: n
      integer(16),allocatable :: a(:)
      integer(16) :: i

      read*,n
      allocate( a(n) )
      read*,a(:)
      do i = 1,n
        if( mod(a(i),2)==0 )then
          if( mod(a(i),3)==0 .or.mod(a(i),5)==0 )then
            cycle
          else
            print*,'DENIED'
            stop
          end if
        end if
      end do

      print*,'APPROVED'

      END PROGRAM

Score: 200 points

Problem Statement

You are an immigration officer in the Kingdom of AtCoder. The document carried by an immigrant has some number of integers written on it, and you need to check whether they meet certain criteria.

According to the regulation, the immigrant should be allowed entry to the kingdom if and only if the following condition is satisfied:

All even numbers written on the document are divisible by 3 or 5.

If the immigrant should be allowed entry according to the regulation, output APPROVED; otherwise, print DENIED.

Notes

The condition in the statement can be rephrased as "If x is an even number written on the document, x is divisible by 3 or 5".
Here "if" and "or" are logical terms.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq A_i \leq 1000

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \dots A_N

Output

If the immigrant should be allowed entry according to the regulation, print APPROVED; otherwise, print DENIED.

Sample Input 1

5
6 7 9 10 31

Sample Output 1

APPROVED

The even numbers written on the document are 6 and 10.

All of them are divisible by 3 or 5, so the immigrant should be allowed entry.

Sample Input 2

3
28 27 24

Sample Output 2

DENIED

28 violates the condition, so the immigrant should not be allowed entry.


      PROGRAM piyo
      IMPLICIT NONE

      integer :: n
      integer,allocatable :: a(:)
      integer :: i

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

      do i = 1,n
        if( mod(a(n),2)==0 )then
          if( mod(a(n),3)==0 .or.mod(a(n),5)==0 )then
            cycle
          else
            print*,'DENIED'
            stop
          end if
        end if
      end do

      print*,'APPROVED'

      END PROGRAM


program papers_please
  implicit none
  integer :: n, a(100) = 0, i
  read(*,*) n
  read(*,*) a(1:n)
  do i = 1, n
    if (mod(a(i),2) /= 0) cycle
    if (mod(a(i),3)*mod(a(i),5) == 0) cycle
    write(*,'(a)') "DENIED"
    stop
  end do
  write(*,'(a)') "APPROVED"
end program papers_please


program answer
  implicit none
  integer :: i, j, N, m, a
  character(len=10), allocatable :: S(:), k(:)
  character(len=10) :: temp
  integer, allocatable :: count(:)

  read(*,*) N
  allocate(S(N))
  allocate(count(N))
  count=0

  !sort
  !do i = 1, m-1
  !   do j = i+1, m
  !      if(k(i)>k(j)) then
  !         temp=k(i)
  !         k(i)=k(j)
  !         k(j)=temp
  !      end if
  !   end do
  !end do

  call heapsort(n,k)
  j=0
  do i = 1, n-1
     if(k(i)/=k(i+1)) then
        count(i)=i-j
        j=i
     end if
  end do

  do i = 1, n-1
     if(count(i)==maxval(count)) then
        write(*,*) k(i)
     end if
  end do

  deallocate(S,k,count)
  stop
  contains

  subroutine heapsort(n,array)
  implicit none
  integer, intent(in) :: n
  character(len=10) :: array(1:n)

  integer ::i,g,j,l
  character(len=10) :: t

  if(n.le.0)then
     write(6,*)"Error, at heapsort"; stop
  endif

  l=n/2+1
  g=n
  do while(g.ne.1)
     if(l.gt.1)then
        l=l-1
        t=array(L)
     else
        t=array(g)
        array(g)=array(1)
        g=g-1
        if(g.eq.1) then
           array(1)=t
           exit
        end if
     end if
     i=l
     j=l+l
     do while(j.le.g)
        if(j.lt.g)then
           if(array(j).lt.array(j+1)) then
              j=j+1
           end if
        endif
        if (t.lt.array(j))then
           array(i)=array(j)
           i=j
           j=j+j
        else
           j=g+1
        end if
     end do
     array(i)=t
  end do

  return
end subroutine heapsort

 end program answer

Score: 300 points

Problem Statement

We have N voting papers. The i-th vote (1 \leq i \leq N) has the string S_i written on it.

Print all strings that are written on the most number of votes, in lexicographical order.

Constraints

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

S_i (1 \leq i \leq N) are strings consisting of lowercase English letters.

The length of S_i (1 \leq i \leq N) is between 1 and 10 (inclusive).

Input

Input is given from Standard Input in the following format:

N
S_1
:
S_N

Output

Print all strings in question in lexicographical order.

Sample Input 1

7
beat
vet
beet
bed
vet
bet
beet

Sample Output 1

beet
vet

beet and vet are written on two sheets each, while beat, bed, and bet are written on one vote each. Thus, we should print the strings beet and vet.

Sample Input 2

8
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo

Sample Output 2

buffalo

Sample Input 3

7
bass
bass
kick
kick
bass
kick
kick

Sample Output 3

kick

Sample Input 4

4
ushi
tapu
nichia
kun

Sample Output 4

kun
nichia
tapu
ushi


module ch
  implicit none
  type char
     character(10)      :: chara
  end type char
  type(char),allocatable :: namec(:)
  type(char),allocatable :: namecmax(:)
end module ch
program main

  use ch, only : namec, namecmax

  implicit none
  integer :: n
  integer, allocatable :: s(:)
  integer :: i,j,k, nmax

  read(*,*) n

  allocate( s(n) )
  allocate( namec(n) )

  do i = 1, n
    read(*,*) namec(i)%chara
  end do

  call quicksort1( 1, n )

  j = 1
  do i = 1, n-1
    if( namec(i)%chara == namec(i+1)%chara ) then
      j = j + 1
      s(i) = j
    else
      j = 1
      s(i) = j
    end if
  end do
  s(n) = 1

  do i = 1, n
    if( s(i) == maxval( s ) ) then
      print*,namec(i)%chara
    end if
  end do

end program main
recursive subroutine quicksort1( first, last )

  use ch, only : namec, namecmax

  implicit none
  integer,intent(in)::first,last
  !
  ! Original
  ! https://gist.github.com/t-nissie/479f0f16966925fa29ea
  !
  character(100)::x,t
  integer::i,j

  x = namec((first+last)/2)%chara
  i = first
  j = last
  do
     do while ( LGT( x, namec(i)%chara ) )
        i=i+1
     end do
     do while ( LGT( namec(j)%chara, x ) )
        j=j-1
     end do
     if (i >= j) exit
     t = namec(i)%chara
     namec(i)%chara = namec(j)%chara
     namec(j)%chara = t
     i=i+1
     j=j-1
  end do
  if (first < i-1) call quicksort1( first, i-1  )
  if (j+1 < last)  call quicksort1( j+1  , last )

  return

end subroutine quicksort1
recursive subroutine quicksort2( first, last )

  use ch, only : namecmax

  implicit none
  integer,intent(in)::first,last
  !
  ! Original
  ! https://gist.github.com/t-nissie/479f0f16966925fa29ea
  !
  character(10)::x,t
  integer::i,j

  x = namecmax((first+last)/2)%chara
  i = first
  j = last
  do
     do while ( LGT( x, namecmax(i)%chara ) )
        i=i+1
     end do
     do while ( LGT( namecmax(j)%chara, x ) )
        j=j-1
     end do
     if (i >= j) exit
     t = namecmax(i)%chara
     namecmax(i)%chara = namecmax(j)%chara
     namecmax(j)%chara = t
     i=i+1
     j=j-1
  end do
  if (first < i-1) call quicksort2( first, i-1  )
  if (j+1 < last)  call quicksort2( j+1  , last )

  return

end subroutine quicksort2


module ch
  implicit none
  type char
     character(10)      :: chara
  end type char
  type(char),allocatable :: namec(:)
  type(char),allocatable :: namecmax(:)
end module ch
program main

  use ch, only : namec, namecmax

  implicit none
  integer :: n
  integer, allocatable :: s(:)
  integer :: i,j,k, nmax

  read(*,*) n

  allocate( s(n) )
  allocate( namec(n) )

  do i = 1, n
    read(*,*) namec(i)%chara
  end do

  call quicksort1( 1, n )

  j = 1
  do i = 1, n-1
    if( namec(i)%chara == namec(i+1)%chara ) then
      j = j + 1
      s(i) = j
    else
      j = 1
      s(i) = j
    end if
  end do
  s(n) = 1

  nmax = 0
  do i = 1, n
    if( s(i) == maxval( s ) ) nmax = nmax + 1
  end do

  allocate( namecmax(nmax) )

  j = 0
  do i = 1, n
    if( s(i) == maxval( s ) ) then
      j = j + 1
      namecmax(j)%chara =  namec(i)%chara
    end if
  end do

  call quicksort2( 1, nmax )

  do i = 1, nmax
    print*,namecmax(i)%chara
  end do

end program main
recursive subroutine quicksort1( first, last )

  use ch, only : namec, namecmax

  implicit none
  integer,intent(in)::first,last
  !
  ! Original
  ! https://gist.github.com/t-nissie/479f0f16966925fa29ea
  !
  character(100)::x,t
  integer::i,j

  x = namec((first+last)/2)%chara
  i = first
  j = last
  do
     do while ( LGT( x, namec(i)%chara ) )
        i=i+1
     end do
     do while ( LGT( namec(j)%chara, x ) )
        j=j-1
     end do
     if (i >= j) exit
     t = namec(i)%chara
     namec(i)%chara = namec(j)%chara
     namec(j)%chara = t
     i=i+1
     j=j-1
  end do
  if (first < i-1) call quicksort1( first, i-1  )
  if (j+1 < last)  call quicksort1( j+1  , last )

  return

end subroutine quicksort1
recursive subroutine quicksort2( first, last )

  use ch, only : namecmax

  implicit none
  integer,intent(in)::first,last
  !
  ! Original
  ! https://gist.github.com/t-nissie/479f0f16966925fa29ea
  !
  character(10)::x,t
  integer::i,j

  x = namecmax((first+last)/2)%chara
  i = first
  j = last
  do
     do while ( LGT( x, namecmax(i)%chara ) )
        i=i+1
     end do
     do while ( LGT( namecmax(j)%chara, x ) )
        j=j-1
     end do
     if (i >= j) exit
     t = namecmax(i)%chara
     namecmax(i)%chara = namecmax(j)%chara
     namecmax(j)%chara = t
     i=i+1
     j=j-1
  end do
  if (first < i-1) call quicksort2( first, i-1  )
  if (j+1 < last)  call quicksort2( j+1  , last )

  return

end subroutine quicksort2


program main
	implicit none
	character(10) s,t,u
	integer a,b

    read(*,*) s,t
    read(*,*) a,b
    read(*,*) u

    if(u==s) then
       write(*,'(i0,a,i0)') a-1,' ',b
    else
       write(*,'(i0,a,i0)') a,' ',b-1
    end if
end program main

Score : 100 points

Problem Statement

We have A balls with the string S written on each of them and B balls with the string T written on each of them.

From these balls, Takahashi chooses one with the string U written on it and throws it away.

Find the number of balls with the string S and balls with the string T that we have now.

Constraints

S, T, and U are strings consisting of lowercase English letters.

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

S \not= T

S=U or T=U.

1 \leq A,B \leq 10

A and B are integers.

Input

Input is given from Standard Input in the following format:

S T
A B
U

Output

Print the answer, with space in between.

Sample Input 1

red blue
3 4
red

Sample Output 1

2 4

Takahashi chose a ball with red written on it and threw it away.
Now we have two balls with the string S and four balls with the string T.

Sample Input 2

red blue
5 5
blue

Sample Output 2

5 4

Takahashi chose a ball with blue written on it and threw it away.
Now we have five balls with the string S and four balls with the string T.


program main
implicit none
integer::a, b
character(100)::s, t, u
	read*,s,t,a,b,u
	s=trim(s)
	t=trim(t)
	u=trim(u)
	if(s==u)then
    	print*,a-1,b
    else
    	print*,a,b-1
    end if
end program main


program main

  implicit none

  character(10) :: s, t, u
  integer :: a, b

  read(*,*) s, t
  read(*,*) a, b
  read(*,*) u

  if( u == s ) then
    print*, a-1, b
  else if( u == t ) then
    print*, a, b-1
  else
    print*, a, b
  end if

end program main


program i_miss_you___
  implicit none
  character(100) :: s
  integer :: n, i
  read(*,*) s
  n = len_trim(s)
  do i = 1, n
    s(i:i) = 'x'
  end do
  write(*,'(a)') trim(s)
end program i_miss_you___

Score : 200 points

Problem Statement

Given is a string S. Replace every character in S with x and print the result.

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

Replace every character in S with x and print the result.

Sample Input 1

sardine

Sample Output 1

xxxxxxx

Replacing every character in S with x results in xxxxxxx.

Sample Input 2

xxxx

Sample Output 2

xxxx

Sample Input 3

gone

Sample Output 3

xxxx


program main
  implicit none

  character,allocatable :: a(:)
  character(:),allocatable :: b
  integer :: nb, i
  character(100) :: c

  read( *, '(A6)' )  c
  b=trim(c)
  nb = len(b)   ! 文字列の長さ

  allocate(a(nb))             ! 文字の配列を割り付け
  do i=1,nb
     a(i)='x'
  enddo

  b = transfer(a(1:nb),b)  ! 逆順に参照した文字の配列を文字列に戻す

  print *, b

end program


program main
      implicit none
      integer :: n, cnt, i
      integer(8), allocatable :: a(:)

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

      call quicksort1( a, 1, n )
      cnt = 0
      do i = 1, n-1
        if ( a(i) == a(i+1) ) then
        else
          cnt = cnt + 1
        end if
      end do
      if ( cnt == n - 1 ) then
        print *, 'YES'
      else
        print *, 'NO'
      end if

end program main
recursive subroutine quicksort1(a, first, last)
  implicit none
  integer(8),intent(inout)::a(*)
  integer,intent(in)::first,last
  !
  ! Original
  ! https://gist.github.com/t-nissie/479f0f16966925fa29ea
  !
  integer(8)::x,t
  integer::i,j

  x = a((first+last)/2)
  i = first
  j = last
  do
     do while (a(i) < x)
        i=i+1
     end do
     do while (x < a(j))
        j=j-1
     end do
     if (i >= j) exit
     t = a(i)
     a(i) = a(j)
     a(j) = t
     i=i+1
     j=j-1
  end do
  if (first < i-1) call quicksort1(a, first, i-1)
  if (j+1 < last)  call quicksort1(a, j+1, last)

  return
end subroutine quicksort1

Score : 300 points

Problem Statement

Given is a sequence of integers A_1, A_2, ..., A_N.
If its elements are pairwise distinct, print YES; otherwise, print NO.

Constraints

2 ≤ N ≤ 200000

1 ≤ A_i ≤ 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 ... A_N

Output

If the elements of the sequence are pairwise distinct, print YES; otherwise, print NO.

Sample Input 1

5
2 6 1 4 5

Sample Output 1

YES

The elements are pairwise distinct.

Sample Input 2

6
4 1 3 1 6 2

Sample Output 2

NO

The second and fourth elements are identical.

Sample Input 3

2
10000000 10000000

Sample Output 3

NO


program kadai
integer  ::  N,i
integer,allocatable  ::  A(:)

read(*,*) N
allocate(A(N))
read(*,*) A

call qsort(A)
do i=1,N-1
    if(A(i)==A(i+1)) then
        write(*,*) "NO"
        stop
    end if
end do
write(*,*) "YES"
stop

contains
!入れ替え!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
subroutine swap(a,b)
implicit none
integer,intent(inout)  ::  a,b
integer  ::  c
c=a
a=b
b=c
end subroutine swap

subroutine qsort(x)
implicit none
integer, intent(inout)  ::  x(:)
integer  ::  first,last
first=1
last=size(x)
call q2sort(x,first,last)
return
end subroutine qsort

recursive subroutine q2sort(x,first,last)
implicit none
integer, intent(inout)  ::  x(:)
integer, intent(inout) ::  first,last
integer  ::  fir, las
integer  ::  i,j
integer  ::  n
n=x((first+last)/2)
i=first
j=last
do
do while (x(i)<n)
i=i+1
end do
do while(x(j)>n)
j=j-1
end do
if (i>j .or. i==j) then
exit
end if
call swap(x(i),x(j))
i=i+1
j=j-1
end do
if (first<i-1) then
las=i-1
call q2sort(x,first,las)
end if
if (last>j+1) then
fir=j+1
call q2sort(x,fir,last)
end if
end subroutine q2sort
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

end program kadai


program main
  implicit none

  integer, parameter ::     i8 = selected_int_kind  ( 18 )

  integer ( kind = i8 ) :: nn, i, jdg
  integer ( kind = i8 ), allocatable, dimension ( : ) :: aa, rtn

  jdg = 1
  read *, nn
  allocate ( aa ( nn ) )

  read *, aa ( : )

  call sort ( aa, nn, rtn, 1 )

  do i = 1, nn - 1
     if ( rtn ( i + 1 ) == rtn ( i ) ) then
        print *, "NO"
        jdg = -1
        exit
     end if
  end do

  if ( jdg == 1 ) then
     print *, "YES"
  end if

  contains

  subroutine sort ( src, n, rtn, pm )
    implicit none

!!! parameter ==================================================

    integer, parameter :: i8 = selected_int_kind  ( 18 )
    integer, intent ( in ) :: pm
    integer ( kind = i8 ),                               intent ( in )  :: n
    integer ( kind = i8 ), allocatable, dimension ( : ), intent ( in )  :: src
    integer ( kind = i8 ), allocatable, dimension ( : ), intent ( out ) :: rtn
    integer ( kind = i8 ), allocatable, dimension ( : ) :: tmp
    integer ( kind = i8 ) :: i, max, min, imax, imin

!!! allocate ==================================================

    allocate ( rtn ( n ) )
    allocate ( tmp ( n ) )

    max  = maxval ( src ( 1:n ) )
    imax = maxval ( src ( 1:n ) )
    min  = minval ( src ( 1:n ) )
    imin = minval ( src ( 1:n ) )

!!! from maxmum ==================================================

    if ( pm == 1 ) then
       tmp = pack ( src, src == max )
       rtn = [ tmp ( : ) ]
       do i = imin, imax
          max = max - 1
          tmp = pack ( src, src == max )
          rtn = [ rtn, tmp ]
       end do
    end if

!!! from minmum ==================================================

    if ( pm == -1 ) then
       tmp = pack ( src, src == min )
       rtn = [ tmp ( : ) ]
       do i = imin, imax
          min = min + 1
          tmp = pack ( src, src == min )
          rtn = [ rtn, tmp ]
       end do
    end if

!!! ==================================================

  end subroutine sort

end program main


program test03_3
 implicit none
 integer first, last
 integer N
 integer,allocatable :: A(:)
 integer i,j
 integer :: kekka=0

 read *, N
 allocate(A(N))
 read *,A

 first=1
 last=N

 call quicksort1(A,first,last)

 do i=1,N-1
  if (A(i)==A(i+1)) kekka=1
 end do

 if (kekka==0) print '(A)', 'YES'
 if (kekka==1) print '(A)', 'NO'

contains
recursive subroutine quicksort1(a, first, last)
implicit none
integer::a(N)
integer::first,last

 double precision::x
 integer t
 integer::i,j

 x = a((first+last)/2)
 i = first
 j = last

do
  do while (a(i) <= x)
   i=i+1
  end do
  do while (x <= a(j))
   j=j-1
  end do

  if (i >= j) exit
    t = a(i)
    a(i) = a(j)
    a(j) = t
    i=i+1
    j=j-1
end do

 if (first < i-1) call quicksort1(a, first, i-1)
 if (j+1 < last)  call quicksort1(a, j+1, last)
end subroutine quicksort1

endprogram test03_3


program main
	implicit none
    integer i,j,n,flag,l,k,tmp
    integer,allocatable :: a(:)

    flag=1 !distinct

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

    l=n/2+1
    k=n
    do while(k/=1)
      if(l<1)then
        l=l-1
        tmp=a(l)
      else
        tmp=a(k)
        a(k)=a(1)
        k=k-1
        if(k==1) then
           a(1)=tmp
           exit
        endif
     endif
     i=l
     j=l+l
     do while(j<=k)
        if(j<k)then
           if(a(j).lt.a(j+1))j=j+1
        endif
        if (tmp<a(j))then
           a(i)=a(j)
           i=j
           j=j+j
        else
           j=k+1
        endif
     enddo
     a(i)=tmp
  enddo
  do i=1,n
     if (a(i)==a(j)) then
        flag=0
        goto 99
     end if
  end do
99 continue
    if (flag==1) then
       write(*,'(a)') 'YES'
    else
       write(*,'(a)') 'NO'
    end if
end program main


program test03_3
 implicit none
 integer first, last
 integer N
 integer,allocatable :: A(:)
 integer i,j
 integer :: kekka=0

 read *, N
 allocate(A(N))
 read *,A

 first=1
 last=N

 call quicksort1(A,first,last)

 if (kekka==0) print *, 'YES'
 if (kekka==1) print *, 'NO'

contains
recursive subroutine quicksort1(a, first, last)
implicit none
integer::a(N)
integer,intent(in)::first,last

 double precision::x,t
 integer::i,j

 x = a((first+last)/2)
 i = first
 j = last

do
  do while (a(i) < x)
   i=i+1
  if ( a(i) == a(i+1) .and. i /= N ) then
! print '(i0,1x,i0)', a(i), a(i+1)
   kekka=1
   exit
  end if
  end do
  do while (x < a(j))
   j=j-1
  if ( a(j) == a(j-1) .and. j /= 1 ) then
! print '(i0,1x,i0)', a(j), a(j-1)
   kekka=1
   exit
  end if
  end do

  if (i >= j) exit
    t = a(i)
    a(i) = a(j)
    a(j) = t
    i=i+1
    j=j-1
  end do

 if (first < i-1) call quicksort1(a, first, i-1)
 if (j+1 < last)  call quicksort1(a, j+1, last)
end subroutine quicksort1

endprogram test03_3


program main
integer::i,n,k
integer,allocatable::a(:),b(:)
read(*,*)n
allocate(a(n))
	read(*,*)a(:)
    k=maxval(a)
allocate(b(k))
b=0
do i=1,n
	b(a(i))=b(a(i))+1
end do
if(maxval(b)==1)then
	print*,'YES'
else
	print*,'NO'
end if
end program main


program main
      implicit none
      integer :: n, k, i
      integer, allocatable :: p(:)
      real, allocatable :: s(:)
      real, allocatable :: sk(:)

      read *, n, k
      allocate( p(n) )
      read *, p
      allocate( s(0:n) )
      allocate( sk(n-k+1) )

      s = 0.0
      do i = 1, n
        s(i) = s(i-1)+(1+p(i))/2.0
      end do

      sk = 0.0
      do i = 1, n-k+1
        sk(i) = s(i+k-1)-s(i-1)
      end do

      print *, maxval(sk)

end program main

Score : 400 points

Problem Statement

We have N dice arranged in a line from left to right. The i-th die from the left shows p_i numbers from 1 to p_i with equal probability when thrown.

We will choose K adjacent dice, throw each of them independently, and compute the sum of the numbers shown. Find the maximum possible value of the expected value of this sum.

Constraints

1 ≤ K ≤ N ≤ 200000

1 ≤ p_i ≤ 1000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
p_1 ... p_N

Output

Print the maximum possible value of the expected value of the sum of the numbers shown.

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

Sample Input 1

5 3
1 2 2 4 5

Sample Output 1

7.000000000000

When we throw the third, fourth, and fifth dice from the left, the expected value of the sum of the numbers shown is 7. This is the maximum value we can achieve.

Sample Input 2

4 1
6 6 6 6

Sample Output 2

3.500000000000

Regardless of which die we choose, the expected value of the number shown is 3.5.

Sample Input 3

10 4
17 13 13 12 15 20 10 13 17 11

Sample Output 3

32.000000000000


program kadai
integer  ::  N,K
integer,allocatable  ::  p(:)
real(8)  ::  ans,ans_max

read(*,*) N,K
allocate(p(N))
read(*,*) p

ans=0.0_8
do i=1,K
    ans=ans+p(i)
end do
ans_max=ans

do i=1,N-K
    ans=ans-p(i)
    ans=ans+p(i+K)
    write(*,*) ans,ans_max
    ans_max=max(ans,ans_max)
end do
write(*,*) (ans_max+real(K,8))/2.0_8

end program kadai


program main
	implicit none
    integer i,j,n,k
    integer,allocatable :: p(:),q(:)

    read(*,*) n,k
    allocate(p(n))
    read(*,*) (p(i),i=1,n)
    allocate(q(n-k+1))
    q(:)=0
    do i=1,n-k+1
       do j=1,k
          q(i)=q(i)+p(i+j-1)
       end do
    end do

    write(*,'(f0.12)') real(maxval(q)+k)/2.0e0
end program main


program many_paths
    implicit none
    integer(8) r1, r2, c1, c2, i, k
    integer(8) :: ans1 = 1, ans2 = 1, ans3 = 1, ans4 = 1, ans = 0
    read *, r1, c1, r2, c2
    do i = r1+1, r1+c1
        ans1 = ans1 * i
        do k = i-r1, i-r1
            ans1 = ans1 / k
            if(k .eq. c1)exit
        end do
        if(i .eq. r1+c1)exit
    end do
    do i = r1+1, r1+c2
        ans2 = ans2 * i
        do k = i-r1, i-r1
            ans2 = ans2 / k
            if(k .eq. c2)exit
        end do
        if(i .eq. r1+c2)exit
    end do
    do i = r2+1, r2+c1
        ans3 = ans3 * i
        do k = i-r2, i-r2
            ans3 = ans3 / k
            if(k .eq. c1)exit
        end do
        if(i .eq. r2+c1)exit
    end do
    do i = r2+1, r2+c2
        ans4 = ans4 * i
        do k = i-r2, i-r2
            ans4 = ans4 / k
            if(k .eq. c2)exit
        end do
        if(i .eq. r2+c2)exit
    end do
    ans = mod(ans1+ans2+ans3+ans4, 10**9+7)
    print *, ans
end program

Score : 600 points

Problem Statement

Snuke is standing on a two-dimensional plane. In one operation, he can move by 1 in the positive x-direction, or move by 1 in the positive y-direction.

Let us define a function f(r, c) as follows:

f(r,c) := (The number of paths from the point (0, 0) to the point (r, c) that Snuke can trace by repeating the operation above)

Given are integers r_1, r_2, c_1, and c_2.
Find the sum of f(i, j) over all pair of integers (i, j) such that r_1 ≤ i ≤ r_2 and c_1 ≤ j ≤ c_2, and compute this value modulo (10^9+7).

Constraints

1 ≤ r_1 ≤ r_2 ≤ 10^6

1 ≤ c_1 ≤ c_2 ≤ 10^6

All values in input are integers.

Input

Input is given from Standard Input in the following format:

r_1 c_1 r_2 c_2

Output

Print the sum of f(i, j) modulo (10^9+7).

Sample Input 1

1 1 2 2

Sample Output 1

14

For example, there are two paths from the point (0, 0) to the point (1, 1): (0,0) → (0,1) → (1,1) and (0,0) → (1,0) → (1,1), so f(1,1)=2.

Similarly, f(1,2)=3, f(2,1)=3, and f(2,2)=6. Thus, the sum is 14.

Sample Input 2

314 159 2653 589

Sample Output 2

602215194


program main
   implicit none
   integer h,a,n
   read(*,*) h,a
   n=h/a
   if (mod(h,a)==0) then
      write(*,'(i0)') n
   else
      write(*,'(i0)') n+1
   end if
end program main

Score : 100 points

Problem Statement

Serval is fighting with a monster.

The health of the monster is H.

In one attack, Serval can decrease the monster's health by A.
There is no other way to decrease the monster's health.

Serval wins when the monster's health becomes 0 or below.

Find the number of attacks Serval needs to make before winning.

Constraints

1 \leq H \leq 10^4

1 \leq A \leq 10^4

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H A

Output

Print the number of attacks Serval needs to make before winning.

Sample Input 1

10 4

Sample Output 1

3

After one attack, the monster's health will be 6.

After two attacks, the monster's health will be 2.

After three attacks, the monster's health will be -2.

Thus, Serval needs to make three attacks to win.

Sample Input 2

1 10000

Sample Output 2

1

Sample Input 3

10000 1

Sample Output 3

10000


program ABC153A
    implicit none
    integer(8)::H,A
    read(5,*)H,A

    if(mod(H,A)==0)then
        print'(i0)',H/A
    else
        print'(i0)',H/A+1
    end if
end program ABC153A


program main
      implicit none
      integer(8) :: h
      integer(4) :: n, i
      integer(4),allocatable :: a(:)

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

      do i = 1, n
        h = h - a(i)
        if ( h <= 0 ) then
          print *, 'Yes'
          exit
        end if
      end do

      if ( h > 0 ) then
        print *, 'No'
      end if
end program main

Score : 200 points

Problem Statement

Raccoon is fighting with a monster.

The health of the monster is H.

Raccoon can use N kinds of special moves. Using the i-th move decreases the monster's health by A_i.
There is no other way to decrease the monster's health.

Raccoon wins when the monster's health becomes 0 or below.

If Raccoon can win without using the same move twice or more, print Yes; otherwise, print No.

Constraints

1 \leq H \leq 10^9

1 \leq N \leq 10^5

1 \leq A_i \leq 10^4

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H N
A_1 A_2 ... A_N

Output

If Raccoon can win without using the same move twice or more, print Yes; otherwise, print No.

Sample Input 1

10 3
4 5 6

Sample Output 1

Yes

The monster's health will become 0 or below after, for example, using the second and third moves.

Sample Input 2

20 3
4 5 6

Sample Output 2

No

Sample Input 3

210 5
31 41 59 26 53

Sample Output 3

Yes

Sample Input 4

211 5
31 41 59 26 53

Sample Output 4

No


program main

  implicit none

  integer    :: n, k, i, j
  integer(8),allocatable :: h(:)
  integer(8) :: sumh

  read(*,*) n,k
  allocate( h(n) )
  read(*,*) h
  call quicksort1( h, 1, n )
  print*,h
stop

  sumh = 0

  if( n - k > 0 ) then
    do i = 1 ,n-k
      sumh = sumh + h(i)
    end do
  end if

  write(*,*) sumh

end program main

recursive subroutine quicksort1(a, first, last)
  implicit none
  integer(8),intent(inout)::a(*)
  integer,intent(in)::first,last
  !
  ! Original
  ! https://gist.github.com/t-nissie/479f0f16966925fa29ea
  !
  integer(8)::x,t
  integer::i,j

  x = a((first+last)/2)
  i = first
  j = last
  do
     do while (a(i) < x)
        i=i+1
     end do
     do while (x < a(j))
        j=j-1
     end do
     if (i >= j) exit
     t = a(i)
     a(i) = a(j)
     a(j) = t
     i=i+1
     j=j-1
  end do
  if (first < i-1) call quicksort1(a, first, i-1)
  if (j+1 < last)  call quicksort1(a, j+1, last)

  return
end subroutine quicksort1

Score : 300 points

Problem Statement

Fennec is fighting with N monsters.

The health of the i-th monster is H_i.

Fennec can do the following two actions:

Attack: Fennec chooses one monster. That monster's health will decrease by 1.

Special Move: Fennec chooses one monster. That monster's health will become 0.

There is no way other than Attack and Special Move to decrease the monsters' health.

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

Find the minimum number of times Fennec needs to do Attack (not counting Special Move) before winning when she can use Special Move at most K times.

Constraints

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

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

1 \leq H_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
H_1 ... H_N

Output

Print the minimum number of times Fennec needs to do Attack (not counting Special Move) before winning.

Sample Input 1

3 1
4 1 5

Sample Output 1

5

By using Special Move on the third monster, and doing Attack four times on the first monster and once on the second monster, Fennec can win with five Attacks.

Sample Input 2

8 9
7 9 3 2 3 8 4 6

Sample Output 2

0

She can use Special Move on all the monsters.

Sample Input 3

3 0
1000000000 1000000000 1000000000

Sample Output 3

3000000000

Watch out for overflow.


program main

  implicit none

  integer    :: n, k, i, j
  integer(8),allocatable :: h(:)
  integer(8) :: sumh

  read(*,*) n,k
  allocate( h(n) )
  read(*,*) h
  call quick_sort( h, 1, n )
  print*, h

  sumh = 0

  if( n - k > 0 ) then
    do i = 1 ,n-k
      sumh = sumh + h(i)
    end do
  end if

  write(*,*) sumh

end program main

 recursive subroutine quick_sort( a, i, j )

  implicit none
  integer, intent(in) :: i,j
  integer(8), intent(inout) :: a(i:j)
  integer :: p, k

  if( i == j ) return

  p  = pivot(a,i,j)

  if( p /= -1 ) then
    call partition(a(i:j),i,j,a(p),k)
    call quick_sort(a(i:k-p),i,k-1)
    call quick_sort(a(k:j),k,j)
  end if

contains

subroutine partition( a, i, j, x, l )

  implicit none
  integer, intent(in) :: i,j
  integer(8), intent(in) :: x
  integer(8), intent(inout) :: a(i:j)
  integer, intent(out) :: l
  integer :: r
  integer(8) :: t

  l = i
  r = j

  do while( l <= r )
    do while( l <= j .and. a(l) <  x )
      l = l+1
    end do
    do while( r > i .and. a(r) >= x )
      r = r-1
    end do
    if( l > r ) exit
    t = a(l)
    a(l) = a(r)
    a(r) = t
    l = l+1
    r = r-1
  end do

end subroutine partition

function pivot( a, i, j ) result( k )

  implicit none
  integer, intent(in) :: i,j
  integer:: k
  integer(8), intent(in) :: a(i:j)

  k = i + 1

  do while( k <= j .and. a(i) == a(k) )
    k = k + 1
  end do

  if( k > j       ) k = -1

  if( a(i) > a(k) ) k = i

end function pivot

end subroutine quick_sort


program main
    implicit none
    integer(8), allocatable:: h(:)
    integer(8):: n, k, one, ans
    one =1
    read*,n,k

    if (k >= n)then
        print*, 0
        stop
    end if

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

    call marge_sort(h(:), one, n)
    ans = sum(h(k+1:))
    print*, ans
contains
recursive subroutine marge_sort(arr,first,last)
    integer(8):: arr(:), tmp
    integer(8):: first, last, mean

    if (last - first <= 2) then
        if (arr(first) <= arr(last))then
            tmp = arr(first)
            arr(first) = arr(last)
            arr(last) = tmp
        end if
        return
    end if

    mean = (first + last) / 2
    call marge_sort(arr, first, mean)
    call marge_sort(arr, mean+1, last)
    call marge(arr, first, mean, last)

end subroutine

subroutine marge(arr, first, mean, last)
    integer(8):: arr(:)
    integer(8), allocatable:: tmp(:)
    integer(8):: first, mean, last
    integer(8):: i, i_left, i_right

    allocate(tmp(last-first+1))
    i_left = first
    i_right = mean + 1
    i = 0
    do while((i_left <= mean) .and. (i_right <= last))
        i = i + 1
        if (arr(i_left) >= arr(i_right))then
            tmp(i) = arr(i_left)
            i_left = i_left + 1
        else
            tmp(i) = arr(i_right)
            i_right = i_right + 1
        end if
    end do

    if (i_left > mean)then
        tmp(i+1:) = arr(i_right:last)
    else
        tmp(i+1:) = arr(i_left:mean)
    end if
    arr(first:last) = tmp(:)

end subroutine
end program main


module priority_queue_modules
implicit none
integer(8), pointer:: array(:)
contains

subroutine build_heap(original)
    integer(8):: original(:)

    allocate(array(size(original)))
    array(:) = original(:)
    call build_max_heap()
end subroutine

recursive subroutine max_heapify(index)
    integer(8)::rc, lc, self
    integer(4):: index

    rc = array(index*2)
    lc = array(index*2+1)
    self = array(index)

    if (self > max(rc,lc)) return
    if (index*2 > size(array)) return
    if (index*2+1 > size(array)) lc = -1

    if (rc >= lc)then
        array(index) = rc
        array(index*2) = self
        call max_heapify(index*2)
    else
        array(index) = lc
        array(index*2+1) = self
        call max_heapify(index*2+1)
    end if
end subroutine

subroutine build_max_heap()
    integer(4):: i

    do i = size(array)/2, 1, -1
        call max_heapify(i)
    end do
end subroutine

function pop_max_heap() result(ret)
    integer(8)::ret
    integer(8), pointer:: tmp(:)

    ret = array(1)
    array(1) = array(size(array))
    allocate(tmp(size(array)-1))
    tmp(:) = array(:size(array)-1)
    deallocate(array)
    allocate(array(size(tmp(:))))
    array(:) = tmp(:)
    deallocate(tmp)
    call max_heapify(1)
end function

function can_pop() result(ret)
    logical:: ret
    if (size(array) >= 1)then
        ret = .true.
    else
        ret = .false.
    end if
end function

end module

program main
    use priority_queue_modules, only: build_heap, pop_max_heap, can_pop
    implicit none
    integer(8), allocatable:: h(:), sorted_h(:)
    integer(4):: n, k, i

    read*, n, k
    allocate(h(n), sorted_h(n))
    read*, h(:)
    call build_heap(h(:))
    deallocate(h)

    do i=1, size(sorted_h(:))
        sorted_h(i) = pop_max_heap()
    end do

    print"(i0)", sum(sorted_h(k+1:))

end program main


program test03
 integer N, K
 integer,allocatable :: H(:)
 integer :: saikyou(1)=0
 integer(8) :: kougeki=0
 integer i

 read *, N, K
 allocate(H(N))

 read *, H

 do i=1,K
  saikyou=maxloc(H)
  H(saikyou(1))=0
 end do

!do i=1,N
! kougeki=kougeki+H(i)
!end do

 print '(i0)',sum(H)

endprogram test03


      module sortMod
        implicit none

        contains

        subroutine bSort( a ,kk )
          implicit none
          integer(16) :: a(:)
          integer(16) :: n,i,k,buffer,kk

          n = size(a)
          if( n==1 ) return
          if( kk==n ) kk = kk -1

          do i = 1,kk
            do k = i+1,n
              if( a(i)<a(k) )then
                buffer = a(i)
                a(i) = a(k)
                a(k) = buffer
              end if
            end do
          end do
        end subroutine

        recursive function qSort( a ) result( r )
          integer(16) :: a(:)
          integer(16),allocatable :: r(:)
          integer(16) :: k

          if( size(a)<=1 )then
            r = a
          else
            k = a(size(a)/2)
            r = [qSort(pack(a,a<k)),pack(a,a==r),qSort(pack(a,a>k))]
          end if
        end function qSort
      end module

      PROGRAM piyo
      use sortMod
      IMPLICIT NONE
      integer(16) :: n,k
      integer(16),allocatable :: h(:),sh(:)

      read*,n,k
      allocate(h(n),sh(n))
      read*,h(:)

      call bsort( h,k )
      !sh = qSort( h )

      if( k/=0 )then
        h(1:k) = 0
      end if

      print*,sum(h)

      END PROGRAM


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

    read(*,*) k
    a=k
    do i=1,100
        a=a/2
        if(a==0)then
            a=i
            exit
        endif
    end do
    write(*,*)2**a-1

    stop
end program sample

Score : 400 points

Problem Statement

Caracal is fighting with a monster.

The health of the monster is H.

Caracal can attack by choosing one monster. When a monster is attacked, depending on that monster's health, the following happens:

If the monster's health is 1, it drops to 0.

If the monster's health, X, is greater than 1, that monster disappears. Then, two new monsters appear, each with the health of \lfloor X/2 \rfloor.

(\lfloor r \rfloor denotes the greatest integer not exceeding r.)

Caracal wins when the healths of all existing monsters become 0 or below.

Find the minimum number of attacks Caracal needs to make before winning.

Constraints

1 \leq H \leq 10^{12}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H

Output

Find the minimum number of attacks Caracal needs to make before winning.

Sample Input 1

2

Sample Output 1

3

When Caracal attacks the initial monster, it disappears, and two monsters appear, each with the health of 1.

Then, Caracal can attack each of these new monsters once and win with a total of three attacks.

Sample Input 2

4

Sample Output 2

7

Sample Input 3

1000000000000

Sample Output 3

1099511627775


program main

  implicit none

  integer(8)    :: h, i, n, num

  read(*,*) h

  n = 0
  do
    n = n + 1
    h = h / 2
    if( h <= 1 ) exit
  end do

  num = 1
  do i = 1, n
    num = num + 2**i
  end do
  print*, num

end program main


program ABC153E
    implicit none
    integer(8)::H,N,div,i,j
    integer(8),allocatable,dimension(:)::A,B
    integer(8),allocatable,dimension(:,:)::dp

    read(5,*)H,N
    allocate(A(N))
    allocate(B(N))
    allocate(dp(H,N))

    do i=1,N
        read(5,*)A(i),B(i)
    end do

    do i=1,N
        do j=1,H
            if(i==1)then
                div=j/A(i)
                if(mod(j,A(i))/=0)then
                    div=div+1
                end if
                dp(j,i)=B(i)*div
            else
                if(j<=A(i))then
                    dp(j,i)=min(dp(j,i-1),B(i))
                else
                    dp(j,i)=min(dp(j,i-1),dp(j-A(i),i)+B(i))
                end if
            end if
        end do
    end do

    print'(i0,1x,i0)',H,N
    print'(i0)',dp(H,N)
end program ABC153E

Score : 500 points

Problem Statement

Ibis is fighting with a monster.

The health of the monster is H.

Ibis can cast N kinds of spells. Casting the i-th spell decreases the monster's health by A_i, at the cost of B_i Magic Points.

The same spell can be cast multiple times. There is no way other than spells to decrease the monster's health.

Ibis wins when the health of the monster becomes 0 or below.

Find the minimum total Magic Points that have to be consumed before winning.

Constraints

1 \leq H \leq 10^4

1 \leq N \leq 10^3

1 \leq A_i \leq 10^4

1 \leq B_i \leq 10^4

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H N
A_1 B_1
:
A_N B_N

Output

Print the minimum total Magic Points that have to be consumed before winning.

Sample Input 1

9 3
8 3
4 2
2 1

Sample Output 1

4

First, let us cast the first spell to decrease the monster's health by 8, at the cost of 3 Magic Points. The monster's health is now 1.

Then, cast the third spell to decrease the monster's health by 2, at the cost of 1 Magic Point. The monster's health is now -1.

In this way, we can win at the total cost of 4 Magic Points.

Sample Input 2

100 6
1 1
2 3
3 9
4 27
5 81
6 243

Sample Output 2

100

It is optimal to cast the first spell 100 times.

Sample Input 3

9999 10
540 7550
691 9680
700 9790
510 7150
415 5818
551 7712
587 8227
619 8671
588 8228
176 2461

Sample Output 3

139815


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

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

    do i = 1, n
        read*, x(i), h(i)
    end do
    ! x(:) = [(i, i=1,n)]
    ! h(:) = [(i, i=1,n)]

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

    i = 1
    j = 1
    do while(j <= n)
        do while(x(j) - x(i) <= 2*d)
            j = j + 1
        end do
        lenge(i) = j-1
        i = i + 1
    end do
    lenge(i:) = j-1

    tot = 0

    do i = 1, n
        if (h(i) == 0) cycle
        num = h(i)/a
        if (mod(h(i),a) /= 0) num = num+1
        ! h(i:lenge(i)) = max(h(i:lenge(i))-num*a, 0)
        do j=i,lenge(i)
            h(j) = max(h(j)-num*a, 0)
        end do
        tot = tot + num
    end do

    print*, tot
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 main
  implicit none
  integer :: n, m
  read *, n, m
  if (n == m) then
    print "(a)", "Yes"
  else
    print "(a)", "No"
  end if
end program main

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 A1
  implicit none
  integer :: n, m

  read (*,*) n, m
  if (m.eq.n) then
     print *, 'Yes'
  else
     print *, 'No'
  endif

end program A1


program main
    implicit none

!    character,allocatable :: str(:)
     character(:),allocatable :: ca, cb!, cca, ccb
    integer :: nb , ia, ib, i, ma, mb, tena, tenb
    character(100) :: cca, ccb
    character(1) :: a,b

    read(*,*) a, b

    read(a,*) ia     ! 内部read文で文字列を整数に変換
    read(b,*) ib     ! 内部read文で文字列を整数に変換

    tena=0
    tenb=0

    do i=1,ib
       tena=tena+10**(i-1)
       ma=tena*ia
    enddo
    do i=1,ia
       tenb=tenb+10**(i-1)
       mb=tenb*ib
    enddo

!    print *, ma, mb

    write(cca,*) ma    ! 内部write文で整数を文字列に変換
    ca =trim(adjustl(cca))
    write(ccb,*) mb    ! 内部write文で整数を文字列に変換
    cb =trim(adjustl(ccb))

    if(LGE(ca,cb).eqv. .true. ) then
       print *,cb
    else
       print *, ca
    end if

  end program main

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 ABC152B
    implicit none
    integer N,M
    read*,N,M
    if(N>M)then
        print"(A)",repeat(char(ichar("0")+M),N)
    else
        print"(A)",repeat(char(ichar("0")+N),M)
    endif
end program ABC152B


program ABC152C
    implicit none
    integer::n,ans,i
    integer,allocatable::a(:)
    read*,n
    allocate(a(n))
    read*,a
    ans = 0
    do i = 1,n
        if (minval(a(1:i)) >= a(i)) then
            ans = ans + 1
        end if
    end do
    print*,ans
end program ABC152C

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 test03_3
 implicit none
 integer,allocatable :: a(:)
 integer N
 integer i, j, jj
 integer mina
 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
  if ( j/=1 ) then
   do jj=js,j-1
    if ( a(jj) < a(j) ) then
     no = 1
     exit
    else
     if ( a(j-1) < a(j) ) then
      no=1
      cycle
     end if
    end if
   end do
  end if
 if ( no==0 ) then
  yesf=yesf+1
 end if
 no=0
end do

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

endprogram test03_3


program ccc
implicit none

integer :: n, m, i, res
integer,allocatable,dimension(:) :: p

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

res = 0
m   = p(1)
do i = 1, n
if (p(i)<=m) then
res = res + 1
m = p(i)
end if
end do

print*, res

end program


program ABC152F
    implicit none
    integer::N
    integer::MS(9,9)=0
    integer::i,j
    integer::ANS=0
    read*,N
    do i=1,N
        if(mod(i,10)/=0)then
            MS(S(i),mod(i,10))=MS(S(i),mod(i,10))+1
        endif
    end do
    do i=1,9
        do j=1,9
            ANS=ANS+MS(i,j)*MS(j,i)
        end do
    end do
    print"(i0)",ANS
contains
function S(Num)
    integer::num
    integer::k
    integer::S
    S=10
    do k=0,6
        if(NUM/10**k/=0)then
        S=min(S,NUM/10**k)
        endif
    end do
end function
end program ABC152F

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_set
  implicit none
  type t_node
    private
    type(t_node), pointer :: left => null(), right => null()
    integer :: level = 1
    integer :: e = 0
  end type
  type t_tree_set
    private
    type(t_node), pointer :: root => null()
    integer :: deflt = -1
  contains
    procedure :: size => size_of
    procedure :: clear => clear
    procedure :: add => add
    procedure :: remove => remove
    procedure :: contain => contain
    procedure :: first => firste
    procedure :: poll_first => poll_first
    procedure :: last => laste
    procedure :: poll_last => poll_last
    procedure :: floor => floore
    procedure :: lower => lowere
    procedure :: ceiling => ceilinge
    procedure :: higher => highere
  end type
contains
  function new_node(e) result(n)
    integer, 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
    integer, intent(in) :: e
    type(t_node), pointer :: t
    t => new_node(e)
    if (.not.associated(n)) return
    t => n
    if (e < t%e) then
      t%left => insert(t%left,e)
    else if (e > t%e) then
      t%right => insert(t%right,e)
    end if
    t => skew(t)
    t => split(t)
  end
  recursive function delete(n,e) result(t)
    type(t_node), pointer, intent(in) :: n
    integer, intent(in) :: e
    type(t_node), pointer :: t, l
    t => n
    if (.not.associated(n)) return
    if (e < t%e) then
      t%left => delete(t%left,e)
    else if (e > t%e) then
      t%right => delete(t%right,e)
    else
      if (is_leaf(t)) then
        t => null()
        return
      end if
      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_element(t,es,num)
    type(t_node), pointer, intent(in) :: t
    integer, intent(inout) :: es(:)
    integer, intent(inout) :: num
    if (.not.associated(t)) return
    call get_element(t%left,es,num)
    num = num+1
    es(num) = t%e
    call get_element(t%right,es,num)
  end
  integer function size_of(this)
    class(t_tree_set), intent(in) :: this
    size_of = tree_size(this%root)
  end
  subroutine clear(this)
    class(t_tree_set), intent(inout) :: this
    call release_tree(this%root)
    this%root => null()
  end
  subroutine set_default(this,deflt)
    class(t_tree_set), intent(inout) :: this
    integer, intent(in) :: deflt
    this%deflt = deflt
  end
  subroutine add(this,e)
    class(t_tree_set), intent(inout) :: this
    integer, intent(in) :: e
    this%root => insert(this%root,e)
  end
  subroutine remove(this,e)
    class(t_tree_set), intent(inout) :: this
    integer, intent(in) :: e
    this%root => delete(this%root,e)
  end
  function contain(this,e) result(ret)
    class(t_tree_set), intent(inout) :: this
    integer, intent(in) :: e
    type(t_node), pointer :: n
    logical :: ret
    ret = .false.
    n => this%root
    do while (associated(n))
      if (e < n%e) then
        n => n%left
      else if (e > n%e) then
        n => n%right
      else
        ret = .true.
        return
      end if
    end do
  end
  function firste(this) result(ret)
    class(t_tree_set), intent(inout) :: this
    type(t_node), pointer :: n
    integer :: ret
    ret = this%deflt
    n => this%root
    if (.not.associated(n)) return
    do while (associated(n%left))
      n => n%left
    end do
    ret = n%e
  end
  function poll_first(this) result(ret)
    class(t_tree_set), intent(inout) :: this
    type(t_node), pointer :: n
    integer :: ret
    ret = firste(this)
    if (ret /= this%deflt) this%root => delete(this%root,ret)
  end
  function laste(this) result(ret)
    class(t_tree_set), intent(inout) :: this
    type(t_node), pointer :: n
    integer :: ret
    ret = this%deflt
    n => this%root
    if (.not.associated(n)) return
    do while (associated(n%right))
      n => n%right
    end do
    ret = n%e
  end
  function poll_last(this) result(ret)
    class(t_tree_set), intent(inout) :: this
    type(t_node), pointer :: n
    integer :: ret
    ret = laste(this)
    if (ret /= this%deflt) this%root => delete(this%root,ret)
  end
  function floore(this,e) result(ret)
    class(t_tree_set), intent(inout) :: this
    integer, intent(in) :: e
    type(t_node), pointer :: n
    integer :: ret
    ret = this%deflt
    n => this%root
    do while (associated(n))
      if (e < n%e) then
        n => n%left
      else if (e > n%e) then
        if (ret == this%deflt) ret = n%e
        if (e-ret > e-n%e) ret = n%e
        n => n%right
      else
        ret = n%e
        return
      end if
    end do
  end
  function lowere(this,e) result(ret)
    class(t_tree_set), intent(inout) :: this
    integer, intent(in) :: e
    integer :: ret
    ret = floore(this,e-1)
  end
  function ceilinge(this,e) result(ret)
    class(t_tree_set), intent(inout) :: this
    integer, intent(in) :: e
    type(t_node), pointer :: n
    integer :: ret
    ret = this%deflt
    n => this%root
    do while (associated(n))
      if (e < n%e) then
        if (ret == this%deflt) ret = n%e
        if (e-ret < e-n%e) ret = n%e
        n => n%left
      else if (e > n%e) then
        n => n%right
      else
        ret = n%e
        return
      end if
    end do
  end
  function highere(this,e) result(ret)
    class(t_tree_set), intent(inout) :: this
    integer, intent(in) :: e
    integer :: ret
    ret = ceilinge(this,e+1)
  end
end module mod_tree_set
program flatten
  use mod_tree_set
  implicit none
  integer(8), parameter :: md = 1000000007
  integer :: n, m = 0, i, j, u, v
  integer(8) :: a(10000) = 0, s = 0, x
  logical, allocatable :: p(:)
  type(t_tree_set) :: set
  read(*,*) n
  read(*,*) a(1:n)
  do i = 1, n
    s = s+inv(a(i))
    m = max(m,a(i))
  end do
  m = ceiling(sqrt(0.5d0+m))
  allocate(p(m))
  p = .false.
  p(1) = .true.
  do i = 1, m
    if (p(i)) cycle
    do j = 2*i, m, i
      p(j) = .true.
    end do
    call set%add(i)
  end do
  deallocate(p)
  x = set%poll_first()
  do while (x > 0)
    v = 0
    do i = 1, n
      u = 0
      do while (mod(a(i),x) == 0)
        a(i) = a(i)/x
        u = u+1
      end do
      v = max(v,u)
    end do
    s = mod(s*pow(x,v),md)
    x = set%poll_first()
  end do
  do i = 1, n
    if (a(i) /= 1) call set%add(int(a(i),4))
  end do
  x = set%poll_first()
  do while (x > 0)
    s = mod(s*x,md)
    x = set%poll_first()
  end do
  write(*,'(i0)') s
contains
  function pow(a,b) result(r)
    integer(8), intent(in) :: a
    integer, intent(in) :: b
    integer :: k
    integer(8) :: r, p
    r = 1
    p = a
    k = b
    do while (k > 0)
      if (btest(k,0)) r = mod(r*p,md)
      p = mod(p*p,md)
      k = rshift(k,1)
    end do
  end
  function inv(n) result(y)
    integer(8), intent(in) :: n
    integer(8) :: a, b, x, y, t, q
    a = modulo(n,md)
    b = md
    x = 0
    y = 1
    do while (b /= 0)
      q = a/b
      t = b
      b = mod(a,b)
      a = t
      t = y
      y = x
      x = t-q*x
    end do
    y = modulo(y,md)
  end
end program flatten

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