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

Initially, tree i has color c_{i}. ZS the Coder and Chris the Baboon recognizes only m different colors, so 0 ≤ c_{i} ≤ m, where c_{i} = 0 means that tree i is uncolored.

ZS the Coder and Chris the Baboon decides to color only the uncolored trees, i.e. the trees with c_{i} = 0. They can color each of them them in any of the m colors from 1 to m. Coloring the i-th tree with color j requires exactly p_{i}, j litres of paint.

The two friends define the beauty of a coloring of the trees as the minimum number of contiguous groups (each group contains some subsegment of trees) you can split all the n trees into so that each group contains trees of the same color. For example, if the colors of the trees from left to right are 2, 1, 1, 1, 3, 2, 2, 3, 1, 3, the beauty of the coloring is 7, 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 is exactly 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 c_1, c_2, ..., c_{n} (0 ≤ c_{i} ≤ m), the initial colors of the trees. c_{i} equals to 0 if the tree number i is uncolored, otherwise the i-th tree has color c_{i}.

Then n lines follow. Each of them contains m integers. The j-th number on the i-th of them line denotes p_{i}, j (1 ≤ p_{i}, j ≤ 10^9) — the amount of litres the friends need to color i-th tree with color j. p_{i}, 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 beauty k, 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 to 1 ({1, 1, 1} is a way to group the trees into a single group of the same color).

In the second sample case, all the trees are colored, but the beauty of the coloring is 3, 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 matches k, so no paint is used and the answer is 0.

Source: codeforces | Difficulty: MEDIUM_HARD
Tags: Dynamic programming


Solutions:
INF = 10 ** 18
MX_SZ = 112
dp = [[[INF for k in range(MX_SZ)] for j in range(MX_SZ)] for i in range(MX_SZ)]
best = [[[(INF, INF) for k in range(MX_SZ)] for j in range(MX_SZ)] for i in range(MX_SZ)]

def read():
	return [int(x) for x in input().split()]
(n, m, k_res) = read()
arr = read()
cost = []
for i in range(n):
	cost.append(read())
dp[0][0][MX_SZ - 1] = 0
best[0][0][0] = (0, MX_SZ - 1)
for i in range(1, n + 1):
	clr = arr[i - 1]
	if clr == 0:
		for j in range(1, k_res + 1):
			for k in range(1, m + 1):
				dp[i][j][k] = dp[i - 1][j][k] + cost[i - 1][k - 1]
				if k == best[i - 1][j - 1][0][1]:
					dp[i][j][k] = min(dp[i][j][k], best[i - 1][j - 1][1][0] + cost[i - 1][k - 1])
				else:
					dp[i][j][k] = min(dp[i][j][k], best[i - 1][j - 1][0][0] + cost[i - 1][k - 1])
				if dp[i][j][k] < best[i][j][0][0]:
					best[i][j][1] = best[i][j][0]
					best[i][j][0] = (dp[i][j][k], k)
				elif dp[i][j][k] < best[i][j][1][0]:
					best[i][j][1] = (dp[i][j][k], k)
	else:
		for j in range(1, n + 1):
			dp[i][j][clr] = dp[i - 1][j][clr]
			if clr == best[i - 1][j - 1][0][1]:
				dp[i][j][clr] = min(dp[i][j][clr], best[i - 1][j - 1][1][0])
			else:
				dp[i][j][clr] = min(dp[i][j][clr], best[i - 1][j - 1][0][0])
			best[i][j][0] = (dp[i][j][clr], clr)
ans = INF
for k in range(1, m + 1):
	if dp[n][k_res][k] < ans:
		ans = dp[n][k_res][k]
if ans == INF:
	ans = -1
print(ans)


def main():
	(n, m, k) = map(int, input().split())
	(c, *cc) = map(int, input().split())
	(pp, *ppp) = (list(map(float, input().split())) for _ in range(n))
	inf = 2.0 ** 47
	nxt = [[0.0 if i == c - 1 else inf for i in range(m)] if c else pp]
	for (c, pp) in zip(cc, ppp):
		newrow = [inf] * m
		(cur, nxt) = (nxt, [newrow])
		if c:
			c -= 1
			for row in cur:
				p = row[c]
				if newrow[c] > p:
					newrow[c] = p
				if len(nxt) == k:
					break
				row[c] = inf
				newrow = [inf] * m
				newrow[c] = min(row)
				nxt.append(newrow)
		else:
			for row in cur:
				for (c, p) in enumerate((a + b for (a, b) in zip(row, pp))):
					if newrow[c] > p:
						newrow[c] = p
				if len(nxt) == k:
					break
				bestclr = min(range(m), key=row.__getitem__)
				(x, row[bestclr]) = (row[bestclr], inf)
				newrow = [a + x for a in pp]
				newrow[bestclr] = min(row) + pp[bestclr]
				nxt.append(newrow)
	p = min(nxt[-1])
	print(int(p) if p < inf else -1)
