Problem: 2050D - Digital string maximization
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 2):
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <limits>
#include <omp.h>
#include <numeric>
#include <set>
#include <queue>
#include <map>
#include <bitset>
#include <math.h>
#include <algorithm>
#include <string>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
using namespace std;

#define int long long
#define all(a) a.begin(),a.end()

const int MOD = 1e9 + 7;
const int INF = 1e9;

void fast() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
}

template <typename T>
istream& operator>>(istream& o, vector<T>& a) {
    for (int i = 0; i < a.size(); ++i) {
        o >> a[i];
    }
    return o;
}

template <typename T, typename Y>
istream& operator>>(istream& o, pair<T, Y>& p) {
    o >> p.first >> p.second;
    return o;
}

template <typename T, typename Y>
ostream& operator<<(ostream& o, pair<T, Y>& p) {
    o << p.first << " " << p.second << "\n";
    return o;
}

template <typename T>
ostream& operator<<(ostream& o, vector<T>& a) {
    for (int i = 0; i < a.size(); ++i) {
        o << a[i] << " ";
    }
    return o;
}

int bpow(int n, int k, int MOD) {
    if (k == 0) return 1;
    else if (k % 2 == 1) return bpow(n, k - 1, MOD) * n % MOD;
    return bpow(n * n % MOD, k / 2, MOD) % MOD;
}

int gcd(int a, int b) {
    
    if (a < b) swap(a, b);
    while (b != 0) {
        int residue = a % b;
        a = b;
        b = residue;
    }
    return a;
}

int lcm(int a, int b) {
    int h = gcd(a, b);
    return h ? (a * (b / h)) : 0;
}

vector<int> z_f(string& s) {
    int n = s.size();
    vector<int> z(n);
    int l = 0, r = 0;
    for (int i = 1; i < n; ++i) {
        z[i] = max((int)0, min(z[i - l], r - i));
        while (z[i] + i < n && s[z[i]] == s[z[i] + i]) z[i]++;
        if (i + z[i] > r) {
            r = i + z[i]; l = i;
        }
    }
    return z;
}


vector<bool> SieveOfEratosthenes(int n) {
    vector<bool> prime(n + 1, true);
    prime[0] = prime[1] = false;
    for (int i = 2; i <= n; i++) {
        if (prime[i]) {
            if (i * 1ll * i <= n) {
                for (int j = i * i; j <= n; j += i) prime[j] = false;
            }
        }
    }
    return prime;
}

set<int> primes;



void solve(int test)
{
    string s;
    cin >> s;
    string s1, s2;
    int knt = 0;
    for (int i = 0; i < s.size(); ++i)
    {
        if (s[i] == '0')
        {
            knt++;
            s1.push_back('0');
        }
        else
        {
            if (s[i] - '0' <= knt)
            {
                s1.push_back(s[i]);
                knt++;
            }
            else s2.push_back((s[i] - '0' - knt) + '0');
        }
    }
    for (int i = 0; i < s2.size(); ++i)
    {
        int mx = s2[i] - '0';
        int pos = i;
        for (int j = i + 1; j < min((int)s2.size(), i + 9); ++j)
        {
            if (mx < (s2[j] - '0') - (j - i))
            {
                mx = (s2[j] - '0') - (j - i);
                pos = j;
            }
        }
        for (int j = pos - 1; j >= i; --j)
        {
            s2[j + 1] = s2[j];
        }
        
        
        s2[i] = mx + '0';
    }

    cout << s2 + s1 << '\n';

}

signed main() {
#ifdef _VSTU
    bool r = freopen("in.txt", "r", stdin);
    //bool w = freopen("out.txt", "w", stdout);
#endif 
    fast();
    int t = 1;
    cin >> t;
#ifdef _VSTU
    double start = omp_get_wtime();
#endif
    for (int i = 0; i < t; ++i) {
        solve(i + 1);
    }
#ifdef _VSTU
    cout << '\n' << omp_get_wtime() - start;
#endif
    return 0;
}


Submission (verdict: Accepted):
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <limits>
#include <omp.h>
#include <numeric>
#include <set>
#include <queue>
#include <map>
#include <bitset>
#include <math.h>
#include <algorithm>
#include <string>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
using namespace std;

