A. Stages

Codeforces
IDCF1011A
Time1000ms
Memory256MB
Difficulty
greedyimplementationsortings
English · Original
Chinese · Translation
Formal · Original
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 \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 $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. [samples] ## 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_.
娜塔莎将前往火星。她需要建造一枚火箭,火箭由若干个阶段按某种顺序组成。每个阶段由一个小写拉丁字母定义,因此火箭可以用字符串表示——即这些字母的拼接。 共有 $n$ 个可用阶段。火箭必须恰好包含其中 $k$ 个。火箭中的阶段必须按重量排序。因此,在某个字母阶段之后,只能跟一个字母,该字母在字母表中至少比它靠后两个位置(中间跳过一个或更多字母)。例如,字母 '_c_' 之后不能跟 '_a_'、'_b_'、'_c_' 或 '_d_',但可以跟 '_e_'、'_f_'、...、'_z_'。 为了让火箭飞得尽可能远,其总重量应尽可能小。火箭的重量等于其所有阶段的重量之和。每个阶段的重量等于其对应字母在字母表中的序号。例如,'_a_' 重 1 吨,'_b_' 重 2 吨,'_z_' 重 $26$ 吨。 请构造出重量最小的火箭,或判断完全无法建造火箭。每个阶段最多只能使用一次。 输入的第一行包含两个整数 $n$ 和 $k$($1 lt.eq k lt.eq n lt.eq 50$)——即可用阶段的数量和火箭中需使用的阶段数量。 第二行包含字符串 $s$,由恰好 $n$ 个小写拉丁字母组成。每个字母代表一个可用的阶段,每个阶段最多只能使用一次。 请输出一个整数——火箭的最小总重量,如果无法建造火箭,则输出 _-1_。 在第一个例子中,以下火箭满足条件: 火箭 "_adx_" 的重量最小,因此答案为 $29$。 在第二个例子中,目标火箭是 "_belo_",其重量为 $2 + 5 + 12 + 15 = 34$。 在第三个例子中,$n = k = 2$,因此火箭必须包含两个阶段:'_a_' 和 '_b_'。但这组字母在字母表中相邻,不满足条件,答案为 _-1_。 ## Input 输入的第一行包含两个整数 $n$ 和 $k$($1 lt.eq k lt.eq n lt.eq 50$)——即可用阶段的数量和火箭中需使用的阶段数量。第二行包含字符串 $s$,由恰好 $n$ 个小写拉丁字母组成。每个字母代表一个可用的阶段,每个阶段最多只能使用一次。 ## Output 请输出一个整数——火箭的最小总重量,如果无法建造火箭,则输出 _-1_。 [samples] ## Note 在第一个例子中,以下火箭满足条件:"_adx_"(重量为 $1 + 4 + 24 = 29$);"_ady_"(重量为 $1 + 4 + 25 = 30$);"_bdx_"(重量为 $2 + 4 + 24 = 30$);"_bdy_"(重量为 $2 + 4 + 25 = 31$)。火箭 "_adx_" 的重量最小,因此答案为 $29$。 在第二个例子中,目标火箭是 "_belo_",其重量为 $2 + 5 + 12 + 15 = 34$。 在第三个例子中,$n = k = 2$,因此火箭必须包含两个阶段:'_a_' 和 '_b_'。但这组字母在字母表中相邻,不满足条件,答案为 _-1_。
**Definitions** Let $ n, k \in \mathbb{Z} $ with $ 1 \leq k \leq n \leq 50 $. Let $ S $ be a multiset of $ n $ lowercase Latin letters (string of length $ n $). For a letter $ c $, define its weight as $ w(c) = \text{ord}(c) - \text{ord}('a') + 1 $, so $ w(a) = 1, w(b) = 2, \dots, w(z) = 26 $. Let $ L = \{ c \in S \} $ be the set of distinct letters in $ S $, and let $ A = \{ w(c) \mid c \in S \} $ be the multiset of their weights. **Constraints** 1. The rocket must consist of exactly $ k $ distinct stages (each stage used at most once). 2. Let the selected stages, ordered by increasing weight, be $ c_1, c_2, \dots, c_k $ with corresponding weights $ w_1 < w_2 < \dots < w_k $. 3. For all $ i \in \{1, 2, \dots, k-1\} $, it must hold that: $$ w_{i+1} \geq w_i + 2 $$ (i.e., consecutive selected letters must be at least two positions apart in the alphabet). **Objective** Find the minimum possible total weight: $$ \min \sum_{i=1}^k w_i $$ over all subsets of $ k $ distinct letters from $ S $ satisfying the spacing constraint. If no such subset exists, output $ -1 $.
Samples
Input #1
5 3
xyabd
Output #1
29
Input #2
7 4
problem
Output #2
34
Input #3
2 2
ab
Output #3
\-1
Input #4
12 1
abaabbaaabbb
Output #4
1
API Response (JSON)
{
  "problem": {
    "name": "A. Stages",
    "description": {
      "content": "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 des",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF1011A"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "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 des...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "娜塔莎将前往火星。她需要建造一枚火箭,火箭由若干个阶段按某种顺序组成。每个阶段由一个小写拉丁字母定义,因此火箭可以用字符串表示——即这些字母的拼接。\n\n共有 $n$ 个可用阶段。火箭必须恰好包含其中 $k$ 个。火箭中的阶段必须按重量排序。因此,在某个字母阶段之后,只能跟一个字母,该字母在字母表中至少比它靠后两个位置(中间跳过一个或更多字母)。例如,字母 '_c_' 之后不能跟 '_a_'、'_b...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n, k \\in \\mathbb{Z} $ with $ 1 \\leq k \\leq n \\leq 50 $.  \nLet $ S $ be a multiset of $ n $ lowercase Latin letters (string of length $ n $).  \nFor a letter $ c $, define its we...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments