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

vector<long long> a;
long long dfs(long long h, long long w) {
    if(h * w == 0) return 0;
    for(auto&& e : a) {
        if(e <= h && e <= w) {
            long long cnt_h = h / e, cnt_w = w / e;
            long long cut_h = cnt_h * e, cut_w = cnt_w * e;
            long long d1 = dfs(cut_h, w - cut_w);
            long long d2 = dfs(h - cut_h, w);
            if(d1 == -1 || d2 == -1) return -1;
            long long r = cnt_h * cnt_w + d1 + d2;
            // cerr << h << ' ' << w << ' ' << r << endl;
            return r;
        }
    }
    return -1;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    long long h, w;
    cin >> h >> w;
    long long n;
    cin >> n;
    a.resize(n);
    for(auto&& e : a) cin >> e;
    reverse(a.begin(), a.end());
    cout << dfs(h, w) << '\n';
}