ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2063C - Remove Exactly Two
Language: C++17 (GCC 7-32)

Previous submission (verdict: Time limit exceeded on test 6):
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair < ll, ll > pp;
const int N = 1e5+2;
void debug(int cnt){
    cout<<"debug "<<cnt<<endl;
}
void solve() {
    int n;cin>>n;
    vector<int> indeg(n+1,0);
    vector<int> vt[n+1];
    for(int i=0;i<n-1;i++){
        int u,v;cin>>u>>v;
        vt[u].push_back(v);
        vt[v].push_back(u);
        indeg[u]++;
        indeg[v]++;
    }
    int res = 0;
    for(int i=1;i<=n;i++){
        int temp = indeg[i];
        for(auto it:vt[i]) indeg[it]--;
        indeg[i] = 0;
        int maxi = *max_element(indeg.begin(),indeg.end());
        res = max(res, temp + maxi-1);
        indeg[i] = temp;
        for(auto it:vt[i]) indeg[it]++;
    }
    cout<<res<<endl;
}
int main() {
    int t;cin>>t;
    while(t--) solve();
}



Submission (verdict: Accepted):
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair < ll, ll > pp;
const int N = 1e5+2;
void debug(int cnt){
    cout<<"debug "<<cnt<<endl;
}
void solve() {
    int n;cin>>n;
    vector<int> indeg(n+1,0);
    vector<int> vt[n+1];
    set<pp,greater<pp> > st;
    for(int i=0;i<n-1;i++){
        int u,v;cin>>u>>v;
        vt[u].push_back(v);
        vt[v].push_back(u);
        indeg[u]++;
        indeg[v]++;
    }
    for(int i=1;i<=n;i++) st.insert({indeg[i],i});
    int res = 0;
    for(int i=1;i<=n;i++){
        for(auto it:vt[i]) 
        {
            st.erase({indeg[it],it});
            st.insert({indeg[it]-1,it});
        }
        st.erase({indeg[i],i});
        // for(auto it:st) cout<<it.first<<" "<<it.second<<"#"<<endl;
        // cout<<endl;
        int maxi = (*st.begin()).first;
        // cout<<i<<" "<<indeg[i]<<" "<<maxi<<endl;
        res = max(res, indeg[i] + maxi-1);
        st.insert({indeg[i],i});
        for(auto it:vt[i]) {
            st.erase({indeg[it]-1,it});
            st.insert({indeg[it],it});
        }
    }
    cout<<res<<endl;
}
int main() {
    int t;cin>>t;
    while(t--) solve();
}
Problem: 2057C - Trip to the Olympiad
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define int long long
#define pii pair<int, int>

int mylog2(int n){
    int res = 0;

    while (n > 1){
        n /= 2;
        res++;
    }

    return res;
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin >> t;
    while(t--){
        int l, r;
        cin >> l >> r;
        
        int log2l = mylog2(l);
        int log2r = mylog2(r);

        if (log2l < log2r){
            int a = (1LL << log2r) - 1;
            int b = (1LL << log2r);
            int c;
            if (r >= b + 1) {
                c = b + 1;
            } else {
                c = a - 1;
            }

            cout << a << " " << b << " " << c << "\n";

        } else {
            int cnst = 0;
            int cur = log2l;
            while ((l >> cur) == (r >> cur)){
                cnst += (1 << cur);
                l -= (1 << cur);
                r -= (1 << cur);
                cur--;
            }

            log2l = mylog2(l);
            int a = cnst + (1LL << log2l) - 1;
            int b = cnst + (1LL << log2l);
            int c;

            if (r >= b + 1) {
                c = b + 1;
            } else {
                c = a - 1;
            }

            cout << a << " " << b << " " << c << "\n";
            
        }

    }
}

Submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define int long long
#define pii pair<int, int>

int mylog2(int n){
    int res = 0;

    while (n > 1){
        n /= 2;
        res++;
    }

    return res;
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin >> t;
    while(t--){
        int l, r;
        cin >> l >> r;
        
        int log2l = mylog2(l);
        int log2r = mylog2(r);

        // cout << log2l << " " << log2r << "\n";

        if (log2l < log2r){
            int a = (1LL << log2r) - 1;
            int b = (1LL << log2r);
            int c;
            if (r >= b + 1) {
                c = b + 1;
            } else {
                c = a - 1;
            }

            // cout << a << " " << b << " " << c << "\n";

        } else {
            int cnst = 0;
            int cur = log2l;
            while ((l >> cur) == (r >> cur)){
                cnst += ((l >> cur) << cur);
                l -= ((l >> cur) << cur);
                r -= ((r >> cur) << cur);
                cur--;
            }

            // cout << cnst << " " << l << " " << r << "\n";

            // log2l = mylog2(l);
            log2r = mylog2(r);
            int a = cnst + (1LL << log2r) - 1;
            int b = cnst + (1LL << log2r);
            int c;

            if (r >= b + 1) {
                c = b + 1;
            } else {
                c = a - 1;
            }

            cout << a << " " << b << " " << c << "\n";
            
        }

    }
}
Problem: 2057C - Trip to the Olympiad
Language: C++23 (GCC 14-64, msys2)


Submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define int long long
#define pii pair<int, int>

int mylog2(int n){
    int res = 0;

    while (n > 1){
        n /= 2;
        res++;
    }

    return res;
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin >> t;
    while(t--){
        int l, r;
        cin >> l >> r;
        
        int log2l = mylog2(l);
        int log2r = mylog2(r);

        if (log2l < log2r){
            int a = (1LL << log2r) - 1;
            int b = (1LL << log2r);
            int c;
            if (r >= b + 1) {
                c = b + 1;
            } else {
                c = a - 1;
            }

            cout << a << " " << b << " " << c << "\n";

        } else {
            int cnst = 0;
            int cur = log2l;
            while ((l >> cur) == (r >> cur)){
                cnst += (1 << cur);
                l -= (1 << cur);
                r -= (1 << cur);
                cur--;
            }

            log2l = mylog2(l);
            int a = cnst + (1LL << log2l) - 1;
            int b = cnst + (1LL << log2l);
            int c;

            if (r >= b + 1) {
                c = b + 1;
            } else {
                c = a - 1;
            }

            cout << a << " " << b << " " << c << "\n";
            
        }

    }
}
Problem: 2057C - Trip to the Olympiad
Language: C++20 (GCC 13-64)


Submission (verdict: Wrong answer on test 2):
//#pragma once
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
using namespace std;

#define REP(i,a,b) for (int i = a; i < (b); i++)
#define rep(i,a,b) for (int i = a; i < (b); i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef set<int> si;
typedef set<long long> sl;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const ll MOD = 1e9+7;
const double pi = acos(-1);


void solve_test_case(){
    ll l, r; cin >> l >> r;
    if(l == 1 && r == 3) {
        cout << 1 << " " << 2 << " " << 3 << "\n";
        return;
    }

    ll a, b, c;
    b = l;
    c = r;
    a = 0;
    for(int j = 30; j >= 0; j--) {
        if((r & (1ll << j)) == (l & (1ll << j))) {
            if(r & (1ll << j)) {
                a |= (1ll << j);
            }
        }else{
            for(int w = j - 1; w >= 0; w--) {
                if((l & (1ll << w)) == 0) a |= (1ll << w);
            }

            if(a < l) a |= (1 << j);
            break;
        }
    }

    cout << a << " " << b << " " << c << "\n";
}


