The only difference between the easy and the hard versions is constraints.
A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa".
You are given a string s consisting of n lowercase Latin letters.
In one move you can take any subsequence t of the given string and add it to the set S. The set S can't contain duplicates. This move costs n−|t|, where |t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters).
Your task is to find out the minimum possible total cost to obtain a set S of size k or report that it is impossible to do so.
Input
The first line of the input contains two integers n and k (1≤n,k≤100) — the length of the string and the size of the set, correspondingly.
The second line of the input contains a string s consisting of n lowercase Latin letters.
Output
Print one integer — if it is impossible to obtain the set S of size k, print -1. Otherwise, print the minimum possible total cost to do it.
Examples
Input
4 5
asdf
Output
4
Input
5 6
aaaaa
Output
15
Input
5 7
aaaaa
Output
-1
Input
10 100
ajihiushda
Output
233
Note
In the first example we can generate S = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in S is 0 and the cost of the others is 1. So the total cost of S is 4.

特别感谢QY大佬讲解枚举子串方法!%%%
题意:给一个长度为n的字符串,将n的子串(空字符串也算一种子串)放入S中,S要求其中的字符串各不相同,每放入1个字符串,代价即为n-|子串长度|,问放入k个子串所需的最小代价为多少,如果没有k个子串,则输出-1.
考虑到k比较小,直接从最长的子串开始枚举并放入S中,同时计算代价并删去相同的子串防止重复统计。
排除的相同子串上限为(100+1)*100/2(远达不到),很小;k也很小,故可以通过。

#include<bits/stdc++.h>
using namespace std;
map<string,int> m;
string s,s2;
queue<string> q;
long long ans;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int n,k;
    cin>>n>>k;
    cin>>s;
    q.push(s);
    --k;
    while(k && !q.empty())
    {
        s=q.front();
        q.pop();
        for(int i=0;k&&i<s.length();++i)
        {
            s2=s;
            s2.erase(i,1);
            if(m.count(s2)) continue;
            else 
            {
                --k;
                m[s2]=1;
                q.push(s2);
                ans+=n-s2.length();
            }
        }
    }
    if(k>0) cout<<-1<<endl;
    else cout<<ans<<endl;
    //s.replace(p,1,s2); 
    return 0;
}