Skip to content

Commit aec452c

Browse files
authored
Create 2542.Maximum-Subsequence-Score.cpp
1 parent 65094f9 commit aec452c

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using LL = long long;
2+
class Solution {
3+
public:
4+
long long maxScore(vector<int>& nums1, vector<int>& nums2, int k)
5+
{
6+
int n = nums1.size();
7+
vector<pair<LL,LL>>arr;
8+
for (int i=0; i<n; i++)
9+
arr.push_back({nums2[i], nums1[i]});
10+
11+
sort(arr.rbegin(), arr.rend());
12+
13+
priority_queue<LL, vector<LL>, greater<>>pq;
14+
LL minVal = INT_MAX;
15+
LL sum = 0;
16+
LL ret = 0;
17+
for (int i=0; i<n; i++)
18+
{
19+
minVal = arr[i].first;
20+
sum += arr[i].second;
21+
pq.push(arr[i].second);
22+
if (pq.size()>k)
23+
{
24+
sum -= pq.top();
25+
pq.pop();
26+
}
27+
if (pq.size()==k)
28+
ret = max(ret, sum * minVal);
29+
}
30+
return ret;
31+
32+
}
33+
};

0 commit comments

Comments
 (0)