Maximum Alternating-Index Packet Value
Problem statement
You are given nonnegative packet values and an integer budget. Select exactly budget packets. The selected packets must alternate between original 0-based even and odd indices; the first selected packet may come from either parity.
The selected values may be arranged in any order that satisfies the parity alternation. Return the maximum possible total value.
Function
maximumAlternatingPacketValue(packetValues: int[], budget: int) → longExamples
Example 1
packetValues = [8,1,7,5,4,9]budget = 4return = 29Choose even-index values 8 and 7 plus odd-index values 9 and 5.
Example 2
packetValues = [8,1,7,5,4,9]budget = 5return = 33Starting with an even index permits three even-index and two odd-index packets.
Constraints
1 <= packetValues.length <= 2000000 <= packetValues[i] <= 10^91 <= budget <= packetValues.length
Source note: Interview prompt excerpt preserved as source evidence.