program main
    implicit none
    integer n
    integer, allocatable, dimension(:) :: x
    integer i, j, d
    character s*1000000

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

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

    do j = 2, n
        do i = 1, n+1-j
            d = x(i) - x(i+1)
            x(i) = xor(d,rshift(d,31))-rshift(d,31)
        end do
    end do

    write(*,*) x(1)

    deallocate(x)

end program

Score : 700 points

Problem Statement

Given is a sequence of N digits a_1a_2\ldots a_N, where each element is 1, 2, or 3.
Let x_{i,j} defined as follows:

x_{1,j} := a_j \quad (1 \leq j \leq N)

x_{i,j} := | x_{i-1,j} - x_{i-1,j+1} | \quad (2 \leq i \leq N and 1 \leq j \leq N+1-i)

Find x_{N,1}.

Constraints

2 \leq N \leq 10^6

a_i = 1,2,3 (1 \leq i \leq N)

Input

Input is given from Standard Input in the following format:

N
a_1a_2\ldotsa_N

Output

Print x_{N,1}.

Sample Input 1

4
1231

Sample Output 1

1

x_{1,1},x_{1,2},x_{1,3},x_{1,4} are respectively 1,2,3,1.

x_{2,1},x_{2,2},x_{2,3} are respectively |1-2| = 1,|2-3| = 1,|3-1| = 2.

x_{3,1},x_{3,2} are respectively |1-1| = 0,|1-2| = 1.

Finally, x_{4,1} = |0-1| = 1, so the answer is 1.

Sample Input 2

10
2311312312

Sample Output 2

0


program main
  implicit none
  integer(8) :: H,W,out
  integer(8) :: a,b, i,j

  read(*,*) H,W
  if (H == 1 .or. W == 1) then
     write(*,'(i0)') 1
     stop
  end if
  if (mod(W,2) == 0) then
     a = W/2
     b = W/2
  else
     a = W/2 + 1
     b = W/2
  end if
  out = 0
  if ( mod(H,2) == 0 ) then
     out = H/2*a + H/2*b
  else
     out = (H/2+1)*a + H/2*b
  end if
  write(*,'(i0)') out
end program main

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

Score : 200 points

Problem Statement

We have a board with H horizontal rows and W vertical columns of squares.
There is a bishop at the top-left square on this board.
How many squares can this bishop reach by zero or more movements?

Here the bishop can only move diagonally.
More formally, the bishop can move from the square at the r_1-th row (from the top) and the c_1-th column (from the left) to the square at the r_2-th row and the c_2-th column if and only if exactly one of the following holds:

r_1 + c_1 = r_2 + c_2

r_1 - c_1 = r_2 - c_2

For example, in the following figure, the bishop can move to any of the red squares in one move:

Constraints

1 \leq H, W \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H \ W

Output

Print the number of squares the bishop can reach.

Sample Input 1

4 5

Sample Output 1

10

The bishop can reach the cyan squares in the following figure:

Sample Input 2

7 3

Sample Output 2

11

The bishop can reach the cyan squares in the following figure:

Sample Input 3

1000000000 1000000000

Sample Output 3

500000000000000000

Submitted source-code file size, in bytes.

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

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


program main
  implicit none
  integer(8) :: H,W,out
  integer(8) :: a,b, i,j

  read(*,*) H,W
  if (mod(W,2) == 0) then
     a = W/2
     b = W/2
  else
     a = W/2 + 1
     b = W/2
  end if
  out = 0

  if ( mod(H,2) == 0 ) then
     out = H/2*a + H/2*b
  else
     out = (H/2+1)*a + H/2*b
  end if
  write(*,'(i0)') out
end program main


program main
    implicit none
    integer i,flag
    integer length
    character s*10

    flag = 0

    read(*,*) s

    length = len_trim(s)

    do i = 1,length
        if (mod(i,2) == 1) then
            if (s(i:i) == "h") then
                flag = 0
            else
                flag = 1
            end if
        else if (mod(i,2) == 0) then
            if (s(i:i) == "i") then
                flag = 0
            else
                flag = 1
            end if
        end if

        if (flag == 1) then
            write(*,*) "No"
            exit
        end if

    end do

    if (length >= 2) then
        if(flag == 0) then
            write(*,*) "Yes"
        end if
    else
        write(*,*) "No"
    end if

end program

Score : 100 points

Problem Statement

A Hitachi string is a concatenation of one or more copies of the string hi.

For example, hi and hihi are Hitachi strings, while ha and hii are not.

Given a string S, determine whether S is a Hitachi string.

Constraints

The length of S is between 1 and 10 (inclusive).

S is a string consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If S is a Hitachi string, print Yes; otherwise, print No.

Sample Input 1

hihi

Sample Output 1

Yes

hihi is the concatenation of two copies of hi, so it is a Hitachi string.

Sample Input 2

hi

Sample Output 2

Yes

Sample Input 3

ha

Sample Output 3

No


program main
    implicit none
    integer a, b, m
    integer, allocatable, dimension(:) :: ai, bi, x, y, c
    integer i, value
    integer a_min, b_min, value_min

    read(*,*) a, b, m

    allocate(ai(a))
    allocate(bi(b))
    allocate(x(m))
    allocate(y(m))
    allocate(c(m))

    read(*,*) ai
    read(*,*) bi
    do i = 1,m
        read(*,*) x(i),y(i),c(i)
    end do

    a_min = ai(1)
    b_min = bi(1)

    do i = 1,a
        if(ai(i) <= a_min) then
            a_min = ai(i)
        end if
    end do

    do i = 1,b
        if(bi(i) <= b_min) then
            b_min = bi(i)
        end if
    end do

    value_min = a_min + b_min

    do i = 1,m
        value = ai(x(i)) + bi(y(i)) - c(i)
        if(value <= value_min) then
            value_min = value
        end if
    end do

    write(*,*) value_min

end program

Score : 200 points

Problem Statement

You are visiting a large electronics store to buy a refrigerator and a microwave.

The store sells A kinds of refrigerators and B kinds of microwaves. The i-th refrigerator ( 1 \le i \le A ) is sold at a_i yen (the currency of Japan), and the j-th microwave ( 1 \le j \le B ) is sold at b_j yen.

You have M discount tickets. With the i-th ticket ( 1 \le i \le M ), you can get a discount of c_i yen from the total price when buying the x_i-th refrigerator and the y_i-th microwave together. Only one ticket can be used at a time.

You are planning to buy one refrigerator and one microwave. Find the minimum amount of money required.

Constraints

All values in input are integers.

1 \le A \le 10^5

1 \le B \le 10^5

1 \le M \le 10^5

1 \le a_i , b_i , c_i \le 10^5

1 \le x_i \le A

1 \le y_i \le B

c_i \le a_{x_i} + b_{y_i}

Input

Input is given from Standard Input in the following format:

A B M
a_1 a_2 ... a_A
b_1 b_2 ... b_B
x_1 y_1 c_1
\vdots
x_M y_M c_M

Output

Print the answer.

Sample Input 1

2 3 1
3 3
3 3 3
1 2 1

Sample Output 1

5

With the ticket, you can get the 1-st refrigerator and the 2-nd microwave for 3+3-1=5 yen.

Sample Input 2

1 1 2
10
10
1 1 5
1 1 10

Sample Output 2

10

Note that you cannot use more than one ticket at a time.

Sample Input 3

2 2 1
3 5
3 5
2 2 2

Sample Output 3

6

You can get the 1-st refrigerator and the 1-st microwave for 6 yen, which is the minimum amount to pay in this case.
Note that using a ticket is optional.


module mod_two_dim_merge_sort
  implicit none
  integer, private, parameter :: intkind = 8
contains
  logical function less(a, b) result(res)
    integer(intkind), intent(in) :: a(:), b(:)
    real(8) :: a1, b1
    a1 = a(1) / real(a(2) + 1, 8)
    b1 = b(1) / real(b(2) + 1, 8)
    res = a1 > b1 .or. (a1 == b1 .and. a(2) >= b(2))
  end
  subroutine merge_sort(a)
    integer(intkind), intent(inout) :: a(:, :)
    integer :: n, m, l, i, u
    n = size(a, 1)
    m = n
    l = 1
    do while (m > 1)
      do i = 1, m / 2
        u = min(2 * i * l, n)
        call merger(a(2 * (i - 1) * l + 1:(2 * i - 1) * l, :), &
        a((2 * i - 1) * l + 1:u, :))
      end do
      l = 2 * l
      m = (m + 1) / 2
    end do
  end
  subroutine merger(a1, a2)
    integer(intkind), intent(inout) :: a1(:, :), a2(:, :)
    integer(intkind) :: a(size(a1, 1) + size(a2, 1), size(a1, 2))
    integer :: i1, i2, n1, n2
    i1 = 1
    i2 = 1
    n1 = size(a1, 1)
    n2 = size(a2, 1)
    do while (i1 <= n1 .and. i2 <= n2)
      if (less(a1(i1, :), a2(i2, :))) then
        a(i1 + i2 - 1, :) = a1(i1, :)
        i1 = i1 + 1
      else
        a(i1 + i2 - 1, :) = a2(i2, :)
        i2 = i2 + 1
       end if
    end do
    if (i1 <= n1) then
      a(i1 + i2 - 1:n1 + n2, :) = a1(i1:n1, :)
    else
      a(i1 + i2 - 1:n1 + n2, :) = a2(i2:n2, :)
    end if
    a1 = a(1:n1, :)
    a2 = a(n1 + 1:n1 + n2, :)
  end
end module mod_two_dim_merge_sort
program manga_market
  use mod_two_dim_merge_sort
  implicit none
  integer(8), parameter :: inf = 3e18
  integer :: n, l, i, j, m = 0, p, q, h
  integer(8) :: t, r
  integer(8), allocatable :: c(:, :), dp(:, :), s(:)
  read(*,*) n, t
  allocate(c(n, 2))
  do i = 1, n
    read(*,*) c(i, :)
  end do
  call merge_sort(c(1:n, 1:2))
  l = n + 1
  do while (c(l - 1, 1) == 0)
    l = l - 1
  end do
  allocate(dp(0:28, 0:l - 1), s(0:n - 1 + 1))
  dp = inf
  dp(0, :) = 0
  do i = 1, l - 1
    do j = 1, min(28, i)
      dp(j, i) = dp(j, i - 1)
      if (dp(j - 1, i - 1) == inf) cycle
      dp(j, i) = min(dp(j, i), 1 + dp(j - 1, i - 1) + c(i, 1) * dp(j - 1, i - 1) + c(i, 2))
    end do
  end do
  s(0) = 0
  do i = n, l, -1
    s(n - i + 1) = s(n - i) + c(i, 2)
  end do
  do i = 0, min(28, l - 1)
    r = t - dp(i, l - 1)
    if (r < 0) cycle
    p = -1
    q = n - l + 1
    do while (q - p > 1)
      h = (p + q) / 2
      if (s(h) < r - h) then
        p = h
      else
        q = h
      end if
    end do
    m = max(m, i + q)
  end do
  write(*,'(i0)') m
end program manga_market

Score : 800 points

Problem Statement

There are N stores called Store 1, Store 2, \cdots, Store N. Takahashi, who is at his house at time 0, is planning to visit some of these stores.

It takes Takahashi one unit of time to travel from his house to one of the stores, or between any two stores.

If Takahashi reaches Store i at time t, he can do shopping there after standing in a queue for a_i \times t + b_i units of time. (We assume that it takes no time other than waiting.)

All the stores close at time T + 0.5. If Takahashi is standing in a queue for some store then, he cannot do shopping there.

Takahashi does not do shopping more than once in the same store.

Find the maximum number of times he can do shopping before time T + 0.5.

Constraints

All values in input are integers.

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

0 \leq a_i \leq 10^9

0 \leq b_i \leq 10^9

0 \leq T \leq 10^9

Input

Input is given from Standard Input in the following format:

N T
a_1 b_1
a_2 b_2
\vdots
a_N b_N

Output

Print the answer.

Sample Input 1

3 7
2 0
3 2
0 3

Sample Output 1

2

Here is one possible way to visit stores:

From time 0 to time 1: in 1 unit of time, he travels from his house to Store 1.

From time 1 to time 3: for 2 units of time, he stands in a queue for Store 1 to do shopping.

From time 3 to time 4: in 1 unit of time, he travels from Store 1 to Store 3.

From time 4 to time 7: for 3 units of time, he stands in a queue for Store 3 to do shopping.

In this way, he can do shopping twice before time 7.5.

Sample Input 2

1 3
0 3

Sample Output 2

0

Sample Input 3

5 21600
2 14
3 22
1 3
1 10
1 9

Sample Output 3

5

Sample Input 4

7 57
0 25
3 10
2 4
5 15
3 22
2 14
1 15

Sample Output 4

3


program main
  implicit none
  integer i
  character(3) S
  read(*, *) S
  !read(*, *) (L(i), i = 1,N)
  if (S == 'AAA') then
    write(*, *) 'No'
  elseif (S == 'BBB') then
    write(*, *) 'No'
  else
    write(*, *) 'Yes'
  endif
