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# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;

public class Program
{
    private string S;
    private int K;

    private int[,,] Memo;

    public void Solve()
    {
        var sc = new Scanner();
        S = sc.Next();
        K = sc.NextInt();
        Memo = new int[S.Length+1,S.Length+1,K+1];
        for (int i = 0; i <=S.Length ; i++)
        {
            for (int j = 0; j <=S.Length ; j++)
            {
                for (int k = 0; k <=K ; k++)
                {
                    Memo[i, j, k] = -1;
                }
            }
        }
        Console.WriteLine(Count(0, S.Length, K));
    }

    private int Count(int l, int r, int k)
    {
        if (r - l == 0) return 0;
        if (r - l == 1) return 1;
        if (Memo[l, r, k] != -1)
        {
            return Memo[l, r, k];
        }
        int result;
        if (S[l] == S[r - 1])
        {
            result = Count(l + 1, r - 1, k) + 2;
        }
        else
        {
            result = Math.Max(Count(l + 1, r, k), Count(l, r - 1, k));
            if (k > 0)
            {
                result = Math.Max(result, Count(l + 1, r - 1, k - 1) + 2);
            }
        }

        Memo[l, r, k] = result;
        return result;
    }

    public static void Main(string[] args)
    {
        new Program().Solve();
    }
}

class Scanner
{
    public Scanner()
    {
        _pos = 0;
        _line = new string[0];
    }

    const char Separator = ' ';
    private int _pos;
    private string[] _line;

    #region スペース区切りで取得

    public string Next()
    {
        if (_pos >= _line.Length)
        {
            _line = Console.ReadLine().Split(Separator);
            _pos = 0;
        }

        return _line[_pos++];
    }

    public int NextInt()
    {
        return int.Parse(Next());
    }

    public long NextLong()
    {
        return long.Parse(Next());
    }

    public double NextDouble()
    {
        return double.Parse(Next());
    }

    #endregion

    #region 型変換

    private int[] ToIntArray(string[] array)
    {
        var result = new int[array.Length];
        for (int i = 0; i < array.Length; i++)
        {
            result[i] = int.Parse(array[i]);
        }

        return result;
    }

    private long[] ToLongArray(string[] array)
    {
        var result = new long[array.Length];
        for (int i = 0; i < array.Length; i++)
        {
            result[i] = long.Parse(array[i]);
        }

        return result;
    }

    private double[] ToDoubleArray(string[] array)
    {
        var result = new double[array.Length];
        for (int i = 0; i < array.Length; i++)
        {
            result[i] = double.Parse(array[i]);
        }

        return result;
    }

    #endregion

    #region 配列取得

    public string[] Array()
    {
        if (_pos >= _line.Length)
            _line = Console.ReadLine().Split(Separator);

        _pos = _line.Length;
        return _line;
    }

    public int[] IntArray()
    {
        return ToIntArray(Array());
    }

    public long[] LongArray()
    {
        return ToLongArray(Array());
    }

    public double[] DoubleArray()
    {
        return ToDoubleArray(Array());
    }

    #endregion
}