PROGRAM c168b

IMPLICIT NONE
INTEGER :: i, K
CHARACTER(LEN=100) :: S

READ *, K
READ *, S

i = LEN_TRIM(S)

IF (i > K) THEN
 PRINT *, S(1:K), "..."
ELSE
 PRINT *, S
END IF

END PROGRAM c168b

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 ABC168_B
  character(100)::S
  read(*,*)K
  read(*,*)S
  if(len_trim(S)<=K) then
     write(*,*)trim(S)
  else
     write(*,*)S(1:K),'...'
  end if
end program ABC168_B

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

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

    integer(8) :: a, b
    real(8) :: h, m
    real(8), parameter :: pi = 3.141592653589_8
    real(8) :: int1, int2, realp
    read(*,*) a, b, h, m

    int1 = real(a**2+b**2, kind=8)
    int2 = real(2*a*b, kind=8)
    realp = cos((h/6-m/30+m/360)*pi)

    write(*,*) sqrt(int1-int2*realp)

    stop
end program abc168C

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
  integer i
  double precision dX, dY, P, ans, A, B, H, M
  read(*, *)A, B, H, M
  !read(*, *) (L(i), i = 1,N)
  P = 3.141592653589793238462643383279d0
  P = 2*P
  P = P/12
  H = H+M/60
  M = M/5
  dX = A*cos(-P*H)-B*cos(-P*M)
  dY = A*sin(-P*H)-B*sin(-P*M)
  ans = (dX**2+dY**2)**(0.5)
  write(*, *) ans
end program main


      PROGRAM Colon
      IMPLICIT NONE
      real(16),parameter :: pi=3.141592653589793238
      real(16) :: a,b,h,m, ans2
      real(16) :: angleA,angleB,angleSub

      read*,a,b,h,m

      angleA = h / real(12) * real(2) * pi
      angleB = m / real(60) * real(2) * pi

   !   print*,angleA,angleB,abs(angleA-angleB)

      angleSub = max(angleA,angleB) - min(angleA,angleB)
  !    print*,angleSub

      ans2 = a*a + b*b - (real(2)*a*b*cos( angleSub ))
      print*,sqrt(ans2)

    !  print*,cos( angleSub )

      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

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.


PROGRAM c167a

IMPLICIT NONE
CHARACTER(LEN=10) :: S
CHARACTER(LEN=11) :: T
INTEGER :: b

READ *, S
READ *, T

b = LEN_TRIM(T)

 IF (T(1 : b-1) == TRIM(S)) THEN
  PRINT *, 'Yes'
 ELSE
  PRINT *, 'No'
 END IF

END PROGRAM c167a

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 registration
  implicit none
  character(11) :: s, t
  integer :: n
  read(*,*) s
  read(*,*) t
  n = len_trim(s)
  if (s(1:n) == t(1:n)) then
    write(*,'(a)') "Yes"
  else
    write(*,'(a)') "No"
  end if
end program registration


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 : 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 ABC167_C
  integer(8)::N,M,X,cost,ans=10000000
  integer(8),allocatable::C(:),A(:,:),S(:)
  read(*,*)N,M,X
  allocate(C(N),A(N,M),S(M))
  do i=1,N
     read(*,*)C(i),A(i,:)
  end do
  do i=0,2**N-1
     S=0
     cost=0
     do j=0,N-1
        if(btest(i,j)) then  !iのj+1番目が1なら真
           do k=1,m
              S(k)=S(k)+A(j+1,k)
           end do
           cost=cost+C(j+1)
        end if
        if(minval(S)>=X) ans=min(cost,ans)
     end do
  end do
  if(ans==10000000) then
     write(*,*)'-1'
  else
     write(*,*)ans
  end if
end program ABC167_C

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 skillUp
      IMPLICIT NONE
      integer(16) :: n,m,x
      integer(16),allocatable :: c(:),a(:,:),su(:)
      integer(16) :: i,k
      logical,allocatable :: flag(:)

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

      outer:do i = i,2**n-1
        do k = 1,n
          flag(k) = btest( i,k-1 )
        end do
  !      print*,flag

        do k = 1,m
          su(k) = sum( a(:,k),flag )
        end do
        !print*,su
        do k = 1,m
          if( su(k)<x )then
            cycle outer
          end if
        end do

        print*,sum( c,flag )
        stop

      end do outer

      print*,'-1'

  !    print*,n,m,x
  !    do i = 1,n
  !      print*,c(i),a(i,1:m)
  !    end do

      END PROGRAM


program main
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32)::n,m,i,x,j,k,ans,cnt
    integer(int32), allocatable:: c(:), a(:,:),r(:)
    logical,allocatable:: buy(:)

    read*, n,m,x

    allocate(c(n), a(m,n), r(m))
    allocate(buy(n))

    do i=1,n
        read*, c(i), a(:,i)
    end do
    ans = 1000000000
    do i=0,2**n
        do j=1,n
            buy(j) = btest(i,j-1)
        end do
        r(:) = 0
        cnt=0
        do j=1,n
            if (buy(j))then
                r(:) = r(:) + a(:,j)
                cnt=cnt+c(j)
            end if
        end do
        if (minval(r) >= x) ans = min(ans,cnt)
    end do
    if (ans == 1000000000) ans=-1
    print'(i0)', ans
end program main


program main
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: n,m,k,ans,i,c,f
    integer(int64), allocatable:: frac(:), ifrac(:)
    integer(int64),parameter:: md = 998244353

    read*, n,m,k

    allocate(frac(0:n), ifrac(0:n))
    frac(0) = 1
    ifrac(0) = 1
    do i=1,n
        frac(i) = mod(frac(i-1)*i,md)
        ifrac(i) = inv(frac(i),md)
    end do

    do i=0,k
        f = mod(m*factorial(m-1,n-i-1,md),md)
        c = comb(n-1,i,md)
        ans = mod(ans + mod(f*c,md),md)
    end do

    print'(i0)', ans
