input: from typing import List
  
  def max_total_cost_of_subarrays(nums: List[int]) -> int:
      """
      Consider you have an integer array nums with length n.
  
      Define the cost of a subarray 
      nums[l..r], where 0 <= l <= r < n, as follows:
  
      cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (−1)^(r − l)
  
      The task is to partition nums into subarrays to maximize the total cost of these subarrays, ensuring that every element is part of exactly one subarray.
  
      Formally, if nums is split into k subarrays, where k > 1, at indices i_1, i_2, ..., i_k - 1, where 0 <= i_1 < i_2 < ... < i_k - 1 < n - 1, the overall cost will be:
  
      cost(0, i_1) + cost(i_1 + 1, i_2) + ... + cost(i_k − 1 + 1, n − 1)
  
      Return an integer representing the maximum total cost of the subarrays after optimally partitioning the array.
      Note:
       If nums is not partitioned into subarrays, i.e. k = 1, the total cost is simply cost(0, n - 1).
  
      Examples:
      >>> nums = [1,-2,3,4]
      10
      >>> nums = [1,-1,1,-1]
      4
  
      Constraints:
      1 <= nums.length <= 10^5
      -10^9 <= nums[i] <= 10^9
      """
output:     n = len(nums)
      if n == 1:
          return nums[0] * (-1) ** 0
  
      total_cost = 0
      for i in range(n - 1):
          total_cost += nums[i] - nums[i + 1]
      total_cost += nums[n - 1] * (-1) ** (n - 1)
  
      max_cost = total_cost
  
      for i in range(n - 1):
          current_cost = total_cost
          current_cost -= nums[i] - nums[i + 1]
          current_cost += nums[i + 1] - nums[i]
  
          max_cost = max(max_cost, current_cost)
      
      return max_cost