#P16692. 染色 Plus
染色 Plus
Problem Description
Xiao G has a grid strip and types of paint. Each type of paint can be used on cells in the interval , and painting one cell costs .
Now, Xiao G wants to paint every cell with one available type of paint. Xiao G wants the colors on this grid strip to be as diverse as possible, so in any consecutive cells, the colors cannot all be the same. Xiao G wants to know the minimum total cost of painting. If there is no coloring method that satisfies the condition, output .
Input Format
The first line contains three integers , as described above.
The next lines each contain three integers , as described above.
Output Format
Output one integer, representing the answer.
10 6 3
1 2 3
1 3 2
4 7 4
1 10 6
1 10 7
6 9 1
28
10 5 3
1 2 3
1 3 2
4 7 4
5 9 6
6 9 1
-1
Hint
Sample Explanation
For the first test case, cells to are painted with paint types respectively. The total cost is . It can be proven that this is the minimum cost that satisfies the condition.
For the second test case, since cell has no available paint, there is no coloring method that satisfies the condition, so output .
Constraints
For of the testdata, , , , and .
Translated by ChatGPT 5