end program main

Score : 100 points

Problem Statement

In AtCoder City, there are three stations numbered 1, 2, and 3.

Each of these stations is operated by one of the two railway companies, A and B. A string S of length 3 represents which company operates each station. If S_i is A, Company A operates Station i; if S_i is B, Company B operates Station i.

To improve the transportation condition, for each pair of a station operated by Company A and one operated by Company B, there will be a bus service connecting them.

Determine if there is a pair of stations that will be connected by a bus service.

Constraints

Each character of S is A or B.

|S| = 3

Input

Input is given from Standard Input in the following format:

S

Output

If there is a pair of stations that will be connected by a bus service, print Yes; otherwise, print No.

Sample Input 1

ABA

Sample Output 1

Yes

Company A operates Station 1 and 3, while Company B operates Station 2.

There will be a bus service between Station 1 and 2, and between Station 2 and 3, so print Yes.

Sample Input 2

BBA

Sample Output 2

Yes

Company B operates Station 1 and 2, while Company A operates Station 3.

There will be a bus service between Station 1 and 3, and between Station 2 and 3, so print Yes.

Sample Input 3

BBB

Sample Output 3

No

Company B operates all the stations. Thus, there will be no bus service, so print No.


program station_and_bus
  implicit none
  character(3) :: s
  integer :: n = 0, i
  read(*,*) s
  do i = 1, 3
    n = n+ichar(s(i:i))-65
  end do
  if (n == 0 .or. n == 3) then
    write(*,'(a)') "No"
  else
    write(*,'(a)') "Yes"
  end if
end program station_and_bus


program main
      implicit none
      integer(8) :: n, a, b, i, j

      read(*,*) n, a, b

      i = mod(n, a+b)
      j = n/(a+b)

      if ( i < a ) then
        print *, j*a + i
      else
        print *, j*a + a
      end if

end program main

Score : 200 points

Problem Statement

Takahashi has many red balls and blue balls. Now, he will place them in a row.

Initially, there is no ball placed.

Takahashi, who is very patient, will do the following operation 10^{100} times:

Place A blue balls at the end of the row of balls already placed. Then, place B red balls at the end of the row.

How many blue balls will be there among the first N balls in the row of balls made this way?

Constraints

1 \leq N \leq 10^{18}

A, B \geq 0

0 < A + B \leq 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print the number of blue balls that will be there among the first N balls in the row of balls.

Sample Input 1

8 3 4

Sample Output 1

4

Let b denote a blue ball, and r denote a red ball. The first eight balls in the row will be bbbrrrrb, among which there are four blue balls.

Sample Input 2

8 0 4

Sample Output 2

0

He placed only red balls from the beginning.

Sample Input 3

6 2 4

Sample Output 3

2

Among bbrrrr, there are two blue balls.


program main
 implicit none
 integer :: a,b,n
  read *, n,a,b
   if (n<=a+b) then
    if (mod(n,a+b)==0) then
     write(*,'(i0)') b
    else if (mod(n,a+b)<=a) then
     write(*,'(i0)') 0
    else if (mod(n,a+b)>a) then
     write(*,'(i0)') mod(n,a+b)-a
    end if
   else if (n>a+b) then
    if (mod(n,a+b)==0) then
     write(*,'(i0)') b*n/(a+b)
    else if  (mod(n,a+b)<=a) then
     write(*,'(i0)') (n-mod(n,a+b))/(a+b)
    else if (mod(n,a+b)>a) then
     write(*,'(i0)') b*(n-mod(n,a+b))/(a+b)+mod(n,a+b)-a
    end if
   end if
  stop
end program main


program main
 implicit none
 integer(8) N, A, B
 integer :: ao=0
 integer(8) i,ii

 read *, N, A, B

do i=1,1000000000
 if (N>(A+B)) then
  N=N-(A+B)
  ao=ao+A
 end if

 if (N<=(A+B)) then
 if (A==0) goto 100
  do ii=1,A
   if ((N-ii)>=0) ao=ao+1
   if ((N-ii)==0) goto 100
  end do
  goto 100
 end if
end do

100 continue
 print '(i0)', ao

endprogram main


program abc
	implicit none
	integer :: n,a,b,c,d,e,x

	read(*,*) n,a,b

	c = a + b

    d = mod(n,c)

    e = (n - d) / c

    if(d < a) then
    	x = a * e + d
    else
    	x = a * e + a
    endif

	write(*,*) x

end program abc


program abc
	implicit none
	integer :: a,b,n,na,nb,x
    double precision :: an,bn

	read(*,*) a,b

    x = -1

    do n = 1, 1000
    	an = n * 0.08
        na = int(an)
        if(na /= a) goto 100
        bn = n * 0.1
        nb = int(bn)
        if(nb /= b) goto 100
        x = n
        goto 200
100		continue
	enddo

200	continue

	write(*,*) x

end program abc

Score : 300 points

Problem Statement

Find the price of a product before tax such that, when the consumption tax rate is 8 percent and 10 percent, the amount of consumption tax levied on it is A yen and B yen, respectively. (Yen is the currency of Japan.)

Here, the price before tax must be a positive integer, and the amount of consumption tax is rounded down to the nearest integer.

If multiple prices satisfy the condition, print the lowest such price; if no price satisfies the condition, print -1.

Constraints

1 \leq A \leq B \leq 100

A and B are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

If there is a price that satisfies the condition, print an integer representing the lowest such price; otherwise, print -1.

Sample Input 1

2 2

Sample Output 1

25

If the price of a product before tax is 25 yen, the amount of consumption tax levied on it is:

When the consumption tax rate is 8 percent: \lfloor 25 \times 0.08 \rfloor = \lfloor 2 \rfloor = 2 yen.

When the consumption tax rate is 10 percent: \lfloor 25 \times 0.1 \rfloor = \lfloor 2.5 \rfloor = 2 yen.

Thus, the price of 25 yen satisfies the condition. There are other possible prices, such as 26 yen, but print the minimum such price, 25.

Sample Input 2

8 10

Sample Output 2

100

If the price of a product before tax is 100 yen, the amount of consumption tax levied on it is:

When the consumption tax rate is 8 percent: \lfloor 100 \times 0.08 \rfloor = 8 yen.

When the consumption tax rate is 10 percent: \lfloor 100 \times 0.1 \rfloor = 10 yen.

Sample Input 3

19 99

Sample Output 3

-1

There is no price before tax satisfying this condition, so print -1.


program main
  implicit none
  integer(8) :: n, m, i, dig, res
  integer(8), allocatable :: d(:), cnt(:)
  character(200000) :: s
  read *, n, m
  read *, s
  allocate (d(n), cnt(0:m - 1))

  if (m == 2 .or. m == 5) then
    res = 0
    do i = 1, n
      read (s(i:i), *) dig
      if (modulo(dig, m) == 0) res = res + i
    end do
  else
    read (s(n:n), *) dig
    cnt = 0
    cnt(0) = 1
    d(n) = modulo(dig, m)
    cnt(d(n)) = cnt(d(n)) + 1
    do i = n - 1, 1, -1
      read (s(i:i), *) dig
      d(i) = d(i + 1) + pow(10_8, n - i) * dig
      d(i) = modulo(d(i), m)
      cnt(d(i)) = cnt(d(i)) + 1
    end do

    res = 0
    do i = 0, m - 1
      res = res + cnt(i) * (cnt(i) - 1) / 2
    end do
  end if
  print "(i0)", res
  contains

  recursive function pow(n, p) result(res)
    integer(8), intent(in) :: n, p
    integer(8) :: res
    if (p == 0) then
      res = 1
    else if (modulo(p, 2_8) == 0) then
      res = modulo(pow(n, p / 2) ** 2, m)
    else
      res = modulo(pow(n, p - 1) * n, m)
    end if
  end function pow
end program main

Score : 500 points

Problem Statement

Takahashi has a string S of length N consisting of digits from 0 through 9.

He loves the prime number P. He wants to know how many non-empty (contiguous) substrings of S - there are N \times (N + 1) / 2 of them - are divisible by P when regarded as integers written in base ten.

Here substrings starting with a 0 also count, and substrings originated from different positions in S are distinguished, even if they are equal as strings or integers.

Compute this count to help Takahashi.

Constraints

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

S consists of digits.

|S| = N

2 \leq P \leq 10000

P is a prime number.

Input

Input is given from Standard Input in the following format:

N P
S

Output

Print the number of non-empty (contiguous) substrings of S that are divisible by P when regarded as an integer written in base ten.

Sample Input 1

4 3
3543

Sample Output 1

6

Here S = 3543. There are ten non-empty (contiguous) substrings of S:

3: divisible by 3.

35: not divisible by 3.

354: divisible by 3.

3543: divisible by 3.

5: not divisible by 3.

54: divisible by 3.

543: divisible by 3.

4: not divisible by 3.

43: not divisible by 3.

3: divisible by 3.

Six of these are divisible by 3, so print 6.

Sample Input 2

4 2
2020

Sample Output 2

10

Here S = 2020. There are ten non-empty (contiguous) substrings of S, all of which are divisible by 2, so print 10.

Note that substrings beginning with a 0 also count.

Sample Input 3

20 11
33883322005544116655

Sample Output 3

68


program prob33
    implicit none
    integer :: n

    read(*,*) n

    write(*,*) (n+1)/2
    stop
contains
end program prob33

Score : 100 points

Problem Statement

Takahashi wants to print a document with N pages double-sided, where two pages of data can be printed on one sheet of paper.

At least how many sheets of paper does he need?

Constraints

N is an integer.

1 \leq N \leq 100

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

5

Sample Output 1

3

By printing the 1-st, 2-nd pages on the 1-st sheet, 3-rd and 4-th pages on the 2-nd sheet, and 5-th page on the 3-rd sheet, we can print all the data on 3 sheets of paper.

Sample Input 2

2

Sample Output 2

1

Sample Input 3

100

Sample Output 3

50


program example
	implicit none

	integer N

    read(*,*) N

    if(mod(N,2)==0) then
    	write(*,*) N/2
    else
    	write(*,*) (N/2)+1
    end if

end program example


program Bingo
    implicit none
    integer(4):: bng(3,3), num
    integer(4):: i,j,k, n
    logical:: is_open(3,3) = .false.

    do i=1, 3
        read*, bng(:,i)
    end do

    read*, n

    do i=1, n
        read*, num

        do j=1,3
            do k=1,3
                if (bng(k,j) == num) is_open(k,j) = .true.
            end do
        end do
    end do

    if (check_bingo(is_open)) then
        print*, 'Yes'
    else
        print*, 'No'
    end if

    contains
    function check_bingo(is_open) result(is_bingo)
        integer(4):: i,j
        logical:: is_open(3,3), is_bingo

        is_bingo = .false.

        do i=1, 3
            if (is_open(1,i) .and. is_open(2,i) .and. is_open(3,i)) is_bingo = .true.
            if (is_open(i,1) .and. is_open(i,2) .and. is_open(i,3)) is_bingo = .true.
        end do

        if (is_open(1,1) .and. is_open(2,2) .and. is_open(3,3)) is_bingo = .true.
        if (is_open(1,3) .and. is_open(2,2) .and. is_open(3,1)) is_bingo = .true.

    end function
end program Bingo

Score : 200 points

Problem Statement

We have a bingo card with a 3\times3 grid. The square at the i-th row from the top and the j-th column from the left contains the number A_{i, j}.

The MC will choose N numbers, b_1, b_2, \cdots, b_N. If our bingo sheet contains some of those numbers, we will mark them on our sheet.

Determine whether we will have a bingo when the N numbers are chosen, that is, the sheet will contain three marked numbers in a row, column, or diagonal.

Constraints

All values in input are integers.

1 \leq A_{i, j} \leq 100

A_{i_1, j_1} \neq A_{i_2, j_2} ((i_1, j_1) \neq (i_2, j_2))

1 \leq N \leq 10

1 \leq b_i \leq 100

b_i \neq b_j (i \neq j)

Input

Input is given from Standard Input in the following format:

A_{1, 1} A_{1, 2} A_{1, 3}
A_{2, 1} A_{2, 2} A_{2, 3}
A_{3, 1} A_{3, 2} A_{3, 3}
N
b_1
\vdots
b_N

Output

If we will have a bingo, print Yes; otherwise, print No.

Sample Input 1

84 97 66
79 89 11
61 59 7
7
89
7
87
79
24
84
30

Sample Output 1

Yes

We will mark A_{1, 1}, A_{2, 1}, A_{2, 2}, A_{3, 3}, and complete the diagonal from the top-left to the bottom-right.

Sample Input 2

41 7 46
26 89 2
78 92 8
5
6
45
16
57
17

Sample Output 2

No

We will mark nothing.

Sample Input 3

60 88 34
92 41 43
65 73 48
10
60
43
88
11
48
73
65
41
92
34

Sample Output 3

Yes

We will mark all the squares.


