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++: #define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<stack>
using namespace std;

#define REP(i,n) for(int i=0;(i)<(n);(i)++)
#define ll long long

static const int INFTY = (1 << 25);

int G[100][100], n;

int makeMST() {
	bool mask[100][100];//MAST????????¨???????????¨???????????¨?????????1
	REP(i, n) {
		REP(j, n) mask[i][j] = 0;
	}

	bool color[100];//MST?????\??£??????1
	REP(i, n) color[i] = 0;

	color[0] = 1;//?????????????????¨??????0?????\??????

	REP(k, n - 1) {
		int minv = INFTY;
		int mNodej = -1;
		int mNodei = -1;
		REP(i, n) {
			if (color[i] == 1) {
				REP(j, n) {
					if (G[i][j] != -1) {
						if (color[j] == 0) {
							if (G[i][j] < minv) {
								minv = G[i][j];
								mNodei = i;
								mNodej = j;
							}
						}
					}
				}
			}
		}
		mask[mNodei][mNodej] = 1;
		mask[mNodej][mNodei] = 1;
		color[mNodej] = 1;
	}

	REP(i, n) {
		REP(j, n) {
			if (mask[i][j] == 0) G[i][j] = -1;
		}
	}

	int ans = 0;
	REP(i, n) {
		REP(j, n) {
			if (G[i][j] != -1) ans += G[i][j];
		}
	}

	return ans / 2;
}

int main() {
	cin >> n;

	REP(i, n) {
		REP(j, n) G[i][j] = INFTY;
	}
	REP(i, n) {
		REP(j, n) cin >> G[i][j];
	}

	cout << makeMST() << endl;


	return 0;
}

C++: #include <iostream>
#include <algorithm>
#define N 100
#define INF 2000000000
#define WHITE 0
#define GRAY 1
#define BLACK 2
using namespace std;

int n;
int M[N][N],color[N],d[N],p[N];
   //M??????????????????color????¨??????¶???(0,1,2),d????????????????°???????????¨????
   //p???MST???????°???¨?????¨??????????????????????????????

int Prim() {
	for (int i = 0;i < n;i++) d[i] = INF;
	d[0] = 0;  //?????????????????????????????§???????????§????´???????????????????????????????
	/*p[0] = -1;  //?????????????????????????????????????????? (???????????£????????????)*/

	int u;
	while (true) {
		int min = INF;
		for (int i = 0;i < n;i++) {  //MST????§???????????????????????????????????±????
			if(color[i]!=BLACK&&min>d[i]){
				min = d[i];  //??´??°?????????????????°min?????°??????d[i]???????´??????????????????????...
				u = i;
			}
		}

	if (min == INF) break; //????????\?????????d[i]?????´??°??§???????????¨???????????¨

	color[u] = BLACK;

	for (int v = 0;v < n;v++) 
		if(color[v]!=BLACK&&M[u][v]!=-1)
			if (M[u][v] < d[v]) {
				d[v] = M[u][v];  //?????????????????????????±????
				/*p[v] = u;*/
				/*color[v] = GRAY;(????????????bcSSSP??§?????¢?´¢?????????????????????????????§?????????S)*/
			}

	}

	int sum = 0;
	for (int i = 0;i < n;i++) sum += d[i];  //d[i]???????¨????MST???????????????
	return sum;
		
}

int main() {
	cin >> n;
	for (int i = 0;i < n;i++)
		for (int j = 0;j < n;j++) {
			cin >> M[i][j];
			if (M[i][j] == -1) M[i][j] = INF;
		}

	cout<<Prim()<<endl;

}

C++: #define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;

//#define int long long
#define DBG 1
#define dump(o) if(DBG){cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) if(DBG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define each(it,c) for(auto it=(c).begin();it!=(c).end();it++)
#define all(c) c.begin(),c.end()
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);
const int W = 0;const int G = 1;const int B = 2;
int n; vector<vector<int>>M;
int prim() {
	int u, minv;
	vector<int>d(n, INF), p(n, -1), color(n, W);
	d[0] = 0;
	while (1) {
		minv = INF;
		u = -1;
		rep(i, 0, n) {
			if (minv > d[i] && color[i] != B) {
				minv = d[i];
				u = i;
			}
		}
		if (u == -1)break;
		color[u] = B;
		rep(v, 0, n) {
			if (color[v] != B&&M[u][v] < d[v]) {
				d[v] = M[u][v];
				p[v] = u;
			}
		}
	}
	int sum(0);
	rep(i, 0, n) {
		if (p[i] != -1)sum += M[i][p[i]];
	}
	return accumulate(d.begin(), d.end(),0);

}
signed main() {
	cin >> n; M = vector<vector<int>>(n, vector<int>(n,INF));
	rep(i, 0, n) {
		rep(j, 0, n) {
			int e; cin >> e;
			M[i][j] = (e == -1) ? INF : e;
		}
	}
	cout << prim() << endl;
	return 0;
}

C++: #include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
	int n;
	scanf("%d", &n);
	
	bool added[n];
	int a[n][n];
	for(int i = 0; i < n; i++){
		for(int j = 0; j < n; j++){
			int buf;
			scanf("%d", &buf);
			a[i][j] = buf==-1 ? INT_MAX : buf;
		}
		added[i] = false;
	}
	added[0] = true;
	
	long total = 0;
	for(;1;){
		bool end = true;
		int minCost = INT_MAX, pi, pj;
		for(int i = 0; i < n; i++){
			if(!added[i]){
				end = false;
				continue;
			}
			for(int j = 0; j < n; j++){
				if(minCost > a[i][j]){
					minCost = a[pi = i][pj = j];
				}
			}
		}
		if(end) break;
		added[pi] = added[pj] = true;
		a[pj][pi] = a[pi][pj] = INT_MAX;
		total += minCost;
	}
	
	printf("%ld", total);
	return 0;
}