package com.silverduner.codejam;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.HashMap;

public class Dancing {
	public static void main(String[] args) throws Exception {
		File input = new File("B-small-attempt0.in");
		BufferedReader in = new BufferedReader(new FileReader(input));

		File output = new File("1.out");
		BufferedWriter out = new BufferedWriter(new FileWriter(output));

		// parse input
		int N = Integer.parseInt(in.readLine());
		for(int i=0;i<N;i++) {
			String str = in.readLine();
			out.write("Case #"+(i+1)+": ");
			System.out.println("-------");
			String[] words = str.split(" ");
			int n = Integer.parseInt(words[0]);
			int suprise = Integer.parseInt(words[1]);
			int threshold = Integer.parseInt(words[2]);
			int[] scores = new int[n];
			for(int j=0;j<n;j++) {
				scores[j] = Integer.parseInt(words[3+j]);
			}
			int count = 0;
			for(int score : scores) {
				int a,b,c;
				boolean find = false;
				boolean sfind = false;
				for(a = 10; a>=0 && !find; a--) {
					if(3*a-4>score)
						continue;
					for(b = a; b>=0 && b >=a-2 && !find; b--) {
						for(c = b; c>=0 && c >=b-2 && c >=a-2 && !find; c--) {
							System.out.println(a+","+b+","+c);
							if(a+b+c==score) {
								if(a >= threshold) {
									if(a-c <= 1){
										count++;
										find = true;
										System.out.println("find!");
									}
									else if(a-c == 2){
										sfind = true;
										System.out.println("suprise!");
									}
								}
							}
							
						}
					}
				}
				if(!find && sfind && suprise > 0)
				{
					count++;
					suprise --;
				}
			}
			int maxCount = count;
			out.write(maxCount+"\n");
		}
		out.flush();
		out.close();
		in.close();
	}
}

package com.first;

public class BDancingWiththeGooglers {

	ReadWriteFileForCodeJam flatFile;

	public BDancingWiththeGooglers() {
		flatFile = new ReadWriteFileForCodeJam();
		initializeFlatFileParameters();
		performSolution();
		finalizeSolutionFileToSend();
	}

	private void performSolution(){
		String[] items;
		int noDancers, noOfSurprises, bestScore;
		int maxGooglers;
		int maxAchieved;
		boolean surprise;
		for(int i=1; i<=Integer.parseInt(flatFile.GetSingleLineFromFile(1)); i++){
			maxGooglers = 0;
			surprise = true;
			items = flatFile.GetSingleLineFromFile(i + 1).split(" ");
			noDancers = Integer.parseInt(items[0]);
			noOfSurprises = Integer.parseInt(items[1]);
			bestScore = Integer.parseInt(items[2]);
			flatFile.setFileContentToWrite("Case #" + i + ":", true, true);
			for(int j=0; j<noDancers; j++){
				if ((noOfSurprises <= 0) && surprise)
					surprise = false;
				maxAchieved = isBestScorePossible(Integer.parseInt(items[j+3]), bestScore);
				switch (maxAchieved){
				case 0: break;
				case 1: maxGooglers++;	break;
				case 2:
					if (surprise){
						maxGooglers++;
						noOfSurprises--;
					}
					break;
				}
			}
			flatFile.setFileContentToWrite(String.valueOf(maxGooglers), true, false);
		}
	}

	private int isBestScorePossible(int total, int least){
		/* return 0 if not possible
		 * 1 if possible without surprise
		 * 2 if possible with surprise
		 */
		
		if (total == 0){
			if (least == 0)
				return 1;
			else
				return 0;
		}
		
		if (total == 1){
			if ((least <= 1))
				return 1;
			else
				return 0;
		}
		
		if (least * 3 - 2 <= total)
			return 1;
		if (least * 3 - 4 <= total)
			return 2;
		return 0;
	}

	private void initializeFlatFileParameters() {
		flatFile.setFileNameForRead("C:\\Users\\i067221\\Downloads\\B-small-attempt0.in");
		flatFile.setFileNameForWrite("C:\\Users\\i067221\\Downloads\\Upload\\B-small-attempt0.txt");
		flatFile.setFileSeperatorForWrite(" ");
		System.out.println("File read, (" + flatFile.ReadFile(false) + ") returned.");
	}

	private void finalizeSolutionFileToSend() {
		System.out.println("Successful write to file performed: " + flatFile.WriteFile(false));
	}

}