program Bingo
    implicit none
    integer(4):: bng(3,3), num
    integer(4):: i,j,k, n
    logical:: is_open(3,3) = .false.

    do i=1, 3
        read*, bng(:,i)
    end do

    read*, n

    do i=1, n
        read*, num

        do j=1,3
            do k=1,3
                if (bng(k,j) == num) is_open(k,j) = .true.
            end do
        end do

        if (check_bingo(is_open)) then
            print*, 'Yes'
            stop
        end if
    end do

    print*, 'No'

    contains
    function check_bingo(is_open) result(is_bingo)
        integer(4):: i,j
        logical:: is_open(3,3), is_bingo

        is_bingo = .false.

        do i=1, 3
            if (is_open(1,i) .and. is_open(2,i) .and. is_open(3,i)) is_bingo = .true.
            if (is_open(i,1) .and. is_open(i,2) .and. is_open(i,3)) is_bingo = .true.
        end do

        if (is_open(1,1) .and. is_open(2,2) .and. is_open(3,3)) is_bingo = .true.

    end function
end program Bingo


program main
  implicit none
  type arr_list
     integer :: N
     integer,allocatable :: point(:)
  end type arr_list
  type(arr_list),allocatable :: data(:)
  integer :: num(97:122)
  integer :: N, Q
  character(len=500000) :: S
  character(:),allocatable :: tmp
  character(len=1) :: c,b,c2
  integer,allocatable :: tmp2(:)
  integer :: a
  integer :: i,j, l,r
  integer :: out
  read(*,*) N
  read(*,*) S
  read(*,*) Q
  allocate(data(97:122))
  allocate(tmp2(0:N-1))
  data(:)%N = 0
  num(:) = 0
  do i = 1,N
     data(ichar(S(i:i)))%N = data(ichar(S(i:i)))%N+1
  end do
  do i = 97,122
     if (data(i)%N > 0) allocate(data(i)%point(0:data(i)%N-1))
  end do
  do i = 1,N
     data(ichar(S(i:i)))%point( num(ichar(S(i:i))) ) = i-1
     num(ichar(S(i:i))) = num(ichar(S(i:i))) + 1
  end do
!  do i = 97,97+4
!     write(*,*) data(i)%point
!  end do
  do i = 1, Q
     read(*,'(i1,1x,a,1x,a)') a,b,c
     if (a == 1) then
        backspace(5)
        !read(*,'(i1,1x,g0,1x,a)') a,j,c
        read(*,*) a,j,c
        c2 = S(j:j)
        call insert_delete(data,j-1,c,c2,tmp2,n)
        S(j:j) = c
     else
        backspace(5)
!        read(*,'(i1,1x,g0,1x,g0)') a,l,r
        read(*,*) a,l,r
        call count_char(data,l-1,r-1,out)
        write(*,'(i0)') out
        !deallocate(tmp)
     end if

  end do
contains
  subroutine insert_delete(data,key,c,c2,tmp,n2)
    implicit none
    type(arr_list),intent(inout) :: data(97:122)
    integer,intent(in) :: key ! key
    character,intent(in) :: c,c2 ! char
    integer,intent(in) :: n2
    integer :: tmp(0:n2-1)
    integer :: i,i2
    integer :: j
    integer :: n

    i = ichar(c)
    i2 = ichar(c2)
    if ( i /= i2) then ! insert and delete
       if ( data(i)%N > 0) then
          j = found_point(data(i)%point(:),data(i)%n, key)
          if ( j == data(i)%N) then ! out bound
             n = data(i)%n
             tmp(0:n-1) = data(i)%point(0:n-1)
             deallocate( data(i)%point )
             data(i)%n = data(i)%n + 1
             allocate( data(i)%point(0:data(i)%n-1) )
             data(i)%point(0:n-1) = tmp(0:n-1)
             data(i)%point(n) = key
             !
             ! delete
             !
             n = data(i2)%n
             data(i2)%n = data(i2)%n - 1
             if ( data(i2)%n > 0) then
                j = found_point(data(i2)%point(:),n, key)
                tmp(0:n-1) = data(i2)%point(0:n-1)
                deallocate( data(i2)%point )
                allocate( data(i2)%point(0:data(i2)%n-1) )
                if ( j == 0 ) then
                   data(i2)%point(j:) = tmp(j+1:n-1)
                else if (j == N-1 ) then
                   data(i2)%point(0:j-1) = tmp(0:j-1)
                else
                   data(i2)%point(0:j-1) = tmp(0:j-1)
                   data(i2)%point(j:) = tmp(j+1:n-1)
                end if
             else
                deallocate( data(i2)%point )
             end if
             return
          else
             n = data(i)%n
             tmp(0:n-1) = data(i)%point(0:n-1)
             deallocate( data(i)%point )
             data(i)%n = data(i)%n + 1
             allocate( data(i)%point(0:data(i)%n-1) )
             if ( j == 0 ) then
                data(i)%point(0:0) = key
                data(i)%point(j+1:) = tmp(0:n-1)
             else
                data(i)%point(0:j-1) = tmp(0:j-1)
                data(i)%point(j) = key
                data(i)%point(j+1:) = tmp(j:n-1)
             end if
             !
             ! delete
             !
             n = data(i2)%n
             data(i2)%n = data(i2)%n - 1
             if ( data(i2)%n > 0) then
                j = found_point(data(i2)%point(:),n, key)
                !allocate(tmp(0:n-1))
                tmp(0:n-1) = data(i2)%point(0:n-1)
                deallocate( data(i2)%point )
                allocate( data(i2)%point(0:data(i2)%n-1) )
                if ( j == 0 ) then
                   data(i2)%point(j:) = tmp(j+1:n-1)
                else if (j == N-1 ) then
                   data(i2)%point(0:j-1) = tmp(0:j-1)
                else
                   data(i2)%point(0:j-1) = tmp(0:j-1)
                   data(i2)%point(j:) = tmp(j+1:n-1)
                end if
                !deallocate(tmp)
             else
                deallocate( data(i2)%point )
             end if
          end if
       else ! N == 0
          data(i)%N = data(i)%N + 1
          allocate(data(i)%point(0:data(i)%N-1))
          data(i)%point(0) = key
          !
          ! delete
          !
          n = data(i2)%n
          data(i2)%n = data(i2)%n - 1
          if ( data(i2)%n > 0) then
             j = found_point(data(i2)%point(:),n, key)
             tmp(0:n-1) = data(i2)%point(0:n-1)
             deallocate( data(i2)%point )
             allocate( data(i2)%point(0:data(i2)%n-1) )
             if ( j == 0 ) then
                data(i2)%point(j:) = tmp(j+1:n-1)
             else if (j == N-1 ) then
                data(i2)%point(0:j-1) = tmp(0:j-1)
             else
                data(i2)%point(0:j-1) = tmp(0:j-1)
                data(i2)%point(j:) = tmp(j+1:n-1)
             end if
          else
             deallocate( data(i2)%point )
          end if
       end if
    end if
    return
  end subroutine insert_delete

  function found_point(a,n,key) result(ok)
    integer,intent(in) :: n
    integer,intent(in) :: a(0:n-1)
    integer,intent(in) :: key
    integer :: ng
    integer :: ok
    integer :: mid
    integer :: test
    ng = -1
    ok = n
    !do while( )
    test = 0
    do
       mid = (ok + ng)/2
       if (isOK(mid,key,a,n)) then
          ok = mid
       else
          ng = mid
       end if
       if (abs(ok-ng) <= 1) exit
       test = test + 1
       if ( test > log(real(n)) + 1 ) write(*,*) "over?"
    end do
  end function found_point

  function isOK (index, key,a,n) result(out)
    integer,intent(in):: n
    integer,intent(in) :: a(0:n-1)
    integer,intent(in) :: index, key
    logical :: out
    if ( a(index) >= key) then
       out = .true.
    else
       out = .false.
    end if
  end function isOK

  subroutine count_char(data,l,r,out)
    type(arr_list) :: data(97:122)
    integer :: l,r
    integer :: out
    integer :: N
    integer :: i, j, tmp
    out = 0
    do i = 97,122
       if ( data(i)%n > 0) then
          j = found_point(data(i)%point(:),data(i)%n,l)
          if ( j < data(i)%N) then
             if ( l<=data(i)%point(j) .and. data(i)%point(j) <= r) then
                out = out + 1
             end if
          end if
       end if
    end do
  end subroutine count_char

  subroutine count_char2(S,out)
    character(*) :: S
    integer :: out
    integer :: N
    integer :: i, tmp
    integer :: c(97:122)
    N = len_trim(S)
    c(97:122) = 0
    do i = 1,N
       tmp = ichar(S(i:i))
       c (tmp) = c(tmp) + 1
    end do
    out = count(c /=0)
  end subroutine count_char2
end program main

Score : 500 points

Problem Statement

You are given a string S of length N consisting of lowercase English letters.

Process Q queries of the following two types:

Type 1: change the i_q-th character of S to c_q. (Do nothing if the i_q-th character is already c_q.)

Type 2: answer the number of different characters occurring in the substring of S between the l_q-th and r_q-th characters (inclusive).

Constraints

N, Q, i_q, l_q, and r_q are integers.

S is a string consisting of lowercase English letters.

c_q is a lowercase English letter.

1 \leq N \leq 500000

1 \leq Q \leq 20000

|S| = N

1 \leq i_q \leq N

1 \leq l_q \leq r_q \leq N

There is at least one query of type 2 in each testcase.

Input

Input is given from Standard Input in the following format:

N
S
Q
Query_1
\vdots
Query_Q

Here, Query_i in the 4-th through (Q+3)-th lines is one of the following:

1 i_q c_q

2 l_q r_q

Output

For each query of type 2, print a line containing the answer.

Sample Input 1

7
abcdbbd
6
2 3 6
1 5 z
2 1 1
1 4 a
1 7 d
2 1 7

Sample Output 1

3
1
5

In the first query, cdbb contains three kinds of letters: b , c , and d, so we print 3.

In the second query, S is modified to abcdzbd.

In the third query, a contains one kind of letter: a, so we print 1.

In the fourth query, S is modified to abcazbd.

In the fifth query, S does not change and is still abcazbd.

In the sixth query, abcazbd contains five kinds of letters: a, b, c, d, and z, so we print 5.


program main
  implicit none
  type arr_list
     integer :: N
     integer,allocatable :: point(:)
  end type arr_list
  type(arr_list),allocatable :: data(:)
  integer :: num(97:122)
  integer :: N, Q
  character(len=500000) :: S
  character(:),allocatable :: tmp
  character(len=1) :: c,b,c2
  integer :: a
  integer :: i,j, l,r
  integer :: out
  read(*,*) N
  read(*,*) S
  read(*,*) Q
  allocate(data(97:122))
  data(:)%N = 0
  num(:) = 0
  do i = 1,N
     data(ichar(S(i:i)))%N = data(ichar(S(i:i)))%N+1
  end do
  do i = 97,122
     if (data(i)%N > 0) allocate(data(i)%point(0:data(i)%N-1))
  end do
  do i = 1,N
     data(ichar(S(i:i)))%point( num(ichar(S(i:i))) ) = i-1
     num(ichar(S(i:i))) = num(ichar(S(i:i))) + 1
  end do
!  do i = 97,97+4
!     write(*,*) data(i)%point
!  end do
  do i = 1, Q
     read(*,'(i1,1x,a,1x,a)') a,b,c
     if (a == 1) then
        backspace(5)
        !read(*,'(i1,1x,g0,1x,a)') a,j,c
        read(*,*) a,j,c
        if (Q> 19999) then
           c2 = S(j:j)
           call insert_delete(data,j-1,c,c2)
        end if
        S(j:j) = c
     else
        backspace(5)
!        read(*,'(i1,1x,g0,1x,g0)') a,l,r
        read(*,*) a,l,r
        !tmp = S(l:r)
        !call count_char(S(l:r),out)
        if (Q> 19999) then
           call count_char(data,l-1,r-1,out)
        else
           tmp = S(l:r)
           call count_char2(tmp,out)
           deallocate(tmp)
        end if
        write(*,'(i0)') out
        !deallocate(tmp)
     end if

  end do
