package codejam;

import fixjava.Pair;

public class Utils {

	public static long minLong(long firstVal, long... otherVals) {
		long minVal = firstVal;
		for (int i = 0; i < otherVals.length; i++)
			minVal = Math.min(firstVal, otherVals[i]);
		return minVal;
	}

	public static int minInt(int firstVal, int... otherVals) {
		int minVal = firstVal;
		for (int i = 0; i < otherVals.length; i++)
			minVal = Math.min(firstVal, otherVals[i]);
		return minVal;
	}

	public static float minFloat(float firstVal, float... otherVals) {
		float minVal = firstVal;
		for (int i = 0; i < otherVals.length; i++)
			minVal = Math.min(firstVal, otherVals[i]);
		return minVal;
	}

	/**
	 * ArgMin returned as a Pair<Integer, Integer>(index, value). Array must
	 * have at least one value.
	 */
	public static Pair<Integer, Integer> argMin(int[] arr) {
		int min = arr[0];
		int argMin = 0;
		for (int i = 1; i < arr.length; i++) {
			if (arr[i] < min) {
				min = arr[i];
				argMin = i;
			}
		}
		return new Pair<Integer, Integer>(argMin, min);
	}

	/**
	 * ArgMax returned as a Pair<Integer, Integer>(index, value). Array must
	 * have at least one value.
	 */
	public static Pair<Integer, Integer> argMax(int[] arr) {
		int max = arr[0];
		int argMax = 0;
		for (int i = 1; i < arr.length; i++) {
			if (arr[i] > max) {
				max = arr[i];
				argMax = i;
			}
		}
		return new Pair<Integer, Integer>(argMax, max);
	}

}

package com.menzus.gcj._2012.qualification.b;

import com.menzus.gcj.common.OutputProducer;

public class BOutputProducer implements OutputProducer<BInput, BOutputEntry> {

    @Override
    public BOutputEntry produceOutput(BInput input) {
	int avaragesAboveLimit = 0;
	int avaragesBelowLimitIfTheyWereSuprising = 0;
	for (int i = 0; i < input.getGooglerScores().length; i++) {
	    if (maximumNotSurprisingScoreOfTotalScore(input.getGooglerScores()[i]) >= input.getLimit()) {
		avaragesAboveLimit++;
	    } else if (maximumSurprisingScoreOfTotalScore(input.getGooglerScores()[i]) >= input.getLimit()) {
		avaragesBelowLimitIfTheyWereSuprising++;
	    }
	}
	BOutputEntry outputEntry = new BOutputEntry();
	outputEntry.setMaximumGooglersNumber(avaragesAboveLimit
		+ Math.min(avaragesBelowLimitIfTheyWereSuprising, input.getSurprisingTripletsNumber()));
	return outputEntry;
    }

    private int maximumNotSurprisingScoreOfTotalScore(int totalScore) {
	if (totalScore % 3 == 0) {
	    return totalScore / 3;
	} else {
	    return totalScore / 3 + 1;
	}
    }

    private int maximumSurprisingScoreOfTotalScore(int totalScore) {
	int maximumNotSurprisingScoreOfTotalScore = maximumNotSurprisingScoreOfTotalScore(totalScore);
	if (maximumNotSurprisingScoreOfTotalScore == 0) {
	    return maximumNotSurprisingScoreOfTotalScore;
	} else {
	    return maximumNotSurprisingScoreOfTotalScore + 1;
	}
    }
}
