Pia Atelier: Alchemist of the Mystery Competition

Time limit3sMemory limit512 MB

Summary
Choose and order 3 of up to 10 candidate 4x4 materials, placing each rotated onto a 5x5 furnace, to maximize a weighted color-sum quality.
Level

Medium6 of 10

Topics
Brute force, Simulation, Implementation, Backtracking
Solved
No attempts yet

Problem

The game Pia Atelier: Alchemist of the Mystery Competition tells the growth story of Pia, a poor alchemist. The competition is the central part. Pia earns a living only when alchemy produces high quality goods and wins prize money. The story is long and is omitted here. This task asks you to check the competition logic.

You place 33 distinct materials in order into a 5×55 \times 5 grid furnace and build a bomb of the highest possible quality. The competition supplies at most 1010 candidate materials. You choose 33 of them, fix an order, and place them into the furnace.

Each furnace cell holds a quality number and an element color. At the start every cell has quality 00 and white color. Each material has 4×44 \times 4 shape, and cell (i,j)(i, j) of a material holds two values.

  1. Efficacy: an integer xi,jx_{i,j} from −9-9 to 99 that is added to the quality of one furnace cell.
  2. Element: a color ci,jc_{i,j} that can change the element of one furnace cell, one of red R, blue B, green G, yellow Y, and white W.

A material never sticks out of the 5×55 \times 5 grid. Rotation in steps of 9090 degrees is allowed, and tilted placement is not allowed. When a material is placed, the furnace changes as follows.

  1. A cell outside the material does not change.

  2. A cell under the material changes as follows.

    • Quality grows by the efficacy of the material. A negative sum becomes 00, and a sum above 99 becomes 99.
    • Color stays the same when the material element is white, and otherwise becomes the material element color.

The bomb is complete only after all 33 materials are placed. Let RR, BB, GG, and YY be the quality sums over cells colored red, blue, green, and yellow in the finished furnace. The bomb quality is 7R+5B+3G+2Y7R + 5B + 3G + 2Y.

Given the candidate materials, choose and place them to maximize the bomb quality.

Input

The first line holds a natural number nn, the count of candidate materials (3≤n≤103 \le n \le 10).

The next lines hold the nn materials in order. Each material uses 88 lines. The first 44 lines hold efficacy: 44 integers separated by spaces on each line. The next 44 lines hold elements: 44 characters separated by spaces on each line. Each character is one of R, B, G, Y, W.

Output

Print the maximum bomb quality on the first line.

Hint

The placement proceeds as follows.

  1. Initial furnace.
0W0W0W0W0W
0W0W0W0W0W
0W0W0W0W0W
0W0W0W0W0W
0W0W0W0W0W
  1. Place the second material with its top left corner at row 11, column 11, rotated once counterclockwise.
0Y9B0G7R0W
0R0R2G0B0W
0B0W0B3G0W
0W1Y8W0Y0W
0W0W0W0W0W
  1. Place the third material with its top left corner at row 22, column 22, without rotation.
0Y9B0G7R0W
0R8Y9G2B1G
0B0Y8B9R0B
0W0Y4W8R6R
0W0R8Y0W8Y
  1. Place the first material with its top left corner at row 22, column 11, rotated once counterclockwise.
0Y9B0G7R0W
0Y9R8R1B1G
3G0R9R6R0B
3B8B9R9R6R
6R0Y9Y0G8Y

The sums are R=69R = 69, B=21B = 21, G=4G = 4, and Y=17Y = 17, so the final quality is 634634.

Examples1

  1. Example 1

    Input
    4
    6 3 3 -9
    -6 8 -6 8
    9 5 1 -1
    -8 2 -3 -1
    R B G Y
    Y B R R
    W R R R
    G R W B
    -6 -2 -4 -3
    1 -3 0 9
    8 -7 2 0
    0 3 -5 7
    W B R Y
    Y W R B
    W B G G
    Y G B R
    8 7 2 1
    -9 8 8 -9
    -1 -4 8 6
    -7 8 -3 8
    Y W W G
    Y B R B
    Y W R R
    R Y W Y
    4 -5 8 3
    2 3 1 3
    -5 0 1 -3
    4 3 3 -6
    W Y G W
    G G R W
    G Y G R
    R R G Y
    Expected output
    634