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

#PS0000225. Giá trị XOR tọa độ lớn thứ K (Find Kth Largest XOR Coordinate Value)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Giá trị XOR tọa độ lớn thứ K (Find Kth Largest XOR Coordinate Value)

Find Kth Largest XOR Coordinate Value

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

Given a nonnegative integer matrix aa of mm rows and nn columns indexed from 1, define the coordinate value at (r,c)(r,c) as the bitwise XOR of entries (i,j)(i,j) with 1≤i≤r1\le i\le r, 1≤j≤c1\le j\le c. Find the KK-th largest among the mnmn coordinate values, counting equal values separately.

Input

The first line contains integers m,n,Km,n,K, followed by mm lines of exactly nn integers.

Output

Print the KK-th largest XOR value.

Subtasks

  • Subtask 1 (20 points): 1≤m,n≤121\le m,n\le 12; 0≤ai,j≤1060\le a_{i,j}\le10^6; 1≤K≤mn1\le K\le mn.
  • Subtask 2 (30 points): 1≤m,n≤10001\le m,n\le 1000; 0≤ai,j≤1060\le a_{i,j}\le10^6; 1≤K≤mn1\le K\le mn; K=1K=1.
  • Subtask 3 (50 points): 1≤m,n≤10001\le m,n\le 1000; 0≤ai,j≤1060\le a_{i,j}\le10^6; 1≤K≤mn1\le K\le mn.

Example 1

Input

2 2 1
5 2
1 6

Output

7

Explanation

The four XOR values sorted descending are 7,5,4,0; rank 1 is 7.

Example 2

Input

2 2 2
5 2
1 6

Output

5

Explanation

The four XOR values sorted descending are 7,5,4,0; rank 2 is 5.

Example 3

Input

2 2 3
5 2
1 6

Output

4

Explanation

The four XOR values sorted descending are 7,5,4,0; rank 3 is 4.