input: from typing import List
  from collections import Counter
  from functools import cache
  def max_total_damage(power: List[int]) -> int:
      """
      A magician has various spells.
  
      You are given an array power, where each element represents the damage of a spell. Multiple spells can have the same damage value.
  
      It is a known fact that if a magician decides to cast a spell with a damage of power[i], they cannot cast any spell with a damage of power[i] - 2, power[i] - 1, power[i] + 1, or power[i] + 2.
  
      Each spell can be cast only once.
  
      Return the maximum possible total damage that a magician can cast.
  
      Examples:
      >>> power = [1,1,3,4]
      6
      >>> power = [7,1,6,6]
      13
  
      Constraints:
      1 <= power.length <= 10^5
      1 <= power[i] <= 10^9
      """
  
output: 
      power_count = Counter(power)
      unique_powers = sorted(power_count.keys())
      
      @cache
      def dp(i: int) -> int:
          if i < 0:
              return 0
          if i == 0:
              return unique_powers[0] * power_count[unique_powers[0]]
          
          current_power = unique_powers[i]
          prev_power = unique_powers[i - 1] if i > 0 else -2
          prev_prev_power = unique_powers[i - 2] if i > 1 else -3
          
          if current_power - prev_power > 2:
              return dp(i - 1) + current_power * power_count[current_power]
          else:
              return max(dp(i - 1), dp(i - 2) + current_power * power_count[current_power])
      
      return dp(len(unique_powers) - 1)
  