      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 graphMod
      implicit none
! 重みなしのグラフを扱うためのモジュール

      integer(16),parameter,private :: prec = 16

      type :: nodes
        integer(prec),allocatable :: next(:)
        integer(prec) :: degree = 0
        contains
        procedure,public,pass :: appendNode
      end type

      contains
      subroutine appendNode( node, nodeNumber )
        implicit none
        class(nodes),intent(inout) :: node
        integer(prec),intent(in)  :: nodeNumber
        integer(prec),allocatable :: buffer(:)

        if(.not.(allocated(node%next)))then
          allocate(node%next(1))
          node%degree = 0
        end if

        if( node%degree==0 )then
          node%next(1) = nodeNumber
          node%degree = 1
          return
        end if

        node%degree = node%degree + 1
        buffer = node%next(1:node%degree-1)
        if( size(node%next)<node%degree )then
          deallocate( node%next )
          allocate( node%next(node%degree*2) )
        end if
        node%next(1:node%degree) = [buffer(1:node%degree-1), nodeNumber]
      end subroutine appendNode

      END MODULE graphMod

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

      module piyoMod
      use intQueueMod
      use graphMod

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

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

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

        do i = 1,rIn(roomNum)%degree
          call nextQ%enQueue( rIn(roomNum)%next(i) )
          call beforeQ%enQueue( roomNum )
        end do

      end subroutine
      end module

      PROGRAM doubleDots
      use piyoMod
      IMPLICIT NONE
      integer(16) :: n,m,numNei,buffer(5*10**5+1)
      integer(16),allocatable :: a(:),b(:)
      type(nodes),allocatable :: room(:)
      integer(16) :: i

      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
        call room(a(i))%appendNode( b(i) )
        call room(b(i))%appendNode( a(i) )
      end do

  !    do i = 1,m
  !      room(a(i))%numNei = room(a(i))%numNei + 1
  !      numNei = room(a(i))%numNei !numNei=1でエラー？
  !      buffer(1:numNei-1) = room(a(i))%nei(1:numNei-1)
  !      if(allocated(room(a(i))%nei).and.size(room(a(i))%nei(:))<numNei )then
  !        deallocate( room(a(i))%nei )
  !        allocate( room(a(i))%nei(numNei*2) )
  !      end if
  !      buffer(numNei) = b(i)
  !      room(a(i))%nei(1:numNei) = buffer(1:numNei)
  !
  !      room(b(i))%numNei = room(b(i))%numNei + 1
  !      numNei = room(b(i))%numNei
  !      buffer(1:numNei-1) = room(b(i))%nei(1:numNei-1)
  !      if(allocated(room(b(i))%nei).and.size(room(b(i))%nei(:))<numNei )then
  !        deallocate( room(b(i))%nei )
  !        allocate( room(b(i))%nei(numNei*2) )
  !      end if
  !      buffer(numNei) = a(i)
  !      room(b(i))%nei(1:numNei) = buffer(1:numNei)
  !    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 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

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

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.

Submitted source-code file size, in bytes.

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

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


program main
	implicit none
	integer(8):: N,i,j,t,B,K
	integer,dimension(1:2*10**5)::B1,A
	read *, N, K
    read(*,*) (A(i), i = 1, K),(B1(i), i = 1, K)
	do i=1,K
		if(A(i)==1)then
		print '(a)',"Yes"
		print '(i0)',A(i)
		stop
	    end if
	    if(B1(i)==1)then
		print '(a)',"Yes"
		print '(i0)',B1(i)
		stop
	    end if

	end do
	print'(a)',"No"
end program main


program main
	implicit none
    character(11)::s,t
    integer::i,lenn
    read(*,*)s
    read(*,*)t
    lenn=len_trim(s)
    do i=1,lenn
    	if(s(i:i)==t(i:i))then
        	cycle
        else
        	write(*,*)'No'
            stop
        end if
    end do
    write(*,*)'Yes'
    stop
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 main
  implicit none
  integer(8) :: a, b, c, k, filled, fill, res
  read *, a, b, c, k

  fill = min(a, k)
  res = fill
  filled = fill

  fill = min(b, k - filled)
  filled = filled + fill

  fill = min(c, k - filled)
  res = res - fill
  filled = filled + fill

  print "(i0)", res
end program main

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 a, b, c, k, i
    read(*,*)a, b, c, k
    if (a>=k) then
    	write(*,*)a
    else
    	i = a-(k-a-b)
        write(*,*)i
	end if
end program main


program test
	implicit none
	integer :: a=0,b=0,c=0,k=0,ans=0
    read *,a,b,c,k
    if(k<=a) then
    	write (*,'(i0)') k
    else
    	ans=a-(k-a-b)
    	write (*,'(i0)') ans
    end if
end program test


program main
  integer(8) :: A,B,C,K
  integer(8) :: i,j
  read(*,*) A,B,C,K
  if ( K-A > 0) then
     if (K-A-B > 0 ) then
        write(*,'(i0)') A-(K-A-B)
     else
        write(*,'(i0)') A
     end if
  else
     write(*,'(i0)')  K
  end if
end program main


program main
  integer :: N, M
  integer :: X
  integer,allocatable :: C(:), A(:, :)
  integer :: i,j
  integer :: out,tmp
  logical,allocatable :: switch(:)
  integer,allocatable :: test(:)

  read(*,*) N, M, X
  allocate(C(N))
  allocate(A(N, M))
  allocate(switch(N))
  allocate(test(M))
  do i = 1, N
     read(*,*) C(i), A(i, :)
  end do
  if (all(sum(A(:,:), dim = 1) >= X)) then
     out = sum(C)
     do i=0,2**n-1
        do j=1,n
           switch(j) = btest(i,j-1)
        end do
        do j = 1,M
           test(j) = sum(A(:,j), mask=switch)
        end do
        if ( all(test >= X) ) then
           tmp = sum(C(:), mask=switch)
           if (tmp < out) then
              out = tmp
           end if
        end if
     end do
      write(*,'(i0)') out
  else
     write(*,'(i0)') -1
  end if
contains

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


program cm
   implicit none
   integer::N,M,X
   integer,allocatable,dimension(:)::C
   integer,allocatable,dimension(:,:)::A
   integer,allocatable,dimension(:)::R
   integer::ans=10**9

   integer::i
   read*,N,M,X
   allocate(C(N),A(N,M))
   do i=1,N
      read*,C(i),A(i,:)
   end do

   allocate(R(M))
   do i=0,2**N-1
      call Rcheck(i)
      if(minval(R)>=X)then
         ans=min(ans,calc(i))
      endif
   end do
   print"(i0)",merge(-1,ans,ans==10**9)

contains
subroutine Rcheck(k)
   integer::k
   integer::i,j
   R=0
   do i=1,N
      if(btest(k,i-1))then
         do j=1,M
            R(j)=R(j)+A(i,j)
         end do
      endif
   end do
end subroutine
integer function calc(k)
   integer::k
   integer::i
   calc=0
   do i=1,N
      if(btest(k,i-1))then
         calc=calc+C(i)
      endif
   end do
end function
end program cm


   program abc166a
      implicit none
      character(3)::s

         read *, s

         if (s=='ARC') then
            print *, 'ABC'
         else
            print *, 'ARC'
         end if

   end program abc166a

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.


module ABC166

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

    ! require all variables to be explicitly declared
    implicit none

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

    ! <type>s for this <module>

    type type_task_B
        integer(INT32),              public :: num_people
        integer(INT32),              public :: num_kinds_snack
        logical,        allocatable, public :: status_snack(:,:)
    end type type_task_B

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

    subroutine read_data (obj_task_B)

        ! arguments for this <subroutine>
        type(type_task_B), intent(inout) :: obj_task_B

        ! variables for this <subroutine>
        integer(INT32)              :: itr_id, itr_snack
        integer(INT32)              :: bffr_num
        integer(INT32), allocatable :: bffr_id(:)

        read(unit= INPUT_UNIT, fmt= *) obj_task_B%num_people, obj_task_B%num_kinds_snack

        allocate( bffr_id(1:obj_task_B%num_kinds_snack) )
        allocate( obj_task_B%status_snack(1:obj_task_B%num_kinds_snack, 1:obj_task_B%num_people), mold= .false. )

        do itr_snack = 1_INT32, obj_task_B%num_kinds_snack, 1_INT32

            read(unit= INPUT_UNIT, fmt= *) bffr_num
            read(unit= INPUT_UNIT, fmt= *) bffr_id(1:bffr_num)

            do itr_id = 1_INT32, bffr_num, 1_INT32
                obj_task_B%status_snack( itr_snack, bffr_id(itr_id) ) = .true.
            end do

        end do

    end subroutine read_data

    subroutine solve_task_B (obj_task_B)

        ! arguments for this <subroutine>
        type(type_task_B), intent(in) :: obj_task_B

        ! variables for this <subroutine>
        integer(INT32) :: num_victims
        integer(INT32) :: itr_id, itr_snack
        logical        :: buffer_status_snack

        num_victims = obj_task_B%num_people

        loop_id: do itr_id    = 1_INT32, obj_task_B%num_people,      1_INT32
        loop_sk: do itr_snack = 1_INT32, obj_task_B%num_kinds_snack, 1_INT32
            if ( obj_task_B%status_snack(itr_snack, itr_id) ) then
                num_victims = num_victims - 1_INT32
                exit loop_sk
            end if
        end do loop_sk
        end do loop_id

        write(unit= OUTPUT_UNIT, fmt='(I0)') num_victims

    end subroutine solve_task_B

end module ABC166

program main

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

    ! require all variables to be explicitly declared
    implicit none

    ! variables for this <program>
    type(type_task_B) :: obj_task_B

    call read_data(obj_task_B)
    call solve_task_B(obj_task_B)

end program main

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 main
    implicit none
    integer n,k,i,d,j,l,a
    integer,allocatable::c(:),b(:)
    read(*,*) n,k
    allocate(b(n))
    do i=1,k
        read(*,*) d
        allocate(c(d))
        read(*,*) c(1:d)
        do j=1,d
            b(c(j))=b(c(j))+1
        end do
        deallocate(c)
    end do
    a=0
    do l=1,n
        if(b(l)==0) a=a+1
    end do
    write(*,*) a
end program main


program trick_or_treat
  implicit none
  integer :: n, k, d, a(100) = 0, c(100) = 1, i, j
  read(*,*) n, k
  do i = 1, k
    read(*,*) d
    read(*,*) a(1:d)
    do j = 1, d
      c(a(j)) = c(a(j))-1
    end do
  end do
  write(*,'(i0)') sum(c(1:n),c(1:n) > 0)
end program trick_or_treat


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

!2020/05/04 sizeを追加．精度をパラメータに変更．
      integer(16),parameter :: binSize = 10**5
      integer(16),parameter :: precision = 4

      type intStack
        private
        !bin(0) must not have any value.
        integer(precision) :: bin(0:binSize) = 0,  top = 0

        contains
        procedure,public,pass :: initialize => intInitialize
        procedure,public,pass :: isEmpty    => intIsEmpty
        procedure,public,pass :: isFull     => intIsFull
        procedure,public,pass :: push       => intPush
        procedure,public,pass :: pop        => intPop
        procedure,public,pass :: size       => intSize
      end type

      contains
!Reset stack
      subroutine intInitialize( s )
        implicit none
        class(intStack),intent(inout) :: s

        s%top = 0
      end subroutine

!Check status
      function intIsEmpty( s ) result( TorF )
        implicit none
        class(intStack),intent(in) :: s
        logical :: TorF

        if( s%top==0 )then
          TorF = .true.
        else
          TorF = .false.
        endif
      end function

      function intIsFull( s ) result( TorF )
        implicit none
        class(intStack),intent(in) :: s
        logical :: TorF

        if( s%top>=binSize )then
          TorF = .true.
        else
          TorF = .false.
        endif
      end function

!Push into STACK
      subroutine intPush( s,x )
        implicit none
        class(intStack)   ,intent(inout) :: s
        integer(precision),intent(in)    :: x

        if( s%isFull() )then
          print*,'Stack is full, Error!';stop
        else
          s%top = s%top + 1
          s%bin(s%top) = x
        endif
      end subroutine

!Get from STACK
      function intPop( s ) result( x )
        implicit none
        class(intStack),intent(inout) :: s
        integer(precision) :: x

        if( s%isEmpty() )then
          print*,'Stack is empty, Error!';stop
        else
          x = s%bin(s%top)
          s%top = s%top - 1
        end if
      end function

!Current STACK size
      function intSize( s ) result( x )
        implicit none
        class(intStack),intent(in) :: s
        integer(precision) :: x

        x = s%top
      end function

      END MODULE stackMod

      PROGRAM piyo
      use stackMod
      IMPLICIT NONE
      integer,parameter :: prec = 4
      integer(prec) :: n,m
      integer(prec),allocatable :: h(:),a(:),b(:)
      integer(prec) :: i,k,ans
      type(intStack),allocatable :: list(:)

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

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

      allocate( list(n) )

      do i = 1,m
        call list(a(i))%push( b(i) )
        call list(b(i))%push( a(i) )
      end do

      ans = 0
      outer:do i = 1,n

        if( list(i)%size()==0 ) cycle outer
        do k = 1,list(i)%size()

          if( list(i)%pop() > h(i) ) cycle outer

        end do
        ans = ans + 1
      end do outer

      print*,ans

      END PROGRAM

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 main
  implicit none
  integer :: N, M
  integer,allocatable :: H(:), A(:), B(:)
  type neig
     integer, allocatable :: cand(:)
  end type neig
  type(neig), allocatable :: obs(:)
  integer, allocatable :: cnt(:)
  logical, allocatable :: flag(:)
  integer :: i,j,k

  read(*,*) N,M
  allocate(H(N),A(M),B(M))
  allocate(obs(N))
  allocate(cnt(N))
  read(*,*) H(:)
  do i = 1,M
     read(*,*) A(i),B(i)
  end do

  cnt(:) = 0
  do i = 1,M
     cnt(A(i)) = cnt(A(i)) + 1
     cnt(B(i)) = cnt(B(i)) + 1
  end do
  do i = 1,N
     allocate( obs(i)%cand(cnt(i)) )
  end do
  cnt(:) = 0
  do i = 1,M
     cnt(A(i)) = cnt(A(i)) + 1
     obs(A(i))%cand(cnt(A(i))) = B(i)
     cnt(B(i)) = cnt(B(i)) + 1
     obs(B(i))%cand(cnt(B(i))) = A(i)
  end do
  allocate(flag(N))
  flag(:) = .true.
  do i = 1,N
     do j = 1,cnt(i)
        if ( H(i) <= H( obs(i)%cand(j) ) ) then
           flag(i) = .false.
           exit
        end if
     end do
  end do
  write(*,'(i0)') count(flag)
  stop