int main(){
    ios::sync_with_stdio(0);
    cout << setprecision(20) << fixed;
    cin.tie(0);
    cout.tie(0);
    int tc = 1;
    cin >> tc;


    rep(i,1,tc + 1) {
        //cout << "Case #" << i << ": ";
        solve_test_case();
    }

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


Submission (verdict: Time limit exceeded on test 6):
#include <bits/stdc++.h>

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

#define lli long long

#define llu unsigned long long

#define vi vector<int>

using namespace std;



void solve() {

    string s;

    cin >> s;

    for (int i = 0; i < s.size() - 1; i++) {

        for (int j = s.size() - 1; j > i; j--) {

            if (s[j] - 1 > s[j - 1]) {

                s[j] = s[j] - 1;

                swap(s[j], s[j - 1]);

            }

        }

    }

    cout << s << '\n';

}



int main() {

    ios::sync_with_stdio(false);

    cin.tie(nullptr);

    int T;

    cin >> T;

    while (T--) {

        solve();

    }

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

Previous submission (verdict: Time limit exceeded on test 6):
#include <bits/stdc++.h>

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

#define lli long long

#define llu unsigned long long

#define vi vector<int>

using namespace std;



void solve() {

    string s;

    cin >> s;

    for (int i = 0; i < s.size() - 1; i++) {

        for (int j = s.size() - 1; j > i; j--) {

            if (s[j] - 1 > s[j - 1]) {

                s[j] = s[j] - 1;

                swap(s[j], s[j - 1]);

            }

        }

    }

    cout << s << '\n';

}



int main() {

    ios::sync_with_stdio(false);

    cin.tie(nullptr);

    int T;

    cin >> T;

    while (T--) {

        solve();

    }

}

Submission (verdict: Time limit exceeded on test 6):
#include <bits/stdc++.h>

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

#define lli long long

#define llu unsigned long long

#define vi vector<int>

using namespace std;



void solve() {

    string s;

    cin >> s;

    bool f = true;

    for (int i = 0; i < s.size() - 1; i++) {

        for (int j = s.size() - 1; j > i; j--) {

            if (s[j] - 1 > s[j - 1]) {

                s[j] = s[j] - 1;

                swap(s[j], s[j - 1]);

                f = false;

            }

        }

        if (f) {

            break;

        }

    }

    cout << s << '\n';

}



int main() {

    ios::sync_with_stdio(false);

    cin.tie(nullptr);

    int T;

    cin >> T;

    while (T--) {

        solve();

    }

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

Previous submission (verdict: Time limit exceeded on test 6):
#include <bits/stdc++.h>

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

#define lli long long

#define llu unsigned long long

#define vi vector<int>

using namespace std;



void solve() {

    string s;

    cin >> s;

    bool f = true;

    for (int i = 0; i < s.size() - 1; i++) {

        for (int j = s.size() - 1; j > i; j--) {

            if (s[j] - 1 > s[j - 1]) {

                s[j] = s[j] - 1;

                swap(s[j], s[j - 1]);

                f = false;

            }

        }

        if (f) {

            break;

        }

    }

    cout << s << '\n';

}



int main() {

    ios::sync_with_stdio(false);

    cin.tie(nullptr);

    int T;

    cin >> T;

    while (T--) {

        solve();

    }

}

Submission (verdict: Accepted):
#include <bits/stdc++.h>

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

#define lli long long

#define llu unsigned long long

#define vi vector<int>

using namespace std;



void merge(vector<char>& a, int p, int q, int r) {

    int n1 = q - p + 1;

    int n2 = r - q;

    vector<char> left(n1);

    vector<char> right(n2);

    for (int i = 0; i < n1; i++) left[i] = a[p + i];

    for (int i = 0; i < n2; i++) right[i] = a[q + i + 1];

    int i = 0, j = 0, k = p;

    while (i < n1 && j < n2) {

        if (right[j] - (n1 + j - (k - p)) > left[i]) {

            a[k++] = right[j] - (n1 + j - (k - p));

            j += 1;

        } else {

            a[k++] = left[i];

            i += 1;

        }

    }

    while (i < n1) a[k++] = left[i++];

    while (j < n2) a[k++] = right[j++];

}



void merge_sort(vector<char>& a, int n) {

    int size = 1;

    while (size < n) {

        for (int l = 0; l < n - size; l += 2 * size) {

            merge(a, l, l + size - 1, min(l + 2 * size - 1, n - 1));

        }

        size += size;

    }

}



void solve() {

    string s;

    cin >> s;

    vector<char> a(all(s));

    merge_sort(a, s.size());

    for (auto& t : a) cout << t;

    cout << '\n';

}



int main() {

    ios::sync_with_stdio(false);

    cin.tie(nullptr);

    int T;

    cin >> T;

    while (T--) {

        solve();

    }

}
Problem: 2060D - Subtract Min Sort
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>



using namespace std;



bool is(int * a, int n) {

for(int i = 0; i < n - 1; i++) {

if(a[i] < a[i + 1]) {

a[i + 1] = a[i + 1] - a[i];

a[i] = 0;

}



else {

return false;

}

}



return true;

}



int main() {

int t;

cin>>t;



while(t--) {

int n;

cin>>n;



int arr[n];



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

cin>>arr[i];

}



if(is(arr, n)) {

cout<<"yes";

}



else {

cout<<"no";

}

cout<<"\n";

}

return 0;

}

Submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>



using namespace std;



bool is(int * a, int n) {

for(int i = 0; i <= n - 1; i++) {

if(a[i] < a[i + 1]) {

a[i + 1] = a[i + 1] - a[i];

a[i] = 0;

}



else {

return false;

}

}



return true;

}



int main() {

int t;

cin>>t;



while(t--) {

int n;

cin>>n;



int arr[n];



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

cin>>arr[i];

}



if(is(arr, n)) {

cout<<"yes";

}



else {

cout<<"no";

}

cout<<"\n";

}

return 0;

}
Problem: 2060D - Subtract Min Sort
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>



using namespace std;



bool is(int * a, int n) {

for(int i = 0; i <= n - 1; i++) {

if(a[i] < a[i + 1]) {

a[i + 1] = a[i + 1] - a[i];

a[i] = 0;

}



else {

return false;

}

}



return true;

}



int main() {

int t;

cin>>t;



while(t--) {

int n;

cin>>n;



int arr[n];



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

cin>>arr[i];

}



if(is(arr, n)) {

cout<<"yes";

}



else {

cout<<"no";

}

cout<<"\n";

}

return 0;

}

