#K50907. Order Weight

    ID: 28968 Type: Default 1000ms 256MiB

Order Weight

Order Weight

You are given a string of space-separated non-negative integers. Your task is to reorder the numbers according to the following rules:

  1. Compute the sum of digits for each number.
  2. Sort the numbers in increasing order of the sum of digits.
  3. If two numbers have the same sum of digits, sort them lexicographically (as strings).

For example, given the input:

56 65 74 100 99 180 90

The digit sums are:

  • 56 -> 11
  • 65 -> 11
  • 74 -> 11
  • 100 -> 1
  • 99 -> 18
  • 180 -> 9
  • 90 -> 9

After sorting by the sum of digits and then lexicographically (if needed), the correct order is:

100 180 90 56 65 74 99

Write a program that reads the input from standard input and writes the correct sorted order to standard output.

The mathematical sorting criteria can be expressed as follows: [ \text{order}(a,b) = \begin{cases} \text{if } s(a) < s(b) \quad a \text{ comes before } b,\ \text{if } s(a) = s(b) \quad \text{compare } a \text{ and } b \text{ lexicographically.} \end{cases} ] where ( s(x) ) denotes the sum of digits of string ( x ).

inputFormat

The input consists of a single line which contains space-separated non-negative integers. The input may be an empty string.

outputFormat

Output a single line containing the sorted order of the numbers, space-separated. If the input is empty, output an empty string.## sample

56 65 74 100 99 180 90
100 180 90 56 65 74 99