Hard · Dynamic Programming

Maximum AND Sum of Array

Given an integer array nums and an integer numSlots with 2 · numSlots ≥ nums.length, place every number into one of the slots numbered 1 to numSlots so that no slot holds more than two numbers, and return the maximum possible sum of (number AND its slot number).

Examples

Example 1

{
  "nums": [1, 2, 3, 4, 5, 6],
  "numSlots": 3
}

Output: 9

Example 2

{
  "nums": [2, 3, 2],
  "numSlots": 2
}

Output: 5

Rebuild it in the studio

Open every published interview problem, solution, scene, and memory room free, with no account.

More Dynamic Programming problems