<p>You are given an array of binary strings <code>strs</code> and two integers <code>m</code> and <code>n</code>.</p>

<p>Return <em>the size of the largest subset of <code>strs</code> such that there are <strong>at most</strong> </em><code>m</code><em> </em><code>0</code><em>&#39;s and </em><code>n</code><em> </em><code>1</code><em>&#39;s in the subset</em>.</p>

<p>A set <code>x</code> is a <strong>subset</strong> of a set <code>y</code> if all elements of <code>x</code> are also elements of <code>y</code>.</p>

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

<pre>
<strong>Input:</strong> strs = [&quot;10&quot;,&quot;0001&quot;,&quot;111001&quot;,&quot;1&quot;,&quot;0&quot;], m = 5, n = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The largest subset with at most 5 0&#39;s and 3 1&#39;s is {&quot;10&quot;, &quot;0001&quot;, &quot;1&quot;, &quot;0&quot;}, so the answer is 4.
Other valid but smaller subsets include {&quot;0001&quot;, &quot;1&quot;} and {&quot;10&quot;, &quot;1&quot;, &quot;0&quot;}.
{&quot;111001&quot;} is an invalid subset because it contains 4 1&#39;s, greater than the maximum of 3.
</pre>

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

<pre>
<strong>Input:</strong> strs = [&quot;10&quot;,&quot;0&quot;,&quot;1&quot;], m = 1, n = 1
<strong>Output:</strong> 2
<b>Explanation:</b> The largest subset is {&quot;0&quot;, &quot;1&quot;}, so the answer is 2.
</pre>

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

<ul>
	<li><code>1 &lt;= strs.length &lt;= 600</code></li>
	<li><code>1 &lt;= strs[i].length &lt;= 100</code></li>
	<li><code>strs[i]</code> consists only of digits <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li>
	<li><code>1 &lt;= m, n &lt;= 100</code></li>
</ul>

class Solution {
public:
    int findMaxForm(vector<string>& strs, int m, int n) {
        vector<pair<int, int>> num(strs.size(), std::pair<int, int>(0, 0));
        for(int i = 0; i < strs.size(); ++i) {
            for(char ch: strs[i]) {
                if (ch == '0') {
                    ++num[i].first;
                } else {
                    ++num[i].second;
                }
            }
            // std::cout << zeros[i] << " " << ones[i] << std::endl;
        }


        std::sort(num.begin(), num.end(), [&m, &n] (const auto& lhs, const auto& rhs) {
            return std::max(static_cast<double>(lhs.first) / m, static_cast<double>(lhs.second) / n) < std::max(static_cast<double>(rhs.first) / m, static_cast<double>(rhs.second) / n);  
        });

        for(const auto& p: num) {
            // std::cout << p.first << " " << p.second << std::endl;
        }

        priority_queue<pair<pair<int, int>, int>> pq; // ((max possible value, value), <zeroes, ones, idx>)
        pq.push({{PossibleValue(num[0].first, num[0].second, m, n), 0}, 0});

        vector<int> best_value ((num.size() + 1) * (n + 1) * (m + 1), -1);
        best_value[0] = 0;
        int result = 0;

        while (!pq.empty()) {
            auto pr = pq.top();
            int possible_value = pr.first.first;
            if (possible_value * 3 < result) {
                return result;
            }
            int value = pr.first.second;
            int zeros = pr.second % (m + 1);
            pr.second /= (m + 1);
            int ones = pr.second % (n + 1);
            int pos = pr.second / (n + 1);
            pq.pop();

            // std::cout << zeros << " " << ones << " " << pos << " " << pr.second << " " << value << " " << possible_value << std::endl;

            int state_no_take = State(zeros, ones, pos + 1, m, n);
            if (best_value[state_no_take] < value) {
                best_value[state_no_take] = value;
                if (pos + 1 < num.size()) {
                    pq.push({{PossibleValue(num[pos + 1].first, num[pos + 1].second, m, n), value},  state_no_take});
                } else {
                    result = max(result, value);
                }
            }
            if (zeros + num[pos].first <= m && ones + num[pos].second <= n) {
                int state_take = State(zeros + num[pos].first, ones + num[pos].second, pos + 1, m, n);
                if (best_value[state_take] < value + 1) {
                    best_value[state_take] = value + 1;
                    if (pos + 1 < num.size()) {
                        pq.push({{PossibleValue(num[pos + 1].first, num[pos + 1].second, m, n), value + 1}, state_take});
                    } else {
                        result = max(result, value + 1);
                    }
                }
            }

        }

        return result;
    }

    int State(int zeros, int ones, int pos, int m, int n) {
        return zeros + ones * (m + 1) + pos * (n + 1) * (m + 1);
    }

    int PossibleValue(int zeros, int ones, int m, int n) {
        if (ones == 0) {
            return m / zeros;
        }
        if (zeros == 0) {
            return n / ones;
        }
        return std::min(n / ones, m / zeros);
    }
};
59298