module mod_wavelet_matrix
  implicit none
  integer, private, parameter :: intkind = 4
  integer, private, parameter :: bitsize = 64
  integer(8), private, parameter :: mask = -1
  type space_saving_array
    integer :: n, size, offset
    integer(8), allocatable :: bit(:)
    integer, allocatable :: cnt(:)
  end type
  type wavelet_matrix
    integer :: n, m
    type(space_saving_array), allocatable :: bit(:)
    integer, private, allocatable :: order(:), start(:)
    integer(intkind), private, allocatable :: vals(:)
  contains
    procedure :: access => access_to
    procedure :: rank => rank_of
    procedure :: select => select_from
    procedure :: quantile => quantile
    procedure :: range_less => range_less
    procedure :: range_freq => range_freq
  end type
  interface wavelet_matrix
    module procedure :: new_wm
  end interface
  private :: space_saving_array, new_ssa, access_ssa, rank_ssa, select_ssa
  private :: find
contains
  type(space_saving_array) function new_ssa(a) result(res)
    logical, intent(in) :: a(:)
    integer :: n, m, i, j, k
    n = size(a)
    m = (n + bitsize - 1) / bitsize
    res%n = n
    res%size = m
    allocate(res%bit(m), res%cnt(0:m))
    res%bit = 0
    j = 1
    k = 0
    do i = 1, n
      if (a(i)) res%bit(j) = ibset(res%bit(j), k)
      k = k + 1
      if (k == bitsize) then
        j = j + 1
        k = 0
      end if
    end do
    res%cnt(0) = 0
    do i = 1, m
      res%cnt(i) = res%cnt(i - 1) + popcnt(res%bit(i))
    end do
    res%offset = n - res%cnt(m)
  end
  logical function access_ssa(this, idx) result(res)
    type(space_saving_array), intent(in) :: this
    integer, intent(in) :: idx
    integer :: i, j
    i = (idx - 1) / bitsize + 1
    j = idx - 1 - (i - 1) * bitsize
    res = btest(this%bit(i), j)
  end
  integer function rank_ssa(this, idx, bool) result(res)
    type(space_saving_array), intent(in) :: this
    integer, intent(in) :: idx
    logical, intent(in) :: bool
    integer :: i, j
    i = (idx - 1) / bitsize + 1
    j = idx - (i - 1) * bitsize
    ! res = this%cnt(i - 1) + popcnt(and(not(lshift(mask, j)), this%bit(i)))
    res = this%cnt(i) - popcnt(and(lshift(mask, j), this%bit(i)))
    if (bool) return
    res = idx - res
  end
  integer function select_ssa(this, ord, bool) result(res)
    type(space_saving_array), intent(in) :: this
    integer, intent(in) :: ord
    logical, intent(in) :: bool
    integer :: l, r, m, s
    res = -1
    s = merge(this%cnt(this%size), this%n - this%cnt(this%size), bool)
    if (s < ord) return
    l = 0
    r = this%size + 1
    do while (r - l > 1)
      m = (l + r) / 2
      s = merge(this%cnt(m), bitsize * m - this%cnt(m), bool)
      if (s < ord) then
        l = m
      else
        r = m
      end if
    end do
    l = bitsize * (r - 1)
    r = bitsize * r + 1
    do while (r - l > 1)
      m = (l + r) / 2
      if (rank_ssa(this, m, bool) < ord) then
        l = m
      else
        r = m
      end if
    end do
    res = r
  end
  type(wavelet_matrix) function new_wm(a, sigma) result(res)
    integer(intkind), intent(in) :: a(:)
    integer(intkind), optional, intent(in) :: sigma
    integer(intkind) :: maxa, x(size(a)), y(size(a)), z(size(a))
    integer, allocatable :: s(:), t(:), u(:)
    integer :: n, m, nx, ny, i, j
    logical :: bool(size(a))
    maxa = merge(sigma, maxval(a), present(sigma))
    n = size(a)
    m = 8 * intkind - 1
    do while (.not.btest(maxa, m))
      m = m - 1
    end do
    res%n = n
    res%m = m
    allocate(res%bit(0:m))
    z = a
    do j = 0, m
      nx = 0
      ny = 0
      do i = 1, n
        bool(i) = btest(z(i), m - j)
        if (bool(i)) then
          ny = ny + 1
          y(ny) = z(i)
        else
          nx = nx + 1
          x(nx) = z(i)
        end if
      end do
      res%bit(j) = new_ssa(bool)
      z(1:nx) = x(1:nx)
      z(nx + 1:n) = y(1:ny)
    end do
    nx = n
    n = 1
    do i = 2, nx
      if (z(i) /= z(i - 1)) n = n + 1
    end do
    allocate(s(n), t(n), u(n), res%order(n), res%start(n), res%vals(n))
    u = [(i, i = 1, n)]
    n = 1
    res%start(1) = 1
    do i = 2, nx
      if (z(i) /= z(n)) then
        n = n + 1
        z(n) = z(i)
        res%start(n) = i
      end if
    end do
    do j = 0, m
      nx = 0
      ny = 0
      do i = 1, n
        if (btest(z(i), j)) then
          ny = ny + 1
          y(ny) = z(i)
          t(ny) = u(i)
        else
          nx = nx + 1
          x(nx) = z(i)
          s(nx) = u(i)
        end if
      end do
      z(1:nx) = x(1:nx)
      z(nx + 1:n) = y(1:ny)
      u(1:nx) = s(1:nx)
      u(nx + 1:n) = t(1:ny)
    end do
    res%order = u
    res%vals = z(1:n)
  end
  integer function find(this, val) result(res)
    type(wavelet_matrix), intent(in) :: this
    integer(intkind) :: val
    integer :: l, r
    l = 0
    r = size(this%vals) + 1
    do while (r - l > 1)
      res = (l + r) / 2
      if (this%vals(res) < val) then
        l = res
      else if (this%vals(res) > val) then
        r = res
      else
        return
      end if
    end do
    res = -1
  end
  integer(intkind) function access_to(this, idx) result(res)
    class(wavelet_matrix), intent(in) :: this
    integer, intent(in) :: idx
    logical :: bool
    integer :: i, j
    i = idx
    res = 0
    do j = 0, this%m
      bool = access_ssa(this%bit(j), i)
      i = rank_ssa(this%bit(j), i, bool)
      if (bool) then
        res = ibset(res, this%m - j)
        i = i + this%bit(j)%offset
      end if
    end do
  end
  integer function rank_of(this, idx, val) result(res)
    class(wavelet_matrix), intent(in) :: this
    integer, intent(in) :: idx
    integer(intkind), intent(in) :: val
    logical :: bool
    integer :: i, j, k
    res = 0
    k = find(this, val)
    if (k < 0) return
    k = this%order(k)
    i = idx
    do j = 0, this%m
      bool = btest(val, this%m - j)
      i = rank_ssa(this%bit(j), i, bool)
      if (bool) i = i + this%bit(j)%offset
    end do
    res = i - this%start(k) + 1
  end
  integer function select_from(this, ord, val) result(res)
    class(wavelet_matrix), intent(in) :: this
    integer, intent(in) :: ord
    integer(intkind), intent(in) :: val
    logical :: bool
    integer :: i, j, k, l
    res = -1
    k = find(this, val)
    if (k < 0) return
    k = this%order(k)
    i = this%start(k) + ord - 1
    if (k == size(this%start)) then
      if (i > this%n) return
    else
      if (i >= this%start(k + 1)) return
    end if
    do j = this%m, 0, -1
      bool = btest(val, this%m - j)
      if (bool) i = i - this%bit(j)%offset
      i = select_ssa(this%bit(j), i, bool)
    end do
    res = i
  end
  integer(intkind) function quantile(this, a, b, ord) result(res)
    class(wavelet_matrix), intent(in) :: this
    integer, intent(in) :: a, b, ord
    logical :: bool
    integer :: l, r, i, j, d
    res = -1
    if (b - a + 1 < ord) return
    l = a
    r = b
    i = ord
    res = 0
    do j = 0, this%m
      d = (r - l + 1) - (rank_ssa(this%bit(j), r, .true.) - rank_ssa(this%bit(j), l - 1, .true.))
      bool = i > d
      l = rank_ssa(this%bit(j), l - 1, bool) + 1
      r = rank_ssa(this%bit(j), r, bool)
      if (bool) then
        res = ibset(res, this%m - j)
        i = i - d
        l = l + this%bit(j)%offset
        r = r + this%bit(j)%offset
      end if
    end do
  end
  integer function range_less(this, a, b, val) result(res)
    class(wavelet_matrix), intent(in) :: this
    integer, intent(in) :: a, b
    integer(intkind), intent(in) :: val
    logical :: bool
    integer :: l, r, i, j, d
    res = 0
    if (a > b) return
    l = a
    r = b
    do j = 0, this%m
      d = (r - l + 1) - (rank_ssa(this%bit(j), r, .true.) - rank_ssa(this%bit(j), l - 1, .true.))
      bool = btest(val, this%m - j)
      l = rank_ssa(this%bit(j), l - 1, bool) + 1
      r = rank_ssa(this%bit(j), r, bool)
      if (bool) then
        res = res + d
        l = l + this%bit(j)%offset
        r = r + this%bit(j)%offset
      end if
    end do
  end
  integer function range_freq(this, a, b, x, y) result(res)
    class(wavelet_matrix), intent(in) :: this
    integer, intent(in) :: a, b
    integer(intkind), intent(in) :: x, y
    res = 0
    if (x > y) return
    res = range_less(this, a, b, y + 1) - range_less(this, a, b, x)
  end
end module mod_wavelet_matrix
program range_set_query
  use mod_wavelet_matrix
  implicit none
  integer :: n, q, l, r, i
  integer, dimension(500000) :: c = 0, a = 0, b = 0
  type(wavelet_matrix) :: wm
  read(*,*) n, q
  read(*,*) c(1:n)
  b(1:n) = n + 1
  do i = 1, n
    a(i) = b(c(i))
    b(c(i)) = i
  end do
  wm = wavelet_matrix(a(1:n))
  do i = 1, q
    read(*,*) l, r
    write(*,'(i0)') r - l + 1 - wm%range_freq(l, r, l, r)
  end do
end program range_set_query

Score : 600 points

Problem Statement

We have N colored balls arranged in a row from left to right; the color of the i-th ball from the left is c_i.

You are given Q queries. The i-th query is as follows: how many different colors do the l_i-th through r_i-th balls from the left have?

Constraints

1\leq N,Q \leq 5 \times 10^5

1\leq c_i \leq N

1\leq l_i \leq r_i \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
c_1 c_2 \cdots c_N
l_1 r_1
l_2 r_2
:
l_Q r_Q

Output

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

Sample Input 1

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

Sample Output 1

2
3
1

The 1-st, 2-nd, and 3-rd balls from the left have the colors 1, 2, and 1 - two different colors.

The 2-st, 3-rd, and 4-th balls from the left have the colors 2, 1, and 3 - three different colors.

The 3-rd ball from the left has the color 1 - just one color.

Sample Input 2

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

Sample Output 2

1
2
2
1
2
2
6
3
3
3


program abc19
 implicit none
 integer::X,s
 read(*,*)X
 if(X<=599)then
  s=8
 else if(X<=799)then
  s=7
 else if(X<=999)then
  s=6
 else if(X<=1199)then
  s=5
 else if(X<=1399)then
  s=4
 else if(X<=1599)then
  s=3
 else if(X<=1799)then
  s=2
 else
  s=1
 end if
 print'(i0)',s
end program abc19

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

Score: 100 points

Problem Statement

M-kun is a competitor in AtCoder, whose highest rating is X.

In this site, a competitor is given a kyu (class) according to his/her highest rating. For ratings from 400 through 1999, the following kyus are given:

From 400 through 599: 8-kyu

From 600 through 799: 7-kyu

From 800 through 999: 6-kyu

From 1000 through 1199: 5-kyu

From 1200 through 1399: 4-kyu

From 1400 through 1599: 3-kyu

From 1600 through 1799: 2-kyu

From 1800 through 1999: 1-kyu

What kyu does M-kun have?

Constraints

400 \leq X \leq 1999

X is an integer.

Input

Input is given from Standard Input in the following format:

X

Output

Print the kyu M-kun has, as an integer.
For example, if he has 8-kyu, print 8.

Sample Input 1

725

Sample Output 1

7

M-kun's highest rating is 725, which corresponds to 7-kyu.

Thus, 7 is the correct output.

Sample Input 2

1600

Sample Output 2

2

M-kun's highest rating is 1600, which corresponds to 2-kyu.

Thus, 2 is the correct output.

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 kyu_in_atcoder
  implicit none
  integer :: x, k
  read(*,*) x
  write(*,'(i0)') 10 - x / 200
end program kyu_in_atcoder


program pr1
  implicit none
  integer :: a, b, c, k, btmp, ctmp
  integer :: i, j
  read(*,*) a,b,c
  read(*,*) k

  do i = 0, k
     do j = 0, i
        btmp = b*(2**j)
        ctmp = c*(2**(i-j))
        if ( a < btmp .and. btmp < ctmp ) then
           write(*,'(a)') 'Yes'
           stop
        end if
     end do
  end do
  write(*,'(a)') 'No'

  stop
