program test
implicit none
integer::i,n,check
character::S*100
check=0
read*,S
S=trim(S)
n=len(S)
do i=1,n
	if(mod(i,2)==1)then
    	if(S(i:i)=='L')then
        	check=check+1
         else
         	check=check
         end if
    else
    	if(S(i:i)=='R')then
        	check=check+1
         else
         	check=check
         end if
    end if
end do
if(check==0)then
	print*,'Yes'
else
	print*,'No'
end if
end program test

Score: 200 points

Problem Statement

Takahashi will do a tap dance. The dance is described by a string S where each character is L, R, U, or D. These characters indicate the positions on which Takahashi should step. He will follow these instructions one by one in order, starting with the first character.

S is said to be easily playable if and only if it satisfies both of the following conditions:

Every character in an odd position (1-st, 3-rd, 5-th, \ldots) is R, U, or D.

Every character in an even position (2-nd, 4-th, 6-th, \ldots) is L, U, or D.

Your task is to print Yes if S is easily playable, and No otherwise.

Constraints

S is a string of length between 1 and 100 (inclusive).

Each character of S is L, R, U, or D.

Input

Input is given from Standard Input in the following format:

S

Output

Print Yes if S is easily playable, and No otherwise.

Sample Input 1

RUDLUDR

Sample Output 1

Yes

Every character in an odd position (1-st, 3-rd, 5-th, 7-th) is R, U, or D.

Every character in an even position (2-nd, 4-th, 6-th) is L, U, or D.

Thus, S is easily playable.

Sample Input 2

DULL

Sample Output 2

No

The 3-rd character is not R, U, nor D, so S is not easily playable.

Sample Input 3

UUUUUUUUUUUUUUU

Sample Output 3

Yes

Sample Input 4

ULURU

Sample Output 4

No

Sample Input 5

RDULULDURURLRDULRLR

Sample Output 5

Yes


program main

  implicit none
  character(len=100) S
  integer :: lnS, i

  read(*,*) S

  lnS = len(trim(S))

  ! check odd
  do i = 1,lnS,2
     if ( S(i:i) == "L") then
        write(*,'(a)') "No"
        stop
     end if
  end do
  ! check even
  do i = 2,lnS,2
     if ( S(i:i) == "R") then
        write(*,'(a)') "No"
        stop
     end if
  end do

  write(*,'(a)') "Yes"

end program main

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

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 answer
      implicit none
      integer(4) :: N, K, Q
      integer(4),allocatable,dimension(:) :: A,num
      integer(4) :: i
      read(*,*) N,K,Q
      allocate(A(N),num(Q))
      num=0
      do i=1,N
        A(i) = 0
      end do
      do i=1,Q
        read(*,*) num(i)
        A(num(i)) = A(num(i)) + 1
      end do
      do i=1,N
        if( (Q-A(i)) < K) then
          write(*,'(A)') 'Yse'
        else
          write(*,'(A)') 'No'
        end if
      end do
      stop
      end program answer

Score: 300 points

Problem Statement

Takahashi has decided to hold fastest-finger-fast quiz games. Kizahashi, who is in charge of making the scoreboard, is struggling to write the program that manages the players' scores in a game, which proceeds as follows.

A game is played by N players, numbered 1 to N. At the beginning of a game, each player has K points.

When a player correctly answers a question, each of the other N-1 players receives minus one (-1) point. There is no other factor that affects the players' scores.

At the end of a game, the players with 0 points or lower are eliminated, and the remaining players survive.

In the last game, the players gave a total of Q correct answers, the i-th of which was given by Player A_i.
For Kizahashi, write a program that determines whether each of the N players survived this game.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq K \leq 10^9

1 \leq Q \leq 10^5

1 \leq A_i \leq N\ (1 \leq i \leq Q)

Input

Input is given from Standard Input in the following format:

N K Q
A_1
A_2
.
.
.
A_Q

Output

Print N lines. The i-th line should contain Yes if Player i survived the game, and No otherwise.

Sample Input 1

6 3 4
3
1
3
2

Sample Output 1

No
No
Yes
No
No
No

In the beginning, the players' scores are (3, 3, 3, 3, 3, 3).

Player 3 correctly answers a question. The players' scores are now (2, 2, 3, 2, 2, 2).

Player 1 correctly answers a question. The players' scores are now (2, 1, 2, 1, 1, 1).

Player 3 correctly answers a question. The players' scores are now (1, 0, 2, 0, 0, 0).

Player 2 correctly answers a question. The players' scores are now (0, 0, 1, -1, -1, -1).

Players 1, 2, 4, 5 and 6, who have 0 points or lower, are eliminated, and Player 3 survives this game.

Sample Input 2

6 5 4
3
1
3
2

Sample Output 2

Yes
Yes
Yes
Yes
Yes
Yes

Sample Input 3

10 13 15
3
1
4
1
5
9
2
6
5
3
5
8
9
7
9

Sample Output 3

No
No
No
No
Yes
No
No
No
Yes
No


      program answer
      implicit none
      integer(4) :: N, K, Q
      integer(4),allocatable,dimension(:) :: A
      integer(4) :: i,num
      read(*,*) N,K,Q
      allocate(A(N))
      num=0
      do i=1,N
        A(i) = 0
      end do
      do i=1,Q
        read(*,*) num
        A(num) = A(num) + 1
        num=0
      end do
      if( N < 2 .or. K < 1 .or. Q < 1 ) stop
      do i=1,N
        if( (Q-A(i)) < K) then
          write(*,'(A)') 'Yse'
        else
          write(*,'(A)') 'No'
        end if
      end do
      stop
      end program answer


program abc140c
  integer :: n, a(100)
  read *, n, a(:n-1)
  print *, sum(merge(a(2:n-1),a(:n-2),a(2:n-1)<a(:n-2)))+a(1)+a(n-1)
end program abc140c

Score : 300 points

Problem Statement

There is an integer sequence A of length N whose values are unknown.

Given is an integer sequence B of length N-1 which is known to satisfy the following:

B_i \geq \max(A_i, A_{i+1})

Find the maximum possible sum of the elements of A.

Constraints

All values in input are integers.

2 \leq N \leq 100

0 \leq B_i \leq 10^5

Input

Input is given from Standard Input in the following format:

N
B_1 B_2 ... B_{N-1}

Output

Print the maximum possible sum of the elements of A.

Sample Input 1

3
2 5

Sample Output 1

9

A can be, for example, ( 2 , 1 , 5 ), ( -1 , -2 , -3 ), or ( 2 , 2 , 5 ). Among those candidates, A = ( 2 , 2 , 5 ) has the maximum possible sum.

Sample Input 2

2
3

Sample Output 2

6

Sample Input 3

6
0 153 10 10 23

Sample Output 3

53


program main
integer,allocatable :: a(:),b(:)
integer :: n,i,ans
read *,n
allocate(a(n),b(n-1))
read *,b

ans = b(1)
do i = 2,n-1
if (b(i)<b(i-1)) then
ans = ans + b(i)
else
ans = ans + b(i-1)
end if
end do
ans = ans + b(n-1)

print *,ans
end program main


program test
implicit none
  integer,allocatable::a(:)
  integer :: N,i,ans
  read*,N
  allocate(a(N))
  read(*,*)(a(i),i=1,N-1)
  ans=a(1)
  do i=1,N-2
  	ans=ans+min(a(i),a(i+1))
  end do
  ans=ans+a(N-1)
 print*,ans
end program


module ABC140

  ! <module>s to import
  use, intrinsic :: iso_fortran_env

  ! require all variables to be explicitly declared
  implicit none

  ! accessibility of <subroutine>s and <function>s in this <module>
  public :: task_C

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

  subroutine task_C

    ! variables for this <subroutine>
    integer(INT64) :: len_sequence_A
    integer(INT64) :: val_sum_sequence_A

    ! arrays for this <subroutine>
    integer(INT64), dimension(:), allocatable :: val_sequence_B

    ! support variables for this <subroutine>
    integer :: itr

    ! STEP.01
    ! read out the given data
    ! (the length of the sequence B)
    read(unit=INPUT_UNIT, fmt=*) len_sequence_A

    ! STEP.02
    ! allocate the arrays to store the given data
    allocate( val_sequence_B(1:len_sequence_A - 1) )

    ! STEP.03
    ! read out the given data
    ! (the values of the sequence B)
    read(unit=INPUT_UNIT, fmt=*) val_sequence_B(:)

    ! STEP.04
    ! calculate the answer of this task
    val_sum_sequence_A &!
    = val_sequence_B(len_sequence_A - 1) &!
    + val_sequence_B(1)

    do itr = len_sequence_A - 1, 2, -1
      val_sum_sequence_A = val_sum_sequence_A + min(val_sequence_B(itr), val_sequence_B(itr - 1))
    end do

    ! STEP.05
    ! output the answer of this task
    write(unit=OUTPUT_UNIT, fmt='(I0)', advance='yes') val_sum_sequence_A

    ! STEP.06
    ! deallocate the arrays to store the given data
    deallocate( val_sequence_B )

    ! STEP.END
    return

  end subroutine task_C

end module ABC140

program main

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

  ! require all variables to be explicitly declared
  implicit none

  call task_C

end program main


program prob6
    implicit none
    integer :: N, K, i, a, b, ans
    character(100005) :: S
    read(*,*) N, K, S

    ans = 0
    do i = 2, N-1
        if(S(i:i) == 'L')then
            if(S(i-1:i-1) == 'L')then
                ans = ans + 1
            end if
        end if
        if(S(i:i) == 'R')then
            if(S(i+1:i+1) == 'R')then
                ans = ans + 1
            end if
        end if
    end do
    if(S(1:1) == 'R')then
        if(S(2:2) == 'R')then
            ans = ans + 1
        end if
    end if
    if(S(N:N) == 'L')then
        if(S(N-1:N-1) == 'L')then
            ans = ans + 1
        end if
    end if

    a = 0
    b = 0
    do i = 1, N-1
        if(S(i:i) == 'L' .and. S(i+1:i+1) == 'R')then
            a = a + 1
        end if
        if(S(i:i) == 'R' .and. S(i+1:i+1) == 'L')then
            b = b + 1
        end if
    end do
    ans = ans + 2 * min(a, min(b, K))
    if(K > min(a, b)) then
        ans = ans + K - min(a, b)
    end if
    ans = min(ans, N-1)

    write(*,*) ans

    stop
contains
end program prob6

Score : 400 points

Problem Statement

There are N people standing in a queue from west to east.

Given is a string S of length N representing the directions of the people.
The i-th person from the west is facing west if the i-th character of S is L, and east if that character of S is R.

A person is happy if the person in front of him/her is facing the same direction.
If no person is standing in front of a person, however, he/she is not happy.

You can perform the following operation any number of times between 0 and K (inclusive):

Operation: Choose integers l and r such that 1 \leq l \leq r \leq N, and rotate by 180 degrees the part of the queue: the l-th, (l+1)-th, ..., r-th persons. That is, for each i = 0, 1, ..., r-l, the (l + i)-th person from the west will stand the (r - i)-th from the west after the operation, facing east if he/she is facing west now, and vice versa.

What is the maximum possible number of happy people you can have?

Constraints

N is an integer satisfying 1 \leq N \leq 10^5.

