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

using namespace std;

#define N 2001

int p[N][N], d[N][N];

int main() {
  int n, k, p_base, p_left, p_right, p_up, best = 2020;
  cin >> n >> k;

  for (int i = 0; i < n; i++)
    for (int j = 0; j <= i; j++)
      cin >> p[n - i - 1][j];

  for (int i = 0; i < n; i++)
    for (int j = 0; j <= i; j++) {
      p_up = n - i + 1 < n ? (j > 0 ? d[n - i + 1][j - 1] : 0) : 0;
      p_left = n - i < n ? (j > 0 ? d[n - i][j - 1] : 0) : 0;
      p_right = n - i < n ? d[n - i][j] : 0;
      p_base = p_left + p_right - p_up + 1;
      d[n - i - 1][j] = p_base;

      if (p_base <= k && p[n - i - 1][j] < best)
        best = p[n - i - 1][j];
    }

  cout << best;
}