module mod_tree_set
  implicit none
  type t_node
    private
    type(t_node), pointer :: left => null(), right => null()
    integer :: level = 1
    integer :: e = 0
  end type
  type t_tree_set
    private
    type(t_node), pointer :: root => null()
    integer :: deflt = -1
  contains
    procedure :: size => size_of
    procedure :: clear => clear
    procedure :: add => add
    procedure :: remove => remove
    procedure :: contain => contain
    procedure :: first => firste
    procedure :: poll_first => poll_first
    procedure :: last => laste
    procedure :: poll_last => poll_last
    procedure :: floor => floore
    procedure :: lower => lowere
    procedure :: ceiling => ceilinge
    procedure :: higher => highere
    procedure :: element_list => element_list
  end type
contains
  function new_node(e) result(n)
    integer, intent(in) :: e
    type(t_node), pointer :: n
    n => null()
    allocate(n)
    n%e = e
  end
  integer function level(n)
    type(t_node), pointer, intent(in) :: n
    level = 0
    if (.not.associated(n)) return
    level = n%level
  end
  logical function is_leaf(n)
    type(t_node), pointer, intent(in) :: n
    is_leaf = associated(n) .and. .not.associated(n%left) .and. &
    & .not.associated(n%right)
  end
  recursive function tree_size(n) result(s)
    type(t_node), pointer, intent(in) :: n
    integer :: s
    s = 0
    if (.not.associated(n)) return
    s = 1+tree_size(n%left)+tree_size(n%right)
  end
  function skew(n) result(l)
    type(t_node), pointer, intent(in) :: n
    type(t_node), pointer :: l
    l => n
    if (.not.(associated(n) .and. associated(n%left) .and. &
    &   n%left%level == n%level)) return
    l => n%left
    n%left => l%right
    l%right => n
  end
  function split(n) result(r)
    type(t_node), pointer, intent(in) :: n
    type(t_node), pointer :: r
    r => n
    if (.not.(associated(n) .and. associated(n%right) .and. &
    &   associated(n%right%right) .and. n%right%right%level == n%level)) return
    r => n%right
    n%right => r%left
    r%left => n
    r%level = r%level+1
  end
  function predecessor(n) result(p)
    type(t_node), pointer, intent(in) :: n
    type(t_node), pointer :: p
    p => null()
    if (.not.associated(n%left)) return
    p => n%left
    do while (associated(p%right))
      p => p%right
    end do
  end
  function successor(n) result(s)
    type(t_node), pointer, intent(in) :: n
    type(t_node), pointer :: s
    s => null()
    if (.not.associated(n%right)) return
    s => n%right
    do while (associated(s%left))
      s => s%left
    end do
  end
  recursive function insert(n,e) result(t)
    type(t_node), pointer, intent(in) :: n
    integer, intent(in) :: e
    type(t_node), pointer :: t
    t => new_node(e)
    if (.not.associated(n)) return
    t => n
    if (e < t%e) then
      t%left => insert(t%left,e)
    else if (e > t%e) then
      t%right => insert(t%right,e)
    end if
    t => skew(t)
    t => split(t)
  end
  recursive function delete(n,e) result(t)
    type(t_node), pointer, intent(in) :: n
    integer, intent(in) :: e
    type(t_node), pointer :: t, l
    t => n
    if (.not.associated(n)) return
    if (e < t%e) then
      t%left => delete(t%left,e)
    else if (e > t%e) then
      t%right => delete(t%right,e)
    else
      if (is_leaf(t)) then
        t => null()
        return
      end if
      if (.not.associated(t%left)) then
        l => successor(t)
        t%right => delete(t%right,l%e)
        t%e = l%e
      else
        l => predecessor(t)
        t%left => delete(t%left,l%e)
        t%e = l%e
      end if
    end if
    t => decrease_level(t)
    t => skew(t)
    t%right => skew(t%right)
    if (associated(t%right)) t%right%right => skew(t%right%right)
    t => split(t)
    t%right => split(t%right)
  end
  function decrease_level(n) result(t)
    type(t_node), pointer, intent(in) :: n
    type(t_node), pointer :: t
    integer :: should_be
    t => n
    should_be = min(level(t%left),level(t%right))+1
    if (t%level > should_be) then
      t%level = should_be
      if (level(t%right) > should_be) t%right%level = should_be
    end if
  end
  recursive subroutine release_tree(t)
    type(t_node), pointer, intent(inout) :: t
    if (.not.associated(t)) return
    call release_tree(t%left)
    call release_tree(t%right)
    deallocate(t)
  end
  recursive subroutine get_element(t,es,num)
    type(t_node), pointer, intent(in) :: t
    integer, intent(inout) :: es(:)
    integer, intent(inout) :: num
    if (.not.associated(t)) return
    call get_element(t%left,es,num)
    num = num+1
    es(num) = t%e
    call get_element(t%right,es,num)
  end
  integer function size_of(this)
    class(t_tree_set), intent(in) :: this
    size_of = tree_size(this%root)
  end
  subroutine clear(this)
    class(t_tree_set), intent(inout) :: this
    call release_tree(this%root)
    this%root => null()
  end
  subroutine set_default(this,deflt)
    class(t_tree_set), intent(inout) :: this
    integer, intent(in) :: deflt
    this%deflt = deflt
  end
  subroutine add(this,e)
    class(t_tree_set), intent(inout) :: this
    integer, intent(in) :: e
    this%root => insert(this%root,e)
  end
  subroutine remove(this,e)
    class(t_tree_set), intent(inout) :: this
    integer, intent(in) :: e
    this%root => delete(this%root,e)
  end
  function contain(this,e) result(ret)
    class(t_tree_set), intent(inout) :: this
    integer, intent(in) :: e
    type(t_node), pointer :: n
    logical :: ret
    ret = .false.
    n => this%root
    do while (associated(n))
      if (e < n%e) then
        n => n%left
      else if (e > n%e) then
        n => n%right
      else
        ret = .true.
        return
      end if
    end do
  end
  function firste(this) result(ret)
    class(t_tree_set), intent(inout) :: this
    type(t_node), pointer :: n
    integer :: ret
    ret = this%deflt
    n => this%root
    if (.not.associated(n)) return
    do while (associated(n%left))
      n => n%left
    end do
    ret = n%e
  end
  function poll_first(this) result(ret)
    class(t_tree_set), intent(inout) :: this
    type(t_node), pointer :: n
    integer :: ret
    ret = firste(this)
    if (ret /= this%deflt) this%root => delete(this%root,ret)
  end
  function laste(this) result(ret)
    class(t_tree_set), intent(inout) :: this
    type(t_node), pointer :: n
    integer :: ret
    ret = this%deflt
    n => this%root
    if (.not.associated(n)) return
    do while (associated(n%right))
      n => n%right
    end do
    ret = n%e
  end
  function poll_last(this) result(ret)
    class(t_tree_set), intent(inout) :: this
    type(t_node), pointer :: n
    integer :: ret
    ret = laste(this)
    if (ret /= this%deflt) this%root => delete(this%root,ret)
  end
  function floore(this,e) result(ret)
    class(t_tree_set), intent(inout) :: this
    integer, intent(in) :: e
    type(t_node), pointer :: n
    integer :: ret
    ret = this%deflt
    n => this%root
    do while (associated(n))
      if (e < n%e) then
        n => n%left
      else if (e > n%e) then
        if (ret == this%deflt) ret = n%e
        if (e-ret > e-n%e) ret = n%e
        n => n%right
      else
        ret = n%e
        return
      end if
    end do
  end
  function lowere(this,e) result(ret)
    class(t_tree_set), intent(inout) :: this
    integer, intent(in) :: e
    integer :: ret
    ret = floore(this,e-1)
  end
  function ceilinge(this,e) result(ret)
    class(t_tree_set), intent(inout) :: this
    integer, intent(in) :: e
    type(t_node), pointer :: n
    integer :: ret
    ret = this%deflt
    n => this%root
    do while (associated(n))
      if (e < n%e) then
        if (ret == this%deflt) ret = n%e
        if (e-ret < e-n%e) ret = n%e
        n => n%left
      else if (e > n%e) then
        n => n%right
      else
        ret = n%e
        return
      end if
    end do
  end
  function highere(this,e) result(ret)
    class(t_tree_set), intent(inout) :: this
    integer, intent(in) :: e
    integer :: ret
    ret = ceilinge(this,e+1)
  end
  subroutine element_list(this,es,num)
    class(t_tree_set), intent(inout) :: this
    integer, intent(out) :: es(:)
    integer, intent(out) :: num
    num = 0
    call get_element(this%root,es,num)
  end
end module mod_tree_set
module mod_union_find
  implicit none
  type union_find
    integer :: n = 0
    integer, pointer :: par(:), rnk(:), siz(:) => null()
  contains
    procedure :: init => inituf
    procedure :: release => releaseuf
    procedure :: find => find
    procedure :: same => same
    procedure :: unite => unite
    procedure :: size => size_of
  end type
  interface union_find
    module procedure :: newuf
  end interface union_find
