#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)
Bài tập thuộc lộ trình
Mục lục đề ⌄
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 has rows and 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 selected entries.
Consider all 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 in the resulting sequence, where the first position is numbered .
Input
- The first line contains three integers : the number of rows, number of columns, and requested position.
- Each of the next lines contains exactly integers; line gives in column order for row .
Output
Print one integer: the -th smallest sum among all valid selections.
Subtasks
- Subtask 1 (20 points): ; ; ; for every , ; for every , .
- Subtask 2 (30 points): ; ; ; for every , ; for every , .
- Subtask 3 (50 points): ; ; ; for every , ; for every , .
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 , and . In sorted order the values are . Position contains .
Input 2
2 3 9
1 3 11
2 4 6
Output 2
17
Explanation 2
There are again nine selections, with sorted sums . This time position is requested. Choosing and yields ; 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 in the first row gives nine sums: . In sorted order they are . Selecting either occurrence of in the first row yields at least . Thus the first seven positions overall contain , and the answer is .