Keeping the Dogs Out
Time limit2sMemory limit512 MB
Given counts of square stones with power-of-two sides, decide whether all stones can tile a gap-free rectangle, and output one valid length and height or report impossible.
Problem
Your best friend keeps telling you about her neighbour's annoying dogs. Apparently, they constantly lay waste to her garden and leave smelly "gifts". Today, she has decided to start working on a solution: building a wall to keep them out.
Of course the wall must have the same height in all places (i.e. be rectangular), and it cannot have any holes that would let the dogs through. Your friend does not really care about the wall's exact dimensions, though.
She has already bought the stones she plans to use, and she wants to use all of them. She has sorted them by size. All of them have the same width, but the length and height may vary. For each stone, its length is equal to its height, and both are a power of two. The width of the wall must be equal to the width of the stones. You may not rotate the stones, and you may not put two stones behind each other.
Given the side lengths and quantities of the stones, determine whether it is possible to build a wall using all available stones, and if so, what length and height such a wall might have.

Figure K.1: Illustration of the first sample.
Input
The first line contains an integer (). The largest stone has a side length of .
The second line contains integers ( for each , ), where is the number of stones with side length .
The combined area of all stones is at most .
Output
If it is possible to build a rectangular wall without any holes using all the stones, output one line with two integers: the length and height of a possible wall. Otherwise, output impossible. If multiple solutions exist, output any of them.