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
	for len(edges) > 0 {
		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
	}
	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 <stdio.h>
#include <vector>
#include <list>
#include <map>
#include <array>
#include <memory>
#include <string>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cmath>
#include <unordered_map>
#include <stdexcept>
#include <numeric>
#include <queue>

using std::string;
using std::vector;


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// util
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

std::vector<std::string> split(const std::string& input, char delimiter)
{
    std::istringstream stream(input);

    std::string field;
    std::vector<std::string> result;
    while (std::getline(stream, field, delimiter)) {
        result.push_back(field);
    }
    return result;
}



//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// user func
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void printList(const vector<int>& list) {
    // print
    for(int i = 0; i < list.size(); i++){
        printf("%d", list[i]);
        if ( i + 1 != list.size() ) {
            printf(" ");
        }
    }
    printf("\n");
}



//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// logic
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// 要素数固定で汎用性低し
// index = key, value = parent key
class UnionFindTree{
private:
    vector<int> list;
public:
    UnionFindTree(int num)
        :list(num+1)
    {
        for(int i = 0; i < this->list.size(); i++) {
            list[i] = i;
        }
    }

    int Find(int key) {
        if (list[key] == key) {
            return key;
        } else {
            return Find(list[key]);
        }
    }
    bool AreSame(int lhs, int rhs){
        return Find(lhs) == Find(rhs);
    }
    void Union(int lhs, int rhs){
        this->list[Find(lhs)] = this->list[Find(rhs)];
    }

    void Print() {
        for(int i = 1; i < this->list.size(); i++) {
            printf("%d -> %d\n", i, this->list[i]);
        }
    }
};

class Node{
public:
    Node(int id, int numEdge)
     : id(id)
     , edges(vector<int>(numEdge + 1))
    {
        // constructer
    }
    void Edge(int id, int weight){
        this->edges[id] = weight;
    }
    vector<int> Edges() {
        return this->edges;
    }

    int id;
    bool done = false;
    vector<int> edges;
};

typedef struct _Relation{
    _Relation(int weight, int from, int to)
        : weight(weight)
        , from(from)
        , to(to)
    {}
    int weight;
    int from;
    int to;
} Relation;

class List{
public:
    // constructer
    List(int numNode)
    {
        this->nodes.push_back(Node(0, 0));
        for (int i = 1; i <= numNode; i++) {
            this->nodes.push_back(Node(i, numNode));
        }
    }

    void SetWeight(int from, int to, int weight)
    {
        this->nodes[from].Edge(to, weight);
    }
    
    int CalcWeight() {
        UnionFindTree tree(this->nodes.size() - 1); // init by num elements
        std::list<Relation> costs;
        for (int i = 1; i < this->nodes.size(); i++) {
            for (int j = 1; j < this->nodes[i].Edges().size(); j++) {
                int cost = this->nodes[i].Edges()[j];
                if(cost == -1) continue;
                costs.push_back(Relation(this->nodes[i].Edges()[j], i, j));
            }
        }
        costs.sort([](const Relation& lhs, const Relation& rhs){ return lhs.weight < rhs.weight; });
        vector<Relation> result;
        while(!costs.empty()) {
            auto min = costs.front();
            costs.pop_front();
            if (!tree.AreSame(min.from, min.to)) {
                tree.Union(min.from, min.to);
                result.push_back(min);
            }
        }
        int sum = 0;
        for (auto each : result) {
            sum += each.weight;
        }
        return sum;
    }
    
    

    void Print() {
        for (int i = 1; i < this->nodes.size(); i++) {
            printf("%d:", i);
            for (int j = 1; j < this->nodes[i].Edges().size(); j++) {
                printf(" %d", this->nodes[i].Edges()[j]);
            }
            printf("\n");
        }
    }
private:
    vector<Node> nodes;
};

void solve() {

    int n;
    scanf("%d", &n);
    List list(n);

    for (int i = 1; i <= n; i++){
        for (int j = 1; j <= n; j++) {
            int w;
            scanf("%d", &w);
            list.SetWeight(i, j, w);
        }
    }
    // list.Print();
    printf("%d\n", list.CalcWeight());
}

void test(){
    UnionFindTree t(5);
    t.Union(1, 2);
    t.Union(2, 3);
    t.Union(4, 5);
    t.Print();
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// main
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main() {
    solve();
    // test();
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

int main(){
  int n,a[100][100],b[100]={},c[100]={};
  c[0]=1;
  cin>>n;
  for(int i=0;i<n;i++)
   for(int j=0;j<n;j++)cin>>a[i][j];

  int k=1,min1,ans=0;
  while(1){
   int min=10000;
   for(int i=0;i<k;i++)
    for(int j=0;j<n;j++)
     if(a[b[i]][j]<min && a[b[i]][j]!=-1 &&c[j]!=1){
      min=a[b[i]][j];
      min1=j;
    }
   if(k==n)break;
   b[k]=min1;
   c[min1]=1;
   ans+=min;
   k++;
  }

 cout<<ans<<endl;
 return 0;
}