module mod_modint
  implicit none
  integer(8) :: modulus = 998244353_8
  type modint
    private
    integer(8) :: num
  contains
    procedure :: get => getnum
  end type
  interface change_modulus
    module procedure :: change_modulus64, change_modulus32
  end interface change_modulus
  interface assignment(=)
    module procedure :: setm, seti64, seti32
  end interface assignment(=)

  interface operator(+)
    module procedure :: posm, addmm, addmi64, addmi32, addi64m, addi32m
  end interface operator(+)

  interface operator(-)
    module procedure :: negm, submm, submi64, submi32, subi64m, subi32m
  end interface operator(-)

  interface operator(*)
    module procedure :: mulmm, mulmi64, mulmi32, muli64m, muli32m
  end interface operator(*)

  interface operator(/)
    module procedure :: divmm, divmi64, divmi32, divi64m, divi32m
  end interface operator(/)

  interface operator(**)
    module procedure :: powmi64, powmi32
  end interface operator(**)

  interface inv
    module procedure :: invm
  end interface inv

contains
  subroutine change_modulus64(newmod)
    integer(8), intent(in) :: newmod
    if (newmod == 0_8) then
      write(*,'(a)') "Error: Invalid value (newmod == 0). (modint, change_modulus64)"
      stop
    end if
    modulus = newmod
  end

  subroutine change_modulus32(newmod)
    integer, intent(in) :: newmod
    if (newmod == 0) then
      write(*,'(a)') "Error: Invalid value (newmod == 0). (modint, change_modulus32)"
      stop
    end if
    modulus = int(newmod,8)
  end
  integer(8) function getnum(this)
    class(modint), intent(in) :: this
    getnum = this%num
  end

  pure elemental type(modint) function newm()
    newm%num = 0_8
  end

  pure elemental subroutine setm(x,y)
    type(modint), intent(inout) :: x
    type(modint), intent(in) :: y
    x%num = y%num
  end

  pure elemental function posm(x) result(n)
    type(modint), intent(in) :: x
    type(modint) :: n
    n%num = x%num
  end

  pure elemental function negm(x) result(n)
    type(modint), intent(in) :: x
    type(modint) :: n
    n%num = modulus-x%num
  end

  pure elemental function addmm(x,y) result(n)
    type(modint), intent(in) :: x, y
    type(modint) :: n
    n%num = x%num+y%num
    if (n%num >= modulus) n%num = n%num-modulus
  end

  pure elemental function submm(x,y) result(n)
    type(modint), intent(in) :: x, y
    type(modint) :: n
    n%num = x%num-y%num
    if (n%num < 0_8) n%num = n%num+modulus
  end

  pure elemental function mulmm(x,y) result(n)
    type(modint), intent(in) :: x, y
    type(modint) :: n
    n%num = mod(x%num*y%num,modulus)
  end

  impure elemental function invm(x) result(n)
    type(modint), intent(in) :: x
    type(modint) :: n
    integer(8) :: a, b, c, q, r, v
    a = x%num
    if (a == 0_8) then
      write(*,'(a)') "Error: Division by zero (x == 0). (modint, invm)"
      stop
    end if
    b = modulus
    c = 0_8
    v = 1_8
    do while (b /= 0_8)
      q = a/b
      r = mod(a,b)
      a = b
      b = r
      r = c
      c = v-c*q
      v = r
    end do
    n%num = mod(v,modulus)
    if (n%num < 0_8) n%num = n%num+modulus
  end

  impure elemental function divmm(x,y) result(n)
    type(modint), intent(in) :: x, y
    type(modint) :: n
    n = mulmm(x,invm(y))
  end

  impure elemental type(modint) function newi(i)
    class(*), intent(in) :: i
    select type(i)
    type is (integer(8))
      newi%num = i
    type is (integer)
      newi%num = int(i,8)
    type is (integer(2))
      newi%num = int(i,8)
    type is (integer(1))
      newi%num = int(i,8)
    class default
      write(*,'(a)') "Error: Invalid value (i is not integer). (modint, newi)"
      stop
    end select
    newi%num = mod(newi%num,modulus)
    if (newi%num < 0_8) newi%num = newi%num+modulus
  end

  impure elemental subroutine seti64(x,i)
    type(modint), intent(inout) :: x
    integer(8), intent(in) :: i
    call setm(x,newi(i))
  end

  impure elemental subroutine seti32(x,i)
    type(modint), intent(inout) :: x
    integer, intent(in) :: i
    call setm(x,newi(i))
  end

  impure elemental function addmi64(x,i) result(n)
    type(modint), intent(in) :: x
    integer(8), intent(in) :: i
    type(modint) :: n
    n = addmm(x,newi(i))
  end

  impure elemental function addmi32(x,i) result(n)
    type(modint), intent(in) :: x
    integer, intent(in) :: i
    type(modint) :: n
    n = addmm(x,newi(i))
  end

  impure elemental function addi64m(i,y) result(n)
    integer(8), intent(in) :: i
    type(modint), intent(in) :: y
    type(modint) :: n
    n = addmm(newi(i),y)
  end

  impure elemental function addi32m(i,y) result(n)
    integer, intent(in) :: i
    type(modint), intent(in) :: y
    type(modint) :: n
    n = addmm(newi(i),y)
  end

  impure elemental function submi64(x,i) result(n)
    type(modint), intent(in) :: x
    integer(8), intent(in) :: i
    type(modint) :: n
    n = submm(x,newi(i))
  end

  impure elemental function submi32(x,i) result(n)
    type(modint), intent(in) :: x
    integer, intent(in) :: i
    type(modint) :: n
    n = submm(x,newi(i))
  end

  impure elemental function subi64m(i,y) result(n)
    integer(8), intent(in) :: i
    type(modint), intent(in) :: y
    type(modint) :: n
    n = submm(newi(i),y)
  end

  impure elemental function subi32m(i,y) result(n)
    integer, intent(in) :: i
    type(modint), intent(in) :: y
    type(modint) :: n
    n = submm(newi(i),y)
  end

  impure elemental function mulmi64(x,i) result(n)
    type(modint), intent(in) :: x
    integer(8), intent(in) :: i
    type(modint) :: n
    n = mulmm(x,newi(i))
  end

  impure elemental function mulmi32(x,i) result(n)
    type(modint), intent(in) :: x
    integer, intent(in) :: i
    type(modint) :: n
    n = mulmm(x,newi(i))
  end

  impure elemental function muli64m(i,y) result(n)
    integer(8), intent(in) :: i
    type(modint), intent(in) :: y
    type(modint) :: n
    n = mulmm(newi(i),y)
  end

  impure elemental function muli32m(i,y) result(n)
    integer, intent(in) :: i
    type(modint), intent(in) :: y
    type(modint) :: n
    n = mulmm(newi(i),y)
  end

  impure elemental function divmi64(x,i) result(n)
    type(modint), intent(in) :: x
    integer(8), intent(in) :: i
    type(modint) :: n
    n = divmm(x,newi(i))
  end

  impure elemental function divmi32(x,i) result(n)
    type(modint), intent(in) :: x
    integer, intent(in) :: i
    type(modint) :: n
    n = divmm(x,newi(i))
  end

  impure elemental function divi64m(i,y) result(n)
    integer(8), intent(in) :: i
    type(modint), intent(in) :: y
    type(modint) :: n
    n = divmm(newi(i),y)
  end

  impure elemental function divi32m(i,y) result(n)
    integer, intent(in) :: i
    type(modint), intent(in) :: y
    type(modint) :: n
    n = divmm(newi(i),y)
  end

  impure elemental function powmi64(x,i) result(n)
    type(modint), intent(in) :: x
    integer(8), intent(in) :: i
    type(modint) :: n, p
    integer(8) :: m
    n = newi(1_8)
    p = x
    m = i
    if (i < 0_8) then
      p = invm(x)
      m = abs(i)
    end if
    do while (m > 0_8)
      if (btest(m,0)) n = mulmm(p,n)
      p = mulmm(p,p)
      m = rshift(m,1)
    end do
  end

  impure elemental function powmi32(x,i) result(n)
    type(modint), intent(in) :: x
    integer, intent(in) :: i
    type(modint) :: n, p
    integer :: m
    n = newi(1_8)
    p = x
    m = i
    if (i < 0) then
      p = invm(x)
      m = abs(i)
    end if
    do while (m > 0)
      if (btest(m,0)) n = mulmm(p,n)
      p = mulmm(p,p)
      m = rshift(m,1)
    end do
  end

