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 <vector>
#include <string>
#include <algorithm>
#include <functional>
#include <tuple>
#include <sstream>

struct Node
{
  int data;
  Node* parent;
  int rank;
  Node() : data(-1), parent(this), rank(0){}
  Node& operator=(int data){ this->data = data; return *this; }
  Node* Find(){
    if (this == this->parent){
      return this;
    } else {
      auto root = this->parent->Find();
      this->parent = root;
      return root;
    }
  }
  static void Union(Node& lhs, Node& rhs){
    Node* lhs_root = lhs.Find();
    Node* rhs_root = rhs.Find();
    if (lhs_root->rank > rhs_root->rank){
      rhs_root->parent = lhs_root;
    } else if (rhs_root->rank > lhs_root->rank){
      lhs_root->parent = rhs_root;
    } else if (rhs_root != lhs_root){
      lhs_root->parent = rhs_root;
      rhs_root->rank += 1;
    }
    return;
  }
};

bool is_not_space(int c){ return c != ' '; }
std::string& ltrim(std::string& str){
  str.erase(str.begin(), std::find_if(str.begin(), str.end(), is_not_space));
  return str;
}
std::string& rtrim(std::string& str){
  str.erase(std::find_if(str.rbegin(), str.rend(), is_not_space).base(), str.end());
  return str;
}
std::string trim(std::string& str){ return ltrim(rtrim(str)); }

struct Edge{
  int i,j,w;
  bool exist;
  Edge(int i,int j,int w): i(i), j(j), w(w), exist(true){ if(w==-1){ this->exist = false; } }
  int Value(){ return this->exist ? this->w : 2005; }
};
int main(void){
  std::string line;
  std::getline(std::cin, line);
  int n = std::stoi(line);
  std::vector<Node> V(n);
  std::vector<Edge> E;
  for(int i = 0; i < n; ++i){
    std::getline(std::cin, line);
    std::stringstream ss(trim(line));
    for(int j = 0; j < n; ++j){
      int w;
      ss >> w;
      E.emplace_back(i,j,w);
    }
  }
  int sum_w = 0;
  for(int remain = n-1; remain > 0;){
    auto min_elem = std::min_element(E.begin(), E.end(), [](auto& lhs, auto& rhs){ return lhs.Value() < rhs.Value(); });
    min_elem->exist = false;
    E[min_elem->j * n + min_elem->i].exist = false;
    if(V[min_elem->i].Find() != V[min_elem->j].Find()){
      Node::Union(V[min_elem->i], V[min_elem->j]);
      sum_w += min_elem->w;
      remain -= 1;
    }
  }
  std::cout << sum_w << std::endl;
  return 0;
}

C++: #define _USE_MATH_DEFINES

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>

using namespace std;

typedef pair<long long int, long long int> P;
long long int INF = 1e18;
#define MAX_V 200

long long int cost[MAX_V][MAX_V]; // cost[u][v] は頂点uからvへのコスト
long long int mincost[MAX_V];
bool used[MAX_V];
int V;// Vは頂点数

// 最小全域木であり、O(V^2)

// 最初にすべての頂点に対するcostを計算しておくこと

long long int prim(){
	
	for(int i = 0; i < V; i++){
		mincost[i] = INF;
		used[i] = false;
	}
	
	mincost[0] = 0;
	
	long long int total = 0;
	
	while(true){
		
		int add_v = -1;
		
		for(int u = 0; u < V; u++){
			if(!used[u] && (add_v == -1 || mincost[u] < mincost[add_v])){
				add_v = u;
			}
		} // 追加する頂点を決定
		
		if(add_v == -1){
			break;
		} // 更新する頂点がない場合、終了
		used[add_v] = true;
		total += mincost[add_v];
		
		for(int u = 0; u < V; u++){
			mincost[u] = min(mincost[u], cost[add_v][u]);
		} // 集合の外の頂点への最小コストを更新
	}
	return total;
}

int main(){
	
	long long int ans;
	
	cin >> V;
	
	for(int i = 0; i < V; i++){
		for(int j = 0; j < V; j++){
			cin >> cost[i][j];
			if(cost[i][j] == -1){
				cost[i][j] = INF;
			}
		}
	}
	
	ans = prim();
	cout << ans << endl;
	
	return 0;
}

C++: #include <iostream>
#include <limits.h>
using namespace std;

int main(void)
{
	int n, now = 0, flag[101];
	long ans = 0, an[100][100], min[100];

	cin >> n;

	for(int i = 0; i < n; i++) {
		for(int j = 0; j < n; j++) {
			cin >> an[i][j];
		}
		min[i] = LONG_MAX - 1;
		flag[i] = 0;
	}

	/* ???????則?????????即?????属????????? */
	do {
		flag[now] = 1;
		flag[100] = 0;
/*
		cout << "\n??? " << now << " ????則???????????????????" << endl;
		cout << "?????贈?????? : ";
		for(int i = 0; i < n; i++) {
			if(flag[i] == 1) {
				cout << i << " ";
			}
		}
		cout << endl;
		cout << "?????即???????属?????????則" << endl;
		for(int i = 0; i < n; i++) {
			if(flag[i] == 0) {
				cout << "??? " << i << " : " << min[i] << endl;
			}
		}
*/
		/* min?????卒??属 */
		for(int j = 0; j < n; j++) {
			if(an[now][j] != -1 && an[now][j] < min[j] && flag[j] == 0) {
/*				cout << "??? " << j <<" ???????属?????????卒??属??????????????? : "<< min[j] << " ??? " << an[now][j] << endl; */
				min[j] = an[now][j];
			}
		}
/*
		cout << "?????即???????属?????????則" << endl;
		for(int i = 0; i < n; i++) {
			if(flag[i] == 0) {
				cout << "??? " << i << " : " << min[i] << endl;
			}
		}
*/
		/* ?測??????蔵?卒蔵???min????属???? */
		int key = LONG_MAX - 1, key_j;

		for(int j = 0; j < n; j++) {
			if(flag[j] == 0 && min[j] < key) {
				key = min[j];
				key_j = j;
			}
		}
/*
		cout << "?則???????????賊??????????????????? : ??? " << key_j << endl;
*/
		/* ?則?????????? */
		now = key_j;
		ans += key;

		/* ??????????????贈????????? */
		for(int i = 0; i < n; i++) {
			if(flag[i] == 1) {
				flag[100]++;
			}
		}
	
	}while(flag[100] != n - 1);

	cout << ans << 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(;114514;){
		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;
		a[pj][pi] = a[pi][pj] = INT_MAX;
		if(!added[pi] || !added[pj]){
			added[pi] = added[pj] = true;
			total += minCost;
		}
	}
	
	printf("%ld\n", total);
	return 0;
}