program main
    implicit none
    integer i,j,k, limit, limit2
    double precision x1y2, x2y1
    double precision :: s,x1,y1,x2,y2

    x1 = 0.d0
    y1 = 0.d0
    x2 = 0.d0
    y2 = 0.d0

    read*, s
    if(s>1.d18)then
        stop 's is too large'
    endif

    x1y2 = s
    x2y1 = 0
    do
        limit = int(dble(x1y2)/1d9) + 1
        limit2 = int(dble(x2y1)/1d9) + 1
        do i = limit, 1000000000
            if (mod(x1y2,real(i)) == 0)then
                x1 = i
                y2 = x1y2/i
                do j = limit2,1000000000
                    if(mod(x2y1, real(j)) == 0) then
                        x2 = j
                        y1 = x2y1/j
                        call output()
                    endif
                enddo
            endif
        enddo
        x1y2 = x1y2 + 1
        x2y1 = s - x1y2
    enddo

contains
    subroutine output()
        implicit none
        character(:), allocatable :: format_output

        format_output = '(i0,1x, i0,1x, i0,1x, i0,1x, i0,1x, i0)'
        print format_output, 0, 0, int(x1), int(y1), int(x2), int(y2)
        stop
    end subroutine output
end program main

Score : 400 points

Problem Statement

Given is an integer S.
Find a combination of six integers X_1,Y_1,X_2,Y_2,X_3, and Y_3 that satisfies all of the following conditions:

0 \leq X_1,Y_1,X_2,Y_2,X_3,Y_3 \leq 10^9

The area of the triangle in a two-dimensional plane whose vertices are (X_1,Y_1),(X_2,Y_2), and (X_3,Y_3) is S/2.

We can prove that there always exist six integers that satisfy the conditions under the constraints of this problem.

Constraints

1 \leq S \leq 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

S

Output

Print six integers X_1,Y_1,X_2,Y_2,X_3, and Y_3 that satisfy the conditions, in this order, with spaces in between.
If multiple solutions exist, any of them will be accepted.

Sample Input 1

3

Sample Output 1

1 0 2 2 0 1

The area of the triangle in a two-dimensional plane whose vertices are (1,0),(2,2), and (0,1) is 3/2.
Printing 3 0 3 1 0 1 or 1 0 0 1 2 2 will also be accepted.

Sample Input 2

100

Sample Output 2

0 0 10 0 0 10

Sample Input 3

311114770564041497

Sample Output 3

314159265 358979323 846264338 327950288 419716939 937510582


program triangle
  implicit none
  integer(8) :: s, h, w, a, b, d
  integer(8) :: x1, y1, x2, y2, x3, y3, i
  read(*,*) s
  x1 = 0_8
  y1 = 0_8
  h = int(ceiling(sqrt(real(s,8))),8)
  w = h
  d = h*w-s
  a = 0_8
  b = 0_8
  if (d > 0_8) then
    a = int(floor(sqrt(real(d,8))),8)
    do while (a < d)
      if (mod(d,a) == 0_8) exit
      a = a+1_8
    end do
    b = d/a
  end if
  x2 = a
  y2 = h
  x3 = w
  y3 = b
  write(*,'(i0,x,i0,x,i0,x,i0,x,i0,x,i0)') x1, y1, x2, y2, x3, y3
  stop
end program triangle

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

Submitted source-code file size, in bytes.

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

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


program dodecagon
    implicit none
    integer(4):: r

    read*, r
    print*, 3*r*r
end program dodecagon

Score : 100 points

Problem Statement

It is known that the area of a regular dodecagon inscribed in a circle of radius a is 3a^2.

Given an integer r, find the area of a regular dodecagon inscribed in a circle of radius r.

Constraints

1 \leq r \leq 100

r is an integer.

Input

Input is given from Standard Input in the following format:

r

Output

Print an integer representing the area of the regular dodecagon.

Sample Input 1

4

Sample Output 1

48

The area of the regular dodecagon is 3 \times 4^2 = 48.

Sample Input 2

15

Sample Output 2

675

Sample Input 3

80

Sample Output 3

19200


Program main
  implicit none
  integer :: r
  read(*,*) r
  write(*,'(i0)') 3*r*r
  stop
end program main


Score : 200 points

Problem Statement

There are N apple trees in a row. People say that one of them will bear golden apples.

We want to deploy some number of inspectors so that each of these trees will be inspected.

Each inspector will be deployed under one of the trees. For convenience, we will assign numbers from 1 through N to the trees. An inspector deployed under the i-th tree (1 \leq i \leq N) will inspect the trees with numbers between i-D and i+D (inclusive).

Find the minimum number of inspectors that we need to deploy to achieve the objective.

Constraints

All values in input are integers.

1 \leq N \leq 20

1 \leq D \leq 20

Input

Input is given from Standard Input in the following format:

N D

Output

Print the minimum number of inspectors that we need to deploy to achieve the objective.

Sample Input 1

6 2

Sample Output 1

2

We can achieve the objective by, for example, placing an inspector under Tree 3 and Tree 4.

Sample Input 2

14 3

Sample Output 2

2

Sample Input 3

20 4

Sample Output 3

3


program main
  implicit none
  integer i,N,D
  !read(*, *) (L(i), i = 1,N)
  read(*, *)N,D
  i = 2*D + 1
  i = N/i + 1

  write(*, *) i
end program main


integer N
integer,allocatable,dimension(:)::A
integer premax,maxnum,cnt
read*,N
allocate(A(N))
do i=1,N
  read*,A(i)
end do
premax=0
maxnum=A(1)
cnt=1
do i=2,N
  if(A(i)>maxnum)then
    premax=maxnum
    maxnum=A(i)
    cnt=1
  else if(A(i)==maxnum)then
    cnt=2
  else if(A(i)>=premax)then
    premax=A(i)
  endif
end do
do i=1,N
  if(A(i)/=maxnum.or.cnt==2)then
    print"(i0)",maxnum
  else
    print"(i0)",premax
  endif
end do
end

Score : 300 points

Problem Statement

You are given a sequence of length N: A_1, A_2, ..., A_N.
For each integer i between 1 and N (inclusive), answer the following question:

Find the maximum value among the N-1 elements other than A_i in the sequence.

Constraints

2 \leq N \leq 200000

1 \leq A_i \leq 200000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

Print N lines. The i-th line (1 \leq i \leq N) should contain the maximum value among the N-1 elements other than A_i in the sequence.

Sample Input 1

3
1
4
3

Sample Output 1

4
3
4

The maximum value among the two elements other than A_1, that is, A_2 = 4 and A_3 = 3, is 4.

The maximum value among the two elements other than A_2, that is, A_1 = 1 and A_3 = 3, is 3.

The maximum value among the two elements other than A_3, that is, A_1 = 1 and A_2 = 4, is 4.

Sample Input 2

2
5
5

Sample Output 2

5
5


program preparing_boxes
    implicit none
    integer(4):: n, i, j
    integer(4), allocatable:: a(:), b(:), d(:)

    read*, n
    allocate(a(n), b(n),d(n))
    read*, a(:)
    do i=n, 1
        if (a(i) > 1)then
            print*, -1
            stop
        else if (a(i) == 1) then
            b(i) = 1
            do j=2,nint(sqrt(dble(i)))
                if (mod(i,j) == 0) a(i/j)=a(i/j)-1
            end do
        end if
    end do

    print*, sum(b(:))
    do i=1,n
        if (b(i)==1) print*, i
    end do
end program preparing_boxes

Score : 400 points

Problem Statement

There are N empty boxes arranged in a row from left to right.
The integer i is written on the i-th box from the left (1 \leq i \leq N).

For each of these boxes, Snuke can choose either to put a ball in it or to put nothing in it.

We say a set of choices to put a ball or not in the boxes is good when the following condition is satisfied:

For every integer i between 1 and N (inclusive), the total number of balls contained in the boxes with multiples of i written on them is congruent to a_i modulo 2.

Does there exist a good set of choices? If the answer is yes, find one good set of choices.

Constraints

All values in input are integers.

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

a_i is 0 or 1.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

If a good set of choices does not exist, print -1.

If a good set of choices exists, print one such set of choices in the following format:

M
b_1 b_2 ... b_M

where M denotes the number of boxes that will contain a ball, and b_1,\ b_2,\ ...,\ b_M are the integers written on these boxes, in any order.

Sample Input 1

3
1 0 0

Sample Output 1

1
1

Consider putting a ball only in the box with 1 written on it.

There are three boxes with multiples of 1 written on them: the boxes with 1, 2, and 3. The total number of balls contained in these boxes is 1.

There is only one box with a multiple of 2 written on it: the box with 2. The total number of balls contained in these boxes is 0.

There is only one box with a multiple of 3 written on it: the box with 3. The total number of balls contained in these boxes is 0.

Thus, the condition is satisfied, so this set of choices is good.

Sample Input 2

5
0 0 0 0 0

Sample Output 2

0

Putting nothing in the boxes can be a good set of choices.


program preparing_boxes
  implicit none
  integer :: n, a(200000) = 0, b(200000) = 0, c(200000) = 1, m = 0, s, i
  read(*,*) n
  read(*,*) a(1:n)
  do i = n, 1, -1
    s = sum(c(i:n:i))
    if (mod(s,2) /= a(i)) c(i) = 0
  end do
  do i = 1, n
    if (c(i) == 1) then
      m = m+1
      b(m) = i
    end if
  end do
  write(*,'(i0)') m
  if (m > 0) then
    write(*,'(i0)',advance='no') b(1)
    do i = 2, m
      write(*,'(x,i0)',advance='no') b(i)
    end do
    write(*,*)
  end if
  stop
end program preparing_boxes


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

    read*, n

    if (mod(n,3) /= 0) then
        print'(a)', 'No'
        stop
    end if

    allocate(a(n))
    read*, a(:)
    call merge_sort(a,1,n)

    if (a(n)==0) then
        print'(a)', 'Yes'
        stop
    end if

    if (a(n/3) == 0 .and. a(n/3+1) > 0 .and. a(n/3+1) == a(n)) then
        print'(a)', 'Yes'
        stop
    end if

    if (a(1)==(a(n/3)) .and. a(n/3+1)==a(2*n/3) .and. a(2*n/3+1)==a(n))then
        if (xor(xor(a(1),a(n/3+1)),a(2*n/3+1)) == 0) then
            print'(a)', 'Yes'
            stop
        end if
    end if

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

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

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

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

        allocate(tmp(lst-fst+1))

        li=fst
        ri=mdl+1
        ti=1

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

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

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

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

Score : 300 points

Problem Statement

Snuke has N hats. The i-th hat has an integer a_i written on it.

There are N camels standing in a circle.
Snuke will put one of his hats on each of these camels.

If there exists a way to distribute the hats to the camels such that the following condition is satisfied for every camel, print Yes; otherwise, print No.

The bitwise XOR of the numbers written on the hats on both adjacent camels is equal to the number on the hat on itself.

What is XOR?

The bitwise XOR x_1 \oplus x_2 \oplus \ldots \oplus x_n of n non-negative integers x_1, x_2, \ldots, x_n is defined as follows:

- When x_1 \oplus x_2 \oplus \ldots \oplus x_n is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if the number of integers among x_1, x_2, \ldots, x_n whose binary representations have 1 in the 2^k's place is odd, and 0 if that count is even.

For example, 3 \oplus 5 = 6.

Constraints

All values in input are integers.

3 \leq N \leq 10^{5}

0 \leq a_i \leq 10^{9}

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 \ldots a_{N}

Output

Print the answer.

Sample Input 1

3
1 2 3

Sample Output 1

Yes

If we put the hats with 1, 2, and 3 in this order, clockwise, the condition will be satisfied for every camel, so the answer is Yes.

Sample Input 2

4
1 2 4 8

Sample Output 2

No

There is no such way to distribute the hats; the answer is No.


Score : 100 points

Problem Statement

N of us are going on a trip, by train or taxi.

The train will cost each of us A yen (the currency of Japan).

The taxi will cost us a total of B yen.

How much is our minimum total travel expense?

Constraints

All values in input are integers.

1 \leq N \leq 20

1 \leq A \leq 50

1 \leq B \leq 50

Input

Input is given from Standard Input in the following format:

N A B

Output

Print an integer representing the minimum total travel expense.

Sample Input 1

4 2 9

Sample Output 1

8

