cswork / OAOpen practice workspace

AmazonOA #175

Find Minimum Time Required (Amazon Bangalore :)

Estimated MediumJudge-ready

Topics (AI-labelled)GreedyHashing

Each character is a task that takes 1 unit of time, and identical characters belong to the same type. You may reorder the tasks freely and insert idle time; two tasks of the same type must be separated by at least minGap full time units. Find the minimum time to finish all tasks.

Input

The first line contains the lowercase task string; the second line contains minGap. This site uses 1≤string length≤100000, 0≤minGap≤10^9.

Output

Output a single integer, the answer.

Example 1

Input:

aaabbb
2

Output:

8

Example 2

Input:

aaabbb
0

Output:

6

Example 3

Input:

abacadaeafag
2

Output:

16

Example notes

Example 1: ab_ ab_ ab (spaces are only for grouping) takes 8 units, with 2 units between tasks of the same type. Example 2: there is no gap requirement, so the 6 tasks run back to back and the answer is 6. Example 3: a has 6 tasks, which need at least 16 units from the first to the last; other tasks can fill the gaps, so the answer is 16.

Editorial

Loading solution…