You are given an integer array arr and need to partition it strategically to maximize the sum. Here's the challenge: you can divide the array into contiguous subarrays where each subarray has at most k elements.
After partitioning, here's the key transformation: every element in each subarray becomes the maximum value of that subarray. Your goal is to find the partitioning strategy that produces the largest possible sum.
Example: If arr = [1, 15, 7, 9, 2] and k = 3, you could partition it as [1, 15, 7] + [9, 2]. The first subarray becomes [15, 15, 15] (sum = 45) and the second becomes [9, 9] (sum = 18), giving a total of 63.
This is a classic dynamic programming problem where you need to explore different partitioning strategies while respecting the constraint that each partition can contain at most k elements.
Input & Output
Constraints
- 1 โค arr.length โค 500
- 0 โค arr[i] โค 109
- 1 โค k โค arr.length
- Answer fits in a 32-bit integer