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

int main() {
	ios::sync_with_stdio(false);
	cout.tie(nullptr);
	cin.tie(nullptr);

	int h, w, n;
	cin >> h >> w >> n;

	int arr[n], ans = 0;
	for(int& i : arr)
		cin >> i;

	sort(arr, arr+n, greater{});
	for(const int& i : arr) {
		if(i > h) continue;
		int _w = w, session = 0;
		for(const int* j = &i; j < arr+n; ++j) {
			session += (_w/(*j))*(i/(*j));
			_w -= (_w/(*j))*(*j);
		}
if(_w) {cout << "-1"; return 0;}
		ans += (h/i)*session;
		h -= (h/i)*i;
	}
	if(h) cout << "-1";
	else cout << ans;
}