1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import sys

def solve(h, w, sizes) -> int:
    if h == 0 or w == 0:
        return 0
    useful_sizes = [s for s in sizes if s <= min(w,h)]
    largest = useful_sizes[-1]
    n = h // largest
    m = w // largest

    rest_h = h % largest
    rest_w = w % largest

    return n * m + solve(rest_h, w, useful_sizes) + solve(rest_w, h - rest_h, useful_sizes)


if __name__ == "__main__":
    lines = [line for line in sys.stdin]
    w, h = [int(x) for x in lines[0].rstrip().split(" ")]
    n = int(lines[1].rstrip())
    sizes = [int(x) for x in lines[2].rstrip().split(" ")]
    possible = (w % sizes[0] == 0) and (h % sizes[0] == 0)
    if not possible:
        print(-1)
        sys.exit(0)
    print(solve(h,w,sizes))