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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<cstring>

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define VI vector<int>
#define PII pair<int,int>
#define st first
#define nd second
#define mp make_pair
#define pb push_back
#define lint long long int
#define max_n 2005

using namespace std;

int n;


vector< pair<int,PII> > wejscie;

int FU[max_n];
int fusize[max_n];

int fufind(int x){
	if(FU[x]==x) return x;
	else{
		int y = fufind(FU[x]);
		FU[x] = y;
		return y;
	}	
}

void funion(int x,int y){
	int xx = fufind(x); int yy = fufind(y);
	if(fusize[xx]>fusize[yy]){;
		FU[yy] = xx;
		fusize[xx]++;
	}else{
		FU[xx] = yy;
		fusize[yy]++;
	}
}

int main(){
	scanf("%d",&n);
	wejscie.clear();
	FOR(i,0,n){
		FOR(j,0,n-i){
			int c;
			scanf("%d",&c);
			wejscie.pb(mp(c,mp(i,i+j+1)));
		}
	}
	sort(wejscie.begin(),wejscie.end());

	FOR(i,0,n+1) { fusize[i] = 1; FU[i] = i;}

	lint res = 0;
	int ile = 0;
	FOR(i,0,wejscie.size()){
		if(ile==n) break;
		PII p = wejscie[i].nd;
		int c = wejscie[i].st;
		if(fufind(p.st)!=fufind(p.nd)){
			ile++;
			res+=c*1LL;
			funion(p.st,p.nd);
		}
	}
	printf("%lld\n",res);
		
	return 0;
}