You have a spell that deals power damage per cast. You are given two arrays damage and health, both of length n, where damage[i] is the damage dealt to you per turn by the i-th enemy and health[i] is the health of the i-th enemy.
Each turn, all alive enemies deal their respective damage to you simultaneously. Then you cast your spell on exactly one alive enemy, dealing power damage to it. An enemy dies when its health drops to 0 or below, so it takes ceil(health[i] / power) turns to kill enemy i.
Return the minimum total damage you will take until all enemies are killed. You may choose the order in which you target enemies.
Function Signature
def min_damage(power: int, damage: list[int], health: list[int]) -> int:
Rules
-
damage[i]
and
health[i]
describe the same enemy
i
.
-
Enemies attack before you cast, so an enemy still deals its damage on the turn you kill it.
-
Return the exact total as an integer. Under the constraints below it can be far larger than a 64-bit integer can hold.
Constraints
-
1 <= power <= 10^9
-
1 <= n <= 10^5
-
n == len(damage) == len(health)
-
1 <= damage[i], health[i] <= 10^9
Examples
Input: power = 4, damage = [1, 2, 3], health = [4, 4, 4]
Output: 10
Every enemy dies in one cast. Killing them in the order 2, 1, 0 (damage 3, then 2, then 1) costs 6 on the first turn while all three are alive, 3 on the second and 1 on the third, for a total of 10.
Input: power = 1, damage = [1, 1, 1], health = [1, 2, 3]
Output: 10
The enemies need 1, 2 and 3 casts. Killing them in the order 0, 1, 2 costs 1 turn with all three alive (3), then 2 turns with two alive (4), then 3 turns with one alive (3), for a total of 10.
Input: power = 2, damage = [10, 1, 6], health = [10, 2, 3]
Output: 90
The enemies need 5, 1 and 2 casts. The best order is 2, 0, 1: 2 turns at 17 damage per turn, 5 turns at 11 and 1 turn at 1, for 34 + 55 + 1 = 90. Killing the highest-damage enemy first (order 0, 2, 1) costs 100, and killing the quickest one first (order 1, 2, 0) costs 99.