K is an integer satisfying 1 \leq K \leq 10^5.

|S| = N

Each character of S is L or R.

Input

Input is given from Standard Input in the following format:

N K
S

Output

Print the maximum possible number of happy people after at most K operations.

Sample Input 1

6 1
LRLRRL

Sample Output 1

3

If we choose (l, r) = (2, 5), we have LLLRLL, where the 2-nd, 3-rd, and 6-th persons from the west are happy.

Sample Input 2

13 3
LRRLRLRRLRLLR

Sample Output 2

9

Sample Input 3

10 1
LLLLLRRRRR

Sample Output 3

9

Sample Input 4

9 2
RRRLRLRLL

Sample Output 4

7


program main
    implicit none
    integer:: n, k, i, j, l,r
    integer,allocatable:: cnum(:)
    character(10**5):: s
    integer:: hap
    hap = 0
    read*, n, k
    read*, s

    allocate(cnum(n+2))

    do i = 2, n+1
        if(s(i-1:i-1) == 'L')then
            cnum(i) = -1
        else
            cnum(i) = 1
        endif
    enddo
    cnum(1) = -1
    cnum(n+2) = 1
    ! print*, cnum
    do i = 2, n
        if (cnum(i) == cnum(i+1))then
            hap = hap + 1
            ! print*, i
        endif
    enddo

    l = 0
    r = 0
    do i=1,n+2
        if (cnum(i) == cnum(i+1))cycle
        if(cnum(i) == -1)then
            ! print*, 'l'
            l = l + 1
        endif

        if(cnum(i) ==  1)then
            ! print*, 'r'
            r = r + 1
        endif
    enddo

    print*,hap + min(k,min(l,r))*2

end program


program main
    implicit none
    integer:: n, k, i, j, l,r
    integer,allocatable:: cnum(:)
    character(10**5):: s
    integer:: hap
    hap = 0
    read*, n, k
    read*, s

    allocate(cnum(n+1))

    do i = 1, n
        if(s(i:i) == 'L')then
            cnum(i) = -1
        else
            cnum(i) = 1
        endif
    enddo

    do i = 1,n-1
        if (cnum(i) == cnum(i+1)) hap = hap + 1
    enddo

    cnum(n+1) = cnum(1)*-1
    l = 0
    r = 0
    do i=1,n
        if (k <= 0)exit
        if (cnum(i) == cnum(i+1))cycle
        if(cnum(i) == -1)then
            ! print*, 'l'
            l = l + 1

        endif
        if (k <= 0)exit
        if(cnum(i) ==  1)then
            ! print*, 'r'
            r = r + 1

        endif
    enddo

    print*, hap + min(k,min(l,r))*2

end program


program tenki
    implicit none
    integer :: i, cnt = 0
    character s(3), t(3)
    read *, s(1:3)
    read *, t(1:3)
    do i = 1, 3
        if(s(i) .eq. t(i))then
            cnt = cnt + 1
        end if
    end do
    print *, cnt
end program

Score : 100 points

Problem Statement

You will be given a string S of length 3 representing the weather forecast for three days in the past.

The i-th character (1 \leq i \leq 3) of S represents the forecast for the i-th day. S, C, and R stand for sunny, cloudy, and rainy, respectively.

You will also be given a string T of length 3 representing the actual weather on those three days.

The i-th character (1 \leq i \leq 3) of S represents the actual weather on the i-th day. S, C, and R stand for sunny, cloudy, and rainy, respectively.

Print the number of days for which the forecast was correct.

Constraints

S and T are strings of length 3 each.

S and T consist of S, C, and R.

Input

Input is given from Standard Input in the following format:

S
T

Output

Print the number of days for which the forecast was correct.

Sample Input 1

CSS
CSR

Sample Output 1

2

For the first day, it was forecast to be cloudy, and it was indeed cloudy.

For the second day, it was forecast to be sunny, and it was indeed sunny.

For the third day, it was forecast to be sunny, but it was rainy.

Thus, the forecast was correct for two days in this case.

Sample Input 2

SSR
SSR

Sample Output 2

3

Sample Input 3

RRR
SSS

Sample Output 3

0


implicit none
integer :: i, j
character(3) :: s, t

read(*,*) s
read(*,*) t

j=0

do i = 1, 3
if (s(i:i)==t(i:i)) then
j = j + 1
end if
end do

write(*,'(i0)') j

end program


program sample
    implicit none
    real(8)::x,y,z
    integer(8) :: a,b,c,i,j,m,n,k,a1,b1,c1

    read(*,*) a,b
    n=0
    do while (a*n-n+1<b)
        n=n+1
    end do

    write(*,*) n

    stop
end program sample

Score : 200 points

Problem Statement

Takahashi's house has only one socket.

Takahashi wants to extend it with some number of power strips, each with A sockets, into B or more empty sockets.

One power strip with A sockets can extend one empty socket into A empty sockets.

Find the minimum number of power strips required.

Constraints

All values in input are integers.

2 \leq A \leq 20

1 \leq B \leq 20

Input

Input is given from Standard Input in the following format:

A B

Output

Print the minimum number of power strips required.

Sample Input 1

4 10

Sample Output 1

3

3 power strips, each with 4 sockets, extend the socket into 10 empty sockets.

Sample Input 2

8 9

Sample Output 2

2

2 power strips, each with 8 sockets, extend the socket into 15 empty sockets.

Sample Input 3

8 8

Sample Output 3

1


module ABC139

  ! <module>s to import
  use, intrinsic :: iso_fortran_env

  ! require all variables to be explicitly declared
  implicit none

  ! accessibility of <subroutine>s and <function>s in this <module>
  public :: task_B

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

  subroutine task_B

    ! variables for this <subroutine>
    integer(INT32) :: num_strip_needed
    integer(INT32) :: num_socket_strip
    integer(INT32) :: num_socket_needed
    integer(INT32) :: num_socket_total

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

    ! STEP.01
    ! read out the given data
    read(unit=INPUT_UNIT, fmt=*) num_socket_strip, num_socket_needed

    ! STEP.02
    ! initialize the variables for this <subroutine>
    num_strip_needed = 0_INT32
    num_socket_total = 1_INT32

    ! STEP.03
    ! calculate the answer of this task
    do while (num_socket_total .lt. num_socket_needed)
      num_strip_needed = num_strip_needed + 1_INT32
      num_socket_total = num_socket_total + num_socket_strip - 1_INT32
    end do

    ! STEP.03
    ! output the answer of this task
    write(unit=OUTPUT_UNIT, fmt='(I0)', advance='yes') num_strip_needed

    ! STEP.END
    return

  end subroutine task_B

end module ABC139

program main

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

  ! require all variables to be explicitly declared
  implicit none

  call task_B

end program main


program answer
  implicit none
  integer :: N, i, j, count, a, b
  integer, allocatable :: H(:), s(:), d(:)
  read(*,*) N
  allocate(H(N))
  read(*,*) H
  allocate(s(N))

 s(:)=0
  count=0
  do i = 1, n-1
     if(H(i)<H(i+1)) then
        count=count+1
        s(count)=i
     end if
  end do

  if(count==1) then
     write(*,*) max(s(1)-2, n-s(1)-2)
     stop
  else if(count==n-1) then
     write(*,*) 0
     stop
  else if(count==0) then
     write(*,*) n-1
     stop
  end if
  allocate(d(count-1))
  do i = 1, count-1
     d(i)=s(i+1)-s(i)-1
  end do

  write(*,*) max(maxval(d), s(1)-2, n-s(count)-2)

  deallocate(d)
  deallocate(s)
  deallocate(H)
  stop
 end program answer

Score : 300 points

Problem Statement

There are N squares arranged in a row from left to right.

The height of the i-th square from the left is H_i.

You will land on a square of your choice, then repeat moving to the adjacent square on the right as long as the height of the next square is not greater than that of the current square.

Find the maximum number of times you can move.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq H_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
H_1 H_2 ... H_N

Output

Print the maximum number of times you can move.

Sample Input 1

5
10 4 8 7 3

Sample Output 1

2

By landing on the third square from the left, you can move to the right twice.

Sample Input 2

7
4 4 5 6 6 5 5

Sample Output 2

3

By landing on the fourth square from the left, you can move to the right three times.

Sample Input 3

4
1 2 3 4

Sample Output 3

0


program main
  implicit none

  integer ::i,j,n,h(100000)=0,k,he=0
  read(*,*)n
  read(*,*)(h(i),i= 1,n)

  do i = 1, n-1
    k = 0
    do j = i, n-1
      if (h(j) >= h(j+1)) then
        k = k + 1
      else
        exit
      end if
    end do
    if (k > he) then
      he = k
    end if
    if (he >= (n-i-1)) then
      exit
    end if
  end do

  write(*,'(i0)')he
end program main


program main
integer :: i,n,cnt=0,big=0
integer,allocatable :: h(:)

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

do i = 1,n-1
cnt = 0
if (big >= n/2) exit
do j = i,n-1
if (h(j)>=h(j+1)) then
cnt = cnt + 1
else
exit
end if
end do
big = max(cnt,big)
end do

print *,big

end program


module ABC139

  ! <module>s to import
  use, intrinsic :: iso_fortran_env

  ! require all variables to be explicitly declared
  implicit none

  ! accessibility of <subroutine>s and <function>s in this <module>
  public :: task_C

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

  subroutine task_C

    ! variables for this <subroutine>
    integer(INT64) :: len_sequence

    ! STEP.01
    ! read out the given data (the number of squares)
    read(unit=INPUT_UNIT, fmt=*) len_sequence

    ! STEP.02
    ! output the answer of this task
    write(unit=OUTPUT_UNIT, fmt='(I0)', advance='yes') (len_sequence - 1_INT64) * len_sequence / 2_INT64

    ! STEP.END
    return

  end subroutine task_C

end module ABC139

program main

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

  ! require all variables to be explicitly declared
  implicit none

  call task_C

end program main

Score : 400 points

Problem Statement

For an integer N, we will choose a permutation \{P_1, P_2, ..., P_N\} of \{1, 2, ..., N\}.

Then, for each i=1,2,...,N, let M_i be the remainder when i is divided by P_i.

Find the maximum possible value of M_1 + M_2 + \cdots + M_N.

Constraints

N is an integer satisfying 1 \leq N \leq 10^9.

Input

Input is given from Standard Input in the following format:

N

Output

Print the maximum possible value of M_1 + M_2 + \cdots + M_N.

Sample Input 1

2

Sample Output 1

1

When the permutation \{P_1, P_2\} = \{2, 1\} is chosen, M_1 + M_2 = 1 + 0 = 1.

Sample Input 2

13

Sample Output 2

78

Sample Input 3

1

Sample Output 3

0


program test
implicit none
integer(8)::N
read*,N
print*,N*(N-1)/2
end program


program league
  implicit none
  integer :: n, a(1000,1000) = 0, i, j, l, k(1000) = 1, m = 0
  logical :: used(1000) = .false., change
  read(*,*) n
  do i = 1, n
    read(*,*) a(i,1:n-1)
  end do
  do while (any(k(1:n) < n))
    m = m+1
    used(1:n) = k(1:n) == n
    change = .false.
    do i = 1, n
      if (used(i)) cycle
      j = a(i,k(i))
      if (used(j)) cycle
      if (a(j,k(j)) == i) then
        k(i) = k(i)+1
        k(j) = k(j)+1
        used(i) = .true.
        used(j) = .true.
        change = .true.
      end if
    end do
    if (.not.change) then
      m = -1
      exit
    end if
  end do
  write(*,'(i0)') m
