ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2063C - Remove Exactly Two
Language: C++20 (GCC 13-64)


Submission (verdict: Time limit exceeded on test 1):
#include<bits/stdc++.h>
#define int long long
using namespace std;




void Solve()
{
    int n,l,r;
    cin >> n;
    set<pair<int,int>>se;
    vector<pair<int,int>>inp;
    vector<int>cnt(n,0);
    for(int i = 0 ; i < n-1; i++){
        cin >> l >> r;
        l--;
        r--;
        se.insert({l,r});
        se.insert({r,l});
        cnt[l]++;
        cnt[r]++;

    }
    int mx = 0;
    for(int i = 0 ; i < n-1 ; i++){
        inp.push_back({cnt[i],i});
    }

    sort(inp.rbegin(),inp.rend());



    for(int i = 0 ; i < inp.size() ; i++){
        for(int j = i + 1 ; j < inp.size() ; j++){
            int cur = inp[i].first + inp[j].first - 2;
            if(!se.count({inp[i].second,inp[j].second})){
                cur++;
            }

            mx = max(mx,cur);
            if(j > i+3 && inp[j].first != inp[j-1].first){
                break;
            }
        }
    }
    cout<<mx<<endl;

}






int32_t main(){

   int t;
   cin >> t;
   for(int i = 0 ; i <t ; i++){
    Solve();
   }
}
Problem: 2063C - Remove Exactly Two
Language: C++20 (GCC 13-64)

Previous submission (verdict: Time limit exceeded on test 1):
#include<bits/stdc++.h>
#define int long long
using namespace std;




void Solve()
{
    int n,l,r;
    cin >> n;
    set<pair<int,int>>se;
    vector<pair<int,int>>inp;
    vector<int>cnt(n,0);
    for(int i = 0 ; i < n-1; i++){
        cin >> l >> r;
        l--;
        r--;
        se.insert({l,r});
        se.insert({r,l});
        cnt[l]++;
        cnt[r]++;

    }
    int mx = 0;
    for(int i = 0 ; i < n-1 ; i++){
        inp.push_back({cnt[i],i});
    }

    sort(inp.rbegin(),inp.rend());



    for(int i = 0 ; i < inp.size() ; i++){
        for(int j = i + 1 ; j < inp.size() ; j++){
            int cur = inp[i].first + inp[j].first - 2;
            if(!se.count({inp[i].second,inp[j].second})){
                cur++;
            }

            mx = max(mx,cur);
            if(j > i+3 && inp[j].first != inp[j-1].first){
                break;
            }
        }
    }
    cout<<mx<<endl;

}






int32_t main(){

   int t;
   cin >> t;
   for(int i = 0 ; i <t ; i++){
    Solve();
   }
}





Submission (verdict: Time limit exceeded on test 1):
#include<bits/stdc++.h>
#define int long long
using namespace std;




void Solve()
{
    int n,l,r;
    cin >> n;
    set<pair<int,int>>se;
    vector<pair<int,int>>inp;
    vector<int>cnt(n,0);
    for(int i = 0 ; i < n-1; i++){
        cin >> l >> r;
        l--;
        r--;
        se.insert({l,r});
        se.insert({r,l});
        cnt[l]++;
        cnt[r]++;

    }
    int mx = 0;
    for(int i = 0 ; i < n-1 ; i++){
        inp.push_back({cnt[i],i});
    }

    sort(inp.rbegin(),inp.rend());



    for(int i = 0 ; i < inp.size() ; i++){
        if(i > 2 && inp[i].first < inp[1].first){
            break;
        }
        for(int j = i + 1 ; j < inp.size() ; j++){
            int cur = inp[i].first + inp[j].first - 2;
            if(!se.count({inp[i].second,inp[j].second})){
                cur++;
            }

            mx = max(mx,cur);
            
        }
    }
    cout<<mx<<endl;

}






int32_t main(){

   int t;
   cin >> t;
   for(int i = 0 ; i <t ; i++){
    Solve();
   }
}
Problem: 2063C - Remove Exactly Two
Language: C++20 (GCC 13-64)

Previous submission (verdict: Time limit exceeded on test 1):
#include<bits/stdc++.h>
#define int long long
using namespace std;




