Go: package main

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

const (
	white int = iota
	gray
	black
)
const INFTY int = 2001

var G [][]int
var color []int
var d []int
var p []int
var N int

func prim() int {
	u := 0
	d[u] = 0
	for {
		minCost := INFTY
		for i:=0; i<N; i++ {
			if color[i] != black && d[i] < minCost {
				minCost = d[i]
				u = i
			}
		}

		if minCost == INFTY {
			break
		}

		color[u] = black

		for v:=0; v<N; v++ {
			if color[v] != black && G[u][v] != -1 {
				if G[u][v] < d[v] {
					d[v] = G[u][v]
					p[v] = u
					color[v] = gray
				}
			}
		}
	}
	// sum
	sum := 0
	for i:=0; i<N; i++ {
		if p[i] != -1 {
			sum += G[i][p[i]]
		}
	}
	return sum
}




func initVariable(N int) {
	G = make([][]int, N)
	for i:=0; i<N; i++ {
		G[i] = make([]int, N)
	}
	color = make([]int, N)
	d = make([]int, N)
	p = make([]int, N)
	for i:=0; i<N; i++ {
		d[i] = INFTY
		p[i] = -1
	}
}

func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	N = nextInt(scanner)
	initVariable(N)
	for i:=0; i<N; i++ {
		for j:=0; j<N; j++ {
			G[i][j] = nextInt(scanner)
		}
	}
	fmt.Printf("%d\n", prim())
}

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

struct edge {int u, v, cost;};

int n;
vector<edge> G;

bool comp(const edge& e1, const edge& e2) {
    return e1.cost < e2.cost;
}

class UnionFind {
 public:
    int g_num;
    vector<int> rank, par;

    UnionFind() {}
    UnionFind(int size) {
        rank.resize(size, 0);
        par.resize(size, 0);
        for (int i = 0; i < size; i++) {
            par[i] = i;
            rank[i] = 0;
        }
        g_num = size;
    }

    bool same(int x, int y) {
        return find(x) == find(y);
    }

    void unite(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) return;

        g_num--;
        if (rank[x] > rank[y]) {
            par[y] = x;
        } else {
            par[x] = y;
            if (rank[x] == rank[y]) rank[y]++;
        }
    }

    int find(int x) {
        if (x == par[x]) return x;
        return par[x] = find(par[x]);
    }

    int group_num() {
        return g_num;
    }
};

int kruskal() {
    sort(G.begin(), G.end(), comp);
    UnionFind uf(n);
    int res = 0;
    for (auto e: G) {
        if (!uf.same(e.u, e.v)) {
            uf.unite(e.u, e.v);
            res += e.cost;
        }
    }
    return res;
}

int main(void) {
    cin >> n;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int cost;
            cin >> cost;
            if (cost == -1) continue;
            G.push_back(edge {i, j, cost});
        }
    }
    int ans = kruskal();
    cout << ans << endl;
    return 0;
}

C++: // #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define Graph vector< vector<int> >
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1000000007;

int main(){
    int N;
    cin >> N;
    Graph M(N, vector<int>(N));
    rep(i, N){
        rep(j, N){
            cin >> M[i][j];
            if(M[i][j] == -1) M[i][j] = INF;
        }
    }

    vector<int> d(N, INF), p(N, -1);
    vector<bool> visited(N);
    d[0] = 0;
    p[0] = -1;
    int sum = 0;

    while(1){
        int mincost = INF;
        int u;
        rep(i, N){
            if( !visited[i] && d[i] < mincost){
                mincost = d[i];
                u = i;
            }
        }

        if(mincost == INF) break;
        sum += d[u];
        visited[u] = true;

        rep(v, N){
            if( !visited[v] && M[u][v] != INF){
                if(M[u][v] < d[v]){
                    d[v] = M[u][v];
                    p[v] = u;
                }
            }
        }
    }
    
    cout << sum << endl;

}

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

bool self(vector<vector<int> >& G, int s, int t, vector<pair<int, int> >& stock_idx, vector<int>& searched){
    if (searched[s] == 1) return false;
    searched[s] = 1;
    if (G[s][t] != -1 && stock_idx.size() > 1) {
        stock_idx.push_back(make_pair(s, t));
        return true;
    }
    for (int i = 0; i < G[s].size(); i++) {
        if (G[s][i] != -1) {
            stock_idx.push_back(make_pair(s, i));
            if (self(G, i, t, stock_idx, searched)) return true;
            stock_idx.pop_back();
        }
    }
    return false;
}

bool DFS(vector<vector<int> >& G, int s, int t, vector<pair<int, int> >& stock_idx){
    vector<int> searched(G.size(), 0);
    return self(G, s, t, stock_idx, searched);
}

void cut(vector<vector<int> >& G, vector<pair<int, int> >& stock_idx){
    pair<int, int> max_idx = stock_idx[0];
    for (int i = 0; i < stock_idx.size(); i++) {
        if (G[stock_idx[i].first][stock_idx[i].second] > G[max_idx.first][max_idx.second]) {
            max_idx = stock_idx[i];
        }
    }
    G[max_idx.first ][max_idx.second] = -1;
    G[max_idx.second][max_idx.first ] = -1;
    stock_idx.clear();
}

int main(){
    int n;
    cin >> n;
    vector<vector<int> > G(n, vector<int>(n));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> G[i][j];
        }
    }
    for (int i = 0; i < n; i++) {
        vector<pair<int, int> > stock_idx;
        while (DFS(G, i, i, stock_idx)) cut(G, stock_idx);
    }
    int sum = 0;
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            if (G[i][j] != -1) sum += G[i][j];
        }
    }
    cout << sum << endl;
    // for (int i = 0; i < n; i++) {
    //     for (int j = 0; j < n; j++) {
    //         cout << " " << G[i][j];
    //     }
    //     cout << endl;
    // }

    return 0;
}