Problem: 2050C - Uninteresting Number
Language: C++17 (GCC 7-32)

Previous submission (verdict: Time limit exceeded on test 3):
#include <iostream>
#include <vector>
#include <queue>
#include <unordered_map>
using namespace std;

#define ll long long
#define ull unsigned long long
#define vi vector<int>

void solve()
{
    string s;
    cin >> s;

    int shuma = 0;

    for (auto &val : s)
        shuma += val - '0';

    if (shuma % 9 == 0)
    {
        cout << "YES";

        return;
    }

    bool mundet = false;

    queue<pair<string, int>> q; // pair<numri, shuma>;
    q.push({s, shuma});

    unordered_map<string, int> memo;
    while (!q.empty())
    {
        auto [nr, sh] = q.front();
        q.pop();

        if (sh % 9 == 0)
        {
            mundet = true;
            break;
        }

        for (int i = 0; i < nr.length(); i++)
        {
            if (nr[i] == '2')
            {
                sh -= 2;
                sh += 4;
                nr[i] = '4';

                if (memo.find(nr) == memo.end())
                {
                    q.push({nr, sh});
                    memo[nr] = 1;
                }
                sh -= 4;
                sh += 2;
                nr[i] = '2';
            }

            if (nr[i] == '3')
            {
                sh -= 3;
                sh += 9;
                nr[i] = '9';

                if (memo.find(nr) == memo.end())
                {
                    q.push({nr, sh});
                    memo[nr] = 1;
                }

                sh -= 9;
                sh += 3;
                nr[i] = '3';
            }
        }
    }

    if (mundet)
        cout << "YES";
    else
        cout << "NO";
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(NULL);

    int t;
    cin >> t;

    while (t--)
    {

        solve();
        cout << '\n';
    }

    return 0;
}

Submission (verdict: Time limit exceeded on test 3):
#include <iostream>
#include <vector>
#include <queue>
#include <unordered_map>
using namespace std;

#define ll long long
#define ull unsigned long long
#define vi vector<int>

void solve()
{
    string s;
    cin >> s;

    int shuma = 0;

    for (auto &val : s)
        shuma += val - '0';

    if (shuma % 9 == 0)
    {
        cout << "YES";

        return;
    }

    bool mundet = false;

    queue<pair<string, int>> q; // pair<numri, shuma>;
    q.push({s, shuma});

    unordered_map<string, int> memo;
    while (!q.empty())
    {
        auto [nr, sh] = q.front();
        q.pop();

        if (sh % 9 == 0)
        {
            mundet = true;
            break;
        }

        int dysha = 0, tresha = 0;

        for (int i = 0; i < nr.length(); i++)
        {
            if (nr[i] == '2' && dysha < 9) 
            {
                dysha++;
                sh -= 2;
                sh += 4;
                nr[i] = '4';

                if (memo.find(nr) == memo.end())
                {
                    q.push({nr, sh});
                    memo[nr] = 1;
                }
                sh -= 4;
                sh += 2;
                nr[i] = '2';
            }

            if (nr[i] == '3' && tresha < 9)
            {
                tresha ++;
                sh -= 3;
                sh += 9;
                nr[i] = '9';

                if (memo.find(nr) == memo.end())
                {
                    q.push({nr, sh});
                    memo[nr] = 1;
                }

                sh -= 9;
                sh += 3;
                nr[i] = '3';
            }
        }
    }

    if (mundet)
        cout << "YES";
    else
        cout << "NO";
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(NULL);

    int t;
    cin >> t;

    while (t--)
    {

        solve();
        cout << '\n';
    }

    return 0;
}