contains
  function newuf(n) result(ret)
    integer, intent(in) :: n
    type(union_find) :: ret
    integer :: i
    ret%n = n
    allocate(ret%par(n),ret%rnk(n),ret%siz(n))
    ret%rnk = 0
    ret%siz = 1
    ret%par = [(i,i=1,n)]
  end
  subroutine inituf(this,n)
    class(union_find), intent(inout) :: this
    integer, intent(in) :: n
    integer :: i
    if (this%n /= n) then
      if (this%n /= 0) deallocate(this%par,this%rnk,this%siz)
      allocate(this%par(n),this%rnk(n),this%siz(n))
    end if
    this%n = n
    this%rnk = 0
    this%siz = 1
    this%par = [(i,i=1,n)]
  end
  subroutine releaseuf(this)
    class(union_find), intent(inout) :: this
    if (this%n /= 0) deallocate(this%par,this%rnk,this%siz)
    this%n = 0
  end
  recursive function find(this,i) result(ret)
    class(union_find), intent(inout) :: this
    integer, intent(in) :: i
    integer :: ret
    if (this%par(i) /= i) this%par(i) = find(this,this%par(i))
    ret = this%par(i)
  end
  logical function same(this,i,j)
    class(union_find), intent(inout) :: this
    integer, intent(in) :: i, j
    same = find(this,i) == find(this,j)
  end
  subroutine unite(this,i,j)
    class(union_find), intent(inout) :: this
    integer, intent(in) :: i, j
    integer :: x, y
    x = find(this,i)
    y = find(this,j)
    if (x == y) return
    if (this%rnk(x) < this%rnk(y)) then
      this%par(x) = y
      this%siz(y) = this%siz(y)+this%siz(x)
    else
      this%par(y) = x
      this%siz(x) = this%siz(x)+this%siz(y)
      if (this%rnk(x) == this%rnk(y)) this%rnk(x) = this%rnk(x)+1
    end if
  end
  integer function size_of(this,i)
    class(union_find), intent(inout) :: this
    integer, intent(in) :: i
    size_of = this%siz(find(this,i))
  end
end module mod_union_find
program unique_path
  use mod_tree_set
  use mod_union_find
  implicit none
  type(t_tree_set) :: set
  type(union_find) :: uf
  integer :: n, q, a(100000) = 0, b(100000) = 0, c(100000) = 0, k, i
  integer(8) :: m
  read(*,*) n, m, q
  uf = union_find(n)
  do i = 1, q
    read(*,*) a(i), b(i), c(i)
    a(i) = a(i)+1
    b(i) = b(i)+1
    if (c(i) == 0) call uf%unite(a(i),b(i))
  end do
  if (m == n-1) then
    if (any(c(1:q) == 1)) then
      write(*,'(a)') "No"
    else
      write(*,'(a)') "Yes"
    end if
    stop
  end if
  do i = 1, q
    if (c(i) == 1 .and. uf%same(a(i),b(i))) then
      write(*,'(a)') "No"
      stop
    end if
  end do
  do i = 1, n
    call set%add(uf%find(i))
  end do
  k = set%size()
  if (m <= int(n,8)+int(k,8)*(int(k,8)-3_8)/2_8) then
    write(*,'(a)') "Yes"
  else
    write(*,'(a)') "No"
  end if
end program unique_path


program test
implicit none
character::S*10
read*,S
S=trim(S)
if(S=='Sunny')then
print*,'Cloudy'
else if(S=='Cloudy')then
print*,'Rainy'
else
print*,'Sunny'
end if
end program test

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

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

Submitted source-code file size, in bytes.

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

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


program main
implicit none
integer :: i, j, k, n, q
integer , allocatable :: a(:), b(:)

read(*,*) n, k, q
allocate( a(q), b(n) )
b = 0
do i = 1, q
   read(*,*) a(i)
   b( a(i) ) = b( a(i) ) + 1
end do
do i = 1, n
   if( b(i) .ge. k ) then
      write(*,*) 'No'
   else
      write(*,*) 'Yes'
   end if
end do

end program main

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


module ABC141

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

  ! require all variables to be explicitly declared
  implicit none

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

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

  subroutine task_C

    ! variables for this <subroutine>
    integer(INT32) :: id_player_answered
    integer(INT32) :: num_players
    integer(INT32) :: val_points_init
    integer(INT32) :: num_questions

    ! arrays for this <subroutine>
    integer(INT32), dimension(:), allocatable :: list_player_points

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

    ! STEP.01
    ! read out the given data
    read(unit=INPUT_UNIT, fmt=*) num_players, val_points_init, num_questions

    ! STEP.02
    ! allocate the array to store the given data
    allocate( list_player_points  (1:num_players)   )

    ! STEP.03
    ! initialize the list of points
    list_player_points(:) = 0_INT32

    ! STEP.04
    ! read out the given data
    ! update the points of the players
    do itr_q = 1_INT32, num_questions, 1_INT32
      read(unit=INPUT_UNIT, fmt=*) id_player_answered
      list_player_points( id_player_answered ) = list_player_points( id_player_answered ) + 1_INT32
    end do

    ! STEP.05
    ! output the answer of this task
    ! update the points of the players
    do itr_p = 1_INT32, num_players, 1_INT32

      if (list_player_points(itr_p) .gt. num_questions - val_points_init) then
        write(unit=OUTPUT_UNIT, fmt='(A)', advance='yes') 'Yes'
      else
        write(unit=OUTPUT_UNIT, fmt='(A)', advance='yes') 'No'
      end if

    end do

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

    ! STEP.END
    return

  end subroutine task_C

end module ABC141

program main

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

  ! require all variables to be explicitly declared
  implicit none

  call task_C

end program main


program c
implicit none

integer :: i, n, k, q, aa
integer,allocatable,dimension(:) :: A

read(*,*) n, k, q

allocate(A(N))
A = k-q

do i = 1, q
read(*,*) aa
A(aa) = A(aa) + 1
end do

do i = 1, n
 if (A(i)<=0) then
 write(*,'(a2)') "No"
 else
 write(*,'(a3)') "Yes"
 end if
end do

end program


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

read(*,*) n,k,q

allocate(a(q))
allocate(b(n))

b=0
do i=1,q
    read(*,*) a(i)
    b(a(i)) = b(a(i)) + 1
end do

do i=1,n
    if (k-q+b(i)>0) then
        write(*,*) "Yes"
    else
        write(*,*) "No"
    end if
end do

stop
contains

end program kadai


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


integer N,Q
integer(16),allocatable,dimension(:)::Point
integer(16) K
integer A
read*,N,K,Q
allocate(Point(N))
Point=0
do i=1,Q
  read*,A
  Point(A)=Point(A)+1
end do
do i=1,N
  if(Q-Point(i)<K)then
    print"(A)","Yes"
  else
    print"(A)","No"
  endif
end do
end


program xor_sum_3
  implicit none
  integer :: n, i, j, k, x(100000,60) = 0
  integer(8) :: a(100000) = 0_8, ans1 = 0_8, ans2 = 0_8, tmp
  read(*,*) n
  read(*,*) a(1:n)
  do i = 1, n
    do j = 1, 60
      if (btest(a(i),j-1)) x(i,j) = 1
    end do
  end do
  do j = 1, 60
    if (mod(sum(x(1:n,j)),2) == 1) then
      ans1 = or(ans1,lshift(1_8,j-1))
      x(1:n,j) = 0
    end if
  end do
  x(1:n,1:60) = x(1:n,60:1:-1)
  call normal_form(x(1:n,:))
  x(1:n,1:60) = x(1:n,60:1:-1)
  j = 60
  do i = 1, n
    do while (j > 0 .and. x(i,j) == 0)
      j = j-1
    end do
    if (j == 0) exit
    tmp = 0_8
    do k = 1, 60
      if (x(i,k) == 1) tmp = ibset(tmp,k-1)
    end do
    ans2 = xor(ans2,tmp)
  end do
  write(*,'(i0)') ans1+2_8*ans2
contains
  subroutine normal_form(a)
    integer, intent(inout) :: a(:,:)
    integer :: n, m, i, j, k, l
    n = size(a,1)
    m = size(a,2)
    i = 1
    do j = 1, m
      if (sum(a(i:n,j)) == 0) cycle
      if (a(i,j) == 0) then
        do k = i+1, n
          if (a(k,j) == 1) then
            do l = j, m
              a(i,l) = xor(a(i,l),a(k,l))
            end do
            exit
          end if
        end do
      end if
      do k = 1, n
        if (k == i) cycle
        if (a(k,j) == 0) cycle
        do l = j, m
          a(k,l) = xor(a(k,l),a(i,l))
        end do
      end do
      i = i+1
      if (i > n) exit
    end do
  end
end program xor_sum_3

Score: 600 points

Problem Statement

We have N non-negative integers: A_1, A_2, ..., A_N.

Consider painting at least one and at most N-1 integers among them in red, and painting the rest in blue.

Let the beauty of the painting be the \mbox{XOR} of the integers painted in red, plus the \mbox{XOR} of the integers painted in blue.

Find the maximum possible beauty of the painting.

What is \mbox{XOR}?

The bitwise \mbox{XOR} x_1 \oplus x_2 \oplus \ldots \oplus x_n of n non-negative integers x_1, x_2, \ldots, x_n is defined as follows:

When x_1 \oplus x_2 \oplus \ldots \oplus x_n is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if the number of integers among x_1, x_2, \ldots, x_n whose binary representations have 1 in the 2^k's place is odd, and 0 if that count is even.

