program codefestival2016qualb
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: n,a,b,i
    integer(int32):: qualifier, foregin_qualifier
    character(:),allocatable:: s
    logical:: yes

    read*, n,a,b
    allocate(character(n):: s)
    read*, s
    qualifier = 0
    foregin_qualifier = 0

    do i=1,n
        if (qualifier >= a+b) then
            yes = .false.
        else if (s(i:i) == 'a') then
            yes = .true.
            qualifier=qualifier+1
        else if (s(i:i) == 'b') then
            if (foregin_qualifier >= b) then
                yes = .false.
            else
                yes = .true.
                qualifier=qualifier+1
                foregin_qualifier=foregin_qualifier+1
            end if
        else
            yes = .false.
        end if

        if (yes) then
            print'(a)', 'Yes'
        else
            print'(a)', 'No'
        end if
    end do
end program codefestival2016qualb

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 agc005_a
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: i,n,ans
    integer(int32), allocatable:: cnt(:)
    character(200000):: x

    read*, x
    n = len_trim(x)
    allocate(cnt(0:n), source=0)
    ans = n
    do i=1, n
        if (x(i:i) == 'S') then
            cnt(i)=cnt(i-1)+1
        else
            cnt(i)=max(cnt(i-1)-1,0)
            if (cnt(i-1) >= 1) ans=ans-2
        end if
    end do
    ! print'(*(i0,1x))', cnt(:)
    print'(i0)', ans
end program agc005_a

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

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.

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 tree_restoring
  implicit none
  integer :: n, a(100), i
  a = 0
  read(*,*) n
  if (n.eq.2) then
    write(*,'(a)') "Possible"
    stop
  end if
  read(*,*) a(1:n)
  call quick_sort(a(1:n))
  do i = 2, n
    if (a(i).ne.a(i-1).and.a(i).ne.a(i-1)+1) then
      write(*,'(a)') "Impossible"
      stop
    end if
  end do
  if (a(n).ne.a(n-1)) then
    write(*,'(a)') "Impossible"
    stop
  end if
  if (a(1).ne.(a(n)+1)/2) then
    write(*,'(a)') "Impossible"
    stop
  end if
  if (mod(a(n),2).eq.0) then
    if (a(1).eq.a(2)) then
      write(*,'(a)') "Impossible"
      stop
    end if
  else
    if (a(1).ne.a(2)) then
      write(*,'(a)') "Impossible"
      stop
    end if
  end if
  write(*,'(a)') "Possible"
  stop
contains
  recursive subroutine quick_sort(a)
    implicit none
    integer, intent(inout) :: a(:)
    integer :: n, l, r, p, c
    n = size(a)
    l = 1
    r = n
    p = a((l+r)/2)+a(l)+a(r)-max(a((l+r)/2),a(l),a(r))-min(a((l+r)/2),a(l),a(r))
    do
      do while (a(l).lt.p)
        l = l+1
      end do
      do while (a(r).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))
    if (n.gt.r+1) call quick_sort(a(r+1:n))
    return
  end subroutine quick_sort
end program tree_restoring

Score : 700 points

Problem Statement

Aoki loves numerical sequences and trees.

One day, Takahashi gave him an integer sequence of length N, a_1, a_2, ..., a_N, which made him want to construct a tree.

Aoki wants to construct a tree with N vertices numbered 1 through N, such that for each i = 1,2,...,N, the distance between vertex i and the farthest vertex from it is a_i, assuming that the length of each edge is 1.

Determine whether such a tree exists.

Constraints

2 ≦ N ≦ 100

1 ≦ a_i ≦ N-1

Input

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

N
a_1 a_2 ... a_N

Output

If there exists a tree that satisfies the condition, print Possible. Otherwise, print Impossible.

Sample Input 1

5
3 2 2 3 3

Sample Output 1

Possible

The diagram above shows an example of a tree that satisfies the conditions. The red arrows show paths from each vertex to the farthest vertex from it.

Sample Input 2

3
1 1 2

Sample Output 2

Impossible

Sample Input 3

10
1 2 2 2 2 2 2 2 2 2

Sample Output 3

Possible

Sample Input 4

10
1 1 2 2 2 2 2 2 2 2

Sample Output 4

Impossible

Sample Input 5

6
1 1 1 1 1 5

Sample Output 5

Impossible

Sample Input 6

5
4 3 2 3 4

Sample Output 6

Possible


program sample
implicit none
character(len=13) :: x,y
integer(8) :: i,j
real(8),allocatable :: a(:),b(:)

read(*,*) x

do i=13,6,-1
x(i:i)=x(i-1:i-1)
end do

x(5:5)=' '

write(*,*) trim(x)
!write(*,*) a

