program sample
    implicit none
    character(100)::s
    integer(8) :: i,j,l,m,n,a,b,c,k,a1,a2,v,cou(4)
    integer(8),allocatable :: x(:,:),y1(:),y2(:),ans(:)

    read(*,*) a,b,k
    allocate(x(a,b),y1(b),y2(b),ans(b))
    x(:, :)=0
    do i=1,k
        read(*,*)a1,a2,v
        x(a1,a2)=v
    end do
    cou(:)=0
    c=1
    y1(1)=x(1,1)
    if(x(1,1)>0)then
        cou(1)=x(1,1)
        c=c+1
    endif
    do j=2,b
        y1(j)=y1(j-1)+x(1,j)
        if(x(1,j)>0)then
            if(c>3)then
                cou(4)=x(1,j)
                y1(j)=y1(j)-minval(cou)
                do l=1,4
                    if(cou(l)==minval(cou))then
                        cou(l)=cou(4)
                    end if
                end do
            else
                cou(c)=x(1,j)
                c=c+1
            end if
        end if
    end do
    ans(:)=y1(:)

    do i=2,a
        do j=1,b
            y2(j)=ans(j)+x(i,j)
        end do

        cou(:)=0
        c=0
        ans(1)=y2(1)
        do j=2,b

            y1(j)=ans(j-1)+x(i,j)

            if(x(i,j)>0)then
                if(c>3)then
                    cou(4)=x(i,j)
                    y1(j)=y1(j)-minval(cou)
                    do l=1,4
                        if(cou(l)==minval(cou))then
                            cou(l)=cou(4)
                        end if
                    end do
                else
                    cou(c)=x(i,j)
                    c=c+1
                end if
            end if
            if(y2(j)>y1(j))then
                ans(j)=y2(j)
                c=1
                cou(:)=0
            else
                ans(j)=y1(j)
            end if
        end do
    end do
    write(*,*)ans(b)

    stop
end program sample

Score : 500 points

Problem Statement

There are K items placed on a grid of squares with R rows and C columns. Let (i, j) denote the square at the i-th row (1 \leq i \leq R) and the j-th column (1 \leq j \leq C). The i-th item is at (r_i, c_i) and has the value v_i.

Takahashi will begin at (1, 1), the start, and get to (R, C), the goal. When he is at (i, j), he can move to (i + 1, j) or (i, j + 1) (but cannot move to a non-existent square).

He can pick up items on the squares he visits, including the start and the goal, but at most three for each row. It is allowed to ignore the item on a square he visits.

Find the maximum possible sum of the values of items he picks up.

Constraints

1 \leq R, C \leq 3000

1 \leq K \leq \min(2 \times 10^5, R \times C)

1 \leq r_i \leq R

1 \leq c_i \leq C

(r_i, c_i) \neq (r_j, c_j) (i \neq j)

1 \leq v_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

R C K
r_1 c_1 v_1
r_2 c_2 v_2
:
r_K c_K v_K

Output

Print the maximum possible sum of the values of items Takahashi picks up.

Sample Input 1

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

Sample Output 1

8

He has two ways to get to the goal:

Visit (1, 1), (1, 2), and (2, 2), in this order. In this case, the total value of the items he can pick up is 3 + 5 = 8.

Visit (1, 1), (2, 1), and (2, 2), in this order. In this case, the total value of the items he can pick up is 3 + 4 = 7.

Thus, the maximum possible sum of the values of items he picks up is 8.

Sample Input 2

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

Sample Output 2

29

We have four items in the 1-st row. The optimal choices are as follows:

Visit (1, 1) (1, 2), (1, 3), (1, 4), (2, 4), and (2, 5), in this order, and pick up all items except the one on (1, 2). Then, the total value of the items he picks up will be 3 + 4 + 2 + 20 = 29.

Sample Input 3

4 5 10
2 5 12
1 5 12
2 3 15
1 2 20
1 1 28
2 4 26
3 2 27
4 5 21
3 5 10
1 3 10

Sample Output 3

142


program making_palindrome
  implicit none
  integer(8), parameter :: inf = 1e18
  integer :: n, i, l
  character(20) :: s(50, 0:1) = ''
  integer(8) :: c(50) = 0, m(50, 21, 0:1) = 0, x = inf
  logical :: u(50, 21, 0:1) = .false.
  read(*,*) n
  do i = 1, n
    read(*,*) s(i, 0), c(i)
    l = len_trim(s(i, 0))
    s(i, 1)(1:l) = reverse(s(i, 0)(1:l))
  end do
  do i = 1, n
    x = min(x, solve(i, 1, 0) + c(i))
  end do
  write(*,'(i0)') merge(x, -1_8, x < inf)
contains
  function reverse(s) result(res)
    character(*), intent(in) :: s
    character(len_trim(s)) :: res
    integer :: n, i
    n = len_trim(s)
    do i = 1, n
      res(i:i) = s(n - i + 1:n - i + 1)
    end do
  end
  recursive integer(8) function solve(i, j, k) result(res)
    integer, intent(in) :: i, j, k
    integer :: l, r, t, l1, l2
    logical :: f
    res = m(i, j, k)
    if (u(i, j, k)) return
    u(i, j, k) = .true.
    m(i, j, k) = inf
    l = j
    r = len_trim(s(i, k))
    f = .true.
    do while (l < r)
      if (s(i, k)(l:l) /= s(i, k)(r:r)) then
        f = .false.
        exit
      end if
      l = l + 1
      r = r - 1
    end do
    if (f) then
      m(i, j, k) = 0
      res = 0
      return
    end if
    l1 = len_trim(s(i, k))
    do t = 1, n
      l = j
      r = 1
      l2 = len_trim(s(t, 1 - k))
      do while (l <= l1 .and. r <= l2)
        if (s(i, k)(l:l) /= s(t, 1 - k)(r:r)) exit
        l = l + 1
        r = r + 1
      end do
      if (l == l1 + 1) then
        m(i, j, k) = min(m(i, j, k), solve(t, r, 1 - k) + c(t))
      end if
      if (r == l2 + 1) then
        m(i, j, k) = min(m(i, j, k), solve(i, l, k) + c(t))
      end if
    end do
    res = m(i, j, k)
  end
end program making_palindrome

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

Score : 600 points

Problem Statement

We have N strings of lowercase English letters: S_1, S_2, \cdots, S_N.

Takahashi wants to make a string that is a palindrome by choosing one or more of these strings - the same string can be chosen more than once - and concatenating them in some order of his choice.

The cost of using the string S_i once is C_i, and the cost of using it multiple times is C_i multiplied by that number of times.

Find the minimum total cost needed to choose strings so that Takahashi can make a palindrome.

If there is no choice of strings in which he can make a palindrome, print -1.

Constraints

1 \leq N \leq 50

1 \leq |S_i| \leq 20

S_i consists of lowercase English letters.

1 \leq C_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
S_1 C_1
S_2 C_2
:
S_N C_N

Output

Print the minimum total cost needed to choose strings so that Takahashi can make a palindrome, or -1 if there is no such choice.

Sample Input 1

3
ba 3
abc 4
cbaa 5

Sample Output 1

7

We have ba, abc, and cbaa.

For example, we can use ba once and abc once for a cost of 7, then concatenate them in the order abc, ba to make a palindrome.
Also, we can use abc once and cbaa once for a cost of 9, then concatenate them in the order cbaa, abc to make a palindrome.

We cannot make a palindrome for a cost less than 7, so we should print 7.

Sample Input 2

2
abcab 5
cba 3

Sample Output 2

11

We can choose abcab once and cba twice, then concatenate them in the order abcab, cba, cba to make a palindrome for a cost of 11.

Sample Input 3

4
ab 5
cba 3
a 12
ab 10

Sample Output 3

8

We can choose a once, which is already a palindrome, but it is cheaper to concatenate ab and cba.

Sample Input 4

2
abc 1
ab 2

Sample Output 4

-1

We cannot make a palindrome, so we should print -1.

Submitted source-code file size, in bytes.

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

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


program main
implicit none

integer*8 n,d,i,j
integer, allocatable :: x,y

read*,n,d
! allocate(x(n),y(n))
j=0
do i=1,n
  read*, x,y
  ! write(*,*) x,y
  if(dsqrt(dble(x**2+y**2)).lt.d)then
    j = j+1
  endif
enddo
write(*,*) j
end program

Score : 100 points

Problem Statement

You will turn on the air conditioner if, and only if, the temperature of the room is 30 degrees Celsius or above.

The current temperature of the room is X degrees Celsius. Will you turn on the air conditioner?

Constraints

-40 \leq X \leq 40

X is an integer.

Input

Input is given from Standard Input in the following format:

X

Output

Print Yes if you will turn on the air conditioner; print No otherwise.

Sample Input 1

25

Sample Output 1

No

Sample Input 2

30

Sample Output 2

Yes


program distance
    integer :: n, d, i
    integer :: ans = 0
    integer(8), allocatable :: x(:)
    integer(8), allocatable :: y(:)

    read *, n, d
    allocate(x(n))
    allocate(y(n))
    do i = 1, n
        read *, x(i), y(i)
        if(d >= sqrt(real((x(i)**2)+(y(i)**2))))then
            ans = ans+1
        end if
    end do
    print *, ans
end program

Score : 200 points

Problem Statement

We have N points in the two-dimensional plane. The coordinates of the i-th point are (X_i,Y_i).

Among them, we are looking for the points such that the distance from the origin is at most D. How many such points are there?

We remind you that the distance between the origin and the point (p, q) can be represented as \sqrt{p^2+q^2}.

Constraints

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

0 \leq D \leq 2\times 10^5

|X_i|,|Y_i| \leq 2\times 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N D
X_1 Y_1
\vdots
X_N Y_N

Output

Print an integer representing the number of points such that the distance from the origin is at most D.

Sample Input 1

4 5
0 5
-2 4
3 4
4 -4

Sample Output 1

3

The distance between the origin and each of the given points is as follows:

\sqrt{0^2+5^2}=5

\sqrt{(-2)^2+4^2}=4.472\ldots

\sqrt{3^2+4^2}=5

\sqrt{4^2+(-4)^2}=5.656\ldots

Thus, we have three points such that the distance from the origin is at most 5.

Sample Input 2

12 3
1 1
1 1
1 1
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3

Sample Output 2

7

Multiple points may exist at the same coordinates.

Sample Input 3

20 100000
14309 -32939
-56855 100340
151364 25430
103789 -113141
147404 -136977
-37006 -30929
188810 -49557
13419 70401
-88280 165170
-196399 137941
-176527 -61904
46659 115261
-153551 114185
98784 -6820
94111 -86268
-30401 61477
-55056 7872
5901 -163796
138819 -185986
-69848 -96669

Sample Output 3

6


program distance
    integer :: n, d, i
    integer :: ans = 0
    integer, allocatable :: x(:)
    integer, allocatable :: y(:)

    read *, n, d
    allocate(x(n))
    allocate(y(n))
    print *, "座標を入力"
    do i = 1, n
        read *, x(i), y(i)
        if(d >= sqrt(real((x(i))**2+(y(i))**2)))then
            ans = ans+1
        end if
    end do
    print *, ans
end program


program abc174b
  use iso_fortran_env
  implicit none
  integer(int64) :: n, d, xy(2,200000)
  read *, n, d, xy(:,1:n)
  print *, count(xy(1,1:n)**2+xy(2,1:n)**2<=d**2)
end program abc174b


program abc29
 implicit none
 integer::N,D,i,ia,c=0
 integer,allocatable::a(:),b(:)
 read(*,*)N,D
 allocate(a(N),b(N))
 do i=1,N
  read(*,*)a(i),b(i)
 end do
 do ia=1,N
  if(a(ia)**2+b(ia)**2<=D**2)then
   c=c+1
  end if
 end do
 print'(i0)',c
 deallocate(a,b)
end program abc29


program main
    implicit none
    integer::n,d,xx,yy,i,cou
    real,allocatable::x(:),y(:)
    real::z

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

    do i=1,n
    read*,xx,yy
    x(i)=xx
    y(i)=yy
    !print*,x(i),y(i)
    end do
    !print*,x,y

    cou=0
    do i=1,n
        z=sqrt(x(i)**2+y(i)**2)
        if(z<=d) then
            cou=cou+1
        end if
     end do

     print*,cou

end program main


program answer
  implicit none
  integer :: i, N, d, count
  integer, allocatable :: x(:), y(:)
  read(*,*) N, D
  allocate(x(n))
  allocate(y(n))
  do i = 1, n
     read(*,*) x(i), y(i)
  end do

  count=0

  do i = 1, n
     if(sqrt(dble(x(i)**2+y(i)**2))<=D) then
        count=count+1
     end if
  end do

  write(*,*) count
  stop
 end program answer


program alter_altar
    implicit none
    integer :: n, m = 0, l = 1, r
    character(200000) :: s
    read(*,*) n
    read(*,*) s
    r = n
    do
      do while (l <= n .and. s(l:l) == 'R')
        l = l + 1
      end do
      do while (r >= 1 .and. s(r:r) == 'W')
        r = r - 1
      end do
      if (l >= r) exit
      m = m + 1
      s(l:l) = 'R'
      s(r:r) = 'W'
    end do
    write(*,'(i0)') m
  end program alter_altar

Score : 400 points

Problem Statement

An altar enshrines N stones arranged in a row from left to right. The color of the i-th stone from the left (1 \leq i \leq N) is given to you as a character c_i; R stands for red and W stands for white.

You can do the following two kinds of operations any number of times in any order:

Choose two stones (not necessarily adjacent) and swap them.

Choose one stone and change its color (from red to white and vice versa).

According to a fortune-teller, a white stone placed to the immediate left of a red stone will bring a disaster. At least how many operations are needed to reach a situation without such a white stone?

Constraints

2 \leq N \leq 200000

c_i is R or W.

Input

Input is given from Standard Input in the following format:

N
c_{1}c_{2}...c_{N}

Output

Print an integer representing the minimum number of operations needed.

Sample Input 1

4
WWRR

Sample Output 1

2

For example, the two operations below will achieve the objective.

Swap the 1-st and 3-rd stones from the left, resulting in RWWR.

Change the color of the 4-th stone from the left, resulting in RWWW.

Sample Input 2

2
RR

Sample Output 2

0

It can be the case that no operation is needed.

Sample Input 3

8
WRWWRWRR

Sample Output 3

3


program alter_altar
    character(200001) :: c = '0'
    integer n
    integer :: r(200001) = 0
    integer :: w(200001) = 0
    integer :: i = 1
    integer :: k = 1
    integer :: j = 0
    read *, n
    read *, c
    do
        if(i == 1)then
            w(i) = index(c,'W')
            r(k) = index(c,'R',back=.true.)
            if(w(i) < r(k))then
                c(w(i):w(i)) = 'R'
                c(r(k):r(k)) = 'W'
                j = j + 1
            end if
            if(w(i) > r(k) .or. w(i) == 0 .or. r(i) == 0) exit
       else
            w(i) = index(c(w(i+1):),'W')
            r(k) = index(c(:r(k-1)),'R',back=.true.)
            !if(w(i) > r(k))exit
            c(w(i):w(i)) = 'R'
            c(r(k):r(k)) = 'W'
            j = j + 1
        end if
    end do
    print *, j
end program


program alter_altar
    character(200001) :: c
    integer n, i, j
    integer :: r(100001)
    integer :: w(100001)
    read *, n
    read *, c
    do i = 1, n
        if(i == 1)then
            w(i) = index(c,'W')
            r(i) = index(c,'R',back=.true.)
            if(w(i) > r(i))exit
        else
            w(i) = index(c(w(i-1):),'W')
            r(i) = index(c(:r(i-1)),'R',back=.true.)
            if(w(i) > r(i))exit
        end if
    end do
    do j = 1, i
        c(w(j):w(j)) = 'R'
        c(r(j):r(j)) = 'W'

    end do
    print *, i-1
end program


program alter_altar
    character(200001) :: c
    integer i, x, y, n
    read *, n
    read *, c
    do i = 1, n
        x = index(c,'W')
        y = index(c,'R',back=.true.)
        c(x:x) = 'R'
        c(y:y) = 'W'
        if(x == 0 .or. y == 0 .or. x > y)exit
    end do
    print *, i-1
end program


program alter_altar
    character(200001) :: c
    integer j, i,x ,y
    integer r(200001), w(200001)
    integer :: n = 0
    integer :: k = 0
    read *, n
    read *, c
    do i = 1, n
        if(x == 0 .or. y == 0 .or. x > y)exit
        x = index(c(1:),'W')
        y = index(c(1:),'R',back=.true.)
        w(i) = x
        r(i) = y
    end do
    do j = 1, i
        c(w(j):w(j)) = 'R'
        c(r(j):r(j)) = 'W'
        k = k + 1
    end do
    print *, k
end program


program alter_altar
    character(200001) :: c
    integer i, x, y
    integer :: k = 0
    integer :: n = 0

    read *, n
    read *, c

    do i = 1, n
        x = index(c,'W')
        y = index(c,'R',back=.true.)
        if(x == 0 .or. y == 0)exit
        if(x < y)then
            c(x:x) = 'R'
            c(y:y) = 'W'
            k  = k + 1
        end if
    end do
        x = index(c,'W')
        y = index(c,'R',back=.true.)
        if(x > y)then
            print *, k
        end if
end program


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

    read(*,*) n
    allocate(x(n))
    read(*,*)s
    c=1
    d=n
    m=0
    do
    do i=c,n
        if(s(i:i) .eq. 'W')then
            a=i
            exit
        elseif(i==n)then
            a=n+1
            exit
        end if
    end do
    do i=d,1,-1
        if(s(i:i) .eq. 'R')then
            b=i
            exit
        elseif(i==1)then
            b=0
            exit
        end if
    end do
    if(a<b)then
        m=m+1
        c=a+1
        d=b-1
    else
        exit
    end if
    end do

    write(*,*) m

    stop
end program sample


program sample
    implicit none

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

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

    do i=1,k
        x(:)=0
        read(*,*)a,b
        do j=a,b
            c=y(j)
            x(c)=1
        end do
        write(*,*) sum(x)
    end do

    stop
end program sample

Score : 600 points

Problem Statement

We have N colored balls arranged in a row from left to right; the color of the i-th ball from the left is c_i.

You are given Q queries. The i-th query is as follows: how many different colors do the l_i-th through r_i-th balls from the left have?

Constraints

1\leq N,Q \leq 5 \times 10^5

1\leq c_i \leq N

1\leq l_i \leq r_i \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
c_1 c_2 \cdots c_N
l_1 r_1
l_2 r_2
:
l_Q r_Q

Output

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

Sample Input 1

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

Sample Output 1

2
3
1

The 1-st, 2-nd, and 3-rd balls from the left have the colors 1, 2, and 1 - two different colors.

The 2-st, 3-rd, and 4-th balls from the left have the colors 2, 1, and 3 - three different colors.

The 3-rd ball from the left has the color 1 - just one color.

Sample Input 2

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

Sample Output 2

1
2
2
1
2
2
6
3
3
3


program answer
  implicit none
  integer :: X

  read(*,*) X
  if(400<=X.and.X<=599) then
     write(*,*) '8'
  else if(600<=X.and.X<=799) then
     write(*,*) '7'
  else if(800<=X.and.X<=999) then
     write(*,*) '6'
  else if(1000<=X.and.X<=1199) then
     write(*,*) '5'
   else if(1200<=X.and.X<=1399) then
      write(*,*) '4'
   else if(1400<=X.and.X<=1599) then
      write(*,*) '3'
    else if(1600<=X.and.X<=1799) then
       write(*,*) '2'
    else if(1800<=X.and.X<=1999) then
       write(*,*) '1'
    end if

    stop
   end program answer

Score: 100 points

Problem Statement

M-kun is a competitor in AtCoder, whose highest rating is X.

In this site, a competitor is given a kyu (class) according to his/her highest rating. For ratings from 400 through 1999, the following kyus are given:

From 400 through 599: 8-kyu

From 600 through 799: 7-kyu

From 800 through 999: 6-kyu

From 1000 through 1199: 5-kyu

From 1200 through 1399: 4-kyu

From 1400 through 1599: 3-kyu

From 1600 through 1799: 2-kyu

From 1800 through 1999: 1-kyu

What kyu does M-kun have?

Constraints

400 \leq X \leq 1999

X is an integer.

Input

Input is given from Standard Input in the following format:

X

Output

Print the kyu M-kun has, as an integer.
For example, if he has 8-kyu, print 8.

Sample Input 1

725

Sample Output 1

7

M-kun's highest rating is 725, which corresponds to 7-kyu.

Thus, 7 is the correct output.

Sample Input 2

1600

Sample Output 2

2

M-kun's highest rating is 1600, which corresponds to 2-kyu.

Thus, 2 is the correct output.


      PROGRAM piyo
      IMPLICIT NONE
      INTEGER,PARAMETER :: p = 16
      integer(p) :: x,kyu,i

      read*,x

      kyu = 8
      do i = 599,1999,200
        if( x<=i )then
          print*,kyu
          stop
        else
          kyu = kyu - 1
        end if
      end do

      stop
      !debugg

      print*,x,i

      END PROGRAM


program main
  implicit none
  integer i,A,B,C,K,ans
  read(*, *) A,B,C
  read(*, *) K
  !read(*, *) (L(i), i = 1,N)
  do
    if (A-B > -1) then
      K = K-1
      B = B*2
    else
      exit
    end if
  end do
  do
    if (B-C > -1) then
      K = K-1
      C = C*2
    else
      exit
    end if
  end do
  if (K < 0) then
    write(*, *) 'No'
  else
    write(*, *) 'Yes'
  end if
end program main

Score: 200 points

Problem Statement

M-kun has the following three cards:

A red card with the integer A.

A green card with the integer B.

A blue card with the integer C.

He is a genius magician who can do the following operation at most K times:

Choose one of the three cards and multiply the written integer by 2.

His magic is successful if both of the following conditions are satisfied after the operations:

The integer on the green card is strictly greater than the integer on the red card.

The integer on the blue card is strictly greater than the integer on the green card.

Determine whether the magic can be successful.

Constraints

1 \leq A, B, C \leq 7

1 \leq K \leq 7

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C
K

Output

If the magic can be successful, print Yes; otherwise, print No.

Sample Input 1

7 2 5
3

Sample Output 1

Yes

The magic will be successful if, for example, he does the following operations:

First, choose the blue card. The integers on the red, green, and blue cards are now 7, 2, and 10, respectively.

Second, choose the green card. The integers on the red, green, and blue cards are now 7, 4, and 10, respectively.

Third, choose the green card. The integers on the red, green, and blue cards are now 7, 8, and 10, respectively.

Sample Input 2

7 4 2
3

Sample Output 2

No

He has no way to succeed in the magic with at most three operations.


program magic_2
  implicit none
  integer :: a, b, c, k, n = 0
  read(*,*) a, b, c
  read(*,*) k
  do while (b <= a)
    b = 2 * b
    n = n + 1
  end do
  do while (c <= b)
    c = 2 * c
    n = n + 1
  end do
  write(*,'(a)') trim(merge('No ', 'Yes', n > k))
end program magic_2


program marks
  implicit none

  integer(8) :: n, k
  integer(8) :: a(0:200000)
  integer(8) :: i
  integer(8) :: eval0, eval

  read(*, *) n, k
  read(*, *) (a(i), i=0, n-1)

  eval = 1
  eval0 = 1

  do i = 0, n-1
    eval0 = eval
    eval = eval*a(i)
    if (i >= k) then
      eval = eval/a(i-k)
      if (eval > eval0) then
        write(*, *) 'Yes'
      else
        write(*, *) 'No'
      end if
    end if
  end do

end program marks

Score: 300 points

Problem Statement

M-kun is a student in Aoki High School, where a year is divided into N terms.

There is an exam at the end of each term. According to the scores in those exams, a student is given a grade for each term, as follows:

For the first through (K-1)-th terms: not given.

For each of the K-th through N-th terms: the multiplication of the scores in the last K exams, including the exam in the graded term.

M-kun scored A_i in the exam at the end of the i-th term.

For each i such that K+1 \leq i \leq N, determine whether his grade for the i-th term is strictly greater than the grade for the (i-1)-th term.

Constraints

2 \leq N \leq 200000

1 \leq K \leq N-1

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 K
A_1 A_2 A_3 \ldots A_N

Output

Print the answer in N-K lines.

The i-th line should contain Yes if the grade for the (K+i)-th term is greater than the grade for the (K+i-1)-th term, and No otherwise.

Sample Input 1

5 3
96 98 95 100 20

Sample Output 1

Yes
No

His grade for each term is computed as follows:

3-rd term: (96 \times 98 \times 95) = 893760

4-th term: (98 \times 95 \times 100) = 931000

5-th term: (95 \times 100 \times 20) = 190000

Sample Input 2

3 2
1001 869120 1001

Sample Output 2

No

Note that the output should be No if the grade for the 3-rd term is equal to the grade for the 2-nd term.

Sample Input 3

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

Sample Output 3

Yes
Yes
No
Yes
Yes
No
Yes
Yes


program main
    implicit none
    integer n,k
    integer, allocatable, dimension(:) :: a, e
    integer i
    read(*,*) n,k
    allocate(a(n))
    allocate(e(n))
    read(*,*) a
    e = 0
    e(k) = 1

    do i = 1, k
        e(k) = e(k)*a(i)
    end do

    do i = k+1,n,1
        e(i) = e(i-1)*a(i)/a(i-k)
        if(e(i) > e(i-1)) then
            write(*,*) "Yes"
        else
            write(*,*) "No"
        end if
    end do

end program


program MSOLUTIOND
    implicit none
    integer(8)::N,i,ans=1000
    integer(8),allocatable,dimension(:)::A
    read*,N
    allocate(A(N))
    read*,A

    do i=1,N-1
        if(A(i+1)>A(i))then
            ans=ans/A(i)*A(i+1)+mod(ans,A(i))
        end if
    end do
    print'(i0)',ans
end program MSOLUTIOND

Score: 400 points

Problem Statement

To become a millionaire, M-kun has decided to make money by trading in the next N days. Currently, he has 1000 yen and no stocks - only one kind of stock is issued in the country where he lives.

He is famous across the country for his ability to foresee the future. He already knows that the price of one stock in the next N days will be as follows:

A_1 yen on the 1-st day, A_2 yen on the 2-nd day, ..., A_N yen on the N-th day.

In the i-th day, M-kun can make the following trade any number of times (possibly zero), within the amount of money and stocks that he has at the time.

Buy stock: Pay A_i yen and receive one stock.

Sell stock: Sell one stock for A_i yen.

What is the maximum possible amount of money that M-kun can have in the end by trading optimally?

Constraints

2 \leq N \leq 80

100 \leq A_i \leq 200

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \cdots A_N

Output

Print the maximum possible amount of money that M-kun can have in the end, as an integer.

Sample Input 1

7
100 130 130 130 115 115 150

Sample Output 1

1685

In this sample input, M-kun has seven days of trading. One way to have 1685 yen in the end is as follows:

Initially, he has 1000 yen and no stocks.

Day 1: Buy 10 stocks for 1000 yen. Now he has 0 yen.

Day 2: Sell 7 stocks for 910 yen. Now he has 910 yen.

Day 3: Sell 3 stocks for 390 yen. Now he has 1300 yen.

Day 4: Do nothing.

Day 5: Buy 1 stock for 115 yen. Now he has 1185 yen.

Day 6: Buy 10 stocks for 1150 yen. Now he has 35 yen.

Day 7: Sell 11 stocks for 1650 yen. Now he has 1685 yen.

There is no way to have 1686 yen or more in the end, so the answer is 1685.

Sample Input 2

6
200 180 160 140 120 100

Sample Output 2

1000

In this sample input, it is optimal to do nothing throughout the six days, after which we will have 1000 yen.

Sample Input 3

2
157 193

Sample Output 3

1216

In this sample input, it is optimal to buy 6 stocks in Day 1 and sell them in Day 2, after which we will have 1216 yen.


program answer
  implicit none
  integer :: i, N, sum, ka
  integer, allocatable :: A(:)
  read(*,*) N
  allocate(A(N))
  read(*,*) A

  sum=1000
  ka=0

  do i = 1, N-1
     if(A(i)<=A(i+1)) then
        ka=sum/A(i)
        sum=sum-ka*A(i)
     else
        ka=0
     end if
     sum=sum+ka*A(i+1)
     ka=0
  end do
 write(*,*) sum
  deallocate(A)
  stop
 end program answer


program answer
  implicit none
  integer :: i, N, sum, ka
  integer, allocatable :: A(:)
  read(*,*) N
  allocate(A(N))
  read(*,*) A

  sum=1000
  ka=0

  do i = 1, N-1
     if(A(i)<=A(i+1)) then
        ka=sum/A(i)
	sum=sum-ka*A(i)
     else
        ka=0
     end if
     sum=sum+ka*A(i+1)
  end do
 write(*,*) sum
  deallocate(A)
  stop
 end program answer


program answer
  implicit none
  integer :: i, N, sum, ka
  integer, allocatable :: A(:)
  read(*,*) N
  allocate(A(N))
  read(*,*) A

  sum=1000
  ka=0

  do i = 1, N-1
     if(A(i)<=A(i+1)) then
        ka=sum/A(i)
        sum=sum-ka*A(i)
     else
        sum=sum+ka*A(i)
	ka=0
     end if
  end do
 write(*,*) sum+ka*A(N)

  deallocate(A)
  stop
 end program answer


program main
  implicit none
  integer i,Mst, Mcash, N
  integer,allocatable :: A(:)
  read(*, *) N
  allocate(A(N))
  read(*, *) (A(i), i = 1,N)
  Mst = 0
  Mcash = 1000
  do i = 1,N-1
    if (A(i) > A(i+1)) then
      Mcash = Mcash + A(i)*Mst
      Mst = 0
    else
      Mst = Mst + (Mcash/A(i))
      Mcash = Mcash-(Mcash/A(i))*A(i)
    end if
  end do
  Mcash = Mcash + A(N)*Mst
  write(*, *) Mcash
end program main


program pr4
  implicit none
  integer :: n
  integer,allocatable :: a(:)
  integer :: i, money, stock

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

  money = 1000
  stock = 0

  do i = 1, n-1
     if ( a(i) < a(i+1) ) then
        call buystock(a(i), money, stock)
     else if ( a(i) > a(i+1) ) then
        call sellstock(a(i), money, stock)
     else
        cycle
     end if
  end do

  if (stock > 0) then
     call sellstock(a(n),money,stock)
  end if

  write(*,'(i0)') money
  stop
contains

  subroutine buystock(price,money,stock)
    integer :: price, money, stock
    do while ( money >= price )
       money = money - price
       stock = stock + 1
    end do
    return
  end subroutine buystock

  subroutine sellstock(price,money,stock)
    integer :: price, money, stock
    do while ( stock > 0 )
       money = money + price
       stock = stock - 1
    end do
    return
  end subroutine sellstock
end program pr4


program AIsing2020B
    implicit none
    integer(8)::N,i,ans=0
    integer(8),allocatable,dimension(:)::A
    read*,N
    allocate(A(N))
    read*,A
    do i=1,N,2
        if(mod(A(i),2)==1)ans=ans+1
    end do
    print'(i0)',ans
end program AIsing2020B

Score : 200 points

Problem Statement

We have N squares assigned the numbers 1,2,3,\ldots,N. Each square has an integer written on it, and the integer written on Square i is a_i.

How many squares i satisfy both of the following conditions?

The assigned number, i, is odd.

The written integer is odd.

Constraints

All values in input are integers.

1 \leq N, a_i \leq 100

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 \cdots a_N

Output

Print the number of squares that satisfy both of the conditions.

Sample Input 1

5
1 3 4 5 7

Sample Output 1

2

Two squares, Square 1 and 5, satisfy both of the conditions.

For Square 2 and 4, the assigned numbers are not odd.

For Square 3, the written integer is not odd.

Sample Input 2

15
13 76 46 15 50 98 93 77 31 43 84 90 6 24 14

Sample Output 2

3


program AIsing2020C
    implicit none
    integer(8)::N,i,j,k,A(10000),m
    read*,N
    A=0

    do i=1,100
        do j=1,100
            do k=1,100
                m=i**2+j**2+k**2+i*j+j*k+k*i
                if(m<=10000)A(m)=A(m)+1
            end do
        end do
    end do

    do i=1,N
        print'(i0)',A(i)
    end do
end program AIsing2020C

Score : 300 points

Problem Statement

Let f(n) be the number of triples of integers (x,y,z) that satisfy both of the following conditions:

1 \leq x,y,z

x^2 + y^2 + z^2 + xy + yz + zx = n

Given an integer N, find each of f(1),f(2),f(3),\ldots,f(N).

Constraints

All values in input are integers.

1 \leq N \leq 10^4

Input

Input is given from Standard Input in the following format:

N

Output

Print N lines. The i-th line should contain the value f(i).

Sample Input 1

20

Sample Output 1

0
0
0
0
0
1
0
0
0
0
3
0
0
0
0
0
3
3
0
0

For n=6, only (1,1,1) satisfies both of the conditions. Thus, f(6) = 1.

For n=11, three triples, (1,1,2), (1,2,1), and (2,1,1), satisfy both of the conditions. Thus, f(6) = 3.

For n=17, three triples, (1,2,2), (2,1,2), and (2,2,1), satisfy both of the conditions. Thus, f(17) = 3.

For n=18, three triples, (1,1,3), (1,3,1), and (3,1,1), satisfy both of the conditions. Thus, f(18) = 3.


program atcoder
implicit none
integer :: n , x , y , z , i , sum
real :: j
read *,n
sum = 0
j = real(n)
do i = 1 , n
  sum = 0
  do x = 1 , int(sqrt(j/6.0))
    do y = x , int(sqrt(j/3.0))+1
      do z = y , int(sqrt(j))+1
        if(x**2 + y**2 + z**2 + x*y + y*z + z*x == i)then
          sum = sum + 1
          if(x == y .and. y == z)goto 10
        end if
      end do
    end do
  end do
  print *, sum * 3
  goto 20
10 print *, sum * 3 - 2
20 end do
end program atcoder


program atcoder
implicit none
integer :: n , x , y , z , i , sum
real :: j
read *,n
sum = 0
j = real(n)
do i = 1 , n
  sum = 0
  do x = 1 , int(sqrt(j/6.0))+1
    do y = x , int(sqrt(j))+1
      do z = y , int(sqrt(j))+1
        if(x**2 + y**2 + z**2 + x*y + y*z + z*x == i)then
          sum = sum + 1
          if(x == y .and. y == z)goto 10
        end if
      end do
    end do
  end do
  print *, sum * 3
  goto 20
10 print *, sum * 3 - 2
20 end do
end program atcoder


program tsundoku
  implicit none
  integer :: n, m, i,j, h = 0, l, r, p
  integer(8) :: k, t
  integer(8), dimension(0:200001) :: a = 0, b = 0,c=0, x = 0, y = 0
  read(*,*) n
    do t=1,n
    m=int(sqrt(dble(t)/1.3))
    p=0
	do i=1,m
    	do j=1,m
        	do k=1,m
            	if((i+j)*(i+j)+(i+k)*(i+k)+(k+j)*(k+j)==2*t)then
   !             a(i)=i
   !             b(i)=j
   !             c(i)=k
                p=p+1
                end if
            end do
        end do
    end do
!   do i=1,m

!    if()
    write(*,*)p
    end do
end program tsundoku


program main
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32), allocatable:: f(:)
    integer(int32):: x,y,z,n,v,i

    read*, n
    allocate(f(n), source=0)

    do x=1,100
        do y=1,100
            do z=1,100
                v = x*x + y*y + z*z +x*y + y*z + z*x
                if (v <= n) f(v)=f(v)+1
            end do
        end do
    end do

    do i=1,n
        print'(i0)', f(i)
    end do
end program main


program abc10
 implicit none
 integer,allocatable::X(:)
 integer::N,i,K,s
 read(*,*)N
 allocate(X(N))
 read(*,'(bN)')K
 do i=1,N
  s=0
  if(ibits(K,N-i,1)==0)then
   X(i)=K+2**(N-i)
  else
   X(i)=K-2**(N-i)
  end if
  do while(X(i)>0)
   X(i)=mod(X(i),popcnt(X(i)))
   s=s+1
  end do
 print'(i0)',s
 end do
 deallocate(X)
end program abc10

Score : 400 points

Problem Statement

Let \mathrm{popcount}(n) be the number of 1s in the binary representation of n.
For example, \mathrm{popcount}(3) = 2, \mathrm{popcount}(7) = 3, and \mathrm{popcount}(0) = 0.

Let f(n) be the number of times the following operation will be done when we repeat it until n becomes 0: "replace n with the remainder when n is divided by \mathrm{popcount}(n)." (It can be proved that, under the constraints of this problem, n always becomes 0 after a finite number of operations.)

For example, when n=7, it becomes 0 after two operations, as follows:

\mathrm{popcount}(7)=3, so we divide 7 by 3 and replace it with the remainder, 1.

\mathrm{popcount}(1)=1, so we divide 1 by 1 and replace it with the remainder, 0.

You are given an integer X with N digits in binary.
For each integer i such that 1 \leq i \leq N, let X_i be what X becomes when the i-th bit from the top is inverted.
Find f(X_1), f(X_2), \ldots, f(X_N).

Constraints

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

X is an integer with N digits in binary, possibly with leading zeros.

Input

Input is given from Standard Input in the following format:

N
X

Output

Print N lines. The i-th line should contain the value f(X_i).

Sample Input 1

3
011

Sample Output 1

2
1
1

X_1 = 7, which will change as follows: 7 \rightarrow 1 \rightarrow 0. Thus, f(7) = 2.

X_2 = 1, which will change as follows: 1 \rightarrow 0. Thus, f(1) = 1.

X_3 = 2, which will change as follows: 2 \rightarrow 0. Thus, f(2) = 1.

Sample Input 2

23
00110111001011011001110

Sample Output 2

2
1
2
2
1
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
1
3


program anything_goes_to_zero
  implicit none
  integer :: n, i, m, k, cnt = 0, rem1to0 = 0, rem0to1 = 0, rem
  character(200000) :: x
  read(*,*) n
  read(*,*) x
  do i = 1, n
    if (x(i:i) == '1') cnt = cnt + 1
  end do
  do i = 1, n
    k = ichar(x(i:i)) - 48
    rem1to0 = mod(2 * rem1to0 + k, cnt - 1)
    rem0to1 = mod(2 * rem0to1 + k, cnt + 1)
  end do
  do i = 1, n
    if (x(i:i) == '1') then
      rem = modulo(rem1to0 - pow(2, n - i, cnt - 1), cnt - 1)
    else
      rem = mod(rem0to1 + pow(2, n - i, cnt + 1), cnt + 1)
    end if
    m = 1
    do while (rem > 0)
      k = popcnt(rem)
      rem = mod(rem, k)
      m = m + 1
    end do
    write(*,'(i0)') m
  end do
contains
  pure elemental integer function pow(a, n, modu) result(res)
    integer, intent(in) :: a, n, modu
    integer :: b, m
    res = 1
    b = a
    m = n
    do while (m > 0)
      if (btest(m, 0)) res = mod(res * b, modu)
      b = mod(b * b, modu)
      m = rshift(m, 1)
    end do
  end
end program anything_goes_to_zero


program testcase
    integer(8) n
    integer i
    integer :: ac = 0
    integer :: tle = 0
    integer :: wa = 0
    integer :: re = 0
    character(3) str
    !character, allocatable :: s(:)
    read *, n
    !allocate (s(n))
    do i = 1, n
        read *, str
        if(str .eq. "AC")then
            ac = ac + 1
        else if(str .eq. "WA")then
            wa = wa + 1
        else if(str .eq. "RE")then
            re = re + 1
        else if(str .eq. "TLE")then
            tle = tle + 1
        end if
    end do
    print *, 'AC ×', ac
    print *, 'WA ×', wa
    print *, 'RE ×', re
    print *, 'TLE ×', tle
end program

Score : 200 points

Problem Statement

Takahashi is participating in a programming contest called AXC002, and he has just submitted his code to Problem A.

The problem has N test cases.

For each test case i (1\leq i \leq N), you are given a string S_i representing the verdict for that test case. Find the numbers of test cases for which the verdict is AC, WA, TLE, and RE, respectively.

See the Output section for the output format.

Constraints

1 \leq N \leq 10^5

S_i is AC, WA, TLE, or RE.

Input

Input is given from Standard Input in the following format:

N
S_1
\vdots
S_N

Output

Let C_0, C_1, C_2, and C_3 be the numbers of test cases for which the verdict is AC, WA, TLE, and RE, respectively. Print the following:

AC x C_0
WA x C_1
TLE x C_2
RE x C_3

Sample Input 1

6
AC
TLE
AC
AC
WA
TLE

Sample Output 1

AC x 3
WA x 1
TLE x 2
RE x 0

We have 3, 1, 2, and 0 test case(s) for which the verdict is AC, WA, TLE, and RE, respectively.

Sample Input 2

10
AC
AC
AC
AC
AC
AC
AC
AC
AC
AC

Sample Output 2

AC x 10
WA x 0
TLE x 0
RE x 0


program main
    use iso_fortran_env
    implicit none
    integer(int32) :: n, i
    integer(int32) :: ac, wa, tle, re
    character(3) :: si

    ac = 0
    wa = 0
    tle = 0
    re = 0
    read *, n

    do i = 1, n
        read *, si
        if (trim(si) == "AC") then
            ac = ac + 1
        elseif (trim(si) == "WA") then
            wa = wa + 1
        elseif (trim(si) == "TLE") then
            tle = tle + 1
        elseif (trim(si) == "RE") then
            re = re + 1
        endif
    enddo

    print "( 'AC x ', i0 )", ac
    print "( 'WA x ', i0 )", wa
    print "( 'TLE x ', i0 )", tle
    print "( 'RE x ', i0 )", re

end program main


program bm
implicit none
integer::N,C(4)=0
character::S
integer::i
read*,N
do i=1,N
    read*,S
    select case(S)
    case("A")
        C(1)=C(1)+1
    case("W")
        C(2)=C(2)+1
    case("T")
        C(3)=C(3)+1
    case("R")
        C(4)=C(4)+1
    end select
end do
print'("AC x ",i0)',C(1)
print'("WA x ",i0)',C(2)
print'("TLE x ",i0)',C(3)
print'("RE x ",i0)',C(4)
end program bm


program answer
  implicit none

  integer :: N, i, a, b, c, d
  character :: S(100000)

  read(*,*) N
  do i = 1, n
     read(*,*) S(i:i)
  end do

  a=0
  b=0
  c=0
  d=0

  do i = 1, n
     if(S(i)=='A') then
        a=a+1
     else if(S(i)=="W") then
        b=b+1
     else if(S(i)=="T") then
        c=c+1
     else if(S(i)=="R") then
        d=d+1
     end if
  end do

  write(*,*) 'AC', ' x',a
  write(*,*) 'WA', ' x', b
  write(*,*) 'TLE', ' x', c
  write(*,*) 'RE', ' x', d

  stop
 end program answer


program b173

implicit none
integer :: ac, wa, tle, re, n, i
character(len=3) :: a

ac = 0
wa = 0
tle = 0
re = 0

read *, n

do i = 1, n

 read *, a

 if (trim(a) == 'AC') then
  ac = ac + 1
 else if (trim(a) == 'WA') then
  wa = wa + 1
 else if (trim(a) == 'TLE') then
  tle = tle + 1
 else
  re = re + 1
 end if
end do

print *, 'AC x ', ac
print *, 'WA x ', wa
print *, 'TLE x ', tle
print *, 'RE x ', re

end program b173


program main
    use iso_fortran_env
    implicit none
    integer(int32) :: i, j, n
    integer(int32) :: k, h, w, ans
    integer(int32), allocatable :: nh(:), nw(:)
    character(1), allocatable :: c(:,:)

    read *, h, w, k
    allocate( c(h, w) )
    allocate( nh(h) )
    allocate( nw(w) )
    ans = 0

    do i = 1, h
        read "(*(a1))", c( i, 1:w )
    enddo

    do j = 0, 2**w - 1
        nw = pack( [(n, n = 1, w)], [(btest(j, n), n = 0, w-1)] )

        do i = 0, 2**h - 1
            nh = pack( [(n, n = 1, h)], [(btest(i, n), n = 0, h-1)] )

            if (( count( c(nh, nw) == "#" ) ) == k) then
                ans = ans + 1
            endif

        enddo
    enddo

    print *, ans

end program main

Score : 300 points

Problem Statement

We have a grid of H rows and W columns of squares. The color of the square at the i-th row from the top and the j-th column from the left (1 \leq i \leq H, 1 \leq j \leq W) is given to you as a character c_{i,j}: the square is white if c_{i,j} is ., and black if c_{i,j} is #.

Consider doing the following operation:

Choose some number of rows (possibly zero), and some number of columns (possibly zero). Then, paint red all squares in the chosen rows and all squares in the chosen columns.

You are given a positive integer K. How many choices of rows and columns result in exactly K black squares remaining after the operation? Here, we consider two choices different when there is a row or column chosen in only one of those choices.

Constraints

1 \leq H, W \leq 6

1 \leq K \leq HW

c_{i,j} is . or #.

Input

Input is given from Standard Input in the following format:

H W K
c_{1,1}c_{1,2}...c_{1,W}
c_{2,1}c_{2,2}...c_{2,W}
:
c_{H,1}c_{H,2}...c_{H,W}

Output

Print an integer representing the number of choices of rows and columns satisfying the condition.

Sample Input 1

2 3 2
..#
###

Sample Output 1

5

Five choices below satisfy the condition.

The 1-st row and 1-st column

The 1-st row and 2-nd column

The 1-st row and 3-rd column

The 1-st and 2-nd column

The 3-rd column

Sample Input 2

2 3 4
..#
###

Sample Output 2

1

One choice, which is choosing nothing, satisfies the condition.

Sample Input 3

2 2 3
##
##

Sample Output 3

0

Sample Input 4

6 6 8
..##..
.#..#.
#....#
######
#....#
#....#

Sample Output 4

208


program main
  implicit none
  integer i, H, W, K, j, ans, l, m, s, v
  integer,allocatable :: G(:,:), G0(:,:)
  character(6) c
  read(*, *)H, W, K
  allocate(G(H,W))
  allocate(G0(H,W))
  do i = 1,H
    read(*, *) c
    do j = 1,W
      if (c(j:j) == '.') then
        G0(i,j) = 0
      else if (c(j:j) == '#') then
        G0(i,j) = 1
      end if
    end do
  end do

  ans = 0

  do i = 0,2**(H + W)-1
    s = i
    G = G0
    v = H + W -1
    do j = 1,H
      if (s >= 2**v) then
        do l = 1,W
          G(j,l) = 0
        end do
        s = s-2**v
      end if
      v = v-1
    end do
    do j = 1, W
      if (s >= 2**v) then
        do l = 1,H
          G(l,j) = 0
        end do
        s = s-2**v
      end if
      v = v-1
    end do
    m = sum(G)
    !write(*, *) G
    if (m == K) then
      ans = ans + 1
    end if
  end do
  !read(*, *) (L(i), i = 1,N)

  write(*, *) ans
end program main


program ABC173D
    implicit none
    integer(8)::N
    integer(8),allocatable,dimension(:)::A
    read*,N
    allocate(A(N))
    read*,A
    print'(i0)',sum(A)-minval(A)
end program ABC173D

Score: 400 points

Problem Statement

Quickly after finishing the tutorial of the online game ATChat, you have decided to visit a particular place with N-1 players who happen to be there. These N players, including you, are numbered 1 through N, and the friendliness of Player i is A_i.

The N players will arrive at the place one by one in some order. To make sure nobody gets lost, you have set the following rule: players who have already arrived there should form a circle, and a player who has just arrived there should cut into the circle somewhere.

