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

using namespace std;

int main(int argc, char** argv)
{
    int n;
    int ** tab;
    int * value;
    int * from;
    int * to;
    int * d;
    int input;
    int index_from;
    int index_to;
    int temp;
    long long result = 0;
    
    cin >> n;
    
    value = new int[n];
    from = new int[n];
    to = new int[n];
    d = new int[n];
    
    for(int i=0; i<n; ++i)
    {
        cin >> value[i];
        from[i] = 0;
        to[i] = i;
        d[i] = 0;
    }
    
    tab = new int*[n-1];
    for(int i=1; i<n; ++i)
    {
        tab[i-1] = new int[n-i];
    }
    
    for(int i=0; i<n-1; ++i)
    {
        for(int j=0;j<n-i-1;++j)
        {
            cin >> tab[i][j];
        }
    }
    
    for(int i = 0; i<n-1;++i)
    {
        for(int j=0; j<i+1; ++j)
        {
            input = tab[j][i-j];
            index_from = j+1;
            index_to = i+1;
            while(true)
            {                
                if(index_from > 0 && input <= value[index_from-1] )
                {
                    if(value[index_from-1] < value[index_to])
                    {
                        temp = value[index_to];
                        value[index_to] = input;
                        input = temp;
                        temp = from[index_to];
                        from[index_to] = index_from;
                        index_from = temp;
                        temp = to[index_to];
                        to[index_to] = index_to;
                        index_to = temp;
                    }
                    else
                    {
                        temp = value[index_from-1];
                        value[index_from-1] = input;
                        input = temp;
                        temp = to[index_from-1];
                        to[index_from-1] = index_to;
                        index_to = temp;
                        temp = from[index_from-1];
                        from[index_from-1] = index_from;
                        index_from = temp;
                    }
                }
                else if(input < value[index_to] ||(input == value[index_to] && index_from > from[index_to]))
                {
                    temp = value[index_to];
                    value[index_to] = input;
                    input = temp;
                    temp = from[index_to];
                    from[index_to] = index_from;
                    index_from = temp;
                    temp = to[index_to];
                    to[index_to] = index_to;
                    index_to = temp;
                }
                else
                {
                    break;
                }
            }
        }
    }
    
    for(int i=0; i<n; ++i)
    {
        result += value[i];
    }
    
    delete [] value;
    delete [] from;
    delete [] to;
    delete [] d;
    
    for(int i=0;i<n-1;++i)
    {
        delete [] tab[i];
    }
    delete [] tab;
    
    cout << result;
    
    return 0;
}