#P1550. [USACO08OCT] Watering Hole G

[USACO08OCT] Watering Hole G

Description

Farmer John’s farm is short of water.

He decides to bring water to his nn fields. He can dig some wells and build channels between fields to connect them for water supply. Digging a well in field ii costs WiW_i units. Connecting field ii and field jj costs Pi,jP_{i,j} (with Pj,i=Pi,jP_{j,i} = P_{i,j}) units.

Find the minimum amount of money FJ needs so that every field is either connected to a field that has water or has its own well.

Input Format

The first line contains an integer nn.

The next nn lines each contain an integer WiW_i.

The next nn lines each contain nn integers; the jj-th number in the ii-th line is the cost Pi,jP_{i,j} to connect field ii and field jj.

Output Format

Output the minimum cost.

4
5
4
4
3
0 2 2 2
2 0 3 3
2 3 0 4
2 3 4 0
9

Hint

Constraints: For 100%100\% of the testdata, 1n3001 \leq n \leq 300, 1Wi1051 \leq W_i \leq 10^5, 0Pi,j1050 \leq P_{i,j} \leq 10^5.

Translated by ChatGPT 5