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
61
62
63
64
65
66
67
68
69
70
#include<iostream>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;

long long MAX = 2'000'000'000'000'000'000;

long long cnt(pair<long long, long long> p, vector<long long> &v, map<pair<long long, long long>, long long> &m) {
    long long h = p.first, w = p.second;
    if (h == 0 || w == 0) {
        return 0;
    }

    if (h > w) swap(h, w);    
    if (m.count(make_pair(h, w)) > 0) {
        return m[make_pair(h, w)];
    }

    long long res = MAX;
    for (long long a : v) {
        if (h >= a && w >= a) {
            long long hh = h / a * a;
            long long ww = w / a * a;
            long long dh = h - hh;
            long long dw = w - ww;
            long long hw = (h / a) * (w / a);

            long long v1 = hw + cnt(make_pair(dh, w), v, m) + cnt(make_pair(hh, dw), v, m);
            long long v2 = hw + cnt(make_pair(dh, ww), v, m) + cnt(make_pair(h, dw), v, m);
            long long v3 = hw + cnt(make_pair(dh, ww), v, m) + cnt(make_pair(hh, dw), v, m) + cnt(make_pair(dh, dw), v, m);

            res = min(res, v1);
            res = min(res, v2);
            res = min(res, v3);
            break;
        }
    }
    
    m[make_pair(h, w)] = res;
    return res;
}

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

    if (h > w) swap(h, w);

    long long n;
    cin >> n;

    vector<long long> v(n);
    for (int i = 0; i < n; i++) {
        cin >> v[i];
    }
    sort(v.rbegin(), v.rend());
    
    map<pair<long long, long long>, long long> m;
    long long res = cnt(make_pair(h, w), v, m);
    
    if (res == MAX) {
        cout << -1 << endl;
    }
    else {
        cout << res << endl;
    }

    return 0;
}