import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

public class CodeJam {

//	public static final String INPUT_FILE_PATH = "D://CodeJamInput.txt";
	public static final String INPUT_FILE_PATH = "D://B-large.in";
	public static final String OUTPUT_FILE_PATH = "D://CodeJamOutput.txt";

	public static List<String> readInput() {
		List<String> list = new ArrayList<String>();
		try {
			BufferedReader input = new BufferedReader(new FileReader(
					INPUT_FILE_PATH));
			try {
				String line = null;

				while ((line = input.readLine()) != null) {
					list.add(line);
				}
			} finally {
				input.close();
			}
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		
		return list;
	}

	public static void writeOutput(List<String> output) {
		PrintWriter writer = null;
		try {
			writer = new PrintWriter(new FileWriter(OUTPUT_FILE_PATH));
			for (String line : output) {
				writer.println(line);
			}
			writer.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (writer != null)
				writer.close();
		}
	}
	
	public static void main(String[] args) {
		List<DanceFloor> danceFloors = DanceFloor.parseFromFile(CodeJam.readInput());
		List<String> output = new ArrayList<String>();

		for(int i = 0; i < danceFloors.size(); i++) {
			System.out.println(danceFloors.get(i));
			String line = "";
			line += "Case #" + (i + 1) + ": " + danceFloors.get(i).calculate();
			output.add(line);
		}
		
		CodeJam.writeOutput(output);
	}
}

class DanceFloor {

	private List<Integer> tripletsTotalCounts;
	private int surpriseCount;
	private int targetScore;
	private int dancersCount;

	public DanceFloor(List<Integer> tripletsTotalCounts, int surpriseCount,
			int targetScore, int dancersCount) {
		this.tripletsTotalCounts = tripletsTotalCounts;
		this.surpriseCount = surpriseCount;
		this.targetScore = targetScore;
		this.dancersCount = dancersCount;
	}

	public int calculate() {
		int result = 0;
		
		if (targetScore == 0) return dancersCount;
		
		for (int i = 0; i < tripletsTotalCounts.size(); i++) {
			int total = tripletsTotalCounts.get(i);

			if (total < (targetScore * 3 - 4)) {
				tripletsTotalCounts.remove(i--);
			} else if (total == 0) {
				tripletsTotalCounts.remove(i--);
			}
		}
		
		for (int i = 0; i < tripletsTotalCounts.size(); i++) {
			int total = tripletsTotalCounts.get(i);

			if ((surpriseCount > 0) && (total == targetScore * 3 - 4)) {
				result++;
				surpriseCount--;
				tripletsTotalCounts.remove(i--);
			} else if ((surpriseCount == 0) && (total == targetScore * 3 - 4)) {
				tripletsTotalCounts.remove(i--);
			}
		}
		
		for (int i = 0; i < tripletsTotalCounts.size(); i++) {
			int total = tripletsTotalCounts.get(i);

			if ((surpriseCount > 0) && (total == targetScore * 3 - 3)) {
				result++;
				surpriseCount--;
				tripletsTotalCounts.remove(i--);
			} else if ((surpriseCount == 0) && (total == targetScore * 3 - 3)) {
				tripletsTotalCounts.remove(i--);
			}
		}
		
		result += tripletsTotalCounts.size();
		
		return result;
	}

	public static List<DanceFloor> parseFromFile(List<String> readInput) {
		List<DanceFloor> result = new ArrayList<DanceFloor>();
		int testCaseCount = Integer.parseInt(readInput.get(0));

		for (int i = 0; i < testCaseCount; i++) {
			String line[] = readInput.get(i + 1).split(" ");

			List<Integer> totalCounts = new ArrayList<Integer>();
			for (int j = 0; j < Integer.parseInt(line[0]); j++) {
				totalCounts.add(Integer.parseInt(line[3 + j]));
			}

			result.add(new DanceFloor(totalCounts, Integer.parseInt(line[1]),
					Integer.parseInt(line[2]), Integer.parseInt(line[0])));
		}

		return result;
	}

