program main
  implicit none

  integer :: i,j,n,tt,c(100),t(100),chk=0,big = 100000,best

  read(*,*)n,tt
  do i = 1, n
    read(*,*)c(i),t(i)
    if (t(i) <= tt) then
      chk = 1
    end if
  end do
  if (chk == 0) then
    write(*,*)'TLE'
    stop
  end if

  do i = 1, n
    if (t(i) > tt) then
      cycle
    end if
    if (c(i) < big) then
      big = c(i)
      best = i
    end if
  end do
  write(*,*)big
end program main

Score : 200 points

Problem Statement

When Mr. X is away from home, he has decided to use his smartwatch to search the best route to go back home, to participate in ABC.

You, the smartwatch, has found N routes to his home.

If Mr. X uses the i-th of these routes, he will get home in time t_i at cost c_i.

Find the smallest cost of a route that takes not longer than time T.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq T \leq 1000

1 \leq c_i \leq 1000

1 \leq t_i \leq 1000

The pairs (c_i, t_i) are distinct.

Input

Input is given from Standard Input in the following format:

N T
c_1 t_1
c_2 t_2
:
c_N t_N

Output

Print the smallest cost of a route that takes not longer than time T.

If there is no route that takes not longer than time T, print TLE instead.

Sample Input 1

3 70
7 60
1 80
4 50

Sample Output 1

4

The first route gets him home at cost 7.

The second route takes longer than time T = 70.

The third route gets him home at cost 4.

Thus, the cost 4 of the third route is the minimum.

Sample Input 2

4 3
1 1000
2 4
3 1000
4 500

Sample Output 2

TLE

There is no route that takes not longer than time T = 3.

Sample Input 3

5 9
25 8
5 9
4 10
1000 1000
6 1

Sample Output 3

5


program partition
  implicit none
  integer :: n, m, i
  read(*,*) n, m
  do i = m/n, 1, -1
     if (mod(m,i) .ne. 0) cycle
     write(*,'(i0)') i
     stop
  end do
end program partition

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

Score : 400 points

Problem Statement

You are given integers N and M.

Consider a sequence a of length N consisting of positive integers such that a_1 + a_2 + ... + a_N = M. Find the maximum possible value of the greatest common divisor of a_1, a_2, ..., a_N.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

N \leq M \leq 10^9

Input

Input is given from Standard Input in the following format:

N M

Output

Print the maximum possible value of the greatest common divisor of a sequence a_1, a_2, ..., a_N that satisfies the condition.

Sample Input 1

3 14

Sample Output 1

2

Consider the sequence (a_1, a_2, a_3) = (2, 4, 8). Their greatest common divisor is 2, and this is the maximum value.

Sample Input 2

10 123

Sample Output 2

3

Sample Input 3

100000 1000000000

Sample Output 3

10000

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 partition
  implicit none
  integer(8) :: n, m, i
  read(*,*) n, m
  do i = m/n, 1, -1
     if (mod(m,i) .ne. 0) cycle
     write(*,'(i0)') i
     stop
  end do
end program partition


program AtCoder_Beginner_Contest_999
  implicit none
  integer :: n
  read(*,*) n
  write(*,'(i0)') 1110-n
end program AtCoder_Beginner_Contest_999

Score : 100 points

Problem Statement

Cat Snuke is learning to write characters.
Today, he practiced writing digits 1 and 9, but he did it the other way around.

You are given a three-digit integer n written by Snuke.
Print the integer obtained by replacing each digit 1 with 9 and each digit 9 with 1 in n.

Constraints

111 \leq n \leq 999

n is an integer consisting of digits 1 and 9.

Input

Input is given from Standard Input in the following format:

n

Output

Print the integer obtained by replacing each occurrence of 1 with 9 and each occurrence of 9 with 1 in n.

Sample Input 1

119

Sample Output 1

991

Replace the 9 in the ones place with 1, the 1 in the tens place with 9 and the 1 in the hundreds place with 9. The answer is 991.

Sample Input 2

999

Sample Output 2

111


program sunuke

implicit none

integer::    n
integer::    x,y,z

read(*,*) n
x=n/100
n=n-100*x
y=n/10
n=n-10*n

if(x==1) then
  x=9
else
  x=1
endif
if(y==1) then
  y=9
else
  y=1
endif
if(z==1) then
  z=9
else
  z=1
endif

n=100*x+10*y+z

write(*,*) n

end program sunuke


program contest999
  implicit none
  integer :: n
  read(*,*) n
  write(*,'(i0)') 1110-n
  stop
end program contest999


program AtCoder_Beginner_Contest_111
  implicit none
  integer :: n
  read(*,*) n
  write(*,'(i0)') (n+110)/111*111
end program AtCoder_Beginner_Contest_111

Score : 200 points

Problem Statement

Kurohashi has never participated in AtCoder Beginner Contest (ABC).

The next ABC to be held is ABC N (the N-th ABC ever held).
Kurohashi wants to make his debut in some ABC x such that all the digits of x in base ten are the same.

What is the earliest ABC where Kurohashi can make his debut?

Constraints

100 \leq N \leq 999

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If the earliest ABC where Kurohashi can make his debut is ABC n, print n.

Sample Input 1

111

Sample Output 1

111

The next ABC to be held is ABC 111, where Kurohashi can make his debut.

Sample Input 2

112

Sample Output 2

222

The next ABC to be held is ABC 112, which means Kurohashi can no longer participate in ABC 111.
Among the ABCs where Kurohashi can make his debut, the earliest one is ABC 222.

Sample Input 3

750

Sample Output 3

777


program main
implicit None
	integer(8)::n,i,p(2),q(2),r(2),s(2)
	integer(8),allocatable::v(:),a(:),b(:)

	read*,n
	allocate(v(n),a(n/2),b(n/2))

	read*,v
	n = n/2
	if(minval(v) == maxval(v))then
		print 101,n
		stop
	endif

	do i = 1,n
		a(i) = v(i*2-1)
		b(i) = v(i*2)
	enddo

	call h1(a,n)
	call h1(b,n)

	p =0;q =0;r =0;s =0

	call a1(a,n,p,q)
	call a1(b,n,r,s)

	if(q(1) /= s(1))then
		print 101,2*n-p(1)-r(1)
		stop
	endif

	i = 2*n-p(1)-r(2)
	if(i > 2*n-p(2)-r(1)) i = 2*n-p(2)-r(1)

	print 101,i

101 format(i0)
end program main

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

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

subroutine a1(a,n,p,q)
Implicit None
integer(8)::n,p(2),q(2),i,t=1
integer(8)::a(n)
	do i = 2,n
		if(a(i) == a(i-1))then
			t = t+1
			else
			if (t > p(1))then
				p(2) = p(1)
				p(1) = t
				q(2) = q(1)
				q(1) = a(i-1)
				t = 1
				else if(t > p(2))then
				p(2) = t
				q(2) = a(i-1)
				t = 1
			endif
		endif
	enddo

	if (t > p(1))then
		p(2) = p(1)
		p(1) = t
		q(2) = q(1)
		q(1) = a(i-1)
		t = 1
		else if(t > p(2))then
		p(2) = t
		q(2) = a(i-1)
		t = 1
	endif
end subroutine a1

Score : 300 points

Problem Statement

A sequence a_1,a_2,... ,a_n is said to be /\/\/\/ when the following conditions are satisfied:

For each i = 1,2,..., n-2, a_i = a_{i+2}.

Exactly two different numbers appear in the sequence.

You are given a sequence v_1,v_2,...,v_n whose length is even.
We would like to make this sequence /\/\/\/ by replacing some of its elements.
Find the minimum number of elements that needs to be replaced.

Constraints

2 \leq n \leq 10^5

n is even.

1 \leq v_i \leq 10^5

v_i is an integer.

Input

Input is given from Standard Input in the following format:

n
v_1 v_2 ... v_n

Output

Print the minimum number of elements that needs to be replaced.

Sample Input 1

4
3 1 3 2

Sample Output 1

1

The sequence 3,1,3,2 is not /\/\/\/, but we can make it /\/\/\/ by replacing one of its elements: for example, replace the fourth element to make it 3,1,3,1.

Sample Input 2

6
105 119 105 119 105 119

Sample Output 2

0

The sequence 105,119,105,119,105,119 is /\/\/\/.

Sample Input 3

4
1 1 1 1

Sample Output 3

2

The elements of the sequence 1,1,1,1 are all the same, so it is not /\/\/\/.


