All problems

Problem 473

Maximum Swap

Medium · 20 pts · Math, Greedy

Problem

You may swap two digits of a non-negative integer at most once (or not at all). Print the largest number you can get. The best swap moves the largest possible digit as far left as possible: find the leftmost digit that has a larger digit somewhere to its right, and swap it with the last occurrence of the largest such digit.

Input

A single integer N.

Output

Print the largest number obtainable with at most one swap.

Constraints

0 <= N <= 100000000

Example input

2736

Example output

7236

Explanation

In 2736, swapping the 2 and the 7 gives 7236, the largest number reachable with one swap.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Wiggle Subsequence Next: Monotone Increasing Digits