It's a simple question.
Sort letters from a number to a small order and then get the most from the problem algorithm
But note that the data type declares money to be a long long
1#include <iostream>2#include <stdio.h>3#include <string.h>4#include <algorithm>5 using namespacestd;6 7 8 intnum[ +];9 Long Longmoney[ +];//because although N is to 10^5 but the operations that have money and money multiplied are stored in the same location, it's super.Ten intN, K; One A BOOLcmpintAintb) - { - returnA >b; the } - intMain () - { - Doublesum =0; +Freopen ("In.txt","R", stdin); - while(~SCANF ("%d%d", &n, &k)) + { A GetChar (); atsum =0; -memset (NUM,0,sizeof(num)); - for(inti =0; I < n; i++) - { - Chartemp; -scanf"%c", &temp); innum[temp-'A']++; - } toSort (num, num+ -, CMP); + for(inti =0; I < -; i++) - { theMoney[i] =min (num[i], k); *K-=Num[i]; $ if(k <=0) Break;Panax Notoginseng } - for(inti =0; I < -; i++) the { +sum+= money[i]*Money[i]; A } theprintf"%.0lf\n", sum); + } - return 0; $ $}
codeforces-462b Appleman and Card Game