Problem 254
Largest Number
Medium · 20 pts · Array, String, Greedy, Sorting
Problem
You are given N non-negative integers. Arrange them in some order and write them one after another, without separators, to form the largest possible number. Print that number. If every value is 0, print a single 0 rather than a string of zeros. Sorting numerically does not work: 3 should come before 30 because 330 > 303. Instead sort with a custom comparison: x goes before y when the string x + y is larger than y + x.
Input
First line: N. Second line: N non-negative integers.
Output
Print the largest number.
Constraints
1 <= N <= 100; 0 <= A[i] <= 1000000000
Example input
5 3 30 34 5 9
Example output
9534330
Explanation
Comparing pairs as strings, 9 comes first, then 5, then 34 (since 343 > 334), then 3 (since 330 > 303), then 30. Writing them out gives 9534330.