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
52
53
54
55
56
57
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll h, w;
ll res;
int n;

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin >> h >> w;
	vector<ll> paints;
	cin >> n;
	if(w < h) swap(w, h);
	for(int i = 1; i <= n; i++){
		int a;
		cin >> a;
		paints.push_back(a);
	}
	reverse(paints.begin(), paints.end());

	while(h){
		int h2;
		bool any = 0;
		for(auto u : paints){
			if(u <= h){
				h2 = (h/u) * u;
				h = (h%u);
				any = 1;
				break;
			}
		}
		if(!any){
			res = -1;
			break;
		}
		
		int actw = w;
		
		for(auto u : paints){
			if(u <= actw && u <= h2){
				ll tmp1 = actw/u;
				ll tmp2 = h2/u; 
				
				res += tmp1*tmp2;
				actw%= u;
			}
		}
		
		if(actw != 0){
			res = -1;
			break;
		}
	}
	
	cout << res << "\n";
}