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

using namespace std;

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

    int n, k;
    int year;
    cin >> n >> k;

    vector<vector<int>> min_bottles_count(n);
    int min_year = 4000;
    int bottles_in_row = 0;
    int half_length = 0;

    for (int i = 0; i < n; ++i) {
        bottles_in_row = i + 1;
        if (bottles_in_row % 2 == 0) {
            half_length = bottles_in_row / 2;
        } else {
            half_length = (bottles_in_row / 2) + 1;
        }
        min_bottles_count[i] = vector<int>(bottles_in_row);

        for (int j = 0; j < bottles_in_row; ++j) {
            cin >> year;

            if (bottles_in_row == 1) {
                min_bottles_count[i][j] = 1;
                min_year = year;
            } else {
                if (j == 0) {
                    min_bottles_count[i][j] = bottles_in_row;
                } else if (j < half_length) {
                    min_bottles_count[i][j] = min_bottles_count[i - 1][j - 1] + bottles_in_row - j;
                } else {
                    min_bottles_count[i][j] = min_bottles_count[i][bottles_in_row - j - 1];
                }
                if (min_bottles_count[i][j] <= k && year < min_year) {
                    min_year = year;
                }
            }
        }

    }

    cout << min_year << '\n';

    return 0;
}