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 <stdio.h>

#define N 2000
#define BLACK 0
#define WHITE 1
#define GRAY 2
#define inf 200000

int Map[N][N];
int n;
int color[N];
int d[N];
int p[N];


void init(){
	int i;
	for(i=0;i<n;i++){
		color[i]=WHITE;
		d[i] = inf;
	}
}

void prim(){
	init();
	int mincost;
	d[0] = 0;
	p[0] = -1;
	int i;
	int u;
	int v;
	while(1){
		mincost = inf;
		for(i=0;i<n;i++){
			if(color[i]!=BLACK && d[i] < mincost){
				mincost = d[i];
				u = i;
			}
		}

		if(mincost == inf){
			break;
		}
		color[u]=BLACK;
		for(v=0;v<n;v++){
			if(color[v] != BLACK && Map[u][v]!= -1){
				if(Map[u][v] < d[v]){
					d[v] = Map[u][v];
					color[v] = GRAY;
					p[v] = u;
				}
			}
		}
	}
}

int main(){
	scanf("%d",&n);
	int i,j;
	for(i=0;i<n;i++){
		for(j=0;j<n;j++){
			scanf("%d",&Map[i][j]);
		}
	}
	prim();
	int total = 0;
	for(i=0;i<n;i++){
		total += d[i];
	}
	printf("%d\n",total);
	return 0;
}

C++: #include <iostream>
#include <queue>
#include <map>
#include <functional>
#include <vector>
using namespace std;

#define V vector<vector<pair<int,int>>>

int main() {
	cin.tie(0); ios::sync_with_stdio(false);
	int n,Ans = 0; cin >> n;
	V v(2001);
	vector<int> f(n + 1,0);
	for (int i = 1; i <= n;i++) {
		for (int j = 1; j <= n; j++) {
			int a; cin >> a;
			if (a != -1) {
				v[a].push_back(make_pair(i, j));
			}
		}
	}

	for (int i = 0; i <= 2000;i++) {
		for (int j = 0; j < v[i].size(); j++) {
			int first = v[i][j].first, second = v[i][j].second;
			if (f[first] >= 2 || f[second] >= 2) continue;
			Ans += i;
			f[first]++; f[second]++;
		}
	}

	cout << Ans << endl;
}

C++: #define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <clocale>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

#define IOS ios::sync_with_stdio(false); cin.tie(0);
#define FOR(i, s, n) for(int i = (s), i##_len=(n); i < i##_len; ++i)
#define FORS(i, s, n) for(int i = (s), i##_len=(n); i <= i##_len; ++i)
#define VFOR(i, s, n) for(int i = (s); i < (n); ++i)
#define VFORS(i, s, n) for(int i = (s); i <= (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FORS(i, 0, n)
#define VREP(i, n) VFOR(i, 0, n)
#define VREPS(i, n) VFORS(i, 0, n)
#define RFOR(i, s, n) for(int i = (s), i##_len=(n); i >= i##_len; --i)
#define RFORS(i, s, n) for(int i = (s), i##_len=(n); i > i##_len; --i)
#define RREP(i, n) RFOR(i, n, 0)
#define RREPS(i, n) RFORS(i, n, 0)
#define ALL(v) (v).begin(), (v).end()
#define SORT(v) sort(ALL(v))
#define RSORT(v) sort(ALL(v), greater<decltype(v[0])>())
#define SZ(x) ((int)(x).size())
#define PB push_back
#define MP make_pair
#define MT make_tuple
#define BIT(n) (1LL<<(n))
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end())

using ll = long long;
using Pi_i = pair<int, int>;
using VB = vector<bool>;
using VC = vector<char>;
using VD = vector<double>;
using VI = vector<int>;
using VLL = vector<ll>;
using VS = vector<string>;
using VSH = vector<short>;

const int MOD = 1000000007;
const int INF = 1000000000;
const int NIL = -1;

template<class T, class S>
bool chmax(T &a, const S &b){
    if(a < b){
        a = b; return true;
    }
    return false;
}
template<class T, class S>
bool chmin(T &a, const S &b){
    if(b < a){
        a = b; return true;
    }
    return false;
}

/*　O(V log E) 
#include <algorithm>
#include <queue>
#include <vector>
#define FOR(i, s, n) for(int i = s, i##_len=(n); i < i##_len; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
const int INF = 1000000000;
using namespace std;
using ll = long long;
using Pi_i = pair<int, int>;
*/

struct edge{
    int to, cost;
    edge(int To, int Cost): to(To), cost(Cost){};
};


int V; //頂点数
vector<vector<edge>> G;
vector<int> d; //既に木に含まれる点からの距離

int prim(){
    int res = 0;
    //Pi_i first: 距離　second: 頂点
    priority_queue<Pi_i, vector<Pi_i>, greater<Pi_i>> que;
    fill(ALL(d), INF);
    d[0] = 0;
    que.push(MP(0, 0));

    while(!que.empty()){
        Pi_i p = que.top(); que.pop();
        int v = p.second;
        if(d[v] < p.first) continue;
        d[v] = 0;
        res += p.first;
        for(edge &e : G[v]){
            if(d[e.to] > e.cost){
                d[e.to] = e.cost;
                que.push(MP(d[e.to], e.to));
            }
        }
    }
    return res;
}

int main(){
    cin >> V;
    G.resize(V);
    d.resize(V);
    REP(i, V){
        REP(j, V){
            int cst; cin >> cst;
            if(cst != -1){
                G[i].PB(edge(j, cst));
                G[j].PB(edge(i, cst));
            }
        }
    }
    cout << prim() << endl;
    return 0;
}