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
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <cmath>
#include <cstring>
#include <string>
#include <iostream>
#include <complex>
#include <sstream>
using namespace std;
 
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef vector<int> VI;
typedef pair<int,int> PII;
 
#define REP(i,n) for(int i=0;i<(n);++i)
#define SIZE(c) ((int)((c).size()))
#define FOR(i,a,b) for (int i=(a); i<(b); ++i)
#define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i)
#define FORD(i,a,b) for (int i=(a)-1; i>=(b); --i)
#define ALL(v) (v).begin(), (v).end()
 
#define pb push_back
#define mp make_pair
#define st first
#define nd second

int T[30];
int P[120];
LL S[120];

LL W[1<<24];

bool DP[2][1<<24];
bool dotry[1<<24];

void scase() {
  int N,M;
  scanf("%d%d",&N,&M);
  REP(i,N) scanf("%d",&T[i]);
  REP(i,M) scanf("%d",&P[i]);
  sort(P, P+M);
  reverse(P, P+M);
  M = min(M, N);
  S[0] = P[0];
  FOR(i,1,M) S[i] = S[i-1] + P[i];

  REP(i, 1<<N) {
    W[i] = 0;
    REP(j,N) if (i&(1<<j)) W[i] = W[i] + T[j]; 
  }

  REP(i, 1<<N) {
    DP[0][i] = (W[i] <= P[0]);
  }
  
  if (DP[0][(1<<N) - 1]) {
    printf("1\n");
    return;
  }
  
  FOR(p,1,M) {
    REP(i, 1<<N) dotry[i] = false;

    REP(i, 1<<N) {
      DP[p%2][i] = W[i] <= P[p];

      if (dotry[i] && S[p] >= W[i]) {
        for(int j = i; !DP[p%2][i]; j = (j-1)&i) {
          if (W[j] <= P[p] && DP[1 - p%2][i - j])
            DP[p%2][i] = true;
          if (!j) break;
        }
      }
      
      if (DP[p%2][i]) REP(k,N) dotry[i|(1<<k)] = true;
    }
    
    if (DP[p%2][(1<<N) - 1]) {
      printf("%d\n", 1 + p);
      return;
    }
  }
  
  printf("NIE\n");
}

int main() {
  scase();
}