end program sample

Score : 100 points

Problem Statement

This contest is CODE FESTIVAL.
However, Mr. Takahashi always writes it CODEFESTIVAL, omitting the single space between CODE and FESTIVAL.

So he has decided to make a program that puts the single space he omitted.

You are given a string s with 12 letters.
Output the string putting a single space between the first 4 letters and last 8 letters in the string s.

Constraints

s contains exactly 12 letters.

All letters in s are uppercase English letters.

Input

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

s

Output

Print the string putting a single space between the first 4 letters and last 8 letters in the string s.
Put a line break at the end.

Sample Input 1

CODEFESTIVAL

Sample Output 1

CODE FESTIVAL

Putting a single space between the first 4 letters and last 8 letters in CODEFESTIVAL makes it CODE FESTIVAL.

Sample Input 2

POSTGRADUATE

Sample Output 2

POST GRADUATE

Sample Input 3

ABCDEFGHIJKL

Sample Output 3

ABCD EFGHIJKL


character s*12
read*, s
print '(a4,1x,a8)', s(:4),s(5:)
end


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

    deallocate(a)

    stop
contains
end program prob3

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 codefestival_2016_qualA_c
    use,intrinsic :: iso_fortran_env
    implicit none
    character(1000000):: s
    integer(int64):: k,n,i,cost,nind

    read*, s
    n = len_trim(s)
    read*, k

    do i=1,n
        if (s(i:i) == 'a') cycle
        cost = 1+ichar('z') - ichar(s(i:i))
        if (cost <= k) then
            s(i:i) = 'a'
            k=k-cost
        end if
    end do
    nind = (ichar(s(n:n)) - ichar('a'))

    s(n:n) = char(mod(nind+k, 26_8) + ichar('a'))

    print'(a)', trim(s)
end program codefestival_2016_qualA_c

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 lru_puzzle
  implicit none
  integer :: n, m, q, a(100000) = 0, i, k
  integer :: x = 0, p(100000) = 0, r(100000) = 0, c(0:100000) = 0
  logical :: u(100000) = .false.
  read(*,*) n, m
  read(*,*) q
  read(*,*) a(1:q)
  do i = q, 1, -1
    if (u(a(i))) cycle
    x = x+1
    p(x) = a(i)
    u(a(i)) = .true.
  end do
  do i = 1, m
    if (u(i)) cycle
    x = x+1
    p(x) = i
  end do
  do i = 1, m
    r(p(i)) = i
  end do
  c(0) = n
  do i = q, 1, -1
    k = r(a(i))
    if (c(k-1) > 0) then
      c(k-1) = c(k-1)-1
      c(k) = c(k)+1
    end if
  end do
  k = 0
  do while (c(k) == 0)
    k = k+1
  end do
  do i = k+1, m-1
    if (p(i) > p(i+1)) then
      write(*,'(a)') "No"
      stop
    end if
  end do
  write(*,'(a)') "Yes"
end program lru_puzzle

Score : 1200 points

Problem Statement

There are N arrays.
The length of each array is M and initially each array contains integers (1，2，...，M) in this order.

Mr. Takahashi has decided to perform Q operations on those N arrays.
For the i-th (1≤i≤Q) time, he performs the following operation.

Choose an arbitrary array from the N arrays and move the integer a_i (1≤a_i≤M) to the front of that array. For example, after performing the operation on a_i=2 and the array (5，4，3，2，1), this array becomes (2，5，4，3，1).

Mr. Takahashi wants to make N arrays exactly the same after performing the Q operations.
Determine if it is possible or not.

Constraints

2≤N≤10^5

2≤M≤10^5

1≤Q≤10^5

1≤a_i≤M

Input

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

N M
Q
a_1 a_2 ... a_Q

Output

Print Yes if it is possible to make N arrays exactly the same after performing the Q operations.
Otherwise, print No.

Sample Input 1

2 2
3
2 1 2

Sample Output 1

Yes

You can perform the operations as follows.

Sample Input 2

3 2
3
2 1 2

Sample Output 2

No

Sample Input 3

2 3
3
3 2 1

Sample Output 3

Yes

You can perform the operations as follows.

Sample Input 4

3 3
6
1 2 2 3 3 3

Sample Output 4

No


program ABC045A
    implicit none
    integer(8)::a,b,h
    read(5,*)a,b,h
    print'(i0)',((a+b)*h)/2
end program ABC045A

Score : 100 points

Problem Statement

You are given a trapezoid. The lengths of its upper base, lower base, and height are a, b, and h, respectively.

An example of a trapezoid

Find the area of this trapezoid.

Constraints

1≦a≦100

1≦b≦100

1≦h≦100

All input values are integers.

h is even.

