ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2062C - Cirno and Operations
Language: c++17 

Solution 1 (runtime: 61 ms):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define vi vector<long long int>
int main(){
    int times;
    cin>>times;
    while(times--){
        int n;
        cin>>n;
        vector<ll>v(n);
        for(auto &i:v){
            cin>>i;
        }
        ll sum=0;
        for(auto &i:v){
            sum+=i;
        }
        for(int i=0;i<n-1;i++){
            vector<ll>t;
            ll sum1=0;
            for(int j=1;j<v.size();j++){
                t.push_back(v[j]-v[j-1]);
                sum1+=(t.back());
            }
            sum=max(sum,sum1);
            sum=max(sum,-1*sum1);
            v=t;

        }
        cout<<sum<<endl;

    }
}

Solution 2 (runtime: 46 ms):
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        long long int n,i,j;
        cin >> n;
        long long int a[n];
        for(i=0;i<n;i++)
        {
            cin >> a[i];
        }
        long long ans = -1e18,sum;
        long long int now=n;
        for(i=0;i<n;i++)
        {
            sum=0;
            for(j=0;j<now;j++)
            {
                sum+=a[j];
            }
            if(i==0)
            {
                ans = max(ans,sum);
            }
            if(i!=0)
            {
                ans = max(ans,max(sum,-sum));
            }
            for(j=0;j<now;j++)
            {
                a[j] = a[j+1]-a[j];
            }
            now--;
        }
        cout << ans << endl;
    }
    return 0;
}
Problem: 2063C - Remove Exactly Two
Language: c++17 

Solution 1 (runtime: 249 ms):
#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int n;
    scanf("%d", &n);
    vector<int> a(n + 1);
    vector<vector<int>> g(n + 1);
    for (int i = 1, u, v; i < n; i++) {
      scanf("%d%d", &u, &v);
      g[u].push_back(v);
      g[v].push_back(u);
      ++a[u];
      ++a[v];
    }
    int ans;
    int max_pos = 1;
    int max_val = a[1];
    int nr_max = 1;
    for (int i = 2; i <= n; i++) {
      if (a[i] == max_val) {
        nr_max++;
      } else if (a[i] > max_val) {
        max_pos = i;
        max_val = a[i];
        nr_max = 1;
      }
    }

    if (nr_max <=2) {
      ans = max_val;
      for (auto i : g[max_pos]) { --a[i]; }
      a[max_pos] = 0;
      ans += *max_element(a.begin() + 1, a.end()) - 1;
    } else {
      ans = max_val * 2 - 1;
    }
    printf("%d\n", ans);
  }
  return 0;
}

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

#define ll long long 
#define ar array

const int mod = 1e9+7;

