integer(8) N,M,ans
read*,N,M
if(N>=M*2)then
  ans=M/2
else
  ans=N+(M-2*N)/4
endif
print"(i0)",ans
end

Score : 300 points

Problem Statement

Snuke loves puzzles.

Today, he is working on a puzzle using S- and c-shaped pieces.
In this puzzle, you can combine two c-shaped pieces into one S-shaped piece, as shown in the figure below:

Snuke decided to create as many Scc groups as possible by putting together one S-shaped piece and two c-shaped pieces.

Find the maximum number of Scc groups that can be created when Snuke has N S-shaped pieces and M c-shaped pieces.

Constraints

1 ≤ N,M ≤ 10^{12}

Input

The input is given from Standard Input in the following format:

N M

Output

Print the answer.

Sample Input 1

1 6

Sample Output 1

2

Two Scc groups can be created as follows:

Combine two c-shaped pieces into one S-shaped piece

Create two Scc groups, each from one S-shaped piece and two c-shaped pieces

Sample Input 2

12345 678901

Sample Output 2

175897


integer N,M
read*,N,M
do while(2*N<M-1)
  N=N+1
  M=M-2
end do
print"(i0)",min(N,M/2)
end

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

Submitted source-code file size, in bytes.

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

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


program main
implicit none
integer :: n

  read(*,*) n
  call wolv_or_sheep(n)

contains

  subroutine wolv_or_sheep(n)
  integer,intent(in) :: n
  character(n)       :: s
  logical            :: t(n),sw(n)
  integer            :: i

    read(*,'(A)') s
    t(:) = [(s(i:i)=='o',i=1,n)]

    call test_seq( n, t, .TRUE.,  .TRUE.)
    call test_seq( n, t, .TRUE.,  .FALSE.)
    call test_seq( n, t, .FALSE., .TRUE.)
    call test_seq( n, t, .FALSE., .FALSE.)

    print'(I0)',-1

  end subroutine wolv_or_sheep

  subroutine test_seq(n,t,a1,a2)
  integer,intent(in) :: n
  logical,intent(in) :: t(n),a1,a2
  logical            :: res(n),accept
  integer            :: i

    res(1) = a1
    res(2) = a2

    do i=2,n-1
      res(i+1) = xor(res(i-1),t(i).neqv.res(i))
    enddo

    accept = xor( (res(n-1).eqv.res(1)).eqv.t(n), .not.res(n) )&
   &    .and.xor( (res(n)  .eqv.res(2)).eqv.t(1), .not.res(1) )

    if(accept)then
      do i=1,n
        if(res(i))then
          write(*,'(A)',advance='NO') 'S'
        else
          write(*,'(A)',advance='NO') 'W'
        endif
      enddo
      write(*,'(A)') ''
      STOP
    endif
!
  end subroutine test_seq
!
end program main

Score : 500 points

Problem Statement

Snuke, who loves animals, built a zoo.

There are N animals in this zoo. They are conveniently numbered 1 through N, and arranged in a circle.
The animal numbered i (2≤i≤N-1) is adjacent to the animals numbered i-1 and i+1. Also, the animal numbered 1 is adjacent to the animals numbered 2 and N, and the animal numbered N is adjacent to the animals numbered N-1 and 1.

There are two kinds of animals in this zoo: honest sheep that only speak the truth, and lying wolves that only tell lies.

Snuke cannot tell the difference between these two species, and asked each animal the following question: "Are your neighbors of the same species?" The animal numbered i answered s_i. Here, if s_i is o, the animal said that the two neighboring animals are of the same species, and if s_i is x, the animal said that the two neighboring animals are of different species.

More formally, a sheep answered o if the two neighboring animals are both sheep or both wolves, and answered x otherwise.
Similarly, a wolf answered x if the two neighboring animals are both sheep or both wolves, and answered o otherwise.

Snuke is wondering whether there is a valid assignment of species to the animals that is consistent with these responses. If there is such an assignment, show one such assignment. Otherwise, print -1.

Constraints

3 ≤ N ≤ 10^{5}

s is a string of length N consisting of o and x.

Input

The input is given from Standard Input in the following format:

N
s

Output

If there does not exist an valid assignment that is consistent with s, print -1.
Otherwise, print an string t in the following format. The output is considered correct if the assignment described by t is consistent with s.

t is a string of length N consisting of S and W.

If t_i is S, it indicates that the animal numbered i is a sheep. If t_i is W, it indicates that the animal numbered i is a wolf.

Sample Input 1

6
ooxoox

Sample Output 1

SSSWWS

For example, if the animals numbered 1, 2, 3, 4, 5 and 6 are respectively a sheep, sheep, sheep, wolf, wolf, and sheep, it is consistent with their responses. Besides, there is another valid assignment of species: a wolf, sheep, wolf, sheep, wolf and wolf.

Let us remind you: if the neiboring animals are of the same species, a sheep answers o and a wolf answers x. If the neiboring animals are of different species, a sheep answers x and a wolf answers o.

Sample Input 2

3
oox

Sample Output 2

-1

Print -1 if there is no valid assignment of species.

Sample Input 3

10
oxooxoxoox

Sample Output 3

SSWWSSSWWS


program frequency
  implicit none
  integer :: n, a(100001,2), m, i
  integer(8) :: s(100000)
  a = 0
  s = 0_8
  read(*,*) n
  read(*,*) a(1:n,1)
  do i = 1, n
    a(i,2) = i
  end do
  call merge_sort(a(1:n,1:2))
  m = n+1
  do i = 1, n
    m = min(m,a(i,2))
    s(m) = s(m)+int(i,8)*int(a(i,1)-a(i+1,1),8)
  end do
  do i = 1, n
    write(*,'(i0)') s(i)
  end do
  stop
contains
  subroutine merge_sort(c)
    implicit none
    integer, intent(inout) :: c(:,:)
    integer :: n, m, l, u, i
    n = size(c(:,1))
    m = n
    l = 1
    do while (m.gt.1)
      do i = 1, m/2
        u = min(2*i*l,n)
        call merger(c(2*(i-1)*l+1:(2*i-1)*l,:),c((2*i-1)*l+1:u,:))
      end do
      l = 2*l
      m = (m+1)/2
    end do
    return
  end subroutine merge_sort
  subroutine merger(c1,c2)
    implicit none
    integer, intent(inout) :: c1(:,:), c2(:,:)
    integer :: c(size(c1(:,1))+size(c2(:,1)),size(c2(1,:)))
    integer :: i1, i2, n1, n2
    i1 = 1
    i2 = 1
    n1 = size(c1(:,1))
    n2 = size(c2(:,1))
    do while ((i1.le.n1).and.(i2.le.n2))
      if (c1(i1,1).gt.c2(i2,1)) then
        c(i1+i2-1,:) = c1(i1,:)
        i1 = i1+1
      else if (c1(i1,1).lt.c2(i2,1)) then
        c(i1+i2-1,:) = c2(i2,:)
        i2 = i2+1
      else if (c1(i1,2).gt.c2(i2,2)) then
        c(i1+i2-1,:) = c1(i1,:)
        i1 = i1+1
      else
        c(i1+i2-1,:) = c2(i2,:)
        i2 = i2+1
      end if
    end do
    if (i1.le.n1) then
      c(i1+i2-1:n1+n2,:) = c1(i1:n1,:)
    else if (i2.le.n2) then
      c(i1+i2-1:n1+n2,:) = c2(i2:n2,:)
    end if
    c1 = c(1:n1,:)
    c2 = c(n1+1:n1+n2,:)
    return
  end subroutine merger
end program frequency

Score : 700 points

Problem Statement

Snuke loves constructing integer sequences.

There are N piles of stones, numbered 1 through N.
The pile numbered i consists of a_i stones.

Snuke will construct an integer sequence s of length Σa_i, as follows:

Among the piles with the largest number of stones remaining, let x be the index of the pile with the smallest index. Append x to the end of s.

Select a pile with one or more stones remaining, and remove a stone from that pile.

If there is a pile with one or more stones remaining, go back to step 1. Otherwise, terminate the process.

We are interested in the lexicographically smallest sequence that can be constructed. For each of the integers 1,2,3,...,N, how many times does it occur in the lexicographically smallest sequence?

Constraints

1 ≤ N ≤ 10^{5}

1 ≤ a_i ≤ 10^{9}

Input

The input is given from Standard Input in the following format:

N
a_1 a_2 ... a_{N}

Output

Print N lines. The i-th line should contain the number of the occurrences of the integer i in the lexicographically smallest sequence that can be constructed.

Sample Input 1

2
1 2

Sample Output 1

2
1

The lexicographically smallest sequence is constructed as follows:

Since the pile with the largest number of stones remaining is pile 2, append 2 to the end of s. Then, remove a stone from pile 2.

Since the piles with the largest number of stones remaining are pile 1 and 2, append 1 to the end of s (we take the smallest index). Then, remove a stone from pile 2.

Since the pile with the largest number of stones remaining is pile 1, append 1 to the end of s. Then, remove a stone from pile 1.

The resulting sequence is (2,1,1). In this sequence, 1 occurs twice, and 2 occurs once.

Sample Input 2

10
1 2 1 3 2 4 2 5 8 1

Sample Output 2

10
7
0
4
0
3
0
2
3
0


program judge
 implicit none
 integer :: a, b

 read(*,*)a, b

 if (a==b) then
  write(*,*)"Draw"
 else if (a==1) then
  write(*,*)"Alice"
 else if (b==1) then
  write(*,*)"Bob"
 else if (a>b) then
  write(*,*)"Alice"
 else
  write(*,*)"Bob"
 end if

end program judge

Score : 100 points

Problem Statement

Alice and Bob are playing One Card Poker.

One Card Poker is a two-player game using playing cards.

Each card in this game shows an integer between 1 and 13, inclusive.

The strength of a card is determined by the number written on it, as follows:

Weak 2 < 3 < 4 < 5 < 6 < 7 < 8 < 9 < 10 < 11 < 12 < 13 < 1 Strong

One Card Poker is played as follows:

Each player picks one card from the deck. The chosen card becomes the player's hand.

The players reveal their hands to each other. The player with the stronger card wins the game.

If their cards are equally strong, the game is drawn.

You are watching Alice and Bob playing the game, and can see their hands.

The number written on Alice's card is A, and the number written on Bob's card is B.

Write a program to determine the outcome of the game.

Constraints

1≦A≦13

1≦B≦13

A and B are integers.

Input

The input is given from Standard Input in the following format:

A B

Output

Print Alice if Alice will win. Print Bob if Bob will win. Print Draw if the game will be drawn.

Sample Input 1

8 6

Sample Output 1

Alice

8 is written on Alice's card, and 6 is written on Bob's card.
Alice has the stronger card, and thus the output should be Alice.

Sample Input 2

1 1

Sample Output 2

Draw

Since their cards have the same number, the game will be drawn.

Sample Input 3

13 1

Sample Output 3

Bob


read*,i,j
if(mod(i+11,13)>mod(j+11,13))then
print"(a)","Alice"
else if(mod(i+11,13)<mod(j+11,13))then
print"(a)","Bob"
else
print"(a)","Draw"
endif
end


program abc054b
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: n,m,i,j
    character(1),allocatable:: a(:,:), b(:,:)

    read*, n,m
    allocate(a(n,n),b(m,m))
    input_char : block
        character(n):: inpt_a
        character(m):: inpt_b
        do i=1,n
            read*, inpt_a
            do j=1,n
                a(j,i) = inpt_a(j:j)
            end do
        end do
        do i=1,m
            read*, inpt_b
            do j=1,m
                b(j,i) = inpt_b(j:j)
            end do
        end do
    end block input_char

    do i=1,n-m+1
        do j=1,n-m+1
            if (fit(i,j))then
                print'(a)', 'Yes'
                stop
            end if
        end do
    end do

    print'(a)', 'No'

contains
    function fit(fx,fy) result(ret)
        integer(int32):: fx,fy
        integer(int32):: x,y
        logical:: ret
        ret = .true.
        do x=1,m
            do y=1,m
                if (b(x,y) /= a(x+fx-1,y+fy-1)) then
                    ret = .false.
                    return
                end if
            end do
        end do
    end function
