program main
implicit None
	integer(8)::x,y,z,k,i,j
	integer(8),allocatable::a(:),b(:),c(:),d(:),e(:)

	read*,x,y,z,k
	allocate(a(x),b(y),c(z),d(x*y),e(k*z))
	read*,a;read*,b;read*,c

	do i =1,x
		do j =1,y
			d((i-1)*y+j) = a(i)+b(j)
		enddo
	enddo

	call h1(d,x*y)
	call rev(d,x*y)
	e = 0

	do i =1,k
		do j = 1,z
			e((i-1)*z+j) = d(i)+c(j)
		enddo
		if((i+1)>x*y)exit
	enddo

	call h1(e,k*z)
	call rev(e,k*z)

	do i = 1,k
		print"(i0)",e(i)
	enddo
end program main

subroutine rev(a,n)
Implicit None
	integer(8)::a(n),b(n)
	integer(8)::n,i

	do i =1,n
	b(i) = a(n-i+1)
	enddo

	a =b
end subroutine rev

subroutine h1(a,n)
implicit None
	integer(8)::a(n),x
	integer(8)::n,i,ie,i0,i1,i2

	do i = n/2,1,-1
		x = a(i);ie = n + 1; i0 = i; i1 = i0 * 2
		do while(i1 < ie)
			i2 = i1 + 1
			if(i2 < ie .and. a(i1) < a(i2)) i1 = i2
			if(a(i1) > x)then
				a(i0) = a(i1);a(i1) = x
				else
				exit
			endif
			i0 = i1;i1 = i0 *2
		enddo
	enddo
	do ie = n,2,-1
		x = a(ie)
		a(ie) = a(1)
		a(1) = x
		i0 =1;i1 =2
		do while(i1 <ie)
			i2 = i1 +1
			if(i2 < ie .and. a(i1) < a(i2)) i1 = i2
			if(a(i1) > x)then
				a(i0) = a(i1);a(i1) = x
				else
				exit
			endif
			i0 = i1;i1 = i0 *2
		enddo
	enddo
end subroutine h1

Score: 400 points

Problem Statement

The Patisserie AtCoder sells cakes with number-shaped candles.
There are X, Y and Z kinds of cakes with 1-shaped, 2-shaped and 3-shaped candles, respectively.
Each cake has an integer value called deliciousness, as follows:

The deliciousness of the cakes with 1-shaped candles are A_1, A_2, ..., A_X.

The deliciousness of the cakes with 2-shaped candles are B_1, B_2, ..., B_Y.

The deliciousness of the cakes with 3-shaped candles are C_1, C_2, ..., C_Z.

Takahashi decides to buy three cakes, one for each of the three shapes of the candles, to celebrate ABC 123.

There are X \times Y \times Z such ways to choose three cakes.

We will arrange these X \times Y \times Z ways in descending order of the sum of the deliciousness of the cakes.

Print the sums of the deliciousness of the cakes for the first, second, ..., K-th ways in this list.

Constraints

1 \leq X \leq 1 \ 000

1 \leq Y \leq 1 \ 000

1 \leq Z \leq 1 \ 000

1 \leq K \leq \min(3 \ 000, X \times Y \times Z)

1 \leq A_i \leq 10 \ 000 \ 000 \ 000

1 \leq B_i \leq 10 \ 000 \ 000 \ 000

1 \leq C_i \leq 10 \ 000 \ 000 \ 000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y Z K
A_1 \ A_2 \ A_3 \ ... \ A_X
B_1 \ B_2 \ B_3 \ ... \ B_Y
C_1 \ C_2 \ C_3 \ ... \ C_Z

Output

Print K lines. The i-th line should contain the i-th value stated in the problem statement.

Sample Input 1

2 2 2 8
4 6
1 5
3 8

Sample Output 1

19
17
15
14
13
12
10
8

There are 2 \times 2 \times 2 = 8 ways to choose three cakes, as shown below in descending order of the sum of the deliciousness of the cakes:

(A_2, B_2, C_2): 6 + 5 + 8 = 19

(A_1, B_2, C_2): 4 + 5 + 8 = 17

(A_2, B_1, C_2): 6 + 1 + 8 = 15

(A_2, B_2, C_1): 6 + 5 + 3 = 14

(A_1, B_1, C_2): 4 + 1 + 8 = 13

(A_1, B_2, C_1): 4 + 5 + 3 = 12

(A_2, B_1, C_1): 6 + 1 + 3 = 10

(A_1, B_1, C_1): 4 + 1 + 3 = 8

Sample Input 2

3 3 3 5
1 10 100
2 20 200
1 10 100

Sample Output 2

400
310
310
301
301

There may be multiple combinations of cakes with the same sum of the deliciousness. For example, in this test case, the sum of A_1, B_3, C_3 and the sum of A_3, B_3, C_1 are both 301.
However, they are different ways of choosing cakes, so 301 occurs twice in the output.

Sample Input 3

10 10 10 20
7467038376 5724769290 292794712 2843504496 3381970101 8402252870 249131806 6310293640 6690322794 6082257488
1873977926 2576529623 1144842195 1379118507 6003234687 4925540914 3902539811 3326692703 484657758 2877436338
4975681328 8974383988 2882263257 7690203955 514305523 6679823484 4263279310 585966808 3752282379 620585736

Sample Output 3

23379871545
22444657051
22302177772
22095691512
21667941469
21366963278
21287912315
21279176669
21160477018
21085311041
21059876163
21017997739
20703329561
20702387965
20590247696
20383761436
20343962175
20254073196
20210218542
20150096547

Note that the input or output may not fit into a 32-bit integer type.


implicit none
integer(8):: n, i, ans, k, x, y, z, j
integer(8),allocatable,dimension(:):: a,b,c,d,e

read (5,*) x,y,z,n

allocate(a(x))
allocate(b(y))
allocate(c(z))
allocate(d(x*y))
allocate(e(min(x*y, 3000) * z))

d = 0
e = 0
read(5,*) (a(i),i=1,x)
read(5,*) (b(i),i=1,y)
read(5,*) (c(i),i=1,z)

do i = 1, x
    do j = 1, y
        d( (i-1) * y + j ) = a(i) + b(j)
    enddo
enddo

call heapsort(x,c)
call heapsort(x*y,d)

do i = x*y, max(x*y-3000+1, 1), -1
    do j = 1, z
        e((x*y-i) * z + j ) = d(i) + c(j)
    enddo
enddo

call heapsort(min(x*y, 3000) * z, e)

do i = min(x*y, 3000) * z,  min(x*y, 3000) * z - n +1, -1
    write(6,*) e(i)
enddo

end

!---http://slpr.sakura.ne.jp/qp/sortf90/---
subroutine heapsort(n,array)
    implicit none
    integer(8),intent(in) :: n
    double precision,intent(inout) :: array(1:n)

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

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

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

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 cake_123
  implicit none
  integer :: x, y, z, k, i, j, m, n
  integer(8) :: a(1000), b(1000), c(1000), d(2000000)
  integer :: u, v, w
  a = 0_8
  b = 0_8
  c = 0_8
  d = 0_8
  read(*,*) x, y, z, k
  read(*,*) a(1:x)
  read(*,*) b(1:y)
  read(*,*) c(1:z)
  call merge_sort(x,a(1:x))
  call merge_sort(y,b(1:y))
  call merge_sort(z,c(1:z))
  if (x*y*z.le.1000000) then
    do i = 1, x
      do j = 1, y
        do m = 1, z
          d(y*z*(i-1)+z*(j-1)+m) = a(i)+b(j)+c(m)
        end do
      end do
    end do
    call merge_sort(x*y*z,d(1:x*y*z))
    do i = 1, k
      write(*,'(i0)') d(i)
    end do
    stop
  end if
  u = x/5
  if (u.lt.1) u = x
  v = y/5
  if (v.lt.1) v = y
  w = z/5
  if (w.lt.1) w = z
  do i = 1, u
    do j = 1, v
      do m = 1, w
        d(v*w*(i-1)+w*(j-1)+m) = a(i)+b(j)+c(m)
      end do
    end do
  end do
  call merge_sort(u*v*w,d(1:u*v*w))
  do i = 1, k
    write(*,'(i0)') d(i)
  end do
contains
  subroutine merge_sort(n,c)
    implicit none
    integer, intent(in)       :: n
    integer(8), intent(inout) :: c(n)
    integer                   :: num, gth, i, ubnd
    num = n
    gth = 1
    do while (num.gt.1)
      do i = 1, num/2
        ubnd = min(2*i*gth,n)
        call merge(c(2*(i-1)*gth+1:(2*i-1)*gth),c((2*i-1)*gth+1:ubnd))
      end do
      gth = 2*gth
      num = (num+1)/2
    end do
    return
  end subroutine merge_sort

  subroutine merge(c1,c2)
    implicit none
    integer(8), intent(inout) :: c1(:), c2(:)
    integer(8)                :: c(size(c1)+size(c2))
    integer                   :: i1, i2, n1, n2
    i1 = 1
    i2 = 1
    n1 = size(c1)
    n2 = size(c2)
    do while (i1.le.n1.and.i2.le.n2)
      if (c1(i1).ge.c2(i2)) then
        c(i1+i2-1) = c1(i1)
        i1 = i1+1
      else
        c(i1+i2-1) = c2(i2)
        i2 = i2+1
      end if
    end do
    if (i1.le.n1) then
      c(i1+i2-1:n1+n2) = c1(i1:n1)
    else if (i2.le.n2) then
      c(i1+i2-1:n1+n2) = c2(i2:n2)
    end if
    c1 = c(1:n1)
    c2 = c(n1+1:n1+n2)
    return
  end subroutine merge
end program cake_123


program cake_123
  implicit none
  integer :: x, y, z, k, i, j, m, n
  integer(8) :: a(1000), b(1000), c(1000), d(40000)
  integer :: u, v, w
  a = 0_8
  b = 0_8
  c = 0_8
  d = 0_8
  read(*,*) x, y, z, k
  read(*,*) a(1:x)
  read(*,*) b(1:y)
  read(*,*) c(1:z)
  call merge_sort(x,a(1:x))
  call merge_sort(y,b(1:y))
  call merge_sort(z,c(1:z))
  if (x*y*z.le.40000) then
    do i = 1, x
      do j = 1, y
        do m = 1, z
          d(y*z*(i-1)+z*(j-1)+m) = a(i)+b(j)+c(m)
        end do
      end do
    end do
    call merge_sort(x*y*z,d(1:x*y*z))
    do i = 1, k
      write(*,'(i0)') d(i)
    end do
    stop
  end if
  u = x/30
  if (u.lt.1) u = x
  v = y/30
  if (v.lt.1) v = y
  w = z/30
  if (w.lt.1) w = z
  do i = 1, u
    do j = 1, v
      do m = 1, w
        d(v*w*(i-1)+w*(j-1)+m) = a(i)+b(j)+c(m)
      end do
    end do
  end do
  call merge_sort(u*v*w,d(1:u*v*w))
  do i = 1, k
    write(*,'(i0)') d(i)
  end do
contains
  subroutine merge_sort(n,c)
    implicit none
    integer, intent(in)       :: n
    integer(8), intent(inout) :: c(n)
    integer                   :: num, gth, i, ubnd
    num = n
    gth = 1
    do while (num.gt.1)
      do i = 1, num/2
        ubnd = min(2*i*gth,n)
        call merge(c(2*(i-1)*gth+1:(2*i-1)*gth),c((2*i-1)*gth+1:ubnd))
      end do
      gth = 2*gth
      num = (num+1)/2
    end do
    return
  end subroutine merge_sort

  subroutine merge(c1,c2)
    implicit none
    integer(8), intent(inout) :: c1(:), c2(:)
    integer(8)                :: c(size(c1)+size(c2))
    integer                   :: i1, i2, n1, n2
    i1 = 1
    i2 = 1
    n1 = size(c1)
    n2 = size(c2)
    do while (i1.le.n1.and.i2.le.n2)
      if (c1(i1).ge.c2(i2)) then
        c(i1+i2-1) = c1(i1)
        i1 = i1+1
      else
        c(i1+i2-1) = c2(i2)
        i2 = i2+1
      end if
    end do
    if (i1.le.n1) then
      c(i1+i2-1:n1+n2) = c1(i1:n1)
    else if (i2.le.n2) then
      c(i1+i2-1:n1+n2) = c2(i2:n2)
    end if
    c1 = c(1:n1)
    c2 = c(n1+1:n1+n2)
    return
  end subroutine merge
end program cake_123


program prob39
    implicit none
    integer :: q,w,e
    read(*,*) q,w,e
    if(q==w .and. w==e)then
        write(*,*) 'Yes'
    else
        write(*,*) 'No'
    end if

    stop
contains
end program prob39

Score : 100 points

Problem Statement

You are given three integers A, B and C.

Determine if there exists an equilateral triangle whose sides have lengths A, B and C.

Constraints

All values in input are integers.

1 \leq A,B,C \leq 100

Input

Input is given from Standard Input in the following format:

A B C

Output

If there exists an equilateral triangle whose sides have lengths A, B and C, print Yes; otherwise, print No.

Sample Input 1

2 2 2

Sample Output 1

Yes

There exists an equilateral triangle whose sides have lengths 2, 2 and 2.

Sample Input 2

3 4 5

Sample Output 2

No

There is no equilateral triangle whose sides have lengths 3, 4 and 5.


Integer A,B,C
read*,A,B,C
if (A==B.and.B==C)then
  print"(a)","Yes"
else
  print"(A)","No"
endif
end


PROGRAM ATCODER

implicit none
character(len=100) :: s
integer :: i,count=0,n

read*, n,s

do i = 1, n, 1
  if ( s(i:i) == 'R' ) then
    count = count + 1
  end if
