Problem: 2050C - Uninteresting Number
Language: C++20 (GCC 13-64)

Previous submission (verdict: Time limit exceeded on test 2):
#include <algorithm>
#include <bits/stdc++.h>
#include <fstream> //includes file stream: write to files and open and use data from files
#include <iostream>
#include <iterator>
#include <map>    //includes map datatype
#include <math.h> //includes math operations like min, max, absolute value, power
#include <queue>
#include <set>
#include <stack>
#include <string> //includes string datatype
#include <vector> //includes vector datastructre: a list of variable size
using namespace std;
using ll = long long;
 
ifstream fin("x.in");   // opens file x.in for reading
ofstream fout("x.out"); // opens or creates file x.out for writing
 
// minus is 1 plus is 0
 
void solve() {
  string n;
  cin >> n;
  bool check = false;

  ll size = n.size();
  ll sum = 0;
  ll twos = 0;
  ll threes = 0;

  for (ll i = 0; i < size; ++i) {
    char sub = n[i];
    sum += (ll)sub - 48;
    if ((ll)sub - 48 == 2) {
      ++twos;
    } else if ((ll)sub - 48 == 3) {
      ++threes;
    }
  }

if (sum % 9 == 0) {
    cout << "YES" << "\n";
  } else {
    while (sum % 9 != 0) {
      if (twos == 0 && threes == 0 && sum % 9 != 0) {
        cout << "NO" << "\n";
        break;
      } else if ((sum + 2) % 9 == 0 && twos != 0) {
        cout << "YES" << "\n";
        break; 
      } else if ((sum + 6) % 9 == 0 && threes != 0) {
        cout << "YES" << "\n";
        break;
      } else if ((sum + 2) % 9 != 0 && threes != 0) {
        sum += 6;
        threes--;
      } else if (threes == 0 && twos != 0) {
        sum += 2;
        twos--;
      }
  }
  }
}

int main() {

  ll t;
  cin >> t;
  while(t--) {
  solve();
  }
 
  return 0;
}

Submission (verdict: Time limit exceeded on test 2):
#include <algorithm>
#include <bits/stdc++.h>
#include <fstream> //includes file stream: write to files and open and use data from files
#include <iostream>
#include <iterator>
#include <map>    //includes map datatype
#include <math.h> //includes math operations like min, max, absolute value, power
#include <queue>
#include <set>
#include <stack>
#include <string> //includes string datatype
#include <vector> //includes vector datastructre: a list of variable size
using namespace std;
using ll = long long;
 
ifstream fin("x.in");   // opens file x.in for reading
ofstream fout("x.out"); // opens or creates file x.out for writing
 
// minus is 1 plus is 0
 
void solve() {
  string n;
  cin >> n;
  bool check = false;

  ll size = n.size();
  ll sum = 0;
  ll twos = 0;
  ll threes = 0;

  for (ll i = 0; i < size; ++i) {
    char sub = n[i];
    sum += (ll)sub - 48;
    if ((ll)sub - 48 == 2) {
      ++twos;
    } else if ((ll)sub - 48 == 3) {
      ++threes;
    }
  }

if (sum % 9 == 0) {
    cout << "YES" << "\n";
    return;
  } else {
    while (sum % 9 != 0) {
      if (twos == 0 && threes == 0 && sum % 9 != 0) {
        cout << "NO" << "\n";
        return;
      } else if ((sum + 2) % 9 == 0 && twos != 0) {
        cout << "YES" << "\n";
        return; 
      } else if ((sum + 6) % 9 == 0 && threes != 0) {
        cout << "YES" << "\n";
        return;
      } else if ((sum + 2) % 9 != 0 && threes != 0) {
        sum += 6;
        threes--;
      } else if (threes == 0 && twos != 0) {
        sum += 2;
        twos--;
      }
  }
  }
}

int main() {

  ll t;
  cin >> t;
  while(t--) {
  solve();
  }
 
  return 0;
}