void solve(){
    int n;cin>>n;
    vector<int> adj[n];
    vector<int> degree(n,0);
    map<pair<int,int>,int> m;

    for(int i=0;i<n-1;i++){
        int x,y;cin>>x>>y;
        x--,y--;
        m[{x,y}]++;
        degree[x]++,degree[y]++;
    }
    map<pair<int,int>,int> m2;
    for(auto i: m){
       int x = degree[i.first.first], y = degree[i.first.second];
       if(x>y)swap(x,y);
       m2[{x,y}]++;
    }
    sort(degree.begin(),degree.end());
    int x = degree[n-1], y = degree[n-2];
    if(x == 1)cout<<"0\n";
    else if(x == y){
        ll freq = count(degree.begin(),degree.end(),x);
        ll num = m2[{y,x}];
        int ans = 2*x - 1;
        ll choice = (1LL*(freq)*(freq-1))/2;
        if(num>=choice) {
            ans--;
        }
        cout<<ans<<"\n";
    }
    else {
        int freq = count(degree.begin(),degree.end(),y);
        int num = m2[{y,x}];
        int ans = y + x -1;
        int choice = freq;
        if(num>=choice) ans--;
        cout<<ans<<"\n";
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int t;cin>>t;
    while(t--)
    solve();
        return 0;
}
Problem: 2062C - Cirno and Operations
Language: c++17 

Solution 1 (runtime: 46 ms):
#include<bits/stdc++.h>
using namespace std;
#define ll            long long
#define TC(t)          int t; cin >> t; for(int i = 1; i <= t; i++)
#define pii            pair<int,int>
#define ft             ios::sync_with_stdio(false);  cin.tie(NULL);
#define endl             "\n"
#define pb             push_back
#define py             cout<<"YES\n";
#define pn             cout<<"NO\n";
#define vrev(v)        reverse(v.begin(),v.end());
#define vsort(v)       sort(v.begin(),v.end());
#define rsort(v)       sort(v.rbegin(),v.rend());
#define vlowerB(v,x)   lower_bound(v.begin(),v.end(),x)
#define vupperB(v,x)   upper_bound(v.begin(),v.end(),x)
#define F              first
#define S              second
#define rep(i,a,b)     for(int i=a;i<=b;i++)
#define rep2(i,b,a)    for(int i=b;i>=a;i--)
#define vi             vector<ll>
#define zrbits(x)      __builtin_ctzll(x)
const int MOD=1e9+7;
void solve(){
    int n; cin>>n;
    vi a(n);
    rep(i,0,n-1) cin>>a[i];
    ll sum=accumulate(a.begin(),a.end(),0ll);
    vi b;
    while(n>1){
        rep(i,0,n-2){
            b.pb(a[i+1]-a[i]);
        }
        sum=max(sum,abs(accumulate(b.begin(),b.end(),0ll)));
        rep(i,0,n-2) {
            a[i]=b[i];
        }
        b.clear();
        n--;
    }
    cout<<sum<<endl;
}
int32_t main()
{
   ft
   TC(t)
   solve(); 
    return 0;
}

Solution 2 (runtime: 46 ms):
#include<bits/stdc++.h>
const int maxn = 55;
#define int long long
using namespace std;
int t,n,a[maxn],ans,sum;
signed main(){
	scanf("%lld",&t);
	while(t--){
		scanf("%lld",&n);
		for(int i = 1;i <= n;i++) scanf("%lld",&a[i]);
		if(n==1){
			printf("%lld\n",a[1]);
			continue;
		}
		ans = -INT_MAX;
		for(int i = n;i >= 1;i--){
			sum=0;
			for(int j = 1;j<=i;j++) sum+=a[j];
//			cout << "!!!" << sum << "\n";
			if (i!=n) ans = max(ans,abs(sum));
			else ans = max(ans,sum);
			for(int j = 1;j < i;j++){
				a[j] = a[j+1]-a[j];
			}
		}
		printf("%lld\n",ans);
	}
}
Problem: 2060D - Subtract Min Sort
Language: c++17 

Solution 1 (runtime: 93 ms):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector <ll>;
using ii = pair <ll, ll>;
using vii = vector <ii>;

void tc () {
    ll n;
    cin >> n;
    vll ve(n);
    for (ll &i : ve) cin >> i;
    ll j = 0;
    for (ll i = 1; i < n; i++) {
        if (ve[i-1] > ve[i]) j = i;
    }
    if (j == 0) { // all done
        cout << "YES\n";
        return;
    }
    auto fop = [&](ll i) {
        ll minN = min(ve[i], ve[i+1]);
        ve[i] -= minN;
        ve[i+1] -= minN;
    };
    for (ll i = 0; i < j; i++) {
        fop(i);
    }
    cout << (is_sorted(ve.begin(), ve.end()) ? "YES" : "NO") << '\n';
}

int main () {
    cin.tie(nullptr) -> sync_with_stdio(false);
    ll T; cin >> T; while (T--) { tc(); }
    return 0;
}

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

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    
    long long t;
    cin >> t;
    while (t--)
    {
        long long n;
        cin >> n;
        vector<long long> lst={};
        for(long long i=0;i<n;i++){
            long long num;
            cin >> num;
            lst.push_back(num);
        }
        long long state=0;
        for(long long i=0;i<n-1;i++){
            if(lst[i]!=0){
                if(i==0){
                    if(lst[i]>lst[i+1]){
                        state=1;
                        break;
                    }
                    else{
                        lst[i+1]-=lst[i];
                        lst[i]=0;
                    }
                }
                else{
                    if(lst[i]>lst[i+1]){
                        state=1;
                        break;
                    }
                    else{
                        lst[i+1]-=lst[i];
                        lst[i]=0;
                    }
                }
            }
        }
        if(state==0){
            cout << "YES";
        }
        else{
            cout << "NO";
        }
        cout << "\n";
    }
    
    
    
}
Problem: 2050D - Digital string maximization
Language: c++17 

