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
78
79
80
81
82
83
84
85
#include <stdio.h>

int n,m;
int a[24],c[100];

int cmp(const void *a, const void *b)
{
 return (*(int*)b-*(int*)a);
}

int test1(int ile,int i)
{
 int j=0,k,z;
 if(i>=n)return 1;
 for(;a[i]<=c[j] && j<ile;++j){
//  printf("%d\n",a[i]);
  c[j]-=a[i]; 
  z=c[j];
  qsort(c,ile,sizeof(int), cmp);
//  for(k=0;k<ile;++k)printf("%d ",c[k]);
//  printf("\n");
  if(test1(ile,i+1))return 1;
  k=0;
  while(c[k]!=z)++k;
  c[k]+=a[i];
  qsort(c,ile,sizeof(int), cmp);
 }
 return 0;
}

int test(int ile)
{
 int i=0;
 for(;i<ile;++i){
  
 }
 return 1;
}

int main(void)
{
 int i=0,ic=1;
 long long int s=0,sc=0;
 scanf("%d %d",&n,&m);
 for(;i<n;++i)
 {
  scanf("%d",a+i);
  s+=*(a+i);
 }
 for(i=0;i<m;++i)
 {
  scanf("%d",c+i);
 }
 qsort(a,n,sizeof(int), cmp);
 qsort(c,m,sizeof(int), cmp);
 for(i=0;i<m;++i)
 {
  scanf("%d",c+i);
  sc+=*(c+i);
  //printf("%d: %lld %lld\n",i,sc,s);
  if(sc<s)++ic;
 }
 //printf(": %lld %lld\n",s,sc);
 if(*a>*c){printf("NIE\n");return 0;}
 if(s>sc){printf("NIE\n");return 0;}
 if(ic>n){printf("NIE\n");return 0;}
 if(test1(ic,0))printf("%d\n",ic);
 else if(ic==m)printf("NIE\n");
 else printf("%d\n",ic+1);
 //printf("%d\n",test1(ic,0));
 //printf("%d\n",ic);
 /*
 for(i=0;i<n;++i)
 {
  printf("%d\t",a[i]);
 }
 printf("\n");
 for(i=0;i<m;++i)
 {
  printf("%d\t",c[i]);
 }
 printf("\n");
 */
 return 0;
}