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

typedef unsigned int uint;
#define REP(i,n) for(uint i = 0; i < (n); i += 1)


vector<int> p;

int Find(int el) {
  int root = el;
  while (p[root] >= 0) root = p[root];
  while (el != root) {
    int next = p[el];
    p[el] = root;
    el = next;
  }
  return el;
}

void Union(int el1, int el2) {
  if (p[el1] < p[el2]) {
    p[el1] += p[el2];
    p[el2] = el1;
  } else {
    p[el2] += p[el1];
    p[el1] = el2;
  }
}


int main () {
  uint n;
  cin >> n;
  vector< pair<uint, pair<uint, uint> > > cs;
  REP(i, n) {
    REP(j, n-i) {
      uint c;
      cin >> c;
      cs.push_back(make_pair(c, make_pair(i, i+1+j)));
    }
  }
  sort(cs.begin(), cs.end());
  p = vector<int>(n+1, -1);
  uint total_c = 0;
  uint n2 = 0;
  REP(i, cs.size()) {
    uint c = cs[i].first;
    uint u = cs[i].second.first;
    uint v = cs[i].second.second;
    int uu = Find(u);
    int vv = Find(v);
    if (uu == vv) continue;
    Union(uu, vv);
    // cerr << u+1 << "-" << v << endl;
    total_c += c;
    n2 += 1;
    if (n2 == n) {
      cout << total_c << endl;
      return 0;
    }
  }
  cout << "ASDASD";
}
// 0 1 2 3
//  a b c