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 Rust (1.15.1) (judge verdict: Accepted)

use std::cmp;

fn main() {
    let (r, w) = (std::io::stdin(), std::io::stdout());
    let mut sc = IO::new(r.lock(), w.lock());
    let s = sc.chars();
    let k: usize = sc.read();
    let n = s.len();
    let ans = solve(
        0,
        s.len(),
        k,
        &s,
        &mut vec![vec![vec![-1; k + 1]; n + 1]; n],
    );
    println!("{}", ans);
}

fn solve(l: usize, r: usize, k: usize, s: &Vec<char>, dp: &mut Vec<Vec<Vec<i64>>>) -> i64 {
    if dp[l][r][k] != -1 {
        return dp[l][r][k];
    }
    if r == l + 1 {
        return 1;
    }
    if r == l {
        return 0;
    }

    let mut ans = 0;
    ans = cmp::max(ans, solve(l, r - 1, k, s, dp));
    ans = cmp::max(ans, solve(l + 1, r, k, s, dp));

    if s[l] == s[r - 1] {
        ans = cmp::max(ans, solve(l + 1, r - 1, k, s, dp) + 2);
    } else if k > 0 {
        ans = cmp::max(ans, solve(l + 1, r - 1, k - 1, s, dp) + 2);
    }

    dp[l][r][k] = ans;
    ans
}

pub struct IO<R, W: std::io::Write>(R, std::io::BufWriter<W>);

impl<R: std::io::Read, W: std::io::Write> IO<R, W> {
    pub fn new(r: R, w: W) -> IO<R, W> {
        IO(r, std::io::BufWriter::new(w))
    }
    pub fn write<S: ToString>(&mut self, s: S) {
        use std::io::Write;
        self.1.write(s.to_string().as_bytes()).unwrap();
    }
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .0
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n' || b == b'\r' || b == b'\t')
            .take_while(|&b| b != b' ' && b != b'\n' && b != b'\r' && b != b'\t')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read()).collect()
    }
    pub fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}