Apply Operations on Array to Maximize Sum of Squares
You are given a 0-indexed integer array nums and a positive integer k.
You can do the following operation on the array any number of times:
- Choose any two distinct indices
iandjand simultaneously update the values ofnums[i]to(nums[i] AND nums[j])andnums[j]to(nums[i] OR nums[j]). Here,ORdenotes the bitwiseORoperation, andANDdenotes the bitwiseANDoperation.
You have to choose k elements from the final array and calculate the sum of their squares.
Return the maximum sum of squares you can achieve.
Since the answer can be very large, return it modulo 10^9 + 7.
Example 1
Input
nums = [2,6,5,8], k = 2Output
261After applying operations, we can choose 15 and 6 from the final array, giving 15^2 + 6^2 = 261, which is maximum.
Example 2
Input
nums = [4,5,4,7], k = 3Output
90No operations are needed, and choosing 7, 5, and 4 gives 7^2 + 5^2 + 4^2 = 90, which is maximum.
Constraints
- 1 <= k <= nums.length <= 10^5
- 1 <= nums[i] <= 10^9