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
#include <iostream>
#include <map>
#include <algorithm>
#include <vector>
#include <set>

int calculate(std::vector<int> *obr, int w, int h){

    int min = std::min(w,h);
    int max = std::max(w,h);
    if(min == 0)
        return 0;
    for(std::vector<int>::reverse_iterator riter = obr->rbegin(); riter != obr->rend(); ++riter){
        if(min / *riter > 0){
            int len_of_pics = min / *riter;

            int sq = len_of_pics * *riter;

            int w1 = sq;
            int h1 = max - sq;
            int w2 = sq;
            int h2 = min - sq;
            int w3 = max - sq;
            int h3 = min - sq;
            int num_of_pics = len_of_pics*len_of_pics;
            return num_of_pics + calculate(obr, w1, h1) + calculate(obr, w2, h2) + calculate(obr, w3, h3);
        }
    }
    return -1;
}

int main() {

    int w;
    int h;
    std::cin >> w;
    std::cin >> h;

    int n;
    std::cin >> n;

    std::vector<int> obr;

    for(int i = 0; i < n; i++){
        int s;
        std::cin >> s;
        obr.push_back(s);
    }

    if(w%obr.at(0) != 0 || h%obr.at(0) != 0){
        std::cout << "-1\n";
        return 0;
    }

    std::cout << calculate(&obr, w, h) << "\n";

    return 0;
}