A circuit is as fast as its slowest path. The critical path is the path from an input to an output with the largest total delay, and it limits how fast the clock can run.
What to build
Compute when every gate's output is ready, and the delay of the whole circuit.
Requirements
- All inputs are ready at time 0.
- A gate line has the gate's delay in picoseconds and the numbers of its sources:
20 0 1is a gate with delay 20 that reads signals 0 and 1. - Signals are numbered like in a netlist: the inputs first, then the gates in order. A gate only uses smaller numbers than its own.
- Print the ready time of every gate on the first line, and the circuit's delay (the largest of them) on the second.
Input
The first line has the number of inputs k and the number of gates m. Then m gate lines.
Output
Two lines, as described.
Constraints
1 ≤ k ≤ 6, 1 ≤ m ≤ 20, delays from 1 to 100.
Example
Input:
3 3
10 1
20 0 3
25 2 4
Output:
10 30 55
55
Why it works
A gate cannot give its output before all of its inputs are ready, so the time of a gate is its own delay after the last input arrives. The longest of these times over all the gates is the delay of the whole circuit. Making a different, shorter path faster does not help: only the critical path matters.
Examples
3 3 10 1 20 0 3 25 2 4
10 30 55 55
2 2 20 0 1 20 2 2
20 40 40
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Keep a list `time`, with 0 for every input, and a list `value` if you also want the outputs. Here only the times matter.
Hint 2
A gate's output time is its delay plus the latest time among its sources.
Hint 3
The critical path length is the largest time of all the gates.
Solution
Show one correct solution (try it yourself first)
k, m = map(int, input().split())
time = [0] * k
for _ in range(m):
parts = list(map(int, input().split()))
delay, sources = parts[0], parts[1:]
time.append(delay + max(time[s] for s in sources))
print(*time[k:])
print(max(time))
▶ Open this solution in the compiler, then press Visualize to watch it run step by step