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 codejam.suraj.quals2012.googledancers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import javax.swing.event.TreeExpansionEvent;

import codejam.suraj.quals2012.CodeJamSkeleton;
import codejam.suraj.quals2012.googlerese.Googlerese;

public class GoogleDancers extends CodeJamSkeleton {

	@Override
	protected void handleTestCase(int testCaseNumber) throws IOException {
		// TODO Auto-generated method stub
		
		String line = readLine();
		String[] lineSplits = line.split("\\s+");
		
		int numberOfG = Integer.parseInt(lineSplits[0]);
		int surprises = Integer.parseInt(lineSplits[1]);
		int p = Integer.parseInt(lineSplits[2]);
		List<Integer> scores = new ArrayList<Integer>(lineSplits.length - 3);
		for (int i = 3; i < numberOfG + 3; ++i){
			scores.add(Integer.parseInt(lineSplits[i]));
		}
		
		Iterator<Integer> iter = scores.iterator();
		int requiredSurprises = 0;
		
		int bestAndRequired = 0;
		int numBestResults = 0;
		System.out.println("Test Case " + testCaseNumber);
		System.out.println(line);
		while(iter.hasNext()){
			int score = iter.next();
			int max = score/3;
			if(score > 0 && score%3 != 0)
				max = max + 1;
			
			if(p == max){
				++numBestResults;
			}
			else if(p > max){
				if(score == 2 || score > 2 && score%3 == 0 ||
						score > 2 && score%3 == 2){
					int possibleMax = max + 1;
					if(possibleMax == p){
						++requiredSurprises;
					}
					
				}
				
			}
			else if (p < max){
				int min = 0;
				if(score <= 2){
					min = 0;
					if(p == min){
						++numBestResults;
					}
					
				}
				else{
					if(score%3 == 0){
						min = max - 1;
						if(min == p){
							++requiredSurprises;
							++numBestResults;
							++bestAndRequired;
						}
						else{
							++numBestResults;
						}
					}
					else if (score%3 == 1 || score%3 == 2){
						min = max - 1;
						if(min == p){
							++numBestResults;
						}
						else if (min - 1 == p){
							++requiredSurprises;
							++numBestResults;
							++bestAndRequired;
						}
						else{
							++numBestResults;
						}
					}
				}
			}
			else if (p == max){
				++numBestResults;
			}
		}
		
		int result = numBestResults + Math.min(requiredSurprises - bestAndRequired, surprises);

		
		addOutput(testCaseNumber, ""+result);

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		CodeJamSkeleton testCase = new GoogleDancers();
		testCase.handleAllTestCases(args[0], args[1]);
	}

}
