文章
13
粉丝
120
获赞
1
访问
6.4k
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int cmp(const void *a, const void *b)
{
return *(char *)a - *(char *)b;
}
int main()
{
int n, k;
while (scanf("%d %d", &n, &k) != EOF)
{
char str[50];
scanf("%s", str);
int len = strlen(str);
qsort(str, len, sizeof(str[0]), cmp);
char current = str[0];
int res = current - 'a' + 1, i, j;
for (i = 1, j = 1; i < len && j < k; i++)
{
if (str[i] - current >= 2)
{
res += str[i] - 'a' + 1;
j++;
current = str[i];
}
}
if (j == k)
printf("%d\n", res);
else
printf("-1\n");
}
}
登录后发布评论
暂无评论,来抢沙发