program r20628b
 
implicit none
integer :: c(1:26)
integer :: interval(1:26)
integer, allocatable :: s(:, :)
integer, allocatable :: t(:), v(:)
integer :: i, j, d

read *, d

allocate(s(1:26, 1:d))
allocate(t(1:d))
allocate(v(0:d))

interval(:) = 0 !interval(i) = d - last(d,i)
v(0) = 0

read *, c(1:26)

do i = 1, d
 read *, s(:, i)
end do

do i = 1, d
 read *, t(i)
 
 v(i) = v(i-1) + s(t(i), i)
 
 do j = 1, 26
  if(t(i) == j) then
   interval(j) = 0
  else
   interval(j) = interval(j) + 1
  end if
  v(i) = v(i) - (c(j) * interval(j))
 end do
 
 print *, v(i)
end do

end program r20628b