end program pr1

Score: 200 points

Problem Statement

M-kun has the following three cards:

A red card with the integer A.

A green card with the integer B.

A blue card with the integer C.

He is a genius magician who can do the following operation at most K times:

Choose one of the three cards and multiply the written integer by 2.

His magic is successful if both of the following conditions are satisfied after the operations:

The integer on the green card is strictly greater than the integer on the red card.

The integer on the blue card is strictly greater than the integer on the green card.

Determine whether the magic can be successful.

Constraints

1 \leq A, B, C \leq 7

1 \leq K \leq 7

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C
K

Output

If the magic can be successful, print Yes; otherwise, print No.

Sample Input 1

7 2 5
3

Sample Output 1

Yes

The magic will be successful if, for example, he does the following operations:

First, choose the blue card. The integers on the red, green, and blue cards are now 7, 2, and 10, respectively.

Second, choose the green card. The integers on the red, green, and blue cards are now 7, 4, and 10, respectively.

Third, choose the green card. The integers on the red, green, and blue cards are now 7, 8, and 10, respectively.

Sample Input 2

7 4 2
3

Sample Output 2

No

He has no way to succeed in the magic with at most three operations.


program MSOLUTIONSC
    implicit none
    integer(8)::N,K,i,l,r
    integer(8),allocatable,dimension(:)::A
    read*,N,K
    allocate(A(N))
    read*,A

    l=A(1)
    do i=2,N-K+1
        if(A(i+K-1)>l)then
            print'(A)',"Yes"
        else
            print'(A)',"No"
        end if
        l=A(i)
    end do
end program MSOLUTIONSC

Score: 300 points

Problem Statement

M-kun is a student in Aoki High School, where a year is divided into N terms.

There is an exam at the end of each term. According to the scores in those exams, a student is given a grade for each term, as follows:

For the first through (K-1)-th terms: not given.

For each of the K-th through N-th terms: the multiplication of the scores in the last K exams, including the exam in the graded term.

M-kun scored A_i in the exam at the end of the i-th term.

For each i such that K+1 \leq i \leq N, determine whether his grade for the i-th term is strictly greater than the grade for the (i-1)-th term.

Constraints

2 \leq N \leq 200000

1 \leq K \leq N-1

1 \leq A_i \leq 10^{9}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 A_3 \ldots A_N

Output

Print the answer in N-K lines.

The i-th line should contain Yes if the grade for the (K+i)-th term is greater than the grade for the (K+i-1)-th term, and No otherwise.

Sample Input 1

5 3
96 98 95 100 20

Sample Output 1

Yes
No

His grade for each term is computed as follows:

3-rd term: (96 \times 98 \times 95) = 893760

4-th term: (98 \times 95 \times 100) = 931000

5-th term: (95 \times 100 \times 20) = 190000

Sample Input 2

3 2
1001 869120 1001

Sample Output 2

No

Note that the output should be No if the grade for the 3-rd term is equal to the grade for the 2-nd term.

Sample Input 3

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

Sample Output 3

Yes
Yes
No
Yes
Yes
No
Yes
Yes


      PROGRAM Marks
      IMPLICIT NONE
      INTEGER,PARAMETER :: p = 16
      integer(p) :: n,k, i
      integer(p),allocatable :: a(:),score(:)

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

      do i = k+1,n
        if( a(i-k)<a(i) )then
          print*,'Yes'
        else
          print*,'No'
        end if
      end do

 !     score = 0
 !     score(k) = 1
 !     do i = 1,k
 !       score(k) = score(k)*a(i)
 !     end do
 !     do i = k+1,n
 !       score(i) = score(i-1)/a(i-k)*a(i)
 !     end do

 !     do i = k+1,n
 !       if( score(i)>score(i-1) )then
 !         print*,'Yes'
 !       else
 !         print*,'No'
 !       end if
 !     end do

      stop
      !debugg

      do i = k,n
        print*,score(i)
      end do

      END PROGRAM


program answer
  implicit none
  integer(8) :: i, N, sum, ka
  integer(8), allocatable :: A(:)
  read(*,*) N
  allocate(A(N))
  read(*,*) A

  sum=1000
  ka=0

  do i = 1, N-1
     if(A(i)<=A(i+1)) then
        ka=sum/A(i)
        sum=sum-ka*A(i)
     else
        ka=0
     end if
     sum=sum+ka*A(i+1)
     ka=0
  end do
  write(*,*) sum
  deallocate(A)
  stop
 end program answer

Score: 400 points

Problem Statement

To become a millionaire, M-kun has decided to make money by trading in the next N days. Currently, he has 1000 yen and no stocks - only one kind of stock is issued in the country where he lives.

He is famous across the country for his ability to foresee the future. He already knows that the price of one stock in the next N days will be as follows:

A_1 yen on the 1-st day, A_2 yen on the 2-nd day, ..., A_N yen on the N-th day.

In the i-th day, M-kun can make the following trade any number of times (possibly zero), within the amount of money and stocks that he has at the time.

Buy stock: Pay A_i yen and receive one stock.

Sell stock: Sell one stock for A_i yen.

What is the maximum possible amount of money that M-kun can have in the end by trading optimally?

Constraints

2 \leq N \leq 80

100 \leq A_i \leq 200

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \cdots A_N

Output

Print the maximum possible amount of money that M-kun can have in the end, as an integer.

Sample Input 1

7
100 130 130 130 115 115 150

Sample Output 1

1685

In this sample input, M-kun has seven days of trading. One way to have 1685 yen in the end is as follows:

Initially, he has 1000 yen and no stocks.

Day 1: Buy 10 stocks for 1000 yen. Now he has 0 yen.

Day 2: Sell 7 stocks for 910 yen. Now he has 910 yen.

Day 3: Sell 3 stocks for 390 yen. Now he has 1300 yen.

Day 4: Do nothing.

Day 5: Buy 1 stock for 115 yen. Now he has 1185 yen.

Day 6: Buy 10 stocks for 1150 yen. Now he has 35 yen.

Day 7: Sell 11 stocks for 1650 yen. Now he has 1685 yen.

There is no way to have 1686 yen or more in the end, so the answer is 1685.

Sample Input 2

6
200 180 160 140 120 100

Sample Output 2

1000

In this sample input, it is optimal to do nothing throughout the six days, after which we will have 1000 yen.

Sample Input 3

2
157 193

Sample Output 3

1216

In this sample input, it is optimal to buy 6 stocks in Day 1 and sell them in Day 2, after which we will have 1216 yen.


program answer
  implicit none
  integer :: i, N, sum, ka
  integer, allocatable :: A(:)
  read(*,*) N
  allocate(A(N))
  read(*,*) A

  sum=1000

  do i = 1, N-1
     if(A(i)<A(i+1)) then
        sum=int(sum/A(i))*(A(i+1)-A(i))+sum
     end if
  end do

  write(*,*) sum

  deallocate(A)
  stop
 end program answer


program main
    implicit none
    integer(8) n
    integer(8), allocatable, dimension(:) :: x,y
    character(:), allocatable :: u(:)
    integer(8) i, j, c
    integer dx, dy, dt, dt_min
    read(*,*) n
    allocate(x(n))
    allocate(y(n))
    allocate(character(1) :: u(n))
    do i = 1, n
        read(*,*) x(i), y(i), u(i)
    end do

    dt_min = 400000
    dt = dt_min
    c = 0

    do i = 1, n
        do j = i+1, n
            dx = x(j) - x(i)
            dy = y(j) - y(i)
            if(u(j) == "U") then
                if(dy < 0) then
                    if(dx < 0) then
                        if(u(i) == "L") then
                            if(abs(dx) == abs(dy)) then
                                dt = abs(dy)*10
                                c = c + 1
                            end if
                        end if
                    else if(dx == 0) then
                        if(u(i) == "D") then
                            dt = abs(dy)*5
                            c = c + 1
                        end if
                    else if(dx > 0) then
                        if(u(i) == "R") then
                            if(abs(dx) == abs(dy)) then
                                dt = abs(dy)*10
                                c = c + 1
                            end if
                        end if
                    end if
                end if
            else if(u(j) == "D") then
                if(dy > 0) then
                    if(dx < 0) then
                        if(u(i) == "L") then
                            if(abs(dx) == abs(dy)) then
                                dt = abs(dy)*10
                                c = c + 1
                            end if
                        end if
                    else if(dx == 0) then
                        if(u(i) == "U") then
                            dt = abs(dy)*5
                            c = c + 1
                        end if
                    else if(dx > 0) then
                        if(u(i) == "R") then
                            if(abs(dx) == abs(dy)) then
                                dt = abs(dy)*10
                                c = c + 1
                            end if
                        end if
                    end if
                end if
            else if(u(j) == "R") then
                if(dx < 0) then
                    if(dy < 0) then
                        if(u(i) == "D") then
                            if(abs(dx) == abs(dy)) then
                                dt = abs(dy)*10
                                c = c + 1
                            end if
                        end if
                    else if(dy == 0) then
                        if(u(i) == "L") then
                            dt = abs(dx)*5
                            c = c + 1
                        end if
                    else if(dy > 0) then
                        if(u(i) == "U") then
                            if(abs(dx) == abs(dy)) then
                                dt = abs(dy)*10
                                c = c + 1
                            end if
                        end if
                    end if
                end if
            else if(u(j) == "L") then
                if(dx > 0) then
                    if(dy < 0) then
                        if(u(i) == "D") then
                            if(abs(dx) == abs(dy)) then
                                dt = abs(dy)*10
                                c = c + 1
                            end if
                        end if
                    else if(dy == 0) then
                        if(u(i) == "R") then
                            dt = abs(dx)*5
                            c = c + 1
                        end if
                    else if(dy > 0) then
                        if(u(i) == "U") then
                            if(abs(dx) == abs(dy)) then
                                dt = abs(dy)*10
                                c = c + 1
                            end if
                        end if
                    end if
                end if
            end if
            if (dt < dt_min) then
                dt_min = dt
            end if
        end do
    end do

    if (c == 0) then
        write(*,*) "SAFE"
    else
        write(*,*) dt_min
    end if

end program

Score: 600 points

Problem Statement

M-kun is a brilliant air traffic controller.

On the display of his radar, there are N airplanes numbered 1, 2, ..., N, all flying at the same altitude.

Each of the airplanes flies at a constant speed of 0.1 per second in a constant direction. The current coordinates of the airplane numbered i are (X_i, Y_i), and the direction of the airplane is as follows:

if U_i is U, it flies in the positive y direction;

if U_i is R, it flies in the positive x direction;

if U_i is D, it flies in the negative y direction;

if U_i is L, it flies in the negative x direction.

To help M-kun in his work, determine whether there is a pair of airplanes that will collide with each other if they keep flying as they are now.

If there is such a pair, find the number of seconds after which the first collision will happen.

We assume that the airplanes are negligibly small so that two airplanes only collide when they reach the same coordinates simultaneously.

Constraints

1 \leq N \leq 200000

0 \leq X_i, Y_i \leq 200000

U_i is U, R, D, or L.

The current positions of the N airplanes, (X_i, Y_i), are all distinct.

N, X_i, and Y_i are integers.

Input

Input is given from Standard Input in the following format:

N
X_1 Y_1 U_1
X_2 Y_2 U_2
X_3 Y_3 U_3
:
X_N Y_N U_N

Output

If there is a pair of airplanes that will collide with each other if they keep flying as they are now, print an integer representing the number of seconds after which the first collision will happen.

If there is no such pair, print SAFE.

Sample Input 1

2
11 1 U
11 47 D

Sample Output 1

230

If the airplanes keep flying as they are now, two airplanes numbered 1 and 2 will reach the coordinates (11, 24) simultaneously and collide.

Sample Input 2

4
20 30 U
30 20 R
20 10 D
10 20 L

Sample Output 2

SAFE

No pair of airplanes will collide.

Sample Input 3

8
168 224 U
130 175 R
111 198 D
121 188 L
201 116 U
112 121 R
145 239 D
185 107 L

Sample Output 3

100


program abc7
 implicit none
 integer::L,R,d,s,i
 read(*,*)L,R,d
 do i=L,R
  if(mod(i,d)==0)then
   s=s+1
  end if
 end do
 print'(i0)',s
end program abc7

Score : 100 points

Problem Statement

How many multiples of d are there among the integers between L and R (inclusive)?

Constraints

All values in input are integers.

1 \leq L \leq R \leq 100

1 \leq d \leq 100

Input

Input is given from Standard Input in the following format:

L R d

Output

Print the number of multiples of d among the integers between L and R (inclusive).

Sample Input 1

5 10 2

Sample Output 1

3

Among the integers between 5 and 10, there are three multiples of 2: 6, 8, and 10.

Sample Input 2

6 20 7

Sample Output 2

2

Among the integers between 6 and 20, there are two multiples of 7: 7 and 14.

Sample Input 3

1 100 1

Sample Output 3

100


program answer
  implicit none
  integer :: N, i, count
  integer, allocatable :: a(:)
  read(*,*) N
  allocate(a(N))
  read(*,*) a

  count=0
  do i = 1, N, 2
     if(mod(a(i),2)==1) then
        count=count+1
     end if
  end do

  write(*,*) count
  deallocate(a)
  stop
 end program answer

Score : 200 points

Problem Statement

We have N squares assigned the numbers 1,2,3,\ldots,N. Each square has an integer written on it, and the integer written on Square i is a_i.

How many squares i satisfy both of the following conditions?

The assigned number, i, is odd.

The written integer is odd.

Constraints

All values in input are integers.

1 \leq N, a_i \leq 100

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 \cdots a_N

Output

Print the number of squares that satisfy both of the conditions.

Sample Input 1

5
1 3 4 5 7

Sample Output 1

2

Two squares, Square 1 and 5, satisfy both of the conditions.

For Square 2 and 4, the assigned numbers are not odd.

For Square 3, the written integer is not odd.

Sample Input 2

15
13 76 46 15 50 98 93 77 31 43 84 90 6 24 14

Sample Output 2

3


program atcoder
implicit none
integer :: n , x , y , z , i , sum
real :: j
read *,n
sum = 0
j = real(n)
do i = 1 , n
  sum = 0
  do x = int(sqrt(j/6.0)) , int(sqrt(j))+1
    do y = x , int(sqrt(j))+1
      do z = y , int(sqrt(j))+1
        if(x**2 + y**2 + z**2 + x*y + y*z + z*x == i)then
          sum = sum + 1
          if(x == y .and. y == z)goto 10
        end if
      end do
    end do
  end do
  print *, sum * 3
  goto 20
10 print *, sum * 3 - 2
20 end do
end program atcoder

Score : 300 points

Problem Statement

Let f(n) be the number of triples of integers (x,y,z) that satisfy both of the following conditions:

1 \leq x,y,z

x^2 + y^2 + z^2 + xy + yz + zx = n

Given an integer N, find each of f(1),f(2),f(3),\ldots,f(N).

Constraints

All values in input are integers.

1 \leq N \leq 10^4

Input

Input is given from Standard Input in the following format:

N

Output

Print N lines. The i-th line should contain the value f(i).

Sample Input 1

20

Sample Output 1

0
0
0
0
0
1
0
0
0
0
3
0
0
0
0
0
3
3
0
0

For n=6, only (1,1,1) satisfies both of the conditions. Thus, f(6) = 1.

For n=11, three triples, (1,1,2), (1,2,1), and (2,1,1), satisfy both of the conditions. Thus, f(6) = 3.

For n=17, three triples, (1,2,2), (2,1,2), and (2,2,1), satisfy both of the conditions. Thus, f(17) = 3.

For n=18, three triples, (1,1,3), (1,3,1), and (3,1,1), satisfy both of the conditions. Thus, f(18) = 3.


program abc9
 implicit none
 integer::N,i,x,y,z,s,t,sa=0,sb=0,sc=0
 read(*,*)N
 do i=1,N
  t=int(sqrt(dble(i))-1)+1
  s=0
  sa=0
  sb=0
  sc=0
  do x=1,t
   do y=x,t
    do z=y,t
     if(x**2+y**2+z**2+x*y+y*z+z*x==i)then
      if(x==y.and.y==z)then
       sb=sb+1
      else if(x==y.or.y==z.or.z==x)then
       sa=sa+1
      else
       sc=sc+1
      end if
     end if
    end do
   end do
  end do
 s=sc*6+sa*3+sb
  print'(i0)',s
 end do
end program abc9


program tsundoku
  implicit none
  integer :: n, m, i,j, h = 0, l, r, p
  integer(8) :: k, t
  integer(8), dimension(0:200001) :: a = 0, b = 0, x = 0, y = 0
  read(*,*) n
    do t=1,n
    m=int(sqrt(dble(t)))
    p=0
	do i=1,m
    	do j=1,m
        	do k=1,m
            	if((i*i+j*j+k*k+i*j+j*k+i*k)==t)then
                p=p+1
                end if
            end do
        end do
    end do
    write(*,*)p
    end do
end program tsundoku


program testcase
    integer(8) n
    integer i
    integer :: ac = 0
    integer :: tle = 0
    integer :: wa = 0
    integer :: re = 0
    character(3) str
    !character, allocatable :: s(:)
    read *, n
    !allocate (s(n))
    do i = 1, n
        read *, str
        if(str .eq. "AC")then
            ac = ac + 1
        else if(str .eq. "WA")then
            wa = wa + 1
        else if(str .eq. "RE")then
            re = re + 1
        else if(str .eq. "TLE")then
            tle = tle + 1
        end if
    end do
    print *, 'AC ×', ac
    print *, 'WA ×', wa
    print *, 'TLE ×', tle
    print *, 'RE ×', re
end program

Score : 200 points

Problem Statement

Takahashi is participating in a programming contest called AXC002, and he has just submitted his code to Problem A.

The problem has N test cases.

For each test case i (1\leq i \leq N), you are given a string S_i representing the verdict for that test case. Find the numbers of test cases for which the verdict is AC, WA, TLE, and RE, respectively.

See the Output section for the output format.

Constraints

1 \leq N \leq 10^5

S_i is AC, WA, TLE, or RE.

Input

Input is given from Standard Input in the following format:

N
S_1
\vdots
S_N

Output

Let C_0, C_1, C_2, and C_3 be the numbers of test cases for which the verdict is AC, WA, TLE, and RE, respectively. Print the following:

AC x C_0
WA x C_1
TLE x C_2
RE x C_3

Sample Input 1

6
AC
TLE
AC
AC
WA
TLE

Sample Output 1

AC x 3
WA x 1
TLE x 2
RE x 0

We have 3, 1, 2, and 0 test case(s) for which the verdict is AC, WA, TLE, and RE, respectively.

Sample Input 2

10
AC
AC
AC
AC
AC
AC
AC
AC
AC
AC

Sample Output 2

AC x 10
WA x 0
TLE x 0
RE x 0


      PROGRAM piyo
      IMPLICIT NONE
      INTEGER,PARAMETER :: prec = 16
      integer(prec) :: n,i,awtr(4)=0,num = 1
      character(3) :: s

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

        select case( s )
          case( 'AC ')
            num = 1
          case( 'WA ' )
            num = 2
          case( 'TLE' )
            num = 3
          case( 'RE' )
            num = 4
        end select

        awtr(num) = awtr(num) + 1
      end do

      print'(a,i0)','AC x ',awtr(1)
      print'(a,i0)','WA x ',awtr(2)
      print'(a,i0)','TLE x ',awtr(3)
      print'(a,i0)','RE x ',awtr(4)

      stop
      !debugg

      END PROGRAM


program prob3
    implicit none
    integer::h, w, k, i, j, l, cnt, it, jt
    integer, allocatable::c(:,:), tmp(:,:), tmpp(:,:)
    character(len=10)::s
    read(*,*) h, w, k
    allocate(c(h,w), tmp(h,w), tmpp(h,w))
    do i = 1, h
        read(*,*) s
        do j = 1, w
            if(s(j:j) .eq. ".") then
                c(i,j) = 0
            else
                c(i,j) = 1
            end if
        end do
    end do
    cnt = 0
    do i = 0, 2**h-1
        tmp = c
        it = i
        do j = 1, h
            if(mod(it,2) == 1) tmp(j,:) = 0
            it = it/2
        end do
        do j = 0, 2**w-1
        	tmpp = tmp
            jt = j
            do l = 1, w
                if(mod(jt,2) == 1) tmpp(:,l) = 0
                jt = jt/2
            end do
            if(sum(tmpp) == k) cnt = cnt + 1
        end do
    end do
    write(*,*) cnt
    deallocate(c,tmp,tmpp)
    stop
end program

Score : 300 points

Problem Statement

We have a grid of H rows and W columns of squares. The color of the square at the i-th row from the top and the j-th column from the left (1 \leq i \leq H, 1 \leq j \leq W) is given to you as a character c_{i,j}: the square is white if c_{i,j} is ., and black if c_{i,j} is #.

Consider doing the following operation:

Choose some number of rows (possibly zero), and some number of columns (possibly zero). Then, paint red all squares in the chosen rows and all squares in the chosen columns.

You are given a positive integer K. How many choices of rows and columns result in exactly K black squares remaining after the operation? Here, we consider two choices different when there is a row or column chosen in only one of those choices.

Constraints

1 \leq H, W \leq 6

1 \leq K \leq HW

c_{i,j} is . or #.

Input

Input is given from Standard Input in the following format:

H W K
c_{1,1}c_{1,2}...c_{1,W}
c_{2,1}c_{2,2}...c_{2,W}
:
c_{H,1}c_{H,2}...c_{H,W}

Output

Print an integer representing the number of choices of rows and columns satisfying the condition.

Sample Input 1

2 3 2
..#
###

Sample Output 1

5

Five choices below satisfy the condition.

The 1-st row and 1-st column

The 1-st row and 2-nd column

The 1-st row and 3-rd column

The 1-st and 2-nd column

The 3-rd column

Sample Input 2

2 3 4
..#
###

Sample Output 2

1

One choice, which is choosing nothing, satisfies the condition.

Sample Input 3

2 2 3
##
##

Sample Output 3

0

Sample Input 4

6 6 8
..##..
.#..#.
#....#
######
#....#
#....#

Sample Output 4

208


program ABC173D
    implicit none
    integer(8)::N,i,ans=0
    integer(8),allocatable,dimension(:)::A
    read*,N
    allocate(A(N))
    read*,A
    call heapsort(N,A)
    do i=1,N-1
        ans=ans+A(N-i/2)
    end do

    print'(i0)',ans
end program ABC173D

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

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

    if(n.eq.1)return

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

    return
end subroutine heapsort

Score: 400 points

Problem Statement

Quickly after finishing the tutorial of the online game ATChat, you have decided to visit a particular place with N-1 players who happen to be there. These N players, including you, are numbered 1 through N, and the friendliness of Player i is A_i.

The N players will arrive at the place one by one in some order. To make sure nobody gets lost, you have set the following rule: players who have already arrived there should form a circle, and a player who has just arrived there should cut into the circle somewhere.

When each player, except the first one to arrive, arrives at the place, the player gets comfort equal to the smaller of the friendliness of the clockwise adjacent player and that of the counter-clockwise adjacent player. The first player to arrive there gets the comfort of 0.

What is the maximum total comfort the N players can get by optimally choosing the order of arrivals and the positions in the circle to cut into?

Constraints

All values in input are integers.

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

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \dots A_N

Output

Print the maximum total comfort the N players can get.

Sample Input 1

4
2 2 1 3

Sample Output 1

7

By arriving at the place in the order Player 4, 2, 1, 3, and cutting into the circle as shown in the figure, they can get the total comfort of 7.

They cannot get the total comfort greater than 7, so the answer is 7.

Sample Input 2

7
1 1 1 1 1 1 1

Sample Output 2

6


module module_sort
    use iso_fortran_env
    implicit none

    interface quicksort
        module procedure :: int32_qsort
        module procedure :: int64_qsort
    end interface quicksort
contains

    recursive function int32_qsort(m) result(res)
        integer(int32), intent(in) :: m(:)
        integer(int32) :: res(size(m))

        if (size(m) <= 1) then; res = m
        else; res = [int32_qsort( pack(m(2:), m(2:) >= m(1)) ), m(1), int32_qsort( pack(m(2:), m(2:) < m(1)) )]
        end if

    end function int32_qsort

    recursive function int64_qsort(m) result(res)
        integer(int64), intent(in) :: m(:)
        integer(int64) :: res(size(m))

        if (size(m) <= 1) then; res = m
        else; res = [int64_qsort( pack(m(2:), m(2:) >= m(1)) ), m(1), int64_qsort( pack(m(2:), m(2:) < m(1)) )]
        end if

    end function int64_qsort

end module module_sort

program main
    use module_sort
    use iso_fortran_env
    implicit none
    integer(8) :: n
    integer(8), allocatable :: ai(:)
    integer(8) :: i, ie
    integer(8) :: ans

    read *, n
    allocate( ai(n) )
    read *, ai( 1:n )
!    ai = quicksort( ai )
!    print *, int32, int64, int128
!    call qsort( ai )
!    ai(n:1:-1) = ai(1:n)
    ai = intQsort( ai )
    ai(n:1:-1) = ai(1:n)

    if ( mod((n-1), 2) == 0 ) then
        ie = (n-1) / 2
        ans = ai(1) + ai(ie + 1)
    else
        ie = n / 2
        ans = ai(1)
    endif

    do i = 2, ie
        ans = ans + ai(i) * 2
    enddo

    print *, ans

contains

recursive subroutine qsort(a)
  implicit none
  integer(8),intent(inout) :: a(:)
  integer(8)::i,j,toobig,toosmall,pv,n

  n = size(a)
