program test
implicit none
integer(8) :: i=0,j=0,n=0,k=0,t(100000)=0,d(100000)=0,dummy(100000)=0,num=0
integer(8) :: tt(100000)=0,dd(100000)=0,lost(100000)=0,lost_cum(100000)=0
integer(8) :: get(100000)=0,get_cum(100000)=0,total(100000)=0,sumi_a(100000)=0,sumi_b(100000)=0
integer(8) :: kind_max_a(100000)=0,kind_max_b(100000)=0,kind_count=0,lost_count=0,get_count=0,count=0,error

read(*,*) n,k
do i=1,n
	read(*,*) t(i),d(i)
enddo

if(n==k) then
	goto 100
else
	!まずは降順に並べる
	call heapsort_down(n,d(1:n),t(1:n))

	!1～k番目が暫定食べる、k+1～n番目が食べない
	tt(1:n) = t(1:n)
	dd(1:n) = d(1:n)

	!食べるグループで、各種類の最高値を計算
	do i=1,k
		kind_max_a(tt(i)) = max(kind_max_a(tt(i)),dd(i))
	enddo

	!各種類の最高値を1個だけ残して、それ以外は新たな種類の寿司に置換される可能性がある
	lost_count = 0
	do i=1,k
		if(dd(i) == kind_max_a(tt(i)) .and. sumi_a(tt(i)) == 0 ) then
			sumi_a(tt(i)) = 1
		else
			lost_count=lost_count+1
			lost(lost_count) = dd(i)
		endif
	enddo

	if(lost_count == 0)then
		goto 200
	endif

	!昇順ソートをかける
	call heapsort_up(lost_count,lost(1:lost_count),dummy(1:lost_count))

	!失う価値の累積和
	do i=1,lost_count
		if(i == 1) then
			lost_cum(i) = lost(1)
		else
			lost_cum(i) = lost_cum(i-1) + lost(i)
		endif
	enddo

	!食べるグループの種類は何種類？
	call heapsort_up(k,tt(1:k),dd(1:k))
	kind_count = 1
	if(k>=2)then
		do i =2,k
			if(tt(i) .ne. tt(i-1))then
			 kind_count = kind_count + 1
			endif
		enddo
	endif

	!食べないグループで、各種類の最高値を列挙
	do i=k+1,n
		kind_max_b(tt(i)) = max(kind_max_b(tt(i)),dd(i))
	enddo

	!食べないグループで、各種類の最高値にあたり、かつ食べるグループにない寿司が置換に使われる可能性がある
	get_count = 0
	do i=k+1,n
		if((dd(i) == kind_max_b(tt(i))) .and. kind_max_a(tt(i)) == 0 .and. sumi_b(tt(i)) == 0) then
			get_count = get_count + 1
			get(get_count) = dd(i)
			sumi_b(tt(i)) = 1
		endif
	enddo

	if(get_count == 0)then
		goto 300
	endif

	!降順ソートをかける
	call heapsort_down(get_count,get(1:get_count),dummy(1:get_count))

	!得る価値
	do i=1,get_count
		if(i == 1) then
			get_cum(i) = get(1) + 2*(kind_count+i)-1
		else
			get_cum(i) = get_cum(i-1) + get(i) +  2*(kind_count+i)-1
		endif
	enddo

	count = min(lost_count,get_count)

	do i = 1,count
		total(i) = get_cum(i) - lost_cum(i)
	enddo

	write(*,*) sum(d(1:k))+ kind_count*kind_count+ max(0,maxval(total(1:count)))
endif

if(1 == 0)then
100 continue
	call heapsort_up(n,t(1:k),d(1:k))
	num = 1
	if(k>=2)then
		do i =2,k
			if(t(i) .ne. t(i-1))then
			 num = num + 1
			endif
		enddo
	endif
	write(*,*) sum(d(1:k)) + num*num
endif

if(1 == 0)then
200 continue
	call heapsort_up(n,t(1:k),d(1:k))
	num = 1
	if(k>=2)then
		do i =2,k
			if(t(i) .ne. t(i-1))then
			 num = num + 1
			endif
		enddo
	endif
	write(*,*) sum(d(1:k)) + num*num
endif

if(1 == 0)then
300 continue
	call heapsort_up(n,t(1:k),d(1:k))
	num = 1
	if(k>=2)then
		do i =2,k
			if(t(i) .ne. t(i-1))then
			 num = num + 1
			endif
		enddo
	endif
	write(*,*) sum(d(1:k)) + num*num + d(1000000) !ERROR
endif

contains

subroutine heapsort_down(n,y1,y2)

	implicit none
	integer(8),intent(in) :: n
	integer(8),intent(inout) :: y1(1:n)
	integer(8),intent(inout) :: y2(1:n)
	integer(8) :: i,k,j,l
	integer(8) :: t1,t2

	l = n/2+1
	k = n

	do while (k /= 1)
		 if(l > 1) then
				l = l-1
				t1 = y1(l)
				t2 = y2(l)

		 else
				t1 = y1(k)
				t2 = y2(k)
				y1(k) = y1(1)
				y2(k) = y2(1)
				k = k-1
				if(k == 1) then
					 y1(1) = t1
					 y2(1) = t2
					 exit
				endif
		 endif

		 i = l
		 j = l+l

		 do while(j <= k)
				if(j < k) then
					 if(y1(j) > y1(j+1)) j = j+1
				endif

				if (t1 > y1(j)) then
					 y1(i) = y1(j)
					 y2(i) = y2(j)
					 i = j
					 j = j+j
				else
					 j = k+1
				endif

		 enddo

		 y1(i) = t1
		 y2(i) = t2

	enddo
	return

end subroutine heapsort_down

subroutine heapsort_up(n,y1,y2)

	implicit none
	integer(8),intent(in) :: n
	integer(8),intent(inout) :: y1(1:n)
	integer(8),intent(inout) :: y2(1:n)
	integer(8) :: i,k,j,l
	integer(8) :: t1,t2

	l = n/2+1
	k = n

	do while (k /= 1)
		 if(l > 1) then
				l = l-1
				t1 = y1(l)
				t2 = y2(l)

		 else
				t1 = y1(k)
				t2 = y2(k)
				y1(k) = y1(1)
				y2(k) = y2(1)
				k = k-1
				if(k == 1) then
					 y1(1) = t1
					 y2(1) = t2
					 exit
				endif
		 endif

		 i = l
		 j = l+l

		 do while(j <= k)
				if(j < k) then
					 if(y1(j) < y1(j+1)) j = j+1
				endif

				if (t1 < y1(j)) then
					 y1(i) = y1(j)
					 y2(i) = y2(j)
					 i = j
					 j = j+j
				else
					 j = k+1
				endif

		 enddo

		 y1(i) = t1
		 y2(i) = t2

	enddo
	return

end subroutine heapsort_up

end program

Score : 400 points

Problem Statement

There are N pieces of sushi. Each piece has two parameters: "kind of topping" t_i and "deliciousness" d_i.
You are choosing K among these N pieces to eat.
Your "satisfaction" here will be calculated as follows:

The satisfaction is the sum of the "base total deliciousness" and the "variety bonus".

The base total deliciousness is the sum of the deliciousness of the pieces you eat.

The variety bonus is x*x, where x is the number of different kinds of toppings of the pieces you eat.

You want to have as much satisfaction as possible.
Find this maximum satisfaction.

Constraints

1 \leq K \leq N \leq 10^5

1 \leq t_i \leq N

1 \leq d_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
t_1 d_1
t_2 d_2
.
.
.
t_N d_N

Output

Print the maximum satisfaction that you can obtain.

Sample Input 1

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

Sample Output 1

26

If you eat Sushi 1,2 and 3:

The base total deliciousness is 9+7+6=22.

The variety bonus is 2*2=4.

Thus, your satisfaction will be 26, which is optimal.

Sample Input 2

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

Sample Output 2

25

It is optimal to eat Sushi 1,2,3 and 4.

Sample Input 3

6 5
5 1000000000
2 990000000
3 980000000
6 970000000
6 960000000
4 950000000

Sample Output 3

4900000016

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


program prob1
    implicit none
    integer(8) :: a(4)
    read(*,*) a

    call msort(a)

    if(a(1) == 1 .and.a(2) == 4 .and.a(3) == 7 .and.a(4) == 9)then
        write(*,*) "YES"
    else
        write(*,*) "NO"
    end if

    stop
contains

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

    integer(8) :: c

    c = a
    a = b
    b = c

  end subroutine swap

  !
  ! merge sort
  !
  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
end program prob1

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

Score : 100 points

Problem Statement

You are given four digits N_1, N_2, N_3 and N_4. Determine if these can be arranged into the sequence of digits "1974".

Constraints

0 \leq N_1, N_2, N_3, N_4 \leq 9

N_1, N_2, N_3 and N_4 are integers.

Input

Input is given from Standard Input in the following format:

N_1 N_2 N_3 N_4

Output

If N_1, N_2, N_3 and N_4 can be arranged into the sequence of digits "1974", print YES; if they cannot, print NO.

Sample Input 1

1 7 9 4

Sample Output 1

YES

We can get 1974 by swapping N_2 and N_3.

Sample Input 2

1 9 7 4

Sample Output 2

YES

We already have 1974 before doing anything.

Sample Input 3

1 2 9 1

Sample Output 3

NO

Sample Input 4

4 9 0 8

Sample Output 4

NO

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.


module Sort_Module
    implicit none
    public
    contains
    recursive subroutine mergesort(a, p, r, b)
        integer(8) a(:), b(:)
        integer(8) p, r
        integer(8) q
        if (p < r) then
            q = (p+r)/2
            call mergesort(a, p, q, b)
            call mergesort(a, q+1, r, b)
            call merge(a, p, q, r, b)
        end if
    end subroutine mergesort
    subroutine merge(a, p, q, r, b)
        integer(8) a(:), b(:)
        integer(8) p, r, q, i, j, k
        integer(8) t
        k = p;   i = p;   j = q+1
        do
            if (.not. (k <= r)) exit
            if (j > r) then
                t = a(i);   i = i+1
            else if (i > q) then
                t = a(j);   j = j+1
            else if (a(i) <= a(j)) then
                t = a(i);   i = i+1
            else
                t = a(j);   j = j+1
            end if
            b(k) = t;   k = k+1
        end do
        do i = p, r
            a(i) = b(i)
        end do
    end subroutine merge
    subroutine sort(a)
      integer(8) a(:), b(size(a)),n
      n = size(a)
      call mergesort(a,1_8,n,b)
    end subroutine sort

end module Sort_Module

program atcoder
    use Sort_Module
    implicit none
    integer(8) :: a(4)
    read*,a(1),a(2),a(3),a(4)
    call sort(a)
    if(a(1)==1.and.a(2)==4.and.a(3)==7.and.a(4)==9)then
        write(*,'(a)')"YES"
    else
        write(*,'(a)')"NO"
    endif

    stop
contains

end program atcoder


program main
  implicit none

  character(len=100) :: s
  character(len=7) :: c
  integer :: n

  read(*,*) s

  n = len_trim(s)
  c = 'keyence'

  if(index(s,c) /= 0) then
     if (index(s,c) == 1 .or. index(s,c, back=.true.) == n-6) then
        write(*,*) 'YES'
     else
        write(*,*) 'NO'
     end if
     stop
  else if(index(s,c(1:1)) /= 0) then
     if(index(s,c(2:7)) /= 0) then
        if(index(s,c(1:1)) == 1 .and. index(s,c(2:7), back=.true.) == n-5) then
           write(*,*) 'YES'
        end if
     else
        if(index(s,c(1:2)) /= 0) then
           if(index(s,c(3:7)) /= 0) then
          if(index(s,c(1:2)) == 1 .and. index(s,c(3:7), back=.true.) == n-4) then
             write(*,*) 'YES'
          end if
       else
          if(index(s,c(1:3)) /= 0) then
           if(index(s,c(4:7)) /= 0) then
	  if(index(s,c(1:3)) == 1 .and. index(s,c(4:7), back=.true.) == n-3) then
             write(*,*) 'YES'
          end if
       else
          if(index(s,c(1:4)) /= 0) then
           if(index(s,c(5:7)) /= 0) then
	  if(index(s,c(1:4)) == 1 .and. index(s,c(5:7), back=.true.) == n-2) then
             write(*,*) 'YES'
          end if
       else
          if(index(s,c(1:5)) /= 0) then
           if(index(s,c(6:7)) /= 0) then
	  if(index(s,c(1:5)) == 1 .and. index(s,c(6:7), back=.true.) == n-1) then
             write(*,*) 'YES'
          end if
       else
          if(index(s,c(1:6)) /= 0) then
           if(index(s,c(7:7)) /= 0) then
	  if(index(s,c(1:6)) == 1 .and. index(s,c(7:7), back=.true.) == n) then
             write(*,*) 'YES'
          end if
       else
          write(*,*) 'NO'
       end if
    end if

 end if
end if
end if
end if
end if
end if

end if
end if
end if
end if
end program main