end program league

Score : 500 points

Problem Statement

N players will participate in a tennis tournament. We will call them Player 1, Player 2, \ldots, Player N.

The tournament is round-robin format, and there will be N(N-1)/2 matches in total.
Is it possible to schedule these matches so that all of the following conditions are satisfied? If the answer is yes, also find the minimum number of days required.

Each player plays at most one matches in a day.

Each player i (1 \leq i \leq N) plays one match against Player A_{i, 1}, A_{i, 2}, \ldots, A_{i, N-1} in this order.

Constraints

3 \leq N \leq 1000

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

A_{i, j} \neq i

A_{i, 1}, A_{i, 2}, \ldots, A_{i, N-1} are all different.

Input

Input is given from Standard Input in the following format:

N
A_{1, 1} A_{1, 2} \ldots A_{1, N-1}
A_{2, 1} A_{2, 2} \ldots A_{2, N-1}
:
A_{N, 1} A_{N, 2} \ldots A_{N, N-1}

Output

If it is possible to schedule all the matches so that all of the conditions are satisfied, print the minimum number of days required; if it is impossible, print -1.

Sample Input 1

3
2 3
1 3
1 2

Sample Output 1

3

All the conditions can be satisfied if the matches are scheduled for three days as follows:

Day 1: Player 1 vs Player 2

Day 2: Player 1 vs Player 3

Day 3: Player 2 vs Player 3

This is the minimum number of days required.

Sample Input 2

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

Sample Output 2

4

All the conditions can be satisfied if the matches are scheduled for four days as follows:

Day 1: Player 1 vs Player 2, Player 3 vs Player 4

Day 2: Player 1 vs Player 3

Day 3: Player 1 vs Player 4, Player 2 vs Player 3

Day 4: Player 2 vs Player 4

This is the minimum number of days required.

Sample Input 3

3
2 3
3 1
1 2

Sample Output 3

-1

Any scheduling of the matches violates some condition.


program main
  implicit none
  integer i,N,fin,pin,ans,j,con
  integer,allocatable :: A(:,:),B(:),tired(:)
  read(*, *) N
  allocate(A(N,N))
  allocate(B(N))
  allocate(tired(N))
  do j= 1,N
    read(*, *) (A(j,i), i = 1,N-1)
    A(j,N) = 0
  end do
  B = 1
  tired = 0
  pin = 0
  fin = 1
  ans = 1
  do
    do i = 1,N
      con = A(i,B(i))
      if (con > 0) then
        if (A(con,B(con)) == i .and. tired(con) == 0 .and. tired(i) == 0) then
          B(i) = B(i) + 1
          B(con) = B(con) + 1
          tired(con) = 1
          tired(i) = 1
          pin = 1
        end if
      end if
    end do
    if (pin == 0) then
      ans = -1
      exit
    elseif (sum(B) == N**2) then
      exit
    else
      ans = ans + 1
      tired = 0
      pin = 0
    end if
  end do

  write(*, *) ans
end program main


integer N
integer,allocatable,dimension(:,:)::A
integer,allocatable,dimension(:)::B,Anum
integer aite
logical nogame
read*,N
allocate(A(N,N-1),B(N),Anum(N))
Anum=1
do i=1,N
  read*,A(i,:)
end do
do i=1,N*N
  nogame=.true.
  B=0
  do j=1,N
    if(B(j)==0.and.Anum(J)/=N)then
      aite=A(J,Anum(J))
      if(A(aite,Anum(aite))==J.and.B(aite)==0)then
        Anum(j)=Anum(j)+1
        Anum(aite)=Anum(aite)+1
        nogame=.false.
        B(j)=1;B(aite)=1
      endif
    endif
  end do
  if(minval(Anum)==N)then
    print"(i0)",i
    stop
  endif
  if(nogame)then
    print"(i0)",-1
    stop
  endif
end do
end


module ABC139

  ! <module>s to import
  use, intrinsic :: iso_fortran_env

  ! require all variables to be explicitly declared
  implicit none

  type coordinate
    real(REAL64), public :: x
    real(REAL64), public :: y
    real(REAL64), public :: distance
  end type coordinate

  ! accessibility of <subroutine>s and <function>s in this <module>
  public :: task_F

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

  subroutine task_F

    ! variables for this <subroutine>
    integer(INT32)   :: num_engines
    real(REAL64)     :: x_max, x_read, x_trial
    real(REAL64)     :: y_max, y_read, y_trial
    real(REAL64)     :: distance_max
    real(REAL64)     :: distance_trial

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

    ! STEP.01
    ! read out the given data (the number of engines)
    read(unit=INPUT_UNIT, fmt=*) num_engines

    ! STEP.02
    ! initialize the variables for this <subroutine>
    distance_max = 0.0_REAL64
    x_max        = 0.0_REAL64
    y_max        = 0.0_REAL64
    x_trial      = 0.0_REAL64
    y_trial      = 0.0_REAL64

    ! STEP.03
    ! calculate the answer of this task
    do itr = 1, num_engines, 1

      ! read out the given data (the movement by the each engine)
      read(unit=INPUT_UNIT, fmt=*) x_read, y_read

      x_trial        = x_trial + x_read
      y_trial        = y_trial + y_read
      distance_trial = sqrt(x_trial * x_trial + y_trial * y_trial)

      if (distance_trial .gt. distance_max) then
        x_max        = x_trial
        y_max        = y_trial
        distance_max = distance_trial
      end if

    end do

    ! STEP.04
    ! output the answer of this task
    write(unit=OUTPUT_UNIT, fmt='(ES23.15e3)', advance='yes') distance_max

    ! STEP.END
    return

  end subroutine task_F

end module ABC139

program main

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

  ! require all variables to be explicitly declared
  implicit none

  call task_F

end program main

Score: 600 points

Problem Statement

E869120 is initially standing at the origin (0, 0) in a two-dimensional plane.

He has N engines, which can be used as follows:

When E869120 uses the i-th engine, his X- and Y-coordinate change by x_i and y_i, respectively. In other words, if E869120 uses the i-th engine from coordinates (X, Y), he will move to the coordinates (X + x_i, Y + y_i).

E869120 can use these engines in any order, but each engine can be used at most once. He may also choose not to use some of the engines.

He wants to go as far as possible from the origin.
Let (X, Y) be his final coordinates. Find the maximum possible value of \sqrt{X^2 + Y^2}, the distance from the origin.

Constraints

1 \leq N \leq 100

-1 \ 000 \ 000 \leq x_i \leq 1 \ 000 \ 000

-1 \ 000 \ 000 \leq y_i \leq 1 \ 000 \ 000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
x_2 y_2
: :
x_N y_N

Output

Print the maximum possible final distance from the origin, as a real value.
Your output is considered correct when the relative or absolute error from the true answer is at most 10^{-10}.

Sample Input 1

3
0 10
5 -5
-5 -5

Sample Output 1

10.000000000000000000000000000000000000000000000000

The final distance from the origin can be 10 if we use the engines in one of the following three ways:

Use Engine 1 to move to (0, 10).

Use Engine 2 to move to (5, -5), and then use Engine 3 to move to (0, -10).

Use Engine 3 to move to (-5, -5), and then use Engine 2 to move to (0, -10).

The distance cannot be greater than 10, so the maximum possible distance is 10.

Sample Input 2

5
1 1
1 0
0 1
-1 0
0 -1

Sample Output 2

2.828427124746190097603377448419396157139343750753

The maximum possible final distance is 2 \sqrt{2} = 2.82842....
One of the ways to achieve it is:

Use Engine 1 to move to (1, 1), and then use Engine 2 to move to (2, 1), and finally use Engine 3 to move to (2, 2).

Sample Input 3

5
1 1
2 2
3 3
4 4
5 5

Sample Output 3

21.213203435596425732025330863145471178545078130654

If we use all the engines in the order 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5, we will end up at (15, 15), with the distance 15 \sqrt{2} = 21.2132... from the origin.

Sample Input 4

3
0 0
0 1
1 0

Sample Output 4

1.414213562373095048801688724209698078569671875376

There can be useless engines with (x_i, y_i) = (0, 0).

Sample Input 5

1
90447 91000

Sample Output 5

128303.000000000000000000000000000000000000000000000000

Note that there can be only one engine.

Sample Input 6

2
96000 -72000
-72000 54000

Sample Output 6

120000.000000000000000000000000000000000000000000000000

There can be only two engines, too.

Sample Input 7

10
1 2
3 4
5 6
7 8
9 10
11 12
13 14
15 16
17 18
19 20

Sample Output 7

148.660687473185055226120082139313966514489855137208


program kadai
   implicit none
    integer  ::  n,k
    integer,allocatable  ::  a(:)
    integer  ::  i,j
    integer(8)  ::  big,eq
    integer(8)  ::  num1,num2,num
    integer(8)  ::  small
    integer(8)  ::  kari

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

    num=0
    big=0
    small=0
    kari=0
    eq=0

    do i=1,n-1
        do j=i+1,n
            if (a(i)>a(j)) then
                big=big+1
            end if
            if (a(i)<a(j)) then
                small=small+1
            end if
            if (a(i)==a(j)) then
                eq=eq+1
            end if
        end do
    end do

    if (big+small+eq/=(n*(n-1)/2)) then
        do
        write(*,*) big+small+eq,n*(n-1)/2
        end do
    end if

    !write(*,*) big,small
    big=mod(big,1000000007)
    small=mod(small,1000000007)

    kari=dble(k-1)*dble(k)/2
    kari=mod(kari,1000000007)
    !write(*,*) kari

    num1=dble(kari)*dble(small)
    num1=mod(num1,1000000007)
    kari=mod(kari+k,1000000007)
    num2=dble(kari)*dble(big)
   ! write(*,*) num1,num2
    num2=mod(num2,1000000007)
   ! write(*,*) num2
    num=mod(num1+num2,1000000007)

    write(*,*) num

stop
contains

end program kadai

Score : 300 points

Problem Statement

We have a sequence of N integers A~=~A_0,~A_1,~...,~A_{N - 1}.

Let B be a sequence of K \times N integers obtained by concatenating K copies of A. For example, if A~=~1,~3,~2 and K~=~2, B~=~1,~3,~2,~1,~3,~2.

Find the inversion number of B, modulo 10^9 + 7.

Here the inversion number of B is defined as the number of ordered pairs of integers (i,~j)~(0 \leq i < j \leq K \times N - 1) such that B_i > B_j.

Constraints

All values in input are integers.

1 \leq N \leq 2000

1 \leq K \leq 10^9

1 \leq A_i \leq 2000

Input

Input is given from Standard Input in the following format:

N K
A_0 A_1 ... A_{N - 1}

Output

Print the inversion number of B, modulo 10^9 + 7.

Sample Input 1

2 2
2 1

Sample Output 1

3

In this case, B~=~2,~1,~2,~1. We have:

B_0 > B_1

B_0 > B_3

B_2 > B_3

Thus, the inversion number of B is 3.

Sample Input 2

3 5
1 1 1

Sample Output 2

0