end program abc054b

Score : 200 points

Problem Statement

You are given an image A composed of N rows and N columns of pixels, and a template image B composed of M rows and M columns of pixels.

A pixel is the smallest element of an image, and in this problem it is a square of size 1×1.

Also, the given images are binary images, and the color of each pixel is either white or black.

In the input, every pixel is represented by a character: . corresponds to a white pixel, and # corresponds to a black pixel.

The image A is given as N strings A_1,...,A_N.

The j-th character in the string A_i corresponds to the pixel at the i-th row and j-th column of the image A (1≦i,j≦N).

Similarly, the template image B is given as M strings B_1,...,B_M.

The j-th character in the string B_i corresponds to the pixel at the i-th row and j-th column of the template image B (1≦i,j≦M).

Determine whether the template image B is contained in the image A when only parallel shifts can be applied to the images.

Constraints

1≦M≦N≦50

A_i is a string of length N consisting of # and ..

B_i is a string of length M consisting of # and ..

Input

The input is given from Standard Input in the following format:

N M
A_1
A_2
:
A_N
B_1
B_2
:
B_M

Output

Print Yes if the template image B is contained in the image A. Print No otherwise.

Sample Input 1

3 2
#.#
.#.
#.#
#.
.#

Sample Output 1

Yes

The template image B is identical to the upper-left 2 × 2 subimage and the lower-right 2 × 2 subimage of A. Thus, the output should be Yes.

Sample Input 2

4 1
....
....
....
....
#

Sample Output 2

No

The template image B, composed of a black pixel, is not contained in the image A composed of white pixels.


program mixing_experiment
  implicit none
  integer :: n, ma, mb, a, b, c, dp(0:400,0:400)
  integer :: i, ia, ib, m
  integer, parameter :: infty = 10000000
  dp = infty
  read(*,*) n, ma, mb
  dp(0,0) = 0
  do i = 1, n
    read(*,*) a, b, c
    do ia = 400, a, -1
      do ib = 400, b, -1
        dp(ia,ib) = min(dp(ia,ib),dp(ia-a,ib-b)+c)
      end do
    end do
  end do
  m = infty
  do i = 1, 400/ma
    ia = i*ma
    ib = i*mb
    if (ib.gt.400) exit
    m = min(dp(ia,ib),m)
  end do
  if (m.eq.infty) then
    write(*,'(i0)') -1
  else
    write(*,'(i0)') m
  end if
  stop
end program mixing_experiment

Score : 400 points

Problem Statement

Dolphin is planning to generate a small amount of a certain chemical substance C.

In order to generate the substance C, he must prepare a solution which is a mixture of two substances A and B in the ratio of M_a:M_b.

He does not have any stock of chemicals, however, so he will purchase some chemicals at a local pharmacy.

The pharmacy sells N kinds of chemicals. For each kind of chemical, there is exactly one package of that chemical in stock.

The package of chemical i contains a_i grams of the substance A and b_i grams of the substance B, and is sold for c_i yen (the currency of Japan).

Dolphin will purchase some of these packages. For some reason, he must use all contents of the purchased packages to generate the substance C.

Find the minimum amount of money required to generate the substance C.

If it is not possible to generate the substance C by purchasing any combination of packages at the pharmacy, report that fact.

Constraints

1≦N≦40

1≦a_i,b_i≦10

1≦c_i≦100

1≦M_a,M_b≦10

gcd(M_a,M_b)=1

a_i, b_i, c_i, M_a and M_b are integers.

Input

The input is given from Standard Input in the following format:

N M_a M_b
a_1 b_1 c_1
a_2 b_2 c_2
:
a_N b_N c_N

Output

Print the minimum amount of money required to generate the substance C. If it is not possible to generate the substance C, print -1 instead.

Sample Input 1

3 1 1
1 2 1
2 1 2
3 3 10

Sample Output 1

3

The amount of money spent will be minimized by purchasing the packages of chemicals 1 and 2.

In this case, the mixture of the purchased chemicals will contain 3 grams of the substance A and 3 grams of the substance B, which are in the desired ratio: 3:3=1:1.

The total price of these packages is 3 yen.

Sample Input 2

1 1 10
10 10 10

Sample Output 2

-1

The ratio 1:10 of the two substances A and B cannot be satisfied by purchasing any combination of the packages. Thus, the output should be -1.


program arc068
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: n,cnt(1:100000),i
    integer(int32), allocatable:: a(:)

    read*, n
    allocate(a(n))
    read*, a(:)
    cnt(:) = 0
    do i=1,n
        cnt(a(i))=cnt(a(i))+1
    end do
    where (cnt > 0)
        cnt=cnt-1
    end where
    ! print'(*(i0,1x))', cnt(1:50)
    ! print'(i0)', sum(cnt(:))

    print'(i0)', n - 2*((sum(cnt))/2 + mod(sum(cnt),2))
end program arc068

Score : 400 points

Problem Statement

Snuke has decided to play a game using cards.
He has a deck consisting of N cards. On the i-th card from the top, an integer A_i is written.

He will perform the operation described below zero or more times, so that the values written on the remaining cards will be pairwise distinct. Find the maximum possible number of remaining cards. Here, N is odd, which guarantees that at least one card can be kept.

Operation: Take out three arbitrary cards from the deck. Among those three cards, eat two: one with the largest value, and another with the smallest value. Then, return the remaining one card to the deck.

Constraints

3 ≦ N ≦ 10^{5}

N is odd.

1 ≦ A_i ≦ 10^{5}

A_i is an integer.

Input

The input is given from Standard Input in the following format:

N
A_1 A_2 A_3 ... A_{N}

Output

Print the answer.

Sample Input 1

5
1 2 1 3 7

Sample Output 1

3

One optimal solution is to perform the operation once, taking out two cards with 1 and one card with 2. One card with 1 and another with 2 will be eaten, and the remaining card with 1 will be returned to deck. Then, the values written on the remaining cards in the deck will be pairwise distinct: 1, 3 and 7.

Sample Input 2

15
1 3 5 2 1 3 2 8 8 6 2 6 11 1 1

Sample Output 2

7


Score : 100 points

Problem Statement

There are two rectangles.
The lengths of the vertical sides of the first rectangle are A, and the lengths of the horizontal sides of the first rectangle are B.
The lengths of the vertical sides of the second rectangle are C, and the lengths of the horizontal sides of the second rectangle are D.

Print the area of the rectangle with the larger area.
If the two rectangles have equal areas, print that area.

Constraints

All input values are integers.

1≤A≤10^4

1≤B≤10^4

1≤C≤10^4

1≤D≤10^4

Input

The input is given from Standard Input in the following format:

A B C D

Output

Print the area of the rectangle with the larger area.
If the two rectangles have equal areas, print that area.

Sample Input 1

3 5 2 7

Sample Output 1

15

The first rectangle has an area of 3×5=15, and the second rectangle has an area of 2×7=14.
Thus, the output should be 15, the larger area.

Sample Input 2

100 600 200 300

Sample Output 2

60000


implicit none

integer::a,b,c,d
read(5,*) a,b,c,d
write(6,*) max(a*b,c*d)

end


program main
  implicit none
  integer :: n, ans, x, i
  character(100) :: s

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

  x = 0
  ans = 0

  do i= 1, n
     if(s(i:i) == 'I')then
        x = x + 1
     else
        x = x - 1
     end if

     ans = max(ans,x)
  end do
  write(*,*) ans
end program main

Score : 200 points

Problem Statement

You have an integer variable x.
Initially, x=0.

Some person gave you a string S of length N, and using the string you performed the following operation N times.
In the i-th operation, you incremented the value of x by 1 if S_i=I, and decremented the value of x by 1 if S_i=D.

Find the maximum value taken by x during the operations (including before the first operation, and after the last operation).

Constraints

1≤N≤100

|S|=N

No characters except I and D occur in S.

Input

The input is given from Standard Input in the following format:

N
S

Output

Print the maximum value taken by x during the operations.

Sample Input 1

5
IIDID

Sample Output 1

2

After each operation, the value of x becomes 1, 2, 1, 2 and 1, respectively. Thus, the output should be 2, the maximum value.

Sample Input 2

7
DDIDDII

Sample Output 2

0

The initial value x=0 is the maximum value taken by x, thus the output should be 0.


program prob1
    implicit none
    character(len=19)::S
    character(len=5)::S1,S3
    character(len=7)::S2
    character(len=1)::a,b
    read(*,*) S1,S2,S3
    S(1:5) = S1
    S(6:6) = " "
    S(7:13) = S2
    S(14:14) = " "
    S(15:19) = S3
    write(*,'(a)') S
    stop
end program

Score : 100 points

Problem Statement

As a New Year's gift, Dolphin received a string s of length 19.

The string s has the following format: [five lowercase English letters],[seven lowercase English letters],[five lowercase English letters].

Dolphin wants to convert the comma-separated string s into a space-separated string.

Write a program to perform the conversion for him.

Constraints

The length of s is 19.

The sixth and fourteenth characters in s are ,.

The other characters in s are lowercase English letters.

Input

The input is given from Standard Input in the following format:

s

Output

Print the string after the conversion.

Sample Input 1

happy,newyear,enjoy

Sample Output 1

happy newyear enjoy

Replace all the commas in happy,newyear,enjoy with spaces to obtain happy newyear enjoy.

Sample Input 2

haiku,atcoder,tasks

Sample Output 2

haiku atcoder tasks

Sample Input 3

abcde,fghihgf,edcba

Sample Output 3

abcde fghihgf edcba


program prob1
    implicit none
    character(19) :: s, t, u, v
    read(*,*) s, t, u
    v = trim(adjustl(s)) // ' ' // trim(adjustl(t)) // ' ' // trim(adjustl(u))
    write(*,'(a)') v

    stop
contains
end program prob1


program ABC051A
    implicit none
    character(5)::S
    character(6)::U
    character(7)::T
    read(5,*)S,T,U
    print'(A,1x,A,1x,A)',S,T,U
end program ABC051A


character(5) ::a,c
character(7) :: b
read*,a,b,c

print*,a//' '//b//' '//c
end


character a*5,b*7,c*5;read*,a,b,c;print*,a//" "//b//" "//c;end


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

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

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

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

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

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

  interface graph
    module procedure :: newg
  end interface graph

contains

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

end module mod_graph
program abc051d
    use mod_graph
    implicit none
    integer::N,M
    integer,allocatable,dimension(:)::A,B
    integer(8),allocatable,dimension(:)::C
    integer(8),allocatable,dimension(:,:)::D
    integer::ans
    type(graph)::G
    integer::i,j
    logical::USED
    read*,N,M
    allocate(A(N),B(N),C(N),D(N,N))
    G=graph(N)
    do i=1,M
        read*,A(i),B(i),C(i)
        call add(G,A(i),B(i),C(i))
        call add(G,B(i),A(i),C(i))
    end do
    do i=1,N
        D(i,:)=dijkstra(G,i)
    end do
    ans=0
    do i=1,M
        USED=.FALSE.
        do j=1,N
            if(D(j,A(i))+C(i)==D(j,B(i)))USED=.TRUE.
        end do
        IF(.not. USED)ANS=ANS+1
    end do
    print"(i0)",ans

end program abc051d

Score : 400 points

Problem Statement

You are given an undirected connected weighted graph with N vertices and M edges that contains neither self-loops nor double edges.

The i-th (1≤i≤M) edge connects vertex a_i and vertex b_i with a distance of c_i.

Here, a self-loop is an edge where a_i = b_i (1≤i≤M), and double edges are two edges where (a_i,b_i)=(a_j,b_j) or (a_i,b_i)=(b_j,a_j) (1≤i<j≤M).

A connected graph is a graph where there is a path between every pair of different vertices.

Find the number of the edges that are not contained in any shortest path between any pair of different vertices.

Constraints

2≤N≤100

N-1≤M≤min(N(N-1)/2,1000)

1≤a_i,b_i≤N

