cho.sh
Notes
Loading...

Police Stations

Time limit

2s

Memory limit

128 MB

Problem

There are N cities. Some ordered pairs of cities are connected by one-way roads. President Ukjong wants to build police stations in some cities. Building a police station in city i costs cost[i].

A police station built in city i can control city j only when there is a path from i to j and also a path from j back to i.

Given the road connectivity and the construction cost for each city, compute the minimum total cost needed to control every city.

Input

The first line contains N (1 <= N <= 100). The second line contains cost[1], ..., cost[N], the police-station construction cost for each city. The next N lines describe the road connectivity. In the i-th of these lines, the j-th character is 0 if there is no direct road from city i to city j, and 1 if there is one.

Each cost is an integer between 1 and 1,000,000, inclusive.

Output

Print the minimum total cost required to control every city.