Score : 200 points

Problem Statement

A string is called a KEYENCE string when it can be changed to keyence by removing its contiguous substring (possibly empty) only once.

Given a string S consisting of lowercase English letters, determine if S is a KEYENCE string.

Constraints

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

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If S is a KEYENCE string, print YES; otherwise, print NO.

Sample Input 1

keyofscience

Sample Output 1

YES

keyence is an abbreviation of key of science.

Sample Input 2

mpyszsbznf

Sample Output 2

NO

Sample Input 3

ashlfyha

Sample Output 3

NO

Sample Input 4

keyence

Sample Output 4

YES


program main
implicit none
character(100) ::S
integer :: count = 0
integer :: n

read *,S
n = len_trim(S)

if(S(1:1)=="k")      count = count + 1
if(S(1:2)=="ke")     count = count + 1
if(S(1:3)=="key")    count = count + 1
if(S(1:4)=="keye")   count = count + 1
if(S(1:5)=="keyen")  count = count + 1
if(S(1:6)=="keyenc") count = count + 1
if(S(1:7)=="keyence")count = count + 1

if(S(n-0:n)==      "e")count = count + 1
if(S(n-1:n)==     "ce")count = count + 1
if(S(n-2:n)==    "nce")count = count + 1
if(S(n-3:n)==   "ence")count = count + 1
if(S(n-4:n)==  "yence")count = count + 1
if(S(n-5:n)== "eyence")count = count + 1
if(S(n-6:n)=="keyence")count = count + 1

if ( count > 6 ) then
print *, "YES"
else
print *, "NO"
end if

end program main


program prob6
    implicit none
    integer(8) :: n, i, ans, minsum
    integer(8), allocatable :: a(:), b(:), d(:)
    read(*,*) n
    allocate(a(n))
    allocate(b(n))
    allocate(d(n))
    read(*,*) a
    read(*,*) b

    d = a - b
    if(sum(d) < 0) then
        write(*,*) "-1"
        stop
    end if

    call msort(d)

    do i = 1, n
        if(d(i) >= 0) then
            ans = i - 1
            exit
        end if
        minsum = minsum - d(i)
    end do

    do i = n, 1, -1
        if(minsum <= 0) then
            exit
        end if
        minsum = minsum - d(i)
        ans = ans + 1
    end do

    write(*,*) ans

    deallocate(a)
    deallocate(b)
    deallocate(d)
    stop
contains

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

    integer(8) :: c

    c = a
    a = b
    b = c

  end subroutine swap

  !
  ! quick sortを乱択化するためのsubroutine
  ! このsubroutineは非公開にしているため、kadai6.f90内の
  ! randintとはバッティングしない
  !
  subroutine randint(x, maxint)
    implicit none
    integer(8), intent(out) :: x
    integer(8), intent(in)  :: maxint

    real(8) :: r

    call random_number(r)

    x = int(maxint * r)

  end subroutine randint

  !
  ! quick sort
  !
  recursive subroutine qsort(x)
    implicit none
    integer(8), intent(inout) :: x(:)

    integer(8) :: n, pivot, pivindex, i, j

    n = size(x)

    !
    ! pivotのindexを1~nの中からランダムで選ぶ
    ! はじめの配列が特殊な形でも時間計算量がO(nlog n)になる
    ! この時間計測プログラムでは不要(配列がランダムなので)
    !
    call randint(pivindex, n)
    pivindex = pivindex + 1

    ! pivotを右端に除けておく
    pivot = x(pivindex)
    call swap(x(pivindex), x(n))

    !
    ! i はpivot以上の値が入っているindexの最小値
    ! j はいま調べているindex
    !
    i = 1
    do j = 1, n-1
      if( x(j) < pivot ) then
        call swap(x(i), x(j))
        i = i + 1
      end if
    end do

    ! i番目にpivot、i-1番目まではpivot未満、i+1番目からはpivot以上になる
    call swap(x(i), x(n))

    ! i <= 2ならiまではソート完了。そうでなければi-1までをソート
    if(i >= 3) then
      call qsort(x(:i-1))
    end if

    ! i >= n-1ならi以降はソート完了。そうでなければi+1以降をソート
    if(i <= n-2) then
      call qsort(x(i+1:))
    end if

    return
  end subroutine qsort

  !
  ! merge sort
  !
  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

end program prob6

Score : 400 points

Problem Statement

A university student, Takahashi, has to take N examinations and pass all of them.
Currently, his readiness for the i-th examination is A_{i}, and according to his investigation, it is known that he needs readiness of at least B_{i} in order to pass the i-th examination.

Takahashi thinks that he may not be able to pass all the examinations, and he has decided to ask a magician, Aoki, to change the readiness for as few examinations as possible so that he can pass all of them, while not changing the total readiness.

For Takahashi, find the minimum possible number of indices i such that A_i and C_i are different, for a sequence C_1, C_2, ..., C_{N} that satisfies the following conditions:

The sum of the sequence A_1, A_2, ..., A_{N} and the sum of the sequence C_1, C_2, ..., C_{N} are equal.

For every i, B_i \leq C_i holds.

If such a sequence C_1, C_2, ..., C_{N} cannot be constructed, print -1.

Constraints

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

1 \leq B_i \leq 10^9

A_i and B_i are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_{N}
B_1 B_2 ... B_{N}

Output

Print the minimum possible number of indices i such that A_i and C_i are different, for a sequence C_1, C_2, ..., C_{N} that satisfies the conditions.
If such a sequence C_1, C_2, ..., C_{N} cannot be constructed, print -1.

Sample Input 1

3
2 3 5
3 4 1

Sample Output 1

3

(A_1, A_2, A_3) = (2, 3, 5) and (B_1, B_2, B_3) = (3, 4, 1). If nothing is done, he cannot pass the first and second exams.
The minimum possible number of indices i such that A_i and C_i are different, 3, is achieved when:

(C_1, C_2, C_3) = (3, 5, 2)

Sample Input 2

3
2 3 3
2 2 1

Sample Output 2

0

In this case, he has to do nothing in order to pass all the exams.

Sample Input 3

3
17 7 1
25 6 14

Sample Output 3

-1

In this case, no matter what is done, he cannot pass all the exams.

Sample Input 4

12
757232153 372327760 440075441 195848680 354974235 458054863 463477172 740174259 615762794 632963102 529866931 64991604
74164189 98239366 465611891 362739947 147060907 118867039 63189252 78303147 501410831 110823640 122948912 572905212

Sample Output 4

5


program exam_and_wizard
  implicit none
  integer :: n, i, num
  integer(8) :: a(100000), b(100000), c(100000), tot
  a = 0_8
  b = 0_8
  c = 0_8
  read(*,*) n
  read(*,*) a(1:n)
  read(*,*) b(1:n)
  c(1:n) = a(1:n) - b(1:n)
  num = 0
  tot = 0_8
  do i = 1, n
    if (c(i).lt.0) then
      num = num + 1
      tot = tot - c(i)
    end if
  end do
  if (num.eq.0) then
    write(*,'(i0)') 0
    stop
  end if
  call merge_sort(n,c(1:n))
  do i = n, 1, -1
    if (c(i).lt.0) exit
    tot = tot - c(i)
    num = num + 1
    if (tot.le.0_8) then
      write(*,'(i0)') num
      stop
    end if
  end do
  write(*,'(i0)') -1
  stop
contains
  subroutine merge_sort(n,c)
    implicit none
    integer, intent(in) :: n
    integer(8), intent(inout) :: c(n)
    integer :: num, lth, ubd, i
    num = n
    lth = 1
    do while (num.gt.1)
      do i = 1, num/2
        ubd = min(2*i*lth,n)
        call merge(c(2*(i-1)*lth+1:(2*i-1)*lth),c((2*i-1)*lth+1:ubd))
      end do
      lth = 2*lth
      num = (num+1)/2
    end do
    return
  end subroutine merge_sort
  subroutine merge(c1,c2)
    implicit none
    integer(8), intent(inout) :: c1(:), c2(:)
    integer(8) :: c(size(c1)+size(c2))
    integer :: i1, i2, n1, n2
    i1 = 1
    i2 = 1
    n1 = size(c1)
    n2 = size(c2)
    do while (i1.le.n1.and.i2.le.n2)
      if (c1(i1).le.c2(i2)) then
        c(i1+i2-1) = c1(i1)
        i1 = i1+1
      else
        c(i1+i2-1) = c2(i2)
        i2 = i2+1
      end if
    end do
    if (i1.le.n1) then
      c(i1+i2-1:n1+n2) = c1(i1:n1)
    else if (i2.le.n2) then
      c(i1+i2-1:n1+n2) = c2(i2:n2)
    end if
    c1 = c(1:n1)
    c2 = c(n1+1:n1+n2)
    return
  end subroutine merge
end program exam_and_wizard


program main
  implicit none
  integer (8)                               :: M, N, count, i, j, JG
  integer (8), allocatable, dimension ( : ) :: A, B, D, tmp

  read *,N
  allocate ( A ( N ) )
  allocate ( B ( N ) )
  allocate ( D ( N ) )
  allocate ( tmp ( N ) )
  read *, A
  read *, B

  count = 0
  JG = 0
  M = 0

  do i = 1, N
     JG = JG + A ( i ) - B ( i )
  end do

  if ( JG < 0 ) then
     print *,-1
     stop
  end if

  do i = 1, N
     D ( i ) = A ( i ) - B ( i )
     if ( D ( i ) < 0 )  then
        M = M + D ( i )
        count = count + 1
     end if
  end do

  do j = 1, N
     do i = 1, N - 1
        if ( D ( i ) < D ( i + 1 ) ) then
           tmp ( i )   = D ( i )
           D ( i )     = D ( i + 1 )
           D ( i + 1 ) = tmp ( i )
        end if
     end do
  end do

  do i = 1, N
     if ( M >= 0 ) exit
        M = M + D ( i )
        count = count + 1
  end do

  print *, count

end program main


program keyence2019
  implicit none
  integer n, i, ans
  integer(8) s
  integer(8), dimension(:), allocatable :: a, b, c

  read *, n
  allocate(a(n), b(n), c(n))
  read *, a, b
  c = a - b
  c = sort_desc(c)
  s = sum(pack(c, c < 0))
  ans = count(c < 0)
  do i = 1, n
     if (s >= 0 .or. c(i) <= 0) exit
     s = s + c(i)
     ans = ans + 1
  end do
  if (s < 0) then
     print '(a)', '-1'
  else
     print '(i0)', ans
  end if
contains
  recursive function sort_desc(a) result(res)
    integer(8), dimension(:), allocatable :: res, l, r
    integer(8), dimension(:), intent(in) :: a
    integer p

    if (size(a) < 2) then
       res = a
    else
       p = size(a) / 2
       l = [pack(a(1:p-1), a(1:p-1) > a(p)), pack(a(p+1:), a(p+1:) > a(p))]
       r = [pack(a(1:p-1), a(1:p-1) <= a(p)), pack(a(p+1:), a(p+1:) <= a(p))]
       res = [sort_desc(l), a(p), sort_desc(r)]
    end if
  end function sort_desc
end program keyence2019


program keyence2019
  implicit none
  integer n, i, ans
  integer(8) s
  integer(8), dimension(:), allocatable :: a, b, c

  read *, n
  allocate(a(n), b(n), c(n))
  read *, a, b
  c = a - b
  c = sort_desc(c)
  s = sum(pack(c, c < 0))
  ans = count(c < 0)
  do i = 1, n
     if (s >= 0 .or. c(i) <= 0) exit
     s = s + c(i)
     ans = ans + 1
  end do
  if (s < 0) then
     print '(a)', '-1'
  else
     print '(i0)', ans
  end if
contains
  recursive function sort_desc(a) result(res)
    integer(8), dimension(:), allocatable :: res
    integer(8), dimension(:), intent(in) :: a

    if (size(a) < 2) then
       res = a
    else
       res = [sort_desc(pack(a(2:), a(2:) > a(1))), a(1), sort_desc(pack(a(2:), a(2:) <= a(1)))]
    end if
  end function sort_desc
end program keyence2019


      PROGRAM Vacation
      IMPLICIT NONE
      integer :: n
      integer,allocatable :: abcTable(:,:)
      integer :: day,Yday,Today
      integer,allocatable :: dp(:,:)

      read*,n
      allocate(abcTable(n,3),dp(0:n,3) )
      do day = 1,n
        read*,abcTable(day,:)
      end do

      dp = 0
      !dp(day,abc)当日に行動abcをとった場合のday(終了時)での最大値

      !abcHielaux,abcHodiaux
      do day = 1,n
        do Today = 1,3
          do Yday = 1,3
            if( Today==Yday )cycle
            dp(day,Today) = max( dp(day,Today), &
              &dp(day-1,Yday)+abcTable(day,Today) )
          end do
        end do
      end do

      print*,maxval(dp(n,:))

      END PROGRAM

Score : 100 points

Problem Statement

Taro's summer vacation starts tomorrow, and he has decided to make plans for it now.