end program main


program sample
implicit none
integer(8)X,k,i
integer(8) A,B,A1,B1
read(*,*)X
A1=0
B1=0
do A=-150,150
do B=-150,150
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 : 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 ABC166_D
  integer::A
  integer::C(10000)
  double precision::X
  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(C(i)*(5*j**4+10*(j**3)*C(i)+10*(j**2)*(C(i)**2)+5*j*(C(i)**3)+C(i)**4)==int(X)) then
           write(*,*)C(i)+j,j
           stop
        end if
     end do
  end do
end program ABC166_D


program main

  implicit none
  integer(8) :: a,b,x, tmp
  integer(8) :: ansa, ansb

  read(*,*) x

  do a = -100, 100
    tmp = a**5 - x
    do b = -100, 100
      if ( tmp == b**5)then
        ansa = a
        ansb = b
      end if
    end do
  end do

  print*, ansa, ansb

end program main


program main

  implicit none
  integer(8) :: a,b,x, tmp, n
  integer(8) :: ansa, ansb

  read(*,*) x

  do a = 0, 100
    tmp = a**5 - x
    do b = -100, 100
      if ( tmp == b**5 )then
        ansa = a
        ansb = b
        n = 1
        exit
      end if
    end do
    if( n == 1) exit
  end do

  print*, ansa, ansb

end program main


program main

  implicit none
  integer :: a,b,x, tmp
  integer :: ansa, ansb

  read(*,*) x

  do a = 1, 65
    tmp = a**5 - x
    do b = -65, 65
      if ( tmp == b**5)then
        ansa = a
        ansb = b
      end if
    end do
  end do

  print*, ansa, ansb

end program main


   program abc166e
      implicit none
      integer(8)::n
      integer(8),allocatable::a(:)
      integer(8)::ans=0

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

         call abs_tall(1_8)

         print *, ans
         stop

   contains

      recursive subroutine abs_tall(k)
         implicit none
         integer(8),intent(in)::k
         integer(8)::i

            do i=k,n
               if ( abs(i-k)==a(i)+a(k) ) ans=ans+1
            end do

            if (k==n-1) return
            call abs_tall(k+1)

      end subroutine abs_tall

   end program abc166e

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 main
    implicit none
    integer n,i,j,x
    integer,allocatable::a(:)
    read(*,*) n
    allocate(a(n))
    read(*,*) a(1:n)
    x=0
    do i=1,n
        if(a(i)>2*10**5) a(i)=0
    end do
    i=0
    do i=1,n-2
        if(a(i)==0) cycle
        do j=2,n-i
            if(a(i+j)==0) cycle
            if(a(i)+a(i+j)==j) x=x+1
        end do
    end do
    write(*,*) x
end program main


program sample
implicit none

integer :: n
integer, allocatable :: a(:)

integer :: i, j
integer :: number

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

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

write(*,*) number

stop
end program sample


program main
    implicit none
    INTEGER n,a(3),i,x(2),x2(2),ans(100000),flag
    CHARACTER s1*2,s2
    read(*,*) n,(a(i),i=1,3)
    i=0
    flag=1
!0が二つ含まれる場合
    if ( ((a(1)==0 .and. a(2)==0) .or. (a(2)==0 .and. a(3)==0) .or. (a(3)==0 .and. a(1)==0)) .and. a(1)+a(2)+a(3)/=2 ) then
        do i = 1,n
            call read_s(s1,x)
            if ( a(x(1))+a(x(2))==0 ) then
                write(*,*) "No"
                flag=0
                exit
            else
                call nomal(a,ans,x,i)
            end if
        end do
!1,1,0の場合
    else if ( a(1)+a(2)+a(3)==2 ) then
        call read_s(s1,x)
        do i = 1, n
            call read_s(s2,x2)
            call exceptional(a,ans,x,x2,i)
            x=x2
        end do
!それ以外
    else
        do i = 1, n
            call read_s(s1,x)
            call nomal(a,ans,x,i)
        end do
    end if

    if ( flag==1 ) then
        write(*,*) "Yes"
        do i = 1, n
            if(ans(i)==1) then
                write(*,*) "A"
            else if ( ans(i)==2 ) then
                write(*,*) "B"
            else
                write(*,*) "c"
            end if
        end do
    end if
contains
    subroutine read_s(sr, xr)
        implicit none
        INTEGER,INTENT(OUT):: xr(2)
        CHARACTER(*) sr
        read(*,*) sr
        if ( sr=="AB" ) then
            xr(1)=1
            xr(2)=2
        else if ( sr=="BC" ) then
            xr(1)=2
            xr(2)=3
        else
            xr(1)=3
            xr(2)=1
        end if
    end subroutine read_s

    subroutine nomal(an, ansn, xn,in)
        implicit none
        INTEGER an(3),xn(2),ansn(100000),in
        if ( an(xn(1))>=an(xn(2)) ) then
            an(xn(1))=an(xn(1))-1
            an(xn(2))=an(xn(2))+1
            ansn(in)=xn(2)
        else
            an(xn(1))=an(xn(1))+1
            an(xn(2))=an(xn(2))-1
            ansn(in)=xn(1)
        end if
    end subroutine nomal

    subroutine exceptional(as, anss, xs, x2s,is)
        implicit none
        INTEGER as(3),xs(2),x2s(2),anss(100000),is
        if ( as(xs(1))>as(xs(2)) ) then
            as(xs(1))=as(xs(1))-1
            as(xs(2))=as(xs(2))+1
            anss(is)=xs(2)
        else if ( a(x(1))<a(x(2)) ) then
            as(xs(1))=as(xs(1))+1
            as(xs(2))=as(xs(2))-1
            anss(is)=xs(1)
        else if ( xs(1)==x2s(1) .or. xs(1)==x2s(2) ) then
            as(xs(1))=as(xs(1))+1
            as(xs(2))=as(xs(2))-1
            anss(is)=xs(1)
        else
            as(xs(1))=as(xs(1))-1
            as(xs(2))=as(xs(2))+1
            anss(is)=xs(2)
        end if
    end subroutine exceptional
end program main

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 percent

 implicit none
 integer :: year,i
 real(4) :: yen
 real(8) :: x

 read*,x
 !print*,x

 year=1
 yen=100

 do i=1,100000
  yen=yen*1.01
  if(yen>=x) exit
  year=year+1
  yen=aint(yen)
  !print*,yen
 end do

 print*,year

end program percent

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 problemB
  implicit none

integer(8) :: X, n
integer(8) :: BNUM, RNUM, NMAX, count,MMAX, sum, A, B, C, D,multi,countA,countC
integer(8) :: money, yr
character(len=4) :: color

read(*,*) X

! yr=log(real(X)/real(100))/log(1.01)
!
! if (int(yr)==yr) then
!   n=int(yr)
! else
!   n=int(yr)+1
! end if

n=0
money=100
do while (int(money)<X)
  n=n+1
  money=money*1.01
end do

write(*,*)n

stop
end program


program percent

 implicit none
 integer(8) :: year,i,k,x
 real(8) :: yen

 read*,x

 year=1
 yen=100
 do i=1,10000000
  yen=yen*1.01
  k=int(yen)
  if(k>=x) exit
  year=year+1
  yen=real(k)
 end do

 print*,year

end program percent