When each player, except the first one to arrive, arrives at the place, the player gets comfort equal to the smaller of the friendliness of the clockwise adjacent player and that of the counter-clockwise adjacent player. The first player to arrive there gets the comfort of 0.

What is the maximum total comfort the N players can get by optimally choosing the order of arrivals and the positions in the circle to cut into?

Constraints

All values in input are integers.

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

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \dots A_N

Output

Print the maximum total comfort the N players can get.

Sample Input 1

4
2 2 1 3

Sample Output 1

7

By arriving at the place in the order Player 4, 2, 1, 3, and cutting into the circle as shown in the figure, they can get the total comfort of 7.

They cannot get the total comfort greater than 7, so the answer is 7.

Sample Input 2

7
1 1 1 1 1 1 1

Sample Output 2

6


module module_sort
    use iso_fortran_env
    implicit none

    interface quicksort
        module procedure :: int32_qsort
        module procedure :: int64_qsort
    end interface quicksort
contains

    recursive function int32_qsort(m) result(res)
        integer(int32), intent(in) :: m(:)
        integer(int32) :: res(size(m))

        if (size(m) <= 1) then; res = m
        else; res = [int32_qsort( pack(m(2:), m(2:) >= m(1)) ), m(1), int32_qsort( pack(m(2:), m(2:) < m(1)) )]
        end if

    end function int32_qsort

    recursive function int64_qsort(m) result(res)
        integer(int64), intent(in) :: m(:)
        integer(int64) :: res(size(m))

        if (size(m) <= 1) then; res = m
        else; res = [int64_qsort( pack(m(2:), m(2:) >= m(1)) ), m(1), int64_qsort( pack(m(2:), m(2:) < m(1)) )]
        end if

    end function int64_qsort

end module module_sort

program main
    use module_sort
    use iso_fortran_env
    implicit none
    integer(int64) :: n
    integer(int64), allocatable :: ai(:)
    integer(int64) :: i, ie
    integer(int64) :: ans

    read *, n
    allocate( ai(n) )
    read *, ai( 1:n )
!    ai = quicksort( ai )
    call qsort( ai )
    ai(n:1:-1) = ai(1:n)

    print *, ai

    if ( mod((n-1), 2) == 0 ) then
        ie = (n-1) / 2
        ans = ai(1) + ai(ie + 1)
    else
        ie = n / 2
        ans = ai(1)
    endif

    do i = 2, ie
        ans = ans + ai(i) * 2
    enddo

    print *, ans

contains

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

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

end program main


program abc5
 implicit none
 integer::N=0,K=0,s=0,i,ishota,ishotb,p=0,q=0
 integer,allocatable::a(:)
 read(*,*)N,K
 allocate(a(N))
 read(*,*)(a(i),i=1,N)
 do ishota=1,N
  do ishotb=1,N-1
   if(a(ishotb)>a(ishotb+1))then
    p=a(ishotb)
    q=a(ishotb+1)
    a(ishotb)=q
    a(ishotb+1)=p
   end if
  end do
 end do
 s=product(a(N-K+1:N))
 print'(i0)',mod(s,(10**9+7))
 deallocate(a)
end program abc5

Score : 500 points

Problem Statement

Given are N integers A_1,\ldots,A_N.

We will choose exactly K of these elements. Find the maximum possible product of the chosen elements.

Then, print the maximum product modulo (10^9+7), using an integer between 0 and 10^9+6 (inclusive).

Constraints

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

|A_i| \leq 10^9

Input

Input is given from Standard Input in the following format:

N K
A_1 \ldots A_N

Output

Print the maximum product modulo (10^9+7), using an integer between 0 and 10^9+6 (inclusive).

Sample Input 1

4 2
1 2 -3 -4

Sample Output 1

12

The possible products of the two chosen elements are 2, -3, -4, -6, -8, and 12, so the maximum product is 12.

Sample Input 2

4 3
-1 -2 -3 -4

Sample Output 2

1000000001

The possible products of the three chosen elements are -24, -12, -8, and -6, so the maximum product is -6.

We print this value modulo (10^9+7), that is, 1000000001.

Sample Input 3

2 1
-1 1000000000

Sample Output 3

1000000000

The possible products of the one chosen element are -1 and 1000000000, so the maximum product is 1000000000.

Sample Input 4

10 10
1000000000 100000000 10000000 1000000 100000 10000 1000 100 10 1

Sample Output 4

999983200

Be sure to print the product modulo (10^9+7).


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 main
    use,intrinsic :: iso_fortran_env
    use merge_sort_mod
    implicit none
    integer(int64):: n,k,i,ans=1,l,n2
    integer(int64),parameter:: md=1000000007
    integer(int64), allocatable:: a(:),ma(:),mb(:)

    read*, n,k
    allocate(a(0:n),source=1_8)
    allocate(ma((k+1)/2),source=1_8)
    n2 = (n-k+1)/2
    allocate(mb(n2),source=1_8)
    read*, a(1:n)

    call merge_sort(a(1:n),1_8,n)
    a(1:n) = a(n:1:-1)
    l=1
    do i=k-1,0,-2
        ma(l) = a(i)*a(i+1)
        l=l+1
    end do

    call merge_sort(ma(:),1_8,(k+1)/2)

    if (n2==0) then
        do i=1,(k+1)/2
            ma(i) = mod(ma(i),md)
            ans=mod(ans*ma(i),md)
        end do
        print'(i0)', modulo(ans,md)
        stop
    end if

    l=1
    a(k) = 1
    do i=n-1,k,-2
        mb(l)=a(i)*a(i+1)
        l=l+1
    end do
    call merge_sort(mb(:), 1_8, n2)
    mb(:) = mb(n2:1:-1)

    l=1
    do i=1,(k+1)/2
        if (ma(i) < mb(l)) then
            ma(i) = mb(l)
            l=l+1
        end if
        ma(i) = modulo(ma(i),md)
        if (l>n2) exit
    end do
    do i=1,(k+1)/2
        ans=modulo(ans*ma(i),md)
    end do
    print'(i0)', ans
end program main


program multiplication_4
  implicit none
  integer(8), parameter :: m = 1000000007
  integer :: n, k, i, j, cnt = 0
  integer(8) :: a(200000, 2) = 0, nega = -1, posi = -1, x = 1, y = -1, z = -1
  real(8) :: nn = -1.d0, pp = -1.d0
  read(*,*) n, k
  read(*,*) a(1:n, 1)
  if (n == k) then
    do i = 1, n
      x = modulo(x * a(i, 1), m)
    end do
    write(*,'(i0)') x
    stop
  end if
  call merge_sort(a(1:n, :))
  if (all(a(1:n, 1) <= 0)) then
    if (mod(k, 2) == 0) then
      do i = 1, k
        x = modulo(x * a(n - i + 1, 1), m)
      end do
    else
      do i = 1, k
        x = modulo(x * a(i, 1), m)
      end do
    end if
    write(*,'(i0)') x
    stop
  end if
  do i = 1, n
    if (a(i, 1) >= 0) then
      a(i, 2) = 0
    else
      a(i, 1) = -a(i, 1)
      a(i, 2) = 1
    end if
  end do
  call merge_sort(a(1:n, :))
  do i = 1, k
    x = mod(x * a(i, 1), m)
    if (a(i, 2) == 1) then
      cnt = cnt + 1
      nega = a(i, 1)
    else
      posi = a(i, 1)
    end if
  end do
  if (x == 0 .or. mod(cnt, 2) == 0) then
    write(*,'(i0)') x
    stop
  end if
  if (posi /= -1) then
    do i = k + 1, n
      if (a(i, 2) == 1) then
        y = mod(x * a(i, 1), m)
        y = mod(y * inv(posi), m)
        pp = a(i, 1) / real(posi, 8)
        exit
      end if
    end do
  end if
  if (nega /= -1) then
    do i = k + 1, n
      if (a(i, 2) == 0) then
        z = mod(x * a(i, 1), m)
        z = mod(z * inv(nega), m)
        nn = a(i, 1) / real(nega, 8)
        exit
      end if
    end do
  end if
  write(*,'(i0)') merge(y, z, pp > nn)
contains
  subroutine merge_sort(a)
    integer(8), intent(inout) :: a(:, :)
    integer :: n, m, l, i, u
    n = size(a, 1)
    m = n
    l = 1
    do while (m > 1)
      do i = 1, m / 2
        u = min(2 * i * l, n)
        call merger(a(2 * (i - 1) * l + 1:(2 * i - 1) * l, :), a((2 * i - 1) * l + 1:u, :))
      end do
      l = 2 * l
      m = (m + 1) / 2
    end do
  end
  subroutine merger(a1, a2)
    integer(8), intent(inout) :: a1(:, :), a2(:, :)
    integer(8) :: a(size(a1, 1) + size(a2, 1), size(a1, 2))
    integer :: i1, i2, n1, n2
    i1 = 1
    i2 = 1
    n1 = size(a1, 1)
    n2 = size(a2, 1)
    do while (i1 <= n1 .and. i2 <= n2)
      if (a1(i1, 1) > a2(i2, 1) .or. &
      (a1(i1, 1) == a2(i2, 1) .and. a1(i1, 2) <= a2(i2, 2))) then
        a(i1+i2-1, :) = a1(i1, :)
        i1 = i1+1
      else
        a(i1+i2-1, :) = a2(i2, :)
        i2 = i2+1
       end if
    end do
    if (i1 <= n1) then
      a(i1 + i2 - 1:n1 + n2, :) = a1(i1:n1, :)
    else
      a(i1 + i2 - 1:n1 + n2, :) = a2(i2:n2, :)
    end if
    a1 = a(1:n1, :)
    a2 = a(n1 + 1:n1 + n2, :)
  end
  pure elemental integer(8) function inv(v) result(res)
    integer(8), intent(in) :: v
    integer(8) :: a, b, c, x, y, z
    a = v
    b = m
    c = 0
    res = 1
    do while (b /= 0)
      x = b
      y = mod(a, b)
      z = res - a / b * c
      res = c
      a = x
      b = y
      c = z
    end do
    res = modulo(res, m)
  end
end program multiplication_4


program sample
implicit none

character(len=2*10**5) :: S, T
integer :: a, i, sum, ans
character(1), allocatable :: p(:), q(:)

read(*,*) S, T
a = len_trim(S)
allocate(p(a))
allocate(q(a))

do i = 1, a
p(i)=S(i:i)
enddo

do i = 1, a
q(i)=T(i:i)
enddo

sum=0
do i =1, a
if(p(i) == q(i))then
sum = sum+1
endif
enddo
ans = a - sum
write(*,*) ans

deallocate(p)
deallocate(q)

stop
end program sample

Score : 200 points

Problem Statement

Given are strings S and T. Consider changing S to T by repeating the operation below. Find the minimum number of operations required to do so.

Operation: Choose one character of S and replace it with a different character.

Constraints

S and T have lengths between 1 and 2\times 10^5 (inclusive).

S and T consists of lowercase English letters.

S and T have equal lengths.

Input

Input is given from Standard Input in the following format:

S
T

Output

Print the answer.

Sample Input 1

cupofcoffee
cupofhottea

Sample Output 1

4

We can achieve the objective in four operations, such as the following:

First, replace the sixth character c with h.

Second, replace the eighth character f with t.

Third, replace the ninth character f with t.

Fourth, replace the eleventh character e with a.

Sample Input 2

abcde
bcdea

Sample Output 2

5

Sample Input 3

apple
apple

Sample Output 3

0

No operations may be needed to achieve the objective.


program main
    use iso_fortran_env
    implicit none
    integer(int64) :: n, i
    integer(int64), allocatable :: a(:)
    integer(int64) :: s, x, d, ans
    logical :: bx, bd

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

    x = 0
    d = 0
    s = a(1) + a(2)
    ans = 0

    if ( n>2 ) then
        do i = 3, n
            x = ieor(x, a(i))
        enddo
    endif

    if ( trailz(s-x) >= 1 ) then
        d = (s - x) / 2
    else
        print *, "-1"
        stop
    endif

    if ( iand(x, d) /= 0) then
        print *, "-1"
        stop
    endif

    do i = 0, 63
        bd = btest(d, i)
        if (bd) then
            ans = ibset(ans, i)
        endif
    enddo

    do i = 63, 0, -1
        bx = btest(x, i)
        if (bx) then
            if ( ibset(ans, i) <= a(1) ) then
                ans = ibset(ans, i)
            endif
        endif
    enddo

    if ((ans == 0) .or. (ans > a(1))) then
        print *, "-1"
        stop
    endif

    print *, a(1) - ans

