ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2063C - Remove Exactly Two
Language: c++17 

Solution 1 (runtime: 811 ms):
#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main() {
    ll t;
    cin >> t;
    while (t--) {
        ll n;
        cin >> n;
        unordered_map<ll, unordered_set<ll>> adj(n + 1);
        for (ll i = 0; i < n - 1; i++) {
            ll u, v;
            cin >> u >> v;
            adj[u].insert(v);
            adj[v].insert(u);
        }

        vector<ll> deg(n + 1);
        for (ll i = 1; i <= n; i++) {
            deg[i] = adj[i].size();
        }
        ll ans = LLONG_MIN;
        for (ll u = 1; u <= n; u++) {
            for (ll v : adj[u]) {
                if (u < v) {
                    ans = max(ans, deg[u] + deg[v] - 2);
                }
            }
        }
        vector<pair<ll, ll>> nodes;
        for (ll i = 1; i <= n; i++) {
            nodes.push_back({deg[i], i});
        }
        sort(nodes.rbegin(), nodes.rend());
        for (ll i = 0; i <nodes.size(); i++) {
            for (ll j = i + 1; j <nodes.size(); j++) {
                ll u = nodes[i].second;
                ll v = nodes[j].second;
                if (adj[u].find(v) == adj[u].end()) {
                    ans = max(ans, deg[u] + deg[v] - 1);
                    break;
                }
            }
        }
        cout << ans << endl;
    }
}

Solution 2 (runtime: 796 ms):
#include<bits/stdc++.h>
using namespace std;

using ll = long long;
using str = string;
using vi = vector<int>;
using pi = pair<int, int>;
using vpi = vector<pi>;
using si = set<int>;

#define sz (int)(x).size()
#define pb push_back
#define all(x) begin(x), end(x)
#define sor(x) sort(all(x))
#define rev(x) reverse(all(x))
#define del(x, i) erase(begin(x) + i)
#define rem(x, i) erase(begin(x), begin(x) + i)

#define mp make_pair
#define sec second
#define fir first

#define ins insert
#define ez erase
#define cnt count

#define MOD 1e9 + 7
#define MAX_INT 1e18 * 9

#define possibilities(x) while(next_permutation(all(x)))

#define Trav(a, x) for (auto &a : x)
#define For(i, a, b) for (int i = (a); i < (b); ++i)

void solve() {
    int n;
    cin >> n;
    vector<vector<int>> adj(n);
    vector<int> degree(n);
    For(i, 0, n - 1) {
        int x, y;
        cin >> x >> y;
        x--, y--;
        degree[x]++, degree[y]++;
        adj[x].pb(y), adj[y].pb(x);
    }
    if(n <= 2) {
        cout << 0 << endl;
        return;
    }
    int res = 1;
    set<pair<int, int>> ms;

    For(i, 0, n) {
        ms.ins({degree[i], i});
    }

    For(i, 0, 2) {
        set<pair<int, int>> temp(ms);
        int curr = 0;
        auto it1 = temp.end();
        For(j, 0, i + 1) it1--; 
        int rm = it1->second;
        curr += it1->first;
        temp.erase({degree[rm], rm});
        for(auto &x: adj[rm]) {
            temp.erase({degree[x], x});
            temp.ins({degree[x] - 1, x});
        }
        auto it2 = temp.end();
        it2--;
        rm = it2->second;
        curr += it2->first;
        res = max(res, curr - 1);
    }
        
    cout << res << endl;
}

int main() {
    int t;
    cin >> t;
    while(t--) {
        solve();
    }
    return 0;
}
Problem: 2050D - Digital string maximization
Language: c++17 

Solution 1 (runtime: 62 ms):
#include<bits/stdc++.h>



using namespace std;



#define rep(i, a, b) for (int i = a; i < (b); i++)

#define rrep(i, a, b) for (int i = a; i >= (b); i--)

#define range(i, a, b, s) for (int i = a; (s > 0 && i < (b)) || (s < 0 && i > (b)); i += s)

#define trav(a, x) for (auto& a : x)

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

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

typedef long long ll;

typedef pair<int, int> pii;

typedef vector<int> vi;

typedef vector<pii> vpi;



template <typename T>

void print_arr(vector<T> a) {

	trav(x, a) cout << to_string(x) << " ";

	cout << "\n";

}



int T;

