#BS0000455. Tăng cường phòng thủ biên giới (Risk)
Bài tập thuộc lộ trình
Mục lục đề ⌄
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 territories numbered from to . Initially, denotes the number of your armies in territory . Territories with belong to you; those with 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 .
The second line contains integers .
The following lines each contain exactly characters, either Y or N, without spaces. Character of line is Y when territories and 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): , , ; the adjacency matrix consists of
YandN, is symmetric, and hasNon its main diagonal; both sides own at least one territory, and at least one adjacent pair belongs to different sides. - Subtask 2 (30 points): , , ; the adjacency matrix consists of
YandN, is symmetric, and hasNon its main diagonal; both sides own at least one territory, and at least one adjacent pair belongs to different sides. - Subtask 3 (50 points): , , ; the adjacency matrix consists of
YandN, is symmetric, and hasNon 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.