program scoring
    implicit none
    integer(8)::D,C(26),last(26),i,j
    integer(8),allocatable::S(:,:),T(:),V(:)
    read*,D,C
    allocate(S(26,D))
    allocate(T(D))
    allocate(V(D))
    read*,S,T
    V=0
    last=0

    do i=1,D
        if(i==1)then
            V(i)=V(i)
        else
            V(i)=V(i-1)
        end if
        V(i)=V(i)+S(T(i),i)
        last(T(i))=i
        do j=1,26
            V(i)=V(i)-C(j)*(i-last(j))
        end do
    end do

    do i=1,D
        print'(i0)',V(i)
    end do
end program scoring