Problem: 2050C - Uninteresting Number
Language: java

Solution 1 (runtime: 327 ms):
import java.util.*;
public class temp{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        sc.nextLine();
        while(t-->0){
            String s=sc.nextLine();
            char[] c=s.toCharArray();
            int[] fre=new int[10];
            int sum=0;
            for(int i=0;i<c.length;i++){
                int cr=c[i]-'0';
                fre[cr]++;
                sum+=cr;
                if(sum>9)sum=sum%10+sum/10;
            }
            int f=-1;
            if(sum==0 || sum==9)f=1;
            else{
                int[][] dp=new int[Math.min(10,fre[2]+1)][Math.min(10,fre[3]+1)];
                for(int i=0;i<dp.length;i++)
                    Arrays.fill(dp[i],-1);
                int ans=solve(sum,dp,1,1);
                if(ans==0)f=1;
            }
            if(f==1)System.out.println("YES");
            else System.out.println("NO");
        }
    }
    public static int solve(int sum,int[][] dp,int i,int j){
        if(sum==0){return 0;}
        if(i==dp.length && j==dp[0].length)return 1;
        if(i<dp.length && j<dp[0].length && dp[i][j]!=-1)return dp[i][j];
        if((i<dp.length && solve((sum+2)%9,dp,i+1,j)==0) || (j<dp[0].length && solve((sum+6)%9,dp,i,j+1)==0)){
            if(i<dp.length && j<dp[0].length)dp[i][j]=0;
            return 0;}
        if(i<dp.length && j<dp[0].length)dp[i][j]=1;
        return 1;
    }
}

Solution 2 (runtime: 296 ms):


import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.*;

 

public class Main {

    static class FastReader {

        BufferedReader br;

        StringTokenizer st;

 

        public FastReader() {

            br = new BufferedReader(

                new InputStreamReader(System.in));

        }

 

        String next() {

            while (st == null || !st.hasMoreElements()) {

                try {

                    st = new StringTokenizer(br.readLine());

                }

                catch (IOException e) {

                    e.printStackTrace();

                }

            }

            return st.nextToken();

        }

 

        int nextInt() { return Integer.parseInt(next()); }

 

        long nextLong() { return Long.parseLong(next()); }

 

        double nextDouble() {

            return Double.parseDouble(next());

        }

 

        String nextLine() {

            String str = "";

            try {

                if (st.hasMoreTokens()) {

                    str = st.nextToken("\n");

                }

                else {

                    str = br.readLine();

                }

            }

            catch (IOException e) {

                e.printStackTrace();

            }

            return str;

        }

    }

    public static int check(ArrayList<Integer> arr,int i,int sum,int k,int dp[][]){

            

            if(i==arr.size()){

                return (sum+k)%9==0?1:0;

            }

            if(dp[i][sum]==-1){

            int take= check(arr,i+1,(sum+arr.get(i))%9,k,dp);

            int ntake=check(arr,i+1,sum,k,dp);

             dp[i][sum]= (take+ntake)>0?1:0;

            }

            return dp[i][sum];

    }

    public static boolean helper(ArrayList<Integer> arr,int k){

        int n=arr.size();

        int dp[][]=new int[n][10];

       

            for(int a[]:dp){

                Arrays.fill(a,-1);

            }

        return check(arr,0,0,k,dp)>0;

    }

    public static void solve(FastReader sc){

        String s=sc.next(); 

        int sum=0;

        ArrayList<Integer> arr=new ArrayList<>();

        for(char c:s.toCharArray()){

            int num=c-'0';

            sum=(sum+num)%9;

            if(num==2){

                arr.add(2);

            }

            else if(num==3){

                arr.add(6);

            }

        }

        if(sum!=0){

           if(helper(arr,sum)){

            System.out.println("YES");

           }

           else{

            System.out.println("NO");

           }

        }

        else{

            System.out.println("YES");

        }





    }

    



    

    

    public static void main(String args[]) {

        FastReader sc = new FastReader();

        int t=sc.nextInt();

        while(t-- >0){

            solve(sc);

        }

    }

}