Fingerpainting Paint Kits

Time limit1sMemory limit128 MB

Summary
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 5050 ml of its color. All kits are identical: the same set of colors, 5050 ml of each.

These paints have a handy property: if you mix XX ml each of any three different colors, you get exactly XX ml of gray. The paint is thick and dense, so mixing does not increase the volume — it just gets denser, turning 3X3X ml of colored paint into XX 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 00 that marks the end of the input. Each test case is one line of five or more space-separated integers. The first integer NN (3≤N≤123 \le N \le 12) is the number of colors in a kit. It is followed by NN integers, each between 00 and 10001000, giving the amount of each color needed. The last integer GG (0≤G≤10000 \le G \le 1000) 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.

Examples1

  1. Example 1

    Input
    3 40 95 21 0
    7 25 60 400 250 0 60 0 500
    4 90 95 75 95 10
    4 90 95 75 95 11
    5 0 0 0 0 0 333
    0
    
    Expected output
    2
    8
    2
    3
    4