module mod_fenwick_tree
  implicit none
  type t_fenwick_tree
    private
    integer :: n, p
    integer, pointer :: arr(:) => null()
  contains
    procedure :: init => init_bit
    procedure :: release => release_bit
    procedure :: add => get_addition
    procedure :: sum => get_summation
    procedure :: range => get_partial_summation
    procedure :: val => get_value
    procedure :: lower_bound => get_lower_bound
    procedure :: update => get_updated
    procedure :: max => get_maximum
  end type t_fenwick_tree
contains
  subroutine init_bit(this,n)
    class(t_fenwick_tree), intent(inout) :: this
    integer, intent(in) :: n
    integer :: p
    p = 1
    this%n = n
    allocate(this%arr(n))
    this%arr = 0
    do while (lshift(p,1) <= n)
      p = lshift(p,1)
    end do
    this%p = p
  end
  subroutine release_bit(this)
    class(t_fenwick_tree), intent(inout) :: this
    if (associated(this%arr)) deallocate(this%arr)
  end
  subroutine get_addition(this,i,v)
    class(t_fenwick_tree), intent(inout) :: this
    integer, intent(in) :: i
    integer, intent(in) :: v
    integer :: x
    x = i
    do while (x <= this%n)
      this%arr(x) = this%arr(x)+v
      x = x+and(x,-x)
    end do
  end
  function get_summation(this,i) result(s)
    class(t_fenwick_tree), intent(in) :: this
    integer, intent(in) :: i
    integer :: x
    integer :: s
    s = -1
    if (i > this%n) return
    s = 0
    x = i
    do while (x > 0)
      s = s+this%arr(x)
      x = x-and(x,-x)
    end do
  end
  function get_partial_summation(this,l,r) result(s)
    class(t_fenwick_tree), intent(in) :: this
    integer, intent(in) :: l, r
    integer :: i, j
    integer :: s
    s = -1
    if (l > r) return
    s = 0
    i = l-1
    j = r
    do while (j > i)
      s = s+this%arr(j)
      j = j-and(j,-j)
    end do
    do while (i > j)
      s = s-this%arr(i)
      i = i-and(i,-i)
    end do
  end
  function get_value(this,i) result(v)
    class(t_fenwick_tree), intent(in) :: this
    integer, intent(in) :: i
    integer :: x
    integer :: v
    v = get_partial_summation(this,i,i)
  end
  function get_lower_bound(this,v) result(x)
    class(t_fenwick_tree), intent(in) :: this
    integer, intent(in) :: v
    integer :: x, k
    integer :: w
    x = 0
    if (v <= 0) return
    k = this%p
    w = v
    do while (k > 0)
      if (x+k <= this%n .and. this%arr(x+k) < w) then
        w = w-this%arr(x+k)
        x = x+k
      end if
      k = rshift(k,1)
    end do
    x = x+1
  end
  subroutine get_updated(this,i,v)
    class(t_fenwick_tree), intent(inout) :: this
    integer, intent(in) :: i
    integer, intent(in) :: v
    integer :: x
    x = i
    do while (x <= this%n)
      if (this%arr(x) < v) this%arr(x) = v
      x = x+and(x,-x)
    end do
  end
  function get_maximum(this,i) result(m)
    class(t_fenwick_tree), intent(in) :: this
    integer, intent(in) :: i
    integer :: x, m
    x = i
    m = 0
    do while (x > 0)
      m = max(m,this%arr(x))
      x = x-and(x,-x)
    end do
  end
end module mod_fenwick_tree
program fountain_walk
  implicit none
  real(8), parameter :: pi = 3.14159265358979d0
  real(8), parameter :: k1 = 100.d0, k2 = 20.d0-5.d0*pi, k3 = 10.d0*pi-20.d0
  integer :: n, i
  integer(8) :: x1, y1, x2, y2, x(200000) = 0_8, y(200000) = 0_8
  integer(8) :: s, p, q, k
  character(64) :: ans
  read(*,*) x1, y1, x2, y2
  read(*,*) n
  do i = 1, n
    read(*,*) x(i), y(i)
  end do
  if (x1 > x2) then
    call swap(x1,x2)
    call swap(y1,y2)
  end if
  if (y1 > y2) then
    y1 = -y1
    y2 = -y2
    y(1:n) = -y(1:n)
  end if
  s = x2-x1+y2-y1
  k = int(max(lis(x(1:n),y(1:n),x1,y1,x2,y2),lis(y(1:n),x(1:n),y1,x1,y2,x2)),8)
  if (k == min(x2-x1,y2-y1)+1_8) then
    p = k-1_8
    q = 1_8
  else
    p = k
    q = 0_8
  end if
  write(ans,'(f32.16)') k1*s-k2*p+k3*q
  write(*,'(a)') trim(adjustl(ans))
contains
  subroutine swap(a,b)
    integer(8), intent(inout) :: a, b
    a = xor(a,b)
    b = xor(a,b)
    a = xor(a,b)
  end
  integer function lis(x,y,x1,y1,x2,y2)
    use mod_fenwick_tree
    integer(8), intent(in) :: x(:), y(:), x1, y1, x2, y2
    integer(8) :: c(size(x),2)
    integer :: n, z(size(x)), t, i, j, k
    type(t_fenwick_tree) :: bit
    lis = 0
    n = size(x)
    c(:,1) = x
    c(:,2) = y
    call merge_sort(c)
    j = 1
    do while (j <= n .and. c(j,1) < x1)
      j = j+1
    end do
    if (j > n) return
    k = n
    do while (k >= 1 .and. c(k,1) > x2)
      k = k-1
    end do
    if (k < 1) return
    z = compress(c(:,2))
    call bit%init(n)
    do i = j, k
      if (c(i,2) <= y1 .or. y2 <= c(i,2)) cycle
      t = bit%max(z(i)-1)+1
      lis = max(lis,t)
      call bit%update(z(i),t)
    end do
  end
  function compress(x) result(ret)
    implicit none
    integer(8), intent(in) :: x(:)
    integer(8) :: y(size(x),2)
    integer :: n, i, k, ret(size(x))
    n = size(x)
    y(:,1) = x
    do i = 1, n
      y(i,2) = int(i,8)
    end do
    call merge_sort(y)
    k = 0
    ret(y(1,2)) = 1
    do i = 2, n
      if (y(i,1) == y(i-1,1)) k = k+1
      ret(y(i,2)) = i-k
    end do
  end
  subroutine merge_sort(c)
    integer(8), intent(inout) :: c(:,:)
    integer :: n, m, l, u, i
    n = size(c(:,1))
    m = n
    l = 1
    do while (m > 1)
      do i = 1, m/2
        u = min(2*i*l,n)
        call merger(c(2*(i-1)*l+1:(2*i-1)*l,:),c((2*i-1)*l+1:u,:))
      end do
      l = 2*l
      m = (m+1)/2
    end do
  end
  subroutine merger(c1,c2)
    integer(8), intent(inout) :: c1(:,:), c2(:,:)
    integer(8) :: c(size(c1(:,1))+size(c2(:,1)),size(c2(1,:)))
    integer :: i1, i2, n1, n2
    i1 = 1
    i2 = 1
    n1 = size(c1(:,1))
    n2 = size(c2(:,1))
    do while (i1 <= n1 .and. i2 <= n2)
      if (c1(i1,1) < c2(i2,1) .or. &
      & c1(i1,1) == c2(i2,1) .and. c1(i1,2) > c2(i2,2)) then
        c(i1+i2-1,:) = c1(i1,:)
        i1 = i1+1
      else
        c(i1+i2-1,:) = c2(i2,:)
        i2 = i2+1
      end if
    end do
    if (i1 <= n1) then
      c(i1+i2-1:n1+n2,:) = c1(i1:n1,:)
    else if (i2 <= n2) then
      c(i1+i2-1:n1+n2,:) = c2(i2:n2,:)
    end if
    c1 = c(1:n1,:)
    c2 = c(n1+1:n1+n2,:)
  end
end program fountain_walk

Score : 900 points

Problem Statement

In the city of Nevermore, there are 10^8 streets and 10^8 avenues, both numbered from 0 to 10^8-1.
All streets run straight from west to east, and all avenues run straight from south to north.
The distance between neighboring streets and between neighboring avenues is exactly 100 meters.

Every street intersects every avenue. Every intersection can be described by pair (x, y), where x is avenue ID and y is street ID.

There are N fountains in the city, situated at intersections (X_i, Y_i).
Unlike normal intersections, there's a circle with radius 10 meters centered at the intersection, and there are no road parts inside this circle.

The picture below shows an example of how a part of the city with roads and fountains may look like.

City governors don't like encountering more than one fountain while moving along the same road.
Therefore, every street contains at most one fountain on it, as well as every avenue.

Citizens can move along streets, avenues and fountain perimeters.
What is the shortest distance one needs to cover in order to get from intersection (x_1, y_1) to intersection (x_2, y_2)?

Constraints

0 \leq x_1, y_1, x_2, y_2 < 10^8

1 \leq N \leq 200,000

0 \leq X_i, Y_i < 10^8

X_i \neq X_j for i \neq j

Y_i \neq Y_j for i \neq j

Intersections (x_1, y_1) and (x_2, y_2) are different and don't contain fountains.

All input values are integers.

Input

Input is given from Standard Input in the following format:

x_1 y_1 x_2 y_2
N
X_1 Y_1
X_2 Y_2
:
X_N Y_N

Output

Print the shortest possible distance one needs to cover in order to get from intersection (x_1, y_1) to intersection (x_2, y_2), in meters.
Your answer will be considered correct if its absolute or relative error doesn't exceed 10^{-11}.

Sample Input 1

1 1 6 5
3
3 2
5 3
2 4

Sample Output 1

891.415926535897938

One possible shortest path is shown on the picture below. The path starts at the blue point, finishes at the purple point and follows along the red line.

Sample Input 2

3 5 6 4
3
3 2
5 3
2 4

Sample Output 2

400.000000000000000

Sample Input 3

4 2 2 2
3
3 2
5 3
2 4

Sample Output 3

211.415926535897938


module mod_modint
  implicit none
  integer(8) :: modulus = 998244353
  type modint
    private
    integer(8) :: num
  contains
    procedure :: get => getnum
  end type

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

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

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

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

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

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

  interface inv
    module procedure :: invm
  end interface inv

contains

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

end module mod_modint
program A
  use mod_modint
  implicit none
  integer::N,M,i
  type(modint),allocatable,dimension(:)::Fact,i_Fact
  type(modint)::ans
  read*,N,M
  if(N<M)then
    N=xor(N,M)
    M=xor(N,M)
    N=xor(N,M)
  endif

  allocate(Fact(0:2*N),i_Fact(0:2*N))
  Fact(0)=1
  do i=1,2*N
    Fact(i)=Fact(i-1)*i
  end do
  i_Fact(2*N)=1/Fact(2*N)
  do i=2*N-1,0,-1
    i_Fact(i)=i_Fact(i+1)*(i+1)
  end do

  ans=N
  do i=1,M
    ans=ans+i_Fact(2)*Fact(N)*i_Fact(i)*Fact(M)*i_Fact(i)*i_Fact(N+M)*Fact(2*i)*Comb(N-i+M-i,M-i)
  end do

  print"(i0)",getnum(ans)

contains

type(modint) function Comb(N,K)
  integer::N,K
  Comb=Fact(N)*i_Fact(K)*i_Fact(N-K)
end function

end program A

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

Score : 2000 points

Problem Statement

You are participating in a quiz with N + M questions and Yes/No answers.

It's known in advance that there are N questions with answer Yes and M questions with answer No, but the questions are given to you in random order.

You have no idea about correct answers to any of the questions.
You answer questions one by one, and for each question you answer, you get to know the correct answer immediately after answering.

Suppose you follow a strategy maximizing the expected number of correct answers you give.

