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
#include <iostream>

#define print \
    for(int i = 0; i < rows; ++i) \
    {\
        for(int j = 0; j < rows; ++j)\
            cout << a[i][j] << " ";\
        cout << endl;\
    }

using namespace std;

int main()
{
    ios_base::sync_with_stdio(0);
    int rows;
    cin >> rows;
    int s = rows*(rows+1)/2;
//    int a[2][3];
    int **a = new int*[rows];
    for(int i = 0; i < rows; ++i)
    {
        a[i] = new int[rows];
        for(int j = i; j< rows; ++j)
            cin >> a[i][j];
    }
//    print
    int min;
    for(int i = rows - 1; i>=0; --i)
    {
        min = a[0][i];
        a[i][0] = min;
        for(int j = 1; j < i; ++j)
        {
            if(a[j][i] < min)
                min = a[j][i];
            a[i][j]=min;
        }
    }
//    print
    long long diagonal = a[0][0];
    for(int i = 1; i < rows; ++i)
        diagonal += (a[i][i-1] > a[i][i]) ? a[i][i] : a[i][i-1];
    long long mindiag = diagonal;
//    cout << diagonal << endl;
    if(rows <=2)
    {
        cout << diagonal << endl;
        return 0;
    }
    mindiag -= (a[rows-1][rows-1] < a[rows - 2][rows-1]) ? a[rows-1][rows-1] : a[rows-2][rows-1];
    long long maxdiag = 0;
    long long edge = 0;
    for(int i = rows - 2; i>0; --i)
    {
        maxdiag = 0;
        edge = a[i][i];
        mindiag -= (a[i][i] < a[i-1][i]) ? a[i][i] : a[i-1][i];
        for(int j = i+1; j< rows; j++)
        {
            if(a[j][j] > a[i][j])
                a[j][j] = a[i][j];
            maxdiag += a[j][j];

            // edge
            if(edge > a[j][i])
                edge = a[j][i];
        }
        long long sum = mindiag + maxdiag + edge;
//        cout << "row " << i << "min " << mindiag << " max " << maxdiag << " edge " << edge << endl;
        if(diagonal > sum)
        {
            diagonal = sum;
//            cout << "row " << i << "min " << mindiag << " max " << maxdiag << " edge " << edge << endl;
        }
    }
    cout << diagonal << endl;
    return 0;
}