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

Previous submission (verdict: Runtime error on test 2):
// ngmiverse
#include <bits/stdc++.h>

#define cases int _t; cin >> _t; while (_t --) solve();
#define mp make_pair
#define f first
#define s second
#define sz(x) int((x).size())
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define rall(x) x.rbegin(), x.rend()
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define pb push_back
#define eb emplace_back
#define pf push_front
#define rtn return
#define endl '\n'

#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define fpr(v, c) F0R(_i, (v).size()) cout << v[_i] << ((_i == ((v).size() - 1)) ? "\n" : (c))
#define R0F(i,a) ROF(i,0,a)
#define rep(a) F0R(_,a)
#define each(a,x) for (auto& a: x)
#define MOD9 998244353
#define MOD10 1000000007

using namespace std;
using ll = long long;
using db = long double;
using str = string;

using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<db, db>;

using vi = vector<int>;
using vb = vector<bool>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vd = vector<db>;
using vs = vector<str>;
using vpi = vector<pi>;
using vvpi = vector<vpi>;
using vpl = vector<pl>;
using vpd = vector<pd>;

struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
//
//struct segtree{
//    typedef ll T; //replace this
//
//    T id = 0, t[2 * N];
//    // id is identity: 1 for mult, 0 for add, ll max for min, ll min for max
//    // Make sure N is >= siz of array
//
//    T func(T a, T b) { return a+b; } // replace this
//
//    void modify(ll p, T v) { //set value v at position p
//        for(t[p += N] = v; p /= 2;) t[p] = func(t[2 * p], t[2 * p + 1]);
//    }
//
//    T query(ll l, ll r) { //query on interval [l, r)
//        T resl = id, resr = id;
//        for(l += N, r += N; l < r; l /= 2, r /= 2) {
//            if(l & 1) resl = func(resl, t[l++]);
//            if(r & 1) resr = func(t[--r], resr);
//        }
//        return func(resl, resr);
//    }
//};

void solve(){
    string s; cin >> s;
//    set<pi> pq;
//    set<pi> frnt;
    vi vals(s.size());
    list<int> lt;
    F0R(i, s.size()) {
        int val = s[i] - '0';
        vals[val] = i;
        lt.push_back(val);
//        pq.emplace(i - val, i);
    }
    vi out(s.size());
    F0R(i, s.size()) {
        int curr = -1;
        int j = 0;
        decltype(lt.begin()) best;
        for(auto itr = lt.begin(); j < 9 && itr != lt.end(); ++j, ++itr) {
            int next = *itr - j;
            if (next > curr) {
                best = itr;
                curr = next;
            }
        }
        lt.erase(best);
        out[i] = curr;
    }
//    F0R(i, s.size()) {
//        int best = 0;
//        int bestIndex = -1;
//        R0F(j, 10) {
//            if (vals[j].empty()) continue;
//            int penalty = j;
//            if (vals[j].front() > i) penalty -= vals[j].front() - i;
//            if (penalty > best) {
//                best = penalty;
//                bestIndex = j;
//            }
//        }
//        out[i] = best;
//        cout << bestIndex << endl;
//        if (bestIndex != -1) vals[bestIndex].pop_front();
//    }
//    F0R(i, s.size()) {
////        cout << v[i].first << " " << v[i].second << endl;
//        assert(!frnt.empty() || !pq.empty());
//        pi p;
//        if (!frnt.empty()) p = *frnt.end();
//        if (!pq.empty()) {
//            auto q = *pq.begin();
//
//        }
//        int diff = max(0, v[i].second - i);
//        out[i] = s[i] - '0' - diff;
//    }
    fpr(out, "");
}

int main() {
    ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
    cases
//    solve();
    return 0;
}

Submission (verdict: Runtime error on test 2):
// ngmiverse
#include <bits/stdc++.h>

#define cases int _t; cin >> _t; while (_t --) solve();
#define mp make_pair
#define f first
#define s second
#define sz(x) int((x).size())
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define rall(x) x.rbegin(), x.rend()
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define pb push_back
#define eb emplace_back
#define pf push_front
#define rtn return
#define endl '\n'

#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define fpr(v, c) F0R(_i, (v).size()) cout << v[_i] << ((_i == ((v).size() - 1)) ? "\n" : (c))
#define R0F(i,a) ROF(i,0,a)
#define rep(a) F0R(_,a)
#define each(a,x) for (auto& a: x)
#define MOD9 998244353
#define MOD10 1000000007

using namespace std;
using ll = long long;
using db = long double;
using str = string;

using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<db, db>;

using vi = vector<int>;
using vb = vector<bool>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vd = vector<db>;
using vs = vector<str>;
using vpi = vector<pi>;
using vvpi = vector<vpi>;
using vpl = vector<pl>;
using vpd = vector<pd>;

struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
//
//struct segtree{
//    typedef ll T; //replace this
//
//    T id = 0, t[2 * N];
//    // id is identity: 1 for mult, 0 for add, ll max for min, ll min for max
//    // Make sure N is >= siz of array
//
//    T func(T a, T b) { return a+b; } // replace this
//
//    void modify(ll p, T v) { //set value v at position p
//        for(t[p += N] = v; p /= 2;) t[p] = func(t[2 * p], t[2 * p + 1]);
//    }
//
//    T query(ll l, ll r) { //query on interval [l, r)
//        T resl = id, resr = id;
//        for(l += N, r += N; l < r; l /= 2, r /= 2) {
//            if(l & 1) resl = func(resl, t[l++]);
//            if(r & 1) resr = func(t[--r], resr);
//        }
//        return func(resl, resr);
//    }
//};

void solve(){
    string s; cin >> s;
//    set<pi> pq;
//    set<pi> frnt;
    vi vals(s.size());
    list<int> lt;
    F0R(i, s.size()) {
        int val = s[i] - '0';
        vals[val] = i;
        lt.push_back(val);
//        pq.emplace(i - val, i);
    }
    vi out(s.size());
    F0R(i, s.size()) {
        int curr = *lt.begin();
        auto best = lt.begin();
        int j = 0;
        for(auto itr = lt.begin(); j < 9 && itr != lt.end(); ++j, ++itr) {
            if (itr == lt.end()) break;
            int next = *itr - j;
            if (next > curr) {
                best = itr;
                curr = next;
            }
        }
        lt.erase(best);
        out[i] = curr;
    }
//    F0R(i, s.size()) {
//        int best = 0;
//        int bestIndex = -1;
//        R0F(j, 10) {
//            if (vals[j].empty()) continue;
//            int penalty = j;
//            if (vals[j].front() > i) penalty -= vals[j].front() - i;
//            if (penalty > best) {
//                best = penalty;
//                bestIndex = j;
//            }
//        }
//        out[i] = best;
//        cout << bestIndex << endl;
//        if (bestIndex != -1) vals[bestIndex].pop_front();
//    }
//    F0R(i, s.size()) {
////        cout << v[i].first << " " << v[i].second << endl;
//        assert(!frnt.empty() || !pq.empty());
//        pi p;
//        if (!frnt.empty()) p = *frnt.end();
//        if (!pq.empty()) {
//            auto q = *pq.begin();
//
//        }
//        int diff = max(0, v[i].second - i);
//        out[i] = s[i] - '0' - diff;
//    }
    fpr(out, "");
}

int main() {
    ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
    cases
//    solve();
    return 0;
}