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 com.javanme.gcj.one.b;

import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Dancing {

    public static void main(String args[]) {
                
        String input = "B-large";

        Path pathin = FileSystems.getDefault().getPath(input + ".in");
        Path pathout = FileSystems.getDefault().getPath(input + ".out");


        try (PrintWriter pout = new PrintWriter(Files.newOutputStream(pathout, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE))) {
            List<String> lines = Files.readAllLines(pathin, Charset.forName("UTF-8"));

            
            String str = null;
            String[] split = null;
            int countGooglers = 0;
            int countSurprises = 0;
            int bestResult = 0;
            int countBestGooglers = 0;
            int[] scores = null;
            int total = 0;
            int totalAux = 0;
            int dif = 0;
            boolean surprise = false;
            int countLines = lines.size();
            for (int i = 1; i < countLines; i++) {
                str = lines.get(i);
                split = str.split(" ");
                
                countGooglers = Integer.parseInt(split[0]);
                countSurprises = Integer.parseInt(split[1]);
                bestResult = Integer.parseInt(split[2]);
                countBestGooglers = 0;
                
                scores = new int[3];
                total = 0;
                totalAux = 0;
                for(int j = 0; j< countGooglers; j++)
                {
                    total = Integer.parseInt(split[j+3]);
                    totalAux = total;
                    scores[0] = totalAux/3;
                    totalAux -= scores[0];
                    scores[1] = totalAux>>1;
                    totalAux -= scores[1];
                    scores[2] = totalAux;
                    
                    if(scores[0] >= bestResult || scores[1] >= bestResult || scores[2] >= bestResult)
                    {
                        countBestGooglers++;
                        continue;
                    }
                    
                    if(countSurprises > 0 && total > 0)
                    {
                        for(int k = 0; k < scores.length; k++)
                        {
                            if(scores[k]+1 < bestResult)
                            {
                                continue;
                            }
                            
                            dif = 0;
                            for(int m = 0; m < scores.length; m++)
                            {
                                if( k == m)
                                {
                                    continue;
                                }
                                
                                dif = Math.abs((scores[k] + 1) - (scores[m] -1));
                                
                                if(dif == 2)
                                {
                                    countSurprises--;
                                    surprise = true;
                                    countBestGooglers++;
                                    break;
                                }
                            }
                            
                            if(surprise)
                            {
                                break;
                            }
                        }
                    }
                }
                
                
                pout.printf("Case #%d: %d\n", i, countBestGooglers);
            }

        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
