def max_sum_with_breaks(arr, k):
n = len(arr)
dp = [-float('inf')] * n # Initialize dp with -inf to find the maximum
dp[0] = arr[0] # Base case: first element can only be taken by itself
for i in range(1, n):
current_sum = 0
# Consider taking up to k successive elements ending at index i
for j in range(k):
if i - j >= 0: # Ensure we're within bounds
current_sum += arr[i - j] # Add the element arr[i-j]
if i - j - 2 >= 0:
dp[i] = max(dp[i], dp[i - j - 2] + current_sum) # Consider the break
else:
dp[i] = max(dp[i], current_sum) # No previous group, just the current sum
return max(dp)
# Test cases:
list1 = [60, 70, 80, 40, 80, 90, 100, 20]
k1 = 3
print(max_sum_with_breaks(list1, k1)) # Expected output: 480
list2 = [45, 12, 78, 34, 56, 89, 23, 67, 91]
k2 = 4
print(max_sum_with_breaks(list2, k2)) # Expected output: 460
To embed this program on your website, copy the following code and paste it into your website's HTML: