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::max;
use std::collections::BTreeMap;

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 ans = dfs(0, s.len(), k, &mut BTreeMap::new(), &s);
    println!("{}", ans);
}

fn dfs(
    left: usize,
    right: usize,
    remain: usize,
    dp: &mut BTreeMap<(usize, usize, usize), usize>,
    s: &Vec<char>,
) -> usize {
    assert!(left <= right);
    let segment_size = right - left;
    if segment_size <= 1 {
        return segment_size;
    }
    if let Some(&v) = dp.get(&(left, right, remain)) {
        return v;
    }

    let mut max_length = 0;
    if s[left] == s[right - 1] {
        max_length = max(max_length, dfs(left + 1, right - 1, remain, dp, s) + 2);
    } else if remain > 0 {
        max_length = max(max_length, dfs(left + 1, right - 1, remain - 1, dp, s) + 2);
    }
    max_length = max(max_length, dfs(left + 1, right, remain, dp, s));
    max_length = max(max_length, dfs(left, right - 1, remain, dp, s));
    dp.insert((left, right, remain), max_length);
    max_length
}

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) -> Self {
        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()
    }
}