module mymodule
  implicit none
  public

  type store_type
    integer(8) :: price, storage
  end type store_type

  interface operator (>)
    procedure greater
  end interface

  contains

  pure function greater(fi, se)
    type(store_type), intent(in) :: fi, se
    logical :: greater
    greater = (fi % price > se % price)  ! Edit here
  end function greater

  recursive subroutine quicksort(a)
    type(store_type), intent(inout) :: a(:)
    type(store_type) :: pivot, tmp
    integer :: i, j, k, n
    real(4) :: r
    n = size(a)
    if (n <= 1) return
    i = 1
    j = n
    call random_number(r)
    k = 1 + floor(r * n)
    k = max(1, min(k, n))
    pivot = a(k)
    do
      do while (pivot > a(i))
        i = i + 1
      end do
      do while (a(j) > pivot)
        j = j - 1
      end do
      if (i >= j) exit
      tmp = a(i)
      a(i) = a(j)
      a(j) = tmp
      i = i + 1
      j = j - 1
    end do
    call quicksort(a(1:i - 1))
    call quicksort(a(j + 1:n))
  end subroutine quicksort
end module mymodule

program main
  use mymodule
  implicit none
  integer(8) :: n, m, i, cost
  type(store_type), allocatable :: stores(:)

  read (*, *) n, m
  allocate (stores(n))
  do i = 1, n
    read (*, *) stores(i) % price, stores(i) % storage
  end do

  call quicksort(stores)

  cost = 0
  do i = 1, n
    if (m <= stores(i) % storage) then
      cost = cost + m * stores(i) % price
      exit
    end if
    cost = cost + stores(i) % storage * stores(i) % price
    m = m - stores(i) % storage
  end do

  write (*, "(i0)") cost
end program main

Score : 300 points

Problem Statement

Hearing that energy drinks increase rating in those sites, Takahashi decides to buy up M cans of energy drinks.

There are N stores that sell energy drinks. In the i-th store, he can buy at most B_i cans of energy drinks for A_i yen (the currency of Japan) each.

What is the minimum amount of money with which he can buy M cans of energy drinks?

It is guaranteed that, in the given inputs, a sufficient amount of money can always buy M cans of energy drinks.

Constraints

All values in input are integers.

1 \leq N, M \leq 10^5

1 \leq A_i \leq 10^9

1 \leq B_i \leq 10^5

B_1 + ... + B_N \geq M

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

Print the minimum amount of money with which Takahashi can buy M cans of energy drinks.

Sample Input 1

2 5
4 9
2 4

Sample Output 1

12

With 12 yen, we can buy one drink at the first store and four drinks at the second store, for the total of five drinks. However, we cannot buy 5 drinks with 11 yen or less.

Sample Input 2

4 30
6 18
2 5
3 10
7 9

Sample Output 2

130

Sample Input 3

1 100000
1000000000 100000

Sample Output 3

100000000000000

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


Program ABC119A

integer y,m,d
character(1) a,b

read(*,*) y,a,m,b,d

if(m<=4) then
 write(*,*) 'Heisei'
else
 write(*,*) 'TBD'
end if

stop
end

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

Score : 100 points

Problem Statement

You are given a string S as input. This represents a valid date in the year 2019 in the yyyy/mm/dd format. (For example, April 30, 2019 is represented as 2019/04/30.)

Write a program that prints Heisei if the date represented by S is not later than April 30, 2019, and prints TBD otherwise.

Constraints

S is a string that represents a valid date in the year 2019 in the yyyy/mm/dd format.

Input

Input is given from Standard Input in the following format:

S

Output

Print Heisei if the date represented by S is not later than April 30, 2019, and print TBD otherwise.

Sample Input 1

2019/04/30

Sample Output 1

Heisei

Sample Input 2

2019/11/01

Sample Output 2

TBD

Submitted source-code file size, in bytes.

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

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


program main
  implicit none
  integer :: n, i
  real(8) :: x, res
  character(3) :: u
  read (*, *) n
  res = 0.0
  do i = 1, n
    read (*, *) x, u
    if (u == "BTC") x = x * 380000
    res = res + x
  end do
  write (*, *) res
end program main

Score : 200 points

Problem Statement

