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
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <bits/stdc++.h>
using namespace std;

vector<long long> obrazy;
long long a,b,c,d,wyn;
bool flag;
void redprst(){
	long long tempa=a,tempb=b,maxn=0,ilmn,ila,ilb;
	if(a>b){
		for(int i=obrazy.size()-1;i>=0;i--){
			if(obrazy[i]<=b){
				maxn=obrazy[i];
				break;
			}
		}
		if(b<=0 or a<=0){
			return;
		}
		else if(maxn<=0){
			//cout << "fllglg";
			flag = true;
			return;
		}
		//cout << "MAXN=" << maxn << endl;
		ilb = b/maxn;
		ila = a/maxn;
		//cout << "ILOSC MAXN=" << ila*ilb <<endl;;
		wyn+=ila*ilb;
		tempb-=ilb*maxn;
		//cout << tempb <<endl;
		while(tempb>0){
			for(int i=obrazy.size()-1;i>=0;i--){
				if(obrazy[i]<=tempb){
					//cout << "A:" << obrazy[i] <<" "<< (maxn/obrazy[i])*(tempb/obrazy[i])*ila << endl;;
					wyn+=(maxn/obrazy[i])*(tempb/obrazy[i])*ila;
					tempb-=tempb/obrazy[i]*obrazy[i];
					//cout << tempb << endl;
					break;
				}
				else if(i==0){
					//cout << "fl";
					flag = true;
					break;
				}
			}
			if(flag) break;
		}
		a-=ila*maxn;

	}
	else{
		for(int i=obrazy.size()-1;i>=0;i--){//cout << obrazy[i];
			if(obrazy[i]<=a){
				
				maxn=obrazy[i];
				break;
			}
		}
		if(b<=0 or a<=0){
			return;
		}
		else if(maxn<=0){
			//cout << " fdklfkdl";
			flag = true;
			return;
		}
		//cout << "MAXN=" << maxn << endl;
		ilb = b/maxn;
		ila = a/maxn;
		wyn+=ila*ilb;
		//cout << "ILOSC MAXN=" << ila*ilb << endl;
		tempa-=ila*maxn;
		while(tempa>0){
			for(int i=obrazy.size()-1;i>=0;i--){
				if(obrazy[i]<=tempa){
					//cout << "B" << obrazy[i] << ' ' <<(maxn/obrazy[i])*(tempa/obrazy[i])*ilb << endl;;
					wyn+=(maxn/obrazy[i])*(tempa/obrazy[i])*ilb;
					tempa-=tempa/obrazy[i]*obrazy[i];
					//cout << tempa << endl;
					break;
				}
				else if(i==0){
					//cout << "fl";
					flag = true;
					break;
				}
			}
			if(flag) break;
		}
		b-=ilb*maxn;

	}
	
}

int main(){
	cin.tie()->sync_with_stdio(0);
	cin >> a >> b >> c;
	for(int i=0;i<c;i++){
		cin >> d;
		obrazy.push_back(d);
	}
	while(a>0 and b>0){
		//cout << "1it" << endl;
		redprst();
		//cout << endl;
		if(flag){ cout << -1;
		return 0;
		}
	}
	cout<< wyn;
}