input: from typing import List
  from itertools import accumulate
  from math import inf
  
  def max_subarray_strength(nums: List[int], k: int) -> int:
      """
      Given a 0-indexed integer array nums of length n, and a positive odd integer k.
  
      The strength of selected subarrays is defined as:
      strength = sum[1] * x - sum[2] * (x - 1) + sum[3] * (x - 2) - sum[4] * (x - 3) + ... + sum[x] * 1 
      where sum[i] is the sum of the elements in the ith subarray. Formally, strength is calculated as 
      the sum of (-1)i+1 * sum[i] * (x - i + 1) for all i such that 1 <= i <= x.
  
      You need to pick k non-overlapping subarrays from nums, such that their strength is maximized.
  
      Return the maximum possible strength obtainable.
  
      Note: The chosen subarrays do not need to span the entire array.
  
      Examples:
      >>> nums = [1,2,3,-1,2], k = 3
      22
      >>> nums = [12,-2,-2,-2,-2], k = 5
      64
      >>> nums = [-1,-2,-3], k = 1
      -1
  
      Constraints:
      1 <= n <= 10^4
      -10^9 <= nums[i] <= 10^9
      1 <= k <= n
      1 <= n * k <= 10^6
      k is odd.
      """
output:     n = len(nums)
      if k == 1:
          return max(nums)
      
      def calc_strength(subarray):
          x = len(subarray)
          ans = 0
          for i in range(1, x+1):
              ans += (-1)**(i+1) * sum(subarray[:i]) * (x - i + 1)
          return ans
  
      dp = [[-inf] * (k + 1) for _ in range(n + 1)]
      prefix_sum = [0] + list(accumulate(nums))
      
      for i in range(1, n + 1):
          dp[i][1] = max(calc_strength(nums[j:i]) for j in range(i))
      
      for j in range(2, k + 1):
          for i in range(j, n + 1):
              for m in range(j - 1, i):
                  dp[i][j] = max(dp[i][j], dp[m][j-1] + calc_strength(nums[m:i]))
      
      return max(dp[n])