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

ll H,W,D,P;
int n;
vector<ll>a;

void rec(int pos, ll left, int taken){
    if(pos==n){
        if(!left){
            cout<<taken<<'\n';
            exit(0);
        }
        return;
    }
    rec(pos+1,left-left/a[pos]*a[pos],taken+left/a[pos]);
    rec(pos+1,left,taken);
}

void solve(){
    cin>>H>>W;
    P=H*W;
    cin>>n;
    a.resize(n);
    for(ll&i:a){
        cin>>i;
        i=i*i;
    }
    reverse(a.begin(),a.end());
    rec(0,P,0);
    cout<<-1<<'\n';
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int t=1;
    //cin>>t;
    while(t--)
        solve();
}