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
#include <cstdio>
#include <iostream>
#include <set>
#include <algorithm>
#include <iomanip>
#include <cassert>

#define REP(i, n) for(int i = 0; i < n; i++)
#define FWD(i, a, b) for(int i = a; i < b; i++)
#define ALL(u) (u).begin(), (u).end()

using namespace std;

typedef pair<int,int> PII;
typedef long long LL;
typedef uint uint;

const int INF = 1000000000;

struct Length {
    int sack, fill;
    static Length inf;
    bool operator<(Length a) const {
        if (sack != a.sack) return sack < a.sack;
        return fill < a.fill;
    }
    bool operator==(Length a) const {
        return sack == a.sack and fill == a.fill;
    }
};

Length Length::inf = Length({INF, INF});

class Sol {
    vector<int> items, sacks;

    Length add(Length a, int size) {
        if (a == Length::inf) {
            return Length::inf;
        }
        assert(a.sack < (int)sacks.size());
        if (sacks[a.sack] >= a.fill + size) {
            return Length({a.sack, a.fill + size});
        }
        if (a.sack+1 >= (int)sacks.size()) {
            return Length::inf;
        }
        if (size > sacks[a.sack + 1]) {
            return Length::inf;
        }
        return Length({a.sack+1, size});
    }

    string to_bin(uint mask) {
        string res = "";
        for(int i = 0; i < (int)items.size(); i++) {
            res += string(1, '0' + mask%2);
            mask /= 2;
        }
        reverse(ALL(res));
        return res;
    }

    vector<Length> lookup;

    uint genbase() {
        return (((uint)1)<<(items.size())) - 1;
    }

    int popcount(uint val) {
        return __builtin_popcount(val);
    }

    uint next(uint val) {
        //uint valold = val;
        int cnt = 0;
        while(val&1) {
            val >>= 1;
            cnt++;
        }
        val <<= cnt;
        uint res = (val & (val - 1));
        uint one = ((val ^ (val - 1)) + 1) >> 2;
        REP(i, cnt+1) {
            res |= (one >> i);
        }
        //printf("next to: %s is %s\n", to_bin(valold).c_str(), to_bin(res).c_str());
        return res;
    }

    uint gen_first(int bits) {
        uint res = 0;
        for(int i = 0; i < bits; i++) {
            res |= (((uint)1)<<(items.size() - 1 - i));
        }
        return res;
    }

    uint gen_last(int bits) {
        return (((uint)1) << bits) - 1;
    }

    public:
    void sol() {
        int n, m;
        scanf("%d %d", &n, &m);
        REP(i, n) {
            int val;
            scanf("%d", &val);
            items.emplace_back(val);
        }
        REP(i, m) {
            int val;
            scanf("%d", &val);
            sacks.emplace_back(val);
        }
        sort(ALL(sacks), greater<int>());
        lookup.resize(1<<items.size(), Length::inf);
        lookup[0] = Length({0, 0});
        /*
        int mask = gen_first(2);
        int last_mask = gen_last(2);
        int limit = 0;
        while (true) {
            printf("cur_mask: %s last_mask: %s\n", to_bin(mask).c_str(), to_bin(last_mask).c_str());
            if (mask == last_mask) break;
            mask = next(mask);
            limit++;
            if (limit == 10) break;
        }
        return;
        */
        for(int bits = 0; bits < (int)items.size(); bits++) {
            uint mask = gen_first(bits);
            uint last_mask = gen_last(bits);
            while (true) {
                if (not (lookup[mask] == Length::inf)) {
                    for(int i = 0; i < n; i++) {
                        //printf("wut\n");
                        if ((mask & (((uint)1)<<i)) == 0) {
                            lookup[mask | (((uint)1)<<i)] = min(lookup[mask | (((uint)1)<<i)], add(lookup[mask], items[i]));
                        }
                    }
                }
                //printf("last_mask: %s\n", to_bin(last_mask).c_str());
                if (mask == last_mask) break;
                mask = next(mask);
            }
        }
        Length res = lookup[genbase()];
        if (res == Length::inf) {
            printf("NIE\n");
        } else {
            printf("%d\n", res.sack + 1);
        }
    }
};

int main() {
    Sol s;
    s.sol();
    return 0;
}