program rotation_matching
  implicit none
  integer :: n, m, i = 0, j, cnt = 0, maxm, d
  read(*,*) n, m
  maxm = (n-1)/2
  d = mod(n,2)
  do
    write(*,'(i0,x,i0)') i+1, maxm+1-i
    cnt = cnt+1
    if (cnt == m) stop
    write(*,'(i0,x,i0)') maxm+3+i-d, n-i
    cnt = cnt+1
    if (cnt == m) stop
    i = i+1
  end do
end program rotation_matching

Score : 500 points

Problem Statement

You are going to hold a competition of one-to-one game called AtCoder Janken. (Janken is the Japanese name for Rock-paper-scissors.)
N players will participate in this competition, and they are given distinct integers from 1 through N.
The arena has M playing fields for two players. You need to assign each playing field two distinct integers between 1 and N (inclusive).
You cannot assign the same integer to multiple playing fields.
The competition consists of N rounds, each of which proceeds as follows:

For each player, if there is a playing field that is assigned the player's integer, the player goes to that field and fight the other player who comes there.

Then, each player adds 1 to its integer. If it becomes N+1, change it to 1.

You want to ensure that no player fights the same opponent more than once during the N rounds.
Print an assignment of integers to the playing fields satisfying this condition.
It can be proved that such an assignment always exists under the constraints given.

Constraints

1 \leq M

M \times 2 +1 \leq N \leq 200000

Input

Input is given from Standard Input in the following format:

N M

Output

Print M lines in the format below.
The i-th line should contain the two integers a_i and b_i assigned to the i-th playing field.

a_1 b_1
a_2 b_2
:
a_M b_M

Sample Input 1

4 1

Sample Output 1

2 3

Let us call the four players A, B, C, and D, and assume that they are initially given the integers 1, 2, 3, and 4, respectively.

The 1-st round is fought by B and C, who has the integers 2 and 3, respectively. After this round, A, B, C, and D have the integers 2, 3, 4, and 1, respectively.

The 2-nd round is fought by A and B, who has the integers 2 and 3, respectively. After this round, A, B, C, and D have the integers 3, 4, 1, and 2, respectively.

The 3-rd round is fought by D and A, who has the integers 2 and 3, respectively. After this round, A, B, C, and D have the integers 4, 1, 2, and 3, respectively.

The 4-th round is fought by C and D, who has the integers 2 and 3, respectively. After this round, A, B, C, and D have the integers 1, 2, 3, and 4, respectively.

No player fights the same opponent more than once during the four rounds, so this solution will be accepted.

Sample Input 2

7 3

Sample Output 2

1 6
2 5
3 4


program sample
implicit none
integer A,B,C,D
READ(*,*)A,B,C,D
10 CONTINUE
DO WHILE (C>0.OR.A>0)
C=C-B
If (C<=0) then
   WRITE(*,*) "Yes"
END IF
A=A-D
IF(A<=0.AND.C>0) then
   WRITE(*,*) "No"
END IF
END DO
stop
end program sample

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

Score : 100 points

Problem Statement

There are S sheep and W wolves.

If the number of wolves is greater than or equal to that of sheep, the wolves will attack the sheep.

If the wolves will attack the sheep, print unsafe; otherwise, print safe.

Constraints

1 \leq S \leq 100

1 \leq W \leq 100

Input

Input is given from Standard Input in the following format:

S W

Output

If the wolves will attack the sheep, print unsafe; otherwise, print safe.

Sample Input 1

4 5

Sample Output 1

unsafe

There are four sheep and five wolves. The number of wolves is not less than that of sheep, so they will attack them.

Sample Input 2

100 2

Sample Output 2

safe

Many a sheep drive away two wolves.

Sample Input 3

10 10

Sample Output 3

unsafe

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 a164

IMPLICIT NONE
INTEGER:: S, W

READ *, S, W

IF (S > W) THEN
	PRINT *, "safe"
    ELSE
    PRINT *, "unsafe"

END IF

END PROGRAM a164


   program abc164a
      implicit none
      integer::s,w

         read *, s,w
         if (w>=s) then
            print *, 'unsafe'
         else
            print *, 'safe'
         end if

   end program abc164a


module ch
  implicit none
  type char
     character(10)      :: chara
  end type char
  type(char),allocatable :: namec(:)
  type(char),allocatable :: namecmax(:)
end module ch
program main

  use ch, only : namec, namecmax

  implicit none
  integer :: n
  integer, allocatable :: s(:)
  integer :: i,j,k, maxvals

  read(*,*) n
  allocate( s(n) )
  allocate( namec(n) )
  do i = 1, n
    read(*,*) namec(i)%chara
  end do

  call quicksort1( 1, n )

  j = 0
  do i = 1, n
    if( namec(i)%chara /= namec(i+1)%chara ) then
      j = j + 1
    end if
  end do

  print*,j

end program main
recursive subroutine quicksort1( first, last )

  use ch, only : namec, namecmax

  implicit none
  integer,intent(in)::first,last
  !
  ! Original
  ! https://gist.github.com/t-nissie/479f0f16966925fa29ea
  !
  character(100)::x,t
  integer::i,j

  x = namec((first+last)/2)%chara
  i = first
  j = last
  do
     do while ( LGT( x, namec(i)%chara ) )
        i=i+1
     end do
     do while ( LGT( namec(j)%chara, x ) )
        j=j-1
     end do
     if (i >= j) exit
     t = namec(i)%chara
     namec(i)%chara = namec(j)%chara
     namec(j)%chara = t
     i=i+1
     j=j-1
  end do
  if (first < i-1) call quicksort1( first, i-1  )
  if (j+1 < last)  call quicksort1( j+1  , last )

  return

end subroutine quicksort1

Score : 300 points

Problem Statement

You drew lottery N times. In the i-th draw, you got an item of the kind represented by a string S_i.

How many kinds of items did you get?

Constraints

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

S_i consists of lowercase English letters and has a length between 1 and 10 (inclusive).

Input

Input is given from Standard Input in the following format:

N
S_1
:
S_N

Output

Print the number of kinds of items you got.

Sample Input 1

3
apple
orange
apple

Sample Output 1

2

You got two kinds of items: apple and orange.

Sample Input 2

5
grape
grape
grape
grape
grape

Sample Output 2

1

Sample Input 3

4
aaaa
a
aaa
aa

Sample Output 3

4


program main
    implicit none
    integer n
    character(len=10),allocatable, dimension(:) :: s
    integer i,j,k,l,sum,minus,up

    type mytype
        integer :: number
        character(len=10),dimension(200000) :: word
    end type mytype

    type(mytype) :: alpha(26,10)

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

    do i = 1,26
        do j = 1,10
            alpha(i,j)%number = 0
        end do
    end do

    do i = 1,n
        read(*,*) s(i)
        if (s(i)(1:1) == "a") then
            k = 1
        else if (s(i)(1:1) == "b") then
            k = 2
        else if (s(i)(1:1) == "c") then
            k = 3
        else if (s(i)(1:1) == "d") then
            k = 4
        else if (s(i)(1:1) == "e") then
            k = 5
        else if (s(i)(1:1) == "f") then
            k = 6
        else if (s(i)(1:1) == "g") then
            k = 7
        else if (s(i)(1:1) == "h") then
            k = 8
        else if (s(i)(1:1) == "i") then
            k = 9
        else if (s(i)(1:1) == "j") then
            k = 10
        else if (s(i)(1:1) == "k") then
            k = 11
        else if (s(i)(1:1) == "l") then
            k = 12
        else if (s(i)(1:1) == "m") then
            k = 13
        else if (s(i)(1:1) == "n") then
            k = 14
        else if (s(i)(1:1) == "o") then
            k = 15
        else if (s(i)(1:1) == "p") then
            k = 16
        else if (s(i)(1:1) == "q") then
            k = 17
        else if (s(i)(1:1) == "r") then
            k = 18
        else if (s(i)(1:1) == "s") then
            k = 19
        else if (s(i)(1:1) == "t") then
            k = 20
        else if (s(i)(1:1) == "u") then
            k = 21
        else if (s(i)(1:1) == "v") then
            k = 22
        else if (s(i)(1:1) == "w") then
            k = 23
        else if (s(i)(1:1) == "x") then
            k = 24
        else if (s(i)(1:1) == "y") then
            k = 25
        else if (s(i)(1:1) == "z") then
            k = 26
        end if
        l = len_trim(s(i))
        alpha(k,l)%number = alpha(k,l)%number + 1
        alpha(k,l)%word(alpha(k,l)%number) = s(i)
    end do

    sum = n

    do i = 1,26
        do j = 1,10

            minus = 0
            up = alpha(i,j)%number
            if (up <= 1) then
                go to 20
            end if
            do k = 1,up
                if (alpha(i,j)%word(k) == "0") then
                    go to 10
                end if
                do l = k + 1, up
                    if(alpha(i,j)%word(k) == alpha(i,j)%word(l)) then
                        minus = minus + 1
                        alpha(i,j)%word(l) = "0"
                    end if
                end do
                10 continue
            end do
            20 continue

            sum = sum - minus

        end do
    end do

    write(*,*) sum

end program


program main
    implicit none
    integer n
    character(len=10),allocatable, dimension(:) :: s
    integer i,j,k,l,sum,minus,up

    type mytype
        integer :: number
        character(len=10),dimension(20000) :: word
    end type mytype

    type(mytype) :: alpha(26,10)

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

    do i = 1,26
        do j = 1,10
            alpha(i,j)%number = 0
        end do
    end do

    do i = 1,n
        read(*,*) s(i)
        if (s(i)(1:1) == "a") then
            k = 1
        else if (s(i)(1:1) == "b") then
            k = 2
        else if (s(i)(1:1) == "c") then
            k = 3
        else if (s(i)(1:1) == "d") then
            k = 4
        else if (s(i)(1:1) == "e") then
            k = 5
        else if (s(i)(1:1) == "f") then
            k = 6
        else if (s(i)(1:1) == "g") then
            k = 7
        else if (s(i)(1:1) == "h") then
            k = 8
        else if (s(i)(1:1) == "i") then
            k = 9
        else if (s(i)(1:1) == "j") then
            k = 10
        else if (s(i)(1:1) == "k") then
            k = 11
        else if (s(i)(1:1) == "l") then
            k = 12
        else if (s(i)(1:1) == "m") then
            k = 13
        else if (s(i)(1:1) == "n") then
            k = 14
        else if (s(i)(1:1) == "o") then
            k = 15
        else if (s(i)(1:1) == "p") then
            k = 16
        else if (s(i)(1:1) == "q") then
            k = 17
        else if (s(i)(1:1) == "r") then
            k = 18
        else if (s(i)(1:1) == "s") then
            k = 19
        else if (s(i)(1:1) == "t") then
            k = 20
        else if (s(i)(1:1) == "u") then
            k = 21
        else if (s(i)(1:1) == "v") then
            k = 22
        else if (s(i)(1:1) == "w") then
            k = 23
        else if (s(i)(1:1) == "x") then
            k = 24
        else if (s(i)(1:1) == "y") then
            k = 25
        else if (s(i)(1:1) == "z") then
            k = 26
        end if
        l = len_trim(s(i))
        alpha(k,l)%number = alpha(k,l)%number + 1
        alpha(k,l)%word(alpha(k,l)%number) = s(i)
    end do

    sum = n

    do i = 1,26
        do j = 1,10

            minus = 0
            up = alpha(i,j)%number
            if (up <= 1) then
                go to 20
            end if
            do k = 1,up
                if (alpha(i,j)%word(k) == "0") then
                    go to 10
                end if
                do l = k + 1, up
                    if(alpha(i,j)%word(k) == alpha(i,j)%word(l)) then
                        minus = minus + 1
                        alpha(i,j)%word(l) = "0"
                    end if
                end do
                10 continue
            end do
            20 continue

            sum = sum - minus

        end do
    end do

    write(*,*) sum

end program


program main
    implicit none
    integer n
    character(len=10),allocatable, dimension(:) :: s
    integer i,j,k,l,sum,minus,up

    type mytype
        integer :: number
        character(len=10),dimension(1000) :: word
    end type mytype

    type(mytype) :: alpha(26,10)

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

    do i = 1,26
        do j = 1,10
            alpha(i,j)%number = 0
        end do
    end do

    do i = 1,n
        read(*,*) s(i)
        if (s(i)(1:1) == "a") then
            k = 1
        else if (s(i)(1:1) == "b") then
            k = 2
        else if (s(i)(1:1) == "c") then
            k = 3
        else if (s(i)(1:1) == "d") then
            k = 4
        else if (s(i)(1:1) == "e") then
            k = 5
        else if (s(i)(1:1) == "f") then
            k = 6
        else if (s(i)(1:1) == "g") then
            k = 7
        else if (s(i)(1:1) == "h") then
            k = 8
        else if (s(i)(1:1) == "i") then
            k = 9
        else if (s(i)(1:1) == "j") then
            k = 10
        else if (s(i)(1:1) == "k") then
            k = 11
        else if (s(i)(1:1) == "l") then
            k = 12
        else if (s(i)(1:1) == "m") then
            k = 13
        else if (s(i)(1:1) == "n") then
            k = 14
        else if (s(i)(1:1) == "o") then
            k = 15
        else if (s(i)(1:1) == "p") then
            k = 16
        else if (s(i)(1:1) == "q") then
            k = 17
        else if (s(i)(1:1) == "r") then
            k = 18
        else if (s(i)(1:1) == "s") then
            k = 19
        else if (s(i)(1:1) == "t") then
            k = 20
        else if (s(i)(1:1) == "u") then
            k = 21
        else if (s(i)(1:1) == "v") then
            k = 22
        else if (s(i)(1:1) == "w") then
            k = 23
        else if (s(i)(1:1) == "x") then
            k = 24
        else if (s(i)(1:1) == "y") then
            k = 25
        else if (s(i)(1:1) == "z") then
            k = 26
        end if
        l = len_trim(s(i))
        alpha(k,l)%number = alpha(k,l)%number + 1
        alpha(k,l)%word(alpha(k,l)%number) = s(i)
    end do

    sum = n

    do i = 1,26
        do j = 1,10

            minus = 0
            up = alpha(i,j)%number
            if (up == 1) then
                go to 20
            end if
            do k = 1,up
                if (alpha(i,j)%word(k) == "0") then
                    go to 10
                end if
                do l = k + 1, up
                    if(alpha(i,j)%word(k) == alpha(i,j)%word(l)) then
                        minus = minus + 1
                        alpha(i,j)%word(l) = "0"
                    end if
                end do
                10 continue
            end do
            20 continue

            sum = sum - minus

        end do
    end do

    write(*,*) sum

end program


   program abc164c
      implicit none
      integer::n
      character(10),allocatable::s1(:),s2(:) !元の配列、ソート後の配列
      integer::i,j,count
      integer,parameter::p=5

         read *, n
         allocate(s1(n),s2(n))
         do i=1,n
            read *,s1(i)
         end do

         do i=1,p
            call sort_quick(n, s1, s2)
         end do

         count=1
         do i=1,n-1
            if (s2(i)==s2(i+1)) cycle
            count=count+1
         end do

         print *, count
         stop

   contains
      subroutine sort_quick(num, a, b)
         implicit none
         integer, intent(in)  :: num
         character(10), intent(in):: a(num)
         character(10), intent(out) :: b(num)
         integer:: i, j
         character(10)::w

         ! 配列コピー
            b(:)=a(:)

         ! ソート処理
            call quick(1, num, num, b)
      end subroutine sort_quick

      recursive subroutine quick(left, right, num, a)
         implicit none
         integer, intent(in)    :: left, right, num
         character(10), intent(inout) :: a(num)
         integer :: i, j
         character(10)::s, w

            if (left >= right) return

         ! 最左項を軸に. 軸より小さいグループ. 軸より大きいグループ.
            s = a(left)
            i = left
            j = right + 1
            do
               i = i + 1
               do while (a(i) < s)
                  i = i + 1
               end do
               j = j - 1
               do while (a(j) > s)
                  j = j - 1
               end do
               if (i >= j) exit
           ! スワップ
               w    = a(i)
               a(i) = a(j)
               a(j) = w
            end do

        ! 軸を正しい位置に挿入
            a(left) = a(j)
            a(j)    = s
            call quick(left,  j - 1, num, a)  ! 左部分列に対する再帰呼び出し
            call quick(j + 1, right, num, a)  ! 右部分列に対する再帰呼び出し
      end subroutine quick

   end program abc164c


program main
    implicit none
    integer n
    character(len=10),allocatable, dimension(:) :: s
    integer i,j,c,num,minus,up
    read(*,*) n
    allocate(s(n))

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

    num = n
    up = n

    do i = 1,n
        minus = 0
        if(i == up) exit
        do j = i+1,up
            if(s(i) == s(j)) then
                num = num - 1
                minus = minus + 1
            end if
            s(j) = s(j+minus)
        end do
        up = up - minus
    end do

    write(*,*) num

end program


program main
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: n,i,j,ans
    character(10),allocatable:: s(:)

    read*, n
    allocate(s(n))
    do i=1,n
        read*, s(i)
    end do
    call character_sort(s,1,n)
    ans=1
    do i=2,n
        if (s(i) /= s(i-1)) ans=ans+1
    end do
    print*, ans

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

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

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

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

        allocate(tmp(lst-fst+1))

        li=fst
        ri=mdl+1
        ti=1

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

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

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

    subroutine swap(x,y)
        character(10),intent(inout):: x,y
        character(10):: tmp
        tmp = x
        x = y
        y = tmp
    end subroutine
end program


program main

  implicit none
  integer(8) :: n, m
  integer(8),allocatable :: a(:)

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

  if ( sum(a) > n ) then
    print*, "-1"
  else
    print*, n - sum( a )
  end if

end program main

Score : 200 points

Problem Statement

Takahashi has N days of summer vacation.

His teacher gave him M summer assignments. It will take A_i days for him to do the i-th assignment.

He cannot do multiple assignments on the same day, or hang out on a day he does an assignment.

What is the maximum number of days Takahashi can hang out during the vacation if he finishes all the assignments during this vacation?

If Takahashi cannot finish all the assignments during the vacation, print -1 instead.

Constraints

1 \leq N \leq 10^6

1 \leq M \leq 10^4

1 \leq A_i \leq 10^4

Input

Input is given from Standard Input in the following format:

N M
A_1 ... A_M

Output

Print the maximum number of days Takahashi can hang out during the vacation, or -1.

Sample Input 1

41 2
5 6

Sample Output 1

30

For example, he can do the first assignment on the first 5 days, hang out on the next 30 days, and do the second assignment on the last 6 days of the vacation. In this way, he can safely spend 30 days hanging out.

Sample Input 2

10 2
5 6

Sample Output 2

-1

He cannot finish his assignments.

Sample Input 3

11 2
5 6

Sample Output 3

0

He can finish his assignments, but he will have no time to hang out.

Sample Input 4

314 15
9 26 5 35 8 9 79 3 23 8 46 2 6 43 3

Sample Output 4

9


program main
    implicit none
    integer(8) N, i
    integer(8), allocatable :: A(:), boss(:)
    read *, N
    allocate(A(N))
    allocate(boss(N - 1))
    read *, boss
    do i = 1, N - 1
        A(boss(i)) = A(boss(i)) + 1
    end do
    do i = 1, N
        print '(I0)', A(i)
    end do
    stop
contains

end program main

Score : 300 points

Problem Statement

A company has N members, who are assigned ID numbers 1, ..., N.

Every member, except the member numbered 1, has exactly one immediate boss with a smaller ID number.

When a person X is the immediate boss of a person Y, the person Y is said to be an immediate subordinate of the person X.

You are given the information that the immediate boss of the member numbered i is the member numbered A_i. For each member, find how many immediate subordinates it has.

Constraints

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

1 \leq A_i < i

Input

Input is given from Standard Input in the following format:

N
A_2 ... A_N

Output

For each of the members numbered 1, 2, ..., N, print the number of immediate subordinates it has, in its own line.

Sample Input 1

5
1 1 2 2

Sample Output 1

2
2
0
0
0

The member numbered 1 has two immediate subordinates: the members numbered 2 and 3.

The member numbered 2 has two immediate subordinates: the members numbered 4 and 5.

The members numbered 3, 4, and 5 do not have immediate subordinates.

Sample Input 2

10
1 1 1 1 1 1 1 1 1

Sample Output 2

9
0
0
0
0
0
0
0
0
0

Sample Input 3

7
1 2 3 4 5 6

Sample Output 3

1
1
1
1
1
1
0


program main
    implicit none
    integer(8) N, M, i, sum
    integer(8), allocatable :: A(:)
    read *, N, M
    sum = 0
    allocate(A(M))
    read *, A
    do i = 1, M
        sum = sum + A(i)
    end do
    if (sum > N) then
        print *, -1
    else
        print '(I0)', N - sum
    end if
    deallocate(A)
    stop
contains

end program main


program ABC163_C
  integer,allocatable::A(:),B(:)
  read(*,*)N
  allocate(A(2:N),B(N))
  B=0
  read(*,*)A
  do i=2,N
     B(A(i))=B(A(i))+1
  end do
  do i=1,N
     write(*,*)B(i)
  end do
end program ABC163_C


program abc163c
   implicit none
   integer :: N, i, j, count
   integer, allocatable :: A(:), numOfBuka(:)

   read(*,*) N
   allocate(A(N), numOfBuka(N))
   read(*,*) A(2:N)

   do i = 1, N
      count = 0
      do j = 1, N
         if (A(j)==i) then
            count = count + 1
         end if
      end do
      numOfBuka(i) = count
   enddo

   do i = 1, N
      print *, numOfBuka(i)
   end do

end program abc163c


program sum_of_large_numbers
  implicit none
  integer(8), parameter :: m = 1000000007_8, h = (m+1)/2, t = (m+1)/3, s = mod(h*t,m)
  integer(8) :: n, k, i
  read(*,*) n, k
  write(*,'(i0)') modulo(x(n+1)-x(k-1),m)
contains
  integer(8) function f(i)
    integer(8), intent(in) :: i
    f = mod(mod(h*i,m)*(i+1),m)
  end
  integer(8) function g(i)
    integer(8), intent(in) :: i
    g = mod(mod(s*i,m)*mod((i+1)*(i*2+1),m),m)
  end
  integer(8) function x(i)
    integer(8), intent(in) :: i
    x = mod((n+1)*f(i)-g(i)+i,m)
  end
end program sum_of_large_numbers


program main

  implicit none
  integer(8) :: n
  integer(8) :: i,j,k,l,m,ans
  integer(8), allocatable :: mm(:), ll(:)
  integer(8) :: tmp, summ

  read(*,*) n, k
  allocate( mm(k:n+1) )
  allocate( ll(k:n+1) )

  tmp = 0
  do j = 0, k - 2
    tmp = tmp + j
  end do
  summ = tmp
  do j = k, n + 1
   summ = summ + j - 1
    mm(j) = mm(j) + summ
  end do

  tmp = 0
  do j = n-k+1, n
    tmp = tmp + j
  end do
  summ = tmp
  tmp  = n-k+1
  do j = k, n+1
   ll(j) = summ
    tmp  = tmp - 1
    summ = summ + tmp
  end do

  ans = 0
  do i = k, n + 1

    !m = 0
    !do j = 0, i - 1
    !  m = m + j
    !end do
    !l = 0
    !do j = n-i+1, n
    !  l = l + j
    !end do

    ans = ans + ll(i) - mm(i) + 1
    !ans = ans + l - mm(i) + 1

  end do
  print*, mod( ans, 10**9+7 )

end program main


module mod_two_dim_merge_sort
  implicit none
  integer, private, parameter :: intkind = 4
contains
  logical function less(a, b) result(res)
    integer(intkind), intent(in) :: a(:), b(:)
    res = a(1) > b(1) .or. (a(1) == b(1) .and. a(2) >= b(2))
  end
  subroutine merge_sort(a)
    integer(intkind), 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(intkind), intent(inout) :: a1(:, :), a2(:, :)
    integer(intkind) :: 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 (less(a1(i1, :), a2(i2, :))) 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
end module mod_two_dim_merge_sort
program sum_of_large_numbers
  use mod_two_dim_merge_sort
  implicit none
  integer :: n, a(2000, 2) = 0, i, j
  integer(8) :: dp(0:2000, 0:2000) = 0, x = 0
  read(*,*) n
  read(*,*) a(1:n, 1)
  a(1:n, 2) = [(i, i = 1, n)]
  call merge_sort(a(1:n, :))
  do i = 0, n
    do j = 0, n - i
      if (i > 0) then
        dp(i, j) = max(dp(i, j), &
        dp(i - 1, j) + a(i + j, 1) * abs(a(i + j, 2) - i))
      end if
      if (j > 0) then
        dp(i, j) = max(dp(i, j), &
        dp(i, j - 1) + a(i + j, 1) * abs(a(i + j, 2) - (n - j + 1)))
      end if
    end do
  end do
  do i = 0, n
    x = max(x, dp(i, n - i))
  end do
  write(*,'(i0)') x
end program sum_of_large_numbers

Score : 500 points

Problem Statement

There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i.

You can rearrange these children just one time in any order you like.

When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points.

Find the maximum total happiness points the children can earn.

Constraints

2 \leq N \leq 2000

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum total happiness points the children can earn.

Sample Input 1

4
1 3 4 2

Sample Output 1

20

If we move the 1-st child from the left to the 3-rd position from the left, the 2-nd child to the 4-th position, the 3-rd child to the 1-st position, and the 4-th child to the 2-nd position, the children earns 1 \times |1-3|+3 \times |2-4|+4 \times |3-1|+2 \times |4-2|=20 happiness points in total.

Sample Input 2

6
5 5 6 1 1 1

Sample Output 2

58

Sample Input 3

6
8 6 9 1 2 1

Sample Output 3

85


program main
  implicit none
  type pairtype
    integer :: v, k
  end type pairtype
  integer :: n, i, j, pos
  integer, allocatable :: a(:)
  integer(8), allocatable :: dp(:),  dppre(:)
  type(pairtype), allocatable :: a1(:), a2(:)
  read *, n
  allocate (a(n), a1(n), a2(n), dp(0:n), dppre(0:n))
  read *, a
  do i = 1, n
    a1(i) = pairtype(a(i), i)
  end do
  call quicksort(a1)

  dp(:) = -4 * 10_8 ** 18
  dp(0) = 0
  do i = n, 1, -1
    dppre = dp

    do j = 0, n - i
      ! 左に入れる
      pos = j + 1
      dp(j + 1) = max(dp(j + 1), dppre(j) + (a1(i) % v) * abs(a1(i) % k - pos))

      ! 右に入れる
      pos = i + j
      dp(j) = max(dp(j), dppre(j) + (a1(i) % v) * abs(a1(i) % k - pos))
    end do
  end do

  print "(i0)", maxval(dp)

  contains

  integer(8) function score(a, n)
    integer, intent(in) :: n
    type(pairtype), intent(in) :: a(n)
    integer :: i
    score = 0
    do i = 1, n
      score = score + abs(a(i) % k - i) * a(i) % v
    end do
  end function score

  recursive subroutine quicksort(a)
    type(pairtype), intent(inout) :: a(:)
    type(pairtype) :: pivot, tmp
    integer :: i, j, k, n
    real(4) :: r
    n = size(a)
    if (n <= 1) return
    i = 1
    j = n
    call random_number(r)
    k = 1 + floor(r * n)
    k = max(1, min(k, n))
    pivot = a(k)
    do
      do while (.not. pivot % v <= a(i) % v)
        i = i + 1
      end do
      do while (.not. a(j) % v <= pivot % v)
        j = j - 1
      end do
      if (i >= j) exit
      tmp = a(i)
      a(i) = a(j)
      a(j) = tmp
      i = i + 1
      j = j - 1
    end do
    call quicksort(a(1:i - 1))
    call quicksort(a(j + 1:n))
  end subroutine quicksort
end program main


program main
  implicit none
  type pairtype
    integer :: v, k
  end type pairtype
  integer :: n, i, p1, p2
  integer, allocatable :: a(:)
  type(pairtype), allocatable :: a1(:), a2(:)
  read *, n
  allocate (a(n), a1(n), a2(n))
  read *, a
  do i = 1, n
    a1(i) = pairtype(a(i), i)
  end do
  do i = 1, n
    a2(i) = a1(n - i + 1)
  end do
  call mergesort(a2, n)
  do i = 1, n
    a1(i) = a2(n - i + 1)
  end do

  ! print *, a1

  p1 = 1
  p2 = n
  do i = 1, n
    a2 = ins(a1, p1, p2, n)
    if (score(a2, n) > score(a1, n)) then
      a1 = a2
      p2 = p2 - 1
    else
      p1 = p1 + 1
    end if
  end do

  print "(i0)", score(a1, n)

  contains

  subroutine merge_arrays(a, na, b, nb, c, nc)
    ! Merge arrays a and b to c
    ! A better implementation is at ~/repos/fortran/Brainerd/sort2_13/main.f90
    implicit none
    integer, intent(in) :: na, nb, nc
    type(pairtype), intent(in) :: a(na), b(nb)
    type(pairtype), intent(inout) :: c(nc)
    integer :: i, j, k

    if (na + nb /= nc) then
      print *, "subroutine merge_arrays size error"
      stop 161
    end if

    i = 1; j = 1; k = 1
    do while(i <= na .and. j <= nb)
      if (a(i) % v <= b(j) % v) then
        c(k) = a(i)
        i = i + 1
      else
        c(k) = b(j)
        j = j + 1
      endif
      k = k + 1
    enddo
    do while (i <= na)  ! b seems to need similar process
      c(k) = a(i)
      i = i + 1
      k = k + 1
    enddo
  end subroutine merge_arrays

  recursive subroutine mergesort(a, n)
    ! Sort an array a(n)
    implicit none
    integer, intent(in) :: n
    type(pairtype), intent(inout) :: a(n)
    integer :: na
    type(pairtype) :: v, t((n + 1) / 2)

    if (n < 2) then
      return
    else if (n == 2) then
      if (a(1) % v > a(2)% v ) then
        v = a(1)
        a(1) = a(2)
        a(2) = v
      endif
      return
    endif

    na = (n + 1) / 2
    call mergesort(a, na)
    call mergesort(a(na + 1), n - na)

    if (a(na)  % v> a(na + 1) % v) then
      t(1:na) = a(1:na)
      call merge_arrays(t, na, a(na + 1:n), n - na, a, n)
    endif
  end subroutine mergesort

  function ins(a, p1, p2, n) result(a2)
    ! p1にあったものをp2に入れる
    integer, intent(in) :: p1, p2, n
    type(pairtype), intent(in) :: a(n)
    type(pairtype) :: a2(n)
    integer :: p2c
    p2c = p2
    do while (p2c < n)
      if (a(p2c + 1) % v == a(p1) % v) then
        p2c = p2c + 1
      else
        exit
      end if
    end do
    a2(1:p1 - 1) = a(1:p1 - 1)
    a2(p1:p2c - 1) = a(p1 + 1:p2c)
    a2(p2c) = a(p1)
    a2(p2c + 1:n) = a(p2c + 1:n)
  end function ins

  integer(8) function score(a, n)
    integer, intent(in) :: n
    type(pairtype), intent(in) :: a(n)
    integer :: i
    score = 0
    do i = 1, n
      score = score + abs(a(i) % k - i) * a(i) % v
    end do
  end function score

  recursive subroutine quicksort(a)
    type(pairtype), intent(inout) :: a(:)
    type(pairtype) :: pivot, tmp
    integer :: i, j, k, n
    real(4) :: r
    n = size(a)
    if (n <= 1) return
    i = 1
    j = n
    call random_number(r)
    k = 1 + floor(r * n)
    k = max(1, min(k, n))
    pivot = a(k)
    do
      do while (.not. pivot % v <= a(i) % v)
        i = i + 1
      end do
      do while (.not. a(j) % v <= pivot % v)
        j = j - 1
      end do
      if (i >= j) exit
      tmp = a(i)
      a(i) = a(j)
      a(j) = tmp
      i = i + 1
      j = j - 1
    end do
    call quicksort(a(1:i - 1))
    call quicksort(a(j + 1:n))
  end subroutine quicksort
end program main


program main
    implicit none
    integer(8) i, n, ans
    ans = 0
    read *, n
    do i = 1, n
        if (mod(i, 3) /= 0 .and. mod(i, 5) /= 0) then
            ans = ans + i
        end if
    end do
    print "(i0)", ans
 end program main

Score : 200 points

Problem Statement

Let us define the FizzBuzz sequence a_1,a_2,... as follows:

If both 3 and 5 divides i, a_i=\mbox{FizzBuzz}.

If the above does not hold but 3 divides i, a_i=\mbox{Fizz}.

If none of the above holds but 5 divides i, a_i=\mbox{Buzz}.

If none of the above holds, a_i=i.

Find the sum of all numbers among the first N terms of the FizzBuzz sequence.

Constraints

1 \leq N \leq 10^6

Input

Input is given from Standard Input in the following format:

N

Output

Print the sum of all numbers among the first N terms of the FizzBuzz sequence.

Sample Input 1

15

Sample Output 1

60

The first 15 terms of the FizzBuzz sequence are:

1,2,\mbox{Fizz},4,\mbox{Buzz},\mbox{Fizz},7,8,\mbox{Fizz},\mbox{Buzz},11,\mbox{Fizz},13,14,\mbox{FizzBuzz}

Among them, numbers are 1,2,4,7,8,11,13,14, and the sum of them is 60.

Sample Input 2

1000000

Sample Output 2

266666333332

Watch out for overflow.


program main
    implicit none
    integer i, n
    integer(selected_int_kind(r=15)) :: total = 0
    read *, n
    do i=1, n
        if ( mod(i, 3) /= 0 .and. mod(i, 5) /= 0) then
            total = total + i
        endif
    enddo
    print *, total
end program main


program abc162a
   implicit none
   integer(8) :: N, i
   integer(8), allocatable :: array(:)

   read(*,*) N
   allocate(array(N))

   do i = 1, N
      array(i) = i
   end do

   do i = 1, N
      if (mod(array(i),3) == 0 .or. mod(array(i), 5) == 0) then
         array(i) = 0
      endif
   end do

   write(*,*) sum(array(:))

end program abc162a


program fizzbuzz_sum
  implicit none
  integer(8) :: n, i, s = 0_8
  read(*,*) n
  do i = 1_8, n
    if (mod(i,3_8)*mod(i,5_8) == 0_8) cycle
    s = s+i
  end do
  write(*,'(i0)') s
end program fizzbuzz_sum


module mod_modint
  implicit none
  integer(8) :: modulus = 10**9+7
  type modint
    private
    integer(8) :: num
  contains
    procedure :: get => getnum
  end type

  interface assignment(=)
    module procedure :: setm, seti64, seti32
  end interface assignment(=)

  interface operator(+)
    module procedure :: posm, addmm, addmi64, addmi32, addi64m, addi32m
  end interface operator(+)

  interface operator(-)
    module procedure :: negm, submm, submi64, submi32, subi64m, subi32m
  end interface operator(-)

  interface operator(*)
    module procedure :: mulmm, mulmi64, mulmi32, muli64m, muli32m
  end interface operator(*)

  interface operator(/)
    module procedure :: divmm, divmi64, divmi32, divi64m, divi32m
  end interface operator(/)

  interface operator(**)
    module procedure :: powmi64, powmi32
  end interface operator(**)

  interface inv
    module procedure :: invm
  end interface inv

contains

  integer(8) function getnum(this)
    class(modint), intent(in) :: this
    getnum = this%num
  end

  pure elemental type(modint) function newm()
    newm%num = 0_8
  end

  pure elemental subroutine setm(x,y)
    type(modint), intent(inout) :: x
    type(modint), intent(in) :: y
    x%num = y%num
  end

  pure elemental function posm(x) result(n)
    type(modint), intent(in) :: x
    type(modint) :: n
    n%num = x%num
  end

  pure elemental function negm(x) result(n)
    type(modint), intent(in) :: x
    type(modint) :: n
    n%num = modulus-x%num
  end

  pure elemental function addmm(x,y) result(n)
    type(modint), intent(in) :: x, y
    type(modint) :: n
    n%num = x%num+y%num
    if (n%num >= modulus) n%num = n%num-modulus
  end

  pure elemental function submm(x,y) result(n)
    type(modint), intent(in) :: x, y
    type(modint) :: n
    n%num = x%num-y%num
    if (n%num < 0_8) n%num = n%num+modulus
  end

  pure elemental function mulmm(x,y) result(n)
    type(modint), intent(in) :: x, y
    type(modint) :: n
    n%num = mod(x%num*y%num,modulus)
  end

  impure elemental function invm(x) result(n)
    type(modint), intent(in) :: x
    type(modint) :: n
    integer(8) :: a, b, c, q, r, v
    a = x%num
    if (a == 0_8) then
      write(*,'(a)') "Error: Division by zero (x == 0). (modint, invm)"
      stop
    end if
    b = modulus
    c = 0_8
    v = 1_8
    do while (b /= 0_8)
      q = a/b
      r = mod(a,b)
      a = b
      b = r
      r = c
      c = v-c*q
      v = r
    end do
    n%num = mod(v,modulus)
    if (n%num < 0_8) n%num = n%num+modulus
  end

  impure elemental function divmm(x,y) result(n)
    type(modint), intent(in) :: x, y
    type(modint) :: n
    n = mulmm(x,invm(y))
  end

  impure elemental type(modint) function newi(i)
    class(*), intent(in) :: i
    select type(i)
    type is (integer(8))
      newi%num = i
    type is (integer)
      newi%num = int(i,8)
    type is (integer(2))
      newi%num = int(i,8)
    type is (integer(1))
      newi%num = int(i,8)
    class default
      write(*,'(a)') "Error: Invalid value (i is not integer). (modint, newi)"
      stop
    end select
    newi%num = mod(newi%num,modulus)
    if (newi%num < 0_8) newi%num = newi%num+modulus
  end

  impure elemental subroutine seti64(x,i)
    type(modint), intent(inout) :: x
    integer(8), intent(in) :: i
    call setm(x,newi(i))
  end

  impure elemental subroutine seti32(x,i)
    type(modint), intent(inout) :: x
    integer, intent(in) :: i
    call setm(x,newi(i))
  end

  impure elemental function addmi64(x,i) result(n)
    type(modint), intent(in) :: x
    integer(8), intent(in) :: i
    type(modint) :: n
    n = addmm(x,newi(i))
  end

  impure elemental function addmi32(x,i) result(n)
    type(modint), intent(in) :: x
    integer, intent(in) :: i
    type(modint) :: n
    n = addmm(x,newi(i))
  end

  impure elemental function addi64m(i,y) result(n)
    integer(8), intent(in) :: i
    type(modint), intent(in) :: y
    type(modint) :: n
    n = addmm(newi(i),y)
  end

  impure elemental function addi32m(i,y) result(n)
    integer, intent(in) :: i
    type(modint), intent(in) :: y
    type(modint) :: n
    n = addmm(newi(i),y)
  end

  impure elemental function submi64(x,i) result(n)
    type(modint), intent(in) :: x
    integer(8), intent(in) :: i
    type(modint) :: n
    n = submm(x,newi(i))
  end

  impure elemental function submi32(x,i) result(n)
    type(modint), intent(in) :: x
    integer, intent(in) :: i
    type(modint) :: n
    n = submm(x,newi(i))
  end

  impure elemental function subi64m(i,y) result(n)
    integer(8), intent(in) :: i
    type(modint), intent(in) :: y
    type(modint) :: n
    n = submm(newi(i),y)
  end

  impure elemental function subi32m(i,y) result(n)
    integer, intent(in) :: i
    type(modint), intent(in) :: y
    type(modint) :: n
    n = submm(newi(i),y)
  end

  impure elemental function mulmi64(x,i) result(n)
    type(modint), intent(in) :: x
    integer(8), intent(in) :: i
    type(modint) :: n
    n = mulmm(x,newi(i))
  end

  impure elemental function mulmi32(x,i) result(n)
    type(modint), intent(in) :: x
    integer, intent(in) :: i
    type(modint) :: n
    n = mulmm(x,newi(i))
  end

  impure elemental function muli64m(i,y) result(n)
    integer(8), intent(in) :: i
    type(modint), intent(in) :: y
    type(modint) :: n
    n = mulmm(newi(i),y)
  end

  impure elemental function muli32m(i,y) result(n)
    integer, intent(in) :: i
    type(modint), intent(in) :: y
    type(modint) :: n
    n = mulmm(newi(i),y)
  end

  impure elemental function divmi64(x,i) result(n)
    type(modint), intent(in) :: x
    integer(8), intent(in) :: i
    type(modint) :: n
    n = divmm(x,newi(i))
  end

  impure elemental function divmi32(x,i) result(n)
    type(modint), intent(in) :: x
    integer, intent(in) :: i
    type(modint) :: n
    n = divmm(x,newi(i))
  end

  impure elemental function divi64m(i,y) result(n)
    integer(8), intent(in) :: i
    type(modint), intent(in) :: y
    type(modint) :: n
    n = divmm(newi(i),y)
  end

  impure elemental function divi32m(i,y) result(n)
    integer, intent(in) :: i
    type(modint), intent(in) :: y
    type(modint) :: n
    n = divmm(newi(i),y)
  end

  impure elemental function powmi64(x,i) result(n)
    type(modint), intent(in) :: x
    integer(8), intent(in) :: i
    type(modint) :: n, p
    integer(8) :: m
    n = newi(1_8)
    p = x
    m = i
    if (i < 0_8) then
      p = invm(x)
      m = abs(i)
    end if
    do while (m > 0_8)
      if (btest(m,0)) n = mulmm(p,n)
      p = mulmm(p,p)
      m = rshift(m,1)
    end do
  end

  impure elemental function powmi32(x,i) result(n)
    type(modint), intent(in) :: x
    integer, intent(in) :: i
    type(modint) :: n, p
    integer :: m
    n = newi(1_8)
    p = x
    m = i
    if (i < 0) then
      p = invm(x)
      m = abs(i)
    end if
    do while (m > 0)
      if (btest(m,0)) n = mulmm(p,n)
      p = mulmm(p,p)
      m = rshift(m,1)
    end do
  end

end module mod_modint
program em
use mod_modint
implicit none
integer::N,K
type(modint)::ANS
type(modint),allocatable,dimension(:)::X
!X(i)には(gcdがiとなるような数列)の個数を記録していく
integer::i,j
type(modint)::TMP
read*,N,K
allocate(X(K))
!gcdは1からkになる
do i=K,1,-1
  TMP=K/i
  X(i)=TMP**N
  do j=2,K/i
    X(i)=X(i)-X(j*i)
  end do
end do
ANS=0
do i=1,K
  ANS=ANS+i*X(i)
end do
print"(i0)",getnum(ANS)
end program em

Score : 500 points

Problem Statement

Consider sequences \{A_1,...,A_N\} of length N consisting of integers between 1 and K (inclusive).

There are K^N such sequences. Find the sum of \gcd(A_1, ..., A_N) over all of them.

Since this sum can be enormous, print the value modulo (10^9+7).

Here \gcd(A_1, ..., A_N) denotes the greatest common divisor of A_1, ..., A_N.

Constraints

2 \leq N \leq 10^5

1 \leq K \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the sum of \gcd(A_1, ..., A_N) over all K^N sequences, modulo (10^9+7).

Sample Input 1

3 2

Sample Output 1

9

\gcd(1,1,1)+\gcd(1,1,2)+\gcd(1,2,1)+\gcd(1,2,2)
+\gcd(2,1,1)+\gcd(2,1,2)+\gcd(2,2,1)+\gcd(2,2,2)
=1+1+1+1+1+1+1+2=9

Thus, the answer is 9.

Sample Input 2

3 200

Sample Output 2

10813692

Sample Input 3

100000 100000

Sample Output 3

742202979

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


program select_half
  implicit none
  integer(8), parameter :: inf = 1e17
  integer :: n, i, j
  integer(8) :: x = -inf
  integer(8), dimension(-1:200002) :: a = 0_8, l = 0_8, r = 0_8
  integer(8), dimension(-1:200002) :: lm = inf, rm = inf
  logical :: is_even = .false.
  read(*,*) n
  read(*,*) a(1:n)
  do i = 1, n
    l(i) = l(i-2)+a(i)
    lm(i) = min(lm(i-2),a(i))
  end do
  do i = n, 1, -1
    r(i) = r(i+2)+a(i)
    rm(i) = min(rm(i+2),a(i))
  end do
  lm(-1:0) = 0_8
  lm(n+1:n+2) = 0_8
  rm(-1:0) = 0_8
  rm(n+1:n+2) = 0_8
  is_even = mod(n,2) == 0
  do i = 0, n
    x = max(x,l(i-1)+r(i+2))
    x = max(x,l(i)+r(i+2))
    x = max(x,l(i-1)+r(i+1))
    if (is_even) cycle
    x = max(x,l(i)+r(i+1)-a(i))
    x = max(x,l(i)+r(i+1)-a(i+1))
    if (mod(i,2) == 1) cycle
    do j = i+4, n, 2
      x = max(x,l(i-1)+r(i+2)-r(j)+r(j+1))
    end do
  end do
  write(*,'(i0)') x
end program select_half

Score : 600 points

Problem Statement

Given is an integer sequence A_1, ..., A_N of length N.

We will choose exactly \left\lfloor \frac{N}{2} \right\rfloor elements from this sequence so that no two adjacent elements are chosen.

Find the maximum possible sum of the chosen elements.

Here \lfloor x \rfloor denotes the greatest integer not greater than x.

Constraints

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

|A_i|\leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 ... A_N

Output

Print the maximum possible sum of the chosen elements.

Sample Input 1

6
1 2 3 4 5 6

Sample Output 1

12

Choosing 2, 4, and 6 makes the sum 12, which is the maximum possible value.

Sample Input 2

5
-1000 -100 -10 0 10

Sample Output 2

0

Choosing -10 and 10 makes the sum 0, which is the maximum possible value.

Sample Input 3

10
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 3

5000000000

Watch out for overflow.

Sample Input 4

27
18 -28 18 28 -45 90 -45 23 -53 60 28 -74 -71 35 -26 -62 49 -77 57 24 -70 -93 69 -99 59 57 -49

Sample Output 4

295


program ABC161C
    implicit none
    integer(8)::N,K
    read*,N,K

    print'(i0)',min(mod(N,K),abs(K-mod(N,K)))
end program ABC161C

Score : 300 points

Problem Statement

Given any integer x, Aoki can do the operation below.

Operation: Replace x with the absolute difference of x and K.

You are given the initial value of an integer N. Find the minimum possible value taken by N after Aoki does the operation zero or more times.

Constraints

0 ≤ N ≤ 10^{18}

1 ≤ K ≤ 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the minimum possible value taken by N after Aoki does the operation zero or more times.

Sample Input 1

7 4

Sample Output 1

1

Initially, N=7.

After one operation, N becomes |7-4| = 3.

After two operations, N becomes |3-4| = 1, which is the minimum value taken by N.

Sample Input 2

2 6

Sample Output 2

2

N=2 after zero operations is the minimum.

Sample Input 3

1000000000000000000 1

Sample Output 3

0


   program abc161c
      implicit none
      integer(8)::n,k
!参考有

      read *,n,k

      n=mod(n,k)
      print *,min(n,k-n) !割り算の余りを両側から考える感覚
      stop

   end program abc161c


program abc161c
   implicit none
   integer(8) :: n, k, min_val, i, x

   read(*,*) n, k

   print *, min(mod(n, k), k-mod(n,k))

end program abc161c


program main

  implicit none
  integer(8) :: n, k

  read(*,*) n, k

  if( mod(n,k) < abs( mod(n,k) - k ) ) then
    print*, mod(n,k)
  else
    print*, abs( mod(n,k) - k )
  end if

end program main


program name
    implicit none
    integer(4):: k,i,dig, kn
    integer(4),allocatable:: r(:)
    character(1),allocatable:: ans(:)

    read*, k

    allocate(r(1))
    r(1) = 0

    do while(k > 0)
        k=k-1
        r(1) = up(1)

        deallocate(ans)
    end do

    allocate(ans(size(r)))
    do i=1,size(r)
        ans(size(r)-i+1) = char(r(i) + ichar('1')-1)
    end do
    print*, ans(:)
contains
recursive function up(k) result(ret)
    integer(4):: k
    integer(4):: ret
    integer(4),allocatable:: tmp(:)

    if (k+1 > size(r)) then
        if (r(k) <= 8) then
            ret = r(k)+1
            return
        else
            allocate(tmp(size(r)))
            tmp(:) = r(:)
            deallocate(r)
            allocate(r(size(tmp)+1))
            r(1:size(tmp)) = tmp(:)
            r(size(tmp)+1) = 1
            ret = 0
            return
        end if
    end if

    if (abs(r(k)+1 - r(k+1)) <= 1) then
        if (r(k) <= 8) then
            ret = r(k)+1
            return
        else
            r(k+1) = up(k+1)
            ret = max(0,r(k+1) -1)
            return
        end if

    end if

! 繰り上がり
    r(k+1) = up(k+1)
    ret = max(0,r(k+1) -1)

end function

end program name

Score : 400 points

Problem Statement

A positive integer X is said to be a lunlun number if and only if the following condition is satisfied:

In the base ten representation of X (without leading zeros), for every pair of two adjacent digits, the absolute difference of those digits is at most 1.

For example, 1234, 1, and 334 are lunlun numbers, while none of 31415, 119, or 13579 is.

You are given a positive integer K. Find the K-th smallest lunlun number.

Constraints

1 \leq K \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

K

Output

Print the answer.

Sample Input 1

15

Sample Output 1

23

We will list the 15 smallest lunlun numbers in ascending order:

1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
21,
22,
23.

Thus, the answer is 23.

Sample Input 2

1

Sample Output 2

1

Sample Input 3

13

Sample Output 3

21

Sample Input 4

100000

Sample Output 4

3234566667

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


      MODULE solveMod
        implicit none

        public
        integer(16) :: counter=0,maxDigit,k
        integer(16) :: ans(10)

        contains
        function numLun( digit ) result( num )
          implicit none
          integer(16),intent(in) :: digit
          integer(16) :: num,i
          logical dummy

          maxDigit = digit

          do i = 1,9
            dummy = check( i,1_16 )
            ans(1) = i
          end do

          num = counter
        end function numLun

        recursive function check( num,numCall ) result( dummy )
          implicit none
          integer(16) :: num,numCall,i
          logical :: dummy
          character(10) :: s

          ans(numCall) = num

          if( num==-1.or.num==10 )then
            dummy = .false.
            return
          else
            if( numCall==maxDigit )then
              counter = counter + 1
              if(counter==k)then
                do i = 1,maxDigit
                  write(s(i:i),'(i1)') ans(i)
                end do
                !print*,ans(1:maxDigit)
                print*,s(1:maxDigit)
                stop
              end if
              return
            else
              dummy = check( num-1,numCall+1 )
              dummy = check( num  ,numCall+1 )
              dummy = check( num+1,numCall+1 )
            end if
          end if
        end function check

      END MODULE solveMod

      PROGRAM LunlunNumber
      use solveMod
      IMPLICIT NONE
      integer(16) ::  digit,buffer

      read*,k

      digit = 1 !digit is digit
      buffer = 0
      do while(.true.)
        buffer = buffer + numLun( digit )
        digit = digit + 1
      end do

      END PROGRAM


program main
    implicit none
    integer i, flag
    character s*6
    read(*,*) s

    flag = 0
    if (s(3:3) == s(4:4)) then
        flag = 0
    else
        flag = 1
    end if

    if (flag == 1) then
        if(s(5:5) == s(6:6)) then
            flag = 0
        else
            flag = 1
        end if
    end if

    if (flag == 0) then
        write(*,*) "Yes"
    else if (flag == 1) then
        write(*,*) "No"
    end if

end program

Score : 100 points

Problem Statement

A string of length 6 consisting of lowercase English letters is said to be coffee-like if and only if its 3-rd and 4-th characters are equal and its 5-th and 6-th characters are also equal.

Given a string S, determine whether it is coffee-like.

Constraints

S is a string of length 6 consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If S is coffee-like, print Yes; otherwise, print No.

Sample Input 1

sippuu

Sample Output 1

Yes

In sippuu, the 3-rd and 4-th characters are equal, and the 5-th and 6-th characters are also equal.

Sample Input 2

iphone

Sample Output 2

No

Sample Input 3

coffee

Sample Output 3

Yes


program sample
    implicit none

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

    read(*,*) k,n
    allocate(x(n))
    a=0
    do i=1,n-1
        b=x(i+1)-x(i)
        if (b>a)then
            a=b
        end if
    end do
    b=k-x(n)+x(1)
    if (b>a)then
        a=b
    end if
    write(*,*)k-a

    stop
end program sample

Score : 300 points

Problem Statement

There is a circular pond with a perimeter of K meters, and N houses around them.

The i-th house is built at a distance of A_i meters from the northmost point of the pond, measured clockwise around the pond.

When traveling between these houses, you can only go around the pond.

Find the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.

Constraints

2 \leq K \leq 10^6

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

0 \leq A_1 < ... < A_N < K

All values in input are integers.

Input

Input is given from Standard Input in the following format:

K N
A_1 A_2 ... A_N

Output

Print the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.

Sample Input 1

20 3
5 10 15

Sample Output 1

10

If you start at the 1-st house and go to the 2-nd and 3-rd houses in this order, the total distance traveled will be 10.

Sample Input 2

20 3
0 5 15

Sample Output 2

10

If you start at the 2-nd house and go to the 1-st and 3-rd houses in this order, the total distance traveled will be 10.


program atcoder0328_3
  integer, dimension(:), allocatable:: A,D
  read(5,*)K,N
  allocate(A(N),D(N))
  read(5,*)A
  do I=1,N-1
     D(I)=A(I+1)-A(I)
  end do
  D(N)=K+A(1)-A(N)
  L=maxval(D)
  write(6,*)int(K-L)
end program atcoder0328_3


program the_number_of_even_pairs

 implicit none
 integer :: n,m,ans

 read*,n,m
 ans=n*(n-1)/2+m*(m-1)/2

 print*,ans

end program the_number_of_even_pairs

Score : 100 points

Problem Statement

We have N+M balls, each of which has an integer written on it.

It is known that:

The numbers written on N of the balls are even.

The numbers written on M of the balls are odd.

Find the number of ways to choose two of the N+M balls (disregarding order) so that the sum of the numbers written on them is even.

It can be shown that this count does not depend on the actual values written on the balls.

Constraints

0 \leq N,M \leq 100

2 \leq N+M

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

Print the answer.

Sample Input 1

2 1

Sample Output 1

1

For example, let us assume that the numbers written on the three balls are 1,2,4.

If we choose the two balls with 1 and 2, the sum is odd;

If we choose the two balls with 1 and 4, the sum is odd;

If we choose the two balls with 2 and 4, the sum is even.

Thus, the answer is 1.

Sample Input 2

4 3

Sample Output 2

9

Sample Input 3

1 1

Sample Output 3

0

Sample Input 4

13 3

Sample Output 4

81

Sample Input 5

0 3

Sample Output 5

3


program main

  implicit none

  integer       ::  n,m
  !character       ::  a(32), k
  integer       ::  i,j,k

  read(*,*) n,m

  print*, n*(n-1)/2 + m*(m-1)/2

end program main


program the_number_of_even_pairs
  implicit none
  integer :: n, m
  read(*,*) n, m
  write(*,'(i0)') (n*(n-1)+m*(m-1))/2
end program the_number_of_even_pairs


   Program ABC159B
   implicit none
   integer::n,m,l,i,j,k
   character(110)::s,t,u

      read(*,*) s
      n=len_trim(s)
      t=s(1:(n-1)/2)
      u=s((n+3)/2:n)
      m=len_trim(t)
      l=len_trim(u)

      A:do i=0,(n+1)/2
         if (s(i+1:i+1)/=s(n-i:n-i)) then
            print *,'No'
            exit A
         else
            B:do j=0,(m+1)/2
               if (t(j+1:j+1)/=t(m-j:m-j)) then
                  print *,'No'
                  exit A
               else
                  C:do k=0,(l+1)/2
                     if (u(k+1:k+1)/=u(l-k:l-k)) then
                        print *,'No'
                        exit A
                     else
                        print *,'Yes'
                        exit A
                     end if
                  end do C
               end if
            end do B
         end if
      end do A

      stop

   End program ABC159B

Score : 200 points

Problem Statement

A string S of an odd length is said to be a strong palindrome if and only if all of the following conditions are satisfied:

S is a palindrome.

Let N be the length of S. The string formed by the 1-st through ((N-1)/2)-th characters of S is a palindrome.

The string consisting of the (N+3)/2-st through N-th characters of S is a palindrome.

Determine whether S is a strong palindrome.

Constraints

S consists of lowercase English letters.

The length of S is an odd number between 3 and 99 (inclusive).

Input

Input is given from Standard Input in the following format:

S

Output

If S is a strong palindrome, print Yes;
otherwise, print No.

Sample Input 1

akasaka

Sample Output 1

Yes

S is akasaka.

The string formed by the 1-st through the 3-rd characters is aka.

The string formed by the 5-th through the 7-th characters is aka.
All of these are palindromes, so S is a strong palindrome.

Sample Input 2

level

Sample Output 2

No

Sample Input 3

atcoder

Sample Output 3

No


   Program ABC159C
   implicit none
   integer::L,i,j
   real(8)::Vmax=0,xmax,ymax,zmax,x,y,z,V

      read *,L
      do i=1,100
         x=real(L)/100.0*real(i)
         do j=1,100
            y=(real(L)-x)/100.0*real(j)
            z=real(L)-x-y
            V=x*y*z
            if (V>Vmax) then
               Vmax=V
               xmax=x
               ymax=y
               zmax=z
            end if
         end do
      end do

      do i=-50,50
         x=xmax+real(L)/10000.0*real(i)
         do j=-50,50
            y=ymax+real(L)/10000.0*real(j)
            z=real(L)-x-y
            V=x*y*z
            if (V>Vmax) then
               Vmax=V
            end if
         end do
      end do

      print *,Vmax
      stop

   End Program ABC159C

Score : 300 points

Problem Statement

Given is a positive integer L.
Find the maximum possible volume of a rectangular cuboid whose sum of the dimensions (not necessarily integers) is L.

Constraints

1 ≤ L ≤ 1000

L is an integer.

Input

Input is given from Standard Input in the following format:

L

Output

Print the maximum possible volume of a rectangular cuboid whose sum of the dimensions (not necessarily integers) is L.
Your output is considered correct if its absolute or relative error from our answer is at most 10^{-6}.

Sample Input 1

3

Sample Output 1

1.000000000000

For example, a rectangular cuboid whose dimensions are 0.8, 1, and 1.2 has a volume of 0.96.

On the other hand, if the dimensions are 1, 1, and 1, the volume of the rectangular cuboid is 1, which is greater.

Sample Input 2

999

Sample Output 2

36926037.000000000000


program sample
    implicit none

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

    read(*,*) n
    allocate(a(n),y(n))
    read(*,*)a
    y(:)=0
    do i=1,n
        b=a(i)
        y(b)=y(b)+1
    end do
    m=0
    do i=1,n
        b=y(i)
        m=m+b*(b-1)/2

    end do
    do i=1,n
        b=a(i)
        if(y(b)<2)then
            write(*,*)m
        else
            write(*,*)m-y(b)+1
        end if
    end do

    stop
end program sample

Score : 400 points

Problem Statement

We have N balls. The i-th ball has an integer A_i written on it.

For each k=1, 2, ..., N, solve the following problem and print the answer.

Find the number of ways to choose two distinct balls (disregarding order) from the N-1 balls other than the k-th ball so that the integers written on them are equal.

Constraints

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

1 \leq A_i \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

For each k=1,2,...,N, print a line containing the answer.

Sample Input 1

5
1 1 2 1 2

Sample Output 1

2
2
3
2
3

Consider the case k=1 for example. The numbers written on the remaining balls are 1,2,1,2.

From these balls, there are two ways to choose two distinct balls so that the integers written on them are equal.

Thus, the answer for k=1 is 2.

Sample Input 2

4
1 2 3 4

Sample Output 2

0
0
0
0

No two balls have equal numbers written on them.

Sample Input 3

5
3 3 3 3 3

Sample Output 3

6
6
6
6
6

Any two balls have equal numbers written on them.

Sample Input 4

8
1 2 1 4 2 1 4 1

Sample Output 4

5
7
5
7
7
5
7
5


program main
      implicit none
      integer(4) :: n
      integer(4), allocatable :: a(:), num(:)
      integer(4) :: i, j, k, cnt

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

      allocate( num(n) )
      num(:) = 0
      do i = 1, n
        j = a(i)
        num(j) = num(j) + 1
      end do

      cnt = 0
      do i = 1, n
        cnt = cnt + num(i) * ( num(i) - 1 ) / 2
      end do

      do i = 1, n
        j = a(i)
        k = cnt - (num(j) - 1)
        print *, k
      end do

end program


program BannedK
  implicit none

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

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

  do k = 1, n
     s = 0
     do i = 1, n-1
        do j = i+1, n
           if (i /= k .and. j /= k .and. a(i) == a(j)) then
              s = s + 1
           end if
        end do
     end do
     print *, s
  end do

  deallocate(a)

  stop
end program BannedK


program main
    implicit none
    integer(8):: h=10,w=1000,k=1000
    integer(8):: i,j
    integer(8), allocatable:: c(:,:), now(:)
    integer(8), parameter:: inf = 1001001001
    character(1), allocatable:: s(:,:)
    read*, h,w,k

    allocate(s(w,h))

    block
        character(:), allocatable:: input_s
        allocate(character(w)::input_s)

        do i=1,h
            read*, input_s(:)
            do j=1,w
                s(j,i) = input_s(j:j)
            end do
        end do
    end block

    s(:,:) = '0'

    block
        integer(8):: div, g, ints, num
        integer(8):: ans = inf
        integer(8), allocatable:: id(:)
        logical:: emp, ok

        allocate(id(h))
        do div=1, lshift(1, h-1)
            g = 1

            do i=1, h
                id(i) = g
                if (and(rshift(div-1,i-1), 1) == 1) g=g+1
            end do

            allocate(c(w, maxval(id)))
            c(:,:) = 0

            do i=1,h
                do j=1,w
                    read(s(j,i),*) ints
                    c(j,id(i))=c(j,id(i))+ints
                end do
            end do

            ! ok=.true.
            ! do i=1,g
            !     do j=1,w
            !         if(c(j,i) > k) ok = .false.
            !     end do
            ! end do

            ! if (.not. ok) then
            !     deallocate(c)
            !     cycle
            ! end if

            ! allocate(now(g+1))
            ! num = g-1
            ! now(:) = 0
            ! do j=1,w
            !     if (.not. add(j,g)) then
            !         num=num+1
            !         now(:)=0
            !         emp = add(j,g)
            !     end if
            ! end do

            ! ans = min(ans,num)
            ! deallocate(c)
            ! deallocate(now)
        end do

        print*, ans
    end block

contains
function add(j,g) result(ok)
    integer(8):: i,j,g
    logical:: ok

    do i=1,g
        now(i)=now(i)+c(j,i)
    end do

    ok=.true.
    do i=1,g+1
        if (now(i) > k) ok=.false.
    end do
end function

end program main

Score : 500 points

Problem Statement

We have a chocolate bar partitioned into H horizontal rows and W vertical columns of squares.

The square (i, j) at the i-th row from the top and the j-th column from the left is dark if S_{i,j} is 0, and white if S_{i,j} is 1.

We will cut the bar some number of times to divide it into some number of blocks. In each cut, we cut the whole bar by a line running along some boundaries of squares from end to end of the bar.

How many times do we need to cut the bar so that every block after the cuts has K or less white squares?

Constraints

1 \leq H \leq 10

1 \leq W \leq 1000

1 \leq K \leq H \times W

S_{i,j} is 0 or 1.

Input

Input is given from Standard Input in the following format:

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

Output

Print the number of minimum times the bar needs to be cut so that every block after the cuts has K or less white squares.

Sample Input 1

3 5 4
11100
10001
00111

Sample Output 1

2

For example, cutting between the 1-st and 2-nd rows and between the 3-rd and 4-th columns - as shown in the figure to the left - works.

Note that we cannot cut the bar in the ways shown in the two figures to the right.

Sample Input 2

3 5 8
11100
10001
00111

Sample Output 2

0

No cut is needed.

Sample Input 3

4 10 4
1110010010
1000101110
0011101001
1101000111

Sample Output 3

3


program main
    implicit none
    integer(8):: h=10,w=1000,k=1000
    integer(8):: i,j
    integer(8), allocatable:: c(:,:), now(:)
    integer(8), parameter:: inf = 1001001001
    character(1), allocatable:: s(:,:)
    read*, h,w,k

    allocate(s(w,h))

    block
        character(:), allocatable:: input_s
        allocate(character(w)::input_s)

        do i=1,h
            read*, input_s(:)
            do j=1,w
                s(j,i) = input_s(j:j)
            end do
        end do
    end block

    ! s(:,:) = '0'

    block
        integer(8):: div, g, ints, num
        integer(8):: ans = inf
        integer(8), allocatable:: id(:)
        logical:: emp, ok

        allocate(id(h))
        do div=1, lshift(1, h-1)
            g = 1

            do i=1, h
                id(i) = g
                if (and(rshift(div-1,i-1), 1) == 1) g=g+1
            end do

            allocate(c(w, maxval(id)))
            c(:,:) = 0

            do i=1,h
                do j=1,w
                    read(s(j,i),*) ints
                    c(j,id(i))=c(j,id(i))+ints
                end do
            end do

            ok=.true.
            do i=1,g
                do j=1,w
                    if(c(j,i) > k) ok = .false.
                end do
            end do

            if (.not. ok) then
                deallocate(c)
                cycle
            end if

            allocate(now(g+1))
            num = g-1
            now(:) = 0
            do j=1,w
                if (.not. add(j,g)) then
                    num=num+1
                    now(:)=0
                    emp = add(j,g)
                end if
            end do

            ans = min(ans,num)
            deallocate(c)
            deallocate(now)
        end do

        print*, ans
    end block

contains
function add(j,g) result(ok)
    integer(8):: i,j,g
    logical:: ok

    do i=1,g
        now(i)=now(i)+c(j,i)
    end do

    ok=.true.
    do i=1,g+1
        if (now(i) > k) ok=.false.
    end do
end function

end program main


program main
    implicit none
    integer(8):: h,w,k
    integer(8):: i,j
    integer(8), allocatable:: c(:,:), now(:)
    integer(8), parameter:: inf = 1001001001
    character(1), allocatable:: s(:,:)
    read*, h,w,k

    allocate(s(w,h))

    block
        character(:), allocatable:: input_s
        allocate(character(w)::input_s)

        do i=1,h
            read*, input_s(:)
            do j=1,w
                s(j,i) = input_s(j:j)
            end do
        end do
    end block

    block
        integer(8):: div, g, ints, num
        integer(8):: ans = inf
        integer(8), allocatable:: id(:)
        logical:: emp, ok

        allocate(id(h))
        do div=1, lshift(1, h-1)
            g = 1

            do i=1, h
                id(i) = g
                if (and(rshift(div-1,i-1), 1) == 1) g=g+1
            end do

            allocate(c(w, maxval(id)))
            c(:,:) = 0

            do i=1,h
                do j=1,w
                    read(s(j,i),*) ints
                    c(j,id(i))=c(j,id(i))+ints
                end do
            end do

            ok=.true.
            do i=1,g
                do j=1,w
                    if(c(j,i) > k) ok = .false.
                end do
            end do

            if (.not. ok) cycle

            allocate(now(g+1))
            num = g-1
            now(:) = 0
            do j=1,w
                if (.not. add(j,g)) then
                    num=num+1
                    now(:)=0
                    emp = add(j,g)
                end if
            end do

            ans = min(ans,num)
            deallocate(c)
            deallocate(now)
        end do

        print*, ans
    end block

contains
function add(j,g) result(ok)
    integer(8):: i,j,g
    logical:: ok

    do i=1,g
        now(i)=now(i)+c(j,i)
    end do

    ok=.true.
    do i=1,g+1
        if (now(i) > k) ok=.false.
    end do
end function

end program main


program abc159f
  integer :: n, s, i, ans=0, a(3000), dp(0:3000)=0
  read *, n, s
  read *, a(:n)
  do i = 1,n
     dp(0) = i
     dp(a(i):s) = mod(dp(:s-a(i))+dp(a(i):s),998244353)
     ans = mod(ans+dp(s),998244353)
  end do
  print *, ans
end program abc159f

Score : 600 points

Problem Statement

Given are a sequence of N integers A_1, A_2, \ldots, A_N and a positive integer S.

For a pair of integers (L, R) such that 1\leq L \leq R \leq N, let us define f(L, R) as follows:

f(L, R) is the number of sequences of integers (x_1, x_2, \ldots , x_k) such that L \leq x_1 < x_2 < \cdots < x_k \leq R and A_{x_1}+A_{x_2}+\cdots +A_{x_k} = S.

Find the sum of f(L, R) over all pairs of integers (L, R) such that 1\leq L \leq R\leq N. Since this sum can be enormous, print it modulo 998244353.

Constraints

All values in input are integers.

1 \leq N \leq 3000

1 \leq S \leq 3000

1 \leq A_i \leq 3000

Input

Input is given from Standard Input in the following format:

N S
A_1 A_2 ... A_N

Output

Print the sum of f(L, R), modulo 998244353.

Sample Input 1

3 4
2 2 4

Sample Output 1

5

The value of f(L, R) for each pair is as follows, for a total of 5.

f(1,1) = 0

f(1,2) = 1 (for the sequence (1, 2))

f(1,3) = 2 (for (1, 2) and (3))

f(2,2) = 0

f(2,3) = 1 (for (3))

f(3,3) = 1 (for (3))

Sample Input 2

5 8
9 9 9 9 9

Sample Output 2

0

Sample Input 3

10 10
3 1 4 1 5 9 2 6 5 3

Sample Output 3

152


module deque_mod
    use,intrinsic :: iso_fortran_env
    implicit none
    type deque
        integer(int32),pointer:: v(:)
        integer(int32):: l,r
        integer(int32):: lmax, rmax
    end type
    private
    public:: deque
    public:: init_deque
    public:: append, appendleft
    public:: pop, popleft
    public:: right, left
    public:: remaining_elements
    public:: remain

contains
    subroutine init_deque(dq)
        ! 初期化
        type(deque):: dq
        dq%r=0; dq%rmax=1
        dq%l=1; dq%lmax=-1
        allocate(dq%v(dq%lmax:dq%rmax))
    end subroutine

    subroutine append(dq,num)
        ! 右端に挿入
        type(deque):: dq
        integer(int32):: num
        if (dq%r+1 > dq%rmax) call add_(dq)
        dq%r=dq%r+1
        dq%v(dq%r) = num
    end subroutine

    subroutine appendleft(dq,num)
        ! 左端に挿入
        type(deque):: dq
        integer(int32):: num
        if (dq%l-1 < dq%lmax) call add_(dq)
        dq%l=dq%l-1
        dq%v(dq%l) = num
    end subroutine

    subroutine add_(dq)
        ! arrayの延長
        type(deque):: dq
        integer(int32):: l
        integer(int32),pointer:: tmp(:)
        l = size(dq%v)
        allocate(tmp(l))
        tmp(:) = dq%v(:)
        deallocate(dq%v)
        allocate(dq%v(2*dq%lmax:2*dq%rmax))
        dq%v(dq%lmax:dq%rmax) = tmp(:)
        dq%lmax = 2*dq%lmax
        dq%rmax = 2*dq%rmax
    end subroutine

    function pop(dq) result(ret)
        ! 右端から取り出し
        type(deque):: dq
        integer(int32):: ret
        ret = dq%v(dq%r)
        dq%r=dq%r-1
    end function

    function popleft(dq) result(ret)
        ! 左端から取り出し
        type(deque):: dq
        integer(int32):: ret
        ret = dq%v(dq%l)
        dq%l=dq%l+1
    end function

    function right(dq) result(ret)
        ! 右端を確認
        type(deque):: dq
        integer(int32):: ret
        ret = dq%v(dq%r)
    end function

    function left(dq) result(ret)
        ! 左端を確認
        type(deque):: dq
        integer(int32):: ret
        ret = dq%v(dq%l)
    end function

    function remaining_elements(dq) result(ret)
        ! 残りの要素数
        type(deque):: dq
        integer(int32):: ret
        ret = dq%r - dq%l + 1
    end function

    function remain(dq) result(ret)
        ! 要素が残っているかどうか
        type(deque):: dq
        logical:: ret
        ret = remaining_elements(dq) > 0
    end function
end module

program agc043a
    use deque_mod
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: h,w,i,j,a
    integer(int32), allocatable:: si(:,:)
    character(1),allocatable:: s(:,:)
    logical,allocatable:: booked(:,:)
    type(deque):: x_next, y_next

    read*, h,w
    allocate(s(w,h), si(w,h),booked(w,h))
    input_s : block
        character(w):: s_row
        do i=1,h
            read*, s_row
            do j=1,w
                s(j,i) = s_row(j:j)
            end do
        end do
    end block input_s

    call init_deque(x_next)
    call init_deque(y_next)

    call append(x_next,1)
    call append(y_next,1)

    si(:,:) = 1000000000
    si(1,1) = 0
    a = 0
    if (s(1,1) == '#') a=a+1
    if (s(w,h) =='#') a=a+1

    booked(:,:) = .false.
    booked(1,1) = .true.
    bfs : block
        integer(int32):: x,y
        do while(remain(x_next))
            x = popleft(x_next)
            y = popleft(y_next)
            if (x+1 <= w) then
                if (.not. booked(x+1,y)) then
                    booked(x+1,y) = .true.
                    call append(x_next,x+1)
                    call append(y_next,y)
                end if
                if (s(x,y) == s(x+1,y)) then
                    si(x+1,y) = min(si(x,y),si(x+1,y))
                else
                    si(x+1,y) = min(si(x,y)+1,si(x+1,y))
                end if
            end if

            if (y+1 <= h) then
                if (.not. booked(x,y+1)) then
                    booked(x,y+1) = .true.
                    call append(x_next,x)
                    call append(y_next,y+1)
                end if
                if (s(x,y) == s(x,y+1)) then
                    si(x,y+1) = min(si(x,y),si(x,y+1))
                else
                    si(x,y+1) = min(si(x,y)+1,si(x,y+1))
                end if
            end if
        end do
    end block bfs

    print'(i0)', (si(w,h)-a)/2 + a