program prob4
    implicit none
    integer::N, i, ans, l, j, k
    character(len=200005)::S,T
    integer::c(26), d(26)
    read(*,*) S, T
    l = len_trim(S)
    c = 0
    d = 0

    do i = 1, l
        j = ichar(S(i:i)) - 96
        k = ichar(T(i:i)) - 96
        if(c(j) > 0) then
            if(k .ne. c(j)) then
                write(*,'(a)') "No"
                stop
            end if
        else
            c(j) = k
        end if
        if(d(k) > 0) then
            if(j .ne. d(k)) then
                write(*,'(a)') "No"
                stop
            end if
        else
            d(k) = j
        end if
    end do
    write(*,'(a)') "Yes"
    stop
end program

Score : 300 points

Problem Statement

You are given strings S and T consisting of lowercase English letters.

You can perform the following operation on S any number of times:

Operation: Choose two distinct lowercase English letters c_1 and c_2, then replace every occurrence of c_1 with c_2, and every occurrence of c_2 with c_1.

Determine if S and T can be made equal by performing the operation zero or more times.

Constraints

1 \leq |S| \leq 2 \times 10^5

|S| = |T|

S and T consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S
T

Output

If S and T can be made equal, print Yes; otherwise, print No.

Sample Input 1

azzel
apple

Sample Output 1

Yes

azzel can be changed to apple, as follows:

Choose e as c_1 and l as c_2. azzel becomes azzle.

Choose z as c_1 and p as c_2. azzle becomes apple.

Sample Input 2

chokudai
redcoder

Sample Output 2

No

No sequences of operation can change chokudai to redcoder.

Sample Input 3

abcdefghijklmnopqrstuvwxyz
ibyhqfrekavclxjstdwgpzmonu

Sample Output 3

Yes


program string_transformation
  implicit none
  character(200000) :: s, t
  integer :: n, i, j, a, b
  logical :: x(26,26) = .false.
  read(*,*) s
  read(*,*) t
  n = len_trim(s)
  do i = 1, n
    x(ord(s(i:i)),ord(t(i:i))) = .true.
  end do
  do i = 1, 26
    a = 0
    b = 0
    do j = 1, 26
      if (x(i,j)) a = a+1
      if (x(j,i)) b = b+1
    end do
    if (a > 1 .or. b > 1) then
      write(*,'(a)') "No"
      stop
    end if
  end do
  write(*,'(a)') "Yes"
contains
  integer function ord(s)
    character(1), intent(in) :: s
    ord = ichar(s)-96
  end
end program string_transformation


program main
  implicit integer(a-z)
  allocatable::a(:)
  allocatable::sort(:)

  read(*,*)n,have
  allocate(a(n))
  allocate(sort(n))
  read(*,*)(a(i),i=1,n)

  do i=1,n
     sort(i)=minval(a(1:n-i+1))
     tmp=minval(a(1:n-i+1))
     do j=1,n-i+1
        if(tmp.eq.a(j)) exit
     end do
     a(j)=a(n-i+1)
     a(n-i+1)=tmp

    !write(*,*)"sort",sort
    !write(*,*)"a   ",a
    !write(*,*)
  end do

  do i=1,n
     if(sum(sort(1:i)).gt.have)then
        !ans=sum(a(1:i-1))
        exit
     end if
  end do

  if(sum(sort(1:i-1)).ne.have .and.&
       sum(sort(1:i-1)).ge.have) i=i-1

  !write(*,*)"sort",sort
  write(*,*)i-1!,ans

end program main

Score : 200 points

Problem Statement

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

Snuke has decided to distribute x sweets among them.
He needs to give out all the x sweets, but some of the children may get zero sweets.

For each i (1 \leq i \leq N), Child i will be happy if he/she gets exactly a_i sweets.
Snuke is trying to maximize the number of happy children by optimally distributing the sweets.
Find the maximum possible number of happy children.

Constraints

All values in input are integers.

2 \leq N \leq 100

1 \leq x \leq 10^9

1 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N x
a_1 a_2 ... a_N

Output

Print the maximum possible number of happy children.

Sample Input 1

3 70
20 30 10

Sample Output 1

2

One optimal way to distribute sweets is (20, 30, 20).

Sample Input 2

3 10
20 30 10

Sample Output 2

1

The optimal way to distribute sweets is (0, 0, 10).

Sample Input 3

4 1111
1 10 100 1000

Sample Output 3

4

The optimal way to distribute sweets is (1, 10, 100, 1000).

Sample Input 4

2 10
20 20

Sample Output 4

0

No children will be happy, no matter how the sweets are distributed.


program main
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: k

    read*, k
    print'(i0)', (k/2)*(k/2+mod(k,2))
end program main

Score : 100 points

Problem Statement

Find the number of ways to choose a pair of an even number and an odd number from the positive integers between 1 and K (inclusive). The order does not matter.

Constraints

2\leq K\leq 100

K is an integer.

Input

Input is given from Standard Input in the following format:

K

Output

Print the number of ways to choose a pair of an even number and an odd number from the positive integers between 1 and K (inclusive).

Sample Input 1

3

Sample Output 1

2

Two pairs can be chosen: (2,1) and (2,3).

Sample Input 2

6

Sample Output 2

9

Sample Input 3

11

Sample Output 3

30

Sample Input 4

50

Sample Output 4

625


program prob2
    implicit none
    integer::x1, x2, x3, x4, y1, y2, y3, y4, dx, dy
    read(*,*) x1, y1, x2, y2
    dx = x2 - x1
    dy = y2 - y1
    if(dx >= 0 .and. dy >= 0) then !-+
        x3 = x2 - dy
        y3 = y2 + dx
    else if(dx >= 0 .and. dy <= 0) then !++
        x3 = x2 - dy
        y3 = y2 + dx
    else if(dx <= 0 .and. dy >= 0) then !--
        x3 = x2 - dy
        y3 = y2 + dx
    else if(dx <= 0 .and. dy <= 0) then !+-
        x3 = x2 - y2
        y3 = y2 + x2
    end if
    x4 = x3 - dx
    y4 = y3 - dy
    write(*,*) x3, y3, x4, y4
    stop
end program

Score : 200 points

Problem Statement

There is a square in the xy-plane. The coordinates of its four vertices are (x_1,y_1),(x_2,y_2),(x_3,y_3) and (x_4,y_4) in counter-clockwise order.
(Assume that the positive x-axis points right, and the positive y-axis points up.)

Takahashi remembers (x_1,y_1) and (x_2,y_2), but he has forgot (x_3,y_3) and (x_4,y_4).

Given x_1,x_2,y_1,y_2, restore x_3,y_3,x_4,y_4. It can be shown that x_3,y_3,x_4 and y_4 uniquely exist and have integer values.

Constraints

|x_1|,|y_1|,|x_2|,|y_2| \leq 100

(x_1,y_1) ≠ (x_2,y_2)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

x_1 y_1 x_2 y_2

Output

Print x_3,y_3,x_4 and y_4 as integers, in this order.

Sample Input 1

0 0 0 1

Sample Output 1

-1 1 -1 0

(0,0),(0,1),(-1,1),(-1,0) is the four vertices of a square in counter-clockwise order.
Note that (x_3,y_3)=(1,1),(x_4,y_4)=(1,0) is not accepted, as the vertices are in clockwise order.

Sample Input 2

2 3 6 6

Sample Output 2

3 10 -1 7

Sample Input 3

31 -41 -59 26

Sample Output 3

-126 -64 -36 -131


INTEGER A,B,C,D
READ*,A,B,C,D
WRITE(*,FMT="(I0)",advance='no') C+B-D,D-A+C,A+B-D,B-A+C
END


Score : 300 points

Problem Statement

You are given integers N and K. Find the number of triples (a,b,c) of positive integers not greater than N such that a+b,b+c and c+a are all multiples of K.
The order of a,b,c does matter, and some of them can be the same.

Constraints

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

N and K are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the number of triples (a,b,c) of positive integers not greater than N such that a+b,b+c and c+a are all multiples of K.

Sample Input 1

3 2

Sample Output 1

9

(1,1,1),(1,1,3),(1,3,1),(1,3,3),(2,2,2),(3,1,1),(3,1,3),(3,3,1) and (3,3,3) satisfy the condition.

Sample Input 2

5 3

Sample Output 2

1

Sample Input 3

31415 9265

Sample Output 3

27

Sample Input 4

35897 932

Sample Output 4

114191


program prob5
    implicit none
    integer(8) :: n, k
    integer(8) :: ans
    read(*,*) n, k
    ans = 0
    if(mod(k , 2) == 0) then
        ans = (N/K) * (N/K) * (N/K)
        ans = ans + ((N/(K/2))+1)/2 * ((N/(K/2))+1)/2 * ((N/(K/2))+1)/2
    else
        ans = (N/K) * (N/K) * (N/K)
    end if
    write(*,*) ans
    stop
