PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000455. Tăng cường phòng thủ biên giới (Risk)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tăng cường phòng thủ biên giới (Risk)

Risk

Source: Kattis

Version: Phuoc Hung OJ Extended

Problem Statement

The map in a game of Risk consists of nn territories numbered from 11 to nn. Initially, aia_i denotes the number of your armies in territory ii. Territories with ai>0a_i>0 belong to you; those with ai=0a_i=0 belong to an opponent.

During a single turn, an army can remain in its starting territory or move once to an adjacent territory that also belongs to you. Adjacent territories share a border. You may order the moves as you wish, provided every territory you own retains at least one army throughout the turn. Enemy territories cannot receive your armies.

A border territory is a territory you own that is adjacent to at least one enemy territory. The strength of the weakest border territory is the minimum number of armies among your border territories after the turn.

Determine the greatest attainable strength of the weakest border territory.

Input

The first line contains an integer nn.

The second line contains nn integers a1,a2,…,ana_1,a_2,\ldots,a_n.

The following nn lines each contain exactly nn characters, either Y or N, without spaces. Character jj of line ii is Y when territories ii and jj share a border, and N otherwise.

Output

Print a single integer: the maximum attainable number of armies in your weakest border territory after one turn.

Subtasks

  • Subtask 1 (20 points): 1≤n≤121\le n\le 12, 0≤ai≤1000\le a_i\le100, 1≤i,j≤n1\le i,j\le n; the adjacency matrix consists of Y and N, is symmetric, and has N on its main diagonal; both sides own at least one territory, and at least one adjacent pair belongs to different sides.
  • Subtask 2 (30 points): 1≤n≤401\le n\le 40, 0≤ai≤1000\le a_i\le100, 1≤i,j≤n1\le i,j\le n; the adjacency matrix consists of Y and N, is symmetric, and has N on its main diagonal; both sides own at least one territory, and at least one adjacent pair belongs to different sides.
  • Subtask 3 (50 points): 1≤n≤1001\le n\le 100, 0≤ai≤1000\le a_i\le100, 1≤i,j≤n1\le i,j\le n; the adjacency matrix consists of Y and N, is symmetric, and has N on its main diagonal; both sides own at least one territory, and at least one adjacent pair belongs to different sides.

Examples

Input 1

3
1 1 0
NYN
YNY
NYN

Output 1

1

Explanation 1

Territory 2 is the only border territory. Territories 1 and 2 share just two armies and each must keep at least one. Therefore territory 2 cannot gain strength; the answer is 1.

Input 2

7
7 3 3 2 0 0 5
NYNNNNN
YNYYNNN
NYNYYNN
NYYNYNN
NNYYNNN
NNNNNNY
NNNNNYN

Output 2

4

Explanation 2

Border territories are 3, 4 and 7. First move three armies from territory 1 to territory 2. Then send one of territory 2’s original armies to territory 3 and two of its original armies to territory 4. The final border strengths are 4, 4 and 5, and no territory is emptied. Achieving at least five in both territories 3 and 4 would require ten armies, but only the eight original armies in territories 2, 3 and 4 can finish in either of them. Hence the optimum is four.