ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2057B - Gorilla and the Exam
Language: c++17 

Solution 1 (runtime: 124 ms):
#include <bits/stdc++.h>
using namespace std;

int assign_smallest_to_largest(vector<pair<int, int>> &lis, int k, int n){
    int i = 0;
    int current_most = lis.back().first;
    int cnt = 0;
    // cout << "current_most "<<current_most << "n:"<< n << endl;
    // for(int i = 0; i < lis.size(); i++){
    //     cout << "data before: "<<lis[i].first << " " << lis[i].second << endl;
    // }
    for (int i = 0; i < lis.size(); i++)
    {
        if(lis[i].first == current_most){
            continue;
        }
        if(lis[i].second <= k){
            lis.back().second += lis[i].second;
            k -= lis[i].second;
            lis[i].second = 0;
        }
        else{
            break;
        }
    }  
    
    for(int i = 0; i < lis.size(); i++){
        // cout << "data: "<<lis[i].first << " " << lis[i].second << endl;
        if(lis[i].second > 0){
            cnt++;
        }
    }
    return cnt;
}

map<int, int> m;
vector<pair<int, int>> lis;
int main(){
    ios::sync_with_stdio(false); // 取消同步流
    cin.tie(0); // 解绑cin与cout
    cout.tie(0); // 解绑cout与cin
    int t, n, k;
    cin >> t;

    for(int i = 0; i < t; i++){
        cin >> n >> k;
        for(int j = 0; j < n; j++){
            int x;
            cin >> x;
            m[x]++;
        }
        // cout << "msize: "<<m.size() << endl;
        lis.clear();
        for (auto i = m.begin(); i != m.end(); i++)
        {
            lis.push_back(make_pair(i->first, i->second));
            //cout << "data pushed: "<<i->first << " " << i->second << endl;
        }
        // cout << "data size: "<<lis.size() << endl;
        
        sort(lis.begin(), lis.end(), [](const pair<int, int> &a, const pair<int, int> &b){
            return a.second < b.second;
        });

        cout << assign_smallest_to_largest(lis, k, n) << endl;
        // cout << "lis size" << lis.size() << endl;
        m.clear();
    }



    return 0;
}

Solution 2 (runtime: 202 ms):
#include<iostream>
#include<bits/stdc++.h>

using namespace std;
#define ll long long



void solve()
{
    int n,k;
    cin>>n>>k;
    vector<int>arr(n,0);
    
    vector<int>freq={1};


    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    

    }

    sort(arr.begin(),arr.end());

    for(int i=1;i<n;i++)
    {
        if(arr[i]==arr[i-1])
        {
            freq.back()++;

        }
        else
        {
            freq.emplace_back(1);

        }
    }
    sort(freq.begin(),freq.end());
    int m=freq.size();

    for (int i = 0; i <m-1; i++)
    {
        if(freq[i]>k)
        {
            cout<<m-i<<endl;
            return ;

        }
        k=k-freq[i];


        
    }
    cout<<1<<endl;

    
    



    
    
    

}

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        solve();

    }
}
Problem: 2060D - Subtract Min Sort
Language: c++17 

Solution 1 (runtime: 327 ms):
#include <bits/stdc++.h>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
    int n;
    cin>>n;
    vector<int> v(n);
    for(int i=0;i<n;i++){
        cin>>v[i];
    }
    bool flag=true;
    int diff=v[0];
    for(int i=1;i<n;i++){
        if(diff<=v[i]){
            // cout<"hvhv"<<endl;
            diff=v[i]-diff;
            // cout<<diff<<endl;
        }
        else{
            flag=false;
            cout<<"No"<<endl;
            break;
        }
    }
    if(flag) cout<<"yes"<<endl;
}
}

Solution 2 (runtime: 108 ms):
#include<bits/stdc++.h>
using namespace std;
#define int long long 
#define sz(x) ((int)(x).size())
#define fast_io ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb push_back
#define vi vector<int>
#define vpi vector<pair<int,int>>
#define ip(n) int n ; cin >> n ; 

bool f(int n,vector<int> &arr){
    for(int i=0;i<n-1;i++){
        if(arr[i+1] >= arr[i]){
            arr[i+1]-=arr[i];
        }
        else{
            return false;
        }
    }
    return true;
}
int32_t main(){
fast_io ;   
    ip(t);
    while(t--){
        int n;
        cin>>n;
        vector<int>ans(n);
        for(int i=0;i<n;i++)cin>>ans[i];
        f(n,ans) == true? cout << "YES"<<endl : cout<<"NO"<<endl;
    }
}
Problem: 2049C - MEX Cycle
Language: c++17 

