program main
  implicit none
  integer(8) :: X,Y,Z,K
  integer:: tot
  integer(8),allocatable :: A(:),B(:),C(:)
  integer(8),allocatable :: ABC(:)
  integer(8),allocatable :: ABC2(:)
  integer :: i1,i2,i3,i
  read(*,*) X,Y,Z,K
  allocate(A(X),B(Y),C(Z))
  read(*,*) A(:)
  read(*,*) B(:)
  read(*,*) C(:)

  allocate( ABC(X*Y*Z) )
  tot=X*Y*Z
  i = 0
  do i1 = 1,X
     do i2 = 1,Y
        do i3 = 1,Z
           i = i + 1
           ABC(i) = A(i1) + B(i2) + C(i3)
        end do
     end do
  end do
  ABC2 = iqsort(ABC)
  do i = 1,K
     write(*,'(i0)') ABC2(tot-i+1)
  end do
  stop
contains
  recursive function iqsort(ix) result(ires)
    integer(8), allocatable :: ires(:)
    integer(8), intent(in)  :: ix(:)
    integer(8) :: 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: 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.


program main
	implicit none
    integer::a,b,c
    read(*,*)a,b,c
    if(a==b .and. a==c)then
    	write(*,*)'Yes'
    else
    	write(*,*)'No'
    end if
    stop
end program main

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

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.

Submitted source-code file size, in bytes.

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

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


program main
implicit none
integer :: n, i, m, l
character :: s*100
read(*,*) n
read(*,*) s(1:n)

m = 0
do i = 1 , n
   if( s(i:i) == 'R' ) then
      m = m+1
   else
      l = l+1
   end if
end do

if( m>l) then
   write(*,*) 'Yes'
   stop
else
   write(*,*) 'No'
end if

end program main

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.


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

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

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

END PROGRAM ATCODER

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


module mod_modulo_operations
  implicit none
  integer(8), parameter :: md = 1000000007_8
  integer(8) :: f(0:100000), invf(0:100000)
  integer(8) :: dp(0:200,0:100000)
  integer :: s(200), leq(100000)
contains
  subroutine init(n)
    implicit none
    integer, intent(in) :: n
    integer :: i
    f = 0_8
    invf = 0_8
    f(0) = 1_8
    invf(0) = 1_8
    dp = -1_8
    do i = 1, n
      f(i) = mod(f(i-1)*int(i,8),md)
      invf(i) = inv(f(i))
    end do
    return
  end subroutine init
  integer(8) function perm(n,k)
    implicit none
    integer, intent(in) :: n, k
    perm = 0_8
    if (n.lt.0.or.k.gt.n) return
    perm = mod(f(n)*invf(n-k),md)
    return
  end function perm
  integer(8) function comb(n,k)
    implicit none
    integer, intent(in) :: n, k
    comb = mod(perm(n,k)*invf(k),md)
    return
  end function comb
  function inv(n) result(y)
    implicit none
    integer(8), intent(in) :: n
    integer(8) :: a, b, x, y, t, q
    a = mod(n,md)
    b = md
    x = 0
    y = 1
    do while(b.ne.0_8)
      q = a/b
      t = b
      b = mod(a,b)
      a = t
      t = y
      y = x
      x = t-q*x
    end do
    do while (y.lt.0_8)
      y = y+md
    end do
    y = mod(y,md)
    return
  end function inv
  recursive function dfs(n,x) result(c)
    implicit none
    integer :: n, x, sml, big, i
    integer(8) :: c, k
    c = dp(n,x)
    if (c.ge.0_8) return
    c = int(x,8)
    if (n.eq.0) return
    c = 0_8
    if (x.eq.0) return
    sml = leq(x)
    big = n-sml+1
    c = mod(f(n)*int(x,8),md)
    if (sml.eq.1) return
    k = mod(comb(n,sml-1)*f(big),md)
    c = 0_8
    do i = 1, sml-1
      c = mod(c+dfs(sml-2,mod(x,s(i))),md)
    end do
    c = mod(k*c,md)
    dp(n,x) = c
    return
  end function dfs
end module mod_modulo_operations
program modulo_operations
  use mod_modulo_operations
  implicit none
  integer :: n, x, i, j
  s = 0
  leq = 0
  read(*,*) n, x
  read(*,*) s(1:n)
  call quick_sort(s(1:n))
  j = 1
  do i = 1, 100000
    do while (j.le.n.and.s(j).le.i)
      j = j+1
    end do
    leq(i) = j
  end do
  call init(100000)
  write(*,'(i0)') dfs(n,x)
  stop
contains
  recursive subroutine quick_sort(a)
    implicit none
    integer, intent(inout) :: a(:)
    integer :: n, l, r, p, c
    n = size(a)
    l = 1
    r = n
    p = a((l+r)/2)+a(l)+a(r)-max(a((l+r)/2),a(l),a(r))-min(a((l+r)/2),a(l),a(r))
    do
      do while (a(l).lt.p)
        l = l+1
      end do
      do while (a(r).gt.p)
        r = r-1
      end do
      if (l.ge.r) exit
      c = a(l)
      a(l) = a(r)
      a(r) = c
      l = l+1
      r = r-1
    end do
    if (l.gt.2) call quick_sort(a(1:l-1))
    if (n.gt.r+1) call quick_sort(a(r+1:n))
  end subroutine quick_sort
end program modulo_operations

Score : 600 points

Problem Statement

Snuke has a blackboard and a set S consisting of N integers.
The i-th element in S is S_i.

He wrote an integer X on the blackboard, then performed the following operation N times:

Choose one element from S and remove it.

Let x be the number written on the blackboard now, and y be the integer removed from S. Replace the number on the blackboard with x \bmod {y}.

There are N! possible orders in which the elements are removed from S.
For each of them, find the number that would be written on the blackboard after the N operations, and compute the sum of all those N! numbers modulo 10^{9}+7.

Constraints

All values in input are integers.

1 \leq N \leq 200

1 \leq S_i, X \leq 10^{5}

S_i are pairwise distinct.

Input

Input is given from Standard Input in the following format:

N X
S_1 S_2 \ldots S_{N}

Output

Print the answer.

Sample Input 1

2 19
3 7

Sample Output 1

3

There are two possible orders in which we remove the numbers from S.

If we remove 3 and 7 in this order, the number on the blackboard changes as follows: 19 \rightarrow 1 \rightarrow 1.

If we remove 7 and 3 in this order, the number on the blackboard changes as follows: 19 \rightarrow 5 \rightarrow 2.

The output should be the sum of these: 3.

Sample Input 2

5 82
22 11 6 5 13

Sample Output 2

288

Sample Input 3

10 100000
50000 50001 50002 50003 50004 50005 50006 50007 50008 50009

Sample Output 3

279669259

Be sure to compute the sum modulo 10^{9}+7.


module mod_black_or_white
  implicit none
  integer(8), parameter :: md = 1000000007_8
  integer(8) :: f(0:200000), invf(0:200000), invp(0:200000)
