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

#BS0000490. Tổng nhỏ thứ K khi chọn từ các hàng đã sắp xếp (Find the Kth Smallest Sum of a Matrix With Sorted Rows)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Tổng nhỏ thứ K khi chọn từ các hàng đã sắp xếp (Find the Kth Smallest Sum of a Matrix With Sorted Rows)

Find the Kth Smallest Sum of a Matrix With Sorted Rows

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

A matrix AA has mm rows and nn columns. The entries within each row are in nondecreasing order. Select exactly one entry from each row; the value of a selection is the sum of its mm selected entries.

Consider all nmn^m selections, each identified by the column chosen in every row. Arrange their values in nondecreasing order, retaining every occurrence of equal values.

Find the value at position kk in the resulting sequence, where the first position is numbered 11.

Input

  • The first line contains three integers m,n,km,n,k: the number of rows, number of columns, and requested position.
  • Each of the next mm lines contains exactly nn integers; line ii gives ai,1,ai,2,…,ai,na_{i,1},a_{i,2},\ldots,a_{i,n} in column order for row ii.

Output

Print one integer: the kk-th smallest sum among all valid selections.

Subtasks

  • Subtask 1 (20 points): 1≤m≤51\le m\le 5; 1≤n≤51\le n\le 5; 1≤k≤min⁡(200,nm)1\le k\le\min(200,n^m); 1≤ai,j≤50001\le a_{i,j}\le 5000 for every 1≤i≤m1\le i\le m, 1≤j≤n1\le j\le n; ai,j≤ai,j+1a_{i,j}\le a_{i,j+1} for every 1≤i≤m1\le i\le m, 1≤j<n1\le j<n.
  • Subtask 2 (30 points): 1≤m≤401\le m\le 40; 1≤n≤401\le n\le 40; 1≤k≤min⁡(200,nm)1\le k\le\min(200,n^m); 1≤ai,j≤201\le a_{i,j}\le 20 for every 1≤i≤m1\le i\le m, 1≤j≤n1\le j\le n; ai,j≤ai,j+1a_{i,j}\le a_{i,j+1} for every 1≤i≤m1\le i\le m, 1≤j<n1\le j<n.
  • Subtask 3 (50 points): 1≤m≤401\le m\le 40; 1≤n≤401\le n\le 40; 1≤k≤min⁡(200,nm)1\le k\le\min(200,n^m); 1≤ai,j≤50001\le a_{i,j}\le 5000 for every 1≤i≤m1\le i\le m, 1≤j≤n1\le j\le n; ai,j≤ai,j+1a_{i,j}\le a_{i,j+1} for every 1≤i≤m1\le i\le m, 1≤j<n1\le j<n.

Examples

Input 1

2 3 5
1 3 11
2 4 6

Output 1

7

Explanation 1

Each of the two rows has three entries. Pair each first-row entry with every second-row entry to obtain (3,5,7)(3,5,7), (5,7,9)(5,7,9) and (13,15,17)(13,15,17). In sorted order the values are 3,5,5,7,7,9,13,15,173,5,5,7,7,9,13,15,17. Position 55 contains 77.

Input 2

2 3 9
1 3 11
2 4 6

Output 2

17

Explanation 2

There are again nine selections, with sorted sums 3,5,5,7,7,9,13,15,173,5,5,7,7,9,13,15,17. This time position 99 is requested. Choosing 1111 and 66 yields 1717; no selection can exceed it because both entries are row maxima.

Input 3

3 3 7
1 10 10
1 4 5
2 3 6

Output 3

9

Explanation 3

Selecting 11 in the first row gives nine sums: 4,5,8,7,8,11,8,9,124,5,8,7,8,11,8,9,12. In sorted order they are 4,5,7,8,8,8,9,11,124,5,7,8,8,8,9,11,12. Selecting either occurrence of 1010 in the first row yields at least 10+1+2=1310+1+2=13. Thus the first seven positions overall contain 4,5,7,8,8,8,94,5,7,8,8,8,9, and the answer is 99.