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
#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int h, w;
	int n;
	cin >> h >> w;
	cin >> n;
	vector <int> v(n);
	for (int i = 0; i < n; i++) 
		cin >> v[i];
	priority_queue <pair <int, int> > q;
	if (h > w)
		swap(h, w);
	q.push({-h, -w});
	ll res = 0;
	while (!q.empty()) {
		auto [a, b] = q.top();
		q.pop();
		a *= -1; b *= -1;
		if (v[0] > a) {
			cout << "-1\n";
			return 0;
		}
		int bok1 = *(upper_bound(v.begin(), v.end(), a)-1);
		int bok2 = bok1 * (int)floor(b / bok1);
		res += (ll)floor(a / bok1) * (ll)floor(b / bok1);
		bok1 *= (int)floor(a / bok1);
		if (b > bok2) {
			int c = b-bok2;
			int d = a;
			if (c > d)
				swap(c, d);
			q.push({-c, -d});
			
		}
		if (a > bok1) {
			int c = a-bok1;
			int d = bok2;
			if (c > d)
				swap(c, d);
			q.push({-c, -d});
		}
	}
	cout << res << "\n";
	return 0;

}