Solution 1 (runtime: 1905 ms):
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <limits.h>
#include <list>
#include <cmath>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <unordered_map>

using namespace std;

#define int long long
#define MOD 1000000007
#define INF 2000000000
#define IN  100001
#define LOG 20
#define MINF -2000000000
#define all(x) x.begin(),x.end()
#define fast_io ios_base::sync_with_stdio(false);cin.tie(nullptr);



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

    string ans = s;
    int n = s.size();
    for(int  i = 0; i < n; i++){
        bool ok = false;
        for(int  k = 9; k > ans[i] - '0' && ok == false; k--){
            for(int j = i; j < i + 9 && j < n; j++){
                int number = (ans[j] - '0') - (j - i);
                if(number == k){
                    ans.erase(j, 1);
                    ans.insert(ans.begin() + i, k + '0');
                    ok = true;
                    break;
                }
            }
        }
    }
    cout << ans << endl;
}


signed main() {
    fast_io
    int t = 1;
    cin >> t;
    while (t--) {
        solution();
    }
    return 0;
}


Solution 2 (runtime: 62 ms):
#include <bits/stdc++.h>
using namespace std;
#define int               long long int
#define endl              "\n"
#define vi                vector<int>
#define vvi               vector<vector<int>>
#define fast              ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);



int32_t main(){
fast;

int t=0;
cin >> t;
while(t--){
    string s;
    cin >> s;
    int n = s.size();
    vi vec;
    for(int i=0;i<n;i++){
        vec.push_back(s[i] - '0');
    }

    for(int i=1;i<n;i++){
        int j=i-1;
        int cur=i;
        while(j>=0){
            if(vec[cur]-vec[j]>1){
                vec[cur]--;
                swap(vec[cur],vec[j]);
                cur--;
                j = cur-1;
            }else{
                break;
            }
        }
    }

    for(int i=0;i<n;i++){
        cout << vec[i];
    }
    cout << endl;

}

return 0;
}
Problem: 2062C - Cirno and Operations
Language: c++17 

Solution 1 (runtime: 62 ms):
#include <bits/stdc++.h>
using namespace std;
#define pip pair<int, pair<int,int>>
#define pii pair<int, int>
#define ll long long
void solve() {
    int n;
    cin >> n;
    
    vector<ll> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
	
	ll ans = accumulate(a.begin(), a.end(), 0ll);
	vector<ll> v;
	while(n > 1){
		ll sum = 0;
		for(int i = 1; i < n; i++){
			v.push_back(a[i] - a[i-1]);
			sum += (a[i] - a[i-1]);
		}
		ans = max(ans , abs(sum));
		for(int i = 0; i < n-1; i++) a[i] = v[i];
		v.clear();
		n--;
	}

	cout << ans << "\n";
}

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


Solution 2 (runtime: 218 ms):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define fr(i, n) for (int i = 0; i < n; i++)
#define all(a) a.begin(), a.end()
#define pb push_back
#define vi vector<int>
#define el '\n'
const int MOD = 998244353;
const int N = 5005;
#define int long long 

