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
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
#include <bits/stdc++.h>

using namespace std;

#define JOIN_(X, Y) X##Y
#define JOIN(X, Y) JOIN_(X, Y)
#define TMP JOIN(tmp, __LINE__)
#define PB push_back
#define SZ(x) int((x).size())
#define REP(i, n) for (int i = 0, TMP = (n); i < TMP; ++i)
#define FOR(i, a, b) for (int i = (a), TMP = (b); i <= TMP; ++i)
#define FORD(i, a, b) for (int i = (a), TMP = (b); i >= TMP; --i)

#ifdef DEBUG
#define DEB(x) (cerr << x)
#else
#define DEB(x)
#endif

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;

const ll INF = (ll)1e18 + 9LL;

vi t;

map<pii, ll> mem;

ll rec(int h, int w) {
    if (h < w) {
        swap(h, w);
    }
    pii key(h, w);
    if (mem.count(key) > 0) {
        return mem[key];
    }
    // cerr << "h=" << h << " w=" << w << " start=" << start << "\n";
    ll result = INF;
    FOR(i, 0, SZ(t) - 1) {
        int x = t[i];
        if (h >= x and w >= x) {
            ll h_left = (ll)(h / x) * x;
            ll w_left = (ll)(w / x) * x;
            ll cnt_1 = (ll)(h / x) * (ll)(w / x);
            if (h % x > 0) {
                // cerr << "call 11 h=" << h % x << " w=" << w_left << " start=" << start + 1 <<
                // "\n";
                cnt_1 = min(INF, cnt_1 + rec(h % x, w_left));
            }
            if (w % x > 0) {
                // cerr << "call 12 h=" << h << " w=" << w % x << " start=" << start + 1 << "\n";
                cnt_1 = min(INF, cnt_1 + rec(h, w % x));
            }
            // cerr << "cnt_1 (" << start << ") = " << cnt_1 << "\n";
            result = min(result, cnt_1);
            // ll cnt_2 = (ll)(h / x) * (ll)(w / x);
            // if (h % x > 0) {
            //     // cerr << "call 21 h=" << h % x << " w=" << w << " start=" << start + 1 << "\n";
            //     cnt_2 = min(INF, cnt_2 + rec(h % x, w, start + 1));
            // }
            // if (w % x > 0) {
            //     // cerr << "call 22 h=" << h_left << " w=" << w % x << " start=" << start + 1 <<
            //     // "\n";
            //     cnt_2 = min(INF, cnt_2 + rec(h_left, w % x, start + 1));
            // }
            // // cerr << "cnt_2 (" << start << ")= " << cnt_2 << "\n";
            // result = min(result, cnt_2);
            break;
        }
    }
    // cerr << "h=" << h << " w=" << w << " start=" << start << " ===> " << result << "\n";
    return mem[key] = result;
}

void inline one() {
    int h, w;
    cin >> h >> w;
    // if (h < w) {
    //     swap(h, w);
    // }
    int n;
    cin >> n;
    REP(i, n) {
        int x;
        cin >> x;
        t.emplace_back(x);
    }
    sort(t.begin(), t.end(), greater());
    ll result = rec(h, w);
    if (result >= INF) {
        cout << -1 << "\n";
    } else {
        cout << result << "\n";
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    // int z; cin >> z; while(z--)
    one();
}