A11910 | Stages
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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.
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.
输入格式
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.
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.
输出格式
Print a single integer — the minimal total weight of the rocket or -1, if it is impossible to build the rocket at all.
输入输出样例
输入 #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=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.
- "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.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted