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
42
43
44
45
46
47
48
49
50
51
import sys

DEBUG = True
DEBUG = False

wh = [int(i) for i in input().split(' ') if i != '']
w, h = wh[0], wh[1]

n = int(input())
z = input().split(' ')
z = [int(i) for i in z if i != '']
z.sort(reverse=True)

to_check = []

def add_to_check(x, y):
    DEBUG and print('to check: ', x, y)
    to_check.append((x, y))

def get_to_check():
    return to_check.pop(0)

add_to_check(w, h)

sum = 0
while True:
    if len(to_check) == 0:
        break
    found = False
    ww, hh = get_to_check()
    for zi in z:
        DEBUG and print(ww, hh, zi)
        if zi > ww or zi > hh:
            continue
        found = True
        x = ww % zi
        y = hh % zi
        sum += ww // zi * hh // zi
        if x > 0 and y > 0:
            add_to_check(x, y)
        if x > 0:
            add_to_check(x, hh-y)
        if y > 0:
            add_to_check(ww-x, y)
        break
    if not found:
        print('-1')
        sys.exit()

DEBUG and print(z)
print(sum)