i = 1
j = n
pv = a((i+j)/2)
do
   do
     if (a(i) < pv) then
       i = i+1
     else
       exit
     end if
   end do
   do
     if(a(j) > pv) then
       j = j-1
     else
       exit
     end if
   end do
   if (i>=j) then
     exit
   else
     toobig = a(j)
     toosmall = a(i)
     a(i) = toobig
     a(j) = toosmall
     i = i+1
     j = j-1
   end if
 end do
 if (i > 2) then
   call qsort(a(1:i-1))
 end if
 if (j < n-1) then
   call qsort(a(j+1:n))
 end if
end subroutine qsort

pure recursive function intQsort( a ) result( r )
implicit none
integer(8), intent(in) :: a(:)
integer(8) :: r( size(a) )
integer(8) :: sample(3), len, i,j

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

  r = [ intQsort( pack(a(:), a(:)< sample(2)) ), &
                  pack(a(:), a(:)==sample(2))  , &
        intQsort( pack(a(:), a(:)> sample(2)) ) ]
end if
end function intQsort

end program main


module module_sort
    use iso_fortran_env
    implicit none

    interface quicksort
        module procedure :: int32_qsort
        module procedure :: int64_qsort
    end interface quicksort
contains

    recursive function int32_qsort(m) result(res)
        integer(int32), intent(in) :: m(:)
        integer(int32) :: res(size(m))

        if (size(m) <= 1) then; res = m
        else; res = [int32_qsort( pack(m(2:), m(2:) >= m(1)) ), m(1), int32_qsort( pack(m(2:), m(2:) < m(1)) )]
        end if

    end function int32_qsort

    recursive function int64_qsort(m) result(res)
        integer(int64), intent(in) :: m(:)
        integer(int64) :: res(size(m))

        if (size(m) <= 1) then; res = m
        else; res = [int64_qsort( pack(m(2:), m(2:) >= m(1)) ), m(1), int64_qsort( pack(m(2:), m(2:) < m(1)) )]
        end if

    end function int64_qsort

end module module_sort

program main
    use module_sort
    use iso_fortran_env
    implicit none
    integer(int64) :: n
    integer(int64), allocatable :: ai(:)
    integer(int64) :: i, ie
    integer(int64) :: ans

    read *, n
    allocate( ai(n) )
    read *, ai( 1:n )
    ans = 0

    ai = quicksort( ai )

    if ( mod((n-1), 2) == 0 ) then
        ie = (n-1) / 2
        ans = 2 * sum( ai(1:ie) ) - ai(1) + ai(ie + 1)
    else
        ie = n / 2
        ans = 2 * sum( ai(1:ie) ) - ai(1)
    endif

    print *, ans

end program main


program main
  implicit none
  integer(8) i, ans, frag, r, N
  integer(8),allocatable :: A(:)

  read(*, *) N
  allocate(A(N))
  read(*, *) (A(i), i = 1,N)
  call qsort(A)
  ans = 0
  r = N
  frag = 1
  do i = 1,N-1
    ans = ans + A(r)
    if (frag == 1) then
      frag = 0
      r = r-1
    end if
  end do

  write(*, *) ans

contains

recursive subroutine qsort(a)
implicit none
!integer(8),intent(in) :: n
integer(8),intent(inout) :: a(:)
integer(8)::i,j,toobig,toosmall,pv,n
n = size(a)
i = 1
j = n
pv = a((i+j)/2)
do
   do
     if (a(i) < pv) then
       i = i+1
     else
       exit
     end if
   end do
   do
     if(a(j) > pv) then
       j = j-1
     else
       exit
     end if
   end do
   if (i>=j) then
     exit
   else
     toobig = a(j)
     toosmall = a(i)
     a(i) = toobig
     a(j) = toosmall
     i = i+1
     j = j-1
   end if
 end do
 if (i > 2) then
   call qsort(a(1:i-1))
 end if
 if (j < n-1) then
   call qsort(a(j+1:n))
 end if
end subroutine qsort
end program main


program multiplication_4
  implicit none
  integer(8), parameter :: m = 1000000007
  integer :: n, k, i, j, cnt = 0
  integer(8) :: a(200000, 2) = 0, nega = -1, posi = -1, x = 1, y = -1, z = -1
  read(*,*) n, k
  read(*,*) a(1:n, 1)
  do i = 1, n
    if (a(i, 1) >= 0) then
      a(i, 2) = 0
    else
      a(i, 1) = -a(i, 1)
      a(i, 2) = 1
    end if
  end do
  call merge_sort(a(1:n, :))
  do i = 1, k
    x = mod(x * a(i, 1), m)
    if (a(i, 2) == 1) then
      cnt = cnt + 1
      nega = a(i, 1)
    else
      posi = a(i, 1)
    end if
  end do
  if (x == 0 .or. mod(cnt, 2) == 0) then
    write(*,'(i0)') x
    stop
  end if
  if (posi /= -1) then
    do i = k + 1, n
      if (a(i, 2) == 1) then
        y = mod(x * a(i, 1), m)
        y = mod(y * inv(posi), m)
        exit
      end if
    end do
  end if
  if (nega /= -1) then
    do i = k + 1, n
      if (a(i, 2) == 0) then
        z = mod(x * a(i, 1), m)
        z = mod(z * inv(nega), m)
        exit
      end if
    end do
  end if
  if (y /= -1 .or. z /= -1) then
    write(*,'(i0)') max(y, z)
    stop
  end if
  j = n
  do while (j >= 1 .and. a(j, 1) == 0)
    j = j - 1
  end do
  if (n - j >= k .or. j - k + 1 < 1) then
    write(*,'(i0)') 0
    stop
  end if
  x = 1
  do i = j, j - k + 1, -1
    x = mod(x * a(i, 1), m)
  end do
  write(*,'(i0)') modulo(-x, m)
contains
  subroutine merge_sort(a)
    integer(8), intent(inout) :: a(:, :)
    integer :: n, m, l, i, u
    n = size(a, 1)
    m = n
    l = 1
    do while (m > 1)
      do i = 1, m / 2
        u = min(2 * i * l, n)
        call merger(a(2 * (i - 1) * l + 1:(2 * i - 1) * l, :), a((2 * i - 1) * l + 1:u, :))
      end do
      l = 2 * l
      m = (m + 1) / 2
    end do
  end
  subroutine merger(a1, a2)
    integer(8), intent(inout) :: a1(:, :), a2(:, :)
    integer(8) :: a(size(a1, 1) + size(a2, 1), size(a1, 2))
    integer :: i1, i2, n1, n2
    i1 = 1
    i2 = 1
    n1 = size(a1, 1)
    n2 = size(a2, 1)
    do while (i1 <= n1 .and. i2 <= n2)
      if (a1(i1, 1) > a2(i2, 1) .or. &
      (a1(i1, 1) == a2(i2, 1) .and. a1(i1, 2) <= a2(i2, 2))) then
        a(i1+i2-1, :) = a1(i1, :)
        i1 = i1+1
      else
        a(i1+i2-1, :) = a2(i2, :)
        i2 = i2+1
       end if
    end do
    if (i1 <= n1) then
      a(i1 + i2 - 1:n1 + n2, :) = a1(i1:n1, :)
    else
      a(i1 + i2 - 1:n1 + n2, :) = a2(i2:n2, :)
    end if
    a1 = a(1:n1, :)
    a2 = a(n1 + 1:n1 + n2, :)
  end
  pure elemental integer(8) function inv(v) result(res)
    integer(8), intent(in) :: v
    integer(8) :: a, b, c, x, y, z
    a = v
    b = m
    c = 0
    res = 1
    do while (b /= 0)
      x = b
      y = mod(a, b)
      z = res - a / b * c
      res = c
      a = x
      b = y
      c = z
    end do
    res = modulo(res, m)
  end
end program multiplication_4

Score : 500 points

Problem Statement

Given are N integers A_1,\ldots,A_N.

We will choose exactly K of these elements. Find the maximum possible product of the chosen elements.

Then, print the maximum product modulo (10^9+7), using an integer between 0 and 10^9+6 (inclusive).

Constraints

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

|A_i| \leq 10^9

Input

Input is given from Standard Input in the following format:

N K
A_1 \ldots A_N

Output

Print the maximum product modulo (10^9+7), using an integer between 0 and 10^9+6 (inclusive).

Sample Input 1

4 2
1 2 -3 -4

Sample Output 1

12

The possible products of the two chosen elements are 2, -3, -4, -6, -8, and 12, so the maximum product is 12.

Sample Input 2

4 3
-1 -2 -3 -4

Sample Output 2

1000000001

The possible products of the three chosen elements are -24, -12, -8, and -6, so the maximum product is -6.

We print this value modulo (10^9+7), that is, 1000000001.

Sample Input 3

2 1
-1 1000000000

Sample Output 3

1000000000

The possible products of the one chosen element are -1 and 1000000000, so the maximum product is 1000000000.

Sample Input 4

10 10
1000000000 100000000 10000000 1000000 100000 10000 1000 100 10 1

Sample Output 4

999983200

Be sure to print the product modulo (10^9+7).


program r20628c

implicit none
integer :: c(1:26)
integer, allocatable :: interval(:, :)
integer, allocatable :: s(:, :)
integer, allocatable :: t(:), v(:)
integer :: i, j, d, m, h
integer, allocatable :: smalld(:)
integer, allocatable :: q(:)

read *, d

allocate(s(1:26, 1:d))
allocate(interval(1:26, 0:d))
allocate(t(1:d))
allocate(v(0:d))

interval(:, :) = 0 !interval(i,d) = d - last(d,i)
v(0) = 0

read *, c(1:26)

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

do i = 1, d
 read *, t(i)

 v(i) = v(i-1) + s(t(i), i)

 do j = 1, 26
  if(t(i) == j) then
   interval(j, i) = 0
  else
   interval(j, i) = interval(j, i-1) + 1
  end if
  v(i) = v(i) - (c(j) * interval(j, i))
 end do

! print *, v(i)
end do

read *, m

allocate(smalld(1:m))
allocate(q(1:m))

do h = 1, m
 read *, smalld(h), q(h)
 t(smalld(h)) = q(h)

  v(smalld(h)) = v(smalld(h)-1) + s(q(h), smalld(h))

  do j = 1, 26

   if(q(h) == j) then
    interval(j, smalld(h)) = 0
   else
    interval(j, smalld(h)) = interval(j, smalld(h)-1) + 1
   end if

   v(smalld(h)) = v(smalld(h)) - (c(j) * interval(j, smalld(h)))
  end do

 do i = smalld(h)+1, d

 v(i) = v(i-1) + s(t(i), i)

  do j = 1, 26
   if(t(i) == j) then
    interval(j, i) = 0
   else
    interval(j, i) = interval(j, i-1) + 1
   end if
   v(i) = v(i) - (c(j) * interval(j, i))
  end do
 end do

 print *, v(d)

end do

end program r20628c

(Please read problem A first. The maximum score you can get by solving this problem C is 1, which will have almost no effect on your ranking.)

Beginner's Guide

"Local search" is a powerful method for finding a high-quality solution.
In this method, instead of constructing a solution from scratch, we try to find a better solution by slightly modifying the already found solution.
If the solution gets better, update it, and if it gets worse, restore it.
By repeating this process, the quality of the solution is gradually improved over time.
The pseudo-code is as follows.

solution = compute an initial solution (by random generation, or by applying other methods such as greedy)
while the remaining time > 0:
slightly modify the solution (randomly)
if the solution gets worse:
restore the solution

For example, in this problem, we can use the following modification: pick the date d and contest type q at random and change the type of contest to be held on day d to q.
The pseudo-code is as follows.

t[1..D] = compute an initial solution (by random generation, or by applying other methods such as greedy)
while the remaining time > 0:
pick d and q at random
old = t[d] # Remember the original value so that we can restore it later
t[d] = q
if the solution gets worse:
t[d] = old

The most important thing when using the local search method is the design of how to modify solutions.

If the amount of modification is too small, we will soon fall into a dead-end (local optimum) and, conversely, if the amount of modification is too large, the probability of finding an improving move becomes extremely small.

In order to increase the number of iterations, it is desirable to be able to quickly calculate the score after applying a modification.

In this problem C, we focus on the second point.
The score after the modification can, of course, be obtained by calculating the score from scratch.
However, by focusing on only the parts that have been modified, it may be possible to quickly compute the difference between the scores before and after the modification.
From another viewpoint, the impossibility of such a fast incremental calculation implies that a small modification to the solution affects a majority of the score calculation.
In such a case, we may need to redesign how to modify solutions, or there is a high possibility that the problem is not suitable for local search.
Let's implement fast incremental score computation.
It's time to demonstrate the skills of algorithms and data structures you have developed in ABC and ARC!

In this kind of contest, where the objective is to find a better solution instead of the optimal one, a bug in a program does not result in a wrong answer, which may delay the discovery of the bug.
For early detection of bugs, it is a good idea to unit test functions you implemented complicated routines.
For example, if you implement fast incremental score calculation, it is a good idea to test that the scores computed by the fast implementation match the scores computed from scratch, as we will do in this problem C.

Problem Statement

