module ABC142

  ! <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_D
  private :: gcd
  private :: info_num

  ! <type>s for this <module>
  type info_num
    logical, public :: is_cd
    logical, public :: is_prime
  end type

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

  pure function gcd (a, b) result (val_gcd)

    ! arguments for this <function>
    integer(INT64), intent(in) :: a, b

    ! variables for this <function>
    integer(INT64) :: buf_a, buf_b, buf

    ! return value of this <function>
    integer(INT64) :: val_gcd

    buf_a = a
    buf_b = b

    do while (buf_b .ne. 0_INT64)
      buf   = mod(buf_a, buf_b)
      buf_a = buf_b
      buf_b = buf
    end do

    val_gcd = buf_a

    return

  end function gcd

  subroutine task_D (A, B)

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

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

    ! arrays for this <subroutine>
    type(info_num), allocatable :: list_candidates(:)

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

    ! STEP.01
    ! calculate the greatest common divisor of A & B
    gcd_AB = gcd(A, B)

    ! STEP.02
    ! judge the greatest common divisor of A & B
    if (gcd_AB .eq. 1_INT64) then
      print '(I0)', 1_INT64
      return
    end if

    ! STEP.03
    ! allocate the array to store the given data
    allocate( list_candidates(2:gcd_AB) )

    ! STEP.02
    ! count up the number of the common divisors which are also prime numbers
    do itr_n = 2_INT64, gcd_AB, 1_INT64
      list_candidates(itr_n)%is_cd = (mod(A, itr_n) .eq. 0_INT64) .and. (mod(B, itr_n) .eq. 0_INT64)
    end do

    ! STEP.03
    ! judge whether the number is a prime number
    if (gcd_AB .ge. 2_INT64) list_candidates(2_INT64)%is_prime = .true.

    do itr_n = 3_INT64, gcd_AB, 1_INT64

      itr                             = 2_INT64
      list_candidates(itr_n)%is_prime = .true.

      do while (itr .lt. itr_n)

        if ( list_candidates(itr)%is_prime ) then
          if ( mod(itr_n, itr) .eq. 0_INT64 ) then
            list_candidates(itr_n)%is_prime = .false.
            exit
          else
          end if
        end if

        itr = itr + 1_INT64

      end do

    end do

    ! STEP.04
    ! count up the number of the target common divisors
    size_target = 1_INT32

    do itr_n = 2_INT64, gcd_AB, 1_INT64
      if ( list_candidates(itr_n)%is_cd .and. list_candidates(itr_n)%is_prime ) size_target = size_target + 1_INT32
    end do

    ! STEP.05
    ! output the answer of this task
    print '(I0,*(1X,I0))', size_target

    ! STEP.06
    ! deallocate the array to store the given data
    deallocate( list_candidates )

    ! STEP.END
    return

  end subroutine task_D

end module ABC142

program main

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

  ! require all variables to be explicitly declared
  implicit none

  ! variables for this <program>
  integer(INT64) :: 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_D (A, B)

end program main


integer(16) A,B
integer(16) g,gf,i
integer(16) cnt

read*,A,B
g=gcd(A,B)

gf=g
cnt=1
do i=2,gf
  if(mod(g,i)==0)then
    do while(mod(g,i)==0)
      g=g/i
    end do
    cnt=cnt+1
  endif
end do
print"(i0)",g

contains
pure function gcd(a,b)
integer(16),intent(in)::a,b
integer(16) gcd
integer(16) s,t
s=a
gcd=b

t=mod(s,gcd)
do while(t/=0)
  s=gcd
  gcd=t
  t=mod(s,gcd)
end do
end function gcd
end

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

Score : 400 points

Problem Statement

Given are positive integers A and B.

Let us choose some number of positive common divisors of A and B.

Here, any two of the chosen divisors must be coprime.

At most, how many divisors can we choose?

Definition of common divisor

An integer d is said to be a common divisor of integers x and y when d divides both x and y.

Definition of being coprime

Integers x and y are said to be coprime when x and y have no positive common divisors other than 1.

Definition of dividing

An integer x is said to divide another integer y when there exists an integer \alpha such that y = \alpha x.

Constraints

All values in input are integers.

1 \leq A, B \leq 10^{12}

Input

Input is given from Standard Input in the following format:

A B

Output

Print the maximum number of divisors that can be chosen to satisfy the condition.

Sample Input 1

12 18

Sample Output 1

3

12 and 18 have the following positive common divisors: 1, 2, 3, and 6.

1 and 2 are coprime, 2 and 3 are coprime, and 3 and 1 are coprime, so we can choose 1, 2, and 3, which achieve the maximum result.

Sample Input 2

420 660

Sample Output 2

4

Sample Input 3

1 2019

Sample Output 3

1

1 and 2019 have no positive common divisors other than 1.

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.


! 2019.9.21
! A
! Error --> 9999
! Correct(H /= 1) --> 8888
! Correct(H = 1) --> 7777
! Correct(W = 1) --> 6666
      program answer
      implicit none
      integer :: H, W, A, B  !input number
      double precision :: Hd, Wd, Ad, Bd
      integer(8) :: i,j
      integer :: mod_j
      integer,allocatable,dimension(:,:) :: ans
      intrinsic dble,mod
! read input number
      read(*,*) H, W, A, B
! H + A > W
      allocate(ans(H,W))
!
      ans = 0
!
      Hd=dble(H)
      Wd=dble(W)
      Ad=dble(A)
      Bd=dble(B)
! B > H/2
      if( Bd .gt. Hd/2.0d0) goto 9999
! H = 1
      if( H .eq. 1) goto 7777
! A > W/2
      if( Ad .gt. Wd/2.0d0) goto 9999
! W = 1
      if( W .eq. 1) goto 6666
! B = 0(H /= 1 & W /= 1)
      if( B .eq. 0) then
        if( W .eq. A) then
          goto 8888
        else
          goto 9999
        end if
! B /= 0(H /= 1 & W /= 1)
      else
        if( Hd*Ad/Bd .eq. Wd) then
          goto 8888
        else
          goto 9999
        end if
      end if
! Output(6666)
6666  do j=1,B
        ans(1,j) = 1
      end do
      do j=1,H
        write(*,'(I1)') ans(1,j)
      end do
      stop
! Output(7777)
7777  do i=1,A
        ans(1,i) = 1
      end do
      write(*,'(1000I1)') (ans(1,j),j=1,W)
      stop
! Output(8888)
8888  do i=1,H
        do j=A*i,A*(i+1)-1
          mod_j = mod(j,W)
          if( mod_j == 0) mod_j = W
          ans(i,mod_j) = 1
        end do
      end do
      do i=1,H
        write(*,'(1000I1)') (ans(i,j),j=1,W)
      end do
      stop
! Error(9999)
9999  write(*,*) 'No'
      stop
      end program answer

Score : 300 points

Problem Statement

We have a square grid with H rows and W columns.
Snuke wants to write 0 or 1 in each of the squares.
Here, all of the following conditions have to be satisfied:

For every row, the smaller of the following is A: the number of 0s contained in the row, and the number of 1s contained in the row. (If these two numbers are equal, “the smaller” should be read as “either”.)

For every column, the smaller of the following is B: the number of 0s contained in the column, and the number of 1s contained in the column.

Determine if these conditions can be satisfied by writing 0 or 1 in each of the squares. If the answer is yes, show one way to fill the squares so that the conditions are satisfied.

Constraints

1 \leq H,W \leq 1000

0 \leq A

2 \times A \leq W

0 \leq B

2 \times B \leq H

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H W A B

Output

If the conditions cannot be satisfied by writing 0 or 1 in each of the squares, print -1.

If the conditions can be satisfied, print one way to fill the squares so that the conditions are satisfied, in the following format:

s_{11}s_{12}\cdots s_{1W}
s_{21}s_{22}\cdots s_{2W}
\vdots
s_{H1}s_{H2}\cdots s_{HW}

Here s_{ij} is the digit written in the square at the i-th row from the top and the j-th column from the left in the grid.

If multiple solutions exist, printing any of them will be accepted.

Sample Input 1

3 3 1 1

Sample Output 1

100
010
001

Every row contains two 0s and one 1, so the first condition is satisfied.
Also, every column contains two 0s and one 1, so the second condition is satisfied.

Sample Input 2

1 5 2 0

Sample Output 2

01010


! 2019.9.21
! A
! Error --> 9999
! Correct(H /= 1) --> 8888
! Correct(H = 1) --> 7777
! Correct(W = 1) --> 6666
      program answer
      implicit none
      integer :: H, W, A, B  !input number
      double precision :: Hd, Wd, Ad, Bd
      integer :: i,j
      integer,allocatable,dimension(:,:) :: ans
      intrinsic dble
! read input number
      read(*,*) H, W, A, B
! H + A > W
      if( H+A .gt. W) then
        allocate(ans(H,H+A))
      else
        allocate(ans(H,W))
      end if
!
      ans = 0
!
      Hd=dble(H)
      Wd=dble(W)
      Ad=dble(A)
      Bd=dble(B)
! B > H/2
      if( Bd .gt. Hd/2.0d0) goto 9999
! H = 1
      if( H .eq. 1) goto 7777
! A > W/2
      if( Ad .gt. Wd/2.0d0) goto 9999
! W = 1
      if( W .eq. 1) goto 6666
! B = 0(H /= 1 & W /= 1)
      if( B .eq. 0) then
        if( W .eq. A) then
          goto 8888
        else
          goto 9999
        end if
! B /= 0(H /= 1 & W /= 1)
      else
        if( Hd*Ad/Bd .eq. Wd) then
          goto 8888
        else
          goto 9999
        end if
      end if
! Output(6666)
6666  do j=1,B
        ans(1,j) = 1
      end do
      do j=1,H
        write(*,'(I1)') ans(1,j)
      end do
      stop
! Output(7777)
7777  do i=1,A
        ans(1,i) = 1
      end do
      write(*,'(1000I1)') (ans(1,j),j=1,W)
      stop
! Output(8888)
8888  do i=1,H
        do j=i,i-1+A
          ans(i,j) = 1
          if(j .gt. W) then
            ans(i,j-W) = ans(i,j)
          end if
        end do
      end do
      do i=1,H
        write(*,'(1000I1)') (ans(i,j),j=1,W)
      end do
      stop
! Error(9999)
9999  write(*,*) 'No'
      stop
      end program answer


module UnionFind

implicit none
integer,allocatable,dimension(:)::par
integer::R
contains

recursive function root(x) result(res)
integer :: x,res
if(par(x) /= x)par(x) = root(par(x))
res = par(x)
end function

subroutine unite(x,y)
integer :: x,y
x = root(x)
y = root(y)
if(x /= y)then
   par(x) = y
   R=R-1
endif
end subroutine

function same(x, y) result (res)
   integer :: x, y
   logical :: res
   res = (root(x) == root(y))
end function

end module UnionFind
module mod_queue
  implicit none

  type t_node
    private
    integer :: item
    type(t_node), pointer :: prev => null()
    type(t_node), pointer :: next => null()
  end type t_node

  type t_queue
    private
    integer :: num = 0
    type(t_node), pointer :: head => null()
    type(t_node), pointer :: tail => null()
  contains
    procedure :: enqueue => enqueue
    procedure :: dequeue => dequeue
    procedure :: peek => peek
    procedure :: clear => clear
    procedure :: size => size_of
    final :: finalize
  end type t_queue

contains

  subroutine finalize(this)
    type(t_queue), intent(inout) :: this

    call clear(this)
  end

  function new_node(item) result(node)
    integer, intent(in) :: item
    type(t_node), pointer :: node

    allocate(node)
    node%item = item
  end

  subroutine enqueue(this,item)
    class(t_queue), intent(inout) :: this
    integer, intent(in) :: item
    type(t_node), pointer :: node

    node => new_node(item)
    if (associated(this%head)) then
      node%prev => this%tail
      this%tail%next => node
    else
      this%head => node
    end if
    this%tail => node
    this%num = this%num+1
  end

  function dequeue(this) result(item)
    class(t_queue), intent(inout) :: this
    integer :: item
    type(t_node), pointer :: node

    item = this%head%item
    node => this%head%next
    deallocate(this%head)
    this%head => node
    if (associated(node)) then
      node%prev => null()
    else
      this%tail => null()
    end if
    this%num = this%num-1
  end

  function peek(this) result(item)
    class(t_queue), intent(in) :: this
    integer :: item

    item = this%head%item
  end

  subroutine clear(this)
    class(t_queue), intent(inout) :: this
    type(t_node), pointer :: node, next

    if (.not.associated(this%head)) return
    node => this%head
    do while (associated(node%next))
      next => node%next
      deallocate(node)
      node => next
    end do
    this%head => null()
    this%tail => null()
    this%num = 0
  end

  integer function size_of(this)
    class(t_queue), intent(in) :: this

    size_of = this%num
  end

end module mod_queue
program D
   use UnionFind
   use mod_queue
   implicit none
   integer::N,M,Q
   integer::A,B,C
   type(t_queue)::U,V
   integer::i
   read*,N,M,Q

   allocate(par(0:N-1))
   do i=0,N-1
      par(i)=i
   end do
   R=N

   do i=1,Q
      read*,A,B,C
      select case(C)
      case(0)
         call unite(A,B)
      case(1)
         call enqueue(U,A)
         call enqueue(V,B)
      end select
   end do
   if(M==N-1)then
      print"(A)",trim(merge("Yes","No ",size_of(U)==0))
      stop
   endif

   do while(size_of(U)/=0)
      if(same(dequeue(U),dequeue(V)))then
         print"(A)","No"
         stop
      endif
   end do

   if(M<=N+R*(R-3)/2)then
      print"(A)","Yes"
   else
      print"(A)","No"
   endif
end program D

Score : 700 points

Problem Statement

Snuke's mother gave Snuke an undirected graph consisting of N vertices numbered 0 to N-1 and M edges.
This graph was connected and contained no parallel edges or self-loops.

One day, Snuke broke this graph.
Fortunately, he remembered Q clues about the graph.
The i-th clue (0 \leq i \leq Q-1) is represented as integers A_i,B_i,C_i and means the following:

If C_i=0: there was exactly one simple path (a path that never visits the same vertex twice) from Vertex A_i to B_i.

If C_i=1: there were two or more simple paths from Vertex A_i to B_i.

Snuke is not sure if his memory is correct, and worried whether there is a graph that matches these Q clues.
Determine if there exists a graph that matches Snuke's memory.

Constraints

2 \leq N \leq 10^5

N-1 \leq M \leq N \times (N-1)/2

1 \leq Q \leq 10^5

0 \leq A_i,B_i \leq N-1

A_i \neq B_i

0 \leq C_i \leq 1

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M Q
A_0 B_0 C_0
A_1 B_1 C_1
\vdots
A_{Q-1} B_{Q-1} C_{Q-1}

Output

If there exists a graph that matches Snuke's memory, print Yes; otherwise, print No.

Sample Input 1

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

Sample Output 1

Yes

For example, consider a graph with edges (0,1),(1,2),(1,4),(2,3),(2,4). This graph matches the clues.

Sample Input 2

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

Sample Output 2

No

Sample Input 3

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

Sample Output 3

No


      program answer
      implicit none
      character(len=6) :: S
      read(*,*) S
      if( S == 'Sunny') write(*,'(A)') 'Cloudy'
      if( S == 'Cloudy') write(*,'(A)') 'Rainy'
      if( S == 'Rainy') write(*,'(A)') 'Sunny'
      stop
      end program answer

Score: 100 points

Problem Statement

The weather in Takahashi's town changes day by day, in the following cycle: Sunny, Cloudy, Rainy, Sunny, Cloudy, Rainy, ...

Given is a string S representing the weather in the town today. Predict the weather tomorrow.

Constraints

S is Sunny, Cloudy, or Rainy.

Input

Input is given from Standard Input in the following format:

S

Output

Print a string representing the expected weather tomorrow, in the same format in which input is given.

Sample Input 1

Sunny

Sample Output 1

Cloudy

In Takahashi's town, a sunny day is followed by a cloudy day.

Sample Input 2

Rainy

Sample Output 2

Sunny


program main
    implicit none
    integer:: l, i,j,k
    character(100):: s
    read*, s
    l = len_trim(s)

    do i = 1,l
        if ((mod(i,2) == 0).and. (s(i:i) == 'R'))then
            print*, 'No'
            stop
        else if((mod(i,2) == 1) .and. (s(i:i) == 'L'))then
            print*, 'No'
            stop
        end if
    enddo

    print*, 'Yes'

end program

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 answer
      implicit none
      integer(8) :: N, K, Q
      integer(8),allocatable,dimension(:) :: A,num
      integer(8) :: 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 sample
	implicit none
    integer ::a

    read(*,*) a
    write(*,*) a**3
    stop
end program sample

Score : 100 points

Problem Statement

Takahashi is going to set a 3-character password.

How many possible passwords are there if each of its characters must be a digit between 1 and N (inclusive)?

Constraints

1 \leq N \leq 9

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the number of possible passwords.

Sample Input 1

2

Sample Output 1

8

There are eight possible passwords: 111, 112, 121, 122, 211, 212, 221, and 222.

Sample Input 2

1

Sample Output 2

1

There is only one possible password if you can only use one kind of character.


program main
integer::n,i,k=0
integer, allocatable::a(:),b(:),c(:)
read*,n
allocate(a(n),b(n),c(n-1))
read*,a,b,c
do i=1,n
k=k+b(a(i))
if(a(i)==a(i-1)+1)then
k=k+c(a(i-1))
end if
end do
print*,k
end program main


program main
    implicit none
    integer n
    read*, n

    print*, n*n*n

end program


program ya
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: n,i,ans=0
    integer(int32), allocatable:: a(:),b(:),c(:)

    read*, n
    allocate(a(n), b(n), c(n-1))
    read*, a(:)
    read*, b(:)
    read*, c(:)

    do i=1,n
        ans=ans+b(a(i))
    end do
    do i=1,n-1
        if (a(i)+1==a(i+1)) ans=ans+c(a(i))
    end do
    print'(i0)', ans
end program ya

Score : 200 points

Problem Statement

Takahashi went to an all-you-can-eat buffet with N kinds of dishes and ate all of them (Dish 1, Dish 2, \ldots, Dish N) once.

The i-th dish (1 \leq i \leq N) he ate was Dish A_i.

When he eats Dish i (1 \leq i \leq N), he gains B_i satisfaction points.

Additionally, when he eats Dish i+1 just after eating Dish i (1 \leq i \leq N - 1), he gains C_i more satisfaction points.

Find the sum of the satisfaction points he gained.

Constraints

All values in input are integers.

2 \leq N \leq 20

1 \leq A_i \leq N

A_1, A_2, ..., A_N are all different.

1 \leq B_i \leq 50

1 \leq C_i \leq 50

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N
B_1 B_2 ... B_N
C_1 C_2 ... C_{N-1}

Output

Print the sum of the satisfaction points Takahashi gained, as an integer.

Sample Input 1

3
3 1 2
2 5 4
3 6

Sample Output 1

14

Takahashi gained 14 satisfaction points in total, as follows:

First, he ate Dish 3 and gained 4 satisfaction points.

Next, he ate Dish 1 and gained 2 satisfaction points.

Lastly, he ate Dish 2 and gained 5 + 3 = 8 satisfaction points.

Sample Input 2

4
2 3 4 1
13 5 8 24
45 9 15

Sample Output 2

74

Sample Input 3

2
1 2
50 50
50

Sample Output 3

150


program test
implicit none
  integer,allocatable::a(:),b(:),c(:)
  integer :: N,i,ans
  read*,N
  allocate(a(N))
  allocate(b(N))
  allocate(c(N-1))
  read(*,*)(a(i),i=1,N)
  read(*,*)(b(i),i=1,N)
  read(*,*)(c(i),i=1,N-1)
  ans=0
  do i=1,N
  	if(a(i+1)==a(i)+1)then
  		ans=ans+b(a(i))+c(a(i))
    else
    	ans=ans+b(a(i))
    end if

  end do
 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_B

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

  subroutine task_B

    ! variables for this <subroutine>
    integer(INT32) :: num_dishes
    integer(INT32) :: sum_satisfaction

    ! arrays for this <subroutine>
    integer(INT32), dimension(:), allocatable :: list_turn
    integer(INT32), dimension(:), allocatable :: list_satisfaction
    integer(INT32), dimension(:), allocatable :: list_satisfaction_additional

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

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

    ! STEP.02
    ! allocate the arrays to store the given data
    allocate( list_turn(1:num_dishes) )
    allocate( list_satisfaction(1:num_dishes) )
    allocate( list_satisfaction_additional(1:num_dishes - 1) )

    ! STEP.03
    ! read out the given data
    read(unit=INPUT_UNIT, fmt=*) list_turn(:)
    read(unit=INPUT_UNIT, fmt=*) list_satisfaction(:)
    read(unit=INPUT_UNIT, fmt=*) list_satisfaction_additional(:)

    ! STEP.04
    ! calculate the sum of the satisfaction
    sum_satisfaction = list_satisfaction( list_turn(1) )

    do itr = 2, num_dishes, 1

      sum_satisfaction = sum_satisfaction + list_satisfaction( list_turn(itr) )

      if ( list_turn(itr - 1) + 1_INT32 .eq. list_turn(itr) ) then
        sum_satisfaction = sum_satisfaction + list_satisfaction_additional( list_turn(itr - 1) )
      end if

    end do

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

    ! STEP.06
    ! deallocate the arrays to store the given data
    deallocate( list_turn )
    deallocate( list_satisfaction )
    deallocate( list_satisfaction_additional )

    ! STEP.END
    return

  end subroutine task_B

end module ABC140

program main

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

  ! require all variables to be explicitly declared
  implicit none

  call task_B

end program main


program ccc

implicit none

integer :: n, i
integer,allocatable,dimension(:) :: a, b

read(*,*) n

allocate(a(n))
allocate(b(n-1))

read(*,*) b

a(1) = b(1)
a(n) = b(n-1)

do i = 2, n-1
a(i) = min(b(i-1),b(i))
end do

write(*,'(i0)') sum(a)

end program

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 maximal_value
  implicit none
  integer :: n, b(100) = 0, a(100) = 0, ans, i
  read(*,*) n
  read(*,*) b(1:n-1)
  a(1) = b(1)
  a(n) = b(n-1)
  do i = 2, n-1
    a(i) = min(b(i-1),b(i))
  end do
  write(*,'(i0)') sum(a(1:n))
end program maximal_value


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

    cnum(n+1) = cnum(1)*(-1)
    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, hap + min(k,min(l,r))*2

end program

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 tenki
    implicit none
    integer :: i, cnt = 0
    character s(3), t(3)
    read *, s
    read *, t
    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


program main
	character*3::s,t
    integer::ans=0
    integer i
    read *,s
    read *,t
    do i=1,3
    	if(s(i:i)==t(i:i)) then
        	ans=ans+1
        end if
    end do
    print '(i0)',ans
end program main


program test
implicit none
character*3,S,T
integer::i,ans
read*,S,T
ans=0
do i=1,3
 if(S(i:i)==T(i:i))then
 	ans=ans+1
 else
 	ans=ans
 end if
end do
print*,ans
end program test


program bbb

implicit none
integer :: i, a, b, res, sum

read(*,*) a, b

sum = 1
res = 0

do i = 1, b
sum = sum + (a-1)
res = res + 1
if (sum>=b) exit
end do

write(*,'(i0)') res

end program

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


program main
integer :: i = 0,a,b

read *,a,b

do
if (b == 1) exit
i = i + 1
if ((a-1)*i + 1 >= b) exit
end do

print *,i

end program


program main
integer :: ans = 0,i = 0,a,b

read *,a,b
do
i = i + 1
if ((a-1)*i + 1 >= b) exit
end do

print *,i
end program


program ccc

implicit none
integer(8) :: i, n, res1, res2
integer(8),allocatable,dimension(:) :: h

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

res1 = 0
res2 = 0

do i=n,2,-1
if (h(i-1)>=h(i)) then
res1 = res1 + 1
res2 = max(res1,res2)
else
res1 = 0
end if
end do

write(*,'(i0)') res2

end program

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(8) :: N
  integer(8) :: i,num
  read *, N
  num = 0
  do n = 1, N
    num = num + (n-1)
  enddo
  print *, num
end program

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 main
  implicit none
  integer :: n
  read (*, *) n
  write (*, "(i0)") (n * (n - 1)) / 2
end program main


program main
    implicit none
    integer:: N,i, no, day
    logical:: battle, flag
    integer, allocatable:: A(:,:), noind(:)
    logical, allocatable:: isbattled(:)
    no = 0
    read*, N
    allocate(A(N,N-1), noind(N), isbattled(N))

    do i=1,N
        read*, A(i,1:N-1)
    enddo
    noind(1:N) = 1
    day = 1
    battle = .true.
    do while(.true.)
        ! print*, '--- ---day', day, battle
        battle = .false.
        isbattled(1:N) = .false.
        do i = 1, N
            if((noind(i)) == N) cycle
            if(isbattled(i) .eqv. .true.) cycle
            no = A(i, noind(i))
            if(isbattled(no) .eqv. .true.) cycle

            if(A(no, noind(no)) == i)then
                ! print*, i , 'vs' ,no
                noind(i) = noind(i) + 1
                noind(no) = noind(no) + 1
                isbattled(i) = .true.
                isbattled(no) = .true.
                battle = .true.
            endif
        enddo

        if (.not. battle) then
            day = day - 1
            exit
        end if

        day = day + 1
    enddo

    ! print*, noind
    flag = .true.
    do i = 1, N
        if(noind(i) /= N)then
            flag = .false.
        endif
    enddo

    if(flag) then
        print'(i0)',day
    else
        print*, '-1'
    endif

end program

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 engines
  implicit none
  integer :: n, i, j
  real(8) :: x(100) = 0.d0, y(100) = 0.d0, a, b, ans = 0.d0
  character(64) :: tmp
  read(*,*) n
  do i = 1, n
    read(*,*) x(i), y(i)
  end do
  do i = 1, n
    a = x(i)
    b = y(i)
    do j = 1, n
      if (j == i) cycle
      if (d(a+x(j),b+y(j)) > d(a,b)) then
        a = a+x(j)
        b = b+y(j)
      end if
    end do
    ans = max(ans,d(a,b))
  end do
  write(tmp,'(f32.16)') ans
  write(*,'(a)') trim(adjustl(tmp))
contains
  real(8) function d(x,y)
    real(8), intent(in) :: x, y
    d = sqrt(x*x+y*y)
  end
end program engines

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  ::  M,D
    integer  ::  dd(2)
integer  ::  num,i,j,k

read(*,*) M,D

if(D<22) then
    write(*,*) 0
    stop
end if

!write(*,*) M,D,dd(1),dd(2)

num=0
do i=1,M
    do j=22,D
        dd(1)=j-(j/10)*10
        dd(2)=j/10
        write(*,*) dd(2),dd(1)
        if (dd(1)>1 .and. dd(1)*dd(2)==i) then
            num=num+1
        end if
    end do
end do

write(*,*) num

stop
contains

end program kadai

Score : 200 points

Problem Statement

Today is August 24, one of the five Product Days in a year.

A date m-d (m is the month, d is the date) is called a Product Day when d is a two-digit number, and all of the following conditions are satisfied (here d_{10} is the tens digit of the day and d_1 is the ones digit of the day):

d_1 \geq 2

d_{10} \geq 2

d_1 \times d_{10} = m

Takahashi wants more Product Days, and he made a new calendar called Takahashi Calendar where a year consists of M month from Month 1 to Month M, and each month consists of D days from Day 1 to Day D.

In Takahashi Calendar, how many Product Days does a year have?

Constraints

All values in input are integers.

1 \leq M \leq 100

1 \leq D \leq 99

Input

Input is given from Standard Input in the following format:

M D

Output

Print the number of Product Days in a year in Takahashi Calender.

Sample Input 1

15 40

Sample Output 1

10

There are 10 Product Days in a year, as follows (m-d denotes Month m, Day d):

4-22

6-23

6-32

8-24

9-33

10-25

12-26

12-34

14-27

15-35

Sample Input 2

12 31

Sample Output 2

5

Sample Input 3

1 1

Sample Output 3

