大体题意:

给你n 个树,你要给这些树染色,  标号是0 表示这棵树还没有染色,标号不是0 表示这棵树已经染色 不需要再染,这片树的美丽程度是  连续相同颜色的数目!

告诉你指定美丽程度K,和  最多的颜色种类m ,求得最小花费,每一棵树染不同颜色的花费也告诉了你!

思路:

显然dp,直接三维即可!不会超时!

令dp[i][j][k]表示当前处理第i 个树,  j 表示哪一个颜色,k 表示当前的美丽程度!

那么直接分四种情况讨论了!

第i 个树  和第i-1个树的组合!

四种:

0   非0

0     0

非0   0

非0   非0

讨论即可!我们需要往后转,因此只有 第2 3 种需要加上花费,另外两种不需要加花费,只取最小值即可!

因为是输出dp[n]..因此我们要往后传,往前转就错了= = !

详细见代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 1e18;
const int maxn = 100 + 10;
ll dp[maxn][maxn][maxn];
int p[maxn][maxn];
int c[maxn];
int main(){
	int n,m,K;
	scanf("%d %d %d",&n,&m,&K);
	for (int i = 1; i <= n; ++i){
		scanf("%d",&c[i]);
	}
	for (int i = 1; i <= n; ++i){
		for (int j = 1; j <= m; ++j){
			scanf("%d",&p[i][j]);
		}
	}
	for (int i = 0; i < maxn; ++i){
		for (int j = 0; j < maxn; ++j){
			for (int k = 0; k < maxn; ++k){
				dp[i][j][k] = inf;
			}
		}
	}
	if (c[1] == 0){
		for (int i = 1; i <= m; ++i) dp[1][i][1] = p[1][i];
	}
	else {
		dp[1][c[1]][1] = 0;
	}
	for (int i = 1; i < n; ++i){
		if (c[i] == 0){
			if (c[i+1] == 0){
				for (int la = 1; la <= m; ++la){
					for (int k = 1; k <= K; ++k){
						for (int ne = 1; ne <= m; ++ne){
							if (ne == la) dp[i+1][ne][k] = min(dp[i+1][ne][k],dp[i][la][k] + p[i+1][ne]);
							else dp[i+1][ne][k] = min(dp[i+1][ne][k],dp[i][la][k-1] + p[i+1][ne]);
						}
						
					}
				}
			}else {
				for (int la = 1; la <= m; ++la){
					for (int k = 1; k <= K; ++k){
						for (int ne = c[i+1]; ne <= c[i+1]; ++ne){
							if (ne == la) dp[i+1][ne][k] = min(dp[i+1][ne][k],dp[i][la][k]);
							else dp[i+1][ne][k] = min(dp[i+1][ne][k],dp[i][la][k-1]);
						}
						
					}
				}
				
				
			}
		}
		else {
			if (c[i+1] == 0){
				for (int la = c[i]; la <= c[i]; ++la){
					for (int k = 1; k <= K; ++k){
						for (int ne = 1; ne <= m; ++ne){
							if (ne == la) dp[i+1][ne][k] = min(dp[i+1][ne][k],dp[i][la][k] + p[i+1][ne]);
							else dp[i+1][ne][k] = min(dp[i+1][ne][k],dp[i][la][k-1] + p[i+1][ne]);
						}
						
					}
				}
				
				
			}else {
				for (int la = c[i]; la <= c[i]; ++la){
					for (int k = 1; k <= K; ++k){
						for (int ne = c[i+1]; ne <= c[i+1]; ++ne){
							if (ne == la) dp[i+1][ne][k] = min(dp[i+1][ne][k],dp[i][la][k]);
							else dp[i+1][ne][k] = min(dp[i+1][ne][k],dp[i][la][k-1]);
						}
						
					}
				}
				
				
			}
			
			
		}
	}
//	cout << dp[2][1][2] << endl;
	ll ans = inf;
	if (c[n] != 0){
		ans = min(ans,dp[n][c[n]][K]);
	}
	else{
		for (int i = 1; i <= m; ++i) ans = min(ans,dp[n][i][K]);
	}
	if (ans == inf)ans = -1;
	printf("%I64d\n",ans);
	
	
	return 0;
}



C. Coloring Trees



time limit per test



memory limit per test



input



output



ZS the Coder and Chris the Baboon has arrived at Udayland! They walked in the park wheren trees grow. They decided to be naughty and color the trees in the park. The trees are numbered with integers from1 to n

Initially, tree i has color ci. ZS the Coder and Chris the Baboon recognizes onlym different colors, so 0 ≤ ci ≤ m, whereci = 0 means that treei is uncolored.

ZS the Coder and Chris the Baboon decides to color only the uncolored trees, i.e. the trees withci = 0. They can color each of them them in any of them colors from 1 tom. Coloring the i-th tree with colorj requires exactly pi, j

The two friends define the beauty of a coloring of the trees as theminimum number of contiguous groups (each group contains some subsegment of trees) you can split all then trees into so that each group contains trees of the same color. For example, if the colors of the trees from left to right are2, 1, 1, 1, 3, 2, 2, 3, 1, 3, the beauty of the coloring is7, since we can partition the trees into 7 contiguous groups of the same color : {2}, {1, 1, 1}, {3}, {2, 2}, {3}, {1}, {3}.

ZS the Coder and Chris the Baboon wants to color all uncolored trees so that the beauty of the coloring isexactly k. They need your help to determine the minimum amount of paint (in litres) needed to finish the job.

Please note that the friends can't color the trees that are already colored.



Input



The first line contains three integers, n,m and k (1 ≤ k ≤ n ≤ 100,1 ≤ m ≤ 100) — the number of trees, number of colors and beauty of the resulting coloring respectively.

The second line contains n integers c1, c2, ..., cn (0 ≤ ci ≤ m), the initial colors of the trees. ci equals to0 if the tree number i is uncolored, otherwise thei-th tree has color ci.

Then n lines follow. Each of them containsm integers. The j-th number on thei-th of them line denotes pi, j (1 ≤ pi, j ≤ 109) — the amount of litres the friends need to colori-th tree with color j. pi, j's are specified even for the initially colored trees, but such trees still can't be colored.



Output



Print a single integer, the minimum amount of paint needed to color the trees. If there are no valid tree colorings of beautyk, print  - 1.



Examples



Input



3 2 2 0 0 0 1 2 3 4 5 6



Output



10



Input



3 2 2 2 1 2 1 3 2 4 3 5



Output



-1



Input



3 2 2 2 0 0 1 3 2 4 3 5



Output



5



Input



3 2 3 2 1 2 1 3 2 4 3 5



Output



0



Note



In the first sample case, coloring the trees with colors 2, 1, 1 minimizes the amount of paint used, which equals to 2 + 3 + 5 = 10. Note that 1, 1, 1 would not be valid because the beauty of such coloring equals to1 ({1, 1, 1}

In the second sample case, all the trees are colored, but the beauty of the coloring is3, so there is no valid coloring, and the answer is - 1.

In the last sample case, all the trees are colored and the beauty of the coloring matchesk, so no paint is used and the answer is 0.