end module mod_modint
program main
  use mod_modint
  implicit none
  integer(8)::L,A,B
  integer::M
  type(modint)::mat(3,3),matd(3,3)
  integer(16)::C_d
  integer::i,j
  type(modint)::ANS(3)
  read*,L,A,B,M
  call change_modulus(M)

  ANS(1)=0;ANS(2)=A;ANS(3)=1
  matd=0
  matd(1,1)=1;matd(1,2)=1
  matd(2,2)=1;matd(2,3)=B
  matd(3,3)=1
  do i=1,18
    C_d=cnt(10_16**i)-cnt(10_16**(i-1))
    matd(1,1)=matd(1,1)*10
    mat=Mat_Pow(matd,C_d)
    ANS=mat_vec(mat,ANS)
  end do
  print"(i0)",getnum(ANS(1))
contains
  function cnt(N)
    integer(16)::N
    integer(16)::cnt
    cnt=merge( 0_16,(N - A + B-1)/ B,N<=A)
    cnt=min(cnt,L)
  end function
  function Mat_Pow(Re,N)result(res)
    type(modint),intent(in)::Re(3,3)
    integer(16)::N
    type(modint)::res(3,3),B(3,3)
    integer::i
    res=0
    do i=1,3
      res(i,i)=1
    end do
    B=Re
    do i=1,bit_size(N)
      if(btest(N,i-1))res=mat_mul(res,B)
      B=mat_mul(B,B)
    end do
  end function
  function mat_mul(A,B)result(res)
    type(modint),intent(in)::A(3,3),B(3,3)
    type(modint)::res(3,3)
    integer::i,j,k
    res=0
    do i=1,3
      do j=1,3
        do k=1,3
          res(i,j)=res(i,j)+A(i,k)*B(k,j)
        end do
      end do
    end do
  end function
  function mat_vec(A,B)result(res)
    type(modint),intent(in)::A(3,3),B(3)
    type(modint)::res(3)
    integer::i,j
    res=0
    do i=1,3
      do j=1,3
          res(i)=res(i)+A(i,j)*B(j)
      end do
    end do
  end function
end program main

Score : 600 points

Problem Statement

There is an arithmetic progression with L terms: s_0, s_1, s_2, ... , s_{L-1}.

The initial term is A, and the common difference is B. That is, s_i = A + B \times i holds.

Consider the integer obtained by concatenating the terms written in base ten without leading zeros. For example, the sequence 3, 7, 11, 15, 19 would be concatenated into 37111519. What is the remainder when that integer is divided by M?

Constraints

All values in input are integers.

1 \leq L, A, B < 10^{18}

2 \leq M \leq 10^9

All terms in the arithmetic progression are less than 10^{18}.

Input

Input is given from Standard Input in the following format:

L A B M

Output

Print the remainder when the integer obtained by concatenating the terms is divided by M.

Sample Input 1

5 3 4 10007

Sample Output 1

5563

Our arithmetic progression is 3, 7, 11, 15, 19, so the answer is 37111519 mod 10007, that is, 5563.

Sample Input 2

4 8 1 1000000

Sample Output 2

891011

Sample Input 3

107 10000000000007 1000000000000007 998244353

Sample Output 3

39122908


program AGC034A
integer N,A,B,C,D
character(2*10**5) S
logical over,rock2,openspace
read*,N,A,B,C,D
read*,S
rock2=.false.
openspace=.false.
do i=A,C-1
    if(S(i:i+1)=="##")rock2=.true.
end do
do i=B,D-1
    if(S(i:i+1)=="##")rock2=.true.
end do
if(rock2)then
  print"(A)","No"
  stop
endif
do i=B-1,D-1
    if(S(i:i+2)=="...")openspace=.true.
end do
over=(C>D)
if(over)then
  if(.not.openspace)then
    print"(A)","No"
    stop
  endif
endif
print"(A)","Yes"
end

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 kenken
 implicit none
 integer n, a, b, c, d
 integer i, p, q, r, sum
 character(200001) s
 read*, n, a, b, c, d
 read*, s
 p=0
 q=0
 r=0
 sum=0

 if (c < d) then

 do i=b+1, 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+1, 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
   exit
   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=sum+1, 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
  print*, "Yes"
  return
 end if

 if (sum.eq.0) then
 do i=b+1, 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+1, 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


program kenken
 implicit none
 integer n, a, b, c, d
 integer i, j, 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 j=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 j=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 j=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 j=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 j=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 j=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


program angle
 implicit none
 integer n, m
 read*, n
 m=180*(n-2)
 write(*,*) m
end program

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

	character(15) S
    integer :: i=1,j,wa=0,k

    read(*,*) S

    k=len_trim(adjustl(S))

    do
    	if (i>k) then
        	exit
        end if

    	j=index(S(i:),"o")

    	if(j/=0) then
        	wa=wa+1
            i=i+j
        else
        	i=i+1
        end if
    end do

    if(wa>=8) then
    	write(*,*) "Yes"

	else
    	if((15-k)+wa>=8) then
        	write(*,*) "Yes"
    	else
        	write(*,*) "No"
        end if
    end if
end program example

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