contains
end program prob5


program arc102_a
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: n,k,i
    integer(int32):: a0, ah
    read*, n,k
    a0=n/k
    ah=0
    if (mod(k,2) == 0) then
        do i=1,n
            if (mod(i,k) == k/2) ah=ah+1
        end do
    end if

    print'(i0)', a0**3 + ah**3

end program arc102_a


program name
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: h,w,i,j
    character(1),allocatable:: a(:,:),ans(:,:)

    read*, h,w
    allocate(a(w,h),ans(w,h))
    a(:,:) = ' '
    ans(:,:) = ' '

    block
        character(w):: inpt_a
        integer(int32):: diff=0
        do i=1,h
            read*, inpt_a
            if (all_dot(inpt_a)) then
                diff=diff+1
                cycle
            end if
            do j=1,w
                a(j,i-diff) = inpt_a(j:j)
            end do
        end do
    end block

    block
        character(h):: ah
        integer(int32):: diff

        do j=1,w
            diff=0
            do i=1,h
                ah(i:i) = a(j,i)
            end do
            if(all_dot(ah))then
                diff=diff+1
                cycle
            end if
            ans(j,:) = a(j+diff,:)
        end do
    end block

    block
        character(w):: ansc
        do i=1,h
            do j=1,w
                ansc(j:j) = ans(j,i)
            end do
            if (len_trim(ansc) == 0) cycle
            print'(a)', trim(ansc)
        end do
    end block

contains
        function all_dot(s) result(ret)
            character(*):: s
            logical:: ret
            integer(int32):: l,i

            l = len(s)
            ret = .true.
            do i=1,l
                if (s(i:i) == '#') ret=.false.
            end do
        end function
end program name

Score : 200 points

Problem Statement

There is a grid of squares with H horizontal rows and W vertical columns.
The square at the i-th row from the top and the j-th column from the left is represented as (i, j).
Each square is black or white.
The color of the square is given as an H-by-W matrix (a_{i, j}).
If a_{i, j} is ., the square (i, j) is white; if a_{i, j} is #, the square (i, j) is black.

Snuke is compressing this grid.
He will do so by repeatedly performing the following operation while there is a row or column that consists only of white squares:

Operation: choose any one row or column that consists only of white squares, remove it and delete the space between the rows or columns.

It can be shown that the final state of the grid is uniquely determined regardless of what row or column is chosen in each operation.
Find the final state of the grid.

Constraints

1 \leq H, W \leq 100

a_{i, j} is . or #.

There is at least one black square in the whole grid.

Input

Input is given from Standard Input in the following format:

H W
a_{1, 1}...a_{1, W}
:
a_{H, 1}...a_{H, W}

Output

Print the final state of the grid in the same format as input (without the numbers of rows and columns); see the samples for clarity.

Sample Input 1

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

Sample Output 1

###
###
.##

The second row and the third column in the original grid will be removed.

Sample Input 2

3 3
#..
.#.
..#

Sample Output 2

#..
.#.
..#

As there is no row or column that consists only of white squares, no operation will be performed.

Sample Input 3

4 5
.....
.....
..#..
.....

Sample Output 3

#

Sample Input 4

7 6
......
....#.
.#....
..#...
..#...
......
.#..#.

Sample Output 4

..#
#..
.#.
.#.
#.#


program test
integer::d
 allocatable :: a(:), b(:)
read(*,*) N, c
z=N-c+1
allocate(a(N),b(z))
read(*, *)(a(i), i = 1,N)
do i=1,N-c+1
k=i+c-1
x=a(i)
y=a(k)
t=abs(x)
u=abs(y)
m=min(t,u)
b(i)=m+(y-x)
end do
d=minval(b)
print*,d
end program test

Score : 300 points

Problem Statement

There are N candles placed on a number line.
The i-th candle from the left is placed on coordinate x_i.
Here, x_1 < x_2 < ... < x_N holds.

Initially, no candles are burning.
Snuke decides to light K of the N candles.

Now, he is at coordinate 0.
He can move left and right along the line with speed 1.
He can also light a candle when he is at the same position as the candle, in negligible time.

Find the minimum time required to light K candles.

Constraints

1 \leq N \leq 10^5

1 \leq K \leq N

x_i is an integer.

|x_i| \leq 10^8

x_1 < x_2 < ... < x_N

Input

Input is given from Standard Input in the following format:

N K
x_1 x_2 ... x_N

Output

Print the minimum time required to light K candles.

Sample Input 1

5 3
-30 -10 10 20 50

Sample Output 1

40

He should move and light candles as follows:

Move from coordinate 0 to -10.

Light the second candle from the left.

Move from coordinate -10 to 10.

Light the third candle from the left.

Move from coordinate 10 to 20.

Light the fourth candle from the left.

Sample Input 2

3 2
10 20 30

Sample Output 2

20

Sample Input 3

1 1
0

Sample Output 3

0

There may be a candle placed at coordinate 0.

Sample Input 4

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

Sample Output 4

10


program sample
	implicit none
    integer ::x,y

    read(*,*) x,y

    write(*,*) (x-1)*(y-1)

    stop
end program sample

Score: 100 points

Problem Statement

There is a farm whose length and width are A yard and B yard, respectively. A farmer, John, made a vertical road and a horizontal road inside the farm from one border to another, as shown below: (The gray part represents the roads.)

What is the area of this yard excluding the roads? Find it.

Note

It can be proved that the positions of the roads do not affect the area.

Constraints

A is an integer between 2 and 100 (inclusive).

B is an integer between 2 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

A B

Output

Print the area of this yard excluding the roads (in square yards).

Sample Input 1

2 2

Sample Output 1

1

In this case, the area is 1 square yard.

Sample Input 2

5 7

Sample Output 2

24

In this case, the area is 24 square yards.


program test
	implicit none
    integer :: A,B

    read(*,*) A,B

    write(*,*) A*B-A-B+1
	stop
end program test


program garden
	implicit none
    integer(8) :: A, B

    read(*,*) A,B
    write(*,*) (A-1)*(B-1)
    stop

end program garden


program prob1
    implicit none
    integer::A, B
    read(*,*) A, B
    write(*,*) A*B - A - B + 1

    stop

end program prob1


program main
  implicit none
  integer :: N
  integer,allocatable  :: divisor(:)
  integer :: out
  integer :: i,j
  read (*,*) N
  allocate(divisor(N))
  divisor = 0
  do i = 1, N
     do j = 1, i
        if (mod(i,j) ==0 ) then
           divisor(i) = divisor(i)+1
        end if
     end do
  end do
  out = 0
  do i = 1, N
     if ( mod(i,2) /= 0 .and. divisor(i) == 8 ) then
        out = out + 1
     end if
  end do
  write(*,'(i0)') out
  stop
end program main

Score: 200 points

Problem Statement

The number 105 is quite special - it is odd but still it has eight divisors.
Now, your task is this: how many odd numbers with exactly eight positive divisors are there between 1 and N (inclusive)?

Constraints

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

Input

Input is given from Standard Input in the following format:

N

Output

Print the count.

Sample Input 1

105

Sample Output 1

1

Among the numbers between 1 and 105, the only number that is odd and has exactly eight divisors is 105.

Sample Input 2

7

Sample Output 2

0

1 has one divisor. 3, 5 and 7 are all prime and have two divisors. Thus, there is no number that satisfies the condition.


program prime_numbers
    implicit none
    integer(8) i, j,n,m,Q,l,r,y,p1,q1
    integer, allocatable :: x(:,:),p(:,:)

    read (*,*)n ,m,q
    allocate(x(2,m))
    allocate(p(2,q))
    read(*,*)x
    read(*,*)p
    !transpose(x)
    !transpose(p)
    do i=1,q
        y=0
        p1=p(i,1)
        q1=p(i,2)
        do j=1,m
          if (p(i,1)<=x(j,1) .and. x(j,2)<=p(i,2))then
            y=y+1
          end if
        end do
        write(*,*)y
    end do
    deallocate(x)
    deallocate(p)

    stop
end program prime_numbers

Score: 300 points

Problem Statement

Mr. Infinity has a string S consisting of digits from 1 to 9. Each time the date changes, this string changes as follows:

Each occurrence of 2 in S is replaced with 22. Similarly, each 3 becomes 333, 4 becomes 4444, 5 becomes 55555, 6 becomes 666666, 7 becomes 7777777, 8 becomes 88888888 and 9 becomes 999999999. 1 remains as 1.

