Narrow Passageway
시간 제한1초메모리 제한2048 MB
2행 N열 격자에 검사, 마법사, 수비수를 제한 수량만큼 배치하되 검사는 변을 공유하지 않고 마법사는 대각선으로 인접하지 않도록 놓아 총 전투력의 최댓값을 구한다.
문제
You are a strategist of The ICPC Kingdom. You received an intel that there will be monster attacks on a narrow passageway near the kingdom. The narrow passageway can be represented as a grid with rows (numbered from to ) and columns (numbered from to ). Denote as the cell in row and column . Each cell can be empty, which is represented by the character .; or blocked, which is represented by the character #.
There are three types of heroes that can be deployed to defend the passageway: swordsman, wizard, and defender. Currently, the kingdom has swordsmen, wizards, and defenders. Each swordsman, wizard, and defender has a power of , , and , respectively.

You can only deploy at most one hero on an empty cell, while no heroes can be deployed on a blocked cell. Furthermore, there should not be two cells sharing a side and both contain a swordsman; and there should not be two cells sharing a corner and both contain a wizard. Formally,
- if contains a swordsman, then , , , and should not contain a swordsman; and
- if contains a wizard, then , , , and should not contain a wizard.
Determine the maximum total power that can be deployed to defend the narrow passageway from the monster attacks.
입력
The first line consists of an integer ().
The second line consists of three integers ().
The third line consists of three integers ().
Each of the next lines consists of a string with characters. They represent the narrow passageway as a grid. The th character of the th string represents . Each character can only be either . or #.
출력
Output a single integer representing the maximum total power that can be deployed to defend the narrow passageway.