Let this expected number be P/Q, an irreducible fraction. Let M = 998244353.
It can be proven that a unique integer R between 0 and M - 1 exists such that P = Q \times R modulo M,
and it is equal to P \times Q^{-1} modulo M, where Q^{-1} is the modular inverse of Q.
Find R.

Constraints

1 \leq N, M \leq 500,000

Both N and M are integers.

Partial Score

1500 points will be awarded for passing the testset satisfying N = M and 1 \leq N, M \leq 10^5.

Input

Input is given from Standard Input in the following format:

N M

Output

Let P/Q be the expected number of correct answers you give if you follow an optimal strategy, represented as an irreducible fraction.
Print P \times Q^{-1} modulo 998244353.

Sample Input 1

1 1

Sample Output 1

499122178

There are two questions.
You may answer randomly to the first question, and you'll succeed with 50% probability.
Then, since you know the second answer is different from the first one, you'll succeed with 100% probability.

The expected number of your correct answers is 3 / 2.
Thus, P = 3, Q = 2, Q^{-1} = 499122177 (modulo 998244353), and P \times Q^{-1} = 499122178 (again, modulo 998244353).

Sample Input 2

2 2

Sample Output 2

831870297

The expected number of your correct answers is 17 / 6.

Sample Input 3

3 4

Sample Output 3

770074220

The expected number of your correct answers is 169 / 35.

Sample Input 4

10 10

Sample Output 4

208827570

Sample Input 5

42 23

Sample Output 5

362936761

Submitted source-code file size, in bytes.

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

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


program main
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: x,a,b

    read*, x,a,b

    if (abs(x-a) < abs(x-b)) then
        print'(a)', 'A'
    else
        print'(a)', 'B'
    end if

end program main

Score : 100 points

Problem Statement

Snuke lives at position x on a number line.
On this line, there are two stores A and B, respectively at position a and b, that offer food for delivery.

Snuke decided to get food delivery from the closer of stores A and B.
Find out which store is closer to Snuke's residence.

Here, the distance between two points s and t on a number line is represented by |s-t|.

Constraints

1 \leq x \leq 1000

1 \leq a \leq 1000

1 \leq b \leq 1000

x, a and b are pairwise distinct.

The distances between Snuke's residence and stores A and B are different.

Input

Input is given from Standard Input in the following format:

x a b

Output

If store A is closer, print A; if store B is closer, print B.

Sample Input 1

5 2 7

Sample Output 1

B

The distances between Snuke's residence and stores A and B are 3 and 2, respectively.
Since store B is closer, print B.

Sample Input 2

1 999 1000

Sample Output 2

A


program main
    integer n
    integer :: a(10**5)
    integer, allocatable :: str_a(:)
    integer, allocatable :: ans(:)
    integer :: first=0, second=0
    integer(8) :: answer
    integer restart

    read *, n
    read *, (a(i), i=1,n)
    str_a = a(1:n)
    ans = quick(str_a)

    do i=1,n-1
        if(ans(i)==ans(i+1)) then
            first = ans(i)
            restart = i+2
            exit
        end if
    end do

    if (result < n) then
        do i=restart,n-1
            if(ans(i)==ans(i+1)) then
                second = ans(i)
                exit
            end if
        end do
    end if

    answer = first * second
    print *, answer

contains
    recursive function quick(a) result(r)
        integer, intent(in) :: a(:)
        integer, allocatable :: r(:)
        integer :: p
        if(size(a) < 2) then
            r = a
        else
            p = a(1)
            r = [quick(pack(a(2:), a(2:) > p)), pack(a, a == p), quick(pack(a(2:), a(2:) < p))]
        end if
    end function quick
end program main

Score : 300 points

Problem Statement

We have N sticks with negligible thickness.
The length of the i-th stick is A_i.

Snuke wants to select four different sticks from these sticks and form a rectangle (including a square), using the sticks as its sides.
Find the maximum possible area of the rectangle.

Constraints

4 \leq N \leq 10^5

1 \leq A_i \leq 10^9

A_i is an integer.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible area of the rectangle.
If no rectangle can be formed, print 0.

Sample Input 1

6
3 1 2 4 2 1

Sample Output 1

2

1 \times 2 rectangle can be formed.

Sample Input 2

4
1 2 3 4

Sample Output 2

0

No rectangle can be formed.

Sample Input 3

10
3 3 3 3 4 4 4 5 5 5

Sample Output 3

20


program main
    integer(8) :: answer = 1
    integer(8) :: m = 1000000007
    integer :: num
    character (52) :: s1, s2
    character :: s(52) = '_'
    integer :: a = 0, start = 1
    logical :: flag = .true.

    read *, num
    read *, s1
    read *, s2
    s1 = trim(s1)
    s2 = trim(s2)

    if(s1(1:1) /= s2(1:1)) then
        answer = 6
        start = start + 2
        flag = .true.
    else
        answer = 3
        start = start + 1
        flag = .false.
    end if

    do i=start,num-1
        if (i+a >= num) then
            exit
        end if

        if (flag) then
            if (s1(i + a:i + a) /= s2(i+a:i+a)) then
                a = a + 1
                answer = mod(answer*3, m)
                flag = .true.
            else
                flag = .false.
            end if
        else
            if (s1(i + a:i + a) /= s2(i+a:i+a)) then
                a = a + 1
                answer = mod(answer*2, m)
                flag = .true.
            else
                answer = mod(answer*2, m)
                flag = .false.
            end if
        end if
    end do

    print *, answer
end program main

Score : 400 points

Problem Statement

We have a board with a 2 \times N grid.
Snuke covered the board with N dominoes without overlaps.
Here, a domino can cover a 1 \times 2 or 2 \times 1 square.

Then, Snuke decided to paint these dominoes using three colors: red, cyan and green.
Two dominoes that are adjacent by side should be painted by different colors.
Here, it is not always necessary to use all three colors.

Find the number of such ways to paint the dominoes, modulo 1000000007.

The arrangement of the dominoes is given to you as two strings S_1 and S_2 in the following manner:

Each domino is represented by a different English letter (lowercase or uppercase).

The j-th character in S_i represents the domino that occupies the square at the i-th row from the top and j-th column from the left.

Constraints

1 \leq N \leq 52

|S_1| = |S_2| = N

S_1 and S_2 consist of lowercase and uppercase English letters.

S_1 and S_2 represent a valid arrangement of dominoes.

Input

Input is given from Standard Input in the following format:

N
S_1
S_2

Output

Print the number of such ways to paint the dominoes, modulo 1000000007.

Sample Input 1

3
aab
ccb

Sample Output 1

6

There are six ways as shown below:

Sample Input 2

1
Z
Z

Sample Output 2

3

Note that it is not always necessary to use all the colors.

Sample Input 3

52
RvvttdWIyyPPQFFZZssffEEkkaSSDKqcibbeYrhAljCCGGJppHHn
RLLwwdWIxxNNQUUXXVVMMooBBaggDKqcimmeYrhAljOOTTJuuzzn

Sample Output 3

958681902


program sample
    implicit none

    integer(8) :: i,j,m,n,k,a,b

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

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

    call msort(x)
    a=0
    b=0
    do i=n,1,-1

        if(x(i)==x(i-1))then
            if (a==1)then
                a=0
                cycle
            end if
            a=1
            b=b+1
            if (b==1)then
                m=x(i)
                a=1
            else
                m=m*x(i)
                exit
            end if
        else
            a=0
        end if
    end do
    if (b==2)then
    write(*,*)m
    else
        write(*,*)0
    end if

    stop

    contains
    recursive subroutine msort(x)
    implicit none
    integer(8), intent(inout) :: x(:)

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

    n = size(x)

    if(n == 1) then
      return
    end if

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

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

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

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

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

    deallocate(tmp)
    return
  end subroutine msort

end program sample


program arc081_d
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: n,i,j,ans
    integer(int64),parameter:: md=1000000007
    character(:),allocatable:: s(:)

    read*, n
    allocate(character(n):: s(2))
    read*, s(1)
    read*, s(2)

    i=1
    if (s(1)(1:1) == s(2)(1:1)) then
        ans = 3
    else
        ans = 6
    end if

    do while (i <= n)
        if (s(1)(i:i) == s(2)(i:i)) then
            if (i+1 <= n) ans=mod(ans*2,md)
            i=i+1
        else
            if (i+2 <= n) then
                j = i+2
                if (s(1)(j:j) /= s(2)(j:j)) ans=mod(ans*3,md)
            end if
            i=i+2
        end if
    end do

    print'(i0)', ans
end program arc081_d


program ABC070A
    implicit none
    integer(8)::N
    read(5,*)N

    if(N/100==mod(N,10))then
        print'(A)',"Yes"
    else
        print'(A)',"No"
    end if
end program ABC070A

Score : 100 points

Problem Statement

You are given a three-digit positive integer N.

Determine whether N is a palindromic number.

Here, a palindromic number is an integer that reads the same backward as forward in decimal notation.

Constraints

100≤N≤999

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If N is a palindromic number, print Yes; otherwise, print No.

Sample Input 1

575

Sample Output 1

Yes

N=575 is also 575 when read backward, so it is a palindromic number. You should print Yes.

Sample Input 2

123

Sample Output 2

No

N=123 becomes 321 when read backward, so it is not a palindromic number. You should print No.

Sample Input 3

812

Sample Output 3

No


      program main
      read(*,*) i
      if (mod(i,10) .eq. i/100) then
            write(*,*) "Yes"
      else
            write(*,*) "No"
      end if
      end


program sample
    implicit none
    character(100)::s,t
    integer(8) :: i,j,k,m,n
    integer(8)::a,b,c,d
    integer(8),allocatable::x(:),y(:)

    read(*,*) n
    allocate(x(n))
    do i=1,n
    read(*,*)x(i)
    end do
    a=x(1)
    m=a
    do i=2,n
        b=max(a,x(i))
        a=min(a,x(i))
        do j=1,10000000
            if(mod(b,a)==0)then
                exit
            else
                c=mod(b,a)
                b=a
                a=c
            end if
        end do
        m=m*(x(i)/a)
        write(*,*)m,'m'
        a=m
    end do

    write(*,*)m
    stop
end program sample

Score : 300 points

Problem Statement

We have N clocks. The hand of the i-th clock (1≤i≤N) rotates through 360° in exactly T_i seconds.

Initially, the hand of every clock stands still, pointing directly upward.

Now, Dolphin starts all the clocks simultaneously.

In how many seconds will the hand of every clock point directly upward again?

Constraints

1≤N≤100

1≤T_i≤10^{18}

All input values are integers.

The correct answer is at most 10^{18} seconds.

Input

Input is given from Standard Input in the following format:

N
T_1
:
T_N

Output

Print the number of seconds after which the hand of every clock point directly upward again.

Sample Input 1

2
2
3

Sample Output 1

6

We have two clocks. The time when the hand of each clock points upward is as follows:

Clock 1: 2, 4, 6, ... seconds after the beginning

Clock 2: 3, 6, 9, ... seconds after the beginning

Therefore, it takes 6 seconds until the hands of both clocks point directly upward.

Sample Input 2

5
2
5
10
1000000000000000000
1000000000000000000

Sample Output 2

1000000000000000000


Score : 100 points

Problem Statement

In K-city, there are n streets running east-west, and m streets running north-south. Each street running east-west and each street running north-south cross each other. We will call the smallest area that is surrounded by four streets a block. How many blocks there are in K-city?

Constraints

2 ≤ n, m ≤ 100

Input

Input is given from Standard Input in the following format:

n m

Output

Print the number of blocks in K-city.

Sample Input 1

3 4

Sample Output 1

6

There are six blocks, as shown below:

Sample Input 2

2 2

Sample Output 2

1

There are one block, as shown below:


program ABC_069_C_4adjacent
   implicit none
   integer N, i, countN2, countN4
   integer(8), allocatable :: a(:)
   read(*, *) N
   allocate(a(1: N))
   read(*, *) a(1: N)
   countN2 = 0
   countN4 = 0
   do i = 1, N
       if(mod(a(i), 2) == 0) then
	       countN2 = countN2 + 1
	   end if
	   if(mod(a(i), 4) == 0) then
	       countN4 = countN4 + 1
	   end if
   end do
   if(N/2 <= countN4) then
       write(*, *) 'Yes'
   else if(N - 2*countN4 <= countN2 - countN4) then
       write(*, *) 'Yes'
   else
       write(*, *) 'No'
   end if
end program ABC_069_C_4adjacent

Score : 400 points

Problem Statement

We have a sequence of length N, a = (a_1, a_2, ..., a_N).
Each a_i is a positive integer.

Snuke's objective is to permute the element in a so that the following condition is satisfied:

For each 1 ≤ i ≤ N - 1, the product of a_i and a_{i + 1} is a multiple of 4.

Determine whether Snuke can achieve his objective.

Constraints

2 ≤ N ≤ 10^5

a_i is an integer.

1 ≤ a_i ≤ 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

If Snuke can achieve his objective, print Yes; otherwise, print No.

Sample Input 1

3
1 10 100

Sample Output 1

Yes

One solution is (1, 100, 10).

Sample Input 2

4
1 2 3 4

Sample Output 2

No

It is impossible to permute a so that the condition is satisfied.

Sample Input 3

3
1 4 1

Sample Output 3

Yes

The condition is already satisfied initially.

Sample Input 4

2
1 1

Sample Output 4

No

Sample Input 5

6
2 7 1 8 2 8

Sample Output 5

Yes


program main
  implicit none
  integer :: n
  integer :: h, w
  integer, allocatable :: an(:)
  integer, allocatable :: masu(:)
  integer :: i, j, cnt

  read *, h, w
  read *, n

  allocate(an(n))
  allocate(masu(h*w))

  read *, an(1:n)

  cnt = 1
  do i = 1, n
    masu(cnt:cnt+an(i)-1) = i
    cnt = cnt + an(i)
  enddo

  do i = 1, h
    if(mod(i,2)==1)then
      print *, (masu(w*(i-1)+j), j = 1,w,1)
    else
      print *, (masu(w*(i-1)+j), j = w,1,-1)
    endif
  enddo

end program

Score : 400 points

Problem Statement

We have a grid with H rows and W columns of squares.
Snuke is painting these squares in colors 1, 2, ..., N.
Here, the following conditions should be satisfied:

For each i (1 ≤ i ≤ N), there are exactly a_i squares painted in Color i. Here, a_1 + a_2 + ... + a_N = H W.

For each i (1 ≤ i ≤ N), the squares painted in Color i are 4-connected. That is, every square painted in Color i can be reached from every square painted in Color i by repeatedly traveling to a horizontally or vertically adjacent square painted in Color i.

Find a way to paint the squares so that the conditions are satisfied.
It can be shown that a solution always exists.

Constraints

1 ≤ H, W ≤ 100

1 ≤ N ≤ H W

a_i ≥ 1

a_1 + a_2 + ... + a_N = H W

Input

Input is given from Standard Input in the following format:

H W
N
a_1 a_2 ... a_N

Output

Print one way to paint the squares that satisfies the conditions.
Output in the following format:

c_{1 1} ... c_{1 W}
:
c_{H 1} ... c_{H W}

Here, c_{i j} is the color of the square at the i-th row from the top and j-th column from the left.

Sample Input 1

2 2
3
2 1 1

Sample Output 1

1 1
2 3

Below is an example of an invalid solution:

1 2
3 1

This is because the squares painted in Color 1 are not 4-connected.

Sample Input 2

3 5
5
1 2 3 4 5

Sample Output 2

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

Sample Input 3

1 1
1
1

Sample Output 3

1


program main
    use,intrinsic :: iso_fortran_env
    implicit none
    character(3):: n

    read*, n
    print'(a)', 'ABC'//n
end program main

Score : 100 points

Problem Statement

This contest, AtCoder Beginner Contest, is abbreviated as ABC.

When we refer to a specific round of ABC, a three-digit number is appended after ABC. For example, ABC680 is the 680th round of ABC.

What is the abbreviation for the N-th round of ABC? Write a program to output the answer.

Constraints

100 ≤ N ≤ 999

Input

Input is given from Standard Input in the following format:

N

Output

Print the abbreviation for the N-th round of ABC.

Sample Input 1

100

Sample Output 1

ABC100

The 100th round of ABC is ABC100.

Sample Input 2

425

Sample Output 2

ABC425

Sample Input 3

999

Sample Output 3

ABC999


program main
	implicit none
	integer n
	read(*,*)n
	write(*,'(a3,i3)')"ABC",n
end program main


integer N,a
read*,N
a=1
do while (a*2<=N)
 a=a*2
end do
print"(I0)",a
end

Score : 200 points

Problem Statement

Takahashi loves numbers divisible by 2.

You are given a positive integer N. Among the integers between 1 and N (inclusive), find the one that can be divisible by 2 for the most number of times. The solution is always unique.

Here, the number of times an integer can be divisible by 2, is how many times the integer can be divided by 2 without remainder.

For example,

6 can be divided by 2 once: 6 -> 3.

8 can be divided by 2 three times: 8 -> 4 -> 2 -> 1.

3 can be divided by 2 zero times.

Constraints

1 ≤ N ≤ 100

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

7

Sample Output 1

4

4 can be divided by 2 twice, which is the most number of times among 1, 2, ..., 7.

Sample Input 2

32

Sample Output 2

32

Sample Input 3

1

Sample Output 3

1

Sample Input 4

100

Sample Output 4

64


program decrease
  implicit none
  integer :: n, i
  integer(8) :: a(50), m, t
  a = 0_8
  read(*,*) n
  read(*,*) a(1:n)
  t = 0_8
  do
    m = maxval(a(1:n))
    if (m.lt.int(n,8)) exit
    do i = 1, n
      if (a(i).eq.m) then
        a(i) = a(i) - int(n+1,8)
        exit
      end if
    end do
    a(1:n) = a(1:n) + 1_8
    t = t + 1_8
  end do
  write(*,'(i0)') t
  stop
end program decrease

Score : 600 points

Problem Statement

We have a sequence of length N consisting of non-negative integers. Consider performing the following operation on this sequence until the largest element in this sequence becomes N-1 or smaller. (The operation is the same as the one in Problem D.)

Determine the largest element in the sequence (if there is more than one, choose one). Decrease the value of this element by N, and increase each of the other elements by 1.

It can be proved that the largest element in the sequence becomes N-1 or smaller after a finite number of operations.

You are given the sequence a_i. Find the number of times we will perform the above operation.

Constraints

2 ≤ N ≤ 50

0 ≤ a_i ≤ 10^{16} + 1000

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the number of times the operation will be performed.

Sample Input 1

4
3 3 3 3

Sample Output 1

0

Sample Input 2

3
1 0 3

Sample Output 2

1

Sample Input 3

2
2 2

Sample Output 3

2

Sample Input 4

7
27 0 0 0 0 0 0

Sample Output 4

3

Sample Input 5

10
1000 193 256 777 0 1 1192 1234567891011 48 425

Sample Output 5

1234567894848


      program main
      read(*,*) i, j

      if (i.eq.0) then
        write(*,*) "Impossible"
      else if(j.eq.0) then
        write(*,*) "Impossible"
      else
        if (mod(i,3).eq.0) then
          write(*,*) "Possible"
        else if (mod(j,3).eq.0) then
          write(*,*) "Possible"
        else if (mod(i+j,3).eq.0) then
          write(*,*) "Possible"
        else
          write(*,*) "Impossible"
        end if
      end if
      end

Score : 100 points

Problem Statement

Snuke is giving cookies to his three goats.

He has two cookie tins. One contains A cookies, and the other contains B cookies. He can thus give A cookies, B cookies or A+B cookies to his goats (he cannot open the tins).

Your task is to determine whether Snuke can give cookies to his three goats so that each of them can have the same number of cookies.

Constraints

1 \leq A,B \leq 100

Both A and B are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

If it is possible to give cookies so that each of the three goats can have the same number of cookies, print Possible; otherwise, print Impossible.

Sample Input 1

4 5

Sample Output 1

Possible

If Snuke gives nine cookies, each of the three goats can have three cookies.

Sample Input 2

1 1

Sample Output 2

Impossible

Since there are only two cookies, the three goats cannot have the same number of cookies no matter what Snuke gives to them.


program main
  implicit none

  integer :: i,j,t,n,k,l(50)=0,s=0

  read(*,*)n,k
  read(*,*)(l(i),i=1,n)

  do i=1,N-1
     do j=i+1,N
        if(l(i) .gt. l(j))then
           t=l(i)
           l(i)=l(j)
           l(j)=t
        end if
     end do
  end do

  do i = n, n-k+1, -1
    s = s + l(i)
  end do

  write(*,*)s
end program main

Score : 200 points

Problem Statement

Snuke has N sticks.
The length of the i-th stick is l_i.

Snuke is making a snake toy by joining K of the sticks together.

The length of the toy is represented by the sum of the individual sticks that compose it.
Find the maximum possible length of the toy.

Constraints

1 \leq K \leq N \leq 50

1 \leq l_i \leq 50

l_i is an integer.

Input

Input is given from Standard Input in the following format:

N K
l_1 l_2 l_3 ... l_{N}

Output

Print the answer.

Sample Input 1

5 3
1 2 3 4 5

Sample Output 1

12

You can make a toy of length 12 by joining the sticks of lengths 3, 4 and 5, which is the maximum possible length.

Sample Input 2

15 14
50 26 27 21 41 7 42 35 7 5 5 36 39 1 45

Sample Output 2

386


program abc066a
  implicit none
  integer :: bell(3)
  read *, bell
  print *, sum(bell) - maxval(bell)
end program abc066a

Score : 100 points

Problem Statement

Snuke is buying a bicycle.
The bicycle of his choice does not come with a bell, so he has to buy one separately.

He has very high awareness of safety, and decides to buy two bells, one for each hand.

The store sells three kinds of bells for the price of a, b and c yen (the currency of Japan), respectively.
Find the minimum total price of two different bells.

Constraints

1 \leq a,b,c \leq 10000

a, b and c are integers.

Input

Input is given from Standard Input in the following format:

a b c

Output

Print the minimum total price of two different bells.

Sample Input 1

700 600 780

Sample Output 1

1300

Buying a 700-yen bell and a 600-yen bell costs 1300 yen.

Buying a 700-yen bell and a 780-yen bell costs 1480 yen.

Buying a 600-yen bell and a 780-yen bell costs 1380 yen.

The minimum among these is 1300 yen.

Sample Input 2

10000 10000 10000

Sample Output 2

20000

Buying any two bells costs 20000 yen.


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

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

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

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

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

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

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

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

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

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

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

program name
    use,intrinsic :: iso_fortran_env
    use deque_mod
    implicit none
    integer(int32):: n,i
    integer(int32), allocatable:: a(:), ans(:)
    type(deque):: b

    call init_deque(b)
    read*, n
    allocate(a(n),ans(n))
    read*, a(:)

    do i=1,n
        if (mod(i,2) == 1) then
            call append(b,a(i))
        else
            call appendleft(b,a(i))
        end if
    end do

    if (mod(n,2) /= 0) then
        do i=1,n
            ans(i) = pop(b)
        end do
    else
        do i=1,n
            ans(i) = popleft(b)
        end do
    end if

    print'(100000(i0,1x))', ans(:)

end program name

Score : 300 points

Problem Statement

You are given an integer sequence of length n, a_1, ..., a_n.
Let us consider performing the following n operations on an empty sequence b.

The i-th operation is as follows:

Append a_i to the end of b.

Reverse the order of the elements in b.

Find the sequence b obtained after these n operations.

Constraints

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

0 \leq a_i \leq 10^9

