# Algorithm to find maximum number of items that can be removed from buckets with set of condition

I am looking for algorithm to rather complex problem and I am wondering if this is some well known problem or can be translated to one.

Let's say that i have buckets B1, B2, B3 that contain some number of colored balls (red and green) and set of conditions K1=(1 green, 2 red), K2=(2 green, 1 red), K3=(2 green, 1 red) and NK1=1, NK2=1, NK3=3. This conditions says that K1 is a set of 1 green and 2 red balls and that we need 1 such set (NK1=1) and that set need to be in one bucket (we cannot get balls for K1 from different buckets, but if NK1=2 for e.g then we can get one set from one bucket and another from another bucket) etc. Now for e.g. we have in bucket B1 1 green and 2 red balls, in B2 2 green and 2 red and in bucket B3 2 green and 1 red. The question is what is the maximum number of balls of each color that we can pull out of the each bucket so that given conditions are still met. In this example we cannot pull out any green balls and we can pull out one red ball from bucket B2. Then 1 K1 is in B1, 1 K2 is in B2 or B3 and 1 K3 is in B2 or B3 but in other bucket than K2.

In reality I need to solve this problem for arbitrary number of buckets with balls in arbitrary number of colors and arbitrary numbers of conditions (conditions will contain small number of colors but there will be large number of different colors in total) and in SQL (but SQL part is not that important for now).

• Can balls be used more than once? For example, if I have a bucket with two red balls and two green, can I satisfy one (1 green, 2 red) and one (2 green, 1 red) with that bucket alone? – David Richerby Jul 5 '17 at 0:26
• No, you can use balls only one time. – abc667 Jul 5 '17 at 6:27