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

constexpr std::size_t max_n = 2000;
constexpr std::size_t max_k = max_n * (max_n + 1ULL) / 2ULL;

static int counts[max_k];

void setup_counts() {
    counts[0] = 1;
    std::size_t left = 1, right = 2;
    for (std::size_t depth = 1; depth < max_n; ++depth) {
        assert(left < max_k);
        assert(right < max_k);

        counts[left] = depth + 1;
        counts[right] = depth + 1;

        left += depth + 1;
        right += depth + 2;
    }

    left = 3; // start with third row
    for (std::size_t depth = 2; depth < max_n; ++depth) {
        assert(left < max_k);
        
        for (std::size_t offset = 1; offset < depth; ++offset) {
            auto index = left + offset;
            auto sum_parents = counts[index - (depth + 1)] + counts[index - depth];
            auto grandparent = counts[index - (depth + 1) - depth + 1];
            counts[index] = sum_parents - grandparent + 1;
        }

        left += depth + 1;
    }
}

int main() {
    std::ios_base::sync_with_stdio(false);
    setup_counts();
    
    std::size_t n, k;
    std::cin >> n >> k;

    int result = std::numeric_limits<int>::max();
    std::size_t left = 0;
    for (std::size_t depth = 0; depth < n; ++depth) {
        for (std::size_t offset = 0; offset <= depth; ++offset) {
            int value;
            std::cin >> value;
            
            std::size_t index = left + offset;
            if (value < result && counts[index] <= k) {
                result = value;
            }
        }

        left += depth + 1;
    }

    std::cout << result << std::endl;
    return 0;
}