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

using namespace std;
typedef long long ll;

ll a[2005][2005];
ll min_rem[2005][2005];


ll max(ll a, ll b)
{
 return a > b ? a : b;   
}

int main()
{
    ios_base::sync_with_stdio(false);
    ll n,k;

    ll best_guy_age = 2020;

    cin >> n >> k;


    for (int i = 0; i < n; i++)
        for (int j = 0; j <= i; j++)
        {
            //cout << "in the loop, waiting for input" << endl;
            //cout << "working with i, j = " << i << " " << j << endl;
            cin >> a[i][j];

            if (i == 0 && j == 0)
            {
                //cout << "Case 1" << endl;
                min_rem[i][j] = 0;
            }
            else if (j == 0)
            {
                //cout << "Case 2" << endl;
                min_rem[i][j] = 1 + min_rem[i-1][j];
            }
            else if (j == i)
            {
                //cout << "Case 3" << endl;
                min_rem[i][j] = 1 + min_rem[i-1][j-1];
            }
            else
            {
                //cout << "Case 4" << endl;
                min_rem[i][j] = 2  + max(min_rem[i-1][j-1], min_rem[i-1][j]);
            }

            if (min_rem[i][j] + 1 <= k) // then we can consider that value
            {
                //cout << "Case 5A" << endl;
                if (a[i][j] < best_guy_age)
                {
                    //cout << "Case 5B" << endl;
                    best_guy_age = a[i][j];
                }
            }
            
            //cout << "for " << a[i][j] << " min_rem = " << min_rem[i][j] << endl;
        }
        
    //cout << "returning " << best_guy_age << endl;
    
    cout << best_guy_age;
    
    return 0;

}