The train will cost us 4 \times 2 = 8 yen, and the taxi will cost us 9 yen, so the minimum total travel expense is 8 yen.

Sample Input 2

4 2 7

Sample Output 2

7

Sample Input 3

4 2 8

Sample Output 3

8


program aaa

implicit none
integer :: n, a, b
read*, n, a, b

write(*,'(i0)') min(a*n,b)

end program


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

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

    ans(1) = (sum(a(1:n:2)) - sum(a(2:n:2)))
    ans(n) = 2*a(n) - ans(1)
    do i=n-1,2,-1
        ans(i) = 2*a(i) - ans(i+1)
    end do
    print'(*(i0,1x))', ans(:)
end program abc133d

Score : 400 points

Problem Statement

There are N mountains in a circle, called Mountain 1, Mountain 2, ..., Mountain N in clockwise order. N is an odd number.

Between these mountains, there are N dams, called Dam 1, Dam 2, ..., Dam N. Dam i (1 \leq i \leq N) is located between Mountain i and i+1 (Mountain N+1 is Mountain 1).

When Mountain i (1 \leq i \leq N) receives 2x liters of rain, Dam i-1 and Dam i each accumulates x liters of water (Dam 0 is Dam N).

One day, each of the mountains received a non-negative even number of liters of rain.

As a result, Dam i (1 \leq i \leq N) accumulated a total of A_i liters of water.

Find the amount of rain each of the mountains received. We can prove that the solution is unique under the constraints of this problem.

Constraints

All values in input are integers.

3 \leq N \leq 10^5-1

N is an odd number.

0 \leq A_i \leq 10^9

The situation represented by input can occur when each of the mountains receives a non-negative even number of liters of rain.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print N integers representing the number of liters of rain Mountain 1, Mountain 2, ..., Mountain N received, in this order.

Sample Input 1

3
2 2 4

Sample Output 1

4 0 4

If we assume Mountain 1, 2, and 3 received 4, 0, and 4 liters of rain, respectively, it is consistent with this input, as follows:

Dam 1 should have accumulated \frac{4}{2} + \frac{0}{2} = 2 liters of water.

Dam 2 should have accumulated \frac{0}{2} + \frac{4}{2} = 2 liters of water.

Dam 3 should have accumulated \frac{4}{2} + \frac{4}{2} = 4 liters of water.

Sample Input 2

5
3 8 7 5 5

Sample Output 2

2 4 12 2 8

Sample Input 3

3
1000000000 1000000000 0

Sample Output 3

0 2000000000 0


program abc133d
   implicit none

   integer :: N, r1, i
   integer, allocatable :: a(:), r(:)

   read(*,*) N
   allocate( a(0:N), r(n))
   read(*,*) a(1:N)

   a(0) = a(N)
   r = 0
   r1 = 0
   do i = 1, n-2, 2
      r1 = r1 + a(i) - a(i+1)
   enddo
   r1 = r1 + a(n)

   r(1) = r1

   do i = n, 2, -1
      if (i == n) then
         r(n) = 2*a(n) - r(1)
         cycle
      else
         r(i) = 2*a(i) - r(i+1)
      end if
   enddo

   print *, r(:)
end program abc133d


module ABC133

  ! <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  :: task_D

  ! variables for this <module>
  integer(INT32), private :: num_mountain

  ! arrays for this <module>
  integer(INT32), dimension(:), allocatable, private :: solution

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

  subroutine task_D

    ! support variables for this <subroutine>
    integer(INT32) :: itr

    ! STEP.01
    ! read out given data (1/2)
    read(unit=INPUT_UNIT, fmt=*) num_mountain

    ! STEP.02
    ! allocate the array to store the given data & the answer of this task
    allocate( solution(1:num_mountain) )

    ! STEP.03
    ! read out given data (2/2)
    read(unit=INPUT_UNIT, fmt=*) solution(:)

    ! STEP.04
    ! calculate the answer of this task
    solution(:) = solution(:) * 2_INT32

    do itr = 2, num_mountain - 1, 1
      solution(itr) = solution(itr) - solution(itr - 1)
    end do

    if ( mod(num_mountain, 2_INT32) .eq. 0_INT32 ) then
      solution(num_mountain) = solution(num_mountain) - solution(num_mountain - 1)
    else
      solution(num_mountain) = solution(num_mountain) + solution(num_mountain - 1)
    end if

    solution(num_mountain) = solution(num_mountain) / 2_INT32

    solution(num_mountain - 1) = solution(num_mountain) - solution(num_mountain - 1)

    do itr = 1, num_mountain - 2, 1
      if ( mod(itr, 2_INT32) .eq. 0_INT32 ) then
        solution(itr) = solution(itr) + solution(num_mountain - 1)
      else
        solution(itr) = solution(itr) - solution(num_mountain - 1)
      end if
    end do

    ! STEP.05
    ! output the answer of this task
    write(unit=OUTPUT_UNIT, fmt='(I0)', advance='no') solution(num_mountain - 1)

    do itr = 2, num_mountain - 1, 1
      write(unit=OUTPUT_UNIT, fmt='(1X,I0)', advance='no') solution(itr - 1)
    end do

    write(unit=OUTPUT_UNIT, fmt='(1X,I0)', advance='no') solution(num_mountain)

    ! STEP.06
    ! deallocate the array to store the given data & the answer of this task
    deallocate( solution )

    ! STEP.END
    return

  end subroutine task_D

end module ABC133

program main

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

  ! require all variables to be explicitly declared
  implicit none

  call task_D

end program main


program main
  implicit none
  integer(8) i,N,n_min,j,t1
  integer(8),allocatable :: A(:)
  integer(8),allocatable :: rain(:)
  integer(8),allocatable :: V(:,:)
  read(*, *) N
  allocate(A(N))
  allocate(rain(N))
  allocate(V(N,N))
  read(*, *) (A(i), i = 1,N)
  V = 1
  do i = 1,N
    do j = 1,(N-1)/2
      if (i+2*j-1>N) then
        t1 = i+2*j-1-N
      else
        t1 = i+2*j-1
      end if
      V(i,t1) = -1
    end do
  end do
  rain = 0
  do i = 1,N
    do j = 1,N
      rain(i) = rain(i) + A(j)*V(i,j)
    end do
  end do
  write(*,*) rain
end program main


program main
  implicit none

  character a,b,c,d

  read(*,*)a,b,c,d
  if ((a == b) .and.(c == d)) then
	write(*,*)"Yes"
  else if ((a == c) .and.(b == d)) then
	write(*,*)"Yes"
  else if ((a == d) .and.(b == c)) then
	write(*,*)"Yes"
  else if ((b == c) .and.(a == d)) then
	write(*,*)"Yes"
  else if ((b == d) .and.(a == c)) then
	write(*,*)"Yes"
  else if ((c == d) .and.(a == b)) then
	write(*,*)"Yes"
  else
	write(*,*)"No"
  end if

end program main

Score : 100 points

Problem Statement

You are given a 4-character string S consisting of uppercase English letters.
Determine if S consists of exactly two kinds of characters which both appear twice in S.

Constraints

The length of S is 4.

S consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If S consists of exactly two kinds of characters which both appear twice in S, print Yes; otherwise, print No.

Sample Input 1

ASSA

Sample Output 1

Yes

S consists of A and S which both appear twice in S.

Sample Input 2

STOP

Sample Output 2

No

Sample Input 3

FFEE

Sample Output 3

Yes

Sample Input 4

FREE

Sample Output 4

No


program abc132a
  implicit none
  character(4) :: s
  integer :: i, m
  logical :: flag=.false.

  read(*,*) s

  if (s(1:1) == s(2:2) .and. s(3:3) == s(4:4)) then
    flag=.True.
  else if (s(1:1) == s(3:3) .and. s(2:2) == s(4:4)) then
    flag=.True.
  else if (s(1:1) == s(4:4) .and. s(2:2) == s(3:3)) then
    flag=.True.
  endif

  if (flag) then
    print *, 'Yes'
  else
    print *, 'No'
  endif

end program abc132a


integer :: n,p(20)
integer :: i,ans

read*,n
read*,p(1:n)

ans = 0

do i = 2,n-1
  if( p(i-1)<p(i) .and. p(i)<p(i+1) ) ans = ans + 1
end do
end program

Score : 200 points

Problem Statement

We have a permutation p = {p_1,\ p_2,\ ...,\ p_n} of {1,\ 2,\ ...,\ n}.

Print the number of elements p_i (1 < i < n) that satisfy the following condition:

p_i is the second smallest number among the three numbers p_{i - 1}, p_i, and p_{i + 1}.

Constraints

All values in input are integers.

3 \leq n \leq 20

p is a permutation of {1,\ 2,\ ...,\ n}.

Input

Input is given from Standard Input in the following format:

n
p_1 p_2 ... p_n

Output

Print the number of elements p_i (1 < i < n) that satisfy the condition.

Sample Input 1

5
1 3 5 4 2

Sample Output 1

2

p_2 = 3 is the second smallest number among p_1 = 1, p_2 = 3, and p_3 = 5. Also, p_4 = 4 is the second smallest number among p_3 = 5, p_4 = 4, and p_5 = 2. These two elements satisfy the condition.

Sample Input 2

9
9 6 3 2 5 8 7 4 1

Sample Output 2

5


program main
  implicit none
  integer :: n, i, res
  integer, allocatable :: p(:)
  read (*, *) n
  allocate (p(n))
  read (*, *) p(:)
  res = 0
  do i = 1, n - 2
    if ((p(i) < p(i + 1) .and. p(i + 1) < p(i + 2)) .or. (p(i) > p(i + 1) .and. p(i + 1) > p(i + 2))) res = res + 1
  end do
  write (*, "(i0)") res
end program main


program main
  implicit none
  integer i,N,al,av
  integer,allocatable :: d(:)
  !read(*, *) (L(i), i = 1,N)
  read(*, *) N
  allocate(d(N))
  read(*, *) (d(i), i = 1,N)
  call qsort(d)
  write(*, *) abs(d(N/2)-d(N/2+1)) + 1

  contains

  recursive subroutine qsort(a)
  implicit none
  integer,intent(inout) :: a(:)
  integer :: start,ed,i,j,toobig,toosmall,pv

  n = size(a)
  start = 1
  ed = n
  pv = a((start+ed)/2)
  i = start
  j = ed
  do
     !pv = a((start+ed)/2)
     !i = start
     !j = ed
     do
       if (a(i) < pv) then
         i = i+1
       else
         exit
       end if
     end do
     do
       if(a(j) > pv) then
         j = j-1
       else
         exit
       end if
     end do
     if (i >= j) then
       exit
     else
       toobig = a(i)
       toosmall = a(j)
       a(j) = toobig
       a(i) = toosmall
       i = i + 1
       j = j - 1
     end if
   end do
  if (i-1 > start) then
    call qsort(a(1:i-1))
  end if
  if (j+1 < ed) then
    call qsort(a(j+1:n))
  end if
  end subroutine qsort

end program main

Score : 300 points

Problem Statement

Takahashi made N problems for competitive programming.
The problems are numbered 1 to N, and the difficulty of Problem i is represented as an integer d_i (the higher, the harder).

He is dividing the problems into two categories by choosing an integer K, as follows:

A problem with difficulty K or higher will be for ARCs.

A problem with difficulty lower than K will be for ABCs.

How many choices of the integer K make the number of problems for ARCs and the number of problems for ABCs the same?

Problem Statement

2 \leq N \leq 10^5

N is an even number.

1 \leq d_i \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
d_1 d_2 ... d_N

Output

Print the number of choices of the integer K that make the number of problems for ARCs and the number of problems for ABCs the same.

Sample Input 1

6
9 1 4 4 6 7

Sample Output 1

2

If we choose K=5 or 6, Problem 1, 5, and 6 will be for ARCs, Problem 2, 3, and 4 will be for ABCs, and the objective is achieved.
Thus, the answer is 2.

Sample Input 2

8
9 1 14 5 5 4 4 14

Sample Output 2

0

There may be no choice of the integer K that make the number of problems for ARCs and the number of problems for ABCs the same.

Sample Input 3

14
99592 10342 29105 78532 83018 11639 92015 77204 30914 21912 34519 80835 100000 1

Sample Output 3

42685


module UIntegerSort
  implicit none

  private

  public QuickSortInteger