contains
  subroutine init(n)
    implicit none
    integer, intent(in) :: n
    integer :: i
    integer(8) :: p
    f = 0_8
    invf = 0_8
    f(0) = 1_8
    p = 1_8
    do i = 1, n
      f(i) = mod(f(i-1)*int(i,8),md)
      p = mod(p*2_8,md)
    end do
    invf(n) = inv(f(n))
    invp(n) = inv(p)
    do i = n, 1, -1
      invf(i-1) = mod(invf(i)*int(i,8),md)
      invp(i-1) = mod(invp(i)*2_8,md)
    end do
    return
  end subroutine init
  integer(8) function perm(n,k)
    implicit none
    integer, intent(in) :: n, k
    perm = 0_8
    if (n.lt.0.or.k.gt.n) return
    perm = mod(f(n)*invf(n-k),md)
    return
  end function perm
  integer(8) function comb(n,k)
    implicit none
    integer, intent(in) :: n, k
    comb = mod(perm(n,k)*invf(k),md)
    return
  end function comb
  function inv(n) result(y)
    implicit none
    integer(8), intent(in) :: n
    integer(8) :: a, b, x, y, t, q
    a = mod(n,md)
    b = md
    x = 0
    y = 1
    do while(b.ne.0_8)
      q = a/b
      t = b
      b = mod(a,b)
      a = t
      t = y
      y = x
      x = t-q*x
    end do
    do while (y.lt.0_8)
      y = y+md
    end do
    y = mod(y,md)
    return
  end function inv
end module mod_black_or_white
program black_or_white
  use mod_black_or_white
  implicit none
  integer :: b, w, i
  integer(8) :: x = 0_8, y = 0_8
  read(*,*) b, w
  call init(b+w)
  do i = 1, b+w
    write(*,'(i0)') mod(mod(md+1_8-x+y,md)*invp(1),md)
    x = mod(x+comb(i-1,b-1)*invp(i),md)
    y = mod(y+comb(i-1,w-1)*invp(i),md)
  end do
  stop
end program black_or_white

Score : 700 points

Problem Statement

Today, Snuke will eat B pieces of black chocolate and W pieces of white chocolate for an afternoon snack.

He will repeat the following procedure until there is no piece left:

Choose black or white with equal probability, and eat a piece of that color if it exists.

For each integer i from 1 to B+W (inclusive), find the probability that the color of the i-th piece to be eaten is black.
It can be shown that these probabilities are rational, and we ask you to print them modulo 10^9 + 7, as described in Notes.

Notes

When you print a rational number, first write it as a fraction \frac{y}{x}, where x, y are integers and x is not divisible by 10^9 + 7
(under the constraints of the problem, such representation is always possible).
Then, you need to print the only integer z between 0 and 10^9 + 6, inclusive, that satisfies xz \equiv y \pmod{10^9 + 7}.

Constraints

All values in input are integers.

1 \leq B,W \leq 10^{5}

Input

Input is given from Standard Input in the following format:

B W

Output

Print the answers in B+W lines. In the i-th line, print the probability that the color of the i-th piece to be eaten is black, modulo 10^{9}+7.

Sample Input 1

2 1

Sample Output 1

500000004
750000006
750000006

There are three possible orders in which Snuke eats the pieces:

white, black, black

black, white, black

black, black, white

with probabilities \frac{1}{2}, \frac{1}{4}, \frac{1}{4}, respectively. Thus, the probabilities of eating a black piece first, second and third are \frac{1}{2},\frac{3}{4} and \frac{3}{4}, respectively.

Sample Input 2

3 2

Sample Output 2

500000004
500000004
625000005
187500002
187500002

They are \frac{1}{2},\frac{1}{2},\frac{5}{8},\frac{11}{16} and \frac{11}{16}, respectively.

Sample Input 3

6 9

Sample Output 3

500000004
500000004
500000004
500000004
500000004
500000004
929687507
218750002
224609377
303710940
633300786
694091802
172485353
411682132
411682132


program main
    use,intrinsic :: iso_fortran_env
    implicit none
    character(1):: b

    read*, b
    if (b == 'A') then
        print'(a)', 'T'
    else if (b == 'G') then
        print'(a)', 'C'
    else if (b == 'T') then
        print'(a)', 'A'
    else
        print'(a)', 'G'
    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 double_helix
  implicit none
  character(1) :: b
  read(*,*) b
  write(*,'(a)') achar(pair(ichar(b)-65)+65)
contains
  integer function pair(n)
    integer, intent(in) :: n
    pair = (228-12*n)/(11*n+12)
  end
end program double_helix


implicit none

character b

read(5,*)b

if(b=="A") then
write(6,*) "T"

else if(b=="T") then
write(6,*) "A"

else if(b=="C") then
write(6,*) "G"

else
write(6,*) "C"

endif

end


program sample
    implicit none
    integer(8) ::a, b,c,i,j,m,n,k,a1,b1,c1

    character(len=10)::s
    character(len=1)::t
    read(*,*) s
    a=len_trim(s)
    b=0
    c=0
    do i=1,a
        t=s(i:i)
        if (t=='A' .or. t=='C' .or. t=='G' .or. t=='T') then
            b=b+1
        else
            if (b>c)then
                c=b
            end if
            b=0
        end if

    end do
    if (b>c)then
        c=b
    end if
    write(*,*)c
    stop
end program sample

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 ATCoder
implicit none
character*10::s
integer::i,l,count=0,ans=0
read*,s
s=trim(adjustl(s))
l=len_trim(s)
!print*,l
do i=1,l
   if(s(i:i)=='A'.or.s(i:i)=='T'.or.s(i:i)=='C'.or.s(i:i)=='G')then
      count=count+1
      ans=max(count,ans)
   else
      count=0
   end if
end do

print*,ans
!print*,s

end program ATCoder


program ATCoder
implicit none
character*10::s
integer::i,l,count=0,ans=0
read*,s
s=trim(adjustl(s))
l=len_trim(s)
!print*,l
do i=1,l
   if(s(i:i)=='A'.or.s(i:i)=='T'.or.s(i:i)=='C'.or.s(i:i)=='G')then
      count=count+1
   else
      if(count>ans)then
         ans=count
         count=0
      else
         count=0
      end if
   end if
end do

print*,ans
!print*,s

end program ATCoder


PROGRAM ATCODER

implicit none
character(len=10) :: s
integer :: i,j
integer :: long(10)

read(*,*) s

long = 0
do i = 1, 10
  do j = i,10
    if ( s(j:j) == 'A' .or. s(j:j) == 'T' .or. s(j:j) == 'G' .or. s(j:j) == 'C' ) then
      long(i) = long(i) + 1
    else
      exit
    end if
  end do
end do

write(*,'(i0)') maxval(long)

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

END PROGRAM ATCODER


implicit none
integer :: n,q
integer :: l,r,i
logical, allocatable :: c(:)
character(100000) :: s
read *,n,q
read *,s

allocate(c(N-1))
c(:) = .False.
i = 1
do while (i < N)
   if (s(i+1:i+1) == 'T' .or. s(i+1:i+1) == 'G') then
      i = i + 2
      cycle
   else if (s(i+1:i+1) == 'A') then
      i = i + 1
      cycle
   else if (s(i:i) == 'A') then
      c(i) = .True.
   endif
   i = i + 2
