module mod_modint
  implicit none
  integer(8) :: modulus = 10_8**9+7
  type modint
    private
    integer(8) :: num
  contains
    procedure :: get => getnum
  end type

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

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

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

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

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

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

  interface inv
    module procedure :: invm
  end interface inv

contains

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

end module mod_modint
program c
   use mod_modint
   implicit none
   integer::N
   integer::a,b
   type(modint),allocatable,dimension(:,:)::f,DP
   type(modint),allocatable,dimension(:)::g
   type(modint)::w,ANS,tmp
   integer::i,j,k
   read*,N,a,b
   if(a>b)then
      a=xor(a,b)
      b=xor(a,b)
      a=xor(a,b)
   endif
   allocate(f(0:N,2),DP(0:N,2))
   allocate(g(0:N))

   f=0
   DP=0
   ANS=0

   if(b>=2)then
      f(0,1)=1;f(0,2)=1
      do i=1,N
         do j=1,2
            do k=1,i
               if(j==1.and.k<a)cycle
               f(i,j)=f(i,j)+f(i-k,mod(j,2)+1)
            end do
         end do
      end do
      g(0)=1
      do i=1,N
         g(i)=f(i,1)+f(i,2)
      end do
      do i=1,N-1
         if(i<a)dp(i,1)=dp(i,1)+1
         do j=1,min(i,a)-1
            dp(i,1)=dp(i,1)+dp(i-j,2)
         end do
         if(i<b)dp(i,2)=dp(i,2)+g(i-1)
         do j=1,min(i,b)-1
            w=1
            if(j/=1)w=g(j-2)
            dp(i,2)=dp(i,2)+dp(i-j,1)*w
         end do
         if(n-i<b)ans=ans+dp(i,1)*g(n-i-1)
         if(n-i<a)ans=ans+dp(i,2)
      end do
   endif
   tmp=2
   tmp=tmp**N
   print*,getnum(tmp-ans)

end program c


program prob67
    implicit none
    integer :: a,b
    read(*,*) a,b
    write(*,*) a*b

    stop
contains
end program prob67

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

Score : 100 points

Problem Statement

Compute A \times B.

Constraints

1 \leq A \leq 100

1 \leq B \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the value A \times B as an integer.

Sample Input 1

2 5

Sample Output 1

10

We have 2 \times 5 = 10.

Sample Input 2

100 100

Sample Output 2

10000

Submitted source-code file size, in bytes.

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

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


program main
	implicit none
    real A, B, C
    read(*,*)A, B
    C = A*B
    write(*,*)C
end program main


program maina

 implicit none
 integer::a,b,ans

 read*,a,b

 ans=a*b

 print*,ans

end program maina


program ABC169_D
  integer(8)::M,O,k=2,l=1,ans=0
  integer(8),allocatable::A(:)
  double precision::N
  read(*,*)N
  O=dint(sqrt(N))
  allocate(A(0:O))
  A=0
  M=dint(N)
  do while(M>1)
     if(mod(M,k)==0) then
        A(k)=A(k)+1
        M=M/k
     else
        k=k+1
        if(k>O) then
           if(sum(A)==0) A(0)=1
           exit
        end if
     end if
  end do
  do i=0,O
     do while(A(i)-l>=0)
        ans=ans+1
        A(i)=A(i)-l
        l=l+1
     end do
     l=1
  end do
  write(*,*)ans
end program ABC169_D

Score : 400 points

Problem Statement

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

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

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

z divides N;

z is different from all integers chosen in previous operations.

Then, replace N with N/z.

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

Constraints

All values in input are integers.

1 \leq N \leq 10^{12}

Input

Input is given from Standard Input in the following format:

N

Output

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

Sample Input 1

24

Sample Output 1

3

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

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

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

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

Sample Input 2

1

Sample Output 2

0

We cannot apply the operation at all.

Sample Input 3

64

Sample Output 3

3

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

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

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

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

Sample Input 4

1000000007

Sample Output 4

1

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

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

Sample Input 5

997764507000

Sample Output 5

7


program e
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: n,i
    integer(int32), allocatable:: a(:),b(:)

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

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

    if (mod(n,2)==1) then
        print'(i0)', b(n/2+1) - a(n/2+1) + 1
    else
        print'(i0)', b(n)+b(n-1) - (a(1)+a(2))+1
    end if