Solution 1 (runtime: 109 ms):
#include <bits/stdc++.h>

using namespace std;



int main() {

	// your code goes here

	int t; cin>>t; while(t--){

	    int n,x,y; cin>>n>>x>>y;

	    vector<int> a(n,0);

	    

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

             a[i]=i%2;

	    }

	    

	    if(!(n%2)){

	        if(x%2==y%2) a[x-1]=2;

	    }

	    else{

	        a[n-1]=2;

	        if(max(x,y)!=n){

	            if(x%2==y%2) {

	                if(min(x,y)==1) {a[0]=2; a[n-1]=0;}

	                else a[min(x,y)-1]=2;

	            }

	        }

	    }

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

             cout<<a[i]<<" ";

	    }

	    cout<<"\n";

	    

	    

	    

	}



}



Solution 2 (runtime: 93 ms):
#include<iostream>

#include<vector>



using namespace std;



void solve() {

	int n, x, y;

	cin >> n >> x >> y;

	x--; y--;

	vector<int> a(n);

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

		a[(x + i) % n] = i % 2;

	}

	if (n % 2 == 1 || (x - y) % 2 == 0) {

		a[x] = 2;

	}

	for (auto val : a) cout << val << ' ';

	cout << "\n";

}



int main() {

	int t;

	cin >> t;

	while (t--) {

		solve();

	}

}
Problem: 2063B - Subsequence Update
Language: c++17 

Solution 1 (runtime: 78 ms):
#include<bits/stdc++.h>
#define int long long
#define ll long long
#define ull unsigned long long
#define db double
#define P point
#define Fr(i,a,b) for(int i=a;i<=b;i++)
#define Fr_(i,a) for(auto i:a)
#define pb push_back
#define fastio ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define pii pair<int,int> 
#define fir first
#define sec second
#define pq priority_queue
#define mkp make_pair
#define memset(a,b) memset(a,b,sizeof(a))
#define new_opt friend bool operator
#define endl '\n'
#define dbug cout<<"Ciallo~(<'w< )~"<<endl

using namespace std;

const int N=2e6+10;
const int N_=6e3+10;
const int M=998244353;
const int inf=2e9;

int fast_read=1 ;
int multiple_test=1 ;
int n,m,k;

int a[N];
int l,r;
int check(vector<int> v)
{
    pq<int> q1,q2;
    Fr(i,l,r)
    {
        q1.push(a[i]);
    }
    Fr_(i,v) q2.push(-i);
    int c=0;
    while(!q1.empty()&&!q2.empty())
    {
        int x=q1.top(),y=-q2.top();
        q1.pop(),q2.pop();
        if(x<y) break;
        else{
            c+=x-y;
        }
    }
    return c;
}
void slv()
{
    cin>>n>>l>>r;
    Fr(i,1,n) cin>>a[i];
    int res=0;
    Fr(i,l,r) res+=a[i];
    vector<int> vl,vr;
    Fr(i,1,l-1) vl.pb(a[i]);
    Fr(i,r+1,n) vr.pb(a[i]);
    cout<<min(res-check(vl),res-check(vr))<<endl;
}

signed main()
{
    if(fast_read) fastio;
    int mt=1;
    if(multiple_test) cin>>mt;
    while(mt--)
    {
        slv();
    }
}

Solution 2 (runtime: 77 ms):
#include<bits/stdc++.h>
using namespace std;
#define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define FOR(a, c) for (int(a) = 0; (a) < (c); (a)++)
typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define F first
#define S second
#define PB push_back
#define POB pop_back
#define MP make_pair
#define all(x) x.begin(), x.end()
#define sortall(x) sort(all(x))
int main(){
  fastIO;
  int t;
  cin>>t;
  while(t--){
    int n,l,r;
    cin>>n>>l>>r;
    vi arr(n);
    FOR(i,n){
      cin>>arr[i];
    }
    vi brr;
    FOR(i,r){
      brr.PB(arr[i]);
    }
    vi crr;
    for(int i=l-1;i<n;i++){
      crr.PB(arr[i]);
    }
    int d=r-l+1;
    sortall(brr);
    sortall(crr);
    long long sum1=0;
    FOR(i,d){
      sum1+=brr[i];
    }
    long long sum2=0;
    FOR(i,d){
      sum2+=crr[i];
    }
    cout<<min(sum1,sum2)<<endl;
  }
  return 0;
}
Problem: 2063C - Remove Exactly Two
Language: c++17 

