Stages CodeForces - 1011A 解题报告

版权声明:大鹏专属 https://blog.csdn.net/NCC__dapeng/article/details/88066840

Natasha is going to fly to Mars. She needs to build a rocket, which consists of several stages in some order. Each of the stages is defined by a lowercase Latin letter. This way, the rocket can be described by the string — concatenation of letters, which correspond to the stages.

There are nn stages available. The rocket must contain exactly kk of them. Stages in the rocket should be ordered by their weight. So, after the stage with some letter can go only stage with a letter, which is at least two positions after in the alphabet (skipping one letter in between, or even more). For example, after letter 'c' can't go letters 'a', 'b', 'c' and 'd', but can go letters 'e', 'f', ..., 'z'.

For the rocket to fly as far as possible, its weight should be minimal. The weight of the rocket is equal to the sum of the weights of its stages. The weight of the stage is the number of its letter in the alphabet. For example, the stage 'a 'weighs one ton,' b 'weighs two tons, and' z' — 2626 tons.

Build the rocket with the minimal weight or determine, that it is impossible to build a rocket at all. Each stage can be used at most once.

Input

The first line of input contains two integers — nn and kk (1≤k≤n≤501≤k≤n≤50) – the number of available stages and the number of stages to use in the rocket.

The second line contains string ss, which consists of exactly nn lowercase Latin letters. Each letter defines a new stage, which can be used to build the rocket. Each stage can be used at most once.

Output

Print a single integer — the minimal total weight of the rocket or -1, if it is impossible to build the rocket at all.

Examples

Input

5 3
xyabd

Output

29

Input

7 4
problem

Output

34

Input

2 2
ab

Output

-1

Input

12 1
abaabbaaabbb

Output

1

Note

In the first example, the following rockets satisfy the condition:

  • "adx" (weight is 1+4+24=291+4+24=29);
  • "ady" (weight is 1+4+25=301+4+25=30);
  • "bdx" (weight is 2+4+24=302+4+24=30);
  • "bdy" (weight is 2+4+25=312+4+25=31).

Rocket "adx" has the minimal weight, so the answer is 2929.

In the second example, target rocket is "belo". Its weight is 2+5+12+15=342+5+12+15=34.

In the third example, n=k=2n=k=2, so the rocket must have both stages: 'a' and 'b'. This rocket doesn't satisfy the condition, because these letters are adjacent in the alphabet. Answer is -1.

题目大意: 就是给你一串字母,让你从里面挑K个,相邻两个的字母相差两个距离或者更多。

思路:这学期第一场比赛的第一道题狠狠地给我了一次教训,很简单的贪心,排序然后贪心就行,然后卡了一个小时还反复提交了好几次,心态崩了,赛后反思了一下,还是假期偷懒了,思维考虑的不全面并且很多细节都忽略了,以后的比赛我会好好注意,继续加油。

下面给出代码,其中会详细的介绍我死在了哪里:

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int maxn=1000;
const int INF=0x3f3f3f3f;
const ll mod=1e9+7;

int main()
{
//  ios::sync_with_stdio(false);
//  freopen("out.txt","w",stdout);
//  freopen("in.txt","r",stdin);

    int n,k;
    char s[maxn],t[maxn];
    scanf("%d %d",&n,&k);
    scanf("%s",s);
    sort(s,s+n);

    int ans=s[0]-'a'+1,flag=0,pos=0;
    t[pos]=s[0];
    k--;
    if(k==0)//K等于零没有特殊考虑,让其进入了下面的FOR循环
    {
        printf("%d",ans);//一开始没有及时终止导致了多种输出;
    }
    else
    {
        for(int i=1; i<n; i++)
        {
            if(s[i]-t[pos]>=2)//太天真,并没有是用另一个数组一开始直接写的s[i]-s[i-1]考虑问题十分的不全面
            {
                ans+=s[i]-'a'+1;
                k--;
                pos++;
                t[pos]=s[i];
                if(k==0)//刚想出思路来太得意忘形,忽略了-1情况的讨论。
                {
                    flag=1;
                    break;
                }
            }
        }
        if(!flag)
            printf("-1");
        else
            printf("%d",ans);
    }
    return 0;

}
//希望各位不要像我一样忽略细节,因为他们真的很重要,也不要得意忘形!!!共勉!!!

猜你喜欢

转载自blog.csdn.net/NCC__dapeng/article/details/88066840