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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <algorithm>
#include <list>

#define USED 1
#define ALL_KNOWN 0

using namespace std;

class Range {
public:
	int cost;
	short x;
	short y;
};

bool compare_ranges(Range r1, Range r2) {
	return r1.cost < r2.cost;
}

int ranges_diff(Range &r1, Range &r2, bool* known, int &known_num, int &n) {
	//printf("    ranges diff=");
	int diff = 0;
	int diff_pos;
	int min = r1.x < r2.x ? r1.x : r2.x;
	int max = r1.y > r2.y ? r1.y : r2.y;
	if (r1.y >= r2.x || r2.y >= r1.x) {
		for (int i = min; i <= max; i++) {
			if (!known[i] && ((i >= r1.x && i <= r1.y) ^ (i >= r2.x && i <= r2.y))) {
				diff++;
				diff_pos = i;
			}
		}
		//printf("%d\n", diff);
		if (diff == 1) {
			known[diff_pos] = true;
			known_num++;
			//printf("known_num=%d (", known_num);
			//for (int i = 0; i < n; i++) {
				//printf(known[i] ? "1" : "0");
			//}
			//printf("\n");
		}
	//} else {
		//printf("no overlap\n");
	}
	return diff;
}

int range_diff(Range &r1, bool* known, int &known_num, int &n) {
	//printf("    range diff=");
	int diff = 0;
	int diff_pos;
	for (int i = r1.x; i <= r1.y; i++) {
		if (!known[i]) {
			diff++;
			diff_pos = i;
		}
	}
	//printf("%d\n", diff);
	if (diff == 1) {
		known[diff_pos] = true;
		known_num++;
		//printf("known_num=%d (", known_num);
		//for (int i = 0; i < n; i++) {
			//printf(known[i] ? "1" : "0");
		//}
		//printf("\n");
	}
	return diff;
}

bool used_completely(Range &r1, bool* known) {
	for (int i = r1.x; i < r1.y; i++) {
		if (!known[i]) {
			return false;
		}
	}
	return true;
}

int main() {
	int n;
	scanf("%d", &n);
	list<Range> ranges;
	bool covered[n + 1];
	for (int i = 0; i < n; i++) {
		for (int j = i; j < n; j++) {
			Range r;
			scanf("%d", &r.cost);
			r.x = i;
			r.y = j;
			ranges.push_back(r);
		}
	}
	ranges.sort(compare_ranges);
//	int z = 0;
//	for (list<Range>::iterator it = ranges.begin(); it != ranges.end() && z++ < 100; ++it) {
//		printf("%d-%d (%d)\n", it->x, it->y, it->cost);
//	}
	
	int known_num = 0;
	bool known[n];
	memset(known, 0, n);
	list<Range> ranges_used;
	vector<Range> ranges_used_completely;
	int diff = -1;
	while (known_num < n) {
		bool mod = false;
		
		//printf("rechecking partially used ranges\n");

		for (list<Range>::iterator it = ranges_used.begin(); !mod && it != ranges_used.end();) {
			//printf(" partially used range %d-%d (%d)\n", it->x, it->y, it->cost);
			for (list<Range>::iterator it2 = ranges_used.begin(); !mod && it2 != it;) {
				//printf("  vs partially used range %d-%d (%d)\n", it2->x, it2->y, it2->cost);
				diff = ranges_diff(*it, *it2, known, known_num, n);
				if (diff == USED) {
					//printf("   matched range 1 ");
					if (used_completely(*it, known)) {
						//printf(" (completely)");
						ranges_used_completely.push_back(*it);
						ranges_used.erase(it);
						mod = true;
					}
					//printf("with range 2 ");
					if (used_completely(*it2, known)) {
						//printf("(completely)");
						ranges_used_completely.push_back(*it2);
						ranges_used.erase(it2);
						mod = true;
					}
					//printf("\n");
				} 
				if (!mod) {
					++it2;
				}
			}
			if (!mod) {
				//printf("  checking on its own\n");
				diff = range_diff(*it, known, known_num, n);
				if (diff == USED || diff == ALL_KNOWN) {
					//printf("   matched: ");
					if (used_completely(*it, known)) {
						//printf("completely\n");
						ranges_used_completely.push_back(*it);
						ranges_used.erase(it);
					} else {
						//printf("partially\n");
					}
					mod = true;
				}
			}

			if (!mod) {
				++it;
			}
		}

		for (list<Range>::iterator it = ranges.begin(); !mod && it != ranges.end();) {
			//printf("considering range %d-%d (%d)\n", it->x, it->y, it->cost);
			int diff = range_diff(*it, known, known_num, n);
			if (diff == ALL_KNOWN) {
				//printf(" all known, removing\n");
				it = ranges.erase(it);
				continue;
			} else if (diff == USED) {
				//printf(" using single range %d-%d (%d)", it->x, it->y, it->cost);
				if (used_completely(*it, known)) {
					//printf(" (completely)\n");
					ranges_used_completely.push_back(*it);
				} else {
					//printf("\n");
					ranges_used.push_back(*it);
				}
				ranges.erase(it);
				mod = true;
			} else {
				for (list<Range>::iterator it2 = ranges_used.begin(); !mod && it2 != ranges_used.end();) {
					//printf(" vs partially used range %d-%d (%d)\n", it2->x, it2->y, it2->cost);
					diff = ranges_diff(*it, *it2, known, known_num, n);
					if (diff == USED) {
						//printf("  matched range 1 ");
						if (used_completely(*it, known)) {
							//printf(" (completely)");
							ranges_used_completely.push_back(*it);
						} else {
							ranges_used.push_back(*it);
						}
						ranges.erase(it);
						mod = true;
						//printf("with range 2 ");
						if (used_completely(*it2, known)) {
							//printf("(completely)");
							ranges_used_completely.push_back(*it2);
							ranges_used.erase(it2);
						}
						//printf("\n");
					} 
					if (!mod) {
						++it2;
					}
				}
			}
			if (!mod) {
				for (list<Range>::iterator it2 = ranges.begin(); !mod && it2 != it;) {
					//printf(" vs range %d-%d (%d)\n", it2->x, it2->y, it2->cost);
					diff = ranges_diff(*it, *it2, known, known_num, n);
					if (diff == USED) {
						//printf("  matched range 1 ");
						if (used_completely(*it, known)) {
							//printf(" (completely)");
							ranges_used_completely.push_back(*it);
						} else {
							ranges_used.push_back(*it);
						}
						ranges.erase(it);
						//printf("with range 2 ");
						if (used_completely(*it2, known)) {
							//printf("(completely)");
							ranges_used_completely.push_back(*it2);
						} else {
							ranges_used.push_back(*it2);
						}
						ranges.erase(it2);
						//printf("\n");
						mod = true;
					} 
					if (!mod) {
						++it2;
					}
				}
			}
			if (!mod) {
				++it;
			}
		}
	}
	long long cost = 0;
	for (vector<Range>::iterator it = ranges_used_completely.begin(); it != ranges_used_completely.end(); ++it) {
		long long cur_cost = it->cost;
		cost += cur_cost;
		//printf("%d-%d (%d)\n", it->x, it->y, it->cost);
	}
	for (list<Range>::iterator it = ranges_used.begin(); it != ranges_used.end(); ++it) {
		long long cur_cost = it->cost;
		cost += cur_cost;
		//printf("%d-%d (%d)\n", it->x, it->y, it->cost);
	}
	printf("%lld\n", cost);
}