class Solution:
    def minimumEffort(self, tasks: List[List[int]]) -> int:
        """
        TODO: thought:  1. binary search  left-  sum(actual )  right: sum(minimun)
        tc O(NlgN) sc O(1)
        each time remaining  saved = mmin - cost, next time mmin  = original mmin- prev_saved 
        we need to maximize each time saved, so max(mmin-cost) = > get min cost-min fisrt 
        """
        tasks.sort(key = lambda x: x[0]-x[1])
        res = prev_saved = 0
        for cost, mmin in tasks:
            if mmin > prev_saved:
                res += mmin - prev_saved#  only update when additional energy is required to add to get next task 
                prev_saved = mmin
            prev_saved -= cost 
        return res