<p>You are given <code>n</code> item&#39;s value and label as two integer arrays <code>values</code> and <code>labels</code>. You are also given two integers <code>numWanted</code> and <code>useLimit</code>.</p>

<p>Your task is to find a subset of items with the <strong>maximum sum</strong> of their values such that:</p>

<ul>
	<li>The number of items is <strong>at most</strong> <code>numWanted</code>.</li>
	<li>The number of items with the same label is <strong>at most</strong> <code>useLimit</code>.</li>
</ul>

<p>Return the maximum sum.</p>

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

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">values = [5,4,3,2,1], labels = [1,1,2,2,3], numWanted = 3, useLimit = 1</span></p>

<p><strong>Output:</strong> <span class="example-io">9</span></p>

<p><strong>Explanation:</strong></p>

<p>The subset chosen is the first, third, and fifth items with the sum of values 5 + 3 + 1.</p>
</div>

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

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">values = [5,4,3,2,1], labels = [1,3,3,3,2], numWanted = 3, useLimit = 2</span></p>

<p><strong>Output:</strong> <span class="example-io">12</span></p>

<p><strong>Explanation:</strong></p>

<p>The subset chosen is the first, second, and third items with the sum of values 5 + 4 + 3.</p>
</div>

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

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">values = [9,8,8,7,6], labels = [0,0,0,1,1], numWanted = 3, useLimit = 1</span></p>

<p><strong>Output:</strong> <span class="example-io">16</span></p>

<p><strong>Explanation:</strong></p>

<p>The subset chosen is the first and fourth items with the sum of values 9 + 7.</p>
</div>

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

<ul>
	<li><code>n == values.length == labels.length</code></li>
	<li><code>1 &lt;= n &lt;= 2 * 10<sup>4</sup></code></li>
	<li><code>0 &lt;= values[i], labels[i] &lt;= 2 * 10<sup>4</sup></code></li>
	<li><code>1 &lt;= numWanted, useLimit &lt;= n</code></li>
</ul>

class Solution {
public:
    int largestValsFromLabels(vector<int>& values, vector<int>& labels, int numWanted, int useLimit)
    {
        unordered_map<int, vector<int> *> *t;
        unordered_map<int, vector<int> *>::iterator p;
        vector<int> *u, *v;
        pair<int, vector<int> *> *w;
        int i, j, k, m, n, q, r;

        t = new unordered_map<int, vector<int> *>();
        for (i = 0; i < values.size(); i++) {
            if ((p = t->find(labels[i])) == t->end()) {
                u = new vector<int>();
                t->insert({ labels[i], u });
            } else
                u = p->second;
            u->push_back(values[i]);
        }
        n = t->size();
        w = new pair<int, vector<int> *>[n];
        std::copy(t->begin(), t->end(), &w[0]);
        for (i = 0; i < n; i++) {
            u = w[i].second;
            std::sort(u->begin(), u->end(),
                      [](int a, int b) {
                          return a > b;
                      });
        }
        std::sort(&w[0], &w[n],
                  [](pair<int, vector<int> *> a,
                     pair<int, vector<int> *> b) {
                      return (*a.second)[0] > (*b.second)[0];
                  });
        v = new vector<int>();
        i = k = 0;
        while (i < n) {
            u = w[i].second;
            m = u->size();
            if (m > useLimit)
                m = useLimit;
            for (j = 0; j < m; j++) {
                v->push_back((*u)[j]);
                k++;
            }
            i++;
        }
        std::sort(v->begin(), v->end());
        m = v->size();
        if (m > numWanted)
            m = numWanted;
        r = v->back();
        for (k = 1, i = v->size() - 2; k < m; i--, k++)
            r += (*v)[i];
        delete v;
        for (i = 0; i < n; i++)
            delete w[i].second;
        delete[] w;
        delete t;

        return r;
    }
};
26700