implicit none

integer :: h,w
character(2000) :: s
logical,allocatable :: map(:,:)
integer,allocatable :: n(:,:)
integer :: i,j,k

read *,h,w
allocate(map(h,w))
map(:,:) = .false.
do i = 1, h
   read *,s
   do j = 1, w
      if (s(j:j) == '#') then
         map(i,j) = .true.
      endif
   enddo
enddo

allocate(n(h,w))
n(:,:) = 0

do i = 1, h
   j = 1
   k = 0
   do while (j <= w)
      if (map(i,j)) then
         if (k > 0) then
            n(i,k:j-1) = j-k
         endif
         k = 0
      else
         if (k == 0) then
            k = j
         endif
      endif
      j = j + 1
   enddo
   if (k > 0) then
      n(i,k:w) = w - k + 1
   endif
enddo

do j = 1, w
   i = 1
   k = 0
   do while (i <= h)
      if (map(i,j)) then
         if (k > 0) then
            n(k:i-1,j) = n(k:i-1,j) + i-k
         endif
         k = 0
      else
         if (k == 0) then
            k = i
         endif
      endif
      i = i + 1
   enddo
   if (k > 0) then
      n(k:h,j) = n(k:h,j) + h - k + 1
   endif
enddo

n(:,:) = n(:,:) - 1

print '(i0)',maxval(n)

stop
end

Score : 400 points

Problem Statement

There is a grid with H horizontal rows and W vertical columns, and there are obstacles on some of the squares.

Snuke is going to choose one of the squares not occupied by an obstacle and place a lamp on it.
The lamp placed on the square will emit straight beams of light in four cardinal directions: up, down, left, and right.
In each direction, the beam will continue traveling until it hits a square occupied by an obstacle or it hits the border of the grid. It will light all the squares on the way, including the square on which the lamp is placed, but not the square occupied by an obstacle.

Snuke wants to maximize the number of squares lighted by the lamp.

You are given H strings S_i (1 \leq i \leq H), each of length W. If the j-th character (1 \leq j \leq W) of S_i is #, there is an obstacle on the square at the i-th row from the top and the j-th column from the left; if that character is ., there is no obstacle on that square.

Find the maximum possible number of squares lighted by the lamp.

Constraints

1 \leq H \leq 2,000

1 \leq W \leq 2,000

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

. occurs at least once in one of the strings S_i (1 \leq i \leq H).

Input

Input is given from Standard Input in the following format:

H W
S_1
:
S_H

Output

Print the maximum possible number of squares lighted by the lamp.

Sample Input 1

4 6
#..#..
.....#
....#.
#.#...

Sample Output 1

8

If Snuke places the lamp on the square at the second row from the top and the second column from the left, it will light the following squares: the first through fifth squares from the left in the second row, and the first through fourth squares from the top in the second column, for a total of eight squares.

Sample Input 2

8 8
..#...#.
....#...
##......
..###..#
...#..#.
##....#.
#...#...
###.#..#

Sample Output 2

13


program kenken
 implicit none
 integer n, a, b, c, d
 integer i, p, q, r, sum
 character(20000) s
 read*, n, a, b, c, d
 read*, s
 p=0
 q=0
 r=0
 sum=0

 if (c < d) then

 do i=b, d-1
  p=index(s(i:i),"#")
  q=index(s(i+1:i+1),"#")
   if ((p+q).eq.2) then
   print*, "No"
   return
   end if
 end do
  s(d:d)="#"
  do i=a, c-1
  p=index(s(i:i),"#")
  q=index(s(i+1:i+1),"#")
   if ((p+q).eq.2) then
   print*, "No"
   return
   end if
  end do
  print*, "Yes"
  return
 end if

 if (c > d) then

  do i=b,d-2
  p=index(s(i:i),".")
  q=index(s(i+1:i+1),".")
  r=index(s(i+2:i+2),".")
   if((p+q+r).eq.3) then
   sum = i+1
   p=0
   q=0
   r=0
   end if
  end do

  if(sum.ne.0) then
   s(sum:sum)="#"
    do i=a, c-1
    p=index(s(i:i),"#")
    q=index(s(i+1:i+1),"#")
     if ((p+q).eq.2) then
     print*, "NO"
     return
     end if
    end do
    s(c:c)="#"
  do i=b, d-1
  p=index(s(i:i),"#")
  q=index(s(i+1:i+1),"#")
   if ((p+q).eq.2) then
   print*, "NO"
   print*, sum
   return
   end if
  end do
  print*, "Yes"
  return
 end if

 if (sum.eq.0) then
 do i=b, d-1
  p=index(s(i:i),"#")
  q=index(s(i+1:i+1),"#")
   if ((p+q).eq.2) then
   print*, "No"
   return
   end if
  end do
  s(d:d)="#"
  do i=a, c-1
  p=index(s(i:i),"#")
  q=index(s(i+1:i+1),"#")
   if ((p+q).eq.2) then
   print*, "No"
   return
   end if
  end do
  print*, "Yes"
  return
 end if
 end if
end program

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

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

Submitted source-code file size, in bytes.

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

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


program sample
	implicit none
    real(8) :: N

    read(*,*) N

	write(*,*) 180*(N-2)

	stop
end program sample

Score : 100 points

Problem Statement

Given an integer N not less than 3, find the sum of the interior angles of a regular polygon with N sides.

Print the answer in degrees, but do not print units.

Constraints

3 \leq N \leq 100

Input

Input is given from Standard Input in the following format:

N

Output

Print an integer representing the sum of the interior angles of a regular polygon with N sides.

Sample Input 1

3

Sample Output 1

180

The sum of the interior angles of a regular triangle is 180 degrees.

Sample Input 2

100

Sample Output 2

17640


program main
	implicit none
    integer::a
    read(*,*)a
    write(*,*) 180*(a-2)
    stop
end program main


program siax
  implicit none
  integer :: N,l,i

  real(8) :: c,d,e
  character(len=15) :: S
  read(*,*) S
  l=0
  do i=1,len_trim(S)
     if(S(i:i).eq.'o') then
        l=l+1
     endif
  enddo
  if((15-len_trim(S)+l).lt.8) then
     write(*,'(a)') 'NO'
  else
     write(*,'(a)') 'YES'
  endif
!  write(*,'(i0)') l
end program siax

Score : 200 points

Problem Statement

Takahashi is competing in a sumo tournament.
The tournament lasts for 15 days, during which he performs in one match per day.
If he wins 8 or more matches, he can also participate in the next tournament.

The matches for the first k days have finished.
You are given the results of Takahashi's matches as a string S consisting of o and x.
If the i-th character in S is o, it means that Takahashi won the match on the i-th day; if that character is x, it means that Takahashi lost the match on the i-th day.

Print YES if there is a possibility that Takahashi can participate in the next tournament, and print NO if there is no such possibility.

Constraints

1 \leq k \leq 15

S is a string of length k consisting of o and x.

Input

Input is given from Standard Input in the following format:

S

Output

Print YES if there is a possibility that Takahashi can participate in the next tournament, and print NO otherwise.

Sample Input 1

oxoxoxoxoxoxox

Sample Output 1

YES

Takahashi has 7 wins and 7 losses before the last match. If he wins that match, he will have 8 wins.

Sample Input 2

xxxxxxxx

Sample Output 2

NO


! fenwic treeでバグらない要注意

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

        ret = vec%l
    end function

    pure subroutine check_array_allocation(vec)
        type(vector_int32),intent(inout):: vec

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

    function vec_at(vec,i) result(ret)
        class(vector_int32),intent(inout):: vec
        integer(int32):: i,ret

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

    function vec_back(vec) result(ret)
        class(vector_int32),intent(inout):: vec
        integer(int32):: ret

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

    function vec_head(vec) result(ret)
        class(vector_int32),intent(inout):: vec
        integer(int32):: ret

        ret = vec%at(1)
    end function

    pure subroutine vec_append_array(vec,l,r)
        type(vector_int32),intent(inout):: vec
        integer(int32),intent(in):: l,r
        integer(int32),allocatable:: tmp(:)

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

    pure subroutine vec_reduce_array(vec,l,r)
        type(vector_int32),intent(inout):: vec
        integer(int32),intent(in):: l,r
        integer(int32),allocatable:: tmp(:)

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

    pure subroutine check_allocation_size(vec)
        type(vector_int32),intent(inout):: vec
        integer(int32):: len_alloc

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

    pure subroutine vec_push_back(vec, v)
        class(vector_int32),intent(inout):: vec
        integer(int32),intent(in):: v

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

    pure subroutine vec_insert(vec,i,v)
        class(vector_int32),intent(inout):: vec
        integer(int32),intent(in)::i, v

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

    function vec_pop_back(vec) result(ret)
        class(vector_int32),intent(inout):: vec
        integer(int32):: ret

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

    function vec_pop(vec,i) result(ret)
        class(vector_int32),intent(inout):: vec
        integer(int32),intent(in):: i
        integer(int32):: ret

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

    subroutine vec_erase(vec,i)
        class(vector_int32):: vec
        integer(int32),intent(in):: i
        integer(int32):: dmp

        dmp = vec%pop(i)
    end subroutine

    function vec_to_array(vec) result(ret)
        class(vector_int32),intent(inout):: vec
        integer(int32):: ret(1:vec%l)

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

