Under construction

Still being built. Things may change

Keeping the Dogs Out

Time limit2sMemory limit512 MB

Summary
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.
Level

Medium6 of 10

Topics
Math, Greedy
Solved
No attempts yet

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 nn (0≤n≤250 \le n \le 25). The largest stone has a side length of 2n2^n.

The second line contains n+1n + 1 integers m0,…,mnm_0, \ldots, m_n (0≤mi≤10150 \le m_i \le 10^{15} for each ii, mn≥1m_n \ge 1), where mim_i is the number of stones with side length 2i2^i.

The combined area of all stones is at most 101510^{15}.

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.

Examples2

  1. Example 1

    Input
    2
    21 3 3
    
    Expected output
    9 9
    
  2. Example 2

    Input
    2
    0 3 2
    
    Expected output
    impossible