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 org.cb.projectb.dancingwiththegooglers;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.StringTokenizer;

public class Contest {
	
	public static void main(String[] args) throws FileNotFoundException {
		File input = new File(System.getProperty("user.dir") + "/" + "resources/projectB/B-large.in");
		Scanner sc = new Scanner(input);

		List<DanceOff> contests = new ArrayList<DanceOff>();
		
		if (sc.hasNext()) {
			Integer.parseInt(sc.nextLine()); // number of cases - ignored
			while (sc.hasNext()) {
				String contestString = sc.nextLine();
				
				StringTokenizer contestTokenizer = new StringTokenizer(contestString);
				
				int contestants = Integer.parseInt(contestTokenizer.nextToken());
				int surprisingTriplets = Integer.parseInt(contestTokenizer.nextToken());
				int scoreWeCareAbout = Integer.parseInt(contestTokenizer.nextToken());
				List<Integer> scores = new ArrayList<Integer>();
				for(int i = 0; i < contestants; i ++) {
					scores.add(Integer.parseInt(contestTokenizer.nextToken()));
				}
				
				contests.add(new DanceOff(contestants, surprisingTriplets, scoreWeCareAbout, scores));
			}
		}
		
		for(int i = 1; i <= contests.size(); i ++) {
			DanceOff danceoff = contests.get(i-1);

			System.out.println("Case #" + i + ": " + danceoff.getNumberAllowedToBeImportant());
		}
	}
	
	private static class DanceOff {
		private final int contestants;
		private final int surprisingTriplets;
		private final int scoreWeCareAbout;
		private final List<Integer> contestantScores;
		private final int numberAllowedToBeImportant;
		
		public DanceOff(int contestants, int surprisingTriplets, int scoreWeCareAbout, List<Integer> contestantScores) {
			this.contestants = contestants;
			this.surprisingTriplets = surprisingTriplets;
			this.scoreWeCareAbout = scoreWeCareAbout;
			this.contestantScores = contestantScores;
			
			this.numberAllowedToBeImportant = fillInDetails();
		}

		private int fillInDetails() {
			int importantScores = 0;
			
			List<Integer[]> allProbableScores = new ArrayList<Integer[]>();
			
			for(Integer score: contestantScores) {
//				System.out.print(score + " - ");
				
				int baseScore = score / 3;
				int remaining = score % 3;
				Integer[] scoresArray = new Integer[]{baseScore, baseScore, baseScore};
				
				for(int i = 0; i < remaining; i++) {
					scoresArray[i] = scoresArray[i] + 1;
				}
				
//				System.out.print("Probable tripplet: [");
//				for(int i = 0; i < scoresArray.length; i++) {
//					System.out.print(scoresArray[i] + " ");
//				}
//				System.out.println("]");
				
				allProbableScores.add(scoresArray);
			}
			
			
			int freebeeImportantScores = 0;
			int ableToBeImportantIfSurprising = 0;
			for (Integer[] scoresList : allProbableScores) {
				// count how many in scores array are already important
				boolean isImportant = false;
				boolean canBeSurprising = false;
				
				for (Integer integer : scoresList) {
					if(integer >= this.scoreWeCareAbout) {
						isImportant = true;
						break;
					}
				}
				if(isImportant) {
					freebeeImportantScores++;
				} else {
					// see how many i can make 'surprising' to then care about
					if(scoresList[0] + 1 >= this.scoreWeCareAbout) {
						if(scoresList[0] == scoresList[1] && scoresList[0] != 0) {
							canBeSurprising = true;
						}
					}
					if(canBeSurprising) {
						ableToBeImportantIfSurprising++;
					}
				}
			}
			

			
			// take min of allowed surprising & how many could be surprised
			int numToMakeSpecial = Math.min(ableToBeImportantIfSurprising, this.surprisingTriplets);
			
			// add min to current important scores
			importantScores = freebeeImportantScores + numToMakeSpecial;
			
//			System.out.println("Freebee important: " + freebeeImportantScores);
//			System.out.println("Can be Made Important with surprising Score: " + ableToBeImportantIfSurprising);
//			System.out.println("Num Of Scores we are making surprising: " + numToMakeSpecial);
//			System.out.println("Max Special Scores: " + importantScores);
//			System.out.println();
			
			return importantScores;
		}
		
		public int getNumberAllowedToBeImportant() {
			return numberAllowedToBeImportant;
		}

		@Override
		public String toString() {
			return "DanceOff [contestants = " + contestants
					+ ", surprisingTriplets = " + surprisingTriplets
					+ ", scoreWeCareAbout = " + scoreWeCareAbout
					+ ", contestantScores = " + contestantScores + "]";
		}
	}

}