module merge_sort_mod
    use,intrinsic :: iso_fortran_env
    implicit none
    private
    public:: merge_sort

    interface merge_sort
        module procedure ms32, ms64
    end interface
contains
    recursive subroutine ms32(ar)
        integer(int32),intent(inout):: ar(:)
        integer(int32):: m

        if (size(ar) <= 1) then
            return
        else
            m = size(ar)/2
            call ms32(ar(:m)); call ms32(ar(m+1:))
            call sub_ms32(ar)
        end if
    end subroutine

    subroutine sub_ms32(ar)
        integer(int32),intent(inout):: ar(:)
        integer(int32):: tmp(1:size(ar))
        integer(int32):: m,l,r,i

        tmp(:) = ar(:)
        m = size(ar)/2; l=1;r=m+1
        do i=1,size(ar)
            if (m < l) then
                ar(i:)=tmp(r:); return
            else if (size(ar) < r) then
                ar(i:)=tmp(l:m); return
            else
                if (tmp(l) <= tmp(r)) then
                    ar(i) = tmp(l); l=l+1
                else
                    ar(i) = tmp(r); r=r+1
                end if
            end if
        end do
    end subroutine

    recursive subroutine ms64(ar)
        integer(int64),intent(inout):: ar(:)
        integer(int32):: m

        if (size(ar) <= 1) then
            return
        else
            m = size(ar)/2
            call ms64(ar(:m)); call ms64(ar(m+1:))
            call sub_ms64(ar)
        end if
    end subroutine

    subroutine sub_ms64(ar)
        integer(int64),intent(inout):: ar(:)
        integer(int64):: tmp(1:size(ar))
        integer(int32):: m,l,r,i

        tmp(:) = ar(:)
        m = size(ar)/2; l=1;r=m+1
        do i=1,size(ar)
            if (m < l) then
                ar(i:)=tmp(r:); return
            else if (size(ar) < r) then
                ar(i:)=tmp(l:r); return
            else
                if (tmp(l) <= tmp(r)) then
                    ar(i) = tmp(l); l=l+1
                else
                    ar(i) = tmp(r); r=r+1
                end if
            end if
        end do
    end subroutine
end module

! ar1のソート順序でar2もソートする。
module double_merge_sort_mod
    use,intrinsic :: iso_fortran_env
    implicit none
    private
    public:: double_merge_sort

    interface double_merge_sort
        module procedure dms32, dms64
    end interface
contains
    recursive subroutine dms32(ar1, ar2)
        integer(int32),intent(inout):: ar1(:), ar2(:)
        integer(int32):: m

        if (size(ar1) <= 1) then
            return
        else
            m = size(ar1)/2
            call dms32(ar1(:m), ar2(:m))
            call dms32(ar1(m+1:), ar2(m+1:))
            call sub_ms32(ar1,ar2)
        end if
    end subroutine

    subroutine sub_ms32(ar1, ar2)
        integer(int32),intent(inout):: ar1(:), ar2(:)
        integer(int32):: tmp1(1:size(ar1)), tmp2(1:size(ar2))
        integer(int32):: m,l,r,i

        m=size(ar1)/2; l=1; r=m+1
        tmp1(:)=ar1(:); tmp2(:)=ar2(:)

        do i=1,size(ar1)
            if (m < l) then
                ar1(i:) = tmp1(r:)
                ar2(i:) = tmp2(r:)
                return
            else if (size(ar1) < r) then
                ar1(i:) = tmp1(l:m)
                ar2(i:) = tmp2(l:m)
                return
            else
                if (tmp1(l) <= tmp1(r)) then
                    ar1(i) = tmp1(l)
                    ar2(i) = tmp2(l)
                    l=l+1
                else
                    ar1(i) = tmp1(r)
                    ar2(i) = tmp2(r)
                    r=r+1
                end if
            end if
        end do
    end subroutine

    recursive subroutine dms64(ar1, ar2)
        integer(int64),intent(inout):: ar1(:), ar2(:)
        integer(int32):: m

        if (size(ar1) <= 1) then
            return
        else
            m = size(ar1)/2
            call dms64(ar1(:m), ar2(:m))
            call dms64(ar1(m+1:), ar2(m+1:))
            call sub_ms64(ar1,ar2)
        end if
    end subroutine

    subroutine sub_ms64(ar1, ar2)
        integer(int64),intent(inout):: ar1(:), ar2(:)
        integer(int64):: tmp1(1:size(ar1)), tmp2(1:size(ar2))
        integer(int32):: m,l,r,i

        m=size(ar1)/2; l=1; r=m+1
        tmp1(:)=ar1(:); tmp2(:)=ar2(:)

        do i=1,size(ar1)
            if (m < l) then
                ar1(i:) = tmp1(r:)
                ar2(i:) = tmp2(r:)
                return
            else if (size(ar1) < r) then
                ar1(i:) = tmp1(l:m)
                ar2(i:) = tmp2(l:m)
                return
            else
                if (tmp1(l) <= tmp1(r)) then
                    ar1(i) = tmp1(l)
                    ar2(i) = tmp2(l)
                    l=l+1
                else
                    ar1(i) = tmp1(r)
                    ar2(i) = tmp2(r)
                    r=r+1
                end if
            end if
        end do
    end subroutine
end module

program main
    use,intrinsic :: iso_fortran_env
    use vector_int32_mod
    use merge_sort_mod
    use double_merge_sort_mod
    implicit none
    integer(int32):: n,a,b,i
    integer(int32), allocatable:: c(:), ne(:), d(:), leaf(:), ind(:), ans(:)
    type(vector_int32),allocatable:: g(:)

    read*, n
    allocate(ne(n),source=0)
    allocate(d(n),source=n+100)
    allocate(g(n))

    do i=1,n-1
        read*, a,b
        call g(a)%push_back(b)
        call g(b)%push_back(a)
        ne(a)=ne(a)+1
        ne(b)=ne(b)+1
    end do

    allocate(c(n))
    read*, c(:)
    call merge_sort(c)

    allocate(ind, source=[(i,i=1,n)])
    allocate(leaf, source=pack(ind,ne==1))
    do i=1,size(leaf)
        d(leaf(i)) = 0
        call dfs(leaf(i))
    end do

    call double_merge_sort(d, ind)

    allocate(ans(n))
    do i=1,n
        ans(ind(i))=c(i)
    end do
    print'(i0)', sum(c(1:size(c)-1))
    print'(*(i0,1x))', ans(:)
contains
    recursive subroutine dfs(ind)
        integer(int32):: ind,i,t

        do i=1,g(ind)%size()
            ! print'(*(i0,1x))', d(:)
            t = g(ind)%at(i)
            if (d(t) <= d(ind)+1) cycle
            d(t)=d(ind)+1
            call dfs(t)
        end do
    end subroutine
end program main

Score : 500 points

Problem Statement

You are given a tree with N vertices 1,2,\ldots,N, and positive integers c_1,c_2,\ldots,c_N.
The i-th edge in the tree (1 \leq i \leq N-1) connects Vertex a_i and Vertex b_i.

We will write a positive integer on each vertex in T and calculate our score as follows:

On each edge, write the smaller of the integers written on the two endpoints.

Let our score be the sum of the integers written on all the edges.

Find the maximum possible score when we write each of c_1,c_2,\ldots,c_N on one vertex in T, and show one way to achieve it. If an integer occurs multiple times in c_1,c_2,\ldots,c_N, we must use it that number of times.

Constraints

1 \leq N \leq 10000

1 \leq a_i,b_i \leq N

1 \leq c_i \leq 10^5

The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
:
a_{N-1} b_{N-1}
c_1 \ldots c_N

Output

Use the following format:

M
d_1 \ldots d_N

where M is the maximum possible score, and d_i is the integer to write on Vertex i.
d_1,d_2,\ldots,d_N must be a permutation of c_1,c_2,\ldots,c_N.
If there are multiple ways to achieve the maximum score, any of them will be accepted.

Sample Input 1

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

Sample Output 1

10
1 2 3 4 5

If we write 1,2,3,4,5 on Vertex 1,2,3,4,5, respectively, the integers written on the four edges will be 1,2,3,4, for the score of 10. This is the maximum possible score.

Sample Input 2

5
1 2
1 3
1 4
1 5
3141 59 26 53 59

Sample Output 2

197
59 26 3141 59 53

c_1,c_2,\ldots,c_N may not be pairwise distinct.


integer a,b
read*, a,b
print*, (3*a+b)/2
end

Score : 100 points

Problem Statement

We have A apples and P pieces of apple.

We can cut an apple into three pieces of apple, and make one apple pie by simmering two pieces of apple in a pan.

Find the maximum number of apple pies we can make with what we have now.

Constraints

All values in input are integers.

0 \leq A, P \leq 100

Input

Input is given from Standard Input in the following format:

A P

Output

Print the maximum number of apple pies we can make with what we have.

Sample Input 1

1 3

Sample Output 1

3

We can first make one apple pie by simmering two of the three pieces of apple. Then, we can make two more by simmering the remaining piece and three more pieces obtained by cutting the whole apple.

Sample Input 2

0 1

Sample Output 2

0

We cannot make an apple pie in this case, unfortunately.

Sample Input 3

32 21

Sample Output 3

58


