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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

// shit, shit, shit
public class kug {

    private static long solve(int n, long[][] c)
    {
        List<C> cs = new ArrayList<>(n * (n + 1) >> 1);
        int i, j;
        for (i=0;i<n;i++) for (j=i;j<n;j++) {
            cs.add(new C(c[i][j], i, j));
        }
        Collections.sort(cs);
        State S = new State(n);
        long res = 0;
        for (C cv : cs)
        {
            if (S.isUsable(cv)) {
                S.use(cv);
                res += cv.value;
            }
            if (S.isOver()) break;
        }
//        System.err.println(S.toStringWalled());
//        System.err.println(S.toStringCovered());
        if (!S.isOver()) throw new RuntimeException("FAILED ...");
        return res;
    }

    public static void main(String[] args) throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        long[][] c = new long[n][n];
        for (int i=0; i< n; i++)
        {
            String[] cs = br.readLine().split(" ");
            for (int j=0; j<cs.length;j++) {
                c[i][i+j] = Long.parseLong(cs[j]);
            }
        }
        System.out.println(solve(n, c));
        System.out.flush();
    }

    private static void print(long[][] a)
    {
        for (long[] r: a)
        {
            System.err.println(Arrays.toString(r));
        }
    }

    public static class C implements Comparable<C>
    {
        public C(long value, int i, int j)
        {
            this.value = value;
            this.i = i;
            this.j = j;
        }

        public long value;
        public int i;
        public int j;

        @Override
        public int compareTo(C o) {
            return Long.compare(value, o.value);
        }

        @Override
        public String toString()
        {
            return "" + value + "[" + i + "][" + j + "]";
        }
    }

    public static class State
    {
        public State(int n)
        {
            this.n = n;
            covered = new boolean[n]; //size n
            walled = new HashSet<>(n+1); //size n+1
        }

        public boolean isUsable(C c)
        {
            boolean wouldCover = false;
            for (int i=c.i;i<=c.j;i++) if (!covered[i]) {
                wouldCover = true;
                break;
            }
            boolean wouldWall = (!walled.contains(c.i)) || (!walled.contains(c.j+1));
            return wouldCover || wouldWall;
        }

        public void use(C c)
        {
            walled.add(c.i);
            walled.add(c.j+1);
            for (int i=c.i; i<= c.j ; i++) {
                if (!covered[i]) {
                    covered[i] = true;
                    covred_count++;
                }
            }
        }

        public boolean isOver()
        {
            // missing disambiguity solving :/
            return walled.size() == n+1 && covred_count == n;
        }

        public String toStringWalled()
        {
            return walled.toString();
        }

        public String toStringCovered()
        {
            return covered.toString();
        }

        private int covred_count = 0;
        private int n;
        private boolean[] covered;
        private Set<Integer> walled;
    }

    public static class Range implements Comparable<Range>
    {

        public Range(int x, int y)
        {
            this.x = x;
            this.y = y;
        }

        @Override
        public int compareTo(Range o) {
            return Integer.compare(x, o.x);
        }

        public boolean overlap(Range o)
        {
            if (x <= o.x && y >= o.x) return true;
            if (o.x <= x && o.y >= x) return true;
            return false;
        }

        public Range join(Range o)
        {
            return new Range(Math.min(x, o.x), Math.max(y, o.y));
        }

        int x;
        int y;
    }
}