For example, if S is 1324, it becomes 1333224444 the next day, and it becomes 133333333322224444444444444444 the day after next.
You are interested in what the string looks like after 5 \times 10^{15} days. What is the K-th character from the left in the string after 5 \times 10^{15} days?

Constraints

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

K is an integer between 1 and 10^{18} (inclusive).

The length of the string after 5 \times 10^{15} days is at least K.

Input

Input is given from Standard Input in the following format:

S
K

Output

Print the K-th character from the left in Mr. Infinity's string after 5 \times 10^{15} days.

Sample Input 1

1214
4

Sample Output 1

2

The string S changes as follows:

Now: 1214

After one day: 12214444

After two days: 1222214444444444444444

After three days: 12222222214444444444444444444444444444444444444444444444444444444444444444

The first five characters in the string after 5 \times 10^{15} days is 12222. As K=4, we should print the fourth character, 2.

Sample Input 2

3
157

Sample Output 2

3

The initial string is 3. The string after 5 \times 10^{15} days consists only of 3.

Sample Input 3

299792458
9460730472580800

Sample Output 3

2


program main
     implicit none
     integer a
     read(*,*)a
     if(a<1200)then
          write(*,*)"ABC"
     elseif(a<2800)then
          write(*,*)"ARC"
     else
          write(*,*)"AGC"
     endif
end program main

Score : 100 points

Problem Statement

A programming competition site AtCode regularly holds programming contests.

The next contest on AtCode is called ABC, which is rated for contestants with ratings less than 1200.

The contest after the ABC is called ARC, which is rated for contestants with ratings less than 2800.

The contest after the ARC is called AGC, which is rated for all contestants.

Takahashi's rating on AtCode is R. What is the next contest rated for him?

Constraints

0 ≤ R ≤ 4208

R is an integer.

Input

Input is given from Standard Input in the following format:

R

Output

Print the name of the next contest rated for Takahashi (ABC, ARC or AGC).

Sample Input 1

1199

Sample Output 1

ABC

1199 is less than 1200, so ABC will be rated.

Sample Input 2

1200

Sample Output 2

ARC

1200 is not less than 1200 and ABC will be unrated, but it is less than 2800 and ARC will be rated.

Sample Input 3

4208

Sample Output 3

AGC


program answer
  implicit none

  character(len=100):: S, T, U
  integer :: i, j, a

  read(*,*) S
  read(*,*) T

  a=len_trim(S)

  if(a==1) then
     if(S==T) then
        write(*,*) 'Yes'
        stop
     else
        write(*,*) 'No'
        stop
     end if
  end if

  do j = 1, a-1
     U=S
        S(j+1:j+1)=U(j:j)
        S(1:1)=U(a:a)
        if(S==T) then
           write(*,*) 'Yes'
           stop
        end if
     end do

     write(*,*) 'No'

  stop
 end program answer

Score : 100 points

Problem Statement

You have three tasks, all of which need to be completed.

First, you can complete any one task at cost 0.

Then, just after completing the i-th task, you can complete the j-th task at cost |A_j - A_i|.

Here, |x| denotes the absolute value of x.

Find the minimum total cost required to complete all the task.

Constraints

All values in input are integers.

1 \leq A_1, A_2, A_3 \leq 100

Input

Input is given from Standard Input in the following format:

A_1 A_2 A_3

Output

Print the minimum total cost required to complete all the task.

Sample Input 1

1 6 3

Sample Output 1

5

When the tasks are completed in the following order, the total cost will be 5, which is the minimum:

Complete the first task at cost 0.

Complete the third task at cost 2.

Complete the second task at cost 3.

Sample Input 2

11 5 5

Sample Output 2

6

Sample Input 3

100 100 100

Sample Output 3

0


integer a,b,c
read*, a,b,c
print*, max(a,b,c) - min(a,b,c)
end


integer x(3)
read*,x
print"(I0)",maxval(x)-minval(x)
end


program task_scheduling_problem
  implicit none
  integer :: a(3)
  read(*,*) a(:)
  write(*,'(i0)') maxval(a) - minval(a)
  stop
end program task_scheduling_problem


program modulo_summation
  implicit none
  integer :: n, a(3000) = 0
  read(*,*) n
  read(*,*) a(1:n)
  write(*,'(i0)') sum(a)-n
end program modulo_summation

Score : 300 points

Problem Statement

You are given N positive integers a_1, a_2, ..., a_N.

For a non-negative integer m, let f(m) = (m\ mod\ a_1) + (m\ mod\ a_2) + ... + (m\ mod\ a_N).

Here, X\ mod\ Y denotes the remainder of the division of X by Y.

Find the maximum value of f.

Constraints

All values in input are integers.

2 \leq N \leq 3000

2 \leq a_i \leq 10^5

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the maximum value of f.

Sample Input 1

3
3 4 6

Sample Output 1

10

f(11) = (11\ mod\ 3) + (11\ mod\ 4) + (11\ mod\ 6) = 10 is the maximum value of f.

Sample Input 2

5
7 46 11 20 11

Sample Output 2

90

Sample Input 3

7
994 518 941 851 647 2 581

Sample Output 3

4527


integer,allocatable,dimension(:)::A
read*,i
allocate(A(i))
read*,A
print"(i0)",sum(A)-i
end


module sub
	implicit none
contains
subroutine heapsort2(n,array,turn)
  implicit none
  integer,intent(in)::n
  integer,intent(out)::turn(1:n)
  integer,intent(inout)::array(1:n)

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

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

  do i=1,N
     turn(i)=i
  enddo

  l=n/2+1
  k=n
  do while(k.ne.1)
     if(l.gt.1)then
        l=l-1
        t=array(l)
        m=turn(l)
     else
        t=array(k)
        m=turn(k)
        array(k)=array(1)
        turn(k)=turn(1)
        k=k-1
        if(k.eq.1) then
           array(1)=t
           turn(1)=m
           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)
           turn(i)=turn(j)
           i=j
           j=j+j
        else
           j=k+1
        endif
     enddo
     array(i)=t
     turn(i)=m
  enddo

  return
end subroutine heapsort2
end module sub

program main
	use sub
	implicit none
	integer :: N, M, i, j, counter
	integer, allocatable :: a(:), b(:), turn(:)
	read(*, *) N, M
	allocate(a(1:M))
	allocate(b(1:M))
	allocate(turn(1:M))
	do i = 1, M
		read(*, *) a(i), b(i)
	end do

	call heapsort2(M,a,turn)
	j = 1
	counter = 0
	do i = 1, M
		if(a(j) /= a(i)) then
			if(b(turn(j)) <= a(i)) then
				counter = counter + 1
				j = i
			else if(b(turn(j)) >= b(turn(i))) then
				j = i
			end if
		else
			if(b(turn(j)) > b(turn(i))) then
				j = i
			end if
		end if
	end do
	write(*, *) counter+1
end program main

Score : 400 points

Problem Statement

There are N islands lining up from west to east, connected by N-1 bridges.

The i-th bridge connects the i-th island from the west and the (i+1)-th island from the west.

One day, disputes took place between some islands, and there were M requests from the inhabitants of the islands:

Request i: A dispute took place between the a_i-th island from the west and the b_i-th island from the west. Please make traveling between these islands with bridges impossible.

You decided to remove some bridges to meet all these M requests.

Find the minimum number of bridges that must be removed.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq a_i < b_i \leq N

All pairs (a_i, b_i) are distinct.

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
a_2 b_2
:
a_M b_M

Output

Print the minimum number of bridges that must be removed.

Sample Input 1

5 2
1 4
2 5

Sample Output 1

1

The requests can be met by removing the bridge connecting the second and third islands from the west.

Sample Input 2

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

Sample Output 2

2

Sample Input 3

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

Sample Output 3

4


module sub
	implicit none
contains
subroutine heapsort2(n,array,turn)
  implicit none
  integer,intent(in)::n
  integer,intent(out)::turn(1:n)
  integer,intent(inout)::array(1:n)

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

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

  do i=1,N
     turn(i)=i
  enddo

  l=n/2+1
  k=n
  do while(k.ne.1)
     if(l.gt.1)then
        l=l-1
        t=array(l)
        m=turn(l)
     else
        t=array(k)
        m=turn(k)
        array(k)=array(1)
        turn(k)=turn(1)
        k=k-1
        if(k.eq.1) then
           array(1)=t
           turn(1)=m
           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)
           turn(i)=turn(j)
           i=j
           j=j+j
        else
           j=k+1
        endif
     enddo
     array(i)=t
     turn(i)=m
  enddo

  return
end subroutine heapsort2
end module sub

