program answer
  implicit none
  integer(8) :: i, j, D, teika, su
  integer, parameter :: n = 26
  integer :: c(n), last(n)
  integer, allocatable :: s(:,:), t(:), v(:)

  read(*,*) D
  read(*,*) c
  allocate(s(D,n))
  do i = 1, D
     read(*,*) s(i,1:n)
  end do
  allocate(t(D))
  do i = 1, D
     read(*,*) t(i)
  end do
  allocate(v(D))

  su=sum(c)
  teika=su-c(t(1))
  v(1)=s(1,t(1))-teika
  last=0
  last(t(1))=1
  do i = 2, D
     teika=0
     do j = 1, 26
        teika=teika+c(j)*(i-last(j))
     end do
     teika=teika-c(t(i))*(i-last(t(i)))
     v(i)=v(i-1)+s(i,t(i))-teika
     last(t(i))=i
  end do

  do i = 1, D
     write(*,*) v(i)
  end do
  
  deallocate(s)
  deallocate(t)
  deallocate(v)

  stop
 end program answer
