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
/* 
 * File:   kug.cpp
 * Author: wukat
 *
 * Created on 12 maj 2014, 12:56
 */

#include <cstdlib>
#include <iostream>
#include <vector>
#include <utility>

using namespace std;

vector<vector <unsigned long int> > readFromStdIn(int n, vector<vector <char> > &mark) {
    long int cost;
    vector<vector <unsigned long int> > data;
    for (int i = 0; i < n; i++) {
        vector<char> temp2;
        vector<unsigned long int> temp;
        for (int j = 0; j < n - i; j++) {
            cin >> cost;
            temp.push_back(cost);
            temp2.push_back(' ');
        }
        data.push_back(temp);
        mark.push_back(temp2);
    }
    return data;
}

void replaceIfBetter(int i, int j, vector<vector <unsigned long int> > &data, vector<vector <char> > &mark, int n) {
    unsigned long int temp = 0;
    if (i > 0 && j < n - i) {
        temp = data[i - 1][0] + data[i - 1][j + 1];
    }
    if (data[i][j] > temp && temp != 0) {
        data[i][j] = temp;
        mark[i][j] = 'b';
    }
    if (j < n - i - 2) {
        temp = (data[i][j + 1] + data[i][j + 2] < temp || temp == 0) ? data[i][j + 1] + data[i][j + 2] : temp;
    }
    if (data[i][j] > temp && temp != 0) {
        data[i][j] = temp;
        mark[i][j] = 'r';
    }

}

void rebuildAll(vector<vector <unsigned long int> > &data, vector<vector <char> > &mark, int n) {
    for (int i = 0; i < n; i++) {
        for (int j = n - i - 1; j >= 0; j--) {
            replaceIfBetter(i, j, data, mark, n);
        }
    }
}

bool in(int i, int j, vector<pair<int, int> > &used) {
    for (int k = 0; k < used.size(); k++) {
        if (used[k].first == i && used[k].second == j)
            return true;
    }
    return false;
}

bool inR(int i, int j, vector<pair<int, int> > &used, vector<vector <char> > &mark) {
    if (in(i, j, used))
        return true;
    if (mark[i][j] == 'b') {
        if (inR(i - 1, 0, used, mark) && inR(i - 1, j + 1, used, mark))
            return true;
    } else if (mark[i][j] == 'r') {
        if (inR(i, j + 1, used, mark) && inR(i, j + 2, used, mark))
            return true;
    }
    return false;
}

unsigned long long int sumR(vector<vector <unsigned long int> > &data, vector<vector <char> > &mark, vector<pair<int, int> > &used, int i, int j) {
    unsigned long long int sum = 0;
    if (mark[i][j] == ' ' && !in(i, j, used)) {
        sum += data[i][j];
    } else if (mark[i][j] == 'b') {
        sum += sumR(data, mark, used, i - 1, 0) + sumR(data, mark, used, i - 1, j + 1);
    } else if (mark[i][j] == 'r') {
        sum += sumR(data, mark, used, i, j + 1) + sumR(data, mark, used, i, j + 2);
    }
    return sum;
}

unsigned long long int sumR2(vector<vector <unsigned long int> > &data, vector<vector <char> > &mark, vector<pair<int, int> > &used, int i, int j) {
    unsigned long long int sum = 0;
    if (mark[i][j] == ' ') {
        sum += data[i][j];
    } else if (mark[i][j] == 'b') {
        sum += sumR(data, mark, used, i - 1, 0) + sumR(data, mark, used, i - 1, j + 1);
    } else if (mark[i][j] == 'r') {
        sum += sumR(data, mark, used, i, j + 1) + sumR(data, mark, used, i, j + 2);
    }
    return sum;
}

void recursivelyGoForward(vector<vector <unsigned long int> > &data, vector<vector <char> > &mark, vector<pair<int, int> > &used, int i, int j) {
    bool flag = in(i, j, used);
    if (mark[i][j] == 'b' && !flag) {
        if (mark[i - 1][0] != ' ') {
            recursivelyGoForward(data, mark, used, i - 1, 0);
        } else {
            if (!in(i - 1, 0, used)) {
                used.push_back(make_pair(i - 1, 0));
            }
        }
        if (mark[i - 1][j + 1] != ' ') {
            recursivelyGoForward(data, mark, used, i - 1, j + 1);
        } else {
            if (!in(i - 1, j + 1, used)) {
                used.push_back(make_pair(i - 1, j + 1));
            }
        }
    } else if (mark[i][j] == 'r' && !flag) {
        if (mark[i][j + 1] != ' ') {
            recursivelyGoForward(data, mark, used, i, j + 1);
        } else {
            if (!in(i, j + 1, used)) {
                used.push_back(make_pair(i, j + 1));
            }
        }
        if (mark[i][j + 2] != ' ') {
            recursivelyGoForward(data, mark, used, i, j + 2);
        } else {
            if (!in(i, j + 2, used)) {

                used.push_back(make_pair(i, j + 2));
            }
        }
    }
}

unsigned long long int sumFromUsed(int from, vector<vector <unsigned long int> > &data, vector<pair<int, int> > &used) {
    unsigned long long int sum = 0;
    for (int i = from; i < used.size(); i++) {
        sum += data[used[i].first][used[i].second];
    }
    return sum;
}

void tryToImprove(vector<vector <unsigned long int> > &data, vector<vector <char> > &mark, vector<pair<int, int> > &used) {
    for (int k = 0; k < data.size(); k++) {
        unsigned long long int max = 0;
        unsigned long long int temp;
        int index;
        for (int i = used.size() - 1; i >= 0; i--) {
            temp = sumR2(data, mark, used, used[i].first, used[i].second);
            if (temp > max) {
                max = temp;
                index = i;
            }
        }
        int indexi = used[index].first;
        int indexj = used[index].second;
        long int numberOfUsed = used.size();
        for (int i = indexi; i >= 0; i--) {
            for (int j = indexi - i + indexj; j < data[i].size(); j++) {
                unsigned long long int temp = sumR(data, mark, used, i, j);
                if (temp < max && !inR(i, j, used, mark)) {
                    max = temp;
                    indexi = i;
                    indexj = j;
                }
            }
        }
        if (mark[indexi][indexj] != ' ') {
            recursivelyGoForward(data, mark, used, indexi, indexj);
        } else {
            used.push_back(make_pair(indexi, indexj));
        }
        if (used.size() > numberOfUsed) {
            used.erase(used.begin() + index);
        }
    }
}

unsigned long long int countRealCost(vector<vector <unsigned long int> > &data, vector<vector <char> > &mark, int n) {
    vector<pair<int, int> > used;
    unsigned long long int cost = 0;
    for (int i = 0; i < n; i++) {
        if (mark[i][0] != ' ') {
            recursivelyGoForward(data, mark, used, i, 0);
        } else {
            used.push_back(make_pair(i, 0));
        }
    }
    tryToImprove(data, mark, used);
    for (int i = 0; i < used.size(); i++) {
        cost += data[used[i].first][used[i].second];
    }
    return cost;
}

int main(int argc, char** argv) {

    int n;
    cin >> n;
    vector< vector <char> > mark;
    vector< vector <unsigned long int> > data = readFromStdIn(n, mark);
    rebuildAll(data, mark, n);
    cout << countRealCost(data, mark, n);
    return 0;
}