Under construction

Still being built. Things may change

Paintings

Time limit2sMemory limit1024 MB

Summary
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.
Level

Medium7 of 10

Topics
Greedy, Recursion, Math
Solved
No attempts yet

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 h×wh \times w meters. A nearby art dealer, a close friend of Bajtazar, sells nn 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 ii-th kind are squares with side length did_i meters. For any two different values of did_i, 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 hh and ww (1≤h,w≤1091 \le h, w \le 10^9), the dimensions of the wall.

The second line contains one integer nn (1≤n≤301 \le n \le 30), the number of kinds of paintings.

The third line contains nn distinct integers d1,d2,…,dnd_1, d_2, \ldots, d_n (1≤di≤1091 \le d_i \le 10^9, di<di+1d_i < d_{i+1}, and di+1d_{i+1} is divisible by did_i), 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.

Examples2

  1. Example 1

    Input
    6 10
    3
    1 3 6
    
    Expected output
    9
    
  2. Example 2

    Input
    3 3
    1
    2
    
    Expected output
    -1