## Algorithm

E. Logo Turtle
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A lot of people associate Logo programming language with turtle graphics. In this case the turtle moves along the straight line and accepts commands "T" ("turn around") and "F" ("move 1 unit forward").

You are given a list of commands that will be given to the turtle. You have to change exactly n commands from the list (one command can be changed several times). How far from the starting point can the turtle move after it follows all the commands of the modified list?

Input

The first line of input contains a string commands — the original list of commands. The string commands contains between 1 and 100 characters, inclusive, and contains only characters "T" and "F".

The second line contains an integer n (1 ≤ n ≤ 50) — the number of commands you have to change in the list.

Output

Output the maximum distance from the starting point to the ending point of the turtle's path. The ending point of the turtle's path is turtle's coordinate after it follows all the commands of the modified list.

Examples
input
Copy
FT
1
output
Copy
2
input
Copy
FFFTFFF
2
output
Copy
6
Note

In the first example the best option is to change the second command ("T") to "F" — this way the turtle will cover a distance of 2 units.

In the second example you have to change two commands. One of the ways to cover maximal distance of 6 units is to change the fourth command and first or last one.

## Code Examples

### #1 Code Example with C++ Programming

Code - C++ Programming

#include <stdio.h>
#include <algorithm>
#include <memory.h>
#include <string.h>

using namespace std;

char s[101];
int n, m, dp[101][101][101][51];

int rec(int idx, int a, int b, int cnt, int add, char lst) {
if(idx == m)
return cnt == n ? max(a, b) : -1e9;

if(cnt > n)
return -1e9;

int &ret = dp[idx][a][b][cnt];
if(ret != -1)
return ret;
ret = 0;

int r1, r2;
if(lst == 'F') {
r2 = rec(idx, a, b, cnt + 1, add, 'T');
} else {
r1 = rec(idx + 1, a, b, cnt, -add, s[idx + 1]);
r2 = rec(idx, a, b, cnt + 1, add, 'F');
}

return ret = max(r1, r2);
}

int main() {
scanf("%s\n%d", s, &n);
m = strlen(s);
memset(dp, -1, sizeof dp);
printf("%d\n", rec(0, 0, 0, 0, 1, s[0]));

return 0;
}
Copy The Code &

Input

cmd
FT
1

Output

cmd
2