#K93112. Minimum Deletions to Satisfy Frequency Constraint

    ID: 38348 Type: Default 1000ms 256MiB

Minimum Deletions to Satisfy Frequency Constraint

Minimum Deletions to Satisfy Frequency Constraint

You are given a string S and an integer K. Your task is to determine the minimum number of deletions required such that every character in the resulting string appears exactly at least K times.

If for any character in the original string, its frequency is less than K, then it is impossible to satisfy the condition and you should output -1.

Note: You are not allowed to completely remove a character from S. That is, every character originally present in S must appear in the final string and with at least K occurrences.

Formal Description:

Given a string \(S\) and an integer \(K\), if there exists any character in \(S\) whose frequency is less than \(K\), output \(-1\). Otherwise, for each character with frequency \(f\), you must delete exactly \(f - K\) occurrences so that its final frequency is \(K\). The result is the sum of these deletions over all characters.

inputFormat

The input consists of two lines:

  1. The first line contains the string \(S\) (a sequence of lowercase letters).
  2. The second line contains a single integer \(K\) representing the minimum required frequency for each character.

outputFormat

Output a single integer: the minimum number of deletions required to ensure that every character in \(S\) appears at least \(K\) times, or \(-1\) if it is impossible.

## sample
aabbcc
2
0

</p>