题意:给你n个数字,一共有m种,如果某数为0则该数为空,空的地方可以填任意种类数,但每填一个数字都要花费一定的费用,
从头到尾,所有相邻且相同的数字看作一个集合,求使n个数字的集合数为k所需的最小费用。
思路:设dp[i,j,k]为前i个连续j段结尾为k的最优值,分类讨论DP即可
var dp:array[0..100,0..100,0..100]of int64;
p:array[1..100,1..100]of int64;
a:array[0..100]of longint;
n,m,k1,i,j,k,x,t,w:longint;
ans:int64;
function min(x,y:int64):int64;
begin
if x<y then exit(x);
exit(y);
end;
begin
//assign(input,'1.in'); reset(input);
// assign(output,'1.out'); rewrite(output);
readln(n,m,k1);
for i:=1 to n do read(a[i]);
for i:=1 to n do
for j:=1 to m do read(p[i,j]);
for i:=1 to n do
if a[i]>0 then
begin
for j:=1 to m do p[i,j]:=1<<60;
p[i,a[i]]:=0;
end;
for i:=0 to n do
for j:=0 to k1 do
for k:=0 to m do dp[i,j,k]:=1<<61;
dp[0,0,0]:=0;
for i:=0 to n-1 do
begin
w:=1; if i=0 then w:=0;
for j:=w to i do
for k:=w to m do
for x:=1 to m do
if x<>k then dp[i+1,j+1,x]:=min(dp[i+1,j+1,x],dp[i,j,k]+p[i+1,x])
else dp[i+1,j,k]:=min(dp[i+1,j,k],dp[i,j,k]+p[i+1,k]);
end;
ans:=1<<60;
for i:=1 to m do ans:=min(ans,dp[n,k1,i]);
if ans<(1<<60) then writeln(ans)
else writeln(-1);
//close(input);
//close(output);
end.