void Solve()
{
    int n,l,r;
    cin >> n;
    set<pair<int,int>>se;
    vector<pair<int,int>>inp;
    vector<int>cnt(n,0);
    for(int i = 0 ; i < n-1; i++){
        cin >> l >> r;
        l--;
        r--;
        se.insert({l,r});
        se.insert({r,l});
        cnt[l]++;
        cnt[r]++;

    }
    int mx = 0;
    for(int i = 0 ; i < n-1 ; i++){
        inp.push_back({cnt[i],i});
    }

    sort(inp.rbegin(),inp.rend());



    for(int i = 0 ; i < inp.size() ; i++){
        if(i > 2 && inp[i].first < inp[1].first){
            break;
        }
        for(int j = i + 1 ; j < inp.size() ; j++){
            int cur = inp[i].first + inp[j].first - 2;
            if(!se.count({inp[i].second,inp[j].second})){
                cur++;
            }

            mx = max(mx,cur);
            
        }
    }
    cout<<mx<<endl;

}






int32_t main(){

   int t;
   cin >> t;
   for(int i = 0 ; i <t ; i++){
    Solve();
   }
}





Submission (verdict: Accepted):
#include<bits/stdc++.h>
#define int long long
using namespace std;




void Solve()
{
    int n,l,r;
    cin >> n;
    set<pair<int,int>>se;
    vector<pair<int,int>>inp;
    vector<int>cnt(n,0);
    for(int i = 0 ; i < n-1; i++){
        cin >> l >> r;
        l--;
        r--;
        se.insert({l,r});
        se.insert({r,l});
        cnt[l]++;
        cnt[r]++;

    }
    int mx = 0;
    for(int i = 0 ; i < n ; i++){
        inp.push_back({cnt[i],i});
    }

    sort(inp.rbegin(),inp.rend());



    for(int i = 0 ; i < min((int)inp.size(),30ll) ; i++){
        if(i > 2 && inp[i].first < inp[1].first ){
            break;
        }
        for(int j = i + 1 ; j < min((int)inp.size(),30ll) ; j++){
            int cur = inp[i].first + inp[j].first - 2;
            if(!se.count({inp[i].second,inp[j].second})){
                cur++;
            }
            mx = max(mx,cur);
            if(j > 3 && inp[j].first < inp[2].first){
                break;
            }
        }

    }
    cout<<mx<<endl;

}






int32_t main(){

   int t;
   cin >> t;
   for(int i = 0 ; i <t ; i++){
    Solve();
   }
}
Problem: 2052A - Adrenaline Rush
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Wrong answer on test 7):
#include <bits/stdc++.h>
#define int long long
#define mod 1000000007
using namespace std;

void solve()
{
    int n;
    cin >> n;
    vector<int> val(n);
    for (int i = 0; i < n; i++)
    {
        cin >> val[i];
    }

    vector<pair<int, int>> ans;
    for (int i = 0; i < n; i++)
    {
        vector<pair<int, int>> v;
        vector<pair<int, int>> s;
        for (int j = i + 1; j < n; j++)
        {
            if (val[i] < val[j])
            {
                ans.push_back({val[j], val[i]});
                v.push_back({val[i], val[j]});
            }
            else
            {
                s.push_back({val[i], val[j]});
            }
        }
        reverse(v.begin(), v.end());
        for (int j = 0; j < v.size(); j++)
        {
            ans.push_back(v[j]);
        }

        for (int j = 0; j < s.size(); j++)
        {
            ans.push_back(s[j]);
        }
    }

    cout << ans.size() << endl;
    for (auto i : ans)
    {
        cout << i.first << " " << i.second << endl;
    }
    cout << endl;
}

signed main()
{
    solve();
    return 0;
}

Submission (verdict: Accepted):
#include <bits/stdc++.h>
#define int long long
#define mod 1000000007
using namespace std;

void solve()
{
    int n;
    cin >> n;
    vector<int> val(n);
    for (int i = 0; i < n; i++)
    {
        cin >> val[i];
    }

    vector<pair<int, int>> ans;
    for (int i = 0; i < n; i++)
    {
        vector<pair<int, int>> v;
        vector<pair<int, int>> s;
        for (int j = i + 1; j < n; j++)
        {
            if (val[i] < val[j])
            {
                // ans.push_back({val[j], val[i]});
                v.push_back({val[i], val[j]});
            }
            else
            {
                s.push_back({val[i], val[j]});
            }
        }

        sort(v.begin(), v.end());
        for (int j = 0; j < v.size(); j++)
        {
            ans.push_back({v[j].second, v[j].first});
        }

        reverse(v.begin(), v.end());
        for (int j = 0; j < v.size(); j++)
        {
            ans.push_back(v[j]);
        }

        sort(s.begin(), s.end());
        reverse(s.begin(), s.end());
        for (int j = 0; j < s.size(); j++)
        {
            ans.push_back(s[j]);
        }
    }

    cout << ans.size() << endl;
    for (auto i : ans)
    {
        cout << i.first << " " << i.second << endl;
    }
    cout << endl;
}

signed main()
{
    solve();
    return 0;
}
Problem: 2052A - Adrenaline Rush
Language: C++23 (GCC 14-64, msys2)


Submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>
#define int long long
#define mod 1000000007
using namespace std;

void solve()
{
    int n;
    cin >> n;
    vector<int> val(n);
    for (int i = 0; i < n; i++)
    {
        cin >> val[i];
    }

    vector<pair<int, int>> ans;
    for (int i = 0; i < n; i++)
    {
        vector<pair<int, int>> v;
        vector<pair<int, int>> s;
        for (int j = i + 1; j < n; j++)
        {
            if (val[i] < val[j])
            {
                ans.push_back({val[i], val[j]});
                v.push_back({val[j], val[i]});
            }
            else
            {
                s.push_back({val[i], val[j]});
            }
        }
        reverse(v.begin(), v.end());
        for (int j = 0; j < v.size(); j++)
        {
            ans.push_back(v[j]);
        }

        for (int j = 0; j < s.size(); j++)
        {
            ans.push_back(s[j]);
        }
    }

    cout << ans.size() << endl;
    for (auto i : ans)
    {
        cout << i.first << " " << i.second << endl;
    }
    cout << endl;
}

signed main()
{
    solve();
    return 0;
}
Problem: 2052A - Adrenaline Rush
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>
#define int long long
#define mod 1000000007
using namespace std;

void solve()
{
    int n;
    cin >> n;
    vector<int> val(n);
    for (int i = 0; i < n; i++)
    {
        cin >> val[i];
    }

    vector<pair<int, int>> ans;
    for (int i = 0; i < n; i++)
    {
        vector<pair<int, int>> v;
        vector<pair<int, int>> s;
        for (int j = i + 1; j < n; j++)
        {
            if (val[i] < val[j])
            {
                ans.push_back({val[i], val[j]});
                v.push_back({val[j], val[i]});
            }
            else
            {
                s.push_back({val[i], val[j]});
            }
        }
        reverse(v.begin(), v.end());
        for (int j = 0; j < v.size(); j++)
        {
            ans.push_back(v[j]);
        }

        for (int j = 0; j < s.size(); j++)
        {
            ans.push_back(s[j]);
        }
    }

    cout << ans.size() << endl;
    for (auto i : ans)
    {
        cout << i.first << " " << i.second << endl;
    }
    cout << endl;
}

signed main()
{
    solve();
    return 0;
}

Submission (verdict: Wrong answer on test 7):
#include <bits/stdc++.h>
#define int long long
#define mod 1000000007
using namespace std;

void solve()
{
    int n;
    cin >> n;
    vector<int> val(n);
    for (int i = 0; i < n; i++)
    {
        cin >> val[i];
    }

    vector<pair<int, int>> ans;
    for (int i = 0; i < n; i++)
    {
        vector<pair<int, int>> v;
        vector<pair<int, int>> s;
        for (int j = i + 1; j < n; j++)
        {
            if (val[i] < val[j])
            {
                ans.push_back({val[j], val[i]});
                v.push_back({val[i], val[j]});
            }
            else
            {
                s.push_back({val[i], val[j]});
            }
        }
        reverse(v.begin(), v.end());
        for (int j = 0; j < v.size(); j++)
        {
            ans.push_back(v[j]);
        }

        for (int j = 0; j < s.size(); j++)
        {
            ans.push_back(s[j]);
        }
    }

    cout << ans.size() << endl;
    for (auto i : ans)
    {
        cout << i.first << " " << i.second << endl;
    }
    cout << endl;
}

signed main()
{
    solve();
    return 0;
}
Problem: 2050D - Digital string maximization
Language: PyPy 3-64