For example, 3 \oplus 5 = 6.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

0 \leq A_i < 2^{60}\ (1 \leq i \leq N)

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible beauty of the painting.

Sample Input 1

3
3 6 5

Sample Output 1

12

If we paint 3, 6, 5 in blue, red, blue, respectively, the beauty will be (6) + (3 \oplus 5) = 12.

There is no way to paint the integers resulting in greater beauty than 12, so the answer is 12.

Sample Input 2

4
23 36 66 65

Sample Output 2

188

Sample Input 3

20
1008288677408720767 539403903321871999 1044301017184589821 215886900497862655 504277496111605629 972104334925272829 792625803473366909 972333547668684797 467386965442856573 755861732751878143 1151846447448561405 467257771752201853 683930041385277311 432010719984459389 319104378117934975 611451291444233983 647509226592964607 251832107792119421 827811265410084479 864032478037725181

Sample Output 3

2012721721873704572

A_i and the answer may not fit into a 32-bit integer type.


program name
    implicit none
    integer(4):: n

    read*, n
    print*, n*n*n
end program name

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.


integer a
character s*3,t*3
read*, s,t
a=0
do i=1, 3
	if(s(i:i)==t(i:i)) a=a+1
end do
print*, a
end

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
    implicit none
    integer:: A, B
    integer:: i, c_num, e_num

    read*, A, B
    c_num = 1

    do while(c_num < B)
        c_num = c_num - 1
        c_num = c_num + A
        e_num = e_num + 1
    enddo

    print'(i0)', e_num
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 answer
  implicit none
  integer :: N, i, j, a
  integer, allocatable :: H(:), s(:)
  read(*,*) N
  allocate(H(N))
  read(*,*) H
  allocate(s(N))

  s(:)=0
  do i = 1, N
     do j = 0, N-i-1
        if(H(i+j)>=H(i+j+1)) then
           s(i)=s(i)+1
        else
           if(i==1) then
              a=s(i)
           else if(s(i)>a) then
              a=s(i)
           end if
           exit
        end if
     end do
  end do

  write(*,*) maxval(s)
  stop
 end program answer

Score : 300 points

Problem Statement

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

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

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

Find the maximum number of times you can move.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq H_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
H_1 H_2 ... H_N

Output

Print the maximum number of times you can move.

Sample Input 1

5
10 4 8 7 3

Sample Output 1

2

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

Sample Input 2

7
4 4 5 6 6 5 5

Sample Output 2

3

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

Sample Input 3

4
1 2 3 4

Sample Output 3

0


program main
  implicit none
  integer(8) :: n, i, streak, res
  integer(8), allocatable :: h(:)
  read (*, *) n
  allocate (h(n))
  read (*, *) h(:)
  res = 0
  streak = 0
  do i = 1, n - 1
    if (h(i + 1) <= h(i)) then
      streak = streak + 1
      res = max(res, streak)
    else
      streak = 0
    end if
  end do
  write (*, "(i0)") streak
end program main


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


program main
    implicit none
    integer:: N,i, no, day
    logical:: battle
    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 = .false.
        isbattled(1:N) = .false.
        do i = 1, N
            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
    do i = 1, N
        if(noind(i) /= N)then
            print'(i0)', -1
            stop
        endif
    enddo

    print'(i0)',day
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 kadai
    implicit none
integer  ::  n
integer,allocatable  ::  a(:,:)
integer,allocatable  ::  now(:)
integer,allocatable  ::  rest(:)
integer  ::  i,j,move,day,num,for

read(*,*) n
allocate(a(n,n-1))
allocate(now(n))
allocate(rest(n))

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

now=1
rest=0
move=0
num=0
day=1

do j=1,n*n
    do i=1,n
    if(now(i)<n) then
        for=a(i,now(i))

        if (now(for)>n-1) then
            write(*,*) -1
            stop
        end if

        if (a(for,now(for))==i .and. rest(for)==0 .and. rest(i)==0) then
            !write(*,*) i,"vs",for
            now(i)=now(i)+1
            now(for)=now(for)+1
            if (now(i)>n .or. now(for)>n) then
                write(*,*) -1
                !write(*,*) now,num
                stop
            end if
            move=1
            num=num+1
            rest(i)=1
            rest(for)=1
        end if
        if (num==n*(n-1)/2) then
            write(*,*) day
            stop
        end if
    end if
    end do
    if (move==0) then
        write(*,*) -1
        stop
    end if
    day=day+1
    rest=0
end do

write(*,*) -1

stop
contains

end program kadai


program t
  implicit none
  integer :: M,D,i,j,ct

  read(*,*) M,D

  ct=0
  do i=1,D
     if((mod(i,10).ne.1).and.(i/10.ne.1)) then
        j=mod(i,10)*(i/10)
        if((j.gt.M)) then
        else
           if(j.gt.0) then
!              write(*,*) i,j
              ct=ct+1
           endif
        endif
     else

     endif

  enddo
  write(*,'(i0)') ct

end program t

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 main
  implicit none
  integer(8) i,M,D,ans,j
  read(*, *) M, D
  !read(*, *) (L(i), i = 1,N)
  ans = 0
  do i = 1,M
    do j = 1,D
      if (i == mod(j,10)*(j/10) .and. mod(j,10)>1 .and. j/10>1) then
        ans = ans + 1
        !write(*, *) i,j
      end if
    end do
  end do

  write(*, *) ans
end program main


module mod_fenwick_tree
  implicit none
  type t_fenwick_tree
    private
    integer :: n, p
    integer, pointer :: arr(:) => null()
  contains
    procedure :: init => init_bit
    procedure :: release => release_bit
    procedure :: add => get_addition
    procedure :: sum => get_summation
    procedure :: range => get_partial_summation
    procedure :: val => get_value
    procedure :: lower_bound => get_lower_bound
    procedure :: update => get_updated
    procedure :: max => get_maximum
  end type t_fenwick_tree
contains
  subroutine init_bit(this,n)
    class(t_fenwick_tree), intent(inout) :: this
    integer, intent(in) :: n
    integer :: p
    p = 1
    this%n = n
    allocate(this%arr(n))
    this%arr = 0
    do while (lshift(p,1) <= n)
      p = lshift(p,1)
    end do
    this%p = p
  end
  subroutine release_bit(this)
    class(t_fenwick_tree), intent(inout) :: this
    if (associated(this%arr)) deallocate(this%arr)
  end
  subroutine get_addition(this,i,v)
    class(t_fenwick_tree), intent(inout) :: this
    integer, intent(in) :: i
    integer, intent(in) :: v
    integer :: x
    x = i
    do while (x <= this%n)
      this%arr(x) = this%arr(x)+v
      x = x+and(x,-x)
    end do
  end
  function get_summation(this,i) result(s)
    class(t_fenwick_tree), intent(in) :: this
    integer, intent(in) :: i
    integer :: x
    integer :: s
    s = -1
    if (i > this%n) return
    s = 0
    x = i
    do while (x > 0)
      s = s+this%arr(x)
      x = x-and(x,-x)
    end do
  end
  function get_partial_summation(this,l,r) result(s)
    class(t_fenwick_tree), intent(in) :: this
    integer, intent(in) :: l, r
    integer :: i, j
    integer :: s
    s = -1
    if (l > r) return
    s = 0
    i = l-1
    j = r
    do while (j > i)
      s = s+this%arr(j)
      j = j-and(j,-j)
    end do
    do while (i > j)
      s = s-this%arr(i)
      i = i-and(i,-i)
    end do
  end
  function get_value(this,i) result(v)
    class(t_fenwick_tree), intent(in) :: this
    integer, intent(in) :: i
    integer :: x
    integer :: v
    v = get_partial_summation(this,i,i)
  end
  function get_lower_bound(this,v) result(x)
    class(t_fenwick_tree), intent(in) :: this
    integer, intent(in) :: v
    integer :: x, k
    integer :: w
    x = 0
    if (v <= 0) return
    k = this%p
    w = v
    do while (k > 0)
      if (x+k <= this%n .and. this%arr(x+k) < w) then
        w = w-this%arr(x+k)
        x = x+k
      end if
      k = rshift(k,1)
    end do
    x = x+1
  end
  subroutine get_updated(this,i,v)
    class(t_fenwick_tree), intent(inout) :: this
    integer, intent(in) :: i
    integer, intent(in) :: v
    integer :: x
    x = i
    do while (x <= this%n)
      if (this%arr(x) < v) this%arr(x) = v
      x = x+and(x,-x)
    end do
  end
  function get_maximum(this,i) result(m)
    class(t_fenwick_tree), intent(in) :: this
    integer, intent(in) :: i
    integer :: x, m
    x = i
    m = 0
    do while (x > 0)
      m = max(m,this%arr(x))
      x = x-and(x,-x)
    end do
  end
end module mod_fenwick_tree
program kleene_inversion
  use mod_fenwick_tree
  implicit none
  type(t_fenwick_tree) :: bit
  integer(8), parameter :: md = 1000000007_8
  integer :: n, a(2000) = 0, b(4000) = 0, inv(4000) = 0, i
  integer(8) :: k, ans = 0_8, coef
  read(*,*) n, k
  read(*,*) a(1:n)
  b(1:n) = a(1:n)
  b(n+1:2*n) = a(1:n)
  call bit%init(2000)
  do i = 1, 2*n
    call bit%add(b(i),1)
    inv(i) = i-bit%sum(b(i))
  end do
  coef = mod(mod(k*(k-1_8),md)*500000004_8,md)
  do i = 1, n
    ans = mod(ans+mod(k*int(inv(i),8),md)+mod(coef*int(inv(n+i)-inv(i),8),md),md)
  end do
  write(*,'(i0)') modulo(ans,md)
