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;

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

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

				cerr << out( j ) << out( curr ) << endl;

				if( chkmax( currMx, curr ) ) indMx = j;
			}
		}
		nas.push_back( currMx );
		ind[ indMx ]++;
	}

	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: 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;
}