contains
        recursive subroutine merge_sort(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 swap(ar(fst),ar(lst))
            return
        end if

        mdl = (fst+lst)/2
        call merge_sort(ar, fst, mdl)
        call merge_sort(ar, mdl+1, lst)
        call merge_(ar, fst, mdl, lst)
    end subroutine

    subroutine merge_(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 swap(x,y)
        integer(int32),intent(inout):: x,y
        integer(int32):: tmp
        tmp = x
        x = y
        y = tmp
    end subroutine
end program e

Score : 500 points

Problem Statement

There are N integers X_1, X_2, \cdots, X_N, and we know that A_i \leq X_i \leq B_i.
Find the number of different values that the median of X_1, X_2, \cdots, X_N can take.

Notes

The median of X_1, X_2, \cdots, X_N is defined as follows. Let x_1, x_2, \cdots, x_N be the result of sorting X_1, X_2, \cdots, X_N in ascending order.

If N is odd, the median is x_{(N+1)/2};

if N is even, the median is (x_{N/2} + x_{N/2+1}) / 2.

Constraints

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

1 \leq A_i \leq B_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
A_2 B_2
:
A_N B_N

Output

Print the answer.

Sample Input 1

2
1 2
2 3

Sample Output 1

3

If X_1 = 1 and X_2 = 2, the median is \frac{3}{2};

if X_1 = 1 and X_2 = 3, the median is 2;

if X_1 = 2 and X_2 = 2, the median is 2;

if X_1 = 2 and X_2 = 3, the median is \frac{5}{2}.

Thus, the median can take three values: \frac{3}{2}, 2, and \frac{5}{2}.

Sample Input 2

3
100 100
10 10000
1 1000000000

Sample Output 2

9991


program prob30
    implicit none
    integer :: a,b,c,d,e,f
    read(*,*) a,b,c,d,e
    f = (c-a)*60 +(d-b)

    write(*,*) f - e

    stop
contains
end program prob30

Score : 100 points

Problem Statement

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

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

Constraints

0 \le H_1, H_2 \le 23

0 \le M_1, M_2 \le 59

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

K \ge 1

Takahashi is up for at least K minutes.

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

Input

Input is given from Standard Input in the following format:

H_1 M_1 H_2 M_2 K

Output

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

Sample Input 1

10 0 15 0 30

Sample Output 1

270

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

Sample Input 2

10 0 12 0 120

Sample Output 2

0

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


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

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

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

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


program folia
  implicit none
  integer :: n, i
  integer(8), dimension(0:100001) :: a = 0, b = 0, s = 0
  read(*,*) n
  read(*,*) a(0:n)
  do i = n, 0, -1
    s(i) = s(i + 1) + a(i)
  end do
  b(0) = 1 - a(0)
  do i = 1, n
    b(i) = min(b(i - 1) * 2 - a(i), s(i + 1))
    if (b(i) < max(b(i - 1) - a(i), 0)) then
      write(*,'(i0)') -1
      stop
    end if
  end do
  write(*,'(i0)') s(0) + sum(b(0:n))
end program folia

Score : 600 points

Problem Statement

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

Notes

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

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

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

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

Constraints

0 \leq N \leq 10^5

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

A_N \geq 1

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_0 A_1 A_2 \cdots A_N

Output

Print the answer as an integer.

Sample Input 1

3
0 1 1 2

Sample Output 1

7

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

Sample Input 2

4
0 0 1 0 2

Sample Output 2

10

Sample Input 3

2
0 3 1

Sample Output 3

-1

Sample Input 4

1
1 1

Sample Output 4

-1

Sample Input 5

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

Sample Output 5

264


program c
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: n,i,ans
    integer(int64), allocatable:: a(:),node(:)
    integer(int64):: ma(0:58)
    read*, n
    allocate(a(0:n), node(0:n))
    read*, a(:)
    node(n) = a(n)
    ma(0) = 1

    do i=1,min(58_8,n)
        ma(i) = max(min(2**i-a(i), 2*ma(i-1)-a(i)),0_8)
    end do
    ! print*, ma(0:min(58,n))
    do i=n-1,0,-1
        if ( i > 58) then
            node(i) = node(i+1)+a(i)
        else
            if (ma(i) < node(i+1)/2+mod(node(i+1),2_8)) then
                print'(i0)', -1
                stop
            end if
            node(i) = min(ma(i),node(i+1))+a(i)
        end if
    end do
    if (node(0) > 1) then
        print'(i0)', -1
    else
        ans=0
        do i=0,n
            ans=ans+node(i)
        end do
        print'(i0)', ans
    end if
end program c


program NOMURA2020C
  integer(8)::N,i
  integer(8),allocatable::A(:),k(:),l(:)
  read(*,*)N
  allocate(A(0:N),k(0:N),l(0:N))
  read(*,*)A
  if(A(0)==0) then
     k(0)=1_8
  else
     k(0)=0_8
  end if
  do i=1,N
     k(i)=k(i-1)*2_8-A(i)
     if(k(i)<0) then
        write(*,*)'-1'
        stop
     end if
  end do
  l(N)=A(N)
  do i=N-1_8,0_8,-1_8
     l(i)=l(i+1)+A(i)
     if(l(i)>k(i)+A(i)) l(i)=k(i)+A(i)
  end do
  write(*,*) sum(l)
end program NOMURA2020C


program c
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: n,i,j
    integer(int64), allocatable:: a(:),node(:)
    integer(int64):: ma(0:50)
    read*, n
    allocate(a(0:n), node(0:n))
    read*, a(:)
    node(n) = a(n)
    ma(0) = 1
    do i=1,min(50,n)
        ma(i) = min(2**i-a(i), 2*ma(i-1)-a(i))
    end do

    do i=n-1,0,-1
        if ( i > 50) then
            node(i) = node(i+1)+a(i)
        else
            if (ma(i) < node(i+1)/2+mod(node(i+1),2)) then
                print'(i0)', -1
                stop
            end if
            node(i) = min(ma(i),node(i+1))+a(i)
        end if
    end do

    if (node(0) > 1) then
        print'(i0)', -1
    else
        print'(i0)', sum(node)
    end if
end program c


program main
integer N,I
character H*3
read *, N
I = mod(N,10)
Select case(I)
	case(2,4,5,7,9)
    	H = 'hon'
    case(0,1,6,8)
		H = 'pon'
    case(3)
    	H = 'bon'
end select
print '(A)',H
end program main

Score: 100 points

Problem Statement

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

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

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

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

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

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

Constraints

N is a positive integer not exceeding 999.

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

16

Sample Output 1

pon

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

Sample Input 2

2

Sample Output 2

hon

Sample Input 3

183

Sample Output 3

bon


program main
	implicit none
	integer(8):: N,i,j,t,B,k
	read *, N
    !read(*,*) (A(i), i = 1, N)

	i=N/100
	j=N/10
	k=N-i*100-j*10
	if(k==2)write(*,*)"hon"
	if(k==4)write(*,*)"hon"
	if(k==5)write(*,*)"hon"
	if(k==7)write(*,*)"hon"
	if(k==9)write(*,*)"hon"
	if(k==0)write(*,*)"pon"
	if(k==1)write(*,*)"pon"
	if(k==6)write(*,*)"pon"
	if(k==8)write(*,*)"pon"
	if(k==3)write(*,*)"bon"

end program main


program main
  implicit none
  integer i, N, ans1
  !character(11) S, T
  read(*, *) N
  i = mod(N,10)
  if (i == 0) then
    write(*, *) 'pon'
  elseif (i == 1) then
    write(*, *) 'pon'
  elseif (i == 6) then
    write(*, *) 'pon'
  elseif (i == 8) then
    write(*, *) 'pon'
  elseif (i == 3) then
    write(*, *) 'bon'
  else
    write(*, *) 'hon'
  end if

  !write(*, *)
end program main


program therefore
  implicit none
  integer :: n
  read(*,*) n
  select case(mod(n,10))
  case (3)
    write(*,'(a)') 'bon'
  case (0, 1, 6, 8)
    write(*,'(a)') 'pon'
  case default
    write(*,'(a)') 'hon'
  end select
end program therefore


character(205)::S
read*, K
read*, S
if(len_trim(S) <= K) then
    write(*,'(a)') trim(adjustl(S))
else
    write(*,'(2a)') trim(adjustl(S(1:K))), "..."
end if
end

Score: 200 points

Problem Statement

We have a string S consisting of lowercase English letters.

If the length of S is at most K, print S without change.

If the length of S exceeds K, extract the first K characters in S, append ... to the end of them, and print the result.

Constraints

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

S is a string consisting of lowercase English letters.

The length of S is between 1 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

K
S

Output

Print a string as stated in Problem Statement.

Sample Input 1

7
nikoandsolstice

Sample Output 1

nikoand...

nikoandsolstice has a length of 15, which exceeds K=7.

We should extract the first 7 characters in this string, append ... to the end of them, and print the result nikoand....

Sample Input 2

40
ferelibenterhominesidquodvoluntcredunt

Sample Output 2

ferelibenterhominesidquodvoluntcredunt

The famous quote from Gaius Julius Caesar.


program main
character S*100
integer K,K2,I

read *,K
read *,S
I = K + 1
K2 = len_trim(S)
if( K <= K2 ) then
  S(I:I) = "."
  S(I+1:I+1) = "."
  S(I+2:I+2) = "."
  S(I+3:K2) = ""
end if
print '(A)', trim(S)
end program main


      PROGRAM Colon
      IMPLICIT NONE
      integer,parameter :: p = 8
      real(p),parameter :: pi=3.1415926535897932384626433832795028
   !   real(p) :: pi = atan(real(1_16))*4
      real(p) :: a,b,h,m, ans2
      real(p) :: angleA,angleB,angleSub
      real(p) :: xa,ya,xb,yb

      read*,a,b,h,m
      h = h + m/60d0

      angleA = h/12d0*2*pi
      angleB = m/60d0*2*pi

      xa = a*sin(angleA)
      ya = a*cos(angleA)
      xb = b*sin(angleB)
      yb = b*cos(angleB)

      print*, sqrt( (xa-xb)**2 + (ya-yb)**2 )

      END PROGRAM

Score: 300 points

Problem Statement

Consider an analog clock whose hour and minute hands are A and B centimeters long, respectively.

An endpoint of the hour hand and an endpoint of the minute hand are fixed at the same point, around which each hand rotates clockwise at constant angular velocity. It takes the hour and minute hands 12 hours and 1 hour to make one full rotation, respectively.

At 0 o'clock, the two hands overlap each other. H hours and M minutes later, what is the distance in centimeters between the unfixed endpoints of the hands?

Constraints

All values in input are integers.

1 \leq A, B \leq 1000

0 \leq H \leq 11

0 \leq M \leq 59

Input

Input is given from Standard Input in the following format:

A B H M

Output

Print the answer without units. Your output will be accepted when its absolute or relative error from the correct value is at most 10^{-9}.

Sample Input 1

3 4 9 0

Sample Output 1

5.00000000000000000000

The two hands will be in the positions shown in the figure below, so the answer is 5 centimeters.

Sample Input 2

3 4 10 40

Sample Output 2

4.56425719433005567605

The two hands will be in the positions shown in the figure below. Note that each hand always rotates at constant angular velocity.


program main
    implicit none
    real(8) a, b, h, m
    real(8) the_a, the_b
    real(8) xa, ya, xb, yb
    real(8) d
    read(*,*) a, b, h, m

    the_a = (h+m/6d1)/1.2d1*2d0*3.14159265358979d0
    the_b = m/6d1*2d0*3.14159265358979d0

    xa = a*cos(the_a)
    ya = a*sin(the_a)

    xb = b*cos(the_b)
    yb = b*sin(the_b)

    d = sqrt((xa-xb)**2d0 + (ya-yb)**2d0)

    write(*,*) d

end program


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

  type item
    integer :: id = 0                             !番号
    integer(8) :: wt = 0_8                        !距離の合計
 end type item

  type priorityqueue
    integer :: num = 0                            !要素数
    type(item), pointer :: heap(:) => null()      !配列へのポインタの大きさは無指定
  contains
    !final :: finalizei
 end type priorityqueue

  type edge                                       !辺のこと．ここから可変長配列の実装
     integer :: fm = 0, to = 0, cp = 0, rv = 0
     !from,to,cpはff法の辺の容量，rvは逆辺のリスト上のインデックスの情報
    integer(8) :: wt = 0_8                        !重み(距離とか)
 end type edge

  type arraylist                                   !メソッドの実装，辺に番号付け？
    integer :: num = 0
    type(edge), pointer :: arr(:) => null()
  contains
    procedure :: add => adde
    procedure :: bellman_ford => bellman_ford
    !final :: finalizee
 end type arraylist

  type graph                                       !メソッドの実装，辺と番号，使用状況
    type(arraylist), pointer :: egs(:) => null()   !隣接リスト
    logical, pointer :: used(:) => null()          !dfsで使う
  contains                                         !type文の中にcontain文を入れる
    procedure :: add => add                       !typeの外のサブルーチンへのポインタ
    procedure :: dijkstra => dijkstra
    procedure :: ford_fulkerson => ford_fulkerson
    !final :: finalize
 end type graph

  interface graph                          !interfaceは安全に関数を引数として渡す
    module procedure :: newg               !コンストラクタ
  end interface graph

contains

  function newi(id,wt) result(ret)   !idとwtをitem型のretに入れる
    integer, intent(in) :: id
    integer(8), intent(in) :: wt
    type(item) :: ret
    ret%id = id
    ret%wt = wt
  end function newi

  subroutine swapi(a,b)               !item型であるa,bを入れ替える
    type(item), intent(inout) :: a, b
    type(item) :: c
    c = a
    a = b
    b = c
  end subroutine swapi

  subroutine appendi(a)                         !aの配列長をnから2nにする.O(n)かかる
    type(item), pointer, intent(inout) :: a(:)
    integer :: n
    type(item), allocatable :: tmp(:)
    n = size(a)                                 !配列の要素の合計数
    allocate(tmp(n))
    tmp = a
    deallocate(a)
    allocate(a(2*n))
    a(1:n) = tmp
    deallocate(tmp)
  end subroutine appendi

  function lessi(a,b) result(ret)         !bのwtが大きければ真，そうでなければ偽
    type(item), intent(in) :: a, b
    logical :: ret
    ret = a%wt < b%wt
  end function lessi

  subroutine finalizei(pq)               !pqのリセット
    type(priorityqueue), intent(inout) :: pq
    if (associated(pq%heap)) deallocate(pq%heap)
    pq%num = 0
  end subroutine finalizei

  subroutine offer(pq,it)                !item型であるitをpqに追加する
    type(priorityqueue), intent(inout) :: pq
    type(item), intent(in) :: it
    integer :: i, j
    if (.not.associated(pq%heap)) allocate(pq%heap(1)) !参照先がなければ1番目参照
    if (pq%num == size(pq%heap)) call appendi(pq%heap) !配列長をnから2nに
    pq%num = pq%num+1
    pq%heap(pq%num) = it                 !新たな数値は最後尾に
    i = pq%num
    do while (i > 1)   !逆転がなくなるまで上にあげていく(小さいほうが上)
      j = i/2
      if (lessi(pq%heap(i),pq%heap(j))) call swapi(pq%heap(i),pq%heap(j))
      i = j
    end do
  end subroutine offer

  function poll(pq) result(ret)   !一番上のitem型を取り出す(pqから削除する)
    type(priorityqueue), intent(inout) :: pq
    type(item) :: ret
    integer :: n, i, j
    n = pq%num
    ret = pq%heap(1)
    pq%heap(1) = pq%heap(n)
    pq%num = pq%num-1
    i = 1
    do while (2*i < n)   !最後尾のものを一番上に上げたので，逆転がなくなるまで下ろす
      j = 2*i
      if (j+1 < n .and. lessi(pq%heap(j+1),pq%heap(j))) j = j+1
      if (lessi(pq%heap(j),pq%heap(i))) call swapi(pq%heap(j),pq%heap(i))
      i = j
    end do
  end function poll

  function newe(to,wt,fm,cp,rv) result(ret)   !新しいedgeの生成
    integer, intent(in) :: to
    integer(8), intent(in) :: wt
    integer, intent(in), optional :: fm, cp, rv
    !省略可能な因数の利用(fmはbf法，cp,rvはff法で使用)
    type(edge) :: ret
    if (present(fm)) ret%fm = fm
    ret%to = to
    ret%wt = wt
    if (present(cp) .and. present(rv)) then
      ret%cp = cp
      ret%rv = rv
    end if
  end function newe

  subroutine appende(a)                        !配列長をnから2nに
    type(edge), pointer, intent(inout) :: a(:)
    integer :: n
    type(edge), allocatable :: tmp(:)
    n = size(a)
    allocate(tmp(n))
    tmp = a
    deallocate(a)
    allocate(a(2*n))
    a(1:n) = tmp
    deallocate(tmp)
  end subroutine appende

  function lesse(a,b) result(ret)              !bのwtが大きければ真
    type(edge), intent(in) :: a, b
    logical :: ret
    ret = a%wt < b%wt
  end function lesse

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

  subroutine adde(list,e)   !メソッドの処理を書くとき，引数はclass文で宣言.edgeをlistに追加
    class(arraylist), intent(inout) :: list
    type(edge), intent(in) :: e
    if (.not.associated(list%arr)) allocate(list%arr(1))
    if (list%num == size(list%arr)) call appende(list%arr)
    list%num = list%num+1
    list%arr(list%num) = e   !最後尾に追加
  end subroutine adde

  function newg(n) result(ret)   !n番目のgraph型の枠をつくる
    integer, intent(in) :: n
    type(graph) :: ret
    allocate(ret%egs(n))
  end function newg

  subroutine add(g,fm,to,wt,cp)   !graph型にデータを追加
    class(graph), intent(inout) :: g
    integer, intent(in) :: fm, to
    integer, intent(in), optional :: cp
    integer(8) :: wt
    if (present(cp)) then
      call adde(g%egs(fm),newe(to,wt,cp=cp,rv=g%egs(to)%num))
      call adde(g%egs(to),newe(fm,wt,cp=0,rv=g%egs(fm)%num-1))
    else
      call adde(g%egs(fm),newe(to,wt))
    end if
  end subroutine add

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

  function dijkstra(g,s) result(ret)   !ダイクストラ法ABC168D，sはstart
    class(graph), intent(in) :: g
    integer, intent(in) :: s
    integer(8) :: ret(size(g%egs))     !graph中のlistの数だけ配列をつくる．最短距離を格納
    type(priorityqueue) :: pq
    type(item) :: it
    type(edge) :: e
    integer :: i
    ret = inf
    ret(s) = 0_8
    call offer(pq,newi(s,ret(s)))   !pqにstartの番号とret(s)=0からなるitem型を追加
    do while (pq%num > 0)           !すべて吐き出す
      it = poll(pq)                 !一番上のものを取り出す．次に使う頂点
      if (it%wt > ret(it%id)) cycle
      do i = 1, g%egs(it%id)%num
        e = g%egs(it%id)%arr(i)     !取り出したitemのid番目のlistをeとする
        if (ret(e%to) > ret(it%id)+e%wt) then   !今いる場所からの方が近い
          ret(e%to) = ret(it%id)+e%wt   !最短経路の更新
          call offer(pq,newi(e%to,ret(e%to)))
        end if
      end do
    end do
    call finalizei(pq)
  end function dijkstra

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

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

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

  function ford_fulkerson(g,s,t) result(ret)   !最大流，最小カットを求める
    class(graph), intent(inout) :: g
    integer, intent(in) :: s, t                !スタート，ゴール
    integer :: ret
    integer :: f
    ret = 0
    if (associated(g%used) .and. size(g%used) /= size(g%egs)) deallocate(g%used)
    if (.not.associated(g%used)) allocate(g%used(size(g%egs)))
    do
      g%used = .false.
      f = dfs_ff(g,s,t,1000000000)
      if (f == 0) then
        deallocate(g%used)
        return
      end if
      ret = ret+f
    end do
  end function ford_fulkerson

end module mod_graph

program ABC168_D
  use mod_graph
  integer::N,M,A,B
  integer,allocatable::ans(:)
  type(graph)::g
  read(*,*)N,M
  allocate(ans(N))
  ans=0
  g=graph(N)
  do i=1,M
     read(*,*)A,B
     call g%add(A,B,1_8)
     call g%add(B,A,1_8)
  end do
  ans=g%dijkstra(1)
  if(any(ans==inf)) then
     write(*,*)'No'
  else
     write(*,*)'Yes'
     do i=2,N
        do j=1,g%egs(i)%num
           k=g%egs(i)%arr(j)%to
           if(ans(k)==ans(i)-1_8) then
              write(*,*)k
              exit
           end if
        end do
     end do
  end if
end program ABC168_D

Score: 400 points

Problem Statement

There is a cave.

The cave has N rooms and M passages. The rooms are numbered 1 to N, and the passages are numbered 1 to M. Passage i connects Room A_i and Room B_i bidirectionally. One can travel between any two rooms by traversing passages. Room 1 is a special room with an entrance from the outside.

It is dark in the cave, so we have decided to place a signpost in each room except Room 1. The signpost in each room will point to one of the rooms directly connected to that room with a passage.

Since it is dangerous in the cave, our objective is to satisfy the condition below for each room except Room 1.

If you start in that room and repeatedly move to the room indicated by the signpost in the room you are in, you will reach Room 1 after traversing the minimum number of passages possible.

Determine whether there is a way to place signposts satisfying our objective, and print one such way if it exists.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

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

1 \leq A_i, B_i \leq N\ (1 \leq i \leq M)

A_i \neq B_i\ (1 \leq i \leq M)

One can travel between any two rooms by traversing passages.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
:
A_M B_M

Output

If there is no way to place signposts satisfying the objective, print No.

Otherwise, print N lines. The first line should contain Yes, and the i-th line (2 \leq i \leq N) should contain the integer representing the room indicated by the signpost in Room i.

Sample Input 1

4 4
1 2
2 3
3 4
4 2

Sample Output 1

Yes
1
2
2

If we place the signposts as described in the sample output, the following happens:

Starting in Room 2, you will reach Room 1 after traversing one passage: (2) \to 1. This is the minimum number of passages possible.

Starting in Room 3, you will reach Room 1 after traversing two passages: (3) \to 2 \to 1. This is the minimum number of passages possible.

Starting in Room 4, you will reach Room 1 after traversing two passages: (4) \to 2 \to 1. This is the minimum number of passages possible.

Thus, the objective is satisfied.

Sample Input 2

6 9
3 4
6 1
2 4
5 3
4 6
1 5
6 2
4 5
5 6

Sample Output 2

Yes
6
5
5
1
1

If there are multiple solutions, any of them will be accepted.


      MODULE intQueueMod
      implicit none
! Module for QUEUE : First in, First out
! reference : プログラミングコンテスト攻略のための
!              アルゴリズムとデータ構造

      integer(16),parameter :: binSize = 10**6
      integer(16),parameter :: precision = 16

      type intQueue
        private
        integer(precision) :: bin( 1:binSize ) = 0
        integer(precision) :: head = 1,   tail = 1
        !head : Where the head value is.
        !tail : Where the blank next to tail is.
        contains
        procedure,public,pass :: initialize => intInit
        procedure,public,pass :: isEmpty    => intIsEmpty
        procedure,public,pass :: isFull     => intIsFull
        procedure,public,pass :: enQueue    => intEnQ
        procedure,public,pass :: deQueue    => intDeQ
      end type

      contains
!Reset queue. Not nessesary in first time.
      pure subroutine intInit( q )
        implicit none
        class(intQueue),intent(inout) :: q

        q%head = 1
        q%tail = 1
      end subroutine intInit

!Check status
      pure function intIsEmpty( q ) result( ans )
        implicit none
        class(intQueue),intent(in) :: q
        logical :: ans

        ans = q%head==q%tail
      end function intIsEmpty

      pure function intIsFull( q ) result( ans )
        implicit none
        class(intQueue),intent(in) :: q
        logical :: ans

        ans = q%head==mod( q%tail+1,binSize )
      end function intIsFull

!set x to tail
      subroutine intEnQ( q,x )
        implicit none
        class(intQueue)      ,intent(inout) :: q
        integer(precision),intent(in   ) :: x

        if( q%isFull() )then
          print*,'Queue is full,Error!';stop
        else
          q%bin(q%tail) = x
          if( q%tail==binSize )then
            q%tail = 1
          else
            q%tail = q%tail + 1
          end if
        end if
      end subroutine intEnQ

!get x from head
      impure function intDeQ( q ) result( x )
        implicit none
        class(intQueue),intent(inout) :: q
        integer(precision) :: x

        if( q%isEmpty() )then
          print*,'Stack is empty, Error!';stop
        else
          x = q%bin(q%head)
          if( q%head==binSize )then
            q%head = 1
          else
            q%head = q%head + 1
          end if
        end if
      end function intDeQ
      END MODULE intQueueMod

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

      module piyoMod
      use intQueueMod

      type rooms
        integer(16),allocatable :: nei(:)
        integer(16) :: numNei=0
      end type

      integer(16),allocatable :: flag(:)
      type(intQueue) :: nextQ,beforeQ

      contains
      subroutine solve( rIn,roomNum,before )
        implicit none
        type(rooms),intent(in),allocatable :: rIn(:)
        integer(16),intent(in) :: roomNum,before
        integer(16) :: i

        if( flag(roomNum)/=-1 ) return
        flag( roomNum ) = before
        if( rIn(roomNum)%numNei==0 ) return

        do i = 1,rIn(roomNum)%numNei
          call nextQ%enQueue( rIn(roomNum)%nei(i) )
          call beforeQ%enQueue( roomNum )
        end do

      end subroutine
      end module

      PROGRAM doubleDots
      use piyoMod
      IMPLICIT NONE
      integer(16) :: n,m,numNei
      integer(16),allocatable :: a(:),b(:)
      type(rooms),allocatable :: room(:)
      integer(16) :: i
      integer(16),allocatable :: buffer(:)

      read*,n,m
      allocate( a(m),b(m),room(n),flag(n) )
      flag(1:n) = -1
      do i = 1,m
        read*,a(i),b(i)
      end do

      do i = 1,m
        room(a(i))%numNei = room(a(i))%numNei + 1
        numNei = room(a(i))%numNei
        allocate( buffer(numNei) )
        buffer(1:numNei-1) = room(a(i))%nei
        if(allocated(room(a(i))%nei) )deallocate( room(a(i))%nei )
        buffer(numNei) = b(i)
        room(a(i))%nei = buffer(1:numNei)
        deallocate( buffer )

        room(b(i))%numNei = room(b(i))%numNei + 1
        numNei = room(b(i))%numNei
        allocate( buffer(numNei) )
        buffer(1:numNei-1) = room(b(i))%nei
        if(allocated(room(b(i))%nei) )deallocate( room(b(i))%nei )
        buffer(numNei) = a(i)
        room(b(i))%nei = buffer(1:numNei)
        deallocate( buffer )
      end do

      call nextQ%enQueue(1_16)
      call beforeQ%enQueue(0_16) !dummy
      do while( .true. )
        if( nextQ%isEmpty() ) exit

        call solve( room,nextQ%deQueue(),beforeQ%deQueue() )
      end do

      if( count(flag(2:n)==-1) >=1 )then
        print*,'No';stop
      end if

      print*,'Yes'
      do i = 2,n
        print*,flag(i)
      end do

   !   print*,n,m
   !   do i = 1,m
   !     print*,a(i),b(i)
   !   end do

   !   do i= 1,n
   !     print*,'room',i,room(i)%numNei
   !     print*,room(i)%nei
   !   end do

      END PROGRAM


module graph_mod
    implicit none
    type node
        integer(4),allocatable:: to(:)
        integer(4):: l
    end type

contains
    subroutine init(n)
        type(node):: n
        allocate(n%to(1))

        n%l = 0
    end subroutine

    subroutine regist(n,m)
        type(node):: n
        integer(4):: m

        if (n%l+1 > size(n%to)) call add_(n)
        n%l=n%l+1
        n%to(n%l) = m
    end subroutine

    subroutine add_(n)
        type(node):: n
        integer(4),allocatable:: tmp(:)
        integer(4):: l

        l = size(n%to)
        allocate(tmp(l))
        tmp(:) = n%to(:)
        deallocate(n%to)
        allocate(n%to(l*2))
        n%to(1:l) = tmp(:)
        deallocate(tmp)
    end subroutine
end module

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

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

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

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

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

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

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

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

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

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

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

program main
    use,intrinsic :: iso_fortran_env
    use graph_mod
    use deque_mod
    implicit none
    integer(int32):: n,m,i,j,next,dst
    integer(int32), allocatable:: dist(:), from(:)
    integer(int32), allocatable:: a(:),b(:)
    type(node),allocatable:: g(:)
    type(deque):: task

    read*, n,m
    allocate(a(m),b(m),g(n),dist(n),from(n))
    do i=1,n
        call init(g(i))
    end do

    do i=1,m
        read*, a(i), b(i)
        call regist(g(a(i)),b(i))
        call regist(g(b(i)),a(i))
    end do
    call init_deque(task)
    call append(task,1)
    dist(:) = -1
    dist(1) = 0
    from(:) = -1

    do while (remain(task))
        next = popleft(task)
        do i=1,g(next)%l
            dst = g(next)%to(i)
            if (dist(dst) == -1 .or. dist(dst) > dist(next)+1) then
                call append(task, g(next)%to(i))
                dist(dst) = dist(next)+1
                from(dst) = next
            end if
        end do
    end do

    do i=1,n
        if (dist(i) == -1) then
            print'(a)', 'No'
            stop
        end if
    end do

    print'(a)', 'Yes'
    do i=2,n
        print'(i0)', from(i)
    end do

end program main


program abc168e
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: n,i
    integer(int64):: a_zero,b_zero,cp,cm,czz,add
    integer(int64):: nakawaru
    integer(int64):: pi,mi,g
    integer(int64):: dp,dm,ans
    integer(int64), parameter:: md=1000000007
    integer(int64), allocatable:: a(:),b(:)
    integer(int64), allocatable:: pa(:),pb(:)
    integer(int64), allocatable:: ma(:),mb(:)

    read*, n
    allocate(a(n),b(n))
    cp=0
    cm=0
    b_zero=0
    a_zero=0
    czz=0
    do i=1,n
        read*, a(i),b(i)
    end do

    do i=1,n
        if (a(i)==0 .and. b(i)==0)then
            ! count zero_zero
            czz=czz+1
        else if (a(i) == 0) then
            ! count zero_nonzero
            a_zero=a_zero+1
        else if (b(i) == 0) then
            ! count nonzero_zero
            b_zero=b_zero+1
        else if (a(i) > 0 .eqv. b(i) > 0) then
            ! count a,b >0 , a,b <0
            cp=cp+1
        else
            cm=cm+1
        end if
    end do
    ! count a>0>b b>0>a
    allocate(pa(cp),pb(cp))
    allocate(ma(cm),mb(cm))
    pi=1
    mi=1
    ans = mod((pow2(a_zero)+pow2(b_zero)-1),md)

    do i=1,n
        if (a(i)==0 .or. b(i)==0) cycle
        ! nonzero a,b
        g = abs(gcd(a(i),b(i)))
        if (a(i) > 0 .eqv. b(i) > 0) then
            pa(pi)=a(i)/g
            pb(pi)=b(i)/g
            pi=pi+1
        else
            ma(mi)=abs(a(i))/g
            mb(mi)=abs(b(i))/g
            mi=mi+1
        end if
    end do

    if (cp > 0) then
        call double_sort(pb,pa,1_8,cp)
        call double_sort(pa,pb,1_8,cp)
    end if

    if (cm > 0) then
        call double_sort(ma,mb,1_8,cm)
        call double_sort(mb,ma,1_8,cm)
    end if

    pi=1
    mi=1
    do while(pi <= cp .and. mi <= cm)
        if (pa(pi) > mb(mi)) then
            ans=mod(ans*2_8,md)
            mi=mi+1
        else if (pa(pi) < mb(mi)) then
            ans=mod(ans*2_8,md)
            pi=pi+1
        else
            if (pb(pi) > ma(mi)) then
                ans=mod(ans*2_8,md)
                mi=mi+1
            else if (pb(pi) < ma(mi)) then
                ans=mod(ans*2_8,md)
                pi=pi+1
            else
                dp=0
                dm=0
                do while(pb(pi)==pb(pi+dp) .and. pa(pi)==pa(pi+dp))
                    dp=dp+1
                    if (pi+dp > cp) exit
                end do
                do while(ma(mi)==ma(mi+dm) .and. mb(mi)==mb(mi+dm))
                    dm=dm+1
                    if (mi+dm > cm) exit
                end do
                nakawaru = mod(pow2(dp)+pow2(dm)-1,md)
                ans=mod(ans*nakawaru,md)
                pi=pi+dp
                mi=mi+dm
            end if
        end if
    end do

    if (pi <= cp) then
        add = pow2(cp-pi+1)
        ans = mod(ans*add,md)
    else
        add = pow2(cm-mi+1)
        ans = mod(ans*add,md)
    end if
    print'(i0)', mod(ans+czz-1,md)
contains
    function pow2(x) result(ret)
        integer(int64):: x,ret
        ret = factorial(2_8,x,md)
    end function
    function factorial(a,b,md) result(ret)
        integer(int64):: a,b,md
        integer(int64):: aa,bb,ret
        aa = a
        bb = b
        ret = 1
        do while(bb > 0)
            if (mod(bb,2_8)/=0) ret=mod(ret*aa,md)
            aa=mod(aa*aa,md)
            bb=bb/2
        end do
    end function

    recursive function gcd(x,y) result(ret)
        integer(int64):: x,y,ret

        if (mod(x,y) == 0) then
            ret = y
            return
        end if

        ret = gcd(y,mod(x,y))
    end function

    recursive subroutine double_sort(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 swap(ar1(fst), ar1(lst))
                call swap(ar2(fst), ar2(lst))
            end if
            return
        end if

        mdl = (fst+lst)/2

        call double_sort(ar1,ar2,fst,mdl)
        call double_sort(ar1,ar2,mdl+1,lst)
        call double_merge_(ar1,ar2,fst,mdl,lst)
    end subroutine

    subroutine double_merge_(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

    subroutine swap(x,y)
        integer(int64),intent(inout):: x,y
        integer(int64):: tmp
        tmp = x
        x = y
        y = tmp
    end subroutine
end program abc168e

Score: 500 points

Problem Statement

We have caught N sardines. The deliciousness and fragrantness of the i-th sardine is A_i and B_i, respectively.

We will choose one or more of these sardines and put them into a cooler. However, two sardines on bad terms cannot be chosen at the same time.

The i-th and j-th sardines (i \neq j) are on bad terms if and only if A_i \cdot A_j + B_i \cdot B_j = 0.

In how many ways can we choose the set of sardines to put into the cooler? Since the count can be enormous, print it modulo 1000000007.

Constraints

All values in input are integers.

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

-10^{18} \leq A_i, B_i \leq 10^{18}

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
:
A_N B_N

Output

Print the count modulo 1000000007.

Sample Input 1

3
1 2
-1 1
2 -1

Sample Output 1

5

There are five ways to choose the set of sardines, as follows:

The 1-st

The 1-st and 2-nd

The 2-nd

The 2-nd and 3-rd

The 3-rd

Sample Input 2

10
3 2
3 2
-1 1
2 -1
-3 -9
-8 12
7 7
8 1
8 2
8 4

Sample Output 2

479


module module_modint
  use, intrinsic :: iso_fortran_env
  implicit none
  integer(int64) :: modu = 1000000007_8
  type modint
    integer(int64), private :: val = 0_8
  contains
    procedure, private :: asgn => asgn
    procedure, private :: add => add
    procedure, private :: sub => sub
    procedure, private :: mul => mul
    procedure, private :: div => div
    procedure, private :: pow => pow
    procedure, public :: inv => inv
    procedure, public :: get => get
    generic, public :: assignment(=) => asgn
    generic, public :: operator(+) => add
    generic, public :: operator(-) => sub
    generic, public :: operator(*) => mul
    generic, public :: operator(/) => div
    generic, public :: operator(**) => pow
  end type
  interface modint
    module procedure :: new_modint, new_modint_with_num
  end interface modint
  interface operator(+)
    module procedure :: add_r
  end interface
  interface operator(-)
    module procedure :: sub_r
  end interface
  interface operator(*)
    module procedure :: mul_r
  end interface
  interface operator(/)
    module procedure :: div_r
  end interface
  interface dot_product
    module procedure :: dot_prod
  end interface dot_product
  interface matmul
    module procedure :: matmul1, matmul2, matmul3
  end interface matmul
contains
  pure elemental integer(int64) function to_int64(num) result(ret)
    class(*), intent(in) :: num
    select type (num)
    type is (integer(int64))
      ret = num
    type is (integer(int32))
      ret = int(num, int64)
    type is (integer(int16))
      ret = int(num, int64)
    type is (integer(int8))
      ret = int(num, int64)
    class default
      ret = 0_8
      ret = 1_8/ret
    end select
  end
  pure elemental type(modint) function new_modint()
  end
  pure elemental type(modint) function new_modint_with_num(num) result(ret)
    class(*), intent(in) :: num
    ret%val = to_int64(num)
  end
  pure elemental subroutine asgn(this,other)
    class(modint), intent(inout) :: this
    class(*), intent(in) :: other
    integer(int64) :: val
    select type (other)
    type is (modint)
      this%val = other%val
    class default
      val = to_int64(other)
      if (0_8 <= val .and. val < modu) then
        this%val = val
      else
        this%val = modulo(val,modu)
      end if
    end select
  end
  pure elemental type(modint) function add(this,other) result(ret)
    class(modint), intent(in) :: this
    class(*), intent(in) :: other
    integer(int64) :: val
    select type (other)
    type is (modint)
      if (0_8 <= this%val+other%val .and. this%val+other%val < modu) then
        ret%val = this%val+other%val
      else
        ret%val = this%val+other%val-modu
      end if
    class default
      val = to_int64(other)
      if (0_8 <= this%val+val .and. this%val+val < modu) then
        ret%val = this%val+val
      else
        ret%val = modulo(this%val+val,modu)
      end if
    end select
  end
  pure elemental type(modint) function sub(this,other) result(ret)
    class(modint), intent(in) :: this
    class(*), intent(in) :: other
    integer(int64) :: val
    select type (other)
    type is (modint)
      if (0_8 <= this%val-other%val .and. this%val-other%val < modu) then
        ret%val = this%val-other%val
      else
        ret%val = this%val-other%val+modu
      end if
    class default
      val = to_int64(other)
      if (0_8 <= this%val-val .and. this%val-val < modu) then
        ret%val = this%val-val
      else
        ret%val = modulo(this%val-val,modu)
      end if
    end select
  end
  pure elemental type(modint) function mul(this,other) result(ret)
    class(modint), intent(in) :: this
    class(*), intent(in) :: other
    integer(int64) :: val
    select type (other)
    type is (modint)
      val = other%val
    class default
      val = to_int64(other)
    end select
    if (0_8 <= this%val*val .and. this%val*val < modu) then
      ret%val = this%val*val
    else
      ret%val = modulo(this%val*val,modu)
    end if
  end
  pure elemental type(modint) function div(this,other) result(ret)
    class(modint), intent(in) :: this
    class(*), intent(in) :: other
    type(modint) :: tmp
    call asgn(tmp,other)
    call asgn(ret,mul(this,inv(tmp)))
  end
  pure elemental integer(int64) function get(this)
    class(modint), intent(in) :: this
    get = this%val
  end
  pure elemental type(modint) function inv(this)
    class(modint), intent(in) :: this
    integer(int64) :: a, b, c, n
    integer(int64) :: x, y, z, m
    a = this%val
    b = modu
    c = 0_8
    n = 1_8
    do while (b /= 0_8)
      x = b
      y = mod(a,b)
      z = n-a/b*c
      m = c
      a = x
      b = y
      c = z
      n = m
    end do
    call asgn(inv,n)
  end
  pure elemental type(modint) function pow(this,other)
    class(modint), intent(in) :: this
    class(*), intent(in) :: other
    integer(int64) :: n
    type(modint) :: i
    pow%val = 1_8
    i%val = this%val
    n = to_int64(other)
    do while (n > 0_8)
      if (btest(n,0)) call asgn(pow,mul(pow,i))
      call asgn(i,mul(i,i))
      n = rshift(n,1)
    end do
  end
  pure elemental type(modint) function add_r(other,this) result(ret)
    class(modint), intent(in) :: this
    class(*), intent(in) :: other
    call asgn(ret,add(this,other))
  end
  pure elemental type(modint) function sub_r(other,this) result(ret)
    class(modint), intent(in) :: this
    class(*), intent(in) :: other
    call asgn(ret,sub(this,other))
  end
  pure elemental type(modint) function mul_r(other,this) result(ret)
    class(modint), intent(in) :: this
    class(*), intent(in) :: other
    call asgn(ret,mul(this,other))
  end
  pure elemental type(modint) function div_r(other,this) result(ret)
    class(modint), intent(in) :: this
    class(*), intent(in) :: other
    call asgn(ret,div(this,other))
  end
  pure type(modint) function dot_prod(x,y) result(ret)
    type(modint), intent(in) :: x(:), y(:)
    integer :: i
    if (size(x,1) /= size(y,1)) i = to_int64("")
    do i = 1, size(x,1)
      call asgn(ret,add(ret,mul(x(i),y(i))))
    end do
  end
  pure function matmul1(x,y) result(ret)
    type(modint), intent(in) :: x(:,:), y(:)
    type(modint) :: ret(size(x,1))
    integer :: i
    if (size(x,2) /= size(y,1)) i = to_int64("")
    do i = 1, size(x,1)
      call asgn(ret(i),dot_prod(x(i,:),y))
    end do
  end
  pure function matmul2(x,y) result(ret)
    type(modint), intent(in) :: x(:), y(:,:)
    type(modint) :: ret(size(y,2))
    integer :: i
    if (size(x,1) /= size(y,1)) i = to_int64("")
    do i = 1, size(y,2)
      call asgn(ret(i),dot_prod(x,y(:,i)))
    end do
  end
  pure function matmul3(x,y) result(ret)
    type(modint), intent(in) :: x(:,:), y(:,:)
    type(modint) :: ret(size(x,1),size(y,2))
    integer :: i
    if (size(x,2) /= size(y,1)) i = to_int64("")
    do i = 1, size(x,1)
      call asgn(ret(i,:),matmul2(x(i,:),y))
    end do
  end
end module module_modint
module modulo_utils
  use module_modint
  implicit none
  type(modint), allocatable :: f(:), invf(:)
contains
  subroutine init(n)
    integer, intent(in) :: n
    integer :: i
    if (allocated(f)) deallocate(f)
    if (allocated(invf)) deallocate(invf)
    allocate(f(0:n),invf(0:n))
    f(0) = 1
    do i = 1, n
      f(i) = f(i-1)*i
    end do
    invf(n) = f(n)%inv()
    do i = n, 1, -1
      invf(i-1) = invf(i)*i
    end do
  end
  pure elemental type(modint) function perm(n,k)
    integer, intent(in) :: n, k
    if (k > n .or. n < 0 .or. k < 0) return
    perm = f(n)*invf(n-k)
  end
  pure elemental type(modint) function comb(n,k)
    integer, intent(in) :: n, k
    comb = perm(n,k)*invf(k)
  end
  pure elemental type(modint) function homo(n,k)
    integer, intent(in) :: n, k
    homo = 1
    if (n == 0 .and. k == 0) return
    homo = comb(n+k-1,k)
  end
end module modulo_utils
program bullet
  use module_modint
  use modulo_utils
  implicit none
  real(16), parameter :: pi = 3.14159265358979323846264338327950288_16
  real(16), parameter :: hi = 0.5_16*pi
  integer :: n, i, k1, k2, k3, k4
  integer(8) :: c(200000,2) = 0_8
  real(16) :: t(-200000:400000) = 0.0_16
  type(modint) :: ans
  read(*,*) n
  do i = 1, n
    read(*,*) c(i,:)
  end do
  call merge_sort(c(1:n,:))
  do i = 1, n
    t(i) = atan2(real(c(i,2),16),real(c(i,1),16))
  end do
  t(-n+1:0) = t(1:n)-2.0_16*pi
  t(n+1:2*n) = t(1:n)+2.0_16*pi
  do i = 1, n
    k1 = right_lower_bound(i)
    k2 = right_upper_bound(i)
    k3 = left_lower_bound(i)
    k4 = left_upper_bound(i)
    ans = ans + modint(2)**(n-k2+k1-k3+k4-1)
  end do
  write(*,'(i0)') ans%get()
contains
  function right_lower_bound(i) result(l)
    integer, intent(in) :: i
    integer :: l, r, m
    l = i
    r = 2*n+1
    do while (r-l > 1)
      m = (l+r)/2
      if (t(m)-t(i) < hi) then
        l = m
      else
        r = m
      end if
    end do
  end
  function right_upper_bound(i) result(r)
    integer, intent(in) :: i
    integer :: l, r, m
    l = i
    r = 2*n
    do while (r-l > 1)
      m = (l+r)/2
      if (t(m)-t(i) <= hi) then
        l = m
      else
        r = m
      end if
    end do
  end
  function left_upper_bound(i) result(l)
    integer, intent(in) :: i
    integer :: l, r, m
    l = -n+1
    r = i
    do while (r-l > 1)
      m = (l+r)/2
      if (t(i)-t(m) < hi) then
        l = m
      else
        r = m
      end if
    end do
  end
  function left_lower_bound(i) result(r)
    integer, intent(in) :: i
    integer :: l, r, m
    l = -n
    r = i
    do while (r-l > 1)
      m = (l+r)/2
      if (t(i)-t(m) <= hi) then
        l = m
      else
        r = m
      end if
    end do
  end
  subroutine merge_sort(a)
    integer(8), intent(inout) :: a(:,:)
    integer :: n, m, l, i, u
    n = size(a,1)
    m = n
    l = 1
    do while (m > 1)
      do i = 1, m/2
        u = min(2*i*l,n)
        call merger(a(2*(i-1)*l+1:(2*i-1)*l,:),a((2*i-1)*l+1:u,:))
      end do
      l = 2*l
      m = (m+1)/2
    end do
  end
  subroutine merger(a1,a2)
    integer(8), intent(inout) :: a1(:,:), a2(:,:)
    integer(8) :: a(size(a1,1)+size(a2,1),size(a1,2))
    integer :: i1, i2, n1, n2
    real(16) :: t1, t2
    i1 = 1
    i2 = 1
    n1 = size(a1,1)
    n2 = size(a2,1)
    do while (i1 <= n1 .and. i2 <= n2)
      t1 = atan2(real(a1(i1,2),16),real(a1(i1,1),16))
      t2 = atan2(real(a2(i2,2),16),real(a2(i2,1),16))
      if (t1 <= t2) then
        a(i1+i2-1,:) = a1(i1,:)
        i1 = i1+1
      else
        a(i1+i2-1,:) = a2(i2,:)
        i2 = i2+1
       end if
    end do
    if (i1 <= n1) then
      a(i1+i2-1:n1+n2,:) = a1(i1:n1,:)
    else
      a(i1+i2-1:n1+n2,:) = a2(i2:n2,:)
    end if
    a1 = a(1:n1,:)
    a2 = a(n1+1:n1+n2,:)
  end
end program bullet


program main
	implicit none
    integer a, b, c, k, i
    read(*,*)a, b, c, k
    if (a>=k.or.a+b>=k) then
    	write(*,*)a
    else
    	i = a-(k-a-b)
        write(*,*)i
	end if
end program main

Score : 100 points

Problem Statement

Takahashi wants to be a member of some web service.

He tried to register himself with the ID S, which turned out to be already used by another user.

Thus, he decides to register using a string obtained by appending one character at the end of S as his ID.

He is now trying to register with the ID T. Determine whether this string satisfies the property above.

Constraints

S and T are strings consisting of lowercase English letters.

1 \leq |S| \leq 10

|T| = |S| + 1

Input

Input is given from Standard Input in the following format:

S
T

Output

If T satisfies the property in Problem Statement, print Yes; otherwise, print No.

Sample Input 1

chokudai
chokudaiz

Sample Output 1

Yes

chokudaiz can be obtained by appending z at the end of chokudai.

Sample Input 2

snuke
snekee

Sample Output 2

No

snekee cannot be obtained by appending one character at the end of snuke.

Sample Input 3

a
aa

Sample Output 3

Yes


   program abc167b
      implicit none
      integer(8)::a,b,c
      integer(8)::k,ans=0

         read *, a,b,c,k

         if (a>=k) then
            print *, k
            stop
         else
            ans=ans+a
            k=k-a
            if (b>=k) then
               print *, ans
               stop
            else
               k=k-b
               ans=ans-k
               print *, ans
               stop
            end if
         end if

   end program abc167b

Score : 200 points

Problem Statement

We have A cards, each of which has an integer 1 written on it. Similarly, we also have B cards with 0s and C cards with -1s.

We will pick up K among these cards. What is the maximum possible sum of the numbers written on the cards chosen?

Constraints

All values in input are integers.

0 \leq A, B, C

1 \leq K \leq A + B + C \leq 2 \times 10^9

Input

Input is given from Standard Input in the following format:

A B C K

Output

Print the maximum possible sum of the numbers written on the cards chosen.

Sample Input 1

2 1 1 3

Sample Output 1

2

Consider picking up two cards with 1s and one card with a 0.
In this case, the sum of the numbers written on the cards is 2, which is the maximum possible value.

Sample Input 2

1 2 3 4

Sample Output 2

0

Sample Input 3

2000000000 0 0 2000000000

Sample Output 3

2000000000


program main
      implicit none
      integer(8) :: a, b, c, k, cnt

      read *, a, b, c, k

      if ( k >= (a+b) ) then
        cnt = a - ( k-a-b )
      else
        cnt = a
      end if

      print *, cnt
end program main


program main
      implicit none
      integer :: n, m, x
      integer, allocatable :: c(:), a(:,:), y(:), z(:)
      integer :: i, cnt
      integer(8) :: price, lowprice = 10**9
      logical, allocatable :: used(:)

      read *, n, m, x
      allocate( c(n) )
      allocate( a(n,m) )
      do i = 1, n
        read *, c(i), a(i,:)
      end do

      allocate( z(m) )
      allocate( y(n) )
      allocate( used(n) )
      y = 1
      used(:) = .false.
      call DFS(1,n)

      print *, lowprice

contains
recursive subroutine DFS(pos,v)
      implicit none
      integer, intent(in) :: pos,v
      integer :: i, k, j

      if( pos == v+1) then
      !   print *, y

         z(:) = 0
         cnt = 0
         price = 0
         do  k = 1, n
           price = price + c(y(k))
      !     print *, price
           do j = 1, m
             z(j) = z(j) + a(y(k),j)
             if ( z(j) >= x ) then
      !         print *, k, j, z(j),x
               cnt = cnt + 1
             end if
           end do

           if ( cnt == m ) then
             if ( lowprice >= price ) then
               lowprice = price
               exit
             end if
           end if

         end do

         if( cnt < m ) then
           print *, '-1'
           stop
         end if

         return
      end if

      do i = 1, v
        if ( .not. used(i) ) then
          y(pos) = i
          used(i) = .true.
          call DFS(pos+1,v)
          used(i) = .false.
        end if
      end do

end subroutine DFS
end program main

Score : 300 points

Problem

Takahashi, who is a novice in competitive programming, wants to learn M algorithms.
Initially, his understanding level of each of the M algorithms is 0.

Takahashi is visiting a bookstore, where he finds N books on algorithms.
The i-th book (1\leq i\leq N) is sold for C_i yen (the currency of Japan). If he buys and reads it, his understanding level of the j-th algorithm will increase by A_{i,j} for each j (1\leq j\leq M).
There is no other way to increase the understanding levels of the algorithms.

Takahashi's objective is to make his understanding levels of all the M algorithms X or higher. Determine whether this objective is achievable. If it is achievable, find the minimum amount of money needed to achieve it.

Constraints

All values in input are integers.

1\leq N, M\leq 12

1\leq X\leq 10^5

1\leq C_i \leq 10^5

0\leq A_{i, j} \leq 10^5

Input

Input is given from Standard Input in the following format:

N M X
C_1 A_{1,1} A_{1,2} \cdots A_{1,M}
C_2 A_{2,1} A_{2,2} \cdots A_{2,M}
\vdots
C_N A_{N,1} A_{N,2} \cdots A_{N,M}

Output

If the objective is not achievable, print -1; otherwise, print the minimum amount of money needed to achieve it.

Sample Input 1

3 3 10
60 2 2 4
70 8 7 9
50 2 3 9

Sample Output 1

120

Buying the second and third books makes his understanding levels of all the algorithms 10 or higher, at the minimum cost possible.

Sample Input 2

3 3 10
100 3 1 4
100 1 5 9
100 2 6 5

Sample Output 2

-1

Buying all the books is still not enough to make his understanding levels of all the algorithms 10 or higher.

Sample Input 3

8 5 22
100 3 7 5 3 1
164 4 5 2 7 8
334 7 2 7 2 9
234 4 7 2 8 2
541 5 4 3 3 6
235 4 8 6 9 7
394 3 6 1 6 2
872 8 4 3 7 2

Sample Output 3

1067


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

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

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

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

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

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

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

  interface inv
    module procedure :: invm
  end interface inv

contains

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

end module mod_modint
program em
  use mod_modint
  implicit none
  integer::N,M,K
  type(modint),allocatable,dimension(:)::M_1,P
  type(modint)::ans
  integer::i
  read*,N,M,K
  allocate(P(0:N))
  P(0)=1
  do i=1,N
    P(i)=P(i-1)*i
  end do
  allocate(M_1(0:N))
  M_1(0)=1
  do i=1,N
    M_1(i)=M_1(i-1)*(M-1)
  end do
  ANS=0
  do i=0,K
    ANS=ANS+M*M_1(N-1-i)*Comb(N-1,i)
  end do
  print"(I0)",getnum(ANS)
  contains
function Comb(kk,jj)
  type(modint)::comb
  integer::kk,jj
  Comb=P(kk)/(P(kk-jj)*P(jj))
end function
end program em

Score : 500 points

Problem Statement

There are N blocks arranged in a row. Let us paint these blocks.

We will consider two ways to paint the blocks different if and only if there is a block painted in different colors in those two ways.

Find the number of ways to paint the blocks under the following conditions:

For each block, use one of the M colors, Color 1 through Color M, to paint it. It is not mandatory to use all the colors.

There may be at most K pairs of adjacent blocks that are painted in the same color.

Since the count may be enormous, print it modulo 998244353.

Constraints

All values in input are integers.

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

0 \leq K \leq N - 1

Input

Input is given from Standard Input in the following format:

N M K

Output

Print the answer.

Sample Input 1

3 2 1

Sample Output 1

6

The following ways to paint the blocks satisfy the conditions: 112, 121, 122, 211, 212, and 221. Here, digits represent the colors of the blocks.

Sample Input 2

100 100 0

Sample Output 2

73074801

Sample Input 3

60522 114575 7559

Sample Output 3

479519525


program sample
implicit none
integer(8)X,k,i
integer A,B,A1,B1
read(*,*)X
do A=-200,200
do B=-200,200
if (A**5-B**5==X) then
A1=A
B1=B
end if
end do
end do
write(*,*) A1,B1
stop
end program sample

Score : 100 points

Problem Statement

AtCoder Inc. holds a contest every Saturday.

There are two types of contests called ABC and ARC, and just one of them is held at a time.

The company holds these two types of contests alternately: an ARC follows an ABC and vice versa.

Given a string S representing the type of the contest held last week, print the string representing the type of the contest held this week.

Constraints

S is ABC or ARC.

Input

Input is given from Standard Input in the following format:

S

Output

Print the string representing the type of the contest held this week.

Sample Input 1

ABC

Sample Output 1

ARC

They held an ABC last week, so they will hold an ARC this week.


program sample
implicit none

character(len=3) :: s
read(*,*) s

if (s == "ABC") then
write(*,*) "ARC"
else
write(*,*) "ABC"
end if
stop
end program sample


program sample
implicit none

integer :: n, k
integer, allocatable :: a(:), b(:)
integer :: d
integer :: number

integer :: i, j

read(*,*) n, k
allocate(a(n))
a = 0
do i = 1, k
read(*,*) d
allocate(b(d))
read(*,*) b
do j = 1, d
a(b(j)) = 1
end do
deallocate(b)
end do

number = 0

do i = 1, n
if (a(i) == 0) then
number = number + 1
end if
end do

write(*,*) number

stop
end program sample

Score : 200 points

Problem Statement

N Snukes called Snuke 1, Snuke 2, ..., Snuke N live in a town.

There are K kinds of snacks sold in this town, called Snack 1, Snack 2, ..., Snack K. The following d_i Snukes have Snack i: Snuke A_{i, 1}, A_{i, 2}, \cdots, A_{i, {d_i}}.

Takahashi will walk around this town and make mischief on the Snukes who have no snacks. How many Snukes will fall victim to Takahashi's mischief?

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq K \leq 100

1 \leq d_i \leq N

1 \leq A_{i, 1} < \cdots < A_{i, d_i} \leq N

Input

Input is given from Standard Input in the following format:

N K
d_1
A_{1, 1} \cdots A_{1, d_1}
\vdots
d_K
A_{K, 1} \cdots A_{K, d_K}

Output

Print the answer.

Sample Input 1

3 2
2
1 3
1
3

Sample Output 1

1

Snuke 1 has Snack 1.

Snuke 2 has no snacks.

Snuke 3 has Snack 1 and 2.

Thus, there will be one victim: Snuke 2.

Sample Input 2

3 3
1
3
1
3
1
3

Sample Output 2

2


   program abc166c
      implicit none
      integer(8)::n,m
      integer(8),allocatable::h(:),a(:),b(:),flag(:)
      integer(8)::i,j,ans

         read *, n,m
         allocate(h(n),a(m),b(m),flag(n))
         read *, h(1:n)

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

         flag=1
         do i=1,m
            if ( h(a(i)) < h(b(i)) ) then
               flag(a(i))=0
            else if ( h(a(i)) > h(b(i)) ) then
               flag(b(i))=0
            else
               flag(a(i))=0;flag(b(i))=0
            end if
         end do

         ans=0
         do i=1,n
            if (flag(i)==1) ans=ans+1
         end do

         print *, ans

   end program abc166c

Score : 300 points

Problem Statement

There are N observatories in AtCoder Hill, called Obs. 1, Obs. 2, ..., Obs. N. The elevation of Obs. i is H_i.
There are also M roads, each connecting two different observatories. Road j connects Obs. A_j and Obs. B_j.

Obs. i is said to be good when its elevation is higher than those of all observatories that can be reached from Obs. i using just one road.
Note that Obs. i is also good when no observatory can be reached from Obs. i using just one road.

How many good observatories are there?

Constraints

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq H_i \leq 10^9

1 \leq A_i,B_i \leq N

A_i \neq B_i

Multiple roads may connect the same pair of observatories.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
H_1 H_2 ... H_N
A_1 B_1
A_2 B_2
:
A_M B_M

Output

Print the number of good observatories.

Sample Input 1

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

Sample Output 1

2

From Obs. 1, you can reach Obs. 3 using just one road. The elevation of Obs. 1 is not higher than that of Obs. 3, so Obs. 1 is not good.

From Obs. 2, you can reach Obs. 3 and 4 using just one road. The elevation of Obs. 2 is not higher than that of Obs. 3, so Obs. 2 is not good.

From Obs. 3, you can reach Obs. 1 and 2 using just one road. The elevation of Obs. 3 is higher than those of Obs. 1 and 2, so Obs. 3 is good.

From Obs. 4, you can reach Obs. 2 using just one road. The elevation of Obs. 4 is higher than that of Obs. 2, so Obs. 4 is good.

Thus, the good observatories are Obs. 3 and 4, so there are two good observatories.

Sample Input 2

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

Sample Output 2

3


program peaks
  implicit none
  integer :: n, m, h(100000) = 0, a, b, i, x = 0
  logical :: g(100000) = .true.
  read(*,*) n, m
  read(*,*) h(1:n)
  do i = 1, m
    read(*,*) a, b
    if (h(a) <= h(b)) g(a) = .false.
    if (h(b) <= h(a)) g(b) = .false.
  end do
  do i = 1, n
    if (g(i)) x = x+1
  end do
  write(*,'(i0)') x
end program peaks


   program abc166c
      implicit none
      integer(8)::n,m
      integer(8),allocatable::h(:),a(:),b(:),flag(:)
      integer(8)::i,j,ans

         read *, n,m
         allocate(h(n),a(m),b(m),flag(n))
         read *, h(1:n)

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

         flag=0
         do i=1,m
            if ( h(a(i)) < h(b(i)) ) then
               if (flag(b(i))==0 .or. flag(b(i))==1) then
                  flag(b(i))=1
                  flag(a(i))=0
               end if
            else
               if (flag(a(i))==0 .or. flag(a(i))==1) then
                  flag(a(i))=1
                  flag(b(i))=0
               end if
            end if
         end do

         ans=0
         do i=1,n
            if (flag(i)==1) ans=ans+1
         end do

         print *, ans

   end program abc166c


program peaks

 implicit none
 integer::n,m,i,j,answer,hikaku
 integer,allocatable::h(:),a(:),b(:),ans(:)

 read*,n,m
 allocate(h(0:n),a(m),b(m),ans(n))

 h(0)=-1
 read*,h(1:n)
 !print*,h
 do i=1,m
  read*,a(i),b(i)
  !print*,a(i),b(i)
 end do

 ans=0
 answer=0

  do i=1,n
   do j=1,m
   if(i==a(j)) hikaku=b(j)
   if(i==b(j)) hikaku=a(j)
   if(i/=a(j) .and. i/=b(j)) hikaku=0
    if(h(i)<=h(hikaku)) then
    ans(i)=1
    exit
    end if
   end do
   if(ans(i)==0) answer=answer+1
  end do

  print*,answer

end program peaks


program i_hate_factorization
    integer(8) X
    integer(8) i, j
    integer(8) :: ans = 0
    logical :: det = .false.
    read *, X
    do i = 100, -100, -1
        if(det .eqv. .true.)exit
        do j = -100, 100, 1
            ans = (i**5)-(j**5)
            if(x == ans)then
                det = .true.
                print *, i, '', j
                exit
            end if
        end do

    end do
end program

Score : 400 points

Problem Statement

Give a pair of integers (A, B) such that A^5-B^5 = X.
It is guaranteed that there exists such a pair for the given integer X.

Constraints

1 \leq X \leq 10^9

X is an integer.

There exists a pair of integers (A, B) satisfying the condition in Problem Statement.

Input

Input is given from Standard Input in the following format:

X

Output

Print A and B, with space in between.
If there are multiple pairs of integers (A, B) satisfying the condition, you may print any of them.

A B

Sample Input 1

33

Sample Output 1

2 -1

For A=2 and B=-1, A^5-B^5 = 33.

Sample Input 2

1

Sample Output 2

0 -1


program i_hate_factorization
    integer(8) X
    integer i, j
    integer :: ans = 0
    logical :: det = .false.
    read *, X
    do i = -100, 100, 1
        do j = -100, 100, 1
            ans = ((i**5)-(j**5))
            if(x == ans)then
                det = .true.
                print "('',i0)", i, j
                exit
            end if
        end do
        if(det .eqv. .true.)exit
    end do
end program


program main
  implicit none
  integer(8) :: x
  integer(8) :: str, ai, a
  real(8) :: rtmp, ar

  read *, x

  str = real(x) ** (1.0/5.0)
  rtmp = real(x) ** (1.0/5.0)

  do
    if ( str == rtmp ) then
      print *, str, '0'
      exit
    else
      a = x - str**5
      ai = real(a) ** (1.0/5.0)
      ar = real(a) ** (1.0/5.0)
      if ( ai == ar ) then
        print *, str, (-1)*ai
        exit
      else
        str = str + 1
      end if
    end if
  end do

end program main


program ABC166_D
  integer::A
  integer::C(100000)
  C=0
  read(*,*)X
  N=0
  A=int(sqrt(X))
  do i=1,A
     if(int(X)/i*i==int(X)) then
        N=N+1
        C(N)=i
     end if
  end do
  do i=1,N
     do j=-100000000,100000000
        if((5*j**4+10*(j**3)*C(i)+10*(j**2)*(C(i)**2)+5*j*(C(i)**3)+C(i)**4)==int(X)/C(i)) then
           write(*,*)C(i)+j,j
           stop
        end if
     end do
  end do
end program ABC166_D


program main
      implicit none
      integer(4) :: n, i
      integer(8), allocatable :: a(:)
      integer(4), allocatable :: tmpi(:), tmpj(:)
      integer(8) :: cnt

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

      allocate(tmpi(n))
      allocate(tmpj(n))
      tmpi(:) = 0
      tmpj(:) = 0

      do i = 1, n
        if ( i + a(i) < n+1 ) then
          tmpi(i+a(i)) = tmpi(i+a(i)) + 1
        end if
        if ( i - a(i) > 0 ) then
          tmpj(i-a(i)) = tmpj(i-a(i)) + 1
        end if
      end do

      cnt = 0
      do i = 1, n
        cnt = cnt + tmpi(i) * tmpj(i)
      end do

      print *, cnt

end program main

Score: 500 points

Problem Statement

You are the top spy of AtCoder Kingdom. To prevent the stolen secret from being handed to AlDebaran Kingdom, you have sneaked into the party where the transaction happens.

There are N attendees in the party, and they are given attendee numbers from 1 through N. The height of Attendee i is A_i.

According to an examination beforehand, you know that a pair of attendees satisfying the condition below will make the transaction.

The absolute difference of their attendee numbers is equal to the sum of their heights.

There are \frac{N(N-1)}{2} ways to choose two from the N attendees and make a pair. Among them, how many satisfy the condition above?

P.S.: We cannot let you know the secret.

Constraints

All values in input are integers.

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

1 \leq A_i \leq 10^9\ (1 \leq i \leq N)

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \dots A_N

Output

Print the number of pairs satisfying the condition.

Sample Input 1

6
2 3 3 1 3 1

Sample Output 1

3

A_1 + A_4 = 3, so the pair of Attendee 1 and 4 satisfy the condition.

A_2 + A_6 = 4, so the pair of Attendee 2 and 6 satisfy the condition.

A_4 + A_6 = 2, so the pair of Attendee 4 and 6 satisfy the condition.

No other pair satisfies the condition, so you should print 3.

Sample Input 2

6
5 2 4 2 8 8

Sample Output 2

0

No pair satisfies the condition, so you should print 0.

Sample Input 3

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

Sample Output 3

22


program three_variables_game
  implicit none
  integer :: n, r(4) = 0, x(100001) = 0, i, j, p(2) = 0
  character(1) :: c(4) = (/ "A", "B", "X", "C" /)
  character(2) :: s
  read(*,*) n, r(1), r(2), r(4)
  if (sum(r) == 0) then
    write(*,'(a)') "No"
    stop
  end if
  do i = 1, n
    read(*,*) s
    x(i) = hash(s)
  end do
  if (sum(r) == 1) then
    j = maxloc(r,1)
    call simulate(.false.)
    r = 0
    r(j) = 1
    call simulate(.true.)
    stop
  end if
  call get_index(x(1),p)
  if (r(p(1)) == 0 .and. r(p(2)) == 0) then
    write(*,'(a)') "No"
    stop
  else
    write(*,'(a)') "Yes"
  end if
  do i = 1, n
    call get_index(x(i),p)
    if (sum(r) == 2 .and. r(p(1))*r(p(2)) == 1 .and. i < n .and. x(i) /= x(i+1)) then
      j = 1
      if (p(2) == and(x(i),x(i+1))) j = 2
      r(p(j)) = r(p(j))+1
      r(p(3-j)) = r(p(3-j))-1
      write(*,'(a)') c(p(j))
    else if (r(p(1)) > r(p(2))) then
      r(p(1)) = r(p(1))-1
      r(p(2)) = r(p(2))+1
      write(*,'(a)') c(p(2))
    else
      r(p(1)) = r(p(1))+1
      r(p(2)) = r(p(2))-1
      write(*,'(a)') c(p(1))
    end if
  end do
contains
  integer function hash(s)
    character(2), intent(in) :: s
    select case (s)
      case ("AB")
        hash = 3
      case ("AC")
        hash = 5
      case ("BC")
        hash = 6
    end select
  end
  subroutine get_index(x,p)
    integer, intent(in) :: x
    integer, intent(out) :: p(2)
    integer :: i, k
    i = 0
    do k = 0, 2
      if (.not.btest(x,k)) cycle
      i = i+1
      p(i) = lshift(1,k)
    end do
  end
  subroutine simulate(out)
    logical, intent(in) :: out
    if (out) write(*,'(a)') "Yes"
    do i = 1, n
      call get_index(x(i),p)
      if (r(p(1)) /= 0) then
        r(p(1)) = r(p(1))-1
        r(p(2)) = r(p(2))+1
        if (out) write(*,'(a)') c(p(2))
      else if (r(p(2)) /= 0) then
        r(p(1)) = r(p(1))+1
        r(p(2)) = r(p(2))-1
        if (out) write(*,'(a)') c(p(1))
      else
        write(*,'(a)') "No"
        stop
      end if
    end do
  end
end program three_variables_game

Score : 600 points

Problem Statement

There is a game that involves three variables, denoted A, B, and C.

As the game progresses, there will be N events where you are asked to make a choice.
Each of these choices is represented by a string s_i. If s_i is AB, you must add 1 to A or B then subtract 1 from the other; if s_i is AC, you must add 1 to A or C then subtract 1 from the other; if s_i is BC, you must add 1 to B or C then subtract 1 from the other.

After each choice, none of A, B, and C should be negative.

Determine whether it is possible to make N choices under this condition. If it is possible, also give one such way to make the choices.

Constraints

1 \leq N \leq 10^5

0 \leq A,B,C \leq 10^9

N, A, B, C are integers.

s_i is AB, AC, or BC.

Input

Input is given from Standard Input in the following format:

N A B C
s_1
s_2
:
s_N

Output

If it is possible to make N choices under the condition, print Yes; otherwise, print No.

Also, in the former case, show one such way to make the choices in the subsequent N lines. The (i+1)-th line should contain the name of the variable (A, B, or C) to which you add 1 in the i-th choice.

Sample Input 1

2 1 3 0
AB
AC

Sample Output 1

Yes
A
C

You can successfully make two choices, as follows:

In the first choice, add 1 to A and subtract 1 from B. A becomes 2, and B becomes 2.

In the second choice, add 1 to C and subtract 1 from A. C becomes 1, and A becomes 1.

Sample Input 2

3 1 0 0
AB
BC
AB

Sample Output 2

No

Sample Input 3

1 0 9 0
AC

Sample Output 3

No

Sample Input 4

8 6 9 1
AC
BC
AB
BC
AC
BC
AB
AB

Sample Output 4

Yes
C
B
B
C
C
B
A
A


program main
  implicit none
  integer :: k, a, b
  read *, k
  read *, a, b
  if (possible(k, a, b)) then
    print "(a)", "OK"
  else
    print "(a)", "NG"
  end if
  contains
  logical function possible(k, a, b)
    integer, intent(in) :: k, a, b
    integer :: i
    possible = .false.
    do i = a, b
      if (modulo(i, k) == 0) possible = .true.
    end do
  end function possible
end program main

Score: 100 points

Problem Statement

Takahashi the Jumbo will practice golf.

His objective is to get a carry distance that is a multiple of K, while he can only make a carry distance of between A and B (inclusive).

If he can achieve the objective, print OK; if he cannot, print NG.

Constraints

All values in input are integers.

1 \leq A \leq B \leq 1000

1 \leq K \leq 1000

Input

Input is given from Standard Input in the following format:

K
A B

Output

If he can achieve the objective, print OK; if he cannot, print NG.

Sample Input 1

7
500 600

Sample Output 1

OK

Among the multiples of 7, for example, 567 lies between 500 and 600.

Sample Input 2

4
5 7

Sample Output 2

NG

No multiple of 4 lies between 5 and 7.

Sample Input 3

1
11 11

Sample Output 3

OK


   program abc165a
      implicit none
      integer::k,a,b
      integer::i

         read *, k
         read *, a,b

         do i=a,b
            if (mod(i,k)==0) then
               print *, 'OK'
               stop
            end if
         end do

         print *, 'NG'

   end program abc165a


program am
implicit none
integer::K
integer::A,B
logical::check=.false.
integer::i

read*,K
read*,A,B
do i=A,B
    if(mod(i,K)==0)check=.true.
end do
print"(A)",merge("OK","NG",check)
contains
end program am


program ABC165A
    implicit none
    integer(8)::K,A,B,i,flg=0
    read*,K,A,B

    do i=A,B
        if(mod(i,K)==0)then
            flg=1
            exit
        end if
    end do

    if(flg==0)print'(A)',"NG"
    if(flg==1)print'(A)',"OK"
end program ABC165A


program main
      implicit none
      integer(8) :: x, i, j, cnt

      read *, x
      cnt = 1
      i = 100
      do
        i = i + i/100
        if ( i >= x ) then
          print *, cnt
          stop
        else
          cnt = cnt + 1
        end if
      end do

end program main

Score : 200 points

Problem Statement

Takahashi has a deposit of 100 yen (the currency of Japan) in AtCoder Bank.

The bank pays an annual interest rate of 1 % compounded annually. (A fraction of less than one yen is discarded.)

Assuming that nothing other than the interest affects Takahashi's balance, in how many years does the balance reach X yen or above for the first time?

Constraints

101 \le X \le 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X

Output

Print the number of years it takes for Takahashi's balance to reach X yen or above for the first time.

Sample Input 1

103

Sample Output 1

3

The balance after one year is 101 yen.

The balance after two years is 102 yen.

The balance after three years is 103 yen.

Thus, it takes three years for the balance to reach 103 yen or above.

Sample Input 2

1000000000000000000

Sample Output 2

3760

Sample Input 3

1333333333

Sample Output 3

1706


program floor_function
  implicit none
  integer(16) :: a, b, n
  read(*,*) a, b, n
  write(*,'(i0)') (a*min(n,b-1_16))/b
end program floor_function

Score : 400 points

Problem Statement

Given are integers A, B, and N.

Find the maximum possible value of floor(Ax/B) - A × floor(x/B) for a non-negative integer x not greater than N.

Here floor(t) denotes the greatest integer not greater than the real number t.

Constraints

1 ≤ A ≤ 10^{6}

1 ≤ B ≤ 10^{12}

1 ≤ N ≤ 10^{12}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B N

Output

Print the maximum possible value of floor(Ax/B) - A × floor(x/B) for a non-negative integer x not greater than N, as an integer.

Sample Input 1

5 7 4

Sample Output 1

2

When x=3, floor(Ax/B)-A×floor(x/B) = floor(15/7) - 5×floor(3/7) = 2. This is the maximum value possible.

Sample Input 2

11 10 9

Sample Output 2

9


program ABC165_D
  integer(8)::A,B,N,X,M,F
  read(*,*)A,B,N
  if(N<B-1) then
     M=int(A*N/B)-A*int(N/B)
  else
     M=int(A*(B-1)/B)
  end if
  write(*,*)M
end program ABC165_D


program floor_function
  implicit none
  integer(16) :: a, b, n, x
  read(*,*) a, b, n
  x = min(n,b-1_16)
  write(*,'(i0)') (a*x)/b-a*(x/b)
end program floor_function