#C9398. Sort by Absolute Value

    ID: 53486 Type: Default 1000ms 256MiB

Sort by Absolute Value

Sort by Absolute Value

You are given a list of integers. Your task is to sort the list in ascending order according to the absolute values of the numbers, while retaining the original sign of each integer.

Note: The input will first provide an integer n, the number of integers in the list, followed by a single line of n space-separated integers. The output should be the sorted list, with the numbers separated by a space.

Mathematical formulation: Given an array \(a = [a_1, a_2, \dots, a_n]\), you need to output a permutation \(b = [b_1, b_2, \dots, b_n]\) such that \[ |b_i| \leq |b_j| \quad \text{for all } i < j, \] while each \(b_i\) is exactly one of the original numbers (including its sign).

inputFormat

The input is read from standard input (stdin). The first line contains an integer n representing the number of integers. The second line contains n space-separated integers.

outputFormat

Print the sorted list of integers on one line. The numbers should be separated by a single space, sorted in ascending order by their absolute values.## sample

1
1
1