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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <stdio.h>
#include <stdlib.h>

#define N_ITEMS 24
#define N_PACKS 100

int item_mass[N_ITEMS];
int pack_vol[N_PACKS];

int item_in_pack[N_ITEMS];
int pack_mass[N_PACKS];

int n_of_items;
int n_of_packs;

int is_item_in_pack(int n)
{
        return (item_in_pack[n] >= 0 && item_in_pack[n] < n_of_packs) ? 1 : 0;
}

int get_pack_of_item(int n)
{
        return item_in_pack[n];
}

void print_items()
{
        int i;
        
        printf("%d", item_in_pack[0]);
        for(i = 1; i < n_of_items; ++i) {
                printf(" %d", item_in_pack[i]);
        }
        printf("\n");
}

void init()
{
        int i;

        for(i = 0; i < n_of_items; ++i) {
                item_in_pack[i] = -1;
        }
        for(i = 0; i < n_of_packs; ++i) {
                pack_mass[i] = 0;
        }
}

int remove_item(int n)
{
        int p;

        p = item_in_pack[n];
        item_in_pack[n] = -1;
        if (p >= 0) {
                pack_mass[p] -= item_mass[n];
        }
        return p;
}

int is_pack_empty(int p)
{
        return (pack_mass[p] == 0) ? 1 : 0;
}

void put_item(int n, int p)
{
        item_in_pack[n] = p;
        pack_mass[p] += item_mass[n];
}

int item_fits_pack(int n, int p)
{
        return (pack_mass[p] + item_mass[n] <= pack_vol[p]) ? 1 : 0;
}

long long int calc_sum(int *a, int n)
{
        int i;
        long long int res;

        res = 0;
        for(i = 0; i < n; ++i) {
                res += a[i];
        }
        return res;
}

void print_t(int *a, int n)
{
        int i;

        printf("%d", a[0]);
        for(i = 1; i < n; ++i) {
                printf(" %d", a[i]);
        }
        printf("\n");
}

/*
static int int_cmp_asc(const void *a, const void *b)
{
        int aa = *(const int *) a;
        int bb = *(const int *) b;

        return aa - bb;
}
*/

static int int_cmp_desc(const void *a, const void *b)
{
        int aa = *(const int *) a;
        int bb = *(const int *) b;

        return bb - aa;
}


int check(int max_n_packs)
{
        int k;
        int p;

        k = 0;
        init();
        while (k >= 0 && k < n_of_items) {
                p = remove_item(k);
                p++;
                while (p < max_n_packs) {
                        if (item_fits_pack(k, p)) {
                                put_item(k, p);
                                k++;
                                break;
                        }
                        if (is_pack_empty(p)) {
                                p = max_n_packs;
                        } else {
                                p++;
                        }
                }
                if (p == max_n_packs) {
                        k--;
                }
//                print_items();
        }
        return (k == n_of_items) ? 1 : 0;
}

int main()
{
        int i;
        long long int sum_items, sum_vol, sum;
        int smallest_n;

        scanf("%d %d\n", &n_of_items, &n_of_packs);
        for(i = 0; i < n_of_items; ++i) {
                scanf("%d", &item_mass[i]);
        }
        for(i = 0; i < n_of_packs; ++i) {
                scanf("%d", &pack_vol[i]);
        }

        sum_items = calc_sum(item_mass, n_of_items);
        sum_vol = calc_sum(pack_vol, n_of_packs);

        if (sum_items > sum_vol) {
                printf("NIE\n");
                return 0;
        }

        qsort( &item_mass, n_of_items, sizeof(int), int_cmp_desc );
        qsort( &pack_vol, n_of_packs, sizeof(int), int_cmp_desc );

//        print_t(item_mass, n_of_items);
//        print_t(pack_vol, n_of_packs);
//        printf("sum items %lld\n", sum_items);
//        printf("sum packs %lld\n", sum_vol);

        if (item_mass[0] > pack_vol[0]) {
                printf("NIE\n");
                return 0;
        }

        if (sum_items <= pack_vol[0]) {
                printf("1\n");
                return 0;
        }

        sum = 0;
        for(i = 0; i < n_of_packs; ++i) {
                sum += pack_vol[i];
                if (sum_items <= sum) {
                        break;
                }
        }
        smallest_n = i+1;
        //printf("smallest possib n %d\n", smallest_n);
        for(i = smallest_n; i <= n_of_packs; ++i) {
                if (check(i)) {
                        printf("%d\n", i);
                        break;
                }
        }
        if (i > n_of_packs) {
                printf("NIE\n");
        }

        return 0;
}