enddo

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

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 main
integer(8)::n,q,i
integer(8),allocatable::a(:),l(:),r(:)
character(len=100000)::s

read*,n,q
allocate(a(n),r(q),l(q))
read*,s
a=0

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

do i=1,n
if(s(i:i+1)=="AC") a(i+1)=1
enddo

do i=1,q
print"(i0)",sum(a(l(i):r(i)))
enddo

end program main


integer N
integer,allocatable,dimension(:)::b
integer,allocatable,dimension(:)::ans
read*,N
allocate(b(N),ans(N))
read*,b
do i=N,1,-1
  do j=i,0,-1
    if(j==0)then
      print"(i0)",-1
      stop
    endif
    if(b(j)==j)then
      ans(i)=j
      b=[b(:j-1),b(j+1:)]
      exit
    endif
  end do
end do

print"(i0)",ans

end

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
integer :: N, i, j
integer,allocatable :: a(:), b(:), ans(:)
read(*, *)N
allocate(a(N))
allocate(ans(N))
read(*, *)a

do i = N, 1, -1
    do j = i, 1, -1
        if (a(j) == j)then
            ans(i) = j
            exit
        endif
    enddo
    if(j == 0)then
        write(*, '(i0)') -1
        stop
    endif
    b = [a(1:j - 1), a(j + 1: i)]
    a = b
enddo

write(*, '(*(i0, :, /))') ans

end program main


module mod_weighted_graph
  implicit none
  integer(8), parameter :: long_infty = lshift(1_8, 62)
  integer, parameter :: int_infty = lshift(1, 30)

  type t_edge
    integer :: from = 0
    integer :: to = 0
    integer :: capacity = 0
    integer :: reverse = 0
    integer(8) :: weight = 0
  end type

  type t_list
    type(t_edge), allocatable :: at(:)
  end type

  type weighted_graph
    type(t_list), allocatable :: list(:)
  contains
    procedure :: dijkstra => dijkstra
    procedure :: ford_fulkerson => ford_fulkerson
  end type

  type t_node
    type(t_node), private, pointer :: left => null(), right => null()
    integer :: rank = 1
    integer :: number
    integer(8) :: cost
  end type

  type leftist_heap
    integer :: size = 0
    type(t_node), private, pointer :: root => null()
  end type

  interface weighted_graph
    module procedure :: newwg0
  end interface weighted_graph

contains

  type(t_edge) function newe0(from, to, weight, capacity, reverse) result(res)
    integer, intent(in) :: from, to
    integer(8), intent(in) :: weight
    integer, optional, intent(in) :: capacity, reverse
    res%from = from
    res%to = to
    res%weight = weight
    if (present(capacity)) res%capacity = capacity
    if (present(reverse)) res%reverse = reverse
  end

  type(weighted_graph) function newwg0(n, m, a, b, weight, capacity, undirected) result(res)
    integer, intent(in) :: n, m, a(:), b(:)
    integer(8), intent(in) :: weight(:)
    integer, optional, intent(in) :: capacity(:)
    logical, optional, intent(in) :: undirected
    logical :: u, cap
    integer :: c(n), i
    if (present(undirected)) then
      u = undirected
    else
      u = .true.
    end if
    cap = present(capacity)
    c = 0
    do i = 1, m
      c(a(i)) = c(a(i)) + 1
      if (cap) c(b(i)) = c(b(i)) + 1
      if (u) then
        c(b(i)) = c(b(i)) + 1
        if (cap) c(a(i)) = c(a(i)) + 1
      end if
    end do
    allocate(res%list(n))
    do i = 1, n
      allocate(res%list(i)%at(c(i)))
    end do
    do i = m, 1, -1
      if (cap) then
        res%list(a(i))%at(c(a(i))) = newe0(a(i), b(i), weight(i), capacity(i), c(b(i)))
        res%list(b(i))%at(c(b(i))) = newe0(b(i), a(i), weight(i), 0, c(a(i)))
        c(a(i)) = c(a(i)) - 1
        c(b(i)) = c(b(i)) - 1
      else
        res%list(a(i))%at(c(a(i))) = newe0(a(i), b(i), weight(i))
        c(a(i)) = c(a(i)) - 1
      end if
      if (u) then
        if (cap) then
          res%list(b(i))%at(c(b(i))) = newe0(b(i), a(i), weight(i), capacity(i), c(a(i)))
          res%list(a(i))%at(c(a(i))) = newe0(a(i), b(i), weight(i), 0, c(b(i)))
          c(a(i)) = c(a(i)) - 1
          c(b(i)) = c(b(i)) - 1
        else
          res%list(b(i))%at(c(b(i))) = newe0(b(i), a(i), weight(i))
          c(b(i)) = c(b(i)) - 1
        end if
      end if
    end do
  end

  logical function less(a, b) result(res)
    type(t_node), pointer, intent(in) :: a, b
    res = a%cost < b%cost
  end

  subroutine swap(a, b)
    type(t_node), pointer, intent(inout) :: a, b
    type(t_node), pointer :: c
    c => a
    a => b
    b => c
  end

  function newn0(number, cost) result(res)
    integer, intent(in) :: number
    integer(8), intent(in) :: cost
    type(t_node), pointer :: res
    res => null()
    allocate(res)
    res%number = number
    res%cost = cost
  end

  recursive function meld_node(a, b) result(res)
    type(t_node), pointer, intent(in) :: a, b
    type(t_node), pointer :: res
    logical :: f
    if (.not.associated(a)) then
      res => b; return
    end if
    if (.not.associated(b)) then
      res => a; return
    end if
    if (less(a, b)) then
      res => a
      res%right => meld_node(res%right, b)
    else
      res => b
      res%right => meld_node(res%right, a)
    end if
    f = .not.associated(res%left)
    if (.not.f) f = res%left%rank < res%right%rank
    if (f) call swap(res%left, res%right)
    res%rank = merge(res%right%rank, 0, associated(res%right)) + 1
  end

  function poll(this) result(res)
    class(leftist_heap), intent(inout) :: this
    type(t_node), pointer :: res
    res => this%root
    this%root => meld_node(this%root%left, this%root%right)
    this%size = this%size - 1
  end

  subroutine offer(this, number, cost)
    class(leftist_heap), intent(inout) :: this
    integer, intent(in) :: number
    integer(8), intent(in) :: cost
    this%root => meld_node(this%root, newn0(number, cost))
    this%size = this%size + 1
  end

  function dijkstra(this, start) result(res)
    class(weighted_graph), intent(in) :: this
    integer, intent(in) :: start
    integer :: number, i
    integer(8) :: res(size(this%list)), cost
    type(t_node), pointer :: node
    type(leftist_heap) :: pq
    type(t_edge) :: e
    res = long_infty
    res(start) = 0
    call offer(pq, start, res(start))
    do while (pq%size > 0)
      node => poll(pq)
      number = node%number
      cost = node%cost
      deallocate(node)
      if (cost > res(number)) cycle
      do i = 1, size(this%list(number)%at)
        e = this%list(number)%at(i)
        if (res(e%to) > res(number) + e%weight) then
          res(e%to) = res(number) + e%weight
          call offer(pq, e%to, res(e%to))
        end if
      end do
    end do
  end

  integer function ford_fulkerson(this, start, goal) result(res)
    class(weighted_graph), intent(inout) :: this
    integer, intent(in) :: start, goal
    integer :: dflow
    logical :: used(size(this%list))
    res = 0
    do
      used = .false.
      dflow = ffdfs(this, used, start, goal, int_infty)
      if (dflow == 0) return
      res = res + dflow
    end do
  end

  recursive integer function ffdfs(this, used, u, goal, dflow) result(res)
    class(weighted_graph), intent(inout) :: this
    logical, intent(inout) :: used(:)
    integer, intent(in) :: u, goal
    integer, intent(in) :: dflow
    integer :: i, v, capacity, r
    if (u == goal) then
      res = dflow
      return
    end if
    used(u) = .true.
    do i = 1, size(this%list(u)%at)
      v = this%list(u)%at(i)%to
      capacity = this%list(u)%at(i)%capacity
      if (used(v) .or. capacity <= 0) cycle
      res = ffdfs(this, used, v, goal, min(dflow, capacity))
      r = this%list(u)%at(i)%reverse
      if (res > 0) then
        this%list(u)%at(i)%capacity = this%list(u)%at(i)%capacity - res
        this%list(v)%at(r)%capacity = this%list(v)%at(r)%capacity + res
        return
      end if
    end do
    res = 0
  end

