Chef and new Recepie Wrong Ans

Why am i getting wrong ans for my this code

import java.io.*;
import java.util.*;
class CHEFRP 
{

public static void main(String[] args)throws IOException{
    
    int TESTCASES;
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    TESTCASES=Integer.parseInt(br.readLine());
    int b=2*TESTCASES,i=0,m,l;
    int a;
    String[] lines=new String[b];
    int[] N=new int[TESTCASES];
    for(a=0;a<b;a++)
    { 
        lines[a]=br.readLine();
    }
       
    

     int[][] Ai =new int[TESTCASES][100001];     
  
        for(a=0;a<b;a=a+2)               
    {
        N[a/2]=Integer.parseInt(lines[a]);     
    }

    
    for(a=1;a<b;a=a+2)      
    {
        StringTokenizer stz=new StringTokenizer(lines[a]);
        for(l=0;l<100001;l++)
        {
         if(stz.countTokens()!=0) 
            {
                    Ai[((a-1)/2)][l]=Integer.parseInt(stz.nextToken());  
            }
         else{
             
             break;
         }
        }
       
    }
        
        
 
       
    int[] min=new int[TESTCASES];
   
    for(a=0;a<TESTCASES;a++)
    {
        min[a]=Ai[a][0];
        
        for(l=0;l<N[a];l++)
          {
                    if(min[a]>Ai[a][l])
                    {
                        min[a]=Ai[a][l];
                           
                    }
                
             
        }
    }
    
    
    int[] sum=new int[TESTCASES];
     
    for(a=0;a<TESTCASES;a++)
          {
              for (l=0;l<N[a];l++)     
              {     
                  sum[a]=sum[a]+Ai[a][l];
              }
            sum[a]=sum[a]+2-min[a];
    }
    int flag=0,minusonecase=-1;
    for(a=0;a<TESTCASES;a++)
             {
                 for(l=0;l<N[a];l++)
                 {
                     if(Ai[a][l]==1)
                     {  
                        System.out.println(minusonecase);
                        flag=1;
                        break;
                        
                     }
                     
                 }
                 if(flag==1)
                 {
                     continue;
                 }
                 System.out.println(sum[a]);
             }

}
}

You are making this problem a bit complex. You just need to sort the array in descending order. And select the quantity of ingredient(whole since the problem asks for the maximum no. of times she can pick the items). Just take care for the last one since she can choose only 2 from the last quantity remaining in the array. And if you get any quantity <2 in the array output -1.

2 Likes