You will be given a contest schedule for D days and M queries of schedule modification.
In the i-th query, given integers d_i and q_i, change the type of contest to be held on day d_i to q_i, and then output the final satisfaction at the end of day D on the updated schedule.
Note that we do not revert each query. That is, the i-th query is applied to the new schedule obtained by the (i-1)-th query.

Input

Input is given from Standard Input in the form of the input of Problem A followed by the output of Problem A and the queries.

D
c_1 c_2 \cdots c_{26}
s_{1,1} s_{1,2} \cdots s_{1,26}
\vdots
s_{D,1} s_{D,2} \cdots s_{D,26}
t_1
t_2
\vdots
t_D
M
d_1 q_1
d_2 q_2
\vdots
d_M q_M

The constraints and generation methods for the input part are the same as those for Problem A.

For each d=1,\ldots,D, t_d is an integer generated independently and uniformly at random from {1,2,\ldots,26}.

The number of queries M is an integer satisfying 1\leq M\leq 10^5.

For each i=1,\ldots,M, d_i is an integer generated independently and uniformly at random from {1,2,\ldots,D}.

For each i=1,\ldots,26, q_i is an integer satisfying 1\leq q_i\leq 26 generated uniformly at random from the 25 values that differ from the type of contest on day d_i.

Output

Let v_i be the final satisfaction at the end of day D on the schedule after applying the i-th query.
Print M integers v_i to Standard Output in the following format:

v_1
v_2
\vdots
v_M

Sample Input 1

5
86 90 69 51 2 96 71 47 88 34 45 46 89 34 31 38 97 84 41 80 14 4 50 83 7 82
19771 12979 18912 10432 10544 12928 13403 3047 10527 9740 8100 92 2856 14730 1396 15905 6534 4650 11469 3628 8433 2994 10899 16396 18355 11424
6674 17707 13855 16407 12232 2886 11908 1705 5000 1537 10440 10711 4917 10770 17272 15364 19277 18094 3929 3705 7169 6159 18683 15410 9092 4570
6878 4239 19925 1799 375 9563 3445 5658 19857 11401 6997 6498 19933 3848 2426 2146 19745 16880 17773 18359 3921 14172 16730 11157 5439 256
8633 15862 15303 10749 18499 7792 10317 5901 9395 11433 3514 3959 5202 19850 19469 9790 5653 784 18500 10552 17975 16615 7852 197 8471 7452
19855 17918 7990 10572 4333 438 9140 9104 12622 4985 12319 4028 19922 12132 16259 17476 2976 547 19195 19830 16285 4806 4471 9457 2864 2192
1
17
13
14
13
5
1 7
4 11
3 4
5 24
4 19

Sample Output 1

72882
56634
38425
27930
42884

Note that this example is a small one for checking the problem specification. It does not satisfy the constraint D=365 and is never actually given as a test case.

Next Step

Let's go back to Problem A and implement the local search algorithm by utilizing the incremental score calculator you just implemented!
For this problem, the current modification "pick the date d and contest type q at random and change the type of contest to be held on day d to q" is actually not so good. By considering why it is not good, let's improve the modification operation.
One of the most powerful and widely used variant of the local search method is "Simulated Annealing (SA)", which makes it easier to reach a better solution by stochastically accepting worsening moves.
For more information about SA and other local search techniques, please refer to the editorial that will be published after the contest.


program main
  implicit none
  integer i, a
  read(*, *) a
  !read(*, *) (L(i), i = 1,N)
  i = a + a**2 + a**3
  write(*, *) i
end program main

Score : 100 points

Problem Statement

Given an integer a as input, print the value a + a^2 + a^3.

Constraints

1 \leq a \leq 10

a is an integer.

Input

Input is given from Standard Input in the following format:

a

Output

Print the value a + a^2 + a^3 as an integer.

Sample Input 1

2

Sample Output 1

14

When a = 2, we have a + a^2 + a^3 = 2 + 2^2 + 2^3 = 2 + 4 + 8 = 14.

Print the answer as an input. Outputs such as 14.0 will be judged as incorrect.

Sample Input 2

10

Sample Output 2

1110


program answer
 implicit none

 integer :: a

 read(*,*) a

 write(*,*) a+a**2+a**3

 stop
end program answer


program sample
    implicit none

    integer(8) :: i,j,m,n

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

    read(*,*) n

    write(*,*) n+n**2+n**3

    stop
end program sample


program million_change
    character :: S(200001)
    character :: T(200001)
    integer i
    read *, S(200001)
    read *, T(200001)
    do i = 1, size(S)
        if(S(i) /= T(i))then
            S(i)  = T(i)
        end if
    end do
end program

Score : 200 points

Problem Statement

Given are strings S and T. Consider changing S to T by repeating the operation below. Find the minimum number of operations required to do so.

Operation: Choose one character of S and replace it with a different character.

Constraints

S and T have lengths between 1 and 2\times 10^5 (inclusive).

S and T consists of lowercase English letters.

S and T have equal lengths.

Input

Input is given from Standard Input in the following format:

S
T

Output

Print the answer.

Sample Input 1

cupofcoffee
cupofhottea

Sample Output 1

4

We can achieve the objective in four operations, such as the following:

First, replace the sixth character c with h.

Second, replace the eighth character f with t.

Third, replace the ninth character f with t.

Fourth, replace the eleventh character e with a.

Sample Input 2

abcde
bcdea

Sample Output 2

5

Sample Input 3

apple
apple

Sample Output 3

0

No operations may be needed to achieve the objective.


program sample
    implicit none

    integer(8) :: i,j,m,n
    character(len=200000)::s,t
    integer(8),allocatable :: x(:),y(:)

    read(*,*)s
    read(*,*)t
    n=len_trim(s)
    m=0
    do i=1,n
        if(s(i:i) .ne. t(i:i))then
            m=m+1
        end if
    end do
    write(*,*) m

    stop
end program sample


program unfair_nim
  implicit none
  integer :: n, i
  integer(8) :: a(300) = 0, s, x = 0, d, y = 0
  read(*,*) n
  read(*,*) a(1:n)
  s = a(1) + a(2)
  do i = 3, n
    x = xor(x, a(i))
  end do
  if (s - x < 0 .or. mod(s - x, 2) /= 0) then
    write(*,'(i0)') -1
    stop
  end if
  d = (s - x) / 2
  if (and(d, x) /= 0 .or. d > a(1)) then
    write(*,'(i0)') -1
    stop
  end if
  do i = 62, 0, -1
    if (.not.btest(x, i)) cycle
    y = ibset(y, i)
    if (xor(d, y) > a(1)) y = ibclr(y, i)
  end do
  write(*,'(i0)') a(1) - xor(d, y)
end program unfair_nim

Score : 600 points

Problem Statement

There are N piles of stones. The i-th pile has A_i stones.

Aoki and Takahashi are about to use them to play the following game:

Starting with Aoki, the two players alternately do the following operation:

Operation: Choose one pile of stones, and remove one or more stones from it.

When a player is unable to do the operation, he loses, and the other player wins.

When the two players play optimally, there are two possibilities in this game: the player who moves first always wins, or the player who moves second always wins, only depending on the initial number of stones in each pile.

In such a situation, Takahashi, the second player to act, is trying to guarantee his win by moving at least zero and at most (A_1 - 1) stones from the 1-st pile to the 2-nd pile before the game begins.

If this is possible, print the minimum number of stones to move to guarantee his victory; otherwise, print -1 instead.

Constraints

2 \leq N \leq 300

1 \leq A_i \leq 10^{12}

Input

Input is given from Standard Input in the following format:

N
A_1 \ldots A_N

Output

Print the minimum number of stones to move to guarantee Takahashi's win; otherwise, print -1 instead.

Sample Input 1

2
5 3

Sample Output 1

1

Without moving stones, if Aoki first removes 2 stones from the 1-st pile, Takahashi cannot win in any way.

If Takahashi moves 1 stone from the 1-st pile to the 2-nd before the game begins so that both piles have 4 stones, Takahashi can always win by properly choosing his actions.

Sample Input 2

2
3 5

Sample Output 2

-1

It is not allowed to move stones from the 2-nd pile to the 1-st.

Sample Input 3

3
1 1 2

Sample Output 3

-1

It is not allowed to move all stones from the 1-st pile.

Sample Input 4

8
10 9 8 7 6 5 4 3

Sample Output 4

3

Sample Input 5

3
4294967297 8589934593 12884901890

Sample Output 5

1

Watch out for overflows.


program abc171
    use,intrinsic :: iso_fortran_env
    implicit none
    character(1):: r

    read*, r
    if (ichar(r) < ichar('a')) then
        print'(a)', 'A'

    else
        print'(a)', 'a'
    end if
end program abc171

Score : 100 points

Problem Statement

An uppercase or lowercase English letter \alpha will be given as input.
If \alpha is uppercase, print A; if it is lowercase, print a.

Constraints

\alpha is an uppercase (A - Z) or lowercase (a - z) English letter.

Input

Input is given from Standard Input in the following format:

α

Output

If \alpha is uppercase, print A; if it is lowercase, print a.

Sample Input 1

B

Sample Output 1

A

B is uppercase, so we should print A.

Sample Input 2

a

Sample Output 2

a

a is lowercase, so we should print a.


program sample
implicit none

integer(8) :: N, K, i, sum, m, j
integer(8), allocatable:: p(:)

read(*,*) N, K
allocate(p(N))
read(*,*) p

do j = 1, N-1
do i = 1, N-1
if(p(i) > p(i+1))then
m=p(i)
p(i)=p(i+1)
p(i+1)=m
end if
end do
end do

sum = 0
do i = 1, K
sum = sum + p(i)
end do

write(*,*) sum

deallocate(p)
stop

end program sample

Score : 200 points

Problem Statement

A shop sells N kinds of fruits, Fruit 1, \ldots, N, at prices of p_1, \ldots, p_N yen per item, respectively. (Yen is the currency of Japan.)

Here, we will choose K kinds of fruits and buy one of each chosen kind. Find the minimum possible total price of those fruits.

Constraints

1 \leq K \leq N \leq 1000

1 \leq p_i \leq 1000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
p_1 p_2 \ldots p_N

Output

Print an integer representing the minimum possible total price of fruits.

Sample Input 1

5 3
50 100 80 120 80

Sample Output 1

210

This shop sells Fruit 1, 2, 3, 4, and 5 for 50 yen, 100 yen, 80 yen, 120 yen, and 80 yen, respectively.

The minimum total price for three kinds of fruits is 50 + 80 + 80 = 210 yen when choosing Fruit 1, 3, and 5.

Sample Input 2

1 1
1000

Sample Output 2

1000


program sample
implicit none

integer(8) :: N, K, i, sum, m
integer(8), allocatable:: p(:)

read(*,*) N, K
allocate(p(N))
read(*,*) p

if(N > 1)then
do i = 1, N-1
if(p(i) > p(i+1))then
m=p(i)
p(i)=p(i+1)
p(i+1)=m
endif
end do

sum = 0
do i = 1, K
sum = sum + p(i)
end do

write(*,*) sum
end if

if(N == 1)then
sum = p(1)
write(*,*) sum
end if

deallocate(p)
stop

end program sample


program answer2
 implicit none

 integer :: i, j, N, K, m
 integer, allocatable :: p(:)

 read(*,*) N, K
 allocate(p(N))
 read(*,*) p

 do j =1, N-1
   do i = 1, N-1
   if(p(i)>p(i+1)) then
     m=p(i)
     p(i)=p(i+1)
     p(i+1)=m
   end if
   end do
 end do

 m=0
 do i =1, K
   m= m+p(i)
 end do

 write(*,*) m
 deallocate(p)
 stop
end program answer2


program main
!    use iso_fortran_env
    implicit none

    integer(16) :: n, m
    character(:),allocatable :: txt
    character(26) :: alp

    alp = "zabcdefghijklmnopqrstuvwxy"

    read(*,*) n
    txt = ""

    do
        m = mod(n, 26)
        n = int(n / 26)
        if (m == 0) then
            n = n - 1
        endif
        txt = alp(m+1:m+1)//txt
        if (n == 0) exit
    enddo

    print *, trim(txt)

end program main

Score : 300 points

Problem Statement

1000000000000001 dogs suddenly appeared under the roof of Roger's house, all of which he decided to keep. The dogs had been numbered 1 through 1000000000000001, but he gave them new names, as follows:

the dogs numbered 1,2,\cdots,26 were respectively given the names a, b, ..., z;

the dogs numbered 27,28,29,\cdots,701,702 were respectively given the names aa, ab, ac, ..., zy, zz;

the dogs numbered 703,704,705,\cdots,18277,18278 were respectively given the names aaa, aab, aac, ..., zzy, zzz;

the dogs numbered 18279,18280,18281,\cdots,475253,475254 were respectively given the names aaaa, aaab, aaac, ..., zzzy, zzzz;

the dogs numbered 475255,475256,\cdots were respectively given the names aaaaa, aaaab, ...;

and so on.

To sum it up, the dogs numbered 1, 2, \cdots were respectively given the following names:

a, b, ..., z, aa, ab, ..., az, ba, bb, ..., bz, ..., za, zb, ..., zz, aaa, aab, ..., aaz, aba, abb, ..., abz, ..., zzz, aaaa, ...