program name
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: x,y,n

    read*, x
    y=100
    n=0
    do while(y < x)
        y=y+y/100
        n=n+1
    end do
    print'(i0)', n
end program name


program one_per_cent
  implicit none
  integer :: i = 1
  integer(16) :: x, m = 100
  read(*,*) x
  do
    m = int(m*1.01,16)
    if (m >= x) then
      write(*,'(i0)') i
      stop
    end if
    i = i+1
  end do
end program one_per_cent


program bm
implicit none
integer(16)::X
integer(16)::M=100_16
integer::i=1
read*,X
do
    M=M+(M/100_16)
    if(M>=X)then
        print"(I0)",i
        stop
    endif
    i=i+1
end do
contains
end program bm


      module piyoMod
      implicit none
      integer(16) :: n,m,q,ans=0
      integer(16),allocatable :: abcd(:,:)

      contains
      recursive subroutine solve( cnt,array )
        implicit none
        integer(16),intent(in) :: cnt,array(:)
        integer(16) :: a(size(array)), i,buffer

        a = array
        if( cnt==1 )then
          do i = 1,m
            a(1) = i
            call solve( cnt+1,a )
          end do
        else if( cnt==n+1 )then

          !calc ans
          buffer = 0
          do i = 1,q
            if( a(abcd(i,2))-a(abcd(i,1))==abcd(i,3) )then
              buffer = buffer + abcd(i,4)
            end if
          end do
          ans = max( ans,buffer )

        else
          do i = a(cnt-1),m
            a(cnt) = i
            call solve( cnt+1,a )
          end do
        end if
      end subroutine

      end module

      PROGRAM ManyRequirments
      use piyoMod
      IMPLICIT NONE
      integer(16) :: i,dummy(10)

      read*,n,m,q
      allocate( abcd(q,4) )
      do i = 1,q
        read*,abcd(i,:)
      end do

      call solve(1_16,dummy(1:n))
      print*,ans

      END PROGRAM

Score : 300 points

Problem Statement

Given are positive integers N, M, Q, and Q quadruples of integers ( a_i , b_i , c_i , d_i ).

Consider a sequence A satisfying the following conditions:

A is a sequence of N positive integers.

1 \leq A_1 \leq A_2 \le \cdots \leq A_N \leq M.

Let us define a score of this sequence as follows:

The score is the sum of d_i over all indices i such that A_{b_i} - A_{a_i} = c_i. (If there is no such i, the score is 0.)

Find the maximum possible score of A.

Constraints

All values in input are integers.

2 ≤ N ≤ 10

1 \leq M \leq 10

1 \leq Q \leq 50

1 \leq a_i < b_i \leq N ( i = 1, 2, ..., Q )

0 \leq c_i \leq M - 1 ( i = 1, 2, ..., Q )

(a_i, b_i, c_i) \neq (a_j, b_j, c_j) (where i \neq j)

1 \leq d_i \leq 10^5 ( i = 1, 2, ..., Q )

Input

Input is given from Standard Input in the following format:

N M Q
a_1 b_1 c_1 d_1
:
a_Q b_Q c_Q d_Q

Output

Print the maximum possible score of A.

Sample Input 1

3 4 3
1 3 3 100
1 2 2 10
2 3 2 10

Sample Output 1

110

When A = \{1, 3, 4\}, its score is 110. Under these conditions, no sequence has a score greater than 110, so the answer is 110.

Sample Input 2

4 6 10
2 4 1 86568
1 4 0 90629
2 3 0 90310
3 4 1 29211
3 4 3 78537
3 4 2 8580
1 2 1 96263
1 4 2 2156
1 2 0 94325
1 4 3 94328

Sample Output 2

357500

Sample Input 3

10 10 1
1 10 9 1

Sample Output 3

1


program main

  implicit none
  integer :: nn, mm, qq, ii, tmp
  integer(8) :: tmpans, ans
  integer,allocatable :: a(:)
  integer,allocatable :: b(:)
  integer,allocatable :: c(:)
  integer(8),allocatable :: d(:)
  integer,allocatable :: aa(:)
  integer :: i,j,k,l,m,n,o,p,q,r,s

  read(*,*) nn, mm, qq
  allocate(a(1:qq))
  allocate(b(1:qq))
  allocate(c(1:qq))
  allocate(d(1:qq))
  allocate(aa(1:n))

  do i = 1, qq
    read(*,*) a(i), b(i), c(i), d(i)
  end do

  aa = 0
  ans = 0
  do i = 1, mm
    aa(1) = i
    do j = 1, mm
      aa(2) = j
      if ( nn == 2 ) then
        tmpans = 0
        do ii = 1, qq
          tmp = aa(b(ii))-aa(a(ii))
          if( tmp == c(ii) ) tmpans = tmpans + d(ii)
        end do
        if( tmpans >= ans ) ans = tmpans
      else
        do k = 1, mm
          aa(3) = k
          if ( nn == 3 ) then
            tmpans = 0
            do ii = 1, qq
              tmp = aa(b(ii))-aa(a(ii))
              if( tmp == c(ii) ) tmpans = tmpans + d(ii)
            end do
            if( tmpans >= ans ) ans = tmpans
          else
            do l = 1, mm
              aa(4) = l
              if ( nn == 4 ) then
                tmpans = 0
                do ii = 1, qq
                  tmp = aa(b(ii))-aa(a(ii))
                  if( tmp == c(ii) ) tmpans = tmpans + d(ii)
                end do
                if( tmpans >= ans ) ans = tmpans
              else
                do m = 1, mm
                  aa(5) = m
                  if ( nn == 5 ) then
                    tmpans = 0
                    do ii = 1, qq
                      tmp = aa(b(ii))-aa(a(ii))
                      if( tmp == c(ii) ) tmpans = tmpans + d(ii)
                    end do
                    if( tmpans >= ans ) ans = tmpans
                  else
                    do n = 1, mm
                      aa(6) = n
                      if ( nn == 6 ) then
                        tmpans = 0
                        do ii = 1, qq
                          tmp = aa(b(ii))-aa(a(ii))
                          if( tmp == c(ii) ) tmpans = tmpans + d(ii)
                        end do
                        if( tmpans >= ans ) ans = tmpans
                      else
                        do o = 1, mm
                          aa(7) = o
                          if ( nn == 7 ) then
                            tmpans = 0
                            do ii = 1, qq
                              tmp = aa(b(ii))-aa(a(ii))
                              if( tmp == c(ii) ) tmpans = tmpans + d(ii)
                            end do
                            if( tmpans >= ans ) ans = tmpans
                          else
                            do p = 1, mm
                              aa(8) = p
                              if ( nn == 8 ) then
                                tmpans = 0
                                do ii = 1, qq
                                  tmp = aa(b(ii))-aa(a(ii))
                                  if( tmp == c(ii) ) tmpans = tmpans + d(ii)
                                end do
                                if( tmpans >= ans ) ans = tmpans
                              else
                                do q = 1, mm
                                  aa(9) = q
                                  if ( nn == 9 ) then
                                    tmpans = 0
                                    do ii = 1, qq
                                      tmp = aa(b(ii))-aa(a(ii))
                                      if( tmp == c(ii) ) tmpans = tmpans + d(ii)
                                    end do
                                    if( tmpans >= ans ) ans = tmpans
                                  else
                                    do r = 1, mm
                                      aa(10) = r
                                      if ( nn == 10 ) then
                                        tmpans = 0
                                        do ii = 1, qq
                                          tmp = aa(b(ii))-aa(a(ii))
                                          if( tmp == c(ii) ) tmpans = tmpans + d(ii)
                                        end do
                                        if( tmpans >= ans ) ans = tmpans
                                      else
                                      end if
                                    end do
                                  end if
                                end do
                              end if
                            end do
                          end if
                        end do
                      end if
                    end do
                  end if
                end do
              end if
            end do
          end if
        end do
      end if
    end do
  end do

  print*, ans

