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

using namespace std;

long long N, WALL_A, WALL_B;
vector<pair<long long, long long>> WALLS;
long long paintings[31];
long long sum=0;

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

    cin>>WALL_A>>WALL_B>>N;
	WALLS.push_back(make_pair(WALL_A, WALL_B));
  
    for(int i=0; i<N; ++i){
		cin>>paintings[i];
	}


	for(int i=0, p=0; i<N && p<N; ++i, ++p){
	    if(WALLS[i].first < paintings[N-p-1] || WALLS[i].second < paintings[N-p-1]){
	        i--;
	        continue;
	    }
	    for(int j = 0; WALLS[i+j].first >= paintings[N-p-1] && WALLS[i+j].second >= paintings[N-p-1]; ++j){
	        
    		sum += WALLS[i+j].first/paintings[N-p-1] * WALLS[i+j].second/paintings[N-p-1];
    
    		WALLS.push_back(make_pair(WALLS[i+j].first%paintings[N-p-1], WALLS[i+j].second));
    		
    		if(WALLS[i+j].first%paintings[N-p-1] == 0 && WALLS[i+j].second%paintings[N-p-1] == 0){
    		    break;
    		}
			
			if(WALLS[i+j].first%paintings[N-p-1] == 0 || WALLS[i+j].second == 0 && WALLS[i+j].first%paintings[N-p-1] != WALLS[i+j].second){
			//cout<<WALLS[WALLS.size()-1].first<<'-'<<WALLS[WALLS.size()-1].second<<endl;
    			WALLS.pop_back();
    		}
    		else {
        		break;
    		}
    		WALLS.push_back(make_pair(WALLS[i+j].first, WALLS[i+j].second%paintings[N-p-1]));
    		
    		if(WALLS[i+j].first%paintings[N-p-1] == 0 && WALLS[i+j].second%paintings[N-p-1] == 0){
    		    break;
    		}
    		
    		if(WALLS[i+j].first== 0 || WALLS[i+j].second%paintings[N-p-1] == 0 && WALLS[i+j].first%paintings[N-p-1] != WALLS[i+j].second){	
    			WALLS.pop_back();
    		}
    		else {
        		break;
    		}
    	}
	}
	
	if(WALLS[WALLS.size()-1].first == 0 || WALLS[WALLS.size()-1].second == 0){
	    cout<<sum;
	}
	else cout<<-1;
	
  return 0;
}