contains
  subroutine insert_delete(data,key,c,c2)
    implicit none
    type(arr_list) :: data(97:122)
    integer :: key ! key
    character :: c,c2 ! char
    integer,allocatable :: tmp(:)
    integer :: i,i2
    integer :: j
    integer :: n

    i = ichar(c)
    i2 = ichar(c2)
    if ( i /= i2) then ! insert and delete
       if (data(i)%N > 0) then
          j = found_point(data(i)%point(:),data(i)%n, key)
       else
          data(i)%N=data(i)%N+1
          allocate(data(i)%point(0:data(i)%N-1))
          data(i)%point(0:0) = key
          !
          ! delete
          !
          n = data(i2)%n
          data(i2)%n = data(i2)%n - 1
          if ( data(i2)%n > 0) then
             j = found_point(data(i2)%point(:),n, key)
             allocate(tmp(0:n-1))
             tmp(0:n-1) = data(i2)%point(0:n-1)
             deallocate( data(i2)%point )
             allocate( data(i2)%point(0:data(i2)%n-1) )
             data(i2)%point(0:j-1) = tmp(0:j-1)
             data(i2)%point(j:) = tmp(j+1:n-1)
             deallocate(tmp)
          else
             deallocate( data(i2)%point )
          end if
          return
       end if
       if ( j == data(i)%N) then ! out bound
          n = data(i)%n
          allocate(tmp(0:n-1))
          tmp(0:n-1) = data(i)%point(0:n-1)
          deallocate( data(i)%point )
          data(i)%n = data(i)%n + 1
          allocate( data(i)%point(0:data(i)%n-1) )
          data(i)%point(0:n-1) = tmp(0:n-1)
          data(i)%point(n) = key
          deallocate(tmp)
          !
          ! delete
          !
          n = data(i2)%n
          data(i2)%n = data(i2)%n - 1
          if ( data(i2)%n > 0) then
             j = found_point(data(i2)%point(:),n, key)
             allocate(tmp(0:n-1))
             tmp(0:n-1) = data(i2)%point(0:n-1)
             deallocate( data(i2)%point )
             allocate( data(i2)%point(0:data(i2)%n-1) )
             data(i2)%point(0:j-1) = tmp(0:j-1)
             data(i2)%point(j:) = tmp(j+1:n-1)
             deallocate(tmp)
          else
             deallocate( data(i2)%point )
          end if
          return
       else
          n = data(i)%n
          allocate(tmp(0:n-1))
          tmp(0:n-1) = data(i)%point(0:n-1)
          deallocate( data(i)%point )
          data(i)%n = data(i)%n + 1
          allocate( data(i)%point(0:data(i)%n-1) )
          if ( j == 0 ) then
             data(i)%point(0:0) = key
             data(i)%point(j+1:) = tmp(0:n-1)
          else
             data(i)%point(0:j-1) = tmp(0:j-1)
             data(i)%point(j) = key
             data(i)%point(j+1:) = tmp(j:n-1)
          end if
          deallocate(tmp)
          !
          ! delete
          !
          n = data(i2)%n
          data(i2)%n = data(i2)%n - 1
          if ( data(i2)%n > 0) then
             j = found_point(data(i2)%point(:),n, key)
             allocate(tmp(0:n-1))
             tmp(0:n-1) = data(i2)%point(0:n-1)
             deallocate( data(i2)%point )
             allocate( data(i2)%point(0:data(i2)%n-1) )
             data(i2)%point(0:j-1) = tmp(0:j-1)
             data(i2)%point(j:) = tmp(j+1:n-1)
             deallocate(tmp)
          else
             deallocate( data(i2)%point )
          end if
       end if
    end if
    return
  end subroutine insert_delete

  function found_point(a,n,key) result(ok)
    integer :: n
    integer :: a(0:n-1)
    integer :: key
    integer :: ng
    integer :: ok
    integer :: mid
    ng = -1
    ok = n
    do while( abs(ok-ng) > 1)
       mid = (ok + ng)/2
       if (isOK(mid,key,a,n)) then
          ok = mid
       else
          ng = mid
       end if
    end do
  end function found_point

  function isOK (index, key,a,n) result(out)
    integer :: n
    integer :: a(0:n-1)
    integer :: index, key
    logical :: out
    if ( a(index) >= key) then
       out = .true.
    else
       out = .false.
    end if
  end function isOK
  subroutine count_char(data,l,r,out)
    type(arr_list) :: data(97:122)
    integer :: l,r
    integer :: out
    integer :: N
    integer :: i, j, tmp
    out = 0
    do i = 97,122
       if ( data(i)%n > 0) then
          j = found_point(data(i)%point(:),data(i)%n,l)
          if ( j < data(i)%N) then
             if ( l<=data(i)%point(j) .and. data(i)%point(j) <= r) then
                out = out + 1
             end if
!!             if ( j+1 < data(i)%N) then
!!                if ( l<=data(i)%point(j) .and. data(i)%point(j) <= r) then
!!                   out = out + 1
!!                   cycle
!!                end if
!!                if ( data(i)%point(j) < l .and. data(i)%point(j+1) <= r) then
!!                   out = out + 1
!!                end if
!!             else
!!             end if
          end if
       end if
    end do
  end subroutine count_char

  subroutine count_char2(S,out)
    character(*) :: S
    integer :: out
    integer :: N
    integer :: i, tmp
    integer :: c(97:122)
    N = len_trim(S)
    c(97:122) = 0
    do i = 1,N
       tmp = ichar(S(i:i))
       c (tmp) = c(tmp) + 1
    end do
    out = count(c /=0)
  end subroutine count_char2
end program main


program main
  implicit none
  type arr_list
     integer :: N
     integer,allocatable :: point(:)
  end type arr_list
  type(arr_list),allocatable :: data(:)
  integer :: num(97:122)
  integer :: N, Q
  character(len=500000) :: S
  character(:),allocatable :: tmp
  character(len=1) :: c,b,c2
  integer :: a
  integer :: i,j, l,r
  integer :: out
  read(*,*) N
  read(*,*) S
  read(*,*) Q
  allocate(data(97:122))
  data(:)%N = 0
  num(:) = 0
  do i = 1,N
     data(ichar(S(i:i)))%N = data(ichar(S(i:i)))%N+1
  end do
  do i = 97,122
     if (data(i)%N > 0) allocate(data(i)%point(0:data(i)%N-1))
  end do
  do i = 1,N
     data(ichar(S(i:i)))%point( num(ichar(S(i:i))) ) = i-1
     num(ichar(S(i:i))) = num(ichar(S(i:i))) + 1
  end do
!  do i = 97,97+4
!     write(*,*) data(i)%point
!  end do
  do i = 1, Q
     read(*,'(i1,1x,a,1x,a)') a,b,c

     if (a == 1) then
        backspace(5)
        !read(*,'(i1,1x,g0,1x,a)') a,j,c
        read(*,*) a,j,c
        if (Q> 1000) then
           c2 = S(j:j)
           call insert_delete(data,j-1,c,c2)
        end if
        S(j:j) = c
     else
        backspace(5)
!        read(*,'(i1,1x,g0,1x,g0)') a,l,r
        read(*,*) a,l,r
        !tmp = S(l:r)
        !call count_char(S(l:r),out)
        if (Q> 10000) then
           call count_char(data,l-1,r-1,out)
        else
           call count_char2(S,out)
        end if
        write(*,'(i0)') out
        !deallocate(tmp)
     end if

  end do
contains
  subroutine insert_delete(data,key,c,c2)
    implicit none
    type(arr_list) :: data(97:122)
    integer :: key ! key
    character :: c,c2 ! char
    integer,allocatable :: tmp(:)
    integer :: i,i2
    integer :: j
    integer :: n

    i = ichar(c)
    i2 = ichar(c2)
    if ( i /= i2) then ! insert and delete
       if (data(i)%N > 0) then
          j = found_point(data(i)%point(:),data(i)%n, key)
       else
          data(i)%N=data(i)%N+1
          allocate(data(i)%point(0:data(i)%N-1))
          data(i)%point(0:0) = key
          !
          ! delete
          !
          n = data(i2)%n
          data(i2)%n = data(i2)%n - 1
          if ( data(i2)%n > 0) then
             j = found_point(data(i2)%point(:),n, key)
             allocate(tmp(0:n-1))
             tmp(0:n-1) = data(i2)%point(0:n-1)
             deallocate( data(i2)%point )
             allocate( data(i2)%point(0:data(i2)%n-1) )
             data(i2)%point(0:j-1) = tmp(0:j-1)
             data(i2)%point(j:) = tmp(j+1:n-1)
             deallocate(tmp)
          else
             deallocate( data(i2)%point )
          end if
          return
       end if
       if ( j == data(i)%N) then ! out bound
          n = data(i)%n
          allocate(tmp(0:n-1))
          tmp(0:n-1) = data(i)%point(0:n-1)
          deallocate( data(i)%point )
          data(i)%n = data(i)%n + 1
          allocate( data(i)%point(0:data(i)%n-1) )
          data(i)%point(0:n-1) = tmp(0:n-1)
          data(i)%point(n) = key
          deallocate(tmp)
          !
          ! delete
          !
          n = data(i2)%n
          data(i2)%n = data(i2)%n - 1
          if ( data(i2)%n > 0) then
             j = found_point(data(i2)%point(:),n, key)
             allocate(tmp(0:n-1))
             tmp(0:n-1) = data(i2)%point(0:n-1)
             deallocate( data(i2)%point )
             allocate( data(i2)%point(0:data(i2)%n-1) )
             data(i2)%point(0:j-1) = tmp(0:j-1)
             data(i2)%point(j:) = tmp(j+1:n-1)
             deallocate(tmp)
          else
             deallocate( data(i2)%point )
          end if
          return
       else
          n = data(i)%n
          allocate(tmp(0:n-1))
          tmp(0:n-1) = data(i)%point(0:n-1)
          deallocate( data(i)%point )
          data(i)%n = data(i)%n + 1
          allocate( data(i)%point(0:data(i)%n-1) )
          if ( j == 0 ) then
             data(i)%point(0:0) = key
             data(i)%point(j+1:) = tmp(0:n-1)
          else
             data(i)%point(0:j-1) = tmp(0:j-1)
             data(i)%point(j) = key
             data(i)%point(j+1:) = tmp(j:n-1)
          end if
          deallocate(tmp)
          !
          ! delete
          !
          n = data(i2)%n
          data(i2)%n = data(i2)%n - 1
          if ( data(i2)%n > 0) then
             j = found_point(data(i2)%point(:),n, key)
             allocate(tmp(0:n-1))
             tmp(0:n-1) = data(i2)%point(0:n-1)
             deallocate( data(i2)%point )
             allocate( data(i2)%point(0:data(i2)%n-1) )
             data(i2)%point(0:j-1) = tmp(0:j-1)
             data(i2)%point(j:) = tmp(j+1:n-1)
             deallocate(tmp)
          else
             deallocate( data(i2)%point )
          end if
       end if
    end if
    return
  end subroutine insert_delete

  function found_point(a,n,key) result(ok)
    integer :: n
    integer :: a(0:n-1)
    integer :: key
    integer :: ng
    integer :: ok
    integer :: mid
    ng = -1
    ok = n
    do while( abs(ok-ng) > 1)
       mid = (ok + ng)/2
       if (isOK(mid,key,a,n)) then
          ok = mid
       else
          ng = mid
       end if
    end do
  end function found_point

  function isOK (index, key,a,n) result(out)
    integer :: n
    integer :: a(0:n-1)
    integer :: index, key
    logical :: out
    if ( a(index) >= key) then
       out = .true.
    else
       out = .false.
    end if
  end function isOK
  subroutine count_char(data,l,r,out)
    type(arr_list) :: data(97:122)
    integer :: l,r
    integer :: out
    integer :: N
    integer :: i, j, tmp
    out = 0
    do i = 97,122
       if ( data(i)%n > 0) then
          j = found_point(data(i)%point(:),data(i)%n,l)
          if ( j < data(i)%N) then
             if ( l<=data(i)%point(j) .and. data(i)%point(j) <= r) then
                out = out + 1
             end if
!!             if ( j+1 < data(i)%N) then
!!                if ( l<=data(i)%point(j) .and. data(i)%point(j) <= r) then
!!                   out = out + 1
!!                   cycle
!!                end if
!!                if ( data(i)%point(j) < l .and. data(i)%point(j+1) <= r) then
!!                   out = out + 1
!!                end if
!!             else
!!             end if
          end if
       end if
    end do
  end subroutine count_char

  subroutine count_char2(S,out)
    character(*) :: S
    integer :: out
    integer :: N
    integer :: i, tmp
    integer :: c(97:122)
    N = len_trim(S)
    c(97:122) = 0

    do i = 97, 122
       c(i) = index(S,char(i))
    end do
    out = count(c /=0)
  end subroutine count_char2
end program main


program main
  implicit none
  integer :: N, Q
  character(len=500000) :: S
  character(:),allocatable :: tmp
  character(len=1) :: c,b
  integer :: a
  integer :: i,j, l,r
  integer :: out
  read(*,*) N
  read(*,*) S
  read(*,*) Q

  do i = 1, Q
     read(*,'(i1,1x,a,1x,a)') a,b,c

     if (a == 1) then
        backspace(5)
        !read(*,'(i1,1x,g0,1x,a)') a,j,c
        read(*,*) a,j,c
        S(j:j) = c
     else
        backspace(5)
!        read(*,'(i1,1x,g0,1x,g0)') a,l,r
        read(*,*) a,l,r
        tmp = S(l:r)
        call count_char(tmp,out)
        write(*,'(i0)') out
        deallocate(tmp)
     end if

  end do
contains
  subroutine count_char(S,out)
    character(*) :: S
    integer :: out
    integer :: N
    integer :: i, tmp
    integer :: c(97:122)
    N = len_trim(S)
    c(97:122) = 0
    do i = 1,N
       tmp = ichar(S(i:i))
       c (tmp) = c(tmp) + 1
    end do
    out = count(c /=0)
  end subroutine count_char
end program main


program yakiniku_optimization_problem
  implicit none
  real(8), parameter :: tol = 1e-7
  integer :: n, k, i, j
  real(8) :: l = 0.d0, r = 1e6, m, xc, yc, s, t, u, v, w, z, ri, rj
  real(8), dimension(60) :: x = 0.d0, y = 0.d0, c = 0.d0
  character(64) :: ans
  read(*,*) n, k
  do i = 1, n
    read(*,*) x(i), y(i), c(i)
    c(i) = 1.d0 / c(i)
  end do
  do while (r - l > tol)
    m = 0.5 * (l + r)
    do i = 1, n
      if (ok(x(i), y(i))) then
        r = m
        goto 100
      end if
      ri = m * c(i)
      do j = i + 1, n
        rj = m * c(j)
        z = d(x(i), x(j), y(i), y(j))
        if (z > ri + rj .or. z < abs(rj - ri)) cycle
        s = 2 * (x(j) - x(i))
        t = 2 * (y(j) - y(i))
        u = x(i) * x(i) - x(j) * x(j) + y(i) * y(i) - y(j) * y(j) + rj * rj - ri * ri
        v = s * s + t * t
        w = abs(s * x(i) + t * y(i) + u)
        z = sqrt(v * ri * ri - w * w)
        xc = (s * w - t * z) / v + x(i)
        yc = (t * w + s * z) / v + y(i)
        if (ok(xc, yc)) then
          r = m
          goto 100
        end if
        xc = (s * w + t * z) / v + x(i)
        yc = (t * w - s * z) / v + y(i)
        if (ok(xc, yc)) then
          r = m
          goto 100
        end if
      end do
    end do
    l = m
  100 end do
  write(ans,'(f32.16)') r
  write(*,'(a)') trim(adjustl(ans))
contains
  real(8) function d(x1, x2, y1, y2) result(res)
    real(8), intent(in) :: x1, x2, y1, y2
    real(8) :: dx, dy
    dx = x2 - x1
    dy = y2 - y1
    res = sqrt(dx * dx + dy * dy)
  end
  logical function ok(xd, yd) result(res)
    real(8), intent(in) :: xd, yd
    integer :: i, cnt
    cnt = 0
    do i = 1, n
      if (d(xd, x(i), yd, y(i)) <= m * c(i)) cnt = cnt + 1
    end do
    res = cnt >= k
  end
end program yakiniku_optimization_problem

Score : 600 points

Problem Statement

Takahashi wants to grill N pieces of meat on a grilling net, which can be seen as a two-dimensional plane. The coordinates of the i-th piece of meat are \left(x_i, y_i\right), and its hardness is c_i.

Takahashi can use one heat source to grill the meat. If he puts the heat source at coordinates \left(X, Y\right), where X and Y are real numbers, the i-th piece of meat will be ready to eat in c_i \times \sqrt{\left(X - x_i\right)^2 + \left(Y-y_i\right)^2} seconds.

Takahashi wants to eat K pieces of meat. Find the time required to have K or more pieces of meat ready if he put the heat source to minimize this time.

Constraints

All values in input are integers.

1 \leq N \leq 60

1 \leq K \leq N

-1000 \leq x_i , y_i \leq 1000

\left(x_i, y_i\right) \neq \left(x_j, y_j\right) \left(i \neq j \right)

1 \leq c_i \leq 100

Input

Input is given from Standard Input in the following format:

N K
x_1 y_1 c_1
\vdots
x_N y_N c_N

Output

Print the answer.

It will be considered correct if its absolute or relative error from our answer is at most 10^{-6}.

Sample Input 1

4 3
-1 0 3
0 0 3
1 0 2
1 1 40

Sample Output 1

2.4

If we put the heat source at \left(-0.2, 0\right), the 1-st, 2-nd, and 3-rd pieces of meat will be ready to eat within 2.4 seconds. This is the optimal place to put the heat source.

Sample Input 2

10 5
-879 981 26
890 -406 81
512 859 97
362 -955 25
128 553 17
-885 763 2
449 310 57
-656 -204 11
-270 76 40
184 170 16

Sample Output 2

7411.2252


program main
 implicit none
 integer N, R

 read *, N, R

 if ( N>= 10 ) then
  print '(i0)', R
 else
  print '(i0)', R+100*(10-N)
 end if

endprogram main

Score : 100 points

Problem Statement

Takahashi is a member of a programming competition site, ButCoder.

Each member of ButCoder is assigned two values: Inner Rating and Displayed Rating.

The Displayed Rating of a member is equal to their Inner Rating if the member has participated in 10 or more contests. Otherwise, the Displayed Rating will be their Inner Rating minus 100 \times (10 - K) when the member has participated in K contests.

Takahashi has participated in N contests, and his Displayed Rating is R. Find his Inner Rating.

Constraints

All values in input are integers.

1 \leq N \leq 100

0 \leq R \leq 4111

Input

Input is given from Standard Input in the following format:

N R

Output

Print his Inner Rating.

Sample Input 1

2 2919

Sample Output 1

3719

Takahashi has participated in 2 contests, which is less than 10, so his Displayed Rating is his Inner Rating minus 100 \times (10 - 2) = 800.

Thus, Takahashi's Inner Rating is 2919 + 800 = 3719.

Sample Input 2

22 3051

Sample Output 2

3051


program ABC156b
    implicit none
    integer::N,K
    integer::cnt
    read*,N,K
    cnt=0
    do while(N/=0)
        N=N/K
        cnt=cnt+1
    end do
    print"(i0)",cnt
end program ABC156b

Score : 200 points

Problem Statement

Given is an integer N. Find the number of digits that N has in base K.

Notes

For information on base-K representation, see Positional notation - Wikipedia.

Constraints

All values in input are integers.

1 \leq N \leq 10^9

2 \leq K \leq 10

Input

Input is given from Standard Input in the following format:

N K

Output

Print the number of digits that N has in base K.

Sample Input 1

11 2

Sample Output 1

4

In binary, 11 is represented as 1011.

Sample Input 2

1010101 10

Sample Output 2

7

Sample Input 3

314159265 3

Sample Output 3

18


   program abc156c
      implicit none
      integer::N,i,p
      integer(8)::sumX,sumA=100000
      integer,allocatable,dimension(:)::X

      read *,N
      allocate(X(N))
      read *,X(1:N)

      do p=1,100
         sumX=0
         do i=1,N
            sumX=sumX+(X(i)-p)**2
         end do
         if (sumX<sumA) sumA=sumX
      end do
      print *,sumA
      stop

   end program abc156c

Score : 300 points

Problem Statement

There are N people living on a number line.

The i-th person lives at coordinate X_i.

You are going to hold a meeting that all N people have to attend.

The meeting can be held at any integer coordinate. If you choose to hold the meeting at coordinate P, the i-th person will spend (X_i - P)^2 points of stamina to attend the meeting.

Find the minimum total points of stamina the N people have to spend.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq X_i \leq 100

Input

Input is given from Standard Input in the following format:

N
X_1 X_2 ... X_N

Output

Print the minimum total stamina the N people have to spend.

Sample Input 1

2
1 4

Sample Output 1

5

Assume the meeting is held at coordinate 2. In this case, the first person will spend (1 - 2)^2 points of stamina, and the second person will spend (4 - 2)^2 = 4 points of stamina, for a total of 5 points of stamina. This is the minimum total stamina that the 2 people have to spend.

Note that you can hold the meeting only at an integer coordinate.

Sample Input 2

7
14 14 2 13 56 2 37

Sample Output 2

2354


program ABC156C
    implicit none
    integer(8)::N,i,j,hp_min,hp
    integer(8),allocatable,dimension(:)::X
    read(5,*)N
    allocate(X(N))
    read(5,*)(X(i),i=1,N)

    hp_min=1000000000
    do i=minval(X),maxval(X)
        hp=0
        do j=1,N
            hp=hp+(X(j)-i)**2
        end do
        hp_min=min(hp_min,hp)
    end do

    print'(i0)',hp_min
end program ABC156C


      MODULE combinationMod
!ref
!https://www.hamayanhamayan.com/entry/2018/06/06/210256

!!!!!  procedure list  !!!!!
!combination( n,k )
!  O(N*logModulus) ==> O(1)
!combinationN( n,k )
!  O(N*logModulus) ==> O( k )
!  for larger N
!initialize( setSize!optional )
!inv( x )
!pow( x,n )
!modSum( a,b )   => .plus.
!modSub( a,b )   => .minus.
!modMulti( a,b ) => .times.
      implicit none
      public
      integer(16),parameter :: modulus = 10**9 + 7
        !sz : max limit of NandK(arg of comb),default is 10**6
      private :: sz, factorial, reciproF
      integer(16) :: sz
      integer(16),allocatable :: factorial(:),reciproF(:)

      interface operator (.plus.)
        module procedure :: modSum
      end interface
      interface operator (.minus.)
        module procedure :: modSub
      end interface
      interface operator (.times.)
        module procedure :: modMulti
      end interface

      contains
      pure function combination( n,k ) result( ans )
        implicit none
        integer(16),intent(in) :: n,k
        integer(16) :: ans, a, b, c, bc

        a = factorial(n)
        b = reciproF(n-k)
        c = reciproF(k)

        bc = b .times. c

        ans = a .times. bc
      end function combination

      pure function combinationN( n,k ) result( ans )
        implicit none
        integer(16),intent(in) :: n,k
        integer(16) :: ans,denominator,numerator !(nu/de)
        integer(16) :: i,nn

        nn = n
        numerator = 1
        do i = 1,k
          numerator = numerator .times. nn
          nn = nn - 1
        end do
        denominator = reciproF(k)
        ans = numerator .times. denominator
      end function combinationN

      subroutine initialize( setSize )
        implicit none
        integer(16),intent(in),optional :: setSize
        integer(16) :: i

        sz = 10**6
        if(present(setSize)) sz = setSize
        allocate( factorial(0:sz),reciproF(0:sz) )

        !calc factrial
        factorial(0) = 1
        do i = 1,sz
          factorial(i) = factorial(i-1) .times. i
        end do
        !calc reciprocal of factorial
        do i = 0,sz
          reciproF(i) = inv( factorial(i) )
        end do
      end subroutine initialize

      pure function inv( x ) result( ans )
        implicit none
        integer(16),intent(in) :: x
        integer(16) :: ans,i

        ans = pow( x,modulus-2 )
      end function inv

!calc x**n (mod)
      pure function pow( x,n ) result( ans )
        implicit none
        integer(16),intent(in) :: x,n
        integer(16) :: ans, k, y

        ans = 1
        k = n
        y = x
        do while( k/=0 )
          if( iand(k,1)==1 )then
            ans = ans .times. y
          end if
          y = y .times. y
          k = k / 2
        end do
      end function pow

      !operators
      pure function modSum( a,b ) result( res )
        implicit none
        integer(16),intent(in) :: a,b
        integer(16) :: res

        res = mod( a+b,modulus )
      end function
      pure function modSub( a,b ) result( res )
        implicit none
        integer(16),intent(in) :: a,b
        integer(16) :: res

        res = mod( a-b+modulus,modulus )
      end function
      pure function modMulti( a,b ) result( res )
        implicit none
        integer(16),intent(in) :: a,b
        integer(16) :: res

        res = mod( a*b,modulus )
      end function

      END MODULE combinationMod

      PROGRAM bouquet
      use combinationMod
      IMPLICIT NONE
      integer(16) :: ans,n,a,b
      integer(16) :: subA,subB

      read*,n,a,b

      ans = pow(2_16,n) - 1

      call initialize()

      print*,ans
      subA = combinationN(n,a)
      print*,subA
      subB = combinationN(n,b)
      print*,subB
      ans = ans .minus. subA .minus. subB

      print*,ans

      END PROGRAM

Score : 400 points

Problem Statement

Akari has n kinds of flowers, one of each kind.

She is going to choose one or more of these flowers to make a bouquet.

However, she hates two numbers a and b, so the number of flowers in the bouquet cannot be a or b.

How many different bouquets are there that Akari can make?

Find the count modulo (10^9 + 7).

Here, two bouquets are considered different when there is a flower that is used in one of the bouquets but not in the other bouquet.

Constraints

All values in input are integers.

2 \leq n \leq 10^9

1 \leq a < b \leq \textrm{min}(n, 2 \times 10^5)

Input

Input is given from Standard Input in the following format:

n a b

Output

Print the number of bouquets that Akari can make, modulo (10^9 + 7). (If there are no such bouquets, print 0.)

Sample Input 1

4 1 3

Sample Output 1

7

In this case, Akari can choose 2 or 4 flowers to make the bouquet.

There are 6 ways to choose 2 out of the 4 flowers, and 1 way to choose 4, so there are a total of 7 different bouquets that Akari can make.

Sample Input 2

1000000000 141421 173205

Sample Output 2

34076506

Print the count modulo (10^9 + 7).


module Factwithmod
  implicit none
  integer(16),allocatable,dimension(:)::Kaijo
  contains
