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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

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

	long long h, w, n;
	cin >> h >> w >> n;
	vector<long long>d(n);
	for (auto& x : d) cin >> x;
	reverse(d.begin(), d.end());

	vector<long long>il(n);
	for (int i = 0; i < n; i++) {
		il[i] = h / d[i];
		h -= d[i] * il[i];
	}

	if (h) {
		cout << "-1\n";
		return 0;
	}

	long long result = 0;
	for (int i = 0; i < n; i++) {
		long long W = w;
		vector<long long>X(n);
		for (int j = i; j < n; j++) {
			X[j] = W / d[j];
			W -= d[j] * X[j];
			result += X[j] * il[i] * (d[i] / d[j]);
		}

		if (W) {
			cout << "-1\n";
			return 0;
		}
	}
	cout << result << '\n';

	return 0;
}