Under construction

Still being built. Things may change

Proving Propositions

Time limit2sMemory limit512 MB

Summary
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 NN propositions are all equivalent. The propositions are numbered from 0 to N−1N-1.

For any two different propositions xx and yy, Yeongseon can prove "if xx then yy", and every such proof has its own difficulty. The difficulty of "if xx then yy" and the difficulty of "if yy then xx" 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 N=3N = 3, 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 NN (1≤N≤501 \le N \le 50).

Each of the next NN lines contains NN integers, the difficulties of the proofs. The jj-th integer on the ii-th line is the difficulty of proving ii => jj. Numbering starts at 0.

Every difficulty is an integer between 0 and 150,000, and the ii-th integer on the ii-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 N=1N = 1, no proof is needed, so print 0.

Examples3

  1. Example 1

    Input
    4
    0 11 13 13
    10 0 12 13
    10 10 0 11
    12 10 10 0
    
    Expected output
    1
    
  2. Example 2

    Input
    5
    0 1000 1000 1000 1000
    1 0 1000 1000 1000
    1 1 0 1000 1000
    1 1 1 0 1000
    1 1 1 1 0
    
    Expected output
    999
    
  3. Example 3

    Input
    4
    0 136946 89305 28946 
    40152 0 150000 85407 
    122920 110420 0 32811 
    2092 79717 53028 0
    
    Expected output
    50771