package template;

import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;

public class TestCase {

    private boolean isSolved;
    private Object solution;
    private Map<String, Integer> intProperties;
    private Map<String, ArrayList<Integer>> intArrayProperties;
    private Map<String, ArrayList<ArrayList<Integer>>> intArray2DProperties;
    private Map<String, Double> doubleProperties;
    private Map<String, ArrayList<Double>> doubleArrayProperties;
    private Map<String, ArrayList<ArrayList<Double>>> doubleArray2DProperties;
    private Map<String, String> stringProperties;
    private Map<String, ArrayList<String>> stringArrayProperties;
    private Map<String, ArrayList<ArrayList<String>>> stringArray2DProperties;
    private Map<String, Boolean> booleanProperties;
    private Map<String, ArrayList<Boolean>> booleanArrayProperties;
    private Map<String, ArrayList<ArrayList<Boolean>>> booleanArray2DProperties;
    private Map<String, Long> longProperties;
    private Map<String, ArrayList<Long>> longArrayProperties;
    private Map<String, ArrayList<ArrayList<Long>>> longArray2DProperties;
    private int ref;
    private double time;

    public TestCase() {
        initialise();
    }

    private void initialise() {
        isSolved = false;
        intProperties = new HashMap<>();
        intArrayProperties = new HashMap<>();
        intArray2DProperties = new HashMap<>();
        doubleProperties = new HashMap<>();
        doubleArrayProperties = new HashMap<>();
        doubleArray2DProperties = new HashMap<>();
        stringProperties = new HashMap<>();
        stringArrayProperties = new HashMap<>();
        stringArray2DProperties = new HashMap<>();
        booleanProperties = new HashMap<>();
        booleanArrayProperties = new HashMap<>();
        booleanArray2DProperties = new HashMap<>();
        longProperties = new HashMap<>();
        longArrayProperties = new HashMap<>();
        longArray2DProperties = new HashMap<>();
        ref = 0;
    }

    public void setSolution(Object o) {
        solution = o;
        isSolved = true;
    }
    
    public Object getSolution() {
        if (!isSolved) {
            Utils.die("getSolution on unsolved testcase");
        }

        return solution;
    }

    public void setRef(int i) {
        ref = i;
    }
    public int getRef() {
        return ref;
    }

    public void setTime(double d) {
        time = d;
    }
    public double getTime() {
        return time;
    }
    
    public void setInteger(String s, Integer i) {
        intProperties.put(s, i);
    }
    
    public Integer getInteger(String s) {
        return intProperties.get(s);
    }

    public void setIntegerList(String s, ArrayList<Integer> l) {
        intArrayProperties.put(s, l);
    }

    public void setIntegerMatrix(String s, ArrayList<ArrayList<Integer>> l) {
        intArray2DProperties.put(s, l);
    }

    public ArrayList<Integer> getIntegerList(String s) {
        return intArrayProperties.get(s);
    }

    public Integer getIntegerListItem(String s, int i) {
        return intArrayProperties.get(s).get(i);
    }

    public ArrayList<ArrayList<Integer>> getIntegerMatrix(String s) {
        return intArray2DProperties.get(s);
    }
    
    public ArrayList<Integer> getIntegerMatrixRow(String s, int row) {
        return intArray2DProperties.get(s).get(row);
    }
    
    public Integer getIntegerMatrixItem(String s, int row, int column) {
        return intArray2DProperties.get(s).get(row).get(column);
    }

    public ArrayList<Integer> getIntegerMatrixColumn(String s, int column) {
        ArrayList<Integer> out = new ArrayList();
        
        for(ArrayList<Integer> row : intArray2DProperties.get(s)) {
            out.add(row.get(column));
        }
        return out;
    }


    public void setDouble(String s, Double i) {
        doubleProperties.put(s, i);
    }
    
    public Double getDouble(String s) {
        return doubleProperties.get(s);
    }

    public void setDoubleList(String s, ArrayList<Double> l) {
        doubleArrayProperties.put(s, l);
    }

    public void setDoubleMatrix(String s, ArrayList<ArrayList<Double>> l) {
        doubleArray2DProperties.put(s, l);
    }

    public ArrayList<Double> getDoubleList(String s) {
        return doubleArrayProperties.get(s);
    }

    public Double getDoubleListItem(String s, int i) {
        return doubleArrayProperties.get(s).get(i);
    }

    public ArrayList<ArrayList<Double>> getDoubleMatrix(String s) {
        return doubleArray2DProperties.get(s);
    }
    
    public ArrayList<Double> getDoubleMatrixRow(String s, int row) {
        return doubleArray2DProperties.get(s).get(row);
    }
    
    public Double getDoubleMatrixItem(String s, int row, int column) {
        return doubleArray2DProperties.get(s).get(row).get(column);
    }

    public ArrayList<Double> getDoubleMatrixColumn(String s, int column) {
        ArrayList<Double> out = new ArrayList();
        
        for(ArrayList<Double> row : doubleArray2DProperties.get(s)) {
            out.add(row.get(column));
        }
        return out;
    }

    
    public void setString(String s, String t) {
        stringProperties.put(s, t);
    }
    
