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
#include <algorithm>
#include <iostream>
#include <iterator>

const int MAX_N = 2005;

int dist[MAX_N][MAX_N];
int best[MAX_N];
bool mark[MAX_N];

int main() {
	std::ios_base::sync_with_stdio(false);
	int n;
	std::cin >> n;

	for(int i = 1; i <= n; ++i)
		for(int j = i; j <= n; ++j) {
			int a;
			std::cin >> a;
			dist[i - 1][j] = dist[j][i - 1] = a;
		}

	mark[0] = true;
	std::copy(dist[0], dist[0] + n + 1, best);

	long long total = 0;
	for(int i = 1; i <= n; ++i) {
		int v = -1;
		for(int c = 0; c <= n; ++c)
			if(!mark[c] && (v == -1 || best[v] > best[c]))
				v = c;

		total += best[v];
		mark[v] = true;

		for(int j = 0; j <= n; ++j)
			best[j] = std::min(best[j], dist[v][j]);
	}

	std::cout << total << std::endl;
}