Go: package main

import (
	"fmt"
	"sort"
)

type Edge struct {
	I int
	J int
	A int
}

func main() {
	var n int
	fmt.Scan(&n)

	g := make([][]int, n)
	edges := make([]Edge, 0, n*(n-1)/2)
	parents := make([]int, n)
	for i := 0; i < n; i++ {
		g[i] = make([]int, n)
		for j := 0; j < n; j++ {
			fmt.Scan(&g[i][j])
			if g[i][j] > 0 {
				edges = append(edges, Edge{i, j, g[i][j]})
			}
		}
		parents[i] = i
	}
	sort.SliceStable(edges, func(i, j int) bool { return edges[i].A > edges[j].A })

	sum := 0
	for len(edges) > 0 {
		edge := edges[len(edges)-1]
		edges = edges[:len(edges)-1]

		if same(edge.I, edge.J, &parents) {
			continue
		}
		union(edge.I, edge.J, &parents)
		sum += edge.A
	}
	fmt.Println(sum)
}



func root(id int, parents *[]int) int {
	if id == (*parents)[id] {
		return id
	}
	return root((*parents)[id], parents)
}

func union(i, j int, parents *[]int) {
	(*parents)[root(j, parents)] = (*parents)[root(i, parents)]
}

func same(i, j int, parents *[]int) bool {
	return root(i, parents) == root(j, parents)
}

C++: #include<iostream>
#include<algorithm>
#define MAX_E 10000
#define MAX_N 100
using namespace std;
void init_union_find(int);
bool same(int ,int);
void unite(int,int);
struct edge{
  int u,v,cost;
  bool operator < (const edge& e) const{
    if(cost != e.cost) return cost < e.cost;
    else if(u != e.u) return u < e.u;
    else return v < e.v;
  }
};

ostream& operator << (ostream& os,const edge& e){
  os <<e.cost<<e.u<<e.v;
}

int E,n;
edge G[MAX_E];

int A[100][100];

int kruskal(){ 
  sort(G,G + E);
  init_union_find(n);
  int res =0;
  for(int i = 0; i < E; i++){
    edge e = G[i];
    if(!same(e.u,e.v)){
       res += e.cost;
       unite(e.u,e.v);
    }
  }

  return res;
}


int main(){
  cin >> n;
  for(int i = 0; i < n; i++){
    for(int j = 0; j < n; j++){
      cin >> A[i][j];
      if(A[i][j]==-1){
	continue;
      }
      G[E]=(edge){i,j,A[i][j]};
      E++;
    }
  }
  cout <<kruskal()<<endl;
}




int par[MAX_N] ,rank[MAX_N];

void init_union_find(int n){
for(int i=0;i<n;i++) {
    par[i]=i;
    rank[i]=0;
 }
}

int find(int x){
  if ( par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}

void unite(int x,int y){
  x = find(x);
  y = find(y);
  if(x == y) return;

  if(rank[x] < rank[y]){
    par[x] = y;
  }else {
    par[y] = x;
    if( rank[x] == rank[y]) rank[x]++;
  }
}
bool same(int x,int y){
  return find(x) == find(y);
}

C++: #include<iostream>
#include<algorithm>
#define MAXV 100
#define INF 2001
using namespace std;

int n, total = 0, mine, minj;
int mst[MAXV] = {}; //1 origin
int edge[MAXV][MAXV] = {}; //1 origin

void prim(void);
bool doesFinished(void);

int main() {
	int i, j;
	
	cin >> n;
	for (i = 1; i <= n; i++) {
		
		for (j = 1; j <= n; j++) {
			cin >> edge[i][j];
		}
	}

	mst[1] = 1;
	while (!doesFinished()) {
		prim();
	}

	cout << total << endl;

	return 0;
}

void prim(void) {
	int i, j;
	mine = INF;
	for (i = 1; i <= n; i++) {
		if (mst[i] == 0)continue;
		for (j = 1; j <= n; j++) {
			if (edge[i][j] == -1 || mst[j] == 1)continue;
			if (edge[i][j] < mine) {
				mine = edge[i][j];
				minj = j;
			}
		}
	}
	mst[minj] = 1;
	if(mine != INF)total += mine;
}

bool doesFinished(void) {
	for (int i = 1; i <= n; i++) {
		if (mst[i] == 0)return false;
	}
	return true;
}