Now, Roger asks you:

"What is the name for the dog numbered N?"

Constraints

N is an integer.

1 \leq N \leq 1000000000000001

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer to Roger's question as a string consisting of lowercase English letters.

Sample Input 1

2

Sample Output 1

b

Sample Input 2

27

Sample Output 2

aa

Sample Input 3

123456789

Sample Output 3

jjddja


program one_quadrillion_and_one_dalmatians
  implicit none
  integer(8) :: n
  integer :: i = 0, k(11) = 0
  read(*,*) n
  do while (n > 0)
    i = i + 1
    k(i) = int(mod(n - 1, 26) + 1) + 96
    n = n / 26
  end do
  do while (i > 0)
    write(*,'(a)',advance='no') achar(k(i))
    i = i - 1
  end do
  write(*,*)
end program one_quadrillion_and_one_dalmatians


program main
    use iso_fortran_env
    implicit none
    integer(int32) :: n, q
    integer(int64) :: an(10000000)
    integer(int32),allocatable :: a(:), b(:), c(:)
    integer(int32) :: i
    integer(int64) :: s

    read *, n
    allocate(a(n))
    read *, (a(i), i = 1, n)
    read *, q
    allocate(b(q), c(q))
    read *, (b(i), c(i), i = 1, q)

    an = 0
    do i = 1, n
        an(a(i)) = an(a(i)) + 1
    enddo

    s = sum(a)

    do i = 1, q
        s = s + (c(i) - b(i)) * an(b(i))
        an(c(i)) = an(c(i)) + an(b(i))
        an(b(i)) = 0
        print *, s
    enddo

end program main

Score : 400 points

Problem Statement

You have a sequence A composed of N positive integers: A_{1}, A_{2}, \cdots, A_{N}.

You will now successively do the following Q operations:

In the i-th operation, you replace every element whose value is B_{i} with C_{i}.

For each i (1 \leq i \leq Q), find S_{i}: the sum of all elements in A just after the i-th operation.

Constraints

All values in input are integers.

1 \leq N, Q, A_{i}, B_{i}, C_{i} \leq 10^{5}

B_{i} \neq C_{i}

Input

Input is given from Standard Input in the following format:

N
A_{1} A_{2} \cdots A_{N}
Q
B_{1} C_{1}
B_{2} C_{2}
\vdots
B_{Q} C_{Q}

Output

Print Q integers S_{i} to Standard Output in the following format:

S_{1}
S_{2}
\vdots
S_{Q}

Note that S_{i} may not fit into a 32-bit integer.

Sample Input 1

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

Sample Output 1

11
12
16

Initially, the sequence A is 1,2,3,4.

After each operation, it becomes the following:

2, 2, 3, 4

2, 2, 4, 4

4, 4, 4, 4

Sample Input 2

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

Sample Output 2

8
4
4

Note that the sequence A may not contain an element whose value is B_{i}.

Sample Input 3

2
1 2
3
1 100
2 100
100 1000

Sample Output 3

102
200
2000


program shift
  implicit none
  integer(8), parameter :: modu = 998244353
  character(300) :: s
  integer :: k, n, a(302) = 0, m = 1, x = 0, h = 0, r, d, w, c, b, i, j
  integer(8) :: dp(0:300, 0:301) = 0
  read(*,*) s, k
  n = len_trim(s)
  do i = 1, n
    if (s(i:i) == '0') then
      m = m + 1
      a(m) = x
    else
      x = x + 1
    end if
  end do
  m = m + 1
  a(m) = x
  k = min(k, x)
  dp(0, 0) = 1
  do i = 1, m - 1
    do j = h, 0, -1
      r = min(k - j, a(i + 1) - a(i))
      do d = 1, r
        w = j + d
        b = a(m) - a(i + 1) + d
        do c = 0, a(m) - a(i + 1)
          b = c + d
          dp(w, c) = dp(w, c) + dp(j, b)
          if (dp(w, c) >= modu) dp(w, c) = dp(w, c) - modu
        end do
      end do
      do c = 1, a(m) - a(i + 1)
        dp(j, c) = dp(j, c) + dp(j, c - 1)
        !if (dp(j, c) >= modu) dp(j, c) = dp(j, c) - modu
      end do
    end do
    h = min(k, h + a(i + 1) - a(i))
  end do
  write(*,'(i0)') mod(sum(dp(0:h, 0)), modu)
end program shift

Score : 800 points

Problem Statement

Given is a string S consisting of 0 and 1. Find the number of strings, modulo 998244353, that can result from applying the following operation on S between 0 and K times (inclusive):

Choose a pair of integers i, j (1\leq i < j\leq |S|) such that the i-th and j-th characters of S are 0 and 1, respectively. Remove the j-th character from S and insert it to the immediate left of the i-th character.

Constraints

1 \leq |S| \leq 300

0 \leq K \leq 10^9

S consists of 0 and 1.

Input

Input is given from Standard Input in the following format:

S K

Output

Find the number of strings, modulo 998244353, that can result from applying the operation on S between 0 and K times (inclusive).

Sample Input 1

0101 1

Sample Output 1

4

Four strings, 0101, 0110, 1001, and 1010, can result.

Sample Input 2

01100110 2

Sample Output 2

14

Sample Input 3

1101010010101101110111100011011111011000111101110101010010101010101 20

Sample Output 3

113434815


program main
   implicit none
   integer x(5),i
   read(*,*)x
   do i=1,5
      if(x(i)==0)then
         write(*,*)i
      	exit
      endif
   enddo
end program main

Score : 100 points

Problem Statement

We have five variables x_1, x_2, x_3, x_4, and x_5.

The variable x_i was initially assigned a value of i.

Snuke chose one of these variables and assigned it 0.

You are given the values of the five variables after this assignment.

Find out which variable Snuke assigned 0.

Constraints

The values of x_1, x_2, x_3, x_4, and x_5 given as input are a possible outcome of the assignment by Snuke.

Input

Input is given from Standard Input in the following format:

x_1 x_2 x_3 x_4 x_5

Output

If the variable Snuke assigned 0 was x_i, print the integer i.

Sample Input 1

0 2 3 4 5

Sample Output 1

1

In this case, Snuke assigned 0 to x_1, so we should print 1.

Sample Input 2

1 2 0 4 5

Sample Output 2

3


program prob7
    implicit none
    integer :: x(5)
    integer :: i
    read(*,*) x

    do i = 1, 5
        if(x(i) /= i) then
            write(*,*) i
        end if
    end do

    stop
contains
end program prob7


program main

read*,i1,i2,i3,i4,i5

if(i1==0)then
print*,'1'
end if
if(i2==0)then
print*,'2'
end if
if(i3==0)then
print*,'3'
end if
if(i4==0)then
print*,'4'
end if
if(i5==0)then
print*,'5'
end if

end program


program main
  implicit none
  integer i, A(5), ans
  !read(*, *)
  read(*, *) (A(i), i = 1,5)
  ans = 0
  do i = 1,5
    if (A(i) == 0) then
      ans = i
    endif
  end do
  write(*, *) ans
end program main


program main
integer::x,y
read*,x,y
if(mod(y,2)==0 .and. 2*x<=y .and. y<=4*x)then
 print*,"Yes"
else
 print*,"No"
end if
end program main

Score : 200 points

Problem Statement

There are some animals in a garden. Each of them is a crane with two legs or a turtle with four legs.

Takahashi says: "there are X animals in total in the garden, and they have Y legs in total." Determine whether there is a combination of numbers of cranes and turtles in which this statement is correct.

Constraints

1 \leq X \leq 100

1 \leq Y \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

If there is a combination of numbers of cranes and turtles in which the statement is correct, print Yes; otherwise, print No.

Sample Input 1

3 8

Sample Output 1

Yes

The statement "there are 3 animals in total in the garden, and they have 8 legs in total" is correct if there are two cranes and one turtle. Thus, there is a combination of numbers of cranes and turtles in which the statement is correct.

Sample Input 2

2 100

Sample Output 2

No

There is no combination of numbers of cranes and turtles in which this statement is correct.

Sample Input 3

1 2

Sample Output 3

Yes

We also consider the case in which there are only cranes or only turtles.


program main
  implicit none

  integer :: x, y

  read(*,*) x, y

  if (mod(4*x-y,2)/=0 .or. mod(y-2*x,2)/=0 .or. y-2*x < 0 .or. 4*x-y < 0 ) then
     write(*,*) 'No'
  else
     write(*,*) 'Yes'
  end if

end program main


program crane

 implicit none
 integer::x,y,n,jj,i,ans

 jj=0

 read*,x,y

 n=y/x+1

 do i=0,n
  ans=2*i+4*(x-i)
  if(x-i>=0) then
   if(ans==y) jj=jj+1
  end if
 end do

 if(jj==0) print*,'No'
 if(jj/=0) print*,'Yes'

end program crane


program main
    implicit none
    integer x, n
    integer, allocatable, dimension(:) :: p
    integer, dimension(102) :: q
    integer i, min, d, a

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

    do i = 1, 102
        q(i) = i-1
    end do

    do i = 1, n
        q(p(i)+1) = 1000
    end do

    a = 1
    min = 200
    do i = 1, 102
        d = abs(q(i)-x)
        if(d < min) then
            min = d
            a = q(i)
        else if (d == min) then
            if (q(i) < a) then
                a = q(i)
            end if
        end if
    end do

    write(*,*) a

end program

Score : 300 points

Problem Statement

Given are an integer X and an integer sequence of length N: p_1, \ldots, p_N.

Among the integers not contained in the sequence p_1, \ldots, p_N (not necessarily positive), find the integer nearest to X, that is, find the integer whose absolute difference with X is the minimum. If there are multiple such integers, report the smallest such integer.

Constraints

1 \leq X \leq 100

0 \leq N \leq 100

1 \leq p_i \leq 100

p_1, \ldots, p_N are all distinct.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X N
p_1 ... p_N

Output

Print the answer.

Sample Input 1

6 5
4 7 10 6 5

Sample Output 1

8

Among the integers not contained in the sequence 4, 7, 10, 6, 5, the one nearest to 6 is 8.

Sample Input 2

10 5
4 7 10 6 5

Sample Output 2

9

Among the integers not contained in the sequence 4, 7, 10, 6, 5, the ones nearest to 10 are 9 and 11. We should print the smaller one, 9.

Sample Input 3

100 0

Sample Output 3

100

When N = 0, the second line in the input will be empty. Also, as seen here, X itself can be the answer.


program main
  implicit none

  integer :: x, y, z, n, i, k
  integer,allocatable :: p(:)

  read(*,*) x, n

  if(n == 0) then
     write(*,*) x
     stop
  end if

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

  call heapsort(p)

  do i = 1, n
     if(p(i) == x) then
        k = i
        exit
     else
        k = -1
     end if
  end do

  if(k == -1) then
     write(*,*) x
     stop
  else
     do i = 1, k-1
        if(p(k-i) /= x-i) then
           y = x-i
           exit
        else
           y = x - k
        end if
     end do

     do i = 1, n-k
        if(p(k+i) /= x+i) then
           z = x+i
           exit
        else
           z = x + (n - k + 1)
        end if

     end do

  end if

  if(abs(y-x) <= abs(z-x))then
     write(*,*) y
  else
     write(*,*) z
  end if

  stop

  contains
  subroutine heapsort(array)
  implicit none
  integer, intent(inout) :: array(:)

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

  if(n <= 0)then
     write(6,*)"Error, at heapsort"; stop
  endif
  if(n == 1)return

  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).lt.array(j+1))j=j+1
        endif
        if (t < array(j))then
           array(i)=array(j)
           i=j
           j=j+j
        else
           j=k+1
        endif
     enddo
     array(i)=t
  enddo

  return
end subroutine heapsort

end program main


module mod_leftist_heap
  implicit none
  type t_key
    integer :: distance, count, x, y, direction
  end type
  type t_node
    type(t_node), private, pointer :: left => null(), right => null()
    integer :: rank = 1
    type(t_key), pointer :: key
  end type
  type leftist_heap
    integer :: size = 0
    type(t_node), private, pointer :: root => null()
  contains
    procedure :: offer => offer
    procedure :: poll => poll
    procedure :: meld => meld
  end type
