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
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

unsigned char P[16777777];
unsigned int  A[16777777];
int S[16777777];
int _ils;

void nxt_num( char *B, int *num_bits )
{
    
    while ( *B )
          {
              (*num_bits)--;
              *(B--) = 0;
          }
    *B = 1;
    (*num_bits)++;
}

bool sort_func_rev( int i, int j )
{return i>j;}

int main()
{
    /*int a = 100000000;
    a *= 24;
    printf( "%d\n", a );*/
    unsigned int W[31],PW[107];
    
    int n,m;
    scanf("%d%d",&n,&m);
    
    for ( int i=0; i<n; i++ )
        scanf("%u",W+i);
    sort( W, W+n, sort_func_rev );
    
    unsigned int _B[107];
    for ( int i=0; i<m; i++ )
        scanf("%u",_B+i);
    sort( _B,_B+m, sort_func_rev );
    
    unsigned int _b = 0;
    for ( int i=0; i<m; i++ )
        {
            _b+=_B[i];
            PW[i]=_b;
        }
    PW[m]=3000000000u;
    
    P[0]=0;
    A[0]=0;
    int il_it = 1<<n;
    int _j = 1<<(n-1);
    
    //memset( P, '\0', sizeof(P) );
    //memset( A, '\0', sizeof(A) );
    
    int TT[30];
    char B[32];
    char *B2 = B+30;
    memset( TT, '\0', sizeof(TT) );
    
    int _n=0;
    memset( B, '\0', sizeof(B) );
    for ( int i=0; i<il_it; i++ )
        {
            TT[ _n ]++;
            nxt_num( B2, &_n);
        }
    
    int c = 0;
    for ( int i=0; i<=n; i++ )
        {
            int _c = c; c+=TT[i];
            TT[i]=_c;
        }
    //printf("%d\n",il_it);
    //for ( int i=0; i<n; i++ ) printf("%d\n",TT[i]); printf("\n");
    
    _n=0;
    memset( B, '\0', sizeof(B) );
    for ( int i=0; i<il_it; i++ )
        {
            S[ TT[ _n ]++ ]=i;
            nxt_num( B2, &_n);
        }
    
    //for ( int i=0; i<il_it; i++ ) printf("%03X\n",S[i]);
    
    //printf("%d\n",il_it); return 0;
    for ( int i=1; i<il_it; i++ )
        {
            //printf("%d\n",i);
            int s = S[i];
            int j = _j;
            unsigned int *_w = W;
            
            int pp=m;
            unsigned int aa=PW[m];
            
            while ( j )
                {
                    if ( s&j )
                       {
                           int          elm = s^j;
                           unsigned int  a  = A[elm];
                           int           p  = P[elm];
                           unsigned int  pw = PW[p];
                           unsigned int  w  = *_w;
                           unsigned int  _a=a+w;
                           
                           if ( _a>pw )
                              {
                                  a=pw+w;
                                  p++;
                                  if ( a>PW[p] ) goto _fail;
                              } else a=_a;
                           
                           if ( a<aa )
                              {
                                  pp=p;
                                  aa=a;
                              }
                           
                           _fail:;
                       }
                    
                    _w++;
                    j >>= 1;
                }
            P[s]=pp;
            A[s]=aa;
        }
    
    if ( P[il_it-1] < m )
        printf("%d\n",P[il_it-1]+1);
       else
        printf("NIE\n");//*/
    
    return 0;
}