program ken
  implicit none
  integer :: n,a,b,c,d
  character(len=200000) :: S
  integer :: dmp
  character(len=2) :: jam
  character(len=3) :: tohan
  tohan='...'
  jam='##'
  dmp=0
  read(*,*) N,A,B,C,D
  read(*,*) S
  if((index(S(A:C),jam).ne.0).or.(index(S(B:D),jam).ne.0)) then
     dmp=1
  else
     if(C.gt.D) then
        if(index(S(B-1:D+1),tohan).eq.0) then
           dmp=1
        else
           dmp=0
        endif
     else
        dmp=0
     endif
  endif

  if(dmp.eq.1)then
     write(*,'(a)') 'No'
  else
     write(*,'(a)') 'Yes'
  endif

end program ken

Score : 400 points

Problem Statement

There are N squares arranged in a row, numbered 1, 2, ..., N from left to right.
You are given a string S of length N consisting of . and #. If the i-th character of S is #, Square i contains a rock; if the i-th character of S is ., Square i is empty.

In the beginning, Snuke stands on Square A, and Fnuke stands on Square B.

You can repeat the following operation any number of times:

Choose Snuke or Fnuke, and make him jump one or two squares to the right. The destination must be one of the squares, and it must not contain a rock or the other person.

You want to repeat this operation so that Snuke will stand on Square C and Fnuke will stand on Square D.

Determine whether this is possible.

Constraints

4 \leq N \leq 200\ 000

S is a string of length N consisting of . and #.

1 \leq A, B, C, D \leq N

Square A, B, C and D do not contain a rock.

A, B, C and D are all different.

A < B

A < C

B < D

Input

Input is given from Standard Input in the following format:

N A B C D
S

Output

Print Yes if the objective is achievable, and No if it is not.

Sample Input 1

7 1 3 6 7
.#..#..

Sample Output 1

Yes

The objective is achievable by, for example, moving the two persons as follows. (A and B represent Snuke and Fnuke, respectively.)

A#B.#..

A#.B#..

.#AB#..

.#A.#B.

.#.A#B.

.#.A#.B

.#..#AB

Sample Input 2

7 1 3 7 6
.#..#..

Sample Output 2

No

Sample Input 3

15 1 3 15 13
...#.#...#.#...

Sample Output 3

Yes


program main
  implicit none
  integer :: N
  integer :: A, B, C, D
  character(len=200000) :: S
  integer,allocatable :: n_array(:)
  logical :: flagS = .false.
  integer :: AandB
  integer :: iN,is
  integer :: i
  !
  read(*,*) N, A, B, C, D
  read(*,*) S
  !
  ! check Stone and non-stone
  !
  is = 0
  flagS = .false.
  do i = A+1,min(C,D)
     if ( S(i:i)  == '#' ) then
        if ( flagS ) then
           write(*,'(A)') 'No'
           stop
        end if
        flagS = .true.
     else
        if ( flagS ) then
           is = is + 1
        end if
        flagS = .false.
     end if
  end do
  allocate(n_array(is))
  is = 1
  flagS = .false.
  do i = A+1,N
     if ( S(i:i)  == '#' ) then
        flagS = .true.
     else
        if ( flagS ) then
           is = is + 1
        end if
        n_array(is) = n_array(is) + 1
        flagS = .false.
     end if
  end do
  n_array(:) = n_array-3
  !
  !
  !
  AandB = B-A

  if ( C-D == 1 ) then
     write(*,'(A)') 'No'
     stop
  elseif ( C > D ) then
     if ( count(n_array > 0)  > 0) then
        write(*,'(A)') 'Yes'
        stop
     end if
  end if
  write(*,'(A)') 'Yes'
  stop
end program main

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

Submitted source-code file size, in bytes.

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

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


program kadai
implicit none
integer  ::  n,a,b,c,d,i,flag,open_flag,op
integer,allocatable  ::  rock(:)
character(len=200000)  ::  s

read(*,*) n,a,b,c,d
read(*,*) s
allocate(rock(n))

do i=1,n
    if(s(i:i)==".") then
        rock(i)=1
    end if
    if (s(i:i)=='#') then
        rock(i)=2
    end if
end do

flag=0
open_flag=0
op=0

do i=b,d
    if (rock(i)==2) then
        if (flag==1) then
            write(*,*) "No"
            stop
        else
            flag=1
            open_flag=0
        end if
    end if
    if(rock(i)==1) then
        flag=0
        open_flag=open_flag+1
        if(open_flag==3) then
            op=1
        end if
    end if
end do

flag=0
do i=a,c
    if (rock(i)==2) then
        if (flag==1) then
            write(*,*) "No"
            stop
        else
            flag=1
        end if
    end if
    if(rock(i)==1) then
        flag=0
    end if
end do

if (c>d) then
    if (op==1) then
        write(*,*) "Yes"
    else
        write(*,*) "No"
    end if
end if

if (c<d) then
    write(*,*) "Yes"
end if

end program kadai


program Guidebook
    implicit none
    integer(4):: n,i,j
    integer(4),allocatable:: p(:), ind(:)
    character(10),allocatable:: s(:)
    logical:: changed

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

    do i=1,n
        read*, s(i),p(i)
        ind(i) = i
    end do

    changed = .true.
    do while(changed)
        changed=.false.
        do i=1, n-1
            if (s(i+1) < s(i)) then
                changed = .true.
                call cswap(s(i),s(i+1))
                call swap(p(i),p(i+1))
                call swap(ind(i),ind(i+1))
            else if (s(i+1) == s(i) .and. p(i+1) > p(i)) then
                changed = .true.
                call swap(p(i),p(i+1))
                call swap(ind(i),ind(i+1))
            end if
        end do
    end do

    do i=1,n
        print*, ind(i)
    end do

contains
subroutine swap(x,y)
    integer(4),intent(inout):: x,y
    integer(4):: tmp

    tmp = x
    x = y
    y = tmp
end subroutine

subroutine cswap(c1, c2)
    character(10),intent(inout)::c1,c2
    character(10):: tmp

    tmp = c1
    c1 = c2
    c2 = tmp
end subroutine
end program Guidebook

Score : 200 points

Problem Statement

You have decided to write a book introducing good restaurants.
There are N restaurants that you want to introduce: Restaurant 1, Restaurant 2, ..., Restaurant N. Restaurant i is in city S_i, and your assessment score of that restaurant on a 100-point scale is P_i.
No two restaurants have the same score.

You want to introduce the restaurants in the following order:

The restaurants are arranged in lexicographical order of the names of their cities.

If there are multiple restaurants in the same city, they are arranged in descending order of score.

Print the identification numbers of the restaurants in the order they are introduced in the book.

Constraints

1 ≤ N ≤ 100

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

0 ≤ P_i ≤ 100

P_i is an integer.

P_i ≠ P_j (1 ≤ i < j ≤ N)

Input

Input is given from Standard Input in the following format:

N
S_1 P_1
:
S_N P_N

Output

Print N lines. The i-th line (1 ≤ i ≤ N) should contain the identification number of the restaurant that is introduced i-th in the book.

Sample Input 1

6
khabarovsk 20
moscow 10
kazan 50
kazan 35
moscow 60
khabarovsk 40

Sample Output 1

3
4
6
1
5
2

The lexicographical order of the names of the three cities is kazan < khabarovsk < moscow. For each of these cities, the restaurants in it are introduced in descending order of score. Thus, the restaurants are introduced in the order 3,4,6,1,5,2.

Sample Input 2

10
yakutsk 10
yakutsk 20
yakutsk 30
yakutsk 40
yakutsk 50
yakutsk 60
yakutsk 70
yakutsk 80
yakutsk 90
yakutsk 100

Sample Output 2

10
9
8
7
6
5
4
3
2
1


PROGRAM ATCODER

implicit none
integer(16) :: n,i,ti,tp,j
character(len=10) :: tmp
integer(16),allocatable :: p(:),turn(:)
character(len=10),allocatable :: s(:)

read*, n
allocate(s(n),p(n),turn(n))
do i = 1, n, 1
  read*, s(i),p(i)
end do

do i = 1, n, 1
  turn(i) = i
end do

do i = 1, n-1, 1
  do j = i+1, n, 1
    if ( p(i) < p(j) ) then
      tp = p(i)
      p(i) = p(j)
      p(j) = tp
      ti = turn(i)
      turn(i) = turn(j)
      turn(j) = ti
      tmp = s(i)
      s(i) = s(j)
      s(j) = tmp
    end if
  end do
end do

do i = 1, n-1, 1
  do j = i+1, n, 1
    if ( s(i) >= s(j) ) then
      tmp = s(i)
      s(i) = s(j)
      s(j) = tmp
      ti = turn(i)
      turn(i) = turn(j)
      turn(j) = ti
    end if
  end do
end do

print'(i0)', turn(1:n)

END PROGRAM ATCODER


      PROGRAM piyo
      IMPLICIT NONE
      integer :: n,m,p(10),k(10),sNum(10,10),ans=0
      logical :: s(10,10) = .false.,switch(10)
      integer :: i,j

      read*,n,m
      do i = 1,m
        read(fmt='(i2)',advance='no',unit=5) k(i)
        read*,sNum(i,1:k(i))
      end do
      read*,p(1:m)

      do i = 1,m
        do j = 1,k(i)
          s(i,sNum(i,j)) = .true.
        end do
      end do
 ! do i = 1,m
 !   print*,s(i,:)
 ! end do
      !s(n,k(i))

      outer:do i = 0,2**n-1
        switch = .false.
        do j = 1,n
          switch(j) = btest(i,j-1)
        end do

        do j = 1,m
          if(p(j)/= mod( count(switch(1:n).and.s(j,1:n)),2 ))then
            cycle outer
          end if
        end do
        ans = ans + 1
      end do outer

      print*,ans

 !debugg
  !    print*,n,m
  !    do i = 1,m
  !      print*,k(i),sNum(i,1:k(i))
  !    end do
  !    print*,p(1:m)

      END PROGRAM

Score : 300 points

Problem Statement

We have N switches with "on" and "off" state, and M bulbs. The switches are numbered 1 to N, and the bulbs are numbered 1 to M.

Bulb i is connected to k_i switches: Switch s_{i1}, s_{i2}, ..., and s_{ik_i}. It is lighted when the number of switches that are "on" among these switches is congruent to p_i modulo 2.

How many combinations of "on" and "off" states of the switches light all the bulbs?

Constraints

1 \leq N, M \leq 10

1 \leq k_i \leq N

1 \leq s_{ij} \leq N

s_{ia} \neq s_{ib} (a \neq b)

p_i is 0 or 1.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
k_1 s_{11} s_{12} ... s_{1k_1}
:
k_M s_{M1} s_{M2} ... s_{Mk_M}
p_1 p_2 ... p_M

Output

Print the number of combinations of "on" and "off" states of the switches that light all the bulbs.

Sample Input 1

2 2
2 1 2
1 2
0 1

Sample Output 1

1

Bulb 1 is lighted when there is an even number of switches that are "on" among the following: Switch 1 and 2.

Bulb 2 is lighted when there is an odd number of switches that are "on" among the following: Switch 2.

There are four possible combinations of states of (Switch 1, Switch 2): (on, on), (on, off), (off, on) and (off, off). Among them, only (on, on) lights all the bulbs, so we should print 1.

Sample Input 2

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

Sample Output 2

0

Bulb 1 is lighted when there is an even number of switches that are "on" among the following: Switch 1 and 2.

Bulb 2 is lighted when there is an even number of switches that are "on" among the following: Switch 1.

Bulb 3 is lighted when there is an odd number of switches that are "on" among the following: Switch 2.

Switch 1 has to be "off" to light Bulb 2 and Switch 2 has to be "on" to light Bulb 3, but then Bulb 1 will not be lighted. Thus, there are no combinations of states of the switches that light all the bulbs, so we should print 0.

Sample Input 3

5 2
3 1 2 5
2 2 3
1 0

Sample Output 3

8


program equeue
    implicit none
    integer(4),allocatable:: v(:)
    integer(4):: n,k
    integer(4):: i,j,li,ri,put, ans
    integer(4),allocatable:: get(:)

    read*, n, k
    allocate(v(n))
    read*, v(:)

    ans=-1000000000
    do li=0, min(n,k)
        do ri=0, min(n,k)-li
            put = min(n,k) - li - ri
            allocate(get(li+ri))
            get(:li) = v(:li)
            get(li+1:) = v(n-ri+1:)
            if (.not. li+ri == 0) call merge_sort(get,1,li+ri)
            do i=1, min(size(get),put)
                if (get(i) < 0) get(i) = 0
            end do
            ! print*, li, ri, sum(get)
            ans=max(ans,sum(get))
            deallocate(get)
        end do
    end do

    print*, ans

contains
recursive subroutine merge_sort(ar,fst,lst)
    integer(4), intent(inout):: ar(:)
    integer(4), intent(in):: fst,lst
    integer(4):: mdl

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

    mdl = (fst+lst)/2

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

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

    allocate(tmp(lst-fst+1))
    li = fst
    ri = mdl+1
    ti = 1

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

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

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

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

end program equeue

Score : 400 points

Problem Statement

Your friend gave you a dequeue D as a birthday present.

D is a horizontal cylinder that contains a row of N jewels.