Submission (verdict: Accepted):
#include <bits/stdc++.h>



using namespace std;



bool is(int * a, int n) {

for(int i = 0; i < n - 1; i++) {

if(a[i] <= a[i + 1]) {

a[i + 1] = a[i + 1] - a[i];

a[i] = 0;

}



else {

return false;

}

}



return true;

}



int main() {

int t;

cin>>t;



while(t--) {

int n;

cin>>n;



int arr[n];



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

cin>>arr[i];

}



if(is(arr, n)) {

cout<<"yes";

}



else {

cout<<"no";

}

cout<<"\n";

}

return 0;

}
Problem: 2060D - Subtract Min Sort
Language: C++17 (GCC 7-32)


Submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>



using namespace std;



bool is(int * a, int n) {

for(int i = 0; i < n - 1; i++) {

if(a[i] < a[i + 1]) {

a[i + 1] = a[i + 1] - a[i];

a[i] = 0;

}



else {

return false;

}

}



return true;

}



int main() {

int t;

cin>>t;



while(t--) {

int n;

cin>>n;



int arr[n];



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

cin>>arr[i];

}



if(is(arr, n)) {

cout<<"yes";

}



else {

cout<<"no";

}

}

return 0;

}
Problem: 2060D - Subtract Min Sort
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 1):
#include <bits/stdc++.h>



using namespace std;



bool is(int * a, int n) {

for(int i = 0; i < n - 1; i++) {

if(a[i] < a[i + 1]) {

a[i + 1] = a[i + 1] - a[i];

a[i] = 0;

}



else {

return false;

}

}



return true;

}



int main() {

int t;

cin>>t;



while(t--) {

int n;

cin>>n;



int arr[n];



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

cin>>arr[i];

}



if(is(arr, n)) {

cout<<"yes";

}



else {

cout<<"no";

}

}

return 0;

}

Submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>



