program sample
    implicit none

    integer(8) :: i,j,m,n,a,b,c,d,dis,p,q,ma
    integer(8),allocatable :: x(:),y(:),indy(:),ans(:)

    read(*,*) n,m
    a=m/2
    allocate(x(n),y(a),indy(a),ans(n))
    read(*,*)x
    call msort(x)
    ans(:)=x(:)
    y(:)=0
    q=1  !キューの要素数

    y(1)=x(n)
    indy(1)=n
    do i=1,m
        ma=0
        do j=1,q !キューでの最大値とその場所
            if(y(j)>ma)then
                ma=y(j)
                b=j
            end if
        end do
        a=indy(b)
        ans(a)=x(a)/2**b
        indy(b)=a-1
        y(b)=x(a-1)/2**b
        if(b==q)then
            q=q+1
            y(q)=x(n)/2**q
            indy(q)=n
        end if
    end do
    write(*,*)sum(ans)

    stop
    contains
    recursive subroutine msort(x)
    implicit none
    integer(8), intent(inout) :: x(:)

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

    n = size(x)

    if(n == 1) then
      return
    end if

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

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

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

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

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

    deallocate(tmp)
    return
  end subroutine msort
end program sample

Score : 400 points

Problem Statement

Takahashi is going to buy N items one by one.

The price of the i-th item he buys is A_i yen (the currency of Japan).

He has M discount tickets, and he can use any number of them when buying an item.

If Y tickets are used when buying an item priced X yen, he can get the item for \frac{X}{2^Y} (rounded down to the nearest integer) yen.

What is the minimum amount of money required to buy all the items?

Constraints

All values in input are integers.

1 \leq N, M \leq 10^5

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 ... A_N

Output

Print the minimum amount of money required to buy all the items.

Sample Input 1

3 3
2 13 8

Sample Output 1

9

We can buy all the items for 9 yen, as follows:

Buy the 1-st item for 2 yen without tickets.

Buy the 2-nd item for 3 yen with 2 tickets.

Buy the 3-rd item for 4 yen with 1 ticket.

Sample Input 2

4 4
1 9 3 5

Sample Output 2

6

Sample Input 3

1 100000
1000000000

Sample Output 3

0

We can buy the item priced 1000000000 yen for 0 yen with 100000 tickets.

Sample Input 4

10 1
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 4

9500000000


program main
integer :: n,m,big
integer,allocatable :: a(:)
read(*,*)n,m
allocate(a(n))
read(*,*)a(1:n)
do i = 1,m
big = maxloc(a,1)
a(big) = a(big) / 2
end do
write(*,*)sum(a)
end program

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.


!
! The below module was made in reference to
! https://ufcpp.net/study/algorithm/col_heap.html
!
module heappq

  implicit none
  integer(8),allocatable :: hp(:)

contains
subroutine heapinit(n)

  integer, intent(in) :: n

  allocate( hp(n) ); hp = 0

end subroutine heapinit

subroutine heappush( newnp, newn )

  integer, intent(in)    :: newn
  integer, intent(in)    :: newnp
  integer :: i,ln, tmp

  ln = newn
  hp(ln) = newnp

  do while ( ln /= 1 )
    i = ln/2
    if ( hp(i) < hp(ln) ) then
      tmp    = hp(ln)
      hp(ln) = hp(i)
      hp(i)  = tmp
    end if
    ln = i
  end do

end subroutine heappush

subroutine heappop( n )

  integer :: i,j,tmp
  integer,intent(in) :: n

  hp(1) = hp(n)

  i = 1
  do while ( 2*i < n )
    j = 2*i
    if ( j /= n - 1 .and. hp(j+1) > hp(j) ) j = j + 1
    if ( hp(i) < hp(j) ) then
      tmp    = hp(j)
      hp(j) = hp(i)
      hp(i)  = tmp
    end if
    i = j
  end do

end subroutine heappop

end module heappq

program main

  use heappq

  implicit none ! 02;01
  integer :: n, m
  integer, allocatable :: a(:)
  integer :: i,j, tmp, tmpo
  integer(8) :: money

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

  call heapinit(n)

  do i = 1, n
    call heappush( a(i), i )
  end do

  do j = 1, m
    tmp = hp(1) / 2
    call heappop(n)

    call heappush(tmp, n)

  end do

  a = hp
  money = 0
  do i = 1, n
    money = money + int( a(i), kind = 8 )
  end do
  print*, money

end program main


!
! The below module was made in reference to
! https://ufcpp.net/study/algorithm/col_heap.html
!
module heappq

  implicit none
  integer(8),allocatable :: hp(:)

contains
subroutine heapinit(n)

  integer, intent(in) :: n

  allocate( hp(n) ); hp = 0

end subroutine heapinit

subroutine heappush( newnp, newn )

  integer, intent(in)    :: newn
  integer, intent(in)    :: newnp
  integer :: i,ln, tmp

  ln = newn
  hp(ln) = newnp

  do while ( ln /= 1 )
    i = ln/2
    if ( hp(i) < hp(ln) ) then
      tmp    = hp(ln)
      hp(ln) = hp(i)
      hp(i)  = tmp
    end if
    ln = i
  end do

end subroutine heappush

subroutine heappop( n )

  integer :: i,j,ln, tmp
  integer,intent(in) :: n

  hp(1) = hp(n)

  i = 1
  do while ( 2*i > n )
    j = 2*i
    if ( j /= n - 1 .and. hp(j+1) > hp(j) ) j = j + 1
    if ( hp(i) < hp(j) ) then
      tmp    = hp(ln)
      hp(ln) = hp(i)
      hp(i)  = tmp
    end if
    i = j
  end do

end subroutine heappop

end module heappq

program main

  use heappq

  implicit none ! 02;01
  integer :: n, m
  integer, allocatable :: a(:)
  integer :: i,j, tmp, tmpo
  integer(8) :: money

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

  call heapinit(n)

  do i = 1, n
    call heappush( a(i), i )
  end do

  a = hp
  do j = 1, m
    tmp = hp(1) / 2
    call heappop(n)
    call heappush(tmp, n)
  a = hp
  end do

  a = hp
  money = 0
  do i = 1, n
    money = money + int( a(i), kind = 8 )
  end do
  print*, money

end program main


program main

  implicit none ! 02;01
  integer :: n, m
  integer, allocatable :: a(:)
  integer :: i,j, tmp, tmpo
  integer(8) :: money

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

  call quick_sort( a, 1, n )

  do j = 1, m
    a(maxloc( a )) = a(maxloc( a )) / 2
  end do
  money = 0
  do i = 1, n
    money = money + int( a(i), kind = 8 )
  end do
  print*, money

end program main

recursive subroutine quick_sort( a, i, j )

  implicit none
  integer, intent(in) :: i,j
  integer, intent(inout) :: a(i:j)
  integer :: p, pl, k, tmp

  if( i == j ) return

  p  = pivot(a,i,j)

  if( p /= -1 ) then
    call partition(a(i:j),i,j,a(p),k)
    call quick_sort(a(i:k-p),i,k-1)
    call quick_sort(a(k:j),k,j)
  end if

contains

subroutine partition( a, i, j, x, l )

  implicit none
  integer, intent(in) :: i,j,x
  integer, intent(inout) :: a(i:j)
  integer, intent(out) :: l
  integer :: r, t

  l = i
  r = j

  do while( l<=r )
    do while( l<=j .and. a(l) <  x )
      l = l+1
    end do
    do while( r> i .and. a(r) >= x )
      r = r-1
    end do
    if(l>r) exit
    t = a(l)
    a(l) = a(r)
    a(r) = t
    l = l+1
    r = r-1
  end do

end subroutine partition

function pivot( a, i, j ) result( k )

  implicit none
  integer, intent(in) :: i,j
  integer, intent(in) :: a(i:j)
  integer :: k

  k = i + 1

  do while( k<=j .and. a(i) == a(k) )
    k = k + 1
  end do

  if(k>j) k = -1

  if(a(i)>a(k)) k = i

end function pivot

end subroutine quick_sort


program main

  implicit none ! 02;01
  integer :: n, m
  integer, allocatable :: a(:)
  integer :: i,j, tmp, tmpo
  integer(8) :: money

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

  call quick_sort( a, 1, n )
  print*, a

  do j = 1, m
    a(maxloc( a )) = a(maxloc( a )) / 2
  end do
  money = 0
  do i = 1, n
    money = money + int( a(i), kind = 8 )
  end do
  print*, money

end program main

recursive subroutine quick_sort( a, i, j )

  implicit none
  integer, intent(in) :: i,j
  integer, intent(inout) :: a(i:j)
  integer :: p, pl, k, tmp

  if( i == j ) return

  p  = pivot(a,i,j)

  if( p /= -1 ) then
    call partition(a(i:j),i,j,a(p),k)
    call quick_sort(a(i:k-p),i,k-1)
    call quick_sort(a(k:j),k,j)
  end if

contains

subroutine partition( a, i, j, x, l )

  implicit none
  integer, intent(in) :: i,j,x
  integer, intent(inout) :: a(i:j)
  integer, intent(out) :: l
  integer :: r, t

  l = i
  r = j

  do while( l<=r )
    do while( l<=j .and. a(l) <  x )
      l = l+1
    end do
    do while( r> i .and. a(r) >= x )
      r = r-1
    end do
    if(l>r) exit
    t = a(l)
    a(l) = a(r)
    a(r) = t
    l = l+1
    r = r-1
  end do

end subroutine partition

function pivot( a, i, j ) result( k )

  implicit none
  integer, intent(in) :: i,j
  integer, intent(in) :: a(i:j)
  integer :: k

  k = i + 1

  do while( k<=j .and. a(i) == a(k) )
    k = k + 1
  end do

  if(k>j) k = -1

  if(a(i)>a(k)) k = i

end function pivot

end subroutine quick_sort


program D
    implicit none
    integer::n,m,i,ans
    integer,allocatable::a(:)
    read*,n,m
    allocate(a(n))
    read*,a
    if ( n == 1) then
        ans = (a(0) / 2 ) ** m
    else
        do i = 0 , m
            a(maxloc(a,1)) = a(maxloc(a,1)) / 2
        end do
        ans = sum(a)
    end if
    print*,ans
end program D


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

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

    do while(m > 0)
        p = maxloc(a(1:n),1)
        a(p) = a(p) / 2
        m = m - 1
    end do

    print*, sum(a)

