The Club Hall

Time limit2sMemory limit512 MB

Summary
Given plank lengths and a rectangular hall, decide whether each row can be spanned by one or two planks and find the fewest planks that cover the floor.
Level

Medium7 of 10

Topics
Greedy, Sorting, Two pointers, Implementation
Solved
No attempts yet

Problem

The Tinguá Recreation Club is building its new clubhouse. The members want the floor of the clubhouse hall to be made of wooden planks, because they consider it the best kind of floor for the club's famous dances. A local lumber mill donated a large number of good-quality planks for the floor. All donated planks have the same width, but their lengths differ.

The hall floor is rectangular. The planks must be laid side by side, with no part of one plank lying on top of another, and they must cover the whole floor of the hall. They must be laid in straight rows along their length, and all of them must point the same way (that is, all planks are parallel lengthwise). The members also do not want many joints in the floor, so if a plank is not long enough to span the distance from one wall of the hall to the opposite wall, it can be joined to at most one other plank to complete the distance. So every row is either one plank whose length equals a side of the hall, or two planks whose lengths add up to that side.

There is one more complication. The head carpenter has great respect for all wood and prefers not to saw any plank. He wants to know whether the whole floor can be covered with the donated planks while obeying these rules. If it can, he also wants to know the smallest number of planks needed.

The figure below shows two possible ways to cover the floor of a 4 × 5 meter hall with a set of ten donated planks, each 100 cm wide, with lengths 1, 2, 2, 2, 2, 3, 3, 4, 4, and 5 meters.

Input

The input contains several test cases. The first line of a test case contains two integers MM and NN, the dimensions of the hall in meters (1≤N,M≤1041 \le N, M \le 10^4). The second line contains an integer LL, the width of the planks in centimeters (1≤L≤1001 \le L \le 100). The third line contains an integer KK, the number of donated planks (1≤K≤1051 \le K \le 10^5). The fourth line contains KK integers XiX_i separated by single spaces, each the length of one plank in meters (1≤Xi≤1041 \le X_i \le 10^4 for 1≤i≤K1 \le i \le K).

The end of the input is marked by a line containing only two zeros separated by a single space.

Output

For each test case, print a single line with the smallest number of planks needed to cover the whole floor of the hall while obeying the rules. If the whole floor cannot be covered while obeying the rules, print a line with the word impossivel (lowercase, no accent).

Examples1

  1. Example 1

    Input
    4 5
    100
    10
    1 2 2 2 2 3 3 4 4 5
    5 4
    100
    7
    4 5 4 4 4 4 3
    4 5
    99
    4
    4 4 4 4
    3 2
    100
    7
    2 4 1 4 2 4 4
    0 0
    
    Expected output
    7
    5
    impossivel
    impossivel