Previous submission (verdict: Wrong answer on test 2):
def highest_var(x):
    com = 'play'
    while com == 'play':
        max_n = max(x)
        check = []
        while max_n > 2:
            pos = [[idx, i] for idx, i in enumerate(x) if i == max_n and idx != 0]

            check_q = 0
            for jup in pos:
                if x[jup[0]-1] < max_n - 1:
                    x[jup[0]-1], x[jup[0]] = x[jup[0]]-1, x[jup[0]-1]
                    check_q = 1
                
            max_n -= 1
            if check_q == 1:
                check.append(1)
        if not check:
            com = 'stop'
    return x


t = int(input())
ab_res = []
for _ in range(t):
    s = input()
    x = []
    res = ''
    for letter in s:
        x.append(int(letter))

    x = highest_var(x)
    for i in x:
        res+=str(i)
    ab_res.append(res)

for i in ab_res:
    print(i)

Submission (verdict: Time limit exceeded on test 7):
def highest_var(x):
    com = 'play'
    while com == 'play':
        max_n = max(x)
        check = []
        while max_n > 1:
            pos = [[idx, i] for idx, i in enumerate(x) if i == max_n and idx != 0]

            check_q = 0
            for jup in pos:
                if x[jup[0]-1] < max_n - 1:
                    x[jup[0]-1], x[jup[0]] = x[jup[0]]-1, x[jup[0]-1]
                    check_q = 1
                
            max_n -= 1
            if check_q == 1:
                check.append(1)
        if not check:
            com = 'stop'
    return x


t = int(input())
ab_res = []
for _ in range(t):
    s = input()
    x = []
    res = ''
    for letter in s:
        x.append(int(letter))

    x = highest_var(x)
    for i in x:
        res+=str(i)
    ab_res.append(res)

for i in ab_res:
    print(i)
Problem: 2050D - Digital string maximization
Language: PyPy 3-64


Submission (verdict: Wrong answer on test 2):
def highest_var(x):
    com = 'play'
    while com == 'play':
        max_n = max(x)
        check = []
        while max_n > 2:
            pos = [[idx, i] for idx, i in enumerate(x) if i == max_n and idx != 0]

            check_q = 0
            for jup in pos:
                if x[jup[0]-1] < max_n - 1:
                    x[jup[0]-1], x[jup[0]] = x[jup[0]]-1, x[jup[0]-1]
                    check_q = 1
                
            max_n -= 1
            if check_q == 1:
                check.append(1)
        if not check:
            com = 'stop'
    return x


t = int(input())
ab_res = []
for _ in range(t):
    s = input()
    x = []
    res = ''
    for letter in s:
        x.append(int(letter))

    x = highest_var(x)
    for i in x:
        res+=str(i)
    ab_res.append(res)

for i in ab_res:
    print(i)
Problem: 2050D - Digital string maximization
Language: PyPy 3-64

Previous submission (verdict: Time limit exceeded on test 7):
def highest_var(x):
    com = 'play'
    while com == 'play':
        max_n = max(x)
        check = []
        while max_n > 1:
            pos = [[idx, i] for idx, i in enumerate(x) if i == max_n and idx != 0]

            check_q = 0
            if pos:
                for jup in pos:
                    if x[jup[0]-1] < max_n - 1:
                        x[jup[0]-1], x[jup[0]] = x[jup[0]]-1, x[jup[0]-1]
                        check_q = 1
                
            max_n -= 1
            if check_q == 1:
                check.append(1)
        if not check:
            com = 'stop'
    return x


t = int(input())
ab_res = []
for _ in range(t):
    s = input()
    x = []
    res = ''
    for letter in s:
        x.append(int(letter))

    x = highest_var(x)
    for i in x:
        res+=str(i)
    ab_res.append(res)

for i in ab_res:
    print(i)

Submission (verdict: Accepted):
def highest_var(x):
    while True:
        max_n = max(x)  
        check = False   
        while max_n > 1:
            for i in range(1, len(x)):  
                if x[i] == max_n and x[i - 1] < max_n - 1:
                    x[i - 1], x[i] = x[i] - 1, x[i - 1]  
                    check = True  
            max_n -= 1
        if not check:  
            break
    return x


t = int(input())
ab_res = []
for _ in range(t):
    s = input().strip()
    x = list(map(int, s))  
    x = highest_var(x)
    ab_res.append(''.join(map(str, x)))

