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
#include <cstdio>
#include <algorithm>
#include <functional>
#include <random>
#include <cstdlib>

#define REP(i,n) for(int i=0; i<(n); ++i)

template<typename T> inline void checkmin(T &a, T b){ if(a>b) a = b; }
template<typename T> inline void checkmax(T &a, T b){ if(a<b) a = b; }

enum { n_max = 24, m_max = 100, N_max = 100000000 };

typedef unsigned int ui;

std::mt19937 rnd(87682736);

struct In
{
  ui n,m;
  ui N[n_max];
  ui M[m_max];

  void read()
  {
    scanf("%d%d",&n,&m);
    REP(i,n) scanf("%u",N+i);
    REP(i,m) scanf("%u",M+i);
  }

  void gen(int _n = -1, int _m = -1)
  {
    n = _n==-1 ? rnd()%10+1 : _n;
    m = _m==-1 ? rnd()%10+1 : _m;
    REP(i,n) N[i] = rnd()%N_max+1;
    REP(i,m) M[i] = rnd()%N_max+1;
  }

  void print()
  {
    printf("%d %d\n",n,m);
    REP(i,n) printf("%u ",N[i]); puts("");
    REP(i,m) printf("%u ",M[i]); puts("");
  }
};

enum { SH = 27 };

ui D[1<<n_max];

int go(const In &I)
{
  //puts("go");
  int M[m_max]; REP(i,I.m) M[i] = I.M[i];
  std::sort(M,M+I.m,std::greater<int>());
  int m = std::min(I.m,I.n); M[m++] = 0;
  
  ui T[m_max]; // tresholds
  REP(i,m) T[i] = ui(i)<<SH|M[i];
  //REP(i,m) printf("T%d: %u:%u\n",i,T[i]>>SH,T[i]<<5>>5);
  ui INF = T[m-1];
  int dh = 1<<I.n;
  REP(d,1<<I.n) D[d] = INF; D[0] = 0;
  REP(d,dh) if(D[d]<INF) REP(i,I.n) if(!(d>>i&1))
  {
    int t = D[d]>>SH;
    ui v = D[d]+I.N[i];
    if(v<=T[t]) checkmin(D[d|1<<i],v);
    else if(I.N[i]<=M[t+1])
      checkmin(D[d|1<<i],t+1<<SH|I.N[i]);
  }
  //REP(d,1<<I.n) printf("%d: %u:%u\n",d,D[d]>>SH,D[d]<<5>>5);
  ui res = D[(1<<I.n)-1];
  return res==INF ? -1 : (res>>SH)+1;
}

int brute(const In &I)
{
  //puts("brute");
  std::vector<ui> A(I.N,I.N+I.n);
  std::vector<ui> C(I.M,I.M+I.m);
  std::sort(A.begin(),A.end());
  std::sort(C.begin(),C.end(),std::greater<int>());
  int res = I.m;
  while(1)
  {
    int ai = 0, ci = 0;
    while(ai<I.n && ci<I.m)
    {
      int c = 0;
      while(ai<I.n && c<=C[ci]) c += A[ai++];
      if(c>C[ci]){ ai--; ci++; }
    }
    checkmin(res,ci);
    if(!std::next_permutation(A.begin(),A.end())) break;
  }
  return res>=I.m ? -1 : res+1;
}

void test()
{
  REP(_,5)
  {
    puts("max data test");
    In I; I.gen(24,100);
    go(I);
  }

  for(int c=0;;c++)
  {
    In I; I.gen();
    int s = go(I);
    int b = brute(I);
    if(s!=b)
    {
      printf("ERROR s=%d; b=%d\n",s,b);
      I.print();
      exit(1);
    }
    printf("OK %d\n",c);
  }
}

int main()
{
  //test();
  In I; I.read();
  int res = go(I);
  if(res==-1) puts("NIE"); else printf("%d\n",res);
  return 0;
}