Codeforces A. Playlist（暴力剪枝）

题目描述：

Playlist

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You have a playlist consisting of n songs. The -th song is characterized by two numbers *t**i* — its length and beauty respectively. The pleasure of listening to set of songs is equal to the total length of the songs in the set multiplied by the minimum beauty among them. For example, the pleasure of listening to a set of 3 and beauty values [11,14,6].

You need to choose at most k songs from your playlist, so the pleasure of listening to the set of these songs them is maximum possible.

Input

The first line contains two integers n and (1≤kn≤3⋅105）

Each of the next n lines contains two integers and *b**i) — the length and beauty of i*

Output

Print one integer — the maximum pleasure you can get.

Examples

Input

Copy

4 3
4 7
15 1
3 6
6 8

Output

Copy

78

Input

Copy

5 3
12 31
112 4
100 100
13 55
55 50

Output

Copy

10000

Note

In the first test case we can choose songs 1,3,4, so the total pleasure is $(4+3+6)*6=78$.

In the second test case we can choose song 3. The total pleasure will be equal to $100*100=10000$.

代码：

#include <iostream>
#include <queue>
#include <algorithm>
#define max_n 300005
using namespace std;
int n;
int k;
struct node
{
long long ti;
long long bi;
};
int cmp(node a,node b)
{
return a.bi>b.bi;
}
node a[max_n];
int main()
{
cin >> n >> k;
for(int i = 0;i<n;i++)
{
cin >> a[i].ti >> a[i].bi;
}
sort(a,a+n,cmp);
priority_queue<int,vector<int>,greater<int> > que;
long long maxm = 0;
long long sum = 0;
for(int i = 0;i<n;i++)
{
if(que.size()<k)
{
que.push(a[i].ti);
sum += a[i].ti;
maxm = max(maxm,sum*a[i].bi);
}
else
{
long long len = que.top();
que.pop();
sum -= len;
len = a[i].ti;
sum += len;
que.push(len);
maxm = max(maxm,sum*a[i].bi);
}
}
cout << maxm << endl;
return 0;
}

参考文章：

LightningUZ，Codeforces 1140C 题解，https://blog.csdn.net/LightningUZ/article/details/89036190

posted @ 2019-08-16 15:25  小张人  阅读(...)  评论(...编辑  收藏