Big AND Circuit
Time limit3sMemory limit1024 MB
Given N source signals with delays n_i, a gate delay D, and an LED delay L, build a tree of N-1 AND gates that minimizes the worst-case delay to flip the LED.
- Level
Medium7 of 10
- Topics
- Tree, Greedy, Sorting, Divide and conquer
- Solved
- No attempts yet
Problem
You need a circuit that computes the logical AND of several source signals. The only part you may use is a two-input AND gate, which outputs True if and only if both of its inputs are True.
The circuit must satisfy these conditions.
- Use exactly AND gates, where is the number of source signals.
- Each source signal connects to exactly one AND gate input.
- Each AND gate input connects to exactly one signal, either a source signal or the output of another AND gate.
- The circuit has no cycles. The output of an AND gate never reaches one of its own inputs.
The circuit output drives an LED. The LED is lit if and only if all source signals are True.
The timing rules are as follows.
- When source signal changes, the change takes exactly nanoseconds to reach the circuit.
- After an AND gate input changes, the gate output changes exactly nanoseconds later, if the new inputs actually change the output.
- The LED changes exactly nanoseconds after its input signal changes.
- Signals travel between gates in 0 nanoseconds.
Build the circuit that minimizes the maximum time from a source change to an LED change, counting only changes that actually flip the LED.

Figure 1: An optimal circuit for each sample input. The numbers on the left are the source signal indices.
Input
The first line contains three integers (), , and (). is the number of source signals, and and are as described in the problem.
The second line contains integers (), where is the number of nanoseconds it takes for a change in source to reach the circuit.
Output
Print the smallest time such that some circuit guarantees the LED takes at most nanoseconds to change whenever a source change would flip it.