    public String getString(String s) {
        return stringProperties.get(s);
    }

    public void setStringList(String s, ArrayList<String> l) {
        stringArrayProperties.put(s, l);
    }

    public void setStringMatrix(String s, ArrayList<ArrayList<String>> l) {
        stringArray2DProperties.put(s, l);
    }

    public ArrayList<String> getStringList(String s) {
        return stringArrayProperties.get(s);
    }

    public String getStringListItem(String s, int i) {
        return stringArrayProperties.get(s).get(i);
    }

    public ArrayList<ArrayList<String>> getStringMatrix(String s) {
        return stringArray2DProperties.get(s);
    }
    
    public ArrayList<String> getStringMatrixRow(String s, int row) {
        return stringArray2DProperties.get(s).get(row);
    }
    
    public String getStringMatrixItem(String s, int row, int column) {
        return stringArray2DProperties.get(s).get(row).get(column);
    }

    public ArrayList<String> getStringMatrixColumn(String s, int column) {
        ArrayList<String> out = new ArrayList();
        
        for(ArrayList<String> row : stringArray2DProperties.get(s)) {
            out.add(row.get(column));
        }
        return out;
    }

    
    public void setBoolean(String s, Boolean b) {
        booleanProperties.put(s, b);
    }
    
    public Boolean getBoolean(String s) {
        return booleanProperties.get(s);
    }

    public void setBooleanList(String s, ArrayList<Boolean> l) {
        booleanArrayProperties.put(s, l);
    }

    public void setBooleanMatrix(String s, ArrayList<ArrayList<Boolean>> l) {
        booleanArray2DProperties.put(s, l);
    }

    public ArrayList<Boolean> getBooleanList(String s) {
        return booleanArrayProperties.get(s);
    }

    public Boolean getBooleanListItem(String s, int i) {
        return booleanArrayProperties.get(s).get(i);
    }

    public ArrayList<ArrayList<Boolean>> getBooleanMatrix(String s) {
        return booleanArray2DProperties.get(s);
    }
    
    public ArrayList<Boolean> getBooleanMatrixRow(String s, int row) {
        return booleanArray2DProperties.get(s).get(row);
    }
    
    public Boolean getBooleanMatrixItem(String s, int row, int column) {
        return booleanArray2DProperties.get(s).get(row).get(column);
    }

    public ArrayList<Boolean> getBooleanMatrixColumn(String s, int column) {
        ArrayList<Boolean> out = new ArrayList();
        
        for(ArrayList<Boolean> row : booleanArray2DProperties.get(s)) {
            out.add(row.get(column));
        }
        return out;
    }

    public void setLong(String s, Long b) {
        longProperties.put(s, b);
    }
    
    public Long getLong(String s) {
        return longProperties.get(s);
    }

    public void setLongList(String s, ArrayList<Long> l) {
        longArrayProperties.put(s, l);
    }

    public void setLongMatrix(String s, ArrayList<ArrayList<Long>> l) {
        longArray2DProperties.put(s, l);
    }

    public ArrayList<Long> getLongList(String s) {
        return longArrayProperties.get(s);
    }

    public Long getLongListItem(String s, int i) {
        return longArrayProperties.get(s).get(i);
    }

    public ArrayList<ArrayList<Long>> getLongMatrix(String s) {
        return longArray2DProperties.get(s);
    }
    
    public ArrayList<Long> getLongMatrixRow(String s, int row) {
        return longArray2DProperties.get(s).get(row);
    }
    
    public Long getLongMatrixItem(String s, int row, int column) {
        return longArray2DProperties.get(s).get(row).get(column);
    }

    public ArrayList<Long> getLongMatrixColumn(String s, int column) {
        ArrayList<Long> out = new ArrayList();
        
        for(ArrayList<Long> row : longArray2DProperties.get(s)) {
            out.add(row.get(column));
        }
        return out;
    }

    

    


}

package sholay;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * GoogleDancers
 */
public class GoogleDancers {

	public static String INPUT_FILE_NAME = "B-small-attempt0.in";
	public static String OUTPUT_FILE_NAME = "B-small-attempt0.out";

	class Player implements Comparable<Player> {

		int totalScore;
		int x;
		int y;
		int z;
		int thresholdScore;
		boolean isSurprise;
		boolean isMax;

		/**
		 * @param totalScore
		 * @param thresholdScore
		 * @param x
		 * @param y
		 * @param z
		 */
		Player(int totalScore, int thresholdScore, int x, int y, int z) {
			this.totalScore = totalScore;
			this.thresholdScore = thresholdScore;
			this.x = x;
			this.y = y;
			this.z = z;
			isSurprise = (Math.abs(x - y) >= 2d || Math.abs(y - z) >= 2d || Math
					.abs(x - z) >= 2d);
			isMax = (x >= thresholdScore) || (y >= thresholdScore)
					|| (z >= thresholdScore);
		}