#define int long long
#define all(a) a.begin(),a.end()

const int MOD = 1e9 + 7;
const int INF = 1e9;

void fast() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
}

template <typename T>
istream& operator>>(istream& o, vector<T>& a) {
    for (int i = 0; i < a.size(); ++i) {
        o >> a[i];
    }
    return o;
}

template <typename T, typename Y>
istream& operator>>(istream& o, pair<T, Y>& p) {
    o >> p.first >> p.second;
    return o;
}

template <typename T, typename Y>
ostream& operator<<(ostream& o, pair<T, Y>& p) {
    o << p.first << " " << p.second << "\n";
    return o;
}

template <typename T>
ostream& operator<<(ostream& o, vector<T>& a) {
    for (int i = 0; i < a.size(); ++i) {
        o << a[i] << " ";
    }
    return o;
}

int bpow(int n, int k, int MOD) {
    if (k == 0) return 1;
    else if (k % 2 == 1) return bpow(n, k - 1, MOD) * n % MOD;
    return bpow(n * n % MOD, k / 2, MOD) % MOD;
}

int gcd(int a, int b) {
    
    if (a < b) swap(a, b);
    while (b != 0) {
        int residue = a % b;
        a = b;
        b = residue;
    }
    return a;
}

int lcm(int a, int b) {
    int h = gcd(a, b);
    return h ? (a * (b / h)) : 0;
}

vector<int> z_f(string& s) {
    int n = s.size();
    vector<int> z(n);
    int l = 0, r = 0;
    for (int i = 1; i < n; ++i) {
        z[i] = max((int)0, min(z[i - l], r - i));
        while (z[i] + i < n && s[z[i]] == s[z[i] + i]) z[i]++;
        if (i + z[i] > r) {
            r = i + z[i]; l = i;
        }
    }
    return z;
}


vector<bool> SieveOfEratosthenes(int n) {
    vector<bool> prime(n + 1, true);
    prime[0] = prime[1] = false;
    for (int i = 2; i <= n; i++) {
        if (prime[i]) {
            if (i * 1ll * i <= n) {
                for (int j = i * i; j <= n; j += i) prime[j] = false;
            }
        }
    }
    return prime;
}

set<int> primes;



void solve(int test)
{
    string s;
    cin >> s;
    string s1, s2;
    int knt = 0;
    for (int i = 0; i < s.size(); ++i)
    {
        if (s[i] == '0')
        {
            knt++;
            s1.push_back('0');
        }
        else
        {
            if (s[i] - '0' <= knt)
            {
                s1.push_back(s[i]);
                knt++;
            }
            else s2.push_back((s[i] - '0' - knt) + '0');
        }
    }
    for (int i = 0; i < s2.size(); ++i)
    {
        int mx = s2[i] - '0';
        int pos = i;
        for (int j = i + 1; j < min((int)s2.size(), i + 9); ++j)
        {
            if (mx < (s2[j] - '0') - (j - i))
            {
                mx = (s2[j] - '0') - (j - i);
                pos = j;
            }
        }
        for (int j = pos - 1; j >= i; --j)
        {
            s2[j + 1] = s2[j];
        }
        
        
        s2[i] = mx + '0';
    }



    for (int i = 0; i < s1.size(); ++i)
    {
        int mx = s1[i] - '0';
        int pos = i;
        for (int j = i + 1; j < min((int)s1.size(), i + 9); ++j)
        {
            if (mx < (s1[j] - '0') - (j - i))
            {
                mx = (s1[j] - '0') - (j - i);
                pos = j;
            }
        }
        for (int j = pos - 1; j >= i; --j)
        {
            s1[j + 1] = s1[j];
        }


        s1[i] = mx + '0';
    }

    cout << s2 + s1 << '\n';

}

signed main() {
#ifdef _VSTU
    bool r = freopen("in.txt", "r", stdin);
    //bool w = freopen("out.txt", "w", stdout);
#endif 
    fast();
    int t = 1;
    cin >> t;
#ifdef _VSTU
    double start = omp_get_wtime();
#endif
    for (int i = 0; i < t; ++i) {
        solve(i + 1);
    }
#ifdef _VSTU
    cout << '\n' << omp_get_wtime() - start;
#endif
    return 0;
}