import math

def minDamage(power: int, damage: list[int], health: list[int]) -> int:
    n = len(damage)
    enemies = []
    
    # Precalculate (damage, time_to_kill) for each enemy
    for d, h in zip(damage, health):
        time_to_kill = (h + power - 1) // power  # Ceiling division
        enemies.append((d, time_to_kill))
    
    # Custom sort using cross-multiplication (D_A * T_B > D_B * T_A)
    # Python sorts ascending by default, so we invert the comparison logic
    from functools import cmp_to_key
    
    def compare(a, b):
        # a = (d_a, t_a), b = (d_b, t_b)
        if a[0] * b[1] > b[0] * a[1]:
            return -1  # 'a' comes before 'b'
        return 1

    enemies.sort(key=cmp_to_key(compare))
    
    total_dps = sum(d for d, t in enemies)
    total_damage_taken = 0
    
    # Process enemies in optimal order
    for d, t in enemies:
        total_damage_taken += total_dps * t
        total_dps -= d  # Enemy is dead, remove its damage output
        
    return total_damage_taken