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
#include <iostream>
#include <stdio.h>
#include <limits>
#include <algorithm>

const int MAXN = 2000;

int c[MAXN + 1][MAXN + 1];
int n;

long long d[MAXN + 1][MAXN + 1];

long long get_cost(int s, int e) {
	if (s > e) return 0;

	if (d[s][e] != 0) {
		return d[s][e];
	}

	long long result = std::numeric_limits<long long>::max();;

	if (s == e) {
		result = c[s][e];
	} else {
		for (int i = s; i < e; ++i) {
			result = std::min(result, get_cost(s, i) + get_cost(i + 1, e));
		}

		for (int i = s; i <= e; ++i) {
			result = std::min(result, get_cost(s, i - 1) + get_cost(i + 1, e) + c[s][e]);
		}
	}

	d[s][e] = result;
	return result;
}

int main() {
	scanf("%i", &n);

	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= (n + 1 - i); ++j) {
			scanf("%i", &c[i][n - j + 1]);
		}
	}

	printf("%lli\n", get_cost(1, n));

	return 0;
}