You are given an array of integers gems representing the values of gems in a row, and an integer k representing the maximum number of consecutive gems you can remove in one operation.
You must perform the removal operation exactly once. The operation removes at most k consecutive gems from the row.
Return the minimum possible sum of the remaining gems after the removal.
Function Signature
def min_remaining_sum(gems: list[int], k: int) -> int:
Rules
-
The removed gems form one contiguous block of the original row, of length at most
k
.
-
If the removal takes every gem, the remaining sum is 0.
Constraints
-
1 <= len(gems) <= 1000
-
1 <= gems[i] <= 1000
-
1 <= k <= len(gems)
Examples
Input: gems = [3, 7, 2, 8, 1, 5], k = 2
Output: 16
The total is 26. The best pair to remove is [3, 7] or [2, 8] (sum 10), so the remaining sum is 26 - 10 = 16.
Input: gems = [1, 2, 3, 4, 5], k = 3
Output: 3
Removing [3, 4, 5] (sum 12) leaves [1, 2], so the minimum remaining sum is 3.
Input: gems = [2, 9, 4], k = 3
Output: 0
Here k equals the length of the row, so one operation can remove all three gems and nothing is left.