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
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <cstdlib>
#include <cassert>
#include <cmath>
#include <cstdarg>
using namespace std;
#define PB push_back
#define IT iterator
#define MP make_pair
#define EB emplace_back
#define F first
#define S second
#define X first
#define Y second
#define NL printf("\n")
#define SZ(v) ((int)(v).size())
#define ALL(x) (x).begin(), (x).end()
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define FORE(i,a,b) for(int i=(a);i<=(b);++i)
#define FORD(i,a,b) for(int i=(a);i>=(b);--i)
#define FORC(i,a) for(__typeof((a).begin()) i = (a).begin(); i != (a).end(); ++i)
#ifdef DBG
#define W(x) do { printf("%s:%d %s = %d\n", __func__, __LINE__, #x, x); } while (0)
#define DBGprint(format, ...) do { fprintf(stderr, "[%s:%d] " format "\n", __func__, __LINE__, __VA_ARGS__); } while (0)
#else
#define W(X) do { } while (0)
#define DBGprint(...) do { } while (0)
#endif

typedef long double LD;
typedef double DL;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<LD,LD> PLD;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<PII> VPII;
void memcheck()
{
#ifdef DBG
    char c[100];
    FILE *p = fopen("/proc/self/status","r");
    while (fgets(c,100,p) )
        if (strncmp("VmPeak",c,6)==0)
            fputs(c, stderr);
    fclose(p);
#endif
}
//------ /headers --------

int items, backpacks;
int item[27];
int backpack[102];
int min_bckpcks = -1;

int fits() {
    REP(i, items) {
        DBGprint("%d: %d", i, item[i]);
    }
    int items_it = items - 1;
    LL curr_fill = 0;
    for (int i = backpacks - 1; i >= 0;) {
        if (min_bckpcks != -1 && backpacks - i >= min_bckpcks) {
            return min_bckpcks;
        }
        if (items_it < 0) {
            return backpacks - i;
        }
        DBGprint("b_it: %d i_it: %d curr_fill: %lld", i, items_it, curr_fill);
        if (curr_fill + item[items_it] <= backpack[i]) {
            curr_fill += item[items_it];
            --items_it;
        } else {
            --i;
            curr_fill = 0;
        }
        DBGprint(" === b_it: %d i_it: %d curr_fill: %lld", i, items_it, curr_fill);
    }
    return -1;
}

int main() {
    scanf("%d%d", &items, &backpacks);
    REP(i, items) {
        scanf("%d", item + i);
    }
    REP(i, backpacks) {
        scanf("%d", backpack + i);
    }
    sort(item, item + items);
    sort(backpack, backpack + backpacks);
    do {
        int tmp = fits();
        DBGprint("returned: %d", tmp);
        if (min_bckpcks == -1) {
            min_bckpcks = tmp;
        } else if (tmp > 0) {
            min_bckpcks = min(min_bckpcks, tmp);
        }
    } while (next_permutation(item, item + items));
    if (min_bckpcks == -1) {
        printf("NIE\n");
    } else {
        printf("%d\n", min_bckpcks);
    }
}