Love Charm - VNOJ - VNOI Online Judge
Love Charm - VNOJ - VNOI Online Judge
Love Charm
Time limit: 3.0s Memory limit: 512M
Having lived nearly twenty years without a lover, Trung decided to go to the temple to seek love. After
completing all the rituals, Trung received a love charm. The charm contains a string of length n consisting of only
two letters O or K. With deep understanding of his own destiny, Trung knows that the effectiveness of the charm
depends on the consecutive pairs of letters. Specifically:
The pair OK has an effect, so if the charm has a pairs of OK, the effectiveness of the charm will increase by
a2.
The pair KO has a counter-effect, so if the charm has b pairs of KO, the effectiveness of the charm will
decrease by b2.
In other words, if the charm has a pairs of OK and b pairs of KO, the effectiveness of the charm will be a2 − b2. For
example, if the charm contains the string OOOKOO, then a = 1, b = 1, so the effectiveness of the charm will be
12 − 12 = 0.
Trung is not very satisfied with his charm, so he asked the temple for another charm. However, the temple only
allows Trung to change the current charm up to k times, each time Trung can choose any two letters on the
charm and swap them. Help Trung obtain a love charm with the maximum possible effectiveness!
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 ≤ t ≤ 10 000). The
description of each test case is as follows.
The first line contains two positive integers n and k (2 ≤ n ≤ 105, 1 ≤ k ≤ 10) — the length of the string written
on the charm and the maximum number of transformations.
The second line contains a string s consisting of n characters O or K, describing the string written on the charm.
The sum of n over all test cases is guaranteed not to exceed 105.
Output
For each test case, output an integer which is the maximum effectiveness the charm can achieve.
Scoring
https://oj.vnoi.info/problem/vnoicup24_r2_d 1/2
6/16/24, 8:01 PM Love Charm - VNOJ: VNOI Online Judge
2 750 k=1
Total 2250
Sample Input 1
5
2 1
KO
2 1
OK
7 1
OOOKKKK
7 1
KKOOKKK
10 2
KOOKOKKKOO
Sample Output 1
1
1
5
3
9
Notes
In the first test case, the optimal swapping sequence is KO → OK. The resulting charm has an effectiveness of 1.
In the second test case, it is optimal to keep the string as OK, with an effectiveness of 1.
In the fifth test case, an optimal swapping sequence is KOOKOKKKOO → OKOKOKKKOO → OKOKOKOKOK, resulting
in a charm with an effectiveness of 9.
https://oj.vnoi.info/problem/vnoicup24_r2_d 2/2