Input

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

a
b
h

Output

Print the area of the given trapezoid. It is guaranteed that the area is an integer.

Sample Input 1

3
4
2

Sample Output 1

7

When the lengths of the upper base, lower base, and height are 3, 4, and 2, respectively, the area of the trapezoid is (3+4)×2/2 = 7.

Sample Input 2

4
4
4

Sample Output 2

16

In this case, a parallelogram is given, which is also a trapezoid.


program atcoder
implicit none
integer :: a, b, h

read(*,*) a, b, h

write(*,'(i0)') (a+b)*h/2

end program atcoder


implicit none
integer (8):: a,b,h
read(*,*) a,b,h
write(*,*)  int( (a+b)*h/2 )
end


integer,dimension(10,10)::a
character(11)S
integer(16) answer
read*,S
a=0
do i=1,len_trim(S)
	do j=i,len_trim(S)
    	if(j==i)then
        	a(i,j)=2**(i-1)
        else
        	a(i,j)=2**(j-2)
        endif
    end do
end do
answer=0
do i=1,len_trim(s)
	read(S(i:i),*) aki
    do j=i,len_trim(S)
    	answer=answer+aki*10**(len_trim(s)-j)*a(i,j)
    end do
    print"(I0)",answer
end do
print"(I0)",answer
end

Score : 300 points

Problem Statement

You are given a string S consisting of digits between 1 and 9, inclusive.
You can insert the letter + into some of the positions (possibly none) between two letters in this string.
Here, + must not occur consecutively after insertion.

All strings that can be obtained in this way can be evaluated as formulas.

Evaluate all possible formulas, and print the sum of the results.

Constraints

1 \leq |S| \leq 10

All letters in S are digits between 1 and 9, inclusive.

Input

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

S

Output

Print the sum of the evaluated value over all possible formulas.

Sample Input 1

125

Sample Output 1

176

There are 4 formulas that can be obtained: 125, 1+25, 12+5 and 1+2+5. When each formula is evaluated,

125

1+25=26

12+5=17

1+2+5=8

Thus, the sum is 125+26+17+8=176.

Sample Input 2

9999999999

Sample Output 2

12656242944


program main
  implicit none

  integer(8) :: a, b, c

  read(*,*) a, b, c

  if (mod(a,2) == 0 .or. mod(b,2) == 0 .or. mod(c,2) == 0) then
     write(*,*) 0

  else
     write(*,*) min(a*b, b*c, c*a)

  end if
  stop

end program main

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 prob4
    implicit none
    integer(8) A, B, C
    integer(16) ans
    read(*,*) A, B, C

    if(mod(A,2)==0 .or. mod(B,2)==0 .or. mod(C,2)==0)then
        write(*,*) 0
    else
        ans = A*B
        if(A*C < ans)then
            ans = A*C
        end if
        if(B*C < ans)then
            ans = B*C
        end if

        write(*,*) ans
    end if

    stop
end program prob4


program main
implicit none
integer :: i, j, k, l, a(26)
character :: s*100

read(*,*) s
l = len(s)
a = 0
do i = 1, l
   do j = 1, 26
      k = ichar(s(i:i)) -96
      if( j == k ) then
         a(j) = a(j) + 1
      end if
   end do
end do
do j = 1, 26
   if( mod(a(j),2) /= 0 ) then
      write(*,*) 'No'
      stop
   end if
end do
write(*,*) 'Yes'

end program main

Score : 200 points

Problem Statement

Let w be a string consisting of lowercase letters.
We will call w beautiful if the following condition is satisfied:

Each lowercase letter of the English alphabet occurs even number of times in w.

You are given the string w. Determine if w is beautiful.

Constraints

1 \leq |w| \leq 100

w consists of lowercase letters (a-z).

Input

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

w

Output

Print Yes if w is beautiful. Print No otherwise.

Sample Input 1

abaccaba

Sample Output 1

Yes

a occurs four times, b occurs twice, c occurs twice and the other letters occur zero times.

Sample Input 2

hthth

Sample Output 2

No


program main
  implicit none
  integer(8) :: n, s, sqrt_n, b, p
  read(*, *) n
  read(*, *) s

  if (n < s) then
    write(*, "(i0)") -1
    stop 0
  else if (n == s) then
    write(*, "(i0)") n + 1
    stop 0
  end if

  sqrt_n = integer_sqrt(n)

  do b = 2, sqrt_n
    if (f(b, n) == s) then
      write(*, "(i0)") b
      stop 0
    end if
  end do

  do p = 1, sqrt_n
    b = (n - s) / p + 1
    if (f(b, n) == s) then
      write(*, "(i0)") b
      stop 0
    end if
  end do

  write(*, "(i0)") -1

  contains

  function integer_sqrt(n)
    implicit none
    integer(8), intent(in) :: n
    integer(8) :: integer_sqrt, i
    if (n < 0) stop 101
    do i = 0, n + 1
      if (i ** 2 > n) then
        integer_sqrt = i - 1
        exit
      end if
    end do
  end function integer_sqrt

  recursive function f(b, n) result(res)
    implicit none
    integer(8), intent(in) :: b, n
    integer(8) :: res
    if (n < b) then
      res = n
    else
      res = f(b, n / b) + mod(n, b)
    end if
  end function f
