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 problem2;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class Blarge implements Runnable {

	private final static long UPDATE_INTERVAL_MILLIS = 3000;
	
	private static long startTime;
	private static long currTime;
	private static long endTime;
	private static int run = 0;
	private static int totalNumberOfRuns = 0; 
	
	private String fileName = "B-large.in";
	
	private BufferedReader br;
	private PrintWriter pw;
	
	/**
	 * This method is called, when set-up is complete. 
	 */
	private void solve() {

		totalNumberOfRuns = Integer.parseInt(readLine());
		
		for (run = 0; run < totalNumberOfRuns; run++) {
			
			String[] ins = readLine().split(" ");
			int index = 0;
			
			int numberOfDancers = Integer.parseInt(ins[index++]);
			int surprisingResults = Integer.parseInt(ins[index++]);
			int minResult =  Integer.parseInt(ins[index++]);
			
			int dancersWithMinResult = 0;
			
			for (int i = 0; i < numberOfDancers; i++) {
				int scoreSum = Integer.parseInt(ins[index + i]);
				
				int singleScore = scoreSum / 3;
				int remainder = scoreSum % 3;
				int bestResult = 0;
				
				if (scoreSum > 0) {
					if (remainder == 1) {
						bestResult = singleScore + 1;
					} else if (remainder == 2) {
						bestResult = singleScore + 1;
						if (bestResult + 1 == minResult && surprisingResults > 0) {
							surprisingResults--;
							bestResult++;
						}
					} else if (remainder == 0) {
						bestResult = singleScore;
						if (bestResult + 1 == minResult && surprisingResults > 0) {
							surprisingResults--;
							bestResult++;
						}
						
					}
				}
				if (bestResult >= minResult) dancersWithMinResult++;
			}
			
			write(run+1, String.valueOf(dancersWithMinResult));
		}
	}
	
	// =====================================================================================
	// File Access
	// =====================================================================================
	
	private String readLine() {
		try {
			return br.readLine();
		} catch (IOException e) {
			throw new IllegalStateException();
		}
	}

	private void write(int caseNumber, String result) {
		pw.println("Case #"+caseNumber+": "+result);
	}
	
	private void loadFile(String fileName) {
		File inFile = new File(this.getClass().getResource(fileName).getFile());
		File outFile = new File(inFile.getAbsolutePath().substring(0, (int)(inFile.getAbsolutePath().length()-2L))+"out");
		outFile.delete();
		try {
			outFile.createNewFile();
			br = new BufferedReader(new InputStreamReader(new FileInputStream(inFile)));
			pw = new PrintWriter(new FileOutputStream(outFile));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	// =====================================================================================
	// Initialisation / cleanup
	// =====================================================================================
	
	public static void main(String[] args) throws InterruptedException {
		System.out.println("Running...");
		startTime = System.currentTimeMillis();
		
		Thread thread = new Thread(new Blarge());
		thread.start();
		
		while (thread.isAlive()) {
			Thread.sleep(UPDATE_INTERVAL_MILLIS);
			updateTime();
		}
		
		endTime = System.currentTimeMillis();
		System.out.println("Finished after "+(endTime-startTime)+" ms");
	}
	
	private static void updateTime() {
		currTime = System.currentTimeMillis();
		float timePerRun;
		if (run > 0)
			timePerRun = (currTime-startTime) / run;
		else
			timePerRun = 0;
		float timeLeft = timePerRun * (totalNumberOfRuns - run);
		
		System.out.println("Runs passed: " + run);
		System.out.println("Estimated time left: "+timeLeft+" ms");
		
	}

	@Override
	public void run() {
		loadFile(fileName);
		solve();
		cleanUp();
	}
	
	private void cleanUp() {
		try {
			pw.flush();
			pw.close();
			br.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}