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

using namespace std;

#ifdef USE_CERR_LOG
#define LOG if (true) cerr
#else
#define LOG if (false) cerr
#endif

typedef long long ll;

#ifdef USE_FILE_CIN
ifstream input("../win.in");
#define cin input
#endif

int n, k;
vector<int> bottlesToVintage;

inline int getBottlesToTake(int row, int col) {
    return col * (row - col + 1);
}

void addBottle(int vintage, int bottlesToTake) {
    LOG << "Adding " << vintage << ", up: " << bottlesToTake << endl;
    
    bottlesToVintage[bottlesToTake] = min(bottlesToVintage[bottlesToTake], vintage);
}

void logBottlesToVintage() {
#ifdef USE_CERR_LOG
    for (int i = 0; i < 30; i++) {
        LOG << i << ": " << bottlesToVintage[i] << endl;
    }
#endif
}


void readData() {
    int row, col;
    int vintage;
    
    cin >> n >> k;
    bottlesToVintage.resize(n * (n+1), 9999);
    
    for (row = 1; row <= n; row++) {
        for (col = 1; col <= row; col++) {
            cin >> vintage;
            addBottle(vintage, getBottlesToTake(row, col));
        }
    }    
}

int solve() {
    int i;
    int minVintage = 9999;
    
    for (i = 1; i <= k; i++) {
        minVintage = min(minVintage, bottlesToVintage[i]);
    }
    
    return minVintage;
}

int main() {
    ios_base::sync_with_stdio(false);
    
    readData();
    logBottlesToVintage();
    cout << solve();
    
    return 0;
}


int main_multi() {
    ios_base::sync_with_stdio(false);
    
    int nn, i;
    cin >> nn;
    
    for (i=0; i<nn; i++) {
        bottlesToVintage.resize(1);
        readData();
        logBottlesToVintage();
        cout << solve() << endl;
    }
    
    return 0;
}