Problem: 2050D - Digital string maximization
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>
#include <string>
#include <vector>
using namespace std;
//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")

#define endl "\n"

template<class T, class T2> inline bool chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline bool chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }

#ifndef LOCAL
#define cerr if(false) cerr
#endif
#define out( x ) #x << " = " << x << "  "

template<typename T, typename S> ostream& operator << (ostream &os, const pair<T, S> &p) {
	return os << "(" << p.first << ", " << p.second << ")";
}

template<typename T, typename B = decay<decltype(*begin(declval<T>()))>, typename enable_if<!is_same<T, string>::value>::type* = nullptr>
ostream& operator <<(ostream &os, const T &c) {
	bool f = false;
	os << "(";
	for(const auto &x : c) {
		if(f) {
			os << ", ";
		}
		f = true;
		os << x;
	}return os << ")";
}

typedef long long ll;
const ll mod = 1e9 + 7;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//

const int MAX_N = 1e6 + 10;

int ind[16];
std::vector< int > pos[16];

void solve() {

	std::string s;
	std::cin >> s;

	int n = (int)s.size();
	for( int i=0 ; i < n ; i++ ){

		int curr = s[i] - '0';
		pos[curr].push_back( i );
	}


	std::vector< int > nas;
	for( int i=0 ; i < n ; i++ ){
		int currMx = -1;
		int indMx = -1;

		int mn = 10;
		int mnInd = 10;
		for( int j=0 ; j < 10 ; j++ ){
			if( ind[j] < (int)pos[j].size() ){
				if( pos[j][ ind[j] ] <= i ) if( chkmin( mnInd, ind[j] ) ) mn = j;
			}
		}

		for( int j=0 ; j < 10 ; j++ ){

			if( ind[j] < (int)pos[j].size() ){
				int curr = -1;
				if( pos[j][ ind[j] ] > i ) curr = j - ( pos[j][ind[j]] - i );

				cerr << out( j ) << out( curr ) << endl;
				if( chkmax( currMx, curr ) ) indMx = j;
			}
		}


		cerr << out( currMx ) << out( mn ) << endl;
		if( currMx > mn ){
			nas.push_back( currMx );
			ind[ indMx ]++;
		}else{
			nas.push_back( mn );
			ind[ mnInd ] ++;
		}
	}

	cerr << out( nas ) << endl;

	for( auto j : nas ) std::cout << j;
	std::cout << endl;

	for( int i = 0 ; i < 10 ; i++ ){
		pos[i].clear();
		ind[i] = 0;
	}

}

signed main() {
#ifndef LOCAL
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#endif

	int t = 1;
	cin >> t;
	while(t --) {
		solve();
	}

	return 0;
}


Submission (verdict: Accepted):
#include <bits/stdc++.h>
#include <vector>
using namespace std;
//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")

#define endl "\n"

template<class T, class T2> inline bool chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline bool chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }

#ifndef LOCAL
#define cerr if(false) cerr
#endif
#define out( x ) #x << " = " << x << "  "

template<typename T, typename S> ostream& operator << (ostream &os, const pair<T, S> &p) {
	return os << "(" << p.first << ", " << p.second << ")";
}

template<typename T, typename B = decay<decltype(*begin(declval<T>()))>, typename enable_if<!is_same<T, string>::value>::type* = nullptr>
ostream& operator <<(ostream &os, const T &c) {
	bool f = false;
	os << "(";
	for(const auto &x : c) {
		if(f) {
			os << ", ";
		}
		f = true;
		os << x;
	}return os << ")";
}

typedef long long ll;
const ll mod = 1e9 + 7;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//

const int MAX_N = 1e6 + 10;

void solve() {

	std::string s;
	cin >> s;

	int n = (int)s.size();
    for (int i = 0; i < n ; i++) {
        int mx = s[i] - '0', intMx = i;
        for (int j = i; j < std::min(i + 10, n ); j++) {
            if (s[j] - '0' - (j - i) > mx) {
                mx = s[j] - '0' - (j - i);
                intMx = j;
            }
        }
        while (intMx > i) {
            swap(s[intMx], s[intMx - 1]);
            intMx--;
        }

        s[i] = char(mx + '0');
    }

	cerr << out( s ) << endl;

	for( auto j : s ) std::cout << j ;
	std::cout << endl;

}

signed main() {
#ifndef LOCAL
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#endif

	int t = 1;
	cin >> t;
	while(t --) {
		solve();
	}

	return 0;
}