#C3403. Construct the Largest Number

    ID: 46827 Type: Default 1000ms 256MiB

Construct the Largest Number

Construct the Largest Number

You are given a string s consisting of digits (0-9). Your task is to form the largest possible integer by concatenating a subset of these digits, with the condition that no digit appears more than once in the final number.

In other words, remove any duplicate occurrences and sort the unique digits in descending order. Then, by concatenating them, output the resulting number. Note that if the input contains only zeros, the output should be 0 (not multiple zeros).

Example:

  • Input: 012345 → Unique digits: 0,1,2,3,4,5 → Sorted descending: 5,4,3,2,1,0 → Output: 543210
  • Input: 3021 → Unique digits: 0,1,2,3 → Sorted descending: 3,2,1,0 → Output: 3210
  • Input: 00000 → Unique digits: 0 → Output: 0

The solution involves basic string manipulation and sorting algorithms.

inputFormat

The input consists of a single line containing a non-empty string of digits (0-9).

outputFormat

Output a single integer representing the largest number that can be formed by concatenating the unique digits from the input, sorted in descending order. The integer must not have any leading zeros (except when the result is 0).## sample

012345
543210