contains

  !!
  ! ピボット値の取得
  !
  subroutine GetPivotIntegerValue(array, n, a, b, pivot_value)
    integer, intent(inout) :: array(:), pivot_value
    integer, intent(in) :: n, a, b

    pivot_value = array((a + b) / 2)
  end subroutine

  !!
  ! ピボット値に従って値を分離
  !
  subroutine GetPartitionInteger(array, n, a, b, pivot)
    integer, intent(inout) :: array(:)
    integer, intent(in)  :: n, a, b
    integer, intent(out) :: pivot

    integer :: buffer, pivot_value
    integer :: l, r

    if (a == b) then
      pivot = 0
      return
    end if

    call GetPivotIntegerValue(array, n, a, b, pivot_value)

    l = a
    r = b

    do while(.true.)
      do while(array(l) < pivot_value)
        if (l >= b) exit
        l = l + 1
      end do

      do while(pivot_value < array(r))
        if (r <= a) exit
        r = r - 1
      end do

      if(r <= l) exit

      buffer   = array(l)
      array(l) = array(r)
      array(r) = buffer

      l = l + 1
      r = r - 1
    end do

    pivot = r
  end subroutine

  !!
  ! 配列の一部分をソートする。
  !
  recursive subroutine QuickSortIntegerSubarray(array, n, a, b)
    integer, intent(inout) :: array(:)
    integer, intent(in)    :: n, a, b

    integer :: pivot

    call GetPartitionInteger(array, n, a, b, pivot)
    if (pivot /= 0) then
      call QuickSortIntegerSubarray(array, n, a, pivot)
      call QuickSortIntegerSubarray(array, n, pivot + 1, b)
    end if
  end subroutine

  !!
  ! 配列全体をソートする
  !
  subroutine QuickSortInteger(array, n)
    integer :: array(:)
    integer :: n

    call QuickSortIntegerSubarray(array, n, 1, n)
  end subroutine

end module

program TestIntegerSort
  use UIntegerSort

  implicit none
  integer,allocatable::a(:)
  integer :: N,i
  read*,N
  allocate(a(N))
  read(*,*)(a(i),i=1,N)
  call QuickSortInteger(a, N)
    print *, a(N/2+1)-a(N)
end program


program main
implicit none
character*16 :: a
integer :: n,i,j,x,y,z,m
integer,allocatable :: p(:)

read(*,*)n
allocate(p(n))
read(*, *)(p(i), i = 1,n)
call quicksort(p,1,n)

z=0

z = p(n/2+1) - p(n/2)

write(*,*) z

contains

recursive subroutine quicksort(a, first, last)
  implicit none
  integer  a(*)
  integer first, last
  integer i, j
  real*8  x, t

  x = a( (first+last) / 2 )
  i = first
  j = last
  do
     do while (a(i) < x)
        i=i+1
     end do
     do while (x < a(j))
        j=j-1
     end do
     if (i >= j) exit
     t = a(i);  a(i) = a(j);  a(j) = t
     i=i+1
     j=j-1
  end do
  if (first < i - 1) call quicksort(a, first, i - 1)
  if (j + 1 < last)  call quicksort(a, j + 1, last)
end subroutine quicksort
end program main


program main
	implicit none
    integer::N,i,ans,r,b,k
    integer, allocatable :: a(:)
    read(*,*) N
    allocate(a(N))
    read(*, *)(a(i), i = 1,N)
    ans=0
	r=0
    b=0
    k=1
    do while(r>=b)
		r=0
        b=0
    	do i=1,N
        	if(a(i)<k) then
            	b=b+1
                r=r
            else
                b=b
                r=r+1
            end if
       end do
       if(b==r) then
       	ans=ans+1
       else
        ans=ans
       end if
       k=k+1
   end do
  print*,ans
 end program main


program sc
  implicit none
  integer :: i,muzu
  character(len=4) :: S

  read(*,*) S
  muzu=0
  do i=1,3
     if(S(i:i).eq.S(i+1:i+1)) then
        muzu=1
     endif
  enddo
  if(muzu.eq.0) then
     write(*,'(a)') 'Good'
  else
     write(*,'(a)') 'Bad'
  endif

end program sc

Score : 100 points

Problem Statement

The door of Snuke's laboratory is locked with a security code.

The security code is a 4-digit number. We say the security code is hard to enter when it contains two consecutive digits that are the same.

You are given the current security code S. If S is hard to enter, print Bad; otherwise, print Good.

Constraints

S is a 4-character string consisting of digits.

Input

Input is given from Standard Input in the following format:

S

Output

If S is hard to enter, print Bad; otherwise, print Good.

Sample Input 1

3776

Sample Output 1

Bad

The second and third digits are the same, so 3776 is hard to enter.

Sample Input 2

8080

Sample Output 2

Good

There are no two consecutive digits that are the same, so 8080 is not hard to enter.

Sample Input 3

1333

Sample Output 3

Bad

Sample Input 4

0024

Sample Output 4

Bad


program sample
	implicit none
    integer :: x,y,i,sum1,maxval1,minval1
    integer, allocatable :: a(:)

    read(*,*) x,y

    allocate(a(x))

    do i=1,x
       a(i)=y+i-1
    end do

    write(*,*) a

    sum1=sum(a)
    maxval1=maxval(a)
	minval1=minval(a)

    if (maxval1 >=0 .and. minval1<=0) then
    	write(*,*) sum1
    else if (minval1>0) then
    	write(*,*) sum1-minval1
    else
    	write(*,*) sum1-maxval1
    end if

    deallocate(a)

    stop
end program sample

Score : 200 points

Problem Statement

You have N apples, called Apple 1, Apple 2, Apple 3, ..., Apple N. The flavor of Apple i is L+i-1, which can be negative.

You can make an apple pie using one or more of the apples. The flavor of the apple pie will be the sum of the flavors of the apples used.

You planned to make an apple pie using all of the apples, but being hungry tempts you to eat one of them, which can no longer be used to make the apple pie.

You want to make an apple pie that is as similar as possible to the one that you planned to make. Thus, you will choose the apple to eat so that the flavor of the apple pie made of the remaining N-1 apples will have the smallest possible absolute difference from the flavor of the apple pie made of all the N apples.

Find the flavor of the apple pie made of the remaining N-1 apples when you choose the apple to eat as above.

We can prove that this value is uniquely determined.

Constraints

2 \leq N \leq 200

-100 \leq L \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N L

Output

Find the flavor of the apple pie made of the remaining N-1 apples when you optimally choose the apple to eat.

Sample Input 1

5 2

Sample Output 1

18

The flavors of Apple 1, 2, 3, 4, and 5 are 2, 3, 4, 5, and 6, respectively. The optimal choice is to eat Apple 1, so the answer is 3+4+5+6=18.

Sample Input 2

3 -1

Sample Output 2

0

The flavors of Apple 1, 2, and 3 are -1, 0, and 1, respectively. The optimal choice is to eat Apple 2, so the answer is (-1)+1=0.

Sample Input 3

30 -50

Sample Output 3

-1044


program BiiteEating
    implicit none
    integer(4)::n, l, i
    integer(4),allocatable:: t(:)

    read*, n,l

    allocate(t(n))

    do i=1,n
        t(i) = l+i-1
    end do

    if (t(1) < 0 .and. t(n) > 0) then
        print'(i0)', sum(t)
    else if (t(1) > 0) then
        print'(i0)', sum(t) - t(1)
    else
        print'(i0)', sum(t) - t(n)
    end if

end program BiiteEating


program pie
  implicit none
  integer ::N,L,ful,kake,i,sa
  integer :: tabe

  read(*,*) N,L
  ful=L*N+N*(N-1)/2
  sa=abs(L*N)
  do i=1,N
     kake=ful-L-(i-1)
     if(abs(kake-ful).lt.sa) then
        sa=abs(kake-ful)
        tabe=kake
     endif
  enddo
  write(*,'(i0)') tabe

end program pie


program ccc

implicit none
integer(8) :: a, b, c, d, cd, ans

read*, a, b, c, d
call lcm(c,d,cd)

ans=b/c+b/d-b/cd
ans=ans-(a-1)/c-(a-1)/d+(a-1)/cd
ans=(b-a+1)-ans
write(*,'(i0)') ans
end program

subroutine lcm(a,b,res)
integer(8) :: aa, bb, a, b, dumy, i, m, res

aa=a
bb=b

if(a<b) then
dumy=a
a=b
b=dumy
end if

do i = 1, 100000
m=mod(a,b)
if (m==0) then
res=aa*bb/b
a=aa
b=bb
return
else
a=b
b=m
end if
end do

end subroutine lcm

Score : 300 points

Problem Statement

You are given four integers A, B, C, and D. Find the number of integers between A and B (inclusive) that can be evenly divided by neither C nor D.

Constraints

1\leq A\leq B\leq 10^{18}

1\leq C,D\leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C D

Output

Print the number of integers between A and B (inclusive) that can be evenly divided by neither C nor D.

Sample Input 1

4 9 2 3

Sample Output 1

2

5 and 7 satisfy the condition.

Sample Input 2

10 40 6 8

Sample Output 2

23

Sample Input 3

314159265358979323 846264338327950288 419716939 937510582

Sample Output 3

532105071133627368


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

    read(*,*) n
    allocate(x(n),y(n))
    do i=1,n
        read(*,*)x(i),y(i)
    end do
    call m2sort(y,x)
    a=0
    do i=1,n
        a=a+y(i)-x(i)
        if(a<0)then
            write(*,*)'No'
            stop
        end if
    end do
    write(*,*)'Yes'

    stop
    contains
    recursive subroutine m2sort(x,y)
    implicit none
    integer(8), intent(inout) :: x(:),y(:)

    integer(8) :: n, mid, i, j, k
    integer(8), allocatable :: tmp(:),tmp2(:)

    n = size(x)

    if(n == 1) then
      return
    end if

    !
    ! 前半と後半に分けてソート
    ! 1~midとmid+1~nをそれぞれソート済みにする
    !
    mid = n / 2
    call m2sort(x(:mid),y(:mid))
    call m2sort(x(mid+1:),y(mid+1:))

    !
    ! tmpという配列にxを一時的に保管
    ! マージソートは外部ソート
    !
    allocate(tmp(n),tmp2(n))
    tmp(:) = x(:)
    tmp2(:)=y(:)
    ! tmpの後半部分を左右反転
    do k = n, mid+1, -1
      tmp(k) = x(mid + n - k + 1)
      tmp2(k) = y(mid + n - k + 1)
    end do

    ! iは前半のリストのうちまだ見ていないものの最小値
    ! jは後半のリストのうちまだ見ていないものの最小値
    ! kは見た回数の合計
    i = 1
    j = n

    do k = 1, n
      !
      ! 一番左と一番右のうち小さい方をxに入れていく
      ! ここが等号つき不等号なので、安定ソートとなる
      !
      if(tmp(i) <= tmp(j)) then
        x(k) = tmp(i)
        y(k)=tmp2(i)
        i = i + 1
      else
        x(k) = tmp(j)
        y(k)=tmp2(j)
        j = j - 1
      end if
    end do

    deallocate(tmp,tmp2)
    return
  end subroutine m2sort
end program sample

Score: 400 points

Problem Statement

Kizahashi, who was appointed as the administrator of ABC at National Problem Workshop in the Kingdom of AtCoder, got too excited and took on too many jobs.

Let the current time be time 0. Kizahashi has N jobs numbered 1 to N.

It takes A_i units of time for Kizahashi to complete Job i. The deadline for Job i is time B_i, and he must complete the job before or at this time.

Kizahashi cannot work on two or more jobs simultaneously, but when he completes a job, he can start working on another immediately.

Can Kizahashi complete all the jobs in time? If he can, print Yes; if he cannot, print No.

Constraints

All values in input are integers.

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

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

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
.
.
.
A_N B_N

Output

If Kizahashi can complete all the jobs in time, print Yes; if he cannot, print No.

Sample Input 1

5
2 4
1 9
1 8
4 9
3 12

Sample Output 1

Yes

He can complete all the jobs in time by, for example, doing them in the following order:

Do Job 2 from time 0 to 1.

Do Job 1 from time 1 to 3.

Do Job 4 from time 3 to 7.

Do Job 3 from time 7 to 8.

Do Job 5 from time 8 to 11.

Note that it is fine to complete Job 3 exactly at the deadline, time 8.

Sample Input 2

3
334 1000
334 1000
334 1000

Sample Output 2

No

