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++14 (GCC 5.4.1) (judge verdict: Accepted)

#include<bits/stdc++.h>
#define rg register
using namespace std;

const int N = 300+10;
char str[N];
int k,n;
int f[N][N][N];


int ck(int x,int y){
	if(x!=y && str[x]==str[y]) return 2;
	if(x==y) return 1;
	return 0;
}

int main(){
	//freopen("d.in","r",stdin);
	//freopen("d.out","w",stdout);
	scanf("%s",str+1);		
	n=strlen(str+1);
	scanf("%d",&k);

	int ans=0;
	for(int i=1; i<=n; ++i)
		for(int j=n; j>=i; --j)
			for(int t=0; t<=k; ++t){
				int x=i+1,y=j-1,c=(bool)(str[i]!=str[j]);
					if(x<=y&&t+c<=k) f[t+c][x][y]=max(f[t+c][x][y],f[t][i][j]+2);
					if(x-1==y){
						if(t+c<=k) ans=max(ans,f[t][i][j]+2);
					}
					if(x-2==y){
						if(t+c<=k) ans=max(ans,f[t][i][j]+1);
					}

					f[t][x][y]=max(f[t][x][y],f[t][i][j]);
					f[t][i][y]=max(f[t][i][y],f[t][i][j]);
					f[t][x][j]=max(f[t][x][j],f[t][i][j]);
					ans=max(ans,f[t][i][j]);
			}

	printf("%d\n",ans);
}