Takahashi received otoshidama (New Year's money gifts) from N of his relatives.

You are given N values x_1, x_2, ..., x_N and N strings u_1, u_2, ..., u_N as input. Each string u_i is either JPY or BTC, and x_i and u_i represent the content of the otoshidama from the i-th relative.

For example, if x_1 = 10000 and u_1 = JPY, the otoshidama from the first relative is 10000 Japanese yen; if x_2 = 0.10000000 and u_2 = BTC, the otoshidama from the second relative is 0.1 bitcoins.

If we convert the bitcoins into yen at the rate of 380000.0 JPY per 1.0 BTC, how much are the gifts worth in total?

Constraints

2 \leq N \leq 10

u_i = JPY or BTC.

If u_i = JPY, x_i is an integer such that 1 \leq x_i \leq 10^8.

If u_i = BTC, x_i is a decimal with 8 decimal digits, such that 0.00000001 \leq x_i \leq 100.00000000.

Input

Input is given from Standard Input in the following format:

N
x_1 u_1
x_2 u_2
:
x_N u_N

Output

If the gifts are worth Y yen in total, print the value Y (not necessarily an integer).

Output will be judged correct when the absolute or relative error from the judge's output is at most 10^{-5}.

Sample Input 1

2
10000 JPY
0.10000000 BTC

Sample Output 1

48000.0

The otoshidama from the first relative is 10000 yen. The otoshidama from the second relative is 0.1 bitcoins, which is worth 38000.0 yen if converted at the rate of 380000.0 JPY per 1.0 BTC. The sum of these is 48000.0 yen.

Outputs such as 48000 and 48000.1 will also be judged correct.

Sample Input 2

3
100000000 JPY
100.00000000 BTC
0.00000001 BTC

Sample Output 2

138000000.0038

In this case, outputs such as 138001000 and 1.38e8 will also be judged correct.


PROGRAM ATCODER

implicit none
real(8) :: sum, b
real(8),allocatable :: x(:)
character(len=3),allocatable :: u(:)
integer :: n, i

b = 380000.0

read(*,*) n
allocate(x(n), u(n))

do i = 1, n
	read(*,*) x(i), u(i)
end do

sum = 0

do i = 1, n
	if(u(i) == 'BTC') then
    	x(i) = x(i)*b
    end if
    sum = sum + x(i)
end do

write(*,'(f0.1)') sum

END PROGRAM ATCODER


program main
Implicit None
	integer(8)::n,a,b,c,i,k=4,u(3),e,f,j
	integer(8),allocatable::l(:),t(:)

	read*,n,a,b,c
	allocate(l(n),t(n))
	read*,l
	f = 1000000

	do i = 1,k**n-1
		u = 0
		call nb(i,k,t,n)
		e = 0
		do j = 1,n
			if(t(j) /= 0)then
				if(u(t(j))/=0) e=e+10
				u(t(j)) = u(t(j))+l(j)
			endif
		enddo
		if(product(u)==0) cycle
		e = e+abs(u(1)-a)+abs(u(2)-b)+abs(u(3)-c)
		if(e<f) f = e
	enddo

	print"(i0)",f
end program main

subroutine nb(a1,n,b,k)
Implicit None
	integer(8)::a1,n,k,i,a
	integer(8)::b(k)
	b = 0
	a = a1

	do i = 1,k
		b(i) = mod(a,n)
		a = (a-b(i))/n
	enddo
end subroutine nb

Score : 300 points

Problem Statement

You have N bamboos. The lengths (in centimeters) of these are l_1, l_2, ..., l_N, respectively.

Your objective is to use some of these bamboos (possibly all) to obtain three bamboos of length A, B, C. For that, you can use the following three kinds of magics any number:

Extension Magic: Consumes 1 MP (magic point). Choose one bamboo and increase its length by 1.

Shortening Magic: Consumes 1 MP. Choose one bamboo of length at least 2 and decrease its length by 1.

Composition Magic: Consumes 10 MP. Choose two bamboos and combine them into one bamboo. The length of this new bamboo is equal to the sum of the lengths of the two bamboos combined. (Afterwards, further magics can be used on this bamboo.)

At least how much MP is needed to achieve the objective?

Constraints

3 \leq N \leq 8

1 \leq C < B < A \leq 1000

1 \leq l_i \leq 1000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N A B C
l_1
l_2
:
l_N

Output

Print the minimum amount of MP needed to achieve the objective.

Sample Input 1

5 100 90 80
98
40
30
21
80

Sample Output 1

23

We are obtaining three bamboos of lengths 100, 90, 80 from five bamboos 98, 40, 30, 21, 80. We already have a bamboo of length 80, and we can obtain bamboos of lengths 100, 90 by using the magics as follows at the total cost of 23 MP, which is optimal.

Use Extension Magic twice on the bamboo of length 98 to obtain a bamboo of length 100. (MP consumed: 2)

Use Composition Magic on the bamboos of lengths 40, 30 to obtain a bamboo of length 70. (MP consumed: 10)

Use Shortening Magic once on the bamboo of length 21 to obtain a bamboo of length 20. (MP consumed: 1)

Use Composition Magic on the bamboo of length 70 obtained in step 2 and the bamboo of length 20 obtained in step 3 to obtain a bamboo of length 90. (MP consumed: 10)

Sample Input 2

8 100 90 80
100
100
90
90
90
80
80
80

Sample Output 2

0

If we already have all bamboos of the desired lengths, the amount of MP needed is 0. As seen here, we do not necessarily need to use all the bamboos.

Sample Input 3

8 1000 800 100
300
333
400
444
500
555
600
666

Sample Output 3

243


program abc119c
  implicit none
  integer n, a, b, c, mp, i, m, ix, bamboo(3)
  integer, allocatable :: l(:), ans(:)

  read *, n, a, b, c
  allocate(l(n), ans(4**n))
  read *, l
  ans = 10**9
  do i = 1, 4**n
     mp = 0
     bamboo = 0
     do m = 1, n
        ix = iand(ishft(i - 1, -(m - 1) * 2), 3)
        if (ix /= 0) then
           bamboo(ix) = bamboo(ix) + l(m)
           mp = mp + 10
        end if
     end do
     if (any(bamboo == 0)) then
        cycle
     end if
     ans(i) = mp + sum(abs(bamboo - [a, b, c])) - 30
  end do
  print *, minval(ans)
end program abc119c


program lazy_faith
  implicit none
  integer(8)              :: a, b, q, n, x, i, k
  integer(8), allocatable :: u(:,:)
  read(*,*) a, b, q
  n = a + b
  allocate(u(n,2))
  u = 0
  do i = 1, a
    read(*,*) u(i,1)
    u(i,2) = 1
  end do
  do i = a+1, n
    read(*,*) u(i,1)
    u(i,2) = 2
  end do
  call merge_sort(n,u,1_8)
  do k = 1, q
    read(*,*) x
    call answer(n,u,x)
  end do
  deallocate(u)
  stop
contains
  subroutine answer(n,c,x)
    implicit none
    integer(8), intent(in) :: n, x, c(n,2)
    integer(8)             :: i, j, m0, m1, i0, i1
    if (x.lt.c(1,1)) then
      do i = 2, n
        if (c(i,2).ne.c(1,2)) then
          write(*,'(i0)') c(i,1)-x
          return
        end if
      end do
    end if
    if (x.gt.c(n,1)) then
      do i = n-1, 1, -1
        if (c(i,2).ne.c(n,2)) then
          write(*,'(i0)') x-c(i,1)
          return
        end if
      end do
    end if
    do i = 1, n-1
      if (x.lt.c(i,1).or.c(i+1,1).lt.x) cycle
      m0 = c(n,1)-c(1,1)
      m1 = m0
      i0 = -1
      i1 = -1
      do j = 1, n
        if (c(i,2).ne.c(j,2).and.abs(c(i,1)-c(j,1)).lt.m0) m0 = abs(c(i,1)-c(j,1))
        if (c(i+1,2).ne.c(j,2).and.abs(c(i+1,1)-c(j,1)).lt.m1) m1 = abs(c(i+1,1)-c(j,1))
      end do
      write(*,'(i0)') min(x-c(i,1)+m0,c(i+1,1)-x+m1)
      return
    end do
    return
  end subroutine answer
  subroutine merge_sort(n,c,idx)
    implicit none
    integer(8), intent(in)    :: n, idx
    integer(8), intent(inout) :: c(n,4)
    integer(8)                :: number, length, i, ubound
    number = n
    length = 1
    do while (number .gt. 1)
      do i = 1, number/2
        ubound = min(2*i*length,n)
        call merge(c(2*(i-1)*length+1:(2*i-1)*length,:),c((2*i-1)*length+1:ubound,:),idx)
      end do
      length = 2*length
      number = (number+1)/2
    end do
    return
  end subroutine merge_sort
  subroutine merge(c1,c2,idx)
    implicit none
    integer(8), intent(inout) :: c1(:,:), c2(:,:)
    integer(8), intent(in)    :: idx
    integer(8)                :: c(size(c1(:,1))+size(c2(:,1)),size(c1(1,:)))
    integer(8)                :: i1, i2, n1, n2
    i1 = 1
    i2 = 1
    n1 = size(c1(:,1))
    n2 = size(c2(:,1))
    do while (i1 .le. n1 .and. i2 .le. n2)
      if (c1(i1,idx) .lt. c2(i2,idx)) then
        c(i1+i2-1,:) = c1(i1,:)
        i1 = i1+1
      else
        c(i1+i2-1,:) = c2(i2,:)
        i2 = i2+1
      end if
    end do
    if (i1 .le. n1) then
      c(i1+i2-1:n1+n2,:) = c1(i1:n1,:)
    else if (i2 .le. n2) then
      c(i1+i2-1:n1+n2,:) = c2(i2:n2,:)
    end if
    c1 = c(1:n1,:)
    c2 = c(n1+1:n1+n2,:)
    return
  end subroutine merge
end program lazy_faith

Score : 400 points

Problem Statement

Along a road running in an east-west direction, there are A shrines and B temples.
The i-th shrine from the west is located at a distance of s_i meters from the west end of the road, and the i-th temple from the west is located at a distance of t_i meters from the west end of the road.

Answer the following Q queries:

Query i (1 \leq i \leq Q): If we start from a point at a distance of x_i meters from the west end of the road and freely travel along the road, what is the minimum distance that needs to be traveled in order to visit one shrine and one temple? (It is allowed to pass by more shrines and temples than required.)

Constraints

1 \leq A, B \leq 10^5

1 \leq Q \leq 10^5

1 \leq s_1 < s_2 < ... < s_A \leq 10^{10}

1 \leq t_1 < t_2 < ... < t_B \leq 10^{10}

1 \leq x_i \leq 10^{10}

s_1, ..., s_A, t_1, ..., t_B, x_1, ..., x_Q are all different.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B Q
s_1
:
s_A
t_1
:
t_B
x_1
:
x_Q

Output

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

Sample Input 1

2 3 4
100
600
400
900
1000
150
2000
899
799

Sample Output 1

350
1400
301
399

There are two shrines and three temples. The shrines are located at distances of 100, 600 meters from the west end of the road, and the temples are located at distances of 400, 900, 1000 meters from the west end of the road.

Query 1: If we start from a point at a distance of 150 meters from the west end of the road, the optimal move is first to walk 50 meters west to visit a shrine, then to walk 300 meters east to visit a temple.

Query 2: If we start from a point at a distance of 2000 meters from the west end of the road, the optimal move is first to walk 1000 meters west to visit a temple, then to walk 400 meters west to visit a shrine. We will pass by another temple on the way, but it is fine.

Query 3: If we start from a point at a distance of 899 meters from the west end of the road, the optimal move is first to walk 1 meter east to visit a temple, then to walk 300 meters west to visit a shrine.

Query 4: If we start from a point at a distance of 799 meters from the west end of the road, the optimal move is first to walk 199 meters west to visit a shrine, then to walk 200 meters west to visit a temple.

Sample Input 2

1 1 3
1
10000000000
2
9999999999
5000000000

Sample Output 2

10000000000
10000000000
14999999998

The road is quite long, and we may need to travel a distance that does not fit into a 32-bit integer.


program test
integer(8) :: nn,k,aa(100),aaa(100),a(100),b(100),c(10000),i,a_min,atai,sig,last(100),j,count,temp

read(*,*) nn,k
read(*,*) (aa(i),i=1,nn)

a(1:nn) = aa(1:nn)

do i = 1,nn
	b(i) = i
enddo

call heapsort2(nn,a(1:nn),b(1:nn))

count = 1

do i = 1,k
	aaa(1:nn) = aa(1:nn)

	if(last(i-1) > 0 )then
		aaa(last(i-1)) = 999
	endif

	minimum = 999999
	do j =1,nn
		if(aaa(j) <= minimum) then
			last(i) = j
			minimum = aaa(j)
		endif
	enddo

	do j=1,nn
		if(i==1) then
			if((j .ne. last(i))) then
				c(count) = j
				count = count + 1
			endif
		else
			if((j .ne. last(i-1)) .and. (j .ne. last(i))) then
				c(count) = j
				count = count + 1
			endif
		endif
	enddo

	do j=1,nn
		if(j == last(i)) then
			c(count) = j
			count = count + 1
		endif
	enddo

enddo

	do j=1,nn
		if(j .ne. last(k)) then
			c(count) = j
			count = count + 1
		endif
	enddo

if( sum(a(1:nn))<nn*(k+1)-k .or. a(1)<1+k/2 ) then
	write(*,*) -1
else
	write(*,*) count-1
	write(*,*) c(1:count-1)
endif

contains

subroutine heapsort2(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 heapsort2

end program

Score : 1000 points

Problem Statement

Snuke participated in a magic show.

A magician prepared N identical-looking boxes.
He put a treasure in one of the boxes, closed the boxes, shuffled them, and numbered them 1 through N.

Since the boxes are shuffled, now Snuke has no idea which box contains the treasure.
Snuke wins the game if he opens a box containing the treasure.
You may think that if Snuke opens all boxes at once, he can always win the game.
However, there are some tricks:

Snuke must open the boxes one by one. After he opens a box and checks the content of the box, he must close the box before opening the next box.

He is only allowed to open Box i at most a_i times.

The magician may secretly move the treasure from a closed box to another closed box, using some magic trick.
For simplicity, assume that the magician never moves the treasure while Snuke is opening some box.
However, he can move it at any other time (before Snuke opens the first box, or between he closes some box and opens the next box).

The magician can perform the magic trick at most K times.

Can Snuke always win the game, regardless of the initial position of the treasure and the movements of the magician?

Constraints

2 \leq N \leq 50

1 \leq K \leq 50

1 \leq a_i \leq 100

Input

Input is given from Standard Input in the following format:

N K
a_1 a_2 \cdots a_N

Output

If the answer is no, print a single -1.

Otherwise, print one possible move of Snuke in the following format:

Q
x_1 x_2 \cdots x_Q

It means that he opens boxes x_1, x_2, \cdots, x_Q in this order.

In case there are multiple possible solutions, you can output any.

Sample Input 1

2 1
5 5

Sample Output 1

7
1 1 2 1 2 2 1

If Snuke opens the boxes 7 times in the order 1 \rightarrow 1 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 2 \rightarrow 1, he can always find the treasure regardless of its initial position and the movements of the magician.

Sample Input 2

3 50
5 10 15

Sample Output 2

-1


program jihanki
  implicit none
  integer :: a, b

  read(*,*)a,b
  if (mod(b,a)==0) then
     write(*,*)a+b
  else
     write(*,*)b-a
  end if

end program jihanki

Score : 100 points

Problem Statement

You are given positive integers A and B.

If A is a divisor of B, print A + B; otherwise, print B - A.

Constraints

All values in input are integers.

1 \leq A \leq B \leq 20

Input

Input is given from Standard Input in the following format:

A B

Output

If A is a divisor of B, print A + B; otherwise, print B - A.

Sample Input 1

4 12

Sample Output 1

16

As 4 is a divisor of 12, 4 + 12 = 16 should be printed.

Sample Input 2

8 20

Sample Output 2

12

Sample Input 3

1 1

Sample Output 3

2

1 is a divisor of 1.


implicit none
integer:: a,b

read(5,*) a,b

if(mod(b,a).eq.0) then
write(6,*)a+b
else
write(6,*)b-a
endif

end


Program ABC118A

integer a,b

read(*,*) a,b

if (mod(b,a)==0) then
 write(*,*) a+b
 else
 write(*,*) b-a
end if

stop
end


program ABC118B
    implicit none
    integer(8)::h,i,j
    integer(8)::N,M,K
    integer(8)::result=0
    integer(8),allocatable,dimension(:)::S,A

    read(5,*)N,M
    allocate(S(M))
    allocate(A(M))

    do i=1,M
        S(i)=0
    end do

    do h=1,N
        read(5,*)K,(A(i),i=1,K)

        do j=1,K
            S(A(j))=S(A(j))+1
        end do
    end do

    do i=1,M
        if(S(i)==N)then
            result=result+1
        end if
    end do

    print'(i0)',result

end program ABC118B

Score : 200 points

Problem Statement

Katsusando loves omelette rice.

Besides, he loves crème brûlée, tenderloin steak and so on, and believes that these foods are all loved by everyone.

To prove that hypothesis, he conducted a survey on M kinds of foods and asked N people whether they like these foods or not.

The i-th person answered that he/she only likes the A_{i1}-th, A_{i2}-th, ..., A_{iK_i}-th food.

Find the number of the foods liked by all the N people.

Constraints

All values in input are integers.

1 \leq N, M \leq 30

1 \leq K_i \leq M

1 \leq A_{ij} \leq M

For each i (1 \leq i \leq N), A_{i1}, A_{i2}, ..., A_{iK_i} are distinct.

Constraints

Input is given from Standard Input in the following format:

N M
K_1 A_{11} A_{12} ... A_{1K_1}
K_2 A_{21} A_{22} ... A_{2K_2}
:
K_N A_{N1} A_{N2} ... A_{NK_N}

Output

Print the number of the foods liked by all the N people.

Sample Input 1

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

Sample Output 1

1

As only the third food is liked by all the three people, 1 should be printed.

Sample Input 2

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

Sample Output 2

0

Katsusando's hypothesis turned out to be wrong.

Sample Input 3

1 30
3 5 10 30

Sample Output 3

3


program main
  implicit none
  integer :: n, m, k, a(30), i, j
  logical, allocatable :: flag(:, :)
  read (*, *) n, m
  allocate (flag(n, m))
  flag(:, :) = .false.
  do i = 1, n
    read (*, *) k, (a(j), j = 1, k)
    do j = 1, k
      flag(i, a(j)) = .true.
    end do
  end do
  print "(i0)", count(all(flag(:, :), dim=1))
end program main


program main
  implicit none
  integer :: N
  integer,allocatable :: A(:)
  integer,allocatable :: A2(:)
  integer :: Amin, i_loop,Amin_bf
  integer :: i,j,k
  read(*,*) N
  allocate(A(N),A2(N))
  read(*,*) A
  A2 = A
  i_loop = minval(A)
  ! calc mod
  do ! i = 1,i_loop
     Amin_bf = minval(A,A >0 )
     A = mod(A2,minval(A,A >0 ))
     if ( all(A == 0) ) exit
     Amin = minval(A,A>0)
     A2 = A + Amin_bf - Amin
  end do
  write(*,'(i0)') minval(A2)
  deallocate(A)
  deallocate(A2)
  stop
contains
  recursive function iqsort(ix) result(ires)
        integer, allocatable :: ires(:)
        integer, intent(in)  :: ix(:)
        integer :: k
        if (size(ix) <= 1) then
          ires = ix
        else
          k = ix(size(ix) / 2)
          ires = [iqsort(pack(ix, ix < k)), pack(ix, ix == k), iqsort(pack(ix, ix > k))]
        end if
      end function iqsort
end program main

Score : 300 points

Problem Statement

There are N monsters, numbered 1, 2, ..., N.

Initially, the health of Monster i is A_i.

Below, a monster with at least 1 health is called alive.

Until there is only one alive monster, the following is repeated:

A random alive monster attacks another random alive monster.

As a result, the health of the monster attacked is reduced by the amount equal to the current health of the monster attacking.

Find the minimum possible final health of the last monster alive.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

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 minimum possible final health of the last monster alive.

Sample Input 1

4
2 10 8 40

Sample Output 1

2

When only the first monster keeps on attacking, the final health of the last monster will be 2, which is minimum.

Sample Input 2

4
5 13 8 1000000000

Sample Output 2

1

Sample Input 3

3
1000000000 1000000000 1000000000

Sample Output 3

1000000000


program monsters_battle_royale
  implicit none
  integer(8)              :: n, i, ans
  integer(8), allocatable :: a(:)
  read(*,*) n
  allocate(a(n))
  read(*,*) a(:)
  ans = a(1)
  do i = 1, n
     ans = gcd(a(i),ans)
  end do
  deallocate(a)
  write(*,'(i0)') ans
  stop

contains

  recursive function gcd(a0,b0) result(c)
    implicit none
    integer(8), intent(in) :: a0, b0
    integer(8)             :: a, b, c
    a = a0
    b = b0
    if (a .lt. b) call swap(a,b)
    if (mod(a,b) .eq. 0) then
       c = b
    else
       c = gcd(b,a-b*(a/b))
    end if
  end function gcd

  subroutine swap(a,b)
    implicit none
    integer(8), intent(inout) :: a, b
    integer(8)                :: c
    c = a
    a = b
    b = c
    return
  end subroutine swap

end program monsters_battle_royale


program minpro2019A
    implicit none
    integer(8)N,K
    read*,N,K
    if(K>N/2+mod(N,2))then
        print'(A)',"YES"
    else
        print'(A)',"NO"
    end if
end program minpro2019A

Score : 100 points

Problem Statement

Determine if we can choose K different integers between 1 and N (inclusive) so that no two of them differ by 1.

Constraints

1\leq N,K\leq 100

N and K are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

If we can choose K integers as above, print YES; otherwise, print NO.

Sample Input 1

3 2

Sample Output 1

YES

We can choose 1 and 3.

Sample Input 2

5 5

Sample Output 2

NO

Sample Input 3

31 10

Sample Output 3

YES

Sample Input 4

10 90

Sample Output 4

NO


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(ubound(a,1)-size(a)+1:ubound(a,1)),n,k
      n = ubound(a,1)
      k = ubound(a,1)-size(a)+1
      call mergesort(a,k,n,b)
    end subroutine sort
    function POSL(a,v) result(r)!find first i:a(i)>=v
        implicit none
        integer(8) :: a(:),l,r,idx,v
        r = ubound(a,1) + 1
        l = ubound(a,1)-size(a)
        do while(r-l>1)
            idx = l + (r-l)/2
            if(a(idx) >= v)then
                r = idx
            else
                l = idx
            endif
        enddo
    end function
    function POSU(a,v) result(r)!find first i:a(i)>v
        implicit none
        integer(8) :: a(:),l,r,idx,v
        r = ubound(a,1) + 1
        l = ubound(a,1)-size(a)
        do while(r-l>1)
            idx = l + (r-l)/2
            if(a(idx) > v)then
                r = idx
            else
                l = idx
            endif
        enddo
    end function
    function NUM(a,v) result(n)
        implicit none
        integer(8) :: a(:),v,n
        n = POSU(a,v) - POSL(a,v)
    end function
end module Sort_Module

program atcoder
    use Sort_Module
    implicit none
    integer(8) :: ans,a,b,c(1:4),i,d(1:4)
    c=0
    do i=1,3
        read*,a,b
        c(a)=c(a)+1
        c(b)=c(b)+1
    enddo
    call sort(c)
    d(1)=1
    d(2)=1
    d(3)=2
    d(4)=2
    if( all(d==c) )then
        write(*,'(a)')"YES"
    else
        write(*,'(a)')"NO"
    endif
    stop
contains
end program atcoder

Score : 200 points

Problem Statement

There are four towns, numbered 1,2,3 and 4.
Also, there are three roads. The i-th road connects different towns a_i and b_i bidirectionally.
No two roads connect the same pair of towns. Other than these roads, there is no way to travel between these towns, but any town can be reached from any other town using these roads.

Determine if we can visit all the towns by traversing each of the roads exactly once.

Constraints

1 \leq a_i,b_i \leq 4(1\leq i\leq 3)

a_i and b_i are different. (1\leq i\leq 3)

No two roads connect the same pair of towns.

Any town can be reached from any other town using the roads.

Input

Input is given from Standard Input in the following format:

a_1 b_1
a_2 b_2
a_3 b_3

Output

If we can visit all the towns by traversing each of the roads exactly once, print YES; otherwise, print NO.

Sample Input 1

4 2
1 3
2 3

Sample Output 1

YES

We can visit all the towns in the order 1,3,2,4.

Sample Input 2

3 2
2 4
1 2

Sample Output 2

NO

Sample Input 3

2 1
3 2
4 3

Sample Output 3

YES


program atcoder
    implicit none
    integer(8) :: ans,k,a,b
    logical x
    read*,k,a,b
    ans = 1
    x = (b-a)>2_8
    do while(k>0_8)
        if(ans>=a .and. x .and. k>1_8)then
            ans = ans + (b-a)
            k = k-1_8
        else
            ans = ans + 1_8
        endif
        k = k-1_8
    enddo
    write(*,'(I0)')ans
    stop
contains
end program atcoder

Score : 400 points

Problem Statement

Snuke has one biscuit and zero Japanese yen (the currency) in his pocket.
He will perform the following operations exactly K times in total, in the order he likes:

Hit his pocket, which magically increases the number of biscuits by one.

Exchange A biscuits to 1 yen.

Exchange 1 yen to B biscuits.

Find the maximum possible number of biscuits in Snuke's pocket after K operations.

Constraints

1 \leq K,A,B \leq 10^9

K,A and B are integers.

Input

Input is given from Standard Input in the following format:

K A B

Output

Print the maximum possible number of biscuits in Snuke's pocket after K operations.

Sample Input 1

4 2 6

Sample Output 1

7

The number of biscuits in Snuke's pocket after K operations is maximized as follows:

Hit his pocket. Now he has 2 biscuits and 0 yen.

Exchange 2 biscuits to 1 yen. his pocket. Now he has 0 biscuits and 1 yen.

Hit his pocket. Now he has 1 biscuits and 1 yen.

Exchange 1 yen to 6 biscuits. his pocket. Now he has 7 biscuits and 0 yen.

Sample Input 2

7 3 4

Sample Output 2

8

Sample Input 3

314159265 35897932 384626433

Sample Output 3

48518828981938099


program ABC117A
    implicit none
    real::X,T
    read(5,*)T,X
    print'(f0.6)',T/X
end program ABC117A

Score : 100 points

Problem Statement

In order to pass the entrance examination tomorrow, Taro has to study for T more hours.

Fortunately, he can leap to World B where time passes X times as fast as it does in our world (World A).

While (X \times t) hours pass in World B, t hours pass in World A.

How many hours will pass in World A while Taro studies for T hours in World B?

Constraints

All values in input are integers.

1 \leq T \leq 100

1 \leq X \leq 100

Input

Input is given from Standard Input in the following format:

T X

Output

Print the number of hours that will pass in World A.

The output will be regarded as correct when its absolute or relative error from the judge's output is at most 10^{-3}.

Sample Input 1

8 3

Sample Output 1

2.6666666667

While Taro studies for eight hours in World B where time passes three times as fast, 2.6666... hours will pass in World A.

Note that an absolute or relative error of at most 10^{-3} is allowed.

Sample Input 2

99 1

Sample Output 2

99.0000000000

Sample Input 3

1 100

Sample Output 3

0.0100000000


program aaa
implicit none

real(8) :: t, x

read*, t, x

write(*,*) t/x

end program


program polygon
  implicit none
  integer              :: n
  integer, allocatable :: l(:)
  read(*,*) n
  allocate(l(n))
  read(*,*) l(:)
  if (sum(l)-2*maxval(l) .gt. 0) then
     write(*,'(a)') 'Yes'
  else
     write(*,'(a)') 'No'
  end if
end program polygon

Score : 200 points

Problem Statement

Determine if an N-sided polygon (not necessarily convex) with sides of length L_1, L_2, ..., L_N can be drawn in a two-dimensional plane.

You can use the following theorem:

Theorem: an N-sided polygon satisfying the condition can be drawn if and only if the longest side is strictly shorter than the sum of the lengths of the other N-1 sides.

Constraints

All values in input are integers.

3 \leq N \leq 10

1 \leq L_i \leq 100

Input

Input is given from Standard Input in the following format:

N
L_1 L_2 ... L_N

Output

If an N-sided polygon satisfying the condition can be drawn, print Yes; otherwise, print No.

Sample Input 1

4
3 8 5 1

Sample Output 1

Yes

Since 8 < 9 = 3 + 5 + 1, it follows from the theorem that such a polygon can be drawn on a plane.

Sample Input 2

4
3 8 4 1

Sample Output 2

No

Since 8 \geq 8 = 3 + 4 + 1, it follows from the theorem that such a polygon cannot be drawn on a plane.

Sample Input 3

10
1 8 10 5 8 12 34 100 11 3

Sample Output 3

No


PROGRAM ATCODER

implicit none
integer :: n, i
integer,allocatable :: l(:)

read(*,*) n
allocate(l(n))

read(*,*) (l(i),i=1,n)

if ( maxval(l) < sum(l)-maxval(l) ) then
	write(*,'(a)') 'Yes'
else
	write(*,'(a)') 'No'
end if

END PROGRAM ATCODER


program main
	implicit none
    integer(8)::n,m,i,t
    integer(8),allocatable::x(:),y(:)
    read(*,*)n,m
    allocate(x(m), y(m-1))
    read(*,*) x
    if(n>=m)then
    	write(*,*) 0
    else
    	t=0
    	call msort(x)
        do i=1,m-1
        	y(i)=abs(x(i+1)-x(i))
        end do
        call msort(y)
        do i=1,n-1
        	t=t+y(m-i)
        end do
        write(*,*) x(m)-x(1)-t
    end if
    deallocate(x,y)

    stop
contains
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 main

Score : 300 points

Problem Statement

We will play a one-player game using a number line and N pieces.

First, we place each of these pieces at some integer coordinate.

Here, multiple pieces can be placed at the same coordinate.

Our objective is to visit all of the M coordinates X_1, X_2, ..., X_M with these pieces, by repeating the following move:

Move: Choose a piece and let x be its coordinate. Put that piece at coordinate x+1 or x-1.

Note that the coordinates where we initially place the pieces are already regarded as visited.

Find the minimum number of moves required to achieve the objective.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

-10^5 \leq X_i \leq 10^5

X_1, X_2, ..., X_M are all different.

Input

Input is given from Standard Input in the following format:

N M
X_1 X_2 ... X_M

Output

Find the minimum number of moves required to achieve the objective.

Sample Input 1

2 5
10 12 1 2 14

Sample Output 1

5

The objective can be achieved in five moves as follows, and this is the minimum number of moves required.

Initially, put the two pieces at coordinates 1 and 10.

Move the piece at coordinate 1 to 2.

Move the piece at coordinate 10 to 11.

Move the piece at coordinate 11 to 12.

Move the piece at coordinate 12 to 13.

Move the piece at coordinate 13 to 14.

Sample Input 2

3 7
-10 -3 0 9 -100 2 17

Sample Output 2

19

Sample Input 3

100 1
-100000

Sample Output 3

0


program prob4
    implicit none
    integer(8) :: N, M
    integer(8), allocatable, dimension(:) :: X, Y
    integer(8) :: i
    integer(8) :: ans
    read(*,*) N, M
    allocate(X(M))
    read(*,*) X
    if(M == 1) then
        write(*,*) maxval(X) - minval(X)
        stop
    end if
    allocate(Y(M-1))

    call msort(X)
    do i = 1, M-1
        Y(i) = X(i+1) - X(i)
    end do

    call msort(Y)
    ans = 0
    do i = 1, M-N
        ans = ans + Y(i)
    end do
    write(*,*) ans

    deallocate(X)
    deallocate(Y)
    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 prob4


program name
    implicit none
    integer(4):: n,m,i
    integer(4),allocatable:: x(:), dst(:)

    read*, n,m
    if (m==1) then
        print*, 0
        stop
    end if
    allocate(x(m),dst(m-1))
    read*, x(:)
    call merge_sort(x,1,m)
    do i=1,m-1
        dst(i) = x(i+1)-x(i)
    end do
    call merge_sort(dst,1,m-1)
    print*, sum(dst(1:m-n))

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

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

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

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

        allocate(tmp(lst-fst+1))

        li=fst
        ri=mdl+1
        ti=1

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

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

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

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


program streamline
  implicit none
  integer :: n, m, x(100000) = 0, i, d(100000) = 0
  read(*,*) n, m
  if (m <= n) then
    write(*,'(i0)') 0
    stop
  end if
  read(*,*) x(1:m)
  call quick_sort(x(1:m))
  do i = 1, m-1
    d(i) = x(i+1)-x(i)
  end do
  call quick_sort(d(1:m-1))
  write(*,'(i0)') sum(int(d(1:m-n),8))
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 streamline


integer n,m,a,ans
integer,allocatable,dimension(:)::x,dx(:)

read*,n, m
allocate(x(m),dx(m-1))
read*, (x(i), i=1,m)

if(n >= m)then
  print"(i0)",0
end if

call heapsort(m,x)

do i =1, m-1
  dx(i)=x(i+1) - x(i)
end do

call heapsort(m-1,dx)

ans = sum(dx)
if (n > 1)ans = ans - sum(dx(m-n+1:m-1))
print"(i0)",ans

contains

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

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


program main
   implicit none
   integer(8) ::n,m
   integer(8) i
   integer(8),allocatable,dimension(:)::x,sa
   read(*,*)n,m
   allocate(x(m),sa(m-1))
   read(*,*)x
   call heapsort(m,x)
   if(m>=2)then
   do i=1,m-1
      sa(i)=x(i+1)-x(i)
   enddo
   call heapsort(m-1,sa)
   if(m<=n)then
      write(*,*)0
   else
      write(*,*)sum(sa(1:m-n))
   endif
   else
      write(*,*)"0"
   endif
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


integer a,b,c
read*,a,b,c
print*,min(b,c), max(0,b+c-a)
end

Score : 100 points

Problem Statement

We conducted a survey on newspaper subscriptions.
More specifically, we asked each of the N respondents the following two questions:

Question 1: Are you subscribing to Newspaper X?

Question 2: Are you subscribing to Newspaper Y?

As the result, A respondents answered "yes" to Question 1, and B respondents answered "yes" to Question 2.

What are the maximum possible number and the minimum possible number of respondents subscribing to both newspapers X and Y?

Write a program to answer this question.

Constraints

1 \leq N \leq 100

0 \leq A \leq N

0 \leq B \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print the maximum possible number and the minimum possible number of respondents subscribing to both newspapers, in this order, with a space in between.

Sample Input 1

10 3 5

Sample Output 1

3 0

In this sample, out of the 10 respondents, 3 answered they are subscribing to Newspaper X, and 5 answered they are subscribing to Newspaper Y.

Here, the number of respondents subscribing to both newspapers is at most 3 and at least 0.

Sample Input 2

10 7 5

Sample Output 2

5 2

In this sample, out of the 10 respondents, 7 answered they are subscribing to Newspaper X, and 5 answered they are subscribing to Newspaper Y.

Here, the number of respondents subscribing to both newspapers is at most 5 and at least 2.

Sample Input 3

100 100 100

Sample Output 3

100 100


program main
   implicit none
   integer n,a,b
   read(*,*)n,a,b
   write(*,*)min(a,b),max((max(a,b)-n+min(a,b)),0)
end program main


program main
implicit None
	integer(8)::n,i,p,q
	integer(8),allocatable::a(:),b(:),c(:)

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

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

	call h3(c,a,b,n)

	p = 1
	q = 1
	do
		if(p > n)exit
		do
			if(q+1 > n)exit
			if(c(p) /=c(q+1))exit
			q = q+1
		enddo
		call h3(a(p:q),b(p:q),c(p:q),q-p+1)
		p = q+1
		q = q+1
	enddo

	p = 0 ;q = 0
	do i = 1,n/2
		p = p + a(n-(2*i-2))
		q = q + b(n-(2*i-1))
	enddo
	if(mod(n,2) == 1)then
		p = p + a(1)
	endif

	print *,p-q
end program main

subroutine h3(a,b,c,n)
implicit None
	integer(8)::a(n),b(n),c(n),x,y,z
	integer(8)::n,i,ie,i0,i1,i2

	do i = n/2,1,-1
		x = a(i);y = b(i);z = c(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
				b(i0) = b(i1);b(i1) = y
				c(i0) = c(i1);c(i1) = z
				else
				exit
			endif
			i0 = i1;i1 = i0 *2
		enddo
	enddo
	do ie = n,2,-1
		x = a(ie);y = b(ie);z = c(ie)
		a(ie) = a(1);b(ie) = b(1);c(ie) = c(1)
		a(1) = x;b(1) = y;c(1) = z
		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
				b(i0) = b(i1);b(i1) = y
				c(i0) = c(i1);c(i1) = z
				else
				exit
			endif
			i0 = i1;i1 = i0 *2
		enddo
	enddo
end subroutine h3

Score : 400 points

Problem Statement

There are N dishes of cuisine placed in front of Takahashi and Aoki.
For convenience, we call these dishes Dish 1, Dish 2, ..., Dish N.

When Takahashi eats Dish i, he earns A_i points of happiness; when Aoki eats Dish i, she earns B_i points of happiness.

Starting from Takahashi, they alternately choose one dish and eat it, until there is no more dish to eat.
Here, both of them choose dishes so that the following value is maximized: "the sum of the happiness he/she will earn in the end" minus "the sum of the happiness the other person will earn in the end".

Find the value: "the sum of the happiness Takahashi earns in the end" minus "the sum of the happiness Aoki earns in the end".

Constraints

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

1 \leq B_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
:
A_N B_N

Output

Print the value: "the sum of the happiness Takahashi earns in the end" minus "the sum of the happiness Aoki earns in the end".

Sample Input 1

3
10 10
20 20
30 30

Sample Output 1

20

In this sample, both of them earns 10 points of happiness by eating Dish 1, 20 points by eating Dish 2, and 30 points by eating Dish 3.

In this case, since Takahashi and Aoki have the same "taste", each time they will choose the dish with which they can earn the greatest happiness. Thus, first Takahashi will choose Dish 3, then Aoki will choose Dish 2, and finally Takahashi will choose Dish 1, so the answer is (30 + 10) - 20 = 20.

Sample Input 2

3
20 10
20 20
20 30

Sample Output 2

20

In this sample, Takahashi earns 20 points of happiness by eating any one of the dishes 1, 2 and 3, but Aoki earns 10 points of happiness by eating Dish 1, 20 points by eating Dish 2, and 30 points by eating Dish 3.

In this case, since only Aoki has likes and dislikes, each time they will choose the dish with which Aoki can earn the greatest happiness. Thus, first Takahashi will choose Dish 3, then Aoki will choose Dish 2, and finally Takahashi will choose Dish 1, so the answer is (20 + 20) - 20 = 20.

Sample Input 3

6
1 1000000000
1 1000000000
1 1000000000
1 1000000000
1 1000000000
1 1000000000

Sample Output 3

-2999999997

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


program prob27
    implicit none
    integer :: a,b,c
    write(*,*) a,b,c
    if(a <= c .and. b <= c) then
        write(*,*) a * b / 2
    else if(a <= b .and. c <= b) then
        write(*,*) a*c/2
    else
        write(*,*) b*c/2
    end if

    stop
contains
end program prob27

Score : 100 points

Problem Statement

There is a right triangle ABC with ∠ABC=90°.

Given the lengths of the three sides, |AB|,|BC| and |CA|, find the area of the right triangle ABC.

It is guaranteed that the area of the triangle ABC is an integer.

Constraints

1 \leq |AB|,|BC|,|CA| \leq 100

All values in input are integers.

The area of the triangle ABC is an integer.

Input

Input is given from Standard Input in the following format:

|AB| |BC| |CA|

Output

Print the area of the triangle ABC.

Sample Input 1

3 4 5

Sample Output 1

6

This triangle has an area of 6.

Sample Input 2

5 12 13

Sample Output 2

30

This triangle has an area of 30.

Sample Input 3

45 28 53

Sample Output 3

630

This triangle has an area of 630.


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

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

end program jihanki


program sample
    implicit none

    integer(8) :: i,j,m,n,x,b,s

    integer(8):: a(1000000)

    read(*,*) s

    do i=1,1000000
        a(i)=0
    end do
    m=1
    do
        if(a(s)==1)then
            exit
        else
            a(s)=a(s)+1
            m=m+1
            if (mod(s,2)==0)then
                s=s/2
            else
                s=s*3+1
            end if
        end if

    end do

        write(*,*)m

    stop
end program sample

Score : 200 points

Problem Statement

A sequence a=\{a_1,a_2,a_3,......\} is determined as follows:

The first term s is given as input.

Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd.

a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1.

Find the minimum integer m that satisfies the following condition:

There exists an integer n such that a_m = a_n (m > n).

Constraints

1 \leq s \leq 100

All values in input are integers.

It is guaranteed that all elements in a and the minimum m that satisfies the condition are at most 1000000.

Input

Input is given from Standard Input in the following format:

s

Output

Print the minimum integer m that satisfies the condition.

Sample Input 1

8

Sample Output 1

5

a=\{8,4,2,1,4,2,1,4,2,1,......\}. As a_5=a_2, the answer is 5.

Sample Input 2

7

Sample Output 2

18

a=\{7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1,4,2,1,......\}.

Sample Input 3

54

Sample Output 3

114


program main
   implicit none
   integer n
   integer,allocatable,dimension(:):: h
   integer:: i,j,counter=0
   read(*,*)n
   allocate(h(n))
   read(*,*)h
   do i=1,n
10       continue
      do j=n,i,-1

         if(all(h(i:j)>0))then
            h(i:j)=h(i:j)-1
            counter=counter+1
            goto 10
         endif
      enddo
   enddo
   write(*,*)counter
end program main

Score : 300 points

Problem Statement

In a flower bed, there are N flowers, numbered 1,2,......,N. Initially, the heights of all flowers are 0.
You are given a sequence h=\{h_1,h_2,h_3,......\} as input. You would like to change the height of Flower k to h_k for all k (1 \leq k \leq N), by repeating the following "watering" operation:

Specify integers l and r. Increase the height of Flower x by 1 for all x such that l \leq x \leq r.

Find the minimum number of watering operations required to satisfy the condition.

Constraints

1 \leq N \leq 100

0 \leq h_i \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
h_1 h_2 h_3 ...... h_N

Output

Print the minimum number of watering operations required to satisfy the condition.

Sample Input 1

4
1 2 2 1

Sample Output 1

2

The minimum number of watering operations required is 2.
One way to achieve it is:

Perform the operation with (l,r)=(1,3).

Perform the operation with (l,r)=(2,4).

Sample Input 2

5
3 1 2 3 1

Sample Output 2

5

Sample Input 3

8
4 23 75 0 23 96 50 100

Sample Output 3

221


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

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 100
	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_a(tt(i)) == 0) then
			get_count = get_count + 1
			get(get_count) = dd(i)
			sumi_a(tt(i)) = 1
		endif
	enddo

	if(get_count == 0)then
		goto 100
	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)))
