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

#BS0000452. Trò chơi trên bảng số (Grid Game)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Trò chơi trên bảng số (Grid Game)

Grid Game

Source: Kattis – https://open.kattis.com/problems/gridgame

Version: Phuoc Hung OJ Extended

Problem Statement

Given an N×NN\times N integer grid AA, select exactly NN cells so that no two selected cells share a row or a column. Thus every row and column is used exactly once.

The score of a selection is the smallest selected entry. Maximize this score.

Input

The first line contains NN. The next NN lines each contain NN integers, describing AA.

Output

Print the maximum possible score as a single integer.

Subtasks

Common conditions apply to all subtasks. Scoring groups are added by Phuoc Hung OJ.

1≤Ai,j≤1061\le A_{i,j}\le10^6.

All input values are integers.

Subtask 1 (20 points)

  • 1≤N≤71\le N\le7.

Subtask 2 (30 points)

  • 1≤N≤151\le N\le15.

Subtask 3 (50 points)

  • 1≤N≤301\le N\le30.

Examples

Example 1

Input

3
3 1 4
1 5 1
2 9 1

Output

2

Explanation

Choose entries 4,5,24,5,2 in columns 3,2,13,2,1. A threshold of 33 forces both rows 2 and 3 to choose column 2, so it is impossible.

Example 2

Input

5
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9

Output

21

Explanation

Choose entries 24,23,22,21,2524,23,22,21,25 in columns 2,1,5,4,32,1,5,4,3. Row 4 has no entry above 2121, proving optimality.