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
#include <bits/stdc++.h>
#define MP make_pair
#define PB push_back
using namespace std;
int a, h, n;
long long solve(int dist, int from, vector<int>& obr){
    long long res=1;
    for(int i=from; i>=0 && dist!=0; i--){
        res+=obr[from]/obr[i]*(dist/obr[i]);
        dist=dist%obr[i];
    }
    if(dist!=0){
        cout << -1 << "\n";
        return -1;   
    }
    return res;
}

int main(){
    ios_base::sync_with_stdio(NULL);
    cin.tie(NULL);
    
    cin >> a >> h >> n;
    vector<int> obr(n);
    for(int i=0; i<n; i++){
        cin >> obr[i];
    }
    long long res=0;
    queue<pair<int, pair<int,int>>> qu;

    for(int i=n-1; i>=0 && a>0; i--){
        if(obr[i]>h || obr[i]>a)
            continue;
        qu.push(MP(h-obr[i], MP(i, a/obr[i])));
        a=a%obr[i];
    }
    if(a!=0){
        cout << -1 << "\n";
        return 0;
    }
    while(!qu.empty()){
        long long dist, from, cnt;
        dist=qu.front().first;
        from=qu.front().second.first;
        cnt = qu.front().second.second;
        qu.pop();
        if(cnt==0)
            continue;
        long long ret = solve(dist, from, obr);
        if(ret==-1)
            return 0;
        ret*=cnt;
        res+=ret;
    }
    cout << res << "\n";
    return 0;
}