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
//Aleksander "kaalex" Kramarz

#include <cstdio>
#include <algorithm>
#include <set>
using namespace std;

int n, edge[2005][2005], d[2005];

struct cmp
{
	bool operator()(const int &a, const int &b)
	{
		if(d[a] != d[b])
			return d[a] < d[b];
		return a < b;
	}
};

set<int, cmp> Q;

void solve()
{
	long long res = 0;
	for(int i = 0; i <= n; i++)
	{
		d[i] = edge[i][0];
		Q.insert(i);
	}
	while(!Q.empty())
	{
		int u = *(Q.begin());
		Q.erase(Q.begin());
		res += d[u];
		d[u] = -1;
		for(int i = 0; i <= n; i++)
			if(d[i] != -1 && edge[u][i] < d[i])
			{
				Q.erase(Q.find(i));
				d[i] = edge[u][i];
				Q.insert(i);
			}
	}
	printf("%lld\n", res);
}	

int main()
{
	scanf("%d", &n);
	for(int i = 0; i < n; i++)
		for(int j = i+1; j <= n; j++)
		{
			scanf("%d", &edge[i][j]);
			edge[j][i] = edge[i][j];
		}
	solve();
}