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

Previous submission (verdict: Wrong answer on test 1):
// Thank God!

#include <bits/stdc++.h>

#include <cstdint>

#include <numeric>

using namespace std;



// Aliases for simple data types

using ll = long long;

using dd = long double;

using mpi = map<int, int>;

using mpl = map<ll, ll>;

using ull = unsigned long long;



// Constant definitions

#define PI 3.14159265358979323

#define E 2.7182818284590

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



// BIG NUMBERS (MODULAR)

const ll MOD = 998244353;

const ll MOD1 = 998244353;

const ll MOD2 = (ll)1e9 + 7;



// simplified aliases for vectors,pairs

template <class T>

using v = vector<T>;

template <class T>

using p = pair<T, T>;



// overloaded instream operators

template <class T1, class T2>

istream &operator>>(istream &in, pair<T1, T2> &p)

{

in >> p.first >> p.second;

return in;

}

template <class T>

istream &operator>>(istream &in, v<T> &v)

{

for (auto &x : v)

in >> x;

return in;

}



// a^b mod MOD

ll POW(ll a, ll b)

{

ll res = 1;

ll m = a;

while (b)

{

if (b & 1)

res = (res * m) % MOD;

m *= m;

m %= MOD;

b >>= 1;

}

return res;

}



// a^-1 mod MOD

ll INV(ll a)

{

return POW(a, MOD - 2);

}



template <typename T>

class Printer

{

private:

int sz;

vector<T> a;



public:

Printer(vector<T> &a) : a(a) {};

Printer(vector<T> &a, int &sz) : a(a), sz(sz) {};

void print(const vector<T> &arr)

{

for (auto &o : arr)

cout << o << " ";

}

void print()

{

print(a);

}

};



// MULTITEST TOGGLE

const bool test = true;

// INTERACTIVE TOGGLE

const bool fileInput = false;

/*-------------------------------------------------------------------------------*/



void solve()

{

string s;

cin >> s;

// Key observation: every s[i] can only be moved at most 9 times

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

{

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

int ind = -1;

int mx = val;

for (int j = i + 1; j <= min((int)s.length(), i + 9); j++)

{

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

{

ind = j;

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

}

}

if (ind != -1)

{

swap(s[i], s[ind]);

s[i] = mx + '0';

}

}

cout << s << "\n";

}

int main()

{

/*Command Line

g++ forkiespad.cpp -o forkiespad

forkiespad.exe

*/



// file input

if (fileInput)

{

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

}

ios::sync_with_stdio(false);

cin.tie(0);



// testcases

int t;

if (test)

{

cin >> t;

}

else

t = 1;



while (t--)

{

solve();

}

}



/*

-try out more cases

-attack the problem creatively/at different angles

-what am i missing?

-CHECK YOUR CODE

-pay attention to BOUNDS

-errors - out of bounds, edge case(n=0, n=1?), integer overflow, etc.

-WHEN IN DOUBT, USE LONG LONG

-check the for loops

-watch out for division errors (use double and ll when needed)

*/

// Thank God!

Submission (verdict: Wrong answer on test 1):
// Thank God!

#include <bits/stdc++.h>

#include <cstdint>

#include <numeric>

using namespace std;



// Aliases for simple data types

using ll = long long;

using dd = long double;

using mpi = map<int, int>;

using mpl = map<ll, ll>;

using ull = unsigned long long;



// Constant definitions

#define PI 3.14159265358979323

#define E 2.7182818284590

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



// BIG NUMBERS (MODULAR)

const ll MOD = 998244353;

const ll MOD1 = 998244353;

const ll MOD2 = (ll)1e9 + 7;



// simplified aliases for vectors,pairs

template <class T>

using v = vector<T>;

template <class T>

using p = pair<T, T>;



// overloaded instream operators

template <class T1, class T2>

istream &operator>>(istream &in, pair<T1, T2> &p)

{

in >> p.first >> p.second;

return in;

}

template <class T>

istream &operator>>(istream &in, v<T> &v)

{

for (auto &x : v)

in >> x;

return in;

}



// a^b mod MOD

ll POW(ll a, ll b)

{

ll res = 1;

ll m = a;

while (b)

{

if (b & 1)

res = (res * m) % MOD;

m *= m;

m %= MOD;

b >>= 1;

}

return res;

}



// a^-1 mod MOD

ll INV(ll a)

{

return POW(a, MOD - 2);

}



template <typename T>

class Printer

{

private:

int sz;

vector<T> a;



public:

Printer(vector<T> &a) : a(a) {};

Printer(vector<T> &a, int &sz) : a(a), sz(sz) {};

void print(const vector<T> &arr)

{

for (auto &o : arr)

cout << o << " ";

}

void print()

{

print(a);

}

};



// MULTITEST TOGGLE

const bool test = true;

// INTERACTIVE TOGGLE

const bool fileInput = false;

/*-------------------------------------------------------------------------------*/



void solve()

{

string s;

cin >> s;

// Key observation: every s[i] can only be moved at most 9 times

// O(n) time

string build = "";

set<int> indices;

int n = s.length();

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

indices.insert(i);

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

{

// remove at most one at every step of the way

int ind = *indices.begin();

int val = s[ind] - '0';

auto it = indices.begin();

int betterInd = -1;

int betterVal = -1;

it++;

int cnt = 1;

while (it != indices.end() && cnt < 10)

{

if ((s[*it] - '0') - *it + ind > val)

{

betterInd = *it;

betterVal = (s[*it] - '0') - *it + ind;

}

cnt++;

it++;

}

if (betterInd != -1)

{

ind = betterInd;

val = betterVal;

}

build += val + '0';

indices.erase(ind);

}

cout << build << "\n";

}

int main()

{

/*Command Line

g++ forkiespad.cpp -o forkiespad

forkiespad.exe

*/



// file input

if (fileInput)

{

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

}

ios::sync_with_stdio(false);

cin.tie(0);



// testcases

int t;

if (test)

{

cin >> t;

}

else

t = 1;



while (t--)

{

solve();

}

}



/*

-try out more cases

-attack the problem creatively/at different angles

-what am i missing?

-CHECK YOUR CODE

-pay attention to BOUNDS

-errors - out of bounds, edge case(n=0, n=1?), integer overflow, etc.

-WHEN IN DOUBT, USE LONG LONG

-check the for loops

-watch out for division errors (use double and ll when needed)

*/

// Thank God!