CF1011A.Stages

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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.

娜塔莎将飞往火星。她需要建造一枚火箭,该火箭由若干级(stage)按特定顺序组成。每一级由一个小写拉丁字母表示。因此,火箭可被描述为一个字符串——即对应各级的字母的拼接。

现有 nn 级可用。火箭必须恰好包含其中 kk 级。火箭中的各级须按其重量升序排列。也就是说,在某个字母所代表的级之后,只能接一个在字母表中至少比它靠后两个位置的字母(即跳过中间至少一个字母,或更多)。例如,在字母 'c' 之后不能接 'a'、'b'、'c' 或 'd',但可以接 'e'、'f'、…、'z'。

为使火箭飞行距离尽可能远,其总重量应最小。火箭的重量等于其所有级的重量之和。每级的重量为其在字母表中的序号:例如,'a' 重 1 吨,'b' 重 2 吨,'z' 重 2626 吨。

请构造出总重量最小的火箭;若无法构造出满足条件的火箭,则判定其不可能。每级最多只能使用一次。

输入格式

The first line of input contains two integers — nn and kk (1≤k≤n≤501 \le k \le n \le 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.

输入的第一行包含两个整数 — nn 和 kk(1≤k≤n≤501 \le k \le n \le 50),分别表示可用的火箭级数和火箭中实际使用的级数。

第二行包含一个字符串 ss,该字符串恰好由 nn 个小写拉丁字母组成。每个字母代表一种新的火箭级,可用于构建火箭。每种火箭级最多只能使用一次。

输出格式

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

输出一个整数——火箭的最小总重量;如果根本无法建造火箭,则输出 −1-1。

输入输出样例

  • 输入#1

    5 3
    xyabd

    输出#1

    29
  • 输入#2

    7 4
    problem

    输出#2

    34
  • 输入#3

    2 2
    ab

    输出#3

    -1
  • 输入#4

    12 1
    abaabbaaabbb

    输出#4

    1

说明/提示

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.

在第一个例子中,满足条件的火箭如下:

  • “adx”(重量为 1+4+24=291+4+24=29);
  • “ady”(重量为 1+4+25=301+4+25=30);
  • “bdx”(重量为 2+4+24=302+4+24=30);
  • “bdy”(重量为 2+4+25=312+4+25=31)。

火箭 “adx” 的重量最小,因此答案为 2929。

在第二个例子中,目标火箭为 “belo”,其重量为 2+5+12+15=342+5+12+15=34。

在第三个例子中,n=k=2n=k=2,因此火箭必须包含两个级段:“a” 和 “b”。该火箭不满足条件,因为这两个字母在字母表中相邻。答案为 −1-1。

输入解题思路,AI测评打分。不知道怎么写?

首页