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
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;

typedef long long ll;
#define left(x) ((x)<<1)
#define right(x) (left(x)+1)
#define parent(x) ((x)>>1)
#define inf 1000000000

inline ll min(ll a, ll b, ll c, ll d){
    return min(a, min(b, min(c, d)));
}

inline ll min(ll a, ll b, ll c){
    return min(a, min(b, c));
}

int c[2001][4100];
int b[2001][2001];
int pos[2001];
int H[2001];
bool o[2002];
ll q[2001];

inline void Heapify(int x, int s){
    int l=x, h=parent(s), y;
    while(x<=h){
        if(q[H[y=left(x)]]<q[H[l]]) l=y;
        if((++y)<=s && q[H[y]]<q[H[l]]) l=y;
        if(l==x) break;
        swap(H[x], H[l]);
        pos[H[x]]=x;
        x=l;
    }
    pos[H[x]]=x;
}

inline void UpHeapify(int x){
    int p;
    while((p=parent(x)) && q[H[x]]<q[H[p]]){
        swap(H[x], H[p]);
        pos[H[x]]=x;
        x=p;
    }
    pos[H[x]]=x;
}

inline void BuildHeap(int s){
    int h=parent(s);
    for(int i=h;i;--i)
        Heapify(i, s);
}

inline int ExtractMin(int& s){
    int res=H[1];
    swap(H[1], H[s--]);
    pos[H[1]]=1;
    Heapify(1, s);
    return res;
}

inline int InitTreeSize(int n){
    if(n&(n-1)){
        frexp(n, &n);
        n=1<<n;
    }
    return n;
}

inline void InitTree(int j, int s){
    for(int i=s, x=(s<<1);i<x;++i)
        if(!c[j][i]) c[j][i]=inf;
    for(int i=s-1;i;--i)
        c[j][i]=min(c[j][left(i)], c[j][right(i)]);
}

inline int Query(int j, int a, int b){
    int res=inf;
    while(a<b){
        if(a&1) res=min(res, c[j][a++]);
        if(!(b&1)) res=min(res, c[j][b--]);
        a>>=1;
        b>>=1;
    }
    if(a==b) res=min(res, c[j][a]);
    return res;
}

int main()
{
    ll res=0;
    int n, s, m;
    scanf("%d", &n);
    m=n;
    s=InitTreeSize(n);
    for(int i=1;i<=n;++i){
        scanf("%d", &c[i+s][i+s]);
        q[i]=c[i+s][i+s];
        pos[i]=i;
        H[i]=i;
        for(int j=i+1;j<=n;++j)
            scanf("%d", &c[i+s][j+s]);
    }
    for(int i=1;i<=n;++i)
        InitTree(i, s);
    BuildHeap(n);
    for(int i=2;i<=n;++i){
        b[i-1][i]=c[i+s-1][i+s];
        for(int j=i-2;j;--j)
           b[j][i]=min(c[j+s][i+s], b[j+1][i]);
    }
    while(m){
        int k=ExtractMin(m);
        res+=q[k];
        o[k]=1;
        int r=k, l=k;
        ll tmp;
        while(o[r]) ++r;
        while(o[l]) --l;
        if(l){
            tmp=min(q[l], (ll)Query(l, l, r-1));
            if(l>1 && o[l-1]){
                int l2=l-1;
                while(o[l2]) --l2;
                for(++l2;l2<l;++l2)
                    tmp=min(tmp, (ll)Query(l2, l+1, r-1));
            }
            if(tmp<q[l]){
                q[l]=tmp;
                UpHeapify(pos[l]);
            }
        }
        if(r<=n){
            tmp=min(q[r], (ll)b[l+1][r]);
            if(r<n && o[r+1]){
                int r2=r+1;
                while(o[r2]) ++r2;
                --r2;
                for(int i=l+1;i<r;++i)
                    tmp=min(tmp, (ll)Query(i, r+1, r2));
            }
            if(tmp<q[r]){
                q[r]=tmp;
                UpHeapify(pos[r]);
            }
        }
    }
    printf("%lld", res);
}