<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code> and an integer <code>k</code>. In an operation, you can choose an element and multiply it by <code>2</code>.</p>

<p>Return <em>the maximum possible value of </em><code>nums[0] | nums[1] | ... | nums[n - 1]</code> <em>that can be obtained after applying the operation on nums at most </em><code>k</code><em> times</em>.</p>

<p>Note that <code>a | b</code> denotes the <strong>bitwise or</strong> between two integers <code>a</code> and <code>b</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [12,9], k = 1
<strong>Output:</strong> 30
<strong>Explanation:</strong> If we apply the operation to index 1, our new array nums will be equal to [12,18]. Thus, we return the bitwise or of 12 and 18, which is 30.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [8,1,2], k = 2
<strong>Output:</strong> 35
<strong>Explanation:</strong> If we apply the operation twice on index 0, we yield a new array of [32,1,2]. Thus, we return 32|1|2 = 35.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
	<li><code>1 &lt;= k &lt;= 15</code></li>
</ul>

class Solution:
    def maximumOr(self, nums: List[int], k: int) -> int:
        # find a number whose highest k bits is max, let it <<k
        # if there are multi choice, select the one could make first bit 0 is in right most position in final result
        C = collections.Counter(nums) # duplicate>2 is useless
        mx = max(C)
        length = len(f'{mx:b}')
        if k >= length: # mx will be the one
            if C[mx] == 1:
                C.pop(mx)
            mx <<= k
            for n in C:
                mx |= n
            return mx
        bits = [0]*length
        for i in range(length):
            for n in C:
                if n&(1<<i):
                    if C[n] == 1:
                        bits[i] += 1
                        if bits[i] == 2: # only need 2 for each bit
                            break
                    else:
                        bits[i] = 2
                        break
        mask = int('1'*k+'0'*(length-k),2)
        target = mx&mask
        ret = 0
        for n in C:
            if n&mask == target: # highest k bits is max
                for i in range(length):
                    if n&(1<<i):
                        bits[i] -= 1
                curr = n<<k
                for i in range(length):
                    if bits[i]:
                        curr |= (1<<i)
                ret = max(ret, curr)
                for i in range(length):
                    if n&(1<<i):
                        bits[i] += 1
        return ret
521