We need to solve a problem: given a string s of length n, we want to obtain a set S of distinct subsequences of s, with size exactly k (or at least? The statement: "find out the minimum possible total cost to obtain a set S of size k" – we need to obtain exactly k distinct subsequences, and we can add them one by one paying cost = n - |t| for each added subsequence t, provided t is not already in S (so we only pay when we add a new distinct subsequence). The total cost is sum of costs of the moves (i.e., sum of (n - |t|) for each distinct subsequence we add). We want the minimum total cost to have a set of size k. We can choose any order and any subsequences as long as they are distinct. The cost for each subsequence is based on its length, independent of order. So essentially, we need to choose a set of k distinct subsequences of s, and the total cost is sum_{t in set} (n - |t|). Minimizing total cost is equivalent to maximizing sum of lengths of chosen subsequences, since total cost = kn - sum(lengths). Since kn is constant for fixed k, minimizing total cost is the same as maximizing total sum of lengths of selected distinct subsequences.
But we need to consider that we can only add subsequences that exist (they are subsequences of s). So we need to select k distinct subsequences from the set of all subsequences of s (including empty?) The empty subsequence is a subsequence? Yes, it says: for string "abaca" following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). So empty string is a subsequence. So the set of all subsequences includes the empty string. That subsequence length 0, cost n. But we want to minimize total cost, so we would prefer longer subsequences because they have lower cost. However, the total number of distinct subsequences of s might be limited. If the total number of distinct subsequences is less than k, output -1.
Otherwise, we need to choose k distinct subsequences to maximize sum of lengths. That's like we want the k longest distinct subsequences? But careful: there could be many subsequences of various lengths. For maximizing sum of lengths given we must pick distinct subsequences, we would naturally pick the longest ones first. But are there constraints? The set of subsequences is not independent; the longest ones are the string itself (length n), then maybe multiple subsequences of length n-1, etc. However, we have to consider that distinct subsequences are based on content, not just length. For a given length L, there may be multiple distinct subsequences of that length, but the number might be limited. So to maximize total sum of lengths, we want to include as many high-length subsequences as possible, moving to lower lengths when we run out.
Thus the problem reduces to: For each possible length L (0 to n), count the maximum number of distinct subsequences of s of length exactly L that we can include? Actually, we need to know the total number of distinct subsequences of each length that exist. Then we can greedily take the longest ones first, because longer length gives higher contribution (lower cost). Since we are maximizing sum of lengths, we want to pick the highest-length subsequences first.
But is there any dependency that picking some subsequence of length L might prevent picking some other subsequence? No, because subsequences are just strings; as long as they are distinct, we can pick any set. So the maximum sum of lengths for selecting k distinct subsequences is achieved by taking the k longest possible distinct subsequences (by length), i.e., we sort all distinct subsequences by length descending, take top k. However, if there are multiple subsequences of the same length, the order among them doesn't matter because they contribute equally. So the total sum is sum over lengths of (number chosen of that length) * length.
Thus we need to know, for each length L, the total number of distinct subsequences of length L. Let's denote cnt[L] = number of distinct subsequences of s of length L. Then the total distinct subsequences total = sum_{L=0}^n cnt[L]. If total < k, answer -1.
Otherwise, we need to select k subsequences to maximize total length = sum_{L} min(cnt[L], take[L]) * L, where take[L] is the number we take from length L, such that sum take[L] = k and 0 ≤ take[L] ≤ cnt[L]. To maximize total length, we take as many as possible from larger L, i.e., we start from L=n down to 0: take from length L as many as we can, up to cnt[L], until we have k total. So the greedy is optimal.
Therefore answer = kn - (sum of lengths of selected subsequences). So answer = kn - max_total_length.
So the problem reduces to computing cnt[L] for each L (0..n). However n ≤ 100 in the easy version? The problem statement: "The only difference between the easy and the hard versions is constraints." In the current problem, n ≤ 100 (easy version). But the note says constraints: 1 ≤ n ≤ 100, 1 ≤ k ≤ 10^12. So easy version. In hard version, n is up to 200? Actually typical Codeforces problem "1185 D2? Actually maybe it's "Subsequences (hard version)" with n up to 200? But here we have n ≤ 100, so it's the easy version. So we can compute cnt[L] using DP perhaps.
But careful: Counting distinct subsequences of a given length is a classic problem. Usually the total number of distinct subsequences (including empty) of a string can be computed using DP with last occurrence to avoid duplicates. For length-specific counts, we can use DP that counts number of distinct subsequences of each length.
We can define DP[i][j] = number of distinct subsequences of length j that can be formed from first i characters (prefix s[0..i-1]), considering distinctness. But we need to avoid counting duplicates. Standard technique: When adding a character, we can compute DP[i][j] = DP[i-1][j] + DP[i-1][j-1] - DP[last[s[i-1]]-1][j-1]? Something like that. Actually, for total distinct subsequences (all lengths), the recurrence: dp[i] = 2*dp[i-1] - dp[last[c]-1] (where dp[i] counts all distinct subsequences from prefix i). For length-specific, we can have dp[i][l] = dp[i-1][l] + dp[i-1][l-1] - dp[last[c]-1][l-1] (if last[c] exists). The idea: when processing s[i], we consider adding this character to all subsequences of length l-1 from the previous prefix to form new subsequences of length l. However, some of these may be duplicates of subsequences already counted. The duplicates occur when the same subsequence could have been formed by a previous occurrence of the same character. So we subtract those that would be formed by using this character but which were already counted from an earlier occurrence. More precisely, for distinct subsequences (not necessarily length-specific), the standard recurrence:
Let dp[i] be the number of distinct subsequences (including empty) using first i characters. Then dp[0] = 1 (empty). For i from 1 to n:
dp[i] = dp[i-1] * 2
if last[s[i-1]] exists: dp[i] -= dp[last[s[i-1]] - 1]
Because the new subsequences formed by appending s[i-1] to every subsequence from prefix i-1, but those that were already formed using a previous occurrence of s[i-1] (i.e., by taking the earlier occurrence and the same tail) are duplicates. The duplicates are exactly the subsequences from before the previous occurrence (i.e., prefix last-1) that were extended with this new character, but the same string could have been made by extending that earlier occurrence with the same tail? Actually it's correct.
For length-specific, we can define dp[i][l] as the number of distinct subsequences of length l from first i characters. Then:
dp[0][0] = 1 (empty), dp[0][l>0] = 0.
For i from 1 to n:
dp[i][l] = dp[i-1][l] // not using s[i-1]
+ dp[i-1][l-1] // using s[i-1] (if l>=1)
But we must subtract duplicates: the subsequences of length l that are formed by using s[i-1] and that were already counted due to previous occurrence of the same character. Those duplicates correspond to subsequences of length l that end with s[i-1] and could have been formed earlier. How many? They are exactly dp[prev][l-1] where prev is the index of previous occurrence of s[i-1] (the last index before i where s[prev]==s[i-1])? Actually, the standard for total subsequences subtracts dp[last-1] because when we double, we count all subsequences formed by adding current char to each subsequence from prefix i-1, but the ones that were already present from previous occurrence correspond to those that were formed by adding current char to subsequences from before the previous occurrence? Let's derive.
Suppose we have processed up to index i-1, and we have computed distinct subsequences from prefix i-1. When we add s[i] = c, we want to consider new subsequences: for each distinct subsequence t from prefix i-1, we can form t + c (if we include c). However, some of these may already exist as subsequences from prefix i-1 (without using this c). Which ones? Those subsequences that end with c and were already formed using an earlier occurrence of c. More precisely, if we have a previous occurrence of c at position j (j < i, last occurrence before i), then any subsequence that ended with that earlier c and then some suffix? Actually, think: The duplicate subsequences when using current c are exactly the subsequences that could have been formed by using the previous occurrence of c instead, but were already counted as distinct subsequences when we processed that previous occurrence. When we process at position i, we consider extending all subsequences from prefix i-1 (including those that might have used the previous occurrence or not). The set of new subsequences we attempt to add are all strings of the form X + c where X is any subsequence from first i-1 characters. However, among these, the ones that were already present (i.e., existed as subsequences before adding this c) are those that could be formed without using this c. That is, they are subsequences that end with c but could be formed using earlier c's. In fact, any subsequence that ends with c and was already counted as a subsequence of prefix i-1 must be of the form Y where Y is a subsequence that ends with some earlier c, and Y itself is a subsequence from prefix i-1. But more systematically: The standard recurrence for total distinct subsequences: dp[i] = 2dp[i-1] - dp[last[c]-1] (if last[c] exists, else just 2dp[i-1]). This is because when we double, the duplicates are exactly the subsequences that were already counted and that would be generated again by using the new character; those correspond to subsequences that were counted after the previous occurrence of c but before this occurrence? Wait: I'll recall the standard proof.
Better: Let S(i) be the set of distinct subsequences from prefix i. Then S(i) = S(i-1) ∪ { t + s[i] : t ∈ S(i-1) }. Now, duplicates occur when t + s[i] is already in S(i-1). When can that happen? If t + s[i] is already a subsequence without using s[i], then there must exist some earlier occurrence of s[i] to form that same string. In particular, if we let j be the last index before i where s[j] = s[i], then any subsequence of the form t + s[i] that is already in S(i-1) must have been formed by using the occurrence at j as the last character, and t is a subsequence from prefix j-1 (since after j, we might have other characters but the last is at j). Actually, if we have the same string X, which ends with s[i] (which is c), then X can be formed as Y + c where Y is a subsequence from the part before i (not necessarily before j). But if we used the occurrence at j, we would have Y from prefix j-1 (since we take last occurrence as j). However, there might be other ways. The known recurrence: dp[i] = 2*dp[i-1] - dp[last[c]-1] (if last[c] exists). Here dp[i] counts number of distinct subsequences including empty. This recurrence works.
So for length-specific: dp[i][l] = dp[i-1][l] + (dp[i-1][l-1] - dp[prev-1][l-1])? Actually, consider S(i,l) = set of distinct subsequences of length l from prefix i. Then S(i,l) = S(i-1,l) ∪ { t + s[i] : t ∈ S(i-1, l-1) }. The duplicates are those strings in the second set that are already in S(i-1,l). Those duplicates would be exactly the strings that can be formed as t + s[i] where t ∈ S(i-1, l-1) but t + s[i] ∈ S(i-1,l). But if there is a previous occurrence of s[i] at position j, then any string of the form u + s[i] where u is a subsequence from prefix j-1 will already be in S(j,l) ⊆ S(i-1,l). However, could there be duplicates from u that are not from prefix j-1? Possibly if u uses characters after j but before i? But the last character is s[i], and if we use a later occurrence, the same string could be formed using the earlier occurrence? Actually, suppose we have s = "abac". For i=4 (c='c' not repeated), no duplicate. For repeated characters: s = "aba", consider i=3 (third char is 'a'), we want length 2. Subsequences of length 2 from prefix 2: "ab", "aa"? Actually from first two chars "ab": distinct subsequences: "" , "a", "b", "ab". Length 1: "a","b". Length 2: "ab". Now adding third char 'a', we consider t in S(2,1): "a","b". t + 'a' yields "aa","ba". Are these already in S(2,2)? No, because S(2,2) = {"ab"}. So no duplicate. But in total distinct subsequences, adding third 'a' duplicates some? Let's compute total: S(2) = {"", "a","b","ab"}. Adding 'a' gives new: "a","aa","ba","aba". But "a" already exists. So duplicate "a". That's length 1. So duplicates affect length 1, not length 2. So the duplicates for length l arise from t + s[i] where t is such that t+s[i] was already present. This happens exactly when t is a subsequence that ends after the last occurrence? More systematically, we can derive using DP with last occurrence.
The known DP for counting distinct subsequences of each length (or for counting number of distinct subsequences of length L) uses DP[i][l] = DP[i-1][l] + DP[i-1][l-1] - (if last occurrence exists) DP[last[i]-1][l-1]. This is used in many problems (e.g., Codeforces 1183H? Actually there is a problem "Subsequences (hard version)" maybe). Let's verify with example.
Let s = "aba". n=3. Compute manually distinct subsequences: all distinct strings: "", "a", "b", "ab", "aa", "ba", "aba". lengths: 0:1 (""), 1:2 ("a","b"), 2:3 ("ab","aa","ba"), 3:1 ("aba").
Now use DP:
Initialize dp[0][0] = 1.
i=1, char='a', last[ 'a' ] = none.
dp[1][0] = dp[0][0] = 1 (not using char). For l>=1: dp[1][l] = dp[0][l] + dp[0][l-1] = 0+? For l=1: dp[0][0]=1 so dp[1][1]=1. Others 0.
So after i=1: dp[1][0]=1, dp[1][1]=1.
i=2, char='b', last['b'] none.
dp[2][0]=dp[1][0]=1.
dp[2][1]=dp[1][1] + dp[1][0] = 1 + 1 = 2.
dp[2][2]=dp[1][2] + dp[1][1] = 0 + 1 = 1.
So dp[2][2]=1, which corresponds to "ab".
i=3, char='a', last['a'] = 1 (index 1). So we subtract dp[last-1][l-1] where last=1, so last-1=0.
dp[3][0] = dp[2][0] = 1.
dp[3][1] = dp[2][1] + dp[2][0] - dp[0][0] = 2 + 1 - 1 = 2.
dp[3][2] = dp[2][2] + dp[2][1] - dp[0][1] = 1 + 2 - 0 = 3.
dp[3][3] = dp[2][3] + dp[2][2] - dp[0][2] = 0 + 1 - 0 = 1.
Matches counts: length1:2 ("a","b"? Actually after i=3, we have "a","b" still, duplicates removed; yes 2). length2:3 ("ab","aa","ba"). length3:1 ("aba").
So the recurrence works: dp[i][l] = dp[i-1][l] + dp[i-1][l-1] - (if last[s[i-1]] exists) dp[last-1][l-1] where last is the previous occurrence index (1-indexed? careful with indices). In our example, we used last occurrence at position 1 (1-indexed) and subtracted dp[0][l-1] (since last-1=0). So it's correct.
Thus we can compute dp[i][l] for i=0..n, l=0..n. dp[i][l] can be large? k up to 1e12, but we only need to know cnt[L] for each L, and these counts might exceed k? Actually we need to know the number of distinct subsequences of each length. However, these numbers could be huge (exponential in n). But since k ≤ 1e12, we only care about counts up to k, because when we greedily take, we will cap at k. Specifically, for each length L, we only need min(cnt[L], k) maybe? But we need to compute total sum of lengths. If cnt[L] > k, we might take at most k from that length. Since k is up to 1e12, but n is only 100, cnt[L] could be huge (like 2^100). So we must cap counts at something > k to avoid overflow. We can cap at a value > k, say INF = 1e18 or k+something. Because we only need to compare with k. Since k up to 1e12, we can set INF = 1e18 (or k+1). So in DP, we can store counts but cap at INF (something larger than k). Then we can compute cnt[L] = dp[n][L] capped.
Now after we have cnt[L] (capped at INF), we can compute total distinct subsequences = sum cnt[L]. If total < k, answer -1. Else, we greedily take from highest L downwards: take[L] = min(cnt[L], remaining). Then total_length = sum take[L] * L. Answer = k*n - total_length.
But careful: Are we allowed to include the empty subsequence? Yes, it's counted. However, does the problem consider empty subsequence as a valid subsequence we can add to set S? The examples: first example s="asdf", n=4, k=5. All distinct subsequences of "asdf": Since all characters are distinct, number of distinct subsequences = 2^4 = 16 (including empty). They needed set of size 5, and they chose {"asdf", "asd", "adf", "asf", "sdf"} – all of length 3 or 4. No empty. So we can choose any subsequences, including empty if we want. But since we want to minimize cost, we would avoid empty because it costs n. So it's okay not to include empty. So our greedy taking longest first would not include empty unless needed. So empty will be taken only if we run out of longer ones and still need more. In example 2: "aaaaa", n=5, k=6. We need to compute distinct subsequences of "aaaaa". All distinct subsequences are just "a"^L for L=0..5, so cnt[L]=1 for each L. Total = 6. So we need all 6 subsequences (including empty). Then greedy take from longest: L=5:1, L=4:1, L=3:1, L=2:1, L=1:1, L=0:1 (since need 6 total). So total length = 5+4+3+2+1+0=15. Then answer = kn - total_length = 65 - 15 = 30 - 15 = 15. Matches example.
Third example: same string, k=7, total distinct =6 <7, so -1.
Fourth example: n=10, k=100, answer given as 233. We'll test later.
So algorithm seems correct.
Now we need to implement DP for n up to 100, k up to 1e12. DP array size (n+1)*(n+1) = 101x101, manageable. For each character, we need last occurrence position. We'll store last[26] as the previous index (1-indexed) where that char appeared. Then when processing i from 1 to n, we compute for l from 0 to i (since subsequence length cannot exceed i). But we can compute up to n. Recurrence:
Initialize dp[0][0] = 1. For i=1..n:
char = s[i-1]
lastIdx = last[char] (the previous index, 0 if none)
for l=0..n:
dp[i][l] = dp[i-1][l] // not use s[i-1]
if l > 0:
dp[i][l] += dp[i-1][l-1]
if lastIdx > 0:
dp[i][l] -= dp[lastIdx-1][l-1] // because last occurrence position (1-indexed) -1 gives index of prefix before that occurrence
// Cap dp[i][l] to INF (like 1e18) to avoid overflow and store as long long.
Then update last[char] = i.
One nuance: The subtraction might cause negative numbers if we capped earlier? But if we cap, we need to ensure we subtract correctly. Actually, dp[lastIdx-1][l-1] might also be capped at INF. However, since we cap, the subtraction could produce something slightly off, but we only care that values > k are treated as >k. Since INF is a big number > k, and all counts that exceed INF are clamped, we need to ensure that after subtraction we still are at least as large as the true value (which might be negative? But true value is non-negative). However, if we clamped dp[i-1][l] and dp[i-1][l-1] to INF, adding them could overflow but we use cap. The subtraction: dp[i-1][l] + dp[i-1][l-1] - dp[lastIdx-1][l-1]. If dp[i-1][l] and dp[i-1][l-1] are capped at INF, the sum could be 2*INF, but we cap after subtraction. However, we want the result to be at least the true count, but if the true count is huge (>> INF), we only need to know it's > k. So we can set INF = k (or k+some). But if we set INF = k, then any count exceeding k we just store k. But careful: when subtracting, we might need to know if the result is still >k. Suppose true count > k, but due to capping we may erroneously reduce below k? For example, dp[i-1][l] = >k, say true 1e15, we cap to k. dp[i-1][l-1] = >k, cap to k. dp[lastIdx-1][l-1] = something (maybe < k). Then dp[i][l] computed as k + k - something = 2k - something > k. So if we cap to k after computation, it's fine: we set it to min(computed, k). But we must ensure we don't do subtraction that could lead to negative if we capped both? Actually, true dp[i][l] = dp[i-1][l] + dp[i-1][l-1] - dp[last-1][l-1]. All terms are non-negative. If we cap dp[i-1][l] and dp[i-1][l-1] to k, then computed may be less than true, but it might even become less than true and less than k. But if true > k, we just need to know it's > k; but if we cap intermediate values, we might incorrectly compute a value less than k even though true > k. That would be a problem if we later rely on the exact count for lengths where count might be large but we need to know if it's at least k. However, we can avoid this by using a large INF that is larger than any possible true value we might need to compare to k. Since n is only 100, the maximum possible distinct subsequences of length L is at most C(100, L) but also limited by alphabet? Actually distinct subsequences counts can be as high as number of possible strings? But due to distinctness constraint, maximum possible distinct subsequences for a given length is limited by the number of combinations of positions, which is C(100, L). That's at most C(100,50) ≈ 1e29, which is huge but still finite. But we only need to compare with k up to 1e12, so we can set INF = 1e18, which is > 1e12 and also > possible true values? Actually C(100,50) is about 1e29, which exceeds 1e18. So if we cap at 1e18, some true values > 1e18 would be capped, but they are > k anyway. However, if we cap intermediate DP values at 1e18, the addition and subtraction could overflow if we use 64-bit? 1e18 + 1e18 = 2e18, still within 2^63-1 ≈ 9e18. So safe. But we need to ensure that we don't produce negative due to subtraction of capped values? Since true values are non-negative, and we cap both dp[i-1][l] and dp[i-1][l-1] at INF, while dp[last-1][l-1] may also be capped at INF. The computed value could be less than true, maybe even negative if we over-subtract due to caps? Let's analyze.
Suppose true: A = dp[i-1][l], B = dp[i-1][l-1], C = dp[last-1][l-1]. We store A' = min(A, INF), B' = min(B, INF), C' = min(C, INF). Then we compute D' = A' + B' - C'. Since A, B, C are non-negative integers. Could D' be negative? Only if C' > A'+B'. But since C ≤ B? Actually, is C necessarily ≤ B? Not always. dp[last-1][l-1] is the number of subsequences of length l-1 from the prefix before last occurrence. dp[i-1][l-1] includes those subsequences plus others. So B ≥ C. True B >= C. So B' >=? If B >= C, but B' and C' are caps; if both B and C are > INF, then B' = INF, C' = INF, so B' >= C'? B' = INF, C' = INF => B' >= C' true. So B' - C' >= 0. Then D' = A' + (B' - C') >= A' >= 0. So non-negative. If B is huge but C small, B' = INF, C' = small, then B' - C' approx INF, D' >= INF. So D' may exceed INF, but we cap D' to INF later. So it's safe.
But there is a subtle point: The recurrence dp[i][l] = dp[i-1][l] + dp[i-1][l-1] - dp[last-1][l-1] works only when last occurrence exists and we subtract the dp value from before that occurrence. However, careful with indexing: In standard total distinct subsequences, we subtract dp[last-1] where last is the previous index (1-indexed). So for length-specific, we use dp[last-1][l-1] where last is the previous index (1-indexed). For example above, last for i=3 was 1, last-1=0. So that works.
Implementation: We'll have an array last[26] initialized to 0. dp[0][0] = 1. For i from 1 to n:
c = s[i-1] - 'a'
prev = last[c] // previous occurrence index (1-indexed)
for l from 0 to n:
dp[i][l] = dp[i-1][l];
if l > 0:
dp[i][l] += dp[i-1][l-1];
if prev > 0:
dp[i][l] -= dp[prev-1][l-1];
// cap to INF
if dp[i][l] > INF: dp[i][l] = INF;
if dp[i][l] < 0: dp[i][l] = 0; // just in case, but shouldn't.
last[c] = i;
After finishing, cnt[l] = dp[n][l] (capped). Then we compute total = sum cnt[l] (capped at INF). Since INF is large, total may exceed k. So we can compute total as sum but cap at >k. Actually we only need to check if total < k. So we can compute sum_total = min(INF, sum(cnt[l])? But sum of cnt[l] could be huge; we can compute running sum but cap at k+1 to avoid overflow beyond 64-bit? Because n=100, each cnt[l] ≤ INF=1e18, sum of 101 values could be up to 1e20, which exceeds 64-bit. But we can early break if sum exceeds k. Since we only need to compare with k (≤1e12), we can compute sum as accumulating but if it exceeds k, we can set a flag that total is > k. So we can compute sum_total = 0, and for l from 0..n, add cnt[l] but if sum_total > k, we can set sum_total = k+1 (or just break after exceeding). Actually we need to know if total >= k. So we can just check if we can achieve k. We'll compute needed takes later.
Simplify: After we have cnt[l], we will perform greedy selection: we have remaining = k. For l from n down to 0:
take = min(cnt[l], remaining)
total_length += take * l
remaining -= take
If after loop remaining > 0, then total distinct subsequences < k, answer -1. Else answer = k*n - total_length.
But we must be careful: cnt[l] are capped at INF (maybe > k). So when we take min(cnt[l], remaining), it's fine. And if remaining > 0 after processing all lengths, that means total distinct subsequences (actual) < k. But due to capping, we might have truncated counts to INF, but if INF is at least k, then if total actual < k, our sum of cnt[l] (capped) will be less than k? Not necessarily; because if some cnt[l] is huge but we capped at INF, the sum of caps might exceed k even if actual total is huge > k, so we will not think it's insufficient. But if actual total < k, that means the actual sum of cnt[l] is less than k. Since we capped at INF which is > actual? Actually, we capped down: if actual value > INF, we set to INF, which is greater than actual? No, INF is a cap, so we set to INF if actual > INF, which is larger than actual? Wait, we set dp[i][l] = min(actual, INF). So if actual is huge > INF, we store INF which is less than actual? Actually min(actual, INF) = INF if actual > INF? Since INF is smaller than actual, min chooses INF. So we are storing a value that is less than or equal to actual. That means our cnt[l] may be smaller than actual when actual > INF. But if actual total is huge, it's certainly > k, so we don't need exact counts, we only need to know that we have enough. However, if actual total is less than k, then all actual counts are less than k (since total < k). In that case, none of the actual counts exceed k, so they won't be capped at INF if INF > k. For example, if INF = 1e18, and actual total < k (≤1e12), then each actual count ≤ 1e12 < INF, so they are stored exactly. So our capped counts equal actual counts. So we can accurately detect insufficient total. If actual total > k, then some counts might be capped, but that's fine because we only need to know that we have enough. So we can use INF = 1e18 or maybe 1e15? Since k up to 1e12, INF can be 1e15 or 1e18 to be safe. But note that dp[i][l] could be as large as C(100,50) ≈ 1e29, which > 1e18. So we cap to 1e18, that's fine because we only need up to k. However, when we compute sum_total (if we wanted), we might overflow adding many 1e18s. But we can avoid computing sum_total. We'll just do the greedy loop: start with remaining = k, and for l from n to 0, take = min(cnt[l], remaining). If after all l, remaining > 0, then insufficient. Because cnt[l] may be capped but if they are capped at INF, and we still have remaining > 0 after taking all (i.e., we exhausted all l), that would imply sum of actual counts < remaining? But if some cnt[l] are capped, they represent that the actual count is at least INF, which is > k. So taking min(cnt[l], remaining) will take remaining fully if remaining <= cnt[l] (since cnt[l] >= remaining). So we would never run out if there is any length with count >= remaining. So if we run out, it means that even after considering all lengths, the sum of our capped cnt[l] is less than k. But because capped cnt[l] are <= actual, sum capped ≤ sum actual. If sum actual < k, then sum capped < k, so we would correctly detect insufficient. If sum actual >= k, then sum capped might be less than k only if we capped many counts down to INF? But INF is huge, so sum capped will be at least as large as the capped values, but since each capped value is at most INF, but if actual values are huge, capped values are INF, so sum capped will be at least INF (which is > k). However, we only have 101 lengths, each INF=1e18, sum capped could be up to 101e18 >> k. So sum capped >= k. So we would not run out. So detection works.
Thus we can use INF = 1e18 (or maybe 2e18 to be safe) and not worry about overflow during addition in greedy? In greedy, we compute total_length = take * l, where take <= k (<=1e12) and l <=100, so product <= 1e14, safe. Also total_length sum over at most 101 lengths, so safe. So fine.
One more nuance: The DP recurrence might produce negative intermediate values if we subtract dp[prev-1][l-1] when dp[i-1][l-1] was capped but dp[prev-1][l-1] might be capped differently? But as argued, B' - C' >= 0 because true B >= C, and capping cannot violate B' >= C'? Is it always true that if B >= C then min(B, INF) >= min(C, INF)? Not necessarily. Example: B=INF+5, C=INF+3. Then B >= C, but min(B, INF) = INF, min(C, INF) = INF, so B' = C' = INF, B' >= C' holds (equal). If B=INF+5, C=5, then B' = INF, C' = 5, B' >= C'. If B=7, C=5, both < INF, B' >= C'. If B=INF+5, C=INF+10? But B >= C implies B cannot be less than C. So C cannot exceed B. So indeed B >= C ensures min(B, INF) >= min(C, INF) because if both are capped, they are equal; if only B is capped, then B' = INF >= C' (since C' <= INF). If none capped, same order. So B' >= C'. So B' - C' >= 0. Good.
Now we need to compute dp[prev-1][l-1] where prev-1 may be less than i-1, but we have those values already computed (since we iterate i forward). So DP table can be a 2D array of size (n+1) x (n+1). Use long long.
Now we need to test the fourth example: n=10, k=100, s="ajihiushda". Let's try to compute manually to verify.
We'll implement and test later.
Edge cases: k can be 1? Then we can just take the longest subsequence, which is the whole string (if non-empty), cost = n - n = 0. So answer 0. That matches greedy: take length n first, cnt[n] is usually 1 (unless multiple same strings? Actually, distinct subsequence of length n is only the whole string itself, so cnt[n] = 1). So we take that, total_length = n, cost = 1*n - n = 0.
If k includes empty subsequence, we may need to take it if not enough longer ones. That's fine.
Now we need to confirm that DP counts the empty subsequence. In DP, dp[i][0] is always 1 for all i? Let's see: dp[0][0]=1. For i>0, dp[i][0] = dp[i-1][0] (since l=0 case doesn't have the second term because l>0 condition). So dp[i][0]=1 for all i. So cnt[0]=1. Good.
Now ensure that the DP correctly counts distinct subsequences for lengths >0, even with duplicate characters. The recurrence is standard. So algorithm is straightforward.
Now about constraints: n=100, k up to 1e12. DP O(n^2) fine.
Now think about if there is any trick: We are minimizing total cost, which is k*n - sum lengths. The greedy of taking longest first maximizes sum lengths. Is it always optimal? Since all subsequences are independent, yes, because longer lengths always give higher contribution to sum lengths and thus lower cost. There is no other trade-off. So fine.
Now we need to output answer as integer. Since total cost can be large, but k*n up to 1e12 * 100 = 1e14, which fits in 64-bit. So use long long.
Now implement.
Pseudo:
const long long INF = 1e18; // or 2e18
int n;
long long k;
string s;
cin >> n >> k >> s;
vector<vector> dp(n+1, vector(n+1, 0));
dp[0][0] = 1;
vector last(26, 0); // last index (1-indexed) of character
for (int i = 1; i <= n; ++i) {
int c = s[i-1] - 'a';
int prev = last[c];
for (int l = 0; l <= n; ++l) {
dp[i][l] = dp[i-1][l];
if (l > 0) {
dp[i][l] += dp[i-1][l-1];
if (prev > 0) {
dp[i][l] -= dp[prev-1][l-1];
}
}
// clamp
if (dp[i][l] > INF) dp[i][l] = INF;
if (dp[i][l] < 0) dp[i][l] = 0; // precaution
}
last[c] = i;
}
vector cnt(n+1);
for (int l = 0; l <= n; ++l) {
cnt[l] = dp[n][l];
}
long long remaining = k;
long long total_len = 0;
for (int l = n; l >= 0; --l) {
long long take = min(cnt[l], remaining);
total_len += take * l;
remaining -= take;
if (remaining == 0) break;
}
if (remaining > 0) {
cout << -1 << endl;
} else {
long long ans = k * n - total_len;
cout << ans << endl;
}
Check example 1: n=4, s="asdf", all distinct chars. Count distinct subsequences of each length: For distinct characters, number of distinct subsequences of length L is C(4, L). So cnt[4]=1, cnt[3]=4, cnt[2]=6, cnt[1]=4, cnt[0]=1. k=5. Greedy: take L=4:1 (len4), remaining=4; L=3: min(4,4)=4 (len3), remaining=0. total_len = 14 + 43 = 4+12=16. ans = 5*4 - 16 = 20-16=4. Correct.
Example 2: n=5, s="aaaaa". All same. Distinct subsequences: only one for each length? Actually because string all 'a', the only distinct subsequences are strings of repeated 'a' of various lengths. So cnt[5]=1, cnt[4]=1, cnt[3]=1, cnt[2]=1, cnt[1]=1, cnt[0]=1. total=6. k=6. Greedy: take L=5:1, L=4:1, L=3:1, L=2:1, L=1:1, L=0:1. total_len = 5+4+3+2+1+0 = 15. ans = 6*5 -15 = 30-15=15. OK.
Example 3: same, k=7, after taking all 6, remaining=1 >0 -> -1. OK.
Now need to test example 4: n=10, k=100, s="ajihiushda". We'll run mental approximate? But we trust code.
But there may be nuance: Is the DP correct for distinct subsequences counting? Let's test with small strings to ensure it matches known results.
Test: s = "aa". n=2. Distinct subsequences: "", "a", "aa"? Also "a"? duplicates? So length0:1, length1:1 (only "a"), length2:1 ("aa"). So cnt[0]=1, cnt[1]=1, cnt[2]=1. Let's DP.
Initialize dp[0][0]=1.
i=1, char='a', prev=0.
l=0: dp[1][0]=dp[0][0]=1.
l=1: dp[1][1]=dp[0][1]+dp[0][0]=0+1=1.
l=2: dp[1][2]=dp[0][2]+dp[0][1]=0+0=0.
last['a']=1.
i=2, char='a', prev=1.
l=0: dp[2][0]=dp[1][0]=1.
l=1: dp[2][1]=dp[1][1] + dp[1][0] - dp[0][0] = 1+1-1=1.
l=2: dp[2][2]=dp[1][2] + dp[1][1] - dp[0][1] = 0+1-0=1.
Matches.
Test: s="abab". n=4. Distinct subsequences? Let's manually compute? We'll trust DP. But we need to be careful about the recurrence for length-specific; it's commonly used.
Now, the DP we wrote subtracts dp[prev-1][l-1] if prev > 0. However, note that when prev > 0, dp[prev-1][l-1] is the count of subsequences of length l-1 from prefix before the previous occurrence. But is that always the correct amount to subtract? In the case of total distinct subsequences, the subtraction uses dp[prev-1] (without length). For length-specific, it's dp[prev-1][l-1]. Yes.
But consider example: s="aba", we already did, works.
Now check with s="abcab". Let's compute manually maybe. But I'm confident.
Thus solution for easy version is done.
But the problem statement says "The only difference between the easy and the hard versions is constraints." So here constraints: n ≤ 100, k ≤ 1e12. That's easy.
Now confirm that our DP is O(n^2) and uses long long.
Now test example 4 using quick mental simulation? Let's try to compute manually with DP to verify answer 233. But we can trust.
But maybe there is a twist: The cost for adding a subsequence t is n - |t|. The total cost we computed is sum(n - |t|) = k*n - sum|t|. But is there any nuance about the process? Could we add subsequences in any order, and the cost is per addition? Since cost depends only on the subsequence length, total cost is sum of costs irrespective of order. So yes.
Now consider if there is any alternative where we could achieve lower total cost by not taking the absolute longest subsequences because of duplicates? But no, because any set of k distinct subsequences, the sum of lengths is at most the sum of the k largest lengths among all distinct subsequences. Since longer lengths are always beneficial (they give lower cost), the optimal is to take the k longest distinct subsequences. So it's a classic "take largest possible" greedy.
Now we need to confirm that our DP correctly counts distinct subsequences. But there is one subtlety: Are we counting the empty subsequence? Yes. And is empty subsequence allowed? In problem, they said "subsequence" includes empty string. So yes. In example 2, they used it (since k=6, they took all 6 distinct subsequences including empty). So correct.
Now about the DP: We need to handle large numbers, but we cap at INF. But we must be careful about overflow when adding or subtracting before capping. In the recurrence, we do dp[i][l] = dp[i-1][l] + dp[i-1][l-1]; then possibly subtract dp[prev-1][l-1]. Since each of these values could be up to INF (1e18), their sum could be up to 2e18, which still fits in signed 64-bit (max about 9e18). INF = 1e18, sum = 2e18 < 9e18, safe. Subtracting gives possibly negative? As argued, it shouldn't become negative, but if due to capping it might? Actually, if dp[i-1][l] and dp[i-1][l-1] are both INF, and dp[prev-1][l-1] is also INF, then dp[i][l] = INF + INF - INF = INF. That's non-negative. If dp[i-1][l] = INF, dp[i-1][l-1] = INF, dp[prev-1][l-1] = some smaller value, sum INF+INF could be 2e18, subtract small, still >0. So fine. Use long long.
Now we need to ensure that dp[prev-1][l-1] is defined for prev-1 >= 0. Since we only subtract when prev > 0, so prev-1 >= 0.
Now about the indices: We use 1-indexed i for string positions. dp[i][l] corresponds to prefix of length i (first i characters). last[c] stores the last index (1-indexed) where character c appeared. So prev = last[c] is between 0 and i-1. Good.
Now one more test: s with all distinct characters, DP should produce binomial coefficients. Let's test: n=4 distinct. DP:
Initialize dp[0][0]=1.
i=1: c new -> dp[1][0]=1, dp[1][1]=1.
i=2: c new -> prev=0: dp[2][0]=1, dp[2][1]=dp[1][1]+dp[1][0] =1+1=2, dp[2][2]=dp[1][2]+dp[1][1]=0+1=1.
i=3: new -> dp[3][0]=1, dp[3][1]=dp[2][1]+dp[2][0]=2+1=3, dp[3][2]=dp[2][2]+dp[2][1]=1+2=3, dp[3][3]=dp[2][3]+dp[2][2]=0+1=1.
i=4: new -> dp[4][0]=1, dp[4][1]=4, dp[4][2]=6, dp[4][3]=4, dp[4][4]=1. matches C(4, L).
Now with repeated characters, it reduces counts.
Thus solution stands.
Now think about whether there is any possibility that the optimal set of k subsequences may not be exactly the set of the k longest distinct subsequences because of some cost structure? For each subsequence, cost = n - length. So each subsequence contributes n to total cost, minus its length. So total cost = kn - sum(lengths). Since kn is fixed, minimizing cost is equivalent to maximizing sum of lengths. So yes.
Now think about if there is any constraint on the process: "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|." So we can add subsequences one by one, paying cost each time. There's no other constraints. So total cost is sum of costs. So it's just as we model.
Now check example 1: They got cost 4. Our greedy yields 4.
Now answer format: print one integer.
Thus implementation straightforward.
Now confirm that k can be up to 1e12, which fits in long long (64-bit). n up to 100, n*k up to 1e14, fits in 64-bit.
Now I'll code in C++.
Potential pitfalls: The DP array size (n+1)*(n+1) = 101x101, fine.
One more nuance: The DP may produce dp[i][l] that is less than 0 due to subtraction of capped values? We argued that due to B' >= C', it's non-negative. But we can still clamp to 0 if negative for safety.
Now I'll test example 4 manually or with code later.
But I'll think: s="ajihiushda", n=10. Let's try to compute counts? Might be many subsequences. The answer given is 233. Let's verify with our approach.
Compute DP manually? Might be tedious but we can approximate to see if answer plausible.
We can quickly implement mental or reasoning: Since string length 10, maximum total distinct subsequences could be up to 2^10=1024 if all chars distinct. But here there are repeated? Let's see: a, j, i, h, i, u, s, h, d, a. So characters: a appears at positions 1 and 10; j once; i at 3 and 5? Actually s[2]? Index from 0: s[0]='a', s[1]='j', s[2]='i', s[3]='h', s[4]='i', s[5]='u', s[6]='s', s[7]='h', s[8]='d', s[9]='a'. So repeats: a twice, i twice, h twice. So distinct subsequences count less than 1024, but still many. The total number of distinct subsequences is less than 1024. We need k=100, which is less than total probably. Let's compute total distinct subsequences.
We can compute total distinct subsequences via DP for total (sum over lengths). The total distinct subsequences (including empty) for string of length 10 with repeats might be something like 600? Not sure. Let's compute quickly.
We'll compute using DP for all lengths, maybe we can compute quickly by hand? Let's try: For each prefix, compute number of distinct subsequences (total). Standard recurrence: total[i] = total[i-1]*2 - total[last(c)-1]. With empty counted. We'll compute total distinct (including empty) and also per length? But we want to verify total >=100. Likely yes.
But we trust answer.
Now compute our greedy: We need to take the 100 longest distinct subsequences. Since total distinct may be >100, we take some of the longest ones.
The longest is length 10: only the full string? Could be multiple? If there are multiple distinct subsequences of length 10? Only the full string itself, because any subsequence of length 10 must be the whole string. So cnt[10]=1.
Length 9: subsequences formed by deleting one character. Since there may be duplicates if deleting different positions yields same string? For example, if string has duplicate characters, deleting different positions might yield same subsequence. For "ajihiushda", let's see if any length-9 subsequences repeat. Deleting each of 10 positions yields 10 subsequences, but some might be identical if the string has identical characters at symmetric positions? For length 9, if there are duplicate characters, possibly some collisions. So cnt[9] ≤ 10. Likely 10 if all 10 deletions yield distinct strings. Let's check: The string is "a j i h i u s h d a". Deleting first 'a' gives "j i h i u s h d a". Deleting last 'a' gives "a j i h i u s h d". Those are different because first char differs. Deleting one of the i's? There are two i's at positions 3 and 5? Actually index starting 1:1:a,2:j,3:i,4:h,5:i,6:u,7:s,8:h,9:d,10:a. Delete position 3 gives "a j h i u s h d a". Delete position 5 gives "a j i h u s h d a". Those are different. Delete h at pos4 vs pos8 similarly. So likely all 10 distinct. So cnt[9] = 10. So we have 1 (len10) + 10 (len9) = 11 longest subsequences.
We need 100, so we must take many of length 8, 7, etc.
We want to compute total length sum for k=100. Answer = kn - total_len = 10010 - total_len = 1000 - total_len = 233, so total_len = 1000 - 233 = 767. So sum of lengths of selected 100 subsequences is 767. Average length ~7.67. So we must be taking many length 8 and 7 subsequences. That seems plausible.
Now we must confirm DP yields such counts.
Thus our solution should output 233.
Now I'll write final solution.
But there may be a nuance: The DP for distinct subsequences counts subsequences as strings, but does it count duplicates correctly when characters are repeated? Yes.
Now about implementation details: Since n is small, we could also compute using another DP: dp[l][c] but we already have method.
Now we also need to think about memory: we can use two rows to save memory (since only need dp[i-1] and dp[prev-1] maybe). But with n=100, we can store full table.
Now I'll craft final answer.
But before finalizing, consider if there is any scenario where our greedy of taking the longest subsequences might not be feasible due to the way we can add subsequences? Because each subsequence is independent, we can add any subsequence at any time. So we can pick any set. So feasible.
One more nuance: The cost is per addition, and if we add a subsequence that is not yet in S, we pay cost. So total cost depends only on the set, not order. So yes.
Now implement.
Potential pitfalls: overflow in dp[i][l] addition before cap: dp[i-1][l] + dp[i-1][l-1] might exceed 2*INF. But we cap after subtraction. However, we can cap intermediate steps: we can compute as:
long long val = dp[i-1][l];
if (l > 0) {
val += dp[i-1][l-1];
if (prev > 0) {
val -= dp[prev-1][l-1];
}
}
if (val > INF) val = INF;
if (val < 0) val = 0;
dp[i][l] = val;
This is fine.
Now we also need to handle case where l > i maybe, but we can loop l=0..n, but dp[i-1][l] will be zero for l > i-1. So it's fine.
Now final code.
Now test with examples.
Example 1:
4 5
asdf
Our code: cnt[4]=1, cnt[3]=4, cnt[2]=6, cnt[1]=4, cnt[0]=1. remaining=5.
l=4: take 1, remaining 4, total_len=4
l=3: take 4, remaining 0, total_len=4+12=16
ans=5*4-16=20-16=4 -> print 4.
Example 2:
5 6
aaaaa
cnt: each length 1. remaining=6.
l=5:1 (rem5,tl=5)
l=4:1 (rem4,tl=9)
l=3:1 (rem3,tl=12)
l=2:1 (rem2,tl=14)
l=1:1 (rem1,tl=15)
l=0:1 (rem0,tl=15)
ans=6*5-15=30-15=15.
Example 3: 5 7 -> remaining after l=0: rem=1 -> -1.
Example 4: we'll trust.
Now edge case: k=1. Then we only need one subsequence. The longest is length n, cnt[n] is usually 1 (unless there are multiple same full strings? but only one full string). So take it, total_len = n, ans = 1*n - n =0. Good.
If k=0? k>=1 per constraints. So ignore.
Now we must consider that the empty subsequence may be taken if needed, cost n. That's okay.
Now complexity O(n^2) with small n, fine.
Now we need to confirm that the DP counts distinct subsequences correctly even when there are many characters? Yes.
One more check: The recurrence for length-specific distinct subsequences we used is valid only if we treat subsequences as strings. However, there is nuance: The subtraction dp[prev-1][l-1] is correct because the duplicates that arise when adding current character correspond exactly to subsequences of length l that end with this character and could have been formed using the previous occurrence. These duplicates are exactly the subsequences formed by taking a subsequence of length l-1 from the prefix before the previous