end program main

Score : 500 points

Problem Statement

For integers b (b \geq 2) and n (n \geq 1), let the function f(b,n) be defined as follows:

f(b,n) = n, when n < b

f(b,n) = f(b,\,{\rm floor}(n / b)) + (n \ {\rm mod} \ b), when n \geq b

Here, {\rm floor}(n / b) denotes the largest integer not exceeding n / b,
and n \ {\rm mod} \ b denotes the remainder of n divided by b.

Less formally, f(b,n) is equal to the sum of the digits of n written in base b.
For example, the following hold:

f(10,\,87654)=8+7+6+5+4=30

f(100,\,87654)=8+76+54=138

You are given integers n and s.
Determine if there exists an integer b (b \geq 2) such that f(b,n)=s.
If the answer is positive, also find the smallest such b.

Constraints

1 \leq n \leq 10^{11}

1 \leq s \leq 10^{11}

n,\,s are integers.

Input

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

n
s

Output

If there exists an integer b (b \geq 2) such that f(b,n)=s, print the smallest such b.
If such b does not exist, print -1 instead.

Sample Input 1

87654
30

Sample Output 1

10

Sample Input 2

87654
138

Sample Output 2

100

Sample Input 3

87654
45678

Sample Output 3

-1

Sample Input 4

31415926535
1

Sample Output 4

31415926535

Sample Input 5

1
31415926535

Sample Output 5

-1


module mod_stack
  implicit none
  type t_node
    private
    integer(8) :: item
    type(t_node), pointer :: prev => null()
    type(t_node), pointer :: next => null()
  end type t_node
  type t_stack
    private
    integer :: num = 0
    type(t_node), pointer :: head => null()
    type(t_node), pointer :: tail => null()
  contains
    procedure :: push => push
    procedure :: pop => pop
    procedure :: peek => peek
    procedure :: clear => clear
    procedure :: size => size_of
  end type t_stack
contains
  function new_node(item) result(node)
    integer(8), intent(in) :: item
    type(t_node), pointer :: node
    allocate(node)
    node%item = item
  end
  subroutine push(this,item)
    class(t_stack), intent(inout) :: this
    integer(8), intent(in) :: item
    type(t_node), pointer :: node
    node => new_node(item)
    if (associated(this%head)) then
      node%prev => this%tail
      this%tail%next => node
    else
      this%head => node
    end if
    this%tail => node
    this%num = this%num+1
  end
  function pop(this) result(item)
    class(t_stack), intent(inout) :: this
    integer(8) :: item
    type(t_node), pointer :: node
    item = this%tail%item
    node => this%tail%prev
    deallocate(this%tail)
    this%tail => node
    if (associated(node)) then
      node%next => null()
    else
      this%head => null()
    end if
    this%num = this%num-1
  end
  function peek(this) result(item)
    class(t_stack), intent(in) :: this
    integer(8) :: item
    item = this%tail%item
  end
  subroutine clear(this)
    class(t_stack), intent(inout) :: this
    type(t_node), pointer :: node, prev
    if (.not.associated(this%tail)) return
    node => this%tail
    do while (associated(node%prev))
      prev => node%prev
      deallocate(node)
      node => prev
    end do
    this%head => null()
    this%tail => null()
    this%num = 0
  end
  integer function size_of(this)
    class(t_stack), intent(in) :: this
    size_of = this%num
  end