main()


MAX = 10 ** 18 + 5
(n, m, k) = map(int, input().split())
a = list(map(int, input().split()))
cost = [list(map(int, input().split())) for _ in range(n)]
min1 = [[MAX] * (k + 1) for _ in range(n)]
min2 = [[MAX] * (k + 1) for _ in range(n)]
idx = [[-1] * (k + 1) for _ in range(n)]
for i in cost:
	i.insert(0, 0)
dp = [[[MAX] * (m + 1) for _ in range(k + 1)] for _ in range(n)]
if a[0] == 0:
	for i in range(1, m + 1):
		if min1[0][1] >= cost[0][i]:
			if min1[0][1] == cost[0][i]:
				idx[0][1] = -1
			else:
				idx[0][1] = i
			min2[0][1] = min1[0][1]
			min1[0][1] = cost[0][i]
		elif min2[0][1] >= cost[0][i]:
			min2[0][1] = cost[0][i]
		dp[0][1][i] = cost[0][i]
else:
	dp[0][1][a[0]] = 0
	min1[0][1] = 0
	idx[0][1] = a[0]
for i in range(1, n):
	for j in range(1, k + 1):
		if a[i] == 0:
			for l in range(1, m + 1):
				dp[i][j][l] = min(dp[i][j][l], dp[i - 1][j][l] + cost[i][l])
				if l == idx[i - 1][j - 1]:
					dp[i][j][l] = min(min2[i - 1][j - 1] + cost[i][l], dp[i][j][l])
				else:
					dp[i][j][l] = min(min1[i - 1][j - 1] + cost[i][l], dp[i][j][l])
		else:
			for l in range(1, m + 1):
				dp[i][j][a[i]] = min(dp[i][j][a[i]], dp[i - 1][j][a[i]])
				if l != a[i]:
					dp[i][j][a[i]] = min(dp[i - 1][j - 1][l], dp[i][j][a[i]])
		for l in range(1, m + 1):
			if min1[i][j] >= dp[i][j][l]:
				if min1[i][j] == dp[i][j][l]:
					idx[i][j] = -1
				else:
					idx[i][j] = l
				min2[i][j] = min1[i][j]
				min1[i][j] = dp[i][j][l]
			elif min2[i][j] >= dp[i][j][l]:
				min2[i][j] = dp[i][j][l]
ans = MAX
for i in dp[-1][-1]:
	ans = min(ans, i)
print(ans if ans < MAX else -1)


(n, m, k) = map(int, input().split())
colors = list(map(int, input().split()))
costs = [list(map(int, input().split())) for _ in range(n)]
dp = [[[float('inf') for _ in range(k + 1)] for _ in range(m + 1)] for _ in range(n + 1)]
for i in range(m + 1):
	for j in range(k + 1):
		dp[0][i][j] = 0
for i in range(1, n + 1):
	if colors[i - 1] > 0:
		c = colors[i - 1]
		for w in range(1, min(i, k) + 1):
			dp[i][c][w] = min(dp[i][c][w], dp[i - 1][c][w])
			for j in range(1, m + 1):
				if j != c:
					dp[i][c][w] = min(dp[i][c][w], dp[i - 1][j][w - 1])
	else:
		for w in range(1, min(i, k) + 1):
			(min1, min2) = (float('inf'), float('inf'))
			for j in range(1, m + 1):
				if dp[i - 1][j][w - 1] < min1:
					(min2, min1) = (min1, dp[i - 1][j][w - 1])
				else:
					min2 = min(min2, dp[i - 1][j][w - 1])
			for j in range(1, m + 1):
				dp[i][j][w] = min(dp[i][j][w], dp[i - 1][j][w] + costs[i - 1][j - 1])
				if min1 != dp[i - 1][j][w - 1]:
					dp[i][j][w] = min(dp[i][j][w], min1 + costs[i - 1][j - 1])
				else:
					dp[i][j][w] = min(dp[i][j][w], min2 + costs[i - 1][j - 1])
