#QHD0000037. Tô màu hàng cây (Coloring Trees)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Tô màu hàng cây (Coloring Trees)
Coloring Trees
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
There are trees in a row, numbered through . The initial color of tree is : denotes an uncolored tree; otherwise, denotes its existing color.
You may paint only uncolored trees in any of the colors. Painting tree with color requires 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 have beauty .
Find the minimum total amount of paint such that the beauty is exactly . Print -1 if no valid coloring exists.
Input
The first line contains . The next line contains integers . Then follow lines, where line contains integers . 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): ; ; ; .
- Subtask 2 (30 points): ; ; ; .
- Subtask 3 (50 points): ; ; ; .
Examples
Input
3 2 2
0 0 0
1 2
3 4
5 6
Output
10
Explanation
Choose colors . The final two trees form one block, so there are exactly two blocks: and . The cost is . Coloring every tree with color would cost only , but its beauty would be , so it is invalid.