Problem: 2050D - Digital string maximization
Language: java

Solution 1 (runtime: 983 ms):
import java.io.*;
import java.util.*;
public class Main
{

    public static void main(String[] args) throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw = new PrintWriter(System.out);
        int t = Integer.parseInt(br.readLine());
        char[] string;
        TreeSet<Integer> unused;
        ArrayList<Integer> used;
        while(t-- > 0)
        {
            char[] c = br.readLine().toCharArray();
            unused = new TreeSet<>();
            string = new char[c.length];
            for(int i = 0; i < c.length; i++)
            {
                unused.add(i);
            }
            for(int i = 0; i < c.length; i++)
            {
                int max = -1;
                int index = -1;
                int size = unused.size();
                used = new ArrayList<>();
                for(int j = 0; j < Math.min(size, 10); j++)
                {
                    int a = unused.pollFirst();
                    if(c[a] - 48 - j > max)
                    {
                        index = a;
                        max = c[a] - 48 - j;
                    }
                    used.add(a);
                }
                unused.addAll(used);
                unused.remove(index);
                string[i] = (char)(max + 48);
            }
            pw.println(new String(string));
        }
        pw.close();

    }
}

Solution 2 (runtime: 234 ms):
import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.StringTokenizer;



public class round991 {

static class Pair implements Comparable<Pair>{

int a;

long b;

String str;



Pair(int a,long b,String str){

this.a=a;

this.b=b;

this.str=str;

}



public int compareTo(Pair o) {

return Integer.compare(this.a, o.a); // Ascending order by a

}

//

//            public int compareTo(Pair o){

//                return Long.compare(this.b,o.b);

//            }

//

//            public int compareTo(Pair other) {

//                return this.str.compareTo(other.str); // Ascending order by str

//            }

//

}

static class FastReader {

BufferedReader br;

StringTokenizer st;



public FastReader() {

br = new BufferedReader(

new InputStreamReader(System.in));

}



String next() {

while (st == null || !st.hasMoreElements()) {

try {

st = new StringTokenizer(br.readLine());

} catch (IOException e) {

e.printStackTrace();

}

}

return st.nextToken();

}



int nextInt() {

return Integer.parseInt(next());

}



long nextLong() {

return Long.parseLong(next());

}



double nextDouble() {

return Double.parseDouble(next());

}



String nextLine() {

String str = "";

try {

if (st.hasMoreTokens()) {

str = st.nextToken("\n");

} else {

str = br.readLine();

}

} catch (IOException e) {

e.printStackTrace();

}

return str;

}

}

static ArrayList<Integer>[] gr;

public static void main(String[] args) {

FastReader sc = new FastReader();

PrintWriter out=new PrintWriter(System.out);

int t = sc.nextInt();

while (t != 0) {

t--;

String str=sc.next();

int[] arr=new int[str.length()];

for (int i = 0; i < arr.length; i++) {

arr[i]=str.charAt(i)-'0';

}

StringBuilder ans=new StringBuilder("");

for (int i = 0; i < arr.length; i++) {

int dig=arr[i];

int j=i-1;

while (j>=0 && arr[j]<dig-1){

arr[j+1]=arr[j];

--dig;

arr[j]=dig;

--j;

}

//                out.println(st);

}

//            out.println(Arrays.toString(arr));

for (int num:arr) {

ans.append(""+num);

}

out.println(ans);

}

out.flush();

}

}