vi a;

string s;



int main() {

	ios_base::sync_with_stdio(false);

	cin.tie(NULL);

	cout.tie(NULL);



	cin >> T;

	while (T--) {

		cin >> s;



		vi skip(sz(s), 0);

		rep(i, 0, sz(s)) {

			if (skip[i]) continue;



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

			int mx_pos = 0;

			rep(j, 1, 10) {

				if (i+j >= sz(s)) break;

				if (s[i+j] - '0' - j > mx) {

					//cout << i << " " << i+j << " " << s[i+j] - '0' - j << '\n';

					mx = s[i+j] -'0' - j;

					mx_pos = j;

				}

			}



			char prev = s[i];

			rep(j, 1, mx_pos+1) {

				if (i+j >= sz(s)) break;

				swap(s[i+j], prev);

			}



			s[i] = mx+'0';

		}

 

		cout << s << '\n';

	}



	return 0;

}



Solution 2 (runtime: 62 ms):
#include <bits/stdc++.h>



using namespace std;

typedef long long ll;



typedef vector<int> vi;

typedef vector<ll> vl;

typedef vector<pair<int, int>> vii;

typedef vector<pair<ll, ll>> vll;

typedef vector<vector<int>> vvi;

typedef vector<vector<ll>> vvl;

typedef vector<string> vs;

typedef pair<int, int> pii;

typedef pair<ll, ll> pll;



#define pb push_back

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



#ifdef DEBUG

#include "debug/debug.hpp"

#else

#define nl

#define db(x)

#endif



int main() {

    ios::sync_with_stdio(false);

    cin.tie(nullptr); cout.tie(nullptr);



#ifdef DEBUG

    freopen("input.txt", "r", stdin);

    freopen("debug.log", "w", stderr);

#endif



    int Q; cin >> Q;

    while (Q--) {

		string str; cin >> str;

		for (int i = 0; i < si(str) - 1; i++) {

			ll l = i; ll r = i + 1;

			while (str[r] - str[l] >= 2 && l >= 0) {

				str[r]--;

				swap(str[l], str[r]);

				l--; r--;

				db(str); db(str[l]); db(str[r]); nl;

			}

		}

		nl;

		cout << str << endl;

    }



    return 0;

}
Problem: 2060D - Subtract Min Sort
Language: c++17 

Solution 1 (runtime: 343 ms):
#include<bits/stdc++.h>

using namespace std;

#define int long long

#define pii pair<int,int>

#define f first

#define s second

const int mod = 998244353;



void solve()

{

int n; cin>>n;

vector<int> inf(n+1);

for(int i=1;i<=n;i++) cin>>inf[i];



for(int i=1;i<n;i++){

int t=min(inf[i],inf[i+1]);

inf[i]-=t;

inf[i+1]-=t;

}



for(int i=1;i<n;i++){

if(inf[i]>inf[i+1]){

cout<<"NO\n";

return;

}

}

cout<<"YES\n";

}

/*



*/

signed main()

{

int t=1;cin>>t;

while(t--){

solve();

}

}







Solution 2 (runtime: 109 ms):
#include <bits/stdc++.h>

using namespace std;



// Header files, namespaces,

// macros as defined above

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;



#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>



using ll = long long;

using ull = unsigned long long;



#define pb(v) push_back(v)

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

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

#define MSET(c, v) memset(c, v, sizeof(c))

#define debug(x) cout << #x << " is " << x << endl



const int NEGINF = 0xC0C0C0C0;

const int NULO = -1;

const double EPS = 1e-10;

const ll mod = 1e9 + 7;

const ll llInf = 4e17;

const int inf = 2e9;



int dy[] = {0,1,-1,0};

int dx[] = {1,0,0,-1};



template <typename T,typename U>

std::pair<T,U> operator+(const std::pair<T,U> & l,const std::pair<T,U> & r) {

return {l.first+r.first,l.second+r.second};

}



const int maxn = 2e5 + 100;



ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }



ll a[maxn];

void solveTestCase(int test) {



int n;

cin >> n;

for(int i = 1; i <= n; i++) {

cin >> a[i];

}

bool ok = true;

for(int i = 1; i <= n - 1; i++) {

if(a[i] > a[i + 1]) {

ok = false;

break;

} else {

a[i + 1] -= a[i];

a[i] = 0;

}

}

cout << (ok ? "YES" : "NO") << endl;

}