Solution 1 (runtime: 562 ms):
#include<bits/stdc++.h>
using namespace std;

void solve(){
	int n;cin>>n;
	vector<vector<int>> adj(n+1);
	vector<int> edges(n+1);
	for(int i=0; i<n-1; i++){
		int a,b;cin>>a>>b;
		adj[a].push_back(b);
		adj[b].push_back(a);
		edges[a]++;
		edges[b]++;
	}
	if(n==2){
		cout<<0<<endl;
		return ;
	}
	long long max = -1,node=-1;;
	for(int i=1; i<=n; i++){
		if(edges[i] > max){
			node = i;
			max = edges[i];
		}
	}
	bool flag = false;
	for(auto it: adj[node]){
		if(edges[it] == max){
			
			if(flag){
			cout<<max*2-1<<endl;
			return ;}flag = true;
		}
		edges[it]--;
	}
	long long max2=-1,node2=-1;
	for(int i=1; i<=n; i++){
		if(edges[i]>max2 && i != node){
			node2=i;
			max2=edges[i];
		} 
	}
	cout<<max+max2-1<<endl;
}

int main(){
	int t;cin>>t;
	while(t--){
		solve();
	}
}

Solution 2 (runtime: 1000 ms):
#include <bits/stdc++.h>

using namespace std;

const int MAXN=2e5+10;

multiset<int> s;
int deg[MAXN], T, n, ans, tmp;
vector<int> e[MAXN];

int main() {
	cin>>T;
	while(T--) {
		cin>>n;
		ans=1;
		s.clear();
		for(int i=1; i<=n; i++) deg[i]=0, e[i].clear();
		for(int i=1; i<n; i++) {
			int u, v;
			cin>>u>>v;
			deg[u]++;deg[v]++;
			e[u].push_back(v);
			e[v].push_back(u);
		}
		if(n==2) {
			cout<<0<<'\n';
			continue;
		}
		for(int i=1; i<=n; i++) s.insert(deg[i]);
		for(int i=1; i<=n; i++) {
			tmp=1;
			tmp+=deg[i]-1;
			s.erase(s.find(deg[i]));
			for(auto v : e[i]) {
				s.erase(s.find(deg[v]));
				s.insert(deg[v]-1);
			}
			tmp+=(*(--s.end()))-1;
			ans=max(ans, tmp);
			for(auto v : e[i]) {
				s.erase(s.find(deg[v]-1));
				s.insert(deg[v]);
			}
			s.insert(deg[i]);
		}
		cout<<ans<<'\n';
	}
	return 0;
}
Problem: 2050C - Uninteresting Number
Language: c++17 

Solution 1 (runtime: 62 ms):
#include <iostream>
using namespace std;

void solve(){
     string s;
        cin>>s;
        
        int two=0,three=0,sum=0;
           bool flag=0;
        for(auto i:s){
            if(i=='2'){
                two++;
            }
            else if(i=='3'){
                three++;
            }
            sum+=(i-'0');
            sum=sum%9;
            
        }
            for(int j=0;j<=three;j++){
                for(int i=0;i<=two;i++){
                    long tsum=sum;
                    tsum+=i*2+j*6;
                    
                    if(tsum%9==0){
                        cout<<"YES"<<endl;
                        return;
                    }
                }
            }
            
        
        
     
                cout<<"NO"<<endl;
            return;
}


int main()
{
    
    
    int t;
    cin>>t;
    while(t--){
        solve();
       
    }
    
}

Solution 2 (runtime: 62 ms):
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
    ll tc ; cin>>tc;
    while(tc--){
        string s ; cin>>s;
        int two = 0 ;int  three = 0;int  sum = 0;
        for(int i = 0; i<s.size(); i++){
            if(s[i]=='2'){
                two++;
            }
            else if(s[i]=='3'){
                three++;
            }
            sum = sum+ (s[i]-'0');
        }
        bool ans = 0;
        if(sum%9==0){
            ans = 1;
        }
        for(int i = 0 ; i<2 ;i++){
            int target = (i==0? 9: 18) -sum%9;
            for(int tworeq = 0; tworeq <= two ; tworeq++){
                int newtarget = target - 2*tworeq;
                int threereq = newtarget/6;
                if(newtarget%6==0 && three>=threereq ){
                    ans = 1;
                    break;
                }
            }
        }
        if(ans!=1){
            cout<<"NO"<<endl;
        }
        else{
            cout<<"YES"<<endl;
        }
    }
    return 0;
}
Problem: 2050B - Transfusion
Language: c++17 

