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

vector<ll> Sides;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    ll h, w, n, d;
    cin >> h >> w;
    cin >> n;

    for(int i = 0; i < n; i++) {
        cin >> d;
        Sides.push_back(d);
    }

    ll sol = 0;

    for(int i = n-1; i >= 0; i--) {
        if(h < Sides[i] || w < Sides[i])
            continue;
        
        ll used = h/Sides[i];
        ll length_used = used*Sides[i];
        sol += used;
        ll w2 = w - Sides[i];
        for(int j = i; j >= 0; j--) {
            if(w2 >= Sides[j]) {
                ll used2 = w2/Sides[j];
                sol += (length_used/Sides[j]) * used2;
                w2 %= Sides[j];
            }
        }
        if(w2 != 0) {
            cout << "-1";
            return 0;
        }
        h -= length_used;
    }
    if(h != 0) {
        cout << "-1";
    } else {
        cout << sol;
    }
}