Problem:
Score : 900 points

Problem Statement

Takahashi has decided to give a string to his mother.

The value of a string T is the length of the longest common subsequence of T and T', where T' is the string obtained by reversing T.
That is, the value is the longest length of the following two strings that are equal: a subsequence of T (possibly non-contiguous), and a subsequence of T' (possibly non-contiguous).

Takahashi has a string S. He wants to give her mother a string of the highest possible value, so he would like to change at most K characters in S to any other characters in order to obtain a string of the highest possible value.
Find the highest possible value achievable.

Constraints

1 \leq |S| \leq 300

0 \leq K \leq |S|

S consists of lowercase English letters.

K is an integer.

Input

Input is given from Standard Input in the following format:

S
K

Output

Print the highest possible value achievable.

Sample Input 1

abcabcabc
1

Sample Output 1

7

Changing the first character to c results in cbcabcabc.
Let this tring be T, then one longest common subsequence of T and T' is cbabcbc, whose length is 7.

Sample Input 2

atcodergrandcontest
3

Sample Output 2

15

Solution in C (GCC 9.2.1) (judge verdict: Accepted)

#include <stdio.h>

int main()
{
	int K;
	char S[301];
	scanf("%s", S);
	scanf("%d", &K);
	
	int i, l;
	char T[301];
	for (l = 0; S[l] != 0; l++);
	for (i = 0, T[l] = 0; i < l; i++) T[i] = S[l-i-1];
	if (K >= l / 2) {
		printf("%d\n", l);
		fflush(stdout);
		return 0;
	}
	
	int j, k, dp[151][301][301] = {};
	for (i = 1; i <= l; i++) {
		for (j = 1; j <= l; j++) {
			for (k = 0; k <= K; k++) {
				if (dp[k][i-1][j] > dp[k][i][j]) dp[k][i][j] = dp[k][i-1][j];
				if (dp[k][i][j-1] > dp[k][i][j]) dp[k][i][j] = dp[k][i][j-1];
				if (S[i-1] == T[j-1] && dp[k][i-1][j-1] >= dp[k][i][j]) dp[k][i][j] = dp[k][i-1][j-1] + 1;
				if (dp[k][i-1][j-1] >= dp[k+1][i][j]) dp[k+1][i][j] = dp[k][i-1][j-1] + 1;
			}
		}
	}
	
	int ans = 0;
	for (i = 0; i <= l - 1; i++) {
		for (k = 0; k <= K; k++) {
			if (dp[k][i][l-i] * 2 > ans) ans = dp[k][i][l-i] * 2;
			if (dp[k][i][l-i-1] * 2 + 1 > ans) ans = dp[k][i][l-i-1] * 2 + 1;
		}
	}
	printf("%d\n", ans);
	fflush(stdout);
	return 0;
}