integer a
character(3) s
a=700
read*,s
do i=1,3
	if(s(i:i)=='o')then
    	a=a+100
    endif
end do
print"(i0)",a
end

Score : 100 points

Problem Statement

In "Takahashi-ya", a ramen restaurant, a bowl of ramen costs 700 yen (the currency of Japan), plus 100 yen for each kind of topping (boiled egg, sliced pork, green onions).

A customer ordered a bowl of ramen and told which toppings to put on his ramen to a clerk. The clerk took a memo of the order as a string S. S is three characters long, and if the first character in S is o, it means the ramen should be topped with boiled egg; if that character is x, it means the ramen should not be topped with boiled egg. Similarly, the second and third characters in S mean the presence or absence of sliced pork and green onions on top of the ramen.

Write a program that, when S is given, prints the price of the corresponding bowl of ramen.

Constraints

S is a string of length 3.

Each character in S is o or x.

Input

Input is given from Standard Input in the following format:

S

Output

Print the price of the bowl of ramen corresponding to S.

Sample Input 1

oxo

Sample Output 1

900

The price of a ramen topped with two kinds of toppings, boiled egg and green onions, is 700 + 100 \times 2 = 900 yen.

Sample Input 2

ooo

Sample Output 2

1000

The price of a ramen topped with all three kinds of toppings is 700 + 100 \times 3 = 1000 yen.

Sample Input 3

xxx

Sample Output 3

700

The price of a ramen without any toppings is 700 yen.


program main
  implicit none

  integer :: n,x,m(100)=0,i,mi,nok,j,t

  read(*,*)n,x
  do i = 1, n
    read(*,*)m(i)
  end do
  nok = x - sum(m)

  do i=1,N-1
     do j=i+1,N
        if(m(i) .gt. m(j))then
           t=m(i)
           m(i)=m(j)
           m(j)=t
        end if
     end do
  end do

  mi = m(1)
  write(*,*)n+nok/mi
end program main

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

Score : 200 points

Problem Statement

Akaki, a patissier, can make N kinds of doughnut using only a certain powder called "Okashi no Moto" (literally "material of pastry", simply called Moto below) as ingredient. These doughnuts are called Doughnut 1, Doughnut 2, ..., Doughnut N. In order to make one Doughnut i (1 ≤ i ≤ N), she needs to consume m_i grams of Moto. She cannot make a non-integer number of doughnuts, such as 0.5 doughnuts.

Now, she has X grams of Moto. She decides to make as many doughnuts as possible for a party tonight. However, since the tastes of the guests differ, she will obey the following condition:

For each of the N kinds of doughnuts, make at least one doughnut of that kind.

At most how many doughnuts can be made here? She does not necessarily need to consume all of her Moto. Also, under the constraints of this problem, it is always possible to obey the condition.

Constraints

2 ≤ N ≤ 100

1 ≤ m_i ≤ 1000

m_1 + m_2 + ... + m_N ≤ X ≤ 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X
m_1
m_2
:
m_N

Output

Print the maximum number of doughnuts that can be made under the condition.

Sample Input 1

3 1000
120
100
140

Sample Output 1

9

She has 1000 grams of Moto and can make three kinds of doughnuts. If she makes one doughnut for each of the three kinds, she consumes 120 + 100 + 140 = 360 grams of Moto. From the 640 grams of Moto that remains here, she can make additional six Doughnuts 2. This is how she can made a total of nine doughnuts, which is the maximum.

Sample Input 2

4 360
90
90
90
90

Sample Output 2

4

Making one doughnut for each of the four kinds consumes all of her Moto.

Sample Input 3

5 3000
150
130
150
130
110

Sample Output 3

26

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.


INTEGER N,X
INTEGER,ALLOCATABLE,DIMENSION(:)::M
READ*,N,X
ALLOCATE(M(N))
READ*,M
PRINT"(I0)",(X-SUM(M))/MINVAL(M)+N
END


program main
   implicit none
   integer N, X, countN, i
   integer, allocatable :: m(:)
   read(*, *) N, X
   allocate(m(1: N))
   do i = 1, N
       read(*, *) m(i)
   end do
   do i = 1, N
      X = X - m(i)
   end do
   countN = N
   countN = countN + X / minval(m(:))
   write(*, *) countN
end program main


program main
   implicit none
   integer A, B, C, X, Y, countN
   read(*, *) A, B, C, X, Y
   countN = 0
   if(X == Y) then
       if(A+B > 2 * C) then
	       countN = 2 * X * C
	   else
	       countN = (A + B) * X
	   end if
   else if (X > Y) then
       if(A+B > 2 * C) then
	       countN = 2 * Y * C
	   else
	       countN = (A + B) * Y
	   end if
	   X = X - Y
	   if(A > 2 * C) then
	       countN = countN + 2 * X * C
	   else
	       countN = countN + X * A
	   end if
   else
       if(A+B > 2 * C) then
	       countN = 2 * X * C
	   else
	       countN = (A + B) * X
	   end if
	   Y = Y - X
	   if(B > 2 * C) then
	       countN = countN + 2 * Y * C
	   else
	       countN = countN + Y * B
	   end if
   end if
   write(*, *) countN
end program main

Score : 300 points

Problem Statement

"Pizza At", a fast food chain, offers three kinds of pizza: "A-pizza", "B-pizza" and "AB-pizza". A-pizza and B-pizza are completely different pizzas, and AB-pizza is one half of A-pizza and one half of B-pizza combined together. The prices of one A-pizza, B-pizza and AB-pizza are A yen, B yen and C yen (yen is the currency of Japan), respectively.

Nakahashi needs to prepare X A-pizzas and Y B-pizzas for a party tonight. He can only obtain these pizzas by directly buying A-pizzas and B-pizzas, or buying two AB-pizzas and then rearrange them into one A-pizza and one B-pizza. At least how much money does he need for this? It is fine to have more pizzas than necessary by rearranging pizzas.

Constraints

1 ≤ A, B, C ≤ 5000

1 ≤ X, Y ≤ 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C X Y

Output

Print the minimum amount of money required to prepare X A-pizzas and Y B-pizzas.

Sample Input 1

1500 2000 1600 3 2

Sample Output 1

7900

It is optimal to buy four AB-pizzas and rearrange them into two A-pizzas and two B-pizzas, then buy additional one A-pizza.

Sample Input 2

1500 2000 1900 3 2

Sample Output 2

8500

It is optimal to directly buy three A-pizzas and two B-pizzas.

Sample Input 3

1500 2000 500 90000 100000

Sample Output 3

100000000

It is optimal to buy 200000 AB-pizzas and rearrange them into 100000 A-pizzas and 100000 B-pizzas. We will have 10000 more A-pizzas than necessary, but that is fine.


program sushi
implicit none

integer(8) :: i,j,k
integer(8) :: n,c
integer(8) :: x(200000)=0,v(200000)=0
integer(8) :: v_sum_a(200000)=0,v_sum_b(200000)=0
integer(8) :: ans1=0,ans2=0,ans3=0,ans4=0
integer(8) :: ans2_a(0:200000)=0,ans2_b(0:200000)=0,ans4_a(0:200000)=0,ans4_b(0:200000)=0
integer(8) :: val=0,ans2_best=0,ans4_best=0,ans=0

read(*,*) n,c

do i= 1,n
	read(*,*) x(i),v(i)
enddo

!時計周りに回した時のカロリー累積和
do i=1,n
	if(i == 1) then
		v_sum_a(i) = v(1)
	else
		v_sum_a(i) = v_sum_a(i-1) + v(i)
	endif
enddo

!反時計周りに回した時のカロリー累積和
do i=n,1,-1
	if(i == n) then
		v_sum_b(i) = v(n)
	else
		v_sum_b(i) = v_sum_b(i+1) + v(i)
	endif
enddo

!ans1 : 時計回りに進んで終了
do i=1,n
	ans1 = max(ans1,v_sum_a(i)-x(i))
enddo

!ans2 : 時計回りに進んでから、反時計回りに戻る
!まずは時計回りの最適値を求める。
!1～i番目の寿司の間で自由に取れて原点まで戻ったときの、カロリーの最大値
do i = 1,n
	val = v_sum_a(i) - 2 * x(i)
	ans2_a(i) = max(ans2_a(i-1),val)
enddo

!反時計周りでi～nまで取れるときの最大値
do i=n,1,-1
	val = v_sum_b(i) - (c - x(i))
	ans2_b(i) = max(ans2_b(i+1),val)
enddo

do i=0,n
	val = ans2_a(i) + ans2_b(i+1)
	ans2 = max(ans2,val)
enddo

!ans3 : 反時計回りに進んで終了
do i=1,n
	ans3 = max(ans3,v_sum_b(i)-(c-x(i)))
enddo

!ans4 : 反時計回りに進んでから、時計回りに戻る
!まずは時計回りの最適値を求める。
do i = 1,n
	val = v_sum_a(i) - x(i)
	ans4_a(i) = max(ans4_a(i-1),val)
enddo

!反時計周りでi～nまで取れるときの最大値
do i=n,1,-1
	val = v_sum_b(i) - 2 * (c - x(i))
	ans4_b(i) = max(ans4_b(i+1),val)
enddo

do i=0,n
	val = ans4_a(i) + ans4_b(i+1)
	ans4 = max(ans4,val)
enddo

ans = max(0,ans1,ans2,ans3,ans4)
write(*,*) ans

end program

Score : 500 points

Problem Statement

"Teishi-zushi", a Japanese restaurant, is a plain restaurant with only one round counter. The outer circumference of the counter is C meters. Customers cannot go inside the counter.

Nakahashi entered Teishi-zushi, and he was guided to the counter. Now, there are N pieces of sushi (vinegared rice with seafood and so on) on the counter. The distance measured clockwise from the point where Nakahashi is standing to the point where the i-th sushi is placed, is x_i meters. Also, the i-th sushi has a nutritive value of v_i kilocalories.

Nakahashi can freely walk around the circumference of the counter. When he reach a point where a sushi is placed, he can eat that sushi and take in its nutrition (naturally, the sushi disappears). However, while walking, he consumes 1 kilocalories per meter.

Whenever he is satisfied, he can leave the restaurant from any place (he does not have to return to the initial place). On balance, at most how much nutrition can he take in before he leaves? That is, what is the maximum possible value of the total nutrition taken in minus the total energy consumed? Assume that there are no other customers, and no new sushi will be added to the counter. Also, since Nakahashi has plenty of nutrition in his body, assume that no matter how much he walks and consumes energy, he never dies from hunger.

Constraints

1 ≤ N ≤ 10^5

2 ≤ C ≤ 10^{14}

1 ≤ x_1 < x_2 < ... < x_N < C

1 ≤ v_i ≤ 10^9

All values in input are integers.

Subscores

300 points will be awarded for passing the test set satisfying N ≤ 100.

Input

Input is given from Standard Input in the following format:

N C
x_1 v_1
x_2 v_2
:
x_N v_N

Output

If Nakahashi can take in at most c kilocalories on balance before he leaves the restaurant, print c.

Sample Input 1

3 20
2 80
9 120
16 1

Sample Output 1

191

There are three sushi on the counter with a circumference of 20 meters. If he walks two meters clockwise from the initial place, he can eat a sushi of 80 kilocalories. If he walks seven more meters clockwise, he can eat a sushi of 120 kilocalories. If he leaves now, the total nutrition taken in is 200 kilocalories, and the total energy consumed is 9 kilocalories, thus he can take in 191 kilocalories on balance, which is the largest possible value.

Sample Input 2

3 20
2 80
9 1
16 120

Sample Output 2

192

The second and third sushi have been swapped. Again, if he walks two meters clockwise from the initial place, he can eat a sushi of 80 kilocalories. If he walks six more meters counterclockwise this time, he can eat a sushi of 120 kilocalories. If he leaves now, the total nutrition taken in is 200 kilocalories, and the total energy consumed is 8 kilocalories, thus he can take in 192 kilocalories on balance, which is the largest possible value.

Sample Input 3

1 100000000000000
50000000000000 1

Sample Output 3

0

Even though the only sushi is so far that it does not fit into a 32-bit integer, its nutritive value is low, thus he should immediately leave without doing anything.

Sample Input 4

15 10000000000
400000000 1000000000
800000000 1000000000
1900000000 1000000000
2400000000 1000000000
2900000000 1000000000
3300000000 1000000000
3700000000 1000000000
3800000000 1000000000
4000000000 1000000000
4100000000 1000000000
5200000000 1000000000
6600000000 1000000000
8000000000 1000000000
9300000000 1000000000
9700000000 1000000000