The vacation consists of N days.
For each i (1 \leq i \leq N), Taro will choose one of the following activities and do it on the i-th day:

A: Swim in the sea. Gain a_i points of happiness.

B: Catch bugs in the mountains. Gain b_i points of happiness.

C: Do homework at home. Gain c_i points of happiness.

As Taro gets bored easily, he cannot do the same activities for two or more consecutive days.

Find the maximum possible total points of happiness that Taro gains.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq a_i, b_i, c_i \leq 10^4

Input

Input is given from Standard Input in the following format:

N
a_1 b_1 c_1
a_2 b_2 c_2
:
a_N b_N c_N

Output

Print the maximum possible total points of happiness that Taro gains.

Sample Input 1

3
10 40 70
20 50 80
30 60 90

Sample Output 1

210

If Taro does activities in the order C, B, C, he will gain 70 + 50 + 90 = 210 points of happiness.

Sample Input 2

1
100 10 1

Sample Output 2

100

Sample Input 3

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

Sample Output 3

46

Taro should do activities in the order C, A, B, A, C, B, A.


program main
  implicit none
  integer :: i, j, N
  integer,allocatable,dimension(:, :) :: v, dp
  logical,dimension(3, 3) :: f
  f = .true.
  do i = 1,3
    f(i, i) = .false.
  enddo

  read*, N
  allocate(dp(3, N), v(3, N))
  do i = 1, N
    read*, v(:, i)
  end do

  dp(:, 1) = v(:, 1)
  do i = 2, N
    do j = 1, 3
      dp(j, i) = maxval(pack(dp(:, i-1), f(:, j))) + v(j, i)
    enddo
  enddo
  print*, maxval(dp(:, N))
end


program eee
implicit none

integer(8) :: n, w, i, j
integer(8),allocatable,dimension(:) :: ww, vv
integer(8),allocatable,dimension(:,:) :: dp
! 初期値の設定
read*, n, w
allocate(ww(n),vv(n))
do i=1, n
	read*, ww(i), vv(i)
end do
allocate(dp(0:n,0:1000*n))
dp=w*n+1
dp(0,0)=0
! end 初期値の設定

do i=1,n
	do j=0,1000*n
		if(j>=vv(i)) then					!追加可能パターン
        	dp(i,j)=dp(i-1,j-vv(i))+ww(i)	!i-1の場合の、価値j-v(i)から参照
        end if
        dp(i,j)=min(dp(i,j),dp(i-1,j))		!追加したものとしていないものの比較を行う
    end do
end do

do j=1000*n,0,-1
    if(dp(n,j)<=w) then
    	write(*,'(i0)') j
        return
    end if
end do

end program

Score : 100 points

Problem Statement

There are N items, numbered 1, 2, \ldots, N.
For each i (1 \leq i \leq N), Item i has a weight of w_i and a value of v_i.

Taro has decided to choose some of the N items and carry them home in a knapsack.
The capacity of the knapsack is W, which means that the sum of the weights of items taken must be at most W.

Find the maximum possible sum of the values of items that Taro takes home.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq W \leq 10^9

1 \leq w_i \leq W

1 \leq v_i \leq 10^3

Input

Input is given from Standard Input in the following format:

N W
w_1 v_1
w_2 v_2
:
w_N v_N

Output

Print the maximum possible sum of the values of items that Taro takes home.

Sample Input 1

3 8
3 30
4 50
5 60

Sample Output 1

90

Items 1 and 3 should be taken.
Then, the sum of the weights is 3 + 5 = 8, and the sum of the values is 30 + 60 = 90.

Sample Input 2

1 1000000000
1000000000 10

Sample Output 2

10

Sample Input 3

6 15
6 5
5 6
6 4
6 6
3 5
7 2

Sample Output 3

17

Items 2, 4 and 5 should be taken.
Then, the sum of the weights is 5 + 6 + 3 = 14, and the sum of the values is 6 + 6 + 5 = 17.


program eee
implicit none

integer(8) :: n, w, i, j
integer(8),allocatable,dimension(:) :: ww, vv
integer(8),allocatable,dimension(:,:) :: dp
! 初期値の設定
read*, n, w
allocate(ww(n),vv(n))
do i=1, n
	read*, ww(i), vv(i)
end do
allocate(dp(0:n,0:sum(vv)))
dp=sum(vv)+1
dp(0,0)=0
! end 初期値の設定

do i=1,n
	do j=0,sum(vv)
		if(j>=vv(i)) then					!追加可能パターン
        	dp(i,j)=dp(i-1,j-vv(i))+ww(i)	!i-1の場合の、価値j-v(i)から参照
        end if
        dp(i,j)=min(dp(i,j),dp(i-1,j))		!追加したものとしていないものの比較を行う
    end do
end do

do j=sum(vv),0,-1
    if(dp(n,j)<=w) then
    	write(*,'(i0)') j
        return
    end if
end do

end program


integer N,M
integer X,Y
integer,allocatable,dimension(:,:)::EDGE
integer,allocatable,dimension(:)::Nindex
integer,allocatable,dimension(:)::DP
integer ans
!読み込み
read*,N,M
allocate(DP(N))
allocate(EDGE(N,M))
allocate(Nindex(N))
EDGE=0
Nindex=0
do i=1,M
  read*,X,Y
  Nindex(X)=Nindex(X)+1
  EDGE(X,Nindex(X))=Y
end do
!読み込み終了

DP = -1
!DP[i]とはノードiからのびる経路のうち最短のもの
!まだ調べていない状態は-1とする
ans = 0
do i=1,N
  if(DP(i)==-1)DP(i)=DFS(i)
  ans=max(ans,DP(i) )
end do

print"(I0)",ans

contains

recursive function DFS(u)result(res)
integer u,res
  if(DP(u) >= 0)then
    res=DP(u)
  else
    DP(u)=0
    do j=1,Nindex(u)
      DP(u)=max(DP(u),DFS(EDGE(u,j))+1)
    end do
  res=DP(u)
  endif
end function DFS
end

Score : 100 points

Problem Statement

There is a directed graph G with N vertices and M edges.
The vertices are numbered 1, 2, \ldots, N, and for each i (1 \leq i \leq M), the i-th directed edge goes from Vertex x_i to y_i.
G does not contain directed cycles.

Find the length of the longest directed path in G.
Here, the length of a directed path is the number of edges in it.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq x_i, y_i \leq N

All pairs (x_i, y_i) are distinct.

G does not contain directed cycles.

Input

Input is given from Standard Input in the following format:

N M
x_1 y_1
x_2 y_2
:
x_M y_M

Output

Print the length of the longest directed path in G.

Sample Input 1

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

Sample Output 1

3

The red directed path in the following figure is the longest:

Sample Input 2

6 3
2 3
4 5
5 6

Sample Output 2

2

The red directed path in the following figure is the longest:

Sample Input 3

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

Sample Output 3

3

The red directed path in the following figure is one of the longest:


integer N
double precision,allocatable,dimension(:)::p
double precision,allocatable,dimension(:,:)::DP
double precision ans
read*,N
allocate(p(N))
read*,p

allocate(DP(0:N,0:N))
DP=0
DP(0,0)=1
do i=1,N
  DP(i,0)=DP(i-1,0)*(1-p(i))
  do j=1,i
    DP(i,j)=DP(i-1,j-1)*p(i) + DP(i-1,j)*(1-p(i))
  end do
end do
ans=0
do i=N/2+merge(1,0,mod(N,2)==1),N
  ans=ans+DP(N,i)
end do

print"(F12.11)",ans
end

Score : 100 points

Problem Statement

Let N be a positive odd number.

There are N coins, numbered 1, 2, \ldots, N.
For each i (1 \leq i \leq N), when Coin i is tossed, it comes up heads with probability p_i and tails with probability 1 - p_i.

Taro has tossed all the N coins.
Find the probability of having more heads than tails.

Constraints

N is an odd number.

1 \leq N \leq 2999

p_i is a real number and has two decimal places.

0 < p_i < 1

Input

Input is given from Standard Input in the following format:

N
p_1 p_2 \ldots p_N

Output

Print the probability of having more heads than tails.
The output is considered correct when the absolute error is not greater than 10^{-9}.

Sample Input 1

3
0.30 0.60 0.80

Sample Output 1

0.612

The probability of each case where we have more heads than tails is as follows:

The probability of having (Coin 1, Coin 2, Coin 3) = (Head, Head, Head) is 0.3 × 0.6 × 0.8 = 0.144;

The probability of having (Coin 1, Coin 2, Coin 3) = (Tail, Head, Head) is 0.7 × 0.6 × 0.8 = 0.336;

The probability of having (Coin 1, Coin 2, Coin 3) = (Head, Tail, Head) is 0.3 × 0.4 × 0.8 = 0.096;

The probability of having (Coin 1, Coin 2, Coin 3) = (Head, Head, Tail) is 0.3 × 0.6 × 0.2 = 0.036.

Thus, the probability of having more heads than tails is 0.144 + 0.336 + 0.096 + 0.036 = 0.612.

Sample Input 2

1
0.50

Sample Output 2

0.5

Outputs such as 0.500, 0.500000001 and 0.499999999 are also considered correct.

Sample Input 3

5
0.42 0.01 0.42 0.99 0.42

Sample Output 3

0.3821815872


program atcoder
    implicit none
    integer :: i,j,n,k
    integer,allocatable,dimension(:) :: dp
    integer,allocatable,dimension(:) :: a
    read*,n,k
    allocate(dp(0:k+1))
    ! dp(i) := はじめ石がi個の時、先攻の人が勝てるかどうか
    allocate(a(n))
    read(*,*)(a(i),i=1,n)
    dp = 0
    do i = 0,k
        do j = 1,n
            if(i-a(j)>=0 .and. dp(i-a(j))==0)then
                !石がi個のとき、a[j]個取って相手が勝てない状態にできるなら勝てる
                dp(i) = 1
            endif
        enddo
    enddo
    if(dp(k)==1)then
        write(*,'(a)')"First"
    else
        write(*,'(a)')"Second"
    endif
    stop
contains

end program atcoder

Score : 100 points

Problem Statement

There is a set A = \{ a_1, a_2, \ldots, a_N \} consisting of N positive integers.
Taro and Jiro will play the following game against each other.

Initially, we have a pile consisting of K stones.
The two players perform the following operation alternately, starting from Taro:

Choose an element x in A, and remove exactly x stones from the pile.

A player loses when he becomes unable to play.
Assuming that both players play optimally, determine the winner.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq K \leq 10^5

1 \leq a_1 < a_2 < \cdots < a_N \leq K

Input

Input is given from Standard Input in the following format:

N K
a_1 a_2 \ldots a_N

Output

If Taro will win, print First; if Jiro will win, print Second.

Sample Input 1

2 4
2 3

Sample Output 1

First

If Taro removes three stones, Jiro cannot make a move.
Thus, Taro wins.

Sample Input 2

2 5
2 3

Sample Output 2

Second

Whatever Taro does in his operation, Jiro wins, as follows:

If Taro removes two stones, Jiro can remove three stones to make Taro unable to make a move.

If Taro removes three stones, Jiro can remove two stones to make Taro unable to make a move.

Sample Input 3

2 7
2 3

Sample Output 3

First

Taro should remove two stones. Then, whatever Jiro does in his operation, Taro wins, as follows:

If Jiro removes two stones, Taro can remove three stones to make Jiro unable to make a move.

If Jiro removes three stones, Taro can remove two stones to make Jiro unable to make a move.

Sample Input 4

3 20
1 2 3

Sample Output 4

Second

Sample Input 5

3 21
1 2 3

Sample Output 5

First

Sample Input 6

1 100000
1

Sample Output 6

Second


program main
	implicit none
    integer::a,ans,i
    read(*,*) a
    ans=0
    do i=1,4
    	if(mod(a,10)==2)then
        	ans=ans+1

        end if
        a=a/10
    end do
    write(*,*) ans
    stop
end program main

Score : 100 points

Problem Statement

You are given an integer N that has exactly four digits in base ten.
How many times does 2 occur in the base-ten representation of N?

Constraints

1000 \leq N \leq 9999

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

1222

Sample Output 1

3

2 occurs three times in 1222. By the way, this contest is held on December 22 (JST).

Sample Input 2

3456

Sample Output 2

0

Sample Input 3

9592

Sample Output 3

1


program atcoder
    implicit none
    character(len=4) k
    integer :: ans=0,i
    read *,k
    do i = 1,4
        if(k(i:i)=='2')ans = ans+1
    end do
    write(*,'(I0)')ans
end program atcoder


program CADDiB2018B
    implicit none
    integer(8)::N,H,W,A,B,i,ans=0
    read*,N,H,W
    do i=1,N
        read*,A,B
        if(A>=H.and.B>=W)ans=ans+1
    end do
    print'(i0)',ans
end program CADDiB2018B

Score : 200 points

Problem Statement

There are N rectangular plate materials made of special metal called AtCoder Alloy.
The dimensions of the i-th material are A_i \times B_i (A_i vertically and B_i horizontally).