program main
	use sub
	implicit none
	integer :: N, M, i, j, counter
	integer, allocatable :: a(:), b(:), turn(:)
	read(*, *) N, M
	allocate(a(1:M))
	allocate(b(1:M))
	allocate(turn(1:M))
	do i = 1, M
		read(*, *) a(i), b(i)
	end do

	call heapsort2(M,a,turn)
	j = 1
	counter = 0
	do i = 1, M
	if(a(j) /= a(i)) then
		if(b(turn(j)) < a(i)) then
			counter = counter + 1
			j = i
		else if(b(turn(j)) > b(turn(i))) then
			j = i
		end if
	end if
	end do
	write(*, *) counter
end program main


program main
	implicit none
    character(1000) S, ans
	integer w, i
	read(*, *) S
	read(*, *) w
	do i = 1, len_trim(S)
		if(w == 1) then
			write(*, *) trim(S)
			goto 1
		else if(mod(i,w) == 1) then
			write(*, '(a)', advance = 'No') S(i:i)
		end if
	end do
	write(*, *) ''
1 continue
end program main

Score : 200 points

Problem Statement

You are given a string S consisting of lowercase English letters.
We will write down this string, starting a new line after every w letters. Print the string obtained by concatenating the letters at the beginnings of these lines from top to bottom.

Constraints

1 \leq w \leq |S| \leq 1000

S consists of lowercase English letters.

w is an integer.

Input

Input is given from Standard Input in the following format:

S
w

Output

Print the desired string in one line.

Sample Input 1

abcdefgh
3

Sample Output 1

adg

When we write down abcdefgh, starting a new line after every three letters, we get the following:

abc

def

gh

Concatenating the letters at the beginnings of these lines, we obtain adg.

Sample Input 2

lllll
1

Sample Output 2

lllll

Sample Input 3

souuundhound
2

Sample Output 3

suudon


module mod_graph
  implicit none
  integer(8), parameter :: inf = 1000000000000000000_8

  type item
    integer :: id = 0
    integer(8) :: wt = 0_8
  end type

  type priorityqueue
    integer :: num = 0
    type(item), pointer :: heap(:) => null()
  end type

  type edge
    integer :: fm = 0, to = 0, cp = 0, rv = 0
    integer(8) :: wt = 0_8
  end type

  type arraylist
    integer :: num = 0
    type(edge), pointer :: arr(:) => null()
  contains
    procedure :: bellman_ford => bellman_ford
  end type

  type graph
    type(arraylist), pointer :: egs(:) => null()
    logical, pointer :: used(:) => null()
  contains
    procedure :: add => add
    procedure :: dijkstra => dijkstra
    procedure :: ford_fulkerson => ford_fulkerson
  end type

  interface graph
    module procedure :: newg
  end interface graph

contains

  function newi(id,wt) result(ret)
    integer, intent(in) :: id
    integer(8), intent(in) :: wt
    type(item) :: ret
    ret%id = id
    ret%wt = wt
  end

  subroutine swapi(a,b)
    type(item), intent(inout) :: a, b
    type(item) :: c
    c = a
    a = b
    b = c
  end

  subroutine appendi(a)
    type(item), pointer, intent(inout) :: a(:)
    integer :: n
    type(item), allocatable :: tmp(:)
    n = size(a)
    allocate(tmp(n))
    tmp = a
    deallocate(a)
    allocate(a(2*n))
    a(1:n) = tmp
    deallocate(tmp)
  end

  function lessi(a,b) result(ret)
    type(item), intent(in) :: a, b
    logical :: ret
    ret = a%wt < b%wt
  end

  subroutine finalizei(pq)
    type(priorityqueue), intent(inout) :: pq
    if (associated(pq%heap)) deallocate(pq%heap)
    pq%num = 0
  end

  subroutine offer(pq,it)
    type(priorityqueue), intent(inout) :: pq
    type(item), intent(in) :: it
    integer :: i, j
    if (.not.associated(pq%heap)) allocate(pq%heap(1))
    if (pq%num == size(pq%heap)) call appendi(pq%heap)
    pq%num = pq%num+1
    pq%heap(pq%num) = it
    i = pq%num
    do while (i > 1)
      j = i/2
      if (lessi(pq%heap(i),pq%heap(j))) call swapi(pq%heap(i),pq%heap(j))
      i = j
    end do
  end

  function poll(pq) result(ret)
    type(priorityqueue), intent(inout) :: pq
    type(item) :: ret
    integer :: n, i, j
    n = pq%num
    ret = pq%heap(1)
    pq%heap(1) = pq%heap(n)
    pq%num = pq%num-1
    i = 1
    do while (2*i < n)
      j = 2*i
      if (j+1 < n .and. lessi(pq%heap(j+1),pq%heap(j))) j = j+1
      if (lessi(pq%heap(j),pq%heap(i))) call swapi(pq%heap(j),pq%heap(i))
      i = j
    end do
  end

  function newe(to,wt,fm,cp,rv) result(ret)
    integer, intent(in) :: to
    integer(8), intent(in) :: wt
    integer, intent(in), optional :: fm, cp, rv
    type(edge) :: ret
    if (present(fm)) ret%fm = fm
    ret%to = to
    ret%wt = wt
    if (present(cp) .and. present(rv)) then
      ret%cp = cp
      ret%rv = rv
    end if
  end

  subroutine appende(a)
    type(edge), pointer, intent(inout) :: a(:)
    integer :: n
    type(edge), allocatable :: tmp(:)
    n = size(a)
    allocate(tmp(n))
    tmp = a
    deallocate(a)
    allocate(a(2*n))
    a(1:n) = tmp
    deallocate(tmp)
  end

  function lesse(a,b) result(ret)
    type(edge), intent(in) :: a, b
    logical :: ret
    ret = a%wt < b%wt
  end

  subroutine finalizee(list)
    type(arraylist), intent(inout) :: list
    if (associated(list%arr)) deallocate(list%arr)
    list%num = 0
  end

  subroutine adde(list,e)
    type(arraylist), intent(inout) :: list
    type(edge), intent(in) :: e
    if (.not.associated(list%arr)) allocate(list%arr(1))
    if (list%num == size(list%arr)) call appende(list%arr)
    list%num = list%num+1
    list%arr(list%num) = e
  end

  function pope(list) result(ret)
    type(arraylist), intent(inout) :: list
    type(edge) :: ret
    ret = list%arr(list%num)
    list%num = list%num-1
  end

  function newg(n) result(ret)
    integer, intent(in) :: n
    type(graph) :: ret
    allocate(ret%egs(n))
  end

  subroutine add(g,fm,to,wt,cp)
    class(graph), intent(inout) :: g
    integer, intent(in) :: fm, to
    integer, intent(in), optional :: cp
    integer(8) :: wt
    if (present(cp)) then
      call adde(g%egs(fm),newe(to,wt,cp=cp,rv=g%egs(to)%num))
      call adde(g%egs(to),newe(fm,wt,cp=0,rv=g%egs(fm)%num-1))
    else
      call adde(g%egs(fm),newe(to,wt))
    end if
  end

  function dijkstra(g,s) result(ret)
    class(graph), intent(in) :: g
    integer, intent(in) :: s
    integer(8) :: ret(size(g%egs))
    type(priorityqueue) :: pq
    type(item) :: it
    type(edge) :: e
    integer :: i
    ret = inf
    ret(s) = 0_8
    call offer(pq,newi(s,ret(s)))
    do while (pq%num > 0)
      it = poll(pq)
      if (it%wt > ret(it%id)) cycle
      do i = 1, g%egs(it%id)%num
        e = g%egs(it%id)%arr(i)
        if (ret(e%to) > ret(it%id)+e%wt) then
          ret(e%to) = ret(it%id)+e%wt
          call offer(pq,newi(e%to,ret(e%to)))
        end if
      end do
    end do
  end

  recursive subroutine dfs_bf(g,u)
    type(graph), intent(inout) :: g
    integer, intent(in) :: u
    integer :: i, v
    g%used(u) = .true.
    do i = 1, g%egs(u)%num
      v = g%egs(u)%arr(i)%to
      if (g%used(v)) cycle
      call dfs_bf(g,v)
    end do
  end

  function bellman_ford(es,s,t) result(ret)
    class(arraylist), intent(in) :: es
    integer, intent(in) :: s, t
    integer(8) :: ret
    integer :: n, i, fm, to, step
    integer(8) :: wt
    logical :: updated
    integer(8), allocatable :: tmp(:)
    type(graph) :: gs, gt
    type(arraylist) :: reach
    n = 0
    do i = 1, es%num
      n = max(n,es%arr(i)%fm,es%arr(i)%to)
    end do
    gs = newg(n)
    gt = newg(n)
    allocate(gs%used(n),gt%used(n))
    gs%used = .false.
    gt%used = .false.
    do i = 1, es%num
      fm = es%arr(i)%fm
      to = es%arr(i)%to
      wt = es%arr(i)%wt
      call add(gs,fm,to,wt)
      call add(gt,to,fm,wt)
    end do
    call dfs_bf(gs,s)
    call dfs_bf(gt,t)
    n = 0
    do i = 1, es%num
      fm = es%arr(i)%fm
      to = es%arr(i)%to
      if (gs%used(fm) .and. gt%used(fm) .and. gs%used(to) .and. gt%used(to)) then
        call adde(reach,es%arr(i))
        n = max(n,fm,to)
      end if
    end do
    deallocate(gs%used,gt%used)
    allocate(tmp(n))
    tmp = inf
    tmp(s) = 0_8
    step = 0
    updated = .true.
    do while (updated)
      updated = .false.
      do i = 1, reach%num
        fm = reach%arr(i)%fm
        to = reach%arr(i)%to
        wt = reach%arr(i)%wt
        if (tmp(to) > tmp(fm)+wt) then
          tmp(to) = tmp(fm)+wt
          updated = .true.
        end if
      end do
      step = step+1
      if (step > n) then
        ret = -inf
        return
      end if
    end do
    ret = tmp(t)
    deallocate(tmp)
  end

  recursive function dfs_ff(g,u,t,f) result(ret)
    class(graph), intent(inout) :: g
    integer, intent(in) :: u, t, f
    integer :: ret
    integer :: i, v, cp, rv
    ret = f
    if (u == t) return
    g%used(u) = .true.
    do i = 1, g%egs(u)%num
      v = g%egs(u)%arr(i)%to
      cp = g%egs(u)%arr(i)%cp
      if (g%used(v) .or. cp <= 0) cycle
      ret = dfs_ff(g,v,t,min(f,cp))
      rv = g%egs(u)%arr(i)%rv
      if (ret > 0) then
        g%egs(u)%arr(i)%cp = g%egs(u)%arr(i)%cp-ret
        g%egs(v)%arr(rv)%cp = g%egs(v)%arr(rv)%cp+ret
        return
      end if
    end do
    ret = 0
  end

  function ford_fulkerson(g,s,t) result(ret)
    class(graph), intent(inout) :: g
    integer, intent(in) :: s, t
    integer :: ret
    integer :: f
    ret = 0
    if (.not.associated(g%used)) allocate(g%used(size(g%egs)))
    do
      g%used = .false.
      f = dfs_ff(g,s,t,1000000000)
      if (f == 0) then
        deallocate(g%used)
        return
      end if
      ret = ret+f
    end do
  end

