Paintings
Time limit2sMemory limit1024 MB
Find the fewest non-overlapping squares, from n sizes where each size divides the next, that exactly cover an h by w wall, or report -1 if it cannot.
Statement
Bajtazar has just moved into a new apartment. After decorating the shelves with trophies from recitation contests and the Bajtocja yodeling championships, he noticed that one wall is completely empty. He wants to fill it with paintings.
The wall is a rectangle of size meters. A nearby art dealer, a close friend of Bajtazar, sells kinds of paintings, and there is an unlimited supply of each kind. All paintings of the same kind have the same size: paintings of the -th kind are squares with side length meters. For any two different values of , one is divisible by the other.
Bajtazar does not care about the price, but he wants no empty space left on the wall. The paintings may not overlap, but they may touch along their edges. He does not want to walk back and forth to the dealer many times, so he wants to buy as few paintings as possible. Compute the minimum number of paintings needed to cover the wall, or determine that covering the wall is impossible.
Input
The first line contains two integers and (), the dimensions of the wall.
The second line contains one integer (), the number of kinds of paintings.
The third line contains distinct integers (, , and is divisible by ), the sizes of the paintings of each kind.
Output
If the wall can be covered, print one integer, the minimum number of paintings needed to cover it. Otherwise, print -1.
Hint
In the first sample, Bajtazar can cover the wall with nine paintings: six of size 1 by 1, two of size 3 by 3, and one of size 6 by 6, arranged as shown in the image below.

In the second sample, it is impossible to cover the wall exactly.