Go: package main

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

const BIG_COST = 3000

const (
	ST_NOT_YET = iota
	ST_JOINED
)

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nNodes := scanInt(sc)
	costs := make([][]int, nNodes)

	for r := 0; r < nNodes; r++ {
		costs[r] = make([]int, nNodes)
		for c := 0; c < nNodes; c++ {
			costs[r][c] = scanInt(sc)
		}
	}

	states := make([]int, nNodes)
	minCosts := make([]int, nNodes)

	for i := 1; i < nNodes; i++ {
		minCosts[i] = BIG_COST
	}

	for {
		var u int
		minCost := BIG_COST

		for i := 0; i < nNodes; i++ {
			if states[i] != ST_JOINED && minCosts[i] < minCost {
				minCost = minCosts[i]
				u = i
			}
		}

		// 全てのノードが MST に JOIN できたので終了。
		if minCost == BIG_COST {
			break
		}

		states[u] = ST_JOINED

		for v := 0; v < nNodes; v++ {
			cost := costs[u][v]

			// MST内の u と MST外の v との辺の重みをそれぞれ minCosts[v] として
			// 保存しておく。このうち辺の重みが最小となる v が次のループで MST に JOIN する。
			if states[v] != ST_JOINED && cost != -1 && cost < minCosts[v] {
				minCosts[v] = cost
			}
		}
	}

	sum := 0
	for _, cost := range minCosts {
		sum += cost
	}
	fmt.Println(sum)
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

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)