<p>You are given two integers <code>n</code> and <code>k</code> and two integer arrays <code>speed</code> and <code>efficiency</code> both of length <code>n</code>. There are <code>n</code> engineers numbered from <code>1</code> to <code>n</code>. <code>speed[i]</code> and <code>efficiency[i]</code> represent the speed and efficiency of the <code>i<sup>th</sup></code> engineer respectively.</p>

<p>Choose <strong>at most</strong> <code>k</code> different engineers out of the <code>n</code> engineers to form a team with the maximum <strong>performance</strong>.</p>

<p>The performance of a team is the sum of its engineers&#39; speeds multiplied by the minimum efficiency among its engineers.</p>

<p>Return <em>the maximum performance of this team</em>. Since the answer can be a huge number, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>

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

<pre>
<strong>Input:</strong> n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 2
<strong>Output:</strong> 60
<strong>Explanation:</strong> 
We have the maximum performance of the team by selecting engineer 2 (with speed=10 and efficiency=4) and engineer 5 (with speed=5 and efficiency=7). That is, performance = (10 + 5) * min(4, 7) = 60.
</pre>

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

<pre>
<strong>Input:</strong> n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 3
<strong>Output:</strong> 68
<strong>Explanation:
</strong>This is the same example as the first but k = 3. We can select engineer 1, engineer 2 and engineer 5 to get the maximum performance of the team. That is, performance = (2 + 10 + 5) * min(5, 4, 7) = 68.
</pre>

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

<pre>
<strong>Input:</strong> n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 4
<strong>Output:</strong> 72
</pre>

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

<ul>
	<li><code>1 &lt;= k &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>speed.length == n</code></li>
	<li><code>efficiency.length == n</code></li>
	<li><code>1 &lt;= speed[i] &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= efficiency[i] &lt;= 10<sup>8</sup></code></li>
</ul>

class Solution {
public:
    int maxPerformance(int n, vector<int>& speed, vector<int>& efficiency, int k) {
        //eff,speed
        vector<pair<long long,long long>>ve(n);

        //speed,eff
        vector<pair<long long,long long>>vs(n);
        // pair<long long, long long>pp;

        // n = speed.size();
        // cout<<" n is "<<n<<endl;
        // cout<<" size is "<<speed.size()<<endl;
        // for(int i=0;i<n;i++)
        // {
        //     cout<<speed[i]<<" "<<efficiency[i]<<endl;
        // }
        // cout<<endl;
        for(int i=0;i<n;i++)
        {
            ve[i].first=(long long)efficiency[i];
            ve[i].second=(long long)speed[i];

            vs[i].first=(long long)speed[i];
            vs[i].second=(long long)efficiency[i];

            // pp.first=(long long)efficiency[i];
            // pp.second=(long long)speed[i];
            // ve.push_back(pp);

            // pp.first=(long long)speed[i];
            // pp.second=(long long)efficiency[i];
            // vs.push_back(pp);
        }

        sort(ve.begin(),ve.end());
        sort(vs.rbegin(),vs.rend());

        // for(int i=0;i<n;i++)
        // {
        //     cout<<vs[i].first<<" "<<vs[i].second<<endl;
        // }

        //eff,speed
        priority_queue<pair<long long,long long>,vector<pair<long long,long long>>,greater<pair<long long,long long>>> pq;

        long long ans=0;
        long long temp=0;
        long long mod = 1000000007;
        int j=-1;
        for(int i=0;i<n;i++)
        {
            ans=max(ans,(ve[i].first*ve[i].second));
            // ans=ans%mod;

            while(pq.empty()==false && pq.top().first < ve[i].first)
            {
                temp=(temp-pq.top().second);
                pq.pop();
            }
            while(pq.size()<k && j<n-1)
            {
                j++;
                // cout<<vs[j].second<<" "<<ve[i].first<<endl;
                long long val1 = vs[j].second;
                long long val2 = ve[i].first;
                // cout<<val1<<" "<<val2;
                if(val1>=val2)
                {
                    temp=(temp+vs[j].first);
                    pq.push({(long long)vs[j].second,(long long)vs[j].first});
                }
            }
            ans=max(ans,(temp*pq.top().first));
        }
        return(ans%mod);

        
    }
};



//           /////////

48771