end program


      program answer
      implicit none
      integer(8) N,M
      integer(8),allocatable,dimension(:) :: A
      double precision,allocatable,dimension(:) :: B
      double precision :: tmp
      integer(8) :: i,j,res
      intrinsic dble,maxval
      res = 0
      read(*,*) N,M
      allocate(A(N),B(N))
      read(*,*) (A(i),i=1,N)
      do i=1,N
        B(i) = dble(A(i))
      end do
      do i=1,M
        tmp = maxval(B)
      lp:do j=1,N
          if( tmp == B(j)) then
            B(j) = tmp/2.0d0
            exit lp
          end if
        end do lp
      end do
      do i=1,N
        A(i) = B(i) + 0.0
        res = res + A(i)
      end do
      write(*,'(I0)') res
      end program answer


program main
  implicit none
  integer :: N, M
  integer(8), allocatable :: A(:)
  integer(8), allocatable :: A2(:)
!  integer(8), allocatable :: A3(:)
  integer :: tmp
  integer :: j2, js
  integer :: i,j
  logical :: fmin
  read(*,*) N,M
  allocate(A(N))
!  allocate(A3(N))
  read(*,*) A(:)
  A2 = iqsort(A) ! sorted
  j2 = N-1
  do i = 1,M
     A2(N) = A2(N) / 2
     !write(*,*) "i:",i
     fmin=.true.
     do j = j2,1,-1
        if (   A2(j) <= A2(N)) then
           ! insert A(N) to A(j+1)
           A(j+1:N) = A2(j+1:N) ! save
           !A2(1:j-2) = A(1:j-2)
           A2(j+1) = A(N)
           A2(j+2:N) = A(j+1:N-1)
           js = j
           fmin = .false.
           exit
        end if
     end do
     if ( fmin ) then
        j = 1
        ! insert A(N) to A(j+1)
        A(j:N) = A2(j:N) ! save
        !A2(1:j-2) = A(1:j-2)
        A2(j) = A(N)
        A2(j+1:N) = A(j:N-1)
        js = 1
     end if
     j2 = js
     !write(*,*) "A2:",A2
  end do
  write(*,'(i0)') sum(A2)
  stop
contains
  recursive function iqsort(ix) result(ires)
    integer(8), allocatable :: ires(:)
    integer(8), intent(in)  :: ix(:)
    integer(8) :: k
    if (size(ix) <= 1) then
       ires = ix
    else
       k = ix(size(ix) / 2)
       ires = [iqsort(pack(ix, ix < k)), pack(ix, ix == k), iqsort(pack(ix, ix > k))]
    end if
  end function iqsort
end program


program kadai
implicit none
integer(8)  ::  n,m
integer(8),allocatable  ::  a(:)
integer(8)  ::  loc,i,j

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

call qsort(a)

do i=1,m
    a(1)=a(1)/2
    do j=1,n-1
        if (a(j)>a(j+1) .or. a(j)==a(j+1)) then
            exit
        end if
        call swap(a(j),a(j+1))
    end do
end do

write(*,*) sum(a)

stop
contains

subroutine swap(a,b)
    implicit none
    integer(8),intent(inout)  ::  a,b
    integer(8)  ::  c
    c=a
    a=b
    b=c
end subroutine swap

subroutine qsort(x)
    implicit none
    integer(8), intent(inout)  ::  x(:)
    integer(8)  ::  first,last
    first=1
    last=size(x)
    call q2sort(x,first,last)
    return
end subroutine qsort

recursive subroutine q2sort(x,first,last)
    implicit none
    integer(8), intent(inout)  ::  x(:)
    integer(8), intent(inout) ::  first,last
    integer(8)  ::  fir, las
    integer(8)  ::  i,j
    integer(8)  ::  n
    n=x((first+last)/2)
    i=first
    j=last
    do
    do while (x(i)>n)
    i=i+1
    end do
    do while(x(j)<n)
    j=j-1
    end do
    if (i>j .or. i==j) then
    exit
    end if
    call swap(x(i),x(j))
    i=i+1
    j=j-1
    end do
    if (first<i-1) then
    las=i-1
    call q2sort(x,first,las)
    end if
    if (last>j+1) then
    fir=j+1
    call q2sort(x,fir,last)
    end if
end subroutine q2sort
end program kadai


      program answer
      implicit none
      integer(8) N,M
      integer(8),allocatable,dimension(:) :: A
      double precision,allocatable,dimension(:) :: B
      double precision :: tmp
      integer(8) :: i,j,res
      intrinsic dble,maxval
      res = 0
      read(*,*) N,M
      allocate(A(N),B(N))
      read(*,*) (A(i),i=1,N)
      do i=1,N
        B(i) = dble(A(i))
      end do
      do i=1,M
        tmp = maxval(B)/2.0d0
      lp:do j=1,N
          if( maxval(B) == B(j)) then
            B(j) = tmp
            exit lp
          end if
        end do lp
      end do
      write(*,*) res
      do i=1,N
        A(i) = B(i) + 0.0
        write(*,*) A(i),B(i),res
        res = res + A(i)
      end do
      write(*,'(I0)') res
      end program answer


program main
  implicit none
  integer(8) i, t, N, M, store
  integer(8),allocatable :: A(:)
  read(*, *) N, M
  allocate(A(N))
  read(*, *) (A(i), i = 1,N)
  call qsort(A)
  do t = 1,M
    A(N) = A(N)/2
    if (A(N) < A(N-1) .and. N /= 1) then
      call qsort(A)
    end if
  end do

  write(*, *) sum(A)
contains

recursive subroutine qsort(a)
implicit none
!降順
!integer(8),intent(in) :: n
integer(8),intent(inout) :: a(:)
integer(8)::i,j,toobig,toosmall,pv,n
n = size(a)
i = 1
j = n
pv = a((i+j)/2)
do
   do
     if (a(i) < pv) then
       i = i+1
     else
       exit
     end if
   end do
   do
     if(a(j) > pv) then
       j = j-1
     else
       exit
     end if
   end do
   if (i>=j) then
     exit
   else
     toobig = a(j)
     toosmall = a(i)
     a(i) = toobig
     a(j) = toosmall
     i = i+1
     j = j-1
   end if
 end do
 if (i > 2) then
   call qsort(a(1:i-1))
 end if
 if (j < n-1) then
   call qsort(a(j+1:n))
 end if
end subroutine qsort
end program main


program kadai
implicit none
integer(8)  ::  n,m
integer(8),allocatable  ::  a(:)
integer(8)  ::  loc,i,j

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

call qsort(a)

do i=1,m
    j=1
    do
        if (j==n) then
            a(j)=a(j)/2
            exit
        end if
        if (a(j)>a(j+1)) then
            a(j)=a(j)/2
            exit
        end if
        j=j+1
    end do
end do

write(*,*) sum(a)

stop
contains

subroutine swap(a,b)
    implicit none
    integer(8),intent(inout)  ::  a,b
    integer(8)  ::  c
    c=a
    a=b
    b=c
end subroutine swap

subroutine qsort(x)
    implicit none
    integer(8), intent(inout)  ::  x(:)
    integer(8)  ::  first,last
    first=1
    last=size(x)
    call q2sort(x,first,last)
    return
end subroutine qsort

recursive subroutine q2sort(x,first,last)
    implicit none
    integer(8), intent(inout)  ::  x(:)
    integer(8), intent(inout) ::  first,last
    integer(8)  ::  fir, las
    integer(8)  ::  i,j
    integer(8)  ::  n
    n=x((first+last)/2)
    i=first
    j=last
    do
    do while (x(i)>n)
    i=i+1
    end do
    do while(x(j)<n)
    j=j-1
    end do
    if (i>j .or. i==j) then
    exit
    end if
    call swap(x(i),x(j))
    i=i+1
    j=j-1
    end do
    if (first<i-1) then
    las=i-1
    call q2sort(x,first,las)
    end if
    if (last>j+1) then
    fir=j+1
    call q2sort(x,fir,last)
    end if
end subroutine q2sort
end program kadai


program test
implicit none
  real,allocatable::a(:)
  integer :: N,M,o,l,j,ans,i
  read*,N,M
  allocate(a(N))
  read(*,*)(a(i),i=1,N)
  o=0
  l=0
  do i=1,M
  	o=maxval(a)
  	do j=1,N
    	if(a(j)==o) then
        	l=j
        	exit
        end if
    end do
    	a(l)=aint(a(l)/2)
  end do
  do i=1,N
  ans=ans+a(i)
  end do
  print*,ans
end program test


program d
implicit none

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

read(*,*) n, m

allocate(a(n))

read(*,*) a

do i = 1, m
x = maxval(a)
y = maxloc(a)
x = int(x/2)
a(y(1)) = x
end do

write(*,'(i0)') sum(a)

end program


program kadai
implicit none
integer(8)  ::  n,m
integer(8),allocatable  ::  a(:)
integer(8)  ::  loc,i,j

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

do i=1,m
    call qsort(a)
    a(1)=a(1)/2
end do

write(*,*) sum(a)

stop
contains

subroutine swap(a,b)
    implicit none
    integer(8),intent(inout)  ::  a,b
    integer(8)  ::  c
    c=a
    a=b
    b=c
end subroutine swap

subroutine qsort(x)
    implicit none
    integer(8), intent(inout)  ::  x(:)
    integer(8)  ::  first,last
    first=1
    last=size(x)
    call q2sort(x,first,last)
    return
end subroutine qsort

recursive subroutine q2sort(x,first,last)
    implicit none
    integer(8), intent(inout)  ::  x(:)
    integer(8), intent(inout) ::  first,last
    integer(8)  ::  fir, las
    integer(8)  ::  i,j
    integer(8)  ::  n
    n=x((first+last)/2)
    i=first
    j=last
    do
    do while (x(i)<n)
    i=i+1
    end do
    do while(x(j)>n)
    j=j-1
    end do
    if (i>j .or. i==j) then
    exit
    end if
    call swap(x(i),x(j))
    i=i+1
    j=j-1
    end do
    if (first<i-1) then
    las=i-1
    call q2sort(x,first,las)
    end if
    if (last>j+1) then
    fir=j+1
    call q2sort(x,fir,last)
    end if
end subroutine q2sort
end program kadai


