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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <cstdio>
#include <queue>

using std::priority_queue;

int begins[3000] = { 0 };
int ends[3000] = { 0 };

struct Hint
{
  int begin;
  int end;
  int cost;

  bool operator<(const Hint & that) const
  {
    return this->cost > that.cost;
  }
};

bool containsRange(int begin, int end)
{
  int nextEnd = ends[begin];
  while (true)
  {
    if (nextEnd == end)
      return true;
    if (nextEnd == 0 || nextEnd > end)
      return false;

    nextEnd = ends[nextEnd + 1];
  }
}

void pushRange(int begin, int end)
{
  //if (begin > end) {
  //  printf("AAAAAAAAAAAAAAAAAAAAAAAAAAAAA!\n");
  //  return;
  //}
  if (containsRange(begin, end))
    return;

  int oldEnd = ends[begin];
  int oldBegin = begins[end];

  if (oldBegin == 0 && oldEnd == 0)
  {
    ends[begin] = end;
    begins[end] = begin;
    return;
  }

  ends[begin] = 0;
  ends[oldBegin] = 0;
  begins[end] = 0;
  begins[oldEnd] = 0;

  if (oldBegin == 0)
  {
    if (oldEnd < end)
    {
      pushRange(begin, oldEnd);
      pushRange(oldEnd + 1, end);
    }
    else
    {
      pushRange(begin, end);
      pushRange(end + 1, oldEnd);
    }
  }
  else if (oldEnd == 0)
  {
    if (oldBegin < begin)
    {
      pushRange(oldBegin, begin - 1);
      pushRange(begin, end);
    }
    else
    {
      pushRange(begin, oldBegin - 1);
      pushRange(oldBegin, end);
    }
  }
  else if (oldBegin < begin)
  {
    if (oldEnd < end)
    {
      pushRange(oldBegin, begin - 1);
      pushRange(begin, oldEnd);
      pushRange(oldEnd + 1, end);
    }
    else
    {
      pushRange(oldBegin, begin - 1);
      pushRange(begin, end);
      pushRange(end + 1, oldEnd);
    }
  }
  else
  {
    if (oldEnd < end)
    {
      if (oldEnd < oldBegin)
      {
        pushRange(begin, oldEnd);
        pushRange(oldEnd + 1, oldBegin - 1);
        pushRange(oldBegin, end);
      }
      else
      {
        pushRange(begin, oldBegin - 1);
        pushRange(oldBegin, oldEnd);
        pushRange(oldEnd + 1, end);
      }
    }
    else
    {
      pushRange(begin, oldBegin - 1);
      pushRange(oldBegin, end);
      pushRange(end + 1, oldEnd);
    }
  }
}

int main()
{
  int n;
  scanf("%d", &n);
  priority_queue<Hint> hints;
  for (int i = 0; i < n; i++)
    for (int j = i; j < n; j++)
    {
      int c;
      scanf("%d", &c);
      Hint hint;
      hint.begin = i + 1;
      hint.end = j + 1;
      hint.cost = c;
      hints.push(hint);
    }

  int validHintsLeft = n;
  long long int wholeCost = 0;
  while (validHintsLeft)
  {
    Hint hint = hints.top();
    hints.pop();

    if (containsRange(hint.begin, hint.end))
      continue;

    wholeCost += hint.cost;
    validHintsLeft--;

    pushRange(hint.begin, hint.end);
  }
  
  printf("%lld\n", wholeCost);

  return 0;
}