Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

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

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const (
	MAX   = 100
	INFTY = (1 << 21)
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

//V: グラフの頂点全体の集合
//T: MSTに属する頂点の集合
var (
	n     int
	M     [MAX][MAX]int //重みを記録する隣接行列
	color [MAX]int
	p     [MAX]int //MSTにおける頂点の親を記録
	d     [MAX]int //Tに属する頂点と、V−Tに属する頂点を繋ぐ辺の中で、重みが最小のものを記録
)

func prim() int {
	var u, minv int
	//初期化
	for i := 0; i < n; i++ {
		d[i] = INFTY
		p[i] = -1
		color[i] = WHITE
	}

	d[0] = 0

	for {
		//頂点uを決めていく
		//つまり、Tに属する頂点と、V−Tに属する頂点を繋ぐ辺の中で、重みが最小のものを決める
		minv = INFTY
		u = -1
		for i := 0; i < n; i++ {
			//未訪問かつ、辺の重みが最小となるuを選ぶ
			if minv > d[i] && color[i] != BLACK {
				u = i
				minv = d[i]
			}
		}

		if u == -1 {
			break
		}

		color[u] = BLACK
		for v := 0; v < n; v++ {
			//未訪問かつ、辺が存在するとき
			if color[v] != BLACK && M[u][v] != INFTY {
				if d[v] > M[u][v] {
					d[v] = M[u][v]
					p[v] = u
					color[v] = GRAY
				}
			}
		}
	}

	sum := 0
	for i := 0; i < n; i++ {
		if p[i] != -1 {
			sum += M[i][p[i]]
		}
	}
	return sum
}

func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()

	for i := 0; i < n; i++ {

		for j := 0; j < n; j++ {
			tmp := nextInt()
			if tmp == -1 {
				tmp = INFTY
			}
			M[i][j] = tmp
		}

	}

	fmt.Println(prim())

}

C++: #include <iostream>
using namespace std;
static const int MAX = 100;
static const int INFTY = (1 << 21);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, M[MAX][MAX];

int prim()  {
  int u, minv;
  int d[MAX], p[MAX], color[MAX];
  
  for(int i = 0; i < n; i++)  {
    d[i] = INFTY;
    p[i] = -1;
    color[i] = WHITE;
  }
  
  d[0] = 0;
  
  while (1)  {
    minv = INFTY;
    u = -1;
    for (int i = 0; i < n; i++)  {
      if (minv > d[i] && color[i] != BLACK )  {
        u = i;
        minv = d[i];
      }
    }
    if (u == -1)  break;
    color[u] = BLACK;
    for (int v = 0; v < n; v++)  {
      if (color[v] != BLACK && M[u][v] != INFTY )  {
        if (d[v] > M[u][v] ) {
          d[v] = M[u][v];
          p[v] = u;
          color[v] = GRAY;
        }
      }
    }
  }
  int sum = 0;
  for ( int i = 0; i < n; i++)  {
    if (p[i] != -1)  sum += M[i][p[i]];
  }
  return sum;
}

