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 nl.bertjesapps.googlecodejam;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class ProblemTwo {
	
	enum Result { NOT_POSSIBLE,POSSIBLE_SURPRISING, POSSIBLE};
	private Map<Integer,Map<Integer, Result>> resultMap;
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		if (args.length == 1) {
			String fileName = args[0];
			ProblemTwo problemOne = new ProblemTwo();
			try {
				problemOne.solveFile(fileName);
			} catch (FileNotFoundException fnf) {
				System.out.println("File not found!");
				System.out.println(fnf.getMessage());
			} catch (IOException e) {
				System.out.println("Could not read file!");
			}
		} else {
			System.out.println("Need 1 argument!");
		}
	}

	private void solveFile(String fileName) throws IOException {
		fillResultMap();
		StringBuilder output = new StringBuilder();
		File file = new File(fileName);
		FileReader fileReader = new FileReader(file);
		BufferedReader reader = new BufferedReader(fileReader);
		String numberOfCasesString = reader.readLine();
		int numberOfCases = Integer.parseInt(numberOfCasesString);
		for (int i = 1; i <= numberOfCases; i++) {
			output.append("Case #" + i + ": ");
			output.append(solveLine(reader.readLine()));
			output.append("\r\n");
		}
		//output.append(results.size());
		FileWriter writer = new FileWriter(fileName.replace(".in", ".out"));
		writer.write(output.toString());
		writer.close();
	}
	
	private String solveLine(String lineToBeSolved) {
		int result = 0;
		String[] parts = lineToBeSolved.split(" ");
		int numberOfCandidates = Integer.parseInt(parts[0]);
		int surprisingScores = Integer.parseInt(parts[1]);
		int scoredAtLeast = Integer.parseInt(parts[2]);
		Map<Integer, Result> findMap = resultMap.get(scoredAtLeast); 
		for (int i = 3; i < numberOfCandidates + 3; i++) {
			if (findMap.get(Integer.parseInt(parts[i])) == Result.POSSIBLE) {
				result++;
			} else if (surprisingScores > 0 && findMap.get(Integer.parseInt(parts[i])) == Result.POSSIBLE_SURPRISING) {
				result++;
				surprisingScores--;
			}
		}
		return "" + result;
	}
	
	private void fillResultMap() {
		this.resultMap = new HashMap<>();
		for (int i = 0; i <= 10; i++) {
			Map<Integer, Result> scoreResults = new HashMap<>();
			for (int j = 0; j <=30;j++) {
				scoreResults.put(j, calculateResult(j, i));
			}
			resultMap.put(i, scoreResults);
		}
	}
	
	private Result calculateResult(int googlerScore, int scoredAtLeast) {
		Result res = Result.NOT_POSSIBLE;
		int scoredAtLeastMin1 = scoredAtLeast - 1 >= 0?  scoredAtLeast - 1 : 0;
		int scoredAtLeastMin2 = scoredAtLeast - 2 >= 0?  scoredAtLeast - 2 : scoredAtLeastMin1;
		if (googlerScore >= scoredAtLeast + scoredAtLeastMin1 + scoredAtLeastMin1) {
			res = Result.POSSIBLE;
		} else if (googlerScore >= scoredAtLeast + scoredAtLeastMin2 + scoredAtLeastMin2) {
			res = Result.POSSIBLE_SURPRISING;
		}
		return res;
	}

}