program main
  implicit none
  integer :: N, M
  integer(8), allocatable :: A(:)
  integer(8), allocatable :: A2(:)
  integer :: tmp
  integer :: i,j
  read(*,*) N,M
  allocate(A(N))
  read(*,*) A(:)
  A2 = iqsort(A) ! sorted
  do i = 1,M
     A2(N) = A2(N) / 2
     !write(*,*) "i:",i
     do j = 1,N
        if (   A2(N) <= A(j)) then
           ! insert A(N) to A(j)
           A(:) = A2(:) ! save
           !A2(1:j-2) = A(1:j-2)
           A2(j) = A(N)
           A2(j+1:N) = A(j:N-1)
           exit
        end if
     end do
     !write(*,*) "A2:",A2
  end do
  write(*,'(i0)') sum(A2)
  stop
contains
  recursive function iqsort(ix) result(ires)
    integer(8), allocatable :: ires(:)
    integer(8), intent(in)  :: ix(:)
    integer(8) :: k
    if (size(ix) <= 1) then
       ires = ix
    else
       k = ix(size(ix) / 2)
       ires = [iqsort(pack(ix, ix < k)), pack(ix, ix == k), iqsort(pack(ix, ix > k))]
    end if
  end function iqsort
end program


program kadai
implicit none
integer(8)  ::  n,m
integer(8),allocatable  ::  a(:)
integer(8)  ::  loc,i

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

do i=1,m
    loc=maxloc(a,1)
    a(loc)=a(loc)/2
end do

write(*,*) sum(a)

stop
contains

end program kadai


module mod_priority_queue
  implicit none
  type t_priority_queue
    private
    integer :: num = 0
    integer, pointer :: heap(:) => null()
  contains
    procedure :: offer => offer
    procedure :: clear => clear
    procedure :: poll => poll
    procedure :: peek => peek
    procedure :: size => size_of
  end type
contains
  integer function compare(a,b)
    integer, intent(in) :: a, b
    compare = b-a
  end
  subroutine offer(this,item)
    class(t_priority_queue), intent(inout) :: this
    integer, intent(in) :: item
    integer :: n, i, t
    integer, allocatable :: tmp(:)
    if (.not.associated(this%heap)) allocate(this%heap(1))
    if (this%num == size(this%heap)) then
      allocate(tmp(this%num))
      tmp = this%heap
      deallocate(this%heap)
      allocate(this%heap(2*this%num))
      this%heap(1:this%num) = tmp
      deallocate(tmp)
    end if
    this%num = this%num+1
    this%heap(this%num) = item
    n = this%num
    do while (n > 1)
      i = n/2
      if (compare(this%heap(n),this%heap(i)) < 0) then
        t = this%heap(n)
        this%heap(n) = this%heap(i)
        this%heap(i) = t
      end if
      n = i
    end do
  end
  subroutine clear(this)
    class(t_priority_queue), intent(inout) :: this
    if (associated(this%heap)) deallocate(this%heap)
    this%num = 0
  end
  function poll(this) result(item)
    class(t_priority_queue), intent(inout) :: this
    integer :: item, n, i, j, tmp
    n = this%num
    item = this%heap(1)
    this%heap(1) = this%heap(n)
    this%num = this%num-1
    i = 1
    do while (2*i < n)
      j = 2*i
      if (j+1 < n .and. compare(this%heap(j+1),this%heap(j)) < 0) j = j+1
      if (compare(this%heap(j),this%heap(i)) < 0) then
        tmp = this%heap(j)
        this%heap(j) = this%heap(i)
        this%heap(i) = tmp
      end if
      i = j
    end do
  end
  function peek(this) result(item)
    class(t_priority_queue), intent(in) :: this
    integer :: item
    item = this%heap(1)
  end
  integer function size_of(this)
    class(t_priority_queue), intent(in) :: this
    size_of = this%num
  end
end module mod_priority_queue
program powerful_discount_tickets
  use mod_priority_queue
  implicit none
  integer :: n, m, a(100000) = 0, i
  type(t_priority_queue) :: pq
  integer(8) :: ans = 0_8
  read(*,*) n, m
  read(*,*) a(1:n)
  do i = 1, n
    call pq%offer(a(i))
  end do
  do i = 1, m
    call pq%offer(pq%poll()/2)
  end do
  do i = 1, n
    ans = ans+int(pq%poll(),8)
  end do
  write(*,'(i0)') ans
end program powerful_discount_tickets


module mod_tree_map
  implicit none

  type t_entry
    private
    integer(16) :: key
    integer :: val
  end type t_entry

  type t_node
    private
    type(t_node), pointer :: left => null(), right => null()
    integer :: level = 1
    type(t_entry), pointer :: e => null()
  end type t_node

  type t_tree_map
    private
    type(t_node), pointer :: root => null()
    integer :: deflt = -1
  end type t_tree_map

