0
点赞
收藏
分享

微信扫一扫

牛客白月赛25【题解】

蛇发女妖 2022-05-06 阅读 70

目录

AOE还是单体?【贪心】

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
typedef long long int LL;
const int N=1e5*2+10;
LL a[N],n,x,sum;
map<int,int>mp;
vector< pair<int,int> >ve;
int main(void) 
{
	cin>>n>>x;
	for(int i=0;i<n;i++)
	{
		 cin>>a[i];
		 mp[a[i]]++;
		 sum+=a[i];
	}
	for(auto i=mp.begin();i!=mp.end();i++) 
		ve.push_back({i->first,i->second});
	LL index=-1;
	for(int i=0;i<ve.size();i++)
	{
		int cnt=ve.size()-i;//剩余人数
		if(cnt>x) index=i;
	}
	if(index==-1) cout<<sum;
	else
	{
		LL ans=ve[index].first*x;
		for(int i=index+1;i<ve.size();i++) 
            ans+=(ve[i].first-ve[index].first)*ve[i].second;
		cout<<ans;
	}
	return 0;
}
举报

相关推荐

0 条评论