contains
  logical function less(a, b) result(res)
    type(t_node), pointer, intent(in) :: a, b
    res = a%key%distance < b%key%distance
  end
  subroutine swap(a, b)
    type(t_node), pointer, intent(inout) :: a, b
    type(t_node), pointer :: c
    c => a
    a => b
    b => c
  end
  function newk0(distance, count, x, y, direction) result(res)
    integer, intent(in) :: distance, count, x, y, direction
    type(t_key), pointer :: res
    res => null()
    allocate(res)
    res%distance = distance
    res%count = count
    res%x = x
    res%y = y
    res%direction = direction
  end
  function newn0(key) result(res)
    type(t_key), pointer, intent(in) :: key
    type(t_node), pointer :: res
    res => null()
    allocate(res)
    res%key => key
  end
  recursive function meld_node(a, b) result(res)
    type(t_node), pointer, intent(in) :: a, b
    type(t_node), pointer :: res
    logical :: f
    if (.not.associated(a)) then
      res => b; return
    end if
    if (.not.associated(b)) then
      res => a; return
    end if
    if (less(a, b)) then
      res => a
      res%right => meld_node(res%right, b)
    else
      res => b
      res%right => meld_node(res%right, a)
    end if
    f = .not.associated(res%left)
    if (.not.f) f = res%left%rank < res%right%rank
    if (f) call swap(res%left, res%right)
    res%rank = merge(res%right%rank, 0, associated(res%right)) + 1
  end
  function poll(this) result(res)
    class(leftist_heap), intent(inout) :: this
    type(t_node), pointer :: left, right
    type(t_key), pointer :: res
    res => this%root%key
    left => this%root%left
    right => this%root%right
    deallocate(this%root)
    this%root => meld_node(left, right)
    this%size = this%size - 1
  end
  subroutine offer(this, distance, count, x, y, direction)
    class(leftist_heap), intent(inout) :: this
    integer, intent(in) :: distance, count, x, y, direction
    this%root => meld_node(this%root, newn0(newk0(distance, count, x, y, direction)))
    this%size = this%size + 1
  end
  type(leftist_heap) function meld(this, other) result(res)
    class(leftist_heap), intent(in) :: this
    type(leftist_heap), intent(in) :: other
    res%root => meld_node(this%root, other%root)
    res%size = this%size + other%size
  end
end module mod_leftist_heap
program pond_skater
  use mod_leftist_heap
  implicit none
  integer, parameter :: infty = lshift(1, 30)
  integer :: h, w, k, x(2), y(2), i, j
  integer :: dx(4) = (/0, 1, 0, -1/), dy(4) = (/1, 0, -1, 0/)
  integer :: dist, cnt, p, q, idx, u, v, cost, next
  integer, allocatable :: a(:, :, :)
  character(1000000) :: s
  type(leftist_heap) :: pq
  type(t_key), pointer :: key
  read(*,*) h, w, k
  read(*,*) x(1), y(1), x(2), y(2)
  allocate(a(0:h + 1, 0:w + 1, 4))
  a = -1
  do i = 1, h
    read(*,*) s
    do j = 1, w
      if (s(j:j) == '.') a(i, j, :) = infty
    end do
  end do
  a(x(1), y(1), :) = 0
  do i = 1, 4
    call pq%offer(0, k - 1, x(1), y(1), i)
  end do
  do while (pq%size > 0)
    key => pq%poll()
    dist = key%distance
    cnt = key%count
    p = key%x
    q = key%y
    idx = key%direction
    if (a(p, q, idx) < dist) cycle
    do i = 1, 4
      u = p + dx(i)
      v = q + dy(i)
      if (i == idx) then
        cost = 0
        next = cnt + 1
        if (next >= k) then
          cost = 1
          next = 0
        end if
        if (a(u, v, i) > dist + cost) then
          a(u, v, i) = dist + cost
          call pq%offer(a(u, v, i), next, u, v, i)
        end if
      else
        if (a(u, v, i) > dist + 1) then
          a(u, v, i) = dist + 1
          call pq%offer(a(u, v, i), 0, u, v, i)
        end if
      end if
    end do
  end do
  dist = minval(a(x(2), y(2), :))
  write(*,'(i0)') merge(-1, dist, dist == infty)
end program pond_skater

Score : 600 points

Problem Statement

Snuke, a water strider, lives in a rectangular pond that can be seen as a grid with H east-west rows and W north-south columns. Let (i,j) be the square at the i-th row from the north and j-th column from the west.

Some of the squares have a lotus leaf on it and cannot be entered.
The square (i,j) has a lotus leaf on it if c_{ij} is @, and it does not if c_{ij} is ..

In one stroke, Snuke can move between 1 and K squares (inclusive) toward one of the four directions: north, east, south, and west.
The move may not pass through a square with a lotus leaf. Moving to such a square or out of the pond is also forbidden.

Find the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2).
If the travel from (x_1,y_1) to (x_2,y_2) is impossible, point out that fact.

Constraints

1 \leq H,W,K \leq 10^6

H \times W \leq 10^6

1 \leq x_1,x_2 \leq H

1 \leq y_1,y_2 \leq W

x_1 \neq x_2 or y_1 \neq y_2.

c_{i,j} is . or @.

c_{x_1,y_1} = .

c_{x_2,y_2} = .

All numbers in input are integers.

Input

Input is given from Standard Input in the following format:

H W K
x_1 y_1 x_2 y_2
c_{1,1}c_{1,2} .. c_{1,W}
c_{2,1}c_{2,2} .. c_{2,W}
:
c_{H,1}c_{H,2} .. c_{H,W}

Output

Print the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2), or print -1 if the travel is impossible.

Sample Input 1

3 5 2
3 2 3 4
.....
.@..@
..@..

Sample Output 1

5

Initially, Snuke is at the square (3,2).
He can reach the square (3, 4) by making five strokes as follows:

From (3, 2), go west one square to (3, 1).

From (3, 1), go north two squares to (1, 1).

From (1, 1), go east two squares to (1, 3).

From (1, 3), go east one square to (1, 4).

From (1, 4), go south two squares to (3, 4).

Sample Input 2

1 6 4
1 1 1 6
......

Sample Output 2

2

Sample Input 3

3 3 1
2 1 2 3
.@.
.@.
.@.

Sample Output 3

-1


program sample
	character(len=20) :: x

    read(*,*) x

    write(*,'(a)') x(1:3)

    stop
end program sample

Score : 100 points

Problem Statement

When you asked some guy in your class his name, he called himself S, where S is a string of length between 3 and 20 (inclusive) consisting of lowercase English letters.
You have decided to choose some three consecutive characters from S and make it his nickname. Print a string that is a valid nickname for him.

Constraints

3 \leq |S| \leq 20

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

Print your answer.

Sample Input 1

takahashi

Sample Output 1

tak

Sample Input 2

naohiro

Sample Output 2

nao


program sample
    implicit none

    integer(8) :: i,j,m,n
    real(8)::a,b
    integer(8),allocatable :: x(:),y(:)
    character(20)::s
    read(*,*) s

    write(*,*) s(1:3)

    stop
end program sample


program main
    implicit none
    integer a, b, v, w, t
    integer flag

    read(*,*) a, v
    read(*,*) b, w
    read(*,*) t

    flag = 0

    if(a > b) then
        if(a-v*t <= b-w*t) then
            flag = 1
        end if
    else if (a < b) then
        if(a+v*t >= b+w*t) then
            flag = 1
        end if
    else if (a == b) then
        flag = 1
    end if

    if(flag == 1) then
        write(*,*) "YES"
    else
        write(*,*) "NO"
    end if

end program

Score : 200 points

Problem Statement

Two children are playing tag on a number line. (In the game of tag, the child called "it" tries to catch the other child.) The child who is "it" is now at coordinate A, and he can travel the distance of V per second.
The other child is now at coordinate B, and she can travel the distance of W per second.

He can catch her when his coordinate is the same as hers.
Determine whether he can catch her within T seconds (including exactly T seconds later).
We assume that both children move optimally.

Constraints

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

1 \leq V,W \leq 10^9

1 \leq T \leq 10^9

A \neq B

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A V
B W
T

Output

If "it" can catch the other child, print YES; otherwise, print NO.

Sample Input 1

1 2
3 1
3

Sample Output 1

YES

Sample Input 2

1 2
3 2
3

Sample Output 2

NO

Sample Input 3

1 2
3 3
3

Sample Output 3

NO


program main
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: n,k,i,j,l,r,jj
    integer(int64), allocatable:: a(:), na(:),ims(:)
    logical:: same

    read*, n,k
    allocate(a(n), na(0:n),ims(n))
    read*, a(:)
    ! a(:) = 0
    ! a(1) = 1

    if (k >= 50) then
        a(:) = n
        print'(*(i0,1x))', a(:)
        stop
    end if

    do i=1,k
        na(:) = 0
        ims(:) = 0
        same = .true.
        do j=1,n

            l = max(j-a(j),1_8)
            r = (j+a(j)+1)
            ! print'(*(i0,1x))', j,l,r
            ims(l)=ims(l)+1
            if (r <= n) ims(r)=ims(r)-1
        end do
        ! print'(*(i0,1x))', ims(:)
        do j=1,n
            na(j) = na(j-1)+ims(j)
        end do

        do j=1,n
            if (na(j) /= a(j)) same = .false.
        end do
        if (same) exit
        a(:) = na(1:)
    end do
    ! print'(i0)', i
    print'(*(i0,1x))', na(1:)
end program main

Score : 500 points

Problem Statement

We have N bulbs arranged on a number line, numbered 1 to N from left to right.
Bulb i is at coordinate i.

Each bulb has a non-negative integer parameter called intensity.
When there is a bulb of intensity d at coordinate x, the bulb illuminates the segment from coordinate x-d-0.5 to x+d+0.5.
Initially, the intensity of Bulb i is A_i. We will now do the following operation K times in a row:

For each integer i between 1 and N (inclusive), let B_i be the number of bulbs illuminating coordinate i. Then, change the intensity of each bulb i to B_i.

Find the intensity of each bulb after the K operations.

Constraints

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

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

0 \leq A_i \leq N

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \ldots A_N

Output

