class Solution:
    def minWastedSpace(self, packages: List[int], boxes: List[List[int]]) -> int:
        """
        tc O(PlgP + all(BlgB))  P: package B: boxes from each supplier
        - sort packages and boxes 
        - iterate through box in from each supplier, get the total box size that can iterate through end of packate 
        - module it in the end 
        """
        M = 10**9+7 
        min_waste = float('inf')
        packages.sort()
        total = sum(packages)
        n = len(packages)
       # print(packages)
        for j, box_l in enumerate(boxes):
            pre = 0
            waste = 0
            cnt = 0
            box_l = sorted(box_l)
            if packages[-1] > box_l[-1]: continue # tuning
            for box in box_l:
                idx = bisect.bisect_right(packages, box)
                # if idx == pre: continue
                # if idx == 0: continue       unnecessary 
                waste = (waste + box * (idx- pre)) 
                pre = idx 
                cnt += 1
            #  if pre < n: continue  this can be ignored since we checked in line 20 
            #if cnt > 0 : 
            min_waste = min(min_waste,waste)
        return (min_waste-total)%M if min_waste < float('inf') else -1