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
// Piotr Golda
#define _CRT_SECURE_NO_WARNINGS  
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
#define LLD long long int

#define MAX_NUM 2000
class Range;
int N;
int Set[MAX_NUM+1];

priority_queue< Range > Queue;


class Range
{
public:
	int m_a;
	int m_b;
	int m_price;

	Range(){}

	Range(int p_a,int p_b, int p_price)
		: m_a(p_a), m_b(p_b), m_price(p_price){}

	bool operator<(const Range& p_rhs) const
	{
		if(this->m_price <= p_rhs.m_price)
			return false;
		return true;
	}
};

void scan()
{
	scanf("%d", &N);
	Set[N] = N;
	int price;
	for(int i = 0; i < N; ++i)
	{
		Set[i] = i;
		for(int j = 0; j < N - i; ++j)
		{
			scanf( "%d", &price);
			Queue.push( Range(i, i+j+1, price) );
		}
	}
}

void merge( int p_setA, int p_setB )
{
	for(int i = 0; i <= N; i++)
	{
		if( Set[i] == p_setB )
			Set[i] = p_setA;
	}
}

LLD computeRangePrices()
{
	int usedRanges = 0;
	Range tmp;
	LLD Price = 0;
	while( usedRanges < N )
	{
		tmp = Queue.top();

		if( Set[tmp.m_a] != Set[tmp.m_b] )
		{
			Price += static_cast<LLD>(tmp.m_price);
			merge( Set[tmp.m_a], Set[tmp.m_b] );
			usedRanges++;
		}

		Queue.pop();
	}
	return Price;
}

int main()
{
	scan();
	printf("%lld\n", computeRangePrices());

	return 0;
}