program applepie
integer::a,p,c
read*,a,p
print*,(a*3+p)/2
end program applepie


      module piyo
      implicit none

      type rest
        character(10) :: s
        integer :: p,num
      end type

      contains
      subroutine sortS( r )
        implicit none
        type(rest),intent(inout) :: r(:)
        type(rest) :: buffer
        integer :: i,k,len

        len = size(r)

        do i = 1,len-1
          do k = i+1,len
            if( r(i)%s(:)>r(k)%s(:) )then
              buffer = r(i)
              r(i) = r(k)
              r(k) = buffer
            end if
          end do
        end do
      end subroutine

      function sortP( r ) result( res )
        implicit none
        type(rest) :: r(:)
        type(rest) :: buffer
        type(rest),allocatable :: res(:)
        integer :: i,k,len

        len = size(r)

        do i = 1,len-1
          do k = i+1,len
            if( r(i)%p<r(k)%p )then
              buffer = r(i)
              r(i) = r(k)
              r(k) = buffer
            end if
          end do
        end do
        res = r
      end
      end module piyo

      PROGRAM GuideBook
      use piyo
      IMPLICIT NONE
      integer :: n
      type(rest),allocatable :: restaurant(:)
      integer :: i,len,k
      character(10) :: buffer

      read*,n
      allocate( restaurant(n) )
      do i = 1,n
        read*,restaurant(i)%s,restaurant(i)%p
        restaurant(i)%num = i
      end do

      call sortS( restaurant )

      i = 1
      do while(.true.)
        len = 0
        do k = 1,n
          if( restaurant(k)%s(1:10)==restaurant(i)%s(1:10) )then
            len = len + 1
          end if
        end do
        restaurant(i:i+len-1) = sortP( restaurant(i:i+len-1) )
        i = i+len

        if( i>=n )then
          exit
        end if
      end do

   !   print *,n
   !   do i = 1,n
   !     print*,restaurant(i)
   !   end do

      do i = 1,n
        print*,restaurant(i)%num
      end do

      END PROGRAM

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 main
Implicit None
	integer i,N
	integer,allocatable :: P(:)
	character(10) s(:)
	allocatable :: s

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

	do i=1,N
		read*,S(i),P(i)
	end do

	do i=1,N
		write(6,'(i0,x,i0)') S(i),P(i)
	end do
!wakannai
end program main


integer N,M
integer,allocatable,dimension(:)::k,p
integer,allocatable,dimension(:,:)::S
integer reader(10)
integer check,on,l,b
integer ans
read*,N,M
allocate(k(M),p(M),S(M,10))

S=0
do i=1,M
  reader=0
  read*,k(i),reader(1:k(i))
  do j=1,k(i)
    S(i,reader(j))=1
  end do
end do
read*,p

ans=0
do i=0,2**N
  check=0
  do j=1,M
    b=i
    on=0
    do l=1,N+1
      if(and(b,1)==1)on=mod(on+S(j,l),2)
      b=rshift(b,1)
    end do
    if(on/=p(j))check=1
  end do
  if(check==0)ans=ans+1
end do
print"(i0)",ans
end

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 ATCODER

implicit none
integer(16) :: n,m,i,max=0,l,count=0,h,j,on,check=0
integer(16),allocatable :: k(:),s(:,:),p(:),sw(:)

read*, n,m
allocate(k(m),s(m,10),p(m),sw(n))
s = 0
do i = 1, m, 1
  read*, k(i),s(i,1:k(i))
end do
read*, p(1:m)

do i = 1, n
  max = max + 2**(i-1)
end do

do i = 0, max, 1
  check = 0
  l = i
  sw = 0
  do j = 1, n, 1
    sw(j) = mod(l,2)
    l = l/2
  end do
  do j = 1, m, 1
    on = 0
    do h = 1, k(j), 1
      if ( sw(s(j,h)) == 1 ) then
        on = on + 1
      end if
    end do
    if ( mod(on,2) /= p(j) ) then
      check = 1
      exit
    end if
  end do
  if ( check == 0 ) then
    count = count + 1
  end if
end do

print'(i0)', count

END PROGRAM ATCODER


program roadwork
  implicit none
  integer :: n, q, stx(200000,3), d, i, k, x, m, u
  stx = 0
  read(*,*) n, q
  do i = 1, n
    read(*,*) stx(i,1:3)
  end do
  call merge_sort(n,stx(1:n,1:3),3)
  m = m
  do k = 1, q
    read(*,*) d
    x = -1
    u = 0
    do i = m, n
      if ((stx(i,1).le.stx(i,3)+d).and.(stx(i,3)+d.lt.stx(i,2))) then
        x = stx(i,3)
        exit
      else if (stx(i,3)+d.ge.stx(i,2)) then
        stx(i,3) = -1
        u = u + 1
      end if
    end do
    m = m + u
    write(*,'(i0)') x
    if (u.gt.0) call merge_sort(n,stx(1:n,1:3),3)
  end do
  stop
contains
  subroutine merge_sort(n,c,idx)
    implicit none
    integer, intent(in) :: n, idx
    integer, intent(inout) :: c(n,3)
    integer :: number, length, i, ubnd
    number = n
    length = 1
    do while (number.gt.1)
      do i = 1, number/2
        ubnd = min(2*i*length,n)
        call merge(c(2*(i-1)*length+1:(2*i-1)*length,:),c((2*i-1)*length+1:ubnd,:),idx)
      end do
      length = 2*length
      number = (number+1)/2
    end do
    return
  end subroutine merge_sort
  subroutine merge(c1,c2,idx)
    implicit none
    integer, intent(inout) :: c1(:,:), c2(:,:)
    integer, intent(in) :: idx
    integer :: c(size(c1(:,1))+size(c2(:,1)),size(c1(1,:)))
    integer :: i1, i2, n1, n2
    i1 = 1
    i2 = 1
    n1 = size(c1(:,1))
    n2 = size(c2(:,1))
    do while ((i1.le.n1).and.(i2.le.n2))
      if (c1(i1,idx).le.c2(i2,idx)) then
        c(i1+i2-1,:) = c1(i1,:)
        i1 = i1+1
      else
        c(i1+i2-1,:) = c2(i2,:)
        i2 = i2+1
      end if
    end do
    if (i1.le.n1) then
      c(i1+i2-1:n1+n2,:) = c1(i1:n1,:)
    else if (i2.le.n2) then
      c(i1+i2-1:n1+n2,:) = c2(i2:n2,:)
    end if
    c1 = c(1:n1,:)
    c2 = c(n1+1:n1+n2,:)
    return
  end subroutine merge
end program roadwork

Score : 500 points

Problem Statement

There is an infinitely long street that runs west to east, which we consider as a number line.

There are N roadworks scheduled on this street.
The i-th roadwork blocks the point at coordinate X_i from time S_i - 0.5 to time T_i - 0.5.

Q people are standing at coordinate 0. The i-th person will start the coordinate 0 at time D_i, continue to walk with speed 1 in the positive direction and stop walking when reaching a blocked point.

Find the distance each of the Q people will walk.

Constraints

All values in input are integers.

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

0 \leq S_i < T_i \leq 10^9

1 \leq X_i \leq 10^9

0 \leq D_1 < D_2 < ... < D_Q \leq 10^9

If i \neq j and X_i = X_j, the intervals [S_i, T_i) and [S_j, T_j) do not overlap.

Input

Input is given from Standard Input in the following format:

N Q
S_1 T_1 X_1
:
S_N T_N X_N
D_1
:
D_Q

Output

Print Q lines. The i-th line should contain the distance the i-th person will walk or -1 if that person walks forever.

Sample Input 1

4 6
1 3 2
7 13 10
18 20 13
3 4 2
0
1
2
3
5
8

Sample Output 1

2
2
10
-1
13
-1

The first person starts coordinate 0 at time 0 and stops walking at coordinate 2 when reaching a point blocked by the first roadwork at time 2.

The second person starts coordinate 0 at time 1 and reaches coordinate 2 at time 3. The first roadwork has ended, but the fourth roadwork has begun, so this person also stops walking at coordinate 2.

The fourth and sixth persons encounter no roadworks while walking, so they walk forever. The output for these cases is -1.


program prob26
    implicit none
    integer :: a,b
    read(*,*) a,b
    if(a <= 5) then
        write(*,*) "0"
    else if( a<= 12) then
        write(*,*) b/2
    else
        write(*,*) b
    end if

    stop
contains
end program prob26

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,N,M,count,Lmax,Rmin
	integer,allocatable :: L(:),R(:)
	read(5,*) N,M
	allocate(L(M),R(M))
	Lmax=0	;Rmin=N

	do i=1,M
		read(5,*) L(i),R(i)
		if(L(i).ge.Lmax)	Lmax=L(i)
		if(R(i).le.Rmin)	Rmin=R(i)
	end do
	count=Rmin-Lmax+1
	write(6,'(i0)') count

end program main


