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
#include <iostream>
#include <vector>
#include <algorithm>

long long chk(long long h, long long w, std::vector<long long> V) {
    if(h == 0 || w == 0)
        return 0;
    while(!V.empty() && (V.back() > h || V.back() > w))
        V.pop_back();
    if(V.empty())
        return -1;
    long long hh = h / V.back();
    long long ww = w / V.back();
    long long ans = hh * ww;
    hh *= V.back();
    ww *= V.back();
    auto tmp = chk(h - hh, w, V);
    if(tmp == -1)
        return -1;
    ans += tmp;
    tmp = chk(hh, w - ww, V);
    if(tmp == -1)
        return -1;
    ans += tmp;
    return ans;
}

int main() {
    long long h, w;
    scanf("%lld%lld", &h, &w);
    long long n;
    scanf("%lld", &n);
    std::vector<long long> V(n);
    for(long long i = 0; i < n; i++)
        scanf("%lld", &V[i]);
    std::sort(V.begin(), V.end());
    printf("%lld", chk(h, w, V));
    return 0;
}