end module mod_weighted_graph
program three_circuits
  use mod_weighted_graph
  implicit none
  integer :: n, m, s = -1, t = -1, x, i
  integer, dimension(100000) :: a = 0, b = 0, c = 1
  type(weighted_graph) :: g
  read(*,*) n, m
  do i = 1, m
    read(*,*) a(i), b(i)
  end do
  g = weighted_graph(n, m, a, b, [(0_8, i=1, m)], c)
  do i = 1, n
    x = size(g%list(i)%at) / 2
    if (mod(x, 2) == 1) then
      write(*,'(a)') "No"; stop
    end if
  end do
  do i = 1, n
    x = size(g%list(i)%at) / 2
    if (x >= 6) then
      write(*,'(a)') "Yes"; stop
    end if
    if (x == 4) then
      if (s < 0) then
        s = i; cycle
      end if
      if (t < 0) then
        t = i; cycle
      end if
      write(*,'(a)') "Yes"; stop
    end if
  end do
  if (t < 0) then
    write(*,'(a)') "No"; stop
  end if
  write(*,'(a)') trim(merge("Yes", "No ", g%ford_fulkerson(s, t) == 2))
end program three_circuits

Score : 800 points

Problem Statement

You are given a simple connected undirected graph consisting of N vertices and M edges.
The vertices are numbered 1 to N, and the edges are numbered 1 to M.

Edge i connects Vertex a_i and b_i bidirectionally.

Determine if three circuits (see Notes) can be formed using each of the edges exactly once.

Notes

A circuit is a cycle allowing repetitions of vertices but not edges.

Constraints

All values in input are integers.

1 \leq N,M \leq 10^{5}

1 \leq a_i, b_i \leq N

The given graph is simple and connected.

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
:
a_M b_M

Output

If three circuits can be formed using each of the edges exactly once, print Yes; if they cannot, print No.

Sample Input 1

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

Sample Output 1

Yes

Three circuits can be formed using each of the edges exactly once, as follows:

Sample Input 2

3 3
1 2
2 3
3 1

Sample Output 2

No

Three circuits are needed.

Sample Input 3

18 27
17 7
12 15
18 17
13 18
13 6
5 7
7 1
14 5
15 11
7 6
1 9
5 4
18 16
4 6
7 2
7 11
6 3
12 14
5 2
10 5
7 8
10 15
3 15
9 8
7 15
5 16
18 15

Sample Output 3

Yes


program name
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: i,j,n,cnt(ichar('a'):ichar('z')),ans
    integer(int64),parameter:: md=1000000007
    character(100000):: s

    read*, n
    read*, s
    cnt(:) = 1
    do i=1,n
        cnt(ichar(s(i:i)))=cnt(ichar(s(i:i)))+1
    end do

    ans = 1
    do i=ichar('a'),ichar('z')
        ans=mod(ans*cnt(i),md)
    end do
    print'(i0)', ans-1
end program name

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


integer N
character(100000) S
integer CNT(26)
integer tmp
integer(16) ans
read*,N
read*,S

!実に読みづらい名前になってしまった
!各文字がどこにいられるかの場所
CNT=1
do i=1,N
  tmp=ichar(S(i:i))-ichar("a")+1
  CNT(tmp)=Cnt(tmp)+1
end do
ans=1
do i=1,26
  ans=mod(ans*cnt(i),10**9+7)
end do
ans=ans-1
print"(i0)",ans
end


program test
implicit none

integer(8) :: n,a,b,count=0,hist = 0,nnn(200000)=0,i,j,gray(200000)=0,ii

read(*,*) n,a,b

do i=0,49
	if(BTEST(a,i) .eqv. BTEST(b,i)) then
		continue
	else
		hist = i
		count = count + 1
	endif
enddo

do i=0,2**n-1
	ii = ishft(i,-1)
	gray(i+1) = ieor(i,ii)
enddo

if(count == 1)then
	write(*,*) "YES"

	if(hist == n-1) then
		continue
	else
		do i = 1, 2**n
			if(BTEST(gray(i),n-1) .eqv. BTEST(gray(i),hist) ) then
				continue
			else
				if(BTEST(gray(i),n-1)) then
					gray(i) = ibset(gray(i),hist)
					gray(i) = ibclr(gray(i),n-1)
				else
					gray(i) = ibset(gray(i),n-1)
					gray(i) = ibclr(gray(i),hist)
				endif
			endif
		enddo
	endif

	do i =1,2**n
		nnn(i) = ieor(a,gray(i))
	enddo

	write(*,*) nnn(1:2**n)/0

else
	write(*,*) "NO"
endif

contains
subroutine heapsort_down(n,y1,y2)

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

	l = n/2+1
	k = n

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

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

		 i = l
		 j = l+l

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

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

		 enddo

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

	enddo
	return

end subroutine heapsort_down

subroutine heapsort_up(n,y1,y2)

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

	l = n/2+1
	k = n

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

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

		 i = l
		 j = l+l

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

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

		 enddo

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

	enddo
	return

end subroutine heapsort_up

