PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#QHD0000037. Tô màu hàng cây (Coloring Trees)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tô màu hàng cây (Coloring Trees)

Coloring Trees

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn trees in a row, numbered 11 through nn. The initial color of tree ii is cic_i: ci=0c_i=0 denotes an uncolored tree; otherwise, 1≤ci≤m1\le c_i\le m denotes its existing color.

You may paint only uncolored trees in any of the mm colors. Painting tree ii with color jj requires pi,jp_{i,j} liters of paint. Already colored trees cannot be repainted and incur no additional cost.

After all trees are colored, partition the row into maximal contiguous blocks of identical colors. The number of blocks is the beauty of the coloring. For example, colors 2,1,1,3,3,22,1,1,3,3,2 have beauty 44.

Find the minimum total amount of paint such that the beauty is exactly kk. Print -1 if no valid coloring exists.

Input

The first line contains n,m,kn,m,k. The next line contains nn integers c1,…,cnc_1,\ldots,c_n. Then follow nn lines, where line ii contains mm integers pi,1,…,pi,mp_{i,1},\ldots,p_{i,m}. Costs are provided even for trees that are already colored.

Output

Print the minimum amount of paint, or -1 if no valid coloring exists.

Subtasks

  • Subtask 1 (20 points): 1≤k≤n≤81\le k\le n\le 8; 1≤m≤41\le m\le 4; 0≤ci≤m0\le c_i\le m; 1≤pi,j≤1091\le p_{i,j}\le 10^9.
  • Subtask 2 (30 points): 1≤k≤n≤301\le k\le n\le 30; 1≤m≤151\le m\le 15; 0≤ci≤m0\le c_i\le m; 1≤pi,j≤1091\le p_{i,j}\le 10^9.
  • Subtask 3 (50 points): 1≤k≤n≤1001\le k\le n\le 100; 1≤m≤1001\le m\le 100; 0≤ci≤m0\le c_i\le m; 1≤pi,j≤1091\le p_{i,j}\le 10^9.

Examples

Input

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

Output

10

Explanation

Choose colors 2,1,12,1,1. The final two trees form one block, so there are exactly two blocks: (2)(2) and (1,1)(1,1). The cost is p1,2+p2,1+p3,1=2+3+5=10p_{1,2}+p_{2,1}+p_{3,1}=2+3+5=10. Coloring every tree with color 11 would cost only 99, but its beauty would be 11, so it is invalid.