Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Change a Password

Password authentication is used in a lot of facilities. The office of JAG also uses password authentication. A password is required to enter their office. A password is a string of $N$ digits '0'-'9'. This password is changed on a regular basis. Taro, a staff of the security division of JAG, decided to use the following rules to generate a new password from an old one.

The new password consists of the same number $N$ of digits to the original one and each digit appears at most once in the new password. It can have a leading zero. (Note that an old password may contain same digits twice or more.)

The new password maximizes the difference from the old password within constraints described above. (Definition of the difference between two passwords is described below.)

If there are two or more candidates, the one which has the minimum value when it is read as an integer will be selected.

The difference between two passwords is defined by min($|a - b|, 10^N - |a - b|$), where $a$ and $b$ are the integers represented by the two passwords. For example, the difference between "11" and "42" is 31, and the difference between "987" and "012" is 25.

Taro would like to use a computer to calculate a new password correctly, but he is not good at programming. Therefore, he asked you to write a program. Your task is to write a program that generates a new password from an old password.

Input

The input consists of a single test case. The first line of the input contains a string $S$ which denotes the old password. You can assume that the length of $S$ is no less than 1 and no greater than 10. Note that old password $S$ may contain same digits twice or more, and may have leading zeros.

Output

Print the new password in a line.

Sample Input 1

201

Output for the Sample Input 1

701

Sample Input 2

512

Output for the Sample Input 2

012

Sample Input 3

99999

Output for the Sample Input 3

49876

Sample Input 4

765876346

Output for the Sample Input 4

265874931

Solution in C# (judge verdict: Accepted)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using static System.Math;
public static class P
{
    static long UB;
    static long N;
    public static void Main()
    {
        string s = Console.ReadLine();
        N = long.Parse(s);
        int[] canUse = Enumerable.Repeat(1, 10).ToArray();
        var dig = (long)Math.Pow(10, s.Length - 1);
        UB = dig * 10;

        Console.WriteLine(Solve(0, dig, canUse).ToString().PadLeft(s.Length, '0'));
    }

    static long GetDiff(long a, long b) => Min(Abs(a - b), UB - Abs(a - b));

    static long Solve(long num, long currentDig, int[] canUse)
    {
        if (currentDig == 0) return num;

        List<Tuple<long, int>> reses = new List<Tuple<long, int>>();
        for (int i = 0; i < 10; i++)
        {
            if (canUse[i] <= 0) continue;
            var diff = GetDiff(N, num + i * currentDig);
            reses.Add(new Tuple<long, int>(diff, i));
        }

        reses.Sort();reses.Reverse();
        long finalDiff = 0;
        long res = -1;

        Action<Tuple<long, int>> update = item =>
        {
            var i = item.Item2;

            var nextCanUse = canUse.ToArray();
            nextCanUse[i]--;
            var curRes = Solve(num + i * currentDig, currentDig / 10, nextCanUse);
            var diff = GetDiff(N, curRes);
            if (diff > finalDiff || (diff == finalDiff && curRes < res))
            {
                finalDiff = diff;
                res = curRes;
            }
        };

        foreach (var item in reses.OrderByDescending(x => x.Item1).ThenBy(x => x.Item2).Take(3))
        {
            update(item);
        }

        return res;
    }
}



struct Top2<T> where T : IComparable
{
    public T First;
    public T Second;
    public Top2(T first, T second) { First = first; Second = second; }
    public static Top2<T> Merge(Top2<T> a, Top2<T> b)
    {
        if (a.First.CompareTo(b.First) > 0)
            return new Top2<T>(a.First, a.Second.CompareTo(b.First) > 0 ? a.Second : b.First);
        else
            return new Top2<T>(b.First, b.Second.CompareTo(a.First) > 0 ? b.Second : a.First);
    }
    public static Top2<T> Merge(Top2<T> a, T b)
    {
        if (a.Second.CompareTo(b) > 0) return a;
        if (a.First.CompareTo(b) > 0) return new Top2<T>(a.First, b);
        return new Top2<T>(b, a.First);
    }
    public static Top2<T> operator |(Top2<T> a, Top2<T> b) => Merge(a, b);
    public static Top2<T> operator |(Top2<T> a, T b) => Merge(a, b);
}