The Perfect Cow

Interview

Time limit1sMemory limit128 MB

Summary
Given an odd N by N grid, take the median of each row, then return the median of those N row medians.
Level

Easy3 of 10

Topics
Sorting, Array, Implementation
Solved
No attempts yet

Problem

For yet another year, Farmer John was named "Dairy Farmer of the Year". He wants to exhibit his most perfect cow at the upcoming Cow Convention.

According to the scientific literature, beauty is a trend toward the average rather than the existence of some superior trait. So Farmer John wants to find his most average cow and bring her to the convention.

Each cow has her beauty rating RijR_{ij} (1≤Rij≤10001 \le R_{ij} \le 1000) inscribed on a tag in her ear. Farmer John lines the cows up into an N×NN \times N square (2≤N≤992 \le N \le 99, and NN is odd).

He first finds the median cow in each row. The median cow is the one whose beauty rating is greater than or equal to half of the cows in her row and also less than or equal to half of them — that is, the middle value when the row is sorted. From those NN medians he then takes the median again, and that value is the beauty rating of the most perfect (average) cow.

Given the N×NN \times N cows, find the beauty rating of the most perfect cow.

Input

  • Line 1: A single integer NN.
  • Lines 2..N+1: Line i+1i+1 contains NN space-separated integers, the NN beauty ratings for row ii of the cow square.

Output

  • Line 1: A single integer, the beauty rating of the most perfect cow as described above.

Hint

In the example, the medians of the rows are, respectively, 5,3,5,3,5, 3, 5, 3, and 44. The median of those five values is 44.

Examples2

  1. Example 1

    Input
    5
    1 5 3 9 5
    2 5 3 8 1
    6 3 5 9 2
    8 8 3 3 2
    5 4 4 4 4
    
    Expected output
    4
    
  2. Example 2

    Input
    3
    1 2 3
    4 5 6
    7 8 9
    
    Expected output
    5