#P15879. [ICPC 2026 NAC] Evil Judges

[ICPC 2026 NAC] Evil Judges

Description

The evil judges preparing the ICPC problem sets are tired of seeing the talented contestants AC their problems and AK ("all kill") their problem sets. They have grown so tired of this that they've started to dislike any strings where AC\texttt{AC} or AK\texttt{AK} appear as subsequences. The judges just found a string ss consisting of only the characters A\texttt{A}, C\texttt{C}, and K\texttt{K} and are determined to destroy these subsequences!

In one operation, the judges are able to swap two adjacent characters in the string ss. To be more precise, the judges may choose an index ii (1i<s1 \le i < |s|) and swap sis_i and si+1s_{i+1}. The judges are very busy and don't have much time, so they can only do this operation up to MM times (independently choosing an index ii each time).

The judges' goal is to minimize the number of subsequences (possibly non-contiguous\textbf{possibly non-contiguous}) that are AC\texttt{AC} or AK\texttt{AK}. Help them calculate the number of AC\texttt{AC} and AK\texttt{AK} subsequences that remain within ss after the judges perform an optimal sequence of up to MM swap operations.

Input Format

The first line of input contains the string ss (1s2105)(1 \leq |s| \leq 2\cdot 10^5). The string consists only of the characters A\texttt{A}, C\texttt{C}, and K\texttt{K}.

The second line contains an integer MM (0M2109)(0 \leq M \leq 2\cdot 10^9), the maximum number of swaps the judges can perform.

Output Format

Print the minimum total number of AC\texttt{AC} and AK\texttt{AK} subsequences that remain in ss after an optimal sequence of at most MM swap operations.

ACAACCAK
5
6
CCKKAKCKCK
1000000000
0
AAAAAAAAACCCCCCCCC
13
68

Hint

Explanation of Sample Input 1

In Sample Input 1, the string ss initially has seven different AC\texttt{AC} subsequences and four different AK\texttt{AK} subsequences. One optimal choice of five swaps yields the new string ACCCAAKA\texttt{ACCCAAKA} which only has three AC\texttt{AC} subsequences and three AK\texttt{AK} subsequences left.