end do

if ( count > n/2 ) then
  print'(a)', 'Yes'
else
  print'(a)', 'No'
end if

contains
!!!!!! サブルーチンは↓
!!!!!!

END PROGRAM ATCODER

Score : 200 points

Problem Statement

There are N people numbered 1 to N. Each person wears a red hat or a blue hat.

You are given a string s representing the colors of the people. Person i wears a red hat if s_i is R, and a blue hat if s_i is B.

Determine if there are more people wearing a red hat than people wearing a blue hat.

Constraints

1 \leq N \leq 100

|s| = N

s_i is R or B.

Input

Input is given from Standard Input in the following format:

N
s

Output

If there are more people wearing a red hat than there are people wearing a blue hat, print Yes; otherwise, print No.

Sample Input 1

4
RRBR

Sample Output 1

Yes

There are three people wearing a red hat, and one person wearing a blue hat.

Since there are more people wearing a red hat than people wearing a blue hat, the answer is Yes.

Sample Input 2

4
BRBR

Sample Output 2

No

There are two people wearing a red hat, and two people wearing a blue hat.

Since there are as many people wearing a red hat as people wearing a blue hat, the answer is No.


Integer N,Q
Character(1000000) moji
character(1),Allocatable,Dimension(:)::t,d
integer l,r
integer mi,ma,md
read*,N,Q
allocate(t(Q),d(Q))

read*,moji

do i=1,Q
  read*,t(i),d(i)
end do
!二部探索法、まずは0に落ちるもの
mi=0!辿り着く
ma=N+1!辿り着かないもの
Do while(mi+1<ma)
  md=(mi+ma)/2
  if( achievableLEFT(N,moji,Q,t,d,md) )then
    mi=md
  else
    ma=md
  end if
End do
l=md

!二部探索法N+1に落ちるもの
mi=0
ma=N+1
Do while(mi+1<ma)
  md=(mi+ma)/2
  if( achievableRIGHT(N,moji,Q,t,d,md) )then
    ma=md
  else
    mi=md
  end if
End do
r=ma

print"(i0)",r-l-1

contains
pure function achievableLEFT(N,moji,Q,t,d,md)
  Integer,intent(in)::N,Q
  Character(1000000),intent(in)::moji
  Character(1),intent(in)::t(1:Q),d(1:Q)
  Integer,intent(in)::md
  Integer soeji,i
  logical::achievableLEFT

  soeji=md
  do i=1,Q
    if (soeji==0 .or. soeji==N+1)EXIT
    if( moji(soeji:soeji) ==t(i) )then
      if(d(i)=="L")then
        soeji=soeji-1
      endif
      if(d(i)=="R")then
        soeji=soeji+1
      endif
    endif
  end do
  achievableLEFT=(soeji==0)
end function achievableLEFT
pure function achievableRIGHT(N,moji,Q,t,d,md)
  Integer,intent(in)::N,Q
  Character(1000000),intent(in)::moji
  Character(1),intent(in)::t(1:Q),d(1:Q)
  Integer,intent(in)::md
  Integer soeji,i
  logical::achievableRIGHT

  soeji=md
  do i=1,Q
    if (soeji==0 .or. soeji==N+1)EXIT
    if( moji(soeji:soeji) == t(i) )then
      if(d(i)=="L")then
        soeji=soeji-1
      endif
      if(d(i)=="R")then
        soeji=soeji+1
      endif
    endif
  end do
  achievableRight=(soeji==N+1)
end function achievableRIGHT
end

Score : 500 points

Problem Statement

There are N squares numbered 1 to N from left to right.
Each square has a character written on it, and Square i has a letter s_i. Besides, there is initially one golem on each square.

Snuke cast Q spells to move the golems.

The i-th spell consisted of two characters t_i and d_i, where d_i is L or R.
When Snuke cast this spell, for each square with the character t_i, all golems on that square moved to the square adjacent to the left if d_i is L, and moved to the square adjacent to the right if d_i is R.

However, when a golem tried to move left from Square 1 or move right from Square N, it disappeared.

Find the number of golems remaining after Snuke cast the Q spells.

Constraints

1 \leq N,Q \leq 2 \times 10^{5}

|s| = N

s_i and t_i are uppercase English letters.

d_i is L or R.

Input

Input is given from Standard Input in the following format:

N Q
s
t_1 d_1
\vdots
t_{Q} d_Q

Output

Print the answer.

Sample Input 1

3 4
ABC
A L
B L
B R
A R

Sample Output 1

2

Initially, there is one golem on each square.

In the first spell, the golem on Square 1 tries to move left and disappears.

In the second spell, the golem on Square 2 moves left.

In the third spell, no golem moves.

In the fourth spell, the golem on Square 1 moves right.

After the four spells are cast, there is one golem on Square 2 and one golem on Square 3, for a total of two golems remaining.

Sample Input 2

8 3
AABCBDBA
A L
B R
A R

Sample Output 2

5

After the three spells are cast, there is one golem on Square 2, two golems on Square 4 and two golems on Square 6, for a total of five golems remaining.

Note that a single spell may move multiple golems.

Sample Input 3

10 15
SNCZWRCEWB
B R
R R
E R
W R
Z L
S R
Q L
W L
B R
C L
A L
N L
E R
Z L
S L

Sample Output 3

3


program main
  implicit none
  character(len=1),parameter ::alp(26) &
       = (/"A","B","C","D","E","F","G","H","I","J","K","L",&
       "M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"/)
  integer :: N ,Q
  character(len=200000) :: S
  integer,allocatable:: g(:),g_bf(:)
  integer :: out
  logical,allocatable:: flag(:)
  integer :: nalp(26)
  integer :: nchr
  type chr
     integer,allocatable :: ind(:)
  end type chr
  type(chr) :: index_chr(26) ! (:,count(nalp/=0))
  character (len=1),allocatable :: SS(:)
  character (len=1),allocatable :: t(:)
  character (len=1),allocatable :: d(:)
  integer :: ichr
  integer :: i,j,jj
  read(*,*) N,Q
  read(*,'(A)') S(1:N)
  allocate(t(Q),d(Q))
  allocate(g(0:N+1))
  allocate(g_bf(N))
  out=N
  g=1
  do i = 1,Q
     read(*,'(A,1x,A)') t(i),d(i)
  end do
  !
  ! set up allay map
  !
  nalp(:) = 0
  do i = 1,N
     do j = 1,26
        if (alp(j) == S(i:i)) then
           nalp(j) = nalp(j) + 1
           exit
        end if
     end do
  end do
  do i = 1,26
     if (nalp(i) ==0 ) cycle
     allocate( index_chr(i)%ind(nalp(i)) )
     ichr = 0
     do j = 1,N
        if (alp(i) == S(j:j)) then
           ichr = ichr + 1
           index_chr(i)%ind(ichr) = j
        end if
     end do
  end do
  ! main loop
  do i = 1,Q
     g_bf = g(1:N)
     ! get alp
     do jj = 1,26
        if (alp(jj) == t(i)) then
           ichr = jj
           exit
        end if
     end do
     if ( nalp(ichr) == 0 ) cycle ! nothing done
     !
     if (d(i) == 'L') then
        do jj = 1,nalp(ichr)
           j = index_chr(ichr)%ind(jj)
           g(j) = g(j) - g_bf(j)
           g(j-1) = g(j-1) + g_bf(j)
        end do
     else
        do jj = 1,nalp(ichr)
           j = index_chr(ichr)%ind(jj)
           g(j) = g(j) - g_bf(j)
           g(j+1) = g(j+1) + g_bf(j)
        end do
     end if
     !where(g<0) g=0
  end do
  write(*,'(i0)') sum(g(1:N))
  !write(*,'(i0)') out
  stop
end program main


PROGRAM ATCODER

implicit none
integer :: n,q,i,c_l=1,c_r=1
character(len=2*10**5) :: s,s_l='',s_r=''
character(len=1),allocatable :: d(:),t(:)

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

do i = q, 1, -1
  if ( d(i) == 'L' ) then
    if ( s(c_l:c_l) == t(i) ) then
      c_l = c_l + 1
    end if
  else if ( d(i) == 'R' ) then
    if ( s(n+1-c_r:n+1-c_r) == t(i) ) then
      c_r = c_r + 1
    end if
  end if
end do

if ( n-(c_l-1)-(c_r-1) < 0 ) then
  print'(i0)',  0
end if

print'(i0)', n-(c_l-1)-(c_r-1)

contains
!!!!!! サブルーチンは↓
!!!!!!

END PROGRAM ATCODER


program main
implicit none
character(len=1) :: c

read(*,*) c
if( c=='A')then
write(*,'(a1)') 'T'
else if ( c =='T')then
write(*,'(a1)') 'A'
else if ( c == 'C')then
write(*,'(a1)') 'G'
else if ( c == 'G')then
write(*,'(a1)') 'C'
end if

end program main

Score : 100 points

Problem Statement

On the Planet AtCoder, there are four types of bases: A, C, G and T. A bonds with T, and C bonds with G.

You are given a letter b as input, which is A, C, G or T. Write a program that prints the letter representing the base that bonds with the base b.

Constraints

b is one of the letters A, C, G and T.

Input

Input is given from Standard Input in the following format:

b

Output

Print the letter representing the base that bonds with the base b.

Sample Input 1

A

Sample Output 1

T

Sample Input 2

G

Sample Output 2

C


program main
  implicit none
  character(len=10) :: s
  integer :: i, j, l = 0

  read *, s

  do i = 1, 10
    do j = 10, i, -1
      if (verify(s(i:j), 'ATGC') .eq. 0) then
        l = max(l, j-i+1)
        cycle
      end if
    end do
  end do

  print '(i0)', l

end program

Score : 200 points

Problem Statement

You are given a string S consisting of uppercase English letters. Find the length of the longest ACGT string that is a substring (see Notes) of S.

Here, a ACGT string is a string that contains no characters other than A, C, G and T.

Notes

A substring of a string T is a string obtained by removing zero or more characters from the beginning and the end of T.

For example, the substrings of ATCODER include TCO, AT, CODER, ATCODER and (the empty string), but not AC.

Constraints

S is a string of length between 1 and 10 (inclusive).

Each character in S is an uppercase English letter.

Input

Input is given from Standard Input in the following format:

S

Output

Print the length of the longest ACGT string that is a substring of S.

Sample Input 1

ATCODER

Sample Output 1

3

Among the ACGT strings that are substrings of ATCODER, the longest one is ATC.

Sample Input 2

HATAGAYA

Sample Output 2

5

Among the ACGT strings that are substrings of HATAGAYA, the longest one is ATAGA.

Sample Input 3

SHINJUKU

Sample Output 3

0

Among the ACGT strings that are substrings of SHINJUKU, the longest one is (the empty string).


program name
    implicit none
    integer(4):: n,q
    character(:),allocatable:: s
    integer(4),allocatable:: acnum(:), lr(:,:)
    integer(4):: i,j

    read*, n,q
    allocate(character(n):: s)
    read*, s
    allocate(acnum(n), lr(2,q))

    do i=1,q
        read*, lr(:,i)
    end do

    acnum(1)=0

    do i=2,n
        if (s(i-1:i) == 'AC')then
            acnum(i) = acnum(i-1)+1
        else
            acnum(i) = acnum(i-1)
        end if
    end do

    do i=1,q
        print*, acnum(lr(2,i)) - acnum(lr(1,i))
    end do

end program name

Score : 300 points

Problem Statement

You are given a string S of length N consisting of A, C, G and T. Answer the following Q queries:

Query i (1 \leq i \leq Q): You will be given integers l_i and r_i (1 \leq l_i < r_i \leq N). Consider the substring of S starting at index l_i and ending at index r_i (both inclusive). In this string, how many times does AC occurs as a substring?

Notes

A substring of a string T is a string obtained by removing zero or more characters from the beginning and the end of T.

For example, the substrings of ATCODER include TCO, AT, CODER, ATCODER and (the empty string), but not AC.

Constraints

2 \leq N \leq 10^5

1 \leq Q \leq 10^5

S is a string of length N.

Each character in S is A, C, G or T.

1 \leq l_i < r_i \leq N

Input

Input is given from Standard Input in the following format:

N Q
S
l_1 r_1
:
l_Q r_Q

Output

Print Q lines. The i-th line should contain the answer to the i-th query.

Sample Input 1

8 3
ACACTACG
3 7
2 3
1 8

Sample Output 1

2
0
3

Query 1: the substring of S starting at index 3 and ending at index 7 is ACTAC. In this string, AC occurs twice as a substring.

Query 2: the substring of S starting at index 2 and ending at index 3 is CA. In this string, AC occurs zero times as a substring.

Query 3: the substring of S starting at index 1 and ending at index 8 is ACACTACG. In this string, AC occurs three times as a substring.


PROGRAM ATCODER

implicit none
integer(16) :: n,q,i,j,l,r,t,u
character(len=10**5) :: s,b
integer,allocatable :: c(:),count(:)

read(*,*) n,q,s

allocate(c(n/2),count(n))
c = n+1
count = 0

t = 1
do i = 1,n/2
  u = index(s(t:),'AC')
  If (u==0) exit
  c(i) = t + u
  t = t + u
end do

do i = 2, n
  do j = 1, n/2
    if(i >= c(j))then
      count(i) = count(i) + 1
    end if
  end do
end do

do i = 1,q
  read(*,*) l,r
  write(*,'(i0)') count(r) - count(l)
end do

contains
!!!!!! サブルーチンは↓
!!!!!!

END PROGRAM ATCODER


implicit none

integer n,q,i,left,right,j,l,r,k
character(:),allocatable:: s
integer,allocatable,dimension(:) ::counts
integer ::cnt=0

read(5,*)n,q
allocate(character(n)::s)
allocate(counts(n))
counts(:)=0

read(5,*)s

do k=1,n-1
if(s(k:k+1)=="AC") then
counts(k)=1
counts(k+1)=2
endif
enddo

do i=1,q
read(5,*)l,r
do j=l,r-1

if(counts(j)==1 .and. counts(j+1)==2) then
cnt=cnt+1
endif

enddo
write(6,*)cnt
cnt=0
enddo

deallocate(counts)
deallocate(s)
end


program atgc3
  implicit none
  character(len=100000) :: S
  integer :: i,j,N,Q,CountAC,sita,ue
  integer,allocatable :: LR(:,:)
  integer,allocatable :: Flags(:)
  read(*,*) N,Q
  allocate(LR(2,Q))
  read(*,*) S
  !write(*,*) trim(S)
  allocate(Flags(N))
  Flags=0

  !write(*,*) Flags
  sita=N
  ue=1
  do i=1,Q
     read(*,*) LR(:,i)
     if(sita.gt.LR(1,i)) then
        sita=LR(1,i)
     endif
     if(ue.lt.LR(2,i)) then
        ue=LR(2,i)
     endif
  enddo
  do i=sita,ue
     if(S(i:i+1).eq.'AC') then
        Flags(i)=1
     endif
  enddo

  do i=1,Q
     CountAC=sum(Flags(LR(1,i):LR(2,i)-1))
     write(*,'(i0)') CountAC
  enddo

  deallocate(LR)
  deallocate(Flags)

end program atgc3


program atgc3
  implicit none
  character(len=10000) :: S
  integer :: i,j,N,Q,CountAC
  integer,allocatable :: LR(:,:)

  read(*,*) N,Q
  allocate(LR(2,Q))
  read(*,*) S
  do i=1,Q
     read(*,*) LR(:,i)
  enddo
  do i=1,Q
     CountAC=0
!     write(*,*) LR(:,i)
     do j=LR(1,i),LR(2,i)-1
!        write(*,*) S(j:j+1)!,j,Q
        if(S(j:j+1).eq.'AC') then
           CountAC=CountAC+1
        endif
       !
     enddo
     write(*,'(i0)') CountAC
  enddo

  deallocate(LR)

end program atgc3


program main
implicit none
integer :: n,q,l,r
character(len=10**5) :: s
integer :: i,j,nc

read(*,*) n,q
read(*,*) s(1:n)
do i = 1, q
   read(*,*) l,r

   nc = 0
   do j = l, r-1
      if (s(j:j+1) =='AC')then
         nc = nc+1
      end if
   end do
   write(*,'(i0)') nc

end do

end program main


INTEGER N,Q
CHARACTER(100000) S
INTEGER L,R
INTEGER CNT
READ*,N,Q
READ*,S
DO I=1,Q
  CNT=0
  READ*,L,R
  DO
    IF(INDEX(S(L:R),"AC")==0)EXIT
    L=L+INDEX(S(L:R),"AC")
    CNT=CNT+1
    CYCLE
  END DO
  PRINT"(I0)",CNT
END DO
END


program main
  implicit none
  integer :: n, i
  integer, allocatable :: b(:), res(:)

  read (*, *) n
  allocate (b(n), res(n))
  read (*, *) b(:)

  do i = 1, n
    if (b(i) > i) then
      write (*, "(i0)") -1
      stop
    end if
  end do

  do i = 1, n
    call remove(b, res(n - i + 1))
  end do

  do i = 1, n
    write (*, "(i0)") res(i)
  end do

  contains

  subroutine remove(b, ret)
    integer, allocatable, intent(inout) :: b(:)
    integer, intent(out) :: ret
    integer :: n, i, c(size(b))
    n = size(b)
    c = [(b(i) - i, i = 1, n)]
    loop: block
      do i = n, 1, -1
        if (c(i) == 0) exit loop
      end do
      stop 1
    end block loop
    ret = b(i)
    b = [b(:i - 1), b(i + 1:)]
  end subroutine remove
end program main

Score : 400 points

Problem Statement

Snuke has an empty sequence a.

He will perform N operations on this sequence.

In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1).

You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq b_i \leq N

Input

Input is given from Standard Input in the following format:

N
b_1 \dots b_N

Output

If there is no sequence of N operations after which a would be equal to b, print -1.
If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted.

Sample Input 1

3
1 2 1

Sample Output 1

1
1
2

In this sequence of operations, the sequence a changes as follows:

After the first operation: (1)

After the second operation: (1,1)

After the third operation: (1,2,1)

Sample Input 2

2
2 2

Sample Output 2

-1

2 cannot be inserted at the beginning of the sequence, so this is impossible.

Sample Input 3

9
1 1 1 2 2 1 2 3 2

Sample Output 3

1
2
2
3
1
2
2
1
1


program main
   implicit none
   integer(8):: n,han(97:122)=0,i,ans=1
   character(100000)::s
   read(*,*)n
   read(*,*)s
   do i=1,n
      han(iachar(s(i:i)))=han(iachar(s(i:i)))+1
   enddo
   do i=97,122
      ans=mod(ans*(han(i)+1),10**9+7)
   enddo
   ans=ans-1
   write(*,*)ans
end program main

Score : 200 points

Problem Statement

You are given a string S of length N.
Among its subsequences, count the ones such that all characters are different, modulo 10^9+7. Two subsequences are considered different if their characters come from different positions in the string, even if they are the same as strings.

Here, a subsequence of a string is a concatenation of one or more characters from the string without changing the order.

Constraints

1 \leq N \leq 100000

S consists of lowercase English letters.

|S|=N

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the number of the subsequences such that all characters are different, modulo 10^9+7.

Sample Input 1

4
abcd

Sample Output 1

15

Since all characters in S itself are different, all its subsequences satisfy the condition.

Sample Input 2

3
baa

Sample Output 2

5

The answer is five: b, two occurrences of a, two occurrences of ba. Note that we do not count baa, since it contains two as.

Sample Input 3

5
abcab

Sample Output 3

17


program differ_by_1_bit
  implicit none
  integer :: n, a, b, i, m, p(0:131071) = 0
  read(*,*) n, a, b
  if (mod(popcnt(a),2) == mod(popcnt(b),2)) then
    write(*,'(a)') "NO"
    stop
  end if
  m = lshift(1,n)-1
  p(0:m) = compose(n,a,b)
  write(*,'(a)') "YES"
  write(*,'(i0)',advance='no') p(0)
  do i = 1, m
    write(*,'(x,i0)',advance='no') p(i)
  end do
  write(*,*)
  stop
contains
  recursive function compose(n,a,b) result(p)
    implicit none
    integer, intent(in) :: n, a, b
    integer :: s, t, x = 0, y = 0, c = 0, i, j, k
    integer :: p(0:lshift(1,n)-1), q(0:lshift(1,n-1)-1), r(0:lshift(1,n-1)-1)
    p = 0
    if (n == 1) then
      p(0:1) = (/a,b/)
      return
    end if
    s = lshift(1,n-1)-1
    t = lshift(1,n)-1
    do i = 0, n-1
      if (btest(a,i) .eqv. btest(b,i)) cycle
      do j = 0, i-1
        if (btest(a,j)) x = ibset(x,j)
        if (btest(b,j)) y = ibset(y,j)
      end do
      k = i
      do j = i+1, n-1
        if (btest(a,j)) x = ibset(x,j-1)
        if (btest(b,j)) y = ibset(y,j-1)
      end do
      c = ibset(x,0)
      if (btest(x,0)) c = ibclr(x,0)
      exit
    end do
    q = compose(n-1,x,c)
    r = compose(n-1,c,y)
    do i = 0, s
      do j = 0, k-1
        if (btest(q(i),j)) p(i) = ibset(p(i),j)
        if (btest(r(i),j)) p(i+s+1) = ibset(p(i+s+1),j)
      end do
      if (btest(a,k)) p(i) = ibset(p(i),k)
      if (btest(b,k)) p(i+s+1) = ibset(p(i+s+1),k)
      do j = k+1, n-1
        if (btest(q(i),j-1)) p(i) = ibset(p(i),j)
        if (btest(r(i),j-1)) p(i+s+1) = ibset(p(i+s+1),j)
      end do
    end do
    return
  end function compose
end program differ_by_1_bit

Score : 800 points

Problem Statement

You are given integers N,\ A and B.
Determine if there exists a permutation (P_0,\ P_1,\ ...\ P_{2^N-1}) of (0,\ 1,\ ...\ 2^N-1) that satisfies all of the following conditions, and create one such permutation if it exists.

P_0=A

P_{2^N-1}=B

For all 0 \leq i < 2^N-1, the binary representations of P_i and P_{i+1} differ by exactly one bit.

Constraints

1 \leq N \leq 17

0 \leq A \leq 2^N-1

0 \leq B \leq 2^N-1

A \neq B

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

If there is no permutation that satisfies the conditions, print NO.

If there is such a permutation, print YES in the first line.
Then, print (P_0,\ P_1,\ ...\ P_{2^N-1}) in the second line, with spaces in between.
If there are multiple solutions, any of them is accepted.

Sample Input 1

2 1 3

Sample Output 1

YES
1 0 2 3

The binary representation of P=(1,0,2,3) is (01,00,10,11), where any two adjacent elements differ by exactly one bit.

Sample Input 2

3 2 1

Sample Output 2

NO


program a_sequence_of_permutations
  implicit none
  integer :: n, k, p(100000) = 0, q(100000) = 0, r(100000,2) = 0, i
  read(*,*) n, k
  read(*,*) p(1:n)
  read(*,*) q(1:n)
  if (k == 1) then
    call output(p(1:n))
    stop
  end if
  r(1:n,2) = q(1:n)
  do i = 3, k
    r(1:n,1) = p(1:n)
    call quick_sort(r(1:n,:),1)
    p(1:n) = q(1:n)
    q(1:n) = r(1:n,2)
  end do
  call output(q(1:n))
contains
  subroutine output(a)
    integer, intent(in) :: a(:)
    integer :: n, i
    n = size(a)
    write(*,'(i0)',advance='no') a(1)
    do i = 2, n
      write(*,'(x,i0)',advance='no') a(i)
    end do
    write(*,*)
  end
  recursive subroutine quick_sort(a,i)
    integer, intent(inout) :: a(:,:)
    integer, intent(in) :: i
    integer :: n, l, r, m
    integer :: p, b(size(a,2))
    n = size(a,1)
    l = 1
    r = n
    m = (l+r)/2
    p = a(m,i)+a(l,i)+a(r,i)-max(a(m,i),a(l,i),a(r,i))-min(a(m,i),a(l,i),a(r,i))
    do
      do while (a(l,i) < p)
        l = l+1
      end do
      do while (a(r,i) > p)
        r = r-1
      end do
      if (l >= r) exit
      b = a(l,:)
      a(l,:) = a(r,:)
      a(r,:) = b
      l = l+1
      r = r-1
    end do
    if (1 < l-1) call quick_sort(a(1:l-1,:),i)
    if (r+1 < n) call quick_sort(a(r+1:n,:),i)
  end
end program a_sequence_of_permutations

Score : 1000 points

Problem Statement

For two permutations p and q of the integers from 1 through N, let f(p,q) be the permutation that satisfies the following:

The p_i-th element (1 \leq i \leq N) in f(p,q) is q_i.
Here, p_i and q_i respectively denote the i-th element in p and q.

You are given two permutations p and q of the integers from 1 through N.
We will now define a sequence {a_n} of permutations of the integers from 1 through N, as follows:

a_1=p, a_2=q

a_{n+2}=f(a_n,a_{n+1}) ( n \geq 1 )

Given a positive integer K, find a_K.

Constraints

1 \leq N \leq 10^5

1 \leq K \leq 10^9

p and q are permutations of the integers from 1 through N.

Input

Input is given from Standard Input in the following format:

N K
p_1 ... p_N
q_1 ... q_N

Output

Print N integers, with spaces in between.
The i-th integer (1 \leq i \leq N) should be the i-th element in a_K.

Sample Input 1

3 3
1 2 3
3 2 1

Sample Output 1

3 2 1

Since a_3=f(p,q), we just need to find f(p,q).
We have p_i=i here, so f(p,q)=q.

Sample Input 2

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

Sample Output 2

4 3 2 1 5

Sample Input 3

10 1000000000
7 10 6 5 4 2 9 1 3 8
4 1 9 2 3 7 8 10 6 5

Sample Output 3

7 9 4 8 2 5 1 6 10 3


Score : 100 points

Problem Statement

There are H rows and W columns of white square cells.

You will choose h of the rows and w of the columns, and paint all of the cells contained in those rows or columns.

How many white cells will remain?

It can be proved that this count does not depend on what rows and columns are chosen.

Constraints

All values in input are integers.

1 \leq H, W \leq 20

1 \leq h \leq H

1 \leq w \leq W

Input

Input is given from Standard Input in the following format:

H W
h w

Output

Print the number of white cells that will remain.

Sample Input 1

3 2
2 1

Sample Output 1

1

There are 3 rows and 2 columns of cells. When two rows and one column are chosen and painted in black, there is always one white cell that remains.

Sample Input 2

5 5
2 3

Sample Output 2

6

Sample Input 3

2 4
2 4

Sample Output 3

0


program sample
    implicit none

    integer(8) :: i,j,m,n,c,k,s

    integer(8),allocatable :: b(:),a(:)

    read(*,*) n,m,c
    allocate(b(m))
    allocate(a(m))
    read(*,*)b
    k=0
    do i=1,n
        read(*,*)a
        s=0
        do j=1,m
            s=s+a(j)*b(j)
        end do
        if (s+c>0)then
            k=k+1
        end if
    end do

    write(*,*) k
    stop