void solve(){
    int n;cin>>n;
    int ans = 0;
    vector<vector<int>> a(5000,vector<int>(50));fr(i,n) {
        cin >>a[0][i];
        ans+= a[0][i];
    }
    int size = n,j = 1;
    while(j < n){
        int temp = 0;
        for(int i = 0;i<size-1;i++){
            a[j][i] = a[j-1][i+1] - a[j-1][i];
            temp+=a[j][i];
        }
        ans = max(ans,temp);
        temp = 0;
        for(int i = size-2;i>=0;i--){
            a[j][i] = a[j-1][i] - a[j-1][i+1];
            temp+=a[j][i];
        }
        ans = max(ans,temp);
        size--;
        j++;
    }
    cout << ans <<el;
}


signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

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

    #ifndef ONLINE_JUDGE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
    #endif

    return 0;
}
Problem: 2062C - Cirno and Operations
Language: c++17 

Solution 1 (runtime: 62 ms):
#include<bits/stdc++.h>
#define ll long long

using namespace std;

void solve(){
    int n; cin >> n;
    vector<ll> a(n);
    for(auto &e:a) cin >> e;
    if(n==1){
        cout << a[0] << "\n";
        return;
    }
    ll ans = accumulate(a.begin(), a.end(), 0);
    while((int)a.size()>1){
        vector<ll> nv;
        ll ns = 0;
        int sz = a.size();
        for(int i=sz-1;i;--i){
            nv.emplace_back(a[i-1]-a[i]);
            ns += a[i-1]-a[i];
        }
        ans = max({ans, ns, -ns});
        swap(nv, a);
    }
    cout << ans << "\n";
}
int main(){
    cin.tie(nullptr)->sync_with_stdio(false);
    int tc; cin >> tc;
    while(tc--) solve();

    return 0;
}


Solution 2 (runtime: 61 ms):
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <string>
#include <cstdlib>
#include <numeric>
using namespace std;

