import java.util.*;
import java.io.*;

public class DancingWithTheGooglers
{
    public DancingWithTheGooglers()
    {
        Scanner inFile = null;
        try
        {
            inFile = new Scanner(new File("inputB.txt"));
        }
        catch(Exception e)
        {
            System.out.println("Problem opening input file. Exiting...");
			System.exit(0);
        }
        int t = inFile.nextInt();
        
        int [] results = new int [t];
        DancingWithTheGooglersMax [] rnc = new DancingWithTheGooglersMax[t];
        
        int i, j;
        int [] scores;
        int s, p;
        for(i = 0; i < t; i++)
        {
            scores = new int [inFile.nextInt()];
            s = inFile.nextInt();
            p = inFile.nextInt();
            for(j = 0; j < scores.length; j++)
                scores[j] = inFile.nextInt();
            
            rnc[i] = new DancingWithTheGooglersMax(i, scores, s, p, results);
            rnc[i].start();
        }
        inFile.close();
        
        boolean done = false;
        
        while(!done)
        {
            done = true;
            for(i = 0; i < t; i++)
                if(rnc[i].isAlive())
                {
                    done = false;
                    break;
                }
        }
        
        PrintWriter outFile = null;
        try
        {
            outFile = new PrintWriter(new File("outputB.txt"));
        }
        catch(Exception e)
        {
            System.out.println("Problem opening output file. Exiting...");
			System.exit(0);
        }
        
        for(i = 0; i < results.length; i++)
            outFile.printf("Case #%d: %d\n", i+1, results[i]);
        outFile.close();
    }
    
    public static void main(String [] args)
    {
        new DancingWithTheGooglers();
    }
    
    private class DancingWithTheGooglersMax extends Thread
    {
        int id;
        int s, p;
        int [] results, scores;
        
        public DancingWithTheGooglersMax(int i, int [] aScores, int aS, int aP,int [] res)
        {
            id = i;
            s = aS;
            p = aP;
            results = res;
            scores = aScores;
        }
        
        public void run()
        {
            int a = 0, b = 0;
            
            if(p == 0)
                results[id] = scores.length;
            else if(p == 1)
            {
                int y;
                y = 3*p - 3;
                
                for(int i = 0; i < scores.length; i++)
                    if(scores[i] > y)
                        a++;
                    else if(scores[i] > 0)
                        b++;
                
                b = Math.min(b, s);
                results[id] = a+b;
            }
            else
            {
                int y, z;
                y = 3*p - 3;
                z = 3*p - 5;
                
                for(int i = 0; i < scores.length; i++)
                    if(scores[i] > y)
                        a++;
                    else if(scores[i] > z)
                        b++;
                
                b = Math.min(b, s);
                results[id] = a+b;
            }
        }
    }
}
package exercicio.b;

import java.awt.List;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class ExerciocioB {

	public static void main(String args[]) throws IOException {

		int[][] combinacoes = new int[1331][3];

		for (int i = 0; i <= 10; i++) {
			for (int j = 0; j <= 10; j++) {
				for (int k = 0; k <= 10; k++) {
					combinacoes[i * 121 + j * 11 + k][0] = i;
					combinacoes[i * 121 + j * 11 + k][1] = j;
					combinacoes[i * 121 + j * 11 + k][2] = k;
				}
			}
		}

		File fileInput = new File("src/exercicio/b/input.txt");

		FileReader fileReaderInput = new FileReader(fileInput);

		BufferedReader leitorInput = new BufferedReader(fileReaderInput);

		String numeroLinhas = leitorInput.readLine();
		Integer numero = new Integer(numeroLinhas);

		String linhaInput = null;
		for (int j = 0; j < numero; j++) {
			linhaInput = leitorInput.readLine();
			String[] linha = linhaInput.split(" ");

			Integer numeroGoo = new Integer(linha[0]);
			Integer numeroSurp = new Integer(linha[1]);
			Integer minScore = new Integer(linha[2]);

			int[] pontos = new int[numeroGoo];
			
			for(int b = 0; b< numeroGoo; b++){
				pontos[b] = new Integer(linha[b+3]);
			}

			boolean[] semSurp = new boolean[numeroGoo];
			boolean[] comSurp = new boolean[numeroGoo];
			for (int k = 0; k < numeroGoo; k++) {
				comSurp[k] = false;
				semSurp[k]=false;
				for (int i = 0; i < combinacoes.length; i++) {
					if (Math.abs(combinacoes[i][0] - combinacoes[i][1]) <= 1
							&& Math.abs(combinacoes[i][0] - combinacoes[i][2]) <= 1
							&& Math.abs(combinacoes[i][1] - combinacoes[i][2]) <= 1
							&& (combinacoes[i][0] + combinacoes[i][1] + combinacoes[i][2]) == pontos[k]
							&& (combinacoes[i][0] >= minScore || combinacoes[i][1] >= minScore || combinacoes[i][2] >= minScore)) {
						semSurp[k] = true;
						break;
					}
				}
				
				for (int i = 0; i < combinacoes.length; i++) {
					if (Math.abs(combinacoes[i][0] - combinacoes[i][1]) <= 2
							&& Math.abs(combinacoes[i][0] - combinacoes[i][2]) <= 2
							&& Math.abs(combinacoes[i][1] - combinacoes[i][2]) <= 2
							&& (combinacoes[i][0] + combinacoes[i][1] + combinacoes[i][2]) == pontos[k]
							&& (combinacoes[i][0] >= minScore || combinacoes[i][1] >= minScore || combinacoes[i][2] >= minScore)) {
						comSurp[k] = true;
						break;
					}
				}
			}
			
			int contaMaior = 0;
			for(int a = 0; a < numeroGoo; a++){
				if(semSurp[a] == true)
					contaMaior++;
			}
			for(int a = 0; a < numeroGoo; a++){
				if(comSurp[a] == true && numeroSurp >0 && !semSurp[a]){
					contaMaior++;
					numeroSurp--;
				} else if (numeroSurp == 0){
					break;
				}
			}
			
			System.out.println("Case #" + (j + 1) + ": "+contaMaior);
		}
	}
}
