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
#include <iostream>
#include <algorithm>

using namespace std;

int itemsCount, packsCount;
long int items[25];
long int packs[101];
long long packed[25];
int currentPacks = 0;
bool answer = false;

bool insert( int pack, int item )
{
//    cout << " . ins item[" << item << "]: " << items[item] << " pack[" << pack << "] " << packs[pack];

    if ( (long long int)packed[pack] + items[item] > packs[pack] )
    {
//        cout << " false" << endl;
        return false;
    }

//    cout << " ok" << endl;

    if ( packed[pack] == 0 )
    {
        ++currentPacks;
//        cout << "+ " << currentPacks << endl;
    }

//    cout << " . was: " << packed[pack] << endl;

    packed[pack] += items[item];

//    cout << " . is:  " << packed[pack] << endl;

    return true;
}

void remove( int pack, int item )
{
    packed[pack] -= items[item];
    if ( packed[pack] == 0 )
    {
        --currentPacks;
//        cout << "- " << currentPacks << endl;
    }
}

void go(int startingItem)
{
    if ( startingItem >= itemsCount )
    {
        packsCount = min(packsCount, currentPacks);
        answer = true;
        return;
    }

    for ( int j = 0; j < packsCount; ++j )
    {
//        cout << endl;
//        cout << " exec " << startingItem << " " << j << endl;

        if ( insert(j, startingItem) )
        {
            go( startingItem + 1 );
            remove(j, startingItem);
        }
    }
}

int main()
{
    cin >> itemsCount >> packsCount;

    for ( int i = 0; i < 25; ++i)
        packed[i] = 0;

    for ( int i = 0; i < itemsCount; ++i )
    {
        cin >> items[i];
    }

    for ( int i = 0; i < packsCount; ++i )
    {
        cin >> packs[i];
    }

    sort(packs, packs + packsCount, greater<int>() );
    sort(items, items + itemsCount, greater<int>() );

    packsCount = min(itemsCount, packsCount);

//    cout << "plecaki" << endl;
//    for ( int i = 0; i < packsCount; ++i )
//    {
//        cout << " " << packs[i];
//    }
//    cout << endl;

//    cout << "itemy" << endl;
//    for ( int i = 0; i < itemsCount; ++i )
//    {
//        cout << " " << items[i];
//    }
//    cout << endl;

    go(0);

    if ( answer )
        cout << packsCount << endl;
    else
        cout << "NIE" << endl;

    return 0;
}