1 #include<iostream>
2 #include<algorithm>
3 using namespace std;
4
5 int fa[200010];
6 int n;
7 int cnt;
8
9 struct Edge
10 {
11 int u, v, w;
12 bool operator <(Edge s)
13 {
14 return w < s.w;
15 }
16 }e[200010];
17
18 int find(int x)
19 {
20 if (x == fa[x]) return x;
21 fa[x] = find(fa[x]);
22 return fa[x];
23 }
24
25 int main()
26 {
27 cin >> n;
28 for (int i = 1; i <= n; i++)
29 {
30 for (int j = 1; j <= n; j++)
31 {
32 cin >> e[++cnt].w;
33 e[cnt].u = i;
34 e[cnt].v = j;
35 }
36 }
37 sort(e + 1, e + cnt + 1);
38 int sum = 0;
39 int tot = 1;
40 for (int i = 1; i <= cnt; i++)
41 {
42 fa[i] = i;
43 if (find(e[i].u) != find(e[i].v))
44 {
45 sum += e[i].w;
46 fa[find(e[i].u)] = e[i].v;
47 tot++;
48 if (tot >= n) break;
49 }
50 }
51 cout << sum;
52 }