You are given an integer array weights and two integers w1 and w2 representing the maximum capacities of two bags.
Each item may be placed in at most one bag such that:
w1 total weight.w2 total weight.Return the maximum total weight that can be packed into the two bags.
Example 1:
Input: weights = [1,4,3,2], w1 = 5, w2 = 4
Output: 9
Explanation:
weights[2] = 3 and weights[3] = 2 as 3 + 2 = 5 <= w1weights[1] = 4 as 4 <= w25 + 4 = 9Example 2:
Input: weights = [3,6,4,8], w1 = 9, w2 = 7
Output: 15
Explanation:
weights[3] = 8 as 8 <= w1weights[0] = 3 and weights[2] = 4 as 3 + 4 = 7 <= w28 + 7 = 15Example 3:
Input: weights = [5,7], w1 = 2, w2 = 3
Output: 0
Explanation:
No weight fits in either bag, thus the answer is 0.
Constraints:
1 <= weights.length <= 1001 <= weights[i] <= 1001 <= w1, w2 <= 300Loading editor...
[1,4,3,2] 5 4