He cannot complete all the jobs in time, no matter what order he does them in.

Sample Input 3

30
384 8895
1725 9791
170 1024
4 11105
2 6
578 1815
702 3352
143 5141
1420 6980
24 1602
849 999
76 7586
85 5570
444 4991
719 11090
470 10708
1137 4547
455 9003
110 9901
15 8578
368 3692
104 1286
3 4
366 12143
7 6649
610 2374
152 7324
4 7042
292 11386
334 5720

Sample Output 3

Yes


program name
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: n,i,time=0
    integer(int32), allocatable:: a(:),b(:)

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

    do i=1,n
        if (time+a(i) > b(i)) then
            print'(a)','No'
            stop
        else
            time=time+a(i)
        end if
    end do
    print'(a)', 'Yes'
contains
    recursive subroutine double_sort(ar1, ar2, fst, lst)
        integer(int32),intent(inout):: ar1(:),ar2(:)
        integer(int32),intent(in):: fst,lst
        integer(int32):: mdl

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

        mdl = (fst+lst)/2

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

    subroutine double_merge_(ar1,ar2,fst,mdl,lst)
        integer(int32),intent(inout):: ar1(:),ar2(:)
        integer(int32),intent(in):: fst,mdl,lst
        integer(int32),allocatable:: t1(:),t2(:)
        integer(int32):: li,ri,ti

        allocate(t1(lst-fst+1), t2(lst-fst+1))

        li=fst
        ri=mdl+1
        ti=1

        do while(li <= mdl .and. ri <= lst)
            if (ar1(li) <= ar1(ri)) then
                t1(ti) = ar1(li)
                t2(ti) = ar2(li)
                li=li+1
            else
                t1(ti) = ar1(ri)
                t2(ti) = ar2(ri)
                ri=ri+1
            end if
            ti=ti+1
        end do

        if (li <= mdl) then
            t1(ti:) = ar1(li:mdl)
            t2(ti:) = ar2(li:mdl)
        else
            t1(ti:) = ar1(ri:lst)
            t2(ti:) = ar2(ri:lst)
        end if

        ar1(fst:lst) = t1(:)
        ar2(fst:lst) = t2(:)

        deallocate(t1,t2)
    end subroutine

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


program main
implicit none
integer :: n
integer , allocatable :: a(:), b(:)
integer :: i, j, s

read(*,*) n
allocate( a(n) )
allocate( b(n) )
do i = 1, n
   read(*,*) a(i), b(i)
end do
call quicksort2(n, b, a, 1, n)
s = 0.
do i = 1, n
   s = s + a(i)
   if(s .gt. b(i) ) then
      write(*,*) 'No'
      stop
   end if
end do
write(*,*) 'Yes'

stop
contains

recursive subroutine quicksort2(n, a, b, first, last)
  implicit none
  integer :: a(n), b(n)
  integer :: first, last
  integer ::  i, j, n
  integer ::  x, y, t, s

  x = a( (first+last) / 2 )
  i = first
  j = last
  do
     do while (a(i) < x)
        i=i+1
     end do
     do while (x < a(j))
        j=j-1
     end do
     if (i >= j) exit
     t = a(i)
    a(i) = a(j)
    a(j) = t
    s = b(i)
    b(i) = b(j)
    b(j) = s
    i=i+1
    j=j-1
  end do
  if (first < i - 1) call quicksort2(n, a, b, first, i - 1)
  if (j + 1 < last)  call quicksort2(n, a, b, j + 1, last)
end subroutine quicksort2

end program main


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

read(*,*) n
allocate( a(n) )
allocate( b(n) )
do i = 1, n
   read(*,*) a(i), b(i)
end do
call heapsort(n, b, a)
do i = 1, n
   if(sum(a(1:i)) .gt. b(i) ) then
      write(*,*) 'No'
      stop
   end if
end do
write(*,*) 'Yes'

stop
contains
subroutine heapsort(n,aa,bb)
  implicit none
  integer,intent(in) :: n
  integer,intent(inout) :: aa(n), bb(n)
  integer ::i,k,j,l
  integer :: t, s

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

end program main


program main
implicit none
integer :: n
integer , allocatable :: a(:), b(:), c(:), d(:)
integer :: i, j

read(*,*) n
allocate( a(n) )
allocate( b(n) )
allocate( c(n) )
allocate( d(n) )
do i = 1, n
   read(*,*) a(i), b(i)
end do
call bubble(n, b, a, d, c)
do i = 1, n

   j = n+1-i
   if( sum(c(1:j)) .gt. d(j))then
      write(*,*) 'No'
      stop
   end if
end do
write(*,*) 'Yes'
stop
contains
  subroutine bubble(n,a,b,aa,bb)
    implicit none
    integer , intent(in) :: n, a(n), b(n)
    integer , intent(out) :: aa(n), bb(n)
    integer :: i, j
    integer :: t, s

    aa = a
    bb = b
    do i = 1, n-1
       do j = i+1 , n
          if(aa(i).gt.aa(j))then
             t = aa(i)
             aa(i) = aa(j)
             aa(j) = t
             s = bb(i)
             bb(i) = bb(j)
             bb(j) = s
          end if
       end do
    end do
  end subroutine bubble

end program main


integer N,K
integer cnt
read*,N,K
if(K>(N-1)*(N-2)/2)then
  print"(i0)",-1
  stop
endif

print"(i0)",(N-1) +((N-1)*(N-2)/2 - K)
do i=1,n-1
  print"(i0,A,i0)",i," ",N
end do

cnt=0
do i=1,n-2
  do j=i+1,n-1
    print"(i0,A,i0)",i," ",j
    cnt=cnt+1
    if( cnt==(N-1)*(N-2)/2 -K )then
      stop
    end if
  end do
end do
end

Score: 500 points

Problem Statement

Does there exist an undirected graph with N vertices satisfying the following conditions?

The graph is simple and connected.

The vertices are numbered 1, 2, ..., N.

Let M be the number of edges in the graph. The edges are numbered 1, 2, ..., M, the length of each edge is 1, and Edge i connects Vertex u_i and Vertex v_i.

There are exactly K pairs of vertices (i,\ j)\ (i < j) such that the shortest distance between them is 2.

If there exists such a graph, construct an example.

Constraints

All values in input are integers.

2 \leq N \leq 100

0 \leq K \leq \frac{N(N - 1)}{2}

Input

Input is given from Standard Input in the following format:

N K

Output

If there does not exist an undirected graph with N vertices satisfying the conditions, print -1.

If there exists such a graph, print an example in the following format (refer to Problem Statement for what the symbols stand for):

M
u_1 v_1
:
u_M v_M

If there exist multiple graphs satisfying the conditions, any of them will be accepted.

Sample Input 1

5 3

Sample Output 1

5
4 3
1 2
3 1
4 5
2 3

This graph has three pairs of vertices such that the shortest distance between them is 2: (1,\ 4), (2,\ 4), and (3,\ 5). Thus, the condition is satisfied.

Sample Input 2

5 8

Sample Output 2

-1

There is no graph satisfying the conditions.


module mod_t_item
  implicit none
  type :: t_item
    integer :: pos
  end type t_item
  private
  public :: t_item, itemize
contains
  function itemize(pos) result(i)
    implicit none
    integer :: pos
    type(t_item) :: i
    i%pos = pos
    return
  end function itemize
end module mod_t_item
module mod_t_list
  use mod_t_item
  implicit none
  type :: t_list
    integer :: vol
    type(t_item), pointer :: arr(:)
  end type t_list
  private
  public :: t_list, init_list, release_list, push_back
  public :: pop_back, get_at, size_of_list
contains
  subroutine init_list(list)
    implicit none
    type(t_list), intent(inout) :: list
    allocate(list%arr(1))
    list%vol = 0
    return
  end subroutine init_list
  subroutine release_list(list)
    implicit none
    type(t_list), intent(inout) :: list
    if (associated(list%arr)) deallocate(list%arr)
    return
  end subroutine release_list
  subroutine push_back(list,item)
    implicit none
    type(t_list), intent(inout) :: list
    type(t_item), intent(in) :: item
    type(t_item), allocatable :: tmp(:)
    if (size(list%arr).eq.list%vol) then
      allocate(tmp(list%vol))
      tmp = list%arr
      deallocate(list%arr)
      allocate(list%arr(2*list%vol))
      list%arr(1:list%vol) = tmp
      deallocate(tmp)
    end if
    list%vol = list%vol+1
    list%arr(list%vol) = item
    return
  end subroutine push_back
  function pop_back(list) result(item)
    implicit none
    type(t_list), intent(inout) :: list
    type(t_item) :: item
    item = list%arr(list%vol)
    list%vol = list%vol-1
    return
  end function pop_back
  function get_at(list,i) result(item)
    implicit none
    type(t_list), intent(in) :: list
    integer, intent(in) :: i
    type(t_item) :: item
    item = list%arr(i)
    return
  end function get_at
  function size_of_list(list) result(s)
    implicit none
    type(t_list), intent(in) :: list
    integer :: s
    s = list%vol
    return
  end function size_of_list
end module mod_t_list
module mod_t_graph
  use mod_t_item
  use mod_t_list
  implicit none
  logical, allocatable :: used(:)
  type :: t_graph
    type(t_list), pointer :: edges(:)
  end type t_graph
  private
  public :: used
  public :: t_graph, init_graph, release_graph, add_edge, add, dfs
contains
  subroutine init_graph(graph,n)
    implicit none
    type(t_graph), intent(inout) :: graph
    integer, intent(in) :: n
    integer :: i
    allocate(used(n))
    used = .false.
    allocate(graph%edges(n))
    do i = 1, n
      call init_list(graph%edges(i))
    end do
    return
  end subroutine init_graph
  subroutine release_graph(graph)
    implicit none
    type(t_graph), intent(inout) :: graph
    integer :: n, i
    deallocate(used)
    n = size(graph%edges)
    do i = 1, n
      call release_list(graph%edges(i))
    end do
    deallocate(graph%edges)
    return
  end subroutine release_graph
  subroutine add_edge(graph,i,edge)
    implicit none
    type(t_graph), intent(inout) :: graph
    type(t_item), intent(in) :: edge
    integer, intent(in) :: i
    call push_back(graph%edges(i),edge)
    return
  end subroutine add_edge
  subroutine add(graph,i,pos)
    implicit none
    type(t_graph), intent(inout) :: graph
    integer, intent(in) :: i, pos
    call push_back(graph%edges(i),itemize(pos))
    return
  end subroutine add
  recursive subroutine dfs(graph,v,x,y)
    implicit none
    type(t_graph), intent(inout) :: graph
    type(t_item) :: e
    integer :: v, x, y, l, i
    if (used(v)) return
    used(v) = .true.
    if (v.gt.100000) then
      y = y+1
    else
      x = x+1
    end if
    l = size_of_list(graph%edges(v))
    if (l.eq.0) return
    do i = 1, l
      e = get_at(graph%edges(v),i)
      call dfs(graph,e%pos,x,y)
    end do
    return
  end subroutine dfs
end module mod_t_graph
program must_be_rectangular
  use mod_t_graph
  implicit none
  type(t_graph) :: graph
  integer :: n, x, y, i
  integer(8) :: m
  call init_graph(graph,200000)
  read(*,*) n
  do i = 1, n
    read(*,*) x, y
    call add(graph,x,100000+y)
    call add(graph,100000+y,x)
  end do
  m = 0_8
  do i = 1, 200000
    if (used(i)) cycle
    x = 0
    y = 0
    call dfs(graph,i,x,y)
    m = m+int(x,8)*int(y,8)
  end do
  write(*,'(i0)') m-int(n,8)
  call release_graph(graph)
  stop
end program must_be_rectangular

Score : 600 points

Problem Statement

There are N dots in a two-dimensional plane. The coordinates of the i-th dot are (x_i, y_i).

We will repeat the following operation as long as possible:

Choose four integers a, b, c, d (a \neq c, b \neq d) such that there are dots at exactly three of the positions (a, b), (a, d), (c, b) and (c, d), and add a dot at the remaining position.

We can prove that we can only do this operation a finite number of times. Find the maximum number of times we can do the operation.

Constraints

1 \leq N \leq 10^5

1 \leq x_i, y_i \leq 10^5

If i \neq j, x_i \neq x_j or y_i \neq y_j.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
:
x_N y_N

Output

Print the maximum number of times we can do the operation.

