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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <limits>
#include <climits>
#include <map>
#include <set>
#include <vector>

const long long LLMAX = (std::numeric_limits<long long>()).max();
const long long LLMIN = (std::numeric_limits<long long>()).min();

int n;

struct Cost {
    int i;
    int j;
    int c;
};
const int MaxCostsLen = 2000 * (2000 + 1) / 2;
Cost costs[MaxCostsLen];
Cost* costsSorted[MaxCostsLen];
int costsLen;

std::vector<Cost*> startsAt[2000];
std::vector<Cost*> endsAt[2000];

std::set<int> possibleStarts;
std::set<int> possibleEnds;

bool costsSortedCmp(Cost* x, Cost* y)
{
    if (x->c != y->c) { return x->c < y->c; }
    else              { return x->j - x->i < y->j - y->i; }
}

void goBack(int x)
{
    if (x < 0) {
        return;
    }

    for (int i=0; i < endsAt[x].size(); ++i) {
        int start = endsAt[x][i]->i;
        possibleStarts.insert(start);
        goBack(start - 1);
    }
}

void goForward(int x)
{
    if (x >= n) {
        return;
    }

    for (int i=0; i < startsAt[x].size(); ++i) {
        int end = startsAt[x][i]->j;
        possibleEnds.insert(end);
        goForward(end + 1);
    }
}

bool tryReach(int x)
{
    if (x >= n) {
        return false;
    }

    for (int i=0; i < startsAt[x].size(); ++i) {
        int end = startsAt[x][i]->j;
        if (possibleEnds.count(end) == 1) {
            return true;
        } else {
            bool reached = tryReach(end + 1);
            if (reached) {
                return true;
            }
        }
    }

    return false;
}

int main()
{
    std::ios_base::sync_with_stdio(false);

    scanf("%d", &n);
    for (int i=0; i < n; ++i) {
        for (int j=i; j < n; ++j) {
            int c; scanf("%d", &c);
            costs[costsLen].i = i;
            costs[costsLen].j = j;
            costs[costsLen].c = c;

            costsSorted[costsLen] = &costs[costsLen];

            ++costsLen;
        }
    }

    std::sort(
        costsSorted,
        costsSorted + costsLen,
        costsSortedCmp
    );

    /*for (int i=0; i < costsLen; ++i) {
        printf("%d %d %d\n", costsSorted[i]->i + 1, costsSorted[i]->j + 1, costsSorted[i]->c);
    }*/

    long long result = 0;
    int includedCount = 0;
    for (int i=0; includedCount < n && i < costsLen; ++i) {
        Cost* curr = costsSorted[i];

        possibleStarts.clear();
        possibleEnds.clear();
        possibleStarts.insert(curr->i);
        possibleEnds.insert(curr->j);
        goBack(curr->i - 1);
        goForward(curr->j + 1);

        bool canInclude = true;
        for (std::set<int>::iterator it=possibleStarts.begin(); it != possibleStarts.end(); ++it) {
            bool reached = tryReach(*it);
            if (reached) {
                canInclude = false;
                break;
            }
        }

        if (canInclude) {
            startsAt[curr->i].push_back(curr);
            endsAt[curr->j].push_back(curr);
            result += curr->c;

            ++includedCount;
        }
    }
    printf("%lld\n", result);
}