1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
//#define LOG(args...) 
#define LOG(args...) fprintf(stderr, args)
#define FOR(i, n) for(int i = 0, __n = (n); i<__n; i++)

#define MAXN 2048

struct cost {
	int c;
	int a,b;
	bool operator<(const cost & x) const {
		return c < x.c;
	}
} cs[MAXN*MAXN/2], cs2[MAXN*MAXN/2];


const int mod1 = 32000;
const int mod2 = 1001001000;

struct hash1 {
	int operator()(const cost & c) const {
		return c.c%mod1;
	}
};

struct hash2 {
	int operator()(const cost & c) const {
		return c.c/mod1;
	}
};

template<class T, class fn>
void countSort(T* from, T* to, int n, int maxhash, fn hash) {

	int* ctr = new int[maxhash];
	FOR(i, maxhash) ctr[i] = 0;
	FOR(i, n) {
		ctr[hash(from[i])]++;
	}
	int prev = 0;
	FOR(i, maxhash) {
		int next = prev;
		prev += ctr[i];
		ctr[i] = next;
	}
	FOR(i, n) {
		int idx = ctr[hash(from[i])]++;
		to[idx] = from[i];
	}
	delete [] ctr;
}



char buffer[1000000];
int head=0;

unsigned getC() {
	if(buffer[head] == 0) {
		head = 0;
		gets(buffer);
		return 10;
	}
//	LOG("%d\n", buffer[head]);
	return buffer[head++] - '0';
}


int readInt() {
	int k = 0; 
	unsigned c;
	do {
		c = getC();
	} while (c > 9u);
	
	do {
		k*=10;
		k+=c;
		c=getC();
	} while(c <=9u);
	//scanf("%d", &k);
	return k;
}



int id[MAXN];
int w[MAXN];

int get(int x) {
	if(id[x] != x) {
		id[x] = get(id[x]);
	}
	return id[x];
}

void join(int x, int y) {
	x = get(x);
	y = get(y);
	if(w[y] > w[x]) id[x] = y;
	else id[y] = x;
}



int main() {
	
	int n; //scanf("%d", &n);
	n = readInt();
	
	int chead = 0;
	for(int i = 0; i<n; i++) {
		for(int j = i; j<n; j++) {
//			scanf("%d", &cs[chead].c);
			cs[chead].c = readInt();
			cs[chead].a=i;
			cs[chead].b=j+1;
			chead++;
		}
	}
			// 
		countSort(cs, cs2, chead, mod1, hash1());
			countSort(cs2, cs, chead, (mod2 + mod1)/mod1, hash2());

//					sort(cs, cs+chead);
										// 
					// FOR(i, chead-1) {
					// 	if(cs[i+1] < cs[i]) LOG("DUPA %d\n", i);
					// }
	//LOG("Sorted: %d\n", is_sorted(cs, cs+chead));
	FOR(i, n+1) {
		id[i] = i;
		w[i] = 1;
	}
	int cnt = n+1;
	long long cost = 0;
	FOR(i, chead) {
		if(cnt == 1) break;
		int a = get(cs[i].a), b = get(cs[i].b);
		if(a != b) {
			join(a, b);
			cost += cs[i].c;
			cnt--;
		}
	}
	printf("%lld\n", cost);
	return 0;
}