contains

  function new_entry(key,val) result(e)
    integer(16), intent(in) :: key
    integer, intent(in) :: val
    type(t_entry), pointer :: e

    e => null()
    allocate(e)
    e%key = key
    e%val = val
  end

  function new_node(e) result(n)
    type(t_entry), pointer, intent(in) :: e
    type(t_node), pointer :: n

    n => null()
    allocate(n)
    n%e => e
  end

  logical function is_leaf(n)
    type(t_node), pointer, intent(in) :: n

    is_leaf = associated(n) .and. .not.associated(n%left) .and. &
    &         .not.associated(n%right)
  end

  recursive function tree_size(n) result(s)
    type(t_node), pointer, intent(in) :: n
    integer :: s

    s = 0
    if (.not.associated(n)) return
    s = 1+tree_size(n%left)+tree_size(n%right)
  end

  function skew(n) result(l)
    type(t_node), pointer, intent(in) :: n
    type(t_node), pointer :: l

    l => n
    if (.not.(associated(n) .and. associated(n%left) .and. &
    &   n%left%level == n%level)) return
    l => n%left
    n%left => l%right
    l%right => n
  end

  function split(n) result(r)
    type(t_node), pointer, intent(in) :: n
    type(t_node), pointer :: r

    r => n
    if (.not.(associated(n) .and. associated(n%right) .and. &
    &   associated(n%right%right) .and. n%right%right%level == n%level)) return
    r => n%right
    n%right => r%left
    r%left => n
    r%level = r%level+1
  end

  function predecessor(n) result(p)
    type(t_node), pointer, intent(in) :: n
    type(t_node), pointer :: p

    p => null()
    if (.not.associated(n%left)) return
    p => n%left
    do while (associated(p%right))
      p => p%right
    end do
  end

  function successor(n) result(s)
    type(t_node), pointer, intent(in) :: n
    type(t_node), pointer :: s

    s => null()
    if (.not.associated(n%right)) return
    s => n%right
    do while (associated(s%left))
      s => s%left
    end do
  end

  recursive function insert(n,e) result(t)
    type(t_node), pointer, intent(in) :: n
    type(t_entry), pointer, intent(in) :: e
    type(t_node), pointer :: t

    t => new_node(e)
    if (.not.associated(n)) return
    t => n
    if (e%key < t%e%key) then
      t%left => insert(t%left,e)
    else if (e%key > t%e%key) then
      t%right => insert(t%right,e)
    else
      t%e => e
    end if

    t => skew(t)
    t => split(t)
  end

  recursive function delete(n,e) result(t)
    type(t_node), pointer, intent(in) :: n
    type(t_entry), pointer, intent(in) :: e
    type(t_node), pointer :: t, l

    t => n
    if (.not.associated(n)) return
    if (e%key < t%e%key) then
      t%left => delete(t%left,e)
    else if (e%key > t%e%key) then
      t%right => delete(t%right,e)
    else
      if (is_leaf(t)) return
      if (.not.associated(t%left)) then
        l => successor(t)
        t%right => delete(t%right,l%e)
        t%e => l%e
      else
        l => predecessor(t)
        t%left => delete(t%left,l%e)
        t%e => l%e
      end if
    end if

    t => decrease_level(t)
    t => skew(t)
    t%right => skew(t%right)
    if (associated(t%right)) t%right%right => skew(t%right%right)
    t => split(t)
    t%right => split(t%right)
  end

  function decrease_level(n) result(t)
    type(t_node), pointer, intent(in) :: n
    type(t_node), pointer :: t
    integer :: should_be

    t => n
    should_be = min(t%left%level,t%right%level)+1
    if (t%level > should_be) then
      t%level = should_be
      if (t%right%level > should_be) t%right%level = should_be
    end if
  end

  recursive subroutine release_tree(t)
    type(t_node), pointer, intent(inout) :: t

    if (.not.associated(t)) return
    call release_tree(t%left)
    call release_tree(t%right)
    deallocate(t)
  end

  recursive subroutine get_keys_list(t,keys,num)
    type(t_node), pointer, intent(in) :: t
    integer(16), intent(inout) :: keys(:)
    integer, intent(inout) :: num

    if (.not.associated(t)) return
    call get_keys_list(t%left,keys,num)
    num = num+1
    keys(num) = t%e%key
    call get_keys_list(t%right,keys,num)
  end

  integer function size_of(map)
    type(t_tree_map), intent(in) :: map

    size_of = tree_size(map%root)
  end

  subroutine clear(map)
    type(t_tree_map), intent(inout) :: map

    call release_tree(map%root)
    map%root => null()
  end

  subroutine set_default(map,deflt)
    type(t_tree_map), intent(inout) :: map
    integer, intent(in) :: deflt

    map%deflt = deflt
  end

  subroutine put_entry(map,e)
    type(t_tree_map), intent(inout) :: map
    type(t_entry), pointer, intent(in) :: e

    map%root => insert(map%root,e)
  end

  subroutine remove_entry(map,e)
    type(t_tree_map), intent(inout) :: map
    type(t_entry), pointer, intent(in) :: e

    map%root => delete(map%root,e)
  end

  function get_entry(map,e) result(ret)
    type(t_tree_map), intent(inout) :: map
    type(t_entry), pointer, intent(in) :: e
    type(t_node), pointer :: n
    type(t_entry), pointer :: ret

    ret => null()
    n => map%root
    do while (associated(n))
      if (e%key < n%e%key) then
        n => n%left
      else if (e%key > n%e%key) then
        n => n%right
      else
        ret => n%e
        return
      end if
    end do
  end

  function contain_entry(map,e) result(ret)
    type(t_tree_map), intent(inout) :: map
    type(t_entry), pointer, intent(in) :: e
    type(t_node), pointer :: n
    logical :: ret

    ret = .false.
    n => map%root
    do while (associated(n))
      if (e%key < n%e%key) then
        n => n%left
      else if (e%key > n%e%key) then
        n => n%right
      else
        ret = .true.
        return
      end if
    end do
  end

  function get_first_entry(map) result(ret)
    type(t_tree_map), intent(inout) :: map
    type(t_node), pointer :: n
    type(t_entry), pointer :: ret

    ret => null()
    n => map%root
    if (.not.associated(n)) return
    do while (associated(n%left))
      n => n%left
    end do
    ret => n%e
  end

  function poll_first_entry(map) result(ret)
    type(t_tree_map), intent(inout) :: map
    type(t_node), pointer :: n
    type(t_entry), pointer :: ret

    ret => null()
    n => map%root
    if (.not.associated(n)) return
    do while (associated(n%left))
      n => n%left
    end do
    ret => n%e

    map%root => delete(map%root,ret)
  end

  function get_last_entry(map) result(ret)
    type(t_tree_map), intent(inout) :: map
    type(t_node), pointer :: n
    type(t_entry), pointer :: ret

    ret => null()
    n => map%root
    if (.not.associated(n)) return
    do while (associated(n%right))
      n => n%right
    end do
    ret => n%e
  end

  function poll_last_entry(map) result(ret)
    type(t_tree_map), intent(inout) :: map
    type(t_node), pointer :: n
    type(t_entry), pointer :: ret

    ret => null()
    n => map%root
    if (.not.associated(n)) return
    do while (associated(n%right))
      n => n%right
    end do
    ret => n%e

    map%root => delete(map%root,ret)
  end

  function floor_entry(map,e) result(ret)
    type(t_tree_map), intent(inout) :: map
    type(t_entry), pointer, intent(in) :: e
    type(t_node), pointer :: n
    type(t_entry), pointer :: ret

    ret => null()
    n => map%root
    do while (associated(n))
      if (e%key < n%e%key) then
        n => n%left
      else if (e%key > n%e%key) then
        if (.not.associated(ret)) then
          ret => n%e
          cycle
        end if
        if (e%key-ret%key > e%key-n%e%key) ret => n%e
        n => n%right
      else
        ret => n%e
        return
      end if
    end do
  end

  function lower_entry(map,e) result(ret)
    type(t_tree_map), intent(inout) :: map
    type(t_entry), pointer, intent(in) :: e
    type(t_entry), pointer :: ret

    ret => floor_entry(map,new_entry(e%key-1,0))
  end

  function ceiling_entry(map,e) result(ret)
    type(t_tree_map), intent(inout) :: map
    type(t_entry), pointer, intent(in) :: e
    type(t_node), pointer :: n
    type(t_entry), pointer :: ret

    ret => null()
    n => map%root
    do while (associated(n))
      if (e%key < n%e%key) then
        if (.not.associated(ret)) then
          ret => n%e
          cycle
        end if
        if (e%key-ret%key < e%key-n%e%key) ret => n%e
        n => n%left
      else if (e%key > n%e%key) then
        n => n%right
      else
        ret => n%e
        return
      end if
    end do
  end

  function higher_entry(map,e) result(ret)
    type(t_tree_map), intent(inout) :: map
    type(t_entry), pointer, intent(in) :: e
    type(t_entry), pointer :: ret

    ret => ceiling_entry(map,new_entry(e%key+1,0))
  end

  subroutine get_keys(map,keys,num)
    type(t_tree_map), intent(inout) :: map
    integer(16), intent(inout) :: keys(:)
    integer, intent(inout) :: num

    keys = 0
    num = 0
    call get_keys_list(map%root,keys,num)
  end

  subroutine put(map,key,val)
    type(t_tree_map), intent(inout) :: map
    integer(16), intent(in) :: key
    integer, intent(in) :: val

    call put_entry(map,new_entry(key,val))
  end

  subroutine remove(map,key)
    type(t_tree_map), intent(inout) :: map
    integer(16), intent(in) :: key

    call remove_entry(map,new_entry(key,0))
  end

  function get(map,key) result(val)
    type(t_tree_map), intent(inout) :: map
    integer(16), intent(in) :: key
    type(t_entry), pointer :: tmp
    integer :: val

    val = map%deflt
    tmp => get_entry(map,new_entry(key,0))
    if (.not.associated(tmp)) return
    val = tmp%val
  end

  logical function contain(map,key)
    type(t_tree_map), intent(inout) :: map
    integer(16), intent(in) :: key

    contain = contain_entry(map,new_entry(key,0))
  end

  function get_first_key(map) result(key)
    type(t_tree_map), intent(inout) :: map
    type(t_entry), pointer :: tmp
    integer(16) :: key

    key = map%deflt
    tmp => get_first_entry(map)
    if (.not.associated(tmp)) return
    key = tmp%key
  end

  function get_last_key(map) result(key)
    type(t_tree_map), intent(inout) :: map
    type(t_entry), pointer :: tmp
    integer(16) :: key

    key = map%deflt
    tmp => get_last_entry(map)
    if (.not.associated(tmp)) return
    key = tmp%key
  end

  function floor_key(map,key) result(ret)
    type(t_tree_map), intent(inout) :: map
    integer(16), intent(in) :: key
    type(t_entry), pointer :: tmp
    integer :: ret

    ret = map%deflt
    tmp => floor_entry(map,new_entry(key,0))
    if (.not.associated(tmp)) return
    ret = tmp%key
  end

  function lower_key(map,key) result(ret)
    type(t_tree_map), intent(inout) :: map
    integer(16), intent(in) :: key
    type(t_entry), pointer :: tmp
    integer :: ret

    ret = map%deflt
    tmp => lower_entry(map,new_entry(key,0))
    if (.not.associated(tmp)) return
    ret = tmp%key
  end

  function ceiling_key(map,key) result(ret)
    type(t_tree_map), intent(inout) :: map
    integer(16), intent(in) :: key
    type(t_entry), pointer :: tmp
    integer :: ret

    ret = map%deflt
    tmp => ceiling_entry(map,new_entry(key,0))
    if (.not.associated(tmp)) return
    ret = tmp%key
  end

  function higher_key(map,key) result(ret)
    type(t_tree_map), intent(inout) :: map
    integer(16), intent(in) :: key
    type(t_entry), pointer :: tmp
    integer :: ret

    ret = map%deflt
    tmp => higher_entry(map,new_entry(key,0))
    if (.not.associated(tmp)) return
    ret = tmp%key
  end

end module mod_tree_map

use mod_tree_map
integer N
character(5000) S

type(t_tree_map) ::seen1,seen2
integer mod1,mod2
integer base1,base2
integer(16) rh1,rh2
integer(16),allocatable,dimension(:)::pow1,pow2

integer ok,ng,md
logical mdcheck

integer i

read*,N
read*,S
mod1=10**9+9
mod2=10**9+7
base1=1007
base2=2009
allocate(pow1(0:N),pow2(0:N))
pow1(0)=1;pow2(0)=1
do i=1,N
  pow1(i)=mod(pow1(i-1)*base1,mod1)
  pow2(i)=mod(pow2(i-1)*base2,mod2)
end do

ok=0;ng=N/2+1
md=(ok+ng)/2
do while(ng-ok>1)
  mdcheck=.false.

  call clear(seen1)
  call clear(seen2)
  call set_default(seen1,5000)
  call set_default(seen2,5000)

  rh1=0;rh2=0
  do i=1,md
    rh1=mod(rh1+ichar(S(i:i))*pow1(md-i),mod1)
    rh2=mod(rh2+ichar(S(i:i))*pow2(md-i),mod2)
  end do
  call put(seen1,rh1,1)
  call put(seen2,rh2,1)
  do i=2,N-md+1
    rh1=modulo(mod(rh1*base1,mod1)+ichar(S(i+md-1:i+md-1))-mod(ichar(S(i-1:i-1))*pow1(md),mod1),mod1)
    rh2=modulo(mod(rh2*base2,mod2)+ichar(S(i+md-1:i+md-1))-mod(ichar(S(i-1:i-1))*pow2(md),mod2),mod2)
	if(rh1<0)rh1=rh1+mod1
    if(rh2<0)rh2=rh2+mod2
    if(i-get(seen1,rh1)>=md .and.i-get(seen2,rh2)>=md)then
      mdcheck=.true.
      exit
    else
      if(get(seen1,rh1)==5000)then
        call put(seen1,rh1,i)
      endif
      if(get(seen2,rh2)==5000)then
        call put(seen2,rh2,i)
      endif
    endif
  end do
  if(mdcheck)then
    ok=md
  else
    ng=md
  endif
  md=(ok+ng)/2
end do
print"(I0)",ok
end

Score : 500 points

Problem Statement

Given is a string S of length N.

Find the maximum length of a non-empty string that occurs twice or more in S as contiguous substrings without overlapping.

More formally, find the maximum positive integer len such that there exist integers l_1 and l_2 ( 1 \leq l_1, l_2 \leq N - len + 1 ) that satisfy the following:

l_1 + len \leq l_2

S[l_1+i] = S[l_2+i] (i = 0, 1, ..., len - 1)

If there is no such integer len, print 0.

Constraints

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

|S| = N

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the maximum length of a non-empty string that occurs twice or more in S as contiguous substrings without overlapping. If there is no such non-empty string, print 0 instead.

Sample Input 1

5
ababa

Sample Output 1

2

The strings satisfying the conditions are: a, b, ab, and ba. The maximum length among them is 2, which is the answer.
Note that aba occurs twice in S as contiguous substrings, but there is no pair of integers l_1 and l_2 mentioned in the statement such that l_1 + len \leq l_2.

Sample Input 2

2
xy

Sample Output 2

0

No non-empty string satisfies the conditions.

Sample Input 3

13
strangeorange

Sample Output 3

5


integer N
character(5000) S

integer,allocatable,dimension(:)::seen1,seen2,seen3,seen4,seen5
integer(16) mod1,mod2,mod3,mod5
integer(16) base1,base2,base3,base4,base5
integer(16) rh1,rh2,rh3,rh4,rh5
integer(16),allocatable,dimension(:)::pow1,pow2,pow3,pow4,pow5

integer ok,ng,md
logical mdcheck

integer i

