module merge_sort_mod
    use,intrinsic :: iso_fortran_env
    implicit none
    private
    public:: merge_sort, double_merge_sort
    interface merge_sort
        module procedure ms32, ms64
    end interface

    interface double_merge_sort
        module procedure msd3232, msd6464
    end interface

contains
    recursive subroutine ms32(ar, fst, lst)
        integer(int32),intent(inout):: ar(:)
        integer(int32),intent(in):: fst,lst
        integer(int32):: mdl

        if (lst-fst < 2) then
            if (ar(fst) > ar(lst)) call swap32(ar(fst),ar(lst))
            return
        end if

        mdl = (fst+lst)/2
        call ms32(ar, fst, mdl)
        call ms32(ar, mdl+1, lst)
        call merge32(ar, fst, mdl, lst)
    end subroutine

    subroutine merge32(ar, fst, mdl, lst)
        integer(int32),intent(inout):: ar(:)
        integer(int32),intent(in):: fst, mdl, lst
        integer(int32),allocatable:: tmp(:)
        integer(int32):: li, ri, ti

        allocate(tmp(lst-fst+1))

        li=fst
        ri=mdl+1
        ti=1

        do while (li <= mdl .and. ri <= lst)
            if (ar(li) <= ar(ri)) then
                tmp(ti) = ar(li)
                li=li+1
            else
                tmp(ti) = ar(ri)
                ri=ri+1
            end if
            ti=ti+1
        end do

        if (li <= mdl) then
            tmp(ti:) = ar(li:mdl)
        else
            tmp(ti:) = ar(ri:lst)
        end if

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

    subroutine swap32(x,y)
        integer(int32),intent(inout):: x,y
        integer(int32):: tmp
        tmp = x
        x = y
        y = tmp
    end subroutine

    recursive subroutine ms64(ar, fst, lst)
        integer(int64),intent(inout):: ar(:)
        integer(int64),intent(in):: fst,lst
        integer(int64):: mdl

        if (lst-fst < 2) then
            if (ar(fst) > ar(lst)) call swap64(ar(fst),ar(lst))
            return
        end if

        mdl = (fst+lst)/2
        call ms64(ar, fst, mdl)
        call ms64(ar, mdl+1, lst)
        call merge64(ar, fst, mdl, lst)
    end subroutine

    subroutine merge64(ar, fst, mdl, lst)
        integer(int64),intent(inout):: ar(:)
        integer(int64),intent(in):: fst, mdl, lst
        integer(int64),allocatable:: tmp(:)
        integer(int64):: li, ri, ti

        allocate(tmp(lst-fst+1))

        li=fst
        ri=mdl+1
        ti=1

        do while (li <= mdl .and. ri <= lst)
            if (ar(li) <= ar(ri)) then
                tmp(ti) = ar(li)
                li=li+1
            else
                tmp(ti) = ar(ri)
                ri=ri+1
            end if
            ti=ti+1
        end do

        if (li <= mdl) then
            tmp(ti:) = ar(li:mdl)
        else
            tmp(ti:) = ar(ri:lst)
        end if

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

    subroutine swap64(x,y)
        integer(int64),intent(inout):: x,y
        integer(int64):: tmp
        tmp = x
        x = y
        y = tmp
    end subroutine

    recursive subroutine msd3232(ar1, ar2, fst, lst)
        integer(int32),intent(inout):: ar1(:),ar2(:)
        integer(int32),intent(in):: fst,lst
        integer(int32):: mdl

        if (lst - fst < 2) then
            if (ar1(fst) > ar1(lst)) then
                call swap32(ar1(fst), ar1(lst))
                call swap32(ar2(fst), ar2(lst))
            end if
            return
        end if

        mdl = (fst+lst)/2

        call msd3232(ar1,ar2,fst,mdl)
        call msd3232(ar1,ar2,mdl+1,lst)
        call merged3232(ar1,ar2,fst,mdl,lst)
    end subroutine

    subroutine merged3232(ar1,ar2,fst,mdl,lst)
        integer(int32),intent(inout):: ar1(:),ar2(:)
        integer(int32),intent(in):: fst,mdl,lst
        integer(int32),allocatable:: t1(:),t2(:)
        integer(int32):: li,ri,ti

        allocate(t1(lst-fst+1), t2(lst-fst+1))

        li=fst
        ri=mdl+1
        ti=1

        do while(li <= mdl .and. ri <= lst)
            if (ar1(li) <= ar1(ri)) then
                t1(ti) = ar1(li)
                t2(ti) = ar2(li)
                li=li+1
            else
                t1(ti) = ar1(ri)
                t2(ti) = ar2(ri)
                ri=ri+1
            end if
            ti=ti+1
        end do

        if (li <= mdl) then
            t1(ti:) = ar1(li:mdl)
            t2(ti:) = ar2(li:mdl)
        else
            t1(ti:) = ar1(ri:lst)
            t2(ti:) = ar2(ri:lst)
        end if

        ar1(fst:lst) = t1(:)
        ar2(fst:lst) = t2(:)

        deallocate(t1,t2)
    end subroutine

    recursive subroutine msd6464(ar1, ar2, fst, lst)
        integer(int64),intent(inout):: ar1(:),ar2(:)
        integer(int64),intent(in):: fst,lst
        integer(int64):: mdl

        if (lst - fst < 2) then
            if (ar1(fst) > ar1(lst)) then
                call swap64(ar1(fst), ar1(lst))
                call swap64(ar2(fst), ar2(lst))
            end if
            return
        end if

        mdl = (fst+lst)/2

        call msd6464(ar1,ar2,fst,mdl)
        call msd6464(ar1,ar2,mdl+1,lst)
        call merged6464(ar1,ar2,fst,mdl,lst)
    end subroutine

    subroutine merged6464(ar1,ar2,fst,mdl,lst)
        integer(int64),intent(inout):: ar1(:),ar2(:)
        integer(int64),intent(in):: fst,mdl,lst
        integer(int64),allocatable:: t1(:),t2(:)
        integer(int64):: li,ri,ti

        allocate(t1(lst-fst+1), t2(lst-fst+1))

        li=fst
        ri=mdl+1
        ti=1

        do while(li <= mdl .and. ri <= lst)
            if (ar1(li) <= ar1(ri)) then
                t1(ti) = ar1(li)
                t2(ti) = ar2(li)
                li=li+1
            else
                t1(ti) = ar1(ri)
                t2(ti) = ar2(ri)
                ri=ri+1
            end if
            ti=ti+1
        end do

        if (li <= mdl) then
            t1(ti:) = ar1(li:mdl)
            t2(ti:) = ar2(li:mdl)
        else
            t1(ti:) = ar1(ri:lst)
            t2(ti:) = ar2(ri:lst)
        end if

        ar1(fst:lst) = t1(:)
        ar2(fst:lst) = t2(:)

        deallocate(t1,t2)
    end subroutine
end module

program agc023_a
    use,intrinsic :: iso_fortran_env
    use merge_sort_mod
    implicit none
    integer(int64):: n,i,c,ans
    integer(int64), allocatable:: a(:),s(:)

    read*, n
    allocate(a(n),s(0:n))
    read*, a(:)
    s(:) = 0
    do i=1,n
        s(i) = s(i-1)+a(i)
    end do

    call merge_sort(s,1_8,n+1)
    ans=0
    c=1
    do i=1,n
        if (s(i) == s(i-1)) then
            c=c+1
        else
            ans=ans+(c*(c-1))/2
            c=1
        end if
    end do
    ans=ans+(c*(c-1))/2
    print'(i0)', ans
end program agc023_a

Score : 200 points

Problem Statement

We have an integer sequence A, whose length is N.

Find the number of the non-empty contiguous subsequences of A whose sums are 0.
Note that we are counting the ways to take out subsequences.
That is, even if the contents of some two subsequences are the same, they are counted individually if they are taken from different positions.

Constraints

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

-10^9 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Find the number of the non-empty contiguous subsequences of A whose sum is 0.

Sample Input 1

6
1 3 -4 2 2 -2

Sample Output 1

3

There are three contiguous subsequences whose sums are 0: (1,3,-4), (-4,2,2) and (2,-2).

Sample Input 2

7
1 -1 1 -1 1 -1 1

Sample Output 2

12

In this case, some subsequences that have the same contents but are taken from different positions are counted individually.
For example, three occurrences of (1, -1) are counted.

Sample Input 3

5
1 -2 3 -4 5

Sample Output 3

0

There are no contiguous subsequences whose sums are 0.


program Half_and_Half
    implicit none
    integer(4):: a,b,c
    integer(4):: x,y

    read*, a,b,c,x,y

    print*, min(x,y) * min(a+b, 2*c) + min(max((x-y)*a,(y-x)*b), abs(x-y)*2*c)

end program Half_and_Half

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

Score : 300 points

Problem Statement

"Pizza At", a fast food chain, offers three kinds of pizza: "A-pizza", "B-pizza" and "AB-pizza". A-pizza and B-pizza are completely different pizzas, and AB-pizza is one half of A-pizza and one half of B-pizza combined together. The prices of one A-pizza, B-pizza and AB-pizza are A yen, B yen and C yen (yen is the currency of Japan), respectively.

Nakahashi needs to prepare X A-pizzas and Y B-pizzas for a party tonight. He can only obtain these pizzas by directly buying A-pizzas and B-pizzas, or buying two AB-pizzas and then rearrange them into one A-pizza and one B-pizza. At least how much money does he need for this? It is fine to have more pizzas than necessary by rearranging pizzas.

Constraints

1 ≤ A, B, C ≤ 5000

1 ≤ X, Y ≤ 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C X Y

Output

Print the minimum amount of money required to prepare X A-pizzas and Y B-pizzas.

Sample Input 1

1500 2000 1600 3 2

Sample Output 1

7900

It is optimal to buy four AB-pizzas and rearrange them into two A-pizzas and two B-pizzas, then buy additional one A-pizza.

Sample Input 2

1500 2000 1900 3 2

Sample Output 2

8500

It is optimal to directly buy three A-pizzas and two B-pizzas.

Sample Input 3

1500 2000 500 90000 100000

Sample Output 3

100000000

It is optimal to buy 200000 AB-pizzas and rearrange them into 100000 A-pizzas and 100000 B-pizzas. We will have 10000 more A-pizzas than necessary, but that is fine.

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 probC
  implicit none
  integer a,b,c,x,y,ans,tmp

  read(*,*) a,b,c,x,y

  ans = 0
  if(a+b>=2*c) then
     tmp = min(x,y)
     ans = tmp*2*c
     x = x - tmp
     y = y - tmp

  end if

  if ( a>=2*c .and. x.ne.0) then
     ans = ans + x*2*c
     x = 0

  end if
  if ( b>=2*c .and. y.ne.0) then
     ans = ans + y*2*c
     y = 0

  end if

     ans = ans + x*a + y*b

  write (*,*) ans

end program probC


program test
implicit none
integer :: i,j,n
integer(8) :: c,x(100001),v(100001),profit(100001)=0,profit2(100001)=0,max_value=0,max_value2=0

read(*,*) n,c
do i = 1,n
read(*,*) x(i),v(i)
enddo

!clockwise_start

do j = n, 1, -1
  profit(j) = sum(v(j:n))-(c-x(j))
  if(j < n) profit(j) = max(profit(j),profit(j+1))
enddo

do i = 1, n
  profit2(i) = sum(v(1:i)) - 2 * x(i) + max(x(i),profit(i+1))
  max_value = max(max_value,profit2(i))
  enddo

!counterclockwise_start

do i = 1, n
  profit(i) = sum(v(1:i))-x(i)
  if(i >= 2) profit(i) = max(profit(i),profit(i-1))
enddo

do j = 1, n
  profit2(j) = sum(v(j:n))- 2 * (c-x(j)) + max((c-x(j)),profit(j-1))
  max_value2 = max(max_value2,profit2(j))
enddo

write(*,*) max(max_value,max_value2)

end program

Score : 500 points

Problem Statement

