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

using namespace std;

int main()
{
    int h, w, n;
    scanf("%d %d", &h, &w);
    scanf("%d", &n);
    int d[n];
    for (int i=0; i<n; i++)
    {
        scanf("%d", &d[i]);
    }

    sort(d, d+n, greater<int>());
    long long answer = 0;
    long long a=0, b=0;
    long long tmp1, tmp2;
    for (auto e : d)
    {
        tmp1 = (h-a)/e;
        tmp2 = (w-b)/e;
        answer += (long long) tmp1*(b/e);
        answer += (long long) tmp2*(a/e);
        answer += (long long) tmp1*tmp2;
        a += tmp1 * e;
        b += tmp2 * e;
    }

    if (a == h && b == w)
    {
        printf("%lld", answer);
    }
    else
    {
        printf("-1");
    }

    return 0;
}