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.qualification;

import com.qualification.ScoreCombo.Score;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class Three {
    public static void main(String[] args) throws FileNotFoundException, IOException {
        FileReader fr = new FileReader("/Users/minerva/Desktop/input.txt");
        FileWriter fw = new FileWriter("/Users/minerva/Desktop/output.txt");
        
        BufferedReader br = new BufferedReader(fr);
        PrintWriter pr = new PrintWriter(fw);
        
        int numInputs = Integer.valueOf(br.readLine());
        
        for(int i=0;i<numInputs;i++) {
            String str = br.readLine();
            String values[] = str.split(" ");
            int[] numValues = new int[values.length];
            for (int j=0;j<values.length;j++) numValues[j] = Integer.parseInt(values[j]);
            pr.println("Case #" + (i + 1) + ": " + compute(numValues));
        }
        pr.flush();
    }
    
    public static int compute(int[] inputs) {
        int numInputs = inputs[0];
        int surprising = inputs[1];
        int best = inputs[2];
        
        List<Score> scores = new ArrayList<Score>();
        for (int indx = 3;numInputs > 0; numInputs--,indx++) {
            Score result = new ScoreCombo(inputs[indx]).testBest(best);
            if (result != null) scores.add(result);
        }
        
        int supCount = 0;
        int num = scores.size();
        
        for(Score item:scores) 
            if (item.isSurprising())
                supCount++;
        
        
        if (supCount > surprising) {
            num -= (supCount - surprising);
        }
        
        return num;
    }
}

class ScoreCombo {
    private Score normalScore;
    private Score surprisingScore;
    
    public ScoreCombo(int total) {
        int rem = total % 3;
        int avg = total / 3;
        
        if (total == 0) {
            normalScore = new Score(0,false);
        } else if (total == 1) {
            normalScore = new Score(1,false);
        } else if (total == 2) {
            normalScore = new Score(1,false);
            surprisingScore = new Score(2,true);
        } else {
            if(rem == 0) {
                normalScore = new Score(avg,false);
                surprisingScore = new Score(avg + 1,true);
            } else if(rem == 1) {
                normalScore = new Score(avg + 1,false);
                surprisingScore = new Score(avg + 1,true);
            } else if(rem == 2) {
                normalScore = new Score(avg + 1,false);
                surprisingScore = new Score(avg + 2,true);
            }
        }
    }
    
    public class Score {
        private int maxScore;
        private boolean flag;
        
        private Score(int maxScore, boolean flag) {
            this.maxScore = maxScore;
            this.flag = flag;
        }
        
        public boolean isSurprising() {
            return flag;
        }
        
        public boolean testBest(int testValue) {
            return maxScore >= testValue; 
        }
    }
    
    public Score testBest(int minScore) {
        if (normalScore.testBest(minScore)) 
            return normalScore;
        else if (surprisingScore != null && surprisingScore.testBest(minScore))
            return surprisingScore;
        else 
            return null;
    }
}