end module mod_stack
program sequential_operations_on_sequence
  use mod_stack
  implicit none
  type(t_stack) :: stack
  integer :: n, q, i, j, x
  integer(8) :: tmp1, tmp2, p(100000) = 0_8, k, l, r, m
  integer(8) :: t(100000) = 0_8, ans(0:100001) = 0_8
  read(*,*) n, q
  call stack%push(int(n,8))
  do i = 1, q
    read(*,*) tmp1
    do while (stack%size() > 0 .and. stack%peek() >= tmp1)
      tmp2 = stack%pop()
    end do
    call stack%push(tmp1)
  end do
  q = stack%size()
  do i = q, 1, -1
    p(i) = stack%pop()
  end do
  t(q) = 1_8
  do i = q, 2, -1
    j = i-1
    t(j) = t(j)+t(i)*(p(i)/p(j))
    k = mod(p(i),p(j))
    do while (k >= p(1))
      r = j
      l = 1
      do while (r-l > 1)
        m = (l+r)/2
        if (p(m) > k) then
          r = m
        else
          l = m
        end if
      end do
      j = l
      t(j) = t(j)+t(i)*(k/p(j))
      k = mod(k,p(j))
    end do
    x = int(k,4)
    ans(x) = ans(x)+t(i)
  end do
  x = int(p(1),4)
  ans(x) = ans(x)+t(1)
  do i = n, 1, -1
    ans(i) = ans(i)+ans(i+1)
  end do
  do i = 1, n
    write(*,'(i0)') ans(i)
  end do
end program sequential_operations_on_sequence

Score : 1400 points

Problem Statement

Snuke got an integer sequence from his mother, as a birthday present. The sequence has N elements, and the i-th of them is i.
Snuke performs the following Q operations on this sequence. The i-th operation, described by a parameter q_i, is as follows:

Take the first q_i elements from the sequence obtained by concatenating infinitely many copy of the current sequence, then replace the current sequence with those q_i elements.

After these Q operations, find how many times each of the integers 1 through N appears in the final sequence.

Constraints

1 ≦ N ≦ 10^5

0 ≦ Q ≦ 10^5

1 ≦ q_i ≦ 10^{18}

All input values are integers.

Input

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

N Q
q_1
:
q_Q

Output

Print N lines. The i-th line (1 ≦ i ≦ N) should contain the number of times integer i appears in the final sequence after the Q operations.

Sample Input 1

5 3
6
4
11

Sample Output 1

3
3
3
2
0

After the first operation, the sequence becomes: 1,2,3,4,5,1.

After the second operation, the sequence becomes: 1,2,3,4.

After the third operation, the sequence becomes: 1,2,3,4,1,2,3,4,1,2,3.

In this sequence, integers 1,2,3,4,5 appear 3,3,3,2,0 times, respectively.

Sample Input 2

10 10
9
13
18
8
10
10
9
19
22
27

Sample Output 2

7
4
4
3
3
2
2
2
0
0


module mod_stack
  implicit none
  type t_node
    private
    integer(8) :: item
    type(t_node), pointer :: prev => null()
    type(t_node), pointer :: next => null()
  end type t_node
  type t_stack
    private
    integer :: num = 0
    type(t_node), pointer :: head => null()
    type(t_node), pointer :: tail => null()
  contains
    procedure :: push => push
    procedure :: pop => pop
    procedure :: peek => peek
    procedure :: clear => clear
    procedure :: size => size_of
  end type t_stack
contains
  function new_node(item) result(node)
    integer(8), intent(in) :: item
    type(t_node), pointer :: node
    allocate(node)
    node%item = item
  end
  subroutine push(this,item)
    class(t_stack), intent(inout) :: this
    integer(8), intent(in) :: item
    type(t_node), pointer :: node
    node => new_node(item)
    if (associated(this%head)) then
      node%prev => this%tail
      this%tail%next => node
    else
      this%head => node
    end if
    this%tail => node
    this%num = this%num+1
  end
  function pop(this) result(item)
    class(t_stack), intent(inout) :: this
    integer(8) :: item
    type(t_node), pointer :: node
    item = this%tail%item
    node => this%tail%prev
    deallocate(this%tail)
    this%tail => node
    if (associated(node)) then
      node%next => null()
    else
      this%head => null()
    end if
    this%num = this%num-1
  end
  function peek(this) result(item)
    class(t_stack), intent(in) :: this
    integer(8) :: item
    item = this%tail%item
  end
  subroutine clear(this)
    class(t_stack), intent(inout) :: this
    type(t_node), pointer :: node, prev
    if (.not.associated(this%tail)) return
    node => this%tail
    do while (associated(node%prev))
      prev => node%prev
      deallocate(node)
      node => prev
    end do
    this%head => null()
    this%tail => null()
    this%num = 0
  end
  integer function size_of(this)
    class(t_stack), intent(in) :: this
    size_of = this%num
  end
end module mod_stack
program sequential_operations_on_sequence
  use mod_stack
  implicit none
  type(t_stack) :: stack
  integer :: n, q, i, j, x
  integer(8) :: tmp1, tmp2, p(100000) = 0_8, k, l, r, m
  integer(8) :: t(100000) = 0_8, ans(100001) = 0_8
  read(*,*) n, q
  call stack%push(int(n,8))
  do i = 1, q
    read(*,*) tmp1
    do while (stack%size() > 0 .and. stack%peek() >= tmp1)
      tmp2 = stack%pop()
    end do
    call stack%push(tmp1)
  end do
  q = stack%size()
  do i = q, 1, -1
    p(i) = stack%pop()
  end do
  t(q) = 1_8
  do i = q, 2, -1
    j = i-1
    t(j) = t(j)+t(i)*(p(i)/p(j))
    k = mod(p(i),p(j))
    do while (k >= p(1))
      r = j
      l = 1
      do while (r-l > 1)
        m = (l+r)/2
        if (p(m) > k) then
          r = m
        else
          l = m
        end if
      end do
      j = l
      t(j) = t(j)+t(i)*(k/p(j))
      k = mod(k,p(j))
    end do
    x = int(k,4)
    ans(x) = ans(x)+t(i)
  end do
  x = int(p(1),4)
  ans(x) = ans(x)+t(1)
  do i = n, 1, -1
    ans(i) = ans(i)+ans(i+1)
  end do
  do i = 1, n
    write(*,'(i0)') ans(i)
  end do
end program sequential_operations_on_sequence


program sample
	implicit none
    integer(8) :: N,i,count,average,sum1
    integer(8),allocatable :: x(:),a(:),b(:)
    real(8) :: count2,average2

    read(*,*) N

    allocate(x(N))
    allocate(b(N))

    read(*,*) x

    !write(*,*) x

    count =0
    count2=0
    do i=1,N
    	b(i) = x(i)-x(1)
        count = count +x(i)
        count2= count2 +real(x(i))
    end do

    average = count / N
    average2 = count2 /N

    sum1= 0
    if (maxval(b) ==minval(b)) then
    	write(*,*) 0
    else if (abs(average2-average) >= abs(average2-average-1) ) then
    	do i=1,N
        	sum1= sum1 + (x(i)-average-1)**2
        end do
        write(*,*) sum1
    else
    	do i=1,N
        	sum1 = sum1 + (x(i)-average) **2
    	end do
    	write(*,*) sum1
    end if

	deallocate(x)
    stop
end program sample

Score : 200 points

Problem Statement

Evi has N integers a_1,a_2,..,a_N. His objective is to have N equal integers by transforming some of them.

He may transform each integer at most once. Transforming an integer x into another integer y costs him (x-y)^2 dollars. Even if a_i=a_j (i≠j), he has to pay the cost separately for transforming each of them (See Sample 2).

Find the minimum total cost to achieve his objective.

Constraints

1≦N≦100

-100≦a_i≦100

Input

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

N
a_1 a_2 ... a_N

Output

Print the minimum total cost to achieve Evi's objective.

Sample Input 1

2
4 8

Sample Output 1

8

Transforming the both into 6s will cost (4-6)^2+(8-6)^2=8 dollars, which is the minimum.

Sample Input 2

3
1 1 3

Sample Output 2

3

Transforming the all into 2s will cost (1-2)^2+(1-2)^2+(3-2)^2=3 dollars. Note that Evi has to pay (1-2)^2 dollar separately for transforming each of the two 1s.

Sample Input 3

3
4 2 5

Sample Output 3

5

Leaving the 4 as it is and transforming the 2 and the 5 into 4s will achieve the total cost of (2-4)^2+(5-4)^2=5 dollars, which is the minimum.

Sample Input 4

4
-100 -100 -100 -100

Sample Output 4

0

Without transforming anything, Evi's objective is already achieved. Thus, the necessary cost is 0.


integer N
integer,allocatable,dimension(:)::a
integer ans,kouho
read*,N
allocate(a(N))
read*,a
ans=huge(ans)
do i=minval(a),maxval(a)
  kouho=0
  do j=1,N
    kouho=kouho+(i-a(j))**2
  end do
  ans=max(ans,kouho)
end do
print"(i0)",ans
end


program main
read*,i,j,k
if(i*j*k==175)then
print*,"YES"
else
print*,"NO"
endif
end program main

Score : 100 points

Problem Statement

Iroha loves Haiku. Haiku is a short form of Japanese poetry. A Haiku consists of three phrases with 5, 7 and 5 syllables, in this order.

To create a Haiku, Iroha has come up with three different phrases. These phrases have A, B and C syllables, respectively. Determine whether she can construct a Haiku by using each of the phrases once, in some order.

Constraints

1≦A,B,C≦10

Input

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

A B C

Output

If it is possible to construct a Haiku by using each of the phrases once, print YES (case-sensitive). Otherwise, print NO.

Sample Input 1

5 5 7

Sample Output 1

YES

Using three phrases of length 5, 5 and 7, it is possible to construct a Haiku.

Sample Input 2

7 7 5

Sample Output 2

NO


