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
#include <bits/stdc++.h>

#define DEBUG if(0)
#define COUT cout << "\e[36m"
#define ENDL "\e[39m" << endl
#define VAR(v) " [\e[32m" << #v << "\e[36m=\e[91m" << v << "\e[36m] "

using namespace std;

int n = 0, k = 0;
vector<vector<int>> valvec, dp;
bool check_index(int i, int j)
{
    return ((i >= 0 /*> tego brakowało > */&& j >= 0)? ((j <= i)? 1 : 0) : 0);
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> k;
    valvec.resize(n);
    dp.resize(n);
    for (int i = 0; i < n; ++i)
    {
        valvec[i].resize(i+1, 0);
        dp[i].resize(i+1, 0);
        for (int j = 0; j <= i; ++j)
        {
            cin >> valvec[i][j];
        }
    }

    int ans = 1e9;
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j <= i; ++j)
        {
            dp[i][j] = 1;
            if(check_index(i-1, j-1))  {assert(i > 0 && j > 0); dp[i][j] += dp[i-1][j-1];} /* tu się wywalało*/
            if(check_index(i-1, j))   dp[i][j] += dp[i-1][j];
            if(check_index(i-2, j-1)) dp[i][j] -= dp[i-2][j-1];
            if(dp[i][j] <= k) ans = min(ans, valvec[i][j]);
        }
    }
    cout << ans << "\n";


    return 0;
}