"Teishi-zushi", a Japanese restaurant, is a plain restaurant with only one round counter. The outer circumference of the counter is C meters. Customers cannot go inside the counter.

Nakahashi entered Teishi-zushi, and he was guided to the counter. Now, there are N pieces of sushi (vinegared rice with seafood and so on) on the counter. The distance measured clockwise from the point where Nakahashi is standing to the point where the i-th sushi is placed, is x_i meters. Also, the i-th sushi has a nutritive value of v_i kilocalories.

Nakahashi can freely walk around the circumference of the counter. When he reach a point where a sushi is placed, he can eat that sushi and take in its nutrition (naturally, the sushi disappears). However, while walking, he consumes 1 kilocalories per meter.

Whenever he is satisfied, he can leave the restaurant from any place (he does not have to return to the initial place). On balance, at most how much nutrition can he take in before he leaves? That is, what is the maximum possible value of the total nutrition taken in minus the total energy consumed? Assume that there are no other customers, and no new sushi will be added to the counter. Also, since Nakahashi has plenty of nutrition in his body, assume that no matter how much he walks and consumes energy, he never dies from hunger.

Constraints

1 ≤ N ≤ 10^5

2 ≤ C ≤ 10^{14}

1 ≤ x_1 < x_2 < ... < x_N < C

1 ≤ v_i ≤ 10^9

All values in input are integers.

Subscores

300 points will be awarded for passing the test set satisfying N ≤ 100.

Input

Input is given from Standard Input in the following format:

N C
x_1 v_1
x_2 v_2
:
x_N v_N

Output

If Nakahashi can take in at most c kilocalories on balance before he leaves the restaurant, print c.

Sample Input 1

3 20
2 80
9 120
16 1

Sample Output 1

191

There are three sushi on the counter with a circumference of 20 meters. If he walks two meters clockwise from the initial place, he can eat a sushi of 80 kilocalories. If he walks seven more meters clockwise, he can eat a sushi of 120 kilocalories. If he leaves now, the total nutrition taken in is 200 kilocalories, and the total energy consumed is 9 kilocalories, thus he can take in 191 kilocalories on balance, which is the largest possible value.

Sample Input 2

3 20
2 80
9 1
16 120

Sample Output 2

192

The second and third sushi have been swapped. Again, if he walks two meters clockwise from the initial place, he can eat a sushi of 80 kilocalories. If he walks six more meters counterclockwise this time, he can eat a sushi of 120 kilocalories. If he leaves now, the total nutrition taken in is 200 kilocalories, and the total energy consumed is 8 kilocalories, thus he can take in 192 kilocalories on balance, which is the largest possible value.

Sample Input 3

1 100000000000000
50000000000000 1

Sample Output 3

0

Even though the only sushi is so far that it does not fit into a 32-bit integer, its nutritive value is low, thus he should immediately leave without doing anything.

Sample Input 4

15 10000000000
400000000 1000000000
800000000 1000000000
1900000000 1000000000
2400000000 1000000000
2900000000 1000000000
3300000000 1000000000
3700000000 1000000000
3800000000 1000000000
4000000000 1000000000
4100000000 1000000000
5200000000 1000000000
6600000000 1000000000
8000000000 1000000000
9300000000 1000000000
9700000000 1000000000

Sample Output 4

6500000000

All these sample inputs above are included in the test set for the partial score.


program test
implicit none
integer :: i,j,n
integer(8) :: c,x(100000),v(100000),tokei,hantokei(100000),goukei(100000)=0,saikou

read(*,*) n,c
do i = 1,n
read(*,*) x(i),v(i)
enddo

do i = 0, n

if(i == 0) then
  tokei = 0
  hantokei = 0
else
tokei = sum(v(1:i)) - x(i)
hantokei = 0
endif

do j = i+1, n
hantokei(j) = sum(v(j:n)) - (x(i) + c - x(j))
enddo

goukei(i) = tokei +  maxval(hantokei(:))

enddo

saikou = maxval(goukei(:))

do i = 1, n

tokei = sum(v(i:n)) - ( c - x(i))
hantokei = 0

do j = 1, i-1
if(j>=1) then
hantokei(j) = sum(v(1:j)) - (c - x(i) + x(j))
endif
enddo

goukei(i) = tokei +  maxval(hantokei(:))

enddo

write(*,*) max(saikou,maxval(goukei(:)))

end program


integer a,b,c
read*, a,b,c
if(a<=c .and. c<=a+b)then
	print*, "YES"
else
	print*, "NO"
end if
end

Score : 100 points

Problem Statement

There are a total of A + B cats and dogs.
Among them, A are known to be cats, but the remaining B are not known to be either cats or dogs.

Determine if it is possible that there are exactly X cats among these A + B animals.

Constraints

1 \leq A \leq 100

1 \leq B \leq 100

1 \leq X \leq 200

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B X

Output

If it is possible that there are exactly X cats, print YES; if it is impossible, print NO.

Sample Input 1

3 5 4

Sample Output 1

YES

If there are one cat and four dogs among the B = 5 animals, there are X = 4 cats in total.

Sample Input 2

2 2 6

Sample Output 2

NO

Even if all of the B = 2 animals are cats, there are less than X = 6 cats in total.

Sample Input 3

5 3 2

Sample Output 3

NO

Even if all of the B = 3 animals are dogs, there are more than X = 2 cats in total.


program ABC_094_B_TollGates
   implicit none
   integer N, M, X, i, countN
   integer, allocatable :: A(:)
   read(*, *) N, M, X
   allocate(A(1: M))
   read(*, *) A(1: M)
   countN = 0
   do i = 1, M
       if(X > A(i)) then
	      countN = countN + 1
	   end if
   end do
   write(*, *) min(countN, M - countN)
end program ABC_094_B_TollGates

Score : 200 points

Problem Statement

There are N + 1 squares arranged in a row, numbered 0, 1, ..., N from left to right.

Initially, you are in Square X.
You can freely travel between adjacent squares. Your goal is to reach Square 0 or Square N.
However, for each i = 1, 2, ..., M, there is a toll gate in Square A_i, and traveling to Square A_i incurs a cost of 1.
It is guaranteed that there is no toll gate in Square 0, Square X and Square N.

Find the minimum cost incurred before reaching the goal.

Constraints

1 \leq N \leq 100

1 \leq M \leq 100

1 \leq X \leq N - 1

1 \leq A_1 < A_2 < ... < A_M \leq N

A_i \neq X

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M X
A_1 A_2 ... A_M

Output

Print the minimum cost incurred before reaching the goal.

Sample Input 1

5 3 3
1 2 4

Sample Output 1

1

The optimal solution is as follows:

First, travel from Square 3 to Square 4. Here, there is a toll gate in Square 4, so the cost of 1 is incurred.

Then, travel from Square 4 to Square 5. This time, no cost is incurred.

Now, we are in Square 5 and we have reached the goal.

In this case, the total cost incurred is 1.

Sample Input 2

7 3 2
4 5 6

Sample Output 2

0

We may be able to reach the goal at no cost.

Sample Input 3

10 7 5
1 2 3 4 6 8 9

Sample Output 3

3


program ABC_094_D_BinomialCoefficients
   implicit none
   integer(8) n, l, r, i
   integer(8), allocatable :: a(:)
   read(*, *) n
   allocate(a(1: n))
   read(*, *) a(1: n)
   l = maxval(a(1:n))
   r = a(1)
   do i = 1, n
       if( abs(dble(l/2) - r) >= abs(dble(l/2) - a(i)) .and. a(i) /= l) then
	       r = a(i)
	   end if
   end do
   write(*, *) l, r
end program ABC_094_D_BinomialCoefficients

Score : 400 points

Problem Statement

Let {\rm comb}(n,r) be the number of ways to choose r objects from among n objects, disregarding order.
From n non-negative integers a_1, a_2, ..., a_n, select two numbers a_i > a_j so that {\rm comb}(a_i,a_j) is maximized.
If there are multiple pairs that maximize the value, any of them is accepted.

Constraints

2 \leq n \leq 10^5

0 \leq a_i \leq 10^9

a_1,a_2,...,a_n are pairwise distinct.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

n
a_1 a_2 ... a_n

Output

Print a_i and a_j that you selected, with a space in between.

Sample Input 1

5
6 9 4 2 11

Sample Output 1

11 6

\rm{comb}(a_i,a_j) for each possible selection is as follows:

\rm{comb}(4,2)=6

\rm{comb}(6,2)=15

\rm{comb}(6,4)=15

\rm{comb}(9,2)=36

\rm{comb}(9,4)=126

\rm{comb}(9,6)=84

\rm{comb}(11,2)=55

\rm{comb}(11,4)=330

\rm{comb}(11,6)=462

\rm{comb}(11,9)=55

Thus, we should print 11 and 6.

Sample Input 2

2
100 0

Sample Output 2

100 0


integer :: piyo(3),i
character(3) :: s
read*,s

piyo = 0
do i = 1,3
  select case( s(i:i) )
  case( 'a' )
    piyo(1) = piyo(1) + 1
  case( 'b' )
    piyo(2) = piyo(2) + 1
  case( 'c' )
    piyo(3) = piyo(3) + 1
  end select
end do

do i = 1,3
  if( piyo(i)/=1 )then
    print*,'No'
    stop
  end if
end do
print*,'Yes'
end

Score : 100 points

Problem Statement

You are given a string S of length 3 consisting of a, b and c. Determine if S can be obtained by permuting abc.

Constraints

|S|=3

S consists of a, b and c.

Input

Input is given from Standard Input in the following format:

S

Output

If S can be obtained by permuting abc, print Yes; otherwise, print No.

Sample Input 1

bac

Sample Output 1

Yes

Swapping the first and second characters in bac results in abc.

Sample Input 2

bab

Sample Output 2

No

Sample Input 3

abc

Sample Output 3

Yes

Sample Input 4

aaa

Sample Output 4

No


program main
  implicit none
  integer :: a, b, k, i
  read(*, *) a, b, k
  if (b - a > 200) then
    do i = 0, k - 1
      write(*, "(i0)") a + i
    end do
    do i = k - 1, 0, -1
      write(*, "(i0)") b - i
    end do
  else
    do i = a, b
      if (i - a < k .or. b - i < k) then
        write(*, "(i0)") i
      end if
    end do
  end if
end program main

Score : 200 points

Problem Statement

Print all the integers that satisfies the following in ascending order:

Among the integers between A and B (inclusive), it is either within the K smallest integers or within the K largest integers.

Constraints

1 \leq A \leq B \leq 10^9

1 \leq K \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B K

Output

Print all the integers that satisfies the condition above in ascending order.

Sample Input 1

3 8 2

Sample Output 1

3
4
7
8

3 is the first smallest integer among the integers between 3 and 8.

4 is the second smallest integer among the integers between 3 and 8.

7 is the second largest integer among the integers between 3 and 8.

8 is the first largest integer among the integers between 3 and 8.

Sample Input 2

4 8 3

Sample Output 2

4
5
6
7
8

Sample Input 3

2 9 100

Sample Output 3

2
3
4
5
6
7
8
9


program ABC_093_B_SmallandLargeIntegers
   implicit none
   integer(8) A, B
   integer K, i
   read(*, *) A, B, K
   if(B-A+1 <= 2 * K) then
       do i = A, B
	       write(*, *) i
	   end do
   else
       do i = A, A + K - 1
          write(*, *) i
       end do
       do i = B - K + 1, B
           write(*, *) i
       end do
   end if
end program ABC_093_B_SmallandLargeIntegers


program name
    implicit none
    character(26):: s
    character(1):: mins
    integer(4):: ls,i,mic, ri
    integer(4),allocatable:: si(:)
    logical:: is_use(26)

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

    do i=1,ls
        si(i) = ichar(s(i:i)) - ichar('a') + 1
    end do

    is_use(:) = .false.

    do i=1,ls
        is_use(si(i)) = .true.
    end do

    mic=10001
    do i=1,26
        if (.not. is_use(i))  mic=min(mic,i)
    end do

    if (mic==10001) then
        ri = 1000
        do i=ls,2,-1
            if (s(i-1:i-1) < s(i:i)) then
                ri = i
                exit
            end if
        end do

        if(ri == 1000) then
            print*, -1
            stop
        end if
        mins='z'
        do i=ri,ls
            if (s(ri-1:ri-1) >= s(i:i)) cycle
            mins = min(s(i:i),mins)
        end do
        print*, s(1:ri-2) // mins
        stop
    end if

    s(ls+1:ls+1) = char(mic+ichar('a')-1)

    print*, trim(s)