Takahashi wants a rectangular plate made of AtCoder Alloy whose dimensions are exactly H \times W.
He is trying to obtain such a plate by choosing one of the N materials and cutting it if necessary.
When cutting a material, the cuts must be parallel to one of the sides of the material.
Also, the materials have fixed directions and cannot be rotated.
For example, a 5 \times 3 material cannot be used as a 3 \times 5 plate.

Out of the N materials, how many can produce an H \times W plate if properly cut?

Constraints

1 \leq N \leq 1000

1 \leq H \leq 10^9

1 \leq W \leq 10^9

1 \leq A_i \leq 10^9

1 \leq B_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N H W
A_1 B_1
A_2 B_2
:
A_N B_N

Output

Print the answer.

Sample Input 1

3 5 2
10 3
5 2
2 5

Sample Output 1

2

Takahashi wants a 5 \times 2 plate.

The dimensions of the first material are 10 \times 3. We can obtain a 5 \times 2 plate by properly cutting it.

The dimensions of the second material are 5 \times 2. We can obtain a 5 \times 2 plate without cutting it.

The dimensions of the third material are 2 \times 5. We cannot obtain a 5 \times 2 plate, whatever cuts are made. Note that the material cannot be rotated and used as a 5 \times 2 plate.

Sample Input 2

10 587586158 185430194
894597290 708587790
680395892 306946994
590262034 785368612
922328576 106880540
847058850 326169610
936315062 193149191
702035777 223363392
11672949 146832978
779291680 334178158
615808191 701464268

Sample Output 2

8


integer(8) N,H,W
integer(8) A,B
integer ans
read*,N,H,W
ans=0
do i=1,N
  read*,A,B
  if(A>=H.and.B>=W)ans=ans+1
end do
print"(i0)",ans
end


program CADDiB2018C
    implicit none
    integer(8)::N,P,Ps,i,i_cnt,ans=1
    read*,N,P
    Ps=int(sqrt(real(P)))+1
    do i=2,Ps
        if(mod(P,i)==0)then
            i_cnt=0
            do
                if(mod(P,i)/=0)exit
                P=P/i
                i_cnt=i_cnt+1
            end do
            ans=ans*(i**(i_cnt/N))
        end if
    end do
    ans=ans*(P**(1/N))
    print'(i0)',ans
end program CADDiB2018C

Score : 300 points

Problem Statement

There are N integers a_1, a_2, ..., a_N not less than 1.
The values of a_1, a_2, ..., a_N are not known, but it is known that a_1 \times a_2 \times ... \times a_N = P.

Find the maximum possible greatest common divisor of a_1, a_2, ..., a_N.

Constraints

1 \leq N \leq 10^{12}

1 \leq P \leq 10^{12}

Input

Input is given from Standard Input in the following format:

N P

Output

Print the answer.

Sample Input 1

3 24

Sample Output 1

2

The greatest common divisor would be 2 when, for example, a_1=2, a_2=6 and a_3=2.

Sample Input 2

5 1

Sample Output 2

1

As a_i are positive integers, the only possible case is a_1 = a_2 = a_3 = a_4 = a_5 = 1.

Sample Input 3

1 111

Sample Output 3

111

Sample Input 4

4 972439611840

Sample Output 4

206


program negative_doubling
  implicit none
  integer :: n, d(2,200000) = 0, dp(2,200000,0:15) = 0, m, i, j, x, y
  integer(8) :: a(200000) = 0_8, p, q
  read(*,*) n
  read(*,*) a(1:n)
  do j = 1, 2
    do i = 1, n-1
      p = a(i)
      q = a(i+1)
      if (p <= q) then
        do while (4_8*p <= q)
          d(j,i) = d(j,i)-1
          p = 4_8*p
        end do
      else
        do while (p > q)
          d(j,i) = d(j,i)+1
          q = 4_8*q
        end do
      end if
    end do
    a(1:n) = a(n:1:-1)
  end do
  do x = 0, 15
    dp(:,n,x) = 2*x
  end do
  do j = 1, 2
    do i = n-1, 1, -1
      do x = 0, 15
        y = max(x+d(j,i),0)
        if (y <= 15) then
          dp(j,i,x) = dp(j,i+1,y)+2*x
        else
          dp(j,i,x) = dp(j,i+1,15)+2*x+2*(y-15)*(n-i)
        end if
      end do
    end do
  end do
  dp(2,1:n,:) = dp(2,n:1:-1,:)
  m = min(dp(1,1,0),dp(2,n,0)+n)
  do i = 2, n
    m = min(m,dp(1,i,0)+dp(2,i-1,0)+i-1)
  end do
  write(*,'(i0)') m
  stop
end program negative_doubling

Score : 800 points

Problem Statement

There are N positive integers A_1, A_2, ..., A_N.
Takahashi can perform the following operation on these integers any number of times:

Choose 1 \leq i \leq N and multiply the value of A_i by -2.

Notice that he multiplies it by minus two.

He would like to make A_1 \leq A_2 \leq ... \leq A_N holds.
Find the minimum number of operations required. If it is impossible, print -1.

Constraints

1 \leq N \leq 200000

1 \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 answer.

Sample Input 1

4
3 1 4 1

Sample Output 1

3

One possible solution is:

Choose i=4 and multiply the value of A_4 by -2. A_1, A_2, A_3, A_4 are now 3, 1, 4, -2.

Choose i=1 and multiply the value of A_1 by -2. A_1, A_2, A_3, A_4 are now -6, 1, 4, -2.

Choose i=4 and multiply the value of A_4 by -2. A_1, A_2, A_3, A_4 are now -6, 1, 4, 4.

Sample Input 2

5
1 2 3 4 5

Sample Output 2

0

A_1 \leq A_2 \leq ... \leq A_N holds before any operation is performed.

Sample Input 3

8
657312726 129662684 181537270 324043958 468214806 916875077 825989291 319670097

Sample Output 3

7


program pro5
    implicit none
    character(len=2*10**5)::C
    integer::i, j, b, ans
    read(*,*) C
    b = 0
    ans = 0

    do i = 1, len_trim(C)
        if(C(i:i) .eq. 'W') then
            ans = ans + b
        else
            b = b+1
        end if
    end do

    write(*,*) ans
    stop

end program pro5

Score : 300 points

Problem Statement

There are N Reversi pieces arranged in a row. (A Reversi piece is a disc with a black side and a white side.)
The state of each piece is represented by a string S of length N.
If S_i=B, the i-th piece from the left is showing black;
If S_i=W, the i-th piece from the left is showing white.

Consider performing the following operation:

Choose i (1 \leq i < N) such that the i-th piece from the left is showing black and the (i+1)-th piece from the left is showing white, then flip both of those pieces. That is, the i-th piece from the left is now showing white and the (i+1)-th piece from the left is now showing black.

Find the maximum possible number of times this operation can be performed.

Constraints

1 \leq |S| \leq 2\times 10^5

S_i=B or W

Input

Input is given from Standard Input in the following format:

S

Output

Print the maximum possible number of times the operation can be performed.

Sample Input 1

BBW

Sample Output 1

2

The operation can be performed twice, as follows:

Flip the second and third pieces from the left.

Flip the first and second pieces from the left.

Sample Input 2

BWBWBW

Sample Output 2

6


program irreversible_operation
  implicit none
  character(len=200000) :: s
  integer               :: i, j, n
  integer(8)            :: a, b
  read(*,*) s
  n = len_trim(s)
  a = 0
  b = 0
  j = 0
  do i = 1, n
     if (s(i:i) .eq. 'B') then
        a = a + n - j
        b = b + i
        j = j + 1
     end if
  end do
  write(*,'(i0)') a - b
  stop
end program irreversible_operation


program main
implicit None
character*200000::s
integer(8)::k,t,i,n
integer(8),allocatable::a(:)

read *,s
k = len_trim(s)
t = 0

do i = k,1,-1
	if(s(i:i) == "B")then
	t = t + 1
	endif
enddo

if(t == 0)then
	print 101,t
	stop
endif

allocate(a(t))

n = 1
do i = k,1,-1
	if(s(i:i) == "B")then
		a(n) = i
		n = n + 1
	endif
enddo

n = 0
do i = 1,t
	n  = n + k - (a(i)) -(i -1)
enddo

print 101,n
deallocate(a)

101 format(i0)
end program main


program main
implicit None
	integer(8)::n,p,i,k,s
	integer(8),allocatable::a(:)
	real(8)::x,y

	s = 0
	read *,n
	allocate(a(n))

	read *,a

	call hsort(a,n)

	y = 2
	do i = n,2,-1
		if(a(i) == 0) cycle
		p =2** (int(log(real(a(i)))/log(real(y))+1))-a(i)

		call schint(a,i-1,p,k)

		if (k == 0) cycle

		if(a(k) == p)then
			a(k) = 0
			s = s + 1
		endif
	enddo

	print 101,s

101 format(i0)
end program main

subroutine makeheap(a,n)
Implicit None
	integer(8)::n,i,c1,c2,p,t,m
	integer(8)::a(n)

	do i = n/2,1,-1
		p = i; c1 = i * 2
		do while (c1 <= n)
			c2 = c1 + 1
			if(c2 <= n .and. a(c2) > a(c1)) c1 = c2
			if(a(p) < a(c1))then
				t = a(p)
				a(p) = a(c1)
				a(c1) = t
				p = c1
				c1 = p*2
				else
					exit
			endif
		enddo
	enddo
end subroutine makeheap

subroutine hsort(a,n)
Implicit None
	integer(8)::n,t,i,p,c1,c2,j
	integer(8)::a(n)

	call makeheap(a,n)

	do i = n,2,-1
		t = a(i)
		a(i) = a(1)
		a(1) = t

		p = 1
		do
			c1 = p*2
			if (c1 >= i) exit

			c2 = c1 +1
			if(c2 < i .and. a(c2) > a(c1))then
					c1 = c2
			endif

			if(a(c1) >a(p))then
				t = a(p)
				a(p) = a(c1)
				a(c1) = t
				p = c1
				else
					exit
			endif
		enddo
	enddo
end subroutine hsort

subroutine schint(a,e1,x,p)
implicit None
	integer(8)::e1,s,e,x,p,t
	integer(8)::a(e1)

	s = 1;e = e1

	if(a(e) < x)then
		p = 0
		return
	endif

	do
		t = (s + e)/2
		if(a(t) < x)then
			if(a(t+1) >= x)then
				p = t + 1
				return
			endif

			s = t
			else if(t == 1 .or. a(t-1) < x)then
				p = t
				return
			else
			e = t
		endif
	enddo

end subroutine schint

Score : 600 points

Problem Statement

Takahashi has N balls with positive integers written on them. The integer written on the i-th ball is A_i.
He would like to form some number of pairs such that the sum of the integers written on each pair of balls is a power of 2.
Note that a ball cannot belong to multiple pairs.
Find the maximum possible number of pairs that can be formed.

Here, a positive integer is said to be a power of 2 when it can be written as 2^t using some non-negative integer t.

Constraints

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

1 \leq A_i \leq 10^9

A_i is an integer.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible number of pairs such that the sum of the integers written on each pair of balls is a power of 2.

Sample Input 1

3
1 2 3

Sample Output 1

1

We can form one pair whose sum of the written numbers is 4 by pairing the first and third balls.
Note that we cannot pair the second ball with itself.

Sample Input 2

5
3 11 14 5 13

Sample Output 2

2


program main
implicit None
integer(8)::n,i,j,p,q,s,e,f,g
integer(8),allocatable::a(:)
real(8)::k,x,y

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

	s = 0
	do i = n,1,-1
		if (a(i) >10e9) cycle
		x = real(a(i));y = 2
		q = 2**(int(log(x)/log(y)+1))

		p = q-a(i)

		e = 1
		f = i-1

		do while (f-e >3)
		g = (e + f)/2
		if(a(g) > p)then
			f = g-1
			else
				e = g
		endif
		enddo

		do j = f,e,-1
			if(a(j) == p)then
				s = s + 1
				a(j) = 1000000001
				exit
			endif
		enddo
	enddo

	print * ,s
101 format(i0)
end program main

subroutine h2(a,n)
implicit None
	integer(8)::a(n),x
	integer(8)::n,i,ie,i0,i1,i2

	do i = n/2,1,-1
		x = a(i);ie = n + 1; i0 = i; i1 = i0 * 2
		do while(i1 < ie)
			i2 = i1 + 1
			if(i2 < ie .and. a(i1) < a(i2)) i1 = i2
			if(a(i1) > x)then
				a(i0) = a(i1);a(i1) = x
				else
				exit
			endif
			i0 = i1;i1 = i0 *2
		enddo
	enddo
	do ie = n,2,-1
		x = a(ie)
		a(ie) = a(1)
		a(1) = x
		i0 =1;i1 =2
		do while(i1 <ie)
			i2 = i1 +1
			if(i2 < ie .and. a(i1) < a(i2)) i1 = i2
			if(a(i1) > x)then
				a(i0) = a(i1);a(i1) = x
				else
				exit
			endif
			i0 = i1;i1 = i0 *2
		enddo
	enddo