n and a_i are integers.

Input

Input is given from Standard Input in the following format:

n
a_1 a_2 ... a_n

Output

Print n integers in a line with spaces in between.
The i-th integer should be b_i.

Sample Input 1

4
1 2 3 4

Sample Output 1

4 2 1 3

After step 1 of the first operation, b becomes: 1.

After step 2 of the first operation, b becomes: 1.

After step 1 of the second operation, b becomes: 1, 2.

After step 2 of the second operation, b becomes: 2, 1.

After step 1 of the third operation, b becomes: 2, 1, 3.

After step 2 of the third operation, b becomes: 3, 1, 2.

After step 1 of the fourth operation, b becomes: 3, 1, 2, 4.

After step 2 of the fourth operation, b becomes: 4, 2, 1, 3.

Thus, the answer is 4 2 1 3.

Sample Input 2

3
1 2 3

Sample Output 2

3 1 2

As shown above in Sample Output 1, b becomes 3, 1, 2 after step 2 of the third operation. Thus, the answer is 3 1 2.

Sample Input 3

1
1000000000

Sample Output 3

1000000000

Sample Input 4

6
0 6 7 6 7 0

Sample Output 4

0 6 6 0 7 7


program sample
  implicit none
  integer(8) :: n,i,b
integer(8),allocatable :: a(:)

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

b=1
do i=1,n
   a(b)=b
   if(a(b)==2)then
      write(*,*) i
      stop
   endif
end do

write(*,*) -1

end program sample

Score : 200 points

Problem Statement

Takahashi wants to gain muscle, and decides to work out at AtCoder Gym.

The exercise machine at the gym has N buttons, and exactly one of the buttons is lighten up.
These buttons are numbered 1 through N.
When Button i is lighten up and you press it, the light is turned off, and then Button a_i will be lighten up. It is possible that i=a_i.
When Button i is not lighten up, nothing will happen by pressing it.

Initially, Button 1 is lighten up. Takahashi wants to quit pressing buttons when Button 2 is lighten up.

Determine whether this is possible. If the answer is positive, find the minimum number of times he needs to press buttons.

Constraints

2 ≤ N ≤ 10^5

1 ≤ a_i ≤ N

Input

Input is given from Standard Input in the following format:

N
a_1
a_2
:
a_N

Output

Print -1 if it is impossible to lighten up Button 2.
Otherwise, print the minimum number of times we need to press buttons in order to lighten up Button 2.

Sample Input 1

3
3
1
2

Sample Output 1

2

Press Button 1, then Button 3.

Sample Input 2

4
3
4
1
2

Sample Output 2

-1

Pressing Button 1 lightens up Button 3, and vice versa, so Button 2 will never be lighten up.

Sample Input 3

5
3
3
4
2
4

Sample Output 3

3


implicit none
integer(8) :: n,i,count,bo,bn
integer(8),allocatable :: a(:)

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

count=1
bo=a(1)
bn=0
do while(bn/=2.or.bo>0)
bn=a(bo)
count=count+1
a(bo)=-1
bo=bn
end do

if(bn<0)then
write(*,*) bn
else
write(*,*) count
endif

end


program reconciled
  implicit none
  integer(8), parameter :: md = 1000000007_8
  integer(8) :: n, m, i, x = 1_8
  read(*,*) n, m
  if (abs(n-m) > 1_8) then
    write(*,'(i0)') 0
  else
    if (n == m) x = 2_8
    do i = 1_8, min(n,m)
      x = mod(mod(i*i,md)*x,md)
    end do
    do i = min(n,m)+1_8, max(n,m)
      x = mod(i*x,md)
    end do
    write(*,'(i0)') x
  end if
end program reconciled

Score : 300 points

Problem Statement

Snuke has N dogs and M monkeys. He wants them to line up in a row.

As a Japanese saying goes, these dogs and monkeys are on bad terms. ("ken'en no naka", literally "the relationship of dogs and monkeys", means a relationship of mutual hatred.) Snuke is trying to reconsile them, by arranging the animals so that there are neither two adjacent dogs nor two adjacent monkeys.

How many such arrangements there are? Find the count modulo 10^9+7 (since animals cannot understand numbers larger than that).
Here, dogs and monkeys are both distinguishable. Also, two arrangements that result from reversing each other are distinguished.

Constraints

1 ≤ N,M ≤ 10^5

Input

Input is given from Standard Input in the following format:

N M

Output

Print the number of possible arrangements, modulo 10^9+7.

Sample Input 1

2 2

Sample Output 1

8

We will denote the dogs by A and B, and the monkeys by C and D. There are eight possible arrangements: ACBD, ADBC, BCAD, BDAC, CADB, CBDA, DACB and DBCA.

Sample Input 2

3 2

Sample Output 2

12

Sample Input 3

1 8

Sample Output 3

0

Sample Input 4

100000 100000

Sample Output 4

530123477


program sample
    implicit none
    character(100)::s
    integer(8) :: i,j,m,n,x(26),y(26),z(26),a,b

    read(*,*) s
    n=len_trim(s)
    x(:)=0
    y(:)=-1
    do i=1,n
        x(:)=x(:)+1
        a=ichar(s(i:i))-96
        x(a)=x(a)-1
        y(a)=max(x(a),y(a))
        x(a)=0
    end do
    do i=1,26
        y(i)=max(x(i),y(i))
    end do
    write(*,*)minval(y)

    stop
end program sample

Score : 300 points

Problem Statement

Snuke can change a string t of length N into a string t' of length N - 1 under the following rule:

For each i (1 ≤ i ≤ N - 1), the i-th character of t' must be either the i-th or (i + 1)-th character of t.

There is a string s consisting of lowercase English letters.
Snuke's objective is to apply the above operation to s repeatedly so that all the characters in s are the same.
Find the minimum necessary number of operations.

Constraints

1 ≤ |s| ≤ 100

s consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s

Output

Print the minimum necessary number of operations to achieve the objective.

Sample Input 1

serval

Sample Output 1

3

One solution is: serval → srvvl → svvv → vvv.

Sample Input 2

jackal

Sample Output 2

2

One solution is: jackal → aacaa → aaaa.

Sample Input 3

zzz

Sample Output 3

0

All the characters in s are the same from the beginning.

Sample Input 4

whbrjpjyhsrywlqjxdbrbaomnw

Sample Output 4

8

In 8 operations, he can change s to rrrrrrrrrrrrrrrrrr.


program plus_minus_rectangle
  implicit none
  integer :: h, w, x, y, a(500,500), i, j
  a = 0
  read(*,*) h, w, x, y
  if (mod(h,x).eq.0.and.mod(w,y).eq.0) then
    write(*,'(a)') "No"
    stop
  end if
  write(*,'(a)') "Yes"
  do i = 1, h, x
    do j = 1, w, y
      a(i,j) = 999999
      if (i+x-1.le.h.and.j+y-1.le.w) a(i+x-1,j+y-1) = -1000000
    end do
  end do
  do i = 1, h
    call output(a(i,1:w))
  end do
  stop
contains
  subroutine output(a)
    implicit none
    integer, intent(in) :: a(:)
    integer :: n, i
    n = size(a)
    do i = 1, n
      write(*,'(i0,x)',advance='no') a(i)
    end do
    write(*,*)
    return
  end subroutine output
end program plus_minus_rectangle

Score : 700 points

Problem Statement

You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W).
Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive:

The matrix has H rows and W columns.

Each element of the matrix is an integer between -10^9 and 10^9 (inclusive).

The sum of all the elements of the matrix is positive.

The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative.

Constraints

1 ≤ h ≤ H ≤ 500

1 ≤ w ≤ W ≤ 500

Input

Input is given from Standard Input in the following format:

H W h w

Output

If there does not exist a matrix that satisfies all of the conditions, print No.

Otherwise, print Yes in the first line, and print a matrix in the subsequent lines in the following format:

a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}

Here, a_{ij} represents the (i,\ j) element of the matrix.

Sample Input 1

3 3 2 2

Sample Output 1

Yes
1 1 1
1 -4 1
1 1 1

The sum of all the elements of this matrix is 4, which is positive.
Also, in this matrix, there are four subrectangles with 2 rows and 2 columns as shown below. For each of them, the sum of all the elements inside is -1, which is negative.

Sample Input 2

2 4 1 2

Sample Output 2

No

Sample Input 3

3 4 2 3

Sample Output 3

Yes
2 -5 8 7
3 -5 -4 -5
2 1 -1 7


program ABC064B
    implicit none
    integer(8)::N,i
    integer(8),allocatable,dimension(:)::A
    read(5,*)N
    allocate(A(N))
    read(5,*)(A(i),i=1,N)
    print'(i0)',maxval(A)-minval(A)
end program ABC064B

Score : 200 points

Problem Statement

It is only six months until Christmas, and AtCoDeer the reindeer is now planning his travel to deliver gifts.

There are N houses along TopCoDeer street. The i-th house is located at coordinate a_i. He has decided to deliver gifts to all these houses.

Find the minimum distance to be traveled when AtCoDeer can start and end his travel at any positions.

Constraints

1 ≤ N ≤ 100

0 ≤ a_i ≤ 1000

a_i is an integer.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the minimum distance to be traveled.

Sample Input 1

4
2 3 7 9

Sample Output 1

7

The travel distance of 7 can be achieved by starting at coordinate 9 and traveling straight to coordinate 2.

It is not possible to do with a travel distance of less than 7, and thus 7 is the minimum distance to be traveled.

Sample Input 2

8
3 1 4 1 5 9 2 6

Sample Output 2

8

There may be more than one house at a position.


program name
    implicit none
    integer(4):: n
    integer(4),allocatable:: a(:)
    integer(4):: over
    integer(4):: i,j
    logical:: iro(8)

    read*, n
    allocate(a(n))
    read*, a(:)
    iro(:) = .false.
    over = 0

    do i=1,n
        do j=0,4800
            if (j*400 <= a(i) .and. a(i) < (j+1)*400) then
                if (j < 8) then
                    iro(j+1) = .true.
                else
                    over=over+1
                end if
            end if
        end do
    end do

    print*, tsum(iro)
    print*, min(tsum(iro)+over, 8)

contains
    function tsum(larr) result(ret)
        logical:: larr(:)
        integer(4):: ret
        integer(4):: i
        ret=0
        do i=1,size(larr)
            if (larr(i)) ret=ret+1
        end do
    end function
end program name

Score : 300 points

Problem Statement

In AtCoder, a person who has participated in a contest receives a color, which corresponds to the person's rating as follows:

Rating 1-399 : gray

Rating 400-799 : brown

Rating 800-1199 : green

Rating 1200-1599 : cyan

Rating 1600-1999 : blue

Rating 2000-2399 : yellow

Rating 2400-2799 : orange

Rating 2800-3199 : red

Other than the above, a person whose rating is 3200 or higher can freely pick his/her color, which can be one of the eight colors above or not.

Currently, there are N users who have participated in a contest in AtCoder, and the i-th user has a rating of a_i.

Find the minimum and maximum possible numbers of different colors of the users.

Constraints

1 ≤ N ≤ 100

1 ≤ a_i ≤ 4800

a_i is an integer.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the minimum possible number of different colors of the users, and the maximum possible number of different colors, with a space in between.

Sample Input 1

4
2100 2500 2700 2700

Sample Output 1

2 2

The user with rating 2100 is "yellow", and the others are "orange". There are two different colors.

Sample Input 2

5
1100 1900 2800 3200 3200

Sample Output 2

3 5

The user with rating 1100 is "green", the user with rating 1900 is blue and the user with rating 2800 is "red".

If the fourth user picks "red", and the fifth user picks "blue", there are three different colors. This is one possible scenario for the minimum number of colors.

If the fourth user picks "purple", and the fifth user picks "black", there are five different colors. This is one possible scenario for the maximum number of colors.

Sample Input 3

20
800 810 820 830 840 850 860 870 880 890 900 910 920 930 940 950 960 970 980 990

Sample Output 3

1 1

All the users are "green", and thus there is one color.


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

read(*,*) n
allocate( a(n) )
read(*,*) a
a = a/400
b = 0
k = 0
do i = 1, n
   do j = 0, 7
      if( a(i) == j ) then
         b( j+1 ) = 1
      end if
   end do
   if( a(i) .ge. 8 ) then
      k = k + 1
   end if
end do
write(*,'(i0)') sum(b), min(8,sum(b)+k)
end program main


program prob5
    implicit none
    character(105) :: s
    integer :: n
    integer :: m, i, tmp, q, w

    read(*,*) n,s

    m = 0
    tmp = 0
    q = 0
    w = 0
    do i = 1, n
        if(s(i:i) == '(') then
            tmp = tmp + 1
            q = q + 1
        else
            tmp = tmp - 1
            w = w + 1
        end if
        m = min(m, tmp)
    end do

    do i = 1, -m
        write(*,'(a)',advance='no') '('
    end do

    q = q - m
    write(*,'(a)',advance='no') trim(adjustl(s))
    do i = 1, q - w
        write(*,'(a)',advance='no') ')'
    end do
    write(*,*)

    stop
contains
end program prob5

Score : 400 points

Problem Statement

You are given a string S of length N consisting of ( and ). Your task is to insert some number of ( and ) into S to obtain a correct bracket sequence.

Here, a correct bracket sequence is defined as follows:

() is a correct bracket sequence.

If X is a correct bracket sequence, the concatenation of (, X and ) in this order is also a correct bracket sequence.

If X and Y are correct bracket sequences, the concatenation of X and Y in this order is also a correct bracket sequence.

Every correct bracket sequence can be derived from the rules above.

Find the shortest correct bracket sequence that can be obtained. If there is more than one such sequence, find the lexicographically smallest one.

Constraints

The length of S is N.

1 ≤ N ≤ 100

S consists of ( and ).

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the lexicographically smallest string among the shortest correct bracket sequences that can be obtained by inserting some number of ( and ) into S.

Sample Input 1

3
())

Sample Output 1

(())

Sample Input 2

6
)))())

Sample Output 2

(((()))())

Sample Input 3

