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
53
54
55
56
57
58
59
60
61
62
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vl = vector<ll>;
using vb = vector<bool>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using str = string;
#define all(a) a.begin(), a.end()
#define print(a) for (auto elem:a) cout<<elem<<' '; cout<<'\n'
#define segprep(b) resize(1<<((int)ceil(log2(b.size()))+1))

ll h, w, n;
vl sizes;
ll total_cost = 0;
ll f_row = 0; //cost of f_row
ll f_row_f; //ind of biggest
void fnd_f_row(){
    for (int i = n-1; i >= 0; i--){
        if ((sizes.at(i) <= h) && (sizes.at(i) <= w)){
            f_row_f = i;
            break;
        }
    }

    ll curr_w = 0;
    f_row = 0;
    for (int i = f_row_f; i >= 0; i--){
        ll times = (w-curr_w)/sizes.at(i);
        f_row += times*(sizes.at(f_row_f)/sizes.at(i));
        curr_w += times*sizes.at(i);
    }
}

void rec(){
    fnd_f_row();
    ll times = h/sizes.at(f_row_f);
    total_cost += f_row*times;
    h -= times*sizes.at(f_row_f);
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    
    cin>>h>>w>>n;
    sizes.resize(n);
    for (auto &i:sizes) cin>>i;

    if ((h%sizes.front() != 0) || (w%sizes.front() != 0)){
        cout<<"-1\n";
        return 0;
    }

    while(h > 0){
        rec();
    }

    cout<<total_cost<<'\n';
}