import java.util.List;


public class Case implements Runnable {

	private int id;
	private String output;

	private int n;
	private int s;
	private int p;
	private List<Integer> g;

	public Case(int id) {
		this.id = id;
	}

	public Case(int id, int n, int s, int p, List<Integer> g) {
		super();
		this.id = id;
		this.n = n;
		this.s = s;
		this.p = p;
		this.g = g;
	}

	public String solve() {
		int res = 0;
		for (int googler : g) {
			int b = googler / 3;
			boolean div = googler % 3 == 0;
			if (b >= p) {
				res++;
				continue;
			}
			if (p == b+1) {
				if (div && s > 0 && b > 0) {
					res++;
					s--;
					continue;
				}
				if (!div) {
					res++;
					continue;
				}
			}
			if (p == b+2) {
				if (3*b+2 == googler && s > 0) {
					res++;
					s--;
				}
			}
		}
		return String.valueOf(res);
	}

	@Override
	public void run() {
		output = solve();
	}

	@Override
	public String toString() {
		return "Case #" + id + ": " + output;
	}

}
package CodeJam2;

import java.io.*;
import java.util.HashMap;
import java.util.Scanner;

/**
 *
 * @author Ivan du Toit <s29363412>
 */
public class CodeJam2 {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        BufferedReader fIn = null;
        PrintWriter fOut = null;
        try {
            fIn = new BufferedReader(new FileReader("in.txt"));
            fOut = new PrintWriter(new FileWriter("out.txt"));
            
        } catch (IOException e) {
            System.out.println("Invalid file given for output.");     
            System.exit(200);
        }
        
        Scanner in = new Scanner(fIn);
        int numberOfCases = Integer.parseInt(in.nextLine());
        
        for (int c=0; c<numberOfCases; c++) {
            fOut.print("Case #" + (c+1) + ": ");
            int numGoogs = in.nextInt();
            int numSuprises = in.nextInt();
            int bestScore = in.nextInt();
            int result = 0;
            for (int pos=0; pos<numGoogs; pos++) {
                int total = in.nextInt();
                int num = (int) Math.floor(total/3);
                int res = total % 3;
                //res = (res  > 1) ? 1 : res; 
                //boolean resBiggerThan1 = (res  > 1);
                boolean resLessThan1 = (res < 1);
                if (num >= bestScore)
                    result++;
                else if ((!resLessThan1) && (num + 1 >= bestScore))
                    result++;
                else if ((numSuprises > 0) && (num > 0) && ((resLessThan1 && (num + 1 >= bestScore)) || (!resLessThan1 && (num + res >= bestScore)))) {
                    numSuprises--;
                    result++;
                }
                else if ((numSuprises > 0) && (!resLessThan1 && (num + res >= bestScore))) {
                    numSuprises--;
                    result++;
                }
            }
            in.nextLine();
            fOut.println(result);
        }
        fOut.flush();
    }
    
    
}
