Problem: 2050D - Digital string maximization
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Wrong answer on test 3):
// ohm namo bhagwate vasudevaya namah

#pragma GCC optimize("O3,unroll-loops")

#include<bits/stdc++.h>

#include<ext/pb_ds/assoc_container.hpp>

#include<ext/pb_ds/tree_policy.hpp>

#include <chrono>

using namespace std;

using namespace __gnu_pbds;

#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)

#define MOD1 998244353

#define INF 1e18

#define pb push_back

#define eb emplace_back

#define ppb pop_back

#define mp make_pair

#define ff first

#define ss second

#define yes cout << "YES" << "\n"

#define no cout << "NO" << "\n"

#define nl cout<<"\n"

#define PI 3.141592653589793238462

#define set_bits __builtin_popcountll

#define sz(x) ((int)(x).size())

#define all(x) (x).begin(), (x).end()

#define vi vector<int>

#define vvi vector<vector<int> >

#define vpii vector<pair<int, int> >

#define pii pair<int, int> 

#define mii map<int, int>

#define rall(x) x.rbegin(), x.rend()

#define mxe(v) *max_element(v.begin(),v.end())

#define mne(v) *min_element(v.begin(),v.end())

template<typename T> using ordered_set = tree<T, null_type,less<T>, rb_tree_tag,tree_order_statistics_node_update>;

template<typename T> using ordered_multiset = tree<T, null_type,less_equal<T>, rb_tree_tag,tree_order_statistics_node_update>;

template<typename T> istream& operator>>(istream& in, vector<T>& a) {for(auto &x : a) in >> x; return in;};

template<typename T> ostream& operator<<(ostream& out, vector<T>& a) {for(auto &x : a) out << x << ' '; return out;};

template<typename T1, typename T2> ostream& operator<<(ostream& out, const pair<T1, T2>& x) {return out << x.ff << ' ' << x.ss;}

template<typename T1, typename T2> istream& operator>>(istream& in, pair<T1, T2>& x) {return in >> x.ff >> x.ss;}

template<typename T> using matrix = vector<vector<T> >;

template<typename T> using rubik = vector<vector<vector<T> > >;

template<typename T> void Unique(T &a) {a.erase(unique(a.begin(), a.end()), a.end());}

#define fr(i,a,b) for(int i=a; ((a<b)?(i<b):(i>=b)); i+=((a<b)?1:-1))

#define fa(i_a) for(auto &i_a)

#ifndef ONLINE_JUDGE

#include "Debug.h"

#else

#define debug(...) 42

#endif

#define int long long

#define ll long long

#define ld long double





void solution() {

    string s; cin>>s;

    for(int i=s.size()-2; i>=0; i--){

        int d1=s[i]-'0';

        int d2=s[i+1]-'0';

        if(d2==0) continue;

        debug(s);

        if(d2-1>d1) {s[i+1]--; swap(s[i],s[i+1]);}

    }

    for(int i=s.size()-2; i>=0; i--){

        int d1=s[i]-'0';

        int d2=s[i+1]-'0';

        if(d2==0) continue;

        debug(s);

        if(d2-1>d1) {s[i+1]--; swap(s[i],s[i+1]);}

    }for(int i=s.size()-2; i>=0; i--){

        int d1=s[i]-'0';

        int d2=s[i+1]-'0';

        if(d2==0) continue;

        debug(s);

        if(d2-1>d1) {s[i+1]--; swap(s[i],s[i+1]);}

    }for(int i=s.size()-2; i>=0; i--){

        int d1=s[i]-'0';

        int d2=s[i+1]-'0';

        if(d2==0) continue;

        debug(s);

        if(d2-1>d1) {s[i+1]--; swap(s[i],s[i+1]);}

    }for(int i=s.size()-2; i>=0; i--){

        int d1=s[i]-'0';

        int d2=s[i+1]-'0';

        if(d2==0) continue;

        debug(s);

        if(d2-1>d1) {s[i+1]--; swap(s[i],s[i+1]);}

    }for(int i=s.size()-2; i>=0; i--){

        int d1=s[i]-'0';

        int d2=s[i+1]-'0';

        if(d2==0) continue;

        debug(s);

        if(d2-1>d1) {s[i+1]--; swap(s[i],s[i+1]);}

    }for(int i=s.size()-2; i>=0; i--){

        int d1=s[i]-'0';

        int d2=s[i+1]-'0';

        if(d2==0) continue;

        debug(s);

        if(d2-1>d1) {s[i+1]--; swap(s[i],s[i+1]);}

    }for(int i=s.size()-2; i>=0; i--){

        int d1=s[i]-'0';

        int d2=s[i+1]-'0';

        if(d2==0) continue;

        debug(s);

        if(d2-1>d1) {s[i+1]--; swap(s[i],s[i+1]);}

    }for(int i=s.size()-2; i>=0; i--){

        int d1=s[i]-'0';

        int d2=s[i+1]-'0';

        if(d2==0) continue;

        debug(s);

        if(d2-1>d1) {s[i+1]--; swap(s[i],s[i+1]);}

    }

    for(int i=s.size()-2; i>=0; i--){

        int d1=s[i]-'0';

        int d2=s[i+1]-'0';

        if(d2==0) continue;

        debug(s);

        if(d2-1>d1) {s[i+1]--; swap(s[i],s[i+1]);}

    }

    for(int i=s.size()-2; i>=0; i--){

        int d1=s[i]-'0';

        int d2=s[i+1]-'0';

        if(d2==0) continue;

        debug(s);

        if(d2-1>d1) {s[i+1]--; swap(s[i],s[i+1]);}

    }

    for(int i=s.size()-2; i>=0; i--){

        int d1=s[i]-'0';

        int d2=s[i+1]-'0';

        if(d2==0) continue;

        debug(s);

        if(d2-1>d1) {s[i+1]--; swap(s[i],s[i+1]);}

    }

    for(int i=s.size()-2; i>=0; i--){

        int d1=s[i]-'0';

        int d2=s[i+1]-'0';

        if(d2==0) continue;

        debug(s);

        if(d2-1>d1) {s[i+1]--; swap(s[i],s[i+1]);}

    }

    for(int i=s.size()-2; i>=0; i--){

        int d1=s[i]-'0';

        int d2=s[i+1]-'0';

        if(d2==0) continue;

        debug(s);

        if(d2-1>d1) {s[i+1]--; swap(s[i],s[i+1]);}

    }

    for(int i=s.size()-2; i>=0; i--){

        int d1=s[i]-'0';

        int d2=s[i+1]-'0';

        if(d2==0) continue;

        debug(s);

        if(d2-1>d1) {s[i+1]--; swap(s[i],s[i+1]);}

    }

    cout<<s<<endl;   

}





