<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>

from typing import List

class Solution:
    def maximumOr(self, nums: List[int], k: int) -> int:
        """
        Find max result by applying OR operations and left-shifting some elements.
        """

        max_bitlen = max(nums).bit_length()  # Get max bit length in nums
        res = 0
        candidate = []

        # Split nums into candidates with max bit length and accumulate OR for others
        for num in nums:
            if num.bit_length() == max_bitlen:
                candidate.append(num)
            else:
                res |= num
        
        print(f"Candidates: {candidate}")
        
        if not candidate:  # Edge case where no candidates exist
            return res
        
        # Initialize dp array where dp[i][0] means not shifting, dp[i][1] means shifting
        dp = [[0, 0] for _ in range(len(candidate))]

        # Base case for first candidate
        dp[0] = [candidate[0] | res, (candidate[0] << k) | res]

        # Fill the dp table
        for i in range(1, len(dp)):
            # Case 1: No shift applied to candidate[i]
            dp[i][0] = dp[i - 1][0] | candidate[i]
            # Case 2: Shift candidate[i] and take the maximum result
            dp[i][1] = max(dp[i - 1][0] | (candidate[i] << k), dp[i - 1][1] | candidate[i])

        # Return the maximum OR result with shifts applied
        return dp[-1][1]

# Example usage
sol = Solution()
nums = [3, 7, 2, 9]
k = 2
print(sol.maximumOr(nums, k))

32000