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
58
59
60
// Online C++ compiler to run C++ program online
#include <iostream>
#include <vector>
#include <queue>
#include <unordered_set>
#include <climits>
#include <algorithm>

using namespace std;

long long int result = 0;

void wypelnij(int pt, vector<int>& v, int a, int b) {
    if(a == 0 || b == 0) return;
    
    if(a >= v[pt] && b >= v[pt]) {
        int maxa = a / v[pt];
        int maxb = b / v[pt];
        
        int c = maxa * v[pt];
        int d = maxb * v[pt];
        
        result += (long long int) maxa * maxb;
        
        wypelnij(pt + 1, v, a - c, d);
        wypelnij(pt + 1, v, a, b - d);
        
    } else {
        wypelnij(pt + 1, v, a, b);
    }
}

int main() {
    // Write C++ code here
    int h, w;
    cin >> h >> w;
    
    int n;
    cin >> n;
    
    vector<int> v(n);
    for(int i = 0; i < n; i++) {
        cin >> v[i];
    }
    
    sort(v.begin(), v.end());
    
    if(! (h % v[0]) == 0 || ! (w % v[0]) == 0) {
        cout << -1 << endl;
        return 0;
    }
    
    reverse(v.begin(), v.end());
    
    wypelnij(0, v, h, w);
    cout << result << endl;
    

    return 0;
}