//============================================================================
// Name        : MinimumSpanningTree.cpp
// Author      : Ken
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <vector>
#include <set>
using namespace std;

struct Edge;

struct Node{
	const int id;
	vector<int> adjEdgeIds;
};

struct Edge{
	int id;
	int weight;
	int startNodeId;
	int endNodeId;
};

class Graph{
public:
	int MAX_WEIGHT=2000;
	vector<Node> nodeList;
	vector<Edge> edgeList;

	Graph():nodeList(),edgeList(){}

	Graph(int n):nodeList(){
		for(int i=0; i<n; i++){
			vector<int> empty;
			Node node = {i, empty};
			nodeList.push_back(node);
		}
		//cout << "initialized\n";
	}

	void write(){
		cout << "***nodeList***\n";
		for(int i=0; i<nodeList.size(); i++){
			Node& node = nodeList.at(i);
			cout << "node id: " << node.id << " ";
			for(int j=0; j<node.adjEdgeIds.size(); j++){
				Edge& edge = edgeList.at(node.adjEdgeIds.at(j));
				cout << "edge id: " << edge.id << " ";
				cout << edge.startNodeId << "<->" << edge.endNodeId << " ";
			}
			cout << "\n";
		}

		cout << "***edgeList***\n";
		for(int i=0; i<edgeList.size(); i++){
			Edge& edge = edgeList.at(i);
			cout << "edge id: " << edge.id << " ";
			cout << "edge weight: " << edge.weight << " ";
			cout << edge.startNodeId << "<->" << edge.endNodeId << "\n";
		}
	}

	void addLightestEdge(set<int>& edgeIds){
		set<int> curNodeIds;
		if(edgeIds.size()==0){
			curNodeIds.insert(0);
		}else{
			set<int>::iterator p;
			for(p=edgeIds.begin(); p!=edgeIds.end(); p++){
				Edge& edge = edgeList.at(*p);
				curNodeIds.insert(edge.startNodeId);
				curNodeIds.insert(edge.endNodeId);
			}
		}

		while(true){
		set<int>::iterator p;
		int lightestWeight = MAX_WEIGHT;
		int lightestEdgeId = -1;
		for(p=curNodeIds.begin(); p!=curNodeIds.end(); p++){
			Node& node = nodeList.at(*p);

			vector<int>::iterator q;
			for(q=node.adjEdgeIds.begin(); q!=node.adjEdgeIds.end(); q++){

				if(curNodeIds.count(edgeList.at(*q).startNodeId) == 0 or
				   curNodeIds.count(edgeList.at(*q).endNodeId) == 0){

					if(edgeList.at(*q).weight < lightestWeight){
						lightestWeight = edgeList.at(*q).weight;
						lightestEdgeId = edgeList.at(*q).id;
					}
				}
			}
		}

		if(lightestEdgeId!=-1){
		    edgeIds.insert(lightestEdgeId);
		    //cout << "inserted edge id: " << lightestEdgeId << "\n";
		    //cout << "inserted edge weight: " << lightestWeight << "\n";
		    Edge& edge = edgeList.at(lightestEdgeId);
		    curNodeIds.insert(edge.startNodeId);
		    curNodeIds.insert(edge.endNodeId);
		}else{
			return;
		}
		}
	}

	int sumEdgeWeight(set<int> edgeIdSet){
		int sum = 0;
		set<int>::iterator p;
		for(p=edgeIdSet.begin(); p!=edgeIdSet.end(); p++){
			sum += edgeList.at(*p).weight;
		}

		return sum;
	}
};

void writeSet(set<int> intSet){
	set<int>::iterator p;
	for(p=intSet.begin(); p!=intSet.end(); p++){
		cout << *p << " ";
	}
}



int main() {
	int n;
	cin >> n;
	Graph g(n);

	//g.write();

	for(int i=0; i<n; i++){
		for(int j=0; j<n; j++){
			int weight;
			cin >> weight;
			if(i<j and weight != -1){
				int edgeId = g.edgeList.size();
				Edge edge = {edgeId, weight, i, j};
				g.edgeList.push_back(edge);
				g.nodeList.at(i).adjEdgeIds.push_back(edgeId);
				g.nodeList.at(j).adjEdgeIds.push_back(edgeId);
			}
		}
	}

	//g.write();

	set<int> edgeIds;
	g.addLightestEdge(edgeIds);
	//writeSet(edgeIds);
	int totalWeight;
	totalWeight = g.sumEdgeWeight(edgeIds);
	cout << totalWeight << "\n";

	return 0;
}



Measured memory_bytes: 3280