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
/* 2014
 * Maciej Szeptuch
 * II UWr
 */
#include <cstdio>
#include <algorithm>

using namespace std;

int objects,
    backpacks,
    object[32],
    backpack[128];

void solve(void);

int main(void)
{
    scanf("%d %d", &objects, &backpacks);
    for(int o = 0; o < objects; ++ o)
        scanf("%d", &object[o]);

    for(int b = 0; b < backpacks; ++ b)
        scanf("%d", &backpack[b]);

    sort(object, object + objects);
    sort(backpack, backpack + backpacks);
    reverse(backpack, backpack + backpacks);

    solve();
    return 0;
}

inline
void solve(void)
{
    int need = 0;
    for(int o = 0, b = 0; o < objects; ++ o, b = 0)
    {
        while(b < backpacks && backpack[b] < object[o])
            ++ b;

        if(b >= backpacks)
        {
            puts("NIE");
            return;
        }

        backpack[b] -= object[o];
        need = max(need, b);
    }

    printf("%d\n", need + 1);
    return;
}