end program main

Score : 600 points

Problem Statement

There are N piles of stones. The i-th pile has A_i stones.

Aoki and Takahashi are about to use them to play the following game:

Starting with Aoki, the two players alternately do the following operation:

Operation: Choose one pile of stones, and remove one or more stones from it.

When a player is unable to do the operation, he loses, and the other player wins.

When the two players play optimally, there are two possibilities in this game: the player who moves first always wins, or the player who moves second always wins, only depending on the initial number of stones in each pile.

In such a situation, Takahashi, the second player to act, is trying to guarantee his win by moving at least zero and at most (A_1 - 1) stones from the 1-st pile to the 2-nd pile before the game begins.

If this is possible, print the minimum number of stones to move to guarantee his victory; otherwise, print -1 instead.

Constraints

2 \leq N \leq 300

1 \leq A_i \leq 10^{12}

Input

Input is given from Standard Input in the following format:

N
A_1 \ldots A_N

Output

Print the minimum number of stones to move to guarantee Takahashi's win; otherwise, print -1 instead.

Sample Input 1

2
5 3

Sample Output 1

1

Without moving stones, if Aoki first removes 2 stones from the 1-st pile, Takahashi cannot win in any way.

If Takahashi moves 1 stone from the 1-st pile to the 2-nd before the game begins so that both piles have 4 stones, Takahashi can always win by properly choosing his actions.

Sample Input 2

2
3 5

Sample Output 2

-1

It is not allowed to move stones from the 2-nd pile to the 1-st.

Sample Input 3

3
1 1 2

Sample Output 3

-1

It is not allowed to move all stones from the 1-st pile.

Sample Input 4

8
10 9 8 7 6 5 4 3

Sample Output 4

3

Sample Input 5

3
4294967297 8589934593 12884901890

Sample Output 5

1

Watch out for overflows.


program main
    use iso_fortran_env
    implicit none
    integer(int64) :: n, i
    integer(int64), allocatable :: a(:)
    integer(int64) :: s, x, d, ans
    logical :: bx, bd

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

    x = 0
    d = 0
    s = a(1) + a(2)
    ans = 0

    if ( n>2 ) then
        do i = 3, n
            x = ieor(x, a(i))
        enddo
    endif

    if ( trailz(s-x) >= 1 ) then
        d = (s - x) / 2
    else
        print *, "-1"
        stop
    endif

    if ( iand(x, d) /= 0) then
        print *, "-1"
        stop
    endif

    do i = 0, 63
        bd = btest(d, i)
        if (bd) then
            ans = ibset(ans, i)
        endif
    enddo

    do i = 63, 0, -1
        bx = btest(x, i)
        if (bx) then
            if ( ibset(ans, i) <= a(1) ) then
                ans = ibset(ans, i)
            endif
        endif
    enddo

    ans = a(1) - ans

    if (ans <= 0) then
        print *, "-1"
    else
        print *, ans
    endif

end program main


program unfair_nim
  implicit none
  integer :: n, i
  integer(8) :: a(300) = 0, s, x = 0, d, y = 0
  read(*,*) n
  read(*,*) a(1:n)
  s = a(1) + a(2)
  do i = 3, n
    x = xor(x, a(i))
  end do
  if (s - x < 0 .or. mod(s - x, 2) /= 0) then
    write(*,'(i0)') -1
    stop
  end if
  d = (s - x) / 2
  if (and(d, x) /= 0 .or. d > a(1)) then
    write(*,'(i0)') -1
    stop
  end if
  do i = 62, 0, -1
    if (.not.btest(x, i)) cycle
    y = ibset(y, i)
    if (xor(d, y) > a(1)) y = ibclr(y, i)
  end do
  write(*,'(i0)') merge(-1_8, a(1) - xor(d, y), xor(d, y) == 0)
end program unfair_nim


program sample
implicit none

integer(8) :: N, K, i, sum
integer(8), allocatable:: p(:)

read(*,*) N, K
allocate(p(N))
read(*,*) p

if(N > 1)then
do i = 1, N
if(p(i) >= p(i+1))then
p(i)=p(i+1)
endif
end do

sum = 0
do i = 1, K
sum = sum + p(i)
end do

write(*,*) sum
end if

if(N == 1)then
sum = p(1)
write(*,*) sum
end if

stop

end program sample

Score : 200 points

Problem Statement

A shop sells N kinds of fruits, Fruit 1, \ldots, N, at prices of p_1, \ldots, p_N yen per item, respectively. (Yen is the currency of Japan.)

Here, we will choose K kinds of fruits and buy one of each chosen kind. Find the minimum possible total price of those fruits.

Constraints

1 \leq K \leq N \leq 1000

1 \leq p_i \leq 1000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
p_1 p_2 \ldots p_N

Output

Print an integer representing the minimum possible total price of fruits.

Sample Input 1

5 3
50 100 80 120 80

Sample Output 1

210

This shop sells Fruit 1, 2, 3, 4, and 5 for 50 yen, 100 yen, 80 yen, 120 yen, and 80 yen, respectively.

The minimum total price for three kinds of fruits is 50 + 80 + 80 = 210 yen when choosing Fruit 1, 3, and 5.

Sample Input 2

1 1
1000

Sample Output 2

1000


program mixjuice

 implicit none
 integer::n,k,temp,i,j,ans
 integer,allocatable::p(:)

 read*,n,k
 allocate(p(n))
 read*,p
! print*,p

!sort

 do i=1,n-1
  do j=1,n-i
   if(p(i)>=p(j+i)) then
    temp=p(j+i)
    p(j+i)=p(i)
    p(i)=temp
   end if
  end do
 end do
!print*,p

ans=0
 do i=1,k
  ans=ans+p(i)
 end do

print*,ans

end program mixjuice


program ABC171_B
  integer::N,K
  integer,allocatable::P(:)
  read(*,*)N,K
  allocate(P(N))
  read(*,*)P
  do i=1,N-1
     do j=i+1,N
        if(P(i)>P(j)) then
           L=P(i)
           P(i)=P(j)
           P(j)=L
        end if
     end do
  end do
  write(*,*)sum(P(1:K))
end program ABC171_B


program ABC171_C
  character(11)::B
  integer(16)::N,cnt=1,i,j
  integer(16),allocatable::C(:)
  read(*,*)N
  do i=1,11
     if(N>26**i) then
        N=N-26**i
        cnt=cnt+1
     else
        allocate(C(cnt))
        C=97
        do j=1,cnt
           C(j)=mod(N,26)+C(j)-1
           if(C(j)==96) C(j)=122
           if(j>1 .and. C(j)/=122) C(j)=C(j)+1
           N=N/26
           if(N==0)  then
              exit
           end if
        end do
        exit
     end if
  end do
  do i=1,cnt
     B(cnt+1-i:cnt+1-i)=char(C(i))
  end do
  B=adjustl(B)
  B=trim(B)
  write(*,'(a)')B(1:cnt)
end program ABC171_C

Score : 300 points

Problem Statement

1000000000000001 dogs suddenly appeared under the roof of Roger's house, all of which he decided to keep. The dogs had been numbered 1 through 1000000000000001, but he gave them new names, as follows:

the dogs numbered 1,2,\cdots,26 were respectively given the names a, b, ..., z;

the dogs numbered 27,28,29,\cdots,701,702 were respectively given the names aa, ab, ac, ..., zy, zz;

the dogs numbered 703,704,705,\cdots,18277,18278 were respectively given the names aaa, aab, aac, ..., zzy, zzz;

the dogs numbered 18279,18280,18281,\cdots,475253,475254 were respectively given the names aaaa, aaab, aaac, ..., zzzy, zzzz;

the dogs numbered 475255,475256,\cdots were respectively given the names aaaaa, aaaab, ...;

and so on.

To sum it up, the dogs numbered 1, 2, \cdots were respectively given the following names:

a, b, ..., z, aa, ab, ..., az, ba, bb, ..., bz, ..., za, zb, ..., zz, aaa, aab, ..., aaz, aba, abb, ..., abz, ..., zzz, aaaa, ...

Now, Roger asks you:

"What is the name for the dog numbered N?"

Constraints

N is an integer.

1 \leq N \leq 1000000000000001

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer to Roger's question as a string consisting of lowercase English letters.

Sample Input 1

2

Sample Output 1

b

Sample Input 2

27

Sample Output 2

aa

Sample Input 3

123456789

Sample Output 3

jjddja


program sample
    implicit none

    integer(8) :: i,j,m,n,k,q,b,c,t(100000)

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

    read(*,*) n
    allocate(a(n))
    read(*,*)a
    read(*,*)q
    do i=1,10000
        t(i)=0
    end do
    do i=1,n
        m=a(i)
        t(m)=t(m)+1
    end do

    do i=1,q
        m=0
        read(*,*)b,c
        t(c)=t(c)+t(b)
        t(b)=0
        do j=1,100000
            m=m+j*t(j)

        end do
        write(*,*)m
    end do

    stop
end program sample

Score : 400 points

Problem Statement

You have a sequence A composed of N positive integers: A_{1}, A_{2}, \cdots, A_{N}.

You will now successively do the following Q operations:

In the i-th operation, you replace every element whose value is B_{i} with C_{i}.

For each i (1 \leq i \leq Q), find S_{i}: the sum of all elements in A just after the i-th operation.

Constraints

All values in input are integers.

1 \leq N, Q, A_{i}, B_{i}, C_{i} \leq 10^{5}

B_{i} \neq C_{i}

Input

Input is given from Standard Input in the following format:

N
A_{1} A_{2} \cdots A_{N}
Q
B_{1} C_{1}
B_{2} C_{2}
\vdots
B_{Q} C_{Q}

Output

Print Q integers S_{i} to Standard Output in the following format:

S_{1}
S_{2}
\vdots
S_{Q}

Note that S_{i} may not fit into a 32-bit integer.

Sample Input 1

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

Sample Output 1

11
12
16

Initially, the sequence A is 1,2,3,4.

After each operation, it becomes the following:

2, 2, 3, 4

2, 2, 4, 4

4, 4, 4, 4

Sample Input 2

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

Sample Output 2

8
4
4

Note that the sequence A may not contain an element whose value is B_{i}.

Sample Input 3

2
1 2
3
1 100
2 100
100 1000

Sample Output 3

102
200
2000


program sample
    implicit none

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

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

    read(*,*) n
    allocate(a(n))
    read(*,*)a
    read(*,*)q
    !call msort(a)

    do i=1,q
        read(*,*)b,c
        do j=1,n
            if(a(j)==b)then
                a(j)=c
            end if
        end do
        write(*,*)sum(a)
    end do

    stop
end program sample


program abc171
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: n,i,j,cnt(0:30)=0
    integer(int32), allocatable:: a(:),b(:)

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

    do i=1,n
        print'(b0)', a(i)
    end do

    do i=1,n
        do j=0,30
            if (btest(a(i),j)) cnt(j)=cnt(j)+1
        end do
    end do

    do i=1,n
        do j=0,30
            if (mod(cnt(j),2) == 0 .neqv. btest(a(i),j)) then
                a(i)=ibclr(a(i),j)
            else
                a(i)=ibset(a(i),j)
            end if
        end do
    end do

    print'(*(i0,1x))', a(:)
end program abc171

Score : 500 points

Problem Statement

There are N Snuke Cats numbered 1, 2, \ldots, N, where N is even.

Each Snuke Cat wears a red scarf, on which his favorite non-negative integer is written.

Recently, they learned the operation called xor (exclusive OR).

What is xor?

For n non-negative integers x_1, x_2, \ldots, x_n, their xor, x_1~\textrm{xor}~x_2~\textrm{xor}~\ldots~\textrm{xor}~x_n is defined as follows:

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

For example, 3~\textrm{xor}~5 = 6.