end program

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, i, loop
  integer :: p(100000), q(100000), p0(100000), q0(100000)
  p = 0
  q = 0
  read(*,*) n, k
  read(*,*) p(1:n)
  read(*,*) q(1:n)
  if (n.eq.1) then
    write(*,'(i0)') 1
    stop
  end if
  if (k.eq.1) then
    write(*,'(i0)',advance='no') p(1)
    do i = 2, n
      write(*,'(x,i0)',advance='no') p(i)
    end do
    write(*,*)
    stop
  else if (k.eq.2) then
    write(*,'(i0)',advance='no') q(1)
    do i = 2, n
      write(*,'(x,i0)',advance='no') q(i)
    end do
    write(*,*)
    stop
  end if
  p0 = p
  q0 = q
  loop = -1
  do i = 3, k
    call swap(n,p(1:n),q(1:n))
    if ((sum(abs(p(1:n)-p0(1:n))).eq.0).and.(sum(abs(q(1:n)-q0(1:n))).eq.0)) then
      loop = i-2
      exit
    end if
  end do
  if (loop.gt.0) then
    k = mod(k,loop) + 1
    if (k.eq.1) then
      write(*,'(i0)',advance='no') p(1)
      do i = 2, n
        write(*,'(x,i0)',advance='no') p(i)
      end do
      write(*,*)
      stop
    else if (k.eq.2) then
      write(*,'(i0)',advance='no') q(1)
      do i = 2, n
        write(*,'(x,i0)',advance='no') q(i)
      end do
      write(*,*)
      stop
    end if
    do i = 3, k
      call swap(n,p(1:n),q(1:n))
    end do
  end if
  write(*,'(i0)',advance='no') q(1)
  do i = 2, n
    write(*,'(x,i0)',advance='no') q(i)
  end do
  write(*,*)
  stop
contains
  subroutine swap(n,p,q)
    implicit none
    integer, intent(inout) :: n, p(n), q(n)
    integer :: r(n), i
    do i = 1, n
      r(p(i)) = q(i)
    end do
    p = q
    q = r
    return
  end subroutine swap
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


      program pro
        implicit none
        integer H, W, hh, ww
        integer ans

        read(*,*) H, W, hh, ww

        ans = (H-hh)*(W-ww)
        write(*,'(I0)') ans
      end

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 main
implicit none
integer :: h0, i0, h, i

read(*,*) h0, i0
read(*,*) h, i
write(*,'(i0)') h0*i0-( h*i0+i*h0-h*i )

end program main


integer a,b,c,d
read*,a,b,c,d
print"(I0)",(a-c)*(b-d)
end


program main
  implicit none
  integer :: N,M
  integer,allocatable :: A(:,:)
  integer,allocatable :: B(:)
  integer,allocatable :: out(:)
  integer :: nans
  integer :: C
  integer :: i
  read(*,*) N,M,C
  allocate(B(M))
  allocate(A(N,M))
  read(*,*) B(:)
  do i = 1,N
     read(*,*) A(i,:)
  end do
  allocate(out(N))
  out=matmul(A,B)
  nans=0
  do i = 1,N
     if (out(i)+C > 0) nans = nans + 1
  end do
  write(*,'(i0)') nans
  stop
end program main

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 answer
  implicit none
  integer(16) :: i, N, M, sum, count, last, aa
  integer(16), allocatable :: A(:), B(:)
  read(*,*) N, M
  allocate(A(N),B(N))
  do i = 1, N
     read(*,*) A(i), B(i)
  end do

  sum=0
  count=0
  do
     if(count>=M) then
        exit
     end if
     aa=dot_product(B(minloc(A)),B(minloc(A)))
     aa=int(sqrt(dble(aa)))
     sum=sum+minval(A)*aa
     count=count+aa
     last=minval(A)
     A(minloc(A))=1000000001
  end do

  if(count>M) then
     sum=sum-(count-M)*last
  end if

  write(*,*) sum
  deallocate(A,B)
  stop
 end program answer

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

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

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

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

call heapsortkai(n,a,b)

j=0
res=0
do while(m>0)
j=j+1
m=m-b(j)
res=res+a(j)*b(j)
end do

res=res-abs(m)*a(j)

write(*,'(i0)') res

end program

subroutine heapsortkai(n,array1,array2)
  implicit none
  integer(8),intent(in) :: n
  integer(8),intent(inout) :: array1(1:n), array2(1:n)

  integer(8) ::i,k,j,l
  integer(8) :: t1, t2

  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
        t1=array1(l)
        t2=array2(l)
     else
        t1=array1(k)
        t2=array2(k)
        array1(k)=array1(1)
        array2(k)=array2(1)
        k=k-1
        if(k.eq.1) then
           array1(1)=t1
           array2(1)=t2
           exit
        endif
     endif
     i=l
     j=l+l
     do while(j.le.k)
        if(j.lt.k)then
           if(array1(j).lt.array1(j+1))j=j+1
        endif
        if (t1.lt.array1(j))then
           array1(i)=array1(j)
           array2(i)=array2(j)
           i=j
           j=j+j
        else
           j=k+1
        endif
     enddo
     array1(i)=t1
     array2(i)=t2
  enddo

  return
end subroutine heapsortkai


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).gt.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


program main
implicit None
	integer(8)::n,m,i,p=0,q=0
	integer(8),allocatable::a(:),b(:)

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

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

	call h2(a,b,n)

	do i =1,n
		if(m-q <= b(i))then
			p = p + a(i)*(m-q)
			exit
		endif
		q = q+b(i)
		p = p+a(i)*b(i)
	enddo

	print"(i0)",p
