Code:

#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
inline int get(char x){ return x - 'a' + 1;}
int main()
{
    int n,k, arr[100], cnt = 0, pre = -1, ans = 0;
    scanf("%d%d",&n,&k);
    char str[100];
    scanf("%s",str + 1);
    for(int i = 1;i <= n; ++i) arr[i] = get(str[i]);
    sort(arr + 1, arr + 1 + n);
    for(int i = 1;i <= n ; ++i)
        if(arr[i] - pre > 1)
        {
            ans += arr[i];
            pre = arr[i];
            ++cnt;
            if(cnt == k) break;
        }
    if(cnt == k) printf("%d",ans);
    else printf("-1");
    return 0;
}
``