end program agc043a

Score : 400 points

Problem Statement

Consider a grid with H rows and W columns of squares. Let (r, c) denote the square at the r-th row from the top and the c-th column from the left.
Each square is painted black or white.

The grid is said to be good if and only if the following condition is satisfied:

From (1, 1), we can reach (H, W) by moving one square right or down repeatedly, while always being on a white square.

Note that (1, 1) and (H, W) must be white if the grid is good.

Your task is to make the grid good by repeating the operation below. Find the minimum number of operations needed to complete the task. It can be proved that you can always complete the task in a finite number of operations.

Choose four integers r_0, c_0, r_1, c_1(1 \leq r_0 \leq r_1 \leq H, 1 \leq c_0 \leq c_1 \leq W). For each pair r, c (r_0 \leq r \leq r_1, c_0 \leq c \leq c_1), invert the color of (r, c) - that is, from white to black and vice versa.

Constraints

2 \leq H, W \leq 100

Input

Input is given from Standard Input in the following format:

H W
s_{11} s_{12} \cdots s_{1W}
s_{21} s_{22} \cdots s_{2W}
\vdots
s_{H1} s_{H2} \cdots s_{HW}

Here s_{rc} represents the color of (r, c) - # stands for black, and . stands for white.

Output

Print the minimum number of operations needed.

Sample Input 1

3 3
.##
.#.
##.

Sample Output 1

1

Do the operation with (r_0, c_0, r_1, c_1) = (2, 2, 2, 2) to change just the color of (2, 2), and we are done.

Sample Input 2

2 2
#.
.#

Sample Output 2

2

Sample Input 3

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

Sample Output 3

0

No operation may be needed.

Sample Input 4

5 5
.#.#.
#.#.#
.#.#.
#.#.#
.#.#.

Sample Output 4

4


program main
    implicit none
    integer(4):: i, j, k
    integer(4):: h, w, cnt=0
    integer(4), parameter:: inf = 1000000
    integer(4), allocatable:: m(:,:), im(:,:), tmp(:,:), dp(:,:)
    character(100)::pcm
    character(1),allocatable:: cm(:,:)
    logical, allocatable:: check(:,:)
    logical:: flip

    read*, h, w
    allocate(cm(w,h), m(w,h), im(w,h), dp(w,h), check(w,h))

    do i=1, h
        read*, pcm
        do j=1, w
            if (pcm(j:j) == '.') then
                m(j,i) = 1
            else
                m(j,i) = 0
            end if
        end do
    end do

    dp(:,:) = inf

    flip = .false.
    call init(m(:,:),1,1, flip)

    dp(1,1) = 0
    if (flip) dp(1,1) = 1
    ! do i=1,h
    !     print*, m(:,i)
    ! end do

    do while(dp(w,h) >= inf)
        check(:,:) = .false.
        do i=1, h
            do j=1, w
                if (check(j,i) .or. dp(j,i) == inf) cycle

                if (m(j,i) == m(j,i+1) .and. i+1 <= h) then
                    dp(j, i+1) = dp(j, i)
                else if (i+1 <= h) then
                    dp(j,i+1) = min(dp(j,i)+1, dp(j,i+1))
                    check(j,i+1) = .true.
                end if

                if (m(j,i) == m(j+1, i) .and. j+1 <= w) then
                    dp(j+1, i) = dp(j, i)
                else if (j+1 <= w) then
                    dp(j+1,i) = min(dp(j,i)+1, dp(j+1,i))
                    check(j+1,i) = .true.
                end if
                ! print*, '-'
                ! print*, m(j,i) == m(j,i+1), i+1 <= h
                ! print*, m(j,i) == m(j+1, i), j+1 <= w
                ! do k=1, h
                !     print*, dp(:,k)
                ! end do
            end do
        end do
    !     print*, '-- --'

    ! do i=1, h
    !     print*, dp(:,i)
    ! end do

    ! do i=1, h
    !     print*, check(:,i)
    ! end do

        do i=1, h
            do j=1, w
                if (.not. check(j,i)) cycle

                if (i+1 <= h) dp(j,i+1) = min(dp(j,i), dp(j,i+1))
                if (j+1 <= w) dp(j+1,i) = min(dp(j,i), dp(j+1,i))
            end do
        end do

        ! print*, '---'
        ! do i=1,h
        !     print*, dp(:,i)
        ! end do

    end do
    print*, dp(w,h)

contains
recursive subroutine init(m, j, i, f)
    integer(4):: m(:,:),i,j
    logical:: f
    if (m(j,i) == 0) then
        f = .true.
        if (i+1 <= h) call init(m(:,:), j, i+1, f)
        if (j+1 <= w) call init(m(:,:), j+1, i, f)
        m(j,i) = 1
    end if

end subroutine init

end program main


program one_two_three_triangle
  implicit none
  integer :: n, a(1000000) = 0, i, j
  character(1000000) :: s
  read(*,*) n
  read(*,*) s
  do i = 1, n
    a(i) = ichar(s(i:i)) - 49
  end do
  if (mod(n, 2) == 0) then
    do i = 1, n - 1
      a(i) = abs(a(i) - a(i + 1))
    end do
    n = n - 1
  end if
  do i = n - 2, 1, -2
    do j = 1, i
      a(j) = abs(abs(a(j) - a(j + 1)) - abs(a(j + 1) - a(j + 2)))
    end do
  end do
  write(*,'(i0)') a(1)
end program one_two_three_triangle

Score : 700 points

Problem Statement

Given is a sequence of N digits a_1a_2\ldots a_N, where each element is 1, 2, or 3.
Let x_{i,j} defined as follows:

x_{1,j} := a_j \quad (1 \leq j \leq N)

x_{i,j} := | x_{i-1,j} - x_{i-1,j+1} | \quad (2 \leq i \leq N and 1 \leq j \leq N+1-i)

Find x_{N,1}.

Constraints

2 \leq N \leq 10^6

a_i = 1,2,3 (1 \leq i \leq N)

Input

Input is given from Standard Input in the following format:

N
a_1a_2\ldotsa_N

Output

Print x_{N,1}.

Sample Input 1

4
1231

Sample Output 1

1

x_{1,1},x_{1,2},x_{1,3},x_{1,4} are respectively 1,2,3,1.

x_{2,1},x_{2,2},x_{2,3} are respectively |1-2| = 1,|2-3| = 1,|3-1| = 2.

x_{3,1},x_{3,2} are respectively |1-1| = 0,|1-2| = 1.

Finally, x_{4,1} = |0-1| = 1, so the answer is 1.

Sample Input 2

10
2311312312

Sample Output 2

0


program one_two_three_triangle
  implicit none
  integer :: n, a(1000000) = 0, i, j, m = 0
  character(1000000) :: s
  read(*,*) n
  read(*,*) s
  do i = 1, n
    a(i) = ichar(s(i:i)) - 48
  end do
  do i = 1, n - 1
    m = m + abs(a(i) - a(i + 1))
  end do
  write(*,'(i0)') mod(m, 3)
end program one_two_three_triangle


program example
	implicit none

	integer(16) :: i=1,j=1,N
    integer(16),allocatable :: x(:,:),A(:)

    read(*,*) N

    allocate(x(N,N),A(N))

	do j=1,N
		read(*,"(i1)",advance="no",end=100) x(1,j)

    end do

100    do i=2,N

    	do j=1,N+1-i

        	x(i,j)=abs(x(i-1,j)-x(i-1,j+1))

        end do

    end do

    write(*,*) x(N,1)

end program


Score : 100 points

Problem Statement

Print the K-th element of the following sequence of length 32:

1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51

Constraints

1 \leq K \leq 32

All values in input are integers.

Input

Input is given from Standard Input in the following format:

K

Output

Print the K-th element.

Sample Input 1

6

Sample Output 1

2

The 6-th element is 2.

Sample Input 2

27

Sample Output 2

5

The 27-th element is 5.


   Program PanasonicB
   Implicit none
   Integer::i,j,k,l

      Read(*,*) i,j
      k=i*j
      If (MOD(k,2)==0) then
         l=k/2
      Else
         l=(k+1)/2
      End If

      Write(*,*) l
      Stop

   End Program PanasonicB

Score : 200 points

Problem Statement

We have a board with H horizontal rows and W vertical columns of squares.
There is a bishop at the top-left square on this board.
How many squares can this bishop reach by zero or more movements?

Here the bishop can only move diagonally.
More formally, the bishop can move from the square at the r_1-th row (from the top) and the c_1-th column (from the left) to the square at the r_2-th row and the c_2-th column if and only if exactly one of the following holds:

r_1 + c_1 = r_2 + c_2

r_1 - c_1 = r_2 - c_2

For example, in the following figure, the bishop can move to any of the red squares in one move:

Constraints

1 \leq H, W \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H \ W

Output

Print the number of squares the bishop can reach.

Sample Input 1

4 5

Sample Output 1

10

The bishop can reach the cyan squares in the following figure:

Sample Input 2

7 3

Sample Output 2

11

The bishop can reach the cyan squares in the following figure:

Sample Input 3

1000000000 1000000000

Sample Output 3

500000000000000000


program main_b
   implicit none
   integer(8) :: H, W

   read(*,*) H, W
   if (H==1 .or. W==1)  then
      print *, 1
      stop
   end if

   if (mod(H,2)==0) then
      print *, H/2*W
   else if (mod(W,2)==0) then
      print *, (H-1)/2*W + W/2
   else
      print *, (H-1)/2*W + (W/2)+1
   end if

end program main_b


program example
	implicit none

	integer(8) H,W

    read(*,*) H,W

    if (mod(H,2)==0) then
    	write(*,*) W*(H/2)
    else
    	if(mod(W,2)==0) then

    		write(*,*) (W*(H/2))+(W/2)
        else
        	write(*,*) (W*(H/2))+(W/2)+1
        end if

    end if

end program example


program abc
	implicit none
	integer :: h,w,i,j,a,b

	read(*,*) h,w

	b = 0

	do i = 1, h
    	if(mod(i,2) == 1) then
			if(mod(w,2) == 0) a = w / 2
			if(mod(w,2) == 1) a = (w + 1) / 2
    		b = b + a
		else
			if(mod(w,2) == 0) a = w / 2
			if(mod(w,2) == 1) a = (w - 1) / 2
    		b = b + a
		endif
	enddo

	write(*,*) b

end program abc


program main
  implicit none

  integer :: ak, bk, mk
  integer, allocatable :: a(:), b(:), m(:,:), total(:)
  integer :: i, j

  read (*,*) ak, bk, mk

  allocate(a(ak))
  allocate(b(bk))
  allocate(m(3,mk))

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

  if(mk <= bk .and. mk <= ak) then
     allocate(total(mk))
     do i = 1, mk
        total(i) = a(m(1,i)) + b(m(2,i)) - m(3,i)
        write(*,*) minval(total)

        deallocate(total)
    end do

 else
    if(ak >= bk) then
       allocate(total(bk))
       do i = 1, bk
          total(i) = a(m(1,i)) + b(m(2,i)) - m(3,i)
          write(*,*) minval(total)
          deallocate(total)
       end do

    else
       do i = 1, ak
          allocate(total(ak))
          total(i) = a(m(1,i)) + b(m(2,i)) - m(3,i)
          write(*,*) minval(total)

          deallocate(total)
       end do

    end if

 end if
 deallocate(a,b,m)
 stop

end program main

Score : 200 points

Problem Statement

You are visiting a large electronics store to buy a refrigerator and a microwave.

The store sells A kinds of refrigerators and B kinds of microwaves. The i-th refrigerator ( 1 \le i \le A ) is sold at a_i yen (the currency of Japan), and the j-th microwave ( 1 \le j \le B ) is sold at b_j yen.

You have M discount tickets. With the i-th ticket ( 1 \le i \le M ), you can get a discount of c_i yen from the total price when buying the x_i-th refrigerator and the y_i-th microwave together. Only one ticket can be used at a time.

You are planning to buy one refrigerator and one microwave. Find the minimum amount of money required.

Constraints

All values in input are integers.

1 \le A \le 10^5

1 \le B \le 10^5

1 \le M \le 10^5

1 \le a_i , b_i , c_i \le 10^5

1 \le x_i \le A

1 \le y_i \le B

c_i \le a_{x_i} + b_{y_i}

Input

Input is given from Standard Input in the following format:

A B M
a_1 a_2 ... a_A
b_1 b_2 ... b_B
x_1 y_1 c_1
\vdots
x_M y_M c_M

Output

Print the answer.