end program name

Score : 300 points

Problem Statement

Gotou just received a dictionary. However, he doesn't recognize the language used in the dictionary. He did some analysis on the dictionary and realizes that the dictionary contains all possible diverse words in lexicographical order.

A word is called diverse if and only if it is a nonempty string of English lowercase letters and all letters in the word are distinct. For example, atcoder, zscoder and agc are diverse words while gotou and connect aren't diverse words.

Given a diverse word S, determine the next word that appears after S in the dictionary, i.e. the lexicographically smallest diverse word that is lexicographically larger than S, or determine that it doesn't exist.

Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.

Constraints

1 \leq |S| \leq 26

S is a diverse word.

Input

Input is given from Standard Input in the following format:

S

Output

Print the next word that appears after S in the dictionary, or -1 if it doesn't exist.

Sample Input 1

atcoder

Sample Output 1

atcoderb

atcoderb is the lexicographically smallest diverse word that is lexicographically larger than atcoder. Note that atcoderb is lexicographically smaller than b.

Sample Input 2

abc

Sample Output 2

abcd

Sample Input 3

zyxwvutsrqponmlkjihgfedcba

Sample Output 3

-1

This is the lexicographically largest diverse word, so the answer is -1.

Sample Input 4

abcdefghijklmnopqrstuvwzyx

Sample Output 4

abcdefghijklmnopqrstuvx


program name
    implicit none
    character(26):: s
    integer(4):: ls,i,mic
    integer(4),allocatable:: si(:)
    logical:: is_use(26), f

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

    do i=1,ls
        si(i) = ichar(s(i:i)) - ichar('a') + 1
    end do

    is_use(:) = .false.

    do i=1,ls
        is_use(si(i)) = .true.
    end do

    mic=10001
    do i=1,26
        if (.not. is_use(i))  mic=min(mic,i)
    end do
    f=.false.

    if (mic==10001) then
        do i=ls,2,-1
            if (si(i) > si(i-1)) then
                s(i-1:i-1) = char(ichar(s(i-1:i-1))+1)
                ls=i-1
                f=.true.
                exit
            end if
        end do
        if(f) then
            print*, s(1:ls)
            stop
        else
            print*, -1
            stop
        end if
    end if

    s(ls+1:ls+1) = char(mic+ichar('a')-1)

    print*, trim(s)

end program name


program sample
	integer ::A,B,C,D,e,f

    read(*,*) A
    read(*,*) B
    read(*,*) C
    read(*,*) D

    if (A>B) then
    	e=B
    else if(A<=B) then
    	e=A
    end if
    if(C>D) then
    	f=D
    else if(C<=D) then
    	f=C
    end if
    write(*,*) e+f
    stop
end program sample

Score : 100 points

Problem Statement

You planned a trip using trains and buses.
The train fare will be A yen (the currency of Japan) if you buy ordinary tickets along the way, and B yen if you buy an unlimited ticket.
Similarly, the bus fare will be C yen if you buy ordinary tickets along the way, and D yen if you buy an unlimited ticket.

Find the minimum total fare when the optimal choices are made for trains and buses.

Constraints

1 \leq A \leq 1 000

1 \leq B \leq 1 000

1 \leq C \leq 1 000

1 \leq D \leq 1 000

All input values are integers.

Input

Input is given from Standard Input in the following format:

A
B
C
D

Output

Print the minimum total fare.

Sample Input 1

600
300
220
420

Sample Output 1

520

The train fare will be 600 yen if you buy ordinary tickets, and 300 yen if you buy an unlimited ticket.
Thus, the optimal choice for trains is to buy an unlimited ticket for 300 yen.
On the other hand, the optimal choice for buses is to buy ordinary tickets for 220 yen.

Therefore, the minimum total fare is 300 + 220 = 520 yen.

Sample Input 2

555
555
400
200

Sample Output 2

755

Sample Input 3

549
817
715
603

Sample Output 3

1152


program sample
	integer ::A,B,C,D,e,f

    read(*,*) A
    read(*,*) B
    read(*,*) C
    read(*,*) D

    if (A>B) then
    	e=B
    else if(B<=A) then
    	e=A
    end if
    if(C>D) then
    	f=D
    else if(C<=D) then
    	f=C
    end if
    write(*,*) e+f
    stop
end program sample


program main
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32)::a,b,c,d
    read*, a
    read*, b
    read*, c
    read*, d

    print'(i0)', min(a,b)+min(c,d)
end program main


character(3) :: p,i,y
read*,p
read*,i
read*,y
print*,p(1:1)//i(2:2)//y(3:3)
end

Score : 100 points

Problem Statement

We have a 3×3 square grid, where each square contains a lowercase English letters.
The letter in the square at the i-th row from the top and j-th column from the left is c_{ij}.

Print the string of length 3 that can be obtained by concatenating the letters in the squares on the diagonal connecting the top-left and bottom-right corner of the grid, from the top-left to bottom-right.

Constraints

Input consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

c_{11}c_{12}c_{13}
c_{21}c_{22}c_{23}
c_{31}c_{32}c_{33}

Output

Print the string of length 3 that can be obtained by concatenating the letters on the diagonal connecting the top-left and bottom-right corner of the grid, from the top-left to bottom-right.

Sample Input 1

ant
obe
rec

Sample Output 1

abc

The letters in the squares on the diagonal connecting the top-left and bottom-right corner of the grid are a, b and c from top-right to bottom-left. Concatenate these letters and print abc.

Sample Input 2

edu
cat
ion

Sample Output 2

ean


program main
implicit None
	integer(8)::n,m
	read *,n,m

	if(n*m == 1)then
		print 101,1
		stop
	endif

	if(n == 1 .or. m == 1)then
		print 101,n*m-2
		stop
	endif

	print 101,(n-2)*(m-2)
101 format(i0)
end program main

Score : 300 points

Problem Statement

There is a grid with infinitely many rows and columns. In this grid, there is a rectangular region with consecutive N rows and M columns, and a card is placed in each square in this region.
The front and back sides of these cards can be distinguished, and initially every card faces up.

We will perform the following operation once for each square contains a card:

For each of the following nine squares, flip the card in it if it exists: the target square itself and the eight squares that shares a corner or a side with the target square.

It can be proved that, whether each card faces up or down after all the operations does not depend on the order the operations are performed.
Find the number of cards that face down after all the operations.

Constraints

1 \leq N,M \leq 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

Print the number of cards that face down after all the operations.

Sample Input 1

2 2

Sample Output 1

0

We will flip every card in any of the four operations. Thus, after all the operations, all cards face up.

Sample Input 2

1 7

Sample Output 2

5

After all the operations, all cards except at both ends face down.

Sample Input 3

314 1592

Sample Output 3

496080


program main
implicit None
	integer::n,m
	read *,n,m

	if(n == 1 .or. m == 1)then
		if(n*m == 1)then
			print 101,1
			stop
		endif
		print 101,n*m-2
		stop
	endif

	print 101,(n-2)*(m-2)
101 format(i0)
end program main


program arc091_d
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: n,k,b,ans=0

    read*, n,k

    do b = k+1,n
        ans=ans+(n/(b)*(b-k))+max((n-((n/b)*b+k)+1),0)
    end do
    if (k==0) ans=ans-n
    print'(i0)', ans
end program arc091_d

Score : 400 points

Problem Statement

Takahashi had a pair of two positive integers not exceeding N, (a,b), which he has forgotten.
He remembers that the remainder of a divided by b was greater than or equal to K.
Find the number of possible pairs that he may have had.

Constraints

1 \leq N \leq 10^5

0 \leq K \leq N-1

All input values are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the number of possible pairs that he may have had.

Sample Input 1

5 2

Sample Output 1

7

There are seven possible pairs: (2,3),(5,3),(2,4),(3,4),(2,5),(3,5) and (4,5).

Sample Input 2

10 0

Sample Output 2

100

Sample Input 3

31415 9265

Sample Output 3

287927211


program strange_nim
  implicit none
  integer :: n, a, k, i, g = 0
  read(*,*) n
  do i = 1, n
    read(*,*) a, k
    g = xor(g,grundy(a,k))
  end do
  if (g == 0) then
    write(*,'(a)') "Aoki"
  else
    write(*,'(a)') "Takahashi"
  end if
contains
  recursive function grundy(a,k) result(ret)
    integer, intent(in) :: a, k
    integer :: ret
    ret = a/k
    if (ret == 0 .or. mod(a,k) == 0) return
    ret = grundy(a-ret-1,k)
  end
end program strange_nim

Score : 900 points

Problem Statement

Takahashi and Aoki are playing a stone-taking game. Initially, there are N piles of stones, and the i-th pile contains A_i stones and has an associated integer K_i.

Starting from Takahashi, Takahashi and Aoki take alternate turns to perform the following operation:

Choose a pile. If the i-th pile is selected and there are X stones left in the pile, remove some number of stones between 1 and floor(X/K_i) (inclusive) from the pile.

The player who first becomes unable to perform the operation loses the game. Assuming that both players play optimally, determine the winner of the game.
Here, floor(x) represents the largest integer not greater than x.

Constraints

1 \leq N \leq 200

1 \leq A_i,K_i \leq 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 K_1
:
A_N K_N

Output

If Takahashi will win, print Takahashi; if Aoki will win, print Aoki.

Sample Input 1

2
5 2
3 3

Sample Output 1

Aoki

Initially, from the first pile at most floor(5/2)=2 stones can be removed at a time, and from the second pile at most floor(3/3)=1 stone can be removed at a time.

If Takahashi first takes two stones from the first pile, from the first pile at most floor(3/2)=1 stone can now be removed at a time, and from the second pile at most floor(3/3)=1 stone can be removed at a time.

Then, if Aoki takes one stone from the second pile, from the first pile at most floor(3/2)=1 stone can be removed at a time, and from the second pile no more stones can be removed (since floor(2/3)=0).

Then, if Takahashi takes one stone from the first pile, from the first pile at most floor(2/2)=1 stone can now be removed at a time, and from the second pile no more stones can be removed.

Then, if Aoki takes one stone from the first pile, from the first pile at most floor(1/2)=0 stones can now be removed at a time, and from the second pile no more stones can be removed.

No more operation can be performed, thus Aoki wins. If Takahashi plays differently, Aoki can also win by play accordingly.

Sample Input 2

3
3 2
4 3
5 1

Sample Output 2

Takahashi

Sample Input 3

3
28 3
16 4
19 2

Sample Output 3

Aoki

Sample Input 4

4
3141 59
26535 897
93 23
8462 64

Sample Output 4

Takahashi


program prob23
    implicit none
    integer :: n
    read(*,*) n
    write(*,*) n/3

    stop
contains
end program prob23

Score : 100 points

Problem Statement

There are N students in a school.

We will divide these students into some groups, and in each group they will discuss some themes.

You think that groups consisting of two or less students cannot have an effective discussion, so you want to have as many groups consisting of three or more students as possible.

Divide the students so that the number of groups consisting of three or more students is maximized.

Constraints

1 \leq N \leq 1000

All input values are integers.

Input

Input is given from Standard Input in the following format:

N

Output

If you can form at most x groups consisting of three or more students, print x.

Sample Input 1

8

Sample Output 1

2

For example, you can form a group of three students and another of five students.

Sample Input 2

2

Sample Output 2

0

Sometimes you cannot form any group consisting of three or more students, regardless of how you divide the students.

Sample Input 3

9

Sample Output 3

3


program main
	implicit none
    integer::a
    read(*,*)a
    write(*,*)a/3
    stop
end program main


program ABC_089_C_March
   implicit none
   integer(8) N, i, j, k, a(1:5)
   integer(8)countN
   character(1) c, m(1:5)
   character(10), allocatable :: S(:)
   read(*, *) N
   allocate(S(1: N))
   do i = 1, N
       read(*, *) S(i)
   end do
   m(1) = 'M'
   m(2) = 'A'
   m(3) = 'R'
   m(4) = 'C'
   m(5) = 'H'
   a(:) = 0
   do j = 1, 5
       do i = 1, N
	       read(S(i), '(a1)') c
		   if( c == m(j) ) then
		       a(j) = a(j) + 1
		   end if
	   end do
   end do
   countN = 0
   do k = 1, 3
       do j = k, 3
	       do i = j, 3
		       countN = countN + a(k)*a(j+1)*a(i+2)
		   end do
	   end do
   end do
   write(*, *) countN
end program ABC_089_C_March

Score : 300 points

Problem Statement

There are N people. The name of the i-th person is S_i.

We would like to choose three people so that the following conditions are met:

The name of every chosen person begins with M, A, R, C or H.

There are no multiple people whose names begin with the same letter.

How many such ways are there to choose three people, disregarding order?

Note that the answer may not fit into a 32-bit integer type.

Constraints

1 \leq N \leq 10^5

S_i consists of uppercase English letters.

1 \leq |S_i| \leq 10

S_i \neq S_j (i \neq j)

Input

Input is given from Standard Input in the following format:

N
S_1
:
S_N

Output

If there are x ways to choose three people so that the given conditions are met, print x.

Sample Input 1

5
MASHIKE
RUMOI
OBIRA
HABORO
HOROKANAI

Sample Output 1

2

We can choose three people with the following names:

MASHIKE, RUMOI, HABORO

MASHIKE, RUMOI, HOROKANAI

Thus, we have two ways.

Sample Input 2

4
ZZ
ZZZ
Z
ZZZZZZZZZZ

Sample Output 2

0

Note that there may be no ways to choose three people so that the given conditions are met.

Sample Input 3

5
CHOKUDAI
RNG
MAKOTO
AOKI
RINGO

Sample Output 3

7


program prob4
    implicit none
    integer(8) :: N
    integer :: m

    read(*,*) N
    N = N + 1

    m = 0
    do while(N >= 10)
        N = N / 10
        m = m + 1
    end do

    write(*,*) 9*m + N - 1

    stop
contains
end program prob4

Score : 300 points

Problem Statement

Find the maximum possible sum of the digits (in base 10) of a positive integer not greater than N.

Constraints

1\leq N \leq 10^{16}

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the maximum possible sum of the digits (in base 10) of a positive integer not greater than N.

Sample Input 1

100

Sample Output 1

18

For example, the sum of the digits in 99 is 18, which turns out to be the maximum value.

Sample Input 2

9995

Sample Output 2

35

For example, the sum of the digits in 9989 is 35, which turns out to be the maximum value.

Sample Input 3

3141592653589793

Sample Output 3

137


program A819B
    implicit none
    integer(8)::i,j,ans,Nlen,calc
    character(17)::N
    integer(8),allocatable,dimension(:)::A
    read*,N
    Nlen=len_trim(N)
    ans=0
    allocate(A(Nlen))
    do i=1,Nlen
        A(i)=ichar(N(i:i))-48
        ans=ans+A(i)
    end do

    do i=1,Nlen
        calc=0
        do j=1,Nlen
            if(j<i)calc=calc+A(j)
            if(j==i)calc=calc+A(j)-1
            if(j>i)calc=calc+9
        end do
        ans=max(ans,calc)
    end do

    print'(i0)',ans
end program A819B


program A819B
    implicit none
    integer(8)::i,j,ans,Nlen,calc
    character(16)::N
    integer(8),allocatable,dimension(:)::A
    read*,N
    Nlen=len_trim(N)
    ans=0
    allocate(A(Nlen))
    do i=1,Nlen
        A(i)=ichar(N(i:i))-48
        ans=ans+A(i)
    end do

    do i=1,Nlen
        calc=0
        if(A(i)/=0)then
            do j=1,Nlen
                if(j<i)calc=calc+A(j)
                if(j==i)calc=calc+A(j)-1
                if(j>i)calc=calc+9
            end do
        end if
        ans=max(ans,calc)
    end do

    print'(i0)',ans
end program A819B


program main
  implicit none
  integer :: n, a
  read *, n
  read *, a
  if (a >= 499) then
    print "(a)", "Yes"
  else if (modulo(n, 500) <= a) then
    print "(a)", "Yes"
  else
    print "(a)", "No"
  end if
end program main

Score: 100 points

Problem Statement

E869120 has A 1-yen coins and infinitely many 500-yen coins.

Determine if he can pay exactly N yen using only these coins.

Constraints

N is an integer between 1 and 10000 (inclusive).

A is an integer between 0 and 1000 (inclusive).

Input

Input is given from Standard Input in the following format:

N
A

Output

If E869120 can pay exactly N yen using only his 1-yen and 500-yen coins, print Yes; otherwise, print No.

Sample Input 1

2018
218

Sample Output 1

Yes

We can pay 2018 yen with four 500-yen coins and 18 1-yen coins, so the answer is Yes.

Sample Input 2

2763
0

Sample Output 2

No

When we have no 1-yen coins, we can only pay a multiple of 500 yen using only 500-yen coins. Since 2763 is not a multiple of 500, we cannot pay this amount.

Sample Input 3

37
514

Sample Output 3

Yes


integer :: n,a
read*,n
read*,a

if( mod(n,500)<=a )then
  print*,'Yes'
else
  print*,'No'
end if
end


integer a,b
read*,a,b
if(mod(a,500)<=b)then
	print"(A)","Yes"
else
	print"(A)","No"
endif
end


      read(*,*) i,j
      if(mod(i,500).le.j) then
        write(*,*) "Yes"
      else
        write(*,*) "No"
      end if
      end


program B086
    implicit none
    integer(8) :: N
    integer(8), allocatable, dimension(:) :: a
    integer(8) :: i, j, max, Alice, Bob

    read(5,*) N
    allocate(a(N))
    read(5,*) (a(i), i = 1, N)
    Alice = 0
    Bob = 0

    do i = 1, N
        if (mod(i,2) /= 0) then
            max = maxval(a)
            Alice = Alice + max

        else
            max = maxval(a)
            Bob = Bob + max

        end if

        j = 1
        do while (a(j) /= max)
            j = j + 1
        end do

        a(j) = 0

    end do

    write(6,*) Alice - Bob

end program B086

Score: 200 points

Problem Statement

We have N cards. A number a_i is written on the i-th card.

Alice and Bob will play a game using these cards. In this game, Alice and Bob alternately take one card. Alice goes first.

The game ends when all the cards are taken by the two players, and the score of each player is the sum of the numbers written on the cards he/she has taken. When both players take the optimal strategy to maximize their scores, find Alice's score minus Bob's score.

Constraints

N is an integer between 1 and 100 (inclusive).

a_i \ (1 \leq i \leq N) is an integer between 1 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 a_3 ... a_N

Output

Print Alice's score minus Bob's score when both players take the optimal strategy to maximize their scores.

Sample Input 1

2
3 1

Sample Output 1

2

First, Alice will take the card with 3. Then, Bob will take the card with 1.
The difference of their scores will be 3 - 1 = 2.

Sample Input 2

3
2 7 4

Sample Output 2

5

First, Alice will take the card with 7. Then, Bob will take the card with 4. Lastly, Alice will take the card with 2. The difference of their scores will be 7 - 4 + 2 = 5. The difference of their scores will be 3 - 1 = 2.

Sample Input 3

4
20 18 2 18

Sample Output 3

18


program tenquestionsix
implicit none
integer N, i, suma, sumb, sumab
integer,allocatable,dimension(:) :: A
suma=0
sumb=0
sumab=0
read(*,*) N
allocate(A(N))
read(*,*) A
call heapsort(N,A)
 do i=N,1,-2
 suma=suma+A(i)
 end do
 do i=N-1,1,-2
 sumb=sumb+A(i)
 end do
sumab=suma-sumb
write(*,*) sumab
end program

subroutine heapsort(n,array)
  implicit none
  integer,intent(in) :: n
  integer,intent(inout) :: array(1:n)
  integer ::i,k,j,l
  double precision :: t

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

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

  return
end subroutine heapsort


implicit none
INTEGER(8) :: n,i,ans,k
REAL(8),ALLOCATABLE,DIMENSION(:) :: a

ans = 0
k = 0

READ(5,*) n
ALLOCATE(a(n))
READ(5,*) (a(i),i=1,n)

call heapsort(n,a)

do i=n,1,-1
    if(MOD(n-i,2)==0)then
        ans = ans+a(i)
    else
        ans = ans-a(i)
    endif
enddo

WRITE(6,"(i0)") ans

end

!---http://slpr.sakura.ne.jp/qp/sortf90/---
subroutine heapsort(n,array)
    implicit none
    INTEGER(8),INTENT(IN) :: n
    DOUBLE PRECISION,INTENT(INOUT) :: array(1:n)

    INTEGER :: i,k,j,l
    DOUBLE PRECISION :: t

    if(n.le.0)then
        WRITE(6,*)"Error, at heapsort"; stop
    endif

    if(n.eq.1)return
        l=n/2+1
        k=n
        do while(k.ne.1)
            if(l.gt.1)then
                l = l-1
                t = array(l)
            else
                t=array(k)
                array(k) = array(l)
                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


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

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

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

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

    return
  end subroutine heapsort

program main
    integer N, diff
    integer, allocatable :: a(:)

    read(*,*) N
    allocate(a(N))
    read(*,*) a(:)

    call heapsort(N,a)

    diff=0
    do i=1,N
        if(i/2/=0)then
            diff = diff + a(i)
        else
            diff = diff - a(i)
        endif
    enddo

    write(*,*) diff

end program


program probC
  implicit none
  integer :: c(3,3),a(3),b(3)
  integer i,j,k,ans

  do i=1,3
     read(*,*) c(i,1:3)
  end do

  do i=0,100
     ans=0
     a(1)=i

     b(1)=c(1,1)-a(1)
     b(2)=c(1,2)-a(1)
     b(3)=c(1,3)-a(1)

     a(2)=c(2,1)-b(1)
     a(3)=c(3,1)-b(1)

     do j=1,3
        do k=1,3
           if(c(j,k).ne.a(j)+b(k)) ans=1
        end do
     end do

     if(ans==0) then
        write(*,*) 'Yes'
        stop
     end if
  end do

  write(*,*) 'No'

end program probC

Score: 300 points

Problem Statement

We have a 3 \times 3 grid. A number c_{i, j} is written in the square (i, j), where (i, j) denotes the square at the i-th row from the top and the j-th column from the left.

According to Takahashi, there are six integers a_1, a_2, a_3, b_1, b_2, b_3 whose values are fixed, and the number written in the square (i, j) is equal to a_i + b_j.

Determine if he is correct.

Constraints

c_{i, j} \ (1 \leq i \leq 3, 1 \leq j \leq 3) is an integer between 0 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

c_{1,1} c_{1,2} c_{1,3}
c_{2,1} c_{2,2} c_{2,3}
c_{3,1} c_{3,2} c_{3,3}

Output

If Takahashi's statement is correct, print Yes; otherwise, print No.

Sample Input 1

1 0 1
2 1 2
1 0 1

Sample Output 1

Yes

Takahashi is correct, since there are possible sets of integers such as: a_1=0,a_2=1,a_3=0,b_1=1,b_2=0,b_3=1.

Sample Input 2

2 2 2
2 1 2
2 2 2

Sample Output 2

No

Takahashi is incorrect in this case.

Sample Input 3

0 8 8
0 8 8
0 8 8

Sample Output 3

Yes

Sample Input 4

1 8 6
2 9 7
0 7 7

Sample Output 4

No


module queue
  implicit none
  type :: JagArray
    integer,allocatable :: array(:)
  end type JagArray
contains
  subroutine push(A,b)
  	integer,allocatable,dimension(:)::A
    integer::b
    A=[A,b]
  end subroutine
  subroutine pop(A)
  	integer,allocatable,dimension(:)::A
    A=A(2:)
  end subroutine
end module queue

module maze
integer,allocatable,dimension(:,:)::mazemap
contains
subroutine getmap(mazemap,R,C)
  integer,allocatable,dimension(:,:)::mazemap
  integer R,C
  character(50) linereader
  allocate(mazemap(0:R+1,0:C+1))
  mazemap(0,:)=-2;mazemap(R+1,:)=-2;mazemap(:,0)=-2;mazemap(:,C+1)=-2;
  do i=1,R
    read*,linereader
    do j=1,C
      if(linereader(j:j)==".")mazemap(i,j)=-1
      if(linereader(j:j)=="#")mazemap(i,j)=-2
    end do
  end do
end subroutine
end module maze

use queue
use maze
integer,allocatable,dimension(:)::queue_x,queue_y
integer H,W
integer numcnt
integer dirx(4),diry(4)
dirx=[1,0,-1,0]
diry=[0,1,0,-1]
read*,H,W
call getmap(mazemap,H,W)

mazemap(1,1)=1
queue_x=[1]
queue_y=[1]
do while(mazemap(H,W)==-1)
  do i=1,4
    if(mazemap(queue_x(1)+dirx(i),queue_y(1)+diry(i))==-1)then
      mazemap(queue_x(1)+dirx(i),queue_y(1)+diry(i))=mazemap(queue_x(1),queue_y(1))+1
      call push(queue_x,queue_x(1)+dirx(i))
      call push(queue_y,queue_y(1)+diry(i))
    endif
  end do
  call pop(queue_x)
  call pop(queue_y)
  if(size(queue_x)==0)exit
end do

numcnt=0
do i=1,H
  do j=1,W
    if(mazemap(i,j)==-2)numcnt=numcnt+1
  end do
end do
print"(i0)",merge(-1,H*W-mazemap(H,W)-numcnt,mazemap(i,j)==-1)
end

Score: 400 points

Problem statement

We have an H \times W grid whose squares are painted black or white. The square at the i-th row from the top and the j-th column from the left is denoted as (i, j).

Snuke would like to play the following game on this grid. At the beginning of the game, there is a character called Kenus at square (1, 1). The player repeatedly moves Kenus up, down, left or right by one square. The game is completed when Kenus reaches square (H, W) passing only white squares.

Before Snuke starts the game, he can change the color of some of the white squares to black. However, he cannot change the color of square (1, 1) and (H, W). Also, changes of color must all be carried out before the beginning of the game.

When the game is completed, Snuke's score will be the number of times he changed the color of a square before the beginning of the game. Find the maximum possible score that Snuke can achieve, or print -1 if the game cannot be completed, that is, Kenus can never reach square (H, W) regardless of how Snuke changes the color of the squares.

The color of the squares are given to you as characters s_{i, j}. If square (i, j) is initially painted by white, s_{i, j} is .; if square (i, j) is initially painted by black, s_{i, j} is #.

Constraints

H is an integer between 2 and 50 (inclusive).

W is an integer between 2 and 50 (inclusive).

s_{i, j} is . or # (1 \leq i \leq H, 1 \leq j \leq W).

s_{1, 1} and s_{H, W} are ..

Input

Input is given from Standard Input in the following format:

H W
s_{1, 1}s_{1, 2}s_{1, 3} ... s_{1, W}
s_{2, 1}s_{2, 2}s_{2, 3} ... s_{2, W}
: :
s_{H, 1}s_{H, 2}s_{H, 3} ... s_{H, W}

Output

Print the maximum possible score that Snuke can achieve, or print -1 if the game cannot be completed.

Sample Input 1

3 3
..#
#..
...

Sample Output 1

2

The score 2 can be achieved by changing the color of squares as follows:

Sample Input 2

10 37
.....................................
...#...####...####..###...###...###..
..#.#..#...#.##....#...#.#...#.#...#.
..#.#..#...#.#.....#...#.#...#.#...#.
.#...#.#..##.#.....#...#.#.###.#.###.
.#####.####..#.....#...#..##....##...
.#...#.#...#.#.....#...#.#...#.#...#.
.#...#.#...#.##....#...#.#...#.#...#.
.#...#.####...####..###...###...###..
.....................................

Sample Output 2

209


integer X,Y
read*,x,y
if(mod(x,y)==0)then
  print"(i0)",-1
else
  print"(i0)",x
endif
end

Score : 100 points

Problem Statement

You are given positive integers X and Y.
If there exists a positive integer not greater than 10^{18} that is a multiple of X but not a multiple of Y, choose one such integer and print it.
If it does not exist, print -1.

Constraints

1 ≤ X,Y ≤ 10^9

X and Y are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

Print a positive integer not greater than 10^{18} that is a multiple of X but not a multiple of Y, or print -1 if it does not exist.

Sample Input 1

8 6

Sample Output 1

16

For example, 16 is a multiple of 8 but not a multiple of 6.

Sample Input 2

3 3

Sample Output 2

-1

A multiple of 3 is a multiple of 3.


program main
    implicit none
    integer X, Y, i
    integer(8) N

    read(*,*) X, Y

    do i = 1, 1000000000
        N = X*i
        if(mod(N,Y)/=0)then
            write(*,*) N
            stop
        endif
    enddo

    write(*,*) -1

end program


integer a,b,c
read*,a,b,c
print"(i0)",mod(a-b,c)
end

Score : 100 points

Problem Statement

You went shopping to buy cakes and donuts with X yen (the currency of Japan).

First, you bought one cake for A yen at a cake shop.
Then, you bought as many donuts as possible for B yen each, at a donut shop.

How much do you have left after shopping?

Constraints

1 \leq A, B \leq 1 000

A + B \leq X \leq 10 000

X, A and B are integers.

Input

Input is given from Standard Input in the following format:

X
A
B

Output

Print the amount you have left after shopping.

Sample Input 1

1234
150
100

Sample Output 1

84

You have 1234 - 150 = 1084 yen left after buying a cake.
With this amount, you can buy 10 donuts, after which you have 84 yen left.

Sample Input 2

1000
108
108

Sample Output 2

28

Sample Input 3

579
123
456

Sample Output 3

0

Sample Input 4

7477
549
593

Sample Output 4

405


program prob4
    implicit none
    integer :: n, i, m, tmp
    integer, allocatable :: a(:), aa(:)
    read(*,*) n
    allocate(a(n))
    allocate(aa(n))
    read(*,*) a
    read(*,*) aa

    tmp = a(1)
    do i = 1, n
        tmp = tmp + aa(i)
    end do
    m = tmp

    do i = 2, n
        tmp = tmp + a(i)
        tmp = tmp - aa(i-1)
        m = max(m, tmp)
    end do

    write(*,*) m
    deallocate(a)
    deallocate(aa)

    stop
contains
end program prob4

Score : 300 points

Problem Statement

We have a 2 \times N grid. We will denote the square at the i-th row and j-th column (1 \leq i \leq 2, 1 \leq j \leq N) as (i, j).

You are initially in the top-left square, (1, 1).
You will travel to the bottom-right square, (2, N), by repeatedly moving right or down.

The square (i, j) contains A_{i, j} candies.
You will collect all the candies you visit during the travel.
The top-left and bottom-right squares also contain candies, and you will also collect them.

At most how many candies can you collect when you choose the best way to travel?

Constraints

1 \leq N \leq 100

1 \leq A_{i, j} \leq 100 (1 \leq i \leq 2, 1 \leq j \leq N)

Input

Input is given from Standard Input in the following format:

N
A_{1, 1} A_{1, 2} ... A_{1, N}
A_{2, 1} A_{2, 2} ... A_{2, N}

Output

Print the maximum number of candies that can be collected.

Sample Input 1

5
3 2 2 4 1
1 2 2 2 1

Sample Output 1

14

The number of collected candies will be maximized when you:

move right three times, then move down once, then move right once.

Sample Input 2

4
1 1 1 1
1 1 1 1

Sample Output 2

5

You will always collect the same number of candies, regardless of how you travel.

Sample Input 3

7
3 3 4 5 4 5 3
5 3 4 4 2 3 2

Sample Output 3

29

Sample Input 4

1
2
3

Sample Output 4

5


module Weighted_UF

    integer, allocatable, dimension(:) :: par, rank, diff_weight

    contains

    subroutine init(N)
        integer :: N
        allocate(par(N), rank(N), diff_weight(N))
        do i = 1, N
            par(i) = i
            rank(i) = 0
            diff_weight(i) = 0
        end do
    end subroutine

    recursive function root(x) result (res)
        integer :: x, res
        if (par(x) == x)then
            res = x
        else
            res = root(par(x))
            diff_weight(x) = diff_weight(x) + diff_weight(par(x))
            par(x) = res
        end if
    end function

    function weight(x) result (res)
        integer :: x, res, r
        res = diff_weight(x)
        r = root(x)
    end function

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

    subroutine unite(x, y, w)
        integer :: x, y, w
        w = w + weight(x) - weight(y)
        x = root(x); y = root(y)
        if (x /= y) then
            if(rank(x) < rank(y)) then
                x = xor(x, y); y = xor(y, x); x = xor(x, y) !swap
                w = -w
            end if
            if(rank(x) == rank(y)) then
                rank(x) = rank(x) + 1
            end if
            par(y) = x
            diff_weight(y) = w
        end if
    end subroutine

    function diff(x, y) result (res)
        integer :: x, y, res
        res = weight(y) - weight(x)
    end function

    subroutine print_UF(N)
        integer :: N
        print"(a)", parents
        do i = 1, N
            print*, "Number :", i , " " , par(i)
        end do
    end subroutine
end module Weighted_UF

program main
    use Weighted_UF

    integer N, M, l, r, d
    read*,N, M

    call init(N)

    do i = 1, M
        read*, l, r, d
        if (same(l, r)) then
            if (diff(l, r) /= d) then
                print*, l, r
                print"(a)", "No"
                stop
            end if
        else
            call unite(l, r, d)
        end if

    end do

    print"(a)", "Yes"
end

Score : 400 points

Problem Statement

There are N people standing on the x-axis.
Let the coordinate of Person i be x_i.
For every i, x_i is an integer between 0 and 10^9 (inclusive).
It is possible that more than one person is standing at the same coordinate.

You will given M pieces of information regarding the positions of these people.
The i-th piece of information has the form (L_i, R_i, D_i).
This means that Person R_i is to the right of Person L_i by D_i units of distance, that is, x_{R_i} - x_{L_i} = D_i holds.

It turns out that some of these M pieces of information may be incorrect.
Determine if there exists a set of values (x_1, x_2, ..., x_N) that is consistent with the given pieces of information.

Constraints

1 \leq N \leq 100 000

0 \leq M \leq 200 000

1 \leq L_i, R_i \leq N (1 \leq i \leq M)

0 \leq D_i \leq 10 000 (1 \leq i \leq M)

L_i \neq R_i (1 \leq i \leq M)

If i \neq j, then (L_i, R_i) \neq (L_j, R_j) and (L_i, R_i) \neq (R_j, L_j).

D_i are integers.

Input

Input is given from Standard Input in the following format:

N M
L_1 R_1 D_1
L_2 R_2 D_2
:
L_M R_M D_M

Output

If there exists a set of values (x_1, x_2, ..., x_N) that is consistent with all given pieces of information, print Yes; if it does not exist, print No.

Sample Input 1

3 3
1 2 1
2 3 1
1 3 2

Sample Output 1

Yes

Some possible sets of values (x_1, x_2, x_3) are (0, 1, 2) and (101, 102, 103).

Sample Input 2

3 3
1 2 1
2 3 1
1 3 5

Sample Output 2

No

If the first two pieces of information are correct, x_3 - x_1 = 2 holds, which is contradictory to the last piece of information.

Sample Input 3

4 3
2 1 1
2 3 5
3 4 2

Sample Output 3

Yes

Sample Input 4

10 3
8 7 100
7 9 100
9 8 100

Sample Output 4

No

Sample Input 5

100 0

Sample Output 5

Yes


      PROGRAM traveling
      IMPLICIT NONE
      INTEGER :: N
      INTEGER,ALLOCATABLE :: t(:), x(:),y(:)

      INTEGER :: i, dt, dx,dy, s
      LOGICAL :: flag = .TRUE.

      READ*,N
      N = N + 1
      ALLOCATE( t(N),x(N),y(N) )
      DO i = 2,N
        READ*,t(i),x(i),y(i)
      END DO
      t(1) = 0
      x(1) = 0
      y(1) = 0

      DO i = 1,N-1
        dt = t(i+1) - t(i)
        dx = abs( x(i+1)-x(i) )
        dy = abs( y(i+1)-y(i) )

        IF(dx+dy>dt)THEN
          flag = .false.
          EXIT
        END IF

        s = dy+dx -dt
        IF( mod(dx+dy,2)/=mod(dt,2) )THEN
          flag = .false.
          EXIT
        END IF
      END DO

      IF(flag)THEN
        print'(A)','YES'
      ELSE
        print'(A)','NO'
      END IF

      END PROGRAM

Score : 300 points

Problem Statement

AtCoDeer the deer is going on a trip in a two-dimensional plane.
In his plan, he will depart from point (0, 0) at time 0, then for each i between 1 and N (inclusive), he will visit point (x_i,y_i) at time t_i.

If AtCoDeer is at point (x, y) at time t, he can be at one of the following points at time t+1: (x+1,y), (x-1,y), (x,y+1) and (x,y-1).
Note that he cannot stay at his place.
Determine whether he can carry out his plan.

Constraints

1 ≤ N ≤ 10^5

0 ≤ x_i ≤ 10^5

0 ≤ y_i ≤ 10^5

1 ≤ t_i ≤ 10^5

t_i < t_{i+1} (1 ≤ i ≤ N-1)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
t_1 x_1 y_1
t_2 x_2 y_2
:
t_N x_N y_N

Output

If AtCoDeer can carry out his plan, print Yes; if he cannot, print No.

Sample Input 1

2
3 1 2
6 1 1

Sample Output 1

Yes

For example, he can travel as follows: (0,0), (0,1), (1,1), (1,2), (1,1), (1,0), then (1,1).

Sample Input 2

1
2 100 100

Sample Output 2

No

It is impossible to be at (100,100) two seconds after being at (0,0).

Sample Input 3

2
5 1 1
100 1 1

Sample Output 3

No


program tenquestionten
 integer,allocatable,dimension(:) :: X
 integer,allocatable,dimension(:) :: Y
 integer,allocatable,dimension(:) :: T
 integer N, i, j, sum, Xl, Yl, Tl, Z
 read(*,*) N
 allocate(X(N+1))
 allocate(Y(N+1))
 allocate(T(N+1))
 sum=0
 X(1) = 0
 Y(1) = 0
 T(1) = 0
  do i=2,N+1
  read(*,*) T(i), X(i), Y(i)
  end do
  do i=1,N
  Xl=abs(X(i+1)-X(i))
  Yl=abs(Y(i+1)-Y(i))
  Tl=abs(T(i+1)-T(i))
  Z=Xl+Yl
   if (Tl.ge.Z) then
    if ((mod(Z,2)).eq.(mod(Tl,2))) then
    sum = sum + 1
    end if
   end if
  end do
 if (sum.eq.N) then
 write(*,*) "YES"
 else if (sum.ne.N) then
 write(*,*) "NO"
 end if
end program


program tenquestionten
 integer,allocatable,dimension(:) :: X
 integer,allocatable,dimension(:) :: Y
 integer,allocatable,dimension(:) :: T
 integer N, i, j, sum, Xl, Yl, Tl, Z
 read(*,*) N
 allocate(X(N+1))
 allocate(Y(N+1))
 allocate(T(N+1))
 sum=0
 X(1) = 0
 Y(1) = 0
 T(1) = 0
  do i=2,N+1
  read(*,*) T(i), X(i), Y(i)
  end do
  do i=1,N
  Xl=abs(X(i+1)-X(i))
  Yl=abs(Y(i+1)-Y(i))
  Tl=abs(T(i+1)-T(i))
  Z=Xl+Yl
   if (Z.ge.Tl) then
    if ((mod(Z,2)).eq.(mod(Tl,2))) then
    sum = sum + 1
    end if
   end if
  end do
 if (sum.eq.N-1) then
 write(*,*) "YES"
 else if (sum.ne.N-1) then
 write(*,*) "NO"
 end if
end program


implicit none

integer:: n,i,j,k,count,jj,count_max
character,allocatable,dimension(:) :: c*1
integer,allocatable,dimension(:):: x,y
read (5,*) n,k

allocate(x(n))
allocate(y(n))
allocate(c(n))

do i=1,n
    read(5,*) x(i),y(i),c(i)
enddo

do j=1,k
    do jj=1,k
    count=0
    do i=1,n
        if (mod(mod(x(i)-j+1,k),2)==1 .and. mod(mod(y(i)-jj+1,k),2)==0 .and. c(i)=="W" ) then
            count=count+1
        if (mod(mod(x(i)-j+1,k),2)==0 .and. mod(mod(y(i)-jj+1,k),2)==1 .and. c(i)=="W" ) then
            count=count+1
        endif
        endif
enddo
enddo
        count_max=max(count,n-count)
enddo

!write(*,*) n,k
!write(*,*) x,y,c
write(*,*) count_max

stop
end

Score : 500 points

Problem Statement

AtCoDeer is thinking of painting an infinite two-dimensional grid in a checked pattern of side K.
Here, a checked pattern of side K is a pattern where each square is painted black or white so that each connected component of each color is a K × K square.
Below is an example of a checked pattern of side 3:

AtCoDeer has N desires.
The i-th desire is represented by x_i, y_i and c_i.
If c_i is B, it means that he wants to paint the square (x_i,y_i) black; if c_i is W, he wants to paint the square (x_i,y_i) white.
At most how many desires can he satisfy at the same time?

Constraints

1 ≤ N ≤ 10^5

1 ≤ K ≤ 1000

0 ≤ x_i ≤ 10^9

0 ≤ y_i ≤ 10^9

If i ≠ j, then (x_i,y_i) ≠ (x_j,y_j).

c_i is B or W.

N, K, x_i and y_i are integers.

Input

Input is given from Standard Input in the following format:

N K
x_1 y_1 c_1
x_2 y_2 c_2
:
x_N y_N c_N

Output

Print the maximum number of desires that can be satisfied at the same time.

Sample Input 1

4 3
0 1 W
1 2 W
5 3 B
5 4 B

Sample Output 1

4

He can satisfy all his desires by painting as shown in the example above.

Sample Input 2

2 1000
0 0 B
0 1 W

Sample Output 2

2

Sample Input 3

6 2
1 2 B
2 1 W
2 2 B
1 0 B
0 6 W
4 5 W

Sample Output 3

4


implicit none

integer(8) :: ans_min=10000000000_8, ans_max=0, count=0
integer(8) :: k, i, j, n, n_org
integer,allocatable,dimension(:):: a

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

read(5,*) (a(i),i=1,k)

n=maxval(a)-mod(maxval(a),a(1))

!write(6,*) n
do j=1,1000
    n_org=n
    do i=1, k
        n=(int(n/a(i)))*a(i)
    enddo

    if (n==2) then
        ans_min=min(ans_min,n_org)
        ans_max=max(ans_max,n_org)
        count=1
    endif
n=n_org+a(1)

enddo

if (count==0)  then
    write(6,*), -1
else
    write(6,*) ans_min,ans_max+a(1)-1
endif

stop
end

Score : 500 points

Problem Statement

An adult game master and N children are playing a game on an ice rink.
The game consists of K rounds.
In the i-th round, the game master announces:

Form groups consisting of A_i children each!

Then the children who are still in the game form as many groups of A_i children as possible.
One child may belong to at most one group.
Those who are left without a group leave the game. The others proceed to the next round.
Note that it's possible that nobody leaves the game in some round.

In the end, after the K-th round, there are exactly two children left, and they are declared the winners.

You have heard the values of A_1, A_2, ..., A_K. You don't know N, but you want to estimate it.

Find the smallest and the largest possible number of children in the game before the start, or determine that no valid values of N exist.

Constraints

1 \leq K \leq 10^5

2 \leq A_i \leq 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

K
A_1 A_2 ... A_K

Output

Print two integers representing the smallest and the largest possible value of N, respectively,
or a single integer -1 if the described situation is impossible.

Sample Input 1

4
3 4 3 2

Sample Output 1

6 8

For example, if the game starts with 6 children, then it proceeds as follows:

In the first round, 6 children form 2 groups of 3 children, and nobody leaves the game.

In the second round, 6 children form 1 group of 4 children, and 2 children leave the game.

In the third round, 4 children form 1 group of 3 children, and 1 child leaves the game.

In the fourth round, 3 children form 1 group of 2 children, and 1 child leaves the game.

The last 2 children are declared the winners.

Sample Input 2

5
3 4 100 3 2

Sample Output 2

-1

This situation is impossible.
In particular, if the game starts with less than 100 children, everyone leaves after the third round.

Sample Input 3

10
2 2 2 2 2 2 2 2 2 2

Sample Output 3

2 3


implicit none

integer(8) :: ans_min=10000000000_8, ans_max=0, count=0
integer(8) :: k, i, j, n, n_org
integer,allocatable,dimension(:):: a

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

read(5,*) (a(i),i=1,k)

n=maxval(a)-mod(maxval(a),a(1))

write(6,*) n
do j=1,300
    n_org=n
    do i=1, k
        n=(int(n/a(i)))*a(i)
    enddo

    if (n==2) then
        ans_min=min(ans_min,n_org)
        ans_max=max(ans_max,n_org)
        count=1
    endif
n=n_org+a(1)

enddo

if (count==0)  then
    write(6,*), -1
else
    write(6,*) ans_min,ans_max+a(1)-1
endif

stop
end


INTEGER A,B
CHARACTER(20) S
READ*,A,B,S
IF(SCAN(S,"-")/=A+1.OR.SCAN(S,"-",.TRUE.)/=A+1)THEN
  PRINT"(A)","No"
ELSE
  PRINT"(A)","Yes"
ENDIF
END

Score : 200 points

Problem Statement

The postal code in Atcoder Kingdom is A+B+1 characters long, its (A+1)-th character is a hyphen -, and the other characters are digits from 0 through 9.

You are given a string S. Determine whether it follows the postal code format in Atcoder Kingdom.

Constraints

1≤A,B≤5

|S|=A+B+1

S consists of - and digits from 0 through 9.

Input

Input is given from Standard Input in the following format:

A B
S

Output

Print Yes if S follows the postal code format in AtCoder Kingdom; print No otherwise.

Sample Input 1

3 4
269-6650

Sample Output 1

Yes

The (A+1)-th character of S is -, and the other characters are digits from 0 through 9, so it follows the format.

Sample Input 2

1 1
---

Sample Output 2

No

S contains unnecessary -s other than the (A+1)-th character, so it does not follow the format.

Sample Input 3

1 2
7444

Sample Output 3

No


program proB
  implicit none
  integer i,j,a,b
  character s*9,check*10

  read(*,*) a,b
  read(*,*) s

  check='0123456789'
  do i=1,a+b
     if(i==a+1) then
        if(s(i:i)=='-') then
           cycle
        else
           write(*,*) 'No'
           stop
        end if
     else if(s(i:i)=='0' .or. s(i:i)=='1' .or. s(i:i)=='2' .or. s(i:i)=='3' &
          .or. s(i:i)=='4' .or. s(i:i)=='5' .or. s(i:i)=='6' .or. s(i:i)=='7' &
          .or. s(i:i)=='8' .or. s(i:i)=='9') then
        cycle
     else
        write(*,*) 'No'
        stop
     end if
  end do

  write(*,*) 'Yes'

end program proB


program C_solution