end program kleene_inversion

Score : 300 points

Problem Statement

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

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

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

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

Constraints

All values in input are integers.

1 \leq N \leq 2000

1 \leq K \leq 10^9

1 \leq A_i \leq 2000

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

2 2
2 1

Sample Output 1

3

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

B_0 > B_1

B_0 > B_3

B_2 > B_3

Thus, the inversion number of B is 3.

Sample Input 2

3 5
1 1 1

Sample Output 2

0

A may contain multiple occurrences of the same number.

Sample Input 3

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

Sample Output 3

185297239

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


program main
	implicit none
    integer n,i
    double precision,allocatable::v(:)
    double precision ans
    read *,n
    allocate(v(n))
    read *,(v(i),i=1,n)
    call quicksort(v,1,n)
    ans=v(1)
    do i=2,n
    	ans=(ans+v(i))/2
    end do
    print '(f0.12)',ans
contains
recursive subroutine quicksort(a, first, last)
  implicit none
  real*8  a(*)
  integer first, last
  integer i, j
  real*8  x, t

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

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


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_C
  private :: sort_heap

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

  subroutine task_C

    ! variables for this <subroutine>
    integer      :: num_ingredient
    real(REAL64) :: value_maximum

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

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

    ! STEP.01
    ! read out the number of given ingredient
    read(unit=INPUT_UNIT, fmt=*) num_ingredient

    ! STEP.02
    ! allocate the array to store value of ingredients
    allocate( value_ingredient(1:num_ingredient) )

    ! STEP.03
    ! read out given value of ingredients
    read(unit=INPUT_UNIT, fmt=*) value_ingredient(:)

    ! STEP.04
    ! sort the array storing the value of ingredients
    call sort_heap ( value_ingredient(:), num_ingredient )

    ! STEP.05
    ! calculate the answer of this task
    value_maximum = value_ingredient(1)

    do itr = 2, num_ingredient, 1
      value_maximum = 0.5_REAL64 * (value_maximum + value_ingredient(itr))
    end do

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

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

    ! STEP.END
    return

  end subroutine task_C

  subroutine sort_heap (array, size)

    ! arguments for this <subroutine>
    real(REAL64), intent(inout) :: array(:)
    integer,      intent(in)    :: size

    ! variables for this <subroutine>
    integer      :: idx_p
    integer      :: idx_c
    integer      :: size_heap
    real(REAL64) :: buffer_val

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

    size_heap = size     + 1
    itr       = size / 2 + 1

    do while (.true.)

      if (itr .gt. 1) then

        itr        = itr - 1
        buffer_val = array(itr)

      else

        size_heap = size_heap - 1
        if (size_heap .eq. 1) return
        buffer_val       = array(size_heap)
        array(size_heap) = array(1)

      end if

      idx_p = itr
      idx_c = itr * 2

      do while ( idx_c .lt. size_heap )

        if ( idx_c + 1 .lt. size_heap ) then
          if ( array(idx_c + 1) .gt. array(idx_c) ) idx_c = idx_c + 1
        end if

        if ( array(idx_c) .gt. buffer_val ) then
          array(idx_p) = array(idx_c)
          idx_p        = idx_c
          idx_c        = idx_p * 2
        else
          exit
        end if

      end do

      array(idx_p) = buffer_val

    end do

  end subroutine sort_heap

end module ABC138

program main

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

  ! require all variables to be explicitly declared
  implicit none

  call task_C

end program main


program main
  implicit none
  integer i,N,ans,t,flag
  character(2*10**5) S
  read(*, *) S
  S = trim(S)
  N = len(trim(S))
  !read(*, *) (L(i), i = 1,N)
  ans = 1
  t = 2
  flag = 0
  do
    if (t == N .and. S(t:t) == S(t-1:t-1) .and. flag == 0) then
      t = t+1
    elseif (S(t:t) == S(t-1:t-1) .and. flag == 0) then
      t = t+2
      ans = ans + 1
      flag = 1
    else
      ans = ans + 1
      t = t + 1
      flag = 0
    endif
    if (t > N) then
      exit
    end if
  end do

  write(*, *) ans
end program main

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 main
  implicit none
  integer i,N,ans,t,flag
  character(2*10**5) S
  read(*, *) S
  S = trim(S)
  N = len(trim(S))
  !read(*, *) (L(i), i = 1,N)
  ans = 1
  t = 2
  flag = 0
  do
    if (t == N .and. S(t:t) == S(t-1:t-1)) then
      t = t+1
    elseif (S(t:t) == S(t-1:t-1) .and. flag == 0) then
      t = t+2
      ans = ans + 1
      flag = 1
    else
      ans = ans + 1
      t = t + 1
      flag = 0
    endif
    if (t > N) then
      exit
    end if
  end do

  write(*, *) ans
end program main


module mod_priority_queue
  implicit none
  type t_item
    integer :: idx, val
  end type t_item
  type t_priority_queue
    private
    integer :: num = 0
    type(t_item), pointer :: heap(:) => null()
  contains
    procedure :: offer => offer
    procedure :: clear => clear
    procedure :: poll => poll
    procedure :: peek => peek
    procedure :: size => size_of
  end type t_priority_queue
  interface t_item
    module procedure new_item
  end interface t_item
contains
  function new_item(idx,val) result(item)
    integer, intent(in) :: idx, val
    type(t_item) :: item
    item%idx = idx
    item%val = val
  end
  integer function compare(a,b)
    type(t_item), intent(in) :: a, b
    compare = -(a%val-b%val)
  end
  subroutine offer(this,item)
    class(t_priority_queue), intent(inout) :: this
    type(t_item), intent(in) :: item
    integer :: n, i
    type(t_item) :: t
    type(t_item), allocatable :: tmp(:)
    if (.not.associated(this%heap)) allocate(this%heap(1))
    if (this%num == size(this%heap)) then
      allocate(tmp(this%num))
      tmp = this%heap
      deallocate(this%heap)
      allocate(this%heap(2*this%num))
      this%heap(1:this%num) = tmp
      deallocate(tmp)
    end if
    this%num = this%num+1
    this%heap(this%num) = item
    n = this%num
    do while (n > 1)
      i = n/2
      if (compare(this%heap(n),this%heap(i)) < 0) then
        t = this%heap(n)
        this%heap(n) = this%heap(i)
        this%heap(i) = t
      end if
      n = i
    end do
  end
  subroutine clear(this)
    class(t_priority_queue), intent(inout) :: this
    if (associated(this%heap)) deallocate(this%heap)
    this%num = 0
  end
  function poll(this) result(item)
    class(t_priority_queue), intent(inout) :: this
    type(t_item) :: item, tmp
    integer :: n, i, j
    n = this%num
    item = this%heap(1)
    this%heap(1) = this%heap(n)
    this%num = this%num-1
    i = 1
    do while (2*i < n)
      j = 2*i
      if (j+1 < n .and. compare(this%heap(j+1),this%heap(j)) < 0) j = j+1
      if (compare(this%heap(j),this%heap(i)) < 0) then
        tmp = this%heap(j)
        this%heap(j) = this%heap(i)
        this%heap(i) = tmp
      end if
      i = j
    end do
  end
  function peek(this) result(item)
    class(t_priority_queue), intent(in) :: this
    type(t_item) :: item
    item = this%heap(1)
  end
  integer function size_of(this)
    class(t_priority_queue), intent(in) :: this
    size_of = this%num
  end
end module mod_priority_queue
program numbers_on_a_circle
  use mod_priority_queue
  implicit none
  type(t_priority_queue) :: pq
  type(t_item) :: item
  integer :: n, a(200000) = 0, b(200000) = 0, i, x
  integer(8) :: ans = 0_8
  read(*,*) n
  read(*,*) a(1:n)
  read(*,*) b(1:n)
  do i = 1, n
    if (b(i) < a(i)) then
      write(*,'(i0)') -1
      stop
    end if
    if (b(i) /= a(i) .and. b(i) > b(f(i-1))+b(f(i+1))) then
      call pq%offer(t_item(i,b(i)))
    end if
  end do
  do while (pq%size() > 0)
    item = pq%poll()
    i = item%idx
    x = b(f(i-1))+b(f(i+1))
    ans = ans+int((b(i)-a(i))/x,8)
    b(i) = mod(b(i)-a(i),x)+a(i)
    if (b(f(i-1)) /= a(f(i-1)) .and. b(f(i-1)) > b(i)+b(f(i-2))) then
      call pq%offer(t_item(f(i-1),b(f(i-1))))
    end if
    if (b(f(i+1)) /= a(f(i+1)) .and. b(f(i+1)) > b(i)+b(f(i+2))) then
      call pq%offer(t_item(f(i+1),b(f(i+1))))
    end if
  end do
  do i = 1, n
    if (b(i) /= a(i)) then
      write(*,'(i0)') -1
      stop
    end if
  end do
  write(*,'(i0)') ans
