cho.sh
Notes
Loading...

Power Plants

Time limit

2s

Memory limit

128 MB

Problem

Eunjin works at a power plant facility. While she briefly fell asleep, several power plants broke down, and her boss is now walking toward her office. She must restore enough plants before the boss arrives.

A broken plant can be restarted by using a plant that is already working. The restart cost depends on which working plant is used and which broken plant is restarted.

Find the minimum total cost needed to make at least P power plants working.

Input

The first line contains the number of power plants, N. N is a positive integer no greater than 16.

The next N lines contain the cost matrix. The j-th value on the i-th line is the cost of restarting plant j by using plant i.

The following line contains a string of length N describing the current state of each plant. Y means the plant is on, and N means it is off.

The last line contains the target count P. Every cost is a nonnegative integer no greater than 36, and P is an integer from 0 to N.

Output

Print the minimum cost required to make at least P plants working. If it is impossible, print -1.