implicit none
integer :: n,c(499),s(499),f(499)
integer :: i,j,k,t(500)

read(*,*) n
do i=1, n-1
  read(*,*) c(i),s(i),f(i)
enddo

do i = 1, n
    t(i) = 0
    if ( i <= n-1 ) then
      do j = i , n-1
        t(i) = max( s(j), ((t(i)-1)/f(j))*f(j) + f(j) ) + c(j)
      enddo
    endif
  write(*,*) t(i)
  enddo

end program

Score : 300 points

Problem Statement

A railroad running from west to east in Atcoder Kingdom is now complete.

There are N stations on the railroad, numbered 1 through N from west to east.

Tomorrow, the opening ceremony of the railroad will take place.

On this railroad, for each integer i such that 1≤i≤N-1, there will be trains that run from Station i to Station i+1 in C_i seconds. No other trains will be operated.

The first train from Station i to Station i+1 will depart Station i S_i seconds after the ceremony begins. Thereafter, there will be a train that departs Station i every F_i seconds.

Here, it is guaranteed that F_i divides S_i.

That is, for each Time t satisfying S_i≤t and t％F_i=0, there will be a train that departs Station i t seconds after the ceremony begins and arrives at Station i+1 t+C_i seconds after the ceremony begins, where A％B denotes A modulo B, and there will be no other trains.

For each i, find the earliest possible time we can reach Station N if we are at Station i when the ceremony begins, ignoring the time needed to change trains.

Constraints

1≤N≤500

1≤C_i≤100

1≤S_i≤10^5

1≤F_i≤10

S_i％F_i=0

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
C_1 S_1 F_1
:
C_{N-1} S_{N-1} F_{N-1}

Output

Print N lines. Assuming that we are at Station i (1≤i≤N) when the ceremony begins, if the earliest possible time we can reach Station N is x seconds after the ceremony begins, the i-th line should contain x.

Sample Input 1

3
6 5 1
1 10 1

Sample Output 1

12
11
0

We will travel from Station 1 as follows:

5 seconds after the beginning: take the train to Station 2.

11 seconds: arrive at Station 2.

11 seconds: take the train to Station 3.

12 seconds: arrive at Station 3.

We will travel from Station 2 as follows:

10 seconds: take the train to Station 3.

11 seconds: arrive at Station 3.

Note that we should print 0 for Station 3.

Sample Input 2

4
12 24 6
52 16 4
99 2 2

Sample Output 2

187
167
101
0

Sample Input 3

4
12 13 1
44 17 17
66 4096 64

Sample Output 3

4162
4162
4162
0


program D_solution

implicit none
integer :: Q,l(10**5),r(10**5)
integer :: i,j
integer :: is_prime(10**5),like_2017(50001)

read(*,*) Q
do i = 1, Q
  read(*,*) l(i),r(i)
enddo

is_prime(1) = 0

do i = 2, 10**5
  is_prime(i) = 1
  do j = 2, int((i)**0.5)
    if(mod(i,j) == 0) then
      is_prime(i) = 0
    endif
  enddo
enddo

like_2017 = 0

do i = 1, 10**5, 2
  like_2017((i+1)/2) = is_prime(i) * is_prime((i+1)/2)
enddo

do i=1,Q
    write(*,*) sum(like_2017((l(i)+1)/2:(r(i)+1)/2))
enddo
end program

Score : 400 points

Problem Statement

We say that a odd number N is similar to 2017 when both N and (N+1)/2 are prime.

You are given Q queries.

In the i-th query, given two odd numbers l_i and r_i, find the number of odd numbers x similar to 2017 such that l_i ≤ x ≤ r_i.

Constraints

1≤Q≤10^5

1≤l_i≤r_i≤10^5

l_i and r_i are odd.

All input values are integers.

Input

Input is given from Standard Input in the following format:

Q
l_1 r_1
:
l_Q r_Q

Output

Print Q lines. The i-th line (1≤i≤Q) should contain the response to the i-th query.

Sample Input 1

1
3 7

Sample Output 1

2

3 is similar to 2017, since both 3 and (3+1)/2=2 are prime.

5 is similar to 2017, since both 5 and (5+1)/2=3 are prime.

7 is not similar to 2017, since (7+1)/2=4 is not prime, although 7 is prime.

Thus, the response to the first query should be 2.

Sample Input 2

4
13 13
7 11
7 11
2017 2017

Sample Output 2

1
0
0
1

Note that 2017 is also similar to 2017.

Sample Input 3

6
1 53
13 91
37 55
19 51
73 91
13 49

Sample Output 3

4
4
1
1
1
2


program D_solution

implicit none
integer :: Q,l(10**5),r(10**5),n
integer :: i,j,is_prime(10**5)

read(*,*) Q
do i = 1, Q
  read(*,*) l(i),r(i)
enddo

  is_prime(1) = 0
do i = 2, 10**5
  is_prime(i) = 1
  do j = 2, int((i)**0.5)
    if(mod(i,j) == 0) then
      is_prime(i) = 0
    endif
  enddo
enddo

do i=1,Q
  n = 0
  do j=l(i),r(i),2
    if(is_prime(j) == 1 .and. is_prime(int((j+1)/2)) == 1) then
	n = n + 1
    endif
  enddo
  write(*,*) n
enddo
end program


program main
  implicit none
  integer :: a, b, c, d
  read *, a, b, c, d
  if (a + b == c + d) then
    print "(a)", "Balanced"
  else if (a + b < c + d) then
    print "(a)", "Right"
  else
    print "(a)", "Left"
  end if
end program main

Score : 100 points

Problem Statement

A balance scale tips to the left if L>R, where L is the total weight of the masses on the left pan and R is the total weight of the masses on the right pan. Similarly, it balances if L=R, and tips to the right if L<R.

Takahashi placed a mass of weight A and a mass of weight B on the left pan of a balance scale, and placed a mass of weight C and a mass of weight D on the right pan.

Print Left if the balance scale tips to the left; print Balanced if it balances; print Right if it tips to the right.

Constraints

1\leq A,B,C,D \leq 10

All input values are integers.

Input

Input is given from Standard Input in the following format:

A B C D

Output

Print Left if the balance scale tips to the left; print Balanced if it balances; print Right if it tips to the right.

Sample Input 1

3 8 7 1

Sample Output 1

Left

The total weight of the masses on the left pan is 11, and the total weight of the masses on the right pan is 8. Since 11>8, we should print Left.

Sample Input 2

3 4 5 2

Sample Output 2

Balanced

The total weight of the masses on the left pan is 7, and the total weight of the masses on the right pan is 7. Since 7=7, we should print Balanced.

Sample Input 3

1 7 6 4

Sample Output 3

Right

The total weight of the masses on the left pan is 8, and the total weight of the masses on the right pan is 10. Since 8<10, we should print Right.


program main
integer(8) X,Y,length

read(*,*) X,Y
An=X*2
length=1
do
	An = X*2
    if(An<=Y)then
	    length = length + 1
    else
    	exit
    endif
enddo

write(*,*) length

end program

Score : 300 points

Problem Statement

As a token of his gratitude, Takahashi has decided to give his mother an integer sequence.
The sequence A needs to satisfy the conditions below:

A consists of integers between X and Y (inclusive).

For each 1\leq i \leq |A|-1, A_{i+1} is a multiple of A_i and strictly greater than A_i.

Find the maximum possible length of the sequence.

Constraints

1 \leq X \leq Y \leq 10^{18}

All input values are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

Print the maximum possible length of the sequence.

Sample Input 1

3 20

Sample Output 1

3

The sequence 3,6,18 satisfies the conditions.

Sample Input 2

25 100

Sample Output 2

3

Sample Input 3

314159265 358979323846264338

Sample Output 3

31


program shift_only
  implicit none
  integer :: n, a(200) = 0, i
  read(*,*) n
  read(*,*) a(1:n)
  write(*,'(i0)') minval(number_of_trailing_zeros(a(1:n)))
contains
  pure elemental integer function number_of_trailing_zeros(i) result(res)
    integer, intent(in) :: i
    integer :: x, y
    if (i == 0) then
      res = 32
      return
    end if
    res = 31
    x = i
    y = lshift(x, 16)
    if (y /= 0) then
      res = res - 16
      x = y
    end if
    y = lshift(x, 8)
    if (y /= 0) then
      res = res - 8
      x = y
    end if
    y = lshift(x, 4)
    if (y /= 0) then
      res = res - 4
      x = y
    end if
    y = lshift(x, 2)
    if (y /= 0) then
      res = res - 2
      x = y
    end if
    res = res - urshift(lshift(x, 1), 31)
  end
  pure elemental integer function urshift(i, n) result(res)
    integer, intent(in) :: i
    integer, intent(in) :: n
    if (n == 0) then
      res = i
    else
      res = rshift(ibclr(rshift(i, 1), 31), n - 1)
    end if
  end
end program shift_only

Score : 200 points

Problem Statement

There are N positive integers written on a blackboard: A_1, ..., A_N.

Snuke can perform the following operation when all integers on the blackboard are even:

Replace each integer X on the blackboard by X divided by 2.

Find the maximum possible number of operations that Snuke can perform.

Constraints

1 \leq N \leq 200

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible number of operations that Snuke can perform.

Sample Input 1

3
8 12 40

Sample Output 1

2

Initially, [8, 12, 40] are written on the blackboard.
Since all those integers are even, Snuke can perform the operation.

After the operation is performed once, [4, 6, 20] are written on the blackboard.
Since all those integers are again even, he can perform the operation.

After the operation is performed twice, [2, 3, 10] are written on the blackboard.
Now, there is an odd number 3 on the blackboard, so he cannot perform the operation any more.

Thus, Snuke can perform the operation at most twice.

Sample Input 2

4
5 6 8 10

Sample Output 2

0

Since there is an odd number 5 on the blackboard already in the beginning, Snuke cannot perform the operation at all.

Sample Input 3

6
382253568 723152896 37802240 379425024 404894720 471526144

Sample Output 3

8


program main
    implicit none
    INTEGER :: i, n, ans = 1e9, ans1 = 0
    INTEGER, ALLOCATABLE :: a(:)
    read(*, *) n
    ALLOCATE(a(n))
    READ(*, *) (a(i), i = 1, n)
    do i = 1, n
        do while(mod(a(i), 2) == 0)
            ans1 = ans1 + 1
            a(i) = a(i) / 2
        enddo
        ans = min(ans, ans1)
        ans1 = 0
    enddo
    write(*, *) ans
end program main


program main
    implicit none
    integer :: n,i,j,divide,cnt
    integer,allocatable,dimension(:) :: a

    read(5,*) n

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

    do j = 1, 1000
        divide = 0

        do i = 1, n
            if ( mod(a(i),2)==0 ) then
                divide = divide +1
            endif
        end do

        if ( divide == n ) then
            cnt = cnt + 1
            do i = 1, n
                a(i) = a(i)/2
            end do
        else
            exit
        endif
    end do

    write(6,"(i0)") cnt

end program main


program tenquestionthree
    implicit none
    integer n, i, x
    integer,allocatable,dimension(:) ::A
    x=30
    read(*,*) n
    allocate(A(n))
     read(*,*) A                                !横に読ます時これでいいらしい
     do i=1,n
      do while ((mod(A(i),2**x)).ne.0)          !do while文．論理が真ならループを繰り返す．最も2の成分が少ないA(i)を探してる
      x=x-1
      end do
    end do
    write(*,*) x
end program


program recording
  implicit none
  integer :: n, k, s, t, c, x(200001,30), i, j, m
  s = 0
  x = 0
  read(*,*) n, k
  do i = 1, n
    read(*,*) s, t, c
    x(2*s-1,c) = x(2*s-1,c) + 1
    x(2*t+1,c) = x(2*t+1,c) - 1
  end do
  do j = 1, k
    do i = 1, 200001
      x(i+1,j) = x(i+1,j) + x(i,j)
    end do
  end do
  m = 0
  do i = 1, 200001
    c = sum(x(i,:))
    if (m.lt.c) m = c
  end do
  write(*,'(i0)') m
  stop