Sample Output 4

6500000000

All these sample inputs above are included in the test set for the partial score.


program ABC094A
	integer(8)::A,B,X
	read(5,*)A,B,X

	if(X>A+B .or. A>X)then
		print'(A)',"NO"
	else
		print'(A)',"YES"
	end if
end program ABC094A

Score : 100 points

Problem Statement

There are a total of A + B cats and dogs.
Among them, A are known to be cats, but the remaining B are not known to be either cats or dogs.

Determine if it is possible that there are exactly X cats among these A + B animals.

Constraints

1 \leq A \leq 100

1 \leq B \leq 100

1 \leq X \leq 200

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B X

Output

If it is possible that there are exactly X cats, print YES; if it is impossible, print NO.

Sample Input 1

3 5 4

Sample Output 1

YES

If there are one cat and four dogs among the B = 5 animals, there are X = 4 cats in total.

Sample Input 2

2 2 6

Sample Output 2

NO

Even if all of the B = 2 animals are cats, there are less than X = 6 cats in total.

Sample Input 3

5 3 2

Sample Output 3

NO

Even if all of the B = 3 animals are dogs, there are more than X = 2 cats in total.


implicit none
integer(8) :: a,b,c,d
character(3) :: s
read(*,*) s

if ( s(1:1)/=s(2:2) .and. s(2:2)/=s(3:3) .and. s(3:3)/=s(1:1) ) then
    write(*,*)  "Yes"
else
    write(*,*) "No"
endif

end

Score : 100 points

Problem Statement

You are given a string S of length 3 consisting of a, b and c. Determine if S can be obtained by permuting abc.

Constraints

|S|=3

S consists of a, b and c.

Input

Input is given from Standard Input in the following format:

S

Output

If S can be obtained by permuting abc, print Yes; otherwise, print No.

Sample Input 1

bac

Sample Output 1

Yes

Swapping the first and second characters in bac results in abc.

Sample Input 2

bab

Sample Output 2

No

Sample Input 3

abc

Sample Output 3

Yes

Sample Input 4

aaa

Sample Output 4

No


      subroutine solve ( a, b, ans )
        integer*8 a, b, ans
        integer*8 lim, low, high
        integer*8 mid, mul
        if ( a .eq. b ) then
          ans = (a-1)*2
          return
        endif

        lim = a*b - 1
        low  = 1
        high = lshift(1, 30)
        do while( high - low > 1 )
          mid = (low + high) / 2
          mul = (mid+1)/2 * (mid+1 - (mid+1)/2)
          if ( mul .gt. lim ) then
            high = mid;
          else
            low = mid;
          endif
        enddo
        ans = low - 1
      end

      program main
        implicit none
        integer*8 I, Q
        integer*8 A, B, ans
        read(*,*) Q

        do I = 1, Q
          read(*,*) A, B
          call solve(A, B, ans)
          write(*,*) ans
        enddo
      end

Score : 700 points

Problem Statement

10^{10^{10}} participants, including Takahashi, competed in two programming contests.
In each contest, all participants had distinct ranks from first through 10^{10^{10}}-th.

The score of a participant is the product of his/her ranks in the two contests.

Process the following Q queries:

In the i-th query, you are given two positive integers A_i and B_i. Assuming that Takahashi was ranked A_i-th in the first contest and B_i-th in the second contest, find the maximum possible number of participants whose scores are smaller than Takahashi's.

Constraints

1 \leq Q \leq 100

1\leq A_i,B_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:

Q
A_1 B_1
:
A_Q B_Q

Output

For each query, print the maximum possible number of participants whose scores are smaller than Takahashi's.

Sample Input 1

8
1 4
10 5
3 3
4 11
8 9
22 40
8 36
314159265 358979323

Sample Output 1

1
12
4
11
14
57
31
671644785

Let us denote a participant who was ranked x-th in the first contest and y-th in the second contest as (x,y).

In the first query, (2,1) is a possible candidate of a participant whose score is smaller than Takahashi's. There are never two or more participants whose scores are smaller than Takahashi's, so we should print 1.


implicit none
integer(8) :: a,b,c,d
read(*,*) a,b,c,d

write(*,*) min(a,b) + min(c,d)

end

Score : 100 points

Problem Statement

You planned a trip using trains and buses.
The train fare will be A yen (the currency of Japan) if you buy ordinary tickets along the way, and B yen if you buy an unlimited ticket.
Similarly, the bus fare will be C yen if you buy ordinary tickets along the way, and D yen if you buy an unlimited ticket.

Find the minimum total fare when the optimal choices are made for trains and buses.

Constraints

1 \leq A \leq 1 000

1 \leq B \leq 1 000

1 \leq C \leq 1 000

1 \leq D \leq 1 000

All input values are integers.

Input

Input is given from Standard Input in the following format:

A
B
C
D

Output

Print the minimum total fare.

Sample Input 1

600
300
220
420

Sample Output 1

520

The train fare will be 600 yen if you buy ordinary tickets, and 300 yen if you buy an unlimited ticket.
Thus, the optimal choice for trains is to buy an unlimited ticket for 300 yen.
On the other hand, the optimal choice for buses is to buy ordinary tickets for 220 yen.

Therefore, the minimum total fare is 300 + 220 = 520 yen.

Sample Input 2

555
555
400
200

Sample Output 2

755

Sample Input 3

549
817
715
603

Sample Output 3

1152


program proA
  implicit none
  integer a,b,c,d

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

  write(*,*) min(a,b)+min(c,d)

end program proA


implicit none
integer :: a,b,c
read*,a,b,c

if( a+b>=c )then
  print*,'Yes'
else
  print*,'No'
end if
end

Score : 100 points

Problem Statement

An elementary school student Takahashi has come to a variety store.

He has two coins, A-yen and B-yen coins (yen is the currency of Japan), and wants to buy a toy that costs C yen. Can he buy it?

Note that he lives in Takahashi Kingdom, and may have coins that do not exist in Japan.

Constraints

All input values are integers.

1 \leq A, B \leq 500

1 \leq C \leq 1000

Input

Input is given from Standard Input in the following format:

A B C

Output

If Takahashi can buy the toy, print Yes; if he cannot, print No.

Sample Input 1

50 100 120

Sample Output 1

Yes

He has 50 + 100 = 150 yen, so he can buy the 120-yen toy.

Sample Input 2

500 100 1000

Sample Output 2

No

He has 500 + 100 = 600 yen, but he cannot buy the 1000-yen toy.

Sample Input 3

19 123 143

Sample Output 3

No

There are 19-yen and 123-yen coins in Takahashi Kingdom, which are rather hard to use.

Sample Input 4

19 123 142

Sample Output 4

Yes


implicit none
integer :: a,b
integer :: n,i,j,k,nstr
character(5) :: str
logical :: flg

read *,a,b

n = 0
do i = a, b
   write(str,'(i0)') i
   flg = .true.
   nstr = len_trim(str)
   k = nstr+1
   do j = 1, nstr/2
      k = k - 1
      if (str(j:j) .ne. str(k:k)) then
         flg = .false.
         exit
      endif
   enddo
   if (flg) then
      n = n + 1
   endif
enddo

write(6,'(i0)') n
stop
end

Score : 200 points

Problem Statement

Find the number of palindromic numbers among the integers between A and B (inclusive).
Here, a palindromic number is a positive integer whose string representation in base 10 (without leading zeros) reads the same forward and backward.

Constraints

10000 \leq A \leq B \leq 99999

All input values are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the number of palindromic numbers among the integers between A and B (inclusive).

Sample Input 1

11009 11332

Sample Output 1

4

There are four integers that satisfy the conditions: 11011, 11111, 11211 and 11311.

Sample Input 2

31415 92653

Sample Output 2

612


program ABC090C
    implicit none
    integer::n,m,ans=0,c
    read*,n,m
    if (n > m) then
        c = m
        m = n
        n = c
    end if
    if (n == 1 .AND. m == 1)then
        ans = 1
    else if (n == 1) then
        ans = m - 2
    else
        ans = (n-2)*(m-2)
    end if
    print*,ans
end program ABC090C

Score : 300 points

Problem Statement

There is a grid with infinitely many rows and columns. In this grid, there is a rectangular region with consecutive N rows and M columns, and a card is placed in each square in this region.
The front and back sides of these cards can be distinguished, and initially every card faces up.

We will perform the following operation once for each square contains a card:

For each of the following nine squares, flip the card in it if it exists: the target square itself and the eight squares that shares a corner or a side with the target square.

It can be proved that, whether each card faces up or down after all the operations does not depend on the order the operations are performed.
Find the number of cards that face down after all the operations.

Constraints

1 \leq N,M \leq 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

Print the number of cards that face down after all the operations.

Sample Input 1

2 2

Sample Output 1

0

We will flip every card in any of the four operations. Thus, after all the operations, all cards face up.

Sample Input 2

1 7

Sample Output 2

5

After all the operations, all cards except at both ends face down.

Sample Input 3

314 1592

Sample Output 3

496080


program sample
	implicit none
    integer ::N

    read(*,*) N

    write(*,*) N/3

    stop
end program sample

Score : 100 points

Problem Statement

There are N students in a school.

We will divide these students into some groups, and in each group they will discuss some themes.

You think that groups consisting of two or less students cannot have an effective discussion, so you want to have as many groups consisting of three or more students as possible.

Divide the students so that the number of groups consisting of three or more students is maximized.

Constraints

1 \leq N \leq 1000

All input values are integers.

Input

Input is given from Standard Input in the following format:

N

Output

If you can form at most x groups consisting of three or more students, print x.

Sample Input 1

8

Sample Output 1

2

For example, you can form a group of three students and another of five students.

Sample Input 2

2

Sample Output 2

0

Sometimes you cannot form any group consisting of three or more students, regardless of how you divide the students.

Sample Input 3

9

Sample Output 3

3


program main

implicit none

integer a
read *, a
print *, a/3

end program main


program name
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: n,i,j,k,cnt(5),ans
    character(10),allocatable:: s(:)
    character(5),parameter:: march = 'MARCH'
    character(1):: initial

    read*, n
    allocate(s(n))
    cnt(:) = 0
    do i=1,n
        read*, s(i)
        initial = s(i)(1:1)
        do j=1,5
            if (initial == march(j:j)) then
                cnt(j)=cnt(j)+1
                exit
            end if
        end do
    end do
    ans=0
    do i=1,3
        do j=i+1,4
            do k=j+1,5
                ans=ans+cnt(i)*cnt(j)*cnt(k)
            end do
        end do
    end do
    print'(i0)', ans
contains
subroutine swap(x,y)
    integer(int64):: x,y,t
    t=x;x=y;y=t
end subroutine
end program name

Score : 300 points

Problem Statement

There are N people. The name of the i-th person is S_i.

We would like to choose three people so that the following conditions are met:

The name of every chosen person begins with M, A, R, C or H.

There are no multiple people whose names begin with the same letter.

How many such ways are there to choose three people, disregarding order?

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

Constraints

1 \leq N \leq 10^5

S_i consists of uppercase English letters.

1 \leq |S_i| \leq 10

S_i \neq S_j (i \neq j)

Input

Input is given from Standard Input in the following format:

N
S_1
:
S_N

Output

If there are x ways to choose three people so that the given conditions are met, print x.

Sample Input 1

5
MASHIKE
RUMOI
OBIRA
HABORO
HOROKANAI

Sample Output 1

2

We can choose three people with the following names:

MASHIKE, RUMOI, HABORO

MASHIKE, RUMOI, HOROKANAI

Thus, we have two ways.

Sample Input 2

4
ZZ
ZZZ
Z
ZZZZZZZZZZ

Sample Output 2

0

Note that there may be no ways to choose three people so that the given conditions are met.

Sample Input 3

5
CHOKUDAI
RNG
MAKOTO
AOKI
RINGO

Sample Output 3

7


program prob4
    implicit none
    integer(16)::N, M
    integer(16)::i,j,ans,com
    integer::v(17)
    read(*,*) N

    v = 0
    M = N
    do i = 16, 0, -1
        if(N >= 10**i) then
            v(i+1) = N/(10**i)
            N = N - N/(10**i) * 10**i
        end if
    end do

    ans = sum(v)
    do i = 17, 1, -1
        if(v(i) > 0)then
            j = i
            exit
        end if
    end do

    if(M/10 > 0)then
        if(v(j) + v(j-1) > 9) then
            ans = v(j) + v(j-1) + 9*(j-2)
            com = 0
            do i = 1, j-2
                com = v(i)*10**(i-1) + v(j-1)*10**(j-2) + v(j)*10**j
            end do
            if(com > M)then
                ans = ans - 1
            end if
        else if(v(j) > 1)then
            ans = v(j) - 1 + 9*(j-1)
        else
            if(sum(v) - 1 < 9*(j-1))then
                ans = 9*(j-1)
            end if
        end if
    end if

    write(*,*) ans
    stop