using namespace std;



bool is(int * a, int n) {

for(int i = 0; i < n - 1; i++) {

if(a[i] < a[i + 1]) {

a[i + 1] = a[i + 1] - a[i];

a[i] = 0;

}



else {

return false;

}

}



return true;

}



int main() {

int t;

cin>>t;



while(t--) {

int n;

cin>>n;



int arr[n];



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

cin>>arr[i];

}



if(is(arr, n)) {

cout<<"yes";

}



else {

cout<<"no";

}

cout<<"\n";

}

return 0;

}
Problem: 2062C - Cirno and Operations
Language: C++20 (GCC 13-64)


Submission (verdict: Wrong answer on test 2):
#include <bits/stdc++.h>
#define ll long long
#define vll vector<ll>
#define pb push_back
#define sll set<ll>
#define fast                      \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0)
const ll mod = 1e9 + 7;
const ll M = 2 * 1e5 + 1;
using namespace std;

ll mod_exp(ll base, ll exp, ll mod)
{
    ll result = 1;
    while (exp > 0)
    {
        if (exp % 2 == 1)
        {
            result = (result * base) % mod;
        }
        base = (base * base) % mod;
        exp /= 2;
    }
    return result;
}

ll inv(ll a, ll b)
{
    return 1 < a ? b - inv(b % a, a) * b / a : 1LL;
}

ll factorial[M + 1];

ll ncr(ll n, ll r)
{
    if (r == 0)
        return 1;
    if (r > n)
        return 0;
    ll ans = factorial[n] * inv((factorial[r] * factorial[n - r]) % mod, mod);
    return (ans % mod);
}

vll primeFactors(ll n)
{
    vll v;
    while (n % 2 == 0)
    {
        v.pb(2);
        n = n / 2;
    }
    for (ll i = 3; i <= sqrt(n); i = i + 2)
    {
        while (n % i == 0)
        {
            v.pb(i);
            n = n / i;
        }
    }
    if (n > 2)
        v.pb(n);
    return v;
}

// Every even natural number less than 4*1e18 can be represented as sum of 2 primes

sll allFactors(ll n)
{
    sll st;
    for (ll i = 1; i <= sqrtl(n); i++)
    {
        if (n % i == 0)
        {
            st.insert(i);
            st.insert(n / i);
        }
    }
    return st;
}

vll SieveOfEratosthenes(ll n)
{
    vll v;
    vector<bool> prime(n + 1, true);
    for (ll p = 2; p * p <= n; p++)
        if (prime[p] == true)
            for (int i = p * p; i <= n; i += p)
                prime[i] = false;
    for (ll p = 2; p <= n; p++)
        if (prime[p])
            v.pb(p);
    return v;
}

int binarysearch(int arr[],int n,int k){
    int s = 0, e = n - 1; // Search range from 0 to n-1
    while (s <= e) // Loop until the search space is valid
    {
        int mid = (s + e) / 2; // Find the middle element
        if (arr[mid] == k) // If the middle element is the target, return the index
            return mid;
        else if (arr[mid] < k) // If target is greater, move the left pointer to mid+1
            s = mid + 1;
        else // If target is smaller, move the right pointer to mid-1
            e = mid - 1;
    }
    return -1; // If the element is not found, return -1
}
int main()
{
    int t;
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        int arr[n],sum=0;
        for(int i=0;i<n;i++){
            cin >> arr[i];
            sum+=arr[i];
        }
        if(n==1){
            cout<<arr[0]<<endl;
        }
        else{
            int z=1,mx=abs(sum);
            while(z){
                if(n==1){cout<<mx<<endl;z=0;}
                else{
                    int sum=0;
                    for(int i=0;i<n-1;i++){
                        sum+=(arr[i+1]-arr[i]);
                        arr[i]=(arr[i+1]-arr[i]);
                    }
                    mx=max(mx,abs(sum));
                    n=n-1;
                }
            }
        }
    }
}
Problem: 2050C - Uninteresting Number
Language: C++17 (GCC 7-32)