Sample Input 1

3
1 1
5 1
5 5

Sample Output 1

1

By choosing a = 1, b = 1, c = 5, d = 5, we can add a dot at (1, 5). We cannot do the operation any more, so the maximum number of operations is 1.

Sample Input 2

2
10 10
20 20

Sample Output 2

0

There are only two dots, so we cannot do the operation at all.

Sample Input 3

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

Sample Output 3

16

We can do the operation for all choices of the form a = 1, b = 1, c = i, d = j (2 \leq i,j \leq 5), and no more. Thus, the maximum number of operations is 16.


program main
	implicit none
    integer::a,b
    read(*,*)a,b
    if(a>=b)then
    	write(*,*) 10
    else
    	write(*,*) 0
    end if
    stop
end program main

Score : 100 points

Problem Statement

X and A are integers between 0 and 9 (inclusive).

If X is less than A, print 0; if X is not less than A, print 10.

Constraints

0 \leq X, A \leq 9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X A

Output

If X is less than A, print 0; if X is not less than A, print 10.

Sample Input 1

3 5

Sample Output 1

0

3 is less than 5, so we should print 0.

Sample Input 2

7 5

Sample Output 2

10

7 is not less than 5, so we should print 10.

Sample Input 3

6 6

Sample Output 3

10

6 is not less than 6, so we should print 10.


program main
integer::x,a
read*,x,a
if(x < a) then
print*,'0'
else
print*,'10'
end if
end program main


program ABC
    integer x, a
    read*, x, a
    if (x < a) then
        print"(i0)", 0
    else
        print"(i0)", 10
    endif
end


program name
    implicit none

    integer :: x, a

    read *, x, a
    if(x < a) write(*, fmt='(a)') "0"
    if(x >= a) write(*, fmt='(a)') "10"

    stop

end program name


program main
  implicit none
  integer N,X,L(100),D,i
  read(*, *) N, X
  read(*, *) (L(i), i = 1,N)
  D = 0
  do i = 1,N
    D = D+L(i)
    if (D>X) then
      write(*, *) i
      exit
    else if(i == N) then
      write(*, *) i
      exit
    end if
  end do
end program main

Score : 200 points

Problem Statement

A ball will bounce along a number line, making N + 1 bounces. It will make the first bounce at coordinate D_1 = 0, and the i-th bounce (2 \leq i \leq N+1) at coordinate D_i = D_{i-1} + L_{i-1}.

How many times will the ball make a bounce where the coordinate is at most X?

Constraints

1 \leq N \leq 100

1 \leq L_i \leq 100

1 \leq X \leq 10000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X
L_1 L_2 ... L_{N-1} L_N

Output

Print the number of times the ball will make a bounce where the coordinate is at most X.

Sample Input 1

3 6
3 4 5

Sample Output 1

2

The ball will make a bounce at the coordinates 0, 3, 7 and 12, among which two are less than or equal to 6.

Sample Input 2

4 9
3 3 3 3

Sample Output 2

4

The ball will make a bounce at the coordinates 0, 3, 6, 9 and 12, among which four are less than or equal to 9.


program prob5
    implicit none
    integer(16) :: W, H, x, y
    character(len=100) :: S
    read(*,*) W, H, x, y

    write(S,'(f44.20)') real(W, kind=16)*real(H, kind=16)/2.0_8

    write(*,'(a)',advance='no') trim(adjustl(S))

    if(2*x == W .and. 2*y == H) then
        write(*,*) ' 1'
    else
        write(*,*) ' 0'
    end if

    stop
contains
end program prob5

Score : 300 points

Problem Statement

There is a rectangle in a coordinate plane. The coordinates of the four vertices are (0,0), (W,0), (W,H), and (0,H).
You are given a point (x,y) which is within the rectangle or on its border. We will draw a straight line passing through (x,y) to cut the rectangle into two parts. Find the maximum possible area of the part whose area is not larger than that of the other. Additionally, determine if there are multiple ways to cut the rectangle and achieve that maximum.

Constraints

1 \leq W,H \leq 10^9

0\leq x\leq W

0\leq y\leq H

All values in input are integers.

Input

Input is given from Standard Input in the following format:

W H x y

Output

Print the maximum possible area of the part whose area is not larger than that of the other, followed by 1 if there are multiple ways to cut the rectangle and achieve that maximum, and 0 otherwise.

The area printed will be judged correct when its absolute or relative error is at most 10^{-9}.

Sample Input 1

2 3 1 2

Sample Output 1

3.000000 0

The line x=1 gives the optimal cut, and no other line does.

Sample Input 2

2 2 1 1

Sample Output 2

2.000000 1


program prob5
    implicit none
    integer(8) :: W, H, x, y
    character(len=100) :: S
    read(*,*) W, H, x, y

    write(S,'(f24.12)') real(W, kind=8)*real(H, kind=8)/2.0_8

    write(*,'(a)',advance='no') trim(adjustl(S))

    if(W==0 .or. H==0) then
        write(*,*) ' 0'
        stop
    end if
    if( mod(W, 2) == 0 .and. mod(H, 2) == 0 .and. x == W/2 .and. y == H/2) then
        write(*,*) ' 1'
    else
        write(*,*) ' 0'
    end if

    stop
contains
end program prob5


program main
  implicit none
  integer(8) :: w, h, x, y, fuku
  real(8) :: half_area
  read (*, *) w, h, x, y
  half_area = w * h * 0.5
  if (x * 2 == w .and. y * 2 == h) then
    fuku = 1
  else
    fuku = 0
  end if
  write (*, "(f0.1, ' ', i0)") half_area, fuku
end program main


program cut
  implicit none
  integer(8) :: w, h, x, y, s1, s2, ans, equality
  read *, w, h, x, y
  equality = 0
  s1 = minval((/x, w-x/))*h
  s2 = minval((/y, h-y/))*w
  ans = minval((/s1, s2/))
  if (s1 == s2) equality = 1
  print '(i0,a,i0)', ans," ", equality
  stop
end program cut


integer(8) W,H,x,y
real(16) ans
read*,W,H,x,y
ans=W*H/2.0
print"(f0.10,A,i0)",ans," ",merge(1,0,2*x==w .and. 2*y==H)
end


program main
  implicit none
  integer :: x, y, w, h
  real :: area
  integer :: flag

  read(*,*) w, h, x, y
  area = w*h/2.0

  if (mod(w,2)/=0 .or. mod(h,2)/=0) then
    flag = 0
  else
    if (x == w/2 .and. y == h/2) then
      flag = 1
    else
      flag = 0
    endif
  endif

  write(*,*) area, flag

end program main


program abc130c
  integer :: w, h, x, y
  read *, w, h, x, y
  print *, real(w)*real(h)/2, merge(1,0,x*2==w.and.y*2==h)
end program abc130c


program kadai
implicit none
integer  ::  w,h,x,y
real(8)  ::  ans
real(8)  ::  down,up,left,right

read(*,*) w,h,x,y

if (mod(h,2)==0 .and. y==h/2) then
    w=real(w,8)
    h=real(h,8)
    ans=w*h/2.0_8
    if (mod(w,2)==0 .and. x==w/2) then
        write(*,*) ans,1
        stop
    end if
    write(*,*) ans,0
    stop
end if

if (mod(w,2)==0 .and. x==w/2) then
    w=real(w,8)
    h=real(h,8)
    ans=w*h/2.0_8
    write(*,*) ans,0
    stop
end if

x=real(x,8)
w=real(w,8)
h=real(h,8)
y=real(y,8)
down=x-(w-2.0_8*x)*y/(h-2.0_8*y)
up=x+(w-2.0_8*x)*(h-y)/(h-2.0_8*y)
if (down<=w .and. down>=0) then
    ans=(up+down)*h/2.0_8
    if (ans>w*h/2.0_8) then
        ans=w*h-ans
    end if
    write(*,*) ans,0
 !write(*,*) up,down
    stop
end if

if (down>w .or. down<0) then
    right=y+(h-2.0_8*y)*(w-x)/(w-2.0_8*x)
    left=y-(h-2.0_8*y)*x/(w-2.0_8*x)
    ans=w*(right+left)/2.0_8
    if (ans>w*h/2.0_8) then
        ans=w*h-ans
       ! write(*,*) "bbbb"
    end if
    write(*,*) ans,0
    stop
end if

end program kadai


program cutting
implicit none
integer w, h, x, y
integer res
real(4) ss, ww, hh, xx, yy, mm, nn ,ms
read*, w, h, x, y
xx=x
yy=y
ww=w
ww=ww/2
hh=h
hh=h/2
if((xx.eq.ww).and.(yy.eq.hh)) then
res=1
else if ((xx.ne.ww).or.(yy.ne.hh)) then
res=0
end if
ss=w*h
ss=ss/2
ss=w*h-ss
mm=max(ww,hh)
nn=min(ww,hh)
mm=mm-1
ms=mm*nn
if(((ss-ms)/ss).lt.(0.00001)) then
res=1
end if
print*, ss, res
end program


program cutting
implicit none
integer w, h, x, y
integer res
real s, ww, hh
read*, w, h, x, y
ww=w
ww=ww/2
hh=h
hh=h/2
if((x.eq.ww).and.(y.eq.hh)) then
res=1
else if ((x.ne.ww).or.(y.ne.hh)) then
res=0
end if
s=w*h
s=s/2
s=w*h-s
print*, s, res
end program


program main
  implicit none
  real(8) W,H,x,y,ans1
  integer ans2
  read(*, *)W,H,x,y
  ans1 = W*H*0.5
  if(2*x == W .and. 2*y == H) then
    ans2 = 1
  else
    ans2 = 0
  end if
  write(* ,*)ans1,ans2
end program main


module mod_mint
  implicit none
  integer(8) :: modul
  type :: mint
    integer(8) :: n
  end type mint
  interface assignment(=)
    module procedure assign1, assign2, assign3_1, assign3_2, assign4_1, assign4_2
  end interface assignment(=)
  interface operator(+)
    module procedure add0, add1_1, add1_2, add2_1, add2_2
  end interface operator(+)
  interface operator(-)
    module procedure sub0, sub1_1, sub1_2, sub2_1, sub2_2
  end interface operator(-)
  interface operator(*)
    module procedure mul0, mul1_1, mul1_2, mul2_1, mul2_2
  end interface operator(*)
  interface operator(/)
    module procedure div0, div1_1, div1_2, div2_1, div2_2
  end interface operator(/)
  interface operator(**)
    module procedure pow1_1, pow1_2
  end interface operator(**)
  private
  public :: mint
  public :: init, show, inv
  public :: assignment(=), operator(+), operator(-)
  public :: operator(*), operator(/), operator(**)
contains
  subroutine init(m)
    implicit none
    integer(8), intent(in) :: m
    modul = m
    return
  end subroutine init
  subroutine show(a)
    implicit none
    type(mint), intent(in) :: a
    write(*,'(i0)') a%n
    return
  end subroutine show
  subroutine assign1(a,b)
    implicit none
    type(mint), intent(inout) :: a
    integer(8), intent(in) :: b
    integer(8) :: c
    c = b
    do while (c.lt.0_8)
      c = c + modul
    end do
    a%n = mod(c,modul)
    return
  end subroutine assign1
  subroutine assign2(a,b)
    implicit none
    type(mint), intent(inout) :: a
    integer, intent(in) :: b
    call assign1(a,int(b,8))
    return
  end subroutine assign2
  subroutine assign3_1(a,b)
    implicit none
    type(mint), intent(inout) :: a(:)
    integer(8), intent(in) :: b
    integer :: n, i
    n = size(a)
    do i = 1, n
      call assign1(a(i),b)
    end do
    return
  end subroutine assign3_1
  subroutine assign3_2(a,b)
    implicit none
    type(mint), intent(inout) :: a(:)
    integer, intent(in) :: b
    call assign3_1(a,int(b,8))
    return
  end subroutine assign3_2
  subroutine assign4_1(a,b)
    implicit none
    type(mint), intent(inout) :: a(:,:)
    integer(8), intent(in) :: b
    integer :: n, i
    n = size(a(:,1))
    do i = 1, n
      call assign3_1(a(i,:),b)
    end do
    return
  end subroutine assign4_1
  subroutine assign4_2(a,b)
    implicit none
    type(mint), intent(inout) :: a(:,:)
    integer, intent(in) :: b
    call assign4_1(a,int(b,8))
    return
  end subroutine assign4_2
  function add0(a,b) result(r)
    implicit none
    type(mint), intent(in) :: a, b
    type(mint) :: r
    r%n = mod(a%n+b%n,modul)
    return
  end function add0
  function add1_1(a,b) result(r)
    implicit none
    type(mint), intent(in) :: a
    integer(8), intent(in) :: b
    type(mint) :: r
    call assign1(r,b)
    r = add0(a,r)
    return
  end function add1_1
  function add1_2(a,b) result(r)
    implicit none
    type(mint), intent(in) :: a
    integer, intent(in) :: b
    type(mint) :: r
    call assign2(r,b)
    r = add0(a,r)
    return
  end function add1_2
  function add2_1(a,b) result(r)
    implicit none
    integer(8), intent(in) :: a
    type(mint), intent(in) :: b
    type(mint) :: r
    call assign1(r,a)
    r = add0(r,b)
    return
  end function add2_1
  function add2_2(a,b) result(r)
    implicit none
    integer, intent(in) :: a
    type(mint), intent(in) :: b
    type(mint) :: r
    call assign2(r,a)
    r = add0(r,b)
    return
  end function add2_2
  function sub0(a,b) result(r)
    implicit none
    type(mint), intent(in) :: a, b
    type(mint) :: r
    r%n = mod(modul+a%n-b%n,modul)
    return
  end function sub0
  function sub1_1(a,b) result(r)
    implicit none
    type(mint), intent(in) :: a
    integer(8), intent(in) :: b
    type(mint) :: r
    call assign1(r,b)
    r = sub0(a,r)
    return
  end function sub1_1
  function sub1_2(a,b) result(r)
    implicit none
    type(mint), intent(in) :: a
    integer, intent(in) :: b
    type(mint) :: r
    call assign2(r,b)
    r = sub0(a,r)
    return
  end function sub1_2
  function sub2_1(a,b) result(r)
    implicit none
    integer(8), intent(in) :: a
    type(mint), intent(in) :: b
    type(mint) :: r
    call assign1(r,a)
    r = sub0(r,b)
    return
  end function sub2_1
  function sub2_2(a,b) result(r)
    implicit none
    integer, intent(in) :: a
    type(mint), intent(in) :: b
    type(mint) :: r
    call assign2(r,a)
    r = sub0(r,b)
    return
  end function sub2_2
  function mul0(a,b) result(r)
    implicit none
    type(mint), intent(in) :: a, b
    type(mint) :: r
    r%n = mod(a%n*b%n,modul)
    return
  end function mul0
  function mul1_1(a,b) result(r)
    implicit none
    type(mint), intent(in) :: a
    integer(8), intent(in) :: b
    type(mint) :: r
    call assign1(r,b)
    r = mul0(a,r)
    return
  end function mul1_1
  function mul1_2(a,b) result(r)
    implicit none
    type(mint), intent(in) :: a
    integer, intent(in) :: b
    type(mint) :: r
    call assign2(r,b)
    r = mul0(a,r)
    return
  end function mul1_2
  function mul2_1(a,b) result(r)
    implicit none
    integer(8), intent(in) :: a
    type(mint), intent(in) :: b
    type(mint) :: r
    call assign1(r,a)
    r = mul0(r,b)
    return
  end function mul2_1
  function mul2_2(a,b) result(r)
    implicit none
    integer, intent(in) :: a
    type(mint), intent(in) :: b
    type(mint) :: r
    call assign2(r,a)
    r = mul0(r,b)
    return
  end function mul2_2
  function div0(a,b) result(r)
    implicit none
    type(mint), intent(in) :: a, b
    type(mint) :: r
    r = mul0(a,inv(b))
    return
  end function div0
  function div1_1(a,b) result(r)
    implicit none
    type(mint), intent(in) :: a
    integer(8), intent(in) :: b
    type(mint) :: r
    call assign1(r,b)
    r = div0(a,r)
    return
  end function div1_1
  function div1_2(a,b) result(r)
    implicit none
    type(mint), intent(in) :: a
    integer, intent(in) :: b
    type(mint) :: r
    call assign2(r,b)
    r = div0(a,r)
    return
  end function div1_2
  function div2_1(a,b) result(r)
    implicit none
    integer(8), intent(in) :: a
    type(mint), intent(in) :: b
    type(mint) :: r
    call assign1(r,a)
    r = div0(r,b)
    return
  end function div2_1
  function div2_2(a,b) result(r)
    implicit none
    integer, intent(in) :: a
    type(mint), intent(in) :: b
    type(mint) :: r
    call assign2(r,a)
    r = div0(r,b)
    return
  end function div2_2
  function inv(n) result(m)
    implicit none
    type(mint), intent(in) :: n
    type(mint) :: m
    integer(8) :: a, b, x, y, t, q
    a = n%n
    b = modul
    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
    call assign1(m,y)
    return
  end function inv
  recursive function pow1_1(a,b) result(r)
    implicit none
    type(mint), intent(in) :: a
    integer(8), intent(in) :: b
    type(mint) :: r
    if (b.eq.0_8) then
      call assign2(r,1)
      return
    end if
    if (mod(b,2_8).eq.0_8) then
      r = pow1_1(a,b/2_8)
      r = mul0(r,r)
      return
    end if
    r = mul0(a,pow1_1(a,b-1_8))
    return
  end function pow1_1
  function pow1_2(a,b) result(r)
    implicit none
    type(mint), intent(in) :: a
    integer, intent(in) :: b
    type(mint) :: r
    r = pow1_1(a,int(b,8))
    return
  end function pow1_2
end module mod_mint
program common_subsequence
  use mod_mint
  implicit none
  integer :: n, m, s(2000), t(2000), i, j
  type(mint) :: dp(0:2000,0:2000)
  call init(1000000007_8)
  s = 0
  t = 0
  dp = 0
  read(*,*) n, m
  read(*,*) s(1:n)
  read(*,*) t(1:m)
  do i = 1, n
    do j = 1, m
      if (s(i).eq.t(j)) then
        dp(i,j) = 1+dp(i,j-1)+dp(i-1,j)
      else
        dp(i,j) = dp(i,j-1)+dp(i-1,j)-dp(i-1,j-1)
      end if
    end do
  end do
  call show(dp(n,m)+1)
  stop
end program common_subsequence

Score : 500 points

Problem Statement

You are given two integer sequences S and T of length N and M, respectively, both consisting of integers between 1 and 10^5 (inclusive).

In how many pairs of a subsequence of S and a subsequence of T do the two subsequences are the same in content?

Here the subsequence of A is a sequence obtained by removing zero or more elements from A and concatenating the remaining elements without changing the order.

For both S and T, we distinguish two subsequences if the sets of the indices of the removed elements are different, even if the subsequences are the same in content.

Since the answer can be tremendous, print the number modulo 10^9+7.

Constraints

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

The length of S is N.

The length of T is M.

1 \leq S_i, T_i \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
S_1 S_2 ... S_{N-1} S_{N}
T_1 T_2 ... T_{M-1} T_{M}

Output

Print the number of pairs of a subsequence of S and a subsequence of T such that the subsequences are the same in content, modulo 10^9+7.

Sample Input 1

2 2
1 3
3 1

Sample Output 1

3

S has four subsequences: (), (1), (3), (1, 3).

T has four subsequences: (), (3), (1), (3, 1).

There are 1 \times 1 pair of subsequences in which the subsequences are both (), 1 \times 1 pair of subsequences in which the subsequences are both (1), and 1 \times 1 pair of subsequences in which the subsequences are both (3), for a total of three pairs.

Sample Input 2

2 2
1 1
1 1

Sample Output 2

6

S has four subsequences: (), (1), (1), (1, 1).

T has four subsequences: (), (1), (1), (1, 1).

There are 1 \times 1 pair of subsequences in which the subsequences are both (), 2 \times 2 pairs of subsequences in which the subsequences are both (1), and 1 \times 1 pair of subsequences in which the subsequences are both (1,1), for a total of six pairs.
Note again that we distinguish two subsequences if the sets of the indices of the removed elements are different, even if the subsequences are the same in content.

Sample Input 3

4 4
3 4 5 6
3 4 5 6

Sample Output 3

16

Sample Input 4

10 9
9 6 5 7 5 9 8 5 6 7
8 6 8 5 5 7 9 9 7

Sample Output 4

191

Sample Input 5

20 20
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Sample Output 5

846527861

Be sure to print the number modulo 10^9+7.


program prob41
    implicit none
    integer :: n,k
    read(*,*) n,k
    if(k /= 1) then
        write(*,*) n-k
    else
        write(*,*) "0"
    end if
    stop
contains
end program prob41

Score : 100 points

Problem Statement

Takahashi is distributing N balls to K persons.

If each person has to receive at least one ball, what is the maximum possible difference in the number of balls received between the person with the most balls and the person with the fewest balls?

Constraints

1 \leq K \leq N \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the maximum possible difference in the number of balls received.

Sample Input 1

3 2

Sample Output 1

1

The only way to distribute three balls to two persons so that each of them receives at least one ball is to give one ball to one person and give two balls to the other person.

Thus, the maximum possible difference in the number of balls received is 1.

Sample Input 2

3 1

Sample Output 2

0

We have no choice but to give three balls to the only person, in which case the difference in the number of balls received is 0.

Sample Input 3

8 5

Sample Output 3

3

For example, if we give 1, 4, 1, 1, 1 balls to the five persons, the number of balls received between the person with the most balls and the person with the fewest balls would be 3, which is the maximum result.


integer N
integer,allocatable,dimension(:)::X
integer(8),allocatable,dimension(:)::ansx,ansy
integer(8)minuscnt,tmp
read*,N
allocate(X(N))
allocate(ansx(N-1),ansy(N-1))
read*,X
call heapsort(N,X)

minuscnt=0
do i=1,N
  if(x(i)<0)minuscnt=minuscnt+1
end do

if(minuscnt>=1.and. N-minuscnt>=1)then
  ansx(1)=X(1)
  ansy(1)=X(N-1)
  do i=2,(N-minuscnt)-2
    ansx(i)=ansx(i-1)-ansy(i-1)
    ansy(i)=ansy(minuscnt+i)
  end do
    ansy(N-minuscnt-1)=ansx(N-minuscnt-2)-ansy(N-minuscnt-2)
    ansx(N-minuscnt-1)=X(N)
  do i=N-minuscnt,N-1
    ansx(i)=ansx(i-1)+ansy(i-1)
    ansy(i)=ansy(N+1-i+1)
  end do
else
  ansx(1)=X(1)
  ansy(1)=X(N-1)
  do i=2,N-2
    ansx(i)=ansx(i-1)-ansy(i-1)
    ansy(i)=X(i)
  end do
  ansx(N-1)=X(N)
  ansy(N-1)=ansx(N-2)-ansy(N-2)
end if

ans=ansx(N-1)-ansy(N-1)
if(ans<0)then
  tmp=ansx(N-1)
  ansx(N-1)=ansy(N-1)
  ansy(N-1)=tmp
  ans=-ans
endif

print"(i0)",ansx(N-1)-ansy(N-1)
do i=1,N-1
  print"(i0,A,i0)",ansx(i)," ",ansy(i)
end do
contains

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

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


