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

using namespace std;

typedef long long ll;

ll solveCase(ll h, ll w, ll palling, ll n, vector<ll> sizes) {
    

    if(h == 0 || w == 0){
        return 0;
    }

    if( palling == n){
        return -1;
    }

    ll pallSize = sizes[palling];

    ll pallUsedH = h/pallSize;
    ll pallUsedW = w/pallSize;
    
    //create recursion for two residual rectangles
    ll resHcov = solveCase(h - pallUsedH * pallSize, w, palling+1, n, sizes);
    ll resWcov = solveCase(pallUsedH * pallSize, w - pallUsedW * pallSize, palling+1, n, sizes);

    if(resHcov == -1 || resWcov == -1){
        return -1;
    }

    return pallUsedH * pallUsedW + resHcov + resWcov;

}

int main() {

    ll h, w;
    cin >> h >> w;

    ll n;
    cin >> n;
    vector<ll> palls;

    for(ll i=0; i<n; i++){
        ll a;
        cin >> a;
        palls.push_back(a);
    }

    sort(palls.rbegin(), palls.rend());
    cout << solveCase(h, w, 0, n, palls) << endl;


    return 0;
}