Problem:
Score : 500 points

Problem Statement

In Takahashi Kingdom, which once existed, there are N cities, and some pairs of cities are connected bidirectionally by roads.
The following are known about the road network:

People traveled between cities only through roads. It was possible to reach any city from any other city, via intermediate cities if necessary.

Different roads may have had different lengths, but all the lengths were positive integers.

Snuke the archeologist found a table with N rows and N columns, A, in the ruin of Takahashi Kingdom.
He thought that it represented the shortest distances between the cities along the roads in the kingdom.

Determine whether there exists a road network such that for each u and v, the integer A_{u, v} at the u-th row and v-th column of A is equal to the length of the shortest path from City u to City v.
If such a network exist, find the shortest possible total length of the roads.

Constraints

1 \leq N \leq 300

If i ≠ j, 1 \leq A_{i, j} = A_{j, i} \leq 10^9.

A_{i, i} = 0

Inputs

Input is given from Standard Input in the following format:

N
A_{1, 1} A_{1, 2} ... A_{1, N}
A_{2, 1} A_{2, 2} ... A_{2, N}
...
A_{N, 1} A_{N, 2} ... A_{N, N}

Outputs

If there exists no network that satisfies the condition, print -1.
If it exists, print the shortest possible total length of the roads.

Sample Input 1

3
0 1 3
1 0 2
3 2 0

Sample Output 1

3

The network below satisfies the condition:

City 1 and City 2 is connected by a road of length 1.

City 2 and City 3 is connected by a road of length 2.

City 3 and City 1 is not connected by a road.

Sample Input 2

3
0 1 3
1 0 1
3 1 0

Sample Output 2

-1

As there is a path of length 1 from City 1 to City 2 and City 2 to City 3, there is a path of length 2 from City 1 to City 3.
However, according to the table, the shortest distance between City 1 and City 3 must be 3.

Thus, we conclude that there exists no network that satisfies the condition.

Sample Input 3

5
0 21 18 11 28
21 0 13 10 26
18 13 0 23 13
11 10 23 0 17
28 26 13 17 0

Sample Output 3

82

Sample Input 4

3
0 1000000000 1000000000
1000000000 0 1000000000
1000000000 1000000000 0

Sample Output 4

3000000000

Solution in C# (Mono 4.6.2.0) (judge verdict: Time Limit Exceeded)

using System;
using System.Collections.Generic;
using System.Linq;

public class Table2D<TKey, TValue>
{
    private readonly Dictionary<TKey, Dictionary<TKey, TValue>> dict = new Dictionary<TKey, Dictionary<TKey, TValue>>();

    public Dictionary<TKey, TValue> this[TKey key]
    {
        get { return dict[key]; }
    }

    public TValue GetValue(TKey key1, TKey key2, TValue defaultValue)
    {
        if (dict.ContainsKey(key1) && dict[key1].ContainsKey(key2))
        {
            return dict[key1][key2];
        }
        return defaultValue;
    }

    public Dictionary<TKey, Dictionary<TKey, TValue>>.KeyCollection Keys
    {
        get { return dict.Keys; }
    }

    public Dictionary<TKey, Dictionary<TKey, TValue>>.ValueCollection Values
    {
        get { return dict.Values; }
    }

    public int Count
    {
        get { return dict.Count; }
    }

    public void Add(TKey key1, TKey key2, TValue value)
    {
        if (!dict.ContainsKey(key1))
        {
            dict.Add(key1, new Dictionary<TKey, TValue>());
        }
        dict[key1].Add(key2, value);
    }

    public void Set(TKey key1, TKey key2, TValue value)
    {
        if (!dict.ContainsKey(key1))
        {
            dict.Add(key1, new Dictionary<TKey, TValue>());
        }
        dict[key1][key2] = value;
    }

    public bool Remove(TKey key1, TKey key2)
    {
        return dict[key1].Remove(key2);
    }

    public bool Remove(TKey key)
    {
        return dict.Remove(key);
    }

    public void Clear()
    {
        dict.Clear();
    }