The values of the jewels are V_1, V_2, ..., V_N from left to right. There may be jewels with negative values.

In the beginning, you have no jewel in your hands.

You can perform at most K operations on D, chosen from the following, at most K times (possibly zero):

Operation A: Take out the leftmost jewel contained in D and have it in your hand. You cannot do this operation when D is empty.

Operation B: Take out the rightmost jewel contained in D and have it in your hand. You cannot do this operation when D is empty.

Operation C: Choose a jewel in your hands and insert it to the left end of D. You cannot do this operation when you have no jewel in your hand.

Operation D: Choose a jewel in your hands and insert it to the right end of D. You cannot do this operation when you have no jewel in your hand.

Find the maximum possible sum of the values of jewels in your hands after the operations.

Constraints

All values in input are integers.

1 \leq N \leq 50

1 \leq K \leq 100

-10^7 \leq V_i \leq 10^7

Input

Input is given from Standard Input in the following format:

N K
V_1 V_2 ... V_N

Output

Print the maximum possible sum of the values of jewels in your hands after the operations.

Sample Input 1

6 4
-10 8 2 1 2 6

Sample Output 1

14

After the following sequence of operations, you have two jewels of values 8 and 6 in your hands for a total of 14, which is the maximum result.

Do operation A. You take out the jewel of value -10 from the left end of D.

Do operation B. You take out the jewel of value 6 from the right end of D.

Do operation A. You take out the jewel of value 8 from the left end of D.

Do operation D. You insert the jewel of value -10 to the right end of D.

Sample Input 2

6 4
-6 -100 50 -2 -5 -3

Sample Output 2

44

Sample Input 3

6 3
-6 -100 50 -2 -5 -3

Sample Output 3

0

It is optimal to do no operation.


program main
Implicit None
	integer A,B
	read*,A,B

	if(a.le.6)	then
		write(6,"(i0)") 0
		stop
		else if(a.le.13)	then
			write(6,"(i0)") B/2
			stop
	end if
	write(6,"(i0)") B
end program main

Score : 100 points

Problem Statement

Takahashi, who is A years old, is riding a Ferris wheel.

It costs B yen (B is an even number) to ride the Ferris wheel if you are 13 years old or older, but children between 6 and 12 years old (inclusive) can ride it for half the cost, and children who are 5 years old or younger are free of charge. (Yen is the currency of Japan.)

Find the cost of the Ferris wheel for Takahashi.

Constraints

0 ≤ A ≤ 100

2 ≤ B ≤ 1000

B is an even number.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the cost of the Ferris wheel for Takahashi.

Sample Input 1

30 100

Sample Output 1

100

Takahashi is 30 years old now, and the cost of the Ferris wheel is 100 yen.

Sample Input 2

12 100

Sample Output 2

50

Takahashi is 12 years old, and the cost of the Ferris wheel is the half of 100 yen, that is, 50 yen.

Sample Input 3

0 100

Sample Output 3

0

Takahashi is 0 years old, and he can ride the Ferris wheel for free.


program main
Implicit None
	integer i,r,x,D,n
	read(5,*) r,D,x

	do n=1,10
	x=r*x-D
	write(6,"(i0)") x
	end do

end program main

Score : 200 points

Problem Statement

The development of algae in a pond is as follows.

Let the total weight of the algae at the beginning of the year i be x_i gram. For i≥2000, the following formula holds:

x_{i+1} = rx_i - D

You are given r, D and x_{2000}. Calculate x_{2001}, ..., x_{2010} and print them in order.

Constraints

2 ≤ r ≤ 5

1 ≤ D ≤ 100

D < x_{2000} ≤ 200

All values in input are integers.

Input

Input is given from Standard Input in the following format:

r D x_{2000}

Output

Print 10 lines. The i-th line (1 ≤ i ≤ 10) should contain x_{2000+i} as an integer.

Sample Input 1

2 10 20

Sample Output 1

30
50
90
170
330
650
1290
2570
5130
10250

For example, x_{2001} = rx_{2000} - D = 2 \times 20 - 10 = 30 and x_{2002} = rx_{2001} - D = 2 \times 30 - 10 = 50.

Sample Input 2

4 40 60

Sample Output 2

200
760
3000
11960
47800
191160
764600
3058360
12233400
48933560


program C
    implicit none
    integer::N,M,i,j, max, min, l, r
    read(*,*) N, M
    do i = 1, M
        read(*,*) l, r
        if(i == 1) then
            max = r
            min = l
        else
            if(l > min) then
                min = l
            end if
            if(r < max) then
                max = r
            end if
        end if
    end do
    write(*,*) max - min + 1
    stop
end program

Score : 300 points

Problem Statement

We have N ID cards, and there are M gates.

We can pass the i-th gate if we have one of the following ID cards: the L_i-th, (L_i+1)-th, ..., and R_i-th ID cards.

How many of the ID cards allow us to pass all the gates alone?

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq L_i \leq R_i \leq N

Input

Input is given from Standard Input in the following format:

N M
L_1 R_1
L_2 R_2
\vdots
L_M R_M

Output

Print the number of ID cards that allow us to pass all the gates alone.

Sample Input 1

4 2
1 3
2 4

Sample Output 1

2

Two ID cards allow us to pass all the gates alone, as follows:

The first ID card does not allow us to pass the second gate.

The second ID card allows us to pass all the gates.

The third ID card allows us to pass all the gates.

The fourth ID card does not allow us to pass the first gate.

Sample Input 2

10 3
3 6
5 7
6 9

Sample Output 2

1

Sample Input 3

100000 1
1 100000

Sample Output 3

100000


program main
   implicit none
   integer:: n,m,i,ans=0,a=0,b=100000
   integer,allocatable,dimension(:)::l,r
   read(*,*)n,m
   allocate(l(m),r(m))
   do i=1,m
      read(*,*)l(i),r(i)
   enddo
   a=0
   do i=1,m
     a=max(a,l(i))
     b=min(b,r(i))
   enddo

   write(*,*)max(b-a+1,0)

