Hide

Problem H
In-place Sorting

/problems/inplacesorting/file/statement/en/img-0001.png
CC BY-SA 4.0 by Balu Ertl on Wikimedia

Woe is you – for your algorithms class you have to write a sorting algorithm, but you missed the relevant lecture! The subject was in-place sorting algorithms, which you deduce must be algorithms that leave each input number in its place and yet somehow also sort the sequence.

Of course you cannot change any of the numbers either, then the result would just be a different sequence. But then it hits you: if you flip a 6 upside-down, it becomes a 9, and vice versa! Certainly no one can complain about this since you changed none of the digits! The deadline to hand in the exercise is in five hours. Try to implement this sorting algorithm before then!

Input

The input consists of:

  • A line with an integer $n$ ($2 \leq n \leq 10\, 000$), the number of integers in the input sequence.

  • $n$ lines, the $i$th of which contains a positive integer $x_i$ ($1\leq x_i \leq 10^{18}$), the $i$th number of the sequence.

Output

If the sequence cannot be sorted in non-decreasing order by flipping some of the digits $6$ or $9$ in the input1, output “impossible”. Otherwise, output “possible” followed by the sorted sequence – each number on its own line.

If there are multiple valid solutions, you may output any one of them.

Sample Input 1 Sample Output 1
4
9
7
7
9
possible
6
7
7
9
Sample Input 2 Sample Output 2
4
97
96
66
160
possible
67
69
99
190
Sample Input 3 Sample Output 3
3
80
97
79
impossible
Sample Input 4 Sample Output 4
2
197
166
possible
167
169

Footnotes

  1. Flipping any of the digits of $n$ is not allowed.

Please log in to submit a solution to this problem

Log in