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

int minPostersHelper(int wallWidth, int wallHeight, const vector<int>& posterSizes, unordered_map<int, int>& memo) {
	if (wallWidth <= 0 || wallHeight <= 0) {
		return 0;
	}

	if (memo.count(wallWidth * 1000 + wallHeight)) {
		return memo[wallWidth * 1000 + wallHeight];
	}

	int minPosters = numeric_limits<int>::max();

	for (int i = 0; i < posterSizes.size(); ++i) {
		if (posterSizes[i] <= wallWidth && posterSizes[i] <= wallHeight) {
			int result = minPostersHelper(wallWidth - posterSizes[i], wallHeight, posterSizes, memo) +
				minPostersHelper(posterSizes[i], wallHeight - posterSizes[i], posterSizes, memo) + 1;
			minPosters = min(minPosters, result);
		}
	}

	memo[wallWidth * 1000 + wallHeight] = minPosters;
	return minPosters;
}

int minPosters(int wallWidth, int wallHeight, const vector<int>& posterSizes) {
	if (wallWidth <= 0 || wallHeight <= 0) {
		return 0;
	}

	unordered_map<int, int> memo;
	return minPostersHelper(wallWidth, wallHeight, posterSizes, memo);
}

int main() {
	int wallWidth, wallHeight;
	vector<int> posterSizes;

	cin >> wallWidth;
	cin >> wallHeight;

	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		int posterSize;
		cin >> posterSize;
		posterSizes.push_back(posterSize);
	}

	int result = minPosters(wallWidth, wallHeight, posterSizes);

	if (result == numeric_limits<int>::max()) {
		cout << -1;
	}
	else {
		cout << result;
	}
	return 0;
}