<p>You are given an <code>m x n</code> matrix <code>mat</code> that has its rows sorted in non-decreasing order and an integer <code>k</code>.</p>

<p>You are allowed to choose <strong>exactly one element</strong> from each row to form an array.</p>

<p>Return <em>the </em><code>k<sup>th</sup></code><em> smallest array sum among all possible arrays</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> mat = [[1,3,11],[2,4,6]], k = 5
<strong>Output:</strong> 7
<strong>Explanation:</strong> Choosing one element from each row, the first k smallest sum are:
[1,2], [1,4], [3,2], [3,4], [1,6]. Where the 5th sum is 7.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> mat = [[1,3,11],[2,4,6]], k = 9
<strong>Output:</strong> 17
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> mat = [[1,10,10],[1,4,5],[2,3,6]], k = 7
<strong>Output:</strong> 9
<strong>Explanation:</strong> Choosing one element from each row, the first k smallest sum are:
[1,1,2], [1,1,3], [1,4,2], [1,4,3], [1,1,6], [1,5,2], [1,5,3]. Where the 7th sum is 9.  
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>m == mat.length</code></li>
	<li><code>n == mat.length[i]</code></li>
	<li><code>1 &lt;= m, n &lt;= 40</code></li>
	<li><code>1 &lt;= mat[i][j] &lt;= 5000</code></li>
	<li><code>1 &lt;= k &lt;= min(200, n<sup>m</sup>)</code></li>
	<li><code>mat[i]</code> is a non-decreasing array.</li>
</ul>

# Data Structures: Use a min-heap (priority queue) to store possible sums and track them in a sorted order.

# Algorithm: We will iterate over each row and use a dynamic approach to continuously generate new sums by adding one element from each row. We maintain the smallest sums using a min-heap. 

# Short Intuition: We begin by selecting one element from the first row, then for each subsequent row, add elements and generate new sums while keeping track of the k smallest sums.

# Detailed Intuition:
# - Start by adding the first row's elements to a heap.
# - For each new row, iterate over the elements and add them to the sums in the heap from the previous row.
# - Keep track of the k smallest sums at every step using the heap.
# - After processing all rows, the heap will contain the k smallest sums, and the kth smallest sum will be at the top of the heap.

# Pseudocode:
# 1. Initialize the heap with the first row's elements.
# 2. For each subsequent row:
#    a. Pop elements from the heap, add each element from the row, and push the new sum back into the heap.
#    b. Maintain only the k smallest sums in the heap.
# 3. Return the kth smallest sum when done.

import heapq

class Solution:
    def kthSmallest(self, mat: list[list[int]], k: int) -> int:
        # Step 1: Initialize the heap with the first row's elements as sums
        heap = [(x, 0) for x in mat[0]]  # Each element (sum, index) for the first row
        heapq.heapify(heap)  # Heapify the first row

        # Step 2: Process each row after the first one
        for i in range(1, len(mat)):
            next_heap = []
            # Process each sum in the current heap and add elements from the next row
            while heap:
                curr_sum, idx = heapq.heappop(heap)  # Get the smallest current sum and index
                for element in mat[i]:  # Add each element from the current row
                    new_sum = curr_sum + element
                    next_heap.append((new_sum, i))
                    
            # Step 3: Keep only the k smallest sums
            next_heap = sorted(next_heap)[:k]  # Take the k smallest sums
            heap = next_heap  # Update the heap with the new sums
        
        # Step 4: Return the kth smallest sum
        return heap[k-1][0]  # The kth smallest sum

# Time Complexity: O(k * m * log(k)), where m is the number of rows and k is the number of smallest sums we track. Sorting and heap operations take O(log(k)).
# Space Complexity: O(k), for storing the heap with k smallest sums.
# Variables:
#   - heap: A min-heap that stores the smallest sums at each step.
#   - next_heap: A temporary list to store the new sums generated at each row.
#   - curr_sum: The current sum from the previous row elements.
#   - new_sum: The new sum generated by adding an element from the next row.

245