# codeforces Round_369 C. Coloring Trees

                             C. Coloring Trees
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output


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 ci. ZS the Coder and Chris the Baboon recognizes only m different colors, so 0 ≤ ci ≤ m, where ci = 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 ci = 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 pi, 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 c1, c2, …, cn (0 ≤ ci ≤ m), the initial colors of the trees. ci equals to 0 if the tree number i is uncolored, otherwise the i-th tree has color ci.

Then n lines follow. Each of them contains m integers. The j-th number on the i-th of them line denotes pi, j (1 ≤ pi, j ≤ 109) — the amount of litres the friends need to color i-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 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.

if(!tree[i])
dp(i, j, seg) = Min(dp(i - 1, k, seg - 1), dp(i - 1, j, seg)); (k : 1 -> m && k != j)
else
dp(i, j, seg) = Min(dp(i - 1, k, seg - 1), dp(i - 1, tree[i], seg)); (k : 1 -> m && k != tree[i])


/*
*
*  Author : Triose
*  Email  : Triose@163.com
*  Update_time : 2016.6.12
*
*/

//#include<bits/stdc++.h>
#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<iterator>
#include<math.h>
#include<stdlib.h>
#include<time.h>
#include<map>
#include<set>
using namespace std;
//#define ONLINE_JUDGE
#define eps 1e-8
#define inf 0x3f3f3f3f
#define INF 0x7fffffff
#define INFL 0x3f3f3f3f3f3f3f3fLL
#define enter putchar(10)
#define rep(i,a,b) for(int i = (a); i < (b); ++i)
#define repe(i,a,b) for(int i = (a); i <= (b); ++i)
#define mem(a,b) (memset((a),b,sizeof(a)))
#define sf(a) scanf("%d",&a)
#define sfI(a) scanf("%I64d",&a)
#define sfd(a,b) scanf("%d%d",&a,&b)
#define sft(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define sfs(a) scanf("%s",a)
#define pf(a) printf("%d\n",a)
#define pfd(a,b) printf("%d %d\n",a,b)
#define pfP(a) printf("%d %d\n",a.fi,a.se)
#define pfs(a) printf("%s\n",a)
#define pfI(a) printf("%I64d\n",a)
#define PR(a,b) pair<a,b>
#define fi first
#define se second
#define LL long long
#define DB double
#define ds(t) int t; sf(t)
const double PI = acos(-1.0);
const double E = exp(1.0);
template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T> inline T Min(T a, T b) { return a < b ? a : b; }
template<class T> inline T Max(T a, T b) { return a > b ? a : b; }
int n, m, seg;

#define N 110
LL tree[N];             //tree[i] : 树的颜色 [1, n]
LL pay[N][N];           //pay[i][j] : 第i棵树刷成第j种颜色需要的花费[1, n] , [1, m]
LL dp[N][N][N];         //dp[i][k][j] : 第i棵树加入第k段涂成第j种颜色需要的最小花费

void Init() {
repe(i, 1, n) cin >> tree[i];
repe(i, 1, n) repe(j, 1, m) cin >> pay[i][j];
repe(i, 0, n) repe(k, 0, seg) repe(j, 0, m) dp[i][k][j] = INFL; //结合下面的dp[0][0][0] = 0 来保证 dp 过程中每一步求得的值都是正确的， 同时也保证前i棵树不能分成k段的话（实际中），有dp[i][k][any_color] = INFL
}

LL solve() {
dp[0][0][0] = 0;            //保证dp[i][1][colors]的值正确，即i == 1时， dp[i][1][colors] = dp[0][0][0] ( = 0 ) + pay[i][colors], 而 i != 1时， dp[i][1][colors] == dp[i][0][j] (==INFL) + pay[i][colors].
repe(i, 1, n) {
if(!tree[i]) {          //无颜色
repe(k, 1, seg) {
repe(j, 1, m) {
dp[i][k][j] = dp[i - 1][k][j] + pay[i][j];
repe(l, 0, m) if(l != j) dp[i][k][j] = Min(dp[i][k][j], dp[i - 1][k - 1][l] + pay[i][j]);
}
}
}
else {                  //有颜色，不能涂
repe(k, 1, seg) {
dp[i][k][tree[i]] = dp[i - 1][k][tree[i]];
repe(j, 0, m) if(j != tree[i]) dp[i][k][tree[i]] = Min(dp[i][k][tree[i]], dp[i - 1][k - 1][j]);
}
}
}
LL ans = INFL;
repe(i, 1, m) ans = Min(ans, dp[n][seg][i]);
return ans == INFL ? -1 : ans;
}

int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
//  freopen("out.txt", "w", stdout);
#endif
while(cin >> n >> m >> seg) {
Init();
cout << solve() << endl;
}
return 0;
}



• 本文已收录于以下专栏：

举报原因： 您举报文章：codeforces Round_369 C. Coloring Trees 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)