int main()  {
  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++: /**
 * @copyright (c) 2020 Daisuke Hashimoto
 */

#include <algorithm>
#include <cstdint>
#include <functional>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>

constexpr int32_t kMaxNumberOfVertices = 100;
constexpr int32_t kInvalidIndex = -1;

void CallPrimAlgorithm(std::istream &input_stream);

struct Edge {
  int32_t to{kInvalidIndex};
  int32_t weight{INT32_MAX};
};

struct Vertex {
  bool assigned_to_tree{false};
  std::vector<Edge> neighbours;
};

class PrimAlgorithm {
 public:
  PrimAlgorithm() noexcept;
  ~PrimAlgorithm() noexcept;
  void SetNumberOfVertices(const int32_t number_of_vertices) noexcept;
  void AddVertex(const int32_t from, const int32_t to, const int32_t weight) noexcept;
  void BuildMinimumSpanningTree(const int32_t start_vertex_index) noexcept;
  int32_t GetSumOfWeightsOfMst() const noexcept;

 private:
  void AddToTree(const int32_t child, const int32_t weight) noexcept;

 private:
  int32_t number_of_vertices_;
  int32_t sum_of_weights_;
  Vertex vertices_[kMaxNumberOfVertices];
  using Pair = std::pair<int32_t, int32_t>;
  std::priority_queue<Pair, std::vector<Pair>, std::greater<Pair>> priority_queue_;
};

void CallPrimAlgorithm(std::istream &input_stream) {
  input_stream.tie(0);
  std::ios::sync_with_stdio(false);
  PrimAlgorithm *minimum_spanning_tree = new PrimAlgorithm();
  try {
    int32_t number_of_vertices;
    input_stream >> number_of_vertices;
    minimum_spanning_tree->SetNumberOfVertices(number_of_vertices);
    for (int32_t from = 0; from < number_of_vertices; ++from) {
      for (int32_t to = 0; to < number_of_vertices; ++to) {
        int32_t weight;
        input_stream >> weight;
        if (weight >= 0) {
          minimum_spanning_tree->AddVertex(from, to, weight);
        }
      }
    }
    constexpr int32_t kStartVertex = 0;
    minimum_spanning_tree->BuildMinimumSpanningTree(kStartVertex);
    const int32_t sum_of_weights = minimum_spanning_tree->GetSumOfWeightsOfMst();
    std::cout << sum_of_weights << std::endl;
  } catch (...) {
    std::cerr << "ERROR: CallPrimAlgorithm()" << std::endl;
    throw;
  }
  delete minimum_spanning_tree;
}

// ****************************************************
PrimAlgorithm::PrimAlgorithm() noexcept : number_of_vertices_(0), sum_of_weights_(0) {}

PrimAlgorithm::~PrimAlgorithm() noexcept {}

void PrimAlgorithm::SetNumberOfVertices(const int32_t number_of_vertices) noexcept {
  number_of_vertices_ = number_of_vertices;
}

void PrimAlgorithm::AddVertex(const int32_t from, const int32_t to, const int32_t weight) noexcept {
  Edge edge1;
  edge1.to = to;
  edge1.weight = weight;
  vertices_[from].neighbours.push_back(edge1);
}

void PrimAlgorithm::BuildMinimumSpanningTree(const int32_t start_vertex_index) noexcept {
  constexpr int32_t kZeroDistance = 0;
  priority_queue_.push(Pair(kZeroDistance, start_vertex_index));
  while (priority_queue_.size() > 0) {
    const int32_t c_distance = priority_queue_.top().first;
    const int32_t c_vertex_index = priority_queue_.top().second;
    priority_queue_.pop();
    if (!vertices_[c_vertex_index].assigned_to_tree) {
      AddToTree(c_vertex_index, c_distance);
      for (const Edge &c_edge : vertices_[c_vertex_index].neighbours) {
        const int32_t neighbour_index = c_edge.to;
        if (!vertices_[neighbour_index].assigned_to_tree) {
          priority_queue_.push(Pair(c_edge.weight, neighbour_index));
        }
      }
    }
  }
}

int32_t PrimAlgorithm::GetSumOfWeightsOfMst() const noexcept {
  return sum_of_weights_;
}

void PrimAlgorithm::AddToTree(const int32_t child, const int32_t weight) noexcept {
  vertices_[child].assigned_to_tree = true;
  sum_of_weights_ += weight;
}

// **********************************************************************

int main() {
  try {
    CallPrimAlgorithm(std::cin);
  } catch (...) {
    std::cerr << "ERROR: main()" << std::endl;
  }
  return 0;
}

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

int n;
int a[101];

void print() {
    cout << a[0];
    for (int i = 1; i < n; i++) {
        cout << " " << a[i];
    }
    cout << endl;
}

int main() {
    while (cin >> n) {
        for (int i = 0; i < n; i++) {
            cin >> a[i];
        }
        print();
        for (int j = 1; j < n; j++) {
            int key = a[j];
            int i = j - 1;
            while (0 <= i && key < a[i]) {
                a[i + 1] = a[i];
                i--;
            }
            a[i + 1] = key;
            print();
        }
    }
}