1≤c_i≤1000

c_i is an integer.

The given graph contains neither self-loops nor double edges.

The given graph is connected.

Input

The input is given from Standard Input in the following format:

N M
a_1 b_1 c_1
a_2 b_2 c_2
:
a_M b_M c_M

Output

Print the number of the edges in the graph that are not contained in any shortest path between any pair of different vertices.

Sample Input 1

3 3
1 2 1
1 3 1
2 3 3

Sample Output 1

1

In the given graph, the shortest paths between all pairs of different vertices are as follows:

The shortest path from vertex 1 to vertex 2 is: vertex 1 → vertex 2, with the length of 1.

The shortest path from vertex 1 to vertex 3 is: vertex 1 → vertex 3, with the length of 1.

The shortest path from vertex 2 to vertex 1 is: vertex 2 → vertex 1, with the length of 1.

The shortest path from vertex 2 to vertex 3 is: vertex 2 → vertex 1 → vertex 3, with the length of 2.

The shortest path from vertex 3 to vertex 1 is: vertex 3 → vertex 1, with the length of 1.

The shortest path from vertex 3 to vertex 2 is: vertex 3 → vertex 1 → vertex 2, with the length of 2.

Thus, the only edge that is not contained in any shortest path, is the edge of length 3 connecting vertex 2 and vertex 3, hence the output should be 1.

Sample Input 2

3 2
1 2 1
2 3 1

Sample Output 2

0

Every edge is contained in some shortest path between some pair of different vertices.


program AGC008A
    implicit none
    integer(8)::x,y
    integer(8)::result=0
    read(5,*)x,y

    result=result+abs(abs(x)-abs(y))

    if(x*y>0 .and. x>y)then
        result=result+2
    else if(x*y<0)then
        result=result+1
    end if

    print'(i0)',result

end program AGC008A

Score : 300 points

Problem Statement

Snuke has a calculator. It has a display and two buttons.

Initially, the display shows an integer x.
Snuke wants to change this value into another integer y, by pressing the following two buttons some number of times in arbitrary order:

Button A: When pressed, the value on the display is incremented by 1.

Button B: When pressed, the sign of the value on the display is reversed.

Find the minimum number of times Snuke needs to press the buttons to achieve his objective.
It can be shown that the objective is always achievable regardless of the values of the integers x and y.

Constraints

x and y are integers.

|x|, |y| ≤ 10^9

x and y are different.

Input

The input is given from Standard Input in the following format:

x y

Output

Print the minimum number of times Snuke needs to press the buttons to achieve his objective.

Sample Input 1

10 20

Sample Output 1

10

Press button A ten times.

Sample Input 2

10 -10

Sample Output 2

1

Press button B once.

Sample Input 3

-10 -20

Sample Output 3

12

Press the buttons as follows:

Press button B once.

Press button A ten times.

Press button B once.


program prob2
    implicit none
    integer(8)::N,i,j,M,ans,P,X
    integer(8),allocatable::T(:)
    read(*,*) N
    allocate(T(N))
    read(*,*) T
    ans = sum(T)
    read(*,*) M
    do i = 1,M
        read(*,*) P,X
        write(*,*) ans - T(P) + X
    end do
    stop
end program

Score : 200 points

Problem Statement

Joisino is about to compete in the final round of a certain programming competition.
In this contest, there are N problems, numbered 1 through N.
Joisino knows that it takes her T_i seconds to solve problem i(1≦i≦N).

Also, there are M kinds of drinks offered to the contestants, numbered 1 through M.
If Joisino takes drink i(1≦i≦M), her brain will be stimulated and the time it takes for her to solve problem P_i will become X_i seconds.
It does not affect the time to solve the other problems.

A contestant is allowed to take exactly one of the drinks before the start of the contest.
For each drink, Joisino wants to know how many seconds it takes her to solve all the problems if she takes that drink.
Here, assume that the time it takes her to solve all the problems is equal to the sum of the time it takes for her to solve individual problems.
Your task is to write a program to calculate it instead of her.

Constraints

All input values are integers.

1≦N≦100

1≦T_i≦10^5

1≦M≦100

1≦P_i≦N

1≦X_i≦10^5

Input

The input is given from Standard Input in the following format:

N
T_1 T_2 ... T_N
M
P_1 X_1
P_2 X_2
:
P_M X_M

Output

For each drink, calculate how many seconds it takes Joisino to solve all the problems if she takes that drink, and print the results, one per line.

Sample Input 1

3
2 1 4
2
1 1
2 3

Sample Output 1

6
9

If Joisino takes drink 1, the time it takes her to solve each problem will be 1, 1 and 4 seconds, respectively, totaling 6 seconds.

If Joisino takes drink 2, the time it takes her to solve each problem will be 2, 3 and 4 seconds, respectively, totaling 9 seconds.

Sample Input 2

5
7 2 3 8 5
3
4 2
1 7
4 13

Sample Output 2

19
25
30


program main
  implicit none

  integer :: n,t(100)=0,m,p(100)=0,x(100)=0,i,j,s=0

  read(*,*)n
  read(*,*)(t(i),i=1,n)
  read(*,*)m
  do i= 1, m
    read(*,*)p(i),x(i)
  end do

  do i = 1, n
    s = s + t(i)
  end do

  do i = 1, m
    write(*,*)s - t(p(i)) + x(i)
  end do
end program main


program main
implicit none
integer :: i, j, k
integer :: n, m
integer , allocatable :: x(:), t(:), p(:)

read(*,*) n
allocate( x(n) )
read(*,*) x
read(*,*) m
allocate( t(m), p(m) )
j = sum( x )
do i = 1, m
   read(*,*) t(i), p(i)
   k = t(i)
   write(*,'(i0)') j - x(k) + p(i)
end do

end program main


PROGRAM ATCODER

implicit none
integer :: n,m,p,x,i,sum,j
integer,allocatable :: t(:)

read*, n
allocate(t(n))
read*, (t(i), i=1,n)
read*, m

do i = 1, m
  sum = 0
  read*, p,x
  do j = 1, n
    if ( p == j ) then
      sum = sum + x
    else
      sum = sum + t(j)
    end if
  end do
  print'(i0)', sum
end do

contains
!!!!!! サブルーチンは↓
!!!!!!

END PROGRAM ATCODER


program main
	implicit none
	integer :: N
	integer, allocatable :: A(:)
	integer :: i, ans

	read(*, *) N
	allocate(A(1:N+1))
	read(*, *) A(1:N)
	A(N+1) = 1000000
	ans = 1

	call heapsort(N+1,A)
	if(mod(N,2) == 0) then
		do i = 1, N/2
			if(A(2*(i-1)+1) /= A(2*i) .OR. A(2*(i-1)+1) /= 2*i-1) then
				write(*, *) 0
				goto 10
			end if
		end do
		do i = 1, N/2
			ans = mod(ans,1000000007)*2
		end do
		write(*, *) ans
	else
		if(A(1) /= 0) then
			write(*, *) 0
			goto 10
		end if
		do i = 1, (N-1)/2
			if(A(2*i) /= A(2*i+1) .OR. A(2*i) /= 2*i) then
				write(*, *) 0
				goto 10
			end if
		end do
		do i = 1, N/2
			ans = mod(ans,1000000007)*2
		end do
		write(*, *) ans
	end if

	10 continue

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

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

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

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

		return
	end subroutine heapsort
end program main

Score : 300 points

Problem Statement

There are N people, conveniently numbered 1 through N.
They were standing in a row yesterday, but now they are unsure of the order in which they were standing.
However, each person remembered the following fact: the absolute difference of the number of the people who were standing to the left of that person, and the number of the people who were standing to the right of that person.
According to their reports, the difference above for person i is A_i.

Based on these reports, find the number of the possible orders in which they were standing.
Since it can be extremely large, print the answer modulo 10^9+7.
Note that the reports may be incorrect and thus there may be no consistent order.
In such a case, print 0.

Constraints

1≦N≦10^5

0≦A_i≦N-1

Input

The input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the number of the possible orders in which they were standing, modulo 10^9+7.

Sample Input 1

5
2 4 4 0 2

Sample Output 1

4

There are four possible orders, as follows:

2,1,4,5,3

2,5,4,1,3

3,1,4,5,2

3,5,4,1,2

Sample Input 2

7
6 4 0 2 4 0 2

Sample Output 2

0

Any order would be inconsistent with the reports, thus the answer is 0.

Sample Input 3

8
7 5 1 1 7 3 5 3

Sample Output 3

16


program main
implicit none
integer :: i, j, k
character :: s*1

read(*,*) s
if( s == 'a' .or. s=='i' .or. s =='u' .or. s=='e' .or. s=='o' ) then
   write(*,*) "vowel"
else
   write(*,*) "consonant"
end if

end program main

Score : 100 points

Problem Statement

Given a lowercase English letter c, determine whether it is a vowel. Here, there are five vowels in the English alphabet: a, e, i, o and u.

Constraints

c is a lowercase English letter.

Input

The input is given from Standard Input in the following format:

c

Output

If c is a vowel, print vowel. Otherwise, print consonant.

Sample Input 1

a

Sample Output 1

vowel

Since a is a vowel, print vowel.

Sample Input 2

z

Sample Output 2

consonant

Sample Input 3

s

Sample Output 3

consonant


module mod1
implicit none
integer(8) :: par(200000),par2(200000),par_total(200000),num(200000),ans(200000)=0
end module mod1

program test
use mod1
implicit none
integer(8) :: n,i,j,k,l,last,kind
integer(8) :: p(100000),q(100000),r(100000),s(100000)

read(*,*) n,k,l
!道路
do i=1,k
	read(*,*) p(i),q(i)
enddo
!鉄道
do i=1,l
	read(*,*) r(i),s(i)
enddo

do i=1,n
	par(i) = i
	par2(i) = i
enddo

!道路
do i=1,k
	call unite(p(i),q(i))
enddo

!鉄道
do i=1,l
	call unite2(r(i),s(i))
enddo

do i=1,n
	num(i) = i
	par_total(i) = 1000000*par(i) + par2(i)
enddo

call heapsort2(n,par_total(1:n),num(1:n))

!種類を数える！
do i=1,n
	if(i==1) then
		last=1
		kind=1
	else if(par_total(i) .eq. par_total(i-1)) then
		kind = kind + 1
	else
		do j = last,i-1
			ans(num(j)) = ans(num(j)) + kind
		enddo
		kind = 1
		last = i
	endif

	!最後の処理
	if(i==n) then
		do j = last,i
			ans(num(j)) = ans(num(j)) + kind
		enddo
	endif
enddo

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

contains
!道路
recursive function root(x) result(res)
integer(8) :: x,res
	if(par(x) == x) then
		continue
	else
		par(x) = root(par(x))
	endif
	res = par(x)
end function

subroutine unite(x,y)
integer(8) :: x,y
x = root(x)
y = root(y)
if(x > y) then
	par(x) = y
	par(y) = y
else if (x < y) then
	par(y) = x
	par(x) = x
endif
end subroutine

!鉄道
recursive function root2(x) result(res)
integer(8) :: x,res
	if(par2(x) == x) then
		continue
	else
		par2(x) = root2(par2(x))
	endif
	res = par2(x)
end function

subroutine unite2(x,y)
integer(8) :: x,y
x = root2(x)
y = root2(y)
if(x > y) then
	par(x) = y
	par(y) = y
else if (x < y) then
	par(y) = x
	par(x) = x
endif
end subroutine

subroutine heapsort2(n,y1,y2)

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

	l = n/2+1
	k = n

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

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

		 i = l
		 j = l+l

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

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

		 enddo

		 y1(i) = t1
		 y2(i) = t2

	enddo
	return

end subroutine heapsort2

end program test

Score : 400 points

Problem Statement

There are N cities. There are also K roads and L railways, extending between the cities.
The i-th road bidirectionally connects the p_i-th and q_i-th cities, and the i-th railway bidirectionally connects the r_i-th and s_i-th cities.
No two roads connect the same pair of cities. Similarly, no two railways connect the same pair of cities.

We will say city A and B are connected by roads if city B is reachable from city A by traversing some number of roads. Here, any city is considered to be connected to itself by roads.
We will also define connectivity by railways similarly.

For each city, find the number of the cities connected to that city by both roads and railways.

Constraints

2 ≦ N ≦ 2*10^5

1 ≦ K, L≦ 10^5

1 ≦ p_i, q_i, r_i, s_i ≦ N

p_i < q_i

r_i < s_i

When i ≠ j, (p_i, q_i) ≠ (p_j, q_j)

When i ≠ j, (r_i, s_i) ≠ (r_j, s_j)

Input

The input is given from Standard Input in the following format:

N K L
p_1 q_1
:
p_K q_K
r_1 s_1
:
r_L s_L

Output

Print N integers. The i-th of them should represent the number of the cities connected to the i-th city by both roads and railways.

Sample Input 1

4 3 1
1 2
2 3
3 4
2 3

Sample Output 1

1 2 2 1

All the four cities are connected to each other by roads.

By railways, only the second and third cities are connected. Thus, the answers for the cities are 1, 2, 2 and 1, respectively.

Sample Input 2

4 2 2
1 2
2 3
1 4
2 3

Sample Output 2

1 2 2 1

Sample Input 3

7 4 4
1 2
2 3
2 5
6 7
3 5
4 5
3 4
6 7

Sample Output 3

1 1 2 1 2 2 2


Score : 900 points

Problem Statement

There is a string S of length N consisting of characters 0 and 1. You will perform the following operation for each i = 1, 2, ..., m:

Arbitrarily permute the characters within the substring of S starting at the l_i-th character from the left and extending through the r_i-th character.

Here, the sequence l_i is non-decreasing.

How many values are possible for S after the M operations, modulo 1000000007(= 10^9+7)?

Constraints

2≦N≦3000

1≦M≦3000

S consists of characters 0 and 1.

The length of S equals N.

1≦l_i < r_i≦N

l_i ≦ l_{i+1}

Input

The input is given from Standard Input in the following format:

N M
S
l_1 r_1
:
l_M r_M

Output

Print the number of the possible values for S after the M operations, modulo 1000000007.

Sample Input 1

5 2
01001
2 4
3 5

Sample Output 1

6

After the first operation, S can be one of the following three: 01001, 00101 and 00011.

After the second operation, S can be one of the following six: 01100, 01010, 01001, 00011, 00101 and 00110.

Sample Input 2

9 3
110111110
1 4
4 6
6 9

Sample Output 2

26

Sample Input 3

11 6
00101000110
2 4
2 3
4 7
5 6
6 10
10 11

Sample Output 3

143


module modulo_util
  implicit none
  integer(8), parameter :: md = 1000000007_8
  integer(8), allocatable :: f(:), invf(:)
contains
  subroutine init(n)
    integer, intent(in) :: n
    integer :: i
    if (allocated(f)) deallocate(f)
    if (allocated(invf)) deallocate(invf)
    allocate(f(0:n),invf(0:n))
    f = 0_8
    invf = 0_8
    f(0) = 1_8
    do i = 1, n
      f(i) = mod(f(i-1)*int(i,8),md)
    end do
    invf(n) = inv(f(n))
    do i = n, 1, -1
      invf(i-1) = mod(invf(i)*int(i,8),md)
    end do
  end
  integer(8) function perm(n,k)
    integer, intent(in) :: n, k
    perm = 0_8
    if (k > n .or. n < 0 .or. k < 0) return
    perm = mod(f(n)*invf(n-k),md)
  end
  integer(8) function comb(n,k)
    integer, intent(in) :: n, k
    comb = mod(perm(n,k)*invf(k),md)
  end
  integer(8) function homo(n,k)
    integer, intent(in) :: n, k
    homo = 1_8
    if (n == 0 .and. k == 0) return
    homo = comb(n+k-1,k)
  end
  function inv(n) result(y)
    integer(8), intent(in) :: n
    integer(8) :: a, b, x, y, t, q
    a = mod(n,md)
    b = md
    x = 0_8
    y = 1_8
    do while (b /= 0_8)
      q = a/b
      t = b
      b = mod(a,b)
      a = t
      t = y
      y = x
      x = t-q*x
    end do
    y = mod(mod(y,md)+md,md)
  end
  function pow(a,b) result(r)
    integer(8), intent(in) :: a
    integer, intent(in) :: b
    integer :: i
    integer(8) :: r, p
    r = 1_8
    p = a
    do i = 0, 32
      if (btest(b,i)) r = mod(r*p,md)
      p = mod(p*p,md)
    end do
  end
end module modulo_util
program shuffling
  use modulo_util
  implicit none
  integer :: n, m, u = 0, ll, rr, i, j
  integer, dimension(0:3000) :: l = 0, r = 0, c = 0
  character(3000) :: s
  integer(8) :: ans = 1_8
  read(*,*) n, m
  call init(n)
  read(*,*) s
  do i = 1, n
    c(i) = c(i-1)+ichar(s(i:i))-48
  end do
  do i = 1, m
    read(*,*) ll, rr
    if (rr <= r(u)) cycle
    u = u+1
    l(u) = ll
    r(u) = rr
  end do
  i = 1
  do while (i <= u)
    j = i
    do while (j < u .and. l(j+1) <= r(j))
      j = j+1
    end do
    ans = mod(ans*solve(l(i:j),r(i:j),1,0),md)
    i = j+1
  end do
  write(*,'(i0)') ans+1_8
contains
  recursive function solve(l,r,i,d) result(ret)
    implicit none
    integer, intent(in) :: l(:), r(:), i, d
    integer :: j, x
    integer(8) :: ret
    if (i == 1) then
      x = c(r(i))-c(l(i)-1)
    else
      x = d+c(r(i))-c(r(i-1))
    end if
    if (i == size(l)) then
      ret = comb(r(i)-l(i)+1,x)
      return
    end if
    ret = 0_8
    do j = max(0,x-(r(i+1)-l(i)+1)), min(x,r(i)-l(i+1)+1)
      ret = ret+mod(solve(l,r,i+1,j)*comb(l(i+1)-l(i),x-j),md)
    end do
    ret = mod(ret,md)
  end
end program shuffling


program main
	implicit none
    character::a,b,c
    read(*,*) a,b,c
    write(*,*) 'A'//b//'C'
    stop
end program main

Score : 100 points

Problem Statement

Snuke is going to open a contest named "AtCoder s Contest".
Here, s is a string of length 1 or greater, where the first character is an uppercase English letter, and the second and subsequent characters are lowercase English letters.

Snuke has decided to abbreviate the name of the contest as "AxC".
Here, x is the uppercase English letter at the beginning of s.

Given the name of the contest, print the abbreviation of the name.

Constraints

The length of s is between 1 and 100, inclusive.

The first character in s is an uppercase English letter.

The second and subsequent characters in s are lowercase English letters.

Input

The input is given from Standard Input in the following format:

AtCoder s Contest

Output

Print the abbreviation of the name of the contest.

Sample Input 1

AtCoder Beginner Contest

Sample Output 1

ABC

The contest in which you are participating now.

Sample Input 2

AtCoder Snuke Contest

Sample Output 2

ASC

This contest does not actually exist.

Sample Input 3

AtCoder X Contest

Sample Output 3

AXC


character(100) :: s
read'(a)',s
print*,s(9:9)
end


integer(16) :: a,b,x
read*,a,b,x

print*,b/x - a/x
end

Score : 200 points

Problem Statement

You are given nonnegative integers a and b (a ≤ b), and a positive integer x.
Among the integers between a and b, inclusive, how many are divisible by x?

Constraints

0 ≤ a ≤ b ≤ 10^{18}

1 ≤ x ≤ 10^{18}

Input

The input is given from Standard Input in the following format:

a b x

Output

Print the number of the integers between a and b, inclusive, that are divisible by x.

Sample Input 1

4 8 2

Sample Output 1

3

There are three integers between 4 and 8, inclusive, that are divisible by 2: 4, 6 and 8.

Sample Input 2

0 5 1

Sample Output 2

6

There are six integers between 0 and 5, inclusive, that are divisible by 1: 0, 1, 2, 3, 4 and 5.

Sample Input 3

9 9 2

Sample Output 3

0

There are no integer between 9 and 9, inclusive, that is divisible by 2.

Sample Input 4

1 1000000000000000000 3

Sample Output 4

333333333333333333

Watch out for integer overflows.


program abc048b
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: a,b,x
    read*, a,b,x
    print'(i0)', b/x - (min(a-1,0_8))/x
end program abc048b


program prob6
    implicit none
    integer(8)::N,x,i, tmp, ans
    integer(8),allocatable::a(:)
    read(*,*) N,x
    allocate(a(N))
    read(*,*) a

    ans = 0
    do i = 2, N
        tmp = a(i-1) + a(i)
        if(tmp > a(i)) then
            if(tmp - x > a(i)) then
                a(i) = 0
            else
                a(i) = a(i) - (tmp - x)
            end if
            ans = ans + tmp - x
        end if
    end do

    write(*,*) ans

    stop
end program

Score : 300 points

Problem Statement

There are N boxes arranged in a row.
Initially, the i-th box from the left contains a_i candies.

Snuke can perform the following operation any number of times:

Choose a box containing at least one candy, and eat one of the candies in the chosen box.

His objective is as follows:

Any two neighboring boxes contain at most x candies in total.

Find the minimum number of operations required to achieve the objective.

Constraints

2 ≤ N ≤ 10^5

0 ≤ a_i ≤ 10^9

0 ≤ x ≤ 10^9

Input

The input is given from Standard Input in the following format:

N x
a_1 a_2 ... a_N

Output

Print the minimum number of operations required to achieve the objective.

Sample Input 1

3 3
2 2 2

Sample Output 1

1

Eat one candy in the second box.
Then, the number of candies in each box becomes (2, 1, 2).

Sample Input 2

6 1
1 6 1 2 0 4

Sample Output 2

11

For example, eat six candies in the second box, two in the fourth box, and three in the sixth box.
Then, the number of candies in each box becomes (1, 0, 1, 0, 0, 1).

Sample Input 3

5 9
3 1 4 1 5

Sample Output 3

0

The objective is already achieved without performing operations.

Sample Input 4

2 0
5 5

Sample Output 4

10

All the candies need to be eaten.


PROGRAM ATCODER

implicit none
integer(16) :: n,x,i,ans=0
integer(16),allocatable :: a(:),b(:,:)

read(*,*) n,x
allocate(a(n),b(n-1,n))
read(*,*) (a(i), i=1,n)

do i = 1, n
  if ( a(i) >x ) then
    ans = ans + a(i) - x
    a(i) = x
  end if
end do

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

do i = 1,n-2
  if ( b(i,i+1) > x ) then
    ans = ans + b(i,i+1) - x
    b(i+1,i+2) = b(i+1,i+2) - (b(i,i+1) - x)
  end if
end do

if ( b(n-1,n) > x ) then
  ans = ans + b(n-1,n) - x
end if

write(*,'(i0)') ans

contains
!!!!!! サブルーチンは↓
!!!!!!

END PROGRAM ATCODER


program rotated_palindromes
  implicit none
  integer(8), parameter :: md = 1000000007_8
  integer :: n, i, j, d(1500) = 0, m = 0
  integer(8) :: k, num(1500) = 0_8, ans = 0_8
  read(*,*) n, k
  i = 1
  do while (i*i <= n)
    if (mod(n,i) == 0) then
      m = m+1
      d(m) = i
      if (i*i == n) exit
      m = m+1
      d(m) = n/i
    end if
    i = i+1
  end do
  call quick_sort(d(1:m))
  do i = 1, m
    num(i) = pow(k,(d(i)+1)/2)
    do j = 1, i-1
      if (mod(d(i),d(j)) == 0) num(i) = modulo(num(i)-num(j),md)
    end do
  end do
  do i = 1, m
    ans = mod(ans+num(i)*int(d(i)/(2-mod(d(i),2)),8),md)
  end do
  write(*,'(i0)') ans
