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

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Lance Chen
 */
public class CodeHelper {

    private static String FILE_ROOT = "D:/workspace/googlecodejam/meta";

    public static List<List<String>> loadInputsExcludes(String fileName) {
        List<List<String>> result = new ArrayList<List<String>>();
        List<String> lines = FileUtil.readLines(FILE_ROOT + fileName);
        int index = 0;
        for (String line : lines) {
            if (index == 0) {
                index++;
                continue;
            }
            if (index % 2 == 1) {
                index++;
                continue;
            }
            List<String> dataList = new ArrayList<String>();
            String[] dataArray = line.split("\\s+");
            for (String data : dataArray) {
                if (data.trim().length() > 0) {
                    dataList.add(data);
                }
            }
            result.add(dataList);
            index++;
        }
        return result;
    }

    public static List<List<String>> loadInputs(String fileName) {
        List<List<String>> result = new ArrayList<List<String>>();
        List<String> lines = FileUtil.readLines(FILE_ROOT + fileName);
        for (String line : lines) {
            List<String> dataList = new ArrayList<String>();
            String[] dataArray = line.split("\\s+");
            for (String data : dataArray) {
                if (data.trim().length() > 0) {
                    dataList.add(data);
                }
            }
            result.add(dataList);
        }
        return result;
    }

    public static List<String> loadInputLines(String fileName) {
        return FileUtil.readLines(FILE_ROOT + fileName);
    }

    public static void writeOutputs(String fileName, List<String> lines) {
        FileUtil.writeLines(FILE_ROOT + fileName, lines);
    }
}

package lt.kasrud.gcj.ex2;

import lt.kasrud.gcj.common.io.Reader;
import lt.kasrud.gcj.common.io.Writer;

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

public class Main {
  public static void main(String[] args) throws IOException {
    List<String> iData = Reader.readFile("B-small-attempt0.in");
    Writer.writeFile("out2.txt", process(iData.subList(1, iData.size())));
  }

  private static List<String> process(List<String> data) {
    List<String> result = new ArrayList<String>(data.size());
    for(String record : data){
      List<String> tokens = Arrays.asList(record.trim().split(" "));
      result.add(calculate(Integer.valueOf(tokens.get(1)), Integer.valueOf(tokens.get(2)), tokens.subList(3, tokens.size())));
    }

    return result;
  }

  private static String calculate(Integer surpCnt, Integer maxValue, List<String> totPoints) {
    int cnt = 0;
    int minDiff = maxValue > 0 ? 1 : maxValue;
    int maxDiff = maxValue > 1 ? 2 : maxValue;
    int maxTotal = maxValue * 3 - 2 * minDiff;
    int surpTotal = maxValue * 3 - 2 * maxDiff;
    for (String t : totPoints) {
      int total = Integer.valueOf(t);
      if (total >= maxTotal){
        cnt++;
      }else if(total >= surpTotal && surpCnt > 0){
        cnt++;
        surpCnt--;
      }
    }

    return String.valueOf(cnt);
  }
}