end module mod_graph

program saving_snuuk
  use mod_graph
  implicit none
  integer(8), parameter :: dyen = 1000000000000000_8
  integer :: n, m, s, t, u, v, i
  integer(8) :: a, b, cy(100000) = 0_8, cs(100000) = 0_8, ans(100000) = 0_8
  type(graph) :: gy, gs
  read(*,*) n, m, s, t
  gy = graph(n)
  gs = graph(n)
  do i = 1, m
    read(*,*) u, v, a, b
    call gy%add(u,v,a)
    call gy%add(v,u,a)
    call gs%add(u,v,b)
    call gs%add(v,u,b)
  end do
  cy(1:n) = gy%dijkstra(s)
  cs(1:n) = gs%dijkstra(t)
  ans(n) = cy(n)+cs(n)
  do i = n-1, 1, -1
    ans(i) = min(ans(i+1),cy(i)+cs(i))
  end do
  do i = 1, n
    write(*,'(i0)') dyen-ans(i)
  end do
end program saving_snuuk

Score : 400 points

Problem Statement

Kenkoooo is planning a trip in Republic of Snuke.
In this country, there are n cities and m trains running.
The cities are numbered 1 through n, and the i-th train connects City u_i and v_i bidirectionally.
Any city can be reached from any city by changing trains.

Two currencies are used in the country: yen and snuuk.
Any train fare can be paid by both yen and snuuk.
The fare of the i-th train is a_i yen if paid in yen, and b_i snuuk if paid in snuuk.

In a city with a money exchange office, you can change 1 yen into 1 snuuk.
However, when you do a money exchange, you have to change all your yen into snuuk.
That is, if Kenkoooo does a money exchange when he has X yen, he will then have X snuuk.
Currently, there is a money exchange office in every city, but the office in City i will shut down in i years and can never be used in and after that year.

Kenkoooo is planning to depart City s with 10^{15} yen in his pocket and head for City t, and change his yen into snuuk in some city while traveling.
It is acceptable to do the exchange in City s or City t.

Kenkoooo would like to have as much snuuk as possible when he reaches City t by making the optimal choices for the route to travel and the city to do the exchange.
For each i=0,...,n-1, find the maximum amount of snuuk that Kenkoooo has when he reaches City t if he goes on a trip from City s to City t after i years.
You can assume that the trip finishes within the year.

Constraints

2 \leq n \leq 10^5

1 \leq m \leq 10^5

1 \leq s,t \leq n

s \neq t

1 \leq u_i < v_i \leq n

1 \leq a_i,b_i \leq 10^9

If i\neq j, then u_i \neq u_j or v_i \neq v_j.

Any city can be reached from any city by changing trains.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

n m s t
u_1 v_1 a_1 b_1
:
u_m v_m a_m b_m

Output

Print n lines.
In the i-th line, print the maximum amount of snuuk that Kenkoooo has when he reaches City t if he goes on a trip from City s to City t after i-1 years.

Sample Input 1

4 3 2 3
1 4 1 100
1 2 1 10
1 3 20 1

Sample Output 1

999999999999998
999999999999989
999999999999979
999999999999897

After 0 years, it is optimal to do the exchange in City 1.

After 1 years, it is optimal to do the exchange in City 2.

Note that City 1 can still be visited even after the exchange office is closed.
Also note that, if it was allowed to keep 1 yen when do the exchange in City 2 and change the remaining yen into snuuk, we could reach City 3 with 999999999999998 snuuk, but this is NOT allowed.

After 2 years, it is optimal to do the exchange in City 3.

After 3 years, it is optimal to do the exchange in City 4.
Note that the same train can be used multiple times.

Sample Input 2

8 12 3 8
2 8 685087149 857180777
6 7 298270585 209942236
2 4 346080035 234079976
2 5 131857300 22507157
4 8 30723332 173476334
2 6 480845267 448565596
1 4 181424400 548830121
4 5 57429995 195056405
7 8 160277628 479932440
1 6 475692952 203530153
3 5 336869679 160714712
2 7 389775999 199123879

Sample Output 2

999999574976994
999999574976994
999999574976994
999999574976994
999999574976994
999999574976994
999999574976994
999999574976994


program main
  implicit none
  integer :: n
  read(*, *) n
  if (mod(n, 2) /= 0) then
    n = n * 2
  end if
  write(*, "(i0)") n
end program main

Score : 100 points

Problem Statement

You are given a positive integer N.
Find the minimum positive integer divisible by both 2 and N.

Constraints

1 \leq N \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N

Output

Print the minimum positive integer divisible by both 2 and N.

Sample Input 1

3

Sample Output 1

6

6 is divisible by both 2 and 3.
Also, there is no positive integer less than 6 that is divisible by both 2 and 3.
Thus, the answer is 6.

Sample Input 2

10

Sample Output 2

10

Sample Input 3

999999999

Sample Output 3

1999999998


program main
  implicit none

  integer :: n,i,j,a(100),t

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

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

  write(*,*)abs(a(1) - a(n))
end program main

Score : 200 points

Problem Statement

You are given an integer sequence A of length N.
Find the maximum absolute difference of two elements (with different indices) in A.

Constraints

2 \leq N \leq 100

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum absolute difference of two elements (with different indices) in A.

Sample Input 1

4
1 4 6 3

Sample Output 1

5

The maximum absolute difference of two elements is A_3-A_1=6-1=5.

Sample Input 2

2
1000000000 1

Sample Output 2

999999999

Sample Input 3

5
1 1 1 1 1

Sample Output 3

0


program c

integer(8) :: n,a(200000),nn,ans=0

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

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

call heapsort(n,a(1:n))
nn = a((n+1)/2)

do i=1,n
	a(i) = abs(a(i) - nn)
enddo

write(*,*) sum(a(1:n))

contains

subroutine heapsort(n,y1)

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

	l = n/2+1
	k = n

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

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

		 i = l
		 j = l+l

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

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

		 enddo

		 y1(i) = t1

	enddo
	return

end subroutine heapsort

end program

Score : 300 points

Problem Statement