		@Override
		public int compareTo(Player arg0) {
			return (isMax && !isSurprise) ? 1
					: ((isMax && isSurprise) ? 0 : -1);
		}
	}

	//
	Map<Integer, java.util.List<Player>> map = new HashMap<Integer, java.util.List<Player>>();

	/**
	 * Read the input file, decode the input and write the output file
	 * 
	 * @throws IOException
	 */
	public void solve() throws IOException {
		BufferedReader in = null;
		PrintWriter out = null;
		try {
			in = new BufferedReader(new FileReader(getFile(INPUT_FILE_NAME)));
			out = new PrintWriter(new FileWriter(OUTPUT_FILE_NAME));
			String line = null;
			int lineNum = 0;
			int numTestCase = 0;
			int count = 0;
			while ((line = in.readLine()) != null) {
				if (lineNum >= 0) {
					if (lineNum == 0) {
						numTestCase = Integer.parseInt(line);
					} else {
						count++;
						if (count <= numTestCase) {
							int solution = getMaxBestResult(line);
							out.write("Case #" + count + ": " + solution);
							if (count != numTestCase) {
								out.write("\n");
							}
						} else {
							break;
						}
					}
				}
				lineNum++;
			}

		} catch (Exception ex) {
			System.out.println("Program failed :" + ex.getMessage());
		} finally {
			in.close();
			out.close();
		}
	}

	/**
	 * @param line
	 * @return
	 */
	private int getMaxBestResult(String line) {
		map.clear();
		String[] lineArray = line.split(" ");
		int totalPlayers = Integer.parseInt(lineArray[0]);
		if (totalPlayers > 0) {
			int surprise = Integer.parseInt(lineArray[1]);
			int threshold = Integer.parseInt(lineArray[2]);
			for (int i = 0; i < totalPlayers; i++) {
				List<Player> players = getPossiblePlayers(surprise, threshold,
						Integer.parseInt(lineArray[3 + i]));
				map.put(i, players);
			}
			return findMaxBest(surprise, map);
		}
		return 0;
	}

	/**
	 * @param surprise
	 * @param map
	 * @return
	 */
	private int findMaxBest(int surprise, Map<Integer, List<Player>> map) {
		int maxBest = 0;
		Set<Integer> players = map.keySet();
		for (Integer player : players) {
			List<Player> possibility = map.get(player);
			Player p = getPlayer(surprise, possibility);
			if (p != null) {
				if (p.isMax) {
					maxBest++;
				}
				if (p.isSurprise) {
					surprise--;
				}
			}
		}
		return maxBest;
	}

	/**
	 * @param surprise
	 * @param possibility
	 * @return
	 */
	private Player getPlayer(int surprise, List<Player> possibility) {
		Player pp = null;
		Collections.sort(possibility,Collections.reverseOrder());
		for (Player p : possibility) {
			if (p.isMax && !p.isSurprise) {
				pp = p;
			break;
			}
		}
		if (pp == null) {
			for (Player p : possibility) {
				if (p.isMax && p.isSurprise && surprise > 0) {
					pp = p;
				break;
				}
			}
		}
		if (pp == null) {
			for (Player p : possibility) {
				if (!p.isSurprise && surprise <= 0) {
					pp = p;
				break;
				}
			}
		}
		if (pp == null) {
			for (Player p : possibility) {
				if (!p.isSurprise) {
					pp = p;
				break;
				}
			}
		}
		return pp;
	}

	/**
	 * @param surprise
	 * @param threshold
	 * @param i
	 * @return
	 */
	private List<Player> getPossiblePlayers(int surprise, int threshold,
			int totalScore) {
		List<Player> players = new ArrayList<Player>();
		//
		if ((totalScore) % 3 == 0) {
			int div = (totalScore) / 3;
			if(div>=0) {
				players.add(new Player(totalScore, threshold, div, div, div));
			}
		}
		//
		if ((totalScore - 1) % 3 == 0) {
			int div = (totalScore - 1) / 3;
			if(div>=0){
				players.add(new Player(totalScore, threshold, div, div, div + 1));
			}
		}
		//
		if ((totalScore - 2) % 3 == 0) {
			int div = (totalScore - 2) / 3;
			if(div>=0) {
				players.add(new Player(totalScore, threshold, div, div + 1, div + 1));
				players.add(new Player(totalScore, threshold, div, div, div + 2));
			}
		}
		//
		if ((totalScore - 3) % 3 == 0) {
			int div = (totalScore - 3) / 3;
			if(div>=0) {
				players.add(new Player(totalScore, threshold, div, div + 1, div + 2));
			}
		}
		//
		if ((totalScore - 4) % 3 == 0) {
			int div = (totalScore - 4) / 3;
			if(div>=0) {
				players.add(new Player(totalScore, threshold, div, div + 2, div + 2));
			}
		}
		return players;
	}

	/**
	 * @param fileName
	 * @return
	 */
	public static File getFile(String fileName) {
		return new File(fileName);
	}

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		GoogleDancers googleDancers = new GoogleDancers();
		googleDancers.solve();
	}

}