int32_t main() {

// #ifndef ONLINE_JUDGE

//     freopen("Error.txt", "w", stderr);

// #endif

    auto start = chrono::steady_clock::now();

    fastio();

    int t=1;

    cin>>t; // comment when 1 entry only

    while(t--) solution();

    auto end = chrono::steady_clock::now(); // Record end time

    auto duration = chrono::duration_cast<chrono::milliseconds>(end - start).count();

    cerr << "Runtime: " << duration << " ms\n";

    return 0;

}

Submission (verdict: Time limit exceeded on test 8):
// ohm namo bhagwate vasudevaya namah

#pragma GCC optimize("O3,unroll-loops")

#include<bits/stdc++.h>

#include<ext/pb_ds/assoc_container.hpp>

#include<ext/pb_ds/tree_policy.hpp>

#include <chrono>

using namespace std;

using namespace __gnu_pbds;

#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)

#define MOD1 998244353

#define INF 1e18

#define pb push_back

#define eb emplace_back

#define ppb pop_back

#define mp make_pair

#define ff first

#define ss second

#define yes cout << "YES" << "\n"

#define no cout << "NO" << "\n"

#define nl cout<<"\n"

#define PI 3.141592653589793238462

#define set_bits __builtin_popcountll

#define sz(x) ((int)(x).size())

#define all(x) (x).begin(), (x).end()

#define vi vector<int>

#define vvi vector<vector<int> >

#define vpii vector<pair<int, int> >

#define pii pair<int, int> 

#define mii map<int, int>

#define rall(x) x.rbegin(), x.rend()

#define mxe(v) *max_element(v.begin(),v.end())

#define mne(v) *min_element(v.begin(),v.end())

template<typename T> using ordered_set = tree<T, null_type,less<T>, rb_tree_tag,tree_order_statistics_node_update>;

template<typename T> using ordered_multiset = tree<T, null_type,less_equal<T>, rb_tree_tag,tree_order_statistics_node_update>;

template<typename T> istream& operator>>(istream& in, vector<T>& a) {for(auto &x : a) in >> x; return in;};

template<typename T> ostream& operator<<(ostream& out, vector<T>& a) {for(auto &x : a) out << x << ' '; return out;};

template<typename T1, typename T2> ostream& operator<<(ostream& out, const pair<T1, T2>& x) {return out << x.ff << ' ' << x.ss;}

template<typename T1, typename T2> istream& operator>>(istream& in, pair<T1, T2>& x) {return in >> x.ff >> x.ss;}

template<typename T> using matrix = vector<vector<T> >;

template<typename T> using rubik = vector<vector<vector<T> > >;

template<typename T> void Unique(T &a) {a.erase(unique(a.begin(), a.end()), a.end());}

#define fr(i,a,b) for(int i=a; ((a<b)?(i<b):(i>=b)); i+=((a<b)?1:-1))

#define fa(i_a) for(auto &i_a)

#ifndef ONLINE_JUDGE

#include "Debug.h"

#else

#define debug(...) 42

#endif

#define int long long

#define ll long long

#define ld long double





void solution() {

    string s; cin>>s;

    int flag = false;

    for(int i=s.size()-2; i>=0; i--){

        int d1=s[i]-'0';

        int d2=s[i+1]-'0';

        if(d2==0) continue;

        debug(s);

        if(d2-1>d1) {s[i+1]--; flag=true; swap(s[i],s[i+1]);}

    }

    while(flag){

        flag = false;

        for(int i=s.size()-2; i>=0; i--){

            int d1=s[i]-'0';

            int d2=s[i+1]-'0';

            if(d2==0) continue;

            debug(s);

            if(d2-1>d1) {s[i+1]--; flag=true; swap(s[i],s[i+1]);}

        }

    }

    cout<<s<<endl;   

}





int32_t main() {

// #ifndef ONLINE_JUDGE

//     freopen("Error.txt", "w", stderr);

// #endif

    auto start = chrono::steady_clock::now();

    fastio();

    int t=1;

    cin>>t; // comment when 1 entry only

    while(t--) solution();

    auto end = chrono::steady_clock::now(); // Record end time

    auto duration = chrono::duration_cast<chrono::milliseconds>(end - start).count();

    cerr << "Runtime: " << duration << " ms\n";

    return 0;

}