public class SemaCorrected { 
    private int waiting, permits;
    public SemaCorrected(int n) { permits = n; }
    public synchronized void up() {
        permits++; if (waiting > 0) notify();
    }
    public synchronized void down() 
                throws InterruptedException {
        waiting++;
        while (permits <= 0) wait();
        waiting--;
        permits--;
    }
} // end of class SemaCorrected
