PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#PS0000211. Ao có trung vị độ cao nhỏ nhất (Pond)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Ao có trung vị độ cao nhỏ nhất (Pond)

Pond

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

Given an N×NN\times N grid of heights Ai,jA_{i,j}, select a contiguous K×KK\times K square entirely inside the grid. Sort its K2K^2 heights in nondecreasing order; the median is the element of rank ⌈K2/2⌉\lceil K^2/2\rceil (1-based), i.e. the lower middle element when the count is even. Find the minimum median among all such squares.

Input

The first line contains N,KN,K. The next NN lines each contain NN integers Ai,jA_{i,j}.

Output

Print the minimum median as one integer.

Subtasks

  • Subtask 1 (20 points): 1≤K≤N≤121\le K\le N\le 12; 0≤Ai,j≤1090\le A_{i,j}\le10^9.
  • Subtask 2 (30 points): 1≤K≤N≤8001\le K\le N\le 800; 0≤Ai,j≤1090\le A_{i,j}\le10^9; K=1K=1.
  • Subtask 3 (50 points): 1≤K≤N≤8001\le K\le N\le 800; 0≤Ai,j≤1090\le A_{i,j}\le10^9.

Example 1

Input

3 2
1 7 0
5 8 11
10 4 2

Output

4

Explanation

The four 2×2 squares have lower medians 5, 7, 5, 4, so the minimum is 4.

Example 2

Input

3 3
1 2 3
4 5 6
7 8 9

Output

5

Explanation

There is only one square; the fifth element is 5.