end program recording

Score : 400 points

Problem Statement

Joisino is planning to record N TV programs with recorders.

The TV can receive C channels numbered 1 through C.

The i-th program that she wants to record will be broadcast from time s_i to time t_i (including time s_i but not t_i) on Channel c_i.

Here, there will never be more than one program that are broadcast on the same channel at the same time.

When the recorder is recording a channel from time S to time T (including time S but not T), it cannot record other channels from time S-0.5 to time T (including time S-0.5 but not T).

Find the minimum number of recorders required to record the channels so that all the N programs are completely recorded.

Constraints

1≤N≤10^5

1≤C≤30

1≤s_i<t_i≤10^5

1≤c_i≤C

If c_i=c_j and i≠j, either t_i≤s_j or s_i≥t_j.

All input values are integers.

Input

Input is given from Standard Input in the following format:

N C
s_1 t_1 c_1
:
s_N t_N c_N

Output

When the minimum required number of recorders is x, print the value of x.

Sample Input 1

3 2
1 7 2
7 8 1
8 12 1

Sample Output 1

2

Two recorders can record all the programs, for example, as follows:

With the first recorder, record Channel 2 from time 1 to time 7. The first program will be recorded. Note that this recorder will be unable to record other channels from time 0.5 to time 7.

With the second recorder, record Channel 1 from time 7 to time 12. The second and third programs will be recorded. Note that this recorder will be unable to record other channels from time 6.5 to time 12.

Sample Input 2

3 4
1 3 2
3 4 4
1 4 3

Sample Output 2

3

There may be a channel where there is no program to record.

Sample Input 3

9 4
56 60 4
33 37 2
89 90 3
32 43 1
67 68 3
49 51 3
31 32 3
70 71 1
11 12 3

Sample Output 3

2


program h1B
    implicit none
    integer(8)::i,N,a_seq,cursor
    integer(8)::flg=0
    character(50)::S
    character(5)::mask="KIHBR"
    read(5,*)S
    N=len_trim(S)
    a_seq=0
    cursor=1

    do i=1,N
        if(S(i:i)/="A")then
            a_seq=0
            if(cursor==6)then
                flg=1
                exit
            end if

            if(S(i:i)==mask(cursor:cursor))then
                cursor=cursor+1
            else
                flg=1
                exit
            end if
        else
            a_seq=a_seq+1
            if(a_seq==2)then
                flg=1
                exit
            end if
        end if
    end do

    if(cursor/=6)flg=1

    if(flg==1)then
        print'(A)',"NO"
    else
        print'(A)',"YES"
    end if
end program h1B

Score : 300 points

Problem Statement

You are given a string S.

Takahashi can insert the character A at any position in this string any number of times.

Can he change S into AKIHABARA?

Constraints

1 \leq |S| \leq 50

S consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If it is possible to change S into AKIHABARA, print YES; otherwise, print NO.

Sample Input 1

KIHBR

Sample Output 1

YES

Insert one A at each of the four positions: the beginning, immediately after H, immediately after B and the end.

Sample Input 2

AKIBAHARA

Sample Output 2

NO

The correct spell is AKIHABARA.

Sample Input 3

AAKIAHBAARA

Sample Output 3

NO


program phobia
  implicit none
  character(len=100000) :: s
  integer :: ca, cb, cc
  integer :: i

  ca = 0
  cb = 0
  cc = 0

  read(*,*) s

  do i = 1, len_trim(s)
     if (s(i:i) == 'a') then
        ca = ca + 1
     else if (s(i:i) == 'b') then
        cb = cb + 1
     else if (s(i:i) == 'c') then
        cc = cc + 1
     end if
  end do

  if ( max(abs(ca-cb) ,abs(cb-cc) ,abs(cc-ca) ) <= 1 ) then
     write(*,'(a)') 'YES'
  else
     write(*,'(a)') 'NO'
  end if
  stop
end program phobia

Score : 400 points

Problem Statement

Snuke has a string S consisting of three kinds of letters: a, b and c.

He has a phobia for palindromes, and wants to permute the characters in S so that S will not contain a palindrome of length 2 or more as a substring. Determine whether this is possible.

Constraints

1 \leq |S| \leq 10^5

S consists of a, b and c.

Input

Input is given from Standard Input in the following format:

S

Output

If the objective is achievable, print YES; if it is unachievable, print NO.

Sample Input 1

abac

Sample Output 1

YES

As it stands now, S contains a palindrome aba, but we can permute the characters to get acba, for example, that does not contain a palindrome of length 2 or more.

Sample Input 2

aba

Sample Output 2

NO

Sample Input 3

babacccabab

Sample Output 3

YES


program ABC_079_B_LucasNumber
   implicit none
   integer N, i
   integer(8) L(0: 86)
   read(*, *) N
   L(0) = 2
   L(1) = 1
   do i = 2, N
       L(i) = L(i-1) + L(i-2)
   end do
   write(*, *) L(N)
end program ABC_079_B_LucasNumber

Score : 200 points

Problem Statement

It is November 18 now in Japan. By the way, 11 and 18 are adjacent Lucas numbers.

You are given an integer N. Find the N-th Lucas number.

Here, the i-th Lucas number L_i is defined as follows:

L_0=2

L_1=1

L_i=L_{i-1}+L_{i-2} (i≥2)

Constraints

1≤N≤86

It is guaranteed that the answer is less than 10^{18}.

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the N-th Lucas number.

Sample Input 1

5

Sample Output 1

11

L_0=2

L_1=1

L_2=L_0+L_1=3

L_3=L_1+L_2=4

L_4=L_2+L_3=7

L_5=L_3+L_4=11

Thus, the 5-th Lucas number is 11.

Sample Input 2

86

Sample Output 2

939587134549734843


implicit none
integer(8):: n,i
integer(8),dimension(86):: l

l(1)=1
l(2)=3

read(5,*) n
do i=2,n-1
    l(i+1)=l(i)+l(i-1)
enddo
write(6,*) l(n)

end


program main
	implicit none
	integer i,n
	integer(8) luc(0:86)
	luc(0)=2
	luc(1)=1
	read(*,*)n
	do i=2,n
		luc(i)=luc(i-1)+luc(i-2)
	enddo
	write(*,*)luc(n)
end program main


Score : 300 points

Problem Statement

Sitting in a station waiting room, Joisino is gazing at her train ticket.

The ticket is numbered with four digits A, B, C and D in this order, each between 0 and 9 (inclusive).

In the formula A op1 B op2 C op3 D = 7, replace each of the symbols op1, op2 and op3 with + or - so that the formula holds.

The given input guarantees that there is a solution. If there are multiple solutions, any of them will be accepted.

Constraints

0≤A,B,C,D≤9

All input values are integers.

It is guaranteed that there is a solution.

Input

Input is given from Standard Input in the following format:

ABCD

Output

Print the formula you made, including the part =7.

Use the signs + and -.

Do not print a space between a digit and a sign.

Sample Input 1

1222

Sample Output 1

1+2+2+2=7

This is the only valid solution.

Sample Input 2

0290

Sample Output 2

0-2+9+0=7

0 - 2 + 9 - 0 = 7 is also a valid solution.

Sample Input 3

3242

Sample Output 3

3+2+4-2=7


program r20718h

implicit none
character(len=1) :: x, y

read *, x, y

if(ichar(x) == ichar(y)) then
 print *, '='
else if(ichar(x) > ichar(y)) then
 print *, '>'
else
 print *, '<'
end if

end program r20718h

Score : 100 points

Problem Statement

In programming, hexadecimal notation is often used.

In hexadecimal notation, besides the ten digits 0, 1, ..., 9, the six letters A, B, C, D, E and F are used to represent the values 10, 11, 12, 13, 14 and 15, respectively.

In this problem, you are given two letters X and Y. Each X and Y is A, B, C, D, E or F.

When X and Y are seen as hexadecimal numbers, which is larger?

Constraints

Each X and Y is A, B, C, D, E or F.

Input

Input is given from Standard Input in the following format:

X Y

Output

If X is smaller, print <; if Y is smaller, print >; if they are equal, print =.

Sample Input 1

A B

Sample Output 1

<

10 < 11.

Sample Input 2

E C

Sample Output 2

>

14 > 12.

Sample Input 3

F F

Sample Output 3

=

15 = 15.


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

    print'(i0)',(1900*M+100*(N-M))*2**M

end program ABC078C

Score : 300 points

Problem Statement

Takahashi is now competing in a programming contest, but he received TLE in a problem where the answer is YES or NO.

When he checked the detailed status of the submission, there were N test cases in the problem, and the code received TLE in M of those cases.

Then, he rewrote the code to correctly solve each of those M cases with 1/2 probability in 1900 milliseconds, and correctly solve each of the other N-M cases without fail in 100 milliseconds.

Now, he goes through the following process:

Submit the code.

Wait until the code finishes execution on all the cases.

If the code fails to correctly solve some of the M cases, submit it again.

Repeat until the code correctly solve all the cases in one submission.

Let the expected value of the total execution time of the code be X milliseconds. Print X (as an integer).

Constraints

All input values are integers.

1 \leq N \leq 100

1 \leq M \leq {\rm min}(N, 5)

Input

Input is given from Standard Input in the following format:

N M

Output

Print X, the expected value of the total execution time of the code, as an integer. It can be proved that, under the constraints in this problem, X is an integer not exceeding 10^9.

Sample Input 1

1 1

Sample Output 1

3800

In this input, there is only one case. Takahashi will repeatedly submit the code that correctly solves this case with 1/2 probability in 1900 milliseconds.

The code will succeed in one attempt with 1/2 probability, in two attempts with 1/4 probability, and in three attempts with 1/8 probability, and so on.

Thus, the answer is 1900 \times 1/2 + (2 \times 1900) \times 1/4 + (3 \times 1900) \times 1/8 + ... = 3800.

Sample Input 2

10 2

Sample Output 2

18400

The code will take 1900 milliseconds in each of the 2 cases, and 100 milliseconds in each of the 10-2=8 cases. The probability of the code correctly solving all the cases is 1/2 \times 1/2 = 1/4.

Sample Input 3

100 5

Sample Output 3

608000


program main
	implicit none
	integer n,m,ans
	read(*,*)n,m
	n=n-m
	ans=(n*100+m*1900)*2**m
	write(*,*)ans
end program main


program ABC077A
    implicit none
    character(3)::C1,C2
    read(5,*)C1,C2

    if(C1(1:1)==C2(3:3) .and. C1(2:2)==C2(2:2) .and. C1(3:3)==C2(1:1))then
        print'(A)',"YES"
    else
        print'(A)',"NO"
    end if

end program ABC077A

Score : 100 points

Problem Statement

You are given a grid with 2 rows and 3 columns of squares.
The color of the square at the i-th row and j-th column is represented by the character C_{ij}.

Write a program that prints YES if this grid remains the same when rotated 180 degrees, and prints NO otherwise.

Constraints

C_{i,j}(1 \leq i \leq 2, 1 \leq j \leq 3) is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

C_{11}C_{12}C_{13}
C_{21}C_{22}C_{23}

Output

Print YES if this grid remains the same when rotated 180 degrees; print NO otherwise.

Sample Input 1

pot
top

Sample Output 1

YES

This grid remains the same when rotated 180 degrees.

Sample Input 2

tab
bet

Sample Output 2

NO

This grid does not remain the same when rotated 180 degrees.

Sample Input 3

eye
eel

Sample Output 3

NO


program main
	integer n
	read(*,*)n
	write(*,*)(int(sqrt(real(n))))**2
end program main

Score : 200 points

Problem Statement

Find the largest square number not exceeding N. Here, a square number is an integer that can be represented as the square of an integer.

Constraints

1 \leq N \leq 10^9

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the largest square number not exceeding N.

Sample Input 1

10

Sample Output 1

9

10 is not square, but 9 = 3 × 3 is. Thus, we print 9.

Sample Input 2

81

Sample Output 2

81

Sample Input 3

271828182

Sample Output 3

271821169