PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FW0000068. Khôi phục mạng đường (Restoring Road Network)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Khôi phục mạng đường (Restoring Road Network)

Restoring Road Network

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

Given nn cities and the all-pairs shortest-distance matrix AA, reconstruct an undirected positive-weight road network matching all distances and minimizing the sum of lengths of its roads. Output -1 if the matrix is impossible.

Input

First nn, then an n×nn\times n symmetric integer matrix AA with zero diagonal and positive off-diagonal entries.

Output

Print the minimum total length, or -1.

Subtasks

  • Subtask 1 (40 points): 2≤n≤62\le n\le 6. Aii=0A_{ii}=0, Aij=Aji>0A_{ij}=A_{ji}>0 với i≠ji\ne j, Aij≤106A_{ij}\le 10^6.

  • Subtask 2 (60 points): 2≤n≤302\le n\le 30. Aii=0A_{ii}=0, Aij=Aji>0A_{ij}=A_{ji}>0 với i≠ji\ne j, Aij≤106A_{ij}\le 10^6.

Examples

Input

3
0 2 5
2 0 3
5 3 0

Output

5

Explanation

Road 1–2 costs 2 and road 2–3 costs 3. The distance 1–3 is already 5 via city 2, so only the two indispensable edges remain, with total weight 5.