contains
  function pow(a,b) result(r)
    integer(8), intent(in) :: a
    integer, intent(in) :: b
    integer :: k
    integer(8) :: r, p
    r = 1_8
    p = a
    k = b
    do while (k > 0)
      if (btest(k,0)) r = mod(r*p,md)
      p = mod(p*p,md)
      k = rshift(k,1)
    end do
  end
  recursive subroutine quick_sort(a)
    integer, intent(inout) :: a(:)
    integer :: n, l, r, m
    integer :: p
    n = size(a)
    l = 1
    r = n
    m = (n+1)/2
    p = a(l)+a(m)+a(r)-max(a(l),a(m),a(r))-min(a(l),a(m),a(r))
    do
      do while (a(l) < p)
        l = l+1
      end do
      do while (a(r) > p)
        r = r-1
      end do
      if (l >= r) exit
      a(l) = xor(a(l),a(r))
      a(r) = xor(a(l),a(r))
      a(l) = xor(a(l),a(r))
      l = l+1
      r = r-1
    end do
    if (l-1 > 1) call quick_sort(a(1:l-1))
    if (n > r+1) call quick_sort(a(r+1:n))
  end
end program rotated_palindromes

Score : 1000 points

Problem Statement

Takahashi and Aoki are going to together construct a sequence of integers.

First, Takahashi will provide a sequence of integers a, satisfying all of the following conditions:

The length of a is N.

Each element in a is an integer between 1 and K, inclusive.

a is a palindrome, that is, reversing the order of elements in a will result in the same sequence as the original.

Then, Aoki will perform the following operation an arbitrary number of times:

Move the first element in a to the end of a.

How many sequences a can be obtained after this procedure, modulo 10^9+7?

Constraints

1≤N≤10^9

1≤K≤10^9

Input

The input is given from Standard Input in the following format:

N K

Output

Print the number of the sequences a that can be obtained after the procedure, modulo 10^9+7.

Sample Input 1

4 2

Sample Output 1

6

The following six sequences can be obtained:

(1, 1, 1, 1)

(1, 1, 2, 2)

(1, 2, 2, 1)

(2, 2, 1, 1)

(2, 1, 1, 2)

(2, 2, 2, 2)

Sample Input 2

1 10

Sample Output 2

10

Sample Input 3

6 3

Sample Output 3

75

Sample Input 4

1000000000 1000000000

Sample Output 4

875699961


program main
  implicit none
  integer(16) dp(0:300,0:300),a(0:300,0:300)
  integer(16) N
  integer(16) r,ret,i,j,k
  integer(16) pw(0:300)
  integer(16)::mo=1000000007
  read*,N
  pw(0)=1_16
  do i=1,n+1
    pw(i)=mod(pw(i-1)*2_16,mo)
  end do
  dp=0_16
  dp(0,0)=1_16
  do j=0,n-1
    do k=0,j
  	  dp(j+1,k+1)=mod(dp(j+1,k+1)+mod(dp(j,k)*(pw(n)-pw(k)),mo),mo)
  	  dp(j+1,k)=mod(dp(j+1,k)+mod(dp(j,k)*pw(k),mo),mo)
	end do
  end do
  a=0
  do i=0,n-1
    read*,a(i,0:N-1)
  end do
  r=gauss(a,N)
  ret=0
  do i= r,n
    ret=mod(ret+mod(dp(n,i)*dp(i,r),mo)*powmod(2_16,n*n-i*n,mo),mo)
  end do
  ret=mod(ret*powmod(dp(n,r),mo-2_16,mo),mo)

  if(ret<=0)ret=ret+mo
  print"(i0)",ret

contains
function powmod(aa,b,mo)
  implicit none
  integer(16) aa,b,mo,ak,bk
  integer(16) powmod
  powmod=1_16
  bk=b
  ak=mod(aa,mo)
  do while(bk/=0_16)
    if(mod(bk,2)==1)then
      powmod=mod(powmod*ak,mo)
    endif
    ak=mod(ak*ak,mo)
    bk=bk/2_16
  end do
end function

function gauss(a,N)
  implicit none
  integer(16) N,a(0:300,0:300)
  integer(16) gauss
  integer(16) p,i,j,k
  integer(16) fg
  p=0
  do i=0,n-1
	fg=0
	do j=p,n-1
      if (a(j,i)>0)then
        do k=0,n-1
          call swap(a(p,k),a(j,k))
        end do
        fg=1
		exit
      endif
    end do
	if (fg==0) cycle
    do j=p+1,n-1
      if (a(j,i)>0)then
	    do k=0,n-1
          a(j,k) = xor(a(j,k),a(p,k))
        end do
	  endif
    end do
	p=p+1
  end do
  gauss=p
end function

subroutine swap(a,b)
  implicit none
  integer(16) a,b
  a=xor(a,b)
  b=xor(a,b)
  a=xor(a,b)
end subroutine
end

Score : 1500 points

Problem Statement

#nck {
width: 30px;
height: auto;
}

Snuke received two matrices A and B as birthday presents.
Each of the matrices is an N by N matrix that consists of only 0 and 1.

Then he computed the product of the two matrices, C = AB.
Since he performed all computations in modulo two, C was also an N by N matrix that consists of only 0 and 1.
For each 1 ≤ i, j ≤ N, you are given c_{i, j}, the (i, j)-element of the matrix C.

However, Snuke accidentally ate the two matrices A and B, and now he only knows C.
Compute the number of possible (ordered) pairs of the two matrices A and B, modulo 10^9+7.

Constraints

1 ≤ N ≤ 300

c_{i, j} is either 0 or 1.

Input

The input is given from Standard Input in the following format:

N
c_{1, 1} ... c_{1, N}
:
c_{N, 1} ... c_{N, N}

Output

Print the number of possible (ordered) pairs of two matrices A and B (modulo 10^9+7).

Sample Input 1

2
0 1
1 0

Sample Output 1

6

Sample Input 2

10
1 0 0 1 1 1 0 0 1 0
0 0 0 1 1 0 0 0 1 0
0 0 1 1 1 1 1 1 1 1
0 1 0 1 0 0 0 1 1 0
0 0 1 0 1 1 1 1 1 1
1 0 0 0 0 1 0 0 0 0
1 1 1 0 1 0 0 0 0 1
0 0 0 1 0 0 1 0 1 0
0 0 0 1 1 1 0 0 0 0
1 0 1 0 0 1 1 1 1 1

Sample Output 2

741992411


program struck_out
  implicit none
  integer :: n, m, k, i, j, l, s, t
  integer(8) :: a(100000), dp(0:100000,300), deq(100000), nxt
  a = 0_8
  dp = 0_8
  deq = 0_8
  read(*,*) n, m, k
  read(*,*) a(1:n)
  dp(1:n,1) = a(1:n)
  do j = 2, k
    s = 1
    t = 1
    do i = 1, n
      if (s.eq.t) then
        dp(i,j) = -1_8
      else
        dp(i,j) = int(j,8)*a(i)+dp(deq(s),j-1)
        if (i+1-deq(s).gt.m) s = s+1
      end if
      nxt = dp(i,j-1)
      if (nxt.ne.-1_8) then
        do while (s.ne.t.and.dp(deq(t-1),j-1).lt.nxt)
          t = t-1
        end do
        deq(t) = i
        t = t+1
      end if
    end do
  end do
  write(*,'(i0)') maxval(dp(1:n,k))
  stop
end program struck_out

Score : 700 points

Problem Statement

There are N panels arranged in a row in Takahashi's house, numbered 1 through N. The i-th panel has a number A_i written on it. Takahashi is playing by throwing balls at these panels.

Takahashi threw a ball K times. Let the panel hit by a boll in the i-th throw be panel p_i. He set the score for the i-th throw as i \times A_{p_i}.

He was about to calculate the total score for his throws, when he realized that he forgot the panels hit by balls, p_1,p_2,...,p_K. The only fact he remembers is that for every i (1 ≦ i ≦ K-1), 1 ≦ p_{i+1}-p_i ≦ M holds. Based on this fact, find the maximum possible total score for his throws.

Constraints

1 ≦ M ≦ N ≦ 100,000

1 ≦ K ≦ min(300,N)

1 ≦ A_i ≦ 10^{9}

Partial Scores

In the test set worth 100 points, M = N.

In the test set worth another 200 points, N ≦ 300 and K ≦ 30.

In the test set worth another 300 points, K ≦ 30.

Input

The input is given from Standard Input in the following format:

N M K
A_1 A_2 … A_N

Output

Print the maximum possible total score for Takahashi's throws.

Sample Input 1

5 2 3
10 2 8 10 2

Sample Output 1

56

The total score is maximized when panels 1,3 and 4 are hit, in this order.

Sample Input 2

5 5 2
5 2 10 5 9

Sample Output 2

28

This case satisfies the additional constraint M = N for a partial score.

Sample Input 3

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

Sample Output 3

5000000078


program prob1
    implicit none
    integer::h, w, i, j, tmp
    character(len=20)::x
    character(len=5), allocatable::s(:)
    read(*,*) h, w
    allocate(s(w))
    do i = 1, h
        read(*,*) s
        do j = 1, w
            if(s(j) .eq. "snuke") then
                write(x,*) i
                write(*,'(2a)') char(ichar("A") + j - 1), trim(adjustl(x))
                stop
            end if
        end do
    end do
    deallocate(s)
    contains
end program

Score : 100 points

Problem Statement

There is a grid with H rows and W columns.

The square at the i-th row and j-th column contains a string S_{i,j} of length 5.

The rows are labeled with the numbers from 1 through H, and the columns are labeled with the uppercase English letters from A through the W-th letter of the alphabet.

Exactly one of the squares in the grid contains the string snuke. Find this square and report its location.

For example, the square at the 6-th row and 8-th column should be reported as H6.

Constraints

1≦H, W≦26

The length of S_{i,j} is 5.

S_{i,j} consists of lowercase English letters (a-z).

Exactly one of the given strings is equal to snuke.

Input

The input is given from Standard Input in the following format:

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

Output

Print the labels of the row and the column of the square containing the string snuke, with no space inbetween.

Sample Input 1

15 10
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snuke snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake

Sample Output 1

H6

Sample Input 2

1 1
snuke

Sample Output 2

A1


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

    read*, n
    print'(i0)', n/2+1
    if (n - (n/2+1) > 0) print'(i0)', n - (n/2+1)
end program Exactly_N_points

Score : 300 points

Problem Statement

The problem set at CODE FESTIVAL 20XX Finals consists of N problems.

The score allocated to the i-th (1≦i≦N) problem is i points.

Takahashi, a contestant, is trying to score exactly N points. For that, he is deciding which problems to solve.

As problems with higher scores are harder, he wants to minimize the highest score of a problem among the ones solved by him.

Determine the set of problems that should be solved.

Constraints

1≦N≦10^7

Partial Score

200 points will be awarded for passing the test set satisfying 1≦N≦1000.

Additional 100 points will be awarded for passing the test set without additional constraints.

Input

The input is given from Standard Input in the following format:

N

Output

Among the sets of problems with the total score of N, find a set in which the highest score of a problem is minimum, then print the indices of the problems in the set in any order, one per line.

If there exists more than one such set, any of them will be accepted.

Sample Input 1

4

Sample Output 1

1
3

Solving only the 4-th problem will also result in the total score of 4 points, but solving the 1-st and 3-rd problems will lower the highest score of a solved problem.

Sample Input 2

7

Sample Output 2

1
2
4

The set \{3,4\} will also be accepted.

Sample Input 3

1

Sample Output 3

1


program exactly_n_points
  implicit none
  integer :: n, i, s, j
  read(*,*) n
  s = 0
  do i = 1, n
    s = s+i
    if (s.ge.n) then
      j = i
      exit
    end if
  end do
  do i = 1, j
    if (i.eq.s-n) cycle
    write(*,'(i0)') i
  end do
  stop
end program exactly_n_points


integer,dimension(3)::x!３つの成分を持つ配列としてxを宣言
read*,x!なんかこれで勝手に読み込んでくれる
if (maxval(x)*2==sum(x)) then!maxval(x)は配列xの最大値,sum(x)は配列xの合計値
	print"(A)","Yes"
else
	print"(A)","No"
endif
end

Score : 100 points

Problem Statement

Two students of AtCoder Kindergarten are fighting over candy packs.

There are three candy packs, each of which contains a, b, and c candies, respectively.

Teacher Evi is trying to distribute the packs between the two students so that each student gets the same number of candies. Determine whether it is possible.

Note that Evi cannot take candies out of the packs, and the whole contents of each pack must be given to one of the students.

Constraints

1 ≦ a, b, c ≦ 100

Input

The input is given from Standard Input in the following format:

a b c

Output

If it is possible to distribute the packs so that each student gets the same number of candies, print Yes. Otherwise, print No.

Sample Input 1

10 30 20

Sample Output 1

Yes

Give the pack with 30 candies to one student, and give the two packs with 10 and 20 candies to the other. Then, each gets 30 candies.

Sample Input 2

30 30 100

Sample Output 2

No

In this case, the student who gets the pack with 100 candies always has more candies than the other.

Note that every pack must be given to one of them.

Sample Input 3

56 25 31

Sample Output 3

Yes


implicit none
character(100) s
integer :: i,j,n
read *,s
n = len_trim(s)
i = scan(s(1:n),'C')
j = scan(s(1:n),'F')
if (i == 0 .or. j == 0 .or. j<i) then
   print '(a)','No'
else
   print '(a)','Yes'
endif
stop
end

Score : 100 points

Problem Statement

This contest is CODEFESTIVAL, which can be shortened to the string CF by deleting some characters.

Mr. Takahashi, full of curiosity, wondered if he could obtain CF from other strings in the same way.

You are given a string s consisting of uppercase English letters.
Determine whether the string CF can be obtained from the string s by deleting some characters.

Constraints

2 ≤ |s| ≤ 100

All characters in s are uppercase English letters (A-Z).

Input

The input is given from Standard Input in the following format:

s

Output

Print Yes if the string CF can be obtained from the string s by deleting some characters.
Otherwise print No.

Sample Input 1

CODEFESTIVAL

Sample Output 1

Yes

CF is obtained by deleting characters other than the first character C and the fifth character F.

Sample Input 2

FESTIVALCODE

Sample Output 2

No

FC can be obtained but CF cannot be obtained because you cannot change the order of the characters.

Sample Input 3

CF

Sample Output 3

Yes

It is also possible not to delete any characters.

Sample Input 4

FCF

Sample Output 4

Yes

CF is obtained by deleting the first character.


program main
implicit none
integer :: i, j, k
integer :: a(3)

read(*,*) a
k = 0
do i = 1, 2
   do j = i+1, 3
      if( a(i) /= a(j) ) then
         k = k + 1
      end if
   end do
end do
write(*,'(i0)') k

end program main

Score : 100 points

Problem Statement

AtCoDeer the deer recently bought three paint cans.
The color of the one he bought two days ago is a, the color of the one he bought yesterday is b, and the color of the one he bought today is c.
Here, the color of each paint can is represented by an integer between 1 and 100, inclusive.

Since he is forgetful, he might have bought more than one paint can in the same color.
Count the number of different kinds of colors of these paint cans and tell him.

Constraints

1≦a,b,c≦100

Input

The input is given from Standard Input in the following format:

a b c

Output

Print the number of different kinds of colors of the paint cans.

Sample Input 1

3 1 4

Sample Output 1

3

Three different colors: 1, 3, and 4.

Sample Input 2

3 3 33

Sample Output 2

2

Two different colors: 3 and 33.


program main
  implicit none
  integer(8) :: n, i, x, y
  integer(8), allocatable :: a(:), t(:)
  read(*, *) n
  allocate(a(n), t(n))
  do i = 1, n
    read(*, *) t(i), a(i)
  end do
  x = 1
  y = 1
  do i = 1, n
    call update(x, y, t(i), a(i))
  end do
  write(*, "(i0)") x + y
  deallocate(a, t)

  contains

  subroutine update(x, y, t, a)
    implicit none
    integer(8), intent(inout) :: x, y
    integer(8), intent(in) :: t, a
    integer(8) :: ma, mb, mult
    ma = x / t
    if (mod(x, t) > 0) ma = ma + 1
    mb = y / a
    if (mod(y, a) > 0) mb = mb + 1
    mult = max(ma, mb)
    x = t * mult
    y = a * mult
  end subroutine update
end

Score : 300 points

Problem Statement

AtCoDeer the deer is seeing a quick report of election results on TV.
Two candidates are standing for the election: Takahashi and Aoki.
The report shows the ratio of the current numbers of votes the two candidates have obtained, but not the actual numbers of votes.
AtCoDeer has checked the report N times, and when he checked it for the i-th (1≦i≦N) time, the ratio was T_i:A_i.
It is known that each candidate had at least one vote when he checked the report for the first time.

Find the minimum possible total number of votes obtained by the two candidates when he checked the report for the N-th time.
It can be assumed that the number of votes obtained by each candidate never decreases.

Constraints

1≦N≦1000

1≦T_i,A_i≦1000 (1≦i≦N)

T_i and A_i (1≦i≦N) are coprime.

It is guaranteed that the correct answer is at most 10^{18}.

Input

The input is given from Standard Input in the following format:

N
T_1 A_1
T_2 A_2
:
T_N A_N

Output

Print the minimum possible total number of votes obtained by Takahashi and Aoki when AtCoDeer checked the report for the N-th time.

Sample Input 1

3
2 3
1 1
3 2

Sample Output 1

10

When the numbers of votes obtained by the two candidates change as 2,3 → 3,3 → 6,4, the total number of votes at the end is 10, which is the minimum possible number.

Sample Input 2

4
1 1
1 1
1 5
1 100

Sample Output 2

101

It is possible that neither candidate obtained a vote between the moment when he checked the report, and the moment when he checked it for the next time.

Sample Input 3

5
3 10
48 17
31 199
231 23
3 2

Sample Output 3

6930


program sample
    implicit none
    real(8)::z
    integer(8) :: a,b,c,i,j,m,n,k,x,y
    character::s !文字列

    read(*,*)n,a,b

    read(*,*)s
    x=0
    y=0
    k=len_trim(s)
    do i=1,k
        if (s(i:i)=='a')then
            if (x+y<a+b)then
                x=x+1
                write(*,*)'Yes'
            else
                write(*,*)'No'
            end if
        else if (s(i:i)=='b')then
            if (x+y<a+b)then
                if (y<b)then
                    y=y+1
                    write(*,*)'Yes'
                end if
            else
                write(*,*)'No'
            end if

        else
            write(*,*)'No'
        end if

    end do

    stop
end program sample

Score : 200 points

Problem Statement

There are N participants in the CODE FESTIVAL 2016 Qualification contests. The participants are either students in Japan, students from overseas, or neither of these.

Only Japanese students or overseas students can pass the Qualification contests. The students pass when they satisfy the conditions listed below, from the top rank down. Participants who are not students cannot pass the Qualification contests.

A Japanese student passes the Qualification contests if the number of the participants who have already definitively passed is currently fewer than A+B.

An overseas student passes the Qualification contests if the number of the participants who have already definitively passed is currently fewer than A+B and the student ranks B-th or above among all overseas students.

A string S is assigned indicating attributes of all participants. If the i-th character of string S is a, this means the participant ranked i-th in the Qualification contests is a Japanese student; b means the participant ranked i-th is an overseas student; and c means the participant ranked i-th is neither of these.

Write a program that outputs for all the participants in descending rank either Yes if they passed the Qualification contests or No if they did not pass.

Constraints

1≦N,A,B≦100000

A+B≦N

S is N characters long.

S consists only of the letters a, b and c.

Input

Inputs are provided from Standard Input in the following form.

N A B
S

Output

Output N lines. On the i-th line, output Yes if the i-th participant passed the Qualification contests or No if that participant did not pass.

Sample Input 1

10 2 3
abccabaabb

Sample Output 1

Yes
Yes
No
No
Yes
Yes
Yes
No
No
No

The first, second, fifth, sixth, and seventh participants pass the Qualification contests.

Sample Input 2

12 5 2
cabbabaacaba

Sample Output 2

No
Yes
Yes
Yes
Yes
No
Yes
Yes
No
Yes
No
No

The sixth participant is third among overseas students and thus does not pass the Qualification contests.

Sample Input 3

5 2 2
ccccc

Sample Output 3

No
No
No
No
No


program gridian_mst
  implicit none
  integer :: w, h, i, a, b
  integer(8) :: c(200000,2), m
  c = 0_8
  read(*,*) w, h
  do i = 1, w
    read(*,*) c(i,1)
    c(i,2) = -1_8
  end do
  do i = w+1, w+h
    read(*,*) c(i,1)
    c(i,2) = 1_8
  end do
  call quick_sort(c(1:w+h,1:2),1)
  a = w+1
  b = h+1
  m = 0_8
  do i = 1, w+h
    if (c(i,2).lt.0_8) then
      if (a.eq.0) cycle
      m = m+c(i,1)*int(b,8)
      a = a-1
    else
      if (b.eq.0) cycle
      m = m+c(i,1)*int(a,8)
      b = b-1
    end if
  end do
  write(*,'(i0)') m
  stop
contains
  recursive subroutine quick_sort(a,i)
    implicit none
    integer(8), intent(inout) :: a(:,:)
    integer, intent(in) :: i
    integer(8) p, c(size(a(1,:)))
    integer :: n, l, r
    n = size(a(:,1))
    l = 1
    r = n
    p = a((l+r)/2,i)+a(l,i)+a(r,i)-max(a((l+r)/2,i),a(l,i),a(r,i))-min(a((l+r)/2,i),a(l,i),a(r,i))
    do
      do while (a(l,i).lt.p)
        l = l+1
      end do
      do while (a(r,i).gt.p)
        r = r-1
      end do
      if (l.ge.r) exit
      c = a(l,:)
      a(l,:) = a(r,:)
      a(r,:) = c
      l = l+1
      r = r-1
    end do
    if (l.gt.2) call quick_sort(a(1:l-1,:),i)
    if (n.gt.r+1) call quick_sort(a(r+1:n,:),i)
    return
  end subroutine quick_sort
end program gridian_mst

Score : 500 points

Problem Statement

On an xy plane, in an area satisfying 0 ≤ x ≤ W, 0 ≤ y ≤ H, there is one house at each and every point where both x and y are integers.

There are unpaved roads between every pair of points for which either the x coordinates are equal and the difference between the y coordinates is 1, or the y coordinates are equal and the difference between the x coordinates is 1.

The cost of paving a road between houses on coordinates (i,j) and (i+1,j) is p_i for any value of j, while the cost of paving a road between houses on coordinates (i,j) and (i,j+1) is q_j for any value of i.

Mr. Takahashi wants to pave some of these roads and be able to travel between any two houses on paved roads only. Find the solution with the minimum total cost.

Constraints

1 ≦ W,H ≦ 10^5

1 ≦ p_i ≦ 10^8(0 ≦ i ≦ W-1)

1 ≦ q_j ≦ 10^8(0 ≦ j ≦ H-1)

p_i (0 ≦ i ≦ W−1) is an integer.

q_j (0 ≦ j ≦ H−1) is an integer.

Input

Inputs are provided from Standard Input in the following form.

W H
p_0
:
p_{W-1}
q_0
:
q_{H-1}

Output

Output an integer representing the minimum total cost.

Sample Input 1

2 2
3
5
2
7

Sample Output 1

29

It is enough to pave the following eight roads.

Road connecting houses at (0,0) and (0,1)

Road connecting houses at (0,1) and (1,1)

Road connecting houses at (0,2) and (1,2)

Road connecting houses at (1,0) and (1,1)

Road connecting houses at (1,0) and (2,0)

Road connecting houses at (1,1) and (1,2)

Road connecting houses at (1,2) and (2,2)

Road connecting houses at (2,0) and (2,1)

Sample Input 2

4 3
2
4
8
1
2
9
3

