Ice blocks
Statement
The freezer at a fish market holds an empty space shaped like a rectangular box. The space has length L, width W and height H, and all three values are natural numbers. The manager has to fill this space with ice blocks and leave no gaps.
An ice block is a cube, and its edge length is always a power of two. A block's edge therefore only takes values that come from multiplying 2 by itself, such as 1, 2, 4, 8, 16. A block has to go in with its three edges parallel to the three edges of the storage space, and it cannot be split or tilted. Blocks cannot overlap each other and cannot stick out of the storage space.
The number of blocks on hand is fixed for each edge length. Not all of the blocks on hand have to be used, and it is enough to fill the space so that not one gap is left. The aim is to use as few blocks as possible. There are cases where the blocks on hand cannot fill the space with no gaps, and in those cases the answer is -1.
Write a program that, given the three edge lengths of the storage space and the information about the blocks on hand, finds the smallest number of blocks needed to fill it with no gaps.
For example, suppose all three edges of the storage space are 10 and the blocks on hand are as in the following table.
| Edge length | 8 | 2 | 1 |
|---|---|---|---|
| Number on hand | 1 | 100 | 1000 |
The volume of this storage space is 10 times 10 times 10, which is 1,000. Putting one block with edge 8 into a corner fills 512, and the remaining 488 can be filled with no gaps by 61 blocks with edge 2. Writing out the result once all the blocks are in gives the following.
| Edge length | 8 | 2 | Total |
|---|---|---|---|
| Number placed | 1 | 61 | 62 |
| Volume filled | 512 | 488 | 1000 |
The volume of 1,000 was filled with no gaps and 62 blocks were used. Not one block with edge 1 was used. There is only one block with edge 8, so no larger block can be added, and leaving that block out would take 125 blocks with edge 2, which is more. The answer in this example is therefore 62.
If the storage space has all three edges equal to 2 and only 7 blocks with edge 1 are on hand, on the other hand, filling the volume of 8 would need 8 of those blocks, so it cannot be filled with no gaps. The answer in that case is -1.
Input and Output Examples
Example 1
Input
10 10 10 3 3 1 1 100 0 1000
Output
62
Example 2
Input
2 2 2 1 0 7
Output
-1
Example 3
Input
3 3 3 2 1 1 0 19
Output
20
Input
The length L, the width W and the height H of the storage space are given first, in that order on the first line, separated by one space. All three values are natural numbers from 1 to 1,048,575 (smaller than 2 to the power of 20).
The second line then holds the number of kinds of block on hand N. N is a natural number from 1 to 20.
From the third line through line N+2, the block information is given one line at a time. Each line holds an exponent i and a count b in that order, separated by one space, which means that b blocks with an edge length of 2 to the power of i are on hand. i is an integer from 0 to 19 and b is a natural number from 1 to 1,000. The same i is never given twice.
Output
Print the smallest number of blocks needed to fill the space with no gaps on the first line. If the blocks on hand cannot fill it with no gaps, print -1.
Print one number and then a newline. Do not attach any other character or space before or after it.
Requirements
- Read the input from standard input and write the answer to standard output
- A block's edge is a power of two, and the input gives it as an exponent i. The edge length is 2 to the power of i
- A block has to go in with its three edges parallel to the three edges of the storage space, and it cannot be split or tilted
- Not all of the blocks on hand have to be used. It is enough to fill the space with no gaps while keeping the count as small as possible
- If the blocks on hand cannot fill the space with no gaps, print -1
- The input has N+2 lines. Three numbers come on the first line, N on the second line, and N lines after that
- 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 16 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 16 cases to solve this.
Examples
3 public cases. Judging also uses hidden cases. You have to pass all 16 cases to solve this.
2 2 2 1 0 7
-1
3 3 3 2 1 1 0 19
20
10 10 10 3 3 1 1 100 0 1000
62
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.