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
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

struct triple {
  unsigned short a,b;
  int c;
  triple(unsigned short a, unsigned short b, int c) :
    a(a), b(b), c(c) {}
  bool operator< (const triple& o) const {
    return c < o.c;
  }
};

long long solve(size_t N, vector<triple>& T) {
  sort(T.begin(), T.end());
  vector<bool> S((N+1)*(N+1), false);
  long long cost=0;
  vector<unsigned short> ends(N+1, 0);
  for(auto& t : T) {
    //cerr << t.a << " " << t.b << " " << t.c << endl;
    unsigned short a = t.a;
    unsigned short b = t.b;
    while (a<=b and ends[b]>0) {
      //cerr << " " << a << " " << b << endl;
      if (S[(N+1)*a+b]) {
        a = b+1;
        break;
      }
      S[(N+1)*a+b] = true;
      if (ends[b] < a) {
        auto c = ends[b];
        b = a-1;
        a = c;
      } else
        b = ends[b]-1;
    }
    if (a>b)
      continue;
    if (a>1 and ends[a-1] > 0)
      a = ends[a-1];
    //cerr << " ADD " << a << " " << b << " " << t.c << endl;
    ends[b] = a;
    cost += t.c;
  }
  return cost;
}

void onecase() {
  size_t N;
  cin >> N;
  vector<triple> T;
  for(size_t i=1;i<=N;i++)
    for(size_t j=i;j<=N;j++) {
      int c;
      cin >> c;
      T.emplace_back(i,j,c);
    }
  cout << solve(N, T) << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  onecase();
/*
  size_t Z;
  cin >> Z;
  while(Z--)
    onecase();
*/
  return 0;
}