integer a, b, c
read*, a, b, c
if ((a * b * c) == 175) then
	print*, "Yes"
else
	print*, "No"
end if
end


module mod_t_item
  implicit none
  type :: t_item
    integer :: pos, cost
  end type t_item
  private
  public :: t_item, itemize, compare, swap
contains
  function itemize(pos,cost) result(i)
    implicit none
    integer :: pos, cost
    type(t_item) :: i
    i%pos = pos
    i%cost = cost
    return
  end function itemize
  function compare(i1,i2) result(c)
    implicit none
    type(t_item), intent(in) :: i1, i2
    integer :: c
    c = i1%cost-i2%cost
    return
  end function compare
  subroutine swap(i1,i2)
    implicit none
    type(t_item), intent(inout) :: i1, i2
    type(t_item) :: i
    i = i1
    i1 = i2
    i2 = i
    return
  end subroutine swap
end module mod_t_item
module mod_t_list
  use mod_t_item
  implicit none
  type :: t_list
    integer :: vol
    type(t_item), pointer :: arr(:)
  end type t_list
  private
  public :: t_list, init_list, release_list,push_back
  public :: pop_back, get_at, size_of_list
contains
  subroutine init_list(list)
    implicit none
    type(t_list), intent(inout) :: list
    list%vol = 0
    allocate(list%arr(1))
    return
  end subroutine init_list
  subroutine release_list(list)
    implicit none
    type(t_list), intent(inout) :: list
    if (associated(list%arr)) deallocate(list%arr)
    return
  end subroutine release_list
  subroutine push_back(list,item)
    implicit none
    type(t_list), intent(inout) :: list
    type(t_item), intent(in) :: item
    type(t_item), allocatable :: tmp(:)
    if (size(list%arr).eq.list%vol) then
      allocate(tmp(list%vol))
      tmp = list%arr
      deallocate(list%arr)
      allocate(list%arr(2*list%vol))
      list%arr(1:list%vol) = tmp
      deallocate(tmp)
    end if
    list%vol = list%vol+1
    list%arr(list%vol) = item
    return
  end subroutine push_back
  function pop_back(list) result(item)
    implicit none
    type(t_list), intent(inout) :: list
    type(t_item) :: item
    item = list%arr(list%vol)
    list%vol = list%vol-1
    return
  end function pop_back
  function get_at(list,i) result(item)
    implicit none
    type(t_list), intent(in) :: list
    integer, intent(in) :: i
    type(t_item) :: item
    item = list%arr(i)
    return
  end function get_at
  function size_of_list(list) result(s)
    implicit none
    type(t_list), intent(in) :: list
    integer :: s
    s = list%vol
    return
  end function size_of_list
end module mod_t_list
module mod_t_priority_queue
  use mod_t_item
  use mod_t_list
  implicit none
  type :: t_heap
    type(t_list) :: list
  end type t_heap
  private
  public :: t_heap, init_heap, release_heap, push, pop, size_of_heap
contains
  subroutine init_heap(heap)
    implicit none
    type(t_heap), intent(inout) :: heap
    call init_list(heap%list)
    return
  end subroutine init_heap
  subroutine release_heap(heap)
    implicit none
    type(t_heap), intent(inout) :: heap
    call release_list(heap%list)
    return
  end subroutine release_heap
  subroutine push(heap,item)
    implicit none
    type(t_heap), intent(inout) :: heap
    type(t_item), intent(in) :: item
    integer :: n, i
    call push_back(heap%list,item)
    n = heap%list%vol
    do while (n.gt.1)
      i = n/2
      if (compare(heap%list%arr(n),heap%list%arr(i)).lt.0_8) then
        call swap(heap%list%arr(n),heap%list%arr(i))
      end if
      n = i
    end do
    return
  end subroutine push
  function pop(heap) result(item)
    implicit none
    type(t_heap), intent(inout) :: heap
    type(t_item) :: item, tmp
    integer :: n, i, j
    n = heap%list%vol
    item = heap%list%arr(1)
    heap%list%arr(1) = heap%list%arr(n)
    tmp = pop_back(heap%list)
    i = 1
    do while (2*i.lt.n)
      j = 2*i
      if ((j.lt.n-1).and.(compare(heap%list%arr(j+1),heap%list%arr(j)).lt.0_8)) then
        j = j+1
      end if
      if (compare(heap%list%arr(j),heap%list%arr(i)).lt.0_8) then
        call swap(heap%list%arr(j),heap%list%arr(i))
      end if
      i = j
    end do
    return
  end function pop
  function size_of_heap(heap) result(s)
    implicit none
    type(t_heap), intent(in) :: heap
    integer :: s
    s = size_of_list(heap%list)
    return
  end function size_of_heap
