Fingerpainting Paint Kits
Time limit1sMemory limit128 MB
Given per-color and gray amounts and a kit of N colors, find the fewest kits whose colors can cover the demand plus produce the gray by mixing triples.
- Level
Medium6 of 10
- Topics
- Greedy, Implementation, Brute force
- Solved
- No attempts yet
Problem
The local toy store sells fingerpainting kits. Each kit contains between three and twelve bottles of paint, one bottle per color, and every bottle holds ml of its color. All kits are identical: the same set of colors, ml of each.
These paints have a handy property: if you mix ml each of any three different colors, you get exactly ml of gray. The paint is thick and dense, so mixing does not increase the volume — it just gets denser, turning ml of colored paint into ml of gray. None of the base colors is gray, and the only way to make gray is to mix three distinct colors; it does not matter which three.
Emily runs a fingerpainting project with her class every Friday. Given the number of colors in a kit, how much of each color is needed, and how much gray is needed, compute the minimum number of kits required.
Input
The input consists of one or more test cases, followed by a line containing only a single that marks the end of the input. Each test case is one line of five or more space-separated integers. The first integer () is the number of colors in a kit. It is followed by integers, each between and , giving the amount of each color needed. The last integer () is the amount of gray needed. All amounts are in ml.
Output
For each test case, print on its own line the smallest number of kits sufficient to provide the required amount of every color and of gray. Because all grays are considered equal, obtaining the minimum may require making gray from several different combinations of three distinct colors.