#K35307. Organize Books by Digit Sum
Organize Books by Digit Sum
Organize Books by Digit Sum
You are given a list of book IDs along with an integer N indicating how many books there are. Each book ID is a positive integer. Your task is to sort the list of book IDs in ascending order based on the digit sum of the ID. The digit sum is defined as the sum of all digits in the number. In case two book IDs have the same digit sum, they should be sorted by their numerical value in ascending order.
Note: You should read from standard input and output the result to standard output. The input will first contain an integer N on a single line, followed by a line containing N space-separated integers. The output should be a single line with the sorted book IDs separated by a single space.
inputFormat
The first line contains an integer N (1 <= N <= 10^5), the number of book IDs. The second line contains N space-separated positive integers representing the book IDs. Each book ID is between 1 and 10^9.
outputFormat
Output a single line containing the N book IDs sorted primarily by the sum of their digits and secondarily by their value in ascending order, separated by a single space.## sample
5
340 14 123 59 32767
14 123 340 59 32767
</p>