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 <vector>
#include <algorithm>
using namespace std;
int LEVEL_MAX, LAST;
vector <int> tab[2], plecaki, koszt;
char blocked[  70000000 ];
char maski[ 35000005 ];
inline int rozdzielnik( int s, int k, int nr, int level){
    int e = s, f = k, l = k+1;
    while (e <= f){
        int mid = (e+f)/2;
        if (tab[nr-1][mid] & (1 << LEVEL_MAX - 1 - level)){
            l = mid;
            if (f == mid){
                f--;
            }
            else {
                f = mid;
            }
        }
        else {
            if (e == mid)
                e++;
            else
                e = mid;
        }
    }
    return l;
}
bool resolve_tree( int pos, int s1, int e1, int s2, int e2, int level ){
    if (level == LEVEL_MAX){
        blocked[ pos ] += 1;
        return true;
    }
    else {
        int r1 = rozdzielnik( s1, e1, 1, level), r2 = rozdzielnik( s2, e2, 2, level);
        //printf("%d %d %d %d %d %d %d\n", pos, s1, e1, s2, e2, r1, r2);
        if (!( blocked[ pos ] & (1 << 0)) ){
            if ((r1 > s1) && (r2 > s2)){
                bool t = resolve_tree( pos*2, s1, r1-1, s2, r2-1, level+1);
                if (t){
                    blocked[ pos ] += 1;
                }
            }
        }
        if (!( blocked[ pos ] & (1 << 1)) ){
            if ((r1 <= e1) && (r2 > s2)){
                bool t = resolve_tree( pos*2+1, r1, e1, s2, r2-1, level+1);
                if (t){
                    blocked[ pos ] += 2;
                }
            }
        }
        if (!( blocked[ pos ] & (1 << 1)) ){
            if ((r2 <= e2) && (r1 > s1)){
                bool t = resolve_tree( pos*2+1, s1, r1-1, r2, e2, level+1);
                if (t){
                    blocked[ pos ] += 2;
                }
            }
        }
        if (blocked[ pos ] == 3){
            return true;
        }
        else {
            return false;
        }
    }
}
void generuj(){
    int mask = 0;
    while (mask < LAST){
        long long k = 0;
        for (int i = 0; i < LEVEL_MAX; i++){
            if (mask & (1 << i)){
                k += koszt[i];
            }
        }
        for (int i = 0; i < plecaki.size(); i++){
            if (k <= plecaki[i]){
                maski[ mask ] = i+1;
                break;
            }
        }
        mask++;
    }
}
void read_data(){
    tab[0].clear();
    tab[1].clear();
    tab[0].reserve( LAST );
    for (int i = LAST; i < 2*LAST; i++){
        if (blocked[ i ] == 1){
            tab[0].push_back( i - LAST );
        }
    }
    for (int i = 0; i < 2*LAST; i++){
       blocked[ i ] = 0;
    }
}
int main(){
    int n,m;
    scanf("%d%d",&n,&m);
    LEVEL_MAX = n;
    int pot = 1;
    for (int i = 0; i < n; i++){
        pot *= 2;
    }
    LAST = pot;
    for (int i = 0; i < LAST; i++){
        maski[ i ] = 105;
    }
    for (int i = 0; i < n; i++){
        int a;
        scanf("%d",&a);
        koszt.push_back(a);
    }
    for (int i = 0; i < m; i++){
        int a;
        scanf("%d", &a);
        plecaki.push_back(a);
    }
    if (m > n){
        m = n;
    }
    sort( plecaki.begin(), plecaki.end() );
    tab[0].push_back( 0 );
    generuj();
    for (int i = (int)plecaki.size()-1; i >= (int)plecaki.size() - m; i--){
        tab[1].reserve( LAST );
        for (int j = 0; j < LAST; j++){
            if (maski[ j ] <= i+1){
                tab[ 1 ].push_back( j );
            }
        }
        resolve_tree( 1, 0, tab[0].size()-1, 0, tab[1].size()-1, 0);
        read_data();
        if ( tab[0][ tab[0].size()-1 ] == LAST-1 ){
            printf("%d\n", plecaki.size() - i);
            return 0;
        }
    }
    printf("NIE\n");
}