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
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>	
#include <limits>
using namespace std;

vector<int> things;
vector<int> knapsacks;

void pack(int knapsackCapacity){
	int cols = knapsackCapacity+1;
	int rows = things.size()+1;
	int tab[rows][cols];	

	for (int i=0 ; i<rows ; i++){
		for (int j=0 ; j<cols ; j++){
			tab[i][j] = 0;
		}
	}

	for (int j = 1 ; j<=things.size() ; j++){
		for (int k=1 ; k<=knapsackCapacity ; k++){
			if (things[j-1]>k) tab[j][k] = tab[j-1][k];
			else tab[j][k] = max(tab[j-1][k], tab[j-1][k-things[j-1]]+things[j-1]);
		}
	}


	int l = knapsackCapacity;
	int i = things.size();
	vector<int> elem;

	while (i>0){
		if ((tab[i][l]-tab[i-1][l])==things[i-1]){
			elem.push_back(things[i-1]);
			i--;
		}else if (tab[i][l]>tab[i-1][l]){
			l--;
		}else {
			i--;
		}
	}

	//cout<<"elem size = "<<elem.size()<<endl; for (int i=0 ; i<elem.size() ; i++) cout<<elem[i]<<" ";
	//cout<<endl;
	//cout<<"things size = "<<things.size()<<endl;
	for (int i=0 ; i<elem.size() ; i++) things.erase(std::remove(things.begin(), things.end(), elem[i]), things.end());
	//cout<<"things size = "<<things.size()<<endl;
	
	/*for (int j = 0 ; j<rows ; j++){
		for (int k=0 ; k<cols ; k++){
			cout<<tab[j][k]<<" ";
		}
		cout<<endl;
	}*/
	//cout<<tab[things.size()-1][knapsackCapacity]<<endl;
}

int main(){
	int n, m;
	scanf("%d", &n); // liczba przedmiotow
	scanf("%d", &m); // liczba dostepnych plecakow
	
	int tmp;

	for (int i=0 ; i<n ; i++){
		scanf("%d", &tmp);
		things.push_back(tmp);
	}

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

	sort(knapsacks.begin(), knapsacks.end(), greater<int>());

	int number = 0;
	//cout<<"Things: "<<things.size()<<endl;
	while (things.size()!=0 && number < knapsacks.size()){
		pack(knapsacks[number]);
		number++;	
	}
	if (things.size()>0) {
		cout<<"NIE"<<endl;
		return 0;
	}
	/*for (int i=0 ; i<things.size() ; i++) cout<<things[i]<<" ";
	cout<<endl;

	for (int i=0 ; i<knapsacks.size() ; i++) cout<<knapsacks[i]<<" ";
	cout<<endl;*/
	cout<<number<<endl;
	return 0;
}