Codeforces Appleman and Card Game

/*
Lionel Messi is such a player that you may catch him, you may touch him, you may feel him
and definitely you may Love him.
Lionel Messi is Messi. A little Magician in this World.

*/

#include<bits/stdc++.h>

using namespace std;

#define fast ios_base::sync_with_stdio(0)
#define bfast cin.tie(0)
#define outs(x) cout << x << " "
#define outn(x) cout << x << "\n"
#define sf scanf
#define pf printf
#define nl puts("")
#define psb push_back

typedef long long LL;
typedef vector<int>vii;
typedef vector<LL>vll;
typedef map<char, int>mpci;

const int mod = 1000007;
const int high = 100;

LL ar[256];

bool cmp(LL a, LL b)
{
     return a > b;
}

int main()
{
    fast;
    LL n , k , i , ans=0;
    string s;

    while(cin >> n >> k)
    {
        ans=0;
        memset(ar , 0, sizeof ar);

        cin >> s;

        int len = s.length();

        for(i=0; i<len; i++) ar[s[i]]++;

        sort(ar , ar+256, cmp);

        i=0;

        while(k >= ar[i])
        {
            ans+=(ar[i] * ar[i]);
            k-=ar[i];
            i++;
        }

        ans+=(k * k);

        outn(ans);
    }

    return 0;
}

Comments

Popular posts from this blog

SPOJ-CMG - Collecting Mango

LightOJ 1009 - Back to Underworld

LeetCode Palindrome Number