8
))))((((

Sample Output 3

(((())))(((())))


program main
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: a,b
    read*, a,b
    if (a+b >= 10) then
        print'(a)', 'error'
    else
        print'(i0)', a+b
    end if

end program main

Score : 100 points

Problem Statement

You are given two integers A and B as the input. Output the value of A + B.

However, if A + B is 10 or greater, output error instead.

Constraints

A and B are integers.

1 ≤ A, B ≤ 9

Input

Input is given from Standard Input in the following format:

A B

Output

If A + B is 10 or greater, print the string error (case-sensitive); otherwise, print the value of A + B.

Sample Input 1

6 3

Sample Output 1

9

Sample Input 2

6 4

Sample Output 2

error


program ABC063A
    implicit none
    integer(8)::A,B
    read(5,*)A,B

    if(A+B>=10)then
        print'(A)',"error"
    else
        print'(i0)',A+B
    end if
end program ABC063A


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

    read(*,*) s
    allocate(x(26))
    n=len_trim(s)
    x(:)=0
    do i=1,n
        a=ichar(s(i:i))-96
        x(a)=x(a)+1
    end do
    if(maxval(x)>1)then
        write(*,*)'no'
    else
        write(*,*)'yes'
    end if

    stop
end program sample

Score : 200 points

Problem Statement

You are given a string S consisting of lowercase English letters. Determine whether all the characters in S are different.

Constraints

2 ≤ |S| ≤ 26, where |S| denotes the length of S.

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If all the characters in S are different, print yes (case-sensitive); otherwise, print no.

Sample Input 1

uncopyrightable

Sample Output 1

yes

Sample Input 2

different

Sample Output 2

no

Sample Input 3

no

Sample Output 3

yes


program bugged
implicit none
integer :: N
integer, allocatable, dimension(:, :) :: score
integer :: i, k
integer :: max_score

read *, N

allocate(score(N, 2))
do i = 1, N

    score(i, 1) = i
    read *, score(i, 2)

end do

call heap_sort(score, N)
max_score = 0

do i = 1, N

    max_score = max_score + score(i, 2)

end do

k = 1

do while (mod(max_score, 10) == 0 .and. max_score > 0)

    max_score = max_score - score(k, 2)
    k = k + 1

end do

if (max_score < 0) then

    max_score = max_score + score(k-1, 2)

end if

print '(I0)', max_score

end program bugged

! heap sort
subroutine heap_sort( list, n )
implicit none

integer :: n
integer, dimension( n, 2 ) :: list

integer :: i, k
integer, dimension( 2 ) :: temporary

do k = n/2, 1, -1

    temporary( 1 ) = list( k, 1 )
    temporary( 2 ) = list( k, 2 )

    call make_heap( n, list, k, n, temporary )

end do

do k = n, 2, -1

    temporary( 1 ) = list( k, 1 )
    temporary( 2 ) = list( k, 2 )

    list( k, 1 ) = list( 1, 1 )
    list( k, 2 ) = list( 1, 2 )

    call make_heap( n, list, 1, k-1, temporary )

end do

!-----------------------------------------------------------------------------------
contains
    subroutine make_heap( n, list, root, leaf, var )
    implicit none

    integer :: n, root, leaf
    integer, dimension( n, 2 ) :: list
    integer, dimension( 2 ) :: var

    ! local
    integer :: i, j

    i = root
    j = i * 2

    ! 条件を満たすまで無限ループ
    do
        if ( j > leaf ) exit

        if ( j < leaf ) then

            if ( list( j, 2 ) < list( j + 1, 2 ) ) j = j + 1

        end if

        if ( list( j, 2 )  >  var( 2 ) ) then

            list( i, 1 ) = list( j, 1 )
            list( i, 2 ) = list( j, 2 )

            i = j
            j = i * 2

        else

            j = leaf + 1

        endif

    end do

    list( i, 1 ) = var( 1 )
    list( i, 2 ) = var( 2 )

    end subroutine make_heap

!----------------------------------------------------------------------------------

end subroutine heap_sort

Score : 300 points

Problem Statement

You are taking a computer-based examination. The examination consists of N questions, and the score allocated to the i-th question is s_i. Your answer to each question will be judged as either "correct" or "incorrect", and your grade will be the sum of the points allocated to questions that are answered correctly. When you finish answering the questions, your answers will be immediately judged and your grade will be displayed... if everything goes well.

However, the examination system is actually flawed, and if your grade is a multiple of 10, the system displays 0 as your grade. Otherwise, your grade is displayed correctly. In this situation, what is the maximum value that can be displayed as your grade?

Constraints

All input values are integers.

1 ≤ N ≤ 100

1 ≤ s_i ≤ 100

Input

Input is given from Standard Input in the following format:

N
s_1
s_2
:
s_N

Output

Print the maximum value that can be displayed as your grade.

Sample Input 1

3
5
10
15

Sample Output 1

25

Your grade will be 25 if the 10-point and 15-point questions are answered correctly and the 5-point question is not, and this grade will be displayed correctly. Your grade will become 30 if the 5-point question is also answered correctly, but this grade will be incorrectly displayed as 0.

Sample Input 2

3
10
10
15

Sample Output 2

35

Your grade will be 35 if all the questions are answered correctly, and this grade will be displayed correctly.

Sample Input 3

3
10
20
30

Sample Output 3

0

Regardless of whether each question is answered correctly or not, your grade will be a multiple of 10 and displayed as 0.


program nuske_vs_phantom_thnook
  implicit none
  integer :: n, m, q, a(0:2000,0:2000), x1, y1, x2, y2, i, j, k
  integer :: x(0:2000,0:2000), y(0:2000,0:2000), z(0:2000,0:2000)
  character(2000) :: s
  a = 0
  x = 0
  y = 0
  z = 0
  read(*,*) n, m, q
  do i = 1, n
    read(*,*) s
    s = trim(adjustl(s))
    do j = 1, m
      if (s(j:j).eq."1") a(i,j) = 1
    end do
  end do
  do i = 1, n
    do j = 1, m
      x(i,j) = x(i,j-1)+x(i-1,j)-x(i-1,j-1)+a(i,j)
      y(i,j) = y(i,j-1)+y(i-1,j)-y(i-1,j-1)+a(i,j)*a(i,j-1)
      z(i,j) = z(i,j-1)+z(i-1,j)-z(i-1,j-1)+a(i,j)*a(i-1,j)
    end do
  end do
  do k = 1, q
    read(*,*) x1, y1, x2, y2
    write(*,'(i0)') f(x,x1,y1,x2,y2)-f(y,x1,y1+1,x2,y2)-f(z,x1+1,y1,x2,y2)
  end do
  stop
contains
  integer function f(x,i1,j1,i2,j2)
    implicit none
    integer, intent(in) :: x(0:2000,0:2000), i1, j1, i2, j2
    f = x(i2,j2)-x(i2,j1-1)-x(i1-1,j2)+x(i1-1,j1-1)
    return
  end function f
end program nuske_vs_phantom_thnook

Score : 700 points

Problem Statement

Nuske has a grid with N rows and M columns of squares. The rows are numbered 1 through N from top to bottom, and the columns are numbered 1 through M from left to right.
Each square in the grid is painted in either blue or white. If S_{i,j} is 1, the square at the i-th row and j-th column is blue; if S_{i,j} is 0, the square is white.
For every pair of two blue square a and b, there is at most one path that starts from a, repeatedly proceeds to an adjacent (side by side) blue square and finally reaches b, without traversing the same square more than once.

Phantom Thnook, Nuske's eternal rival, gives Q queries to Nuske. The i-th query consists of four integers x_{i,1}, y_{i,1}, x_{i,2} and y_{i,2} and asks him the following: when the rectangular region of the grid bounded by (and including) the x_{i,1}-th row, x_{i,2}-th row, y_{i,1}-th column and y_{i,2}-th column is cut out, how many connected components consisting of blue squares there are in the region?

Process all the queries.

Constraints

1 ≤ N,M ≤ 2000

1 ≤ Q ≤ 200000

S_{i,j} is either 0 or 1.

S_{i,j} satisfies the condition explained in the statement.

1 ≤ x_{i,1} ≤ x_{i,2} ≤ N(1 ≤ i ≤ Q)

1 ≤ y_{i,1} ≤ y_{i,2} ≤ M(1 ≤ i ≤ Q)

Input

The input is given from Standard Input in the following format:

N M Q
S_{1,1}..S_{1,M}
:
S_{N,1}..S_{N,M}
x_{1,1} y_{i,1} x_{i,2} y_{i,2}
:
x_{Q,1} y_{Q,1} x_{Q,2} y_{Q,2}

Output

For each query, print the number of the connected components consisting of blue squares in the region.

Sample Input 1

3 4 4
1101
0110
1101
1 1 3 4
1 1 3 1
2 2 3 4
1 2 2 4

Sample Output 1

3
2
2
2

In the first query, the whole grid is specified. There are three components consisting of blue squares, and thus 3 should be printed.

In the second query, the region within the red frame is specified. There are two components consisting of blue squares, and thus 2 should be printed.
Note that squares that belong to the same component in the original grid may belong to different components.

Sample Input 2

5 5 6
11010
01110
10101
11101
01010
1 1 5 5
1 2 4 5
2 3 3 4
3 3 3 3
3 1 3 5
1 1 3 4

Sample Output 2

3
2
1
1
3
2


Score : 100 points

Problem Statement

Based on some criterion, Snuke divided the integers from 1 through 12 into three groups as shown in the figure below.
Given two integers x and y (1 ≤ x < y ≤ 12), determine whether they belong to the same group.

Constraints

x and y are integers.

1 ≤ x < y ≤ 12

Input

Input is given from Standard Input in the following format:

x y

Output

If x and y belong to the same group, print Yes; otherwise, print No.

Sample Input 1

1 3

Sample Output 1

Yes

Sample Input 2

2 4

Sample Output 2

No


integer x(12)
integer a,b
x=(/1,2,1,3,1,3,1,1,3,1,3,1/)
read*,a,b
if(x(a)==x(b))then
	print"(A)","Yes"
else
	print"(A)","No"
endif
end


CHARACTER(150) JOGE
CHARACTER(150) BUTSUGIRI_PICTURE
INTEGER H,W
READ*,H,W

JOGE=""
DO I=1,W+2
  JOGE=TRIM(JOGE)//"#"
END DO

PRINT"(A)",TRIM(JOGE)
DO I=1,H
  READ*,BUTSUGIRI_PICTURE
  PRINT"(A)","#"//TRIM(BUTSUGIRI_PICTURE)//"#"
END DO
PRINT"(A)",TRIM(JOGE)
END

Score : 200 points

Problem Statement

You are given a image with a height of H pixels and a width of W pixels.
Each pixel is represented by a lowercase English letter.
The pixel at the i-th row from the top and j-th column from the left is a_{ij}.

Put a box around this image and output the result. The box should consist of # and have a thickness of 1.

Constraints

1 ≤ H, W ≤ 100

a_{ij} is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

H W
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}

Output

Print the image surrounded by a box that consists of # and has a thickness of 1.

Sample Input 1

2 3
abc
arc

Sample Output 1

#####
#abc#
#arc#
#####

Sample Input 2

1 1
z

Sample Output 2

###
#z#
###


program main
  implicit none
  integer :: h, w, i, j
  character(102) :: line
  character(1), allocatable :: tab(:, :)
  read(*, *) h, w
  allocate(tab(0:w + 1, 0:h + 1))
  tab(:, :) = "#"
  do i = 1, h
    read(*, *) line
    do j = 1, w
      tab(j, i) = line(j:j)
    end do
  end do
  do i = 0, h + 1
    do j = 0, w + 1
      line(j + 1:j + 1) = tab(j, i)
    end do
    write(*, "(a)") trim(line)
  end do
end program main


program between
  implicit none
  integer :: a,b,c

  read(*,*) a, b, c
  if ( c<=b .and. c>=a) then
     write(*,*) 'Yes'
  else
     write(*,*) 'No'
  end if
  stop
end program between

Score : 100 points

Problem Statement

You are given three integers A, B and C.
Determine whether C is not less than A and not greater than B.

Constraints

-100≤A,B,C≤100

A, B and C are all integers.

Input

Input is given from Standard Input in the following format:

A B C

Output

If the condition is satisfied, print Yes; otherwise, print No.

Sample Input 1

1 3 2

Sample Output 1

Yes

C=2 is not less than A=1 and not greater than B=3, and thus the output should be Yes.

Sample Input 2

6 5 4

Sample Output 2

No

C=4 is less than A=6, and thus the output should be No.

Sample Input 3

2 2 2

Sample Output 3

Yes


program main
   integer(8) N, K, i
   integer(8), allocatable :: a(:), b(:)
   read(*, *) N, K
   allocate(a(1: N))
   allocate(b(1: N))
   do i = 1, N
       read(*, *) a(i), b(i)
   end do
   call heapsort(N,a,b)

   do i = 1, N
       if(1 <= K .and. K <= b(i)) then
	       write(*, *) a(i)
	       exit
	   else
	       K = K - b(i)
	   end if
   end do
end program main

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

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

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

  l=n/2+1
  k=n
  do while(k.ne.1)
     if(l.gt.1)then
        l=l-1
        t=array(L)
		tb=arrayb(L)
     else
        t=array(k)
		tb=arrayb(L)
        array(k)=array(1)
		arrayb(k)=arrayb(1)
        k=k-1
        if(k.eq.1) then
           array(1)=t
		   arrayb(1)=tb
           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)
		   arrayb(i)=arrayb(j)
           i=j
           j=j+j
        else
           j=k+1
        endif
     enddo
     array(i)=t
	 arrayb(i)=tb
  enddo

  return
end subroutine heapsort

Score : 300 points

Problem Statement

There is an empty array.
The following N operations will be performed to insert integers into the array.
In the i-th operation (1≤i≤N), b_i copies of an integer a_i are inserted into the array.
Find the K-th smallest integer in the array after the N operations.
For example, the 4-th smallest integer in the array \{1,2,2,3,3,3\} is 3.

Constraints

1≤N≤10^5

1≤a_i,b_i≤10^5

1≤K≤b_1…+…b_n

All input values are integers.

Input

Input is given from Standard Input in the following format:

N K
a_1 b_1
:
a_N b_N

Output

Print the K-th smallest integer in the array after the N operations.

Sample Input 1

3 4
1 1
2 2
3 3

Sample Output 1

3

The resulting array is the same as the one in the problem statement.

Sample Input 2

10 500000
1 100000
1 100000
1 100000
1 100000
1 100000
100000 100000
100000 100000
100000 100000
100000 100000
100000 100000

Sample Output 2

1


integer N,M
integer(16),allocatable::a(:),b(:),c(:)
integer(16),allocatable::dist(:)
integer(16) ans

logical,allocatable::negative(:)

read*,N,M
allocate( a(M),b(M),c(M) )
do i=1,M
  read*,a(i),b(i),c(i)
  c(i)=-c(i)
end do

allocate(dist(N))
dist=huge(a(J))/2
dist(1)=0
do i=1,N
  do j=1,M
    if(dist(a(j))==huge(a(j))/2)cycle
    if (dist(b(j)) > dist(a(j))+c(j) )dist(b(j))= dist(a(j))+ c(j)
  end do
end do
ans=dist(N)

allocate(negative(N))
negative=.false.
do i=1,N
  do j=1,M
   if (dist(a(j)) == INF)cycle
   if (dist(b(j)) > dist(a(j)) + c(j) )then
     dist(b(j)) = dist(a(j)) + c(j)
     negative(b(j)) = .true.
   endif
   if (negative(a(j)) ) negative(b(j)) =.true.
  end do
end do

if(negative(N))then
  print"(A)","inf"
else
  print"(i0)",-ans
end if
end

Score : 400 points

Problem Statement

There is a directed graph with N vertices and M edges.
The i-th edge (1≤i≤M) points from vertex a_i to vertex b_i, and has a weight c_i.
We will play the following single-player game using this graph and a piece.

Initially, the piece is placed at vertex 1, and the score of the player is set to 0.
The player can move the piece as follows:

When the piece is placed at vertex a_i, move the piece along the i-th edge to vertex b_i. After this move, the score of the player is increased by c_i.

The player can end the game only when the piece is placed at vertex N.
The given graph guarantees that it is possible to traverse from vertex 1 to vertex N.

When the player acts optimally to maximize the score at the end of the game, what will the score be?
If it is possible to increase the score indefinitely, print inf.

Constraints

2≤N≤1000

1≤M≤min(N(N-1),2000)

1≤a_i,b_i≤N (1≤i≤M)

a_i≠b_i (1≤i≤M)

a_i≠a_j or b_i≠b_j (1≤i<j≤M)

-10^9≤c_i≤10^9 (1≤i≤M)

c_i is an integer.

In the given graph, there exists a path from vertex 1 to vertex N.

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1 c_1
a_2 b_2 c_2
:
a_M b_M c_M

Output

Print the maximum possible score at the end of the game, if it is finite. If it is possible to increase the score indefinitely, print inf.

Sample Input 1

3 3
1 2 4
2 3 3
1 3 5

Sample Output 1

7

There are two ways to move the piece to vertex N=3:

vertex 1 → vertex 2 → vertex 3 : score 4+3=7

vertex 1 → vertex 3 : score 5

Thus, the maximum possible score at the end of the game is 7.

Sample Input 2

2 2
1 2 1
2 1 1

Sample Output 2

inf

It is possible to increase the score indefinitely by alternating between vertex 1 and 2.

Sample Input 3

6 5
1 2 -1000000000
2 3 -1000000000
3 4 -1000000000
4 5 -1000000000
5 6 -1000000000

Sample Output 3

-5000000000


program main
    implicit none
    integer(8), parameter:: inf = 10**9+1
    integer(4):: n,m, i,j,k
    integer(8), allocatable:: a(:),b(:),c(:)
    integer(8), allocatable:: dist(:)

    read*, n, m
    allocate(a(m), b(m), c(m), dist(n))
    do i = 1, m
        read*, a(i),b(i),c(i)
    end do

    dist(1:n) = inf*m
    dist(1) = 0
    c = c*(-1)
    do j = 1, m
        do i = 1, m
            if (dist(b(i)) <= dist(a(i)) + c(i))cycle
            dist(b(i)) = dist(a(i)) + c(i)
            if(j <= m-1) cycle
            print*, 'inf'
            stop
        end do
    end do

    print*, -dist(n)

end program


program contest
  implicit none

  integer :: a, b, c, d

  read(*,*) a, b, c

  if( mod(a,2)==0 ) then
     d = 0
  else if( mod(b,2)==0 ) then
     d = 0
  else if( mod(c,2)==0 ) then
     d = 0
  else if( a>=b .and. a>=c ) then
     d = b*c
  else if( b>=a .and. b>=c ) then
     d = a*c
  else if( c>=a .and. c>=b ) then
     d = b*a
  end if

  write(*,*) d

  stop
end program contest

Score : 300 points

Problem Statement

Takahashi, Aoki and Snuke love cookies. They have A, B and C cookies, respectively. Now, they will exchange those cookies by repeating the action below:

Each person simultaneously divides his cookies in half and gives one half to each of the other two persons.

This action will be repeated until there is a person with odd number of cookies in hand.

How many times will they repeat this action?
Note that the answer may not be finite.

Constraints

1 ≤ A,B,C ≤ 10^9

Input

Input is given from Standard Input in the following format:

A B C

Output

Print the number of times the action will be performed by the three people, if this number is finite.
If it is infinite, print -1 instead.

Sample Input 1

4 12 20

Sample Output 1

3

Initially, Takahashi, Aoki and Snuke have 4, 12 and 20 cookies. Then,

After the first action, they have 16, 12 and 8.

After the second action, they have 10, 12 and 14.

After the third action, they have 13, 12 and 11.

Now, Takahashi and Snuke have odd number of cookies, and therefore the answer is 3.

Sample Input 2

14 14 14

Sample Output 2

-1

Sample Input 3

454 414 444

Sample Output 3

1


  program agc014
      implicit none
      integer(8)::a,b,c
      integer(8)::aa,bb,cc
      integer(8)::count
      logical::check=.true.

         read *, a,b,c

         if (a==b .and. b==c .and. c==a) then
            print *, '-1'
            stop
         end if

         aa=a;bb=b;cc=c
         count=0
         do while (mod(a,2)/=0 .or. mod(b,2)/=0 .or. mod(c,2)/=0)
            a=a+bb/2+cc/2
            b=b+cc/2+aa/2
            c=c+aa/2+bb/2
            count=count+1
            aa=a;bb=b;cc=c
         end do

         print *, count

   end program agc014


implicit none

integer :: n
logical :: flg_a, flg_b, flg_c
integer :: tmp_a, tmp_b, tmp_c
integer :: a,b,c

read *,a,b,c

flg_a = isOdd(a)
flg_b = isOdd(b)
flg_c = isOdd(c)
if (flg_a .or. flg_b .or. flg_c) then
   print '(i0)',0
endif

if (a == b .and. b == c .and. c == a) then
   print '(i0)',-1
endif

flg_a = .false.
flg_b = .false.
flg_c = .false.

n = 0
do while (.true.)
   flg_a = isOdd(a)
   flg_b = isOdd(b)
   flg_c = isOdd(c)
   if (flg_a .or. flg_b .or. flg_c) then
      exit
   endif
   n = n + 1
   tmp_a = a
   tmp_b = b
   tmp_c = c
   a = (tmp_b+tmp_c)/2
   b = (tmp_c+tmp_a)/2
   c = (tmp_a+tmp_b)/2
enddo

write(6,'(i0)') n
stop

contains

   logical function isOdd(n)
      integer,intent(in) :: n
      isOdd = mod(n,2) == 1
   end function isOdd

end


module mod_graph
  implicit none
  integer(8), parameter :: inf = 1000000000000000000_8

  type item
    integer :: id = 0
    integer(8) :: wt = 0_8
  end type

  type priorityqueue
    integer :: num = 0
    type(item), pointer :: heap(:) => null()
  contains
    !final :: finalizei
  end type

  type edge
    integer :: fm = 0, to = 0, cp = 0, rv = 0
    integer(8) :: wt = 0_8
  end type

  type arraylist
    integer :: num = 0
    type(edge), pointer :: arr(:) => null()
  contains
    procedure :: add => adde
    procedure :: bellman_ford => bellman_ford
    !final :: finalizee
  end type

  type graph
    type(arraylist), pointer :: egs(:) => null()
    logical, pointer :: used(:) => null()
  contains
    procedure :: add => add
    procedure :: dijkstra => dijkstra
    procedure :: ford_fulkerson => ford_fulkerson
    !final :: finalize
  end type

  interface graph
    module procedure :: newg
  end interface graph

contains

  function newi(id,wt) result(ret)
    integer, intent(in) :: id
    integer(8), intent(in) :: wt
    type(item) :: ret
    ret%id = id
    ret%wt = wt
  end

  subroutine swapi(a,b)
    type(item), intent(inout) :: a, b
    type(item) :: c
    c = a
    a = b
    b = c
  end

  subroutine appendi(a)
    type(item), pointer, intent(inout) :: a(:)
    integer :: n
    type(item), allocatable :: tmp(:)
    n = size(a)
    allocate(tmp(n))
    tmp = a
    deallocate(a)
    allocate(a(2*n))
    a(1:n) = tmp
    deallocate(tmp)
  end

  function lessi(a,b) result(ret)
    type(item), intent(in) :: a, b
    logical :: ret
    ret = a%wt < b%wt
  end

  subroutine finalizei(pq)
    type(priorityqueue), intent(inout) :: pq
    if (associated(pq%heap)) deallocate(pq%heap)
    pq%num = 0
  end

  subroutine offer(pq,it)
    type(priorityqueue), intent(inout) :: pq
    type(item), intent(in) :: it
    integer :: i, j
    if (.not.associated(pq%heap)) allocate(pq%heap(1))
    if (pq%num == size(pq%heap)) call appendi(pq%heap)
    pq%num = pq%num+1
    pq%heap(pq%num) = it
    i = pq%num
    do while (i > 1)
      j = i/2
      if (lessi(pq%heap(i),pq%heap(j))) call swapi(pq%heap(i),pq%heap(j))
      i = j
    end do
  end

  function poll(pq) result(ret)
    type(priorityqueue), intent(inout) :: pq
    type(item) :: ret
    integer :: n, i, j
    n = pq%num
    ret = pq%heap(1)
    pq%heap(1) = pq%heap(n)
    pq%num = pq%num-1
    i = 1
    do while (2*i < n)
      j = 2*i
      if (j+1 < n .and. lessi(pq%heap(j+1),pq%heap(j))) j = j+1
      if (lessi(pq%heap(j),pq%heap(i))) call swapi(pq%heap(j),pq%heap(i))
      i = j
    end do
  end

  function newe(to,wt,fm,cp,rv) result(ret)
    integer, intent(in) :: to
    integer(8), intent(in) :: wt
    integer, intent(in), optional :: fm, cp, rv
    type(edge) :: ret
    if (present(fm)) ret%fm = fm
    ret%to = to
    ret%wt = wt
    if (present(cp) .and. present(rv)) then
      ret%cp = cp
      ret%rv = rv
    end if
  end

  subroutine appende(a)
    type(edge), pointer, intent(inout) :: a(:)
    integer :: n
    type(edge), allocatable :: tmp(:)
    n = size(a)
    allocate(tmp(n))
    tmp = a
    deallocate(a)
    allocate(a(2*n))
    a(1:n) = tmp
    deallocate(tmp)
  end

  function lesse(a,b) result(ret)
    type(edge), intent(in) :: a, b
    logical :: ret
    ret = a%wt < b%wt
  end

  subroutine finalizee(list)
    type(arraylist), intent(inout) :: list
    if (associated(list%arr)) deallocate(list%arr)
    list%num = 0
  end

  subroutine adde(list,e)
    class(arraylist), intent(inout) :: list
    type(edge), intent(in) :: e
    if (.not.associated(list%arr)) allocate(list%arr(1))
    if (list%num == size(list%arr)) call appende(list%arr)
    list%num = list%num+1
    list%arr(list%num) = e
  end

  function newg(n) result(ret)
    integer, intent(in) :: n
    type(graph) :: ret
    allocate(ret%egs(n))
  end

  subroutine add(g,fm,to,wt,cp)
    class(graph), intent(inout) :: g
    integer, intent(in) :: fm, to
    integer, intent(in), optional :: cp
    integer(8) :: wt
    if (present(cp)) then
      call adde(g%egs(fm),newe(to,wt,cp=cp,rv=g%egs(to)%num))
      call adde(g%egs(to),newe(fm,wt,cp=0,rv=g%egs(fm)%num-1))
    else
      call adde(g%egs(fm),newe(to,wt))
    end if
  end

  subroutine finalize(g)
    type(graph), intent(inout) :: g
    integer :: i
    if (associated(g%used)) deallocate(g%used)
    if (.not.associated(g%egs)) return
    do i = 1, size(g%egs)
      call finalizee(g%egs(i))
    end do
    deallocate(g%egs)
  end

  function dijkstra(g,s) result(ret)
    class(graph), intent(in) :: g
    integer, intent(in) :: s
    integer(8) :: ret(size(g%egs))
    type(priorityqueue) :: pq
    type(item) :: it
    type(edge) :: e
    integer :: i
    ret = inf
    ret(s) = 0_8
    call offer(pq,newi(s,ret(s)))
    do while (pq%num > 0)
      it = poll(pq)
      if (it%wt > ret(it%id)) cycle
      do i = 1, g%egs(it%id)%num
        e = g%egs(it%id)%arr(i)
        if (ret(e%to) > ret(it%id)+e%wt) then
          ret(e%to) = ret(it%id)+e%wt
          call offer(pq,newi(e%to,ret(e%to)))
        end if
      end do
    end do
    call finalizei(pq)
  end

  recursive subroutine dfs_bf(g,u)
    type(graph), intent(inout) :: g
    integer, intent(in) :: u
    integer :: i, v
    g%used(u) = .true.
    do i = 1, g%egs(u)%num
      v = g%egs(u)%arr(i)%to
      if (g%used(v)) cycle
      call dfs_bf(g,v)
    end do
  end

  function bellman_ford(es,s,t) result(ret)
    class(arraylist), intent(in) :: es
    integer, intent(in) :: s, t
    integer(8) :: ret
    integer :: n, i, fm, to, step
    integer(8) :: wt
    logical :: updated
    integer(8), allocatable :: tmp(:)
    type(graph) :: gs, gt
    type(arraylist) :: reach
    n = 0
    do i = 1, es%num
      n = max(n,es%arr(i)%fm,es%arr(i)%to)
    end do
    gs = newg(n)
    gt = newg(n)
    allocate(gs%used(n),gt%used(n))
    gs%used = .false.
    gt%used = .false.
    do i = 1, es%num
      fm = es%arr(i)%fm
      to = es%arr(i)%to
      wt = es%arr(i)%wt
      call add(gs,fm,to,wt)
      call add(gt,to,fm,wt)
    end do
    call dfs_bf(gs,s)
    call dfs_bf(gt,t)
    n = 0
    do i = 1, es%num
      fm = es%arr(i)%fm
      to = es%arr(i)%to
      if (gs%used(fm) .and. gt%used(fm) .and. gs%used(to) .and. gt%used(to)) then
        call adde(reach,es%arr(i))
        n = max(n,fm,to)
      end if
    end do
    call finalize(gs)
    call finalize(gt)
    allocate(tmp(n))
    tmp = inf
    tmp(s) = 0_8
    step = 0
    updated = .true.
    ret = inf
    do while (updated)
      updated = .false.
      do i = 1, reach%num
        fm = reach%arr(i)%fm
        to = reach%arr(i)%to
        wt = reach%arr(i)%wt
        if (tmp(to) > tmp(fm)+wt) then
          tmp(to) = tmp(fm)+wt
          updated = .true.
        end if
      end do
      step = step+1
      if (step > n) then
        ret = -inf
        exit
      end if
    end do
    if (ret == inf) ret = tmp(t)
    deallocate(tmp)
    call finalizee(reach)
  end

  recursive function dfs_ff(g,u,t,f) result(ret)
    class(graph), intent(inout) :: g
    integer, intent(in) :: u, t, f
    integer :: ret
    integer :: i, v, cp, rv
    ret = f
    if (u == t) return
    g%used(u) = .true.
    do i = 1, g%egs(u)%num
      v = g%egs(u)%arr(i)%to
      cp = g%egs(u)%arr(i)%cp
      if (g%used(v) .or. cp <= 0) cycle
      ret = dfs_ff(g,v,t,min(f,cp))
      rv = g%egs(u)%arr(i)%rv
      if (ret > 0) then
        g%egs(u)%arr(i)%cp = g%egs(u)%arr(i)%cp-ret
        g%egs(v)%arr(rv)%cp = g%egs(v)%arr(rv)%cp+ret
        return
      end if
    end do
    ret = 0
  end

  function ford_fulkerson(g,s,t) result(ret)
    class(graph), intent(inout) :: g
    integer, intent(in) :: s, t
    integer :: ret
    integer :: f
    ret = 0
    if (associated(g%used) .and. size(g%used) /= size(g%egs)) deallocate(g%used)
    if (.not.associated(g%used)) allocate(g%used(size(g%egs)))
    do
      g%used = .false.
      f = dfs_ff(g,s,t,1000000000)
      if (f == 0) then
        deallocate(g%used)
        return
      end if
      ret = ret+f
    end do
  end

end module mod_graph

program black_and_white_tree
  use mod_graph
  implicit none
  type(graph) :: g
  integer :: n, a, b, i
  read(*,*) n
  g = graph(n)
  do i = 1, n-1
    read(*,*) a, b
    call g%add(a,b,0_8)
    call g%add(b,a,0_8)
  end do
  if (dfs(1,-1) == 0) then
    write(*,'(a)') "Second"
  else
    write(*,'(a)') "First"
  end if
contains
  recursive function dfs(u,p) result(ret)
    integer, intent(in) :: u, p
    integer :: ret, tmp, i, v
    ret = 0
    tmp = 0
    do i = 1, g%egs(u)%num
      v = g%egs(u)%arr(i)%to
      if (v == p) cycle
      tmp = tmp+dfs(v,u)
    end do
    if (tmp >= 2) then
      write(*,'(a)') "First"
      stop
    end if
    if (tmp == 0) ret = 1
  end
end program black_and_white_tree

Score : 900 points

Problem Statement

There is a tree with N vertices numbered 1 through N.
The i-th of the N-1 edges connects vertices a_i and b_i.

Initially, each vertex is uncolored.

Takahashi and Aoki is playing a game by painting the vertices. In this game, they alternately perform the following operation, starting from Takahashi:

Select a vertex that is not painted yet.

If it is Takahashi who is performing this operation, paint the vertex white; paint it black if it is Aoki.

Then, after all the vertices are colored, the following procedure takes place:

Repaint every white vertex that is adjacent to a black vertex, in black.

Note that all such white vertices are repainted simultaneously, not one at a time.

If there are still one or more white vertices remaining, Takahashi wins; if all the vertices are now black, Aoki wins.
Determine the winner of the game, assuming that both persons play optimally.

Constraints

2 ≤ N ≤ 10^5

1 ≤ a_i,b_i ≤ N

a_i ≠ b_i

The input graph is a tree.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
:
a_{N-1} b_{N-1}

Output

Print First if Takahashi wins; print Second if Aoki wins.

Sample Input 1

3
1 2
2 3

Sample Output 1

First

Below is a possible progress of the game:

First, Takahashi paint vertex 2 white.

Then, Aoki paint vertex 1 black.

Lastly, Takahashi paint vertex 3 white.

In this case, the colors of vertices 1, 2 and 3 after the final procedure are black, black and white, resulting in Takahashi's victory.

Sample Input 2

4
1 2
2 3
2 4

Sample Output 2

First

Sample Input 3

6
1 2
2 3
3 4
2 5
5 6

Sample Output 3

Second


Score : 100 points

Problem Statement

You are given three strings A, B and C. Check whether they form a word chain.

More formally, determine whether both of the following are true:

The last character in A and the initial character in B are the same.

The last character in B and the initial character in C are the same.

If both are true, print YES. Otherwise, print NO.

Constraints

A, B and C are all composed of lowercase English letters (a - z).

1 ≤ |A|, |B|, |C| ≤ 10, where |A|, |B| and |C| are the lengths of A, B and C, respectively.

Input

Input is given from Standard Input in the following format:

A B C

Output

Print YES or NO.

Sample Input 1

rng gorilla apple

Sample Output 1

YES

They form a word chain.

Sample Input 2

yakiniku unagi sushi

Sample Output 2

NO

A and B form a word chain, but B and C do not.

Sample Input 3

a a a

Sample Output 3

YES

Sample Input 4

aaaaaaaaab aaaaaaaaaa aaaaaaaaab

Sample Output 4

NO


program main
   integer(8) N, Time, total, i
   integer(8), allocatable :: t(:)
   read(*, *) N, Time
   allocate(t(1: N+1))
   read(*, *) t(1: N)
   t(N+1) = t(N) + Time
   total = 0
   do i = 1, N
       if( t(i+1)-t(i) <= Time) then
	       total = total + (t(i+1)-t(i))
	   else
	       total = total + Time
	   end if
   end do
   write(*, *) total
end program main

Score : 300 points

Problem Statement

In a public bath, there is a shower which emits water for T seconds when the switch is pushed.

If the switch is pushed when the shower is already emitting water, from that moment it will be emitting water for T seconds.
Note that it does not mean that the shower emits water for T additional seconds.

N people will push the switch while passing by the shower.
The i-th person will push the switch t_i seconds after the first person pushes it.

How long will the shower emit water in total?

Constraints

1 ≤ N ≤ 200,000

1 ≤ T ≤ 10^9

0 = t_1 < t_2 < t_3 < , ..., < t_{N-1} < t_N ≤ 10^9

T and each t_i are integers.

Input

Input is given from Standard Input in the following format:

N T
t_1 t_2 ... t_N

Output

Assume that the shower will emit water for a total of X seconds. Print X.

Sample Input 1

2 4
0 3

Sample Output 1

7

Three seconds after the first person pushes the water, the switch is pushed again and the shower emits water for four more seconds, for a total of seven seconds.

Sample Input 2

2 4
0 5

Sample Output 2

8

One second after the shower stops emission of water triggered by the first person, the switch is pushed again.

Sample Input 3

4 1000000000
0 1000 1000000 1000000000

Sample Output 3

2000000000

Sample Input 4

1 1
0

Sample Output 4

1

Sample Input 5

9 10
0 3 5 7 100 110 200 300 311

Sample Output 5

67


program main
   integer(8) N, Time, total, i
   integer(8), allocatable :: t(:)
   read(*, *) N, Time
   allocate(t(1: N))
   read(*, *) t(1: N)
   total = 0
   do i = 1, N
       if( t(i+1)-t(i) <= Time) then
	       total = total + t(i+1)-t(i)
	   else
	       total = total + Time
	   end if
   end do
   write(*, *) total
end program main


program ARC073D
    implicit none
    integer(8)::N,W,i,j,k,l,a,b,c,d,Vall(100,4),wi,w1,vi,vcount(4),ans,wall,v
    read*,N,W
    vcount=0
    Vall=0
    ans=0
    do i=1,N
        read*,wi,vi
        if(i==1)w1=wi
        vcount(wi-w1+1)=vcount(wi-w1+1)+1
        Vall(vcount(wi-w1+1),wi-w1+1)=vi
    end do

    do i=1,4
        call heapsort(100,Vall(:,i))
    end do

    do i=0,vcount(1)
        do j=0,vcount(2)
            do k=0,vcount(3)
                do l=0,vcount(4)
                    wall=i*w1+j*(w1+1)+k*(w1+2)+l*(w1+3)
                    if(wall<=W)then
                        v=0
                        do a=0,i-1
                            v=v+Vall(100-a,1)
                        end do

                        do b=0,j-1
                            v=v+Vall(100-b,2)
                        end do

                        do c=0,k-1
                            v=v+Vall(100-c,3)
                        end do

                        do d=0,l-1
                            v=v+Vall(100-d,4)
                        end do
                        ans=max(ans,v)
                    end if
                end do
            end do
        end do
    end do

    print'(i0)',ans
end program ARC073D

subroutine heapsort(n,array)
    implicit none
    integer,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

You have N items and a bag of strength W.
The i-th item has a weight of w_i and a value of v_i.

You will select some of the items and put them in the bag.
Here, the total weight of the selected items needs to be at most W.

Your objective is to maximize the total value of the selected items.

Constraints

1 ≤ N ≤ 100

1 ≤ W ≤ 10^9

1 ≤ w_i ≤ 10^9

For each i = 2,3,...,N, w_1 ≤ w_i ≤ w_1 + 3.

1 ≤ v_i ≤ 10^7

W, each w_i and v_i are integers.

Input

Input is given from Standard Input in the following format:

N W
w_1 v_1
w_2 v_2
:
w_N v_N

Output

Print the maximum possible total value of the selected items.

Sample Input 1

4 6
2 1
3 4
4 10
3 4

Sample Output 1

11

The first and third items should be selected.

Sample Input 2

4 6
2 1
3 7
4 10
3 6

Sample Output 2

13

The second and fourth items should be selected.

Sample Input 3

4 10
1 100
1 100
1 100
1 100

Sample Output 3

400

You can take everything.

Sample Input 4

4 1
10 100
10 100
10 100
10 100

Sample Output 4

0

You can take nothing.


module mod_segment_tree
  implicit none
  integer(8), parameter :: inf = 100000000000_8
  type t_segment_tree
    private
    integer(8) :: deflt = inf
    integer :: n, p
    integer(8), pointer :: arr(:) => null()
  contains
    procedure :: init => init_segtree
    procedure :: release => release_segtree
    procedure :: update => update
    procedure :: query => query
    procedure :: default => set_default
    procedure :: change => change_default
  end type
contains
  integer function op(x,y)
    integer(8), intent(in) :: x, y
    op = min(x,y)
  end
  subroutine init_segtree(this,n)
    class(t_segment_tree), intent(inout) :: this
    integer, intent(in) :: n
    integer :: p
    p = 1
    do while (p < n)
      p = 2*p
    end do
    this%n = n
    this%p = p
    allocate(this%arr(2*p-1))
    this%arr = this%deflt
  end
  subroutine release_segtree(this)
    class(t_segment_tree), intent(inout) :: this
    if (associated(this%arr)) deallocate(this%arr)
  end
  subroutine set_default(this,i)
    class(t_segment_tree), intent(inout) :: this
    integer, intent(in) :: i
    integer :: x
    x = i+this%p-1
    this%arr(x) = this%deflt
    do while (x > 1)
      x = x/2
      this%arr(x) = op(this%arr(2*x),this%arr(2*x+1))
    end do
  end
  subroutine change_default(this,deflt)
    class(t_segment_tree), intent(inout) :: this
    integer(8), intent(in) :: deflt
    this%deflt = deflt
  end
  subroutine update(this,i,v)
    class(t_segment_tree), intent(inout) :: this
    integer, intent(in) :: i
    integer(8), intent(in) :: v
    integer :: x
    x = i+this%p-1
    this%arr(x) = v
    do while (x > 1)
      x = x/2
      this%arr(x) = op(this%arr(2*x),this%arr(2*x+1))
    end do
  end
  integer(8) function query(this,a,b)
    class(t_segment_tree), intent(inout) :: this
    integer, intent(in) :: a, b
    integer :: l, r
    query = this%deflt
    l = a+this%p-1
    r = b+this%p-1
    do while (l <= r)
      if (mod(l,2) == 1) then
        query = op(query,this%arr(l))
        l = l+1
      end if
      if (mod(r,2) == 0) then
        query = op(query,this%arr(r))
        r = r-1
      end if
      l = l/2
      r = r/2
    end do
  end
end module mod_segment_tree
program many_moves
  use mod_segment_tree
  implicit none
  type(t_segment_tree) :: plus, minus
  integer :: n, q, a, b, x(200000) = 0, i, j
  integer(8) :: ans = 1000000_8*inf, offset = 0_8, tmp, v1, v2
  read(*,*) n, q, a, b
  read(*,*) x(1:q)
  call plus%init(n)
  call minus%init(n)
  call set(a,0_8)
  do i = 1, q
    tmp = int(abs(x(i)-b),8)
    v1 = rmq(x(i))
    v2 = get(b)+tmp
    call set(b,min(v1,v2)-tmp)
    offset = offset+tmp
    b = x(i)
  end do
  do i = 1, n
    ans = min(ans,get(i))
  end do
  write(*,'(i0)') ans
contains
  subroutine set(i,v)
    integer, intent(in) :: i
    integer(8), intent(in) :: v
    call plus%update(i,v+int(i,8))
    call minus%update(i,v-int(i,8))
  end
  integer(8) function get(i)
    integer, intent(in) :: i
    get = plus%query(i,i)-int(i,8)+offset
  end
  integer(8) function rmq(i)
    integer, intent(in) :: i
    rmq = min(minus%query(1,i-1)+int(i,8),plus%query(i,n)-int(i,8))
  end
end program many_moves

Score : 900 points

Problem Statement

There are N squares in a row. The squares are numbered 1, 2, ..., N from left to right.

You have two pieces, initially placed on square A and B, respectively.
You will be asked to process Q queries of the following kind, in the order received:

Given an integer x_i, move one of the two pieces of your choice to square x_i.

Here, it takes you one second to move a piece one square.
That is, the time it takes to move a piece from square X to Y is |X-Y| seconds.

Your objective is to process all the queries in the shortest possible time.

You may only move the pieces in response to queries, and you may not move both pieces at the same time.
Also, it is not allowed to rearrange the order in which queries are given.
It is, however, allowed to have both pieces in the same square at the same time.

Constraints

1 ≤ N, Q ≤ 200,000

1 ≤ A, B ≤ N

1 ≤ x_i ≤ N

Input

Input is given from Standard Input in the following format:

N Q A B
x_1 x_2 ... x_Q

Output

Let the shortest possible time to process all the queries be X seconds. Print X.

Sample Input 1

8 3 1 8
3 5 1

Sample Output 1

7

All the queries can be processed in seven seconds, by:

moving the piece at square 1 to 3

moving the piece at square 8 to 5

moving the piece at square 3 to 1

Sample Input 2

9 2 1 9
5 1

Sample Output 2

4

The piece at square 9 should be moved first.

Sample Input 3

9 2 1 9
5 9

Sample Output 3

4

The piece at square 1 should be moved first.

Sample Input 4

11 16 8 1
1 1 5 1 11 4 5 2 5 3 3 3 5 5 6 7

Sample Output 4

21