Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)

	g := make([][]int, n)
	ans := make([][]int, n)

	inf := 1 << 30
	for i := 0; i < n; i++ {
		g[i] = make([]int, n)
		ans[i] = make([]int, n)
		for ii := 0; ii < n; ii++ {
			g[i][ii] = getNextInt(scanner)
			if g[i][ii] == -1 {
				g[i][ii] = inf
			}
			ans[i][ii] = -1
		}
	}

	im := make(IntMap, 1)

	im.increment(1)

	for len(im) < n {
		min := inf
		u := -1
		v := -1
		for i := range im {
			for ii := 0; ii < n; ii++ {
				if im.get(ii) != 0 {
					continue
				}
				if min > g[i][ii] {
					min = g[i][ii]
					u = i
					v = ii
				}
			}
		}
		im.increment(v)
		ans[u][v] = min
		ans[v][u] = min
	}

	sum := 0
	for i := 0; i < n-1; i++ {
		for ii := i + 1; ii < n; ii++ {
			if ans[i][ii] != -1 {
				sum += ans[i][ii]
			}
		}
	}

	fmt.Fprintln(writer, sum)
	writer.Flush()
}

// IntMap ...
type IntMap map[int]int

func (m IntMap) get(key int) int {
	if _, ok := m[key]; ok {
		return m[key]
	}
	return 0
}
func (m IntMap) increment(key int) {
	if _, ok := m[key]; ok == false {
		m[key] = 0
	}
	m[key]++
}

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,size;
int A[MAX];
int B[MAX];
int W[MAX];
int V[MAX];
pair<int,int> edge[MAX];
int main(){
  size = 0;
  cin >> n;
  rep(i,n) 
    rep(j,n) {
      cin >> W[size];
      if (i <= j) {        
        A[size] = i;
        B[size] = j;
        edge[size].first = W[size];
        edge[size].second = size;
        size++;
      }
    }

  // sort by lenght of edge
  sort(edge, edge+size+1);
  // printing
  // rep(i,size+1) {
  //   cout << A[edge[i].second] << " " << B[edge[i].second]
  //        << " " << edge[i].first << endl;;
  // }
  int sum = 0;
  rep(i,n) V[i] = i;
  rep(i,size) {
    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;
}

C++: #include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

#define MIN(a,b) ((a)<(b)?(a):(b))
#define MAX(a,b) ((a)>(b)?(a):(b))

struct NODE {
    int number;
    NODE* root;
};

struct EDGE {
    int n1;
    int n2;
    int w;
};

int findRoot(NODE& n) {
    NODE temp_node=n;
    while(temp_node.root!=nullptr) {
        temp_node=*(temp_node.root);
    }
    return temp_node.number;
}

int main()
{
    int n;
    int rt1,rt2;
    int i,j;
    int w;
    EDGE temp_e;
    NODE temp_node;
    vector<EDGE> e;
    vector<NODE> node;

    scanf("%d",&n);
    for(i=0; i<n; ++i) {
        temp_node.number=i;
        temp_node.root=nullptr;
        node.push_back(temp_node);
        for(j=0;j<n;++j) {
            scanf("%d",&w);
            if(w!=-1 && i<j) {
                temp_e.n1=i;
                temp_e.n2=j;
                temp_e.w=w;
                e.push_back(temp_e);
            }
        }
    }
    sort(e.begin(),e.end(),[](EDGE& lhs,EDGE& rhs) { return (lhs.w==rhs.w)?(lhs.n1<rhs.n1):(lhs.w<rhs.w); });
    w=0;
    for(auto itr=e.begin(); itr!=e.end(); ++itr) {
        rt1=findRoot(node[itr->n1]);
        rt2=findRoot(node[itr->n2]);
        if(rt1!=rt2) {
            w+=itr->w;
            node[MAX(rt1,rt2)].root = &node[MIN(rt1,rt2)];
        }
    }
    printf("%d\n",w);
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <queue>

#define INPUT_FROM_FILE (0) 
#if INPUT_FROM_FILE
#include <fstream>
#endif

class UnionFindTree{
public:
    std::vector<int> parent;
    std::vector<int> rank;
 
    UnionFindTree(int n){
        parent.resize(n, 0);
        rank.resize(n, 0);
 
        for(int i = 0; i < n; i++){
            parent[i] = i;
        }
        return;
    }
    ~UnionFindTree(){
        return;
    }
 
    int find(int x){
        if(parent[x] == x){
            return x;
        }
        else{
            return parent[x] = find(parent[x]);
        }
    }
 
    void unite(int x, int y){
        x = find(x);
        y = find(y);
 
        if(x == y){
            return;
        }
 
        if(rank[x] < rank[y]){
            parent[x] = y;
        }
        else{
            parent[y] = x;
            if(rank[x] == rank[y]){
                rank[x]++;
            }
        }
    }
 
    bool is_same(int x, int y){
        return find(x) == find(y);
    }
};

class Edge{
public:
	int u;
	int v;
	int cost;

	Edge(){
		return;
	}
	
	~Edge(){
		return;
	}
	bool operator < (const Edge &r){
		return this->cost < r.cost;
	}
};


int main(int argc, char **argv){
	int n;
	std::vector<int> A;
	std::vector<Edge> edges;

#if INPUT_FROM_FILE
	std::ifstream ifs("test.txt");
	//FILE *fp = fopen("test.txt", "r");
#endif

#if INPUT_FROM_FILE
	ifs >> n;
#else
	std::cin >> n;
#endif

	A.resize(n * n);
	for(int i = 0; i < n; i++){
		int offset = i * n;
		for(int j = 0; j < n; j++){
#if INPUT_FROM_FILE
			ifs >> A[offset + j];
#else
			std::cin >> A[offset + j];
#endif
		}
	}

	for(int i = 0; i < n; i++){
		int offset = i * n;
		for(int j = i + 1; j < n; j++){
			if(A[offset + j] >= 0){
				Edge e;
				e.u = i;
				e.v = j;
				e.cost = A[offset + j];

				edges.push_back(e);
			}
		}
	}

	std::sort(edges.begin(), edges.end());
   UnionFindTree uft(n);
   int result = 0;
 
    for(int i = 0; i < edges.size(); i++){
        Edge &e = edges[i];
        if(!uft.is_same(e.u, e.v)){
            uft.unite(e.u, e.v);
            result += e.cost;
        }
    }
 
    std::cout << result << std::endl;
	return 0;
}

C++: import heapq

n = int(input())
#A = [[int(i) for i in input().split()] for i in range(n)]
h = []
DP = {}
heapq.heappush(h,(0,0))
c = 0
while(len(DP)<n):
    tmp = heapq.heappop(h)
    if tmp[1] in DP:
        continue
    c += tmp[0]
    DP[tmp[1]] = 1
    for i,j in enumerate(A[tmp[1]]):
        if j > 0 and not i in DP:
            heapq.heappush(h,(j,i))
print(c)