program main
  implicit none
  integer(8) :: r, d, x, i
  read (*, *) r, d, x
  do i = 1, 10
    x = r * x - d
    write(*, "(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 n1
  implicit none
  integer :: r,d,x2000,x200x,i

  read(*,*) r,d,x2000
  x200x=x2000
  do i=1,10
     x200x=r*x200x-d
     write(*,'(i0)') x200x
  enddo

end program n1


program ccc
implicit none

integer :: n, m, i, lmax, rmin, res
integer,allocatable,dimension(:) :: l, r

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

lmax=maxval(l)
rmin=minval(r)

res=rmin-lmax+1

if(res<0) then
write(*,'(i0)') 0
return
end if
write(*,'(i0)') res

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 prison
 integer N, M, i, j, res
 integer :: X(10**5) = 0
 integer,allocatable,dimension(:) :: A
 integer,allocatable,dimension(:) :: B
  res = 0
 read(*,*) N, M
 allocate(A(M))
 allocate(B(M))
  do 100 i=1,M
  read(*,*) A(i), B(i)
   do 200 j=A(i), B(i)
   X(j) = X(j) +1
   200 continue
  100 continue
 do 1000 i=1, B(M)
  if (X(i).eq.M) then
  res = res + 1
  end if
 1000 continue
 write(*,*) res
end program


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

    read(*,*) n,m
    allocate(x(n),y(m),z(m))
    read(*,*)x
    do i=1,m
        read(*,*)y(i),z(i)
    end do

    call msort(x)
    if(m>1)then
        call m2sort(z,y)
    end if
    a=n
    b=m
    c=n
    ans=0
    d=0
    do j=1,n
        if(x(a)>=z(b))then
            ans=ans+x(a)
            c=c-1
            a=a-1
            if(c==0)then
                exit
            endif
        elseif(c<=y(b))then
                ans=ans+z(b)*c
                exit
        elseif(b==1)then
                ans=ans+z(b)*y(b)
                c=c-y(b)
                b=b-1

                    do i=1,c
                        ans=ans+x(a)
                        a=a+1
                    end do
                    exit
        else
            ans=ans+z(b)*y(b)
                c=c-y(b)
                b=b-1

        endif
    end do
    write(*,*)ans
    stop
    contains
    recursive subroutine msort(x)
    implicit none
    integer(8), intent(inout) :: x(:)

    integer(8) :: n, mid, i, j, k
    integer(8), allocatable :: tmp(:)

    n = size(x)

    if(n == 1) then
      return
    end if

    !
    ! 前半と後半に分けてソート
    ! 1~midとmid+1~nをそれぞれソート済みにする
    !
    mid = n / 2
    call msort(x(:mid))
    call msort(x(mid+1:))

    !
    ! tmpという配列にxを一時的に保管
    ! マージソートは外部ソート
    !
    allocate(tmp(n))
    tmp = x

    ! tmpの後半部分を左右反転
    do k = n, mid+1, -1
      tmp(k) = x(mid + n - k + 1)
    end do

    ! iは前半のリストのうちまだ見ていないものの最小値
    ! jは後半のリストのうちまだ見ていないものの最小値
    ! kは見た回数の合計
    i = 1
    j = n

    do k = 1, n
      !
      ! 一番左と一番右のうち小さい方をxに入れていく
      ! ここが等号つき不等号なので、安定ソートとなる
      !
      if(tmp(i) <= tmp(j)) then
        x(k) = tmp(i)
        i = i + 1
      else
        x(k) = tmp(j)
        j = j - 1
      end if
    end do

    deallocate(tmp)
    return
  end subroutine msort

  recursive subroutine m2sort(x,y)
    implicit none
    integer(8), intent(inout) :: x(:),y(:)

    integer(8) :: n, mid, i, j, k
    integer(8), allocatable :: tmp(:),tmp2(:)

    n = size(x)

    if(n == 1) then
      return
    end if

    mid = n / 2
    call m2sort(x(:mid),y(:mid))
    call m2sort(x(mid+1:),y(mid+1:))

    !
    ! tmpという配列にxを一時的に保管
    ! マージソートは外部ソート
    !
    allocate(tmp(n),tmp2(n))
    tmp(:) = x(:)
    tmp2(:)=y(:)
    ! tmpの後半部分を左右反転
    do k = n, mid+1, -1
      tmp(k) = x(mid + n - k + 1)
      tmp2(k) = y(mid + n - k + 1)
    end do

    ! iは前半のリストのうちまだ見ていないものの最小値
    ! jは後半のリストのうちまだ見ていないものの最小値
    ! kは見た回数の合計
    i = 1
    j = n

    do k = 1, n
      !
      ! 一番左と一番右のうち小さい方をxに入れていく
      ! ここが等号つき不等号なので、安定ソートとなる
      !
      if(tmp(i) <= tmp(j)) then
        x(k) = tmp(i)
        y(k)=tmp2(i)
        i = i + 1
      else
        x(k) = tmp(j)
        y(k)=tmp2(j)
        j = j - 1
      end if
    end do

    deallocate(tmp,tmp2)
    return
  end subroutine m2sort
end program sample

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 main
implicit none
integer(8) :: i, j, k
integer(8) :: n, m, nn
integer(8), allocatable :: a(:), b(:), c(:), d(:)

read(*,*) n, m
allocate( a(n), b(m), c(m) )
read(*,*) a
do i = 1, m
   read(*,*) b(i), c(i)
end do
nn = n + sum(b)
allocate( d(nn) )
d(1:n) = a(1:n)
k = n
do i = 1, m
   do j = k+1,k+b(i)
      d(j) = c(i)
   end do
   k = k+b(i)
end do
call heapsort( nn, d )
write(*,'(i0)') sum(d(nn-n+1:nn))

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

  integer(8) ::i,k,j,l
  integer(8) :: t

  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
        end if
     end if
     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
        end if
        if (t.lt.array(j))then
           array(i)=array(j)
           i=j
           j=j+j
        else
           j=k+1
        end if
     end do
     array(i)=t
  end do
end subroutine heapsort
end program main


PROGRAM ATCODER

implicit none
integer(16) :: i,j,n,m,k,ans=0,count
integer(16),allocatable :: a(:),b(:),c(:),d(:),i_d(:),index(:)

read*, n,m
allocate(a(n),b(m),c(m))
read*, a(1:n)
do i = 1, m, 1
  read*, b(i),c(i)
end do

allocate(d(n+m),i_d(n+m),index(n+m))
i_d = 1

do i = 1, n+m, 1
  if ( i >= n+1 ) then
    d(i) = c(i-n)
    i_d(i) = b(i-n)
  else
    d(i) = a(i)
  end if
end do

call heapsort2(n+m,d,index)

count = n
do i = n+m, m+1, -1
  if ( count - i_d(index(i)) <= 0 ) then
    ans = ans + count*d(i)
    exit
  end if
  ans = ans + i_d(index(i))*d(i)
  count = count - i_d(index(i))
end do

print'(i0)', ans

contains
  subroutine heapsort2(n,array,turn)
  implicit none
  integer(16),intent(in)::n
  integer(16),intent(out)::turn(1:n)
  integer(16),intent(inout)::array(1:n)

  integer::i,k,j,l,m
  double precision::t

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

  do i=1,N
     turn(i)=i
  enddo

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

  return
  end subroutine heapsort2

END PROGRAM ATCODER


program main
implicit none
integer*8 :: n, m
integer*8 , allocatable :: a(:), b(:), c(:)
integer*8 :: i, j, k

read(*,*) n, m
allocate( a(n) )
allocate( b(m) )
allocate( c(m) )
read(*,*) a
do j = 1, m
   call heapsort(n,a)
   read(*,*) b(j), c(j)
   do k = 1, b(j)
      if( a(k) .ge. c(j) ) exit
         a(k) = c(j)
   end do
end do
write(*,'(i0)') sum(a)

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

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

  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
        end if
     end if
     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
        end if
        if (t.lt.array(j))then
           array(i)=array(j)
           i=j
           j=j+j
        else
           j=k+1
        end if
     end do
     array(i)=t
  end do
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 neoheapsort(M,C,B)
cindex=M
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==0)exit
  else
    exit
  endif
end do
print"(I0)",sum(A)

contains

subroutine neoheapsort(n,array,b)
  implicit none
!ここの入力は状況に応じて変更すること
  integer(16),intent(in) :: n
  integer(16),intent(inout) :: array(1:n)
  integer(16),intent(inout) :: b(1:n)
  integer(16)::i,k,j,l
  integer(16):: t,tb

  l=n/2+1
  k=n
  do while(k /= 1)
     if(l > 1)then
        l=l-1
        t=array(L)
        tb=b(l)
     else
        t=array(k)
        tb=b(k)
        array(k)=array(1)
        b(k)=b(1)
        k=k-1
        if(k == 1) then
           array(1)=t
           b(1)=tb
           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)
           b(i)=b(j)
           i=j
           j=j+j
        else
           j=k+1
        endif
     enddo
     array(i)=t
     b(i)=tb
  enddo
  return
end subroutine neoheapsort

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


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 neoheapsort(M,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 neoheapsort(n,array,b)
  implicit none
!ここの入力は状況に応じて変更すること
  integer(16),intent(in) :: n
  integer(16),intent(inout) :: array(1:n)
  integer(16),intent(inout) :: b(1:n)
  integer(16)::i,k,j,l
  integer(16):: t,tb

  l=n/2+1
  k=n
  do while(k /= 1)
     if(l > 1)then
        l=l-1
        t=array(L)
        tb=b(l)
     else
        t=array(k)
        tb=b(k)
        array(k)=array(1)
        b(k)=b(1)
        k=k-1
        if(k == 1) then
           array(1)=t
           b(1)=tb
           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)
           b(i)=b(j)
           i=j
           j=j+j
        else
           j=k+1
        endif
     enddo
     array(i)=t
     b(i)=tb
  enddo
  return
end subroutine neoheapsort

subroutine heapsort(n,array)
  implicit none
!ここの入力は状況に応じて変更すること
  integer(16),intent(in) :: n
  integer(16),intent(inout) :: array(1:n)
  integer(16)::i,k,j,l
  integer(16):: t

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


program main
implicit none
integer :: n, m
integer , allocatable :: a(:), b(:), c(:)
integer :: i, j, k

read(*,*) n, m
allocate( a(n) )
allocate( b(m) )
allocate( c(m) )
read(*,*) a(1:n)
call heapsort( n, a )
do j = 1, m
   read(*,*) b(j), c(j)
   do k = 1, b(j)
      if( a(k) .le.c(j) ) then
         a(k) = c(j)
      end if
   end do
   call heapsort( n,a )
end do
write(*,'(i0)') sum(a)

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

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

  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
        end if
     end if
     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
        end if
        if (t.lt.array(j))then
           array(i)=array(j)
           i=j
           j=j+j
        else
           j=k+1
        end if
     end do
     array(i)=t
  end do
end subroutine heapsort

end program main


integer(16) N,M,K
integer(16) waruno
integer(16) ans,tmp,i,j,com
read*,N,M,K
waruno=10**9+7
com=combination(N*M-2,K-2,waruno)
ans=0
do i=0,M-1
  do j=0,N-1
    tmp=mod((i+j)*(M-i)*(N-j)*com*merge(1,2,i*j==0),waruno)
    ans=mod(ans+tmp,waruno)
  end do
end do
print"(i0)",ans

contains

function combination(n,k,l)
implicit none
integer(16),intent(in)::n,k,l
integer(16) combination,i
integer(16),allocatable,dimension(:)::Kaijo

!まず最初に階乗について計算します
allocate(Kaijo(0:N))
Kaijo(0)=1
do i=1,N
  Kaijo(i)=mod(Kaijo(i-1)*i,l)
end do

!そこから階乗を使ってコンビネーションを計算します
!invはmod 10**9+7の下での逆数です
combination=mod(Kaijo(N)*inv(Kaijo(K),l)*inv(Kaijo(N-K),l),l)

end function combination

function inv(K,L)
integer(16),intent(in)::k,l
integer(16) l2,y
integer(16) inv
!フェルマーの小定理から-1乗とは10**9+7-2乗だよねということで
!10**9+7-2乗を計算します

!計算に使うのは繰り返し二乗法
inv=1
l2=l-2
y=k
do
  if(and(l2,1)==1)inv=mod(inv*y,l)
  y=mod(y*y,l)
  l2=rshift(l2,1)
  if(l2==0)exit
end do
end function inv
end

Score : 500 points

Problem Statement

We have a grid of squares with N rows and M columns. Let (i, j) denote the square at the i-th row from the top and j-th column from the left. We will choose K of the squares and put a piece on each of them.

If we place the K pieces on squares (x_1, y_1), (x_2, y_2), ..., and (x_K, y_K), the cost of this arrangement is computed as:

\sum_{i=1}^{K-1} \sum_{j=i+1}^K (|x_i - x_j| + |y_i - y_j|)

Find the sum of the costs of all possible arrangements of the pieces. Since this value can be tremendous, print it modulo 10^9+7.

We consider two arrangements of the pieces different if and only if there is a square that contains a piece in one of the arrangements but not in the other.

Constraints

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

2 \leq K \leq N \times M

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M K

Output

Print the sum of the costs of all possible arrangements of the pieces, modulo 10^9+7.

Sample Input 1

2 2 2

Sample Output 1

8

There are six possible arrangements of the pieces, as follows:

((1,1),(1,2)), with the cost |1-1|+|1-2| = 1

((1,1),(2,1)), with the cost |1-2|+|1-1| = 1

((1,1),(2,2)), with the cost |1-2|+|1-2| = 2

((1,2),(2,1)), with the cost |1-2|+|2-1| = 2

((1,2),(2,2)), with the cost |1-2|+|2-2| = 1

((2,1),(2,2)), with the cost |2-2|+|1-2| = 1

The sum of these costs is 8.

Sample Input 2

4 5 4

Sample Output 2

87210

Sample Input 3

100 100 5000

Sample Output 3

817260251

Be sure to print the sum modulo 10^9+7.


program main
  implicit none
  integer :: n, i, mi
  integer, allocatable :: a(:)
  read (*, *) n
  allocate (a(n))
  read (*, *) (a(i), i = 1, n)
  mi = 999999
  do i = 1, n
    mi = min(mi, fact_of_two(a(i)))
  end do
  write (*, "(i0)") mi
  deallocate (a)
  contains

  function fact_of_two(i)
    implicit none
    integer :: i, j, fact_of_two
    do j = 0, 100
      if (mod(i, 2 ** j) /= 0) then
        exit
      end if
    end do
    if (j < 1) stop 1
    fact_of_two = j - 1
  end function fact_of_two
end program main

Score : 400 points

Problem Statement

We have a tree with N vertices numbered 1 to N.
The i-th edge in the tree connects Vertex u_i and Vertex v_i, and its length is w_i.
Your objective is to paint each vertex in the tree white or black (it is fine to paint all vertices the same color) so that the following condition is satisfied:

For any two vertices painted in the same color, the distance between them is an even number.

Find a coloring of the vertices that satisfies the condition and print it. It can be proved that at least one such coloring exists under the constraints of this problem.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq u_i < v_i \leq N

1 \leq w_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
u_1 v_1 w_1
u_2 v_2 w_2
.
.
.
u_{N - 1} v_{N - 1} w_{N - 1}

Output

Print a coloring of the vertices that satisfies the condition, in N lines.
The i-th line should contain 0 if Vertex i is painted white and 1 if it is painted black.

If there are multiple colorings that satisfy the condition, any of them will be accepted.

Sample Input 1

3
1 2 2
2 3 1

Sample Output 1

0
0
1

Sample Input 2

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

Sample Output 2

1
0
1
0
1


program kadai
implicit none

integer  ::  in
integer,allocatable  ::  u(:),v(:),w(:),flag(:),a(:)
integer  ::  i,j
read(*,*) in

allocate(u(in))
allocate(v(in))
allocate(w(in))
allocate(flag(in))
allocate(a(in))

do i=1,in-1
read(*,*) u(i),v(i),w(i)
a(i)=0
end do
a(in)=0

do j=1,in-1
if(u(i)==j) then
if(mod(w(i),2)==0) then
a(u(i))=1
a(v(i))=1
end if
end if
end do

do i=1,in
write(*,*) a(i)
end do

end program kadai


PROGRAM ATCODER

implicit none
integer(16) :: n,i,j,count=0,count_a=0,count_b=0
character(len=10),allocatable :: s(:)

read*, n
allocate(s(n))
read*, s(1:n)

do i = 1, n
  do j = 1, len_trim(s(i))-1
    if ( s(i)(j:j+1) == 'AB' ) then
      count = count + 1
    end if
  end do
  if ( s(i)(1:1) == 'B' ) then
      count_b = count_b + 1
    end if
    if ( s(i)(len_trim(s(i)):len_trim(s(i))) == 'A' ) then
      count_a = count_a + 1
    end if
end do

print'(i0)',  count + min(count_a,count_b)

END PROGRAM ATCODER

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


program divertaA
	integer N, K, X
    read(*, *) N, K
    X = N - K +1
    print '(i0)', X
end program


integer N,K
read*,N,K
print"(i0)",N-K+1
end


program main
  implicit none
  integer :: i, j, k, r, g, b, n, cnt = 0

  read *, r, g, b, n
  do i = 0, int(n/r)
    do j = 0, int(n/g)
      if (mod(n - r*i + g*j, b) == 0) cnt = cnt + 1
    end do
  end do

  print '(i0)', cnt
end program

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 ATCODER

implicit none
integer :: r,g,b,n,x,y,z,max_r,max_g,max_b,i,j,count=0

read*, r,g,b,n

max_r = n/r ; max_g = n/g ; max_b = n/b

do i = 0, max_r, 1
  do j = 0, max_g, 1
    x = i ; y = j ; z = (n - x*r - y*g)/b
    if ( mod(n - x*r - y*g,b) == 0 .and. z >=0 .and. z <= max_b ) then
      count = count + 1
    end if
  end do
end do

print'(i0)',  count

END PROGRAM ATCODER


program prob3
    implicit none
    integer::N, i, l, ans, a, b, c, j
    character(len=10)::s

    ans = 0
    a = 0
    b = 0
    read(*,*) N
    do j = 1, N
        read(*,*) s
        l = len_trim(s)
        do i = 1, l-1
            if(s(i:i) .eq. "A" .and. s(i+1:i+1) .eq. "B") then
                ans = ans + 1
            end if
        end do
        if(s(1:1) .eq. "B")then
            b = b+1
        end if
        if(s(l:l) .eq. "A")then
            a = a+1
        end if
    end do
    ans = ans + min(a,b)
    write(*,*) ans

    stop
end program

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 :: i, j, n, lt, cnt = 0, a = 0, b = 0, ab = 0
  character*10 :: s

  read *, n
  do i = 1, n
    read *, s
    lt = len_trim(s)
    if ((s(1:1) /= 'B') .and. (s(lt:lt) == 'A')) a = a + 1
    if ((s(1:1) == 'B') .and. (s(lt:lt) /= 'A')) b = b + 1
    if ((s(1:1) == 'B') .and. (s(lt:lt) == 'A')) ab = ab + 1
    do j = 1, lt
      if (s(j: j+1) == 'AB') cnt = cnt + 1
    end do
  end do
  if (a > b) then
    print '(i0)', cnt + b + ab
  else if (b > a) then
    print '(i0)', cnt + a + ab
  else
    print '(i0)', cnt + a + max(0, ab-1)
  end if
end program


PROGRAM ATCODER

implicit none
integer(16) :: n,i,s_n,ans=0

read*, n

s_n = ceiling(sqrt(real(n,8)),16)

do i = 1, s_n, 1
  if ( mod(n,i) == 0 ) then
    if ( i /= 1 ) then
      if ( mod(n,i-1) == n/(i-1) ) then
        ans = ans + (i-1)
      end if
    end if
    if ( n/i /= 1 ) then
      if ( mod(n,n/i-1) == n/(n/i-1) ) then
        ans = ans + (n/i-1)
      end if
    end if
  end if
end do

print'(i0)', ans

END PROGRAM ATCODER

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 main
Implicit None
	integer(8)::n,a=1,p=0

	read*,n

	do
		if((n/a-1) < a) exit
		if(mod(n,a) == 0)then
			if((n/a-1) > a) p = p + n/a-1
		endif
		a = a+1
	enddo
	print"(i0)",p
end program main


program main
	implicit none
    integer::a,b,c
    read(*,*)a,b,c
    write(*,*) (c/a)*b
    stop
end program main

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 a,b,c
read*, a,b,c
print*, c/a*b
end


integer :: n,i,ans=0
integer,allocatable :: v(:),c(:)

read*,n
allocate( v(n),c(n) )
read*,v
read*,c

do i = 1,n
  if( v(i)>=c(i) )then
    ans = ans + v(i) - c(i)
  end if
end do
print*,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


program rss
  implicit none
  integer :: N,i,val
  integer,allocatable :: V(:),C(:)

  read(*,*) N
  allocate(V(N))
  allocate(C(N))

  read(*,*) V
  read(*,*) C
  val=0
  do i=1,N
     if(V(i).gt.C(i)) then
        val=val+(V(i)-C(i))
     endif
  enddo
  write(*,'(i0)') val
  deallocate(V)
  deallocate(C)

end program rss


integer(16) n
integer(16),allocatable,dimension(:)::a
integer check
integer zero
read*,N
allocate(a(N))
read*,a
check=2
zero=1
do i=1,N
  if(a(i)==0)zero=0
  if(a(i)<0)then
    check=check+1
    a(i)=-a(i)
  end if
end do
print"(I0)",sum(a)-2*minval(a)*zero*merge(0,1,mod(check,2)==0)
end

Score : 400 points

Problem Statement

There are N integers, A_1, A_2, ..., A_N, arranged in a row in this order.

You can perform the following operation on this integer sequence any number of times:

Operation: Choose an integer i satisfying 1 \leq i \leq N-1. Multiply both A_i and A_{i+1} by -1.

Let B_1, B_2, ..., B_N be the integer sequence after your operations.

Find the maximum possible value of B_1 + B_2 + ... + B_N.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

-10^9 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible value of B_1 + B_2 + ... + B_N.

Sample Input 1

3
-10 5 -4

Sample Output 1

19

If we perform the operation as follows:

Choose 1 as i, which changes the sequence to 10, -5, -4.

Choose 2 as i, which changes the sequence to 10, 5, 4.

we have B_1 = 10, B_2 = 5, B_3 = 4. The sum here, B_1 + B_2 + B_3 = 10 + 5 + 4 = 19, is the maximum possible result.

Sample Input 2

5
10 -4 -8 -11 3

Sample Output 2

30

Sample Input 3

11
-1000000000 1000000000 -1000000000 1000000000 -1000000000 0 1000000000 -1000000000 1000000000 -1000000000 1000000000

Sample Output 3

10000000000

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


Program Tenka2019A

integer a,b,c

read(*,*) a,b,c

if (a<b) then
 if (a<c .and. c<b) then
  write(*,*) 'Yes'
 else
  write(*,*) 'No'
 end if
else
 if (b<c .and. c<a) then
  write(*,*) 'Yes'
 else
  write(*,*) 'No'
 end if
end if

stop
end

Score : 100 points

Problem Statement

There are three houses on a number line: House 1, 2 and 3, with coordinates A, B and C, respectively.
Print Yes if we pass the coordinate of House 3 on the straight way from House 1 to House 2 without making a detour, and print No otherwise.

Constraints

0\leq A,B,C\leq 100

A, B and C are distinct integers.

Input

Input is given from Standard Input in the following format:

A B C

Output

Print Yes if we pass the coordinate of House 3 on the straight way from House 1 to House 2 without making a detour, and print No otherwise.

Sample Input 1

3 8 5

Sample Output 1

Yes

We pass the coordinate 5 on the straight way from the house at coordinate 3 to the house at coordinate 8.

Sample Input 2

7 3 1

Sample Output 2

No

Sample Input 3

10 2 4

Sample Output 3

Yes

Sample Input 4

31 41 59

Sample Output 4

No


program main
  implicit none
  integer :: a, b, c
  logical :: flag = .false.
  read *, a, b, c

  if ((a <= c) .and. (b >= c)) flag = .true.
  if ((a >= c) .and. (b <= c)) flag = .true.

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

end program


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

  read(*,*) a,b,c

  if((a-c)*(b-c).lt.0) then
     write(*,'(a)') 'Yes'
  else
     write(*,'(a)') 'No'
  endif

end program otw


program main
  implicit none
  integer A, B, C
  read(*, *) A, B, C
  if (C<A .and. C<B) then
    write(*,'(A)') 'No'
  elseif (C>A .and. C>B) then
    write(*,'(A)') 'No'
  else
    write(*,'(A)') 'Yes'
  end if
end program main


program main
 implicit none

 integer N,K,i
 character*80 S,e
 read(*,*) N,S,K
 e(1:1) = S(K:K)
 do i=1,N
  if(S(i:i) /= e(1:1)) then
   S(i:i) = '*'
  end if
 end do

 write(*,*) S
end program main

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 prob3
    implicit none
    integer::N,i,j,l,w,b
    character(len=200005)::S
    read(*,*) N, S
    l = len_trim(S)
    w = 0
    b = 0
    do i = 1, l
        if(S(i:i) .eq. ".") then
            j = i
        end if
    end do
    do i = 1, j
        if(S(i:i) .eq. ".") then
            w = w + 1
        else
            b = b + 1
        end if
    end do
    write(*,*) min(w,b)
    stop
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


integer(16) N
character(2*10**5) S
integer(16) kcnt,scnt,i
integer ans
read*,N,S
kcnt=0
scnt=0
do i=1,N
  if(S(i:i)==".")scnt=scnt+1
end do
ans=min(scnt,N-scnt)
do i=1,N
  if(S(i:i)=="#")kcnt=kcnt+1
  if(S(i:i)==".")scnt=scnt-1
  if(S(i:i)/=S(i+1:i+1))then
    ans=min(ans,scnt+kcnt)
  endif
end do
print"(I0)",ans
end


program main
  implicit none
  integer(8) N, nd, ns, i, firsts, nd2,ans, finald, ns2
  !integer loc(4) loc2(4) !left ., left #, right ., right #
  character(2*(10**5)) S
  read(*, *) N
  read(*, *) S
  nd = 0
  ns = 0
  firsts = N
  finald = 1
  ns2 = 0
  nd2 = 0
  do i = 1,N
    if (S(i:i) == '.') then
      nd = nd + 1
    else
      ns = ns + 1
    end if
  end do
  !if (ns /= 0) then
    do i = 1,N
      if (S(i:i) == '#') then
        firsts = i
        exit
      end if
    end do

      do i = firsts,N
        if (S(i:i) == '.') then
          nd2 = nd2 + 1
        end if
      end do

  !end if
  !if (nd /= 0) then
    do i = 1,N
      if (S(i:i) == '.') then
        finald = i
      end if
    end do
      do i = 1,finald
        if (S(i:i) == '#') then
          ns2 = ns2 + 1
        end if
      end do

  ans = min(nd, ns, nd2, ns2)
  write(*,'(i0)') ans
end program main


PROGRAM ATCODER

implicit none
integer :: n,ans_b=0,ans_w=0,i
character(len=2*10**5) ::  s

read*, n,s

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

print'(i0)', min(ans_b,ans_w)

END PROGRAM ATCODER


program main
  implicit none
  integer A, B
  read(*, *) A, B
  if (A < B) then
    write(*, '(i0)') 2*B - 1
  else if (A > B) then
    write(*, '(i0)') 2*A - 1
  else
    write(*, '(i0)') A + B
  end if
end program main

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 main
  implicit none
  integer :: n, i, res
  integer, allocatable :: h(:)
  read (*, *) n
  allocate (h(n))
  read (*, *) h(:)
  res = 1
  do i = 2, n
    if (h(i) >= maxval(h(1:i - 1))) then
      res = res + 1
    end if
  end do
  write (*, "(i0)") res
end program main

Score : 200 points

Problem Statement

There are N mountains ranging from east to west, and an ocean to the west.

At the top of each mountain, there is an inn. You have decided to choose where to stay from these inns.

The height of the i-th mountain from the west is H_i.

You can certainly see the ocean from the inn at the top of the westmost mountain.

For the inn at the top of the i-th mountain from the west (i = 2, 3, ..., N), you can see the ocean if and only if H_1 \leq H_i, H_2 \leq H_i, ..., and H_{i-1} \leq H_i.

From how many of these N inns can you see the ocean?

Constraints

All values in input are integers.

1 \leq N \leq 20

1 \leq H_i \leq 100

Input

Input is given from Standard Input in the following format:

N
H_1 H_2 ... H_N

Output

Print the number of inns from which you can see the ocean.

Sample Input 1

4
6 5 6 8

Sample Output 1

3

You can see the ocean from the first, third and fourth inns from the west.

Sample Input 2

5
4 5 3 5 4

Sample Output 2

3

Sample Input 3

5
9 5 6 8 4

Sample Output 3

1


program great_ocean_view
  implicit none
  integer :: n, h(0:20), i, m
  h = 0
  read(*,*) n
  read(*,*) h(1:n)
  m = 0
  do i = 1, n
    if (h(i).ge.maxval(h(0:i-1))) m = m + 1
  end do
  write(*,'(i0)') m
  stop
end program great_ocean_view


program gob
    implicit none
    integer :: n=0,i=0,is_DomainOver=0,j=0,is_fault=0,cnt=1
    integer, allocatable :: H(:)
    read(*,*)n
    allocate(H(n))
    read(*,*) (H(i),i=1,n)
    !Domein check
    if(n<1 .or. 20<n)then
        is_DomainOver = 1
    endif
    do i=1,n
        if(H(i)<1 .or. 100<H(i))then
            is_DomainOver = 1
        endif
    enddo
    if(is_DomainOver == 1)then
    else
        do i=2,n
            if(H(i)<H(i-1))then
            else
                do j=1,i-1
                    if(H(i)<H(j))then
                        is_fault = 1
                    else
                    endif
                enddo
                if(.not. is_fault == 1)then
                    cnt = cnt + 1
                endif
            endif
        enddo
    endif

    write(*,*)cnt

    deallocate(H)
end program gob


implicit none
integer n,i,j
integer :: counts=0,max_height=0
integer,allocatable,dimension(:)::h

read(5,*) n

allocate(h(n))

read(5,*) (h(i),i=1,n)

do j=1,n
if(h(j).ge.max_height) then
counts=counts+1
endif
max_height=max(max_height,h(j))
enddo

write(6,*) counts

deallocate(h)
end


program ccc
implicit none

integer :: l, i, res1, res2
character(10**5+1) :: s, s1, s2

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

res1=0
res2=0
do i=1,l,2
s1(i:i)='0'
s1(i+1:i+1)='1'
s2(i:i)='1'
s2(i+1:i+1)='0'
end do

s1=trim(s1(1:l))
s2=trim(s2(1:l))

do i=1,l
if(s(i:i)==s1(i:i)) res1=res1+1
if(s(i:i)==s2(i:i)) res2=res2+1
end do

write(*,'(i0)') min(res1,res2)

end program

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


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
  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
    num_operations = 0_INT32

    do itr = 1_INT32, len_trim(given_string), 1_INT32
      if (mod(itr, 2_INT32) .ne. 0_INT32) then
        if (given_string(itr:itr) .ne. '0') num_operations(0) = num_operations(0) + 1_INT32
        if (given_string(itr:itr) .ne. '1') num_operations(1) = num_operations(1) + 1_INT32
      else
        if (given_string(itr:itr) .ne. '1') num_operations(0) = num_operations(0) + 1_INT32
        if (given_string(itr:itr) .ne. '0') num_operations(1) = num_operations(1) + 1_INT32
      end if
    end do

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

    ! 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, c
  read(*, *) s
  s = trim(s)
  n = len(s)
  !01010101010
  do i = 1,n
    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 coloring_colorfully
  implicit none
  character(100000) :: s
  integer :: n, i, o0, o1, e0, e1
  read(*,*) s
  s = trim(adjustl(s))
  n = len_trim(s)
  o0 = 0
  o1 = 0
  e0 = 0
  e1 = 0
  do i = 1, n, 2
    if (s(i:i).eq."0") then
      o0 = o0 + 1
    else
      o1 = o1 + 1
    end if
  end do
  do i = 2, n, 2
    if (s(i:i).eq."0") then
      e0 = e0 + 1
    else
      e1 = e1 + 1
    end if
  end do
  write(*,'(i0)') n - max(o0+e1,o1+e0)
  stop
end program coloring_colorfully


character(10**5) S
integer cnt
read*,S
!0101のリズムとあわせてみて1010のリズムどっちがあってるか判定して
!あってる方と合わせる感じでいきます
cnt=0
do i=1, len_trim(S)
  if(mod(i,2)==1)then
    if(S(i:i)=="0")cnt=cnt+1
  else
    if(S(i:i)=="1")cnt=cnt+1
  endif
end do
print"(I0)",min(len_trim(S)-cnt,cnt)
end


program handstand
  implicit none
  integer :: n, k, i, m, a(100000), r, x, y, z, t, j
  logical :: b(100000)
  character(100000) :: s
  a = 0
  b = .false.
  read(*,*) n, k
  if (n.eq.1) then
    write(*,'(i0)') 1
    stop
  end if
  read(*,*) s
  s = trim(adjustl(s))
  r = 1
  if (s(1:1).eq."0") r = -r
  a(1) = r
  x = -1
  y = -1
  do i = 2, n
    if (s(i:i).eq.s(i-1:i-1)) then
      a(i) = a(i-1) + r
    else
      b(i-1) = .true.
      if ((x.lt.0).and.(r.gt.0)) x = i-1
      if ((y.lt.0).and.(r.lt.0)) y = i-1
      r = -r
      a(i) = r
    end if
  end do
  b(n) = .true.
  if ((x.lt.0).or.(y.lt.0)) then
    write(*,'(i0)') abs(a(n))
    stop
  end if
  t = a(x)
  if (y.lt.x) then
    t = t - a(y)
    k = k - 1
  end if
  if ((k.gt.0).and.(n.gt.x)) then
    do i = x+1, n
      if (b(i)) then
        if (a(i).lt.0) then
          t = t - a(i)
          k = k - 1
        else
          t = t + a(i)
          z = i
          if (k.eq.0) exit
        end if
      end if
    end do
  end if
  m = t
  if (n.gt.z) then
    do i = z+1, n
      if (b(i)) then
        if (a(i).lt.0) then
          t = t + a(y) - a(i)
          if (x.lt.y) then
            t = t - a(x)
            do j = x+1, n
              if (b(j).and.a(j).gt.0) then
                x = j
                exit
              end if
            end do
          end if
          do j = y+1, n
            if (b(j).and.a(j).lt.0) then
              y = j
              exit
            end if
          end do
          if (m.lt.t) m = t
        else
          t = t + a(i)
          if (m.lt.t) m = t
        end if
      end if
    end do
  end if
  write(*,'(i0)') m
  stop
end program handstand

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(8)::n,k,i,p,q,a
	integer(8),allocatable::t(:),u(:)
	character(len=100000)::s

	read*,n,k
	read*,s

	p=1
	if(s(1:1) == "0")then
		p=p+1
	endif

	do i = 2,n
		if(s(i:i) /= s(i-1:i-1)) p = p+1
	enddo

	a = p

	allocate(t(p),u(a))

	p = 1
	q = 1
	if(s(1:1) == "0")then
		t(1) = 0
		q = q+1
	endif

	do i = 2,n
		if(s(i:i) == s(i-1:i-1))then
			p = p+1
			else
			t(q) = p
			p = 1
			q = q+1
		endif
	enddo
	t(q) = p

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

	k = 2*k+1

	if(a < k)then
		print"(i0)",n
		stop
	endif

	p = t(k)
	do i = 3,a-k+1,2
		if(p < t(k+i-1)-t(i-1)) p = t(k+i-1)-t(i-1)
	enddo

	print"(i0)",p
end program main


program sample
	implicit none
    integer(8)::i,j
    real(8)::a,b,c,d,e,k,sub
    real(8) :: sort(5)

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

    sort(1) =a
    sort(2) =b
    sort(3)=c
    sort(4)=d
    sort(5)=e

    do i=1,5
    	do j=i,5
        	if(sort(i)>sort(j)) then
            	sub=sort(i)
                sort(i)=sort(j)
                sort(j)=sub
            end if
        end do
    end do

    if(abs(sort(5)-sort(1))<=k) then
    	write(*,*) 'Yay!'
	else
    	write(*,*) ':('
    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 answer
  implicit none

  integer :: a, b, c, d, e, k

  read(*,*) a
  read(*,*) b
  read(*,*) c
  read(*,*) d
  read(*,*) e
  read(*,*) k
  if(abs(a-b)<=k .and. abs(a-c)<=k .and. abs(a-d)<=k .and. abs(a-e)<=k) then
     if(abs(b-c)<=k .and. abs(b-d)<=k .and. abs(b-e)<=k .and.abs(c-d)<=k) then
        if( abs(c-e)<=k .and. abs(d-e)<=k) then
           write(*,*) 'Yay!'
        else
           write(*,*) ':('
        end if
     else
        write(*,*) ':('
     end if
  else
     write(*,*) ':('
  end if

  stop
 end program answer