subroutine initiationFactorial(maxnum,mo)
  implicit none
  integer(16)::maxnum
  integer(16)::mo
  integer(16)::i

  allocate(Kaijo(0:maxnum))
  Kaijo(0)=1
  do i=1,maxnum
    Kaijo(i)=mod(Kaijo(i-1)*i,mo)
  end do
end subroutine

function combination(n,k,l)
implicit none
integer(16),intent(in)::n,k,l
integer(16) combination

combination=mod(Kaijo(N)*inv(Kaijo(K),l)*inv(Kaijo(N-K),l),l)

end function

function inv(K,L)
integer(16),intent(in)::k,l
integer(16) l2,y
integer(16) inv
inv=1
l2=l-2
y=k
do
  if(mod(l2,2)==1)inv=mod(inv*y,l)
  y=mod(y*y,l)
  l2=l2/2
  if(l2==0)exit
end do
end function

function permutation(n,k,mo)
implicit none
  integer(16)::n,k
  integer(16)::mo,permutation
  permutation=mod(Kaijo(N)*inv(Kaijo(N-K),mo),mo)
end function
end module Factwithmod
program ABC156b
    use Factwithmod
    implicit none
    integer(16)::N
    integer(16)::a,b
    integer(16)::mo=10**9+7
    integer(16)::allc
    integer(16)::ans
    read*,N,a,b
    allc=1
    call initiationFactorial(N,mo)
    allc=power_func(2_16,N,mo)-1
    ans=allc-combination(N,a,mo)-combination(N,b,mo)
    ans=mod(ans,mo)
    if(ans<0)ans=ans+mo
    print"(i0)",ans
contains
integer(16) function power_func(a,n,mo)
    integer(16)::a,n,mo
    integer(16)::l2,y
    power_func=1
    l2=N
    y=a
    do
        if(mod(l2,2)==1)power_func=mod(power_func*y,mo)
        y=mod(y*y,mo)
        l2=l2/2
        if(l2==0)exit
    end do
end function power_func
end program ABC156b


program modularness
  implicit none
  integer :: k, q, n, p, r, s, i, j
  integer(8) :: d(0:5000) = 0, x, m, dj, dn, a
  read(*,*) k, q
  read(*,*) d(0:k - 1)
  do i = 1, q
    read(*,*) n, x, m
    p = (n - 1) / k
    r = mod(n - 1, k)
    dn = x
    a = n - 1
    do j = 0, k - 1
      s = p + merge(1, 0, j < r)
      dj = mod(d(j), m)
      if (dj == 0) a = a - s
      dn = dn + dj * s
    end do
    a = a - dn / m + x / m
    write(*,'(i0)') a
  end do
end program modularness

Score : 600 points

Problem Statement

We have a sequence of k numbers: d_0,d_1,...,d_{k - 1}.

Process the following q queries in order:

The i-th query contains three integers n_i, x_i, and m_i.
Let a_0,a_1,...,a_{n_i - 1} be the following sequence of n_i numbers: \begin{eqnarray} a_j = \begin{cases} x_i & ( j = 0 ) \\ a_{j - 1} + d_{(j - 1)~\textrm{mod}~k} & ( 0 < j \leq n_i - 1 ) \end{cases}\end{eqnarray}
Print the number of j~(0 \leq j < n_i - 1) such that (a_j~\textrm{mod}~m_i) < (a_{j + 1}~\textrm{mod}~m_i).

Here (y~\textrm{mod}~z) denotes the remainder of y divided by z, for two integers y and z~(z > 0).

Constraints

All values in input are integers.

1 \leq k, q \leq 5000

0 \leq d_i \leq 10^9

2 \leq n_i \leq 10^9

0 \leq x_i \leq 10^9

2 \leq m_i \leq 10^9

Input

Input is given from Standard Input in the following format:

k q
d_0 d_1 ... d_{k - 1}
n_1 x_1 m_1
n_2 x_2 m_2
:
n_q x_q m_q

Output

Print q lines.

The i-th line should contain the response to the i-th query.

Sample Input 1

3 1
3 1 4
5 3 2

Sample Output 1

1

For the first query, the sequence {a_j} will be 3,6,7,11,14.

(a_0~\textrm{mod}~2) > (a_1~\textrm{mod}~2)

(a_1~\textrm{mod}~2) < (a_2~\textrm{mod}~2)

(a_2~\textrm{mod}~2) = (a_3~\textrm{mod}~2)

(a_3~\textrm{mod}~2) > (a_4~\textrm{mod}~2)

Thus, the response to this query should be 1.

Sample Input 2

7 3
27 18 28 18 28 46 1000000000
1000000000 1 7
1000000000 2 10
1000000000 3 12

Sample Output 2

224489796
214285714
559523809


program poor
    integer(4):: a,b,c
    read*, a,b,c

    if (((a/=b) .and. (a/=c) .and. (b/=c)) .or. ((a==b) .and.(a==c))) then
        print*, "No"
    else
        print*, "Yes"
    end if
end program poor

Score: 100 points

Problem Statement

A triple of numbers is said to be poor when two of those numbers are equal but the other number is different from those two numbers.

You will be given three integers A, B, and C. If this triple is poor, print Yes; otherwise, print No.

Constraints

A, B, and C are all integers between 1 and 9 (inclusive).

Input

Input is given from Standard Input in the following format:

A B C

Output

If the given triple is poor, print Yes; otherwise, print No.

Sample Input 1

5 7 5

Sample Output 1

Yes

A and C are equal, but B is different from those two numbers, so this triple is poor.

Sample Input 2

4 4 4

Sample Output 2

No

A, B, and C are all equal, so this triple is not poor.

Sample Input 3

4 9 6

Sample Output 3

No

Sample Input 4

3 3 4

Sample Output 4

Yes


program main

  implicit none

  !character(10) :: s, t, u
  integer :: a, b, c, n

  read(*,*) a, b, c

  n = 0
  if( a == b ) then
    n = n + 1
  end if
  if( a == c ) then
    n = n + 1
  end if
  if( b == c ) then
    n = n + 1
  end if

  if ( n == 1 ) then
    print*, "Yes"
  else
    print*, "No"
  end if

end program main


      module charaSort
      implicit none

      contains
      recursive function sort( s ) result( r )
        implicit none
        character(10),intent(in) :: s(:)
        character(10) :: r( size(s) )

        if( size(s)>1 )then
        !  r = [sort( pack(s, s<s(1)) ), &
        !       pack(s,s==s(1)), &
        !       sort( pack(s,s>s(1)) )]
          r = [sort( pack(s, s< s(1)) ), &
               sort( pack(s, s>=s(1)) )]
        else
          r = s
        end if
      end function
      end module

      PROGRAM Poll
      use charaSort
      IMPLICIT NONE
      integer(16) :: n
      character(10),allocatable :: s(:)
      character(10),allocatable :: note1(:)
      integer(16) :: i,counter
      integer(16),allocatable :: note2(:)
      logical,allocatable :: flag(:)

      read*,n
      allocate( s(1:n) )
      allocate(note1(n),note2(n),flag(n))
      do i = 1,n
        read*,s(i)
      end do

      s = sort(s)

      counter = 1
      note1(1) = s(1)
      note2(:) = 0
      do i = 2,n
        if(note1(counter)==s(i))then
          note2(counter) = note2(counter) + 1
        else
          counter = counter + 1
          note1(counter) = s(i)
        end if
      end do

      flag = note2==maxval(note2)

      do i = 1,counter
        if(flag(i))then
          print*,note1(i)
        end if
      end do

      END PROGRAM

Score: 300 points

Problem Statement

We have N voting papers. The i-th vote (1 \leq i \leq N) has the string S_i written on it.

Print all strings that are written on the most number of votes, in lexicographical order.

Constraints

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

S_i (1 \leq i \leq N) are strings consisting of lowercase English letters.

The length of S_i (1 \leq i \leq N) is between 1 and 10 (inclusive).

Input

Input is given from Standard Input in the following format:

N
S_1
:
S_N

Output

Print all strings in question in lexicographical order.

Sample Input 1

7
beat
vet
beet
bed
vet
bet
beet

Sample Output 1

beet
vet

beet and vet are written on two sheets each, while beat, bed, and bet are written on one vote each. Thus, we should print the strings beet and vet.

Sample Input 2

8
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo

Sample Output 2

buffalo

Sample Input 3

7
bass
bass
kick
kick
bass
kick
kick

Sample Output 3

kick

Sample Input 4

4
ushi
tapu
nichia
kun

Sample Output 4

kun
nichia
tapu
ushi


module mod_unweighted_graph
  implicit none
  type t_list
    integer, allocatable :: at(:, :)
  end type
  type unweighted_graph
    type(t_list), allocatable :: list(:)
  end type
  interface unweighted_graph
    module procedure :: newug0
  end interface unweighted_graph
contains
  type(unweighted_graph) function newug0(n, m, a, b, e, undirected) result(res)
    integer, intent(in) :: n, m, a(:), b(:), e(:)
    logical, optional, intent(in) :: undirected
    logical :: u
    integer :: c(n), i
    u = merge(undirected, .true., present(undirected))
    c = 0
    do i = 1, m
      c(a(i)) = c(a(i)) + 1
      if (u) c(b(i)) = c(b(i)) + 1
    end do
    allocate(res%list(n))
    do i = 1, n
      allocate(res%list(i)%at(c(i), 2))
    end do
    do i = m, 1, -1
      res%list(a(i))%at(c(a(i)), 1) = b(i)
      res%list(a(i))%at(c(a(i)), 2) = e(i)
      c(a(i)) = c(a(i)) - 1
      if (u) then
        res%list(b(i))%at(c(b(i)), 1) = a(i)
        res%list(b(i))%at(c(b(i)), 2) = e(i)
        c(b(i)) = c(b(i)) - 1
      end if
    end do
  end
end module mod_unweighted_graph
module mod_two_dim_merge_sort
  implicit none
  integer, private, parameter :: intkind = 4
contains
  logical function less(a, b) result(res)
    integer(intkind), intent(in) :: a(:), b(:)
    res = a(1) < b(1)
  end
  subroutine merge_sort(a)
    integer(intkind), intent(inout) :: a(:, :)
    integer :: n, m, l, i, u
    n = size(a, 1)
    m = n
    l = 1
    do while (m > 1)
      do i = 1, m / 2
        u = min(2 * i * l, n)
        call merger(a(2 * (i - 1) * l + 1:(2 * i - 1) * l, :), &
        a((2 * i - 1) * l + 1:u, :))
      end do
      l = 2 * l
      m = (m + 1) / 2
    end do
  end
  subroutine merger(a1, a2)
    integer(intkind), intent(inout) :: a1(:, :), a2(:, :)
    integer(intkind) :: a(size(a1, 1) + size(a2, 1), size(a1, 2))
    integer :: i1, i2, n1, n2
    i1 = 1
    i2 = 1
    n1 = size(a1, 1)
    n2 = size(a2, 1)
    do while (i1 <= n1 .and. i2 <= n2)
      if (less(a1(i1, :), a2(i2, :))) then
        a(i1 + i2 - 1, :) = a1(i1, :)
        i1 = i1 + 1
      else
        a(i1 + i2 - 1, :) = a2(i2, :)
        i2 = i2 + 1
       end if
    end do
    if (i1 <= n1) then
      a(i1 + i2 - 1:n1 + n2, :) = a1(i1:n1, :)
    else
      a(i1 + i2 - 1:n1 + n2, :) = a2(i2:n2, :)
    end if
    a1 = a(1:n1, :)
    a2 = a(n1 + 1:n1 + n2, :)
  end
end module mod_two_dim_merge_sort
program perils_in_parallel
  use mod_unweighted_graph
  use mod_two_dim_merge_sort
  implicit none
  integer :: n, m, cnt = 0, i, j, l, r, p
  integer :: a(100000, 2) = 0, x(200000, 3) = 0, y(200000, 1) = 0, z(100001) = 0
  logical :: used(100001) = .false.
  type(unweighted_graph) :: g
  read(*,*) n, m
  do i = 1, n
    read(*,*) a(i, :)
  end do
  call merge_sort(a(1:n, :))
  z(1) = a(1, 2)
  do i = 2, n
    z(i) = xor(a(i, 2), a(i - 1, 2))
  end do
  z(n + 1) = a(n, 2)
  do i = 1, m
    j = cnt + 1
    read(*,*) x(j, 1:2)
    x(j, 3) = i
    if (x(j, 1) > a(n, 1)) cycle
    if (x(j, 2) < a(1, 1)) cycle
    l = 0
    r = n
    do while (r - l > 1)
      p = (l + r) / 2
      if (x(j, 1) <= a(p, 1)) then
        r = p
      else
        l = p
      end if
    end do
    x(j, 1) = r
    l = 1
    r = n + 1
    do while (r - l > 1)
      p = (l + r) / 2
      if (x(j, 2) >= a(p, 1)) then
        l = p
      else
        r = p
      end if
    end do
    x(j, 2) = r
    if (x(j, 1) < x(j, 2)) cnt = cnt + 1
  end do
  m = cnt
  g = unweighted_graph(n + 1, m, x(1:m, 1), x(1:m, 2), x(1:m, 3))
  p = 0
  do i = 1, n + 1
    if (used(i)) cycle
    if (btest(dfs(g, i), 0)) then
      write(*,'(i0)') -1
      stop
    end if
  end do
  write(*,'(i0)') p
  if (p > 0) then
    call merge_sort(y(1:p, :))
    write(*,'(i0)',advance='no') y(1, 1)
    do i = 2, p
      write(*,'(x,i0)',advance='no') y(i, 1)
    end do
  end if
  write(*,*)
