text
stringlengths 594
23.8k
| conversation_id
int64 97
109k
| embedding
sequence | cluster
int64 0
0
|
|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
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 ≤ 100, 1 ≤ k ≤ 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
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.
Tags: dp, strings
Correct Solution:
```
n, k = map(int, input().split())
s = input()
ns = len(s)
pos = [0 for i in range(26)]
dp = [[0 for j in range(ns + 1)] for i in range(ns + 1)]
dp[0][0] = 1
for i in range(1, ns + 1):
v = ord(s[i - 1]) - ord('a')
for j in range(0, i + 1):
if j > 0:
dp[i][j] += dp[i - 1][j - 1]
dp[i][j] += dp[i - 1][j]
if pos[v] >= 1 and j >= (i - pos[v]):
dp[i][j] -= dp[pos[v] - 1][j - (i - pos[v])]
pos[v] = i
# print(dp[ns])
res = 0
for j in range(0, ns + 1):
if k >= dp[ns][j]:
res += dp[ns][j] * j
k -= dp[ns][j]
else:
res += k * j
k = 0
break
print(res if k == 0 else -1)
```
| 97
|
[
-0.05120849609375,
0.27587890625,
0.2734375,
0.371337890625,
-0.06317138671875,
-0.1590576171875,
-0.1883544921875,
-0.1881103515625,
0.467529296875,
0.73974609375,
0.712890625,
-0.29345703125,
-0.26806640625,
-0.95263671875,
-0.5869140625,
0.1370849609375,
-0.65234375,
-0.80859375,
-0.42431640625,
-0.251953125,
0.09039306640625,
-0.1343994140625,
-1.35546875,
-0.447265625,
0.2366943359375,
1.1220703125,
0.2095947265625,
0.242431640625,
1.09375,
1.2998046875,
-0.2283935546875,
-0.8681640625,
0.5048828125,
-1.091796875,
-0.4921875,
-0.1614990234375,
0.3349609375,
-0.56982421875,
-0.486083984375,
-0.088623046875,
0.400390625,
-0.68994140625,
0.6279296875,
-0.4775390625,
-1.212890625,
-0.15966796875,
-0.10748291015625,
-0.402587890625,
-0.5009765625,
-0.111083984375,
0.08184814453125,
-0.06683349609375,
0.50830078125,
0.06793212890625,
0.0711669921875,
0.32275390625,
0.2330322265625,
-0.3115234375,
-0.74560546875,
0.5927734375,
0.22998046875,
-0.119873046875,
0.125732421875,
-0.578125,
-0.52294921875,
0.2044677734375,
-0.3955078125,
0.1322021484375,
0.044921875,
-0.72119140625,
-0.032501220703125,
0.06494140625,
-0.033843994140625,
-0.61328125,
-0.237548828125,
-0.439453125,
0.1610107421875,
-0.2646484375,
-0.55029296875,
0.658203125,
0.052459716796875,
0.92529296875,
0.345458984375,
-0.0211029052734375,
-0.6953125,
-0.6181640625,
-0.050933837890625,
0.09881591796875,
0.368896484375,
0.08502197265625,
-0.544921875,
1.001953125,
-0.71923828125,
-0.05816650390625,
0.3994140625,
0.615234375,
-0.5810546875,
0.1981201171875,
0.30419921875,
0.090576171875,
0.472412109375,
0.740234375,
-0.484375,
1.1767578125,
-0.062469482421875,
-0.2291259765625,
0.255859375,
-0.38427734375,
-0.255859375,
-0.288330078125,
-0.2005615234375,
-0.464111328125,
0.4111328125,
-0.123046875,
-0.48095703125,
1.015625,
-0.1063232421875,
0.5615234375,
-0.6201171875,
0.43359375,
0.5361328125,
0.07989501953125,
0.63134765625,
-0.1341552734375,
-0.13232421875,
-0.3681640625,
-0.53173828125,
1.126953125,
-0.6162109375,
0.37939453125,
0.62939453125,
-0.231201171875,
-0.2548828125,
0.3359375,
-0.2021484375,
0.70703125,
-0.2027587890625,
0.5517578125,
0.61474609375,
-0.5390625,
0.73486328125,
-0.2069091796875,
-0.059814453125,
1.552734375,
0.60107421875,
0.2474365234375,
1.0458984375,
0.3505859375,
-0.56982421875,
0.444091796875,
-1.041015625,
0.74267578125,
0.42333984375,
0.12359619140625,
-0.302001953125,
-0.08660888671875,
-0.0733642578125,
0.448486328125,
0.023193359375,
0.486572265625,
-0.33984375,
0.328857421875,
-0.282470703125,
0.390380859375,
-0.599609375,
0.88330078125,
-0.1895751953125,
-0.29541015625,
0.04205322265625,
-0.73193359375,
0.42626953125,
-0.154052734375,
-0.1405029296875,
0.705078125,
0.371337890625,
0.986328125,
1.107421875,
-0.03387451171875,
0.51513671875,
0.677734375,
-0.322998046875,
0.258544921875,
0.371337890625,
0.51904296875,
0.89794921875,
0.1796875,
0.147705078125,
-0.040924072265625,
-0.73046875,
-0.70361328125,
0.240966796875,
0.69677734375,
-0.255859375,
0.237060546875,
0.0736083984375,
0.053131103515625,
-1.158203125,
-0.2476806640625,
-0.1666259765625,
-0.9052734375,
-0.6572265625,
0.5498046875,
0.0914306640625,
0.2108154296875,
-0.57861328125,
-0.8212890625,
0.66845703125,
1.1162109375,
-0.459716796875,
-0.0162506103515625,
0.72119140625,
0.10369873046875,
-0.40673828125,
0.471923828125,
-0.10272216796875,
-0.406494140625,
-0.67724609375,
0.6611328125,
-0.324462890625,
-0.19140625,
-0.31591796875,
0.56982421875,
0.27587890625,
0.6376953125,
0.08624267578125,
-0.1968994140625,
0.140869140625,
1.521484375,
0.300048828125,
0.307373046875,
0.1361083984375,
0.7099609375,
0.5029296875,
0.62841796875,
0.269287109375,
0.29296875,
0.414306640625,
1.36328125,
0.031585693359375,
0.6103515625,
0.134521484375,
0.302734375,
0.66796875,
0.058837890625,
0.10137939453125,
0.49169921875,
-0.01210784912109375,
0.0157470703125,
-0.68359375,
-0.048095703125,
-0.384765625,
0.6845703125,
0.09173583984375,
0.55029296875,
-0.177001953125,
-0.28466796875,
0.430419921875,
1.2939453125,
-1.158203125,
-0.58740234375,
-0.255126953125,
0.0706787109375,
0.403564453125,
-0.00400543212890625,
0.52685546875,
0.397705078125,
0.402099609375,
0.6005859375,
-0.30810546875,
-0.218505859375,
-0.58447265625,
-0.91015625,
-0.552734375,
-0.1993408203125,
-0.4716796875,
0.016510009765625,
0.1944580078125,
-0.94921875,
0.36328125,
-0.6142578125,
-0.016998291015625,
0.057586669921875,
-0.6943359375,
0.5146484375,
-0.42578125,
0.51904296875,
-0.7021484375,
0.732421875,
0.75390625,
1.0927734375,
-0.40771484375,
-0.11395263671875,
0.0176544189453125,
-1.07421875,
0.2373046875,
0.2308349609375,
0.1524658203125,
-0.10015869140625,
-0.52001953125,
-0.75341796875,
-0.51806640625,
0.034881591796875,
-0.38671875,
0.172607421875,
-0.505859375,
0.5712890625,
0.56982421875,
-0.5283203125,
0.658203125,
0.7294921875,
-0.91796875,
0.11676025390625,
-0.00611114501953125,
0.1429443359375,
-0.95556640625,
1.2236328125,
0.33251953125,
0.611328125,
-0.63916015625,
-0.56982421875,
-0.51904296875,
-0.09002685546875,
0.342529296875,
-0.44921875,
-0.1622314453125,
0.479248046875,
0.052764892578125,
-1.3115234375,
0.4150390625,
-1.06640625,
-0.3173828125,
-0.26416015625,
-0.0299072265625,
0.572265625,
0.84765625,
0.06988525390625,
-0.76904296875,
-0.412109375,
0.11041259765625,
1.1220703125,
0.7236328125,
-0.72900390625,
0.11767578125,
0.76123046875,
0.1439208984375,
0.0467529296875,
0.307861328125,
-0.505859375,
-0.34912109375,
0.2783203125,
0.1478271484375,
0.85693359375,
0.1207275390625,
0.513671875,
0.708984375,
0.69287109375,
-0.689453125,
0.10235595703125,
-0.2744140625,
1.083984375,
0.8115234375,
0.48046875,
0.318603515625,
-0.458251953125,
-0.4228515625,
-1.03515625,
0.5341796875,
0.08056640625,
0.8388671875,
-0.703125,
0.66650390625,
0.348388671875,
-0.54052734375,
0.52001953125,
-0.92529296875,
-0.20263671875,
1.189453125,
-0.38623046875,
0.64306640625,
-0.373779296875,
0.281982421875,
-0.127685546875,
0.197265625,
0.238525390625,
0.85693359375,
0.62890625,
-0.47265625,
-0.374755859375,
-0.5087890625,
-0.01428985595703125,
0.07568359375,
-0.60107421875,
0.342529296875,
-0.130859375,
-0.312744140625,
-0.77294921875,
-0.032745361328125,
1.0693359375,
0.76318359375,
0.059539794921875,
0.11151123046875,
0.17041015625,
0.331298828125,
0.6171875,
-0.56298828125,
0.0723876953125,
-0.53271484375,
0.98046875,
0.068359375,
-0.0282135009765625,
-0.10394287109375,
-0.2132568359375,
-0.18505859375,
0.2108154296875,
-0.00818634033203125,
-0.41259765625,
-1.029296875,
0.08056640625,
-0.1375732421875,
0.32177734375,
-0.5791015625,
-0.08892822265625,
-0.288818359375,
0.67626953125,
0.15234375,
-1.275390625,
-0.0626220703125,
-0.9716796875,
0.445068359375,
0.15478515625,
0.027557373046875,
-0.47119140625,
-0.26171875,
-0.418212890625,
-0.63671875,
0.5185546875,
0.5947265625,
-0.310791015625,
-0.0240631103515625,
-1.220703125,
0.626953125,
0.21923828125,
0.1240234375,
0.28515625,
0.1768798828125,
0.5263671875,
-0.133056640625,
0.89404296875,
-0.57177734375,
-0.69189453125,
0.188232421875,
-1.1015625,
0.583984375,
-0.65283203125,
0.215576171875,
-0.31005859375,
-0.1298828125,
0.0110626220703125,
0.1995849609375,
-0.162841796875,
0.32275390625,
-0.471923828125,
0.0350341796875,
-0.9052734375,
-0.70166015625,
0.68896484375,
-0.499267578125,
-0.31787109375,
0.73291015625,
-0.03662109375,
0.06756591796875,
0.363037109375,
0.7421875,
-0.288818359375,
0.2802734375,
-0.445556640625,
0.87255859375,
-0.58837890625,
-0.74755859375,
-0.0528564453125,
-0.36279296875,
0.80859375,
-0.10882568359375,
-0.9482421875,
-0.38232421875,
-1.126953125,
-0.28369140625,
-0.0167694091796875,
-0.337158203125,
0.1990966796875,
0.1451416015625,
-0.227783203125,
0.09637451171875,
-0.26123046875,
-0.17333984375,
-0.68994140625,
-0.74072265625,
-0.17431640625,
0.52978515625,
0.306640625,
0.95263671875,
0.044403076171875,
-0.2332763671875,
-0.208251953125,
0.1435546875,
0.0670166015625,
-0.37841796875,
-0.81103515625,
-0.26513671875,
-0.208251953125,
-0.517578125,
0.4951171875,
0.1890869140625,
0.31298828125,
0.7724609375,
0.28564453125,
0.244384765625,
0.12939453125,
-0.52294921875,
1.0517578125,
0.41015625,
-1.0234375,
-0.7734375,
0.45947265625,
0.13232421875,
0.375,
0.38134765625,
-0.8349609375,
-0.61376953125,
-1.29296875,
0.483642578125,
-0.299072265625,
0.1671142578125,
-0.96923828125,
-0.9423828125,
-0.51220703125,
0.32421875,
-0.167724609375,
-0.44287109375,
0.0175018310546875,
-1.0810546875,
0.059661865234375,
-0.81494140625,
-0.2261962890625,
-0.7607421875,
-0.9453125,
-0.1422119140625,
0.7099609375,
0.33544921875,
0.312255859375,
-0.054534912109375,
-0.29638671875,
0.72607421875,
-0.09686279296875,
-0.8515625,
-0.4755859375,
-0.09698486328125,
0.10797119140625,
-0.2666015625,
-0.06817626953125,
-0.485107421875,
0.8173828125,
-0.966796875,
0.241943359375,
-0.0021305084228515625,
-0.43408203125,
-0.2415771484375,
-0.360107421875,
1.240234375,
-0.497314453125,
-0.22314453125,
-0.279296875,
0.80615234375,
0.54296875,
0.01009368896484375,
-0.415283203125,
-0.81884765625,
-0.72265625,
-0.92919921875,
0.3349609375,
-0.60400390625,
0.2081298828125,
-0.7890625,
-0.212646484375,
1.0087890625,
-0.6123046875,
0.3125,
1.2998046875,
-0.10357666015625,
0.197998046875,
-0.7255859375,
0.53662109375,
0.501953125,
-0.65576171875,
-0.09613037109375,
0.0888671875,
-0.67919921875,
0.0265960693359375,
-0.1177978515625,
-1.208984375,
-0.453857421875,
0.450439453125,
1.5126953125,
-0.4111328125,
0.76171875,
-0.13330078125,
-1.240234375,
-0.76025390625,
0.923828125,
0.444091796875,
0.85498046875,
1.07421875,
-0.3251953125,
-0.271484375,
0.034515380859375,
-0.08905029296875,
-0.564453125,
-0.429443359375,
1.3134765625,
-0.41357421875,
-0.35546875,
0.75732421875,
0.86767578125,
-1.279296875,
-0.64453125,
0.216064453125,
0.11297607421875,
-0.6064453125,
-0.5400390625,
0.39892578125,
0.6982421875,
-0.60546875,
0.2149658203125,
0.43359375,
-0.169189453125,
0.368896484375,
0.2073974609375,
-0.0340576171875,
-0.55322265625,
0.54296875,
0.54443359375,
-0.482177734375,
-0.470703125,
-1.0537109375,
-0.50927734375,
-0.32275390625,
0.11285400390625,
0.74755859375,
-0.30908203125,
0.376953125,
0.341552734375,
0.342529296875,
0.5732421875,
-0.319091796875,
-0.564453125,
0.415283203125,
-0.5185546875,
-0.425537109375,
0.2783203125,
0.436767578125,
-0.053192138671875,
0.484619140625,
-0.55224609375,
0.06585693359375,
0.32421875,
0.48876953125,
-0.419189453125,
-0.572265625,
-0.1478271484375,
-0.9931640625,
-0.4375,
-0.57958984375,
-0.94384765625,
-0.257080078125,
0.56591796875,
-0.019500732421875,
-0.7431640625,
-0.057281494140625,
0.51904296875,
-0.66943359375,
0.86083984375,
-0.42724609375,
0.2049560546875,
-0.49072265625,
-0.471435546875,
-0.6796875,
-0.04095458984375,
-0.61572265625,
0.1883544921875,
-0.77392578125,
0.55615234375,
-0.5126953125,
0.420654296875,
0.008636474609375,
0.82568359375,
-0.30908203125,
-0.8818359375,
-0.2462158203125,
0.053955078125,
0.0882568359375,
0.9130859375,
0.468505859375,
0.246826171875,
0.1292724609375,
-0.27685546875,
-0.11053466796875,
-0.6240234375,
0.385986328125,
0.76904296875,
-0.11566162109375,
-0.320068359375,
-0.48583984375,
0.2218017578125,
-0.91357421875,
0.409423828125,
-0.6103515625,
-0.043914794921875,
0.400390625,
-0.4384765625,
0.08074951171875,
1.310546875,
-0.1346435546875,
0.313232421875,
0.1903076171875,
-0.1802978515625,
0.419921875,
0.39111328125,
0.1378173828125,
0.362548828125,
-0.065185546875,
-0.357421875,
-0.07391357421875,
0.12335205078125,
0.193603515625,
0.2041015625,
-0.39990234375,
-0.5771484375,
-1.0771484375,
-0.300048828125,
0.08453369140625,
0.326416015625,
0.31396484375,
-0.1751708984375,
-0.160400390625,
-0.2425537109375,
-0.318359375,
-0.216552734375,
-1.1416015625,
-1.3388671875,
0.486328125,
-0.2425537109375,
-0.76318359375,
-0.355224609375,
-0.365478515625,
-0.1932373046875,
-0.00447845458984375,
0.43310546875,
-0.283935546875,
0.77880859375,
-0.208984375,
0.57666015625,
0.0312347412109375,
-0.2451171875,
0.270263671875,
0.312744140625,
-0.92822265625,
-0.6484375,
0.1934814453125,
1.51171875,
0.194580078125,
-0.298583984375,
-0.44873046875,
0.0887451171875,
-0.07806396484375,
0.2069091796875,
-0.5048828125,
0.1187744140625,
-0.323486328125,
0.289306640625,
-0.66943359375,
-0.335693359375,
-0.236328125,
0.45654296875,
0.59619140625,
-0.55126953125,
-0.77197265625,
0.78076171875,
0.60400390625,
-0.265625,
0.295654296875,
0.11065673828125,
0.22412109375,
-0.137451171875,
-0.01477813720703125,
-0.145751953125,
0.82763671875,
0.927734375,
0.63330078125,
-0.281982421875,
0.39111328125,
0.5234375,
-0.0290679931640625,
-0.027618408203125,
0.236328125,
0.39111328125,
0.09637451171875,
-0.1790771484375,
-0.64990234375,
-0.3515625,
-0.4140625,
-0.587890625,
0.11920166015625,
-0.1951904296875,
0.03594970703125,
-0.12384033203125,
-0.802734375,
0.9228515625,
-0.104248046875,
-0.1285400390625,
0.33056640625,
-0.478515625,
0.13427734375,
0.86865234375,
-0.2177734375,
-0.465087890625,
0.61083984375,
0.82568359375,
0.418701171875,
0.673828125,
0.01264190673828125,
0.397705078125,
-0.299072265625,
0.19775390625,
-0.1884765625,
0.0092010498046875,
-0.63916015625,
-0.50244140625,
-0.9111328125,
0.1402587890625,
-0.529296875,
-0.299072265625,
0.6201171875,
-0.487548828125,
0.061248779296875,
-0.040069580078125,
0.8505859375,
-0.6044921875,
0.422119140625,
0.434814453125,
-0.28076171875,
-0.95068359375,
0.81640625,
-0.250244140625,
0.276123046875,
-0.0291900634765625,
0.86328125,
-0.077392578125,
1.3564453125,
0.53125,
-0.3603515625,
0.10467529296875,
0.5859375,
-0.1552734375,
-0.6201171875,
-1.0302734375,
-0.141845703125,
-0.2125244140625,
-0.98876953125,
-0.0928955078125,
-0.1065673828125,
0.80029296875,
-0.05767822265625,
-0.8330078125,
-0.09075927734375,
0.255615234375,
-1.1904296875,
0.89697265625,
0.407470703125,
0.54296875,
-0.051116943359375,
-0.00634765625,
0.12255859375,
-1.1259765625,
0.35791015625,
-0.8369140625,
0.53173828125,
-0.7275390625,
-0.476806640625,
-1.0126953125,
-0.4970703125,
-0.097412109375,
-0.568359375,
-0.43310546875,
-1.1806640625,
0.21435546875,
0.6025390625,
0.5068359375,
0.333984375,
-0.293701171875,
0.3427734375,
1.2275390625,
0.80810546875,
0.1156005859375,
0.436767578125,
0.49853515625,
0.05914306640625,
-0.32373046875,
0.04132080078125,
0.366455078125,
-0.06854248046875,
0.272705078125,
-0.24658203125,
0.0615234375,
-0.59912109375,
-1.6318359375,
0.1636962890625,
0.6123046875,
0.406005859375,
-0.67138671875,
-0.65576171875,
-0.12493896484375,
-0.7861328125,
-0.3896484375,
-0.51708984375,
0.60791015625,
-0.228271484375,
-0.0002467632293701172,
-0.30615234375,
-1.1572265625,
4.21875,
0.98193359375,
1.162109375,
0.2325439453125,
0.415771484375,
1.0458984375,
0.1405029296875,
-0.4619140625,
0.1605224609375,
-0.37255859375,
0.315185546875,
-0.421142578125,
0.17529296875,
0.62890625,
0.016998291015625,
0.7451171875,
-0.44677734375,
0.292724609375,
-0.029327392578125,
-1.1015625,
-0.97021484375,
0.556640625,
-0.07232666015625,
1.0263671875,
0.262451171875,
0.20556640625,
0.346435546875,
-0.39501953125,
-0.11981201171875,
-0.488037109375,
0.365478515625,
-0.404541015625,
0.89892578125,
0.06488037109375,
-0.055389404296875,
0.3779296875,
-0.033843994140625,
-0.239013671875,
-0.005645751953125,
0.1221923828125,
0.06805419921875,
0.1497802734375,
0.60498046875,
-1.1845703125,
-0.2406005859375,
0.46533203125,
-0.76953125,
0.412109375,
0.154541015625,
-0.96435546875,
0.69873046875,
-0.294189453125,
0.474853515625,
-0.578125,
-0.8447265625,
-0.06976318359375,
-0.058624267578125,
-0.0401611328125,
0.0555419921875,
0.67919921875,
0.671875,
0.890625,
-0.07586669921875,
0.1494140625,
-1.2314453125,
0.66162109375,
0.35400390625,
0.619140625,
-0.66748046875,
0.07379150390625,
-0.1365966796875,
-0.437744140625,
-0.25341796875,
-0.140380859375,
0.62109375,
0.403076171875,
-0.27197265625,
0.853515625,
0.44921875,
-0.671875,
0.2056884765625,
-0.6171875,
-0.221435546875,
-0.55029296875,
0.671875,
1.2314453125,
-0.55712890625,
-0.426025390625,
-0.264892578125,
0.42626953125,
0.311767578125,
0.85205078125,
0.33203125,
-0.68212890625,
0.07000732421875
] | 0
|
Provide tags and a correct Python 3 solution for this coding contest problem.
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 ≤ 100, 1 ≤ k ≤ 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
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.
Tags: dp, strings
Correct Solution:
```
def super_solve(n, k, s):
last = []
for i in range (0, 256):
last.append(0)
dp = []
for i in range (0, 105):
tmp = []
for j in range (0, 105):
tmp.append(0)
dp.append( tmp )
now = []
for i in range (0, 105):
tmp = []
for j in range (0, 105):
tmp.append(0)
now.append( tmp )
dp[0][0] = 1
now[0][0] = 1
for i in range (1, n + 1):
c = ord(s[i])
for j in range (0, n + 1):
dp[i][j] += dp[i-1][j]
for j in range (1, n + 1):
dp[i][j] += dp[i-1][j-1]
if last[c] > 0:
for j in range (1, n + 1):
dp[i][j] -= dp[ last[c] - 1 ][j - 1]
for j in range (0, n + 1):
now[i][j] = dp[i][j] - dp[i-1][j]
last[c] = i
cost = 0
baki = k
j = n
while( j >= 0 ):
for i in range (0, n + 1):
cur = now[i][j]
my = min(baki, cur)
cost += my * j
baki -= my
j -= 1
ret = k * n - cost
if baki > 0:
ret = -1
return ret
def main():
line = input()
line = line.split(' ')
n = int(line[0])
k = int(line[1])
tmp = input()
s = []
s.append(0)
for i in range (0, n):
s.append( tmp[i] )
ret = super_solve(n, k, s)
print (ret)
if __name__== "__main__":
main()
```
| 98
|
[
0.00617218017578125,
0.28955078125,
0.285888671875,
0.430419921875,
-0.07403564453125,
-0.213623046875,
-0.1988525390625,
-0.1016845703125,
0.423095703125,
0.67236328125,
0.77294921875,
-0.2490234375,
-0.23974609375,
-1.0107421875,
-0.587890625,
0.190185546875,
-0.634765625,
-0.81396484375,
-0.39794921875,
-0.2073974609375,
0.062347412109375,
-0.08056640625,
-1.3642578125,
-0.373046875,
0.273193359375,
1.14453125,
0.23193359375,
0.1328125,
1.109375,
1.2734375,
-0.1951904296875,
-0.876953125,
0.478515625,
-1.0224609375,
-0.50732421875,
-0.1282958984375,
0.363037109375,
-0.54150390625,
-0.51123046875,
-0.147705078125,
0.3623046875,
-0.7119140625,
0.5908203125,
-0.49462890625,
-1.283203125,
-0.1632080078125,
-0.07025146484375,
-0.355712890625,
-0.499755859375,
-0.11383056640625,
0.07611083984375,
-0.10247802734375,
0.4921875,
0.06756591796875,
0.054412841796875,
0.400634765625,
0.1937255859375,
-0.302490234375,
-0.748046875,
0.6220703125,
0.32373046875,
-0.08306884765625,
0.0941162109375,
-0.52197265625,
-0.51171875,
0.2010498046875,
-0.404052734375,
0.12384033203125,
0.08538818359375,
-0.623046875,
0.043548583984375,
0.1265869140625,
0.1336669921875,
-0.50439453125,
-0.26025390625,
-0.355224609375,
0.1370849609375,
-0.212890625,
-0.48486328125,
0.6435546875,
0.047210693359375,
0.92236328125,
0.287353515625,
-0.064208984375,
-0.69775390625,
-0.63916015625,
-0.00518035888671875,
0.123046875,
0.35498046875,
0.022186279296875,
-0.54150390625,
0.99609375,
-0.60400390625,
-0.0140838623046875,
0.5048828125,
0.673828125,
-0.59326171875,
0.2357177734375,
0.3837890625,
0.16845703125,
0.461669921875,
0.67529296875,
-0.4794921875,
1.154296875,
-0.1263427734375,
-0.22607421875,
0.1900634765625,
-0.395263671875,
-0.17578125,
-0.292724609375,
-0.1314697265625,
-0.40625,
0.458740234375,
-0.17626953125,
-0.430908203125,
0.95947265625,
-0.0887451171875,
0.52880859375,
-0.662109375,
0.36083984375,
0.489501953125,
0.0870361328125,
0.630859375,
-0.1092529296875,
-0.1806640625,
-0.35546875,
-0.64794921875,
1.1728515625,
-0.63623046875,
0.362060546875,
0.56689453125,
-0.272216796875,
-0.28173828125,
0.411865234375,
-0.235107421875,
0.705078125,
-0.2073974609375,
0.5361328125,
0.61865234375,
-0.5,
0.7431640625,
-0.17626953125,
-0.005245208740234375,
1.529296875,
0.65283203125,
0.2130126953125,
1.0419921875,
0.332275390625,
-0.5888671875,
0.50390625,
-1.0380859375,
0.75,
0.3173828125,
0.1427001953125,
-0.33349609375,
-0.10296630859375,
-0.064208984375,
0.394287109375,
0.11968994140625,
0.427734375,
-0.3486328125,
0.33251953125,
-0.296142578125,
0.3779296875,
-0.61279296875,
0.80517578125,
-0.1622314453125,
-0.1873779296875,
0.0321044921875,
-0.75244140625,
0.423095703125,
-0.2083740234375,
-0.1011962890625,
0.75634765625,
0.357666015625,
0.9990234375,
1.0869140625,
-0.07318115234375,
0.50830078125,
0.595703125,
-0.26513671875,
0.2236328125,
0.462158203125,
0.51611328125,
0.927734375,
0.187255859375,
0.0728759765625,
0.0205535888671875,
-0.673828125,
-0.69580078125,
0.222412109375,
0.6220703125,
-0.339111328125,
0.301513671875,
0.03973388671875,
0.010955810546875,
-1.1728515625,
-0.273681640625,
-0.1097412109375,
-0.85888671875,
-0.7001953125,
0.69384765625,
0.042938232421875,
0.24951171875,
-0.59228515625,
-0.88671875,
0.6376953125,
1.1650390625,
-0.443603515625,
0.047088623046875,
0.71728515625,
0.03643798828125,
-0.375,
0.47900390625,
-0.09429931640625,
-0.427490234375,
-0.7177734375,
0.69287109375,
-0.332275390625,
-0.144287109375,
-0.309814453125,
0.5849609375,
0.2415771484375,
0.646484375,
0.05633544921875,
-0.2232666015625,
0.177734375,
1.486328125,
0.22802734375,
0.30419921875,
0.1517333984375,
0.671875,
0.51904296875,
0.63916015625,
0.286865234375,
0.33349609375,
0.4609375,
1.3212890625,
-0.039337158203125,
0.5673828125,
0.159912109375,
0.2388916015625,
0.65478515625,
0.1605224609375,
0.1055908203125,
0.5517578125,
0.0276641845703125,
-0.005428314208984375,
-0.6748046875,
-0.0234527587890625,
-0.34326171875,
0.66796875,
0.06903076171875,
0.56640625,
-0.26123046875,
-0.1900634765625,
0.4130859375,
1.2861328125,
-1.1181640625,
-0.65234375,
-0.25634765625,
0.01371002197265625,
0.3525390625,
-0.0160064697265625,
0.463623046875,
0.38671875,
0.411376953125,
0.64208984375,
-0.2666015625,
-0.2166748046875,
-0.60400390625,
-0.9384765625,
-0.55322265625,
-0.263671875,
-0.484619140625,
0.014495849609375,
0.1845703125,
-0.8544921875,
0.302001953125,
-0.53369140625,
-0.0355224609375,
0.06121826171875,
-0.693359375,
0.54736328125,
-0.356689453125,
0.55615234375,
-0.74365234375,
0.6748046875,
0.70458984375,
1.0361328125,
-0.4296875,
-0.13330078125,
0.07135009765625,
-1.095703125,
0.2357177734375,
0.2332763671875,
0.11578369140625,
-0.136962890625,
-0.50537109375,
-0.732421875,
-0.53564453125,
0.0994873046875,
-0.37353515625,
0.2286376953125,
-0.5498046875,
0.58447265625,
0.52587890625,
-0.5048828125,
0.640625,
0.728515625,
-0.88916015625,
0.145751953125,
0.005161285400390625,
0.167236328125,
-0.96044921875,
1.25390625,
0.378662109375,
0.55126953125,
-0.638671875,
-0.63232421875,
-0.45654296875,
-0.05474853515625,
0.320068359375,
-0.47265625,
-0.16552734375,
0.478271484375,
-0.0228271484375,
-1.2724609375,
0.37548828125,
-1.0732421875,
-0.4677734375,
-0.2479248046875,
-0.07952880859375,
0.5546875,
0.78125,
0.0782470703125,
-0.71630859375,
-0.344970703125,
0.0621337890625,
1.15234375,
0.705078125,
-0.7197265625,
0.17431640625,
0.82275390625,
0.168212890625,
0.076171875,
0.284912109375,
-0.41015625,
-0.370361328125,
0.265625,
0.10601806640625,
0.81884765625,
0.100341796875,
0.488525390625,
0.73779296875,
0.611328125,
-0.72412109375,
0.03936767578125,
-0.315185546875,
1.0673828125,
0.73828125,
0.5166015625,
0.373291015625,
-0.40966796875,
-0.48681640625,
-0.9912109375,
0.513671875,
0.07855224609375,
0.84521484375,
-0.64501953125,
0.642578125,
0.37890625,
-0.5595703125,
0.53271484375,
-0.9638671875,
-0.1348876953125,
1.1484375,
-0.353515625,
0.6376953125,
-0.412841796875,
0.30126953125,
-0.13671875,
0.2091064453125,
0.245361328125,
0.77685546875,
0.58740234375,
-0.4609375,
-0.43505859375,
-0.51904296875,
-0.0191497802734375,
0.035980224609375,
-0.5703125,
0.40625,
-0.18994140625,
-0.31884765625,
-0.82275390625,
0.0027294158935546875,
1.078125,
0.68994140625,
0.076416015625,
0.2198486328125,
0.1629638671875,
0.315673828125,
0.62060546875,
-0.544921875,
0.021759033203125,
-0.568359375,
0.91259765625,
0.0928955078125,
-0.0182342529296875,
-0.14013671875,
-0.22705078125,
-0.218994140625,
0.129150390625,
0.00644683837890625,
-0.43798828125,
-1.041015625,
0.09100341796875,
-0.06988525390625,
0.322509765625,
-0.65966796875,
-0.01256561279296875,
-0.259521484375,
0.5966796875,
0.19091796875,
-1.3076171875,
-0.0487060546875,
-1.107421875,
0.455810546875,
0.11480712890625,
0.102783203125,
-0.435546875,
-0.271484375,
-0.469482421875,
-0.609375,
0.448486328125,
0.62548828125,
-0.2445068359375,
0.02032470703125,
-1.2392578125,
0.6064453125,
0.303466796875,
0.0689697265625,
0.309326171875,
0.1717529296875,
0.51708984375,
-0.1385498046875,
0.9423828125,
-0.50439453125,
-0.66748046875,
0.175048828125,
-1.0361328125,
0.595703125,
-0.65283203125,
0.193603515625,
-0.3544921875,
-0.1470947265625,
0.0531005859375,
0.225341796875,
-0.16064453125,
0.296875,
-0.515625,
-0.0282135009765625,
-0.85888671875,
-0.724609375,
0.70361328125,
-0.473876953125,
-0.2445068359375,
0.7314453125,
-0.0408935546875,
-0.0814208984375,
0.2978515625,
0.78369140625,
-0.3544921875,
0.325927734375,
-0.441650390625,
0.83740234375,
-0.54443359375,
-0.810546875,
0.00882720947265625,
-0.430908203125,
0.8271484375,
-0.127685546875,
-0.95556640625,
-0.353759765625,
-1.1337890625,
-0.1988525390625,
0.018798828125,
-0.33837890625,
0.32177734375,
0.10369873046875,
-0.265869140625,
0.08416748046875,
-0.2421875,
-0.2020263671875,
-0.68017578125,
-0.7021484375,
-0.1907958984375,
0.51806640625,
0.321533203125,
0.947265625,
0.0875244140625,
-0.12457275390625,
-0.2144775390625,
0.20947265625,
0.1279296875,
-0.403564453125,
-0.779296875,
-0.316162109375,
-0.252685546875,
-0.51953125,
0.51220703125,
0.1640625,
0.323486328125,
0.78857421875,
0.322509765625,
0.296875,
0.105712890625,
-0.50341796875,
1.0927734375,
0.376708984375,
-1.0947265625,
-0.7958984375,
0.408447265625,
0.12237548828125,
0.40771484375,
0.28759765625,
-0.88134765625,
-0.5810546875,
-1.271484375,
0.4736328125,
-0.37841796875,
0.090576171875,
-0.9306640625,
-0.94921875,
-0.480224609375,
0.3583984375,
-0.142333984375,
-0.43701171875,
-0.013916015625,
-1.1279296875,
-0.035247802734375,
-0.80322265625,
-0.227783203125,
-0.84716796875,
-0.953125,
-0.1431884765625,
0.75244140625,
0.301025390625,
0.296875,
0.008392333984375,
-0.27783203125,
0.71728515625,
-0.06494140625,
-0.869140625,
-0.5224609375,
-0.07769775390625,
0.004001617431640625,
-0.25146484375,
-0.07147216796875,
-0.509765625,
0.7392578125,
-0.97802734375,
0.2890625,
-0.036102294921875,
-0.428466796875,
-0.2393798828125,
-0.34716796875,
1.2734375,
-0.4677734375,
-0.25341796875,
-0.3251953125,
0.73876953125,
0.60888671875,
0.025238037109375,
-0.489990234375,
-0.76806640625,
-0.69580078125,
-1.0048828125,
0.383056640625,
-0.57275390625,
0.2188720703125,
-0.7724609375,
-0.2381591796875,
0.9111328125,
-0.576171875,
0.296630859375,
1.21875,
-0.08831787109375,
0.2484130859375,
-0.62158203125,
0.56005859375,
0.52197265625,
-0.662109375,
-0.1456298828125,
0.09576416015625,
-0.6025390625,
-0.003009796142578125,
-0.183349609375,
-1.2421875,
-0.458984375,
0.449462890625,
1.435546875,
-0.533203125,
0.857421875,
-0.1458740234375,
-1.2998046875,
-0.7470703125,
0.89208984375,
0.39990234375,
0.8662109375,
1.0302734375,
-0.329833984375,
-0.2841796875,
-0.0024585723876953125,
0.0487060546875,
-0.58447265625,
-0.477294921875,
1.2958984375,
-0.40625,
-0.392333984375,
0.7705078125,
0.91650390625,
-1.23046875,
-0.71044921875,
0.2152099609375,
0.09326171875,
-0.52734375,
-0.54248046875,
0.47705078125,
0.7060546875,
-0.67626953125,
0.2398681640625,
0.494873046875,
-0.2314453125,
0.40478515625,
0.1998291015625,
0.010009765625,
-0.6220703125,
0.51513671875,
0.5498046875,
-0.447998046875,
-0.50146484375,
-1.01171875,
-0.492431640625,
-0.298828125,
0.103759765625,
0.73974609375,
-0.313720703125,
0.356201171875,
0.35107421875,
0.362060546875,
0.59033203125,
-0.35302734375,
-0.52880859375,
0.3525390625,
-0.6044921875,
-0.4794921875,
0.25244140625,
0.39453125,
-0.062225341796875,
0.51513671875,
-0.50048828125,
0.042236328125,
0.326904296875,
0.564453125,
-0.451904296875,
-0.5517578125,
-0.2318115234375,
-0.91796875,
-0.466796875,
-0.65673828125,
-0.89892578125,
-0.20947265625,
0.65576171875,
-0.058685302734375,
-0.751953125,
-0.0205841064453125,
0.4970703125,
-0.732421875,
0.845703125,
-0.41650390625,
0.15869140625,
-0.5380859375,
-0.414794921875,
-0.64208984375,
-0.06256103515625,
-0.55126953125,
0.2423095703125,
-0.8056640625,
0.60693359375,
-0.5185546875,
0.435302734375,
-0.0224609375,
0.77587890625,
-0.385986328125,
-0.81689453125,
-0.254638671875,
0.1083984375,
0.0205535888671875,
0.98486328125,
0.449951171875,
0.27099609375,
0.06365966796875,
-0.3388671875,
-0.13134765625,
-0.57275390625,
0.47314453125,
0.65966796875,
-0.10467529296875,
-0.275146484375,
-0.444580078125,
0.1749267578125,
-0.884765625,
0.36376953125,
-0.6162109375,
-0.0567626953125,
0.340576171875,
-0.455810546875,
0.1375732421875,
1.2841796875,
-0.2237548828125,
0.31298828125,
0.1630859375,
-0.208740234375,
0.4091796875,
0.3671875,
0.140380859375,
0.33984375,
-0.053131103515625,
-0.312744140625,
-0.10986328125,
0.1573486328125,
0.184326171875,
0.166748046875,
-0.29443359375,
-0.63427734375,
-1.1279296875,
-0.302001953125,
0.0853271484375,
0.301513671875,
0.29541015625,
-0.2144775390625,
-0.2005615234375,
-0.1871337890625,
-0.40625,
-0.1630859375,
-1.1240234375,
-1.337890625,
0.436279296875,
-0.276123046875,
-0.75537109375,
-0.230224609375,
-0.367919921875,
-0.11016845703125,
-0.05523681640625,
0.495361328125,
-0.285888671875,
0.74658203125,
-0.1632080078125,
0.52734375,
0.0293426513671875,
-0.23876953125,
0.258544921875,
0.32275390625,
-0.90234375,
-0.615234375,
0.11529541015625,
1.4765625,
0.2626953125,
-0.2490234375,
-0.494873046875,
0.11370849609375,
-0.039306640625,
0.22021484375,
-0.475830078125,
0.179931640625,
-0.39501953125,
0.341552734375,
-0.73486328125,
-0.330322265625,
-0.255859375,
0.489990234375,
0.53271484375,
-0.52490234375,
-0.76708984375,
0.7255859375,
0.60791015625,
-0.30224609375,
0.279541015625,
0.10260009765625,
0.154052734375,
-0.04095458984375,
-0.029083251953125,
-0.156494140625,
0.83349609375,
0.9189453125,
0.6982421875,
-0.302978515625,
0.413330078125,
0.58203125,
0.0687255859375,
0.0156707763671875,
0.2017822265625,
0.42919921875,
0.1566162109375,
-0.1513671875,
-0.58154296875,
-0.341064453125,
-0.318115234375,
-0.56298828125,
0.1177978515625,
-0.27685546875,
0.03594970703125,
-0.1026611328125,
-0.80029296875,
0.88623046875,
-0.1043701171875,
-0.13427734375,
0.307861328125,
-0.5126953125,
0.1165771484375,
0.77685546875,
-0.250244140625,
-0.468994140625,
0.60595703125,
0.83984375,
0.43115234375,
0.72900390625,
0.05810546875,
0.4384765625,
-0.2366943359375,
0.1392822265625,
-0.1693115234375,
-0.0245208740234375,
-0.58154296875,
-0.5029296875,
-0.95947265625,
0.07965087890625,
-0.4970703125,
-0.328125,
0.62744140625,
-0.48486328125,
0.032623291015625,
-0.04779052734375,
0.8134765625,
-0.59814453125,
0.4140625,
0.440673828125,
-0.3125,
-0.9765625,
0.80859375,
-0.2071533203125,
0.2252197265625,
-0.0081329345703125,
0.8193359375,
-0.06585693359375,
1.4404296875,
0.533203125,
-0.332275390625,
0.041229248046875,
0.5849609375,
-0.20849609375,
-0.56201171875,
-0.96728515625,
-0.1708984375,
-0.26318359375,
-1.013671875,
-0.168701171875,
-0.1002197265625,
0.7548828125,
-0.09112548828125,
-0.78125,
-0.0731201171875,
0.371826171875,
-1.1806640625,
0.88671875,
0.428955078125,
0.607421875,
-0.0274658203125,
-0.06268310546875,
0.08599853515625,
-1.103515625,
0.3916015625,
-0.84716796875,
0.51123046875,
-0.685546875,
-0.406982421875,
-0.96826171875,
-0.3603515625,
-0.1484375,
-0.64013671875,
-0.443603515625,
-1.2060546875,
0.234130859375,
0.607421875,
0.46240234375,
0.330322265625,
-0.1700439453125,
0.342041015625,
1.185546875,
0.88525390625,
0.052734375,
0.4677734375,
0.48779296875,
0.036468505859375,
-0.372314453125,
0.0770263671875,
0.341552734375,
-0.11248779296875,
0.2509765625,
-0.303955078125,
0.045074462890625,
-0.67724609375,
-1.58203125,
0.1700439453125,
0.6552734375,
0.42919921875,
-0.71923828125,
-0.71923828125,
-0.09613037109375,
-0.84375,
-0.424072265625,
-0.50732421875,
0.57958984375,
-0.242919921875,
0.035919189453125,
-0.353515625,
-1.140625,
4.234375,
1.0029296875,
1.1591796875,
0.30419921875,
0.5048828125,
1.015625,
0.127685546875,
-0.490478515625,
0.1707763671875,
-0.43798828125,
0.255126953125,
-0.468505859375,
0.23046875,
0.5419921875,
0.0038585662841796875,
0.76708984375,
-0.392333984375,
0.399658203125,
-0.034942626953125,
-1.111328125,
-1.0751953125,
0.47998046875,
-0.046722412109375,
0.9296875,
0.32958984375,
0.252685546875,
0.3271484375,
-0.34375,
-0.1739501953125,
-0.4873046875,
0.3349609375,
-0.3974609375,
0.92919921875,
0.1051025390625,
-0.0228424072265625,
0.51220703125,
-0.0262603759765625,
-0.22998046875,
0.00897216796875,
0.1578369140625,
0.03546142578125,
0.1253662109375,
0.599609375,
-1.2001953125,
-0.26708984375,
0.43408203125,
-0.67578125,
0.394775390625,
0.0628662109375,
-0.9072265625,
0.681640625,
-0.30419921875,
0.474853515625,
-0.49072265625,
-0.76953125,
-0.021881103515625,
-0.1275634765625,
0.0080108642578125,
0.0682373046875,
0.7080078125,
0.77783203125,
0.91259765625,
-0.0994873046875,
0.1064453125,
-1.2294921875,
0.62255859375,
0.361572265625,
0.599609375,
-0.65869140625,
0.099609375,
-0.1575927734375,
-0.57080078125,
-0.226318359375,
-0.168701171875,
0.70068359375,
0.373291015625,
-0.2471923828125,
0.8076171875,
0.449951171875,
-0.60595703125,
0.2364501953125,
-0.60595703125,
-0.277099609375,
-0.60498046875,
0.69873046875,
1.2294921875,
-0.53662109375,
-0.425048828125,
-0.242919921875,
0.51220703125,
0.349365234375,
0.873046875,
0.321044921875,
-0.673828125,
0.061614990234375
] | 0
|
Provide tags and a correct Python 3 solution for this coding contest problem.
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 ≤ 100, 1 ≤ k ≤ 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
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.
Tags: dp, strings
Correct Solution:
```
# NUMBER OF DISTINCT SUBSEQUENCES OF EVERY LENGTH K FOR 0<=K<=len(s)
n,k=map(int,input().split())
s=input()
pos=[-1]*26
lst=[]
for i in range(0,len(s)):
pos[ord(s[i])-97]=i
h=[]
for j in range(26):
h.append(pos[j])
lst.append(h)
dp=[]
for i in range(n):
h=[]
for j in range(n+1):
h.append(0)
dp.append(h)
for i in range(n):
dp[i][1]=1
for j in range(2,n+1):
for i in range(1,n):
for e in range(26):
if(lst[i-1][e]!=-1):
dp[i][j]=dp[i][j]+dp[lst[i-1][e]][j-1]
ans=0
for j in range(n,0,-1):
c=0
for e in range(26):
if(lst[n-1][e]!=-1):
c+=dp[lst[n-1][e]][j]
if(k-c>=0):
ans+=(c*(n-j))
k-=c
else:
req=k
ans+=(req*(n-j))
k-=req
break
if(k==1):
ans+=n
k-=1
if(k>0):
print(-1)
else:
print(ans)
"""
# TOTAL NUMBER OF DISTINCT SUBSEQUENCES IN A STRING
## APPROACH 1
MOD=1000000007
s=input()
n=len(s)
dp=[1] # for empty string ''
kk=dict()
for i in range(0,len(s)):
term=(dp[-1]*2)%MOD
if(kk.get(s[i])!=None):
term=(term-dp[kk[s[i]]])%MOD
kk[s[i]]=i
dp.append(term)
print(dp[-1])
## APPROACH 2
MOD=1000000007
s=input()
n=len(s)
pos=[-1]*26
dp=[1] # for empty string ''
for i in range(0,len(s)):
c=1
for j in range(26):
if(pos[j]!=-1):
c=(c+dp[pos[j]+1])%MOD
pos[ord(s[i])-97]=i
dp.append(c)
ans=1
for i in range(0,26):
if(pos[i]!=-1):
ans=(ans+dp[pos[i]+1])%MOD
print(ans)
"""
```
| 99
|
[
-0.07379150390625,
0.302978515625,
0.236572265625,
0.43212890625,
-0.032440185546875,
-0.1434326171875,
-0.208251953125,
-0.1617431640625,
0.477783203125,
0.79443359375,
0.7138671875,
-0.3037109375,
-0.198974609375,
-0.98193359375,
-0.6611328125,
0.153076171875,
-0.66650390625,
-0.84423828125,
-0.407470703125,
-0.234375,
0.089599609375,
-0.13720703125,
-1.3876953125,
-0.434814453125,
0.2198486328125,
1.1318359375,
0.2139892578125,
0.229736328125,
1.1064453125,
1.318359375,
-0.2020263671875,
-0.89599609375,
0.433837890625,
-1.1376953125,
-0.50634765625,
-0.133056640625,
0.347900390625,
-0.55859375,
-0.456298828125,
-0.099853515625,
0.392822265625,
-0.7421875,
0.67431640625,
-0.461669921875,
-1.2158203125,
-0.1533203125,
-0.106201171875,
-0.441162109375,
-0.50927734375,
-0.0797119140625,
0.1343994140625,
-0.10089111328125,
0.54931640625,
0.05487060546875,
0.109130859375,
0.35302734375,
0.259033203125,
-0.326416015625,
-0.6845703125,
0.59033203125,
0.15869140625,
-0.107666015625,
0.08966064453125,
-0.626953125,
-0.5478515625,
0.2274169921875,
-0.427734375,
0.0997314453125,
0.07928466796875,
-0.7490234375,
-0.0654296875,
0.0609130859375,
-0.064697265625,
-0.6318359375,
-0.196044921875,
-0.4248046875,
0.1597900390625,
-0.259765625,
-0.57470703125,
0.63623046875,
0.1119384765625,
0.9384765625,
0.35009765625,
-0.039520263671875,
-0.6630859375,
-0.638671875,
-0.05157470703125,
0.15673828125,
0.3779296875,
0.07794189453125,
-0.484130859375,
1.0048828125,
-0.716796875,
-0.12286376953125,
0.4248046875,
0.6142578125,
-0.57275390625,
0.28759765625,
0.3037109375,
0.0693359375,
0.471435546875,
0.7509765625,
-0.473876953125,
1.18359375,
-0.035614013671875,
-0.261474609375,
0.296630859375,
-0.369384765625,
-0.2371826171875,
-0.350830078125,
-0.2010498046875,
-0.459228515625,
0.382568359375,
-0.154541015625,
-0.46484375,
1.048828125,
-0.19140625,
0.5693359375,
-0.6123046875,
0.435791015625,
0.52197265625,
0.12176513671875,
0.626953125,
-0.1307373046875,
-0.11962890625,
-0.41064453125,
-0.54052734375,
1.177734375,
-0.68505859375,
0.43359375,
0.58740234375,
-0.205322265625,
-0.29248046875,
0.373779296875,
-0.174072265625,
0.677734375,
-0.177001953125,
0.5009765625,
0.5986328125,
-0.509765625,
0.73876953125,
-0.2015380859375,
-0.0185089111328125,
1.6328125,
0.6357421875,
0.26025390625,
1.0869140625,
0.326416015625,
-0.580078125,
0.483642578125,
-1.0615234375,
0.7666015625,
0.407958984375,
0.1185302734375,
-0.305419921875,
-0.10302734375,
-0.04168701171875,
0.485107421875,
0.028045654296875,
0.4521484375,
-0.390625,
0.294677734375,
-0.21875,
0.365478515625,
-0.59130859375,
0.9453125,
-0.19384765625,
-0.275146484375,
0.0257110595703125,
-0.77099609375,
0.4345703125,
-0.14306640625,
-0.126708984375,
0.7060546875,
0.40625,
1.064453125,
1.046875,
-0.0216217041015625,
0.5419921875,
0.61279296875,
-0.30224609375,
0.185546875,
0.4248046875,
0.54052734375,
0.8642578125,
0.197509765625,
0.11053466796875,
-0.0623779296875,
-0.724609375,
-0.7138671875,
0.2119140625,
0.69091796875,
-0.2449951171875,
0.1456298828125,
0.104736328125,
0.033905029296875,
-1.1416015625,
-0.219482421875,
-0.1287841796875,
-0.90185546875,
-0.6513671875,
0.5537109375,
0.1263427734375,
0.22412109375,
-0.5810546875,
-0.78515625,
0.66845703125,
1.09765625,
-0.443603515625,
-0.00848388671875,
0.705078125,
0.10601806640625,
-0.4306640625,
0.4453125,
-0.06915283203125,
-0.38232421875,
-0.71435546875,
0.62060546875,
-0.3388671875,
-0.185302734375,
-0.2880859375,
0.59619140625,
0.23974609375,
0.60693359375,
0.0207366943359375,
-0.1820068359375,
0.2119140625,
1.556640625,
0.230224609375,
0.347900390625,
0.170654296875,
0.73681640625,
0.51904296875,
0.6435546875,
0.28466796875,
0.313720703125,
0.45849609375,
1.35546875,
0.057708740234375,
0.58935546875,
0.1168212890625,
0.350830078125,
0.6318359375,
0.11285400390625,
0.09820556640625,
0.50537109375,
-0.0294342041015625,
-0.01149749755859375,
-0.67333984375,
-0.0012102127075195312,
-0.3740234375,
0.6708984375,
0.07666015625,
0.56640625,
-0.1253662109375,
-0.286376953125,
0.463134765625,
1.2861328125,
-1.1328125,
-0.6416015625,
-0.21533203125,
0.10137939453125,
0.4033203125,
-0.037841796875,
0.52783203125,
0.40478515625,
0.3916015625,
0.60205078125,
-0.259765625,
-0.2073974609375,
-0.58447265625,
-0.90478515625,
-0.56396484375,
-0.2054443359375,
-0.50146484375,
0.0269927978515625,
0.183349609375,
-0.96533203125,
0.35400390625,
-0.59765625,
-0.034149169921875,
0.04156494140625,
-0.74853515625,
0.471923828125,
-0.45263671875,
0.50341796875,
-0.7001953125,
0.7275390625,
0.7158203125,
1.064453125,
-0.406982421875,
-0.1324462890625,
0.0253448486328125,
-1.05859375,
0.25,
0.24658203125,
0.1895751953125,
-0.1573486328125,
-0.5029296875,
-0.73779296875,
-0.48974609375,
-0.0279998779296875,
-0.384765625,
0.10943603515625,
-0.52587890625,
0.63427734375,
0.62158203125,
-0.498291015625,
0.66552734375,
0.69970703125,
-0.90966796875,
0.1451416015625,
-0.039093017578125,
0.2125244140625,
-0.97265625,
1.2958984375,
0.341796875,
0.6259765625,
-0.64453125,
-0.55859375,
-0.501953125,
-0.06634521484375,
0.326171875,
-0.458984375,
-0.1427001953125,
0.497314453125,
0.03399658203125,
-1.3525390625,
0.439453125,
-1.1142578125,
-0.320068359375,
-0.265380859375,
0.0256500244140625,
0.56103515625,
0.86376953125,
0.058746337890625,
-0.794921875,
-0.443603515625,
0.07421875,
1.064453125,
0.6806640625,
-0.7255859375,
0.0732421875,
0.763671875,
0.10736083984375,
0.0804443359375,
0.25537109375,
-0.54052734375,
-0.28955078125,
0.25537109375,
0.1485595703125,
0.90283203125,
0.166748046875,
0.48046875,
0.68212890625,
0.68359375,
-0.73974609375,
0.06781005859375,
-0.306640625,
1.1396484375,
0.80859375,
0.5,
0.318603515625,
-0.471435546875,
-0.44384765625,
-1.1083984375,
0.5390625,
0.055633544921875,
0.87255859375,
-0.7451171875,
0.6708984375,
0.43603515625,
-0.552734375,
0.49365234375,
-0.9169921875,
-0.21337890625,
1.220703125,
-0.400634765625,
0.61279296875,
-0.3330078125,
0.25341796875,
-0.116943359375,
0.181884765625,
0.26171875,
0.87451171875,
0.64697265625,
-0.470458984375,
-0.42724609375,
-0.5068359375,
-0.0284271240234375,
0.10614013671875,
-0.63623046875,
0.282958984375,
-0.1683349609375,
-0.318115234375,
-0.8115234375,
-0.06756591796875,
1.072265625,
0.73095703125,
0.017486572265625,
0.10321044921875,
0.193359375,
0.2607421875,
0.60302734375,
-0.6083984375,
0.08135986328125,
-0.580078125,
0.96728515625,
0.0714111328125,
-0.04241943359375,
-0.115966796875,
-0.2449951171875,
-0.154296875,
0.194091796875,
0.019622802734375,
-0.404296875,
-1.013671875,
0.09429931640625,
-0.1370849609375,
0.308837890625,
-0.6005859375,
-0.1214599609375,
-0.304931640625,
0.73779296875,
0.1915283203125,
-1.2998046875,
-0.0693359375,
-0.9423828125,
0.409423828125,
0.1993408203125,
0.043182373046875,
-0.494873046875,
-0.251953125,
-0.4794921875,
-0.61181640625,
0.52587890625,
0.65869140625,
-0.392822265625,
-0.0096893310546875,
-1.2392578125,
0.671875,
0.2119140625,
0.0286712646484375,
0.269775390625,
0.1844482421875,
0.52392578125,
-0.1405029296875,
0.8974609375,
-0.52392578125,
-0.71240234375,
0.2269287109375,
-1.052734375,
0.58349609375,
-0.7001953125,
0.1820068359375,
-0.323486328125,
-0.09375,
0.01491546630859375,
0.2059326171875,
-0.1258544921875,
0.359619140625,
-0.451171875,
0.0909423828125,
-0.9111328125,
-0.69091796875,
0.73291015625,
-0.5283203125,
-0.30126953125,
0.7255859375,
-0.038970947265625,
0.0478515625,
0.35107421875,
0.6875,
-0.340087890625,
0.2451171875,
-0.467529296875,
0.85888671875,
-0.60009765625,
-0.7509765625,
-0.0261688232421875,
-0.414794921875,
0.8388671875,
-0.11846923828125,
-0.9384765625,
-0.45458984375,
-1.1875,
-0.31201171875,
-0.08099365234375,
-0.374267578125,
0.1766357421875,
0.09124755859375,
-0.23291015625,
0.07867431640625,
-0.26220703125,
-0.2056884765625,
-0.64306640625,
-0.673828125,
-0.16650390625,
0.537109375,
0.29931640625,
0.93505859375,
0.060516357421875,
-0.243408203125,
-0.2178955078125,
0.119384765625,
0.09466552734375,
-0.325439453125,
-0.77392578125,
-0.28466796875,
-0.181884765625,
-0.51416015625,
0.4814453125,
0.2471923828125,
0.31591796875,
0.76025390625,
0.271484375,
0.274658203125,
0.10614013671875,
-0.59033203125,
1.09765625,
0.4091796875,
-1.0869140625,
-0.7412109375,
0.487060546875,
0.1328125,
0.461181640625,
0.42431640625,
-0.80029296875,
-0.59912109375,
-1.26171875,
0.54443359375,
-0.3173828125,
0.2271728515625,
-0.9287109375,
-0.89697265625,
-0.5048828125,
0.373046875,
-0.1627197265625,
-0.363525390625,
-0.006252288818359375,
-1.0625,
0.018280029296875,
-0.7958984375,
-0.2139892578125,
-0.72021484375,
-0.92578125,
-0.1514892578125,
0.73291015625,
0.3095703125,
0.29052734375,
-0.0227203369140625,
-0.2486572265625,
0.71630859375,
-0.130615234375,
-0.83203125,
-0.429931640625,
-0.113037109375,
0.06927490234375,
-0.289794921875,
-0.1429443359375,
-0.50732421875,
0.81396484375,
-0.97216796875,
0.221923828125,
-0.033966064453125,
-0.409912109375,
-0.2325439453125,
-0.379638671875,
1.1962890625,
-0.5029296875,
-0.2265625,
-0.270263671875,
0.77392578125,
0.56201171875,
0.08013916015625,
-0.424560546875,
-0.85595703125,
-0.69287109375,
-0.92431640625,
0.359130859375,
-0.57421875,
0.258056640625,
-0.78466796875,
-0.2078857421875,
1.0185546875,
-0.619140625,
0.302490234375,
1.3046875,
-0.05792236328125,
0.1727294921875,
-0.75341796875,
0.5126953125,
0.5380859375,
-0.6826171875,
-0.08648681640625,
0.091552734375,
-0.7529296875,
0.0220184326171875,
-0.09759521484375,
-1.267578125,
-0.457763671875,
0.453857421875,
1.4775390625,
-0.380126953125,
0.6923828125,
-0.0958251953125,
-1.294921875,
-0.7744140625,
0.9599609375,
0.435302734375,
0.8681640625,
1.0869140625,
-0.349609375,
-0.318115234375,
0.044097900390625,
-0.11749267578125,
-0.5458984375,
-0.422607421875,
1.3779296875,
-0.451904296875,
-0.39453125,
0.7783203125,
0.80908203125,
-1.318359375,
-0.62255859375,
0.1864013671875,
0.139404296875,
-0.64697265625,
-0.5537109375,
0.405029296875,
0.6767578125,
-0.65087890625,
0.190673828125,
0.47314453125,
-0.1497802734375,
0.3974609375,
0.211181640625,
-0.0478515625,
-0.55517578125,
0.560546875,
0.552734375,
-0.490234375,
-0.426025390625,
-1.0380859375,
-0.478759765625,
-0.318115234375,
0.0977783203125,
0.71044921875,
-0.357666015625,
0.383544921875,
0.311767578125,
0.329833984375,
0.58447265625,
-0.3662109375,
-0.5517578125,
0.456298828125,
-0.5048828125,
-0.4462890625,
0.19140625,
0.433349609375,
-0.01537322998046875,
0.51513671875,
-0.53125,
0.06585693359375,
0.34521484375,
0.4765625,
-0.4228515625,
-0.54833984375,
-0.1588134765625,
-1.0166015625,
-0.432861328125,
-0.58740234375,
-0.95458984375,
-0.239013671875,
0.568359375,
0.0067291259765625,
-0.68408203125,
-0.02703857421875,
0.5048828125,
-0.6787109375,
0.89697265625,
-0.50146484375,
0.1790771484375,
-0.544921875,
-0.42138671875,
-0.64208984375,
-0.03619384765625,
-0.62109375,
0.1805419921875,
-0.7822265625,
0.5927734375,
-0.48779296875,
0.4033203125,
-0.01019287109375,
0.8095703125,
-0.320556640625,
-0.826171875,
-0.2227783203125,
0.046966552734375,
0.11077880859375,
0.90283203125,
0.4453125,
0.317626953125,
0.1630859375,
-0.2880859375,
-0.160888671875,
-0.6171875,
0.384033203125,
0.78662109375,
-0.0927734375,
-0.332763671875,
-0.449951171875,
0.26123046875,
-0.92041015625,
0.45263671875,
-0.61376953125,
-0.0904541015625,
0.44384765625,
-0.422119140625,
0.07012939453125,
1.37890625,
-0.148681640625,
0.343017578125,
0.226318359375,
-0.2451171875,
0.422119140625,
0.41796875,
0.1885986328125,
0.37744140625,
-0.09130859375,
-0.385986328125,
-0.048919677734375,
0.1279296875,
0.2027587890625,
0.226318359375,
-0.3896484375,
-0.61572265625,
-1.0908203125,
-0.309326171875,
0.09814453125,
0.36083984375,
0.303466796875,
-0.121826171875,
-0.138916015625,
-0.289794921875,
-0.28369140625,
-0.2100830078125,
-1.21875,
-1.34765625,
0.53173828125,
-0.1644287109375,
-0.76513671875,
-0.40185546875,
-0.334716796875,
-0.1434326171875,
0.01349639892578125,
0.453125,
-0.244873046875,
0.779296875,
-0.1978759765625,
0.6015625,
0.032440185546875,
-0.1986083984375,
0.333740234375,
0.3291015625,
-0.912109375,
-0.64111328125,
0.2261962890625,
1.525390625,
0.157470703125,
-0.275390625,
-0.4111328125,
0.1214599609375,
-0.0654296875,
0.2159423828125,
-0.50732421875,
0.12060546875,
-0.32958984375,
0.2646484375,
-0.685546875,
-0.333984375,
-0.186279296875,
0.491455078125,
0.60498046875,
-0.59033203125,
-0.8193359375,
0.80322265625,
0.6416015625,
-0.244384765625,
0.332275390625,
0.1280517578125,
0.23583984375,
-0.12042236328125,
-0.004039764404296875,
-0.12152099609375,
0.857421875,
0.96875,
0.607421875,
-0.294189453125,
0.406982421875,
0.5537109375,
0.007656097412109375,
-0.01513671875,
0.282470703125,
0.373046875,
0.10125732421875,
-0.19921875,
-0.68408203125,
-0.3720703125,
-0.3720703125,
-0.5400390625,
0.094482421875,
-0.202392578125,
0.033355712890625,
-0.09149169921875,
-0.79296875,
0.95068359375,
-0.11932373046875,
-0.160888671875,
0.31201171875,
-0.462890625,
0.06866455078125,
0.8955078125,
-0.19140625,
-0.4970703125,
0.6484375,
0.8154296875,
0.379638671875,
0.69677734375,
-0.0237274169921875,
0.354248046875,
-0.33056640625,
0.1910400390625,
-0.155517578125,
-0.0081024169921875,
-0.65478515625,
-0.484130859375,
-0.94775390625,
0.1312255859375,
-0.52734375,
-0.290283203125,
0.59423828125,
-0.5146484375,
0.047210693359375,
-0.09136962890625,
0.8779296875,
-0.583984375,
0.40478515625,
0.419189453125,
-0.27001953125,
-0.98193359375,
0.818359375,
-0.287841796875,
0.2255859375,
-0.0302276611328125,
0.87646484375,
-0.057769775390625,
1.404296875,
0.56103515625,
-0.335205078125,
0.10443115234375,
0.68603515625,
-0.15771484375,
-0.61962890625,
-1.0517578125,
-0.1485595703125,
-0.195068359375,
-0.982421875,
-0.0855712890625,
-0.08392333984375,
0.830078125,
-0.08343505859375,
-0.79736328125,
-0.1585693359375,
0.244140625,
-1.203125,
0.953125,
0.370361328125,
0.49267578125,
-0.051910400390625,
0.0178375244140625,
0.0911865234375,
-1.1865234375,
0.36669921875,
-0.83251953125,
0.485107421875,
-0.75146484375,
-0.4736328125,
-1.0380859375,
-0.54443359375,
-0.10858154296875,
-0.63671875,
-0.478271484375,
-1.2431640625,
0.20947265625,
0.60986328125,
0.473388671875,
0.298095703125,
-0.27978515625,
0.3759765625,
1.224609375,
0.79296875,
0.1163330078125,
0.437744140625,
0.51171875,
0.11236572265625,
-0.314208984375,
0.0235595703125,
0.3740234375,
-0.09356689453125,
0.299560546875,
-0.268310546875,
0.0836181640625,
-0.61572265625,
-1.650390625,
0.1798095703125,
0.6044921875,
0.427001953125,
-0.703125,
-0.65966796875,
-0.1160888671875,
-0.75244140625,
-0.38427734375,
-0.4912109375,
0.5712890625,
-0.2314453125,
-0.0249786376953125,
-0.33251953125,
-1.154296875,
4.1640625,
0.9716796875,
1.1318359375,
0.142333984375,
0.455078125,
1.044921875,
0.17919921875,
-0.429443359375,
0.1734619140625,
-0.403076171875,
0.323974609375,
-0.399169921875,
0.1307373046875,
0.63671875,
0.036865234375,
0.78955078125,
-0.433837890625,
0.26025390625,
0.01068878173828125,
-1.1494140625,
-0.97314453125,
0.53076171875,
-0.0966796875,
0.9755859375,
0.259033203125,
0.2327880859375,
0.389404296875,
-0.435546875,
-0.11163330078125,
-0.489501953125,
0.335693359375,
-0.365234375,
0.9072265625,
0.0745849609375,
-0.053802490234375,
0.374267578125,
-0.04412841796875,
-0.267822265625,
-0.0160675048828125,
0.153076171875,
0.11602783203125,
0.1800537109375,
0.63818359375,
-1.1982421875,
-0.247802734375,
0.4716796875,
-0.77978515625,
0.365478515625,
0.223388671875,
-0.95947265625,
0.734375,
-0.326171875,
0.470703125,
-0.59375,
-0.896484375,
-0.05126953125,
-0.0662841796875,
-0.072998046875,
0.046600341796875,
0.67236328125,
0.66796875,
0.880859375,
-0.0634765625,
0.2120361328125,
-1.23828125,
0.68115234375,
0.3388671875,
0.55419921875,
-0.7158203125,
0.08624267578125,
-0.135498046875,
-0.486328125,
-0.2396240234375,
-0.1546630859375,
0.6689453125,
0.47705078125,
-0.308349609375,
0.83251953125,
0.467041015625,
-0.69970703125,
0.1961669921875,
-0.63623046875,
-0.2171630859375,
-0.583984375,
0.70556640625,
1.2099609375,
-0.56201171875,
-0.46826171875,
-0.30419921875,
0.4296875,
0.340087890625,
0.869140625,
0.316650390625,
-0.6962890625,
0.051513671875
] | 0
|
Provide tags and a correct Python 3 solution for this coding contest problem.
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 ≤ 100, 1 ≤ k ≤ 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
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.
Tags: dp, strings
Correct Solution:
```
import os
import sys
from io import BytesIO, IOBase
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# ------------------------------
from math import factorial
from collections import Counter, defaultdict, deque
from heapq import heapify, heappop, heappush
def RL(): return map(int, sys.stdin.readline().rstrip().split())
def RLL(): return list(map(int, sys.stdin.readline().rstrip().split()))
def N(): return int(input())
def comb(n, m): return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0
def perm(n, m): return factorial(n) // (factorial(n - m)) if n >= m else 0
def mdis(x1, y1, x2, y2): return abs(x1 - x2) + abs(y1 - y2)
mod = 998244353
INF = float('inf')
# ------------------------------
def c(chr): return ord(chr)-ord('a')
def main():
n, k = RL()
s = input()
dp = [[0]*(n+1) for i in range(n+1)]
rec = [-1]*26
pre = [-1]*n
for i in range(n+1):
dp[i][0] = 1
for i in range(n):
now = c(s[i])
pre[i] = rec[now]
rec[now] = i
for i in range(1, n+1):
for j in range(1, i+1):
# if j>i: break
dp[i][j] = dp[i-1][j] + dp[i-1][j-1]
# if i == 6 and j == 1: print(dp[i-1][j], dp[i-1][j-1])
if pre[i-1]!=-1:
# if i==5 and j==4: print(pre[i-1], '+++')
# if i==6 and j==1: print(pre[i-1], dp[pre[i-1]][j], dp[i][j])
dp[i][j] -= dp[pre[i-1]][j-1]
res = 0
for j in range(n, -1, -1):
if k:
num = min(k, dp[-1][j])
k-=num
res += (n-j)*num
# for i in dp: print(i)
print(res if k==0 else -1)
if __name__ == "__main__":
main()
```
| 100
|
[
-0.038482666015625,
0.2017822265625,
0.224853515625,
0.42919921875,
-0.1370849609375,
-0.1966552734375,
-0.16259765625,
-0.1715087890625,
0.529296875,
0.77880859375,
0.7158203125,
-0.30224609375,
-0.244873046875,
-0.99169921875,
-0.57373046875,
0.177734375,
-0.662109375,
-0.86279296875,
-0.388427734375,
-0.25732421875,
0.0849609375,
-0.1846923828125,
-1.318359375,
-0.431396484375,
0.20947265625,
1.1123046875,
0.2008056640625,
0.252197265625,
1.14453125,
1.2236328125,
-0.23876953125,
-0.90625,
0.576171875,
-1.091796875,
-0.53271484375,
-0.184814453125,
0.37060546875,
-0.55712890625,
-0.544921875,
-0.239013671875,
0.44970703125,
-0.74609375,
0.63427734375,
-0.44970703125,
-1.234375,
-0.1962890625,
-0.07147216796875,
-0.4677734375,
-0.5517578125,
-0.0034332275390625,
0.035308837890625,
-0.07513427734375,
0.4775390625,
0.04266357421875,
0.061920166015625,
0.340087890625,
0.2763671875,
-0.356201171875,
-0.7021484375,
0.57958984375,
0.25634765625,
-0.1529541015625,
0.07269287109375,
-0.681640625,
-0.46142578125,
0.229248046875,
-0.396484375,
0.13330078125,
-0.04376220703125,
-0.7275390625,
0.0145721435546875,
0.0745849609375,
0.08642578125,
-0.60302734375,
-0.232666015625,
-0.3916015625,
0.2110595703125,
-0.1964111328125,
-0.533203125,
0.6767578125,
0.06756591796875,
0.9150390625,
0.351806640625,
-0.08587646484375,
-0.6826171875,
-0.63525390625,
-0.1148681640625,
-0.0120849609375,
0.287353515625,
0.085693359375,
-0.5078125,
1.0068359375,
-0.67724609375,
-0.04840087890625,
0.443359375,
0.6650390625,
-0.60986328125,
0.2109375,
0.28857421875,
0.09906005859375,
0.48876953125,
0.74951171875,
-0.430419921875,
1.0888671875,
-0.08514404296875,
-0.212158203125,
0.312255859375,
-0.344482421875,
-0.25,
-0.357666015625,
-0.220458984375,
-0.458984375,
0.462890625,
-0.0723876953125,
-0.394287109375,
0.93896484375,
-0.135009765625,
0.52734375,
-0.599609375,
0.368896484375,
0.397216796875,
0.08648681640625,
0.66552734375,
-0.1434326171875,
-0.1822509765625,
-0.427001953125,
-0.552734375,
1.1826171875,
-0.64794921875,
0.43798828125,
0.60888671875,
-0.245849609375,
-0.22998046875,
0.3837890625,
-0.2021484375,
0.705078125,
-0.16259765625,
0.50830078125,
0.69384765625,
-0.564453125,
0.7333984375,
-0.2442626953125,
0.00859832763671875,
1.5380859375,
0.51806640625,
0.234619140625,
0.95361328125,
0.337646484375,
-0.6318359375,
0.464599609375,
-1.05078125,
0.65771484375,
0.34619140625,
0.1627197265625,
-0.318115234375,
0.00807952880859375,
-0.0673828125,
0.52099609375,
0.0614013671875,
0.46923828125,
-0.41357421875,
0.330810546875,
-0.27978515625,
0.426513671875,
-0.65576171875,
0.86962890625,
-0.174072265625,
-0.2685546875,
0.1436767578125,
-0.63818359375,
0.431640625,
-0.149658203125,
-0.1326904296875,
0.68359375,
0.364501953125,
0.89990234375,
1.1376953125,
-0.1031494140625,
0.51806640625,
0.71044921875,
-0.3720703125,
0.283447265625,
0.481201171875,
0.390380859375,
0.97119140625,
0.2137451171875,
0.099365234375,
-0.07427978515625,
-0.69287109375,
-0.720703125,
0.240478515625,
0.7578125,
-0.2481689453125,
0.321533203125,
-0.00823211669921875,
0.0465087890625,
-1.1298828125,
-0.31201171875,
-0.204833984375,
-0.97705078125,
-0.64892578125,
0.53857421875,
0.10858154296875,
0.2076416015625,
-0.4765625,
-0.81396484375,
0.69384765625,
1.15234375,
-0.374267578125,
0.06268310546875,
0.79833984375,
-0.0275726318359375,
-0.42529296875,
0.595703125,
-0.1298828125,
-0.422607421875,
-0.62890625,
0.67333984375,
-0.27099609375,
-0.1959228515625,
-0.253662109375,
0.55419921875,
0.288818359375,
0.57666015625,
0.10394287109375,
-0.1875,
0.08642578125,
1.509765625,
0.314453125,
0.281982421875,
0.133544921875,
0.75,
0.4560546875,
0.55126953125,
0.302001953125,
0.284912109375,
0.419677734375,
1.3212890625,
-0.07391357421875,
0.6103515625,
0.070068359375,
0.367919921875,
0.60888671875,
0.073486328125,
0.09246826171875,
0.446044921875,
0.06256103515625,
0.047576904296875,
-0.67431640625,
-0.038726806640625,
-0.361083984375,
0.73876953125,
0.014678955078125,
0.5791015625,
-0.160400390625,
-0.30615234375,
0.384765625,
1.240234375,
-1.173828125,
-0.56884765625,
-0.2086181640625,
0.0770263671875,
0.34765625,
0.05120849609375,
0.44677734375,
0.453125,
0.427978515625,
0.6806640625,
-0.307373046875,
-0.22802734375,
-0.56298828125,
-0.85986328125,
-0.59521484375,
-0.262451171875,
-0.349853515625,
0.040008544921875,
0.1376953125,
-0.98681640625,
0.374755859375,
-0.61474609375,
-0.00891876220703125,
0.059478759765625,
-0.7333984375,
0.52587890625,
-0.44775390625,
0.55078125,
-0.63818359375,
0.732421875,
0.6767578125,
1.1103515625,
-0.3388671875,
-0.1307373046875,
-0.060302734375,
-1.1015625,
0.265625,
0.21533203125,
0.1639404296875,
-0.059234619140625,
-0.50830078125,
-0.80029296875,
-0.489501953125,
-0.044647216796875,
-0.3828125,
0.1732177734375,
-0.54052734375,
0.5859375,
0.491943359375,
-0.56787109375,
0.6806640625,
0.69189453125,
-0.8203125,
0.24560546875,
-0.01251220703125,
0.22119140625,
-0.8857421875,
1.2080078125,
0.2196044921875,
0.57177734375,
-0.70947265625,
-0.6474609375,
-0.481689453125,
-0.16162109375,
0.301513671875,
-0.53564453125,
-0.2359619140625,
0.5205078125,
0.042144775390625,
-1.28515625,
0.417236328125,
-1.0341796875,
-0.323974609375,
-0.26171875,
-0.060638427734375,
0.488037109375,
0.8720703125,
0.0894775390625,
-0.7890625,
-0.351318359375,
0.05511474609375,
0.998046875,
0.697265625,
-0.75634765625,
0.11993408203125,
0.78369140625,
0.07147216796875,
0.0160675048828125,
0.373291015625,
-0.5615234375,
-0.348388671875,
0.29736328125,
0.1575927734375,
0.9599609375,
0.119140625,
0.55615234375,
0.68603515625,
0.68798828125,
-0.70947265625,
0.167724609375,
-0.264892578125,
0.97607421875,
0.83349609375,
0.436279296875,
0.25732421875,
-0.380126953125,
-0.325439453125,
-0.99755859375,
0.50244140625,
0.091064453125,
0.85400390625,
-0.681640625,
0.7080078125,
0.29736328125,
-0.51318359375,
0.494384765625,
-0.9287109375,
-0.18115234375,
1.224609375,
-0.421630859375,
0.66796875,
-0.411865234375,
0.31103515625,
-0.1268310546875,
0.1572265625,
0.2470703125,
0.84326171875,
0.60107421875,
-0.446044921875,
-0.405517578125,
-0.5166015625,
-0.04510498046875,
0.11126708984375,
-0.4677734375,
0.323486328125,
-0.1751708984375,
-0.235107421875,
-0.8232421875,
-0.031219482421875,
1.1904296875,
0.78369140625,
0.0587158203125,
0.023895263671875,
0.08441162109375,
0.26708984375,
0.6328125,
-0.57763671875,
0.055145263671875,
-0.568359375,
0.95458984375,
0.1329345703125,
0.00797271728515625,
-0.08050537109375,
-0.2283935546875,
-0.1475830078125,
0.133056640625,
-0.02630615234375,
-0.341552734375,
-1.0419921875,
0.0760498046875,
-0.09344482421875,
0.291259765625,
-0.6357421875,
-0.018341064453125,
-0.39013671875,
0.7314453125,
0.11407470703125,
-1.228515625,
-0.07415771484375,
-0.8798828125,
0.4013671875,
0.185791015625,
0.058746337890625,
-0.46923828125,
-0.231689453125,
-0.462158203125,
-0.5830078125,
0.57421875,
0.556640625,
-0.2322998046875,
0.0215606689453125,
-1.2197265625,
0.66650390625,
0.214111328125,
0.01288604736328125,
0.322509765625,
0.155517578125,
0.5419921875,
-0.10040283203125,
0.80908203125,
-0.5263671875,
-0.66455078125,
0.2529296875,
-1.072265625,
0.638671875,
-0.69921875,
0.189208984375,
-0.3935546875,
-0.145751953125,
0.0086212158203125,
0.2288818359375,
-0.08099365234375,
0.307861328125,
-0.53564453125,
0.1534423828125,
-0.92578125,
-0.783203125,
0.7392578125,
-0.48046875,
-0.36962890625,
0.7138671875,
0.08868408203125,
-0.011322021484375,
0.3935546875,
0.77587890625,
-0.285888671875,
0.325927734375,
-0.4384765625,
0.78564453125,
-0.61083984375,
-0.74853515625,
-0.1376953125,
-0.3740234375,
0.78515625,
-0.02825927734375,
-0.9853515625,
-0.35595703125,
-1.146484375,
-0.400634765625,
-0.03668212890625,
-0.26708984375,
0.2332763671875,
0.0572509765625,
-0.229736328125,
0.07916259765625,
-0.263671875,
-0.287841796875,
-0.68359375,
-0.677734375,
-0.268798828125,
0.6416015625,
0.335205078125,
0.9462890625,
0.07427978515625,
-0.16796875,
-0.25244140625,
0.01300811767578125,
-0.037567138671875,
-0.351806640625,
-0.79248046875,
-0.216552734375,
-0.20263671875,
-0.52587890625,
0.5205078125,
0.237060546875,
0.344482421875,
0.74560546875,
0.28955078125,
0.32080078125,
0.1502685546875,
-0.60791015625,
1.0498046875,
0.3583984375,
-0.95751953125,
-0.724609375,
0.459228515625,
0.0295867919921875,
0.3974609375,
0.289306640625,
-0.82373046875,
-0.64404296875,
-1.3310546875,
0.556640625,
-0.25537109375,
0.198974609375,
-1.0185546875,
-0.8544921875,
-0.5,
0.275146484375,
-0.11041259765625,
-0.434326171875,
0.036041259765625,
-1.0537109375,
0.055816650390625,
-0.86328125,
-0.223388671875,
-0.787109375,
-0.90771484375,
-0.114501953125,
0.66259765625,
0.328369140625,
0.30419921875,
0.006130218505859375,
-0.1995849609375,
0.62158203125,
-0.11669921875,
-0.85986328125,
-0.453857421875,
-0.129638671875,
0.058380126953125,
-0.30419921875,
-0.0036640167236328125,
-0.47607421875,
0.7353515625,
-0.88720703125,
0.20556640625,
0.04180908203125,
-0.36572265625,
-0.2037353515625,
-0.32177734375,
1.236328125,
-0.56689453125,
-0.2149658203125,
-0.2154541015625,
0.81591796875,
0.49267578125,
0.049957275390625,
-0.5107421875,
-0.81201171875,
-0.62548828125,
-0.998046875,
0.2315673828125,
-0.6220703125,
0.2401123046875,
-0.77001953125,
-0.1829833984375,
1.0068359375,
-0.5712890625,
0.361328125,
1.2177734375,
-0.10870361328125,
0.143798828125,
-0.76806640625,
0.66357421875,
0.4775390625,
-0.6171875,
-0.027313232421875,
0.12176513671875,
-0.74365234375,
0.04534912109375,
-0.157958984375,
-1.255859375,
-0.4560546875,
0.3984375,
1.505859375,
-0.358154296875,
0.8134765625,
-0.0810546875,
-1.1923828125,
-0.70458984375,
0.89111328125,
0.396240234375,
0.7822265625,
1.0263671875,
-0.370849609375,
-0.2069091796875,
0.10906982421875,
-0.0701904296875,
-0.517578125,
-0.447998046875,
1.337890625,
-0.390625,
-0.398193359375,
0.80224609375,
0.84521484375,
-1.3046875,
-0.72265625,
0.1336669921875,
0.2144775390625,
-0.544921875,
-0.50927734375,
0.27685546875,
0.728515625,
-0.62548828125,
0.1619873046875,
0.406494140625,
-0.1756591796875,
0.453125,
0.1842041015625,
-0.026153564453125,
-0.59765625,
0.498779296875,
0.55615234375,
-0.468505859375,
-0.489990234375,
-0.99951171875,
-0.467529296875,
-0.373779296875,
0.120361328125,
0.69287109375,
-0.33154296875,
0.362060546875,
0.322265625,
0.30517578125,
0.46337890625,
-0.2340087890625,
-0.5263671875,
0.3828125,
-0.6025390625,
-0.387451171875,
0.29931640625,
0.431640625,
-0.032867431640625,
0.50390625,
-0.57421875,
0.05322265625,
0.4130859375,
0.482666015625,
-0.389892578125,
-0.59912109375,
-0.1558837890625,
-0.935546875,
-0.35986328125,
-0.50732421875,
-0.98828125,
-0.1923828125,
0.54052734375,
0.008636474609375,
-0.77978515625,
0.001979827880859375,
0.52734375,
-0.5341796875,
0.765625,
-0.454833984375,
0.19140625,
-0.46337890625,
-0.379638671875,
-0.69287109375,
-0.0302276611328125,
-0.5771484375,
0.1466064453125,
-0.8447265625,
0.53466796875,
-0.439697265625,
0.475341796875,
0.03704833984375,
0.8408203125,
-0.329345703125,
-0.75927734375,
-0.335205078125,
0.08941650390625,
0.052886962890625,
1.009765625,
0.40087890625,
0.261474609375,
0.0892333984375,
-0.2744140625,
-0.05987548828125,
-0.5771484375,
0.34912109375,
0.73486328125,
-0.1396484375,
-0.37158203125,
-0.487060546875,
0.20703125,
-0.87939453125,
0.36181640625,
-0.6376953125,
-0.0309295654296875,
0.3740234375,
-0.38330078125,
0.0604248046875,
1.283203125,
-0.180419921875,
0.283447265625,
0.093505859375,
-0.222412109375,
0.39404296875,
0.345458984375,
0.1729736328125,
0.3837890625,
-0.11529541015625,
-0.42626953125,
-0.1044921875,
0.22802734375,
0.161865234375,
0.220947265625,
-0.4111328125,
-0.46240234375,
-1.091796875,
-0.225341796875,
0.1513671875,
0.325927734375,
0.275146484375,
-0.2305908203125,
-0.203369140625,
-0.19091796875,
-0.40234375,
-0.1591796875,
-1.1328125,
-1.3505859375,
0.458984375,
-0.21142578125,
-0.72021484375,
-0.302978515625,
-0.3916015625,
-0.162109375,
-0.031951904296875,
0.341552734375,
-0.28857421875,
0.712890625,
-0.1640625,
0.465087890625,
-0.047088623046875,
-0.295166015625,
0.291259765625,
0.319091796875,
-0.927734375,
-0.68505859375,
0.268798828125,
1.5712890625,
0.1302490234375,
-0.2265625,
-0.4755859375,
0.08734130859375,
-0.03155517578125,
0.271240234375,
-0.484375,
0.1513671875,
-0.31396484375,
0.3125,
-0.66015625,
-0.33154296875,
-0.202392578125,
0.364990234375,
0.5751953125,
-0.5771484375,
-0.8154296875,
0.76611328125,
0.625,
-0.261962890625,
0.289306640625,
0.066162109375,
0.2083740234375,
-0.16455078125,
-0.03759765625,
-0.156494140625,
0.90087890625,
0.9140625,
0.6044921875,
-0.263427734375,
0.45458984375,
0.57958984375,
-0.08258056640625,
0.01494598388671875,
0.2083740234375,
0.40283203125,
0.0208740234375,
-0.1812744140625,
-0.7001953125,
-0.392822265625,
-0.395751953125,
-0.58056640625,
0.1790771484375,
-0.2113037109375,
0.054962158203125,
-0.08123779296875,
-0.75830078125,
0.88427734375,
-0.132568359375,
-0.2322998046875,
0.394287109375,
-0.51025390625,
0.20556640625,
0.83203125,
-0.1859130859375,
-0.47509765625,
0.70263671875,
0.76708984375,
0.357666015625,
0.708984375,
0.0518798828125,
0.47412109375,
-0.3017578125,
0.1422119140625,
-0.192626953125,
0.01195526123046875,
-0.65380859375,
-0.35205078125,
-0.927734375,
0.09747314453125,
-0.486328125,
-0.3193359375,
0.59912109375,
-0.42041015625,
0.04296875,
-0.09442138671875,
0.7607421875,
-0.59814453125,
0.450439453125,
0.497802734375,
-0.2308349609375,
-1.0029296875,
0.83349609375,
-0.226318359375,
0.212646484375,
0.0699462890625,
0.83544921875,
0.00873565673828125,
1.423828125,
0.46826171875,
-0.375244140625,
0.0288543701171875,
0.55908203125,
-0.213134765625,
-0.669921875,
-0.95556640625,
-0.14501953125,
-0.1800537109375,
-0.88427734375,
-0.1563720703125,
0.018768310546875,
0.75390625,
-0.044830322265625,
-0.8330078125,
-0.01531219482421875,
0.20556640625,
-1.1884765625,
0.9296875,
0.3603515625,
0.541015625,
-0.021392822265625,
0.042938232421875,
0.189453125,
-1.2080078125,
0.368896484375,
-0.78662109375,
0.60791015625,
-0.72021484375,
-0.43212890625,
-1.0546875,
-0.51318359375,
-0.119873046875,
-0.59814453125,
-0.347900390625,
-1.197265625,
0.2880859375,
0.703125,
0.432861328125,
0.330810546875,
-0.24365234375,
0.465087890625,
1.275390625,
0.87548828125,
0.09552001953125,
0.447509765625,
0.454345703125,
0.1395263671875,
-0.276611328125,
0.1785888671875,
0.35498046875,
-0.054351806640625,
0.2064208984375,
-0.235107421875,
0.12646484375,
-0.615234375,
-1.5625,
0.21875,
0.515625,
0.406982421875,
-0.720703125,
-0.7080078125,
-0.1392822265625,
-0.86767578125,
-0.43359375,
-0.62451171875,
0.533203125,
-0.28857421875,
0.04229736328125,
-0.34716796875,
-1.0625,
4.23828125,
1.009765625,
1.1796875,
0.2298583984375,
0.4140625,
1.0517578125,
0.15576171875,
-0.484375,
0.125732421875,
-0.379150390625,
0.31396484375,
-0.452880859375,
0.1368408203125,
0.57861328125,
0.1063232421875,
0.7578125,
-0.37841796875,
0.197021484375,
0.01474761962890625,
-1.044921875,
-0.97412109375,
0.467529296875,
0.01377105712890625,
0.94775390625,
0.307373046875,
0.2430419921875,
0.410888671875,
-0.38623046875,
-0.104736328125,
-0.49609375,
0.341064453125,
-0.3955078125,
0.8544921875,
0.0252838134765625,
0.007404327392578125,
0.41064453125,
-0.0640869140625,
-0.25048828125,
0.0526123046875,
0.191162109375,
0.01558685302734375,
0.2080078125,
0.5244140625,
-1.181640625,
-0.220947265625,
0.44189453125,
-0.72900390625,
0.46142578125,
0.2398681640625,
-1.0146484375,
0.693359375,
-0.30615234375,
0.4521484375,
-0.54345703125,
-0.8486328125,
-0.039276123046875,
-0.057373046875,
-0.052703857421875,
-0.054229736328125,
0.669921875,
0.72900390625,
0.90576171875,
-0.1585693359375,
0.126220703125,
-1.19140625,
0.61572265625,
0.357666015625,
0.7138671875,
-0.75048828125,
0.020111083984375,
-0.222412109375,
-0.424560546875,
-0.2108154296875,
-0.11865234375,
0.654296875,
0.32177734375,
-0.34814453125,
0.85009765625,
0.40966796875,
-0.640625,
0.23193359375,
-0.65576171875,
-0.251220703125,
-0.59912109375,
0.66943359375,
1.3369140625,
-0.59619140625,
-0.462890625,
-0.365478515625,
0.430419921875,
0.365234375,
0.8349609375,
0.330322265625,
-0.7431640625,
0.07025146484375
] | 0
|
Provide tags and a correct Python 3 solution for this coding contest problem.
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 ≤ 100, 1 ≤ k ≤ 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
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.
Tags: dp, strings
Correct Solution:
```
from sys import stdin
n, k = [int(i) for i in stdin.readline().strip().split()]
s = stdin.readline().strip()
def get_f(s):
F = [len(s) * [0] for _ in range((len(s) + 1))]
size = 0
for index in range(len(s)):
F[size][index] = 1
F[1][0] = 1
p = {s[0]: 0}
for i in range(1, len(s)):
for k in range(1, len(s) + 1):
if k > i + 1:
val = 0
else:
val = F[k][i - 1] + F[k - 1][i - 1]
if s[i] in p:
# sic: if k - 1 == 0 then answer is always 1
# if k - 1 == 0:
# print(i)
# val -= 1
if p[s[i]] - 1 >= 0:
val -= F[k - 1][p[s[i]] - 1]
elif p[s[i]] == 0 and k - 1 == 0:
val -= 1
F[k][i] = val
p[s[i]] = i
return F
_F = get_f(s)
# for sz in range(len(s) + 1):
# for index in range(len(s)):
# print(_F[sz][index], end=' ')
# print()
def count(size, index = None):
return _F[size][index or (len(s) - 1)]
# recursive solution with memoization
previous_letter_index = {}
found = {}
for index, letter in enumerate(s):
if letter in found:
previous_letter_index[(letter, index)] = found[letter]
found[letter] = index
_subsequences = {}
def subsequences(size, index):
"""Get number of distinct subsequences of given size in sequence[0:index + 1] slice"""
if (size, index) not in _subsequences:
if size == 0:
res = 1
elif size > index + 1:
res = 0
else:
res = subsequences(size, index - 1) + subsequences(size - 1, index - 1)
letter = s[index]
if (letter, index) in previous_letter_index:
res -= subsequences(size - 1, previous_letter_index[(letter, index)] - 1)
_subsequences[(size, index)] = res
return _subsequences[(size, index)]
total_cost = 0
for size in range(len(s), -1, -1):
if k == 0:
break
step_cost = n - size
sequence_count = count(size, len(s) - 1)
# print('size = ', size, 'count = ', sequence_count)
if sequence_count > k:
sequence_count = k
total_cost += step_cost * sequence_count
k -= sequence_count
if k == 0:
print(total_cost)
else:
print(-1)
```
| 101
|
[
-0.05987548828125,
0.28125,
0.308837890625,
0.34423828125,
-0.1097412109375,
-0.177978515625,
-0.2120361328125,
-0.18603515625,
0.425048828125,
0.75537109375,
0.689453125,
-0.309814453125,
-0.264404296875,
-0.97412109375,
-0.5927734375,
0.1285400390625,
-0.63427734375,
-0.83447265625,
-0.439697265625,
-0.2744140625,
0.1053466796875,
-0.122314453125,
-1.328125,
-0.406982421875,
0.2474365234375,
1.078125,
0.267333984375,
0.225830078125,
1.095703125,
1.310546875,
-0.2412109375,
-0.89794921875,
0.487548828125,
-1.0927734375,
-0.472900390625,
-0.1640625,
0.34326171875,
-0.58935546875,
-0.487060546875,
-0.1337890625,
0.378662109375,
-0.67578125,
0.6083984375,
-0.54931640625,
-1.1982421875,
-0.168701171875,
-0.1346435546875,
-0.383056640625,
-0.50048828125,
-0.080078125,
0.08367919921875,
-0.055999755859375,
0.479736328125,
0.0286865234375,
0.06524658203125,
0.332763671875,
0.231689453125,
-0.312255859375,
-0.7392578125,
0.57666015625,
0.234130859375,
-0.11285400390625,
0.1029052734375,
-0.5673828125,
-0.47607421875,
0.26025390625,
-0.427978515625,
0.151123046875,
0.0224609375,
-0.7314453125,
-0.04766845703125,
0.057342529296875,
-0.055328369140625,
-0.66259765625,
-0.252197265625,
-0.438232421875,
0.1905517578125,
-0.1707763671875,
-0.576171875,
0.650390625,
0.03924560546875,
0.96533203125,
0.293701171875,
-0.01522064208984375,
-0.67236328125,
-0.61767578125,
-0.06683349609375,
0.08660888671875,
0.31396484375,
0.09967041015625,
-0.55810546875,
0.96484375,
-0.71435546875,
-0.052337646484375,
0.3505859375,
0.62548828125,
-0.5732421875,
0.188232421875,
0.295166015625,
0.09405517578125,
0.435791015625,
0.736328125,
-0.5283203125,
1.1484375,
-0.058319091796875,
-0.2188720703125,
0.27734375,
-0.3740234375,
-0.265380859375,
-0.3037109375,
-0.1695556640625,
-0.45458984375,
0.40478515625,
-0.07733154296875,
-0.47314453125,
0.99365234375,
-0.09722900390625,
0.55859375,
-0.64306640625,
0.43212890625,
0.52783203125,
0.07989501953125,
0.6083984375,
-0.1785888671875,
-0.1307373046875,
-0.372802734375,
-0.51513671875,
1.125,
-0.5927734375,
0.34033203125,
0.5830078125,
-0.25390625,
-0.250244140625,
0.31494140625,
-0.1951904296875,
0.7568359375,
-0.2354736328125,
0.50830078125,
0.58154296875,
-0.5478515625,
0.70947265625,
-0.243896484375,
-0.061492919921875,
1.6005859375,
0.60107421875,
0.27978515625,
1.037109375,
0.361328125,
-0.50390625,
0.427734375,
-1.0224609375,
0.7392578125,
0.38916015625,
0.12115478515625,
-0.279052734375,
-0.07989501953125,
-0.09375,
0.4912109375,
0.0267791748046875,
0.5263671875,
-0.35888671875,
0.35693359375,
-0.2445068359375,
0.40234375,
-0.58642578125,
0.890625,
-0.1483154296875,
-0.33984375,
0.04681396484375,
-0.69140625,
0.44970703125,
-0.162841796875,
-0.134765625,
0.68017578125,
0.401611328125,
0.98486328125,
1.1279296875,
-0.053680419921875,
0.49462890625,
0.701171875,
-0.408447265625,
0.277099609375,
0.40869140625,
0.485595703125,
0.93212890625,
0.2137451171875,
0.1658935546875,
-0.047332763671875,
-0.7705078125,
-0.7177734375,
0.254150390625,
0.7314453125,
-0.248779296875,
0.25732421875,
0.0257568359375,
0.0245819091796875,
-1.1328125,
-0.2587890625,
-0.1478271484375,
-0.89794921875,
-0.66943359375,
0.548828125,
0.11578369140625,
0.201904296875,
-0.56640625,
-0.80224609375,
0.73828125,
1.1787109375,
-0.44384765625,
-0.025421142578125,
0.7490234375,
0.090087890625,
-0.415771484375,
0.470703125,
-0.1063232421875,
-0.407958984375,
-0.6728515625,
0.6162109375,
-0.263427734375,
-0.22607421875,
-0.30029296875,
0.595703125,
0.272216796875,
0.60791015625,
0.05419921875,
-0.184326171875,
0.1522216796875,
1.4970703125,
0.312255859375,
0.29931640625,
0.140625,
0.689453125,
0.47216796875,
0.587890625,
0.26806640625,
0.24609375,
0.41455078125,
1.359375,
-0.00751495361328125,
0.640625,
0.1380615234375,
0.317626953125,
0.607421875,
0.06756591796875,
0.1239013671875,
0.450439453125,
0.006282806396484375,
0.00765228271484375,
-0.703125,
-0.04718017578125,
-0.42333984375,
0.7158203125,
0.08648681640625,
0.505859375,
-0.1915283203125,
-0.33935546875,
0.453369140625,
1.2578125,
-1.1826171875,
-0.56884765625,
-0.249755859375,
0.04278564453125,
0.380126953125,
0.01016998291015625,
0.521484375,
0.3935546875,
0.395263671875,
0.59521484375,
-0.298095703125,
-0.204345703125,
-0.515625,
-0.8984375,
-0.576171875,
-0.2017822265625,
-0.488037109375,
-0.0214385986328125,
0.14501953125,
-0.95556640625,
0.321044921875,
-0.61767578125,
0.02154541015625,
0.039459228515625,
-0.693359375,
0.499755859375,
-0.436279296875,
0.450927734375,
-0.71630859375,
0.755859375,
0.7294921875,
1.1396484375,
-0.40380859375,
-0.10260009765625,
-0.005786895751953125,
-1.078125,
0.233642578125,
0.1551513671875,
0.1573486328125,
-0.10968017578125,
-0.480712890625,
-0.76953125,
-0.572265625,
0.039215087890625,
-0.396728515625,
0.18896484375,
-0.458984375,
0.55517578125,
0.55029296875,
-0.556640625,
0.69287109375,
0.7421875,
-0.87939453125,
0.09661865234375,
-0.0205230712890625,
0.09747314453125,
-0.95263671875,
1.2490234375,
0.317626953125,
0.60205078125,
-0.640625,
-0.5859375,
-0.491943359375,
-0.0631103515625,
0.330078125,
-0.474365234375,
-0.1700439453125,
0.5126953125,
0.08673095703125,
-1.271484375,
0.392578125,
-1.095703125,
-0.293212890625,
-0.2171630859375,
-0.087158203125,
0.62109375,
0.853515625,
0.08251953125,
-0.78173828125,
-0.43017578125,
0.1160888671875,
1.0751953125,
0.7470703125,
-0.7734375,
0.10272216796875,
0.77880859375,
0.1766357421875,
-0.0025081634521484375,
0.31787109375,
-0.5439453125,
-0.333740234375,
0.2120361328125,
0.1651611328125,
0.86669921875,
0.142822265625,
0.55322265625,
0.734375,
0.66162109375,
-0.75537109375,
0.0916748046875,
-0.253173828125,
1.0478515625,
0.83349609375,
0.403564453125,
0.2880859375,
-0.397216796875,
-0.416015625,
-1.009765625,
0.5146484375,
0.05316162109375,
0.837890625,
-0.6748046875,
0.66259765625,
0.314208984375,
-0.50439453125,
0.56396484375,
-0.93798828125,
-0.171875,
1.2001953125,
-0.371826171875,
0.62939453125,
-0.40966796875,
0.27978515625,
-0.133056640625,
0.1365966796875,
0.2484130859375,
0.8486328125,
0.61572265625,
-0.4658203125,
-0.39892578125,
-0.56396484375,
-0.019378662109375,
0.0831298828125,
-0.62109375,
0.32177734375,
-0.1439208984375,
-0.31005859375,
-0.7841796875,
-0.03668212890625,
1.05078125,
0.74462890625,
0.060455322265625,
0.08599853515625,
0.157958984375,
0.37060546875,
0.615234375,
-0.54736328125,
0.0892333984375,
-0.5380859375,
1.0107421875,
0.08489990234375,
-0.01953125,
-0.123291015625,
-0.1962890625,
-0.1732177734375,
0.2568359375,
-0.0227508544921875,
-0.381591796875,
-1.0126953125,
0.0704345703125,
-0.1417236328125,
0.266357421875,
-0.6123046875,
-0.091552734375,
-0.3388671875,
0.66259765625,
0.1668701171875,
-1.2685546875,
-0.08697509765625,
-0.9931640625,
0.460693359375,
0.1719970703125,
0.036956787109375,
-0.457763671875,
-0.25732421875,
-0.4072265625,
-0.65673828125,
0.5146484375,
0.59521484375,
-0.28466796875,
-0.019439697265625,
-1.2314453125,
0.66357421875,
0.2098388671875,
0.09619140625,
0.28515625,
0.1705322265625,
0.4990234375,
-0.1416015625,
0.86767578125,
-0.53564453125,
-0.70068359375,
0.1661376953125,
-1.10546875,
0.63037109375,
-0.65576171875,
0.1988525390625,
-0.289794921875,
-0.128662109375,
-0.005054473876953125,
0.212890625,
-0.1702880859375,
0.313232421875,
-0.4677734375,
0.045166015625,
-0.8984375,
-0.78955078125,
0.70654296875,
-0.5166015625,
-0.3212890625,
0.76708984375,
-0.0185394287109375,
0.0745849609375,
0.36328125,
0.76123046875,
-0.25634765625,
0.32275390625,
-0.444091796875,
0.83642578125,
-0.52880859375,
-0.76318359375,
-0.1175537109375,
-0.3251953125,
0.81005859375,
-0.05853271484375,
-0.94775390625,
-0.39599609375,
-1.13671875,
-0.26513671875,
-0.0229034423828125,
-0.315185546875,
0.1614990234375,
0.132568359375,
-0.22119140625,
0.09906005859375,
-0.27880859375,
-0.15283203125,
-0.6572265625,
-0.75927734375,
-0.2044677734375,
0.5263671875,
0.2783203125,
0.99853515625,
0.032196044921875,
-0.221923828125,
-0.23876953125,
0.1285400390625,
0.0203704833984375,
-0.36474609375,
-0.78662109375,
-0.2607421875,
-0.248779296875,
-0.52392578125,
0.47021484375,
0.194580078125,
0.336181640625,
0.748046875,
0.307373046875,
0.238525390625,
0.162109375,
-0.51220703125,
1.0751953125,
0.408935546875,
-1.048828125,
-0.68505859375,
0.44775390625,
0.08447265625,
0.360107421875,
0.3837890625,
-0.85693359375,
-0.599609375,
-1.29296875,
0.51611328125,
-0.3134765625,
0.1466064453125,
-0.99658203125,
-0.953125,
-0.5,
0.3388671875,
-0.203857421875,
-0.45703125,
-0.0031681060791015625,
-1.056640625,
0.0233306884765625,
-0.833984375,
-0.2100830078125,
-0.7841796875,
-0.9375,
-0.1123046875,
0.72509765625,
0.2958984375,
0.289794921875,
-0.047882080078125,
-0.2373046875,
0.712890625,
-0.06072998046875,
-0.84814453125,
-0.466796875,
-0.09906005859375,
0.1375732421875,
-0.267578125,
-0.055145263671875,
-0.5205078125,
0.7919921875,
-0.9736328125,
0.2412109375,
-0.00885009765625,
-0.441650390625,
-0.1937255859375,
-0.359619140625,
1.2109375,
-0.51953125,
-0.2088623046875,
-0.28955078125,
0.8408203125,
0.5986328125,
0.044158935546875,
-0.399658203125,
-0.81005859375,
-0.677734375,
-0.90576171875,
0.302734375,
-0.59521484375,
0.238525390625,
-0.80224609375,
-0.1962890625,
0.9755859375,
-0.66259765625,
0.318603515625,
1.2890625,
-0.10150146484375,
0.1746826171875,
-0.7421875,
0.59228515625,
0.50244140625,
-0.68359375,
-0.07110595703125,
0.11126708984375,
-0.70703125,
0.043304443359375,
-0.1209716796875,
-1.22265625,
-0.46728515625,
0.429443359375,
1.5263671875,
-0.3837890625,
0.7705078125,
-0.15869140625,
-1.234375,
-0.7255859375,
0.9638671875,
0.428466796875,
0.88720703125,
1.099609375,
-0.322509765625,
-0.234619140625,
0.09375,
-0.11749267578125,
-0.52099609375,
-0.4140625,
1.3134765625,
-0.465087890625,
-0.354248046875,
0.7646484375,
0.81884765625,
-1.298828125,
-0.6591796875,
0.182373046875,
0.102294921875,
-0.6162109375,
-0.54345703125,
0.374755859375,
0.6943359375,
-0.5888671875,
0.1875,
0.45654296875,
-0.136962890625,
0.396484375,
0.22021484375,
-0.0248565673828125,
-0.52197265625,
0.5947265625,
0.5703125,
-0.450927734375,
-0.468994140625,
-1.0595703125,
-0.466552734375,
-0.337890625,
0.12139892578125,
0.734375,
-0.3037109375,
0.426025390625,
0.371337890625,
0.33935546875,
0.60400390625,
-0.312255859375,
-0.572265625,
0.41455078125,
-0.5390625,
-0.4453125,
0.3037109375,
0.430908203125,
-0.038604736328125,
0.54443359375,
-0.56298828125,
0.034912109375,
0.327880859375,
0.51806640625,
-0.421875,
-0.52197265625,
-0.12432861328125,
-0.9765625,
-0.44140625,
-0.53515625,
-0.92138671875,
-0.2296142578125,
0.556640625,
-0.00740814208984375,
-0.74462890625,
-0.04248046875,
0.475830078125,
-0.6572265625,
0.86767578125,
-0.46826171875,
0.2060546875,
-0.490234375,
-0.455078125,
-0.68603515625,
-0.04193115234375,
-0.6669921875,
0.1551513671875,
-0.7607421875,
0.51513671875,
-0.498291015625,
0.393310546875,
0.019195556640625,
0.80078125,
-0.302001953125,
-0.85693359375,
-0.2442626953125,
0.1119384765625,
0.06982421875,
0.94482421875,
0.47216796875,
0.260009765625,
0.148681640625,
-0.2548828125,
-0.1005859375,
-0.58203125,
0.39111328125,
0.77392578125,
-0.07598876953125,
-0.30029296875,
-0.499267578125,
0.2125244140625,
-0.89111328125,
0.42919921875,
-0.60791015625,
-0.0288543701171875,
0.399169921875,
-0.45751953125,
0.09405517578125,
1.3037109375,
-0.120361328125,
0.299072265625,
0.2005615234375,
-0.1905517578125,
0.38720703125,
0.347412109375,
0.138427734375,
0.378662109375,
-0.0714111328125,
-0.3193359375,
-0.0888671875,
0.154052734375,
0.2078857421875,
0.213623046875,
-0.40576171875,
-0.56005859375,
-1.1337890625,
-0.280517578125,
0.09625244140625,
0.299560546875,
0.3681640625,
-0.214599609375,
-0.193603515625,
-0.232177734375,
-0.338134765625,
-0.2178955078125,
-1.12109375,
-1.3818359375,
0.455322265625,
-0.258544921875,
-0.76904296875,
-0.37353515625,
-0.391357421875,
-0.193359375,
-0.018951416015625,
0.4140625,
-0.23388671875,
0.759765625,
-0.1689453125,
0.572265625,
0.02264404296875,
-0.175537109375,
0.2412109375,
0.28173828125,
-0.92529296875,
-0.65625,
0.230224609375,
1.509765625,
0.18212890625,
-0.33935546875,
-0.47705078125,
0.11053466796875,
-0.0587158203125,
0.221435546875,
-0.515625,
0.1480712890625,
-0.288818359375,
0.28369140625,
-0.68115234375,
-0.3603515625,
-0.236328125,
0.44873046875,
0.54443359375,
-0.54833984375,
-0.75830078125,
0.8046875,
0.63134765625,
-0.2325439453125,
0.321533203125,
0.1258544921875,
0.2490234375,
-0.12017822265625,
-0.0102996826171875,
-0.1551513671875,
0.7939453125,
0.94921875,
0.6318359375,
-0.291748046875,
0.396484375,
0.5458984375,
-0.061614990234375,
-0.01154327392578125,
0.1680908203125,
0.354736328125,
0.058563232421875,
-0.1514892578125,
-0.64892578125,
-0.3427734375,
-0.376220703125,
-0.58544921875,
0.1373291015625,
-0.21875,
0.05792236328125,
-0.1656494140625,
-0.7880859375,
0.92333984375,
-0.137451171875,
-0.1260986328125,
0.360595703125,
-0.458251953125,
0.149169921875,
0.88330078125,
-0.1553955078125,
-0.45263671875,
0.64990234375,
0.82958984375,
0.44189453125,
0.63818359375,
0.060211181640625,
0.41845703125,
-0.2890625,
0.2447509765625,
-0.181640625,
0.0255126953125,
-0.62548828125,
-0.452392578125,
-0.9462890625,
0.1429443359375,
-0.53271484375,
-0.322265625,
0.587890625,
-0.458984375,
0.0794677734375,
-0.04718017578125,
0.87451171875,
-0.59765625,
0.39990234375,
0.450439453125,
-0.296142578125,
-0.96044921875,
0.80517578125,
-0.2452392578125,
0.26513671875,
-0.00905609130859375,
0.8486328125,
-0.089599609375,
1.3974609375,
0.5341796875,
-0.37646484375,
0.10699462890625,
0.58447265625,
-0.1544189453125,
-0.609375,
-1.0400390625,
-0.08807373046875,
-0.149658203125,
-0.9716796875,
-0.1129150390625,
-0.049774169921875,
0.7900390625,
-0.054046630859375,
-0.78369140625,
-0.10028076171875,
0.26318359375,
-1.1767578125,
0.92333984375,
0.374755859375,
0.5712890625,
-0.055145263671875,
-0.0208892822265625,
0.1348876953125,
-1.1787109375,
0.3046875,
-0.79443359375,
0.58984375,
-0.71533203125,
-0.472900390625,
-1.0205078125,
-0.466064453125,
-0.09716796875,
-0.55859375,
-0.41748046875,
-1.15625,
0.262939453125,
0.60498046875,
0.47119140625,
0.346923828125,
-0.272216796875,
0.380126953125,
1.1884765625,
0.77490234375,
0.1114501953125,
0.447021484375,
0.483642578125,
0.035003662109375,
-0.33740234375,
0.09393310546875,
0.365478515625,
-0.072265625,
0.2017822265625,
-0.285400390625,
0.0787353515625,
-0.63134765625,
-1.6142578125,
0.1697998046875,
0.619140625,
0.42529296875,
-0.6748046875,
-0.671875,
-0.10919189453125,
-0.8291015625,
-0.392578125,
-0.5244140625,
0.55029296875,
-0.2239990234375,
-0.00016999244689941406,
-0.30078125,
-1.142578125,
4.22265625,
0.97021484375,
1.1552734375,
0.1578369140625,
0.419189453125,
1.0556640625,
0.11492919921875,
-0.49169921875,
0.1468505859375,
-0.394287109375,
0.34521484375,
-0.444580078125,
0.1668701171875,
0.63623046875,
0.050933837890625,
0.76904296875,
-0.442626953125,
0.2264404296875,
-0.0141448974609375,
-1.1142578125,
-0.96240234375,
0.54248046875,
-0.09033203125,
0.9755859375,
0.2464599609375,
0.2744140625,
0.3564453125,
-0.42529296875,
-0.106201171875,
-0.497314453125,
0.375,
-0.37353515625,
0.87548828125,
0.0211639404296875,
-0.05169677734375,
0.41552734375,
-0.0777587890625,
-0.243896484375,
-0.0085601806640625,
0.138671875,
0.053466796875,
0.177490234375,
0.57275390625,
-1.20703125,
-0.254638671875,
0.50390625,
-0.75927734375,
0.398681640625,
0.178955078125,
-0.9814453125,
0.697265625,
-0.280029296875,
0.483154296875,
-0.587890625,
-0.82177734375,
-0.040679931640625,
-0.047943115234375,
-0.01558685302734375,
0.045257568359375,
0.66259765625,
0.6796875,
0.9228515625,
-0.0491943359375,
0.187744140625,
-1.19921875,
0.6435546875,
0.341796875,
0.67138671875,
-0.68798828125,
0.0599365234375,
-0.153076171875,
-0.453125,
-0.2364501953125,
-0.12548828125,
0.65576171875,
0.4072265625,
-0.278076171875,
0.84228515625,
0.46044921875,
-0.66748046875,
0.2139892578125,
-0.59716796875,
-0.190185546875,
-0.60205078125,
0.64990234375,
1.248046875,
-0.55810546875,
-0.418701171875,
-0.2607421875,
0.431640625,
0.301025390625,
0.85791015625,
0.3359375,
-0.6826171875,
0.037506103515625
] | 0
|
Provide tags and a correct Python 3 solution for this coding contest problem.
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 ≤ 100, 1 ≤ k ≤ 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
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.
Tags: dp, strings
Correct Solution:
```
def naiveSolve():
return
def charToInt(c): #'a'->0
return ord(c)-ord('a')
def main():
n,k=readIntArr()
s=input()
arr=[charToInt(c) for c in s]
dp=[[0 for _ in range(n+1)] for __ in range(n)]
# dp[i][l]
for i in range(n):
dp[i][1]=1
prevLatestIdx=[-1]*26
# to avoid repeated counting like ababa -> aba x 3,
# take only from the latest appearance of each character
for i in range(n):
for l in range(2,n+1):
for c in range(26):
if prevLatestIdx[c]!=-1:
dp[i][l]+=dp[prevLatestIdx[c]][l-1]
prevLatestIdx[arr[i]]=i
cnts=[0]*(n+1)
for c in range(26):
if prevLatestIdx[c]!=-1:
for l in range(1,n+1):
cnts[l]+=dp[prevLatestIdx[c]][l]
cnts[0]=1
cost=0
for l in range(n,-1,-1):
diff=min(k,cnts[l])
cost+=diff*(n-l)
k-=diff
if k==0:
print(cost)
else:
print(-1)
return
import sys
# input=sys.stdin.buffer.readline #FOR READING PURE INTEGER INPUTS (space separation ok)
input=lambda: sys.stdin.readline().rstrip("\r\n") #FOR READING STRING/TEXT INPUTS.
def oneLineArrayPrint(arr):
print(' '.join([str(x) for x in arr]))
def multiLineArrayPrint(arr):
print('\n'.join([str(x) for x in arr]))
def multiLineArrayOfArraysPrint(arr):
print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))
def readIntArr():
return [int(x) for x in input().split()]
# def readFloatArr():
# return [float(x) for x in input().split()]
def makeArr(defaultValFactory,dimensionArr): # eg. makeArr(lambda:0,[n,m])
dv=defaultValFactory;da=dimensionArr
if len(da)==1:return [dv() for _ in range(da[0])]
else:return [makeArr(dv,da[1:]) for _ in range(da[0])]
def queryInteractive(l,r):
print('? {} {}'.format(l,r))
sys.stdout.flush()
return int(input())
def answerInteractive(x):
print('! {}'.format(x))
sys.stdout.flush()
inf=float('inf')
# MOD=10**9+7
MOD=998244353
from math import gcd,floor,ceil
# from math import floor,ceil # for Python2
for _abc in range(1):
main()
```
| 102
|
[
-0.048187255859375,
0.250244140625,
0.2254638671875,
0.39501953125,
-0.1309814453125,
-0.208740234375,
-0.11566162109375,
-0.058319091796875,
0.55029296875,
0.7216796875,
0.7138671875,
-0.352294921875,
-0.279541015625,
-0.92822265625,
-0.59912109375,
0.2288818359375,
-0.6669921875,
-0.81787109375,
-0.425537109375,
-0.19677734375,
0.05682373046875,
-0.1710205078125,
-1.40625,
-0.379638671875,
0.2054443359375,
1.0087890625,
0.2352294921875,
0.185546875,
1.177734375,
1.2978515625,
-0.1298828125,
-0.85205078125,
0.46435546875,
-1.0419921875,
-0.45263671875,
-0.1005859375,
0.331298828125,
-0.54052734375,
-0.46240234375,
-0.10595703125,
0.463134765625,
-0.70751953125,
0.599609375,
-0.51708984375,
-1.21875,
-0.09600830078125,
-0.08807373046875,
-0.498779296875,
-0.478515625,
-0.09552001953125,
0.107421875,
-0.10992431640625,
0.4580078125,
-0.06402587890625,
0.0662841796875,
0.31298828125,
0.32861328125,
-0.36767578125,
-0.7236328125,
0.67822265625,
0.251953125,
-0.11492919921875,
0.2081298828125,
-0.580078125,
-0.56494140625,
0.250732421875,
-0.484619140625,
0.211669921875,
-0.0018482208251953125,
-0.7216796875,
-0.03466796875,
0.10772705078125,
-0.007282257080078125,
-0.54052734375,
-0.17236328125,
-0.445068359375,
0.10797119140625,
-0.2900390625,
-0.483154296875,
0.61865234375,
0.058502197265625,
0.935546875,
0.35888671875,
-0.0308837890625,
-0.66015625,
-0.59130859375,
0.057342529296875,
0.128662109375,
0.35546875,
0.1475830078125,
-0.5400390625,
0.9921875,
-0.6103515625,
-0.154296875,
0.38818359375,
0.68701171875,
-0.6240234375,
0.2685546875,
0.29541015625,
0.060546875,
0.4443359375,
0.75830078125,
-0.4228515625,
1.08203125,
-0.08123779296875,
-0.207763671875,
0.24951171875,
-0.32177734375,
-0.1934814453125,
-0.323486328125,
-0.193603515625,
-0.43017578125,
0.357666015625,
-0.12066650390625,
-0.5048828125,
0.95654296875,
-0.156982421875,
0.52294921875,
-0.5908203125,
0.41552734375,
0.436767578125,
0.0936279296875,
0.71728515625,
-0.2181396484375,
-0.035400390625,
-0.381591796875,
-0.58984375,
1.1953125,
-0.6337890625,
0.274169921875,
0.53564453125,
-0.29150390625,
-0.31884765625,
0.423095703125,
-0.22705078125,
0.771484375,
-0.13525390625,
0.51025390625,
0.65234375,
-0.489501953125,
0.6806640625,
-0.21337890625,
-0.0062103271484375,
1.5576171875,
0.51416015625,
0.32373046875,
1.0283203125,
0.345458984375,
-0.61865234375,
0.5244140625,
-1.021484375,
0.7021484375,
0.384033203125,
0.1702880859375,
-0.299560546875,
-0.1326904296875,
-0.0396728515625,
0.48046875,
0.096435546875,
0.38916015625,
-0.31591796875,
0.31982421875,
-0.251220703125,
0.352783203125,
-0.62353515625,
0.84814453125,
-0.1578369140625,
-0.27978515625,
0.021209716796875,
-0.71435546875,
0.38720703125,
-0.07794189453125,
-0.0322265625,
0.6728515625,
0.4677734375,
0.9931640625,
1.099609375,
-0.0299530029296875,
0.49462890625,
0.62158203125,
-0.3603515625,
0.171142578125,
0.426025390625,
0.5849609375,
0.93505859375,
0.136474609375,
0.1846923828125,
-0.046630859375,
-0.66357421875,
-0.67919921875,
0.2330322265625,
0.76708984375,
-0.32568359375,
0.25927734375,
-0.02935791015625,
-0.023529052734375,
-1.119140625,
-0.262451171875,
-0.06787109375,
-0.8896484375,
-0.58642578125,
0.5634765625,
0.061492919921875,
0.316162109375,
-0.59619140625,
-0.85009765625,
0.63720703125,
1.0693359375,
-0.57958984375,
-0.00115966796875,
0.66162109375,
0.148681640625,
-0.438232421875,
0.4140625,
-0.10968017578125,
-0.347900390625,
-0.63330078125,
0.64501953125,
-0.26904296875,
-0.28125,
-0.2186279296875,
0.58837890625,
0.29931640625,
0.6943359375,
0.09344482421875,
-0.1494140625,
0.15380859375,
1.47265625,
0.2049560546875,
0.32421875,
0.11212158203125,
0.72265625,
0.45361328125,
0.65771484375,
0.306640625,
0.326416015625,
0.487548828125,
1.310546875,
0.12274169921875,
0.60986328125,
0.1407470703125,
0.367919921875,
0.61669921875,
0.102294921875,
0.109375,
0.428466796875,
0.0285491943359375,
-0.048583984375,
-0.701171875,
0.006587982177734375,
-0.395263671875,
0.68359375,
0.08782958984375,
0.5380859375,
-0.207275390625,
-0.1627197265625,
0.38232421875,
1.369140625,
-1.146484375,
-0.73388671875,
-0.1048583984375,
0.04937744140625,
0.400634765625,
-0.03173828125,
0.53515625,
0.348388671875,
0.38330078125,
0.59716796875,
-0.289794921875,
-0.26611328125,
-0.55517578125,
-0.9462890625,
-0.489990234375,
-0.289794921875,
-0.5029296875,
-0.0014734268188476562,
0.1427001953125,
-0.97900390625,
0.286376953125,
-0.59130859375,
-0.0718994140625,
0.04388427734375,
-0.6689453125,
0.49365234375,
-0.40185546875,
0.472412109375,
-0.7060546875,
0.77001953125,
0.70361328125,
1.052734375,
-0.525390625,
-0.1552734375,
0.05572509765625,
-1.1025390625,
0.2529296875,
0.225341796875,
0.104248046875,
-0.0677490234375,
-0.4462890625,
-0.7900390625,
-0.4892578125,
0.06451416015625,
-0.328857421875,
0.09161376953125,
-0.476806640625,
0.6982421875,
0.5712890625,
-0.5166015625,
0.6279296875,
0.712890625,
-0.8349609375,
0.183837890625,
0.004039764404296875,
0.19482421875,
-0.96630859375,
1.2822265625,
0.31787109375,
0.5283203125,
-0.6396484375,
-0.6259765625,
-0.48486328125,
-0.12066650390625,
0.2467041015625,
-0.42919921875,
-0.2171630859375,
0.537109375,
0.00780487060546875,
-1.275390625,
0.391845703125,
-1.0498046875,
-0.413330078125,
-0.2469482421875,
-0.060333251953125,
0.546875,
0.97216796875,
0.09967041015625,
-0.81103515625,
-0.36474609375,
0.01197052001953125,
1.091796875,
0.71044921875,
-0.79736328125,
0.1522216796875,
0.7392578125,
0.2017822265625,
0.09521484375,
0.384765625,
-0.53515625,
-0.408203125,
0.258544921875,
0.08807373046875,
0.90380859375,
0.10614013671875,
0.59228515625,
0.7373046875,
0.65869140625,
-0.76953125,
-0.015045166015625,
-0.307861328125,
0.99560546875,
0.82080078125,
0.39697265625,
0.273193359375,
-0.48291015625,
-0.444580078125,
-0.9638671875,
0.470947265625,
0.05279541015625,
0.85107421875,
-0.69775390625,
0.75927734375,
0.43896484375,
-0.5712890625,
0.4736328125,
-0.9306640625,
-0.1961669921875,
1.2197265625,
-0.36572265625,
0.61669921875,
-0.439453125,
0.282470703125,
-0.135986328125,
0.2294921875,
0.28515625,
0.8623046875,
0.5625,
-0.45166015625,
-0.315185546875,
-0.53125,
-0.0601806640625,
0.08935546875,
-0.5234375,
0.413818359375,
-0.2374267578125,
-0.3291015625,
-0.85791015625,
0.002361297607421875,
1.0537109375,
0.7275390625,
0.10455322265625,
0.136474609375,
0.236328125,
0.351318359375,
0.60400390625,
-0.53369140625,
0.06683349609375,
-0.55126953125,
0.98193359375,
0.162841796875,
-0.0218353271484375,
-0.10723876953125,
-0.2205810546875,
-0.167236328125,
0.1357421875,
-0.0782470703125,
-0.445556640625,
-1.001953125,
0.10491943359375,
-0.0958251953125,
0.383544921875,
-0.58642578125,
-0.12042236328125,
-0.29541015625,
0.68408203125,
0.215576171875,
-1.3046875,
-0.0213470458984375,
-0.94580078125,
0.388916015625,
0.25244140625,
0.043304443359375,
-0.386962890625,
-0.26220703125,
-0.44677734375,
-0.6064453125,
0.501953125,
0.5703125,
-0.291748046875,
-0.08819580078125,
-1.2705078125,
0.6669921875,
0.291748046875,
0.023284912109375,
0.351318359375,
0.218994140625,
0.52490234375,
-0.1431884765625,
0.91455078125,
-0.53759765625,
-0.63720703125,
0.2171630859375,
-1.056640625,
0.60888671875,
-0.5947265625,
0.211181640625,
-0.2431640625,
-0.1123046875,
0.002521514892578125,
0.2296142578125,
-0.193115234375,
0.354248046875,
-0.5126953125,
-0.05035400390625,
-0.95263671875,
-0.7626953125,
0.7578125,
-0.52734375,
-0.307373046875,
0.7841796875,
0.00738525390625,
-0.01096343994140625,
0.352783203125,
0.6533203125,
-0.364501953125,
0.194091796875,
-0.427978515625,
0.8984375,
-0.6689453125,
-0.73828125,
0.00554656982421875,
-0.385986328125,
0.791015625,
-0.1458740234375,
-0.923828125,
-0.451904296875,
-1.1689453125,
-0.250244140625,
0.0258636474609375,
-0.384033203125,
0.1827392578125,
0.07147216796875,
-0.2352294921875,
0.1578369140625,
-0.327880859375,
-0.1773681640625,
-0.6884765625,
-0.7353515625,
-0.1368408203125,
0.50439453125,
0.39501953125,
0.9609375,
0.06195068359375,
-0.229736328125,
-0.2078857421875,
0.200439453125,
0.080322265625,
-0.400146484375,
-0.74365234375,
-0.251708984375,
-0.2457275390625,
-0.380859375,
0.489501953125,
0.1497802734375,
0.337158203125,
0.67822265625,
0.257568359375,
0.261962890625,
0.10028076171875,
-0.488037109375,
1.1416015625,
0.328369140625,
-1.056640625,
-0.689453125,
0.369873046875,
0.15283203125,
0.363525390625,
0.2041015625,
-0.798828125,
-0.630859375,
-1.255859375,
0.47216796875,
-0.38623046875,
0.1275634765625,
-0.98291015625,
-0.96875,
-0.438720703125,
0.302490234375,
-0.147216796875,
-0.446533203125,
-0.043121337890625,
-1.130859375,
0.0084686279296875,
-0.86376953125,
-0.2548828125,
-0.84912109375,
-0.82861328125,
-0.082275390625,
0.689453125,
0.366943359375,
0.2958984375,
-0.03912353515625,
-0.2188720703125,
0.76904296875,
-0.0257110595703125,
-0.91162109375,
-0.41552734375,
-0.11810302734375,
0.1181640625,
-0.25830078125,
-0.1453857421875,
-0.52783203125,
0.712890625,
-0.970703125,
0.322021484375,
-0.052093505859375,
-0.455322265625,
-0.2266845703125,
-0.413330078125,
1.1904296875,
-0.53564453125,
-0.228271484375,
-0.3037109375,
0.7275390625,
0.6240234375,
-0.08428955078125,
-0.442626953125,
-0.85107421875,
-0.70068359375,
-0.89208984375,
0.36083984375,
-0.55322265625,
0.2025146484375,
-0.83251953125,
-0.28662109375,
0.92041015625,
-0.57568359375,
0.253173828125,
1.25,
-0.049285888671875,
0.2095947265625,
-0.75537109375,
0.5078125,
0.59765625,
-0.61376953125,
-0.2130126953125,
0.10809326171875,
-0.720703125,
0.047576904296875,
-0.080810546875,
-1.2490234375,
-0.52099609375,
0.42236328125,
1.3505859375,
-0.3828125,
0.81201171875,
-0.08770751953125,
-1.30078125,
-0.80078125,
0.92333984375,
0.410400390625,
0.830078125,
0.99169921875,
-0.31640625,
-0.29541015625,
0.0286407470703125,
-0.04437255859375,
-0.50634765625,
-0.361328125,
1.34375,
-0.51708984375,
-0.42138671875,
0.78857421875,
0.88818359375,
-1.3486328125,
-0.64794921875,
0.1822509765625,
0.1573486328125,
-0.6240234375,
-0.66259765625,
0.387939453125,
0.765625,
-0.58837890625,
0.178466796875,
0.474853515625,
-0.1446533203125,
0.447021484375,
0.160400390625,
-0.04144287109375,
-0.497314453125,
0.55078125,
0.489990234375,
-0.43798828125,
-0.49072265625,
-1.05078125,
-0.4560546875,
-0.300537109375,
0.099853515625,
0.69775390625,
-0.2626953125,
0.43798828125,
0.386474609375,
0.318603515625,
0.5947265625,
-0.31201171875,
-0.470703125,
0.493408203125,
-0.58740234375,
-0.44140625,
0.216552734375,
0.43212890625,
-0.01117706298828125,
0.50634765625,
-0.57373046875,
0.038299560546875,
0.352294921875,
0.61328125,
-0.459716796875,
-0.5126953125,
-0.1956787109375,
-1.1181640625,
-0.428466796875,
-0.6162109375,
-1.060546875,
-0.1727294921875,
0.59375,
-0.036376953125,
-0.6474609375,
-0.032073974609375,
0.394287109375,
-0.7373046875,
0.91064453125,
-0.447021484375,
0.264404296875,
-0.5361328125,
-0.48291015625,
-0.62646484375,
-0.09124755859375,
-0.59814453125,
0.199462890625,
-0.796875,
0.54345703125,
-0.54345703125,
0.423095703125,
0.026824951171875,
0.7607421875,
-0.333740234375,
-0.8798828125,
-0.2373046875,
0.09625244140625,
-0.0221710205078125,
0.97998046875,
0.392822265625,
0.335205078125,
0.11871337890625,
-0.28515625,
-0.1468505859375,
-0.658203125,
0.395751953125,
0.66748046875,
-0.19580078125,
-0.305908203125,
-0.331298828125,
0.1282958984375,
-0.9384765625,
0.482177734375,
-0.666015625,
-0.0124664306640625,
0.410400390625,
-0.45458984375,
0.07373046875,
1.31640625,
-0.1502685546875,
0.3408203125,
0.1724853515625,
-0.25244140625,
0.40234375,
0.349365234375,
0.25732421875,
0.36376953125,
-0.1365966796875,
-0.41845703125,
-0.0533447265625,
0.16796875,
0.284423828125,
0.211669921875,
-0.298583984375,
-0.62158203125,
-1.0615234375,
-0.34375,
0.0035533905029296875,
0.295166015625,
0.298583984375,
-0.187255859375,
-0.0178070068359375,
-0.2276611328125,
-0.34814453125,
-0.2486572265625,
-1.2041015625,
-1.3662109375,
0.436279296875,
-0.29150390625,
-0.689453125,
-0.329833984375,
-0.295166015625,
-0.2071533203125,
-0.06707763671875,
0.412353515625,
-0.322021484375,
0.71533203125,
-0.2425537109375,
0.61962890625,
0.055694580078125,
-0.2509765625,
0.285888671875,
0.3505859375,
-0.896484375,
-0.64111328125,
0.08148193359375,
1.4228515625,
0.2239990234375,
-0.2293701171875,
-0.364013671875,
0.01084136962890625,
-0.0433349609375,
0.25,
-0.5791015625,
0.200927734375,
-0.271728515625,
0.31689453125,
-0.67529296875,
-0.285888671875,
-0.18359375,
0.4462890625,
0.53076171875,
-0.5498046875,
-0.767578125,
0.84423828125,
0.6318359375,
-0.2093505859375,
0.286376953125,
0.09271240234375,
0.296142578125,
-0.1817626953125,
-0.012359619140625,
-0.176025390625,
0.7890625,
0.94287109375,
0.6591796875,
-0.32373046875,
0.412109375,
0.52978515625,
-0.0186920166015625,
0.00783538818359375,
0.269287109375,
0.417724609375,
0.149169921875,
-0.12139892578125,
-0.71044921875,
-0.369140625,
-0.363525390625,
-0.4521484375,
0.10040283203125,
-0.108642578125,
0.1258544921875,
-0.1968994140625,
-0.828125,
0.89013671875,
-0.146484375,
-0.044769287109375,
0.2222900390625,
-0.47412109375,
0.1329345703125,
0.82861328125,
-0.196044921875,
-0.469970703125,
0.5927734375,
0.818359375,
0.427734375,
0.7197265625,
0.0070037841796875,
0.424072265625,
-0.269775390625,
0.251708984375,
-0.21240234375,
0.020416259765625,
-0.5986328125,
-0.498291015625,
-0.955078125,
0.10174560546875,
-0.50439453125,
-0.30126953125,
0.58203125,
-0.51416015625,
0.031768798828125,
-0.000957489013671875,
0.83544921875,
-0.68408203125,
0.39990234375,
0.39599609375,
-0.2802734375,
-0.94873046875,
0.8427734375,
-0.22900390625,
0.1982421875,
0.040313720703125,
0.92333984375,
-0.09735107421875,
1.31640625,
0.56298828125,
-0.31298828125,
0.054168701171875,
0.65625,
-0.1868896484375,
-0.449462890625,
-1.0498046875,
-0.09088134765625,
-0.269775390625,
-0.99072265625,
-0.19970703125,
-0.09832763671875,
0.82666015625,
-0.09295654296875,
-0.76416015625,
-0.1392822265625,
0.285400390625,
-1.1435546875,
0.87841796875,
0.396484375,
0.56298828125,
-0.08758544921875,
-0.08099365234375,
0.0947265625,
-1.162109375,
0.43408203125,
-0.78515625,
0.476318359375,
-0.6474609375,
-0.53369140625,
-0.97265625,
-0.488525390625,
-0.171630859375,
-0.64501953125,
-0.3955078125,
-1.228515625,
0.2486572265625,
0.666015625,
0.486572265625,
0.33740234375,
-0.302734375,
0.38818359375,
1.1708984375,
0.91650390625,
0.0623779296875,
0.51708984375,
0.58935546875,
0.1378173828125,
-0.385009765625,
0.06927490234375,
0.38037109375,
-0.12353515625,
0.324951171875,
-0.329345703125,
0.1446533203125,
-0.64501953125,
-1.623046875,
0.10797119140625,
0.59326171875,
0.4267578125,
-0.74267578125,
-0.6455078125,
-0.1602783203125,
-0.7724609375,
-0.4697265625,
-0.54833984375,
0.62109375,
-0.248046875,
-0.01386260986328125,
-0.3251953125,
-1.1494140625,
4.2109375,
0.92138671875,
1.0625,
0.25390625,
0.53564453125,
1.1298828125,
0.1396484375,
-0.476318359375,
0.26416015625,
-0.45654296875,
0.315185546875,
-0.405517578125,
0.11199951171875,
0.61279296875,
0.032958984375,
0.8037109375,
-0.41748046875,
0.263671875,
0.0147857666015625,
-1.19140625,
-1.0400390625,
0.497314453125,
-0.0152130126953125,
1,
0.311279296875,
0.2110595703125,
0.409912109375,
-0.45458984375,
-0.043365478515625,
-0.54296875,
0.22802734375,
-0.4765625,
0.9306640625,
0.1121826171875,
-0.049346923828125,
0.360107421875,
0.01432037353515625,
-0.2459716796875,
0.0770263671875,
0.15283203125,
0.0865478515625,
0.15087890625,
0.68603515625,
-1.205078125,
-0.208984375,
0.422119140625,
-0.77734375,
0.41357421875,
0.05987548828125,
-0.88232421875,
0.78857421875,
-0.328857421875,
0.57666015625,
-0.59375,
-0.826171875,
-0.10076904296875,
-0.0030460357666015625,
-0.05633544921875,
0.06451416015625,
0.69287109375,
0.65771484375,
0.91748046875,
-0.113037109375,
0.1868896484375,
-1.1943359375,
0.55908203125,
0.352294921875,
0.495361328125,
-0.65771484375,
0.1180419921875,
-0.1600341796875,
-0.44970703125,
-0.274169921875,
-0.1007080078125,
0.70703125,
0.38623046875,
-0.254638671875,
0.88818359375,
0.45458984375,
-0.62109375,
0.30029296875,
-0.580078125,
-0.15234375,
-0.50439453125,
0.6943359375,
1.1513671875,
-0.537109375,
-0.417724609375,
-0.296142578125,
0.450927734375,
0.340576171875,
0.9306640625,
0.3125,
-0.6513671875,
0.0176544189453125
] | 0
|
Provide tags and a correct Python 3 solution for this coding contest problem.
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 ≤ 100, 1 ≤ k ≤ 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
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.
Tags: dp, strings
Correct Solution:
```
def main():
from sys import stdin
input = stdin.readline
# input = open('25-B.txt', 'r').readline
n, k = map(int, input().split())
s = input()[:-1]
dp = [[0] * 26 for i in range(n + 1)]
dp[0][0] = 1
for ch in s:
j = ord(ch) - ord('a')
for i in range(n, 0, -1):
dp[i][j] = sum(dp[i - 1])
x = 0
y = 0
for i in range(n, -1, -1):
if x + sum(dp[i]) >= k:
print(k * n - y - (k - x) * i)
break
x += sum(dp[i])
y += i * sum(dp[i])
else:
print(-1)
main()
```
| 103
|
[
-0.0823974609375,
0.2425537109375,
0.30908203125,
0.381103515625,
-0.0887451171875,
-0.162841796875,
-0.2061767578125,
-0.1630859375,
0.43017578125,
0.7177734375,
0.69287109375,
-0.316162109375,
-0.243408203125,
-0.97216796875,
-0.60546875,
0.139404296875,
-0.642578125,
-0.861328125,
-0.40966796875,
-0.2464599609375,
0.08294677734375,
-0.1285400390625,
-1.3671875,
-0.4150390625,
0.2491455078125,
1.0869140625,
0.246826171875,
0.2396240234375,
1.0869140625,
1.3017578125,
-0.2330322265625,
-0.89599609375,
0.465576171875,
-1.1064453125,
-0.491943359375,
-0.1451416015625,
0.35400390625,
-0.64306640625,
-0.501953125,
-0.14111328125,
0.396240234375,
-0.71435546875,
0.61328125,
-0.5458984375,
-1.2021484375,
-0.16015625,
-0.1336669921875,
-0.41064453125,
-0.473388671875,
-0.06292724609375,
0.11846923828125,
-0.08966064453125,
0.50146484375,
0.01099395751953125,
0.06805419921875,
0.36767578125,
0.238525390625,
-0.328125,
-0.73828125,
0.59375,
0.1939697265625,
-0.1387939453125,
0.1053466796875,
-0.60498046875,
-0.482666015625,
0.2705078125,
-0.399658203125,
0.1414794921875,
0.037811279296875,
-0.72802734375,
-0.01617431640625,
0.08404541015625,
-0.0526123046875,
-0.66357421875,
-0.2161865234375,
-0.45703125,
0.1746826171875,
-0.234375,
-0.55078125,
0.67822265625,
0.0325927734375,
0.95703125,
0.3310546875,
-0.0089569091796875,
-0.67724609375,
-0.62255859375,
-0.045867919921875,
0.11468505859375,
0.33642578125,
0.11541748046875,
-0.5634765625,
0.9560546875,
-0.69970703125,
-0.0728759765625,
0.350830078125,
0.6630859375,
-0.5693359375,
0.2288818359375,
0.27978515625,
0.0948486328125,
0.4326171875,
0.76513671875,
-0.5205078125,
1.154296875,
-0.032501220703125,
-0.2298583984375,
0.313720703125,
-0.336181640625,
-0.262939453125,
-0.28271484375,
-0.1822509765625,
-0.44580078125,
0.403564453125,
-0.11053466796875,
-0.493896484375,
1.021484375,
-0.10882568359375,
0.5361328125,
-0.62939453125,
0.464599609375,
0.50048828125,
0.0869140625,
0.6015625,
-0.207763671875,
-0.1055908203125,
-0.411376953125,
-0.5380859375,
1.1806640625,
-0.634765625,
0.29833984375,
0.5615234375,
-0.240966796875,
-0.2471923828125,
0.343505859375,
-0.2288818359375,
0.763671875,
-0.208984375,
0.5078125,
0.5908203125,
-0.5810546875,
0.71826171875,
-0.2489013671875,
-0.055419921875,
1.63671875,
0.580078125,
0.282958984375,
1.029296875,
0.34130859375,
-0.56591796875,
0.47412109375,
-1.0283203125,
0.6943359375,
0.424560546875,
0.158447265625,
-0.2734375,
-0.0791015625,
-0.0584716796875,
0.4794921875,
0.055084228515625,
0.45703125,
-0.345703125,
0.3857421875,
-0.269287109375,
0.40234375,
-0.591796875,
0.9140625,
-0.1795654296875,
-0.2666015625,
-0.01145172119140625,
-0.728515625,
0.419677734375,
-0.1568603515625,
-0.1104736328125,
0.71875,
0.40966796875,
1.0029296875,
1.0849609375,
-0.04559326171875,
0.49658203125,
0.6484375,
-0.387939453125,
0.24169921875,
0.368896484375,
0.53466796875,
0.935546875,
0.181884765625,
0.1800537109375,
-0.038604736328125,
-0.73291015625,
-0.71435546875,
0.2396240234375,
0.73046875,
-0.25927734375,
0.2366943359375,
0.08172607421875,
0.022308349609375,
-1.1201171875,
-0.250244140625,
-0.130615234375,
-0.89208984375,
-0.65185546875,
0.55712890625,
0.1107177734375,
0.2288818359375,
-0.556640625,
-0.81982421875,
0.72216796875,
1.1533203125,
-0.4365234375,
-0.0162506103515625,
0.7421875,
0.1141357421875,
-0.42333984375,
0.498779296875,
-0.1256103515625,
-0.400390625,
-0.71142578125,
0.6484375,
-0.280517578125,
-0.17578125,
-0.30615234375,
0.5966796875,
0.273681640625,
0.64794921875,
0.0540771484375,
-0.1514892578125,
0.156982421875,
1.5078125,
0.265869140625,
0.304931640625,
0.104736328125,
0.7099609375,
0.461669921875,
0.66162109375,
0.281005859375,
0.269775390625,
0.49169921875,
1.3603515625,
0.0120849609375,
0.64501953125,
0.0816650390625,
0.35205078125,
0.60986328125,
0.09674072265625,
0.11279296875,
0.427734375,
-0.01099395751953125,
-0.00815582275390625,
-0.6875,
-0.035430908203125,
-0.3955078125,
0.72216796875,
0.0670166015625,
0.5126953125,
-0.20263671875,
-0.300537109375,
0.466552734375,
1.2666015625,
-1.181640625,
-0.60107421875,
-0.199951171875,
0.064208984375,
0.379150390625,
0.01561737060546875,
0.52099609375,
0.35986328125,
0.348876953125,
0.65966796875,
-0.306884765625,
-0.24072265625,
-0.5419921875,
-0.8896484375,
-0.59130859375,
-0.197998046875,
-0.5087890625,
-0.005096435546875,
0.1700439453125,
-0.95654296875,
0.295166015625,
-0.6318359375,
0.00746917724609375,
0.0775146484375,
-0.71044921875,
0.48779296875,
-0.415771484375,
0.46875,
-0.7119140625,
0.77197265625,
0.7314453125,
1.0908203125,
-0.4296875,
-0.10400390625,
0.002422332763671875,
-1.080078125,
0.26171875,
0.206298828125,
0.1683349609375,
-0.07965087890625,
-0.49853515625,
-0.76806640625,
-0.58837890625,
0.00940704345703125,
-0.400390625,
0.11614990234375,
-0.49609375,
0.57080078125,
0.56396484375,
-0.5361328125,
0.70751953125,
0.74755859375,
-0.912109375,
0.129638671875,
-0.0241546630859375,
0.1640625,
-0.9716796875,
1.2578125,
0.310791015625,
0.5751953125,
-0.6591796875,
-0.5732421875,
-0.466064453125,
-0.07781982421875,
0.307373046875,
-0.447509765625,
-0.177978515625,
0.50439453125,
0.09320068359375,
-1.291015625,
0.427734375,
-1.1259765625,
-0.30322265625,
-0.267333984375,
-0.067138671875,
0.59228515625,
0.8857421875,
0.07147216796875,
-0.75634765625,
-0.424072265625,
0.09161376953125,
1.1064453125,
0.75439453125,
-0.7236328125,
0.1112060546875,
0.787109375,
0.1103515625,
0.005992889404296875,
0.30859375,
-0.55615234375,
-0.306396484375,
0.23583984375,
0.17333984375,
0.8837890625,
0.172607421875,
0.5595703125,
0.740234375,
0.69189453125,
-0.76513671875,
0.0758056640625,
-0.27294921875,
1.056640625,
0.81396484375,
0.4599609375,
0.327392578125,
-0.435546875,
-0.40380859375,
-1.0078125,
0.4921875,
0.036865234375,
0.88720703125,
-0.69140625,
0.671875,
0.334228515625,
-0.52001953125,
0.51806640625,
-0.9189453125,
-0.2010498046875,
1.2509765625,
-0.38818359375,
0.67041015625,
-0.39794921875,
0.31103515625,
-0.14208984375,
0.12420654296875,
0.306884765625,
0.81884765625,
0.61962890625,
-0.461181640625,
-0.402587890625,
-0.57421875,
-0.01311492919921875,
0.044952392578125,
-0.60107421875,
0.315673828125,
-0.1734619140625,
-0.293701171875,
-0.86767578125,
-0.032958984375,
1.05859375,
0.7109375,
0.08099365234375,
0.0682373046875,
0.150634765625,
0.336669921875,
0.5908203125,
-0.546875,
0.0941162109375,
-0.61767578125,
1.01953125,
0.08648681640625,
-0.01515960693359375,
-0.142578125,
-0.20556640625,
-0.1776123046875,
0.251953125,
-0.0215911865234375,
-0.385986328125,
-1.0263671875,
0.0997314453125,
-0.1478271484375,
0.25732421875,
-0.59912109375,
-0.10577392578125,
-0.313232421875,
0.7001953125,
0.1749267578125,
-1.3017578125,
-0.0931396484375,
-0.96337890625,
0.41748046875,
0.203125,
0.04718017578125,
-0.44921875,
-0.238037109375,
-0.443115234375,
-0.6435546875,
0.499755859375,
0.6279296875,
-0.27392578125,
0.01248931884765625,
-1.2373046875,
0.6865234375,
0.24365234375,
0.002178192138671875,
0.271240234375,
0.1661376953125,
0.50244140625,
-0.11041259765625,
0.91357421875,
-0.50830078125,
-0.716796875,
0.1727294921875,
-1.0908203125,
0.638671875,
-0.67138671875,
0.1934814453125,
-0.30517578125,
-0.1253662109375,
0.0404052734375,
0.1995849609375,
-0.2064208984375,
0.299072265625,
-0.45703125,
0.0751953125,
-0.91552734375,
-0.72509765625,
0.7119140625,
-0.5205078125,
-0.35986328125,
0.75390625,
-0.0254669189453125,
0.056304931640625,
0.37060546875,
0.7509765625,
-0.278076171875,
0.301513671875,
-0.5009765625,
0.85400390625,
-0.60107421875,
-0.759765625,
-0.126708984375,
-0.328369140625,
0.82177734375,
-0.02813720703125,
-0.90771484375,
-0.40380859375,
-1.138671875,
-0.2734375,
-0.03240966796875,
-0.34716796875,
0.1895751953125,
0.11065673828125,
-0.2034912109375,
0.0806884765625,
-0.32080078125,
-0.191162109375,
-0.61474609375,
-0.7353515625,
-0.2227783203125,
0.56201171875,
0.3173828125,
0.99169921875,
-0.0210113525390625,
-0.2242431640625,
-0.2403564453125,
0.1190185546875,
0.0233917236328125,
-0.36572265625,
-0.78857421875,
-0.280517578125,
-0.195068359375,
-0.5498046875,
0.470947265625,
0.195068359375,
0.334716796875,
0.76220703125,
0.310302734375,
0.278564453125,
0.1324462890625,
-0.5400390625,
1.1083984375,
0.39501953125,
-1.0537109375,
-0.69287109375,
0.453125,
0.09027099609375,
0.415771484375,
0.390869140625,
-0.82080078125,
-0.61865234375,
-1.283203125,
0.50390625,
-0.34130859375,
0.140869140625,
-1.0029296875,
-0.95751953125,
-0.5244140625,
0.343994140625,
-0.1561279296875,
-0.457275390625,
0.0020275115966796875,
-1.0830078125,
0.0028705596923828125,
-0.85400390625,
-0.231689453125,
-0.78369140625,
-0.96484375,
-0.11773681640625,
0.767578125,
0.29345703125,
0.303955078125,
-0.08203125,
-0.219482421875,
0.73388671875,
-0.04901123046875,
-0.86572265625,
-0.44091796875,
-0.10150146484375,
0.12548828125,
-0.294677734375,
-0.058990478515625,
-0.54638671875,
0.76513671875,
-1.0068359375,
0.2493896484375,
-0.042724609375,
-0.4384765625,
-0.2166748046875,
-0.348388671875,
1.2001953125,
-0.53955078125,
-0.1845703125,
-0.293212890625,
0.8359375,
0.60595703125,
0.065673828125,
-0.4423828125,
-0.84716796875,
-0.6748046875,
-0.89111328125,
0.31103515625,
-0.611328125,
0.21337890625,
-0.79248046875,
-0.21044921875,
0.9912109375,
-0.67822265625,
0.30029296875,
1.279296875,
-0.0931396484375,
0.141845703125,
-0.787109375,
0.5966796875,
0.5224609375,
-0.70166015625,
-0.06817626953125,
0.12237548828125,
-0.755859375,
0.04876708984375,
-0.1158447265625,
-1.2060546875,
-0.490966796875,
0.405517578125,
1.5068359375,
-0.3740234375,
0.7763671875,
-0.1243896484375,
-1.2529296875,
-0.7470703125,
0.98681640625,
0.42626953125,
0.8759765625,
1.1005859375,
-0.2890625,
-0.266357421875,
0.0830078125,
-0.121337890625,
-0.486328125,
-0.431640625,
1.359375,
-0.459716796875,
-0.35107421875,
0.78173828125,
0.8447265625,
-1.345703125,
-0.66796875,
0.2103271484375,
0.12646484375,
-0.63623046875,
-0.58203125,
0.375732421875,
0.69189453125,
-0.56591796875,
0.189208984375,
0.493896484375,
-0.142333984375,
0.40966796875,
0.20556640625,
-0.047882080078125,
-0.5400390625,
0.5751953125,
0.58740234375,
-0.42822265625,
-0.489990234375,
-1.0751953125,
-0.456298828125,
-0.322021484375,
0.11749267578125,
0.74169921875,
-0.3037109375,
0.4423828125,
0.37060546875,
0.31787109375,
0.6015625,
-0.31591796875,
-0.5478515625,
0.451416015625,
-0.5244140625,
-0.45263671875,
0.2296142578125,
0.398193359375,
-0.0465087890625,
0.53173828125,
-0.580078125,
0.06671142578125,
0.341552734375,
0.5517578125,
-0.402587890625,
-0.556640625,
-0.127685546875,
-1.0263671875,
-0.439208984375,
-0.5751953125,
-0.962890625,
-0.271728515625,
0.5693359375,
0.03717041015625,
-0.7373046875,
-0.10186767578125,
0.489013671875,
-0.67529296875,
0.89697265625,
-0.463623046875,
0.18798828125,
-0.499267578125,
-0.4423828125,
-0.693359375,
-0.0224456787109375,
-0.6552734375,
0.1468505859375,
-0.751953125,
0.5341796875,
-0.53125,
0.40234375,
0.0273895263671875,
0.7822265625,
-0.30859375,
-0.8525390625,
-0.1822509765625,
0.05224609375,
0.056427001953125,
0.95458984375,
0.475830078125,
0.26025390625,
0.2021484375,
-0.30859375,
-0.06622314453125,
-0.5771484375,
0.381591796875,
0.7578125,
-0.1090087890625,
-0.322265625,
-0.47216796875,
0.2130126953125,
-0.9169921875,
0.47314453125,
-0.59619140625,
-0.04315185546875,
0.396240234375,
-0.477294921875,
0.0882568359375,
1.3671875,
-0.083251953125,
0.309326171875,
0.18701171875,
-0.196044921875,
0.431640625,
0.3642578125,
0.148193359375,
0.367919921875,
-0.04876708984375,
-0.3251953125,
-0.062286376953125,
0.157470703125,
0.2044677734375,
0.21240234375,
-0.422607421875,
-0.5908203125,
-1.126953125,
-0.290771484375,
0.08270263671875,
0.31884765625,
0.333984375,
-0.1883544921875,
-0.166259765625,
-0.264404296875,
-0.3173828125,
-0.206298828125,
-1.1611328125,
-1.384765625,
0.4814453125,
-0.25244140625,
-0.779296875,
-0.380126953125,
-0.36181640625,
-0.156982421875,
-0.01222991943359375,
0.37939453125,
-0.25048828125,
0.80029296875,
-0.167724609375,
0.6376953125,
0.04595947265625,
-0.17578125,
0.298828125,
0.3076171875,
-0.90771484375,
-0.66064453125,
0.2042236328125,
1.5,
0.187744140625,
-0.338134765625,
-0.466064453125,
0.102783203125,
-0.07415771484375,
0.2388916015625,
-0.5390625,
0.155029296875,
-0.301025390625,
0.321044921875,
-0.65234375,
-0.33837890625,
-0.2359619140625,
0.421142578125,
0.572265625,
-0.54931640625,
-0.74462890625,
0.796875,
0.62890625,
-0.2294921875,
0.3173828125,
0.138427734375,
0.251953125,
-0.1376953125,
0.017608642578125,
-0.1474609375,
0.85302734375,
0.951171875,
0.6572265625,
-0.283935546875,
0.412353515625,
0.54345703125,
-0.03857421875,
-0.016510009765625,
0.173095703125,
0.333984375,
0.0286407470703125,
-0.166015625,
-0.66845703125,
-0.392333984375,
-0.38525390625,
-0.5869140625,
0.08203125,
-0.2276611328125,
0.10357666015625,
-0.1893310546875,
-0.814453125,
0.9267578125,
-0.1351318359375,
-0.140869140625,
0.369140625,
-0.4619140625,
0.1697998046875,
0.9052734375,
-0.1739501953125,
-0.421875,
0.65478515625,
0.84619140625,
0.4208984375,
0.64208984375,
0.028167724609375,
0.3974609375,
-0.309814453125,
0.209228515625,
-0.2149658203125,
0.0016040802001953125,
-0.615234375,
-0.45654296875,
-0.97119140625,
0.130615234375,
-0.50439453125,
-0.30078125,
0.60546875,
-0.487060546875,
0.08544921875,
-0.07720947265625,
0.845703125,
-0.611328125,
0.412841796875,
0.436279296875,
-0.265869140625,
-0.97216796875,
0.822265625,
-0.2476806640625,
0.30126953125,
-0.0254669189453125,
0.8759765625,
-0.06634521484375,
1.404296875,
0.57177734375,
-0.3955078125,
0.10595703125,
0.63720703125,
-0.1854248046875,
-0.626953125,
-1.013671875,
-0.0926513671875,
-0.1837158203125,
-0.9755859375,
-0.1416015625,
-0.035919189453125,
0.80908203125,
-0.051300048828125,
-0.80712890625,
-0.12481689453125,
0.282958984375,
-1.1767578125,
0.95361328125,
0.35888671875,
0.55029296875,
-0.07745361328125,
-0.02056884765625,
0.100830078125,
-1.1689453125,
0.3798828125,
-0.7822265625,
0.595703125,
-0.75830078125,
-0.484619140625,
-1.0693359375,
-0.488037109375,
-0.12335205078125,
-0.5400390625,
-0.434814453125,
-1.19921875,
0.2919921875,
0.60400390625,
0.455322265625,
0.34521484375,
-0.27978515625,
0.35888671875,
1.2216796875,
0.82421875,
0.11517333984375,
0.513671875,
0.48974609375,
0.07177734375,
-0.32861328125,
0.041839599609375,
0.3837890625,
-0.0828857421875,
0.2398681640625,
-0.2646484375,
0.07659912109375,
-0.61181640625,
-1.6279296875,
0.1854248046875,
0.64990234375,
0.456298828125,
-0.6728515625,
-0.7236328125,
-0.1021728515625,
-0.84423828125,
-0.423095703125,
-0.533203125,
0.57958984375,
-0.224853515625,
-0.004947662353515625,
-0.3134765625,
-1.125,
4.16796875,
0.95654296875,
1.16015625,
0.1905517578125,
0.46142578125,
1.04296875,
0.16015625,
-0.45751953125,
0.1802978515625,
-0.404296875,
0.341552734375,
-0.4599609375,
0.1474609375,
0.65673828125,
0.0316162109375,
0.783203125,
-0.441650390625,
0.185302734375,
-0.014862060546875,
-1.154296875,
-0.955078125,
0.486328125,
-0.079833984375,
0.97509765625,
0.250244140625,
0.241943359375,
0.364990234375,
-0.4365234375,
-0.0902099609375,
-0.5322265625,
0.3984375,
-0.394775390625,
0.9384765625,
0.09014892578125,
-0.05938720703125,
0.403076171875,
-0.0802001953125,
-0.260986328125,
0.0020294189453125,
0.1414794921875,
0.00128936767578125,
0.209716796875,
0.56689453125,
-1.177734375,
-0.265380859375,
0.53173828125,
-0.76025390625,
0.392578125,
0.2275390625,
-0.98486328125,
0.75732421875,
-0.272216796875,
0.49853515625,
-0.5751953125,
-0.833984375,
-0.054290771484375,
-0.03033447265625,
-0.048980712890625,
0.0050811767578125,
0.65966796875,
0.6435546875,
0.93701171875,
-0.06646728515625,
0.2423095703125,
-1.224609375,
0.6435546875,
0.325439453125,
0.63427734375,
-0.68701171875,
0.047760009765625,
-0.164306640625,
-0.449951171875,
-0.256591796875,
-0.129638671875,
0.59814453125,
0.415283203125,
-0.27880859375,
0.86083984375,
0.491943359375,
-0.68017578125,
0.1842041015625,
-0.62255859375,
-0.2091064453125,
-0.6044921875,
0.68798828125,
1.2470703125,
-0.5849609375,
-0.42626953125,
-0.290283203125,
0.47607421875,
0.326416015625,
0.82861328125,
0.3193359375,
-0.63232421875,
0.043792724609375
] | 0
|
Provide tags and a correct Python 3 solution for this coding contest problem.
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 ≤ 100, 1 ≤ k ≤ 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
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.
Tags: dp, strings
Correct Solution:
```
[n, k] = [int(i) for i in input().split()]
s = input()
cntsz = [0 for i in range(105)]
dp = [[0 for i in range(105)] for j in range(105)]
lst = [0 for i in range(105)]
prv = [0 for i in range(26)]
n = len(s)
s = '%' + s
for i in range(n + 1):
dp[i][0]=1
for i in range(1, n + 1):
lst[i] = prv[ord(s[i])-ord('a')]
prv[ord(s[i]) - ord('a')] = i
for sz in range(1, n + 1):
for i in range(1, n + 1):
dp[i][sz] += dp[i - 1][sz]
dp[i][sz] += dp[i - 1][sz - 1]
if lst[i] != 0:
dp[i][sz] -= dp[lst[i]-1][sz-1]
for sz in range(1, n + 1):
for i in range(1, n + 1):
cntsz[sz] += dp[i][sz]
cntsz[sz] -= dp[i - 1][sz]
cntsz[0] += 1
done = 0
ans = 0
for i in range(n, -1, -1):
if done + cntsz[i] >= k:
ans += (n - i) * (k - done)
done = k
break
done += cntsz[i]
ans += cntsz[i] * (n - i)
if done < k:
print(-1)
else:
print(ans)
```
| 104
|
[
-0.061309814453125,
0.25390625,
0.314453125,
0.350830078125,
-0.04852294921875,
-0.181640625,
-0.1962890625,
-0.19140625,
0.4658203125,
0.7763671875,
0.662109375,
-0.25439453125,
-0.27197265625,
-0.9345703125,
-0.6064453125,
0.114990234375,
-0.62841796875,
-0.79296875,
-0.417724609375,
-0.23681640625,
0.0819091796875,
-0.170654296875,
-1.3720703125,
-0.462646484375,
0.2349853515625,
1.103515625,
0.186767578125,
0.2137451171875,
1.09375,
1.28125,
-0.267333984375,
-0.8544921875,
0.49267578125,
-1.1015625,
-0.5048828125,
-0.190185546875,
0.2958984375,
-0.56787109375,
-0.464111328125,
-0.07977294921875,
0.399169921875,
-0.68701171875,
0.62060546875,
-0.48876953125,
-1.208984375,
-0.1812744140625,
-0.09649658203125,
-0.3642578125,
-0.4755859375,
-0.13134765625,
0.12200927734375,
-0.03521728515625,
0.51611328125,
0.0989990234375,
0.0946044921875,
0.316162109375,
0.259521484375,
-0.2841796875,
-0.77587890625,
0.61328125,
0.2244873046875,
-0.156494140625,
0.14208984375,
-0.580078125,
-0.499755859375,
0.1905517578125,
-0.433837890625,
0.134033203125,
0.0279693603515625,
-0.73193359375,
-0.029449462890625,
0.06817626953125,
-0.073974609375,
-0.607421875,
-0.2486572265625,
-0.40185546875,
0.173095703125,
-0.28369140625,
-0.5771484375,
0.6328125,
0.03912353515625,
0.84375,
0.286865234375,
-0.05487060546875,
-0.6826171875,
-0.60498046875,
-0.04571533203125,
0.086669921875,
0.35546875,
0.10675048828125,
-0.56201171875,
0.99951171875,
-0.72509765625,
-0.049346923828125,
0.41064453125,
0.59912109375,
-0.580078125,
0.2186279296875,
0.31640625,
0.07037353515625,
0.47802734375,
0.77783203125,
-0.486328125,
1.1533203125,
-0.1033935546875,
-0.238525390625,
0.2578125,
-0.372802734375,
-0.274658203125,
-0.27294921875,
-0.1636962890625,
-0.45703125,
0.395263671875,
-0.06591796875,
-0.497802734375,
1.04296875,
-0.1134033203125,
0.564453125,
-0.60791015625,
0.3916015625,
0.482666015625,
0.046661376953125,
0.60595703125,
-0.146728515625,
-0.1253662109375,
-0.381591796875,
-0.5166015625,
1.1689453125,
-0.63671875,
0.376708984375,
0.654296875,
-0.2242431640625,
-0.204345703125,
0.318603515625,
-0.217041015625,
0.7119140625,
-0.244873046875,
0.55712890625,
0.59375,
-0.525390625,
0.7412109375,
-0.18896484375,
-0.037750244140625,
1.5595703125,
0.62890625,
0.2340087890625,
1.0478515625,
0.34814453125,
-0.53173828125,
0.431884765625,
-1.01171875,
0.74951171875,
0.428955078125,
0.15625,
-0.306884765625,
-0.085693359375,
-0.0950927734375,
0.43505859375,
0.0160064697265625,
0.475830078125,
-0.28125,
0.32861328125,
-0.233154296875,
0.44775390625,
-0.611328125,
0.91259765625,
-0.2049560546875,
-0.2724609375,
-0.0021209716796875,
-0.7451171875,
0.445068359375,
-0.1875,
-0.11187744140625,
0.6953125,
0.3603515625,
1.01171875,
1.0693359375,
-0.048187255859375,
0.51416015625,
0.67236328125,
-0.33251953125,
0.232666015625,
0.373779296875,
0.495361328125,
0.89208984375,
0.1690673828125,
0.1650390625,
-0.035552978515625,
-0.7216796875,
-0.67431640625,
0.272705078125,
0.6923828125,
-0.259033203125,
0.2169189453125,
0.0693359375,
0.059326171875,
-1.1728515625,
-0.2235107421875,
-0.1756591796875,
-0.876953125,
-0.6259765625,
0.5703125,
0.05267333984375,
0.203369140625,
-0.60888671875,
-0.79443359375,
0.689453125,
1.10546875,
-0.47119140625,
-0.034393310546875,
0.71142578125,
0.128662109375,
-0.36376953125,
0.452392578125,
-0.0740966796875,
-0.39892578125,
-0.68115234375,
0.65185546875,
-0.29736328125,
-0.2166748046875,
-0.31640625,
0.5908203125,
0.30712890625,
0.65869140625,
0.08770751953125,
-0.2095947265625,
0.1336669921875,
1.576171875,
0.328125,
0.33154296875,
0.1519775390625,
0.73828125,
0.521484375,
0.6640625,
0.312255859375,
0.297119140625,
0.430419921875,
1.3564453125,
0.031280517578125,
0.63623046875,
0.127685546875,
0.25,
0.6845703125,
0.061004638671875,
0.0953369140625,
0.5224609375,
-0.003795623779296875,
0.050689697265625,
-0.6806640625,
-0.09405517578125,
-0.379150390625,
0.65625,
0.128662109375,
0.51953125,
-0.2047119140625,
-0.268798828125,
0.44287109375,
1.3095703125,
-1.169921875,
-0.58056640625,
-0.26171875,
0.08837890625,
0.384033203125,
-0.010406494140625,
0.54736328125,
0.43115234375,
0.421630859375,
0.61279296875,
-0.340576171875,
-0.2103271484375,
-0.58056640625,
-0.90625,
-0.55908203125,
-0.2188720703125,
-0.479736328125,
0.00800323486328125,
0.1778564453125,
-0.953125,
0.394775390625,
-0.63037109375,
-0.05413818359375,
0.04852294921875,
-0.6923828125,
0.513671875,
-0.391845703125,
0.52490234375,
-0.75146484375,
0.73779296875,
0.70654296875,
1.0830078125,
-0.43017578125,
-0.05902099609375,
0.033935546875,
-1.103515625,
0.2061767578125,
0.2271728515625,
0.14111328125,
-0.1072998046875,
-0.53173828125,
-0.6962890625,
-0.5244140625,
0.09027099609375,
-0.367919921875,
0.2000732421875,
-0.484130859375,
0.56103515625,
0.5146484375,
-0.47509765625,
0.65087890625,
0.75439453125,
-0.9248046875,
0.119384765625,
0.0098876953125,
0.177734375,
-0.97802734375,
1.19140625,
0.343994140625,
0.61865234375,
-0.62060546875,
-0.52587890625,
-0.52880859375,
-0.06927490234375,
0.333740234375,
-0.47119140625,
-0.20458984375,
0.44873046875,
0.07342529296875,
-1.2939453125,
0.403076171875,
-1.056640625,
-0.31591796875,
-0.2626953125,
-0.032958984375,
0.60595703125,
0.85986328125,
0.044830322265625,
-0.8037109375,
-0.407470703125,
0.036590576171875,
1.056640625,
0.7119140625,
-0.72998046875,
0.11968994140625,
0.73828125,
0.1820068359375,
0.024169921875,
0.29638671875,
-0.53369140625,
-0.335205078125,
0.289306640625,
0.142333984375,
0.85888671875,
0.1455078125,
0.491455078125,
0.71435546875,
0.71484375,
-0.6708984375,
0.09173583984375,
-0.255126953125,
1.048828125,
0.841796875,
0.4873046875,
0.364013671875,
-0.495849609375,
-0.442138671875,
-1.017578125,
0.521484375,
0.05804443359375,
0.85693359375,
-0.7099609375,
0.63916015625,
0.376953125,
-0.5146484375,
0.52392578125,
-0.93505859375,
-0.2227783203125,
1.1650390625,
-0.401611328125,
0.64697265625,
-0.373291015625,
0.26123046875,
-0.12420654296875,
0.223876953125,
0.290283203125,
0.83251953125,
0.6064453125,
-0.49462890625,
-0.400146484375,
-0.49267578125,
0.00453948974609375,
0.1004638671875,
-0.5966796875,
0.2958984375,
-0.10626220703125,
-0.318603515625,
-0.78857421875,
-0.00832366943359375,
1.076171875,
0.77734375,
0.047698974609375,
0.15869140625,
0.19384765625,
0.345703125,
0.60693359375,
-0.55078125,
0.0784912109375,
-0.52783203125,
0.9599609375,
0.07916259765625,
-0.03155517578125,
-0.125732421875,
-0.2266845703125,
-0.144287109375,
0.208251953125,
-0.00850677490234375,
-0.39892578125,
-0.994140625,
0.09222412109375,
-0.1710205078125,
0.3251953125,
-0.609375,
-0.1053466796875,
-0.289794921875,
0.68994140625,
0.1622314453125,
-1.28515625,
-0.04241943359375,
-0.9912109375,
0.47119140625,
0.1661376953125,
0.028411865234375,
-0.44580078125,
-0.2783203125,
-0.418212890625,
-0.63916015625,
0.484375,
0.60107421875,
-0.34423828125,
-0.046966552734375,
-1.1875,
0.6123046875,
0.23193359375,
0.1251220703125,
0.285888671875,
0.1982421875,
0.5498046875,
-0.139892578125,
0.91259765625,
-0.5693359375,
-0.68603515625,
0.153564453125,
-1.1103515625,
0.58154296875,
-0.658203125,
0.208740234375,
-0.326416015625,
-0.1468505859375,
0.002712249755859375,
0.2100830078125,
-0.153564453125,
0.28271484375,
-0.491455078125,
0.032257080078125,
-0.89697265625,
-0.6865234375,
0.70068359375,
-0.47998046875,
-0.31396484375,
0.72900390625,
-0.0362548828125,
0.1304931640625,
0.365234375,
0.78515625,
-0.292236328125,
0.273193359375,
-0.4189453125,
0.8466796875,
-0.5712890625,
-0.7470703125,
-0.048614501953125,
-0.381103515625,
0.80322265625,
-0.08648681640625,
-0.96875,
-0.43212890625,
-1.1201171875,
-0.279296875,
-0.0275115966796875,
-0.33642578125,
0.2100830078125,
0.1201171875,
-0.2210693359375,
0.12054443359375,
-0.2374267578125,
-0.227294921875,
-0.73388671875,
-0.77099609375,
-0.1434326171875,
0.53076171875,
0.319580078125,
0.98583984375,
0.0295257568359375,
-0.22021484375,
-0.2005615234375,
0.169677734375,
0.0552978515625,
-0.38232421875,
-0.8193359375,
-0.2374267578125,
-0.1849365234375,
-0.5205078125,
0.49951171875,
0.2236328125,
0.289794921875,
0.7939453125,
0.316650390625,
0.163330078125,
0.14208984375,
-0.51708984375,
1.048828125,
0.421142578125,
-1.0205078125,
-0.76171875,
0.4462890625,
0.10748291015625,
0.3984375,
0.4189453125,
-0.857421875,
-0.61474609375,
-1.263671875,
0.466064453125,
-0.298095703125,
0.1876220703125,
-0.92431640625,
-0.94921875,
-0.51904296875,
0.344970703125,
-0.1551513671875,
-0.41357421875,
-0.006809234619140625,
-1.076171875,
0.06121826171875,
-0.8720703125,
-0.215087890625,
-0.748046875,
-0.94921875,
-0.2015380859375,
0.71875,
0.321044921875,
0.305908203125,
-0.059234619140625,
-0.288818359375,
0.7236328125,
-0.1141357421875,
-0.84130859375,
-0.493408203125,
-0.0802001953125,
0.09515380859375,
-0.281494140625,
-0.06689453125,
-0.462890625,
0.83349609375,
-0.97509765625,
0.2529296875,
-0.00958251953125,
-0.46337890625,
-0.268798828125,
-0.32763671875,
1.2548828125,
-0.487548828125,
-0.251953125,
-0.2447509765625,
0.78125,
0.5068359375,
-0.0181732177734375,
-0.443115234375,
-0.8017578125,
-0.73974609375,
-0.9365234375,
0.323974609375,
-0.59423828125,
0.2177734375,
-0.77587890625,
-0.1607666015625,
0.9521484375,
-0.59765625,
0.310546875,
1.267578125,
-0.1439208984375,
0.2247314453125,
-0.7578125,
0.5595703125,
0.474365234375,
-0.65234375,
-0.0819091796875,
0.08612060546875,
-0.6953125,
0.01474761962890625,
-0.14013671875,
-1.2177734375,
-0.466064453125,
0.439453125,
1.5146484375,
-0.43408203125,
0.74169921875,
-0.1536865234375,
-1.2548828125,
-0.79541015625,
0.93359375,
0.421875,
0.87060546875,
1.076171875,
-0.311767578125,
-0.260498046875,
0.002063751220703125,
-0.10345458984375,
-0.5869140625,
-0.4677734375,
1.2890625,
-0.375732421875,
-0.324462890625,
0.71142578125,
0.8701171875,
-1.24609375,
-0.685546875,
0.212646484375,
0.09844970703125,
-0.59716796875,
-0.54833984375,
0.440673828125,
0.68896484375,
-0.58349609375,
0.2308349609375,
0.42236328125,
-0.163330078125,
0.359619140625,
0.232421875,
-0.038482666015625,
-0.55908203125,
0.5693359375,
0.52099609375,
-0.445068359375,
-0.445556640625,
-1.044921875,
-0.5302734375,
-0.337646484375,
0.07220458984375,
0.728515625,
-0.34033203125,
0.342041015625,
0.34716796875,
0.349853515625,
0.5791015625,
-0.315673828125,
-0.63134765625,
0.451171875,
-0.48193359375,
-0.419189453125,
0.294921875,
0.4013671875,
-0.045013427734375,
0.496826171875,
-0.5654296875,
0.061920166015625,
0.350830078125,
0.46142578125,
-0.443115234375,
-0.61279296875,
-0.1851806640625,
-0.99658203125,
-0.45361328125,
-0.60498046875,
-0.97509765625,
-0.271240234375,
0.552734375,
-0.0293426513671875,
-0.71875,
-0.05560302734375,
0.49560546875,
-0.64111328125,
0.82177734375,
-0.437255859375,
0.2073974609375,
-0.48486328125,
-0.51171875,
-0.671875,
-0.06878662109375,
-0.5830078125,
0.2122802734375,
-0.80859375,
0.53271484375,
-0.49658203125,
0.402587890625,
-0.0131072998046875,
0.79443359375,
-0.310791015625,
-0.92529296875,
-0.2509765625,
0.0694580078125,
0.11572265625,
0.92529296875,
0.49560546875,
0.2802734375,
0.12139892578125,
-0.297607421875,
-0.10076904296875,
-0.65087890625,
0.40185546875,
0.76953125,
-0.125244140625,
-0.29833984375,
-0.4794921875,
0.2005615234375,
-0.93896484375,
0.39501953125,
-0.56982421875,
-0.04150390625,
0.390625,
-0.431640625,
0.09002685546875,
1.3046875,
-0.1529541015625,
0.31787109375,
0.2073974609375,
-0.123046875,
0.395263671875,
0.35302734375,
0.125732421875,
0.365234375,
-0.00482940673828125,
-0.352294921875,
-0.08056640625,
0.1417236328125,
0.1387939453125,
0.20166015625,
-0.3798828125,
-0.5556640625,
-1.0546875,
-0.337158203125,
0.07440185546875,
0.31689453125,
0.35400390625,
-0.1607666015625,
-0.12457275390625,
-0.213134765625,
-0.30126953125,
-0.2127685546875,
-1.146484375,
-1.298828125,
0.44287109375,
-0.268310546875,
-0.75146484375,
-0.373046875,
-0.37109375,
-0.1805419921875,
-0.032684326171875,
0.479736328125,
-0.26513671875,
0.77197265625,
-0.1878662109375,
0.578125,
-0.00096893310546875,
-0.246337890625,
0.26904296875,
0.2939453125,
-0.93603515625,
-0.68017578125,
0.2095947265625,
1.4794921875,
0.185302734375,
-0.349365234375,
-0.466064453125,
0.0821533203125,
-0.07080078125,
0.2032470703125,
-0.498046875,
0.1033935546875,
-0.33642578125,
0.28466796875,
-0.67333984375,
-0.334716796875,
-0.2220458984375,
0.4599609375,
0.60009765625,
-0.5732421875,
-0.77294921875,
0.7412109375,
0.6005859375,
-0.2744140625,
0.273681640625,
0.1048583984375,
0.2064208984375,
-0.140625,
-0.042633056640625,
-0.09429931640625,
0.80126953125,
0.92333984375,
0.66064453125,
-0.2578125,
0.362060546875,
0.515625,
-0.0199127197265625,
-0.056884765625,
0.2335205078125,
0.376953125,
0.1258544921875,
-0.18798828125,
-0.63671875,
-0.293212890625,
-0.3974609375,
-0.5849609375,
0.10888671875,
-0.1888427734375,
0.049163818359375,
-0.10400390625,
-0.7841796875,
0.92138671875,
-0.069091796875,
-0.10894775390625,
0.33447265625,
-0.464111328125,
0.138671875,
0.88525390625,
-0.208984375,
-0.46484375,
0.61083984375,
0.8212890625,
0.44384765625,
0.6728515625,
0.00637054443359375,
0.43994140625,
-0.276611328125,
0.1927490234375,
-0.183837890625,
-0.0117950439453125,
-0.6328125,
-0.51025390625,
-0.92724609375,
0.12078857421875,
-0.560546875,
-0.3037109375,
0.61572265625,
-0.4873046875,
0.10845947265625,
-0.05352783203125,
0.83447265625,
-0.60205078125,
0.40087890625,
0.41162109375,
-0.25341796875,
-0.9482421875,
0.7685546875,
-0.23876953125,
0.258056640625,
-0.009429931640625,
0.8056640625,
-0.08709716796875,
1.3544921875,
0.515625,
-0.326416015625,
0.09832763671875,
0.55615234375,
-0.1812744140625,
-0.58740234375,
-0.97998046875,
-0.164306640625,
-0.1976318359375,
-0.96875,
-0.08587646484375,
-0.0292816162109375,
0.81787109375,
-0.0191497802734375,
-0.8349609375,
-0.09747314453125,
0.29638671875,
-1.1630859375,
0.8828125,
0.45458984375,
0.541015625,
-0.07684326171875,
0.02386474609375,
0.118896484375,
-1.134765625,
0.360107421875,
-0.89404296875,
0.5458984375,
-0.7099609375,
-0.487060546875,
-1.009765625,
-0.505859375,
-0.11993408203125,
-0.57568359375,
-0.46240234375,
-1.2060546875,
0.24267578125,
0.603515625,
0.486328125,
0.34716796875,
-0.296630859375,
0.32421875,
1.1953125,
0.830078125,
0.11199951171875,
0.457763671875,
0.53857421875,
0.10906982421875,
-0.289794921875,
0.05322265625,
0.380859375,
-0.07916259765625,
0.281494140625,
-0.20654296875,
0.043487548828125,
-0.57373046875,
-1.626953125,
0.152587890625,
0.60888671875,
0.4384765625,
-0.6787109375,
-0.6591796875,
-0.14404296875,
-0.771484375,
-0.416015625,
-0.52978515625,
0.62744140625,
-0.230224609375,
-0.013458251953125,
-0.296630859375,
-1.185546875,
4.2265625,
0.9208984375,
1.125,
0.2213134765625,
0.398193359375,
1.0615234375,
0.110595703125,
-0.439697265625,
0.12274169921875,
-0.37646484375,
0.310302734375,
-0.456298828125,
0.1448974609375,
0.64404296875,
0.0100250244140625,
0.75927734375,
-0.466796875,
0.30126953125,
-0.03594970703125,
-1.0703125,
-0.98779296875,
0.5703125,
-0.09454345703125,
1.041015625,
0.251220703125,
0.1934814453125,
0.307373046875,
-0.411865234375,
-0.11627197265625,
-0.47705078125,
0.385986328125,
-0.368408203125,
0.9091796875,
0.06329345703125,
-0.07635498046875,
0.362548828125,
0.0195465087890625,
-0.2447509765625,
-0.0340576171875,
0.1727294921875,
0.0677490234375,
0.172607421875,
0.60986328125,
-1.19921875,
-0.248046875,
0.484375,
-0.78955078125,
0.44580078125,
0.1527099609375,
-0.96337890625,
0.708984375,
-0.2626953125,
0.488037109375,
-0.54931640625,
-0.814453125,
-0.06707763671875,
-0.100830078125,
-0.02337646484375,
0.0931396484375,
0.68115234375,
0.693359375,
0.88232421875,
-0.0533447265625,
0.1295166015625,
-1.2001953125,
0.6533203125,
0.3173828125,
0.65673828125,
-0.6376953125,
0.08465576171875,
-0.09326171875,
-0.465087890625,
-0.2265625,
-0.195556640625,
0.63427734375,
0.4453125,
-0.26806640625,
0.8984375,
0.4296875,
-0.673828125,
0.218994140625,
-0.61767578125,
-0.229736328125,
-0.57275390625,
0.70361328125,
1.2490234375,
-0.572265625,
-0.415283203125,
-0.257080078125,
0.427001953125,
0.296630859375,
0.8203125,
0.33447265625,
-0.65673828125,
0.0946044921875
] | 0
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
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 ≤ 100, 1 ≤ k ≤ 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
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.
Submitted Solution:
```
import sys
import math
from collections import defaultdict,deque
import heapq
n,k = map(int,sys.stdin.readline().split())
ss = sys.stdin.readline()[:-1]
dp = [[0 for _ in range(n+1)] for i in range(n+1)]
dp[n][1]=1
dp[n][0]=1
dic=defaultdict(list)
for i in range(n):
dic[ss[i]].append(i+1)
#print(dic,'dic')
for i in range(n-1,0,-1):
#vis=defaultdict(int)
for j in range(1,n+1):
s=0
for x in range(i+1,n+1):
s+=dp[x][j-1]
z=-1
cnt=0
#print(ss[i-1])
#print(dic[ss[i-1]])
for y in dic[ss[i-1]]:
cnt+=1
if y==i:
break
sub=0
if len(dic[ss[i-1]])>cnt:
for q in range(cnt,len(dic[ss[i-1]])):
sub+=dp[dic[ss[i-1]][q]][j]
#print(dic[ss[i-1]][q],'row',j,'col',sub,'sub')
#sub=dp[dic[ss[i-1]][cnt]][j]
#print(cnt,'cnt',i,'i',j,'j',s,'sssss',sub,'sub',cnt,'cnt',dic[ss[i-1]])
dp[i][j]+=s-sub
#print(s,'s',sub,'sub')
'''for i in range(n+1):
print(dp[i],'dp')'''
cost=0
for i in range(n,-1,-1):
for j in range(n+1):
#print(k,'k',i,'i',j,'j')
if dp[j][i]<=k:
cost+=(n-i)*dp[j][i]
k-=dp[j][i]
else:
#print(n,'n',i,'i',k,'k')
cost+=k*(n-i)
k=0
break
#print(cost,'cost',k,'k')
if k!=0:
print(-1)
else:
print(cost)
```
Yes
| 105
|
[
-0.072021484375,
0.3310546875,
0.194580078125,
0.367919921875,
-0.1759033203125,
-0.0845947265625,
-0.226318359375,
-0.1673583984375,
0.384765625,
0.75,
0.69189453125,
-0.330810546875,
-0.249267578125,
-1.083984375,
-0.716796875,
0.160888671875,
-0.6640625,
-0.80712890625,
-0.40478515625,
-0.30029296875,
0.08795166015625,
-0.1256103515625,
-1.2705078125,
-0.312744140625,
0.200439453125,
1.12109375,
0.30908203125,
0.192138671875,
1.1328125,
1.2119140625,
-0.25634765625,
-0.84716796875,
0.443115234375,
-1.0537109375,
-0.336181640625,
-0.1466064453125,
0.396240234375,
-0.509765625,
-0.467041015625,
-0.2381591796875,
0.345947265625,
-0.71044921875,
0.529296875,
-0.482177734375,
-1.3037109375,
-0.234130859375,
-0.0272674560546875,
-0.385986328125,
-0.50048828125,
-0.1839599609375,
0.060577392578125,
-0.12445068359375,
0.521484375,
-0.046661376953125,
0.22509765625,
0.40576171875,
0.321044921875,
-0.328369140625,
-0.80859375,
0.46728515625,
0.1766357421875,
-0.0401611328125,
0.0865478515625,
-0.71826171875,
-0.5146484375,
0.414306640625,
-0.32568359375,
0.08929443359375,
0.04449462890625,
-0.60546875,
-0.2025146484375,
-0.031005859375,
-0.16796875,
-0.5693359375,
-0.1591796875,
-0.363525390625,
0.134765625,
-0.051788330078125,
-0.6455078125,
0.68994140625,
0.05072021484375,
1.0087890625,
0.42431640625,
0.0703125,
-0.67138671875,
-0.53515625,
0.0673828125,
0.031036376953125,
0.21142578125,
0.1728515625,
-0.6005859375,
0.828125,
-0.70068359375,
-0.0716552734375,
0.41943359375,
0.6728515625,
-0.5615234375,
0.252685546875,
0.263671875,
0.07330322265625,
0.52685546875,
0.6982421875,
-0.50537109375,
1.279296875,
-0.050018310546875,
-0.193603515625,
0.242431640625,
-0.43359375,
-0.286376953125,
-0.408447265625,
-0.19970703125,
-0.492919921875,
0.411865234375,
-0.09381103515625,
-0.413818359375,
0.97705078125,
-0.1939697265625,
0.5751953125,
-0.60400390625,
0.38037109375,
0.55029296875,
0.1260986328125,
0.6220703125,
-0.1478271484375,
-0.09722900390625,
-0.3505859375,
-0.62060546875,
1.12890625,
-0.599609375,
0.2509765625,
0.4736328125,
-0.2132568359375,
-0.40478515625,
0.365966796875,
-0.26806640625,
0.5908203125,
-0.1739501953125,
0.4638671875,
0.69189453125,
-0.560546875,
0.65869140625,
-0.23291015625,
-0.0250244140625,
1.673828125,
0.50244140625,
0.2222900390625,
0.86474609375,
0.385009765625,
-0.56884765625,
0.51806640625,
-0.96533203125,
0.59423828125,
0.353515625,
-0.051727294921875,
-0.363525390625,
-0.00586700439453125,
-0.1546630859375,
0.59716796875,
0.149658203125,
0.372314453125,
-0.417724609375,
0.29345703125,
-0.358642578125,
0.32421875,
-0.55029296875,
0.7724609375,
-0.1495361328125,
-0.281494140625,
0.1068115234375,
-0.63720703125,
0.34423828125,
-0.1075439453125,
-0.01210784912109375,
0.81884765625,
0.483642578125,
1.015625,
0.951171875,
-0.04486083984375,
0.36572265625,
0.61767578125,
-0.33935546875,
0.18603515625,
0.423828125,
0.59228515625,
0.82470703125,
0.1295166015625,
0.0516357421875,
-0.08111572265625,
-0.56201171875,
-0.77197265625,
0.2861328125,
0.75439453125,
-0.31689453125,
0.238037109375,
-0.16455078125,
-0.00788116455078125,
-1.1357421875,
-0.1705322265625,
-0.0484619140625,
-0.9267578125,
-0.6826171875,
0.576171875,
0.217529296875,
0.2646484375,
-0.6171875,
-0.92919921875,
0.64306640625,
1.1318359375,
-0.4755859375,
0.11993408203125,
0.78857421875,
0.1446533203125,
-0.4599609375,
0.466064453125,
-0.009552001953125,
-0.408447265625,
-0.62744140625,
0.46240234375,
-0.2274169921875,
-0.1900634765625,
-0.352294921875,
0.638671875,
0.17138671875,
0.634765625,
-0.0299835205078125,
-0.1263427734375,
0.2271728515625,
1.4736328125,
0.2332763671875,
0.3037109375,
0.22998046875,
0.76220703125,
0.529296875,
0.6123046875,
0.28857421875,
0.224853515625,
0.62548828125,
1.337890625,
-0.142333984375,
0.54345703125,
0.14501953125,
0.493896484375,
0.57568359375,
0.070068359375,
0.2271728515625,
0.5302734375,
0.0081939697265625,
-0.051483154296875,
-0.8251953125,
0.1339111328125,
-0.351806640625,
0.697265625,
-0.0158843994140625,
0.501953125,
-0.2098388671875,
-0.35693359375,
0.413818359375,
1.1650390625,
-1.107421875,
-0.79345703125,
-0.266357421875,
0.058441162109375,
0.4267578125,
0.06756591796875,
0.40966796875,
0.3779296875,
0.375244140625,
0.6025390625,
-0.172119140625,
-0.1739501953125,
-0.487060546875,
-0.95068359375,
-0.599609375,
-0.234619140625,
-0.56103515625,
-0.0224761962890625,
0.1190185546875,
-0.890625,
0.392578125,
-0.55078125,
-0.07281494140625,
-0.06756591796875,
-0.69140625,
0.48046875,
-0.41845703125,
0.421630859375,
-0.65771484375,
0.6171875,
0.64990234375,
1.0654296875,
-0.41748046875,
-0.09210205078125,
0.0186767578125,
-0.92724609375,
0.285888671875,
0.1689453125,
0.276611328125,
-0.0167999267578125,
-0.392578125,
-0.77197265625,
-0.57861328125,
0.0248260498046875,
-0.309326171875,
0.07550048828125,
-0.49072265625,
0.6806640625,
0.494384765625,
-0.71484375,
0.716796875,
0.66552734375,
-0.92919921875,
0.2330322265625,
0.08673095703125,
0.342041015625,
-0.88037109375,
1.2294921875,
0.371826171875,
0.6083984375,
-0.61669921875,
-0.515625,
-0.434814453125,
-0.09429931640625,
0.1798095703125,
-0.49072265625,
-0.2235107421875,
0.45849609375,
-0.10821533203125,
-1.29296875,
0.509765625,
-1.1455078125,
-0.383544921875,
-0.253662109375,
-0.11663818359375,
0.599609375,
0.931640625,
0.08056640625,
-0.6396484375,
-0.359375,
0.1187744140625,
1.0771484375,
0.79248046875,
-0.88232421875,
0.084228515625,
0.78662109375,
0.2095947265625,
0.1800537109375,
0.251953125,
-0.42431640625,
-0.336181640625,
0.2431640625,
0.04974365234375,
0.9091796875,
0.16552734375,
0.55810546875,
0.65380859375,
0.6162109375,
-0.69921875,
0.07666015625,
-0.37646484375,
1.1435546875,
0.7431640625,
0.352783203125,
0.385498046875,
-0.364990234375,
-0.4833984375,
-1.0390625,
0.42578125,
0.03338623046875,
0.82470703125,
-0.60986328125,
0.7529296875,
0.30712890625,
-0.6943359375,
0.65234375,
-0.87548828125,
0.000804901123046875,
1.3056640625,
-0.425537109375,
0.5478515625,
-0.5849609375,
0.28662109375,
-0.14453125,
0.0587158203125,
0.2457275390625,
0.81787109375,
0.7607421875,
-0.455810546875,
-0.381591796875,
-0.5400390625,
0.1622314453125,
0.0638427734375,
-0.60791015625,
0.339599609375,
-0.293212890625,
-0.407958984375,
-0.80029296875,
-0.008514404296875,
1.0693359375,
0.80078125,
0.07818603515625,
0.1783447265625,
0.1842041015625,
0.16162109375,
0.60498046875,
-0.479248046875,
0.100341796875,
-0.662109375,
1.048828125,
0.20263671875,
-0.08685302734375,
0.06231689453125,
-0.240478515625,
-0.18798828125,
0.223876953125,
-0.0259246826171875,
-0.216796875,
-0.994140625,
0.11962890625,
-0.1441650390625,
0.29638671875,
-0.64794921875,
-0.0224761962890625,
-0.43408203125,
0.5986328125,
0.1590576171875,
-1.322265625,
-0.08013916015625,
-1.0302734375,
0.337158203125,
0.2069091796875,
0.055145263671875,
-0.517578125,
-0.11468505859375,
-0.640625,
-0.76806640625,
0.430419921875,
0.66943359375,
-0.390625,
-0.00707244873046875,
-1.4130859375,
0.6650390625,
0.317626953125,
-0.0513916015625,
0.3671875,
0.17529296875,
0.57080078125,
-0.182861328125,
0.78271484375,
-0.52978515625,
-0.57177734375,
0.1536865234375,
-1.0712890625,
0.576171875,
-0.6142578125,
0.315185546875,
-0.181396484375,
-0.1673583984375,
-0.05084228515625,
0.23388671875,
-0.2310791015625,
0.40673828125,
-0.474365234375,
0.1143798828125,
-0.95947265625,
-0.71142578125,
0.73583984375,
-0.51171875,
-0.2861328125,
0.8623046875,
-0.0280609130859375,
-0.026275634765625,
0.31591796875,
0.55419921875,
-0.337890625,
0.466796875,
-0.41064453125,
0.77587890625,
-0.49365234375,
-0.638671875,
-0.021575927734375,
-0.360107421875,
0.935546875,
-0.222412109375,
-0.912109375,
-0.446044921875,
-1.15234375,
-0.301513671875,
-0.01450347900390625,
-0.497802734375,
0.106689453125,
0.1158447265625,
-0.256591796875,
0.033966064453125,
-0.275146484375,
-0.2734375,
-0.60400390625,
-0.52783203125,
-0.1968994140625,
0.491455078125,
0.31689453125,
0.89013671875,
0.1285400390625,
-0.2294921875,
-0.2215576171875,
0.053558349609375,
0.0035419464111328125,
-0.382568359375,
-0.77490234375,
-0.25927734375,
-0.350830078125,
-0.5693359375,
0.63671875,
0.16015625,
0.32470703125,
0.7783203125,
0.336669921875,
0.230224609375,
0.10052490234375,
-0.552734375,
1.1806640625,
0.294189453125,
-1.1259765625,
-0.6767578125,
0.60400390625,
0.061187744140625,
0.445068359375,
0.226806640625,
-0.7958984375,
-0.63671875,
-1.3349609375,
0.6142578125,
-0.42822265625,
0.0748291015625,
-1.029296875,
-0.94677734375,
-0.4794921875,
0.493896484375,
-0.16259765625,
-0.42431640625,
0.1259765625,
-1.1767578125,
0.0946044921875,
-0.8984375,
-0.26904296875,
-0.75146484375,
-0.82080078125,
-0.1383056640625,
0.89892578125,
0.25244140625,
0.36279296875,
0.00971221923828125,
-0.0445556640625,
0.6123046875,
0.021240234375,
-0.865234375,
-0.447021484375,
-0.1180419921875,
0.035888671875,
-0.29833984375,
0.0101776123046875,
-0.499267578125,
0.75537109375,
-0.90625,
0.27783203125,
0.09027099609375,
-0.336181640625,
-0.1202392578125,
-0.333740234375,
1.17578125,
-0.54443359375,
-0.259033203125,
-0.37158203125,
0.62451171875,
0.57275390625,
0.051422119140625,
-0.36669921875,
-0.806640625,
-0.7001953125,
-1.001953125,
0.382080078125,
-0.461669921875,
0.3056640625,
-0.9326171875,
-0.267333984375,
1.1064453125,
-0.68701171875,
0.28857421875,
1.171875,
0.0009589195251464844,
0.2548828125,
-0.68359375,
0.5283203125,
0.5517578125,
-0.68896484375,
-0.1002197265625,
0.23291015625,
-0.73291015625,
-0.026611328125,
-0.330078125,
-1.2353515625,
-0.49658203125,
0.48681640625,
1.3310546875,
-0.380126953125,
0.80810546875,
-0.200439453125,
-1.2587890625,
-0.63525390625,
0.98193359375,
0.326416015625,
0.72216796875,
1.0361328125,
-0.237548828125,
-0.4140625,
0.06463623046875,
-0.14892578125,
-0.5322265625,
-0.39306640625,
1.33203125,
-0.693359375,
-0.461669921875,
0.89892578125,
0.79345703125,
-1.3876953125,
-0.63671875,
0.1456298828125,
0.1912841796875,
-0.44287109375,
-0.68603515625,
0.410400390625,
0.7685546875,
-0.398681640625,
0.2152099609375,
0.41015625,
-0.253173828125,
0.392578125,
0.30322265625,
0.06378173828125,
-0.62939453125,
0.43115234375,
0.6181640625,
-0.490234375,
-0.57421875,
-0.98046875,
-0.3828125,
-0.1798095703125,
0.097900390625,
0.802734375,
-0.419677734375,
0.4814453125,
0.44677734375,
0.383544921875,
0.60205078125,
-0.43798828125,
-0.340576171875,
0.49365234375,
-0.474365234375,
-0.40576171875,
0.2452392578125,
0.404296875,
-0.05810546875,
0.58154296875,
-0.5009765625,
0.002910614013671875,
0.352294921875,
0.58349609375,
-0.375244140625,
-0.489990234375,
-0.1864013671875,
-1.076171875,
-0.5517578125,
-0.5068359375,
-1.0751953125,
-0.07086181640625,
0.5712890625,
0.1541748046875,
-0.72314453125,
-0.031494140625,
0.5849609375,
-0.765625,
0.85205078125,
-0.56005859375,
0.1156005859375,
-0.421630859375,
-0.36328125,
-0.73779296875,
-0.0662841796875,
-0.73388671875,
-0.022064208984375,
-0.60009765625,
0.5068359375,
-0.406982421875,
0.29052734375,
-0.041168212890625,
0.89501953125,
-0.258056640625,
-0.662109375,
-0.28662109375,
0.1044921875,
0.035400390625,
0.927734375,
0.380859375,
0.3759765625,
0.1219482421875,
-0.239501953125,
-0.10589599609375,
-0.43994140625,
0.52587890625,
0.7666015625,
-0.0740966796875,
-0.341796875,
-0.418212890625,
0.0916748046875,
-0.89306640625,
0.4619140625,
-0.5478515625,
0.0295867919921875,
0.470947265625,
-0.416259765625,
0.190185546875,
1.37109375,
-0.269775390625,
0.3642578125,
0.314453125,
-0.248779296875,
0.261962890625,
0.49267578125,
0.1328125,
0.438720703125,
-0.1197509765625,
-0.4248046875,
-0.032318115234375,
0.05047607421875,
0.2349853515625,
0.164794921875,
-0.51220703125,
-0.66162109375,
-1.212890625,
-0.2178955078125,
0.1790771484375,
0.322021484375,
0.42822265625,
-0.251708984375,
-0.15478515625,
-0.253173828125,
-0.4873046875,
-0.1427001953125,
-1.26953125,
-1.4384765625,
0.56640625,
-0.26513671875,
-0.8046875,
-0.265380859375,
-0.267333984375,
-0.1260986328125,
-0.038848876953125,
0.3359375,
-0.2493896484375,
0.60546875,
-0.0966796875,
0.609375,
0.09075927734375,
-0.265625,
0.2152099609375,
0.392822265625,
-1.056640625,
-0.52099609375,
0.0849609375,
1.5478515625,
0.141845703125,
-0.319580078125,
-0.3779296875,
0.010009765625,
-0.05120849609375,
0.258544921875,
-0.53125,
0.033294677734375,
-0.189208984375,
0.389404296875,
-0.69580078125,
-0.339111328125,
-0.093505859375,
0.460693359375,
0.515625,
-0.59228515625,
-0.7646484375,
0.8154296875,
0.755859375,
-0.08367919921875,
0.398193359375,
0.1087646484375,
0.33349609375,
-0.0384521484375,
0.053070068359375,
-0.1666259765625,
0.69775390625,
0.96728515625,
0.69775390625,
-0.251708984375,
0.483154296875,
0.64453125,
-0.0927734375,
0.1527099609375,
0.2666015625,
0.380859375,
0.05224609375,
-0.10552978515625,
-0.59423828125,
-0.51708984375,
-0.201171875,
-0.564453125,
0.14404296875,
-0.2193603515625,
0.06268310546875,
-0.171875,
-0.70458984375,
0.9599609375,
-0.328857421875,
-0.0831298828125,
0.32763671875,
-0.485595703125,
0.230224609375,
0.7802734375,
-0.099365234375,
-0.5146484375,
0.73828125,
0.91943359375,
0.465087890625,
0.71923828125,
0.160888671875,
0.316162109375,
-0.396484375,
0.2098388671875,
-0.1630859375,
0.05047607421875,
-0.59375,
-0.48193359375,
-1.0107421875,
0.27197265625,
-0.40869140625,
-0.25244140625,
0.459716796875,
-0.51171875,
-0.01401519775390625,
-0.17822265625,
0.9658203125,
-0.7490234375,
0.36865234375,
0.389892578125,
-0.0999755859375,
-1.037109375,
0.861328125,
-0.183837890625,
0.2371826171875,
0.1475830078125,
0.818359375,
0.09454345703125,
1.34765625,
0.65869140625,
-0.362060546875,
0.134033203125,
0.640625,
-0.144775390625,
-0.5654296875,
-1.04296875,
-0.1357421875,
-0.26611328125,
-0.89013671875,
-0.152587890625,
-0.1915283203125,
0.84228515625,
-0.03851318359375,
-0.7763671875,
-0.2000732421875,
0.215087890625,
-1.0439453125,
1.0498046875,
0.328857421875,
0.5986328125,
0.01352691650390625,
-0.08984375,
0.08795166015625,
-1.091796875,
0.40576171875,
-0.67236328125,
0.525390625,
-0.80908203125,
-0.38720703125,
-1.1328125,
-0.452880859375,
-0.056640625,
-0.455078125,
-0.361572265625,
-1.1337890625,
0.248291015625,
0.52685546875,
0.418701171875,
0.2442626953125,
-0.407958984375,
0.374755859375,
1.1865234375,
0.74609375,
-0.029052734375,
0.572265625,
0.57470703125,
0.1513671875,
-0.4267578125,
0.1400146484375,
0.425048828125,
-0.10498046875,
0.267333984375,
-0.388671875,
0.02069091796875,
-0.6435546875,
-1.5849609375,
0.1480712890625,
0.60595703125,
0.305419921875,
-0.72607421875,
-0.8369140625,
-0.275390625,
-0.67529296875,
-0.2166748046875,
-0.6396484375,
0.64306640625,
-0.224609375,
-0.06048583984375,
-0.38427734375,
-1.1103515625,
4.17578125,
0.93701171875,
0.98876953125,
0.0865478515625,
0.5361328125,
1.150390625,
0.1470947265625,
-0.599609375,
0.21240234375,
-0.424072265625,
0.409912109375,
-0.35498046875,
0.2314453125,
0.7255859375,
0.06488037109375,
0.79150390625,
-0.4697265625,
0.047576904296875,
0.09307861328125,
-1.2578125,
-1.0234375,
0.53662109375,
-0.051910400390625,
0.873046875,
0.2216796875,
0.2958984375,
0.6123046875,
-0.497314453125,
-0.150146484375,
-0.44775390625,
0.271484375,
-0.381591796875,
0.921875,
0.1092529296875,
-0.10931396484375,
0.434326171875,
0.014678955078125,
-0.361572265625,
-0.06011962890625,
0.209716796875,
0.11566162109375,
0.2333984375,
0.5771484375,
-1.1103515625,
-0.254150390625,
0.55908203125,
-0.7421875,
0.430419921875,
0.1510009765625,
-1.068359375,
0.75390625,
-0.2626953125,
0.435302734375,
-0.400146484375,
-0.86767578125,
-0.0220184326171875,
-0.060455322265625,
-0.0140228271484375,
-0.00836181640625,
0.64306640625,
0.525390625,
0.8994140625,
-0.1376953125,
0.34716796875,
-1.2509765625,
0.435302734375,
0.35400390625,
0.58056640625,
-0.71875,
0.264404296875,
-0.2264404296875,
-0.505859375,
-0.351806640625,
-0.197265625,
0.64892578125,
0.46728515625,
-0.29150390625,
0.783203125,
0.471923828125,
-0.61572265625,
0.236572265625,
-0.64501953125,
-0.1947021484375,
-0.77587890625,
0.576171875,
1.1904296875,
-0.6845703125,
-0.5087890625,
-0.258056640625,
0.389404296875,
0.390625,
0.8779296875,
0.302734375,
-0.72314453125,
-0.08648681640625
] | 0
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
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 ≤ 100, 1 ≤ k ≤ 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
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.
Submitted Solution:
```
#import sys
#input = sys.stdin.buffer.readline
def main():
n, K = map(int, input().split())
s = str(input())
next = [[10**18]*26 for _ in range(n+1)]
for i in reversed(range(n)):
for j in range(26):
next[i][j] = next[i+1][j]
if s[i] == chr(j+ord('a')):
next[i][j] = i
#print(next)
dp = [[0]*(n+1) for i in range(n+1)]
dp[0][0] = 1
for i in range(n):
for j in range(n+1):
#dp[i+1][j] += dp[i][j]
for k in range(26):
if next[i][k] >= n:
continue
else:
if j+1 <= n:
dp[next[i][k]+1][j+1] += dp[i][j]
#print(dp)
d = {}
V = [0]*(n+1)
for i in range(n+1):
for j in range(n+1):
V[j] += dp[i][j]
V.reverse()
#print(V)
ans = 0
for i, v in enumerate(V):
if v >= K:
ans += K*i
break
else:
ans += v*i
K -= v
else:
print(-1)
exit()
print(ans)
if __name__ == '__main__':
main()
```
Yes
| 106
|
[
-0.057220458984375,
0.304931640625,
0.2490234375,
0.33740234375,
-0.2039794921875,
-0.131591796875,
-0.27001953125,
-0.1019287109375,
0.321533203125,
0.74365234375,
0.68115234375,
-0.28662109375,
-0.223876953125,
-1.078125,
-0.64599609375,
0.1512451171875,
-0.63916015625,
-0.818359375,
-0.372314453125,
-0.3125,
0.060089111328125,
-0.1378173828125,
-1.310546875,
-0.330078125,
0.1927490234375,
1.0810546875,
0.308837890625,
0.1668701171875,
1.09375,
1.2236328125,
-0.2822265625,
-0.88818359375,
0.432861328125,
-1.029296875,
-0.342041015625,
-0.1881103515625,
0.3642578125,
-0.5244140625,
-0.5234375,
-0.26806640625,
0.325927734375,
-0.75048828125,
0.6103515625,
-0.5185546875,
-1.328125,
-0.2313232421875,
-0.004405975341796875,
-0.402099609375,
-0.55224609375,
-0.12841796875,
0.09075927734375,
-0.0643310546875,
0.483642578125,
-0.10504150390625,
0.198974609375,
0.431884765625,
0.31787109375,
-0.32470703125,
-0.77392578125,
0.5029296875,
0.195556640625,
-0.060943603515625,
0.1031494140625,
-0.6767578125,
-0.499755859375,
0.4423828125,
-0.339111328125,
0.1331787109375,
0.057403564453125,
-0.57958984375,
-0.1968994140625,
-0.0408935546875,
-0.174560546875,
-0.5830078125,
-0.1318359375,
-0.406005859375,
0.16748046875,
0.0192108154296875,
-0.69482421875,
0.673828125,
0.022552490234375,
1.0498046875,
0.409423828125,
0.04754638671875,
-0.64306640625,
-0.56494140625,
0.0418701171875,
0.06170654296875,
0.25048828125,
0.1634521484375,
-0.58251953125,
0.80712890625,
-0.71875,
-0.090087890625,
0.353515625,
0.6318359375,
-0.59521484375,
0.25439453125,
0.2440185546875,
0.07763671875,
0.489990234375,
0.658203125,
-0.52685546875,
1.2353515625,
-0.062744140625,
-0.239013671875,
0.28173828125,
-0.392333984375,
-0.244384765625,
-0.404541015625,
-0.1759033203125,
-0.448974609375,
0.364013671875,
-0.0770263671875,
-0.452392578125,
0.9423828125,
-0.1707763671875,
0.63134765625,
-0.59423828125,
0.39404296875,
0.55029296875,
0.062042236328125,
0.63037109375,
-0.137939453125,
-0.0743408203125,
-0.34814453125,
-0.59033203125,
1.0888671875,
-0.57958984375,
0.246337890625,
0.46826171875,
-0.20849609375,
-0.3779296875,
0.345703125,
-0.305908203125,
0.65234375,
-0.2027587890625,
0.43115234375,
0.6103515625,
-0.56787109375,
0.66552734375,
-0.2314453125,
0.0239410400390625,
1.7373046875,
0.4853515625,
0.2030029296875,
0.9052734375,
0.3583984375,
-0.5576171875,
0.5517578125,
-0.99560546875,
0.62939453125,
0.3466796875,
-0.03668212890625,
-0.321044921875,
-0.003314971923828125,
-0.1114501953125,
0.58251953125,
0.184326171875,
0.414794921875,
-0.400634765625,
0.34912109375,
-0.352294921875,
0.317626953125,
-0.544921875,
0.787109375,
-0.091552734375,
-0.32568359375,
0.09356689453125,
-0.6201171875,
0.3583984375,
-0.11920166015625,
-0.0007410049438476562,
0.763671875,
0.50244140625,
1.0400390625,
1.0048828125,
0.004146575927734375,
0.39013671875,
0.673828125,
-0.431884765625,
0.224365234375,
0.43701171875,
0.5703125,
0.90673828125,
0.2091064453125,
0.07659912109375,
-0.07574462890625,
-0.57080078125,
-0.75244140625,
0.257080078125,
0.75146484375,
-0.2587890625,
0.31884765625,
-0.0885009765625,
-0.00930023193359375,
-1.13671875,
-0.20166015625,
-0.058135986328125,
-0.93212890625,
-0.69580078125,
0.55908203125,
0.2161865234375,
0.28515625,
-0.603515625,
-0.87451171875,
0.64208984375,
1.177734375,
-0.44482421875,
0.1282958984375,
0.767578125,
0.158447265625,
-0.442626953125,
0.418212890625,
-0.009033203125,
-0.39697265625,
-0.6171875,
0.480712890625,
-0.219482421875,
-0.213623046875,
-0.309326171875,
0.6484375,
0.1898193359375,
0.654296875,
-0.1318359375,
-0.2027587890625,
0.210205078125,
1.4736328125,
0.244384765625,
0.340576171875,
0.2052001953125,
0.76953125,
0.53662109375,
0.62060546875,
0.25830078125,
0.2198486328125,
0.568359375,
1.3154296875,
-0.12115478515625,
0.56298828125,
0.11956787109375,
0.4677734375,
0.5283203125,
0.1248779296875,
0.2442626953125,
0.470703125,
-0.0101776123046875,
-0.0723876953125,
-0.82080078125,
0.0753173828125,
-0.4111328125,
0.71875,
-0.031402587890625,
0.482421875,
-0.155517578125,
-0.373779296875,
0.4736328125,
1.171875,
-1.1669921875,
-0.77783203125,
-0.236572265625,
0.03216552734375,
0.35546875,
0.0953369140625,
0.420654296875,
0.32470703125,
0.383056640625,
0.58740234375,
-0.196044921875,
-0.2098388671875,
-0.44482421875,
-0.95947265625,
-0.654296875,
-0.2401123046875,
-0.59375,
-0.0888671875,
0.1358642578125,
-0.94677734375,
0.358642578125,
-0.63427734375,
-0.00989532470703125,
-0.06317138671875,
-0.66259765625,
0.481201171875,
-0.4296875,
0.372802734375,
-0.66015625,
0.65087890625,
0.70068359375,
1.111328125,
-0.39306640625,
-0.0972900390625,
-0.08636474609375,
-0.93212890625,
0.276123046875,
0.0721435546875,
0.2413330078125,
-0.00951385498046875,
-0.4111328125,
-0.82861328125,
-0.6005859375,
0.039581298828125,
-0.395751953125,
0.0821533203125,
-0.47998046875,
0.68408203125,
0.46142578125,
-0.70458984375,
0.66796875,
0.70751953125,
-0.89404296875,
0.2337646484375,
0.0333251953125,
0.330322265625,
-0.8623046875,
1.25390625,
0.358642578125,
0.6474609375,
-0.62841796875,
-0.576171875,
-0.443359375,
-0.045379638671875,
0.25927734375,
-0.4560546875,
-0.2386474609375,
0.46240234375,
-0.04473876953125,
-1.2705078125,
0.52490234375,
-1.1640625,
-0.35302734375,
-0.2135009765625,
-0.2176513671875,
0.630859375,
0.94384765625,
0.0882568359375,
-0.63330078125,
-0.408203125,
0.064697265625,
1.0322265625,
0.7939453125,
-0.8515625,
0.13720703125,
0.8505859375,
0.1751708984375,
0.1497802734375,
0.310791015625,
-0.477294921875,
-0.29443359375,
0.2320556640625,
0.08197021484375,
0.89794921875,
0.189208984375,
0.5283203125,
0.6123046875,
0.615234375,
-0.7734375,
0.0684814453125,
-0.371826171875,
1.0859375,
0.7685546875,
0.3564453125,
0.408935546875,
-0.318603515625,
-0.476318359375,
-1.0146484375,
0.469482421875,
-0.004787445068359375,
0.84912109375,
-0.56494140625,
0.74853515625,
0.2822265625,
-0.669921875,
0.62353515625,
-0.88671875,
-0.0307464599609375,
1.306640625,
-0.3779296875,
0.54248046875,
-0.55322265625,
0.26220703125,
-0.10186767578125,
0.0200347900390625,
0.21923828125,
0.806640625,
0.74755859375,
-0.4541015625,
-0.37646484375,
-0.55322265625,
0.1417236328125,
0.042938232421875,
-0.61572265625,
0.295654296875,
-0.25537109375,
-0.404541015625,
-0.8408203125,
-0.01122283935546875,
1.060546875,
0.80419921875,
0.11419677734375,
0.1656494140625,
0.129638671875,
0.22607421875,
0.64208984375,
-0.454345703125,
0.08197021484375,
-0.677734375,
1.0888671875,
0.1773681640625,
-0.0682373046875,
0.0296630859375,
-0.2001953125,
-0.1678466796875,
0.23779296875,
-0.0677490234375,
-0.2335205078125,
-1.0224609375,
0.057220458984375,
-0.11572265625,
0.2154541015625,
-0.69189453125,
-0.0068359375,
-0.4287109375,
0.60107421875,
0.1650390625,
-1.3115234375,
-0.12158203125,
-1.0361328125,
0.34228515625,
0.2078857421875,
0.017578125,
-0.499267578125,
-0.1156005859375,
-0.6474609375,
-0.7841796875,
0.449462890625,
0.67822265625,
-0.33740234375,
-0.03167724609375,
-1.39453125,
0.7265625,
0.336181640625,
-0.1103515625,
0.346923828125,
0.1112060546875,
0.53369140625,
-0.13525390625,
0.7841796875,
-0.50244140625,
-0.6259765625,
0.151123046875,
-1.08203125,
0.59521484375,
-0.6396484375,
0.3046875,
-0.2022705078125,
-0.177978515625,
-0.0570068359375,
0.215576171875,
-0.1968994140625,
0.372802734375,
-0.474609375,
0.126220703125,
-0.96435546875,
-0.75244140625,
0.67138671875,
-0.4921875,
-0.322265625,
0.81201171875,
-0.0005850791931152344,
0.0095062255859375,
0.3076171875,
0.6181640625,
-0.31591796875,
0.458740234375,
-0.44921875,
0.7734375,
-0.450439453125,
-0.67138671875,
-0.10345458984375,
-0.34521484375,
0.87841796875,
-0.188720703125,
-0.94384765625,
-0.4052734375,
-1.2060546875,
-0.28662109375,
-0.055328369140625,
-0.477783203125,
0.155517578125,
0.14208984375,
-0.2393798828125,
0.089111328125,
-0.293212890625,
-0.2529296875,
-0.5419921875,
-0.595703125,
-0.233642578125,
0.50390625,
0.26904296875,
0.9248046875,
0.1595458984375,
-0.23974609375,
-0.266845703125,
-0.0245819091796875,
0.0206146240234375,
-0.338623046875,
-0.79150390625,
-0.27294921875,
-0.349609375,
-0.595703125,
0.66015625,
0.16015625,
0.38330078125,
0.7587890625,
0.297119140625,
0.258544921875,
0.140380859375,
-0.5830078125,
1.1904296875,
0.330078125,
-1.1318359375,
-0.609375,
0.51953125,
0.037628173828125,
0.448974609375,
0.2449951171875,
-0.81494140625,
-0.6533203125,
-1.314453125,
0.642578125,
-0.427734375,
0.09747314453125,
-1.0263671875,
-0.990234375,
-0.455810546875,
0.477783203125,
-0.18408203125,
-0.407958984375,
0.07611083984375,
-1.16796875,
0.0589599609375,
-0.89306640625,
-0.2496337890625,
-0.7626953125,
-0.8603515625,
-0.1343994140625,
0.88623046875,
0.29052734375,
0.329833984375,
0.00879669189453125,
-0.06390380859375,
0.642578125,
0.0202789306640625,
-0.85498046875,
-0.458251953125,
-0.103759765625,
0.08544921875,
-0.3291015625,
-0.04266357421875,
-0.52490234375,
0.748046875,
-0.93359375,
0.3125,
0.06500244140625,
-0.337890625,
-0.110107421875,
-0.32177734375,
1.2119140625,
-0.57861328125,
-0.2374267578125,
-0.375,
0.681640625,
0.66162109375,
0.102783203125,
-0.357421875,
-0.79443359375,
-0.71875,
-0.9794921875,
0.363525390625,
-0.46875,
0.293212890625,
-0.9013671875,
-0.21826171875,
1.0703125,
-0.71533203125,
0.3330078125,
1.201171875,
-0.004138946533203125,
0.221435546875,
-0.74560546875,
0.55419921875,
0.56591796875,
-0.67724609375,
-0.078125,
0.1505126953125,
-0.7763671875,
-0.0137786865234375,
-0.326416015625,
-1.240234375,
-0.48828125,
0.447021484375,
1.37109375,
-0.35595703125,
0.85595703125,
-0.2388916015625,
-1.25,
-0.58056640625,
1.046875,
0.318603515625,
0.70458984375,
1.08203125,
-0.2386474609375,
-0.37353515625,
0.11358642578125,
-0.1954345703125,
-0.53662109375,
-0.33203125,
1.341796875,
-0.70068359375,
-0.433837890625,
0.90625,
0.80908203125,
-1.416015625,
-0.70166015625,
0.0870361328125,
0.158935546875,
-0.468994140625,
-0.66064453125,
0.3544921875,
0.72705078125,
-0.427978515625,
0.1929931640625,
0.441650390625,
-0.2010498046875,
0.4462890625,
0.301513671875,
-0.0096588134765625,
-0.6630859375,
0.486328125,
0.65087890625,
-0.485107421875,
-0.552734375,
-1.0400390625,
-0.364501953125,
-0.2421875,
0.10223388671875,
0.7783203125,
-0.381103515625,
0.464111328125,
0.494140625,
0.31396484375,
0.6181640625,
-0.481201171875,
-0.36328125,
0.458740234375,
-0.5146484375,
-0.39794921875,
0.1890869140625,
0.445068359375,
-0.0335693359375,
0.60986328125,
-0.51904296875,
-0.0177001953125,
0.336669921875,
0.57861328125,
-0.350341796875,
-0.4453125,
-0.1651611328125,
-1.0810546875,
-0.54736328125,
-0.482177734375,
-1.01171875,
-0.10211181640625,
0.57568359375,
0.08575439453125,
-0.67529296875,
-0.0242156982421875,
0.53125,
-0.7685546875,
0.85693359375,
-0.55615234375,
0.150146484375,
-0.450927734375,
-0.383056640625,
-0.63720703125,
-0.04486083984375,
-0.76806640625,
-0.055999755859375,
-0.5576171875,
0.485595703125,
-0.41162109375,
0.298095703125,
-0.0445556640625,
0.86474609375,
-0.215087890625,
-0.66943359375,
-0.237548828125,
0.1083984375,
-0.0170440673828125,
0.96875,
0.3759765625,
0.360595703125,
0.132080078125,
-0.25341796875,
-0.08453369140625,
-0.39599609375,
0.5009765625,
0.765625,
-0.0171966552734375,
-0.297607421875,
-0.440185546875,
0.14404296875,
-0.8271484375,
0.444091796875,
-0.53369140625,
0.00948333740234375,
0.432373046875,
-0.410400390625,
0.2266845703125,
1.33203125,
-0.26025390625,
0.333251953125,
0.31787109375,
-0.24658203125,
0.318359375,
0.46923828125,
0.158935546875,
0.434814453125,
-0.09552001953125,
-0.400146484375,
-0.087890625,
0.1253662109375,
0.2061767578125,
0.1734619140625,
-0.496337890625,
-0.64501953125,
-1.2119140625,
-0.205810546875,
0.1778564453125,
0.333984375,
0.43017578125,
-0.2305908203125,
-0.16845703125,
-0.2183837890625,
-0.4453125,
-0.139892578125,
-1.3076171875,
-1.3955078125,
0.54638671875,
-0.265625,
-0.8134765625,
-0.265625,
-0.301513671875,
-0.07470703125,
0.0002598762512207031,
0.3388671875,
-0.247314453125,
0.619140625,
-0.0748291015625,
0.6357421875,
0.030670166015625,
-0.199462890625,
0.2088623046875,
0.28564453125,
-1.0146484375,
-0.5556640625,
0.1444091796875,
1.5361328125,
0.117919921875,
-0.385498046875,
-0.41943359375,
0.0426025390625,
-0.077880859375,
0.1929931640625,
-0.5009765625,
0.097900390625,
-0.1915283203125,
0.29833984375,
-0.6708984375,
-0.400634765625,
-0.13720703125,
0.436279296875,
0.44189453125,
-0.5830078125,
-0.771484375,
0.81640625,
0.779296875,
-0.0787353515625,
0.412109375,
0.102294921875,
0.36474609375,
-0.043548583984375,
0.0391845703125,
-0.1864013671875,
0.6982421875,
1.0107421875,
0.7275390625,
-0.2305908203125,
0.53466796875,
0.61865234375,
-0.08599853515625,
0.11444091796875,
0.2744140625,
0.37109375,
0.01268768310546875,
-0.12139892578125,
-0.67431640625,
-0.491943359375,
-0.231689453125,
-0.56298828125,
0.10711669921875,
-0.20849609375,
0.10858154296875,
-0.133056640625,
-0.73681640625,
0.97119140625,
-0.279052734375,
-0.09783935546875,
0.294189453125,
-0.5009765625,
0.2401123046875,
0.8212890625,
-0.0791015625,
-0.448486328125,
0.71240234375,
0.88037109375,
0.46826171875,
0.642578125,
0.1385498046875,
0.274169921875,
-0.37890625,
0.1832275390625,
-0.181396484375,
0.035797119140625,
-0.58251953125,
-0.477294921875,
-1.0390625,
0.2230224609375,
-0.392822265625,
-0.2315673828125,
0.421142578125,
-0.53369140625,
0.04461669921875,
-0.10528564453125,
0.94287109375,
-0.70849609375,
0.314208984375,
0.4482421875,
-0.1793212890625,
-1.0224609375,
0.8720703125,
-0.18408203125,
0.2476806640625,
0.1351318359375,
0.83642578125,
0.053985595703125,
1.412109375,
0.64404296875,
-0.382568359375,
0.177001953125,
0.65673828125,
-0.1124267578125,
-0.603515625,
-0.986328125,
-0.1146240234375,
-0.25390625,
-0.8984375,
-0.2025146484375,
-0.2001953125,
0.8310546875,
-0.048828125,
-0.78857421875,
-0.1856689453125,
0.20166015625,
-1.0595703125,
1.044921875,
0.282958984375,
0.64404296875,
0.04296875,
-0.0304718017578125,
0.036163330078125,
-1.0576171875,
0.35498046875,
-0.6416015625,
0.57861328125,
-0.79296875,
-0.367919921875,
-1.109375,
-0.460693359375,
-0.06634521484375,
-0.48046875,
-0.423828125,
-1.1162109375,
0.279052734375,
0.560546875,
0.42822265625,
0.296875,
-0.371337890625,
0.390869140625,
1.2001953125,
0.7275390625,
-0.05596923828125,
0.54541015625,
0.52978515625,
0.1451416015625,
-0.388427734375,
0.10626220703125,
0.4375,
-0.057037353515625,
0.2071533203125,
-0.3759765625,
0.02252197265625,
-0.728515625,
-1.5654296875,
0.1439208984375,
0.6259765625,
0.3125,
-0.68017578125,
-0.84619140625,
-0.2344970703125,
-0.75732421875,
-0.2314453125,
-0.63330078125,
0.60595703125,
-0.1910400390625,
-0.0655517578125,
-0.36083984375,
-1.06640625,
4.171875,
0.943359375,
0.99755859375,
0.050079345703125,
0.54931640625,
1.1357421875,
0.095947265625,
-0.56787109375,
0.221923828125,
-0.4345703125,
0.391845703125,
-0.399658203125,
0.2122802734375,
0.73828125,
0.11724853515625,
0.8330078125,
-0.448486328125,
0.027313232421875,
0.09674072265625,
-1.236328125,
-1.0068359375,
0.496826171875,
-0.05908203125,
0.84375,
0.2135009765625,
0.36767578125,
0.53955078125,
-0.477294921875,
-0.1431884765625,
-0.481201171875,
0.28662109375,
-0.373779296875,
0.87451171875,
0.0733642578125,
-0.040069580078125,
0.444580078125,
-0.02264404296875,
-0.3349609375,
-0.042877197265625,
0.202392578125,
0.0706787109375,
0.2310791015625,
0.53662109375,
-1.126953125,
-0.28955078125,
0.58837890625,
-0.69091796875,
0.46240234375,
0.2021484375,
-1.0380859375,
0.751953125,
-0.265625,
0.445556640625,
-0.4345703125,
-0.83447265625,
0.0185394287109375,
-0.02947998046875,
-0.021575927734375,
0.04010009765625,
0.65771484375,
0.5654296875,
0.93115234375,
-0.1724853515625,
0.347900390625,
-1.2490234375,
0.51318359375,
0.3896484375,
0.63134765625,
-0.75439453125,
0.1932373046875,
-0.2342529296875,
-0.46826171875,
-0.319091796875,
-0.19091796875,
0.69287109375,
0.490966796875,
-0.24072265625,
0.7783203125,
0.49462890625,
-0.62841796875,
0.24658203125,
-0.62548828125,
-0.173095703125,
-0.7890625,
0.59228515625,
1.193359375,
-0.64794921875,
-0.490478515625,
-0.23876953125,
0.416748046875,
0.39013671875,
0.8896484375,
0.35107421875,
-0.77490234375,
-0.10040283203125
] | 0
|
End of preview. Expand
in Data Studio
- Downloads last month
- 1