Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

const INF = 1 << 60

// Main
func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N := sc.nextInt()
	G := make([][]int, N)
	for i := 0; i < N; i++ {
		G[i] = make([]int, N)
		for j := 0; j < N; j++ {
			G[i][j] = sc.nextInt()
		}
	}
	d := make([]int, N)
	for i := 0; i < N; i++ {
		d[i] = INF
	}
	seen := make([]bool, N)
	d[0] = 0
	ans := 0
	for {
		cur := 0
		minCost := INF
		for i := 0; i < N; i++ {
			if !seen[i] && d[i] < minCost {
				minCost = d[i]
				cur = i
			}
		}
		if minCost == INF {
			break
		}
		seen[cur] = true
		ans += d[cur]
		for i := 0; i < N; i++ {
			cost := G[cur][i]
			if cost != -1 && cost < d[i] {
				d[i] = cost
			}
		}
	}

	fmt.Fprintln(wtr, ans)
	wtr.Flush()
}

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