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
#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

struct Edge {
  int u, v;
  int c;

  Edge() {
    u = v = c = -1;
  }

  Edge(int _u, int _v, int _c) {
    u = _u;
    v = _v;
    c = _c;
  }

  bool operator < (const Edge& e) const {
    return c < e.c;
  }
};

const int MX = 2005;

int n;
int FU[MX], FUrank[MX];
vector<Edge> edges;

long long res;

int FUfind(int u) {
  if (u != FU[u]) FU[u] = FUfind(FU[u]);
  return FU[u];
}

void FUunion(int u, int v) {
  u = FUfind(u);
  v = FUfind(v);

  if (FUrank[u] < FUrank[v]) swap(u, v);
  
  if (FUrank[u] == FUrank[v]) ++FUrank[u];

  FU[v] = u;
}

int main() {
  scanf("%d", &n);

  for (int i = 0; i < n+1; ++i) FU[i] = i;

  for (int i = 0; i < n; ++i) {
    for (int j = i; j < n; ++j) {
      int u = i, v = j+1, c;
      scanf("%d", &c);
      edges.push_back(Edge(u, v, c));
    }
  }
  sort(edges.begin(), edges.end());
  
  for (int i = 0; i < (int) edges.size(); ++i) {
    if (FUfind(edges[i].u) != FUfind(edges[i].v)) {
      res += (long long) edges[i].c;
      FUunion(edges[i].u, edges[i].v);
    }
  }

  printf("%lld\n", res);
  return 0;
}