read*,N
read*,S
mod1=30203
mod2=30211
mod3=6871
mod5=4096
allocate(seen1(0:mod1-1),seen2(0:mod2-1),seen3(0:mod3-1),seen4(0:mod3-1),seen5(0:mod5-1))
base1=353
base2=359
base3=139
base4=11
base5=64
allocate(pow1(0:N),pow2(0:N),pow3(0:N),pow4(0:N),pow5(0:N))
pow1(0)=1;pow2(0)=1;pow3(0)=1;pow4(0)=1;pow5(0)=1
do i=1,N
  pow1(i)=mod(pow1(i-1)*base1,mod1)
  pow2(i)=mod(pow2(i-1)*base2,mod2)
  pow3(i)=mod(pow3(i-1)*base3,mod3)
  pow4(i)=mod(pow4(i-1)*base4,mod3)
  pow5(i)=mod(pow5(i-1)*base5,mod5)
end do

ok=0;ng=N/2+1
md=(ok+ng)/2
do while(ng-ok>1)
  mdcheck=.false.

  seen1=-1;seen2=-1;seen3=-1;seen4=-1;seen5=-1
  rh1=0;rh2=0;rh3=0;rh4=0;rh5=0
  do i=1,md
    rh1=mod(rh1+ichar(S(i:i))*pow1(md-i),mod1)
    rh2=mod(rh2+ichar(S(i:i))*pow2(md-i),mod2)
    rh3=mod(rh3+ichar(S(i:i))*pow3(md-i),mod3)
    rh4=mod(rh4+ichar(S(i:i))*pow4(md-i),mod3)
    rh5=mod(rh5+ichar(S(i:i))*pow5(md-i),mod5)
  end do
  seen1(rh1)=1;seen2(rh2)=1;seen3(rh3)=1;seen4(rh4)=1;seen5(rh5)=1
  do i=2,N-md+1
    rh1=modulo(mod(rh1*base1,mod1)+ichar(S(i+md-1:i+md-1))-modulo(ichar(S(i-1:i-1))*pow1(md),mod1),mod1)
    rh2=modulo(mod(rh2*base2,mod2)+ichar(S(i+md-1:i+md-1))-modulo(ichar(S(i-1:i-1))*pow2(md),mod2),mod2)
    rh3=modulo(mod(rh3*base3,mod3)+ichar(S(i+md-1:i+md-1))-modulo(ichar(S(i-1:i-1))*pow3(md),mod3),mod3)
	rh4=modulo(mod(rh4*base4,mod3)+ichar(S(i+md-1:i+md-1))-modulo(ichar(S(i-1:i-1))*pow4(md),mod3),mod3)
    rh5=modulo(mod(rh5*base5,mod5)+ichar(S(i+md-1:i+md-1))-modulo(ichar(S(i-1:i-1))*pow5(md),mod5),mod5)
    if(rh1<0)rh1=rh1+mod1
    if(rh2<0)rh2=rh2+mod2
    if(rh3<0)rh3=rh3+mod3
    if(rh4<0)rh4=rh4+mod3
    if(rh5<0)rh5=rh5+mod5
    if(seen1(rh1)>0 .and. seen2(rh2)>0 .and. seen3(rh3)>0 .and.seen4(rh4)>0 .and.seen5(rh5)>0)then
      if(i-seen1(rh1)>=md .and. i-seen2(rh2)>=md.and.i-seen3(rh3)>=md.and.i-seen4(rh4)>=md.and.i-seen4(rh5)>=md)then
        mdcheck=.true.
        exit
      endif
    else
      if(seen1(rh1)<0)seen1(rh1)=i
      if(seen2(rh2)<0)seen2(rh2)=i
      if(seen3(rh3)<0)seen3(rh3)=i
      if(seen4(rh4)<0)seen4(rh4)=i
      if(seen4(rh5)<0)seen5(rh5)=i
    endif

  end do
  if(mdcheck)then
    ok=md
  else
    ng=md
  endif
  md=(ok+ng)/2
end do

print"(I0)",ok
end


integer N
character(5000) S

integer,allocatable,dimension(:)::seen1,seen2,seen3,seen4
integer mod1,mod2,mod3
integer base1,base2,base3,base4
integer(16) rh1,rh2,rh3,rh4
integer(16),allocatable,dimension(:)::pow1,pow2,pow3,pow4

integer ok,ng,md
logical mdcheck

integer i

read*,N
read*,S
mod1=30203
mod2=30211
mod3=6871
allocate(seen1(0:mod1-1),seen2(0:mod2-1),seen3(0:mod3-1),seen4(0:mod3-1))
base1=353
base2=359
base3=139
base4=11
allocate(pow1(0:N),pow2(0:N),pow3(0:N),pow4(0:N))
pow1(0)=1;pow2(0)=1;pow3(0)=1;pow4(0)=1
do i=1,N
  pow1(i)=mod(pow1(i-1)*base1,mod1)
  pow2(i)=mod(pow2(i-1)*base2,mod2)
  pow3(i)=mod(pow3(i-1)*base3,mod3)
  pow4(i)=mod(pow4(i-1)*base4,mod3)
end do

ok=0;ng=N/2+1
md=(ok+ng)/2
do while(ng-ok>1)
  mdcheck=.false.

  seen1=-1;seen2=-1;seen3=-1;seen4=-1
  rh1=0;rh2=0;rh3=0;rh4=0
  do i=1,md
    rh1=mod(rh1+ichar(S(i:i))*pow1(md-i),mod1)
    rh2=mod(rh2+ichar(S(i:i))*pow2(md-i),mod2)
    rh3=mod(rh3+ichar(S(i:i))*pow3(md-i),mod3)
    rh4=mod(rh4+ichar(S(i:i))*pow4(md-i),mod3)
  end do
  seen1(rh1)=1;seen2(rh2)=1;seen3(rh3)=1;seen4(rh4)=1
  do i=2,N-md+1
    rh1=modulo(mod(rh1*base1,mod1)+ichar(S(i+md-1:i+md-1))-modulo(ichar(S(i-1:i-1))*pow1(md),mod1),mod1)
    rh2=modulo(mod(rh2*base2,mod2)+ichar(S(i+md-1:i+md-1))-modulo(ichar(S(i-1:i-1))*pow2(md),mod2),mod2)
    rh3=modulo(mod(rh3*base3,mod3)+ichar(S(i+md-1:i+md-1))-modulo(ichar(S(i-1:i-1))*pow3(md),mod3),mod3)
	rh4=modulo(mod(rh4*base4,mod3)+ichar(S(i+md-1:i+md-1))-modulo(ichar(S(i-1:i-1))*pow4(md),mod3),mod3)
    if(rh1<0)rh1=rh1+mod1
    if(rh2<0)rh2=rh2+mod2
    if(rh3<0)rh3=rh3+mod3
    if(rh4<0)rh4=rh4+mod3
    if(seen1(rh1)>0 .and. seen2(rh2)>0 .and. seen3(rh3)>0 .and.seen4(rh4)>0)then
      if(i-seen1(rh1)>=md .and. i-seen2(rh2)>=md.and.i-seen3(rh3)>=md.and.i-seen4(rh4)>=md)then
        mdcheck=.true.
        exit
      endif
    else
      if(seen1(rh1)<0)seen1(rh1)=i
      if(seen2(rh2)<0)seen2(rh2)=i
      if(seen3(rh3)<0)seen3(rh3)=i
      if(seen4(rh4)<0)seen4(rh4)=i
    endif

  end do
  if(mdcheck)then
    ok=md
  else
    ng=md
  endif
  md=(ok+ng)/2
end do

print"(I0)",ok
end


integer N
character(5000) S

integer,allocatable,dimension(:)::seen1,seen2,seen3
integer mod1,mod2,mod3
integer base1,base2,base3
integer(16) rh1,rh2,rh3
integer,allocatable,dimension(:)::pow1,pow2,pow3

integer ok,ng,md
logical mdcheck

integer i

read*,N
read*,S
mod1=30203
mod2=30211
mod3=6871
allocate(seen1(0:mod1-1),seen2(0:mod2-1),seen3(0:mod3-1))
base1=353
base2=359
base3=139
allocate(pow1(0:N),pow2(0:N),pow3(0:N))
pow1(0)=1;pow2(0)=1;pow3(0)=1
do i=1,N
  pow1(i)=mod(pow1(i-1)*base1,mod1)
  pow2(i)=mod(pow2(i-1)*base2,mod2)
  pow3(i)=mod(pow3(i-1)*base3,mod3)
end do

ok=0;ng=N/2+1
md=(ok+ng)/2
do while(ng-ok>1)
  mdcheck=.false.

  seen1=-1;seen2=-1;seen3=-1
  rh1=0;rh2=0;rh3=0
  do i=1,md
    rh1=mod(rh1+ichar(S(i:i))*pow1(md-i),mod1)
    rh2=mod(rh2+ichar(S(i:i))*pow2(md-i),mod2)
    rh3=mod(rh3+ichar(S(i:i))*pow3(md-i),mod3)
  end do
  seen1(rh1)=1;seen2(rh2)=1;seen3(rh3)=1
  do i=2,N-md+1
    rh1=mod(mod(rh1*base1,mod1)+ichar(S(i+md-1:i+md-1))-mod(ichar(S(i-1:i-1))*pow1(md),mod1),mod1)
    rh2=mod(mod(rh2*base2,mod2)+ichar(S(i+md-1:i+md-1))-mod(ichar(S(i-1:i-1))*pow2(md),mod2),mod2)
    rh3=mod(mod(rh3*base3,mod3)+ichar(S(i+md-1:i+md-1))-mod(ichar(S(i-1:i-1))*pow3(md),mod3),mod3)
	if(rh1<0)rh1=rh1+mod1
    if(rh2<0)rh2=rh2+mod2
    if(rh3<0)rh3=rh3+mod3
    if(seen1(rh1)>0 .and. seen2(rh2)>0 .and. seen3(rh3)>0 )then
      if(i-seen1(rh1)>=md .and. i-seen2(rh2)>=md.and.i-seen3(rh3)>=md)then
        mdcheck=.true.
        exit
      endif
    else
      if(seen1(rh1)<0)seen1(rh1)=i
      if(seen2(rh2)<0)seen2(rh2)=i
      if(seen3(rh3)<0)seen3(rh3)=i
    endif
  end do
  if(mdcheck)then
    ok=md
  else
    ng=md
  endif
  md=(ok+ng)/2
