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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include <cstdlib>
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#include <climits>
#include <cstdio>
#include <utility>
#include <algorithm>
#include <stack>
#include <vector>
#include <set>

using namespace std;


struct IntTripple {
  int f, s, t;
  IntTripple() : f(0), s(0), t(0) { }
  IntTripple(IntTripple const &o) : f(o.f), s(o.s), t(o.t) { }
  IntTripple(int const &f, int const &s, int const &t) : f(f), s(s), t(t) { }
  ~IntTripple() { }
  
  bool operator<(IntTripple const &o) const {
    if(f < o.f) return true;
    if(f > o.f) return false;
    if(s < o.s) return true;
    if(s > o.s) return false;
    if(t < o.t) return true;
    if(t > o.t) return false;
    return false;
  }
  
  bool operator==(IntTripple const &o) const {
    return (f == o.f and s == o.s and t == o.t);
  }
  
  IntTripple& operator=(IntTripple const &o) {
    f = o.f;
    s = o.s;
    t = o.t;
    return *this;
  }
};


enum Side {LEFT=1, RIGHT=2, ANY=3};


void disable_pickability(int const &i0, int const &j0, int const &n, bool (&pickable)[2048][2048]) {
  stack< pair<int,int> > st;
  
  st.push(make_pair(i0, j0));
  
  // printf("DISABLEFROM %d %d\n", i0, j0);
  
  while(not st.empty()) {
    int i = st.top().first;
    int j = st.top().second;
    st.pop();
    
    if(not pickable[i][j]) continue;
    if(i < 0 or i >= n or j < 0 or j >= n - i) continue;
  
    // printf("DISABLE %d %d\n", i, j);
    
    pickable[i][j] = false;
    
    // RIGHT
    if(j < n - i - 1) {
      if(not pickable[i  ][j+1]) {
        st.push(make_pair(i+1, j  ));
        // st.push(make_pair(i-1, j+1));
      }
    }
    
    // TOP RIGHT
    if(i > 0 and j < n - i - 1) {
      if(not pickable[i-1][j+1]) {
        // st.push(make_pair(i  , j+1));
        st.push(make_pair(i-1, j  ));
      }
    }
    
    // TOP LEFT
    if(i > 0) {
      if(not pickable[i-1][j]) {
        st.push(make_pair(i-1, j+1));
        // st.push(make_pair(i  , j-1));
      }
    }
    
    // LEFT
    if(j > 0) {
      if(not pickable[i  ][j-1]) {
        // st.push(make_pair(i-1, j  ));
        st.push(make_pair(i+1, j-1));
      }
    }
    
    // BOTTOM LEFT
    if(i < n-1 and j > 0) {
      if(not pickable[i+1][j-1]) {
        st.push(make_pair(i  , j-1));
        // st.push(make_pair(i+1, j  ));
      }
    }
    
    // BOTTOM RIGHT
    if(i < n-1 and j < n - i - 1) {
      if(not pickable[i+1][j  ]) {
        // st.push(make_pair(i+1, j-1));
        st.push(make_pair(i  , j+1));
      }
    }
  }
}