end program

Score : 300 points

Problem Statement

Find the maximum possible sum of the digits (in base 10) of a positive integer not greater than N.

Constraints

1\leq N \leq 10^{16}

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the maximum possible sum of the digits (in base 10) of a positive integer not greater than N.

Sample Input 1

100

Sample Output 1

18

For example, the sum of the digits in 99 is 18, which turns out to be the maximum value.

Sample Input 2

9995

Sample Output 2

35

For example, the sum of the digits in 9989 is 35, which turns out to be the maximum value.

Sample Input 3

3141592653589793

Sample Output 3

137


program main
 implicit none
 integer N, A, i
 read*, N
 read*, A
 i = N / 500
 if( ( N - 500 * i ) <= A ) then
  print*, "Yes"
 else
  print*, "No"
 end if
end program main

Score: 100 points

Problem Statement

E869120 has A 1-yen coins and infinitely many 500-yen coins.

Determine if he can pay exactly N yen using only these coins.

Constraints

N is an integer between 1 and 10000 (inclusive).

A is an integer between 0 and 1000 (inclusive).

Input

Input is given from Standard Input in the following format:

N
A

Output

If E869120 can pay exactly N yen using only his 1-yen and 500-yen coins, print Yes; otherwise, print No.

Sample Input 1

2018
218

Sample Output 1

Yes

We can pay 2018 yen with four 500-yen coins and 18 1-yen coins, so the answer is Yes.

Sample Input 2

2763
0

Sample Output 2

No

When we have no 1-yen coins, we can only pay a multiple of 500 yen using only 500-yen coins. Since 2763 is not a multiple of 500, we cannot pay this amount.

Sample Input 3

37
514

Sample Output 3

Yes


program main
implicit none
integer:: n,i,j,b,alice,bob,gap
integer,allocatable:: a(:)

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

do j=1,n
do i=1,n-1
if(a(i)<a(i+1))then
b=a(i+1)
a(i+1)=a(i)
a(i)=b
endif
end do
end do

alice=0
bob=0
if(mod(n,2)==0)then
do i=1,n/2
alice=alice+a(i)
bob=bob+a(i+n/2)
end do
else
do i=1,(n+1)/2
alice=alice+a(i)
end do
do i=(n+3)/2,n
bob=bob+a(i)
end do
endif

gap=alice-bob
write(*,*) gap

end program main

Score: 200 points

Problem Statement

We have N cards. A number a_i is written on the i-th card.

Alice and Bob will play a game using these cards. In this game, Alice and Bob alternately take one card. Alice goes first.

The game ends when all the cards are taken by the two players, and the score of each player is the sum of the numbers written on the cards he/she has taken. When both players take the optimal strategy to maximize their scores, find Alice's score minus Bob's score.

Constraints

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

a_i \ (1 \leq i \leq N) is an integer between 1 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 a_3 ... a_N

Output

Print Alice's score minus Bob's score when both players take the optimal strategy to maximize their scores.

Sample Input 1

2
3 1

Sample Output 1

2

First, Alice will take the card with 3. Then, Bob will take the card with 1.
The difference of their scores will be 3 - 1 = 2.

Sample Input 2

3
2 7 4

Sample Output 2

5

First, Alice will take the card with 7. Then, Bob will take the card with 4. Lastly, Alice will take the card with 2. The difference of their scores will be 7 - 4 + 2 = 5. The difference of their scores will be 3 - 1 = 2.

Sample Input 3

4
20 18 2 18

Sample Output 3

18


program main
  implicit none

  integer :: n,i,j,t,a(100) = 0,al=0,bo=0,endal,endbo

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

  if (n == 1) then
    write(*,*)a(i)
    stop
  end if

  do i=1,N-1
     do j=i+1,N
        if(a(i) .gt. a(j))then
           t=a(i)
           a(i)=a(j)
           a(j)=t
        end if
     end do
  end do
  if (mod(n,2)==0) then
    endal = 2
    endbo = 1
  else
    endal = 1
    endbo = 2
  end if
  do i = n, endal, -2
    al = al + a(i)
  end do

  do i = n-1, endbo, -2
    bo = bo + a(i)
  end do
  write(*,*)al-bo
end program main


program test
  implicit none
  integer :: n, a(100), k, l, i
  a=0
  k=0
  l=0
  read(*,*) n
  read(*,*) a(1:n)
  call quick_sort(a(1:n))
  do i=1,n
  	if(mod(i,2)==1)then
    	k=k+a(n-i+1)
    else
    	l=l+a(n-i+1)
    end if
  end do
  write(*,*)k-l
  stop
contains
  recursive subroutine quick_sort(a)
    implicit none
    integer, intent(inout) :: a(:)
    integer :: p, c
    integer :: n, l, r
    n = size(a)
    l = 1
    r = n
    p = a((l+r)/2)+a(l)+a(r)-max(a((l+r)/2),a(l),a(r))-min(a((l+r)/2),a(l),a(r))
    do
      do while (a(l).lt.p)
        l = l+1
      end do
      do while (a(r).gt.p)
        r = r-1
      end do
      if (l.ge.r) exit
      c = a(l)
      a(l) = a(r)
      a(r) = c
      l = l+1
      r = r-1
    end do
    if (l.gt.2) call quick_sort(a(1:l-1))
    if (n.gt.r+1) call quick_sort(a(r+1:n))
  end subroutine quick_sort
end program test


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

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

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

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

    return
  end subroutine heapsort

program main
    integer N, diff
    integer, allocatable :: a(:)

    read(*,*) N
    allocate(a(N))
    read(*,*) a(:)

    call heapsort(N,a)

    diff=0
    do i=1,N
        if(mod(i,2)/=0)then
            diff = diff + a(N-i+1)
        else
            diff = diff - a(N-i+1)
        endif
    enddo

    write(*,*) diff

end program


program main
    integer H, W, i, j, path, N_black
    character, allocatable :: s(:,:)

    read(*,*) H, W
    allocate(s(H,W))
    do i = 1,H
        do j = 1, W
        read(s(i,j),*)
        enddo
    enddo

    N_black=0
    do i=1,H
        do j=1,W
            if(s(i,j)=='#') N_black=N_black+1
        enddo
    enddo

    write(*,*) H*W-(H+W-1)-N_black
end program

Score: 400 points

Problem statement

We have an H \times W grid whose squares are painted black or white. The square at the i-th row from the top and the j-th column from the left is denoted as (i, j).

Snuke would like to play the following game on this grid. At the beginning of the game, there is a character called Kenus at square (1, 1). The player repeatedly moves Kenus up, down, left or right by one square. The game is completed when Kenus reaches square (H, W) passing only white squares.

Before Snuke starts the game, he can change the color of some of the white squares to black. However, he cannot change the color of square (1, 1) and (H, W). Also, changes of color must all be carried out before the beginning of the game.

When the game is completed, Snuke's score will be the number of times he changed the color of a square before the beginning of the game. Find the maximum possible score that Snuke can achieve, or print -1 if the game cannot be completed, that is, Kenus can never reach square (H, W) regardless of how Snuke changes the color of the squares.

The color of the squares are given to you as characters s_{i, j}. If square (i, j) is initially painted by white, s_{i, j} is .; if square (i, j) is initially painted by black, s_{i, j} is #.

Constraints

H is an integer between 2 and 50 (inclusive).

W is an integer between 2 and 50 (inclusive).

s_{i, j} is . or # (1 \leq i \leq H, 1 \leq j \leq W).

s_{1, 1} and s_{H, W} are ..

Input

Input is given from Standard Input in the following format:

H W
s_{1, 1}s_{1, 2}s_{1, 3} ... s_{1, W}
s_{2, 1}s_{2, 2}s_{2, 3} ... s_{2, W}
: :
s_{H, 1}s_{H, 2}s_{H, 3} ... s_{H, W}

Output

Print the maximum possible score that Snuke can achieve, or print -1 if the game cannot be completed.

Sample Input 1

3 3
..#
#..
...

Sample Output 1

2

The score 2 can be achieved by changing the color of squares as follows:

Sample Input 2

10 37
.....................................
...#...####...####..###...###...###..
..#.#..#...#.##....#...#.#...#.#...#.
..#.#..#...#.#.....#...#.#...#.#...#.
.#...#.#..##.#.....#...#.#.###.#.###.
.#####.####..#.....#...#..##....##...
.#...#.#...#.#.....#...#.#...#.#...#.
.#...#.#...#.##....#...#.#...#.#...#.
.#...#.####...####..###...###...###..
.....................................

Sample Output 2

209


program main
    integer H, W, i, j, path, N_black
    character, allocatable :: s(:,:)

    read(*,*) H, W
    allocate(s(H,W))
    do i = 1,W
        read(*,*) s(:,i)
    enddo

    N_black=0
    do i=1,H
        do j=1,W
            if(s(i,j)=='#') N_black=N_black+1
        enddo
    enddo

    write(*,*) H*W-(H+W-1)-N_black
end program


implicit none
integer:: x,a,b,okane
read(5,*) x,a,b
 okane=x-a

do while (okane>=0)
    okane=okane-b
enddo

write(6,*) okane+b

end

Score : 100 points

Problem Statement

You went shopping to buy cakes and donuts with X yen (the currency of Japan).

First, you bought one cake for A yen at a cake shop.
Then, you bought as many donuts as possible for B yen each, at a donut shop.

How much do you have left after shopping?

Constraints

1 \leq A, B \leq 1 000

A + B \leq X \leq 10 000

X, A and B are integers.

Input

Input is given from Standard Input in the following format:

X
A
B

Output

Print the amount you have left after shopping.

Sample Input 1

1234
150
100

Sample Output 1

84

You have 1234 - 150 = 1084 yen left after buying a cake.
With this amount, you can buy 10 donuts, after which you have 84 yen left.

Sample Input 2

1000
108
108

Sample Output 2

28

Sample Input 3

579
123
456

Sample Output 3

0

Sample Input 4

7477
549
593

Sample Output 4

405


program probC
  implicit none
  integer*8,allocatable :: ame(:,:)
  integer*8 N,j,tot,i

  read(*,*) N
  allocate(ame(0:n+1,2))
  ame=0

  read(*,*) ame(1:n,1)
  read(*,*) ame(1:n,2)

  tot=0
  do j=1,n
     tot=tot+ame(j,1)
     if(sum(ame(j+1:n,1))>=sum(ame(j:n-1,2))) then
     else
        tot=tot+sum(ame(j:n-1,2))
        exit
     end if
  end do
  tot=tot+ame(n,2)

  write(*,*) tot

end program probC


program sample
    implicit none

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

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

    read(*,*) n
    allocate(x(n))
    allocate(y(n))
    read(*,*)x
    read(*,*)y
    a=0 !aは上の道の差
    c=X(1)
    do i=1,n-1

    if (x(i+1)+a >y(i) )then
        a=0
        b=i+1
        c=c+a+x(i+1)
    else
        a=x(i+1)-y(i) +a
        C=C+y(i)
    end if
    c=c+y(n)
    end do

    write(*,*) c

    stop
end program sample

Score : 300 points

Problem Statement

We have a 2 \times N grid. We will denote the square at the i-th row and j-th column (1 \leq i \leq 2, 1 \leq j \leq N) as (i, j).

You are initially in the top-left square, (1, 1).
You will travel to the bottom-right square, (2, N), by repeatedly moving right or down.

The square (i, j) contains A_{i, j} candies.
You will collect all the candies you visit during the travel.
The top-left and bottom-right squares also contain candies, and you will also collect them.

At most how many candies can you collect when you choose the best way to travel?

Constraints

1 \leq N \leq 100

1 \leq A_{i, j} \leq 100 (1 \leq i \leq 2, 1 \leq j \leq N)

Input

Input is given from Standard Input in the following format:

N
A_{1, 1} A_{1, 2} ... A_{1, N}
A_{2, 1} A_{2, 2} ... A_{2, N}

Output

Print the maximum number of candies that can be collected.

Sample Input 1

5
3 2 2 4 1
1 2 2 2 1

Sample Output 1

14

The number of collected candies will be maximized when you:

move right three times, then move down once, then move right once.

Sample Input 2

4
1 1 1 1
1 1 1 1

Sample Output 2

5

You will always collect the same number of candies, regardless of how you travel.

Sample Input 3