Solution 1 (runtime: 328 ms):
#include <bits/stdc++.h>
using namespace std;
#define int long long
int t, n, a1, a2, x;
int n1, n2;
signed main()
{
    cin >> t;
    for (int i = 1; i <= t; i++)
    {
        a1 = a2 = 0;
        cin >> n;
        for (int j = 1; j <= n; j++)
        {
            cin >> x;
            if (j % 2 == 1) a1 += x;
            else a2 += x;
        }
        if ((a1+a2) % n) {cout << "No" << '\n'; continue;}
        int avg = (a1+a2) / n;
        if (n % 2) {n1 = n/2+1; n2 = n - n1;}
        else {n1 = n/2; n2 = n - n1;}
        if (avg * (n1) != a1 || avg * n2 != a2)
        {
            cout << "No" << '\n';
            continue;
        }
        cout << "Yes" << '\n';
    }
    return 0;
}


Solution 2 (runtime: 359 ms):
#include <bits/stdc++.h>

using namespace std;



void solve() {

    int n; cin>> n;

    long long int sumodd =0, sumeven = 0;

   

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

      int u; cin>> u;

      

      if(i&1){

        sumodd += u;

      }

      else sumeven += u;

    }

    

    int oddsize = n/2 + (n&1);

    int evensize = n/2;

    

    if((sumodd%evensize) != 0 || (sumeven% oddsize) != 0  ) {

      cout<< "NO"<< endl;

    return;

      

    }

    if((sumodd/evensize) == (sumeven/oddsize)){

      cout<<"YES"<< endl;

    }

      else cout<<"NO"<< endl;

    

}



int main() {

    int t;

    cin >> t;

    while (t--) {

        solve();

    }

    return 0;

}
Problem: 2057B - Gorilla and the Exam
Language: c++17 

Solution 1 (runtime: 264 ms):
#include<iostream>
#include<vector>
#include<utility>
#include<algorithm>
#include<map>
using namespace std;
typedef long long ll;
typedef pair<ll,int> Pair;
const ll N=1e9+5;
ll a[100005];
map<ll,int> mp;

bool cmp(pair<ll,int> x,pair<ll,int> y){
	return x.second<y.second;
} 

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n,k;
		cin>>n>>k;
		for(int i=0;i<n;i++){
			cin>>a[i];
			mp[a[i]]++;
		}
		vector<Pair> mp_vec(mp.begin(),mp.end());
		sort(mp_vec.begin(),mp_vec.end(),cmp);
		
/*		for(auto i:mp_vec){
			cout<<i.second<<" ";
		}
		cout<<endl;*/
		
		for(auto &i:mp_vec){
			if(k>=i.second)
			{
				k-=i.second;
				i.second=0;
			}
			if(k<=0){
				break;
			}
		}
/*		cout<<k<<endl;
		for(auto i:mp_vec){
			cout<<i.second<<" ";
		}
		cout<<endl;*/
		int cnt=0;
		for(auto i:mp_vec){
			if(i.second!=0)
				cnt++;
		}
		if(cnt==0)
			cout<<1<<endl;
		else
			cout<<cnt<<endl;
		for(int i=0;i<n;i++){
			a[i]=0;
		}
		mp.clear();
		mp_vec.clear();
	}
	return 0;
}

Solution 2 (runtime: 109 ms):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
void fns()
{
  ll n,k;
  cin>>n>>k;
  int maxi=1;
  vector<int>v(n+1);
  map<ll,ll>mp;
  for(int i=1;i<=n;i++){
    cin>>v[i];
     mp[v[i]]++;
  }
  vector<int>v1;
  for(auto it:mp){
    v1.push_back(it.second);
  }
  sort(v1.begin(),v1.end());
  int x = v1.size();
    for(int i=0;i<v1.size(); i++){
        if(k>=v1[i]) k-= v1[i],x--;
        else break;
    }
    cout<<max(x,1)<<endl;
}

