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
import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

/**
 *
 * @author Marcin Lewandowski
 *
 */
public class kug {

    public static void main(String[] args) throws IOException {
        int n = readInt(System.in);

        int[][] costs = new int[n][n];
        TreeSet<Integer> sortedCosts = new TreeSet<Integer>();
        for (int i = 0; i < n; i++) {
            for (int j = i; j < n; j++) {
                costs[i][j] = readInt(System.in);
                sortedCosts.add(costs[i][j]);
            }
        }
        HashSet<GroupInfo> found = new HashSet<GroupInfo>();
        Iterator<Integer> iterator = sortedCosts.iterator();
        search:
        while (iterator.hasNext()) {
            int cost = iterator.next();
            for (int i = 0; i < n; i++) {
                for (int j = i; j < n; j++) {
                    if (costs[i][j] == cost) {
                        GroupInfo groupInfo = new GroupInfo(i, j, cost);
                        if (isWorthBuying(groupInfo, found)) {
                            found.add(groupInfo);
                            if (found.size() == n) {
                                break search;
                            }
                        }
                    }
                }
            }

        }

        long sum = 0;
        for (GroupInfo gi : found) {
            sum += gi.cost;
        }
        System.out.println(sum);
    }

    private static int readInt(InputStream in) throws IOException {
        int result = 0;
        boolean isDigit = false;
        int c = 0;
        while ((c = in.read()) != -1) {
            if (c >= '0' && c <= '9') {
                isDigit = true;
                result = result * 10 + c - '0';
            } else if (isDigit) {
                break;
            }
        }
        return result;
    }

    private static class GroupInfo {

        int from;
        int to;
        int cost;

        public GroupInfo(int from, int to, int cost) {
            this.from = from;
            this.to = to;
            this.cost = cost;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final GroupInfo other = (GroupInfo) obj;
            if (this.from != other.from) {
                return false;
            }
            if (this.to != other.to) {
                return false;
            }
            return true;
        }

        @Override
        public int hashCode() {
            int hash = 5;
            hash = 83 * hash + this.from;
            hash = 83 * hash + this.to;
            return hash;
        }
    }


    private static boolean isWorthBuying(GroupInfo info, Set<GroupInfo> infos) {
        boolean infoCoverage[] = new boolean[info.to - info.from + 1];
        for (GroupInfo i : infos) {

            if ((i.from == info.from && i.to >= info.to) || (i.from <= info.from && i.to == info.to)) {
                boolean[] coverage = new boolean[i.to - i.from + 1];
                for (GroupInfo i2 : infos) {
                    if ((i2.from >= i.from && i2.to <= i.to) && !i2.equals(i)) {
                        for (int j = i2.from; j <= i2.to; j++) {
                            coverage[j - i.from] = true;
                        }
                    }
                }
                for (int j = info.from; j <= info.to; j++) {
                    coverage[j - i.from] = true;
                }
                boolean flag = true;
                for (int j = 0; j < coverage.length; j++) {
                    flag = flag & coverage[j];
                }
                if (flag) {
                    return false;
                }
            } else if (i.from >= info.from && i.to <= info.to) {
                for (int j = i.from; j <= i.to; j++) {
                    infoCoverage[j - info.from] = true;
                }
            }
        }
        boolean flag = true;
        for (int j = 0; j < infoCoverage.length; j++) {
            flag = flag & infoCoverage[j];
        }
        if (flag) {
            return false;
        }
        return true;
    }
}