Go: package main

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

// Define disjoint-set forests

type vertexNo int

type vertex struct {
	number vertexNo
	rank int
	parent *vertex
}

func makeSet(number vertexNo) vertex {
	return vertex {
		number: number,
		rank: 0,
		parent: nil,
	}
}

func union(x, y *vertex) *vertex {
	return link(findSet(x), findSet(y))
}

func link(x, y *vertex) *vertex {
	if x.rank > y.rank {
		y.parent = x
		return x
	} else {
		x.parent = y
		if x.rank == y.rank {
			y.rank += 1
		}
		return y
	}
}

func findSet(v *vertex) *vertex {
	if v.parent == nil {
		return v
	}
	v.parent = findSet(v.parent)
	return v.parent
}

// Function to calculate minimum spanning tree

type matrix [][]int

type edge struct {
	one vertexNo
	two vertexNo
	weight int
}

const(
	MAX_VERTICES = 100
)

func newEdgesFromMatrix(matrix matrix) []edge {
	edges := make([]edge, 0, MAX_VERTICES * MAX_VERTICES)

	for i := 0; i < len(matrix); i++ {
		row := matrix[i]
		for j := i; j < len(row); j++ {
			if row[j] >= 0 {
				edge := edge{one: vertexNo(i+1), two: vertexNo(j+1), weight: row[j]}
				edges = append(edges, edge)
			}
		}
	}

	return edges
}

func solve(matrix matrix) int {
	nVertices := len(matrix)

	// initialize vertices
	vertices := make([]vertex, nVertices)
	for i := 0; i < nVertices; i++ {
		vertices[i] = makeSet(vertexNo(i+1))
	}

	// prepare edges sorted in the ascending order of weight
	edges := newEdgesFromMatrix(matrix)
	sort.Slice(edges, func(i, j int) bool {
		return edges[i].weight < edges[j].weight
	})

	unionCount := 0
	weightSum := 0
	for unionCount < nVertices-1 {
		for i := 0; i < len(edges); i++ {
			edge := edges[i]
			v1 := &vertices[edge.one-1]
			v2 := &vertices[edge.two-1]
			rep1 := findSet(v1)
			rep2 := findSet(v2)
			if rep1.number == rep2.number {
				continue
			}
			union(rep1, rep2)
			edges = append(edges[0:i], edges[i+1:]...)
			weightSum += edge.weight
			unionCount++
			break
		}
	}

	return weightSum
}

// main

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	matrix := parseMatrix(n, sc)

	edges := newEdgesFromMatrix(matrix)
	sort.Slice(edges, func(i, j int) bool {
		return edges[i].weight < edges[j].weight
	})

	fmt.Println(solve(matrix))
}

func parseMatrix(n int, sc *bufio.Scanner) [][]int {
	matrix := make([][]int, n)
	for i := 0; i < n; i++ {
		matrix[i] = make([]int, n)
		for j := 0; j < n; j++ {
			x, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			matrix[i][j] = x
		}
	}
	return matrix
}

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

func nextInt(sc *bufio.Scanner) (int, error) {
	return strconv.Atoi(nextText(sc))
}

C++: #include <iostream>
#include <vector>
#include <queue>
#include <tuple>

int main(int argc, char const *argv[]) {
  int n;
  std::vector<std::vector<int> > a;
  std::cin >> n; // .. 100
  a = std::vector<std::vector<int> >(n, std::vector<int>(n));
  // a_ij <= 2000, a_ij = a_ji, this graph is connected;

  for (size_t i = 0; i < n; i++) {
    for (size_t j = 0; j < n; j++) {
      std::cin >> a[i][j];
    }
  }

  //ALGORITHM
  std::vector<bool> found_vtx(n, false);
  // std::tuple<int,int,int> x(1,2,3);
  std::priority_queue<
    std::tuple<int,int,int>,
    std::vector<std::tuple<int, int, int> >,
    std::greater<std::tuple<int, int, int> >
  > q;

  int new_elm = 0;
  found_vtx[new_elm] = true;
  for (size_t i = 0; i < n; i++) {
    q.push(std::make_tuple(a[new_elm][i], new_elm, i));
  }
  int mst_weight = 0;
  while(!q.empty()){
    auto x = q.top(); q.pop();
    // std::cout << "e: "
    //   << std::get<0>(x) << " "
    //   << std::get<1>(x) << " "
    //   << std::get<2>(x) << std::endl;
    if(found_vtx[std::get<2>(x)] || std::get<0>(x) < 0) continue;
    else{
      mst_weight += std::get<0>(x);
      // std::cout << std::get<0>(x) << std::endl;
      new_elm = std::get<2>(x);
      found_vtx[new_elm] = true;
      for (size_t i = 0; i < n; i++) {
        q.push(std::make_tuple(a[new_elm][i], new_elm, i));
      }
    }
  }

  std::cout << mst_weight << std::endl;
  return 0;
}

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;
}