package problem1;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Arrays;





public class Problem1 {

    public static void main(String[] args) throws FileNotFoundException, IOException{
        try {
             TextIO.readFile("L:\\Coodejam\\Input\\Input.txt");
        }
        catch (IllegalArgumentException e) {
              System.out.println("Can't open file ");
              System.exit(0);
         }
        
        FileOutputStream fout = new FileOutputStream("L:\\Coodejam\\Output\\output.txt");
        PrintStream ps=new PrintStream(fout);
        
        int cases=TextIO.getInt();
        int googlers=0,surprising=0,least=0;
        for(int i=0;i<cases;i++){
            int counter=0;
            googlers=TextIO.getInt();
            surprising=TextIO.getInt();
            least=TextIO.getInt();
            int score[]=new int[googlers];
            for(int j=0;j<googlers;j++){
                score[j]=TextIO.getInt();
                 }
            
            
            
            
            Arrays.sort(score);
            int temp=0;
            int sup[]=new int[surprising];
            
          for(int j=0;j<googlers && surprising>0;j++){
               if(biggestNS(score[j])<least && biggestS(score[j])==least){
                   surprising--;
                   counter++;
                }
               }
           
            for(int j=0;j<googlers;j++){
                if(surprising==0)temp=biggestNS(score[j]);
                    
                else {
                    temp=biggestS(score[j]);
                    surprising--;     
                } 
                   if(temp>=least)counter++;
           
             }
            
             ps.println("Case #"+(i+1)+": "+counter);
        }
    }
    
   static int biggestNS(int x){
       if(x==0)return 0;
       if(x==1)return 1;
       return ((x-1)/3)+1;
   }
   
   static int biggestS(int x){
       if(x==0)return 0;
       if(x==1)return 1;
       return ((x-2)/3)+2;
   }
}
package com.codejam.two12.GooglerDance;

import java.util.ArrayList;
import java.util.Arrays;

import com.jp.common.Puzzle;

public class GooglerDance implements Puzzle{

  private int numberOfGooglers;
  private int surprisingTriplets;
  private int parValue;
  private int[] totalPointArray;
  private int currentNumberOfSurprises;
  private ArrayList<Integer> ResultList;
  
  @Override
  public String[] solve(String[] dataSet) {
    // TODO Auto-generated method stub
    ResultList = new ArrayList<Integer>();
    processInput( dataSet);
    calculateResult();
    
    String result[] = new String[1];  
    result[0] = String.valueOf(ResultList.size());
    return result;
  }
  
  public void processInput(String[] dataSet){
    String data[] = dataSet[0].split(" ");
    numberOfGooglers = Integer.parseInt(data[0]);
    surprisingTriplets = Integer.parseInt(data[1]);
    parValue = Integer.parseInt(data[2]);
    
    totalPointArray = new int[numberOfGooglers];
    
    for(int i = 0; i < totalPointArray.length; i++){
      totalPointArray[i] = Integer.parseInt(data[ 3 + i]);
    }
    
    Arrays.sort(totalPointArray);
    
   }
  
  public void calculateResult(){
    
    for (int i = 0; i < totalPointArray.length; i++){
      int total = totalPointArray[i];
      int remainder = total / 3;
      int quotient = total % 3;
      
      int diff = parValue - remainder;
      
      // This cant be part of result. so skip and continue
      if(diff > 2 || (total == 0 && parValue != 0) ){
        continue;
      }
      else if(diff <= 0 ){
        ResultList.add(total);
        continue;
      }
      
      if(diff == 1 && quotient == 0 && currentNumberOfSurprises < surprisingTriplets){
        ++ currentNumberOfSurprises;
        ResultList.add(total);
      }
      else if(diff == 1 && quotient == 1 ){
        if(currentNumberOfSurprises < surprisingTriplets)
          ++ currentNumberOfSurprises;
        
        ResultList.add(total);
      }
      else if(diff == 1 && quotient == 2){
        ResultList.add(total);
      }
      else if(diff == 2 && quotient == 2 && currentNumberOfSurprises < surprisingTriplets){
        ++ currentNumberOfSurprises;
        ResultList.add(total);
      }
    }
    
    
  }
  
}
