cf Educational Codeforces Round 62 C. Playlist

原題:
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 n songs. The i-th song is characterized by two numbers ti and bi — 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 songs having lengths [5,7,4] and beauty values [11,14,6] is equal to (5+7+4)⋅6=96.

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 k (1≤k≤n≤3⋅10^5 ) – the number of songs in the playlist and the maximum number of songs you can choose, respectively.

Each of the next n lines contains two integers ti and bi (1≤ti,bi≤10^6) — the length and beauty of i-th song.

Output
Print one integer — the maximum pleasure you can get.

Examples
Input

4 3
4 7
15 1
3 6
6 8

Output

78

Input

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

Output

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.

中文:
有n首歌,每首歌有兩個值,分別是歌曲長度和愉悅感。現在讓你選k首歌,得到的最終的總長度是這k首歌的長度和,愉悅感是這k首歌中最小值。

現在讓你選k首歌,使得選出的k首歌的愉悅感與總長度的乘積最大。

代碼:

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<string>
    #include<vector>
    #include<algorithm>
    #include<stack>
    #include<queue>
    using namespace std;
     
     
    const int maxn = 300005;
     
     
    typedef long long ll;
    typedef pair<ll, ll> pii;
     
    int n, k;
    pii ps[maxn];
    /*
    4 2
    14 15
    90 10
    80 8
    100 7
     
    */
     
    struct cmp
    {
    	bool operator()(pii& p1, pii& p2)
    	{
    		return p1.first>p2.first;
    	}
    };
     
    int main()
    {
    	ios::sync_with_stdio(false);
    	while (cin >> n >> k)
    	{
    		for (int i = 1; i <= n; i++)
    			cin >> ps[i].first >> ps[i].second;
    		sort(ps + 1, ps + 1 + n, [](pii &p1, pii &p2)
    		{
    			if (p1.second != p2.second)
    				return p1.second>p2.second;
    			else
    				return p1.first>p2.first;
    		});
     
    		ll len = 0, ans = 0;
     
    		priority_queue<pii, vector<pii>, cmp> Q;
     
    		for (int i = 1; i <= n; i++)
    		{
    			Q.push(ps[i]);
    			len += ps[i].first;
    			if (Q.size()>k)
    			{
    				len -= Q.top().first;
    				Q.pop();
    			}
    			ans = max(ans, len*ps[i].second);
     
    		}
    		cout << ans << endl;
    	}
    	return 0;
    }

思路:

將n首歌按照愉悅感從大到小排序,維持一個容量爲k的堆,堆頂元素爲當前歌曲長度最小的歌,遍歷歌曲時,每次更新當前歌曲的愉悅感值,由於歌曲按照愉悅感從小到大排序,愉悅感肯定越來越小,每次更新記錄當前k首的總長度乘以當前最小愉悅感即可。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章