module ABC101

  ! <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
  private :: calculate_min_num_operations

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

  subroutine task_C

    ! variables for this <subroutine>
    integer(INT32) :: len_sequence
    integer(INT32) :: num_elements_replacing

    ! STEP.01
    ! read out ...
    ! 1. the length of the given sequence
    ! 2. the number of elements to replace at once
    read(unit=INPUT_UNIT, fmt=*) len_sequence, num_elements_replacing

    ! STEP.02
    ! calculate & output minumum value of the number of operations that can be realized in a given sequence
    write(unit=OUTPUT_UNIT, fmt='(I0)', advance='yes') calculate_min_num_operations(len_sequence, num_elements_replacing)

    ! STEP.END
    return

  end subroutine task_C

  pure function calculate_min_num_operations (len_sequence, num_elements_replacing) result(num_operations)

    ! arguments for this <function>
    integer(INT32), intent(in) :: len_sequence
    integer(INT32), intent(in) :: num_elements_replacing

    ! return value of this <function>
    integer(INT32) :: num_operations

    num_operations &!
      = ceiling( &!
        real(len_sequence           - 1_INT32, kind=REAL64) / &!
        real(num_elements_replacing - 1_INT32, kind=REAL64),  &!
        kind=INT32                                            &!
      )

      return

  end function calculate_min_num_operations

end module ABC101

program main

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

  ! require all variables to be explicitly declared
  implicit none

  call task_C

end program main

Score : 300 points

Problem Statement

There is a sequence of length N: A_1, A_2, ..., A_N. Initially, this sequence is a permutation of 1, 2, ..., N.

On this sequence, Snuke can perform the following operation:

Choose K consecutive elements in the sequence. Then, replace the value of each chosen element with the minimum value among the chosen elements.

Snuke would like to make all the elements in this sequence equal by repeating the operation above some number of times.
Find the minimum number of operations required.
It can be proved that, Under the constraints of this problem, this objective is always achievable.

Constraints

2 \leq K \leq N \leq 100000

A_1, A_2, ..., A_N is a permutation of 1, 2, ..., N.

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 ... A_N

Output

Print the minimum number of operations required.

Sample Input 1

4 3
2 3 1 4

Sample Output 1

2

One optimal strategy is as follows:

In the first operation, choose the first, second and third elements. The sequence A becomes 1, 1, 1, 4.

In the second operation, choose the second, third and fourth elements. The sequence A becomes 1, 1, 1, 1.

Sample Input 2

3 3
1 2 3

Sample Output 2

1

Sample Input 3

8 3
7 3 1 8 4 6 2 5

Sample Output 3

4


program main
	implicit none
	integer N, K, i, m, n1, l1, S, l, S1, S2
	integer, allocatable :: A(:)
	read(*, *) N, K
	allocate(A(1: N))
	read(*, *) A(1: N)
	S = 0
	S1 = 0
	S2 = 0
	do i = 1, N
		if(A(i) == 1) then
			l = i
			exit
		end if
	end do
	m = 1
	do i = 1, K
		if((l-K+i) == 1) then
			m = -K+i
		end if
	end do
	n1 = -1
	do i = 1, K
		if((l+K-i) == 1) then
			n1 = K-i
		end if
	end do
	if(n1-m+1==N) then
		write(*, *) 1
	else if(m /= 1 .or. n1 /= -1) then
		do i = 1, 10000000
			N = N - K + 1
			S = S + 1
			if(N < 2) then
				write(*, *) S
				exit
			end if
		end do
	else
	l1 = l
		do i = 1, 10000000
			l1 = l1 - K + 1
			S1 = S1 + 1
			if(N < 2) then
				exit
			end if
		end do
		l = (N-l+1)
		do i = 1, 10000000
			l = (N-l+1) - K + 1
			S2 = S2 + 1
			if(N < 2) then
				exit
			end if
		end do
		write(*, *) S1+S2
	end if
end program main

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

    integer(8) :: i,j,m,n,a,b,c,k
    integer(8),allocatable :: x(:),y(:)

    read(*,*) n,k
    allocate(x(n))
    read(*,*)x
    do i=1,n
        if(x(i)==1)then
            a=i
            exit
        endif
    end do
    k=k-1
    j=0
    if(mod(a-1,k)==0)then
        b=(a-1)/k
        c=0
    else
        b=(a-1)/k+1
        c=mod(a-1,k)
        j=1
    end if
    if(mod(n-a,k)==0)then
        b=b+(n-a)/k
    else
        b=b+(n-a)/k+1
        c=c+mod(n-a,k)
        j=j+1
    end if
    if(j==2)then
        b=b-1
    end if

    write(*,*) b
    stop
end program sample


program main
    implicit none
    integer(4):: k,n
    integer(4),allocatable:: a(:)

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

    print*, nint(dble(n-k)/dble(k-1))+1
end program main


program atcoder
  implicit none
  integer a, b
  read *, a, b    ! 変数mに、標準入力から数字を読み込み代入する
  if (a.lt.9 .and. b.lt.9) then
  write(*,*) 'Yay!'
  else
  write(*,*) ':('
  end if    !  ","のあとに解答となる変数を書く
end program atcoder

Score: 100 points

Problem Statement

E869120's and square1001's 16-th birthday is coming soon.

Takahashi from AtCoder Kingdom gave them a round cake cut into 16 equal fan-shaped pieces.

E869120 and square1001 were just about to eat A and B of those pieces, respectively,

when they found a note attached to the cake saying that "the same person should not take two adjacent pieces of cake".

Can both of them obey the instruction in the note and take desired numbers of pieces of cake?

Constraints

A and B are integers between 1 and 16 (inclusive).

A+B is at most 16.

Input

Input is given from Standard Input in the following format:

A B

Output