0


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

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

    num=0
    big=0
    small=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=1

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

    num1=mod(kari*small,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.


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

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

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

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

end function

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

function permutation(n,k,mo)
implicit none
  integer(16)::n,k
  integer(16)::mo,permutation
  permutation=mod(Kaijo(N)*inv(Kaijo(N-K),mo),mo)
end function
end module

use Factwithmod
integer(16) N
character(200000) S
integer(16) match
integer(16)ans,mo,i
mo=10**9+7
read*,N
read*,S
call initiationFactorial(N,mo)
match=0
ans=1
do i=1,2*N
  if(mod(I,2)==1)then
    if(S(I:I)=="B")then
      match=match+1
    else
      ans=ans*match
      match=match-1
    endif
  else
    if(S(I:I)=="W")then
      match=match+1
    else
      ans=mod(ans*match,mo)
      match=match-1
    endif
  end if
end do
if(match/=0)ans=0
ans=mod(ans*kaijo(N),mo)
print"(I0)",ans
end

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 main
	implicit none
    integer n,i
    double precision,allocatable::a(:)
    double precision::s=0
    read *, n
    allocate(a(n))
    read *, (a(i), i=1,n)
    do i=1,n
    	s=s+1/(1.0*a(i))
    end do
    print '(f0.12)',1/s
end program main

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.


module ABC138

  ! <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
  private :: calc_solution

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

  subroutine task_B

    ! variables for this <subroutine>
    integer :: len_data

    ! arrays for this <subroutine>
    real(REAL64), dimension(:), allocatable :: val_data

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

    ! STEP.02
    ! allocate the array to store the given data
    allocate( val_data(1:len_data) )

    ! STEP.03
    ! read out given data (value)
    read(unit=INPUT_UNIT, fmt=*) val_data(:)

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

    ! STEP.05
    ! deallocate the array to store the given data
    deallocate( val_data )

    ! STEP.END
    return

  end subroutine task_B

  pure function calc_solution (array) result(solution)

    ! arguments for this <function>
    real(REAL64), dimension(:), intent(in) :: array

    ! return value of this <function>
    real(REAL64) :: solution

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

    solution = 0.0_REAL64

    do itr = 1, size(array(:), dim=1), 1
      solution = solution + 1.0_REAL64 / array(itr)
    end do

    solution = 1.0_REAL64 / solution

  end function calc_solution

end module ABC138

program main

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

  ! require all variables to be explicitly declared
  implicit none

  call task_B

end program main


module UIntegerSort
  implicit none

  private

  public QuickSortInteger

contains

  !!
  ! ピボット値の取得
  !
  subroutine GetPivotIntegerValue(array, n, a, b, pivot_value)
    integer, intent(inout) :: array(:), pivot_value
    integer, intent(in) :: n, a, b

    pivot_value = array((a + b) / 2)
  end subroutine

  !!
  ! ピボット値に従って値を分離
  !
  subroutine GetPartitionInteger(array, n, a, b, pivot)
    integer, intent(inout) :: array(:)
    integer, intent(in)  :: n, a, b
    integer, intent(out) :: pivot

    integer :: buffer, pivot_value
    integer :: l, r

    if (a == b) then
      pivot = 0
      return
    end if

    call GetPivotIntegerValue(array, n, a, b, pivot_value)

    l = a
    r = b

    do while(.true.)
      do while(array(l) < pivot_value)
        if (l >= b) exit
        l = l + 1
      end do

      do while(pivot_value < array(r))
        if (r <= a) exit
        r = r - 1
      end do

      if(r <= l) exit

      buffer   = array(l)
      array(l) = array(r)
      array(r) = buffer

      l = l + 1
      r = r - 1
    end do

    pivot = r
  end subroutine

  !!
  ! 配列の一部分をソートする。
  !
  recursive subroutine QuickSortIntegerSubarray(array, n, a, b)
    integer, intent(inout) :: array(:)
    integer, intent(in)    :: n, a, b

    integer :: pivot

    call GetPartitionInteger(array, n, a, b, pivot)
    if (pivot /= 0) then
      call QuickSortIntegerSubarray(array, n, a, pivot)
      call QuickSortIntegerSubarray(array, n, pivot + 1, b)
    end if
  end subroutine

  !!
  ! 配列全体をソートする
  !
  subroutine QuickSortInteger(array, n)
    integer :: array(:)
    integer :: n

    call QuickSortIntegerSubarray(array, n, 1, n)
  end subroutine

end module

program TestIntegerSort
  use UIntegerSort

  implicit none
  integer,allocatable::a(:)
  integer :: N,i
  real(8)::ans
  read*,N
  allocate(a(N))
  read(*,*)(a(i),i=1,N)
  call QuickSortInteger(a, N)
  ans=a(1)
  do i=2,N
  ans=(ans+a(i))/2
  end do
print*,ans
end program

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 kadai
   implicit none
    character(len=100000)  ::  s
    character(len=100000)  ::  t
    character(len=1),allocatable  ::  sos(:)
    character(len=1),allocatable  ::  sot(:)
    integer  ::  i,all
    integer(8)  ::  count
    integer  ::  size_s,size_t
    integer  ::  flag,sflag,j

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

    size_s=len_trim(s)
    size_t=len_trim(t)
    allocate(sos(size_s))
    allocate(sot(size_t))
    !write(*,*) len_trim(s),len_trim(t)

    do i=1,size_s
        sos(i)=s(i:i)
    end do
    do i=1,size_t
        sot(i)=t(i:i)
    end do

    call qsort(sos)
    call qsort(sot)
    !write(*,*) sos
    !write(*,*) sot
    j=1
    flag=0
    sflag=0
    do i=1,size_s
        if (sos(i)==sot(j)) then
            do while(sos(i)==sot(j))
                flag=flag+1
                j=j+1
            end do
        end if
        if (j==size_t+1) then
            sflag=1
            exit
        end if
    end do

    if (sflag==0) then
        write(*,*) -1
        stop
    end if

    all=1
    count=1
    j=1
    do
        do i=1,size_s
            if (s(i:i)==t(j:j)) then
                j=j+1
            end if
            count=count+1
            if (j==size_t+1) then
                write(*,*) count-1
                stop
            end if
        end do
    end do

stop
contains

subroutine swap(a,b)
implicit none
character(len=*),intent(inout)  ::  a,b
character(len=10)  ::  c
c=a
a=b
b=c
end subroutine swap

subroutine qsort(x)
implicit none
character(len=*), intent(inout)  ::  x(:)
integer  ::  first,last
first=1
last=size(x)
call q2sort(x,first,last)
return
end subroutine qsort

recursive subroutine q2sort(x,first,last)
implicit none
character(len=*), intent(inout)  ::  x(:)
integer, intent(inout) ::  first,last
integer  ::  fir, las
integer  ::  i,j
character(len=10)  ::  n
n=x((first+last)/2)
i=first
j=last
do
do while (x(i)<n)
i=i+1
end do
do while(x(j)>n)
j=j-1
end do
if (i>j .or. i==j) then
exit
end if
call swap(x(i),x(j))
i=i+1
j=j-1
end do
if (first<i-1) then
las=i-1
call q2sort(x,first,las)
end if
if (last>j+1) then
fir=j+1
call q2sort(x,fir,last)
end if
end subroutine q2sort

end program kadai

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 prob3
    implicit none
    character(len=200005) :: s
    integer, allocatable :: dp1(:), dp2(:)
    integer :: n, m

    read(*,*) s
    n = len_trim(s)
    allocate(dp1(n))
    allocate(dp2(n))

    dp1(1) = 1
    dp1(2) = 1
    dp2(1) = 0
    dp2(2) = 1
    if(s(1:1) /= s(2:2)) then
        dp1(2) = dp1(2) + 1
    end if

    do m = 3, n
        if(s(m-1:m-1) == s(m:m)) then
            dp1(m) = dp2(m-1) + 1
        else
            dp1(m) = max(dp1(m-1) + 1, dp2(m-1) + 1)
        end if
        if(m == 3) then
            dp2(m) = dp1(m-2) + 1
        else if(s(m-3:m-2) == s(m-1:m)) then
            dp2(m) = dp2(m-2) + 1
        else
            dp2(m) = max(dp1(m-2) + 1, dp2(m-2) + 1)
        end if
    end do

    write(*,*) max(dp1(n), dp2(n))

    deallocate(dp2)
    deallocate(dp1)
    stop
contains
end program prob3

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


program AGC37_A
  character(200000)::S
  integer(8)::ans=1,N,i=1,L
  read(*,*)S
  N=len_trim(S)-1
  L=len_trim(S)
  do while(N>0)
     if(S(i:i)==S(i+1:i+1) .and. N>1) then
        N=N-2
        ans=ans+1
        i=i+2
        if(N/=2 .or. S(L-1:L-1)/=S(L:L)) then
           N=N-1
           ans=ans+1
           i=i+1
        else
           N=N-2
           ans=ans+1
           i=i+2
        end if
     else
        if(N/=2 .or. S(L-1:L-1)/=S(L:L)) then
           N=N-1
           ans=ans+1
           i=i+1
        else
           N=N-2
           ans=ans+1
           i=i+2
        end if
     end if
  end do
  write(*,*)ans
end program AGC37_A


program main
  implicit none
  integer :: n, i, cnt = 1
  character(200000) :: s
  logical :: skip = .false., skipped = .false.

  read *, s
  n = len_trim(s)

  do i = 2, n
    if (skip) then
      skip = .false.
      skipped = .true.
      cycle
    end if
    cnt = cnt + 1
    if (s(i:i) == s(i-1:i-1)) then
      if (skipped) then
        skipped = .false.
      else
        skip = .true.
      end if
    else
      skipped = .false.
    end if
  end do
  if (skip) cnt = cnt - 1
  print '(i0)', cnt
end program


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

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 main
  implicit none

  integer k,x,m,n,i

  read(*,*)k,x
  m = 1 - k
  n = k - 1

  write(*,*) (i + x, i = m, n)

end program main

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(8) :: n
    integer(8), allocatable :: a(:)
    integer(8) :: p(10)
    integer(8) :: s(10)
    integer(8) :: i, j
    integer(8) :: ans
    integer(8) :: cnt
    character(10) :: tmp
    integer(8) :: inttmp

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

    p(1) = 1
    do i = 2, 10
        p(i) = p(i-1) * 26
    end do
    do i = 1, n
        read(*,*) tmp
        do j = 1, 10
            s(j) = ichar(tmp(j:j)) - ichar('a')
        end do
        call msort(s)
        a(i) = 0
        do j = 1, 10
            a(i) = a(i) + s(j) * p(j)
        end do
    end do

    call msort(a)

    cnt = 1
    inttmp = a(1)
    ans = 0
    do i = 2, n
        if(a(i) == inttmp)then
            cnt = cnt + 1
        else
            ans = ans + cnt * (cnt - 1) / 2
            inttmp = a(i)
            cnt = 1
        end if
    end do
    ans = ans + cnt * (cnt - 1) / 2

    write(*,*) ans

    deallocate(a)

    stop
contains

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

    integer(8) :: c

    c = a
    a = b
    b = c

  end subroutine swap

  !
  ! merge sort
  !
  recursive subroutine msort(x)
    implicit none
    integer(8), intent(inout) :: x(:)

    integer(8) :: n, mid, i, j, k
    integer(8), allocatable :: tmp(:)

    n = size(x)

    if(n == 1) then
      return
    end if

    !
    ! 前半と後半に分けてソート
    ! 1~midとmid+1~nをそれぞれソート済みにする
    !
    mid = n / 2
    call msort(x(:mid))
    call msort(x(mid+1:))

    !
    ! tmpという配列にxを一時的に保管
    ! マージソートは外部ソート
    !
    allocate(tmp(n))
    tmp = x

    ! tmpの後半部分を左右反転
    do k = n, mid+1, -1
      tmp(k) = x(mid + n - k + 1)
    end do

    ! iは前半のリストのうちまだ見ていないものの最小値
    ! jは後半のリストのうちまだ見ていないものの最小値
    ! kは見た回数の合計
    i = 1
    j = n

    do k = 1, n
      !
      ! 一番左と一番右のうち小さい方をxに入れていく
      ! ここが等号つき不等号なので、安定ソートとなる
      !
      if(tmp(i) <= tmp(j)) then
        x(k) = tmp(i)
        i = i + 1
      else
        x(k) = tmp(j)
        j = j - 1
      end if
    end do

    deallocate(tmp)
    return
  end subroutine msort
end program prob4

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 ccc

implicit none
integer :: n, i, j
integer :: co, res1 = 0, res2 = 0
integer,dimension(26) :: alp = [2,3,5,7,11,13,17,19,&
&23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101]
integer,allocatable,dimension(:) :: bet
character(10),allocatable,dimension(:) :: s

read*, n
allocate(s(n))
allocate(bet(n))
bet = 1

read*, s

do i=1, n
 do j=1,10
 bet(i)=bet(i)*(alp(ichar(s(i)(j:j))-96))
 end do
end do

call heapsort(n,bet)

co=bet(1)
do i=2,n
 if(co==bet(i)) then
 res1=res1+1
 else
 co=bet(i)
 res2=res2+res1*(res1+1)/2
 res1=0
 end if
end do

res2=res2+res1*(res1+1)/2

write(*,'(i0)') res2

end program

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

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

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

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

  return
end subroutine heapsort


program kadai
implicit none
integer  ::  n
character(len=10),allocatable  ::  s(:)
character(len=10)  ::  hako
character(len=1)  ::  word(10)
integer  ::  i,j,k
integer(8)  ::  num
integer(8)  ::  ans

read(*,*) n

allocate(s(n))

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

do i=1,n
    hako=s(i)
    do j=1,10
        word(j)=hako(j:j)
    end do
    call qsort(word)
    do j=1,10
        hako(j:j)=word(j)
    end do
    s(i)=hako
end do

num=0
call qsort(s)

!do i=1,n
!   write(*,*) s(i)
!end do

i=1
num=0
ans=0
do while (i<n+1)
    j=i+1
    do while (j<n+1)
        if (s(i)==s(j)) then
            num=num+1
            j=j+1
        else
            exit
        end if
    end do
    ans=ans+(num+1)*(num)/2
    i=j
    num=0
end do

write(*,*) ans

stop
contains

subroutine swap(a,b)
implicit none
character(len=*),intent(inout)  ::  a,b
character(len=10)  ::  c
c=a
a=b
b=c
end subroutine swap

subroutine qsort(x)
implicit none
character(len=*), intent(inout)  ::  x(:)
integer  ::  first,last
first=1
last=size(x)
call q2sort(x,first,last)
return
end subroutine qsort

recursive subroutine q2sort(x,first,last)
implicit none
character(len=*), intent(inout)  ::  x(:)
integer, intent(inout) ::  first,last
integer  ::  fir, las
integer  ::  i,j
character(len=10)  ::  n
n=x((first+last)/2)
i=first
j=last
do
do while (x(i)<n)
i=i+1
end do
do while(x(j)>n)
j=j-1
end do
if (i>j .or. i==j) then
exit
end if
call swap(x(i),x(j))
i=i+1
j=j-1
end do
if (first<i-1) then
las=i-1
call q2sort(x,first,las)
end if
if (last>j+1) then
fir=j+1
call q2sort(x,fir,last)
end if
end subroutine q2sort

end program kadai


program kadai
implicit none
integer  ::  n,m
integer,allocatable  ::  a(:),b(:)
integer  ::  i,k,j
integer  ::  ans
integer  ::  loc

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

do i=1,n
    read(*,*) a(i),b(i)
end do

ans=0
call qsort(a,b)
j=1

do while (m>0)
    !write(*,*) a
    !write(*,*) ""
    do i=j,n
        if (a(i)>m .or. a(i)<0) then
            a(i)=0
            b(i)=0
            j=j+1
        else
            exit
        end if
    end do
    if (a(j)==0) then
        write(*,*) ans,j,"koko"
        stop
    end if
    !write(*,*) a
    !write(*,*) ""

    loc=maxloc(b,1)
    ans=ans+maxval(b)
    a(loc)=-1
    b(loc)=0
    m=m-1

    !write(*,*) loc,ans,m

end do

write(*,*) ans

stop

contains

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

subroutine qsort(x,y)
implicit none
integer, intent(inout)  ::  x(:)
integer, intent(inout)  ::  y(:)
integer  ::  first,last
first=1
last=size(x)
call q2sort(x,y,first,last)
return
end subroutine qsort

recursive subroutine q2sort(x,y,first,last)
implicit none
integer, intent(inout)  ::  x(:)
integer, intent(inout)  ::  y(:)
integer, intent(inout) ::  first,last
integer  ::  fir, las
integer  ::  i,j
integer  ::  n
    n=x((first+last)/2)
    i=first
    j=last
    do
        do while (x(i)>n)
            i=i+1
        end do
        do while(x(j)<n)
            j=j-1
        end do
        if (i>j .or. i==j) then
            exit
        end if
        call swap(x(i),x(j))
        call swap(y(i),y(j))
        i=i+1
        j=j-1
    end do
    if (first<i-1) then
        las=i-1
        call q2sort(x,y,first,las)
    end if
    if (last>j+1) then
        fir=j+1
        call q2sort(x,y,fir,last)
    end if
end subroutine q2sort

end program kadai

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


program kadai
implicit none
integer  ::  n,m
integer,allocatable  ::  a(:),b(:)
integer  ::  i,k,j
integer  ::  ans
integer  ::  loc

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

do i=1,n
    read(*,*) a(i),b(i)
end do

ans=0
call qsort(a,b)
j=1

do while (m>0)
    !write(*,*) a
    !write(*,*) ""
    if (a(1)==0) then
        write(*,*) ans
        stop
    end if
    do i=j,n
        if (a(i)>M) then
            a(i)=0
            b(i)=0
            j=j+1
        else
            exit
        end if
    end do
    !write(*,*) a
    !write(*,*) ""

    loc=maxloc(b,1)
    ans=ans+maxval(b)
    a(loc)=0
    b(loc)=0
    m=m-1

    !write(*,*) loc,ans,m

end do

write(*,*) ans

stop

contains

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

subroutine qsort(x,y)
implicit none
integer, intent(inout)  ::  x(:)
integer, intent(inout)  ::  y(:)
integer  ::  first,last
first=1
last=size(x)
call q2sort(x,y,first,last)
return
end subroutine qsort

recursive subroutine q2sort(x,y,first,last)
implicit none
integer, intent(inout)  ::  x(:)
integer, intent(inout)  ::  y(:)
integer, intent(inout) ::  first,last
integer  ::  fir, las
integer  ::  i,j
integer  ::  n
    n=x((first+last)/2)
    i=first
    j=last
    do
        do while (x(i)>n)
            i=i+1
        end do
        do while(x(j)<n)
            j=j-1
        end do
        if (i>j .or. i==j) then
            exit
        end if
        call swap(x(i),x(j))
        call swap(y(i),y(j))
        i=i+1
        j=j-1
    end do
    if (first<i-1) then
        las=i-1
        call q2sort(x,y,first,las)
    end if
    if (last>j+1) then
        fir=j+1
        call q2sort(x,y,fir,last)
    end if
end subroutine q2sort

end program kadai


program kadai
implicit none
integer  ::  n,m
integer,allocatable  ::  a(:),b(:)
integer  ::  i,k
integer  ::  ans
integer  ::  loc

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

do i=1,n
    read(*,*) a(i),b(i)
end do

ans=0

do while (m>0)
    call qsort(a,b)
    !write(*,*) a
    !write(*,*) ""
    if (a(1)==0) then
        write(*,*) ans
        stop
    end if
    do i=1,n
        if (a(i)>M) then
            a(i)=0
            b(i)=0
        else
            exit
        end if
    end do
    !write(*,*) a
    !write(*,*) ""

    loc=maxloc(b,1)
    ans=ans+maxval(b)
    a(loc)=0
    b(loc)=0
    m=m-1

    !write(*,*) loc,ans,m

end do

write(*,*) ans

stop

contains

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

subroutine qsort(x,y)
implicit none
integer, intent(inout)  ::  x(:)
integer, intent(inout)  ::  y(:)
integer  ::  first,last
first=1
last=size(x)
call q2sort(x,y,first,last)
return
end subroutine qsort

recursive subroutine q2sort(x,y,first,last)
implicit none
integer, intent(inout)  ::  x(:)
integer, intent(inout)  ::  y(:)
integer, intent(inout) ::  first,last
integer  ::  fir, las
integer  ::  i,j
integer  ::  n
    n=x((first+last)/2)
    i=first
    j=last
    do
        do while (x(i)>n)
            i=i+1
        end do
        do while(x(j)<n)
            j=j-1
        end do
        if (i>j .or. i==j) then
            exit
        end if
        call swap(x(i),x(j))
        call swap(y(i),y(j))
        i=i+1
        j=j-1
    end do
    if (first<i-1) then
        las=i-1
        call q2sort(x,y,first,las)
    end if
    if (last>j+1) then
        fir=j+1
        call q2sort(x,y,fir,last)
    end if
end subroutine q2sort

end program kadai


integer N,M,x,y,maxhi
integer,allocatable,dimension(:,:)::A
integer,allocatable,dimension(:)::B
integer(16)ans
read*,N,M
allocate(A(1:M,0:N),B(M))
A=0
B=0
do i=1,N
  read*,x,y
  if(x<=M)then
    B(x)=B(x)+1
    A(x,B(x))=y
  endif
end do
ans=0
do i=1,M
  if(B(i)/=0)then
    call Heapsort(B(i),A(i,1:B(i)))
  end if
  maxhi=0
  do j=1,i
    maxhi=max(maxhi,A(j,B(j)))
  end do
  if(maxhi/=0)then
    do j=1,i
      if(maxhi==A(j,B(j)))then
        B(j)=B(j)-1
        exit
      endif
    end do
  endif
  ans=ans+maxhi
end do

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

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


program main
	implicit none
    integer::a,b,c
    read(*,*)a,b,c
    write(*,*) max(c-(a-b),0)
    stop
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 :: n, i, l, res
  character(10) :: s
  read (*, *) n
  res = 0
  do i = 1, n
    write (s, "(i0)") i
    l = len(trim(s))
    res = res + mod(l, 2)
  end do
  write (*, "(i0)") res
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 abc136b
   implicit none
   integer :: n
   integer :: digit, counter

   read(*,*) n

   digit = int(log10(real(n))) + 1

   if ( digit == 6 ) then
      write(*,*) 90909
      stop
   endif

   counter = 0
   if ( digit == 5 ) then
      counter =  (n - 9999)
      counter = counter + 909
   else if (digit == 4) then
      counter = 909
   else if ( digit == 3) then
      counter =  (n - 99) + 9
   else if ( digit == 2) then
      counter = 9
   else if (digit == 1 ) then
      counter = n
   end if

   write(*,*) counter

end program abc136b


program uneven_numbers
  implicit none
  integer :: n, ans = 0
  read(*,*) n
  if (n > 0) ans = ans + min(9,n)
  if (n > 99) ans = ans + min(900,n-99)
  if (n > 9999) ans = ans + min(90000,n-9999)
  write(*,'(i0)') ans
  stop
end program uneven_numbers


program sample
    implicit none
    character(200000)::s
    integer(8) :: i,j,m,n
    integer(8),allocatable :: x(:),y(:)

    read(*,*) s
    n=len_trim(s)
    allocate(x(n))
    x(:)=1
    do i=1,n-1

        if(s(i:i+1) .eq. 'RR')then
            x(i+2)=x(i+2)+x(i)
            x(i)=0
        end if
    end do
    do i=n,2,-1

        if(s(i-1:i) .eq. 'LL')then
            x(i-2)=x(i-2)+x(i)
            x(i)=0
        end if
    end do

    write(*,*) x

    stop
end program sample

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 max_gcd
  implicit none
  integer :: n, k, a(500) = 0, s, d(100000) = 0, num = 0, i, j
  integer :: x(500) = 0, 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 = 10**9
    q = sum(d(i)-x(1:n))
    p = 0
    do j = 1, n
      p = p+x(j)
      q = q-(d(i)-x(j))
      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 n, d
    integer km, i
    integer, allocatable :: m(:), y(:)
    read*,n
    allocate(m(n+1), y(n))

    read*, m
    read*, y

    km = 0

    do i=1,n
        d = y(i) - m(i)
        if(d>=0)then
            km = km + m(i)
            y(i) = y(i) - m(i)
            m(i) = 0

        else
            km = km + y(i)
            m(i) = m(i) - y(i)
            y(i) = 0
        endif

        if ( y(i)>0) then
            if(y(i) > m(i+1))then
                km = km + m(i+1)
                y(i) = y(i) - m(i+1)
                m(i+1) = 0

            else
                km =  km + y(i)
                m(i+1) = m(i+1) - y(i)
                y(i) = 0
            endif

        end if
    enddo
    print'(i0)', km

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 abc135a
   implicit none
   integer :: a, b, k

   read(*,*) a, b

   if (mod(a+b,2)==0) then
      k = (a+b)/2
      write(*,*) k
   else
      write(*,*) "IMPOSSIBLE"
   end if

end program abc135a


implicit none

integer :: N
integer, allocatable :: p(:)
integer :: i,s

read *, N
allocate(p(N))

read *, (p(i),i=1,N)

s = 0
do i = 1, N
   if (p(i) /= i) then
      s = s + 1
   endif
enddo

if (s == 0 .or. s == 2) then
   write(6,'(a)') 'YES'
else
   write(6,'(a)') 'NO'
endif
stop
end

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 answer
  implicit none
  integer(8) :: i, N
  integer, allocatable :: A(:), B(:), ato(:)
  read(*,*) N
  allocate(A(N+1),B(N),ato(N+1))
  read(*,*) A
  read(*,*) B
  ato=A
  do i = 1, N
     if(ato(i)<=B(i)) then
        if(ato(i+1)+ato(i)<=B(i)) then
           ato(i)=0
           ato(i+1)=0
         else
           ato(i+1)=ato(i+1)-(B(i)-ato(i))
           ato(i)=0
        end if
     else
        ato(i)=ato(i)-B(i)
     end if
  end do

  write(*,*) sum(A)-sum(ato)
  stop
 end program answer

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 prob1
    implicit none
    integer(8) :: n, i, ans
    integer(8), allocatable :: a(:), b(:)
    read(*,*) n
    allocate(a(n+1), b(n))
    read(*,*) a
    read(*,*) b
    ans = 0
    do i = 1, n
        if(a(i) >= b(i)) then
            ans = ans + b(i)
            a(i) = a(i) - b(i)
            b(i) = 0
        else
            ans = ans + a(i)
            b(i) = b(i) - a(i)
            a(i) = 0
        end if
        if(a(i+1) >= b(i)) then
            ans = ans + b(i)
            a(i+1) = a(i+1) - b(i)
            b(i) = 0
        else
            ans = ans + a(i+1)
            b(i) = b(i) - a(i+1)
            a(i+1) = 0
        end if
    end do
    write(*,*) ans

    stop
contains
end program prob1


program digits_parade
  implicit none
  integer(8), parameter :: md = 1000000007_8
  character(100000) :: s
  integer :: n, i, j, k, l = 0
  integer(8) :: dp(0:1,0:12) = 0_8
  read(*,*) s
  s = trim(adjustl(s))
  n = len_trim(s)
  dp(0,0) = 1_8
  do i = 1, n
    l = 1-l
    dp(l,:) = 0_8
    if (s(i:i) == "?") then
      do k = 0, 9
        do j = 0, 12
          dp(l,mod(10*j+k,13)) = mod(dp(l,mod(10*j+k,13))+dp(1-l,j),md)
        end do
      end do
    else
      k = ichar(s(i:i))-48
      do j = 0, 12
        dp(l,mod(10*j+k,13)) = dp(1-l,j)
      end do
    end if
  end do
  write(*,'(i0)') dp(l,5)
  stop
end program digits_parade

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


Score : 400 points

Problem Statement

Given is an integer S.
Find a combination of six integers X_1,Y_1,X_2,Y_2,X_3, and Y_3 that satisfies all of the following conditions:

0 \leq X_1,Y_1,X_2,Y_2,X_3,Y_3 \leq 10^9

The area of the triangle in a two-dimensional plane whose vertices are (X_1,Y_1),(X_2,Y_2), and (X_3,Y_3) is S/2.

We can prove that there always exist six integers that satisfy the conditions under the constraints of this problem.

Constraints

1 \leq S \leq 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

S

Output

Print six integers X_1,Y_1,X_2,Y_2,X_3, and Y_3 that satisfy the conditions, in this order, with spaces in between.
If multiple solutions exist, any of them will be accepted.

Sample Input 1

3

Sample Output 1

1 0 2 2 0 1

The area of the triangle in a two-dimensional plane whose vertices are (1,0),(2,2), and (0,1) is 3/2.
Printing 3 0 3 1 0 1 or 1 0 0 1 2 2 will also be accepted.

Sample Input 2

100

Sample Output 2

0 0 10 0 0 10

Sample Input 3

311114770564041497

Sample Output 3

314159265 358979323 846264338 327950288 419716939 937510582


program triangle
  implicit none
  integer(8) :: s, h, w, a, b, d
  integer(8) :: x1, y1, x2, y2, x3, y3, i
  read(*,*) s
  x1 = 0_8
  y1 = 0_8
  h = int(ceiling(sqrt(real(s,8))),8)
  w = h
  d = h*w-s
  a = 0_8
  b = 0_8
  if (d > 0_8) then
    a = int(floor(sqrt(real(d,8))),8)
    do while (a > 0_8)
      if (mod(d,a) == 0_8) exit
      a = a-1_8
    end do
    b = d/a
  end if
  x2 = a
  y2 = h
  x3 = w
  y3 = b
  write(*,'(i0,x,i0,x,i0,x,i0,x,i0,x,i0)') x1, y1, x2, y2, x3, y3
  stop
end program triangle


program aaa
implicit none
integer(8) :: r
read*, r
write(*,'(i0)') 3*r**2
end program

Score : 100 points

Problem Statement

It is known that the area of a regular dodecagon inscribed in a circle of radius a is 3a^2.

Given an integer r, find the area of a regular dodecagon inscribed in a circle of radius r.

Constraints

1 \leq r \leq 100

r is an integer.

Input

Input is given from Standard Input in the following format:

r

Output

Print an integer representing the area of the regular dodecagon.

Sample Input 1

4

Sample Output 1

48

The area of the regular dodecagon is 3 \times 4^2 = 48.

Sample Input 2

15

Sample Output 2

675

Sample Input 3

80

Sample Output 3

19200


program main
implicit none
integer :: i, j, k
integer :: n, d

read(*,*) n, d
i = (n-1)/(2*d) +1
write(*,'(i0)') i

end program main

Score : 200 points

Problem Statement

There are N apple trees in a row. People say that one of them will bear golden apples.

We want to deploy some number of inspectors so that each of these trees will be inspected.

Each inspector will be deployed under one of the trees. For convenience, we will assign numbers from 1 through N to the trees. An inspector deployed under the i-th tree (1 \leq i \leq N) will inspect the trees with numbers between i-D and i+D (inclusive).

Find the minimum number of inspectors that we need to deploy to achieve the objective.

Constraints

All values in input are integers.

1 \leq N \leq 20

1 \leq D \leq 20

Input

Input is given from Standard Input in the following format:

N D

Output

Print the minimum number of inspectors that we need to deploy to achieve the objective.

Sample Input 1

6 2

Sample Output 1

2

We can achieve the objective by, for example, placing an inspector under Tree 3 and Tree 4.

Sample Input 2

14 3

Sample Output 2

2

Sample Input 3

20 4

Sample Output 3

3


program main
  implicit none
  integer :: n, i, largest, second
  integer, allocatable :: a(:)
  read (*, *) n
  allocate (a(n))
  read (*, *) (a(i), i = 1, n)
  largest = 0
  second = 0
  do i = 1, n
    if (a(i) > largest) then
      second = largest
      largest = a(i)
    else if (a(i) > second) then
      second = a(i)
    end if
  end do

  do i = 1, n
    if (a(i) == largest) then
      write (*, "(i0)") second
    else
      write (*, "(i0)") largest
    end if
  end do
end program main

Score : 300 points

Problem Statement

You are given a sequence of length N: A_1, A_2, ..., A_N.
For each integer i between 1 and N (inclusive), answer the following question:

Find the maximum value among the N-1 elements other than A_i in the sequence.

Constraints

2 \leq N \leq 200000

1 \leq A_i \leq 200000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

Print N lines. The i-th line (1 \leq i \leq N) should contain the maximum value among the N-1 elements other than A_i in the sequence.

Sample Input 1

3
1
4
3

Sample Output 1

4
3
4

The maximum value among the two elements other than A_1, that is, A_2 = 4 and A_3 = 3, is 4.

The maximum value among the two elements other than A_2, that is, A_1 = 1 and A_3 = 3, is 3.

The maximum value among the two elements other than A_3, that is, A_1 = 1 and A_2 = 4, is 4.

Sample Input 2

2
5
5

Sample Output 2

5
5


program abc134c2
   implicit none
   integer(8) :: n, i, lowMax, ans, highmax
   integer(8), allocatable :: a(:)

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

   highmax = 0
   lowMax = 0
   do i = 1, n
      if (i ==1) then
         write(*,*) maxval(a(2:n))
         lowMax = a(1)
         cycle
      else if ( i == n) then
         write(*,*) lowMax
         cycle
      endif

      !lowMax = max(lowmax, a(i-1))
      highmax =  maxval(a(i+1:n))
      write(*,*) max(lowMax, highMax)
      lowMax = max(lowmax, a(i))
   enddo

end