input: from typing import List
  from math import inf
  
  def min_moves_to_collect_ones(nums: List[int], k: int, maxChanges: int) -> int:
      """
      You are given a binary list nums of length n, a positive integer k and a non-negative integer maxChanges.
  
      Alice participates in a game, targeting to collect k ones from nums with the fewest moves. Initially, Alice picks any index aliceIndex in the range [0, n - 1] and stands there. If nums[aliceIndex] == 1, Alice picks up the one and nums[aliceIndex] becomes 0 (this does not count as a move). After the initial move, Alice can make any number of moves (including zero) where in each move, Alice must perform one of the following actions:
  
      Choose any index j != aliceIndex where nums[j] == 0 and set nums[j] = 1. This action can be performed at most maxChanges times.
      Select any two adjacent indices x and y (|x - y| == 1) such that nums[x] == 1 and nums[y] == 0, then swap their values (set nums[y] = 1 and nums[x] = 0). If y == aliceIndex, Alice collects the one after this move and nums[y] becomes 0.
      Return the minimum number of moves needed for Alice to collect exactly k ones.
  
      Examples:
      >>> nums = [1,1,0,0,0,1,1,0,0,1], k = 3, maxChanges = 1
      3
      >>> nums = [0,0,0,0], k = 2, maxChanges = 3
      4
  
      Constraints:
      2 <= n <= 10^5
      0 <= nums[i] <= 1
      1 <= k <= 10^5
      0 <= maxChanges <= 10^5
      maxChanges + sum(nums) >= k
      """
output:     
      n = len(nums)
      ones = [i for i, num in enumerate(nums) if num == 1]
      ones_count = len(ones)
  
      if ones_count >= k:
          min_moves = inf
          for start in range(ones_count - k + 1):
              total_moves = 0
              middle = ones[start + k // 2]
              for i in range(start, start + k):
                  total_moves += abs(middle - ones[i])
              min_moves = min(min_moves, total_moves - (k // 2) * ((k - 1) % 2))
          return min_moves
  
      left_empty = 0
      right_empty = 0
      for i in range(ones[0] - 1, -1, -1):
          if nums[i] == 0:
              left_empty += 1
          if left_empty >= maxChanges:
              break
      for i in range(ones[-1] + 1, n):
          if nums[i] == 0:
              right_empty += 1
          if right_empty >= maxChanges:
              break
  
      additional_needed = k - ones_count
      additional_moves = additional_needed - min(left_empty, right_empty)
      if additional_moves <= 0:
          return additional_needed
      return additional_needed + 2 * additional_moves - 1
  