program prob5
    implicit none
    integer :: N
    logical, allocatable :: G(:, :)
    integer :: a, b, i, j
    integer, allocatable :: queue(:), ans(:), c(:)
    integer :: ind, v, last, su
    logical, allocatable :: used(:)
    read(*,*) N

    allocate(G(N, N), c(N), ans(N), used(N))
    do i = 1, N
        do j = 1, N
            G(i, j) = .false.
        end do
    end do
    do i = 1, N-1
        read(*,*) a, b
        G(a, b) = .true.
        G(b, a) = .true.
    end do
    read(*,*) c
    call msort(c)

    allocate(queue(N))
    do i = 1, N
        queue(i) = 0
    end do
    ind = 1
    last = 2
    queue(1) = 1
    ans(1) = c(N)
    do i = 1, N
        used(i) = .false.
    end do
    used(1) = .true.

    do while(last <= N)
        v = queue(ind)
        do i = 1, N
            if(G(v, i) .and. .not. used(i)) then
                queue(last) = i
                last = last + 1
                used(i) = .true.
            end if
        end do
        ind = ind + 1
    end do

    do i = 1, N
        ans(queue(i)) = c(N - i + 1)
    end do
    su = 0
    do i = 1, N-1
        su = su + c(i)
    end do

    write(*,*) su
    write(*,*) ans

    deallocate(G, queue, c, ans, used)
    stop
contains

  !
  ! swap two arguments
  !
subroutine swap(a, b)
    implicit none
    integer, intent(inout) :: a, b

    integer :: c

    c = a
    a = b
    b = c

  end subroutine swap

  !
  ! merge sort
  !
  recursive subroutine msort(x)
    implicit none
    integer, intent(inout) :: x(:)

    integer :: n, mid, i, j, k
    integer, 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

end program prob5

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.


program main
  implicit none
  integer :: a, p
  read (*, *) a, p
  p = p + a * 3
  write (*, "(i0)") p / 2
end program main

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 ATCODER

implicit none
integer(16) :: i,j,a,p

read*, a,p

print'(i0)', (a*3+p)/2

END PROGRAM ATCODER


program bbb
implicit none

integer(16) :: n, i, j, l, dumy
character(10),allocatable,dimension(:) :: s
integer(16),allocatable,dimension(:) :: t, p, q

read*, n
allocate(s(n))
allocate(t(n))
allocate(p(n))
allocate(q(n))
t=0

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

p=-p

do i=1,n
l=len_trim(s(i))
 do j=1,l
 t(i)=t(i)+((iachar(s(i)(j:j))-80))*(10**(2*(11-j)))
 end do
t(i)=t(i)+p(i)
end do

do i=1,n
 do j=i+1,n
 if(t(i)>t(j)) then
 dumy=t(i)
 t(i)=t(j)
 t(j)=dumy
 dumy=q(i)
 q(i)=q(j)
 q(j)=dumy
 end if
 end do
end do

do i=1,n
write(*,'(i0)') q(i)
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 ATCODER

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

