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 Rust (1.15.1) (judge verdict: Accepted)

// This code is generated by [cargo-atcoder](https://github.com/tanakh/cargo-atcoder)
// Original source code:
/*
use proconio::input;

fn f(n: usize, e: &Vec<Vec<(usize, usize)>>, u: usize, v: usize) -> Option<usize> {
    let mut c = vec![None; n];
    let mut pq = std::collections::BinaryHeap::new();
    pq.push(std::cmp::Reverse((0, u)));
    while let Some(std::cmp::Reverse((c_u, u))) = pq.pop() {
        if c[u].is_some() && Some(c_u) > c[u] {
            continue;
        }
        for &(v, c_v1) in e[u].iter() {
            let c_v2 = c_u + c_v1;
            if c[v].is_none() || Some(c_v2) < c[v] {
                c[v] = Some(c_v2);
                pq.push(std::cmp::Reverse((c_v2, v)));
            }
        }
    }
    c[v]
}

fn main() {
    input! {
        n: usize,
        a: [[usize; n]; n],
    };

    let mut e_a = vec![];
    for u in 0..n {
        for v in u + 1..n {
            e_a.push((a[u][v], u, v));
        }
    }
    e_a.sort();

    let mut ans = 0;
    let mut e = vec![vec![]; n];
    for &(a_i, u, v) in e_a.iter() {
        match f(n, &e, u, v) {
            None => {
                e[u].push((v, a_i));
                e[v].push((u, a_i));
                ans += a_i;
            }
            Some(d) => {
                if d < a_i {
                    println!("-1");
                    return;
                }
                // d == a_i はほかから辿れるため不要な辺
                if d > a_i {
                    e[u].push((v, a_i));
                    e[v].push((u, a_i));
                    ans += a_i;
                }
            }
        }
    }
    println!("{}", ans);
}
*/

fn main() {
    let exe = "/tmp/bin91E84C0B";
    std::io::Write::write_all(&mut std::fs::File::create(exe).unwrap(), &decode(BIN)).unwrap();
    std::fs::set_permissions(exe, std::os::unix::fs::PermissionsExt::from_mode(0o755)).unwrap();
    std::process::exit(std::process::Command::new(exe).status().unwrap().code().unwrap())
}

fn decode(v: &str) -> Vec<u8> {
    let mut ret = vec![];
    let mut buf = 0;
    let mut tbl = vec![64; 256];
    for i in 0..64 { tbl[TBL[i] as usize] = i as u8; }
    for (i, c) in v.bytes().filter_map(|c| { let c = tbl[c as usize]; if c < 64 { Some(c) } else { None } }).enumerate() {
        match i % 4 {
            0 => buf = c << 2,
            1 => { ret.push(buf | c >> 4); buf = c << 4; }
            2 => { ret.push(buf | c >> 2); buf = c << 6; }
            3 => ret.push(buf | c),
            _ => unreachable!(),
        }
    }
    ret
}

const TBL: &'static [u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const BIN: &'static str = "<base64-encoded ELF executable removed>";