Snuke has an integer sequence A of length N.

He will freely choose an integer b.
Here, he will get sad if A_i and b+i are far from each other.
More specifically, the sadness of Snuke is calculated as follows:

abs(A_1 - (b+1)) + abs(A_2 - (b+2)) + ... + abs(A_N - (b+N))

Here, abs(x) is a function that returns the absolute value of x.

Find the minimum possible sadness of Snuke.

Constraints

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

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the minimum possible sadness of Snuke.

Sample Input 1

5
2 2 3 5 5

Sample Output 1

2

If we choose b=0, the sadness of Snuke would be abs(2-(0+1))+abs(2-(0+2))+abs(3-(0+3))+abs(5-(0+4))+abs(5-(0+5))=2.
Any choice of b does not make the sadness of Snuke less than 2, so the answer is 2.

Sample Input 2

9
1 2 3 4 5 6 7 8 9

Sample Output 2

0

Sample Input 3

6
6 5 4 3 2 1

Sample Output 3

18

Sample Input 4

7
1 1 1 1 2 3 4

Sample Output 4

6


program main
     implicit none
     integer n,i,sumv1,sumv2,b1,b2
     integer,allocatable,dimension(:)::a
     read(*,*)n
     allocate(a(n))
     read(*,*)a
     do i=1,n
          a(i)=a(i)-i
     enddo
     sumv1=0;sumv2=0
     b1=sum(a)/n
     b2=b1-1
     do i=1,n
          sumv1=sumv1+abs(b1-a(i))
          sumv2=sumv2+abs(b2-a(i))
     enddo
     write(*,*)min(sumv1,sumv2)

end program main


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

    interface double_merge_sort
        module procedure msd3232, msd6464
    end interface

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

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

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

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

        allocate(tmp(lst-fst+1))

        li=fst
        ri=mdl+1
        ti=1

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

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

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

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

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

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

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

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

        allocate(tmp(lst-fst+1))

        li=fst
        ri=mdl+1
        ti=1

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

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

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

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

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

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

        mdl = (fst+lst)/2

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

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

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

        li=fst
        ri=mdl+1
        ti=1

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

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

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

        deallocate(t1,t2)
    end subroutine

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

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

        mdl = (fst+lst)/2

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

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

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

        li=fst
        ri=mdl+1
        ti=1

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

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

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

        deallocate(t1,t2)
    end subroutine
end module

program arc100_a
    use,intrinsic :: iso_fortran_env
    use merge_sort_mod
    implicit none
    integer(int64):: n,i,ans
    integer(int64), allocatable:: a(:)

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

    call merge_sort(a,1_8,n)
    ans = sum(abs(a(:)-a(n/2+1)))
    print'(i0)', ans
end program arc100_a


program test
implicit none

character(4) :: eat
integer :: i,j,k,l,m,n

read(*,*) eat
if(eat=='++++') then
	write(*,*) 4
else if(eat=='-+++' .or. eat=='+-++' .or. eat=='++-+' .or. eat=='+++-') then
	write(*,*) 2
else if (eat=='----') then
	write(*,*) -4
else if (eat=='---+' .or. eat=='--+-' .or. eat=='-+--' .or. eat=='+---') then
	write(*,*) -2
else
	write(*,*) 0
endif

end program

Score : 100 points

Problem Statement

There is always an integer in Takahashi's mind.

Initially, the integer in Takahashi's mind is 0. Takahashi is now going to eat four symbols, each of which is + or -. When he eats +, the integer in his mind increases by 1; when he eats -, the integer in his mind decreases by 1.

The symbols Takahashi is going to eat are given to you as a string S. The i-th character in S is the i-th symbol for him to eat.

Find the integer in Takahashi's mind after he eats all the symbols.

Constraints

The length of S is 4.

Each character in S is + or -.

Input

Input is given from Standard Input in the following format:

S

Output

Print the integer in Takahashi's mind after he eats all the symbols.

Sample Input 1

+-++

Sample Output 1

2

Initially, the integer in Takahashi's mind is 0.

The first integer for him to eat is +. After eating it, the integer in his mind becomes 1.

The second integer to eat is -. After eating it, the integer in his mind becomes 0.

The third integer to eat is +. After eating it, the integer in his mind becomes 1.

The fourth integer to eat is +. After eating it, the integer in his mind becomes 2.

Thus, the integer in Takahashi's mind after he eats all the symbols is 2.

Sample Input 2

-+--

Sample Output 2

-2

Sample Input 3

----

Sample Output 3

-4


program main
	implicit none
	integer i, S
	integer(8) N, Nh
	read(*, *) N
	Nh = N
	S = 0
	do i = 1, 10
		S = S + mod(N, 10)
		N = N / 10
		if(N < 10) then
			S = S + N
			exit
		end if
	end do
	if(mod(Nh, S) == 0) then
		write(*, *) 'Yes'
	else
		write(*, *) 'No'
	end if
end program main

Score : 200 points

Problem Statement

Let S(n) denote the sum of the digits in the decimal notation of n.
For example, S(101) = 1 + 0 + 1 = 2.

Given an integer N, determine if S(N) divides N.

Constraints

1 \leq N \leq 10^9

Input

Input is given from Standard Input in the following format:

N

Output

If S(N) divides N, print Yes; if it does not, print No.

Sample Input 1

12

Sample Output 1

Yes

In this input, N=12.
As S(12) = 1 + 2 = 3, S(N) divides N.

Sample Input 2

101

Sample Output 2

No

As S(101) = 1 + 0 + 1 = 2, S(N) does not divide N.

Sample Input 3

999999999

Sample Output 3

Yes


program minimization
    implicit none
    integer :: k=0,n=0,cnt=0,i=0
    integer, allocatable :: a(:)

    read(*,*)n,k
    allocate(a(n))
    read(*,*) (a(i),i=1,n)
    if(n<2 .or. 100000<n .or. k<2 .or. n<k)then
    else
        do
            n = n - 2
            cnt = cnt + 1
            if(n<0)then
                goto 1
            else
            endif
        enddo
    endif

    1 continue
        write(*,*) cnt-1
    deallocate(a)

end program minimization

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 hbd
    implicit none
    integer ::a=0,b=0
    read(*,*)a,b
    if (1<=a .and. a<=8 .and. 1<=b .and. b<=8) then
        write(*,*) 'Yay!'
    else
        write(*,*) ':('
    endif
end program hbd

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 apple
implicit none
integer::d, n
 read(5,*)d, n
  if(d==0) then
   print*,n
  else if(d==1)then
   print*,n*100
  else
   print*,n*10000
  end if
end program apple


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

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

do i = 1, n
   k = 0
   do while (mod(a(i),2) == 0)
      a(i) = a(i) / 2
      k = k + 1
   enddo
   a(i) = k
enddo

print '(i0)',sum(a)
stop
end

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


integer N
integer,allocatable,dimension(:)::a
integer ans
read*,N
allocate(a(N))
read*,a

ans=0
do i=1,N
  do while(and(a(i),1)==0)
    ans=ans+1
    a(i)=rshift(a(i),1)
  end do
end do
print"(i0)",ans
end


program twofold
  implicit none
  integer(8) :: n, i, ans
  integer(8), allocatable :: a(:)
  read *, n
  allocate(a(n))
  read *, a
  ans = 0
  do i = 1, n
    do while (mod(a(i),2) == 0)
      a(i) = a(i) / 2
      ans = ans + 1
    end do
  end do
  print '(i0)', ans
  stop
end program twofold


 integer,dimension(10000)::a
  read*,in
  do i=1,in
     read*,ia
     a(i)=ia
  end do

  ix=0
  do i=1,in
     if (mod(a(i),2)==0) then
        do j=1,1000000000
           a(i)=a(i)/2
           ix=ix+1
           if (mod(a(i),2)/=0) exit
        end do
     end if
  end do

2 print*,ix
end program


  integer,dimension(10000)::a
  read*,in
  do i=1,in
     read*,ia
     a(i)=ia
  end do

  ix=0
  do k=1,100000
     iper2=0
     do i=1,in
        if (mod(a(i),2)==0) then
           a(i)=a(i)/2
           iper2=1
           ix=ix+1
           do j=i+1,in
              a(j)=3*a(j)
           end do
           exit
        else
           a(i)=3*a(i)
        end if
     end do
      if (iper2==0) go to 2
1 end do

2 print*,ix
end program