#define ll long long
bool allPositve(vector<ll>& v)
{
    bool check = true;
    for (ll i = 0; i < v.size(); i++)
    {
        if(v[i] < 0 )
        {
            check = false;
            break;
        }
    }
    return check;
    
}
void func(vector<ll>& v, ll *sum)
{
    ll n = v.size();
    if(n==1) return;
    for (ll i = 0; i < n-1; i++)
    {
        v[i] = v[i+1]-v[i];
    }
    v.pop_back();
    ll new_sum = accumulate(v.begin(), v.end(), 0LL);
    *sum = max(*sum, abs(new_sum));
    func(v, sum);
}
void solve()
{
    ll n;
    cin >> n;
    vector<ll> a(n);
    for (ll i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    if (n==1)
    {
        cout << a[0] << endl;
        return;
    }
    ll sum= accumulate(a.begin(), a.end(), 0LL);
    func(a, &sum);
    cout << sum << endl;
}
int32_t main()
{
    ll t;
    cin >> t;
    while (t--)
    {
        solve();
    }
    
    return 0;
}
Problem: 2050D - Digital string maximization
Language: c++17 

Solution 1 (runtime: 77 ms):
#include<iostream>
using namespace std;
long long t;
string s;
void zad(){
    cin>>s;
    int right;
    int left;
    for(int i=1; i<s.size(); i++){
        for(int j=i; j>0; j--){
            right = s[j]-'0';
            left = s[j-1]-'0';
            if(right-1 > left){
                s[j-1] = (right-1)+'0';
                s[j] = left+'0';
            }else{
                break;
            }
        }
    }
    cout<<s<<'\n';
}

int main(){
    cin>>t;
    for(long long i=0; i<t; i++){
        zad();
    }
}

Solution 2 (runtime: 93 ms):
#include <bits/stdc++.h>
using namespace std;
int main() {
    int t;
    cin >> t;
    while (t--) {
        string s;
        cin >> s; 
        for (int i = 0; i < s.size(); i++) {
            int best = s[i] - '0', pos = i;
            for (int j = i; j < min(i + 10, (int)s.size()); j++) {
                if (s[j] - '0' - (j - i) > best) {
                    best = s[j] - '0' - (j - i);
                    pos = j;
                }
            }
            while (pos > i) {
                swap(s[pos], s[pos - 1]);
                pos--;
            }
            s[i] = char(best + '0');
        }
        cout << s << '\n';
    }

    return 0;
}
Problem: 2063C - Remove Exactly Two
Language: c++17 

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

struct node{
    vector<int> to;
    int cnt = 0;
};

vector<node> graph;

void solve(){
    graph.clear();
    int n;
    cin>>n;
    graph.resize(n);
    for(int i = 0, a,b; i < n-1; i++){
        cin>>a>>b;
        a--, b--;
        graph[a].to.push_back(b), graph[a].cnt++;
        graph[b].to.push_back(a), graph[b].cnt++;
    }

    pair<pair<int,int> ,int> mxm = {{-1, -1}, 1e9};
    for(int i = 0; i < n; i++) {
        int a = 0;
        for(int& b : graph[i].to) if(graph[i].cnt == graph[b].cnt) a++; 
        mxm = max(mxm, {{graph[i].cnt, -a}, i});
    }
    int sol = 1;
    sol += mxm.first.first - 1;
    for(int& a : graph[mxm.second].to) graph[a].cnt--;
    graph[mxm.second].cnt = -1e9;
    mxm = {{-1, -1}, 1e9};
    for(int i = 0; i < n; i++) {
        int a = 0;
        for(int& b : graph[i].to) if(graph[i].cnt == graph[b].cnt) a++; 
        mxm = max(mxm, {{graph[i].cnt, -a}, i});
    }
    sol += mxm.first.first - 1;
    cout << sol << "\n";

}

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

Solution 2 (runtime: 218 ms):
#include<bits/stdc++.h>
using namespace std;
int t,n,u,v,d[200055],maxx,id,ans,vis[200055];
vector<int> to[200055];
int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    cin>>t;
    while(t--){
        maxx=0;
        ans=0;
        cin>>n;
        for(int i=1;i<=n;i++) vis[i]=0,to[i].clear(),d[i]=0;
        for(int i=1;i<n;i++){
            cin>>u>>v;
            to[u].push_back(v);
            to[v].push_back(u);
            d[u]++, d[v]++;
        }
        maxx=*max_element(d+1,d+n+1); 
        vector<int> m1;
        for(int i=1;i<=n;i++){
            if(d[i]==maxx){
                m1.push_back(i);
                vis[i]=1;
            }
        }
        for(int i=0;i<m1.size();i++){
            int x=m1[i];
            int tmp=0;
            for(int j=0;j<to[x].size();j++){
                int y=to[x][j];
                tmp+=vis[y];
            }
            if(tmp<=1){
                id=x;
                break;
            }
        }
        ans+=d[id];
        for(int i=0;i<to[id].size();i++){
            int y=to[id][i];
            d[y]--;
        }
        d[id]=0;
        maxx=*max_element(d+1,d+n+1);
        
        for(int i=1;i<=n;i++){
            if(d[i]==maxx){
                id=i;
                break;
            }
        }
        ans+=d[id];
        cout<<ans-1<<endl;
    }
    return 0;
}
Problem: 2060D - Subtract Min Sort
Language: c++17 

Solution 1 (runtime: 109 ms):
#include <bits/stdc++.h>
#define mis(x) cout << #x <<" = " <<x<<endl
#define rep(i, a, b) for(int i=(a);(i)<(b);(i)++)
#define re(i,a) rep((i),0,(a))
#define eb emplace_back
#define x first
#define y second
	//	#define cin fin
