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
#include<iostream>
#include<vector>
#include<algorithm>
#define ll long long
using namespace std;

ll rek(ll x, ll y, ll w, ll h,vector<ll>tab){

    if(x==w || y==h){
        return 0;
    }
    vector<ll>osx;
    ll a = x;
    for(ll i = 0;i<tab.size();++i){
        ll ile_maks = (w-a)/tab[i];
        if (tab[i]>h-y){
            ile_maks=0;
        }
        a = a+ile_maks*tab[i];
        osx.push_back(ile_maks);
    }
    if(a!=w)return -1;
    vector<ll>osy;
    a = y;
    for(ll i = 0;i<tab.size();++i){
        ll ile_maks = (h-a)/tab[i];
        if(tab[i]>w-x){
            ile_maks=0;
        }
        a = a+ile_maks*tab[i];
        osy.push_back(ile_maks);
    }
    if(a!=h)return -1;
    ll fx=0;
    while(osx[fx]==0){
        ++fx;
    }
    
    ll big_x = tab[fx]*osx[fx];
    ll fy=0;
    while(osy[fy]==0){
        ++fy;
    }
    ll big_y = tab[fy]*osy[fy];
    ll ile=0;
    ile+=osx[fx]*osy[fy];


    for(ll i = fx+1;i<tab.size();++i){
        ll rep_y=big_y/tab[i];
        ile+=rep_y*osx[i];

    }
    for(ll i = fy+1;i<tab.size();++i){
        ll rep_x=big_x/tab[i];
        ile+=rep_x*osy[i];
    }
    ll small=rek(x+big_x,y+big_y,w,h,tab);
    if(small==-1)return -1;
    return small+ile;

}


int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll h,w,n;
    cin>>h>>w>>n;
    vector<ll>tab(n);
    for(ll i = 0;i<n;++i){
        cin>>tab[i];
    }
    sort(tab.begin(),tab.end());
    reverse(tab.begin(),tab.end());
    cout<<rek(0,0,w,h,tab);

}