Subsequences (hard version)
Contest: Codeforces Round 570 (Div. 3)
Difficulty rating: 1900
Tags: dp, strings

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 format:
The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100, 1 \le k \le 10^{12}$$$) — 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 format:
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.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

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$$$.

Example input:
4 5
asdf

Example output:
4

Example input:
5 6
aaaaa

Example output:
15

Editorial:
Firstly, let's calculate the following auxiliary matrix: $$$lst_{i, j}$$$ means the maximum position $$$pos$$$ that is less than or equal to $$$i$$$, and the character $$$s_{pos} = j$$$ (in order from $$$0$$$ to $$$25$$$, 'a' = $$$0$$$, 'b' = $$$1$$$, and so on). It can be calculated naively or with some easy dynamic programming (initially all $$$lst_{i, j}$$$ are $$$-1$$$ and then for each $$$i$$$ from $$$1$$$ to $$$n-1$$$ all values $$$lst_{i, j}$$$ are equal to $$$lst_{i - 1, j}$$$ except $$$lst_{i, s_i}$$$ which is $$$i$$$).
After calculating this matrix we can solve the problem by the following dynamic programming: let $$$dp_{i, j}$$$ be the number of subsequences of length $$$j$$$ that ends exactly in the position $$$i$$$. Initially all values are zeros except $$$dp_{i, 1} = 1$$$ for each $$$i$$$ from $$$0$$$ to $$$n-1$$$.
How do we perform transitionss? Let's iterate over all lengths $$$j$$$ from $$$2$$$ to $$$n$$$, then let's iterate over all positions $$$i$$$ from $$$1$$$ to $$$n-1$$$ in a nested loop, and for the current state $$$dp_{i, j}$$$ we can calculate it as $$$\sum\limits_{c=0}^{25}dp_{lst_{i - 1, c}},{j - 1}$$$. If $$$lst_{i - 1, c} = -1$$$ then we don't need to add this state of the dynamic programming to the current state. Don't forget to take the minimum with $$$10^{12}$$$ after each transition!
This transition means that we take all subsequences that end with each possible character of the alphabet and try to add the current character to each of them. You can understand that there are no overlapping subsequences in this dynamic programming.
After that let's iterate over all possible lengths $$$j$$$ from $$$n$$$ to $$$1$$$ and calculate the number of subsequences of the current length $$$j$$$. It equals to $$$cnt = \sum\limits_{c=0}^{25}dp_{lst_{n - 1, c}, j}$$$. The same, if $$$lst_{n - 1, c} = -1$$$ then we don't need to add this state of the dynamic programming to $$$cnt$$$. Don't forget to take the minimum with $$$10^{12}$$$! If $$$cnt \ge k$$$ then let's add $$$k(n - len)$$$ to the answer and break the cycle. Otherwise let's add $$$cnt(n - len)$$$ to the answer and decrease $$$k$$$ by $$$cnt$$$.
If after all iterations $$$k$$$ is greater than zero then let's try to add the empty string to the answer (we didn't take it into account earlier). Increase the answer by $$$n$$$ and decrease $$$k$$$ by one. If after this $$$k$$$ is still greater than zero then the answer is -1, otherwise the answer is the calculated sum.
Time complexity: $$$O(n^2)$$$.