contains
  integer function f(i)
    integer, intent(in) :: i
    f = mod(n+i-1,n)+1
  end
end program numbers_on_a_circle

Score : 800 points

Problem Statement

There are N positive integers arranged in a circle.

Now, the i-th number is A_i. Takahashi wants the i-th number to be B_i. For this objective, he will repeatedly perform the following operation:

Choose an integer i such that 1 \leq i \leq N.

Let a, b, c be the (i-1)-th, i-th, and (i+1)-th numbers, respectively. Replace the i-th number with a+b+c.

Here the 0-th number is the N-th number, and the (N+1)-th number is the 1-st number.

Determine if Takahashi can achieve his objective.
If the answer is yes, find the minimum number of operations required.

Constraints

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

1 \leq A_i, B_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N
B_1 B_2 ... B_N

Output

Print the minimum number of operations required, or -1 if the objective cannot be achieved.

Sample Input 1

3
1 1 1
13 5 7

Sample Output 1

4

Takahashi can achieve his objective by, for example, performing the following operations:

Replace the second number with 3.

Replace the second number with 5.

Replace the third number with 7.

Replace the first number with 13.

Sample Input 2

4
1 2 3 4
2 3 4 5

Sample Output 2

-1

Sample Input 3

5
5 6 5 2 1
9817 1108 6890 4343 8704

Sample Output 3

25


program main
integer :: a,b,ans

read *,a,b
ans = max(a+b,a-b)
ans = max(ans,a*b)

print *,ans
end program main

Score : 100 points

Problem Statement

We have two integers: A and B.

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

Constraints

All values in input are integers.

-100 \leq A,\ B \leq 100

Input

Input is given from Standard Input in the following format:

A B

Output

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

Sample Input 1

-13 3

Sample Output 1

-10

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

Sample Input 2

1 -33

Sample Output 2

34

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

Sample Input 3

13 3

Sample Output 3

39

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


implicit none
integer a,b,c
read *,a,b,c
print '(i0)',max(a+b,a-b,a*b)
end


program main
    implicit none
    integer k,x,i

    read*, k,x

    do i = x - (k-1), x + (k-1)
        write(*, fmt='(i0,1x)', advance='no') i
    enddo
    write(*,*)

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


      MODULE charaQsortMod
! 文字のクイックソート
! 文字列内の文字を昇順に並び替える．

!!!!!!!!!!!!!!!!!!!  caution  !!!!!!!!!!!!!!!!!!!
! 大文字小文字を併用する場合に注意！！！！
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

      implicit none

      interface qSort
        module procedure :: charaQsort
      end interface

      contains
!引数，出力は文字列．文字の配列ではない．
      pure recursive function charaQsort( s ) result( r )
        implicit none
        character(*),intent(in) :: s
        character(len(s)) :: r, buff
        character(1) :: buffer(1),sample(3),piv
        integer :: i,j,l,head,tail

        l = len( s )
        if( l<=1 )then
          r = s
        else
          !select pivot
          sample(1:3) = [s(1:1), s(l/2:l/2), s(l:l)]
          do i = 1,2
            do j = 2,3
              if( sample(i)>sample(j) )then
                buffer(1:1) = sample(i:i)
                sample(i:i) = sample(j:j)
                sample(j:j) = buffer(1:1)
              end if
            end do
          end do
          piv = sample(2)

          head = 1; tail = l; j = 1 !次に代入される場所
          do i = 1,l
            if( s(i:i) < piv )then
              r(head:head) = s(i:i)
              head = head + 1
            else if( s(i:i) >piv )then
              r(tail:tail) = s(i:i)
              tail = tail - 1
            else
              buff(j:j) = s(i:i)
              j = j + 1
            end if
          end do !i

          r(1:head-1) = charaQsort( r(1:head-1) )
          r(tail+1:l) = charaQsort( r(tail+1:l) )
          r(head:tail) = buff(1:j)

        end if
      end function
      end MODULE charaQsortMod

      MODULE strQsortMod
! 文字列のクイックソート
! charLen で使用可能な文字列サイズの上限を定める．

!!!!!!!!!!!!!!!!!!!  caution  !!!!!!!!!!!!!!!!!!!
! 大文字小文字を併用する場合に注意！！！！
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

!!2020/05/16
!select pivot のとこのバッファ部分おかしくない？後で確認．

      implicit none
      integer(4),parameter,private :: charLen = 10

      interface qSort
        module procedure :: strQsort
      end interface

      contains
      pure recursive function strQsort( s ) result( r )
        implicit none
        character(charLen), intent(in) :: s(:)
        character(charLen) :: r( size(s) ), sample(3), buffer
        integer(4) :: len, i,j

        len = size( s(:) )
        if( len<=1 )then
          r = s
        else
          !select pivot
          sample(1:3) = [s(1), s(len/2), s(len)]
          do i = 1,2
            do j = 2,3
              if( sample(i)>sample(j) )then
                buffer = sample(i)
                sample(i) = sample(j)
                sample(j) = buffer
              end if
            end do
          end do

          r = [ strQsort( pack(s(:), s(:)< sample(2)) ), &
                          pack(s(:), s(:)==sample(2))  , &
                strQsort( pack(s(:), s(:)> sample(2)) ) ]
        end if
      end function
      end MODULE strQsortMod

      PROGRAM GreenBin
      use charaQsortMod
      use strQsortMod
      IMPLICIT NONE
      integer(16) :: n
      character(10),allocatable :: s(:)
      integer(16) :: i,ans=0

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

      do i = 1,n
        s(i)(1:10) = qsort( s(i)(1:10) )
      end do

      s = qsort( s )

      if( s(1)(1:10)==s(2)(1:10) ) ans = ans + 1

      do i = 2,n
        if( s(i-1)(1:10)==s(i)(1:10) )then
          ans = ans + 1
        end if
      end do

      print*,ans

    !  print*,n
    !  do i = 1,n
    !    print*,s(i)(1:10)
    !  end do

      END PROGRAM

Score : 300 points

Problem Statement

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

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

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

Constraints

2 \leq N \leq 10^5

s_i is a string of length 10.

Each character in s_i is a lowercase English letter.

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

Input

Input is given from Standard Input in the following format:

N
s_1
s_2
:
s_N

Output

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

Sample Input 1

3
acornistnt
peanutbomb
constraint

Sample Output 1

1

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

Sample Input 2

2
oneplustwo
ninemodsix

Sample Output 2

0

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

Sample Input 3

5
abaaaaaaaa
oneplustwo
aaaaaaaaba
twoplusone
aaaabaaaaa

Sample Output 3

4

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


program name
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: i,j,n
    integer(int64):: ans
    character(10),allocatable:: s(:)
    character(1):: tmp
    logical:: ok

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

    do i=1,n
        ok=.false.
        do while(.not. ok)
            ok = .true.
            do j=1,9
                if (s(i)(j:j) > s(i)(j+1:j+1)) then
                    ok = .false.
                    tmp = s(i)(j:j)
                    s(i)(j:j) = s(i)(j+1:j+1)
                    s(i)(j+1:j+1) = tmp
                end if
            end do
        end do
    end do

    call char_sort(s,1,n)
    j=1
    ans=0
    do i=1,n-1
        if (s(i) == s(i+1)) then
            ans=ans+j
            j=j+1
        else
            j=1
        end if
    end do

    print'(i0)', ans
contains
    recursive subroutine char_sort(c,fst,lst)
        integer(int32),intent(in):: fst, lst
        character(*),intent(inout):: c(:)
        integer(int32):: mdl

        if (lst-fst < 2)then
            if (c(fst) > c(lst)) call cswap(c(fst),c(lst))
            return
        end if

        mdl = (fst+lst)/2
        call char_sort(c,fst,mdl)
        call char_sort(c,mdl+1,lst)
        call char_merge(c,fst,mdl,lst)
    end subroutine

    subroutine char_merge(c,fst,mdl,lst)
        integer(int32),intent(in):: fst,mdl,lst
        character(*),intent(inout):: c(:)
        character(10),allocatable:: tmp(:)
        integer(int32):: l,r,ti

        allocate(tmp(lst-fst+1))
        l=fst
        r=mdl+1
        ti=1

        do while(l <= mdl .and. r <= lst)
            if (c(l) <= c(r)) then
                tmp(ti) = c(l)
                l=l+1
            else
                tmp(ti) = c(r)
                r=r+1
            end if
            ti=ti+1
        end do

        if (l <= mdl) then
            tmp(ti:) = c(l:mdl)
        else
            tmp(ti:) = c(r:lst)
        end if

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

    subroutine cswap(c1,c2)
        character(*):: c1,c2
        character(10),allocatable:: tmp

        tmp = c1
        c1 = c2
        c2 = tmp
    end subroutine
end program name


program name
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: i,j,n,ans
    character(10),allocatable:: s(:)
    character(1):: tmp
    logical:: ok

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

    do i=1,n
        ok=.false.
        do while(.not. ok)
            ok = .true.
            do j=1,9
                if (s(i)(j:j) > s(i)(j+1:j+1)) then
                    ok = .false.
                    tmp = s(i)(j:j)
                    s(i)(j:j) = s(i)(j+1:j+1)
                    s(i)(j+1:j+1) = tmp
                end if
            end do
        end do
    end do

    call char_sort(s,1,n)
    j=1
    ans=0
    do i=1,n-1
        if (s(i) == s(i+1)) then
            ans=ans+j
            j=j+1
        else
            j=1
        end if
    end do

    print'(i0)', ans