int main() 
{ 
  ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
   int t=1;
   cin>>t;
   while(t--){
     fns();
   }
}
Problem: 2057B - Gorilla and the Exam
Language: c++17 

Solution 1 (runtime: 187 ms):
#include <bits/stdc++.h>

using namespace std;

int main() {
    // your code goes here
    int t;
    cin >> t;
    while (t--) {
        int n, k;
        cin >> n >> k;
        vector < int > arr(n);
        for (int i = 0; i < n; i++) {
            cin >> arr[i];
        }
        sort(arr.begin(), arr.end());
        vector < int > nums;
        int count = 1;
        for (int i = 1; i < n; i++) {
            if (arr[i] == arr[i - 1]) {
                count++;
            } else {
                nums.push_back(count);
                count = 1;
            }
        }
        nums.push_back(count);
        sort(nums.begin(), nums.end());
        for (int i = 0; i < nums.size(); i++) {
            if (nums[i] <= k) {
                k -= nums[i];
                nums[i] = 0;
            } else {
                break;
            }
        }
        int count1 = 0;
        for (int i = 0; i < nums.size(); i++) {
            if (nums[i] != 0) {
                count1++;
            }
        }
        if (count1 == 0) {
            cout << count1 + 1 << endl;
        } else {
            cout << count1 << endl;
        }



    }

}

Solution 2 (runtime: 499 ms):
#include <bits/stdc++.h>

using namespace std;

map<int, int> b;
vector<int> a;

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	int t; cin >> t;
	while (t--) {
		b.clear();
		a.clear();
		int n, k; cin >> n >> k;
		for (int i = 1; i <= n; i++) {
			int num; cin >> num;
			if (!b[num]) a.push_back(num);
			b[num]++;
		}
		sort(a.begin(), a.end(), [&] (int a1, int b1) {
			return b[a1] > b[b1];
		}); 
		while (b[a.back()] <= k && a.size() > 1) {k -= b[a.back()]; b[a[0]] += b[a.back()]; a.pop_back();}
		cout << a.size() << '\n';
	}
	return 0;
}
Problem: 2063B - Subsequence Update
Language: c++17 

Solution 1 (runtime: 186 ms):
#include <bits/stdc++.h>



using namespace std;



int main()

{

    int t;

    cin>>t;

    while (t--){

        int n,l,r;

        cin>>n>>l>>r;

        int c=r-l+1;

        long long a[r];

        long long b[n-l+1];

        long long arr[n];

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

            cin>>arr[i];

        }

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

            a[i]=arr[i];

        }

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

            b[i-l+1]=arr[i];

        }





        sort(a,a+r);

        sort(b,b+n-l+1);

        long long s1=0;

        long long s2=0;

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

            s1=s1+a[i];

            s2=s2+b[i];

        }

        cout<<min(s1,s2)<<'\n';

    }

    return 0;

}



Solution 2 (runtime: 203 ms):
#include <bits/stdc++.h>
using namespace std;

void solve(){
    int n,l,r;
    cin>>n>>l>>r;
    vector<int> vec;
    for(int i=0;i<n;i++){
        int a;cin>>a;
        vec.push_back(a);
    }
    vector<int> v1,v2;
    for(int i=0;i<r;i++){
        v1.push_back(vec[i]);
    }
    for(int i=l-1;i<n;i++){
        v2.push_back(vec[i]);
    }
    sort(v1.begin(),v1.end());
    sort(v2.begin(),v2.end());
    int k = r-l+1;
    long long sum1=0,sum2=0;
    for(int i=0;i<k;i++){
        sum1+=v1[i];
        sum2+=v2[i];
    }
    cout<<min(sum1,sum2)<<endl;
   
}

int main(){
  int t;cin>>t;
  while(t--){
    solve();
  }
}
Problem: 2049B - pspspsps
Language: c++17 

Solution 1 (runtime: 62 ms):
//Hara Hara Mahadev

#include<bits/stdc++.h>

using namespace std;

 

#define int 	long long

#define pi      3.1415926535897932384626433832795028841971693993751058

#define YES     cout<<"YES"<<"\n"

#define NO      cout<<"NO"<<"\n"

#define m1      cout<<-1<<"\n"

#define ssort(S)  sort((S).begin(),(S).end())

