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

long long tab[2000][2000];

long long min[2000][2];

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

	for(int i = 0; i < n; ++i)
		for(int j = i; j < n; ++j)
		{
			scanf("%ld", &tab[i][j]);

			if(i == 0)
			{
				min[j][0] = tab[i][j];
				min[j][1] = i;
			}

			if(tab[i][j] <= min[j][0])
			{
				min[j][0] = tab[i][j];
				min[j][1] = i;
			}
		}

	if(n == 1) //only one cup
	{
		printf("%lld\n", tab[0][0]);
		return 0;
	}

	int maxColumn = 0;
	long long sumOfMins = 0;

	for(int i = 0; i < n; ++i)
	{
		sumOfMins += min[i][0];
		if(min[i][0] > min[maxColumn][0])
			maxColumn = i;
	}

	if(maxColumn == n - 1) //last column
	{
		printf("%lld\n", sumOfMins);
		return 0;
	}

	auto newSum = sumOfMins;

	auto val = tab[maxColumn + 1][maxColumn + 1];

	newSum += val;
	newSum -= min[maxColumn + 1][0];
	newSum -= min[maxColumn][0];

	long minVal = tab[0][maxColumn + 1];
	
	for(int row = 0; row <= maxColumn; ++row)
		for(int col = maxColumn + 1; col < n; ++col)
		{
			if(tab[row][col] < minVal && min[col][1] != row)
				minVal = tab[row][col];
		}


	newSum += minVal;

	if(newSum < sumOfMins )
	{
		sumOfMins = newSum;
	}

	printf("%lld\n", sumOfMins);
}