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

using namespace std;

int main() {
    int n, k;
    cin >> n;
    cin >> k;

    int **minYear = new int*[n];
    int **pascal = new int*[n];
    for (int i = 0; i < n; i++) {
        minYear[i] = new int[n];
        pascal[i] = new int[n];
    }

    int globalMin = 2020;

                for (int i = 0; i < n; i++) {
                for (int j = 0; j < i + 1; j++) {
                    cin >> minYear[i][j];
                    int parentLeft = 0;
                    int parentRight = 0;
                    if (i > 0) {
                        if (j > 0) {
                            minYear[i][j] = min(minYear[i][j], minYear[i - 1][j - 1]);
                            parentLeft = pascal[i - 1][j - 1];
                        }
                        if (j < i) {
                            minYear[i][j] = min(minYear[i][j], minYear[i - 1][j]);
                            parentRight = pascal[i - 1][j];
                        }
                    }
                    if (parentRight != 0) pascal[i][j] = parentRight + (j + 1);
                    else if (parentLeft != 0) pascal[i][j] = parentLeft + 1;
                    else pascal[i][j] = 1;

                    if (k >= pascal[i][j]) {
                        globalMin = min(globalMin, minYear[i][j]);
                    }
                }
            }

    cout << globalMin;
    return 0;
}