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
/* 
 * File:   kug.cpp
 * Author: baca
 *
 * Created on 13 maj 2014, 16:47
 */

#include <cstdlib>
#include <iostream>
#include <vector>
#include <utility>
#include <cstring>
#include <algorithm>

using namespace std;

int n, i, j, k, l = 0, x, from, to;

long long tmp, suma = 0;

bool b;

vector< pair<long long, pair<int, int>  > > c;

bool comp_c(pair<long long, pair<int, int> > c1, pair<long long, pair<int, int> > c2) {
    return c1.first < c2.first;
}

int main(int argc, char** argv) {
    cin.sync_with_stdio(false);
    
    scanf("%d", &n);
    
    c.reserve(n * (n - 1) / 2);
    
    for (i = 1; i <= n; i++) {
        for (j = i; j <= n; j++) {
            scanf("%lld", &tmp);
            
            c.push_back(make_pair(tmp, make_pair(i, j)));
        }
    }
    
    sort(c.begin(), c.end(), comp_c);
    
//    for (vector< pair<long long, pair<int, int>  > >::iterator it = c.begin(); it!=c.end(); ++it) {
//        printf("%lld %d %d\n", it->first, it->second.first, it->second.second);
//    }
    
    bool ** s;
    
    s = new bool*[n + 1];
    for (i = 1; i <= n; i++) {
        s[i] = new bool[n + 1];
        memset(s[i], 0, n + 1);
    }
    
    vector< pair<long long, pair<int, int>  > >::iterator it = c.begin();
    while (l < n && it!=c.end()) {
        from = it->second.first;
        to = it->second.second;
        
        b = true;
        
        for (k = 1; k < from; k++) {
            if (s[k][from - 1] && s[k][to]) {
                b = false;
                break;
            }
        }
        
        if (b) {
            for (k = from; k < to; k++) {
                if (s[from][k] && s[k + 1][to]) {
                    b = false;
                    break;
                }
            }
        }
        
        if (b) {
            for (k = to + 1; k <= n; k++) {
                if (s[from][k] && s[to + 1][k]) {
                    b = false;
                    break;
                }
            }
        }
        
        if (b) {
            suma += it->first;
            ++l;
            
            s[from][to] = true;
            
            for (k = 1; k < from; k++) {
                if (s[k][from - 1]) {
                    s[k][to] = true;
                } else if (s[k][to]) {
                    s[k][from - 1] = true;
                }
            }
            
            for (k = from; k < to; k++) {
                if (s[from][k]) {
                    s[k + 1][to] = true;
                } else if (s[k + 1][to]) {
                    s[from][k] = true;
                }
            }
            
            for (k = to + 1; k <= n; k++) {
                if (s[from][k]) {
                    s[to + 1][k] = true;
                } else if (s[to + 1][k]) {
                    s[from][k] = true;
                }
            }
        }
        
        ++it;
    }
    
    printf("%lld\n", suma);
    
    return 0;
}