contains
    recursive subroutine char_sort(c,fst,lst)
        integer(int32),intent(in):: fst, lst
        character(*),intent(inout):: c(:)
        integer(int32):: mdl

        if (lst-fst < 2)then
            if (c(fst) > c(lst)) call cswap(c(fst),c(lst))
            return
        end if

        mdl = (fst+lst)/2
        call char_sort(c,fst,mdl)
        call char_sort(c,mdl+1,lst)
        call char_merge(c,fst,mdl,lst)
    end subroutine

    subroutine char_merge(c,fst,mdl,lst)
        integer(int32),intent(in):: fst,mdl,lst
        character(*),intent(inout):: c(:)
        character(10),allocatable:: tmp(:)
        integer(int32):: l,r,ti

        allocate(tmp(lst-fst+1))
        l=fst
        r=mdl+1
        ti=1

        do while(l <= mdl .and. r <= lst)
            if (c(l) <= c(r)) then
                tmp(ti) = c(l)
                l=l+1
            else
                tmp(ti) = c(r)
                r=r+1
            end if
            ti=ti+1
        end do

        if (l <= mdl) then
            tmp(ti:) = c(l:mdl)
        else
            tmp(ti:) = c(r:lst)
        end if

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

    subroutine cswap(c1,c2)
        character(*):: c1,c2
        character(10),allocatable:: tmp

        tmp = c1
        c1 = c2
        c2 = tmp
    end subroutine
end program name


program ccc

implicit none
integer :: n, i, j
integer(8) :: 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(8),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(8),intent(inout) :: array(1:n)

  integer ::i,k,j,l
  integer(8) :: t

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

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

  return
end subroutine heapsort


program main

implicit none

integer:: i, j, k, n, idx, pair, ierr
integer, allocatable:: counts(:,:)
logical:: flag
character(1):: c

read *, n
allocate( counts(1:26,1:n) )
counts(:,:)= 0

do i= 1, n
  do while( .true. )
    read(*,'(a1)',advance='no',iostat=ierr) c
    if( ierr/=0 ) exit
    idx= ichar(c) - 96
    counts(idx,i)= counts(idx,i) + 1
  end do
end do

pair= 0
do i= 1, n
  do j= i+1, n
     flag= .true.
     do k= 1, 26
       if( counts(k,i)/=counts(k,j) ) then
         flag = .false.
         exit
       end if
     end do
     if( flag ) pair= pair + 1
  end do
end do
print '(i0)', pair

end program


module ABC137

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

  ! require all variables to be explicitly declared
  implicit none

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

  ! constants for this <module>
  integer(INT8), parameter, private :: len_string = 10_INT8
  integer(INT8), parameter, private :: ichar_a    = ichar("a", kind=INT8)
  integer(INT8), parameter, private :: ichar_z    = ichar("z", kind=INT8)

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

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

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

  subroutine fetch_data_string (index)

    ! arguments for this <subroutine>
    integer(INT32), intent(in) :: index

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

    do itr = 1_INT8, len_string, 1_INT8
      val_ichar                         = ichar(c=given_data(index)%string(itr:itr), kind=INT8)
      given_data(index)%code(val_ichar) = given_data(index)%code(val_ichar) + 1_INT8
    end do

    ! STEP.END
    return

  end subroutine fetch_data_string

  pure function isAnagram (index_origin, index_target) result(stat)

    ! arguments for this <function>
    integer(INT32), intent(in) :: index_origin
    integer(INT32), intent(in) :: index_target

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

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

    ! STEP.01
    ! initialize the return value of this function
    stat = .true.

    ! STEP.02
    ! judge the status of given data
    do itr = ichar_a, ichar_z, 1_INT8
      if ( given_data(index_origin)%code(itr) .ne. given_data(index_target)%code(itr) ) then
        stat = .false.
        exit
      end if
    end do

    ! STEP.END
    return

  end function isAnagram

  subroutine task_C

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

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

    ! STEP.01
    ! read out the given data

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

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

    ! STEP.01.03
    do itr = 1_INT32, num_string, 1_INT32

      ! initialize the variable
      given_data(itr)%code(:) = 0_INT8
      given_data(itr)%times   = 0_INT64

      ! read out the given string
      read(unit=INPUT_UNIT, fmt=*) given_data(itr)%string

    end do

    ! STEP.02
    ! count up the number of anagram
    num_anagram = 0_INT64

    do itr_origin = 1_INT32, num_string, 1_INT32

      call fetch_data_string(itr_origin)

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

    end do

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

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

    ! STEP.END
    return

  end subroutine task_C

end module ABC137

program main

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

  ! require all variables to be explicitly declared
  implicit none

  call task_C

end program main


module ABC137

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

  ! require all variables to be explicitly declared
  implicit none

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

  ! constants for this <module>
  integer(INT32), parameter, private :: len_string   = 10_INT32
  integer(INT32), parameter, private :: ichar_a      = ichar("a", kind=INT32)
  integer(INT32), parameter, private :: ichar_z      = ichar("z", kind=INT32)

  ! arrays for this <module>
  character(len=len_string, kind=1), allocatable, private :: given_string(:)
  integer(int8),                     allocatable, private :: data_string (:,:)

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

  subroutine fetch_data_string (index_target)

    ! arguments for this <subroutine>
    integer(INT32), intent(in) :: index_target

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

    do itr = 1_INT32, len_string, 1_INT32
      val_char = ichar(given_string(index_target)(itr:itr))
      data_string(val_char, index_target) = data_string(val_char, index_target) + 1_INT32
    end do

    ! STEP.END
    return

  end subroutine fetch_data_string

  pure function isAnagram (index_orign, index_target) result(stat)

    ! arguments for this <function>
    integer(INT32), intent(in) :: index_orign
    integer(INT32), intent(in) :: index_target

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

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

    ! STEP.01
    ! initialize the return value of this function
    stat = .true.

    ! STEP.02
    ! judge the status of given data
    do itr = ichar_a, ichar_z, 1_INT32
      if ( data_string(itr, index_orign) .ne. data_string(itr, index_target) ) then
        stat = .false.
        exit
      end if
    end do

    ! STEP.END
    return

  end function isAnagram

  subroutine task_C

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

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

    ! STEP.01
    ! read out the given data

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

    ! STEP.01.02
    ! allocate the array to store the given strings
    allocate( given_string(1:num_string) )
    allocate( data_string(ichar_a:ichar_z, 1:num_string) )

    ! STEP.01.03
    ! read out the given strings
    do itr = 1_INT32, num_string, 1_INT32
      read(unit=INPUT_UNIT, fmt=*) given_string(itr)
    end do

    ! STEP.02
    ! calcualte the answer of this task

    ! STEP.02.02
    ! fetch the data of the given string
    data_string(:,:) = 0_INT32

    do itr = 1_INT32, num_string, 1_INT32
      call fetch_data_string(itr)
    end do

    ! STEP.02.02
    ! count up the number of anagram
    num_anagram = 0_INT64

    do itr_origin = 1_INT32,              num_string - 1_INT32, 1_INT32
    do itr_target = itr_origin + 1_INT32, num_string,           1_INT32
      if ( isAnagram (itr_origin, itr_target) ) then
        num_anagram = num_anagram + 1_INT64
      end if
    end do
    end do

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

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

    ! STEP.END
    return

  end subroutine task_C

end module ABC137

program main

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

  ! require all variables to be explicitly declared
  implicit none

  call task_C

end program main


program kadai
implicit none
integer  ::  n,m
integer,allocatable  ::  a(:),b(:)
integer  ::  i,k,j
integer  ::  fir,las
integer  ::  ans,flag
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
fir=1
flag=0
las=1
do k=1,m
    do i=j,n
        if (a(i)==k) then
            flag=1
        end if
        if ((flag==1 .and. (a(i)>k .or. i==n))) then
            flag=0
            las=i-1
            if (i==n) then
                las=n
            end if
            exit
        end if
        if (a(i)>k) then
            flag=2
            exit
        end if
        j=j+1
    end do

    if (flag /= 2) then
        loc=maxloc(b(1:las),1)
        ans=ans+b(loc)
        b(loc)=0
    end if

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 main
implicit none
integer :: i, j, k, l, x, y, i1, i2, res
integer :: n, m
integer , allocatable :: a(:), b(:), c(:), bb(:), cc(:), d(:), dd(:)

read(*,*) n, m
allocate( a(n), b(n), c(n), bb(n), cc(n), d(n), dd(n) )
c = 0
do i = 1, n
   d(i) = i
   read(*,*) a(i), b(i)
end do
call quicksort2( n ,a, b, c, 1, n )
if( a(1) .gt. m ) then
   write(*,'(i0)') 0
   stop
end if
i2 = 1
res = 0
do i = m, 1, -1
   x = m+1-i
   i1 = i2
   do j = i1, n
      if( a(j) .le. x ) then
         i2 = j
      end if
      if( a(j) .gt. x ) exit
   end do
   y = 0
   call heapsort2( i2, b(1:i2), bb(1:i2), c(1:i2), cc(1:i2), d(1:i2), dd(1:i2) )
   do j = i2, 1, -1
      if( cc(j) == 0 ) then
         y = bb(j)
         c(dd(j)) = 1
         exit
      end if
   end do
   res = res + y
