import java.util.*;
import java.io.*;

public class DancingWithTheGooglers
{
    public DancingWithTheGooglers()
    {
        Scanner inFile = null;
        try
        {
            inFile = new Scanner(new File("inputB.txt"));
        }
        catch(Exception e)
        {
            System.out.println("Problem opening input file. Exiting...");
			System.exit(0);
        }
        int t = inFile.nextInt();
        
        int [] results = new int [t];
        DancingWithTheGooglersMax [] rnc = new DancingWithTheGooglersMax[t];
        
        int i, j;
        int [] scores;
        int s, p;
        for(i = 0; i < t; i++)
        {
            scores = new int [inFile.nextInt()];
            s = inFile.nextInt();
            p = inFile.nextInt();
            for(j = 0; j < scores.length; j++)
                scores[j] = inFile.nextInt();
            
            rnc[i] = new DancingWithTheGooglersMax(i, scores, s, p, results);
            rnc[i].start();
        }
        inFile.close();
        
        boolean done = false;
        
        while(!done)
        {
            done = true;
            for(i = 0; i < t; i++)
                if(rnc[i].isAlive())
                {
                    done = false;
                    break;
                }
        }
        
        PrintWriter outFile = null;
        try
        {
            outFile = new PrintWriter(new File("outputB.txt"));
        }
        catch(Exception e)
        {
            System.out.println("Problem opening output file. Exiting...");
			System.exit(0);
        }
        
        for(i = 0; i < results.length; i++)
            outFile.printf("Case #%d: %d\n", i+1, results[i]);
        outFile.close();
    }
    
    public static void main(String [] args)
    {
        new DancingWithTheGooglers();
    }
    
    private class DancingWithTheGooglersMax extends Thread
    {
        int id;
        int s, p;
        int [] results, scores;
        
        public DancingWithTheGooglersMax(int i, int [] aScores, int aS, int aP,int [] res)
        {
            id = i;
            s = aS;
            p = aP;
            results = res;
            scores = aScores;
        }
        
        public void run()
        {
            int a = 0, b = 0;
            
            if(p == 0)
                results[id] = scores.length;
            else if(p == 1)
            {
                int y;
                y = 3*p - 3;
                
                for(int i = 0; i < scores.length; i++)
                    if(scores[i] > y)
                        a++;
                    else if(scores[i] > 0)
                        b++;
                
                b = Math.min(b, s);
                results[id] = a+b;
            }
            else
            {
                int y, z;
                y = 3*p - 3;
                z = 3*p - 5;
                
                for(int i = 0; i < scores.length; i++)
                    if(scores[i] > y)
                        a++;
                    else if(scores[i] > z)
                        b++;
                
                b = Math.min(b, s);
                results[id] = a+b;
            }
        }
    }
}
package codeJam;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * Created by IntelliJ IDEA.
 * User: vsaurabh
 * Date: 4/14/12
 * Time: 6:10 AM
 * To change this template use File | Settings | File Templates.
 */
public class DancingWithTheGooglers {

    public static void main(String args[]) throws Exception {
        String inputPath = "C:\\myTestProj\\src\\codeJam\\B-large.in";
        String outputPath = "C:\\myTestProj\\src\\codeJam\\B-large.out";

        DancingWithTheGooglers dwg = new DancingWithTheGooglers();
        String[] str = dwg.readFromFile(inputPath);

        str = dwg.solution(str);
        dwg.writeToFile(outputPath, str);
    }

    private String[] solution(String[] str){
        String[] strNew = new String[str.length-1];
        for(int i=0; i<strNew.length; i++){
            strNew[i] = "Case #" +(i+1)+ ": " + find(str[i + 1]);
        }
        return strNew;
    }

    public int find(String str){
        String s1[] = str.split(" ");
        int n = Integer.parseInt(s1[0]);
        int s = Integer.parseInt(s1[1]);
        int p = Integer.parseInt(s1[2]);
        int y=0;
        int[] t = new int[n];
        for(int i=0; i<n; i++){
            t[i] = Integer.parseInt(s1[i+3]);
        }
        Arrays.sort(t);
        for(int x : t){
            int max1=0;
            int max2=0;
            if(x < 2){
                max1=max2=x;
            }
            else if(x==2){
                max1=1;
                if(s > 0){
                    max2=2;
                }
                if(max2 >= p){
                    s--;
                }
            }
            else if(x%3 == 0){
                max1 = x/3;
                if(s > 0 && max1<p){
                    max2 = x/3 + 1;
                    if(max2 >= p){
                        s--;
                    }

                }
            }
            else if(x%3 == 1){
                max1 = x/3 + 1;
            }
            else{
                max1 = x/3 + 1;
                if(s > 0 && (x/3 + 2) <= 10 && max1<p){
                    max2 = x/3 + 2;
                    if(max2 >= p){
                        s--;
                    }
                }
            }
            if(max1>=p || max2>=p){
                y++;
            }
        }

        return y;
    }



    public String[] readFromFile(String inputPath) throws FileNotFoundException,IOException {
        FileInputStream fis = null;
        DataInputStream dis = null;
        BufferedReader br = null;
        ArrayList<String> strArrList = new ArrayList<String>();
        try{
            fis = new FileInputStream(inputPath);
            dis = new DataInputStream(fis);
            br = new BufferedReader(new InputStreamReader(dis));
            String strLine;
            while ((strLine = br.readLine()) != null)   {
                strArrList.add(strLine);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            try{
                if (br != null) {
                    br.close();
                }
                if (dis != null) {
                    dis.close();
                }
                if (fis != null) {
                    fis.close();
                }
            } catch(IOException e) {
                e.printStackTrace();
            }
        }
        return strArrList.toArray(new String[strArrList.size()]);
    }

    public void writeToFile(String outputPath, String[] strArr) {
        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new FileWriter(outputPath));

            for(int i=0; i<strArr.length; i++){
                bw.write(strArr[i]);
                bw.newLine();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bw != null) {
                    bw.flush();
                    bw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
