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

ll d[33];

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ll h,w,n;
    cin >> h >> w;
    cin >> n;
    for (int x=1;x<=n;x++) cin >> d[x];
    ll odp=0;
    for (int x=n;x>=1;x--){
        ll temp = h;
        ll temp2=0;
        for (int y=x;y>=1;y--){
            temp2 += (temp/d[y])*(d[x]/d[y]);
            temp = temp%d[y];
        }
        if (temp>0){
            cout << "-1\n";
            return 0;
        }
        odp += temp2*(w/d[x]);
        w%=d[x];
    }
    if (w>0){
        cout << "-1\n";
        return 0;
    }
    cout << odp << '\n';
    return 0;
}