    public IEnumerator<KeyValuePair<TKey, Dictionary<TKey, TValue>>> GetEnumerator()
    {
        foreach (var n in dict)
        {
            yield return n;
        }
    }

    public IEnumerable<Tuple<TKey, TKey, TValue>> GetFlatten()
    {
        foreach (var n in dict)
        {
            foreach (var v in n.Value)
            {
                yield return new Tuple<TKey, TKey, TValue>(n.Key, v.Key, v.Value);
            }
        }
    }
}

public class Graph
{
    // [点i][点j] = 距離 の有向グラフ
    public Table2D<int, long> Edges;
    // [MinVertexIndex, MaxVertexIndex)がIndexの存在区間
    public int MinVertexIndex;
    public int MaxVertexIndex;

    public Graph(int min, int max)
    {
        Edges = new Table2D<int, long>();
        MinVertexIndex = min;
        MaxVertexIndex = max;
    }

    public void AddEdge(int i, int j, long dist)
    {
        Edges.Add(i, j, dist);
    }

    public void SetEdge(int i, int j, long dist)
    {
        Edges.Set(i, j, dist);
    }

    // n
    // A1,1 A1,2 … A1,N
    // …
    // AN,1 AN,2 … AN,N
    // の形式から読み込む
    public static Graph BuildByReadLine(int origin = 0)
    {
        var n = int.Parse(Console.ReadLine());
        var graph = new Graph(origin, n);
        for (int i = 0; i < n; i++)
        {
            var row = Console.ReadLine().Split(' ').Select(long.Parse).ToArray();
            for (int j = 0; j < row.Length; j++)
            {
                graph.AddEdge(i + origin, j + origin, row[j]);
            }
        }
        return graph;
    }

    public long Distance(int i, int j)
    {
        if (i == j)
        {
            return 0;
        }
        return Edges.GetValue(i, j, long.MaxValue);
    }
    
    public static long Sum(long a, long b)
    {
        if (a == long.MaxValue || b == long.MaxValue)
        {
            return long.MaxValue;
        }
        return a + b;
    }

    public Graph WarshallFloyd()
    {
        var first = MinVertexIndex;
        var last = MaxVertexIndex;
        var dist = new Graph(first, last);

        for (int i = first; i < last; i++)
        {
            for (int j = first; j < last; j++)
            {
                dist.AddEdge(i, j, Distance(i, j));
            }
        }

        for (int k = first; k < last; k++)
        {
            for (int i = first; i < last; i++)
            {
                for (int j = first; j < last; j++)
                {
                    var newDist = Sum(dist.Distance(i, k), dist.Distance(k, j));
                    if (newDist < dist.Distance(i, j))
                    {
                        dist.SetEdge(i, j, newDist);
                    }
                }
            }
        }

        return dist;
    }

    // WarshallFloydで求めた双方向グラフについて，
    // 各最短経路を維持できる辺の最小合計値を求める
    public long GetMinDistanceSum()
    {
        long sum = 0;
        var first = MinVertexIndex;
        var last = MaxVertexIndex;

        // 辺ijを自身の辺を通らずkを経由したもので代替できるかチェック
        for (int i = first; i < last; i++)
        {
            for (int j = i+1; j < last; j++)
            {
                long d = long.MaxValue;
                for (int k = first; k < last; k++)
                {
                    if (i == k || j == k)
                    {
                        continue;
                    }
                    d = Math.Min(d, Distance(i, k) + Distance(k, j));
                }
                // 代替不可であれば，必要な辺
                if (Distance(i, j) < d)
                {
                    sum += Distance(i, j);
                }
            }
        }
        return sum;
    }
}

class Solution
{
    static void Main()
    {
        var graph = Graph.BuildByReadLine();
        var minDist = graph.WarshallFloyd();
        for(int i = 0; i < graph.MaxVertexIndex;i++)
        {
            for (int j = 0; j < graph.MaxVertexIndex; j++)
            {
                if(graph.Distance(i, j) != minDist.Distance(i, j))
                {
                    Console.WriteLine(-1);
                    return;
                }
            }
        }

        var sum = graph.GetMinDistanceSum();
        Console.WriteLine(sum);
    }
}