7
3 3 4 5 4 5 3
5 3 4 4 2 3 2

Sample Output 3

29

Sample Input 4

1
2
3

Sample Output 4

5


program main
  implicit none
  integer :: ab
  character(6) :: sa, sb, sab
  read(*, *) sa, sb
  sab = trim(sa) // trim(sb)
  read(sab, *) ab
  if (nint(ab ** 0.5) ** 2 == ab) then
    write(*, "(a)") "Yes"
  else
    write(*, "(a)") "No"
  end if
end program main

Score : 200 points

Problem Statement

AtCoDeer the deer has found two positive integers, a and b.
Determine whether the concatenation of a and b in this order is a square number.

Constraints

1 ≤ a,b ≤ 100

a and b are integers.

Input

Input is given from Standard Input in the following format:

a b

Output

If the concatenation of a and b in this order is a square number, print Yes; otherwise, print No.

Sample Input 1

1 21

Sample Output 1

Yes

As 121 = 11 × 11, it is a square number.

Sample Input 2

100 100

Sample Output 2

No

100100 is not a square number.

Sample Input 3

12 10

Sample Output 3

No


implicit none
integer:: n,i
integer,allocatable:: t(:),x(:),y(:),ta(:),xa(:),ya(:)

read(*,*) n
allocate(t(n))
allocate(x(n))
allocate(y(n))
allocate(ta(n))
allocate(xa(n))
allocate(ya(n))
do i=1,n
read(*,*) t(i),x(i),y(i)
end do
ta(1)=t(1)
xa(1)=x(1)
ya(1)=y(1)
do i=2,n
ta(i)=abs(t(i)-t(i-1))
xa(i)=abs(x(i)-x(i-1))
ya(i)=abs(y(i)-y(i-1))
end do

do i=1,n
if(ta(i)<xa(i)+ya(i).or.mod(ta(i),2)/=mod(xa(i)+ya(i),2))then
write(*,*) 'No'
goto 100
endif
end do

write(*,*) 'Yes'

100 end

Score : 300 points

Problem Statement

AtCoDeer the deer is going on a trip in a two-dimensional plane.
In his plan, he will depart from point (0, 0) at time 0, then for each i between 1 and N (inclusive), he will visit point (x_i,y_i) at time t_i.

If AtCoDeer is at point (x, y) at time t, he can be at one of the following points at time t+1: (x+1,y), (x-1,y), (x,y+1) and (x,y-1).
Note that he cannot stay at his place.
Determine whether he can carry out his plan.

Constraints

1 ≤ N ≤ 10^5

0 ≤ x_i ≤ 10^5

0 ≤ y_i ≤ 10^5

1 ≤ t_i ≤ 10^5

t_i < t_{i+1} (1 ≤ i ≤ N-1)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
t_1 x_1 y_1
t_2 x_2 y_2
:
t_N x_N y_N

Output

If AtCoDeer can carry out his plan, print Yes; if he cannot, print No.

Sample Input 1

2
3 1 2
6 1 1

Sample Output 1

Yes

For example, he can travel as follows: (0,0), (0,1), (1,1), (1,2), (1,1), (1,0), then (1,1).

Sample Input 2

1
2 100 100

Sample Output 2

No

It is impossible to be at (100,100) two seconds after being at (0,0).

Sample Input 3

2
5 1 1
100 1 1

Sample Output 3

No


program main
  integer, allocatable :: i(:, :)
  read (*, *) n
  allocate(i(3, 0:n))
  i(:, 0) = (/0, 0, 0/)
  read (*, *) i(:, 1:n)
  do k = 1, n
    jx = abs(i(2, k) - i(2, k - 1)) + abs(i(3, k) - i(3, k - 1))
    jt = i(1, k) - i(1, k - 1)
    if (jx > jt .or. ibits(jx, 0, 1) /= ibits(jt, 0, 1)) then
      write (*, *) "No"
      stop
    endif
  enddo
  write (*, *) "Yes"
end


program Traveling
    implicit none
    integer(4):: n,i
    integer(4),allocatable:: t(:),x(:),y(:),d(:)

    read*, n
    allocate(t(n),x(n),y(n),d(n))

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

    d(:) = x(:)+y(:)

    do i=1,n
        if (t(i) < d(i) .or. (mod(t(i),2) /= mod(d(i),2))) then
            print*, 'No'
            stop
        end if
    end do

    print*, 'Yes'

end program Traveling


program tenquestionten
 integer,allocatable,dimension(:) :: X
 integer,allocatable,dimension(:) :: Y
 integer,allocatable,dimension(:) :: T
 integer N, i, j, sum, Xl, Yl, Tl, Z
 read(*,*) N
 allocate(X(N+1))
 allocate(Y(N+1))
 allocate(T(N+1))
 sum=0
 X(1) = 0
 Y(1) = 0
 T(1) = 0
  do i=2,N+1
  read(*,*) T(i), X(i), Y(i)
  end do
  do i=1,N
  Xl=abs(X(i+1)-X(i))
  Yl=abs(Y(i+1)-Y(i))
  Tl=abs(T(i+1)-T(i))
  Z=Xl+Yl
   if (Tl.ge.Z) then
    if ((mod(Z,2)).eq.(mod(Tl,2))) then
    sum = sum + 1
    end if
   end if
  end do
 if (sum.eq.N) then
 write(*,'(A)') "Yes"
 else if (sum.ne.N) then
 write(*,'(A)') "No"
 end if
end program


program tenquestionten
 integer,allocatable,dimension(:) :: X
 integer,allocatable,dimension(:) :: Y
 integer,allocatable,dimension(:) :: T
 integer N, i, j, sum, Xl, Yl, Tl, Z
 read(*,*) N
 allocate(X(N+1))
 allocate(Y(N+1))
 allocate(T(N+1))
 sum=0
 X(1) = 0
 Y(1) = 0
 T(1) = 0
  do i=2,N+1
  read(*,*) T(i), X(i), Y(i)
  end do
  do i=1,N
  Xl=abs(X(i+1)-X(i))
  Yl=abs(Y(i+1)-Y(i))
  Tl=abs(T(i+1)-T(i))
  Z=Xl+Yl
   if (Tl.ge.Z) then
    if ((mod(Z,2)).eq.(mod(Tl,2))) then
    sum = sum + 1
    end if
   end if
  end do
 if (sum.eq.N) then
 write(*,'(A)') "YES"
 else if (sum.ne.N) then
 write(*,'(A)') "NO"
 end if
end program


program probC
  implicit none
  integer*8,allocatable :: t(:),x(:),y(:)
  integer*8 N,i,del_x,del_y,del_t,del_min

  read(*,*) N
  allocate(t(0:N),x(0:N),y(0:N))
  t=0
  x=0
  y=0
  do i=1,n
     read(*,*) t(i),x(i),y(i)
  end do

  do i=1,N
     del_t=t(i)-t(i-1)
     del_x=x(i)-x(i-1)
     del_y=y(i)-y(i-1)
     del_min=del_t-abs(del_x)-abs(del_y)
     select case(del_min)
     case(:(-1))
        write(*,*) 'No'
        stop
     end select
     select case(mod(del_min,2))
     case(1)
        write(*,*) 'No'
        stop
     end select
  end do

  write(*,*) 'Yes'
end program probC


program proA
  implicit none
  integer n,a,b

  read(*,*) n,a,b

  if(mod(b-a,2)==0) then
     write(*,*) 'Alice'
  else
     write(*,*) 'Borys'
  end if

end program proA

Score : 300 points

Problem Statement

A game is played on a strip consisting of N cells consecutively numbered from 1 to N.

Alice has her token on cell A. Borys has his token on a different cell B.

Players take turns, Alice moves first.
The moving player must shift his or her token from its current cell X to the neighboring cell on the left, cell X-1, or on the right, cell X+1.
Note that it's disallowed to move the token outside the strip or to the cell with the other player's token.
In one turn, the token of the moving player must be shifted exactly once.

The player who can't make a move loses, and the other player wins.

Both players want to win. Who wins if they play optimally?

Constraints

2 \leq N \leq 100

1 \leq A < B \leq N

All input values are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print Alice if Alice wins, Borys if Borys wins, and Draw if nobody wins.

Sample Input 1

5 2 4

Sample Output 1

Alice

Alice can move her token to cell 3.
After that, Borys will be unable to move his token to cell 3, so he will have to move his token to cell 5.
Then, Alice moves her token to cell 4. Borys can't make a move and loses.

Sample Input 2

2 1 2

Sample Output 2

Borys

Alice can't make the very first move and loses.

Sample Input 3

58 23 42

Sample Output 3

Borys


module mod_bitset
  implicit none
  type bitset
    integer :: k
    integer(8), pointer :: bits(:)
  end type bitset
  private
  public :: bitset, init_bitset, set, get, shift_right, shift_left
  public :: shift_or, or_with, and_with
contains
  subroutine init_bitset(bs,n)
    implicit none
    type(bitset), intent(inout) :: bs
    integer, intent(in) :: n
    integer :: k
    k = (n+63)/64
    bs%k = k
    allocate(bs%bits(0:k))
    bs%bits = 0_8
    return
  end subroutine init_bitset
  subroutine set(bs,idx,bit)
    implicit none
    type(bitset), intent(in) :: bs
    integer, intent(in) :: idx
    logical, intent(in) :: bit
    integer :: i, k
    i = idx/64
    k = mod(idx,64)
    if (bit) then
      bs%bits(i) = or(bs%bits(i),lshift(1_8,k))
    else
      bs%bits(i) = and(bs%bits(i),not(lshift(1_8,k)))
    end if
    return
  end subroutine set
  function get(bs,idx) result(bit)
    implicit none
    type(bitset), intent(in) :: bs
    integer, intent(in) :: idx
    logical :: bit
    integer :: i, k
    i = idx/64
    k = mod(idx,64)
    bit = and(bs%bits(i),ishft(1_8,k)).ne.0_8
    return
  end function get
  subroutine shift_right(bs,n)
    implicit none
    type(bitset), intent(in) :: bs
    integer, intent(in) :: n
    integer :: i, k, x, s
    i = n/64
    k = mod(n,64)
    do x = 0, bs%k
      s = x+i
      if (s.gt.bs%k) then
        bs%bits(x) = 0_8
      end if
      bs%bits(x) = rshift(bs%bits(s),k)
      if (k.gt.0.and.s+1.le.bs%k) then
        bs%bits(x) = or(bs%bits(x),lshift(bs%bits(s+1),64-k))
      end if
    end do
    return
  end subroutine shift_right
  subroutine shift_left(bs,n)
    implicit none
    type(bitset), intent(in) :: bs
    integer, intent(in) :: n
    integer :: i, k, x, s
    i = n/64
    k = mod(n,64)
    do x = bs%k, 0, -1
      s = x-i
      if (s.lt.0) then
        bs%bits(x) = 0_8
      end if
      bs%bits(x) = lshift(bs%bits(s),k)
      if (k.gt.0.and.s.gt.0) then
        bs%bits(x) = or(bs%bits(x),rshift(bs%bits(s-1),64-k))
      end if
    end do
    return
  end subroutine shift_left
  subroutine shift_or(bs,n)
    implicit none
    type(bitset), intent(in) :: bs
    integer, intent(in) :: n
    integer :: i, k, x, s
    i = n/64
    k = mod(n,64)
    do x = bs%k, i+1, -1
      s = x-i
      bs%bits(x) = lshift(bs%bits(s),k)
      if (k.gt.0) then
        bs%bits(x) = or(bs%bits(x),rshift(bs%bits(s-1),64-k))
      end if
    end do
    bs%bits(i) = or(bs%bits(i),lshift(bs%bits(0),k))
    return
  end subroutine shift_or
  subroutine or_with(bs1,bs2)
    implicit none
    type(bitset), intent(in) :: bs1, bs2
    integer :: i
    do i = 0, bs1%k
      bs1%bits(i) = or(bs1%bits(i),bs2%bits(i))
    end do
    return
  end subroutine or_with
  subroutine and_with(bs1,bs2)
    implicit none
    type(bitset), intent(in) :: bs1, bs2
    integer :: i
    do i = 0, bs1%k
      bs1%bits(i) = and(bs1%bits(i),bs2%bits(i))
    end do
    return
  end subroutine and_with
end module mod_bitset
program median_sum
  use mod_bitset
  implicit none
  type(bitset) :: bs
  integer :: n, a(2000), s, i
  a = 0
  read(*,*) n
  read(*,*) a(1:n)
  call init_bitset(bs,2000*n)
  do i = 1, n
    call shift_or(bs,a(i))
    call set(bs,a(i),.true.)
  end do
  s = sum(a(1:n))
  do i = (s+1)/2, 2000*n
    if (get(bs,i)) then
      write(*,'(i0)') i
      stop
    end if
  end do
  stop
