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
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct Price {
	Price(int a, int b, int p) : A(a), B(b), P(p) {}
	int A, B, P;

	inline bool operator<(const Price &b) const {
		if(P < b.P)
			return true;

		return false;
	}
};

typedef vector<Price>::iterator priceIter;

int isDetermined(const Price &p, const bool *tab) {
	int notKnown = 0, kubek = 0;
	for(int i=p.A;i<=p.B;++i) {
		if( !tab[i] ) {
			++notKnown;
			kubek = i;
		}

		if( notKnown > 1 )
			return 0;
	}

	if( notKnown == 0 )
		return -1;

	return kubek;
}

int main()
{
	int n, p, isKnown, cost, action;
	bool tab[2001];

	for(int i=0;i<2001;++i)
		tab[i] = false;

	vector<Price> prices;

	scanf("%d", &n);
	for(int i=1;i<=n;++i) {
		for(int j=i;j<=n;++j) {
			scanf("%d", &p);
			prices.push_back(Price(i,j,p));
		}
	}

	sort(prices.begin(), prices.end());

	isKnown = 0;
	cost = 0;

	while(isKnown != n) {
		for(priceIter iter = prices.begin(); iter != prices.end();) {
			action = isDetermined(*iter, tab);

			if( action > 0 ) {
				tab[action] = true;
				++isKnown;
				cost += (*iter).P;
				prices.erase(iter);
				break;
			} else if (action < 0) {
				priceIter tmpIter = iter;
				++iter;
				prices.erase(tmpIter);
			} else {
				++iter;
			}
		}
	}

	printf("%d\n", cost);

	return 0;
}