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

#ifdef	DEBUG
#define	DBG(x)	x
#else
#define	DBG(x)
#endif

int n;
unsigned int c[2000][2000];

int i, j;
int run;
uint64_t koszt;
uint64_t koszt_min;

typedef struct {
	int i, j;
} prz_t;

prz_t p[2000];

int main(void)
{
	scanf("%d", &n);
	for (i = 0; i < n; i++) {
		for (j = i; j < n; j++) {
			scanf("%u", &c[i][j]);
		}
	}

	for (i = 0; i < n; i++) {
		p[i].i = 0;
		p[i].j = i;
	}

	koszt_min = UINT_MAX;
	for (;;) {
		run = 0;
		for (i = n - 1; i >= 0; i--) {
			/* nie max w p[i] -> break */
			if (p[i].i < i || p[i].j < n - 1) {
				run++;
				break;
			}
		}
		if (!run) {
			break;
		}

		if (p[i].j < n - 1) {
			p[i].j++;
		} else {
			p[i].i++;
			p[i].j = p[i].i;
		}

		for (j = i + 1; j < n; j++) {
			p[j].i = p[j - 1].i;
			p[j].j = p[j - 1].j + 1;
			if (p[j].j >= n) {
				p[j].i++;
				p[j].j = n - 1;
			}
		}

		koszt = 0;
		for (i = 0; i < n; i++) {
			DBG(printf("%d-%d(%u) ", p[i].i+1, p[i].j+1, c[p[i].i][p[i].j]));
			koszt += (uint64_t) c[p[i].i][p[i].j];
		}
		DBG(printf("= %llu\n", koszt));
		if (koszt_min > koszt) {
			koszt_min = koszt;
		}
	}

	printf("%llu\n", koszt_min);
	return 0;
}