If both E869120 and square1001 can obey the instruction in the note and take desired numbers of pieces of cake, print Yay!; otherwise, print :(.

Sample Input 1

5 4

Sample Output 1

Yay!

Both of them can take desired number of pieces as follows:

Sample Input 2

8 8

Sample Output 2

Yay!

Both of them can take desired number of pieces as follows:

Sample Input 3

11 4

Sample Output 3

:(

In this case, there is no way for them to take desired number of pieces, unfortunately.


program Ringo
	integer D, N
    read(*, *) D, N
    if (N.NE.100) then
    X = (100**D)*N
    print '(i0)', int(X)
    else if (N.EQ.100) then
    X = (100**D)*(N + 1)
	end if
end program


program HappyBirthday
	integer A, B, X
    read(*, *)A, B
    X = max(A, B)
    if (X.LT.9) then
    print *, "yay!"
    else if (X.GE.9) then
    print *, ":("
	end if
end program


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

read(*,*) n
allocate(a(n))
read(*,*) a
k = 0
do i = 1, n
   j = a(i)
   do while( mod(j,2) == 0 )
      k = k + 1
      j = j/2
   end do
end do
write(*,'(i0)') k

end program main

Score: 300 points

Problem Statement

As AtCoder Beginner Contest 100 is taking place, the office of AtCoder, Inc. is decorated with a sequence of length N, a = {a_1, a_2, a_3, ..., a_N}.

Snuke, an employee, would like to play with this sequence.

Specifically, he would like to repeat the following operation as many times as possible:

For every i satisfying 1 \leq i \leq N, perform one of the following: "divide a_i by 2" and "multiply a_i by 3".
Here, choosing "multiply a_i by 3" for every i is not allowed, and the value of a_i after the operation must be an integer.

At most how many operations can be performed?

Constraints

N is an integer between 1 and 10 \ 000 (inclusive).

a_i is an integer between 1 and 1 \ 000 \ 000 \ 000 (inclusive).

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 a_3 ... a_N

Output

Print the maximum number of operations that Snuke can perform.

Sample Input 1

3
5 2 4

Sample Output 1

3

The sequence is initially {5, 2, 4}. Three operations can be performed as follows:

First, multiply a_1 by 3, multiply a_2 by 3 and divide a_3 by 2. The sequence is now {15, 6, 2}.

Next, multiply a_1 by 3, divide a_2 by 2 and multiply a_3 by 3. The sequence is now {45, 3, 6}.

Finally, multiply a_1 by 3, multiply a_2 by 3 and divide a_3 by 2. The sequence is now {135, 9, 3}.

Sample Input 2

4
631 577 243 199

Sample Output 2

0

No operation can be performed since all the elements are odd. Thus, the answer is 0.

Sample Input 3

10
2184 2126 1721 1800 1024 2528 3360 1945 1280 1776

Sample Output 3

39


program abc100c
  implicit none
  integer n
  integer,allocatable,dimension(:) :: a

  read *, n
  allocate(a(n))
  read *, a
  print '(i0)', sum(trailz(a))
end program abc100c


program patisserie_abc
  implicit none
  integer :: n, m, i
  integer(8) :: r(1000,3), x(3)
  r = 0_8
  read(*,*) n, m
  if (m.eq.0) then
    write(*,'(i0)') 0
    stop
  end if
  do i = 1, n
    read(*,*) r(i,:)
  end do
  do i = 1, 3
    call merge_sort(n,r(1:n,:),i)
    x(i) = abs(sum(r(1:m,1)))+abs(sum(r(1:m,2)))+abs(sum(r(1:m,3)))
  end do
  write(*,'(i0)') maxval(x)
  stop
contains
  subroutine merge_sort(n,c,idx)
    implicit none
    integer, intent(in)       :: n, idx
    integer(8), intent(inout) :: c(n,3)
    integer                   :: number, length, i, ubnd
    number = n
    length = 1
    do while (number.gt.1)
      do i = 1, number/2
        ubnd = min(2*i*length,n)
        call merge(c(2*(i-1)*length+1:(2*i-1)*length,:),c((2*i-1)*length+1:ubnd,:),idx)
      end do
      length = 2*length
      number = (number+1)/2
    end do
    return
  end subroutine merge_sort
  subroutine merge(c1,c2,idx)
    implicit none
    integer(8), intent(inout) :: c1(:,:), c2(:,:)
    integer, intent(in)       :: idx
    integer(8)                :: c(size(c1(:,1))+size(c2(:,1)),size(c1(1,:)))
    integer                   :: i1, i2, n1, n2
    i1 = 1
    i2 = 1
    n1 = size(c1(:,1))
    n2 = size(c2(:,1))
    do while ((i1.le.n1).and.(i2.le.n2))
      if (c1(i1,idx).le.c2(i2,idx)) then
        c(i1+i2-1,:) = c1(i1,:)
        i1 = i1+1
      else
        c(i1+i2-1,:) = c2(i2,:)
        i2 = i2+1
      end if
    end do
    if (i1.le.n1) then
      c(i1+i2-1:n1+n2,:) = c1(i1:n1,:)
    else if (i2 .le. n2) then
      c(i1+i2-1:n1+n2,:) = c2(i2:n2,:)
    end if
    c1 = c(1:n1,:)
    c2 = c(n1+1:n1+n2,:)
    return
  end subroutine merge
end program patisserie_abc

Score: 400 points

Problem Statement

Takahashi became a pastry chef and opened a shop La Confiserie d'ABC to celebrate AtCoder Beginner Contest 100.

The shop sells N kinds of cakes.

Each kind of cake has three parameters "beauty", "tastiness" and "popularity". The i-th kind of cake has the beauty of x_i, the tastiness of y_i and the popularity of z_i.

These values may be zero or negative.

Ringo has decided to have M pieces of cakes here. He will choose the set of cakes as follows:

Do not have two or more pieces of the same kind of cake.

Under the condition above, choose the set of cakes to maximize (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity).

Find the maximum possible value of (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity) for the set of cakes that Ringo chooses.

Constraints

N is an integer between 1 and 1 \ 000 (inclusive).

M is an integer between 0 and N (inclusive).

x_i, y_i, z_i \ (1 \leq i \leq N) are integers between -10 \ 000 \ 000 \ 000 and 10 \ 000 \ 000 \ 000 (inclusive).

Input

Input is given from Standard Input in the following format:

N M
x_1 y_1 z_1
x_2 y_2 z_2
: :
x_N y_N z_N

Output

Print the maximum possible value of (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity) for the set of cakes that Ringo chooses.

Sample Input 1

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

Sample Output 1

56

Consider having the 2-nd, 4-th and 5-th kinds of cakes. The total beauty, tastiness and popularity will be as follows:

Beauty: 1 + 3 + 9 = 13

Tastiness: 5 + 5 + 7 = 17

Popularity: 9 + 8 + 9 = 26

The value (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity) here is 13 + 17 + 26 = 56. This is the maximum value.

Sample Input 2

5 3
1 -2 3
-4 5 -6
7 -8 -9
-10 11 -12
13 -14 15

Sample Output 2

54

Consider having the 1-st, 3-rd and 5-th kinds of cakes. The total beauty, tastiness and popularity will be as follows:

Beauty: 1 + 7 + 13 = 21

Tastiness: (-2) + (-8) + (-14) = -24

Popularity: 3 + (-9) + 15 = 9

The value (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity) here is 21 + 24 + 9 = 54. This is the maximum value.

Sample Input 3

10 5
10 -80 21
23 8 38
-94 28 11
-26 -2 18
-69 72 79
-26 -86 -54
-72 -50 59
21 65 -32
40 -94 87
-62 18 82

Sample Output 3

638

If we have the 3-rd, 4-th, 5-th, 7-th and 10-th kinds of cakes, the total beauty, tastiness and popularity will be -323, 66 and 249, respectively.

The value (the absolute value of the total beauty) + (the absolute value of the total tastiness) + (the absolute value of the total popularity) here is 323 + 66 + 249 = 638. This is the maximum value.

Sample Input 4

3 2
2000000000 -9000000000 4000000000
7000000000 -5000000000 3000000000
6000000000 -1000000000 8000000000

Sample Output 4

30000000000

The values of the beauty, tastiness and popularity of the cakes and the value to be printed may not fit into 32-bit integers.


program sample
	implicit none
    integer :: N

    read(*,*) N

    if (N<=999) then
    	write(*,*) 'ABC'
    else
    	write(*,*) 'ABD'
	end if

    stop
end program sample

Score : 100 points

Problem Statement

Decades have passed since the beginning of AtCoder Beginner Contest.

The contests are labeled as ABC001, ABC002, ... from the first round, but after the 999-th round ABC999, a problem occurred: how the future rounds should be labeled?

In the end, the labels for the rounds from the 1000-th to the 1998-th are decided: ABD001, ABD002, ..., ABD999.

You are given an integer N between 1 and 1998 (inclusive). Print the first three characters of the label of the N-th round of AtCoder Beginner Contest.

Constraints

1 \leq N \leq 1998

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the first three characters of the label of the N-th round of AtCoder Beginner Contest.

Sample Input 1

999

Sample Output 1

ABC

The 999-th round of AtCoder Beginner Contest is labeled as ABC999.

Sample Input 2

1000

Sample Output 2

ABD

The 1000-th round of AtCoder Beginner Contest is labeled as ABD001.

Sample Input 3

1481

Sample Output 3

ABD

The 1481-th round of AtCoder Beginner Contest is labeled as ABD482.


program prob20
    implicit none
    integer :: n
    read(*,*) n
    if(n >= 1000) then
        write(*,'(a)') "ABD"
    else
        write(*,'(a)') "ABC"
    end if
    stop
contains
end program prob20


program main
  implicit none

  integer :: a,b,c,s,i

  read(*,*)a,b
  c = b-a-1
  s = 0
  do i = 1, c
    s = s + i
  end do

  write(*,*)s-a
end program main

Score : 200 points

Problem Statement

In some village, there are 999 towers that are 1,(1+2),(1+2+3),...,(1+2+3+...+999) meters high from west to east, at intervals of 1 meter.

It had been snowing for a while before it finally stopped. For some two adjacent towers located 1 meter apart, we measured the lengths of the parts of those towers that are not covered with snow, and the results are a meters for the west tower, and b meters for the east tower.

Assuming that the depth of snow cover and the altitude are the same everywhere in the village, find the amount of the snow cover.

Assume also that the depth of the snow cover is always at least 1 meter.

Constraints

1 \leq a < b < 499500(=1+2+3+...+999)

All values in input are integers.

There is no input that contradicts the assumption.

Input

Input is given from Standard Input in the following format:

a b

Output

If the depth of the snow cover is x meters, print x as an integer.

Sample Input 1

8 13

Sample Output 1

2

The heights of the two towers are 10 meters and 15 meters, respectively.
Thus, we can see that the depth of the snow cover is 2 meters.

Sample Input 2

54 65

Sample Output 2

1


Score : 200 points

Problem Statement

Takahashi has two positive integers A and B.

It is known that A plus B equals N.
Find the minimum possible value of "the sum of the digits of A" plus "the sum of the digits of B" (in base 10).

Constraints

2 ≤ N ≤ 10^5

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the minimum possible value of "the sum of the digits of A" plus "the sum of the digits of B".

Sample Input 1

15

Sample Output 1

6

When A=2 and B=13, the sums of their digits are 2 and 4, which minimizes the value in question.

Sample Input 2

100000

Sample Output 2

10


program interval_game
  implicit none
  integer :: n, l(100001), r(100001), i
  integer(8) :: k
  l = 0
  r = 0
  k = 0_8
  read(*,*) n
  do i = 1, n
    read(*,*) l(i), r(i)
  end do
  call bitonic_sort(l(1:n+1),.false.)
  call bitonic_sort(r(1:n+1),.true.)
  do i = 1, n+1
    k = k+2_8*max(0_8,int(l(i)-r(i),8))
  end do
  write(*,'(i0)') k
  stop
contains
  recursive subroutine bitonic_sort(a,ascend)
    implicit none
    integer, intent(inout) :: a(:)
    logical, intent(in) :: ascend
    integer :: n
    n = size(a)
    if (n.eq.1) return
    call bitonic_sort(a(1:n/2),.not.ascend)
    call bitonic_sort(a(n/2+1:n),ascend)
    call bitonic_merge(a,ascend)
    return
  end subroutine bitonic_sort
  recursive subroutine bitonic_merge(a,ascend)
    implicit none
    integer, intent(inout) :: a(:)
    logical, intent(in) :: ascend
    integer :: n, m, i, b
    n = size(a)
    if (n.eq.1) return
    m = 1
    do while (2*m.lt.n)
      m = 2*m
    end do
    do i = 1, n-m
      if ((a(i).gt.a(i+m)).eqv.ascend) then
        b = a(i)
        a(i) = a(i+m)
        a(i+m) = b
      end if
    end do
    call bitonic_merge(a(1:m),ascend)
    call bitonic_merge(a(m+1:n),ascend)
    return
  end subroutine bitonic_merge
end program interval_game

Score : 700 points

Problem Statement

Takahashi and Aoki will play a game with a number line and some segments.
Takahashi is standing on the number line and he is initially at coordinate 0.
Aoki has N segments. The i-th segment is [L_i,R_i], that is, a segment consisting of points with coordinates between L_i and R_i (inclusive).

The game has N steps. The i-th step proceeds as follows:

First, Aoki chooses a segment that is still not chosen yet from the N segments and tells it to Takahashi.

Then, Takahashi walks along the number line to some point within the segment chosen by Aoki this time.

After N steps are performed, Takahashi will return to coordinate 0 and the game ends.

Let K be the total distance traveled by Takahashi throughout the game. Aoki will choose segments so that K will be as large as possible, and Takahashi walks along the line so that K will be as small as possible. What will be the value of K in the end?

Constraints

1 ≤ N ≤ 10^5

-10^5 ≤ L_i < R_i ≤ 10^5

L_i and R_i are integers.

Input

Input is given from Standard Input in the following format:

N
L_1 R_1
:
L_N R_N

Output

Print the total distance traveled by Takahashi throughout the game when Takahashi and Aoki acts as above.
It is guaranteed that K is always an integer when L_i,R_i are integers.

Sample Input 1

3
-5 1
3 7
-4 -2

Sample Output 1

10

One possible sequence of actions of Takahashi and Aoki is as follows:

Aoki chooses the first segment. Takahashi moves from coordinate 0 to -4, covering a distance of 4.

Aoki chooses the third segment. Takahashi stays at coordinate -4.

Aoki chooses the second segment. Takahashi moves from coordinate -4 to 3, covering a distance of 7.

Takahashi moves from coordinate 3 to 0, covering a distance of 3.

The distance covered by Takahashi here is 14 (because Takahashi didn't move optimally).
It turns out that if both players move optimally, the distance covered by Takahashi will be 10.

Sample Input 2

3
1 2
3 4
5 6

Sample Output 2

12

Sample Input 3

5
-2 0
-2 0
7 8
9 10
-2 -1

Sample Output 3

34


program main
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: a,b
    read*, a,b
    print'(i0)', max(a+b,a-b,a*b)
end program main

Score : 100 points

Problem Statement

You are given two integers A and B.
Find the largest value among A+B, A-B and A \times B.

Constraints

-1000 \leq A,B \leq 1000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the largest value among A+B, A-B and A \times B.

Sample Input 1

3 1

Sample Output 1

4

3+1=4, 3-1=2 and 3 \times 1=3. The largest among them is 4.

Sample Input 2

4 -2

Sample Output 2

6

The largest is 4 - (-2) = 6.

Sample Input 3

0 0

Sample Output 3

0


program main
      implicit integer(a-z)

      read(*,*)a,b

      ans=max(a+b,a-b,a*b)

      write(*,*)ans

end program main


program name
    implicit none
    character(:),allocatable:: s
    integer(4):: n,i,cnt

    read*, n
    allocate(character(n):: s)
    read*, s
    cnt = 0
    do i=1,n-1
        cnt = max(cnt,snum(s(1:i), s(i+1:n)))
    end do
    print*, cnt

contains
function snum(s1, s2) result(ret)
    implicit none
    character(*):: s1, s2
    integer(4):: ret,i
    logical:: is1(ichar('a'):ichar('z')),is2(ichar('a'):ichar('z'))

    is1(:) = .false.
    is2(:) = .false.
    do i=1,len(s1)
        is1(ichar(s1(i:i))) = .true.
    end do
    do i=1,len(s2)
        is2(ichar(s2(i:i))) = .true.
    end do

    ret = 0
    do i=ichar('a'),ichar('z')
        if (is1(i) .and. is2(i)) ret=ret+1
    end do
end function snum
end program name

Score : 200 points

Problem Statement

You are given a string S of length N consisting of lowercase English letters.
We will cut this string at one position into two strings X and Y.
Here, we would like to maximize the number of different letters contained in both X and Y.
Find the largest possible number of different letters contained in both X and Y when we cut the string at the optimal position.

Constraints

2 \leq N \leq 100

|S| = N

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the largest possible number of different letters contained in both X and Y.

Sample Input 1

6
aabbca

Sample Output 1

2

If we cut the string between the third and fourth letters into X = aab and Y = bca, the letters contained in both X and Y are a and b.
There will never be three or more different letters contained in both X and Y, so the answer is 2.

Sample Input 2

10
aaaaaaaaaa

Sample Output 2

1

However we divide S, only a will be contained in both X and Y.

Sample Input 3

45
tgxgdqkyjzhyputjjtllptdfxocrylqfqjynmfbfucbir

Sample Output 3

9


INTEGER N
CHARACTER(100) S
CHARACTER(26)::A="abcdefghijklmnopqrstuvwxyz"
INTEGER B,P
INTEGER M,U
READ*,N,S
P=0
DO I=1, N-1
  B=0
  DO J=1,26
    M=SCAN(S,A(J:J))
    IF(M==0)CYCLE
    U=SCAN(S,A(J:J),.TRUE.)
    IF( M<=I .AND. I<U )B=B+1
  END DO
  P=MAX(P,B)
END DO
PRINT"(I0)",P
END


program sample
  implicit none
  integer :: n,i,smax,ans
  integer,allocatable :: a(:),b(:)
  character(100) :: s
  allocate(a(n))
  allocate(b(n))
  read(*,*) n
  read(*,*) s

  do i=1,n
     if(s(i:i)=='W')then
        a(i)=-1
     else
        a(i)=1
     endif
  enddo

  write(*,*) a

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

  smax=maxval(b)
  ans=((n-1)-smax)/2

  write(*,*) ans

end program sample

Score : 300 points

Problem Statement

There are N people standing in a row from west to east.
Each person is facing east or west.
The directions of the people is given as a string S of length N.
The i-th person from the west is facing east if S_i = E, and west if S_i = W.

You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader.
Here, we do not care which direction the leader is facing.

The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized.
Find the minimum number of people who have to change their directions.

Constraints

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

|S| = N

S_i is E or W.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the minimum number of people who have to change their directions.

Sample Input 1

5
WEEWW

Sample Output 1

1

Assume that we appoint the third person from the west as the leader.
Then, the first person from the west needs to face east and has to turn around.
The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case.
It is not possible to have 0 people who have to change their directions, so the answer is 1.

Sample Input 2

12
WEWEWEEEWWWE

Sample Output 2

4

Sample Input 3

8
WWWWWEEE

Sample Output 3

3


program main
  implicit none
  integer :: n, i, nout, mi
  character(300000) :: s

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

  nout = 0
  do i = 2, n
    if (s(i:i) == "E") then
      nout = nout + 1
    end if
  end do

  mi = nout
  do i = 2, n
    if (s(i:i) == "E") then
      nout = nout - 1
    end if
    if (s(i - 1:i - 1) == "W") then
      nout = nout + 1
    end if
    mi = min(mi, nout)
  end do
  write(*, "(i0)") mi
end program main


program main
implicit None
integer(8)::a,b,c,k
read *,a,b,c,k

if(abs(b-a) <= 10e18)then
	print 101,(b-a)*((-1)**(2-mod(k,2)))
	else
	print 102,"Unfair"
endif

101 format(i0)
102 format(a)
end program main

Score : 300 points

Problem Statement

Takahashi, Nakahashi and Hikuhashi have integers A, B and C, respectively.
After repeating the following operation K times, find the integer Takahashi will get minus the integer Nakahashi will get:

Each of them simultaneously calculate the sum of the integers that the other two people have, then replace his own integer with the result.

However, if the absolute value of the answer exceeds 10^{18}, print Unfair instead.

Constraints

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

0 \leq K \leq 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C K

Output

Print the integer Takahashi will get minus the integer Nakahashi will get, after repeating the following operation K times.
If the absolute value of the answer exceeds 10^{18}, print Unfair instead.

Sample Input 1

1 2 3 1

Sample Output 1

1

After one operation, Takahashi, Nakahashi and Hikuhashi have 5, 4 and 3, respectively. We should print 5-4=1.

Sample Input 2

2 3 2 0

Sample Output 2

-1

Sample Input 3

1000000000 1000000000 1000000000 1000000000000000000

Sample Output 3

0


program test
implicit none
integer(8) :: n,i
integer(8) :: retsu(2*10**5) = 0, imp=0, number=0

read(*,*) n

do i=1,n
	read(*,*) retsu(i)
enddo

if(retsu(1)>0) then
imp = 1
endif

do i=2,n

		if(retsu(i) - retsu(i-1) > 1) then
			imp = 1
		endif

		if(retsu(i)==0) then
			number = number
		else if(retsu(i)==1)then
			number = number + 1
		else if(retsu(i) - retsu(i-1) == 1) then
			number = number + 1
		else if(retsu(i) <= retsu(i-1)) then
			number = number + retsu(i)
		endif

enddo

if(imp == 1)then
	write(*,*) -1
else
	write(*,*) number
endif

end program

Score : 700 points

Problem Statement

There is a sequence X of length N, where every element is initially 0. Let X_i denote the i-th element of X.

You are given a sequence A of length N. The i-th element of A is A_i. Determine if we can make X equal to A by repeating the operation below. If we can, find the minimum number of operations required.

Choose an integer i such that 1\leq i\leq N-1. Replace the value of X_{i+1} with the value of X_i plus 1.

Constraints

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

0 \leq A_i \leq 10^9(1\leq i\leq N)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

If we can make X equal to A by repeating the operation, print the minimum number of operations required. If we cannot, print -1.

Sample Input 1

4
0
1
1
2

Sample Output 1

3

We can make X equal to A as follows:

Choose i=2. X becomes (0,0,1,0).

Choose i=1. X becomes (0,1,1,0).

Choose i=3. X becomes (0,1,1,2).

Sample Input 2

3
1
2
1

Sample Output 2

-1

Sample Input 3

9
0
1
1
0
1
2
2
1
2

Sample Output 3

8


integer a,b,c,d
read*, a,b,c,d
if(abs(a-b)<d .and. abs(b-c)<d .or. abs(a-c)<d)then
	print*, "Yes"
else
	print*, "No"
end if
end

Score : 100 points

Problem Statement

Three people, A, B and C, are trying to communicate using transceivers.
They are standing along a number line, and the coordinates of A, B and C are a, b and c (in meters), respectively.
Two people can directly communicate when the distance between them is at most d meters.
Determine if A and C can communicate, either directly or indirectly.
Here, A and C can indirectly communicate when A and B can directly communicate and also B and C can directly communicate.

Constraints

1 ≤ a,b,c ≤ 100

1 ≤ d ≤ 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

a b c d

Output

If A and C can communicate, print Yes; if they cannot, print No.

Sample Input 1

4 7 9 3

Sample Output 1

Yes

A and B can directly communicate, and also B and C can directly communicate, so we should print Yes.

Sample Input 2

100 10 1 2

Sample Output 2

No

They cannot communicate in this case.

Sample Input 3

10 10 10 1

Sample Output 3

Yes

There can be multiple people at the same position.

Sample Input 4

1 100 2 10

Sample Output 4

Yes


program beki
  implicit none

  integer :: x, n, p, b

  read(*,*) x

  n = 1
  if (x < 4 ) then
     n = 1
  else
     do b = 2, x-1
        p = 2
        do while ( b**p <= x )

           if ( n < b**p ) then
              n = b**P
           end if
           p = p + 1
        end do
     end do
  end if
  write(*,*) n

  stop
end program beki

Score : 200 points

Problem Statement

You are given a positive integer X.
Find the largest perfect power that is at most X.
Here, a perfect power is an integer that can be represented as b^p, where b is an integer not less than 1 and p is an integer not less than 2.

Constraints

1 ≤ X ≤ 1000

X is an integer.

Input

Input is given from Standard Input in the following format:

X

Output

Print the largest perfect power that is at most X.

Sample Input 1

10

Sample Output 1

9

There are four perfect powers that are at most 10: 1, 4, 8 and 9.
We should print the largest among them, 9.

Sample Input 2

1

Sample Output 2

1

Sample Input 3

999

Sample Output 3

961


program sample
  implicit none
  integer(8) :: i,j,x,y,count
  integer(8) :: a(41)

  read(*,*) x

  a(1) =1

  do i=2,9
     a(i) = 2**i
  end do

  do i=10,14
     a(i) = 3**(i-8)
  end do

  do i=15,17
     a(i) = 5**(i-13)
  end do
  do i=18,19
     a(i) =6**(i-16)
  end do

  do i=20,21
     a(i) = 7**(i-18)
  end do

  do i=22,23
     a(i) = 10**(i-20)
  end do

  do i=24,28
     a(i) = (i-13)**2
  end do

  do i=29,36
     a(i) = (i-12)**2
  end do

  a(37) = 26**2

  do i=38,41
     a(i)=(i-10)**2
  end do

  do i=1,41
     do j=i,41
        if (a(i)>a(j)) then
           y=a(i)
           a(i)=a(j)
           a(j)=y
        end if
     end do
  end do

  do i=1,41
     if (a(i) <= x) then
        count=a(i)
     end if
  end do

  write(*,*) count

  stop
end program sample


program main
	implicit none
    integer(8)::x,i,j,ans

    read(*,*)x
    ans=0

    do i=1,x
    	do j=2,x
        	if(i**j<=x .and. i**j>ans)then
            	ans=i**j
            end if
        end do
    end do
    write(*,*) ans
    stop
end program main


program exp
	implicit none
	integer :: y, c, i
	real(8) :: x
	integer,parameter :: money(3) = (/300000,200000,100000/)

	read(*,*) x

	do i = 1, int(sqrt(x))+1
		do y = 2, int(log(x)/log(2.0_8))
			if (i**y <= x) then
				c = i ** y
			else
				exit
			end if
		end do
	end do

	write(*,'(i0)') c

end program exp


program exponential
  implicit none
  integer :: x, n, i, s, t, k
  read(*,*) x
  if (x.lt.4) then
    write(*,'(i0)') 1
  end if
  k = 1+int(sqrt(real(x,8)))
  s = 0
  do i = 2, k
    t = i*i
    do
      if (t.gt.x) exit
      if (s.lt.t) s = t
      t = t*i
    end do
  end do
  write(*,'(i0)') s
  stop
end program exponential


program arc097_a
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: n,i,j,l,r,k
    character(5000):: s, s_list(5),subs

    read*, s
    read*, k
    s_list(:) = ''
    do i=1,5
        do j=1,5000
            s_list(i) = trim(s_list(i)) // 'z'
        end do
    end do
    n = len_trim(s)

    do l=1,n
        do r=l,n
            subs = s(l:r)
            do i=1,5
                if (subs == s_list(i)) exit
                if (subs < s_list(i)) call swap(subs, s_list(i))
            end do
        end do
    end do
    print'(a)', trim(s_list(k))
contains
    subroutine swap(s1,s2)
        character(5000):: s1,s2,tmp
        tmp=s1; s1=s2; s2=tmp
    end subroutine

end program arc097_a

Score : 300 points

Problem Statement

You are given a string s.
Among the different substrings of s, print the K-th lexicographically smallest one.

A substring of s is a string obtained by taking out a non-empty contiguous part in s.
For example, if s = ababc, a, bab and ababc are substrings of s, while ac, z and an empty string are not.
Also, we say that substrings are different when they are different as strings.

Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.

Constraints

1 ≤ |s| ≤ 5000

s consists of lowercase English letters.

1 ≤ K ≤ 5

s has at least K different substrings.

Partial Score

200 points will be awarded as a partial score for passing the test set satisfying |s| ≤ 50.

Input

Input is given from Standard Input in the following format:

s
K

Output

Print the K-th lexicographically smallest substring of K.

Sample Input 1

aba
4

Sample Output 1

b

s has five substrings: a, b, ab, ba and aba.
Among them, we should print the fourth smallest one, b.
Note that we do not count a twice.

Sample Input 2

atcoderandatcodeer
5

Sample Output 2

andat

Sample Input 3

z
1

Sample Output 3

z


program takahashi
  implicit none
  integer a, b ,c
  read *, a, b
  if (a>b) then
     c=a-1
   else
    c=a
  end if
  print *, c
  end program takahashi

Score: 100 points

Problem Statement

In AtCoder Kingdom, Gregorian calendar is used, and dates are written in the "year-month-day" order, or the "month-day" order without the year.

For example, May 3, 2018 is written as 2018-5-3, or 5-3 without the year.

In this country, a date is called Takahashi when the month and the day are equal as numbers. For example, 5-5 is Takahashi.

How many days from 2018-1-1 through 2018-a-b are Takahashi?

Constraints

a is an integer between 1 and 12 (inclusive).

b is an integer between 1 and 31 (inclusive).

2018-a-b is a valid date in Gregorian calendar.

Input

Input is given from Standard Input in the following format:

a b

Output

Print the number of days from 2018-1-1 through 2018-a-b that are Takahashi.

Sample Input 1

5 5

Sample Output 1

5

There are five days that are Takahashi: 1-1, 2-2, 3-3, 4-4 and 5-5.

Sample Input 2

2 1

Sample Output 2

1

There is only one day that is Takahashi: 1-1.

Sample Input 3

11 30

Sample Output 3

11

There are eleven days that are Takahashi: 1-1, 2-2, 3-3, 4-4, 5-5, 6-6, 7-7, 8-8, 9-9, 10-10 and 11-11.


program ABC096B
	integer(8)::A,B,C,K,i,result
	read(5,*)A,B,C
	read(5,*)K

	result=A+B+C

	do i=1,K
		result=result+i*max(A,max(B,C))
	end do

	print'(i0)',result
end program ABC096B

Score: 200 points

Problem Statement

There are three positive integers A, B and C written on a blackboard. E869120 performs the following operation K times:

Choose one integer written on the blackboard and let the chosen integer be n. Replace the chosen integer with 2n.

What is the largest possible sum of the integers written on the blackboard after K operations?

Constraints

A, B and C are integers between 1 and 50 (inclusive).

K is an integer between 1 and 10 (inclusive).

Input

Input is given from Standard Input in the following format:

A B C
K

Output

Print the largest possible sum of the integers written on the blackboard after K operations by E869220.

Sample Input 1

5 3 11
1

Sample Output 1

30

In this sample, 5, 3, 11 are initially written on the blackboard, and E869120 can perform the operation once.

There are three choices:

Double 5: The integers written on the board after the operation are 10, 3, 11.

Double 3: The integers written on the board after the operation are 5, 6, 11.

Double 11: The integers written on the board after the operation are 5, 3, 22.

If he chooses 3., the sum of the integers written on the board afterwards is 5 + 3 + 22 = 30, which is the largest among 1. through 3.

Sample Input 2

3 3 4
2

Sample Output 2

22

E869120 can perform the operation twice. The sum of the integers eventually written on the blackboard is maximized as follows:

First, double 4. The integers written on the board are now 3, 3, 8.

Next, double 8. The integers written on the board are now 3, 3, 16.

Then, the sum of the integers eventually written on the blackboard is 3 + 3 + 16 = 22.


program main
   implicit none
   integer a(1:3), K, i
   read(*, *) a(1:3)
   read(*, *) K
   do i = 1, K
        a(maxloc(a,1))=2*maxval(a)
   end do
   write(*, *) sum(a)
end program main


program main
    implicit none
    integer*8::a,b,c,k
    read(*,*)a,b,c
    read*,k
    print*,max(lshift(a,k)+b+c,a+lshift(b,k)+c,a+b+lshift(c,k))
end program


program test
implicit none

integer :: sosuu(55555)=1,i,j,k,nn,n,ans(55555),count=0

read(*,*) n

sosuu(1) = 0

nn = int(55555**0.5)

do i=2,nn
	do j=2,55555
		if(j>i .and. mod(j,i) == 0)then
			sosuu(j) = 0
		endif
	enddo
enddo

count=0
do i=1,55555
	if(sosuu(i)==1 .and. mod(i,10)==1 )then
		count =count + 1
		ans(count)=i
	endif

	if(count == 55)then
	goto 100
	endif

enddo

100 continue

write(*,*) ans(1:n)

end program

Score: 400 points

Problem Statement

Print a sequence a_1, a_2, ..., a_N whose length is N that satisfies the following conditions:

a_i (1 \leq i \leq N) is a prime number at most 55 555.

The values of a_1, a_2, ..., a_N are all different.

In every choice of five different integers from a_1, a_2, ..., a_N, the sum of those integers is a composite number.

If there are multiple such sequences, printing any of them is accepted.

Notes

An integer N not less than 2 is called a prime number if it cannot be divided evenly by any integers except 1 and N, and called a composite number otherwise.

Constraints

N is an integer between 5 and 55 (inclusive).

Input

Input is given from Standard Input in the following format:

N

Output

Print N numbers a_1, a_2, a_3, ..., a_N in a line, with spaces in between.

Sample Input 1

5

Sample Output 1

3 5 7 11 31

Let us see if this output actually satisfies the conditions.

First, 3, 5, 7, 11 and 31 are all different, and all of them are prime numbers.

The only way to choose five among them is to choose all of them, whose sum is a_1+a_2+a_3+a_4+a_5=57, which is a composite number.

There are also other possible outputs, such as 2 3 5 7 13, 11 13 17 19 31 and 7 11 5 31 3.

Sample Input 2

6

Sample Output 2

2 3 5 7 11 13

2, 3, 5, 7, 11, 13 are all different prime numbers.

2+3+5+7+11=28 is a composite number.

2+3+5+7+13=30 is a composite number.

2+3+5+11+13=34 is a composite number.

2+3+7+11+13=36 is a composite number.

2+5+7+11+13=38 is a composite number.

3+5+7+11+13=39 is a composite number.

Thus, the sequence 2 3 5 7 11 13 satisfies the conditions.

Sample Input 3

8

Sample Output 3

2 5 7 13 19 37 67 79


program test
implicit none
integer(8) :: n,a(200000),ruiseki(200000)=0,y(200000)=0.,t
integer    :: x=0,i,j,k,l,combo
read(*,*) n

  read(*,*) (a(i),i=1,n)

ruiseki(1) = a(1)

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

do i=1,n
  if(ruiseki(i) == 0) then
    x = x+1
  endif
enddo

y = ruiseki

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

combo = 1

do i = 2,n
  if(y(i) == y(i-1)) then
    combo = combo + 1
  else if (combo >= 2) then
    x = x + combo * (combo-1) /2
    combo = 1
  else
    combo = 1
  endif
enddo

  if (combo >= 2) then
    x = x + combo * (combo-1) /2
  endif

write(*,*) x

end program

Score : 200 points

Problem Statement

We have an integer sequence A, whose length is N.

Find the number of the non-empty contiguous subsequences of A whose sums are 0.
Note that we are counting the ways to take out subsequences.
That is, even if the contents of some two subsequences are the same, they are counted individually if they are taken from different positions.

Constraints

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

-10^9 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Find the number of the non-empty contiguous subsequences of A whose sum is 0.

Sample Input 1

6
1 3 -4 2 2 -2

Sample Output 1

3

There are three contiguous subsequences whose sums are 0: (1,3,-4), (-4,2,2) and (2,-2).

Sample Input 2

7
1 -1 1 -1 1 -1 1

Sample Output 2

12

In this case, some subsequences that have the same contents but are taken from different positions are counted individually.
For example, three occurrences of (1, -1) are counted.

Sample Input 3

5
1 -2 3 -4 5

Sample Output 3

0

There are no contiguous subsequences whose sums are 0.


program test
implicit none
integer(8) :: n,a(200000),ruiseki(200000)=0,x=0,i,j
read(*,*) n

  read(*,*) (a(i),i=1,n)

ruiseki(1) = a(1)

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

do i=2,n
  if(ruiseki(i) == 0) then
  x = x+1
  endif
enddo

do i=1,n
  do j=i+1,n
    if(ruiseki(i) == ruiseki(j)) then
      x  = x + 1
    endif
  enddo
enddo

write(*,*) x

end program


module mod_modulo_util
  implicit none
  integer(8), parameter :: md = 1000000007_8
  integer(8), allocatable :: f(:), invf(:)
contains
  subroutine init(n)
    implicit none
    integer, intent(in) :: n
    integer :: i
    if (allocated(f)) deallocate(f)
    if (allocated(invf)) deallocate(invf)
    allocate(f(0:n),invf(0:n))
    f = 0_8
    invf = 0_8
    f(0) = 1_8
    do i = 1, n
      f(i) = mod(f(i-1)*int(i,8),md)
    end do
    invf(n) = inv(f(n))
    do i = n, 1, -1
      invf(i-1) = mod(invf(i)*int(i,8),md)
    end do
    return
  end subroutine init
  integer(8) function perm(n,k)
    implicit none
    integer, intent(in) :: n, k
    perm = 0_8
    if (n < 0 .or. k > n) return
    perm = mod(f(n)*invf(n-k),md)
    return
  end function perm
  integer(8) function comb(n,k)
    implicit none
    integer, intent(in) :: n, k
    comb = mod(perm(n,k)*invf(k),md)
    return
  end function comb
  integer(8) function homo(n,k)
    implicit none
    integer, intent(in) :: n, k
    homo = comb(n+k-1,k)
    return
  end function homo
  function inv(n) result(y)
    implicit none
    integer(8), intent(in) :: n
    integer(8) :: a, b, x, y, t, q
    a = mod(n,md)
    b = md
    x = 0_8
    y = 1_8
    do while (b /= 0_8)
      q = a/b
      t = b
      b = mod(a,b)
      a = t
      t = y
      y = x
      x = t-q*x
    end do
    y = mod(y,md)
    if (y < 0_8) y = y+md
    return
  end function inv
  function pow(a,b) result(r)
    implicit none
    integer(8), intent(in) :: a
    integer, intent(in) :: b
    integer :: i
    integer(8) :: r, p
    r = 1_8
    p = a
    do i = 0, 32
      if (btest(b,i)) r = mod(r*p,md)
      p = mod(p*p,md)
    end do
    return
  end function pow
  integer(8) function fact(n)
    implicit none
    integer, intent(in) :: n
    fact = 0_8
    if (n < 0) return
    fact = f(n)
    return
  end function fact
end module mod_modulo_util
program painting_machines
  use mod_modulo_util
  implicit none
  integer :: n, k
  integer(8) :: ans = 0_8
  read(*,*) n
  call init(n)
  ans = mod(mod(f(n)*f(n),md)*invf(n),md)
  do k = (n+1)/2, n-1
    ans = mod(ans+md-mod(mod(f(k-1)*f(k),md)*invf(2*k-n),md),md)
  end do
  write(*,'(i0)') ans
  stop
end program painting_machines

Score : 800 points

Problem Statement

There are N squares lining up in a row, numbered 1 through N from left to right.
Initially, all squares are white.
We also have N-1 painting machines, numbered 1 through N-1.
When operated, Machine i paints Square i and i+1 black.

Snuke will operate these machines one by one.
The order in which he operates them is represented by a permutation of (1, 2, ..., N-1), P, which means that the i-th operated machine is Machine P_i.

Here, the score of a permutation P is defined as the number of machines that are operated before all the squares are painted black for the first time, when the machines are operated in the order specified by P.
Snuke has not decided what permutation P to use, but he is interested in the scores of possible permutations.
Find the sum of the scores over all possible permutations for him.
Since this can be extremely large, compute the sum modulo 10^9+7.

Constraints

2 \leq N \leq 10^6

Input

Input is given from Standard Input in the following format:

N

Output

Print the sum of the scores over all possible permutations, modulo 10^9+7.

Sample Input 1

4

Sample Output 1

16

There are six possible permutations as P.
Among them, P = (1, 3, 2) and P = (3, 1, 2) have a score of 2, and the others have a score of 3.
Thus, the answer is 2 \times 2 + 3 \times 4 = 16.

Sample Input 2

2

Sample Output 2

1

There is only one possible permutation: P = (1), which has a score of 1.

Sample Input 3

5

Sample Output 3

84

Sample Input 4

100000

Sample Output 4

341429644


program ABC095C
    implicit none
    integer(8)::A,B,C,X,Y
    read*,A,B,C,X,Y

    if(X<Y)then
        print'(i0)',min(A*X+B*Y,2*C*X+B*(Y-X),2*C*Y)
    else if(X>Y)then
        print'(i0)',min(A*X+B*Y,2*C*Y+A*(X-Y),2*C*X)
    else
        print'(i0)',min(A*X+B*Y,2*C*X)
    end if
end program ABC095C

Score : 300 points

Problem Statement

"Pizza At", a fast food chain, offers three kinds of pizza: "A-pizza", "B-pizza" and "AB-pizza". A-pizza and B-pizza are completely different pizzas, and AB-pizza is one half of A-pizza and one half of B-pizza combined together. The prices of one A-pizza, B-pizza and AB-pizza are A yen, B yen and C yen (yen is the currency of Japan), respectively.

Nakahashi needs to prepare X A-pizzas and Y B-pizzas for a party tonight. He can only obtain these pizzas by directly buying A-pizzas and B-pizzas, or buying two AB-pizzas and then rearrange them into one A-pizza and one B-pizza. At least how much money does he need for this? It is fine to have more pizzas than necessary by rearranging pizzas.

Constraints

1 ≤ A, B, C ≤ 5000

1 ≤ X, Y ≤ 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C X Y

Output

Print the minimum amount of money required to prepare X A-pizzas and Y B-pizzas.

Sample Input 1

1500 2000 1600 3 2

Sample Output 1

7900

It is optimal to buy four AB-pizzas and rearrange them into two A-pizzas and two B-pizzas, then buy additional one A-pizza.

Sample Input 2

1500 2000 1900 3 2

Sample Output 2

8500

It is optimal to directly buy three A-pizzas and two B-pizzas.

Sample Input 3

1500 2000 500 90000 100000

Sample Output 3

100000000

It is optimal to buy 200000 AB-pizzas and rearrange them into 100000 A-pizzas and 100000 B-pizzas. We will have 10000 more A-pizzas than necessary, but that is fine.


program Half_and_Half
    implicit none
    integer(4):: a,b,c
    integer(4):: x,y

    read*, a,b,c,x,y

    print*, min(x,y) * min(a+b, 2*c) + abs(x-y)*min(extra(),2*c)

contains
    function extra() result(ret)
        integer(4):: ret

        if (x==y) then
            ret=0
        else if (x > y) then
            ret=a
        else
            ret=b
        end if
    end function
end program Half_and_Half


program test
implicit none
integer :: a,b,c,x,y,p(3)

read(*,*) a,b,c,x,y

p(1) = b * max(y-x,0) + c * 2 * x
p(2) = a * max(x-y,0) + c * 2 * y
p(3) = a * x + b * y

write(*,*) minval(p(1:3))

end program


program test
implicit none
integer :: i,j,n
double precision :: c,x(100000),v(100000),tokei,hantokei(100000),goukei(100000)=0,saikou

read(*,*) n,c
do i = 1,n
read(*,*) x(i),v(i)
enddo

do i = 0, n

if(i == 0) then
  tokei = 0
  hantokei = 0
else
tokei = sum(v(1:i)) - x(i)
hantokei = 0
endif

do j = i+1, n
hantokei(j) = sum(v(j:n)) - (x(i) + c - x(j))
enddo

goukei(i) = tokei +  maxval(hantokei(:))

enddo

saikou = maxval(goukei(:))

do i = 1, n

tokei = sum(v(i:n)) - ( c - x(i))
hantokei = 0

do j = 1, i-1
if(j>=1) then
hantokei(j) = sum(v(1:j)) - (c - x(i) + x(j))
endif
enddo

goukei(i) = tokei +  maxval(hantokei(:))

enddo

write(*,*) max(saikou,maxval(goukei(:)))

end program

Score : 500 points

Problem Statement

"Teishi-zushi", a Japanese restaurant, is a plain restaurant with only one round counter. The outer circumference of the counter is C meters. Customers cannot go inside the counter.

Nakahashi entered Teishi-zushi, and he was guided to the counter. Now, there are N pieces of sushi (vinegared rice with seafood and so on) on the counter. The distance measured clockwise from the point where Nakahashi is standing to the point where the i-th sushi is placed, is x_i meters. Also, the i-th sushi has a nutritive value of v_i kilocalories.

Nakahashi can freely walk around the circumference of the counter. When he reach a point where a sushi is placed, he can eat that sushi and take in its nutrition (naturally, the sushi disappears). However, while walking, he consumes 1 kilocalories per meter.

Whenever he is satisfied, he can leave the restaurant from any place (he does not have to return to the initial place). On balance, at most how much nutrition can he take in before he leaves? That is, what is the maximum possible value of the total nutrition taken in minus the total energy consumed? Assume that there are no other customers, and no new sushi will be added to the counter. Also, since Nakahashi has plenty of nutrition in his body, assume that no matter how much he walks and consumes energy, he never dies from hunger.

Constraints

1 ≤ N ≤ 10^5

2 ≤ C ≤ 10^{14}

1 ≤ x_1 < x_2 < ... < x_N < C

1 ≤ v_i ≤ 10^9

All values in input are integers.

Subscores

300 points will be awarded for passing the test set satisfying N ≤ 100.

Input

Input is given from Standard Input in the following format:

N C
x_1 v_1
x_2 v_2
:
x_N v_N

Output

If Nakahashi can take in at most c kilocalories on balance before he leaves the restaurant, print c.

Sample Input 1

3 20
2 80
9 120
16 1

Sample Output 1

191

There are three sushi on the counter with a circumference of 20 meters. If he walks two meters clockwise from the initial place, he can eat a sushi of 80 kilocalories. If he walks seven more meters clockwise, he can eat a sushi of 120 kilocalories. If he leaves now, the total nutrition taken in is 200 kilocalories, and the total energy consumed is 9 kilocalories, thus he can take in 191 kilocalories on balance, which is the largest possible value.

Sample Input 2

3 20
2 80
9 1
16 120

Sample Output 2

192

The second and third sushi have been swapped. Again, if he walks two meters clockwise from the initial place, he can eat a sushi of 80 kilocalories. If he walks six more meters counterclockwise this time, he can eat a sushi of 120 kilocalories. If he leaves now, the total nutrition taken in is 200 kilocalories, and the total energy consumed is 8 kilocalories, thus he can take in 192 kilocalories on balance, which is the largest possible value.

Sample Input 3

1 100000000000000
50000000000000 1

Sample Output 3

0

Even though the only sushi is so far that it does not fit into a 32-bit integer, its nutritive value is low, thus he should immediately leave without doing anything.

Sample Input 4

15 10000000000
400000000 1000000000
800000000 1000000000
1900000000 1000000000
2400000000 1000000000
2900000000 1000000000
3300000000 1000000000
3700000000 1000000000
3800000000 1000000000
4000000000 1000000000
4100000000 1000000000
5200000000 1000000000
6600000000 1000000000
8000000000 1000000000
9300000000 1000000000
9700000000 1000000000

Sample Output 4

6500000000

All these sample inputs above are included in the test set for the partial score.


integer a,b,x
read*,a,b,x
if(x<a.or.a+b<x)then
	print"(A)","NO"
else
	print"(A)","YES"
endif
end

Score : 100 points

Problem Statement

There are a total of A + B cats and dogs.
Among them, A are known to be cats, but the remaining B are not known to be either cats or dogs.

Determine if it is possible that there are exactly X cats among these A + B animals.

Constraints

1 \leq A \leq 100

1 \leq B \leq 100

1 \leq X \leq 200

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B X

Output

If it is possible that there are exactly X cats, print YES; if it is impossible, print NO.

Sample Input 1

3 5 4

Sample Output 1

YES

If there are one cat and four dogs among the B = 5 animals, there are X = 4 cats in total.

Sample Input 2

2 2 6

Sample Output 2

NO

Even if all of the B = 2 animals are cats, there are less than X = 6 cats in total.

Sample Input 3

5 3 2

Sample Output 3

NO

Even if all of the B = 3 animals are dogs, there are more than X = 2 cats in total.


program test
implicit none
integer :: a,b,x

read(*,*) a,b,x

if(x >= a .and. x <= a+b ) then
  write(*,*) "YES"
else
  write(*,*) "NO"
endif

end program


program main
  implicit none

  integer :: n,m,x,a(100),i,j,b(100)=0,ss=0,bb=0

  read(*,*)n,m,x
  read(*,*)(a(i),i=1, m)
  do i = 1, m
    if (i == a(i)) then
      b(i) = 1
    end if
  end do

  do i = 1, x-1
    ss = ss + b(i)
  end do
  do i = x+1,100
    bb = bb + b(i)
  end do

  write(*,*)min(ss,bb)
end program main

Score : 200 points

Problem Statement

There are N + 1 squares arranged in a row, numbered 0, 1, ..., N from left to right.

Initially, you are in Square X.
You can freely travel between adjacent squares. Your goal is to reach Square 0 or Square N.
However, for each i = 1, 2, ..., M, there is a toll gate in Square A_i, and traveling to Square A_i incurs a cost of 1.
It is guaranteed that there is no toll gate in Square 0, Square X and Square N.

Find the minimum cost incurred before reaching the goal.

Constraints

1 \leq N \leq 100

1 \leq M \leq 100

1 \leq X \leq N - 1

1 \leq A_1 < A_2 < ... < A_M \leq N

A_i \neq X

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M X
A_1 A_2 ... A_M

Output

Print the minimum cost incurred before reaching the goal.

Sample Input 1

5 3 3
1 2 4

Sample Output 1

1

The optimal solution is as follows:

First, travel from Square 3 to Square 4. Here, there is a toll gate in Square 4, so the cost of 1 is incurred.

Then, travel from Square 4 to Square 5. This time, no cost is incurred.

Now, we are in Square 5 and we have reached the goal.

In this case, the total cost incurred is 1.

Sample Input 2

7 3 2
4 5 6

Sample Output 2

0

We may be able to reach the goal at no cost.

Sample Input 3

10 7 5
1 2 3 4 6 8 9

Sample Output 3

3


program abc094b
  implicit none
  integer :: n, m, x
  integer,allocatable :: a(:)

  read *, n, m, x
  allocate(a(m))
  read *, a
  print *, min(count(a<x),count(a>x))
end program abc094b


program main
	implicit none
    integer::n,i
    real(8)::t
    integer,allocatable::x(:),y(:)
    read(*,*) n
    allocate(x(n))
    allocate(y(n))

    read(*,*) x
    y=x
    call heapsort(n,y)

    t=(real(y(n/2),kind=8)+real(y(n/2 +1),kind=8))/2.0_8

    do i=1,n
    	if(real(x(i),8)<t)then
        	write(*,*) y(n/2 +1)
        else
        	write(*,*) y(n/2)
        end if
    end do

    deallocate(x)
    deallocate(y)

    stop
contains
	subroutine heapsort(n,array)
        implicit none
        integer,intent(in) :: n
        integer,intent(inout) :: array(1:n)

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

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

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

        return
    end subroutine heapsort
end program main

Score : 300 points

Problem Statement

When l is an odd number, the median of l numbers a_1, a_2, ..., a_l is the (\frac{l+1}{2})-th largest value among a_1, a_2, ..., a_l.

You are given N numbers X_1, X_2, ..., X_N, where N is an even number.
For each i = 1, 2, ..., N, let the median of X_1, X_2, ..., X_N excluding X_i, that is, the median of X_1, X_2, ..., X_{i-1}, X_{i+1}, ..., X_N be B_i.

Find B_i for each i = 1, 2, ..., N.

Constraints

2 \leq N \leq 200000

N is even.

1 \leq X_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
X_1 X_2 ... X_N

Output

Print N lines.
The i-th line should contain B_i.

Sample Input 1

4
2 4 4 3

Sample Output 1

4
3
3
4

Since the median of X_2, X_3, X_4 is 4, B_1 = 4.

Since the median of X_1, X_3, X_4 is 3, B_2 = 3.

Since the median of X_1, X_2, X_4 is 3, B_3 = 3.

Since the median of X_1, X_2, X_3 is 4, B_4 = 4.

Sample Input 2

2
1 2

Sample Output 2

2
1

Sample Input 3

6
5 5 4 4 3 3

Sample Output 3

4
4
4
4
4
4


program ABC_094_D_BinomialCoefficients
   implicit none
   integer(8) n, l, r, i
   integer(8), allocatable :: a(:)
   read(*, *) n
   allocate(a(1: n))
   read(*, *) a(1: n)
   l = maxval(a(1:n))
   r = a(1)
   do i = 1, n
       if( abs(dble(l/2) - dble(r)) >= abs(dble(l/2) - dble(a(i))) .and. a(i) /= l) then
	       r = a(i)
	   end if
   end do
   write(*, *) l, r
end program ABC_094_D_BinomialCoefficients

Score : 400 points

Problem Statement

Let {\rm comb}(n,r) be the number of ways to choose r objects from among n objects, disregarding order.
From n non-negative integers a_1, a_2, ..., a_n, select two numbers a_i > a_j so that {\rm comb}(a_i,a_j) is maximized.
If there are multiple pairs that maximize the value, any of them is accepted.

Constraints

2 \leq n \leq 10^5

0 \leq a_i \leq 10^9

a_1,a_2,...,a_n are pairwise distinct.

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 a_i and a_j that you selected, with a space in between.

Sample Input 1

5
6 9 4 2 11

Sample Output 1

11 6

\rm{comb}(a_i,a_j) for each possible selection is as follows:

\rm{comb}(4,2)=6

\rm{comb}(6,2)=15

\rm{comb}(6,4)=15

\rm{comb}(9,2)=36

\rm{comb}(9,4)=126

\rm{comb}(9,6)=84

\rm{comb}(11,2)=55

\rm{comb}(11,4)=330

\rm{comb}(11,6)=462

\rm{comb}(11,9)=55

Thus, we should print 11 and 6.

Sample Input 2

2
100 0

Sample Output 2

100 0


program ABC_094_D_BinomialCoefficients
   implicit none
   integer n, l, r, i
   integer, allocatable :: a(:)
   read(*, *) n
   allocate(a(1: n))
   read(*, *) a(1: n)
   l = maxval(a(1:n))
   r = a(1)
   do i = 2, n
       if( abs(dble(l/2) - r) > abs(dble(l/2) - a(i)) .and. a(i) /= l) then
	       r = a(i)
	   end if
   end do
   write(*, *) l, r
end program ABC_094_D_BinomialCoefficients


Score : 100 points

Problem Statement

You are given a string S of length 3 consisting of a, b and c. Determine if S can be obtained by permuting abc.

Constraints

|S|=3

S consists of a, b and c.

Input

Input is given from Standard Input in the following format:

S

Output

If S can be obtained by permuting abc, print Yes; otherwise, print No.

Sample Input 1

bac

Sample Output 1

Yes

Swapping the first and second characters in bac results in abc.

Sample Input 2

bab

Sample Output 2

No

Sample Input 3

abc

Sample Output 3

Yes

Sample Input 4

aaa

Sample Output 4

No


implicit none
integer :: a,b,k
integer :: i

read*,a,b,k

do i = a,min(a+k-1,b)
   write(*,'(i0)') i
enddo
do i = max(a+k,b-k+1),b
   write(*,'(i0)') i
enddo
stop
end

Score : 200 points

Problem Statement

Print all the integers that satisfies the following in ascending order:

Among the integers between A and B (inclusive), it is either within the K smallest integers or within the K largest integers.

Constraints

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

1 \leq K \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B K

Output

Print all the integers that satisfies the condition above in ascending order.

Sample Input 1

3 8 2

Sample Output 1

3
4
7
8

3 is the first smallest integer among the integers between 3 and 8.

4 is the second smallest integer among the integers between 3 and 8.

7 is the second largest integer among the integers between 3 and 8.

8 is the first largest integer among the integers between 3 and 8.

Sample Input 2

4 8 3

Sample Output 2

4
5
6
7
8

Sample Input 3

2 9 100

Sample Output 3

2
3
4
5
6
7
8
9


INTEGER(16) A,B,K,AKI
READ*,A,B,K
DO I=0,K-1
  AKI=A+I
  IF(A+I>B)EXIT
  PRINT"(I0)",A+I
END DO
DO I=1,K
  IF(B-K+I<=AKI)CYCLE
  PRINT"(I0)",B-K+I
END DO
END


read*,i,j,k
if(j<i)then
n=i;i=j;j=n
endif
if(k<j)then
n=j;j=k;k=n
endif
if(j<i)then
n=i;i=j;j=n
endif
n=(k-i)/2+(k-j)/2;i=i+2*((k-i)/2);j=j+2*((k-j)/2);k=i+j-k-k
if(k==-1)then
n=n+2
else if(k==-2)then
n=n+1
endif
print"(i0)",n
end

Score : 300 points

Problem Statement

You are given three integers A, B and C. Find the minimum number of operations required to make A, B and C all equal by repeatedly performing the following two kinds of operations in any order:

Choose two among A, B and C, then increase both by 1.

Choose one among A, B and C, then increase it by 2.

It can be proved that we can always make A, B and C all equal by repeatedly performing these operations.

Constraints

0 \leq A,B,C \leq 50

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C

Output

Print the minimum number of operations required to make A, B and C all equal.

Sample Input 1

2 5 4

Sample Output 1

2

We can make A, B and C all equal by the following operations:

Increase A and C by 1. Now, A, B, C are 3, 5, 5, respectively.

Increase A by 2. Now, A, B, C are 5, 5, 5, respectively.

Sample Input 2

2 6 3

Sample Output 2

5

Sample Input 3

31 41 5

Sample Output 3

23


program name
    implicit none
    character(26):: s
    integer(4):: ls,i,mic, min_
    integer(4),allocatable:: si(:)
    logical:: is_use(26), f

    read*, s
    ls = len_trim(s)
    allocate(si(ls))

    do i=1,ls
        si(i) = ichar(s(i:i)) - ichar('a') + 1
    end do

    is_use(:) = .false.

    do i=1,ls
        is_use(si(i)) = .true.
    end do

    mic=10001
    do i=1,26
        if (.not. is_use(i))  mic=min(mic,i)
    end do
    f=.false.
    min_=1000001
    if (mic==10001) then
        do i=ls,2,-1
            if (si(i) == 27-i) cycle
            ! print*, minval(si(i:)), ichar(s(i-1:i-1))-ichar('a')+1
            if (minval(si(i:)) <= ichar(s(i-1:i-1))-ichar('a')+1) cycle
            s(i-1:i-1) = char(minval(si(i:))+ichar('a')-1)
            print*, s(1:i-1)
            stop
        end do
        print*, -1
        stop
    end if

    s(ls+1:ls+1) = char(mic+ichar('a')-1)

    print*, trim(s)

end program name

Score : 300 points

Problem Statement

Gotou just received a dictionary. However, he doesn't recognize the language used in the dictionary. He did some analysis on the dictionary and realizes that the dictionary contains all possible diverse words in lexicographical order.

A word is called diverse if and only if it is a nonempty string of English lowercase letters and all letters in the word are distinct. For example, atcoder, zscoder and agc are diverse words while gotou and connect aren't diverse words.

Given a diverse word S, determine the next word that appears after S in the dictionary, i.e. the lexicographically smallest diverse word that is lexicographically larger than S, or determine that it doesn't exist.

Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.

Constraints

1 \leq |S| \leq 26

S is a diverse word.

Input

Input is given from Standard Input in the following format:

S

Output

Print the next word that appears after S in the dictionary, or -1 if it doesn't exist.

Sample Input 1

atcoder

Sample Output 1

atcoderb

atcoderb is the lexicographically smallest diverse word that is lexicographically larger than atcoder. Note that atcoderb is lexicographically smaller than b.

Sample Input 2

abc

Sample Output 2

abcd

Sample Input 3

zyxwvutsrqponmlkjihgfedcba

Sample Output 3

-1

This is the lexicographically largest diverse word, so the answer is -1.

Sample Input 4

abcdefghijklmnopqrstuvwzyx

Sample Output 4

abcdefghijklmnopqrstuvx


program test
implicit none
character*26 :: s1
character*26 :: s2 = 'abcdefghijklmnopqrstuvwxyz'
integer :: i,max_i=0

read(*,*) s1

if(s1 == 'zyxwvutsrqponmlkjihgfedcba') then
  write(*,*) -1
else if (len_trim(s1) < 26) then
  write(*,*) trim(s1)//s2(verify(s2,s1):verify(s2,s1))
else
  do i = 1,25
    if(llt(s1(i:i),s1(i+1:i+1))) then
      max_i = i
    endif
  end do
  write(*,*) s1(1:max_i-1),s2(scan(s2,s1(max_i+1:26)):scan(s2,s1(max_i+1:26)))
end if
end program


program main
	implicit none
    integer::a,b,c,d
    read(*,*)a,b,c,d
    write(*,*)min(a,b)+min(c,d)
    stop
end program main

Score : 100 points

Problem Statement

You planned a trip using trains and buses.
The train fare will be A yen (the currency of Japan) if you buy ordinary tickets along the way, and B yen if you buy an unlimited ticket.
Similarly, the bus fare will be C yen if you buy ordinary tickets along the way, and D yen if you buy an unlimited ticket.

Find the minimum total fare when the optimal choices are made for trains and buses.

Constraints

1 \leq A \leq 1 000

1 \leq B \leq 1 000

1 \leq C \leq 1 000

1 \leq D \leq 1 000

All input values are integers.

Input

Input is given from Standard Input in the following format:

A
B
C
D

Output

Print the minimum total fare.

Sample Input 1

600
300
220
420

Sample Output 1

520

The train fare will be 600 yen if you buy ordinary tickets, and 300 yen if you buy an unlimited ticket.
Thus, the optimal choice for trains is to buy an unlimited ticket for 300 yen.
On the other hand, the optimal choice for buses is to buy ordinary tickets for 220 yen.

Therefore, the minimum total fare is 300 + 220 = 520 yen.

Sample Input 2

555
555
400
200

Sample Output 2

755

Sample Input 3

549
817
715
603

Sample Output 3

1152


integer a,b,c,d
read*, a,b,c,d
print*, min(a,b)+min(c,d)
end


integer :: a, b, c, d
read *, a, b, c, d
write (*,'(I0)') MIN(a, b, c, d)
end


program ABC090A
	character(3)::a,b,c
	read(5,*)a,b,c
	print'(A)',a(1:1)//b(2:2)//c(3:3)
end program ABC090A

Score : 100 points

Problem Statement

We have a 3×3 square grid, where each square contains a lowercase English letters.
The letter in the square at the i-th row from the top and j-th column from the left is c_{ij}.

Print the string of length 3 that can be obtained by concatenating the letters in the squares on the diagonal connecting the top-left and bottom-right corner of the grid, from the top-left to bottom-right.

Constraints

Input consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

c_{11}c_{12}c_{13}
c_{21}c_{22}c_{23}
c_{31}c_{32}c_{33}

Output

Print the string of length 3 that can be obtained by concatenating the letters on the diagonal connecting the top-left and bottom-right corner of the grid, from the top-left to bottom-right.

Sample Input 1

ant
obe
rec

Sample Output 1

abc

The letters in the squares on the diagonal connecting the top-left and bottom-right corner of the grid are a, b and c from top-right to bottom-left. Concatenate these letters and print abc.

Sample Input 2

edu
cat
ion

Sample Output 2

ean


program main
implicit None
	integer::n,m
	read *,n,m

	if(n == 1 .or. m == 1)then
		print "(i0)",n*m-2
		stop
	endif

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

Score : 300 points

Problem Statement

There is a grid with infinitely many rows and columns. In this grid, there is a rectangular region with consecutive N rows and M columns, and a card is placed in each square in this region.
The front and back sides of these cards can be distinguished, and initially every card faces up.

We will perform the following operation once for each square contains a card:

For each of the following nine squares, flip the card in it if it exists: the target square itself and the eight squares that shares a corner or a side with the target square.

It can be proved that, whether each card faces up or down after all the operations does not depend on the order the operations are performed.
Find the number of cards that face down after all the operations.

Constraints

1 \leq N,M \leq 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

Print the number of cards that face down after all the operations.

Sample Input 1

2 2

Sample Output 1

0

We will flip every card in any of the four operations. Thus, after all the operations, all cards face up.

Sample Input 2

1 7

Sample Output 2

5

After all the operations, all cards except at both ends face down.

Sample Input 3

314 1592

Sample Output 3

496080


integer(8) n,m
read *,n,m
print '(i0)',abs((n-2)*(m-2))
end


program arc091_d
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: n,k,b,ans=0

    read*, n,k

    do b = k+1,n
        ans=ans+(n/(b)*(b-k))+max((n-((n/b)*b+k)+1),0)
    end do
    if (k==0) ans=ans-n
    print'(i0)', ans
end program arc091_d

Score : 400 points

Problem Statement

Takahashi had a pair of two positive integers not exceeding N, (a,b), which he has forgotten.
He remembers that the remainder of a divided by b was greater than or equal to K.
Find the number of possible pairs that he may have had.

Constraints

1 \leq N \leq 10^5

0 \leq K \leq N-1

All input values are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the number of possible pairs that he may have had.

Sample Input 1

5 2

Sample Output 1

7

There are seven possible pairs: (2,3),(5,3),(2,4),(3,4),(2,5),(3,5) and (4,5).

Sample Input 2

10 0

Sample Output 2

100

Sample Input 3

31415 9265

Sample Output 3

287927211


program strange_nim
  implicit none
  integer :: n, a, k, d, m, i, g = 0
  read(*,*) n
  do i = 1, n
    read(*,*) a, k
    do
      d = a/k
      m = mod(a,k)
      if (m == 0) exit
      a = a-(m+d)/(d+1)*(d+1)
    end do
    g = xor(g,d)
  end do
  if (g == 0) then
    write(*,'(a)') "Aoki"
  else
    write(*,'(a)') "Takahashi"
  end if
end program strange_nim

Score : 900 points

Problem Statement

Takahashi and Aoki are playing a stone-taking game. Initially, there are N piles of stones, and the i-th pile contains A_i stones and has an associated integer K_i.

Starting from Takahashi, Takahashi and Aoki take alternate turns to perform the following operation:

Choose a pile. If the i-th pile is selected and there are X stones left in the pile, remove some number of stones between 1 and floor(X/K_i) (inclusive) from the pile.

The player who first becomes unable to perform the operation loses the game. Assuming that both players play optimally, determine the winner of the game.
Here, floor(x) represents the largest integer not greater than x.

Constraints

1 \leq N \leq 200

1 \leq A_i,K_i \leq 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 K_1
:
A_N K_N

Output

If Takahashi will win, print Takahashi; if Aoki will win, print Aoki.

Sample Input 1

2
5 2
3 3

Sample Output 1

Aoki

Initially, from the first pile at most floor(5/2)=2 stones can be removed at a time, and from the second pile at most floor(3/3)=1 stone can be removed at a time.

If Takahashi first takes two stones from the first pile, from the first pile at most floor(3/2)=1 stone can now be removed at a time, and from the second pile at most floor(3/3)=1 stone can be removed at a time.

Then, if Aoki takes one stone from the second pile, from the first pile at most floor(3/2)=1 stone can be removed at a time, and from the second pile no more stones can be removed (since floor(2/3)=0).

Then, if Takahashi takes one stone from the first pile, from the first pile at most floor(2/2)=1 stone can now be removed at a time, and from the second pile no more stones can be removed.

Then, if Aoki takes one stone from the first pile, from the first pile at most floor(1/2)=0 stones can now be removed at a time, and from the second pile no more stones can be removed.

No more operation can be performed, thus Aoki wins. If Takahashi plays differently, Aoki can also win by play accordingly.

Sample Input 2

3
3 2
4 3
5 1

Sample Output 2

Takahashi

Sample Input 3

3
28 3
16 4
19 2

Sample Output 3

Aoki

Sample Input 4

4
3141 59
26535 897
93 23
8462 64

Sample Output 4

Takahashi


program main
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: n
    read*, n
    print'(i0)', n/3
end program main

Score : 100 points

Problem Statement

There are N students in a school.

We will divide these students into some groups, and in each group they will discuss some themes.

You think that groups consisting of two or less students cannot have an effective discussion, so you want to have as many groups consisting of three or more students as possible.

Divide the students so that the number of groups consisting of three or more students is maximized.

Constraints

1 \leq N \leq 1000

All input values are integers.

Input

Input is given from Standard Input in the following format:

N

Output

If you can form at most x groups consisting of three or more students, print x.

Sample Input 1

8

Sample Output 1

2

For example, you can form a group of three students and another of five students.

Sample Input 2

2

Sample Output 2

0

Sometimes you cannot form any group consisting of three or more students, regardless of how you divide the students.

Sample Input 3

9

Sample Output 3

3


IMPLICIT NONE
INTEGER :: N, I
CHARACTER,DIMENSION(100) :: C
LOGICAL :: P = .false. , W = .false. , G = .false. , Y = .false.

READ*, N
READ*, C(1:N)

DO I = 1, N
    IF ( C(I) == "P") THEN
        P = .true.
    ELSE IF (C(I) == "W") THEN
        W = .true.
    ELSE IF (C(I) == "G") THEN
        G = .true.
    ELSE IF (C(I) == "Y") THEN
        Y = .true.
    END IF
END DO

IF (P .AND. W .AND. G .AND. Y) THEN
    PRINT*, "Four"
ELSE
    PRINT*, "Three"
END IF

END

Score : 200 points

Problem Statement

In Japan, people make offerings called hina arare, colorful crackers, on March 3.

We have a bag that contains N hina arare. (From here, we call them arare.)

It is known that the bag either contains arare in three colors: pink, white and green, or contains arare in four colors: pink, white, green and yellow.

We have taken out the arare in the bag one by one, and the color of the i-th arare was S_i, where colors are represented as follows - pink: P, white: W, green: G, yellow: Y.

If the number of colors of the arare in the bag was three, print Three; if the number of colors was four, print Four.

Constraints

1 \leq N \leq 100

S_i is P, W, G or Y.

There always exist i, j and k such that S_i=P, S_j=W and S_k=G.

Input

Input is given from Standard Input in the following format:

N
S_1 S_2 ... S_N

Output

If the number of colors of the arare in the bag was three, print Three; if the number of colors was four, print Four.

Sample Input 1

6
G W Y P Y W

Sample Output 1

Four

The bag contained arare in four colors, so you should print Four.

Sample Input 2

9
G W W G P W P G G

Sample Output 2

Three

The bag contained arare in three colors, so you should print Three.

Sample Input 3

8
P Y W G Y W Y Y

Sample Output 3

Four


program probc
    implicit none
    integer::i,j,k,N
    integer(16)::ans,v(5)
    character(len=10)::S
    character::c(5) = (/"M","A","R","C","H"/)

    read(*,*) N
    v = 0
    do i = 1, N
        read(*,*) S
        do j = 1,5
            if(S(1:1) .eq. c(j)) then
                v(j) = v(j) + 1
            end if
        end do
    end do

    ans = 0
    do i = 1,3
        do j = 2,4
            do k = 3,5
                if(i < j .and. j < k)then
                    ans = ans + v(i)*v(j)*v(k)
                end if
            end do
        end do
    end do

    write(*,*) ans
    stop
end program

Score : 300 points

Problem Statement

There are N people. The name of the i-th person is S_i.

We would like to choose three people so that the following conditions are met:

The name of every chosen person begins with M, A, R, C or H.

There are no multiple people whose names begin with the same letter.

How many such ways are there to choose three people, disregarding order?

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

Constraints

1 \leq N \leq 10^5

S_i consists of uppercase English letters.

1 \leq |S_i| \leq 10

S_i \neq S_j (i \neq j)

Input

Input is given from Standard Input in the following format:

N
S_1
:
S_N

Output

If there are x ways to choose three people so that the given conditions are met, print x.

Sample Input 1

5
MASHIKE
RUMOI
OBIRA
HABORO
HOROKANAI

Sample Output 1

2

We can choose three people with the following names:

MASHIKE, RUMOI, HABORO

MASHIKE, RUMOI, HOROKANAI

Thus, we have two ways.

Sample Input 2

4
ZZ
ZZZ
Z
ZZZZZZZZZZ

Sample Output 2

0

Note that there may be no ways to choose three people so that the given conditions are met.

Sample Input 3

5
CHOKUDAI
RNG
MAKOTO
AOKI
RINGO

Sample Output 3

7


program march
  implicit none
  integer :: n, i, j, k
  integer(8) :: cnt(5) = 0_8, ans = 0_8
  character(10) :: s
  read(*,*) n
  do i = 1, n
    read(*,*) s
    select case (s(1:1))
    case ("M")
      cnt(1) = cnt(1)+1_8
    case ("A")
      cnt(2) = cnt(2)+1_8
    case ("R")
      cnt(3) = cnt(3)+1_8
    case ("C")
      cnt(4) = cnt(4)+1_8
    case ("H")
      cnt(5) = cnt(5)+1_8
    end select
  end do
  do i = 1, 3
    do j = i+1, 4
      do k = j+1, 5
        ans = ans+cnt(i)*cnt(j)*cnt(k)
      end do
    end do
  end do
  write(*,'(i0)') ans
end program march


program prob4
    implicit none
    integer(16)::N, M
    integer(16)::i,j,ans,com
    integer::v(17)
    read(*,*) N

    v = 0
    M = N
    do i = 16, 0, -1
        if(N >= 10**i) then
            v(i+1) = N/(10**i)
            N = N - N/(10**i) * 10**i
        end if
    end do

    ans = sum(v)
    do i = 17, 1, -1
        if(v(i) > 0)then
            j = i
            exit
        end if
    end do

    if(M/10 > 0)then
        if( M < 100)then
            if(M/10 > 2) then
                write(*,*) M/10 + 8
            else
                write(*,*) 9
            end if
            stop
        else
            if(v(j) + v(j-1) > 9) then
                ans = v(j) + v(j-1) + 9*(j-2)
                com = 0
                do i = 1, j-2
                    com = v(i)*10**(i-1) + v(j-1)*10**(j-2) + v(j)*10**j
                end do
                if(com > M)then
                    ans = ans - 1
                end if
            else if(v(j) > 1)then
                ans = v(j) - 1 + 9*(j-1)
            else
                if(sum(v) - 1 < 9*(j-1))then
                    ans = 9*(j-1)
                end if
            end if
        end if
    end if

    write(*,*) ans
    stop
end program

Score : 300 points

Problem Statement

Find the maximum possible sum of the digits (in base 10) of a positive integer not greater than N.

Constraints

1\leq N \leq 10^{16}

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the maximum possible sum of the digits (in base 10) of a positive integer not greater than N.

Sample Input 1

100

Sample Output 1

18

For example, the sum of the digits in 99 is 18, which turns out to be the maximum value.

Sample Input 2

9995

Sample Output 2

35

For example, the sum of the digits in 9989 is 35, which turns out to be the maximum value.

Sample Input 3

3141592653589793

Sample Output 3

137


program digit
	implicit none
    character(len=17) :: n
    character(len=17),parameter :: q = '99999999999999999'
    integer :: j
    integer :: i, ans

    read(*,*) n
    read(n(1:1),*) j !最高位の数字

    i = len_trim(n) !桁数

    if ( i==1 ) then
    	write(*,*) n
        stop
    end if

    if ( n(2:i) == q(2:i) ) then
    	ans = 9*(i-1) + j
        write (*,*) ans
        stop
    end if

    ans = 9*(i-1) + j - 1
    write(*,*) ans
    stop
end program digit


program main
  implicit none

  character(len=17) :: num
  integer, allocatable :: n(:)
  integer :: k, i, ans, end

  read(*,*) num
  k = len_trim(num)
  allocate(n(k))

  do i = 1, k
     read(num(i:i),*) n(i)
  end do

  end = 0
  ans = 0
  if(k == 1) then
     ans = n(1)
  else if(n(2) /= 9) then
     ans = 9 * (k-1) + (n(1) - 1)
  else
     ans = n(1)
     do i = 3, k
        if(n(i) == 9) then
           ans = ans + 9
           end = 1
        else
           ans = ans + 17 + 9 * (k - i)
           end = 0
           exit
        end if
     end do
     if(end == 1) then
        ans = ans + 9
     end if

  end if

  write(*,*) ans

  deallocate(n)

end program main


program A819B
    implicit none
    integer(8)::i,ans,Nlen
    character(16)::N
    integer(8),allocatable,dimension(:)::A
    read*,N
    Nlen=len_trim(N)
    ans=0
    allocate(A(Nlen))
    do i=1,Nlen
        A(i)=ichar(N(i:i))-48
        ans=ans+A(i)
    end do

    if(ans==A(1)+9*(Nlen-1))then
        print'(i0)',ans
    else
        print'(i0)',A(1)+9*(Nlen-1)-1
    end if
end program A819B


program coin
  implicit NONE
  integer::n, a, m

  read*,n, a
  m=mod(n,500)
  if (m.le.a) then
    print*,"Yes"
  else
    print*,"No"
  end if

end program coin

Score: 100 points

Problem Statement

E869120 has A 1-yen coins and infinitely many 500-yen coins.

Determine if he can pay exactly N yen using only these coins.

Constraints

N is an integer between 1 and 10000 (inclusive).

A is an integer between 0 and 1000 (inclusive).

Input

Input is given from Standard Input in the following format:

N
A

Output

If E869120 can pay exactly N yen using only his 1-yen and 500-yen coins, print Yes; otherwise, print No.

Sample Input 1

2018
218

Sample Output 1

Yes

We can pay 2018 yen with four 500-yen coins and 18 1-yen coins, so the answer is Yes.

Sample Input 2

2763
0

Sample Output 2

No

When we have no 1-yen coins, we can only pay a multiple of 500 yen using only 500-yen coins. Since 2763 is not a multiple of 500, we cannot pay this amount.

Sample Input 3

37
514

Sample Output 3

Yes


module ABC088

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

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

	subroutine ABC088_A

		! variables for this <subroutine>
		character( len=3, kind=1 ) :: stat
		integer( kind= INT16 )     :: N, A

		! STEP.01
		! read out the target positive integers
		read *, N
		read *, A

		! STEP.02
		! judge whether the target integer is the multiplie of 4 or not
		if( mod( N, 500 ) .le. A ) then
			stat = 'Yes'
		else
			stat = 'No'
		end if

		! STEP.03
		! output the result
		write( unit=OUTPUT_UNIT, fmt='(A)', advance='yes' ) trim( stat )

		! STEP.END
		return

	end subroutine ABC088_A

end module ABC088

program main

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

	! require all variables to be explicitly declared
	implicit none

	call ABC088_A

end program main


      PROGRAM cardGameForTwo
      IMPLICIT NONE
      INTEGER :: N
      INTEGER,ALLOCATABLE :: a(:)
      INTEGER :: buffer(2),ans,i,piyo, alice,bob
      LOGICAL :: flag

      READ*,N
      ALLOCATE( a(N) )
      READ*,a(:)

      DO while(.TRUE.)
        flag = .TRUE.
        DO i = 1,N-1
          IF(a(i)<a(i+1))THEN
            buffer(1:2) = a(i:i+1)
            a(i) = buffer(2); a(i+1) = buffer(1)
            flag = .FALSE.
          END IF
        END DO

        IF(flag)THEN
        !sort has end
        EXIT
        END IF
      END DO

      ans = 0
      alice = 0
      bob = 0

      flag = .TRUE.
      DO i = 1,N
        IF(flag)THEN
          alice = alice + a(i)
          flag = .FALSE.
        ELSE
          bob = bob + a(i)
          flag = .TRUE.
        END IF
      END DO

      ans = alice - bob

      print'(I0)',ans

      END PROGRAM

Score: 200 points

Problem Statement

We have N cards. A number a_i is written on the i-th card.

Alice and Bob will play a game using these cards. In this game, Alice and Bob alternately take one card. Alice goes first.

The game ends when all the cards are taken by the two players, and the score of each player is the sum of the numbers written on the cards he/she has taken. When both players take the optimal strategy to maximize their scores, find Alice's score minus Bob's score.

Constraints

N is an integer between 1 and 100 (inclusive).

a_i \ (1 \leq i \leq N) is an integer between 1 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 a_3 ... a_N

Output

Print Alice's score minus Bob's score when both players take the optimal strategy to maximize their scores.

Sample Input 1

2
3 1

Sample Output 1

2

First, Alice will take the card with 3. Then, Bob will take the card with 1.
The difference of their scores will be 3 - 1 = 2.

Sample Input 2

3
2 7 4

Sample Output 2

5

First, Alice will take the card with 7. Then, Bob will take the card with 4. Lastly, Alice will take the card with 2. The difference of their scores will be 7 - 4 + 2 = 5. The difference of their scores will be 3 - 1 = 2.

Sample Input 3

4
20 18 2 18

Sample Output 3

18


INTEGER M,ALICE,BOB
INTEGER,ALLOCATABLE,DIMENSION(:)::A
READ*,M
ALLOCATE(A(M))
READ*,A
CALL BUBBLESORT(M,A)

ALICE=0
BOB=0
DO I=1,M,2
 ALICE=ALICE+A(I)
END DO
DO I=2,M,2
 BOB=BOB+A(I)
END DO
PRINT"(I0)",ALICE-BOB

CONTAINS

subroutine bubblesort(N,array)
  integer,intent(in)::N
  INTEGER,intent(inout)::array(1:N)
  integer::i,j
  INTEGER::t

  do i=1,N-1
     do j=i+1,N
        if(array(i) < array(j))then
           t=array(i)
           array(i)=array(j)
           array(j)=t
        end if
     end do
  end do

  return
end subroutine bubblesort
END


implicit none
INTEGER(4) :: n,i,ans,k
REAL(8),ALLOCATABLE,DIMENSION(:) :: a

ans = 0
k = 0

READ(5,*) n
ALLOCATE(a(n))
READ(5,*) (a(i),i=1,n)

call heapsort(n,a)

do i=n,1,-1
    if(MOD(n-i,2)==0)then
        ans = ans+a(i)
    else
        ans = ans-a(i)
    endif
enddo

WRITE(6,"(i0)") ans

end

!---http://slpr.sakura.ne.jp/qp/sortf90/---
subroutine heapsort(n,array)
    implicit none
    INTEGER(4),INTENT(IN) :: n
    DOUBLE PRECISION,INTENT(INOUT) :: array(1:n)

    INTEGER(4) :: i,k,j,l
    DOUBLE PRECISION :: t

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

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


program probB
  implicit none
  integer n
  integer i,j,k,alice,bob
  integer,allocatable :: a(:),a_tmp(:)

  read(*,*) n
  allocate(a(n),a_tmp(n))
  read(*,*) a(1:n)

  a_tmp=a
  call quicksort(a_tmp,1,n)

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

  alice=0
  bob=0

  do i=n,1,-1
     if(mod(i,2)==1) then
        alice=alice+a(i)
     else
        bob=bob+a(i)
     end if
  end do

  write(*,*) alice-bob

end program probB

recursive subroutine quicksort(a, first, last)
  implicit none
  integer a(*)
  integer first, last
  integer i, j
  integer 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


Score: 300 points

Problem Statement

We have a 3 \times 3 grid. A number c_{i, j} is written in the square (i, j), where (i, j) denotes the square at the i-th row from the top and the j-th column from the left.

According to Takahashi, there are six integers a_1, a_2, a_3, b_1, b_2, b_3 whose values are fixed, and the number written in the square (i, j) is equal to a_i + b_j.

Determine if he is correct.

Constraints

c_{i, j} \ (1 \leq i \leq 3, 1 \leq j \leq 3) is an integer between 0 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

c_{1,1} c_{1,2} c_{1,3}
c_{2,1} c_{2,2} c_{2,3}
c_{3,1} c_{3,2} c_{3,3}

Output

If Takahashi's statement is correct, print Yes; otherwise, print No.

Sample Input 1

1 0 1
2 1 2
1 0 1

Sample Output 1

Yes

Takahashi is correct, since there are possible sets of integers such as: a_1=0,a_2=1,a_3=0,b_1=1,b_2=0,b_3=1.

Sample Input 2

2 2 2
2 1 2
2 2 2

Sample Output 2

No

Takahashi is incorrect in this case.

Sample Input 3

0 8 8
0 8 8
0 8 8

Sample Output 3

Yes

Sample Input 4

1 8 6
2 9 7
0 7 7

Sample Output 4

No


module mod_que
    type queue
        integer,pointer:: v(:)
        integer(4):: i=0
    end type
contains
    subroutine que_init(q)
        type(queue):: q
        allocate(q%v(1))
    end subroutine

    subroutine que(q,num)
        type(queue):: q
        integer(4):: num

        q%i=q%i+1
        if (q%i > size(q%v)) call add(q)
        q%v(q%i) = num
    end subroutine

    subroutine add(q)
        type(queue):: q
        integer(4),pointer:: tmp(:)
        integer(4):: l

        l = size(q%v)
        allocate(tmp(l))
        tmp(1:l) = q%v(1:l)
        deallocate(q%v)
        allocate(q%v(2*l))
        q%v(1:l) = tmp(1:l)
        deallocate(tmp)
    end subroutine

    function deque(q) result(ret)
        type(queue):: q
        integer(4):: ret

        ret = q%v(1)
        q%i=q%i-1
        q%v(1:q%i) = q%v(2:q%i+1)
    end function
end module mod_que

program name
    use mod_que
    implicit none
    integer(4):: h,w
    integer(4),allocatable:: dtc(:,:)
    integer(4):: i,j, nx,ny, dx,dy
    integer(4):: black_num
    character(1),allocatable:: c(:,:)
    character(:),allocatable:: inpt_c
    type(queue):: x,y
    call que_init(x)
    call que_init(y)
    read*, h,w

    allocate(dtc(w,h), c(0:w+1,0:h+1))
    allocate(character(w):: inpt_c)
    black_num = 0
    do i=1,h
        read*, inpt_c(:)
        do j=1,w
            c(j,i) = inpt_c(j:j)
            if (c(j,i) == '#') black_num=black_num+1
        end do
    end do

    c(:,0) = '#'
    c(:,h+1) = '#'
    c(0,:) = '#'
    c(w+1,:) = '#'

    call que(x,1)
    call que(y,1)
    dtc(:,:) = -1
    dtc(1,1) = 1

    do while(x%i > 0)
        nx = deque(x)
        ny = deque(y)
        do dx=-1,1
            do dy=-1,1
                if (abs(dx) == abs(dy)) cycle
                if (c(nx+dx,ny+dy) == '#') cycle
                if (dtc(nx+dx, ny+dy) == -1) then
                    call que(x,nx+dx)
                    call que(y,ny+dy)
                    dtc(nx+dx,ny+dy) = dtc(nx,ny)+1
                else if (dtc(nx+dx,ny+dy) > dtc(nx,ny)+1) then
                    call que(x,nx+dx)
                    call que(y,ny+dy)
                    dtc(nx+dx,ny+dy) = dtc(nx,ny)+1
                end if
            end do
        end do
    end do

    if (dtc(w,h) == -1) then
        print*, -1
    else
        print*, w*h - dtc(w,h) - black_num
    end if
end program name

Score: 400 points

Problem statement

We have an H \times W grid whose squares are painted black or white. The square at the i-th row from the top and the j-th column from the left is denoted as (i, j).

Snuke would like to play the following game on this grid. At the beginning of the game, there is a character called Kenus at square (1, 1). The player repeatedly moves Kenus up, down, left or right by one square. The game is completed when Kenus reaches square (H, W) passing only white squares.

Before Snuke starts the game, he can change the color of some of the white squares to black. However, he cannot change the color of square (1, 1) and (H, W). Also, changes of color must all be carried out before the beginning of the game.

When the game is completed, Snuke's score will be the number of times he changed the color of a square before the beginning of the game. Find the maximum possible score that Snuke can achieve, or print -1 if the game cannot be completed, that is, Kenus can never reach square (H, W) regardless of how Snuke changes the color of the squares.

The color of the squares are given to you as characters s_{i, j}. If square (i, j) is initially painted by white, s_{i, j} is .; if square (i, j) is initially painted by black, s_{i, j} is #.

Constraints

H is an integer between 2 and 50 (inclusive).

W is an integer between 2 and 50 (inclusive).

s_{i, j} is . or # (1 \leq i \leq H, 1 \leq j \leq W).

s_{1, 1} and s_{H, W} are ..

Input

Input is given from Standard Input in the following format:

H W
s_{1, 1}s_{1, 2}s_{1, 3} ... s_{1, W}
s_{2, 1}s_{2, 2}s_{2, 3} ... s_{2, W}
: :
s_{H, 1}s_{H, 2}s_{H, 3} ... s_{H, W}

Output

Print the maximum possible score that Snuke can achieve, or print -1 if the game cannot be completed.

Sample Input 1

3 3
..#
#..
...

Sample Output 1

2

The score 2 can be achieved by changing the color of squares as follows:

Sample Input 2

10 37
.....................................
...#...####...####..###...###...###..
..#.#..#...#.##....#...#.#...#.#...#.
..#.#..#...#.#.....#...#.#...#.#...#.
.#...#.#..##.#.....#...#.#.###.#.###.
.#####.####..#.....#...#..##....##...
.#...#.#...#.#.....#...#.#...#.#...#.
.#...#.#...#.##....#...#.#...#.#...#.
.#...#.####...####..###...###...###..
.....................................

Sample Output 2

209


program main
    implicit none
    integer X, Y, i
    integer(8) N

    read(*,*) X, Y

    if(mod(X,Y)==0)then
        write(*,*) -1
        stop
    endif

    do i = 1, 1000000000
        N = X*i
        if(mod(N,Y)/=0)then
            write(*,*) N
            stop
        endif
    enddo

    write(*,*) -1

end program

Score : 100 points

Problem Statement

You are given positive integers X and Y.
If there exists a positive integer not greater than 10^{18} that is a multiple of X but not a multiple of Y, choose one such integer and print it.
If it does not exist, print -1.

Constraints

1 ≤ X,Y ≤ 10^9

X and Y are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

Print a positive integer not greater than 10^{18} that is a multiple of X but not a multiple of Y, or print -1 if it does not exist.

Sample Input 1

8 6

Sample Output 1

16

For example, 16 is a multiple of 8 but not a multiple of 6.

Sample Input 2

3 3

Sample Output 2

-1

A multiple of 3 is a multiple of 3.


program probB
  implicit none
  integer*8 n,i,j,pl,mi,tmp
  integer*8,allocatable :: a(:),b(:),del(:)

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

  del(:)=b(:)-a(:)

  if(sum(del)<0) then
     write(*,*)'No'
     stop
  end if

  do i=1,n
     if(del(i)>0) then
        pl=pl+del(i)
     else
        mi=mi-del(i)
     end if
  end do

  tmp=1
  do i=1,n
     do while(del(i)<0)
        do j=tmp,n
           if(del(j)>0) then
              a(j)=a(j)+2
              b(i)=b(i)+1
              del(i)=b(i)-a(i)
              del(j)=b(j)-a(j)
              tmp=j
              if(sum(del)<0) then
                 write(*,*)'No'
                 stop
              end if
              exit
           end if
        end do
        if(sum(del)<0) then
           write(*,*)'No'
           stop
        end if
     end do
  end do

  write(*,*) 'Yes'
end program probB

Score : 300 points

Problem Statement

You are given two integer sequences of length N: a_1,a_2,..,a_N and b_1,b_2,..,b_N.
Determine if we can repeat the following operation zero or more times so that the sequences a and b become equal.

Operation: Choose two integers i and j (possibly the same) between 1 and N (inclusive), then perform the following two actions simultaneously:

Add 2 to a_i.

Add 1 to b_j.

Constraints

1 ≤ N ≤ 10 000

0 ≤ a_i,b_i ≤ 10^9 (1 ≤ i ≤ N)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 .. a_N
b_1 b_2 .. b_N

Output

If we can repeat the operation zero or more times so that the sequences a and b become equal, print Yes; otherwise, print No.

Sample Input 1

3
1 2 3
5 2 2

Sample Output 1

Yes

For example, we can perform three operations as follows to do our job:

First operation: i=1 and j=2. Now we have a = \{3,2,3\}, b = \{5,3,2\}.

Second operation: i=1 and j=2. Now we have a = \{5,2,3\}, b = \{5,4,2\}.

Third operation: i=2 and j=3. Now we have a = \{5,4,3\}, b = \{5,4,3\}.

Sample Input 2

5
3 1 4 1 5
2 7 1 8 2

Sample Output 2

No

Sample Input 3

5
2 7 1 8 2
3 1 4 1 5

Sample Output 3

No