end subroutine h2


program main
implicit None
integer(8)::n,i,j,p,q,s
integer(8),allocatable::a(:)
real(8)::k,x,y

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

	s = 0
	do i = n,1,-1
		if (a(i) >10e9) cycle
		x = a(i);y = 2
		k = log(x)/log(y)
		p = int(k)+1
		q = 2**p

		p = q-a(i)

		do j = i-1,1,-1
			if(a(j) == p)then
				s = s + 1
				a(j) = 1000000001
				exit
			endif
		enddo
	enddo

	print * ,s
101 format(i0)
end program main

subroutine h2(a,n)
implicit None
	integer(8)::a(n),x
	integer(8)::n,i,ie,i0,i1,i2

	do i = n/2,1,-1
		x = a(i);ie = n + 1; i0 = i; i1 = i0 * 2
		do while(i1 < ie)
			i2 = i1 + 1
			if(i2 < ie .and. a(i1) < a(i2)) i1 = i2
			if(a(i1) > x)then
				a(i0) = a(i1);a(i1) = x
				else
				exit
			endif
			i0 = i1;i1 = i0 *2
		enddo
	enddo
	do ie = n,2,-1
		x = a(ie)
		a(ie) = a(1)
		a(1) = x
		i0 =1;i1 =2
		do while(i1 <ie)
			i2 = i1 +1
			if(i2 < ie .and. a(i1) < a(i2)) i1 = i2
			if(a(i1) > x)then
				a(i0) = a(i1);a(i1) = x
				else
				exit
			endif
			i0 = i1;i1 = i0 *2
		enddo
	enddo
end subroutine h2


program main
   implicit none
   integer(8) n,beki(31)
   integer(8),allocatable,dimension(:)::a
   integer(8) ::i,j,k,counter=0
   read(*,*)n
   allocate(a(n))
   read(*,*)a
   call heapsort(n,a)
   do i=1,31
      beki(i)=2**i
   enddo
 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
   do i=n,2,-1
      do j=i-1,1,-1
         do k=1,31
            if(beki(k)==a(i)+a(j) .and. a(i)/=0 .and. a(j)/=0)then
               counter=counter+1
               a(j)=0
            endif
         enddo
      enddo
   enddo
   write(*,*)counter
contains
subroutine heapsort(n,array)
  implicit none
  integer(8),intent(in) :: n
  integer(8),intent(inout) :: array(1:n)

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

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

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

  return
end subroutine heapsort
end program main


module mod_tree_map
  implicit none
  type t_entry
    private
    integer :: key
    integer :: val
  end type
  type t_node
    private
    type(t_node), pointer :: left => null(), right => null()
    integer :: level = 1
    type(t_entry), pointer :: e => null()
  end type
  type t_tree_map
    private
    type(t_node), pointer :: root => null()
    integer :: deflt = 0
  contains
    procedure :: size => size_of
    procedure :: clear => clear
    procedure :: set_default => set_default
    procedure :: put => put
    procedure :: remove => remove
    procedure :: get => get
    procedure :: contain => contain
    procedure :: first_key => get_first_key
    procedure :: last_key => get_last_key
    procedure :: floor_key => floor_key
    procedure :: lower_key => lower_key
    procedure :: ceiling_key => ceiling_key
    procedure :: higher_key => higher_key
    procedure :: get_keys => get_keys
  end type
contains
  function new_entry(key,val) result(e)
    integer, intent(in) :: key
    integer, intent(in) :: val
    type(t_entry), pointer :: e
    e => null()
    allocate(e)
    e%key = key
    e%val = val
  end
  function new_node(e) result(n)
    type(t_entry), pointer, intent(in) :: e
    type(t_node), pointer :: n
    n => null()
    allocate(n)
    n%e => e
  end
  integer function level(n)
    type(t_node), pointer, intent(in) :: n
    level = 0
    if (.not.associated(n)) return
    level = n%level
  end
  logical function is_leaf(n)
    type(t_node), pointer, intent(in) :: n
    is_leaf = associated(n) .and. .not.associated(n%left) .and. &
    &         .not.associated(n%right)
  end
  recursive function tree_size(n) result(s)
    type(t_node), pointer, intent(in) :: n
    integer :: s
    s = 0
    if (.not.associated(n)) return
    s = 1+tree_size(n%left)+tree_size(n%right)
  end
  function skew(n) result(l)
    type(t_node), pointer, intent(in) :: n
    type(t_node), pointer :: l
    l => n
    if (.not.(associated(n) .and. associated(n%left) .and. &
    &   n%left%level == n%level)) return
    l => n%left
    n%left => l%right
    l%right => n
  end
  function split(n) result(r)
    type(t_node), pointer, intent(in) :: n
    type(t_node), pointer :: r
    r => n
    if (.not.(associated(n) .and. associated(n%right) .and. &
    &   associated(n%right%right) .and. n%right%right%level == n%level)) return
    r => n%right
    n%right => r%left
    r%left => n
    r%level = r%level+1
  end
  function predecessor(n) result(p)
    type(t_node), pointer, intent(in) :: n
    type(t_node), pointer :: p
    p => null()
    if (.not.associated(n%left)) return
    p => n%left
    do while (associated(p%right))
      p => p%right
    end do
  end
  function successor(n) result(s)
    type(t_node), pointer, intent(in) :: n
    type(t_node), pointer :: s
    s => null()
    if (.not.associated(n%right)) return
    s => n%right
    do while (associated(s%left))
      s => s%left
    end do
  end
  recursive function insert(n,e) result(t)
    type(t_node), pointer, intent(in) :: n
    type(t_entry), pointer, intent(in) :: e
    type(t_node), pointer :: t
    t => new_node(e)
    if (.not.associated(n)) return
    t => n
    if (e%key < t%e%key) then
      t%left => insert(t%left,e)
    else if (e%key > t%e%key) then
      t%right => insert(t%right,e)
    else
      t%e => e
    end if
    t => skew(t)
    t => split(t)
  end
  recursive function delete(n,e) result(t)
    type(t_node), pointer, intent(in) :: n
    type(t_entry), pointer, intent(in) :: e
    type(t_node), pointer :: t, l
    t => n
    if (.not.associated(n)) return
    if (e%key < t%e%key) then
      t%left => delete(t%left,e)
    else if (e%key > t%e%key) then
      t%right => delete(t%right,e)
    else
      if (is_leaf(t)) then
        t => null()
        return
      end if
      if (.not.associated(t%left)) then
        l => successor(t)
        t%right => delete(t%right,l%e)
        t%e => l%e
      else
        l => predecessor(t)
        t%left => delete(t%left,l%e)
        t%e => l%e
      end if
    end if
    t => decrease_level(t)
    t => skew(t)
    t%right => skew(t%right)
    if (associated(t%right)) t%right%right => skew(t%right%right)
    t => split(t)
    t%right => split(t%right)
  end
  function decrease_level(n) result(t)
    type(t_node), pointer, intent(in) :: n
    type(t_node), pointer :: t
    integer :: should_be
    t => n
    should_be = min(level(t%left),level(t%right))+1
    if (t%level > should_be) then
      t%level = should_be
      if (level(t%right) > should_be) t%right%level = should_be
    end if
  end
  recursive subroutine release_tree(t)
    type(t_node), pointer, intent(inout) :: t
    if (.not.associated(t)) return
    call release_tree(t%left)
    call release_tree(t%right)
    deallocate(t)
  end
  recursive subroutine get_keys_list(t,keys,num)
    type(t_node), pointer, intent(in) :: t
    integer, intent(inout) :: keys(:)
    integer, intent(inout) :: num
    if (.not.associated(t)) return
    call get_keys_list(t%left,keys,num)
    num = num+1
    keys(num) = t%e%key
    call get_keys_list(t%right,keys,num)
  end
  integer function size_of(this)
    class(t_tree_map), intent(in) :: this
    size_of = tree_size(this%root)
  end
  subroutine clear(this)
    class(t_tree_map), intent(inout) :: this
    call release_tree(this%root)
    this%root => null()
  end
  subroutine set_default(this,deflt)
    class(t_tree_map), intent(inout) :: this
    integer, intent(in) :: deflt
    this%deflt = deflt
  end
  subroutine put_entry(this,e)
    type(t_tree_map), intent(inout) :: this
    type(t_entry), pointer, intent(in) :: e
    this%root => insert(this%root,e)
  end
  subroutine remove_entry(this,e)
    type(t_tree_map), intent(inout) :: this
    type(t_entry), pointer, intent(in) :: e
    this%root => delete(this%root,e)
  end
  function get_entry(this,e) result(ret)
    type(t_tree_map), intent(in) :: this
    type(t_entry), pointer, intent(in) :: e
    type(t_node), pointer :: n
    type(t_entry), pointer :: ret
    ret => null()
    n => this%root
    do while (associated(n))
      if (e%key < n%e%key) then
        n => n%left
      else if (e%key > n%e%key) then
        n => n%right
      else
        ret => n%e
        return
      end if
    end do
  end
  function contain_entry(this,e) result(ret)
    type(t_tree_map), intent(in) :: this
    type(t_entry), pointer, intent(in) :: e
    type(t_node), pointer :: n
    logical :: ret
    ret = .false.
    n => this%root
    do while (associated(n))
      if (e%key < n%e%key) then
        n => n%left
      else if (e%key > n%e%key) then
        n => n%right
      else
        ret = .true.
        return
      end if
    end do
  end
  function get_first_entry(this) result(ret)
    type(t_tree_map), intent(in) :: this
    type(t_node), pointer :: n
    type(t_entry), pointer :: ret
    ret => null()
    n => this%root
    if (.not.associated(n)) return
    do while (associated(n%left))
      n => n%left
    end do
    ret => n%e
  end
  function poll_first_entry(this) result(ret)
    type(t_tree_map), intent(inout) :: this
    type(t_node), pointer :: n
    type(t_entry), pointer :: ret
    ret => null()
    n => this%root
    if (.not.associated(n)) return
    do while (associated(n%left))
      n => n%left
    end do
    ret => n%e
    this%root => delete(this%root,ret)
  end
  function get_last_entry(this) result(ret)
    type(t_tree_map), intent(in) :: this
    type(t_node), pointer :: n
    type(t_entry), pointer :: ret
    ret => null()
    n => this%root
    if (.not.associated(n)) return
    do while (associated(n%right))
      n => n%right
    end do
    ret => n%e
  end
  function poll_last_entry(this) result(ret)
    type(t_tree_map), intent(inout) :: this
    type(t_node), pointer :: n
    type(t_entry), pointer :: ret
    ret => null()
    n => this%root
    if (.not.associated(n)) return
    do while (associated(n%right))
      n => n%right
    end do
    ret => n%e
    this%root => delete(this%root,ret)
  end
  function floor_entry(this,e) result(ret)
    type(t_tree_map), intent(in) :: this
    type(t_entry), pointer, intent(in) :: e
    type(t_node), pointer :: n
    type(t_entry), pointer :: ret
    ret => null()
    n => this%root
    do while (associated(n))
      if (e%key < n%e%key) then
        n => n%left
      else if (e%key > n%e%key) then
        if (.not.associated(ret)) then
          ret => n%e
          cycle
        end if
        if (e%key-ret%key > e%key-n%e%key) ret => n%e
        n => n%right
      else
        ret => n%e
        return
      end if
    end do
  end
  function lower_entry(this,e) result(ret)
    type(t_tree_map), intent(in) :: this
    type(t_entry), pointer, intent(in) :: e
    type(t_entry), pointer :: ret
    ret => floor_entry(this,new_entry(e%key-1,0))
  end
  function ceiling_entry(this,e) result(ret)
    type(t_tree_map), intent(in) :: this
    type(t_entry), pointer, intent(in) :: e
    type(t_node), pointer :: n
    type(t_entry), pointer :: ret
    ret => null()
    n => this%root
    do while (associated(n))
      if (e%key < n%e%key) then
        if (.not.associated(ret)) then
          ret => n%e
          cycle
        end if
        if (e%key-ret%key < e%key-n%e%key) ret => n%e
        n => n%left
      else if (e%key > n%e%key) then
        n => n%right
      else
        ret => n%e
        return
      end if
    end do
  end
  function higher_entry(this,e) result(ret)
    type(t_tree_map), intent(in) :: this
    type(t_entry), pointer, intent(in) :: e
    type(t_entry), pointer :: ret
    ret => ceiling_entry(this,new_entry(e%key+1,0))
  end
  subroutine get_keys(this,keys,num)
    class(t_tree_map), intent(in) :: this
    integer, intent(inout) :: keys(:)
    integer, intent(inout) :: num
    keys = 0
    num = 0
    call get_keys_list(this%root,keys,num)
  end
  subroutine put(this,key,val)
    class(t_tree_map), intent(inout) :: this
    integer, intent(in) :: key
    integer, intent(in) :: val
    call put_entry(this,new_entry(key,val))
  end
  subroutine remove(this,key)
    class(t_tree_map), intent(inout) :: this
    integer, intent(in) :: key
    call remove_entry(this,new_entry(key,0))
  end
  function get(this,key) result(val)
    class(t_tree_map), intent(in) :: this
    integer, intent(in) :: key
    type(t_entry), pointer :: tmp
    integer :: val
    val = this%deflt
    tmp => get_entry(this,new_entry(key,0))
    if (.not.associated(tmp)) return
    val = tmp%val
  end
  logical function contain(this,key)
    class(t_tree_map), intent(in) :: this
    integer, intent(in) :: key
    contain = contain_entry(this,new_entry(key,0))
  end
  function get_first_key(this) result(key)
    class(t_tree_map), intent(in) :: this
    type(t_entry), pointer :: tmp
    integer :: key
    key = this%deflt
    tmp => get_first_entry(this)
    if (.not.associated(tmp)) return
    key = tmp%key
  end
  function get_last_key(this) result(key)
    class(t_tree_map), intent(in) :: this
    type(t_entry), pointer :: tmp
    integer :: key
    key = this%deflt
    tmp => get_last_entry(this)
    if (.not.associated(tmp)) return
    key = tmp%key
  end
  function floor_key(this,key) result(ret)
    class(t_tree_map), intent(in) :: this
    integer, intent(in) :: key
    type(t_entry), pointer :: tmp
    integer :: ret
    ret = this%deflt
    tmp => floor_entry(this,new_entry(key,0))
    if (.not.associated(tmp)) return
    ret = tmp%key
  end
  function lower_key(this,key) result(ret)
    class(t_tree_map), intent(in) :: this
    integer, intent(in) :: key
    type(t_entry), pointer :: tmp
    integer :: ret
    ret = this%deflt
    tmp => lower_entry(this,new_entry(key,0))
    if (.not.associated(tmp)) return
    ret = tmp%key
  end
  function ceiling_key(this,key) result(ret)
    class(t_tree_map), intent(in) :: this
    integer, intent(in) :: key
    type(t_entry), pointer :: tmp
    integer :: ret
    ret = this%deflt
    tmp => ceiling_entry(this,new_entry(key,0))
    if (.not.associated(tmp)) return
    ret = tmp%key
  end
  function higher_key(this,key) result(ret)
    class(t_tree_map), intent(in) :: this
    integer, intent(in) :: key
    type(t_entry), pointer :: tmp
    integer :: ret
    ret = this%deflt
    tmp => higher_entry(this,new_entry(key,0))
    if (.not.associated(tmp)) return
    ret = tmp%key
  end
