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

#define ref &
#define st first
#define nd second
#define mp make_pair
#define pb push_back
#define poka(x) cerr << #x << " = " << x << endl;

typedef pair<int,int> pii;

////////////////////////////////////////////////////////////////////////////////

typedef pair<int,pair<int,int> > prz;

const int mx = 2000 + 9;
bool se[mx][mx];

int main(){
	int n;
	scanf("%d", &n);
	vector<prz> v;
	v.reserve(n*(n+1)/2 + 9);
	for(int i = 1; i <= n; ++i){
		for(int k = i; k <= n; ++k){
			int w;
			scanf("%d", &w);
			v.pb(mp(w,mp(i,k)));
		}
	}
	
	sort(v.begin(), v.end());
	
	vector<vector<int> > kon(n+9);
	for(int i = 0; i < kon.size(); ++i){
		kon[i].reserve(n+9);
		kon[i].pb(i);
	}
	
	long long wyn = 0;
	for(int j = 0; j < v.size(); ++j){
		const int w = v[j].st, a = v[j].nd.st - 1, b = v[j].nd.nd;
		if(se[a][b]) continue;
		wyn += w;
		
		vector<pii> dodac;
		for(int i = 0; i < kon[a].size(); ++i)
			for(int k = 0; k < kon[b].size(); ++k)
				dodac.pb(pii(min(kon[a][i],kon[b][k]),max(kon[a][i],kon[b][k])));
				
		for(int i = 0; i < dodac.size(); ++i){					
			const int x = dodac[i].st, y = dodac[i].nd;
			se[x][y] = se[y][x] = 1;
			kon[x].pb(y);
			kon[y].pb(x);
		}
	}
	printf("%lld\n", wyn);
	return 0;
}