C. Playlist Educational Codeforces Round 62 (Rated for Div. 2) 贪心+优先队列

C. 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 nn songs. The ii-th song is characterized by two numbers titi and bibi — 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 33 songs having lengths [5,7,4][5,7,4] and beauty values [11,14,6][11,14,6] is equal to (5+7+4)⋅6=96(5+7+4)⋅6=96.

You need to choose at most kk 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 nn and kk (1≤k≤n≤3⋅1051≤k≤n≤3⋅105) – the number of songs in the playlist and the maximum number of songs you can choose, respectively.

Each of the next nn lines contains two integers titi and bibi (1≤ti,bi≤1061≤ti,bi≤106) — the length and beauty of ii-th song.

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,41,3,4, so the total pleasure is (4+3+6)⋅6=78(4+3+6)⋅6=78.

In the second test case we can choose song 33. The total pleasure will be equal to 100⋅100=10000100⋅100=10000.

这个题目要好好想想,一开始以为是dp,因为数据太大可以发现肯定不是dp,后来想到用贪心,但是并不会写。。。
实际上这个是贪心+优先队列,用优先队列来处理,然后进行贪心。
优先队列怎么处理呢,首先我们对b进行排序,b大的放在前面,然后把每一个数的t值都放进队列里面,t越小放在越前面,
然后如果这个队列的size大于k,就要清除头部的那个元素,但是这个时候之后的取最大值并不会受到影响,因为如果是刚刚放进去
的被pop出来了,那sum肯定没有变大,而且同时b变小了,所以这个时候就不会对这里进行转移,如果是别的元素pop出来了,
那就没有影响了。
这里要想清楚为什么是对b进行排序,想清楚为什么对b进行排序之后进行优先队列的处理,就是最优的。
这个我之前没有想清楚,所以就写题目的时候就有各种天马行空的想法。。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long ll;
const int maxn = 3e5 + 100;
struct node
{
	int t, b;
	node(int t=0,int b=0):t(t),b(b){}
	bool operator<(const node&a)const
	{
		if (a.b == this->b) return a.t < this->t;
		return a.b < this->b;
	}
}exa[maxn];
priority_queue<ll, vector<ll>, greater<ll>>que;
int main()
{
	ll sum = 0,ans=0;
	int n, k;
	cin >> n >> k;
	for (int i = 1; i <= n; i++) scanf("%d%d", &exa[i].t, &exa[i].b);
	sort(exa + 1, exa + 1 + n);
	for(int i=1;i<=n;i++)
	{
		sum += exa[i].t;
		que.push(exa[i].t);
		if(que.size()>k)
		{
			sum -= que.top();
			que.pop();
		}
		//printf("exa[%d]=%d %lld  %lld\n", i, exa[i].t, sum,ans);
		ans = max(ans, sum*exa[i].b);
	}
	printf("%lld\n", ans);
	return 0;
}

  

原文地址:https://www.cnblogs.com/EchoZQN/p/10630161.html

时间: 2024-10-20 05:51:18

C. Playlist Educational Codeforces Round 62 (Rated for Div. 2) 贪心+优先队列的相关文章

Educational Codeforces Round 62 (Rated for Div. 2)

layout: post title: Educational Codeforces Round 62 (Rated for Div. 2) author: "luowentaoaa" catalog: true tags: mathjax: true - codeforces - dp --- " target="_blank" style="font-size:24px;">传送门 D - Minimum Triangulat

Educational Codeforces Round 36 (Rated for Div. 2)

Educational Codeforces Round 36 (Rated for Div. 2) F. Imbalance Value of a Tree You are given a tree T consisting of n vertices. A number is written on each vertex; the number written on vertex i is ai. Let's denote the function I(x,?y) as the differ

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars There are n pillars aligned in a row and numbered from 1 to n. Initially each pillar contains exactly one disk. The i-th pillar contains a disk having radius ai. You can move these disks

Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations

原文链接:https://www.cnblogs.com/xwl3109377858/p/11405773.html Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations You are given a sequence of n pairs of integers: (a1,b1),(a2,b2),…,(an,bn). This sequence is called bad if it is

Educational Codeforces Round 36 (Rated for Div. 2) 题解

Educational Codeforces Round 36 (Rated for Div. 2) 题目的质量很不错(不看题解做不出来,笑 Codeforces 920C 题意 给定一个\(1\)到\(n\)组成的数组,只可以交换某些相邻的位置,问是否可以将数组调整为升序的 解题思路 首先如果每个数都能通过交换到它应该到的位置,那么就可以调整为升序的. 但实际上交换是对称的,如果应该在的位置在当前位置前方的数都交换完成,那么整体就是排好序的,因为不可能所有不在相应位置的数都在相应位置的后方.

Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes

Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://codeforces.com/contest/985/problem/E Description Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome w

Educational Codeforces Round 55 (Rated for Div. 2)

Educational Codeforces Round 55 (Rated for Div. 2) 链接 A Vasya and Book 傻逼题..注意判边界. #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<set> #include<map> #include<vector> #include<cm

Educational Codeforces Round 57 (Rated for Div. 2)

get人生第二场CF! 成绩:(exACM) rank858 AC3/7 Penalty57 rating1648(+52) 题目:Educational Codeforces Round 57 (Rated for Div. 2) 错题题解: D. Easy Problem E. The Top Scorer F. Inversion Expectation G. Lucky Tickets 原文地址:https://www.cnblogs.com/xht37/p/10198321.html

Educational Codeforces Round 58 (Rated for Div. 2)(待更新)

get人生第七场CF! 成绩:(exACM) rank AC3/7 Penalty104 rating() 题目:Educational Codeforces Round 58 (Rated for Div. 2) 错题题解: C. Division and Union 原文地址:https://www.cnblogs.com/xht37/p/10260260.html