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
#include <iostream>
#include <vector>
#include <assert.h>
#include <algorithm>
#include <limits>
#include <cstdint>
#include <string.h>

//#define DEBUG
#ifdef DEBUG
#define D(...) __VA_ARGS__
#else
#define D(...)
#endif

using namespace std;

vector<uint32_t> capacities, weights;

struct Combination
{
	uint32_t c;
	uint32_t weight;
};
vector<Combination> combinations;

int n, m;

void input()
{
	ios_base::sync_with_stdio(false);
	cin >> n >> m;
	assert(1 <= n);
	assert(n <= 24);
	assert(1 <= m);
	assert(m <= 100);
	weights.resize(n);
	capacities.resize(m);
	for(int i=0; i<n; i++)
	{
		cin >> weights[i];
		assert(1 <= weights[i]);
		assert(weights[i] <= 100000000);
	}
	for(int i=0; i<m; i++)
	{
		cin >> capacities[i];
		assert(1 <= capacities[i]);
		assert(capacities[i] <= 100000000);
	}
}

bool rcmp(uint32_t a, uint32_t b)
{
	return a > b;
}

bool cmp(uint32_t a, uint32_t b)
{
	return a < b;
}

bool cmp_combinations_by_weight(const Combination &a, const Combination &b)
{
	return a.weight < b.weight;
}

void prepare()
{
	sort(capacities.begin(), capacities.end(), &rcmp);
	if(m > n)
	{
		m = n;
		capacities.resize(m);
	}
	sort(weights.begin(), weights.end(), &cmp);
	combinations.resize(1<<n);
	for(size_t i=1; i<combinations.size(); i++)
	{
		int delta_bit = ffs(i)-1;
		combinations[i].c = i;
		combinations[i].weight = combinations[i & ~(1 << delta_bit)].weight + weights[delta_bit];
	}
	sort(combinations.begin(), combinations.end(), &cmp_combinations_by_weight);
}

uint32_t chosen = 0;

bool filter_chosen(const Combination &c)
{
	return (c.c & chosen) == 0;
}

size_t min_bags = numeric_limits<size_t>::max();

void go(int i, vector<Combination>::iterator fstart = combinations.begin()+1, vector<Combination>::iterator fend = combinations.end())
{
	if(i >= min_bags)
	{
//		D(cerr << "no point in going deeper" << endl;)
		return;
	}
	if(chosen == (1<<n)-1)
	{
		assert(i < min_bags);
		min_bags = i;
		D(cerr << "new min_bags: " << min_bags << endl;)
		return;
	}
	if(i >= capacities.size())
	{
		D(cerr << "finished all buckets" << endl;)
		return;
	}
	auto C = capacities[i];
	Combination x{0, C};
	uint32_t sum = 0;
	for(int x=0, a=1; x<n; x++, a<<=1)
	{
		if((chosen & a) == 0)
		{
			if(weights[x] > C)
			{
//				D(cerr << "item " << x << " (" << a << "), weight = " << weights[x] << ", won't fit anymore..." << endl;)
				return;
			}
			sum += weights[x];
		}
	}
	uint32_t c_to_go = 0, max_c = min(min_bags,capacities.size());
	for(int x=i; x<max_c; x++)
	{
		c_to_go += capacities[x];
	}
	if(sum > c_to_go)
	{
//		D(cerr << "bailing out earlier!" << endl);
		return;
	}

	;
	vector<Combination> filtered;
	if(i < 4) // so shame! such many awful!
	{
		copy_if(fstart, fend, back_inserter(filtered), [=](const Combination &c) -> bool { return (c.c & chosen) == 0; });
		fstart = filtered.begin();
		fend = filtered.end();
	}
	auto j = upper_bound(fstart, fend, x, &cmp_combinations_by_weight);
	bool chosen_any = false;
	for(j--; j >= fstart; j--)
	{
		if((j->c & chosen) == 0)
		{
			chosen_any = true;
			D(if(i<=5) cerr << i <<  " choosing " << j->c << " (" << j->weight << ") for " << C << endl;)
			chosen |= j->c;
			go(i+1, fstart, fend);
			chosen &= ~j->c;
		}
		else
		{
//			D(cerr << "not choosing " << j->c << " (" << j->weight << ") for " << C << endl;)
		}
	}
	if(!chosen_any)
	{
		D(cerr << "didn't choose any!" << endl;)
		D(abort();)
	}
}

int main(int argc, char **argv)
{
	input();
	prepare();
	go(0);
	if(min_bags == numeric_limits<size_t>::max())
	{
		cout << "NIE" << endl;
	}
	else
	{
		cout << min_bags << endl;
	}
	return 0;
}