Baskets for the apples
Statement
An orchard ships its harvested apples in baskets. The orchard has N sizes of basket, and a basket of a given size holds a fixed number of apples. A basket is never left short and never overfilled. There are plenty of baskets of every size, so any number of baskets of the same size can be used.
Today exactly K apples have to be shipped. A few baskets are picked, and the numbers of apples they hold have to add up to exactly K. The aim is to use as few baskets as possible.
For example, suppose the N=10 basket sizes are the ones in the following table and K=4,790 apples have to be shipped.
| Basket | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
|---|---|---|---|---|---|---|---|---|---|---|
| Apples it holds | 1 | 5 | 10 | 50 | 100 | 500 | 1000 | 5000 | 10000 | 50000 |
Starting from the basket that holds the most and using as many as possible of each gives the following. The baskets that hold 50,000, 10,000 and 5,000 apples hold more than 4,790, so not one of them can be used.
| Apples it holds | 1000 | 500 | 100 | 50 | 10 | 5 | 1 |
|---|---|---|---|---|---|---|---|
| Baskets used | 4 | 1 | 2 | 1 | 4 | 0 | 0 |
| Apples left | 790 | 290 | 90 | 40 | 0 | 0 | 0 |
Adding up the baskets used gives 4 + 1 + 2 + 1 + 4 = 12. No way uses fewer, so the answer in this case is 12.
Let us see why starting from the basket that holds the most is best. In the example above, suppose only three baskets of 1,000 are used instead of four and the apples left over go into smaller baskets.
| Baskets of 1,000 used | Baskets for the apples left over | Total baskets |
|---|---|---|
| 4 | 8 | 12 |
| 3 | 10 | 13 |
Using one basket of 1,000 fewer means 1,000 more apples have to go into smaller baskets, so the number of baskets goes up instead. This always happens because the numbers of apples the baskets hold are multiples of one another. Without that condition, starting from the basket that holds the most does not give the smallest number.
Given the kinds of basket and the number of apples to ship, write a program that finds the smallest number of baskets that have to be used.
Input and output examples
Example 1
Input
10 4790 1 5 10 50 100 500 1000 5000 10000 50000
Output
12
Example 2
Input
1 11 1
Output
11
Example 3
Input
3 1234 1 2 4
Output
309
Input
The first line gives the number of kinds of basket N and the number of apples to ship K, separated by one space. N is a natural number from 1 to 10, and K is a natural number less than or equal to 100,000,000.
The next N lines give the number of apples each basket holds, one number per line. This value is a natural number from 1 to 100,000.
The numbers of apples the baskets hold are given from the smallest one in order. The smallest basket holds 1 apple.
And the number of apples a basket holds is a multiple of the number of apples any smaller basket holds.
Output
Print the smallest number of baskets that have to be used to hold exactly K apples on the first line.
Print one number and then a newline. Do not add any other character or space before or after it.
Requirements
- Read the input from standard input and write the answer to standard output
- Several baskets of the same size may be used. A size that is not used at all is allowed too
- The numbers of apples the baskets hold have to add up to exactly K
- The smallest basket holds 1 apple, so whatever K is, there is always a way to hold the apples
- The condition that the numbers of apples the baskets hold are multiples of one another is the heart of this problem. Without that condition, starting from the basket that holds the most does not give the smallest number
- Grading compares the output with the expected value down to the last character
- 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.
- 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 11 1
11
3 1234 1 2 4
309
10 4790 1 5 10 50 100 500 1000 5000 10000 50000
12
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
Nobody yet. You could be the first.