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
#include <stdio.h>
#include <stdlib.h>

static int cmpint(const void *a, const void *b) {
        return *((int *)b) - *((int *)a);
}

int main(void) {
        int n,m,i,j,u, *v, *p;
        scanf("%d%d", &n, &m);
        v = malloc(n*sizeof(*v));
        p = malloc(m*sizeof(*p));
        for (i=0; i<n; ++i)
                scanf("%d", v+i);
        for (i=0; i<m; ++i)
                scanf("%d", p+i);

        qsort(v, n, sizeof(*v), cmpint);
        qsort(p, m, sizeof(*v), cmpint);

        u = -1;
        for (i=0; i<n; ++i) {
                for (j=0; j < m && v[i] >= p[j]; ++j);
                if (j==m) {
                        printf("NIE\n");
                        return 0;
                }
                p[j] -= v[i];
                if (u<j) u = j;
        }

        printf("%d\n", u+1);

        return 0;
}