http://codeforces.com/problemset/problem/462/B
简单的贪心,排序即可看出来。
#include<cstdio> #include<iostream> #include<algorithm> using namespace std; int main() { long long a[26] = {0},n,k; long long sum = 0; char temp; scanf("%lld%lld",&n,&k); getchar(); for(int i = 0;i < n;i++) { temp = getchar(); a[temp-'A']++; } sort(a,a+26); for(int i = 25;i >= 0;i--) { if(k <= a[i]) { sum += k*k; break; } else { sum += a[i]*a[i]; k -= a[i]; } } printf("%lld ",sum); return 0; }