Counting the seedling boxes
Statement
A nursery sells its seedlings in boxes. There are only two kinds of box, one holding 3 seedlings and the other holding 5. A box is never opened to sell a few seedlings on their own. There are plenty of boxes of both kinds, so any number of them can be bought.
The field owner wants to buy exactly N seedlings. Both kinds may be mixed, or just one kind bought, but the seedlings in the boxes have to add up to exactly N. The aim is to buy as few boxes as possible.
For example, suppose N=18 seedlings have to be bought. The ways to pick boxes are the following.
| Boxes of 5 | Boxes of 3 | Seedlings | Boxes |
|---|---|---|---|
| 0 | 6 | 18 | 6 |
| 3 | 1 | 18 | 4 |
Buying three boxes of 5 and one box of 3 gives 5 × 3 + 3 × 1 = 18 seedlings with only 4 boxes bought. No way buys fewer, so the answer in this case is 4.
Sometimes exactly N seedlings cannot be picked. Take N=4 seedlings. One box of 3 leaves one seedling short, and one box of 5 overshoots by one seedling. No mix of the two boxes makes 4 seedlings. The answer in such a case is -1.
Writing out a few small values of N gives the following.
| N | Boxes of 5 | Boxes of 3 | Boxes |
|---|---|---|---|
| 3 | 0 | 1 | 1 |
| 4 | not possible | not possible | -1 |
| 5 | 1 | 0 | 1 |
| 6 | 0 | 2 | 2 |
| 7 | not possible | not possible | -1 |
| 8 | 1 | 1 | 2 |
| 9 | 0 | 3 | 3 |
| 10 | 2 | 0 | 2 |
| 11 | 1 | 2 | 3 |
Only 4 seedlings and 7 seedlings cannot be made, and every value beyond those can. Buying fewer boxes calls for buying as many boxes of 5 as possible, since they hold more. Boxes of 5 on their own sometimes miss the right number of seedlings, though, so how many boxes of 3 to mix in has to be worked out as well.
Given the number of seedlings N that have to be bought, write a program that finds the smallest number of boxes that have to be bought. When exactly N seedlings cannot be made, find -1.
Input and output examples
Example 1
Input
18
Output
4
Example 2
Input
4
Output
-1
Example 3
Input
5000
Output
1000
Input
The first line gives the number of seedlings N the field owner has to buy. N is a natural number from 3 to 5,000.
The input ends with this one line. The 3 and the 5 that the boxes hold are not given in the input and are the same in every input. There are plenty of boxes of both kinds, so no number bought ever runs short.
N is never given as a value smaller than 3.
Output
Print the smallest number of boxes that have to be bought on the first line. When exactly N seedlings cannot be made, print -1.
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
- The seedlings in the boxes have to add up to exactly N. Nothing may be left over and nothing may run short
- Buying just one of the two kinds is allowed
- Find the way that buys the fewest boxes. When several ways land on the right number of seedlings, take the one with the fewest boxes
- When exactly N seedlings cannot be made, print -1
- 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.
4
-1
18
4
5000
1000
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.