int main() {

ios::sync_with_stdio(false);

int test = 1;

cin >> test;

for (int t = 1; t <= test; t++) {

solveTestCase(t);

}



return 0;

}
Problem: 2063B - Subsequence Update
Language: c++17 

Solution 1 (runtime: 171 ms):
#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--){
	    int n,l,r;
	    cin>>n>>l>>r;
	    
	    vector<int> vec(n);
	    for(int i = 0;i<n;i++){
	        cin>>vec[i];
	    }
	    int k = r- l +1;
	    vector<int> v1,v2;
	    for(int i = 0;i<r;i++){
	        v1.push_back(vec[i]);
	    }
	    for(int i = l-1;i<n;i++){
	        v2.push_back(vec[i]);
	    }
	    long long a1= 0,a2=0;
	    sort(v1.begin(),v1.end());
	    sort(v2.begin(),v2.end());
	    for(int i = 0;i<k;i++){
	        a1 += v1[i];
	        a2 += v2[i];
	        
	    }
	   // cout<<"Her"<<endl;
	    cout<<min(a1,a2)<<endl;
	}


}


Solution 2 (runtime: 202 ms):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n, l, r;
        cin >> n >> l >> r;
        vector<int> a(n + 1), b(n + 1);

        for (int i = 1; i <= n; i++) {
            cin >> a[i];
            b[i] = a[i];
        }

        sort(b.begin() + 1, b.begin() + r + 1);
        sort(a.begin() + l, a.begin() + n + 1);

        ll s1 = 0, s2 = 0;
        for (int i = 1; i <= r - l + 1; i++) 
            s1 += b[i];
        for (int i = l; i <= r; i++) 
            s2 += a[i];

        cout << min(s1, s2) << endl;
    }
    return 0;
}
Problem: 2063B - Subsequence Update
Language: c++17 

Solution 1 (runtime: 62 ms):
#include<bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <numeric>
#include <cmath>
#include <stack>
#include <numeric>
using namespace std;
using i64 = long long;
using u64 = unsigned long long;
#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define all(x) (x).begin(), (x).end()
#define debug(x) cerr << #x<<" "; _print(x); cerr << endl;
#define nl "\n" 
i64 gcd(i64 a,i64 b){
    return b?gcd(b,a%b):a;
}
void solve(){
    int n, l, r;
    long long sum = 0;
    cin >> n >> l >> r;
    int a[n];
    l--, r--;
    for(int i=0; i<n; ++i) cin >> a[i];
    for(int i=l; i<=r; ++i) sum += a[i];
    sort(a, a+l);
    sort(a+l, a+r+1);
    sort(a+r+1, a+n);

    // left half
    long long x = 0, y = 0;
    int i=0, j=r;
    while(i < l && j >= l && a[i] < a[j]){
        x += a[i];
        y += a[j];
        i++, j--;
    }
    long long sum_1 = (sum - y) + x;

    // right half
    x = 0, y = 0;
    i=r+1, j=r;
    while(i < n && j >= l && a[i] < a[j]){
        x += a[i];
        y += a[j];
        i++, j--;
    }
    long long sum_2 = (sum - y) + x;

    cout << min(sum_1, sum_2) << nl;

}
int main(){
    fastio();
    int testcase_count;
    cin>>testcase_count;
    while(testcase_count--){
        solve();
    }
}

Solution 2 (runtime: 77 ms):
#include <bits/stdc++.h>

using namespace std;



//Type aliasesa

typedef long long ll;

typedef unsigned long long ull;

typedef double db;



//Constants

const int MOD = 1e9+7;



//Macros

#define fast_io() ios::sync_with_stdio(false); cin.tie(nullptr);

#define pb push_back

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

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

#define ff first

#define ss second

#define vi vector<ll>

#define vpi vector<pair<ll,ll>>

#define vvi vector<vector<ll>>

#define mp make_pair



//Loops

#define fr(i, a, b) for (ll i = (a); i < (b); ++i)

#define frd(i, a, b) for (ll i = (a); i >= (b); --i)



//Helpers

template <typename T> void read_vector(vector<T> &v) {

    for (auto &x : v) cin >> x;

}



