Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

type ElementKey int
type Node struct {
	key    ElementKey
	parent *Node
	rank   int
}

func newNode(key ElementKey) *Node {
	node := Node{}
	node.key = key
	node.parent = &node
	node.rank = 0

	return &node
}

func union(x, y *Node) {
	xSet := findSet(x)
	ySet := findSet(y)
	link(xSet, ySet)
}

func link(x, y *Node) {
	if x.rank > y.rank {
		y.parent = x
	} else {
		x.parent = y

		if x.rank == y.rank {
			y.rank++
		}
	}
}

func findSet(x *Node) *Node {
	if x != x.parent {
		x.parent = findSet(x.parent)
	}
	return x.parent
}

func newDisjointSets(nSets int) *DisjointSets {
	sets := DisjointSets{}
	sets.keyToNode = map[ElementKey]*Node{}
	for i := 0; i < nSets; i++ {
		key := ElementKey(i)
		sets.keyToNode[key] = newNode(key)
	}
	return &sets
}

type DisjointSets struct {
	keyToNode map[ElementKey]*Node
}

func (sets *DisjointSets) union(x, y ElementKey) {
	xNode := sets.keyToNode[x]
	yNode := sets.keyToNode[y]
	union(xNode, yNode)
}

func (sets DisjointSets) findSet(x ElementKey) *Node {
	xNode := sets.keyToNode[x]
	return findSet(xNode)
}

type Edge struct {
	i, j   int
	weight int
}

func main() {
	scanner.Split(bufio.ScanWords)

	n := nextInt()
	sets := newDisjointSets(n)
	edges := []Edge{}
	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, n)
		for j := 0; j < n; j++ {
			a[i][j] = nextInt()
			if a[i][j] >= 0 {
				e := Edge{
					i:      i,
					j:      j,
					weight: a[i][j],
				}
				edges = append(edges, e)
			}
		}
	}
	sort.Slice(edges, func(i, j int) bool { return edges[i].weight < edges[j].weight })
	totalWeight := 0
	for _, edge := range edges {
		i := ElementKey(edge.i)
		j := ElementKey(edge.j)
		if sets.findSet(i) != sets.findSet(j) {
			totalWeight += edge.weight
			sets.union(i, j)
		}
	}
	fmt.Println(totalWeight)
}

C++: #include <bits/stdc++.h>
using namespace std;

struct UF{
   vector<int> par;
   vector<int> sz;
   UF(){}
   UF(int n){
       par.resize(n);
       sz.resize(n, 1);
       for(int i = 0; i < n; i++) par[i] = i;
   }
   int find(int x){
       if(x == par[x]) return x;
       return par[x] = find(par[x]);
   }
   void unite(int x, int y){
       x = find(x); y = find(y);
       if(x == y) return;
       if(sz[x] < sz[y]) swap(x, y);
       sz[x] += sz[y];
       par[y] = x;
   }
   bool same(int x, int y){
       return find(x) == find(y);
   }
};

struct edge{
    int u, v, cost;
    bool operator<(const edge &e) const{
        return cost < e.cost;
    }
};

int n, m;
vector<edge> es; 

int kruskal(){
    int res = 0;    
    UF uf(n);
    sort(es.begin(), es.end());
    for(int i = 0; i < m; i++){
        edge e = es[i];
        if(!uf.same(e.u, e.v)){
            uf.unite(e.u, e.v);
            res += e.cost;
        }
    }
    return res;
}

int main(){
    cin >> n;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            int in;
            cin >> in;
            if(in == -1) continue;
            es.push_back({i, j, in});
        }
    }
    m = es.size();
    cout << kruskal() << endl;
}

C++: # include <iostream>
using namespace std;
const int MAXN = 1000;
int main(void)
{
	int d[MAXN],a[MAXN][MAXN],vis[MAXN],p[MAXN];
	int n,ans = 0,cnt = 0;
	scanf("%d",&n);
	for(int i = 0;i < n;i++) for(int j = 0;j < n;j++) scanf("%d",&a[i][j]);
	for(int i = 0;i < n;i++)
	{
		vis[i] = 0;
		p[i] = -1;
		d[i] = 2001;
	}
	d[0] = 0;
	while(true)
	{
		int u = -1,minv = 2001;
		for(int i = 0;i < n;i++)
		{
			if(d[i] < minv && !vis[i])
			{
				u = i;
				minv = d[i];
			}	
		}
		if(u == -1) break;
		cnt++;
		vis[u] = 1;
		for(int v = 0;v < n;v++)
		{
			if(!vis[v] && a[u][v] != -1)
			{
				if(d[v] > a[u][v])
				{
					d[v] = a[u][v];
					p[v] = u;
				}
			}
		}
	}
	for(int i = 0;i < n;i++) if(p[i] != -1) ans += a[i][p[i]];
	printf("%d\n",(cnt == n)?ans:-1);
	return 0;
}

C++: #if 0
????????£????????????1.5h
??¢?????????????????§??????????????¢??°????????£????????????sum????¨??????¨????????????????????£????????§?????????
???????????¢??????????????§????????????????????????????????????"-1"???????????????????????????????????§???????????§????????¨???????°???????????????????
#endif

#include <iostream>
#include <algorithm>
#include <utility>
using namespace std;

int P[10000+10];

void init(int N){
	for(int i=0;i<=N;++i) P[i]=i;
}
int root(int a){
	if(P[a]==a)return a;
	else return(P[a]=root(P[a]));
}
bool is_same_set(int a,int b){
	return root(a)==root(b);
}
void unite(int a,int b){
	P[root(a)]=root(b);
}
int main(){
	int N;
	cin>>N;
	int A[N][N];
	
	for(int i=0; i<N;i++){
		for(int j=0;j<N;j++){
			cin>>A[j][i]; //???????????\???
		}
	}

	int M = 0; 
	int cost[N*N],src[N*N],dst[N*N];
	for (int r=0; r<N;r++)
		for (int c=0; c<r;c++) //r<c???????§?????????????
			if (A[r][c] > -1) {
				cost[M] = A[r][c];
				src[M] = r;
				dst[M] = c;
				M += 1;
			}
	pair<int,int> edges[2000+10];
	for(int i=0;i<M;i++)
		edges[i]=make_pair(cost[i],i); //first???????????????second???ID
	sort(edges,edges+M);
	
	//for(int i=0;i<M;++i)
		//cout<<edges[i].first<<endl;

	init(N);
	int sum=0;
	for(int i=0;i<M;i++){
		if(is_same_set(src[edges[i].second],dst[edges[i].second])==0){
			unite(src[edges[i].second],dst[edges[i].second]);
			sum+=edges[i].first;
		}
	}
		cout<<sum<<endl;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, n) for (int i = (int)(n); i >= 0; i--)
#define REP(i, m, n) for (int i = (int)(m); i <= (int)(n); i++)
#define REPR(i, m, n) for (int i = (int)(m); i >= (int)(n); i--)
#define all(v) v.begin(), v.end()
typedef long long ll;

using Graph = vector<vector<int> >;

int n, sum;
vector<bool> seen;
priority_queue<pair<int, int> > pq;

void dfs(const Graph &G, int v, int p){
  seen[v] = true;

  rep(u, n){
    if(u == p) continue;
    if(G[v][u] == -1) continue;
    pq.push(make_pair((-1)*G[v][u], u));
  }

  while(!pq.empty()){
    pair<int, int> tmp;
    tmp = pq.top();
    pq.pop();
    if(seen[tmp.second]) continue;
    sum += (-1)*tmp.first;
    int nv = tmp.second;
    p = v;
    dfs(G, nv, p);
  }

  return;
}

int main(){
  cin >> n;
  Graph G(n, vector<int>(n));
  rep(i, n) rep(j, n) cin >> G[i][j];

  seen.assign(n, false);
  sum = 0;
  dfs(G, 0, -1);

  cout << sum << endl;

  return 0;
}

C++: #include <iostream>
#include <utility>
#include <string>
#include <algorithm>
#include <set>
#include <deque>
#include <vector>
#define MAX 150
#define rep(i,p) for(int i=0;i<p;i++)
#define rep1(i,p) for(int i=1;i<=p;i++)
#define pb push_back
using namespace std;
int n,temp,a,b;
int A[MAX];
int B[MAX];
int W[MAX];
int V[MAX];
pair<int,int> edge[MAX];
int main(){
  cin >> n;
  rep(i,n) 
    rep(j,n) {
      int nnn = i*n + j;
      cin >> W[nnn];
      A[nnn] = i;
      B[nnn] = j;
      edge[nnn].first = W[nnn];
      edge[nnn].second = nnn;
  }
  // sort by lenght of edge
  sort(edge, edge+n*n);
  // printing
  // rep(i,n*n) {
  //   cout << A[edge[i].second] << " " << B[edge[i].second]
  //        << " " << edge[i].first << endl;;
  // }
  int sum = 0;
  rep(i,n) V[i] = i;
  rep(i,n*n) {
    if (edge[i].first == -1) continue;
    temp = edge[i].second;
    // a , b : vertex
    a = A[temp]; 
    b = B[temp];
    if (V[a] != V[b]) {
      // ?????????????????????
      sum += edge[i].first;
      if (V[a] != a && V[b] == b){
        V[b] = V[a];
      } else if (V[a] == a && V[b] != b) {
        V[a] = V[b];
      } else if (V[a] == a && V[b] == b) {
        V[b] = V[a];
      }
    } else {

    }
  }
  cout << sum << endl;
  return 0;
}