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
#include<stdio.h>
#include<algorithm>
#include<set>
#define inf 2000000000
using namespace std;
int c[2005][2005],d[2005];
bool byl[2005];
struct cmp
{
    bool operator()(const int &a ,const int &b)
    {
	return d[a]< d[b] || (d[a]==d[b] && a<b);
    }
};
int main()
{
    int n;
    scanf("%d",&n);
    for (int i=0;i<n;i++)
    {
	d[i] = d[n] = inf;
	for (int j=i;j<n;j++)
	{
	    int a;
	    scanf("%d",&a);
	    c[i][j+1] = c[j+1][i] = a;
	}
    }
    n++;
    byl[0]= true;d[0] = 0;
    long long res=0;
    set<int, cmp> s;
    for (int i=0;i<n;i++) s.insert(i);
    int ile=0;
    while(!s.empty())
    {
	int u = *s.begin();
	s.erase(s.begin());
	byl[u]= true;
	for (int i =0;i<n;i++)
	{
	    if(byl[i]) continue;
	    if(c[u][i] < d[i])
	    {
		s.erase(s.find(i));
		d[i] = c[u][i];
		s.insert(i);
	    }
	}
    }
    for (int i=0;i<n;i++)
	res+=d[i];
    printf("%lld\n",res);
    return 0;
}