void solve(){

    ll n,l,r;

    cin>>n>>l>>r;

    vi a(n);

    read_vector(a);

    vi s1,s2;

    fr(i,0,r){

        s1.pb(a[i]);

    }

    fr(i,l-1,n){

        s2.pb(a[i]);

    }

    sort(all(s1));

    sort(all(s2));

    ll ans1=0,ans2=0;

    fr(i,0,r-l+1){

        ans1+=s1[i];

        ans2+=s2[i];

    }

    cout<<min(ans1,ans2)<<endl;

}



int main() {

    fast_io();

    int t;

    cin>>t;

    while(t--){

        solve();

    }

    return 0;

}
Problem: 2060D - Subtract Min Sort
Language: c++17 

Solution 1 (runtime: 358 ms):
#include<bits/stdc++.h>
using namespace std;
#define long long ll

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        vector<int> a(n);
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
        }
        if(a[1]<a[0])
        {
            cout<<"NO"<<endl;
            continue;
        }
        if(n==2)
        {
            cout<<"YES"<<endl;
            continue;
        }
        bool ans = false;
        for(int i=1;i<n-1;i++)
        {
            if(a[i]-min(a[i],a[i-1])<=a[i+1])
            {
                a[i] = a[i] - min(a[i],a[i-1]);
                ans = true;
                continue;
            }
            else{
                ans = false;
                break;
            }
        }
        if(ans)
        {
            cout<<"YES"<<endl;
        }
        else{
            cout<<"NO"<<endl;
        }
    }
}

Solution 2 (runtime: 109 ms):
# include<bits/stdc++.h>
#define ll long long
#define yes cout <<"YES"<< endl
#define no cout << "NO" << endl
#define forn for(int i=0;i<n;i++)
#define form for(int j=0;j<m;j++)
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define int long long
#define endl "\n"
#define test cout<<"TEST"<<endl;
#define IO_FAST  ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,
        tree_order_statistics_node_update> indexed_set;
//////////////
const int N=2e5+11;
///////////////////
const int mod=1e9+7;
/////////////////
int a[N];
void so()
{
int n;
cin>>n;
forn
cin>>a[i];
forn
{
    int mn=min(a[i],a[i+1]);
    a[i]-=mn;
    a[i+1]-=mn;
}
if(is_sorted(a,a+n))
    yes;
else
    no;
}


signed main()
{
    IO_FAST
    int ttt=1;
cin>>ttt;
    while(ttt--)
    {
       so();
    }

    return 0;
}
Problem: 2050B - Transfusion
Language: c++17 

Solution 1 (runtime: 93 ms):
#include <bits/stdc++.h>
using namespace std;

#define ll long long

void solve()
{
    int n;
    cin >> n;
    ll a[2] = {0};
    for (int i = 0, tt; i < n; ++i)
    {
        cin >> tt;
        a[i % 2] += tt;
    }
    if (a[0] % (n + 1 >> 1) == 0 && a[1] % (n >> 1) == 0 && a[0] / (n + 1 >> 1) == a[1] / (n >> 1))
        cout << "YES" << endl;
    else
        cout << "NO" << endl;
}

int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

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

Solution 2 (runtime: 77 ms):
#include <bits/stdc++.h>
#define ll long long
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    ll t;
    cin >> t;
    while (t--)
    {
        ll n;
        cin >> n;

        vector<ll> a(n);
        for (ll i = 0; i < n; i++)
        {
            cin >> a[i];
        }

        ll odd_sum = 0, even_sum = 0;

        for (ll i = 0; i < n; ++i)
        {
            if (i % 2 == 0)
            {
                even_sum += a[i];
            }
            else
            {
                odd_sum += a[i];
            }
        }

        ll odd_count = n / 2;
        ll even_count = (n + 1) / 2;

        if ((odd_sum % odd_count != 0) || (even_sum % even_count != 0) ||
            (odd_sum / odd_count != even_sum / even_count))
        {
            cout << "NO\n";
        }
        else
        {
            cout << "YES\n";
        }
    }

    return 0;
}
Problem: 2050B - Transfusion
Language: c++17 

Solution 1 (runtime: 78 ms):
#include<bits/stdc++.h>

using namespace std;

#define ll long long

signed solve()

