Code

def maximumUnits(boxTypes, truckSize):
    # Step 1: Sort box types based on the number of units per box in descending order    boxTypes.sort(key=lambda x: x[1], reverse=True)
    total_units = 0    # Step 2: Greedily take boxes from the sorted list    for number_of_boxes, units_per_box in boxTypes:
        if truckSize <= 0:
            break  # The truck is full        # Step 3: Take as many boxes as possible from this type        boxes_to_take = min(number_of_boxes, truckSize)
        total_units += boxes_to_take * units_per_box
        # Step 4: Update the remaining truck capacity        truckSize -= boxes_to_take
    return total_units
# Example usage:boxTypes = [[1, 3], [2, 2], [3, 1]]
truckSize = 4result = maximumUnits(boxTypes, truckSize)
print(result)  # Expected output: 8