program main
  implicit none
  integer :: n, i, res
  integer, allocatable :: a(:)
  read(*, *) n
  allocate(a(n))
  read(*, *) a(:)
  res = 0
  do i = 1, n
    res = res + count_two(a(i))
  end do
  write(*, "(i0)") res
  deallocate(a)
  contains
  function count_two(d)
    implicit none
    integer, intent(in) :: d
    integer :: count_two, i, d2
    count_two = 0
    d2 = d
    do i = 1, 100
      if (mod(d2, 2) == 0) then
        count_two = count_two + 1
        d2 = d2 / 2
      else
        exit
      end if
    end do
  end function count_two
end program main


program main
	integer N, counter, i, j
	integer(8), allocatable :: a(:)
	read(*, *) N
	allocate(a(1: N))
	read(*, *) a(1: N)
	counter = 0
	do i = 1, N
		do
			if(mod(a(i), 2) == 0) then
				counter = counter + 1
				a(i) = a(i) / 2
			else
				exit
			end if
		end do
	end do
	write(*, *) counter
end program main


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 main
	implicit none
	integer N, M, i, j
	integer(8), allocatable :: x(:), y(:), z(:), Sx(:, :), Sy(:, :), Sz(:, :), dp(:, :)
	read(*, *) N, M
	allocate(x(1: N))
	allocate(y(1: N))
	allocate(z(1: N))
	allocate(Sx(0:N, 0:M))
	allocate(Sy(0:N, 0:M))
	allocate(Sz(0:N, 0:M))
	allocate(dp(0:N, 0:M))
	do i = 1, N
		read(*, *) x(i), y(i), z(i)
	end do
	Sx(0, 0:M) = 0
	Sx(0:N, 0) = 0
	Sy(0, 0:M) = 0
	Sy(0:N, 0) = 0
	Sz(0, 0:M) = 0
	Sz(0:N, 0) = 0
	dp(0, 0:M) = 0
	dp(0:N, 0) = 0
	do i = 1, N
		do j = 1, M
			Sx(i, j) = Sx(i-1, j-1) + x(i)
			Sy(i, j) = Sy(i-1, j-1) + y(i)
			Sz(i, j) = Sz(i-1, j-1) + z(i)
			if(dp(i-1,j) < abs(Sx(i, j))+ abs(Sy(i, j))+ abs(Sz(i, j))) then
				dp(i, j) = abs(Sx(i, j))+ abs(Sy(i, j)) + abs(Sz(i, j))
			else
				dp(i, j) = dp(i-1, j)
				Sx(i, j) = Sx(i-1, j)
				Sy(i, j) = Sy(i-1, j)
				Sz(i, j) = Sz(i-1, j)
			end if
		end do
	end do
	write(*, *) dp(N, M)
end program main


program main
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: n

    read*, n
    if (n < 1000) then
        print'(a)', 'ABC'
    else
        print'(a)', 'ABD'
    end if
end program main

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 interval_game
  implicit none
  integer :: n, l(100001), r(100001), i
  integer :: 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


implicit none
integer:: a,b
read(*,*) a,b
write(*,*) max(a+b,a-b,a*b)
end

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

    read(*,*) n

    read(*,*)s
    x(:)=0
    y(:)=0

    do i=1,n
        call alfnumber(s(i:i),a)
        y(a)=y(a)+1
    end do
    m=0

    do i=1,n
        call alfnumber(s(i:i),a)
        y(a)=y(a)-1
        x(a)=x(a)+1
        b=0
        do j=1,26
            if(x(j)*y(j)>0)then
                b=b+1
            end if
        end do
        if (b>m)then
            m=b
        end if
    end do
    write(*,*) m

    stop
    contains
    subroutine alfnumber(st,a)
        implicit none
        character,intent(in) :: st
        integer(8),intent(out) ::  a
        integer::i
        character(1)::ch(26)
        ch=(/'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'/)
        do i=1,26
            if(st .eq. ch(i))then
                a=i
                return
            end if
        end do
        end subroutine alfnumber
end program sample

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


program ARC098C
    implicit none
    integer(8)::N,i
    integer(8)::e_count=0
    integer(8)::w_count=0
    integer(8)::result
    integer(8),allocatable,dimension(:)::E,W
    character(300000)::S

    read(5,*)N
    read(5,*)S
    allocate(E(N))
    allocate(W(N))
    result=N-1

    do i=1,N
        if(S(i:i)=='E')then
            e_count=e_count+1
        else
            w_count=w_count+1
        end if
        E(i)=e_count
        W(i)=w_count
    end do

    do i=1,N
        if(i==1)then
            if(E(N)-E(i)<result)then
                result=E(N)-E(i)
            end if
        else if(i<N)then
            if(W(i-1)+E(N)-E(i)<result)then
                result=W(i-1)+E(N)-E(i)
            end if
        else if(W(i-1)<result)then
            result=W(i-1)
        end if
    end do

    print'(i0)',result
end program ARC098C

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 sample
    implicit none
    real(8)::x,y,z
    integer(8) :: a,b,c,i,j,m,n,k,a1,b1,c1

    read(*,*) a,b,c,k
    if (mod(k,2)==0)then
        write(*,*) a-b
    else
        write(*,*) b-a
    end if
    stop
end program sample

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 AGC024A
    implicit none
    integer(8)::A,B,C,K
    read*,A,B,C,K
    if(mod(K,2)==0)print'(i0)',A-B
    if(mod(K,2)==1)print'(i0)',-A+B
end program AGC024A


program test
implicit none
integer(8) a,b,c,k

read(*,*) a,b,c,k

if(mod(k,2) == 0) then
		write(*,*) a-b
else
		write(*,*) b-a
	endif

end program


program test
implicit none
integer :: n,i
integer :: 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


program test
implicit none
integer :: n,i
integer :: 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+1) - retsu(i) > 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


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

    read(*,*)x
    ans=0

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

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 exp
implicit none
integer i,j,base,index
integer in,temp
read *, in ! 1<=in<=1000
base = 1
index =1
do i=1,int(sqrt(real(in)))  ! Base of exponential
  do j=1, 9 ! Index of exponential, 2^10>1000 therefore j<=9
    if(i**j<=in) then
      if(i**j>base**index) then
        base=i
        index = j
      end if
    end if
  end do
end do
print *, base**index
end program exp


INTEGER N,C
READ*,N
C=1
DO I=1,N
  DO J=2,N
    IF(I**J>N)EXIT
    C=MAX(C,I**J)
  END DO
END DO
PRINT"(I0)",C
END


program main
    implicit none
    integer(8) X, i, j, ans
	read(*, *) X
	ans = 0
	do i = 1, 40
	    do j = 2, 10
			if(i**j < 1 .or. i**j > 1000) then
				exit
		    else if(i**j <= X) then
			    ans = max(i**j, ans)
			end if
		end do
	end do
	write(*, *) ans
end program main


program main
    implicit none
    integer(8) X, i, j, ans
	read(*, *) X
	ans = 0
	do i = 1, 40
	    do j = 2, 10
			if(i**j < 0) then
				exit
		    else if(i**j <= X) then
			    ans = i**j
			end if
		end do
	end do
	write(*, *) ans
end program main


program prob6
    implicit none
    character(5005) :: s
    character(5) :: t
    integer :: K
    character(5), dimension(:), allocatable :: a
    integer :: i, j, L, cnt
    read(*,*) s
    read(*,*) K

    L = len_trim(s)
    allocate(a(L*K))
    do i = 1, L
        do j = 1, K
            a(L*(j-1)+i) = adjustl(s(i:i+j-1))
        end do
    end do

    call msort(a)
    cnt = 1
    i = 2
    t = a(1)
    do while(cnt < K)
        if(t /= a(i)) then
            t = a(i)
            cnt = cnt + 1
        end if
        i = i + 1
    end do

    write(*,'(a)') trim(adjustl(t))
    stop
contains

  !
  ! swap two arguments
  !
subroutine swap(a, b)
    implicit none
    character(5), intent(inout) :: a, b

    character(5) :: c

    c = a
    a = b
    b = c

  end subroutine swap

  !
  ! merge sort
  !
  recursive subroutine msort(x)
    implicit none
    character(5), intent(inout) :: x(:)

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

    n = size(x)

    if(n == 1) then
      return
    end if

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

    !
    ! tmpという配列にxを一時的に保管
    ! マージソートは外部ソート
    !
    allocate(tmp(n))
    tmp = x

    ! tmpの後半部分を左右反転
    do k = n, mid+1, -1
      tmp(k) = x(mid + n - k + 1)
    end do

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

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

    deallocate(tmp)
    return
  end subroutine msort
end program prob6

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 main
  implicit none
  integer :: a, b, c, k
  read(*, *) a, b, c
  read(*, *) k
  write(*, "(i0)") max(a, b, c) * (2 ** k - 1) + (a + b + c)
end program main

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.