module merge_sort_mod
    use,intrinsic :: iso_fortran_env
    implicit none
    private
    public:: merge_sort, double_merge_sort
    interface merge_sort
        module procedure ms32, ms64
    end interface

    interface double_merge_sort
        module procedure msd3232, msd6464
    end interface

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

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

        mdl = (fst+lst)/2
        call ms32(ar, fst, mdl)
        call ms32(ar, mdl+1, lst)
        call merge32(ar, fst, mdl, lst)
    end subroutine

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

        allocate(tmp(lst-fst+1))

        li=fst
        ri=mdl+1
        ti=1

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

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

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

    subroutine swap32(x,y)
        integer(int32),intent(inout):: x,y
        integer(int32):: tmp
        tmp = x
        x = y
        y = tmp
    end subroutine

    recursive subroutine ms64(ar, fst, lst)
        integer(int64),intent(inout):: ar(:)
        integer(int64),intent(in):: fst,lst
        integer(int64):: mdl

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

        mdl = (fst+lst)/2
        call ms64(ar, fst, mdl)
        call ms64(ar, mdl+1, lst)
        call merge64(ar, fst, mdl, lst)
    end subroutine

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

        allocate(tmp(lst-fst+1))

        li=fst
        ri=mdl+1
        ti=1

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

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

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

    subroutine swap64(x,y)
        integer(int64),intent(inout):: x,y
        integer(int64):: tmp
        tmp = x
        x = y
        y = tmp
    end subroutine

    recursive subroutine msd3232(ar1, ar2, fst, lst)
        integer(int32),intent(inout):: ar1(:),ar2(:)
        integer(int32),intent(in):: fst,lst
        integer(int32):: mdl

        if (lst - fst < 2) then
            if (ar1(fst) > ar1(lst)) then
                call swap32(ar1(fst), ar1(lst))
                call swap32(ar2(fst), ar2(lst))
            end if
            return
        end if

        mdl = (fst+lst)/2

        call msd3232(ar1,ar2,fst,mdl)
        call msd3232(ar1,ar2,mdl+1,lst)
        call merged3232(ar1,ar2,fst,mdl,lst)
    end subroutine

    subroutine merged3232(ar1,ar2,fst,mdl,lst)
        integer(int32),intent(inout):: ar1(:),ar2(:)
        integer(int32),intent(in):: fst,mdl,lst
        integer(int32),allocatable:: t1(:),t2(:)
        integer(int32):: li,ri,ti

        allocate(t1(lst-fst+1), t2(lst-fst+1))

        li=fst
        ri=mdl+1
        ti=1

        do while(li <= mdl .and. ri <= lst)
            if (ar1(li) <= ar1(ri)) then
                t1(ti) = ar1(li)
                t2(ti) = ar2(li)
                li=li+1
            else
                t1(ti) = ar1(ri)
                t2(ti) = ar2(ri)
                ri=ri+1
            end if
            ti=ti+1
        end do

        if (li <= mdl) then
            t1(ti:) = ar1(li:mdl)
            t2(ti:) = ar2(li:mdl)
        else
            t1(ti:) = ar1(ri:lst)
            t2(ti:) = ar2(ri:lst)
        end if

        ar1(fst:lst) = t1(:)
        ar2(fst:lst) = t2(:)

        deallocate(t1,t2)
    end subroutine

    recursive subroutine msd6464(ar1, ar2, fst, lst)
        integer(int64),intent(inout):: ar1(:),ar2(:)
        integer(int64),intent(in):: fst,lst
        integer(int64):: mdl

        if (lst - fst < 2) then
            if (ar1(fst) > ar1(lst)) then
                call swap64(ar1(fst), ar1(lst))
                call swap64(ar2(fst), ar2(lst))
            end if
            return
        end if

        mdl = (fst+lst)/2

        call msd6464(ar1,ar2,fst,mdl)
        call msd6464(ar1,ar2,mdl+1,lst)
        call merged6464(ar1,ar2,fst,mdl,lst)
    end subroutine

    subroutine merged6464(ar1,ar2,fst,mdl,lst)
        integer(int64),intent(inout):: ar1(:),ar2(:)
        integer(int64),intent(in):: fst,mdl,lst
        integer(int64),allocatable:: t1(:),t2(:)
        integer(int64):: li,ri,ti

        allocate(t1(lst-fst+1), t2(lst-fst+1))

        li=fst
        ri=mdl+1
        ti=1

        do while(li <= mdl .and. ri <= lst)
            if (ar1(li) <= ar1(ri)) then
                t1(ti) = ar1(li)
                t2(ti) = ar2(li)
                li=li+1
            else
                t1(ti) = ar1(ri)
                t2(ti) = ar2(ri)
                ri=ri+1
            end if
            ti=ti+1
        end do

        if (li <= mdl) then
            t1(ti:) = ar1(li:mdl)
            t2(ti:) = ar2(li:mdl)
        else
            t1(ti:) = ar1(ri:lst)
            t2(ti:) = ar2(ri:lst)
        end if

        ar1(fst:lst) = t1(:)
        ar2(fst:lst) = t2(:)

        deallocate(t1,t2)
    end subroutine
end module

program diverta2019_2_b
    use,intrinsic :: iso_fortran_env
    use merge_sort_mod
    implicit none
    integer(int32):: n,i,j,p,q,cost
    integer(int32), allocatable:: x(:), y(:)

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

    do i=1,n
        read*, x(i), y(i)
    end do
    cost = 1000
    do i=1,n-1
        do j=i+1,n
            p = x(j)-x(i)
            q = y(j)-y(i)
            cost = min(cost, calc_cost(p,q))
        end do
    end do
    print'(i0)', cost
contains
    function calc_cost(p,q) result(ret)
        integer(int32):: p,q,ret
        integer(int32):: i,j
        ret = n

        do i=1,n-1
            do j=1,n
                if (x(j)-x(i) == p .and. y(j)-y(i) == q)ret=ret-1
            end do
        end do
    end function
end program diverta2019_2_b

Score : 300 points

Problem Statement

There are N balls in a two-dimensional plane. The i-th ball is at coordinates (x_i, y_i).

We will collect all of these balls, by choosing two integers p and q such that p \neq 0 or q \neq 0 and then repeating the following operation:

Choose a ball remaining in the plane and collect it. Let (a, b) be the coordinates of this ball. If we collected a ball at coordinates (a - p, b - q) in the previous operation, the cost of this operation is 0. Otherwise, including when this is the first time to do this operation, the cost of this operation is 1.

Find the minimum total cost required to collect all the balls when we optimally choose p and q.

Constraints

1 \leq N \leq 50

|x_i|, |y_i| \leq 10^9

If i \neq j, x_i \neq x_j or y_i \neq y_j.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
:
x_N y_N

Output

Print the minimum total cost required to collect all the balls.

Sample Input 1

2
1 1
2 2

Sample Output 1

1

If we choose p = 1, q = 1, we can collect all the balls at a cost of 1 by collecting them in the order (1, 1), (2, 2).

Sample Input 2

3
1 4
4 6
7 8

Sample Output 2

1

If we choose p = -3, q = -2, we can collect all the balls at a cost of 1 by collecting them in the order (7, 8), (4, 6), (1, 4).

Sample Input 3

4
1 1
1 2
2 1
2 2

Sample Output 3

2


program picking_up
  implicit none
  integer :: n, x(50), y(50), i, j, k, l, p, q, m, t
  x = 0
  y = 0
  read(*,*) n
  do i = 1, n
    read(*,*) x(i), y(i)
  end do
  m = 0
  do i = 1, n
    do j = 1, n
      if (j.eq.i) cycle
      p = x(i)-x(j)
      q = y(i)-y(j)
      t = 0
      do k = 1, n
        do l = 1, n
        if ((x(k)-x(l).eq.p).and.(y(k)-y(l).eq.q)) t = t+1
        end do
      end do
      if (m.lt.t) m = t
    end do
  end do
  write(*,'(i0)') n-m
  stop
end program picking_up


program prob6
    implicit none
    integer, allocatable::a(:), ans(:,:)
    integer::n, i, j
    read(*,*) n
    allocate(a(n),ans(n,2))
    read(*,*) a
    call hsort(n,1,1,a)
    if(a(n) <= 0) then
        do i = 1, n-1
            ans(i,1) = a(n)
            ans(i,2) = a(n-1)
            a(n) = a(n) - a(n-1)
        end do
    else if(a(1) >= 0 .and. a(n) >= 0) then
        do i = 1, n-2
            ans(i,1) = a(1)
            ans(i,2) = a(i+1)
            a(1) = a(1) - a(i+1)
        end do
        ans(n-1,1) = a(n)
        ans(n-1,2) = a(1)
        a(n) = a(n) - a(1)
    else
        do i = 1, n
            if(a(i) >= 0) then
                do j = i, n-1
                    ans(j-i+1,1) = a(1)
                    ans(j-i+1,2) = a(j)
                    a(1) = a(1) - a(j)
                end do
                do j = 1, i-1
                    ans(n-i+j,1) = a(n)
                    ans(n-i+j,2) = a(j)
                    a(n) = a(n) - a(j)
                end do
                exit
            end if
        end do
    end if
    write(*,*) a(n)
    do i = 1, n-1
        write(*,*) ans(i,1), ans(i,2)
    end do
    deallocate(a, ans)
    stop
contains
    ! n行m列の配列をp列目でsort
    ! 1次元配列ならm=p=1
    subroutine hsort(n,m,p,array)
        implicit none
        integer,intent(in)::n, m, p
        integer,intent(inout)::array(n,m)

        integer ::i,k,j,l
        integer::t(m)

        if(n.le.0)then
        write(6,*)"Error, at heapsort"; stop
        end if
        if(n.eq.1)return

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

        return
    end subroutine hsort
end program

Score : 500 points

Problem Statement

There are N integers, A_1, A_2, ..., A_N, written on a blackboard.

We will repeat the following operation N-1 times so that we have only one integer on the blackboard.

Choose two integers x and y on the blackboard and erase these two integers. Then, write a new integer x-y.

Find the maximum possible value of the final integer on the blackboard and a sequence of operations that maximizes the final integer.

Constraints

2 \leq N \leq 10^5

-10^4 \leq A_i \leq 10^4

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible value M of the final integer on the blackboard, and a sequence of operations x_i, y_i that maximizes the final integer, in the format below.

Here x_i and y_i represent the integers x and y chosen in the i-th operation, respectively.

If there are multiple sequences of operations that maximize the final integer, any of them will be accepted.

M
x_1 y_1
:
x_{N-1} y_{N-1}

Sample Input 1

3
1 -1 2

Sample Output 1

4
-1 1
2 -2

If we choose x = -1 and y = 1 in the first operation, the set of integers written on the blackboard becomes (-2, 2).

Then, if we choose x = 2 and y = -2 in the second operation, the set of integers written on the blackboard becomes (4).

In this case, we have 4 as the final integer. We cannot end with a greater integer, so the answer is 4.

Sample Input 2

3
1 1 1

Sample Output 2

1
1 1
1 0


program prob6
    implicit none
    integer, allocatable::a(:), ans(:,:)
    integer::n, i, j
    read(*,*) n
    allocate(a(n),ans(n,2))
    read(*,*) a
    call hsort(n,1,1,a)
    if(a(n) <= 0) then
        do i = 1, n-1
            ans(i,1) = a(n)
            ans(i,2) = a(n-1)
            a(n) = a(n) - a(n-1)
        end do
    else if(a(n) >= 0) then
        do i = 1, n-2
            ans(i,1) = a(1)
            ans(i,2) = a(i+1)
            a(1) = a(1) - a(i+1)
        end do
        ans(n-1,1) = a(n)
        ans(n-1,2) = a(1)
        a(n) = a(n) - a(1)
    else
        do i = 1, n
            if(a(i) >= 0) then
                do j = i, n-1
                    ans(j-i+1,1) = a(1)
                    ans(j-i+1,2) = a(j)
                    a(1) = a(1) - a(j)
                end do
                do j = 1, i-1
                    ans(n-i+j,1) = a(n)
                    ans(n-i+j,2) = a(j)
                    a(n) = a(n) - a(j)
                end do
                exit
            end if
        end do
    end if
    write(*,*) a(n)
    do i = 1, n-1
        write(*,*) ans(i,1), ans(i,2)
    end do
    deallocate(a, ans)
    stop
contains
    ! n行m列の配列をp列目でsort
    ! 1次元配列ならm=p=1
    subroutine hsort(n,m,p,array)
        implicit none
        integer,intent(in)::n, m, p
        integer,intent(inout)::array(n,m)

        integer ::i,k,j,l
        integer::t(m)

        if(n.le.0)then
        write(6,*)"Error, at heapsort"; stop
        end if
        if(n.eq.1)return

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

        return
    end subroutine hsort
end program


integer N
integer,allocatable,dimension(:)::X
integer,allocatable,dimension(:)::ansx,ansy
integer ans
read*,N
allocate(X(N))
read*,X
call heapsort(N,X)

allocate(ansx(N-1),ansy(N-1))
if(mod(N,2)==1)then
  ansx(1)=x(1)
  ansy(1)=x(N)
  do i=2,N-1
    if(mod(i,2)==0)then
      ansx(i)=x(1+i/2)
    else
      ansx(i)=x(N-i/2)
    endif
      ansy(i)=ansx(i-1)-ansy(i-1)
  end do
else
  ansx(1)=x(N)
  ansy(1)=x(1)
  do i=2,N-1
    if(mod(i,2)==0)then
    ansx(i)=x(N-i/2)
  else
    ansx(i)=x(1+i/2)
  endif
    ansy(i)=ansx(i-1)-ansy(i-1)
  end do