res = float('inf')
for i in range(1, m + 1):
	if dp[n][i][k] < res:
		res = dp[n][i][k]
print(-1 if res == float('inf') else res)


(n, m, k) = list(map(int, input().split()))
arr = list(map(int, input().split()))
paint = [list(map(int, input().split())) for i in range(n)]
if 0 not in arr and len(set(arr)) > k or len(set(arr)) - 1 > k:
	print(-1)
	return
dp = [0 for n_idx in range(n)]
for n_idx in range(n):
	dp[n_idx] = [0 for m_idx in range(m)]
	for m_idx in range(m):
		dp[n_idx][m_idx] = [float('inf') for k_idx in range(k + 1)]
c0 = arr[0]
if c0 != 0:
	dp[0][c0 - 1][1] = 0
else:
	for color in range(m):
		dp[0][color][1] = paint[0][color]
for idx in range(1, n):
	min_last_idx = {}
	for beauty in range(max(1, k - (n - idx) + 1), min(k + 1, idx + 2)):
		min_cost = float('inf')
		min_cost_color = -1
		sec_min_cost = float('inf')
		sec_min_cost_color = -1
		for color in range(m):
			next_cost = dp[idx - 1][color][beauty - 1]
			if next_cost < min_cost:
				sec_min_cost = min_cost
				sec_min_cost_color = min_cost_color
				min_cost = next_cost
				min_cost_color = color
			elif next_cost < sec_min_cost:
				sec_min_cost = next_cost
				sec_min_cost_color = color
		min_last_idx[beauty] = [[min_cost, min_cost_color], [sec_min_cost, sec_min_cost_color]]
	curr_color = arr[idx]
	if curr_color != 0:
		for beauty in range(max(1, k - (n - idx) + 1), min(k + 1, idx + 2)):
			cost_prev_same_color = dp[idx - 1][curr_color - 1][beauty]
			(min_cost, min_cost_color) = min_last_idx[beauty][0]
			if min_cost_color == curr_color - 1:
				min_cost = min_last_idx[beauty][1][0]
			dp[idx][curr_color - 1][beauty] = min(cost_prev_same_color, min_cost)
	else:
		for curr_color in range(m):
			cost_to_color = paint[idx][curr_color]
			for beauty in range(max(1, k - (n - idx) + 1), min(k + 1, idx + 2)):
				cost_prev_same_color = dp[idx - 1][curr_color][beauty]
				(min_cost, min_cost_color) = min_last_idx[beauty][0]
				if min_cost_color == curr_color:
					min_cost = min_last_idx[beauty][1][0]
				dp[idx][curr_color][beauty] = cost_to_color + min(cost_prev_same_color, min_cost)
min_paint = min([l[k] for l in dp[n - 1]])
if min_paint != float('inf'):
	print(min_paint)
else:
	print(-1)


import math
dp = [[[math.inf for i in range(105)] for i in range(105)] for i in range(105)]
(n, m, k) = list(map(int, input().split()))
k += 1
z = list(map(int, input().split()))
matrix = []
for i in range(n):
	ans = list(map(int, input().split()))
	matrix.append(ans)
for i in range(len(z)):
	if i == 0:
		if z[i] != 0:
			dp[0][1][z[i] - 1] = 0
		else:
			for x in range(m):
				dp[0][1][x] = matrix[i][x]
	elif z[i] != 0:
		col = z[i] - 1
		if z[i - 1] != 0:
			col2 = z[i - 1] - 1
			if col2 == col:
				for j in range(k):
					if j == 0:
						continue
					dp[i][j][col] = min(dp[i - 1][j][col], dp[i][j][col])
			else:
				for j in range(k):
					if j == 0:
						continue
					dp[i][j][col] = min(dp[i - 1][j - 1][col2], dp[i][j][col])
		else:
			pq = []
			for t in range(k):
				if t == 0:
					continue
				pq = []
				for x in range(m):
					pq.append([dp[i - 1][t - 1][x], x])
				pq.append([math.inf, math.inf])
				pq.sort()
				col = z[i] - 1
				if col != pq[0][1] and pq[0][0] != math.inf:
					dp[i][t][col] = min(dp[i][t][col], pq[0][0], dp[i - 1][t][col])
				elif pq[0][0] != math.inf and col == pq[0][1]:
					dp[i][t][col] = min(dp[i][t][col], pq[1][0], dp[i - 1][t][col])
				elif pq[0][0] == math.inf:
					dp[i][t][col] = min(dp[i][t][col], dp[i - 1][t][col])
	elif z[i - 1] != 0:
		col = z[i - 1] - 1
		for t in range(k):
			if t == 0:
				continue
			for x in range(m):
				if x != col:
					dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t - 1][col] + matrix[i][x])
				else:
					dp[i][t][x] = min(dp[i][t][x], dp[i - 1][t][col] + matrix[i][x])
	else:
		for t in range(k):
			if t == 0:
				continue
			pq = []
			for x in range(m):
				pq.append([dp[i - 1][t - 1][x], x])
			pq.append([math.inf, math.inf])
			pq.sort()
			for v in range(m):
				if v != pq[0][1]:
					dp[i][t][v] = min(dp[i][t][v], pq[0][0] + matrix[i][v], dp[i - 1][t][v] + matrix[i][v])
				else:
					dp[i][t][v] = min(dp[i][t][x], dp[i - 1][t][v] + matrix[i][v], pq[1][0] + matrix[i][v])
