Proving Propositions
Time limit2sMemory limit512 MB
Choose directed edges so all N propositions become mutually reachable, minimizing the difference between the hardest and easiest chosen proof difficulties.
- Level
Medium7 of 10
- Topics
- Graph, Two pointers, Union-find, Minimum spanning tree
- Solved
- No attempts yet
Problem
Yeongseon is studying for a calculus exam and wants to prove that propositions are all equivalent. The propositions are numbered from 0 to .
For any two different propositions and , Yeongseon can prove "if then ", and every such proof has its own difficulty. The difficulty of "if then " and the difficulty of "if then " may differ.
Today's task is to choose a set of proofs so that any two propositions imply each other, either directly or through other propositions. For example, when , one way is to prove 0 => 1, 1 => 0, 0 => 2, 2 => 0. Another way is to prove 0 => 1, 1 => 2, 2 => 0.
Among the chosen proofs, Yeongseon wants the gap between the hardest difficulty and the easiest difficulty to be as small as possible. Write a program that finds that minimum gap.
Input
The first line contains the number of propositions ().
Each of the next lines contains integers, the difficulties of the proofs. The -th integer on the -th line is the difficulty of proving => . Numbering starts at 0.
Every difficulty is an integer between 0 and 150,000, and the -th integer on the -th line is always 0.
Output
Print on the first line the smallest possible difference between the hardest and the easiest difficulty among the chosen proofs, over all choices that make every proposition imply every other one.
When , no proof is needed, so print 0.