Under construction

Still being built. Things may change

Agri-Net

Interview

Time limit1sMemory limit1024 MB

Summary
Given an N by N distance matrix of farms, find the total length of the cheapest set of connections that links all farms together.
Level

Easy3 of 10

Topics
Minimum spanning tree, Greedy, Graph
Solved
No attempts yet

Problem

Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course. Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms. Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must be connected to some other farm such that a path exists from any farm to any other farm.

Input

The first line of input is the number of farms, N (3 ≤ N ≤ 100). Each of the N subsequent lines contains N non-negative integers (each less than 32000) that represent the distance between farm i and farms 1, 2, 3, ..., N. Of course, the diagonal will be 0, since the distance from farm i to itself is not interesting for this problem.

Output

Output a single integer, the minimum total length of fiber required to connect the entire set of farms.

Examples1

  1. Example 1

    Input
    4
    0 4 9 21
    4 0 8 17
    9 8 0 16
    21 17 16 0
    
    Expected output
    28