mini = math.inf
for i in range(m):
	mini = min(mini, dp[n - 1][k - 1][i])
if mini == math.inf:
	print(-1)
else:
	print(mini)


(n, m, k) = map(int, input().split())
colour = list(map(int, input().split()))
dollar = []
for i in range(n):
	dollar.append([float('inf')] + list(map(int, input().split())))
dp = [[[99999999999999999999999 for i in range(m + 1)] for j in range(n + 1)] for l in range(n + 1)]
dp[0][1] = dollar[0]
for i in range(n - 1):
	if colour[i] != 0:
		cost = colour[i]
		for j in range(1, i + 2):
			for l in range(1, m + 1):
				if cost == l:
					dp[i + 1][j][l] = min(dp[i + 1][j][l], dp[i][j][cost] + dollar[i + 1][l] - dollar[i][cost])
				else:
					dp[i + 1][j + 1][l] = min(dp[i + 1][j + 1][l], dp[i][j][cost] + dollar[i + 1][l] - dollar[i][cost])
	else:
		for j in range(1, i + 2):
			(mini1, mini2) = (99999999999999999999999, 99999999999999999999999)
			for l in range(1, m + 1):
				if dp[i][j][l] < mini1:
					(mini2, mini1) = (mini1, dp[i][j][l])
				else:
					mini2 = min(mini2, dp[i][j][l])
			for l in range(1, m + 1):
				dp[i + 1][j][l] = min(dp[i + 1][j][l], dp[i][j][l] + dollar[i + 1][l])
				if dp[i][j][l] == mini1:
					dp[i + 1][j + 1][l] = min(dp[i + 1][j + 1][l], dollar[i + 1][l] + mini2)
				else:
					dp[i + 1][j + 1][l] = min(mini1 + dollar[i + 1][l], dp[i + 1][j + 1][l])
if colour[n - 1] > 0:
	if dp[n - 1][k][colour[n - 1]] > 999999999999999999999:
		print(-1)
	else:
		print(dp[n - 1][k][colour[n - 1]] - dollar[n - 1][colour[n - 1]])
elif min(dp[n - 1][k]) > 99999999999999999999:
	print(-1)
else:
	print(min(dp[n - 1][k]))


(n, m, k) = map(int, input().split())
col = list(map(int, input().split()))
p = []
for i in range(n):
	p.append(list(map(int, input().split())))
dp = [[[float('inf')] * m for i in range(k + 1)] for i in range(n)]
if col[0] == 0:
	for i in range(m):
		dp[0][1][i] = p[0][i]
else:
	dp[0][1][col[0] - 1] = 0
for i in range(1, n):
	for j in range(1, k + 1):
		if j == 1:
			if col[i]:
				dp[i][j][col[i] - 1] = dp[i - 1][j][col[i] - 1]
			else:
				for c in range(m):
					dp[i][j][c] = dp[i - 1][j][c] + p[i][c]
		elif col[i]:
			x = dp[i - 1][j][col[i] - 1]
			for t in range(m):
				if t != col[i] - 1:
					x = min(x, dp[i - 1][j - 1][t])
			dp[i][j][col[i] - 1] = x
		else:
			for c in range(m):
				x = dp[i - 1][j][c]
				for t in range(m):
					if t != c:
						x = min(x, dp[i - 1][j - 1][t])
				dp[i][j][c] = x + p[i][c]
ans = min(dp[-1][-1])
if ans != float('inf'):
	print(ans)
else:
	print(-1)