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: Accepted)

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Text;
using static System.Console;
using static System.Convert;
using static System.Math;
using static Extentions;

class IO
{
    int idx;
    string[] input = In.ReadToEnd().Split(new[] { " ", "\n", "\r" },
        StringSplitOptions.RemoveEmptyEntries);

    T Get<T>(Func<string, T> parser) => parser(input[idx++]);

    public string S => Get(s => s);
    public char C => Get(char.Parse);
    public int I => Get(int.Parse);
    public long L => Get(long.Parse);
    public double F => Get(double.Parse);
    public decimal D => Get(decimal.Parse);
    public BigInteger B => Get(BigInteger.Parse);

    T[] Gets<T>(int n, Func<string, T> parser)
        => input.Skip((idx += n) - n).Take(n).Select(parser).ToArray();

    public string[] Ss(int n) => Gets(n, s => s);
    public char[] Cs(int n) => Gets(n, char.Parse);
    public int[] Is(int n) => Gets(n, int.Parse);
    public long[] Ls(int n) => Gets(n, long.Parse);
    public double[] Fs(int n) => Gets(n, double.Parse);
    public decimal[] Ds(int n) => Gets(n, decimal.Parse);
    public BigInteger[] Bs(int n) => Gets(n, BigInteger.Parse);

    public void Write<T>(params T[] xs) => WriteLine(string.Join(" ", xs));
    public void Write(params object[] xs) => WriteLine(string.Join(" ", xs));
}

#region Library

class Monoid<T>
{
    public T Unit { get; private set; }
    Func<T, T, T> append;
    public T Append(T left, T right) => append(left, right);

    public Monoid(T unit, Func<T, T, T> append)
    {
        this.Unit = unit;
        this.append = append;
    }
}

abstract class Graph<T> where T : IEquatable<T>
{
    public abstract T this[int source, int target] { get; set; }

    public int V { get; protected set; }
    public Monoid<T> Monoid { get; protected set; }
    public Monoid<T> Comparer { get; protected set; }

    public event Action GraphChanged;
    protected void OnGraphChanged() => GraphChanged?.Invoke();

    public bool HasEdge(int source, int target)
        => !this[source, target].Equals(this.Comparer.Unit);
}

class AdjacencyMatrix<T> : Graph<T> where T : IEquatable<T>
{
    protected readonly T[,] graph;

    public override T this[int source, int target]
    {
        // O(1)
        get { return graph[source, target]; }

        // O(1)
        set
        {
            var a = this.Comparer.Append(graph[source, target], value);
            if (a.Equals(graph[source, target])) return;
            graph[source, target] = a;
            OnGraphChanged();
        }
    }

    // O(V^2)
    public AdjacencyMatrix(int v, Monoid<T> monoid, Monoid<T> comparer)
    {
        this.V = v;
        this.Monoid = monoid;
        this.Comparer = comparer;
        graph = new T[v, v];
        for (var i = 0; i < v; i++)
            for (var j = 0; j < v; j++)
                graph[i, j] = i == j ? monoid.Unit : comparer.Unit;
    }
}

class WarshallFloyd<T> where T : IEquatable<T>
{
    AdjacencyMatrix<T> graph;
    T[,] d;
    bool calculated = false;

    // O(1)
    public WarshallFloyd(AdjacencyMatrix<T> graph)
    {
        this.graph = graph;
        d = new T[graph.V, graph.V];
        graph.GraphChanged += () => calculated = false;
    }

    // O(V^3)
    public void Calculate()
    {
        if (calculated) return;

        for (var i = 0; i < graph.V; i++)
            for (var j = 0; j < graph.V; j++) d[i, j] = graph[i, j];

        for (var k = 0; k < graph.V; k++)
            for (var i = 0; i < graph.V; i++)
                for (var j = 0; j < graph.V; j++)
                    d[i, j] = graph.Comparer.Append(d[i, j],
                        d[i, k].Equals(graph.Comparer.Unit)
                        || d[k, j].Equals(graph.Comparer.Unit)
                        ? graph.Comparer.Unit
                        : graph.Monoid.Append(d[i, k], d[k, j]));

        calculated = true;
    }

    // O(1)
    public T GetMinCost(int source, int target)
    {
        if (!calculated) throw new InvalidOperationException();
        return d[source, target];
    }
}

class UnionFind
{
    int[] tree;

    // O(N)
    public int CountGroups => tree.Count(x => x < 0);

    // O(N)
    public UnionFind(int length)
    {
        tree = Enumerable.Repeat(-1, length).ToArray();
    }

    // O(α(N))
    public bool Union(int x, int y)
    {
        x = this.Find(x);
        y = this.Find(y);
        if (x == y) return false;
        if (tree[x] > tree[y]) Swap(ref x, ref y);
        tree[x] += tree[y];
        tree[y] = x;
        return true;
    }

    // O(α(N))
    public int Find(int x)
    {
        if (tree[x] < 0) return x;
        return tree[x] = this.Find(tree[x]);
    }

    // O(α(N))
    public bool Same(int x, int y) => this.Find(x) == this.Find(y);

    // O(α(N))
    public int Count(int x) => -tree[this.Find(x)];
}

static class Extentions
{
    public static long Sum(long left, long right) => left + right;

    public static void Swap<T>(ref T x, ref T y)
    {
        var tmp = x;
        x = y;
        y = tmp;
    }
}

#endregion

static class Program
{
    public static void Main()
    {
        var sw = new StreamWriter(OpenStandardOutput()) { NewLine = "\n" };
#if DEBUG
        sw.AutoFlush = true;
#else
        sw.AutoFlush = false;
#endif
        SetOut(sw);
        Solve(new IO());
        Out.Flush();
    }

    static void Solve(IO io)
    {
        var n = io.I;
        var a = new long[n, n];

        var gr = new AdjacencyMatrix<long>(n,
            new Monoid<long>(0, Sum), new Monoid<long>(long.MaxValue, Min));

        for (var i = 0; i < n; i++)
            for (var j = 0; j < n; j++) gr[i, j] = a[i, j] = io.L;

        var wf = new WarshallFloyd<long>(gr);
        wf.Calculate();

        var sum = 0L;

        for (var i = 0; i < n; i++)
            for (var j = 0; j < n; j++) sum += wf.GetMinCost(i, j);

        if (a.Cast<long>().Sum() != sum)
        {
            io.Write(-1);
            return;
        }


        var ans = 0L;

        for (var i = 0; i < n; i++)
        {
            for (var j = 0; j < n; j++)
            {
                var flg = true;

                for (var k = 0; k < n; k++)
                {
                    if (i == j || i == k || j == k) continue;

                    if (a[i, j] >= a[i, k] + a[k, j])
                    {
                        flg = false;
                        break;
                    }
                    //                    Error.WriteLine($"{i} {j} {k} {a[i, j]} {a[i, k]} {a[k, j]} {(a[i, j] < a[i, k] + a[k, j] ? '!' : ' ')}");
                }

                if (flg) ans += a[i, j];
            }
        }

        io.Write(ans >> 1);
    }
}