{

	ll n;

	cin>>n;

	vector<ll> arr(n);

	ll sum=0;

	ll odd_sum=0,odd_cnt=0;

	ll eve_sum=0,eve_cnt=0;

	for(int i=0;i<n;i++)

	{

		cin>>arr[i];

		sum+=arr[i];

		if(i%2==0)

			{

				eve_sum+=arr[i];

				eve_cnt++;

			}

			else

			{

				odd_sum+=arr[i];

				odd_cnt++;

			}

	}

	if(sum%n==0)

	{

		ll eve_avg=eve_sum/eve_cnt;

		ll odd_avg=odd_sum/odd_cnt;

		if(eve_avg==odd_avg)

		{

			cout<<"YES"<<endl;

		}

		else

		{

			cout<<"NO"<<endl;

		}

	}

	else

	{

		cout<<"NO"<<endl;

	}

    return 0;

}

signed main()

{

    int t;

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

    cout.tie(NULL);

    cin>>t;

    //t=1;

    while(t--)

    {

        solve();

    }

    return 0;

}

Solution 2 (runtime: 327 ms):
#include <bits/stdc++.h>

using namespace std;

 

int main() {

    int t;

    cin >> t;

    while (t--){

        int n;

        cin >> n;

        int a[n];

        long long sum1=0;

        long long sum2=0;

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

            cin >>a[i];

            if(i%2==0){

                sum1+=a[i];

            }

            else{

                sum2+=a[i];

            }

        }

        long long x = (sum1+sum2);

        if(x%n!=0){

            cout << "No" << endl;

        }

        else{

            long long y = x/n;

            if(n%2==0){

                if(sum1==sum2 and sum1==y*(n/2)){

                    cout << "Yes" << endl;

                }

                else{

                    cout << "No" << endl;

                }

            }

            else{

                if(sum1==y*((n+1)/2) and sum2==y*((n-1)/2)){

                    cout << "Yes" << endl;

                }

                else{

                    cout << "No" << endl;

                }

            }

        }

        

    }

}
Problem: 2050D - Digital string maximization
Language: c++17 

Solution 1 (runtime: 108 ms):
#include<bits/stdc++.h>
using namespace std;
int main(){
 int n;
 cin>>n;
 while(n--){
  string s;
  cin>>s;
  int i=1;
  while(i<s.length()){
   int j=i;
   while(j>0 && s[j]>0 && s[j]-1>s[j-1]){
    swap(s[j],s[j-1]);
    s[j-1]=s[j-1]-1;
    j--;
   }
   i++;
 }
 for(auto i:s){
  cout<<i;
 }
 cout<<endl;
}}

Solution 2 (runtime: 62 ms):
#include <bits/stdc++.h>

using namespace std;

int main() {
    cin.sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--) {
        string s;
        cin >> s;
        int n = s.size();
        for (int i = 1; i < n; i++) {
            for (int j = i; j > 0 && s[j] > '0' && s[j] > s[j - 1] + 1; j--) {
                --s[j];
                swap(s[j], s[j - 1]);
            }
        }
        cout << s << '\n';
    }
    return 0;
}
Problem: 2049B - pspspsps
Language: c++17 

Solution 1 (runtime: 61 ms):
#include <bits/stdc++.h>

using namespace std;

 

int main() {

	// your code goes here

	int t;

	cin>>t;

	int i= 1;

	while(t--){

	    int n;

	    cin>>n;

	    string s;

	    cin>>s;

	   // cout<<"hii"<<endl;

	    int lasts=-1;

	    int firstp=-1;

	    int counts=0;

	    int countp=0;

	    bool flag=true;

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

	        //cout<<s[i]<<endl;

	        if(s[i]=='s'){

	            lasts=i;

	            counts++;

	        }

	        if(s[i]=='p'){

	            countp++;

	        }

	        if(s[i]=='p' && flag){

	            firstp=i;

	            flag=false;

	        }

	        

	    }

	    //cout<<lasts<<" "<<firstp<<endl;

	    if(counts==0 || countp==0){

	        cout<<"YES"<<endl;

	        continue;

	    }

	    if(lasts>firstp){

	       cout<<"NO"<<endl;

	        continue;

	    }

	   

	    

	    if(counts>1 && countp>1){

	        cout<<"NO"<<endl;

	        continue;

	    }

	    int countdot1=0;

	    int countdot2=0;

	    for(int i=0;i<lasts;i++){

	        if(s[i]=='.')countdot1++;

	    }

	    for(int i=firstp+1;i<s.size();i++){

	        if(s[i]=='.')countdot2++;

	    }

	    

	    if(countdot1>0 && countdot2>0){

	        cout<<"NO"<<endl;

	        continue;

	    }

	    if(countp==1 && countdot2>0 && counts!=1){

	        cout<<"NO"<<endl;

	        continue;

	    }

	    if(counts==1 && countdot1>0 && countp!=1){

	        cout<<"NO"<<endl;

	        continue;

	    }

	    cout<<"YES"<<endl;

	 

	    

	}



 

}

