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
#include <iostream>
#include <vector>

using namespace std;

bool is_good = true;

long long count(long long x, long long y, vector<long long> &squares, long long idx) {
	if (x == 0 || y == 0)
		return 0;
    while (idx >= 0 && squares[idx] > y) {
        idx--;
	}

	if (idx == -1) {
		is_good = false;
		return 0;
	}

	long long max_x = x / squares[idx], max_y = y / squares[idx];

	return max_x * max_y
		+ count(x - max_x * squares[idx], y, squares, idx - 1)
		+ count(min(y - max_y * squares[idx], max_x * squares[idx]),
			max(y - max_y * squares[idx], max_x * squares[idx]), squares, idx - 1);
}

int main() {
	long long h, w, n;
	cin >> h >> w >> n;

	vector<long long> input(n);
	for (long long i = 0; i < n; i++) {
		cin >> input[i];
	}

    long long c = count(min(h, w), max(h, w), input, input.size() - 1);
	if (is_good) {
		cout << c;
	} else {
		cout << -1;
	}

	return 0;
}