#define srev(S)   reverse((S).begin(),(S).end())

#define ll      long long

#define pb      push_back

#define vi      vector<int>

#define vvi 	vector<vector<int>>

#define vll      vector<long long>

#define vc      vector<char>

#define pii     pair<int,int>

#define vpii    vector<pair<int,int>>

#define vs	 vector<string>

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

#define for0(i, n) for (int i = 0; i < n; i++)

#define inp(v) for (auto & a : (v)) cin >> a;

#define endl "\n"

#define speed (ios_base:: sync_with_stdio(false),cin.tie(NULL))

#define mod 1000000007

 

int gcd(int a,int b) { if (a == 0) return b; return gcd(b%a, a);}

int max(int a,int b){if(a>b) return a; else return b;}

int min(int a,int b){if(a<b) return a; else return b;}  



int ceil_div(int a, int b) {

   return a / b + ((a ^ b) > 0 && a % b != 0);

}



void solve()

{

   int n;

   cin >> n;

   string s;

   cin >> s;



   if(s[0]=='s')    s[0]= '.';

   if(s.back() == 'p')  s.back() = '.';



   bool foundP = false, foundS = false;



   for(auto it: s) {

        if(it=='p') foundP = true;

        else if(it=='s')    foundS = true;

        if(foundP && foundS) {

            NO;

            return;

        }

   }



   YES;

}



int t = 1;

int32_t main() {

    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0),cin>>t;

    //cin >> t;

    while(t--){solve();}

    return 0;

}

Solution 2 (runtime: 46 ms):
#include<iostream>

using namespace std;

char str[505];

inline void f() {

    int l;

    cin >> l;

    bool ss = false,pp = false;

    int cnts = 0,cntp = 0;

    for(int i = 1;i<=l;i++) {

        cin >> str[i];

        if(str[i] == 's') {

            ss = true;

            cnts ++;

        }

        if(str[i] == 'p') {

            cntp++;

            pp = true;

        }

    }

    if(cnts > 1 && cntp > 1) {

        cout << "NO" << endl;

        return ;

    }

    if(ss == true&& pp == true && str[1] != 's' && str[l]!= 'p') {

        cout << "NO" << endl;

        return ;

    }

    cout << "YES" << endl;

}



int main() {

    int k;

    cin >> k;

    while(k--) f();

}
Problem: 2050C - Uninteresting Number
Language: c++17 

Solution 1 (runtime: 62 ms):
#include <iostream>
#include <string>
using namespace std;

int main() {
    long long t;
    cin >> t;
    while (t--) {
        string n;
        cin >> n;

        long long count1 = 0, a = 0, b = 0;
        for (char digit : n) {
            count1 += digit - '0';
            if (digit == '2') a++;
            if (digit == '3') b++;
        }

        bool found = false;
        for (int i = 0; i <= a && !found; i++) {
            for (int j = 0; j <= b; j++) {
                if ((count1 + 2 * i + 6 * j) % 9 == 0) {
                    found = true;
                    break;
                }
            }
        }

        cout << (found ? "YES" : "NO") << endl;
    }

    return 0;
}


Solution 2 (runtime: 62 ms):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
const int mod = 1e9 + 7;

void solve(int tc)
{
    string n;
    cin >> n;

    int sum = 0;
    vector<int> temp;

    for (auto x : n)
    {
        if (x == '2' || x == '3')
        {
            temp.push_back(x - '0');
        }
        sum += (x - '0');
    }

    if (sum % 9 == 0)
    {
        cout << "YES" << endl;
        return;
    }

    // DP to check if we can modify sum to make it divisible by 9
    vector<bool> dp(9, false);
    dp[sum % 9] = true;

    for (int x : temp)
    {
        vector<bool> next_dp = dp; // Copy current DP state
        int val = (x * x - x) % 9;

        for (int j = 0; j < 9; j++)
        {
            if (dp[j])
            {
                next_dp[(j + val) % 9] = true; // Add current value
            }
        }

        dp = next_dp;
    }

    if (dp[0])
    {
        cout << "YES" << endl;
    }
    else
    {
        cout << "NO" << endl;
    }
}

signed main()
{
    ios::sync_with_stdio(false);
    cout.tie(0);
    cin.tie(0);
    cout << fixed << setprecision(0);
    int t;
    cin >> t;
    for (int tc = 0; tc < t; tc++)
    {
        solve(tc);
    }
}