Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)

	g := make([][]int, n)
	ans := make([][]int, n)

	inf := 1 << 30
	for i := 0; i < n; i++ {
		g[i] = make([]int, n)
		ans[i] = make([]int, n)
		for ii := 0; ii < n; ii++ {
			g[i][ii] = getNextInt(scanner)
			if g[i][ii] == -1 {
				g[i][ii] = inf
			}
			ans[i][ii] = -1
		}
	}

	im := make(IntMap, 1)

	im.increment(1)

	for len(im) < n {
		min := inf
		u := -1
		v := -1
		for i := range im {
			for ii := 0; ii < n; ii++ {
				if im.get(ii) != 0 {
					continue
				}
				if min > g[i][ii] {
					min = g[i][ii]
					u = i
					v = ii
				}
			}
		}
		im.increment(v)
		ans[u][v] = min
		ans[v][u] = min
	}

	sum := 0
	for i := 0; i < n-1; i++ {
		for ii := i + 1; ii < n; ii++ {
			if ans[i][ii] != -1 {
				sum += ans[i][ii]
			}
		}
	}

	fmt.Fprintln(writer, sum)
	writer.Flush()
}

// IntMap ...
type IntMap map[int]int

func (m IntMap) get(key int) int {
	if _, ok := m[key]; ok {
		return m[key]
	}
	return 0
}
func (m IntMap) increment(key int) {
	if _, ok := m[key]; ok == false {
		m[key] = 0
	}
	m[key]++
}

C++: #include<iostream>
#include<iomanip>
#include<algorithm>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<numeric>
#include<stdio.h>

using std::cout;
using std::cin;
using std::setw;
using std::setfill;
using std::string;
using std::pair;
using std::make_pair;
using std::vector;
using std::queue;
using std::deque;
using std::priority_queue;
using std::next_permutation;
using std::iota;
using std::sort;
using std::greater;
using std::max;
using std::min;

#define INF 999999999
#define MOD 1000000007

#define EVEL 1

#ifndef EVEL
#define dbug(X) std::cout << #X << ":" <<X<<" " ;
#define dbugf(s) std::cout << s << " ";
#define dbugln std::cout<<"\n";
#else
#define dbug(X) {}
#define dbugf(s) {}
#define dbugln {}
#endif

/*const int M = 10000;

class Matrix{
	public:
	int n;//行
	int m;//列
	vector<int> data;
	Matrix(int a, int b, const vector<int> &v){
		n = a;
		m = b;
		data.resize(n*m);
		for(int i = 0; i < n*m; i++){
			data[i] = v[i];
		}
	}

	int show(){
		cout << this->n << "x" << this->m << "Matrix\n"; 
		for(int i = 0; i < this->n; i++){
			for(int j = 0; j < this->m; j++){
				cout << data[i * this->m + j];
				if(j == this->m-1) cout << "\n";
				else cout << " ";
			}
		}
		return 0;
	}
	Matrix operator * (Matrix obj){
		vector<int> rt;
		int a=0;
		for(int i = 0; i < this->n; i++){
			for(int j = 0; j < obj.m; j++){

				for(int k = 0; k < this->m; k++){
					//a += this->data[i * this->m + k] * obj.data[k * obj.m + j];
					a = (a%M + ((this->data[i * this->m + k]%M)*(obj.data[k * obj.m + j]%M))%M)%M;
				}
				rt.push_back(a);
				a = 0;

			}
		}
		Matrix RT(this->n, obj.m, rt);
		return RT;
	}

	Matrix expt(long long int p){
		if(p == 0){
			vector<int> v{1, 0, 0, 1};
			Matrix RT(2, 2, v);
			return RT;
		}else if(p == 1) return *this;
		else if(p%2 == 1){
			Matrix T = this->expt(p-1);
			return *this * T;
		}else{
			Matrix T = this->expt(p/2);
			return T*T;
		}
	}
};*/

class UF{
	vector<int> oya;