int main(int const /*argc*/, char const * const * const /*argv*/) {
  static int array[2048][2048];
  static bool pickable[2048][2048];
  int n;
  
  scanf("%d", &n);
  
  for(int i = 0; i < n; ++i) {
    for(int j = 0; j < n - i; ++j) {
      scanf("%d", &(array[i][j]));
      pickable[i][j] = true;
    }
  }
  
  set<IntTripple> availableLeft;
  set<IntTripple> availableRight;
  
  int h = n >> 1;
  
  int64_t result = 0;
  
  for(int j = 0; j < h; ++j) {
    for(int i = 0; i < n - j; ++i) {
      if(pickable[i][j]) availableLeft.insert(IntTripple(array[i][j], i, j));
      if(pickable[i][(n-1-i)-j]) availableRight.insert(IntTripple(array[i][(n-1-i)-j], i, (n-1-i)-j));
    }
    
    while(not availableLeft.empty() and not pickable[availableLeft.begin()->s][availableLeft.begin()->t]) availableLeft.erase(availableLeft.begin());
    while(not availableRight.empty() and not pickable[availableRight.begin()->s][availableRight.begin()->t]) availableRight.erase(availableRight.begin());
    
    // printf("SIZES %lu %lu\n", availableLeft.size(), availableRight.size());
    
    IntTripple cL = *(availableLeft.begin());
    IntTripple cR = *(availableRight.begin());
    Side nextPick;
    
    if(cL < cR) {
      if(availableRight.find(cL) != availableRight.end()) {
        nextPick = ANY;
        availableRight.erase(cL);
      } else {
        nextPick = RIGHT;
      }
      availableLeft.erase(cL);
      result += cL.f;
      disable_pickability(cL.s, cL.t, n, pickable);
      // printf("PICK %d;%d,%d\n", cL.f, cL.s, cL.t);
    } else {
      if(availableLeft.find(cR) != availableLeft.end()) {
        nextPick = ANY;
        availableLeft.erase(cR);
      } else {
        nextPick = LEFT;
      }
      availableRight.erase(cR);
      result += cR.f;
      disable_pickability(cR.s, cR.t, n, pickable);
      // printf("PICK %d;%d,%d\n", cR.f, cR.s, cR.t);
    }
    
    while(not availableLeft.empty() and not pickable[availableLeft.begin()->s][availableLeft.begin()->t]) availableLeft.erase(availableLeft.begin());
    while(not availableRight.empty() and not pickable[availableRight.begin()->s][availableRight.begin()->t]) availableRight.erase(availableRight.begin());
    
    if(nextPick == ANY and availableLeft.empty())  nextPick = RIGHT;
    if(nextPick == ANY and availableRight.empty()) nextPick = LEFT;
    
    IntTripple c;
    if(nextPick == ANY) {
      c = min(*(availableLeft.begin()), *(availableRight.begin()));
    } else if(nextPick == LEFT) {
      c = *(availableLeft.begin());
    } else {
      c = *(availableRight.begin());
    }
    
    availableLeft.erase(c);
    availableRight.erase(c);
    result += c.f;
    disable_pickability(c.s, c.t, n, pickable);
    // printf("PICK %d;%d,%d\n", c.f, c.s, c.t);
    
    for(int jj = 0; jj <= j; ++jj) {
      availableLeft.erase(IntTripple(array[j-jj][jj], j-jj, jj));
      // printf("LREMOVE %d;%d,%d\n", array[j-jj][jj], j-jj, jj);
      availableRight.erase(IntTripple(array[j-jj][(n-1-(j-jj))-jj], j-jj, (n-1-(j-jj))-jj));
      // printf("RREMOVE %d;%d,%d\n", array[j-jj][(n-1-(j-jj))-jj], j-jj, (n-1-(j-jj))-jj);
    }
  }
  
  if(n & 1) {
    int j = h;
    
    for(int i = 0; i < n - j; ++i) {
      if(pickable[i][j]) availableLeft.insert(IntTripple(array[i][j], i, j));
      if(pickable[i][(n-1-i)-j]) availableRight.insert(IntTripple(array[i][(n-1-i)-j], i, (n-1-i)-j));
    }
    
    Side nextPick = ANY;
    IntTripple c;
    
    while(not availableLeft.empty() and not pickable[availableLeft.begin()->s][availableLeft.begin()->t]) availableLeft.erase(availableLeft.begin());
    while(not availableRight.empty() and not pickable[availableRight.begin()->s][availableRight.begin()->t]) availableRight.erase(availableRight.begin());
    
    // printf("SIZES %lu %lu\n", availableLeft.size(), availableRight.size());
    
    if(nextPick == ANY and availableLeft.empty())  nextPick = RIGHT;
    if(nextPick == ANY and availableRight.empty()) nextPick = LEFT;
    
    if(nextPick == ANY) {
      c = min(*(availableLeft.begin()), *(availableRight.begin()));
    } else if(nextPick == LEFT) {
      c = *(availableLeft.begin());
    } else {
      c = *(availableRight.begin());
    }
    
    availableLeft.erase(c);
    availableRight.erase(c);
    result += c.f;
    // disable_pickability(c.s, c.t, n, pickable);
    // printf("PICK %d;%d,%d\n", c.f, c.s, c.t);
  }
  
  printf("%" PRId64 "\n", result);
  
  /* for(int i = 0; i < n; ++i) {
    for(int j = 0; j < n - i; ++j) {
      printf("%d ", array[i][j]);
    }
    printf("\n");
  } */
  
  
  return EXIT_SUCCESS;
}