spoj ADAFRIEN - Ada and Friends(水题)

通过算法帮助Ada筛选不必要的开销,减少购买礼物的成本。输入包括庆祝次数及可删除好友数量,输出为节省的最大金额。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ADAFRIEN - Ada and Friends

#sorting #datastructures

 

Ada the Ladybug has many friends. They always celebrate something and Ada has to buy them gifts. It is pretty costly so she have decided to unfriend some of them. What is the maximum of money she can spare?

Input

The first line of each test-case will contain two integers 1 ≤ Q K ≤ 105, the number of celebrations (Q) and the maximum number of friends Ada wants to unfriend (K).

The next Q lines will contain s (the name of friend to whom Ada will buy gift) and 1 ≤ E ≤ 109+1 (the expenses).

Names will contain at most 40 lowercase english letters.

Output

For each test-case, print the number of money Ada could spare.

Example Input

6 1
uvuvwevwevweonyetenyevweugwemubwemossas 10
ryuk 11
uvuvwevwevweonyetenyevweugwemubwemossas 5
fegla 3
tenshikanade 7
fegla 2

Example Output

15

Example Input

4 3
frodo 1
harrypotter 2
frodo 2
harrypotter 2

Example Output

7

Example Input

7 2
waynebot 7
lhic 4
petr 5
umnik 9
izrak 6
tourist 11
zlobobber 9

Example Output

20

Example Input

6 3
dufresne 5
gump 11
dufresne 3
mcmurphy 19
leon 10
dufresne 1

Example Output

40

 

#include<bits/stdc++.h>
#define MAXN 100005
using namespace std;
map<string,long long> mp;
map<string,long long> ::iterator it;
long long a[MAXN];
int main()
{
	long long k,q;
	string s;
	scanf("%lld%lld",&q,&k);
	for(long long i=0;i<q;i++)
	{
		long long tmp;
		cin>>s;
		scanf("%lld",&tmp);
		it=mp.find(s);
		if(it==mp.end())
			mp[s]=tmp;
		else
			mp[s]+=tmp;
	}
	long long cnt=0;
	for(it=mp.begin();it!=mp.end();it++)
		a[cnt++]=(*it).second;
	sort(a,a+cnt);
	long long sum=0;
	for(long long i=cnt-1,j=0;j<k&&i>=0;i--,j++)
		sum+=a[i];
	printf("%lld\n",sum);
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值