import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Scanner;


public class Dancing_With_the_Googlers {


    /**
     * The first line of the input gives the number of test cases, T.
     *  T test cases follow. 
     *  Each test case consists of a single line containing integers separated by single spaces. 
     *  The first integer will be N, the number of Googlers, and the second integer will be S, the number of surprising triplets of scores. 
     *  The third integer will be p, as described above. Next will be N integers ti: the total points of the Googlers. 
     * @throws IOException
     */
	   public static void main(String[] args) throws IOException {
	        // TODO Auto-generated method stub
			// Reading the input file
			Scanner in = new Scanner(new File("B-large.in"));
			// writting the input file
			FileOutputStream fos = new FileOutputStream("B-large.out");
			PrintStream out = new PrintStream(fos);
	         //Close the output stream
	        int testCase=Integer.parseInt(in.nextLine());
	        for(int i=0;i<testCase;i++) {
	        	out.print("Case #" + (i + 1) + ":\t");
	        	int NoOfGooglers= in.nextInt();
	        	int NoOfSurprisingResults = in.nextInt();
	        	int atLeastP=in.nextInt();
	        	ArrayList<Integer> scores= new ArrayList<Integer>();
        		int BestResults=0;
        		int Surprising=0;
	        	for(int j=0;j<NoOfGooglers;j++)
	        	{
	        		scores.add(in.nextInt());
	        		//System.out.print(scores.get(j));
	        		int modulus=scores.get(j)%3;
	        		int temp = scores.get(j);
	    			switch (modulus) {
					case 0:
						if (((temp / 3) >= atLeastP)) {
							BestResults++;
						} else {
							if (((((temp / 3) + 1) >= atLeastP) && ((temp / 3) >= 1))
									&& (Surprising < NoOfSurprisingResults)) {
								BestResults++;
								Surprising++;
								
							}
							System.out.println("Case 0"+"itr:"+i+"  surprising:"+temp);
						}
						
						break;
					case 1:
						if ((temp / 3) + 1 >= atLeastP) {
							BestResults++;
							continue;
						}
						
						break;
					case 2:
						if ((temp / 3) + 1 >= atLeastP) {
							BestResults++;
						} else {
							if (((temp / 3) + 2 >= atLeastP)
									&& (Surprising < NoOfSurprisingResults)) {
								BestResults++;
								Surprising++;
							}
							System.out.println("Case 2"+"itr:"+i+"  surprising:"+temp);
						}
						break;
					default:
						System.out.println("Error");
					}
	    			
				}// Internal for
				out.println(BestResults);
				// System.out.println(BestResults);
				System.out.println(Surprising);
				BestResults = 0;
			}// for
			in.close();
			out.close();
			fos.close();

	   }
}

package ch.socaciu.andrei.codejam;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Andrei Socaciu
 */
public class Dancing {

    public static void main(String[] args) throws Exception {
        String in = "data/Dancing-large.in.txt";
        String out = "data/Dancing-large.out.txt";
        String line = null;
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(in)));
        List<String> lines = new ArrayList<String>();
        while ((line = reader.readLine()) != null) {
            lines.add(line);
        }
        reader.close();
        int tests = Integer.parseInt(lines.get(0));
        List<String> res = new ArrayList<String>();
        int k = 1;
        for (int i = 1; i <= tests; i++) {
            line = lines.get(k++);
            String[] parts = line.split(" ");
            int n = Integer.parseInt(parts[0]);
            int[] scores = new int[n];
            int s = Integer.parseInt(parts[1]);
            int p = Integer.parseInt(parts[2]);
            for (int j = 0; j < n; j++) {
                scores[j] = Integer.parseInt(parts[3+j]);
            }
            int result = solve(scores, s, p);
            res.add("Case #" + i + ": " + result);
        }
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(out)));
        for (String re : res) {
            writer.write(re);
            writer.write("\n");
        }
        writer.close();
    }

    private static int solve(int[] scores, int s, int p) {
        int count = 0;
        int special = s;
        for (int score : scores) {
            if (score / 3 >= p) {
                //avg. larger than p, at least one score will be >= p
                count++;
            } else if (score == 3*p - 1 || score == 3*p - 2) {
                //these are covered by the not-special cases
                count++;
            } else if (special > 0) {
                //try to see if a special case matches
                if (score % 3 == 0) { //s-1,s,s+1
                    if (score / 3 + 1 >= p && score / 3 - 1 >= 0)  {
                        count++;
                        special--;
                    }
                } else if (score % 3 == 1) {//s,s,s-2
                    if ((score + 2) / 3 >= p) {
                        count++;
                        special--;
                    }
                } else {//s,s,s+2
                    if ((score - 2) / 3 + 2 >= p) {
                        count++;
                        special--;
                    }
                }
            }
        }
        return count;
    }

}
