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 <cstdio>

int d[32];

long long zapisz_rect(int n, int w, int h, int &new_h)
{
  int leftH = h;
  int leftW = w;
  long long ret = 0;
  int alignH = -1;
  for (int i = n - 1; i >= 0; i--)
  {
    if (leftW >= d[i] && leftH >= d[i])
    {
      int x = leftW / d[i];
      int y = leftH / d[i];
      ret += 1LL * x * y;

      x *= d[i];
      y *= d[i];
      leftW -= x;
      if (alignH == -1)
      {
        alignH = d[i];
        leftH = y;
        new_h = h - y;
      }
    }
  }
  if (leftW > 0) return -1;
  return ret;
}

int main()
{
  int h, w;
  scanf("%d %d", &h, &w);
  
  int n;
  scanf("%d", &n);
  
  
  for (int i = 0; i < n; i++)
  {
    scanf("%d", d + i);
  }
  
  int new_h = h;
  long long ret = 0;
  
  for (int i = 0; i < n; i++)
  {
    if (new_h == 0) break;
    long long s = zapisz_rect(n, w, h, new_h);
    //printf("i = %d, n = %d, w = %d, h = %d, new_h = %d, s =%lld\n", i, n, w, h, new_h, s);
    if (s == -1)
    {
      printf("-1\n");
      return 0;
    }
    h = new_h;
    ret += s;
  }
  if (new_h > 0) ret = -1;
  printf("%lld\n", ret);  
  return 0;
}