end do

print"(I0)",ok
end


integer N
character(5000) S

integer,allocatable,dimension(:)::seen1,seen2
integer mod1,mod2
integer base1,base2
integer(16) rh1,rh2
integer,allocatable,dimension(:)::pow1,pow2

integer ok,ng,md
logical mdcheck

integer i

read*,N
read*,S
mod1=30203
mod2=30211
allocate(seen1(0:mod1-1),seen2(0:mod2-1))
base1=4051
base2=359
allocate(pow1(0:N),pow2(0:N))
pow1(0)=1;pow2(0)=1
do i=1,N
  pow1(i)=mod(pow1(i-1)*base1,mod1)
  pow2(i)=mod(pow2(i-1)*base2,mod2)
end do

ok=0;ng=N/2+1
md=(ok+ng)/2
do while(ng-ok>1)
  mdcheck=.false.

  seen1=-1;seen2=-1
  rh1=0;rh2=0
  do i=1,md
    rh1=mod(rh1+ichar(S(i:i))*pow1(md-i),mod1)
    rh2=mod(rh2+ichar(S(i:i))*pow2(md-i),mod2)
  end do
  seen1(rh1)=1;seen2(rh2)=1
  do i=2,N-md+1
    rh1=mod(mod(rh1*base1,mod1)+ichar(S(i+md-1:i+md-1))-mod(ichar(S(i-1:i-1))*pow1(md),mod1),mod1)
    rh2=mod(mod(rh2*base2,mod2)+ichar(S(i+md-1:i+md-1))-mod(ichar(S(i-1:i-1))*pow2(md),mod2),mod2)
	if(rh1<0)rh1=rh1+mod1
    if(rh2<0)rh2=rh2+mod2
    if(seen1(rh1)>0 .and. seen2(rh2)>0)then
      if(i-seen1(rh1)>=md .and. i-seen2(rh2)>=md)then
        mdcheck=.true.
        exit
      endif
    else
      seen1(rh1)=i;seen2(rh2)=i
    endif
  end do
  if(mdcheck)then
    ok=md
  else
    ng=md
  endif
  md=(ok+ng)/2
end do

print"(I0)",ok
end


program who_says_a_pun
  implicit none
  integer :: n, i, j, ans = 0, z(5000) = 0
  character(5000) :: s
  read(*,*) n
  read(*,*) s
  do i = 1, n
    z(1:n-i+1) = z_algorithm(s(i:n))
    do j = 2, n-i+1
      ans = max(ans,min(j-1,z(j)))
    end do
  end do
  write(*,'(i0)') ans
contains
  function z_algorithm(s) result(z)
    implicit none
    character(*) :: s
    integer :: z(len_trim(s)), n, i, j, k
    z = 0
    n = len_trim(s)
    z(1) = n
    i = 1
    j = 0
    do while (i < n)
      do while (i+j < n .and. s(j+1:j+1) == s(i+j+1:i+j+1))
        j = j+1
      end do
      z(i+1) = j
      if (j == 0) then
        i = i+1
        cycle
      end if
      k = 1
      do while (i+k < n .and. k+z(k+1) < j)
        z(i+k+1) = z(k+1)
        k = k+1
      end do
      i = i+k
      j = j-k
    end do
  end
end program who_says_a_pun


program who_says_a_pun
  implicit none
  integer :: n, i, j, ans = 0, cntl(26) = 0, cntr(26) = 0
  character(5000) :: s
  read(*,*) n
  read(*,*) s
  do i = 1, n
    j = ord(s(i:i))
    cntr(j) = cntr(j)+1
  end do
  do i = 1, n-1
    j = ord(s(i:i))
    cntr(j) = cntr(j)-1
    cntl(j) = cntl(j)+1
    if (cntr(j)-cntl(j) < -1) cycle
    ans = max(ans,lcs(s(1:i),s(i+1:n)))
  end do
  write(*,'(i0)') ans
contains
  integer function ord(s)
    character(1), intent(in) :: s
    ord = ichar(s)-96
  end
  function lcs(s,t) result(ret)
    character(*), intent(in) :: s, t
    integer :: n, m, i, j, ret
    integer, allocatable :: dp(:,:)
    n = len_trim(s)
    m = len_trim(t)
    allocate(dp(0:n,0:m))
    dp = 0
    do i = 1, n
      do j = 1, m
        if (s(i:i) == t(j:j)) then
          dp(i,j) = dp(i-1,j-1)+1
        else
          dp(i,j) = max(dp(i-1,j),dp(i,j-1))
        end if
      end do
    end do
    ret = dp(n,m)
  end
end program who_says_a_pun


program sample
	integer ::A
    character(len=10) :: b

    read(*,*) A
    read(*,*) b

    if (A>=3200) then
    	write(*,*) trim(b)
    else
    	write(*,*) 'red'
    end if
    stop
end program sample

Score : 100 points

Problem Statement

You will be given an integer a and a string s consisting of lowercase English letters as input.

Write a program that prints s if a is not less than 3200 and prints red if a is less than 3200.

Constraints

2800 \leq a < 5000

s is a string of length between 1 and 10 (inclusive).

Each character of s is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

a
s

Output

If a is not less than 3200, print s; if a is less than 3200, print red.

Sample Input 1

3200
pink

Sample Output 1

pink

a = 3200 is not less than 3200, so we print s = pink.

Sample Input 2

3199
pink

Sample Output 2

red

a = 3199 is less than 3200, so we print red.

Sample Input 3

4049
red

Sample Output 3

red

a = 4049 is not less than 3200, so we print s = red.


program main
	implicit none
    integer::a,lenn
    character(10)::s
    read(*,*)a
    read(*,*)s
    lenn=len_trim(s)
    if(a>=3200)then
    	write(*,*) s(1:lenn)
    else
    	write(*,*) 'red'
    end if
    stop
end program main


program prob46
    implicit none
    integer :: a
    character(20) :: s
    read(*,*) a
    read(*,*) s
    if(a >= 3200) then
        write(*,'(a)') trim(adjustl(s))
    else
        write(*,'(a)') "red"
    end if

    stop
contains
end program prob46


program red_or_not
    implicit none
    integer(4):: a
    character(10):: s
    read*, a
    read*, s
    if (a >= 3200) then
        print*, s
    else
        print*, 'red'
    end if
end program red_or_not


program aaa

integer :: a
character(10) :: s

read*, a
read*, s

if (a>=3200) then
write(*,'(a)') s
else
write(*,'(a)') "red"
end if

end program


program main
	integer :: a
	character*10 :: s

	read *, a
	read *, s
	if(a >= 3200) then
    	print *, s
	else
    	print *,'red'
	end if
end program


module pair_stack
    implicit none
    private
    integer, allocatable:: stack(:)
    integer:: depth, max_depth
    public:: init, pop, push, is_remaining

contains
    subroutine init(x)
        integer, intent(in):: x
        max_depth = x
        allocate(stack(x))
        stack(:) = 0
        depth = 0
    end subroutine

    subroutine pop(a)
        integer, intent(out):: a
        if (depth <= 0)stop '-- --Stack is empty'
        a = stack(depth)
        depth = depth - 1
        ! print*, '-- -- poped =>', a, b
    end subroutine

    subroutine push(a)
        integer, intent(in):: a
        if (depth >= max_depth)stop '-- --Stack over flow'
        ! print*, '-- -- pushed =>', a, b
        depth = depth + 1
        stack(depth) = a
    end subroutine

    function is_remaining() result(ret)
        logical:: ret
        if (depth <= 0)then
            ret = .false.
        else
            ret = .true.
        end if
    end function
end module

program main
    implicit none
    type to
        integer,allocatable :: arr(:)
    end type
    integer:: N, Q
    integer, allocatable:: px(:, :), counter(:), ab(:, :), cnum(:)
    type(to),allocatable :: tree(:)
    integer:: i,a,b

    read*, N,Q
    allocate(px(2, Q), counter(N), ab(2, N-1), cnum(N), source=0)
    allocate(tree(N))

    do i=1, N-1
        read*, a,b
        cnum(a) = cnum(a) + 1
        cnum(b) = cnum(b) + 1
        ab(1,i) = a
        ab(2,i) = b
    enddo

    read*, px(1:2, 1:Q)

    do i=1,N
        allocate(tree(i)%arr(cnum(i)),source=0)
    enddo

    cnum(1:N) = 1

    do i=1,N-1
        a = ab(1, i)
        b = ab(2, i)
        tree(a)%arr(cnum(a)) = b
        tree(b)%arr(cnum(b)) = a
        cnum(a) = cnum(a) + 1
        cnum(b) = cnum(b) + 1
    enddo

    do i=1,Q
        counter(px(1, i)) = counter(px(1, i)) + px(2, i)
    enddo

    call dfs()

    do i=1, N
        write(*, '(i0)') counter(i)
    enddo

contains
    subroutine dfs()
        use pair_stack
        integer:: v, u, p, tmp, child
        logical:: visited(N)
        call init(10 + int(log10(real(N))))
        call push(1)
        child = 0
        p = 0
        visited(1:N) = .false.
        do while(is_remaining())
            call pop(v)
            visited(v) = .true.
            do i=1, size(tree(v)%arr)
                u = tree(v)%arr(i)
                if(visited(u)) cycle
                call push(u)
                counter(u) = counter(u) + counter(v)
            enddo
        enddo

    end subroutine
end program main


character(10) S
read*,i
read*,S
if(i>=3200)then
  print"(A)",S
else
  print"(A)","red"
endif
end


program main
  implicit none

  integer a
  character*128 s

  read(*,*)a
  read(*,*)s

  if (a >= 3200) then
    write(*,*)s
  else
    write(*,*)"red"
  end if

end program main


module ABC138

  ! <module>s to import
  use, intrinsic :: iso_fortran_env

  ! require all variables to be explicitly declared
  implicit none

  ! accessibility of <subroutine>s and <function>s in this <module>
  public :: task_A

  ! constants for this <module>
  integer, parameter, private :: len_string = 10

  ! contained <subroutine>s and <function>s are below
  contains

  subroutine task_A

    ! variables for this <subroutine>
    character(len=len_string, kind=1) :: given_string
    integer(INT16)                    :: given_integer

    ! STEP.01
    ! read out the given data
    read(unit=INPUT_UNIT, fmt=*) given_integer
    read(unit=INPUT_UNIT, fmt=*) given_string

    ! STEP.02
    ! output the answer of this task
    if ( given_integer .ge. 3200_INT16 ) then
      write(unit=OUTPUT_UNIT, fmt='(A)', advance='yes') trim(given_string)
    else
      write(unit=OUTPUT_UNIT, fmt='(A)', advance='yes') 'red'
    end if

    ! STEP.END
    return

  end subroutine task_A