Print the intensity A{'}_i of each bulb i after the K operations to Standard Output in the following format:

A{'}_1 A{'}_2 \ldots A{'}_N

Sample Input 1

5 1
1 0 0 1 0

Sample Output 1

1 2 2 1 2

Initially, only Bulb 1 illuminates coordinate 1, so the intensity of Bulb 1 becomes 1 after the operation.
Similarly, the bulbs initially illuminating coordinate 2 are Bulb 1 and 2, so the intensity of Bulb 2 becomes 2.

Sample Input 2

5 2
1 0 0 1 0

Sample Output 2

3 3 4 4 3


program light
  implicit none
  integer :: n, k, i, j
  integer,allocatable :: a(:), a2(:)

  read(*,*) n, k

  if ( k>=n ) then
     read(*,*)
     do i = 1, n
        write(*,'(200000(i0,x))',advance='no') n
     end do
     stop
  end if

  allocate(a(n),a2(n))
  read(*,*) a
  a2 = 0
  do i = 1, k
     do j = 1, n
        a2(max(1,j-a(j)):min(j+a(j),n))= a2(max(1,j-a(j)):min(j+a(j),n)) + 1
     end do
     a = a2
     a2 = 0
     if (minval(a)==n) then
        exit
     end if
  end do
  write(*,'(200000(i0,x))') a
  deallocate(a,a2)
  stop
end program light


program knapsack_queries_on_a_tree
  implicit none
  integer :: n, q, u(100000,3) = 0, p(0:18) = -1, m, r, h, i, j, k
  integer, dimension(262144) :: v = 0, w = 0
  integer(8) :: dp(0:18,100000) = 0, ans(100000) = 0
  read(*,*) n
  do i = 1, n
    read(*,*) v(i), w(i)
  end do
  read(*,*) q
  do i = 1, q
    read(*,*) u(i,1:2)
    u(i,3) = i
  end do
  call merge_sort(u(1:q,1:3),1)
  p(0) = 1
  dp(0,w(1):100000) = v(1)
  do i = 1, q
    m = u(i,1)
    h = headbit(m)
    do j = 1, h
      r = rshift(m,h-j)
      if (p(j) == r) cycle
      p(j) = r
      dp(j,:) = 0
      do k = 100000, w(r), -1
        dp(j,k) = max(dp(j-1,k),dp(j-1,k-w(r))+v(r))
      end do
    end do
    ans(u(i,3)) = dp(h,u(i,2))
  end do
  do i = 1, q
    write(*,'(i0)') ans(i)
  end do
contains
  integer function headbit(n)
    integer, intent(in) :: n
    integer :: i
    do i = 18, 0, -1
      if (btest(n,i)) then
        headbit = i
        return
      end if
    end do
  end
  subroutine merge_sort(a,k)
    integer, intent(inout) :: a(:,:)
    integer, intent(in) :: k
    integer :: n, m, l, i, u
    n = size(a,1)
    m = n
    l = 1
    do while (m > 1)
      do i = 1, m/2
        u = min(2*i*l,n)
        call merger(a(2*(i-1)*l+1:(2*i-1)*l,:),a((2*i-1)*l+1:u,:),k)
      end do
      l = 2*l
      m = (m+1)/2
    end do
  end
  subroutine merger(a1,a2,k)
    integer, intent(inout) :: a1(:,:), a2(:,:)
    integer, intent(in) :: k
    integer :: a(size(a1,1)+size(a2,1),size(a1,2))
    integer :: i1, i2, n1, n2
    i1 = 1
    i2 = 1
    n1 = size(a1,1)
    n2 = size(a2,1)
    do while (i1 <= n1 .and. i2 <= n2)
      if (a1(i1,k) <= a2(i2,k)) then
        a(i1+i2-1,:) = a1(i1,:)
        i1 = i1+1
      else
        a(i1+i2-1,:) = a2(i2,:)
        i2 = i2+1
       end if
    end do
    if (i1 <= n1) then
      a(i1+i2-1:n1+n2,:) = a1(i1:n1,:)
    else
      a(i1+i2-1:n1+n2,:) = a2(i2:n2,:)
    end if
    a1 = a(1:n1,:)
    a2 = a(n1+1:n1+n2,:)
  end
end program knapsack_queries_on_a_tree

Score : 700 points

Problem Statement

We have a rooted binary tree with N vertices, where the vertices are numbered 1 to N.
Vertex 1 is the root, and the parent of Vertex i (i \geq 2) is Vertex \left[ \frac{i}{2} \right].

Each vertex has one item in it. The item in Vertex i has a value of V_i and a weight of W_i.
Now, process the following query Q times:

Given are a vertex v of the tree and a positive integer L.
Let us choose some (possibly none) of the items in v and the ancestors of v so that their total weight is at most L.
Find the maximum possible total value of the chosen items.

Here, Vertex u is said to be an ancestor of Vertex v when u is an indirect parent of v, that is, there exists a sequence of vertices w_1,w_2,\ldots,w_k (k\geq 2) where w_1=v, w_k=u, and w_{i+1} is the parent of w_i for each i.

Constraints

All values in input are integers.

1 \leq N < 2^{18}

1 \leq Q \leq 10^5

1 \leq V_i \leq 10^5

1 \leq W_i \leq 10^5

For the values v and L given in each query, 1 \leq v \leq N and 1 \leq L \leq 10^5.

Input

Let v_i and L_i be the values v and L given in the i-th query.
Then, Input is given from Standard Input in the following format:

N
V_1 W_1
:
V_N W_N
Q
v_1 L_1
:
v_Q L_Q

Output

For each integer i from 1 through Q,
the i-th line should contain the response to the i-th query.

Sample Input 1

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

Sample Output 1

0
3
3

In the first query, we are given only one choice: the item with (V, W)=(1,2). Since L = 1, we cannot actually choose it, so our response should be 0.

In the second query, we are given two choices: the items with (V, W)=(1,2) and (V, W)=(2,3). Since L = 5, we can choose both of them, so our response should be 3.

Sample Input 2

15
123 119
129 120
132 112
126 109
118 103
115 109
102 100
130 120
105 105
132 115
104 102
107 107
127 116
121 104
121 115
8
8 234
9 244
10 226
11 227
12 240
13 237
14 206
15 227

Sample Output 2

256
255
250
247
255
259
223
253


program main
  implicit none
  integer :: a, b
  read *, a, b
  print "(i0)", a * b
end program main

Score : 100 points

Problem Statement

Compute A \times B.

Constraints

1 \leq A \leq 100

1 \leq B \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the value A \times B as an integer.

Sample Input 1

2 5

Sample Output 1

10

We have 2 \times 5 = 10.

Sample Input 2

100 100

Sample Output 2

10000


program test
implicit none
integer::a,b
read*,a,b
print*,a*b
end program test


program ABC169_A
  integer::A,B
  read(*,*)A,B
  write(*,*)A*B
end program ABC169_A


program b169a

implicit none
integer :: a, b

read *, a, b

print *, a * b

end program b169a


program main
  implicit none
  integer(8) i, N, lim, m(100), t, ans, exp, level, N1
  !integer(8),allocatable:: A(:,:)
  read(*, *) N
  N1 = N
  lim = 1000000
  !allocate(A(N,2)
  m = 0
  t = 0
  do i = 1,50
    m(i) = t+i
    t = m(i)
  end do
  !read(*, *) (L(i), i = 1,N)
  t = 2
  exp = 0
  level = 0
  ans = 0
  if (N == 1) then
    ans = 0
  else
    do
      if (mod(N,t) == 0) then
        exp = exp + 1
        N = N/t
        if (exp == m(level+1)) then
          level = level + 1
        end if
      else
        ans = ans + level
        exp = 0
        level = 0
        t = t+1
      endif
      if (t > lim) then
        if (N1 == N) then
          ans = ans + 1
        endif
        exit
      endif
    end do
  endif
  write(*, *) ans
end program main

Score : 400 points

Problem Statement

Given is a positive integer N. Consider repeatedly applying the operation below on N:

First, choose a positive integer z satisfying all of the conditions below:

z can be represented as z=p^e, where p is a prime number and e is a positive integer;

z divides N;

z is different from all integers chosen in previous operations.

Then, replace N with N/z.

Find the maximum number of times the operation can be applied.

Constraints

All values in input are integers.

1 \leq N \leq 10^{12}

Input

Input is given from Standard Input in the following format:

N

Output

Print the maximum number of times the operation can be applied.

Sample Input 1

24

Sample Output 1

3

We can apply the operation three times by, for example, making the following choices:

Choose z=2 (=2^1). (Now we have N=12.)

Choose z=3 (=3^1). (Now we have N=4.)

Choose z=4 (=2^2). (Now we have N=1.)

Sample Input 2

1

Sample Output 2

0

We cannot apply the operation at all.

Sample Input 3

64

Sample Output 3

3

We can apply the operation three times by, for example, making the following choices:

Choose z=2 (=2^1). (Now we have N=32.)

Choose z=4 (=2^2). (Now we have N=8.)

Choose z=8 (=2^3). (Now we have N=1.)

Sample Input 4

1000000007

Sample Output 4

1

We can apply the operation once by, for example, making the following choice:

z=1000000007 (=1000000007^1). (Now we have N=1.)

Sample Input 5

997764507000

Sample Output 5

7


program main
      implicit none
      integer :: h1, m1, h2, m2, k
      integer :: str, fin, ans

      read *, h1, m1, h2, m2, k

      str = h1*60+m1
      fin = h2*60+m2

      ans = fin-str-k
      print *, ans

end program main

Score : 100 points

Problem Statement

In this problem, we use the 24-hour clock.

Takahashi gets up exactly at the time H_1 : M_1 and goes to bed exactly at the time H_2 : M_2. (See Sample Inputs below for clarity.)
He has decided to study for K consecutive minutes while he is up.
What is the length of the period in which he can start studying?

Constraints

0 \le H_1, H_2 \le 23

0 \le M_1, M_2 \le 59

The time H_1 : M_1 comes before the time H_2 : M_2.

K \ge 1

Takahashi is up for at least K minutes.

All values in input are integers (without leading zeros).

Input

Input is given from Standard Input in the following format:

H_1 M_1 H_2 M_2 K

Output

Print the length of the period in which he can start studying, as an integer.

Sample Input 1

10 0 15 0 30

Sample Output 1

270

Takahashi gets up at exactly ten in the morning and goes to bed at exactly three in the afternoon.
It takes 30 minutes to do the study, so he can start it in the period between ten o'clock and half-past two. The length of this period is 270 minutes, so we should print 270.

Sample Input 2

10 0 12 0 120

Sample Output 2

0

Takahashi gets up at exactly ten in the morning and goes to bed at exactly noon. It takes 120 minutes to do the study, so he has to start it at exactly ten o'clock. Thus, we should print 0.


program a
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: h1, m1, h2, m2, k
    integer(int32):: t1, t2

    read*, h1, m1, h2, m2, k

    t1 = 60*h1+h2
    t2 = 60*h2+h2

    print'(i0)', t2-t1-k
end program a


program b0530

implicit none
character(len=200000) :: t
integer :: i

read *, t

do i = 1, 200000
 if (t(i:i) == '?') then
  t(i:i) = 'D'
 end if
end do

print *, trim(t)

end program b0530

Score : 200 points

Problem Statement

For a string S consisting of the uppercase English letters P and D, let the doctoral and postdoctoral quotient of S be the total number of occurrences of D and PD in S as contiguous substrings. For example, if S = PPDDP, it contains two occurrences of D and one occurrence of PD as contiguous substrings, so the doctoral and postdoctoral quotient of S is 3.

We have a string T consisting of P, D, and ?.

Among the strings that can be obtained by replacing each ? in T with P or D, find one with the maximum possible doctoral and postdoctoral quotient.

Constraints

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

T consists of P, D, and ?.

Input

Input is given from Standard Input in the following format:

T

Output

Print one string with the maximum possible doctoral and postdoctoral quotient among the strings that can be obtained by replacing each ? in T with P or D.
If there are multiple such strings, you may print any of them.

Sample Input 1

PD?D??P

Sample Output 1

PDPDPDP

This string contains three occurrences of D and three occurrences of PD as contiguous substrings, so its doctoral and postdoctoral quotient is 6, which is the maximum doctoral and postdoctoral quotient of a string obtained by replacing each ? in T with P or D.

Sample Input 2

P?P?

Sample Output 2

PDPD


program NOMURA2020B
  character(200000)::T
  integer(8)::ans=0
  read(*,*)T
  do i=1,len_trim(T)
     if(T(i:i)=='?') T(i:i)='D'
  end do
  write(*,*)T
end program NOMURA2020B


program NOMURA2020B
  character(200000)::T
  read(*,*)T
  do i=1,len_trim(T)
     if(T(i:i)=='?') T(i:i)='D'
  end do
  write(*,*)trim(T)
end program NOMURA2020B


program folia
  implicit none
  integer :: n, i
  integer(8) :: a(0:100001) = 0, p(0:60) = 0, x = 0, m = 0
  read(*,*) n
  read(*,*) a(0:n)
  p(0) = 1
  do i = 1, 60
    p(i) = (p(i - 1) - a(i - 1)) * 2
    if (a(i) > p(i)) then
      write(*,'(i0)') -1
      stop
    end if
  end do
  do i = n, 61, -1
    m = m + a(i)
    x = x + m
  end do
  do i = min(n, 60), 0, -1
    m = min(m + a(i), p(i))
    x = x + m
  end do
  write(*,'(i0)') x
end program folia

Score : 600 points

Problem Statement

Given is an integer sequence of length N+1: A_0, A_1, A_2, \ldots, A_N. Is there a binary tree of depth N such that, for each d = 0, 1, \ldots, N, there are exactly A_d leaves at depth d? If such a tree exists, print the maximum possible number of vertices in such a tree; otherwise, print -1.

Notes

A binary tree is a rooted tree such that each vertex has at most two direct children.

A leaf in a binary tree is a vertex with zero children.

The depth of a vertex v in a binary tree is the distance from the tree's root to v. (The root has the depth of 0.)

The depth of a binary tree is the maximum depth of a vertex in the tree.

Constraints

0 \leq N \leq 10^5

0 \leq A_i \leq 10^{8} (0 \leq i \leq N)

A_N \geq 1

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_0 A_1 A_2 \cdots A_N

Output

Print the answer as an integer.

Sample Input 1

3
0 1 1 2

Sample Output 1

7

Below is the tree with the maximum possible number of vertices. It has seven vertices, so we should print 7.

Sample Input 2

4
0 0 1 0 2

Sample Output 2

10

Sample Input 3

2
0 3 1

Sample Output 3

-1

Sample Input 4

1
1 1

Sample Output 4

-1

Sample Input 5

10
0 0 1 1 2 3 5 8 13 21 34

Sample Output 5

264


program folia
  implicit none
  integer :: n, i
  integer(8) :: a(0:100001) = 0, p(0:60) = 0, r, m = 0
  read(*,*) n
  read(*,*) a(0:n)
  p = (/(ibset(0_8, i), i = 0, 60)/)
  do i = min(60, n), 0, -1
    r = (a(i + 1) + 1) / 2 + a(i)
    if (r > p(i)) then
      write(*,'(i0)') -1
      stop
    end if
  end do
  r = 0
  do i = n, 61, -1
    m = m + a(i)
    r = r + m
  end do
  do i = min(n, 60), 1, -1
    m = min(m + a(i), (p(i - 1) - a(i - 1)) * 2)
    r = r + m
  end do
  write(*,'(i0)') r + 1
end program folia


      PROGRAM piyo
      IMPLICIT NONE
      integer :: n

      read*,n

      n = n - n/10*10

      select case( n )
        case( 2,4,5,7,9 )
          print*,'hon'
        case( 0,1,6,8 )
          print*,'pon'
        case( 3 )
          print*,'bon'

      end select

      END PROGRAM

Score: 100 points

Problem Statement

The cat Snuke wants to play a popular Japanese game called ÅtCoder, so Iroha has decided to teach him Japanese.

When counting pencils in Japanese, the counter word "本" follows the number. The pronunciation of this word varies depending on the number. Specifically, the pronunciation of "本" in the phrase "N 本" for a positive integer N not exceeding 999 is as follows:

hon when the digit in the one's place of N is 2, 4, 5, 7, or 9;

pon when the digit in the one's place of N is 0, 1, 6 or 8;

bon when the digit in the one's place of N is 3.

Given N, print the pronunciation of "本" in the phrase "N 本".

Constraints

N is a positive integer not exceeding 999.

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

16

Sample Output 1

pon

The digit in the one's place of 16 is 6, so the "本" in "16 本" is pronounced pon.

Sample Input 2

2

Sample Output 2

hon

Sample Input 3

183

Sample Output 3

bon