Scanf.scanf "%d" (fun d ->
    let c = Array.init 26 (fun _ -> Scanf.scanf " %d" (fun c -> c)) in
    let s = Array.init d (fun _ ->
        Array.init 26 (fun _ ->
            Scanf.scanf " %d" (fun s -> s)))
    in
    let t = Array.init d (fun _ -> Scanf.scanf " %d" (fun t -> t - 1)) in
    let last = Array.make 26 (-1) in
    let rec loop i sat =
        if i < d then (
            let sat = sat + s.(i).(t.(i)) in
            last.(t.(i)) <- i;
            let rec loop2 j sat =
                if j = 26 then sat else
                    loop2 (j + 1) (sat - c.(j) * (i - last.(j)))
            in
            let sat = loop2 0 sat in
            Printf.printf "%d\n" sat;
            loop (i + 1) sat
        )
    in
    loop 0 0
)