end program sample

Score : 200 points

Problem Statement

There are N pieces of source code. The characteristics of the i-th code is represented by M integers A_{i1}, A_{i2}, ..., A_{iM}.

Additionally, you are given integers B_1, B_2, ..., B_M and C.

The i-th code correctly solves this problem if and only if A_{i1} B_1 + A_{i2} B_2 + ... + A_{iM} B_M + C > 0.

Among the N codes, find the number of codes that correctly solve this problem.

Constraints

All values in input are integers.

1 \leq N, M \leq 20

-100 \leq A_{ij} \leq 100

-100 \leq B_i \leq 100

-100 \leq C \leq 100

Input

Input is given from Standard Input in the following format:

N M C
B_1 B_2 ... B_M
A_{11} A_{12} ... A_{1M}
A_{21} A_{22} ... A_{2M}
\vdots
A_{N1} A_{N2} ... A_{NM}

Output

Print the number of codes among the given N codes that correctly solve this problem.

Sample Input 1

2 3 -10
1 2 3
3 2 1
1 2 2

Sample Output 1

1

Only the second code correctly solves this problem, as follows:

Since 3 \times 1 + 2 \times 2 + 1 \times 3 + (-10) = 0 \leq 0, the first code does not solve this problem.

1 \times 1 + 2 \times 2 + 2 \times 3 + (-10) = 1 > 0, the second code solves this problem.

Sample Input 2

5 2 -4
-2 5
100 41
100 40
-3 0
-6 -2
18 -13

Sample Output 2

2

Sample Input 3

3 3 0
100 -100 0
0 100 100
100 100 100
-100 100 100

Sample Output 3

0

All of them are Wrong Answer. Except yours.


program abc121b
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: n,m,c,i,ans
    integer(int32), allocatable:: b(:), a(:,:)

    read*, n,m,c
    allocate(b(m))
    allocate(a(m,n))
    read*, b(:)
    do i=1,n
        read*, a(:,i)
    end do
    ans=0
    do i=1,n
        if (sum(a(:,i)*b(:))+c > 0)ans=ans+1
    end do
    print'(i0)', ans
end program abc121b


program main
implicit none

integer :: n,m,c
integer :: i,j,sm,cnt=0;
integer,allocatable :: a(:,:),b(:)
read(*,*)n,m,c
allocate(a(n,m),b(m))

read(*,*)b(1:m)
do i=1,n
	read(*,*)a(i,1:m)
end do
do i=1,n
	sm=0;
	do j=1,m
        sm = sm + a(i,j)*b(j)
    end do
    if (sm+c > 0) then
    	cnt = cnt +1
    end if
end do

write(*,'(i0)')cnt

end program


integer(8) :: n,m,c,ans=0,p
integer(8),allocatable,dimension(:) :: B,A
read*,n,m,c
allocate(B(m),A(m))
read(*,*)(B(i),i=1,m)
do i = 1,n
    read(*,*)(A(j),j=1,m)
    p = sum(A*B) + c
    if(p>0)then
        ans = ans + 1
    endif
enddo
write(*,'(i0)')ans
end


program main
	implicit none
	integer :: N, M, C
	integer, allocatable :: A(:, :), B(:)
	integer :: counter, i
	read(*, *) N, M, C
	allocate(A(1:N, 1:M), B(1:M))
	read(*, *) B
	do i = 1, N
		read(*, *) A(i, 1:M)
	end do
	B = MATMUL(A,B)+C
	counter = 0
	do i = 1, N
		if(B(i) > 0) then
			counter = counter + 1
		end if
	end do
	write(*, *) counter
end program main


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

read(*,*) n, m
allocate( a(n), b(n) )
do i = 1, n
   read(*,*) a(i), b(i)
end do
call heapsort2( n, a, b )

i = 0
j = 1
k = 0
do while( i+b(j) .le. m )
   k = k + a(j)*b(j)
   i = i + b(j)
   j = j + 1
end do
k = k + (m-i)*a(j)
write(*,'(i0)') k

stop
contains
subroutine heapsort2(n,array,array2)
  implicit none
  integer(8),intent(in) :: n
  integer(8),intent(inout) :: array(1:n), array2(1:n)
  integer(8) ::i,k,j,l
  integer(8) :: t, u

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

end program main

Score : 300 points

Problem Statement

Hearing that energy drinks increase rating in those sites, Takahashi decides to buy up M cans of energy drinks.

There are N stores that sell energy drinks. In the i-th store, he can buy at most B_i cans of energy drinks for A_i yen (the currency of Japan) each.

What is the minimum amount of money with which he can buy M cans of energy drinks?

It is guaranteed that, in the given inputs, a sufficient amount of money can always buy M cans of energy drinks.

Constraints

All values in input are integers.

1 \leq N, M \leq 10^5

1 \leq A_i \leq 10^9

1 \leq B_i \leq 10^5

B_1 + ... + B_N \geq M

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

Print the minimum amount of money with which Takahashi can buy M cans of energy drinks.

Sample Input 1

2 5
4 9
2 4

Sample Output 1

12

With 12 yen, we can buy one drink at the first store and four drinks at the second store, for the total of five drinks. However, we cannot buy 5 drinks with 11 yen or less.

Sample Input 2

4 30
6 18
2 5
3 10
7 9

Sample Output 2

130

Sample Input 3

1 100000
1000000000 100000

Sample Output 3

100000000000000

The output may not fit into a 32-bit integer type.


program main
	implicit none
    integer :: n, m
    integer, allocatable :: a(:), b(:)
    integer :: i, j, tmp1, tmp2
    integer :: tenpo, kakaku, kosu, amari
    read(*, *) n, m
    allocate(a(n)); allocate(b(n))
    do i = 1, n
    	read(*, *) a(i), b(i)
    enddo
    do i = 1, n
    	do j = i+1, n
        	if (a(i).lt.a(j)) then
            	tmp1 = a(i)
                a(i) = a(j)
                a(j) = tmp1
                tmp2 = b(i)
                b(i) = b(j)
                b(j) = tmp2
            end if
		enddo
	enddo
    kosu = 0
    tenpo = 0
    do i = 1, n
		kosu = kosu + b(i)
        tenpo = tenpo + 1
        if (kosu.ge.m) then
        	amari = kosu - m
            exit
        end if
    enddo
    do i = 1, tenpo-1
    	kakaku = kakaku + a(i)*b(i)
    enddo
    kakaku = kakaku + a(tenpo)*(b(tenpo)-amari)
    write(*, *) kakaku
stop
end program main


module mymodule
  implicit none
  public
  type store_type
    integer(8) :: price, storage
  end type store_type
end module mymodule

program main
  use mymodule
  implicit none
  integer(8) :: n, m, i, cost
  type(store_type), allocatable :: stores(:)

  read (*, *) n, m
  allocate (stores(n))
  do i = 1, n
    read (*, *) stores(i) % price, stores(i) % storage
  end do

  call quicksort(stores)

  cost = 0
  do i = 1, n
    if (m <= stores(i) % storage) then
      cost = cost + m * stores(i) % price
      exit
    end if
    cost = cost + stores(i) % storage * stores(i) % price
    m = m - stores(i) % storage
  end do

  write (*, "(i0)") cost

  contains

  recursive subroutine quicksort(a)
    type(store_type), intent(inout) :: a(:)
    type(store_type) :: pivot, tmp
    integer :: i, j, k, n
    real(4) :: r
    n = size(a)
    if (n <= 1) return
    i = 1
    j = n
    call random_number(r)
    k = 1 + floor(r * n)
    k = max(1, min(k, n))
    pivot = a(k)
    do
      do while (pivot % price > a(i) % price)  ! Derived type comparison
        i = i + 1
      end do
      do while (a(j) % price > pivot % price)  ! Derived type comparison
        j = j - 1
      end do
      if (i >= j) exit
      tmp = a(i)
      a(i) = a(j)
      a(j) = tmp
      i = i + 1
      j = j - 1
    end do
    call quicksort(a(1:i - 1))
    call quicksort(a(j + 1:n))
  end subroutine quicksort
end program main


Program ABC121C

integer(8) n,m,c,p,x
integer(8),allocatable::a(:),b(:)

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

c=0
p=0

do i=1,n
 do j=i+1,n
  if (a(i)>a(j)) then
   x=a(i)
   a(i)=a(j)
   a(j)=x
   x=b(i)
   b(i)=b(j)
   b(j)=x
  end if
 end do
end do

do i=1,n
 c=c+a(i)*b(i)
 p=p+b(i)
 if (p>=m) then
  c=c-a(i)*(p-m)
  go to 10
 end if
end do

10 write(*,*) c

stop
end


program main
  implicit none
  integer(8) :: N,M
  integer(8),allocatable :: A(:),B(:)
  integer(8),allocatable :: A2(:,:)
  integer(8) :: i
  integer(8) :: nout
  integer(8) :: out
  read(*,*) N,M
  allocate(A(N),B(N))
  do i = 1,N
     read(*,*) A(i),B(i)
  end do
  allocate(A2(N,2) )
  A2(:,1) = A(:)
  A2(:,2) = B(:)

  call hsort(N,A2)
  out=0
  nout=0
  do i = 1,N
     nout = nout + A2(i,2)
     out = out +A2(i,1) * A2(i,2)
     if ( nout >= M ) THEn
        out = out - A2(i,1)*(nout-M)
        exit
     end if
  end do
  write(*,'(i0)') out
  stop
contains
  subroutine hsort(n, x)
    implicit none
    integer(8), intent(IN) :: n
    integer(8), intent(INOUT) :: x(n,2)
    ! local
    integer(8) k
    integer(8) :: tmp,tmp2
    ! external subroutine
    !external mkheap

    ! 配列 x 全体をヒープにする。
    do k = n/2, 1, -1
       tmp = x(k,1)
       call mkheap(n, x, k, n, tmp)
    enddo

    ! ヒープの根と右下の葉を交換してソートする。
    do k = n, 2, -1
       tmp = x(k,1)
       x(k,1) = x(1,1)
       tmp2 = x(1,2)
       x(1,2) =x(k,2)
       x(k,2) = tmp2

       call mkheap(n, x, int(1,8), k-1, tmp)
    end do

    return
  end subroutine hsort

  subroutine mkheap(n, x, root, leaf, val)
    implicit none
    integer(8), intent(IN)    :: n, root, leaf
    integer(8), intent(INOUT) :: x(1:n,2)
    integer(8), intent(IN)    :: val
    ! local
    integer(8) :: i, j
    integer(8) :: tmp

    i = root
    j = i * 2

    do
       if (j > leaf) exit

       if (j < leaf) then
          if (x(j,1) < x(j+1,1)) j = j + 1
       end if

       if (x(j,1) > val) then
          x(i,1) = x(j,1)
          tmp = x(j,2)
          x(j,2) = x(i,2)
          x(i,2) = tmp
          i = j
          j = i * 2
       else
          j = leaf + 1
       endif
    end do

    x(i,1) = val

    return
  end subroutine mkheap

 end program main


program main
integer :: a,b,k,cnt=0
read(*,*)a,b,k

do i = 100,1,-1
if(mod(a,i)==0 .and. mod(b,i)==0) cnt = cnt + 1
if(cnt == k) exit
end do

write(*,*) i
end program main

Score : 200 points

Problem Statement

You are given positive integers A and B.

Find the K-th largest positive integer that divides both A and B.

The input guarantees that there exists such a number.

Constraints

All values in input are integers.

1 \leq A, B \leq 100

The K-th largest positive integer that divides both A and B exists.

K \geq 1

Input

Input is given from Standard Input in the following format:

A B K

Output

Print the K-th largest positive integer that divides both A and B.

Sample Input 1

8 12 2

Sample Output 1

2

Three positive integers divides both 8 and 12: 1, 2 and 4.
Among them, the second largest is 2.

Sample Input 2

100 50 4

Sample Output 2

5

Sample Input 3

1 1 1

Sample Output 3

1


program main
  implicit none
  integer :: a, b, k, i, j = 0

  read *, a, b, k

  do i = min(a, b), 1, -1
    j = j + 1
    if (mod(a, i) + mod(b, i) > 0) then
      j = j - 1
    end if
    if (j == k) then
      print '(i0)', i
      exit
    end if
  end do

end


program answer
  implicit none
  integer :: i, j, N, ans, count
  character(len=10**5) :: S
  read(*,*) S
  N=len_trim(S)
  ans=0
  count=1
  do
     if(count==0) then
        exit
     end if
     count=0
     do i = 1, N-1
        if(S(i:i)=='2') then
           cycle
        else if(S(i:i)=='1') then
           do j = i+1, N
              if(S(j:j)=='2') then
                 cycle
              else if(S(j:j)=='1') then
                 exit
              else if(S(j:j)=='0') then
                 S(i:i)='2'
                 S(j:j)='2'
                 ans=ans+1
                 count=count+1
                 exit
              end if
           end do
        else
            do j = i+1, N
              if(S(j:j)=='2') then
                 cycle
              else if(S(j:j)=='0') then
                 exit
              else
                 S(i:i)='2'
                 S(j:j)='2'
                 ans=ans+1
                 count=count+1
                 exit
              end if
           end do
        end if
     end do
  end do

  write(*,*) ans*2
  stop
 end program answer

Score : 300 points

Problem Statement

There are N cubes stacked vertically on a desk.

You are given a string S of length N. The color of the i-th cube from the bottom is red if the i-th character in S is 0, and blue if that character is 1.

You can perform the following operation any number of times: choose a red cube and a blue cube that are adjacent, and remove them. Here, the cubes that were stacked on the removed cubes will fall down onto the object below them.

