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
#include<bits/stdc++.h>
using namespace std;
vector <long long> v;
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    long long h, w;
    cin>>h>>w;
    long long n;
    cin>>n;
    long long x;
    for(int i=0; i<n; i++){
        cin>>x;
        v.push_back(x);
    }
    sort(v.begin(), v.end());
    reverse(v.begin(), v.end());
    long long ans=0, w2, zp1, zp2, zp3, zp4;
    for(int i=0; i<v.size(); i++){
        if(v[i]>h||v[i]>w) continue;
        w2=w;
        zp1=w2/v[i];
        zp2=h/v[i];
        ans+=zp1*zp2;
        w2%=v[i];
        h%=v[i];
        for(int j=i+1; j<v.size(); j++){
            zp3=w2/v[j];
            zp4=(zp2*v[i])/v[j];
            ans+=zp3*zp4;
            w2%=v[j];
        }
        if(w2!=0){
            cout<<-1;
            return 0;
        }
    }
    if(h!=0){
        cout<<-1;
        return 0;
    }
    cout<<ans;
    return 0;
}