【Codeforces Round 263 (Div 2)B】【水题 贪心】Appleman and Card Game 选字符,每个字符个数的平方


Appleman and Card Game
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Appleman has n cards. Each card has an uppercase letter written on it. Toastman must choose k cards from Appleman's cards. Then Appleman should give Toastman some coins depending on the chosen cards. Formally, for each Toastman's card i you should calculate how much Toastman's cards have the letter equal to letter on ith, then sum up all these quantities, such a number of coins Appleman should give to Toastman.

Given the description of Appleman's cards. What is the maximum number of coins Toastman can get?

Input

The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105). The next line contains n uppercase letters without spaces — the i-th letter describes the i-th card of the Appleman.

Output

Print a single integer – the answer to the problem.

Examples
input
15 10
DZFDFZDFDDDDDDF
output
82
input
6 4
YJSNPI
output
4
Note

In the first test example Toastman can choose nine cards with letter D and one additional card with any letter. For each card with D he will get 9 coins and for the additional card he will get 1 coin.


#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 0, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
int n, k;
int cnt[26];
int main()
{
	while (~scanf("%d%d", &n,&k))
	{
		for (int i = 1; i <= n; ++i)
		{
			char ch; scanf(" %c", &ch);
			ch -= 'A';
			++cnt[ch];
		}
		LL ans = 0;
		sort(cnt, cnt + 26);
		for (int i = 25; i >= 0; --i)
		{
			LL take = min(k, cnt[i]);
			k -= take;
			ans += take*take;
		}
		printf("%lld\n", ans);
	}
	return 0;
}
/*
【题意】
给你一个字符串,长度为n(1e5)
我们选其中的k个,使得∑每个字符i(i∈[1,k])*该字符个数尽可能大

【类型】
水题 贪心

【分析】
直接贪心选择数量最多的字符即可

【时间复杂度&&优化】
O(n)
*/


### Codeforces Round 1028 (Div. 2) 目概述 Codeforces Round 1028 (Div. 2) 是一场包含多个算法问的比赛,涵盖了从简单到复杂的不同难度级别。以下是该比赛的部分目内容及简要说明: #### A. Gellyfish and Flower 在本中,有两个角色:Gellyfish 和 Flower。每个角色有两滴血量,分别用 \(a\) 和 \(c\) 表示 Gellyfish 的血量,用 \(b\) 和 \(d\) 表示 Flower 的血量[^3]。 获胜条件是攻击对方的最低血量,并比较双方的最低血量值。如果 Gellyfish 的最低血量小于 Flower 的最低血量,则 Gellyfish 获胜;否则,Flower 获胜。 #### B. Problem - B - Codeforces B 的具体内容未完全提供,但通常涉及数组、字符串或其他数据结构的操作。可以参考比赛页面获取完整描述。 #### C. Rectangles C 要求处理矩形的相关问。具体来说,给定一些矩形的边长信息,需要判断某些条件是否满足。时间限制为每组测试用例 2 秒,内存限制为 256 MB[^2]。输入和输出均为标准格式,详细规则可参考比赛页面。 ### 示例代码(A ) 以下是一个实现 A 逻辑的 C++ 程序: ```cpp #include <iostream> #include <algorithm> using namespace std; int main() { int t; cin >> t; while (t--) { int a, b, c, d; cin >> a >> b >> c >> d; int mina = min(a, c); int minb = min(b, d); if (mina < minb) cout << "Flower" << endl; else cout << "Gellyfish" << endl; } } ``` 此代码通过多次循环读取输入并计算两个角色的最低血量,最终输出获胜者名称。 ### 注意事项 - 比赛中的所有目均可以通过官方链接访问,例如 [Codeforces Round 1028 (Div. 2)](http://codeforces.com/contest/1028)[^2]。 - 每道目的详细规则和样例输入输出可在对应页面找到。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值