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
	num := 0
	for len(edges) > 0 && num < n-1 {
		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
		num += 1
	}
	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>
using namespace std;
#define MAX 100
#define INFTY 1<<21
#define WHITE 0
#define GRAY 1
#define BLACK 2

int n, M[MAX][MAX];

int prim()
{
    int u,minv;
    int d[MAX],p[MAX],color[MAX];

    for(int i=0;i<n;i++)
    {
        d[i]=INFTY;
        p[i]=-1;
        color[i]=WHITE;
    }

    d[0]=0;
    while(1)
    {
        minv=INFTY;
        u=-1;
        for(int i=0;i<n;i++)
        {
            if(minv>d[i] && color[i]!=BLACK)
            {
                u=i;
                minv=d[i];
            }
        }
        if(u==-1)break;

        color[u]=BLACK;
        for(int v=0;v<n;v++)
        {
            if(color[v]!=BLACK && M[u][v]!=INFTY)
            {
                if(d[v]>M[u][v])
                {
                    d[v]=M[u][v];
                    p[v]=u;
                    color[v]=GRAY;
                }
            }
        }
    }
    int sum=0;
    for(int i=0;i<n;i++)
    {
        if(p[i]!=-1)sum+=M[i][p[i]];
    }
    return sum;
}

int main()
{
    cin >> n;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            int e;
            cin >>e;
            if(e==-1)
            {
                M[i][j]=INFTY;
            }
            else
            {
                M[i][j]=e;
            }
        }
    }

    cout << prim() << 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,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)