They wanted to use this operation quickly, so each of them calculated the xor of the integers written on their scarfs except his scarf.

We know that the xor calculated by Snuke Cat i, that is, the xor of the integers written on the scarfs except the scarf of Snuke Cat i is a_i.
Using this information, restore the integer written on the scarf of each Snuke Cat.

Constraints

All values in input are integers.

2 \leq N \leq 200000

N is even.

0 \leq a_i \leq 10^9

There exists a combination of integers on the scarfs that is consistent with the given information.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 \ldots a_N

Output

Print a line containing N integers separated with space.

The i-th of the integers from the left should represent the integer written on the scarf of Snuke Cat i.

If there are multiple possible solutions, you may print any of them.

Sample Input 1

4
20 11 9 24

Sample Output 1

26 5 7 22

5~\textrm{xor}~7~\textrm{xor}~22 = 20

26~\textrm{xor}~7~\textrm{xor}~22 = 11

26~\textrm{xor}~5~\textrm{xor}~22 = 9

26~\textrm{xor}~5~\textrm{xor}~7 = 24

Thus, this output is consistent with the given information.


module module_swap
    use, intrinsic :: iso_fortran_env
    implicit none
    private
    public swap

    interface swap
        module procedure swap_scalar
    end interface

contains

    subroutine swap_scalar( lhs, rhs )
        class(*) :: lhs, rhs
        class(*), allocatable :: tmp, a, b

        allocate(a, source = lhs)
        allocate(b, source = rhs)

        call move_alloc(b, tmp)
        call move_alloc(a, b)
        call move_alloc(tmp, a)
        call bytes_to_anything(a, lhs)
        call bytes_to_anything(b, rhs)

    contains

        subroutine bytes_to_anything(bytes, anything)
            class(*) :: bytes
            class(*) :: anything

            select type(anything)
            type is ( character(*) )
                anything = transfer(bytes, anything)
            type is ( complex(real32) )
                anything = transfer(bytes, anything)
            type is ( complex(real64) )
                anything = transfer(bytes, anything)
            type is ( integer(int8) )
                anything = transfer(bytes, anything)
            type is ( integer(int16 ))
                anything = transfer(bytes, anything)
            type is ( integer(int32) )
                anything = transfer(bytes, anything)
            type is ( integer(int64) )
                anything = transfer(bytes, anything)
            type is ( real(real32) )
                anything = transfer(bytes, anything)
            type is ( real(real64) )
                anything = transfer(bytes, anything)
            type is ( real(real128) )
                anything = transfer(bytes, anything)
            class default
                stop 'bytes_to_anything(1) does not know about this type'
            end select

        end subroutine bytes_to_anything

    end subroutine swap_scalar

end module module_swap

module module_modint
    use, intrinsic :: iso_fortran_env
    use module_swap
    implicit none
    private
    public :: modint, set_modulus, modulo_utils, permutation, combination, homo

    type modint
        private
        integer(int64) :: val
    contains
        procedure :: n => get
        procedure :: add
        procedure :: sub
        procedure :: mul
        procedure :: inv
        procedure :: div
        procedure :: pow
        generic :: operator(+) => add
        generic :: operator(-) => sub
        generic :: operator(*) => mul
        generic :: operator(/) => div
        generic :: operator(**) => pow
    end type

    integer(int64) :: modulus = 1000000007
    type(modint), allocatable :: f(:), invf(:)

    interface modint
        module procedure :: init_modint
    end interface modint

    interface modulo_utils
        module procedure :: init_modulo_utils
    end interface modulo_utils

contains

    integer(int64) function mval(i) result(res)
        integer(int64), intent(in) :: i

        if ( (0_int64 <= i) .and. (i < modulus) ) then
            res = i
        else
            res = modulo(i, modulus)
        endif

    end function mval

    integer(int64) function to_int64(i) result(res)
        class(*), intent(in) :: i

        select type(i)
        type is (integer(int64))
            res = int(i, int64)
        type is (integer(int32))
            res = int(i, int64)
        type is (integer(int16))
            res = int(i, int64)
        type is (integer(int8))
            res = int(i, int64)
        end select

    end function to_int64

    subroutine set_modulus(m)
        class(*), intent(in) :: m

        select type(m)
        type is (modint)
            modulus = m%val
        class default
            modulus = to_int64(m)
        end select

    end subroutine set_modulus

    type(modint) function init_modint(i) result(res)
        class(*), intent(in) :: i

        select type(i)
        type is (modint)
            res%val = i%val
        class default
            res%val = mval( to_int64(i) )
        end select

    end function init_modint

    function get(this) result(res)
        class(modint), intent(in) :: this
        integer(int64) :: res

        res = this%val

    end function get

    type(modint) function add(this, other) result(res)
        class(modint), intent(in) :: this
        type(modint), intent(in) :: other

        res%val = mval( this%val + other%val )

    end function add

    type(modint) function sub(this, other) result(res)
        class(modint), intent(in) :: this
        type(modint), intent(in) :: other

        res%val = mval( this%val - other%val )

    end function sub

    type(modint) function mul(this, other) result(res)
        class(modint), intent(in) :: this
        type(modint), intent(in) :: other

        res%val = mval( this%val * other%val )

    end function mul

    type(modint) function div(this, other) result(res)
        class(modint), intent(in) :: this
        type(modint), intent(in) :: other

        res = this%mul( other%inv() )

    end function div

    type(modint) function inv(this) result(res)
        implicit none
        class(modint), intent(in) :: this
        integer(int64) :: a, b, u, v, t

        a = this%val
        b = modulus
        u = 1
        v = 0

        do while ( b /= 0 )
            t = a / b
            a = a - t * b
            call swap(a, b)
            u = u - t * v
            call swap(u, v)
        enddo

        res%val = modulo(u, modulus)

    end function inv

    type(modint) function pow(this, other) result(res)
        implicit none
        class(modint), intent(in) :: this
        class(*), intent(in) :: other
        integer(int64) :: n, k
        type(modint) :: i

        res%val = mval( to_int64(1) )
        i%val = this%val
        n = to_int64(other)

        do k = 1, abs(n)
            res = res%mul(i)
        enddo

        if ( n < 0) then
            res = modint(1) / res
        endif

    end function pow

    subroutine init_modulo_utils(i)
        class(*), intent(in) :: i
        integer(int64) :: k, n

        if ( allocated(f) ) deallocate(f)
        if ( allocated(invf) ) deallocate(invf)

        select type(i)
        type is (modint)
            n = i%val
        class default
            n = to_int64(i)
        end select

        allocate( f(0:n) )
        allocate( invf(0:n) )

        f(0) = modint(1)
        do k = 1, n
            f(k) = f(k-1) * modint(k)
        enddo

        invf(n) = f(n)%inv()
        do k = n, 1, -1
            invf(k-1) = invf(k) * modint(k)
        enddo

    end subroutine init_modulo_utils

    type(modint) function permutation(n, k)
        class(*), intent(in) :: n, k
        integer(int64) :: nt, kt

        select type(n)
        type is (integer(int64))
            nt = n
        class default
            nt = to_int64(n)
        end select

        select type(k)
        type is (integer(int64))
            kt = k
        class default
            kt = to_int64(k)
        end select

        if ( (kt > nt) .or. (nt < 0) .or. (kt < 0) ) return
        permutation = f(nt) * invf(nt-kt)

    end function permutation

    type(modint) function combination(n, k)
        class(*), intent(in) :: n, k
        integer(int64) :: nt, kt

        select type(n)
        type is (integer(int64))
            nt = n
        class default
            nt = to_int64(n)
        end select

        select type(k)
        type is (integer(int64))
            kt = k
        class default
            kt = to_int64(k)
        end select

        combination = permutation(nt, kt) * invf(kt)

    end function combination

    type(modint) function homo(n, k)
        class(*), intent(in) :: n, k
        integer(int64) :: nt, kt

        select type(n)
        type is (integer(int64))
            nt = n
        class default
            nt = to_int64(n)
        end select

        select type(k)
        type is (integer(int64))
            kt = k
        class default
            kt = to_int64(k)
        end select

        homo = modint(1)
        if ( (nt == 0) .and. (kt == 0) ) return
        homo = combination(nt+kt-1, kt)

    end function homo

end module module_modint

program main
    use,intrinsic :: iso_fortran_env
    use :: module_modint
    implicit none

    integer(int64) :: div, k, n, i
    character(10**6) :: s
    type(modint) :: ans

    div = 10**9 + 7
    ans = modint(0)

    call set_modulus(div)

    read *, k
    read *, s

    n = len_trim(s)
    call modulo_utils( n+k )

    do i = 0, k
        ans = ans + (modint(25) ** ( k-i )) * combination( n+k-i-1, n-1 ) * (modint(26) ** i)
    enddo

    print *, ans%n()

end program main

Score: 600 points

Problem Statement

How many strings can be obtained by applying the following operation on a string S exactly K times: "choose one lowercase English letter and insert it somewhere"?

The answer can be enormous, so print it modulo (10^9+7).

Constraints

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

S is a string of length between 1 and 10^6 (inclusive) consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

K
S

Output

Print the number of strings satisfying the condition, modulo (10^9+7).

Sample Input 1

5
oof

Sample Output 1

575111451

For example, we can obtain proofend, moonwolf, and onionpuf, while we cannot obtain oofsix, oofelevennn, voxafolt, or fooooooo.

Sample Input 2

37564
whydidyoudesertme

Sample Output 2

318008117


program shift
  implicit none
  integer(8), parameter :: p = 998244353
  character(300) :: s
  integer :: k, n, c(300) = 0, m = 0, i, j, x = 0
  integer(8) :: dp(0:300, 0:300) = 0
  integer(8) :: ans = 0
  read(*,*) s, k
  n = len_trim(s)
  do i = 1, n
    if (s(i:i) == '0') m = m + 1
  end do
  if (m == 0 .or. m == n) then
    write(*,'(i0)') 0
    stop
  end if
  m = m + 1
  j = m
  do i = n, 1, -1
    if (s(i:i) == '0') then
      j = j - 1
    else
      c(j) = c(j) + 1
    end if
  end do
  dp(m, 0) = 1
  x = c(m)
  do i = m - 1, 0, -1
    do j = x, 0, -1
      dp(i, j) = mod(sum(dp(i + 1, max(j - k, 0):j)), p)
    end do
    x = x + c(i)
  end do
  write(*,'(i0)') sum(dp(0, 1:k))
end program shift

Score : 800 points

Problem Statement

Given is a string S consisting of 0 and 1. Find the number of strings, modulo 998244353, that can result from applying the following operation on S between 0 and K times (inclusive):

Choose a pair of integers i, j (1\leq i < j\leq |S|) such that the i-th and j-th characters of S are 0 and 1, respectively. Remove the j-th character from S and insert it to the immediate left of the i-th character.

Constraints

1 \leq |S| \leq 300

0 \leq K \leq 10^9

S consists of 0 and 1.

Input

Input is given from Standard Input in the following format:

S K

Output

Find the number of strings, modulo 998244353, that can result from applying the operation on S between 0 and K times (inclusive).

Sample Input 1

0101 1

Sample Output 1

4

Four strings, 0101, 0110, 1001, and 1010, can result.

Sample Input 2

01100110 2

Sample Output 2

14

Sample Input 3

1101010010101101110111100011011111011000111101110101010010101010101 20

Sample Output 3

113434815


program main
     implicit none
     integer a, b, c, d, e
     read(*, *) a, b, c, d, e

     if(a == 0)then
          write(*, *) 1
     else if (b == 0) then
          write(*, *) 2
     else if (c == 0) then
          write(*, *) 3
     else if (d == 0) then
          write(*, *) 4
     else if (e == 0) then
          write(*, *) 5
     endif
     stop
end program main

Score : 100 points

Problem Statement

We have five variables x_1, x_2, x_3, x_4, and x_5.

The variable x_i was initially assigned a value of i.

Snuke chose one of these variables and assigned it 0.

You are given the values of the five variables after this assignment.

Find out which variable Snuke assigned 0.

Constraints

The values of x_1, x_2, x_3, x_4, and x_5 given as input are a possible outcome of the assignment by Snuke.

Input

Input is given from Standard Input in the following format:

x_1 x_2 x_3 x_4 x_5

Output

If the variable Snuke assigned 0 was x_i, print the integer i.

Sample Input 1

0 2 3 4 5

Sample Output 1

1

In this case, Snuke assigned 0 to x_1, so we should print 1.

Sample Input 2

1 2 0 4 5

Sample Output 2

3


program abc170a
   implicit none
   integer :: x(5)
   integer :: i

   read(*,*) x(:)

   do i = 1, 5
      if(x(i)==0) then
         print *, i
         stop
      endif
   enddo

end program abc170a


program main
  implicit none

  integer :: x(5)

  read(*,*) x

  write(*,*) 15 - sum(x)

end program main


program main
implicit none
integer i, j
if(j < 2*i .or. j > 4*i) then
     write(*, *) "No"
else if (ibits(j, 0, 1)==1) then
     write(*, *) "No"
else
     write(*, *) "Yes"
endif
end program main

Score : 200 points

Problem Statement

There are some animals in a garden. Each of them is a crane with two legs or a turtle with four legs.

Takahashi says: "there are X animals in total in the garden, and they have Y legs in total." Determine whether there is a combination of numbers of cranes and turtles in which this statement is correct.

Constraints

1 \leq X \leq 100

1 \leq Y \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

If there is a combination of numbers of cranes and turtles in which the statement is correct, print Yes; otherwise, print No.

Sample Input 1

3 8

Sample Output 1

Yes

The statement "there are 3 animals in total in the garden, and they have 8 legs in total" is correct if there are two cranes and one turtle. Thus, there is a combination of numbers of cranes and turtles in which the statement is correct.

Sample Input 2

2 100

Sample Output 2

No

There is no combination of numbers of cranes and turtles in which this statement is correct.

Sample Input 3

1 2

Sample Output 3

Yes

We also consider the case in which there are only cranes or only turtles.


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

    read(*,*) x,y

    a=2*x-y/2
    b=y/2-x
    if (mod(y,2)==1)then
        write(*,*)'No'
        stop
    end if
    if (b<0 .or. a<0)then
        write(*,*)'No'
        stop

    end if
    write(*,*)'Yes'

    stop
end program sample


program tsurukame
read*,ix,iy

do i=0,ix
 do j=0,ix-i
  if(2*i+4*j==iy)then
   print*,'yes'
   goto 30
  end if
 end do
end do
 print*,'no'
30 end program


program crane

 implicit none
 integer::x,y,n,jj,i,ans

 jj=0

 read*,x,y

 n=y/x+1

 do i=0,n
  ans=2*i+4*(x-i)
  if(ans==y) jj=jj+1
 end do

 if(jj==0) print*,'No'
 if(jj/=0) print*,'Yes'

end program crane


program main
	implicit none
	integer(8):: N,i,j,t,K,X
	integer,dimension(1:100)::A,B,C
	read *, X,N
    read(*,*) (A(i), i = 1, N)
	t=0
	k=0
	do j=1,N
	do i=1,N
		if((A(i)-X+t)==0)then
			t=t+1
			exit
		end if
	end do
	do i=1,N
		if((A(i)-X-k)==0)then
			k=k+1
			exit
		end if
	end do
	end do

	if(t==0.and.k==0)		print'(I0)',X

	if (t>k)t=k

	do i=1,N

		if((A(i)-X+t)/=0)then
		print'(I0)',X-t
		stop
		end if
		if((A(i)-X-t)/=0)then
		print'(I0)',X+t
		stop
		end if
	end do
end program main

Score : 300 points

Problem Statement

Given are an integer X and an integer sequence of length N: p_1, \ldots, p_N.

Among the integers not contained in the sequence p_1, \ldots, p_N (not necessarily positive), find the integer nearest to X, that is, find the integer whose absolute difference with X is the minimum. If there are multiple such integers, report the smallest such integer.

Constraints

1 \leq X \leq 100

0 \leq N \leq 100

1 \leq p_i \leq 100

p_1, \ldots, p_N are all distinct.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X N
p_1 ... p_N

Output

Print the answer.

Sample Input 1

6 5
4 7 10 6 5

Sample Output 1

8

Among the integers not contained in the sequence 4, 7, 10, 6, 5, the one nearest to 6 is 8.

Sample Input 2

10 5
4 7 10 6 5

Sample Output 2

9

Among the integers not contained in the sequence 4, 7, 10, 6, 5, the ones nearest to 10 are 9 and 11. We should print the smaller one, 9.

Sample Input 3

100 0

Sample Output 3

100

When N = 0, the second line in the input will be empty. Also, as seen here, X itself can be the answer.


program TMPC2020A
    implicit none
    character(20)S
    read*,S
    print'(A)',S(1:3)
end program TMPC2020A

Score : 100 points

Problem Statement

When you asked some guy in your class his name, he called himself S, where S is a string of length between 3 and 20 (inclusive) consisting of lowercase English letters.
You have decided to choose some three consecutive characters from S and make it his nickname. Print a string that is a valid nickname for him.

Constraints

3 \leq |S| \leq 20

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

Print your answer.

Sample Input 1

takahashi

Sample Output 1

tak

Sample Input 2

naohiro

Sample Output 2

nao


program prob37
    implicit none
    character(3) ::s
    read(*,*) s
    write(*,'(a)') s
    stop
contains
end program prob37


program answer
  implicit none

  character :: S(20)

  read(*,*) S
  write(*,*) S(1),S(2),S(3)

  stop
 end program answer


program sample
	implicit none
    character(len=20) :: name

    read(*,*) name
    write(*,*) name(1:3)

    stop
end program sample


program answer
  implicit none

  character(len = 20) :: S

  read(*,*) S

  write(*,*) S(1:3)

  stop
 end program answer


program tag
  implicit none
  integer(8) :: a, b, v, w, t, i, j, a1, b1

  read(*,*) a, v
  read(*,*) b, w
  read(*,*) t

  do i = 1, t
     a1 = a + i*v
     if (mod(a1-b,w) == 0 .and.mod((a1-b)/w,2)==mod(i,2) ) then
        write(*,*) 'YES'
        stop
     end if
  end do

  write(*,*) 'NO'
  stop
end program tag

Score : 200 points

Problem Statement

Two children are playing tag on a number line. (In the game of tag, the child called "it" tries to catch the other child.) The child who is "it" is now at coordinate A, and he can travel the distance of V per second.
The other child is now at coordinate B, and she can travel the distance of W per second.

He can catch her when his coordinate is the same as hers.
Determine whether he can catch her within T seconds (including exactly T seconds later).
We assume that both children move optimally.

Constraints

-10^9 \leq A,B \leq 10^9

1 \leq V,W \leq 10^9

1 \leq T \leq 10^9

A \neq B

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A V
B W
T

Output

If "it" can catch the other child, print YES; otherwise, print NO.

Sample Input 1

1 2
3 1
3

Sample Output 1

YES

Sample Input 2

1 2
3 2
3

Sample Output 2

NO

Sample Input 3

1 2
3 3
3

Sample Output 3

NO


      program tokyoA
      implicit none

      integer(8) :: i,n,k,l,m,j,d,p
      integer(8),allocatable::B(:),A(:)

      read(*,*)n,k
      allocate(A(n),B(n))

      read(*,*)A(:)

      do i =1,k
        do j = 1,n
        B(j) =0
        end do
        do m =1,n
            d = A(m)
            B(m) =B(m)+1
            do l = 1,d
                if ((m+l).le.n)then
                    B(m+l) =B(m+l)+1
                endif
                if (m.gt.l)then
                    B(m-l) = B(m-l)+1
                endif
            end do
        end do
        do p = 1,n
            A(p) = B(p)
        end do

      end do

      write(*,*)A

      end program

Score : 500 points

Problem Statement

We have N bulbs arranged on a number line, numbered 1 to N from left to right.
Bulb i is at coordinate i.

Each bulb has a non-negative integer parameter called intensity.
When there is a bulb of intensity d at coordinate x, the bulb illuminates the segment from coordinate x-d-0.5 to x+d+0.5.
Initially, the intensity of Bulb i is A_i. We will now do the following operation K times in a row:

For each integer i between 1 and N (inclusive), let B_i be the number of bulbs illuminating coordinate i. Then, change the intensity of each bulb i to B_i.

Find the intensity of each bulb after the K operations.

Constraints

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

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

0 \leq A_i \leq N

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \ldots A_N

Output

Print the intensity A{'}_i of each bulb i after the K operations to Standard Output in the following format:

A{'}_1 A{'}_2 \ldots A{'}_N

Sample Input 1

5 1
1 0 0 1 0

Sample Output 1

1 2 2 1 2

Initially, only Bulb 1 illuminates coordinate 1, so the intensity of Bulb 1 becomes 1 after the operation.
Similarly, the bulbs initially illuminating coordinate 2 are Bulb 1 and 2, so the intensity of Bulb 2 becomes 2.

Sample Input 2

5 2
1 0 0 1 0

Sample Output 2

3 3 4 4 3


program main
    implicit none
    integer n,k
    integer i,j,l, tau, sum
    integer, allocatable, dimension(:) :: a, b

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

    sum = 0
    do i = 1,n
        sum = sum + a(i)
    end do

    if (sum /= 0) then
        tau = 2*int(log(real(n))) + 10

        if(k > tau) then
            a = n
        else
            do l = 1,k

                b = 0
                do i = 1,n
                    do j = i-a(i), i+a(i)
                        if(j >= 1 .and. j <= n) then
                            b(j) = b(j) + 1
                        end if
                    end do
                end do

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

            end do
        end if

    else
        a = 0
    end if

    write(*,*) a

end program


program main
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: n,k,i,j,l,r,jj
    integer(int64), allocatable:: a(:), na(:)
    logical:: same

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

    ! if (k >= 100) then
    !     a(:) = n
    !     print'(*(i0,1x))', a(:)
    !     stop
    ! end if

    do i=1,k
        na(:) = 0
        same = .true.
        do j=1,n
            l = max(j-a(j),1_8)
            r = min(j+a(j),n)
            na(l:r)=na(l:r)+1
        end do
        a(:) = na(:)
    end do
    ! print'(i0)', i
    print'(*(i0,1x))', na(:)
end program main


program range_set
  implicit none
  integer(8), parameter :: modu = 1000000007
  integer :: n, a, b, i, j
  integer(8) :: x = 1, f(0:5000, 2) = 0, g(0:5000, 2) = 0
  read(*,*) n, a, b
  if (a > b) then
    i = a
    a = b
    b = i
  end if
  g(1, 2) = 1
  do i = 2, b - 1
    g(i, 2) = mod(sum(g(i - 1, :)), modu)
    g(i, 1) = mod(sum(g(1:i - a, 2)), modu)
  end do
  f(0, :) = 1
  do i = 1, n
    x = mod(x * 2, modu)
    f(i, 1) = mod(sum(f(max(i - a + 1, 0):i - 1, 2)), modu)
    do j = 1, min(b - 1, i)
      if (i == n) f(i, 1) = mod(f(i, 1) + f(i - j, 1) * g(j, 1), modu)
      f(i, 2) = f(i, 2) + merge(sum(g(j, :)), mod(f(i - j, 1) * g(j, 2), modu), j == i)
    end do
    f(i, 2) = mod(f(i, 2), modu)
  end do
  write(*,'(i0)') modulo(x - sum(f(n, :)), modu)
end program range_set

Score : 800 points

Problem Statement

Snuke has a string x of length N.
Initially, every character in x is 0.

Snuke can do the following two operations any number of times in any order:

Choose A consecutive characters in x and replace each of them with 0.

Choose B consecutive characters in x and replace each of them with 1.

Find the number of different strings that x can be after Snuke finishes doing operations.
This count can be enormous, so compute it modulo (10^9+7).

Constraints

1 \leq N \leq 5000

1 \leq A,B \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print the number of different strings that x can be after Snuke finishes doing operations, modulo (10^9+7).

Sample Input 1

4 2 3

Sample Output 1

11

For example, x can be 0011 or 1111 in the end, but cannot be 0110.

Sample Input 2

10 7 2

Sample Output 2

533

Sample Input 3

1000 100 10

Sample Output 3

828178524