Under construction

Still being built. Things may change

Once You Shoot, You Cannot Stop

Time limit1sMemory limit128 MB

Summary
Given board dimensions and bead counts per color, arrange beads and clear groups to maximize the sum of squared group sizes.
Level

Hard9 of 10

Topics
Combinatorics, Dynamic programming, Greedy
Solved
No attempts yet

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 77 in the figure above scores 4949 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 hh, the number of columns ww, and the number of colors cc (1≤h,w≤101 \le h, w \le 10, 1≤c≤91 \le c \le 9).

The second line contains cc positive integers; the ii-th integer is the number of beads of color ii. The beads count sums to h⋅wh \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,74, 4, 7, the maximum score is 8181. Below, each color is removed as a single group for 16+16+49=8116 + 16 + 49 = 81 points.

16 points16 points49 points

Examples1

  1. Example 1

    Input
    3 5 3
    4 4 7
    
    Expected output
    81