end module mod_t_priority_queue
module mod_t_graph
  use mod_t_item
  use mod_t_list
  use mod_t_priority_queue
  implicit none
  integer, parameter :: infty = 1000000000
  type :: t_graph
    type(t_list), pointer :: edges(:)
  end type t_graph
  private
  public :: infty
  public :: t_graph, init_graph, release_graph, add_edge, add, dijkstra
contains
  subroutine init_graph(graph,n)
    implicit none
    type(t_graph), intent(inout) :: graph
    integer, intent(in) :: n
    integer :: i
    allocate(graph%edges(n))
    do i = 1, n
      call init_list(graph%edges(i))
    end do
    return
  end subroutine init_graph
  subroutine release_graph(graph)
    implicit none
    type(t_graph), intent(inout) :: graph
    integer :: n, i
    if (.not.associated(graph%edges)) return
    n = size(graph%edges)
    do i = 1, n
      call release_list(graph%edges(i))
    end do
    deallocate(graph%edges)
    return
  end subroutine release_graph
  subroutine add_edge(graph,i,edge)
    implicit none
    type(t_graph), intent(inout) :: graph
    type(t_item), intent(in) :: edge
    integer, intent(in) :: i
    call push_back(graph%edges(i),edge)
    return
  end subroutine add_edge
  subroutine add(graph,i,pos,cost)
    implicit none
    type(t_graph), intent(inout) :: graph
    integer, intent(in) :: i, pos, cost
    call push_back(graph%edges(i),itemize(pos,cost))
    return
  end subroutine add
  function dijkstra(graph,start) result(c)
    implicit none
    type(t_graph), intent(in) :: graph
    integer, intent(in) :: start
    type(t_heap) :: pq
    type(t_item) :: now, next
    integer :: n, i, c(size(graph%edges))
    n = size(graph%edges)
    c = infty
    call init_heap(pq)
    c(start) = 0
    call push(pq,itemize(start,c(start)))
    do while (size_of_heap(pq).gt.0)
      now = pop(pq)
      if (now%cost.gt.c(now%pos)) cycle
      do i = 1, size_of_list(graph%edges(now%pos))
        next = get_at(graph%edges(now%pos),i)
        if (c(next%pos).gt.c(now%pos)+next%cost) then
          c(next%pos) = c(now%pos)+next%cost
          call push(pq,itemize(next%pos,c(next%pos)))
        end if
      end do
    end do
    call release_heap(pq)
    return
  end function dijkstra
end module mod_t_graph
program shorten_diameter
  use mod_t_graph
  implicit none
  type(t_graph) :: tree
  integer :: n, k, a(2000), b(2000), d(2000,2000), m, i, j, s
  a = 0
  b = 0
  d = 0
  read(*,*) n, k
  call init_graph(tree,n)
  do i = 1, n-1
    read(*,*) a(i), b(i)
    call add(tree,a(i),b(i),1)
    call add(tree,b(i),a(i),1)
  end do
  do i = 1, n
    d(i,1:n) = dijkstra(tree,i)
  end do
  m = 1000000000
  if (mod(k,2).eq.0) then
    do i = 1, n
      s = 0
      do j = 1, n
        if (d(i,j).gt.k/2) s = s+1
      end do
      m = min(m,s)
    end do
  else
    do i = 1, n-1
      s = 0
      do j = 1, n
        if (d(a(i),j)+d(b(i),j).gt.k) s = s+1
      end do
      m = min(m,s)
    end do
  end if
  write(*,'(i0)') m
  call release_graph(tree)
  stop
end program shorten_diameter

Score : 600 points

Problem Statement

Given an undirected tree, let the distance between vertices u and v be the
number of edges on the simple path from u to v.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree good if and only if its diameter is at most K.

You are given an undirected tree with N vertices numbered 1 through N.
For each i (1≦i≦N-1), there is an edge connecting vertices A_i and B_i.

You want to remove zero or more vertices from the tree, so that the resulting tree is good.
When a vertex is removed, all incident edges will also be removed.
The resulting graph must be connected.

Find the minimum number of vertices that you need to remove in order to produce a good tree.

Constraints

2≦N≦2000

1≦K≦N-1

1≦A_i≦N, 1≦B_i≦N

The graph defined by A_i and B_i is a tree.

Input

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

N K
A_1 B_1
A_2 B_2
:
A_{N-1} B_{N-1}

Output

Print the minimum number of vertices that you need to remove in order to produce a good tree.

Sample Input 1

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

Sample Output 1

2

The tree is shown below. Removing vertices 5 and 6 will result in a good tree with the diameter of 2.

Sample Input 2

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

Sample Output 2

0

Since the given tree is already good, you do not need to remove any vertex.