end module ABC138

program main

  ! <module>s to import
  use, non_intrinsic :: ABC138

  ! require all variables to be explicitly declared
  implicit none

  call task_A

end program main


module graph_mod
    implicit none
    type node
        integer(4),allocatable:: to(:)
        integer(4):: l
    end type

contains
    subroutine init(n)
        type(node):: n
        allocate(n%to(1))

        n%l = 0
    end subroutine

    subroutine regist(n,m)
        type(node):: n
        integer(4):: m

        if (n%l+1 > size(n%to)) call add_(n)
        n%l=n%l+1
        n%to(n%l) = m
    end subroutine

    subroutine add_(n)
        type(node):: n
        integer(4),allocatable:: tmp(:)
        integer(4):: l

        l = size(n%to)
        allocate(tmp(l))
        tmp(:) = n%to(:)
        deallocate(n%to)
        allocate(n%to(l*2))
        n%to(1:l) = tmp(:)
        deallocate(tmp)
    end subroutine
end module

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

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

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

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

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

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

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

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

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

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

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

program Ki
    use graph_mod
    use deque_mod
    implicit none
    integer(4):: n,q,a,b,p,x
    integer(4):: i,t,dst
    integer(4),allocatable:: cnt(:)
    logical,allocatable:: see(:)
    type(node),allocatable:: tree(:)
    type(deque):: order, todo

    ! input -------------------------------
    read*, n,q
    allocate(tree(n), see(n), cnt(n))

    do i=1,n
        call init(tree(i))
    end do

    do i=1,n-1
        read*, a,b
        call regist(tree(a),b)
        call regist(tree(b),a)
    end do
    cnt(:) = 0

    do i=1,q
        read*, p,x
        cnt(p) = cnt(p) + x
    end do

    ! shori -------------------------------

    call init_deque(order)
    call init_deque(todo)

    call append(todo,1)
    see(:) = .false.

    do while(remain(todo))
        t = popleft(todo)
        see(t) = .true.
        call append(order,t)

        do i=1,tree(t)%l
            if(see(tree(t)%to(i))) cycle
            call append(todo,tree(t)%to(i))
        end do
    end do

    see(:) = .false.
    do while(remain(order))
        t = popleft(order)
        see(t) = .true.
        do i=1,tree(t)%l
            dst = tree(t)%to(i)
            if (see(dst)) cycle
            cnt(dst) = cnt(dst) + cnt(t)
        end do
    end do

    print*, cnt(:)

end program Ki

Score : 400 points

Problem Statement

Given is a rooted tree with N vertices numbered 1 to N.
The root is Vertex 1, and the i-th edge (1 \leq i \leq N - 1) connects Vertex a_i and b_i.

Each of the vertices has a counter installed. Initially, the counters on all the vertices have the value 0.

Now, the following Q operations will be performed:

Operation j (1 \leq j \leq Q): Increment by x_j the counter on every vertex contained in the subtree rooted at Vertex p_j.

Find the value of the counter on each vertex after all operations.

Constraints

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

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

1 \leq a_i < b_i \leq N

1 \leq p_j \leq N

1 \leq x_j \leq 10^4

The given graph is a tree.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
a_1 b_1
:
a_{N-1} b_{N-1}
p_1 x_1
:
p_Q x_Q

Output

Print the values of the counters on Vertex 1, 2, \ldots, N after all operations, in this order, with spaces in between.

Sample Input 1

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

Sample Output 1

100 110 111 110

The tree in this input is as follows:

Each operation changes the values of the counters on the vertices as follows:

Operation 1: Increment by 10 the counter on every vertex contained in the subtree rooted at Vertex 2, that is, Vertex 2, 3, 4. The values of the counters on Vertex 1, 2, 3, 4 are now 0, 10, 10, 10, respectively.

Operation 2: Increment by 100 the counter on every vertex contained in the subtree rooted at Vertex 1, that is, Vertex 1, 2, 3, 4. The values of the counters on Vertex 1, 2, 3, 4 are now 100, 110, 110, 110, respectively.

Operation 3: Increment by 1 the counter on every vertex contained in the subtree rooted at Vertex 3, that is, Vertex 3. The values of the counters on Vertex 1, 2, 3, 4 are now 100, 110, 111, 110, respectively.

Sample Input 2

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

Sample Output 2

20 20 20 20 20 20


module graph_mod
    implicit none
    type node
        integer(4),allocatable:: to(:)
        integer(4):: l
    end type

contains
    subroutine init(n)
        type(node):: n
        allocate(n%to(1))

        n%l = 0
    end subroutine

    subroutine regist(n,m)
        type(node):: n
        integer(4):: m

        if (n%l+1 > size(n%to)) call add_(n)
        n%l=n%l+1
        n%to(n%l) = m
    end subroutine

    subroutine add_(n)
        type(node):: n
        integer(4),allocatable:: tmp(:)
        integer(4):: l

        l = size(n%to)
        allocate(tmp(l))
        tmp(:) = n%to(:)
        deallocate(n%to)
        allocate(n%to(l+1))
        n%to(1:l) = tmp(:)
        deallocate(tmp)
    end subroutine
end module

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

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

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

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

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

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

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

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

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

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

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

program Ki
    use graph_mod
    use deque_mod
    implicit none
    integer(4):: n,q,a,b,p,x
    integer(4):: i,t,dst
    integer(4),allocatable:: cnt(:)
    logical,allocatable:: see(:)
    type(node),allocatable:: tree(:)
    type(deque):: order, todo

    ! input -------------------------------
    read*, n,q
    allocate(tree(n), see(n), cnt(n))

    do i=1,n
        call init(tree(i))
    end do

    do i=1,n-1
        read*, a,b
        call regist(tree(a),b)
        call regist(tree(b),a)
    end do
    cnt(:) = 0

    do i=1,q
        read*, p,x
        cnt(p) = cnt(p) + x
    end do

    ! shori -------------------------------

    call init_deque(order)
    call init_deque(todo)

    call append(todo,1)
    see(:) = .false.

    ! do while(remain(todo))
    !     t = popleft(todo)
    !     see(t) = .true.
    !     call append(order,t)

    !     do i=1,tree(t)%l
    !         if(see(tree(t)%to(i))) cycle
    !         call append(todo,tree(t)%to(i))
    !     end do
    ! end do

    ! see(:) = .false.
    ! do while(remain(order))
    !     t = popleft(order)
    !     see(t) = .true.
    !     do i=1,tree(t)%l
    !         dst = tree(t)%to(i)
    !         if (see(dst)) cycle
    !         cnt(dst) = cnt(dst) + cnt(t)
    !     end do
    ! end do

    ! print*, cnt(:)

end program Ki


module graph_mod
    implicit none
    type node
        integer(4),allocatable:: to(:)
        integer(4):: l
    end type

contains
    subroutine init(n)
        type(node):: n
        allocate(n%to(1))

        n%l = 0
    end subroutine

    subroutine regist(n,m)
        type(node):: n
        integer(4):: m

        if (n%l+1 > size(n%to)) call add_(n)
        n%l=n%l+1
        n%to(n%l) = m
    end subroutine

    subroutine add_(n)
        type(node):: n
        integer(4),allocatable:: tmp(:)
        integer(4):: l

        l = size(n%to)
        allocate(tmp(l))
        tmp(:) = n%to(:)
        deallocate(n%to)
        allocate(n%to(l+1))
        n%to(1:l) = tmp(:)
        deallocate(tmp)
    end subroutine
end module

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

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

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

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

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

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

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

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

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

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

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

program Ki
    use graph_mod
    use deque_mod
    implicit none
    integer(4):: n,q,a,b,p,x
    integer(4):: i,t,dst
    integer(4),allocatable:: cnt(:)
    logical,allocatable:: see(:)
    type(node),allocatable:: tree(:)
    type(deque):: order, todo

    ! input -------------------------------
    read*, n,q
    allocate(tree(n), see(n), cnt(n))

    do i=1,n
        call init(tree(i))
    end do

    do i=1,n-1
        read*, a,b
        call regist(tree(a),b)
        call regist(tree(b),a)
    end do
    cnt(:) = 0

    do i=1,q
        read*, p,x
        cnt(p) = cnt(p) + x
    end do

    ! shori -------------------------------

    call init_deque(order)
    call init_deque(todo)

    call append(todo,1)
    see(:) = .false.

    do while(remain(todo))
        t = popleft(todo)
        see(t) = .true.
        call append(order,t)

        do i=1,tree(t)%l
            if(see(tree(t)%to(i))) cycle
            call append(todo,tree(t)%to(i))
        end do
    end do

    see(:) = .false.
    do while(remain(order))
        t = popleft(order)
        see(t) = .true.
        do i=1,tree(t)%l
            dst = tree(t)%to(i)
            if (see(dst)) cycle
            cnt(dst) = cnt(dst) + cnt(t)
        end do
    end do

    print*, cnt(:)

end program Ki


module mod_tree
  implicit none
  type arraylist
    integer :: num = 0
    integer, pointer :: arr(:) => null()
  end type
  type tree
    type(arraylist), pointer :: egs(:) => null()
    integer, pointer :: dist(:) => null()
  contains
    procedure :: add => add
    procedure :: dfs => dfs
  end type
  interface tree
    module procedure :: newt
  end interface tree