Solution 2 (runtime: 46 ms):
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const ll inf = 4e18;

const int N = 2e5 + 5;

int n;

string s;

void solve(){

	cin >> n >> s;

	int cs = 0, cp = 0;

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

		if(s[i] == 's' && cp){

			cout << "NO" << '\n';

			return;

		}else if(s[i] == 'p' && cs){



			if(cs == 1 && s[0] == 's');

			else if(i == n - 1);

			else{

				cout << "NO" << '\n';

				return;

			}

		}



		if(s[i] == 's')cs ++;

		else if(s[i] == 'p')cp ++;

	}

	cout << "YES" << '\n';

}



int main(){

	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);

	int _ = 1;cin >> _;

	while(_ --){

		solve();

	}

	return 0;

}

/*





*/
Problem: 2050D - Digital string maximization
Language: c++17 

Solution 1 (runtime: 62 ms):
#include <bits/stdc++.h>
#define ll long long
#define vi vector<int>
using namespace std;
const ll mod = 1e9;


void solve(){
    string s;
    cin>>s;
    for(int i=1;i<s.size();i++){
        int l = i-1;
        int r = i;
        while(l>=0 && (s[r]-s[l])>=2){
            //s[r]--
            //swap s[l] & s[r]
            s[r] -= 1;
            swap(s[l],s[r]);
            //cout<<"string at i="<<i<<": "<<s<<endl;           
            l--;
            r--;
        }
    }
    cout<<s<<endl;
}
/*
5891917899

7591917899

7851917899

7858117899

7875117899

7875161899

7875511899

7875567711
*/

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}




Solution 2 (runtime: 77 ms):
#include<iostream>
using namespace std;
void solve(){
    string s;
    cin>>s;
    int n = s.size();
    for(int i=n-1;i>=1;i--){
        if(int(s[i])-int(s[i-1])>1){
            // swap the elements
            char temp = s[i];
            s[i] = s[i-1];
            s[i-1] = char(int(temp)-1);
            int idx = i;
            while(idx<n-1 && s[idx+1]-s[idx]>1){
              
                     //swap the elements
                    char temp = s[idx+1];                  
                    s[idx+1] = s[idx];
                    s[idx] = char(int(temp)-1);
                
                idx++;
            }
        }
    }
    cout<<s<<endl;
}
int main(){
    int t;
    cin>>t;
    while(t--){
        solve();
    }
}
Problem: 2050D - Digital string maximization
Language: c++17 

Solution 1 (runtime: 77 ms):
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
	int t;
	cin>>t;
	while(t--){
		string a;
		cin>>a;
		for(int i=a.size()-1;i>0;--i){
			if(a[i]-a[i-1]>1){
				a[i]-=1;
				swap(a[i],a[i-1]);
				i+=2;
			}
		}
		cout<<a<<endl;
	}
}

Solution 2 (runtime: 62 ms):
#include<bits/stdc++.h>

  

using namespace std;

using ll = long long;

using ld = long double;

#define mod 998244353





void solve(){

    //code here

    string s; cin >> s;

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

        int tmp = s[i] - '0', pos = i;

        for (int j = i; j < min(i + 10, (int) s.size()); j++) {

            if (s[j] - '0' - (j - i) > tmp) {

                tmp = s[j] - '0' - (j - i);

                pos = j;

            }

        }

        while (pos > i) {

            swap(s[pos], s[pos - 1]);

            pos--;

        }

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

    }

    cout << s;



    

    

    return ;

}

 

int main(){

    ios_base::sync_with_stdio(false);

    cin.tie(nullptr);



    // freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);

    ll t = 1;

    cin >> t;

 

    while (t--){

        // cout<<"-----------"<<endl;

        solve();

        cout<<"\n";

    }

 

    return 0;

}