	public:
	UF(int s){
		oya.resize(s);
		for(int i = 0; i < s; i++) oya[i] = i;
	}

	int root(int x){
		vector<int> path;
		while(oya[x] != x){
			path.push_back(x);
			x = oya[x];
		}
		for(int i = 0; i < path.size(); i++){
			oya[ path[i] ] = x;
		}
		return x;
	}

	void unite(int x, int y){
		//x <= s, y <= s が保証されているものとする
		int ox = root(x), oy = root(y);
		oya[oy] = ox;
	}

	bool same(int x, int y){
		int ox = root(x), oy = root(y);
		return (ox == oy);
	}
};

int n, vsum=0;

struct path{
	int value;
	int start;
	int end;
};

vector<path> ps;

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

	cin >> n;
	for(int i = 0; i < n; i++){
		for(int j = 0; j < n; j++){
			int a;
			cin >> a;
			if(a >= 0 && i < j){
				path p;
				p.value = a;
				p.start = i;
				p.end = j;
				ps.push_back(p);
			}
		}
	}

	UF uf(n);
	sort(ps.begin(), ps.end(), [](path a, path b){return (a.value < b.value);});
	
	for(auto itr = ps.begin(); itr != ps.end(); itr++){
		if(!uf.same(itr->start, itr->end)){
			uf.unite(itr->start, itr->end);
			vsum+=itr->value;
		}
		dbug(vsum)dbug(itr->value)dbug(itr->start)dbug(itr->end)dbugln;
	}

	cout << vsum << "\n";


	return 0;
}

C++: //compiledef
#include <bits/stdc++.h>
#define rep(i, n)for(i=0;i<(n);i++)
#define per(i, n) for(i=(n);i>0;i--)
#define repx(i, n, x) for(i=(x);i<(n);i++)
#define xper(i, n, x) for(i=(n);i>(x);i--)
#define pback push_back

//c++def
using namespace std;
typedef long long int ll;
const long long int MOD = 1e9+7;
const long long int MOD2 = 998244353;
int w[200][200];
int prim_mst(int n){
	int i;
	int j;
	int cost = 0;
	int d[200];
	int pi[200];
	int color[200];
	rep(i, n){
		d[i]= 2e9;
		pi[i]=-1;
		color[i] = 0;
	}
	d[0] = 0;
	while(29091101){
		int mincost = 2e9;
		rep(i, n){
			if(color[i] != 2 && d[i] < mincost){
				mincost = d[i];
				j = i;
			}
		}
		if(mincost == 2e9)break;
		color[j] = 2;
		for(int v=0; v<n;v++){
			if(color[v] != 2 && w[j][v] < d[v]){
				pi[v] = j;
				d[v] = w[j][v];
			}
		}
	}
	rep(i, n){
		if(pi[i] != -1) cost += w[i][pi[i]];
	}
	return cost;
}

int main(){
	int n;
	int i, j;
	int tmp;
	cin >> n;
	rep(i, n){
		rep(j, n){
			cin >> tmp;
			if(tmp == -1) w[i][j] = 2e9;
			else w[i][j] = tmp;
		}
	}
	cout << prim_mst(n) << endl;
}

C++: //kruskal tree
#include <iostream>
#include <algorithm>
using namespace std;
#define M 999
int parent[M],a[M],b[M];
pair<int,int>node[M];
int root(int a){return parent[a]==a?a:parent[a]=root(parent[a]);}
int unite(int a,int b){
	int x=root(a),y=root(b);
	if(x==y)return 0;
	parent[x]=y;
	return 1;
}
int main(){
	int i,j,s,n,m,x;
	cin>>n;
		for(m=i=0;i<n;i++)for(j=0;j<n;j++)if(cin>>x,~x)a[m]=i,b[m]=j,node[m].first=x,node[m].second=m++;
		sort(node,node+m);
		for(i=0;i<n;i++)parent[i]=i;
		for(s=i=0;i<m;i++)if(unite(a[node[i].second],b[node[i].second]))s+=node[i].first;
	cout<<s<<endl;
}