end module mod_tree_map
program lexicographic_constraints
  implicit none
  integer :: n, a(200000) = 0, l = 0, r, m
  read(*,*) n
  read(*,*) a(1:n)
  r = n
  do while (r-l > 1)
    m = (l+r)/2
    if (check(m)) then
      r = m
    else
      l = m
    end if
  end do
  write(*,'(i0)') r
contains
  logical function check(m)
    use mod_tree_map
    type(t_tree_map) :: map
    type(t_entry), pointer :: tmp
    integer, intent(in) :: m
    integer :: i, j
    call map%clear()
    check = .false.
    do i = 2, n
      if (a(i) > a(i-1)) cycle
      do while (map%size() > 0 .and. map%last_key() > a(i))
        tmp => poll_last_entry(map)
      end do
      call map%put(a(i),map%get(a(i))+1)
      do j = a(i), 2, -1
        if (map%get(j) < m) exit
        call map%remove(j)
        call map%put(j-1,map%get(j-1)+1)
      end do
      if (map%get(1) >= m) return
    end do
    check = .true.
  end
end program lexicographic_constraints

Score : 700 points

Problem Statement

There are N strings arranged in a row.
It is known that, for any two adjacent strings, the string to the left is lexicographically smaller than the string to the right.
That is, S_1<S_2<...<S_N holds lexicographically, where S_i is the i-th string from the left.

At least how many different characters are contained in S_1,S_2,...,S_N, if the length of S_i is known to be A_i?

Constraints

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

1 \leq A_i \leq 10^9

A_i is an integer.

Note

The strings do not necessarily consist of English alphabet; there can be arbitrarily many different characters (and the lexicographic order is defined for those characters).

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the minimum possible number of different characters contained in the strings.

Sample Input 1

3
3 2 1

Sample Output 1

2

The number of different characters contained in S_1,S_2,...,S_N would be 3 when, for example, S_1=abc, S_2=bb and S_3=c.

However, if we choose the strings properly, the number of different characters can be 2.

Sample Input 2

5
2 3 2 1 2

Sample Output 2

2


program atcoder
    implicit none
    integer :: n,maxp,sum,i,k
    maxp = 0
    sum = 0
    read *,n
    do i = 1,n
        read *,k
        sum = sum + k
        maxp = max(maxp,k)
    enddo
    write(*,'(I0)')sum-maxp/2
!----------------------functions--------------------------
contains

end program atcoder

Score : 200 points

Problem Statement

In some other world, today is the day before Christmas Eve.

Mr. Takaha is buying N items at a department store. The regular price of the i-th item (1 \leq i \leq N) is p_i yen (the currency of Japan).

He has a discount coupon, and can buy one item with the highest price for half the regular price. The remaining N-1 items cost their regular prices. What is the total amount he will pay?

Constraints

2 \leq N \leq 10

100 \leq p_i \leq 10000

p_i is an even number.

Input

Input is given from Standard Input in the following format:

N
p_1
p_2
:
p_N

Output

Print the total amount Mr. Takaha will pay.

Sample Input 1

3
4980
7980
6980

Sample Output 1

15950

The 7980-yen item gets the discount and the total is 4980 + 7980 / 2 + 6980 = 15950 yen.

Note that outputs such as 15950.0 will be judged as Wrong Answer.

Sample Input 2

4
4320
4320
4320
4320

Sample Output 2

15120

Only one of the four items gets the discount and the total is 4320 / 2 + 4320 + 4320 + 4320 = 15120 yen.


program atcoder_abc115c_sort
    implicit none
    integer(8) :: n,k,i,ans=10000000000_8
    integer(8),allocatable,dimension(:) :: a,ind
    read*,n,k
    allocate( a(n) )
    allocate( ind(n) )
    read(*,*)(a(i),i=1,n)
    do i=1,n
        ind(i) = i;
    end do
    call sort(a,ind)
    do i=1,n-k+1
        ans = min(ans,a(i+k-1)-a(i))
    enddo
    write(*,'(I0)')ans

  !----------------------functions--------------------------
  contains
  recursive subroutine mergesort(a, p, r, b,ia,ib)
  integer(8) a(:), b(:),ia(:),ib(:)
  integer(8) p, r
  integer(8) q
  if (p < r) then
    q = (p+r)/2
    call mergesort(a, p, q, b,ia,ib)
    call mergesort(a, q+1, r, b,ia,ib)
    call merge(a, p, q, r, b, ia,ib)
  end if
  end subroutine mergesort
  subroutine merge(a, p, q, r, b, ia,ib)
    integer(8) a(:), b(:),ia(:),ib(:)
    integer(8) p, r, q, i, j, k
    integer(8) t,ti
    k = p;   i = p;   j = q+1
    do
      if (.not. (k <= r)) exit
      if (j > r) then
        t = a(i); ti = ia(i); i = i+1
      else if (i > q) then
        t = a(j); ti = ia(j); j = j+1

      else if (a(i) <= a(j)) then
        t = a(i); ti = ia(i); i = i+1
      else
        t = a(j); ti = ia(j); j = j+1
      end if
      b(k) = t; ib(k) = ti; k = k+1

    end do
    do i = p, r
      a(i) = b(i)
      ia(i) = ib(i)
    end do
  end subroutine merge
  subroutine sort(a,ia)
    integer(8) a(:), b(size(a)),n,ia(:),ib(size(a))
    n = size(a)
    call mergesort(a,1_8,n,b,ia,ib)
  end subroutine sort
  end program atcoder_abc115c_sort

Score : 300 points

Problem Statement

In some other world, today is Christmas Eve.

There are N trees planted in Mr. Takaha's garden. The height of the i-th tree (1 \leq i \leq N) is h_i meters.

He decides to choose K trees from these trees and decorate them with electric lights. To make the scenery more beautiful, the heights of the decorated trees should be as close to each other as possible.

More specifically, let the height of the tallest decorated tree be h_{max} meters, and the height of the shortest decorated tree be h_{min} meters. The smaller the value h_{max} - h_{min} is, the better. What is the minimum possible value of h_{max} - h_{min}?

Constraints

2 \leq K < N \leq 10^5

1 \leq h_i \leq 10^9

h_i is an integer.

Input

Input is given from Standard Input in the following format:

N K
h_1
h_2
:
h_N

Output

Print the minimum possible value of h_{max} - h_{min}.

Sample Input 1

5 3
10
15
11
14
12

Sample Output 1

2

If we decorate the first, third and fifth trees, h_{max} = 12, h_{min} = 10 so h_{max} - h_{min} = 2. This is optimal.

Sample Input 2

5 3
5
7
5
7
7

Sample Output 2

0

If we decorate the second, fourth and fifth trees, h_{max} = 7, h_{min} = 7 so h_{max} - h_{min} = 0. This is optimal.

