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

#BS0000461. Quà năm mới (New Year's Problem)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Quà năm mới (New Year's Problem)

New Year's Problem

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Vlad must buy one gift for each of his nn friends. Buying friend jj a gift at shop ii yields joy pi,jp_{i,j}. He may visit at most n−1n-1 of the mm shops and purchase multiple gifts per visited shop. Maximize the minimum joy among all friends.

Input

The first line contains m,nm,n. Each of the next mm lines contains nn integers pi,jp_{i,j}.

Output

Print the maximum achievable minimum joy.

Subtasks

  • Subtask 1 (20 points): m≥1m\ge1; n≥2n\ge2; 2≤mn≤1002\le mn\le 100; 1≤pi,j≤1091\le p_{i,j}\le10^9 (1≤i≤m1\le i\le m, 1≤j≤n1\le j\le n).
  • Subtask 2 (30 points): m≥1m\ge1; n≥2n\ge2; 2≤mn≤50002\le mn\le 5000; 1≤pi,j≤1091\le p_{i,j}\le10^9 (1≤i≤m1\le i\le m, 1≤j≤n1\le j\le n).
  • Subtask 3 (50 points): m≥1m\ge1; n≥2n\ge2; 2≤mn≤1000002\le mn\le 100000; 1≤pi,j≤1091\le p_{i,j}\le10^9 (1≤i≤m1\le i\le m, 1≤j≤n1\le j\le n).

Examples

Example 1

Input

2 2
1 2
3 4

Output

3

Explanation

Visit shop 2: joys 3 and 4. Friend 1 cannot exceed 3.

Example 2

Input

4 3
1 3 1
3 1 1
1 2 2
1 1 3

Output

2

Explanation

Use shops 2 and 3 for joys 3,2,2. Threshold 3 requires three separate shops.

Example 3

Input

2 3
5 3 4
2 5 1

Output

4

Explanation

Use both shops for joys 5,5,4; friend 3 is capped at 4.

Example 4

Input

4 2
7 9
8 1
9 6
10 8

Output

8

Explanation

Use shop 4 for 10 and 8. No row has both entries above 8.

Example 5

Input

2 4
6 5 2 1
7 9 7 2

Output

2

Explanation

Visit shop 2; friend 4 cannot exceed 2.