-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path2542.cpp
More file actions
25 lines (23 loc) · 703 Bytes
/
Copy path2542.cpp
File metadata and controls
25 lines (23 loc) · 703 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution {
typedef pair<long long, long long> elem;
public:
long long maxScore(vector<int> &nums1, vector<int> &nums2, int k) {
int n = nums1.size();
vector<elem> arr(n);
for (int i = 0; i < n; ++i)
arr[i] = {nums2[i], nums1[i]};
sort(rbegin(arr), rend(arr));
long long sum = 0, res = 0;
priority_queue<int, vector<int>, greater<int>> pq;
for (auto &[a, b] : arr) {
pq.emplace(b);
sum += b;
if (pq.size() > k) {
sum -= pq.top();
pq.pop();
}
if (pq.size() == k) res = max(res, sum * a);
}
return res;
}
};