Narrow Passageway

시간 제한1초메모리 제한2048 MB

요약
2행 N열 격자에 검사, 마법사, 수비수를 제한 수량만큼 배치하되 검사는 변을 공유하지 않고 마법사는 대각선으로 인접하지 않도록 놓아 총 전투력의 최댓값을 구한다.
난이도

보통10점 중 7점

유형
동적 계획법, 그리디
정답자
아직 제출이 없습니다

문제

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 22 rows (numbered from 11 to 22) and NN columns (numbered from 11 to NN). Denote (r,c)(r, c) as the cell in row rr and column cc. 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 C_sC\_s swordsmen, C_wC\_w wizards, and C_dC\_d defenders. Each swordsman, wizard, and defender has a power of P_sP\_s, P_wP\_w, and P_dP\_d, 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 (r,c)(r, c) contains a swordsman, then (r−1,c)(r - 1, c), (r,c+1)(r, c + 1), (r+1,c)(r + 1, c), and (r,c−1)(r, c - 1) should not contain a swordsman; and
  • if (r,c)(r, c) contains a wizard, then (r−1,c−1)(r - 1, c - 1), (r−1,c+1)(r - 1, c + 1), (r+1,c+1)(r + 1, c + 1), and (r+1,c−1)(r + 1, c - 1) 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 NN (1≤N≤10001 ≤ N ≤ 1000).

The second line consists of three integers C_sC\_s C_wC\_w C_dC\_d (0≤C_s,C_w,C_d≤10000 ≤ C\_s, C\_w, C\_d ≤ 1000).

The third line consists of three integers P_sP\_s P_wP\_w P_dP\_d (1≤P_s,P_w,P_d≤100,0001 ≤ P\_s, P\_w, P\_d ≤ 100\\, 000).

Each of the next 22 lines consists of a string with NN characters. They represent the narrow passageway as a grid. The ccth character of the rrth string represents (r,c)(r, c). 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.

예제4

  1. 예제 1

    입력
    7
    4 4 3
    10 30 20
    #.#..#.
    .#...#.
    
    예상 출력
    200
    
  2. 예제 2

    입력
    7
    4 4 3
    40 20 30
    #.#..#.
    .#...#.
    
    예상 출력
    290
    
  3. 예제 3

    입력
    2
    1 1 1
    10 10 10
    ..
    ..
    
    예상 출력
    30
    
  4. 예제 4

    입력
    1
    2 1 2
    20 10 5
    .
    .
    
    예상 출력
    30