You are given an array parcels of parcel weights. You remove the parcels one at a time, and in each round you remove either the first or the last parcel of what remains.
Rounds are numbered starting from 1. Removing a parcel of weight w in round i adds i * w to the total efficiency.
Return the maximum total efficiency achievable after removing all parcels.
Function Signature
def max_efficiency(parcels: list[int]) -> int:
Rules
-
Each round removes exactly one parcel, so there are
len(parcels)
rounds.
-
When one parcel remains, it is both the first and the last parcel.
Constraints
-
1 <= parcels.length <= 1000
-
1 <= parcels[i] <= 1000
Examples
Input: parcels = [3, 1, 5, 2]
Output: 31
Remove 2 from the right in round 1 (1 * 2 = 2), then 3 from the left in round 2 (2 * 3 = 6), then 1 in round 3 (3 * 1 = 3), and finally 5 in round 4 (4 * 5 = 20). The total is 2 + 6 + 3 + 20 = 31. Removing 3, 2, 1, 5 instead gives 3 + 4 + 3 + 20 = 30, and none of the 8 possible orders beats 31.
Input: parcels = [5]
Output: 5
There is only one parcel, removed in round 1 for 1 * 5 = 5.
Input: parcels = [2, 3, 5, 1, 4]
Output: 50
Remove 2, 4, 1, 3, 5 in that order: 1 * 2 + 2 * 4 + 3 * 1 + 4 * 3 + 5 * 5 = 2 + 8 + 3 + 12 + 25 = 50. Always removing the lighter end gives the order 2, 3, 4, 1, 5 and a total of 49.