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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <functional>
#include <bitset>




using namespace std;

int n,m;
int best;


int minimum;

vector<int> orgc;


int counter =0;

int L0 (const vector<int> &w, int wstart, const vector<int> &c )
{

    long long int sumaw=0;
    for (int i=wstart;i<w.size();i++)
    {
        sumaw+=w[i];
    }
    int mini = 0;
    while ( sumaw>0 && mini<m )
    {
        if (w.back()<c[mini])sumaw-=c[mini];
        mini++;
    }
    if (sumaw>0)
    {

        return 30;
    }

    return mini;


}

int Lalfa (int c,  const vector<int> &w, int alfa)
{

    int j1=0, j2=0, j3=0;
    long long int s2=0,s3=0;

    for (int i=0;i<w.size();i++)
    {
        if (w[i]>c-alfa)
        {
            j1++;
        }
        else if (2*w[i]>c)
        {
            j2++;
            s2+=w[i];
        }
        else if (w[i]>alfa)
        {
            j3++;
            s3+=w[i];
        }
    }

    int wyn = j1+j2+ max(0LL , (s3 - (j2*c - s2) +(c-1))/c );
    //cout<<"Lalfa gada "<<wyn<<" "<< alfa/(double)c<<endl;

    return wyn;
}

int L2 (const vector<int> &ww, int wstart, const vector<int> &c )
{
    int cmax= 0;

    vector<int> w(ww.begin()+wstart, ww.end() );

    for (int i=0;i<c.size();i++)
    {
        if (cmax<c[i])cmax=c[i];
    }

    for (int i=0;i<c.size();i++)
    {
        if (( c[i]!=orgc[i]  )&&(cmax-c[i]>0))
        {
            //cout<<"dod "<<cmax-c[i]<<" ";
            w.push_back(cmax-c[i]);
        }
    }
    //cout<<endl;

    int res = -1;
  //  cout<<"cmax "<<cmax<<endl;
  /*  for (int i=0;i<w.size();i++)
    {
        cout<<"w "<<w[i]<<" ";
    }cout<<endl;
*/
    for (int i=0;i<w.size();i++)
    {
        if (w[i]*2<=cmax) res = max(res, Lalfa(cmax, w,w[i]));
    }
    if (res==-1) res = w.size();

    return res;

}



int heuryst( const vector<int> &w, vector<int> c )
{
    int max_wykorzystany=0;
    for (int iw =0; iw<w.size(); iw++ )
    {
        int i=0;
        while ((i<m) && (w[iw]>c[i]))
            i++;

        if (i==m) return 10000;
        else max_wykorzystany = max(max_wykorzystany,i+1);
        c[i]-=w[iw];
    }
    return max_wykorzystany;


}


void idz( const vector<int> & w,int nastepny, int inic, vector<int> &c )
{
    int licz=0;

    counter++;
    if (counter>1500000) return;


    int propozycja = heuryst(w,c);
    best = min(propozycja, best);



    for (int i=0;i<c.size();i++)
    {
        if (c[i]!=orgc[i]) licz++;
       // cout<<c[i]<<" ";
    }
    if ( (licz>=best) || (best == minimum)) return;


    int lower_aprox =  max( max(licz, L2(w,nastepny,c)) ,L0(w,nastepny,c) ) ;
    //int lower_aprox =   max(licz, L2(w,nastepny,c))   ;
    //cout<<"L2 "<<lower_aprox<<endl;

    if (lower_aprox >= best)
    {
       // cout<<"L2 2 "<<lower_aprox<<endl;
        return;
    }


    if (nastepny==w.size())
    {
        //   cout<< "propozycna ";
        best = min(best, licz);
   //     cout<< "wyn "<< licz;
      //  cout<<endl;
        return;
    }
    for (int i=0;i<min((int)c.size(),best);i++)
    {
        c[i] -= w[nastepny];
        if (c[i]>=0) idz(w,nastepny+1, inic, c);
        c[i] += w[nastepny];
    }
   /* if (inic+1< c.size() )
    {
        inic++;
        c[inic] -= w[nastepny];
        if (c[inic]>=0) idz(w,nastepny+1, inic, c);
        c[inic] += w[nastepny];
    }*/
}




int main()
{


    cin.sync_with_stdio(false);
    cin>>n>>m;


    best = 10000;


    vector <int> w(n,0),c(m,0);


    for (int i=0;i<n;i++)
        cin>>w[i];
    for (int i=0;i<m;i++)
        cin>>c[i];




    sort(c.begin(),c.end(),greater<int>());
    sort(w.begin(),w.end(),greater<int>());


    long long int sumaw=0;
    for (int i=0;i<w.size();i++)
    {
        sumaw+=w[i];
    }
    minimum = 0;
    while ( sumaw>0 && minimum<m )
    {
        sumaw-=c[minimum];
        minimum++;
    }
    if (sumaw>0)
    {
        cout<<"NIE"<<endl;
        return 0;
    }


    m = min(m,n);
    c.resize( m );
    orgc=c;

    best = heuryst(w,c);
    int lower = L2(w,0,c);

   // cout<<"heur "<<best<<" min "<<minimum<<" L "<<lower<<endl;



    idz(w,0,0,c);

    if (best==10000)
        cout<<"NIE"<<endl;
    else
        cout<<best <<endl;

  /*  for (int i=0;i<n;i++)
        cout<<w[i]<<" ";
    cout<<endl;
    for (int i=0;i<m;i++)
        cout<<c[i]<<" ";
*/


    return 0;
}

/*

4 3
4 2 10 3
11 18 9



 *
 *
 *
4 7
4 2 10 3
11 18 9 1 2 4 8



  */