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
#include <iostream>

using namespace std;


long numItem;
long numBag;
long * items;
long * bags;
long itemsSum;

void print(long * data, long size)
{
	for (long i=0; i<size; i++)
	{
		cout << data[i] << " ";
	}
	cout << "\n";
}


void sort(long * data, long size)
{
	for (long i=0; i<size-1; i++)
	{
		for (long j=0; j<size-i-1; j++)
		{
			if (data[j] < data[j+1])
			{
				long temp = data[j];
				data[j] = data[j+1];
				data[j+1] = temp;
			}
		}
	}
}

long getItemSum()
{
	long res = 0;
	for (long i=0; i<numItem; i++)
	{
		res += items[i];
	}
	return res;
}

long getBagSum(long size)
{
	long res = 0;
	for (long i=0; i<size; i++)
	{
		res += bags[i];
	}
	return res;
}



long * copyArray(long * in, long size)
{
	long * out = new long[size];
	for (long i=0; i<size; i++)
	{
		out[i] = in[i];
	}
	return out;
}

long isPos(long * in, long size, long itemC)
{
	if (itemC >= numItem) return 1;
	long res = 0;
	long * newData;
	//print(in, size);
	for (long i=0; i<size; i++)
	{
		if (items[itemC] <= in[i])
		{
			
			newData = copyArray(in, size);
			newData[i] = newData[i] - items[itemC];
			//print(newData, size);
			sort(newData, size);
			res = isPos(newData, size, itemC + 1);
		}
		if (res == 1) break;
	}
	return res;
}

void getAnswer()
{
	long bagCounter = 1;
	long * bagSizes;
	long res = 0;
	for (long i=0; i<numBag; i++,bagCounter++)
	{
		//cout << getBagSum(bagCounter) << "\n";
		if (getBagSum(bagCounter) < itemsSum) continue;
		bagSizes = new long[bagCounter];
		for (long k=0; k<bagCounter; k++)
		{
			bagSizes[k] = bags[k];
		}
		res = isPos(bagSizes,bagCounter, 0);
		if (res == 1) 
		{
			//cout << getBagSum(bagCounter) << "\n";
			break;
		}
	}
	if (res == 0) cout << "NIE\n"; else cout << bagCounter;
}

int main()
{
	cin >> numItem;
	cin >> numBag;
	items = new long[numItem];
	bags = new long[numBag];
	for (long i=0; i<numItem; i++)
	{
		cin >> items[i];
	}
	for (long i=0; i<numBag; i++)
	{
		cin >> bags[i];
	}
	itemsSum = getItemSum();
	sort(items, numItem);
	sort(bags, numBag);
	//print(items, numItem);
	//print(bags, numBag);
	getAnswer();
	return 0;
}