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

using namespace std;

long long products[25];
int n;
vector<long long> backs;

vector<int> curPerm;

int check()
{
    //printf( "Check\n" );
    int i = 0;
    int j = 0;
    for( i = 0 ; i < backs.size(); ++i)
    {
        long long sum = 0;
        while( (j != n ) && ( sum + products[curPerm[j]] <= backs[i] ))
        {

            //printf( "Sum %d\n", sum );
            sum += products[curPerm[j]];
            j++;
        }
        if( j == n ) break;
    }

    if( j != n ) return 100;

    //printf( "%d\n", i );

    return i + 1;

}

int main()
{
    int m;
    scanf( "%d%d", &n, &m );
    for ( int i = 0; i < n; i++ )
    {
        scanf( "%d", &products[i] );
    }


    for ( int i = 0; i < m; i++ )
    {
        int a;
        scanf( "%d", &a );
        backs.push_back( -a );
    }

    sort( backs.begin(), backs.end() );

    while( backs.size() > 24 ) backs.pop_back();

    for( int i = 0 ; i < backs.size(); i++ )
    {
        backs[i] = -backs[i];
    }

    for( int i = 0 ; i < n; i++ )
        curPerm.push_back(i);

    int res = 100;

    do
    {
        res = min( res, check());
    } while( next_permutation( curPerm.begin(), curPerm.end() ) );

    if( res == 100 )
        printf( "NIE\n" );
    else
        printf( "%d\n", res );
    return 0;

}