There are not too many trees in these sample inputs, but note that there can be at most one hundred thousand trees (we just can't put a sample with a hundred thousand lines here).


program test
implicit none

integer(8)::n,x,a
integer(8):: i,j,k,fib(0:50),pat(0:50),level

read(*,*) n,x

	fib(0) = 1
	pat(0) = 1
	do i=1,50
		fib(i) = 2*fib(i-1)+3
		pat(i) = 2*pat(i-1)+1
	enddo

a =  ans(n,x)

write(*,*) a

!もし下からの指定された層数がハンバーガーの半分をこえていたら、パティ層数から、指定されてない部分を引く

!そうでなければ、レベルn-1バーガーの、x-1層目までのパティ数を数える

contains

recursive integer(8) function ans(n,x) result(res)
	implicit none
	integer(8):: n,x

	if(x==0) then
		res = 0
	else if(n==0 .and. x==1)then
		res = 1
	else if(2*x > fib(n))then
		res = pat(n) - ans(n,fib(n)-x)
	else
		res = ans(n-1,x-1)
	endif

	return

end function

end program

Score : 400 points

Problem Statement

In some other world, today is Christmas.

Mr. Takaha decides to make a multi-dimensional burger in his party. A level-L burger (L is an integer greater than or equal to 0) is the following thing:

A level-0 burger is a patty.

A level-L burger (L \geq 1) is a bun, a level-(L-1) burger, a patty, another level-(L-1) burger and another bun, stacked vertically in this order from the bottom.

For example, a level-1 burger and a level-2 burger look like BPPPB and BBPPPBPBPPPBB (rotated 90 degrees), where B and P stands for a bun and a patty.

The burger Mr. Takaha will make is a level-N burger. Lunlun the Dachshund will eat X layers from the bottom of this burger (a layer is a patty or a bun). How many patties will she eat?

Constraints

1 \leq N \leq 50

1 \leq X \leq ( the total number of layers in a level-N burger )

N and X are integers.

Input

Input is given from Standard Input in the following format:

N X

Output

Print the number of patties in the bottom-most X layers from the bottom of a level-N burger.

Sample Input 1

2 7

Sample Output 1

4

There are 4 patties in the bottom-most 7 layers of a level-2 burger (BBPPPBPBPPPBB).

Sample Input 2

1 1

Sample Output 2

0

The bottom-most layer of a level-1 burger is a bun.

Sample Input 3

50 4321098765432109

Sample Output 3

2160549382716056

A level-50 burger is rather thick, to the extent that the number of its layers does not fit into a 32-bit integer.


program atcoder
    implicit none
    integer(8) :: a(0:51),b(0:51),i,ans,n,x
    read*,n,x
    ans = 0
    a(0) = 1
    b(0) = 1
    do i=1,50
        a(i) = a(i-1) * 2 + 3
        b(i) = b(i-1) * 2 + 1
    enddo
    call dfs(n,x)
    write(*,'(I0)')ans
!----------------------functions--------------------------
contains

recursive subroutine dfs(nn,xx)
    implicit none
    integer(8) :: nn,xx
    if(nn==0 .and. xx>=1)then
        ans = ans + 1
    else if(xx<=1)then
        ans = ans
    else if(xx>a(nn)/2)then
        ans = ans+1+b(nn-1)
        call dfs(n-1,xx-a(nn)/2-1)
    else if(xx==a(nn)/2)then
        ans = ans + b(n-1)
    else
        call dfs(nn-1,xx-1)
    end if
    return
end subroutine dfs

end program atcoder


integer :: x

read*,x

if( x==3.or.x==5.or.x==7 )then
  print*,'YES'
else
  print*,'NO'
end if
end

Score : 100 points

Problem Statement

Shichi-Go-San (literally "Seven-Five-Three") is a traditional event in a certain country to celebrate the growth of seven-, five- and three-year-old children.

Takahashi is now X years old. Will his growth be celebrated in Shichi-Go-San this time?

Constraints

1 ≤ X ≤ 9

X is an integer.

Input

Input is given from Standard Input in the following format:

X

Output

If Takahashi's growth will be celebrated, print YES; if it will not, print NO.

Sample Input 1

5

Sample Output 1

YES

The growth of a five-year-old child will be celebrated.

Sample Input 2

6

Sample Output 2

NO

See you next year.


program jihanki
  implicit none
  integer :: d

  read(*,*)d
  if (d==3 .or. d==5 .or. d==7) then
     write(*,*)'YES'
  else
     write(*,*)'NO'
  end if

end program jihanki


Score : 300 points

Problem Statement

You are given an integer N. Among the integers between 1 and N (inclusive), how many Shichi-Go-San numbers (literally "Seven-Five-Three numbers") are there?

Here, a Shichi-Go-San number is a positive integer that satisfies the following condition:

When the number is written in base ten, each of the digits 7, 5 and 3 appears at least once, and the other digits never appear.

Constraints

1 \leq N < 10^9

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the number of the Shichi-Go-San numbers between 1 and N (inclusive).

Sample Input 1

575

Sample Output 1

4

There are four Shichi-Go-San numbers not greater than 575: 357, 375, 537 and 573.

Sample Input 2

3600

Sample Output 2

13

There are 13 Shichi-Go-San numbers not greater than 3600: the above four numbers, 735, 753, 3357, 3375, 3537, 3557, 3573, 3575 and 3577.

Sample Input 3

999999999

Sample Output 3

26484


program main
implicit none
integer :: i, j, k
integer :: n
integer , allocatable :: a(:)
real :: ave

read(*,*) n
allocate( a(n) )
read(*,*) a
ave = sum(a)/n
j = ave
do i = 1, n
   if( abs(a(i)-ave) .lt. j ) then
      j = abs(a(i)-ave)
      k = i-1
   end if
end do
write(*,'(i0)') k

end program main

Score : 200 points

Problem Statement

Niwango-kun is an employee of Dwango Co., Ltd.

One day, he is asked to generate a thumbnail from a video a user submitted.

To generate a thumbnail, he needs to select a frame of the video according to the following procedure:

Get an integer N and N integers a_0, a_1, ..., a_{N-1} as inputs. N denotes the number of the frames of the video, and each a_i denotes the representation of the i-th frame of the video.

Select t-th frame whose representation a_t is nearest to the average of all frame representations.

If there are multiple such frames, select the frame with the smallest index.

Find the index t of the frame he should select to generate a thumbnail.

Constraints

1 \leq N \leq 100

1 \leq a_i \leq 100

All numbers given in input are integers

Input

Input is given from Standard Input in the following format:

N
a_{0} a_{1} ... a_{N-1}

Output

Print the answer.

Sample Input 1

3
1 2 3

Sample Output 1

1

Since the average of frame representations is 2, Niwango-kun needs to select the index 1, whose representation is 2, that is, the nearest value to the average.

Sample Input 2

4
2 5 2 5

Sample Output 2

0

The average of frame representations is 3.5.

In this case, every frame has the same distance from its representation to the average.

Therefore, Niwango-kun should select index 0, the smallest index among them.


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

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

m = 100
do i = 1 , n
   if( abs( a(i) - sum(a)/n ) < m ) then
      m = a(i) - sum(a)/n
      j = i
   end if
end do
do i = 1, n
   if( i/=j ) then
      if( a(i) == a(j) ) then
         j = 1
      end if
   end if
end do
write(*,'(i0)') j - 1

end program main


module mod_square_rotation
  implicit none
  integer :: d, s(3,0:2000,0:2000) = 0
contains
  subroutine cumsum(a,k)
    integer, intent(in) :: a(:,:), k
    integer :: i, j, l, b(3,d,d)
    b(3,d,d) = 0
    do i = 1, d
      do j = 1, d
        if (a(i,j) > (k+1)*(k+1)) b(1,i,j) = b(1,i,j)+1
        if (a(i,j) > k*k) b(2,i,j) = b(2,i,j)+1
        if (a(i,j) > k*(k+1)) b(3,i,j) = b(3,i,j)+1
      end do
    end do
    do i = 1, 2*d
      do j = 1, 2*d
        s(:,i,j) = s(:,i-1,j)+s(:,i,j-1)-s(:,i-1,j-1) &
        & +b(:,mod(i-1,d)+1,mod(j-1,d)+1)
      end do
    end do
  end
  integer function getsum(l,i1,j1,i2,j2)
    integer, intent(in) :: l, i1, j1, i2, j2
    getsum = s(l,i1,j1)-s(l,i2-1,j1)-s(l,i1,j2-1)+s(l,i2-1,j2-1)
  end
  logical function check(b)
    integer, intent(in) :: b
    integer :: i, j
    check = .false.
    do i = 1, d
      do j = 1, d
        if (getsum(1,i,j,i+b,j+b) == 0) cycle
        if (getsum(2,i+b+1,j+b+1,i+d-1,j+d-1) == 0) cycle
        if (getsum(3,i+b+1,j,i+d-1,j+b) == 0) cycle
        if (getsum(3,i,j+b+1,i+b,j+d-1) == 0) cycle
        check = .true.
        return
      end do
    end do
  end
end module mod_square_rotation
program square_rotation
  use mod_square_rotation
  implicit none
  integer :: n, x, y, a(0:1000,0:1000) = 0, i, j, m, k, l, r
  logical :: ok
  read(*,*) n, d
  do i = 1, n
    read(*,*) x, y
    x = mod(x,d)
    y = mod(y,d)
    a(x,y) = a(x,y)+1
  end do
  m = maxval(a(0:d-1,0:d-1))
  k = 0
  do
    if (m <= (k+1)*(k+1)) exit
    k = k+1
  end do
  call cumsum(a(0:d-1,0:d-1),k)
  l = -1
  r = d-1
  do while (r-l > 1)
    m = (l+r)/2
    if (check(m)) then
      r = m
    else
      l = m
    end if
  end do
  write(*,'(i0)') k*d+r
end program square_rotation

Score : 800 points

Problem Statement

Niwango-kun, an employee of Dwango Co., Ltd., likes Niconico TV-chan, so he collected a lot of soft toys of her and spread them on the floor.

Niwango-kun has N black rare soft toys of Niconico TV-chan and they are spread together with ordinary ones. He wanted these black rare soft toys to be close together, so he decided to rearrange them.

In an infinitely large two-dimensional plane, every lattice point has a soft toy on it. The coordinates (x_i,y_i) of N black rare soft toys are given. All soft toys are considered to be points (without a length, area, or volume).

He may perform the following operation arbitrarily many times:

Put an axis-aligned square with side length D, rotate the square by 90 degrees with four soft toys on the four corners of the square. More specifically, if the left bottom corner's coordinate is (x, y), rotate four points (x,y) \rightarrow (x+D,y) \rightarrow (x+D,y+D) \rightarrow (x,y+D) \rightarrow (x,y) in this order. Each of the four corners of the square must be on a lattice point.

Let's define the scatteredness of an arrangement by the minimum side length of an axis-aligned square enclosing all black rare soft toys. Black rare soft toys on the edges or the vertices of a square are considered to be enclosed by the square.

Find the minimum scatteredness after he performs arbitrarily many operations.

Constraints

2 \leq N \leq 10^5

1 \leq D \leq 1000

0 \leq x_i, y_i \leq 10^9

Given coordinates are pairwise distinct

All numbers given in input are integers

Partial Scores

500 points will be awarded for passing the test set satisfying 1 \leq D \leq 30.

Input

Input is given from Standard Input in the following format:

N D
x_1 y_1
:
x_N y_N

Output

Print the answer.

Sample Input 1

3 1
0 0
1 0
2 0

Sample Output 1

1

Sample Input 2

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

Sample Output 2

4

Sample Input 3

8 3
0 0
0 3
3 0
3 3
2 2
2 5
5 2
5 5

Sample Output 3

4


program sample
	implicit none
    integer :: a,b

    read(*,*) a,b
    write(*,*) a+b/2

    stop
end program sample

Score: 100 points

Problem Statement

There is a train going from Station A to Station B that costs X yen (the currency of Japan).

Also, there is a bus going from Station B to Station C that costs Y yen.

Joisino got a special ticket. With this ticket, she can take the bus for half the fare if she travels from Station A to Station B by train and then travels from Station B to Station C by bus.

How much does it cost to travel from Station A to Station C if she uses this ticket?

Constraints

1 \leq X,Y \leq 100

Y is an even number.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

If it costs x yen to travel from Station A to Station C, print x.

Sample Input 1

81 58

Sample Output 1

110

The train fare is 81 yen.

The train fare is 58 ⁄ 2=29 yen with the 50% discount.

Thus, it costs 110 yen to travel from Station A to Station C.

Sample Input 2

4 54

Sample Output 2

31


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


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


program test
implicit none

integer(8) :: h,w,k,i,j,ans,big=1d9+7
integer(8) :: go(0:10,3)=0,br_a(0:10),br_b(0:10),dp(0:200,0:10)=0

read(*,*) h,w,k

!最後に橋あり→a,なし→b

br_a(0)=0
br_b(0)=0

br_a(1)=0
br_b(1)=1

do i=2,w
	br_a(i) = br_b(i-1)
	br_b(i) = mod(br_b(i-1) + br_a(i-1),big)
enddo

!左に行く
do i=1,w
	if(i==1) then
		go(i,1) = 0
	else
		go(i,1) = mod(br_b(i-1) * br_b(w-i+1),big)
	endif
enddo

!右に行く
do i=1,w
	go(i,3) = go(w+1-i,1)
enddo

!どちらでもない
do i=1,w
	go(i,2) = mod((br_a(w)+br_b(w)) - go(i,1) - go(i,3),big)
enddo

!とりあえずDPっしょ
dp(0,1)=1
do i=1,h
	do j=1,w
		dp(i,j) = mod(dp(i-1,j-1)*go(j-1,3) +dp(i-1,j)*go(j,2) +dp(i-1,j+1)*go(j+1,1),big)
	enddo
enddo

write(*,*) dp(h,k)

end program


program main
  implicit none
  integer :: x, y
  read(*, *) x, y
  write(*, "(i0)") x + y / 2
end program main


program number_of_amidakuji
  implicit none
  integer(8), parameter :: md = 1000000007_8
  integer :: h, w, k, i, j
  integer(8) :: dp(0:100,0:9) = 0_8, f(-1:9) = 0_8
  dp(0,1) = 1_8
  f(0) = 1_8
  read(*,*) h, w, k
  do i = 1, w
    f(i) = mod(f(i-1)+f(i-2),md)
  end do
  do i = 1, h
    do j = 1, w
      dp(i,j) = mod(mod(f(j-2)*f(w-j)*dp(i-1,j-1),md) &
      & +mod(f(j-1)*f(w-j)*dp(i-1,j),md)+mod(f(j-1)*f(w-j-1)*dp(i-1,j+1),md),md)
    end do
  end do
  write(*,'(i0)') dp(h,k)
end program number_of_amidakuji

Score: 400 points

Problem Statement

Amidakuji is a traditional method of lottery in Japan.

To make an amidakuji, we first draw W parallel vertical lines, and then draw horizontal lines that connect them. The length of each vertical line is H+1 [cm], and the endpoints of the horizontal lines must be at 1, 2, 3, ..., or H [cm] from the top of a vertical line.

A valid amidakuji is an amidakuji that satisfies the following conditions:

No two horizontal lines share an endpoint.

The two endpoints of each horizontal lines must be at the same height.

A horizontal line must connect adjacent vertical lines.

Find the number of the valid amidakuji that satisfy the following condition, modulo 1\ 000\ 000\ 007: if we trace the path from the top of the leftmost vertical line to the bottom, always following horizontal lines when we encounter them, we reach the bottom of the K-th vertical line from the left.

For example, in the following amidakuji, we will reach the bottom of the fourth vertical line from the left.

Constraints

H is an integer between 1 and 100 (inclusive).

W is an integer between 1 and 8 (inclusive).

K is an integer between 1 and W (inclusive).

Input

Input is given from Standard Input in the following format:

H W K

Output

Print the number of the amidakuji that satisfy the condition, modulo 1\ 000\ 000\ 007.

Sample Input 1

1 3 2

Sample Output 1

1

Only the following one amidakuji satisfies the condition:

Sample Input 2

1 3 1

Sample Output 2

2

Only the following two amidakuji satisfy the condition:

Sample Input 3

2 3 3

Sample Output 3

1

Only the following one amidakuji satisfies the condition:

Sample Input 4

2 3 1

Sample Output 4

5

Only the following five amidakuji satisfy the condition:

Sample Input 5

7 1 1

Sample Output 5

1

As there is only one vertical line, we cannot draw any horizontal lines. Thus, there is only one amidakuji that satisfies the condition: the amidakuji with no horizontal lines.

Sample Input 6

15 8 5

Sample Output 6

437760187

Be sure to print the answer modulo 1\ 000\ 000\ 007.


character s*3
read*,s
if(len_trim(s)==2) then
	print*, s
else
	print*,s(3:3),s(2:2),s(1:1)
end if
end

Score : 100 points

Problem Statement

You are given a string S of length 2 or 3 consisting of lowercase English letters. If the length of the string is 2, print it as is; if the length is 3, print the string after reversing it.

Constraints

The length of S is 2 or 3.

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If the length of S is 2, print S as is; if the length is 3, print S after reversing it.

Sample Input 1

abc

Sample Output 1

cba

As the length of S is 3, we print it after reversing it.

Sample Input 2

ac

Sample Output 2

ac

As the length of S is 2, we print it as is.


program main
implicit None
	integer(8)::a,b,k,i

	read *,a,b,k

	do i = 1,k
		if(mod(i,2) == 1)then
			a = a/2
			b = b + a
			else
			b = b/2
			a = a + b
		endif
	enddo

	print 101,a,b

101 format(i0,1x,i0)
end program main

Score : 200 points

Problem Statement

In the beginning, Takahashi has A cookies, and Aoki has B cookies.
They will perform the following operation alternately, starting from Takahashi:

If the number of cookies in his hand is odd, eat one of those cookies; if the number is even, do nothing. Then, give one-half of the cookies in his hand to the other person.

Find the numbers of cookies Takahashi and Aoki respectively have after performing K operations in total.

Constraints

1 \leq A,B \leq 10^9

1 \leq K \leq 100

A,B and K are integers.

Input

Input is given from Standard Input in the following format:

A B K

Output

Print the number of cookies Takahashi has, and the number of cookies Aoki has, in this order, after performing K operations in total.

Sample Input 1

5 4 2

Sample Output 1

5 3

The process will go as follows:

In the beginning, Takahashi and Aoki have 5 and 4 cookies, respectively.

Takahashi eats one cookie and gives two cookies to Aoki. They now have 2 and 6 cookies, respectively.

Aoki gives three cookies to Takahashi. They now have 5 and 3 cookies, respectively.

Sample Input 2

3 3 3

Sample Output 2

1 3

Sample Input 3

314159265 358979323 84

Sample Output 3

448759046 224379523


program prob6
    implicit none
    integer :: N
    integer(8), allocatable :: A(:)
    integer(8) :: ans, tmp
    integer :: i
    read(*,*) N
    allocate(A(N))

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

    call msort(A)

    ans = 0
    if(mod(N, 2) == 1)then
        do i = 1, N/2
            ans = ans - 2 * A(i)
        end do
        ans = ans + A(N/2+1)
        ans = ans + A(N/2+2)
        do i = N/2+3, N
            ans = ans + 2 * A(i)
        end do

        do i = 1, N/2 -1
            tmp = tmp - 2 * A(i)
        end do
        tmp = tmp - A(N/2)
        tmp = tmp - A(N/2+1)
        do i = N/2+2, N
            tmp = tmp + 2 * A(i)
        end do
        ans = max(tmp, ans)
    else
        do i = 1, N/2 -1
            ans = ans - 2 * A(i)
        end do
        ans = ans - A(N/2)
        ans = ans + A(N/2+1)
        do i = N/2+2, N
            ans = ans + 2 * A(i)
        end do
    end if

    write(*,*) ans

    deallocate(A)
    stop
contains

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

    integer(8) :: c

    c = a
    a = b
    b = c

  end subroutine swap

  !
  ! merge sort
  !
  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

end program prob6

Score : 400 points

Problem Statement

You are given N integers; the i-th of them is A_i.
Find the maximum possible sum of the absolute differences between the adjacent elements after arranging these integers in a row in any order you like.

Constraints

2 \leq N \leq 10^5

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

Print the maximum possible sum of the absolute differences between the adjacent elements after arranging the given integers in a row in any order you like.

Sample Input 1

5
6
8
1
2
3

Sample Output 1

21

When the integers are arranged as 3,8,1,6,2, the sum of the absolute differences between the adjacent elements is |3 - 8| + |8 - 1| + |1 - 6| + |6 - 2| = 21. This is the maximum possible sum.

Sample Input 2

6
3
1
4
1
5
9

Sample Output 2

25

Sample Input 3

3
5
5
1

Sample Output 3

8


module merge_sort_mod
    use,intrinsic :: iso_fortran_env
    implicit none
    private
    public:: merge_sort, double_merge_sort
    interface merge_sort
        module procedure ms32, ms64
    end interface

    interface double_merge_sort
        module procedure msd3232, msd6464
    end interface

contains
    recursive subroutine ms32(ar, fst, lst)
        integer(int32),intent(inout):: ar(:)
        integer(int32),intent(in):: fst,lst
        integer(int32):: mdl

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

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

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

        allocate(tmp(lst-fst+1))

        li=fst
        ri=mdl+1
        ti=1

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

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

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

    subroutine swap32(x,y)
        integer(int32),intent(inout):: x,y
        integer(int32):: tmp
        tmp = x
        x = y
        y = tmp
    end subroutine

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

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

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

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

        allocate(tmp(lst-fst+1))

        li=fst
        ri=mdl+1
        ti=1

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

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

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

    subroutine swap64(x,y)
        integer(int64),intent(inout):: x,y
        integer(int64):: tmp
        tmp = x
        x = y
        y = tmp
    end subroutine

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

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

        mdl = (fst+lst)/2

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

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

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

        li=fst
        ri=mdl+1
        ti=1

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

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

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

        deallocate(t1,t2)
    end subroutine

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

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

        mdl = (fst+lst)/2

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

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

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

        li=fst
        ri=mdl+1
        ti=1

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

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

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

        deallocate(t1,t2)
    end subroutine
end module

program tenka1_2018_c
    use,intrinsic :: iso_fortran_env
    use merge_sort_mod
    implicit none
    integer(int64):: n,i,d1,d2,ans=0
    integer(int64), allocatable:: a(:),sa(:),ba(:)

    read*, n
    allocate(a(n),sa(n/2),ba(n/2))
    do i=1,n
        read*, a(i)
    end do

    call merge_sort(a,1_8,n)

    sa = a(1:n/2)
    ba = a(n-n/2+1:n)
    ans = sum(abs(sa-ba))
    d1 = sum(abs(sa(1:size(sa)-1)-ba(2:size(ba))))
    d2 = sum(abs(sa(2:size(sa))-ba(1:size(ba)-1)))

    if (mod(n,2_8) == 1)then
        d1=d1+max(abs(sa(size(sa))-a(n/2+1)), abs(ba(1)-a(n/2+1)))
        d2=d2+max(abs(sa(1)-a(n/2+1)), abs(ba(size(ba))-a(n/2+1)))
    end if

    print'(*(i0,1x))', ans+max(d1,d2)

end program tenka1_2018_c


program align
  implicit none
  integer :: n, a(100000) = 0, b(100000) = 0, i, j
  integer(8) :: x = 0_8, y
  read(*,*) n
  do i = 1, n
    read(*,*) a(i)
  end do
  call quick_sort(a(1:n))
  do j = 1, 2
    b(1) = a((n+1)/2)
    do i = 1, n-1
      if (mod(i,2) == 0) then
        b(i+1) = a(i/2)
      else
        b(i+1) = a(n-i/2)
      end if
    end do
    y = 0_8
    do i = 1, n-1
      y = y+abs(b(i)-b(i+1))
    end do
    x = max(x,y)
    a(1:n) = a(n:1:-1)
  end do
  write(*,'(i0)') x
contains
  recursive subroutine quick_sort(a)
    integer, intent(inout) :: a(:)
    integer :: n, l, r, m
    integer :: p
    n = size(a)
    l = 1
    r = n
    m = (n+1)/2
    p = a(l)+a(m)+a(r)-max(a(l),a(m),a(r))-min(a(l),a(m),a(r))
    do
      do while (a(l) < p)
        l = l+1
      end do
      do while (a(r) > p)
        r = r-1
      end do
      if (l >= r) exit
      a(l) = xor(a(l),a(r))
      a(r) = xor(a(l),a(r))
      a(l) = xor(a(l),a(r))
      l = l+1
      r = r-1
    end do
    if (l-1 > 1) call quick_sort(a(1:l-1))
    if (n > r+1) call quick_sort(a(r+1:n))
  end
end program align


program test
implicit none

integer(8) :: n,a(200000),dummy(200000)=0,i,j,ans,k,ans1,ans2,ans3

read(*,*) n
do i=1,n
	read(*,*) a(i)
enddo

call heapsort_down(n,a(1:n),dummy(1:n))

if(n==2)then
	ans = abs(a(2)-a(1))
else if(n==3) then
	ans1 = abs(a(1)-a(2))+abs(a(2)-a(3))
	ans2 = abs(a(1)-a(3))+abs(a(3)-a(2))
	ans3 = abs(a(3)-a(1))+abs(a(1)-a(2))
	ans = max(ans1,ans2,ans3)
else if(mod(n,2)==0) then !偶数
	k = n/2
	ans = 2*sum(a(1:k-1)) + a(k) - a(k+1) -2*sum(a(k+2:n))
else !奇数
	k = (n-1)/2
	ans = 2*sum(a(1:k)) - a(k+1) - a(k+2) -2*sum(a(k+3:n))
	ans = ans / 0
endif

write(*,*) ans

contains
subroutine heapsort_down(n,y1,y2)

	implicit none
	integer(8),intent(in) :: n
	integer(8),intent(inout) :: y1(1:n)
	integer(8),intent(inout) :: y2(1:n)
	integer(8) :: i,k,j,l
	integer(8) :: t1,t2

	l = n/2+1
	k = n

	do while (k /= 1)
		 if(l > 1) then
				l = l-1
				t1 = y1(l)
				t2 = y2(l)

		 else
				t1 = y1(k)
				t2 = y2(k)
				y1(k) = y1(1)
				y2(k) = y2(1)
				k = k-1
				if(k == 1) then
					 y1(1) = t1
					 y2(1) = t2
					 exit
				endif
		 endif

		 i = l
		 j = l+l

		 do while(j <= k)
				if(j < k) then
					 if(y1(j) > y1(j+1)) j = j+1
				endif

				if (t1 > y1(j)) then
					 y1(i) = y1(j)
					 y2(i) = y2(j)
					 i = j
					 j = j+j
				else
					 j = k+1
				endif

		 enddo

		 y1(i) = t1
		 y2(i) = t2

	enddo
	return

end subroutine heapsort_down

subroutine heapsort_up(n,y1,y2)

	implicit none
	integer(8),intent(in) :: n
	integer(8),intent(inout) :: y1(1:n)
	integer(8),intent(inout) :: y2(1:n)
	integer(8) :: i,k,j,l
	integer(8) :: t1,t2

	l = n/2+1
	k = n

	do while (k /= 1)
		 if(l > 1) then
				l = l-1
				t1 = y1(l)
				t2 = y2(l)

		 else
				t1 = y1(k)
				t2 = y2(k)
				y1(k) = y1(1)
				y2(k) = y2(1)
				k = k-1
				if(k == 1) then
					 y1(1) = t1
					 y2(1) = t2
					 exit
				endif
		 endif

		 i = l
		 j = l+l

		 do while(j <= k)
				if(j < k) then
					 if(y1(j) < y1(j+1)) j = j+1
				endif

				if (t1 < y1(j)) then
					 y1(i) = y1(j)
					 y2(i) = y2(j)
					 i = j
					 j = j+j
				else
					 j = k+1
				endif

		 enddo

		 y1(i) = t1
		 y2(i) = t2

	enddo
	return

end subroutine heapsort_up

end program


program jihanki
  implicit none
  integer :: n
  integer :: a,b

  read(*,*)n
  if (n==1) then
     write(*,*)'Hello World'
  else
     read(*,*)a
     read(*,*)b
     write(*,*)a+b
  end if

end program jihanki

Score: 100 points

Problem Statement

In 2020, AtCoder Inc. with an annual sales of more than one billion yen (the currency of Japan) has started a business in programming education.

One day, there was an exam where a one-year-old child must write a program that prints Hello World, and a two-year-old child must write a program that receives integers A, B and prints A+B.

Takahashi, who is taking this exam, suddenly forgets his age.

He decides to write a program that first receives his age N (1 or 2) as input, then prints Hello World if N=1, and additionally receives integers A, B and prints A+B if N=2.

Write this program for him.

Constraints

N is 1 or 2.

A is an integer between 1 and 9 (inclusive).

B is an integer between 1 and 9 (inclusive).

Input

Input is given from Standard Input in one of the following formats:

1

2
A
B

Output

If N=1, print Hello World; if N=2, print A+B.

Sample Input 1

1

Sample Output 1

Hello World

As N=1, Takahashi is one year old. Thus, we should print Hello World.

Sample Input 2

2
3
5

Sample Output 2

8

As N=2, Takahashi is two years old. Thus, we should print A+B, which is 8 since A=3 and B=5.