program prob1
    implicit none
    integer :: a,b,c,d,e,k
    read(*,*) a,b,c,d,e,k

    if(e-a > k) then
        write(*,*) ':('
    else
        write(*,*) 'Yay!'
    end if

    stop
contains
end program prob1


program kyomu
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: antena(5),k,i,j

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

    do i=1,4
        do j=i+1,5
            if (antena(j)-antena(i) > k) then
                print'(a)', ':('
                stop
            end if
        end do
    end do
    print'(a)', 'Yay!'

end program kyomu


program aaa
implicit none

integer :: k, i, j
integer, dimension(5) :: a

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

do i=1, 5
 do j=i+1, 5
 if(abs(a(i)-a(j))>k) then
 write(*,'(a2)') ':('
 return
 end if
 end do
end do

write(*,'(a4)') 'Yay!'

end program


module ABC123

  ! <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 :: ABC123_A

  ! constants for this <module>
  character(len=4, kind=1), parameter, private :: string_communicatable     = 'Yay!'
  character(len=2, kind=1), parameter, private :: string_NOT_communicatable = ':('
  integer  (kind=INT8),     parameter, private :: num_antenna               = 5_INT8

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

  subroutine ABC123_A

    ! variables for this <subroutine>
    integer(kind=INT8) :: coordinate_A, coordinate_E
    integer(kind=INT8) :: distance_max

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

    ! STEP.01
    ! read out the coordinate of the antennas
    read(unit=INPUT_UNIT, fmt='(I3)') coordinate_A

    do itr = 2_INT8, num_antenna, 1_INT8
      read(unit=INPUT_UNIT, fmt='(I3)') coordinate_E
    end do

    ! STEP.02
    ! read out the maximum communicatable distance
    read(unit=INPUT_UNIT, fmt='(I3)') distance_max

    ! STEP.03
    ! judge the distance among the antennas
    if ( coordinate_E - coordinate_A .gt. distance_max ) then
      write(unit=OUTPUT_UNIT, fmt='(A)', advance='yes') string_NOT_communicatable
    else
      write(unit=OUTPUT_UNIT, fmt='(A)', advance='yes') string_communicatable
    end if

    ! STEP.END
    return

  end subroutine ABC123_A

end module ABC123

program main

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

  ! require all variables to be explicitly declared
  implicit none

  call ABC123_A

end program main


integer a,b,c,d,e,k
read*,a,b,c,d,e,k
if(e-a>k)then
print*,":("
else
print*,"Yay!"
endif
end


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

do i=1,5
read(5,*) a(i)
enddo
read(5,*) k

if(a(5)-a(1).gt.k) then
write(6,*) ":("
else
write(6,*) "Yay!"
endif

end


program main
  implicit none
  integer :: a(5),k
  integer :: distance
  integer :: i,j
  do i = 1,5
     read(*,*) a(i)
  end do
  read(*,*) k
  do i = 1,4
     do j=i+1,5
        distance = a(j)-a(i)
        if ( k >= a(i)) then
           cycle
        else
           write(*,'(a)')':('
           stop
        end if
     end do
  end do
  write(*,'(a)')'Yay!'
  stop
contains

end program main


program answer
  implicit none
  integer :: i, A, B, C, D, E, aa, bb, cc, dd, ee, ans
  read(*,*) A
  read(*,*) B
  read(*,*) C
  read(*,*) D
  read(*,*) E
  aa=A-(A/10)*10
  bb=B-(B/10)*10
  cc=C-(C/10)*10
  dd=D-(D/10)*10
  ee=E-(E/10)*10

  ans=(A/10+B/10+C/10+D/10+E/10+5)*10
  if(aa==0) then
     ans=ans-10
  end if
  if(bb==0) then
     ans=ans-10
  end if
  if(cc==0) then
     ans=ans-10
  end if

  if(dd==0) then
     ans=ans-10
  end if
  if(ee==0) then
     ans=ans-10
  end if

  if(min(aa,bb,cc,dd,ee)==aa) then
     ans=ans-(A/10+1)*10+A
  else if(min(aa,bb,cc,dd,ee)==bb) then
     ans=ans-(B/10+1)*10+B
  else if(min(aa,bb,cc,dd,ee)==cc) then
     ans=ans-(C/10+1)*10+C
   else if(min(aa,bb,cc,dd,ee)==dd) then
     ans=ans-(D/10+1)*10+D
  else if(min(aa,bb,cc,dd,ee)==ee) then
     ans=ans-(E/10+1)*10+E
  end if

  if(aa==0.or.bb==0.or.cc==0.or.dd==0.or.ee==0) then
     if(aa==0) then
        aa=10
     end if
     if(bb==0) then
        bb=10
     end if
     if(cc==0) then
        cc=10
     end if
     if(dd==0) then
        dd=10
     end if
     if(ee==0) then
        ee=10
     end if
     ans=ans+min(aa,bb,cc,dd,ee)
  end if

  write(*,*) ans
  stop
 end program answer

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 prob3
    implicit none
    integer :: a(5), ans, i, mi
    read(*,*) a
    ans = 0
    do i = 1, 5
        ans = ans + a(i)
        ans = ((ans - 1) / 10 + 1) * 10
    end do
    mi = 10
    do i = 1, 5
        if(mod(a(i), 10) == 0) then
            cycle
        end if
        mi = min(mod(a(i), 10), mi)
    end do
    ans = ans - (10 - mi)
    write(*,*) ans

    stop
contains
end program prob3


program c20829

implicit none
integer(16) :: a, b, c, d, e, n, a1, b1, c1, d1, e1, a2, b2, c2, d2, e2, z

n = 0

read *, a
read *, b
read *, c
read *, d
read *, e

a1 = mod(a, 10)
b1 = mod(b, 10)
c1 = mod(c, 10)
d1 = mod(d, 10)
e1 = mod(e, 10)

!print *, a1, b1, c1, d1, e1

!stop

z = 10

if(z > a1 .and. a1 .ne. 0) then
 z = a1
end if

if(z > b1 .and. b1 .ne. 0) then
 z = b1
end if

if(z > c1 .and. c1 .ne. 0) then
 z = c1
end if

if(z > d1 .and. d1 .ne. 0) then
 z = d1
end if

if(z > e1 .and. e1 .ne. 0) then
 z = e1
end if

!print *, z

n = n + ceiling(real(a)/10) * 10
n = n + ceiling(real(b)/10) * 10
n = n + ceiling(real(c)/10) * 10
n = n + ceiling(real(d)/10) * 10
n = n + ceiling(real(e)/10) * 10
n = n - 10 + z

print *, n

end program c20829


program bbb
implicit none

integer :: i, m, res
integer, dimension(5) :: meal, one
integer, dimension(1) :: mloc
read*, meal

res=0

do i=1,5
one(i)=meal(i)-(meal(i)/10)*10
if(one(i)==0) one(i)=10
end do

m=minval(one)
mloc=minloc(one)

do i=1, 5
 if(one(i)==10) then
 res=res+meal(i)
 else
 res=res+(meal(i)/10)*10+10
 end if
end do

res=res-10+one(mloc(1))

write(*,'(i0)') res

end program


program fivedishes
  implicit none

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

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


Program ABC123B

integer a(5),b(5),x,y,s,p

read(*,*) a

do i=1,5
 b(i)=mod(a(i),10)
end do

do i=1,5
 do j=1,5
  if (b(i)<b(j)) then
   x=b(i)
   b(i)=b(j)
   b(j)=x
   y=a(i)
   a(i)=a(j)
   a(j)=y
  end if
 end do
end do

s=0
p=1
do while (b(p)==0)
 s=s+a(p)
 p=p+1
end do

s=s+a(p)

do i=p+1,5
 s=s+(a(i)/10+1)*10
end do

write(*,*) s

stop
end


program name
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: n,a,b,c,d,e
    read*, n
    read*, a
    read*, b
    read*, c
    read*, d
    read*, e

    print'(i0)', 5 + n/minval([a,b,c,d,e])
end program name

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 ccc
implicit none

integer(16) :: n, m, res
integer(16), dimension(5) :: move

read*, n
read*, move

m=minval(move)
if(mod(n,m)==0) then
res=n/m+4
else
res=n/m+5
end if

write(*,'(i0)') res

end program


PROGRAM ATCODER

implicit none
integer(16) :: n,t(1:5)

read*, n,t(1:5)

print'(i0)', ceiling(n/dble(minval(t)),16) + 4

contains

END PROGRAM ATCODER


program abc
    integer(8),dimension(5) :: limits
    integer(8) :: est_time, num_person

    read *, num_person
    do i=1,5
        read *, limits(i)
    end do

    est_time = 5 + num_person/MINVAL(limits)
    if (MOD(num_person, MINVAL(limits)) == 0) then
        est_time = est_time-1
    end if

    print '(I0)', est_time
end program abc