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

using namespace std;

int main(){
	int fW=0,fH=0,w=0,h=0,a=0,b=0,fB=0,hH=0;
	bool z = 1;
	cin >> h >> w >> b;
	fH = h;
	fW = w;
	fB = b;
	int s[b];

	for(int i = 0;i < b;i++)cin >> s[i];


	for(int i = (b-1);i >= 0;i--){
		if(s[i] <= h && s[i] <= fW /*check if square fits*/){
			h = h%s[i];
			//cout << "woo, a = " << a << "  new a = a + (fH-h)/s[i] =  " << (a+(fH-h)/s[i]) << "  fh - h = " << (fH-h) << "  s[i] = " << s[i] << "  fhhh/s[i] = " << ((fH-h)/s[i]) << endl;
			a = a + (fH - h)/s[i];
			hH = max(s[i],hH);
			if(h==0){
				//cout << "wee : " << fW << " - " << hH << " = " << fW-hH << endl;
				fW = fW - hH;
				if(fW == 0){
					cout << a;
					z = 0;
					break;
				}
				else{
					h = fH;
					hH = 0;
					i = (b-1);
				}
			}
		}
	}
	if(z)cout<<"-1";
}