input: from typing import List
  
  def longest_optimal_subsequence(nums: List[int], k: int) -> int:
      """
      Given a list of integers nums and a non-negative integer k, a sequence is defined as optimal if there are at most k indices i in the range [0, seq.length - 2] where seq[i] differs from seq[i + 1].
  
      Determine the maximum length of an optimal subsequence in nums.
  
      Examples:
      >>> nums = [1,2,1,1,3], k = 2
      4
      >>> nums = [1,2,3,4,5,1], k = 0
      2
  
      Constraints:
      1 <= len(nums) <= 500
      1 <= nums[i] <= 10^9
      0 <= k <= min(len(nums), 25)
      """
output:     
      n = len(nums)
      max_length = 0
  
      for i in range(n):
          count_diffs = 0
          current_length = 1
          for j in range(i, n - 1):
              if nums[j] != nums[j + 1]:
                  count_diffs += 1
              if count_diffs > k:
                  break
              current_length += 1
          max_length = max(max_length, current_length)
  
      return max_length
  