A may contain multiple occurrences of the same number.

Sample Input 3

10 998244353
10 9 8 7 5 6 3 4 2 1

Sample Output 3

185297239

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


program kadai
   implicit none
    integer  ::  n,k
    integer,allocatable  ::  a(:)
    integer  ::  i,j
    integer(8)  ::  big
    integer(8)  ::  num1,num2,num
    integer(8)  ::  small
    integer(8)  ::  kari

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

    num=0
    big=0
    small=0
    kari=0

    do i=1,n-1
        do j=i+1,n
            if (a(i)>a(j)) then
                big=big+1
            end if
            if (a(i)<a(j)) then
                small=small+1
            end if
        end do
    end do

    !write(*,*) big,small
    big=mod(big,1000000007)
    small=mod(small,1000000007)

    kari=dble(k-1)*dble(k)/2
    kari=mod(kari,1000000007)
    !write(*,*) kari

    num1=dble(kari)*dble(small)
    num1=mod(num1,1000000007)
    kari=mod(kari+k,1000000007)
    num2=dble(kari)*dble(big)
   ! write(*,*) num1,num2
    num2=mod(num2,1000000007)
   ! write(*,*) num2
    num=mod(num1+num2,1000000007)

    write(*,*) num

stop
contains

end program kadai


program kadai
   implicit none
    integer  ::  n,k
    integer,allocatable  ::  a(:)
    integer  ::  i,j
    integer(8)  ::  big
    integer(8)  ::  num1,num2,num
    integer(8)  ::  small
    integer(8)  ::  kari

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

    num=0
    big=0
    small=0
    kari=0

    do i=1,n-1
        do j=i+1,n
            if (a(i)>a(j)) then
                big=big+1
            end if
            if (a(i)<a(j)) then
                small=small+1
            end if
        end do
    end do

    !write(*,*) big,small

    kari=dble(k-1)*dble(k)/2
    kari=mod(kari,1000000007)
    !write(*,*) kari

    num1=dble(kari)*dble(small)
    num1=mod(num1,1000000007)
    kari=mod(kari+k,1000000007)
    num2=dble(kari)*dble(big)
   ! write(*,*) num1,num2
    num2=mod(num2,1000000007)
   ! write(*,*) num2
    num=mod(num1+num2,1000000007)

    write(*,*) num

stop
contains

end program kadai


integer(16) N,K,i,j
integer(16),allocatable,dimension(:)::A,B,C
integer(16) cnt
integer(16) ans
read*,N,K
allocate(A(N),B(N),C(N))
read*,A
do i=1,N
  cnt=0
  do j=i+1,N
    if(A(i)>A(j))cnt=cnt+1
  end do
  B(i)=cnt
end do
do i=1,N
  cnt=0
  do j=1,N
    if(A(i)>A(j))cnt=cnt+1
  end do
  C(i)=cnt
end do
ans=0
do i=1,N
  ans=mod(ans+B(i)*K,10**9+7)
  ans=mod(ans+C(i)*((K-1)*K)/2,10**9+7)
end do
print"(i0)",ans
end


integer(8) N,K,i
integer(8),allocatable,dimension(:)::A,B,C
integer(8) cnt
integer(8) ans
read*,N,K
allocate(A(N),B(N),C(N))
read*,A
do i=1,N
  cnt=0
  do j=i+1,N
    if(A(i)>A(j))cnt=cnt+1
  end do
  B(i)=cnt
end do
do i=1,N
  cnt=0
  do j=1,N
    if(A(i)>A(j))cnt=cnt+1
  end do
  C(i)=cnt
end do
ans=0
do i=1,N
  ans=mod(ans+B(i)*K,10**9+7)
  ans=mod(ans+C(i)*((K-1)*K)/2,10**9+7)
end do
print"(i0)",ans
end


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

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

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

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

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

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

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

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

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

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

  interface inv
    module procedure :: invm
  end interface inv

contains

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

end module mod_modint

program cell_inversion
  use mod_modint
  implicit none
  integer :: n, a(200000) = 0, i, l(0:200001) = 0, r(0:200001) = 0
  character(200000) :: s
  type(modint) :: ans
  read(*,*) n
  read(*,*) s
  do i = 1, 2*n
    if (s(i:i) == "B") a(i) = 1
    if (mod(i-1,2) == a(i)) then
      l(i) = l(i)+1
    else
      r(i) = r(i)+1
    end if
  end do
  do i = 1, 2*n
    r(i) = r(i)+r(i-1)
  end do
  do i = 2*n, 1, -1
    l(i) = l(i)+l(i+1)
  end do
  ans = 1
  do i = 1, 2*n
    if (mod(i-1,2) == a(i)) then
      ans = ans*r(i)
    else
      ans = ans*l(i)
    end if
  end do
  ans = ans/modint(2)**(2*n-2)
  write(*,'(i0)') modulo(ans%get(),modulus)
end program cell_inversion

Score : 500 points

Problem Statement

There are 2N squares arranged from left to right. You are given a string of length 2N representing the color of each of the squares.

The color of the i-th square from the left is black if the i-th character of S is B, and white if that character is W.

You will perform the following operation exactly N times: choose two distinct squares, then invert the colors of these squares and the squares between them. Here, to invert the color of a square is to make it white if it is black, and vice versa.

Throughout this process, you cannot choose the same square twice or more. That is, each square has to be chosen exactly once.

Find the number of ways to make all the squares white at the end of the process, modulo 10^9+7.

Two ways to make the squares white are considered different if and only if there exists i (1 \leq i \leq N) such that the pair of the squares chosen in the i-th operation is different.

Constraints

1 \leq N \leq 10^5

|S| = 2N

Each character of S is B or W.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the number of ways to make all the squares white at the end of the process, modulo 10^9+7. If there are no such ways, print 0.

Sample Input 1

2
BWWB

Sample Output 1

4

There are four ways to make all the squares white, as follows:

Choose Squares 1, 3 in the first operation, and choose Squares 2, 4 in the second operation.

Choose Squares 2, 4 in the first operation, and choose Squares 1, 3 in the second operation.

Choose Squares 1, 4 in the first operation, and choose Squares 2, 3 in the second operation.

Choose Squares 2, 3 in the first operation, and choose Squares 1, 4 in the second operation.

Sample Input 2

4
BWBBWWWB

Sample Output 2

288

Sample Input 3

5
WWWWWWWWWW

Sample Output 3

0


program bbb

implicit none
integer :: n, i
real(8),allocatable,dimension(:) :: a

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

do i=1,n
a(i) = 1/a(i)
end do

write(*,'(f0.14)') 1/sum(a)

end program

Score : 200 points

Problem Statement

Given is a sequence of N integers A_1, \ldots, A_N.

Find the (multiplicative) inverse of the sum of the inverses of these numbers, \frac{1}{\frac{1}{A_1} + \ldots + \frac{1}{A_N}}.

Constraints

1 \leq N \leq 100

1 \leq A_i \leq 1000

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \ldots A_N

Output

Print a decimal number (or an integer) representing the value of \frac{1}{\frac{1}{A_1} + \ldots + \frac{1}{A_N}}.

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

Sample Input 1

2
10 30

Sample Output 1

7.5

\frac{1}{\frac{1}{10} + \frac{1}{30}} = \frac{1}{\frac{4}{30}} = \frac{30}{4} = 7.5.

Printing 7.50001, 7.49999, and so on will also be accepted.

Sample Input 2

3
200 200 200

Sample Output 2

66.66666666666667

\frac{1}{\frac{1}{200} + \frac{1}{200} + \frac{1}{200}} = \frac{1}{\frac{3}{200}} = \frac{200}{3} = 66.6666....

Printing 6.66666e+1 and so on will also be accepted.

Sample Input 3

1
1000

Sample Output 3

1000

\frac{1}{\frac{1}{1000}} = 1000.

Printing +1000.0 and so on will also be accepted.


program main
  implicit none

  integer :: n, a(100),i
  real :: s = 0.0

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

  do i = 1, n
    s = s + 1.d0/real(a(i))
  end do
  write(*,*)s

end program main


program ccc

implicit none
integer :: n, i
real(8) :: res
real(8),allocatable, dimension(:) :: v

read*, n
allocate(v(n))
read*, v

call heapsort(n,v)

v(1) = v(1)/((2.0D0)**(n-1))
res= v(1)

do i=2,n
v(i) = v(i)/((2.0D0)**(n-i+1))
res = res + v(i)
end do

write(*,'(f0.14)') res

end program

subroutine heapsort(n,array)
  implicit none
  integer,intent(in) :: n
  double precision,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

Score : 300 points

Problem Statement

You have a pot and N ingredients. Each ingredient has a real number parameter called value, and the value of the i-th ingredient (1 \leq i \leq N) is v_i.

When you put two ingredients in the pot, they will vanish and result in the formation of a new ingredient. The value of the new ingredient will be (x + y) / 2 where x and y are the values of the ingredients consumed, and you can put this ingredient again in the pot.

After you compose ingredients in this way N-1 times, you will end up with one ingredient. Find the maximum possible value of this ingredient.

Constraints

2 \leq N \leq 50

1 \leq v_i \leq 1000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
v_1 v_2 \ldots v_N

Output

Print a decimal number (or an integer) representing the maximum possible value of the last ingredient remaining.

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

Sample Input 1

2
3 4

Sample Output 1

3.5

If you start with two ingredients, the only choice is to put both of them in the pot. The value of the ingredient resulting from the ingredients of values 3 and 4 is (3 + 4) / 2 = 3.5.

Printing 3.50001, 3.49999, and so on will also be accepted.

Sample Input 2

3
500 300 200

Sample Output 2

375

You start with three ingredients this time, and you can choose what to use in the first composition. There are three possible choices:

Use the ingredients of values 500 and 300 to produce an ingredient of value (500 + 300) / 2 = 400. The next composition will use this ingredient and the ingredient of value 200, resulting in an ingredient of value (400 + 200) / 2 = 300.

Use the ingredients of values 500 and 200 to produce an ingredient of value (500 + 200) / 2 = 350. The next composition will use this ingredient and the ingredient of value 300, resulting in an ingredient of value (350 + 300) / 2 = 325.

Use the ingredients of values 300 and 200 to produce an ingredient of value (300 + 200) / 2 = 250. The next composition will use this ingredient and the ingredient of value 500, resulting in an ingredient of value (250 + 500) / 2 = 375.

Thus, the maximum possible value of the last ingredient remaining is 375.

Printing 375.0 and so on will also be accepted.

Sample Input 3

5
138 138 138 138 138

Sample Output 3

138


program main
  implicit none
  integer(8) i,ns,nt,judge,ans,j,c2
  character(10**5) s,t
  character c1
  read(*, *)s, t
  ns = len(trim(s))
  nt = len(trim(t))
  ans = 1
  c2 = 1
  judge = 1
  do i = 1,nt
    c1 = t(i:i)
    do j = 1,ns+1
      ans = ans + 1
      c2 = c2 + 1
      if (c2 == ns + 1) then
        c2 = 1
      end if
      if (j == ns + 1) then
        judge = 0
        exit
      elseif (s(c2:c2) == c1) then
        exit
      end if
    end do
    if (judge == 0) then
      exit
    end if
  end do
  !read(*, *) (L(i), i = 1,N)
  if (judge == 0) then
    write(*, *) -1
  else
    write(*, *) ans
  end if
