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
#include<bits/stdc++.h>
using namespace std;
struct beczka{
    int lo;
    int po;
    int rok;
    int licz;
    int rz;
};
beczka b[4005000];
int k, n;
int main(){
    ios_base::sync_with_stdio(0);
    cin >> k >> n;
    int ind = 1;
    for(int i = 1; i <= k ; i++){
        for(int j = 1; j <= i; j++){
            cin >> b[ind].rok;
            b[ind].rz = i;
            b[ind].lo = ind - i;
            b[ind].po = ind - i + 1;
            if(b[ind].rz != b[b[ind].lo].rz + 1){
                b[ind].lo = 0;
            }
            if(b[ind].rz != b[b[ind].po].rz + 1){
                b[ind].po = 0;
            }
            ind++;
        }
    }
    for(int i = 1; i < ind; i++){
        b[i].licz = b[b[i].lo].licz + b[b[i].po].licz - b[b[b[i].po].lo].licz + 1;

    }
    /*cout << ind << '\n';
    for(int i = 1; i < ind; i++){
        cout << i << "  " << b[i].lo << " " << b[i].po << " " << b[i].licz << '\n';
    }
    for(int i = 1; i < ind; i++){
        cout << b[i].licz << " ";
        if(i == 1 || i == 2 || i == 6 || i == 10 || i == 15 || i == 21 || i == 28){
            cout << '\n';
        }
    }//*/
    int mini = 23525;
    for(int i = 1; i < ind; i++){
        if(b[i].licz <= n){
            mini = min(mini, b[i].rok);
        }
    }
    cout << mini;
}