contains
  recursive integer function dfs(this, v) result(res)
    type(unweighted_graph), intent(in) :: this
    integer, intent(in) :: v
    integer :: i, u, tmp
    used(v) = .true.
    res = z(v)
    do i = 1, size(this%list(v)%at, 1)
      u = this%list(v)%at(i, 1)
      if (used(u)) cycle
      tmp = dfs(this, u)
      if (btest(tmp, 0)) then
        p = p + 1
        y(p, 1) = this%list(v)%at(i, 2)
      end if
      res = xor(res, tmp)
    end do
  end
end program perils_in_parallel

Score: 600 points

Problem Statement

After being invaded by the Kingdom of AlDebaran, bombs are planted throughout our country, AtCoder Kingdom.

Fortunately, our military team called ABC has managed to obtain a device that is a part of the system controlling the bombs.

There are N bombs, numbered 1 to N, planted in our country. Bomb i is planted at the coordinate A_i. It is currently activated if B_i=1, and deactivated if B_i=0.

The device has M cords numbered 1 to M. If we cut Cord j, the states of all the bombs planted between the coordinates L_j and R_j (inclusive) will be switched - from activated to deactivated, and vice versa.

Determine whether it is possible to deactivate all the bombs at the same time. If the answer is yes, output a set of cords that should be cut.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

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

A_i are pairwise distinct.

B_i is 0 or 1. (1 \leq i \leq N)

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

1 \leq L_j \leq R_j \leq 10^9\ (1 \leq j \leq M)

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
:
A_N B_N
L_1 R_1
:
L_M R_M

Output

If it is impossible to deactivate all the bombs at the same time, print -1. If it is possible to do so, print a set of cords that should be cut, as follows:

k
c_1 c_2 \dots c_k

Here, k is the number of cords (possibly 0), and c_1, c_2, \dots, c_k represent the cords that should be cut. 1 \leq c_1 < c_2 < \dots < c_k \leq M must hold.

Sample Input 1

3 4
5 1
10 1
8 0
1 10
4 5
6 7
8 9

Sample Output 1

2
1 4

There are two activated bombs at the coordinates 5, 10, and one deactivated bomb at the coordinate 8.

Cutting Cord 1 switches the states of all the bombs planted between the coordinates 1 and 10, that is, all of the three bombs.

Cutting Cord 4 switches the states of all the bombs planted between the coordinates 8 and 9, that is, Bomb 3.

Thus, we can deactivate all the bombs by cutting Cord 1 and Cord 4.

Sample Input 2

4 2
2 0
3 1
5 1
7 0
1 4
4 7

Sample Output 2

-1

Cutting any set of cords will not deactivate all the bombs at the same time.

Sample Input 3

3 2
5 0
10 0
8 0
6 9
66 99

Sample Output 3

0

All the bombs are already deactivated, so we do not need to cut any cord.

Sample Input 4

12 20
536130100 1
150049660 1
79245447 1
132551741 0
89484841 1
328129089 0
623467741 0
248785745 0
421631475 0
498966877 0
43768791 1
112237273 0
21499042 142460201
58176487 384985131
88563042 144788076
120198276 497115965
134867387 563350571
211946499 458996604
233934566 297258009
335674184 555985828
414601661 520203502
101135608 501051309
90972258 300372385
255474956 630621190
436210625 517850028
145652401 192476406
377607297 520655694
244404406 304034433
112237273 359737255
392593015 463983307
150586788 504362212
54772353 83124235

Sample Output 4

5
1 7 8 9 11

If there are multiple sets of cords that deactivate all the bombs when cut, any of them can be printed.


program main
	implicit none
    character(10)::s,t,u
    integer::a,b
    read(*,*) s,t
    read(*,*)a,b
    read(*,*)u

    if(s==u)then
    	write(*,*) a-1,b
    else
    	write(*,*) a,b-1
    end if
    stop
end program main

Score : 100 points

Problem Statement

We have A balls with the string S written on each of them and B balls with the string T written on each of them.

From these balls, Takahashi chooses one with the string U written on it and throws it away.

Find the number of balls with the string S and balls with the string T that we have now.

Constraints

S, T, and U are strings consisting of lowercase English letters.

The lengths of S and T are each between 1 and 10 (inclusive).

S \not= T

S=U or T=U.

1 \leq A,B \leq 10

A and B are integers.

Input

Input is given from Standard Input in the following format:

S T
A B
U

Output

Print the answer, with space in between.

Sample Input 1

red blue
3 4
red

Sample Output 1

2 4

Takahashi chose a ball with red written on it and threw it away.
Now we have two balls with the string S and four balls with the string T.

Sample Input 2

red blue
5 5
blue

Sample Output 2

5 4

Takahashi chose a ball with blue written on it and threw it away.
Now we have five balls with the string S and four balls with the string T.


program main
      implicit none
      character(10) :: s, t, u
      integer :: a, b

      read *, s, t
      read *, a, b
      read *, u

      if ( u == s ) then
        print *, a-1, b
      else
        print *, a, b-1
      end if

end program main


  Program Main

    Implicit None
    Character            :: S*10,T*10,U*10
    Integer              :: A,B

    Read(*,*) S,T
    Read(*,*) A,B
    Read(*,*) U

    If(S==U) A=A-1
    If(T==U) B=B-1

    Write(*,"(I0,x,I0)") A,B

  End Program


! 2020.2.9
!
      program answer
      implicit none
      integer :: N
      integer,allocatable,dimension(:) :: A
      integer :: i
      character(len=3) :: ans
!
      ans = 'YES'
      read(*,*) N
      allocate(A(N))
      read(*,*) (A(i),i=1,N)
!
      call quicksort(N,A,1,N)
      do i=1,N-1
        if(A(i) == A(i+1)) ans='NO'
      end do
!
      write(*,*) ans
!
      end program answer
!
!-------------------------------------------------
      recursive subroutine quicksort(n,x,l,r)
!-------------------------------------------------
! Variables I/O
!-------------------------------------------------
! n     ...  the number of data (IN) : integer
! x(i)  ...  data (INOUT) : integer
! l     ...  the smallest label in a sorted region
!            (compute label)
! r     ...  the largest label in a sorted region
!            (compute label)
!-------------------------------------------------
! Variables (The others)
!-------------------------------------------------
! c     ...  the average of l and r : (l+r)/2
! ls    ...  the smallest label in a sorted region
!            (Input label)
! i     ...  the largest label in a sorted region
!            (Input label)
!-------------------------------------------------
! Contain
!-------------------------------------------------
! Subroutine : swap(a,b) : swaping a for b
!-------------------------------------------------
      implicit none
      integer,intent(in) :: n,l,r
      integer,dimension(n),intent(inout) :: x
      integer :: c,ls,i
!
      if(l < r) then
        c = (l+r) / 2 ! representative value
        call swap(x(l),x(c))
        ls = l
        do i=l+1,r
          if(x(i) < x(l)) then
            ls = ls + 1
            call swap(x(ls),x(i))
          end if
        end do
        call swap(x(ls),x(l))
        call quicksort(n,x,l,ls-1)
        call quicksort(n,x,ls+1,r)
      end if
      contains
!-------------------------------------------------
      subroutine swap(a,b)
!-------------------------------------------------
        integer,intent(inout) :: a,b
        integer :: w
        w=a
        a=b
        b=w
      end subroutine swap
      end subroutine quicksort
!

Score : 300 points

Problem Statement

Given is a sequence of integers A_1, A_2, ..., A_N.
If its elements are pairwise distinct, print YES; otherwise, print NO.

Constraints

2 ≤ N ≤ 200000

1 ≤ A_i ≤ 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 ... A_N

Output

If the elements of the sequence are pairwise distinct, print YES; otherwise, print NO.

Sample Input 1

5
2 6 1 4 5

Sample Output 1

YES

The elements are pairwise distinct.

Sample Input 2

6
4 1 3 1 6 2

Sample Output 2

NO

The second and fourth elements are identical.

Sample Input 3

2
10000000 10000000

Sample Output 3

NO


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

  integer ::i,k,j,l
  double precision :: 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 Beg3
  implicit none
  integer :: N, i, j
  integer, allocatable :: A(:)

  read(*,*) N
  allocate(A(N))
  read(*,*) (A(i), i=1,N)

  call heapsort(N,A)

  do i=1,N-1
     if(A(i).eq.A(i+1)) goto 500
  enddo

  print*, 'YES'
  stop

  500 print *, 'NO'

end program Beg3


program ABC154_D
  integer::N,K
  double precision,allocatable::p(:),a(:),b(:)
  read(*,*)N,K
  allocate(p(N),a(N),b(N))
  read(*,*)p
  do i=1,N
     a(i)=(p(i)+1.0)/2.0
  end do
  b=0
  do i=1,N-K+1
     b(i)=sum(a(i:i+K-1))
  end do
  ans=maxval(b)
  write(*,*)ans
end program ABC154_D

Score : 400 points

Problem Statement

We have N dice arranged in a line from left to right. The i-th die from the left shows p_i numbers from 1 to p_i with equal probability when thrown.

We will choose K adjacent dice, throw each of them independently, and compute the sum of the numbers shown. Find the maximum possible value of the expected value of this sum.

Constraints

1 ≤ K ≤ N ≤ 200000

1 ≤ p_i ≤ 1000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
p_1 ... p_N

Output

Print the maximum possible value of the expected value of the sum of the numbers shown.

Your output will be considered correct when its absolute or relative error from our answer is at most 10^{-6}.

Sample Input 1

5 3
1 2 2 4 5

Sample Output 1

7.000000000000

When we throw the third, fourth, and fifth dice from the left, the expected value of the sum of the numbers shown is 7. This is the maximum value we can achieve.

Sample Input 2

4 1
6 6 6 6

Sample Output 2

3.500000000000

Regardless of which die we choose, the expected value of the number shown is 3.5.

Sample Input 3

10 4
17 13 13 12 15 20 10 13 17 11

Sample Output 3

32.000000000000


program main
	implicit none
    integer::h,a
    read(*,*) h,a
    if(mod(h,a)==0)then
    	write(*,*)h/a
    else
    	write(*,*)h/a +1
    end if
    stop
end program main

Score : 100 points

Problem Statement

Serval is fighting with a monster.

The health of the monster is H.

In one attack, Serval can decrease the monster's health by A.
There is no other way to decrease the monster's health.

Serval wins when the monster's health becomes 0 or below.

Find the number of attacks Serval needs to make before winning.

Constraints

1 \leq H \leq 10^4

1 \leq A \leq 10^4

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H A

Output

Print the number of attacks Serval needs to make before winning.

Sample Input 1

10 4

Sample Output 1

3

After one attack, the monster's health will be 6.

After two attacks, the monster's health will be 2.

After three attacks, the monster's health will be -2.

Thus, Serval needs to make three attacks to win.

Sample Input 2

1 10000

Sample Output 2

1

Sample Input 3

10000 1

Sample Output 3

10000


program example
	implicit none

	integer(8) :: N,K,i,wa=0
    integer(8),allocatable :: H(:)

    read(*,*) N,K

    allocate(H(N))

    read(*,*) H(:)

    if (K==0) then
    	goto 1
    end if

    do i=1,K

        if (sum(H)==0) then
        	exit
        else
            H(maxloc(H(:),1))=0
        end if
    end do

1    wa=wa+sum(H(:))

    write(*,*) wa

end program example


program main
   implicit none
   integer h,a,n
   read(*,*) h,a
   n=h/a+1
   write(*,'(i0)') n
end program main


program main

  implicit none

  integer    :: n, k, i, j
  integer(8),allocatable :: h(:)
  integer(8) :: sumh

  read(*,*) n,k
  allocate( h(n) )
  read(*,*) h

  if ( k == 0 ) then
    sumh = 0
    do i = 1 ,n
      sumh = sumh + h(i)
    end do
    print*, sumh
    stop
  end if

  if ( k >= n ) then
    print*, "0"
  else
    call quick_sort( h, 1, n )
    sumh = 0
    do i = 1 ,n-k
      sumh = sumh + h(i)
    end do
    print "(i0)", sumh
  end if

end program main
 recursive subroutine quick_sort( a, i, j )

  implicit none
  integer, intent(in) :: i,j
  integer(8), intent(inout) :: a(i:j)
  integer :: p, k

  if( i == j ) return

  p  = pivot(a,i,j)

  if( p /= -1 ) then
    call partition(a(i:j),i,j,a(p),k)
    call quick_sort(a(i:k-p),i,k-1)
    call quick_sort(a(k:j),k,j)
  end if

contains