!	write(*,*) lost_cum(1:count)
!	write(*,*) get_cum(1:count)
endif

!最初の取り方から変更ない場合
if(1 == 0)then
100 continue
	call heapsort_up(n,t(1:k),d(1:k))
	num = 1
	do i =2,k
		if(t(i) .ne. t(i-1))then
		 num = num + 1
		endif
	enddo
	write(*,*) sum(d(1:k)) + num*num
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 main
integer(8)::a(4),i,c,j,p
	read*,a
	do j = 1,4
		c =0
		p = (j-1)*3+1
		if (p == 10) p = 9
		do i = 1,4
			if(a(i) == p) c = 1
		enddo
		if(c == 0)then
			print "(a)","NO"
			print *,j
			stop
		endif
	enddo

	print"(a)","YES"

end program main

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


program sample
    implicit none

    integer(8) :: i,j,m,n
    real(8)::a,b
    integer(8),allocatable :: x(:),y(:)
    character(len=100)::c !文字列

    read(*,*) c
    j=0
    n=len_trim(c)  !len_trimは空白を無視する、大きめに取って
    if (n==7)then
        if (c=='keyence')then
            write(*,*)'YES'
        else
            write(*,*)'NO'
        end if

        stop
    end if

    if (c(1:7)=='keyence')then
        write(*,*)'YES'
        stop
    end if
    if (c(n-6:n)=='keyence')then
        write(*,*)'YES'
        stop
    end if

    do i=1,6

        if (c(1:i)//c(n+i-6:n)=='keyence')then
            write(*,*)'YES'
            stop

        end if

    end do
    write(*,*)'NO'

    stop
end program sample

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(len=100) :: s
  character(len=7) :: c
  integer :: n, i

  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
     do i = 1, 7
        if(index(s,c(1:i)) == 1 .and. index(s,c(i+1:7), back=.true.) == n-i) then
           write(*,*) 'YES'
           stop
        end if
     end do

     write(*,*) 'NO'
  end if
end program main


program keyence
  implicit none
  character(len=100)::s
  character(len=7)::ke='keyence'
  integer :: i, k
  read(*,*) s
  s=trim(s)
  k=len_trim(s)

  do i = 1, 6
     if ( s(1:i)==ke(1:i) .and. s(k+i-6:k)==ke(i+1:7)) then
        write(*,*) 'YES'
        stop
     end if
  end do

  if ( s(1:7) == ke .or. s(k-6:k) == ke ) then
     write(*,*) 'YES'
     stop
  end if

  write(*,*) 'NO'

  stop
end program keyence


character(102) S,ans
read*,S
do i=1,len_trim(S)+1
  do j=i,len_trim(S)+1
    if(i==1)then
      ans=trim(S(j+1:))
    else
      ans=S(1:i-1)//S(j+1:)
    end if
    if(trim(ans)=="keyence")then
      print"(A)","YES"
      STOP
    endif
  end do
end do
print"(A)","NO"
end


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 atcoder
    implicit none
    integer(8) :: n,k,i,j
    character(len=7) :: a,b
    character(len=100) :: s
    a = "keyence"
    read*,s
    n = len_trim(s)
    do i = 0,7
        j = 7 - i
        if(i/=0)then
            b(1:i) = s(1:i)
        endif
        if(j/=0)then
            b(7-j+1:7) = s(n-j+1:n)
        endif
        if(b==a)then
            print"(a)","YES"
            stop
        endif
    enddo
    print"(a)","NO"
    stop
contains

end program atcoder


program exam
  implicit none
  integer :: n, c, i
  integer(8),allocatable :: a(:), b(:), p(:)
  integer(8) :: pmax, d, y
  read(*,*) n
  allocate(a(n), b(n), p(n))
  read(*,*) a
  read(*,*) b
  if (sum(a) < sum(b)) then
     write(*,*) -1
     stop
  end if

  p = a - b
  c = 0

  do i = 1, n
     if (p(i) < 0) then
        c = c + 1
        d = d + abs(p(i))
     end if
  end do

  if (c == 0) then
     write(*,*) 0
     stop
  end if

  call heapsort(p)

  do i = n, 1, -1
     c = c + 1
     d = d - p(i)
     if (d <= 0) then
        exit
     end if
  end do
  write(*,*) c

  stop

contains

  subroutine heapsort(array)
    implicit none
    integer(8):: array(:)
    integer ::i,k,j,l
    integer(8) :: t

    n=size(array)

    if (n==1) then
       return
    end if

    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 == 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
       end do
       array(i)=t
    end do
    return
  end subroutine heapsort

end program

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 keyence2019
  implicit none
  integer n, i, ans
  integer(8) s
  integer, 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, dimension(:), allocatable :: res
    integer, 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 AIsing2019A
    implicit none
    integer(8)N,H,W
    read*,N,H,W
    print'(i0)',(N-W+1)*(N-H+1)
end program AIsing2019A

Score : 100 points

Problem Statement

It has been decided that a programming contest sponsored by company A will be held, so we will post the notice on a bulletin board.

The bulletin board is in the form of a grid with N rows and N columns, and the notice will occupy a rectangular region with H rows and W columns.

How many ways are there to choose where to put the notice so that it completely covers exactly HW squares?

Constraints

1 \leq H, W \leq N \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
H
W

Output

Print the answer.

Sample Input 1

3
2
3

Sample Output 1

2

There are two ways to put the notice, as follows:

### ...
### ###
... ###

Here, # represents a square covered by the notice, and . represents a square not covered.

Sample Input 2

100
1
1

Sample Output 2

10000

Sample Input 3

5
4
2

Sample Output 3

8


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


implicit none
integer :: n, h, w
read(5, *) n, h, w
write(6, *) (n - h + 1) * (n - w + 1)
end


integer N
integer A,B
integer P
integer Q1,Q2,Q3
read*,N
read*,A,B
Q1=0;Q2=0;Q3=0
do i=1,N
  read*,P
  if(P<=A)then
    Q1=Q1+1
  else if(P<=B)then
    Q2=Q2+1
  else
    Q3=Q3+1
  endif
end do
print"(I0)",min(Q1,Q2,Q3)
end

Score : 200 points

Problem Statement

You have written N problems to hold programming contests.
The i-th problem will have a score of P_i points if used in a contest.

With these problems, you would like to hold as many contests as possible under the following condition:

A contest has three problems. The first problem has a score not greater than A points, the second has a score between A + 1 and B points (inclusive), and the third has a score not less than B + 1 points.

The same problem should not be used in multiple contests.
At most how many contests can be held?

Constraints

3 \leq N \leq 100

1 \leq P_i \leq 20 (1 \leq i \leq N)

1 \leq A < B < 20

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A B
P_1 P_2 ... P_N

Output

Print the answer.

Sample Input 1

7
5 15
1 10 16 2 7 20 12

Sample Output 1

2

Two contests can be held by putting the first, second, third problems and the fourth, fifth, sixth problems together.

Sample Input 2

8
3 8
5 5 5 10 10 10 15 20

Sample Output 2

0

No contest can be held, because there is no problem with a score of A = 3 or less.

Sample Input 3

3
5 6
5 6 10

Sample Output 3

1


program nearest_card_game
  implicit none
  integer :: n, q, i, l, h, m
  integer(8) :: x, a(100000), s(0:100000), e(0:100000)
  a = 0_8
  s = 0_8
  e = 0_8
  read(*,*) n, q
  read(*,*) a(1:n)
  if (n.le.2) then
    do i = 1, q
      write(*,'(i0)') a(n)
    end do
    stop
  end if
  do i = 1, n
    s(i) = s(i-1)+a(i)
  end do
  e(1) = a(1)
  do i = 2, n
    e(i) = e(i-2)+a(i)
  end do
  do i = 1, q
    read(*,*) x
    h = (n-1)/2
    if (abs(x-a(n-2*h)).le.abs(x-a(n-h))) then
      write(*,'(i0)') s(n)-s(n/2)
      cycle
    end if
    l = -1
    do while (h-l.gt.1)
      m = (h+l)/2
      if (abs(x-a(n-2*m)).gt.abs(x-a(n-m))) then
        h = m
      else
        l = m
      end if
    end do
    write(*,'(i0)') e(n-2*h)+s(n)-s(n-h)
  end do
  stop
end program nearest_card_game

Score : 500 points

Problem Statement

There are N cards. The i-th card has an integer A_i written on it.
For any two cards, the integers on those cards are different.

Using these cards, Takahashi and Aoki will play the following game:

Aoki chooses an integer x.

Starting from Takahashi, the two players alternately take a card. The card should be chosen in the following manner:

Takahashi should take the card with the largest integer among the remaining card.

Aoki should take the card with the integer closest to x among the remaining card. If there are multiple such cards, he should take the card with the smallest integer among those cards.

The game ends when there is no card remaining.

You are given Q candidates for the value of x: X_1, X_2, ..., X_Q.
For each i (1 \leq i \leq Q), find the sum of the integers written on the cards that Takahashi will take if Aoki chooses x = X_i.

Constraints

2 \leq N \leq 100 000

1 \leq Q \leq 100 000

1 \leq A_1 < A_2 < ... < A_N \leq 10^9

1 \leq X_i \leq 10^9 (1 \leq i \leq Q)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
A_1 A_2 ... A_N
X_1
X_2
:
X_Q

Output

Print Q lines. The i-th line (1 \leq i \leq Q) should contain the answer for x = X_i.

Sample Input 1

5 5
3 5 7 11 13
1
4
9
10
13

Sample Output 1

31
31
27
23
23

For example, when x = X_3(= 9), the game proceeds as follows:

Takahashi takes the card with 13.

Aoki takes the card with 7.

Takahashi takes the card with 11.

Aoki takes the card with 5.

Takahashi takes the card with 3.

Thus, 13 + 11 + 3 = 27 should be printed on the third line.

Sample Input 2

4 3
10 20 30 40
2
34
34

Sample Output 2

70
60
60


integer(16) N
integer(16),allocatable,dimension(:)::A
integer(16),allocatable,dimension(:,:)::DP
read*,N
allocate(A(N))
read*,A
allocate(DP(N,N))
DP=huge(N)

print"(i0)",DFS(1,1)

contains
recursive function DFS(turn,l)result(res)
  integer,intent(in)::turn,l
  integer(16) res
  integer r
  if(DP(turn,l)/=huge(N))then
    res=DP(turn,l)
    return
  endif
  r=N-turn+l
  select case(mod(turn,2))
    case(1)!Taro's turn
      if(turn==N)then
        res=A(l)
        return
      endif
      res=max(DFS(turn+1,l+1)+A(l),DFS(turn+1,l)+A(r))
    case(0)!Jiro's turn
      if(turn==N)then
        res=-A(l)
        return
      endif
      res=min(DFS(turn+1,l+1)-A(l),DFS(turn+1,l)-A(r))
  end select
  DP(turn,l)=res
  return
end function
end

Score : 100 points

Problem Statement

Taro and Jiro will play the following game against each other.

Initially, they are given a sequence a = (a_1, a_2, \ldots, a_N).
Until a becomes empty, the two players perform the following operation alternately, starting from Taro:

Remove the element at the beginning or the end of a. The player earns x points, where x is the removed element.

Let X and Y be Taro's and Jiro's total score at the end of the game, respectively.
Taro tries to maximize X - Y, while Jiro tries to minimize X - Y.

Assuming that the two players play optimally, find the resulting value of X - Y.

Constraints

All values in input are integers.

1 \leq N \leq 3000

1 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 \ldots a_N

Output

Print the resulting value of X - Y, assuming that the two players play optimally.

Sample Input 1

4
10 80 90 30

Sample Output 1

10

The game proceeds as follows when the two players play optimally (the element being removed is written bold):

Taro: (10, 80, 90, 30) → (10, 80, 90)

Jiro: (10, 80, 90) → (10, 80)

Taro: (10, 80) → (10)

Jiro: (10) → ()

Here, X = 30 + 80 = 110 and Y = 90 + 10 = 100.

Sample Input 2

3
10 100 10

Sample Output 2

-80

The game proceeds, for example, as follows when the two players play optimally:

Taro: (10, 100, 10) → (100, 10)

Jiro: (100, 10) → (10)

Taro: (10) → ()

Here, X = 10 + 10 = 20 and Y = 100.

Sample Input 3

1
10

Sample Output 3

10

Sample Input 4

10
1000000000 1 1000000000 1 1000000000 1 1000000000 1 1000000000 1

Sample Output 4

4999999995

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

Sample Input 5

6
4 2 9 7 1 5

Sample Output 5

2

The game proceeds, for example, as follows when the two players play optimally:

Taro: (4, 2, 9, 7, 1, 5) → (4, 2, 9, 7, 1)

Jiro: (4, 2, 9, 7, 1) → (2, 9, 7, 1)

Taro: (2, 9, 7, 1) → (2, 9, 7)

Jiro: (2, 9, 7) → (2, 9)

Taro: (2, 9) → (2)

Jiro: (2) → ()

Here, X = 5 + 1 + 9 = 15 and Y = 4 + 7 + 2 = 13.


module mod_modint
  implicit none
  integer(8) :: modulus = 1000000007_8
  ! integer(8) :: modulus = 998244353_8
  ! integer(8) :: modulus = 1000000009_8

  type modint
    private
    integer(8) :: num
  contains
    procedure :: get => getnum
  end type

  interface change_modulus
    module procedure :: change_modulus64, change_modulus32
  end interface change_modulus

  interface modint
    module procedure :: newm, newi
  end interface modint

  interface assignment(=)
    module procedure :: setm, seti64, seti32
  end interface assignment(=)

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

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

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

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

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

  interface inv
    module procedure :: invm
  end interface inv

contains

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

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

  integer(8) function getnum(this)
    class(modint), intent(in) :: this
    getnum = this%num
  end

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

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

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

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

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

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

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

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

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

  !##########################################################################
  !##################### overload with (normal) integer #####################
  !##########################################################################

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

end module mod_modint
program speedrun
   use mod_modint
   implicit none
   integer N,K
   integer,allocatable,dimension(:)::A
   integer::i,j
   type(modint),allocatable,dimension(:,:)::DP
   read*,N,K
   allocate(A(N))
   read*,A
   allocate(DP(0:N,0:K))
   do i=1,N
      DP(i,0)=1
   end do
   do i=0,K
      DP(0,i)=0
   end do
   DP(0,0)=1
   do i=1,N
      do j=1,K
         dp(i,j) = dp(i - 1,j) + dp(i,j - 1)
         if(j - a(i)-1  >= 0)then
            dp(i,j)=dp(i,j)- dp(i - 1,j - a(i)-1)
         endif
      end do
   end do
    print"(i0)",DP(N,K)%get()
end program speedrun

Score : 100 points

Problem Statement

There are N children, numbered 1, 2, \ldots, N.

They have decided to share K candies among themselves.
Here, for each i (1 \leq i \leq N), Child i must receive between 0 and a_i candies (inclusive).
Also, no candies should be left over.

Find the number of ways for them to share candies, modulo 10^9 + 7.
Here, two ways are said to be different when there exists a child who receives a different number of candies.

Constraints

All values in input are integers.

1 \leq N \leq 100

0 \leq K \leq 10^5

0 \leq a_i \leq K

Input

Input is given from Standard Input in the following format:

N K
a_1 a_2 \ldots a_N

Output

Print the number of ways for the children to share candies, modulo 10^9 + 7.

Sample Input 1

3 4
1 2 3

Sample Output 1

5

There are five ways for the children to share candies, as follows:

(0, 1, 3)

(0, 2, 2)

(1, 0, 3)

(1, 1, 2)

(1, 2, 1)

Here, in each sequence, the i-th element represents the number of candies that Child i receives.

Sample Input 2

1 10
9

Sample Output 2

0

There may be no ways for the children to share candies.

Sample Input 3

2 0
0 0

Sample Output 3

1

There is one way for the children to share candies, as follows:

(0, 0)

Sample Input 4

4 100000
100000 100000 100000 100000

Sample Output 4

665683269

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


program atcoder
    implicit none
    integer(8) :: i,j,n,k,modd
    integer(8),allocatable,dimension(:,:) :: dp
    integer(8),allocatable,dimension(:) :: a
    read*,n,k
    allocate(dp(0:n,0:k))
    allocate(a(0:n))
    read(*,*)(a(i),i=1,n)
    modd = 1000000007
    do i = 0,n
        dp(i,0) = 1
    enddo
    do i = 1,n
        do j = 1,k
            if(j-a(i)-1 >= 0 ) then
                dp(i,j) = dp(i,j-1)+dp(i-1,j)-dp(i-1,j-a(i)-1)
            else
                dp(i,j) = dp(i,j-1)+dp(i-1,j)
            end if
            dp(i,j) = mod(mod(dp(i,j),modd)+modd,modd)
        enddo
    enddo
    write(*,'(I0)')dp(n,k)
    stop
!---------------------------------------
contains

end program atcoder


program main
implicit None
	integer::a,b,c,d

	read *,a,b,c

	if(c <= a+b+1)then
		print 101,b+c
		else
			print 101,a+b+b+1
	endif

101 format(i0)
end program main

Score : 200 points

Problem Statement

Takahashi has A untasty cookies containing antidotes, B tasty cookies containing antidotes and C tasty cookies containing poison.

Eating a cookie containing poison results in a stomachache, and eating a cookie containing poison while having a stomachache results in a death.
As he wants to live, he cannot eat one in such a situation.
Eating a cookie containing antidotes while having a stomachache cures it, and there is no other way to cure stomachaches.

Find the maximum number of tasty cookies that Takahashi can eat.

Constraints

0 \leq A,B,C \leq 10^9

A,B and C are integers.

Input

Input is given from Standard Input in the following format:

A B C

Output

Print the maximum number of tasty cookies that Takahashi can eat.

Sample Input 1

3 1 4

Sample Output 1

5

We can eat all tasty cookies, in the following order:

A tasty cookie containing poison

An untasty cookie containing antidotes

A tasty cookie containing poison

A tasty cookie containing antidotes

A tasty cookie containing poison

An untasty cookie containing antidotes

A tasty cookie containing poison

Sample Input 2

5 2 9

Sample Output 2

10

Sample Input 3

8 8 1

Sample Output 3

9


program main
   implicit none
   character n*4
   integer i,counter
   read(*,*)n
   counter=0
   do i=1,4
      if(n(i:i)=="2")counter=counter+1
   enddo
   write(*,*)counter
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 main
implicit None
	integer(8)::n,p,q,i

	read *,n,p

	if(n >= 40)then
		print 101,1
		stop
		else if(n ==1)then
			print 101,p
			stop
	endif

	q = int(real(p)**(1.0/real(n))+0.00001)

	do i =  q,2,-1
		if(mod(p,i**n) == 0)then
			print 101,i
			stop
		endif
	enddo

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

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 CADDiB2018D
    implicit none
    integer(8)N,C,i
    integer(8),allocatable::A(:)
    read*,N
    allocate(A(N))
    read*,A
    C=0
    do i=1,N
        if(mod(A(i),2)==1)then
            C=1
            exit
        end if
    end do
    if(C==0)print'(A)',"second"
    if(C==1)print'(A)',"first"
end program CADDiB2018D

Score : 500 points

Problem Statement

There is an apple tree that bears apples of N colors. The N colors of these apples are numbered 1 to N, and there are a_i apples of Color i.

You and Lunlun the dachshund alternately perform the following operation (starting from you):

Choose one or more apples from the tree and eat them. Here, the apples chosen at the same time must all have different colors.

The one who eats the last apple from the tree will be declared winner. If both you and Lunlun play optimally, which will win?

Constraints

1 \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_2
:
a_N

Output

If you will win, print first; if Lunlun will win, print second.

Sample Input 1

2
1
2

Sample Output 1

first

Let Color 1 be red, and Color 2 be blue. In this case, the tree bears one red apple and two blue apples.

You should eat the red apple in your first turn. Lunlun is then forced to eat one of the blue apples, and you can win by eating the other in your next turn.

Note that you are also allowed to eat two apples in your first turn, one red and one blue (not a winning move, though).

Sample Input 2

3
100000
30000
20000

Sample Output 2

second


integer(8) N
integer(8) P
integer(8) ans
integer(8) i
read*,N,P

ans=1
do i=2,P
  if(mod(P,i**N)==0)then
    ans=i
  endif
  if(i**N>P)exit
end do
print"(i0)",ans
end


program main
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: n,i,a

    read*, n
    do i=1,n
        read*, a
        if (btest(a,0)) then
            print'(a)', "first"
            stop
        end if
    end do
    print'(a)', "second"
end program main


program caddi2018_b
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: n,i
    integer(int32):: a
    logical:: all_even = .true.

    read*, n

    do i=1,n
        read*, a
        all_even = all_even .and. (mod(a,2)==0)
    end do

    if (all_even) then
        print'(a)', 'second'
    else
        print'(a)', 'first'
    end if
end program caddi2018_b


program test
implicit none

integer(8) :: i,j,a(0:200001)=2,n,ans=0,b=0
character(len = 200000) :: s

read(*,*) s

n = len_trim(s)

do i= 1,n
	if(s(i:i) == "B")then
		a(i) = 0
	else
		a(i) = 1
	endif
enddo

do i= 1,n
	if(a(i) == 0) then
	b = b+1
	else
	ans = ans + b
	endif
enddo

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

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 main
implicit None
	integer(8)::n,p,i,k,s
	integer(8),allocatable::a(:),b(:)
	real(8)::x,y

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

	b = 1
	read *,a

	call hsort(a,n)

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

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

		do while (b(k) == 0)
			k = k + 1
		enddo

		if(a(k) == p)then
			b(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) >1000000000)then
			cycle
		endif
		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 >1)
			g = (e + f)/2
				print *,g
			if(a(g) > p)then
				f = g
				else
					e = g
			endif
			print *,a(i),p,e,f
		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