CodeForces - 1011A A. Stages 水题一枚

A. Stages

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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

Copy

5 3
xyabd

output

Copy

29

input

Copy

7 4
problem

output

Copy

34

input

Copy

2 2
ab

output

Copy

-1

input

Copy

12 1
abaabbaaabbb

output

Copy

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.

题意:

给你n,m,n个字母,求m个最小字母和,不能选连续的字母。

水题,直接上代码

#include<cstdio>  
#include<cstring>  
#include<cstdlib>  
#include<cctype>  
#include<cmath>  
#include<iostream>  
#include<sstream>  
#include<iterator>  
#include<algorithm>  
#include<string>  
#include<vector>  
#include<set>  
#include<map>  
#include<stack>  
#include<deque>  
#include<queue>  
#include<list>  
using namespace std;  
const double eps = 1e-8;  
typedef long long LL;  
typedef unsigned long long ULL;  
const int INT_INF = 0x3f3f3f3f;  
const int INT_M_INF = 0x7f7f7f7f;  
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;  
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;  
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};  
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};  
const int MOD = 1e9 + 7;  
const double pi = acos(-1.0);  
const int MAXN = 1e5 + 10;  
const int MAXT = 10000 + 10;  
#define N 100005
int n,m;
char s[1050];
int main()
{
	int n,m;
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		cin>>s[i];
	}
	sort(s+1,s+n+1);
	int minn=s[1]-'a'+1,k=1;
	char t=s[1];
	for(int i=2;i<=n;i++)
	{
		if(k==m)break;   //写在下面,就错了。
		if(s[i]-t>1)
		{
			t=s[i];
			k++;
			minn+=s[i]-'a'+1;
		}
		
	}
	if(k!=m)cout<<-1<<endl;
	else cout<<minn<<endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/sdz20172133/article/details/81711152