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

long long n, pocz, koniec, suma = 0, licz = 0;

long long tab[35];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    long long p, n, h, w, licz = 0;
    cin >> h >> w >> n;
    p = h * w;
    for (long long i = 0; i < n; i++)
    {
        long long a;
        cin >> a;
        tab[i] = a;
    }
    long long j = n - 1;


    while (h > 0)
    {
        if (h == 0 || w == 0)
        {
            break;
        }
        bool pierw = false;
        long long wi = w, nowyh = h;

        for (long long i = n - 1; i >= 0; i--)
        {
            if (h == 0 || w == 0) break;
            if (wi / tab[i] > 0 && h / tab[i] > 0 && pierw == false)
            {
                pierw = true;
                nowyh = (h / tab[i]) * tab[i];
                licz += ((wi / tab[i]) * (h / tab[i]));
                wi = wi % tab[i];
            }
            else if (wi / tab[i] > 0 && nowyh / tab[i] > 0)
            {
                licz += ((wi / tab[i]) * (nowyh / tab[i]));
                wi = wi % tab[i];
            }
            
            if (h == 0 || w == 0) break;
            
        }
        h = h - nowyh;
        if (wi != 0)
        {
            cout << "-1";
            return 0;
        }
        j = n - 1;
        if (h == 0 || w == 0) break;
        
    }
    cout << licz;
}