<p>You are given a binary string <code>s</code> and a positive integer <code>k</code>.</p>

<p>Return <em>the length of the <strong>longest</strong> subsequence of </em><code>s</code><em> that makes up a <strong>binary</strong> number less than or equal to</em> <code>k</code>.</p>

<p>Note:</p>

<ul>
	<li>The subsequence can contain <strong>leading zeroes</strong>.</li>
	<li>The empty string is considered to be equal to <code>0</code>.</li>
	<li>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</li>
</ul>

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

<pre>
<strong>Input:</strong> s = &quot;1001010&quot;, k = 5
<strong>Output:</strong> 5
<strong>Explanation:</strong> The longest subsequence of s that makes up a binary number less than or equal to 5 is &quot;00010&quot;, as this number is equal to 2 in decimal.
Note that &quot;00100&quot; and &quot;00101&quot; are also possible, which are equal to 4 and 5 in decimal, respectively.
The length of this subsequence is 5, so 5 is returned.
</pre>

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

<pre>
<strong>Input:</strong> s = &quot;00101001&quot;, k = 1
<strong>Output:</strong> 6
<strong>Explanation:</strong> &quot;000001&quot; is the longest subsequence of s that makes up a binary number less than or equal to 1, as this number is equal to 1 in decimal.
The length of this subsequence is 6, so 6 is returned.
</pre>

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

<ul>
	<li><code>1 &lt;= s.length &lt;= 1000</code></li>
	<li><code>s[i]</code> is either <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li>
	<li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution:
    def longestSubsequence(self, s: str, k: int) -> int:
        # Initialize variables
        n = len(s)
        value = 0
        length = 0
        
        # List to store the indices of '1's we might consider including
        ones_indices = []
        
        # First, count all the '0's since they can be included without increasing value
        for i in range(n):
            if s[i] == '0':
                length += 1
            else:
                ones_indices.append(i)
        
        # Try to include '1's starting from the least significant bit
        # We traverse the list of '1's from the end to the start
        for i in reversed(ones_indices):
            # Calculate the potential new value if we include this '1'
            # The position in the original string s gives us the power of 2
            potential_value = value + (1 << (n - 1 - i))
            
            # Check if including this '1' keeps the value within the limit k
            if potential_value <= k:
                value = potential_value
                length += 1
        
        return length

# Example usage
solution = Solution()
print(solution.longestSubsequence("1001010", 5))  # Output: 5
print(solution.longestSubsequence("00101001", 1)) # Output: 6
30