contains
    function comb(a,b,md) result(ret)
        integer(int64):: a,b,md
        integer(int64):: ret
        ret = mod(mod(frac(a)*ifrac(b),md)*ifrac(a-b),md)
    end function

    function inv(x,md) result(ret)
        integer(int64),intent(in):: x,md
        integer(int64):: ret
        ret = factorial(x,md-2,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
end program main

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 main
  implicit none
  character(len=3) :: S

  read(*,*) S

  if (S == 'ABC') write(*,'(a)') 'ARC'
  if (S == 'ARC') write(*,'(a)') 'ABC'
end program main

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 main
  implicit none
  integer :: n, i, j, k, d, a(100)
  integer :: pena(100)
  pena(:) = 0
  read *, n, k
  pena(1:n) = 1
  do i = 1, k
    read *, d
    read *, a(1:d)
    do j = 1, d
      pena(a(j)) = 0
    end do
  end do
  print "(i0)", sum(pena)
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
  integer,allocatable :: d(:),A(:,:)
  logical,allocatable :: have(:)
  integer :: i,j

  read(*,*) N,K
  allocate(d(K))
  allocate(A(K,N))
  allocate(have(N))
  have(:) = .false.
  do i = 1,K
     read(*,*) d(i)
     read(*,*) A(i,1:d(i))
  end do

  do i = 1,K
     do j = 1,d(i)
        have(A(i,j)) = .true.
     end do
  end do

  write(*,'(i0)') count(.not.have)

end program main


   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
               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

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, k, i, j
  integer, allocatable :: h(:), a(:),b(:), cnct(:)
  integer :: ans, maxh

  read(*,*) n,m
  allocate(h(n))
  allocate(a(m))
  allocate(b(m))
  allocate(cnct(n))
  read(*,*) h(i)
  cnct = 0
  do i = 1, m
    read(*,*) a(i), b(i)
    cnct(a(i)) = cnct(a(i)) + 1
    cnct(b(i)) = cnct(b(i)) + 1
  end do

  ans = 0
  maxh = maxval(h)
  do i = 1, n
    if( maxh == h(i) .or. cnct(i) == 0 ) ans = ans + 1
  end do

  print*, ans

end program main


program main
    implicit none
    integer n,m
    integer, allocatable, dimension(:) :: h, a, b, ex
    integer i,j,k,sum

    read(*,*) n, m
    allocate(h(n))
    allocate(a(m))
    allocate(b(m))
    allocate(ex(n))

    do i = 1,n
        ex(i) = 0
    end do

    read(*,*) h
    do i = 1,m
        read(*,*) a(i), b(i)
        if(h(a(i)) > h(b(i))) then
            ex(b(i)) = 1
        else if (h(a(i)) < h(b(i))) then
            ex(a(i)) = 1
        else if (h(a(i)) == h(b(i))) then
            ex(a(i)) = 1
            ex(b(i)) = 1
        end if
    end do

    sum = 0
    do i = 1,n
        sum = sum + ex(i)
    end do

    write(*,*) n-sum

end program


program ABC166C
    implicit none
    integer(8)::N,M,i,A,B,ans
    integer(8),allocatable,dimension(:)::H,checked,C
    read*,N,M
    allocate(H(N))
    allocate(checked(N))
    allocate(C(N))
    read*,H
    C=0
    checked=0
    ans=0

    do i=1,M
        read*,A,B
        checked(A)=1
        checked(B)=1
        if(H(A)>H(B))then
            C(A)=1
            C(B)=0
        else if(H(B)>H(A))then
            C(B)=1
            C(A)=0
        end if
    end do

    do i=1,N
        if(C(i)==1.or.checked(i)==0)then
            ans=ans+1
        end if
    end do

    print'(i0)',ans
end program ABC166C


program i_hate_factorization
    integer(8) X
    integer(8) 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

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 name
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: x,n,i,j
    integer(int64), allocatable:: f(:)

    read*, x
    n=0
    do while( n**5 <= 100000000000000000_8)
        n=n+1
    end do
    allocate(f(-n:n))

    do i=-n,n
        f(i) = i**5
    end do

    do i=-n,n
        do j=-n,n
            if (f(i)-f(j) == x) then
                print*, i,j
                stop
            end if
        end do
    end do
end program name


program abc166e
  use iso_fortran_env
  implicit none
  integer(int64) :: n, i, t
  integer(int64),allocatable :: a(:), x(:), y(:)
  read *, n
  allocate(a(n),x(n),y(n))
  read *, a
  x = 0
  y = 0
  do i = 1,n
     t = min(i+a(i),n)
     x(t) = x(t) + 1
     t = max(i-a(i),0)
     y(t) = y(t) + 1
  end do
  print *, sum(x*y)
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(8) n,a(3),i,x(2),x2(2),ans(100000),flag,j
    CHARACTER s1*2,s2*2
    read(*,*) n,(a(i),i=1,3)
    flag=1
!0が二つ含まれる/A+B+C=2の場合
    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
                do j = i+1, n
                    read(*,*) s1
                end do
                exit
            else
                call nomal(a,ans,x,i)
            end if
        end do
    else 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
            if ( i==1 ) then
               call read_s(s1,x)
            end if
            if ( a(x(1))+a(x(2))==0 ) then
                write(*,*) "No"
                flag=0
                do j = i+1, n
                    read(*,*) s1
                end do
                exit
            else
                if ( i<n ) then
                    call read_s(s2,x2)
                end if
                call exceptional(a,ans,x,x2,i)
                x=x2
            end if
        end do
!A+B+C=2の場合
    else if ( a(1)+a(2)+a(3)==2 ) then
        call read_s(s1,x)
        do i = 1, n
            if ( i<n ) then
                call read_s(s2,x2)
            end if
            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(8),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(8) 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(ae, anse, xe, x2e,ie)
        implicit none
        INTEGER(8) ae(3),xe(2),x2e(2),anse(100000),ie
        if ( ae(xe(1))>ae(xe(2)) ) then
            ae(xe(1))=ae(xe(1))-1
            ae(xe(2))=ae(xe(2))+1
            anse(ie)=xe(2)
        else if ( a(x(1))<a(x(2)) ) then
            ae(xe(1))=ae(xe(1))+1
            ae(xe(2))=ae(xe(2))-1
            anse(ie)=xe(1)
        else if ( xe(1)==x2e(1) .or. xe(1)==x2e(2) ) then
            ae(xe(1))=ae(xe(1))+1
            ae(xe(2))=ae(xe(2))-1
            anse(ie)=xe(1)
        else
            ae(xe(1))=ae(xe(1))-1
            ae(xe(2))=ae(xe(2))+1
            anse(ie)=xe(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 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 (r(p(1)) == 0) then
      r(p(1)) = r(p(1))+1
      r(p(2)) = r(p(2))-1
      write(*,'(a)') c(p(1))
    else if (r(p(2)) == 0) then
      r(p(1)) = r(p(1))-1
      r(p(2)) = r(p(2))+1
      write(*,'(a)') c(p(2))
    else 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


program main
real :: x,mon = 100
integer :: y = 0
logical :: ans = .false.

read(*,*)x
do while(mon < x)
y = y + 1
mon = mon * 1.01
end do

write(*,*)y

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 ABC165B
    implicit none
    integer(8)::ans=0,X,y
    read*,X
    y=100
    do
        if(y>=X)exit
        y=y+y/100
        ans=ans+1
    end do

    print'(i0)',ans
end program ABC165B


program sample
implicit none
integer(16) X,year,Y1,Y2
read(*,*)X
Y1=100
Y2=100
year=0
do while (X>Y1)
Y1=Y1+Y2*0.01
Y2=Y1
year=year+1
end do
write(*,*)year
stop
end program sample


program sum
    implicit none
    integer i,j,n,m,q,a(10),d(50,4),c,score,score1,flag
    read(*,*) n,m,q
    c=1
    score=0
    score1=0
    flag=0
    do i = 1, q
        read(*,*) d(i,1),d(i,2),d(i,3),d(i,4)
    end do
    do i = 1, n
        c=c*(11-i)
    end do
    do i = 2, n
        c=c/i
    end do
    do i = 1, n
        a(i)=i
    end do
    do while (a(n)<=10)
        ! do i = 1, n
        !     write(*, fmt='(I4)', advance='no') a(i)
        ! end do
        ! Write(*,*)
        do j = 1, q
            if ( a(d(j,2))-a(d(j,1))==d(j,3) ) then
                score=score+d(j,4)
            end if
        end do
        if ( score>score1 ) then
            score1=score
        end if
        score=0
        a(n-flag)=a(n-flag)+1
        if ( flag==n-1 ) then
            flag=0
        else
            flag=flag+1
        end if
    end do
    write(*,*) score1
end program sum

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 piyo
      IMPLICIT NONE
      integer(16) :: A,B,N
      integer(16) :: maxB,i,ans,buffer,x
      real(16) :: reA,reB,reX

      read*,A,B,N

      maxB = (N/B) * B
 !     maxB = maxB - B !念のため
      if( maxB<0 ) maxB = 0

      ans = 0
   !   do i = maxB,0,-

   !     buffer = floor( real(A*i)/real(B) ) - A*floor(real(i)/real(B))
   !     if( buffer>ans )then
   !       ans = buffer
   !     end if
   !   end do

      reA = real(A)
      reB = real(B)

      reX = real(maxB)
      buffer = int( floor( reA*reX/reB) - A*floor( reX/reB ) )
      ans = max( ans,buffer )
      reX = real(N)
      buffer = int( floor( reA*reX/reB) - A*floor( reX/reB ) )
      ans = max( ans,buffer )
      print*,ans

      END PROGRAM

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 em
implicit none
integer::N,M
integer::i
read*,N,M
do i=1,M
select case(mod(i,2))
case(0)
  print"(i0,A,i0)",M/2+1-i/2," ",M/2+1+i/2
case(1)
  print"(i0,A,i0)",(3*M)/2+1-i/2," ",(3*M)/2+2+i/2
end select
end do
contains
end program em

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 rotation_matching
  implicit none
  integer :: n, m, i = 0, j, cnt = 0, maxm
  read(*,*) n, m
  maxm = (n-1)/2
  do
    write(*,'(i0,x,i0)') i+1, maxm+1-i
    cnt = cnt+1
    if (cnt == m) stop
    write(*,'(i0,x,i0)') maxm+2+i, n-i
    cnt = cnt+1
    if (cnt == m) stop
    i = i+1
  end do
end program rotation_matching


program sample
implicit none
integer S,W
read(*,*)S,W
If (S>W) then
write(*,*) "safe"
else
write(*,*) "unsafe"
end if
stop
end program sample

Score : 100 points

Problem Statement

There are S sheep and W wolves.

If the number of wolves is greater than or equal to that of sheep, the wolves will attack the sheep.

If the wolves will attack the sheep, print unsafe; otherwise, print safe.

Constraints

1 \leq S \leq 100

1 \leq W \leq 100

Input

Input is given from Standard Input in the following format:

S W

Output

If the wolves will attack the sheep, print unsafe; otherwise, print safe.

Sample Input 1

4 5

Sample Output 1

unsafe

There are four sheep and five wolves. The number of wolves is not less than that of sheep, so they will attack them.

Sample Input 2

100 2

Sample Output 2

safe

Many a sheep drive away two wolves.

Sample Input 3

10 10

Sample Output 3

unsafe


program atcoder
   implicit none
   integer S, W
   read *, S, W

    if (S >W) then
     print *,'safe'
    else
     print *,'unsafe'
    end if
end program atcoder


program sheep_and_wolves
  implicit none
  integer :: s, w
  read(*,*) s, w
  if (s > w) then
    write(*,'(a)') "safe"
  else
    write(*,'(a)') "unsafe"
  end if
end program sheep_and_wolves


program main
    implicit none
    integer n
    character(len=10),allocatable, dimension(:) :: s
    integer i,j,k,l,sum,minus,up

    type mytype
        integer :: number
        character(len=10),dimension(100000) :: word
    end type mytype

    type(mytype) :: alpha(26,10)

    read(*,*) n
    allocate(s(n))

    do i = 1,26
        do j = 1,10
            alpha(i,j)%number = 0
        end do
    end do

    do i = 1,n
        read(*,*) s(i)
        if (s(i)(1:1) == "a") then
            k = 1
        else if (s(i)(1:1) == "b") then
            k = 2
        else if (s(i)(1:1) == "c") then
            k = 3
        else if (s(i)(1:1) == "d") then
            k = 4
        else if (s(i)(1:1) == "e") then
            k = 5
        else if (s(i)(1:1) == "f") then
            k = 6
        else if (s(i)(1:1) == "g") then
            k = 7
        else if (s(i)(1:1) == "h") then
            k = 8
        else if (s(i)(1:1) == "i") then
            k = 9
        else if (s(i)(1:1) == "j") then
            k = 10
        else if (s(i)(1:1) == "k") then
            k = 11
        else if (s(i)(1:1) == "l") then
            k = 12
        else if (s(i)(1:1) == "m") then
            k = 13
        else if (s(i)(1:1) == "n") then
            k = 14
        else if (s(i)(1:1) == "o") then
            k = 15
        else if (s(i)(1:1) == "p") then
            k = 16
        else if (s(i)(1:1) == "q") then
            k = 17
        else if (s(i)(1:1) == "r") then
            k = 18
        else if (s(i)(1:1) == "s") then
            k = 19
        else if (s(i)(1:1) == "t") then
            k = 20
        else if (s(i)(1:1) == "u") then
            k = 21
        else if (s(i)(1:1) == "v") then
            k = 22
        else if (s(i)(1:1) == "w") then
            k = 23
        else if (s(i)(1:1) == "x") then
            k = 24
        else if (s(i)(1:1) == "y") then
            k = 25
        else if (s(i)(1:1) == "z") then
            k = 26
        end if
        l = len_trim(s(i))
        alpha(k,l)%number = alpha(k,l)%number + 1
        alpha(k,l)%word(alpha(k,l)%number) = s(i)
    end do

    sum = n

    do i = 1,26
        do j = 1,10

            minus = 0
            up = alpha(i,j)%number
            if (up <= 1) then
                go to 20
            end if
            do k = 1,up
                if (alpha(i,j)%word(k) == "0") then
                    go to 10
                end if
                do l = k + 1, up
                    if(alpha(i,j)%word(k) == alpha(i,j)%word(l)) then
                        minus = minus + 1
                        alpha(i,j)%word(l) = "0"
                    end if
                end do
                10 continue
            end do
            20 continue

            sum = sum - minus

        end do
    end do

    write(*,*) sum

end program

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 main
    implicit none
    integer n
    character(len=10),allocatable, dimension(:) :: s
    integer i,j,k,l,sum,minus,up

    type mytype
        integer :: number
        character(len=10),dimension(10000) :: word
    end type mytype

    type(mytype) :: alpha(26,10)

    read(*,*) n
    allocate(s(n))

    do i = 1,26
        do j = 1,10
            alpha(i,j)%number = 0
        end do
    end do

    do i = 1,n
        read(*,*) s(i)
        if (s(i)(1:1) == "a") then
            k = 1
        else if (s(i)(1:1) == "b") then
            k = 2
        else if (s(i)(1:1) == "c") then
            k = 3
        else if (s(i)(1:1) == "d") then
            k = 4
        else if (s(i)(1:1) == "e") then
            k = 5
        else if (s(i)(1:1) == "f") then
            k = 6
        else if (s(i)(1:1) == "g") then
            k = 7
        else if (s(i)(1:1) == "h") then
            k = 8
        else if (s(i)(1:1) == "i") then
            k = 9
        else if (s(i)(1:1) == "j") then
            k = 10
        else if (s(i)(1:1) == "k") then
            k = 11
        else if (s(i)(1:1) == "l") then
            k = 12
        else if (s(i)(1:1) == "m") then
            k = 13
        else if (s(i)(1:1) == "n") then
            k = 14
        else if (s(i)(1:1) == "o") then
            k = 15
        else if (s(i)(1:1) == "p") then
            k = 16
        else if (s(i)(1:1) == "q") then
            k = 17
        else if (s(i)(1:1) == "r") then
            k = 18
        else if (s(i)(1:1) == "s") then
            k = 19
        else if (s(i)(1:1) == "t") then
            k = 20
        else if (s(i)(1:1) == "u") then
            k = 21
        else if (s(i)(1:1) == "v") then
            k = 22
        else if (s(i)(1:1) == "w") then
            k = 23
        else if (s(i)(1:1) == "x") then
            k = 24
        else if (s(i)(1:1) == "y") then
            k = 25
        else if (s(i)(1:1) == "z") then
            k = 26
        end if
        l = len_trim(s(i))
        alpha(k,l)%number = alpha(k,l)%number + 1
        alpha(k,l)%word(alpha(k,l)%number) = s(i)
    end do

    sum = n

    do i = 1,26
        do j = 1,10

            minus = 0
            up = alpha(i,j)%number
            if (up <= 1) then
                go to 20
            end if
            do k = 1,up
                if (alpha(i,j)%word(k) == "0") then
                    go to 10
                end if
                do l = k + 1, up
                    if(alpha(i,j)%word(k) == alpha(i,j)%word(l)) then
                        minus = minus + 1
                        alpha(i,j)%word(l) = "0"
                    end if
                end do
                10 continue
            end do
            20 continue

            sum = sum - minus

        end do
    end do

    write(*,*) sum

end program


program i_hate_matrix_construction
  implicit none
  integer :: n, i, j, k
  integer(8) :: x
  integer, dimension(500) :: s = 0, t = 0
  integer(8), dimension(500) :: u = 0, v = 0
  integer(8), 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, 62
    do i = 1, n
      if (s(i) == merge(1, 0, btest(u(i), k))) cycle
      do j = 1, n
        a(i, j) = set(a(i, j), k, s(i) == 0)
      end do
    end do
    do j = 1, n
      if (t(j) == merge(1, 0, btest(v(j), k))) cycle
      do i = 1, n
        a(i, j) = set(a(i, j), k, t(j) == 0)
      end do
    end do
    do i = 1, n
      if (s(i) == 0) cycle
      if (.not.btest(u(i), k)) cycle
      do j = 1, n
        !if (t(j) == 1) cycle
        if (btest(a(i, j), k)) cycle
        a(i, j) = ibset(a(i, j), k)
        if (all(btest(a(i, 1:n), k))) then
          a(i, j) = ibclr(a(i, j), k)
        else
          exit
        end if
      end do
    end do
    do j = 1, n
      if (t(j) == 0) cycle
      if (.not.btest(v(j), k)) cycle
      do i = 1, n
        !if (s(i) == 1) cycle
        if (btest(a(i, j), k)) cycle
        a(i, j) = ibset(a(i, j), k)
        if (all(btest(a(1:n, j), k))) then
          a(i, j) = ibclr(a(i, j), k)
        else
          exit
        end if
      end do
    end do
  end do
  do i = 1, n
    x = a(i, 1)
    do j = 2, n
      x = operate(x, a(i, j), s(i) == 0)
    end do
    if (x /= u(i)) then
      write(*,'(i0)') -1
      stop
    end if
  end do
  do j = 1, n
    x = a(1, j)
    do i = 2, n
      x = operate(x, a(i, j), t(j) == 0)
    end do
    if (x /= v(j)) then
      write(*,'(i0)') -1
      stop
    end if
  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(8) function set(a, k, b) result(res)
    integer(8), intent(in) :: a
    integer, intent(in) :: k
    logical, intent(in) :: b
    res = merge(ibset(a, k), ibclr(a, k), b)
  end
  integer(8) function operate(a1, a2, b) result(res)
    integer(8), intent(in) :: a1, a2
    logical, intent(in) :: b
    res = merge(and(a1, a2), or(a1, a2), b)
  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 sample
implicit none
integer(8) N,M,A(10000),count,S,i
read(*,*) N,M
read(*,*) (A(i),i=1,M)
S=0
do i=1,M
S=A(i)+S
end do
count=N-S
if(count>=0) then
   write(*,*) count
else
   write(*,*) -1
end if

stop
end program sample

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,i
   integer ,ALLOCATABLE :: a(:), ans(:)
   read(*,*) n
   allocate (a(n),ans(n))
   read(*,*)a(2:n)
   ans=0
   do i=1,n
      ans(a(i))=ans(a(i))+1
   enddo
   do i=1,n
      write(*,*) ans(i)
   enddo
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, i, j, s
  integer, allocatable :: a(:), b(:)

  read *, n
  allocate(a(2:n), b(n))
  read *, a
  b = 0

  do i = 2, n
     b(a(i)) = b(a(i)) + 1
  end do

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

  stop
end program main


program sum_of_large_numbers
  implicit none
  integer(8) :: n, k
  read(*,*) n, k
  write(*,'(i0)') mod(((n+1)*(n+k+1)*(n-k+2)*3+(n-k+2)*6-(n+1)*(n+2)*(2*n+3)+(k-1)*k*(2*k-1))/6,1000000007)
end program sum_of_large_numbers


program main
      implicit none
      integer(8) :: n, k, i, j
      integer(16) :: sta, fin
      integer(16) :: cnt

      read *, n, k

      cnt = 0
      do j = k, n+1
        sta = j*(j-1)/2
        fin = 0
        do i = 1, j
          fin = fin + (n+1-i)
        end do
        cnt = cnt + fin-sta+1
      end do

      print *, mod(cnt,1000000007)

end program main


program main
    implicit none
    integer n,k,j,p,q,x
    read(*,*) n,k
    p=0
    q=0
    x=0
    do j=k,n+1
        p=j*(j-1)/2
        q=n*j-p
        x=x+q-p+1
    end do
    write(*,*) x
end program main


program test

integer(8) :: n,a(3000),b(3000),i,j,k,dp(0:3000,0:3000)=0,left,right,total,max_dp

read(*,*) n
read(*,*) (a(i),i=1,n)
do i=1,n
  b(i) = i
enddo

call heapsort_down(n,a(1:n),b(1:n))

! aが活発度、bが幼児の初期位置(x)
do total=1,n
  do left=0,total-1
    right=total-1-left
    !左詰め
    dp(left+1,right) = max(dp(left+1,right),dp(left,right)+a(total)*abs(b(total)-(left+1)))
    !右詰め
    dp(left,right+1) = max(dp(left,right+1),dp(left,right)+a(total)*abs(b(total)-(n-right)))
  enddo
enddo

max_dp = 0
do left=0,n
  max_dp = max(max_dp,dp(left,n-left))
enddo

write(*,*) max_dp

contains
subroutine heapsort_down(n,y1,y2)

  implicit none
  integer(8),intent(in) :: n
  integer(8),intent(inout) :: y1(1:n)
  integer(8),intent(inout) :: y2(1:n)
  integer(8) :: i,k,j,l
  integer(8) :: t1,t2

  l = n/2+1
  k = n

  do while (k /= 1)
     if(l > 1) then
        l = l-1
        t1 = y1(l)
        t2 = y2(l)

     else
        t1 = y1(k)
        t2 = y2(k)
        y1(k) = y1(1)
        y2(k) = y2(1)
        k = k-1
        if(k == 1) then
           y1(1) = t1
           y2(1) = t2
           exit
        endif
     endif

     i = l
     j = l+l

     do while(j <= k)
        if(j < k) then
           if(y1(j) > y1(j+1)) j = j+1
        endif

        if (t1 > y1(j)) then
           y1(i) = y1(j)
           y2(i) = y2(j)
           i = j
           j = j+j
        else
           j = k+1
        endif

     enddo

     y1(i) = t1
     y2(i) = t2

  enddo
  return

end subroutine heapsort_down

end program

Score : 500 points

Problem Statement

There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i.

You can rearrange these children just one time in any order you like.

When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points.

Find the maximum total happiness points the children can earn.

Constraints

2 \leq N \leq 2000

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum total happiness points the children can earn.

Sample Input 1

4
1 3 4 2

Sample Output 1

20

If we move the 1-st child from the left to the 3-rd position from the left, the 2-nd child to the 4-th position, the 3-rd child to the 1-st position, and the 4-th child to the 2-nd position, the children earns 1 \times |1-3|+3 \times |2-4|+4 \times |3-1|+2 \times |4-2|=20 happiness points in total.

Sample Input 2

6
5 5 6 1 1 1

Sample Output 2

58

Sample Input 3

6
8 6 9 1 2 1

Sample Output 3

85


program FizzBuzz
      implicit none

      integer(8) N,S,i
      read(*,*) N
      S=0
      do i =1, N
      if (mod(i,5)==0.or.mod(i,3)==0) then
            go to 1
      else
            S=S+i
      1 end if
      end do

      write(*,'(i0)') S
      stop
      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 main
  implicit none
  integer(8) :: m = 10 ** 9 + 7
  integer(8) :: n, k, i, g, res
  integer(8), allocatable :: s(:), t(:)
  read *, n, k
  allocate (s(k), t(k))
  do i = 1, k
    t(i) = get_t(i)
  end do
  s(:) = t(:)
  do g = k, 1, -1
    do i = 2, k / g
      s(g) = s(g) - s(i * g)
      s(g) = modulo(s(g), m)
    end do
  end do
  res = 0
  do g = 1, k
    res = res + g * s(g)
    res = modulo(res, m)
  end do
  print "(i0)", res

  contains

  integer(8) function get_t(f)
    integer(8), intent(in) :: f
    integer(8) :: ng
    ng = k / f
    get_t = pow(ng, n)
  end function get_t

  function pow(n, p) result(res)
    integer(8), intent(in) :: n, p
    integer(8) :: res, i, imax, buf(0:63)
    imax = bit_size(p) - leadz(p) - 1
    buf(0) = modulo(n, m)
    do i = 1, imax
      buf(i) = modulo(buf(i - 1) ** 2, m)
    end do
    res = 1
    do i = 0, imax
      if (btest(p, i)) then
        res = res * buf(i)
        res = modulo(res, m)
      end if
    end do
  end function pow
end program main

Score : 500 points

Problem Statement

Consider sequences \{A_1,...,A_N\} of length N consisting of integers between 1 and K (inclusive).

There are K^N such sequences. Find the sum of \gcd(A_1, ..., A_N) over all of them.

Since this sum can be enormous, print the value modulo (10^9+7).

Here \gcd(A_1, ..., A_N) denotes the greatest common divisor of A_1, ..., A_N.

Constraints

2 \leq N \leq 10^5

1 \leq K \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the sum of \gcd(A_1, ..., A_N) over all K^N sequences, modulo (10^9+7).

Sample Input 1

3 2

Sample Output 1

9

\gcd(1,1,1)+\gcd(1,1,2)+\gcd(1,2,1)+\gcd(1,2,2)
+\gcd(2,1,1)+\gcd(2,1,2)+\gcd(2,2,1)+\gcd(2,2,2)
=1+1+1+1+1+1+1+2=9

Thus, the answer is 9.

Sample Input 2

3 200

Sample Output 2

10813692

Sample Input 3

100000 100000

Sample Output 3

742202979

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


program name
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: n,i,ans, tn
    integer(int64), allocatable:: a(:), ind(:)
    logical,allocatable:: take(:)

    read*, n
    allocate(a(n), ind(n),take(0:n+1))

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

    call double_sort(a, ind, 1_8, n)
    take(:) = .false.

    ans=0
    tn=0
    do i=1,n
        if ((.not. take(ind(i)-1)) .and. (.not. take(ind(i)+1))) then
            ans=ans+a(i)
            take(ind(i)) = .true.
            tn=tn+1
        end if
        if (tn >= n/2) exit
    end do

    print*, ans

contains
    recursive subroutine double_sort(ar1, ar2, fst, lst)
        integer(8),intent(inout):: ar1(:),ar2(:)
        integer(8),intent(in):: fst,lst
        integer(8):: 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(8),intent(inout):: ar1(:),ar2(:)
        integer(8),intent(in):: fst,mdl,lst
        integer(8),allocatable:: t1(:),t2(:)
        integer(8):: 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(8),intent(inout):: x,y
        integer(8):: tmp
        tmp = x
        x = y
        y = tmp
    end subroutine

end program name

Score : 600 points

Problem Statement

Given is an integer sequence A_1, ..., A_N of length N.

We will choose exactly \left\lfloor \frac{N}{2} \right\rfloor elements from this sequence so that no two adjacent elements are chosen.

Find the maximum possible sum of the chosen elements.

Here \lfloor x \rfloor denotes the greatest integer not greater than x.

Constraints

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

|A_i|\leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 ... A_N

Output

Print the maximum possible sum of the chosen elements.

Sample Input 1

6
1 2 3 4 5 6

Sample Output 1

12

Choosing 2, 4, and 6 makes the sum 12, which is the maximum possible value.

Sample Input 2

5
-1000 -100 -10 0 10

Sample Output 2

0

Choosing -10 and 10 makes the sum 0, which is the maximum possible value.

Sample Input 3

10
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 3

5000000000

Watch out for overflow.

Sample Input 4

27
18 -28 18 28 -45 90 -45 23 -53 60 28 -74 -71 35 -26 -62 49 -77 57 24 -70 -93 69 -99 59 57 -49

Sample Output 4

295


      PROGRAM replacingInteger
      IMPLICIT NONE
      integer(16) :: n,k
      integer(16) :: numK

      read*,n,k

      numK = n/K

      n = n - numK*k

      print*,min( n,abs(n-k) )

      END PROGRAM

Score : 300 points

Problem Statement

Given any integer x, Aoki can do the operation below.

Operation: Replace x with the absolute difference of x and K.

You are given the initial value of an integer N. Find the minimum possible value taken by N after Aoki does the operation zero or more times.

Constraints

0 ≤ N ≤ 10^{18}

1 ≤ K ≤ 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the minimum possible value taken by N after Aoki does the operation zero or more times.

Sample Input 1

7 4

Sample Output 1

1

Initially, N=7.

After one operation, N becomes |7-4| = 3.

After two operations, N becomes |3-4| = 1, which is the minimum value taken by N.

Sample Input 2

2 6

Sample Output 2

2

N=2 after zero operations is the minimum.

Sample Input 3

1000000000000000000 1

Sample Output 3

0


program replacing_integer
  implicit none
  integer(8) :: n, k
  read(*,*) n, k
  n = mod(n,k)
  write(*,'(i0)') min(n,k-n)
end program replacing_integer


program test
implicit none
integer(8) :: n,k,c,i,j,dp_left(0:200000)=10**6,dp_right(0:200000)=10**6,workday=0
character(len=200000) :: s

read(*,*) n,k,c
read(*,*) s(1:n)

!dp_leftは,左から見たときにi日目の最速の労働日
workday = 0
dp_left(0) = 0
dp_right(0) = 0

do i=1,n
  if(s(i:i)=='x')then
    continue
  else
    if(workday == 0)then
      workday = workday + 1
      dp_left(workday) = i
    else
      if(i > dp_left(workday) + c) then
        workday = workday + 1
        dp_left(workday) = i
      endif
    endif
  endif
enddo

!dp_rightは,右から見たときにi日目の最遅の労働日
workday = 0
do i=n,1,-1
  if(s(i:i)=='x')then
    continue
  else
    if(workday == 0)then
      workday = workday + 1
      dp_right(workday) = i
    else
      if(i < dp_right(workday) - c) then
        workday = workday + 1
        dp_right(workday) = i
      endif
    endif
  endif
enddo

do i=1,k
  if(dp_left(i)==dp_right(k+1-i)) then
    write(*,*) dp_left(i)
  endif
enddo

end

Score : 500 points

Problem Statement

Takahashi has decided to work on K days of his choice from the N days starting with tomorrow.

You are given an integer C and a string S. Takahashi will choose his workdays as follows:

After working for a day, he will refrain from working on the subsequent C days.

If the i-th character of S is x, he will not work on Day i, where Day 1 is tomorrow, Day 2 is the day after tomorrow, and so on.

Find all days on which Takahashi is bound to work.

Constraints

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

1 \leq K \leq N

0 \leq C \leq N

The length of S is N.

Each character of S is o or x.

Takahashi can choose his workdays so that the conditions in Problem Statement are satisfied.

Input

Input is given from Standard Input in the following format:

N K C
S

Output

Print all days on which Takahashi is bound to work in ascending order, one per line.

Sample Input 1

11 3 2
ooxxxoxxxoo

Sample Output 1

6

Takahashi is going to work on 3 days out of the 11 days. After working for a day, he will refrain from working on the subsequent 2 days.

There are four possible choices for his workdays: Day 1,6,10, Day 1,6,11, Day 2,6,10, and Day 2,6,11.

Thus, he is bound to work on Day 6.

Sample Input 2

5 2 3
ooxoo

Sample Output 2

1
5

There is only one possible choice for his workdays: Day 1,5.

Sample Input 3

5 1 0
ooooo

Sample Output 3

There may be no days on which he is bound to work.

Sample Input 4

16 4 3
ooxxoxoxxxoxoxxo

Sample Output 4

11
16


program division_or_substraction
  implicit none
  integer(8) :: n, r, k, x = 2_8
  read(*,*) n
  if (n == 2_8) then
    write(*,'(i0)') 1
    stop
  end if
  r = sqrt(real(n,8)+0.5d0)
  do k = 2_8, r
    if (ok(n,k)) then
      x = x+1_8
    else
      if (mod(n-1_8,k) == 0_8) x = x+1_8
      if (k*k /= n-1_8) x = x+1_8
    end if
  end do
  write(*,'(i0)') x
contains
  logical function ok(n,k)
    integer(8), intent(in) :: n, k
    integer(8) :: m
    m = n
    do while (m >= k)
      if (mod(m,k) == 0) then
        m = m/k
      else
        m = mod(m,k)
      end if
    end do
    ok = m == 1
  end
end program division_or_substraction

Score : 600 points

Problem Statement

Given is a positive integer N.

We will choose an integer K between 2 and N (inclusive), then we will repeat the operation below until N becomes less than K.

Operation: if K divides N, replace N with N/K; otherwise, replace N with N-K.

In how many choices of K will N become 1 in the end?

Constraints

2 \leq N \leq 10^{12}

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the number of choices of K in which N becomes 1 in the end.

Sample Input 1

6

Sample Output 1

3

There are three choices of K in which N becomes 1 in the end: 2, 5, and 6.

In each of these choices, N will change as follows:

When K=2: 6 \to 3 \to 1

When K=5: 6 \to 1

When K=6: 6 \to 1

Sample Input 2

3141

Sample Output 2

13

Sample Input 3

314159265358

Sample Output 3

9


program division_or_substraction
  implicit none
  integer(8) :: n, r, i, x = 0_8
  read(*,*) n
  r = sqrt(real(n,8)+0.5d0)
  do i = 2_8, r
    if (ok(n,i)) x = x+1_8
  end do
  write(*,'(i0)') 2_8*x+1_8
contains
  logical function ok(n,k)
    integer(8), intent(in) :: n, k
    integer(8) :: m
    m = n
    do while (m >= k)
      if (mod(m,k) == 0) then
        m = m/k
      else
        m = mod(m,k)
      end if
    end do
    ok = m == 1
  end
end program division_or_substraction


program coffee

 character,dimension(1:6)::s

 do i=1,6
  read*,s(i)
 end do

 if(s(3)==s(4) .and. s(5)==s(6)) then
  print*,'Yes'
 else
  print*,'No'
 end if

end program coffee

Score : 100 points

Problem Statement

A string of length 6 consisting of lowercase English letters is said to be coffee-like if and only if its 3-rd and 4-th characters are equal and its 5-th and 6-th characters are also equal.

Given a string S, determine whether it is coffee-like.

Constraints

S is a string of length 6 consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If S is coffee-like, print Yes; otherwise, print No.

Sample Input 1

sippuu

Sample Output 1

Yes

In sippuu, the 3-rd and 4-th characters are equal, and the 5-th and 6-th characters are also equal.

Sample Input 2

iphone

Sample Output 2

No

Sample Input 3

coffee

Sample Output 3

Yes


program main
      implicit none
      integer :: x, uresi
      read(*,*) x

      uresi = int(real(x)/real(500)) * 1000 + int(real(mod(x,500))/real(5)) * 5

      print *, uresi
end program main

Score : 200 points

Problem Statement

Takahashi loves gold coins. He gains 1000 happiness points for each 500-yen coin he has and gains 5 happiness points for each 5-yen coin he has. (Yen is the currency of Japan.)

Takahashi has X yen. If he exchanges his money so that he will gain the most happiness points, how many happiness points will he earn?

(We assume that there are six kinds of coins available: 500-yen, 100-yen, 50-yen, 10-yen, 5-yen, and 1-yen coins.)

Constraints

0 \leq X \leq 10^9

X is an integer.

Input

Input is given from Standard Input in the following format:

X

Output

Print the maximum number of happiness points that can be earned.

Sample Input 1

1024

Sample Output 1

2020

By exchanging his money so that he gets two 500-yen coins and four 5-yen coins, he gains 2020 happiness points, which is the maximum number of happiness points that can be earned.

Sample Input 2

0

Sample Output 2

0

He is penniless - or yenless.

Sample Input 3

1000000000

Sample Output 3

2000000000

He is a billionaire - in yen.


program abc
	implicit none
	integer(8) :: n
    integer :: a,b,c,d

	read(*,*) n

	a = n / 500
    b = n - a * 500
    c = b / 5
    d = 1000 * a + 5 * c

	write(*,*) d

end program abc


   program abc160c
      implicit none
      integer(8)::K,N,i,j,m,p,q,Cmin=100000000
      integer(8),allocatable,dimension(:)::A,R,L

         read *,K,N
         allocate(A(N),R(N),L(N))
         read *,A

         R=0
         L=0
         do i=1,N

            do j=i,N-1
               R(i)=R(i)+(A(j+1)-A(j))
            end do
            do m=1,i-2
               R(i)=R(i)+(A(m+1)-A(m))
            end do
            if (i/=1) then
               R(i)=R(i)+(K-A(N))+A(1)
            end if

            do p=i,2,-1
               L(i)=L(i)+(A(p)-A(p-1))
            end do
            do q=N-1,i+1,-1
               L(i)=L(i)+(A(q+1)-A(q))
            end do
            if (i/=N) then
               L(i)=L(i)+(K-A(N))+A(1)
            end if

         end do

         do i=1,N
            if (Cmin>R(i)) then
               Cmin=R(i)
            end if
         end do

         do j=1,N
            if (Cmin>L(i)) then
               Cmin=L(i)
            end if
         end do

         print *,Cmin
         stop

   end program abc160c

Score : 300 points

Problem Statement

There is a circular pond with a perimeter of K meters, and N houses around them.

The i-th house is built at a distance of A_i meters from the northmost point of the pond, measured clockwise around the pond.

When traveling between these houses, you can only go around the pond.

Find the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.

Constraints

2 \leq K \leq 10^6

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

0 \leq A_1 < ... < A_N < K

All values in input are integers.

Input

Input is given from Standard Input in the following format:

K N
A_1 A_2 ... A_N

Output

Print the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.

Sample Input 1

20 3
5 10 15

Sample Output 1

10

If you start at the 1-st house and go to the 2-nd and 3-rd houses in this order, the total distance traveled will be 10.

Sample Input 2

20 3
0 5 15

Sample Output 2

10

If you start at the 2-nd house and go to the 1-st and 3-rd houses in this order, the total distance traveled will be 10.


program main
  implicit none
  integer :: N, X, Y
  integer :: i, j, distance
  integer, allocatable :: npath(:)

  read(*,*) N, X, Y
  allocate(npath(N-1))
  npath(:) = 0

  do j = 1, N-1
    do i = j+1, N
      if(i<=X)then
        distance = i - j
      elseif(j>=Y)then
        distance = i - j
      elseif(j <=X .and. Y<=i)then
        distance = i - j -(Y-X-1)
      elseif(X <=j .and. i<=Y)then
        distance = min(i-j, (j-X)+(Y-i)+1)
      elseif(j<= X .and. X < i .and. i <= Y)then
        distance = (X-j) + min(i-X, Y-i+1)
      else
        distance = (i-Y) + min(Y-j, j-X+1)
      endif
      npath(distance) = npath(distance) + 1
    enddo
  enddo

  do i = 1, N-1
    write(*,*) npath(i)
  enddo

end program main

Score : 400 points

Problem Statement

We have an undirected graph G with N vertices numbered 1 to N and N edges as follows:

For each i=1,2,...,N-1, there is an edge between Vertex i and Vertex i+1.

There is an edge between Vertex X and Vertex Y.

For each k=1,2,...,N-1, solve the problem below:

Find the number of pairs of integers (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j in G is k.

Constraints

3 \leq N \leq 2 \times 10^3

1 \leq X,Y \leq N

X+1 < Y

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X Y

Output

For each k=1, 2, ..., N-1 in this order, print a line containing the answer to the problem.

Sample Input 1

5 2 4

Sample Output 1

5
4
1
0

The graph in this input is as follows:

There are five pairs (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j is 1: (1,2)\,,(2,3)\,,(2,4)\,,(3,4)\,,(4,5).

There are four pairs (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j is 2: (1,3)\,,(1,4)\,,(2,5)\,,(3,5).

There is one pair (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j is 3: (1,5).

There are no pairs (i,j) (1 \leq i < j \leq N) such that the shortest distance between Vertex i and Vertex j is 4.

Sample Input 2

3 1 3

Sample Output 2

3
0

The graph in this input is as follows:

Sample Input 3

7 3 7

Sample Output 3

7
8
4
2
0
0

Sample Input 4

10 4 8

Sample Output 4

10
12
10
8
4
1
0
0
0


program line_plus_plus
  implicit none
  integer :: n, x, y, i, j, k
  integer :: d(2001,2001) = 1e8, c(2000) = 0
  read(*,*) n, x, y
  do i = 1, n
    d(i,i) = 0
    d(i,i+1) = 1
    d(i+1,i) = 1
  end do
  d(x,y) = 1
  d(y,x) = 1
  do k = 1, n
    do i = 1, n
      do j = 1, n
        d(i,j) = min(d(i,j),d(i,k)+d(k,j))
        if (k == n) c(d(i,j)) = c(d(i,j))+1
      end do
    end do
  end do
  do k = 1, n-1
    write(*,'(i0)') c(k)/2
  end do
end program line_plus_plus


program main
	implicit none
    integer::n,m
    read(*,*)n,m
    write(*,*) n*(n-1)/2 +m*(m-1)/2
    stop
end program main

Score : 100 points

Problem Statement

We have N+M balls, each of which has an integer written on it.

It is known that:

The numbers written on N of the balls are even.

The numbers written on M of the balls are odd.

Find the number of ways to choose two of the N+M balls (disregarding order) so that the sum of the numbers written on them is even.

It can be shown that this count does not depend on the actual values written on the balls.

Constraints

0 \leq N,M \leq 100

2 \leq N+M

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

Print the answer.

Sample Input 1

2 1

Sample Output 1

1

For example, let us assume that the numbers written on the three balls are 1,2,4.

If we choose the two balls with 1 and 2, the sum is odd;

If we choose the two balls with 1 and 4, the sum is odd;

If we choose the two balls with 2 and 4, the sum is even.

Thus, the answer is 1.

Sample Input 2

4 3

Sample Output 2

9

Sample Input 3

1 1

Sample Output 3

0

Sample Input 4

13 3

Sample Output 4

81

Sample Input 5

0 3

Sample Output 5

3


program no1

integer N,M,sum

read *,N,M
sum = (N*(N-1)+M*(M-1))/2

print *,sum

end program no1


program beg
implicit none
integer N,M,sum
read * ,N,M
sum = N+M
print *,sum
end program beg


   Program ABC159B
   implicit none
   integer::n,m,l,i,j,k
   character(100)::s,t,u

      read(*,*) s
      n=len_trim(s)
      t=s(1:(n-1)/2)
      u=s((n+3)/2:n)
      m=len_trim(t)
      l=len_trim(u)

      A:do i=1,(n+1)/2
         if (s(i:i)/=s(n-i+1:n-i+1)) then
            print *,'No'
            exit A
         else
            B:do j=1,(m+1)/2
               if (t(j:j)/=t(m-j+1:m-j+1)) then
                  print *,'No'
                  exit A
               else
                  C:do k=1,(l+1)/2
                     if (u(k:k)/=u(l-k+1:l-k+1)) then
                        print *,'No'
                        exit A
                     else
                        print *,'Yes'
                        exit A
                     end if
                  end do C
               end if
            end do B
         end if
      end do A

      stop

   End program ABC159B

Score : 200 points

Problem Statement

A string S of an odd length is said to be a strong palindrome if and only if all of the following conditions are satisfied:

S is a palindrome.

Let N be the length of S. The string formed by the 1-st through ((N-1)/2)-th characters of S is a palindrome.

The string consisting of the (N+3)/2-st through N-th characters of S is a palindrome.

Determine whether S is a strong palindrome.

Constraints

S consists of lowercase English letters.

The length of S is an odd number between 3 and 99 (inclusive).

Input

Input is given from Standard Input in the following format:

S

Output

If S is a strong palindrome, print Yes;
otherwise, print No.

Sample Input 1

akasaka

Sample Output 1

Yes

S is akasaka.

The string formed by the 1-st through the 3-rd characters is aka.

The string formed by the 5-th through the 7-th characters is aka.
All of these are palindromes, so S is a strong palindrome.

Sample Input 2

level

Sample Output 2

No

Sample Input 3

atcoder

Sample Output 3

No


   Program ABC159B
   implicit none
   integer::n,m,l,i,j,k
   character(100)::s,t,u

      read(*,*) s
      n=len_trim(s)
      t=s(1:(n-1)/2)
      u=s((n+3)/2:n)
      m=len_trim(t)
      l=len_trim(u)

      A:do i=1,n
         if (s(i:i)/=s(n-i+1:n-i+1)) then
            print *,'No'
            exit A
         else
            B:do j=1,m
               if (t(j:j)/=t(m-j+1:m-j+1)) then
                  print *,'No'
                  exit A
               else
                  C:do k=1,l
                     if (u(k:k)/=u(l-k+1:l-k+1)) then
                        print *,'No'
                        exit A
                     else
                        print *,'Yes'
                        exit A
                     end if
                  end do C
               end if
            end do B
         end if
      end do A

      stop

   End program ABC159B


program example
	implicit none

    character(100) S,A,B
    integer :: N,i,countA=0,countB=0,countS=0

    read(*,*) S

    S=adjustl(S)

    N=len_trim(S)

	do i=1,N

    	if(S(i:i)==S(N+1-i:N+1-i)) then
        	countS=countS+1
        end if

    end do

    if(countS/=N) then
    	write(*,*) "No"
        goto 1
    end if

    A=S(1:(N-1)/2)

    B=S((N+3)/2:N)

	do i=1,(N-1)/2

    	if(A(i:i)==A(((N-1)/2)+1-i:((N-1)/2)+1-i)) then
        	countA=countA+1
        end if

    end do

    do i=(N+3)/2,N

    	if(B(i:i)==B(N+((N+3)/2)-i:N+((N+3)/2)-i)) then
        	countB=countB+1
        end if

    end do

    if(countA==((N-1)/2) .and. countB==((N-1)/2)) then
    	write(*,*) "Yes"
    else
    	write(*,*) "No"

    end if

1 end program


program MaximumVolume
  implicit none

  real(8) :: l

  read *, l

  print *, (l/3.0d0)**3

  stop
end program MaximumVolume

Score : 300 points

Problem Statement

Given is a positive integer L.
Find the maximum possible volume of a rectangular cuboid whose sum of the dimensions (not necessarily integers) is L.

Constraints

1 ≤ L ≤ 1000

L is an integer.

Input

Input is given from Standard Input in the following format:

L

Output

Print the maximum possible volume of a rectangular cuboid whose sum of the dimensions (not necessarily integers) is L.
Your output is considered correct if its absolute or relative error from our answer is at most 10^{-6}.

Sample Input 1

3

Sample Output 1

1.000000000000

For example, a rectangular cuboid whose dimensions are 0.8, 1, and 1.2 has a volume of 0.96.

On the other hand, if the dimensions are 1, 1, and 1, the volume of the rectangular cuboid is 1, which is greater.

Sample Input 2

999

Sample Output 2

36926037.000000000000


program main

  implicit none

  integer(8) :: l

  read(*,*) l

  print*, ( real( l ) / 3d0 )**3d0

end program main


program BannedK
  implicit none

  integer(kind=8) :: n, i, j, k, s, t
  integer(kind=8), allocatable :: a(:), b(:,:), c(:,:)

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

  call count(a, b)
  s = 0_8
  do i = 1, n
     call combi(b(2,i),2_8,t)
     s = s + t
  end do

  do k = 1, n
     print *, s-b(2,a(k))+1 !!!!
  end do

  deallocate(a)

  stop
contains

  subroutine count(array, dtb)
    implicit none
    integer(8), intent(in) :: array(:)
    integer(8), allocatable, intent(out) :: dtb(:,:)
    integer(8) :: n, i, j, s

    n = size(array)
    allocate(dtb(2_8,n))
    dtb = 0_8

    do i = 1, n
       dtb(1,i) = i
       dtb(2,array(i)) = dtb(2,array(i)) + 1
    end do

    return
  end subroutine count

  subroutine combi(n, m, rslt)
    implicit none
    integer(8), intent(in) :: n, m
    integer(8), intent(out) :: rslt
    integer(8) :: i

    rslt = 1_8
    do i = 1, m
       rslt = rslt*(n-i+1_8)
    end do
    do i = 1, m
       rslt = rslt/(m-i+1_8)
    end do

    return
  end subroutine combi

end program BannedK

Score : 400 points

Problem Statement

We have N balls. The i-th ball has an integer A_i written on it.

For each k=1, 2, ..., N, solve the following problem and print the answer.

Find the number of ways to choose two distinct balls (disregarding order) from the N-1 balls other than the k-th ball so that the integers written on them are equal.

Constraints

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

1 \leq A_i \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

For each k=1,2,...,N, print a line containing the answer.

Sample Input 1

5
1 1 2 1 2

Sample Output 1

2
2
3
2
3

Consider the case k=1 for example. The numbers written on the remaining balls are 1,2,1,2.

From these balls, there are two ways to choose two distinct balls so that the integers written on them are equal.

Thus, the answer for k=1 is 2.

Sample Input 2

4
1 2 3 4

Sample Output 2

0
0
0
0

No two balls have equal numbers written on them.

Sample Input 3

5
3 3 3 3 3

Sample Output 3

6
6
6
6
6

Any two balls have equal numbers written on them.

Sample Input 4

8
1 2 1 4 2 1 4 1

Sample Output 4

5
7
5
7
7
5
7
5


program example
	implicit none

	integer :: N,i,j,k,B,wa=0
    integer,allocatable :: A(:)

    read(*,*) N

    allocate(A(N))

!    do i=1,N
    read(*,*) A
!    end do

!    write(*,*) A

    do i=1,N

        B=A(i)
    	A(i)=N+1
!        write(*,*) A
!        if(i==1) then

!        	do j=2,N
!            	do k=j+1,N

!                	if(A(j)==A(k)) then

!                		wa=wa+1
!                	end if
!            	end do
!            end do

!            write(*,*) wa
!            wa=0

!        else if(i/=1 .and. i/=N)  then

        do j=1,N

            do k=j+1,N

                if(A(j)==A(k)) then

                    wa=wa+1
                end if
            end do
        end do

        write(*,*) wa

!        else if(i==N) then
!        	do j=1,N-1
!            	do k=j+1,N-1

!                    if(A(j)==A(k)) then

!                	    wa=wa+1
!                    end if
!                end do
!            end do

!            write(*,*) wa
!            wa=0
!        end if

    	A(i)=B
        wa=0
    end do

end program example


      PROGRAM DividingChocolate
      IMPLICIT NONE
      integer :: h,w,k
      integer,allocatable :: choco(:,:)
      integer,allocatable :: memo1(:),memo2(:),counter2(:),this(:)
      character(1000) :: s
      integer :: i,j,buffer,piyo

      read*,h,w,k
      allocate( choco(h,w),memo1(h-1),memo2(w-1) )
      choco = 0
      memo1 = 0
      memo2 = 0
      do i = 1,h
        read*,s
        do j = 1,w
          if( s(j:j)=='1' )then
            choco(i,j) = 1
          end if
        end do
      end do

      !tate
      buffer = sum( choco(1,:) )
      do i = 2,h
        if( buffer+sum(choco(i,:))>k )then
          buffer = sum( choco(i,:) )
          memo1(i-1) = 1
        end if
      end do

      !yoko
      allocate( counter2(sum(memo1)),this(sum(memo1)) )
      counter2 = 0
      piyo = 1
      do i = 1,h
        counter2(piyo) = counter2(piyo) + choco(i,1)
        if(memo1(i)==1) piyo = piyo + 1
      end do
      this = 0
      do j = 2,w
        piyo = 1

        do i = 1,h
          this(piyo) = this(piyo) + choco(i,j)

          if(memo1(i)==1)then
            piyo = piyo + 1
          end if
        end do

        if( maxval(counter2+this)>k )then
          counter2 = this
          memo2(j-1) = 1
        end if
      end do

      print*,sum(memo1) + sum(memo2)

      !debugg
    !  print*,trim(s)
    !  do i = 1,h
    !    print*,choco(i,:)
    !  end do
    !
    !  print*,memo1
    !  print*,memo2

      END PROGRAM

Score : 500 points

Problem Statement

We have a chocolate bar partitioned into H horizontal rows and W vertical columns of squares.

The square (i, j) at the i-th row from the top and the j-th column from the left is dark if S_{i,j} is 0, and white if S_{i,j} is 1.

We will cut the bar some number of times to divide it into some number of blocks. In each cut, we cut the whole bar by a line running along some boundaries of squares from end to end of the bar.

How many times do we need to cut the bar so that every block after the cuts has K or less white squares?

Constraints

1 \leq H \leq 10

1 \leq W \leq 1000

1 \leq K \leq H \times W

S_{i,j} is 0 or 1.

Input

Input is given from Standard Input in the following format:

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

Output

Print the number of minimum times the bar needs to be cut so that every block after the cuts has K or less white squares.

Sample Input 1

3 5 4
11100
10001
00111

Sample Output 1

2

For example, cutting between the 1-st and 2-nd rows and between the 3-rd and 4-th columns - as shown in the figure to the left - works.

Note that we cannot cut the bar in the ways shown in the two figures to the right.

Sample Input 2

3 5 8
11100
10001
00111

Sample Output 2

0

No cut is needed.

Sample Input 3

4 10 4
1110010010
1000101110
0011101001
1101000111

Sample Output 3

3


module ch
  implicit none
  type char
     character(100)      :: n
  end type char
  type(char),allocatable :: s(:)
end module ch
program main

  use ch, only : s
  implicit none

  integer              ::  h, w
  integer              ::  i, j, ans
  integer,allocatable  ::  ss(:,:)

  read(*,*) h, w
  allocate( s(h) )
  allocate( ss(h,w) )
  do i = 1, h
    read(*,*) s(i)%n
  end do

  ss = 0
  do i = 1, h
    do j = 1, w
      if( s(i)%n(j:j) == "#" ) then
        ss(i,j) = ss(i,j) + 1
      end if
      if( i == 1 .and. j == 1 ) then
        cycle
      end if

      if( i == 1 .and. j /= 1 ) then
        ss(i,j) = ss(i,j) + ss(i,j-1)
      end if

      if( i /= 1 .and. j == 1 ) then
        ss(i,j) = ss(i,j) + ss(i-1,j)
      end if

      if( i /= 1 .and. j /= 1 ) then
        if( ss(i,j-1) <= ss(i-1,j) ) then
          ss(i,j) = ss(i,j) + ss(i,j-1)
        else
          ss(i,j) = ss(i,j) + ss(i-1,j)
        end if
      end if

    end do
  end do

  if( ss(h-1,w) <= ss(h,w-1) ) then
    ans = ss(h-1,w)
  else
    ans = ss(h,w-1)
  end if

  if( s(h)%n(w:w) == "#" ) then
    ans = ans + 1
  end if
  print*, ans

end program main

Score : 400 points

Problem Statement

Consider a grid with H rows and W columns of squares. Let (r, c) denote the square at the r-th row from the top and the c-th column from the left.
Each square is painted black or white.

The grid is said to be good if and only if the following condition is satisfied:

From (1, 1), we can reach (H, W) by moving one square right or down repeatedly, while always being on a white square.

Note that (1, 1) and (H, W) must be white if the grid is good.

Your task is to make the grid good by repeating the operation below. Find the minimum number of operations needed to complete the task. It can be proved that you can always complete the task in a finite number of operations.

Choose four integers r_0, c_0, r_1, c_1(1 \leq r_0 \leq r_1 \leq H, 1 \leq c_0 \leq c_1 \leq W). For each pair r, c (r_0 \leq r \leq r_1, c_0 \leq c \leq c_1), invert the color of (r, c) - that is, from white to black and vice versa.

Constraints

2 \leq H, W \leq 100

Input

Input is given from Standard Input in the following format:

H W
s_{11} s_{12} \cdots s_{1W}
s_{21} s_{22} \cdots s_{2W}
\vdots
s_{H1} s_{H2} \cdots s_{HW}

Here s_{rc} represents the color of (r, c) - # stands for black, and . stands for white.

Output

Print the minimum number of operations needed.

Sample Input 1

3 3
.##
.#.
##.

Sample Output 1

1

Do the operation with (r_0, c_0, r_1, c_1) = (2, 2, 2, 2) to change just the color of (2, 2), and we are done.

Sample Input 2

2 2
#.
.#

Sample Output 2

2

Sample Input 3

4 4
..##
#...
###.
###.

Sample Output 3

0

No operation may be needed.

Sample Input 4

5 5
.#.#.
#.#.#
.#.#.
#.#.#
.#.#.

Sample Output 4

4