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

namespace {
  long long mst(vector<vector<int>> const& dist)
  {
    int n = dist.size();
    vector<int> best = dist[0];
    long long res = 0;
    for (int i = 1; i < n; ++i) {
      int v = 0;
      for (int j = 0; j < n; ++j) {
        if (best[j] == 0) continue;
        if (best[v] == 0 || best[v] > best[j]) v = j;
      }
      res += best[v];
      for (int j = 0; j < n; ++j) {
        best[j] = min(best[j], dist[v][j]);
      }
    }
    return res;
  }
}

int main()
{
  iostream::sync_with_stdio(false);
  cin.tie(NULL);

  int n;
  cin >> n;

  vector<vector<int>> dist(n+1, vector<int>(n+1));

  for (int i = 0; i < n; ++i) {
    for (int j = i+1; j <= n; ++j) {
      cin >> dist[i][j];
      dist[j][i] = dist[i][j];
    }
  }

  cout << mst(dist) << '\n';

  return 0;
}