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
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

vector<int> readVector(int n) {
    vector<int> vec(n);
    for (int i = 0; i < n; ++i) {
        scanf("%i", &vec[i]);
    }
    sort(vec.begin(), vec.end(), greater<int>());
    return vec;
}

vector<int> items;
vector<int> bags;
vector<bool> set;

void genSet(int bagSize, int mask = 0, int i = 0) {
    if (i >= items.size()) {
        set[mask] = true;
        return;
    }

    if (items[i] <= bagSize) {
        genSet(bagSize - items[i], mask | (1 << i), i + 1);
    }
    genSet(bagSize, mask, i + 1);
}

int main() {
    int n, m;
    scanf("%i %i", &n, &m);

    items = readVector(n);
    bags = readVector(m);

    const int spaceSize = 1 << n;
    vector<bool> prevSet(spaceSize, false);
    vector<bool> nextSet;
    prevSet[0] = true;

    for (int i = 0; i < bags.size(); ++i) {
        set.clear();
        set.resize(spaceSize, false);
        genSet(bags[i]);

        nextSet.clear();
        nextSet.resize(spaceSize, false);
        for (int u = 0; u < spaceSize; ++u) {
            for (int v = 0; v < spaceSize; ++v) {
                if (prevSet[u] && set[v]) {
                    nextSet[u | v] = true;
                }
            }
        }

        if (nextSet[spaceSize - 1]) {
            printf("%i", i + 1);
            return 0;
        }

        nextSet.swap(prevSet);
    }

    puts("NIE");
    return 0;
}