1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <bits/stdc++.h>

#define FWD(a,b,c) for(int a=(b); a<(c); ++a)

using namespace std;

typedef long long LL;

const int MAXN = 2010;
const int INF = 1000000010;

struct edge{
	int a, b, c;
};

inline bool cmp(const edge &a, const edge &b){
	return a.c < b.c;
}

int n;
LL r;
vector<edge> edges;
int p[MAXN];

int find(int u){
	return p[u]==u?u:p[u]=find(p[u]);
}

int main(){
	scanf("%d", &n);
	FWD(i,0,n)
		FWD(j,i+1,n+1){
			edge e;
			e.a = i;
			e.b = j;
			scanf("%d", &e.c);
			edges.push_back(e);
		}
	FWD(i,0,n+1)
		p[i] = i;
	sort(edges.begin(), edges.end(), cmp);
	for(edge e : edges){
		if(find(e.a) != find(e.b)){
			r += e.c;
			p[p[e.a]] = p[e.b];
		}
	}
	printf("%lld\n", r);
	return 0;
}