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
#include<bits/stdc++.h>
#define ll long long
using namespace std;
long long h,w,n;
bool NIEEEE;
long long d[1000007];
long long ile=0;
ll suma=0;
void zapel(int A, int B, int N){
    if(min(A,B)==0){
        return;
    }
    if(min(A,B)!=0 && min(A,B)<d[1]){
        NIEEEE=true;
        return;
    }
    ll h;
    for(int i=N; i>=1; i--){
        if(min(A,B)>=d[i]){
            h=i;
            i=-1;
        }
    }
    ll X=A/d[h];
    ll Y=B/d[h];
    suma+=X*Y;
    ll AA=A-X*d[h];
    ll BB=B-Y*d[h];
    zapel(AA, B, h);
    zapel(A-AA, BB, h);

}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    NIEEEE=false;
    cin>>h>>w;
    cin>>n;
    for(int i=1; i<=n; i++){
        cin>>d[i];
    }
    zapel(h,w,n);
    if(NIEEEE){
        cout<<-1;

    }
    else
    cout<<suma;


}