fork download
  1. import math
  2.  
  3. def minDamage(power: int, damage: list[int], health: list[int]) -> int:
  4. n = len(damage)
  5. enemies = []
  6.  
  7. # Precalculate (damage, time_to_kill) for each enemy
  8. for d, h in zip(damage, health):
  9. time_to_kill = (h + power - 1) // power # Ceiling division
  10. enemies.append((d, time_to_kill))
  11.  
  12. # Custom sort using cross-multiplication (D_A * T_B > D_B * T_A)
  13. # Python sorts ascending by default, so we invert the comparison logic
  14. from functools import cmp_to_key
  15.  
  16. def compare(a, b):
  17. # a = (d_a, t_a), b = (d_b, t_b)
  18. if a[0] * b[1] > b[0] * a[1]:
  19. return -1 # 'a' comes before 'b'
  20. return 1
  21.  
  22. enemies.sort(key=cmp_to_key(compare))
  23.  
  24. total_dps = sum(d for d, t in enemies)
  25. total_damage_taken = 0
  26.  
  27. # Process enemies in optimal order
  28. for d, t in enemies:
  29. total_damage_taken += total_dps * t
  30. total_dps -= d # Enemy is dead, remove its damage output
  31.  
  32. return total_damage_taken
Success #stdin #stdout 0.11s 14024KB
stdin
Standard input is empty
stdout
Standard output is empty