package code12.qualification;

import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;

public class B {
	public static String solve(int N, int S, int p, int[] t) {
		
		// 3a 		-> (a, a, a), *(a - 1, a, a + 1)
		// 3a + 1	-> (a, a, a + 1), *(a - 1, a + 1, a + 1)
		// 3a + 2	-> (a, a + 1, a + 1), *(a, a, a + 2)
		
		int numPNoS = 0;
		int numPWithS = 0;
		
		for (int i = 0; i < N; i++) {
			int a = (int)Math.floor(t[i] / 3);
			int r = t[i] % 3;
			
			switch (r) {
			case 0:
				if (a >= p) {
					numPNoS++;
				} else if (a + 1 >= p && a - 1 >= 0) {
					numPWithS++;
				}
				break;
			case 1:
				if (a >= p || a + 1 >= p) {
					numPNoS++;
				}
				break;
			case 2:
				if (a >= p || a + 1 >= p) {
					numPNoS++;
				} else if (a + 2 >= p) {
					numPWithS++;
				}
				break;
			}
		}
		
		return "" + (numPNoS + Math.min(S, numPWithS));
	}

	public static void main(String[] args) throws Exception {
		// file name
		//String fileName = "Sample";
		//String fileName = "B-small";
		String fileName = "B-large";
		
		// file variable
		File inputFile = new File(fileName + ".in");
		File outputFile = new File(fileName + ".out");
		Scanner scanner = new Scanner(inputFile);
		FileWriter writer = new FileWriter(outputFile);

		// problem variable
		int totalCase;
		int N, S, p;
		int[] t;

		// get total case
		totalCase = scanner.nextInt();

		for (int caseIndex = 1; caseIndex <= totalCase; caseIndex++) {
			// get input
			N = scanner.nextInt();
			S = scanner.nextInt();
			p = scanner.nextInt();
			t = new int[N];
			
			for (int i = 0; i < N; i++) {
				t[i] = scanner.nextInt();
			}
			
			String output = "Case #" + caseIndex + ": " + solve(N, S, p, t);
			System.out.println(output);
			writer.write(output + "\n");
		}
		scanner.close();
		writer.close();
	}
}

package exercise;

public class Solver {
	private static int	VOTES_NB = 3;
	private static int	VARIABLES_NB = 3;
	
	public static Integer[]	solve(Integer[][] input) {
		Integer[]	result = new Integer[input.length];
		
		for (int cnt = 0; cnt < input.length; ++cnt)
			result[cnt] = solve(input[cnt]);
		
		return result;
	}

	public static int	solve(Integer[] input) {
		if (input == null || input.length < 3)
			return 0;
		
		int	googlersNb = input[0];
		int	surprisingTriplets = input[1];
		int	target = input[2];
		
		if (input.length < VARIABLES_NB + googlersNb)
			return 0;
		
		int	result = 0;
		
		Integer[]	candidates = new Integer[surprisingTriplets]; 
		for (int cnt = VARIABLES_NB; cnt < input.length; ++cnt) {
			if (input[cnt] < target * VOTES_NB - (VOTES_NB - 1)) {
				int	minIndex = 0;
				int	min = -1;
				for (int tmp = 0; tmp < candidates.length; ++tmp) {
					if (candidates[tmp] == null || input[cnt] > candidates[tmp]) {
						if (candidates[tmp] == null || min == -1 || candidates[tmp] < min) {
							min = (candidates[tmp] == null) ? 0 : candidates[tmp];
							minIndex = tmp;
						}
					}
				}
				if (min >= 0 && input[cnt] >= min)
					candidates[minIndex] = input[cnt];
			} else
				++result;
		}
		
		if (candidates.length > 0) {
			int	surprisingTarget = target + target - 2 + target - 2;
			if (surprisingTarget <= 0)
				surprisingTarget = target;
			if (candidates != null)
				for (int cnt = 0; cnt < candidates.length; ++cnt)
					if (candidates[cnt] != null && candidates[cnt] >= surprisingTarget)
						result++;
		}
		
		return result;
	}
}
