#P3325. Voltage Stabilization Components Installation

    ID: 16582 Type: Default 1000ms 256MiB

Voltage Stabilization Components Installation

Voltage Stabilization Components Installation

A renowned chip company requires you to install voltage stabilizing components on their latest products. Each chip is designed as an N×NN \times N square board with slots. Some slots are unavailable (represented by '/'), and some are already occupied by components (represented by 'C'). The remaining available slots (represented by '.') can be used to add new components.

However, for electrical stability you must satisfy the following constraints:
1. For each row ii, you are given a non-negative integer TiT_i and TiT_i column indices ri,1,ri,2,,ri,Tir_{i,1}, r_{i,2}, \ldots, r_{i,T_i}. It must hold that
[ R_i \leq C_{r_{i,1}} + C_{r_{i,2}} + \cdots + C_{r_{i,T_i}}, ] where RiR_i is the total number of components in row ii (including pre-occupied slots) and CjC_j is the total number of components in column jj.

2. For each row ii, a floating-point number sis_i (0si10 \leq s_i \leq 1) is given. The number of components in row ii must not exceed sis_i times the total components on the chip, i.e., [ R_i \leq s_i \cdot X, ] where XX is the total number of components on the chip.

3. For each column jj, a floating-point number tjt_j (0tj10 \leq t_j \leq 1) is provided. The number of components in column jj must not exceed tjt_j times the total components, i.e., [ C_j \leq t_j \cdot X. ]

Your task is to insert as many additional components as possible into available slots, while obeying all the above constraints. Note that the already occupied slots are counted in the totals.

inputFormat

The first line contains an integer NN, denoting the size of the chip (the chip is an N×NN \times N board).

The next NN lines each contain a string of length NN, consisting of characters '.', '/', and 'C', representing available slots, unavailable slots, and pre-occupied slots, respectively.

Then follow NN lines, one per row ii (1-indexed). Each such line begins with an integer TiT_i, followed by TiT_i integers (the column indices ri,1,ri,2,,ri,Tir_{i,1}, r_{i,2}, \ldots, r_{i,T_i}) and a floating point number sis_i.

The last line contains NN floating point numbers, representing t1,t2,,tNt_1, t_2, \ldots, t_N.

outputFormat

Output a single integer: the maximum number of additional components that can be inserted while satisfying all given constraints.

sample

3
.C.
/..
...
1 2 0.5
0 1.0
2 1 3 0.7
0.6 0.8 1.0
1

</p>