import java.util.List;


public class Case implements Runnable {

	private int id;
	private String output;

	private int n;
	private int s;
	private int p;
	private List<Integer> g;

	public Case(int id) {
		this.id = id;
	}

	public Case(int id, int n, int s, int p, List<Integer> g) {
		super();
		this.id = id;
		this.n = n;
		this.s = s;
		this.p = p;
		this.g = g;
	}

	public String solve() {
		int res = 0;
		for (int googler : g) {
			int b = googler / 3;
			boolean div = googler % 3 == 0;
			if (b >= p) {
				res++;
				continue;
			}
			if (p == b+1) {
				if (div && s > 0 && b > 0) {
					res++;
					s--;
					continue;
				}
				if (!div) {
					res++;
					continue;
				}
			}
			if (p == b+2) {
				if (3*b+2 == googler && s > 0) {
					res++;
					s--;
				}
			}
		}
		return String.valueOf(res);
	}

	@Override
	public void run() {
		output = solve();
	}

	@Override
	public String toString() {
		return "Case #" + id + ": " + output;
	}

}
package codejam2012.qualification.b;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class Round {

    private static Logger log = LoggerFactory.getLogger(Round.class);

    private final List<Integer> totals;
    private final Memory memory;

    public Round(List<Integer> totals, Memory memory) {
        this.totals = totals;
        this.memory = memory;
    }

    public int run(int s, int p) {
        int count = 0;

        for (Integer total : totals) {
            List<Triplet> triplets = memory.data.get(total);
            if (triplets.isEmpty()) {
                throw new IllegalStateException();
            }

            log.debug("for total: {} triplets are: {}", total, triplets);
            boolean has = false;
            boolean hasNotSurprising = false;

            for (Triplet triplet : triplets) {
                if (triplet.hasAtLeast(p)) {
                    has = true;
                    if (!triplet.surprising) {
                        hasNotSurprising = true;
                    }
                }
            }

            log.debug("--> has: {} hasNotSurprising: {}", has, hasNotSurprising);

            if (has) {
                if (hasNotSurprising) {
                    ++count;
                } else {
                    if (s > 0) {
                        --s;
                        ++count;
                    }
                }
            }
        }

        return count;
    }

}
