Once You Shoot, You Cannot Stop

Time limit1sMemory limit128 MB

Problem

You play a Chain Shot! style game (also known as SameGame, Jawbreaker, Bubble Shot, and so on) on a rectangular board.

Each cell of the board holds one colored bead. On each turn the player picks one group of same-colored beads connected horizontally and vertically. Every bead in the chosen group is removed from the board. Then any bead left floating falls straight down, and any column with no beads left is removed, so the columns on its sides move together.

Chosen groupBeads falling to the bottomEmpty column removedFinal state

The score for a turn is the square of the number of beads in the chosen group. For example, removing a group of size $7$ in the figure above scores $49$ points.

When every bead on the board is gone the game ends, and the final score is the sum of the scores from all turns.

A level blueprint consists of the board size and the number of beads of each color. Given a blueprint, find the maximum score obtainable over all boards that can be built from it and all ways of playing them.

Input

The first line contains the number of rows $h$, the number of columns $w$, and the number of colors $c$ ($1 \le h, w \le 10$, $1 \le c \le 9$).

The second line contains $c$ positive integers; the $i$-th integer is the number of beads of color $i$. The beads count sums to $h \cdot w$.

Output

Print, as a single integer, the highest total score obtainable across every board that can be built from the blueprint and every way of playing it.

Hint

For a blueprint with bead counts $4, 4, 7$, the maximum score is $81$. Below, each color is removed as a single group for $16 + 16 + 49 = 81$ points.

16 points16 points49 points