subroutine partition( a, i, j, x, l )

  implicit none
  integer, intent(in) :: i,j
  integer(8), intent(in) :: x
  integer(8), intent(inout) :: a(i:j)
  integer, intent(out) :: l
  integer :: r
  integer(8) :: t

  l = i
  r = j

  do while( l<=r )
    do while( l<=j .and. a(l) <  x )
      l = l+1
    end do
    do while( r> i .and. a(r) >= x )
      r = r-1
    end do
    if(l>r) exit
    t = a(l)
    a(l) = a(r)
    a(r) = t
    l = l+1
    r = r-1
  end do

end subroutine partition

function pivot( a, i, j ) result( k )

  implicit none
  integer, intent(in) :: i,j
  integer:: k
  integer(8), intent(in) :: a(i:j)

  k = i + 1

  do while( k<=j .and. a(i) == a(k) )
    k = k + 1
  end do

  if(k>j) k = -1

  if(a(i)>a(k)) k = i

end function pivot

end subroutine quick_sort


program example
	implicit none

	integer(8) :: N,K,i,wa=0,j,kari
    integer(8),allocatable :: H(:)

    read(*,*) N,K

    allocate(H(N))

    read(*,*) H(:)

    if (K==0) then
    	goto 1
    end if

    do i=1,N
    	do j=i+1,N

        	if(H(j)>=H(i)) then
            	kari=H(i)
            	H(i)=H(j)
                H(j)=kari

            end if

        end do
    end do

1   	write(*,*) sum(H(K+1:))

end program example

Score : 300 points

Problem Statement

Fennec is fighting with N monsters.

The health of the i-th monster is H_i.

Fennec can do the following two actions:

Attack: Fennec chooses one monster. That monster's health will decrease by 1.

Special Move: Fennec chooses one monster. That monster's health will become 0.

There is no way other than Attack and Special Move to decrease the monsters' health.

Fennec wins when all the monsters' healths become 0 or below.

Find the minimum number of times Fennec needs to do Attack (not counting Special Move) before winning when she can use Special Move at most K times.

Constraints

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

0 \leq K \leq 2 \times 10^5

1 \leq H_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
H_1 ... H_N

Output

Print the minimum number of times Fennec needs to do Attack (not counting Special Move) before winning.

Sample Input 1

3 1
4 1 5

Sample Output 1

5

By using Special Move on the third monster, and doing Attack four times on the first monster and once on the second monster, Fennec can win with five Attacks.

Sample Input 2

8 9
7 9 3 2 3 8 4 6

Sample Output 2

0

She can use Special Move on all the monsters.

Sample Input 3

3 0
1000000000 1000000000 1000000000

Sample Output 3

3000000000

Watch out for overflow.


program example
	implicit none

	integer(8) :: N,K,i,wa=0,location
    integer(8),allocatable :: H(:)

    read(*,*) N,K

    allocate(H(N))

    read(*,*) H(:)

    do i=1,K

        if (sum(H)==0) then
        	exit
        else
        	location=maxloc(H(:),1)
            H(location)=0
        end if
    end do

    wa=wa+sum(H(:))

    write(*,*) wa

end program example


  Program Main

    Implicit None

    Integer, Allocatable :: H(:)
    Integer(8)           :: i,j,N,K,s,Hmax

    N = 0 ; K = 0 ; s = 0 ; Hmax = 0
    Read(*,*) N,K
    Allocate (H(N)); H(:) = 0.0d0
    Read(*,*)(H(i),i=1,N)

    If (K < N) then
       Do i = 1,K
          Hmax = Maxval(H(:))
          Check:Do j = 1,N
                   If (H(j) == Hmax) then
                      H(j) = 0
                      Exit Check
                   Endif
                Enddo Check
       Enddo
       Do i = 1,N
          s = s + H(i)
       Enddo
    Endif

    Write(*,"(I0)") s
    Deallocate(H)

  End Program


program main
    implicit none
    integer(8), allocatable:: h(:)
    integer(8):: n, k, one, ans, i
    one =1
    read*, n, k

    if (k >= n)then
        print*, 0
        stop
    end if

    allocate(h(n))
    read*, h(:)
    call marge_sort(h(:), one, n)
    h(1:k) = 0
    print*, sum(h(:))
contains
recursive subroutine marge_sort(arr,first,last)
    integer(8):: arr(:), tmp
    integer(8):: first, last, mean

    if (last - first <= 2) then
        if (arr(first) <= arr(last))then
            tmp = arr(first)
            arr(first) = arr(last)
            arr(last) = tmp
        end if
        return
    end if

    mean = (first + last) / 2
    call marge_sort(arr, first, mean)
    call marge_sort(arr, mean+1, last)
    call marge(arr, first, mean, last)

end subroutine

subroutine marge(arr, first, mean, last)
    integer(8):: arr(:)
    integer(8), allocatable:: tmp(:)
    integer(8):: first, mean, last
    integer(8):: i, i_left, i_right

    allocate(tmp(last-first+1))
    i_left = first
    i_right = mean + 1
    i = 0
    do while((i_left <= mean) .and. (i_right <= last))
        i = i + 1
        if (arr(i_left) >= arr(i_right))then
            tmp(i) = arr(i_left)
            i_left = i_left + 1
        else
            tmp(i) = arr(i_right)
            i_right = i_right + 1
        end if
    end do

    if (i_left > mean)then
        tmp(i+1:) = arr(i_right:last)
    else
        tmp(i+1:) = arr(i_left:mean)
    end if
    arr(first:last) = tmp(:)

end subroutine
end program main


program main
    implicit none
    integer(8), allocatable:: h(:)
    integer(8):: n, k, one
    one =1
    read*,n,k

    if (k >= n)then
        print*, 0
        stop
    end if

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

    call marge_sort(h(:), one, n)

    print*, sum(h(k+1:))
contains
recursive subroutine marge_sort(arr,first,last)
    integer(8):: arr(:), tmp
    integer(8):: first, last, mean

    if (last - first <= 2) then
        if (arr(first) <= arr(last))then
            tmp = arr(first)
            arr(first) = arr(last)
            arr(last) = tmp
        end if
        return
    end if

    mean = (first + last) / 2
    call marge_sort(arr, first, mean)
    call marge_sort(arr, mean+1, last)
    call marge(arr, first, mean, last)

end subroutine

subroutine marge(arr, first, mean, last)
    integer(8):: arr(:)
    integer(8), allocatable:: tmp(:)
    integer(8):: first, mean, last
    integer(8):: i, i_left, i_right

    allocate(tmp(last-first+1))
    i_left = first
    i_right = mean + 1
    i = 0
    do while((i_left <= mean) .and. (i_right <= last))
        i = i + 1
        if (arr(i_left) >= arr(i_right))then
            tmp(i) = arr(i_left)
            i_left = i_left + 1
        else
            tmp(i) = arr(i_right)
            i_right = i_right + 1
        end if
    end do

    if (i_left > mean)then
        tmp(i+1:) = arr(i_right:last)
    else
        tmp(i+1:) = arr(i_left:mean)
    end if
    arr(first:last) = tmp(:)

end subroutine
end program main


program main
    implicit none
    integer(8), allocatable:: h(:)
    integer(4):: n, k

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

    call marge_sort(h(:), 1, n)

    print*, sum(h(k+1:))
contains
recursive subroutine marge_sort(arr,first,last)
    integer(8):: arr(:), tmp
    integer(4):: first, last, mean

    if (last - first <= 2) then
        if (arr(first) <= arr(last))then
            tmp = arr(first)
            arr(first) = arr(last)
            arr(last) = tmp
        end if
        return
    end if

    mean = (first + last) / 2
    call marge_sort(arr, first, mean)
    call marge_sort(arr, mean+1, last)
    call marge(arr, first, mean, last)

end subroutine

subroutine marge(arr, first, mean, last)
    integer(8):: arr(:)
    integer(8), allocatable:: tmp(:)
    integer(4):: first, mean, last
    integer(4):: i, i_left, i_right

    allocate(tmp(last-first+1))
    i_left = first
    i_right = mean + 1
    i = 0
    do while((i_left <= mean) .and. (i_right <= last))
        i = i + 1
        if (arr(i_left) >= arr(i_right))then
            tmp(i) = arr(i_left)
            i_left = i_left + 1
        else
            tmp(i) = arr(i_right)
            i_right = i_right + 1
        end if
    end do

    if (i_left > mean)then
        tmp(i+1:) = arr(i_right:last)
    else
        tmp(i+1:) = arr(i_left:mean)
    end if
    arr(first:last) = tmp(:)

end subroutine
end program main


      module sortMod
        implicit none

        contains

        subroutine bSort( a ,kk )
          implicit none
          integer(16) :: a(:)
          integer(16) :: n,i,k,buffer,kk

          n = size(a)
          if( n==1 ) return
          if( kk==n ) kk = kk -1

          do i = 1,kk
            do k = i+1,n
              if( a(i)<a(k) )then
                buffer = a(i)
                a(i) = a(k)
                a(k) = buffer
              end if
            end do
          end do
        end subroutine

      end module

      PROGRAM piyo
      use sortMod
      IMPLICIT NONE
      integer(16) :: n,k
      integer(16),allocatable :: h(:),sh(:)

      read*,n,k
      allocate(h(n),sh(n))
      read*,h(:)

      call bsort( h,k )
      !sh = qSort( h )

      if( k/=0 )then
        if(k>n) k = n
        h(1:k) = 0
      end if

      print*,sum(h)

      END PROGRAM


      module sortMod
        implicit none

        contains

        subroutine bSort( a ,kk )
          implicit none
          integer(16) :: a(:)
          integer(16) :: n,i,k,buffer,kk

          n = size(a)
          if( kk==n ) kk = kk -1

          do i = 1,n
            do k = i+1,n
              if( a(i)<a(k) )then
                buffer = a(i)
                a(i) = a(k)
                a(k) = buffer
              end if
            end do
          end do
        end subroutine

        recursive function qSort( a ) result( r )
          integer(16) :: a(:)
          integer(16),allocatable :: r(:)
          integer(16) :: k

          if( size(a)<=1 )then
            r = a
          else
            k = a(size(a)/2)
            r = [qSort(pack(a,a<k)),pack(a,a==r),qSort(pack(a,a>k))]
          end if
        end function qSort
      end module

      PROGRAM piyo
      use sortMod
      IMPLICIT NONE
      integer(16) :: n,k
      integer(16),allocatable :: h(:),sh(:)

      read*,n,k
      allocate(h(n),sh(n))
      read*,h(:)

      call bsort( h,k )
      !sh = qSort( h )

      if( k/=0 )then
        h(1:k) = 0
      end if

      print*,sum(h)

      END PROGRAM


program main
      implicit none
      integer(16) :: h
      integer(8) :: k, a, ppow

      read *, h

      if ( h == 1 ) then
         print *, '1'
      else
        k = 0
        do
          h = h/2
          k = k + 1
          if ( h == 1 ) exit
        end do
        a = 2
        call pow(a,k+1,ppow)
        print *, ppow - 1
      end if

contains
subroutine pow(a, n, ppow)
        implicit none
        integer(8), intent(in) :: a, n
        integer(8), intent(out) :: ppow
        integer(8) :: ntmp, atmp

        ppow = 1
        atmp = a
        ntmp = n
        do while ( ntmp > 0 )
          if ( iand(ntmp,1) > 0 ) then
            ppow = ppow * atmp
          end if
          atmp = atmp * atmp
          ntmp = ishft( ntmp, -1 )
        end do

end subroutine pow

end program main

Score : 400 points

Problem Statement

Caracal is fighting with a monster.

The health of the monster is H.

Caracal can attack by choosing one monster. When a monster is attacked, depending on that monster's health, the following happens:

If the monster's health is 1, it drops to 0.

If the monster's health, X, is greater than 1, that monster disappears. Then, two new monsters appear, each with the health of \lfloor X/2 \rfloor.

(\lfloor r \rfloor denotes the greatest integer not exceeding r.)

Caracal wins when the healths of all existing monsters become 0 or below.

Find the minimum number of attacks Caracal needs to make before winning.

Constraints

1 \leq H \leq 10^{12}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H

Output

Find the minimum number of attacks Caracal needs to make before winning.

Sample Input 1

2

Sample Output 1

3

When Caracal attacks the initial monster, it disappears, and two monsters appear, each with the health of 1.

Then, Caracal can attack each of these new monsters once and win with a total of three attacks.

Sample Input 2

4

Sample Output 2

7

Sample Input 3

1000000000000

Sample Output 3

1099511627775


program BegD
  implicit none
  integer(8) :: H, cnt
  integer(8) ::i!,amr,Hnew

  read(*,*) H
  i=0
  cnt=0
  do
     i=i+1
     H=H/2
     cnt=cnt+2**(i-1)
     if(H.eq.1) goto 500
  enddo
  500 continue
  print*, cnt+2**i

end program BegD


program ddd
implicit none

integer(8) :: h, enm, atk

read*, h

atk = 0
enm = 1

do while(h/=1)

atk = atk + enm
enm = enm*2
h = h/2

end do

atk = atk + enm
print*, atk

end program