Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const (
	MAX   = 100
	INFTY = (1 << 21)
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

//V: グラフの頂点全体の集合
//T: MSTに属する頂点の集合
var (
	n     int
	M     [MAX][MAX]int //重みを記録する隣接行列
	color [MAX]int
	p     [MAX]int //MSTにおける頂点の親を記録
	d     [MAX]int //Tに属する頂点と、V−Tに属する頂点を繋ぐ辺の中で、重みが最小のものを記録
)

func prim() int {
	var u, minv int
	//初期化
	for i := 0; i < n; i++ {
		d[i] = INFTY
		p[i] = -1
		color[i] = WHITE
	}

	d[0] = 0

	for {
		//頂点uを決めていく
		//つまり、Tに属する頂点と、V−Tに属する頂点を繋ぐ辺の中で、重みが最小のものを決める
		minv = INFTY
		u = -1
		for i := 0; i < n; i++ {
			//未訪問かつ、辺の重みが最小となるuを選ぶ
			if minv > d[i] && color[i] != BLACK {
				u = i
				minv = d[i]
			}
		}

		if u == -1 {
			break
		}

		color[u] = BLACK
		for 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
				}
			}
		}
	}

	sum := 0
	for i := 0; i < n; i++ {
		if p[i] != -1 {
			sum += M[i][p[i]]
		}
	}
	return sum
}

func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()

	for i := 0; i < n; i++ {

		for j := 0; j < n; j++ {
			tmp := nextInt()
			if tmp == -1 {
				tmp = INFTY
			}
			M[i][j] = tmp
		}

	}

	fmt.Println(prim())

}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <cmath>
#include <deque>
#include <iterator>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <utility>
using namespace std;

using ll=long long;
template<class T> using V = vector<T>;
template<class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define rep(i, k, n) for (ll i=k; i<(ll)n; ++i)
#define REP(i, n) rep(i, 0, n)
template<class T> inline bool chmax(T& a, T b) {if (a<b) {a=b; return true;} return false;}
template<class T> inline bool chmin(T& a, T b) {if (a>b) {a=b; return true;} return false;}

vll parent(100);
vll size(100);

ll root(ll i) {
  if (parent.at(i) != i) parent.at(i) = root(parent.at(i));
  return parent.at(i);
}

bool same(ll i, ll j) {
  return root(i) == root(j);
}

void unite(ll i, ll j) {
  ll pi = root(i), pj = root(j);
  if (pi == pj) return;
  if (size.at(pi) < size.at(pj)) parent.at(pi) = pj;
  else if (size.at(pi) > size.at(pj)) parent.at(pj) = pi;
  else {
    if (pi < pj) {
      parent.at(pj) = pi; size.at(pi)++;
    } else {
      parent.at(pi) = pj; size.at(pj)++;
    }
  }
}

int main() {
  ll n; cin >> n;
  REP(i, n) parent.at(i) = i;
  priority_queue<vll, vvll, greater<vll> > pq;
  REP(i, n) REP(j, n) {
    ll c; cin >> c;
    if (i >= j) continue;
    if (c != -1) {
      vll p; p.push_back(c); p.push_back(i); p.push_back(j);
      pq.push(p);
    }
  }
  ll ans = 0;
  while (!pq.empty()) {
    vll q = pq.top(); pq.pop();
    if (root(q.at(1)) == root(q.at(2))) continue;
    else {
      ans += q.at(0);
      unite(q.at(1), q.at(2));
    }
  }
  cout << ans << endl;
  return 0;
}

C++: #include <algorithm>
#include <iostream>
#include <set>
#include <vector>
#define FOR(i,k,n) for (int (i)=(k); (i)<(n); ++(i))
#define REP(i,n) FOR(i,0,n)
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
#define all(v) v.begin(), v.end()
using namespace std;
typedef pair<int,int> PII;

int P[100]; //union_find

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

bool same(int x, int y){
    return root(P[x]) == root(P[y]);
}

void unite(int x, int y){
    P[root(x)] = root(y);
}

int main(){
    int n;
    cin >> n;
    int g[n][n];
    vector<pair<int,PII> > V;
    REP(i,n){
        REP(j,n){
            cin >> g[i][j];
            if(i<j && g[i][j] >= 0) V.pb(mp(g[i][j],mp(i,j)));
        }
    }
    sort(all(V));
    REP(i,n){
        P[i] = i;
    }
    int wsum = 0, count = 0;
    REP(i,n*n){
        int x = V[i].snd.fst;
        int y = V[i].snd.snd;
        if(same(x,y)) continue;
        unite(x,y);
        wsum += V[i].fst;
        count++;
        if(count == n-1) break;
    }

    cout <<wsum<<endl;

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define int long long   // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;

class UnionFind {
private:
    int n;
    vector<int> uni;
public:
    UnionFind(int _n) {
        n = _n;
        uni.clear();
        uni.resize(n, -1);
    }
    int root(int x) {
        if (uni[x] < 0) return x;
        return uni[x] = root(uni[x]);
    }
    bool same(int x, int y) {
        return root(x) == root(y);
    }
    void unite(int x, int y) {
        x = root(x);
        y = root(y);
        if (x == y) return;
        if (uni[x] > uni[y]) swap(x, y);
        uni[x] += uni[y];
        uni[y] = x;
    }
    void print() {
        for (auto x : uni) cout << x << " ";
        cout << endl;
    }
};

struct edge {
    int from, to, cost;
    edge(){}
    edge(int _from, int _to, int _cost) : from(_from), to(_to), cost(_cost) {}
    bool operator< (const edge& e) const {
        return cost == e.cost ? (from == e.from ? to < e.to : from < e.from) : cost < e.cost;
    }
};

class Kruskal {
private:
    int n;
    vector<edge> edges;
    UnionFind uf;
public:
    Kruskal(int _n) : n(_n), uf(UnionFind(n)) {
        rep(i, n) {
            rep(j, n) {
                int c;
                cin >> c;
                if (c == -1) c = 1e9;
                edges.emplace_back(i, j, c);
            }
        }
        sort(all(edges));
    }
    int calc() {
        int res = 0;
        for (auto &e : edges) {
            if (!uf.same(e.from, e.to)) {
                uf.unite(e.from, e.to);
                res += e.cost;
            }
        }
        return res;
    }
};

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

    int n;
    cin >> n;
    Kruskal kr(n);
    cout << kr.calc() << endl;
}

C++: #include "bits/stdc++.h"
using namespace std;
static const int MAX=100;
static const int INFTY=(1<<21);
static const int WHITE=0;
static const int GRAY=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]=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;
			M[i][j]=(e==-1)?INFTY:e;
		}
	}
	cout<<prim()<<endl;
	return 0;
}