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: Wrong Answer)

#include <bits/stdc++.h>

using namespace std;

#define DBG cerr << '!' << endl;
#define REP(i,n) for(ll (i) = (0);(i) < (n);++i)
#define rep(i,s,g) for(ll (i) = (s);(i) < (g);++i)
#define rrep(i,s,g) for(ll (i) = (s);i >= (g);--(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {for(int W = 0;W < (n);W++)cerr << v[W] << ' ';cerr << endl << endl;}
#define SHOW2d(v,i,j) {for(int aaa = 0;aaa < i;aaa++){for(int bbb = 0;bbb < j;bbb++)cerr << v[aaa][bbb] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(10)
#define INF 1000000000

typedef long long ll;
typedef pair<ll,ll> P;

int dp[333][333][333];
string s;
int k;

int dfs(int K,int L,int R)
{
//	cout << K << ' ' << L << ' ' << R << endl;
	if(K < 0 || L > R)return 0;
	if(dp[K][L][R] != 0)return dp[K][L][R];
	int ret = 0;
	int p = 0;
	if(s[L] == s[R])p += 2;
	ret = max(ret,dfs(K,L+1,R-1)+p);
	ret = max(ret,dfs(K-1,L+1,R-1)+2);
	ret = max(ret,dfs(K,L+1,R));
	ret = max(ret,dfs(K,L,R-1));
	
	return dp[K][L][R] = ret;
}


int main()
{
	cin >> s >> k;
	
	REP(K,k)REP(i,333)dp[K][i][i] = 1;
	REP(K,k)REP(i,332)
	{
		if(s[i] == s[i+1])dp[K][i][i+1] = 2;
		else dp[K][i][i+1] = 1;
	}
	
	cout << dfs(k,0,s.size()-1) << endl;
	
	return 0;
}