At most how many cubes can be removed?

Constraints

1 \leq N \leq 10^5

|S| = N

Each character in S is 0 or 1.

Input

Input is given from Standard Input in the following format:

S

Output

Print the maximum number of cubes that can be removed.

Sample Input 1

0011

Sample Output 1

4

All four cubes can be removed, by performing the operation as follows:

Remove the second and third cubes from the bottom. Then, the fourth cube drops onto the first cube.

Remove the first and second cubes from the bottom.

Sample Input 2

11011010001011

Sample Output 2

12

Sample Input 3

0

Sample Output 3

0


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

    read(*,*)s
    n=len_trim(s)

    a=0
    b=0
    do i=1,n
        if(s(i:i).eq.'0')then
            a=a+1
        else
            b=b+1
        end if
    end do
    c=min(a,b)
    write(*,*)2*c
    stop
end program sample


module mod_union_find
  implicit none
  type union_find
    integer :: n = 0
    integer, pointer :: par(:), rnk(:), siz(:) => null()
  contains
    procedure :: init => inituf
    procedure :: release => releaseuf
    procedure :: find => find
    procedure :: same => same
    procedure :: unite => unite
    procedure :: size => size_of
  end type
  interface union_find
    module procedure :: newuf
  end interface union_find
contains
  function newuf(n) result(ret)
    integer, intent(in) :: n
    type(union_find) :: ret
    integer :: i
    ret%n = n
    allocate(ret%par(n),ret%rnk(n),ret%siz(n))
    ret%rnk = 0
    ret%siz = 1
    ret%par = [(i,i=1,n)]
  end
  subroutine inituf(this,n)
    class(union_find), intent(inout) :: this
    integer, intent(in) :: n
    integer :: i
    if (this%n /= n) then
      if (this%n /= 0) deallocate(this%par,this%rnk,this%siz)
      allocate(this%par(n),this%rnk(n),this%siz(n))
    end if
    this%n = n
    this%rnk = 0
    this%siz = 1
    this%par = [(i,i=1,n)]
  end
  subroutine releaseuf(this)
    class(union_find), intent(inout) :: this
    if (this%n /= 0) deallocate(this%par,this%rnk,this%siz)
    this%n = 0
  end
  recursive function find(this,i) result(ret)
    class(union_find), intent(inout) :: this
    integer, intent(in) :: i
    integer :: ret
    if (this%par(i) /= i) this%par(i) = find(this,this%par(i))
    ret = this%par(i)
  end
  logical function same(this,i,j)
    class(union_find), intent(inout) :: this
    integer, intent(in) :: i, j
    same = find(this,i) == find(this,j)
  end
  subroutine unite(this,i,j)
    class(union_find), intent(inout) :: this
    integer, intent(in) :: i, j
    integer :: x, y
    x = find(this,i)
    y = find(this,j)
    if (x == y) return
    if (this%rnk(x) < this%rnk(y)) then
      this%par(x) = y
      this%siz(y) = this%siz(y)+this%siz(x)
    else
      this%par(y) = x
      this%siz(x) = this%siz(x)+this%siz(y)
      if (this%rnk(x) == this%rnk(y)) this%rnk(x) = this%rnk(x)+1
    end if
  end
  integer function size_of(this,i)
    class(union_find), intent(inout) :: this
    integer, intent(in) :: i
    size_of = this%siz(find(this,i))
  end
end module mod_union_find
program decayed_bridges
  use mod_union_find
  implicit none
  integer :: n, m, a(100000) = 0, b(100000) = 0, i
  integer(8) :: x(0:100000) = 0
  type(union_find) :: uf
  read(*,*) n, m
  do i = 1, m
    read(*,*) a(i), b(i)
  end do
  uf = union_find(n)
  x(m) = int(n,8)*(n-1)/2
  do i = m, 1, -1
    x(i-1) = x(i)
    if (.not.uf%same(a(i),b(i))) then
      x(i-1) = x(i-1)-int(uf%size(a(i)),8)*uf%size(b(i))
      call uf%unite(a(i),b(i))
    end if
  end do
  do i = 1, m
    write(*,'(i0)') x(i)
  end do
end program decayed_bridges

Score : 400 points

Problem Statement

There are N islands and M bridges.

The i-th bridge connects the A_i-th and B_i-th islands bidirectionally.

Initially, we can travel between any two islands using some of these bridges.

However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the M-th bridge.

