This page is still under construction.

Parts of this page are still being built. What you see may change.

Absurdistan Roads

Time limit5sMemory limit128 MB

Summary
Given all-pairs shortest distances, find the minimum total length of a connected N-edge road network that reproduces the table.
Level

Hard8 of 10

Topics
Minimum spanning tree, Graph, Shortest path
Solved
No attempts yet

Problem

The people of Absurdistan worked out how to build roads only last year. Every one of the NN cities then built exactly one road from itself to another city, and every road can be travelled in both directions. Each road has an integer length between 1 and 1,000,000. Two cities can end up joined by two roads, but no road joins a city to itself.

The work took all NN cities precisely one year, and once it was finished, every city could reach every other city over the new roads.

Your tourist guide has no map of the new roads. It only prints a table with the shortest travelling distance between every pair of cities. Several different networks of NN roads can produce the same table. Given the table, find the smallest possible total length of the NN roads.

Input

The input holds several test cases and ends at the end of the file.

Each test case starts with a line holding an integer NN (2≤N≤20002 \le N \le 2000), the number of cities, which is also the number of roads. The next NN lines hold NN integers each. The jj-th integer on the ii-th line is the shortest distance from city ii to city jj. The distance from ii to ii is 0, the distance from ii to jj equals the distance from jj to ii, and every distance between two distinct cities is positive and at most 1,000,000. At least one network of NN roads matches the table.

Output

For each test case, print one line with the smallest possible total length of the NN roads.

Examples1

  1. Example 1

    Input
    4
    0 1 2 1
    1 0 2 1
    2 2 0 1
    1 1 1 0
    4
    0 1 1 1
    1 0 2 2
    1 2 0 2
    1 2 2 0
    3
    0 4 1
    4 0 3
    1 3 0
    
    Expected output
    4
    4
    5