Previous submission (verdict: Time limit exceeded on test 5):
#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main(){
  int t;
  cin >> t;
  while(t--){
    string s;
    cin >> s;
    int cnt2 = 0 ;
    int cnt3 = 0 ;
    int sum = 0 ;
    for(int i = 0 ; i < s.size() ; i++){
      if(s[i] == '2'){
        cnt2++;
      }
      else if(s[i] == '3'){
        cnt3++;
      }
      sum += s[i] - '0';
      sum %= 9;
    }
    if(sum % 9 == 0){
      cout << "yes" << endl;
      continue;
    }
    bool flag = false;
    for(int i = 0 ; i <= cnt2 ; i++){
      for(int j = 0 ; j <= cnt3 ; j++){
        int tsum = sum ;
        tsum += (i * 2 + j * 6);
        if(tsum % 9 == 0){
          flag = true;
          break;
        }
      }
    }
    if(flag){
      cout << "yes" << endl;
      continue;
    }
    cout << "no" << endl;
  }
}

Submission (verdict: Accepted):
#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main(){
  int t;
  cin >> t;
  while(t--){
    string s;
    cin >> s;
    int cnt2 = 0 ;
    int cnt3 = 0 ;
    int sum = 0 ;
    for(int i = 0 ; i < s.size() ; i++){
      if(s[i] == '2'){
        cnt2++;
      }
      else if(s[i] == '3'){
        cnt3++;
      }
      sum += s[i] - '0';
      sum %= 9;
    }
    if(sum % 9 == 0){
      cout << "yes" << endl;
      continue;
    }
    bool flag = true;
    for(int i = 0 ; i <= cnt2 && flag ; i++){
      for(int j = 0 ; j <= cnt3 && flag ; j++){
        if((sum + (i * 2 + j * 6)) % 9 == 0){
          flag = false;
          break;
        }
      }
    }
    if(!flag){
      cout << "yes" << endl;
      continue;
    }
    cout << "no" << endl;
  }
}
Problem: 2050C - Uninteresting Number
Language: C++17 (GCC 7-32)

Previous submission (verdict: Time limit exceeded on test 5):
#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main(){
  int t;
  cin >> t;
  while(t--){
    string s;
    cin >> s;
    int cnt2 = 0 ;
    int cnt3 = 0 ;
    int sum = 0 ;
    for(int i = 0 ; i < s.size() ; i++){
      if(s[i] == '2'){
        cnt2++;
      }
      else if(s[i] == '3'){
        cnt3++;
      }
      sum += s[i] - '0';
    }
    if(sum % 9 == 0){
      cout << "yes" << endl;
      continue;
    }
    bool flag = false;
    for(int i = 0 ; i <= cnt2 ; i++){
      for(int j = 0 ; j <= cnt3 ; j++){
        if((sum + (i * 2 + j * 6)) % 9 == 0){
          flag = true;
          break;
        }
      }
    }
    if(flag){
      cout << "yes" << endl;
      continue;
    }
    cout << "no" << endl;
  }
}

Submission (verdict: Time limit exceeded on test 5):
#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main(){
  int t;
  cin >> t;
  while(t--){
    string s;
    cin >> s;
    int cnt2 = 0 ;
    int cnt3 = 0 ;
    int sum = 0 ;
    for(int i = 0 ; i < s.size() ; i++){
      if(s[i] == '2'){
        cnt2++;
      }
      else if(s[i] == '3'){
        cnt3++;
      }
      sum += s[i] - '0';
      sum %= 9;
    }
    if(sum % 9 == 0){
      cout << "yes" << endl;
      continue;
    }
    bool flag = false;
    for(int i = 0 ; i <= cnt2 ; i++){
      for(int j = 0 ; j <= cnt3 ; j++){
        if((sum + (i * 2 + j * 6)) % 9 == 0){
          flag = true;
          break;
        }
      }
    }
    if(flag){
      cout << "yes" << endl;
      continue;
    }
    cout << "no" << endl;
  }
}