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

int calculate_cost(int row, int col);

int main() {
	int n, k;
	int best = 2020;
	scanf("%d %d\n", &n, &k);
	for (int row = 1; row <= n; row ++) {
		for (int col = 1; col <= row; col++) {
			int year;
			scanf("%d", &year);
			if (calculate_cost(row, col) <= k && year < best) {
				best = year;
			}
		}
	}
	printf("%d\n", best);
	return 0;
}

int calculate_cost(int row, int col) {
	int depth;
	if (2 * col <= row) {
		depth = col;
	} else {
		depth = row - col + 1;
	}
	int bottom_triangle = (depth * (depth + 1)) / 2;

	int strip_height = (row - 2 * depth + 1);
	int strip =  strip_height * depth;

	int top_triangle_size = row - depth - strip_height;
	int top_triangle = (top_triangle_size * (top_triangle_size + 1)) / 2;

	return bottom_triangle + strip + top_triangle;
}