endif

ans=ansx(N-1)-ansy(N-1)

print"(i0)",ans
do i=1,N-1
  print"(i0,A,i0)",ansx(i)," ",ansy(i)
end do
contains

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

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


Score : 900 points

Problem Statement

Diverta City is a new city consisting of N towns numbered 1, 2, ..., N.

The mayor Ringo is planning to connect every pair of two different towns with a bidirectional road. The length of each road is undecided.

A Hamiltonian path is a path that starts at one of the towns and visits each of the other towns exactly once.
The reversal of a Hamiltonian path is considered the same as the original Hamiltonian path.

There are N! / 2 Hamiltonian paths. Ringo wants all these paths to have distinct total lengths (the sum of the lengths of the roads on a path), to make the city diverse.

Find one such set of the lengths of the roads, under the following conditions:

The length of each road must be a positive integer.

The maximum total length of a Hamiltonian path must be at most 10^{11}.

Constraints

N is a integer between 2 and 10 (inclusive).

Input

Input is given from Standard Input in the following format:

N

Output

Print a set of the lengths of the roads that meets the objective, in the following format:

w_{1, 1} \ w_{1, 2} \ w_{1, 3} \ ... \ w_{1, N}
w_{2, 1} \ w_{2, 2} \ w_{2, 3} \ ... \ w_{2, N}
: : :
w_{N, 1} \ w_{N, 2} \ w_{N, 3} \ ... \ w_{N, N}

where w_{i, j} is the length of the road connecting Town i and Town j, which must satisfy the following conditions:

w_{i, i} = 0

w_{i, j} = w_{j, i} \ (i \neq j)

1 \leq w_{i, j} \leq 10^{11} \ (i \neq j)

If there are multiple sets of lengths of the roads that meet the objective, any of them will be accepted.

Sample Input 1

3

Sample Output 1

0 6 15
6 0 21
15 21 0

There are three Hamiltonian paths. The total lengths of these paths are as follows:

1 → 2 → 3: The total length is 6 + 21 = 27.

1 → 3 → 2: The total length is 15 + 21 = 36.

2 → 1 → 3: The total length is 6 + 15 = 21.

They are distinct, so the objective is met.

Sample Input 2

4

Sample Output 2

0 111 157 193
111 0 224 239
157 224 0 258
193 239 258 0

There are 12 Hamiltonian paths, with distinct total lengths.


program Airplane
    implicit none
    integer(4):: p,q,r
    read*, p,q,r
    print*, p+q+r - max(max(p,q),r)
end program Airplane

Score : 100 points

Problem Statement

There are three airports A, B and C, and flights between each pair of airports in both directions.

A one-way flight between airports A and B takes P hours, a one-way flight between airports B and C takes Q hours, and a one-way flight between airports C and A takes R hours.

Consider a route where we start at one of the airports, fly to another airport and then fly to the other airport.

What is the minimum possible sum of the flight times?

Constraints

1 \leq P,Q,R \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

P Q R

Output

Print the minimum possible sum of the flight times.

Sample Input 1

1 3 4

Sample Output 1

4

The sum of the flight times in the route A \rightarrow B \rightarrow C: 1 + 3 = 4 hours

The sum of the flight times in the route A \rightarrow C \rightarrow C: 4 + 3 = 7 hours

The sum of the flight times in the route B \rightarrow A \rightarrow C: 1 + 4 = 5 hours

The sum of the flight times in the route B \rightarrow C \rightarrow A: 3 + 4 = 7 hours

The sum of the flight times in the route C \rightarrow A \rightarrow B: 4 + 1 = 5 hours

The sum of the flight times in the route C \rightarrow B \rightarrow A: 3 + 1 = 4 hours

The minimum of these is 4 hours.

Sample Input 2

3 2 3

Sample Output 2

5


program Balance
    implicit none
    integer(4):: n, mi, i
    integer(4),allocatable:: w(:)

    read*, n
    allocate(w(n))
    read*, w(:)

    mi = 100000

    do i=1,n-1
        mi = min(mi, abs(sum(w(1:i))-sum(w(i+1:n))))
    end do

    print*, mi

end program Balance

Score : 200 points

Problem Statement

We have N weights indexed 1 to N. The mass of the weight indexed i is W_i.

We will divide these weights into two groups: the weights with indices not greater than T, and those with indices greater than T, for some integer 1 \leq T < N. Let S_1 be the sum of the masses of the weights in the former group, and S_2 be the sum of the masses of the weights in the latter group.

Consider all possible such divisions and find the minimum possible absolute difference of S_1 and S_2.

Constraints

2 \leq N \leq 100

1 \leq W_i \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
W_1 W_2 ... W_{N-1} W_N

Output

Print the minimum possible absolute difference of S_1 and S_2.

Sample Input 1

3
1 2 3

Sample Output 1

0

If T = 2, S_1 = 1 + 2 = 3 and S_2 = 3, with the absolute difference of 0.

Sample Input 2

4
1 3 1 1

Sample Output 2

2

If T = 2, S_1 = 1 + 3 = 4 and S_2 = 1 + 1 = 2, with the absolute difference of 2. We cannot have a smaller absolute difference.

Sample Input 3

8
27 23 76 2 3 5 62 52

Sample Output 3

2


program main
    implicit none
    integer :: n, w(100), s1, s2, i, j, k, ms

    read(*,*)n
    read(*,*)(w(i),i = 1, n)
    ms = 1000000

    do j = 1, k-1
      s1 = 0
      s2 = 0
      do i = 1, j
        s1 = s1 + w(i)
      end do
      do i = j+1, n
        s2 = s2 + w(i)
      end do
      ms = min(ms, abs(s1-s2))
    end do

    write(*,*)ms
end program main


integer N
integer,allocatable,dimension(:)::P
integer mae,ushiro
integer ans
read*,N
allocate(P(N))
read*,P
mae=0
ushiro=sum(P)
ans=huge(ans)
do i=1,N
  mae=mae+p(i)
  ushiro=ushiro-p(i)
  ans=min(ans,abs(mae-ushiro))
end do
print"(I0)",ans
end


      PROGRAM TypicalStairs
      IMPLICIT NONE
      integer(16) :: n,m,a(10**5)
      integer(16) :: i,pattern(-1:10**5),aCnt=1

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

      pattern = 0
      !i=1
      if( a(aCnt)==1 )then
        pattern(1) = 0
        aCnt = aCnt + 1
      else
        pattern(1) = 1
      end if

      !i=2
      if( a(aCnt)==2 )then
        pattern(2) = 0
        aCnt = aCnt + 1
      else
        pattern(2) = pattern(1) + 1
      end if

      do i = 3,n
        if( a(aCnt)==i )then
          pattern(i) = 0
          aCnt = aCnt + 1
        else
          pattern(i) = pattern(i-2) + pattern(i-1)
          pattern(i) = mod(pattern(i),10**9+7)
        end if
      end do

      print*,pattern(n)
  !    print*,pattern(1:n)

      END PROGRAM

Score : 300 points

Problem Statement

There is a staircase with N steps. Takahashi is now standing at the foot of the stairs, that is, on the 0-th step.
He can climb up one or two steps at a time.

However, the treads of the a_1-th, a_2-th, a_3-th, \ldots, a_M-th steps are broken, so it is dangerous to set foot on those steps.

How many are there to climb up to the top step, that is, the N-th step, without setting foot on the broken steps?
Find the count modulo 1\ 000\ 000\ 007.

Constraints

1 \leq N \leq 10^5

0 \leq M \leq N-1

1 \leq a_1 < a_2 < ... < a_M \leq N-1

Input

Input is given from Standard Input in the following format:

N M
a_1
a_2
.
.
.
a_M

Output

Print the number of ways to climb up the stairs under the condition, modulo 1\ 000\ 000\ 007.

Sample Input 1

6 1
3

Sample Output 1

4

There are four ways to climb up the stairs, as follows:

0 \to 1 \to 2 \to 4 \to 5 \to 6

0 \to 1 \to 2 \to 4 \to 6

0 \to 2 \to 4 \to 5 \to 6

0 \to 2 \to 4 \to 6

Sample Input 2

10 2
4
5

Sample Output 2

0

There may be no way to climb up the stairs without setting foot on the broken steps.

Sample Input 3

100 5
1
23
45
67
89

Sample Output 3

608200469

Be sure to print the count modulo 1\ 000\ 000\ 007.


program ccc

implicit none

integer :: n, m, i
integer,allocatable,dimension(:) :: a, dp

read*, n, m
allocate(a(m))
allocate(dp(0:n))
if(m/=0) then
read*, a
else
a=0
end if

dp=1

do i=1,m
dp(a(i))=0
end do

do i=2,n
if(dp(i)==1) then
dp(i)=mod(dp(i-2)+dp(i-1),1000000007)
end if
end do

write(*,'(i0)') dp(n)

end program


program main
 integer(16) n,m,i,p
 integer(16), allocatable :: a(:),pat(:)

 p=0
 read(*,*) n,m
 if(m==0)then
  p=-1
 end if

 if(p==0)then
  allocate(a(1:m),pat(1:n+2))
  read(*,*) a
  do i=1,n
   pat(i)=0
  end do

  pat(a(1)+1)=-1
  do i=2,m
   if(a(i)==a(i-1)+1)then
    p=-1
    exit
   end if
   pat(a(i)+1) = -1
  end do

  pat(1)=1
  if(p==0)then
   do i=1,n
    if(pat(i)/=-1)then
     if(pat(i+1)/=-1)then
      pat(i+1)=mod(pat(i+1)+pat(i),1000000007)
     end if
     if(pat(i+2)/=-1)then
      pat(i+2)=mod(pat(i+2)+pat(i),1000000007)
     end if
    end if
   end do
  end if
 end if

 write(*,*) pat(n+1)
end program main


module ABC129

  ! <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  :: task_C

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

  subroutine task_C

    ! variables for this <subroutine>
    integer(INT32) :: num_stairs_total
    integer(INT32) :: num_stairs_broken
    integer(INT64) :: sum_ways

    ! arrays for this <subroutine>
    integer(INT32), allocatable :: index_broken_stair(:)
    integer(INT64), allocatable :: list_sum_ways(:)

    ! support variables for this <subroutine>
    integer(INT32) :: itr

    ! STEP.01
    ! read out the number of total & broken stairs
    read(unit=INPUT_UNIT, fmt=*) num_stairs_total, num_stairs_broken

    ! STEP.02
    ! allocate the array to store ...
    ! 1. the indexes of broken stairs
    ! 2. how many are there to climb up to the `itr`-th step
    allocate( index_broken_stair(1:num_stairs_broken) )
    allocate( list_sum_ways     (1:num_stairs_total ) )

    ! STEP.03
    ! read out indexes of broken stairs
    do itr = 1_INT32, num_stairs_broken, 1_INT32
      read(unit=INPUT_UNIT, fmt=*) index_broken_stair(itr)
    end do

    ! STEP.04
    ! check which step can not be stepped
    list_sum_ways(1)                  =  1_INT64
    list_sum_ways(2)                  =  2_INT64
    list_sum_ways(3:num_stairs_total) = -1_INT64

    do itr = 1_INT32, num_stairs_broken, 1_INT32
      list_sum_ways( index_broken_stair(itr) ) = 0_INT64
    end do

    ! STEP.05
    ! calculate how many are there to climb up to the top step
    do itr = 3_INT32, num_stairs_total, 1_INT32
      if ( list_sum_ways(itr) .eq. 0_INT64 ) then
        continue
      else
        list_sum_ways(itr) = list_sum_ways(itr-2) + list_sum_ways(itr-1)
      end if
    end do

    if ( sum_ways .ne. 0_INT64 ) sum_ways = mod( list_sum_ways(num_stairs_total), 1000000007_INT64 )

    ! STEP.06
    ! output how many are there to climb up to the top step
    write(unit=OUTPUT_UNIT, fmt='(I0)', advance='yes') sum_ways

    ! STEP.07
    ! deallocate the array to store the indexes of broken stairs
    deallocate( index_broken_stair )
    deallocate( list_sum_ways      )

    ! STEP.END
    return

  end subroutine task_C

end module ABC129

program main

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

  ! require all variables to be explicitly declared
  implicit none

  call task_C

end program main