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
/*************************************************************************
 *                                                                       *
 *                   Potyczki Algorytmiczne 2014                         *
 *                                                                       *
 *                  Zadanie:           Kuglarz [A]                       *
 *                  Autor:             Jakub Sroka                       *
 *                                                                       *
 *************************************************************************/

#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

bool comparison(int i, int j) { return i>j; }

int main(int argc, const char * argv[])
{
    int n;
    scanf("%d", &n);
    
    vector< vector<int> > prices(n);
    
    for (int i=0; i<n; i++) {
        for (int j=0; j<n-i; j++) {
            prices[i].push_back(0);
            scanf("%d ", &prices[i].back());
        }
    }
    
    for (int i=0; i<n; i++) {
        sort(prices[i].begin(), prices[i].end(), comparison);
    }
    
    long long result = 0;
    int minVal, index;
    
    for (int i=1; i<=n; i++) {
        minVal = INT32_MAX;
        for (int j=0; j<i; j++) {
            if (prices[j].back() < minVal) {
                minVal = prices[j].back();
                index = j;
            }
        }
        result += minVal;
        prices[index].pop_back();
    }
    
    printf("%lld", result);

    return 0;
}