Sample Output 2

60


program greedy_customers
  implicit none
  integer :: n, a(100000), x, i
  integer(8) :: m
  a = 0
  read(*,*) n
  do i = 1, n
    read(*,*) a(i)
  end do
  x = 1
  m = int(a(1)-1,8)
  do i = 2, n
    if (a(i).le.x+1) then
      x = max(x,a(i))
    else
      m = m+int((a(i)-1)/(x+1),8)
    end if
  end do
  write(*,'(i0)') m
  stop
end program greedy_customers

Score : 700 points

Problem Statement

N people are waiting in a single line in front of the Takahashi Store. The cash on hand of the i-th person from the front of the line is a positive integer A_i.

Mr. Takahashi, the shop owner, has decided on the following scheme: He picks a product, sets a positive integer P indicating its price, and shows this product to customers in order, starting from the front of the line. This step is repeated as described below.

At each step, when a product is shown to a customer, if price P is equal to or less than the cash held by that customer at the time, the customer buys the product and Mr. Takahashi ends the current step. That is, the cash held by the first customer in line with cash equal to or greater than P decreases by P, and the next step begins.

Mr. Takahashi can set the value of positive integer P independently at each step.

He would like to sell as many products as possible. However, if a customer were to end up with 0 cash on hand after a purchase, that person would not have the fare to go home. Customers not being able to go home would be a problem for Mr. Takahashi, so he does not want anyone to end up with 0 cash.

Help out Mr. Takahashi by writing a program that determines the maximum number of products he can sell, when the initial cash in possession of each customer is given.

Constraints

1 ≦ | N | ≦ 100000

1 ≦ A_i ≦ 10^9(1 ≦ i ≦ N)

All inputs are integers.

Input

Inputs are provided from Standard Inputs in the following form.

N
A_1
:
A_N

Output

Output an integer representing the maximum number of products Mr. Takahashi can sell.

Sample Input 1

3
3
2
5

Sample Output 1

3

As values of P, select in order 1, 4, 1.

Sample Input 2

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

Sample Output 2

18


program agc005_a
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: i,n,l,r,cnt
    character(200000):: x

    read*, x
    n = len_trim(x)

    cnt = 0
    do i=1,n-1
        if (x(i:i+1) /= 'ST') cycle
        l = i
        r = i+1
        do while(x(l:l) == 'S' .and. x(r:r) == 'T')
            cnt=cnt+1
            l=l-1
            r=r+1
            if (l < 1 .or. n < r) exit
        end do
    end do

    print'(i0)', n-2*cnt
end program agc005_a

Score : 300 points

Problem Statement

We have a string X, which has an even number of characters. Half the characters are S, and the other half are T.

Takahashi, who hates the string ST, will perform the following operation 10^{10000} times:

Among the occurrences of ST in X as (contiguous) substrings, remove the leftmost one. If there is no occurrence, do nothing.

Find the eventual length of X.

Constraints

2 ≦ |X| ≦ 200,000

The length of X is even.

Half the characters in X are S, and the other half are T.

Partial Scores

In test cases worth 200 points, |X| ≦ 200.

Input

The input is given from Standard Input in the following format:

X

Output

Print the eventual length of X.

Sample Input 1

TSTTSS

Sample Output 1

4

In the 1-st operation, the 2-nd and 3-rd characters of TSTTSS are removed.
X becomes TTSS, and since it does not contain ST anymore, nothing is done in the remaining 10^{10000}-1 operations.
Thus, the answer is 4.

Sample Input 2

SSTTST

Sample Output 2

0

X will eventually become an empty string: SSTTST ⇒ STST ⇒ ST ⇒ ``.

Sample Input 3

TSSTTTSS

Sample Output 3

4

X will become: TSSTTTSS ⇒ TSTTSS ⇒ TTSS.


program not_k_perm_counting
  implicit none
  integer(8), parameter :: md = 924844033_8
  integer :: n, k, i, j, l, m, p
  integer(8) :: ans = 0_8, t = 1_8, g(0:4000,0:1) = 0_8
  integer(8), dimension(0:4000) :: f = 0_8, a = 0_8
  f(0) = 1_8
  g(0,0) = 1_8
  read(*,*) n, k
  do i = 1, n
    f(i) = mod(f(i-1)*int(i,8),md)
  end do
  do i = 1, k
    m = (n-i)/k
    do p = 1, 2
      do j = 1, m
        do l = n, 1, -1
          g(l,0) = mod(g(l,0)+g(l,1),md)
          g(l,1) = g(l-1,0)
        end do
        g(0,0) = mod(g(0,0)+g(0,1),md)
      end do
      do j = 0, n
        g(j,0) = mod(g(j,0)+g(j,1),md)
        g(j,1) = 0_8
      end do
    end do
  end do
  do i = 0, n
    ans = modulo(ans+t*mod(g(i,0)*f(n-i),md),md)
    t = -t
  end do
  write(*,'(i0)') ans
end program not_k_perm_counting

Score : 900 points

Problem Statement

Snuke loves permutations. He is making a permutation of length N.

Since he hates the integer K, his permutation will satisfy the following:

Let the permutation be a_1, a_2, ..., a_N. For each i = 1,2,...,N, |a_i - i| \neq K.

Among the N! permutations of length N, how many satisfies this condition?

Since the answer may be extremely large, find the answer modulo 924844033(prime).

Constraints

2 ≦ N ≦ 2000

1 ≦ K ≦ N-1

Input

The input is given from Standard Input in the following format:

N K

Output

Print the answer modulo 924844033.

Sample Input 1

3 1

Sample Output 1

2

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

Sample Input 2

4 1

Sample Output 2

5

5 permutations (1, 2, 3, 4), (1, 4, 3, 2), (3, 2, 1, 4), (3, 4, 1, 2), (4, 2, 3, 1) satisfy the condition.

Sample Input 3

4 2

Sample Output 3

9

Sample Input 4

4 3

Sample Output 4

14

Sample Input 5

425 48

Sample Output 5

756765083


program sample
    implicit none

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

    read(*,*) n
    allocate(x(n))
    read(*,*)x
    m=0
    do i=1,n
        a=x(i)
        if(x(a)==i)then
            m=m+1
        end if
    end do
    write(*,*) m/2

    stop
end program sample

Score : 200 points

Problem Statement

There are N rabbits, numbered 1 through N.

The i-th (1≤i≤N) rabbit likes rabbit a_i.
Note that no rabbit can like itself, that is, a_i≠i.

For a pair of rabbits i and j (i＜j), we call the pair (i，j) a friendly pair if the following condition is met.

Rabbit i likes rabbit j and rabbit j likes rabbit i.

Calculate the number of the friendly pairs.

Constraints

2≤N≤10^5

1≤a_i≤N

a_i≠i

Input

The input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the number of the friendly pairs.

Sample Input 1

4
2 1 4 3

Sample Output 1

2

There are two friendly pairs: (1，2) and (3，4).

Sample Input 2

3
2 3 1

Sample Output 2

0

There are no friendly pairs.

Sample Input 3

5
5 5 5 5 1

Sample Output 3

1

There is one friendly pair: (1，5).


program prob3
    implicit none
    integer::N, i, ans, tmp
    integer, allocatable::a(:), c(:)
    read(*,*) N
    allocate(a(N), c(N))
    read(*,*) a
    ans = 0
    tmp = 0
    do i = 1, N
        if(a(a(i)) == i) then
            ans = ans + 1
        end if
    end do
    write(*,*) ans/2
    deallocate(a,c)
    stop
end program


program main
	implicit none
	character(100010) :: s
	integer :: K, i

	read(*, *) s
	read(*, *) K

	do i = 1, len_trim(s)
		if(K-search(s(i:i)) >= 0) then
			K = K - search(s(i:i))
			s(i:i) ="a"
		end if
	end do
	s(len_trim(s):len_trim(s)) = search_out(mod((26-(search(s(len_trim(s):len_trim(s)))-mod(K,26))),26))

	write(*, *) trim(s)

	contains
	integer function search(moji)
		character(1) :: moji
		if(moji == "a")  search = 0
		if(moji == "b")  search = 25
		if(moji == "c")  search = 24
		if(moji == "d")  search = 23
		if(moji == "e")  search = 22
		if(moji == "f")  search = 21
		if(moji == "g")  search = 20
		if(moji == "h")  search = 19
		if(moji == "i")  search = 18
		if(moji == "j")  search = 17
		if(moji == "k")  search = 16
		if(moji == "l")  search = 15
		if(moji == "m")  search = 14
		if(moji == "n")  search = 13
		if(moji == "o")  search = 12
		if(moji == "p")  search = 11
		if(moji == "q")  search = 10
		if(moji == "r")  search = 9
		if(moji == "s")  search = 8
		if(moji == "t")  search = 7
		if(moji == "u")  search = 6
		if(moji == "v")  search = 5
		if(moji == "w")  search = 4
		if(moji == "x")  search = 3
		if(moji == "y")  search = 2
		if(moji == "z")  search = 1
	end function search

	character(1) function search_out(kazu)
		integer :: kazu
		if(kazu == 0)  search_out = "a"
		if(kazu == 1)  search_out = "b"
		if(kazu == 2)  search_out = "c"
		if(kazu == 3)  search_out = "d"
		if(kazu == 4)  search_out = "e"
		if(kazu == 5)  search_out = "f"
		if(kazu == 6)  search_out = "g"
		if(kazu == 7)  search_out = "h"
		if(kazu == 8)  search_out = "i"
		if(kazu == 9)  search_out = "j"
		if(kazu == 10)  search_out = "k"
		if(kazu == 11)  search_out = "l"
		if(kazu == 12)  search_out = "m"
		if(kazu == 13)  search_out = "n"
		if(kazu == 14)  search_out = "o"
		if(kazu == 15)  search_out = "p"
		if(kazu == 16)  search_out = "q"
		if(kazu == 17)  search_out = "r"
		if(kazu == 18)  search_out = "s"
		if(kazu == 19)  search_out = "t"
		if(kazu == 20)  search_out = "u"
		if(kazu == 21)  search_out = "v"
		if(kazu == 22)  search_out = "w"
		if(kazu == 23)  search_out = "x"
		if(kazu == 24)  search_out = "y"
		if(kazu == 25)  search_out = "z"
	end function search_out
end program main

Score : 400 points

Problem Statement

Mr. Takahashi has a string s consisting of lowercase English letters.
He repeats the following operation on s exactly K times.

Choose an arbitrary letter on s and change that letter to the next alphabet. Note that the next letter of z is a.

For example, if you perform an operation for the second letter on aaz, aaz becomes abz.
If you then perform an operation for the third letter on abz, abz becomes aba.

Mr. Takahashi wants to have the lexicographically smallest string after performing exactly K operations on s.
Find the such string.

Constraints

1≤|s|≤10^5

All letters in s are lowercase English letters.

1≤K≤10^9

Input

The input is given from Standard Input in the following format:

s
K

Output

Print the lexicographically smallest string after performing exactly K operations on s.

Sample Input 1

xyz
4

Sample Output 1

aya

For example, you can perform the following operations: xyz, yyz, zyz, ayz, aya.

Sample Input 2

a
25

Sample Output 2

z

You have to perform exactly K operations.

Sample Input 3

codefestival
100

Sample Output 3

aaaafeaaivap


program atcoder
implicit none
character(len=100) :: a, b, c
character :: t

read(*,*) a, b, c

t = a(1:1)
a = a(2:100)

do
	if (t == 'a') then
    	if (trim(a) == '')then
    		write(*,'(a)') 'A'
        	stop
    	end if
    	t = a(1:1)
        a = a(2:100)
	else if (t == 'b') then
    	if (trim(b) == '')then
    		write(*,'(a)') 'B'
        	stop
    	end if
    	t = b(1:1)
        b = b(2:100)
	else
    	if (trim(c) == '')then
    		write(*,'(a)') 'C'
        	stop
    	end if
    	t = c(1:1)
        c = c(2:100)
	end if
end do

end program atcoder

Score : 200 points

Problem Statement

