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
#include <cstdio>
#include <queue>
#define MAXN 2010

using namespace std;

int c[MAXN][MAXN];
long long result;
bool processed[MAXN];
std::priority_queue<pair<int,int>, std::vector<pair<int,int> >, std::greater<pair<int,int> > > q;
int n;

void add_e(int v) {
  for (int i = 0; i < v; i++) {
    q.push(make_pair(c[i+1][v], i));
  }
  for (int i = v+1; i <= n; i++) {
    q.push(make_pair(c[v+1][i], i)); 
  }
}

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

  processed[0] = true;
  add_e(0);

  while (!q.empty()) {
    pair<int,int> e = q.top();
    q.pop();
    if (!processed[e.second]) {
      result += e.first;
      processed[e.second] = true;
      add_e(e.second);
    }
  }
  
  printf("%lld\n", result);
}