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

using namespace std;

const int N = 2e3 + 15;

int n, k;

vector <int> bottles[N], taken[N];

int main(){
    ios_base::sync_with_stdio(NULL);
    cin.tie(NULL);
    cin >> n >> k;
    for(int i = 1; i <= n; i++){
        for(int j = 0; j < i; j++){
            int age;
            cin >> age;
            bottles[i].push_back(age);
        }
    }

    taken[1].push_back(1);
    // cout << taken[1][0] << '\n';
    for(int i = 2; i <= n; i++){
        taken[i].push_back(taken[i - 1][0] + 1);
        for(int j = 1; j < i - 1; j++){
            int toTake = taken[i - 1][j] + taken[i - 1][j - 1] - taken[i - 2][j - 1] + 1;
            taken[i].push_back(toTake);
        }
        taken[i].push_back(taken[i - 1][taken[i - 1].size() - 1] + 1);
        // for(int t : taken[i]) cout << t << ' ';
        // cout << '\n';
    }

    int mn = INT_MAX;

    for(int i = 1; i <= n; i++){
        for(int j = 0; j < i; j++){
            if(taken[i][j] <= k) mn = min(mn, bottles[i][j]);
        }
    }

    cout << mn;
    return 0;
}