Codeforces 263B. Appleman and Card Game

Source: Internet
Author: User
Tags uppercase letter

B. Appleman and Card GameTime limit per test 1 secondmemory limit per test megabytes Input Standard Input Output Standard Output

Appleman has n cards. Each card has a 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 should calculate how much Toastman ' s cards has the letter equal to Letter-on i-th, then sum-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 the integers n and k (1≤ kn ≤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.

Sample Test (s)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'll get 9 coins and for the additional card he'll get 1 coin.

Problem solving: greed.

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <algorithm>6#include <climits>7#include <vector>8#include <queue>9#include <cstdlib>Ten#include <string> One#include <Set> A#include <stack> - #defineLL Long Long - #definePII pair<int,int> the #defineINF 0x3f3f3f3f - using namespacestd; - intletter[ -]; - Charstr[100100]; + BOOLcmpConst int&x,Const int&y) { -     returnX >y; + } A intMain () { at     intn,k; - LL ans; -      while(~SCANF ("%d%d",&n,&k)) { -memset (Letter,0,sizeof(letter)); -scanf"%s", str); -          for(inti =0; Str[i]; i++) letter[str[i]-'A']++; inSort (letter,letter+ -, CMP); -          for(inti = ans =0; K && I < -; i++){ toAns + = 1ll*min (K,letter[i]) *min (k,letter[i]); +K-=min (k,letter[i]); -         } thecout<<ans<<Endl; *     } $     return 0;Panax Notoginseng}
View Code

Codeforces 263B. Appleman and Card Game

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.