ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2062C - Cirno and Operations
Language: Python 3

Previous submission (verdict: Wrong answer on test 1):
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 21 22:10:53 2025

@author: hp
"""
n = int(input())
for i in range(n):
    v = list(map(int,input().strip().split()))
    su = sum(v)
    flag = True
    while flag:
        if len(v) < 2:
            break

        v = v[::-1]
        c = []

        for i in range(1,len(v)):
            c.append(v[i] - v[i-1])

        v = c
        if su < sum(v):
            su = sum(v)



    print(su)
    
    


Submission (verdict: Wrong answer on test 2):
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 21 22:10:53 2025

@author: hp
"""
n = int(input())
for i in range(n):
    nu = input()
    v = list(map(int,input().strip().split()))
    su = sum(v)
    flag = True
    while flag:
        if len(v) < 2:
            break

        v = v[::-1]
        c = []

        for i in range(1,len(v)):
            c.append(v[i] - v[i-1])

        v = c
        if su < sum(v):
            su = sum(v)



    print(su)
Problem: 2062C - Cirno and Operations
Language: Python 3

Previous submission (verdict: Wrong answer on test 2):
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 21 22:10:53 2025

@author: hp
"""
k = []
n = int(input())
for i in range(n):
    nu = input()
    v = list(map(int,input().strip().split()))
    su = sum(v)
    flag = True
    while flag:
        if len(v) < 2:
            break

        v = v[::-1]
        c = []

        for i in range(1,len(v)):
            c.append(v[i] - v[i-1])

        v = c
        if su < sum(v):
            su = sum(v)



    k.append(su)
for d in k:
    print(d)  
    


Submission (verdict: Accepted):
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 21 22:10:53 2025

@author: hp
"""
t = int(input())  # Number of test cases

for _ in range(t):
    n = int(input())  # Length of the sequence
    v = list(map(int, input().split()))  # The sequence

    max_sum = sum(v)  # Track the maximum sum encountered

    while len(v) > 1:
        # Try without reversing
        diff_v = [v[i] - v[i - 1] for i in range(1, len(v))]
        max_sum = max(max_sum, sum(diff_v))  # Update max sum

        # Try with reversing
        v.reverse()
        rev_diff_v = [v[i] - v[i - 1] for i in range(1, len(v))]
        max_sum = max(max_sum, sum(rev_diff_v))  # Update max sum

        # Pick the better option
        if sum(diff_v) > sum(rev_diff_v):
            v = diff_v
        else:
            v = rev_diff_v

    print(max_sum)
Problem: 2062C - Cirno and Operations
Language: Python 3


Submission (verdict: Wrong answer on test 1):
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 21 22:10:53 2025

@author: hp
"""
n = int(input())
for i in range(n):
    v = list(map(int,input().strip().split()))
    su = sum(v)
    flag = True
    while flag:
        if len(v) < 2:
            break

        v = v[::-1]
        c = []

        for i in range(1,len(v)):
            c.append(v[i] - v[i-1])

        v = c
        if su < sum(v):
            su = sum(v)



    print(su)
Problem: 2062C - Cirno and Operations
Language: Python 3

Previous submission (verdict: Wrong answer on test 2):
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 21 22:10:53 2025

@author: hp
"""
n = int(input())
for i in range(n):
    nu = input()
    v = list(map(int,input().strip().split()))
    su = sum(v)
    flag = True
    while flag:
        if len(v) < 2:
            break

        v = v[::-1]
        c = []

        for i in range(1,len(v)):
            c.append(v[i] - v[i-1])

        v = c
        if su < sum(v):
            su = sum(v)



    print(su)
    
    


Submission (verdict: Wrong answer on test 2):
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 21 22:10:53 2025

