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
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<tr1/unordered_map>
#include<queue>
#include<cstdlib>
#include<list>
#include<set>
#include<map>
#include<cmath>
#define MP make_pair
#define PB push_back
#define s second
#define f first
#define PII pair<int,int>
#define VPII vector <pair<int,int> >
#define VI vector <int>
#define abs(a) max((a),-(a))
#define LL long long
#define LD long double
#define ALL(x) x.begin(),x.end()
#define PU putchar_unlocked
#define GU getchar_unlocked
#define DBG(x) cerr<<#x<<" "<<(x)<<endl;
using namespace std;
int a,b,c,d,e,f,n,m,mx,l,z,r,k;
int wynik;
char ch;
int INF=1e9+1;
const int mmask=(1<<24)+1;
pair<char,int> dp[mmask];//ile plecaków, suma
VI przed;
VI plec;
void solve()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
    {
    scanf("%d",&a);
    przed.PB(a);
    }
for(int i=1;i<=m;i++)
    {
    scanf("%d",&a);
    plec.PB(a);
    }
sort(ALL(plec));
reverse(ALL(plec));
while(plec.size()>n)plec.pop_back();
for(int i=1;i<(1<<n);i++)dp[i]=MP(30,INF);
PII x;
for(int i=0;i<(1<<n);i++)
    {
  //  printf("%d %d %d\n",i,dp[i].f,dp[i].s);
    if(dp[i].f==30)continue;
    for(int j=0;j<n;j++)
        {
        if(!(i&(1<<j)))
            {
            if(dp[i].s+przed[j]<=plec[dp[i].f])
                {
                dp[i|(1<<j)]=min(dp[i|(1<<j)],MP(dp[i].f,dp[i].s+przed[j]));
                }
            else
                {
                if(dp[i].f+1<plec.size()&&przed[j]<=plec[dp[i].f+1])
                    {
                    dp[i|(1<<j)]=min(dp[i|(1<<j)],MP((char)(dp[i].f+1),przed[j]));
                    }
                }
            }
        }
    }
if(dp[(1<<n)-1].f==30)puts("NIE");
else printf("%d",dp[(1<<n)-1].f+1);
}
main()
{
solve();
}