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 <climits>
using namespace std;

int n;
int g[101][101];

int d[101];
char c[101];
int p[101];

void prim() {
    for (int i = 0; i < n; ++i) {
        d[i] = INT_MAX;
        c[i] = 'w';
    }
    c[0] = 'b';
    d[0] = 0;
    while (true) {
        int u, v;
        int mine = INT_MAX;
        for (int j = 0; j < n; ++j) {
            if (c[j] == 'b') {
                u = j;
                for (int k = 0; k < n; ++k) {
                    if(g[u][k] > -1 && g[u][k] <= d[k] && c[k] != 'b' && g[u][k] <= mine) {
                        d[k] = g[u][k];
                        mine = d[k];
                        v = k;
                    }
                }
            }
        }
        p[v] = u;
        c[v] = 'b';
        if(mine == INT_MAX) break;
    }
}

int main() {
    cin >> n;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            int w;
            cin >> w;
            g[i][j] = w;
        }
    }
    prim();
    int sum;
    for (int k = 0; k < n; ++k) {
        sum += d[k];
    }
    cout << sum << endl;


    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
constexpr int di[] = { 0, 1, 0, -1 }, dj[] = { 1, 0, -1, 0 };
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define SORT(v) sort((v).begin(), (v).end())
#define SORTR(v) sort((v).rbegin(), (v).rend())
#define all(v) (v).begin(), (v).end()
#define MAX 100
#define vmax 10000
#define sentinel 2000000000
#define INF (1<<30)
constexpr long long int inf = 1000000000000;
constexpr long long int rinf = -(INT64_MAX);
static const int NIL=-1;

//cout<<fixed<<setprecision(20);cin.tie(0);ios::sync_with_stdio(false);

struct edge
{
    int to,cost;
};


int V;                //Vは頂点数
bool used[MAX+1];         //頂点iがxに含まれているか
vector<edge> g[MAX+1];


int prim(int s){
    for(int i=1;i<=V;i++){
        used[i]=false;
    }
    int res=0;
    priority_queue<P,vector<P>,greater<P>> que;
    que.push(P(0,s));
    while(!que.empty()){
        P p=que.top();que.pop();
        int v=p.second;
        if(used[v]) continue;
        res+=p.first;
        used[v]=true;
        for(auto e:g[v]){
            que.push(P(e.cost,e.to));
        }
    }
    return res;
}

int main() {
    int x;
    cin>>V;
    for(int i=1;i<=V;i++){
        for(int j=1;j<=V;j++){
            cin>>x;
            if(x!=-1){
                g[i].push_back({j,x});
            }
        }
    }
    cout<<prim(1)<<endl;
    return 0;
}

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

#define rep(i, n) REP(i, 0, n)
#define REP(i, k, n) for(auto i = k; i != n; i++)
#define rrep(i, n) RREP(i, n, 0)
#define RREP(i, n, k) for(auto i = n; i != k; i--)
#define all(x, r) (x), (x)+(r)
#define ALL(x) (x).begin(), (x).end()
#define debug(x) cerr << #x << " " << x << endl
#define exst(x, data) (data).find(x) != (data).end()
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
#define np next_permutation
#define b32 bitset<32>

using ll = long long;
using Pii = pair<int,int>;
using Tiii = tuple<int, int, int>;
template<class T>using V = vector<T>;

// class Kruskal {
// private:
//
//
// public:
//     int node_n;
//     vector<vector<int>> cost;
//
//
//     Kruskal(int n) {
//         node_n = n;
//     }
//
// };

const int MAX_V = 101;
int cost[MAX_V][MAX_V];
int mincost[MAX_V];
bool used[MAX_V];
int node_n;

int prim() {
    for(int i=0; i<node_n; i++) {
        mincost[i] = INT_MAX;
        used[i] = false;
    }
    mincost[0] = 0;
    int res = 0;

    while(true) {
        int v = -1;

        for(int u=0; u<node_n; u++) {
            if (!used[u] && (v == -1 || mincost[u] < mincost[v]))
                v = u;
        }
        if (v == -1) break;
        used[v] = true;
        res += mincost[v];

        for(int u=0; u<node_n; u++) {
            mincost[u] = min(mincost[u], cost[v][u]);
        }
    }
    return res;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n; cin >> n; node_n = n;
    rep(i,n) rep(j,n) cost[i][j] = INT_MAX;
    rep(i,n) rep(j,n) {
        int c;
        cin >> c;
        if(c != -1) cost[i][j] = c;
    }

    cout << prim() << endl;

    // rep(i,n) {rep(j,n) cout << cost[i][j] << " "; cout << endl;}
    return 0;
}

C++: //Kruskal's algorithm
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

struct E
{
    int weight;
    int begin;
    int end;
};

struct Unionfind
{
    vector<int> parents;

    Unionfind(int n) : parents(n)
    {
        for (int i = 0; i < n; i++)
        {
            parents[i] = i;
        }
    }

    int root(int x)
    {
        if (parents[x] == x)
        {
            return x;
        }
        return parents[x] = root(parents[x]);
    }

    void unite(int x, int y)
    {
        int par_x = root(x);
        int par_y = root(y);
        if (par_x == par_y)
        {
            return;
        }
        parents[par_x] = par_y;
    }

    bool same(int x, int y)
    {
        int par_x = root(x);
        int par_y = root(y);
        return par_x == par_y;
    }
};

int main()
{
    int n;
    cin >> n;
    struct Unionfind tree(n);

    vector<E> e(n * (n - 1) / 2);
    int index = 0;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            int temp;
            cin >> temp;
            if (i < j)
            {
                e[index].weight = temp;
                e[index].begin = i;
                e[index].end = j;
                index++;
            }
        }
    }

    sort(e.begin(), e.end(),
         [](const E &l, const E &r) {
             return l.weight < r.weight;
         });

    int ans = 0;
    for (int i = 0; i < e.size(); i++)
    {
        if (e[i].weight == -1)
        {
            continue;
        }

        if (tree.same(e[i].begin, e[i].end))
        {
            continue;
        }
        else
        {
            tree.unite(e[i].begin, e[i].end);
            ans += e[i].weight;
        }
    }

    cout << ans << endl;
}

/*

<INPUT>
5
 -1 2 3 1 -1
 2 -1 -1 4 -1
 3 -1 -1 1 1
 1 4 1 -1 3
 -1 -1 1 3 -1

<OUTPUT>
5

*/