蓝桥杯测试结果返回 无效的返回

在蓝桥杯里面做题 火星人
写完代码在别的地方测试结果和题给的结果一致,但是在蓝桥杯里面提交一直返回的 无效的返回
以下是我写的代码

Scanner scan = new Scanner(System.in);
        //在此输入您的代码...
        int N = scan.nextInt(); 
        int M = scan.nextInt(); 
        int[] num = new int[N];
        int max = 1;
        for(int i = 0;i < N;i++) {
            num[i] = scan.nextInt();
            max *= num[i];
        }
        
        int[] nums = new int[max + 1];
        for(int i = 1;i <= N;i++) {
            nums[0] += num[i - 1] * Math.pow(10,N - i);
            nums[1] += i * Math.pow(10,N - i);
            
        }    
        int m = 2;
        for(int i = nums[1] + 1;nums[max] == 0;i++) {
            String str = i + "";
            int flag = 1;
            for(int j = 1;j < N;j++) {
                char ch = str.charAt(j);
                
                String str1 = str.substring(0,j);
                if(str1.indexOf(ch) != -1 || ch > N + 48 || ch == 48)
                    flag = 0;
            }
            if(flag == 1) nums[m++] = i;
        }
        for(int i = 0;i < max;i++) {
            if(nums[i] == nums[0]) {
                int term = i;
                i = i +  M + 1;
                String str = nums[i] + "";
                for(int j = 0;j < str.length();j++) {
                    int x = (int)(str.charAt(j) - 48);
                    System.out.print(x + " ");
                }
                break;
            }
        }
        scan.close();