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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct Tri {
	int a, b, val;
	Tri(int a, int b, int val) : a(a), b(b), val(val) {
	}
};

bool operator<(Tri a, Tri b) {
	return a.val < b.val;
}

vector<Tri> v;
int n, a;
int f[2010];
long long int res;

int rep(int a) {
	if (f[a] == a) {
		return a;
	}
	return f[a] = rep(f[a]);
}


int main () {
	cin >> n;
	for (int i=0; i<=n; i++) {
		f[i] = i;
	}
	for (int i=0; i<n; i++) {
		for (int j=i+1; j<=n; j++) {
			cin >> a;
			v.push_back(Tri(i, j, a));
		}
	}
	sort(v.begin(), v.end());
	for (int i=0; i<v.size(); i++) {
		if (rep(v[i].a) != rep(v[i].b)) {
			res += v[i].val;
			f[rep(v[i].a)] = rep(v[i].b);
		}
	}
	cout << res << endl;
	return 0;
}