	@Override
	public String toString() {
		return "dancersCount = " + dancersCount + "; surpriseCount = " + surpriseCount 
								 + "; targetScore = " + targetScore + "; tripletsTotalCounts" 
								 + tripletsTotalCounts.toString();
	}
}


package qual2012.B;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class MainDefault2 {

    //private static String inputFileName = "sample.in";
    //private static String inputFileName = "B-small-attempt1.in";
    private static String inputFileName = "B-large.in";
    //private static String outputFileName = "sample.out";
    //private static String outputFileName = "B-small-1.out";
    private static String outputFileName = "B-large.out";

    private static int nbrOfTests;
    private static String inputFolder = "input/";
    private static String outputFolder = "output/";

    /**
     * @param args
     */
    public static void main(String[] args) {

        MainDefault2 mainDefault = new MainDefault2();

        MyTestCase[] input = mainDefault.readInputCodeJam(inputFileName);
        StringBuffer output = mainDefault.createOutput(input);
        printToFile(output, outputFileName);
    }

    public MainDefault2() {
        // TODO if anything should be prepared for all testcases
    }

    public MyTestCase[] readInputCodeJam(String fileName) {
        MyTestCase[] testCases = null;
        File file = new File(inputFolder + fileName);
        try {
            Scanner scanner = new Scanner(file);

            nbrOfTests = Integer.valueOf(scanner.nextLine());
            testCases = new MyTestCase[nbrOfTests];

            for (int i = 0; i < nbrOfTests; i++) {
                // TODO line(s) / testcase
                String tmp = scanner.nextLine();

                String[] testCaseInput = tmp.split(" ");
                MyTestCase obj = new MyTestCase(testCaseInput);

                testCases[i] = obj;
            }

            scanner.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return testCases;
    }

    public StringBuffer createOutput(MyTestCase[] myTestCases) {
        StringBuffer buffer = new StringBuffer();
        int count = 1;
        for (int i = 0; i < nbrOfTests; i++) {
            buffer.append("Case #" + count++ + ": ");

            MyTestCase myTestCase = myTestCases[i];
            buffer.append(myTestCase.getAnswer());

            buffer.append("\n");
        }
        return buffer;
    }

    public static void printToFile(StringBuffer output, String fileName) {
        try {
            FileWriter fstream = new FileWriter(outputFolder + fileName);
            BufferedWriter out = new BufferedWriter(fstream);
            out.write(output.toString());
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    class MyTestCase {
        private String answer;
        private int nbrOfGooglers;
        private int nbrOfSuprises;
        private int bestResult;
        private int[] totalPoints;

        public MyTestCase(String[] input) {
            nbrOfGooglers = Integer.valueOf(input[0]);
            nbrOfSuprises = Integer.valueOf(input[1]);
            bestResult = Integer.valueOf(input[2]);

            totalPoints = new int[nbrOfGooglers];
            for (int i = 0; i < nbrOfGooglers; i++) {
                totalPoints[i] = Integer.valueOf(input[3 + i]);
            }
            if(bestResult == 0) {
                answer = String.valueOf(nbrOfGooglers);
            } else if(bestResult == 1) {
                int atLeastCount = 0;
                for(int oneTot : totalPoints) {
                    if(oneTot != 0) {
                        atLeastCount++;
                    }
                }
                answer = String.valueOf(atLeastCount);
            } else {
                solve();
            }
        }

        private void solve() {
            //System.out.println("nbrOf: " + nbrOfGooglers + ", suprises: " + nbrOfSuprises + ", bestResult: " + bestResult);
            int count = 0;
            for(int oneTot : totalPoints) {
                //System.out.println("oneTot: " + oneTot);
                int limitNbr = 3*bestResult;
                int inclNbr = limitNbr - 2;
                int supNbr = limitNbr - 4;
                if(oneTot >= inclNbr) {
                    count++;
                } else if (oneTot >= supNbr) {
                    if(nbrOfSuprises > 0) {
                        nbrOfSuprises--;
                        count++;
                    }
                }
            }
            answer = String.valueOf(count);
        }
        
        private void solve2() {
            int count = 0;
            for (int tot : totalPoints) {
                int avgNbr = tot / 3;
                //System.out.println("tot: " + tot + " avg: " + avgNbr + " bestResult: " + bestResult);
                if (avgNbr == 0) {
                    if(bestResult <= tot) {
                        //System.out.println("small");
                        count++;
                    }
                } else if (avgNbr >= bestResult) {
                    count++;
                    //System.out.println(count);
                } else if (avgNbr + 1 >= bestResult) {
                    int oneExtra = tot % 3;
                    if (oneExtra > 0) {
                        count++;
                        //System.out.println("one: " + count);
                    } else {
                        if (nbrOfSuprises > 0) {
                            nbrOfSuprises--;
                            count++;
                            //System.out.println("two: " + count);
                        }
                    }
                } else if (avgNbr + 2 >= bestResult) {
                    int twoExtra = tot % 3;
                    if (twoExtra == 2 && nbrOfSuprises > 0) {
                        nbrOfSuprises--;
                        count++;
                        //System.out.println("three: " + count);
                    }
                }
            }
            System.out.println("finish: " + count);
            System.out.println("nbrOfSuprises left: " + nbrOfSuprises);
            answer = String.valueOf(count);
        }

        public String getAnswer() {
            return answer;
        }
    }
}
