1011A Codeforces - Stages


1011A Codeforces - Stages






Solution in C++


#include <bits/stdc++.h>

using namespace std;

#define pii pair<int,int>
typedef long long int lli;

int n,k,sol;
string inp;
char lst;
vector<char> all;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin>>n>>k>>inp;
    for(int i=0;i<n;i++)all.push_back(inp[i]);
    sort(all.begin(),all.end(),greater<int>());

    for(int i=0;i<n;i++)
    {
        if(all.size()==0 || k==0)break;
        sol+=all.back()-'a'+1;
        char lst=all.back();
        all.pop_back();
        while(all.size()>0 && all.back()<=char(lst+1))all.pop_back();
        k--;
    }
    if(k==0)cout<<sol;
    else cout<<-1;
    return 0;
}
Share on Google Plus

About Mohamed Sobhi

My name is Mo.Sobhy ,I study at School excelling high school in Ain Shams ,Cairo
My hobbies is programming ,and web development ,playing chess and writing horror stories.
    Blogger Comment
    Facebook Comment

0 التعليقات:

إرسال تعليق