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 nl.maartenvangrootel.googlecodejam.qualifiers.dancing;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Pattern;

/**
 * Hello world!
 * 
 */
public class DancingScores {
	private File file;

	private static Pattern pattern = Pattern.compile("[\\ ]");

	public DancingScores(String filename) {
		if (filename == null)
			throw new IllegalArgumentException("filename should not be null");

		file = new File(filename);
		if (!(file.exists() && file.isFile()))
			throw new IllegalArgumentException(String.format(
					"'%s' does not exist or is not a file.", filename));

	}

	private BufferedReader getReader() {
		BufferedReader reader = null;
		try {
			FileReader stream = new FileReader(file);
			reader = new BufferedReader(stream);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		return reader;
	}

	public String calculateResult() {
		BufferedReader reader = getReader();

		StringBuilder builder = new StringBuilder();
		try {
			int T = Integer.parseInt(reader.readLine());
			int i = 1;
			String line;
			while ((line = reader.readLine()) != null) {
				if (i > 1) {
					builder.append("\n");
				}
				builder.append("Case #");
				builder.append(i);
				builder.append(": ");
				builder.append(parseLine(line));
				i++;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		return builder.toString();
	}

	public static int parseLine(String line) {
		String[] input = pattern.split(line);
		int googlers = Integer.parseInt(input[0]);
		int maxSurprises = Integer.parseInt(input[1]);
		int bestResultGoal = Integer.parseInt(input[2]);

		int[] totalResults = new int[googlers];
		int[] totalResultsMinusGoal = new int[googlers];

		int surpriseOrNot = 0, not = 0, normal = 0, surpriseOrNormal = 0;

		for (int i = 0; i < googlers; i++) {
			// totalResults[i] = Integer.parseInt(input[i + 3]);
			// totalResultsMinusGoal[i] = totalResults[i] - bestResultGoal;
			int result = Integer.parseInt(input[i + 3]);
			int resultMinusGoal = result - bestResultGoal;

			if (resultMinusGoal < 0
					|| resultMinusGoal < (bestResultGoal - 2) * 2) {
				not++;
			} else if (resultMinusGoal < (bestResultGoal + (bestResultGoal - 2))) {
				surpriseOrNot++;
			} else if (resultMinusGoal == (bestResultGoal + (bestResultGoal - 2))) {
				surpriseOrNormal++;
			} else {
				normal++;
			}

		}
		int retVal = 0;
		if (surpriseOrNot == maxSurprises) {
			// exactly enough surpriseOrNot: all surpriseOrNormal are normal
			retVal = normal + surpriseOrNormal + surpriseOrNot;
		} else if (surpriseOrNot > maxSurprises) {
			// too many surpriseOrNot, all surpriseOrNormals are normal, some
			// surpriseOrNot are not.
			retVal = normal + surpriseOrNormal + maxSurprises;
		} else if (surpriseOrNot < maxSurprises) {
			// Too little surprisesOrNot, take them from surpriseOrNormal, the
			// rest is Normal.
			retVal = normal + surpriseOrNot + surpriseOrNormal;
		}
		return retVal;
	}

	public static void main(String[] args) {
		if (args.length > 0) {
			DancingScores starter = new DancingScores(args[0]);
			String result = starter.calculateResult();

			System.out.println(result);

		} else {
			System.err.println("No arguments given.");
		}

	}
}
