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.bchetty.gcj2012;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

/**
 *
 * @author Babji Prashanth, Chetty
 */
public class DancingGooglers {
    public static void main(String[] args) {
        DancingGooglers dancingGooglers = new DancingGooglers();
        LineNumberReader lineNumberReader = null;
        BufferedWriter bufferedWriter = null;

        try {
            lineNumberReader = new LineNumberReader(new FileReader("/Users/babjichetty/Downloads/B-small-attempt0.in"));
            bufferedWriter = new BufferedWriter(new FileWriter("/Users/babjichetty/Downloads/B-small.out.txt"));
            String line = null;
            int index = 0;
            int count = 0;

            while ((line = lineNumberReader.readLine()) != null) {
                if(index == 0) {
                    count = Integer.parseInt(line);
                    System.out.println("Count : " + count);
                    index++;
                    continue;
                }
                if(index > count) {
                    break;
                }
                String[] arr = line.split(" ");
                int numGooglers = Integer.parseInt(arr[0]);
                int surprise = Integer.parseInt(arr[1]);
                int threshold = Integer.parseInt(arr[2]);
                ArrayList<Integer> scoreList = new ArrayList<Integer>();
                
                for(int i=3;i<3+numGooglers;i++) {
                    scoreList.add(Integer.parseInt(arr[i]));
                }
                
                bufferedWriter.write("Case #" + index + ": " + dancingGooglers.findMaxGooglers(numGooglers, surprise, threshold, scoreList));
                bufferedWriter.newLine();
                index++;
                
            }
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            //Close the BufferedWriter
            try {
                if (lineNumberReader != null) {
                    lineNumberReader.close();
                }
                if (bufferedWriter != null) {
                    bufferedWriter.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
    
    public int findMaxGooglers(int N, int s, int p, ArrayList<Integer> scoreList) {
        int res = 0;
        int surpriseNum = s;
        int threshold = p + (2 * (p - 2));
        Collections.sort(scoreList);
        
        for(Integer score: scoreList) {
            if(score >= p) {
                System.out.print("Score : " + score + " divided into : ");
                int[] triplet = split(score);
                
                if(triplet[2] >= p) {
                    res++;
                } else if(surpriseNum > 0 && score >= threshold) {
                    res++;
                    surpriseNum--;
                }
            }
        }
        
        return res;
    }
    
    private int[] split(int score) {
        int div = score/3;
        int rem = score - (3 * div);
        int[] triplet = new int[3];
        triplet[0] = div;
        triplet[1] = div;
        triplet[2] = div;
        //System.out.println("Div : " + div + " Init Rem : " + rem);
        
        int index = 0;
        while(rem > 0) {
            if(index > 2) {
                index = 0;
            }
            triplet[index] += 1;
            index++;
            rem--;
        }
        
        Arrays.sort(triplet);
        System.out.println(triplet[0] + " - " + triplet[1] + " - " + triplet[2]);
        
        return triplet;
    }
}