using namespace std;
typedef long long ll;
ifstream fin("Sisend.txt");
void ye(bool b){cout<<(b? "Yes\n":"NO\n");}
const int N=2e5+9;
ll tt,n,m,k;
bool boo(pair<int,int> a,pair<int,int> b){
	return a.x<b.x;
}
int main(){
	ios::sync_with_stdio(0);cin.tie(0);	cout.tie(0);cerr.tie(0);
	cin>>tt;while(tt--){
		cin>>n;
		vector<int> v(n);
		re(i,n)cin>>v[i];
		int in=-1;
		for(int i=n-1;i>=1;i--){
			if(v[i-1]> v[i]){
				in=i-1;break;
			}
		}
		bool saab=(in==-1);
		if(!saab){
			saab=1;
			re(i,in+1){
				if(v[i]>v[i+1]){
					saab=0;break;
				}
				v[i+1]-=v[i];
			}
		}
		if(saab)cout<<"YES\n";else cout<<"NO\n";
	}
}



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

#define ll long long

using namespace std;



int main(int argc, char** argv) {

    ios_base::sync_with_stdio(0);

    cin.tie(0);

    int tt, n;

    cin >> tt;

    while(tt--) {

        cin >> n;

        vector<int> v(n);

        for(int &x: v) {

            cin >> x;

        }

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

            int minV = min(v[i], v[i - 1]);

            if(v[i] == 0 || v[i - 1] == 0) continue;

            v[i] -= minV, v[i - 1] -= minV;

        }

        int ans = true;

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

            if(v[i + 1] < v[i]) ans = false;

        }

        if(ans) cout << "YES\n";

        else cout << "NO\n";

    }

    return 0;

}



// Maura Eu amo-te
Problem: 2050C - Uninteresting Number
Language: c++17 

Solution 1 (runtime: 93 ms):
#include <bits/stdc++.h>
#define ll long long int
using namespace std;

int main() {
    int t;
    cin >> t;
    while (t--) {
        string n;
        cin >> n;
        vector<int> digits;
        int sum = 0;
        for (char c : n) {
            int digit = c - '0';  
            digits.push_back(digit);
            sum += digit;  
        }
        if (sum % 9 == 0) {
            cout << "YES" << endl;
            continue;
        }
        int count2 = 0, count3 = 0;
        for (int digit : digits) {
            if (digit == 2) count2++;
            if (digit == 3) count3++;
        }
        bool possible = false;
        for (int i = 0; i <= count2 && !possible; i++) { 
            for (int j = 0; j <= count3 && !possible; j++) { 
                int newSum = sum + i * 2 + j * 6;
                if (newSum % 9 == 0) {
                    possible = true;
                    break;
                }
            }
        }
        if (possible) {
            cout << "YES" << endl;
        } else {
            cout << "NO" << endl;
        }
    }
    return 0;
}


Solution 2 (runtime: 62 ms):
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
const ll mod = 1000000007;
#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define ppb pop_back
#define fi first
#define se second
#define si(x) int(x.size())
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define repk(i,a,b) for(int i=a;i<=b;i++)
void pr(string x){
    cout<<x<<endl;
}
void pr(ll x){
    cout<<x<<endl;
}
void pr(int x){
    cout<<x<<endl;
}

int main(){
    fastio();
    int t=1;
    cin>>t;
    while(t--){
        string s;
        cin>>s;
        int sum = 0;
        int two=0,three=0;
        for(int i=0;i<s.length();i++){
            if(s[i]=='2') two++;
            if(s[i]=='3') three++;
            sum += int(s[i]-'0');
        }
        string ans = "NO";
        for(int i=0;i<=min(two,8);i++){
            for(int j=0;j<=min(three,2);j++){
                int curr = i*2 + j*6;
                if((sum+curr)%9==0) {
                    ans = "YES";
                    break;
                }
            }
        }
        pr(ans);
    }
}