Given an integer array arr, split it into contiguous subarrays of length at most k. After partitioning, every value in a subarray becomes the maximum of that subarray. Return the largest possible sum of the array after partitioning.
Given an integer array arr, split it into contiguous subarrays of length at most k. After partitioning, every value in a subarray becomes the maximum of that subarray. Return the largest possible sum of the array after partitioning.
Example 1:
Input: {"arr":[1,15,7,9,2,5,10],"k":3}
Output: 84
Constraints:
1≤arr.length≤500
0≤arr[i]≤109
1≤k≤arr.length
The answer fits in a 32-bit integer.
Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result
Run code to see results.
Input
arr ={"arr":[1,15,7,9,2,5,10],"k":3}
dp[i] = best sum for the first i numbers. dp[0] = 0. For each i, try every length (up to k = 3) for the LAST block; that block is flattened to its maximum.