end program main

Score : 500 points

Problem Statement

Given are two strings s and t consisting of lowercase English letters. Determine if there exists an integer i satisfying the following condition, and find the minimum such i if it exists.

Let s' be the concatenation of 10^{100} copies of s. t is a subsequence of the string {s'}_1{s'}_2\ldots{s'}_i (the first i characters in s').

Notes

A subsequence of a string a is a string obtained by deleting zero or more characters from a and concatenating the remaining characters without changing the relative order. For example, the subsequences of contest include net, c, and contest.

Constraints

1 \leq |s| \leq 10^5

1 \leq |t| \leq 10^5

s and t consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s
t

Output

If there exists an integer i satisfying the following condition, print the minimum such i; otherwise, print -1.

Sample Input 1

contest
son

Sample Output 1

10

t = son is a subsequence of the string contestcon (the first 10 characters in s' = contestcontestcontest...), so i = 10 satisfies the condition.

On the other hand, t is not a subsequence of the string contestco (the first 9 characters in s'), so i = 9 does not satisfy the condition.

Similarly, any integer less than 9 does not satisfy the condition, either. Thus, the minimum integer i satisfying the condition is 10.

Sample Input 2

contest
programming

Sample Output 2

-1

t = programming is not a substring of s' = contestcontestcontest.... Thus, there is no integer i satisfying the condition.

Sample Input 3

contest
sentence

Sample Output 3

33

Note that the answer may not fit into a 32-bit integer type, though we cannot put such a case here.


program prob2
    implicit none
    integer::i, l, cnt, k, j
    character(len=200005)::S
    character(len=2)::a
    read(*,*) S
    l = len_trim(S)

    a = S(1:1)
    cnt = 1
    i = 2
    do while(i <= l)
        k = len_trim(a)
        if(k > 1) then
            a = S(i:i)
            i = i+1
            cnt = cnt + 1
        else
            if(S(i:i) .ne. a(1:1)) then
                a = S(i:i)
                i = i+1
            else if(i /= l) then
                a = S(i:i+1)
                i = i+2
            else
                exit
            end if
            cnt = cnt + 1
        end if
    end do

    write(*,*) cnt
    stop
end program prob2

Score : 300 points

Problem Statement

Given is a string S consisting of lowercase English letters. Find the maximum positive integer K that satisfies the following condition:

There exists a partition of S into K non-empty strings S=S_1S_2...S_K such that S_i \neq S_{i+1} (1 \leq i \leq K-1).

Here S_1S_2...S_K represents the concatenation of S_1,S_2,...,S_K in this order.

Constraints

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

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

Print the maximum positive integer K that satisfies the condition.

Sample Input 1

aabbaa

Sample Output 1

4

We can, for example, divide S into four strings aa, b, ba, and a.

Sample Input 2

aaaccacabaababc

Sample Output 2

12


module ABC137

  ! <module>s to import
  use, intrinsic :: iso_fortran_env

  ! require all variables to be explicitly declared
  implicit none

  ! accessibility of <subroutine>s and <function>s in this <module>
  public :: task_A

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

  subroutine task_A (A, B)

    ! arguments for this <subroutine>
    integer(INT16), intent(in) :: A, B

    ! STEP.01
    ! calculate & output the answer of this task
    write(unit=OUTPUT_UNIT, fmt='(I0)', advance='yes') maxval( [A + B, A - B, A * B] )

    ! STEP.END
    return

  end subroutine task_A

end module ABC137

program main

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

  ! require all variables to be explicitly declared
  implicit none

  ! variables for this <program>
  integer(INT16) :: A, B

  ! STEP.01
  ! read out the given data
  read(unit=INPUT_UNIT, fmt=*) A, B

  ! STEP.02
  ! calculate & output the answer of this task
  call task_A (A, B)

end program main

Score : 100 points

Problem Statement

We have two integers: A and B.

Print the largest number among A + B, A - B, and A \times B.

Constraints

All values in input are integers.

-100 \leq A,\ B \leq 100

Input

Input is given from Standard Input in the following format:

A B

Output

Print the largest number among A + B, A - B, and A \times B.

Sample Input 1

-13 3

Sample Output 1

-10

The largest number among A + B = -10, A - B = -16, and A \times B = -39 is -10.

Sample Input 2

1 -33

Sample Output 2

34

The largest number among A + B = -32, A - B = 34, and A \times B = -33 is 34.

Sample Input 3

13 3

Sample Output 3

39

The largest number among A + B = 16, A - B = 10, and A \times B = 39 is 39.


program abc136a
   implicit none
   integer :: a, b

   read(*,*) a, b
   write(*,*) max( a+b, a-b, a*b)

end program


program kadai
implicit none
integer  ::  k,x
integer  ::  i,j,l,n
integer,allocatable  ::  ans(:)

read(*,*) k,x

i=x-k+1
j=x+k-1

allocate(ans(2*k-1))
n=1
do l=i,j
    ans(n)=l
    n=n+1
end do

write(*,*) ans
stop

end program kadai

Score : 200 points

Problem Statement

There are 2000001 stones placed on a number line. The coordinates of these stones are -1000000, -999999, -999998, \ldots, 999999, 1000000.

Among them, some K consecutive stones are painted black, and the others are painted white.

Additionally, we know that the stone at coordinate X is painted black.

Print all coordinates that potentially contain a stone painted black, in ascending order.

Constraints

1 \leq K \leq 100

0 \leq X \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

K X

Output

Print all coordinates that potentially contain a stone painted black, in ascending order, with spaces in between.

Sample Input 1

3 7

Sample Output 1

5 6 7 8 9

We know that there are three stones painted black, and the stone at coordinate 7 is painted black. There are three possible cases:

The three stones painted black are placed at coordinates 5, 6, and 7.

The three stones painted black are placed at coordinates 6, 7, and 8.

The three stones painted black are placed at coordinates 7, 8, and 9.

Thus, five coordinates potentially contain a stone painted black: 5, 6, 7, 8, and 9.

Sample Input 2

4 0

Sample Output 2

-3 -2 -1 0 1 2 3

Negative coordinates can also contain a stone painted black.

Sample Input 3

1 100

Sample Output 3

100


program prob4
    implicit none
    integer::i, j, l, n, tmp
    integer(8)::ans
    integer, dimension(10)::c
    integer, allocatable::p(:,:)
    character(len=10)::S
    read(*,*) n
    allocate(p(n,10))
    do i = 1, n
        read(*,*) S
        c = 0
        do j = 1,10
            c(j) = ichar(S(j:j)) - ichar('a') + 1
        end do
        call heapsort(10,c)
        p(i,:) = c
    end do
    call lsort(n,p)
    ans = 0
    tmp = 0
    do i = 2,n
        do j = 1, 10
            if(p(i,j) .ne. p(i-1,j)) then
                tmp = 0
                exit
            else if(j == 10) then
                tmp = tmp + 1
                ans = ans + tmp
            end if
        end do
    end do
    write(*,*) ans
    stop
contains
    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

    subroutine lsort(n,array)
        implicit none
        integer,intent(in) :: n
        integer,intent(inout) :: array(n,10)

        integer ::i,k,j,l,m,mode
        integer::t(10)

        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
                    do m = 1, 10
                        if(array(j,m).lt.array(j+1,m))j=j+1
                        exit
                    end do
                endif
                mode = 0
                do m = 1, 10
                    if(t(m).lt.array(j,m)) then
                        mode = 1
                        exit
                    end if
                end do
                if (mode==1)then
                    array(i,:)=array(j,:)
                    i=j
                    j=j+j
                else
                    j=k+1
                endif
            enddo
            array(i,:)=t
        enddo

        return
    end subroutine lsort
end program

Score : 300 points

Problem Statement

We will call a string obtained by arranging the characters contained in a string a in some order, an anagram of a.

For example, greenbin is an anagram of beginner. As seen here, when the same character occurs multiple times, that character must be used that number of times.

Given are N strings s_1, s_2, \ldots, s_N. Each of these strings has a length of 10 and consists of lowercase English characters. Additionally, all of these strings are distinct. Find the number of pairs of integers i, j (1 \leq i < j \leq N) such that s_i is an anagram of s_j.

Constraints

2 \leq N \leq 10^5

s_i is a string of length 10.

Each character in s_i is a lowercase English letter.

s_1, s_2, \ldots, s_N are all distinct.

Input

Input is given from Standard Input in the following format:

N
s_1
s_2
:
s_N

Output

Print the number of pairs of integers i, j (1 \leq i < j \leq N) such that s_i is an anagram of s_j.

Sample Input 1

3
acornistnt
peanutbomb
constraint

Sample Output 1

1

s_1 = acornistnt is an anagram of s_3 = constraint. There are no other pairs i, j such that s_i is an anagram of s_j, so the answer is 1.

Sample Input 2

2
oneplustwo
ninemodsix

Sample Output 2

0

If there is no pair i, j such that s_i is an anagram of s_j, print 0.

Sample Input 3

5
abaaaaaaaa
oneplustwo
aaaaaaaaba
twoplusone
aaaabaaaaa

Sample Output 3

4

Note that the answer may not fit into a 32-bit integer type, though we cannot put such a case here.


program main
integer,allocatable :: a(:,:)
integer :: tmp(10)
character,allocatable :: s(:)
integer :: n,i,j,ans = 0

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

do i = 1,n
do j = 1,10
a(i,j) = ichar(s(i)(j:j))
end do
end do

do i = 1,n
call quicksort(a(i,:),1,10)
end do

do i = 1,n-1
do j = i+1, n
tmp = abs(a(i,:) - a(j,:))
if (maxval(tmp) == 0) ans = ans +1
end do
end do

print *,ans

contains
recursive subroutine quicksort(a, first, last)
  implicit none
  integer :: a(*)
  integer first, last
  integer i, j
  real*8  x, t

  x = a( (first+last) / 2 )
  i = first
  j = last
  do
     do while (a(i) < x)
        i=i+1
     end do
     do while (x < a(j))
        j=j-1
     end do
     if (i >= j) exit
     t = a(i);  a(i) = a(j);  a(j) = t
     i=i+1
     j=j-1
  end do
  if (first < i - 1) call quicksort(a, first, i - 1)
  if (j + 1 < last)  call quicksort(a, j + 1, last)
end subroutine quicksort
end program


program main
integer,allocatable :: a(:,:)
integer :: tmp(10)
character,allocatable :: s(:)
integer :: n,i,j,ans = 0

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

do i = 1,n
do j = 1,10
a(i,j) = ichar(s(i)(j:j))
end do
end do

do i = 1,n
call quicksort(a(i,:),1,10)
end do

do i = 1,n-1
do j = i+1, n
tmp = abs(a(i,:) - a(j,:))
print *,tmp
if (maxval(tmp) == 0) ans = ans +1
end do
end do

print *,ans

contains
recursive subroutine quicksort(a, first, last)
  implicit none
  integer :: a(*)
  integer first, last
  integer i, j
  real*8  x, t

  x = a( (first+last) / 2 )
  i = first
  j = last
  do
     do while (a(i) < x)
        i=i+1
     end do
     do while (x < a(j))
        j=j-1
     end do
     if (i >= j) exit
     t = a(i);  a(i) = a(j);  a(j) = t
     i=i+1
     j=j-1
  end do
  if (first < i - 1) call quicksort(a, first, i - 1)
  if (j + 1 < last)  call quicksort(a, j + 1, last)
end subroutine quicksort
end program


program main
integer,allocatable :: a(:,:)
integer :: tmp(10)
character,allocatable :: s(:)
integer :: n,i,j,ans = 0

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

do i = 1,n
do j = 1,10
a(i,j) = ichar(s(i)(j:j))
end do
end do

do i = 1,n
call quicksort(a(i,:),1,10)
end do

do i = 1,n-1
do j = i+1, n
tmp = a(i,:) - a(j,:)
if (maxval(tmp) == 0) ans = ans +1
end do
end do

print *,ans
contains
recursive subroutine quicksort(a, first, last)
  implicit none
  integer :: a(*)
  integer first, last
  integer i, j
  real*8  x, t

  x = a( (first+last) / 2 )
  i = first
  j = last
  do
     do while (a(i) < x)
        i=i+1
     end do
     do while (x < a(j))
        j=j-1
     end do
     if (i >= j) exit
     t = a(i);  a(i) = a(j);  a(j) = t
     i=i+1
     j=j-1
  end do
  if (first < i - 1) call quicksort(a, first, i - 1)
  if (j + 1 < last)  call quicksort(a, j + 1, last)
end subroutine quicksort
end program


module ABC137

  ! <module>s to import
  use, intrinsic :: iso_fortran_env

  ! require all variables to be explicitly declared
  implicit none

  ! accessibility of <subroutine>s and <function>s in this <module>
  public  :: task_C
  private :: sort_string
  private :: isEqual

  ! constants for this <module>
  integer(INT8), parameter, private :: len_string = 10_INT32

  ! <type>s for this <module>
  type data_task
    character(len=len_string, kind=1), public :: string
    integer(INT64),                    public :: times
  end type data_task

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

  subroutine sort_string (str_target)

    ! arguments for this <subroutine>
    character(len=len_string, kind=1), intent(inout) :: str_target

    ! variables for this <subroutine>
    character(len=1, kind=1) :: buffer

    ! support variables for this <subroutine>
    integer(INT8) :: itr1, itr2

    do itr1 = 1_INT8,        len_string - 1_INT8, 1_INT8
    do itr2 = itr1 + 1_INT8, len_string,           1_INT8
      if ( str_target(itr1:itr1) .gt. str_target(itr2:itr2) ) then
        buffer                = str_target(itr1:itr1)
        str_target(itr1:itr1) = str_target(itr2:itr2)
        str_target(itr2:itr2) = buffer
      end if
    end do
    end do

    ! STEP.END
    return

  end subroutine sort_string

  pure function isEqual (str1, str2) result(stat)

    ! arguments for this <function>
    character(len=*, kind=1), intent(in) :: str1
    character(len=*, kind=1), intent(in) :: str2

    ! return value of this <function>
    logical :: stat

    stat = lge(str1, str2) .and. lle(str1, str2)
    return

  end function isEqual

  subroutine task_C

    ! variables for this <subroutine>
    integer(INT32) :: num_string
    integer(INT64) :: num_anagram

    ! arrays for this <module>
    type(data_task), allocatable :: given_data(:)

    ! support variables for this <subroutine>
    integer(INT32) :: itr, itr_origin, itr_target

    ! STEP.01
    ! read out the given data

    ! STEP.01.01
    ! read out the number of given strings
    read(unit=INPUT_UNIT, fmt=*) num_string

    ! STEP.01.02
    ! allocate the array to store the given strings
    allocate( given_data(1:num_string) )

    ! STEP.01.03
    ! read out the given strings
    do itr = 1_INT32, num_string, 1_INT32
      read(unit=INPUT_UNIT, fmt='(A)') given_data(itr)%string
    end do

    ! STEP.02
    ! calcualte the answer of this task

    ! STEP.02.01
    ! initialize the variables
    num_anagram         = 0_INT64
    given_data(:)%times = 0_INT64

    ! STEP.02.02
    ! count up the number of anagram
    do itr_origin = 1_INT32, num_string, 1_INT32

      ! sort the each given string
      call sort_string( given_data(itr_origin)%string )

      do itr_target = 1_INT32, itr_origin - 1_INT32, 1_INT32
        if ( isEqual( given_data(itr_origin)%string, given_data(itr_target)%string ) ) then
          given_data(itr_target)%times = 1_INT64     + given_data(itr_target)%times
          num_anagram                  = num_anagram + given_data(itr_target)%times
          exit
        end if
      end do

      print '(10(1X,I0))', num_anagram, given_data(:)%times

    end do

    ! STEP.03
    ! output the answer of this task
    write(unit=OUTPUT_UNIT, fmt='(I0)', advance='yes') num_anagram

    ! STEP.04
    ! deallocate the array to store the given strings
    deallocate( given_data )

    ! STEP.END
    return

  end subroutine task_C

end module ABC137

program main

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

  ! require all variables to be explicitly declared
  implicit none

  call task_C

end program main


implicit none
integer :: n,i,j,k,ans
character(10) :: s
integer(16),allocatable :: p(:)
logical :: flg

read *,n
allocate(p(n))

p(:) = 0
do i = 1,n
   read *,s
   flg = .true.
   do j = 2, 10
      if (s(j:j) /= s(1:1)) then
         flg = .false.
         exit
      endif
   enddo
   if (flg) then
      p(i) = 1
   endif
   do j = 1, 10
      if (s(j:j) == 'a') then
         k = 1
      else if (s(j:j) == 'b') then
         k = 2
      else if (s(j:j) == 'c') then
         k = 3
      else if (s(j:j) == 'd') then
         k = 4
      else if (s(j:j) == 'e') then
         k = 5
      else if (s(j:j) == 'f') then
         k = 6
      else if (s(j:j) == 'g') then
         k = 7
      else if (s(j:j) == 'h') then
         k = 8
      else if (s(j:j) == 'i') then
         k = 9
      else if (s(j:j) == 'j') then
         k = 10
      else if (s(j:j) == 'k') then
         k = 11
      else if (s(j:j) == 'l') then
         k = 12
      else if (s(j:j) == 'm') then
         k = 13
      else if (s(j:j) == 'n') then
         k = 14
      else if (s(j:j) == 'o') then
         k = 15
      else if (s(j:j) == 'p') then
         k = 16
      else if (s(j:j) == 'q') then
         k = 17
      else if (s(j:j) == 'r') then
         k = 18
      else if (s(j:j) == 's') then
         k = 19
      else if (s(j:j) == 't') then
         k = 20
      else if (s(j:j) == 'u') then
         k = 21
      else if (s(j:j) == 'v') then
         k = 22
      else if (s(j:j) == 'w') then
         k = 23
      else if (s(j:j) == 'x') then
         k = 24
      else if (s(j:j) == 'y') then
         k = 25
      else if (s(j:j) == 'z') then
         k = 26
      endif
      p(i) = p(i) + 10**k
   enddo
enddo

call cs11(n,p)

ans = 0
do i = 1, n-1
   do j = i+1, n
      if (p(i) == p(j)) then
         ans = ans + 1
      else
         exit
      endif
   enddo
enddo

print '(i0)',ans

contains

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

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


program green_bin
  implicit none
  integer :: n, c(100000,26) = 0, i, j, k
  logical :: used(100000) = .false.
  character(10) :: s
  integer(8) :: ans = 0_8, m
  read(*,*) n
  do i = 1, n
    read(*,*) s
    do j = 1, 10
      k = ichar(s(j:j))-96
      c(i,k) = c(i,k)+1
    end do
  end do
  do i = 1, n
    if (used(i)) cycle
    m = 0_8
    do j = i, n
      if (used(j)) cycle
      if (all(c(i,:) == c(j,:))) then
        used(j) = .true.
        m = m+1_8
      end if
    end do
    ans = ans+m*(m-1_8)/2_8
  end do
  write(*,'(i0)') ans
end program green_bin


module mod_priority_queue
    private swap
    type pq
        integer(4):: i=0
        integer(4),pointer:: n(:) => null()
    end type

contains
subroutine append(que,x)
    type(pq):: que
    integer(4):: x

    que%i = que%i+1
    if ( .not. associated(que%n)) allocate(que%n(1))
    if (que%i >= size(que%n(:))) then
        block; integer(4), pointer:: tmp(:)
            allocate(tmp(que%i))
            tmp(:) = que%n(:)
            deallocate(que%n)
            allocate(que%n(que%i*2))
            que%n(:que%i) = tmp(:)
            deallocate(tmp)
        end block
    end if
    que%n(que%i) = x

    block
        integer(4):: self, p
        self = que%i

        do while(self >= 2)
            p = self/2
            if (que%n(p) > que%n(self)) exit
            call swap(que%n(p), que%n(self))
            self = p
        end do
    end block

end subroutine

function pop(que) result(ret)
    type(pq):: que
    integer(4):: ret
    integer(4),pointer:: tmp(:)
    ret = que%n(1)
    que%n(1) = que%n(que%i)
    que%i=que%i-1

    block
        integer:: self, c1, c2
        self = 1
        c1=self*2
        c2=self*2+1

        do while(c1 <= que%i)

            if (c2 <= que%i .and. que%n(c2) > que%n(c1)) c1 = c2
            if (que%n(c1) < que%n(self)) exit
            call swap(que%n(c1), que%n(self))
            self = c1
            c1 = self*2
            c2 = self*2+1
        end do
    end block
end function

subroutine swap(a,b)
    integer(4):: a, b, tmp
    tmp =  a
    a = b
    b = tmp
end subroutine
end module

program summer_vacation
    use mod_priority_queue
    implicit none
    integer(4):: n,m
    integer(4):: i,j,k
    integer(4),allocatable:: a(:), b(:)
    integer(8):: ans=0
    type(pq):: que

    read*,n,m
    allocate(a(n),b(n))
    do i=1,n
        read*,a(i),b(i)
    end do
    call merge_sort(a(:),b(:),1,n)
    j=1

    do i=1, m
        do while (a(j) == i)
            call append(que,b(j))
            j=j+1
        end do
        if (que%i > 0) ans = ans + pop(que)
    end do

    print*, ans

contains
recursive subroutine merge_sort(a,b,fst,lst)
    integer(4):: a(:),b(:),fst,lst,mdl

    if (lst-fst < 2) then
        if(a(lst) < a(fst)) then
            call swap(a(:), fst,lst)
            call swap(b(:), fst,lst)
        end if
        return
    end if

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

subroutine merge_(a,b,fst,mdl,lst)
    integer(4):: a(:),b(:),fst,mdl,lst
    integer(4),pointer:: ta(:),tb(:)
    integer(4):: it,il,ir

    allocate(ta(lst-fst+1), tb(lst-fst+1))
    it=1; il=fst; ir=mdl+1

    do while(il <= mdl .and. ir <= lst)
        if (a(il) <= a(ir)) then
            ta(it) = a(il)
            tb(it) = b(il)
            il=il+1
        else
            ta(it) = a(ir)
            tb(it) = b(ir)
            ir=ir+1
        end if
        it=it+1
    end do

    if (ir > lst) then
        ta(it:) = a(il:mdl)
        tb(it:) = b(il:mdl)
    else
        ta(it:) = a(ir:lst)
        tb(it:) = b(ir:lst)
    end if
    a(fst:lst) = ta(:)
    b(fst:lst) = tb(:)
    deallocate(ta,tb)
end subroutine

subroutine swap(ar,x,y)
    integer(4):: ar(:), x, y, tmp
    tmp = ar(x)
    ar(x) = ar(y)
    ar(y) = tmp
end subroutine

end program summer_vacation

Score : 400 points

Problem Statement

There are N one-off jobs available. If you take the i-th job and complete it, you will earn the reward of B_i after A_i days from the day you do it.

You can take and complete at most one of these jobs in a day.

However, you cannot retake a job that you have already done.

Find the maximum total reward that you can earn no later than M days from today.

You can already start working today.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq A_i \leq 10^5

1 \leq B_i \leq 10^4

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

Print the maximum total reward that you can earn no later than M days from today.

Sample Input 1

3 4
4 3
4 1
2 2

Sample Output 1

5

You can earn the total reward of 5 by taking the jobs as follows:

Take and complete the first job today. You will earn the reward of 3 after four days from today.

Take and complete the third job tomorrow. You will earn the reward of 2 after two days from tomorrow, that is, after three days from today.

Sample Input 2

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

Sample Output 2

10

Sample Input 3

1 1
2 1

Sample Output 3

0


module mod_priority_queue
  implicit none
  type t_priority_queue
    private
    integer :: num = 0 !中に入ってる物の数
    integer, pointer :: heap(:) => null()!pqの容量
  contains
    procedure :: offer => offer
    procedure :: clear => clear
    procedure :: poll => poll
    procedure :: peek => peek
    procedure :: size => size_of
  end type t_priority_queue

contains

  subroutine offer(pq,item)
    class(t_priority_queue), intent(inout) :: pq
    integer, intent(in) :: item
    integer :: n, i, t
    integer, allocatable :: tmp(:)
    if (.not.associated(pq%heap)) allocate(pq%heap(1))

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

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

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

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

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

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

use mod_priority_queue
integer N,M,K
integer,allocatable,dimension(:)::A,B
type(t_priority_queue) :: pq
integer(16) ans
read*,N,M
allocate(A(N),B(N))
do i=1,N
  read*,A(i),B(i)
end do

call HeapsortPair(N,A,B)

ans=0;k=1
do i=1,M
  do while(K<=N .and. A(K)<=i)
    call offer(pq,B(K))
    K=K+1
  end do
  ans=ans+poll(pq)
end do

print"(i0)",ans

contains

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

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

end


program Main
	Implicit none
    Integer::A,B,C
    read(*,*) A,B,C
    C=C-(A-B)
    IF(C<0)THEN
    	C=0
    END IF
    write(*,*) C
end program Main

Score : 100 points

Problem Statement

We have two bottles for holding water.

Bottle 1 can hold up to A milliliters of water, and now it contains B milliliters of water.

Bottle 2 contains C milliliters of water.

We will transfer water from Bottle 2 to Bottle 1 as much as possible.

How much amount of water will remain in Bottle 2?

Constraints

All values in input are integers.

1 \leq B \leq A \leq 20

1 \leq C \leq 20

Input

Input is given from Standard Input in the following format:

A B C

Output

Print the integer representing the amount of water, in milliliters, that will remain in Bottle 2.

Sample Input 1

6 4 3

Sample Output 1

1

We will transfer two milliliters of water from Bottle 2 to Bottle 1, and one milliliter of water will remain in Bottle 2.

Sample Input 2

8 3 9

Sample Output 2

4

Sample Input 3

12 3 7

Sample Output 3

0


program main
  implicit none

  integer :: a, b ,c

  read *, a, b, c

  print *, c - ( a - b )

end program main


module ABC136

  ! <module>s to import
  use, intrinsic :: iso_fortran_env

  ! require all variables to be explicitly declared
  implicit none

  ! accessibility of <subroutine>s and <function>s in this <module>
  public :: task_A

  contains

  subroutine task_A (volume_Container1_max, volume_Container1_contained, volume_Container2_contained)

    ! arguments for this <subroutine>
    integer(INT8), intent(in) :: volume_Container1_max
    integer(INT8), intent(in) :: volume_Container1_contained
    integer(INT8), intent(in) :: volume_Container2_contained

    ! variables for this <subroutine>
    integer(INT8) :: volume_Container1_receivable
    integer(INT8) :: volume_remained

    volume_Container1_receivable = volume_Container1_max - volume_Container1_contained

    if (volume_Container1_receivable .ge. volume_Container2_contained) then
      volume_remained = 0_INT8
    else
      volume_remained = volume_Container2_contained - volume_Container1_receivable
    end if

    write(unit=OUTPUT_UNIT, fmt='(I0)', advance='yes') volume_remained

    ! STEP.END
    return

  end subroutine task_A

end module ABC136

program main

  ! <module>s to import
  use,     intrinsic :: iso_fortran_env
  use, non_intrinsic :: ABC136

  ! variables for this <program>
  integer(INT8) :: A
  integer(INT8) :: B
  integer(INT8) :: C

  read(unit=INPUT_UNIT, fmt=*) A, B, C

  call task_A (A, B, C)

end program main


Score : 200 points

Problem Statement

Given is an integer N. Find the number of positive integers less than or equal to N that have an odd number of digits (in base ten without leading zeros).

Constraints

1 \leq N \leq 10^5

Input

Input is given from Standard Input in the following format:

N

Output

Print the number of positive integers less than or equal to N that have an odd number of digits.

Sample Input 1

11

Sample Output 1

9

Among the positive integers less than or equal to 11, nine integers have an odd number of digits: 1, 2, \ldots, 9.

Sample Input 2

136

Sample Output 2

46

In addition to 1, 2, \ldots, 9, another 37 integers also have an odd number of digits: 100, 101, \ldots, 136.

Sample Input 3

100000

Sample Output 3

90909


program main
 implicit none
 integer*8 N,i,j,k
 read(*,*) N
 k = 0
 j = 1
 do i=1,N
  if(j>N) exit
  if(10 <= j .and. j < 100) then
   j = 100
  else if(1000 <= j .and. j < 10000) then
   j = 10000
  else if(j >= 100000) then
   exit
  else
   j = j+1
   k = k+1
  end if
 end do
 write(*,*) k
end program main


program nn
  implicit none
  integer :: N,digi,i
  integer :: alle

  read(*,*) N

  digi=int(log(N*1.0)/log(10.0))+1
  alle=0
!  write(*,*) digi
  if(mod(digi,2).eq.0) then
     do i=1,digi/2
        alle=alle+9*100**(i-1)
!        write(*,*) alle
     enddo
  else
    do i=1,digi/2
        alle=alle+9*100**(i-1)
     enddo
     alle=alle+N-(10**(digi-1))+1
  endif

  write(*,'(i0)') int(alle)
end program nn


implicit none

integer :: n, m
integer :: i

read *, n

m = 0
do i = 1, n
   if (i < 10) then
      m = m + 1
      cycle
   else if (99 < i .and. i < 1000) then
      m = m + 1
      cycle
   else if (9999 < i .and. i < 100000) then
      m = m + 1
      cycle
   endif
enddo

write(6,'(i0)') m

stop
end


program abc136d
    use,intrinsic :: iso_fortran_env
    implicit none
    character(100000):: s
    integer(int32):: n,i,f,l,m
    integer(int32), allocatable:: ans(:)
    logical,allocatable:: col(:), sep(:)

    read*, s
    n = len_trim(s)
    allocate(col(n-1), sep(0:n), ans(n))
    col(:) = .false.
    sep(:) = .false.
    sep(0) = .true.; sep(n) = .true.

    do i=1,n-1
        if (s(i:i+1) == 'RL') then
            col(i) = .true.
        else if (s(i:i+1) == 'LR') then
            sep(i) = .true.
        end if
    end do
    ! print*, col(:)
    ! print*, sep(1:n-1)
    f=1
    ans(:) = 0
    do i=1,n-1
        if (col(i)) then
            m = i
        else if (sep(i)) then
            l = i
            ! print*, '---',f,l
            if (mod(f,2) /= mod(l,2)) then
                ans(m:m+1) = (l-f+1)/2
                ! print*, ans(m:m+1)
            else if (mod(f,2) /= mod(m,2)) then
                ans(m) = (l-f+1)/2
                ans(m+1) = (l-f+1) - ans(m)
                ! print*, ans(m:m+1)
            else
                ans(m+1) = (l-f+1)/2
                ans(m) = (l-f+1) - ans(m+1)
                ! print*, ans(m:m+1)
            end if
            f = l+1
        end if
    end do

    l=n
    i=n
    ! print*, '---',f,l
    if (mod(f,2) /= mod(l,2)) then
        ans(m:m+1) = (l-f+1)/2
        ! print*, ans(m:m+1)
    else if (mod(f,2) == mod(m,2)) then
        ans(m) = (l-f+1)/2
        ans(m+1) = (l-f+1) - ans(m)
        ! print*, ans(m:m+1)
    else
        ans(m+1) = (l-f+1)/2
        ans(m) = (l-f+1) - ans(m+1)
        ! print*, ans(m:m+1)
    end if

    print*, ans(:)
end program abc136d

Score : 400 points

Problem Statement

Given is a string S consisting of L and R.

Let N be the length of S. There are N squares arranged from left to right, and the i-th character of S from the left is written on the i-th square from the left.

The character written on the leftmost square is always R, and the character written on the rightmost square is always L.

Initially, one child is standing on each square.

Each child will perform the move below 10^{100} times:

Move one square in the direction specified by the character written in the square on which the child is standing. L denotes left, and R denotes right.

Find the number of children standing on each square after the children performed the moves.

Constraints

S is a string of length between 2 and 10^5 (inclusive).

Each character of S is L or R.

The first and last characters of S are R and L, respectively.

Input

Input is given from Standard Input in the following format:

S

Output

Print the number of children standing on each square after the children performed the moves, in order from left to right.

Sample Input 1

RRLRL

Sample Output 1

0 1 2 1 1

After each child performed one move, the number of children standing on each square is 0, 2, 1, 1, 1 from left to right.

After each child performed two moves, the number of children standing on each square is 0, 1, 2, 1, 1 from left to right.

After each child performed 10^{100} moves, the number of children standing on each square is 0, 1, 2, 1, 1 from left to right.

Sample Input 2

RRLLLLRLRRLL

Sample Output 2

0 3 3 0 0 0 1 1 0 2 2 0

Sample Input 3

RRRLLRLLRRRLLLLL

Sample Output 3

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


program abc136d
    use,intrinsic :: iso_fortran_env
    implicit none
    character(100000):: s
    integer(int32):: n,i,f,l,m
    integer(int32), allocatable:: ans(:)
    logical,allocatable:: col(:), sep(:)

    read*, s
    n = len_trim(s)
    allocate(col(n-1), sep(0:n), ans(n))
    col(:) = .false.
    sep(:) = .false.
    sep(0) = .true.; sep(n) = .true.

    do i=1,n-1
        if (s(i:i+1) == 'RL') then
            col(i) = .true.
        else if (s(i:i+1) == 'LR') then
            sep(i) = .true.
        end if
    end do
    ! print*, col(:)
    ! print*, sep(1:n-1)
    f=1
    ans(:) = 0
    do i=1,n-1
        if (col(i)) then
            m = i
        else if (sep(i)) then
            l = i
            ! print*, '---',f,l
            if (mod(f,2) /= mod(l,2)) then
                ans(m:m+1) = (l-f+1+1)/2
                ! print*, ans(m:m+1)
            else if (mod(f,2) /= mod(m,2)) then
                ans(m) = (l-f+1)/2
                ans(m+1) = (l-f+1) - ans(m)
                ! print*, ans(m:m+1)
            else
                ans(m+1) = (l-f+1)/2
                ans(m) = (l-f+1) - ans(m+1)
                ! print*, ans(m:m+1)
            end if
            f = l+1
        end if
    end do

    l=n
    i=n
    ! print*, '---',f,l
    if (mod(f,2) /= mod(l,2)) then
        ans(m:m+1) = (l-f+1)/2
        ! print*, ans(m:m+1)
    else if (mod(f,2) == mod(m,2)) then
        ans(m) = (l-f+1)/2
        ans(m+1) = (l-f+1) - ans(m)
        ! print*, ans(m:m+1)
    else
        ans(m+1) = (l-f+1)/2
        ans(m) = (l-f+1) - ans(m+1)
        ! print*, ans(m:m+1)
    end if

    print'(100000(i0,1x))', ans(:)
end program abc136d


program max_gcd
  implicit none
  integer :: n, a(500) = 0, s, d(100000) = 0, num = 0, i, j, x(500) = 0
  integer(8) :: k, m, p, q
  read(*,*) n, k
  read(*,*) a(1:n)
  s = sum(a(1:n))
  do i = 1, s
    if (i*i > s) exit
    if (mod(s,i) /= 0) cycle
    num = num+1
    d(num) = i
    if (s/i /= i) then
      num = num+1
      d(num) = s/i
    end if
  end do
  call quick_sort(d(1:num))
  do i = num, 1, -1
    x(1:n) = mod(a(1:n),d(i))
    call quick_sort(x(1:n))
    m = 1e18
    q = sum(int(d(i)-x(1:n),8))
    p = 0_8
    do j = 1, n
      if (x(j) == 0) cycle
      p = p+int(x(j),8)
      q = q-int(d(i)-x(j),8)
      m = min(m,max(p,q))
    end do
    if (m <= k) then
      write(*,'(i0)') d(i)
      stop
    end if
  end do
  stop
contains
  recursive subroutine quick_sort(a)
    implicit none
    integer, intent(inout) :: a(:)
    integer :: p, c
    integer :: n, l, r, m
    n = size(a)
    l = 1
    r = n
    m = (l+r)/2
    p = a(l)+a(m)+a(r)-max(a(l),a(m),a(r))-min(a(l),a(m),a(r))
    do
      do while (a(l) < p)
        l = l+1
      end do
      do while (a(r) > p)
        r = r-1
      end do
      if (l >= r) exit
      c = a(l)
      a(l) = a(r)
      a(r) = c
      l = l+1
      r = r-1
    end do
    if (l > 2) call quick_sort(a(1:l-1))
    if (n > r+1) call quick_sort(a(r+1:n))
  end subroutine quick_sort
end program max_gcd

Score : 500 points

Problem Statement

We have a sequence of N integers: A_1, A_2, \cdots, A_N.

You can perform the following operation between 0 and K times (inclusive):

Choose two integers i and j such that i \neq j, each between 1 and N (inclusive). Add 1 to A_i and -1 to A_j, possibly producing a negative element.

Compute the maximum possible positive integer that divides every element of A after the operations. Here a positive integer x divides an integer y if and only if there exists an integer z such that y = xz.

Constraints

2 \leq N \leq 500

1 \leq A_i \leq 10^6

0 \leq K \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \cdots A_{N-1} A_{N}

Output

Print the maximum possible positive integer that divides every element of A after the operations.

Sample Input 1

2 3
8 20

Sample Output 1

7

7 will divide every element of A if, for example, we perform the following operation:

Choose i = 2, j = 1. A becomes (7, 21).

We cannot reach the situation where 8 or greater integer divides every element of A.

Sample Input 2

2 10
3 5

Sample Output 2

8

Consider performing the following five operations:

Choose i = 2, j = 1. A becomes (2, 6).

Choose i = 2, j = 1. A becomes (1, 7).

Choose i = 2, j = 1. A becomes (0, 8).

Choose i = 2, j = 1. A becomes (-1, 9).

Choose i = 1, j = 2. A becomes (0, 8).

Then, 0 = 8 \times 0 and 8 = 8 \times 1, so 8 divides every element of A. We cannot reach the situation where 9 or greater integer divides every element of A.

Sample Input 3

4 5
10 1 2 22

Sample Output 3

7

Sample Input 4

8 7
1 7 5 6 8 2 6 5

Sample Output 4

5


program main
	implicit none
    integer::a,b
    read(*,*)a,b
    if(mod(a,2)/=mod(b,2))then
    	write(*,*) 'IMPOSSIBLE'
    else
    	write(*,*) (a+b)/2
    end if
    stop
end program main

Score: 100 points

Problem Statement

We have two distinct integers A and B.

Print the integer K such that |A - K| = |B - K|.

If such an integer does not exist, print IMPOSSIBLE instead.

Constraints

All values in input are integers.

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

A and B are distinct.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the integer K satisfying the condition.

If such an integer does not exist, print IMPOSSIBLE instead.

Sample Input 1

2 16

Sample Output 1

9

|2 - 9| = 7 and |16 - 9| = 7, so 9 satisfies the condition.

Sample Input 2

0 3

Sample Output 2

IMPOSSIBLE

No integer satisfies the condition.

Sample Input 3

998244353 99824435

Sample Output 3

549034394


program main
    implicit none
    integer n
    integer a,b
    integer alice, bob
    integer, allocatable :: card(:)

    read*, a, b
    if (mod(a+b, 2) == 0)then
        print'(i0)', (a+b) /2
    else
        print'(a)', 'IMPOSSIBLE'
    endif

end program main


program main
  implicit none
  integer :: i, j, n
  logical :: ans
  integer, allocatable :: p(:), q(:)

  read *, n
  allocate (p(n), q(n))
  read *, p
  q(:) = p(:)
  if (minval(q(2:n) - q(1:n-1)) > 0) ans = .true.
  do i = 1, n
    do j = i+1, n
      q(:) = p(:)
      q(i) = p(j)
      q(j) = p(i)
      if (minval(q(2:n) - q(1:n-1)) == 1 .and. maxval(q(2:n) - q(1:n-2)) == 1) ans = .true.
    end do
  end do
  if (ans) then
    print '(a)', 'YES'
  else
    print '(a)', 'NO'
  end if
end program

Score : 200 points

Problem Statement

We have a sequence p = {p_1,\ p_2,\ ...,\ p_N} which is a permutation of {1,\ 2,\ ...,\ N}.

You can perform the following operation at most once: choose integers i and j (1 \leq i < j \leq N), and swap p_i and p_j. Note that you can also choose not to perform it.

Print YES if you can sort p in ascending order in this way, and NO otherwise.

Constraints

All values in input are integers.

2 \leq N \leq 50

p is a permutation of {1,\ 2,\ ...,\ N}.

Input

Input is given from Standard Input in the following format:

N
p_1 p_2 ... p_N

Output

Print YES if you can sort p in ascending order in the way stated in the problem statement, and NO otherwise.

Sample Input 1

5
5 2 3 4 1

Sample Output 1

YES

You can sort p in ascending order by swapping p_1 and p_5.

Sample Input 2

5
2 4 3 5 1

Sample Output 2

NO

In this case, swapping any two elements does not sort p in ascending order.

Sample Input 3

7
1 2 3 4 5 6 7

Sample Output 3

YES

p is already sorted in ascending order, so no operation is needed.


program main
  implicit none
  integer(8) :: n, i, res, kill
  integer(8), allocatable :: a(:), b(:)
  read (*, *) n
  allocate (a(n + 1), b(n))
  read (*, *) a
  read (*, *) b
  res = 0
  do i = 1, n
    kill = min(a(i), b(i))
    res = res + kill
    b(i) = b(i) - kill
    kill = min(a(i + 1), b(i))
    res = res + kill
    a(i + 1) = a(i + 1) - kill
  end do
  write (*, "(i0)") res
end program main

Score : 300 points

Problem Statement

There are N+1 towns. The i-th town is being attacked by A_i monsters.

We have N heroes. The i-th hero can defeat monsters attacking the i-th or (i+1)-th town, for a total of at most B_i monsters.

What is the maximum total number of monsters the heroes can cooperate to defeat?

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

1 \leq B_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_{N+1}
B_1 B_2 ... B_N

Output

Print the maximum total number of monsters the heroes can defeat.

Sample Input 1

2
3 5 2
4 5

Sample Output 1

9

If the heroes choose the monsters to defeat as follows, they can defeat nine monsters in total, which is the maximum result.

The first hero defeats two monsters attacking the first town and two monsters attacking the second town.

The second hero defeats three monsters attacking the second town and two monsters attacking the third town.

Sample Input 2

3
5 6 3 8
5 100 8

Sample Output 2

22

Sample Input 3

2
100 1 1
1 100

Sample Output 3

3


program main
    implicit none
    integer i, j, k
    integer n_len, tg, pop
    integer :: mod_ = 10**9+7
    integer, allocatable :: dp(:),old_dp(:)
    character(10**5):: S

    read*, S
    n_len = len_trim(S)
    allocate(dp(0:12), old_dp(0:12))
    dp(0:12) = 0
    dp(0) = 1
    old_dp(0:12) = 0

    do i=1, n_len
        old_dp(0:12) = dp(0:12)
        dp(0:12) = 0

        if(S(i:i) == "?")then
            do j = 0,9
                do k=0,12
                    pop = mod(k*10+j,13)
                    dp(pop) = dp(pop) + old_dp(k)
                    dp(pop) = mod(dp(pop),mod_)
                enddo
            enddo

        else
            read(S(i:i),*) tg
            do k=0,12
                pop = mod(k*10+tg,13)
                dp(pop) = dp(pop) + old_dp(k)
                dp(pop) = mod(dp(pop),mod_)
            enddo
        endif

    enddo

print'(i0)', dp(5)

stop
end program main

Score : 400 points

Problem Statement

Given is a string S. Each character in S is either a digit (0, ..., 9) or ?.

Among the integers obtained by replacing each occurrence of ? with a digit, how many have a remainder of 5 when divided by 13? An integer may begin with 0.

Since the answer can be enormous, print the count modulo 10^9+7.

Constraints

S is a string consisting of digits (0, ..., 9) and ?.

1 \leq |S| \leq 10^5

Input

Input is given from Standard Input in the following format:

S

Output

Print the number of integers satisfying the condition, modulo 10^9+7.

Sample Input 1

??2??5

Sample Output 1

768

For example, 482305, 002865, and 972665 satisfy the condition.

Sample Input 2

?44

Sample Output 2

1

Only 044 satisfies the condition.

Sample Input 3

7?4

Sample Output 3

0

We may not be able to produce an integer satisfying the condition.

Sample Input 4

?6?42???8??2??06243????9??3???7258??5??7???????774????4?1??17???9?5?70???76???

Sample Output 4

153716888