end program median_sum

Score : 700 points

Problem Statement

You are given N integers A_1, A_2, ..., A_N.

Consider the sums of all non-empty subsequences of A. There are 2^N - 1 such sums, an odd number.

Let the list of these sums in non-decreasing order be S_1, S_2, ..., S_{2^N - 1}.

Find the median of this list, S_{2^{N-1}}.

Constraints

1 \leq N \leq 2000

1 \leq A_i \leq 2000

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the median of the sorted list of the sums of all non-empty subsequences of A.

Sample Input 1

3
1 2 1

Sample Output 1

2

In this case, S = (1, 1, 2, 2, 3, 3, 4). Its median is S_4 = 2.

Sample Input 2

1
58

Sample Output 2

58

In this case, S = (58).


implicit none
integer:: n,i,j,a,count
integer,allocatable:: d(:)

read(*,*) n
allocate(d(n))
read(*,*) d

do i=1,n-1
do j=i+1,n
if(d(i)>d(j))then
a=d(j)
d(j)=d(i)
d(i)=a
endif
end do
end do

count=1
do i=1,n-1
if(d(i)<d(i+1))then
count=count+1
endif
end do

write(*,*) count

end

Score : 200 points

Problem Statement

An X-layered kagami mochi (X ≥ 1) is a pile of X round mochi (rice cake) stacked vertically where each mochi (except the bottom one) has a smaller diameter than that of the mochi directly below it. For example, if you stack three mochi with diameters of 10, 8 and 6 centimeters from bottom to top in this order, you have a 3-layered kagami mochi; if you put just one mochi, you have a 1-layered kagami mochi.

Lunlun the dachshund has N round mochi, and the diameter of the i-th mochi is d_i centimeters. When we make a kagami mochi using some or all of them, at most how many layers can our kagami mochi have?

Constraints

1 ≤ N ≤ 100

1 ≤ d_i ≤ 100

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
d_1
:
d_N

Output

Print the maximum number of layers in a kagami mochi that can be made.

Sample Input 1

4
10
8
8
6

Sample Output 1

3

If we stack the mochi with diameters of 10, 8 and 6 centimeters from bottom to top in this order, we have a 3-layered kagami mochi, which is the maximum number of layers.

Sample Input 2

3
15
15
15

Sample Output 2

1

When all the mochi have the same diameter, we can only have a 1-layered kagami mochi.

Sample Input 3

7
50
30
50
100
50
80
30

Sample Output 3

4


implicit none
integer:: n,i,j,a,count
integer,allocatable:: d(:)

read(*,*) n
allocate(d(n))
read(*,*) d

do i=1,n-1
do j=i+1,n
if(d(i)>d(j))then
a=d(j+1)
d(j+1)=d(j)
d(j)=a
endif
end do
end do

count=1
do i=1,n-1
if(d(i)<d(i+1))then
count=count+1
endif
end do

end


Score : 100 points

Problem Statement

How many hours do we have until New Year at M o'clock (24-hour notation) on 30th, December?

Constraints

1≤M≤23

M is an integer.

Input

Input is given from Standard Input in the following format:

M

Output

If we have x hours until New Year at M o'clock on 30th, December, print x.

Sample Input 1

21

Sample Output 1

27

We have 27 hours until New Year at 21 o'clock on 30th, December.

Sample Input 2

12

Sample Output 2

36


program prob8
    implicit none
    integer :: m
    read(*,*) m

    write(*,*) 48-m

    stop
contains
end program prob8


program newyeartime
    implicit none
    integer M

    read(*,*) M

    write(*,*) 48-M

end program newyeartime


program atcoder
  implicit none
  integer m
  read(*,*) m    ! 変数mに、標準入力から数字を読み込み代入する
  write(*,'(i0)') 48 - m   ! 答えである(48 - m)を出力
end program atcoder


integer a,b,c,d
read*, a,b,c,d
if(a+b>c+d) then
	print*, "Left"
else if(a+b<c+d) then
	print*, "Right"
else
	print*, "Balanced"
end if
end

Score : 100 points

Problem Statement

A balance scale tips to the left if L>R, where L is the total weight of the masses on the left pan and R is the total weight of the masses on the right pan. Similarly, it balances if L=R, and tips to the right if L<R.

Takahashi placed a mass of weight A and a mass of weight B on the left pan of a balance scale, and placed a mass of weight C and a mass of weight D on the right pan.

Print Left if the balance scale tips to the left; print Balanced if it balances; print Right if it tips to the right.

Constraints

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

All input values are integers.

Input

Input is given from Standard Input in the following format:

A B C D

Output

Print Left if the balance scale tips to the left; print Balanced if it balances; print Right if it tips to the right.

Sample Input 1

3 8 7 1

Sample Output 1

Left

The total weight of the masses on the left pan is 11, and the total weight of the masses on the right pan is 8. Since 11>8, we should print Left.

Sample Input 2

3 4 5 2

Sample Output 2

Balanced

The total weight of the masses on the left pan is 7, and the total weight of the masses on the right pan is 7. Since 7=7, we should print Balanced.

Sample Input 3

1 7 6 4

Sample Output 3

Right

The total weight of the masses on the left pan is 8, and the total weight of the masses on the right pan is 10. Since 8<10, we should print Right.


integer(8) A,B
integer cnt
read*,A,B

cnt=0
do while(A<=B)
  A=A*2
  cnt=cnt+1
end do

print"(i0)",cnt
end

Score : 300 points

Problem Statement

As a token of his gratitude, Takahashi has decided to give his mother an integer sequence.
The sequence A needs to satisfy the conditions below:

A consists of integers between X and Y (inclusive).

For each 1\leq i \leq |A|-1, A_{i+1} is a multiple of A_i and strictly greater than A_i.

Find the maximum possible length of the sequence.

Constraints

1 \leq X \leq Y \leq 10^{18}

All input values are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

Print the maximum possible length of the sequence.

Sample Input 1

3 20

Sample Output 1

3

The sequence 3,6,18 satisfies the conditions.

Sample Input 2

25 100

Sample Output 2

3

Sample Input 3

314159265 358979323846264338

Sample Output 3

31


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

Score : 100 points

Problem Statement

You are given two positive integers a and b.
Let x be the average of a and b.
Print x rounded up to the nearest integer.

Constraints

a and b are integers.

1 \leq a, b \leq 100

Input

Input is given from Standard Input in the following format:

a b

Output

Print x rounded up to the nearest integer.

Sample Input 1

1 3

Sample Output 1

2

The average of 1 and 3 is 2.0, and it will be rounded up to the nearest integer, 2.

Sample Input 2

7 4

Sample Output 2

6

The average of 7 and 4 is 5.5, and it will be rounded up to the nearest integer, 6.

Sample Input 3

5 5

Sample Output 3

5


program main
  implicit none
  character(100) :: s, t
  read(*, *) s, t
  if (sorted(trim(s), .false.) <sorted(trim(t), .true.)) then
    write(*, "(a)") "Yes"
  else
    write(*, "(a)") "No"
  end if
  contains

  function sorted(s, reverse)
    implicit none
    character(*), intent(in) :: s
    logical, intent(in) :: reverse
    character(len(s)) :: sorted
    integer :: i, j
    sorted = s
    do i = 2, len(s)  ! [1, i] の範囲を昇順に保つ
      do j = i, 2, -1
        if (sorted(j:j) < sorted(j - 1:j - 1) .neqv. reverse) then
          call swap(sorted(j:j), sorted(j - 1:j - 1))
        else
          exit
        end if
      end do
    end do
  end function sorted

  subroutine swap(x, y)
    implicit none
    character(1), intent(inout) :: x, y
    character(1) :: tmp
    tmp = x
    x = y
    y = tmp
  end subroutine swap
end program main

Score : 200 points

Problem Statement

You are given strings s and t, consisting of lowercase English letters.
You will create a string s' by freely rearranging the characters in s.
You will also create a string t' by freely rearranging the characters in t.
Determine whether it is possible to satisfy s' < t' for the lexicographic order.

Notes

For a string a = a_1 a_2 ... a_N of length N and a string b = b_1 b_2 ... b_M of length M, we say a < b for the lexicographic order if either one of the following two conditions holds true:

N < M and a_1 = b_1, a_2 = b_2, ..., a_N = b_N.

There exists i (1 \leq i \leq N, M) such that a_1 = b_1, a_2 = b_2, ..., a_{i - 1} = b_{i - 1} and a_i < b_i. Here, letters are compared using alphabetical order.

For example, xy < xya and atcoder < atlas.

Constraints

The lengths of s and t are between 1 and 100 (inclusive).

s and t consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s
t

Output

If it is possible to satisfy s' < t', print Yes; if it is not, print No.

Sample Input 1

yx
axy

Sample Output 1

Yes

We can, for example, rearrange yx into xy and axy into yxa. Then, xy < yxa.

Sample Input 2

ratcode
atlas

Sample Output 2

Yes

We can, for example, rearrange ratcode into acdeort and atlas into tslaa. Then, acdeort < tslaa.

Sample Input 3

cd
abc

Sample Output 3

No

No matter how we rearrange cd and abc, we cannot achieve our objective.

Sample Input 4

w
ww

Sample Output 4

Yes

Sample Input 5

zzz
zzz

Sample Output 5

No


program sample
    implicit none
    character(100000)::s,t
    integer(8) :: i,j,k,m,n
    integer(8)::a,b,c,d
    integer(8),allocatable::x(:),y(:)

    read(*,*)n
    allocate(x(n),y(n))
    read(*,*)x
    m=0
    y(:)=0
    do i=1,n
        if(x(i)>n)then
            m=m+1
        else
            a=x(i)
            y(a)=y(a)+1
        endif
    end do
    do i=1,n
        if(y(i)>i)then
            m=m+y(i)-i
        elseif(y(i)<i)then
            m=m+y(i)
        end if
    end do

    write(*,*)m
    stop
end program sample

Score : 300 points

Problem Statement

You are given a sequence of positive integers of length N, a = (a_1, a_2, ..., a_N).
Your objective is to remove some of the elements in a so that a will be a good sequence.

Here, an sequence b is a good sequence when the following condition holds true:

For each element x in b, the value x occurs exactly x times in b.

For example, (3, 3, 3), (4, 2, 4, 1, 4, 2, 4) and () (an empty sequence) are good sequences, while (3, 3, 3, 3) and (2, 4, 1, 4, 2) are not.

Find the minimum number of elements that needs to be removed so that a will be a good sequence.

Constraints

1 \leq N \leq 10^5

a_i is an integer.

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 number of elements that needs to be removed so that a will be a good sequence.

Sample Input 1

4
3 3 3 3

Sample Output 1

1

We can, for example, remove one occurrence of 3. Then, (3, 3, 3) is a good sequence.

Sample Input 2

5
2 4 1 4 2

Sample Output 2

2

We can, for example, remove two occurrences of 4. Then, (2, 1, 2) is a good sequence.

Sample Input 3

6
1 2 2 3 3 3

Sample Output 3

0

Sample Input 4

1
1000000000

Sample Output 4

1

Remove one occurrence of 10^9. Then, () is a good sequence.

Sample Input 5

8
2 7 1 8 2 8 1 8

Sample Output 5

5


integer N
integer(16),allocatable,dimension(:)::A
integer(16) tmp,cnt,ans
read*,N
allocate(A(N))
read*,A
call heapsort(N,A)
ans=0
tmp=A(1);cnt=0
do i=1,N
  if(tmp==A(i))then
    cnt=cnt+1
  else
    if(cnt>=tmp)cnt=cnt-tmp
    ans=ans+cnt

    cnt=1;tmp=A(i)
  endif
end do
if(cnt>=tmp)cnt=cnt-tmp
ans=ans+cnt

print"(i0)",ans
contains

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

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


program A081
    implicit none
    character(3) :: s
    integer(4) :: i, sum

    read(5,*) s
    sum = 0
    do i = 1, 3
        if (s(i:i) == "1") then
            sum = sum + 1
        end if

    end do

    write(6,*) sum

end program A081

Score : 200 points

Problem Statement

There are N positive integers written on a blackboard: A_1, ..., A_N.

Snuke can perform the following operation when all integers on the blackboard are even:

Replace each integer X on the blackboard by X divided by 2.

Find the maximum possible number of operations that Snuke can perform.

Constraints

1 \leq N \leq 200

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 maximum possible number of operations that Snuke can perform.

