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

Previous submission (verdict: Time limit exceeded on test 8):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_updat
using namespace std;
using namespace chrono;
using namespace __gnu_pbds;
template<class T> using oset = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
#define ll long long
void dbg_out() { cerr << endl; }
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) {
  cerr << ' ' << H;
  dbg_out(T...);
}
#ifndef ONLINE_JUDGE
#define deb(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define deb(...)
#endif
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define sortall(x) sort(all(x))
#define forEach(arr) for (auto &x : arr)
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
template <typename A, typename B>
ostream &operator<<(ostream &cout, pair<A, B> const &p) { return cout << "(" << p.F << ", " << p.S << ")"; }
template <typename A>
ostream &operator<<(ostream &cout,vector<A> const &v){cout<<"[";for (int i = 0; i < v.size(); i++){if(i) cout<<", ";cout << v[i];} return cout << "]";}
template <typename A>
ostream &operator<<(ostream &cout,set<A> const &s){cout<<"[";for (auto &x: s){cout << s;} return cout << "]";}
template <typename A, typename B>
ostream &operator<<(ostream &cout,unordered_map<A, B> const &v){cout<<"{";for(auto &x: v){cout<<"{"<<x.first<<"="<<x.second<<"}";} return cout << "}";}
template <typename A>
istream &operator>>(istream &cin, vector<A> &v) {for (auto &i : v) cin >> i; return cin;}

const int mod = 1e9+7;
//=======================

void solve() {
  string s; cin>>s;
  int n = s.length();
  string ans;
  for(int i=0; i<n; ) {
    if(s[i] == '#') {
      i++; continue;
    }
    int j = i+1, maxValIndex = i, maxVal = s[i]-'0', hashCnt = 0;
    while(j-i-hashCnt <= 9 and j < n) {
      if(s[j] == '#') {
        hashCnt++; j++;
        continue;
      }
      int currVal = s[j]-'0' - (j-i) + hashCnt;
      if(currVal > maxVal) {
        maxVal = currVal; 
        maxValIndex = j;
      }
      deb(i,j, currVal, maxVal, maxValIndex);
      j++;
    }
    s[maxValIndex] = '#';
    ans += (maxVal + '0');
  }
  cout<<ans;
}


int32_t main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("error.txt", "w", stderr);
  freopen("output.out", "w", stdout);
  freopen("input.in", "r", stdin);
#endif
  auto start1 = high_resolution_clock::now();
  int t = 1;
  cin >> t;
  while (t--) {
    solve();
    cout << "\n";
    deb("tc_end\n");
  }
  auto stop1 = high_resolution_clock::now();
  auto duration = duration_cast<microseconds>(stop1 - start1);
#ifndef ONLINE_JUDGE
  cerr << "Time: " << duration.count()<<"ms"<< endl;
#endif
  return 0;
}


Submission (verdict: Accepted):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_updat
using namespace std;
using namespace chrono;
using namespace __gnu_pbds;
template<class T> using oset = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
#define ll long long
void dbg_out() { cerr << endl; }
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) {
  cerr << ' ' << H;
  dbg_out(T...);
}
#ifndef ONLINE_JUDGE
#define deb(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define deb(...)
#endif
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define sortall(x) sort(all(x))
#define forEach(arr) for (auto &x : arr)
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
template <typename A, typename B>
ostream &operator<<(ostream &cout, pair<A, B> const &p) { return cout << "(" << p.F << ", " << p.S << ")"; }
template <typename A>
ostream &operator<<(ostream &cout,vector<A> const &v){cout<<"[";for (int i = 0; i < v.size(); i++){if(i) cout<<", ";cout << v[i];} return cout << "]";}
template <typename A>
ostream &operator<<(ostream &cout,set<A> const &s){cout<<"[";for (auto &x: s){cout << s;} return cout << "]";}
template <typename A, typename B>
ostream &operator<<(ostream &cout,unordered_map<A, B> const &v){cout<<"{";for(auto &x: v){cout<<"{"<<x.first<<"="<<x.second<<"}";} return cout << "}";}
template <typename A>
istream &operator>>(istream &cin, vector<A> &v) {for (auto &i : v) cin >> i; return cin;}

const int mod = 1e9+7;
//=======================

void solve() {
  string s; cin>>s;
  int n = s.length();
  string ans;
  map<int, char> idx;
  for(int i=0; i<n; i++) idx[i] = s[i];
  for(int i=0; i<n; ) {
    if(s[i] == '#') {
      i++; continue;
    }
    auto it = idx.lower_bound(i+1);
    int maxValIndex = i, maxVal = s[i]-'0', covered = 1;
    while(covered <= 9 and it != idx.end()) {
      int currVal = (*it).second-'0' - covered;
      if(currVal > maxVal) {
        maxVal = currVal; 
        maxValIndex = (*it).first;
      }
      deb(i, currVal, maxVal, maxValIndex);
      it++; covered++;
    }
    s[maxValIndex] = '#';
    idx.erase(maxValIndex);
    ans += (maxVal + '0');
  }
  cout<<ans;
}


int32_t main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("error.txt", "w", stderr);
  freopen("output.out", "w", stdout);
  freopen("input.in", "r", stdin);
#endif
  auto start1 = high_resolution_clock::now();
  int t = 1;
  cin >> t;
  while (t--) {
    solve();
    cout << "\n";
    deb("tc_end\n");
  }
  auto stop1 = high_resolution_clock::now();
  auto duration = duration_cast<microseconds>(stop1 - start1);
#ifndef ONLINE_JUDGE
  cerr << "Time: " << duration.count()<<"ms"<< endl;
#endif
  return 0;
}