read*, n,m
allocate(k(m),s(m,n),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 = 0, max, 1
  check = 0
  l = i
  sw = 0
  do j = 1, n, 1
    if ( l == 0 ) exit
    sw(j) = mod(l,2)
    l = l/2
  end do
  if ( minval(sw) == 0 .and. maxval(sw) == 0 .and. i /= 0 ) exit
  do j = 1, m, 1
    on = 0
    do h = 1, k(m), 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

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,k,a,b,cd,i,j,h,c=1
integer(16),allocatable :: v(:),m(:),ans(:)

read*, n,k
allocate(v(n),m(n),ans(k**2))
read*, v(1:n)

do i = 0, min(n,k), 1
  a = i
  do j = 0, min(n,k)-i, 1
    m = 10**9
    b = j
    if ( a + b >= min(n,k) + 1 ) exit
    cd = k - (a+b)
    if ( a /= 0 ) then
      do h = 1, a, 1
        m(h) = v(h)
      end do
    end if
    if ( b /= 0 ) then
      do h = 1, b, 1
        m(n+1-h) = v(n+1-h)
      end do
    end if
    call hs(n,m)
    do h = 1, cd
      if ( m(h) > 0 ) then
        cd = h - 1
        exit
      end if
    end do
    m(1:cd) = 0
    do h = 1, n, 1
      if ( m(h) == 10**9 ) then
        m(h) = 0
      end if
    end do
    ans(c) = sum(m)
    c = c + 1
  end do
end do

print'(i0)', maxval(ans)

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

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

  if(n <= 0)then
     write(6,*)"Error, at heapsort"; stop
  endif
  if(n == 1)return

  l=n/2+1
  k=n
  do while(k /= 1)
     if(l > 1)then
        l=l-1
        t=array(L)
     else
        t=array(k)
        array(k)=array(1)
        k=k-1
        if(k == 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 hs

END PROGRAM ATCODER

Score : 400 points

Problem Statement

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

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

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

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

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

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

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

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

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

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

Constraints

All values in input are integers.

1 \leq N \leq 50

1 \leq K \leq 100

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

Input

Input is given from Standard Input in the following format:

N K
V_1 V_2 ... V_N

Output

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

Sample Input 1

6 4
-10 8 2 1 2 6

Sample Output 1

14

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

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

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

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

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

Sample Input 2

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

Sample Output 2

44

Sample Input 3

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

Sample Output 3

0

It is optimal to do no operation.


integer N,Q
integer,allocatable,dimension(:)::S,T,X
integer num
integer ans
read*,N,Q
allocate(S(N),T(N),X(N))
do i=1,N
  read*,S(i),T(i),X(i)
end do
do i=1,Q
  read*,num
  ans=huge(ans)
  do j=1,N
    if(S(j)-X(j)<=num.and.num < T(j)-X(j))then
      ans=min(ans,X(j))
    endif
  end do
  if(ans==huge(ans))ans=-1

  print"(i0)",ans
end do
end

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.


integer(16) N,M,K
integer(16) waruno
integer(16) ans,tmp,i,j,com
read*,N,M,K
waruno=10**9+7
com=comb(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*,ans

contains
pure function comb(n,k,waruno)
integer(16),intent(in)::n,k,waruno
integer(16) i,j
integer(16),allocatable,dimension(:,:)::Table
integer(16) comb
allocate(Table(0:n,0:k))
Table=0
Table(:,0)=1
do i=1,N
  do j=1,k
    Table(i,j)=mod(Table(i-1,j-1)+Table(i-1,j),waruno)
  end do
end do
comb=Table(n,k)
end function comb
end

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.


implicit none

integer A,B,money,n

write(6,*) 'A B'
do n=1,10
	read *,A,B
	if(A.gt.0 .and. A.lt.100 .and. B.gt.2 .and. B.lt.1000 .and.mod(B,2)==0) exit
end do

if(A.ge.13)					money=B
if(A.ge.6 .and. A.le.12) 	money=B*0.5
if(A.le.5)					money=0
write(6,'(i4)') money

end


PROGRAM ATCODER

implicit none
integer :: i,a,b

read*, a,b

if ( a >= 13 ) then
  print'(i0)', b
else if ( a <= 5 ) then
  print'(i0)', 0
else
  print'(i0)', b/2
end if

END PROGRAM ATCODER


program piyo
  integer :: r,x2000,d,i

  read*,r,d,x2000

  do i = 1,10

    x2000 = r * x2000 - d
    print*,x2000
  end do
end program

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 name
    implicit none
    integer(8):: r,d,x
    integer(8):: i

    read*, r,d,x

    do i=1,10
        x=r*x-d
        print*, x
    end do
end program name


program main
Implicit None
	integer(8)::r,d,v,i

	read*,r,d,v

	do i =1,10
		v = r*v-d
		print"(i0)",v
	enddo
end program main


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(M), B(M)
   do 200 j=A(M), B(M)
   X(j) = X(j) +1
   200 continue
  100 continue
 do 1000 i=1, M
  if (X(i).eq.M) then
  res = res + 1
  end if
 1000 continue
 write(*,*) 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 sample
    implicit none
    character(100)::s,t
    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)
    call m2sort(z,y)
    a=n
    b=m
    c=n
    ans=0
    d=0
    do
        if(x(a)>=z(b))then
            ans=ans+x(a)
            c=c-1
            a=a-1
            if(c==0)then
                exit
            endif
        else
            if(c<=y(b))then
                ans=ans+z(b)*c
                exit
            else
                ans=ans+z(b)*y(b)
                c=c-y(b)
                b=b-1
                if(b==0)then
                    do i=1,c
                        ans=ans+x(a)
                        a=a+1
                    end do
                    exit
                endif
            end if
        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

    !
    ! 前半と後半に分けてソート
    ! 1~midとmid+1~nをそれぞれソート済みにする
    !
    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 kadai
implicit none

integer  ::  n,m,i,j
integer(16)  ::  ans
integer,allocatable  ::  A(:)
integer,allocatable  ::  B(:)
integer(8),allocatable  ::  C(:)

read(*,*) n,m

allocate(A(n))
allocate(B(m))
allocate(C(m))

read(*,*) A
do i=1,m
    read(*,*) B(i),C(i)
end do

do i=1,m
    do j=1,B(i)
        if(minval(A)==C(i) .or. minval(A)>C(i)) then
            exit
        end if
        if(minval(A)<C(i)) then
            A(minloc(A,minval(A)))=C(i)
        end if
    end do

end do
ans=0
do i=1,n
ans=ans+A(i)
end do
write(*,*) ans

end program kadai


integer(16) N,M,K
integer(16) waruno
integer(16) ans,tmp,i,j,com
read*,N,M,K
waruno=10**9+7
com=comb(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*,ans

contains
pure function comb(n,k,waruno)
integer(16),intent(in)::n,k,waruno
integer(16) i,j
integer(16),allocatable,dimension(:,:)::Table
integer(16) comb
allocate(Table(0:n,0:n))
Table=0
Table(:,0)=1
do i=1,N
  do j=1,i
    Table(i,j)=mod(Table(i-1,j-1)+Table(i-1,j),waruno)
  end do
end do
comb=Table(n,k)
end function comb
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.


module mod_priority_queue
  implicit none
  type t_priority_queue
    private
    integer :: num = 0 !中に入ってる物の数
    integer, pointer :: heap(:) => null()!pqの容量
  contains
    procedure :: offer => offer
    procedure :: clear => clear
    procedure :: poll => poll
    procedure :: peek => peek
    procedure :: size => size_of
  end type t_priority_queue
contains
  subroutine offer(pq,item)
    class(t_priority_queue), intent(inout) :: pq
    integer, intent(in) :: item
    integer :: n, i, t
    integer, allocatable :: tmp(:)
    if (.not.associated(pq%heap)) allocate(pq%heap(1))

    !いっぱいいっぱいのときには拡張している
    if (pq%num == size(pq%heap)) then
      allocate(tmp(pq%num))
      tmp = pq%heap
      deallocate(pq%heap)
      allocate(pq%heap(2*pq%num))
      pq%heap(1:pq%num) = tmp
      deallocate(tmp)
    end if

    pq%num = pq%num+1
    pq%heap(pq%num) = item
    n = pq%num
    do while (n > 1)
      i = n/2
      if (pq%heap(n) > pq%heap(i)) then
        t = pq%heap(n)
        pq%heap(n) = pq%heap(i)
        pq%heap(i) = t
      end if
      n = i
    end do
    return
  end subroutine offer

  subroutine clear(pq)
    class(t_priority_queue), intent(inout) :: pq
    if (associated(pq%heap)) deallocate(pq%heap)
    pq%num = 0
    return
  end subroutine clear

  function poll(pq) result(item)
    class(t_priority_queue), intent(inout) :: pq
    integer :: item, n, i, j, tmp
    n = pq%num
    item = pq%heap(1)
    pq%heap(1) = pq%heap(n)
    pq%num = pq%num-1
    i = 1
    do while (2*i < n)
      j = 2*i
      if (j+1 < n .and. pq%heap(j+1) > pq%heap(j)) j = j+1
      if (pq%heap(j) > pq%heap(i)) then
        tmp = pq%heap(j)
        pq%heap(j) = pq%heap(i)
        pq%heap(i) = tmp
      end if
      i = j
    end do
  end function poll
  subroutine pop(pq)
    class(t_priority_queue), intent(inout) :: pq
    integer :: item, n, i, j, tmp
    n = pq%num
    item = pq%heap(1)
    pq%heap(1) = pq%heap(n)
    pq%num = pq%num-1
    i = 1
    do while (2*i < n)
      j = 2*i
      if (j+1 < n .and. pq%heap(j+1) > pq%heap(j)) j = j+1
      if (pq%heap(j) > pq%heap(i)) then
        tmp = pq%heap(j)
        pq%heap(j) = pq%heap(i)
        pq%heap(i) = tmp
      end if
      i = j
    end do
  end subroutine

  function peek(pq) result(item)
    class(t_priority_queue), intent(inout) :: pq
    integer :: item
    item = pq%heap(1)
  end function peek

  integer function size_of(pq)
    class(t_priority_queue), intent(in) :: pq
    size_of = pq%num
  end
end module mod_priority_queue

program am
use mod_priority_queue
implicit none
integer::Q
integer::T,A(2)
integer(16)::ANS

type(t_priority_queue)::L,R
integer::i

read*,Q
ANS=0
call offer(L,-huge(i))
call offer(R,-huge(i))
do i=1,Q
    read*,T,A(2*T-1:2)
    select case(T)
    case(1)
        ANS=ANS+A(2)
        if(A(1)<peek(L))then
            ANS=ANS+peek(L)-A(1)
        else if(-peek(R)<A(1))then
            ANS=ANS+A(1)+peek(R)
        endif
        call offer(l,A(1))
        call offer(r,-A(1))
        if(peek(L)>-peek(R))then
            call offer(l,-peek(R))
            call offer(r,-peek(l))
            call pop(l)
            call pop(r)
        endif
    case(2)
        print"(i0,a,i0)",peek(l)," ",ANS
    end select
end do
end program am

Score : 600 points

Problem Statement

There is a function f(x), which is initially a constant function f(x) = 0.

We will ask you to process Q queries in order. There are two kinds of queries, update queries and evaluation queries, as follows:

An update query 1 a b: Given two integers a and b, let g(x) = f(x) + |x - a| + b and replace f(x) with g(x).

An evaluation query 2: Print x that minimizes f(x), and the minimum value of f(x). If there are multiple such values of x, choose the minimum such value.

We can show that the values to be output in an evaluation query are always integers, so we ask you to print those values as integers without decimal points.

Constraints

All values in input are integers.

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

-10^9 \leq a, b \leq 10^9

The first query is an update query.

Input

Input is given from Standard Input in the following format:

Q
Query_1
:
Query_Q

See Sample Input 1 for an example.

Output

For each evaluation query, print a line containing the response, in the order in which the queries are given.

The response to each evaluation query should be the minimum value of x that minimizes f(x), and the minimum value of f(x), in this order, with space in between.

Sample Input 1

4
1 4 2
2
1 1 -8
2

Sample Output 1

4 2
1 -3

In the first evaluation query, f(x) = |x - 4| + 2, which attains the minimum value of 2 at x = 4.

In the second evaluation query, f(x) = |x - 1| + |x - 4| - 6, which attains the minimum value of -3 when 1 \leq x \leq 4. Among the multiple values of x that minimize f(x), we ask you to print the minimum, that is, 1.

Sample Input 2

4
1 -1000000000 1000000000
1 -1000000000 1000000000
1 -1000000000 1000000000
2

Sample Output 2

-1000000000 3000000000


program yymm_or_mmyy
  implicit none
  character(4) :: s
  logical :: mmyy, yymm
  read(*,'(a)') s
  mmyy = "01" <= s(1:2) .and. s(1:2) <= "12"
  yymm = "01" <= s(3:4) .and. s(3:4) <= "12"
  if (yymm .and. mmyy) then
    write(*,'(a)') "AMBIGUOUS"
  else if (yymm) then
    write(*,'(a)') "YYMM"
  else if (mmyy) then
    write(*,'(a)') "MMYY"
  else
    write(*,'(a)') "NA"
  end if
end program yymm_or_mmyy

Score : 200 points

Problem Statement

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

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

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

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

Constraints

S is a digit sequence of length 4.

Input

Input is given from Standard Input in the following format:

S

Output

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

Sample Input 1

1905

Sample Output 1

YYMM

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

Sample Input 2

0112

Sample Output 2

AMBIGUOUS

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

Sample Input 3

1700

Sample Output 3

NA

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


      PROGRAM YYMMorMMYY
      IMPLICIT NONE
      integer :: ant,post

      read'(i2i2)',ant,post

      if( 1<=ant.and.ant<=12 )then
        if( 1<=post.and.post<=12 )then
          print*,'AMBIGUOUS'
        else
          print*,'MMYY'
        end if
      else
        if( 1<=post.and.post<=12 )then
          print*,'YYMM'
        else
          print*,'NA'
        end if
      end if

      END PROGRAM


program cc
  implicit none
  integer :: n, k, i,point,sum
  integer,allocatable :: div(:)
  double precision :: prob, ans
  read *, n, k
  allocate(div(n))
  ans = 0
  div = 0
  point = 1
  prob = 1
  do i = 1, n
    point = i
    prob = 1
    do while (point < k)
      point = point * 2
      div(i) = div(i) + 1
    end do
  end do
  sum = 0
  do i = 1, n
    sum = sum + 2**(div(1)-div(i))
  end do
  ans = dble(sum)/(2.0**dble(div(1))*dble(n))
  print '(f15.13)', ans
  stop
end program cc

Score : 300 points

Problem Statement

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

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

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

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

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

Constraints

1 ≤ N ≤ 10^5

1 ≤ K ≤ 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

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

Sample Input 1

3 10

Sample Output 1

0.145833333333

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

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

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

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

Sample Input 2

100000 5

Sample Output 2

0.999973749998


module UF
	implicit none
	private
	type Union_Find
		integer, allocatable :: par(:), rank(:), counter(:)
	end type Union_Find
	public Union_Find, make_Union_Find, unite, UF_size, root,  same

	contains

	subroutine make_Union_Find(UFty, N)
		integer, intent(in)           :: N
		type(Union_Find), intent(out) :: UFty
		allocate(UFty%par(1:N), UFty%rank(1:N), UFty%counter(1:N))
		call format_Union_Find(UFty, N)
	end subroutine make_Union_Find

	subroutine format_Union_Find(UFty, N)
		integer, intent(in)             :: N
		type(Union_Find), intent(inout) :: UFty
		integer :: i
		do i = 1, N
			UFty%par(i) = i
		end do
		UFty%rank    = 0
		UFty%counter = 1
	end subroutine format_Union_Find

	subroutine unite(UFty, x, y)
		integer, value                  :: x, y
		type(Union_Find), intent(inout) :: UFty
		x = root(UFty, x)
		y = root(UFty, y)
		if(x == y) return
		if(UFty%rank(x) < UFty%rank(y)) then
			UFty%par(x)     = y
			UFty%counter(y) = UFty%counter(y) + UFty%counter(x)
		else
			UFty%par(y)     = x
			UFty%counter(x) = UFty%counter(x) + UFty%counter(y)
			if(UFty%rank(x) == UFty%rank(y)) UFty%rank(x) = UFty%rank(x) + 1
		end if
	end subroutine unite

	function UF_size(UFty, x) result(y)
		integer, intent(in)             :: x
		type(Union_Find), intent(inout) :: UFty
		integer :: y
		y = UFty%counter(root(UFty, x))
		return
	end function UF_size

	recursive function root(UFty, x) result(root_ans)
		integer, intent(in)             :: x
		type(Union_Find), intent(inout) :: UFty
		integer :: root_ans
		if(UFty%par(x) == x) then
			root_ans = x
			return
		else
			UFty%par(x) = root(UFty, UFty%par(x))
			root_ans    = UFty%par(x)
			return
		end if
	end function root

	logical function same(UFty, x, y)
		integer, intent(in) :: x, y
		type(Union_Find), intent(inout) :: UFty
		if(root(UFty, x) == root(UFty, y)) then
			same = .TRUE.
		else
			same = .FALSE.
		end if
	end function same
end module UF

program main
	use UF
	implicit none
	type(Union_Find) :: X
	integer :: N, i
	integer, allocatable :: u(:), v(:), w(:), ans(:)
	read(*, *) N
	allocate(u(1:N-1), v(1:N-1), w(1:N-1), ans(1:N-1))
	do i = 1, N-1
		read(*, *) u(i), v(i), w(i)
	end  do
	ans = -1

	call make_Union_Find(X, N)
	do i = 1, N-1
		if(mod(w(i),2) == 0) then
			call unite(X, u(i), v(i))
		end if
	end do
	do i = 1, N-1
		if(mod(w(i),2) == 1) then
			if(ans(root(X, u(i))) == 0) then
				ans(root(X, v(i))) = 1
			else if(ans(root(X, u(i))) == 1) then
				ans(root(X, v(i))) = 0
			else if(ans(root(X, v(i))) == 0) then
				ans(root(X, u(i))) = 1
			else if(ans(root(X, v(i))) == 1) then
				ans(root(X, u(i))) = 0
			else
				ans(root(X, u(i))) = 1
				ans(root(X, v(i))) = 0
			end if
		end if
	end do
	do i = 1, N
		write(*, *) abs(ans(root(X, i)))
	end do
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


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


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 diverta2019c
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: n,l
    character(10),allocatable:: s(:)
    integer(int64):: i,j, ca, cb, cab,ans

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

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

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

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

end program diverta2019c

Score : 400 points

Problem Statement

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

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

Constraints

1 \leq N \leq 10^{4}

2 \leq |s_i| \leq 10

s_i consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

N
s_1
\vdots
s_N

Output

Print the answer.

Sample Input 1

3
ABCA
XBAZ
BAD

Sample Output 1

2

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

Sample Input 2

9
BEWPVCRWH
ZZNQYIJX
BAVREA
PA
HJMYITEOX
BCJHMRMNK
BP
QVFABZ
PRGKSPUNA

Sample Output 2

4

Sample Input 3

7
RABYBBE
JOZ
BMHQUVA
BPA
ISU
MCMABAOBHZ
SZMEHMA

Sample Output 3

4


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

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

    do i=1,n
        if (index(s(i),'AB') /= 0) ans=ans+1
    end do

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

        if (s(i)(1:1) == 'B' .and. s(i)(l:l) == 'A') then
            cab=cab+1
        else if (s(i)(1:1) == 'B') then
            cb=cb+1
        else
            ca=ca+1
        end if
    end do

    if (ca == 0 .and. cb==0) then
        print'(i0)', ans+cab-1
    else
        print'(i0)', ans+min(ca+cab,cb+cab)
    end if

end program diverta2019c


PROGRAM ATCODER

implicit none
integer(16) :: n,i,j,count=0,count_a=0,count_b=0,count_ab=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
  if ( s(i)(1:1) == 'B' .and. s(i)(len_trim(s(i)):len_trim(s(i))) == 'A' ) then
    count_ab = count_ab + 1
  end if
end do

count_a = count_a - count_ab
count_b = count_b - count_ab

if ( count_a == 0 .and. count_b == 0 ) then
  print'(i0)',  count + count_ab - 1
else
  print'(i0)',  count + min(count_a,count_b) + count_ab
end if

END PROGRAM ATCODER


program diverta2019_d
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: n,i,ans=0

    read*, n
    i=1
    do while(i*i <= n)
        if (mod(n,i) == 0) then
            if (i*(i+1) < n) ans=ans+(n/i-1)
        end if
        i=i+1
    end do
    print'(i0)', ans
end program diverta2019_d

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 divertaD
    integer(8) i, N, sum
    read(*, *) N
    sum = 0
    DO 10 i = 1, N
     if (mod(N,i).NE.0) then
      if (((N-mod(N,i))/i).EQ.(mod(N,i))) then
      sum = sum + i
      end if
     end if
    10 continue
    print '(i0)', sum
end program


program biscuit
	implicit none
    integer :: a, b, t, n
    read(*,*) a, b, t
    n = t/a
    n = n*b
    write(*,*) n
    stop

end program biscuit

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


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


program main
 implicit none

 integer :: i,c(50),v(50),k,s = 0,n

 read(*,*)n
 read(*,*)(v(i),i=1,n)
 read(*,*)(c(i),i=1,n)

 do i = 1, n
   k = v(i) - c(i)
   if  (k > 0) then
     s = s + k
   end if
 end do

 write(*,*)s
end program main

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 ATCODER

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

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

do i = 1, n, 1
  if ( v(i)-c(i) > 0 ) then
    ans = ans + v(i) - c(i)
  end if
end do

print'(i0)', ans

END PROGRAM ATCODER


program main
implicit none

integer :: n,i,ans
integer, allocatable :: a(:)
read(*,*)n
allocate(a(n))
read(*,*)a(1:n)

do i=1,n-1
	if (-(a(i) + a(i+1)) > (a(i) + a(i+1))) then
    	a(i) = -a(i)
        a(i+1) = -a(i+1)
    end if
end do
ans = sum(a)
write(*,*)ans

end program

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 flipping_signs
  implicit none
  integer :: n, i, k
  integer(8) :: a(100000), m
  a = 0_8
  read(*,*) n
  read(*,*) a(1:n)
  m = 10000000000_8
  k = 0
  do i = 1, n
    if (m.gt.abs(a(i))) m = abs(a(i))
    if (a(i).lt.0_8) k = k + 1
  end do
  if (mod(k,2).eq.0) then
    m = sum(abs(a(1:n)))
  else
    m = sum(abs(a(1:n))) - m
  end if
  write(*,'(i0)') m
  stop
end program flipping_signs


module mod_modulo_util
  implicit none
  integer(8), parameter :: md = 998244353_8
  integer(8), allocatable :: f(:), invf(:)
contains
  subroutine init(n)
    integer, intent(in) :: n
    integer :: i
    if (allocated(f)) deallocate(f)
    if (allocated(invf)) deallocate(invf)
    allocate(f(0:n),invf(0:n))
    f = 0_8
    invf = 0_8
    f(0) = 1_8
    do i = 1, n
      f(i) = mod(f(i-1)*int(i,8),md)
    end do
    invf(n) = inv(f(n))
    do i = n, 1, -1
      invf(i-1) = mod(invf(i)*int(i,8),md)
    end do
  end
  integer(8) function perm(n,k)
    integer, intent(in) :: n, k
    perm = 0_8
    if (k > n .or. n < 0 .or. k < 0) return
    perm = mod(f(n)*invf(n-k),md)
  end
  integer(8) function comb(n,k)
    integer, intent(in) :: n, k
    comb = mod(perm(n,k)*invf(k),md)
  end
  integer(8) function homo(n,k)
    integer, intent(in) :: n, k
    homo = comb(n+k-1,k)
  end
  function inv(n) result(y)
    integer(8), intent(in) :: n
    integer(8) :: a, b, x, y, t, q
    a = mod(n,md)
    b = md
    x = 0_8
    y = 1_8
    do while (b /= 0_8)
      q = a/b
      t = b
      b = mod(a,b)
      a = t
      t = y
      y = x
      x = t-q*x
    end do
    y = mod(mod(y,md)+md,md)
  end
  function pow(a,b) result(r)
    integer(8), intent(in) :: a
    integer, intent(in) :: b
    integer :: i
    integer(8) :: r, p
    r = 1_8
    p = a
    do i = 0, 32
      if (btest(b,i)) r = mod(r*p,md)
      p = mod(p*p,md)
    end do
  end
  integer(8) function fact(n)
    integer, intent(in) :: n
    fact = 0_8
    if (n < 0) return
    fact = f(n)
  end
end module mod_modulo_util
program banned_x
  use mod_modulo_util
  implicit none
  integer :: n, x, i, j, k, t, w
  integer(8) :: ans = 0_8, tmp
  read(*,*) n, x
  call init(3*n)
  do i = 0, n
    tmp = 0_8
    do j = 0, min(x-1,i)
      if (2*j < x-1) cycle
      k = i-j
      if (i < k) cycle
      t = j-k
      if (t >= 0) then
        w = x-1-j-k
        tmp = mod(tmp+comb(t,w),md)
      else if (2*j == x-1) then
        tmp = mod(tmp+1_8,md)
      end if
    end do
    do j = 0, x-2
      tmp = mod(tmp+comb(i,j-i),md)
    end do
    ans = mod(ans+mod(tmp*comb(n,i),md),md)
  end do
  write(*,'(i0)') ans
end program banned_x

Score : 800 points

Problem Statement

Find the number, modulo 998244353, of sequences of length N consisting of 0, 1 and 2 such that none of their contiguous subsequences totals to X.

Constraints

1 \leq N \leq 3000

1 \leq X \leq 2N

N and X are integers.

Input

Input is given from Standard Input in the following format:

N X

Output

Print the number, modulo 998244353, of sequences that satisfy the condition.

Sample Input 1

3 3

Sample Output 1

14

14 sequences satisfy the condition: (0,0,0),(0,0,1),(0,0,2),(0,1,0),(0,1,1),(0,2,0),(0,2,2),(1,0,0),(1,0,1),(1,1,0),(2,0,0),(2,0,2),(2,2,0) and (2,2,2).

Sample Input 2

8 6

Sample Output 2

1179

Sample Input 3

10 1

Sample Output 3

1024

Sample Input 4

9 13

Sample Output 4

18402

Sample Input 5

314 159

Sample Output 5

459765451


program sample
	implicit none
    integer :: i,j,k,l,m
    integer(8) :: x,y,z
    real(8) :: a(4),b(4)

    read(*,*) x,y,z

    if (x<=z .and. z<=y) then
    	write(*,*) 'Yes'
    else if (z<=x .and. y<=z) then
    	write(*,*) 'Yes'
    else
    	write(*,*) 'No'
    end if
    stop
end program sample

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 ee
    implicit none
    integer :: n
    character(10) :: s
    integer :: k
    integer :: i
    character :: m
    read(*,*)n
    read(*,*)s
    read(*,*)k
    m = s(k:k)
    do i = 1,n
        !TODO_statement
        if ( m /= s(i:i) ) then
            s(i:i) = '*'
        end if
    end do
    write(*,*)s
end program ee

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 tenka1_2019_c
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: n,i,cl,cr,ans
    character(:),allocatable:: s

    read*, n
    allocate(character(n):: s)
    read*, s(:)
    cl = 0
    cr = 0
    do i=1,n
        if (s(i:i) == '.') cr=cr+1
    end do

    ans = cl+cr

    do i=1,n
        if (s(i:i) == '.') then
            cr=cr-1
        else
            cl=cl+1
        end if
        ans = min(ans,cl+cr)
    end do

    print'(i0)', ans
end program tenka1_2019_c

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


implicit none
integer n,k,i,slen
integer :: cnt=0,black=0,white=0
character(200000) s
logical :: cnc=.false.

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

slen=len_trim(s)

do i=1,slen

if(s(i:i).eq."#") then
black=black+1
else if(s(i:i).eq.".") then
white=white+1
endif

if(s(i:i+1) .eq. "#.") then
cnc=.true.
else if (s(i:i+1) .eq. ".#" .and.  cnc) then
cnt=cnt+min(black,white)
white=0
black=0
cnc=.false.
endif

enddo

if(s(slen-1:slen).eq."#.") then
cnt=cnt+1
endif

write(6,*) cnt

end


program main
  implicit none
  integer ::le,i,j,a=0,b=0,am,bm
  character(300000)::n
  read(*,*)j
  read(*,*)n
  le=len_trim(n)
  do i=1,le
    if(n(i:i)=='.')a=a+1
    if(n(i:i)=='#')b=b+1
  enddo
  am=a
  bm=b
  do j=1,le
    if(n(j:j)=='.')then
      a=a-1
      if(am>a)am=a
    else
      a=a+1
    endif
  enddo
  do j=le,1,-1
      if(n(j:j)=='#')then
      b=b-1
      if(bm>b)bm=b
    else
      b=b+1
    endif

  enddo
  write(*,*)min(am,bm)
end program main


PROGRAM ATCODER

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

read*, n,s

do i = 1, n-1
  if ( s(i:i+1) == '#.' ) then
    ans = ans + 1
  end if
end do

print'(i0)', ans

END PROGRAM ATCODER


program answer
  implicit none

  integer :: N, i, count, sum
  character(len=100000) :: S, p, q

  read(*,*) S

  N = len_trim(S)
  count = 0

  do i = 1, N, 2
     p(i:i) = '0'
     q(i:i) = '1'
  end do

  do i = 2, N, 2
     p(i:i) = '1'
     q(i:i) = '0'
  end do
 do i = 1, N
     if(S(i:i)/=p(i:i)) then
        count = count + 1
     end if
  end do

  sum = 0
  do i = 1, N
     if(S(i:i)/=q(i:i)) then
        sum = sum + 1
     end if
  end do

  if(count>=sum) then
     write(*,*) sum
  else
     write(*,*) count
  end if

  stop
 end program answer

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


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_B

  ! constants for this <module>
  integer(kind=INT8), parameter, private :: num_mountains_max = 100_INT8

  ! variables for this <module>
  integer(kind=INT8) :: num_mountains_all
  integer(kind=INT8) :: num_mountains_visible
  integer(kind=INT8) :: height_mountain(1:num_mountains_max)
  logical            :: is_visible

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

  subroutine task_B

    ! variables for this <subroutine>
    integer(kind=INT8) :: itr_Mt, itr_ToWest

    ! STEP.01
    ! read out the number of the mountains
    read(unit=INPUT_UNIT, fmt=*) num_mountains_all

    ! STEP.02
    ! read out the heights of the mountains
    read(unit=INPUT_UNIT, fmt=*) height_mountain(1:num_mountains_all)

    ! STEP.03
    ! count up the number of mountains which you can see the sea from the top of the mountain
    num_mountains_visible = 0_INT8

    do itr_Mt = 1_INT8, num_mountains_all, 1_INT8

      is_visible = .true.

      do itr_ToWest = itr_Mt-1_INT8, 1_INT8, -1_INT8
        if (height_mountain(itr_ToWest) .gt. height_mountain(itr_Mt)) then
          is_visible = .false.
          exit
        end if
      end do

      if (is_visible) num_mountains_visible = num_mountains_visible + 1_INT8

    end do

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

    ! STEP.END
    return

  end subroutine task_B

end module ABC124

program main

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

  ! require all variables to be explicitly declared
  implicit none

  call task_B

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 ATCODER

implicit none
integer :: n,count=0,i
integer,allocatable :: h(:)

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

do i = 1, n, 1
  if ( h(i) >= maxval(h(1:i)) ) then
    count = count + 1
  end if
end do

print'(i0)', count

END PROGRAM ATCODER


program main
  implicit none

  character(len=100000) :: s, s1 , s2
  integer :: n, i, ans1, ans2

  read(*,*) s
  s1 = s
  s2 = s
  n = len_trim(s)

  ans1 = 0
  ans2 = 0
  do i = 2, n
     if(s1(i:i) == s1(i-1:i-1)) then
        select case(s1(i:i))
        case('0')
           s1(i:i) = '1'
        case('1')
           s1(i:i) = '0'
        end select
        ans1 = ans1 + 1
     end if

  end do

  do i = 1, n-1
     if(s2(n-i:n-i) == s2(n-i+1:n-i+1)) then
        select case(s2(n-i:n-i))
        case('0')
           s2(n-i:n-i) = '1'
        case('1')
           s2(n-i:n-i) = '0'
        end select
        ans2 = ans2 + 1
     end if
  end do

  write(*,*) min(ans1, ans2)

end program main

Score : 300 points

Problem Statement

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

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

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

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

Constraints

1 \leq |S| \leq 10^5

S_i is 0 or 1.

Input

Input is given from Standard Input in the following format:

S

Output

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

Sample Input 1

000

Sample Output 1

1

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

Sample Input 2

10010010

Sample Output 2

3

Sample Input 3

0

Sample Output 3

0


program main
	implicit none
    character(10**5)::s
    integer(8)::i,len1,ans
    read(*,*) s
    len1=len_trim(s)
    ans=0
    if (s(1:1)=='1')then

    	do i=1,len1
        	if(mod(i,2)==0)then
            	if(s(i:i)/='0')then
                	ans=ans+1
                end if
            else
            	if(s(i:i)/='1')then
                	ans=ans+1
                end if
            end if
        end do
    else
    	do i=1,len1
        	if(mod(i,2)==0)then
            	if(s(i:i)/='1')then
                	ans=ans+1
                end if
            else
            	if(s(i:i)/='0')then
                	ans=ans+1
                end if
            end if
        end do
    end if
    write(*,*) ans
    stop
end program main


program name
    implicit none
    character(100000):: s, f
    integer(4):: len_s,i, diff

    read*, s
    len_s = len_trim(s)

    f(1:1) = '0'
    do i=2,len_s
        f(i:i) = char(ichar('0')+ichar('1') - ichar(f(i-1:i-1)))
    end do

    diff = 0
    do i=1,len_s
        if (s(i:i) == f(i:i)) diff=diff+1
    end do

    print*, min(diff, len_s-diff)
end program name


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 = 10000_INT32

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

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

  subroutine task_C

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

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

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

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

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

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

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

    ! STEP.END
    return

  end subroutine task_C

end module ABC124

program main

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

  ! require all variables to be explicitly declared
  implicit none

  call task_C

end program main


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

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

1 format(':(')
2 format('Yay!')

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

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

  if (e - a > k) then
    write(*,1)
  else
    write(*,2)
  end if

end program main

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 main
  implicit none
  integer :: i, k, ant(5)

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

  if (maxval(ant) - minval(ant) > k) then
    print '(a)', ':('
  else
    print '(a)', 'Yay!'
  end if
end program


program fivedishes
  implicit none

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

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
      else
         if(count<m)then
            m=count
         end if
   end if
end do

 print*,m

end program fivedishes

Score: 200 points

Problem Statement

The restaurant AtCoder serves the following five dishes:

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

ARC Curry: takes B minutes to serve.

AGC Pasta: takes C minutes to serve.

APC Ramen: takes D minutes to serve.

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

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

This restaurant has the following rules on orders:

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

Only one dish can be ordered at a time.

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

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

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

Constraints

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

Input

Input is given from Standard Input in the following format:

A
B
C
D
E

Output

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

Sample Input 1

29
20
7
35
120

Sample Output 1

215

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

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

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

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

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

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

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

Sample Input 2

101
86
119
108
57

Sample Output 2

481

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

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

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

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

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

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

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

Sample Input 3

123
123
123
123
123

Sample Output 3

643

This is the largest valid case.


program main
  implicit none

1 format(i0)

  integer dish(1:5),tmp1,tmp2,tmp3, i, j,k

  read(*,*) dish(1)
  read(*,*) dish(2)
  read(*,*) dish(3)
  read(*,*) dish(4)
  read(*,*) dish(5)

  tmp1 = 10
  k = 0
  do i=1,5
     tmp2 = mod(dish(i),10)
     if (tmp2 /= 0 .and. tmp2 < tmp1) then
        tmp1 = tmp2
        k = i
     end if
  end do

  tmp2 = 0
  if (tmp1 == 10) then
     do i=1,5
        tmp2 = tmp2 + dish(i)
     end do
  else
     do i=1,5
        if (i==k) cycle
        if (mod(dish(i),10) == 0) then
           tmp2 = tmp2 + dish(i)
        else
           tmp2 = tmp2 + (dish(i) / 10 + 1) * 10
        end if
     end do
     tmp2 = tmp2 + dish(k)
  end if

  write(*,1) tmp2

end program main


implicit none
integer,dimension(5)::a
integer i
integer:: min1=10
integer:: sum=0

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

if(mod(a(i),10).eq.0) then
sum=sum+a(i)
else
sum=sum+(a(i)/10+1)*10
if(mod(a(i),10).lt.min1) then
min1=mod(a(i),10)
endif
endif

enddo

if(min1.eq.10) then
write(6,*)sum
else
write(6,*)sum-10+min1
endif

end


program main
  implicit none

1 format(i0)

  integer*8 N,v(1:5),tmp2,tmp3,i
  integer(8) tmp1

  read(*,*) N
  read(*,*) v(1)
  read(*,*) v(2)
  read(*,*) v(3)
  read(*,*) v(4)
  read(*,*) v(5)

  tmp1 = min(v(1),v(2),v(3),v(4),v(5))

  tmp2 = N / tmp1
  tmp3 = mod(N,tmp1)

  if (tmp3 == 0) then
     write(*,1) (tmp2+4)
  else
     write(*,1) (tmp2+5)
  end if
end program main

Score: 300 points

Problem Statement

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

There are five means of transport in this empire:

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

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

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

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

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

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

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

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

Constraints

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

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A
B
C
D
E

Output

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

Sample Input 1

5
3
2
4
3
5

Sample Output 1

7

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

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

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

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

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

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

Sample Input 2

10
123
123
123
123
123

Sample Output 2

5

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

Sample Input 3

10000000007
2
3
5
7
11

Sample Output 3

5000000008

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


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