Sample Input 1

3
8 12 40

Sample Output 1

2

Initially, [8, 12, 40] are written on the blackboard.
Since all those integers are even, Snuke can perform the operation.

After the operation is performed once, [4, 6, 20] are written on the blackboard.
Since all those integers are again even, he can perform the operation.

After the operation is performed twice, [2, 3, 10] are written on the blackboard.
Now, there is an odd number 3 on the blackboard, so he cannot perform the operation any more.

Thus, Snuke can perform the operation at most twice.

Sample Input 2

4
5 6 8 10

Sample Output 2

0

Since there is an odd number 5 on the blackboard already in the beginning, Snuke cannot perform the operation at all.

Sample Input 3

6
382253568 723152896 37802240 379425024 404894720 471526144

Sample Output 3

8


program atcoder
    implicit none
    integer :: N, i00, i01, counter, devide
    integer, allocatable :: a(:)

    read *, N
    allocate(a(N))
    read *, (a(i00), i00 = 1, N )
    counter = 0

    do i00 = 1, 1000
        devide = 0
        do i01 = 1, N
            if(mod(a(i01), 2) == 0) then
                devide = devide + 1
            end if
        end do
        if(devide == N) then
            counter = counter + 1
            do i01 = 1, N
                a(i01) = a(i01)/2
            end do
        else
            exit
        end if
    end do

    print '(i0)', counter
    deallocate(a)

end program atcoder


      PROGRAM B
      IMPLICIT NONE
      INTEGER :: N, i, ans, f,g, counter, index, s
      INTEGER,ALLOCATABLE :: A(:)
      LOGICAL :: flag1

      read*,N
      ALLOCATE( A(N) )
      read*,A(1:N)

      ans = 0
      flag1 = .false.

      outer:DO s = 1,10**9

        inner:DO index=1,N
          IF( .not.MOD(A(index),2)==0 )THEN
            flag1 = .true.
          END IF
        END DO inner

        IF(flag1)THEN
          EXIT
        END IF
        A(:) = A(:)/2
        ans = ans + 1

      END DO outer

      print'(I0)', ans

      END PROGRAM


program tenquestionthree
    implicit none
    integer n, i, x
    integer,allocatable,dimension(:) ::A
    x=30
    read(*,*) n
    allocate(A(n))
     do i=1,n
     read(*,*) A(i)
     end do
     do i=1,n
      do while ((mod(A(i),2**x)).ne.0)
      x=x-1
      end do
    end do
    write(*,*) x
end program


INTEGER N,A
INTEGER(16),ALLOCATABLE,DIMENSION(:)::X
READ*,N
A=30
ALLOCATE(X(N))
READ*,X
DO I=1,N
  DO WHILE(MOD(X(I),2**A)/=0)
    A=A-1
  END DO
END DO
PRINT"(I0)",A
END


program blackboard
	implicit none
    integer :: N,devided,i,j
    integer,allocatable :: A(:)

    j=0
    devided=0
    read(*,*) N
    allocate(A(N))
    read(*,*) A
    do
    	do i =1,N
        	!write(*,*) mod(A(i),2)
            if(mod(A(i),2)==1) then
        		j=1 ! Owarimasu
                exit
            else
            	A(i)=A(i)/2
            endif
        end do

        if(j==1) then
        	exit
        endif
        devided=devided+1
    end do
    write(*,'(i0)') devided
    deallocate(A)

end program blackboard


program ABC081C
    implicit none
    integer(8)::N,K,val_count=0,i,ans=0
    integer(8),allocatable::A(:),C(:)
    read*,N,K
    allocate(A(N))
    allocate(C(N))
    read*,A
    C=0
    do i=1,N
        if(C(A(i))==0)val_count=val_count+1
        C(A(i))=C(A(i))+1
    end do

    if(val_count<=K)then
        print'(i0)',0
    else
        val_count=val_count-K
        call heapsort(N,C)
        do i=1,N
            if(val_count==0)exit
            if(C(i)>0)then
                ans=ans+C(i)
                val_count=val_count-1
            end if
        end do
        print'(i0)',ans
    end if
end program ABC081C

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.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

Score : 300 points

Problem Statement

Takahashi has N balls. Initially, an integer A_i is written on the i-th ball.

He would like to rewrite the integer on some balls so that there are at most K different integers written on the N balls.

Find the minimum number of balls that Takahashi needs to rewrite the integers on them.

Constraints

1 \leq K \leq N \leq 200000

1 \leq A_i \leq N

All input values are integers.

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 ... A_N

Output

Print the minimum number of balls that Takahashi needs to rewrite the integers on them.

Sample Input 1

5 2
1 1 2 2 5

Sample Output 1

1

For example, if we rewrite the integer on the fifth ball to 2, there are two different integers written on the balls: 1 and 2.
On the other hand, it is not possible to rewrite the integers on zero balls so that there are at most two different integers written on the balls, so we should print 1.

Sample Input 2

4 4
1 1 2 2

Sample Output 2

0

Already in the beginning, there are two different integers written on the balls, so we do not need to rewrite anything.

Sample Input 3

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

Sample Output 3

3


program name
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: n,k,i,nk,ans
    integer(int32), allocatable:: a(:), cnt(:), num(:)

    read*, n,k
    allocate(a(n))
    read*, a(:)
    allocate(cnt(maxval(a)), source=0)

    do i=1,n
        cnt(a(i))=cnt(a(i))+1
    end do
    deallocate(a)

    nk=0
    do i=1,size(cnt)
        if (cnt(i) /= 0) nk=nk+1
    end do

    allocate(num(nk))
    nk=0

    do i=1,size(cnt)
        if (cnt(i) /= 0) then
            nk=nk+1
            num(nk)=cnt(i)
        end if
    end do
    deallocate(cnt)
    call merge_sort(num,1,nk)
    ans=0
    i=0
    do while (nk-i > k)
        i=i+1
        ans=ans+num(i)
    end do
    print'(i0)',ans
