Mixing dyes
Statement
A dyeing workshop has N dyes, numbered 1 through N. Each dye has two values, a strength and a cloudiness.
The owner picks one or more of the N dyes and mixes them in one vat. The same dye is never put in twice. The strength and the cloudiness of the mixture are settled as follows.
1) The strength of the mixture is the product of the strengths of the dyes picked. 2) The cloudiness of the mixture is the sum of the cloudinesses of the dyes picked.
A good color comes out when the strength and the cloudiness are close to each other. So the owner wants the strength and the cloudiness of the mixture to come as close together as possible.
Write a program that goes through every way of picking dyes and finds the smallest absolute difference between the strength and the cloudiness of the mixture. At least one dye has to be picked.
For example, suppose N=4 and the strength and the cloudiness of each dye are as in the table below.
| Dye number | 1 | 2 | 3 | 4 |
|---|---|---|---|---|
| Strength | 5 | 3 | 4 | 2 |
| Cloudiness | 8 | 6 | 2 | 1 |
There are 15 ways to pick dyes here. Seven of them are in the table below.
| Dyes picked | Strength of the mixture | Cloudiness of the mixture | Difference |
|---|---|---|---|
| Dye 1 | 5 | 8 | 3 |
| Dye 3 | 4 | 2 | 2 |
| Dye 4 | 2 | 1 | 1 |
| Dyes 1 and 2 | 15 | 14 | 1 |
| Dyes 3 and 4 | 8 | 3 | 5 |
| Dyes 1, 2 and 3 | 60 | 16 | 44 |
| Dyes 1, 2, 3 and 4 | 120 | 17 | 103 |
Read the fourth row of the table. Dyes 1 and 2 are mixed, so the strength is 5 times 3, which is 15, and the cloudiness is 8 plus 6, which is 14. The difference between the two is 1. Going through all 15 ways, the smallest difference is 1, so the answer in this example is 1.
Input and Output Examples
Example 1
Input
4 5 8 3 6 4 2 2 1
Output
1
Example 2
Input
1 727 421
Output
306
Example 3
Input
2 64 7207 433 7318
Output
6885
Input
First the number of dyes N is given as a natural number. N is a natural number from 1 to 10.
Next the information on each dye is given over N lines. The i-th line holds the strength and the cloudiness of dye i, separated by a space. The strength is a natural number from 1 to 1,000, and the cloudiness is a natural number from 1 to 1,000,000.
Also, however the dyes are picked, the product of the strengths of the dyes picked never goes over 100,000,000,000.
Output
Print the smallest absolute difference between the strength and the cloudiness of the mixture on the first line, as a natural number.
Only one newline follows the number you print. Do not put a space before or after the number, and do not add a blank line after that newline.
Requirements
- At least one dye has to be picked
- The same dye is never put in twice
- The strength of the mixture is the product of the strengths of the dyes picked
- The cloudiness of the mixture is the sum of the cloudinesses of the dyes picked
- The difference is worked out as an absolute value
- You must write a program that reads from standard input and writes to standard output
- Trailing spaces at the end of a line and blank lines at the end of the output are ignored when grading
- The languages you can use are Python and JavaScript
- The memory limit is 256MB. The browser does not measure it, it only states it
- The time limit is set by measuring the setter's reference solution on the same runtime
- On this problem you submit code, not a prompt. That code runs in the browser on this device, and all that goes to the server is a fingerprint of the output and the time it took. Whether it is correct is decided by the server. The run time is a number each device reports, so the code and the device details are posted next to it on the leaderboard.
- The output has to match the expected value down to the character. A space inside a line counts the same as any other character.
- There are 15 cases, and 3 of them show the input together with the expected output. The rest are used only when you submit and are not shown on this page.
- You have to pass all 15 cases to solve this.
Examples
3 public cases. Judging also uses hidden cases. You have to pass all 15 cases to solve this.
1 727 421
306
2 64 7207 433 7318
6885
4 5 8 3 6 4 2 2 1
1
Solution
Your code runs on this device. Only the fingerprint of the output and the elapsed time go to the server. The server decides whether it is correct.
Submitting requires you to log in. Running against the public cases works without one.
You can test with 3 public cases. Submitting runs the hidden cases too.
People who solved this problem
The time shown is the time spent solving. Input reading time is outside the solver's control, so it is excluded. And since each device reports this value itself, we record which device measured it.
Log in and you can see the code once you solve this problem.