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 <bits/stdc++.h>  // g++ -std=c++14 -o a a.cpp

using namespace std;

typedef pair<int, int> P;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define repto(i, n) for(int i = 0; i <= (n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end()) // ユニークなvectorにする。連続した値を除去して切り出す。
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007
#define DEBUG(x) cout << #x << ": " << x << endl;

__attribute__((constructor))
void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

int E[2001][2001] = {{0}};
int T[2001] = {0};
const int TAKEN = 1;

int main() {
  int N;
  cin >> N;

  rep(i, N) {
    rep(j, N) {
      int w;
      cin >> w;
      if (w == -1) {
         E[i][j] = INF;
      } else {
         E[i][j] = w;
      }
      // DEBUG(E[i][j]);
	}
  }

  T[0] = TAKEN;
  int count = 1;
  int sum = 0;

  while (count < N) {
    int minW = INF;
    int minI = -1;
    int minJ = -1;
    rep(i, N) {
       if (T[i] == TAKEN) {
          rep(j, N) {
			if(T[j] != TAKEN) {
              int w = E[i][j];
              if (w < minW) {
//                DEBUG(i);
//                DEBUG(j);
//                DEBUG(w);
                minW = w;
                minI = i;
                minJ = j;
              }
			}
          }
       }
    }
//    DEBUG("--------------------");
//    DEBUG(minI);
//    DEBUG(minJ);
//    DEBUG(minW);

	T[minJ] = TAKEN;
//	rep (i , N) {
//		DEBUG(i);
//		DEBUG(T[i]);
//	}

	count++;
	sum += minW;
  }

  cout << sum << endl;
}

C++: #include <algorithm>
#include <vector>
#include <iostream>

typedef std::vector<int> TTree;
struct TPath
{
    int Id1;
    int Id2;
    int Cost;
    TPath(int AId1, int AId2, int ACost)
     : Id1(AId1), Id2(AId2), Cost(ACost)
    {}
};
bool Compare(const TPath& Left, const TPath& Right)
{
    if(Left.Cost  < 0) return false;
    if(Right.Cost < 0) return true;
    return Left.Cost < Right.Cost;
}


int main()
{
    int n;
    std::cin >> n;
    std::vector<TTree*> Table(n, NULL);
    for(std::size_t i = 0; i < n; ++i){
        Table[i] = new std::vector<int>(1, i);
    }
    std::vector<TPath> PathList;
    PathList.reserve(n*n);
    
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < n; ++j){
            int Cost;
            std::cin >> Cost;
            PathList.push_back(TPath(i, j, Cost));
        }
    }
    std::sort(PathList.begin(), PathList.end(), Compare);
    
    int Total = 0;
    for(std::vector<TPath>::const_iterator It = PathList.begin();
        It != PathList.end(); ++It)
    {
        const TPath& Path = *It;
        TTree* Tree1 = Table[Path.Id1];
        TTree* Tree2 = Table[Path.Id2];
        if(Tree1 == Tree2){
            continue;
        }
        Tree1->insert(Tree1->end(), Tree2->begin(), Tree2->end());
        for(std::size_t j = 0; j < Tree2->size(); ++j){
            int Id = (*Tree2)[j];
            Table[Id] = Tree1;
        }
        delete Tree2;
        Total += Path.Cost;
        // std::cout << "[" << Path.Id1 << "]" << "[" << Path.Id2 << "] = " << Path.Cost << std::endl;
    }
    std::cout << Total << std::endl;
    return 0;
}

C++: #include <utility>
#include <iostream>
#include <algorithm>
using namespace std;

int root(int a,int P[])
{
    if(P[a] == a)
        return a;
    else    return (P[a] = root(P[a],P));
}
int is_same_set(int a,int b,int P[])
{
    return root(a,P) == root(b,P);
}

int main()
{
    int n,i,j,M=0,minsum=0;
    cin >> n;
    //init(n);
    int a[n][n],cost[n*n],src[n*n],dst[n*n];
    for(i=0;i<n;i++)
        for(j=0;j<n;j++)
            cin >> a[i][j];
    for(int r=0;r<n;r++)
        for(int c=r+1;c<n;c++)
            if(a[r][c] > -1)
            {
                cost[M] = a[r][c];
                src[M] = r;
                dst[M] = c;
                M++;
            }
    pair<int,int> edeges[M];
    int P[n];
    for(i=0;i<n;i++)
        P[i] = i;
    for(i=0;i<M;i++)
    {
        edeges[i].first = cost[i];
        edeges[i].second = i;
    }
    sort(edeges,edeges+M);
    for(i=0;i<M;i++)
    {
        if(is_same_set(src[edeges[i].second], dst[edeges[i].second], P))
            continue;
        else    {
                    P[root(src[edeges[i].second],P)] = root(dst[edeges[i].second],P);
                    minsum += edeges[i].first;
                }
    }
    cout << minsum << endl;
    return 0;
}

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

vector< vector < int > > adjMatrix;

int findMSTCost(int n) {
  vector< bool > visited (n, false);
  visited[0] = true;
  int cost = 0;
  for (int numberVertices = 1; numberVertices < n; numberVertices++) {
    int minEdge = 3000, minVertex = n;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
	if ((i != j) &&
	    (visited[i] == true) &&
	    (visited[j] == false) &&
	    (adjMatrix[i][j] != -1) &&
	    (adjMatrix[i][j] < minEdge)) {
	  minEdge = adjMatrix[i][j];
	  minVertex = j;
	}
      }
    }
    cost += minEdge;
    visited[minVertex] = true;
  }
  return cost;
}

int main(int argc, char **argv) {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  adjMatrix = vector< vector < int > > (n, vector< int > (n, 0));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      cin >> adjMatrix[i][j];
  cout << findMSTCost(n) << endl;
  return 0;
}