Sample Input 1

2 3 1
3 3
3 3 3
1 2 1

Sample Output 1

5

With the ticket, you can get the 1-st refrigerator and the 2-nd microwave for 3+3-1=5 yen.

Sample Input 2

1 1 2
10
10
1 1 5
1 1 10

Sample Output 2

10

Note that you cannot use more than one ticket at a time.

Sample Input 3

2 2 1
3 5
3 5
2 2 2

Sample Output 3

6

You can get the 1-st refrigerator and the 1-st microwave for 6 yen, which is the minimum amount to pay in this case.
Note that using a ticket is optional.


program prob2
    implicit none

    integer(8)::i,ans,disc,AA,BB,M
    integer(8), allocatable::A(:),B(:), C(:,:)
    read(*,*) AA, BB, M
    allocate(A(AA))
    allocate(B(BB))
    allocate(C(3,M))
    read(*,*) A
    read(*,*) B
    read(*,*) C

    ans = minval(A) + minval(B)

    do i = 1, M
        disc = A(C(1,i)) + B(C(2,i)) - C(3,i)
        if(disc < ans) then
            ans = disc
        end if
    end do

    write(*,*) ans

    deallocate(A)
    deallocate(B)
    deallocate(C)

    stop
end program prob2


program test
implicit none
integer::k,l,m,i,s
integer::ans=1000000
integer,allocatable::a(:),b(:),c(:),x(:),y(:)
read(5,*),k,l,m
allocate(a(k))
allocate(b(l))
allocate(c(m))
allocate(x(m))
allocate(y(m))

	read(5,*)(a(i),i=1,k)
	read(5,*)(b(i),i=1,l)
 	read(5,*)(x(i),y(i),c(i),i=1,m)

do i=1,m
    s=a(x(i))+b(y(i))-c(i)
    ans=min(s,ans)
end do

ans=min(minval(a)+minval(b),ans)
print*,ans
end program test


module mod_two_dim_merge_sort
  implicit none
  integer, private, parameter :: intkind = 8
contains
  logical function less(a, b) result(res)
    integer(intkind), intent(in) :: a(:), b(:)
    real(8) :: a1, b1
    a1 = a(1) / real(a(2) + 1, 8)
    b1 = b(1) / real(b(2) + 1, 8)
    res = a1 > b1 .or. (a1 == b1 .and. a(2) >= b(2))
  end
  subroutine merge_sort(a)
    integer(intkind), 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(intkind), intent(inout) :: a1(:, :), a2(:, :)
    integer(intkind) :: 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 (less(a1(i1, :), a2(i2, :))) 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
end module mod_two_dim_merge_sort
program manga_market
  use mod_two_dim_merge_sort
  implicit none
  integer(8), parameter :: inf = 3e18
  integer, parameter :: k = 30
  integer :: n, l, i, j, m = 0
  integer(8) :: t, r, tmp
  integer(8), allocatable :: c(:, :), dp(:), s(:)
  read(*,*) n, t
  allocate(c(n, 2))
  do i = 1, n
    read(*,*) c(i, :)
  end do
  call merge_sort(c(1:n, 1:2))
  l = n + 1
  do while (c(l - 1, 1) == 0)
    l = l - 1
  end do
  allocate(dp(0:k), s(0:n - l + 1))
  dp = inf
  dp(0) = 0
  do i = 1, l - 1
    do j = min(k, i), 1, -1
      if (dp(j - 1) > t) cycle
      tmp = dp(j - 1) + 1
      dp(j) = min(dp(j), tmp + c(i, 1) * tmp + c(i, 2))
    end do
  end do
  s(0) = 0
  do i = n, l, -1
    s(n - i + 1) = s(n - i) + c(i, 2) + 1
  end do
  do i = 0, min(k, l - 1)
    r = t - dp(i)
    if (r < 0) cycle
    do j = 0, n - l + 1
      if (s(j) > r) exit
      m = max(m, i + j)
    end do
  end do
  write(*,'(i0)') m
end program manga_market

Score : 800 points

Problem Statement

There are N stores called Store 1, Store 2, \cdots, Store N. Takahashi, who is at his house at time 0, is planning to visit some of these stores.

It takes Takahashi one unit of time to travel from his house to one of the stores, or between any two stores.

If Takahashi reaches Store i at time t, he can do shopping there after standing in a queue for a_i \times t + b_i units of time. (We assume that it takes no time other than waiting.)

All the stores close at time T + 0.5. If Takahashi is standing in a queue for some store then, he cannot do shopping there.

Takahashi does not do shopping more than once in the same store.

Find the maximum number of times he can do shopping before time T + 0.5.

Constraints

All values in input are integers.

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

0 \leq a_i \leq 10^9

0 \leq b_i \leq 10^9

0 \leq T \leq 10^9

Input

Input is given from Standard Input in the following format:

N T
a_1 b_1
a_2 b_2
\vdots
a_N b_N

Output

Print the answer.

Sample Input 1

3 7
2 0
3 2
0 3

Sample Output 1

2

Here is one possible way to visit stores:

From time 0 to time 1: in 1 unit of time, he travels from his house to Store 1.

From time 1 to time 3: for 2 units of time, he stands in a queue for Store 1 to do shopping.

From time 3 to time 4: in 1 unit of time, he travels from Store 1 to Store 3.

From time 4 to time 7: for 3 units of time, he stands in a queue for Store 3 to do shopping.

In this way, he can do shopping twice before time 7.5.

Sample Input 2

1 3
0 3

Sample Output 2

0

Sample Input 3

5 21600
2 14
3 22
1 3
1 10
1 9

Sample Output 3

5

Sample Input 4

7 57
0 25
3 10
2 4
5 15
3 22
2 14
1 15

Sample Output 4

3


program ABC158_A
  character(3)::  S
  read(5,*)S
  if(S(1:1)==S(2:2) .and. S(2:2)==S(3:3)) then
     write(6,*)'No'
  else
     write(6,*)'Yes'
  end if
end program ABC158_A

Score : 100 points

Problem Statement

In AtCoder City, there are three stations numbered 1, 2, and 3.

Each of these stations is operated by one of the two railway companies, A and B. A string S of length 3 represents which company operates each station. If S_i is A, Company A operates Station i; if S_i is B, Company B operates Station i.

To improve the transportation condition, for each pair of a station operated by Company A and one operated by Company B, there will be a bus service connecting them.

Determine if there is a pair of stations that will be connected by a bus service.

Constraints

Each character of S is A or B.

|S| = 3

Input

Input is given from Standard Input in the following format:

S

Output

If there is a pair of stations that will be connected by a bus service, print Yes; otherwise, print No.

Sample Input 1

ABA

Sample Output 1

Yes

Company A operates Station 1 and 3, while Company B operates Station 2.

There will be a bus service between Station 1 and 2, and between Station 2 and 3, so print Yes.

Sample Input 2

BBA

Sample Output 2

Yes

Company B operates Station 1 and 2, while Company A operates Station 3.

There will be a bus service between Station 1 and 3, and between Station 2 and 3, so print Yes.

Sample Input 3

BBB

Sample Output 3

No

Company B operates all the stations. Thus, there will be no bus service, so print No.


program test
implicit none
character::S*3
read*,S
	if(S=='AAA')then
    	print*,'No'
    else if(S == 'BBB')then
    	print*,'No'
    else
    	print*,'Yes'
    end if
end program test


program ABC158_B
  integer::A,B
  read(5,*)N,A,B
  K=N/(A+B)*A
  if(mod(N,A+B)<=A) then
     K=K+mod(N,A+B)
  else
     K=K+A
  end if
  write(6,*)K
end program ABC158_B

Score : 200 points

Problem Statement

Takahashi has many red balls and blue balls. Now, he will place them in a row.

Initially, there is no ball placed.

Takahashi, who is very patient, will do the following operation 10^{100} times:

Place A blue balls at the end of the row of balls already placed. Then, place B red balls at the end of the row.

How many blue balls will be there among the first N balls in the row of balls made this way?

Constraints

1 \leq N \leq 10^{18}

A, B \geq 0

0 < A + B \leq 10^{18}

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 blue balls that will be there among the first N balls in the row of balls.

Sample Input 1

8 3 4

Sample Output 1

4

Let b denote a blue ball, and r denote a red ball. The first eight balls in the row will be bbbrrrrb, among which there are four blue balls.

Sample Input 2

8 0 4

Sample Output 2

0

He placed only red balls from the beginning.

Sample Input 3

6 2 4

Sample Output 3

2

Among bbrrrr, there are two blue balls.


program main
 implicit none
 integer(8) :: a,b,n
  read *, n,a,b
   if (n<=a+b) then
    if (mod(n,a+b)==0) then
     write(*,'(i0)') b
    else if (mod(n,a+b)<=a) then
     write(*,'(i0)') 0
    else if (mod(n,a+b)>a) then
     write(*,'(i0)') mod(n,a+b)-a
    end if
   else if (n>a+b) then
    if (mod(n,a+b)==0) then
     write(*,'(i0)') b*n/(a+b)
    else if  (mod(n,a+b)<=a) then
     write(*,'(i0)') (n-mod(n,a+b))/(a+b)
    else if (mod(n,a+b)>a) then
     write(*,'(i0)') b*(n-mod(n,a+b))/(a+b)+mod(n,a+b)-a
    end if
   end if
  stop
end program main


program main
 implicit none
 integer(16) :: a,b,n
  read *, n,a,b
   if (n<=a+b) then
    if (mod(n,a+b)==0) then
     write(*,'(i0)') b
    else if (mod(n,a+b)<=a) then
     write(*,'(i0)') 0
    else if (mod(n,a+b)>a) then
     write(*,'(i0)') mod(n,a+b)-a
    end if
   else if (n>a+b) then
    if (mod(n,a+b)==0) then
     write(*,'(i0)') b*n/(a+b)
    else if  (mod(n,a+b)<=a) then
     write(*,'(i0)') (n-mod(n,a+b))/(a+b)
    else if (mod(n,a+b)>a) then
     write(*,'(i0)') b*(n-mod(n,a+b))/(a+b)+mod(n,a+b)-a
    end if
   end if
  stop
end program main


program main
integer :: n,a,b,c,d,e
read *,n,a,b

c = n/(a+b)
d=mod(n,(a+b))
e = c*a+min(d,a)
print '(i0)',e

end


program example
	implicit none

	integer(8) N,A,B,amari

    read(*,*)N,A,B

    if(A==0) then
    	write(*,*)"0"
    else
    	if(N/(A+B)==0) then
        	write(*,*) A
        else
    		amari=mod(N,A+B)

        	if(amari<=A) then
        		write(*,*) (N/(A+B))*A+amari
        	else
        		write(*,*) (N/(A+B))*A+A
        	end if
    	end if
    end if
end program example


program tax_increase
    integer a, b, i, ansa, ansb
    real m, n
    read *, a, b
    do i = 1, 1009
        ansa = int(i * 0.08)
        ansb = int(i * 0.1)
        if(ansa .eq. a .and. ansb .eq. b)then
            print *, i
            exit
        end if
    end do
end program

Score : 300 points

Problem Statement

Find the price of a product before tax such that, when the consumption tax rate is 8 percent and 10 percent, the amount of consumption tax levied on it is A yen and B yen, respectively. (Yen is the currency of Japan.)

Here, the price before tax must be a positive integer, and the amount of consumption tax is rounded down to the nearest integer.

If multiple prices satisfy the condition, print the lowest such price; if no price satisfies the condition, print -1.

Constraints

1 \leq A \leq B \leq 100

A and B are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

If there is a price that satisfies the condition, print an integer representing the lowest such price; otherwise, print -1.

Sample Input 1

2 2

Sample Output 1

25

If the price of a product before tax is 25 yen, the amount of consumption tax levied on it is:

When the consumption tax rate is 8 percent: \lfloor 25 \times 0.08 \rfloor = \lfloor 2 \rfloor = 2 yen.

When the consumption tax rate is 10 percent: \lfloor 25 \times 0.1 \rfloor = \lfloor 2.5 \rfloor = 2 yen.

Thus, the price of 25 yen satisfies the condition. There are other possible prices, such as 26 yen, but print the minimum such price, 25.

Sample Input 2

8 10

Sample Output 2

100

If the price of a product before tax is 100 yen, the amount of consumption tax levied on it is:

When the consumption tax rate is 8 percent: \lfloor 100 \times 0.08 \rfloor = 8 yen.

When the consumption tax rate is 10 percent: \lfloor 100 \times 0.1 \rfloor = 10 yen.

Sample Input 3

19 99

Sample Output 3

-1

There is no price before tax satisfying this condition, so print -1.