/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package IO;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
    
/**
 * 
 * @author dannocz
 */
public class InputReader {
    
    public static Input readFile(String filename){
        try {
              
              /*  Sets up a file reader to read the file passed on the command
          77        line one character at a time */
              FileReader input = new FileReader(filename);
              
              /* Filter FileReader through a Buffered read to read a line at a
                 time */
              BufferedReader bufRead = new BufferedReader(input);
              
              String line;    // String that holds current file line
              int count = 0;  // Line number of count 
              ArrayList<String> cases= new ArrayList<String>();
              
              // Read first line
              line = bufRead.readLine();
              int noTestCases=Integer.parseInt(line);
              count++;
              
              // Read through file one line at time. Print line # and line
              while (count<=noTestCases){
                  //System.out.println("Reading. "+count+": "+line);
                  line = bufRead.readLine();
                  cases.add(line);
                  count++;
              }
              
              bufRead.close();
              
              return new Input(noTestCases,cases);
              
          }catch (ArrayIndexOutOfBoundsException e){
              /* If no file was passed on the command line, this expception is
              generated. A message indicating how to the class should be
              called is displayed */
              e.printStackTrace();        
  
          }catch (IOException e){
              // If another exception is generated, print a stack trace
              e.printStackTrace();
          }
        
        return null;
          
      }// end main
    }

package com.google.code.p_b;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;

public class Main {

	private static final String fromFile = "D:\\hobbies & work\\programing\\CodeJamTextFiles\\2012\\B-large.in";
	private static final String toFile = "D:\\hobbies & work\\programing\\CodeJamTextFiles\\2012\\B-large.out";

	
	/**
	 * @param args
	 */
	public static void main(String[] args) 
	{
		System.out.println("Problem B start");
		
		try {

			FileInputStream fstream = new FileInputStream(fromFile);

			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			
			FileWriter fstreamout = new FileWriter(toFile);
			BufferedWriter out = new BufferedWriter(fstreamout);
			
			String strLine;
			boolean firstLine = true;
			int i = 1;
			while ((strLine = br.readLine()) != null) 
			{
				if (!firstLine)
				{
					System.out.println(strLine);
					System.out.println("Case #" + i + ": " + ansForLine(strLine) + "\n");

					out.write("Case #" + i + ": " + ansForLine(strLine) + "\n");
					i++;
				}
				else
				{
					firstLine = false;
				}
			}
			out.close();
			in.close();
		} catch (Exception e) {
			System.err.println("Error: " + e.getMessage());
		}
		
		System.out.println("Problem B end");
	}


	private static int ansForLine(String strLine) 
	{
		String [] splitedLine = strLine.split(" ");
		int nRes = Integer.parseInt(splitedLine[0]);
		int nSurprises = Integer.parseInt(splitedLine[1]);
		int nMax = Integer.parseInt(splitedLine[2]);
		System.out.println("res: " + nRes + ", surprises: " + nSurprises +", max: " + nMax);

		int count = 0;
		
		for (int i = 3; i < splitedLine.length; i++)
		{
			int j = Integer.parseInt(splitedLine[i]);
			int avg = j / 3;
			int m = j % 3;
			
			if (j == 0)
			{
				if (nMax == 0)
					count ++;
				continue;
			}
			
			if (avg >= nMax)
			{
				count ++;
			}
			else if (avg + 1 >= nMax)
			{
				if (m == 0 && nSurprises > 0)
				{
					count ++;
					nSurprises --;
				}
				else if (m == 1 || m == 2)
				{
					count ++;
				}
			}
			else if (avg + 2 >= nMax)
			{
				if (m == 2 && nSurprises > 0)
				{
					count ++;
					nSurprises --;
				}
			}
		}
		
		return count;
	}

}
