Problem: 2050C - Uninteresting Number
Language: C++23 (GCC 14-64, msys2)


Submission (verdict: Wrong answer on test 2):
#include <unordered_map>
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
#include <iomanip>
#include <numeric>
using namespace std;

template <typename T>
void printVector(const vector<T>& vec) {
    for (const T& val : vec) {
        cout << val << " ";
    }
    cout << endl;
}
void print2DVector(vector<vector<int>> arr) {
    for (int i = 0; i < arr.size(); i++)
    {
        cout << '[' << " ";
        for (int j = 0; j < arr[i].size(); j++)
        {
            cout << arr[i][j] << " ";
        }
        cout << ']';
        
    }
    
    cout << endl;
}

int indexOf(const vector<int>& lst, int x) {
    for (size_t i = 0; i < lst.size(); i++) {
        if (lst[i] == x) {
            return static_cast<int>(i); 
        }
    }
    return -1;
}

int min(long long a, long long b) {
    if (a > b) {
        return b;
    } 
    return a;
}

bool is_prime(int x) {
    if (x == 1) {
        return false;
    }
    for (size_t i = 2; i <= sqrt(x); i++)
    {
        if (x % i == 0) {
            return false;
        }
    }
    return true;
}


int main() {
    int t;
    cin >> t;
    cin.ignore();
    for (int sfe = 0; sfe < t; ++sfe) {
        string n;
        cin >> n;
        long long basicSum = 0;
        vector<int> arr;
        long long arrsm = 0;
        long long tws = 0;
        long long trs = 0;
        for (size_t i = 0; i < n.size(); i++)
        {
            if ((n[i] - '0') == 3) {
                int x = pow(n[i] - '0', 2);
                arr.push_back(x);
                arrsm += x;
                trs += 1;
            }
            else if ((n[i] - '0') == 2) {
                int x = pow(n[i] - '0', 2);
                arr.push_back(x);
                tws += 1;
                arrsm += x;
            }
            basicSum += n[i] - '0';
        }
        if (basicSum % 9 == 0) {
            cout << "YES" << endl;
        }
        else {
            vector<vector<int>> dp;
            dp.push_back({});
            for (size_t ps = 0; ps < arrsm; ps++)
            {
                if (ps == 3) {
                    dp.push_back({4});
                }
                else if (ps == 8) {
                    dp.push_back({9});
                }
                else {
                    dp.push_back({});
                }
            }

            string ans = "NO";
            for (size_t i = 4; i < dp.size(); i++)
            {
                if (!dp[i].empty()) {
                    if (trs > count(dp[i].begin(), dp[i].end(), 9)) {
                        if (i + 9 < dp.size()) {
                            vector<int> lst = dp[i];
                            lst.push_back(9);
                            dp[i + 9] = lst;
                            long long sm = accumulate(lst.begin(), lst.end(), 0) + basicSum;
                            for (size_t l = 0; l < lst.size(); l++)
                            {
                                sm -= sqrt(lst[l]);
                            }
                            
                            // cout << "+++++++" << sm << "++++++++++++" << endl;
                            if ((sm) % 9 == 0) {
                                ans = "YES";
                                break;
                            }
                        }
                    }
                        
                    if (tws > count(dp[i].begin(), dp[i].end(), 4)) {
                        if (i + 4 < dp.size()) {
                            vector<int> lst = dp[i];
                            lst.push_back(4);
                            dp[i + 4] = lst;
                            long long sm = accumulate(lst.begin(), lst.end(), 0) + basicSum;
                            for (size_t l = 0; l < lst.size(); l++)
                            {
                                sm -= sqrt(lst[l]);
                            }
                            // cout << "+++++++" << sm << "++++++++++++" << endl;
                            if ((sm - 2) % 9 == 0) {
                                ans = "YES";
                                break;
                            }
                        }
                    }
                }
            }
            // cout << tws << " " << trs << " " << arrsm << endl;
            // for (size_t pr = 0; pr < dp.size(); pr++)
            // {
            //     if (!dp[pr].empty()) {
            //         for (size_t cs = 0; cs < dp[pr].size(); cs++)
            //         {
            //             cout << dp[pr][cs] << " ";
            //         }
            //         cout << endl;
            //     }
            // }
            
            cout << ans << endl;
        }
    }
        
        // ------------Input--------------
        

    
        return 0;
    };