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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import numpy as np

startH, startW = map(int, input().split())
n = int(input())
d = np.array(input().split(), int)

# startH = 9
# startW = 10
# d = np.array([6, 3, 1])

d[::-1].sort()

result = 0
h = startH
w = startW

if h % min(d) != 0 or w % min(d) != 0:
    print("-1")
    exit(0)

while h > 0:
    d = d[d <= min(h, w)]
    # print(f"  h={h}; w={w}; d={d}")

    firstD = 0
    firstHCount = 0

    for i in d:
        # print(f"h={h}; w={w}; i={i}")
        if i <= w:
            if firstD == 0:
                firstD = i
                firstHCount = int(h / i)
            result += int(w / i) * int(firstD / i) * firstHCount
            # print(f"result={result}")
            w -= int(w / i) * i

    h -= firstHCount * firstD
    w = startW

print(result)