contains
  subroutine heapsort(n,array)
  implicit none
  integer,intent(in) :: n
  integer,intent(inout) :: array(1:n)

  integer ::i,k,j,l
  integer :: t

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

  l=n/2+1
  k=n
  do while(k.ne.1)
     if(l.gt.1)then
        l=l-1
        t=array(L)
     else
        t=array(k)
        array(k)=array(1)
        k=k-1
        if(k.eq.1) then
           array(1)=t
           exit
        endif
     endif
     i=l
     j=l+l
     do while(j.le.k)
        if(j.lt.k)then
           if(array(j).lt.array(j+1))j=j+1
        endif
        if (t.lt.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 main


integer(16),allocatable,dimension(:)::A
integer(16),allocatable,dimension(:)::B
integer(16),allocatable,dimension(:)::C
integer(16) N,M
integer cindex
read*,N,M
allocate(A(N),B(M),C(M))
read*,A
call heapsort(N,A)
do i=1,M
  read*,B(i),C(i)
end do
call insertionsort(C,B)
cindex=1
do i=1,N
  if(C(cindex)>A(i))then
    A(i)=C(cindex)
    B(cindex)=B(cindex)-1
    if(B(cindex)==0)cindex=cindex+1
    if(cindex>M)exit
  else
    exit
  endif
end do
print"(I0)",sum(A)

contains
subroutine insertionsort(s,b)
  implicit none
  integer(16), intent(inout) :: s(:),b(:)
  integer(16) :: i, j, tmp
  do i = 2, size(s)  ! ループ終了時に [1, i] の範囲が昇順
    do j = i, 2, -1  ! i の位置にあった新要素が適切な位置に来るまで落ちてくる
      if (s(j) <= s(j - 1)) exit
      tmp = s(j)
      s(j) = s(j - 1)
      s(j - 1) = tmp
      tmp=b(j)
      b(j)=b(j-1)
      b(j-1)=tmp
    end do
  end do
end subroutine insertionsort

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

Score : 400 points

Problem Statement

You have N cards. On the i-th card, an integer A_i is written.

For each j = 1, 2, ..., M in this order, you will perform the following operation once:

Operation: Choose at most B_j cards (possibly zero). Replace the integer written on each chosen card with C_j.

Find the maximum possible sum of the integers written on the N cards after the M operations.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq A_i, C_i \leq 10^9

1 \leq B_i \leq N

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 ... A_N
B_1 C_1
B_2 C_2
\vdots
B_M C_M

Output

Print the maximum possible sum of the integers written on the N cards after the M operations.

Sample Input 1

3 2
5 1 4
2 3
1 5

Sample Output 1

14

By replacing the integer on the second card with 5, the sum of the integers written on the three cards becomes 5 + 5 + 4 = 14, which is the maximum result.

Sample Input 2

10 3
1 8 5 7 100 4 52 33 13 5
3 10
4 30
1 4

Sample Output 2

338

Sample Input 3

3 2
100 100 100
3 99
3 99

Sample Output 3

300

Sample Input 4

11 3
1 1 1 1 1 1 1 1 1 1 1
3 1000000000
4 1000000000
3 1000000000

Sample Output 4

10000000001

The output may not fit into a 32-bit integer type.


program aaa
implicit none

integer :: n, k, i
character(50) :: s

read*, n, k
read*, s

s=trim(s)
i=iachar(s(k:k))+32
s(k:k)=achar(i)

write(*,'(a)') s

end program

Score : 100 points

Problem Statement

You are given a string S of length N consisting of A, B and C, and an integer K which is between 1 and N (inclusive).
Print the string S after lowercasing the K-th character in it.

Constraints

1 ≤ N ≤ 50

1 ≤ K ≤ N

S is a string of length N consisting of A, B and C.

Input

Input is given from Standard Input in the following format:

N K
S

Output

Print the string S after lowercasing the K-th character in it.

Sample Input 1

3 1
ABC

Sample Output 1

aBC

Sample Input 2

4 3
CABA

Sample Output 2

CAbA


program kadai
	implicit none

	integer  ::  a(2)
    character(len=50)  ::  c
    read(*,*) a
    read(*,*) c

	c(a(2):a(2))=char(ichar(c(a(2):a(2)))+32)

	write(*,'(a)') trim(c)
    stop

end program kadai


program main
implicit none
integer :: n, k, i
character :: s*50,t*50

read(*,*) n, k
read(*,*) s(1:n)
do i = 1, n
   if( i/= k ) then
   t(i:i) = s(i:i)
   else
   t(i:i) = char( ichar(s(i:i))+32 )
   end if
end do
write(*,*) t

end program main


program main
  implicit none
  integer i, mae, ushiro
  character(4) S
  read(*, *) S
  mae = 0
  ushiro = 0
  if (S(1:1) == '0') then
    if (S(2:2) /= '0') then
      mae = 1
    endif
  else if (S(1:1) == '1') then
    if (S(2:2) == '0' .or. S(2:2) == '1' .or. S(2:2) == '2') then
      mae = 1
    endif
  end if

  if (S(3:3) == '0') then
    if (S(4:4) /= '0') then
      ushiro = 1
    endif
  else if (S(3:3) == '1') then
    if (S(4:4) == '0' .or. S(4:4) == '1' .or. S(4:4) == '2') then
      ushiro = 1
    endif
  end if

  if (mae == 1) then
    if (ushiro == 1) then
      write(*,'(A)') 'AMBIGUOUS'
    else
      write(*,'(A)') 'MMYY'
    end if
  else
    if (ushiro == 1) then
      write(*,'(A)') 'YYMM'
    else
      write(*,'(A)') 'NA'
    end if
  end if

end program main

Score : 200 points

Problem Statement

You have a digit sequence S of length 4. You are wondering which of the following formats S is in:

YYMM format: the last two digits of the year and the two-digit representation of the month (example: 01 for January), concatenated in this order

MMYY format: the two-digit representation of the month and the last two digits of the year, concatenated in this order

If S is valid in only YYMM format, print YYMM; if S is valid in only MMYY format, print MMYY; if S is valid in both formats, print AMBIGUOUS; if S is valid in neither format, print NA.

Constraints

S is a digit sequence of length 4.

Input

Input is given from Standard Input in the following format:

S

Output

Print the specified string: YYMM, MMYY, AMBIGUOUS or NA.

Sample Input 1

1905

Sample Output 1

YYMM

May XX19 is a valid date, but 19 is not valid as a month. Thus, this string is only valid in YYMM format.

Sample Input 2

0112

Sample Output 2

AMBIGUOUS

Both December XX01 and January XX12 are valid dates. Thus, this string is valid in both formats.

Sample Input 3

1700

Sample Output 3

NA

Neither 0 nor 17 is valid as a month. Thus, this string is valid in neither format.


program kadai
	implicit none

	integer  ::  a,b,c
    integer  ::  flag1,flag2
    character(len=4)  ::  Q,X
    character(len=2)  ::  W
    character(len=9)  ::  Z
    flag1=0
    flag2=0

    read(*,*) a

    b=a/100
    c=a-b*100

    Q="YYMM"
    X="MMYY"
    Z="AMBIGUOUS"
    W="NA"
    if (0<c .and. c<13) then
    	flag1=1  !YYMM
    end if

    if (0<b .and. b<13) then
    	flag2=1  !MMYY
    end if

    if (flag1==0) then
    	if(flag2==1) then
        	write(*,'(a)') trim(Q)
        else
        	write(*,'(a)') trim(W)
        end if
    end if

    if (flag1==1) then
    	if(flag2==0) then
        	write(*,'(a)') trim(X)
        else
         	write(*,'(a)') trim(Z)
        end if
     end if

end program kadai


program main
Implicit None
	integer(8)::s

	read*,s
	if(s/100 > 12 .or. s < 100)then
		if(mod(s,100) > 12 .or. mod(s,100) == 0)then
			print"(a)","NA"
			else
			print"(a)","YYMM"
		endif
		else
		if(mod(s,100) > 12)then
			print"(a)","MMYY"
			else
			print"(a)","AMBIGUOUS"
		endif
	endif
end program main


program main
  implicit none
  integer :: n, k, j
  real(8) :: res
  read (*, *) n, k
  res = 0.0d0
  do j = 1, n
    res = res + 0.5 ** d(j, k)
  end do
  write (*, "(f12.10)") res / dble(n)
  contains
  pure integer function d(j, k)
    integer, intent(in) :: j, k
    do d = 0, 100
      if (2 ** d * j >= k) return
    end do
  end function d
end program main

Score : 300 points

Problem Statement

Snuke has a fair N-sided die that shows the integers from 1 to N with equal probability and a fair coin. He will play the following game with them:

Throw the die. The current score is the result of the die.

As long as the score is between 1 and K-1 (inclusive), keep flipping the coin. The score is doubled each time the coin lands heads up, and the score becomes 0 if the coin lands tails up.

The game ends when the score becomes 0 or becomes K or above. Snuke wins if the score is K or above, and loses if the score is 0.

You are given N and K. Find the probability that Snuke wins the game.

Constraints

1 ≤ N ≤ 10^5

1 ≤ K ≤ 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the probability that Snuke wins the game. The output is considered correct when the absolute or relative error is at most 10^{-9}.

Sample Input 1

3 10

Sample Output 1

0.145833333333

If the die shows 1, Snuke needs to get four consecutive heads from four coin flips to obtain a score of 10 or above. The probability of this happening is \frac{1}{3} \times (\frac{1}{2})^4 = \frac{1}{48}.

If the die shows 2, Snuke needs to get three consecutive heads from three coin flips to obtain a score of 10 or above. The probability of this happening is \frac{1}{3} \times (\frac{1}{2})^3 = \frac{1}{24}.

If the die shows 3, Snuke needs to get two consecutive heads from two coin flips to obtain a score of 10 or above. The probability of this happening is \frac{1}{3} \times (\frac{1}{2})^2 = \frac{1}{12}.

Thus, the probability that Snuke wins is \frac{1}{48} + \frac{1}{24} + \frac{1}{12} = \frac{7}{48} \simeq 0.1458333333.

Sample Input 2

100000 5

Sample Output 2

0.999973749998


PROGRAM ATCODER

implicit none
integer :: i=0,n,k,s,c1,c2,r(17),r0
real(8) :: ans=0.d0,a(17)

r = 0
a = 0

read*, n,k

c1 = n
s = 1

if ( n >= k ) then
  r0 = n - k + 1
  c1 = k
end if

do
  i = i + 1
  if ( s > k  ) exit
  s = s*2
  c2 = k/s
  if ( mod(k,s) /= 0 ) then
    c2 = c2 + 1
  end if
  if ( c2 > n ) cycle
  r(i) = c1 - c2
  if ( c1 == c2 ) then
    r(i) = 1
  end if
  c1 = c2
end do

ans = (1.d0/real(n,8))*real(r0,8)

do i = 1, 17, 1
  if ( r(i) == 0.d0 ) cycle
  a(i) = ((1.d0/real(n,8))*(0.5d0)**real(i,8))*real(r(i),8)
  ans = ans + a(i)
end do

write(*,'(i0,f0.10)') 0,ans

END PROGRAM ATCODER


program main
 implicit none

 integer*8 i
 real*8 N,K,P

 read(*,*) N,K
 P = 0.0D0
 do i=1,int(N)
  P = P + 2 ** (-real(ceiling(log(K/real(i))/log(2.0D0))))
 end do
 write(*,*) P/N
end program main


program main
	implicit none
	integer :: N, K, counter, i, m, N_const
	real(8) :: Right, Left, ans
	read(*, *) N, K
	ans = 0.0d0
	counter = 0
	N_const = N
	if(N >= K) then
		ans = ans + dble(N-K+1)  / dble(N_const)
		N   = K
	end if
	do m = 1, 20
		Right = dble(K)*(0.50d0)**(m-1)
		Left  = dble(K)*(0.50d0)**m
		counter = 0
		do i = N, 1, -1
			if(Left <= i .AND. i < Right)then
				counter = counter + 1
			end if
		end do
		ans = ans + (dble(counter)/dble(N_const))*(0.50d0)**m
		N = N - counter
!		write(*, *) Left, Right, ans, N
		if(Left <= 1) then
			exit
		end if
	end do
	write(*, *) ans
end program main


program main
implicit none
integer :: n, k, kk
integer :: i, j, l, m, ii
real    :: p

read(*,*) n, k

kk = k
ii = 0
do while ( kk .ge. 1)
   kk = kk /2.
   ii = ii + 1
end do

m = 0
do i = 1, n
   j = i
   l = 0
   do while (j<k)
      j = j*2
      l = l + 1
   end do
   m = m + 1
end do
p = m/(n*2**ii)
write(*,'(f16.14)') p
end program main


program main
Implicit None
	integer(8)::n,k

	read*,n,k

	print"(i0)",n-k+1
end program main

Score : 100 points

Problem Statement

Snuke has N integers: 1,2,\ldots,N.
He will choose K of them and give those to Takahashi.

How many ways are there to choose K consecutive integers?

Constraints

All values in input are integers.

1 \leq K \leq N \leq 50

Input

Input is given from Standard Input in the following format:

N K

Output

Print the answer.

Sample Input 1

3 2

Sample Output 1

2

There are two ways to choose two consecutive integers: (1,2) and (2,3).

Sample Input 2

13 3

Sample Output 2

11


implicit none
integer(8) :: i, j, k, n, r, g, b, ans
read(5,*) r, g, b, n
ans = 0
do i = 0, n/r + 1
    do j = 0, n/g + 1
        do k = 0, n/b + 1
            if(r*i+g*j+b*k==n) then
            ans = ans + 1
            endif
        enddo
    enddo
enddo
!write(6, *) r, g, b, n
write(6,"(i0)") ans
end

Score : 200 points

Problem Statement

Snuke has come to a store that sells boxes containing balls. The store sells the following three kinds of boxes:

Red boxes, each containing R red balls

Green boxes, each containing G green balls

Blue boxes, each containing B blue balls

Snuke wants to get a total of exactly N balls by buying r red boxes, g green boxes and b blue boxes.
How many triples of non-negative integers (r,g,b) achieve this?

Constraints

All values in input are integers.

1 \leq R,G,B,N \leq 3000

Input

Input is given from Standard Input in the following format:

R G B N

Output

Print the answer.

Sample Input 1

1 2 3 4

Sample Output 1

4

Four triples achieve the objective, as follows:

(4,0,0)

(2,1,0)

(1,0,1)

(0,2,0)

Sample Input 2

13 1 4 3000

Sample Output 2

87058


program diverta2019c
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: n,l
    character(10),allocatable:: s(:)
    integer(int32):: i,j, ca, cb, cab,ans

    ca=0; cb=0; cab=0; ans=0
    read*, n
    allocate(s(n))
    do i=1,n
        read*, s(i)
    end do

    do i=1,n
        do j=1,len_trim(s(i))-1
            if (s(i)(j:j+1) == 'AB') ans=ans+1
        end do
    end do

    do i=1,n
        l = len_trim(s(i))
        if (s(i)(1:1) /= 'B' .and. s(i)(l:l) /= 'A') cycle

        if (s(i)(1:1) == 'B' .and. s(i)(l:l) == 'A') then
            cab=cab+1
        else if (s(i)(1:1) == 'B') then
            cb=cb+1
        else
            ca=ca+1
        end if
    end do
    if (ca==0 .and. cb==0)then
        print'(i0)', ans+cab-1
    else
        print'(i0)', ans+min(ca+cab,cb+cab)
    end if

end program diverta2019c

Score : 400 points

Problem Statement

Snuke has N strings. The i-th string is s_i.

Let us concatenate these strings into one string after arranging them in some order.
Find the maximum possible number of occurrences of AB in the resulting string.

Constraints

1 \leq N \leq 10^{4}

2 \leq |s_i| \leq 10

s_i consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

N
s_1
\vdots
s_N

Output

Print the answer.

Sample Input 1

3
ABCA
XBAZ
BAD

Sample Output 1

2

For example, if we concatenate ABCA, BAD and XBAZ in this order, the resulting string ABCABADXBAZ has two occurrences of AB.

Sample Input 2

9
BEWPVCRWH
ZZNQYIJX
BAVREA
PA
HJMYITEOX
BCJHMRMNK
BP
QVFABZ
PRGKSPUNA

Sample Output 2

4

Sample Input 3

7
RABYBBE
JOZ
BMHQUVA
BPA
ISU
MCMABAOBHZ
SZMEHMA

Sample Output 3

4


program main
  implicit none
  integer*8 :: n, cnt = 0
  read *, n

  cnt = cnt + n - 1
  do
    if (mod(n, 2) /= 0) exit
    n = n / 2
    cnt = cnt + n - 1
  end do
  print '(i0)', cnt - 1
end program

Score : 500 points

Problem Statement

Snuke received a positive integer N from Takahashi.
A positive integer m is called a favorite number when the following condition is satisfied:

The quotient and remainder of N divided by m are equal, that is, \lfloor \frac{N}{m} \rfloor = N \bmod m holds.

Find all favorite numbers and print the sum of those.

Constraints

All values in input are integers.

1 \leq N \leq 10^{12}

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

8

Sample Output 1

10

There are two favorite numbers: 3 and 7. Print the sum of these, 10.

Sample Input 2

1000000000000

Sample Output 2

2499686339916

Watch out for overflow.


program xor_partitioning
  implicit none
  integer(8), parameter :: md = 1000000007_8
  integer, parameter :: p = 1048575
  integer :: n, a(500000) = 0, x(0:500000) = 0, m, i, k
  integer(8) :: ans = 0_8, dp0(0:p) = 1_8, dpx(0:p) = 0_8, dpz(0:p) = 0_8
  integer(8) :: prev0, prevx, next0, nextx
  read(*,*) n
  read(*,*) a(1:n)
  do i = 1, n
    x(i) = xor(x(i-1),a(i))
  end do
  if (x(n) == 0) then
    m = 1
    do i = 0, n-1
      if (x(i) == 0) then
        m = m+1
        cycle
      end if
      k = m-dpz(x(i))
      dp0(x(i)) = mod(dp0(x(i))+int(k,8)*dpx(x(i)),md)
      dpx(x(i)) = mod(dpx(x(i))+dp0(x(i)),md)
      dpz(x(i)) = int(m,8)
    end do
    ans = pow(2_8,m-2)
    do i = 0, p
      ans = mod(ans+dpx(i),md)
    end do
    write(*,'(i0)') ans
    stop
  end if
  prev0 = 1_8
  prevx = 0_8
  do i = 1, n
    next0 = prev0
    nextx = prevx
    if (x(i) == 0) next0 = mod(prev0+prevx,md)
    if (x(i) == x(n)) nextx = mod(prev0+prevx,md)
    prev0 = next0
    prevx = nextx
  end do
  write(*,'(i0)') prev0
contains
  integer(8) function pow(x,n)
    integer(8), intent(in) :: x
    integer, intent(in) :: n
    integer :: m
    integer(8) :: p
    pow = 1_8
    p = x
    m = n
    do while (m > 0)
      if (btest(m,0)) pow = mod(pow*p,md)
      p = mod(p*p,md)
      m = rshift(m,1)
    end do
  end
end program xor_partitioning

Score : 800 points

Problem Statement

The beauty of a sequence a of length n is defined as a_1 \oplus \cdots \oplus a_n, where \oplus denotes the bitwise exclusive or (XOR).

You are given a sequence A of length N.
Snuke will insert zero or more partitions in A to divide it into some number of non-empty contiguous subsequences.

There are 2^{N-1} possible ways to insert partitions.
How many of them divide A into sequences whose beauties are all equal? Find this count modulo 10^{9}+7.

Constraints

All values in input are integers.

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

0 \leq A_i < 2^{20}

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \ldots A_{N}

Output

Print the answer.

Sample Input 1

3
1 2 3

Sample Output 1

3

Four ways of dividing A shown below satisfy the condition. The condition is not satisfied only if A is divided into (1),(2),(3).

(1,2,3)

(1),(2,3)

(1,2),(3)

Sample Input 2

3
1 2 2

Sample Output 2

1

Sample Input 3

32
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Sample Output 3

147483634

Find the count modulo 10^{9}+7.

Sample Input 4

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

Sample Output 4

292


program name
    implicit none
    integer(4):: a,b,t
    integer(4):: i,cnt=0

    read*, a,b,t

    i=0
    do
        i=i+1
        if(a*i > t+0.5) exit
        cnt=cnt+b
    end do

    print*, cnt
end program name

Score : 100 points

Problem Statement

A biscuit making machine produces B biscuits at the following moments: A seconds, 2A seconds, 3A seconds and each subsequent multiple of A seconds after activation.

Find the total number of biscuits produced within T + 0.5 seconds after activation.

Constraints

All values in input are integers.

1 \leq A, B, T \leq 20

Input

Input is given from Standard Input in the following format:

A B T

Output

Print the total number of biscuits produced within T + 0.5 seconds after activation.

Sample Input 1

3 5 7

Sample Output 1

10

Five biscuits will be produced three seconds after activation.

Another five biscuits will be produced six seconds after activation.

Thus, a total of ten biscuits will be produced within 7.5 seconds after activation.

Sample Input 2

3 2 9

Sample Output 2

6

Sample Input 3

20 20 19

Sample Output 3

0


integer n
integer,allocatable,dimension(:)::x,y
integer ans
read*,n
allocate(x(n),y(n))
read*,x
read*,y

ans=0
do i=1,N
  if(x(i)-y(i)>0)ans=ans+x(i)-y(i)
end do
print"(I0)",ans
end

Score : 200 points

Problem Statement

There are N gems. The value of the i-th gem is V_i.

You will choose some of these gems, possibly all or none, and get them.

However, you need to pay a cost of C_i to get the i-th gem.

Let X be the sum of the values of the gems obtained, and Y be the sum of the costs paid.

Find the maximum possible value of X-Y.

Constraints

All values in input are integers.

1 \leq N \leq 20

1 \leq C_i, V_i \leq 50

Input

Input is given from Standard Input in the following format:

N
V_1 V_2 ... V_N
C_1 C_2 ... C_N

Output

Print the maximum possible value of X-Y.

Sample Input 1

3
10 2 5
6 3 4

Sample Output 1

5

If we choose the first and third gems, X = 10 + 5 = 15 and Y = 6 + 4 = 10.
We have X-Y = 5 here, which is the maximum possible value.

Sample Input 2

4
13 21 6 19
11 30 6 15

Sample Output 2

6

Sample Input 3

1
1
50

Sample Output 3

0


implicit none
integer n,k,i,len
character(10) s

read(5,*) n
read(5,*) s
read(5,*) k

len=len_trim(s)

do i=1,len

if(s(i:i) .ne. s(k:k)) then
s(i:i)="*"
endif
enddo

write(6,*) s

end

Score : 200 points

Problem Statement

You are given a string S of length N consisting of lowercase English letters, and an integer K.
Print the string obtained by replacing every character in S that differs from the K-th character of S, with *.

Constraints

1 \leq K \leq N\leq 10

S is a string of length N consisting of lowercase English letters.

N and K are integers.

Input

Input is given from Standard Input in the following format:

N
S
K

Output

Print the string obtained by replacing every character in S that differs from the K-th character of S, with *.

Sample Input 1

5
error
2

Sample Output 1

*rr*r

The second character of S is r. When we replace every character in error that differs from r with *, we get the string *rr*r.

Sample Input 2

6
eleven
5

Sample Output 2

e*e*e*

Sample Input 3

9
education
7

Sample Output 3

******i**


PROGRAM ATCODER

implicit none
integer :: n,k,i
character(len=10) :: s
character(len=1) :: c

read*, n,s,k

c = s(k:k)

do i = 1, n
  if ( s(i:i) /= c ) then
    s(i:i) = '*'
  end if
end do

print'(a)', trim(s)

END PROGRAM ATCODER


program prob3
    implicit none
    integer::N,i,j,l,ans
    character(len=200005)::S
    read(*,*) N,S
    l = len_trim(S)
    ans = 0
    do i = 1, l-1
        if(S(i:i) .eq. "#" .and. S(i+1:i+1) .eq. ".") then
            ans = ans + 1
        end if
    end do
    write(*,*) ans
end program

Score : 300 points

Problem Statement

There are N stones arranged in a row. Every stone is painted white or black.
A string S represents the color of the stones. The i-th stone from the left is white if the i-th character of S is ., and the stone is black if the character is #.

Takahashi wants to change the colors of some stones to black or white so that there will be no white stone immediately to the right of a black stone.
Find the minimum number of stones that needs to be recolored.

Constraints

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

S is a string of length N consisting of . and #.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the minimum number of stones that needs to be recolored.

Sample Input 1

3
#.#

Sample Output 1

1

It is enough to change the color of the first stone to white.

Sample Input 2

5
#.##.

Sample Output 2

2

Sample Input 3

9
.........

Sample Output 3

0


program main
 implicit none

 integer N,M,i,j,k
 character*200000 Str
 read(*,*) N, Str
 j = 0
 do i=1,N
  if(Str(i:i) == '#') then
   M = i+1
   j=1
   exit
  end if
 end do

 if(j == 1) then
  k = 0
  do i=M,N
   if(Str(i:i) == '.') then
    k = k+1
   else
    j = j+1
   end if
  end do
  if(k == 0) then
   write(*,*) 0
  else
   write(*,*) min(j,k)
  end if
 else
  write(*,*) 0
 end if

end program main


program main
 implicit none

 integer N,M,i,j,k
 character*200000 Str
 read(*,*) N, Str
 j = 0
 do i=1,N
  if(Str(i:i) == '#') then
   M = i+1
   j=1
   exit
  end if
 end do

 if(j == 1) then
  k = 0
  do i=M,N
   if(Str(i:i) == '.') then
    k = k+1
   else
    j = j+1
   end if
  end do
  write(*,*) min(j,k)
 else
  write(*,*) 0
 end if

end program main


Program Tenka2019C

integer n,a,b
character(200000) s

read(*,*) n,s

a=0
b=0

do i=1,n
if (s(i:i)=='#') then
 a=a+1
else
 b=b+1
end if
end do

if(s(n:n)=='#') then
 write(*,*) min(a-1,b)
else
 write(*,*) min(a,b)
end if

stop
end


program main
  implicit none
  integer :: i, n, white=0, black=0
  logical :: flag = .false.
  character(200000) :: s

  read *, n
  read *, s

  do i = 1, n
    if (s(i:i) == '#') flag = .true.
    if ((flag) .and. (s(i:i) == '#')) black = black + 1
    if ((flag) .and. (s(i:i) == '.')) white = white + 1
  end do

  print '(i0)', min(black, white)

end program


integer a,b
read*, a,b
print*, max(2*max(a,b)-1, a+b)
end

Score : 100 points

Problem Statement

There are two buttons, one of size A and one of size B.

When you press a button of size X, you get X coins and the size of that button decreases by 1.

You will press a button twice. Here, you can press the same button twice, or press both buttons once.

At most how many coins can you get?

Constraints

All values in input are integers.

3 \leq A, B \leq 20

Input

Input is given from Standard Input in the following format:

A B

Output

Print the maximum number of coins you can get.

Sample Input 1

5 3

Sample Output 1

9

You can get 5 + 4 = 9 coins by pressing the button of size 5 twice, and this is the maximum result.

Sample Input 2

3 4

Sample Output 2

7

Sample Input 3

6 6

Sample Output 3

12


program prob2
    implicit none
    character(len=100005) :: S
    integer :: l, cnt, ans, i
    read(*,*) S

    l = len_trim(S)

    cnt = 0
    do i = 1, l
        if(mod(i,2) == 1) then
            if(S(i:i) == '1') then
                cnt = cnt + 1
            end if
        else
            if(S(i:i) == '0') then
                cnt = cnt + 1
            end if
        end if
    end do

    ans = cnt
    cnt = 0

    do i = 1, l
        if(mod(i,2) == 0) then
            if(S(i:i) == '1') then
                cnt = cnt + 1
            end if
        else
            if(S(i:i) == '0') then
                cnt = cnt + 1
            end if
        end if
    end do

    ans = min(ans, cnt)

    write(*,*) ans

    stop
contains
end program prob2

Score : 300 points

Problem Statement

N tiles are arranged in a row from left to right. The initial color of each tile is represented by a string S of length N.

The i-th tile from the left is painted black if the i-th character of S is 0, and painted white if that character is 1.

You want to repaint some of the tiles black or white, so that any two adjacent tiles have different colors.

At least how many tiles need to be repainted to satisfy the condition?

Constraints

1 \leq |S| \leq 10^5

S_i is 0 or 1.

Input

Input is given from Standard Input in the following format:

S

Output

Print the minimum number of tiles that need to be repainted to satisfy the condition.

Sample Input 1

000

Sample Output 1

1

The condition can be satisfied by repainting the middle tile white.

Sample Input 2

10010010

Sample Output 2

3

Sample Input 3

0

Sample Output 3

0


program main
  implicit none
  character(100000) :: s
  character(1) :: c
  integer :: n, i, res, r
  read (*, *) s
  n = len_trim(s)
  res = 0
  do i = 1, n
    write (c, "(i1)") mod(i, 2)
    if (s(i:i) == c) res = res + 1
  end do
  r = 0
  do i = 1, n
    write (c, "(i1)") mod(i + 1, 2)
    if (s(i:i) == c) r = r + 1
  end do
  res = min(res, r)
  write (*, "(i0)") res
end program main


program main
  implicit none
  character(100000) :: s
  integer :: i, k, n, a = 0, b = 0

  read *, s
  n = len_trim(s)

  do i = 1, n
    read (s(i:i), *), k
    if (k /= mod(i, 2)) a = a + 1
    if (k /= mod(i+1, 2)) b = b + 1
  end do

  print '(i0)', min(a, b)

end


module ABC124

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

  ! require all variables to be explicitly declared
  implicit none

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

  ! constants for this <module>
  integer(kind=INT32), parameter, private :: len_string_max = 100000_INT32

  ! variables for this <module>
  character(len=len_string_max, kind=1) :: given_string
  character(len=len_string_max, kind=1) :: reference_0, reference_1
  integer  (kind=INT32)                 :: len_string
  integer  (kind=INT32)                 :: num_operations(0:1)

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

  subroutine task_C

    ! variables for this <subroutine>
    integer(kind=INT32) :: itr

    ! STEP.01
    ! read out the given string
    read(unit=INPUT_UNIT, fmt='(A)') given_string

    ! STEP.02
    ! count up the number of operations
    len_string     = len_trim(given_string)
    num_operations = 0_INT32

    do itr = 1_INT32, len_string, 2_INT32
      reference_0(itr:itr) = '0'
      reference_1(itr:itr) = '1'
    end do

    do itr = 2_INT32, len_string, 2_INT32
      reference_0(itr:itr) = '1'
      reference_1(itr:itr) = '0'
    end do

    do itr = 1_INT32, len_string, 1_INT32
      if (given_string(itr:itr) .ne. reference_0(itr:itr)) num_operations(0) = num_operations(0) + 1_INT32
      if (given_string(itr:itr) .ne. reference_1(itr:itr)) num_operations(1) = num_operations(1) + 1_INT32
    end do

    ! STEP.04
    ! output the result
    write(unit=OUTPUT_UNIT, fmt='(I0)', advance='yes') min(num_operations(0), num_operations(1))

    ! STEP.END
    return

  end subroutine task_C

end module ABC124

program main

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

  ! require all variables to be explicitly declared
  implicit none

  call task_C

end program main


program main
  implicit none
  integer CC, i, n, renzoku, CC2
  character(10**5) s
  character c
  read(*, *) s
  s = trim(s)
  n = len(s)
  CC = 0
  CC2 = 0
  !01010101010
  do i = 1,n
    if (s(i:i) == ' ') then
      exit
    end if
    if (mod(i,2) == 1) then
      if (s(i:i) == '0') then
        CC = CC + 1
      else
        CC2 = CC2 + 1
      end if
    else
      if (s(i:i) == '1') then
        CC = CC + 1
      else
        CC2 = CC2 + 1
      end if
    end if
  end do
  write(*, '(i0)') min(CC,CC2)

end program main


program main
  implicit none
  character(len=100000) :: S
  character :: S1
  integer :: N,i,ic
  integer :: out
  read(*,*) S

  N=len(trim(S))
  S1=S(1:1)
  ic=1
  out=0
  do i = 2,N
     if (S1 == S(i:i)) then
        ic = ic+1
        cycle
     else
        out = out + ic/2
        S1=S(i:i)
        if (mod(ic,2) == 0) then
           ic = 2
        else
           ic = 1
        end if
     end if
  end do
  out = out + ic/2
  write(*,'(i0)') out
  stop
end program main


program main
implicit none
character :: s*100000
integer   :: i, l, res, iloop, chk( 2 )

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

do iloop = 1, 2

if( iloop == 1 ) then
   res = 0
else
   res = 1
   if( s(1:1) == '0' ) then
      s(1:1) = '1'
   else
      s(1:1) = '0'
   end if
end if
do i = 2, l
   if( s(i:i) /= s(i-1:i-1) ) then
   else if( i == 2 ) then
      if( s(i-1:i-1) =='0')then
         s(i:i) = '1'
      else
         s(i:i) = '0'
      end if
      res = res + 1
   else
      s(i:i) = s(i-2:i-2)
      res = res + 1
   end if
end do
chk( iloop ) = res
end do

write(*,'(i0)') minval( chk )

end program main


program main
  implicit none
  integer :: n, k, j, l=2, res=1
  integer :: i(1000000)
  character(100000) :: s

  read *, n, k
  read *, s
  i(:) = n + 1
  i(1) = 1

  do j = 2, n
    if (s(j:j) /= s(j-1:j-1)) then
      i(l) = j
      l = l + 1
    end if
  end do

  do j = 1, n-1
    if (i(j) > n) exit
    if (s(i(j):i(j)) == '0') res = max(res, i(j+2*k)-i(j))
    if (s(i(j):i(j)) == '1') res = max(res, i(j+2*k+1)-i(j))
  end do

  print '(i0)', res

end program

Score : 400 points

Problem Statement

N people are arranged in a row from left to right.

You are given a string S of length N consisting of 0 and 1, and a positive integer K.

The i-th person from the left is standing on feet if the i-th character of S is 0, and standing on hands if that character is 1.

You will give the following direction at most K times (possibly zero):

Direction: Choose integers l and r satisfying 1 \leq l \leq r \leq N, and flip the l-th, (l+1)-th, ..., and r-th persons. That is, for each i = l, l+1, ..., r, the i-th person from the left now stands on hands if he/she was standing on feet, and stands on feet if he/she was standing on hands.

Find the maximum possible number of consecutive people standing on hands after at most K directions.

Constraints

N is an integer satisfying 1 \leq N \leq 10^5.

K is an integer satisfying 1 \leq K \leq 10^5.

The length of the string S is N.

Each character of the string S is 0 or 1.

Input

Input is given from Standard Input in the following format:

N K
S

Output

Print the maximum possible number of consecutive people standing on hands after at most K directions.

Sample Input 1

5 1
00010

Sample Output 1

4

We can have four consecutive people standing on hands, which is the maximum result, by giving the following direction:

Give the direction with l = 1, r = 3, which flips the first, second and third persons from the left.

Sample Input 2

14 2
11101010110011

Sample Output 2

8

Sample Input 3

1 1
1

Sample Output 3

1

No directions are necessary.


program main
implicit none
integer :: n, k
character :: s*100000

integer :: i, j, nc, chank, c, m, a
integer , allocatable :: t( : ), u( : )
read(*,*) n, k
read(*,*) s(1:n)
allocate( t(n+1) )

chank = 0
nc = 1
t = 0
do i = 2, n-1
   if( s(i:i) == s(i-1:i-1) ) then
      nc = nc + 1
   else
      chank = chank+1
      t( i ) = nc
      nc = 1
   end if
end do
if( s(n:n) == s(n-1:n-1) ) then
   chank = chank + 1
   t( n+1 ) = nc+1
else
   t( n ) = nc
   t( n+1 ) = 1
   chank = chank + 2
end if
allocate( u(chank) )

nc = 0
u  = 0
do i = 1, n+1
   if( t( i ) /= 0 ) then
      nc = nc+1
      u(nc) = t(i)
   end if
end do

if( s(1:1) == '1' ) then
   c = 2*k+1
   if( c .ge. chank ) then
      write(*,*) n
      stop
   else
      m = 0
      do j = k+1, chank-k
         if( m .le. sum( u(j-k:j+k) ) ) then
            m = sum( u(j-k:j+k) )
         end if
      end do
      write(*,'(i0)') m
   end if
else
   c = 2*k
   if( c .ge. chank ) then
      write(*,'(i0)') n
      stop
   else
      m = 0
      do j = k+1, chank-k
         if( max(1,j-k) == 1 ) then
            m = sum(u(1:2*k) )
         else if( min( chank, j+k) == chank ) then
            if( m.le. sum( u(chank-2*k+1:chank) ) ) then
               m = sum( u(chank-2*k+1:chank) )
               stop
            end if
         else if( m .le. sum( u(j-k:j+k) ) ) then
            m = sum( u(j-k:j+k) )
         end if
      end do
      write(*,'(i0)') m
   end if
end if
end program main


program main
implicit none
integer :: n, k
character :: s*100000

integer :: i, j, nc, chank, c, m, a
integer , allocatable :: t( : ), u( : )
read(*,*) n, k
read(*,*) s(1:n)
allocate( t(n+1) )

chank = 0
nc = 1
t = 0
do i = 2, n-1
   if( s(i:i) == s(i-1:i-1) ) then
      nc = nc + 1
   else
      chank = chank+1
      t( i ) = nc
      nc = 1
   end if
end do
if( s(n:n) == s(n-1:n-1) ) then
   chank = chank + 1
   t( n+1 ) = nc+1
else
   t( n ) = nc
   t( n+1 ) = 1
   chank = chank + 2
end if
allocate( u(chank) )

nc = 0
u  = 0
do i = 1, n+1
   if( t( i ) /= 0 ) then
      nc = nc+1
      u(nc) = t(i)
   end if
end do

if( s(1:1) == '1' ) then
   c = 2*k+1
   if( c .ge. chank ) then
      write(*,*) n
      stop
   else
      m = 0
      do j = k+1, chank-k
         if( m .le. sum( u(max(1,j-k):min(chank,j+k)) ) ) then
            m = sum( u(max(1,j-k):min(chank,j+k)) )
         end if
      end do
      write(*,'(i0)') m
   end if

else
   c = 2*k
   if( c .ge. chank ) then
      write(*,'(i0)') n
      stop
   else
      m = 0
      do j = k+1, chank-k
         if( max(1,j-k) == 1 ) then
            m = sum(u(1:2*k) )
         else if( m .le. sum( u(max(2,j-k):min(chank,j+k)) ) ) then
            m = sum( u(max(2,j-k):min(chank,j+k)) )
         else if( min( chank, j+k) == chank ) then
            if( m.le. sum( u(chank-2*k+1:chank) ) ) then
               m = sum( u(chank-2*k+1:chank) )
               stop
            end if
         end if
      end do
      write(*,'(i0)') m
   end if
end if
end program main


program main
   implicit none
   character(100000)s
   integer n,k
   integer ::i,ma=1,counter=0,j=1
   integer ::a(100000)=0
   read(*,*)n,k
   read(*,*)s
   do i=1,n
      if(mod(j,2)==1)then
        if(s(i:i)=="1")then
          a(j)=a(j)+1
        else
          j=j+1
        endif
        cycle
      else
        if(s(i:i)=="0")then
          a(j)=a(j)+1
        else
          j=j+1
        endif
      endif
      cycle
   enddo
   a(2:j)=a(2:j)+1
   if(nint((j+0.001)/2)<k)then
      write(*,*)n
   endif
   do i=1,n-2*k,2
      if(ma<sum(a(i:i+k*2)))ma=sum(a(i:i+k*2))
   enddo
   write(*,'(i0)')ma
end program main


program sample
    implicit none

    integer(8) :: i,j,m,n,a,b,c,d,e,k

    integer(8),allocatable :: x(:),y(:)

    read(*,*) a
    read(*,*) b
    read(*,*) c
    read(*,*) d
    read(*,*) e
    read(*,*) k
    if (e-a>k)then
        write(*,*) ':('
    else
        write(*,*) 'Yey!'
    end if

    stop
end program sample

Score: 100 points

Problem Statement

In AtCoder city, there are five antennas standing in a straight line. They are called Antenna A, B, C, D and E from west to east, and their coordinates are a, b, c, d and e, respectively.

Two antennas can communicate directly if the distance between them is k or less, and they cannot if the distance is greater than k.

Determine if there exists a pair of antennas that cannot communicate directly.

Here, assume that the distance between two antennas at coordinates p and q (p < q) is q - p.

Constraints

a, b, c, d, e and k are integers between 0 and 123 (inclusive).

a < b < c < d < e

Input

Input is given from Standard Input in the following format:

a
b
c
d
e
k

Output

Print :( if there exists a pair of antennas that cannot communicate directly, and print Yay! if there is no such pair.

Sample Input 1

1
2
4
8
9
15

Sample Output 1

Yay!

In this case, there is no pair of antennas that cannot communicate directly, because:

the distance between A and B is 2 - 1 = 1

the distance between A and C is 4 - 1 = 3

the distance between A and D is 8 - 1 = 7

the distance between A and E is 9 - 1 = 8

the distance between B and C is 4 - 2 = 2

the distance between B and D is 8 - 2 = 6

the distance between B and E is 9 - 2 = 7

the distance between C and D is 8 - 4 = 4

the distance between C and E is 9 - 4 = 5

the distance between D and E is 9 - 8 = 1

and none of them is greater than 15. Thus, the correct output is Yay!.

Sample Input 2

15
18
26
35
36
18

Sample Output 2

:(

In this case, the distance between antennas A and D is 35 - 15 = 20 and exceeds 18, so they cannot communicate directly.
Thus, the correct output is :(.


program abc
    integer,dimension(5) :: anthennas
    integer k

    do i=1,5
        read *, anthennas(i)
    end do
    read *, k

    if ((maxval(anthennas)-minval(anthennas)) > k) then
        print '(A)', ':('
    else
        print '(A)', 'Yay!'
    end if
end program abc


PROGRAM ATCODER

implicit none
integer :: a(5),k,i,j

do i = 1,5
  read*, a(i)
end do

read*, k

do i = 1, 5, 1
  do j = 1, 5, 1
    if ( i == j ) cycle
    if ( abs(a(i)-a(j)) > k ) then
      print'(a)', ':('
      stop
    end if
  end do
end do

print'(a)', 'Yay!'

END PROGRAM ATCODER


program fivedishes
  implicit none

integer::a(5),i,count=0,m=650

do i=1,5
   read*,a(i)
   if(mod(a(i),10)==0)then
      count=count+a(i)
   else
      count=count+(a(i)/10+1)*10
   end if
end do

do i=1,5
   if(mod(a(i),10)/=0)then
      if(count-(a(i)/10+1)*10+a(i)<m)then
         m=count-(a(i)/10+1)*10+a(i)
      end if
   end if
end do

 print*,m

end program fivedishes

Score: 200 points

Problem Statement

The restaurant AtCoder serves the following five dishes:

ABC Don (rice bowl): takes A minutes to serve.

ARC Curry: takes B minutes to serve.

AGC Pasta: takes C minutes to serve.

APC Ramen: takes D minutes to serve.

ATC Hanbagu (hamburger patty): takes E minutes to serve.

Here, the time to serve a dish is the time between when an order is placed and when the dish is delivered.

This restaurant has the following rules on orders:

An order can only be placed at a time that is a multiple of 10 (time 0, 10, 20, ...).

Only one dish can be ordered at a time.

No new order can be placed when an order is already placed and the dish is still not delivered, but a new order can be placed at the exact time when the dish is delivered.

E869120 arrives at this restaurant at time 0. He will order all five dishes. Find the earliest possible time for the last dish to be delivered.

Here, he can order the dishes in any order he likes, and he can place an order already at time 0.

Constraints

A, B, C, D and E are integers between 1 and 123 (inclusive).

Input

Input is given from Standard Input in the following format:

A
B
C
D
E

Output

Print the earliest possible time for the last dish to be delivered, as an integer.

Sample Input 1

29
20
7
35
120

Sample Output 1

215

If we decide to order the dishes in the order ABC Don, ARC Curry, AGC Pasta, ATC Hanbagu, APC Ramen, the earliest possible time for each order is as follows:

Order ABC Don at time 0, which will be delivered at time 29.

Order ARC Curry at time 30, which will be delivered at time 50.

Order AGC Pasta at time 50, which will be delivered at time 57.

Order ATC Hanbagu at time 60, which will be delivered at time 180.

Order APC Ramen at time 180, which will be delivered at time 215.

There is no way to order the dishes in which the last dish will be delivered earlier than this.

Sample Input 2

101
86
119
108
57

Sample Output 2

481

If we decide to order the dishes in the order AGC Pasta, ARC Curry, ATC Hanbagu, APC Ramen, ABC Don, the earliest possible time for each order is as follows:

Order AGC Pasta at time 0, which will be delivered at time 119.

Order ARC Curry at time 120, which will be delivered at time 206.

Order ATC Hanbagu at time 210, which will be delivered at time 267.

Order APC Ramen at time 270, which will be delivered at time 378.

Order ABC Don at time 380, which will be delivered at time 481.

There is no way to order the dishes in which the last dish will be delivered earlier than this.

Sample Input 3

123
123
123
123
123

Sample Output 3

643

This is the largest valid case.


program main
  implicit none
  integer a, b, c, d, e
  read(*, *) a
  read(*, *) b
  read(*, *) c
  read(*, *) d
  read(*, *) e
  a = a/10
  a = 10*(a + 1)
  b = b/10
  b = 10*(b + 1)
  c = c/10
  c = 10*(c + 1)
  d = d/10
  d = 10*(d + 1)
  e = e/10
  e = 10*(e + 1)
  write(*, '(i0)') a+b+c+d+e
end program main


program five_transportations
  implicit none
  integer(8) :: n, a(5) = 0, m, i
  read(*,*) n
  do i = 1, 5
    read(*,*) a(i)
  end do
  m = minval(a)
  write(*,'(i0)') (n+m-1)/m+4
end program five_transportations

Score: 300 points

Problem Statement

In 2028 and after a continuous growth, AtCoder Inc. finally built an empire with six cities (City 1, 2, 3, 4, 5, 6)!

There are five means of transport in this empire:

Train: travels from City 1 to 2 in one minute. A train can occupy at most A people.

Bus: travels from City 2 to 3 in one minute. A bus can occupy at most B people.

Taxi: travels from City 3 to 4 in one minute. A taxi can occupy at most C people.

Airplane: travels from City 4 to 5 in one minute. An airplane can occupy at most D people.

Ship: travels from City 5 to 6 in one minute. A ship can occupy at most E people.

For each of them, one vehicle leaves the city at each integer time (time 0, 1, 2, ...).

There is a group of N people at City 1, and they all want to go to City 6.

At least how long does it take for all of them to reach there?
You can ignore the time needed to transfer.

Constraints

1 \leq N, A, B, C, D, E \leq 10^{15}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A
B
C
D
E

Output

Print the minimum time required for all of the people to reach City 6, in minutes.

Sample Input 1

5
3
2
4
3
5

Sample Output 1

7

One possible way to travel is as follows.
First, there are N = 5 people at City 1, as shown in the following image:

In the first minute, three people travels from City 1 to City 2 by train. Note that a train can only occupy at most three people.

In the second minute, the remaining two people travels from City 1 to City 2 by train, and two of the three people who were already at City 2 travels to City 3 by bus. Note that a bus can only occupy at most two people.

In the third minute, two people travels from City 2 to City 3 by train, and another two people travels from City 3 to City 4 by taxi.

From then on, if they continue traveling without stopping until they reach City 6, all of them can reach there in seven minutes.

There is no way for them to reach City 6 in 6 minutes or less.

Sample Input 2

10
123
123
123
123
123

Sample Output 2

5

All kinds of vehicles can occupy N = 10 people at a time.
Thus, if they continue traveling without stopping until they reach City 6, all of them can reach there in five minutes.

Sample Input 3

10000000007
2
3
5
7
11

Sample Output 3

5000000008

Note that the input or output may not fit into a 32-bit integer type.


program main
  integer(8) :: n, i, a(5)
  read (*, *) n
  read (*, *) (a(i), i = 1, 5)
  write (*, "(i0)") ceiling(dble(n) / minval(a), kind(n)) + 4
end program main


integer(16) N
integer(16) A,B,C,D,E
integer(16) t
integer(16) town(1:5)
read*,N,A,B,C,D,E
if(A==min(A,B,C,D,E))then
  t=5+(N-1)/A
  else
    if(B==min(B,C,D,E))then
      t=5+(N-1)/B
      else
      if(C==min(C,D,E))then
        t=5+(N-1)/C
        else
      if(D==min(D,E))then
        t=5+(N-1)/D
        else
        t=5+(N-1)/E
      end if
    end if
  endif
endif
print"(I0)",t
end


program main
  implicit none
  integer(8) n, time(5), nekku
  read(*, *) n
  read(*, *) time(1)
  read(*, *) time(2)
  read(*, *) time(3)
  read(*, *) time(4)
  read(*, *) time(5)
  nekku = min(time(1),time(2),time(3),time(4),time(5))
  write(*, '(i0)') (N/nekku)+5
end program main


program main
  implicit none
  integer(8) :: N
  integer(8) :: A(5)
  integer(8) :: Amin
  integer(8) :: out
  integer :: mind(1)
  integer :: i

  read(*,*) N
  do i = 1,5
     read(*,*) A(i)
  end do
  Amin = minval( A )
  out = N/Amin +5
  write(*,'(i0)') out
  stop
end program main


integer(16) X,Y,Z,K,t
integer(16),allocatable,dimension(:)::xi,yi,zi
integer(16),allocatable,dimension(:)::xy,xyk
integer(16),allocatable,dimension(:)::xyz
read*,X,Y,Z,K
allocate(xi(x),yi(y),zi(z))
read*,xi
read*,yi
read*,zi
allocate(xy(x*y))
allocate(xyk(k))
do i=1,x
  do j=1,y
    xy(x*(j-1)+i)=xi(i)+yi(j)
  end do
end do

call heapsort(x*y,xy)

if(x*y>K)then
  do i=1,K
    xyk(i)=xy(x*y-i+1)
  end do
  t=k*z
  allocate(xyz(t))
  do i=1,K
    do j=1,z
      xyz((j-1)*k+i)=xyk(i)+zi(j)
    end do
  end do

  call heapsort(k*z,xyz)
  do i=1,k
    print"(i0)",xyz(k*z-i+1)
  end do
else
  t=x*y*z
  allocate(xyz(t))
  do i=1,x*y
    do j=1,z
      xyz((j-1)*x*y+i)=xy(i)+zi(j)
    end do
  end do

  call heapsort(x*y*z,xyz)

  do i=1,k
  print"(i0)",xyz(x*y*z-i+1)
  end do
endif

contains

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

  if(n.eq.1)return

  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.eq.1) then
           array(1)=t
           exit
        endif
     endif
     i=l
     j=l+l
     do while(j.le.k)
        if(j.lt.k)then
           if(array(j).lt.array(j+1))j=j+1
        endif
        if (t.lt.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

Score: 400 points

Problem Statement

The Patisserie AtCoder sells cakes with number-shaped candles.
There are X, Y and Z kinds of cakes with 1-shaped, 2-shaped and 3-shaped candles, respectively.
Each cake has an integer value called deliciousness, as follows:

The deliciousness of the cakes with 1-shaped candles are A_1, A_2, ..., A_X.

The deliciousness of the cakes with 2-shaped candles are B_1, B_2, ..., B_Y.

The deliciousness of the cakes with 3-shaped candles are C_1, C_2, ..., C_Z.

Takahashi decides to buy three cakes, one for each of the three shapes of the candles, to celebrate ABC 123.

There are X \times Y \times Z such ways to choose three cakes.

We will arrange these X \times Y \times Z ways in descending order of the sum of the deliciousness of the cakes.

Print the sums of the deliciousness of the cakes for the first, second, ..., K-th ways in this list.

Constraints

1 \leq X \leq 1 \ 000

1 \leq Y \leq 1 \ 000

1 \leq Z \leq 1 \ 000

1 \leq K \leq \min(3 \ 000, X \times Y \times Z)

1 \leq A_i \leq 10 \ 000 \ 000 \ 000

1 \leq B_i \leq 10 \ 000 \ 000 \ 000

1 \leq C_i \leq 10 \ 000 \ 000 \ 000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y Z K
A_1 \ A_2 \ A_3 \ ... \ A_X
B_1 \ B_2 \ B_3 \ ... \ B_Y
C_1 \ C_2 \ C_3 \ ... \ C_Z

Output

Print K lines. The i-th line should contain the i-th value stated in the problem statement.

Sample Input 1

2 2 2 8
4 6
1 5
3 8

Sample Output 1

19
17
15
14
13
12
10
8

There are 2 \times 2 \times 2 = 8 ways to choose three cakes, as shown below in descending order of the sum of the deliciousness of the cakes:

(A_2, B_2, C_2): 6 + 5 + 8 = 19

(A_1, B_2, C_2): 4 + 5 + 8 = 17

(A_2, B_1, C_2): 6 + 1 + 8 = 15

(A_2, B_2, C_1): 6 + 5 + 3 = 14

(A_1, B_1, C_2): 4 + 1 + 8 = 13

(A_1, B_2, C_1): 4 + 5 + 3 = 12

(A_2, B_1, C_1): 6 + 1 + 3 = 10

(A_1, B_1, C_1): 4 + 1 + 3 = 8

Sample Input 2

3 3 3 5
1 10 100
2 20 200
1 10 100

Sample Output 2

400
310
310
301
301

There may be multiple combinations of cakes with the same sum of the deliciousness. For example, in this test case, the sum of A_1, B_3, C_3 and the sum of A_3, B_3, C_1 are both 301.
However, they are different ways of choosing cakes, so 301 occurs twice in the output.

Sample Input 3

10 10 10 20
7467038376 5724769290 292794712 2843504496 3381970101 8402252870 249131806 6310293640 6690322794 6082257488
1873977926 2576529623 1144842195 1379118507 6003234687 4925540914 3902539811 3326692703 484657758 2877436338
4975681328 8974383988 2882263257 7690203955 514305523 6679823484 4263279310 585966808 3752282379 620585736

Sample Output 3

23379871545
22444657051
22302177772
22095691512
21667941469
21366963278
21287912315
21279176669
21160477018
21085311041
21059876163
21017997739
20703329561
20702387965
20590247696
20383761436
20343962175
20254073196
20210218542
20150096547

Note that the input or output may not fit into a 32-bit integer type.


program tri
  implicit none
  integer :: a,b,c

  read(*,*) a,b,c
  if((a.eq.b).and.(b.eq.c)) then
     write(*,'(a)') 'Yes'
  else
     write(*,'(a)') 'No'
  endif

end program tri

Score : 100 points

Problem Statement

You are given three integers A, B and C.

Determine if there exists an equilateral triangle whose sides have lengths A, B and C.

Constraints

All values in input are integers.

1 \leq A,B,C \leq 100

Input

Input is given from Standard Input in the following format:

A B C

Output

If there exists an equilateral triangle whose sides have lengths A, B and C, print Yes; otherwise, print No.

Sample Input 1

2 2 2

Sample Output 1

Yes

There exists an equilateral triangle whose sides have lengths 2, 2 and 2.

Sample Input 2

3 4 5

Sample Output 2

No

There is no equilateral triangle whose sides have lengths 3, 4 and 5.


PROGRAM ATCODER

implicit none
integer :: n,q,i,j
character(len=2*10**5) :: s
character(len=1),allocatable :: d(:),t(:)

read*, n,q,s
allocate(t(q),d(q))
do i = 1, q, 1
  read*, t(i),d(i)
end do

print'(i0)', bin_s(1)-bin_s(0)

contains
!!!!!! サブルーチンは↓
logical function isOK(index,key) !indexが条件を満たすかどうか
  implicit none
  integer,intent(in) :: index,key
  if ( check(index) >= key ) then !a(index)がkey以上になる最小のindex
    isOK = .true.
  else
    isOK = .false.
  end if
end function isOK

integer function bin_s(key)
  implicit none
  integer,intent(in) :: key
  integer :: ng,ok,mid
  ng = 0 ; ok = len_trim(s)+1
  do while ( abs(ok-ng) > 1 )
    mid = (ok+ng)/2
    if ( isOK(mid,key) ) then
      ok = mid
    else
      ng = mid
    end if
  end do
  bin_s = ok
end function bin_s

!とある地点(x=start)のゴーレムが落ちるか否か
integer function check(start)
  implicit none
  integer,intent(in) :: start
  integer :: i,x
  x = start
  do i = 1, q, 1
    if ( s(x:x) == t(i) ) then
      if ( d(i) == 'L' ) then
        x = x - 1
      else
        x = x + 1
      end if
    end if
    if ( x == 0 ) then
      check = -1
      return
    else if ( x == n+1 ) then
      check = +1
      return
    end if
  end do
  check = 0
end function

END PROGRAM ATCODER

Score : 500 points

Problem Statement

There are N squares numbered 1 to N from left to right.
Each square has a character written on it, and Square i has a letter s_i. Besides, there is initially one golem on each square.

Snuke cast Q spells to move the golems.

The i-th spell consisted of two characters t_i and d_i, where d_i is L or R.
When Snuke cast this spell, for each square with the character t_i, all golems on that square moved to the square adjacent to the left if d_i is L, and moved to the square adjacent to the right if d_i is R.

However, when a golem tried to move left from Square 1 or move right from Square N, it disappeared.

Find the number of golems remaining after Snuke cast the Q spells.

Constraints

1 \leq N,Q \leq 2 \times 10^{5}

|s| = N

s_i and t_i are uppercase English letters.

d_i is L or R.

Input

Input is given from Standard Input in the following format:

N Q
s
t_1 d_1
\vdots
t_{Q} d_Q

Output

Print the answer.

Sample Input 1

3 4
ABC
A L
B L
B R
A R

Sample Output 1

2

Initially, there is one golem on each square.

In the first spell, the golem on Square 1 tries to move left and disappears.

In the second spell, the golem on Square 2 moves left.

In the third spell, no golem moves.

In the fourth spell, the golem on Square 1 moves right.

After the four spells are cast, there is one golem on Square 2 and one golem on Square 3, for a total of two golems remaining.

Sample Input 2

8 3
AABCBDBA
A L
B R
A R

Sample Output 2

5

After the three spells are cast, there is one golem on Square 2, two golems on Square 4 and two golems on Square 6, for a total of five golems remaining.

Note that a single spell may move multiple golems.

Sample Input 3

10 15
SNCZWRCEWB
B R
R R
E R
W R
Z L
S R
Q L
W L
B R
C L
A L
N L
E R
Z L
S L

Sample Output 3

3


program prob1
    implicit none
    character :: s
    read(*,*) s
    if(s == 'A') then
        write(*,*) 'T'
    else if(s == 'T') then
        write(*,*) 'A'
    else if(s == 'G') then
        write(*,*) 'C'
    else if(s == 'C') then
        write(*,*) 'G'
    end if
    stop
contains
end program prob1

Score : 100 points

Problem Statement

On the Planet AtCoder, there are four types of bases: A, C, G and T. A bonds with T, and C bonds with G.

You are given a letter b as input, which is A, C, G or T. Write a program that prints the letter representing the base that bonds with the base b.

Constraints

b is one of the letters A, C, G and T.

Input

Input is given from Standard Input in the following format:

b

Output

Print the letter representing the base that bonds with the base b.

Sample Input 1

A

Sample Output 1

T

Sample Input 2

G

Sample Output 2

C


program main
implicit None
	character(len = 100000)::s
	integer(8)::n,q,i
	integer(8),allocatable::l(:),r(:),a(:)

	read*,n,q
	read*,s

	allocate(l(q),r(q),a(n))

	a =0
	do i = 2,n
		if(s(i:i) == "C")then
			if(s(i-1:i-1) == "A")then
				a(i) = 1
			endif
		endif
	enddo

	do i = 2,n
		a(i) = a(i-1)+a(i)
	enddo

	do i = 1,q
		read*,l(i),r(i)
	enddo

	do i = 1,q
		print"(i0)",a(r(i))-a(l(i))
	enddo
end program main

Score : 300 points

Problem Statement

You are given a string S of length N consisting of A, C, G and T. Answer the following Q queries:

Query i (1 \leq i \leq Q): You will be given integers l_i and r_i (1 \leq l_i < r_i \leq N). Consider the substring of S starting at index l_i and ending at index r_i (both inclusive). In this string, how many times does AC occurs as a substring?

Notes

A substring of a string T is a string obtained by removing zero or more characters from the beginning and the end of T.

For example, the substrings of ATCODER include TCO, AT, CODER, ATCODER and (the empty string), but not AC.

Constraints

2 \leq N \leq 10^5

1 \leq Q \leq 10^5

S is a string of length N.

Each character in S is A, C, G or T.

1 \leq l_i < r_i \leq N

Input

Input is given from Standard Input in the following format:

N Q
S
l_1 r_1
:
l_Q r_Q

Output

Print Q lines. The i-th line should contain the answer to the i-th query.

Sample Input 1

8 3
ACACTACG
3 7
2 3
1 8

Sample Output 1

2
0
3

Query 1: the substring of S starting at index 3 and ending at index 7 is ACTAC. In this string, AC occurs twice as a substring.

Query 2: the substring of S starting at index 2 and ending at index 3 is CA. In this string, AC occurs zero times as a substring.

Query 3: the substring of S starting at index 1 and ending at index 8 is ACACTACG. In this string, AC occurs three times as a substring.


program main
  implicit none
  character(len=100000) :: s
  integer :: n, q, i, j, k, l, r
  integer, allocatable :: a(:), ans(:)

  read *, n, q
  read *, s

  allocate (a(n), ans(q))
  ans(:) = 0
  a(:) = 0

  do i = 1, len_trim(s) - 1
    if (s(i:i+1) == 'AC') then
      a(i+1:) = a(i+1:) + 1
    end if
  end do

  do k = 1, q
    read *, l, r
    ans(k) = a(r) - a(l)
  end do

  do k = 1, q
    print '(i0)', ans(k)
  end do
  print *, a

end


PROGRAM ATCODER

implicit none
integer(16) :: n,q,i,j,count_s=0,l,r
character(len=10**5) :: s,b
integer,allocatable :: c(:)

read(*,*) n,q,s

allocate(c(n))

do i = 1, n-1
  if ( s(i:i) == 'A' .and. s(i+1:i+1) == 'C') then
    s(i:i) = 'a'
  end if
end do

c = 0
do i = 2,n
  if ( s(i-1:i-1) == 'a' ) then
    c(i) = c(i-1) + 1
  else
    c(i) = c(i-1)
  end if
end do

do i = 1, q, 1
  read(*,*) l, r
  write(*,'(i0)') c(r)-c(l)
end do

contains
!!!!!! サブルーチンは↓
!!!!!!

END PROGRAM ATCODER


program main
  implicit none
  character(len=100000) :: s
  integer :: n, q, i, j, k, l, r
  integer, allocatable :: ans(:)

  read *, n, q
  read *, s

  allocate (ans(q))
  ans(:) = 0

  do k = 1, q
    read *, l, r
    i = 1
    do i = l, r-1
      if (index(s(i:r),'AC') .eq. 0) then
        exit
      else if (s(i:i+1) .eq. 'AC') then
        ans(k) = ans(k) + 1
      end if
    end do
  end do
  do k = 1, q
    print '(i0)', ans(k)
  end do

end


program main
  implicit none
  character(len=100000) :: S
  character,allocatable:: S2(:)
  integer :: N,Q
  integer :: N2
  integer,allocatable :: l(:),r(:)
  integer :: i,ii
  integer,allocatable :: out(:),out2(:)
  read(*,*) N,Q
  read(*,*) S(1:N)
  allocate(l(Q),r(Q))
  do i=1,Q
     read(*,*) l(i),r(i)
  end do
  allocate(out(N))
  allocate(out2(N))
  ! set datas
  out=0
  do i = 1,N-1
     if ( S( i:i+1 ) == 'AC' ) then
        out(1:i)=out(1:i)+1
     end if
  end do
  out2=-out
!@  do i = N-1,1,-1
!@     if ( S( i:i+1 ) == 'AC' ) then
!@        out2(1:i) = out2(1:i)-1
!@     end if
!@  end do
  ! output
  do i = 1,Q
     write(*,'(i0)')  out( l(i) )+out2( r(i) )
  end do
end program main


program main
  implicit none
  character(len=100000) :: S
  character,allocatable:: S2(:)
  integer :: N,Q
  integer :: N2
  integer :: l,r
  integer :: i,ii
  integer :: out
  read(*,*) N,Q
  read(*,*) S(1:N)
  do i=1,Q
     read(*,*) l,r
     N2=r-l+1
     out=0
     do ii = 1,N2-1
        if ( S( l+ii-1:l+ii ) == 'AC' ) then
           out=out+1
        end if
     end do
     write(*,'(i0)') out
  end do

end program main


PROGRAM ATCODER

implicit none
integer :: n,q,i,j,count,l,r,t,u
character(len=10**5) :: s,b

read(*,*) n,q,s
do i = 1, q, 1
  count = 0
  read(*,*) l, r
  b = s(l:r)
  u = 1
  do
    t = index(b(u:),'AC')
    If (t==0) exit
    u = u + t
    count = count + 1
  end do
  write(*,'(i0)') count
end do

contains
!!!!!! サブルーチンは↓
!!!!!!

END PROGRAM ATCODER

Score : 400 points

Problem Statement

You are given an integer N. Find the number of strings of length N that satisfy the following conditions, modulo 10^9+7:

The string does not contain characters other than A, C, G and T.

The string does not contain AGC as a substring.

The condition above cannot be violated by swapping two adjacent characters once.

Notes

A substring of a string T is a string obtained by removing zero or more characters from the beginning and the end of T.

For example, the substrings of ATCODER include TCO, AT, CODER, ATCODER and (the empty string), but not AC.

Constraints

3 \leq N \leq 100

Input

Input is given from Standard Input in the following format:

N

Output

Print the number of strings of length N that satisfy the following conditions, modulo 10^9+7.

Sample Input 1

3

Sample Output 1

61

There are 4^3 = 64 strings of length 3 that do not contain characters other than A, C, G and T. Among them, only AGC, ACG and GAC violate the condition, so the answer is 64 - 3 = 61.

Sample Input 2

4

Sample Output 2

230

Sample Input 3

100

Sample Output 3

388130742

Be sure to print the number of strings modulo 10^9+7.


module mod_graph
  implicit none
  integer(8), parameter :: inf = 1000000000000000000_8

  type item
    integer :: id = 0
    integer(8) :: wt = 0_8
  end type

  type priorityqueue
    integer :: num = 0
    type(item), pointer :: heap(:) => null()
  contains
    !final :: finalizei
  end type

  type edge
    integer :: fm = 0, to = 0, cp = 0, rv = 0
    integer(8) :: wt = 0_8
  end type

  type arraylist
    integer :: num = 0
    type(edge), pointer :: arr(:) => null()
  contains
    procedure :: add => adde
    procedure :: bellman_ford => bellman_ford
    !final :: finalizee
  end type

  type graph
    type(arraylist), pointer :: egs(:) => null()
    logical, pointer :: used(:) => null()
  contains
    procedure :: add => add
    procedure :: dijkstra => dijkstra
    procedure :: ford_fulkerson => ford_fulkerson
    !final :: finalize
  end type

  interface graph
    module procedure :: newg
  end interface graph

contains

  function newi(id,wt) result(ret)
    integer, intent(in) :: id
    integer(8), intent(in) :: wt
    type(item) :: ret
    ret%id = id
    ret%wt = wt
  end

  subroutine swapi(a,b)
    type(item), intent(inout) :: a, b
    type(item) :: c
    c = a
    a = b
    b = c
  end

  subroutine appendi(a)
    type(item), pointer, intent(inout) :: a(:)
    integer :: n
    type(item), allocatable :: tmp(:)
    n = size(a)
    allocate(tmp(n))
    tmp = a
    deallocate(a)
    allocate(a(2*n))
    a(1:n) = tmp
    deallocate(tmp)
  end

  function lessi(a,b) result(ret)
    type(item), intent(in) :: a, b
    logical :: ret
    ret = a%wt < b%wt
  end

  subroutine finalizei(pq)
    type(priorityqueue), intent(inout) :: pq
    if (associated(pq%heap)) deallocate(pq%heap)
    pq%num = 0
  end

  subroutine offer(pq,it)
    type(priorityqueue), intent(inout) :: pq
    type(item), intent(in) :: it
    integer :: i, j
    if (.not.associated(pq%heap)) allocate(pq%heap(1))
    if (pq%num == size(pq%heap)) call appendi(pq%heap)
    pq%num = pq%num+1
    pq%heap(pq%num) = it
    i = pq%num
    do while (i > 1)
      j = i/2
      if (lessi(pq%heap(i),pq%heap(j))) call swapi(pq%heap(i),pq%heap(j))
      i = j
    end do
  end

  function poll(pq) result(ret)
    type(priorityqueue), intent(inout) :: pq
    type(item) :: ret
    integer :: n, i, j
    n = pq%num
    ret = pq%heap(1)
    pq%heap(1) = pq%heap(n)
    pq%num = pq%num-1
    i = 1
    do while (2*i < n)
      j = 2*i
      if (j+1 < n .and. lessi(pq%heap(j+1),pq%heap(j))) j = j+1
      if (lessi(pq%heap(j),pq%heap(i))) call swapi(pq%heap(j),pq%heap(i))
      i = j
    end do
  end

  function newe(to,wt,fm,cp,rv) result(ret)
    integer, intent(in) :: to
    integer(8), intent(in) :: wt
    integer, intent(in), optional :: fm, cp, rv
    type(edge) :: ret
    if (present(fm)) ret%fm = fm
    ret%to = to
    ret%wt = wt
    if (present(cp) .and. present(rv)) then
      ret%cp = cp
      ret%rv = rv
    end if
  end

  subroutine appende(a)
    type(edge), pointer, intent(inout) :: a(:)
    integer :: n
    type(edge), allocatable :: tmp(:)
    n = size(a)
    allocate(tmp(n))
    tmp = a
    deallocate(a)
    allocate(a(2*n))
    a(1:n) = tmp
    deallocate(tmp)
  end

  function lesse(a,b) result(ret)
    type(edge), intent(in) :: a, b
    logical :: ret
    ret = a%wt < b%wt
  end

  subroutine finalizee(list)
    type(arraylist), intent(inout) :: list
    if (associated(list%arr)) deallocate(list%arr)
    list%num = 0
  end

  subroutine adde(list,e)
    class(arraylist), intent(inout) :: list
    type(edge), intent(in) :: e
    if (.not.associated(list%arr)) allocate(list%arr(1))
    if (list%num == size(list%arr)) call appende(list%arr)
    list%num = list%num+1
    list%arr(list%num) = e
  end

  function newg(n) result(ret)
    integer, intent(in) :: n
    type(graph) :: ret
    allocate(ret%egs(n))
  end

  subroutine add(g,fm,to,wt,cp)
    class(graph), intent(inout) :: g
    integer, intent(in) :: fm, to
    integer, intent(in), optional :: cp
    integer(8) :: wt
    if (present(cp)) then
      call adde(g%egs(fm),newe(to,wt,cp=cp,rv=g%egs(to)%num))
      call adde(g%egs(to),newe(fm,wt,cp=0,rv=g%egs(fm)%num-1))
    else
      call adde(g%egs(fm),newe(to,wt))
    end if
  end

  subroutine finalize(g)
    type(graph), intent(inout) :: g
    integer :: i
    if (associated(g%used)) deallocate(g%used)
    if (.not.associated(g%egs)) return
    do i = 1, size(g%egs)
      call finalizee(g%egs(i))
    end do
    deallocate(g%egs)
  end

  function dijkstra(g,s) result(ret)
    class(graph), intent(in) :: g
    integer, intent(in) :: s
    integer(8) :: ret(size(g%egs))
    type(priorityqueue) :: pq
    type(item) :: it
    type(edge) :: e
    integer :: i
    ret = inf
    ret(s) = 0_8
    call offer(pq,newi(s,ret(s)))
    do while (pq%num > 0)
      it = poll(pq)
      if (it%wt > ret(it%id)) cycle
      do i = 1, g%egs(it%id)%num
        e = g%egs(it%id)%arr(i)
        if (ret(e%to) > ret(it%id)+e%wt) then
          ret(e%to) = ret(it%id)+e%wt
          call offer(pq,newi(e%to,ret(e%to)))
        end if
      end do
    end do
    call finalizei(pq)
  end

  recursive subroutine dfs_bf(g,u)
    type(graph), intent(inout) :: g
    integer, intent(in) :: u
    integer :: i, v
    g%used(u) = .true.
    do i = 1, g%egs(u)%num
      v = g%egs(u)%arr(i)%to
      if (g%used(v)) cycle
      call dfs_bf(g,v)
    end do
  end

  function bellman_ford(es,s,t) result(ret)
    class(arraylist), intent(in) :: es
    integer, intent(in) :: s, t
    integer(8) :: ret
    integer :: n, i, fm, to, step
    integer(8) :: wt
    logical :: updated
    integer(8), allocatable :: tmp(:)
    type(graph) :: gs, gt
    type(arraylist) :: reach
    n = 0
    do i = 1, es%num
      n = max(n,es%arr(i)%fm,es%arr(i)%to)
    end do
    gs = newg(n)
    gt = newg(n)
    allocate(gs%used(n),gt%used(n))
    gs%used = .false.
    gt%used = .false.
    do i = 1, es%num
      fm = es%arr(i)%fm
      to = es%arr(i)%to
      wt = es%arr(i)%wt
      call add(gs,fm,to,wt)
      call add(gt,to,fm,wt)
    end do
    call dfs_bf(gs,s)
    call dfs_bf(gt,t)
    n = 0
    do i = 1, es%num
      fm = es%arr(i)%fm
      to = es%arr(i)%to
      if (gs%used(fm) .and. gt%used(fm) .and. gs%used(to) .and. gt%used(to)) then
        call adde(reach,es%arr(i))
        n = max(n,fm,to)
      end if
    end do
    call finalize(gs)
    call finalize(gt)
    allocate(tmp(n))
    tmp = inf
    tmp(s) = 0_8
    step = 0
    updated = .true.
    ret = inf
    do while (updated)
      updated = .false.
      do i = 1, reach%num
        fm = reach%arr(i)%fm
        to = reach%arr(i)%to
        wt = reach%arr(i)%wt
        if (tmp(to) > tmp(fm)+wt) then
          tmp(to) = tmp(fm)+wt
          updated = .true.
        end if
      end do
      step = step+1
      if (step > n) then
        ret = -inf
        exit
      end if
    end do
    if (ret == inf) ret = tmp(t)
    deallocate(tmp)
    call finalizee(reach)
  end

  recursive function dfs_ff(g,u,t,f) result(ret)
    class(graph), intent(inout) :: g
    integer, intent(in) :: u, t, f
    integer :: ret
    integer :: i, v, cp, rv
    ret = f
    if (u == t) return
    g%used(u) = .true.
    do i = 1, g%egs(u)%num
      v = g%egs(u)%arr(i)%to
      cp = g%egs(u)%arr(i)%cp
      if (g%used(v) .or. cp <= 0) cycle
      ret = dfs_ff(g,v,t,min(f,cp))
      rv = g%egs(u)%arr(i)%rv
      if (ret > 0) then
        g%egs(u)%arr(i)%cp = g%egs(u)%arr(i)%cp-ret
        g%egs(v)%arr(rv)%cp = g%egs(v)%arr(rv)%cp+ret
        return
      end if
    end do
    ret = 0
  end

  function ford_fulkerson(g,s,t) result(ret)
    class(graph), intent(inout) :: g
    integer, intent(in) :: s, t
    integer :: ret
    integer :: f
    ret = 0
    if (associated(g%used) .and. size(g%used) /= size(g%egs)) deallocate(g%used)
    if (.not.associated(g%used)) allocate(g%used(size(g%egs)))
    do
      g%used = .false.
      f = dfs_ff(g,s,t,1000000000)
      if (f == 0) then
        deallocate(g%used)
        return
      end if
      ret = ret+f
    end do
  end

end module mod_graph

program three_circuits
  use mod_graph
  implicit none
  integer :: n, m, a, b, i, s = -1, t = -1, x
  type(graph) :: g
  read(*,*) n, m
  g = graph(n)
  do i = 1, m
    read(*,*) a, b
    call g%add(a,b,0_8,cp=1)
    call g%add(b,a,0_8,cp=1)
  end do
  do i = 1, n
    x = g%egs(i)%num/2
    if (mod(x,2) == 1) then
      write(*,'(a)') "No"
      stop
    end if
  end do
  do i = 1, n
    x = g%egs(i)%num/2
    if (x >= 6) then
      write(*,'(a)') "Yes"
      stop
    end if
    if (x == 4) then
      if (s < 0) then
        s = i
        cycle
      end if
      if (t < 0) then
        t = i
        cycle
      end if
      write(*,'(a)') "Yes"
      stop
    end if
  end do
  if (t < 0) then
    write(*,'(a)') "No"
    stop
  end if
  if (g%ford_fulkerson(s,t) == 2) then
    write(*,'(a)') "Yes"
  else
    write(*,'(a)') "No"
  end if
  !call finalize(g)
end program three_circuits

Score : 800 points

Problem Statement

You are given a simple connected undirected graph consisting of N vertices and M edges.
The vertices are numbered 1 to N, and the edges are numbered 1 to M.

Edge i connects Vertex a_i and b_i bidirectionally.

Determine if three circuits (see Notes) can be formed using each of the edges exactly once.

Notes

A circuit is a cycle allowing repetitions of vertices but not edges.

Constraints

All values in input are integers.

1 \leq N,M \leq 10^{5}

1 \leq a_i, b_i \leq N

The given graph is simple and connected.

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
:
a_M b_M

Output

If three circuits can be formed using each of the edges exactly once, print Yes; if they cannot, print No.

Sample Input 1

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

Sample Output 1

Yes

Three circuits can be formed using each of the edges exactly once, as follows:

Sample Input 2

3 3
1 2
2 3
3 1

Sample Output 2

No

Three circuits are needed.

Sample Input 3

18 27
17 7
12 15
18 17
13 18
13 6
5 7
7 1
14 5
15 11
7 6
1 9
5 4
18 16
4 6
7 2
7 11
6 3
12 14
5 2
10 5
7 8
10 15
3 15
9 8
7 15
5 16
18 15

Sample Output 3

Yes


program answer
  implicit none
  integer(16) :: i, N, ans
  integer(16) :: count(26)
  character(len=100000) :: S

  read(*,*) N
  read(*,*) S
  count=0
  do i = 1, N
     count(ichar(S(i:i))-96)=count(ichar(S(i:i))-96)+1
  end do

  ans=1
  do i = 1, 26
     ans=ans*(count(i)+1)
     ans=mod(ans,10**9+7)
  end do
  ans=ans-1
  write(*,*) ans
  stop
 end program answer

Score : 200 points

Problem Statement

You are given a string S of length N.
Among its subsequences, count the ones such that all characters are different, modulo 10^9+7. Two subsequences are considered different if their characters come from different positions in the string, even if they are the same as strings.

Here, a subsequence of a string is a concatenation of one or more characters from the string without changing the order.

Constraints

1 \leq N \leq 100000

S consists of lowercase English letters.

|S|=N

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the number of the subsequences such that all characters are different, modulo 10^9+7.

Sample Input 1

4
abcd

Sample Output 1

15

Since all characters in S itself are different, all its subsequences satisfy the condition.

Sample Input 2

3
baa

Sample Output 2

5

The answer is five: b, two occurrences of a, two occurrences of ba. Note that we do not count baa, since it contains two as.

Sample Input 3

5
abcab

Sample Output 3

17


program main
  integer :: N
  integer,allocatable :: C(:)
  integer :: i,j
  integer :: count
  integer :: nsave
  integer(8) :: out
  integer(8) :: itmp
  read(*,*) N
  allocate(C(N))
  do i = 1,N
     read(*,*) C(i)
  end do
  itmp = 0
  out=counter(C,1,itmp)+1
  write(*,'(i0)') out
  deallocate(C)
  stop
contains
  recursive function counter(C,Ns,in) result(ires)
    integer,intent(in) ::  C(:)
    integer(8) :: ires
    integer :: nsave
    integer,intent(in) :: Ns
    integer(8) :: in
    integer :: i,j
    ires = in
    do j = Ns,N-1
       nsave = C(j)
       do i = j+1,N
          if (C(i) == nsave) then
             if (C(i) /= C(i-1)) then
                ires = ires + 1
             elseif(i < N-1) then
                ires = counter(C,i+1,ires)
             end if
          end if
       end do
    end do
  end function counter
end program main

Score : 700 points

Problem Statement

There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i.

Snuke will perform the following operation zero or more times:

Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones.

Find the number of possible final sequences of colors of the stones, modulo 10^9+7.

Constraints

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

1 \leq C_i \leq 2\times 10^5(1\leq i\leq N)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
C_1
:
C_N

Output

Print the number of possible final sequences of colors of the stones, modulo 10^9+7.

Sample Input 1

5
1
2
1
2
2

Sample Output 1

3

We can make three sequences of colors of stones, as follows:

(1,2,1,2,2), by doing nothing.

(1,1,1,2,2), by choosing the first and third stones to perform the operation.

(1,2,2,2,2), by choosing the second and fourth stones to perform the operation.

Sample Input 2

6
4
2
5
4
2
4

Sample Output 2

5

Sample Input 3

7
1
3
1
2
3
3
2

Sample Output 3

5


program answer
  implicit none

  integer :: H, W, ha, wa

  read(*,*) H, W
  read(*,*) ha, wa

  write(*,*) H*W-H*wa-ha*W+ha*wa

  stop
 end program answer

Score : 100 points

Problem Statement

There are H rows and W columns of white square cells.

You will choose h of the rows and w of the columns, and paint all of the cells contained in those rows or columns.

How many white cells will remain?

It can be proved that this count does not depend on what rows and columns are chosen.

Constraints

All values in input are integers.

1 \leq H, W \leq 20

1 \leq h \leq H

1 \leq w \leq W

Input

Input is given from Standard Input in the following format:

H W
h w

Output

Print the number of white cells that will remain.

Sample Input 1

3 2
2 1

Sample Output 1

1

There are 3 rows and 2 columns of cells. When two rows and one column are chosen and painted in black, there is always one white cell that remains.

Sample Input 2

5 5
2 3

Sample Output 2

6

Sample Input 3

2 4
2 4

Sample Output 3

0


integer :: height,width,h,w
read*,height,width
read*,h,w

print*,(height-h)*(width-w)
end


program b_121
    implicit none
    integer :: N,M,C
    integer :: A, B(20)
    integer :: i,j, ans = 0, count = 0
    read(*,*) N,M,C

    do i =1,M
        read(*,*) B(i)
    end do

    do i = 1,N
        do j = 1,M
            read(*,*) A
            ans = ans + A*B(i)
        end do
        if((ans+C) > 0) then
            count = count + 1
        end if
        ans = 0
    end do

    write(*,*) count

end program b_121

Score : 200 points

Problem Statement

There are N pieces of source code. The characteristics of the i-th code is represented by M integers A_{i1}, A_{i2}, ..., A_{iM}.

Additionally, you are given integers B_1, B_2, ..., B_M and C.

The i-th code correctly solves this problem if and only if A_{i1} B_1 + A_{i2} B_2 + ... + A_{iM} B_M + C > 0.

Among the N codes, find the number of codes that correctly solve this problem.

Constraints

All values in input are integers.

1 \leq N, M \leq 20

-100 \leq A_{ij} \leq 100

-100 \leq B_i \leq 100

-100 \leq C \leq 100

Input

Input is given from Standard Input in the following format:

N M C
B_1 B_2 ... B_M
A_{11} A_{12} ... A_{1M}
A_{21} A_{22} ... A_{2M}
\vdots
A_{N1} A_{N2} ... A_{NM}

Output

Print the number of codes among the given N codes that correctly solve this problem.

Sample Input 1

2 3 -10
1 2 3
3 2 1
1 2 2

Sample Output 1

1

Only the second code correctly solves this problem, as follows:

Since 3 \times 1 + 2 \times 2 + 1 \times 3 + (-10) = 0 \leq 0, the first code does not solve this problem.

1 \times 1 + 2 \times 2 + 2 \times 3 + (-10) = 1 > 0, the second code solves this problem.

Sample Input 2

5 2 -4
-2 5
100 41
100 40
-3 0
-6 -2
18 -13

Sample Output 2

2

Sample Input 3

3 3 0
100 -100 0
0 100 100
100 100 100
-100 100 100

Sample Output 3

0

All of them are Wrong Answer. Except yours.


program atcoder
implicit none
integer :: n,m,c,i,j,check
integer, allocatable :: a(:,:),b(:),ans(:)

read(*,*) n,m,c
allocate(a(n,m),b(m),ans(m))
ans = c

read(*,*) (b(i), i=1,m)

do i = 1,n
	read(*,*) (a(i,j), j=1,m)
end do

check = 0
do i = 1,n
	do j = 1,m
    	ans(i) = ans(i) + a(i,j)*b(j)
    end do
    if(ans(i)>0)then
    	check = check + 1
    end if
end do

write(*,'(i0)') check

end program atcoder