#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <cctype>
#include <map>
#include <set>
#include <bitset>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <limits>
#include <fstream>
using namespace std;
#define mem(A, X) memset(A, X, sizeof A)
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define all(x) x.begin(), x.end()
#define sz(x) (int)((x).size())
#define rep(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
#define Rep(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define dbg(a) cout << a << endl;
#define fi first
#define se second
typedef long long int64;
int gcd(const int64 &a, const int64 &b) {return b == 0 ? a : gcd(b, a % b);}
int64 int64pow(int64 a, int64 b){if(b == 0) return 1;int64 t = int64pow(a, b / 2);if(b % 2) return t * t * a;return t * t;}
const int inf = 1 << 30;
const double eps = 1e-8;
const double pi = acos(-1.0);
const int MAX_N = 500005;
int64 n, k, ans;
string s;
map<char, int> m;
void solve()
{
m.clear();
ans = 0;
cin >> s;
rep(i, 0, sz(s)) {
++m[s[i]];
}
while (k > 0) {
int64 MAX = -inf;
int64 pos = -1;
rep(i, 0, sz(m)) {
if (MAX < m[i]) {
MAX = m[i];
pos = i;
}
}
if (k <= MAX) {
ans += k * k;
k = 0;
}
if (k > MAX) {
ans += MAX * MAX;
k -= MAX;
m[pos] = 0;
}
}
cout << ans << endl;
}
int main()
{
while (cin >> n >> k) {
solve();
}
return 0;
}
Codeforces Round #263 (Div. 2)B. Appleman and Card Game
最新推荐文章于 2022-11-22 23:15:14 发布