end do
write(*,'(i0)') res
!do i = 1, n
!   write(*,*) a(i), b(i), c(i)
!end do

stop
contains
recursive subroutine quicksort2(n, a, b, c, first, last)
  implicit none
  integer :: a(n), b(n), c(n)
  integer :: first, last
  integer ::  i, j, n
  integer ::  x, y, t, s, u

  x = a( (first+last) / 2 )
  i = first
  j = last
  do
     do while (a(i) < x)
        i=i+1
     end do
     do while (x < a(j))
        j=j-1
     end do
     if (i >= j) exit
     t = a(i)
    a(i) = a(j)
    a(j) = t
    s = b(i)
    b(i) = b(j)
    b(j) = s
    u = c(i)
    c(i) = c(j)
    c(j) = u
    i=i+1
    j=j-1
  end do
  if (first < i - 1) call quicksort2(n, a, b, c, first, i - 1)
  if (j + 1 < last)  call quicksort2(n, a, b, c, j + 1, last)
end subroutine quicksort2
subroutine heapsort2(n, a, aa, b, bb, c, cc )
  implicit none
  integer,intent(in) :: n
  integer,intent(in) :: a(1:n), b(1:n), c(1:n)
  integer,intent(out) :: aa(1:n), bb(1:n), cc(1:n)

  integer ::i,k,j,l
  double precision :: t, u, v
  aa = a
  bb = b
  cc = c
  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=aa(L)
        u=bb(L)
        v = cc(l)
     else
        t=aa(k)
        aa(k)=aa(1)
        u = bb(k)
        bb(k) = bb(1)
        v = cc(k)
        cc(k) = cc(1)
        k = k -1
        if(k.eq.1) then
           aa(1) = t
           bb(1) = u
           cc(1) = v
           exit
        endif
     endif
     i=l
     j=l+l
     do while(j.le.k)
        if(j.lt.k)then
           if(aa(j).lt.aa(j+1))j=j+1
        endif
        if (t.lt.aa(j))then
           aa(i)=aa(j)
           bb(i) = bb(j)
           cc(i) = cc(j)
           i=j
           j=j+j
        else
           j=k+1
        endif
     end do
     aa(i)=t
     bb(i) =u
     cc(i) = v
  end do

  return
end subroutine heapsort2
end program main


integer A,B,C
read*,a,b,c
print"(I0)",max(0,c-(a-b))
end

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 ABC136B
    implicit none
    integer::N,i,digits,j,ans=0
    read*,N
    do i=1,N
        j=i
        digits=1
        do
            if(j/10==0)exit
            j=j/10
            digits=digits+1
        end do
        if(mod(digits,2)==1)ans=ans+1
    end do
    print'(i0)',ans
end program ABC136B

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 abc136d
    use,intrinsic :: iso_fortran_env
    implicit none
    character(100000):: s
    integer(int32):: n,i,f,l,m
    integer(int32), allocatable:: ans(:)
    logical,allocatable:: col(:), sep(:)

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

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

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

    print*, ans(:)
end program abc136d

Score : 400 points

Problem Statement

Given is a string S consisting of L and R.

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

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

Initially, one child is standing on each square.

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

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

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

Constraints

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

Each character of S is L or R.

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

Input

Input is given from Standard Input in the following format:

S

Output

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

Sample Input 1

RRLRL

Sample Output 1

0 1 2 1 1

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

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

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

Sample Input 2

RRLLLLRLRRLL

Sample Output 2

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

Sample Input 3

RRRLLRLLRRRLLLLL

Sample Output 3

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


module deque_mod
    implicit none
    type deque
        integer(4),pointer:: v(:)
        integer(4):: l,r
        integer(4):: lmax, rmax
    end type
    private
    public:: deque
    public:: init_deque
    public:: append, appendleft
    public:: pop, popleft
    public:: right, left
    public:: remaining_elements
    public:: remain
    public:: show

contains
    subroutine init_deque(dq)
        ! 初期化
        type(deque):: dq
        dq%r=0; dq%rmax=1
        dq%l=1; dq%lmax=-1
        allocate(dq%v(dq%lmax:dq%rmax))
    end subroutine

    subroutine append(dq,num)
        ! 右端に挿入
        type(deque):: dq
        integer(4):: num
        if (dq%r+1 > dq%rmax) call add_(dq)
        dq%r=dq%r+1
        dq%v(dq%r) = num
    end subroutine

    subroutine appendleft(dq,num)
        ! 左端に挿入
        type(deque):: dq
        integer(4):: num
        if (dq%l-1 < dq%lmax) call add_(dq)
        dq%l=dq%l-1
        dq%v(dq%l) = num
    end subroutine

    subroutine add_(dq)
        ! arrayの延長
        type(deque):: dq
        integer(4):: l
        integer(4),pointer:: tmp(:)
        l = size(dq%v)
        allocate(tmp(l))
        tmp(:) = dq%v(:)
        deallocate(dq%v)
        allocate(dq%v(2*dq%lmax:2*dq%rmax))
        dq%v(dq%lmax:dq%rmax) = tmp(:)
        dq%lmax = 2*dq%lmax
        dq%rmax = 2*dq%rmax
    end subroutine

    function pop(dq) result(ret)
        ! 右端から取り出し
        type(deque):: dq
        integer(4):: ret
        ret = dq%v(dq%r)
        dq%r=dq%r-1
    end function

    function popleft(dq) result(ret)
        ! 左端から取り出し
        type(deque):: dq
        integer(4):: ret
        ret = dq%v(dq%l)
        dq%l=dq%l+1
    end function

    function right(dq) result(ret)
        ! 右端を確認
        type(deque):: dq
        integer(4):: ret
        ret = dq%v(dq%r)
    end function

    function left(dq) result(ret)
        ! 左端を確認
        type(deque):: dq
        integer(4):: ret
        ret = dq%v(dq%l)
    end function

    function remaining_elements(dq) result(ret)
        ! 残りの要素数
        type(deque):: dq
        integer(4):: ret
        ret = dq%r - dq%l + 1
    end function

    function remain(dq) result(ret)
        ! 要素が残っているかどうか
        type(deque):: dq
        logical:: ret
        ret = remaining_elements(dq) > 0
    end function

    function show(dq) result(ret)
        type(deque):: dq
        integer(4),allocatable:: ret(:)
        allocate(ret(remaining_elements(dq)))
        ret = dq%v(dq%l:dq%r)
    end function
end module

program Gathering_Children
    use deque_mod
    implicit none
    character(200000):: s
    character(1):: serch
    integer(4):: ls,cnt,i, l, nr,nl
    type(deque):: rc, lc
    integer(4),allocatable:: ans(:)

    call init_deque(rc)
    call init_deque(lc)

    read*, s
    ls = len_trim(s)
    allocate(ans(ls))

    serch = 'R'
    cnt=0
    do i=1,ls
        if (s(i:i) == serch) then
            cnt=cnt+1
        else
            if (serch == 'R') then
                call append(rc,cnt)
            else
                call append(lc,cnt)
            end if
            cnt=1
            serch = s(i:i)
        end if
    end do
    call append(lc,cnt)
    l = 0
    ans(:) = 0
    do while (remain(rc))
        nr = popleft(rc)
        nl = popleft(lc)
        l=l+nr
        if (mod(nr+nl,2) /= 0) then
            if (mod(nr,2) /= 0) then
                ans(l) = (nl+nr)/2+1
                ans(l+1) = (nl+nr)/2
            else
                ans(l) = (nl+nr)/2
                ans(l+1) = (nl+nr)/2+1
            end if
        else
            ans(l) = (nr+nl)/2
            ans(l+1) = (nr+nl)/2
        end if
        l=l+nl
    end do

    print*, ans(:)
end program Gathering_Children


program gathering_children
  implicit none
  integer :: n, a(100000) = 1, i
  character(100000) :: s
  read(*,*) s
  n = len_trim(s)
  do i = 1, n-2
    if (s(i:i) == "R" .and. s(i+1:i+1) == "R") then
      a(i+2) = a(i+2)+a(i)
      a(i) = 0
    end if
  end do
  do i = n, 3, -1
    if (s(i:i) == "L" .and. s(i-1:i-1) == "L") then
      a(i-2) = a(i-2)+a(i)
      a(i) = 0
    end if
  end do
  write(*,'(i0)',advance='no') a(1)
  do i = 2, n
    write(*,'(x,i0)',advance='no') a(i)
  end do
  write(*,*)
  stop
end program gathering_children


program main

integer :: a,b,k

read*,a,b

if(mod((a+b),2)==0) then
	print*,(a+b)/2
else
	print*,'IMPOSSIBLE'
end if
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 kadai
implicit none
integer  ::  a,b,i,k

read(*,*) a,b

if (a<b) then
    k=b
else if (a>b) then
    k=a
end if

do while(k>0)
    if (abs(a-k)==abs(b-k)) then
        write(*,*) k
        stop
    end if
    k=k-1
end do

write(*,*) "IMPOSSIBLE"

stop

end program kadai


implicit none

integer :: A, B

read *,A,B

