This page is still under construction.

Parts of this page are still being built. What you see may change.

Proportional Representation

Time limit5sMemory limit128 MB

Summary
Given total votes and seats each party won under the D'Hondt rule, find the smallest and largest vote count each party could have received.
Level

Medium7 of 10

Topics
Binary search, Math
Solved
No attempts yet

Problem

JAG Kingdom elected the members of its parliament. The country uses only party-list proportional representation: each citizen votes for one party, and the number of seats a party wins is proportional to the number of votes it receives. The parliament has an integer number of seats, so an exactly proportional split is usually impossible. The kingdom splits the seats with the D'Hondt method.

Every party has an unlimited supply of candidates, and the candidates of a party are ordered. The yy-th candidate of a party that received xx votes gets the value xy\dfrac{x}{y}. All candidates are sorted by value in decreasing order, and the first TT candidates win, where TT is the total number of seats. The number of seats a party wins is the number of its winning candidates.

Take three parties with 4040, 6060 and 3030 votes as an example. With T=9T = 9 seats the first party wins 33 seats, the second wins 44 seats, and the third wins 22 seats.

Ties are broken by lottery, so every tied candidate has a chance to win. In the same example with T=5T = 5, two candidates tie at the value 2020, and both of the following outcomes are possible.

  • The first party wins 22 seats, the second wins 22 seats, and the third wins 11 seat.
  • The first party wins 11 seat, the second wins 33 seats, and the third wins 11 seat.

You have just heard the results of the election on TV. You know the total number of valid votes and the number of seats each party won, and you wonder how many votes each party received.

You are given the total number of valid votes NN, the number of parties MM, and the number of seats SiS_i that party ii won. For each party, determine the minimum and the maximum number of votes it could have received. For some inputs no vote count produces the given seats at all.

Input

The first line contains two integers NN (1≤N≤1091 \le N \le 10^9) and MM (1≤M≤300001 \le M \le 30000), the total number of valid votes and the number of parties. Each of the next MM lines contains one integer SiS_i (0≤Si≤300000 \le S_i \le 30000), the number of seats party ii won. At least one SiS_i is not zero.

Output

If no vote count produces the given NN, MM and SiS_i, print impossible. Otherwise print MM lines. The ii-th line contains two integers, the minimum and the maximum number of votes party ii could have received.

Examples5

  1. Example 1

    Input
    10 2
    2
    1
    
    Expected output
    5 7
    3 5
    
  2. Example 2

    Input
    5 6
    0
    2
    0
    2
    6
    0
    
    Expected output
    0 0
    1 1
    0 0
    1 1
    3 3
    0 0
    
  3. Example 3

    Input
    2000 5
    200
    201
    202
    203
    204
    
    Expected output
    396 397
    397 399
    399 401
    401 403
    403 404
    
  4. Example 4

    Input
    15 10
    1
    1
    1
    1
    1
    1
    1
    1
    1
    13
    
    Expected output
    impossible
    
  5. Example 5

    Input
    1000000000 9
    12507
    16653
    26746
    21516
    29090
    10215
    28375
    21379
    18494
    
    Expected output
    67611619 67619582
    90024490 90033301
    144586260 144597136
    116313392 116323198
    157257695 157269050
    55221291 55228786
    153392475 153403684
    115572783 115582561
    99976756 99985943