input: from typing import List
  
  def battle_max_score(energies: List[int], initial_energy: int) -> int:
      """
      Given an integer array energies representing the energy values of various adversaries,
      and an integer initial_energy indicating the amount of energy you start with.
  
      You begin with 0 points, and all adversaries are initially unmarked.
  
      You can undertake either of the following actions multiple times to accrue points:
  
      Select an unmarked adversary i where initial_energy >= energies[i]. If choosing this:
      You earn 1 point.
      Your energy is decreased by the adversary's energy, i.e. initial_energy = initial_energy - energies[i].
      If you have at least 1 point, you can select an unmarked adversary i. If choosing this:
      Your energy is increased by the adversary's energy, i.e. initial_energy = initial_energy + energies[i].
      The adversary i becomes marked.
      Return the maximum points you can achieve by optimally performing the actions.
  
      Examples:
      >>> energies = [3,2,2], initial_energy = 2
      3
      >>> energies = [2], initial_energy = 10
      5
      
      Constraints:
      1 <= energies.length <= 10^5
      1 <= energies[i] <= 10^9
      0 <= initial_energy <= 10^9
      """
output:     energies.sort()
      points = 0
      max_points = 0
      left = 0
      right = len(energies) - 1
  
      while left <= right:
          if initial_energy >= energies[left]:
              initial_energy -= energies[left]
              points += 1
              max_points = max(max_points, points)
              left += 1
          elif points > 0:
              points -= 1
              initial_energy += energies[right]
              right -= 1
          else:
              break
  
      return max_points