Let the inconvenience be the number of pairs of islands (a, b) (a < b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining.

For each i (1 \leq i \leq M), find the inconvenience just after the i-th bridge collapses.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq A_i < B_i \leq N

All pairs (A_i, B_i) are distinct.

The inconvenience is initially 0.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
\vdots
A_M B_M

Output

In the order i = 1, 2, ..., M, print the inconvenience just after the i-th bridge collapses.
Note that the answer may not fit into a 32-bit integer type.

Sample Input 1

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

Sample Output 1

0
0
4
5
6

For example, when the first to third bridges have collapsed, the inconvenience is 4 since we can no longer travel between the pairs (1, 2), (1, 3), (2, 4) and (3, 4).

Sample Input 2

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

Sample Output 2

8
9
12
14
15

Sample Input 3

2 1
1 2

Sample Output 3

1


module uf
  implicit none
  integer(8) :: unionfind(100000) = -1

contains

  recursive function root(i) result(res)
    integer(8),intent(in) :: i
    integer(8) res

    if (unionfind(i) < 0) then
       res = i
    else
       unionfind(i) = root(unionfind(i))
       res = unionfind(i)
    end if
  end function root

  subroutine unite(a, b)
    integer(8),intent(in) :: a,b

    if (unionfind(a) < unionfind(b)) then
       unionfind(a) = unionfind(a) + unionfind(b)
       unionfind(b) = a
    else
       unionfind(b) = unionfind(b) + unionfind(a)
       unionfind(a) = b
    end if
  end subroutine unite

end module uf

program abc120d
  use uf
  implicit none
  integer(8) n, m, i, ra, rb
  integer(8),dimension(:),allocatable :: a, b, ans

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

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

  ans(m) = n*(n-1)/2

  do i = m,2,-1
     ans(i-1) = ans(i)
     ra = root(a(i))
     rb = root(b(i))
     if (ra /= rb) then
        ans(i-1) = ans(i-1)-unionfind(ra)*unionfind(rb)
        call unite(ra,rb)
     end if
  end do

  do i = 1,m
     print *, ans(i)
  end do

end program abc120d


module UF
	implicit none
	private
	type Union_Find
		integer, allocatable :: par(:), rank(:), counter(:)
	end type Union_Find
	public Union_Find, make_Union_Find, unite, UF_size, root,  same

	contains

	subroutine make_Union_Find(UFty, N)
		integer, intent(in)           :: N
		type(Union_Find), intent(out) :: UFty
		allocate(UFty%par(1:N), UFty%rank(1:N), UFty%counter(1:N))
		call format_Union_Find(UFty, N)
	end subroutine make_Union_Find

	subroutine format_Union_Find(UFty, N)
		integer, intent(in)             :: N
		type(Union_Find), intent(inout) :: UFty
		integer :: i
		do i = 1, N
			UFty%par(i) = i
		end do
		UFty%rank    = 0
		UFty%counter = 1
	end subroutine format_Union_Find

	subroutine unite(UFty, x, y)
		integer, value                  :: x, y
		type(Union_Find), intent(inout) :: UFty
		x = root(UFty, x)
		y = root(UFty, y)
		if(x == y) return
		if(UFty%rank(x) < UFty%rank(y)) then
			UFty%par(x)     = y
			UFty%counter(y) = UFty%counter(y) + UFty%counter(x)
		else
			UFty%par(y)     = x
			UFty%counter(x) = UFty%counter(x) + UFty%counter(y)
			if(UFty%rank(x) == UFty%rank(y)) UFty%rank(x) = UFty%rank(x) + 1
		end if
	end subroutine unite

	function UF_size(UFty, x) result(y)
		integer, intent(in)             :: x
		type(Union_Find), intent(inout) :: UFty
		integer :: y
		y = UFty%counter(root(UFty, x))
		return
	end function UF_size

	recursive function root(UFty, x) result(root_ans)
		integer, intent(in)             :: x
		type(Union_Find), intent(inout) :: UFty
		integer :: root_ans
		if(UFty%par(x) == x) then
			root_ans = x
			return
		else
			UFty%par(x) = root(UFty, UFty%par(x))
			root_ans    = UFty%par(x)
			return
		end if
	end function root

	logical function same(UFty, x, y)
		integer, intent(in) :: x, y
		type(Union_Find), intent(inout) :: UFty
		if(root(UFty, x) == root(UFty, y)) then
			same = .TRUE.
		else
			same = .FALSE.
		end if
	end function same
end module UF

program main
	use UF
	implicit none
	integer :: N, M, i
	integer, allocatable :: A(:), B(:)
	integer(8), allocatable :: ans(:)
	type(Union_Find) :: Island
	read(*, *) N, M
	allocate(A(1:M), B(1:M))
	allocate(ans(1:M))
	do i = 1, M
		read(*, *) A(i), B(i)
	end do

	call make_Union_Find(Island, N)

	ans(M) = (N*(N-1)) / 2
	do i = M, 2, -1
		if(same(Island, A(i), B(i))) then
			ans(i-1) = ans(i)
		else
			ans(i-1) = ans(i) - UF_size(Island, A(i))*UF_size(Island, B(i))
		end if
		call unite(Island, A(i), B(i))
	end do
	do i = 1, M
		write(*, *) ans(i)
	end do
end program main


module m1
  implicit none
  integer(8) :: u(100000) = -1
contains
  recursive integer(8) function root(i) result(res)
    integer(8), intent(in) :: i
    if (u(i) < 0) then
       res = i
    else
       u(i) = root(u(i))
       res = u(i)
    end if
  end function root
end module m1
program abc120d
  use m1
  implicit none
  integer(8) n, m, i, ra, rb
  integer(8), dimension(:), allocatable :: a, b, ans
  read *, n, m
  allocate(a(m), b(m), ans(m))
  do i = 1, m
     read *, a(i), b(i)
  end do
  ans(m) = n * (n - 1) / 2
  do i = m, 2, -1
     ans(i - 1) = ans(i)
     ra = root(a(i))
     rb = root(b(i))
     if (ra /= rb) then
        ans(i - 1) = ans(i - 1) - u(ra) * u(rb)
        if (u(ra) < u(rb)) then
           u(ra) = u(ra) + u(rb)
           u(rb) = ra
        else
           u(rb) = u(rb) + u(ra)
           u(ra) = rb
        end if
     end if
  end do
  do i = 1, m
     print '(i0)', ans(i)
  end do
end program abc120d


character(10) :: s
integer :: y,m,d

read*,s

read(s(1:4),*),y
read(s(2:3),*),m
read(s(2:3),*),d

if( y<2019 ) goto 200
if( y>2020 ) goto 100
if( m<3 ) goto 200
if( m>5 ) goto 100
if( d<31 ) goto 100

100 continue
print*,'TBD'
stop
200 continue
print*,'Heisei'
stop
end

Score : 100 points

Problem Statement

You are given a string S as input. This represents a valid date in the year 2019 in the yyyy/mm/dd format. (For example, April 30, 2019 is represented as 2019/04/30.)

Write a program that prints Heisei if the date represented by S is not later than April 30, 2019, and prints TBD otherwise.

Constraints

S is a string that represents a valid date in the year 2019 in the yyyy/mm/dd format.

Input

Input is given from Standard Input in the following format:

S

Output

Print Heisei if the date represented by S is not later than April 30, 2019, and print TBD otherwise.

Sample Input 1

2019/04/30

Sample Output 1

Heisei

Sample Input 2

2019/11/01

Sample Output 2

TBD


program main
   implicit none
   integer(8)::n,i
   real(8)::su,u
   character(3) a
   read(*,*)n
   su=0
   do i=1,n
      read(*,*)u,a
      if(a=="JPY")then
         su=su+u
      else
         su=su+u*380000.0
      endif
   enddo
   write(*,*)su
end program main

Score : 200 points

Problem Statement

Takahashi received otoshidama (New Year's money gifts) from N of his relatives.

You are given N values x_1, x_2, ..., x_N and N strings u_1, u_2, ..., u_N as input. Each string u_i is either JPY or BTC, and x_i and u_i represent the content of the otoshidama from the i-th relative.

For example, if x_1 = 10000 and u_1 = JPY, the otoshidama from the first relative is 10000 Japanese yen; if x_2 = 0.10000000 and u_2 = BTC, the otoshidama from the second relative is 0.1 bitcoins.

If we convert the bitcoins into yen at the rate of 380000.0 JPY per 1.0 BTC, how much are the gifts worth in total?

Constraints

2 \leq N \leq 10

u_i = JPY or BTC.

If u_i = JPY, x_i is an integer such that 1 \leq x_i \leq 10^8.

If u_i = BTC, x_i is a decimal with 8 decimal digits, such that 0.00000001 \leq x_i \leq 100.00000000.

Input

Input is given from Standard Input in the following format:

N
x_1 u_1
x_2 u_2
:
x_N u_N

Output

If the gifts are worth Y yen in total, print the value Y (not necessarily an integer).

Output will be judged correct when the absolute or relative error from the judge's output is at most 10^{-5}.

Sample Input 1

2
10000 JPY
0.10000000 BTC

Sample Output 1

48000.0

The otoshidama from the first relative is 10000 yen. The otoshidama from the second relative is 0.1 bitcoins, which is worth 38000.0 yen if converted at the rate of 380000.0 JPY per 1.0 BTC. The sum of these is 48000.0 yen.

Outputs such as 48000 and 48000.1 will also be judged correct.

Sample Input 2

3
100000000 JPY
100.00000000 BTC
0.00000001 BTC

Sample Output 2

138000000.0038

In this case, outputs such as 138001000 and 1.38e8 will also be judged correct.


program kadomatsu
  implicit none
  integer :: n,a,b,c,i,j,k,count,distro
  integer :: bar(4)
  integer, allocatable :: l(:)
  integer, allocatable :: mp(:)

  read *, n,a,b,c
  allocate(l(n))
  do i = 1, n
    read*, l(i)
  end do
  allocate(mp(4**n))
    do j = 1, 4**n!pattern of junction
      bar = 0
      mp(j) = 0
      count = j
      do k = n,1,-1!number of bamboo
        distro = count/4**(k-1) + 1
        bar(distro) = bar(distro) + l(k)
        if (distro /= 4) mp(j) = mp(j) + 10
        count = mod(count,4**(k-1))
      end do
      if (bar(1)*bar(2)*bar(3) == 0) then
        mp(j) = 10000
      else
        mp(j) = mp(j) + abs(a-bar(1)) + abs(b-bar(2)) + abs (c-bar(3)) - 30
      end if
    end do
  print '(i0)', minval(mp)
  stop
end program kadomatsu

Score : 300 points

Problem Statement

You have N bamboos. The lengths (in centimeters) of these are l_1, l_2, ..., l_N, respectively.

Your objective is to use some of these bamboos (possibly all) to obtain three bamboos of length A, B, C. For that, you can use the following three kinds of magics any number:

Extension Magic: Consumes 1 MP (magic point). Choose one bamboo and increase its length by 1.

Shortening Magic: Consumes 1 MP. Choose one bamboo of length at least 2 and decrease its length by 1.

Composition Magic: Consumes 10 MP. Choose two bamboos and combine them into one bamboo. The length of this new bamboo is equal to the sum of the lengths of the two bamboos combined. (Afterwards, further magics can be used on this bamboo.)

At least how much MP is needed to achieve the objective?

Constraints

3 \leq N \leq 8

1 \leq C < B < A \leq 1000

1 \leq l_i \leq 1000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N A B C
l_1
l_2
:
l_N

Output

Print the minimum amount of MP needed to achieve the objective.

Sample Input 1

5 100 90 80
98
40
30
21
80

Sample Output 1

23

We are obtaining three bamboos of lengths 100, 90, 80 from five bamboos 98, 40, 30, 21, 80. We already have a bamboo of length 80, and we can obtain bamboos of lengths 100, 90 by using the magics as follows at the total cost of 23 MP, which is optimal.

Use Extension Magic twice on the bamboo of length 98 to obtain a bamboo of length 100. (MP consumed: 2)

Use Composition Magic on the bamboos of lengths 40, 30 to obtain a bamboo of length 70. (MP consumed: 10)

Use Shortening Magic once on the bamboo of length 21 to obtain a bamboo of length 20. (MP consumed: 1)

Use Composition Magic on the bamboo of length 70 obtained in step 2 and the bamboo of length 20 obtained in step 3 to obtain a bamboo of length 90. (MP consumed: 10)

Sample Input 2

8 100 90 80
100
100
90
90
90
80
80
80

Sample Output 2

0

If we already have all bamboos of the desired lengths, the amount of MP needed is 0. As seen here, we do not necessarily need to use all the bamboos.

Sample Input 3

8 1000 800 100
300
333
400
444
500
555
600
666

Sample Output 3

243


program main
implicit none
integer :: n, a, b, c
integer , allocatable :: l(:),l2(:)
integer :: i, j ,k
integer :: mp
integer :: a_check, n_i, n_j
integer :: a_diff, a_state

read(*,*) n,a,b,c
allocate(l(n))
allocate(l2(n*(n+1)/2))
do i = 1, n
   read(*,*) l(i)
end do
call bubble(n,l)

mp = 0
k = 0
a_check = 1000
do i = 1, n-1
   do j = i+1, n
      k = k+1
      l2(k)= l(i)+l(j)
      if ( a_check .gt. abs(a-l2(k))) then
         a_check = abs(a - l2(k))
         n_i = i
         n_j = j
      end if
   end do
end do
do i = 1, n-1
   if (l(i).le.a .and. l(i+1) .ge. a) then
      if ( abs(a-l(i)).lt.abs(a-l(i+1)))then
         a_diff = abs(a-l(i))
      else
         a_diff = abs(a-l(i+1))
      end if
      if(a_check+10 .lt. a_diff)then
         mp = mp + a_check +10
         a_state = 1
      else
         mp = mp + a_diff
         a_state = 0
      end if
   end if
end do
write(*,'(i0)') mp

stop
contains
  subroutine bubble(n,array)
    implicit none
    integer , intent(in) :: n
    integer , intent(inout) :: array(n)
    integer :: i, j
    integer :: t

    do i = 1, n-1
       do j = i+1 , n
          if(array(i).gt.array(j))then
             t = array(i)
             array(i) = array(j)
             array(j) = t
          end if
       end do
    end do
  end subroutine bubble

end program main


program main
  implicit none
  integer :: A,B,Q
  integer(8),allocatable :: S(:)
  integer(8),allocatable :: T(:)
  integer(8) :: xtmp
  integer(8) :: xtmp_loc(1)
  integer(8) :: cand(2)
  integer(8),allocatable :: x(:)
  integer(8),allocatable :: sorted(:)
  integer :: i,j
  integer :: is,ie
  read(*,*) A,B,Q
  allocate(S(A),T(B),x(Q))
  do i = 1,A
     read(*,*) S(i)
  end do
  do i = 1,B
     read(*,*) T(i)
  end do
  do i = 1,Q
     read(*,*) X(i)
  end do

  do i = 1,Q
     ! first visit T
     cand(1)=0
     xtmp=X(i)
     xtmp_loc = T( minloc( abs(T(:)-xtmp)) )
     cand(1) = cand(1) + abs(xtmp - xtmp_loc(1))
     xtmp = xtmp_loc(1)
     cand(1) = cand(1) + minval( abs(S(:)-xtmp))

     ! first visit S
     cand(2)=0
     xtmp=X(i)
     xtmp_loc = S( minloc( abs(S(:)-xtmp)) )
     cand(2) = cand(2) + abs(xtmp - xtmp_loc(1))
     xtmp = xtmp_loc(1)
     cand(2) = cand(2) + minval( abs(T(:)-xtmp))
     write(*,'(i0)') minval(cand)
  end do
  stop
contains
  subroutine near(is,ie,B,T,xtmp)
    integer,intent(out) :: is,ie
    integer(8) :: xtmp
    integer,intent(in)  :: B
    integer(8)  :: T(B)
    is = 1 ; ie = B
    if ( B/2 /= 1 ) then
       if (T(B/2) < xtmp ) then
          is = B/2-1 ; ie = B
       else
          is = 1 ; ie = B/2
       end if
    else
       return
    end if
!@     if ( T(B/8*7) < xtmp  ) then
!@        is = B/8*7 ; ie =B
!@     elseif( T(B/4*3)<xtmp .and. xtmp <= T(B/8*7) ) then
!@        is = B/4*3 ; ie =B/8*7
!@     elseif( T(B/2) < xtmp .and. xtmp <= T(B/4*3) ) then
!@        is = B/2 ; ie = B/4*3
!@     elseif(   xtmp <= T(B/4)) then
!@        is = 1; ie =B/4
!@     elseif(   xtmp <= T(B/2) .and. T(B/4) < xtmp)then
!@        is = B/4 ; ie =B/2
!@     end if
    return
  end subroutine near
end program main

Score : 400 points

Problem Statement

Along a road running in an east-west direction, there are A shrines and B temples.
The i-th shrine from the west is located at a distance of s_i meters from the west end of the road, and the i-th temple from the west is located at a distance of t_i meters from the west end of the road.

Answer the following Q queries:

Query i (1 \leq i \leq Q): If we start from a point at a distance of x_i meters from the west end of the road and freely travel along the road, what is the minimum distance that needs to be traveled in order to visit one shrine and one temple? (It is allowed to pass by more shrines and temples than required.)

Constraints

1 \leq A, B \leq 10^5

1 \leq Q \leq 10^5

1 \leq s_1 < s_2 < ... < s_A \leq 10^{10}

1 \leq t_1 < t_2 < ... < t_B \leq 10^{10}

1 \leq x_i \leq 10^{10}

s_1, ..., s_A, t_1, ..., t_B, x_1, ..., x_Q are all different.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B Q
s_1
:
s_A
t_1
:
t_B
x_1
:
x_Q

Output

Print Q lines. The i-th line should contain the answer to the i-th query.

Sample Input 1

2 3 4
100
600
400
900
1000
150
2000
899
799

Sample Output 1

350
1400
301
399

There are two shrines and three temples. The shrines are located at distances of 100, 600 meters from the west end of the road, and the temples are located at distances of 400, 900, 1000 meters from the west end of the road.

Query 1: If we start from a point at a distance of 150 meters from the west end of the road, the optimal move is first to walk 50 meters west to visit a shrine, then to walk 300 meters east to visit a temple.

Query 2: If we start from a point at a distance of 2000 meters from the west end of the road, the optimal move is first to walk 1000 meters west to visit a temple, then to walk 400 meters west to visit a shrine. We will pass by another temple on the way, but it is fine.

Query 3: If we start from a point at a distance of 899 meters from the west end of the road, the optimal move is first to walk 1 meter east to visit a temple, then to walk 300 meters west to visit a shrine.

Query 4: If we start from a point at a distance of 799 meters from the west end of the road, the optimal move is first to walk 199 meters west to visit a shrine, then to walk 200 meters west to visit a temple.

Sample Input 2

1 1 3
1
10000000000
2
9999999999
5000000000

Sample Output 2

10000000000
10000000000
14999999998

The road is quite long, and we may need to travel a distance that does not fit into a 32-bit integer.


program main
  implicit none
  integer :: A,B,Q
  integer(8),allocatable :: S(:)
  integer(8),allocatable :: T(:)
  integer(8) :: xtmp
  integer(8) :: xtmp_loc(1)
  integer(8) :: cand(2)
  integer(8),allocatable :: x(:)
  integer :: i

  read(*,*) A,B,Q
  allocate(S(A),T(B),x(Q))
  do i = 1,A
     read(*,*) S(i)
  end do
  do i = 1,B
     read(*,*) T(i)
  end do
  do i = 1,Q
     read(*,*) X(i)
  end do

  do i = 1,Q
     ! first visit T
     cand(1)=0
     xtmp=X(i)
     cand(1) = cand(1) + minval( abs(T(:)-xtmp))
     xtmp_loc = T( minloc( abs(T(:)-xtmp)) )
     xtmp = xtmp_loc(1)
     cand(1) = cand(1) + minval( abs(S(:)-xtmp))

     ! first visit S
     cand(2)=0
     xtmp=X(i)
     cand(2) = cand(2) + minval( abs(S(:)-xtmp))
     xtmp_loc = S( minloc( abs(S(:)-xtmp)) )
     xtmp = xtmp_loc(1)
     cand(2) = cand(2) + minval( abs(T(:)-xtmp))
     write(*,'(i0)') minval(cand)
  end do

  stop
end program main


program bbb
implicit none

integer :: n,m,i,j,res
integer,dimension(30) :: a, b
integer,allocatable, dimension(:) :: k

read*, n,m
allocate(k(n))

res=0
b=0

do i=1,n
a=0
read*, k(i), a(1:k(i))
 do j=1,k(i)
 b(a(j))=b(a(j))+1
 end do
end do

do i=1,m
if(b(i)==n) res=res+1
end do
write(*,'(i0)') res

end program

Score : 200 points

Problem Statement

Katsusando loves omelette rice.

Besides, he loves crème brûlée, tenderloin steak and so on, and believes that these foods are all loved by everyone.

To prove that hypothesis, he conducted a survey on M kinds of foods and asked N people whether they like these foods or not.

The i-th person answered that he/she only likes the A_{i1}-th, A_{i2}-th, ..., A_{iK_i}-th food.

Find the number of the foods liked by all the N people.

Constraints

All values in input are integers.

1 \leq N, M \leq 30

1 \leq K_i \leq M

1 \leq A_{ij} \leq M

For each i (1 \leq i \leq N), A_{i1}, A_{i2}, ..., A_{iK_i} are distinct.

Constraints

Input is given from Standard Input in the following format:

N M
K_1 A_{11} A_{12} ... A_{1K_1}
K_2 A_{21} A_{22} ... A_{2K_2}
:
K_N A_{N1} A_{N2} ... A_{NK_N}

Output

Print the number of the foods liked by all the N people.

Sample Input 1

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

Sample Output 1

1

As only the third food is liked by all the three people, 1 should be printed.

Sample Input 2

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

Sample Output 2

0

Katsusando's hypothesis turned out to be wrong.

Sample Input 3

1 30
3 5 10 30

Sample Output 3

3


program atcoder
    implicit none
    integer(8) :: i,j,n,m,ans=0,k
    integer(8),allocatable,dimension(:) :: a,sum
    read*,n,m
    allocate(a(m))
    allocate(sum(m))
    sum = 0
    do i=1,n
        a = 0
        read(*,*)k,(a(j),j=1,k)
        do j=1,k
            sum(a(j)) = sum(a(j)) + 1
        enddo
    enddo
    do i=1,m
        if(sum(i)==n)then
            ans = ans + 1
        endif
    enddo
    write(*,'(i0)')ans
    stop
contains
end program atcoder


program main
  implicit none
  integer :: N
  integer,allocatable :: A(:)
  integer :: Amin, i_loop,Amin_bf
  integer :: i,j,k
  read(*,*) N
  allocate( A(N) )
  read(*,*) A
  Amin = minval(A)
  where ( mod(A,Amin) == 0 )   A = Amin
  A = mod(A,Amin)
  ! calc mod
  do
     where(A > Amin ) A = A-Amin
     where ( mod(A,Amin) == 0 )   A = Amin
     if ( any(A < Amin ) ) then
        Amin = minval(A,A>0)
     end if
     if ( count(A > 0 ) == 1 .or. all( A == Amin ) ) exit
  end do
  write(*,'(i0)') minval(A,A >0 )
  deallocate(A)
  stop
contains
  recursive function iqsort(ix) result(ires)
        integer, allocatable :: ires(:)
        integer, intent(in)  :: ix(:)
        integer :: k
        if (size(ix) <= 1) then
          ires = ix
        else
          k = ix(size(ix) / 2)
          ires = [iqsort(pack(ix, ix < k)), pack(ix, ix == k), iqsort(pack(ix, ix > k))]
        end if
      end function iqsort
end program main

Score : 300 points

Problem Statement

There are N monsters, numbered 1, 2, ..., N.

Initially, the health of Monster i is A_i.

Below, a monster with at least 1 health is called alive.

Until there is only one alive monster, the following is repeated:

A random alive monster attacks another random alive monster.

As a result, the health of the monster attacked is reduced by the amount equal to the current health of the monster attacking.

Find the minimum possible final health of the last monster alive.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the minimum possible final health of the last monster alive.

Sample Input 1

4
2 10 8 40

Sample Output 1

2

When only the first monster keeps on attacking, the final health of the last monster will be 2, which is minimum.

Sample Input 2

4
5 13 8 1000000000

Sample Output 2

1

Sample Input 3

3
1000000000 1000000000 1000000000

Sample Output 3

1000000000


program main
  implicit none
  integer :: N
  integer,allocatable :: A(:)
  integer,allocatable :: A2(:)
  integer :: Amin, i_loop,Amin_bf
  integer :: i,j,k
  read(*,*) N
  allocate(A(N),A2(N))
  read(*,*) A
  A2 = A
  Amin = minval(A)
  where ( mod(A,Amin) == 0 )   A = Amin
  ! calc mod
  do
     where(A > Amin ) A = A-Amin
     where ( mod(A,Amin) == 0 )   A = Amin
     if ( any(A < Amin ) ) then
        Amin = minval(A,A>0)
     end if
     if ( count(A > 0 ) == 1 .or. all( A == A(1)) ) exit
  end do
  write(*,'(i0)') minval(A,A >0 )
  deallocate(A)
  deallocate(A2)
  stop
contains
  recursive function iqsort(ix) result(ires)
        integer, allocatable :: ires(:)
        integer, intent(in)  :: ix(:)
        integer :: k
        if (size(ix) <= 1) then
          ires = ix
        else
          k = ix(size(ix) / 2)
          ires = [iqsort(pack(ix, ix < k)), pack(ix, ix == k), iqsort(pack(ix, ix > k))]
        end if
      end function iqsort
end program main


program main
  implicit none
  integer :: N
  integer,allocatable :: A(:)
  integer,allocatable :: A2(:)
  integer :: Amin, i_loop,Amin_bf
  integer :: i,j,k
  read(*,*) N
  allocate(A(N),A2(N))
  read(*,*) A
  A2 = A
  Amin = minval(A)
  ! calc mod
  do
     where(A > Amin ) A = A-Amin
     if ( any(A < Amin ) ) then
        Amin_bf = Amin_bf
        Amin = minval(A,A>0)
     end if
     if ( count(A > 0 ) == 1 .or. all( A == A(1)) ) exit
  end do
  write(*,'(i0)') minval(A,A >0 )
  deallocate(A)
  deallocate(A2)
  stop
contains
  recursive function iqsort(ix) result(ires)
        integer, allocatable :: ires(:)
        integer, intent(in)  :: ix(:)
        integer :: k
        if (size(ix) <= 1) then
          ires = ix
        else
          k = ix(size(ix) / 2)
          ires = [iqsort(pack(ix, ix < k)), pack(ix, ix == k), iqsort(pack(ix, ix > k))]
        end if
      end function iqsort
end program main


PROGRAM ATCODER

implicit none
integer :: n, i, check
integer, allocatable :: a(:), m(:)

read(*,*) n
allocate (a(n), m(n-1))
read(*,*) (a(i), i=1,n)

call hs(n,a)

check = 0
do i = 1, n-1
	m(i) = mod(a(n),a(i))
    if(m(i) /= 0) then
    	check = 1
    end if
end do

if(check == 1)then
	write(*,'(i0)') 1
else
	write(*,'(i0)') a(1)
end if

END PROGRAM ATCODER

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

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

  if(n <= 0)then
     write(6,*)"Error, at heapsort"; stop
  endif
  if(n == 1)return

  l=n/2+1
  k=n
  do while(k /= 1)
     if(l > 1)then
        l=l-1
        t=array(L)
     else
        t=array(k)
        array(k)=array(1)
        k=k-1
        if(k == 1) then
           array(1)=t
           exit
        endif
     endif
     i=l
     j=l+l
     do while(j <= k)
        if(j < k)then
           if(array(j) < array(j+1))j=j+1
        endif
        if (t < array(j))then
           array(i)=array(j)
           i=j
           j=j+j
        else
           j=k+1
        endif
     enddo
     array(i)=t
  enddo

  return
end subroutine hs


program atcoder
    implicit none
    integer(8) :: i,j,n,m,ans=0,k,mincost=-1,use2max=-1
    integer(8),allocatable,dimension(:) :: a,canuse,cost
    character(len=10000),allocatable,dimension(:) :: dp,x
    read*,n,m
    allocate(a(m))
    allocate(canuse(9))
    allocate(cost(9))
    allocate(dp(0:n))
    dp = "-"
    x = (/"1","2","3","4","5","6","7","8","9"/);
    cost = (/2,5,5,4,5,6,3,7,6/)
    read(*,*)(a(j),j=1,m)
    dp(0) = ""
    do i=0,N
        if(trim(dp(i))=="-")then
            cycle
        endif
        do j=1,m
            if(i+cost(a(j)) > N )then
                cycle
            endif
           dp(i+cost(a(j))) = maxst( dp(i+cost(a(j))) , trim(dp(i))//x(a(j)) )
        enddo
    enddo
    write(*,'(a)')trim(dp(n))
    stop
contains
function maxst(x,y)
    character maxst*10000,x*10000,y*10000
    if(x=="-")then
        maxst = y
    else if(y=="-")then
        maxst = x
    else if(len(trim(x))>len(trim(y)))then
        maxst = x
    else if(len(trim(x))<len(trim(y)))then
        maxst = y
    else if(x>=y)then
        maxst = x
    else
        maxst = y
    endif
end function maxst
end program atcoder

Score : 400 points

Problem Statement

Find the largest integer that can be formed with exactly N matchsticks, under the following conditions:

Every digit in the integer must be one of the digits A_1, A_2, ..., A_M (1 \leq A_i \leq 9).

The number of matchsticks used to form digits 1, 2, 3, 4, 5, 6, 7, 8, 9 should be 2, 5, 5, 4, 5, 6, 3, 7, 6, respectively.

Constraints

All values in input are integers.

2 \leq N \leq 10^4

1 \leq M \leq 9

1 \leq A_i \leq 9

A_i are all different.

There exists an integer that can be formed by exactly N matchsticks under the conditions.

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 ... A_M

Output

Print the largest integer that can be formed with exactly N matchsticks under the conditions in the problem statement.

Sample Input 1

20 4
3 7 8 4

Sample Output 1

777773

The integer 777773 can be formed with 3 + 3 + 3 + 3 + 3 + 5 = 20 matchsticks, and this is the largest integer that can be formed by 20 matchsticks under the conditions.

Sample Input 2

101 9
9 8 7 6 5 4 3 2 1

Sample Output 2

71111111111111111111111111111111111111111111111111

The output may not fit into a 64-bit integer type.

Sample Input 3

15 3
5 4 6

Sample Output 3

654


integer town(4)
integer a,b
town=0
do i=1,3
  read*,a,b
  town(a)=town(a)+1
  town(b)=town(b)+1
end do
if(maxval(town)==3 )then
  print"(A)","NO"
else
  print"(A)","YES"
endif
end

Score : 200 points

Problem Statement

There are four towns, numbered 1,2,3 and 4.
Also, there are three roads. The i-th road connects different towns a_i and b_i bidirectionally.
No two roads connect the same pair of towns. Other than these roads, there is no way to travel between these towns, but any town can be reached from any other town using these roads.

Determine if we can visit all the towns by traversing each of the roads exactly once.

Constraints

1 \leq a_i,b_i \leq 4(1\leq i\leq 3)

a_i and b_i are different. (1\leq i\leq 3)

No two roads connect the same pair of towns.

Any town can be reached from any other town using the roads.

Input

Input is given from Standard Input in the following format:

a_1 b_1
a_2 b_2
a_3 b_3

Output

If we can visit all the towns by traversing each of the roads exactly once, print YES; otherwise, print NO.

Sample Input 1

4 2
1 3
2 3

Sample Output 1

YES

We can visit all the towns in the order 1,3,2,4.

Sample Input 2

3 2
2 4
1 2

Sample Output 2

NO

Sample Input 3

2 1
3 2
4 3

Sample Output 3

YES


program name
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: k,a,b,ans

    read*, k,a,b
    ans=a + ((k-(a-1))/2)*(b-a)
    if (mod(k-(a-1),2_8) == 1) ans=ans+1
    print'(i0)', max(k+1,ans)
end program name

Score : 400 points

Problem Statement

Snuke has one biscuit and zero Japanese yen (the currency) in his pocket.
He will perform the following operations exactly K times in total, in the order he likes:

Hit his pocket, which magically increases the number of biscuits by one.

Exchange A biscuits to 1 yen.

Exchange 1 yen to B biscuits.

Find the maximum possible number of biscuits in Snuke's pocket after K operations.

Constraints

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

K,A and B are integers.

Input

Input is given from Standard Input in the following format:

K A B

Output

Print the maximum possible number of biscuits in Snuke's pocket after K operations.

Sample Input 1

4 2 6

Sample Output 1

7

The number of biscuits in Snuke's pocket after K operations is maximized as follows:

Hit his pocket. Now he has 2 biscuits and 0 yen.

Exchange 2 biscuits to 1 yen. his pocket. Now he has 0 biscuits and 1 yen.

Hit his pocket. Now he has 1 biscuits and 1 yen.

Exchange 1 yen to 6 biscuits. his pocket. Now he has 7 biscuits and 0 yen.

Sample Input 2

7 3 4

Sample Output 2

8

Sample Input 3

314159265 35897932 384626433

Sample Output 3

48518828981938099


Score : 100 points

Problem Statement

In order to pass the entrance examination tomorrow, Taro has to study for T more hours.

Fortunately, he can leap to World B where time passes X times as fast as it does in our world (World A).

While (X \times t) hours pass in World B, t hours pass in World A.

How many hours will pass in World A while Taro studies for T hours in World B?

Constraints

All values in input are integers.

1 \leq T \leq 100

1 \leq X \leq 100

Input

Input is given from Standard Input in the following format:

T X

Output

Print the number of hours that will pass in World A.

The output will be regarded as correct when its absolute or relative error from the judge's output is at most 10^{-3}.

Sample Input 1

8 3

Sample Output 1

2.6666666667

While Taro studies for eight hours in World B where time passes three times as fast, 2.6666... hours will pass in World A.

Note that an absolute or relative error of at most 10^{-3} is allowed.

Sample Input 2

99 1

Sample Output 2

99.0000000000

Sample Input 3

1 100

Sample Output 3

0.0100000000


program shiken
  implicit none
  real(8) :: T,X,Y

  read(*,*) T,X

  Y=T*1.0/X
  write(*,*) Y

end program shiken


INTEGER N
integer,allocatable,dimension(:)::x
READ*,N
allocate(x(n))
read*,x
PRINT"(A)",MERGE("Yes"," No",Maxval(x)*2<Sum(x))
end

Score : 200 points

Problem Statement

Determine if an N-sided polygon (not necessarily convex) with sides of length L_1, L_2, ..., L_N can be drawn in a two-dimensional plane.

You can use the following theorem:

Theorem: an N-sided polygon satisfying the condition can be drawn if and only if the longest side is strictly shorter than the sum of the lengths of the other N-1 sides.

Constraints

All values in input are integers.

3 \leq N \leq 10

1 \leq L_i \leq 100

Input

Input is given from Standard Input in the following format:

N
L_1 L_2 ... L_N

Output

If an N-sided polygon satisfying the condition can be drawn, print Yes; otherwise, print No.

Sample Input 1

4
3 8 5 1

Sample Output 1

Yes

Since 8 < 9 = 3 + 5 + 1, it follows from the theorem that such a polygon can be drawn on a plane.

Sample Input 2

4
3 8 4 1

Sample Output 2

No

Since 8 \geq 8 = 3 + 4 + 1, it follows from the theorem that such a polygon cannot be drawn on a plane.

Sample Input 3

10
1 8 10 5 8 12 34 100 11 3

Sample Output 3

No


program max
implicit none
integer N
real L(100)
integer mx
integer i,j,x

read *, N
!データの読み込み
do i = 1,N
 read *, L(i)
end do
!最大値を探索
 mx=L(1)
do i =1,N
if(mx < L(i+1))then
 mx=L(i+1)
else if(mx > L(i+1))then
 mx=mx
endif
end do
!結果の書き出し

do i=0,N
if(mx==L(i)) cycle
x=L(i)+x
end do

if(mx<x)then
Write(*,*) 'Yes'
else
Write(*,*) 'No'
end if

end program max


program name
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: n,m,i
    integer(int32), allocatable:: x(:),dx(:)

    read*, n,m
    if (n >= m) then
        print'(i0)', 0
        stop
    end if
    allocate(x(m))
    read*, x(:)
    allocate(dx(m-1))
    call merge_sort(x,1,m)
    do i=1,m-1
        dx(i) = x(i+1)-x(i)
    end do
    call merge_sort(dx,1,m-1)
    print'(i0)', sum(dx(1:m-n))

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

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

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

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

    allocate(tmp(lst-fst+1))

    li=fst
    ri=mdl+1
    ti=1

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

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

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

subroutine swap(x,y)
    integer(int32),intent(inout):: x,y
    integer(int32):: tmp
    tmp = x
    x = y
    y = tmp
end subroutine
end program name

Score : 300 points

Problem Statement

We will play a one-player game using a number line and N pieces.

First, we place each of these pieces at some integer coordinate.

Here, multiple pieces can be placed at the same coordinate.

Our objective is to visit all of the M coordinates X_1, X_2, ..., X_M with these pieces, by repeating the following move:

Move: Choose a piece and let x be its coordinate. Put that piece at coordinate x+1 or x-1.

Note that the coordinates where we initially place the pieces are already regarded as visited.

Find the minimum number of moves required to achieve the objective.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

-10^5 \leq X_i \leq 10^5

X_1, X_2, ..., X_M are all different.

Input

Input is given from Standard Input in the following format:

N M
X_1 X_2 ... X_M

Output

Find the minimum number of moves required to achieve the objective.

Sample Input 1

2 5
10 12 1 2 14

Sample Output 1

5

The objective can be achieved in five moves as follows, and this is the minimum number of moves required.

Initially, put the two pieces at coordinates 1 and 10.

Move the piece at coordinate 1 to 2.

Move the piece at coordinate 10 to 11.

Move the piece at coordinate 11 to 12.

Move the piece at coordinate 12 to 13.

Move the piece at coordinate 13 to 14.

Sample Input 2

3 7
-10 -3 0 9 -100 2 17

Sample Output 2

19

Sample Input 3

100 1
-100000

Sample Output 3

0


integer n,m,a,ans
integer,allocatable,dimension(:)::x,dx(:)

read*,n, m
allocate(x(m),dx(m-1))
read*, (x(i), i=1,m)

if(n >= m)then
  print"(i0)",0
  stop
end if

call heapsort(m,x)

do i =1, m-1
  dx(i)=x(i+1) - x(i)
end do

call heapsort(m-1,dx)

ans = sum(dx)
if (n > 1)ans = ans - sum(dx(m-n+1:m-1))
print"(i0)",ans

contains

subroutine heapsort(n,array)
  implicit none
!ここの入力は状況に応じて変更すること
  integer,intent(in) :: n
  integer,intent(inout) :: array(1:n)
  integer::i,k,j,l
  integer:: t

  l=n/2+1
  k=n
  do while(k /= 1)
     if(l > 1)then
        l=l-1
        t=array(L)
     else
        t=array(k)
        array(k)=array(1)
        k=k-1
        if(k == 1) then
           array(1)=t
           exit
        endif
     endif
     i=l
     j=l+l
     do while(j<=k)
        if(j < k)then
           if(array(j) < array(j+1))j=j+1
        endif
        if (t < array(j))then
           array(i)=array(j)
           i=j
           j=j+j
        else
           j=k+1
        endif
     enddo
     array(i)=t
  enddo
  return
end subroutine heapsort
end


program stream
  integer(8) :: n,m, i, j, k, l, ans
  integer(8),allocatable :: x(:),y(:),z(:),w(:)

  read *, n, m
  ans = 0
  allocate(x(m),y(m),z(m-1),w(m-1))
  read *, x
  do i = 1, m
    y(i) = minval(x)
    x(minloc(x)) = 200000
  end do

  !print '(i0)', y

  do j = 1, m-1
    z(j) = y(j+1) - y(j)
  end do

  !print '(i0)', z

  do k = 1, m-1
    w(k) = minval(z)
    z(minloc(z)) = 300000
  end do

  !print '(i0)', w

  if (n >= m) then
    print '(i0)', 0
  else
    do l = 1, m-n
      ans = ans + w(l)
    end do
    print '(i0)', ans
  end if
  stop
end program


program main
implicit None
	integer(8)::n,p=0,i
	character(len =100)::a,b,c

	read*,n
	read*,a,b,c

	do i = 1,n
		if(a(i:i) == b(i:i))then
			if(a(i:i) /= c(i:i)) p = p+1
			else
				if(a(i:i) == c(i:i))then
					p = p+1
					else if(b(i:i) == c(i:i))then
					p = p+1
					else
					p = p+2
				endif
		endif
	enddo

	print "(i0)",p
end program main

Score : 200 points

Problem Statement

You are given three strings A, B and C. Each of these is a string of length N consisting of lowercase English letters.

Our objective is to make all these three strings equal. For that, you can repeatedly perform the following operation:

Operation: Choose one of the strings A, B and C, and specify an integer i between 1 and N (inclusive). Change the i-th character from the beginning of the chosen string to some other lowercase English letter.

What is the minimum number of operations required to achieve the objective?

Constraints

1 \leq N \leq 100

Each of the strings A, B and C is a string of length N.

Each character in each of the strings A, B and C is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

N
A
B
C

Output

Print the minimum number of operations required.

Sample Input 1

4
west
east
wait

Sample Output 1

3

In this sample, initially A = west、B = east、C = wait. We can achieve the objective in the minimum number of operations by performing three operations as follows:

Change the second character in A to a. A is now wast.

Change the first character in B to w. B is now wast.

Change the third character in C to s. C is now wast.

Sample Input 2

9
different
different
different

Sample Output 2

0

If A, B and C are already equal in the beginning, the number of operations required is 0.

Sample Input 3

7
zenkoku
touitsu
program

Sample Output 3

13


PROGRAM ATCODER

implicit none
integer(8) :: n,i,j,s,dif
integer(8),allocatable :: a(:), b(:), c(:)

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

dif = 0
do i = 1, n
    read(*,*) a(i),b(i)
end do

c(:) = a(:) + b(:)

dif = -sum(b)

write(*,*) c(:)
call hsort(n,c)
write(*,*) c(:)

do i = 1, n, 2
    dif = dif + c(i)
end do

deallocate(a) ; deallocate(b) ; deallocate(c)

write(*,'(i0)') dif

END PROGRAM ATCODER

!　以下サブルーチン
subroutine hsort(n, x)
  implicit none
  integer, intent(IN) :: n
  real(8), intent(INOUT) :: x(n)
  ! local
  integer k
  real(8) :: tmp
  ! external subroutine
  external mkheap

  do k = n/2, 1, -1
     tmp = x(k)
     call mkheap(n, x, k, n, tmp)
  enddo

  do k = n, 2, -1
     tmp = x(k)
     x(k) = x(1)
     call mkheap(n, x, 1, k-1, tmp)
  end do

  return
end subroutine hsort

subroutine mkheap(n, x, root, leaf, val)
  implicit none
  integer, intent(IN)    :: n, root, leaf
  real(8), intent(INOUT) :: x(1:n)
  real(8), intent(IN)    :: val
  ! local
  integer :: i, j

  i = root
  j = i * 2

  do
     if (j > leaf) exit

     if (j < leaf) then
        if (x(j) < x(j+1)) j = j + 1
     end if

     if (x(j) > val) then
        x(i) = x(j)
        i = j
        j = i * 2
     else
        j = leaf + 1
     endif
  end do

  x(i) = val

  return
end subroutine mkheap
!サブルーチン終了

Score : 400 points

Problem Statement

There are N dishes of cuisine placed in front of Takahashi and Aoki.
For convenience, we call these dishes Dish 1, Dish 2, ..., Dish N.

When Takahashi eats Dish i, he earns A_i points of happiness; when Aoki eats Dish i, she earns B_i points of happiness.

Starting from Takahashi, they alternately choose one dish and eat it, until there is no more dish to eat.
Here, both of them choose dishes so that the following value is maximized: "the sum of the happiness he/she will earn in the end" minus "the sum of the happiness the other person will earn in the end".

Find the value: "the sum of the happiness Takahashi earns in the end" minus "the sum of the happiness Aoki earns in the end".

Constraints

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

1 \leq B_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
:
A_N B_N

Output

Print the value: "the sum of the happiness Takahashi earns in the end" minus "the sum of the happiness Aoki earns in the end".

Sample Input 1

3
10 10
20 20
30 30

Sample Output 1

20

In this sample, both of them earns 10 points of happiness by eating Dish 1, 20 points by eating Dish 2, and 30 points by eating Dish 3.

In this case, since Takahashi and Aoki have the same "taste", each time they will choose the dish with which they can earn the greatest happiness. Thus, first Takahashi will choose Dish 3, then Aoki will choose Dish 2, and finally Takahashi will choose Dish 1, so the answer is (30 + 10) - 20 = 20.

Sample Input 2

3
20 10
20 20
20 30

Sample Output 2

20

In this sample, Takahashi earns 20 points of happiness by eating any one of the dishes 1, 2 and 3, but Aoki earns 10 points of happiness by eating Dish 1, 20 points by eating Dish 2, and 30 points by eating Dish 3.

In this case, since only Aoki has likes and dislikes, each time they will choose the dish with which Aoki can earn the greatest happiness. Thus, first Takahashi will choose Dish 3, then Aoki will choose Dish 2, and finally Takahashi will choose Dish 1, so the answer is (20 + 20) - 20 = 20.

Sample Input 3

6
1 1000000000
1 1000000000
1 1000000000
1 1000000000
1 1000000000
1 1000000000

Sample Output 3

-2999999997

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


PROGRAM ATCODER

implicit none
integer :: n,i,j,s
integer(8) :: dif
integer,allocatable :: a(:), b(:), c(:)

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

do i = 1, n
    read(*,*) a(i),b(i)
    c(i) = b(i) + a(i)
end do

do i = 1, n
    do j = i+1, n
        if (c(j) > c(i))then
            s = c(i)
            c(i) = c(j)
            c(j) = s
        end if
    end do
end do

dif = -(sum(b))
if (mod(n,2) == 0)then
    do i = 1, n-1, 2
        dif = dif + c(i)
    end do
else
    do i = 1, n, 2
        dif = dif + c(i)
    end do
end if

deallocate(a) ; deallocate(b) ; deallocate(c)

write(*,'(i0)') dif

END PROGRAM ATCODER


PROGRAM ATCODER

implicit none
integer(8) :: n,i,j,s,t,u,taka,ao
integer(8),allocatable :: a(:), b(:), c(:)

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

do i = 1, n
    read(*,*) a(i),b(i)
    c(i) = b(i) - a(i)
end do

do i = 1, n
    do j = i+1, n
        if (c(j) > c(i))then
            s = c(i) ; t = a(i) ; u = b(i)
            c(i) = c(j) ; a(i) = a(j) ; b(i) = b(j)
            c(j) = s ; a(j) = t ; b(j) = u
        end if
    end do
end do

taka = 0 ; ao = 0
do i = 1, n, 2
    j = i + 1
    if (i <= n)then
        taka = taka + a(i)
    end if
    if (j <= n)then
        ao = ao + b(j)
    end if
end do

print'(i0)', taka - ao

END PROGRAM ATCODER


program sample
    implicit none

    integer(8) :: i,j,m,n
    integer(8),allocatable :: x(:),y(:)

    read(*,*) n
    allocate(x(n))
    read(*,*)x
    m=x(1)
    do i=1,n-1
        if(x(i+1)>x(i))then
            m=m+x(i+1)-x(i)
        end if
    end do
    write(*,*)m
    stop
end program sample

Score : 300 points

Problem Statement

In a flower bed, there are N flowers, numbered 1,2,......,N. Initially, the heights of all flowers are 0.
You are given a sequence h=\{h_1,h_2,h_3,......\} as input. You would like to change the height of Flower k to h_k for all k (1 \leq k \leq N), by repeating the following "watering" operation:

Specify integers l and r. Increase the height of Flower x by 1 for all x such that l \leq x \leq r.

Find the minimum number of watering operations required to satisfy the condition.

Constraints

1 \leq N \leq 100

0 \leq h_i \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
h_1 h_2 h_3 ...... h_N

Output

Print the minimum number of watering operations required to satisfy the condition.

Sample Input 1

4
1 2 2 1

Sample Output 1

2

The minimum number of watering operations required is 2.
One way to achieve it is:

Perform the operation with (l,r)=(1,3).

Perform the operation with (l,r)=(2,4).

Sample Input 2

5
3 1 2 3 1

Sample Output 2

5

Sample Input 3

8
4 23 75 0 23 96 50 100

Sample Output 3

221


program garden
  implicit none

  integer :: n, min, max, i, cnt, j, k, t, s
  integer, allocatable :: h(:), h2(:)

  read(*,*) n
  allocate(h(n))
  allocate(h2(n))
  read(*,*) h

  min = minval(h)
  max = maxval(h)

  if (product(h) /= 0) then
     cnt = 1
  else
     cnt = 0
  end if

  do j = min, max
     do t = 1, n
        if(h(t) >= j) then
           h2(t) = h(t) - j
        else
           h2(t) = 0
        end if

     end do

     do i = 1, n
        if (h2(i) >= 1) then
           if(product(h2(i:n)) == 0) then
              do k = i, n
                 if(h2(k) == 0) then
                    cnt = cnt + 1
                    exit
                 end if
              end do
              do s = i, k
                 h2(s) = 0
              end do

           else
              cnt = cnt + 1
              exit
           end if
        end if
     end do

  end do

  write(*,*) cnt
end program garden