package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"

)

func main() {
	readVariables()
	result := judge.EvalDays(t)
	for _,v := range result {
		fmt.Println(v)
	}
}

type Solution [365]int
type Gain [365][26]int
type Penalty [26]int
type Judge struct {
	gain Gain
	penalty Penalty
	span int
	kind int
	penaltySum int
}

func NewJudge(g Gain, p Penalty, span, kind int) Judge {
	penaltySum := 0
	for _,v := range p {
		penaltySum+=v
	}
	return Judge{
		gain: g,
		penalty: p,
		span: span,
		kind: kind,
		penaltySum: penaltySum,
	}
}

// EvalDays returns score for given solution
func (jd Judge) EvalDays(s Solution) []int {
	var last []int = make([]int, K)
	for i:=0;i<K;i++ {
		last[i] = -1 // day should be 0-indexed
	}
	var score int
	var result []int
	var penalty int
	for i:=0;i<D;i++ {
		// Add satisfaction
		score += jd.gain[i][s[i]]
		// Update penalty
		penalty += jd.penaltySum
		penalty -= jd.penalty[s[i]] * (i - last[s[i]])
		// Update last 
		last[s[i]] = i
		// Calculate penalty for not holding certain type of contests
		// can be O(1)
		score -= penalty
		result = append(result, score)
	}
	return result
}

var (
	D int
	K int = 26
	c Penalty
	s Gain
	t Solution
	judge Judge
)

func readVariables() {
	D = nextInt()
	for i:=0;i<K;i++ {
		c[i] = nextInt()
	}
	for i:=0;i<D;i++ {
		for j:=0;j<K;j++ {
			s[i][j] = nextInt()
		}
	}
	judge = NewJudge(s,c,D,K)
	for i:=0;i<D;i++ {
		t[i] = nextInt() - 1 // convert to 0-indexed 
	}
}



/* Template */

var scanner *bufio.Scanner

func init() {
	Max := 1001001
	scanner = bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]byte, 0, Max), Max)
	scanner.Split(bufio.ScanWords)
}

//nextInt converts next token from stdin and returns integer value.
//nextInt panics when conversion into an integer fails.
func nextInt() int {
	if !scanner.Scan() {
		panic("No more token.")
	}
	num, err := strconv.Atoi(scanner.Text())
	if err != nil {
		panic("nextInt(): cannot convert to int: " + scanner.Text())
	}
	return num
}

func nextStr() string {
	if !scanner.Scan() {
		panic("No more token.")
	}
	return scanner.Text()
}

// MinInt returns minimum argument
func MinInt(x, y int) int {
	if x < y {
		return x
	} else {
		return y
	}
}

//MaxInt returns maximum argument
func MaxInt(x, y int) int {
	if x < y {
		return y
	} else {
		return x
	}
}

//AbsInt returns |x| for x
func AbsInt(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

//ModPow calculates integer power with modulo operation
//if modulo <= 1, it powers w/o module operation
//if base < 0, return value might be negative too.
func ModPow(base, exponent, modulo int) (result int) {
	result = 1
	for exponent > 0 {
		if exponent%2 == 1 {
			result *= base
			if modulo > 1 {
				result %= modulo
			}
		}
		base *= base
		if modulo > 1 {
			base %= modulo
		}
		exponent /= 2
	}
	return
}

//Gcd
func Gcd(vals ...int) (result int) {
	if len(vals) == 0 {
		return
	}
	result = vals[0]
	for i := 1; i < len(vals); i++ {
		result = gcd(result, vals[i])
	}
	return
}

func gcd(x, y int) int {
	x, y = AbsInt(x), AbsInt(y)
	for y > 0 {
		x, y = y, x%y
	}
	return x
}

//Lcm
func Lcm(vals ...int) (result int) {
	if len(vals) == 0 {
		return
	}
	result = vals[0]
	for i := 1; i < len(vals); i++ {
		result = lcm(result, vals[i])
	}
	return
}

func lcm(x, y int) int {
	return x * y / gcd(x, y)
}


Measured memory_bytes: 2704