#C12717. Largest Number Concatenation

    ID: 42175 Type: Default 1000ms 256MiB

Largest Number Concatenation

Largest Number Concatenation

Given a list of non-negative integers, arrange them in such a way that they form the largest possible number when concatenated. For example, given the numbers [3, 30, 34, 5, 9], the largest number that can be formed is 9534330.

The challenge is to determine an ordering of the integers such that the concatenated result is maximal.

Note: If the resulting number has a leading zero, the final answer should simply be "0" (e.g., input [0, 0, 0] should output 0).

The solution involves using a custom comparator which compares two numbers a and b by checking whether ab is larger than ba (concatenated as strings). Use LaTeX to represent any formulas if needed; however, the key idea remains the custom ordering.

inputFormat

Input is provided via stdin. The first line contains an integer n representing the number of integers. The second line contains n non-negative integers separated by spaces.

For instance:

5
3 30 34 5 9

outputFormat

Output the largest number (as a string) that can be formed by concatenating the given integers. The output should be printed to stdout.

Examples:

9534330
## sample
5
3 5 1 4 2
54321