Go: package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
	"strconv"
)

type Node struct {
	id     int
	weight int
}

func init() {
	sc.Split(bufio.ScanWords)
}

type NodeHeap []Node

func (h NodeHeap) Len() int           { return len(h) }
func (h NodeHeap) Less(i, j int) bool { return h[i].weight < h[j].weight }
func (h NodeHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

func (h *NodeHeap) Push(x interface{}) {
	*h = append(*h, x.(Node))
}

func (h *NodeHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func main() {
	n := scanInt()

	list := make([][]int, n)
	seen := make([]bool, n)
	for i := range list {
		list[i] = make([]int, n)
		for j := range list[i] {
			list[i][j] = scanInt()
		}
	}

	start := Node{id: 0, weight: 0}
	h := &NodeHeap{start}
	heap.Init(h)

	sum := 0
	for h.Len() > 0 {
		node := heap.Pop(h).(Node)
		if seen[node.id] {
			continue
		}
		seen[node.id] = true
		sum += node.weight

		for i, w := range list[node.id] {
			if w < 0 {
				continue
			}
			heap.Push(h, Node{id: i, weight: w})
		}
	}

	fmt.Println(sum)
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1e16;
const ll mod = 1000000007;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
template<class T> inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template<class T> inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
int ctoi(char c) {
  if (c >= '0' && c <= '9') {
    return c - '0';
  }
  return 0;
}
ll factorial(ll n) {
  if (n == 1) {
    return 1;
  }
  ll ans = factorial(n-1);
  return ans*n;
}
ll gcd(ll a, ll b) {
  if (a < b) {
    ll tmp = a;
    a = b;
    b = tmp;
  }
  ll r = a%b;
  while(r != 0) {
    a = b;
    b = r;
    r = a%b;
  }
  return b;
}
ll lcm(ll a, ll b) {
  return (a/gcd(a, b))*b;
}
bool prime(ll n) {
  if (n == 1) {
    return false;
  } else {
    for (ll i = 2; i <= (ll)pow(n, 0.5); i++) {
      if (n%i == 0) return false;
    }
  }
  return true;
}
ll RepeatSquaring(ll n, ll p, ll m) {
  if (p == 0) return 1;
  if (p%2 == 0) {
    ll t = RepeatSquaring(n, p/2, m);
    return (t*t)%m;
  }
  return (n*RepeatSquaring(n, p-1, m))%m;
}

vector <ll> color(100, 0);
vector <vector <ll>> m(100, vector <ll> (100, INF));
vector <ll> d(100, INF);
vector <ll> p(100, -1);

ll prim(ll n) {
  d.at(0) = 0;
  while (true) {
    ll mincost = INF;
    ll u;
    rep(i, n) {
      if (color.at(i) != 2 && d.at(i) < mincost) {
        mincost = d.at(i);
        u = i;
      }
    }
    if (mincost == INF) {
      break;
    }
    color.at(u) = 2;
    rep(i, n) {
      if (color.at(i) != 2 && m.at(u).at(i) != INF) {
        if (m.at(u).at(i) < d.at(i)) {
          d.at(i) = m.at(u).at(i);
          p.at(i) = u;
          color.at(i) = 1;
        }
      }
    }
  }
  ll sum = 0;
  rep(i, n) {
    if (p.at(i) != -1) sum += m.at(p.at(i)).at(i);
  }
  return sum;
}

int main() {
  ll n;
  cin >> n;
  rep(i, n) {
    rep(j, n) {
      ll a;
      cin >> a;
      if (a != -1) {
        m.at(i).at(j) = a;
      }
    }
  }
  cout << prim(n) << endl;
}

C++: n = int( raw_input() )
A = [0]
p = range(1, n)
a = [ [ 0 for i in xrange(n) ] for j in xrange(n) ]
for i in xrange(n):
    a[i] = map( int,raw_input().split() )
l = 0
while p != []:
    a_min = 2001
    for i in A:
        for j in p:
            if 0 <= a[i][j] < a_min:
                a_min = a[i][j]
                add_A = j
    A.append( add_A )
    NY.remove( add_A )
    l += a_min
print l

C++: #include<iomanip>
#include<limits>
#include<thread>
#include<utility>
#include<iostream>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<numeric>
#include<cassert>
#include<random>
#include<chrono>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pi;
typedef pair<double,double> pd;
typedef pair<double,ll> pdl;
#define F first
#define S second
#define MK make_pair
const ll E=1e18+7;
const ll MOD=1000000007;

vector<ll> uft;

ll search(ll where){
    if(uft[where]==where){return where;}
    else{return uft[where]=search(uft[where]);}
}

void add(ll a,ll b){
    uft[search(a)]=search(b);
}


int main(){
    ll n;
    cin>>n;
    priority_queue<vector<ll>,vector<vector<ll>>,greater<vector<ll>>> E;
    for(int i=0;i<n;i++){
        for(int t=0;t<n;t++){
            ll a;
            cin>>a;
            if(a!=-1){
                E.push({a,i,t});
            }
        }
    }
    uft.resize(n);
    for(int i=0;i<n;i++){
        uft[i]=i;
    }
    ll cost=0;
    while(!E.empty()){
        vector<ll> e=E.top();
        E.pop();
        if(search(e[1])!=search(e[2])){
            cost+=e[0];
            add(e[1],e[2]);
        }
    }
    cout<<cost<<endl;
    
    
    return 0;
}

C++: #include <iostream>
using namespace std;
static const int INFTY = (1 << 21);
int n,M[100][100];

int prim(){
  int u,v,d[100],p[100],color[100],mincost;
  for (int i = 0; i < n; i++) {
    color[i] = 0;
    d[i] = INFTY;
    p[i] = -1;
  }
  d[0] = 0;

  while (1) {
    mincost = INFTY;
    u = -1;
    for (int i = 0; i < n; i++) {
      if (color[i] != 2 && d[i] < mincost) {
        u = i;
        mincost = d[i];
      }
    }
    if (u == -1) break;
    color[u] = 2;
    for (v = 0; v < n; v++) {
      if (color[v] != 2 && M[u][v] != INFTY) {
        if (M[u][v] < d[v]) {
          d[v] = M[u][v];
          p[v] = u;
          color[v] = 1;
        }
      }
    }
  }
  int sum = 0;
  for (int i = 0; i < n; i++) {
    if (p[i] != -1) sum += M[i][p[i]];
  }
  return sum;
}

int main() {
  int t;
  cin >> n;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int e; cin >> e;
      M[i][j] = (e == -1) ? INFTY : e;
    }
  }
  cout << prim() << endl;
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
	int mapp[101][101];
	int d[101],vis[101];
	int ans=0,n,u;
	cin>>n;
	for (int i=0; i<n; i++)
	    for (int j=0; j<n; j++)
	        cin>>mapp[i][j];
	memset(vis,0,sizeof(vis));
	memset(d,0x1f1f1f1f,sizeof(d));
	d[0]=0; 
	while (true){
		  int minn=0x1f1f1f1f;
		  for (int i=0; i<n; i++)
		  	  if (!vis[i] && d[i]<minn){
		  	  	 minn=d[i]; u=i;
			  }
		  vis[u]=1;
		  if (minn==0x1f1f1f1f)
		     break;
		  for (int i=0; i<n; i++)
		      if (!vis[i] && mapp[u][i]!=-1 && mapp[u][i]<d[i]){
		      	 d[i]=mapp[u][i];
			  }
	}
	for (int i=0; i<n; i++)
		ans+=d[i];
	cout<<ans<<endl;
	return 0;
}