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
#include <iostream>
#include <algorithm>
using namespace std;

struct ival {
    int start;
    int end;
    int cost;
};

bool compare (ival a, ival b)
{
    return a.cost < b.cost;
}

int main ()
{
    int n;
    cin >> n;
    
    ival * ivals = new ival[2002000];
    int c = 0;
    for (int i = 1; i <= n; i++)
        for (int j = 0; j < n - i + 1; j++)
        {            
            int v;
            cin >> v;
            ivals[c].cost = v;
            ivals[c].start= j;
            ivals[c++].end = j + i - 1;
            
        }
    
    sort(ivals, ivals + c, compare);
    
    int res = 0;
    for (int i = 0; i < n; i++)
    {
        res += ivals[i].cost;
    }

    cout << res << endl;
    return 0;
}