Alice, Bob and Charlie are playing Card Game for Three, as below:

At first, each of the three players has a deck consisting of some number of cards. Each card has a letter a, b or c written on it. The orders of the cards in the decks cannot be rearranged.

The players take turns. Alice goes first.

If the current player's deck contains at least one card, discard the top card in the deck. Then, the player whose name begins with the letter on the discarded card, takes the next turn. (For example, if the card says a, Alice takes the next turn.)

If the current player's deck is empty, the game ends and the current player wins the game.

You are given the initial decks of the players.
More specifically, you are given three strings S_A, S_B and S_C. The i-th (1≦i≦|S_A|) letter in S_A is the letter on the i-th card in Alice's initial deck. S_B and S_C describes Bob's and Charlie's initial decks in the same way.

Determine the winner of the game.

Constraints

1≦|S_A|≦100

1≦|S_B|≦100

1≦|S_C|≦100

Each letter in S_A, S_B, S_C is a, b or c.

Input

The input is given from Standard Input in the following format:

S_A
S_B
S_C

Output

If Alice will win, print A. If Bob will win, print B. If Charlie will win, print C.

Sample Input 1

aca
accc
ca

Sample Output 1

A

The game will progress as below:

Alice discards the top card in her deck, a. Alice takes the next turn.

Alice discards the top card in her deck, c. Charlie takes the next turn.

Charlie discards the top card in his deck, c. Charlie takes the next turn.

Charlie discards the top card in his deck, a. Alice takes the next turn.

Alice discards the top card in her deck, a. Alice takes the next turn.

Alice's deck is empty. The game ends and Alice wins the game.

Sample Input 2

abcb
aacb
bccc

Sample Output 2

C


program atcoder
implicit none
character(len=100) :: a, b, c
character :: t

read(*,*) a, b, c

t = a(1:1)
a = a(2:100)

do
	if (t == 'a') then
    	if (trim(a) == '')then
    		write(*,'(a)') 'A'
        	stop
    	end if
    	t = a(1:1)
        a = a(2:100)
	else if (t == 'b') then
    	if (trim(b) == '')then
    		write(*,'(a)') 'B'
        	stop
    	end if
    	t = a(1:1)
        b = b(2:100)
	else
    	if (trim(c) == '')then
    		write(*,'(a)') 'C'
        	stop
    	end if
    	t = a(1:1)
        c = c(2:100)
	end if
end do

end program atcoder


character(100) a,b,c
character x
read*,a,b,c
x="a"
do
	if(x=="a")then
		if(trim(a)=="")then
        	print"(A)","A"
        	exit
        else
        	x=a(1:1)
            a=a(2:)
       	endif
    else if(x=="b")then
			if(trim(b)=="")then
        		print"(A)","B"
        		exit
        	else
        		x=b(1:1)
            	b=b(2:)
       		endif
	else
		if(trim(c)=="")then
        	print"(A)","C"
        	exit
        else
        	x=c(1:1)
            c=c(2:)
       	endif
    endif
end do
end


program contest
  implicit none

  integer :: a, b, c, d

  read(*,*) a, b, c

  if( mod(a,2)==0 ) then
     d = 0
  else if( mod(b,2)==0 ) then
     d = 0
  else if( mod(c,2)==0 ) then
     d = 0
  else if( a>b .and. a>c ) then
     d = b*c
  else if( b>a .and. b>c ) then
     d = a*c
  else if( c>a .and. c>b ) then
     d = b*a
  end if

  write(*,*) d

  stop
end program contest

Score : 200 points

Problem Statement

We have a rectangular parallelepiped of size A×B×C, built with blocks of size 1×1×1. Snuke will paint each of the A×B×C blocks either red or blue, so that:

There is at least one red block and at least one blue block.

The union of all red blocks forms a rectangular parallelepiped.

The union of all blue blocks forms a rectangular parallelepiped.

Snuke wants to minimize the difference between the number of red blocks and the number of blue blocks. Find the minimum possible difference.

Constraints

2≤A,B,C≤10^9

Input

The input is given from Standard Input in the following format:

A B C

Output

Print the minimum possible difference between the number of red blocks and the number of blue blocks.

Sample Input 1

3 3 3

Sample Output 1

9

For example, Snuke can paint the blocks as shown in the diagram below.
There are 9 red blocks and 18 blue blocks, thus the difference is 9.

Sample Input 2

2 2 4

Sample Output 2

0

For example, Snuke can paint the blocks as shown in the diagram below.
There are 8 red blocks and 8 blue blocks, thus the difference is 0.

Sample Input 3

5 3 5

Sample Output 3

15

For example, Snuke can paint the blocks as shown in the diagram below.
There are 45 red blocks and 30 blue blocks, thus the difference is 9.


program and_grid
  implicit none
  integer :: h, w, i, j, k
  integer :: r(500,500), b(500,500)
  character(500) :: s
  r = 0
  b = 0
  read(*,*) h, w
  do i = 1, h
    read(*,*) s
    s = trim(adjustl(s))
    do j = 1, w
      if (s(j:j).eq."#") then
        r(i,j) = 1
        b(i,j) = 1
      end if
    end do
  end do
  do i = h-1, 2, -1
    do j = 1, w
      if (r(i,j).gt.0.and.r(i-1,j).eq.0) r(1:i,j) = 1
    end do
  end do
  r(1,2:w-1) = 1
  do j = w-1, 2, -1
    do i = 1, h
      if (b(i,j).gt.0.and.b(i,j-1).eq.0) then
        do k = 1, j
          if (r(i,k).eq.0) b(i,k) = 1
        end do
      end if
    end do
  end do
  b(2:h-1,1) = 1
  call output(r(1:h,1:w))
  write(*,*)
  call output(b(1:h,1:w))
  stop
contains
  subroutine output(a)
    implicit none
    integer, intent(in) :: a(:,:)
    integer :: h, w, i
    character(500) :: s
    h = size(a(:,1))
    w = size(a(1,:))
    do i = 1, h
      s = ""
      do j = 1, w
        s(j:j) = "."
        if (a(i,j).gt.0) s(j:j) = "#"
      end do
      write(*,'(a)') trim(adjustl(s))
    end do
    return
  end subroutine output
end program and_grid

Score : 700 points

Problem Statement

Snuke and Ciel went to a strange stationery store. Each of them got a transparent graph paper with H rows and W columns.

Snuke painted some of the cells red in his paper. Here, the cells painted red were 4-connected, that is, it was possible to traverse from any red cell to any other red cell, by moving to vertically or horizontally adjacent red cells only.

Ciel painted some of the cells blue in her paper. Here, the cells painted blue were 4-connected.

Afterwards, they precisely overlaid the two sheets in the same direction. Then, the intersection of the red cells and the blue cells appeared purple.

You are given a matrix of letters a_{ij} (1≤i≤H, 1≤j≤W) that describes the positions of the purple cells. If the cell at the i-th row and j-th column is purple, then a_{ij} is #, otherwise a_{ij} is .. Here, it is guaranteed that no outermost cell is purple. That is, if i=1, H or j = 1, W, then a_{ij} is ..

Find a pair of the set of the positions of the red cells and the blue cells that is consistent with the situation described. It can be shown that a solution always exists.

Constraints

3≤H,W≤500

a_{ij} is # or ..

If i=1,H or j=1,W, then a_{ij} is ..

At least one of a_{ij} is #.

Input

The input is given from Standard Input in the following format:

H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}

Output

Print a pair of the set of the positions of the red cells and the blue cells that is consistent with the situation, as follows:

The first H lines should describe the positions of the red cells.

The following 1 line should be empty.

The following H lines should describe the positions of the blue cells.

The description of the positions of the red or blue cells should follow the format of the description of the positions of the purple cells.

Sample Input 1

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

Sample Output 1

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

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

One possible pair of the set of the positions of the red cells and the blue cells is as follows:

Sample Input 2

7 13
.............
.###.###.###.
.#.#.#...#...
.###.#...#...
.#.#.#.#.#...
.#.#.###.###.
.............

Sample Output 2

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

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

One possible pair of the set of the positions of the red cells and the blue cells is as follows:


program teleporter
  implicit none
  integer :: n, k, a(0:30,100000) = 0, i, j, b, u(100000) = 0
  read(*,*) n, k
  read(*,*) a(0,1:n)
  do i = 1, 30
    do j = 1, n
      a(i,j) = a(i-1,a(i-1,j))
    end do
  end do
  do i = 1, n
    b = i
    do j = 30, 0, -1
      if (btest(k,j)) b = a(j,b)
    end do
    u(b) = 1
  end do
  write(*,'(i0)') sum(u(2:n))
  stop
end program teleporter

Score : 800 points

Problem Statement

There are N towns in Snuke Kingdom, conveniently numbered 1 through N.
Town 1 is the capital.

Each town in the kingdom has a Teleporter, a facility that instantly transports a person to another place.
The destination of the Teleporter of town i is town a_i (1≤a_i≤N).
It is guaranteed that one can get to the capital from any town by using the Teleporters some number of times.

King Snuke loves the integer K.
The selfish king wants to change the destination of the Teleporters so that the following holds:

Starting from any town, one will be at the capital after using the Teleporters exactly K times in total.

Find the minimum number of the Teleporters whose destinations need to be changed in order to satisfy the king's desire.

Constraints

2≤N≤10^5

1≤a_i≤N

One can get to the capital from any town by using the Teleporters some number of times.

1≤K≤10^9

Input

The input is given from Standard Input in the following format:

N K
a_1 a_2 ... a_N

Output

Print the minimum number of the Teleporters whose destinations need to be changed in order to satisfy King Snuke's desire.

Sample Input 1

3 1
2 3 1

Sample Output 1

2

Change the destinations of the Teleporters to a = (1,1,1).

Sample Input 2

4 2
1 1 2 2

Sample Output 2

0

There is no need to change the destinations of the Teleporters, since the king's desire is already satisfied.

Sample Input 3

8 2
4 1 2 3 1 2 3 4

Sample Output 3

3

For example, change the destinations of the Teleporters to a = (1,1,2,1,1,2,2,4).


program ABC044A
    implicit none
    integer(8)::N,K,X,Y
    read(5,*)N,K,X,Y
    print'(i0)',min(N,K)*X+max(0,N-K)*Y
end program ABC044A

Score : 100 points

Problem Statement

There is a hotel with the following accommodation fee:

X yen (the currency of Japan) per night, for the first K nights

Y yen per night, for the (K+1)-th and subsequent nights

Tak is staying at this hotel for N consecutive nights.
Find his total accommodation fee.

Constraints

1 \leq N, K \leq 10000

1 \leq Y < X \leq 10000

N,\,K,\,X,\,Y are integers.

Input

The input is given from Standard Input in the following format:

N
K
X
Y

Output

Print Tak's total accommodation fee.

Sample Input 1

5
3
10000
9000

Sample Output 1

48000

The accommodation fee is as follows:

10000 yen for the 1-st night

10000 yen for the 2-nd night

10000 yen for the 3-rd night

9000 yen for the 4-th night

9000 yen for the 5-th night

Thus, the total is 48000 yen.

Sample Input 2

2
3
10000
9000

Sample Output 2

20000


program main
integer x,y
read*,n,k,x,y
if(n<k)then
print*,n*x
else
print*,k*x+(n-k)*y
endif
end program main


integer n, k, x, y, a
a = 0
read*, n, k, x, y
if(n > k) then
	ans = (n - k) * y
    n = k
end if
ans = ans + n * x
print*, int(ans)
end


implicit none
integer :: N, K, X, Y

read *, N
read *, K
read *, X
read *, Y

if (N <= K) then
   write(6,'(i0)') X * N
else
   write(6,'(i0)') X * K + Y * (N-K)
endif
stop
end


PROGRAM ATCODER

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

read(*,*) n,k,x,y    !ここが入力欄

! ここに計算式
ans = 0
do i = 1, n
	if(i <= k)then
		ans = ans + x
	else
		ans = ans + y
	end if
end do

! ここに出力結果
write(*,'(i0)') ans

END PROGRAM ATCODER