link
class Solution {
public:
#define LL long long
int maxPerformance(int n, vector<int>& speed, vector<int>& efficiency, int k) {
vector<pair<int,int>> spef;
priority_queue<int, vector<int>, greater<int>> pq;
for(int i=0;i<n;i++){
spef.push_back({efficiency[i],speed[i]});
}
sort(spef.begin(), spef.end());
LL res=0;
LL sum=0;
for(int i=n-1;i>=n-k;i--){
sum+=spef[i].second;
pq.push(spef[i].second);
res=max(res,sum*spef[i].first);
}
for(int i=n-k-1;i>=0;i--){
if(spef[i].second>pq.top()){
sum-=pq.top();
sum+=spef[i].second;
pq.pop();
pq.push(spef[i].second);
res=max(res,sum*spef[i].first);
}
}
int mod=1E9+7;
return (int)(res%mod);
}
};