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
51
#include <algorithm>
#include <cstdio>
using namespace std;

#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define REP(i,n) FOR(i,0,n)
typedef long long LL;

struct E {
	int c, i, j;
};

bool operator<(const E& e1, const E e2) {
	return e1.c < e2.c;
}

E e[2001000];
int fu[2001];

int find(int i) {
	if (fu[i] == i) return i;
	return fu[i] = find(fu[i]);
}

void unify(int i, int j) {
	fu[find(i)] = find(j);
}

int main() {
	int n;
	scanf("%d", &n);
	int nn = 0;
	REP(i,n) FOR(j,i+1,n+1) {
		e[nn].i = i;
		e[nn].j = j;
		scanf("%d", &e[nn].c);
		++nn;
	}
	sort(e, e + nn);
	REP(i,n+1) fu[i] = i;
	LL r = 0;
	int k = 0;
	REP(ii,nn) {
		if (find(e[ii].i) != find(e[ii].j)) {
			r += e[ii].c;
			if (++k == n) break;
			unify(e[ii].i, e[ii].j);
		}
	}
	printf("%lld\n", r);
}