@author: hp
"""
k = []
n = int(input())
for i in range(n):
    nu = input()
    v = list(map(int,input().strip().split()))
    su = sum(v)
    flag = True
    while flag:
        if len(v) < 2:
            break

        v = v[::-1]
        c = []

        for i in range(1,len(v)):
            c.append(v[i] - v[i-1])

        v = c
        if su < sum(v):
            su = sum(v)



    k.append(su)
for d in k:
    print(d)
Problem: 2049C - MEX Cycle
Language: C++20 (GCC 13-64)


Submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>

using namespace std;

using namespace __gnu_pbds;

typedef long long ll;

template <class type1>

using ordered_multiset = tree<type1, null_type, less_equal<type1>, rb_tree_tag, tree_order_statistics_node_update>;

typedef __gnu_pbds::tree<ll, __gnu_pbds::null_type, less<ll>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update> ordered_set;

#define pb push_back

#define pp pop_back

#define endl '\n' 

#define dbg(x) cerr << #x << " = " << x << endl

const int mod = 1e9 + 7;

const double e = 1e-6;



void solve() {

    ll n, x, y;

    cin >> n >> x >> y;

    vector<ll> a(n+1);

    if(y < x) swap(x, y);

    if((y-x)%2 || (n+y-x)%2) {

        for(ll i = 0; x+i < y; ++i){

            if(i%2) a[i+x] = 1;

            else a[i+x] = 0;

        }

        a[y] = 2;

        for(ll i = 0; (n+x-i) > y  ; i++){

            if((x-i)%2){

                if((n+x-i)%n == 0) a[n] = 1;

                else a[(n+x-i)%n] = 1;

            } 

            else{

                if((n+x-i)%n == 0) a[n] = 0;

                else a[(n+x-i)%n] = 0;

            }

        }

    }

    else{

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

            if(i%2){

                if((x+i)%n == 0) a[n] = 1;

                else a[(x+i)%n] = 1;

            }

            else{

                if((x+i)%n == 0) a[n] = 0;

                else a[(x+i)%n] = 0;

            }

        }

    }

    for(ll i = 1; i <= n; ++i) cout << a[i] << " ";

    cout << endl;

}



int main() {

    ios_base::sync_with_stdio(false);

    cin.tie(nullptr);

    int t; cin >> t; while(t--)

    solve();

}
Problem: 2062C - Cirno and Operations
Language: C++17 (GCC 7-32)


Submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
using namespace std;

int main() {
	int t;
	cin>>t;
	while (t--)
	{  
	    int n, x;
        cin >> n; 
        vector<int> v;

        for (int i = 0; i < n; i++) {
        cin >> x;
        v.push_back(x);
    }
    int loop=0;
    int sum ;
    if(v.size()>1)
    {
    sum = abs(accumulate(v.begin(), v.end(), 0));
    }
    else {sum = v[0];}
    while(v.size()-1)
    {
        for (size_t i = v.size() - 1; i > 0; i--) {
        v[i] = v[i] - v[i - 1]; // Update current index with difference
    }
    
    v.erase(v.begin());
    if (abs(accumulate(v.begin(), v.end(), 0))>sum)
    {
        sum = abs(accumulate(v.begin(), v.end(), 0));
    }

    }
    cout<<sum<<endl;
 }
}
Problem: 2062C - Cirno and Operations
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
using namespace std;

int main() {
	int t;
	cin>>t;
	while (t--)
	{  
	    int n, x;
        cin >> n; 
        vector<int> v;

        for (int i = 0; i < n; i++) {
        cin >> x;
        v.push_back(x);
    }
    int loop=0;
    int sum ;
    if(v.size()>1)
    {
    sum = abs(accumulate(v.begin(), v.end(), 0));
    }
    else {sum = v[0];}
    while(v.size()-1)
    {
        for (size_t i = v.size() - 1; i > 0; i--) {
        v[i] = v[i] - v[i - 1]; // Update current index with difference
    }
    
    v.erase(v.begin());
    if (abs(accumulate(v.begin(), v.end(), 0))>sum)
    {
        sum = abs(accumulate(v.begin(), v.end(), 0));
    }

    }
    cout<<sum<<endl;
 }
}


Submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
using namespace std;

int main() {
	int t;
	cin>>t;
	while (t--)
	{  
	    int n, x;
        cin >> n; 
        vector<int> v;

        for (int i = 0; i < n; i++) {
        cin >> x;
        v.push_back(x);
    }
    int loop=0;
    int sum ;
    if(v.size()>1)
    {
    sum = accumulate(v.begin(), v.end(), 0);
    }
    else {sum = v[0];}
    while(v.size()-1)
    {
        for (size_t i = v.size() - 1; i > 0; i--) {
        v[i] = v[i] - v[i - 1]; // Update current index with difference
    }
    
    v.erase(v.begin());
    if (abs(accumulate(v.begin(), v.end(), 0))>sum)
    {
        sum = abs(accumulate(v.begin(), v.end(), 0));
    }

    }
    cout<<sum<<endl;
 }
}
Problem: 2062C - Cirno and Operations
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
using namespace std;

int main() {
	int t;
	cin>>t;
	while (t--)
	{  
	    int n, x;
        cin >> n; 
        vector<int> v;

        for (int i = 0; i < n; i++) {
        cin >> x;
        v.push_back(x);
    }
    int loop=0;
    int sum ;
    if(v.size()>1)
    {
    sum = accumulate(v.begin(), v.end(), 0);
    }
    else {sum = v[0];}
    while(v.size()-1)
    {
        for (size_t i = v.size() - 1; i > 0; i--) {
        v[i] = v[i] - v[i - 1]; // Update current index with difference
    }
    
    v.erase(v.begin());
    if (abs(accumulate(v.begin(), v.end(), 0))>sum)
    {
        sum = abs(accumulate(v.begin(), v.end(), 0));
    }

    }
    cout<<sum<<endl;
 }
}


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

int main() {
    long long t;
    cin >> t;
    while (t--) {  
        long long n, x;
        cin >> n; 
        vector<long long> v;

        for (long long i = 0; i < n; i++) {
            cin >> x;
            v.push_back(x);
        }

        long long loop = 0;
        long long sum;
        if (v.size() > 1) {
            sum = accumulate(v.begin(), v.end(), 0LL); // Use 0LL to indicate long long initial value
        } else {
            sum = v[0];
        }

        while (v.size() - 1) {
            for (size_t i = v.size() - 1; i > 0; i--) {
                v[i] = v[i] - v[i - 1]; // Update current index with difference
            }

            v.erase(v.begin());
            if (abs(accumulate(v.begin(), v.end(), 0LL)) > sum) {
                sum = abs(accumulate(v.begin(), v.end(), 0LL));
            }
        }
        
        cout << sum << endl;
    }

    return 0;
}
Problem: 2060D - Subtract Min Sort
Language: C++17 (GCC 7-32)


Submission (verdict: Wrong answer on test 2):

/* Programmed by : https://www.linkedin.com/in/ritik-shankar-537741264/ */

#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> p32;
typedef pair<ll, ll> p64;
typedef pair<double, double> pdd;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<vector<int>> vv32;
typedef vector<vector<ll>> vv64;
typedef vector<vector<p64>> vvp64;
typedef vector<p64> vp64;
typedef vector<p32> vp32;
ll MOD = 998244353;
double eps = 1e-12;
#define forn(i, e) for (ll i = 0; i < e; i++)
#define forsn(i, s, e) for (ll i = s; i < e; i++)
#define rforn(i, s) for (ll i = s; i >= 0; i--)
#define rforsn(i, s, e) for (ll i = s; i >= e; i--)
#define ln "\n"
#define dbg(x) cout << #x << " = " << x << ln
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define INF 2e18
#define fast_cin()                    \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())

void solve()
{
    int n;
    cin>>n;
    vector<int> arr(n);
    forn(i,n)cin>>arr[i];
    if(is_sorted(all(arr))){
        cout<<"YES"<<endl;
        return;
    }
    bool flag=true;
    for(int i=0;i<n;i++){
        if(arr[i]<=arr[i+1]){
            i++;
        }
        else{
            flag=false;
            break;
        }
    }
    if(flag)cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
}
int main()
{
    fast_cin();
    ll t;
    cin >> t;
    for (int it = 1; it <= t; it++)
    {
        solve();
    }
    return 0;
}
Problem: 2061D - Kevin and Numbers
Language: C++20 (GCC 13-64)


Submission (verdict: Wrong answer on test 3):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T>
using os = tree<T, null_type,less<T>, rb_tree_tag,tree_order_statistics_node_update>;
#define ll long long 
#define pb push_back
#define ceils(x, y) (x + y - 1) / y 
ll fact[1000005], ifact[1000005];
#define M 998244353
ll gcd(ll a, ll b) {if (b > a) {return gcd(b, a);} if (b == 0) {return a;} return gcd(b, a % b);}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
ll exp(ll a, ll b, ll mod = M) {a %= mod; ll res = 1; while (b > 0) {if (b & 1)res = (res * a) % mod; a = (a * a) % mod; b = b >> 1;} return res;}
ll inv(ll a, ll b = M) {return exp(a, b - 2, b);}
ll nCr(ll n, ll r, ll m = M) {ll val1 = fact[n]; ll val2 = ifact[n - r]; ll val3 = ifact[r]; return (((val1 * val2) % m) * val3) % m;}
ll add(ll a, ll b, ll m = M) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;}
ll mul(ll a, ll b, ll m = M) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;}
ll sub(ll a, ll b, ll m = M) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;}
ll dvd(ll a, ll b, ll m = M) {a = a % m; b = b % m; return (mul(a, inv(b, m), m) + m) % m;}

// ------------------------------------------------------- main code ----------------------------------------------------------------//

void solve(ll test) {
  priority_queue<ll,vector<ll>,greater<ll>> pq;
  ll n,m;cin >> n >> m;
  vector<ll>a(n),b(m);
  for(int i = 0;i < n;i++) {
    cin >> a[i];
    pq.push(a[i]);
  }
  for(int i = 0;i < m;i++) {
    cin >> b[i];
  }
  sort(b.begin(),b.end());
  ll i = 0;
  while(i < m) {
    if(pq.top() == b[i]) {
      i++;
      pq.pop();
    }else {
      ll f = pq.top();pq.pop();
      if(f > b[i]) {
        cout << "No\n";return;
      }
      if(pq.size() == 0) {
        cout << "No\n";return;
      }
      ll s = pq.top();pq.pop();
      if(abs(f - s) > 1) {
        cout << "No\n";return;
      }
      pq.push(f + s);
    }
  }
  if(pq.size() > 0)cout << "No\n";
  else cout << "Yes\n";
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);cout.tie(0);
  int t = 1; 
  cin >> t;
  // fact[0] = 1;
  // for(int i = 1;i <= 1000005;i++) {
  //   fact[i] = mul(i,fact[i - 1]);
  // }
  // ifact[1000005] = inv(fact[1000005]);
  // ifact[0] = 1;
  // for(int i = 1000005 - 1;i >= 1;i--) {
  //   ifact[i] = mul(i + 1,ifact[i + 1]);
  // }
  while(t > 0) {
    solve(t);
    t--;
  }
}


//seperate case for k = 1
// for k > 1 
//
Problem: 2057B - Gorilla and the Exam
Language: C++17 (GCC 7-32)

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

int solve(int n, int k, vector<int> &a)
{
    unordered_map<int, int> freq;
    for (int i = 0; i < n; ++i)
    {
        freq[a[i]]++;
    }

    if (k == 0)
    {
        return freq.size();
    }

    map<int, int> freq_count;
    for (auto &p : freq)
    {
        freq_count[p.second]++;
    }

    int ans = freq_count.size();
    for (auto &entry : freq_count)
    {
        int count_value = entry.first;
        int count_frequency = entry.second;

        if (k >= count_value * count_frequency)
        {
            k -= count_value * count_frequency;
            ans -= count_frequency;
        }
        else
        {
            int possible_removals = k / count_value;
            ans -= possible_removals;
            break;
        }
    }

    return max(1, ans);
}

int main()
{
    int t;
    cin >> t;
    vector<int> ans;
    while (t--)
    {
        int n, k;
        cin >> n >> k;

        vector<int> a(n);
        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
        }
        ans.push_back(solve(n, k, a));
    }

    for (int &i : ans)
        cout << i << endl;

    return 0;
}


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

int main()
{
    int t;
    cin >> t;
    vector<int> ansvec;
    while (t--)
    {
        int n, k;
        cin >> n >> k;
        map<int, int> freq;
        int temp;
        vector<int> f;
        for (int i = 0; i < n; i++)
        {
            cin >> temp;
            freq[temp]++;
        }
        for (auto &x : freq)
            f.push_back(x.second);
        sort(f.begin(), f.end());
        temp = 0;
        int ans = f.size();
        while (k >= f[temp] && ans > temp)
        {
            k -= f[temp];
            temp++;
        }
        ans = ans - temp;
        ans = max(1, ans);
        ansvec.push_back(ans);
    }
    for (int &i : ansvec)
        cout << i << endl;
    return 0;
}
Problem: 2057B - Gorilla and the Exam
Language: C++17 (GCC 7-32)

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

int solve(int n, int k, vector<int> &a)
{
    unordered_map<int, int> freq;
    for (int i = 0; i < n; ++i)
    {
        freq[a[i]]++;
    }
    if (k == 0)
    {
        return freq.size();
    }

    vector<pair<int, int>> vec(freq.begin(), freq.end());

    sort(vec.begin(), vec.end(), [](const pair<int, int> &a, const pair<int, int> &b)
         { return a.second < b.second; });
    int ans = vec.size();
    for (auto &m : vec)
    {
        if (k >= m.second)
        {
            k -= m.second;
            ans--;
        }
        else
            break;
    }
    ans = (ans > 0) ? ans : 1;
    return ans;
}

int main()
{
    int t;
    cin >> t;
    vector<int> ans;
    while (t--)
    {
        int n, k;
        cin >> n >> k;

        vector<int> a(n);
        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
        }
        ans.push_back(solve(n, k, a));
    }
    for (int &i : ans)
        cout << i << endl;
    return 0;
}


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

int solve(int n, int k, vector<int> &a)
{
    unordered_map<int, int> freq;
    for (int i = 0; i < n; ++i)
    {
        freq[a[i]]++;
    }

    if (k == 0)
    {
        return freq.size();
    }

    map<int, int> freq_count;
    for (auto &p : freq)
    {
        freq_count[p.second]++;
    }

    int ans = freq_count.size();
    for (auto &entry : freq_count)
    {
        int count_value = entry.first;
        int count_frequency = entry.second;

        if (k >= count_value * count_frequency)
        {
            k -= count_value * count_frequency;
            ans -= count_frequency;
        }
        else
        {
            int possible_removals = k / count_value;
            ans -= possible_removals;
            break;
        }
    }

    return max(1, ans);
}

int main()
{
    int t;
    cin >> t;
    vector<int> ans;
    while (t--)
    {
        int n, k;
        cin >> n >> k;

        vector<int> a(n);
        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
        }
        ans.push_back(solve(n, k, a));
    }

    for (int &i : ans)
        cout << i << endl;

    return 0;
}
Problem: 2057B - Gorilla and the Exam
Language: C++17 (GCC 7-32)


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

int solve(int n, int k, vector<int> &a)
{
    unordered_map<int, int> freq;
    for (int i = 0; i < n; ++i)
    {
        freq[a[i]]++;
    }
    if (k == 0)
    {
        return freq.size();
    }

    vector<pair<int, int>> vec(freq.begin(), freq.end());

    sort(vec.begin(), vec.end(), [](const pair<int, int> &a, const pair<int, int> &b)
         { return a.second < b.second; });
    int ans = vec.size();
    for (auto &m : vec)
    {
        if (k >= m.second)
        {
            k -= m.second;
            ans--;
        }
        else
            break;
    }
    ans = (ans > 0) ? ans : 1;
    return ans;
}

int main()
{
    int t;
    cin >> t;
    vector<int> ans;
    while (t--)
    {
        int n, k;
        cin >> n >> k;

        vector<int> a(n);
        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
        }
        ans.push_back(solve(n, k, a));
    }
    for (int &i : ans)
        cout << i << endl;
    return 0;
}
Problem: 2050D - Digital string maximization
Language: C++17 (GCC 7-32)

Previous submission (verdict: Time limit exceeded on test 3):
#include <bits/stdc++.h>

using namespace std;



#define ll long long





void dfs(string curr, string &prev) {

    if (curr > prev)  

        prev = curr;



    for (ll i = 1; i < curr.length(); i++) {

        if (curr[i] == '0') continue;  

        

        char ch = curr[i] - 1;

        

        if (ch > curr[i - 1]) {  

            

            curr[i] = ch;

            swap(curr[i], curr[i - 1]);



            dfs(curr, prev);  



            swap(curr[i], curr[i - 1]);  

            curr[i] +=1;

        }

    }

}



void solve() {

    string s;

    cin >> s;

    

    string prev = s;

    

    dfs(s, prev);

    

    cout<<prev<<endl;

}



int main() {

    ios_base::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);

    ll t;

    cin >> t;

    for (ll i = 0; i < t; i++) {

        solve();

    }

    return 0;

}









Submission (verdict: Time limit exceeded on test 6):
#include <bits/stdc++.h>

using namespace std;



#define ll long long





// void dfs(string curr, string &prev) {

//     if (curr > prev)  

//         prev = curr;



//     for (ll i = 1; i < curr.length(); i++) {

//         if (curr[i] == '0') continue;  

        

//         char ch = curr[i] - 1;

        

//         if (ch > curr[i - 1]) {  

            

//             curr[i] = ch;

//             swap(curr[i], curr[i - 1]);



//             dfs(curr, prev);  



//             swap(curr[i], curr[i - 1]);  

//             curr[i] +=1;

//         }

//     }

// }



void solve() {

    string s;

    cin >> s;

    

    ll n = s.size();

    

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

        for(ll j=n-1; j>=i; j--){

            if(s[j]=='0')

               continue;

            

            char ch = s[j] - 1;

            

            if(ch > s[j-1]){

                s[j] = ch;

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

            }

        

        }

    }

    

    cout<<s<<endl;

}



int main() {

    ios_base::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);

    ll t;

    cin >> t;

    for (ll i = 0; i < t; i++) {

        solve();

    }

    return 0;

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


Submission (verdict: Time limit exceeded on test 3):
#include <bits/stdc++.h>

using namespace std;



#define ll long long





void dfs(string curr, string &prev) {

    if (curr > prev)  

        prev = curr;



    for (ll i = 1; i < curr.length(); i++) {

        if (curr[i] == '0') continue;  

        

        char ch = curr[i] - 1;

        

        if (ch > curr[i - 1]) {  

            

            curr[i] = ch;

            swap(curr[i], curr[i - 1]);



            dfs(curr, prev);  



            swap(curr[i], curr[i - 1]);  

            curr[i] +=1;

        }

    }

}



void solve() {

    string s;

    cin >> s;

    

    string prev = s;

    

    dfs(s, prev);

    

    cout<<prev<<endl;

}



int main() {

    ios_base::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);

    ll t;

    cin >> t;

    for (ll i = 0; i < t; i++) {

        solve();

    }

    return 0;

}