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

using namespace std;

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

    long long x, y, n, l=0, cl = 0;
    cin >> x >> y >> n;
    long long wymiary[n], cx = x, cy = y;
    vector <pair <int, int>> xy;
    xy.push_back(make_pair(x, y));
    for(int i=0; i<n; i++)
    {
        cin >> wymiary[i];
    }
    sort(wymiary, wymiary+n);
    for(int i=n-1; i>=0; i--)
    {
        for(int j = 0; j<xy.size(); j++)
        {
            if(xy[j].first >= wymiary[i] && xy[j].second >= wymiary[i])
            {
                cx = xy[j].first / wymiary[i];
                cy = xy[j].second / wymiary[i];

                if(xy[j].first % wymiary[i] == 0 && xy[j].second > 0)
                {
                    xy[j].second -= cy * wymiary[i];
                }
                else if(xy[j].second % wymiary[i] == 0 && xy[j].first > 0)
                {
                    xy[j].first -= cx * wymiary[i];
                }
                else if(xy[j].second % wymiary[i] == 0 && xy[j].first % wymiary[i] == 0)
                {
                    l += cx * cy;
                    break;
                }
                else
                {
                    xy.push_back(make_pair(wymiary[i], xy[j].first -= cx * wymiary[i]));
                    //xy[j].second -= cy * wymiary[i];   
                }
            }
            l += cx * cy;
            cl = 0;
            cx = 0;
            cy = 0;
            if(xy[j].second == 0 || xy[j].first == 0)
            {
                xy.erase(xy.begin() + j);
                j--;
            }
            else if(xy[j].second < wymiary[0] || xy[j].first < wymiary[0])
            {
                cout << -1;
                return 0;
            }
        }
        
    }

    if(xy.empty())    cout << l;
    else    cout << -l;
    



    return 0;
}