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 <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int main() {
    int n;
    cin >> n;
    vector<vector<int>> edges(n, vector<int>(n));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int a;
            cin >> a;
            edges[i][j] = a;
        }
    }

    vector<bool> done(n, false);
    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
    q.push(make_pair(0, 0));
    int ans = 0;
    while (!q.empty()) {
        int d = q.top().first;
        int v = q.top().second;
        q.pop();
        if (done[v]) {
            continue;
        }
        done[v] = true;
        ans += d;
        for (int i = 0; i < n; i++) {
            if (edges[v][i] < 0) {
                continue;
            }
            q.push(make_pair(edges[v][i], i));
        }
    }
    cout << ans << endl;
}

C++: #include<iostream>
#include<vector>
#include<queue>
#include<utility>
#include<algorithm>
#define fi first
#define se second

using namespace std;

typedef vector<int> vi;
typedef vector< vector<int> > vvi;
typedef pair< int,pair<int,int> > pp;

vi P;
vector<pp> d;

inline void init()
{
  int n;
  vvi a;
  cin >> n;
  P.resize(n);
  for(int i = 0; i < n; ++i)
    P[i] = i;
  a.resize(n);
  for(int i = 0; i < n; ++i){
    a[i].resize(n);
    for(int j = 0; j < n; ++j)
      cin >> a[i][j];
  }
  for(int i = 0; i < n; ++i)
    for(int j = i + 1; j < n; ++j)
      if(a[i][j] != -1){
	d.push_back(make_pair(a[i][j],make_pair(i,j)));
      }
  sort(d.begin(),d.end());
}

inline int root(int a)
{
  if(P[a] == a) return a;
  else return (P[a] = root(P[a]));
}

inline bool same(int a, int b)
{
  return root(a) != root(b);
}

inline void unite(int a, int b)
{
  P[root(a)] = root(b);
}

inline int answer()
{
  int answer = 0;
  for(size_t i = 0; i < d.size(); ++i){
    if(same(d[i].se.fi,d[i].se.se)){
      unite(d[i].se.fi,d[i].se.se);
	answer += d[i].fi;
    }
  }
  return answer;
}

int main()
{
  init();
  cout << answer() << endl;
  return 0;
}

C++: #include<iostream>

using namespace std;

static const int MAX = 100;
static const int NIL = (1 << 21);
static const int WHITE = 0;
static const int GREY = 1;
static const int 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] = INFY;
    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; i < n; i++) {
      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;
      M[i][j] = (e == -1) ? INFTY : e;
    }
  }

  cout << prim() << endl;

  return 0;
}

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

struct edge {int u, v, cost;};

int n;
vector<edge> G;

bool comp(const edge& e1, const edge& e2) {
    return e1.cost < e2.cost;
}

class UnionFind {
 public:
    int g_num;
    vector<int> rank, par;

    UnionFind() {}
    UnionFind(int size) {
        rank.resize(size, 0);
        par.resize(size, 0);
        for (int i = 0; i < size; i++) {
            par[i] = i;
            rank[i] = 0;
        }
        g_num = size;
    }

    bool same(int x, int y) {
        return find(x) == find(y);
    }

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

        g_num--;
        if (rank[x] > rank[y]) {
            par[y] = x;
        } else {
            par[x] = y;
            if (rank[x] == rank[y]) rank[y]++;
        }
    }

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

    int group_num() {
        return g_num;
    }
};

int kruscal() {
    sort(G.begin(), G.end(), comp);
    UnionFind uf(n);
    int res = 0;
    for (auto e: G) {
        if (!uf.same(e.u, e.v)) {
            uf.unite(e.u, e.v);
            res += e.cost;
        }
    }
    return res;
}

int main(void) {
    cin >> n;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int cost;
            cin >> cost;
            if (cost == -1) continue;
            G.push_back(edge {i, j, cost});
        }
    }
    int ans = prim();
    cout << ans << endl;
    return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
using ll = long long;

struct edge
{
    int u, v, cost;
};

bool comp(const edge &e1, const edge &e2)
{
    return e1.cost < e2.cost;
}

int n, E, A[100][100];
int p[10010];
vector<edge> es(0);

void init(int n)
{
    rep(i, n) p[i] = i;
}

int root(int a)
{
    if (p[a] == a)
        return a;
    return (p[a] = root(p[a]));
}

bool is_same_set(int a, int b)
{
    return root(a) == root(b);
}

void unite(int a, int b)
{
    p[root(a)] = root(b);
}

int kruskal()
{
    int sum = 0;
    E = 0;
    rep(i, n) rep(j, i)
    {
        if (A[i][j] != -1)
        {
            E++;
            edge e;
            e.u = i;
            e.v = j;
            e.cost = A[i][j];
            es.push_back(e);
        }
    }
    sort(es.begin(), es.end(), comp);
    rep(i, E)
    {
        edge e = es[i];
        if (!is_same_set(e.u, e.v))
        {
            unite(e.u, e.v);
            sum += e.cost;
        }
    }
    return sum;
}

int main()
{
    cin >> n;
    rep(i, n) rep(j, n) cin >> A[i][j];
    init(n);
    int ans = kruskal();
    cout << ans << endl;
    return 0;
}