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

using namespace std;
queue <long long> Q;
long long tab[50];

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
   long long w,h;
   cin>>w>>h;
   Q.push(w);
   Q.push(h);
   int n;
   cin>>n;
   for(int i=0; i<n; i++)cin>>tab[i];
   sort (tab,tab+n);
   long long wynik=0;
   while(!Q.empty())
   {
       w=Q.front();
       Q.pop();
       h=Q.front();
       Q.pop();
       if(h==0||w==0)continue;
       bool flag=0;
       for(int i=n-1; i>=0; i--)
       {
           if(tab[i]<=h&&tab[i]<=w)
           {
               flag=1;
                   Q.push(h-tab[i]);
                   Q.push(w);
                   Q.push(w-tab[i]);
                   Q.push(tab[i]);
              wynik++;
              break;
           }
       }
       if(flag==0)
       {
           wynik=-1;
           break;
       }
   }
   cout<<wynik;

    return 0;
}