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

int numberToRemove(int i, int j, int year) {
    // if (year == 337) {
    //     //cerr << "[" << i << "," << j << "] #";
    // }
    int tmp = 0;
    for(int x = i; x> 0; x--) {
        int rowBottles = (i-(x-1));
        for(int y = j-rowBottles+1; y<= j;y++) {
            if (y > 0 && y <= x) {
                // if (year == 337) {
                //     //cerr << "(" << x << "," << y << "),";
                // }
                tmp++;
            }
        }
    }
    // if (year == 337) {
    //     cerr << "(" << i << ":" << j << ") -> " << tmp << endl;
    // }
    return tmp;
}

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

    //read input
    int year = -1;
    int n, k, a;
    vector<pair<int,pair<int, int>>> v;
    v.reserve((2000*2001)/2);
    cin >> n >> k;
    int piramid_size = (n*(n+1))/2;
    int i = 1, j = 1;
    for(int z = 1; z <= piramid_size; z++) {
        cin >> a;
        v.push_back(make_pair(a ,make_pair(i, j)));
        j++;
        if (j > i) {
            i++;
            j = 1;
        }
    }

    //find solution
    sort(v.begin(), v.end());
    // cerr << "aaa";
    for(int z = 0; z < v.size(); z++) {
        pair<int,pair<int, int>> val = v.at(z);
        // cerr << z << ":" << val.first << " ";
        i = val.second.first;
        j = val.second.second;
        if (numberToRemove(i,j, val.first) <= k) {
            year = val.first;
            break;
        }
    }

    //print solution
    cout << year;
}