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
#include <iostream>
#include <vector>

using namespace std;

long long h,w,x,y;
int n;
vector <long long> d;
int main()
{
    cin >> h >> w;
    cin >> n;
    for(int i=0;i<n;++i)
    {
        cin >> x;
        if(x<=h && x<=w)d.push_back(x);
    }
    x=0;
    if(d.size()==0)cout << -1;
    else{
        for(int i=n-1;i>=0;--i)
            {
                while(w>=d[i] && w>0)
                {
                    w=w-d[i];
                    x++;
                }
                if(h%d[i]==0){
                    y=y+(h/d[i])*x;
                    x=0;
                }
                if(w==0)break;
            }
            if(w>0)cout << -1;
            else cout << y;
    }
        return 0;
}