end program main

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

	do i = n/2,1,-1
		x = a(i);y = b(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
				b(i0) = b(i1);b(i1) = y
				else
				exit
			endif
			i0 = i1;i1 = i0 *2
		enddo
	enddo
	do ie = n,2,-1
		x = a(ie);y = b(ie)
		a(ie) = a(1);b(ie) = b(1)
		a(1) = x;b(1) = y
		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
				b(i0) = b(i1);b(i1) = y
				else
				exit
			endif
			i0 = i1;i1 = i0 *2
		enddo
	enddo
end subroutine h2


program k_th_common_divisor
  implicit none
  integer :: a, b, k, i, m = 0
  read(*,*) a, b, k
  do i = 100, 1, -1
    if (mod(a,i) == 0 .and. mod(b,i) == 0) m = m+1
    if (m == k) then
      write(*,'(i0)') i
      stop
    end if
  end do
end program k_th_common_divisor

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,count
    count = 0

    read(*,*) A,B,K

    do i = max(A,B),1
        if (mod(A,i)==0 .and. mod(B,i)==0)then
            count = count + 1
            if (count == K) then
                write(*,"(I0)") i
                exit
            end if
        end if
    enddo

end program main


program main
    implicit none

    integer A,B,K,i,count
    count = 0

    read(*,*) A,B,K

    do i = 1, max(A,B)
        if (mod(A,i)==0 .and. mod(B,i)==0)then
            count = count + 1
            if (count == K) then
                write(*,"(I0)") i
                exit
            end if
        end if
    enddo

end program main


program main
   implicit none
   integer(8)::a,b,k,cou=0
   integer(8)::i,j

  ! integer,allocatable,dimension(:)::c
   read(*,*)a,b,k
   do i=min(a,b),1,-1

      if(mod(a,i)==0 .and. mod(b,i)==0)then
         cou=cou+1
         if(cou==k)exit
      endif
   enddo
   write(*,*)i
end program main


program ccc
implicit none

character(10**5+1) :: s
integer :: l, zero, one, i

read*, s
l=len_trim(s)

zero=0
one=0
do i=1,l
if(s(i:i)=='0') then
zero=zero+1
else
one=one+1
end if
end do

write(*,'(i0)') min(zero,one)*2

end program

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 main
  implicit none
  integer :: n, i, m
  character(100000) :: s
  read (*, *) s
  n = len(trim(s))
  m = 0
  do i = 1, n
    if (s(i:i) == "0") m = m + 1
  end do

  write(*, "(i0)") min(m, n - m) * 2
end program main


! https://beta.atcoder.jp/contests/abc097/submissions/3768108

module UnionFind
    implicit none
    public
        type :: Union_Find
            integer(8),allocatable,dimension(:) :: par,g
        end type Union_Find
    contains
        subroutine init(p,n)
            implicit none
            type(Union_Find)  p
            integer(8) n,i
            allocate( p%par(n) )
            allocate( p%g(n) )
            do i=1,n
                p%par(i) = i
                p%g(i) = 1
            enddo
        end subroutine init
        recursive function root(p,n) result(ret)
            implicit none
            type(Union_Find) p
            integer(8) ret,n
            if(p%par(n)==n)then
                ret = n
                return
            else
                p%par(n) = root(p,p%par(n))
                ret = p%par(n)
                return
            endif
        end
        function gsize(p,x) result(a)
            implicit none
            integer(8) x,a
            type(Union_Find) p
            a = p%g(root(p,x))
        end
        subroutine unite(p,a,b)
            implicit none
            type(Union_Find) p
            integer(8) a,b
            a = root(p,a)
            b = root(p,b)
            if(a/=b)then
                p%par(a) = b
                p%g(b) = p%g(b) + p%g(a)
                p%g(a) = 0
            endif
        end
end module UnionFind

program atcoder_abc97d
    use UnionFind
    implicit none
    type(Union_Find) uf
    integer(8) n,m,i
    integer(8) ans,x,y
    integer(8),allocatable,dimension(:) :: a,b
    integer(8),allocatable,dimension(:) :: c
    read*,n,m
    allocate( a(m) , b(m) , c(m) )
    call init(uf,n+1)
    do i = 1,m
        read*,a(i),b(i)
    enddo
    ans = (n-1)*n/2
    do i=m,1,-1
        c(i) = ans
        x = gsize(uf,a(i))
        y = gsize(uf,b(i))
        if(root(uf,a(i)) /= root(uf,b(i)))then
            ans = ans - x*y
        endif
        call unite(uf,a(i),b(i))
    enddo
    do i=1,M
        write(*,'(I0)')c(i)
    enddo
!----------------------functions--------------------------
contains
end program atcoder_abc97d

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


Program ABC119A

integer y,m,d
character(1) a,b

read'(i4,a1,i2,a1,i2)', y,a,m,b,d

if(m<=4) then
 write(*,*) 'Heisei'
else
 write(*,*) 'TBD'
end if

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


module ABC119

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

	! require all variables to be explicitly declared
	implicit none

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

	! constants for this <module>
	character(len=6, kind=1), parameter, private :: word_before = 'Heisei'
	character(len=3, kind=1), parameter, private :: word_after  = 'TBD'

	integer( kind=INT8 ), parameter, private :: day_border   = 30
	integer( kind=INT8 ), parameter, private :: month_border =  4

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

	subroutine ABC119_A

		! variables for this <subroutine>
		character(len=10, kind=1) :: buffer
		integer( kind=INT8 )      :: month

		! STEP.01
		! read out the the date
		read( unit=INPUT_UNIT, fmt='(A)' ) buffer

		! STEP.02
		! convert the day and month to integer
		read( unit=buffer(6:7), fmt=* ) month

		! STEP.02
		! judge the read date
		if( month .le. month_border ) then
			write( unit=OUTPUT_UNIT, fmt='(A)', advance='yes' ) word_before
		else
			write( unit=OUTPUT_UNIT, fmt='(A)', advance='yes' ) word_after
		end if

		! STEP.END
		return

	end subroutine ABC119_A

end module ABC119

program main

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

	! require all variables to be explicitly declared
	implicit none

	call ABC119_A

end program main


program digital_gifts
  implicit none
  integer :: n, i, j = 0
  character(1) :: u
  real(8) :: x, b = 0.d0
  character(64) :: s
  read(*,*) n
  do i = 1, n
    read(*,*) x, u
    if (u == "J") then
      j = j+nint(x)
    else
      b = b+x
    end if
  end do
  write(s,'(f32.16)') 380000.d0*b+j
  write(*,'(a)') trim(adjustl(s))
end program digital_gifts

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 main
implicit none
integer :: i, j, n
real(8) :: nc_jpy, nc_btc
character(len=3) , allocatable :: cmoney(:)
real(8) , allocatable :: x(:)
real(8) :: y
real(8), parameter :: btc_jpy = 380000.

read(*,*) n
allocate(x(n))
allocate(cmoney(n))
nc_jpy = 0
nc_btc = 0
do i = 1, n
   read(*,*) x(i), cmoney(i)
   if (cmoney(i) == 'JPY') then
      nc_jpy = nc_jpy + x(i)
   else
      nc_btc = nc_btc + x(i)
   end if
end do

y = btc_jpy * nc_btc + nc_jpy
write(*,*) y

end program main


PROGRAM ATCODER

implicit none
double precision :: sum, b
double precision,allocatable :: x(:)
character(len=3),allocatable :: u(:)
integer :: n, i

b = 380000.d0

read(*,*) n
allocate(x(n), u(n))

do i = 1, n
	read(*,*) x(i), u(i)
end do

sum = 0.d0

do i = 1, n
	if(u(i) == 'BTC') then
    	x(i) = x(i)*b
    end if
    sum = sum + x(i)
end do

if (sum == 0.d0) then
	write(*,'(a)') '0.00000'
	stop
end if

write(*,'(f0.5)') sum

END PROGRAM ATCODER


PROGRAM ATCODER

implicit none
real(8) :: sum, b
real(8),allocatable :: x(:)
character(len=3),allocatable :: u(:)
integer :: n, i

b = 380000.0

read(*,*) n
allocate(x(n), u(n))

do i = 1, n
	read(*,*) x(i), u(i)
end do

sum = 0

do i = 1, n
	if(u(i) == 'BTC') then
    	x(i) = x(i)*b
    end if
    sum = sum + x(i)
end do

if (sum == 0.d0) then
	write(*,'(a)') '0.0'
	stop
end if

write(*,'(f0.1)') sum

END PROGRAM ATCODER


PROGRAM ATCODER

implicit none
integer :: n, a(3), i, ans, j, k
integer, allocatable :: l(:), cost(:,:)

read(*,*) n, a(1), a(2), a(3)
allocate(l(n), cost(n,n))

do i = 1, n
	read(*,*) l(i)
end do

call hs(n,l)
call hs(3,a)

ans = 0

do i = 1, n
	if(a(i) == 0)cycle
	if(l(i) /= 0)then
		do j = 1, n
    		if(l(j) /= 0)then
    			if(l(i)+l(j) <= minval(a) .and. i /= j)then
        			l(i) = l(i) + l(j)
            		ans = ans + 10
            		l(j) = 0
        		end if
        	end if
    	end do
    end if
end do

do i = 1, 3
	do j = 1, n
    	if(l(j) == a(i)) then
        	l(j) = 0
            a(i) = 0
        end if
    end do
end do

do i = 1, 3
    cost = 0
	do j = 1, n
    	do k = 1, n
        	if(j == k) then
            	cost(j,k) = abs(l(j)-a(i))
            else
            	cost(j,k) = 10 + abs(l(j)+l(k)-a(i))
            end if
    	end do
    end do
	do j = 1, n
    	do k = 1, n
        	if (cost(j,k) == minval(cost))then
            	ans = ans + minval(cost)
            	cost(j,k) = 0
                l(j) = 0
                l(k) = 0
            end if
        end do
    end do
end do

write(*,'(i0)') ans

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

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 abc119d
  implicit none
  integer a, b, q, i
  integer(8), dimension(:), allocatable :: s, t, x, ans
  integer(8), parameter :: inf = 10_8**11

  read *, a, b, q
  allocate(s(a), t(b), x(q), ans(q))
  read *, s, t, x
  s = [-inf, s, inf]
  t = [-inf, t, inf]
  do concurrent (i = 1:q)
     block
       integer si, ti
       integer(8) s_west, s_east, t_west, t_east, d(4)
       si = bsrch(s, x(i))
       ti = bsrch(t, x(i))
       s_west = x(i) - s(si - 1)
       s_east = s(si) - x(i)
       t_west = x(i) - t(ti - 1)
       t_east = t(ti) - x(i)
       d(1) = merge(s_west, t_west, s_west > t_west)
       d(2) = merge(s_east, t_east, s_east > t_east)
       d(3) = merge(s_west + t_east * 2, s_west * 2 + t_east, s_west > t_east)
       d(4) = merge(t_west + s_east * 2, t_west * 2 + s_east, t_west > s_east)
       ans(i) = minval(d)
     end block
  end do
  do i = 1, q
     print '(i0)', ans(i)
  end do
contains
  pure integer function bsrch(ary, v)
    integer(8), intent(in) :: ary(:), v
    integer low, mid, high

    low = lbound(ary, 1)
    high = ubound(ary, 1)

    do while (low < high)
       mid = low + (high - low) / 2
       if (ary(mid) < v) then
          low = mid + 1
       else
          high = mid
       end if
    end do
    bsrch = low
  end function bsrch
end program abc119d

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(8)::a,b,q,ma,mb,pa,pb
   integer(8)::i
   integer(8),allocatable,dimension(:)::s,t,x,dums,dumt
   read(*,*)a,b,q
   allocate(s(a),dums(a),t(b),dumt(b),x(q))
   do i=1,a
      read(*,*)s(i)
   enddo
   do i=1,b
      read(*,*)t(i)
   enddo
   do i=1,q
      read(*,*)x(i)
   enddo
   do i=1,q
      dums=s-x(i)
      dumt=t-x(i)
      ma=minval(abs(dums),dums<=0)
      mb=minval(abs(dumt),dumt<=0)
      pa=minval(abs(dums),dums>=0)
      pb=minval(abs(dumt),dumt>=0)
      write(*,*)min(max(ma,mb),max(pa,pb),2*min(ma,pa)+min(mb,pb),min(ma,pa)+2*min(mb,pb))
   enddo
end program main


program test
implicit none
integer::a,b,c,d

read*,a,b

c=mod(a,b)

if (c==0) then
d=a+b
else
d=b-a
end if

print*,d

end program test

Score : 100 points

Problem Statement

You are given positive integers A and B.

If A is a divisor of B, print A + B; otherwise, print B - A.

Constraints

All values in input are integers.

1 \leq A \leq B \leq 20

Input

Input is given from Standard Input in the following format:

A B

Output

If A is a divisor of B, print A + B; otherwise, print B - A.

Sample Input 1

4 12

Sample Output 1

16

As 4 is a divisor of 12, 4 + 12 = 16 should be printed.

Sample Input 2

8 20

Sample Output 2

12

Sample Input 3

1 1

Sample Output 3

2

1 is a divisor of 1.


program aba
  implicit none
  integer :: a, b
  read(*,*) a, b
  if (mod(b,a) .eq. 0) then
     write(*,'(i0)') a+b
  else
     write(*,'(i0)') b-a
  end if
  stop
end program aba


program main
implicit none
integer :: n, m
integer , allocatable :: k(:), A(:,:), Ai(:)
integer :: i, j, l, nc

read(*,*) n, m
allocate(k(n))
allocate(A(n,m))
do i = 1, n
   read(*,*) k(i)
   allocate(Ai(k(i)))
   read(*,*) Ai(:)
   do j = 1, k(i)
      do l = 1, m
         if(l == ai(j)) then
            a(i,l) = 1
         else
            a(i,l) = 0
         end if
      end do
   end do
   deallocate(Ai)
end do

nc = 0
do j = 1, m
   if(sum(a(:,j)) == m ) then
      nc = nc + 1
   end if
end do
write(*,'(i0)') nc
deallocate(k)
deallocate(a)
stop
end program main

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 main
implicit None
	integer(8)::n,p,q,i
	integer(8),allocatable::a(:)

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

	p = a(1)
	q = p

	do i = 1,n
		call euc(a(i),p,q)
		p = q
	enddo

	print"(i0)",p

end program main

subroutine euc(a1,b1,c)
implicit None
integer(8)::a1,b1,c,a,b
	! return GCD of a1 and b1 in c

	a = a1
	b = b1
	c = 1

	do while (a /=0 .and. b /= 0)
		if (a > b)then
			c = b
			a = mod(a,b)
			else
			c = a
			b = mod(b,a)
		endif
	enddo
end subroutine euc

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 ABC118C

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

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

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

do while (a(n-1)/=0)

k=1
do j=1,n
if (a(k)/=0) then
do i=k+1,n
 a(i)=mod(a(i),a(1))
end do
go to 10
else
k=k+1
end if
end do

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

end do

write(*,*) a(n)

stop
end


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
  i_loop = 0
  do  i = 1,4
     where(A > Amin ) A = A-Amin
     if ( any(A < Amin ) ) then
        Amin_bf = Amin_bf
        Amin = minval(A,A>0)
     end if
     i_loop = i_loop + 1
     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 match_matching
  implicit none
  integer              :: n, m, b(9), i, j, d, r
  integer, allocatable :: a(:), dp(:)
  character(16384)     :: answer
  read(*,*) n, m
  allocate(a(m),dp(0:n))
  read(*,*) a(:)
  b = (/2,5,5,4,5,6,3,7,6/)
  call reverse_sort(m,a)
  dp = -1
  dp(0) = 0
  do i = 2, n
    d = -1
    do j = 1, m
      if (i-b(a(j)) .ge. 0) then
        if (dp(i-b(a(j))) .gt. d) d = dp(i-b(a(j)))
      end if
      if (d .lt. 0) then
        dp(i) = -1
      else
        dp(i) = d + 1
      end if
    end do
  end do
  r = n
  i = 1
  answer = ''
  do while (r .gt. 0)
    do j = 1, m
      if (r-b(a(j)) .ge. 0) then
        if (dp(r-b(a(j))) .eq. dp(r)-1) then
          answer(i:i) = achar(48+a(j))
          r = r - b(a(j))
          i = i + 1
          exit
        end if
      end if
    end do
  end do
  deallocate(a,dp)
  write(*,'(a)') answer(1:i)
  stop

contains

  subroutine reverse_sort(m,a)
    implicit none
    integer, intent(in)    :: m
    integer, intent(inout) :: a(m)
    integer                :: i, j
    do i = m-1, 1, -1
      do j = 1, i
        if (a(j) .lt. a(j+1)) call swap(a(j),a(j+1))
      end do
    end do
    return
  end subroutine reverse_sort

  subroutine swap(a,b)
    implicit none
    integer, intent(inout) :: a, b
    integer                :: c
    c = a
    a = b
    b = c
    return
  end subroutine swap

end program match_matching

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


program minpro2019B
    implicit none
    integer(8)::i
    integer(8)::flg=0
    integer(8),dimension(6)::A
    integer(8),dimension(4)::L=0
    read(5,*)(A(i),i=1,6)

    do i=1,6
        L(A(i))=L(A(i))+1
    end do

    do i=1,6
        if(A(i)>=3)flg=1
    end do

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

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 sample
    implicit none
    character(100)::s
    integer(8) :: i,j,m,n,a,b,k,l
    integer(8),allocatable :: x(:),y(:)

    read(*,*) k,a,b
    if(a+2>=b .or. k<=a)then
        write(*,*)k+1
        stop
    end if
    l=k-a+1
    m=a+(l/2)*(b-a)+mod(l,2)
    write(*,*)m

    stop
end program sample

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


program atcoder
    implicit none
    integer(8) :: ans,k,a,b
    logical x
    read*,k,a,b
    ans = 0
    x = (b-a)>2_8
    do while(k>0_8)
        if(ans>=a .and. x .and. k>1_8)then
            ans = ans + (b-a)
            k = k-1_8
        else
            ans = ans + 1_8
        endif
        k = k-1_8
    enddo
    write(*,'(I0)')ans
    stop
contains
end program atcoder


program timer
implicit none
integer :: x, t
read(*,*) x,t
write(*,*) real(x)/real(t)
stop
end program timer

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 main
	implicit none
    real(8) :: T,X

    write(*,*) T/X

    stop
end program main


program main
  implicit none

  integer :: n,i,j,a(10) = 0,t,s=0

  read(*,*)n
  read(*,*)(a(i), i = 1, n)
  do i=1,N-1
     do j=i+1,N
        if(a(i) .gt. a(j))then
           t=a(i)
           a(i)=a(j)
           a(j)=t
        end if
     end do
  end do

  do i = 1, n-1
    s = s + a(i)
  end do

  if (a(n) < s) then
    write(*,*)'Yes'
  else
    write(*,*)'No'
  end if
end program main

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 polygon
  implicit none
  integer :: n, l(10) = 0
  read(*,*) n
  read(*,*) l(1:n)
  if (2*maxval(l) < sum(l)) then
    write(*,'(a)') "Yes"
  else
    write(*,'(a)') "No"
  end if
end program polygon


program main
  implicit none
  integer :: n, i
  integer, allocatable :: l(:)
  read (*, *) n
  allocate (l(n))
  read (*, *) l(:)
  if (maxval(l) * 2 < sum(l)) then
    print "(a)", "Yes"
  else
    print "(a)", "No"
  end if
end program main


program main
  implicit none
  integer :: n, m, res
  integer, allocatable :: x(:), y(:)
  read (*, *) n, m
  allocate (x(m), y(m - 1))
  read (*, *) x(:)

  call quicksort(x)
  y(:) = x(2:) - x(:m - 1)
  if (m - 1 > 1) call quicksort(y)

  res = sum(y(:m - n))
  write (*, "(i0)") res

  contains

  recursive subroutine quicksort(a)
    integer, intent(inout) :: a(:)
    integer :: pivot, i, j, k, tmp, n
    real(4) :: r
    n = size(a)
    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 (.not. pivot <= a(i))
        i = i + 1
      end do
      do while (.not. a(j) <= pivot)
        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
    if (1 < i - 1) call quicksort(a(1:i - 1))
    if (j + 1 < n) call quicksort(a(j + 1:n))
  end subroutine quicksort
end program main

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


program main
implicit none
integer ::n , m
integer , allocatable :: x(:), y(:)
integer :: i
integer :: a

read(*,*) n,m
allocate(x(m))
allocate(y(m-1))
read(*,*) x
call bubble(m,x)
do i = 1, m-1
   y(i) = x(i+1)-x(i)
end do
call bubble(m-1,y)

a = 0
if( n.ge.m ) then
   a = 0
else
   do i = 1, m-n
      a = a + y(i)
   end do
end if

write(*,'(i0)') a
deallocate(x)
deallocate(y)

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 ATCODER

implicit none
integer :: n,i,s
character(len=100) :: a,b,c

read(*,*) n,a,b,c    !ここが入力欄

s = 0
do i = 1,n
if (a(i:i) /= b(i:i) .or. a(i:i) /= c(i:i))then
    s = s + 2
    if (a(i:i) == b(i:i) .or. b(i:i) == c(i:i) .or. c(i:i) == a(i:i))then
        s = s - 1
    end if
end if
end do

write(*,'(i0)') s

END PROGRAM ATCODER

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,t,u,dif
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

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

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

write(*,'(i0)') dif

END PROGRAM ATCODER

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 main
   implicit none
   integer s1
   integer:: s(1000000)
   integer i,j
   read(*,*)s1
   s(1)=s1
   do i=1,1000000
      if(mod(s(i),2)==0)then
         s(i+1)=s(i)/2
      else
         s(i+1)=3*s(i)+1
      endif
      do j=1,i
         if(s(i+1)==s(j))then
            write(*,*)i+1
            goto 100
         endif
      enddo
   enddo
100 continue
end program main

Score : 200 points

Problem Statement

A sequence a=\{a_1,a_2,a_3,......\} is determined as follows:

The first term s is given as input.

Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd.

a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1.

Find the minimum integer m that satisfies the following condition:

There exists an integer n such that a_m = a_n (m > n).

Constraints

1 \leq s \leq 100

All values in input are integers.

It is guaranteed that all elements in a and the minimum m that satisfies the condition are at most 1000000.

Input

Input is given from Standard Input in the following format:

s

Output

Print the minimum integer m that satisfies the condition.

Sample Input 1

8

Sample Output 1

5

a=\{8,4,2,1,4,2,1,4,2,1,......\}. As a_5=a_2, the answer is 5.

Sample Input 2

7

Sample Output 2

18

a=\{7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1,4,2,1,......\}.

Sample Input 3

54

Sample Output 3

114


program sample
    implicit none

    integer(8) :: i,j,m,n,x
    real(8)::a,b
    integer(8),allocatable ::h(:)

    read(*,*) n
    allocate(h(n))
    read(*,*)h
    x=0
    m=0
    do i=1,100
        do j=1,100
         if (h(j)>0) then
           x=1
           h(j)=h(j)-1
         else if (h(j)==0 .and. x==1) then
           m=m+1
           x=0
         end if
        end do
        if (x==1) then
            m=m+1
            x=0
        end if
    end do
    write(*,*) m
    deallocate(h)
    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