if (mod(A-B,2) /= 0) then
   write(6,'(a)') 'IMPOSSIBLE'
else
   write(6,'(i0)') (A+B)/2
endif

stop
end


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

  read *, n
  allocate (p(n), q(n))
  read *, p
  q(:) = p(:)
  do k = 1, n
    if (q(k) /= k) ans = .false.
  end do
  if (ans) goto 10
  do i = 1, n
    do j = i+1, n
      q(:) = p(:)
      q(i) = p(j)
      q(j) = p(i)
      ans = .true.
      do k = 1, n
        if (q(k) /= k) ans = .false.
      end do
      if (ans) goto 10
    end do
  end do
  10 if (ans) then
    print '(a)', 'YES'
  else
    print '(a)', 'NO'
  end if
end program

Score : 200 points

Problem Statement

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

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

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

Constraints

All values in input are integers.

2 \leq N \leq 50

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

Input

Input is given from Standard Input in the following format:

N
p_1 p_2 ... p_N

Output

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

Sample Input 1

5
5 2 3 4 1

Sample Output 1

YES

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

Sample Input 2

5
2 4 3 5 1

Sample Output 2

NO

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

Sample Input 3

7
1 2 3 4 5 6 7

Sample Output 3

YES

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


program test
  implicit none
  integer :: n, i, flag, tmp
  integer, dimension ( : ), allocatable :: p

  read ( unit = *, fmt = * ) n

  allocate ( p ( 1:n ) )

  read ( unit = *, fmt = * ) p ( 1:n )

  flag = 0

  do i = 1, n

     if ( p ( i ) == i ) then

        continue

     else if ( p ( i ) /= i .and. p ( p ( i ) ) == i ) then

        tmp = p ( i )

        p ( i ) = p ( p ( i ) )

        p ( tmp ) = tmp

        flag = flag + 1

     else

        write ( unit = *, fmt = '(a2)' ) 'NO'

        stop

     end if

     if ( flag > 1 ) then

        write ( unit = *, fmt = '(a2)' ) 'NO'

        stop

     else

        write ( unit = *, fmt = '(a3)' ) 'YES'

        stop

     end if

  end do

end program test


program abc135b
   implicit none
   integer :: n, counter, i, j
   integer, allocatable :: p(:), q(:)

   read(*,*) n
   allocate(p(n), q(n))
   read(*,*) p(:)

   do i = 1, n
      q(i) = i
   end do

   counter = 0
   do i = 1, n
      if (p(i) /= q(i)) counter = counter + 1
   end do

   if ( counter <= 2) then
      write(*, *) 'YES'
   else
      write(*,*) 'NO'
   end if
end program abc135b


program main
  implicit none
  integer :: i, n, cnt = 0
  integer, allocatable :: p(:)

  read *, n
  allocate (p(n))
  read *, p
  do i = 1, n
    if (i == 1) cycle
    if (p(i) - p(i-1) /= 1) cnt = cnt + 1
  end do
  if (cnt <= 2) then
    print '(a)', 'YES'
  else
    print '(a)', 'NO'
  end if
end program


integer,allocatable,dimension(:)::A,B
integer N
integer check
read*,N
allocate(a(N),B(N))
read*,a
b=a
call heapsort(n,b)
check=0
do i=1,N
  if(a(i)==b(i))cycle
  check=check+1
end do
if(check<=2)then
  print"(A)","YES"
else
  print"(A)","NO"
endif
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 sample
    implicit none
    character(100)::s,t
    integer(8) :: i,j,k,m,n
    integer(8)::a,b,c,d,ans
    integer(8),allocatable::x(:),y(:)

    read(*,*) n
    allocate(x(n+1),y(n))
    read(*,*)x
    read(*,*)y
    a=0
    ans=0
    do i=1,n
        if(y(i)+a<=x(i))then
            a=0
            ans=ans+y(i)+a
        elseif(a>=x(i))then
            ans=ans+x(i)
            a=y(i)
        else
            ans=ans+x(i)
            a=y(i)+a-x(i)
        end if
    end do
    if(x(n+1)>a)then
        ans=ans+a
    else
        ans=ans+x(n+1)
    end if
    write(*,*)ans
    stop
end program sample

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


module ABC135

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

  ! require all variables to be explicitly declared
  implicit none

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

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

  subroutine task_C

    ! variables for this <subroutine>
    integer(INT32) :: num_city
    integer(INT32) :: num_Killed_total

    ! arrays for this <subroutine>
    integer(INT64), dimension(:),   allocatable :: num_monster
    integer(INT64), dimension(:,:), allocatable :: num_Killed
    integer(INT64), dimension(:),   allocatable :: num_ToKill

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

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

    ! STEP.02
    ! allocate the array to store the given data
    allocate( num_monster(1_INT32:num_city + 1_INT32) )
    allocate( num_ToKill(1_INT32:num_city) )
    allocate( num_Killed(1_INT32:2_INT32, 1_INT32:num_city) )

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

    ! STEP.04
    ! calculate the answer of this task

    num_Killed(:,:)              = 0_INT64
    num_Killed(1_INT32, 1_INT32) = min( num_monster(1_INT32), num_ToKill(1_INT32) )
    num_Killed(2_INT32, 1_INT32) = min( num_monster(2_INT32), num_ToKill(1_INT32) - num_Killed(1_INT32, 1_INT32) )

    do itr = 2_INT32, num_city, 1_INT32
      num_Killed(1_INT32, itr) = min( num_monster(itr) - num_Killed(1_INT32, itr - 1_INT32), num_ToKill(itr) )
      num_Killed(2_INT32, itr) = min( num_monster(itr + 1_INT32), num_ToKill(itr) - num_Killed(1_INT32, itr) )
    end do

    num_Killed_total = 0_INT64

    do itr = 1_INT32, num_city, 1_INT32
        num_Killed_total       &!
        = num_Killed_total     &!
        + num_Killed(1_INT32, itr) &!
        + num_Killed(2_INT32, itr)
    end do

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

    ! STEP.08
    ! deallocate the array to store the given data
    deallocate( num_monster        )
    deallocate( num_ToKill )
    deallocate( num_Killed )

    ! STEP.END
    return

  end subroutine task_C

end module ABC135

program main

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

  call task_C

end program main


program main
  implicit none
  integer i,kill1,kill2,N
  integer,allocatable :: A(:),B(:),A1(:)
  read(*, *) N
  allocate(A(N+1))
  allocate(A1(N+1))
  allocate(B(N))
  read(*, *) (A(i), i = 1,N+1)
  read(*, *) (B(i), i = 1,N)
  A1 = A

  kill1 = 0
  do i = 1,N
    if (A(i) < B(i)) then
      kill1 = kill1 + A(i)
      kill1 = kill1 + min(A(i+1),B(i)-A(i))
      A(i+1) = A(i+1)-min(A(i+1),B(i)-A(i))
    else
      kill1 = kill1 + B(i)
    end if
  end do

  A = A1
  kill2 = 0
  do i = N,1,-1
    if (A(i+1) < B(i)) then
      kill2 = kill2 + A(i+1)
      kill2 = kill2 + min(A(i),B(i)-A(i+1))
      A(i) = A(i)-min(A(i),B(i)-A(i+1))
    else
      kill2 = kill2 + B(i)
    end if
  end do

  write(*, *) max(kill1,kill2)
end program main


program main
  implicit none
  integer i,kill,N
  integer(8),allocatable :: A(:),B(:)
  read(*, *) N
  allocate(A(N+1))
  allocate(B(N))
  read(*, *) (A(i), i = 1,N+1)
  read(*, *) (B(i), i = 1,N)
  kill = 0
  do i = 1,N
    if (A(i) < B(i)) then
      kill = kill + A(i)
      kill = kill + min(A(i+1),B(i)-A(i))
      A(i+1) = A(i+1)-min(A(i+1),B(i)-A(i))
    else
      kill = kill + B(i)
    end if
  end do
  write(*, *) kill
end program main


program main
    implicit none
    integer n, d
    integer km, i
    integer warn(2)
    integer, allocatable :: m_num(:), y_num(:)
    read*,n
    allocate(m_num(n+1), y_num(n))

    read*, m_num
    read*, y_num

    km = 0

    do i=1,n
        print*, '--- ---'
        print*, y_num(i), m_num(i)
        if(i-1 > 0)then
            if(y_num(i-1) > 0)then
                y_num(i) = y_num(i) + y_num(i-1)
                y_num(i-1) = 0

            endif
        endif

        if(y_num(i) >= m_num(i))then
            km = km + m_num(i)
            y_num(i) = y_num(i) - m_num(i)
            m_num(i) = 0

        elseif(y_num(i) < m_num(i) )then
            d = m_num(i) - y_num(i)

            if (d > y_num(i+1))then
                y_num(i) = y_num(i) + y_num(i+1)
                y_num(i+1) = 0
                m_num(i) = m_num(i) - y_num(i)
                km = km + y_num(i)

            else
                km = km + y_num(i)
                y_num(i) = 0
                m_num(i) = d
                km = km + y_num(i)
            endif

        endif

        print*, y_num(i), m_num(i), km
    enddo

    if(y_num(n) > m_num(n+1))then
        km = km + m_num(n+1)
    else
        km = km + y_num(n)
    endif

    print'(i0)', km

end program main