A - Stages

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 n stages available. The rocket must contain exactly k 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’ — 26 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 — n and k (1≤k≤n≤50) – the number of available stages and the number of stages to use in the rocket.

The second line contains string s, which consists of exactly n 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=29);
“ady” (weight is 1+4+25=30);
“bdx” (weight is 2+4+24=30);
“bdy” (weight is 2+4+25=31).
Rocket “adx” has the minimal weight, so the answer is 29.

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

In the third example, n=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.

这道题目自己错这么多次是因为做题思想有问题。比如
5 3
abcde
这个自己原来的代码就过不去。

#include
#include
#include

using namespace std;

int main()
{
    int m, n;
    while(~scanf ("%d %d", &m, &n))
    {
        char str[m];
        cin >> str; 
        int a[m];
        for (int i = 0; i < m; i++)
            a[i] = str[i] - 'a' + 1;
        sort (a, a+m);
        int k = 1, sum = a[0];
        int rec = 0;
        for(int i=1;iif(k>=n) break;
            rec += a[i] - a[i-1];
// 这个很厉害比如:
//abcde
//a[] = 1,2,3,4,5
//rec = 1;
//rec = 2;
// sum = 4; 
            if(rec > 1)
            {
                sum += a[i];
                rec = 0;
                k ++;
            }
        }
         if(k < n) cout << "-1" << endl;
         else cout << sum << endl;
    }
    return 0;
}

你可能感兴趣的:(二分,+,尺取)