contains
    recursive subroutine merge_sort(ar, fst, lst)
        integer(4),intent(inout):: ar(:)
        integer(4),intent(in):: fst,lst
        integer(4):: mdl

        if (lst-fst < 2) then
            if (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


integer N,K
integer,allocatable,dimension(:)::A
integer B(200000),sizeB
integer tmp,tmpcnt,ans
read*,N,K
allocate(A(N))
read*,A
call heapsort(N,A)
tmp=A(1);tmpcnt=0;sizeB=0
do i=1,N
  if(A(i)==tmp)then
    tmpcnt=tmpcnt+1
  else
    sizeB=sizeB+1
    B(sizeB)=tmpcnt
    tmp=A(i)
    tmpcnt=1
  endif
end do
sizeB=sizeB+1
B(sizeB)=tmpcnt
call heapsort(sizeB,B(1:sizeB))
if(sizeB>K)then
  ans=sum(B(1:sizeB-K))
else
  ans=0
endif
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


integer n, a, b
read *, n, a, b

if(n*a <= b) then
    write(*,'(I0)') n*a
else
    write(*,'(I0)') b
endif
end

Score : 100 points

Problem Statement

You are parking at a parking lot. You can choose from the following two fee plans:

Plan 1: The fee will be A×T yen (the currency of Japan) when you park for T hours.

Plan 2: The fee will be B yen, regardless of the duration.

Find the minimum fee when you park for N hours.

Constraints

1≤N≤20

1≤A≤100

1≤B≤2000

All input values are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

When the minimum fee is x yen, print the value of x.

Sample Input 1

7 17 120

Sample Output 1

119

If you choose Plan 1, the fee will be 7×17=119 yen.

If you choose Plan 2, the fee will be 120 yen.

Thus, the minimum fee is 119 yen.

Sample Input 2

5 20 100

Sample Output 2

100

The fee might be the same in the two plans.

Sample Input 3

6 18 100

Sample Output 3

100


program recording
  implicit none
  integer :: n, k, s, t, c, x(200000,30), i, j, m
  s = 0
  x = 0
  read(*,*) n, k
  do i = 1, n
    read(*,*) s, t, c
    x(2*s-1,c) = x(2*s-1,c) + 1
    x(2*t,c) = x(2*t,c) - 1
  end do
  do j = 1, k
    do i = 2, 200000
      x(i,j) = x(i,j) + x(i-1,j)
    end do
  end do
  m = 0
  do i = 1, 200000
    c = sum(x(i,1:k))
    if (m.lt.c) m = c
  end do
  write(*,'(i0)') m
  stop
end program recording

Score : 400 points

Problem Statement

Joisino is planning to record N TV programs with recorders.

The TV can receive C channels numbered 1 through C.

The i-th program that she wants to record will be broadcast from time s_i to time t_i (including time s_i but not t_i) on Channel c_i.

Here, there will never be more than one program that are broadcast on the same channel at the same time.

When the recorder is recording a channel from time S to time T (including time S but not T), it cannot record other channels from time S-0.5 to time T (including time S-0.5 but not T).

Find the minimum number of recorders required to record the channels so that all the N programs are completely recorded.

Constraints

1≤N≤10^5

1≤C≤30

1≤s_i<t_i≤10^5

1≤c_i≤C

If c_i=c_j and i≠j, either t_i≤s_j or s_i≥t_j.

All input values are integers.

Input

Input is given from Standard Input in the following format:

N C
s_1 t_1 c_1
:
s_N t_N c_N

Output

When the minimum required number of recorders is x, print the value of x.

Sample Input 1

3 2
1 7 2
7 8 1
8 12 1

Sample Output 1

2

Two recorders can record all the programs, for example, as follows:

With the first recorder, record Channel 2 from time 1 to time 7. The first program will be recorded. Note that this recorder will be unable to record other channels from time 0.5 to time 7.

With the second recorder, record Channel 1 from time 7 to time 12. The second and third programs will be recorded. Note that this recorder will be unable to record other channels from time 6.5 to time 12.

Sample Input 2

3 4
1 3 2
3 4 4
1 4 3

Sample Output 2

3

There may be a channel where there is no program to record.

Sample Input 3

9 4
56 60 4
33 37 2
89 90 3
32 43 1
67 68 3
49 51 3
31 32 3
70 71 1
11 12 3

Sample Output 3

2


program recording
  implicit none
  integer :: n, k, s, t, c, x(100001,30), i, j, m
  s = 0
  x = 0
  read(*,*) n, k
  do i = 1, n
    read(*,*) s, t, c
    x(s,c) = x(s,c) + 1
    x(t+1,c) = x(t+1,c) - 1
  end do
  do j = 1, k
    do i = 1, n
      x(i+1,j) = x(i+1,j) + x(i,j)
    end do
  end do
  m = 0
  do i = 1, n
    c = sum(x(i,:))
    if (m.lt.c) m = c
  end do
  write(*,'(i0)') m
  stop
end program recording


program sample
    implicit none
    character(len=100000)::s
    integer(8) :: i,j,m,n,a,b,c,x(3)
    integer(8),allocatable :: y(:)

    read(*,*) s
    x(:)=0
    n=len_trim(s)
    do i=1,n
        if (s(i:i) .eq. 'a')then
            x(1)=x(1)+1
        else if (s(i:i) .eq. 'b')then
            x(2)=x(2)+1
        else
            x(3)=x(3)+1
        end if
    end do

    if (abs(x(1)-x(2))<2 .and. abs(x(2)-x(3))<2 .and. abs(x(1)-x(3))<2 )then
     write(*,*) 'Yes'
    else
        write(*,*)'No'
    end if

    stop
end program sample

Score : 400 points

Problem Statement

Snuke has a string S consisting of three kinds of letters: a, b and c.

He has a phobia for palindromes, and wants to permute the characters in S so that S will not contain a palindrome of length 2 or more as a substring. Determine whether this is possible.

Constraints

1 \leq |S| \leq 10^5

S consists of a, b and c.

Input

Input is given from Standard Input in the following format:

S

Output

If the objective is achievable, print YES; if it is unachievable, print NO.

Sample Input 1

abac

Sample Output 1

YES

As it stands now, S contains a palindrome aba, but we can permute the characters to get acba, for example, that does not contain a palindrome of length 2 or more.

Sample Input 2

aba

Sample Output 2

NO

Sample Input 3

babacccabab

Sample Output 3

YES


program main
	implicit none
    integer(8)::len1,a,b,c,i,nmax,nmin
    character(10**5+1)::s
    read(*,*) s
    a=0
    b=0
    c=0
    len1=len_trim(s)
    do i=1,len1
    	if(s(i:i)=='a')then
        	a=a+1
        else if (s(i:i)=='b')then
        	b=b+1
        else
        	c=c+1
        end if
    end do
    nmax=max(a,max(b,c))
    nmin=min(a,min(b,c))
    if(nmax-nmin<=1)then
    	write(*,*) 'Yes'
    else
    	write(*,*)'No'
    end if
    stop
end program main


 IMPLICIT NONE
 INTEGER :: N, A, B, C, D
 READ*, N
 A = N / 1000
 B = (N - A*1000) / 100
 C = (N - A*1000 - B*100) / 10
 D = (N - A*1000 - B*100 - C*10)

 IF ( A == B ) THEN
     IF ( A == C ) THEN
     	PRINT*, "Yes"
     END IF
 ELSE IF( B == C ) THEN
     IF( B == D ) THEN
         PRINT*, "Yes"
     END IF
 ELSE
     PRINT*, "No"
 END IF
END

Score : 100 points

Problem Statement

We call a 4-digit integer with three or more consecutive same digits, such as 1118, good.

You are given a 4-digit integer N. Answer the question: Is N good?

Constraints

1000 ≤ N ≤ 9999

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If N is good, print Yes; otherwise, print No.

Sample Input 1

1118

Sample Output 1

Yes

N is good, since it contains three consecutive 1.

Sample Input 2

7777

Sample Output 2

Yes

An integer is also good when all the digits are the same.

Sample Input 3

1234

Sample Output 3

No


program ABC_079_A_GoodInteger
   implicit none
   character(4) N
   read(*, *) N
   if((N(2:2) == N(1:1) .and. N(2:2) == N(3:3)) .or. (N(3:3) == N(2:2) .and. N(3:3) == N(4:4))) then
       write(*, *) 'Yes'
   else
       write(*, *) 'No'
   end if
end program ABC_079_A_GoodInteger


program solve
  integer  :: n
  INTEGER(8) :: L0
  INTEGER(8) :: L1
  INTEGER(8) :: tmp
  read (*,*) n

  L0 = 2
  L1 = 1
  do i = 1, n-1
    tmp = L0 + L1
    L0 = L1
    L1 = tmp
  end do
  ! i0 は整数を左寄せでprintするフォーマットの意味
  PRINT '(i0)', L1
end program solve

Score : 200 points

Problem Statement

It is November 18 now in Japan. By the way, 11 and 18 are adjacent Lucas numbers.

You are given an integer N. Find the N-th Lucas number.

Here, the i-th Lucas number L_i is defined as follows:

L_0=2

L_1=1

L_i=L_{i-1}+L_{i-2} (i≥2)

Constraints

1≤N≤86

It is guaranteed that the answer is less than 10^{18}.

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the N-th Lucas number.

Sample Input 1

5

Sample Output 1

11

L_0=2

L_1=1

L_2=L_0+L_1=3

L_3=L_1+L_2=4

L_4=L_2+L_3=7

L_5=L_3+L_4=11

Thus, the 5-th Lucas number is 11.

Sample Input 2

86

Sample Output 2

939587134549734843


program main
    use,intrinsic :: iso_fortran_env
    implicit none
    character(1):: x,y

    read*, x,y

    if (x<y)then
        print'(a)', '<'
    else if (x==y) then
        print'(a)', '='
    else
        print'(a)', '>'
    end if
end program main

Score : 100 points

Problem Statement

In programming, hexadecimal notation is often used.

In hexadecimal notation, besides the ten digits 0, 1, ..., 9, the six letters A, B, C, D, E and F are used to represent the values 10, 11, 12, 13, 14 and 15, respectively.

In this problem, you are given two letters X and Y. Each X and Y is A, B, C, D, E or F.

When X and Y are seen as hexadecimal numbers, which is larger?

Constraints

Each X and Y is A, B, C, D, E or F.

Input

Input is given from Standard Input in the following format:

X Y

Output

If X is smaller, print <; if Y is smaller, print >; if they are equal, print =.

Sample Input 1

A B

Sample Output 1

<

10 < 11.

Sample Input 2

E C

Sample Output 2

>

14 > 12.

Sample Input 3

F F

Sample Output 3

=

15 = 15.


program main
   implicit none
   integer x,y,z
   read(*,*)x,y,Z
   write(*,*)(x-z)/(y+z)

end program main

Score : 200 points

Problem Statement

We have a long seat of width X centimeters.
There are many people who wants to sit here. A person sitting on the seat will always occupy an interval of length Y centimeters.

We would like to seat as many people as possible, but they are all very shy, and there must be a gap of length at least Z centimeters between two people, and between the end of the seat and a person.

At most how many people can sit on the seat?

Constraints

All input values are integers.

1 \leq X, Y, Z \leq 10^5

Y+2Z \leq X

Input

Input is given from Standard Input in the following format:

X Y Z

Output

Print the answer.

Sample Input 1

13 3 1

Sample Output 1

3

There is just enough room for three, as shown below:

Figure

Sample Input 2

12 3 1

Sample Output 2

2

Sample Input 3

100000 1 1

Sample Output 3

49999

Sample Input 4

64146 123 456

Sample Output 4

110

Sample Input 5

64145 123 456

Sample Output 5

109


program main
   implicit none
   integer X, Y, Z
   read(*, *) X, Y, Z
   write(*, *) (X-Z)/(Y+Z)
end program main


program name
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: n,m

    read*, n,m
    print'(i0)', (1900*m+100*(n-m))*2**m

end program name

Score : 300 points

Problem Statement

Takahashi is now competing in a programming contest, but he received TLE in a problem where the answer is YES or NO.

When he checked the detailed status of the submission, there were N test cases in the problem, and the code received TLE in M of those cases.

Then, he rewrote the code to correctly solve each of those M cases with 1/2 probability in 1900 milliseconds, and correctly solve each of the other N-M cases without fail in 100 milliseconds.

Now, he goes through the following process:

Submit the code.

Wait until the code finishes execution on all the cases.

If the code fails to correctly solve some of the M cases, submit it again.

Repeat until the code correctly solve all the cases in one submission.

Let the expected value of the total execution time of the code be X milliseconds. Print X (as an integer).

Constraints

All input values are integers.

1 \leq N \leq 100

1 \leq M \leq {\rm min}(N, 5)

Input

Input is given from Standard Input in the following format:

N M

Output

Print X, the expected value of the total execution time of the code, as an integer. It can be proved that, under the constraints in this problem, X is an integer not exceeding 10^9.

Sample Input 1

1 1

Sample Output 1

3800

In this input, there is only one case. Takahashi will repeatedly submit the code that correctly solves this case with 1/2 probability in 1900 milliseconds.

The code will succeed in one attempt with 1/2 probability, in two attempts with 1/4 probability, and in three attempts with 1/8 probability, and so on.

Thus, the answer is 1900 \times 1/2 + (2 \times 1900) \times 1/4 + (3 \times 1900) \times 1/8 + ... = 3800.

Sample Input 2

10 2

Sample Output 2

18400

The code will take 1900 milliseconds in each of the 2 cases, and 100 milliseconds in each of the 10-2=8 cases. The probability of the code correctly solving all the cases is 1/2 \times 1/2 = 1/4.

Sample Input 3

100 5

Sample Output 3

608000


program ABC_077_A_Rotation
   implicit none
   integer i, countN
   character(3) c1, c2
   read(*, *) c1, c2
   countN = 0
   do i = 1, 3
       if(c1(i:i) == c2(4-i:4-i)) then
	       countN = countN + 1
	   end if
   end do
   if(countN == 3) then
       write(*, *) 'YES'
   else
       write(*, *) 'NO'
   end if
end program ABC_077_A_Rotation

Score : 100 points

Problem Statement

You are given a grid with 2 rows and 3 columns of squares.
The color of the square at the i-th row and j-th column is represented by the character C_{ij}.

Write a program that prints YES if this grid remains the same when rotated 180 degrees, and prints NO otherwise.

Constraints

C_{i,j}(1 \leq i \leq 2, 1 \leq j \leq 3) is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

C_{11}C_{12}C_{13}
C_{21}C_{22}C_{23}

Output

Print YES if this grid remains the same when rotated 180 degrees; print NO otherwise.

Sample Input 1

pot
top

Sample Output 1

YES

This grid remains the same when rotated 180 degrees.

Sample Input 2

tab
bet

Sample Output 2

NO

This grid does not remain the same when rotated 180 degrees.

Sample Input 3

eye
eel

Sample Output 3

NO


implicit none
integer :: n
integer,allocatable :: a(:),b(:),c(:),dp(:)
integer :: i,j,ans,s

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

allocate(dp(0:n))
dp(0) = 0

j = 1
do i = 1, n
   do while (j < n .and. a(j) < b(i))
      j = j + 1
   enddo
   if (j == n .and. a(n) < b(i)) then
      dp(i:n) = n
      exit
   endif
   dp(i) = j - 1
enddo

ans = 0
j = 1
s = 0
do i = 1, n
   do while (j < n .and. b(j) < c(i))
      s = s + dp(j)
      j = j + 1
   enddo
   if (j == n .and. b(j) < c(i)) then
      s = s + dp(n)
      ans = ans + s * (n-i+1)
      exit
   endif
   ans = ans + s
enddo

print '(i0)',ans

stop

contains

subroutine cs11(Ncomb,a)
integer, intent(in) :: Ncomb
integer, intent(inout) :: a(Ncomb)
integer :: icomb, jcomb,rswp
logical :: flg_swp

icomb = Ncomb
flg_swp = .false.
do while (icomb > 1 .or. flg_swp)
   icomb = (icomb * 10) / 13
   if (icomb < 1) then
      icomb = 1
   else if (icomb == 9 .or. icomb == 10) then
      icomb = 11
   endif
   flg_swp = .false.
   do jcomb = 1, Ncomb-icomb
      if (a(jcomb) > a(jcomb+icomb)) then
         rswp = a(jcomb)
         a(jcomb) = a(jcomb+icomb)
         a(jcomb+icomb) = rswp
         flg_swp = .true.
      endif
   enddo
enddo
endsubroutine cs11

end

Score : 300 points

Problem Statement

The season for Snuke Festival has come again this year. First of all, Ringo will perform a ritual to summon Snuke. For the ritual, he needs an altar, which consists of three parts, one in each of the three categories: upper, middle and lower.

He has N parts for each of the three categories. The size of the i-th upper part is A_i, the size of the i-th middle part is B_i, and the size of the i-th lower part is C_i.

To build an altar, the size of the middle part must be strictly greater than that of the upper part, and the size of the lower part must be strictly greater than that of the middle part. On the other hand, any three parts that satisfy these conditions can be combined to form an altar.

How many different altars can Ringo build? Here, two altars are considered different when at least one of the three parts used is different.

Constraints

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9(1\leq i\leq N)

1 \leq B_i \leq 10^9(1\leq i\leq N)

1 \leq C_i \leq 10^9(1\leq i\leq N)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 ... A_N
B_1 ... B_N
C_1 ... C_N

Output

Print the number of different altars that Ringo can build.

Sample Input 1

2
1 5
2 4
3 6

Sample Output 1

3

The following three altars can be built:

Upper: 1-st part, Middle: 1-st part, Lower: 1-st part

Upper: 1-st part, Middle: 1-st part, Lower: 2-nd part

Upper: 1-st part, Middle: 2-nd part, Lower: 2-nd part

Sample Input 2

3
1 1 1
2 2 2
3 3 3

Sample Output 2

27

Sample Input 3

6
3 14 159 2 6 53
58 9 79 323 84 6
2643 383 2 79 50 288

Sample Output 3

87


Score : 100 points

Problem Statement

Takahashi is a user of a site that hosts programming contests.

When a user competes in a contest, the rating of the user (not necessarily an integer) changes according to the performance of the user, as follows:

Let the current rating of the user be a.

Suppose that the performance of the user in the contest is b.

Then, the new rating of the user will be the avarage of a and b.

For example, if a user with rating 1 competes in a contest and gives performance 1000, his/her new rating will be 500.5, the average of 1 and 1000.

Takahashi's current rating is R, and he wants his rating to be exactly G after the next contest.

Find the performance required to achieve it.

Constraints

0 \leq R, G \leq 4500

All input values are integers.

Input

Input is given from Standard Input in the following format:

R
G

Output

Print the performance required to achieve the objective.

Sample Input 1

2002
2017

Sample Output 1

2032

Takahashi's current rating is 2002.

If his performance in the contest is 2032, his rating will be the average of 2002 and 2032, which is equal to the desired rating, 2017.

Sample Input 2

4500
0

Sample Output 2

-4500

Although the current and desired ratings are between 0 and 4500, the performance of a user can be below 0.


program codefestival2017qualc
  implicit none
  integer :: n
  integer,dimension(:),allocatable :: a
  read *, n
  allocate(a(n))
  read *, a
  print *, 3**n-product(merge(2,1,mod(a,2)==0))
end program codefestival2017qualc

Score : 200 points

Problem Statement

We will say that two integer sequences of length N, x_1, x_2, ..., x_N and y_1, y_2, ..., y_N, are similar when |x_i - y_i| \leq 1 holds for all i (1 \leq i \leq N).

In particular, any integer sequence is similar to itself.

You are given an integer N and an integer sequence of length N, A_1, A_2, ..., A_N.

How many integer sequences b_1, b_2, ..., b_N are there such that b_1, b_2, ..., b_N is similar to A and the product of all elements, b_1 b_2 ... b_N, is even?

Constraints

1 \leq N \leq 10

1 \leq A_i \leq 100

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the number of integer sequences that satisfy the condition.

Sample Input 1

2
2 3

Sample Output 1

7

There are seven integer sequences that satisfy the condition:

1, 2

1, 4

2, 2

2, 3

2, 4

3, 2

3, 4

Sample Input 2

3
3 3 3

Sample Output 2

26

Sample Input 3

1
100

Sample Output 3

1

Sample Input 4

10
90 52 56 71 44 8 13 30 57 84

Sample Output 4

58921


program sample
    implicit none
    character(100000)::s,t
    character::top,las
    integer(8) :: i,j,m,n,a,b,c0,c1
    integer(8),allocatable :: x(:),y(:)

    read(*,*) s
    n=len_trim(s)
    a=1
    b=n
    c0=0
    c1=0
    m=0
    do i=1,n
        do
        if(s(a:a) .eq. 'x' .and. a<n)then
            a=a+1
            c0=c0+1
        elseif(a==n)then
            write(*,*)0
            stop
        else
            top=s(a:a)
            exit
        end if
        end do
        do
        if(s(b:b) .eq. 'x')then
            b=b-1
            c1=c1+1
        else
            las=s(b:b)
            exit
        end if
        end do
        if(top .ne. las)then
            write(*,*)-1
            stop
        else
            m=m+abs(c0-c1)
            a=a+1
            b=b-1
            c0=0
            c1=0
        end if
        if(a>=b)then
            exit
        endif
    end do

    write(*,*) m

    stop
end program sample

Score : 400 points

Problem Statement

We have a string s consisting of lowercase English letters.
Snuke can perform the following operation repeatedly:

Insert a letter x to any position in s of his choice, including the beginning and end of s.

Snuke's objective is to turn s into a palindrome.
Determine whether the objective is achievable. If it is achievable, find the minimum number of operations required.

Notes

A palindrome is a string that reads the same forward and backward.
For example, a, aa, abba and abcba are palindromes, while ab, abab and abcda are not.

Constraints

1 \leq |s| \leq 10^5

s consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s

Output

If the objective is achievable, print the number of operations required.
If it is not, print -1 instead.

Sample Input 1

xabxa

Sample Output 1

2

One solution is as follows (newly inserted x are shown in bold):

xabxa → xaxbxa → xaxbxax

Sample Input 2

ab

Sample Output 2

-1

No sequence of operations can turn s into a palindrome.

Sample Input 3

a

Sample Output 3

0

s is a palindrome already at the beginning.

Sample Input 4

oxxx

Sample Output 4

3

One solution is as follows:

oxxx → xoxxx → xxoxxx → xxxoxxx


program main
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: a,b,c

    read*, a,b,c
    if (a==b) then
        print'(i0)', c
    else if(b==c) then
        print'(i0)', a
    else
        print'(i0)', b
    end if
end program main

Score : 100 points

Problem Statement

You are given three integers, A, B and C.

Among them, two are the same, but the remaining one is different from the rest.

For example, when A=5,B=7,C=5, A and C are the same, but B is different.

Find the one that is different from the rest among the given three integers.

Constraints

-100 \leq A,B,C \leq 100

A, B and C are integers.

The input satisfies the condition in the statement.

Input

Input is given from Standard Input in the following format:

A B C

Output

Among A, B and C, print the integer that is different from the rest.

Sample Input 1

5 7 5

Sample Output 1

7

This is the same case as the one in the statement.

Sample Input 2

1 1 7

Sample Output 2

7

In this case, C is the one we seek.

Sample Input 3

-100 100 100

Sample Output 3

-100


program ABC075A
    implicit none
    integer(8)::A,B,C
    read(5,*)A,B,C

    if(A==B)then
        print'(i0)',C
    else if(B==C)then
        print'(i0)',A
    else if(A==C)then
        print'(i0)',B
    end if
end program ABC075A


module UnionFind
implicit none
integer,allocatable,dimension(:)::par
!parentより

contains
subroutine UFinit(N)
  integer :: N,i
  allocate(par(N))
    do i = 1, N
      par(i) = i
    end do
  end subroutine
  recursive function root(x) result (res)
    integer :: x, res
    if (par(x) == x)then
       res = x
    else
      res = root(par(x))
      par(x) = res
    end if
  end function

subroutine unite(x,y)
integer,intent(in):: x,y
integer xr,yr
xr = root(x)
yr = root(y)
if(xr /= yr) par(xr) = yr
end subroutine

end module UnionFind

use UnionFind
integer N,M
integer,allocatable,dimension(:)::a,b
integer ans,cnt
integer i,j
read*,N,M
allocate(A(M),B(M))
do i=1,M
  read*,A(i),B(i)
end do

ans=0
do i=1,M
  call UFinit(N)
  do j=1,M
    if(i==j)cycle
    call unite(a(j),b(j))
  end do
  cnt=0
  do j=1,N
    if(root(j)==j)cnt=cnt+1
  end do
  if(cnt>=2)then
    ans=ans+1
  endif
  deallocate(par)
end do
print"(i0)",ans
end

Score : 300 points

Problem Statement

You are given an undirected connected graph with N vertices and M edges that does not contain self-loops and double edges.

The i-th edge (1 \leq i \leq M) connects Vertex a_i and Vertex b_i.

An edge whose removal disconnects the graph is called a bridge.

Find the number of the edges that are bridges among the M edges.

Notes

A self-loop is an edge i such that a_i=b_i (1 \leq i \leq M).

Double edges are a pair of edges i,j such that a_i=a_j and b_i=b_j (1 \leq i<j \leq M).

An undirected graph is said to be connected when there exists a path between every pair of vertices.

Constraints

2 \leq N \leq 50

N-1 \leq M \leq min(N(N−1)⁄2,50)

1 \leq a_i<b_i \leq N

The given graph does not contain self-loops and double edges.

The given graph is connected.

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
a_2 b_2
:
a_M b_M

Output

Print the number of the edges that are bridges among the M edges.

Sample Input 1

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

Sample Output 1

4

The figure below shows the given graph:

The edges shown in red are bridges. There are four of them.

Sample Input 2

3 3
1 2
1 3
2 3

Sample Output 2

0

It is possible that there is no bridge.

Sample Input 3

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

Sample Output 3

5

It is possible that every edge is a bridge.


program name
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: n,m,i,j
    integer(int32), allocatable:: d(:),t(:)

    read*, n
    allocate(d(n))
    read*, d(:)
    read*, m
    allocate(t(m))
    read*, t(:)
    call merge_sort(d,1,n)
    call merge_sort(t,1,m)
    j=1
    do i=1,m
        if (t(i) /= d(j)) then
            do while(t(i) /= d(j))
                j=j+1
                if (j > n) then
                    print'(a)', 'NO'
                    stop
                end if
            end do
            j=j+1
        end if
    end do
    print'(a)', 'YES'
contains
recursive subroutine merge_sort(ar, fst, lst)
integer(int32),intent(inout):: ar(:)
integer(int32),intent(in):: fst,lst
integer(int32):: mdl

if (lst-fst < 2) then
    if (ar(fst) > ar(lst)) call 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(int32),intent(inout):: ar(:)
integer(int32),intent(in):: fst, mdl, lst
integer(int32),allocatable:: tmp(:)
integer(int32):: li, ri, ti

allocate(tmp(lst-fst+1))

li=fst
ri=mdl+1
ti=1

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

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

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

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

Score : 200 points

Problem Statement

Rng is preparing a problem set for a qualification round of CODEFESTIVAL.

He has N candidates of problems. The difficulty of the i-th candidate is D_i.

There must be M problems in the problem set, and the difficulty of the i-th problem must be T_i. Here, one candidate of a problem cannot be used as multiple problems.

Determine whether Rng can complete the problem set without creating new candidates of problems.

Constraints

1 \leq N \leq 200,000

1 \leq D_i \leq 10^9

1 \leq M \leq 200,000

1 \leq T_i \leq 10^9

All numbers in the input are integers.

Partial Score

100 points will be awarded for passing the test set satisfying N \leq 100 and M \leq 100.

Input

Input is given from Standard Input in the following format:

N
D_1 D_2 ... D_N
M
T_1 T_2 ... T_M

Output

Print YES if Rng can complete the problem set without creating new candidates of problems; print NO if he cannot.

Sample Input 1

5
3 1 4 1 5
3
5 4 3

Sample Output 1

YES

Sample Input 2

7
100 200 500 700 1200 1600 2000
6
100 200 500 700 1600 1600

Sample Output 2

NO

Not enough 1600s.

Sample Input 3

1
800
5
100 100 100 100 100

Sample Output 3

NO

Sample Input 4

15
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
9
5 4 3 2 1 2 3 4 5

Sample Output 4

YES


program ioi_to_oio
  implicit none
  integer :: n, a(500000), m, i
  character(500000) :: s
  a = 0
  m = 0
  read(*,*) n
  read(*,*) s
  do i = 1, n
    if (s(i:i).eq."1") a(i) = 1
  end do
  i = 1
  do while (i.le.n)
    if (a(i).eq.1) then
      if (i.ge.3.and.a(i-2)-a(i-1).eq.1) then
        a(i-2) = 0
        a(i-1) = 1
        a(i) = 0
        i = i-2
        m = m+1
        cycle
      end if
    end if
    i = i+1
  end do
  write(*,'(i0)') m
  stop
end program ioi_to_oio

Score : 700 points

Problem Statement

N cells are arranged in a row.
Some of them may contain tokens.
You are given a string s that consists of 0s and 1s.
If the i-th character of s is 1, the i-th cell (from left) contains a token.
Otherwise, it doesn't contain a token.

Snuke wants to perform the following operation as many times as possible.
In each operation, he chooses three consecutive cells.
Let's call the cells X, Y, Z from left to right.
In order for the operation to be valid, both X and Z must contain tokens and Y must not contain a token.
Then, he removes these two tokens and puts a new token on Y.

How many operations can he perform if he performs operations in the optimal way?

Constraints

1 \leq N \leq 500,000

|s| = N

Each character in s is either 0 or 1.

Input

Input is given from Standard Input in the following format:

N
s

Output

Print the answer.

Sample Input 1

7
1010101

Sample Output 1

2

For example, he can perform two operations in the following way:

Perform an operation on the last three cells. Now the string that represents tokens becomes 1010010.

Perform an operation on the first three cells. Now the string that represents tokens becomes 0100010.

Note that the choice of operations matters.
For example, if he chooses three cells in the middle first, he can perform no more operations.

Sample Input 2

50
10101000010011011110001001111110000101010111100110

Sample Output 2

10


integer N
integer A,B
integer biri,biripoint
read*,N
biri=0
do i=1,N
  read*,a,b
  if(a>biri)then
    biri=a
    biripoint=b
  endif
end do
print"(i0)",biri+biripoint
end

Score : 200 points

Problem Statement

A group of people played a game. All players had distinct scores, which are positive integers.

Takahashi knows N facts on the players' scores. The i-th fact is as follows: the A_i-th highest score among the players is B_i.

Find the maximum possible number of players in the game.

Constraints

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9(1\leq i\leq N)

0 \leq B_i \leq 10^9(1\leq i\leq N)

If i ≠ j, A_i ≠ A_j.

There exists a possible outcome of the game that are consistent with the facts.

All input values are integers.

Inputs

Input is given from Standard Input in the following format:

N
A_1 B_1
:
A_N B_N

Outputs

Print the maximum possible number of players in the game.

Sample Input 1

3
4 7
2 9
6 2

Sample Output 1

8

The maximum possible number of players is achieved when, for example, the players have the following scores: 12,9,8,7,5,2,1,0.

Sample Input 2

5
1 10
3 6
5 2
4 4
2 8

Sample Output 2

7

Sample Input 3

2
1 1000000000
1000000000 1

Sample Output 3

1000000001