print('\n'.join(ab_res))
Problem: 2050D - Digital string maximization
Language: PyPy 3-64

Previous submission (verdict: Time limit exceeded on test 7):
def highest_var(x):
    com = 'play'
    while com == 'play':
        max_n = max(x)
        check = []
        while max_n > 1:
            pos = [[idx, i] for idx, i in enumerate(x) if i == max_n and idx != 0]

            check_q = 0
            for jup in pos:
                if x[jup[0]-1] < max_n - 1:
                    x[jup[0]-1], x[jup[0]] = x[jup[0]]-1, x[jup[0]-1]
                    check_q = 1
                
            max_n -= 1
            if check_q == 1:
                check.append(1)
        if not check:
            com = 'stop'
    return x


t = int(input())
ab_res = []
for _ in range(t):
    s = input()
    x = []
    res = ''
    for letter in s:
        x.append(int(letter))

    x = highest_var(x)
    for i in x:
        res+=str(i)
    ab_res.append(res)

for i in ab_res:
    print(i)

Submission (verdict: Time limit exceeded on test 7):
def highest_var(x):
    com = 'play'
    while com == 'play':
        max_n = max(x)
        check = []
        while max_n > 1:
            pos = [[idx, i] for idx, i in enumerate(x) if i == max_n and idx != 0]

            check_q = 0
            if pos:
                for jup in pos:
                    if x[jup[0]-1] < max_n - 1:
                        x[jup[0]-1], x[jup[0]] = x[jup[0]]-1, x[jup[0]-1]
                        check_q = 1
                
            max_n -= 1
            if check_q == 1:
                check.append(1)
        if not check:
            com = 'stop'
    return x


t = int(input())
ab_res = []
for _ in range(t):
    s = input()
    x = []
    res = ''
    for letter in s:
        x.append(int(letter))

    x = highest_var(x)
    for i in x:
        res+=str(i)
    ab_res.append(res)

for i in ab_res:
    print(i)
Problem: 2050D - Digital string maximization
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Time limit exceeded on test 7):
#include <iostream>
#include <string>
int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie();
  std::cout.tie();
  int t;
  std::cin >> t;
  for (int j = 0; j < t; ++j) {     
    std::string input;
    std::cin >> input;
    // input =;
    for (int i = 0; i < size(input); ++i) {
      int index_to_swap = -1;
      char max_number = input[i]; 
      for (int ii = 1; ii <= std::min(8, int(size(input)) - i - 1); ++ii) {
        if (input[i + ii] - ii > max_number) {
          index_to_swap = i + ii;
          max_number = input[i + ii] - ii;
        }
      }
      if (index_to_swap != -1) {
        input[index_to_swap] = max_number;
        input = input.substr(0, i) + input[index_to_swap] + input.substr(i, index_to_swap - i) + input.substr(index_to_swap + 1, size(input) - 1 - index_to_swap);
      }
    }
    std::cout << input << '\n';
  }  
}

Submission (verdict: Accepted):
#include <iostream>
#include <string>
int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie();
  std::cout.tie();
  int t;
  std::cin >> t;
  for (int j = 0; j < t; ++j) {     
    std::string input;
    std::cin >> input;
    // input = "333333333333333333333336666666666666666999999999999999000000000";
    for (int i = 0; i < size(input); ++i) {
      int index_to_swap = -1;
      char max_number = input[i]; 
      for (int ii = 1; ii <= std::min(8, int(size(input)) - i - 1); ++ii) {
        if (input[i + ii] - ii > max_number) {
          index_to_swap = i + ii;
          max_number = input[i + ii] - ii;
        }
      }
      if (index_to_swap != -1) {
        input[index_to_swap] = max_number;
        for (int iii = index_to_swap; iii > i; --iii) {
          std::swap(input[iii], input[iii - 1]);
        }
        // input = input.substr(0, i) + input[index_to_swap] + input.substr(i, index_to_swap - i) + input.substr(index_to_swap + 1, size(input) - 1 - index_to_swap);
      }
    }
    std::cout << input << '\n';
  }  
}
Problem: 2050D - Digital string maximization
Language: C++23 (GCC 14-64, msys2)