end program main


program ksfloor
 implicit none
 integer :: n,answer,i
 integer,allocatable ::s(:)
 real :: a,b

 read*,a,b,n
 allocate(s(0:n))

 do i=0,n
  s(i)=aint(a*i/b)-a*aint(i/b)
 end do

 answer=maxval(s)
 print*,answer

end program ksfloor

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 name
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: n,m,i,c,d

    read*, n,m
    c=0
    d=1
    do i=1,m
        print'(2(i0,1x))', c+1,d+1
        d=d+1
    end do

end program name

Score : 500 points

Problem Statement

You are going to hold a competition of one-to-one game called AtCoder Janken. (Janken is the Japanese name for Rock-paper-scissors.)
N players will participate in this competition, and they are given distinct integers from 1 through N.
The arena has M playing fields for two players. You need to assign each playing field two distinct integers between 1 and N (inclusive).
You cannot assign the same integer to multiple playing fields.
The competition consists of N rounds, each of which proceeds as follows:

For each player, if there is a playing field that is assigned the player's integer, the player goes to that field and fight the other player who comes there.

Then, each player adds 1 to its integer. If it becomes N+1, change it to 1.

You want to ensure that no player fights the same opponent more than once during the N rounds.
Print an assignment of integers to the playing fields satisfying this condition.
It can be proved that such an assignment always exists under the constraints given.

Constraints

1 \leq M

M \times 2 +1 \leq N \leq 200000

Input

Input is given from Standard Input in the following format:

N M

Output

Print M lines in the format below.
The i-th line should contain the two integers a_i and b_i assigned to the i-th playing field.

a_1 b_1
a_2 b_2
:
a_M b_M

Sample Input 1

4 1

Sample Output 1

2 3

Let us call the four players A, B, C, and D, and assume that they are initially given the integers 1, 2, 3, and 4, respectively.

The 1-st round is fought by B and C, who has the integers 2 and 3, respectively. After this round, A, B, C, and D have the integers 2, 3, 4, and 1, respectively.

The 2-nd round is fought by A and B, who has the integers 2 and 3, respectively. After this round, A, B, C, and D have the integers 3, 4, 1, and 2, respectively.

The 3-rd round is fought by D and A, who has the integers 2 and 3, respectively. After this round, A, B, C, and D have the integers 4, 1, 2, and 3, respectively.

The 4-th round is fought by C and D, who has the integers 2 and 3, respectively. After this round, A, B, C, and D have the integers 1, 2, 3, and 4, respectively.

No player fights the same opponent more than once during the four rounds, so this solution will be accepted.

Sample Input 2

7 3

Sample Output 2

1 6
2 5
3 4


program name
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: n,m,i,c,d

    read*, n,m
    c=0
    d=1
    do i=1,m
        print'(2(i0,1x))', c+1,d+1
        c=modulo(c-1,n)
        d=d+1
    end do

end program name


   program abc164c
      implicit none
      integer::n
      character(10),allocatable::s(:)
      integer::i,count

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

         call sort_quick(1,n,n,s)

         count=1
         do i=1,n-1
            if (s(i)/=s(i+1)) count=count+1
         end do

         print *,count
         stop

   contains
      recursive subroutine sort_quick(left,right,n,array)
       ! split two group with the medium element as the standard, and sort the array at each section
       ! if want to sort the array of strings, change 'double precision' variables to 'character' variables
         implicit none
         integer,intent(in)::n,left,right
         character(10),intent(inout)::array(1:n)
         integer::i,j
         character(10)::s,w

            if (left.ge.right) return

           !第一要素を一時的に軸とし、大小のグループへ分ける
            s=array(left)
            i=left
            j=right+1

            do
               i=i+1
               do while (s.gt.array(i))
                  i=i+1
               end do

               j=j-1
               do while (s.lt.array(j))
                  j=j-1
               end do

               if (i.ge.j) exit
              !スワップ
               w=array(i)
               array(i)=array(j)
               array(j)=w
            end do

           !軸を正しい位置に入れる
            array(left)=array(j)
            array(j)=s

            call sort_quick(left,  j-1,n,array) !左部分再帰
            call sort_quick( j+1,right,n,array) !右部分再帰

            return
      end subroutine sort_quick

   end program abc164c

Score : 300 points

Problem Statement

You drew lottery N times. In the i-th draw, you got an item of the kind represented by a string S_i.

How many kinds of items did you get?

Constraints

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

S_i consists of lowercase English letters and has a length between 1 and 10 (inclusive).

Input

Input is given from Standard Input in the following format:

N
S_1
:
S_N

Output

Print the number of kinds of items you got.

Sample Input 1

3
apple
orange
apple

Sample Output 1

2

You got two kinds of items: apple and orange.

Sample Input 2

5
grape
grape
grape
grape
grape

Sample Output 2

1

Sample Input 3

4
aaaa
a
aaa
aa

Sample Output 3

4


   program abc164c
      implicit none
      integer::n
      character(10),allocatable::s1(:),s2(:) !元の配列、ソート後の配列
      integer::i,j,count
      integer,parameter::p=100

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

         do i=1,p
            call sort_quick(n, s1, s2)
         end do

         count=1
         do i=1,n-1
            if (s2(i)==s2(i+1)) cycle
            count=count+1
         end do

         print *, count
         stop

   contains
      subroutine sort_quick(num, a, b)
         implicit none
         integer, intent(in)  :: num
         character(10), intent(in):: a(num)
         character(10), intent(out) :: b(num)
         integer:: i, j
         character(10)::w

         ! 配列コピー
            b(:)=a(:)

         ! ソート処理
            call quick(1, num, num, b)
      end subroutine sort_quick

      recursive subroutine quick(left, right, num, a)
         implicit none
         integer, intent(in)    :: left, right, num
         character(10), intent(inout) :: a(num)
         integer :: i, j
         character(10)::s, w

            if (left >= right) return

         ! 最左項を軸に. 軸より小さいグループ. 軸より大きいグループ.
            s = a(left)
            i = left
            j = right + 1
            do
               i = i + 1
               do while (a(i) < s)
                  i = i + 1
               end do
               j = j - 1
               do while (a(j) > s)
                  j = j - 1
               end do
               if (i >= j) exit
           ! スワップ
               w    = a(i)
               a(i) = a(j)
               a(j) = w
            end do

        ! 軸を正しい位置に挿入
            a(left) = a(j)
            a(j)    = s
            call quick(left,  j - 1, num, a)  ! 左部分列に対する再帰呼び出し
            call quick(j + 1, right, num, a)  ! 右部分列に対する再帰呼び出し
      end subroutine quick

   end program abc164c


module treap
  implicit none
  type node
    type(node), pointer :: left => null(), right => null()
    character(10) :: key
    integer :: val
    integer :: pri  ! min-heap
    integer :: cnt = 1
  end type node

  contains

  subroutine update(root)
    implicit none
    type(node), pointer, intent(in) :: root
    root%cnt = my_count(root%left) + my_count(root%right) + 1
  end subroutine update

  function my_count(root)
    implicit none
    type(node), pointer, intent(in) :: root
    integer :: my_count
    if (associated(root)) then
      my_count = root%cnt
    else
      my_count = 0
    end if
  end function my_count

  function rotate_ccw(root)
    implicit none
    type(node), pointer, intent(in) :: root
    type(node), pointer :: tmp, rotate_ccw
    if (.not. associated(root%right)) stop 1
    tmp => root%right
    root%right => tmp%left
    tmp%left => root
    rotate_ccw => tmp
    call update(root)
    call update(tmp)
  end function rotate_ccw

  function rotate_cw(root)
    implicit none
    type(node), pointer, intent(in) :: root
    type(node), pointer :: tmp, rotate_cw
    if (.not. associated(root%left)) stop 1
    tmp => root%left
    root%left => tmp%right
    tmp%right => root
    rotate_cw => tmp
    call update(root)
    call update(tmp)
  end function rotate_cw

  recursive function insert(root, key, val, pri) result(res)
    implicit none
    type(node), pointer, intent(in) :: root
    integer, intent(in) :: pri
    character(10), intent(in) :: key
    integer, intent(in) :: val
    type(node), pointer :: res

    if (.not. associated(root)) then
      allocate(res)
      res%key = key
      res%pri = pri
      res%val = val
    else
      res => root
      if (key > root%key) then
        root%right => insert(root%right, key, val, pri)
        call update(root)
        if (root%pri > root%right%pri) then
          res => rotate_ccw(res)
        end if
      else
        root%left => insert(root%left, key, val, pri)
        call update(root)
        if (root%pri > root%left%pri) then
          res => rotate_cw(res)
        end if
      end if
    end if
  end function insert

  recursive function erase(root, key) result(res)
    implicit none
    type(node), pointer, intent(in) :: root
    character(10), intent(in) :: key
    type(node), pointer :: res, tmp

    if (.not. associated(root)) then
      print *, "Erase failed"
      stop 1
    end if

    if (key < root%key) then
      root%left => erase(root%left, key)
      res => root
    else if (key > root%key) then
      root%right => erase(root%right, key)
      res => root
    else
      if ((.not. associated(root%left)) .or. (.not. associated(root%right))) then
        tmp => root
        if (.not. associated(root%left)) then
          res => root%right
        else
          res => root%left
        end if
        deallocate(tmp)
      else
        if (root%left%pri < root%right%pri) then
          res => rotate_ccw(root)
          res%left => erase(res%left, key)
        else
          res => rotate_cw(root)
          res%right => erase(res%right, key)
        end if
      end if
    end if
    if (associated(res)) call update(res)
  end function erase

  recursive function find_node(root, key) result(res)
    implicit none
    type(node), pointer, intent(in) :: root
    character(10), intent(in) :: key
    type(node), pointer :: res
    if (.not. associated(root)) then
      res => null()
    else if (root%key == key) then
      res => root
    else if (key < root%key) then
      res => find_node(root%left, key)
    else
      res => find_node(root%right, key)
    end if
  end function find_node

  recursive function kth_node(root, k) result(res)
    implicit none
    type(node), pointer, intent(in) :: root
    integer, intent(in) :: k
    type(node), pointer :: res
    if (.not. associated(root)) then
      res => null()
    else if (k <= my_count(root%left)) then
      res => kth_node(root%left, k)
    else if (k == my_count(root%left) + 1) then
      res => root
    else
      res => kth_node(root%right, k - my_count(root%left) - 1)
    end if
  end function kth_node

  recursive subroutine delete_all(root)
    implicit none
    type(node), pointer, intent(inout) :: root
    if (.not. associated(root)) return

    call delete_all(root%left)
    call delete_all(root%right)
    deallocate(root)
    nullify(root)
  end subroutine delete_all

  recursive subroutine inorder(root, keys, vals, counter)
    implicit none
    type(node), pointer, intent(in) :: root
    character(10), intent(inout) :: keys(:)
    integer, intent(inout) :: vals(:)
    integer, intent(inout) :: counter
    if (.not. associated(root)) return

    call inorder(root%left, keys, vals, counter)
    counter = counter + 1
    keys(counter) = root%key
    vals(counter) = root%val
    call inorder(root%right, keys, vals, counter)
  end subroutine inorder
end module treap

module dictionary
  use treap
  implicit none

  type dict
    type(node), pointer :: root => null()
    integer :: randstate = 1231767121
    ! contains
    ! final :: destruct_dict
  end type dict

  contains

  pure function xorshift32(i)
    implicit none
    integer(4), intent(in) :: i
    integer(4) :: xorshift32
    if (i == 0) then
      xorshift32 = 1231767121
    else
      xorshift32 = i
    end if
    xorshift32 = ieor(xorshift32, ishft(xorshift32, 13))
    xorshift32 = ieor(xorshift32, ishft(xorshift32, -17))
    xorshift32 = ieor(xorshift32, ishft(xorshift32, 15))
  end function xorshift32

  function get_val(t, key)
    implicit none
    type(dict), intent(in) :: t
    character(10), intent(in) :: key
    type(node), pointer :: nd
    integer :: get_val
    nd => find_node(t%root, key)
    if (.not. associated(nd)) then
      stop 105
    end if
    get_val = nd%val
  end function get_val

  function exists(t, key)
    implicit none
    type(dict), intent(in) :: t
    character(10), intent(in) :: key
    type(node), pointer :: nd
    logical :: exists
    nd => find_node(t%root, key)
    exists = (associated(nd))
  end function exists

  subroutine insert_or_assign(t, key, val)
    implicit none
    type(dict), intent(inout) :: t
    character(10), intent(in) :: key
    integer, intent(in) :: val
    type(node), pointer :: nd
    nd => find_node(t%root, key)
    if (associated(nd)) then
      nd%val = val
    else  ! This implementation is not optimal
      t%root => insert(t%root, key, val, t%randstate)
      t%randstate = xorshift32(t%randstate)
    end if
  end subroutine insert_or_assign

  subroutine remove(t, key)
    implicit none
    type(dict), intent(inout) :: t
    character(10), intent(in) :: key
    t%root => erase(t%root, key)
  end subroutine remove

  function get_kth_key(t, k)
    implicit none
    type(dict), intent(in) :: t
    integer, intent(in) :: k
    type(node), pointer :: res
    character(10) :: get_kth_key
    if (k < 1 .or. k > my_count(t%root)) then
      print *, "get_kth_key failed"
      stop 2
    else
      res => kth_node(t%root, k)
      get_kth_key = res%key
    end if
  end function get_kth_key

  subroutine get_keys_vals(t, keys, vals, n)
    implicit none
    type(dict), intent(in) :: t
    integer, intent(in) :: n
    character(10), intent(out) :: keys(n)
    integer, intent(out) :: vals(n)
    integer :: counter
    if (my_count(t%root) /= n) stop 5
    counter = 0
    call inorder(t%root, keys, vals, counter)
  end subroutine get_keys_vals

  function get_size(t)
    implicit none
    type(dict), intent(in) :: t
    integer :: get_size
    get_size = my_count(t%root)
  end function get_size

  subroutine destruct_dict(t)
    implicit none
    type(dict), intent(inout) :: t
    call delete_all(t%root)
  end subroutine destruct_dict

end module dictionary

program main
  use dictionary
  implicit none
  integer :: n, i, cnts(200000), mc, k
  character(10) :: s(200000)
  type(dict) :: cnt

  read (*, *) n
  do i = 1, n
    read (*, *) s(i)
    if (exists(cnt, s(i))) then
      call insert_or_assign(cnt, s(i), 1 + get_val(cnt, s(i)))
    else
      call insert_or_assign(cnt, s(i), 1)
    end if
  end do
  k = get_size(cnt)
  write(*,*)k
end program main


program c1

 implicit none

 integer :: n,i,j,k,l
 character,allocatable :: s(:)*10

 read*,n

 allocate(s(n))

 read*,s

 k=n
 do i=1,n
  do j=1,n-i
   if(s(i+j)==s(i)) s(i+j)='0'
  end do
 end do

 do l=1,n
  if(s(l)=='0') k=k-1
 end do

 if(k==0) k=1

 print*,k

end program c1


program i_hate_matrix_construction
  implicit none
  integer, parameter :: intkind = 16
  integer :: n, i, j, k
  integer, dimension(500) :: s = 0, t = 0, p = 0, q = 0, x = 0, y = 0
  integer(intkind), dimension(500) :: u = 0, v = 0
  integer(intkind), dimension(500, 500) :: a = 0
  read(*,*) n
  read(*,*) s(1:n)
  read(*,*) t(1:n)
  read(*,*) u(1:n)
  read(*,*) v(1:n)
  do k = 0, 64
    do i = 1, n
      p(i) = pattern(s(i), btest(u(i), k))
      q(i) = pattern(t(i), btest(v(i), k))
    end do
    x(1:n) = 0
    y(1:n) = 0
    do i = 1, n
      do j = 1, n
        if (p(i) + q(j) == 1) call halt()
        if (p(i) == 1 .or. q(j) == 1 .or. p(i) + q(j) == 6) then
          a(i, j) = ibset(a(i, j), k)
          x(i) = x(i) + 1
          y(j) = y(j) + 1
        end if
      end do
    end do
    j = 1
    do i = 1, n
      if (p(i) == 2 .and. x(i) == n) call halt()
      if (.not.(p(i) == 3 .and. x(i) == 0)) cycle
      do while (j <= n .and. .not.(q(j) == 2 .and. y(j) < n - 1))
        j = j + 1
      end do
      if (j > n) call halt()
      a(i, j) = ibset(a(i, j), k)
      x(i) = x(i) + 1
      y(j) = y(j) + 1
    end do
    i = 1
    do j = 1, n
      if (q(j) == 2 .and. y(j) == n) call halt()
      if (.not.(q(j) == 3 .and. y(j) == 0)) cycle
      do while (i <= n .and. .not.(p(i) == 2 .and. x(i) < n - 1))
        i = i + 1
      end do
      if (i > n) call halt()
      a(i, j) = ibset(a(i, j), k)
      x(i) = x(i) + 1
      y(j) = y(j) + 1
    end do
  end do
  do i = 1, n
    write(*,'(i0)',advance='no') a(i, 1)
    do j = 2, n
      write(*,'(x,i0)',advance='no') a(i, j)
    end do
    write(*,*)
  end do
contains
  integer function pattern(s, b) result(res)
    integer, intent(in) :: s
    logical, intent(in) :: b
    ! 0: all 0
    ! 1: all 1
    ! 2: any 0
    ! 3: any 1
    res = mod(2 * s + merge(0, 1, b) + 1, 4)
  end
  subroutine halt()
    write(*,'(i0)') -1
    stop
  end
end program i_hate_matrix_construction

Score : 600 points

Problem Statement

Given are an integer N and arrays S, T, U, and V, each of length N.
Construct an N×N matrix a that satisfy the following conditions:

a_{i,j} is an integer.

0 \leq a_{i,j} \lt 2^{64}.

If S_{i} = 0, the bitwise AND of the elements in the i-th row is U_{i}.

If S_{i} = 1, the bitwise OR of the elements in the i-th row is U_{i}.

If T_{i} = 0, the bitwise AND of the elements in the i-th column is V_{i}.

If T_{i} = 1, the bitwise OR of the elements in the i-th column is V_{i}.

However, there may be cases where no matrix satisfies the conditions.

Constraints

All values in input are integers.

1 \leq N \leq 500

0 \leq S_{i} \leq 1

0 \leq T_{i} \leq 1

0 \leq U_{i} \lt 2^{64}

0 \leq V_{i} \lt 2^{64}

Input

Input is given from Standard Input in the following format:

N
S_{1} S_{2} ... S_{N}
T_{1} T_{2} ... T_{N}
U_{1} U_{2} ... U_{N}
V_{1} V_{2} ... V_{N}

Output

If there exists a matrix that satisfies the conditions, print one such matrix in the following format:

a_{1,1} ... a_{1,N}
:
a_{N,1} ... a_{N,N}

Note that any matrix satisfying the conditions is accepted.

If no matrix satisfies the conditions, print -1.

Sample Input 1

2
0 1
1 0
1 1
1 0

Sample Output 1

1 1
1 0

In Sample Input 1, we need to find a matrix such that:

the bitwise AND of the elements in the 1-st row is 1;

the bitwise OR of the elements in the 2-nd row is 1;

the bitwise OR of the elements in the 1-st column is 1;

the bitwise AND of the elements in the 2-nd column is 0.

Sample Input 2

2
1 1
1 0
15 15
15 11

Sample Output 2

15 11
15 11


program ABC163_B
  integer,allocatable::A(:)
  read(*,*)N,M
  allocate(A(M))
  read(*,*)A
  if(N >= sum(A)) then
     write(*,*)N-sum(A)
  else
     write(*,*)'-1'
  end if
end program ABC163_B

Score : 200 points

Problem Statement

Takahashi has N days of summer vacation.

His teacher gave him M summer assignments. It will take A_i days for him to do the i-th assignment.

He cannot do multiple assignments on the same day, or hang out on a day he does an assignment.

What is the maximum number of days Takahashi can hang out during the vacation if he finishes all the assignments during this vacation?

If Takahashi cannot finish all the assignments during the vacation, print -1 instead.

Constraints

1 \leq N \leq 10^6

1 \leq M \leq 10^4

1 \leq A_i \leq 10^4

Input

Input is given from Standard Input in the following format:

N M
A_1 ... A_M

Output

Print the maximum number of days Takahashi can hang out during the vacation, or -1.

Sample Input 1

41 2
5 6

Sample Output 1

30

For example, he can do the first assignment on the first 5 days, hang out on the next 30 days, and do the second assignment on the last 6 days of the vacation. In this way, he can safely spend 30 days hanging out.

Sample Input 2

10 2
5 6

Sample Output 2

-1

He cannot finish his assignments.

Sample Input 3

11 2
5 6

Sample Output 3

0

He can finish his assignments, but he will have no time to hang out.

Sample Input 4

314 15
9 26 5 35 8 9 79 3 23 8 46 2 6 43 3

Sample Output 4

9


program main
    implicit none
    integer n,m
    integer,allocatable::a(:)
    read(*,*) n,m
    allocate(a(m))
    read(*,*) a(1:m)
    n=n-sum(a(1:m))
    if(n>=0) write(*,*) n
    if(n<0) write(*,*) "-1"
end program main


program main
integer :: n
integer,allocatable :: a(:),cnt(:)

read(*,*)n
allocate(a(n),cnt(n))
read(*,*)a(1:n-1)
cnt = 0
do i = 1,n
cnt(a(i)) =  cnt(a(i)) + 1
end do
do i = 1,n
write(*,*)cnt(i)
end do
end program main

Score : 300 points

Problem Statement

A company has N members, who are assigned ID numbers 1, ..., N.

Every member, except the member numbered 1, has exactly one immediate boss with a smaller ID number.

When a person X is the immediate boss of a person Y, the person Y is said to be an immediate subordinate of the person X.

You are given the information that the immediate boss of the member numbered i is the member numbered A_i. For each member, find how many immediate subordinates it has.

Constraints

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

1 \leq A_i < i

Input

Input is given from Standard Input in the following format:

N
A_2 ... A_N

Output

For each of the members numbered 1, 2, ..., N, print the number of immediate subordinates it has, in its own line.

Sample Input 1

5
1 1 2 2

Sample Output 1

2
2
0
0
0

The member numbered 1 has two immediate subordinates: the members numbered 2 and 3.

The member numbered 2 has two immediate subordinates: the members numbered 4 and 5.

The members numbered 3, 4, and 5 do not have immediate subordinates.

Sample Input 2

10
1 1 1 1 1 1 1 1 1

Sample Output 2

9
0
0
0
0
0
0
0
0
0

Sample Input 3

7
1 2 3 4 5 6

Sample Output 3

1
1
1
1
1
1
0


program main
  implicit none
  integer :: N
  integer,allocatable :: A(:)
  integer,allocatable :: out(:)
  integer :: i
  read(*,*) N
  allocate(A(N))
  read(*,*) A(2:)
  allocate(out(N))
  out(:) = 0
  do i = 2,N
     out(A(i)) = out(A(i)) + 1
  end do
  do i = 1,N
     write(*,'(i0)') out(i)
  end do
end program main


program main
    implicit none
    integer n,i,j,p
    integer,allocatable::a(:)
    read(*,*) n
    allocate(a(n))
    read(*,*) a(2:n)
    do i=1,n
        p=0
        do j=i+1,n
            if(a(j)-i>0) exit
            if(a(j)-i==0) p=p+1
        end do
        write(*,*) p
    end do
    deallocate(a)
end program main


program hello
implicit none

integer N,i,j
integer,allocatable,dimension(:)::A
integer,allocatable,dimension(:)::cont

read*,N
allocate(A(N-1),cont(N-1))
read*,A

do j = 1,N
 cont(j) = 0
end do

do i = 1,N
do j = 1,N
	if (A(i) == j) then
    	cont(j) = cont(j) +1
    endif
end do
end do

do i = 1,N
	write(*,*)cont(i)
end do

end program Hello


program sample
implicit none
integer(8) N,A(200000),B(200000),i
read(*,*) N
read(*,*) (A(i),i=2,N)
do i=2,N
B(A(i))=B(A(i))+1
end do
do i=1,N
write(*,'(i0)') B(i)
end do

stop
end program sample


program problemC
  implicit none

integer :: NMAX,n
integer, allocatable :: A(:), man(:)

read(*,*) NMAX

allocate(A(NMAX-1), man(NMAX))

read(*,*) A

man(:)=0

do n=1, NMAX-1
  man(A(n))=man(A(n))+1
end do

do n=1,NMAX
  write(*,*) man(n)
end do

deallocate(A,man)

end program


program management
  implicit none
  integer :: n, a(200000) = -1, i, b(200000) = 0
  read(*,*) n
  read(*,*) a(2:n)
  do i = 2, n
    b(a(i)) = b(a(i))+1
  end do
  do i = 1, n
    write(*,'(i0)') b(i)
  end do
end program management


program sum_of_large_numbers
  implicit none
  integer(8), parameter :: m = 1000000007_8, h = (m+1)/2, s = (m+1)/6
  integer(8) :: n, k, i
  read(*,*) n, k
  write(*,'(i0)') modulo(f(n+1)-f(k-1),m)
contains
  integer(8) function f(i)
    integer(8), intent(in) :: i
    f = i*mod((n+1)*mod(h*(i+1),m)-mod(s*mod((i+1)*(i*2+1),m),m)+1,m)
  end
end program sum_of_large_numbers


program sum_of_large_numbers
  implicit none
  integer(8), parameter :: md = 1000000007_8
  integer :: n, k, i
  integer(8) :: x = 0, l = -1, r
  read(*,*) n, k
  r = n
  do i = 1, n+1
    if (i >= k) x = modulo(x+r-l,md)
    l = mod(l+i,md)
    r = mod(r+(n-i),md)
  end do
  write(*,'(i0)') x
end program sum_of_large_numbers


program name
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: n,k,l,r,ans,i
    integer(int64),parameter:: md=1000000007

    read*, n,k

    l=0
    r=n
    ans=0
    do i=1,n+1
        if (i >= k) ans=mod(ans+(r+1-l),md)
        l=l+i
        r=r+(n-i)
    end do
    print*, ans
end program name


program problemA
  implicit none

integer(8) :: N,K, a,b,c,d,ans

read(*,*) N,K

ans=0
if (K<=N) then
  do while (K<=N)
    call ex(N-1,a)
    b=2*N-K+1
    call ex(N-K+1,c)
    call ex(K-1,d)
    ans=ans+a*b/(c*d)
    K=K+1
  end do
end if

ans=ans+1

if (K==1) ans=ans+1

write(*,*) mod(ans,10**9+7)

stop
end program

subroutine ex(n,k)
  implicit none

  integer(8), intent(in) :: n
  integer(8), intent(out) :: k
  integer(8) :: l, i

  l=1
  i=n

  do while (i>0)
    l=l*i
    i=i-1
  end do

  k=l

  return
end subroutine ex


program fizzbuzz

 implicit none
 integer(8) :: n,ans,i
 integer(8),allocatable :: a(:)

 read*,n
 allocate(a(n))
 ans=0

 do i=1,n
  if(mod(i,3)==0 .or. mod(i,5)==0) then
      a(i)=0
  else
      a(i)=i
     ! print*,a(i)
  end if
  ans=ans+a(i)
 end do

 print*,ans

end program fizzbuzz

Score : 200 points

Problem Statement

Let us define the FizzBuzz sequence a_1,a_2,... as follows:

If both 3 and 5 divides i, a_i=\mbox{FizzBuzz}.

If the above does not hold but 3 divides i, a_i=\mbox{Fizz}.

If none of the above holds but 5 divides i, a_i=\mbox{Buzz}.

If none of the above holds, a_i=i.

Find the sum of all numbers among the first N terms of the FizzBuzz sequence.

Constraints

1 \leq N \leq 10^6

Input

Input is given from Standard Input in the following format:

N

Output

Print the sum of all numbers among the first N terms of the FizzBuzz sequence.

Sample Input 1

15

Sample Output 1

60

The first 15 terms of the FizzBuzz sequence are:

1,2,\mbox{Fizz},4,\mbox{Buzz},\mbox{Fizz},7,8,\mbox{Fizz},\mbox{Buzz},11,\mbox{Fizz},13,14,\mbox{FizzBuzz}

Among them, numbers are 1,2,4,7,8,11,13,14, and the sum of them is 60.

Sample Input 2

1000000

Sample Output 2

266666333332

Watch out for overflow.


      PROGRAM piyo
      IMPLICIT NONE
      integer(16) :: ans,n,i

      read*,n

      ans = 0
      do i = 1,n
        if( mod(i,3)/=0 .and. mod(i,5)/=0 )then
          ans = ans + i
        endif
      end do

      print*,ans

      END PROGRAM