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
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cmath>
#include <algorithm>
using namespace std;
#define REP(i,a,b) for(int i=(a);i<=(b);++i)
#define FORI(i,n) REP(i,1,n)
#define FOR(i,n) REP(i,0,int(n)-1)
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define vi vector<int>
#define ll long long
#define SZ(x) int((x).size())
#define DBG(v) cerr << #v << " = " << (v) << endl;
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define SORT(X) sort(X.begin(),X.end())
#define fi first
#define se second

vector<pair<ll,pair<int,int> > > V;

ll suma;

int Rep[2020];
int Find(int a){
  if(Rep[a] != a) Rep[a] = Find(Rep[a]);
  return Rep[a];
}
int Union(int a,int b){
  a = Find(a);
  b = Find(b);
  if(a == b) return 0;
  Rep[a] = b;
  return 1;
}

int main () {
  int n;
  scanf("%d",&n);
  FORI(i,n) Rep[i] = i;
  FORI(i,n) REP(j,i,n){
    ll a;
    scanf("%lld",&a);
    V.pb(mp(a,mp(i,j+1)));
  }
  sort(V.begin(),V.end());
  
  FOR(i,SZ(V)){
    if(Union(V[i].se.fi,V[i].se.se)) suma += V[i].fi;
  }
  printf("%lld\n", suma);
  return 0;
  
}