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.google.codejam._2012;

import java.io.File;
import java.util.Scanner;

public class DancingWithGooglers
{

   int[][] maxScores;
   
   
   private void setup()
   {
      maxScores = new int[31][2];
      
      for(int i = 0; i <= 30; i++)
         maxScores[i][1] = -1;
      
      for(int score = 0; score <= 30; score++)
      {
         for(int s1 = 0; s1 <= 10; s1++)
            for(int s2 = 0; s2 <= 10; s2++)
               for(int s3 = 0; s3 <= 10; s3++)
               {
                  if (s1 + s2 + s3 != score)
                     continue;
                  
                  if (s1 > s2 || s1 > s3 || s2 > s3)
                     continue;
                  
                  if (Math.abs(s1 - s2) > 2 || Math.abs(s1 - s3) > 2 || Math.abs(s2 - s3) > 2)
                     continue;
                  
                  if (Math.abs(s1 - s2) == 2 || Math.abs(s1 - s3) == 2 || Math.abs(s2 - s3) == 2)
                     maxScores[score][1] = s3;
                  else
                     maxScores[score][0] = s3;
               }
      }
      
   }
   

   
   public void solveCases() throws Exception
   {
      setup();
      
      Scanner s = new Scanner(new File("./input/B-large.in"));

      
      int t = s.nextInt();
      s.nextLine();
      
      for(int i = 0; i < t; i++)
         solveProblem(s, i + 1);
   }

   

   
  
   private void solveProblem(Scanner s, int num)
   {
      System.out.print("Case #" + num + ": ");
      
      int n = s.nextInt();
      int sur = s.nextInt();
      int p = s.nextInt();

      int totSurprising = 0;
      int totalBest = 0;
      
      for(int i = 0; i < n; i++)
      {
         int score = s.nextInt();
         
         if (maxScores[score][0] >= p)
         {
            totalBest++;
            continue;
         }
         
         if (maxScores[score][1] != -1 && maxScores[score][1] >= p && totSurprising < sur)
         {
            totalBest++;
            totSurprising++;
         }
      }
      
      
      System.out.println(totalBest);
   }

   
   
   
   
   public static void main(String[] args)
   {
      DancingWithGooglers dwg = new DancingWithGooglers();
      
      try
      {
         dwg.solveCases();
      }
      catch (Exception e)
      {
         e.printStackTrace();
      }
      
   }
}