contains
  subroutine appende(a)
    integer, pointer, intent(inout) :: a(:)
    integer :: n
    integer, allocatable :: tmp(:)
    n = size(a)
    allocate(tmp(n))
    tmp = a
    deallocate(a)
    allocate(a(2*n))
    a(1:n) = tmp
    deallocate(tmp)
  end
  subroutine adde(list,e)
    class(arraylist), intent(inout) :: list
    integer, 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 gete(list,i) result(ret)
    class(arraylist), intent(inout) :: list
    integer, intent(in) :: i
    integer :: ret
    ret = list%arr(i)
  end
  function newt(n) result(ret)
    integer, intent(in) :: n
    type(tree) :: ret
    allocate(ret%egs(n))
    allocate(ret%dist(n))
    ret%dist = 0
  end
  subroutine add(this,fm,to)
    class(tree), intent(inout) :: this
    integer, intent(in) :: fm, to
    call adde(this%egs(fm),to)
  end
  recursive subroutine dfs(this,v,p,d)
    class(tree), intent(inout) :: this
    integer, intent(in) :: v, p, d
    integer :: n, i, u
    this%dist(v) = d
    n = this%egs(v)%num
    do i = 1, n
      u = gete(this%egs(v),i)
      if (u == p) cycle
      call dfs(this,u,v,d+1)
    end do
  end
end module mod_tree
program abc138d
    use mod_tree
    implicit none
    integer::N,Q,P,X
    integer::i
    integer,allocatable,dimension(:)::U,T,A,B
    integer(8),allocatable,dimension(:)::D
    logical,allocatable,dimension(:)::Visited
    type(tree)::G
    read*,N,Q
    allocate(U(0:N),T(0:N),D(N),A(N-1),B(N-1))
    G=tree(N)
    do i=1,N-1
        read*,A(i),B(i)
        call add(G,A(i),B(i))
        call add(G,B(i),A(i))
    end do
    call dfs(G,1,-1,0)
    U=0;T=0
    do i=1,N-1
        if(G%dist(A(i))<G%dist(B(i)))then
            U(B(i))=A(i)
        else
            U(A(i))=B(i)
        endif
    end do

    do i=1,Q
        read*,P,X
        T(P)=T(P)+X
    end do

    allocate(Visited(0:N))
    Visited=.false.
    do i=1,N
        if(.not.visited(i))then
            call solve(i)
        endif
        print"(i0)",T(i)
    end do
contains
recursive subroutine solve(Num)
    integer::Num
    if(U(Num)/=0.and. .not.(Visited(U(Num))))then
        call solve(U(Num))
    endif
    T(Num)=T(Num)+T(U(Num))
    Visited(Num)=.true.
end subroutine
end program


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 abc138d
    use mod_graph
    implicit none
    integer::N,Q,P,X
    integer::i
    integer,allocatable,dimension(:)::U,T,A,B
    integer(8),allocatable,dimension(:)::D
    logical,allocatable,dimension(:)::Visited
    type(graph)::G
    read*,N,Q
    allocate(U(0:N),T(0:N),D(N),A(N-1),B(N-1))
    G=graph(N)
    do i=1,N-1
        read*,A(i),B(i)
        call add(G,A(i),B(i),1_8)
        call add(G,B(i),A(i),1_8)
    end do
    D=dijkstra(g,1)
    U=0;T=0
    do i=1,N-1
        if(D(A(i))<D(B(i)))then
            U(B(i))=A(i)
        else
            U(A(i))=B(i)
        endif
    end do

    do i=1,Q
        read*,P,X
        T(P)=T(P)+X
    end do

    allocate(Visited(0:N))
    Visited=.false.
    do i=1,N
        if(.not.visited(i))then
            call solve(i)
        endif
        print"(i0)",T(i)
    end do
contains
recursive subroutine solve(Num)
    integer::Num
    if(U(Num)/=0.and. .not.(Visited(U(Num))))then
        call solve(U(Num))
    endif
    T(Num)=T(Num)+T(U(Num))
    Visited(Num)=.true.
end subroutine
end program


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 abc138d
    use mod_graph
    implicit none
    integer::N,Q,P,X
    integer::i
    integer,allocatable,dimension(:)::U,T,A,B
    integer(8),allocatable,dimension(:)::D
    logical,allocatable,dimension(:)::Visited
    type(graph)::G
    read*,N,Q
    allocate(U(0:N),T(0:N),D(N),A(N-1),B(N-1))
    G=graph(N)
    do i=1,N-1
        read*,A(i),B(i)
        call add(G,A(i),B(i),1_8)
        call add(G,B(i),A(i),1_8)
    end do
    D=dijkstra(g,1)
    U=0;T=0
    do i=1,N-1
        if(D(A(i))<D(B(i)))then
            U(B(i))=A(i)
        else
            U(A(i))=B(i)
        endif
    end do

    do i=1,Q
        read*,P,X
        T(P)=T(P)+X
    end do

    allocate(Visited(0:N))
    Visited=.false.
    do i=1,N
        if(.not.visited(i))then
            call solve(i)
        endif
        print"(i0)",T(i)
    end do
contains
recursive subroutine solve(Num)
    integer::Num
    if(U(Num)/=0.and. .not.(Visited(U(Num))))then
        call solve(Num)
    endif
    T(Num)=T(Num)+T(U(Num))
    Visited(Num)=.true.
end subroutine
end program


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 abc138d
    use mod_graph
    implicit none
    integer::N,Q,P,X
    integer::i
    integer,allocatable,dimension(:)::U,T,A,B
    integer(8),allocatable,dimension(:)::D
    type(graph)::G
    read*,N,Q
    allocate(U(0:N),T(0:N),D(N),A(N-1),B(N-1))
    G=graph(N)
    do i=1,N-1
        read*,A(i),B(i)
        call add(G,A(i),B(i),1_8)
        call add(G,B(i),A(i),1_8)
    end do
    D=dijkstra(g,1)
    U=0;T=0
    do i=1,N-1
        if(D(A(i))<D(B(i)))then
            U(B)=A
        else
            U(A)=B
        endif
    end do
    do i=1,Q
        read*,P,X
        T(P)=T(P)+X
    end do
    do i=1,N
        T(i)=T(i)+T(U(i))
        print"(i0)",T(i)
    end do
end program


program main
    implicit none
    integer n,q,a,b,c,p,x,i,st_size,s,t
    integer,allocatable::d(:),v(:,:),ans(:),st(:)
    type parr
        integer,allocatable::array(:)
    end type
    type(parr),allocatable::g(:)
    logical,allocatable::f(:)
    read *,n,q
    allocate(d(n))
    allocate(v(n-1,2))
    d=0
    do i=1,n-1
        read *,a,b
        d(a)=d(a)+1
        d(b)=d(b)+1
        v(i,1)=a
        v(i,2)=b
    end do
    allocate(g(n))
    do i=1,n
        allocate(g(i)%array(d(i)))
    end do
    d=0
    do i=1,n-1
        a=v(i,1)
        b=v(i,2)
        d(a)=d(a)+1
        d(b)=d(b)+1
        g(a)%array(d(a))=b
        g(b)%array(d(b))=a
    end do
    deallocate(v)
    allocate(ans(n))
    ans=0
    do i=1,q
        read *,p,x
        ans(p)=ans(p)+x
    end do
    allocate(f(n))
    f=.true.
    allocate(st(n))
    st(1)=1
    st_size=1
    do while(st_size>0)
        s=st(st_size)
        st_size=st_size-1
        f(s)=.false.
        do i=1,d(s)
            t=g(s)%array(i)
            if(f(t))then
                ans(t)=ans(t)+ans(s)
                st_size=st_size+1
                st(st_size)=t
            end if
        end do
    end do
    print '(i0)',ans
end program main


program main
	implicit none
    integer n,q,a,b,p,x,i,st_size,u,v
    integer,allocatable::d(:),g(:,:),ans(:),st(:)
    logical,allocatable::f(:)
	read *,n,q
    allocate(d(n))
    d=0
    allocate(g(n,n))
    allocate(ans(n))
    allocate(st(n))
    allocate(f(n))
    do i=1,n-1
    	read *,a,b
        d(a)=d(a)+1
        d(b)=d(b)+1
        g(a,d(a))=b
        g(b,d(b))=a
    end do
    ans=0
    do i=1,q
    	read *,p,x
        ans(p)=ans(p)+x
    end do
    f=.true.
    st(1)=1
    st_size=1
    do
    	if(st_size==0) exit
        v=st(st_size)
        st_size=st_size-1
        f(v)=.false.
        do i=1,d(v)
        	u=g(v,i)
            if(f(u)) then
            	ans(u)=ans(u)+ans(v)
                st_size=st_size+1
                st(st_size)=u
            end if
        end do
    end do
    print '(i0)', ans
end program main


program kadai
implicit none

integer  ::  n
integer  ::  q
integer,allocatable  ::  a(:)
integer,allocatable  ::  b(:)
integer,allocatable  ::  p(:)
integer,allocatable  ::  x(:)
integer,allocatable  ::  counter(:)
integer  ::  i

read(*,*) n,q
allocate(a(n-1))
allocate(b(n-1))
allocate(p(q))
allocate(x(q))
allocate(counter(n))

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

do i=1,q
    read(*,*) p(i),x(i)
end do

counter=0

do i=1,q
    counter(p(i))=counter(p(i))+x(i)
end do

call plus(a,b,counter,1,n)

write(*,*) counter

stop
contains

recursive subroutine plus(a,b,counter,num,n)
    implicit none
    integer, intent(inout)  ::  a(:),b(:),counter(:)
    integer, intent(in)  ::  num,n
    integer  ::  next
    integer  ::  i
    next=0
    i=1
    do i=1,n-1
        if (a(i)==num) then
            counter(b(i)) = counter(b(i)) + counter(num)
            next=b(i)
            a(i)=10**6
            b(i)=10**6
            call plus(a,b,counter,next,n)
        else if (b(i)==num) then
            counter(a(i)) = counter(a(i)) + counter(num)
            next=a(i)
            a(i)=10**6
            b(i)=10**6
            call plus(a,b,counter,next,n)
        end if
    end do

    return
    stop
end subroutine plus

end program kadai


program kadai
implicit none

integer(8)  ::  n
integer(8)  ::  q
integer(8),allocatable  ::  a(:)
integer(8),allocatable  ::  b(:)
integer(8),allocatable  ::  p(:)
integer(8),allocatable  ::  x(:)
integer(8),allocatable  ::  counter(:)
integer(8),allocatable  ::  ans(:)
integer(8)  ::  i , j , k

read(*,*) n,q
allocate(a(n-1))
allocate(b(n-1))
allocate(p(q))
allocate(x(q))
allocate(counter(n))
allocate(ans(n))

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

do i=1,q
    read(*,*) p(i),x(i)
end do

counter=0

do i=1,q
    counter(p(i))=counter(p(i))+x(i)
end do

ans=counter

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

write(*,*) ans

stop
contains

end program kadai