Submission (verdict: Wrong answer on test 1):
#include <iostream>
#include <string>
int main() {
  int t;
  std::cin >> t;
  for (int j = 0; j < t; ++j) {     
    std::string input;
    std::cin >> input;
    for (int i = 0; i < size(input); ++i) {
      int index_to_swap = -1;
      char max_number = input[i];
      for (int ii = 1; ii <= std::min(8, int(size(input)) - i - 1); ++ii) {
        if (input[i + ii] - ii > max_number) {
          index_to_swap = i + ii;
          max_number = input[i + ii] - ii;
        }
      }
      if (index_to_swap != -1) {
        input[index_to_swap] -= input[index_to_swap] - max_number;
        std::swap(input[i], input[index_to_swap]);
      }
    }
    std::cout << input << '\n';
  }  
}
Problem: 2050D - Digital string maximization
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Time limit exceeded on test 7):
#include <iostream>
#include <string>
int main() {
  int t;
  std::cin >> t;
  for (int j = 0; j < t; ++j) {     
    std::string input;
    std::cin >> input;
    for (int i = 0; i < size(input); ++i) {
      int index_to_swap = -1;
      char max_number = input[i]; 
      for (int ii = 1; ii <= std::min(8, int(size(input)) - i - 1); ++ii) {
        if (input[i + ii] - ii > max_number) {
          index_to_swap = i + ii;
          max_number = input[i + ii] - ii;
        }
      }
      if (index_to_swap != -1) {
        input[index_to_swap] = max_number;
        input = input.substr(0, i) + input[index_to_swap] + input.substr(i, index_to_swap - i) + input.substr(index_to_swap + 1, size(input) - 1 - index_to_swap);
      }
    }
    std::cout << input << '\n';
  }  
}

Submission (verdict: Time limit exceeded on test 7):
#include <iostream>
#include <string>
int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie();
  std::cout.tie();
  int t;
  std::cin >> t;
  for (int j = 0; j < t; ++j) {     
    std::string input;
    std::cin >> input;
    // input =;
    for (int i = 0; i < size(input); ++i) {
      int index_to_swap = -1;
      char max_number = input[i]; 
      for (int ii = 1; ii <= std::min(8, int(size(input)) - i - 1); ++ii) {
        if (input[i + ii] - ii > max_number) {
          index_to_swap = i + ii;
          max_number = input[i + ii] - ii;
        }
      }
      if (index_to_swap != -1) {
        input[index_to_swap] = max_number;
        input = input.substr(0, i) + input[index_to_swap] + input.substr(i, index_to_swap - i) + input.substr(index_to_swap + 1, size(input) - 1 - index_to_swap);
      }
    }
    std::cout << input << '\n';
  }  
}
Problem: 2050D - Digital string maximization
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Wrong answer on test 1):
#include <iostream>
#include <string>
int main() {
  int t;
  std::cin >> t;
  for (int j = 0; j < t; ++j) {     
    std::string input;
    std::cin >> input;
    for (int i = 0; i < size(input); ++i) {
      int index_to_swap = -1;
      char max_number = input[i];
      for (int ii = 1; ii <= std::min(8, int(size(input)) - i - 1); ++ii) {
        if (input[i + ii] - ii > max_number) {
          index_to_swap = i + ii;
          max_number = input[i + ii] - ii;
        }
      }
      if (index_to_swap != -1) {
        input[index_to_swap] -= input[index_to_swap] - max_number;
        std::swap(input[i], input[index_to_swap]);
      }
    }
    std::cout << input << '\n';
  }  
}

Submission (verdict: Time limit exceeded on test 7):
#include <iostream>
#include <string>
int main() {
  int t;
  std::cin >> t;
  for (int j = 0; j < t; ++j) {     
    std::string input;
    std::cin >> input;
    for (int i = 0; i < size(input); ++i) {
      int index_to_swap = -1;
      char max_number = input[i]; 
      for (int ii = 1; ii <= std::min(8, int(size(input)) - i - 1); ++ii) {
        if (input[i + ii] - ii > max_number) {
          index_to_swap = i + ii;
          max_number = input[i + ii] - ii;
        }
      }
      if (index_to_swap != -1) {
        input[index_to_swap] = max_number;
        input = input.substr(0, i) + input[index_to_swap] + input.substr(i, index_to_swap - i) + input.substr(index_to_swap + 1, size(input) - 1 - index_to_swap);
      }
    }
    std::cout << input << '\n';
  }  
}