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

using namespace std;
using LL = long long;

int main() {
  LL n, k;
cin >> n >> k;
  vector<vector<LL>> p;
  vector<vector<LL>> dp;
  // cin >> n >> k;

  for (LL i = 1; i <= n; ++i) {
    vector<LL> v;
    for (LL j = 1; j <= i; ++j) {
      LL x;
      cin >> x;
      v.push_back(x);
    }
    p.push_back(v);
  }

  LL wyn = p[0][0];
  dp.push_back(vector<LL>({0}));
  for (LL i = 1; i < n; ++i) {
    vector<LL> v;
    dp.push_back(vector<LL>());
    for (LL j = 0; j < (LL)p[i].size(); ++j) {
      LL pom = (j < (LL)dp[i - 1].size() ? (LL)dp[i - 1][j] : 0LL) + (j > 0 ? (LL)dp[i - 1][j - 1] : 0LL) + 1LL;
      if (i - 2 >= 0 && dp[i - 2].size() > j - 1 && j - 1 >= 0) {
        pom -= (LL)dp[i - 2][j - 1];
      }
      dp[i].push_back(pom);
      if (pom  + 1 <= k) {
        wyn = min(wyn, p[i][j]);
      }
    }
  }
  cout << wyn << endl;
}